Don't discard parameters in digest groups when replying
[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 mark:         The article's mark.
1144 uncached:     Non-nil if the article is uncached."
1145   :group 'gnus-summary-visual
1146   :type '(repeat (cons (sexp :tag "Form" nil)
1147                        face)))
1148 (put 'gnus-summary-highlight 'risky-local-variable t)
1149
1150 (defcustom gnus-alter-header-function nil
1151   "Function called to allow alteration of article header structures.
1152 The function is called with one parameter, the article header vector,
1153 which it may alter in any way."
1154   :type '(choice (const :tag "None" nil)
1155                  function)
1156   :group 'gnus-summary)
1157
1158 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1159   "Function used to decode a string with encoded words.")
1160
1161 (defvar gnus-decode-encoded-address-function
1162   'mail-decode-encoded-address-string
1163   "Function used to decode addresses with encoded words.")
1164
1165 (defcustom gnus-extra-headers '(To Cc Keywords Gcc Newsgroups)
1166   "*Extra headers to parse."
1167   :version "24.1"                       ; added Cc Keywords Gcc
1168   :group 'gnus-summary
1169   :type '(repeat symbol))
1170
1171 (defcustom gnus-ignored-from-addresses
1172   (and user-mail-address
1173        (not (string= user-mail-address ""))
1174        (regexp-quote user-mail-address))
1175   "*From headers that may be suppressed in favor of To headers.
1176 This can be a regexp or a list of regexps."
1177   :version "21.1"
1178   :group 'gnus-summary
1179   :type '(choice regexp
1180                  (repeat :tag "Regexp List" regexp)))
1181
1182 (defsubst gnus-ignored-from-addresses ()
1183   (gmm-regexp-concat gnus-ignored-from-addresses))
1184
1185 (defcustom gnus-summary-to-prefix "-> "
1186   "*String prefixed to the To field in the summary line when
1187 using `gnus-ignored-from-addresses'."
1188   :version "22.1"
1189   :group 'gnus-summary
1190   :type 'string)
1191
1192 (defcustom gnus-summary-newsgroup-prefix "=> "
1193   "*String prefixed to the Newsgroup field in the summary
1194 line when using the option `gnus-ignored-from-addresses'."
1195   :version "22.1"
1196   :group 'gnus-summary
1197   :type 'string)
1198
1199 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1200   "List of charsets that should be ignored.
1201 When these charsets are used in the \"charset\" parameter, the
1202 default charset will be used instead."
1203   :version "21.1"
1204   :type '(repeat symbol)
1205   :group 'gnus-charset)
1206
1207 (defcustom gnus-newsgroup-maximum-articles nil
1208   "The maximum number of articles a newsgroup.
1209 If this is a number, old articles in a newsgroup exceeding this number
1210 are silently ignored.  If it is nil, no article is ignored.  Note that
1211 setting this variable to a number might prevent you from reading very
1212 old articles."
1213   :group 'gnus-group-select
1214   :version "22.2"
1215   :type '(choice (const :tag "No limit" nil)
1216                  integer))
1217
1218 (gnus-define-group-parameter
1219  ignored-charsets
1220  :type list
1221  :function-document
1222  "Return the ignored charsets of GROUP."
1223  :variable gnus-group-ignored-charsets-alist
1224  :variable-default
1225  '(("alt\\.chinese\\.text" iso-8859-1))
1226  :variable-document
1227  "Alist of regexps (to match group names) and charsets that should be ignored.
1228 When these charsets are used in the \"charset\" parameter, the
1229 default charset will be used instead."
1230  :variable-group gnus-charset
1231  :variable-type '(repeat (cons (regexp :tag "Group")
1232                                (repeat symbol)))
1233  :parameter-type '(choice :tag "Ignored charsets"
1234                           :value nil
1235                           (repeat (symbol)))
1236  :parameter-document       "\
1237 List of charsets that should be ignored.
1238
1239 When these charsets are used in the \"charset\" parameter, the
1240 default charset will be used instead.")
1241
1242 (defcustom gnus-group-highlight-words-alist nil
1243   "Alist of group regexps and highlight regexps.
1244 This variable uses the same syntax as `gnus-emphasis-alist'."
1245   :version "21.1"
1246   :type '(repeat (cons (regexp :tag "Group")
1247                        (repeat (list (regexp :tag "Highlight regexp")
1248                                      (number :tag "Group for entire word" 0)
1249                                      (number :tag "Group for displayed part" 0)
1250                                      (symbol :tag "Face"
1251                                              gnus-emphasis-highlight-words)))))
1252   :group 'gnus-summary-visual)
1253
1254 (defcustom gnus-summary-show-article-charset-alist
1255   nil
1256   "Alist of number and charset.
1257 The article will be shown with the charset corresponding to the
1258 numbered argument.
1259 For example: ((1 . cn-gb-2312) (2 . big5))."
1260   :version "21.1"
1261   :type '(repeat (cons (number :tag "Argument" 1)
1262                        (symbol :tag "Charset")))
1263   :group 'gnus-charset)
1264
1265 (defcustom gnus-preserve-marks t
1266   "Whether marks are preserved when moving, copying and respooling messages."
1267   :version "21.1"
1268   :type 'boolean
1269   :group 'gnus-summary-marks)
1270
1271 (defcustom gnus-alter-articles-to-read-function nil
1272   "Function to be called to alter the list of articles to be selected."
1273   :type '(choice (const nil) function)
1274   :group 'gnus-summary)
1275
1276 (defcustom gnus-orphan-score nil
1277   "*All orphans get this score added.  Set in the score file."
1278   :group 'gnus-score-default
1279   :type '(choice (const nil)
1280                  integer))
1281
1282 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1283   "*A regexp to match MIME parts when saving multiple parts of a
1284 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1285 This regexp will be used by default when prompting the user for which
1286 type of files to save."
1287   :group 'gnus-summary
1288   :type 'regexp)
1289
1290 (defcustom gnus-read-all-available-headers nil
1291   "Whether Gnus should parse all headers made available to it.
1292 This is mostly relevant for slow back ends where the user may
1293 wish to widen the summary buffer to include all headers
1294 that were fetched."
1295   :version "22.1"
1296   :group 'gnus-summary
1297   :type '(choice boolean regexp))
1298
1299 (defcustom gnus-summary-pipe-output-default-command nil
1300   "Command (and optional arguments) used to pipe article to subprocess.
1301 This will be used as the default command if it is non-nil.  The value
1302 will be updated if you modify it when executing the command
1303 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1304   :version "23.1" ;; No Gnus
1305   :group 'gnus-summary
1306   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1307
1308 (defcustom gnus-summary-muttprint-program "muttprint"
1309   "Command (and optional arguments) used to run Muttprint.
1310 The value will be updated if you modify it when executing the command
1311 `gnus-summary-muttprint'."
1312   :version "22.1"
1313   :group 'gnus-summary
1314   :type 'string)
1315
1316 (defcustom gnus-article-loose-mime t
1317   "If non-nil, don't require MIME-Version header.
1318 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1319 supply the MIME-Version header or deliberately strip it from the mail.
1320 If non-nil (the default), Gnus will treat some articles as MIME
1321 even if the MIME-Version header is missing."
1322   :version "22.1"
1323   :type 'boolean
1324   :group 'gnus-article-mime)
1325
1326 (defcustom gnus-article-emulate-mime t
1327   "If non-nil, use MIME emulation for uuencode and the like.
1328 This means that Gnus will search message bodies for text that look
1329 like uuencoded bits, yEncoded bits, and so on, and present that using
1330 the normal Gnus MIME machinery."
1331   :version "22.1"
1332   :type 'boolean
1333   :group 'gnus-article-mime)
1334
1335 ;;; Internal variables
1336
1337 (defvar gnus-summary-display-cache nil)
1338 (defvar gnus-article-mime-handles nil)
1339 (defvar gnus-article-decoded-p nil)
1340 (defvar gnus-article-charset nil)
1341 (defvar gnus-article-ignored-charsets nil)
1342 (defvar gnus-scores-exclude-files nil)
1343 (defvar gnus-page-broken nil)
1344
1345 (defvar gnus-original-article nil)
1346 (defvar gnus-article-internal-prepare-hook nil)
1347 (defvar gnus-newsgroup-process-stack nil)
1348
1349 (defvar gnus-thread-indent-array nil)
1350 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1351 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1352   "Function called to sort the articles within a thread after it has been gathered together.")
1353
1354 (defvar gnus-summary-save-parts-type-history nil)
1355 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1356
1357 ;; Avoid highlighting in kill files.
1358 (defvar gnus-summary-inhibit-highlight nil)
1359 (defvar gnus-newsgroup-selected-overlay nil)
1360 (defvar gnus-inhibit-limiting nil)
1361 (defvar gnus-newsgroup-adaptive-score-file nil)
1362 (defvar gnus-current-score-file nil)
1363 (defvar gnus-current-move-group nil)
1364 (defvar gnus-current-copy-group nil)
1365 (defvar gnus-current-crosspost-group nil)
1366 (defvar gnus-newsgroup-display nil)
1367
1368 (defvar gnus-newsgroup-dependencies nil)
1369 (defvar gnus-newsgroup-adaptive nil)
1370 (defvar gnus-summary-display-article-function nil)
1371 (defvar gnus-summary-highlight-line-function nil
1372   "Function called after highlighting a summary line.")
1373
1374 (defvar gnus-summary-line-format-alist
1375   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1376     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1377     (?s gnus-tmp-subject-or-nil ?s)
1378     (?n gnus-tmp-name ?s)
1379     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1380         ?s)
1381     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1382             gnus-tmp-from) ?s)
1383     (?F gnus-tmp-from ?s)
1384     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1385     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1386     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1387     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1388     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1389     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1390     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1391     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1392     (?L gnus-tmp-lines ?s)
1393     (?Z (or (nnir-article-rsv (mail-header-number gnus-tmp-header))
1394             0) ?d)
1395     (?G (or (nnir-article-group (mail-header-number gnus-tmp-header))
1396             "") ?s)
1397     (?g (or (gnus-group-short-name
1398              (nnir-article-group (mail-header-number gnus-tmp-header)))
1399             "") ?s)
1400     (?O gnus-tmp-downloaded ?c)
1401     (?I gnus-tmp-indentation ?s)
1402     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1403     (?R gnus-tmp-replied ?c)
1404     (?\[ gnus-tmp-opening-bracket ?c)
1405     (?\] gnus-tmp-closing-bracket ?c)
1406     (?\> (make-string gnus-tmp-level ? ) ?s)
1407     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1408     (?i gnus-tmp-score ?d)
1409     (?z gnus-tmp-score-char ?c)
1410     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1411     (?U gnus-tmp-unread ?c)
1412     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1413         ?s)
1414     (?t (gnus-summary-number-of-articles-in-thread
1415          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1416         ?d)
1417     (?e (gnus-summary-number-of-articles-in-thread
1418          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1419         ?c)
1420     (?u gnus-tmp-user-defined ?s)
1421     (?P (gnus-pick-line-number) ?d)
1422     (?B gnus-tmp-thread-tree-header-string ?s)
1423     (user-date (gnus-user-date
1424                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1425   "An alist of format specifications that can appear in summary lines.
1426 These are paired with what variables they correspond with, along with
1427 the type of the variable (string, integer, character, etc).")
1428
1429 (defvar gnus-summary-dummy-line-format-alist
1430   `((?S gnus-tmp-subject ?s)
1431     (?N gnus-tmp-number ?d)
1432     (?u gnus-tmp-user-defined ?s)))
1433
1434 (defvar gnus-summary-mode-line-format-alist
1435   `((?G gnus-tmp-group-name ?s)
1436     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1437     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1438     (?A gnus-tmp-article-number ?d)
1439     (?Z gnus-tmp-unread-and-unselected ?s)
1440     (?V gnus-version ?s)
1441     (?U gnus-tmp-unread-and-unticked ?d)
1442     (?S gnus-tmp-subject ?s)
1443     (?e gnus-tmp-unselected ?d)
1444     (?u gnus-tmp-user-defined ?s)
1445     (?d (length gnus-newsgroup-dormant) ?d)
1446     (?t (length gnus-newsgroup-marked) ?d)
1447     (?h (length gnus-newsgroup-spam-marked) ?d)
1448     (?r (length gnus-newsgroup-reads) ?d)
1449     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1450     (?E gnus-newsgroup-expunged-tally ?d)
1451     (?s (gnus-current-score-file-nondirectory) ?s)))
1452
1453 ;; This is here rather than in gnus-art for compilation reasons.
1454 (defvar gnus-article-mode-line-format-alist
1455   (nconc '((?w (gnus-article-wash-status) ?s)
1456            (?m (gnus-article-mime-part-status) ?s))
1457          gnus-summary-mode-line-format-alist))
1458
1459 (defvar gnus-last-search-regexp nil
1460   "Default regexp for article search command.")
1461
1462 (defvar gnus-last-shell-command nil
1463   "Default shell command on article.")
1464
1465 (defvar gnus-newsgroup-agentized nil
1466   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1467 (defvar gnus-newsgroup-begin nil)
1468 (defvar gnus-newsgroup-end nil)
1469 (defvar gnus-newsgroup-last-rmail nil)
1470 (defvar gnus-newsgroup-last-mail nil)
1471 (defvar gnus-newsgroup-last-folder nil)
1472 (defvar gnus-newsgroup-last-file nil)
1473 (defvar gnus-newsgroup-last-directory nil)
1474 (defvar gnus-newsgroup-auto-expire nil)
1475 (defvar gnus-newsgroup-active nil)
1476 (defvar gnus-newsgroup-highest nil)
1477
1478 (defvar gnus-newsgroup-data nil)
1479 (defvar gnus-newsgroup-data-reverse nil)
1480 (defvar gnus-newsgroup-limit nil)
1481 (defvar gnus-newsgroup-limits nil)
1482 (defvar gnus-summary-use-undownloaded-faces nil)
1483
1484 (defvar gnus-newsgroup-unreads nil
1485   "Sorted list of unread articles in the current newsgroup.")
1486
1487 (defvar gnus-newsgroup-unselected nil
1488   "Sorted list of unselected unread articles in the current newsgroup.")
1489
1490 (defvar gnus-newsgroup-reads nil
1491   "Alist of read articles and article marks in the current newsgroup.")
1492
1493 (defvar gnus-newsgroup-expunged-tally nil)
1494
1495 (defvar gnus-newsgroup-marked nil
1496   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1497
1498 (defvar gnus-newsgroup-spam-marked nil
1499   "List of ranges of articles that have been marked as spam.")
1500
1501 (defvar gnus-newsgroup-killed nil
1502   "List of ranges of articles that have been through the scoring process.")
1503
1504 (defvar gnus-newsgroup-cached nil
1505   "Sorted list of articles that come from the article cache.")
1506
1507 (defvar gnus-newsgroup-saved nil
1508   "List of articles that have been saved.")
1509
1510 (defvar gnus-newsgroup-kill-headers nil)
1511
1512 (defvar gnus-newsgroup-replied nil
1513   "List of articles that have been replied to in the current newsgroup.")
1514
1515 (defvar gnus-newsgroup-forwarded nil
1516   "List of articles that have been forwarded in the current newsgroup.")
1517
1518 (defvar gnus-newsgroup-expirable nil
1519   "Sorted list of articles in the current newsgroup that can be expired.")
1520
1521 (defvar gnus-newsgroup-processable nil
1522   "List of articles in the current newsgroup that can be processed.")
1523
1524 (defvar gnus-newsgroup-downloadable nil
1525   "Sorted list of articles in the current newsgroup that can be processed.")
1526
1527 (defvar gnus-newsgroup-unfetched nil
1528   "Sorted list of articles in the current newsgroup whose headers have
1529 not been fetched into the agent.
1530
1531 This list will always be a subset of gnus-newsgroup-undownloaded.")
1532
1533 (defvar gnus-newsgroup-undownloaded nil
1534   "List of articles in the current newsgroup that haven't been downloaded.")
1535
1536 (defvar gnus-newsgroup-unsendable nil
1537   "List of articles in the current newsgroup that won't be sent.")
1538
1539 (defvar gnus-newsgroup-bookmarks nil
1540   "List of articles in the current newsgroup that have bookmarks.")
1541
1542 (defvar gnus-newsgroup-dormant nil
1543   "Sorted list of dormant articles in the current newsgroup.")
1544
1545 (defvar gnus-newsgroup-unseen nil
1546   "List of unseen articles in the current newsgroup.")
1547
1548 (defvar gnus-newsgroup-seen nil
1549   "Range of seen articles in the current newsgroup.")
1550
1551 (defvar gnus-newsgroup-unexist nil
1552   "Range of unexisting articles in the current newsgroup.")
1553
1554 (defvar gnus-newsgroup-articles nil
1555   "List of articles in the current newsgroup.")
1556
1557 (defvar gnus-newsgroup-scored nil
1558   "List of scored articles in the current newsgroup.")
1559
1560 (defvar gnus-newsgroup-headers nil
1561   "List of article headers in the current newsgroup.")
1562
1563 (defvar gnus-newsgroup-threads nil)
1564
1565 (defvar gnus-newsgroup-prepared nil
1566   "Whether the current group has been prepared properly.")
1567
1568 (defvar gnus-newsgroup-ancient nil
1569   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1570
1571 (defvar gnus-newsgroup-sparse nil)
1572
1573 (defvar gnus-current-article nil)
1574 (defvar gnus-article-current nil)
1575 (defvar gnus-current-headers nil)
1576 (defvar gnus-have-all-headers nil)
1577 (defvar gnus-last-article nil)
1578 (defvar gnus-newsgroup-history nil)
1579 (defvar gnus-newsgroup-charset nil)
1580 (defvar gnus-newsgroup-ephemeral-charset nil)
1581 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1582
1583 (defvar gnus-article-before-search nil)
1584
1585 (defvar gnus-summary-local-variables
1586   '(gnus-newsgroup-name
1587
1588     ;; Marks lists
1589     gnus-newsgroup-unreads
1590     gnus-newsgroup-unselected
1591     gnus-newsgroup-marked
1592     gnus-newsgroup-spam-marked
1593     gnus-newsgroup-reads
1594     gnus-newsgroup-saved
1595     gnus-newsgroup-replied
1596     gnus-newsgroup-forwarded
1597     gnus-newsgroup-expirable
1598     gnus-newsgroup-killed
1599     gnus-newsgroup-unseen
1600     gnus-newsgroup-seen
1601     gnus-newsgroup-unexist
1602     gnus-newsgroup-cached
1603     gnus-newsgroup-downloadable
1604     gnus-newsgroup-undownloaded
1605     gnus-newsgroup-unsendable
1606
1607     gnus-newsgroup-begin gnus-newsgroup-end
1608     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1609     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1610     gnus-newsgroup-last-directory
1611     gnus-newsgroup-auto-expire
1612     gnus-newsgroup-processable
1613     gnus-newsgroup-unfetched
1614     gnus-newsgroup-articles
1615     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1616     gnus-newsgroup-headers gnus-newsgroup-threads
1617     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1618     gnus-current-article gnus-current-headers gnus-have-all-headers
1619     gnus-last-article gnus-article-internal-prepare-hook
1620     (gnus-summary-article-delete-hook . global)
1621     (gnus-summary-article-move-hook . global)
1622     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1623     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1624     gnus-thread-expunge-below
1625     gnus-score-alist gnus-current-score-file
1626     (gnus-summary-expunge-below . global)
1627     (gnus-summary-mark-below . global)
1628     (gnus-orphan-score . global)
1629     gnus-newsgroup-active gnus-scores-exclude-files
1630     gnus-newsgroup-highest
1631     gnus-newsgroup-history gnus-newsgroup-ancient
1632     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1633     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1634     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1635     (gnus-newsgroup-expunged-tally . 0)
1636     gnus-cache-removable-articles
1637     gnus-newsgroup-data gnus-newsgroup-data-reverse
1638     gnus-newsgroup-limit gnus-newsgroup-limits
1639     gnus-newsgroup-charset gnus-newsgroup-display
1640     gnus-summary-use-undownloaded-faces)
1641   "Variables that are buffer-local to the summary buffers.")
1642
1643 (defvar gnus-newsgroup-variables nil
1644   "A list of variables that have separate values in different newsgroups.
1645 A list of newsgroup (summary buffer) local variables, or cons of
1646 variables and their default expressions to be evalled (when the default
1647 values are not nil), that should be made global while the summary buffer
1648 is active.
1649
1650 Note: The default expressions will be evaluated (using function `eval')
1651 before assignment to the local variable rather than just assigned to it.
1652 If the default expression is the symbol `global', that symbol will not
1653 be evaluated but the global value of the local variable will be used
1654 instead.
1655
1656 These variables can be used to set variables in the group parameters
1657 while still allowing them to affect operations done in other buffers.
1658 For example:
1659
1660 \(setq gnus-newsgroup-variables
1661      '(message-use-followup-to
1662        (gnus-visible-headers .
1663          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1664 ")
1665
1666 (eval-when-compile
1667   ;; Bind features so that require will believe that gnus-sum has
1668   ;; already been loaded (avoids infinite recursion)
1669   (let ((features (cons 'gnus-sum features)))
1670     (require 'gnus-art)))
1671
1672 ;; MIME stuff.
1673
1674 (defvar gnus-decode-encoded-word-methods
1675   '(mail-decode-encoded-word-string)
1676   "List of methods used to decode encoded words.
1677
1678 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1679 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1680 \(REGEXP . FUNCTION), FUNCTION will be applied only to the newsgroups
1681 whose names match REGEXP.
1682
1683 For example:
1684 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1685  mail-decode-encoded-word-string
1686  (\"chinese\" . rfc1843-decode-string))")
1687
1688 (defvar gnus-decode-encoded-word-methods-cache nil)
1689
1690 (defun gnus-multi-decode-encoded-word-string (string)
1691   "Apply the functions from `gnus-encoded-word-methods' that match."
1692   (unless (and gnus-decode-encoded-word-methods-cache
1693                (eq gnus-newsgroup-name
1694                    (car gnus-decode-encoded-word-methods-cache)))
1695     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1696     (dolist (method gnus-decode-encoded-word-methods)
1697       (if (symbolp method)
1698           (nconc gnus-decode-encoded-word-methods-cache (list method))
1699         (if (and gnus-newsgroup-name
1700                  (string-match (car method) gnus-newsgroup-name))
1701             (nconc gnus-decode-encoded-word-methods-cache
1702                    (list (cdr method)))))))
1703   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1704     (setq string (funcall method string))))
1705
1706 ;; Subject simplification.
1707
1708 (defun gnus-simplify-whitespace (str)
1709   "Remove excessive whitespace from STR."
1710   ;; Multiple spaces.
1711   (while (string-match "[ \t][ \t]+" str)
1712     (setq str (concat (substring str 0 (match-beginning 0))
1713                         " "
1714                         (substring str (match-end 0)))))
1715   ;; Leading spaces.
1716   (when (string-match "^[ \t]+" str)
1717     (setq str (substring str (match-end 0))))
1718   ;; Trailing spaces.
1719   (when (string-match "[ \t]+$" str)
1720     (setq str (substring str 0 (match-beginning 0))))
1721   str)
1722
1723 (defun gnus-simplify-all-whitespace (str)
1724   "Remove all whitespace from STR."
1725   (while (string-match "[ \t\n]+" str)
1726     (setq str (replace-match "" nil nil str)))
1727   str)
1728
1729 (defsubst gnus-simplify-subject-re (subject)
1730   "Remove \"Re:\" from subject lines."
1731   (if (string-match message-subject-re-regexp subject)
1732       (substring subject (match-end 0))
1733     subject))
1734
1735 (defun gnus-simplify-subject (subject &optional re-only)
1736   "Remove `Re:' and words in parentheses.
1737 If RE-ONLY is non-nil, strip leading `Re:'s only."
1738   (let ((case-fold-search t))           ;Ignore case.
1739     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1740     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1741       (setq subject (substring subject (match-end 0))))
1742     ;; Remove uninteresting prefixes.
1743     (when (and (not re-only)
1744                gnus-simplify-ignored-prefixes
1745                (string-match gnus-simplify-ignored-prefixes subject))
1746       (setq subject (substring subject (match-end 0))))
1747     ;; Remove words in parentheses from end.
1748     (unless re-only
1749       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1750         (setq subject (substring subject 0 (match-beginning 0)))))
1751     ;; Return subject string.
1752     subject))
1753
1754 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1755 ;; all whitespace.
1756 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1757   (goto-char (point-min))
1758   (while (re-search-forward regexp nil t)
1759     (replace-match (or newtext ""))))
1760
1761 (defun gnus-simplify-buffer-fuzzy (regexp)
1762   "Simplify string in the buffer fuzzily.
1763 The string in the accessible portion of the current buffer is simplified.
1764 It is assumed to be a single-line subject.
1765 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1766 matter is removed.  Additional things can be deleted by setting
1767 `gnus-simplify-subject-fuzzy-regexp'."
1768   (let ((case-fold-search t)
1769         (modified-tick))
1770     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1771
1772     (while (not (eq modified-tick (buffer-modified-tick)))
1773       (setq modified-tick (buffer-modified-tick))
1774       (cond
1775        ((listp regexp)
1776         (mapc 'gnus-simplify-buffer-fuzzy-step regexp))
1777        (regexp
1778         (gnus-simplify-buffer-fuzzy-step regexp)))
1779       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1780       (gnus-simplify-buffer-fuzzy-step
1781        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1782       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1783
1784     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1785     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1786     (gnus-simplify-buffer-fuzzy-step " $")
1787     (gnus-simplify-buffer-fuzzy-step "^ +")))
1788
1789 (defun gnus-simplify-subject-fuzzy (subject)
1790   "Simplify a subject string fuzzily.
1791 See `gnus-simplify-buffer-fuzzy' for details."
1792   (save-excursion
1793     (let ((regexp gnus-simplify-subject-fuzzy-regexp))
1794       (gnus-set-work-buffer)
1795       (let ((case-fold-search t))
1796         ;; Remove uninteresting prefixes.
1797         (when (and gnus-simplify-ignored-prefixes
1798                    (string-match gnus-simplify-ignored-prefixes subject))
1799           (setq subject (substring subject (match-end 0))))
1800         (insert subject)
1801         (inline (gnus-simplify-buffer-fuzzy regexp))
1802         (buffer-string)))))
1803
1804 (defsubst gnus-simplify-subject-fully (subject)
1805   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1806   (cond
1807    (gnus-simplify-subject-functions
1808     (gnus-map-function gnus-simplify-subject-functions subject))
1809    ((null gnus-summary-gather-subject-limit)
1810     (gnus-simplify-subject-re subject))
1811    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1812     (gnus-simplify-subject-fuzzy subject))
1813    ((numberp gnus-summary-gather-subject-limit)
1814     (truncate-string-to-width (gnus-simplify-subject-re subject)
1815                               gnus-summary-gather-subject-limit))
1816    (t
1817     subject)))
1818
1819 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1820   "Check whether two subjects are equal.
1821 If optional argument SIMPLE-FIRST is t, first argument is already
1822 simplified."
1823   (cond
1824    ((null simple-first)
1825     (equal (gnus-simplify-subject-fully s1)
1826            (gnus-simplify-subject-fully s2)))
1827    (t
1828     (equal s1
1829            (gnus-simplify-subject-fully s2)))))
1830
1831 (defun gnus-summary-bubble-group ()
1832   "Increase the score of the current group.
1833 This is a handy function to add to `gnus-summary-exit-hook' to
1834 increase the score of each group you read."
1835   (gnus-group-add-score gnus-newsgroup-name))
1836
1837 \f
1838 ;;;
1839 ;;; Gnus summary mode
1840 ;;;
1841
1842 (put 'gnus-summary-mode 'mode-class 'special)
1843
1844 (defvar gnus-article-commands-menu)
1845
1846 ;; Non-orthogonal keys
1847
1848 (gnus-define-keys gnus-summary-mode-map
1849   " " gnus-summary-next-page
1850   [?\S-\ ] gnus-summary-prev-page
1851   "\177" gnus-summary-prev-page
1852   [delete] gnus-summary-prev-page
1853   "\r" gnus-summary-scroll-up
1854   "\M-\r" gnus-summary-scroll-down
1855   "n" gnus-summary-next-unread-article
1856   "p" gnus-summary-prev-unread-article
1857   "N" gnus-summary-next-article
1858   "P" gnus-summary-prev-article
1859   "\M-\C-n" gnus-summary-next-same-subject
1860   "\M-\C-p" gnus-summary-prev-same-subject
1861   "\M-n" gnus-summary-next-unread-subject
1862   "\M-p" gnus-summary-prev-unread-subject
1863   "." gnus-summary-first-unread-article
1864   "," gnus-summary-best-unread-article
1865   "\M-s" gnus-summary-search-article-forward
1866   "\M-r" gnus-summary-search-article-backward
1867   "\M-S" gnus-summary-repeat-search-article-forward
1868   "\M-R" gnus-summary-repeat-search-article-backward
1869   "<" gnus-summary-beginning-of-article
1870   ">" gnus-summary-end-of-article
1871   "j" gnus-summary-goto-article
1872   "^" gnus-summary-refer-parent-article
1873   "\M-^" gnus-summary-refer-article
1874   "u" gnus-summary-tick-article-forward
1875   "!" gnus-summary-tick-article-forward
1876   "U" gnus-summary-tick-article-backward
1877   "d" gnus-summary-mark-as-read-forward
1878   "D" gnus-summary-mark-as-read-backward
1879   "E" gnus-summary-mark-as-expirable
1880   "\M-u" gnus-summary-clear-mark-forward
1881   "\M-U" gnus-summary-clear-mark-backward
1882   "k" gnus-summary-kill-same-subject-and-select
1883   "\C-k" gnus-summary-kill-same-subject
1884   "\M-\C-k" gnus-summary-kill-thread
1885   "\M-\C-l" gnus-summary-lower-thread
1886   "e" gnus-summary-edit-article
1887   "#" gnus-summary-mark-as-processable
1888   "\M-#" gnus-summary-unmark-as-processable
1889   "\M-\C-t" gnus-summary-toggle-threads
1890   "\M-\C-s" gnus-summary-show-thread
1891   "\M-\C-h" gnus-summary-hide-thread
1892   "\M-\C-f" gnus-summary-next-thread
1893   "\M-\C-b" gnus-summary-prev-thread
1894   [(meta down)] gnus-summary-next-thread
1895   [(meta up)] gnus-summary-prev-thread
1896   "\M-\C-u" gnus-summary-up-thread
1897   "\M-\C-d" gnus-summary-down-thread
1898   "&" gnus-summary-execute-command
1899   "c" gnus-summary-catchup-and-exit
1900   "\C-w" gnus-summary-mark-region-as-read
1901   "\C-t" gnus-summary-toggle-truncation
1902   "?" gnus-summary-mark-as-dormant
1903   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1904   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1905   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1906   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1907   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1908   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1909   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1910   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1911   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1912   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1913   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1914   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1915   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1916   "=" gnus-summary-expand-window
1917   "\C-x\C-s" gnus-summary-reselect-current-group
1918   "\M-g" gnus-summary-rescan-group
1919   "\C-c\C-r" gnus-summary-caesar-message
1920   "f" gnus-summary-followup
1921   "F" gnus-summary-followup-with-original
1922   "C" gnus-summary-cancel-article
1923   "r" gnus-summary-reply
1924   "R" gnus-summary-reply-with-original
1925   "\C-c\C-f" gnus-summary-mail-forward
1926   "o" gnus-summary-save-article
1927   "\C-o" gnus-summary-save-article-mail
1928   "|" gnus-summary-pipe-output
1929   "\M-k" gnus-summary-edit-local-kill
1930   "\M-K" gnus-summary-edit-global-kill
1931   ;; "V" gnus-version
1932   "\C-c\C-d" gnus-summary-describe-group
1933   "q" gnus-summary-exit
1934   "Q" gnus-summary-exit-no-update
1935   "\C-c\C-i" gnus-info-find-node
1936   gnus-mouse-2 gnus-mouse-pick-article
1937   [follow-link] mouse-face
1938   "m" gnus-summary-mail-other-window
1939   "a" gnus-summary-post-news
1940   "x" gnus-summary-limit-to-unread
1941   "s" gnus-summary-isearch-article
1942   "\t" gnus-summary-widget-forward
1943   [backtab] gnus-summary-widget-backward
1944   "t" gnus-summary-toggle-header
1945   "g" gnus-summary-show-article
1946   "l" gnus-summary-goto-last-article
1947   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1948   "\C-d" gnus-summary-enter-digest-group
1949   "\M-\C-d" gnus-summary-read-document
1950   "\M-\C-e" gnus-summary-edit-parameters
1951   "\M-\C-a" gnus-summary-customize-parameters
1952   "\C-c\C-b" gnus-bug
1953   "*" gnus-cache-enter-article
1954   "\M-*" gnus-cache-remove-article
1955   "\M-&" gnus-summary-universal-argument
1956   "\C-l" gnus-recenter
1957   "I" gnus-summary-increase-score
1958   "L" gnus-summary-lower-score
1959   "\M-i" gnus-symbolic-argument
1960   "h" gnus-summary-select-article-buffer
1961
1962   "b" gnus-article-view-part
1963   "\M-t" gnus-summary-toggle-display-buttonized
1964
1965   "V" gnus-summary-score-map
1966   "X" gnus-uu-extract-map
1967   "S" gnus-summary-send-map)
1968
1969 ;; Sort of orthogonal keymap
1970 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1971   "t" gnus-summary-tick-article-forward
1972   "!" gnus-summary-tick-article-forward
1973   "d" gnus-summary-mark-as-read-forward
1974   "r" gnus-summary-mark-as-read-forward
1975   "c" gnus-summary-clear-mark-forward
1976   " " gnus-summary-clear-mark-forward
1977   "e" gnus-summary-mark-as-expirable
1978   "x" gnus-summary-mark-as-expirable
1979   "?" gnus-summary-mark-as-dormant
1980   "b" gnus-summary-set-bookmark
1981   "B" gnus-summary-remove-bookmark
1982   "#" gnus-summary-mark-as-processable
1983   "\M-#" gnus-summary-unmark-as-processable
1984   "S" gnus-summary-limit-include-expunged
1985   "C" gnus-summary-catchup
1986   "H" gnus-summary-catchup-to-here
1987   "h" gnus-summary-catchup-from-here
1988   "\C-c" gnus-summary-catchup-all
1989   "k" gnus-summary-kill-same-subject-and-select
1990   "K" gnus-summary-kill-same-subject
1991   "P" gnus-uu-mark-map)
1992
1993 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1994   "c" gnus-summary-clear-above
1995   "u" gnus-summary-tick-above
1996   "m" gnus-summary-mark-above
1997   "k" gnus-summary-kill-below)
1998
1999 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
2000   "/" gnus-summary-limit-to-subject
2001   "n" gnus-summary-limit-to-articles
2002   "b" gnus-summary-limit-to-bodies
2003   "h" gnus-summary-limit-to-headers
2004   "w" gnus-summary-pop-limit
2005   "s" gnus-summary-limit-to-subject
2006   "a" gnus-summary-limit-to-author
2007   "u" gnus-summary-limit-to-unread
2008   "m" gnus-summary-limit-to-marks
2009   "M" gnus-summary-limit-exclude-marks
2010   "v" gnus-summary-limit-to-score
2011   "*" gnus-summary-limit-include-cached
2012   "D" gnus-summary-limit-include-dormant
2013   "T" gnus-summary-limit-include-thread
2014   "d" gnus-summary-limit-exclude-dormant
2015   "t" gnus-summary-limit-to-age
2016   "." gnus-summary-limit-to-unseen
2017   "x" gnus-summary-limit-to-extra
2018   "p" gnus-summary-limit-to-display-predicate
2019   "E" gnus-summary-limit-include-expunged
2020   "c" gnus-summary-limit-exclude-childless-dormant
2021   "C" gnus-summary-limit-mark-excluded-as-read
2022   "o" gnus-summary-insert-old-articles
2023   "N" gnus-summary-insert-new-articles
2024   "S" gnus-summary-limit-to-singletons
2025   "r" gnus-summary-limit-to-replied
2026   "R" gnus-summary-limit-to-recipient
2027   "A" gnus-summary-limit-to-address)
2028
2029 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2030   "n" gnus-summary-next-unread-article
2031   "p" gnus-summary-prev-unread-article
2032   "N" gnus-summary-next-article
2033   "P" gnus-summary-prev-article
2034   "\C-n" gnus-summary-next-same-subject
2035   "\C-p" gnus-summary-prev-same-subject
2036   "\M-n" gnus-summary-next-unread-subject
2037   "\M-p" gnus-summary-prev-unread-subject
2038   "f" gnus-summary-first-unread-article
2039   "b" gnus-summary-best-unread-article
2040   "j" gnus-summary-goto-article
2041   "g" gnus-summary-goto-subject
2042   "l" gnus-summary-goto-last-article
2043   "o" gnus-summary-pop-article)
2044
2045 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2046   "k" gnus-summary-kill-thread
2047   "E" gnus-summary-expire-thread
2048   "l" gnus-summary-lower-thread
2049   "i" gnus-summary-raise-thread
2050   "T" gnus-summary-toggle-threads
2051   "t" gnus-summary-rethread-current
2052   "^" gnus-summary-reparent-thread
2053   "\M-^" gnus-summary-reparent-children
2054   "s" gnus-summary-show-thread
2055   "S" gnus-summary-show-all-threads
2056   "h" gnus-summary-hide-thread
2057   "H" gnus-summary-hide-all-threads
2058   "n" gnus-summary-next-thread
2059   "p" gnus-summary-prev-thread
2060   "u" gnus-summary-up-thread
2061   "o" gnus-summary-top-thread
2062   "d" gnus-summary-down-thread
2063   "#" gnus-uu-mark-thread
2064   "\M-#" gnus-uu-unmark-thread)
2065
2066 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2067   "g" gnus-summary-prepare
2068   "c" gnus-summary-insert-cached-articles
2069   "d" gnus-summary-insert-dormant-articles
2070   "t" gnus-summary-insert-ticked-articles)
2071
2072 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2073   "c" gnus-summary-catchup-and-exit
2074   "C" gnus-summary-catchup-all-and-exit
2075   "E" gnus-summary-exit-no-update
2076   "Q" gnus-summary-exit
2077   "Z" gnus-summary-exit
2078   "n" gnus-summary-catchup-and-goto-next-group
2079   "p" gnus-summary-catchup-and-goto-prev-group
2080   "R" gnus-summary-reselect-current-group
2081   "G" gnus-summary-rescan-group
2082   "N" gnus-summary-next-group
2083   "s" gnus-summary-save-newsrc
2084   "P" gnus-summary-prev-group)
2085
2086 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2087   " " gnus-summary-next-page
2088   "n" gnus-summary-next-page
2089   [?\S-\ ] gnus-summary-prev-page
2090   "\177" gnus-summary-prev-page
2091   [delete] gnus-summary-prev-page
2092   "p" gnus-summary-prev-page
2093   "\r" gnus-summary-scroll-up
2094   "\M-\r" gnus-summary-scroll-down
2095   "<" gnus-summary-beginning-of-article
2096   ">" gnus-summary-end-of-article
2097   "b" gnus-summary-beginning-of-article
2098   "e" gnus-summary-end-of-article
2099   "^" gnus-summary-refer-parent-article
2100   "r" gnus-summary-refer-parent-article
2101   "C" gnus-summary-show-complete-article
2102   "D" gnus-summary-enter-digest-group
2103   "R" gnus-summary-refer-references
2104   "T" gnus-summary-refer-thread
2105   "W" gnus-warp-to-article
2106   "g" gnus-summary-show-article
2107   "s" gnus-summary-isearch-article
2108   "\t" gnus-summary-widget-forward
2109   [backtab] gnus-summary-widget-backward
2110   "P" gnus-summary-print-article
2111   "S" gnus-sticky-article
2112   "M" gnus-mailing-list-insinuate
2113   "t" gnus-article-babel)
2114
2115 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2116   "b" gnus-article-add-buttons
2117   "B" gnus-article-add-buttons-to-head
2118   "o" gnus-article-treat-overstrike
2119   "e" gnus-article-emphasize
2120   "w" gnus-article-fill-cited-article
2121   "Q" gnus-article-fill-long-lines
2122   "L" gnus-article-toggle-truncate-lines
2123   "C" gnus-article-capitalize-sentences
2124   "c" gnus-article-remove-cr
2125   "q" gnus-article-de-quoted-unreadable
2126   "6" gnus-article-de-base64-unreadable
2127   "Z" gnus-article-decode-HZ
2128   "A" gnus-article-treat-ansi-sequences
2129   "h" gnus-article-wash-html
2130   "u" gnus-article-unsplit-urls
2131   "s" gnus-summary-force-verify-and-decrypt
2132   "f" gnus-article-display-x-face
2133   "l" gnus-summary-stop-page-breaking
2134   "r" gnus-summary-caesar-message
2135   "m" gnus-summary-morse-message
2136   "t" gnus-summary-toggle-header
2137   "g" gnus-treat-smiley
2138   "v" gnus-summary-verbose-headers
2139   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2140   "p" gnus-article-verify-x-pgp-sig
2141   "d" gnus-article-treat-dumbquotes
2142   "U" gnus-article-treat-non-ascii
2143   "i" gnus-summary-idna-message)
2144
2145 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2146   ;; mnemonic: deuglif*Y*
2147   "u" gnus-article-outlook-unwrap-lines
2148   "a" gnus-article-outlook-repair-attribution
2149   "c" gnus-article-outlook-rearrange-citation
2150   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2151
2152 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2153   "a" gnus-article-hide
2154   "h" gnus-article-hide-headers
2155   "b" gnus-article-hide-boring-headers
2156   "s" gnus-article-hide-signature
2157   "c" gnus-article-hide-citation
2158   "C" gnus-article-hide-citation-in-followups
2159   "l" gnus-article-hide-list-identifiers
2160   "B" gnus-article-strip-banner
2161   "P" gnus-article-hide-pem
2162   "\C-c" gnus-article-hide-citation-maybe)
2163
2164 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2165   "a" gnus-article-highlight
2166   "h" gnus-article-highlight-headers
2167   "c" gnus-article-highlight-citation
2168   "s" gnus-article-highlight-signature)
2169
2170 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2171   "f" gnus-article-treat-fold-headers
2172   "u" gnus-article-treat-unfold-headers
2173   "n" gnus-article-treat-fold-newsgroups)
2174
2175 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2176   "x" gnus-article-display-x-face
2177   "d" gnus-article-display-face
2178   "s" gnus-treat-smiley
2179   "D" gnus-article-remove-images
2180   "W" gnus-article-show-images
2181   "f" gnus-treat-from-picon
2182   "m" gnus-treat-mail-picon
2183   "n" gnus-treat-newsgroups-picon
2184   "g" gnus-treat-from-gravatar
2185   "h" gnus-treat-mail-gravatar)
2186
2187 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2188   "w" gnus-article-decode-mime-words
2189   "c" gnus-article-decode-charset
2190   "v" gnus-mime-view-all-parts
2191   "b" gnus-article-view-part)
2192
2193 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2194   "z" gnus-article-date-ut
2195   "u" gnus-article-date-ut
2196   "l" gnus-article-date-local
2197   "p" gnus-article-date-english
2198   "e" gnus-article-date-lapsed
2199   "o" gnus-article-date-original
2200   "i" gnus-article-date-iso8601
2201   "s" gnus-article-date-user)
2202
2203 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2204   "t" gnus-article-remove-trailing-blank-lines
2205   "l" gnus-article-strip-leading-blank-lines
2206   "m" gnus-article-strip-multiple-blank-lines
2207   "a" gnus-article-strip-blank-lines
2208   "A" gnus-article-strip-all-blank-lines
2209   "s" gnus-article-strip-leading-space
2210   "e" gnus-article-strip-trailing-space
2211   "w" gnus-article-remove-leading-whitespace)
2212
2213 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2214   "v" gnus-version
2215   "d" gnus-summary-describe-group
2216   "h" gnus-summary-describe-briefly
2217   "i" gnus-info-find-node)
2218
2219 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2220   "e" gnus-summary-expire-articles
2221   "\M-\C-e" gnus-summary-expire-articles-now
2222   "\177" gnus-summary-delete-article
2223   [delete] gnus-summary-delete-article
2224   "m" gnus-summary-move-article
2225   "r" gnus-summary-respool-article
2226   "w" gnus-summary-edit-article
2227   "c" gnus-summary-copy-article
2228   "B" gnus-summary-crosspost-article
2229   "q" gnus-summary-respool-query
2230   "t" gnus-summary-respool-trace
2231   "i" gnus-summary-import-article
2232   "I" gnus-summary-create-article
2233   "p" gnus-summary-article-posted-p)
2234
2235 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2236   "o" gnus-summary-save-article
2237   "m" gnus-summary-save-article-mail
2238   "F" gnus-summary-write-article-file
2239   "r" gnus-summary-save-article-rmail
2240   "f" gnus-summary-save-article-file
2241   "b" gnus-summary-save-article-body-file
2242   "B" gnus-summary-write-article-body-file
2243   "h" gnus-summary-save-article-folder
2244   "v" gnus-summary-save-article-vm
2245   "p" gnus-summary-pipe-output
2246   "P" gnus-summary-muttprint)
2247
2248 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2249   "b" gnus-summary-display-buttonized
2250   "m" gnus-summary-repair-multipart
2251   "v" gnus-article-view-part
2252   "o" gnus-article-save-part
2253   "O" gnus-article-save-part-and-strip
2254   "r" gnus-article-replace-part
2255   "d" gnus-article-delete-part
2256   "t" gnus-article-view-part-as-type
2257   "j" gnus-article-jump-to-part
2258   "c" gnus-article-copy-part
2259   "C" gnus-article-view-part-as-charset
2260   "e" gnus-article-view-part-externally
2261   "H" gnus-article-browse-html-article
2262   "E" gnus-article-encrypt-body
2263   "i" gnus-article-inline-part
2264   "|" gnus-article-pipe-part)
2265
2266 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2267   "p" gnus-summary-mark-as-processable
2268   "u" gnus-summary-unmark-as-processable
2269   "U" gnus-summary-unmark-all-processable
2270   "v" gnus-uu-mark-over
2271   "s" gnus-uu-mark-series
2272   "r" gnus-uu-mark-region
2273   "g" gnus-uu-unmark-region
2274   "R" gnus-uu-mark-by-regexp
2275   "G" gnus-uu-unmark-by-regexp
2276   "t" gnus-uu-mark-thread
2277   "T" gnus-uu-unmark-thread
2278   "a" gnus-uu-mark-all
2279   "b" gnus-uu-mark-buffer
2280   "S" gnus-uu-mark-sparse
2281   "k" gnus-summary-kill-process-mark
2282   "y" gnus-summary-yank-process-mark
2283   "w" gnus-summary-save-process-mark
2284   "i" gnus-uu-invert-processable)
2285
2286 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2287   ;;"x" gnus-uu-extract-any
2288   "m" gnus-summary-save-parts
2289   "u" gnus-uu-decode-uu
2290   "U" gnus-uu-decode-uu-and-save
2291   "s" gnus-uu-decode-unshar
2292   "S" gnus-uu-decode-unshar-and-save
2293   "o" gnus-uu-decode-save
2294   "O" gnus-uu-decode-save
2295   "b" gnus-uu-decode-binhex
2296   "B" gnus-uu-decode-binhex
2297   "Y" gnus-uu-decode-yenc
2298   "p" gnus-uu-decode-postscript
2299   "P" gnus-uu-decode-postscript-and-save)
2300
2301 (gnus-define-keys
2302     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2303   "u" gnus-uu-decode-uu-view
2304   "U" gnus-uu-decode-uu-and-save-view
2305   "s" gnus-uu-decode-unshar-view
2306   "S" gnus-uu-decode-unshar-and-save-view
2307   "o" gnus-uu-decode-save-view
2308   "O" gnus-uu-decode-save-view
2309   "b" gnus-uu-decode-binhex-view
2310   "B" gnus-uu-decode-binhex-view
2311   "p" gnus-uu-decode-postscript-view
2312   "P" gnus-uu-decode-postscript-and-save-view)
2313
2314 (defvar gnus-article-post-menu nil)
2315
2316 (defconst gnus-summary-menu-maxlen 20)
2317
2318 (defun gnus-summary-menu-split (menu)
2319   ;; If we have lots of elements, divide them into groups of 20
2320   ;; and make a pane (or submenu) for each one.
2321   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2322       (let ((menu menu) sublists next
2323             (i 1))
2324         (while menu
2325           ;; Pull off the next gnus-summary-menu-maxlen elements
2326           ;; and make them the next element of sublist.
2327           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2328           (if next
2329               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2330                       nil))
2331           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2332                                              (aref (car (last menu)) 0)) menu)
2333                                sublists))
2334           (setq i (1+ i))
2335           (setq menu next))
2336         (nreverse sublists))
2337     ;; Few elements--put them all in one pane.
2338     menu))
2339
2340 (defun gnus-summary-make-menu-bar ()
2341   (gnus-turn-off-edit-menu 'summary)
2342
2343   (unless (boundp 'gnus-summary-misc-menu)
2344
2345     (easy-menu-define
2346       gnus-summary-kill-menu gnus-summary-mode-map ""
2347       (cons
2348        "Score"
2349        (nconc
2350         (list
2351          ["Customize" gnus-score-customize t])
2352         (gnus-make-score-map 'increase)
2353         (gnus-make-score-map 'lower)
2354         '(("Mark"
2355            ["Kill below" gnus-summary-kill-below t]
2356            ["Mark above" gnus-summary-mark-above t]
2357            ["Tick above" gnus-summary-tick-above t]
2358            ["Clear above" gnus-summary-clear-above t])
2359           ["Current article score" gnus-summary-current-score t]
2360           ["Current thread score" (gnus-summary-current-score 'total) t]
2361           ["Set score" gnus-summary-set-score t]
2362           ["Switch current score file..." gnus-score-change-score-file t]
2363           ["Set mark below..." gnus-score-set-mark-below t]
2364           ["Set expunge below..." gnus-score-set-expunge-below t]
2365           ["Edit current score file" gnus-score-edit-current-scores t]
2366           ["Edit score file..." gnus-score-edit-file t]
2367           ["Trace score" gnus-score-find-trace t]
2368           ["Find words" gnus-score-find-favourite-words t]
2369           ["Rescore buffer" gnus-summary-rescore t]
2370           ["Increase score..." gnus-summary-increase-score t]
2371           ["Lower score..." gnus-summary-lower-score t]))))
2372
2373     ;; Define both the Article menu in the summary buffer and the
2374     ;; equivalent Commands menu in the article buffer here for
2375     ;; consistency.
2376     (let ((innards
2377            `(("Hide"
2378               ["All" gnus-article-hide t]
2379               ["Headers" gnus-article-hide-headers t]
2380               ["Signature" gnus-article-hide-signature t]
2381               ["Citation" gnus-article-hide-citation t]
2382               ["List identifiers" gnus-article-hide-list-identifiers t]
2383               ["Banner" gnus-article-strip-banner t]
2384               ["Boring headers" gnus-article-hide-boring-headers t])
2385              ("Highlight"
2386               ["All" gnus-article-highlight t]
2387               ["Headers" gnus-article-highlight-headers t]
2388               ["Signature" gnus-article-highlight-signature t]
2389               ["Citation" gnus-article-highlight-citation t])
2390              ("MIME"
2391               ["Words" gnus-article-decode-mime-words t]
2392               ["Charset" gnus-article-decode-charset t]
2393               ["QP" gnus-article-de-quoted-unreadable t]
2394               ["Base64" gnus-article-de-base64-unreadable t]
2395               ["View MIME buttons" gnus-summary-display-buttonized t]
2396               ["View all" gnus-mime-view-all-parts t]
2397               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2398               ["Encrypt body" gnus-article-encrypt-body
2399                :active (not (gnus-group-read-only-p))
2400                ,@(if (featurep 'xemacs) nil
2401                    '(:help "Encrypt the message body on disk"))]
2402               ["Extract all parts..." gnus-summary-save-parts t]
2403               ("Multipart"
2404                ["Repair multipart" gnus-summary-repair-multipart t]
2405                ["Pipe part..." gnus-article-pipe-part t]
2406                ["Inline part" gnus-article-inline-part t]
2407                ["View part as type..." gnus-article-view-part-as-type t]
2408                ["Encrypt body" gnus-article-encrypt-body
2409                 :active (not (gnus-group-read-only-p))
2410                ,@(if (featurep 'xemacs) nil
2411                    '(:help "Encrypt the message body on disk"))]
2412                ["View part externally" gnus-article-view-part-externally t]
2413                ["View HTML parts in browser" gnus-article-browse-html-article t]
2414                ["View part with charset..." gnus-article-view-part-as-charset t]
2415                ["Copy part" gnus-article-copy-part t]
2416                ["Save part..." gnus-article-save-part t]
2417                ["View part" gnus-article-view-part t]))
2418              ("Date"
2419               ["Local" gnus-article-date-local t]
2420               ["ISO8601" gnus-article-date-iso8601 t]
2421               ["UT" gnus-article-date-ut t]
2422               ["Original" gnus-article-date-original t]
2423               ["Lapsed" gnus-article-date-lapsed t]
2424               ["User-defined" gnus-article-date-user t])
2425              ("Display"
2426               ["Remove images" gnus-article-remove-images t]
2427               ["Toggle smiley" gnus-treat-smiley t]
2428               ["Show X-Face" gnus-article-display-x-face t]
2429               ["Show picons in From" gnus-treat-from-picon t]
2430               ["Show picons in mail headers" gnus-treat-mail-picon t]
2431               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2432               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2433               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2434               ("View as different encoding"
2435                ,@(gnus-summary-menu-split
2436                   (mapcar
2437                    (lambda (cs)
2438                      ;; Since easymenu under Emacs doesn't allow
2439                      ;; lambda forms for menu commands, we should
2440                      ;; provide intern'ed function symbols.
2441                      (let ((command (intern (format "\
2442 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2443                        (fset command
2444                              `(lambda ()
2445                                 (interactive)
2446                                 (let ((gnus-summary-show-article-charset-alist
2447                                        '((1 . ,cs))))
2448                                   (gnus-summary-show-article 1))))
2449                        `[,(symbol-name cs) ,command t]))
2450                    (sort (if (fboundp 'coding-system-list)
2451                              (coding-system-list)
2452                            (mapcar 'car mm-mime-mule-charset-alist))
2453                          'string<)))))
2454              ("Washing"
2455               ("Remove Blanks"
2456                ["Leading" gnus-article-strip-leading-blank-lines t]
2457                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2458                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2459                ["All of the above" gnus-article-strip-blank-lines t]
2460                ["All" gnus-article-strip-all-blank-lines t]
2461                ["Leading space" gnus-article-strip-leading-space t]
2462                ["Trailing space" gnus-article-strip-trailing-space t]
2463                ["Leading space in headers"
2464                 gnus-article-remove-leading-whitespace t])
2465               ["Overstrike" gnus-article-treat-overstrike t]
2466               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2467               ["Non-ASCII" gnus-article-treat-non-ascii t]
2468               ["Emphasis" gnus-article-emphasize t]
2469               ["Word wrap" gnus-article-fill-cited-article t]
2470               ["Fill long lines" gnus-article-fill-long-lines t]
2471               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2472               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2473               ["Remove CR" gnus-article-remove-cr t]
2474               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2475               ["Base64" gnus-article-de-base64-unreadable t]
2476               ["Rot 13" gnus-summary-caesar-message
2477                ,@(if (featurep 'xemacs) '(t)
2478                    '(:help "\"Caesar rotate\" article by 13"))]
2479               ["De-IDNA" gnus-summary-idna-message t]
2480               ["Morse decode" gnus-summary-morse-message t]
2481               ["Unix pipe..." gnus-summary-pipe-message t]
2482               ["Add buttons" gnus-article-add-buttons t]
2483               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2484               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2485               ["Verbose header" gnus-summary-verbose-headers t]
2486               ["Toggle header" gnus-summary-toggle-header t]
2487               ["Unfold headers" gnus-article-treat-unfold-headers t]
2488               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2489               ["Html" gnus-article-wash-html t]
2490               ["Unsplit URLs" gnus-article-unsplit-urls t]
2491               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2492               ["Decode HZ" gnus-article-decode-HZ t]
2493               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2494               ("(Outlook) Deuglify"
2495                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2496                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2497                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2498                ["Full (Outlook) deuglify"
2499                 gnus-article-outlook-deuglify-article t])
2500               )
2501              ("Output"
2502               ["Save in default format..." gnus-summary-save-article
2503                ,@(if (featurep 'xemacs) '(t)
2504                    '(:help "Save article using default method"))]
2505               ["Save in file..." gnus-summary-save-article-file
2506                ,@(if (featurep 'xemacs) '(t)
2507                    '(:help "Save article in file"))]
2508               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2509               ["Save in MH folder..." gnus-summary-save-article-folder t]
2510               ["Save in VM folder..." gnus-summary-save-article-vm t]
2511               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2512               ["Save body in file..." gnus-summary-save-article-body-file t]
2513               ["Pipe through a filter..." gnus-summary-pipe-output t]
2514               ["Print with Muttprint..." gnus-summary-muttprint t]
2515               ["Print" gnus-summary-print-article
2516                ,@(if (featurep 'xemacs) '(t)
2517                    '(:help "Generate and print a PostScript image"))])
2518              ("Copy, move,... (Backend)"
2519               ,@(if (featurep 'xemacs) nil
2520                   '(:help "Copying, moving, expiring articles..."))
2521               ["Respool article..." gnus-summary-respool-article t]
2522               ["Move article..." gnus-summary-move-article
2523                (gnus-check-backend-function
2524                 'request-move-article gnus-newsgroup-name)]
2525               ["Copy article..." gnus-summary-copy-article t]
2526               ["Crosspost article..." gnus-summary-crosspost-article
2527                (gnus-check-backend-function
2528                 'request-replace-article gnus-newsgroup-name)]
2529               ["Import file..." gnus-summary-import-article
2530                (gnus-check-backend-function
2531                 'request-accept-article gnus-newsgroup-name)]
2532               ["Create article..." gnus-summary-create-article
2533                (gnus-check-backend-function
2534                 'request-accept-article gnus-newsgroup-name)]
2535               ["Check if posted" gnus-summary-article-posted-p t]
2536               ["Edit article" gnus-summary-edit-article
2537                (not (gnus-group-read-only-p))]
2538               ["Delete article" gnus-summary-delete-article
2539                (gnus-check-backend-function
2540                 'request-expire-articles gnus-newsgroup-name)]
2541               ["Query respool" gnus-summary-respool-query t]
2542               ["Trace respool" gnus-summary-respool-trace t]
2543               ["Delete expirable articles" gnus-summary-expire-articles-now
2544                (gnus-check-backend-function
2545                 'request-expire-articles gnus-newsgroup-name)])
2546              ("Extract"
2547               ["Uudecode" gnus-uu-decode-uu
2548                ,@(if (featurep 'xemacs) '(t)
2549                    '(:help "Decode uuencoded article(s)"))]
2550               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2551               ["Unshar" gnus-uu-decode-unshar t]
2552               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2553               ["Save" gnus-uu-decode-save t]
2554               ["Binhex" gnus-uu-decode-binhex t]
2555               ["PostScript" gnus-uu-decode-postscript t]
2556               ["All MIME parts" gnus-summary-save-parts t])
2557              ("Cache"
2558               ["Enter article" gnus-cache-enter-article t]
2559               ["Remove article" gnus-cache-remove-article t])
2560              ["Translate" gnus-article-babel t]
2561              ["Select article buffer" gnus-summary-select-article-buffer t]
2562              ["Make article buffer sticky" gnus-sticky-article t]
2563              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2564              ["Isearch article..." gnus-summary-isearch-article t]
2565              ["Beginning of the article" gnus-summary-beginning-of-article t]
2566              ["End of the article" gnus-summary-end-of-article t]
2567              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2568              ["Fetch referenced articles" gnus-summary-refer-references t]
2569              ["Fetch current thread" gnus-summary-refer-thread t]
2570              ["Fetch article with id..." gnus-summary-refer-article t]
2571              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2572              ["Redisplay" gnus-summary-show-article t]
2573              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2574       (easy-menu-define
2575         gnus-summary-article-menu gnus-summary-mode-map ""
2576         (cons "Article" innards))
2577
2578       (if (not (keymapp gnus-summary-article-menu))
2579           (easy-menu-define
2580             gnus-article-commands-menu gnus-article-mode-map ""
2581             (cons "Commands" innards))
2582         ;; in Emacs, don't share menu.
2583         (setq gnus-article-commands-menu
2584               (copy-keymap gnus-summary-article-menu))
2585         (define-key gnus-article-mode-map [menu-bar commands]
2586           (cons "Commands" gnus-article-commands-menu))))
2587
2588     (easy-menu-define
2589       gnus-summary-thread-menu gnus-summary-mode-map ""
2590       '("Threads"
2591         ["Find all messages in thread" gnus-summary-refer-thread t]
2592         ["Toggle threading" gnus-summary-toggle-threads t]
2593         ["Hide threads" gnus-summary-hide-all-threads t]
2594         ["Show threads" gnus-summary-show-all-threads t]
2595         ["Hide thread" gnus-summary-hide-thread t]
2596         ["Show thread" gnus-summary-show-thread t]
2597         ["Go to next thread" gnus-summary-next-thread t]
2598         ["Go to previous thread" gnus-summary-prev-thread t]
2599         ["Go down thread" gnus-summary-down-thread t]
2600         ["Go up thread" gnus-summary-up-thread t]
2601         ["Top of thread" gnus-summary-top-thread t]
2602         ["Mark thread as read" gnus-summary-kill-thread t]
2603         ["Mark thread as expired" gnus-summary-expire-thread t]
2604         ["Lower thread score" gnus-summary-lower-thread t]
2605         ["Raise thread score" gnus-summary-raise-thread t]
2606         ["Rethread current" gnus-summary-rethread-current t]))
2607
2608     (easy-menu-define
2609       gnus-summary-post-menu gnus-summary-mode-map ""
2610       `("Post"
2611         ["Send a message (mail or news)" gnus-summary-post-news
2612          ,@(if (featurep 'xemacs) '(t)
2613              '(:help "Compose a new message (mail or news)"))]
2614         ["Followup" gnus-summary-followup
2615          ,@(if (featurep 'xemacs) '(t)
2616              '(:help "Post followup to this article"))]
2617         ["Followup and yank" gnus-summary-followup-with-original
2618          ,@(if (featurep 'xemacs) '(t)
2619              '(:help "Post followup to this article, quoting its contents"))]
2620         ["Supersede article" gnus-summary-supersede-article t]
2621         ["Cancel article" gnus-summary-cancel-article
2622          ,@(if (featurep 'xemacs) '(t)
2623              '(:help "Cancel an article you posted"))]
2624         ["Reply" gnus-summary-reply t]
2625         ["Reply and yank" gnus-summary-reply-with-original t]
2626         ["Wide reply" gnus-summary-wide-reply t]
2627         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2628          ,@(if (featurep 'xemacs) '(t)
2629              '(:help "Mail a reply, quoting this article"))]
2630         ["Very wide reply" gnus-summary-very-wide-reply t]
2631         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2632          ,@(if (featurep 'xemacs) '(t)
2633              '(:help "Mail a very wide reply, quoting this article"))]
2634         ["Mail forward" gnus-summary-mail-forward t]
2635         ["Post forward" gnus-summary-post-forward t]
2636         ["Digest and mail" gnus-uu-digest-mail-forward t]
2637         ["Digest and post" gnus-uu-digest-post-forward t]
2638         ["Resend message" gnus-summary-resend-message t]
2639         ["Resend message edit" gnus-summary-resend-message-edit t]
2640         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2641         ["Send a mail" gnus-summary-mail-other-window t]
2642         ["Create a local message" gnus-summary-news-other-window t]
2643         ["Uuencode and post" gnus-uu-post-news
2644          ,@(if (featurep 'xemacs) '(t)
2645              '(:help "Post a uuencoded article"))]
2646         ["Followup via news" gnus-summary-followup-to-mail t]
2647         ["Followup via news and yank"
2648          gnus-summary-followup-to-mail-with-original t]
2649         ["Strip signature on reply"
2650          (lambda ()
2651            (interactive)
2652            (if (not (memq message-cite-function
2653                           '(message-cite-original-without-signature
2654                             message-cite-original)))
2655                ;; Stupid workaround for XEmacs not honoring :visible.
2656                (message "Can't toggle this value of `message-cite-function'")
2657              (setq message-cite-function
2658                    (if (eq message-cite-function
2659                            'message-cite-original-without-signature)
2660                        'message-cite-original
2661                      'message-cite-original-without-signature))))
2662          ;; XEmacs barfs on :visible.
2663          ,@(if (featurep 'xemacs) nil
2664              '(:visible (memq message-cite-function
2665                               '(message-cite-original-without-signature
2666                                 message-cite-original))))
2667          :style toggle
2668          :selected (eq message-cite-function
2669                        'message-cite-original-without-signature)
2670          ,@(if (featurep 'xemacs) nil
2671              '(:help "Strip signature from cited article when replying."))]
2672         ;;("Draft"
2673         ;;["Send" gnus-summary-send-draft t]
2674         ;;["Send bounced" gnus-resend-bounced-mail t])
2675         ))
2676
2677     (cond
2678      ((not (keymapp gnus-summary-post-menu))
2679       (setq gnus-article-post-menu gnus-summary-post-menu))
2680      ((not gnus-article-post-menu)
2681       ;; Don't share post menu.
2682       (setq gnus-article-post-menu
2683             (copy-keymap gnus-summary-post-menu))))
2684     (define-key gnus-article-mode-map [menu-bar post]
2685       (cons "Post" gnus-article-post-menu))
2686
2687     (easy-menu-define
2688       gnus-summary-misc-menu gnus-summary-mode-map ""
2689       `("Gnus"
2690         ("Mark Read"
2691          ["Mark as read" gnus-summary-mark-as-read-forward t]
2692          ["Mark same subject and select"
2693           gnus-summary-kill-same-subject-and-select t]
2694          ["Mark same subject" gnus-summary-kill-same-subject t]
2695          ["Catchup" gnus-summary-catchup
2696           ,@(if (featurep 'xemacs) '(t)
2697               '(:help "Mark unread articles in this group as read"))]
2698          ["Catchup all" gnus-summary-catchup-all t]
2699          ["Catchup to here" gnus-summary-catchup-to-here t]
2700          ["Catchup from here" gnus-summary-catchup-from-here t]
2701          ["Catchup region" gnus-summary-mark-region-as-read
2702           (gnus-mark-active-p)]
2703          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2704         ("Mark Various"
2705          ["Tick" gnus-summary-tick-article-forward t]
2706          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2707          ["Remove marks" gnus-summary-clear-mark-forward t]
2708          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2709          ["Set bookmark" gnus-summary-set-bookmark t]
2710          ["Remove bookmark" gnus-summary-remove-bookmark t])
2711         ("Limit to"
2712          ["Marks..." gnus-summary-limit-to-marks t]
2713          ["Subject..." gnus-summary-limit-to-subject t]
2714          ["Author..." gnus-summary-limit-to-author t]
2715          ["Recipient..." gnus-summary-limit-to-recipient t]
2716          ["Address..." gnus-summary-limit-to-address t]
2717          ["Age..." gnus-summary-limit-to-age t]
2718          ["Extra..." gnus-summary-limit-to-extra t]
2719          ["Score..." gnus-summary-limit-to-score t]
2720          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2721          ["Unread" gnus-summary-limit-to-unread t]
2722          ["Unseen" gnus-summary-limit-to-unseen t]
2723          ["Singletons" gnus-summary-limit-to-singletons t]
2724          ["Replied" gnus-summary-limit-to-replied t]
2725          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2726          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2727          ["Pop limit" gnus-summary-pop-limit t]
2728          ["Show dormant" gnus-summary-limit-include-dormant t]
2729          ["Hide childless dormant"
2730           gnus-summary-limit-exclude-childless-dormant t]
2731          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2732          ["Hide marked" gnus-summary-limit-exclude-marks t]
2733          ["Show expunged" gnus-summary-limit-include-expunged t])
2734         ("Process Mark"
2735          ["Set mark" gnus-summary-mark-as-processable t]
2736          ["Remove mark" gnus-summary-unmark-as-processable t]
2737          ["Remove all marks" gnus-summary-unmark-all-processable t]
2738          ["Invert marks" gnus-uu-invert-processable t]
2739          ["Mark above" gnus-uu-mark-over t]
2740          ["Mark series" gnus-uu-mark-series t]
2741          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2742          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2743          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2744          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2745          ["Mark all" gnus-uu-mark-all t]
2746          ["Mark buffer" gnus-uu-mark-buffer t]
2747          ["Mark sparse" gnus-uu-mark-sparse t]
2748          ["Mark thread" gnus-uu-mark-thread t]
2749          ["Unmark thread" gnus-uu-unmark-thread t]
2750          ("Process Mark Sets"
2751           ["Kill" gnus-summary-kill-process-mark t]
2752           ["Yank" gnus-summary-yank-process-mark
2753            gnus-newsgroup-process-stack]
2754           ["Save" gnus-summary-save-process-mark t]
2755           ["Run command on marked..." gnus-summary-universal-argument t]))
2756         ("Registry Marks")
2757         ("Scroll article"
2758          ["Page forward" gnus-summary-next-page
2759           ,@(if (featurep 'xemacs) '(t)
2760               '(:help "Show next page of article"))]
2761          ["Page backward" gnus-summary-prev-page
2762           ,@(if (featurep 'xemacs) '(t)
2763               '(:help "Show previous page of article"))]
2764          ["Line forward" gnus-summary-scroll-up t])
2765         ("Move"
2766          ["Next unread article" gnus-summary-next-unread-article t]
2767          ["Previous unread article" gnus-summary-prev-unread-article t]
2768          ["Next article" gnus-summary-next-article t]
2769          ["Previous article" gnus-summary-prev-article t]
2770          ["Next unread subject" gnus-summary-next-unread-subject t]
2771          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2772          ["Next article same subject" gnus-summary-next-same-subject t]
2773          ["Previous article same subject" gnus-summary-prev-same-subject t]
2774          ["First unread article" gnus-summary-first-unread-article t]
2775          ["Best unread article" gnus-summary-best-unread-article t]
2776          ["Go to subject number..." gnus-summary-goto-subject t]
2777          ["Go to article number..." gnus-summary-goto-article t]
2778          ["Go to the last article" gnus-summary-goto-last-article t]
2779          ["Pop article off history" gnus-summary-pop-article t])
2780         ("Sort"
2781          ["Sort by number" gnus-summary-sort-by-number t]
2782          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2783          ["Sort by author" gnus-summary-sort-by-author t]
2784          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2785          ["Sort by subject" gnus-summary-sort-by-subject t]
2786          ["Sort by date" gnus-summary-sort-by-date t]
2787          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2788          ["Sort by score" gnus-summary-sort-by-score t]
2789          ["Sort by lines" gnus-summary-sort-by-lines t]
2790          ["Sort by characters" gnus-summary-sort-by-chars t]
2791          ["Randomize" gnus-summary-sort-by-random t]
2792          ["Original sort" gnus-summary-sort-by-original t])
2793         ("Help"
2794          ["Describe group" gnus-summary-describe-group t]
2795          ["Read manual" gnus-info-find-node t])
2796         ("Modes"
2797          ["Pick and read" gnus-pick-mode t]
2798          ["Binary" gnus-binary-mode t])
2799         ("Regeneration"
2800          ["Regenerate" gnus-summary-prepare t]
2801          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2802          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2803          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2804          ["Toggle threading" gnus-summary-toggle-threads t])
2805         ["See old articles" gnus-summary-insert-old-articles t]
2806         ["See new articles" gnus-summary-insert-new-articles t]
2807         ["Filter articles..." gnus-summary-execute-command t]
2808         ["Run command on articles..." gnus-summary-universal-argument t]
2809         ["Search articles forward..." gnus-summary-search-article-forward t]
2810         ["Search articles backward..." gnus-summary-search-article-backward t]
2811         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2812         ["Expand window" gnus-summary-expand-window t]
2813         ["Expire expirable articles" gnus-summary-expire-articles
2814          (gnus-check-backend-function
2815           'request-expire-articles gnus-newsgroup-name)]
2816         ["Edit local kill file" gnus-summary-edit-local-kill t]
2817         ["Edit main kill file" gnus-summary-edit-global-kill t]
2818         ["Edit group parameters" gnus-summary-edit-parameters t]
2819         ["Customize group parameters" gnus-summary-customize-parameters t]
2820         ["Send a bug report" gnus-bug t]
2821         ("Exit"
2822          ["Catchup and exit" gnus-summary-catchup-and-exit
2823           ,@(if (featurep 'xemacs) '(t)
2824               '(:help "Mark unread articles in this group as read, then exit"))]
2825          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2826          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2827          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2828          ["Exit group" gnus-summary-exit
2829           ,@(if (featurep 'xemacs) '(t)
2830               '(:help "Exit current group, return to group selection mode"))]
2831          ["Exit group without updating" gnus-summary-exit-no-update t]
2832          ["Exit and goto next group" gnus-summary-next-group t]
2833          ["Exit and goto prev group" gnus-summary-prev-group t]
2834          ["Reselect group" gnus-summary-reselect-current-group t]
2835          ["Rescan group" gnus-summary-rescan-group t]
2836          ["Update dribble" gnus-summary-save-newsrc t])))
2837
2838     (gnus-run-hooks 'gnus-summary-menu-hook)))
2839
2840 (defvar gnus-summary-tool-bar-map nil)
2841
2842 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2843 ;; affect _new_ message buffers.  We might add a function that walks thru all
2844 ;; summary-mode buffers and force the update.
2845 (defun gnus-summary-tool-bar-update (&optional symbol value)
2846   "Update summary mode toolbar.
2847 Setter function for custom variables."
2848   (setq-default gnus-summary-tool-bar-map nil)
2849   (when symbol
2850     ;; When used as ":set" function:
2851     (set-default symbol value))
2852   (when (gnus-buffer-live-p gnus-summary-buffer)
2853     (with-current-buffer gnus-summary-buffer
2854       (gnus-summary-make-tool-bar))))
2855
2856 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2857                                      'gnus-summary-tool-bar-gnome
2858                                    'gnus-summary-tool-bar-retro)
2859   "Specifies the Gnus summary tool bar.
2860
2861 It can be either a list or a symbol referring to a list.  See
2862 `gmm-tool-bar-from-list' for the format of the list.  The
2863 default key map is `gnus-summary-mode-map'.
2864
2865 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2866 `gnus-summary-tool-bar-retro'."
2867   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2868                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2869                  (repeat :tag "User defined list" gmm-tool-bar-item)
2870                  (symbol))
2871   :version "23.1" ;; No Gnus
2872   :initialize 'custom-initialize-default
2873   :set 'gnus-summary-tool-bar-update
2874   :group 'gnus-summary)
2875
2876 (defcustom gnus-summary-tool-bar-gnome
2877   '((gnus-summary-post-news "mail/compose" nil)
2878     (gnus-summary-insert-new-articles "mail/inbox" nil
2879                                       :visible (or (not gnus-agent)
2880                                                    gnus-plugged))
2881     (gnus-summary-reply-with-original "mail/reply")
2882     (gnus-summary-reply "mail/reply" nil :visible nil)
2883     (gnus-summary-followup-with-original "mail/reply-all")
2884     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2885     (gnus-summary-mail-forward "mail/forward")
2886     (gnus-summary-save-article "mail/save")
2887     (gnus-summary-search-article-forward "search" nil :visible nil)
2888     (gnus-summary-print-article "print")
2889     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2890     ;; Some new commands that may need more suitable icons:
2891     (gnus-summary-save-newsrc "save" nil :visible nil)
2892     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2893     (gnus-summary-prev-article "left-arrow")
2894     (gnus-summary-next-article "right-arrow")
2895     (gnus-summary-next-page "next-page")
2896     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2897     ;;
2898     ;; Maybe some sort-by-... could be added:
2899     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2900     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2901     (gnus-summary-mark-as-expirable
2902      "delete" nil
2903      :visible (gnus-check-backend-function 'request-expire-articles
2904                                            gnus-newsgroup-name))
2905     (gnus-summary-mark-as-spam
2906      "mail/spam" t
2907      :visible (and (fboundp 'spam-group-ham-contents-p)
2908                    (spam-group-ham-contents-p gnus-newsgroup-name))
2909      :help "Mark as spam")
2910     (gnus-summary-mark-as-read-forward
2911      "mail/not-spam" nil
2912      :visible (and (fboundp 'spam-group-spam-contents-p)
2913                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2914     ;;
2915     (gnus-summary-exit "exit")
2916     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2917     (gnus-info-find-node "help"))
2918   "List of functions for the summary tool bar (GNOME style).
2919
2920 See `gmm-tool-bar-from-list' for the format of the list."
2921   :type '(repeat gmm-tool-bar-item)
2922   :version "23.1" ;; No Gnus
2923   :initialize 'custom-initialize-default
2924   :set 'gnus-summary-tool-bar-update
2925   :group 'gnus-summary)
2926
2927 (defcustom gnus-summary-tool-bar-retro
2928   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2929     (gnus-summary-next-unread-article "gnus/next-ur")
2930     (gnus-summary-post-news "gnus/post")
2931     (gnus-summary-followup-with-original "gnus/fuwo")
2932     (gnus-summary-followup "gnus/followup")
2933     (gnus-summary-reply-with-original "gnus/reply-wo")
2934     (gnus-summary-reply "gnus/reply")
2935     (gnus-summary-caesar-message "gnus/rot13")
2936     (gnus-uu-decode-uu "gnus/uu-decode")
2937     (gnus-summary-save-article-file "gnus/save-aif")
2938     (gnus-summary-save-article "gnus/save-art")
2939     (gnus-uu-post-news "gnus/uu-post")
2940     (gnus-summary-catchup "gnus/catchup")
2941     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2942     (gnus-summary-exit "gnus/exit-summ")
2943     ;; Some new command that may need more suitable icons:
2944     (gnus-summary-print-article "gnus/print" nil :visible nil)
2945     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2946     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2947     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2948     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2949     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2950     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2951     ;;
2952     (gnus-info-find-node "gnus/help" nil :visible nil))
2953   "List of functions for the summary tool bar (retro look).
2954
2955 See `gmm-tool-bar-from-list' for the format of the list."
2956   :type '(repeat gmm-tool-bar-item)
2957   :version "23.1" ;; No Gnus
2958   :initialize 'custom-initialize-default
2959   :set 'gnus-summary-tool-bar-update
2960   :group 'gnus-summary)
2961
2962 (defcustom gnus-summary-tool-bar-zap-list t
2963   "List of icon items from the global tool bar.
2964 These items are not displayed in the Gnus summary mode tool bar.
2965
2966 See `gmm-tool-bar-from-list' for the format of the list."
2967   :type 'gmm-tool-bar-zap-list
2968   :version "23.1" ;; No Gnus
2969   :initialize 'custom-initialize-default
2970   :set 'gnus-summary-tool-bar-update
2971   :group 'gnus-summary)
2972
2973 (defvar image-load-path)
2974 (defvar tool-bar-map)
2975
2976 (defun gnus-summary-make-tool-bar (&optional force)
2977   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2978 When FORCE, rebuild the tool bar."
2979   (when (and (not (featurep 'xemacs))
2980              (boundp 'tool-bar-mode)
2981              tool-bar-mode
2982              (or (not gnus-summary-tool-bar-map) force))
2983     (let* ((load-path
2984             (gmm-image-load-path-for-library "gnus"
2985                                              "mail/save.xpm"
2986                                              nil t))
2987            (image-load-path (cons (car load-path)
2988                                   (when (boundp 'image-load-path)
2989                                     image-load-path)))
2990            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2991                                         gnus-summary-tool-bar-zap-list
2992                                         'gnus-summary-mode-map)))
2993       (when map
2994         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2995         ;; uses its value.
2996         (setq gnus-summary-tool-bar-map map))))
2997   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2998
2999 (defun gnus-make-score-map (type)
3000   "Make a summary score map of type TYPE."
3001   (if t
3002       nil
3003     (let ((headers '(("author" "from" string)
3004                      ("subject" "subject" string)
3005                      ("article body" "body" string)
3006                      ("article head" "head" string)
3007                      ("xref" "xref" string)
3008                      ("extra header" "extra" string)
3009                      ("lines" "lines" number)
3010                      ("followups to author" "followup" string)))
3011           (types '((number ("less than" <)
3012                            ("greater than" >)
3013                            ("equal" =))
3014                    (string ("substring" s)
3015                            ("exact string" e)
3016                            ("fuzzy string" f)
3017                            ("regexp" r))))
3018           (perms '(("temporary" (current-time-string))
3019                    ("permanent" nil)
3020                    ("immediate" now)))
3021           header)
3022       (list
3023        (apply
3024         'nconc
3025         (list
3026          (if (eq type 'lower)
3027              "Lower score"
3028            "Increase score"))
3029         (let (outh)
3030           (while headers
3031             (setq header (car headers))
3032             (setq outh
3033                   (cons
3034                    (apply
3035                     'nconc
3036                     (list (car header))
3037                     (let ((ts (cdr (assoc (nth 2 header) types)))
3038                           outt)
3039                       (while ts
3040                         (setq outt
3041                               (cons
3042                                (apply
3043                                 'nconc
3044                                 (list (caar ts))
3045                                 (let ((ps perms)
3046                                       outp)
3047                                   (while ps
3048                                     (setq outp
3049                                           (cons
3050                                            (vector
3051                                             (caar ps)
3052                                             (list
3053                                              'gnus-summary-score-entry
3054                                              (nth 1 header)
3055                                              (if (or (string= (nth 1 header)
3056                                                               "head")
3057                                                      (string= (nth 1 header)
3058                                                               "body"))
3059                                                  ""
3060                                                (list 'gnus-summary-header
3061                                                      (nth 1 header)))
3062                                              (list 'quote (nth 1 (car ts)))
3063                                              (list 'gnus-score-delta-default
3064                                                    nil)
3065                                              (nth 1 (car ps))
3066                                              t)
3067                                             t)
3068                                            outp))
3069                                     (setq ps (cdr ps)))
3070                                   (list (nreverse outp))))
3071                                outt))
3072                         (setq ts (cdr ts)))
3073                       (list (nreverse outt))))
3074                    outh))
3075             (setq headers (cdr headers)))
3076           (list (nreverse outh))))))))
3077
3078
3079 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3080 (defvar bookmark-make-record-function)
3081 \f
3082 (defvar bidi-paragraph-direction)
3083
3084 (defun gnus-summary-mode (&optional group)
3085   "Major mode for reading articles.
3086
3087 All normal editing commands are switched off.
3088 \\<gnus-summary-mode-map>
3089 Each line in this buffer represents one article.  To read an
3090 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3091 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3092 respectively.
3093
3094 You can also post articles and send mail from this buffer.  To
3095 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3096 of an article, type `\\[gnus-summary-reply]'.
3097
3098 There are approx. one gazillion commands you can execute in this
3099 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3100
3101 The following commands are available:
3102
3103 \\{gnus-summary-mode-map}"
3104   ;; FIXME: Use define-derived-mode.
3105   (interactive)
3106   (kill-all-local-variables)
3107   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3108     (gnus-summary-make-local-variables))
3109   (gnus-summary-make-local-variables)
3110   (setq gnus-newsgroup-name group)
3111   (when (gnus-visual-p 'summary-menu 'menu)
3112     (gnus-summary-make-menu-bar)
3113     (gnus-summary-make-tool-bar))
3114   (gnus-make-thread-indent-array)
3115   (gnus-simplify-mode-line)
3116   (setq major-mode 'gnus-summary-mode)
3117   (setq mode-name "Summary")
3118   (use-local-map gnus-summary-mode-map)
3119   (buffer-disable-undo)
3120   (setq buffer-read-only t              ;Disable modification
3121         show-trailing-whitespace nil)
3122   (setq truncate-lines t)
3123   ;; Force paragraph direction to be left-to-right.  Don't make it
3124   ;; bound globally in old Emacsen and XEmacsen.
3125   (set (make-local-variable 'bidi-paragraph-direction) 'left-to-right)
3126   (add-to-invisibility-spec '(gnus-sum . t))
3127   (gnus-summary-set-display-table)
3128   (gnus-set-default-directory)
3129   (make-local-variable 'gnus-summary-line-format)
3130   (make-local-variable 'gnus-summary-line-format-spec)
3131   (make-local-variable 'gnus-summary-dummy-line-format)
3132   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3133   (make-local-variable 'gnus-summary-mark-positions)
3134   (gnus-make-local-hook 'pre-command-hook)
3135   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3136   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3137   (turn-on-gnus-mailing-list-mode)
3138   (mm-enable-multibyte)
3139   (set (make-local-variable 'bookmark-make-record-function)
3140        'gnus-summary-bookmark-make-record)
3141   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3142   (gnus-update-summary-mark-positions))
3143
3144 (defun gnus-summary-make-local-variables ()
3145   "Make all the local summary buffer variables."
3146   (let (global)
3147     (dolist (local gnus-summary-local-variables)
3148       (if (consp local)
3149           (progn
3150             (if (eq (cdr local) 'global)
3151                 ;; Copy the global value of the variable.
3152                 (setq global (symbol-value (car local)))
3153               ;; Use the value from the list.
3154               (setq global (eval (cdr local))))
3155             (set (make-local-variable (car local)) global))
3156         ;; Simple nil-valued local variable.
3157         (set (make-local-variable local) nil)))))
3158
3159 ;; Summary data functions.
3160
3161 (defmacro gnus-data-number (data)
3162   `(car ,data))
3163
3164 (defmacro gnus-data-set-number (data number)
3165   `(setcar ,data ,number))
3166
3167 (defmacro gnus-data-mark (data)
3168   `(nth 1 ,data))
3169
3170 (defmacro gnus-data-set-mark (data mark)
3171   `(setcar (nthcdr 1 ,data) ,mark))
3172
3173 (defmacro gnus-data-pos (data)
3174   `(nth 2 ,data))
3175
3176 (defmacro gnus-data-set-pos (data pos)
3177   `(setcar (nthcdr 2 ,data) ,pos))
3178
3179 (defmacro gnus-data-header (data)
3180   `(nth 3 ,data))
3181
3182 (defmacro gnus-data-set-header (data header)
3183   `(setf (nth 3 ,data) ,header))
3184
3185 (defmacro gnus-data-level (data)
3186   `(nth 4 ,data))
3187
3188 (defmacro gnus-data-unread-p (data)
3189   `(= (nth 1 ,data) gnus-unread-mark))
3190
3191 (defmacro gnus-data-read-p (data)
3192   `(/= (nth 1 ,data) gnus-unread-mark))
3193
3194 (defmacro gnus-data-pseudo-p (data)
3195   `(consp (nth 3 ,data)))
3196
3197 (defmacro gnus-data-find (number)
3198   `(assq ,number gnus-newsgroup-data))
3199
3200 (defmacro gnus-data-find-list (number &optional data)
3201   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3202      (memq (assq ,number bdata)
3203            bdata)))
3204
3205 (defmacro gnus-data-make (number mark pos header level)
3206   `(list ,number ,mark ,pos ,header ,level))
3207
3208 (defun gnus-data-enter (after-article number mark pos header level offset)
3209   (let ((data (gnus-data-find-list after-article)))
3210     (unless data
3211       (error "No such article: %d" after-article))
3212     (setcdr data (cons (gnus-data-make number mark pos header level)
3213                        (cdr data)))
3214     (setq gnus-newsgroup-data-reverse nil)
3215     (gnus-data-update-list (cddr data) offset)))
3216
3217 (defun gnus-data-enter-list (after-article list &optional offset)
3218   (when list
3219     (let ((data (and after-article (gnus-data-find-list after-article)))
3220           (ilist list))
3221       (if (not (or data
3222                    after-article))
3223           (let ((odata gnus-newsgroup-data))
3224             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3225             (when offset
3226               (gnus-data-update-list odata offset)))
3227         ;; Find the last element in the list to be spliced into the main
3228         ;; list.
3229         (setq list (last list))
3230         (if (not data)
3231             (progn
3232               (setcdr list gnus-newsgroup-data)
3233               (setq gnus-newsgroup-data ilist)
3234               (when offset
3235                 (gnus-data-update-list (cdr list) offset)))
3236           (setcdr list (cdr data))
3237           (setcdr data ilist)
3238           (when offset
3239             (gnus-data-update-list (cdr list) offset))))
3240       (setq gnus-newsgroup-data-reverse nil))))
3241
3242 (defun gnus-data-remove (article &optional offset)
3243   (let ((data gnus-newsgroup-data))
3244     (if (= (gnus-data-number (car data)) article)
3245         (progn
3246           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3247                 gnus-newsgroup-data-reverse nil)
3248           (when offset
3249             (gnus-data-update-list gnus-newsgroup-data offset)))
3250       (while (cdr data)
3251         (when (= (gnus-data-number (cadr data)) article)
3252           (setcdr data (cddr data))
3253           (when offset
3254             (gnus-data-update-list (cdr data) offset))
3255           (setq data nil
3256                 gnus-newsgroup-data-reverse nil))
3257         (setq data (cdr data))))))
3258
3259 (defmacro gnus-data-list (backward)
3260   `(if ,backward
3261        (or gnus-newsgroup-data-reverse
3262            (setq gnus-newsgroup-data-reverse
3263                  (reverse gnus-newsgroup-data)))
3264      gnus-newsgroup-data))
3265
3266 (defun gnus-data-update-list (data offset)
3267   "Add OFFSET to the POS of all data entries in DATA."
3268   (setq gnus-newsgroup-data-reverse nil)
3269   (while data
3270     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3271     (setq data (cdr data))))
3272
3273 (defun gnus-summary-article-pseudo-p (article)
3274   "Say whether this article is a pseudo article or not."
3275   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3276
3277 (defmacro gnus-summary-article-sparse-p (article)
3278   "Say whether this article is a sparse article or not."
3279   `(memq ,article gnus-newsgroup-sparse))
3280
3281 (defmacro gnus-summary-article-ancient-p (article)
3282   "Say whether this article is a sparse article or not."
3283   `(memq ,article gnus-newsgroup-ancient))
3284
3285 (defun gnus-article-children (number)
3286   "Return a list of all children to NUMBER."
3287   (let* ((data (gnus-data-find-list number))
3288          (level (gnus-data-level (car data)))
3289          children)
3290     (setq data (cdr data))
3291     (while (and data
3292                 (= (gnus-data-level (car data)) (1+ level)))
3293       (push (gnus-data-number (car data)) children)
3294       (setq data (cdr data)))
3295     children))
3296
3297 (defmacro gnus-summary-skip-intangible ()
3298   "If the current article is intangible, then jump to a different article."
3299   '(let ((to (get-text-property (point) 'gnus-intangible)))
3300      (and to (gnus-summary-goto-subject to))))
3301
3302 (defmacro gnus-summary-article-intangible-p ()
3303   "Say whether this article is intangible or not."
3304   '(get-text-property (point) 'gnus-intangible))
3305
3306 ;; Some summary mode macros.
3307
3308 (defmacro gnus-summary-article-number ()
3309   "The article number of the article on the current line.
3310 If there isn't an article number here, then we return the current
3311 article number."
3312   '(progn
3313      (gnus-summary-skip-intangible)
3314      (or (get-text-property (point) 'gnus-number)
3315          (gnus-summary-last-subject))))
3316
3317 (defmacro gnus-summary-article-header (&optional number)
3318   "Return the header of article NUMBER."
3319   `(gnus-data-header (gnus-data-find
3320                       ,(or number '(gnus-summary-article-number)))))
3321
3322 (defmacro gnus-summary-thread-level (&optional number)
3323   "Return the level of thread that starts with article NUMBER."
3324   `(if (and (eq gnus-summary-make-false-root 'dummy)
3325             (get-text-property (point) 'gnus-intangible))
3326        0
3327      (gnus-data-level (gnus-data-find
3328                        ,(or number '(gnus-summary-article-number))))))
3329
3330 (defmacro gnus-summary-article-mark (&optional number)
3331   "Return the mark of article NUMBER."
3332   `(gnus-data-mark (gnus-data-find
3333                     ,(or number '(gnus-summary-article-number)))))
3334
3335 (defmacro gnus-summary-article-pos (&optional number)
3336   "Return the position of the line of article NUMBER."
3337   `(gnus-data-pos (gnus-data-find
3338                    ,(or number '(gnus-summary-article-number)))))
3339
3340 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3341 (defmacro gnus-summary-article-subject (&optional number)
3342   "Return current subject string or nil if nothing."
3343   `(let ((headers
3344           ,(if number
3345                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3346              '(gnus-data-header (assq (gnus-summary-article-number)
3347                                       gnus-newsgroup-data)))))
3348      (and headers
3349           (vectorp headers)
3350           (mail-header-subject headers))))
3351
3352 (defmacro gnus-summary-article-score (&optional number)
3353   "Return current article score."
3354   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3355                   gnus-newsgroup-scored))
3356        gnus-summary-default-score 0))
3357
3358 (defun gnus-summary-article-children (&optional number)
3359   "Return a list of article numbers that are children of article NUMBER."
3360   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3361          (level (gnus-data-level (car data)))
3362          l children)
3363     (while (and (setq data (cdr data))
3364                 (> (setq l (gnus-data-level (car data))) level))
3365       (and (= (1+ level) l)
3366            (push (gnus-data-number (car data))
3367                  children)))
3368     (nreverse children)))
3369
3370 (defun gnus-summary-article-parent (&optional number)
3371   "Return the article number of the parent of article NUMBER."
3372   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3373                                     (gnus-data-list t)))
3374          (level (gnus-data-level (car data))))
3375     (if (zerop level)
3376         ()                              ; This is a root.
3377       ;; We search until we find an article with a level less than
3378       ;; this one.  That function has to be the parent.
3379       (while (and (setq data (cdr data))
3380                   (not (< (gnus-data-level (car data)) level))))
3381       (and data (gnus-data-number (car data))))))
3382
3383 (defun gnus-unread-mark-p (mark)
3384   "Say whether MARK is the unread mark."
3385   (= mark gnus-unread-mark))
3386
3387 (defun gnus-read-mark-p (mark)
3388   "Say whether MARK is one of the marks that mark as read.
3389 This is all marks except unread, ticked, dormant, and expirable."
3390   (not (or (= mark gnus-unread-mark)
3391            (= mark gnus-ticked-mark)
3392            (= mark gnus-spam-mark)
3393            (= mark gnus-dormant-mark)
3394            (= mark gnus-expirable-mark))))
3395
3396 (defmacro gnus-article-mark (number)
3397   "Return the MARK of article NUMBER.
3398 This macro should only be used when computing the mark the \"first\"
3399 time; i.e., when generating the summary lines.  After that,
3400 `gnus-summary-article-mark' should be used to examine the
3401 marks of articles."
3402   `(cond
3403     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3404     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3405     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3406     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3407     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3408     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3409     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3410     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3411            gnus-ancient-mark))))
3412
3413 ;; Saving hidden threads.
3414
3415 (defmacro gnus-save-hidden-threads (&rest forms)
3416   "Save hidden threads, eval FORMS, and restore the hidden threads."
3417   (let ((config (make-symbol "config")))
3418     `(let ((,config (gnus-hidden-threads-configuration)))
3419        (unwind-protect
3420            (save-excursion
3421              ,@forms)
3422          (gnus-restore-hidden-threads-configuration ,config)))))
3423 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3424 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3425
3426 (defun gnus-data-compute-positions ()
3427   "Compute the positions of all articles."
3428   (setq gnus-newsgroup-data-reverse nil)
3429   (let ((data gnus-newsgroup-data))
3430     (save-excursion
3431       (gnus-save-hidden-threads
3432         (gnus-summary-show-all-threads)
3433         (goto-char (point-min))
3434         (while data
3435           (while (get-text-property (point) 'gnus-intangible)
3436             (forward-line 1))
3437           (gnus-data-set-pos (car data) (+ (point) 3))
3438           (setq data (cdr data))
3439           (forward-line 1))))))
3440
3441 (defun gnus-hidden-threads-configuration ()
3442   "Return the current hidden threads configuration."
3443   (save-excursion
3444     (let (config)
3445       (goto-char (point-min))
3446       (while (not (eobp))
3447         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3448           (push (save-excursion (forward-line 0) (point)) config))
3449         (forward-line 1))
3450       config)))
3451
3452 (defun gnus-restore-hidden-threads-configuration (config)
3453   "Restore hidden threads configuration from CONFIG."
3454   (save-excursion
3455     (let (point (inhibit-read-only t))
3456       (while (setq point (pop config))
3457         (goto-char point)
3458         (gnus-summary-hide-thread)))))
3459
3460 ;; Various summary mode internalish functions.
3461
3462 (defun gnus-mouse-pick-article (e)
3463   (interactive "e")
3464   (mouse-set-point e)
3465   (gnus-summary-next-page nil t))
3466
3467 (defun gnus-summary-set-display-table ()
3468   "Change the display table.
3469 Odd characters have a tendency to mess
3470 up nicely formatted displays - we make all possible glyphs
3471 display only a single character."
3472
3473   ;; We start from the standard display table, if any.
3474   (let ((table (or (copy-sequence standard-display-table)
3475                    (make-display-table)))
3476         (i 32))
3477     ;; Nix out all the control chars...
3478     (while (>= (setq i (1- i)) 0)
3479       (gnus-put-display-table i [??] table))
3480    ;; ... but not newline and cr, of course.  (cr is necessary for the
3481     ;; selective display).
3482     (gnus-put-display-table ?\n nil table)
3483     (gnus-put-display-table ?\r nil table)
3484     ;; We keep TAB as well.
3485     (gnus-put-display-table ?\t nil table)
3486     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3487     ;; Emacs 23 (unicode), that are not set already.
3488     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3489                  160
3490                256)))
3491       (while (>= (setq i (1- i)) 127)
3492         ;; Only modify if the entry is nil.
3493         (unless (gnus-get-display-table i table)
3494           (gnus-put-display-table i [??] table))))
3495     (setq buffer-display-table table)))
3496
3497 (defun gnus-summary-set-article-display-arrow (pos)
3498   "Update the overlay arrow to point to line at position POS."
3499   (when gnus-summary-display-arrow
3500     (make-local-variable 'overlay-arrow-position)
3501     (make-local-variable 'overlay-arrow-string)
3502     (save-excursion
3503       (goto-char pos)
3504       (beginning-of-line)
3505       (unless overlay-arrow-position
3506         (setq overlay-arrow-position (make-marker)))
3507       (setq overlay-arrow-string "=>"
3508             overlay-arrow-position (set-marker overlay-arrow-position
3509                                                (point)
3510                                                (current-buffer))))))
3511
3512 (defun gnus-summary-setup-buffer (group)
3513   "Initialize summary buffer.
3514 If the setup was successful, non-nil is returned."
3515   (let ((buffer (gnus-summary-buffer-name group))
3516         (dead-name (concat "*Dead Summary "
3517                            (gnus-group-decoded-name group) "*")))
3518     ;; If a dead summary buffer exists, we kill it.
3519     (when (gnus-buffer-live-p dead-name)
3520       (gnus-kill-buffer dead-name))
3521     (if (get-buffer buffer)
3522         (progn
3523           (set-buffer buffer)
3524           (setq gnus-summary-buffer (current-buffer))
3525           (not gnus-newsgroup-prepared))
3526       (set-buffer (gnus-get-buffer-create buffer))
3527       (setq gnus-summary-buffer (current-buffer))
3528       (gnus-summary-mode group)
3529       (when (gnus-group-quit-config group)
3530         (set (make-local-variable 'gnus-single-article-buffer) nil))
3531       (make-local-variable 'gnus-article-buffer)
3532       (make-local-variable 'gnus-article-current)
3533       (make-local-variable 'gnus-original-article-buffer)
3534       (setq gnus-newsgroup-name group)
3535       ;; Set any local variables in the group parameters.
3536       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3537       t)))
3538
3539 (defun gnus-set-global-variables ()
3540   "Set the global equivalents of the buffer-local variables.
3541 They are set to the latest values they had.  These reflect the summary
3542 buffer that was in action when the last article was fetched."
3543   (when (derived-mode-p 'gnus-summary-mode)
3544     (setq gnus-summary-buffer (current-buffer))
3545     (let ((name gnus-newsgroup-name)
3546           (marked gnus-newsgroup-marked)
3547           (spam gnus-newsgroup-spam-marked)
3548           (unread gnus-newsgroup-unreads)
3549           (headers gnus-current-headers)
3550           (data gnus-newsgroup-data)
3551           (summary gnus-summary-buffer)
3552           (article-buffer gnus-article-buffer)
3553           (original gnus-original-article-buffer)
3554           (gac gnus-article-current)
3555           (reffed gnus-reffed-article-number)
3556           (score-file gnus-current-score-file)
3557           (default-charset gnus-newsgroup-charset)
3558           vlist)
3559       (let ((locals gnus-newsgroup-variables))
3560         (while locals
3561           (if (consp (car locals))
3562               (push (eval (caar locals)) vlist)
3563             (push (eval (car locals)) vlist))
3564           (setq locals (cdr locals)))
3565         (setq vlist (nreverse vlist)))
3566       (with-temp-buffer
3567         (setq gnus-newsgroup-name name
3568               gnus-newsgroup-marked marked
3569               gnus-newsgroup-spam-marked spam
3570               gnus-newsgroup-unreads unread
3571               gnus-current-headers headers
3572               gnus-newsgroup-data data
3573               gnus-article-current gac
3574               gnus-summary-buffer summary
3575               gnus-article-buffer article-buffer
3576               gnus-original-article-buffer original
3577               gnus-reffed-article-number reffed
3578               gnus-current-score-file score-file
3579               gnus-newsgroup-charset default-charset)
3580         (let ((locals gnus-newsgroup-variables))
3581           (while locals
3582             (if (consp (car locals))
3583                 (set (caar locals) (pop vlist))
3584               (set (car locals) (pop vlist)))
3585             (setq locals (cdr locals))))))))
3586
3587 (defun gnus-summary-article-unread-p (article)
3588   "Say whether ARTICLE is unread or not."
3589   (memq article gnus-newsgroup-unreads))
3590
3591 (defun gnus-summary-first-article-p (&optional article)
3592   "Return whether ARTICLE is the first article in the buffer."
3593   (if (not (setq article (or article (gnus-summary-article-number))))
3594       nil
3595     (eq article (caar gnus-newsgroup-data))))
3596
3597 (defun gnus-summary-last-article-p (&optional article)
3598   "Return whether ARTICLE is the last article in the buffer."
3599   (if (not (setq article (or article (gnus-summary-article-number))))
3600       ;; All non-existent numbers are the last article.  :-)
3601       t
3602     (not (cdr (gnus-data-find-list article)))))
3603
3604 (defun gnus-make-thread-indent-array (&optional n)
3605   (when (or n
3606             (progn (setq n 200) nil)
3607             (null gnus-thread-indent-array)
3608             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3609     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3610           gnus-thread-indent-array-level gnus-thread-indent-level)
3611     (while (>= n 0)
3612       (aset gnus-thread-indent-array n
3613             (make-string (* n gnus-thread-indent-level) ? ))
3614       (setq n (1- n)))))
3615
3616 (defun gnus-update-summary-mark-positions ()
3617   "Compute where the summary marks are to go."
3618   (save-excursion
3619     (when (gnus-buffer-exists-p gnus-summary-buffer)
3620       (set-buffer gnus-summary-buffer))
3621     (let ((spec gnus-summary-line-format-spec)
3622           pos)
3623       (save-excursion
3624         (gnus-set-work-buffer)
3625         (let ((gnus-tmp-unread ?Z)
3626               (gnus-replied-mark ?Z)
3627               (gnus-score-below-mark ?Z)
3628               (gnus-score-over-mark ?Z)
3629               (gnus-undownloaded-mark ?Z)
3630               (gnus-summary-line-format-spec spec)
3631               (gnus-newsgroup-downloadable '(0))
3632               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3633               case-fold-search ignores)
3634           ;; Here, all marks are bound to Z.
3635           (gnus-summary-insert-line header
3636                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3637           (goto-char (point-min))
3638           ;; Memorize the positions of the same characters as dummy marks.
3639           (while (re-search-forward "[A-D]" nil t)
3640             (push (point) ignores))
3641           (erase-buffer)
3642           ;; We use A-D as dummy marks in order to know column positions
3643           ;; where marks should be inserted.
3644           (setq gnus-tmp-unread ?A
3645                 gnus-replied-mark ?B
3646                 gnus-score-below-mark ?C
3647                 gnus-score-over-mark ?C
3648                 gnus-undownloaded-mark ?D)
3649           (gnus-summary-insert-line header
3650                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3651           ;; Ignore characters which aren't dummy marks.
3652           (dolist (p ignores)
3653             (delete-region (goto-char (1- p)) p)
3654             (insert ?Z))
3655           (goto-char (point-min))
3656           (setq pos (list (cons 'unread
3657                                 (and (search-forward "A" nil t)
3658                                      (- (point) (point-min) 1)))))
3659           (goto-char (point-min))
3660           (push (cons 'replied (and (search-forward "B" nil t)
3661                                     (- (point) (point-min) 1)))
3662                 pos)
3663           (goto-char (point-min))
3664           (push (cons 'score (and (search-forward "C" nil t)
3665                                   (- (point) (point-min) 1)))
3666                 pos)
3667           (goto-char (point-min))
3668           (push (cons 'download (and (search-forward "D" nil t)
3669                                      (- (point) (point-min) 1)))
3670                 pos)))
3671       (setq gnus-summary-mark-positions pos))))
3672
3673 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3674   "Insert a dummy root in the summary buffer."
3675   (beginning-of-line)
3676   (gnus-add-text-properties
3677    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3678    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3679
3680 (defun gnus-summary-extract-address-component (from)
3681   (or (car (funcall gnus-extract-address-components from))
3682       from))
3683
3684 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3685   (let ((mail-parse-charset gnus-newsgroup-charset)
3686         (ignored-from-addresses (gnus-ignored-from-addresses))
3687         ;; Is it really necessary to do this next part for each summary line?
3688         ;; Luckily, doesn't seem to slow things down much.
3689         (mail-parse-ignored-charsets
3690          (with-current-buffer gnus-summary-buffer
3691            gnus-newsgroup-ignored-charsets)))
3692     (or
3693      (and ignored-from-addresses
3694           (string-match ignored-from-addresses gnus-tmp-from)
3695           (let ((extra-headers (mail-header-extra header))
3696                 to
3697                 newsgroups)
3698             (cond
3699              ((setq to (cdr (assq 'To extra-headers)))
3700               (concat gnus-summary-to-prefix
3701                       (inline
3702                         (gnus-summary-extract-address-component
3703                          (funcall gnus-decode-encoded-address-function to)))))
3704              ((setq newsgroups
3705                     (or
3706                      (cdr (assq 'Newsgroups extra-headers))
3707                      (and
3708                       (memq 'Newsgroups gnus-extra-headers)
3709                       (eq (car (gnus-find-method-for-group
3710                                 gnus-newsgroup-name)) 'nntp)
3711                       (gnus-group-real-name gnus-newsgroup-name))))
3712               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3713      (gnus-string-mark-left-to-right
3714       (inline
3715         (gnus-summary-extract-address-component gnus-tmp-from))))))
3716
3717 (defun gnus-summary-insert-line (gnus-tmp-header
3718                                  gnus-tmp-level gnus-tmp-current
3719                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3720                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3721                                  &optional gnus-tmp-dummy gnus-tmp-score
3722                                  gnus-tmp-process)
3723   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3724       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3725                                           gnus-tmp-level)))
3726   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3727          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3728          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3729          (gnus-tmp-score-char
3730           (if (or (null gnus-summary-default-score)
3731                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3732                       gnus-summary-zcore-fuzz))
3733               ?                         ;Whitespace
3734             (if (< gnus-tmp-score gnus-summary-default-score)
3735                 gnus-score-below-mark gnus-score-over-mark)))
3736          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3737          (gnus-tmp-replied
3738           (cond (gnus-tmp-process gnus-process-mark)
3739                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3740                  gnus-cached-mark)
3741                 (gnus-tmp-replied gnus-replied-mark)
3742                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3743                  gnus-forwarded-mark)
3744                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3745                  gnus-saved-mark)
3746                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3747                  gnus-unseen-mark)
3748                 (t gnus-no-mark)))
3749          (gnus-tmp-downloaded
3750           (cond (undownloaded
3751                  gnus-undownloaded-mark)
3752                 (gnus-newsgroup-agentized
3753                  gnus-downloaded-mark)
3754                 (t
3755                  gnus-no-mark)))
3756          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3757          (gnus-tmp-name
3758           (cond
3759            ((string-match "<[^>]+> *$" gnus-tmp-from)
3760             (let ((beg (match-beginning 0)))
3761               (or (and (string-match "^\".+\"" gnus-tmp-from)
3762                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3763                   (substring gnus-tmp-from 0 beg))))
3764            ((string-match "(.+)" gnus-tmp-from)
3765             (substring gnus-tmp-from
3766                        (1+ (match-beginning 0)) (1- (match-end 0))))
3767            (t gnus-tmp-from)))
3768          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3769          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3770          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3771          (inhibit-read-only t))
3772     (when (string= gnus-tmp-name "")
3773       (setq gnus-tmp-name gnus-tmp-from))
3774     (unless (numberp gnus-tmp-lines)
3775       (setq gnus-tmp-lines -1))
3776     (if (= gnus-tmp-lines -1)
3777         (setq gnus-tmp-lines "?")
3778       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3779     (condition-case ()
3780         (gnus-put-text-property
3781          (point)
3782          (progn (eval gnus-summary-line-format-spec) (point))
3783          'gnus-number gnus-tmp-number)
3784       (error (gnus-message 5 "Error updating the summary line")))
3785     (when (gnus-visual-p 'summary-highlight 'highlight)
3786       (forward-line -1)
3787       (gnus-summary-highlight-line)
3788       (gnus-run-hooks 'gnus-summary-update-hook)
3789       (forward-line 1))))
3790
3791 (defun gnus-summary-update-line (&optional dont-update)
3792   "Update summary line after change."
3793   (when (and gnus-summary-default-score
3794              (not gnus-summary-inhibit-highlight))
3795     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3796            (article (gnus-summary-article-number))
3797            (score (gnus-summary-article-score article)))
3798       (unless dont-update
3799         (if (and gnus-summary-mark-below
3800                  (< (gnus-summary-article-score)
3801                     gnus-summary-mark-below))
3802             ;; This article has a low score, so we mark it as read.
3803             (when (memq article gnus-newsgroup-unreads)
3804               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3805           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3806             ;; This article was previously marked as read on account
3807             ;; of a low score, but now it has risen, so we mark it as
3808             ;; unread.
3809             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3810         (gnus-summary-update-mark
3811          (if (or (null gnus-summary-default-score)
3812                  (<= (abs (- score gnus-summary-default-score))
3813                      gnus-summary-zcore-fuzz))
3814              ?                          ;Whitespace
3815            (if (< score gnus-summary-default-score)
3816                gnus-score-below-mark gnus-score-over-mark))
3817          'score))
3818       ;; Do visual highlighting.
3819       (when (gnus-visual-p 'summary-highlight 'highlight)
3820         (gnus-summary-highlight-line)
3821         (gnus-run-hooks 'gnus-summary-update-hook)))))
3822
3823 (defvar gnus-tmp-new-adopts nil)
3824
3825 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3826   "Return the number of articles in THREAD.
3827 This may be 0 in some cases -- if none of the articles in
3828 the thread are to be displayed."
3829   (let* ((number
3830          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3831           (cond
3832            ((not (listp thread))
3833             1)
3834            ((and (consp thread) (cdr thread))
3835             (apply
3836              '+ 1 (mapcar
3837                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3838            ((null thread)
3839             1)
3840            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3841             1)
3842            (t 0))))
3843     (when (and level (zerop level) gnus-tmp-new-adopts)
3844       (incf number
3845             (apply '+ (mapcar
3846                        'gnus-summary-number-of-articles-in-thread
3847                        gnus-tmp-new-adopts))))
3848     (if char
3849         (if (> number 1) gnus-not-empty-thread-mark
3850           gnus-empty-thread-mark)
3851       number)))
3852
3853 (defsubst gnus-summary-line-message-size (head)
3854   "Return pretty-printed version of message size.
3855 This function is intended to be used in
3856 `gnus-summary-line-format-alist'."
3857   (let ((c (or (mail-header-chars head) -1)))
3858     (cond ((< c 0) "n/a")               ; chars not available
3859           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3860           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3861           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3862           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3863
3864 (defcustom gnus-user-date-format-alist
3865   '(((gnus-seconds-today) . "Today, %H:%M")
3866     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3867     (604800 . "%A %H:%M")               ; That's one week
3868     ((gnus-seconds-month) . "%A %d")
3869     ((gnus-seconds-year) . "%B %d")
3870     (t . "%b %d %Y"))                   ; This one is used when no other
3871                                         ; does match
3872   "Specifies date format depending on age of article.
3873 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3874 seconds) or a Lisp expression evaluating to a number.  When the age of
3875 the article is less than this number, then use `format-time-string'
3876 with the corresponding FORMAT for displaying the date of the article.
3877 If AGE is not a number or a Lisp expression evaluating to a
3878 non-number, then the corresponding FORMAT is used as a default value.
3879
3880 Note that the list is processed from the beginning, so it should be
3881 sorted by ascending AGE.  Also note that items following the first
3882 non-number AGE will be ignored.
3883
3884 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3885 and `gnus-seconds-year' in the AGE spec.  They return the number of
3886 seconds passed since the start of today, of this month, of this year,
3887 respectively."
3888   :version "24.1"
3889   :group 'gnus-summary-format
3890   :type '(alist :key-type sexp :value-type string))
3891
3892 (defun gnus-user-date (messy-date)
3893   "Format the messy-date according to `gnus-user-date-format-alist'.
3894 Returns \"  ?  \" if there's bad input or if another error occurs.
3895 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3896   (condition-case ()
3897       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3898              (now (gnus-float-time))
3899              ;;If we don't find something suitable we'll use this one
3900              (my-format "%b %d '%y"))
3901         (let* ((difference (- now messy-date))
3902                (templist gnus-user-date-format-alist)
3903                (top (eval (caar templist))))
3904           (while (if (numberp top) (< top difference) (not top))
3905             (progn
3906               (setq templist (cdr templist))
3907               (setq top (eval (caar templist)))))
3908           (if (stringp (cdr (car templist)))
3909               (setq my-format (cdr (car templist)))))
3910         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3911     (error "  ?   ")))
3912
3913 (defun gnus-summary-set-local-parameters (group)
3914   "Go through the local params of GROUP and set all variable specs in that list."
3915   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3916                                         ; really variables.
3917     (dolist (elem (gnus-group-find-parameter group))
3918       (and (consp elem)                 ; Has to be a cons.
3919            (consp (cdr elem))           ; The cdr has to be a list.
3920            (symbolp (car elem))         ; Has to be a symbol in there.
3921            (not (memq (car elem) vars))
3922            (ignore-errors
3923              (push (car elem) vars)
3924              ;; Variables like `gnus-show-threads' that are globally
3925              ;; bound, if used as group parameters, need to get to be
3926              ;; buffer-local, whereas just parameters like `gcc-self',
3927              ;; `timestamp', etc. should not be bound as variables.
3928              (if (boundp (car elem))
3929                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3930                (eval (nth 1 elem))))))))
3931
3932 (defun gnus-summary-read-group (group &optional show-all no-article
3933                                       kill-buffer no-display backward
3934                                       select-articles)
3935   "Start reading news in newsgroup GROUP.
3936 If SHOW-ALL is non-nil, already read articles are also listed.
3937 If NO-ARTICLE is non-nil, no article is selected initially.
3938 If NO-DISPLAY, don't generate the summary buffer contents.
3939 If KILL-BUFFER, it should be a buffer that's killed once the new
3940 summary buffer has been generated.
3941 If BACKWARD, move point to the previous group in the group buffer
3942 If SELECT-ARTICLES, only select those articles from GROUP."
3943   (let (result)
3944     (while (and group
3945                 (null (setq result
3946                             (let ((gnus-auto-select-next nil))
3947                               (or (gnus-summary-read-group-1
3948                                    group show-all no-article
3949                                    kill-buffer no-display
3950                                    select-articles)
3951                                   (setq show-all nil
3952                                         select-articles nil)))))
3953                 (eq gnus-auto-select-next 'quietly))
3954       (set-buffer gnus-group-buffer)
3955       ;; The entry function called above goes to the next
3956       ;; group automatically, so we go two groups back
3957       ;; if we are searching for the previous group.
3958       (when backward
3959         (gnus-group-prev-unread-group 2))
3960       (if (not (equal group (gnus-group-group-name)))
3961           (setq group (gnus-group-group-name))
3962         (setq group nil)))
3963     result))
3964
3965 (defun gnus-summary-read-group-1 (group show-all no-article
3966                                         kill-buffer no-display
3967                                         &optional select-articles)
3968   ;; Killed foreign groups can't be entered.
3969   ;;  (when (and (not (gnus-group-native-p group))
3970   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3971   ;;    (error "Dead non-native groups can't be entered"))
3972   (gnus-message 7 "Retrieving newsgroup: %s..."
3973                 (gnus-group-decoded-name group))
3974   (let* ((new-group (gnus-summary-setup-buffer group))
3975          (quit-config (gnus-group-quit-config group))
3976          (did-select (and new-group (gnus-select-newsgroup
3977                                      group show-all select-articles))))
3978     (cond
3979      ;; This summary buffer exists already, so we just select it.
3980      ((not new-group)
3981       (gnus-set-global-variables)
3982       (when kill-buffer
3983         (gnus-kill-or-deaden-summary kill-buffer))
3984       (gnus-configure-windows 'summary 'force)
3985       (gnus-set-mode-line 'summary)
3986       (gnus-summary-position-point)
3987       (message "")
3988       t)
3989      ;; We couldn't select this group.
3990      ((null did-select)
3991       (when (and (derived-mode-p 'gnus-summary-mode)
3992                  (not (equal (current-buffer) kill-buffer)))
3993         (kill-buffer (current-buffer))
3994         (if (not quit-config)
3995             (progn
3996               ;; Update the info -- marks might need to be removed,
3997               ;; for instance.
3998               (gnus-summary-update-info)
3999               (set-buffer gnus-group-buffer)
4000               (gnus-group-jump-to-group group)
4001               (gnus-group-next-unread-group 1))
4002           (gnus-handle-ephemeral-exit quit-config)))
4003       (if (null (gnus-list-of-unread-articles group))
4004           (gnus-message 3 "Group %s contains no messages" group)
4005         (gnus-message 3 "Can't select group"))
4006       nil)
4007      ;; The user did a `C-g' while prompting for number of articles,
4008      ;; so we exit this group.
4009      ((eq did-select 'quit)
4010       (and (derived-mode-p 'gnus-summary-mode)
4011            (not (equal (current-buffer) kill-buffer))
4012            (kill-buffer (current-buffer)))
4013       (when kill-buffer
4014         (gnus-kill-or-deaden-summary kill-buffer))
4015       (if (not quit-config)
4016           (progn
4017             (set-buffer gnus-group-buffer)
4018             (gnus-group-jump-to-group group)
4019             (gnus-configure-windows 'group 'force))
4020         (gnus-handle-ephemeral-exit quit-config))
4021       ;; Finally signal the quit.
4022       (signal 'quit nil))
4023      ;; The group was successfully selected.
4024      (t
4025       (gnus-set-global-variables)
4026       ;; Save the active value in effect when the group was entered.
4027       (setq gnus-newsgroup-active
4028             (gnus-copy-sequence
4029              (gnus-active gnus-newsgroup-name)))
4030       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4031       ;; You can change the summary buffer in some way with this hook.
4032       (gnus-run-hooks 'gnus-select-group-hook)
4033       (when (memq 'summary (gnus-update-format-specifications
4034                             nil 'summary 'summary-mode 'summary-dummy))
4035         ;; The format specification for the summary line was updated,
4036         ;; so we need to update the mark positions as well.
4037         (gnus-update-summary-mark-positions))
4038       ;; Do score processing.
4039       (when gnus-use-scoring
4040         (gnus-possibly-score-headers))
4041       ;; Check whether to fill in the gaps in the threads.
4042       (when gnus-build-sparse-threads
4043         (gnus-build-sparse-threads))
4044       ;; Find the initial limit.
4045       (if show-all
4046           (let ((gnus-newsgroup-dormant nil))
4047             (gnus-summary-initial-limit show-all))
4048         (gnus-summary-initial-limit show-all))
4049       ;; Generate the summary buffer.
4050       (unless no-display
4051         (gnus-summary-prepare))
4052       (when gnus-use-trees
4053         (gnus-tree-open)
4054         (setq gnus-summary-highlight-line-function
4055               'gnus-tree-highlight-article))
4056       ;; If the summary buffer is empty, but there are some low-scored
4057       ;; articles or some excluded dormants, we include these in the
4058       ;; buffer.
4059       (when (and (zerop (buffer-size))
4060                  (not no-display))
4061         (cond (gnus-newsgroup-dormant
4062                (gnus-summary-limit-include-dormant))
4063               ((and gnus-newsgroup-scored show-all)
4064                (gnus-summary-limit-include-expunged t))))
4065       ;; Function `gnus-apply-kill-file' must be called in this hook.
4066       (gnus-run-hooks 'gnus-apply-kill-hook)
4067       (if (and (zerop (buffer-size))
4068                (not no-display))
4069           (progn
4070             ;; This newsgroup is empty.
4071             (gnus-summary-catchup-and-exit nil t)
4072             (gnus-message 6 "No unread news")
4073             (when kill-buffer
4074               (gnus-kill-or-deaden-summary kill-buffer))
4075             ;; Return nil from this function.
4076             nil)
4077         ;; Hide conversation thread subtrees.  We cannot do this in
4078         ;; gnus-summary-prepare-hook since kill processing may not
4079         ;; work with hidden articles.
4080         (gnus-summary-maybe-hide-threads)
4081         (gnus-configure-windows 'summary)
4082         (when kill-buffer
4083           (gnus-kill-or-deaden-summary kill-buffer))
4084         (gnus-summary-auto-select-subject)
4085         ;; Show first unread article if requested.
4086         (if (and (not no-article)
4087                  (not no-display)
4088                  gnus-newsgroup-unreads
4089                  gnus-auto-select-first)
4090             (progn
4091               (let ((art (gnus-summary-article-number)))
4092                 (when (and art
4093                            gnus-plugged
4094                            (not (memq art gnus-newsgroup-undownloaded))
4095                            (not (memq art gnus-newsgroup-downloadable)))
4096                   (gnus-summary-goto-article art))))
4097           ;; Don't select any articles.
4098           (gnus-summary-position-point)
4099           (gnus-configure-windows 'summary 'force)
4100           (gnus-set-mode-line 'summary))
4101         (when (and gnus-auto-center-group
4102                    (get-buffer-window gnus-group-buffer t))
4103           ;; Gotta use windows, because recenter does weird stuff if
4104           ;; the current buffer ain't the displayed window.
4105           (let ((owin (selected-window)))
4106             (select-window (get-buffer-window gnus-group-buffer t))
4107             (when (gnus-group-goto-group group)
4108               (recenter))
4109             (select-window owin)))
4110         ;; Mark this buffer as "prepared".
4111         (setq gnus-newsgroup-prepared t)
4112         (gnus-run-hooks 'gnus-summary-prepared-hook)
4113         (unless (gnus-ephemeral-group-p group)
4114           (gnus-group-update-group group nil t))
4115         t)))))
4116
4117 (defun gnus-summary-auto-select-subject ()
4118   "Select the subject line on initial group entry."
4119   (goto-char (point-min))
4120   (cond
4121    ((eq gnus-auto-select-subject 'best)
4122     (gnus-summary-best-unread-subject))
4123    ((eq gnus-auto-select-subject 'unread)
4124     (gnus-summary-first-unread-subject))
4125    ((eq gnus-auto-select-subject 'unseen)
4126     (gnus-summary-first-unseen-subject))
4127    ((eq gnus-auto-select-subject 'unseen-or-unread)
4128     (gnus-summary-first-unseen-or-unread-subject))
4129    ((eq gnus-auto-select-subject 'first)
4130     ;; Do nothing.
4131     )
4132    ((functionp gnus-auto-select-subject)
4133     (funcall gnus-auto-select-subject))))
4134
4135 (defun gnus-summary-prepare ()
4136   "Generate the summary buffer."
4137   (interactive)
4138   (let ((inhibit-read-only t))
4139     (erase-buffer)
4140     (setq gnus-newsgroup-data nil
4141           gnus-newsgroup-data-reverse nil)
4142     (gnus-run-hooks 'gnus-summary-generate-hook)
4143     ;; Generate the buffer, either with threads or without.
4144     (when gnus-newsgroup-headers
4145       (gnus-summary-prepare-threads
4146        (if gnus-show-threads
4147            (gnus-sort-gathered-threads
4148             (funcall gnus-summary-thread-gathering-function
4149                      (gnus-sort-threads
4150                       (gnus-cut-threads (gnus-make-threads)))))
4151          ;; Unthreaded display.
4152          (gnus-sort-articles gnus-newsgroup-headers))))
4153     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4154     ;; Call hooks for modifying summary buffer.
4155     (goto-char (point-min))
4156     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4157
4158 (defsubst gnus-general-simplify-subject (subject)
4159   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4160   (setq subject
4161         (cond
4162          ;; Truncate the subject.
4163          (gnus-simplify-subject-functions
4164           (gnus-map-function gnus-simplify-subject-functions subject))
4165          ((numberp gnus-summary-gather-subject-limit)
4166           (setq subject (gnus-simplify-subject-re subject))
4167           (if (> (length subject) gnus-summary-gather-subject-limit)
4168               (substring subject 0 gnus-summary-gather-subject-limit)
4169             subject))
4170          ;; Fuzzily simplify it.
4171          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4172           (gnus-simplify-subject-fuzzy subject))
4173          ;; Just remove the leading "Re:".
4174          (t
4175           (gnus-simplify-subject-re subject))))
4176
4177   (if (and gnus-summary-gather-exclude-subject
4178            (string-match gnus-summary-gather-exclude-subject subject))
4179       nil                         ; This article shouldn't be gathered
4180     subject))
4181
4182 (defun gnus-summary-simplify-subject-query ()
4183   "Query where the respool algorithm would put this article."
4184   (interactive)
4185   (gnus-summary-select-article)
4186   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4187
4188 (defun gnus-gather-threads-by-subject (threads)
4189   "Gather threads by looking at Subject headers."
4190   (if (not gnus-summary-make-false-root)
4191       threads
4192     (let ((hashtb (gnus-make-hashtable 1024))
4193           (prev threads)
4194           (result threads)
4195           subject hthread whole-subject)
4196       (while threads
4197         (setq subject (gnus-general-simplify-subject
4198                        (setq whole-subject (mail-header-subject
4199                                             (caar threads)))))
4200         (when subject
4201           (if (setq hthread (gnus-gethash subject hashtb))
4202               (progn
4203                 ;; We enter a dummy root into the thread, if we
4204                 ;; haven't done that already.
4205                 (unless (stringp (caar hthread))
4206                   (setcar hthread (list whole-subject (car hthread))))
4207                 ;; We add this new gathered thread to this gathered
4208                 ;; thread.
4209                 (setcdr (car hthread)
4210                         (nconc (cdar hthread) (list (car threads))))
4211                 ;; Remove it from the list of threads.
4212                 (setcdr prev (cdr threads))
4213                 (setq threads prev))
4214             ;; Enter this thread into the hash table.
4215             (gnus-sethash subject
4216                           (if gnus-summary-make-false-root-always
4217                               (progn
4218                                 ;; If you want a dummy root above all
4219                                 ;; threads...
4220                                 (setcar threads (list whole-subject
4221                                                       (car threads)))
4222                                 threads)
4223                             threads)
4224                           hashtb)))
4225         (setq prev threads)
4226         (setq threads (cdr threads)))
4227       result)))
4228
4229 (defun gnus-gather-threads-by-references (threads)
4230   "Gather threads by looking at References headers."
4231   (let ((idhashtb (gnus-make-hashtable 1024))
4232         (thhashtb (gnus-make-hashtable 1024))
4233         (prev threads)
4234         (result threads)
4235         ids references id gthread gid entered ref)
4236     (while threads
4237       (when (setq references (mail-header-references (caar threads)))
4238         (setq id (mail-header-id (caar threads))
4239               ids (inline (gnus-split-references references))
4240               entered nil)
4241         (while (setq ref (pop ids))
4242           (setq ids (delete ref ids))
4243           (if (not (setq gid (gnus-gethash ref idhashtb)))
4244               (progn
4245                 (gnus-sethash ref id idhashtb)
4246                 (gnus-sethash id threads thhashtb))
4247             (setq gthread (gnus-gethash gid thhashtb))
4248             (unless entered
4249               ;; We enter a dummy root into the thread, if we
4250               ;; haven't done that already.
4251               (unless (stringp (caar gthread))
4252                 (setcar gthread (list (mail-header-subject (caar gthread))
4253                                       (car gthread))))
4254               ;; We add this new gathered thread to this gathered
4255               ;; thread.
4256               (setcdr (car gthread)
4257                       (nconc (cdar gthread) (list (car threads)))))
4258             ;; Add it into the thread hash table.
4259             (gnus-sethash id gthread thhashtb)
4260             (setq entered t)
4261             ;; Remove it from the list of threads.
4262             (setcdr prev (cdr threads))
4263             (setq threads prev))))
4264       (setq prev threads)
4265       (setq threads (cdr threads)))
4266     result))
4267
4268 (defun gnus-sort-gathered-threads (threads)
4269   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4270   (let ((result threads))
4271     (while threads
4272       (when (stringp (caar threads))
4273         (setcdr (car threads)
4274                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4275       (setq threads (cdr threads)))
4276     result))
4277
4278 (defun gnus-thread-loop-p (root thread)
4279   "Say whether ROOT is in THREAD."
4280   (let ((stack (list thread))
4281         (infloop 0)
4282         th)
4283     (while (setq thread (pop stack))
4284       (setq th (cdr thread))
4285       (while (and th
4286                   (not (eq (caar th) root)))
4287         (pop th))
4288       (if th
4289           ;; We have found a loop.
4290           (let (ref-dep)
4291             (setcdr thread (delq (car th) (cdr thread)))
4292             (if (boundp (setq ref-dep (intern "none"
4293                                               gnus-newsgroup-dependencies)))
4294                 (setcdr (symbol-value ref-dep)
4295                         (nconc (cdr (symbol-value ref-dep))
4296                                (list (car th))))
4297               (set ref-dep (list nil (car th))))
4298             (setq infloop 1
4299                   stack nil))
4300         ;; Push all the subthreads onto the stack.
4301         (push (cdr thread) stack)))
4302     infloop))
4303
4304 (defun gnus-make-threads ()
4305   "Go through the dependency hashtb and find the roots.  Return all threads."
4306   (let (threads)
4307     (while (catch 'infloop
4308              (mapatoms
4309               (lambda (refs)
4310                 ;; Deal with self-referencing References loops.
4311                 (when (and (car (symbol-value refs))
4312                            (not (zerop
4313                                  (apply
4314                                   '+
4315                                   (mapcar
4316                                    (lambda (thread)
4317                                      (gnus-thread-loop-p
4318                                       (car (symbol-value refs)) thread))
4319                                    (cdr (symbol-value refs)))))))
4320                   (setq threads nil)
4321                   (throw 'infloop t))
4322                 (unless (car (symbol-value refs))
4323                   ;; These threads do not refer back to any other
4324                   ;; articles, so they're roots.
4325                   (setq threads (append (cdr (symbol-value refs)) threads))))
4326               gnus-newsgroup-dependencies)))
4327     threads))
4328
4329 ;; Build the thread tree.
4330 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4331   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4332
4333 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4334 if it was already present.
4335
4336 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4337 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4338 Message-IDs will be renamed to a unique Message-ID before being
4339 entered.
4340
4341 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4342   (let* ((id (mail-header-id header))
4343          (id-dep (and id (intern id dependencies)))
4344          parent-id ref ref-dep ref-header replaced)
4345     ;; Enter this `header' in the `dependencies' table.
4346     (cond
4347      ((not id-dep)
4348       (setq header nil))
4349      ;; The first two cases do the normal part: enter a new `header'
4350      ;; in the `dependencies' table.
4351      ((not (boundp id-dep))
4352       (set id-dep (list header)))
4353      ((null (car (symbol-value id-dep)))
4354       (setcar (symbol-value id-dep) header))
4355
4356      ;; From here the `header' was already present in the
4357      ;; `dependencies' table.
4358      (force-new
4359       ;; Overrides an existing entry;
4360       ;; just set the header part of the entry.
4361       (setcar (symbol-value id-dep) header)
4362       (setq replaced t))
4363
4364      ;; Renames the existing `header' to a unique Message-ID.
4365      ((not gnus-summary-ignore-duplicates)
4366       ;; An article with this Message-ID has already been seen.
4367       ;; We rename the Message-ID.
4368       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4369            (list header))
4370       (mail-header-set-id header id))
4371
4372      ;; The last case ignores an existing entry, except it adds any
4373      ;; additional Xrefs (in case the two articles came from different
4374      ;; servers.
4375      ;; Also sets `header' to `nil' meaning that the `dependencies'
4376      ;; table was *not* modified.
4377      (t
4378       (mail-header-set-xref
4379        (car (symbol-value id-dep))
4380        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4381                    "")
4382                (or (mail-header-xref header) "")))
4383       (setq header nil)))
4384
4385     (when (and header (not replaced))
4386       ;; First check that we are not creating a References loop.
4387       (setq parent-id (gnus-parent-id (mail-header-references header)))
4388       (setq ref parent-id)
4389       (while (and ref
4390                   (setq ref-dep (intern-soft ref dependencies))
4391                   (boundp ref-dep)
4392                   (setq ref-header (car (symbol-value ref-dep))))
4393         (if (string= id ref)
4394             ;; Yuk!  This is a reference loop.  Make the article be a
4395             ;; root article.
4396             (progn
4397               (mail-header-set-references (car (symbol-value id-dep)) "none")
4398               (setq ref nil)
4399               (setq parent-id nil))
4400           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4401       (setq ref-dep (intern (or parent-id "none") dependencies))
4402       (if (boundp ref-dep)
4403           (setcdr (symbol-value ref-dep)
4404                   (nconc (cdr (symbol-value ref-dep))
4405                          (list (symbol-value id-dep))))
4406         (set ref-dep (list nil (symbol-value id-dep)))))
4407     header))
4408
4409 (defun gnus-extract-message-id-from-in-reply-to (string)
4410   (if (string-match "<[^>]+>" string)
4411       (substring string (match-beginning 0) (match-end 0))
4412     nil))
4413
4414 (defun gnus-build-sparse-threads ()
4415   (let ((headers gnus-newsgroup-headers)
4416         (mail-parse-charset gnus-newsgroup-charset)
4417         (gnus-summary-ignore-duplicates t)
4418         header references generation relations
4419         subject child end new-child date)
4420     ;; First we create an alist of generations/relations, where
4421     ;; generations is how much we trust the relation, and the relation
4422     ;; is parent/child.
4423     (gnus-message 7 "Making sparse threads...")
4424     (save-excursion
4425       (nnheader-set-temp-buffer " *gnus sparse threads*")
4426       (while (setq header (pop headers))
4427         (when (and (setq references (mail-header-references header))
4428                    (not (string= references "")))
4429           (insert references)
4430           (setq child (mail-header-id header)
4431                 subject (mail-header-subject header)
4432                 date (mail-header-date header)
4433                 generation 0)
4434           (while (search-backward ">" nil t)
4435             (setq end (1+ (point)))
4436             (when (search-backward "<" nil t)
4437               (setq new-child (buffer-substring (point) end))
4438               (push (list (incf generation)
4439                           child (setq child new-child)
4440                           subject date)
4441                     relations)))
4442           (when child
4443             (push (list (1+ generation) child nil subject) relations))
4444           (erase-buffer)))
4445       (kill-buffer (current-buffer)))
4446     ;; Sort over trustworthiness.
4447     (dolist (relation (sort relations 'car-less-than-car))
4448       (when (gnus-dependencies-add-header
4449              (make-full-mail-header
4450               gnus-reffed-article-number
4451               (nth 3 relation) "" (or (nth 4 relation) "")
4452               (nth 1 relation)
4453               (or (nth 2 relation) "") 0 0 "")
4454              gnus-newsgroup-dependencies nil)
4455         (push gnus-reffed-article-number gnus-newsgroup-limit)
4456         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4457         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4458               gnus-newsgroup-reads)
4459         (decf gnus-reffed-article-number)))
4460     (gnus-message 7 "Making sparse threads...done")))
4461
4462 (defun gnus-build-old-threads ()
4463   ;; Look at all the articles that refer back to old articles, and
4464   ;; fetch the headers for the articles that aren't there.  This will
4465   ;; build complete threads - if the roots haven't been expired by the
4466   ;; server, that is.
4467   (let ((mail-parse-charset gnus-newsgroup-charset)
4468         id heads)
4469     (mapatoms
4470      (lambda (refs)
4471        (when (not (car (symbol-value refs)))
4472          (setq heads (cdr (symbol-value refs)))
4473          (while heads
4474            (if (memq (mail-header-number (caar heads))
4475                      gnus-newsgroup-dormant)
4476                (setq heads (cdr heads))
4477              (setq id (symbol-name refs))
4478              (while (and (setq id (gnus-build-get-header id))
4479                          (not (car (gnus-id-to-thread id)))))
4480              (setq heads nil)))))
4481      gnus-newsgroup-dependencies)))
4482
4483 (defsubst gnus-remove-odd-characters (string)
4484   "Translate STRING into something that doesn't contain weird characters."
4485   (mm-subst-char-in-string
4486    ?\r ?\-
4487    (mm-subst-char-in-string ?\n ?\- string t) t))
4488
4489 ;; This function has to be called with point after the article number
4490 ;; on the beginning of the line.
4491 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4492   (let ((eol (point-at-eol))
4493         (buffer (current-buffer))
4494         header references in-reply-to)
4495
4496     ;; overview: [num subject from date id refs chars lines misc]
4497     (unwind-protect
4498         (let (x)
4499           (narrow-to-region (point) eol)
4500           (unless (eobp)
4501             (forward-char))
4502
4503           (setq header
4504                 (make-full-mail-header
4505                  number                 ; number
4506                  (condition-case ()     ; subject
4507                      (gnus-remove-odd-characters
4508                       (funcall gnus-decode-encoded-word-function
4509                                (setq x (nnheader-nov-field))))
4510                    (error x))
4511                  (condition-case ()     ; from
4512                      (gnus-remove-odd-characters
4513                       (funcall gnus-decode-encoded-address-function
4514                                (setq x (nnheader-nov-field))))
4515                    (error x))
4516                  (nnheader-nov-field)   ; date
4517                  (nnheader-nov-read-message-id number)  ; id
4518                  (setq references (nnheader-nov-field)) ; refs
4519                  (nnheader-nov-read-integer) ; chars
4520                  (nnheader-nov-read-integer) ; lines
4521                  (unless (eobp)
4522                    (if (looking-at "Xref: ")
4523                        (goto-char (match-end 0)))
4524                    (nnheader-nov-field)) ; Xref
4525                  (nnheader-nov-parse-extra)))) ; extra
4526
4527       (widen))
4528
4529     (when (and (string= references "")
4530                (setq in-reply-to (mail-header-extra header))
4531                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4532       (mail-header-set-references
4533        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4534
4535     (when gnus-alter-header-function
4536       (funcall gnus-alter-header-function header))
4537     (gnus-dependencies-add-header header dependencies force-new)))
4538
4539 (defun gnus-build-get-header (id)
4540   "Look through the buffer of NOV lines and find the header to ID.
4541 Enter this line into the dependencies hash table, and return
4542 the id of the parent article (if any)."
4543   (let ((deps gnus-newsgroup-dependencies)
4544         found header)
4545     (prog1
4546         (with-current-buffer nntp-server-buffer
4547           (let ((case-fold-search nil))
4548             (goto-char (point-min))
4549             (while (and (not found)
4550                         (search-forward id nil t))
4551               (beginning-of-line)
4552               (setq found (looking-at
4553                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4554                                    (regexp-quote id))))
4555               (or found (beginning-of-line 2)))
4556             (when found
4557               (beginning-of-line)
4558               (and
4559                (setq header (gnus-nov-parse-line
4560                              (read (current-buffer)) deps))
4561                (gnus-parent-id (mail-header-references header))))))
4562       (when header
4563         (let ((number (mail-header-number header)))
4564           (push number gnus-newsgroup-limit)
4565           (push header gnus-newsgroup-headers)
4566           (if (memq number gnus-newsgroup-unselected)
4567               (progn
4568                 (setq gnus-newsgroup-unreads
4569                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4570                                                number))
4571                 (setq gnus-newsgroup-unselected
4572                       (delq number gnus-newsgroup-unselected)))
4573             (push number gnus-newsgroup-ancient)))))))
4574
4575 (defun gnus-build-all-threads ()
4576   "Read all the headers."
4577   (let ((gnus-summary-ignore-duplicates t)
4578         (mail-parse-charset gnus-newsgroup-charset)
4579         (dependencies gnus-newsgroup-dependencies)
4580         header article)
4581     (with-current-buffer nntp-server-buffer
4582       (let ((case-fold-search nil))
4583         (goto-char (point-min))
4584         (while (not (eobp))
4585           (ignore-errors
4586             (setq article (read (current-buffer))
4587                   header (gnus-nov-parse-line article dependencies t)))
4588           (when header
4589             (with-current-buffer gnus-summary-buffer
4590               (push header gnus-newsgroup-headers)
4591               (if (memq (setq article (mail-header-number header))
4592                         gnus-newsgroup-unselected)
4593                   (progn
4594                     (setq gnus-newsgroup-unreads
4595                           (gnus-add-to-sorted-list
4596                            gnus-newsgroup-unreads article))
4597                     (setq gnus-newsgroup-unselected
4598                           (delq article gnus-newsgroup-unselected)))
4599                 (push article gnus-newsgroup-ancient)))
4600             (forward-line 1)))))))
4601
4602 (defun gnus-summary-update-article-line (article header)
4603   "Update the line for ARTICLE using HEADER."
4604   (let* ((id (mail-header-id header))
4605          (thread (gnus-id-to-thread id)))
4606     (unless thread
4607       (error "Article in no thread"))
4608     ;; Update the thread.
4609     (setcar thread header)
4610     (gnus-summary-goto-subject article)
4611     (let* ((datal (gnus-data-find-list article))
4612            (data (car datal))
4613            (inhibit-read-only t)
4614            (level (gnus-summary-thread-level)))
4615       (gnus-delete-line)
4616       (let ((inserted (- (point)
4617                          (progn
4618                            (gnus-summary-insert-line
4619                             header level nil
4620                             (memq article gnus-newsgroup-undownloaded)
4621                             (gnus-article-mark article)
4622                             (memq article gnus-newsgroup-replied)
4623                             (memq article gnus-newsgroup-expirable)
4624                             ;; Only insert the Subject string when it's different
4625                             ;; from the previous Subject string.
4626                             (if (and
4627                                  gnus-show-threads
4628                                  (gnus-subject-equal
4629                                   (condition-case ()
4630                                       (mail-header-subject
4631                                        (gnus-data-header
4632                                         (cadr
4633                                          (gnus-data-find-list
4634                                           article
4635                                           (gnus-data-list t)))))
4636                                     ;; Error on the side of excessive subjects.
4637                                     (error ""))
4638                                   (mail-header-subject header)))
4639                                 ""
4640                               (mail-header-subject header))
4641                             nil (cdr (assq article gnus-newsgroup-scored))
4642                             (memq article gnus-newsgroup-processable))
4643                            (point)))))
4644         (when (cdr datal)
4645           (gnus-data-update-list
4646            (cdr datal)
4647            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4648
4649 (defun gnus-summary-update-article (article &optional iheader)
4650   "Update ARTICLE in the summary buffer."
4651   (set-buffer gnus-summary-buffer)
4652   (let* ((header (gnus-summary-article-header article))
4653          (id (mail-header-id header))
4654          (data (gnus-data-find article))
4655          (thread (gnus-id-to-thread id))
4656          (references (mail-header-references header))
4657          (parent
4658           (gnus-id-to-thread
4659            (or (gnus-parent-id
4660                 (when (and references
4661                            (not (equal "" references)))
4662                   references))
4663                "none")))
4664          (inhibit-read-only t)
4665          (old (car thread)))
4666     (when thread
4667       (unless iheader
4668         (setcar thread nil)
4669         (when parent
4670           (delq thread parent)))
4671       (if (gnus-summary-insert-subject id header)
4672           ;; Set the (possibly) new article number in the data structure.
4673           (gnus-data-set-number data (gnus-id-to-article id))
4674         (setcar thread old)
4675         nil))))
4676
4677 (defun gnus-rebuild-thread (id &optional line)
4678   "Rebuild the thread containing ID.
4679 If LINE, insert the rebuilt thread starting on line LINE."
4680   (let ((inhibit-read-only t)
4681         old-pos current thread data)
4682     (if (not gnus-show-threads)
4683         (setq thread (list (car (gnus-id-to-thread id))))
4684       ;; Get the thread this article is part of.
4685       (setq thread (gnus-remove-thread id)))
4686     (setq old-pos (point-at-bol))
4687     (setq current (save-excursion
4688                     (and (re-search-backward "[\r\n]" nil t)
4689                          (gnus-summary-article-number))))
4690     ;; If this is a gathered thread, we have to go some re-gathering.
4691     (when (stringp (car thread))
4692       (let ((subject (car thread))
4693             roots thr)
4694         (setq thread (cdr thread))
4695         (while thread
4696           (unless (memq (setq thr (gnus-id-to-thread
4697                                    (gnus-root-id
4698                                     (mail-header-id (caar thread)))))
4699                         roots)
4700             (push thr roots))
4701           (setq thread (cdr thread)))
4702         ;; We now have all (unique) roots.
4703         (if (= (length roots) 1)
4704             ;; All the loose roots are now one solid root.
4705             (setq thread (car roots))
4706           (setq thread (cons subject (gnus-sort-threads roots))))))
4707     (let (threads)
4708       ;; We then insert this thread into the summary buffer.
4709       (when line
4710         (goto-char (point-min))
4711         (forward-line (1- line)))
4712       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4713         (if gnus-show-threads
4714             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4715           (gnus-summary-prepare-unthreaded thread))
4716         (setq data (nreverse gnus-newsgroup-data))
4717         (setq threads gnus-newsgroup-threads))
4718       ;; We splice the new data into the data structure.
4719       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4720       ;;!!! then we want to insert at the beginning of the buffer.
4721       ;;!!! That happens to be true with Gnus now, but that may
4722       ;;!!! change in the future.  Perhaps.
4723       (gnus-data-enter-list
4724        (if line nil current) data (- (point) old-pos))
4725       (setq gnus-newsgroup-threads
4726             (nconc threads gnus-newsgroup-threads))
4727       (gnus-data-compute-positions))))
4728
4729 (defun gnus-number-to-header (number)
4730   "Return the header for article NUMBER."
4731   (let ((headers gnus-newsgroup-headers))
4732     (while (and headers
4733                 (not (= number (mail-header-number (car headers)))))
4734       (pop headers))
4735     (when headers
4736       (car headers))))
4737
4738 (defun gnus-parent-headers (in-headers &optional generation)
4739   "Return the headers of the GENERATIONth parent of HEADERS."
4740   (unless generation
4741     (setq generation 1))
4742   (let ((parent t)
4743         (headers in-headers)
4744         references)
4745     (while (and parent
4746                 (not (zerop generation))
4747                 (setq references (mail-header-references headers)))
4748       (setq headers (if (and references
4749                              (setq parent (gnus-parent-id references)))
4750                         (car (gnus-id-to-thread parent))
4751                       nil))
4752       (decf generation))
4753     (and (not (eq headers in-headers))
4754          headers)))
4755
4756 (defun gnus-id-to-thread (id)
4757   "Return the (sub-)thread where ID appears."
4758   (gnus-gethash id gnus-newsgroup-dependencies))
4759
4760 (defun gnus-id-to-article (id)
4761   "Return the article number of ID."
4762   (let ((thread (gnus-id-to-thread id)))
4763     (when (and thread
4764                (car thread))
4765       (mail-header-number (car thread)))))
4766
4767 (defun gnus-id-to-header (id)
4768   "Return the article headers of ID."
4769   (car (gnus-id-to-thread id)))
4770
4771 (defun gnus-article-displayed-root-p (article)
4772   "Say whether ARTICLE is a root(ish) article."
4773   (let ((level (gnus-summary-thread-level article))
4774         (refs (mail-header-references  (gnus-summary-article-header article)))
4775         particle)
4776     (cond
4777      ((null level) nil)
4778      ((zerop level) t)
4779      ((null refs) t)
4780      ((null (gnus-parent-id refs)) t)
4781      ((and (= 1 level)
4782            (null (setq particle (gnus-id-to-article
4783                                  (gnus-parent-id refs))))
4784            (null (gnus-summary-thread-level particle)))))))
4785
4786 (defun gnus-root-id (id)
4787   "Return the id of the root of the thread where ID appears."
4788   (let (last-id prev)
4789     (while (and id (setq prev (car (gnus-id-to-thread id))))
4790       (setq last-id id
4791             id (gnus-parent-id (mail-header-references prev))))
4792     last-id))
4793
4794 (defun gnus-articles-in-thread (thread)
4795   "Return the list of articles in THREAD."
4796   (cons (mail-header-number (car thread))
4797         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4798
4799 (defun gnus-remove-thread (id &optional dont-remove)
4800   "Remove the thread that has ID in it."
4801   (let (headers thread last-id)
4802     ;; First go up in this thread until we find the root.
4803     (setq last-id (gnus-root-id id)
4804           headers (message-flatten-list (gnus-id-to-thread last-id)))
4805     ;; We have now found the real root of this thread.  It might have
4806     ;; been gathered into some loose thread, so we have to search
4807     ;; through the threads to find the thread we wanted.
4808     (let ((threads gnus-newsgroup-threads)
4809           sub)
4810       (while threads
4811         (setq sub (car threads))
4812         (if (stringp (car sub))
4813             ;; This is a gathered thread, so we look at the roots
4814             ;; below it to find whether this article is in this
4815             ;; gathered root.
4816             (progn
4817               (setq sub (cdr sub))
4818               (while sub
4819                 (when (member (caar sub) headers)
4820                   (setq thread (car threads)
4821                         threads nil
4822                         sub nil))
4823                 (setq sub (cdr sub))))
4824           ;; It's an ordinary thread, so we check it.
4825           (when (eq (car sub) (car headers))
4826             (setq thread sub
4827                   threads nil)))
4828         (setq threads (cdr threads)))
4829       ;; If this article is in no thread, then it's a root.
4830       (if thread
4831           (unless dont-remove
4832             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4833         (setq thread (gnus-id-to-thread last-id)))
4834       (when thread
4835         (prog1
4836             thread                      ; We return this thread.
4837           (unless dont-remove
4838             (if (stringp (car thread))
4839                 (progn
4840                   ;; If we use dummy roots, then we have to remove the
4841                   ;; dummy root as well.
4842                   (when (eq gnus-summary-make-false-root 'dummy)
4843                     ;; We go to the dummy root by going to
4844                     ;; the first sub-"thread", and then one line up.
4845                     (gnus-summary-goto-article
4846                      (mail-header-number (caadr thread)))
4847                     (forward-line -1)
4848                     (gnus-delete-line)
4849                     (gnus-data-compute-positions))
4850                   (setq thread (cdr thread))
4851                   (while thread
4852                     (gnus-remove-thread-1 (car thread))
4853                     (setq thread (cdr thread))))
4854               (gnus-remove-thread-1 thread))))))))
4855
4856 (defun gnus-remove-thread-1 (thread)
4857   "Remove the thread THREAD recursively."
4858   (let ((number (mail-header-number (pop thread)))
4859         d)
4860     (setq thread (reverse thread))
4861     (while thread
4862       (gnus-remove-thread-1 (pop thread)))
4863     (when (setq d (gnus-data-find number))
4864       (goto-char (gnus-data-pos d))
4865       (gnus-summary-show-thread)
4866       (gnus-data-remove
4867        number
4868        (- (point-at-bol)
4869           (prog1
4870               (1+ (point-at-eol))
4871             (gnus-delete-line)))))))
4872
4873 (defun gnus-sort-threads-recursive (threads func)
4874   ;; Responsible for sorting the root articles of threads.
4875   (let ((subthread-sort-func (if (eq gnus-subthread-sort-functions
4876                                      'gnus-thread-sort-functions)
4877                                  func
4878                                (gnus-make-sort-function
4879                                 gnus-subthread-sort-functions))))
4880     (sort (mapcar (lambda (thread)
4881                     (cons (car thread)
4882                           (and (cdr thread)
4883                                (gnus-sort-subthreads-recursive
4884                                 (cdr thread) subthread-sort-func))))
4885                   threads) func)))
4886
4887 (defun gnus-sort-subthreads-recursive (threads func)
4888   ;; Responsible for sorting subthreads.
4889   (sort (mapcar (lambda (thread)
4890                   (cons (car thread)
4891                         (and (cdr thread)
4892                              (gnus-sort-subthreads-recursive (cdr thread) func))))
4893                 threads) func))
4894
4895 (defun gnus-sort-threads-loop (threads func)
4896   (let* ((superthread (cons nil threads))
4897          (stack (list (cons superthread threads)))
4898          remaining-threads thread)
4899     (while stack
4900       (setq remaining-threads (cdr (car stack)))
4901       (if remaining-threads
4902           (progn (setq thread (car remaining-threads))
4903                  (setcdr (car stack) (cdr remaining-threads))
4904                  (if (cdr thread)
4905                      (push (cons thread (cdr thread)) stack)))
4906         (setq thread (caar stack))
4907         (setcdr thread (sort (cdr thread) func))
4908         (pop stack)))
4909     (cdr superthread)))
4910
4911 (defun gnus-sort-threads (threads)
4912   "Sort THREADS."
4913   (if (not gnus-thread-sort-functions)
4914       threads
4915     (gnus-message 8 "Sorting threads...")
4916     (prog1
4917         (condition-case nil
4918             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000))
4919                   (sort-func (gnus-make-sort-function gnus-thread-sort-functions)))
4920               (gnus-sort-threads-recursive threads sort-func))
4921           ;; Even after binding max-lisp-eval-depth, the recursive
4922           ;; sorter might fail for very long threads.  In that case,
4923           ;; try using a (less well-tested) non-recursive sorter.
4924           (error (gnus-message 9 "Sorting threads with loop...")
4925                  (gnus-sort-threads-loop
4926                   threads (gnus-make-sort-function
4927                            gnus-thread-sort-functions))))
4928       (gnus-message 8 "Sorting threads...done"))))
4929
4930 (defun gnus-sort-articles (articles)
4931   "Sort ARTICLES."
4932   (when gnus-article-sort-functions
4933     (gnus-message 7 "Sorting articles...")
4934     (prog1
4935         (setq gnus-newsgroup-headers
4936               (sort articles (gnus-make-sort-function
4937                               gnus-article-sort-functions)))
4938       (gnus-message 7 "Sorting articles...done"))))
4939
4940 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4941 (defmacro gnus-thread-header (thread)
4942   "Return header of first article in THREAD.
4943 Note that THREAD must never, ever be anything else than a variable -
4944 using some other form will lead to serious barfage."
4945   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4946   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4947   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4948         (vector thread) 2))
4949
4950 (defsubst gnus-article-sort-by-number (h1 h2)
4951   "Sort articles by article number."
4952   (< (mail-header-number h1)
4953      (mail-header-number h2)))
4954
4955 (defun gnus-thread-sort-by-number (h1 h2)
4956   "Sort threads by root article number."
4957   (gnus-article-sort-by-number
4958    (gnus-thread-header h1) (gnus-thread-header h2)))
4959
4960 (defsubst gnus-article-sort-by-random (h1 h2)
4961   "Sort articles randomly."
4962   (zerop (random 2)))
4963
4964 (defun gnus-thread-sort-by-random (h1 h2)
4965   "Sort threads randomly."
4966   (gnus-article-sort-by-random
4967    (gnus-thread-header h1) (gnus-thread-header h2)))
4968
4969 (defsubst gnus-article-sort-by-lines (h1 h2)
4970   "Sort articles by article Lines header."
4971   (< (mail-header-lines h1)
4972      (mail-header-lines h2)))
4973
4974 (defun gnus-thread-sort-by-lines (h1 h2)
4975   "Sort threads by root article Lines header."
4976   (gnus-article-sort-by-lines
4977    (gnus-thread-header h1) (gnus-thread-header h2)))
4978
4979 (defsubst gnus-article-sort-by-chars (h1 h2)
4980   "Sort articles by octet length."
4981   (< (mail-header-chars h1)
4982      (mail-header-chars h2)))
4983
4984 (defun gnus-thread-sort-by-chars (h1 h2)
4985   "Sort threads by root article octet length."
4986   (gnus-article-sort-by-chars
4987    (gnus-thread-header h1) (gnus-thread-header h2)))
4988
4989 (defsubst gnus-article-sort-by-author (h1 h2)
4990   "Sort articles by root author."
4991   (gnus-string<
4992    (let ((extract (funcall
4993                    gnus-extract-address-components
4994                    (mail-header-from h1))))
4995      (or (car extract) (cadr extract) ""))
4996    (let ((extract (funcall
4997                    gnus-extract-address-components
4998                    (mail-header-from h2))))
4999      (or (car extract) (cadr extract) ""))))
5000
5001 (defun gnus-thread-sort-by-author (h1 h2)
5002   "Sort threads by root author."
5003   (gnus-article-sort-by-author
5004    (gnus-thread-header h1)  (gnus-thread-header h2)))
5005
5006 (defsubst gnus-article-sort-by-recipient (h1 h2)
5007   "Sort articles by recipient."
5008   (gnus-string<
5009    (let ((extract (funcall
5010                    gnus-extract-address-components
5011                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
5012      (or (car extract) (cadr extract)))
5013    (let ((extract (funcall
5014                    gnus-extract-address-components
5015                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
5016      (or (car extract) (cadr extract)))))
5017
5018 (defun gnus-thread-sort-by-recipient (h1 h2)
5019   "Sort threads by root recipient."
5020   (gnus-article-sort-by-recipient
5021    (gnus-thread-header h1) (gnus-thread-header h2)))
5022
5023 (defsubst gnus-article-sort-by-subject (h1 h2)
5024   "Sort articles by root subject."
5025   (gnus-string<
5026    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5027    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5028
5029 (defun gnus-thread-sort-by-subject (h1 h2)
5030   "Sort threads by root subject."
5031   (gnus-article-sort-by-subject
5032    (gnus-thread-header h1) (gnus-thread-header h2)))
5033
5034 (defsubst gnus-article-sort-by-date (h1 h2)
5035   "Sort articles by root article date."
5036   (time-less-p
5037    (gnus-date-get-time (mail-header-date h1))
5038    (gnus-date-get-time (mail-header-date h2))))
5039
5040 (defun gnus-thread-sort-by-date (h1 h2)
5041   "Sort threads by root article date."
5042   (gnus-article-sort-by-date
5043    (gnus-thread-header h1) (gnus-thread-header h2)))
5044
5045 (defsubst gnus-article-sort-by-score (h1 h2)
5046   "Sort articles by root article score.
5047 Unscored articles will be counted as having a score of zero."
5048   (> (or (cdr (assq (mail-header-number h1)
5049                     gnus-newsgroup-scored))
5050          gnus-summary-default-score 0)
5051      (or (cdr (assq (mail-header-number h2)
5052                     gnus-newsgroup-scored))
5053          gnus-summary-default-score 0)))
5054
5055 (defun gnus-thread-sort-by-score (h1 h2)
5056   "Sort threads by root article score."
5057   (gnus-article-sort-by-score
5058    (gnus-thread-header h1) (gnus-thread-header h2)))
5059
5060 (defun gnus-thread-sort-by-total-score (h1 h2)
5061   "Sort threads by the sum of all scores in the thread.
5062 Unscored articles will be counted as having a score of zero."
5063   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5064
5065 (defun gnus-thread-total-score (thread)
5066   ;; This function find the total score of THREAD.
5067   (cond
5068    ((null thread)
5069     0)
5070    ((consp thread)
5071     (if (stringp (car thread))
5072         (apply gnus-thread-score-function 0
5073                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5074       (gnus-thread-total-score-1 thread)))
5075    (t
5076     (gnus-thread-total-score-1 (list thread)))))
5077
5078 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5079   "Sort articles by number."
5080   (gnus-article-sort-by-number h1 h2))
5081
5082 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5083   "Sort threads such that the thread with the most recently arrived article comes first."
5084   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5085
5086 (defun gnus-thread-highest-number (thread)
5087   "Return the highest article number in THREAD."
5088   (apply 'max (mapcar (lambda (header)
5089                         (mail-header-number header))
5090                       (message-flatten-list thread))))
5091
5092 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5093   "Sort articles by number."
5094   (gnus-article-sort-by-date h1 h2))
5095
5096 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5097   "Sort threads such that the thread with the most recently dated article comes first."
5098   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5099
5100 ; Since this is called not only to sort the top-level threads, but
5101 ; also in recursive sorts to order the articles within a thread, each
5102 ; article will be processed many times.  Thus it speeds things up
5103 ; quite a bit to use gnus-date-get-time, which caches the time value.
5104 (defun gnus-thread-latest-date (thread)
5105   "Return the highest article date in THREAD."
5106   (apply 'max
5107          (mapcar (lambda (header) (gnus-float-time
5108                                    (gnus-date-get-time
5109                                     (mail-header-date header))))
5110                  (message-flatten-list thread))))
5111
5112 (defun gnus-thread-total-score-1 (root)
5113   ;; This function find the total score of the thread below ROOT.
5114   (setq root (car root))
5115   (apply gnus-thread-score-function
5116          (or (append
5117               (mapcar 'gnus-thread-total-score
5118                       (cdr (gnus-id-to-thread (mail-header-id root))))
5119               (when (> (mail-header-number root) 0)
5120                 (list (or (cdr (assq (mail-header-number root)
5121                                      gnus-newsgroup-scored))
5122                           gnus-summary-default-score 0))))
5123              (list gnus-summary-default-score)
5124              '(0))))
5125
5126 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5127 (defvar gnus-tmp-prev-subject nil)
5128 (defvar gnus-tmp-false-parent nil)
5129 (defvar gnus-tmp-root-expunged nil)
5130 (defvar gnus-tmp-dummy-line nil)
5131
5132 (defun gnus-extra-header (type &optional header)
5133   "Return the extra header of TYPE."
5134   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5135       ""))
5136
5137 (defvar gnus-tmp-thread-tree-header-string "")
5138
5139 (defcustom gnus-sum-thread-tree-root "> "
5140   "With %B spec, used for the root of a thread.
5141 If nil, use subject instead."
5142   :version "22.1"
5143   :type '(radio (const :format "%v  " nil) string)
5144   :group 'gnus-thread)
5145
5146 (defcustom gnus-sum-thread-tree-false-root "> "
5147   "With %B spec, used for a false root of a thread.
5148 If nil, use subject instead."
5149   :version "22.1"
5150   :type '(radio (const :format "%v  " nil) string)
5151   :group 'gnus-thread)
5152
5153 (defcustom gnus-sum-thread-tree-single-indent ""
5154   "With %B spec, used for a thread with just one message.
5155 If nil, use subject instead."
5156   :version "22.1"
5157   :type '(radio (const :format "%v  " nil) string)
5158   :group 'gnus-thread)
5159
5160 (defcustom gnus-sum-thread-tree-vertical "| "
5161   "With %B spec, used for drawing a vertical line."
5162   :version "22.1"
5163   :type 'string
5164   :group 'gnus-thread)
5165
5166 (defcustom gnus-sum-thread-tree-indent "  "
5167   "With %B spec, used for indenting."
5168   :version "22.1"
5169   :type 'string
5170   :group 'gnus-thread)
5171
5172 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5173   "With %B spec, used for a leaf with brothers."
5174   :version "22.1"
5175   :type 'string
5176   :group 'gnus-thread)
5177
5178 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5179   "With %B spec, used for a leaf without brothers."
5180   :version "22.1"
5181   :type 'string
5182   :group 'gnus-thread)
5183
5184 (defcustom gnus-summary-display-while-building nil
5185   "If non-nil, show and update the summary buffer as it's being built.
5186 If the value is t, update the buffer after every line is inserted.  If
5187 the value is an integer (N), update the display every N lines."
5188   :version "22.1"
5189   :group 'gnus-thread
5190   :type '(choice (const :tag "off" nil)
5191                  number
5192                  (const :tag "frequently" t)))
5193
5194 (defun gnus-summary-prepare-threads (threads)
5195   "Prepare summary buffer from THREADS and indentation LEVEL.
5196 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5197 or a straight list of headers."
5198   (gnus-message 7 "Generating summary...")
5199
5200   (setq gnus-newsgroup-threads threads)
5201   (beginning-of-line)
5202
5203   (let ((gnus-tmp-level 0)
5204         (default-score (or gnus-summary-default-score 0))
5205         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5206         (building-line-count gnus-summary-display-while-building)
5207         (building-count (integerp gnus-summary-display-while-building))
5208         thread number subject stack state gnus-tmp-gathered beg-match
5209         new-roots gnus-tmp-new-adopts thread-end simp-subject
5210         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5211         gnus-tmp-replied gnus-tmp-subject-or-nil
5212         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5213         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5214         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5215         tree-stack)
5216
5217     (setq gnus-tmp-prev-subject nil
5218           gnus-tmp-thread-tree-header-string "")
5219
5220     (if (vectorp (car threads))
5221         ;; If this is a straight (sic) list of headers, then a
5222         ;; threaded summary display isn't required, so we just create
5223         ;; an unthreaded one.
5224         (gnus-summary-prepare-unthreaded threads)
5225
5226       ;; Do the threaded display.
5227
5228       (if gnus-summary-display-while-building
5229           (switch-to-buffer (buffer-name)))
5230       (while (or threads stack gnus-tmp-new-adopts new-roots)
5231
5232         (if (and (= gnus-tmp-level 0)
5233                  (or (not stack)
5234                      (= (caar stack) 0))
5235                  (not gnus-tmp-false-parent)
5236                  (or gnus-tmp-new-adopts new-roots))
5237             (if gnus-tmp-new-adopts
5238                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5239                       thread (list (car gnus-tmp-new-adopts))
5240                       gnus-tmp-header (caar thread)
5241                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5242               (when new-roots
5243                 (setq thread (list (car new-roots))
5244                       gnus-tmp-header (caar thread)
5245                       new-roots (cdr new-roots))))
5246
5247           (if threads
5248               ;; If there are some threads, we do them before the
5249               ;; threads on the stack.
5250               (setq thread threads
5251                     gnus-tmp-header (caar thread))
5252             ;; There were no current threads, so we pop something off
5253             ;; the stack.
5254             (setq state (car stack)
5255                   gnus-tmp-level (car state)
5256                   tree-stack (cadr state)
5257                   thread (caddr state)
5258                   stack (cdr stack)
5259                   gnus-tmp-header (caar thread))))
5260
5261         (setq gnus-tmp-false-parent nil)
5262         (setq gnus-tmp-root-expunged nil)
5263         (setq thread-end nil)
5264
5265         (if (stringp gnus-tmp-header)
5266             ;; The header is a dummy root.
5267             (cond
5268              ((eq gnus-summary-make-false-root 'adopt)
5269               ;; We let the first article adopt the rest.
5270               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5271                                                (cddar thread)))
5272               (setq gnus-tmp-gathered
5273                     (nconc (mapcar
5274                             (lambda (h) (mail-header-number (car h)))
5275                             (cddar thread))
5276                            gnus-tmp-gathered))
5277               (setq thread (cons (list (caar thread)
5278                                        (cadar thread))
5279                                  (cdr thread)))
5280               (setq gnus-tmp-level -1
5281                     gnus-tmp-false-parent t))
5282              ((eq gnus-summary-make-false-root 'empty)
5283               ;; We print adopted articles with empty subject fields.
5284               (setq gnus-tmp-gathered
5285                     (nconc (mapcar
5286                             (lambda (h) (mail-header-number (car h)))
5287                             (cddar thread))
5288                            gnus-tmp-gathered))
5289               (setq gnus-tmp-level -1))
5290              ((eq gnus-summary-make-false-root 'dummy)
5291               ;; We remember that we probably want to output a dummy
5292               ;; root.
5293               (setq gnus-tmp-dummy-line gnus-tmp-header)
5294               (setq gnus-tmp-prev-subject gnus-tmp-header))
5295              (t
5296               ;; We do not make a root for the gathered
5297               ;; sub-threads at all.
5298               (setq gnus-tmp-level -1)))
5299
5300           (setq number (mail-header-number gnus-tmp-header)
5301                 subject (mail-header-subject gnus-tmp-header)
5302                 simp-subject (gnus-simplify-subject-fully subject))
5303
5304           (cond
5305            ;; If the thread has changed subject, we might want to make
5306            ;; this subthread into a root.
5307            ((and (null gnus-thread-ignore-subject)
5308                  (not (zerop gnus-tmp-level))
5309                  gnus-tmp-prev-subject
5310                  (not (string= gnus-tmp-prev-subject simp-subject)))
5311             (setq new-roots (nconc new-roots (list (car thread)))
5312                   thread-end t
5313                   gnus-tmp-header nil))
5314            ;; If the article lies outside the current limit,
5315            ;; then we do not display it.
5316            ((not (memq number gnus-newsgroup-limit))
5317             (setq gnus-tmp-gathered
5318                   (nconc (mapcar
5319                           (lambda (h) (mail-header-number (car h)))
5320                           (cdar thread))
5321                          gnus-tmp-gathered))
5322             (setq gnus-tmp-new-adopts (if (cdar thread)
5323                                           (append gnus-tmp-new-adopts
5324                                                   (cdar thread))
5325                                         gnus-tmp-new-adopts)
5326                   thread-end t
5327                   gnus-tmp-header nil)
5328             (when (zerop gnus-tmp-level)
5329               (setq gnus-tmp-root-expunged t)))
5330            ;; Perhaps this article is to be marked as read?
5331            ((and gnus-summary-mark-below
5332                  (< (or (cdr (assq number gnus-newsgroup-scored))
5333                         default-score)
5334                     gnus-summary-mark-below)
5335                  ;; Don't touch sparse articles.
5336                  (not (gnus-summary-article-sparse-p number))
5337                  (not (gnus-summary-article-ancient-p number)))
5338             (setq gnus-newsgroup-unreads
5339                   (delq number gnus-newsgroup-unreads))
5340             (if gnus-newsgroup-auto-expire
5341                 (setq gnus-newsgroup-expirable
5342                       (gnus-add-to-sorted-list
5343                        gnus-newsgroup-expirable number))
5344               (push (cons number gnus-low-score-mark)
5345                     gnus-newsgroup-reads))))
5346
5347           (when gnus-tmp-header
5348             ;; We may have an old dummy line to output before this
5349             ;; article.
5350             (when (and gnus-tmp-dummy-line
5351                        (gnus-subject-equal
5352                         gnus-tmp-dummy-line
5353                         (mail-header-subject gnus-tmp-header)))
5354               (gnus-summary-insert-dummy-line
5355                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5356               (setq gnus-tmp-dummy-line nil))
5357
5358             ;; Compute the mark.
5359             (setq gnus-tmp-unread (gnus-article-mark number))
5360
5361             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5362                                   gnus-tmp-header gnus-tmp-level)
5363                   gnus-newsgroup-data)
5364
5365             ;; Actually insert the line.
5366             (setq
5367              gnus-tmp-subject-or-nil
5368              (cond
5369               ((and gnus-thread-ignore-subject
5370                     gnus-tmp-prev-subject
5371                     (not (string= gnus-tmp-prev-subject simp-subject)))
5372                subject)
5373               ((zerop gnus-tmp-level)
5374                (if (and (eq gnus-summary-make-false-root 'empty)
5375                         (memq number gnus-tmp-gathered)
5376                         gnus-tmp-prev-subject
5377                         (string= gnus-tmp-prev-subject simp-subject))
5378                    gnus-summary-same-subject
5379                  subject))
5380               (t gnus-summary-same-subject)))
5381             (if (and (eq gnus-summary-make-false-root 'adopt)
5382                      (= gnus-tmp-level 1)
5383                      (memq number gnus-tmp-gathered))
5384                 (setq gnus-tmp-opening-bracket ?\<
5385                       gnus-tmp-closing-bracket ?\>)
5386               (setq gnus-tmp-opening-bracket ?\[
5387                     gnus-tmp-closing-bracket ?\]))
5388             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5389                 (gnus-make-thread-indent-array
5390                  (max (* 2 (length gnus-thread-indent-array))
5391                       gnus-tmp-level)))
5392             (setq
5393              gnus-tmp-indentation
5394              (aref gnus-thread-indent-array gnus-tmp-level)
5395              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5396              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5397                                 gnus-summary-default-score 0)
5398              gnus-tmp-score-char
5399              (if (or (null gnus-summary-default-score)
5400                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5401                          gnus-summary-zcore-fuzz))
5402                  ?                      ;Whitespace
5403                (if (< gnus-tmp-score gnus-summary-default-score)
5404                    gnus-score-below-mark gnus-score-over-mark))
5405              gnus-tmp-replied
5406              (cond ((memq number gnus-newsgroup-processable)
5407                     gnus-process-mark)
5408                    ((memq number gnus-newsgroup-cached)
5409                     gnus-cached-mark)
5410                    ((memq number gnus-newsgroup-replied)
5411                     gnus-replied-mark)
5412                    ((memq number gnus-newsgroup-forwarded)
5413                     gnus-forwarded-mark)
5414                    ((memq number gnus-newsgroup-saved)
5415                     gnus-saved-mark)
5416                    ((memq number gnus-newsgroup-unseen)
5417                     gnus-unseen-mark)
5418                    (t gnus-no-mark))
5419              gnus-tmp-downloaded
5420              (cond ((memq number gnus-newsgroup-undownloaded)
5421                     gnus-undownloaded-mark)
5422                    (gnus-newsgroup-agentized
5423                     gnus-downloaded-mark)
5424                    (t
5425                     gnus-no-mark))
5426              gnus-tmp-from (mail-header-from gnus-tmp-header)
5427              gnus-tmp-name
5428              (cond
5429               ((string-match "<[^>]+> *$" gnus-tmp-from)
5430                (setq beg-match (match-beginning 0))
5431                (or (and (string-match "^\".+\"" gnus-tmp-from)
5432                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5433                    (substring gnus-tmp-from 0 beg-match)))
5434               ((string-match "(.+)" gnus-tmp-from)
5435                (substring gnus-tmp-from
5436                           (1+ (match-beginning 0)) (1- (match-end 0))))
5437               (t gnus-tmp-from))
5438
5439              ;; Do the %B string
5440              gnus-tmp-thread-tree-header-string
5441              (cond
5442               ((not gnus-show-threads) "")
5443               ((zerop gnus-tmp-level)
5444                (cond ((cdar thread)
5445                       (or gnus-sum-thread-tree-root subject))
5446                      (gnus-tmp-new-adopts
5447                       (or gnus-sum-thread-tree-false-root subject))
5448                      (t
5449                       (or gnus-sum-thread-tree-single-indent subject))))
5450               (t
5451                (concat (apply 'concat
5452                               (mapcar (lambda (item)
5453                                         (if (= item 1)
5454                                             gnus-sum-thread-tree-vertical
5455                                           gnus-sum-thread-tree-indent))
5456                                       (cdr (reverse tree-stack))))
5457                        (if (nth 1 thread)
5458                            gnus-sum-thread-tree-leaf-with-other
5459                          gnus-sum-thread-tree-single-leaf)))))
5460             (when (string= gnus-tmp-name "")
5461               (setq gnus-tmp-name gnus-tmp-from))
5462             (unless (numberp gnus-tmp-lines)
5463               (setq gnus-tmp-lines -1))
5464             (if (= gnus-tmp-lines -1)
5465                 (setq gnus-tmp-lines "?")
5466               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5467             (gnus-put-text-property
5468              (point)
5469              (progn (eval gnus-summary-line-format-spec) (point))
5470              'gnus-number number)
5471             (when gnus-visual-p
5472               (forward-line -1)
5473               (gnus-summary-highlight-line)
5474               (when gnus-summary-update-hook
5475                 (gnus-run-hooks 'gnus-summary-update-hook))
5476               (forward-line 1))
5477
5478             (setq gnus-tmp-prev-subject simp-subject)))
5479
5480         (when (nth 1 thread)
5481           (push (list (max 0 gnus-tmp-level)
5482                       (copy-sequence tree-stack)
5483                       (nthcdr 1 thread))
5484                 stack))
5485         (push (if (nth 1 thread) 1 0) tree-stack)
5486         (incf gnus-tmp-level)
5487         (setq threads (if thread-end nil (cdar thread)))
5488         (if gnus-summary-display-while-building
5489             (if building-count
5490                 (progn
5491                   ;; use a set frequency
5492                   (setq building-line-count (1- building-line-count))
5493                   (when (= building-line-count 0)
5494                     (sit-for 0)
5495                     (setq building-line-count
5496                           gnus-summary-display-while-building)))
5497               ;; always
5498               (sit-for 0)))
5499         (unless threads
5500           (setq gnus-tmp-level 0)))))
5501   (gnus-message 7 "Generating summary...done"))
5502
5503 (defun gnus-summary-prepare-unthreaded (headers)
5504   "Generate an unthreaded summary buffer based on HEADERS."
5505   (let (header number mark)
5506
5507     (beginning-of-line)
5508
5509     (while headers
5510       ;; We may have to root out some bad articles...
5511       (when (memq (setq number (mail-header-number
5512                                 (setq header (pop headers))))
5513                   gnus-newsgroup-limit)
5514         ;; Mark article as read when it has a low score.
5515         (when (and gnus-summary-mark-below
5516                    (< (or (cdr (assq number gnus-newsgroup-scored))
5517                           gnus-summary-default-score 0)
5518                       gnus-summary-mark-below)
5519                    (not (gnus-summary-article-ancient-p number)))
5520           (setq gnus-newsgroup-unreads
5521                 (delq number gnus-newsgroup-unreads))
5522           (if gnus-newsgroup-auto-expire
5523               (push number gnus-newsgroup-expirable)
5524             (push (cons number gnus-low-score-mark)
5525                   gnus-newsgroup-reads)))
5526
5527         (setq mark (gnus-article-mark number))
5528         (push (gnus-data-make number mark (1+ (point)) header 0)
5529               gnus-newsgroup-data)
5530         (gnus-summary-insert-line
5531          header 0 number
5532          (memq number gnus-newsgroup-undownloaded)
5533          mark (memq number gnus-newsgroup-replied)
5534          (memq number gnus-newsgroup-expirable)
5535          (mail-header-subject header) nil
5536          (cdr (assq number gnus-newsgroup-scored))
5537          (memq number gnus-newsgroup-processable))))))
5538
5539 (declare-function gnus-parameter-list-identifier "gnus-art" (name) t)
5540
5541 (defun gnus-group-get-list-identifiers (group)
5542   "Get list identifier regexp for GROUP."
5543   (or (gnus-parameter-list-identifier group)
5544       (if (consp gnus-list-identifiers)
5545           (mapconcat 'identity gnus-list-identifiers " *\\|")
5546         gnus-list-identifiers)))
5547
5548 (defun gnus-summary-remove-list-identifiers ()
5549   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5550   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5551         changed subject)
5552     (when regexp
5553       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5554       (dolist (header gnus-newsgroup-headers)
5555         (setq subject (mail-header-subject header)
5556               changed nil)
5557         (while (string-match regexp subject)
5558           (setq subject
5559                 (concat (substring subject 0 (match-beginning 1))
5560                         (substring subject (match-end 0)))
5561                 changed t))
5562         (when changed
5563           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5564             (setq subject
5565                   (concat (substring subject 0 (match-beginning 1))
5566                           (substring subject (match-end 1)))))
5567           (mail-header-set-subject header subject))))))
5568
5569 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5570   "Fetch headers of ARTICLES."
5571   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5572     (gnus-message 7 "Fetching headers for %s..." name)
5573     (prog1
5574         (if (eq 'nov
5575                 (setq gnus-headers-retrieved-by
5576                       (gnus-retrieve-headers
5577                        articles gnus-newsgroup-name
5578                        (or limit
5579                            ;; We might want to fetch old headers, but
5580                            ;; not if there is only 1 article.
5581                            (and (or (and
5582                                      (not (eq gnus-fetch-old-headers 'some))
5583                                      (not (numberp gnus-fetch-old-headers)))
5584                                     (> (length articles) 1))
5585                                 gnus-fetch-old-headers)))))
5586             (gnus-get-newsgroup-headers-xover
5587              articles force-new dependencies gnus-newsgroup-name t)
5588           (gnus-get-newsgroup-headers dependencies force-new))
5589       (gnus-message 7 "Fetching headers for %s...done" name))))
5590
5591 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5592   "Select newsgroup GROUP.
5593 If READ-ALL is non-nil, all articles in the group are selected.
5594 If SELECT-ARTICLES, only select those articles from GROUP."
5595   (let* ((entry (gnus-group-entry group))
5596          ;;!!! Dirty hack; should be removed.
5597          (gnus-summary-ignore-duplicates
5598           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5599               t
5600             gnus-summary-ignore-duplicates))
5601          (info (nth 2 entry))
5602          charset articles fetched-articles cached)
5603
5604     (unless (gnus-check-server
5605              (set (make-local-variable 'gnus-current-select-method)
5606                   (gnus-find-method-for-group group)))
5607       (error "Couldn't open server"))
5608     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5609
5610     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5611         (gnus-activate-group group)     ; Or we can activate it...
5612         (progn                          ; Or we bug out.
5613           (when (derived-mode-p 'gnus-summary-mode)
5614             (gnus-kill-buffer (current-buffer)))
5615           (error
5616            "Couldn't activate group %s: %s"
5617            (mm-decode-coding-string group charset)
5618            (mm-decode-coding-string (gnus-status-message group) charset))))
5619
5620     (unless (gnus-request-group group t)
5621       (when (derived-mode-p 'gnus-summary-mode)
5622         (gnus-kill-buffer (current-buffer)))
5623       (error "Couldn't request group %s: %s"
5624              (mm-decode-coding-string group charset)
5625              (mm-decode-coding-string (gnus-status-message group) charset)))
5626
5627     (when (and gnus-agent
5628                (gnus-active group))
5629       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5630
5631       (setq gnus-summary-use-undownloaded-faces
5632             (gnus-agent-find-parameter
5633              group
5634              'agent-enable-undownloaded-faces)))
5635
5636     (setq gnus-newsgroup-name group
5637           gnus-newsgroup-unselected nil
5638           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5639
5640     (let ((display (gnus-group-find-parameter group 'display)))
5641       (setq gnus-newsgroup-display
5642             (cond
5643              ((not (zerop (or (car-safe read-all) 0)))
5644               ;; The user entered the group with C-u SPC/RET, let's show
5645               ;; all articles.
5646               'gnus-not-ignore)
5647              ((eq display 'all)
5648               'gnus-not-ignore)
5649              ((arrayp display)
5650               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5651              ((numberp display)
5652               ;; The following is probably the "correct" solution, but
5653               ;; it makes Gnus fetch all headers and then limit the
5654               ;; articles (which is slow), so instead we hack the
5655               ;; select-articles parameter instead. -- Simon Josefsson
5656               ;; <jas@kth.se>
5657               ;;
5658               ;; (gnus-byte-compile
5659               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5660               ;;                         display)))))
5661               (setq select-articles
5662                     (gnus-uncompress-range
5663                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5664                              (if (> tmp 0)
5665                                  tmp
5666                                1))
5667                            (cdr (gnus-active group)))))
5668               nil)
5669              (t
5670               nil))))
5671
5672     (gnus-summary-setup-default-charset)
5673
5674     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5675     (when (gnus-virtual-group-p group)
5676       (setq cached gnus-newsgroup-cached))
5677
5678     (setq gnus-newsgroup-unreads
5679           (gnus-sorted-ndifference
5680            (gnus-sorted-ndifference gnus-newsgroup-unreads
5681                                     gnus-newsgroup-marked)
5682            gnus-newsgroup-dormant))
5683
5684     (setq gnus-newsgroup-processable nil)
5685
5686     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5687
5688     ;; Adjust and set lists of article marks.
5689     (when info
5690       (gnus-adjust-marked-articles info))
5691     (if (setq articles select-articles)
5692         (setq gnus-newsgroup-unselected
5693               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5694       (setq articles (gnus-articles-to-read group read-all)))
5695
5696     (cond
5697      ((null articles)
5698       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5699       'quit)
5700      ((eq articles 0) nil)
5701      (t
5702       ;; Init the dependencies hash table.
5703       (setq gnus-newsgroup-dependencies
5704             (gnus-make-hashtable (length articles)))
5705       (if (gnus-buffer-live-p gnus-group-buffer)
5706           (gnus-set-global-variables)
5707         (set-default 'gnus-newsgroup-name gnus-newsgroup-name))
5708       ;; Retrieve the headers and read them in.
5709
5710       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5711
5712       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5713       (when cached
5714         (setq gnus-newsgroup-cached cached))
5715
5716       ;; Suppress duplicates?
5717       (when gnus-suppress-duplicates
5718         (gnus-dup-suppress-articles))
5719
5720       ;; Set the initial limit.
5721       (setq gnus-newsgroup-limit (copy-sequence articles))
5722       ;; Remove canceled articles from the list of unread articles.
5723       (setq fetched-articles
5724             (mapcar (lambda (headers) (mail-header-number headers))
5725                     gnus-newsgroup-headers))
5726       (setq gnus-newsgroup-articles fetched-articles)
5727       (setq gnus-newsgroup-unreads
5728             (gnus-sorted-nintersection
5729              gnus-newsgroup-unreads fetched-articles))
5730       (gnus-compute-unseen-list)
5731
5732       ;; Removed marked articles that do not exist.
5733       (gnus-update-missing-marks
5734        (gnus-sorted-difference articles fetched-articles))
5735       ;; We might want to build some more threads first.
5736       (when (and gnus-fetch-old-headers
5737                  (eq gnus-headers-retrieved-by 'nov))
5738         (if (eq gnus-fetch-old-headers 'invisible)
5739             (gnus-build-all-threads)
5740           (gnus-build-old-threads)))
5741       ;; Let the Gnus agent mark articles as read.
5742       (when gnus-agent
5743         (gnus-agent-get-undownloaded-list))
5744       ;; Remove list identifiers from subject
5745       (gnus-summary-remove-list-identifiers)
5746       ;; Check whether auto-expire is to be done in this group.
5747       (setq gnus-newsgroup-auto-expire
5748             (and (gnus-group-auto-expirable-p group)
5749                  (not (gnus-group-read-only-p group))))
5750       ;; Set up the article buffer now, if necessary.
5751       (unless (and gnus-single-article-buffer
5752                    (equal gnus-article-buffer "*Article*"))
5753         (gnus-article-setup-buffer))
5754       ;; First and last article in this newsgroup.
5755       (when gnus-newsgroup-headers
5756         (setq gnus-newsgroup-begin
5757               (mail-header-number (car gnus-newsgroup-headers))
5758               gnus-newsgroup-end
5759               (mail-header-number
5760                (gnus-last-element gnus-newsgroup-headers))))
5761       ;; GROUP is successfully selected.
5762       (or gnus-newsgroup-headers t)))))
5763
5764 (defun gnus-compute-unseen-list ()
5765   ;; The `seen' marks are treated specially.
5766   (if (not gnus-newsgroup-seen)
5767       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5768     (setq gnus-newsgroup-unseen
5769           (gnus-inverse-list-range-intersection
5770            gnus-newsgroup-articles gnus-newsgroup-seen))))
5771
5772 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5773
5774 (defun gnus-summary-display-make-predicate (display)
5775   (require 'gnus-agent)
5776   (when (= (length display) 1)
5777     (setq display (car display)))
5778   (unless gnus-summary-display-cache
5779     (dolist (elem (append '((unread . unread)
5780                             (read . read)
5781                             (unseen . unseen))
5782                           gnus-article-mark-lists))
5783       (push (cons (cdr elem)
5784                   (gnus-byte-compile    ;Why bother?
5785                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5786             gnus-summary-display-cache)))
5787   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5788         (gnus-category-predicate-cache gnus-summary-display-cache))
5789     (gnus-get-predicate display)))
5790
5791 ;; Uses the dynamically bound `gnus-number' variable.
5792 (defvar gnus-number)
5793 (defun gnus-article-marked-p (type &optional article)
5794   (let ((article (or article gnus-number)))
5795     (cond
5796      ((eq type 'tick)
5797       (memq article gnus-newsgroup-marked))
5798      ((eq type 'spam)
5799       (memq article gnus-newsgroup-spam-marked))
5800      ((eq type 'unsend)
5801       (memq article gnus-newsgroup-unsendable))
5802      ((eq type 'undownload)
5803       (memq article gnus-newsgroup-undownloaded))
5804      ((eq type 'download)
5805       (memq article gnus-newsgroup-downloadable))
5806      ((eq type 'unread)
5807       (memq article gnus-newsgroup-unreads))
5808      ((eq type 'read)
5809       (memq article gnus-newsgroup-reads))
5810      ((eq type 'dormant)
5811       (memq article gnus-newsgroup-dormant) )
5812      ((eq type 'expire)
5813       (memq article gnus-newsgroup-expirable))
5814      ((eq type 'reply)
5815       (memq article gnus-newsgroup-replied))
5816      ((eq type 'killed)
5817       (memq article gnus-newsgroup-killed))
5818      ((eq type 'bookmark)
5819       (assq article gnus-newsgroup-bookmarks))
5820      ((eq type 'score)
5821       (assq article gnus-newsgroup-scored))
5822      ((eq type 'save)
5823       (memq article gnus-newsgroup-saved))
5824      ((eq type 'cache)
5825       (memq article gnus-newsgroup-cached))
5826      ((eq type 'forward)
5827       (memq article gnus-newsgroup-forwarded))
5828      ((eq type 'seen)
5829       (not (memq article gnus-newsgroup-unseen)))
5830      (t t))))
5831
5832 (defun gnus-articles-to-read (group &optional read-all)
5833   "Find out what articles the user wants to read."
5834   (let* ((only-read-p t)
5835          (articles
5836           (gnus-list-range-difference
5837           ;; Select all articles if `read-all' is non-nil, or if there
5838           ;; are no unread articles.
5839           (if (or read-all
5840                   (and (zerop (length gnus-newsgroup-marked))
5841                        (zerop (length gnus-newsgroup-unreads)))
5842                   ;; Fetch all if the predicate is non-nil.
5843                   gnus-newsgroup-display)
5844               ;; We want to select the headers for all the articles in
5845               ;; the group, so we select either all the active
5846               ;; articles in the group, or (if that's nil), the
5847               ;; articles in the cache.
5848               (or
5849                (if gnus-newsgroup-maximum-articles
5850                    (let ((active (gnus-active group)))
5851                      (gnus-uncompress-range
5852                       (cons (max (car active)
5853                                  (- (cdr active)
5854                                     gnus-newsgroup-maximum-articles
5855                                     -1))
5856                             (cdr active))))
5857                  (gnus-uncompress-range (gnus-active group)))
5858                (gnus-cache-articles-in-group group))
5859             ;; Select only the "normal" subset of articles.
5860             (setq only-read-p nil)
5861             (gnus-sorted-nunion
5862              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5863              gnus-newsgroup-unreads))
5864           (cdr (assq 'unexist (gnus-info-marks (gnus-get-info group))))))
5865          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5866          (scored (length scored-list))
5867          (number (length articles))
5868          (marked (+ (length gnus-newsgroup-marked)
5869                     (length gnus-newsgroup-dormant)))
5870          (select
5871           (cond
5872            ((numberp read-all)
5873             read-all)
5874            ((numberp gnus-newsgroup-display)
5875             gnus-newsgroup-display)
5876            (t
5877             (condition-case ()
5878                 (cond
5879                  ((and (or (<= scored marked) (= scored number))
5880                        (numberp gnus-large-newsgroup)
5881                        (> number gnus-large-newsgroup))
5882                   (let* ((cursor-in-echo-area nil)
5883                          (initial (gnus-parameter-large-newsgroup-initial
5884                                    gnus-newsgroup-name))
5885                          (default (if only-read-p
5886                                       (or initial gnus-large-newsgroup)
5887                                     number))
5888                          (input
5889                           (read-string
5890                            (if only-read-p
5891                                (format
5892                                 "How many articles from %s (available %d, default %d): "
5893                                 (gnus-group-decoded-name
5894                                  (gnus-group-real-name gnus-newsgroup-name))
5895                                 number default)
5896                              (format
5897                               "How many articles from %s (%d default): "
5898                               (gnus-group-decoded-name
5899                                (gnus-group-real-name gnus-newsgroup-name))
5900                               default))
5901                            nil
5902                            nil
5903                            (number-to-string default))))
5904                     (if (string-match "^[ \t]*$" input) number input)))
5905                  ((and (> scored marked) (< scored number)
5906                        (> (- scored number) 20))
5907                   (let ((input
5908                          (read-string
5909                           (format "%s %s (%d scored, %d total): "
5910                                   "How many articles from"
5911                                   (gnus-group-decoded-name
5912                                    (gnus-group-real-name gnus-newsgroup-name))
5913                                   scored number))))
5914                     (if (string-match "^[ \t]*$" input)
5915                         number input)))
5916                  (t number))
5917               (quit
5918                (message "Quit getting the articles to read")
5919                nil))))))
5920     (setq select (if (stringp select) (string-to-number select) select))
5921     (if (or (null select) (zerop select))
5922         select
5923       (if (and (not (zerop scored)) (<= (abs select) scored))
5924           (progn
5925             (setq articles (sort scored-list '<))
5926             (setq number (length articles)))
5927         (setq articles (copy-sequence articles)))
5928
5929       (when (< (abs select) number)
5930         (if (< select 0)
5931             ;; Select the N oldest articles.
5932             (setcdr (nthcdr (1- (abs select)) articles) nil)
5933           ;; Select the N most recent articles.
5934           (setq articles (nthcdr (- number select) articles))))
5935       (setq gnus-newsgroup-unselected
5936             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5937       (when gnus-alter-articles-to-read-function
5938         (setq articles
5939               (sort
5940                (funcall gnus-alter-articles-to-read-function
5941                         gnus-newsgroup-name articles)
5942                '<)))
5943       articles)))
5944
5945 (defun gnus-killed-articles (killed articles)
5946   (let (out)
5947     (while articles
5948       (when (inline (gnus-member-of-range (car articles) killed))
5949         (push (car articles) out))
5950       (setq articles (cdr articles)))
5951     out))
5952
5953 (defun gnus-article-mark-to-type (mark)
5954   "Return the type of MARK."
5955   (or (cadr (assq mark gnus-article-special-mark-lists))
5956       'list))
5957
5958 (defun gnus-article-unpropagatable-p (mark)
5959   "Return whether MARK should be propagated to back end."
5960   (memq mark gnus-article-unpropagated-mark-lists))
5961
5962 (defun gnus-adjust-marked-articles (info)
5963   "Set all article lists and remove all marks that are no longer valid."
5964   (let* ((marked-lists (gnus-info-marks info))
5965          (active (gnus-active (gnus-info-group info)))
5966          (min (car active))
5967          (max (cdr active))
5968          (types gnus-article-mark-lists)
5969          marks var articles article mark mark-type
5970          bgn end)
5971     ;; Hack to avoid adjusting marks for imap.
5972     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5973               'nnimap)
5974       (setq min 1))
5975
5976     (dolist (marks marked-lists)
5977       (setq mark (car marks)
5978             mark-type (gnus-article-mark-to-type mark)
5979             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5980       ;; We set the variable according to the type of the marks list,
5981       ;; and then adjust the marks to a subset of the active articles.
5982       (cond
5983        ;; Adjust "simple" lists - compressed yet unsorted
5984        ((eq mark-type 'list)
5985         ;; Simultaneously uncompress and clip to active range
5986         ;; See gnus-uncompress-range for a description of possible marks
5987         (let (l lh)
5988           (if (not (cadr marks))
5989               (set var nil)
5990             (setq articles (if (numberp (cddr marks))
5991                                (list (cdr marks))
5992                              (cdr marks))
5993                   lh (cons nil nil)
5994                   l lh)
5995
5996             (while (setq article (pop articles))
5997               (cond ((consp article)
5998                      (setq bgn (max (car article) min)
5999                            end (min (cdr article) max))
6000                      (while (<= bgn end)
6001                        (setq l (setcdr l (cons bgn nil))
6002                              bgn (1+ bgn))))
6003                     ((and (<= min article)
6004                           (>= max article))
6005                      (setq l (setcdr l (cons article nil))))))
6006             (set var (cdr lh)))))
6007        ;; Adjust assocs.
6008        ((eq mark-type 'tuple)
6009         (set var (setq articles (cdr marks)))
6010         (when (not (listp (cdr (symbol-value var))))
6011           (set var (list (symbol-value var))))
6012         (when (not (listp (cdr articles)))
6013           (setq articles (list articles)))
6014         (while articles
6015           (when (or (not (consp (setq article (pop articles))))
6016                     (< (car article) min)
6017                     (> (car article) max))
6018             (set var (delq article (symbol-value var))))))
6019        ;; Adjust ranges (sloppily).
6020        ((eq mark-type 'range)
6021         (cond
6022          ((eq mark 'seen)
6023           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
6024           ;; It should be (seen (NUM1 . NUM2)).
6025           (when (numberp (cddr marks))
6026             (setcdr marks (list (cdr marks))))
6027           (setq articles (cdr marks))
6028           (while (and articles
6029                       (or (and (consp (car articles))
6030                                (> min (cdar articles)))
6031                           (and (numberp (car articles))
6032                                (> min (car articles)))))
6033             (pop articles))
6034           (set var articles))
6035          ((eq mark 'unexist)
6036           (set var (cdr marks)))))))))
6037
6038 (defun gnus-update-missing-marks (missing)
6039   "Go through the list of MISSING articles and remove them from the mark lists."
6040   (when missing
6041     (let (var m)
6042       ;; Go through all types.
6043       (dolist (elem gnus-article-mark-lists)
6044         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6045           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6046           (when (symbol-value var)
6047             ;; This list has articles.  So we delete all missing
6048             ;; articles from it.
6049             (setq m missing)
6050             (while m
6051               (set var (delq (pop m) (symbol-value var))))))))))
6052
6053 (defun gnus-update-marks ()
6054   "Enter the various lists of marked articles into the newsgroup info list."
6055   (let ((types gnus-article-mark-lists)
6056         (info (gnus-get-info gnus-newsgroup-name))
6057         type list newmarked symbol delta-marks)
6058     (when info
6059       ;; Add all marks lists to the list of marks lists.
6060       (while (setq type (pop types))
6061         (setq list (symbol-value
6062                     (setq symbol
6063                           (intern (format "gnus-newsgroup-%s" (car type))))))
6064
6065         (when list
6066           ;; Get rid of the entries of the articles that have the
6067           ;; default score.
6068           (when (and (eq (cdr type) 'score)
6069                      gnus-save-score
6070                      list)
6071             (let* ((arts list)
6072                    (prev (cons nil list))
6073                    (all prev))
6074               (while arts
6075                 (if (or (not (consp (car arts)))
6076                         (= (cdar arts) gnus-summary-default-score))
6077                     (setcdr prev (cdr arts))
6078                   (setq prev arts))
6079                 (setq arts (cdr arts)))
6080               (setq list (cdr all)))))
6081
6082         (when (eq (cdr type) 'seen)
6083           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6084
6085         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6086           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6087
6088         (when (and (gnus-check-backend-function
6089                     'request-set-mark gnus-newsgroup-name)
6090                    (not (gnus-article-unpropagatable-p (cdr type))))
6091           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6092                  ;; Don't do anything about marks for articles we
6093                  ;; didn't actually get any headers for.
6094                  (del
6095                   (gnus-list-range-intersection
6096                    gnus-newsgroup-articles
6097                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6098                  (add
6099                   (gnus-list-range-intersection
6100                    gnus-newsgroup-articles
6101                    (gnus-remove-from-range
6102                     (gnus-copy-sequence list) old))))
6103             (when add
6104               (push (list add 'add (list (cdr type))) delta-marks))
6105             (when del
6106               ;; Don't delete marks from outside the active range.
6107               ;; This shouldn't happen, but is a sanity check.
6108               (setq del (gnus-sorted-range-intersection
6109                          (gnus-active gnus-newsgroup-name) del))
6110               (push (list del 'del (list (cdr type))) delta-marks))))
6111
6112         (when (or list
6113                   (eq (cdr type) 'unexist))
6114           (push (cons (cdr type) list) newmarked)))
6115
6116       (when delta-marks
6117         (unless (gnus-check-group gnus-newsgroup-name)
6118           (error "Can't open server for %s" gnus-newsgroup-name))
6119         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6120
6121       ;; Enter these new marks into the info of the group.
6122       (if (nthcdr 3 info)
6123           (setcar (nthcdr 3 info) newmarked)
6124         ;; Add the marks lists to the end of the info.
6125         (when newmarked
6126           (setcdr (nthcdr 2 info) (list newmarked))))
6127
6128       ;; Cut off the end of the info if there's nothing else there.
6129       (let ((i 5))
6130         (while (and (> i 2)
6131                     (not (nth i info)))
6132           (when (nthcdr (decf i) info)
6133             (setcdr (nthcdr i info) nil)))))))
6134
6135 (defun gnus-set-mode-line (where)
6136   "Set the mode line of the article or summary buffers.
6137 If WHERE is `summary', the summary mode line format will be used."
6138   ;; Is this mode line one we keep updated?
6139   (when (and (memq where gnus-updated-mode-lines)
6140              (symbol-value
6141               (intern (format "gnus-%s-mode-line-format-spec" where))))
6142     (let (mode-string)
6143       ;; We evaluate this in the summary buffer since these
6144       ;; variables are buffer-local to that buffer.
6145       (with-current-buffer gnus-summary-buffer
6146         ;; We bind all these variables that are used in the `eval' form
6147         ;; below.
6148         (let* ((mformat (symbol-value
6149                          (intern
6150                           (format "gnus-%s-mode-line-format-spec" where))))
6151                (gnus-tmp-group-name (gnus-mode-string-quote
6152                                      (gnus-group-decoded-name
6153                                       gnus-newsgroup-name)))
6154                (gnus-tmp-article-number (or gnus-current-article 0))
6155                (gnus-tmp-unread gnus-newsgroup-unreads)
6156                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6157                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6158                (gnus-tmp-unread-and-unselected
6159                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6160                             (zerop gnus-tmp-unselected))
6161                        "")
6162                       ((zerop gnus-tmp-unselected)
6163                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6164                       (t (format "{%d(+%d) more}"
6165                                  gnus-tmp-unread-and-unticked
6166                                  gnus-tmp-unselected))))
6167                (gnus-tmp-subject
6168                 (if (and gnus-current-headers
6169                          (vectorp gnus-current-headers))
6170                     (gnus-mode-string-quote
6171                      (mail-header-subject gnus-current-headers))
6172                   ""))
6173                bufname-length max-len
6174                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6175           (setq mode-string (eval mformat))
6176           (setq bufname-length (if (string-match "%b" mode-string)
6177                                    (- (length
6178                                        (buffer-name
6179                                         (if (eq where 'summary)
6180                                             nil
6181                                           (get-buffer gnus-article-buffer))))
6182                                       2)
6183                                  0))
6184           (setq max-len (max 4 (if gnus-mode-non-string-length
6185                                    (- (window-width)
6186                                       gnus-mode-non-string-length
6187                                       bufname-length)
6188                                  (length mode-string))))
6189           ;; We might have to chop a bit of the string off...
6190           (when (> (length mode-string) max-len)
6191             (setq mode-string
6192                   (concat (truncate-string-to-width mode-string (- max-len 3))
6193                           "...")))))
6194       ;; Update the mode line.
6195       (setq mode-line-buffer-identification
6196             (gnus-mode-line-buffer-identification (list mode-string)))
6197       (set-buffer-modified-p t))))
6198
6199 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6200   "Go through the HEADERS list and add all Xrefs to a hash table.
6201 The resulting hash table is returned, or nil if no Xrefs were found."
6202   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6203          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6204          (xref-hashtb (gnus-make-hashtable))
6205          start group entry number xrefs header)
6206     (while headers
6207       (setq header (pop headers))
6208       (when (and (setq xrefs (mail-header-xref header))
6209                  (not (memq (setq number (mail-header-number header))
6210                             unreads)))
6211         (setq start 0)
6212         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6213           (setq start (match-end 0))
6214           (setq group (if prefix
6215                           (concat prefix (substring xrefs (match-beginning 1)
6216                                                     (match-end 1)))
6217                         (substring xrefs (match-beginning 1) (match-end 1))))
6218           (setq number
6219                 (string-to-number (substring xrefs (match-beginning 2)
6220                                           (match-end 2))))
6221           (if (setq entry (gnus-gethash group xref-hashtb))
6222               (setcdr entry (cons number (cdr entry)))
6223             (gnus-sethash group (cons number nil) xref-hashtb)))))
6224     (and start xref-hashtb)))
6225
6226 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6227   "Look through all the headers and mark the Xrefs as read."
6228   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6229         name info xref-hashtb idlist method nth4)
6230     (with-current-buffer gnus-group-buffer
6231       (when (setq xref-hashtb
6232                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6233         (mapatoms
6234          (lambda (group)
6235            (unless (string= from-newsgroup (setq name (symbol-name group)))
6236              (setq idlist (symbol-value group))
6237              ;; Dead groups are not updated.
6238              (and (prog1
6239                       (setq info (gnus-get-info name))
6240                     (when (stringp (setq nth4 (gnus-info-method info)))
6241                       (setq nth4 (gnus-server-to-method nth4))))
6242                   ;; Only do the xrefs if the group has the same
6243                   ;; select method as the group we have just read.
6244                   (or (gnus-methods-equal-p
6245                        nth4 (gnus-find-method-for-group from-newsgroup))
6246                       virtual
6247                       (equal nth4 (setq method (gnus-find-method-for-group
6248                                                 from-newsgroup)))
6249                       (and (equal (car nth4) (car method))
6250                            (equal (nth 1 nth4) (nth 1 method))))
6251                   gnus-use-cross-reference
6252                   (or (not (eq gnus-use-cross-reference t))
6253                       virtual
6254                       ;; Only do cross-references on subscribed
6255                       ;; groups, if that is what is wanted.
6256                       (<= (gnus-info-level info) gnus-level-subscribed))
6257                   (gnus-group-make-articles-read name idlist))))
6258          xref-hashtb)))))
6259
6260 (defun gnus-compute-read-articles (group articles)
6261   (let* ((entry (gnus-group-entry group))
6262          (info (nth 2 entry))
6263          (active (gnus-active group))
6264          ninfo)
6265     (when entry
6266       ;; First peel off all invalid article numbers.
6267       (when active
6268         (let ((ids articles)
6269               id first)
6270           (while (setq id (pop ids))
6271             (when (and first (> id (cdr active)))
6272               ;; We'll end up in this situation in one particular
6273               ;; obscure situation.  If you re-scan a group and get
6274               ;; a new article that is cross-posted to a different
6275               ;; group that has not been re-scanned, you might get
6276               ;; crossposted article that has a higher number than
6277               ;; Gnus believes possible.  So we re-activate this
6278               ;; group as well.  This might mean doing the
6279               ;; crossposting thingy will *increase* the number
6280               ;; of articles in some groups.  Tsk, tsk.
6281               (setq active (or (gnus-activate-group group) active)))
6282             (when (or (> id (cdr active))
6283                       (< id (car active)))
6284               (setq articles (delq id articles))))))
6285       ;; If the read list is nil, we init it.
6286       (if (and active
6287                (null (gnus-info-read info))
6288                (> (car active) 1))
6289           (setq ninfo (cons 1 (1- (car active))))
6290         (setq ninfo (gnus-info-read info)))
6291       ;; Then we add the read articles to the range.
6292       (gnus-add-to-range
6293        ninfo (setq articles (sort articles '<))))))
6294
6295 (defun gnus-group-make-articles-read (group articles)
6296   "Update the info of GROUP to say that ARTICLES are read."
6297   (let* ((num 0)
6298          (entry (gnus-group-entry group))
6299          (info (nth 2 entry))
6300          (active (gnus-active group))
6301          (set-marks
6302           (gnus-method-option-p
6303            (gnus-find-method-for-group group)
6304            'server-marks))
6305          range)
6306     (if (not entry)
6307         ;; Group that Gnus doesn't know exists, but still allow the
6308         ;; backend to set marks.
6309         (when set-marks
6310           (gnus-request-set-mark
6311            group (list (list (gnus-compress-sequence (sort articles #'<))
6312                              'add '(read)))))
6313       ;; Normal, subscribed groups.
6314       (setq range (gnus-compute-read-articles group articles))
6315       (with-current-buffer gnus-group-buffer
6316         (gnus-undo-register
6317           `(progn
6318              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6319              (gnus-info-set-read ',info ',(gnus-info-read info))
6320              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6321              (when ,set-marks
6322                (gnus-request-set-mark
6323                 ,group (list (list ',range 'del '(read)))))
6324              (gnus-group-update-group ,group t))))
6325       ;; Add the read articles to the range.
6326       (gnus-info-set-read info range)
6327       (when set-marks
6328         (gnus-request-set-mark group (list (list range 'add '(read)))))
6329       ;; Then we have to re-compute how many unread
6330       ;; articles there are in this group.
6331       (when active
6332         (cond
6333          ((not range)
6334           (setq num (- (1+ (cdr active)) (car active))))
6335          ((not (listp (cdr range)))
6336           (setq num (- (cdr active) (- (1+ (cdr range))
6337                                        (car range)))))
6338          (t
6339           (while range
6340             (if (numberp (car range))
6341                 (setq num (1+ num))
6342               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6343             (setq range (cdr range)))
6344           (setq num (- (cdr active) num))))
6345         ;; Update the number of unread articles.
6346         (setcar entry num)
6347         ;; Update the group buffer.
6348         (unless (gnus-ephemeral-group-p group)
6349           (gnus-group-update-group group t))))))
6350
6351 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6352   (let ((cur nntp-server-buffer)
6353         (dependencies
6354          (or dependencies
6355              (with-current-buffer gnus-summary-buffer
6356                gnus-newsgroup-dependencies)))
6357         headers id end ref number
6358         (mail-parse-charset gnus-newsgroup-charset)
6359         (mail-parse-ignored-charsets
6360          (save-current-buffer (condition-case nil
6361                                   (set-buffer gnus-summary-buffer)
6362                                 (error))
6363                               gnus-newsgroup-ignored-charsets)))
6364     (with-current-buffer nntp-server-buffer
6365       ;; Translate all TAB characters into SPACE characters.
6366       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6367       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6368       (ietf-drums-unfold-fws)
6369       (gnus-run-hooks 'gnus-parse-headers-hook)
6370       (let ((case-fold-search t)
6371             in-reply-to header p lines chars)
6372         (goto-char (point-min))
6373         ;; Search to the beginning of the next header.  Error messages
6374         ;; do not begin with 2 or 3.
6375         (while (re-search-forward "^[23][0-9]+ " nil t)
6376           (setq id nil
6377                 ref nil)
6378           ;; This implementation of this function, with nine
6379           ;; search-forwards instead of the one re-search-forward and
6380           ;; a case (which basically was the old function) is actually
6381           ;; about twice as fast, even though it looks messier.  You
6382           ;; can't have everything, I guess.  Speed and elegance
6383           ;; doesn't always go hand in hand.
6384           (setq
6385            header
6386            (vector
6387             ;; Number.
6388             (prog1
6389                 (setq number (read cur))
6390               (end-of-line)
6391               (setq p (point))
6392               (narrow-to-region (point)
6393                                 (or (and (search-forward "\n.\n" nil t)
6394                                          (- (point) 2))
6395                                     (point))))
6396             ;; Subject.
6397             (progn
6398               (goto-char p)
6399               (if (search-forward "\nsubject:" nil t)
6400                   (funcall gnus-decode-encoded-word-function
6401                            (nnheader-header-value))
6402                 "(none)"))
6403             ;; From.
6404             (progn
6405               (goto-char p)
6406               (if (search-forward "\nfrom:" nil t)
6407                   (funcall gnus-decode-encoded-address-function
6408                            (nnheader-header-value))
6409                 "(nobody)"))
6410             ;; Date.
6411             (progn
6412               (goto-char p)
6413               (if (search-forward "\ndate:" nil t)
6414                   (nnheader-header-value) ""))
6415             ;; Message-ID.
6416             (progn
6417               (goto-char p)
6418               (setq id (if (re-search-forward
6419                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6420                            ;; We do it this way to make sure the Message-ID
6421                            ;; is (somewhat) syntactically valid.
6422                            (buffer-substring (match-beginning 1)
6423                                              (match-end 1))
6424                          ;; If there was no message-id, we just fake one
6425                          ;; to make subsequent routines simpler.
6426                          (nnheader-generate-fake-message-id number))))
6427             ;; References.
6428             (progn
6429               (goto-char p)
6430               (if (search-forward "\nreferences:" nil t)
6431                   (progn
6432                     (setq end (point))
6433                     (prog1
6434                         (nnheader-header-value)
6435                       (setq ref
6436                             (buffer-substring
6437                              (progn
6438                                (end-of-line)
6439                                (search-backward ">" end t)
6440                                (1+ (point)))
6441                              (progn
6442                                (search-backward "<" end t)
6443                                (point))))))
6444                 ;; Get the references from the in-reply-to header if there
6445                 ;; were no references and the in-reply-to header looks
6446                 ;; promising.
6447                 (if (and (search-forward "\nin-reply-to:" nil t)
6448                          (setq in-reply-to (nnheader-header-value))
6449                          (string-match "<[^>]+>" in-reply-to))
6450                     (let (ref2)
6451                       (setq ref (substring in-reply-to (match-beginning 0)
6452                                            (match-end 0)))
6453                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6454                         (setq ref2 (substring in-reply-to (match-beginning 0)
6455                                               (match-end 0)))
6456                         (when (> (length ref2) (length ref))
6457                           (setq ref ref2)))
6458                       ref)
6459                   (setq ref nil))))
6460             ;; Chars.
6461             (progn
6462               (goto-char p)
6463               (if (search-forward "\nchars: " nil t)
6464                   (if (numberp (setq chars (ignore-errors (read cur))))
6465                       chars -1)
6466                 -1))
6467             ;; Lines.
6468             (progn
6469               (goto-char p)
6470               (if (search-forward "\nlines: " nil t)
6471                   (if (numberp (setq lines (ignore-errors (read cur))))
6472                       lines -1)
6473                 -1))
6474             ;; Xref.
6475             (progn
6476               (goto-char p)
6477               (and (search-forward "\nxref:" nil t)
6478                    (nnheader-header-value)))
6479             ;; Extra.
6480             (when gnus-extra-headers
6481               (let ((extra gnus-extra-headers)
6482                     out)
6483                 (while extra
6484                   (goto-char p)
6485                   (when (search-forward
6486                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6487                     (push (cons (car extra) (nnheader-header-value))
6488                           out))
6489                   (pop extra))
6490                 out))))
6491           (when (equal id ref)
6492             (setq ref nil))
6493
6494           (when gnus-alter-header-function
6495             (funcall gnus-alter-header-function header)
6496             (setq id (mail-header-id header)
6497                   ref (gnus-parent-id (mail-header-references header))))
6498
6499           (when (setq header
6500                       (gnus-dependencies-add-header
6501                        header dependencies force-new))
6502             (push header headers))
6503           (goto-char (point-max))
6504           (widen))
6505         (nreverse headers)))))
6506
6507 ;; Goes through the xover lines and returns a list of vectors
6508 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6509                                                   force-new dependencies
6510                                                   group also-fetch-heads)
6511   "Parse the news overview data in the server buffer.
6512 Return a list of headers that match SEQUENCE (see
6513 `nntp-retrieve-headers')."
6514   ;; Get the Xref when the users reads the articles since most/some
6515   ;; NNTP servers do not include Xrefs when using XOVER.
6516   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6517   (let ((mail-parse-charset gnus-newsgroup-charset)
6518         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6519         (cur nntp-server-buffer)
6520         (dependencies (or dependencies gnus-newsgroup-dependencies))
6521         (allp (cond
6522                ((eq gnus-read-all-available-headers t)
6523                 t)
6524                ((and (stringp gnus-read-all-available-headers)
6525                      group)
6526                 (string-match gnus-read-all-available-headers group))
6527                (t
6528                 nil)))
6529         number headers header)
6530     (with-current-buffer nntp-server-buffer
6531       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6532       ;; Allow the user to mangle the headers before parsing them.
6533       (gnus-run-hooks 'gnus-parse-headers-hook)
6534       (goto-char (point-min))
6535       (gnus-parse-without-error
6536         (while (and (or sequence allp)
6537                     (not (eobp)))
6538           (setq number (read cur))
6539           (when (not allp)
6540             (while (and sequence
6541                         (< (car sequence) number))
6542               (setq sequence (cdr sequence))))
6543           (when (and (or allp
6544                          (and sequence
6545                               (eq number (car sequence))))
6546                      (progn
6547                        (setq sequence (cdr sequence))
6548                        (setq header (inline
6549                                       (gnus-nov-parse-line
6550                                        number dependencies force-new)))))
6551             (push header headers))
6552           (forward-line 1)))
6553       ;; A common bug in inn is that if you have posted an article and
6554       ;; then retrieves the active file, it will answer correctly --
6555       ;; the new article is included.  However, a NOV entry for the
6556       ;; article may not have been generated yet, so this may fail.
6557       ;; We work around this problem by retrieving the last few
6558       ;; headers using HEAD.
6559       (if (or (not also-fetch-heads)
6560               (not sequence))
6561           ;; We (probably) got all the headers.
6562           (nreverse headers)
6563         (let ((gnus-nov-is-evil t))
6564           (nconc
6565            (nreverse headers)
6566            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6567              (gnus-get-newsgroup-headers))))))))
6568
6569 (defun gnus-article-get-xrefs ()
6570   "Fill in the Xref value in `gnus-current-headers', if necessary.
6571 This is meant to be called in `gnus-article-internal-prepare-hook'."
6572   (let ((headers (with-current-buffer gnus-summary-buffer
6573                    gnus-current-headers)))
6574     (or (not gnus-use-cross-reference)
6575         (not headers)
6576         (and (mail-header-xref headers)
6577              (not (string= (mail-header-xref headers) "")))
6578         (let ((case-fold-search t)
6579               xref)
6580           (save-restriction
6581             (nnheader-narrow-to-headers)
6582             (goto-char (point-min))
6583             (when (or (and (not (eobp))
6584                            (eq (downcase (char-after)) ?x)
6585                            (looking-at "Xref:"))
6586                       (search-forward "\nXref:" nil t))
6587               (goto-char (1+ (match-end 0)))
6588               (setq xref (buffer-substring (point) (point-at-eol)))
6589               (mail-header-set-xref headers xref)))))))
6590
6591 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6592   "Find article ID and insert the summary line for that article.
6593 OLD-HEADER can either be a header or a line number to insert
6594 the subject line on.
6595 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6596 and OLD-HEADER will be used when the summary line is inserted,
6597 too, instead of trying to fetch new headers."
6598   (let* ((line (and (numberp old-header) old-header))
6599          (old-header (and (vectorp old-header) old-header))
6600          (header (cond ((and old-header use-old-header)
6601                         old-header)
6602                        ((and (numberp id)
6603                              (gnus-number-to-header id))
6604                         (gnus-number-to-header id))
6605                        (t
6606                         (gnus-read-header id))))
6607          (number (and (numberp id) id))
6608          d)
6609     (when header
6610       ;; Rebuild the thread that this article is part of and go to the
6611       ;; article we have fetched.
6612       (when (and (not gnus-show-threads)
6613                  old-header)
6614         (when (and number
6615                    (setq d (gnus-data-find (mail-header-number old-header))))
6616           (goto-char (gnus-data-pos d))
6617           (gnus-data-remove
6618            number
6619            (- (point-at-bol)
6620               (prog1
6621                   (1+ (point-at-eol))
6622                 (gnus-delete-line))))))
6623       ;; Remove list identifiers from subject.
6624       (let ((gnus-newsgroup-headers (list header)))
6625         (gnus-summary-remove-list-identifiers))
6626       (when old-header
6627         (mail-header-set-number header (mail-header-number old-header)))
6628       (setq gnus-newsgroup-sparse
6629             (delq (setq number (mail-header-number header))
6630                   gnus-newsgroup-sparse))
6631       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6632       (push number gnus-newsgroup-limit)
6633       (gnus-rebuild-thread (mail-header-id header) line)
6634       (gnus-summary-goto-subject number nil t))
6635     (when (and (numberp number)
6636                (> number 0))
6637       ;; We have to update the boundaries even if we can't fetch the
6638       ;; article if ID is a number -- so that the next `P' or `N'
6639       ;; command will fetch the previous (or next) article even
6640       ;; if the one we tried to fetch this time has been canceled.
6641       (unless (and gnus-newsgroup-end (< number gnus-newsgroup-end))
6642         (setq gnus-newsgroup-end number))
6643       (unless (and gnus-newsgroup-begin (> number gnus-newsgroup-begin))
6644         (setq gnus-newsgroup-begin number))
6645       (setq gnus-newsgroup-unselected
6646             (delq number gnus-newsgroup-unselected)))
6647     ;; Report back a success?
6648     (and header (mail-header-number header))))
6649
6650 ;;; Process/prefix in the summary buffer
6651
6652 (defun gnus-summary-work-articles (n)
6653   "Return a list of articles to be worked upon.
6654 The prefix argument, the list of process marked articles, and the
6655 current article will be taken into consideration."
6656   (with-current-buffer gnus-summary-buffer
6657     (cond
6658      (n
6659       ;; A numerical prefix has been given.
6660       (setq n (prefix-numeric-value n))
6661       (let ((backward (< n 0))
6662             (n (abs (prefix-numeric-value n)))
6663             articles article)
6664         (save-excursion
6665           (while
6666               (and (> n 0)
6667                    (push (setq article (gnus-summary-article-number))
6668                          articles)
6669                    (if backward
6670                        (gnus-summary-find-prev nil article)
6671                      (gnus-summary-find-next nil article)))
6672             (decf n)))
6673         (nreverse articles)))
6674      ((and (gnus-region-active-p) (mark))
6675       (message "region active")
6676       ;; Work on the region between point and mark.
6677       (let ((max (max (point) (mark)))
6678             articles article)
6679         (save-excursion
6680           (goto-char (min (point) (mark)))
6681           (while
6682               (and
6683                (push (setq article (gnus-summary-article-number)) articles)
6684                (gnus-summary-find-next nil article)
6685                (< (point) max)))
6686           (nreverse articles))))
6687      (gnus-newsgroup-processable
6688       ;; There are process-marked articles present.
6689       ;; Save current state.
6690       (gnus-summary-save-process-mark)
6691       ;; Return the list.
6692       (reverse gnus-newsgroup-processable))
6693      (t
6694       ;; Just return the current article.
6695       (list (gnus-summary-article-number))))))
6696
6697 (defmacro gnus-summary-iterate (arg &rest forms)
6698   "Iterate over the process/prefixed articles and do FORMS.
6699 ARG is the interactive prefix given to the command.  FORMS will be
6700 executed with point over the summary line of the articles."
6701   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6702     `(let ((,articles (gnus-summary-work-articles ,arg)))
6703        (while ,articles
6704          (gnus-summary-goto-subject (car ,articles))
6705          ,@forms
6706          (pop ,articles)))))
6707
6708 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6709 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6710
6711 (defun gnus-summary-save-process-mark ()
6712   "Push the current set of process marked articles on the stack."
6713   (interactive)
6714   (push (copy-sequence gnus-newsgroup-processable)
6715         gnus-newsgroup-process-stack))
6716
6717 (defun gnus-summary-kill-process-mark ()
6718   "Push the current set of process marked articles on the stack and unmark."
6719   (interactive)
6720   (gnus-summary-save-process-mark)
6721   (gnus-summary-unmark-all-processable))
6722
6723 (defun gnus-summary-yank-process-mark ()
6724   "Pop the last process mark state off the stack and restore it."
6725   (interactive)
6726   (unless gnus-newsgroup-process-stack
6727     (error "Empty mark stack"))
6728   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6729
6730 (defun gnus-summary-process-mark-set (set)
6731   "Make SET into the current process marked articles."
6732   (gnus-summary-unmark-all-processable)
6733   (mapc 'gnus-summary-set-process-mark set))
6734
6735 ;;; Searching and stuff
6736
6737 (defun gnus-summary-search-group (&optional backward use-level)
6738   "Search for next unread newsgroup.
6739 If optional argument BACKWARD is non-nil, search backward instead."
6740   (with-current-buffer gnus-group-buffer
6741     (when (gnus-group-search-forward
6742            backward nil (if use-level (gnus-group-group-level) nil))
6743       (gnus-group-group-name))))
6744
6745 (defun gnus-summary-best-group (&optional exclude-group)
6746   "Find the name of the best unread group.
6747 If EXCLUDE-GROUP, do not go to this group."
6748   (with-current-buffer gnus-group-buffer
6749     (save-excursion
6750       (gnus-group-best-unread-group exclude-group))))
6751
6752 (defun gnus-summary-find-next (&optional unread article backward)
6753   (if backward
6754       (gnus-summary-find-prev unread article)
6755     (let* ((dummy (gnus-summary-article-intangible-p))
6756            (article (or article (gnus-summary-article-number)))
6757            (data (gnus-data-find-list article))
6758            result)
6759       (when (and (not dummy)
6760                  (or (not gnus-summary-check-current)
6761                      (not unread)
6762                      (not (gnus-data-unread-p (car data)))))
6763         (setq data (cdr data)))
6764       (when (setq result
6765                   (if unread
6766                       (progn
6767                         (while data
6768                           (unless (memq (gnus-data-number (car data))
6769                                         (cond
6770                                          ((eq gnus-auto-goto-ignores
6771                                               'always-undownloaded)
6772                                           gnus-newsgroup-undownloaded)
6773                                          (gnus-plugged
6774                                           nil)
6775                                          ((eq gnus-auto-goto-ignores
6776                                               'unfetched)
6777                                           gnus-newsgroup-unfetched)
6778                                          ((eq gnus-auto-goto-ignores
6779                                               'undownloaded)
6780                                           gnus-newsgroup-undownloaded)))
6781                             (when (gnus-data-unread-p (car data))
6782                               (setq result (car data)
6783                                     data nil)))
6784                           (setq data (cdr data)))
6785                         result)
6786                     (car data)))
6787         (goto-char (gnus-data-pos result))
6788         (gnus-data-number result)))))
6789
6790 (defun gnus-summary-find-prev (&optional unread article)
6791   (let* ((eobp (eobp))
6792          (article (or article (gnus-summary-article-number)))
6793          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6794          result)
6795     (when (and (not eobp)
6796                (or (not gnus-summary-check-current)
6797                    (not unread)
6798                    (not (gnus-data-unread-p (car data)))))
6799       (setq data (cdr data)))
6800     (when (setq result
6801                 (if unread
6802                     (progn
6803                       (while data
6804                         (unless (memq (gnus-data-number (car data))
6805                                       (cond
6806                                        ((eq gnus-auto-goto-ignores
6807                                             'always-undownloaded)
6808                                         gnus-newsgroup-undownloaded)
6809                                        (gnus-plugged
6810                                         nil)
6811                                        ((eq gnus-auto-goto-ignores
6812                                             'unfetched)
6813                                         gnus-newsgroup-unfetched)
6814                                        ((eq gnus-auto-goto-ignores
6815                                             'undownloaded)
6816                                         gnus-newsgroup-undownloaded)))
6817                           (when (gnus-data-unread-p (car data))
6818                             (setq result (car data)
6819                                   data nil)))
6820                         (setq data (cdr data)))
6821                       result)
6822                   (car data)))
6823       (goto-char (gnus-data-pos result))
6824       (gnus-data-number result))))
6825
6826 (defun gnus-summary-find-subject (subject &optional unread backward article)
6827   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6828          (article (or article (gnus-summary-article-number)))
6829          (articles (gnus-data-list backward))
6830          (arts (gnus-data-find-list article articles))
6831          result)
6832     (when (or (not gnus-summary-check-current)
6833               (not unread)
6834               (not (gnus-data-unread-p (car arts))))
6835       (setq arts (cdr arts)))
6836     (while arts
6837       (and (or (not unread)
6838                (gnus-data-unread-p (car arts)))
6839            (vectorp (gnus-data-header (car arts)))
6840            (gnus-subject-equal
6841             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6842            (setq result (car arts)
6843                  arts nil))
6844       (setq arts (cdr arts)))
6845     (and result
6846          (goto-char (gnus-data-pos result))
6847          (gnus-data-number result))))
6848
6849 (defun gnus-summary-search-forward (&optional unread subject backward)
6850   "Search forward for an article.
6851 If UNREAD, look for unread articles.  If SUBJECT, look for
6852 articles with that subject.  If BACKWARD, search backward instead."
6853   (cond (subject (gnus-summary-find-subject subject unread backward))
6854         (backward (gnus-summary-find-prev unread))
6855         (t (gnus-summary-find-next unread))))
6856
6857 (defun gnus-recenter (&optional n)
6858   "Center point in window and redisplay frame.
6859 Also do horizontal recentering."
6860   (interactive "P")
6861   (when (and gnus-auto-center-summary
6862              (not (eq gnus-auto-center-summary 'vertical)))
6863     (gnus-horizontal-recenter))
6864   (if (fboundp 'recenter-top-bottom)
6865       (recenter-top-bottom n)
6866     (recenter n)))
6867
6868 (put 'gnus-recenter 'isearch-scroll t)
6869
6870 (defun gnus-forward-line-ignore-invisible (n)
6871   "Move N lines forward (backward if N is negative).
6872 Like forward-line, but skip over (and don't count) invisible lines."
6873   (let (done)
6874     (while (and (> n 0) (not done))
6875       ;; If the following character is currently invisible,
6876       ;; skip all characters with that same `invisible' property value.
6877       (while (gnus-invisible-p (point))
6878         (goto-char (gnus-next-char-property-change (point))))
6879       (forward-line 1)
6880       (if (eobp)
6881           (setq done t)
6882         (setq n (1- n))))
6883     (while (and (< n 0) (not done))
6884       (forward-line -1)
6885       (if (bobp) (setq done t)
6886         (setq n (1+ n))
6887         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6888           (goto-char (gnus-previous-char-property-change (point))))))))
6889
6890 (defun gnus-summary-recenter ()
6891   "Center point in the summary window.
6892 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6893 displayed, no centering will be performed."
6894   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6895   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6896   (interactive)
6897   ;; The user has to want it.
6898   (when gnus-auto-center-summary
6899     (let* ((top (cond ((< (window-height) 4) 0)
6900                       ((< (window-height) 7) 1)
6901                       (t (if (numberp gnus-auto-center-summary)
6902                              gnus-auto-center-summary
6903                            (/ (1- (window-height)) 2)))))
6904            (height (1- (window-height)))
6905            (bottom (save-excursion
6906                      (goto-char (point-max))
6907                      (gnus-forward-line-ignore-invisible (- height))
6908                      (point)))
6909            (window (get-buffer-window (current-buffer))))
6910       (when (get-buffer-window gnus-article-buffer)
6911         ;; Only do recentering when the article buffer is displayed,
6912         ;; Set the window start to either `bottom', which is the biggest
6913         ;; possible valid number, or the second line from the top,
6914         ;; whichever is the least.
6915         (let ((top-pos (save-excursion
6916                          (gnus-forward-line-ignore-invisible (- top))
6917                          (point))))
6918           (if (> bottom top-pos)
6919               ;; Keep the second line from the top visible
6920               (set-window-start window top-pos)
6921             ;; Try to keep the bottom line visible; if it's partially
6922             ;; obscured, either scroll one more line to make it fully
6923             ;; visible, or revert to using TOP-POS.
6924             (save-excursion
6925               (goto-char (point-max))
6926               (gnus-forward-line-ignore-invisible -1)
6927               (let ((last-line-start (point)))
6928                 (goto-char bottom)
6929                 (set-window-start window (point) t)
6930                 (when (not (pos-visible-in-window-p last-line-start window))
6931                   (gnus-forward-line-ignore-invisible 1)
6932                   (set-window-start window (min (point) top-pos) t)))))))
6933       ;; Do horizontal recentering while we're at it.
6934       (when (and (get-buffer-window (current-buffer) t)
6935                  (not (eq gnus-auto-center-summary 'vertical)))
6936         (let ((selected (selected-window)))
6937           (select-window (get-buffer-window (current-buffer) t))
6938           (gnus-summary-position-point)
6939           (gnus-horizontal-recenter)
6940           (select-window selected))))))
6941
6942 (defun gnus-summary-jump-to-group (newsgroup)
6943   "Move point to NEWSGROUP in group mode buffer."
6944   ;; Keep update point of group mode buffer if visible.
6945   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6946       (save-window-excursion
6947         ;; Take care of tree window mode.
6948         (when (get-buffer-window gnus-group-buffer)
6949           (pop-to-buffer gnus-group-buffer))
6950         (gnus-group-jump-to-group newsgroup))
6951     (save-excursion
6952       ;; Take care of tree window mode.
6953       (if (get-buffer-window gnus-group-buffer 0)
6954           (pop-to-buffer gnus-group-buffer)
6955         (set-buffer gnus-group-buffer))
6956       (gnus-group-jump-to-group newsgroup))))
6957
6958 ;; This function returns a list of article numbers based on the
6959 ;; difference between the ranges of read articles in this group and
6960 ;; the range of active articles.
6961 (defun gnus-list-of-unread-articles (group)
6962   (let* ((read (gnus-info-read (gnus-get-info group)))
6963          (active (or (gnus-active group) (gnus-activate-group group)))
6964          (last (or (cdr active)
6965                    (error "Group %s couldn't be activated " group)))
6966          (bottom (if gnus-newsgroup-maximum-articles
6967                      (max (car active)
6968                           (- last gnus-newsgroup-maximum-articles -1))
6969                    (car active)))
6970          first nlast unread)
6971     ;; If none are read, then all are unread.
6972     (if (not read)
6973         (setq first bottom)
6974       ;; If the range of read articles is a single range, then the
6975       ;; first unread article is the article after the last read
6976       ;; article.  Sounds logical, doesn't it?
6977       (if (and (not (listp (cdr read)))
6978                (or (< (car read) bottom)
6979                    (progn (setq read (list read))
6980                           nil)))
6981           (setq first (max bottom (1+ (cdr read))))
6982         ;; `read' is a list of ranges.
6983         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6984                                   (caar read)))
6985                   1)
6986           (setq first bottom))
6987         (while read
6988           (when first
6989             (while (< first nlast)
6990               (setq unread (cons first unread)
6991                     first (1+ first))))
6992           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6993           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6994           (setq read (cdr read)))))
6995     ;; And add the last unread articles.
6996     (while (<= first last)
6997       (setq unread (cons first unread)
6998             first (1+ first)))
6999     ;; Return the list of unread articles.
7000     (delq 0 (nreverse unread))))
7001
7002 (defun gnus-list-of-read-articles (group)
7003   "Return a list of unread, unticked and non-dormant articles."
7004   (let* ((info (gnus-get-info group))
7005          (marked (gnus-info-marks info))
7006          (active (gnus-active group)))
7007     (and info active
7008          (gnus-list-range-difference
7009           (gnus-list-range-difference
7010            (gnus-sorted-complement
7011             (gnus-uncompress-range
7012              (if gnus-newsgroup-maximum-articles
7013                  (cons (max (car active)
7014                             (- (cdr active)
7015                                gnus-newsgroup-maximum-articles
7016                                -1))
7017                        (cdr active))
7018                active))
7019             (gnus-list-of-unread-articles group))
7020            (cdr (assq 'dormant marked)))
7021           (cdr (assq 'tick marked))))))
7022
7023 ;; This function returns a sequence of article numbers based on the
7024 ;; difference between the ranges of read articles in this group and
7025 ;; the range of active articles.
7026 (defun gnus-sequence-of-unread-articles (group)
7027   (let* ((read (gnus-info-read (gnus-get-info group)))
7028          (active (or (gnus-active group) (gnus-activate-group group)))
7029          (last (cdr active))
7030          (bottom (if gnus-newsgroup-maximum-articles
7031                      (max (car active)
7032                           (- last gnus-newsgroup-maximum-articles -1))
7033                    (car active)))
7034          first nlast unread)
7035     ;; If none are read, then all are unread.
7036     (if (not read)
7037         (setq first bottom)
7038       ;; If the range of read articles is a single range, then the
7039       ;; first unread article is the article after the last read
7040       ;; article.  Sounds logical, doesn't it?
7041       (if (and (not (listp (cdr read)))
7042                (or (< (car read) bottom)
7043                    (progn (setq read (list read))
7044                           nil)))
7045           (setq first (max bottom (1+ (cdr read))))
7046         ;; `read' is a list of ranges.
7047         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7048                                   (caar read)))
7049                   1)
7050           (setq first bottom))
7051         (while read
7052           (when first
7053             (push (cons first nlast) unread))
7054           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7055           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7056           (setq read (cdr read)))))
7057     ;; And add the last unread articles.
7058     (cond ((not (and first last))
7059            nil)
7060           ((< first last)
7061            (push (cons first last) unread))
7062           ((= first last)
7063            (push first unread)))
7064     ;; Return the sequence of unread articles.
7065     (delq 0 (nreverse unread))))
7066
7067 ;; Various summary commands
7068
7069 (defun gnus-summary-select-article-buffer ()
7070   "Reconfigure windows to show the article buffer.
7071 If `gnus-widen-article-window' is set, show only the article
7072 buffer."
7073   (interactive)
7074   (if (not (gnus-buffer-live-p gnus-article-buffer))
7075       (error "There is no article buffer for this summary buffer")
7076     (unless (get-buffer-window gnus-article-buffer)
7077       (gnus-summary-show-article))
7078     (gnus-configure-windows
7079      (if gnus-widen-article-window
7080          'only-article
7081        'article)
7082      t)
7083     (select-window (get-buffer-window gnus-article-buffer))))
7084
7085 (defun gnus-summary-universal-argument (arg)
7086   "Perform any operation on all articles that are process/prefixed."
7087   (interactive "P")
7088   (let ((articles (gnus-summary-work-articles arg))
7089         func article)
7090     (if (eq
7091          (setq
7092           func
7093           (key-binding
7094            (read-key-sequence
7095             (substitute-command-keys
7096              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7097          'undefined)
7098         (gnus-error 1 "Undefined key")
7099       (save-excursion
7100         (while articles
7101           (gnus-summary-goto-subject (setq article (pop articles)))
7102           (let (gnus-newsgroup-processable)
7103             (command-execute func))
7104           (gnus-summary-remove-process-mark article)))))
7105   (gnus-summary-position-point))
7106
7107 (defun gnus-summary-toggle-truncation (&optional arg)
7108   "Toggle truncation of summary lines.
7109 With ARG, turn line truncation on if ARG is positive."
7110   (interactive "P")
7111   (setq truncate-lines
7112         (if (null arg) (not truncate-lines)
7113           (> (prefix-numeric-value arg) 0)))
7114   (redraw-display))
7115
7116 (defun gnus-summary-find-for-reselect ()
7117   "Return the number of an article to stay on across a reselect.
7118 The current article is considered, then following articles, then previous
7119 articles.  An article is sought which is not canceled and isn't a temporary
7120 insertion from another group.  If there's no such then return a dummy 0."
7121   (let (found)
7122     (dolist (rev '(nil t))
7123       (unless found      ; don't demand the reverse list if we don't need it
7124         (let ((data (gnus-data-find-list
7125                      (gnus-summary-article-number) (gnus-data-list rev))))
7126           (while (and data (not found))
7127             (if (and (< 0 (gnus-data-number (car data)))
7128                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7129                 (setq found (gnus-data-number (car data))))
7130             (setq data (cdr data))))))
7131     (or found 0)))
7132
7133 (defun gnus-summary-reselect-current-group (&optional all rescan)
7134   "Exit and then reselect the current newsgroup.
7135 The prefix argument ALL means to select all articles."
7136   (interactive "P")
7137   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7138     (error "Ephemeral groups can't be reselected"))
7139   (let ((current-subject (gnus-summary-find-for-reselect))
7140         (group gnus-newsgroup-name))
7141     (setq gnus-newsgroup-begin nil)
7142     (gnus-summary-exit nil 'leave-hidden)
7143     ;; We have to adjust the point of group mode buffer because
7144     ;; point was moved to the next unread newsgroup by exiting.
7145     (gnus-summary-jump-to-group group)
7146     (when rescan
7147       (save-excursion
7148         (gnus-group-get-new-news-this-group 1)))
7149     (gnus-group-read-group all t)
7150     (gnus-summary-goto-subject current-subject nil t)))
7151
7152 (defun gnus-summary-rescan-group (&optional all)
7153   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7154   (interactive "P")
7155   (let ((config gnus-current-window-configuration))
7156     (gnus-summary-reselect-current-group all t)
7157     (gnus-configure-windows config)
7158     (when (eq config 'article)
7159       (gnus-summary-select-article))))
7160
7161 (defun gnus-summary-update-info (&optional non-destructive)
7162   (save-excursion
7163     (let ((group gnus-newsgroup-name))
7164       (when group
7165         (when gnus-newsgroup-kill-headers
7166           (setq gnus-newsgroup-killed
7167                 (gnus-compress-sequence
7168                  (gnus-sorted-union
7169                   (gnus-list-range-intersection
7170                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7171                   gnus-newsgroup-unreads)
7172                  t)))
7173         (unless (listp (cdr gnus-newsgroup-killed))
7174           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7175         (let ((headers gnus-newsgroup-headers)
7176               (ephemeral-p (gnus-ephemeral-group-p group))
7177               info)
7178           (unless ephemeral-p
7179             (setq info (copy-sequence (gnus-get-info group))
7180                   info (delq (gnus-info-params info) info)))
7181           ;; Set the new ranges of read articles.
7182           (with-current-buffer gnus-group-buffer
7183             (gnus-undo-force-boundary))
7184           (gnus-update-read-articles
7185            group (gnus-sorted-union
7186                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7187           ;; Set the current article marks.
7188           (let ((gnus-newsgroup-scored
7189                  (if (and (not gnus-save-score)
7190                           (not non-destructive))
7191                      nil
7192                    gnus-newsgroup-scored)))
7193             (save-excursion
7194               (gnus-update-marks)))
7195           ;; Do the cross-ref thing.
7196           (when gnus-use-cross-reference
7197             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7198           ;; Do not switch windows but change the buffer to work.
7199           (set-buffer gnus-group-buffer)
7200           (unless ephemeral-p
7201             (gnus-group-update-group
7202              group nil
7203              (equal info
7204                     (setq info (copy-sequence (gnus-get-info group))
7205                           info (delq (gnus-info-params info) info))))))))))
7206
7207 (defun gnus-summary-save-newsrc (&optional force)
7208   "Save the current number of read/marked articles in the dribble buffer.
7209 The dribble buffer will then be saved.
7210 If FORCE (the prefix), also save the .newsrc file(s)."
7211   (interactive "P")
7212   (gnus-summary-update-info t)
7213   (if force
7214       (gnus-save-newsrc-file)
7215     (gnus-dribble-save)))
7216
7217 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7218
7219 (defun gnus-summary-exit (&optional temporary leave-hidden)
7220   "Exit reading current newsgroup, and then return to group selection mode.
7221 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7222   (interactive)
7223   (gnus-set-global-variables)
7224   (when (gnus-buffer-live-p gnus-article-buffer)
7225     (with-current-buffer gnus-article-buffer
7226       (mm-destroy-parts gnus-article-mime-handles)
7227       ;; Set it to nil for safety reason.
7228       (setq gnus-article-mime-handle-alist nil)
7229       (setq gnus-article-mime-handles nil)))
7230   (gnus-kill-save-kill-buffer)
7231   (gnus-async-halt-prefetch)
7232   (let* ((group gnus-newsgroup-name)
7233          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7234          (gnus-group-is-exiting-p t)
7235          (article-buffer gnus-article-buffer)
7236          (original-article-buffer gnus-original-article-buffer)
7237          (mode major-mode)
7238          (group-point nil)
7239          (buf (current-buffer))
7240          ;; `gnus-single-article-buffer' is nil buffer-locally in
7241          ;; ephemeral group of which summary buffer will be killed,
7242          ;; but the global value may be non-nil.
7243          (single-article-buffer gnus-single-article-buffer))
7244     (unless quit-config
7245       ;; Do adaptive scoring, and possibly save score files.
7246       (when gnus-newsgroup-adaptive
7247         (gnus-score-adaptive))
7248       (when gnus-use-scoring
7249         (gnus-score-save)))
7250     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7251     (when gnus-use-cache
7252       (gnus-cache-possibly-remove-articles)
7253       (gnus-cache-save-buffers))
7254     (gnus-async-prefetch-remove-group group)
7255     (when gnus-suppress-duplicates
7256       (gnus-dup-enter-articles))
7257     (when gnus-use-trees
7258       (gnus-tree-close))
7259     (when gnus-use-cache
7260       (gnus-cache-write-active))
7261     ;; Remove entries for this group.
7262     (nnmail-purge-split-history (gnus-group-real-name group))
7263     ;; Make all changes in this group permanent.
7264     (unless quit-config
7265       (gnus-run-hooks 'gnus-exit-group-hook)
7266       (gnus-summary-update-info))
7267     (gnus-close-group group)
7268     ;; Make sure where we were, and go to next newsgroup.
7269     (when (buffer-live-p (get-buffer gnus-group-buffer))
7270       (set-buffer gnus-group-buffer))
7271     (unless quit-config
7272       (gnus-group-jump-to-group group))
7273     (gnus-run-hooks 'gnus-summary-exit-hook)
7274     (unless (or quit-config
7275                 (not gnus-summary-next-group-on-exit)
7276                 ;; If this group has disappeared from the summary
7277                 ;; buffer, don't skip forwards.
7278                 (not (string= group (gnus-group-group-name))))
7279       (gnus-group-next-unread-group 1))
7280     (setq group-point (point))
7281     (gnus-article-stop-animations)
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))
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       (gnus-article-stop-animations)
7373       (when quit-config
7374         (gnus-handle-ephemeral-exit quit-config)))))
7375
7376 (defun gnus-handle-ephemeral-exit (quit-config)
7377   "Handle movement when leaving an ephemeral group.
7378 The state which existed when entering the ephemeral is reset."
7379   (if (not (buffer-live-p (car quit-config)))
7380       (when (gnus-buffer-live-p gnus-group-buffer)
7381         (gnus-configure-windows 'group 'force))
7382     (set-buffer (car quit-config))
7383     (unless (eq (cdr quit-config) 'group)
7384       (setq gnus-current-select-method
7385             (gnus-find-method-for-group gnus-newsgroup-name)))
7386     (cond ((derived-mode-p 'gnus-summary-mode)
7387            (gnus-set-global-variables))
7388           ((derived-mode-p 'gnus-article-mode)
7389            (save-current-buffer
7390              ;; The `gnus-summary-buffer' variable may point
7391              ;; to the old summary buffer when using a single
7392              ;; article buffer.
7393              (unless (gnus-buffer-live-p gnus-summary-buffer)
7394                (set-buffer gnus-group-buffer))
7395              (set-buffer gnus-summary-buffer)
7396              (gnus-set-global-variables))))
7397     (if (or (eq (cdr quit-config) 'article)
7398             (eq (cdr quit-config) 'pick))
7399         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7400             (gnus-configure-windows 'pick 'force)
7401           (gnus-configure-windows (cdr quit-config) 'force))
7402       (gnus-configure-windows (cdr quit-config) 'force))
7403     (when (derived-mode-p 'gnus-summary-mode)
7404       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7405                                                      next-unread-noselect))
7406           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7407                                   'next-noselect)
7408                               (gnus-summary-next-subject 1 nil t))
7409                              ((eq gnus-auto-select-on-ephemeral-exit
7410                                   'next-unread-noselect)
7411                               (gnus-summary-next-subject 1 t t))))
7412             ;; Hide the article buffer which displays the article different
7413             ;; from the one that the cursor points to in the summary buffer.
7414             (gnus-configure-windows 'summary 'force))
7415         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7416                (gnus-summary-next-subject 1))
7417               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7418                (gnus-summary-next-subject 1 t))))
7419       (gnus-summary-recenter)
7420       (gnus-summary-position-point))))
7421
7422 ;;; Dead summaries.
7423
7424 (defvar gnus-dead-summary-mode-map
7425   (let ((map (make-keymap)))
7426     (suppress-keymap map)
7427     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7428     (dolist (key '("\C-d" "\r" "\177" [delete]))
7429       (define-key map key 'gnus-summary-wake-up-the-dead))
7430     (dolist (key '("q" "Q"))
7431       (define-key map key 'bury-buffer))
7432     map))
7433
7434 (define-minor-mode gnus-dead-summary-mode
7435   "Minor mode for Gnus summary buffers."
7436   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7437   (unless (derived-mode-p 'gnus-summary-mode)
7438     (setq gnus-dead-summary-mode nil)))
7439
7440 (defun gnus-deaden-summary ()
7441   "Make the current summary buffer into a dead summary buffer."
7442   ;; Kill any previous dead summary buffer.
7443   (when (and gnus-dead-summary
7444              (buffer-name gnus-dead-summary))
7445     (with-current-buffer gnus-dead-summary
7446       (when gnus-dead-summary-mode
7447         (kill-buffer (current-buffer)))))
7448   ;; Make this the current dead summary.
7449   (setq gnus-dead-summary (current-buffer))
7450   (gnus-dead-summary-mode 1)
7451   (let ((name (buffer-name)))
7452     (when (string-match "Summary" name)
7453       (rename-buffer
7454        (concat (substring name 0 (match-beginning 0)) "Dead "
7455                (substring name (match-beginning 0)))
7456        t)
7457       (bury-buffer))))
7458
7459 (defun gnus-kill-or-deaden-summary (buffer)
7460   "Kill or deaden the summary BUFFER."
7461   (save-excursion
7462     (when (and (buffer-name buffer)
7463                (not gnus-single-article-buffer))
7464       (with-current-buffer buffer
7465         (gnus-kill-buffer gnus-article-buffer)
7466         (gnus-kill-buffer gnus-original-article-buffer)))
7467     (cond
7468      ;; Kill the buffer.
7469      (gnus-kill-summary-on-exit
7470       (when (and gnus-use-trees
7471                  (gnus-buffer-exists-p buffer))
7472         (with-current-buffer buffer
7473           (gnus-tree-close)))
7474       (gnus-kill-buffer buffer))
7475      ;; Deaden the buffer.
7476      ((gnus-buffer-exists-p buffer)
7477       (with-current-buffer buffer
7478         (gnus-deaden-summary))))))
7479
7480 (defun gnus-summary-wake-up-the-dead (&rest args)
7481   "Wake up the dead summary buffer."
7482   (interactive)
7483   (gnus-dead-summary-mode -1)
7484   (let ((name (buffer-name)))
7485     (when (string-match "Dead " name)
7486       (rename-buffer
7487        (concat (substring name 0 (match-beginning 0))
7488                (substring name (match-end 0)))
7489        t)))
7490   (gnus-message 3 "This dead summary is now alive again"))
7491
7492 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7493 (defun gnus-summary-describe-group (&optional force)
7494   "Describe the current newsgroup."
7495   (interactive "P")
7496   (gnus-group-describe-group force gnus-newsgroup-name))
7497
7498 (defun gnus-summary-describe-briefly ()
7499   "Describe summary mode commands briefly."
7500   (interactive)
7501   (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")))
7502
7503 ;; Walking around group mode buffer from summary mode.
7504
7505 (defun gnus-summary-next-group (&optional no-article target-group backward)
7506   "Exit current newsgroup and then select next unread newsgroup.
7507 If prefix argument NO-ARTICLE is non-nil, no article is selected
7508 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7509 previous group instead."
7510   (interactive "P")
7511   ;; Stop pre-fetching.
7512   (gnus-async-halt-prefetch)
7513   (let ((current-group gnus-newsgroup-name)
7514         (current-buffer (current-buffer))
7515         entered)
7516     ;; First we semi-exit this group to update Xrefs and all variables.
7517     ;; We can't do a real exit, because the window conf must remain
7518     ;; the same in case the user is prompted for info, and we don't
7519     ;; want the window conf to change before that...
7520     (gnus-summary-exit t)
7521     (while (not entered)
7522       ;; Then we find what group we are supposed to enter.
7523       (set-buffer gnus-group-buffer)
7524       (gnus-group-jump-to-group current-group)
7525       (setq target-group
7526             (or target-group
7527                 (if (eq gnus-keep-same-level 'best)
7528                     (gnus-summary-best-group gnus-newsgroup-name)
7529                   (gnus-summary-search-group backward gnus-keep-same-level))))
7530       (if (not target-group)
7531           ;; There are no further groups, so we return to the group
7532           ;; buffer.
7533           (progn
7534             (gnus-message 5 "Returning to the group buffer")
7535             (setq entered t)
7536             (when (gnus-buffer-live-p current-buffer)
7537               (set-buffer current-buffer)
7538               (gnus-summary-exit))
7539             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7540         ;; We try to enter the target group.
7541         (gnus-group-jump-to-group target-group)
7542         (let ((unreads (gnus-group-group-unread)))
7543           (if (and (or (eq t unreads)
7544                        (and unreads (not (zerop unreads))))
7545                    (gnus-summary-read-group
7546                     target-group nil no-article
7547                     (and (buffer-name current-buffer) current-buffer)
7548                     nil backward))
7549               (setq entered t)
7550             (setq current-group target-group
7551                   target-group nil)))))))
7552
7553 (defun gnus-summary-prev-group (&optional no-article)
7554   "Exit current newsgroup and then select previous unread newsgroup.
7555 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7556   (interactive "P")
7557   (gnus-summary-next-group no-article nil t))
7558
7559 ;; Walking around summary lines.
7560
7561 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7562   "Go to the first subject satisfying any non-nil constraint.
7563 If UNREAD is non-nil, the article should be unread.
7564 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7565 If UNSEEN is non-nil, the article should be unseen as well as unread.
7566 Returns the article selected or nil if there are no matching articles."
7567   (interactive "P")
7568   (cond
7569    ;; Empty summary.
7570    ((null gnus-newsgroup-data)
7571     (gnus-message 3 "No articles in the group")
7572     nil)
7573    ;; Pick the first article.
7574    ((not (or unread undownloaded unseen))
7575     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7576     (gnus-data-number (car gnus-newsgroup-data)))
7577    ;; Find the first unread article.
7578    (t
7579     (let ((data gnus-newsgroup-data))
7580       (while (and data
7581                   (let ((num (gnus-data-number (car data))))
7582                     (or (memq num gnus-newsgroup-unfetched)
7583                         (not (or (and unread
7584                                       (memq num gnus-newsgroup-unreads))
7585                                  (and undownloaded
7586                                       (memq num gnus-newsgroup-undownloaded))
7587                                  (and unseen
7588                                       (memq num gnus-newsgroup-unseen)
7589                                       (memq num gnus-newsgroup-unreads)))))))
7590         (setq data (cdr data)))
7591       (prog1
7592           (if data
7593               (progn
7594                 (goto-char (gnus-data-pos (car data)))
7595                 (gnus-data-number (car data)))
7596             (gnus-message 3 "No more%s articles"
7597                           (let* ((r (when unread " unread"))
7598                                  (d (when undownloaded " undownloaded"))
7599                                  (s (when unseen " unseen"))
7600                                  (l (delq nil (list r d s))))
7601                             (cond ((= 3 (length l))
7602                                    (concat r "," d ", or" s))
7603                                   ((= 2 (length l))
7604                                    (concat (car l) ", or" (cadr l)))
7605                                   ((= 1 (length l))
7606                                    (car l))
7607                                   (t
7608                                    ""))))
7609             nil
7610             )
7611         (gnus-summary-position-point))))))
7612
7613 (defun gnus-summary-next-subject (n &optional unread dont-display)
7614   "Go to next N'th summary line.
7615 If N is negative, go to the previous N'th subject line.
7616 If UNREAD is non-nil, only unread articles are selected.
7617 The difference between N and the actual number of steps taken is
7618 returned."
7619   (interactive "p")
7620   (let ((backward (< n 0))
7621         (n (abs n)))
7622     (while (and (> n 0)
7623                 (if backward
7624                     (gnus-summary-find-prev unread)
7625                   (gnus-summary-find-next unread)))
7626       (unless (zerop (setq n (1- n)))
7627         (gnus-summary-show-thread)))
7628     (when (/= 0 n)
7629       (gnus-message 7 "No more%s articles"
7630                     (if unread " unread" "")))
7631     (unless dont-display
7632       (gnus-summary-recenter)
7633       (gnus-summary-position-point))
7634     n))
7635
7636 (defun gnus-summary-next-unread-subject (n)
7637   "Go to next N'th unread summary line."
7638   (interactive "p")
7639   (gnus-summary-next-subject n t))
7640
7641 (defun gnus-summary-prev-subject (n &optional unread)
7642   "Go to previous N'th summary line.
7643 If optional argument UNREAD is non-nil, only unread article is selected."
7644   (interactive "p")
7645   (gnus-summary-next-subject (- n) unread))
7646
7647 (defun gnus-summary-prev-unread-subject (n)
7648   "Go to previous N'th unread summary line."
7649   (interactive "p")
7650   (gnus-summary-next-subject (- n) t))
7651
7652 (defun gnus-summary-goto-subjects (articles)
7653   "Insert the subject header for ARTICLES in the current buffer."
7654   (save-excursion
7655     (dolist (article articles)
7656       (gnus-summary-goto-subject article t)))
7657   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7658   (gnus-summary-position-point))
7659
7660 (defun gnus-summary-goto-subject (article &optional force silent)
7661   "Go to the subject line of ARTICLE.
7662 If FORCE, also allow jumping to articles not currently shown."
7663   (interactive "nArticle number: ")
7664   (unless (numberp article)
7665     (error "Article %s is not a number" article))
7666   (let ((b (point))
7667         (data (gnus-data-find article)))
7668     ;; We read in the article if we have to.
7669     (and (not data)
7670          force
7671          (gnus-summary-insert-subject
7672           article
7673           (if (or (numberp force) (vectorp force)) force)
7674           t)
7675          (setq data (gnus-data-find article)))
7676     (goto-char b)
7677     (if (not data)
7678         (progn
7679           (unless silent
7680             (gnus-message 3 "Can't find article %d" article))
7681           nil)
7682       (let ((pt (gnus-data-pos data)))
7683         (goto-char pt)
7684         (gnus-summary-set-article-display-arrow pt))
7685       (gnus-summary-position-point)
7686       article)))
7687
7688 ;; Walking around summary lines with displaying articles.
7689
7690 (defun gnus-summary-expand-window (&optional arg)
7691   "Make the summary buffer take up the entire Emacs frame.
7692 Given a prefix, will force an `article' buffer configuration."
7693   (interactive "P")
7694   (if arg
7695       (gnus-configure-windows 'article 'force)
7696     (gnus-configure-windows 'summary 'force)))
7697
7698 (defun gnus-summary-display-article (article &optional all-header)
7699   "Display ARTICLE in article buffer."
7700   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7701                (with-current-buffer gnus-article-buffer
7702                  (derived-mode-p 'gnus-article-mode)))
7703     (gnus-article-setup-buffer))
7704   (gnus-set-global-variables)
7705   (with-current-buffer gnus-article-buffer
7706     (setq gnus-article-charset gnus-newsgroup-charset)
7707     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7708     (mm-enable-multibyte))
7709   (if (null article)
7710       nil
7711     (prog1
7712         (if gnus-summary-display-article-function
7713             (funcall gnus-summary-display-article-function article all-header)
7714           (gnus-article-prepare article all-header))
7715       (gnus-run-hooks 'gnus-select-article-hook)
7716       (when (and gnus-current-article
7717                  (not (zerop gnus-current-article)))
7718         (gnus-summary-goto-subject gnus-current-article))
7719       (gnus-summary-recenter)
7720       (when (and gnus-use-trees gnus-show-threads)
7721         (gnus-possibly-generate-tree article)
7722         (gnus-highlight-selected-tree article))
7723       ;; Successfully display article.
7724       (gnus-article-set-window-start
7725        (cdr (assq article gnus-newsgroup-bookmarks))))))
7726
7727 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7728   "Select the current article.
7729 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7730 non-nil, the article will be re-fetched even if it already present in
7731 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7732 be displayed."
7733   ;; Make sure we are in the summary buffer to work around bbdb bug.
7734   (unless (derived-mode-p 'gnus-summary-mode)
7735     (set-buffer gnus-summary-buffer))
7736   (let ((article (or article (gnus-summary-article-number)))
7737         (all-headers (not (not all-headers))) ;Must be t or nil.
7738         gnus-summary-display-article-function)
7739     (and (not pseudo)
7740          (gnus-summary-article-pseudo-p article)
7741          (error "This is a pseudo-article"))
7742     (with-current-buffer gnus-summary-buffer
7743       (if (or (and gnus-single-article-buffer
7744                    (or (null gnus-current-article)
7745                        (null gnus-article-current)
7746                        (null (get-buffer gnus-article-buffer))
7747                        (not (eq article (cdr gnus-article-current)))
7748                        (not (equal (car gnus-article-current)
7749                                    gnus-newsgroup-name))
7750                        (not (get-buffer gnus-original-article-buffer))))
7751               (and (not gnus-single-article-buffer)
7752                    (or (null gnus-current-article)
7753                        (not (get-buffer gnus-original-article-buffer))
7754                        (not (eq gnus-current-article article))))
7755               force)
7756           ;; The requested article is different from the current article.
7757           (progn
7758             (gnus-summary-display-article article all-headers)
7759             (when (gnus-buffer-live-p gnus-article-buffer)
7760               (with-current-buffer gnus-article-buffer
7761                 (if (not gnus-article-decoded-p) ;; a local variable
7762                     (mm-disable-multibyte))))
7763             (gnus-article-set-window-start
7764              (cdr (assq article gnus-newsgroup-bookmarks)))
7765             article)
7766         'old))))
7767
7768 (defun gnus-summary-force-verify-and-decrypt ()
7769   "Display buttons for signed/encrypted parts and verify/decrypt them."
7770   (interactive)
7771   (let ((mm-verify-option 'known)
7772         (mm-decrypt-option 'known)
7773         (gnus-article-emulate-mime t)
7774         (gnus-buttonized-mime-types (append (list "multipart/signed"
7775                                                   "multipart/encrypted")
7776                                             gnus-buttonized-mime-types)))
7777     (gnus-summary-select-article nil 'force)))
7778
7779 (defun gnus-summary-next-article (&optional unread subject backward push)
7780   "Select the next article.
7781 If UNREAD, only unread articles are selected.
7782 If SUBJECT, only articles with SUBJECT are selected.
7783 If BACKWARD, the previous article is selected instead of the next."
7784   (interactive "P")
7785   ;; Make sure we are in the summary buffer.
7786   (unless (derived-mode-p 'gnus-summary-mode)
7787     (set-buffer gnus-summary-buffer))
7788   (cond
7789    ;; Is there such an article?
7790    ((and (gnus-summary-search-forward unread subject backward)
7791          (or (gnus-summary-display-article (gnus-summary-article-number))
7792              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7793     (gnus-summary-position-point))
7794    ;; If not, we try the first unread, if that is wanted.
7795    ((and subject
7796          gnus-auto-select-same
7797          (gnus-summary-first-unread-article))
7798     (gnus-summary-position-point)
7799     (gnus-message 6 "Wrapped"))
7800    ;; Try to get next/previous article not displayed in this group.
7801    ((and gnus-auto-extend-newsgroup
7802          (not unread) (not subject))
7803     (gnus-summary-goto-article
7804      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7805      nil (count-lines (point-min) (point))))
7806    ;; Go to next/previous group.
7807    (t
7808     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7809       (gnus-summary-jump-to-group gnus-newsgroup-name))
7810     (let ((cmd (if (featurep 'xemacs)
7811                    last-command-char
7812                  last-command-event))
7813           (point
7814            (with-current-buffer gnus-group-buffer
7815              (point)))
7816           (current-summary (current-buffer))
7817           (group
7818            (if (eq gnus-keep-same-level 'best)
7819                (gnus-summary-best-group gnus-newsgroup-name)
7820              (gnus-summary-search-group backward gnus-keep-same-level))))
7821       ;; Select next unread newsgroup automagically.
7822       (cond
7823        ((or (not gnus-auto-select-next)
7824             (not cmd))
7825         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7826        ((or (eq gnus-auto-select-next 'quietly)
7827             (and (eq gnus-auto-select-next 'slightly-quietly)
7828                  push)
7829             (and (eq gnus-auto-select-next 'almost-quietly)
7830                  (gnus-summary-last-article-p)))
7831         ;; Select quietly.
7832         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7833             (gnus-summary-exit)
7834           (gnus-message 7 "No more%s articles (%s)..."
7835                         (if unread " unread" "")
7836                         (if group (concat "selecting " group)
7837                           "exiting"))
7838           (gnus-summary-next-group nil group backward)))
7839        (t
7840         (when (gnus-key-press-event-p last-input-event)
7841           ;; Somehow or other, we may now have selected a different
7842           ;; window.  Make point go back to the summary buffer.
7843           (when (eq current-summary (current-buffer))
7844             ;; FIXME: This burps when get-buffer-window returns nil.
7845             (select-window (get-buffer-window current-summary 0)))
7846           (gnus-summary-walk-group-buffer
7847            gnus-newsgroup-name cmd unread backward point))))))))
7848
7849 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7850   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7851                       (?\C-p (gnus-group-prev-unread-group 1))))
7852         (cursor-in-echo-area t)
7853         keve key group ended prompt)
7854     (with-current-buffer gnus-group-buffer
7855       (goto-char start)
7856       (setq group
7857             (if (eq gnus-keep-same-level 'best)
7858                 (gnus-summary-best-group gnus-newsgroup-name)
7859               (gnus-summary-search-group backward gnus-keep-same-level))))
7860     (while (not ended)
7861       (setq prompt
7862             (format
7863              "No more%s articles%s " (if unread " unread" "")
7864              (if (and group
7865                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7866                  (format " (Type %s for %s [%s])"
7867                          (single-key-description cmd)
7868                          (gnus-group-decoded-name group)
7869                          (gnus-group-unread group))
7870                (format " (Type %s to exit %s)"
7871                        (single-key-description cmd)
7872                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7873       ;; Confirm auto selection.
7874       (setq key (car (setq keve (gnus-read-event-char prompt)))
7875             ended t)
7876       (cond
7877        ((assq key keystrokes)
7878         (let ((obuf (current-buffer)))
7879           (switch-to-buffer gnus-group-buffer)
7880           (when group
7881             (gnus-group-jump-to-group group))
7882           (eval (cadr (assq key keystrokes)))
7883           (setq group (gnus-group-group-name))
7884           (switch-to-buffer obuf))
7885         (setq ended nil))
7886        ((equal key cmd)
7887         (if (or (not group)
7888                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7889             (gnus-summary-exit)
7890           (gnus-summary-next-group nil group backward)))
7891        (t
7892         (push (cdr keve) unread-command-events))))))
7893
7894 (defun gnus-summary-next-unread-article ()
7895   "Select unread article after current one."
7896   (interactive)
7897   (gnus-summary-next-article
7898    (or (not (eq gnus-summary-goto-unread 'never))
7899        (gnus-summary-last-article-p (gnus-summary-article-number)))
7900    (and gnus-auto-select-same
7901         (gnus-summary-article-subject))))
7902
7903 (defun gnus-summary-prev-article (&optional unread subject)
7904   "Select the article before the current one.
7905 If UNREAD is non-nil, only unread articles are selected."
7906   (interactive "P")
7907   (gnus-summary-next-article unread subject t))
7908
7909 (defun gnus-summary-prev-unread-article ()
7910   "Select unread article before current one."
7911   (interactive)
7912   (gnus-summary-prev-article
7913    (or (not (eq gnus-summary-goto-unread 'never))
7914        (gnus-summary-first-article-p (gnus-summary-article-number)))
7915    (and gnus-auto-select-same
7916         (gnus-summary-article-subject))))
7917
7918 (declare-function gnus-article-only-boring-p "gnus-art" ())
7919
7920 (defun gnus-summary-next-page (&optional lines circular stop)
7921   "Show next page of the selected article.
7922 If at the end of the current article, select the next article.
7923 LINES says how many lines should be scrolled up.
7924
7925 If CIRCULAR is non-nil, go to the start of the article instead of
7926 selecting the next article when reaching the end of the current
7927 article.
7928
7929 If STOP is non-nil, just stop when reaching the end of the message.
7930
7931 Also see the variable `gnus-article-skip-boring'."
7932   (interactive "P")
7933   (gnus-set-global-variables)
7934   (let ((article (gnus-summary-article-number))
7935         (article-window (get-buffer-window gnus-article-buffer t))
7936         endp)
7937     ;; If the buffer is empty, we have no article.
7938     (unless article
7939       (error "No article to select"))
7940     (gnus-configure-windows 'article)
7941     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7942         (if (and (eq gnus-summary-goto-unread 'never)
7943                  (not (gnus-summary-last-article-p article)))
7944             (gnus-summary-next-article)
7945           (gnus-summary-next-unread-article))
7946       (if (or (null gnus-current-article)
7947               (null gnus-article-current)
7948               (/= article (cdr gnus-article-current))
7949               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7950           ;; Selected subject is different from current article's.
7951           (gnus-summary-display-article article)
7952         (when article-window
7953           (gnus-eval-in-buffer-window gnus-article-buffer
7954             (setq endp (or (gnus-article-next-page lines)
7955                            (gnus-article-only-boring-p))))
7956           (when endp
7957             (cond ((or stop gnus-summary-stop-at-end-of-message)
7958                    (gnus-message 3 "End of message"))
7959                   (circular
7960                    (gnus-summary-beginning-of-article))
7961                   (lines
7962                    (gnus-message 3 "End of message"))
7963                   ((null lines)
7964                    (if (and (eq gnus-summary-goto-unread 'never)
7965                             (not (gnus-summary-last-article-p article)))
7966                        (gnus-summary-next-article)
7967                      (gnus-summary-next-unread-article))))))))
7968     (gnus-summary-recenter)
7969     (gnus-summary-position-point)))
7970
7971 (defun gnus-summary-prev-page (&optional lines move)
7972   "Show previous page of selected article.
7973 Argument LINES specifies lines to be scrolled down.
7974 If MOVE, move to the previous unread article if point is at
7975 the beginning of the buffer."
7976   (interactive "P")
7977   (let ((article (gnus-summary-article-number))
7978         (article-window (get-buffer-window gnus-article-buffer t))
7979         endp)
7980     (gnus-configure-windows 'article)
7981     (if (or (null gnus-current-article)
7982             (null gnus-article-current)
7983             (/= article (cdr gnus-article-current))
7984             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7985         ;; Selected subject is different from current article's.
7986         (gnus-summary-display-article article)
7987       (gnus-summary-recenter)
7988       (when article-window
7989         (gnus-eval-in-buffer-window gnus-article-buffer
7990           (setq endp (gnus-article-prev-page lines)))
7991         (when (and move endp)
7992           (cond (lines
7993                  (gnus-message 3 "Beginning of message"))
7994                 ((null lines)
7995                  (if (and (eq gnus-summary-goto-unread 'never)
7996                           (not (gnus-summary-first-article-p article)))
7997                      (gnus-summary-prev-article)
7998                    (gnus-summary-prev-unread-article))))))))
7999   (gnus-summary-position-point))
8000
8001 (defun gnus-summary-prev-page-or-article (&optional lines)
8002   "Show previous page of selected article.
8003 Argument LINES specifies lines to be scrolled down.
8004 If at the beginning of the article, go to the next article."
8005   (interactive "P")
8006   (gnus-summary-prev-page lines t))
8007
8008 (defun gnus-summary-scroll-up (lines)
8009   "Scroll up (or down) one line current article.
8010 Argument LINES specifies lines to be scrolled up (or down if negative).
8011 If no article is selected, then the current article will be selected first."
8012   (interactive "p")
8013   (gnus-configure-windows 'article)
8014   (gnus-summary-show-thread)
8015   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
8016     (gnus-eval-in-buffer-window gnus-article-buffer
8017       (cond ((> lines 0)
8018              (when (gnus-article-next-page lines)
8019                (gnus-message 3 "End of message")))
8020             ((< lines 0)
8021              (gnus-article-prev-page (- lines))))))
8022   (gnus-summary-recenter)
8023   (gnus-summary-position-point))
8024
8025 (defun gnus-summary-scroll-down (lines)
8026   "Scroll down (or up) one line current article.
8027 Argument LINES specifies lines to be scrolled down (or up if negative).
8028 If no article is selected, then the current article will be selected first."
8029   (interactive "p")
8030   (gnus-summary-scroll-up (- lines)))
8031
8032 (defun gnus-summary-next-same-subject ()
8033   "Select next article which has the same subject as current one."
8034   (interactive)
8035   (gnus-summary-next-article nil (gnus-summary-article-subject)))
8036
8037 (defun gnus-summary-prev-same-subject ()
8038   "Select previous article which has the same subject as current one."
8039   (interactive)
8040   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8041
8042 (defun gnus-summary-next-unread-same-subject ()
8043   "Select next unread article which has the same subject as current one."
8044   (interactive)
8045   (gnus-summary-next-article t (gnus-summary-article-subject)))
8046
8047 (defun gnus-summary-prev-unread-same-subject ()
8048   "Select previous unread article which has the same subject as current one."
8049   (interactive)
8050   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8051
8052 (defun gnus-summary-first-unread-article ()
8053   "Select the first unread article.
8054 Return nil if there are no unread articles."
8055   (interactive)
8056   (prog1
8057       (when (gnus-summary-first-subject t)
8058         (gnus-summary-show-thread)
8059         (gnus-summary-first-subject t)
8060         (gnus-summary-display-article (gnus-summary-article-number)))
8061     (gnus-summary-position-point)))
8062
8063 (defun gnus-summary-first-unread-subject ()
8064   "Place the point on the subject line of the first unread article.
8065 Return nil if there are no unread articles."
8066   (interactive)
8067   (prog1
8068       (when (gnus-summary-first-subject t)
8069         (gnus-summary-show-thread)
8070         (gnus-summary-first-subject t))
8071     (gnus-summary-position-point)))
8072
8073 (defun gnus-summary-first-unseen-subject ()
8074   "Place the point on the subject line of the first unseen article.
8075 Return nil if there are no unseen articles."
8076   (interactive)
8077   (prog1
8078       (when (gnus-summary-first-subject nil nil t)
8079         (gnus-summary-show-thread)
8080         (gnus-summary-first-subject nil nil t))
8081     (gnus-summary-position-point)))
8082
8083 (defun gnus-summary-first-unseen-or-unread-subject ()
8084   "Place the point on the subject line of the first unseen and unread article.
8085 If all article have been seen, on the subject line of the first unread
8086 article."
8087   (interactive)
8088   (prog1
8089       (unless (when (gnus-summary-first-subject nil nil t)
8090                 (gnus-summary-show-thread)
8091                 (gnus-summary-first-subject nil nil t))
8092         (when (gnus-summary-first-subject t)
8093           (gnus-summary-show-thread)
8094           (gnus-summary-first-subject t)))
8095     (gnus-summary-position-point)))
8096
8097 (defun gnus-summary-first-article ()
8098   "Select the first article.
8099 Return nil if there are no articles."
8100   (interactive)
8101   (prog1
8102       (when (gnus-summary-first-subject)
8103         (gnus-summary-show-thread)
8104         (gnus-summary-first-subject)
8105         (gnus-summary-display-article (gnus-summary-article-number)))
8106     (gnus-summary-position-point)))
8107
8108 (defun gnus-summary-best-unread-article (&optional arg)
8109   "Select the unread article with the highest score.
8110 If given a prefix argument, select the next unread article that has a
8111 score higher than the default score."
8112   (interactive "P")
8113   (let ((article (if arg
8114                      (gnus-summary-better-unread-subject)
8115                    (gnus-summary-best-unread-subject))))
8116     (if article
8117         (gnus-summary-goto-article article)
8118       (error "No unread articles"))))
8119
8120 (defun gnus-summary-best-unread-subject ()
8121   "Select the unread subject with the highest score."
8122   (interactive)
8123   (let ((best -1000000)
8124         (data gnus-newsgroup-data)
8125         article score)
8126     (while data
8127       (and (gnus-data-unread-p (car data))
8128            (> (setq score
8129                     (gnus-summary-article-score (gnus-data-number (car data))))
8130               best)
8131            (setq best score
8132                  article (gnus-data-number (car data))))
8133       (setq data (cdr data)))
8134     (when article
8135       (gnus-summary-goto-subject article))
8136     (gnus-summary-position-point)
8137     article))
8138
8139 (defun gnus-summary-better-unread-subject ()
8140   "Select the first unread subject that has a score over the default score."
8141   (interactive)
8142   (let ((data gnus-newsgroup-data)
8143         article score)
8144     (while (and (setq article (gnus-data-number (car data)))
8145                 (or (gnus-data-read-p (car data))
8146                     (not (> (gnus-summary-article-score article)
8147                             gnus-summary-default-score))))
8148       (setq data (cdr data)))
8149     (when article
8150       (gnus-summary-goto-subject article))
8151     (gnus-summary-position-point)
8152     article))
8153
8154 (defun gnus-summary-last-subject ()
8155   "Go to the last displayed subject line in the group."
8156   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8157     (when article
8158       (gnus-summary-goto-subject article))))
8159
8160 (defun gnus-summary-goto-article (article &optional all-headers force)
8161   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8162 If ALL-HEADERS is non-nil, no header lines are hidden.
8163 If FORCE, go to the article even if it isn't displayed.  If FORCE
8164 is a number, it is the line the article is to be displayed on."
8165   (interactive
8166    (list
8167     (gnus-completing-read
8168      "Article number or Message-ID"
8169      (mapcar 'int-to-string gnus-newsgroup-limit))
8170     current-prefix-arg
8171     t))
8172   (prog1
8173       (if (and (stringp article)
8174                (string-match "@\\|%40" article))
8175           (gnus-summary-refer-article article)
8176         (when (stringp article)
8177           (setq article (string-to-number article)))
8178         (if (gnus-summary-goto-subject article force)
8179             (gnus-summary-display-article article all-headers)
8180           (gnus-message 4 "Couldn't go to article %s" article) nil))
8181     (gnus-summary-position-point)))
8182
8183 (defun gnus-summary-goto-last-article ()
8184   "Go to the previously read article."
8185   (interactive)
8186   (prog1
8187       (when gnus-last-article
8188         (gnus-summary-goto-article gnus-last-article nil t))
8189     (gnus-summary-position-point)))
8190
8191 (defun gnus-summary-pop-article (number)
8192   "Pop one article off the history and go to the previous.
8193 NUMBER articles will be popped off."
8194   (interactive "p")
8195   (let (to)
8196     (setq gnus-newsgroup-history
8197           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8198     (if to
8199         (gnus-summary-goto-article (car to) nil t)
8200       (error "Article history empty")))
8201   (gnus-summary-position-point))
8202
8203 ;; Summary commands and functions for limiting the summary buffer.
8204
8205 (defun gnus-summary-limit-to-articles (n)
8206   "Limit the summary buffer to the next N articles.
8207 If not given a prefix, use the process marked articles instead."
8208   (interactive "P")
8209   (prog1
8210       (let ((articles (gnus-summary-work-articles n)))
8211         (setq gnus-newsgroup-processable nil)
8212         (gnus-summary-limit articles))
8213     (gnus-summary-position-point)))
8214
8215 (defun gnus-summary-pop-limit (&optional total)
8216   "Restore the previous limit.
8217 If given a prefix, remove all limits."
8218   (interactive "P")
8219   (when total
8220     (setq gnus-newsgroup-limits
8221           (list (mapcar (lambda (h) (mail-header-number h))
8222                         gnus-newsgroup-headers))))
8223   (unless gnus-newsgroup-limits
8224     (error "No limit to pop"))
8225   (prog1
8226       (gnus-summary-limit nil 'pop)
8227     (gnus-summary-position-point)))
8228
8229 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8230   "Limit the summary buffer to articles that have subjects that match a regexp.
8231 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8232   (interactive
8233    (list (read-string (if current-prefix-arg
8234                           "Exclude subject (regexp): "
8235                         "Limit to subject (regexp): "))
8236          nil current-prefix-arg))
8237   (unless header
8238     (setq header "subject"))
8239   (when (not (equal "" subject))
8240     (prog1
8241         (let ((articles (gnus-summary-find-matching
8242                          (or header "subject") subject 'all nil nil
8243                          not-matching)))
8244           (unless articles
8245             (error "Found no matches for \"%s\"" subject))
8246           (gnus-summary-limit articles))
8247       (gnus-summary-position-point))))
8248
8249 (defun gnus-summary-limit-to-author (from &optional not-matching)
8250   "Limit the summary buffer to articles that have authors that match a regexp.
8251 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8252   (interactive
8253    (list (let* ((header (gnus-summary-article-header))
8254                 (default (and header (car (mail-header-parse-address
8255                                            (mail-header-from header))))))
8256            (read-string (concat (if current-prefix-arg
8257                                     "Exclude author (regexp"
8258                                   "Limit to author (regexp")
8259                                 (if default
8260                                     (concat ", default \"" default "\"): ")
8261                                   "): "))
8262                         nil nil
8263                         default))
8264          current-prefix-arg))
8265   (gnus-summary-limit-to-subject from "from" not-matching))
8266
8267 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8268   "Limit the summary buffer to articles with the given RECIPIENT.
8269
8270 If NOT-MATCHING, exclude RECIPIENT.
8271
8272 To and Cc headers are checked.  You need to include them in
8273 `nnmail-extra-headers'."
8274   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8275   (interactive
8276    (list (read-string (format "%s recipient (regexp): "
8277                               (if current-prefix-arg "Exclude" "Limit to")))
8278          current-prefix-arg))
8279   (when (not (equal "" recipient))
8280     (prog1 (let* ((to
8281                    (if (memq 'To nnmail-extra-headers)
8282                        (gnus-summary-find-matching
8283                         (cons 'extra 'To) recipient 'all nil nil
8284                         not-matching)
8285                      (gnus-message
8286                       1 "`To' isn't present in `nnmail-extra-headers'")
8287                      (sit-for 1)
8288                      nil))
8289                   (cc
8290                    (if (memq 'Cc nnmail-extra-headers)
8291                        (gnus-summary-find-matching
8292                         (cons 'extra 'Cc) recipient 'all nil nil
8293                         not-matching)
8294                      (gnus-message
8295                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8296                      (sit-for 1)
8297                      nil))
8298                   (articles
8299                    (if not-matching
8300                        ;; We need the numbers that are in both lists:
8301                        (mapcar (lambda (a)
8302                                  (and (memq a to) a))
8303                                cc)
8304                      (nconc to cc))))
8305              (unless articles
8306                (error "Found no matches for \"%s\"" recipient))
8307              (gnus-summary-limit articles))
8308       (gnus-summary-position-point))))
8309
8310 (defun gnus-summary-limit-to-address (address &optional not-matching)
8311   "Limit the summary buffer to articles with the given ADDRESS.
8312
8313 If NOT-MATCHING, exclude ADDRESS.
8314
8315 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8316 in `nnmail-extra-headers'."
8317   (interactive
8318    (list (read-string (format "%s address (regexp): "
8319                               (if current-prefix-arg "Exclude" "Limit to")))
8320          current-prefix-arg))
8321   (when (not (equal "" address))
8322     (prog1 (let* ((to
8323                    (if (memq 'To nnmail-extra-headers)
8324                        (gnus-summary-find-matching
8325                         (cons 'extra 'To) address 'all nil nil
8326                         not-matching)
8327                      (gnus-message
8328                       1 "`To' isn't present in `nnmail-extra-headers'")
8329                      (sit-for 1)
8330                      t))
8331                   (cc
8332                    (if (memq 'Cc nnmail-extra-headers)
8333                        (gnus-summary-find-matching
8334                         (cons 'extra 'Cc) address 'all nil nil
8335                         not-matching)
8336                      (gnus-message
8337                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8338                      (sit-for 1)
8339                      t))
8340                   (from
8341                    (gnus-summary-find-matching "from" address
8342                                                'all nil nil not-matching))
8343                   (articles
8344                    (if not-matching
8345                        ;; We need the numbers that are in all lists:
8346                        (if (eq cc t)
8347                            (if (eq to t)
8348                                from
8349                              (mapcar (lambda (a) (car (memq a from))) to))
8350                          (if (eq to t)
8351                              (mapcar (lambda (a) (car (memq a from))) cc)
8352                            (mapcar (lambda (a) (car (memq a from)))
8353                                    (mapcar (lambda (a) (car (memq a to)))
8354                                            cc))))
8355                      (nconc (if (eq to t) nil to)
8356                             (if (eq cc t) nil cc)
8357                             from))))
8358              (unless articles
8359                (error "Found no matches for \"%s\"" address))
8360              (gnus-summary-limit articles))
8361       (gnus-summary-position-point))))
8362
8363 (defun gnus-summary-limit-strange-charsets-predicate (header)
8364   (when (fboundp 'char-charset)
8365     (let ((string (concat (mail-header-subject header)
8366                           (mail-header-from header)))
8367           charset found)
8368       (dotimes (i (1- (length string)))
8369         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8370         (when (string-match "unicode\\|big\\|japanese" charset)
8371           (setq found t)))
8372       found)))
8373
8374 (defun gnus-summary-limit-to-predicate (predicate)
8375   "Limit to articles where PREDICATE returns non-nil.
8376 PREDICATE will be called with the header structures of the
8377 articles."
8378   (let ((articles nil)
8379         (case-fold-search t))
8380     (dolist (header gnus-newsgroup-headers)
8381       (when (funcall predicate header)
8382         (push (mail-header-number header) articles)))
8383     (gnus-summary-limit (nreverse articles))))
8384
8385 (defun gnus-summary-limit-to-age (age &optional younger-p)
8386   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8387 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8388 articles that are younger than AGE days."
8389   (interactive
8390    (let ((younger current-prefix-arg)
8391          (days-got nil)
8392          days)
8393      (while (not days-got)
8394        (setq days (if younger
8395                       (read-string "Limit to articles younger than (in days, older when negative): ")
8396                     (read-string
8397                      "Limit to articles older than (in days, younger when negative): ")))
8398        (when (> (length days) 0)
8399          (setq days (read days)))
8400        (if (numberp days)
8401            (progn
8402              (setq days-got t)
8403              (when (< days 0)
8404                (setq younger (not younger))
8405                (setq days (* days -1))))
8406          (message "Please enter a number.")
8407          (sleep-for 1)))
8408      (list days younger)))
8409   (prog1
8410       (let ((data gnus-newsgroup-data)
8411             (cutoff (days-to-time age))
8412             articles d date is-younger)
8413         (while (setq d (pop data))
8414           (when (and (vectorp (gnus-data-header d))
8415                      (setq date (mail-header-date (gnus-data-header d))))
8416             (setq is-younger (time-less-p
8417                               (time-since (gnus-date-get-time date))
8418                               cutoff))
8419             (when (if younger-p
8420                       is-younger
8421                     (not is-younger))
8422               (push (gnus-data-number d) articles))))
8423         (gnus-summary-limit (nreverse articles)))
8424     (gnus-summary-position-point)))
8425
8426 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8427   "Limit the summary buffer to articles that match an 'extra' header."
8428   (interactive
8429    (let ((header
8430           (intern
8431            (gnus-completing-read
8432             (if current-prefix-arg
8433                 "Exclude extra header"
8434               "Limit extra header")
8435             (mapcar 'symbol-name gnus-extra-headers)
8436             t nil nil
8437             (symbol-name (car gnus-extra-headers))))))
8438      (list header
8439            (read-string (format "%s header %s (regexp): "
8440                                 (if current-prefix-arg "Exclude" "Limit to")
8441                                 header))
8442            current-prefix-arg)))
8443   (when (not (equal "" regexp))
8444     (prog1
8445         (let ((articles (gnus-summary-find-matching
8446                          (cons 'extra header) regexp 'all nil nil
8447                          not-matching)))
8448           (unless articles
8449             (error "Found no matches for \"%s\"" regexp))
8450           (gnus-summary-limit articles))
8451       (gnus-summary-position-point))))
8452
8453 (defun gnus-summary-limit-to-display-predicate ()
8454   "Limit the summary buffer to the predicated in the `display' group parameter."
8455   (interactive)
8456   (unless gnus-newsgroup-display
8457     (error "There is no `display' group parameter"))
8458   (let (articles)
8459     (dolist (gnus-number gnus-newsgroup-articles)
8460       (when (funcall gnus-newsgroup-display)
8461         (push gnus-number articles)))
8462     (gnus-summary-limit articles))
8463   (gnus-summary-position-point))
8464
8465 (defun gnus-summary-limit-to-unread (&optional all)
8466   "Limit the summary buffer to articles that are not marked as read.
8467 If ALL is non-nil, limit strictly to unread articles."
8468   (interactive "P")
8469   (if all
8470       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8471     (gnus-summary-limit-to-marks
8472      ;; Concat all the marks that say that an article is read and have
8473      ;; those removed.
8474      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8475            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8476            gnus-low-score-mark gnus-expirable-mark
8477            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8478            gnus-duplicate-mark)
8479      'reverse)))
8480
8481 (defun gnus-summary-limit-to-headers (match &optional reverse)
8482   "Limit the summary buffer to articles that have headers that match MATCH.
8483 If REVERSE (the prefix), limit to articles that don't match."
8484   (interactive "sMatch headers (regexp): \nP")
8485   (gnus-summary-limit-to-bodies match reverse t))
8486
8487 (declare-function article-goto-body "gnus-art" ())
8488
8489 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8490   "Limit the summary buffer to articles that have bodies that match MATCH.
8491 If REVERSE (the prefix), limit to articles that don't match."
8492   (interactive "sMatch body (regexp): \nP")
8493   (let ((articles nil)
8494         (gnus-select-article-hook nil)  ;Disable hook.
8495         (gnus-article-prepare-hook nil)
8496         (gnus-use-article-prefetch nil)
8497         (gnus-keep-backlog nil)
8498         (gnus-break-pages nil)
8499         (gnus-summary-display-arrow nil)
8500         (gnus-updated-mode-lines nil)
8501         (gnus-auto-center-summary nil)
8502         (gnus-display-mime-function nil))
8503     (dolist (data gnus-newsgroup-data)
8504       (let (gnus-mark-article-hook)
8505         (gnus-summary-select-article t t nil (gnus-data-number data)))
8506       (with-current-buffer gnus-article-buffer
8507         (article-goto-body)
8508         (let* ((case-fold-search t)
8509                (found (if headersp
8510                           (re-search-backward match nil t)
8511                         (re-search-forward match nil t))))
8512           (when (or (and found
8513                          (not reverse))
8514                     (and (not found)
8515                          reverse))
8516             (push (gnus-data-number data) articles)))))
8517     (if (not articles)
8518         (message "No messages matched")
8519       (gnus-summary-limit articles)))
8520   (gnus-summary-position-point))
8521
8522 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8523   "Limit the summary buffer to articles that aren't part on any thread.
8524 If THREADSP (the prefix), limit to articles that are in threads."
8525   (interactive "P")
8526   (let ((articles nil)
8527         thread-articles
8528         threads)
8529     (dolist (thread gnus-newsgroup-threads)
8530       (if (stringp (car thread))
8531           (dolist (thread (cdr thread))
8532             (push thread threads))
8533         (push thread threads)))
8534     (dolist (thread threads)
8535       (setq thread-articles (gnus-articles-in-thread thread))
8536       (when (or (and threadsp
8537                      (> (length thread-articles) 1))
8538                 (and (not threadsp)
8539                      (= (length thread-articles) 1)))
8540         (setq articles (nconc thread-articles articles))))
8541     (if (not articles)
8542         (message "No messages matched")
8543       (gnus-summary-limit articles))
8544     (gnus-summary-position-point)))
8545
8546 (defun gnus-summary-limit-to-replied (&optional unreplied)
8547   "Limit the summary buffer to replied articles.
8548 If UNREPLIED (the prefix), limit to unreplied articles."
8549   (interactive "P")
8550   (if unreplied
8551       (gnus-summary-limit
8552        (gnus-set-difference gnus-newsgroup-articles
8553         gnus-newsgroup-replied))
8554     (gnus-summary-limit gnus-newsgroup-replied))
8555   (gnus-summary-position-point))
8556
8557 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8558   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8559 If REVERSE, limit the summary buffer to articles that are marked
8560 with MARKS.  MARKS can either be a string of marks or a list of marks.
8561 Returns how many articles were removed."
8562   (interactive "sMarks: ")
8563   (gnus-summary-limit-to-marks marks t))
8564
8565 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8566   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8567 If REVERSE (the prefix), limit the summary buffer to articles that are
8568 not marked with MARKS.  MARKS can either be a string of marks or a
8569 list of marks.
8570 Returns how many articles were removed."
8571   (interactive "sMarks: \nP")
8572   (prog1
8573       (let ((data gnus-newsgroup-data)
8574             (marks (if (listp marks) marks
8575                      (append marks nil))) ; Transform to list.
8576             articles)
8577         (while data
8578           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8579                   (memq (gnus-data-mark (car data)) marks))
8580             (push (gnus-data-number (car data)) articles))
8581           (setq data (cdr data)))
8582         (gnus-summary-limit articles))
8583     (gnus-summary-position-point)))
8584
8585 (defun gnus-summary-limit-to-score (score)
8586   "Limit to articles with score at or above SCORE."
8587   (interactive "NLimit to articles with score of at least: ")
8588   (let ((data gnus-newsgroup-data)
8589         articles)
8590     (while data
8591       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8592                 score)
8593         (push (gnus-data-number (car data)) articles))
8594       (setq data (cdr data)))
8595     (prog1
8596         (gnus-summary-limit articles)
8597       (gnus-summary-position-point))))
8598
8599 (defun gnus-summary-limit-to-unseen ()
8600   "Limit to unseen articles."
8601   (interactive)
8602   (prog1
8603       (gnus-summary-limit gnus-newsgroup-unseen)
8604     (gnus-summary-position-point)))
8605
8606 (defun gnus-summary-limit-include-thread (id)
8607   "Display all the hidden articles that is in the thread with ID in it.
8608 When called interactively, ID is the Message-ID of the current
8609 article."
8610   (interactive (list (mail-header-id (gnus-summary-article-header))))
8611   (let ((articles (gnus-articles-in-thread
8612                    (gnus-id-to-thread (gnus-root-id id))))
8613         ;;we REALLY want the whole thread---this prevents cut-threads
8614         ;;from removing the thread we want to include.
8615         (gnus-fetch-old-headers nil)
8616         (gnus-build-sparse-threads nil))
8617     (prog1
8618         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8619       (gnus-summary-limit-include-matching-articles
8620        "subject"
8621        (regexp-quote (gnus-simplify-subject-re
8622                       (mail-header-subject (gnus-id-to-header id)))))
8623       (gnus-summary-position-point))))
8624
8625 (defun gnus-summary-limit-include-matching-articles (header regexp)
8626   "Display all the hidden articles that have HEADERs that match REGEXP."
8627   (interactive (list (read-string "Match on header: ")
8628                      (read-string "Regexp: ")))
8629   (let ((articles (gnus-find-matching-articles header regexp)))
8630     (prog1
8631         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8632       (gnus-summary-position-point))))
8633
8634 (defun gnus-summary-insert-dormant-articles ()
8635   "Insert all the dormant articles for this group into the current buffer."
8636   (interactive)
8637   (let ((gnus-verbose (max 6 gnus-verbose)))
8638     (if (not gnus-newsgroup-dormant)
8639         (gnus-message 3 "No dormant articles for this group")
8640       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8641
8642 (defun gnus-summary-insert-ticked-articles ()
8643   "Insert ticked articles for this group into the current buffer."
8644   (interactive)
8645   (let ((gnus-verbose (max 6 gnus-verbose)))
8646     (if (not gnus-newsgroup-marked)
8647         (gnus-message 3 "No ticked articles for this group")
8648       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8649
8650 (defun gnus-summary-limit-include-dormant ()
8651   "Display all the hidden articles that are marked as dormant.
8652 Note that this command only works on a subset of the articles currently
8653 fetched for this group."
8654   (interactive)
8655   (unless gnus-newsgroup-dormant
8656     (error "There are no dormant articles in this group"))
8657   (prog1
8658       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8659     (gnus-summary-position-point)))
8660
8661 (defun gnus-summary-include-articles (articles)
8662   "Fetch the headers for ARTICLES and then display the summary lines."
8663   (let ((gnus-inhibit-demon t)
8664         (gnus-agent nil)
8665         (gnus-read-all-available-headers t))
8666     (setq gnus-newsgroup-headers
8667           (gnus-merge
8668            'list gnus-newsgroup-headers
8669            (gnus-fetch-headers articles nil t)
8670            'gnus-article-sort-by-number))
8671     (setq gnus-newsgroup-articles
8672           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8673     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8674
8675 (defun gnus-summary-limit-exclude-dormant ()
8676   "Hide all dormant articles."
8677   (interactive)
8678   (prog1
8679       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8680     (gnus-summary-position-point)))
8681
8682 (defun gnus-summary-limit-exclude-childless-dormant ()
8683   "Hide all dormant articles that have no children."
8684   (interactive)
8685   (let ((data (gnus-data-list t))
8686         articles d children)
8687     ;; Find all articles that are either not dormant or have
8688     ;; children.
8689     (while (setq d (pop data))
8690       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8691                 (and (setq children
8692                            (gnus-article-children (gnus-data-number d)))
8693                      (let (found)
8694                        (while children
8695                          (when (memq (car children) articles)
8696                            (setq children nil
8697                                  found t))
8698                          (pop children))
8699                        found)))
8700         (push (gnus-data-number d) articles)))
8701     ;; Do the limiting.
8702     (prog1
8703         (gnus-summary-limit articles)
8704       (gnus-summary-position-point))))
8705
8706 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8707   "Mark all unread excluded articles as read.
8708 If ALL, mark even excluded ticked and dormants as read."
8709   (interactive "P")
8710   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8711   (let ((articles (gnus-sorted-ndifference
8712                    (sort
8713                     (mapcar (lambda (h) (mail-header-number h))
8714                             gnus-newsgroup-headers)
8715                     '<)
8716                    gnus-newsgroup-limit))
8717         article)
8718     (setq gnus-newsgroup-unreads
8719           (gnus-sorted-intersection gnus-newsgroup-unreads
8720                                     gnus-newsgroup-limit))
8721     (if all
8722         (setq gnus-newsgroup-dormant nil
8723               gnus-newsgroup-marked nil
8724               gnus-newsgroup-reads
8725               (nconc
8726                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8727                gnus-newsgroup-reads))
8728       (while (setq article (pop articles))
8729         (unless (or (memq article gnus-newsgroup-dormant)
8730                     (memq article gnus-newsgroup-marked))
8731           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8732
8733 (defun gnus-summary-limit (articles &optional pop)
8734   (if pop
8735       ;; We pop the previous limit off the stack and use that.
8736       (setq articles (car gnus-newsgroup-limits)
8737             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8738     ;; We use the new limit, so we push the old limit on the stack.
8739     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8740   ;; Set the limit.
8741   (setq gnus-newsgroup-limit articles)
8742   (let ((total (length gnus-newsgroup-data))
8743         (data (gnus-data-find-list (gnus-summary-article-number)))
8744         (gnus-summary-mark-below nil)   ; Inhibit this.
8745         found)
8746     ;; This will do all the work of generating the new summary buffer
8747     ;; according to the new limit.
8748     (gnus-summary-prepare)
8749     ;; Hide any threads, possibly.
8750     (gnus-summary-maybe-hide-threads)
8751     ;; Try to return to the article you were at, or one in the
8752     ;; neighborhood.
8753     (when data
8754       ;; We try to find some article after the current one.
8755       (while data
8756         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8757           (setq data nil
8758                 found t))
8759         (setq data (cdr data))))
8760     (unless found
8761       ;; If there is no data, that means that we were after the last
8762       ;; article.  The same goes when we can't find any articles
8763       ;; after the current one.
8764       (goto-char (point-max))
8765       (gnus-summary-find-prev))
8766     (gnus-set-mode-line 'summary)
8767     ;; We return how many articles were removed from the summary
8768     ;; buffer as a result of the new limit.
8769     (- total (length gnus-newsgroup-data))))
8770
8771 (defsubst gnus-invisible-cut-children (threads)
8772   (let ((num 0))
8773     (while threads
8774       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8775         (incf num))
8776       (pop threads))
8777     (< num 2)))
8778
8779 (defsubst gnus-cut-thread (thread)
8780   "Go forwards in the thread until we find an article that we want to display."
8781   (when (or (eq gnus-fetch-old-headers 'some)
8782             (eq gnus-fetch-old-headers 'invisible)
8783             (numberp gnus-fetch-old-headers)
8784             (eq gnus-build-sparse-threads 'some)
8785             (eq gnus-build-sparse-threads 'more))
8786     ;; Deal with old-fetched headers and sparse threads.
8787     (while (and
8788             thread
8789             (or
8790              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8791              (gnus-summary-article-ancient-p
8792               (mail-header-number (car thread))))
8793             (if (or (<= (length (cdr thread)) 1)
8794                     (eq gnus-fetch-old-headers 'invisible))
8795                 (setq gnus-newsgroup-limit
8796                       (delq (mail-header-number (car thread))
8797                             gnus-newsgroup-limit)
8798                       thread (cadr thread))
8799               (when (gnus-invisible-cut-children (cdr thread))
8800                 (let ((th (cdr thread)))
8801                   (while th
8802                     (if (memq (mail-header-number (caar th))
8803                               gnus-newsgroup-limit)
8804                         (setq thread (car th)
8805                               th nil)
8806                       (setq th (cdr th))))))))))
8807   thread)
8808
8809 (defun gnus-cut-threads (threads)
8810   "Cut off all uninteresting articles from the beginning of THREADS."
8811   (when (or (eq gnus-fetch-old-headers 'some)
8812             (eq gnus-fetch-old-headers 'invisible)
8813             (numberp gnus-fetch-old-headers)
8814             (eq gnus-build-sparse-threads 'some)
8815             (eq gnus-build-sparse-threads 'more))
8816     (let ((th threads))
8817       (while th
8818         (setcar th (gnus-cut-thread (car th)))
8819         (setq th (cdr th)))))
8820   ;; Remove nixed out threads.
8821   (delq nil threads))
8822
8823 (defun gnus-summary-initial-limit (&optional show-if-empty)
8824   "Figure out what the initial limit is supposed to be on group entry.
8825 This entails weeding out unwanted dormants, low-scored articles,
8826 fetch-old-headers verbiage, and so on."
8827   ;; Most groups have nothing to remove.
8828   (unless (or gnus-inhibit-limiting
8829               (and (null gnus-newsgroup-dormant)
8830                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8831                    (not (eq gnus-fetch-old-headers 'some))
8832                    (not (numberp gnus-fetch-old-headers))
8833                    (not (eq gnus-fetch-old-headers 'invisible))
8834                    (null gnus-summary-expunge-below)
8835                    (not (eq gnus-build-sparse-threads 'some))
8836                    (not (eq gnus-build-sparse-threads 'more))
8837                    (null gnus-thread-expunge-below)))
8838     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8839     (setq gnus-newsgroup-limit nil)
8840     (mapatoms
8841      (lambda (node)
8842        (unless (car (symbol-value node))
8843          ;; These threads have no parents -- they are roots.
8844          (let ((nodes (cdr (symbol-value node)))
8845                thread)
8846            (while nodes
8847              (if (and gnus-thread-expunge-below
8848                       (< (gnus-thread-total-score (car nodes))
8849                          gnus-thread-expunge-below))
8850                  (gnus-expunge-thread (pop nodes))
8851                (setq thread (pop nodes))
8852                (gnus-summary-limit-children thread))))))
8853      gnus-newsgroup-dependencies)
8854     ;; If this limitation resulted in an empty group, we might
8855     ;; pop the previous limit and use it instead.
8856     (when (and (not gnus-newsgroup-limit)
8857                show-if-empty)
8858       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8859     gnus-newsgroup-limit))
8860
8861 (defun gnus-summary-limit-children (thread)
8862   "Return 1 if this subthread is visible and 0 if it is not."
8863   ;; First we get the number of visible children to this thread.  This
8864   ;; is done by recursing down the thread using this function, so this
8865   ;; will really go down to a leaf article first, before slowly
8866   ;; working its way up towards the root.
8867   (when thread
8868     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8869            (children
8870            (if (cdr thread)
8871                (apply '+ (mapcar 'gnus-summary-limit-children
8872                                  (cdr thread)))
8873              0))
8874            (number (mail-header-number (car thread)))
8875            score)
8876       (if (and
8877            (not (memq number gnus-newsgroup-marked))
8878            (or
8879             ;; If this article is dormant and has absolutely no visible
8880             ;; children, then this article isn't visible.
8881             (and (memq number gnus-newsgroup-dormant)
8882                  (zerop children))
8883             ;; If this is "fetch-old-headered" and there is no
8884             ;; visible children, then we don't want this article.
8885             (and (or (eq gnus-fetch-old-headers 'some)
8886                      (numberp gnus-fetch-old-headers))
8887                  (gnus-summary-article-ancient-p number)
8888                  (zerop children))
8889             ;; If this is "fetch-old-headered" and `invisible', then
8890             ;; we don't want this article.
8891             (and (eq gnus-fetch-old-headers 'invisible)
8892                  (gnus-summary-article-ancient-p number))
8893             ;; If this is a sparsely inserted article with no children,
8894             ;; we don't want it.
8895             (and (eq gnus-build-sparse-threads 'some)
8896                  (gnus-summary-article-sparse-p number)
8897                  (zerop children))
8898             ;; If we use expunging, and this article is really
8899             ;; low-scored, then we don't want this article.
8900             (when (and gnus-summary-expunge-below
8901                        (< (setq score
8902                                 (or (cdr (assq number gnus-newsgroup-scored))
8903                                     gnus-summary-default-score))
8904                           gnus-summary-expunge-below))
8905               ;; We increase the expunge-tally here, but that has
8906               ;; nothing to do with the limits, really.
8907               (incf gnus-newsgroup-expunged-tally)
8908               ;; We also mark as read here, if that's wanted.
8909               (when (and gnus-summary-mark-below
8910                          (< score gnus-summary-mark-below))
8911                 (setq gnus-newsgroup-unreads
8912                       (delq number gnus-newsgroup-unreads))
8913                 (if gnus-newsgroup-auto-expire
8914                     (push number gnus-newsgroup-expirable)
8915                   (push (cons number gnus-low-score-mark)
8916                         gnus-newsgroup-reads)))
8917               t)
8918             ;; Do the `display' group parameter.
8919             (and gnus-newsgroup-display
8920                  (let ((gnus-number number))
8921                    (not (funcall gnus-newsgroup-display))))))
8922           ;; Nope, invisible article.
8923           0
8924         ;; Ok, this article is to be visible, so we add it to the limit
8925         ;; and return 1.
8926         (push number gnus-newsgroup-limit)
8927         1))))
8928
8929 (defun gnus-expunge-thread (thread)
8930   "Mark all articles in THREAD as read."
8931   (let* ((number (mail-header-number (car thread))))
8932     (incf gnus-newsgroup-expunged-tally)
8933     ;; We also mark as read here, if that's wanted.
8934     (setq gnus-newsgroup-unreads
8935           (delq number gnus-newsgroup-unreads))
8936     (if gnus-newsgroup-auto-expire
8937         (push number gnus-newsgroup-expirable)
8938       (push (cons number gnus-low-score-mark)
8939             gnus-newsgroup-reads)))
8940   ;; Go recursively through all subthreads.
8941   (mapcar 'gnus-expunge-thread (cdr thread)))
8942
8943 ;; Summary article oriented commands
8944
8945 (defun gnus-summary-refer-parent-article (n)
8946   "Refer parent article N times.
8947 If N is negative, go to ancestor -N instead.
8948 The difference between N and the number of articles fetched is returned."
8949   (interactive "p")
8950   (let ((skip 1)
8951         error header ref)
8952     (when (not (natnump n))
8953       (setq skip (abs n)
8954             n 1))
8955     (while (and (> n 0)
8956                 (not error))
8957       (setq header (gnus-summary-article-header))
8958       (if (and (eq (mail-header-number header)
8959                    (cdr gnus-article-current))
8960                (equal gnus-newsgroup-name
8961                       (car gnus-article-current)))
8962           ;; If we try to find the parent of the currently
8963           ;; displayed article, then we take a look at the actual
8964           ;; References header, since this is slightly more
8965           ;; reliable than the References field we got from the
8966           ;; server.
8967           (with-current-buffer gnus-original-article-buffer
8968             (nnheader-narrow-to-headers)
8969             (unless (setq ref (message-fetch-field "references"))
8970               (when (setq ref (message-fetch-field "in-reply-to"))
8971                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8972             (widen))
8973         (setq ref
8974               ;; It's not the current article, so we take a bet on
8975               ;; the value we got from the server.
8976               (mail-header-references header)))
8977       (if (and ref
8978                (not (equal ref "")))
8979           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8980             (gnus-message 1 "Couldn't find parent"))
8981         (gnus-message 1 "No references in article %d"
8982                       (gnus-summary-article-number))
8983         (setq error t))
8984       (decf n))
8985     (gnus-summary-position-point)
8986     n))
8987
8988 (defun gnus-summary-refer-references ()
8989   "Fetch all articles mentioned in the References header.
8990 Return the number of articles fetched."
8991   (interactive)
8992   (let ((ref (mail-header-references (gnus-summary-article-header)))
8993         (current (gnus-summary-article-number))
8994         (n 0))
8995     (if (or (not ref)
8996             (equal ref ""))
8997         (error "No References in the current article")
8998       ;; For each Message-ID in the References header...
8999       (while (string-match "<[^>]*>" ref)
9000         (incf n)
9001         ;; ... fetch that article.
9002         (gnus-summary-refer-article
9003          (prog1 (match-string 0 ref)
9004            (setq ref (substring ref (match-end 0))))))
9005       (gnus-summary-goto-subject current)
9006       (gnus-summary-position-point)
9007       n)))
9008
9009 (defun gnus-delete-duplicate-headers (headers)
9010   ;; First remove leading duplicates.
9011   (while (and (> (length headers) 1)
9012               (= (mail-header-number (car headers))
9013                  (mail-header-number (cadr headers))))
9014     (pop headers))
9015   ;; Then the rest.
9016   (let ((result headers))
9017     (while (> (length headers) 1)
9018       (if (= (mail-header-number (car headers))
9019              (mail-header-number (cadr headers)))
9020           (setcdr headers (cddr headers))
9021         (pop headers)))
9022     result))
9023
9024 (defun gnus-summary-refer-thread (&optional limit)
9025   "Fetch all articles in the current thread. For backends that
9026 know how to search for threads (currently only 'nnimap) a
9027 non-numeric prefix arg will use nnir to search the entire
9028 server; without a prefix arg only the current group is
9029 searched. If the variable `gnus-refer-thread-use-nnir' is
9030 non-nil the prefix arg has the reverse meaning. If no
9031 backend-specific 'request-thread function is available fetch
9032 LIMIT (the numerical prefix) old headers. If LIMIT is
9033 non-numeric or nil fetch the number specified by the
9034 `gnus-refer-thread-limit' variable."
9035   (interactive "P")
9036   (gnus-warp-to-article)
9037   (let* ((header (gnus-summary-article-header))
9038          (id (mail-header-id header))
9039          (gnus-inhibit-demon t)
9040          (gnus-summary-ignore-duplicates t)
9041          (gnus-read-all-available-headers t)
9042          (gnus-refer-thread-use-nnir
9043           (if (and (not (null limit)) (listp limit))
9044               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9045          (new-headers
9046           (if (gnus-check-backend-function
9047                'request-thread gnus-newsgroup-name)
9048               (gnus-request-thread header gnus-newsgroup-name)
9049             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9050                             gnus-refer-thread-limit))
9051                    (last (if (numberp limit)
9052                              (min (+ (mail-header-number header)
9053                                      limit)
9054                                   gnus-newsgroup-highest)
9055                            gnus-newsgroup-highest))
9056                    (subject (gnus-simplify-subject
9057                              (mail-header-subject header)))
9058                    (refs (split-string (or (mail-header-references header)
9059                                            "")))
9060                    (gnus-parse-headers-hook
9061                     `(lambda () (goto-char (point-min))
9062                       (keep-lines
9063                        (regexp-opt ',(append refs (list id subject)))))))
9064               (gnus-fetch-headers (list last) (if (numberp limit)
9065                                                   (* 2 limit) limit) t))))
9066          article-ids)
9067     (when (listp new-headers)
9068       (dolist (header new-headers)
9069         (push (mail-header-number header) article-ids)
9070         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9071           (push (mail-header-number header) gnus-newsgroup-unreads)
9072           (setq gnus-newsgroup-unselected
9073                 (delete (mail-header-number header)
9074                         gnus-newsgroup-unselected))))
9075       (setq gnus-newsgroup-headers
9076             (gnus-delete-duplicate-headers
9077              (gnus-merge
9078               'list gnus-newsgroup-headers new-headers
9079               'gnus-article-sort-by-number)))
9080       (setq gnus-newsgroup-articles
9081             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9082       (gnus-summary-limit-include-thread id)))
9083   (gnus-summary-show-thread))
9084
9085 (defun gnus-summary-refer-article (message-id)
9086   "Fetch an article specified by MESSAGE-ID."
9087   (interactive "sMessage-ID: ")
9088   (gnus-warp-to-article)
9089   (when (and (stringp message-id)
9090              (not (zerop (length message-id))))
9091     (setq message-id (gnus-replace-in-string message-id " " ""))
9092     ;; Construct the correct Message-ID if necessary.
9093     ;; Suggested by tale@pawl.rpi.edu.
9094     (unless (string-match "^<" message-id)
9095       (setq message-id (concat "<" message-id)))
9096     (unless (string-match ">$" message-id)
9097       (setq message-id (concat message-id ">")))
9098     ;; People often post MIDs from URLs, so unhex it:
9099     (unless (string-match "@" message-id)
9100       (setq message-id (gnus-url-unhex-string message-id)))
9101     (let* ((header (gnus-id-to-header message-id))
9102            (sparse (and header
9103                         (gnus-summary-article-sparse-p
9104                          (mail-header-number header))
9105                         (memq (mail-header-number header)
9106                               gnus-newsgroup-limit)))
9107            number)
9108       (cond
9109        ;; If the article is present in the buffer we just go to it.
9110        ((and header
9111              (or (not (gnus-summary-article-sparse-p
9112                        (mail-header-number header)))
9113                  sparse))
9114         (prog1
9115             (gnus-summary-goto-article
9116              (mail-header-number header) nil t)
9117           (when sparse
9118             (gnus-summary-update-article (mail-header-number header)))))
9119        (t
9120         ;; We fetch the article.
9121         (catch 'found
9122           (dolist (gnus-override-method (gnus-refer-article-methods))
9123             (when (and (gnus-check-server gnus-override-method)
9124                        ;; Fetch the header,
9125                        (setq number (gnus-summary-insert-subject message-id)))
9126               ;; and display the article.
9127               (gnus-summary-select-article nil nil nil number)
9128               (throw 'found t)))
9129           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9130
9131 (defun gnus-refer-article-methods ()
9132   "Return a list of referable methods."
9133   (cond
9134    ;; No method, so we default to current and native.
9135    ((null gnus-refer-article-method)
9136     (list gnus-current-select-method gnus-select-method))
9137    ;; Current.
9138    ((eq 'current gnus-refer-article-method)
9139     (list gnus-current-select-method))
9140    ;; List of select methods.
9141    ((not (and (symbolp (car gnus-refer-article-method))
9142               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9143     (let (out)
9144       (dolist (method gnus-refer-article-method)
9145         (push (if (eq 'current method)
9146                   gnus-current-select-method
9147                 (if (eq 'nnir (car method))
9148                     (list
9149                      'nnir
9150                      (or (cadr method)
9151                          (gnus-method-to-server gnus-current-select-method)))
9152                   method))
9153               out))
9154       (nreverse out)))
9155    ;; One single select method.
9156    (t
9157     (list gnus-refer-article-method))))
9158
9159 (defun gnus-summary-edit-parameters ()
9160   "Edit the group parameters of the current group."
9161   (interactive)
9162   (gnus-group-edit-group gnus-newsgroup-name 'params))
9163
9164 (defun gnus-summary-customize-parameters ()
9165   "Customize the group parameters of the current group."
9166   (interactive)
9167   (gnus-group-customize gnus-newsgroup-name))
9168
9169 (defun gnus-summary-enter-digest-group (&optional force)
9170   "Enter an nndoc group based on the current article.
9171 If FORCE, force a digest interpretation.  If not, try to guess
9172 what the document format is.
9173
9174 To control what happens when you exit the group, see the
9175 `gnus-auto-select-on-ephemeral-exit' variable."
9176   (interactive "P")
9177   (let ((conf gnus-current-window-configuration))
9178     (save-window-excursion
9179       (save-excursion
9180         (let (gnus-article-prepare-hook
9181               gnus-display-mime-function
9182               gnus-break-pages)
9183           (gnus-summary-select-article))))
9184     (setq gnus-current-window-configuration conf)
9185     (let* ((name (format "%s-%d"
9186                          (gnus-group-prefixed-name
9187                           gnus-newsgroup-name (list 'nndoc ""))
9188                          (with-current-buffer gnus-summary-buffer
9189                            gnus-current-article)))
9190            (ogroup gnus-newsgroup-name)
9191            (params (append (gnus-info-params (gnus-get-info ogroup))
9192                            (list (cons 'to-group ogroup))
9193                            (list (cons 'parent-group ogroup))
9194                            (list (cons 'save-article-group ogroup))))
9195            (case-fold-search t)
9196            (buf (current-buffer))
9197            dig to-address charset)
9198       (with-current-buffer gnus-original-article-buffer
9199         ;; Have the digest group inherit the main mail address of
9200         ;; the parent article.
9201         (when (setq to-address (or (gnus-fetch-field "reply-to")
9202                                    (gnus-fetch-field "from")))
9203           (setq params
9204                 (append
9205                  params
9206                  (list (cons 'to-address
9207                              (funcall gnus-decode-encoded-address-function
9208                                       to-address))))))
9209         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9210         (insert-buffer-substring gnus-original-article-buffer)
9211         (narrow-to-region
9212          (goto-char (point-min))
9213          (or (search-forward "\n\n" nil t) (point)))
9214         ;; Remove lines that may lead nndoc to misinterpret the
9215         ;; document type.
9216         (goto-char (point-min))
9217         (delete-matching-lines "^Path:\\|^From ")
9218         ;; Parse charset, and decode content transfer encoding.
9219         (setq charset (mail-content-type-get
9220                        (mail-header-parse-content-type
9221                         (or (gnus-fetch-field "content-type") ""))
9222                        'charset))
9223         (let ((encoding (gnus-fetch-field "content-transfer-encoding")))
9224           (when encoding
9225             (message-remove-header "content-transfer-encoding")
9226             (goto-char (point-max))
9227             (widen)
9228             (narrow-to-region (point) (point-max))
9229             (mm-decode-content-transfer-encoding
9230              (intern (downcase (mail-header-strip encoding))))))
9231         (widen))
9232       (unwind-protect
9233           (if (let ((gnus-newsgroup-ephemeral-charset
9234                      (if charset
9235                          (intern (downcase (gnus-strip-whitespace charset)))
9236                        gnus-newsgroup-charset))
9237                     (gnus-newsgroup-ephemeral-ignored-charsets
9238                      gnus-newsgroup-ignored-charsets))
9239                 (gnus-group-read-ephemeral-group
9240                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9241                               (nndoc-article-type
9242                                ,(if force 'mbox 'guess)))
9243                  t nil nil nil
9244                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9245                                                         "ADAPT")))))
9246               ;; Make all postings to this group go to the parent group.
9247               (nconc (gnus-info-params (gnus-get-info name))
9248                      params)
9249             ;; Couldn't select this doc group.
9250             (switch-to-buffer buf)
9251             (gnus-set-global-variables)
9252             (gnus-configure-windows 'summary)
9253             (gnus-message 3 "Article couldn't be entered?"))
9254         (kill-buffer dig)))))
9255
9256 (defun gnus-summary-read-document (n)
9257   "Open a new group based on the current article(s).
9258 This will allow you to read digests and other similar
9259 documents as newsgroups.
9260 Obeys the standard process/prefix convention."
9261   (interactive "P")
9262   (let* ((ogroup gnus-newsgroup-name)
9263          (params (append (gnus-info-params (gnus-get-info ogroup))
9264                          (list (cons 'to-group ogroup))))
9265          group egroup groups vgroup)
9266     (dolist (article (gnus-summary-work-articles n))
9267       (setq group (format "%s-%d" gnus-newsgroup-name article))
9268       (gnus-summary-remove-process-mark article)
9269       (when (gnus-summary-display-article article)
9270         (save-excursion ;;What for?
9271           (with-temp-buffer
9272             (insert-buffer-substring gnus-original-article-buffer)
9273             ;; Remove some headers that may lead nndoc to make
9274             ;; the wrong guess.
9275             (message-narrow-to-head)
9276             (goto-char (point-min))
9277             (delete-matching-lines "^Path:\\|^From ")
9278             (widen)
9279             (if (setq egroup
9280                       (gnus-group-read-ephemeral-group
9281                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9282                                      (nndoc-article-type guess))
9283                        t nil t))
9284                 (progn
9285                   ;; Make all postings to this group go to the parent group.
9286                   (nconc (gnus-info-params (gnus-get-info egroup))
9287                          params)
9288                   (push egroup groups))
9289               ;; Couldn't select this doc group.
9290               (gnus-error 3 "Article couldn't be entered"))))))
9291     ;; Now we have selected all the documents.
9292     (cond
9293      ((not groups)
9294       (error "None of the articles could be interpreted as documents"))
9295      ((gnus-group-read-ephemeral-group
9296        (setq vgroup (format
9297                      "nnvirtual:%s-%s" gnus-newsgroup-name
9298                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9299        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9300        t
9301        (cons (current-buffer) 'summary)))
9302      (t
9303       (error "Couldn't select virtual nndoc group")))))
9304
9305 (defun gnus-summary-widget-forward (arg)
9306   "Move point to the next field or button in the article.
9307 With optional ARG, move across that many fields."
9308   (interactive "p")
9309   (gnus-summary-select-article)
9310   (gnus-configure-windows 'article)
9311   (select-window (gnus-get-buffer-window gnus-article-buffer))
9312   (widget-forward arg))
9313
9314 (defun gnus-summary-widget-backward (arg)
9315   "Move point to the previous field or button in the article.
9316 With optional ARG, move across that many fields."
9317   (interactive "p")
9318   (gnus-summary-select-article)
9319   (gnus-configure-windows 'article)
9320   (select-window (gnus-get-buffer-window gnus-article-buffer))
9321   (unless (widget-at (point))
9322     (goto-char (point-max)))
9323   (widget-backward arg))
9324
9325 (defun gnus-summary-isearch-article (&optional regexp-p)
9326   "Do incremental search forward on the current article.
9327 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9328   (interactive "P")
9329   (gnus-summary-select-article)
9330   (gnus-configure-windows 'article)
9331   (gnus-eval-in-buffer-window gnus-article-buffer
9332     (save-restriction
9333       (widen)
9334       (isearch-forward regexp-p))))
9335
9336 (defun gnus-summary-repeat-search-article-forward ()
9337   "Repeat the previous search forwards."
9338   (interactive)
9339   (unless gnus-last-search-regexp
9340     (error "No previous search"))
9341   (gnus-summary-search-article-forward gnus-last-search-regexp))
9342
9343 (defun gnus-summary-repeat-search-article-backward ()
9344   "Repeat the previous search backwards."
9345   (interactive)
9346   (unless gnus-last-search-regexp
9347     (error "No previous search"))
9348   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9349
9350 (defun gnus-summary-search-article-forward (regexp &optional backward)
9351   "Search for an article containing REGEXP forward.
9352 If BACKWARD, search backward instead."
9353   (interactive
9354    (list (read-string
9355           (format "Search article %s (regexp%s): "
9356                   (if current-prefix-arg "backward" "forward")
9357                   (if gnus-last-search-regexp
9358                       (concat ", default " gnus-last-search-regexp)
9359                     "")))
9360          current-prefix-arg))
9361   (if (string-equal regexp "")
9362       (setq regexp (or gnus-last-search-regexp ""))
9363     (setq gnus-last-search-regexp regexp)
9364     (setq gnus-article-before-search gnus-current-article))
9365   ;; Intentionally set gnus-last-article.
9366   (setq gnus-last-article gnus-article-before-search)
9367   (let ((gnus-last-article gnus-last-article))
9368     (if (gnus-summary-search-article regexp backward)
9369         (gnus-summary-show-thread)
9370       (signal 'search-failed (list regexp)))))
9371
9372 (defun gnus-summary-search-article-backward (regexp)
9373   "Search for an article containing REGEXP backward."
9374   (interactive
9375    (list (read-string
9376           (format "Search article backward (regexp%s): "
9377                   (if gnus-last-search-regexp
9378                       (concat ", default " gnus-last-search-regexp)
9379                     "")))))
9380   (gnus-summary-search-article-forward regexp 'backward))
9381
9382 (defun gnus-summary-search-article (regexp &optional backward)
9383   "Search for an article containing REGEXP.
9384 Optional argument BACKWARD means do search for backward.
9385 `gnus-select-article-hook' is not called during the search."
9386   ;; We have to require this here to make sure that the following
9387   ;; dynamic binding isn't shadowed by autoloading.
9388   (require 'gnus-async)
9389   (require 'gnus-art)
9390   (let ((gnus-select-article-hook nil)  ;Disable hook.
9391         (gnus-article-prepare-hook nil)
9392         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9393         (gnus-use-article-prefetch nil)
9394         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9395         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9396         (gnus-visual nil)
9397         (gnus-keep-backlog nil)
9398         (gnus-break-pages nil)
9399         (gnus-summary-display-arrow nil)
9400         (gnus-updated-mode-lines nil)
9401         (gnus-auto-center-summary nil)
9402         (sum (current-buffer))
9403         (gnus-display-mime-function nil)
9404         (found nil)
9405         point)
9406     (gnus-save-hidden-threads
9407       (gnus-summary-select-article)
9408       (set-buffer gnus-article-buffer)
9409       (goto-char (window-point (get-buffer-window (current-buffer))))
9410       (when backward
9411         (forward-line -1))
9412       (while (not found)
9413         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9414         (if (if backward
9415                 (re-search-backward regexp nil t)
9416               (re-search-forward regexp nil t))
9417             ;; We found the regexp.
9418             (progn
9419               (setq found 'found)
9420               (beginning-of-line)
9421               (set-window-start
9422                (get-buffer-window (current-buffer))
9423                (point))
9424               (forward-line 1)
9425               (set-window-point
9426                (get-buffer-window (current-buffer))
9427                (point))
9428               (set-buffer sum)
9429               (setq point (point)))
9430           ;; We didn't find it, so we go to the next article.
9431           (set-buffer sum)
9432           (setq found 'not)
9433           (while (eq found 'not)
9434             (if (not (if backward (gnus-summary-find-prev)
9435                        (gnus-summary-find-next)))
9436                 ;; No more articles.
9437                 (setq found t)
9438               ;; Select the next article and adjust point.
9439               (unless (gnus-summary-article-sparse-p
9440                        (gnus-summary-article-number))
9441                 (setq found nil)
9442                 (gnus-summary-select-article)
9443                 (set-buffer gnus-article-buffer)
9444                 (widen)
9445                 (goto-char (if backward (point-max) (point-min))))))))
9446       (gnus-message 7 ""))
9447     ;; Return whether we found the regexp.
9448     (when (eq found 'found)
9449       (goto-char point)
9450       (gnus-summary-show-thread)
9451       (gnus-summary-goto-subject gnus-current-article)
9452       (gnus-summary-position-point)
9453       t)))
9454
9455 (defun gnus-find-matching-articles (header regexp)
9456   "Return a list of all articles that match REGEXP on HEADER.
9457 This search includes all articles in the current group that Gnus has
9458 fetched headers for, whether they are displayed or not."
9459   (let ((articles nil)
9460         ;; Can't eta-reduce because it's a macro.
9461         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9462         (case-fold-search t))
9463     (dolist (header gnus-newsgroup-headers)
9464       (when (string-match regexp (funcall func header))
9465         (push (mail-header-number header) articles)))
9466     (nreverse articles)))
9467
9468 (defun gnus-summary-find-matching (header regexp &optional backward unread
9469                                           not-case-fold not-matching)
9470   "Return a list of all articles that match REGEXP on HEADER.
9471 The search stars on the current article and goes forwards unless
9472 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9473 If UNREAD is non-nil, only unread articles will
9474 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9475 in the comparisons. If NOT-MATCHING, return a list of all articles that
9476 not match REGEXP on HEADER."
9477   (let ((case-fold-search (not not-case-fold))
9478         articles d func)
9479     (if (consp header)
9480         (if (eq (car header) 'extra)
9481             (setq func
9482                   `(lambda (h)
9483                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9484                          "")))
9485           (error "%s is an invalid header" header))
9486       (unless (fboundp (intern (concat "mail-header-" header)))
9487         (error "%s is not a valid header" header))
9488       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9489     (dolist (d (if (eq backward 'all)
9490                    gnus-newsgroup-data
9491                  (gnus-data-find-list
9492                   (gnus-summary-article-number)
9493                   (gnus-data-list backward))))
9494       (when (and (or (not unread)       ; We want all articles...
9495                      (gnus-data-unread-p d)) ; Or just unreads.
9496                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9497                  (if not-matching
9498                      (not (string-match
9499                            regexp
9500                            (funcall func (gnus-data-header d))))
9501                    (string-match regexp
9502                                  (funcall func (gnus-data-header d)))))
9503         (push (gnus-data-number d) articles))) ; Success!
9504     (nreverse articles)))
9505
9506 (defun gnus-summary-execute-command (header regexp command &optional backward)
9507   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9508 If HEADER is an empty string (or nil), the match is done on the entire
9509 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9510   (interactive
9511    (list (let ((completion-ignore-case t))
9512            (gnus-completing-read
9513             "Header name"
9514             (mapcar 'symbol-name
9515                     (append
9516                      '(Number Subject From Lines Date
9517                        Message-ID Xref References Body)
9518                      gnus-extra-headers))
9519             'require-match))
9520          (read-string "Regexp: ")
9521          (read-key-sequence "Command: ")
9522          current-prefix-arg))
9523   (when (equal header "Body")
9524     (setq header ""))
9525   ;; Hidden thread subtrees must be searched as well.
9526   (gnus-summary-show-all-threads)
9527   ;; We don't want to change current point nor window configuration.
9528   (save-excursion
9529     (save-window-excursion
9530       (let (gnus-visual
9531             gnus-treat-strip-trailing-blank-lines
9532             gnus-treat-strip-leading-blank-lines
9533             gnus-treat-strip-multiple-blank-lines
9534             gnus-treat-hide-boring-headers
9535             gnus-treat-fold-newsgroups
9536             gnus-article-prepare-hook)
9537         (gnus-message 6 "Executing %s..." (key-description command))
9538         ;; We'd like to execute COMMAND interactively so as to give arguments.
9539         (gnus-execute header regexp
9540                       `(call-interactively ',(key-binding command))
9541                       backward)
9542         (gnus-message 6 "Executing %s...done" (key-description command))))))
9543
9544 (defun gnus-summary-beginning-of-article ()
9545   "Scroll the article back to the beginning."
9546   (interactive)
9547   (gnus-summary-select-article)
9548   (gnus-configure-windows 'article)
9549   (gnus-eval-in-buffer-window gnus-article-buffer
9550     (widen)
9551     (goto-char (point-min))
9552     (when gnus-break-pages
9553       (gnus-narrow-to-page))))
9554
9555 (defun gnus-summary-end-of-article ()
9556   "Scroll to the end of the article."
9557   (interactive)
9558   (gnus-summary-select-article)
9559   (gnus-configure-windows 'article)
9560   (gnus-eval-in-buffer-window gnus-article-buffer
9561     (widen)
9562     (goto-char (point-max))
9563     (recenter -3)
9564     (when gnus-break-pages
9565       (gnus-narrow-to-page))))
9566
9567 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9568   "Truncate to LEN and quote all \"(\"'s in STRING."
9569   (gnus-replace-in-string (if (and len (> (length string) len))
9570                               (substring string 0 len)
9571                             string)
9572                           "[()]" "\\\\\\&"))
9573
9574 (defun gnus-summary-print-article (&optional filename n)
9575   "Generate and print a PostScript image of the process-marked (mail) articles.
9576
9577 If used interactively, print the current article if none are
9578 process-marked.  With prefix arg, prompt the user for the name of the
9579 file to save in.
9580
9581 When used from Lisp, accept two optional args FILENAME and N.  N means
9582 to print the next N articles.  If N is negative, print the N previous
9583 articles.  If N is nil and articles have been marked with the process
9584 mark, print these instead.
9585
9586 If the optional first argument FILENAME is nil, send the image to the
9587 printer.  If FILENAME is a string, save the PostScript image in a file with
9588 that name.  If FILENAME is a number, prompt the user for the name of the file
9589 to save in."
9590   (interactive (list (ps-print-preprint current-prefix-arg)))
9591   (dolist (article (gnus-summary-work-articles n))
9592     (gnus-summary-select-article nil nil 'pseudo article)
9593     (gnus-eval-in-buffer-window gnus-article-buffer
9594       (gnus-print-buffer))
9595     (gnus-summary-remove-process-mark article))
9596   (ps-despool filename))
9597
9598 (defun gnus-print-buffer ()
9599   (let ((ps-left-header
9600          (list
9601           (concat "("
9602                   (gnus-summary-print-truncate-and-quote
9603                    (mail-header-subject gnus-current-headers)
9604                    66) ")")
9605           (concat "("
9606                   (gnus-summary-print-truncate-and-quote
9607                    (mail-header-from gnus-current-headers)
9608                    45) ")")))
9609         (ps-right-header
9610          (list
9611           "/pagenumberstring load"
9612           (concat "("
9613                   (mail-header-date gnus-current-headers) ")"))))
9614     (gnus-run-hooks 'gnus-ps-print-hook)
9615     (save-excursion
9616       (if ps-print-color-p
9617           (ps-spool-buffer-with-faces)
9618         (ps-spool-buffer)))))
9619
9620 (declare-function gnus-flush-original-article-buffer "gnus-art" ())
9621
9622 (defun gnus-summary-show-complete-article ()
9623   "Show a complete version of the current article.
9624 This is only useful if you're looking at a partial version of the
9625 article currently."
9626   (interactive)
9627   (let ((gnus-keep-backlog nil)
9628         (gnus-use-cache nil)
9629         (gnus-agent nil)
9630         (variable (intern
9631                    (format "%s-fetch-partial-articles"
9632                            (car (gnus-find-method-for-group
9633                                  gnus-newsgroup-name)))
9634                    obarray))
9635         old-val)
9636     (unwind-protect
9637         (progn
9638           (setq old-val (symbol-value variable))
9639           (set variable nil)
9640           (gnus-flush-original-article-buffer)
9641           (gnus-summary-show-article))
9642       (set variable old-val))))
9643
9644 (defun gnus-summary-show-article (&optional arg)
9645   "Force redisplaying of the current article.
9646 If ARG (the prefix) is a number, show the article with the charset
9647 defined in `gnus-summary-show-article-charset-alist', or the charset
9648 input.
9649 If ARG (the prefix) is non-nil and not a number, show the article,
9650 but without running any of the article treatment functions
9651 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9652 C-u g', show the raw article."
9653   (interactive "P")
9654   (cond
9655    ((numberp arg)
9656     (gnus-summary-show-article t)
9657     (let ((gnus-newsgroup-charset
9658            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9659                (mm-read-coding-system
9660                 "View as charset: " ;; actually it is coding system.
9661                 (with-current-buffer gnus-article-buffer
9662                   (mm-detect-coding-region (point) (point-max))))))
9663           (gnus-newsgroup-ignored-charsets 'gnus-all))
9664       (gnus-summary-select-article nil 'force)
9665       (let ((deps gnus-newsgroup-dependencies)
9666             head header lines)
9667         (with-current-buffer gnus-original-article-buffer
9668           (save-restriction
9669             (message-narrow-to-head)
9670             (setq head (buffer-string))
9671             (goto-char (point-min))
9672             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9673               (goto-char (point-max))
9674               (widen)
9675               (setq lines (1- (count-lines (point) (point-max))))))
9676           (with-temp-buffer
9677             (insert (format "211 %d Article retrieved.\n"
9678                             (cdr gnus-article-current)))
9679             (insert head)
9680             (if lines (insert (format "Lines: %d\n" lines)))
9681             (insert ".\n")
9682             (let ((nntp-server-buffer (current-buffer)))
9683               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9684         (gnus-data-set-header
9685          (gnus-data-find (cdr gnus-article-current))
9686          header)
9687         (gnus-summary-update-article-line
9688          (cdr gnus-article-current) header)
9689         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9690           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9691    ((not arg)
9692     ;; Select the article the normal way.
9693     (if (eq mm-text-html-renderer 'shr)
9694         (progn
9695           (require 'shr)
9696           (let ((shr-ignore-cache t))
9697             (gnus-summary-select-article nil 'force)))
9698       (gnus-summary-select-article nil 'force)))
9699    ((equal arg '(16))
9700     ;; C-u C-u g
9701     (let ((gnus-inhibit-article-treatments t))
9702       (gnus-summary-select-article nil 'force)))
9703    (t
9704     ;; We have to require this here to make sure that the following
9705     ;; dynamic binding isn't shadowed by autoloading.
9706     (require 'gnus-async)
9707     (require 'gnus-art)
9708     ;; Bind the article treatment functions to nil.
9709     (let ((gnus-have-all-headers t)
9710           gnus-article-prepare-hook
9711           gnus-article-decode-hook
9712           gnus-display-mime-function
9713           gnus-break-pages)
9714       ;; Destroy any MIME parts.
9715       (when (gnus-buffer-live-p gnus-article-buffer)
9716         (with-current-buffer gnus-article-buffer
9717           (gnus-article-stop-animations)
9718           (gnus-stop-downloads)
9719           (mm-destroy-parts gnus-article-mime-handles)
9720           ;; Set it to nil for safety reason.
9721           (setq gnus-article-mime-handle-alist nil)
9722           (setq gnus-article-mime-handles nil)))
9723       (gnus-summary-select-article nil 'force))))
9724   (gnus-summary-goto-subject gnus-current-article)
9725   (gnus-summary-position-point))
9726
9727 (defun gnus-summary-show-raw-article ()
9728   "Show the raw article without any article massaging functions being run."
9729   (interactive)
9730   (gnus-summary-show-article t))
9731
9732 (defun gnus-summary-verbose-headers (&optional arg)
9733   "Toggle permanent full header display.
9734 If ARG is a positive number, turn header display on.
9735 If ARG is a negative number, turn header display off."
9736   (interactive "P")
9737   (setq gnus-show-all-headers
9738         (cond ((or (not (numberp arg))
9739                    (zerop arg))
9740                (not gnus-show-all-headers))
9741               ((natnump arg)
9742                t)))
9743   (gnus-summary-show-article))
9744
9745 (declare-function article-narrow-to-head "gnus-art" ())
9746 (declare-function gnus-article-hidden-text-p "gnus-art" (type))
9747 (declare-function gnus-delete-wash-type "gnus-art" (type))
9748
9749 (defun gnus-summary-toggle-header (&optional arg)
9750   "Show the headers if they are hidden, or hide them if they are shown.
9751 If ARG is a positive number, show the entire header.
9752 If ARG is a negative number, hide the unwanted header lines."
9753   (interactive "P")
9754   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9755                      (get-buffer-window gnus-article-buffer t))))
9756     (with-current-buffer gnus-article-buffer
9757       (widen)
9758       (article-narrow-to-head)
9759       (let* ((inhibit-read-only t)
9760              (inhibit-point-motion-hooks t)
9761              (hidden (if (numberp arg)
9762                          (>= arg 0)
9763                        (or (not (looking-at "[^ \t\n]+:"))
9764                            (gnus-article-hidden-text-p 'headers))))
9765              s e)
9766         (delete-region (point-min) (point-max))
9767         (with-current-buffer gnus-original-article-buffer
9768           (goto-char (setq s (point-min)))
9769           (setq e (if (search-forward "\n\n" nil t)
9770                       (1- (point))
9771                     (point-max))))
9772         (insert-buffer-substring gnus-original-article-buffer s e)
9773         (run-hooks 'gnus-article-decode-hook)
9774         (if hidden
9775             (let ((gnus-treat-hide-headers nil)
9776                   (gnus-treat-hide-boring-headers nil))
9777               (gnus-delete-wash-type 'headers)
9778               (gnus-treat-article 'head))
9779           (gnus-treat-article 'head))
9780         (widen)
9781         (if window
9782             (set-window-start window (goto-char (point-min))))
9783         (if gnus-break-pages
9784             (gnus-narrow-to-page)
9785           (when (gnus-visual-p 'page-marker)
9786             (let ((inhibit-read-only t))
9787               (gnus-remove-text-with-property 'gnus-prev)
9788               (gnus-remove-text-with-property 'gnus-next))))
9789         (gnus-set-mode-line 'article)))))
9790
9791 (defun gnus-summary-show-all-headers ()
9792   "Make all header lines visible."
9793   (interactive)
9794   (gnus-summary-toggle-header 1))
9795
9796 (defun gnus-summary-caesar-message (&optional arg)
9797   "Caesar rotate the current article by 13.
9798 With a non-numerical prefix, also rotate headers.  A numerical
9799 prefix specifies how many places to rotate each letter forward."
9800   (interactive "P")
9801   (gnus-summary-select-article)
9802   (let ((mail-header-separator ""))
9803     (gnus-eval-in-buffer-window gnus-article-buffer
9804       (save-restriction
9805         (widen)
9806         (let ((start (window-start))
9807               (inhibit-read-only t))
9808           (if (equal arg '(4))
9809               (message-caesar-buffer-body nil t)
9810             (message-caesar-buffer-body arg))
9811           (set-window-start (get-buffer-window (current-buffer)) start)))))
9812   ;; Create buttons and stuff...
9813   (gnus-treat-article nil))
9814
9815 (declare-function idna-to-unicode "ext:idna" (str))
9816
9817 (defun gnus-summary-idna-message (&optional arg)
9818   "Decode IDNA encoded domain names in the current articles.
9819 IDNA encoded domain names looks like `xn--bar'.  If a string
9820 remain unencoded after running this function, it is likely an
9821 invalid IDNA string (`xn--bar' is invalid).
9822
9823 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9824 installed for this command to work."
9825   (interactive "P")
9826   (if (not (and (condition-case nil (require 'idna)
9827                   (file-error))
9828                 (mm-coding-system-p 'utf-8)
9829                 (executable-find (symbol-value 'idna-program))))
9830       (gnus-message
9831        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9832     (gnus-summary-select-article)
9833     (let ((mail-header-separator ""))
9834       (gnus-eval-in-buffer-window gnus-article-buffer
9835         (save-restriction
9836           (widen)
9837           (let ((start (window-start))
9838                 buffer-read-only)
9839             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9840               (replace-match (idna-to-unicode (match-string 1))))
9841             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9842
9843 (defun gnus-summary-morse-message (&optional arg)
9844   "Morse decode the current article."
9845   (interactive "P")
9846   (gnus-summary-select-article)
9847   (let ((mail-header-separator ""))
9848     (gnus-eval-in-buffer-window gnus-article-buffer
9849       (save-excursion
9850         (save-restriction
9851           (widen)
9852           (let ((pos (window-start))
9853                 (inhibit-read-only t))
9854             (goto-char (point-min))
9855             (when (message-goto-body)
9856               (gnus-narrow-to-body))
9857             (goto-char (point-min))
9858             (while (search-forward "·" (point-max) t)
9859               (replace-match "."))
9860             (unmorse-region (point-min) (point-max))
9861             (widen)
9862             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9863
9864 (defun gnus-summary-stop-page-breaking ()
9865   "Stop page breaking in the current article."
9866   (interactive)
9867   (gnus-summary-select-article)
9868   (gnus-eval-in-buffer-window gnus-article-buffer
9869     (widen)
9870     (when (gnus-visual-p 'page-marker)
9871       (let ((inhibit-read-only t))
9872         (gnus-remove-text-with-property 'gnus-prev)
9873         (gnus-remove-text-with-property 'gnus-next))
9874       (setq gnus-page-broken nil))))
9875
9876 (defun gnus-summary-move-article (&optional n to-newsgroup
9877                                             select-method action)
9878   "Move the current article to a different newsgroup.
9879 If N is a positive number, move the N next articles.
9880 If N is a negative number, move the N previous articles.
9881 If N is nil and any articles have been marked with the process mark,
9882 move those articles instead.
9883 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9884 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9885 re-spool using this method.
9886
9887 When called interactively with TO-NEWSGROUP being nil, the value of
9888 the variable `gnus-move-split-methods' is used for finding a default
9889 for the target newsgroup.
9890
9891 For this function to work, both the current newsgroup and the
9892 newsgroup that you want to move to have to support the `request-move'
9893 and `request-accept' functions.
9894
9895 ACTION can be either `move' (the default), `crosspost' or `copy'."
9896   (interactive "P")
9897   (unless action
9898     (setq action 'move))
9899   ;; Check whether the source group supports the required functions.
9900   (cond ((and (eq action 'move)
9901               (not (gnus-check-backend-function
9902                     'request-move-article gnus-newsgroup-name)))
9903          (error "The current group does not support article moving"))
9904         ((and (eq action 'crosspost)
9905               (not (gnus-check-backend-function
9906                     'request-replace-article gnus-newsgroup-name)))
9907          (error "The current group does not support article editing")))
9908   (let ((articles (gnus-summary-work-articles n))
9909         (prefix (if (gnus-check-backend-function
9910                      'request-move-article gnus-newsgroup-name)
9911                     (funcall gnus-move-group-prefix-function
9912                              gnus-newsgroup-name)
9913                   ""))
9914         (names '((move "Move" "Moving")
9915                  (copy "Copy" "Copying")
9916                  (crosspost "Crosspost" "Crossposting")))
9917         (copy-buf (save-excursion
9918                     (nnheader-set-temp-buffer " *copy article*")))
9919         art-group to-method new-xref article to-groups
9920         articles-to-update-marks encoded)
9921     (unless (assq action names)
9922       (error "Unknown action %s" action))
9923     ;; Read the newsgroup name.
9924     (when (and (not to-newsgroup)
9925                (not select-method))
9926       (if (and gnus-move-split-methods
9927                (not
9928                 (and (memq gnus-current-article articles)
9929                      (gnus-buffer-live-p gnus-original-article-buffer))))
9930           ;; When `gnus-move-split-methods' is non-nil, we have to
9931           ;; select an article to give `gnus-read-move-group-name' an
9932           ;; opportunity to suggest an appropriate default.  However,
9933           ;; we needn't render or mark the article.
9934           (let ((gnus-display-mime-function nil)
9935                 (gnus-article-prepare-hook nil)
9936                 (gnus-mark-article-hook nil))
9937             (gnus-summary-select-article nil nil nil (car articles))))
9938       (setq to-newsgroup (gnus-read-move-group-name
9939                           (cadr (assq action names))
9940                           (symbol-value
9941                            (intern (format "gnus-current-%s-group" action)))
9942                           articles prefix)
9943             encoded to-newsgroup
9944             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9945       (set (intern (format "gnus-current-%s-group" action))
9946            (mm-decode-coding-string
9947             to-newsgroup
9948             (gnus-group-name-charset to-method to-newsgroup))))
9949     (unless to-method
9950       (setq to-method (or select-method
9951                           (gnus-server-to-method
9952                            (gnus-group-method to-newsgroup)))))
9953     (setq to-newsgroup
9954           (or encoded
9955               (and to-newsgroup
9956                    (mm-encode-coding-string
9957                     to-newsgroup
9958                     (gnus-group-name-charset to-method to-newsgroup)))))
9959     ;; Check the method we are to move this article to...
9960     (unless (gnus-check-backend-function
9961              'request-accept-article (car to-method))
9962       (error "%s does not support article copying" (car to-method)))
9963     (unless (gnus-check-server to-method)
9964       (error "Can't open server %s" (car to-method)))
9965     (gnus-message 6 "%s to %s: %s..."
9966                   (caddr (assq action names))
9967                   (or (car select-method)
9968                       (gnus-group-decoded-name to-newsgroup))
9969                   articles)
9970     (while articles
9971       (setq article (pop articles))
9972       ;; Set any marks that may have changed in the summary buffer.
9973       (when gnus-preserve-marks
9974         (gnus-summary-push-marks-to-backend article))
9975       (setq
9976        art-group
9977        (cond
9978         ;; Move the article.
9979         ((eq action 'move)
9980          ;; Remove this article from future suppression.
9981          (gnus-dup-unsuppress-article article)
9982          (let* ((from-method (gnus-find-method-for-group
9983                               gnus-newsgroup-name))
9984                 (to-method (or select-method
9985                                (gnus-find-method-for-group to-newsgroup)))
9986                 (move-is-internal (gnus-server-equal from-method to-method)))
9987            (gnus-request-move-article
9988             article                     ; Article to move
9989             gnus-newsgroup-name         ; From newsgroup
9990             (nth 1 (gnus-find-method-for-group
9991                     gnus-newsgroup-name)) ; Server
9992             (list 'gnus-request-accept-article
9993                   to-newsgroup (list 'quote select-method)
9994                   (not articles) t)     ; Accept form
9995             (not articles)              ; Only save nov last time
9996             (and move-is-internal
9997                  to-newsgroup           ; Not respooling
9998                                         ; Is this move internal?
9999                  (gnus-group-real-name to-newsgroup)))))
10000         ;; Copy the article.
10001         ((eq action 'copy)
10002          (with-current-buffer copy-buf
10003            (when (gnus-request-article-this-buffer article
10004                                                    gnus-newsgroup-name)
10005              (save-restriction
10006                (nnheader-narrow-to-headers)
10007                (dolist (hdr gnus-copy-article-ignored-headers)
10008                  (message-remove-header hdr t)))
10009              (gnus-request-accept-article
10010               to-newsgroup select-method (not articles) t))))
10011         ;; Crosspost the article.
10012         ((eq action 'crosspost)
10013          (let ((xref (message-tokenize-header
10014                       (mail-header-xref (gnus-summary-article-header
10015                                          article))
10016                       " ")))
10017            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
10018                                   ":" (number-to-string article)))
10019            (unless xref
10020              (setq xref (list (system-name))))
10021            (setq new-xref
10022                  (concat
10023                   (mapconcat 'identity
10024                              (delete "Xref:" (delete new-xref xref))
10025                              " ")
10026                   " " new-xref))
10027            (with-current-buffer copy-buf
10028              ;; First put the article in the destination group.
10029              (gnus-request-article-this-buffer article gnus-newsgroup-name)
10030              (when (consp (setq art-group
10031                                 (gnus-request-accept-article
10032                                  to-newsgroup select-method (not articles)
10033                                  t)))
10034                (setq new-xref (concat new-xref " " (car art-group)
10035                                       ":"
10036                                       (number-to-string (cdr art-group))))
10037                ;; Now we have the new Xrefs header, so we insert
10038                ;; it and replace the new article.
10039                (nnheader-replace-header "Xref" new-xref)
10040                (gnus-request-replace-article
10041                 (cdr art-group) to-newsgroup (current-buffer) t)
10042                art-group))))))
10043       (cond
10044        ((not art-group)
10045         (gnus-message 1 "Couldn't %s article %s: %s"
10046                       (cadr (assq action names)) article
10047                       (nnheader-get-report (car to-method))))
10048        ((eq art-group 'junk)
10049         (when (eq action 'move)
10050           (gnus-summary-mark-article article gnus-canceled-mark)
10051           (gnus-message 4 "Deleted article %s" article)
10052           ;; run the delete hook
10053           (run-hook-with-args 'gnus-summary-article-delete-hook
10054                               action
10055                               (gnus-data-header
10056                                (assoc article (gnus-data-list nil)))
10057                               gnus-newsgroup-name nil
10058                               select-method)))
10059        (t
10060         (let* ((pto-group (gnus-group-prefixed-name
10061                            (car art-group) to-method))
10062                (info (gnus-get-info pto-group))
10063                (to-group (gnus-info-group info))
10064                to-marks)
10065           ;; Update the group that has been moved to.
10066           (when (and info
10067                      (memq action '(move copy)))
10068             (unless (member to-group to-groups)
10069               (push to-group to-groups))
10070
10071             (when (and (not (memq article gnus-newsgroup-unreads))
10072                        (cdr art-group))
10073               (push 'read to-marks)
10074               (gnus-info-set-read
10075                info (gnus-add-to-range (gnus-info-read info)
10076                                        (list (cdr art-group)))))
10077
10078             ;; See whether the article is to be put in the cache.
10079             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10080                    (marks (if expirable
10081                               gnus-article-mark-lists
10082                             (delete '(expirable . expire)
10083                                     (copy-sequence
10084                                      gnus-article-mark-lists))))
10085                    (to-article (cdr art-group)))
10086
10087               ;; Enter the article into the cache in the new group,
10088               ;; if that is required.
10089               (when (and to-article
10090                          gnus-use-cache)
10091                 (gnus-cache-possibly-enter-article
10092                  to-group to-article
10093                  (memq article gnus-newsgroup-marked)
10094                  (memq article gnus-newsgroup-dormant)
10095                  (memq article gnus-newsgroup-unreads)))
10096
10097               (when (and gnus-preserve-marks
10098                          to-article)
10099                 ;; Copy any marks over to the new group.
10100                 (when (and (equal to-group gnus-newsgroup-name)
10101                            (not (memq article gnus-newsgroup-unreads)))
10102                   ;; Mark this article as read in this group.
10103                   (push (cons to-article gnus-read-mark)
10104                         gnus-newsgroup-reads)
10105                   ;; Increase the active status of this group.
10106                   (setcdr (gnus-active to-group) to-article)
10107                   (setcdr gnus-newsgroup-active to-article))
10108
10109                 (while marks
10110                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10111                     (when (memq article (symbol-value
10112                                          (intern (format "gnus-newsgroup-%s"
10113                                                          (caar marks)))))
10114                       (push (cdar marks) to-marks)
10115                       ;; If the other group is the same as this group,
10116                       ;; then we have to add the mark to the list.
10117                       (when (equal to-group gnus-newsgroup-name)
10118                         (set (intern (format "gnus-newsgroup-%s"
10119                                              (caar marks)))
10120                              (cons to-article
10121                                    (symbol-value
10122                                     (intern (format "gnus-newsgroup-%s"
10123                                                     (caar marks)))))))
10124                       ;; Copy the marks to other group.
10125                       (gnus-add-marked-articles
10126                        to-group (cdar marks) (list to-article) info)))
10127                   (setq marks (cdr marks)))
10128
10129                 (when (and expirable
10130                            gnus-mark-copied-or-moved-articles-as-expirable
10131                            (not (memq 'expire to-marks)))
10132                   ;; Mark this article as expirable.
10133                   (push 'expire to-marks)
10134                   (when (equal to-group gnus-newsgroup-name)
10135                     (push to-article gnus-newsgroup-expirable))
10136                   ;; Copy the expirable mark to other group.
10137                   (gnus-add-marked-articles
10138                    to-group 'expire (list to-article) info))
10139
10140                 (when (and to-marks
10141                            (gnus-method-option-p
10142                             (gnus-find-method-for-group to-group)
10143                             'server-marks))
10144                   (gnus-request-set-mark
10145                    to-group (list (list (list to-article) 'add to-marks)))))
10146
10147               (gnus-dribble-enter
10148                (concat "(gnus-group-set-info '"
10149                        (gnus-prin1-to-string (gnus-get-info to-group))
10150                        ")")
10151                (concat "^(gnus-group-set-info '(\""
10152                        (regexp-quote to-group) "\""))))
10153
10154           ;; Update the Xref header in this article to point to
10155           ;; the new crossposted article we have just created.
10156           (when (eq action 'crosspost)
10157             (with-current-buffer copy-buf
10158               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10159               (nnheader-replace-header "Xref" new-xref)
10160               (gnus-request-replace-article
10161                article gnus-newsgroup-name (current-buffer) t)))
10162
10163           ;; run the move/copy/crosspost/respool hook
10164           (run-hook-with-args 'gnus-summary-article-move-hook
10165                               action
10166                               (gnus-data-header
10167                                (assoc article (gnus-data-list nil)))
10168                               gnus-newsgroup-name
10169                               to-newsgroup
10170                               select-method))
10171
10172         ;;;!!!Why is this necessary?
10173         (set-buffer gnus-summary-buffer)
10174
10175         (when (eq action 'move)
10176           (save-excursion
10177             (gnus-summary-goto-subject article)
10178             (gnus-summary-mark-article article gnus-canceled-mark)))))
10179       (push article articles-to-update-marks))
10180
10181     (save-excursion
10182       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10183     ;; Re-activate all groups that have been moved to.
10184     (with-current-buffer gnus-group-buffer
10185       (let ((gnus-group-marked to-groups))
10186         (gnus-group-get-new-news-this-group nil t)))
10187
10188     (gnus-kill-buffer copy-buf)
10189     (gnus-summary-position-point)
10190     (gnus-set-mode-line 'summary)))
10191
10192 (defun gnus-summary-push-marks-to-backend (article)
10193   (let ((set nil)
10194         (del nil)
10195         (marks gnus-article-mark-lists))
10196     (unless (memq article gnus-newsgroup-unreads)
10197       (push 'read set))
10198     (while marks
10199       (if (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10200                (memq article (symbol-value
10201                               (intern (format "gnus-newsgroup-%s"
10202                                               (caar marks))))))
10203           (push (cdar marks) set)
10204         (push (cdar marks) del))
10205       (pop marks))
10206     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)
10207                                                  ((,article) del ,del)))))
10208
10209 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10210   "Copy the current article to some other group.
10211 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10212 When called interactively, if TO-NEWSGROUP is nil, use the value of
10213 the variable `gnus-move-split-methods' for finding a default target
10214 newsgroup.
10215 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10216 re-spool using this method."
10217   (interactive "P")
10218   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10219
10220 (defun gnus-summary-crosspost-article (&optional n)
10221   "Crosspost the current article to some other group."
10222   (interactive "P")
10223   (gnus-summary-move-article n nil nil 'crosspost))
10224
10225 (defcustom gnus-summary-respool-default-method nil
10226   "Default method type for respooling an article.
10227 If nil, use to the current newsgroup method."
10228   :type 'symbol
10229   :group 'gnus-summary-mail)
10230
10231 (defun gnus-summary-respool-article (&optional n method)
10232   "Respool the current article.
10233 The article will be squeezed through the mail spooling process again,
10234 which means that it will be put in some mail newsgroup or other
10235 depending on `nnmail-split-methods'.
10236 If N is a positive number, respool the N next articles.
10237 If N is a negative number, respool the N previous articles.
10238 If N is nil and any articles have been marked with the process mark,
10239 respool those articles instead.
10240
10241 Respooling can be done both from mail groups and \"real\" newsgroups.
10242 In the former case, the articles in question will be moved from the
10243 current group into whatever groups they are destined to.  In the
10244 latter case, they will be copied into the relevant groups."
10245   (interactive
10246    (list current-prefix-arg
10247          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10248                 (methname
10249                  (symbol-name (or gnus-summary-respool-default-method
10250                                   (car (gnus-find-method-for-group
10251                                         gnus-newsgroup-name)))))
10252                 (method
10253                  (gnus-completing-read
10254                   "Backend to use when respooling"
10255                   methods t nil 'gnus-mail-method-history methname))
10256                 ms)
10257            (cond
10258             ((zerop (length (setq ms (gnus-servers-using-backend
10259                                       (intern method)))))
10260              (list (intern method) ""))
10261             ((= 1 (length ms))
10262              (car ms))
10263             (t
10264              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10265                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10266                            ms-alist))))))))
10267   (unless method
10268     (error "No method given for respooling"))
10269   (if (assoc (symbol-name
10270               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10271              (gnus-methods-using 'respool))
10272       (gnus-summary-move-article n nil method)
10273     (gnus-summary-copy-article n nil method)))
10274
10275 (defun gnus-summary-import-article (file &optional edit)
10276   "Import an arbitrary file into a mail newsgroup."
10277   (interactive "fImport file: \nP")
10278   (let ((group gnus-newsgroup-name)
10279         (now (current-time))
10280         atts lines group-art)
10281     (unless (gnus-check-backend-function 'request-accept-article group)
10282       (error "%s does not support article importing" group))
10283     (or (file-readable-p file)
10284         (not (file-regular-p file))
10285         (error "Can't read %s" file))
10286     (with-current-buffer (gnus-get-buffer-create " *import file*")
10287       (erase-buffer)
10288       (nnheader-insert-file-contents file)
10289       (goto-char (point-min))
10290       (if (nnheader-article-p)
10291           (save-restriction
10292             (goto-char (point-min))
10293             (search-forward "\n\n" nil t)
10294             (narrow-to-region (point-min) (1- (point)))
10295             (goto-char (point-min))
10296             (unless (re-search-forward "^date:" nil t)
10297               (goto-char (point-max))
10298               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10299        ;; This doesn't look like an article, so we fudge some headers.
10300         (setq atts (file-attributes file)
10301               lines (count-lines (point-min) (point-max)))
10302         (insert "From: " (read-string "From: ") "\n"
10303                 "Subject: " (read-string "Subject: ") "\n"
10304                 "Date: " (message-make-date (nth 5 atts)) "\n"
10305                 "Message-ID: " (message-make-message-id) "\n"
10306                 "Lines: " (int-to-string lines) "\n"
10307                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10308       (setq group-art (gnus-request-accept-article group nil t))
10309       (kill-buffer (current-buffer)))
10310     (setq gnus-newsgroup-active (gnus-activate-group group))
10311     (forward-line 1)
10312     (gnus-summary-goto-article (cdr group-art) nil t)
10313     (when edit
10314       (gnus-summary-edit-article))))
10315
10316 (defun gnus-summary-create-article ()
10317   "Create an article in a mail newsgroup."
10318   (interactive)
10319   (let ((group gnus-newsgroup-name)
10320         (now (current-time))
10321         group-art)
10322     (unless (gnus-check-backend-function 'request-accept-article group)
10323       (error "%s does not support article importing" group))
10324     (with-current-buffer (gnus-get-buffer-create " *import file*")
10325       (erase-buffer)
10326       (goto-char (point-min))
10327       ;; This doesn't look like an article, so we fudge some headers.
10328       (insert "From: " (read-string "From: ") "\n"
10329               "Subject: " (read-string "Subject: ") "\n"
10330               "Date: " (message-make-date now) "\n"
10331               "Message-ID: " (message-make-message-id) "\n")
10332       (setq group-art (gnus-request-accept-article group nil t))
10333       (kill-buffer (current-buffer)))
10334     (setq gnus-newsgroup-active (gnus-activate-group group))
10335     (forward-line 1)
10336     (gnus-summary-goto-article (cdr group-art) nil t)
10337     (gnus-summary-edit-article)))
10338
10339 (defun gnus-summary-article-posted-p ()
10340   "Say whether the current (mail) article is available from news as well.
10341 This will be the case if the article has both been mailed and posted."
10342   (interactive)
10343   (let ((id (mail-header-references (gnus-summary-article-header)))
10344         (gnus-override-method (car (gnus-refer-article-methods))))
10345     (if (gnus-request-head id "")
10346         (gnus-message 2 "The current message was found on %s"
10347                       gnus-override-method)
10348       (gnus-message 2 "The current message couldn't be found on %s"
10349                     gnus-override-method)
10350       nil)))
10351
10352 (defun gnus-summary-expire-articles (&optional now)
10353   "Expire all articles that are marked as expirable in the current group."
10354   (interactive)
10355   (when (and (not gnus-group-is-exiting-without-update-p)
10356              (gnus-check-backend-function
10357               'request-expire-articles gnus-newsgroup-name))
10358     ;; This backend supports expiry.
10359     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10360            (expirable
10361             (gnus-list-range-difference
10362              (if total
10363                  (progn
10364                    ;; We need to update the info for
10365                    ;; this group for `gnus-list-of-read-articles'
10366                    ;; to give us the right answer.
10367                    (gnus-run-hooks 'gnus-exit-group-hook)
10368                    (gnus-summary-update-info)
10369                    (gnus-list-of-read-articles gnus-newsgroup-name))
10370                (setq gnus-newsgroup-expirable
10371                      (sort gnus-newsgroup-expirable '<)))
10372              gnus-newsgroup-unexist))
10373            (expiry-wait (if now 'immediate
10374                           (gnus-group-find-parameter
10375                            gnus-newsgroup-name 'expiry-wait)))
10376            (nnmail-expiry-target
10377             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10378                 nnmail-expiry-target))
10379            es)
10380       (when expirable
10381         ;; There are expirable articles in this group, so we run them
10382         ;; through the expiry process.
10383         (gnus-message 6 "Expiring articles...")
10384         (when (gnus-check-group gnus-newsgroup-name)
10385           ;; The list of articles that weren't expired is returned.
10386           (save-excursion
10387             (if expiry-wait
10388                 (let ((nnmail-expiry-wait-function nil)
10389                       (nnmail-expiry-wait expiry-wait))
10390                   (setq es (gnus-request-expire-articles
10391                             expirable gnus-newsgroup-name)))
10392               (setq es (gnus-request-expire-articles
10393                         expirable gnus-newsgroup-name)))
10394             (unless total
10395               (setq gnus-newsgroup-expirable es))
10396             ;; We go through the old list of expirable, and mark all
10397             ;; really expired articles as nonexistent.
10398             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10399               (let ((gnus-use-cache nil))
10400                 (dolist (article expirable)
10401                   (when (and (not (memq article es))
10402                              (gnus-data-find article))
10403                     (gnus-summary-mark-article article gnus-canceled-mark)
10404                     (run-hook-with-args 'gnus-summary-article-expire-hook
10405                                         'delete
10406                                         (gnus-data-header
10407                                          (assoc article (gnus-data-list nil)))
10408                                         gnus-newsgroup-name
10409                                         nil
10410                                         nil)))))))
10411         (gnus-message 6 "Expiring articles...done")))))
10412
10413 (defun gnus-summary-expire-articles-now ()
10414   "Expunge all expirable articles in the current group.
10415 This means that *all* articles that are marked as expirable will be
10416 deleted forever, right now."
10417   (interactive)
10418   (or gnus-expert-user
10419       (gnus-yes-or-no-p
10420        "Are you really, really sure you want to delete all expirable messages? ")
10421       (error "Phew!"))
10422   (gnus-summary-expire-articles t))
10423
10424 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10425 (defun gnus-summary-delete-article (&optional n)
10426   "Delete the N next (mail) articles.
10427 This command actually deletes articles.  This is not a marking
10428 command.  The article will disappear forever from your life, never to
10429 return.
10430
10431 If N is negative, delete backwards.
10432 If N is nil and articles have been marked with the process mark,
10433 delete these instead.
10434
10435 If `gnus-novice-user' is non-nil you will be asked for
10436 confirmation before the articles are deleted."
10437   (interactive "P")
10438   (unless (gnus-check-backend-function 'request-expire-articles
10439                                        gnus-newsgroup-name)
10440     (error "The current newsgroup does not support article deletion"))
10441   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10442     (error "Couldn't open server"))
10443   ;; Compute the list of articles to delete.
10444   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10445         (nnmail-expiry-target 'delete)
10446         not-deleted)
10447     (if (and gnus-novice-user
10448              (not (gnus-yes-or-no-p
10449                    (format "Do you really want to delete %s forever? "
10450                            (if (> (length articles) 1)
10451                                (format "these %s articles" (length articles))
10452                              "this article")))))
10453         ()
10454       ;; Delete the articles.
10455       (setq not-deleted (gnus-request-expire-articles
10456                          articles gnus-newsgroup-name 'force))
10457       (save-excursion
10458         (while articles
10459           (gnus-summary-remove-process-mark (car articles))
10460           ;; The backend might not have been able to delete the article
10461           ;; after all.
10462           (unless (memq (car articles) not-deleted)
10463             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10464             (let* ((article (car articles))
10465                    (ghead  (gnus-data-header
10466                             (assoc article (gnus-data-list nil)))))
10467               (run-hook-with-args 'gnus-summary-article-delete-hook
10468                                   'delete ghead gnus-newsgroup-name nil
10469                                   nil)))
10470           (setq articles (cdr articles))))
10471       (when not-deleted
10472         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10473     (gnus-summary-position-point)
10474     (gnus-set-mode-line 'summary)
10475     not-deleted))
10476
10477 (defun gnus-summary-edit-article (&optional arg)
10478   "Edit the current article.
10479 This will have permanent effect only in mail groups.
10480 If ARG is nil, edit the decoded articles.
10481 If ARG is 1, edit the raw articles.
10482 If ARG is 2, edit the raw articles even in read-only groups.
10483 If ARG is 3, edit the articles with the current handles.
10484 Otherwise, allow editing of articles even in read-only
10485 groups."
10486   (interactive "P")
10487   (let (force raw current-handles)
10488     (cond
10489      ((null arg))
10490      ((eq arg 1)
10491       (setq raw t))
10492      ((eq arg 2)
10493       (setq raw t
10494             force t))
10495      ((eq arg 3)
10496       (setq current-handles
10497             (and (gnus-buffer-live-p gnus-article-buffer)
10498                  (with-current-buffer gnus-article-buffer
10499                    (prog1
10500                        gnus-article-mime-handles
10501                      (setq gnus-article-mime-handles nil))))))
10502      (t
10503       (setq force t)))
10504     (when (and raw (not force)
10505                (member gnus-newsgroup-name '("nndraft:delayed"
10506                                              "nndraft:drafts"
10507                                              "nndraft:queue")))
10508       (error "Can't edit the raw article in group %s"
10509              gnus-newsgroup-name))
10510     (with-current-buffer gnus-summary-buffer
10511       (let ((mail-parse-charset gnus-newsgroup-charset)
10512             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10513         (gnus-set-global-variables)
10514         (when (and (not force)
10515                    (gnus-group-read-only-p))
10516           (error "The current newsgroup does not support article editing"))
10517         (gnus-summary-show-article t)
10518         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10519           (with-current-buffer gnus-article-buffer
10520             (mm-enable-multibyte)))
10521         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10522             (setq raw t))
10523         (gnus-article-edit-article
10524          (if raw 'ignore
10525            `(lambda ()
10526               (let ((mbl mml-buffer-list))
10527                 (setq mml-buffer-list nil)
10528                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10529                   (mime-to-mml ,'current-handles))
10530                 (let ((mbl1 mml-buffer-list))
10531                   (setq mml-buffer-list mbl)
10532                   (set (make-local-variable 'mml-buffer-list) mbl1))
10533                 (gnus-make-local-hook 'kill-buffer-hook)
10534                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10535          `(lambda (no-highlight)
10536             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10537                   (message-options message-options)
10538                   (message-options-set-recipient)
10539                   (mail-parse-ignored-charsets
10540                    ',gnus-newsgroup-ignored-charsets)
10541                   (rfc2047-header-encoding-alist
10542                    ',(let ((charset (gnus-group-name-charset
10543                                      (gnus-find-method-for-group
10544                                       gnus-newsgroup-name)
10545                                      gnus-newsgroup-name)))
10546                        (append (list (cons "Newsgroups" charset)
10547                                      (cons "Followup-To" charset)
10548                                      (cons "Xref" charset))
10549                                rfc2047-header-encoding-alist))))
10550               ,(if (not raw) '(progn
10551                                 (mml-to-mime)
10552                                 (mml-destroy-buffers)
10553                                 (remove-hook 'kill-buffer-hook
10554                                              'mml-destroy-buffers t)
10555                                 (kill-local-variable 'mml-buffer-list)))
10556               (gnus-summary-edit-article-done
10557                ,(or (mail-header-references gnus-current-headers) "")
10558                ,(gnus-group-read-only-p)
10559                ,gnus-summary-buffer no-highlight))))))))
10560
10561 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10562
10563 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10564                                                  no-highlight)
10565   "Make edits to the current article permanent."
10566   (interactive)
10567   (save-excursion
10568     ;; The buffer restriction contains the entire article if it exists.
10569     (when (article-goto-body)
10570       (let ((lines (count-lines (point) (point-max)))
10571             (length (- (point-max) (point)))
10572             (case-fold-search t)
10573             (body (copy-marker (point))))
10574         (goto-char (point-min))
10575         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10576           (delete-region (match-beginning 1) (match-end 1))
10577           (insert (number-to-string length)))
10578         (goto-char (point-min))
10579         (when (re-search-forward
10580                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10581           (delete-region (match-beginning 1) (match-end 1))
10582           (insert (number-to-string length)))
10583         (goto-char (point-min))
10584         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10585           (delete-region (match-beginning 1) (match-end 1))
10586           (insert (number-to-string lines))))))
10587   ;; Replace the article.
10588   (let ((buf (current-buffer))
10589         (article (cdr gnus-article-current))
10590         replace-result)
10591     (with-temp-buffer
10592       (insert-buffer-substring buf)
10593       (if (and (not read-only)
10594                (not (setq replace-result
10595                           (gnus-request-replace-article
10596                            article (car gnus-article-current)
10597                            (current-buffer) t))))
10598           (error "Couldn't replace article")
10599         ;; If we got a number back, then that's the new article number
10600         ;; for this article.  Otherwise, the article number didn't change.
10601         (when (numberp replace-result)
10602           (with-current-buffer gnus-summary-buffer
10603             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10604             (gnus-summary-limit gnus-newsgroup-limit)
10605             (setq article replace-result)
10606             (gnus-summary-goto-subject article t)))
10607         ;; Update the summary buffer.
10608         (if (and references
10609                  (equal (message-tokenize-header references " ")
10610                         (message-tokenize-header
10611                          (or (message-fetch-field "references") "") " ")))
10612             ;; We only have to update this line.
10613             (save-excursion
10614               (save-restriction
10615                 (message-narrow-to-head)
10616                 (let ((head (buffer-substring-no-properties
10617                              (point-min) (point-max)))
10618                       header)
10619                   (with-temp-buffer
10620                     (insert (format "211 %d Article retrieved.\n" article))
10621                     (insert head)
10622                     (insert ".\n")
10623                     (let ((nntp-server-buffer (current-buffer)))
10624                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10625                     (with-current-buffer gnus-summary-buffer
10626                       (gnus-data-set-header (gnus-data-find article) header)
10627                       (gnus-summary-update-article-line article header)
10628                       (if (gnus-summary-goto-subject article nil t)
10629                           (gnus-summary-update-secondary-mark article)))))))
10630           ;; Update threads.
10631           (set-buffer (or buffer gnus-summary-buffer))
10632           (gnus-summary-update-article article)
10633           (if (gnus-summary-goto-subject article nil t)
10634               (gnus-summary-update-secondary-mark article)))
10635         ;; Prettify the article buffer again.
10636         (unless no-highlight
10637           (with-current-buffer gnus-article-buffer
10638             ;;!!! Fix this -- article should be rehighlighted.
10639             ;;(gnus-run-hooks 'gnus-article-display-hook)
10640             (set-buffer gnus-original-article-buffer)
10641             (gnus-request-article
10642              article (car gnus-article-current) (current-buffer))))
10643         ;; Prettify the summary buffer line.
10644         (when (gnus-visual-p 'summary-highlight 'highlight)
10645           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10646
10647 (defun gnus-summary-edit-wash (key)
10648   "Perform editing command KEY in the article buffer."
10649   (interactive
10650    (list
10651     (progn
10652       (message "%s" (concat (this-command-keys) "- "))
10653       (read-char))))
10654   (message "")
10655   (gnus-summary-edit-article)
10656   (execute-kbd-macro (concat (this-command-keys) key))
10657   (gnus-article-edit-done))
10658
10659 ;;; Respooling
10660
10661 (defun gnus-summary-respool-query (&optional silent trace)
10662   "Query where the respool algorithm would put this article."
10663   (interactive)
10664   (let (gnus-mark-article-hook)
10665     (gnus-summary-select-article)
10666     (with-current-buffer gnus-original-article-buffer
10667       (let ((groups (nnmail-article-group 'identity trace)))
10668         (unless silent
10669           (if groups
10670               (message "This message would go to %s"
10671                        (mapconcat 'car groups ", "))
10672             (message "This message would go to no groups"))
10673           groups)))))
10674
10675 (defun gnus-summary-respool-trace ()
10676   "Trace where the respool algorithm would put this article.
10677 Display a buffer showing all fancy splitting patterns which matched."
10678   (interactive)
10679   (gnus-summary-respool-query nil t))
10680
10681 ;; Summary marking commands.
10682
10683 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10684   "Mark articles which has the same subject as read, and then select the next.
10685 If UNMARK is positive, remove any kind of mark.
10686 If UNMARK is negative, tick articles."
10687   (interactive "P")
10688   (when unmark
10689     (setq unmark (prefix-numeric-value unmark)))
10690   (let ((count
10691          (gnus-summary-mark-same-subject
10692           (gnus-summary-article-subject) unmark)))
10693     ;; Select next unread article.  If auto-select-same mode, should
10694     ;; select the first unread article.
10695     (gnus-summary-next-article t (and gnus-auto-select-same
10696                                       (gnus-summary-article-subject)))
10697     (gnus-message 7 "%d article%s marked as %s"
10698                   count (if (= count 1) " is" "s are")
10699                   (if unmark "unread" "read"))))
10700
10701 (defun gnus-summary-kill-same-subject (&optional unmark)
10702   "Mark articles which has the same subject as read.
10703 If UNMARK is positive, remove any kind of mark.
10704 If UNMARK is negative, tick articles."
10705   (interactive "P")
10706   (when unmark
10707     (setq unmark (prefix-numeric-value unmark)))
10708   (let ((count
10709          (gnus-summary-mark-same-subject
10710           (gnus-summary-article-subject) unmark)))
10711     ;; If marked as read, go to next unread subject.
10712     (when (null unmark)
10713       ;; Go to next unread subject.
10714       (gnus-summary-next-subject 1 t))
10715     (gnus-message 7 "%d articles are marked as %s"
10716                   count (if unmark "unread" "read"))))
10717
10718 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10719   "Mark articles with same SUBJECT as read, and return marked number.
10720 If optional argument UNMARK is positive, remove any kinds of marks.
10721 If optional argument UNMARK is negative, mark articles as unread instead."
10722   (let ((count 1))
10723     (save-excursion
10724       (cond
10725        ((null unmark)                   ; Mark as read.
10726         (while (and
10727                 (progn
10728                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10729                   (gnus-summary-show-thread) t)
10730                 (gnus-summary-find-subject subject))
10731           (setq count (1+ count))))
10732        ((> unmark 0)                    ; Tick.
10733         (while (and
10734                 (progn
10735                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10736                   (gnus-summary-show-thread) t)
10737                 (gnus-summary-find-subject subject))
10738           (setq count (1+ count))))
10739        (t                               ; Mark as unread.
10740         (while (and
10741                 (progn
10742                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10743                   (gnus-summary-show-thread) t)
10744                 (gnus-summary-find-subject subject))
10745           (setq count (1+ count)))))
10746       (gnus-set-mode-line 'summary)
10747       ;; Return the number of marked articles.
10748       count)))
10749
10750 (defun gnus-summary-mark-as-processable (n &optional unmark)
10751   "Set the process mark on the next N articles.
10752 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10753 the process mark instead.  The difference between N and the actual
10754 number of articles marked is returned."
10755   (interactive "P")
10756   (if (and (null n) (gnus-region-active-p))
10757       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10758     (setq n (prefix-numeric-value n))
10759     (let ((backward (< n 0))
10760           (n (abs n)))
10761       (while (and
10762               (> n 0)
10763               (if unmark
10764                   (gnus-summary-remove-process-mark
10765                    (gnus-summary-article-number))
10766                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10767               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10768         (setq n (1- n)))
10769       (when (/= 0 n)
10770         (gnus-message 7 "No more articles"))
10771       (gnus-summary-recenter)
10772       (gnus-summary-position-point)
10773       n)))
10774
10775 (defun gnus-summary-unmark-as-processable (n)
10776   "Remove the process mark from the next N articles.
10777 If N is negative, unmark backward instead.  The difference between N and
10778 the actual number of articles unmarked is returned."
10779   (interactive "P")
10780   (gnus-summary-mark-as-processable n t))
10781
10782 (defun gnus-summary-unmark-all-processable ()
10783   "Remove the process mark from all articles."
10784   (interactive)
10785   (save-excursion
10786     (while gnus-newsgroup-processable
10787       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10788   (gnus-summary-position-point))
10789
10790 (defun gnus-summary-add-mark (article type)
10791   "Mark ARTICLE with a mark of TYPE."
10792   (let ((vtype (car (assq type gnus-article-mark-lists)))
10793         var)
10794     (if (not vtype)
10795         (error "No such mark type: %s" type)
10796       (setq var (intern (format "gnus-newsgroup-%s" type)))
10797       (set var (cons article (symbol-value var)))
10798       (if (memq type '(processable cached replied forwarded recent saved))
10799           (gnus-summary-update-secondary-mark article)
10800         ;;; !!! This is bogus.  We should find out what primary
10801         ;;; !!! mark we want to set.
10802         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10803
10804 (defun gnus-summary-mark-as-expirable (n)
10805   "Mark N articles forward as expirable.
10806 If N is negative, mark backward instead.  The difference between N and
10807 the actual number of articles marked is returned."
10808   (interactive "p")
10809   (gnus-summary-mark-forward n gnus-expirable-mark))
10810
10811 (defun gnus-summary-mark-as-spam (n)
10812   "Mark N articles forward as spam.
10813 If N is negative, mark backward instead.  The difference between N and
10814 the actual number of articles marked is returned."
10815   (interactive "p")
10816   (gnus-summary-mark-forward n gnus-spam-mark))
10817
10818 (defun gnus-summary-mark-article-as-replied (article)
10819   "Mark ARTICLE as replied to and update the summary line.
10820 ARTICLE can also be a list of articles."
10821   (interactive (list (gnus-summary-article-number)))
10822   (let ((articles (if (listp article) article (list article))))
10823     (dolist (article articles)
10824       (unless (numberp article)
10825         (error "%s is not a number" article))
10826       (push article gnus-newsgroup-replied)
10827       (let ((inhibit-read-only t))
10828         (when (gnus-summary-goto-subject article nil t)
10829           (gnus-summary-update-secondary-mark article))))))
10830
10831 (defun gnus-summary-mark-article-as-forwarded (article)
10832   "Mark ARTICLE as forwarded and update the summary line.
10833 ARTICLE can also be a list of articles."
10834   (let ((articles (if (listp article) article (list article))))
10835     (dolist (article articles)
10836       (push article gnus-newsgroup-forwarded)
10837       (let ((inhibit-read-only t))
10838         (when (gnus-summary-goto-subject article nil t)
10839           (gnus-summary-update-secondary-mark article))))))
10840
10841 (defun gnus-summary-set-bookmark (article)
10842   "Set a bookmark in current article."
10843   (interactive (list (gnus-summary-article-number)))
10844   (when (or (not (get-buffer gnus-article-buffer))
10845             (not gnus-current-article)
10846             (not gnus-article-current)
10847             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10848     (error "No current article selected"))
10849   ;; Remove old bookmark, if one exists.
10850   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10851   ;; Set the new bookmark, which is on the form
10852   ;; (article-number . line-number-in-body).
10853   (push
10854    (cons article
10855          (with-current-buffer gnus-article-buffer
10856            (count-lines
10857             (min (point)
10858                  (save-excursion
10859                    (article-goto-body)
10860                    (point)))
10861             (point))))
10862    gnus-newsgroup-bookmarks)
10863   (gnus-message 6 "A bookmark has been added to the current article."))
10864
10865 (defun gnus-summary-remove-bookmark (article)
10866   "Remove the bookmark from the current article."
10867   (interactive (list (gnus-summary-article-number)))
10868   ;; Remove old bookmark, if one exists.
10869   (if (not (assq article gnus-newsgroup-bookmarks))
10870       (gnus-message 6 "No bookmark in current article.")
10871     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10872     (gnus-message 6 "Removed bookmark.")))
10873
10874 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10875 (defun gnus-summary-mark-as-dormant (n)
10876   "Mark N articles forward as dormant.
10877 If N is negative, mark backward instead.  The difference between N and
10878 the actual number of articles marked is returned."
10879   (interactive "p")
10880   (gnus-summary-mark-forward n gnus-dormant-mark))
10881
10882 (defun gnus-summary-set-process-mark (article)
10883   "Set the process mark on ARTICLE and update the summary line."
10884   (setq gnus-newsgroup-processable
10885         (cons article
10886               (delq article gnus-newsgroup-processable)))
10887   (when (gnus-summary-goto-subject article)
10888     (gnus-summary-show-thread)
10889     (gnus-summary-goto-subject article)
10890     (gnus-summary-update-secondary-mark article)))
10891
10892 (defun gnus-summary-remove-process-mark (&rest articles)
10893   "Remove the process mark from ARTICLES and update the summary line."
10894   (dolist (article articles)
10895     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10896     (when (gnus-summary-goto-subject article)
10897       (gnus-summary-show-thread)
10898       (gnus-summary-goto-subject article)
10899       (gnus-summary-update-secondary-mark article)))
10900   t)
10901
10902 (defun gnus-summary-set-saved-mark (article)
10903   "Set the process mark on ARTICLE and update the summary line."
10904   (push article gnus-newsgroup-saved)
10905   (when (gnus-summary-goto-subject article)
10906     (gnus-summary-update-secondary-mark article)))
10907
10908 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10909   "Mark N articles as read forwards.
10910 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10911 The difference between N and the actual number of articles marked is
10912 returned.
10913 If NO-EXPIRE, auto-expiry will be inhibited."
10914   (interactive "p")
10915   (gnus-summary-show-thread)
10916   (let ((backward (< n 0))
10917         (gnus-summary-goto-unread
10918          (and gnus-summary-goto-unread
10919               (not (eq gnus-summary-goto-unread 'never))
10920               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10921                                     gnus-ticked-mark gnus-dormant-mark)))))
10922         (n (abs n))
10923         (mark (or mark gnus-del-mark)))
10924     (while (and (> n 0)
10925                 (gnus-summary-mark-article nil mark no-expire)
10926                 (zerop (gnus-summary-next-subject
10927                         (if backward -1 1)
10928                         (and gnus-summary-goto-unread
10929                              (not (eq gnus-summary-goto-unread 'never)))
10930                         t)))
10931       (setq n (1- n)))
10932     (when (/= 0 n)
10933       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10934     (gnus-summary-recenter)
10935     (gnus-summary-position-point)
10936     (gnus-set-mode-line 'summary)
10937     n))
10938
10939 (defun gnus-summary-mark-article-as-read (mark)
10940   "Mark the current article quickly as read with MARK."
10941   (let ((article (gnus-summary-article-number)))
10942     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10943     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10944     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10945     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10946     (push (cons article mark) gnus-newsgroup-reads)
10947     ;; Possibly remove from cache, if that is used.
10948     (when gnus-use-cache
10949       (gnus-cache-enter-remove-article article))
10950     ;; Allow the backend to change the mark.
10951     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10952     ;; Check for auto-expiry.
10953     (when (and gnus-newsgroup-auto-expire
10954                (memq mark gnus-auto-expirable-marks))
10955       (setq mark gnus-expirable-mark)
10956       ;; Let the backend know about the mark change.
10957       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10958       (push article gnus-newsgroup-expirable))
10959     ;; Set the mark in the buffer.
10960     (gnus-summary-update-mark mark 'unread)
10961     t))
10962
10963 (defun gnus-summary-mark-article-as-unread (mark)
10964   "Mark the current article quickly as unread with MARK."
10965   (let* ((article (gnus-summary-article-number))
10966          (old-mark (gnus-summary-article-mark article)))
10967     ;; Allow the backend to change the mark.
10968     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10969     (if (eq mark old-mark)
10970         t
10971       (if (<= article 0)
10972           (progn
10973             (gnus-error 1 "Can't mark negative article numbers")
10974             nil)
10975         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10976         (setq gnus-newsgroup-spam-marked
10977               (delq article gnus-newsgroup-spam-marked))
10978         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10979         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10980         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10981         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10982         (cond ((= mark gnus-ticked-mark)
10983                (setq gnus-newsgroup-marked
10984                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10985                                               article)))
10986               ((= mark gnus-spam-mark)
10987                (setq gnus-newsgroup-spam-marked
10988                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10989                                               article)))
10990               ((= mark gnus-dormant-mark)
10991                (setq gnus-newsgroup-dormant
10992                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10993                                               article)))
10994               (t
10995                (setq gnus-newsgroup-unreads
10996                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10997                                               article))))
10998         (gnus-alist-pull article gnus-newsgroup-reads)
10999
11000         ;; See whether the article is to be put in the cache.
11001         (and gnus-use-cache
11002              (vectorp (gnus-summary-article-header article))
11003              (save-excursion
11004                (gnus-cache-possibly-enter-article
11005                 gnus-newsgroup-name article
11006                 (= mark gnus-ticked-mark)
11007                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11008
11009         ;; Fix the mark.
11010         (gnus-summary-update-mark mark 'unread)
11011         t))))
11012
11013 (defun gnus-summary-mark-article (&optional article mark no-expire)
11014   "Mark ARTICLE with MARK.  MARK can be any character.
11015 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11016 `??' (dormant) and `?E' (expirable).
11017 If MARK is nil, then the default character `?r' is used.
11018 If ARTICLE is nil, then the article on the current line will be
11019 marked.
11020 If NO-EXPIRE, auto-expiry will be inhibited."
11021   ;; The mark might be a string.
11022   (when (stringp mark)
11023     (setq mark (aref mark 0)))
11024   ;; If no mark is given, then we check auto-expiring.
11025   (when (null mark)
11026     (setq mark gnus-del-mark))
11027   (when (and (not no-expire)
11028              gnus-newsgroup-auto-expire
11029              (memq mark gnus-auto-expirable-marks))
11030     (setq mark gnus-expirable-mark))
11031   (let ((article (or article (gnus-summary-article-number)))
11032         (old-mark (gnus-summary-article-mark article)))
11033     ;; Allow the backend to change the mark.
11034     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11035     (if (eq mark old-mark)
11036         t
11037       (unless article
11038         (error "No article on current line"))
11039       (if (not (if (or (= mark gnus-unread-mark)
11040                        (= mark gnus-ticked-mark)
11041                        (= mark gnus-spam-mark)
11042                        (= mark gnus-dormant-mark))
11043                    (gnus-mark-article-as-unread article mark)
11044                  (gnus-mark-article-as-read article mark)))
11045           t
11046         ;; See whether the article is to be put in the cache.
11047         (and gnus-use-cache
11048              (not (= mark gnus-canceled-mark))
11049              (vectorp (gnus-summary-article-header article))
11050              (save-excursion
11051                (gnus-cache-possibly-enter-article
11052                 gnus-newsgroup-name article
11053                 (= mark gnus-ticked-mark)
11054                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11055
11056         (when (gnus-summary-goto-subject article nil t)
11057           (let ((inhibit-read-only t))
11058             (gnus-summary-show-thread)
11059             ;; Fix the mark.
11060             (gnus-summary-update-mark mark 'unread)
11061             t))))))
11062
11063 (defun gnus-summary-update-secondary-mark (article)
11064   "Update the secondary (read, process, cache) mark."
11065   (gnus-summary-update-mark
11066    (cond ((memq article gnus-newsgroup-processable)
11067           gnus-process-mark)
11068          ((memq article gnus-newsgroup-cached)
11069           gnus-cached-mark)
11070          ((memq article gnus-newsgroup-replied)
11071           gnus-replied-mark)
11072          ((memq article gnus-newsgroup-forwarded)
11073           gnus-forwarded-mark)
11074          ((memq article gnus-newsgroup-saved)
11075           gnus-saved-mark)
11076          ((memq article gnus-newsgroup-unseen)
11077           gnus-unseen-mark)
11078          (t gnus-no-mark))
11079    'replied)
11080   (when (gnus-visual-p 'summary-highlight 'highlight)
11081     (gnus-summary-highlight-line)
11082     (gnus-run-hooks 'gnus-summary-update-hook))
11083   t)
11084
11085 (defun gnus-summary-update-download-mark (article)
11086   "Update the download mark."
11087   (gnus-summary-update-mark
11088    (cond ((memq article gnus-newsgroup-undownloaded)
11089           gnus-undownloaded-mark)
11090          (gnus-newsgroup-agentized
11091           gnus-downloaded-mark)
11092          (t
11093           gnus-no-mark))
11094    'download)
11095   (gnus-summary-update-line t)
11096   t)
11097
11098 (defun gnus-summary-update-mark (mark type)
11099   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11100         (inhibit-read-only t))
11101     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11102     (when forward
11103       (when (looking-at "\r")
11104         (incf forward))
11105       (when (<= (+ forward (point)) (point-max))
11106         ;; Go to the right position on the line.
11107         (goto-char (+ forward (point)))
11108         ;; Replace the old mark with the new mark.
11109         (let ((to-insert
11110                (mm-subst-char-in-string
11111                 (char-after) mark
11112                 (buffer-substring (point) (1+ (point))))))
11113           (delete-region (point) (1+ (point)))
11114           (insert to-insert))
11115         ;; Optionally update the marks by some user rule.
11116         (when (eq type 'unread)
11117           (gnus-data-set-mark
11118            (gnus-data-find (gnus-summary-article-number)) mark)
11119           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11120
11121 (defun gnus-mark-article-as-read (article &optional mark)
11122   "Enter ARTICLE in the pertinent lists and remove it from others."
11123   ;; Make the article expirable.
11124   (let ((mark (or mark gnus-del-mark)))
11125     (setq gnus-newsgroup-expirable
11126           (if (= mark gnus-expirable-mark)
11127               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11128             (delq article gnus-newsgroup-expirable)))
11129     ;; Remove from unread and marked lists.
11130     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11131     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11132     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11133     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11134     (push (cons article mark) gnus-newsgroup-reads)
11135     ;; Possibly remove from cache, if that is used.
11136     (when gnus-use-cache
11137       (gnus-cache-enter-remove-article article))
11138     t))
11139
11140 (defun gnus-mark-article-as-unread (article &optional mark)
11141   "Enter ARTICLE in the pertinent lists and remove it from others."
11142   (let ((mark (or mark gnus-ticked-mark)))
11143     (if (<= article 0)
11144         (progn
11145           (gnus-error 1 "Can't mark negative article numbers")
11146           nil)
11147       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11148             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11149             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11150             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11151             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11152
11153       ;; Unsuppress duplicates?
11154       (when gnus-suppress-duplicates
11155         (gnus-dup-unsuppress-article article))
11156
11157       (cond ((= mark gnus-ticked-mark)
11158              (setq gnus-newsgroup-marked
11159                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11160             ((= mark gnus-spam-mark)
11161              (setq gnus-newsgroup-spam-marked
11162                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11163                                             article)))
11164             ((= mark gnus-dormant-mark)
11165              (setq gnus-newsgroup-dormant
11166                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11167             (t
11168              (setq gnus-newsgroup-unreads
11169                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11170       (gnus-alist-pull article gnus-newsgroup-reads)
11171       t)))
11172
11173 (defun gnus-summary-tick-article-forward (n)
11174   "Tick N articles forwards.
11175 If N is negative, tick backwards instead.
11176 The difference between N and the number of articles ticked is returned."
11177   (interactive "p")
11178   (gnus-summary-mark-forward n gnus-ticked-mark))
11179
11180 (defun gnus-summary-tick-article-backward (n)
11181   "Tick N articles backwards.
11182 The difference between N and the number of articles ticked is returned."
11183   (interactive "p")
11184   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11185
11186 (defun gnus-summary-tick-article (&optional article clear-mark)
11187   "Mark current article as unread.
11188 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11189 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11190   (interactive)
11191   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11192                                        gnus-ticked-mark)))
11193
11194 (defun gnus-summary-mark-as-read-forward (n)
11195   "Mark N articles as read forwards.
11196 If N is negative, mark backwards instead.
11197 The difference between N and the actual number of articles marked is
11198 returned."
11199   (interactive "p")
11200   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11201
11202 (defun gnus-summary-mark-as-read-backward (n)
11203   "Mark the N articles as read backwards.
11204 The difference between N and the actual number of articles marked is
11205 returned."
11206   (interactive "p")
11207   (gnus-summary-mark-forward
11208    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11209
11210 (defun gnus-summary-mark-as-read (&optional article mark)
11211   "Mark current article as read.
11212 ARTICLE specifies the article to be marked as read.
11213 MARK specifies a string to be inserted at the beginning of the line."
11214   (gnus-summary-mark-article article mark))
11215
11216 (defun gnus-summary-clear-mark-forward (n)
11217   "Clear marks from N articles forward.
11218 If N is negative, clear backward instead.
11219 The difference between N and the number of marks cleared is returned."
11220   (interactive "p")
11221   (gnus-summary-mark-forward n gnus-unread-mark))
11222
11223 (defun gnus-summary-clear-mark-backward (n)
11224   "Clear marks from N articles backward.
11225 The difference between N and the number of marks cleared is returned."
11226   (interactive "p")
11227   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11228
11229 (defun gnus-summary-mark-unread-as-read ()
11230   "Intended to be used by `gnus-mark-article-hook'."
11231   (when (memq gnus-current-article gnus-newsgroup-unreads)
11232     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11233
11234 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11235   "Intended to be used by `gnus-mark-article-hook'."
11236   (let ((mark (gnus-summary-article-mark)))
11237     (when (or (gnus-unread-mark-p mark)
11238               (gnus-read-mark-p mark))
11239       (gnus-summary-mark-article gnus-current-article
11240                                  (or new-mark gnus-read-mark)))))
11241
11242 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11243   "Intended to be used by `gnus-mark-article-hook'."
11244   (let ((mark (gnus-summary-article-mark)))
11245     (when (or (gnus-unread-mark-p mark)
11246               (gnus-read-mark-p mark))
11247       (gnus-summary-mark-article (gnus-summary-article-number)
11248                                  (or new-mark gnus-read-mark)))))
11249
11250 (defun gnus-summary-mark-unread-as-ticked ()
11251   "Intended to be used by `gnus-mark-article-hook'."
11252   (when (memq gnus-current-article gnus-newsgroup-unreads)
11253     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11254
11255 (defun gnus-summary-mark-region-as-read (point mark all)
11256   "Mark all unread articles between point and mark as read.
11257 If given a prefix, mark all articles between point and mark as read,
11258 even ticked and dormant ones."
11259   (interactive "r\nP")
11260   (save-excursion
11261     (let (article)
11262       (goto-char point)
11263       (beginning-of-line)
11264       (while (and
11265               (< (point) mark)
11266               (progn
11267                 (when (or all
11268                           (memq (setq article (gnus-summary-article-number))
11269                                 gnus-newsgroup-unreads))
11270                   (gnus-summary-mark-article article gnus-del-mark))
11271                 t)
11272               (gnus-summary-find-next))))))
11273
11274 (defun gnus-summary-mark-below (score mark)
11275   "Mark articles with score less than SCORE with MARK."
11276   (interactive "P\ncMark: ")
11277   (setq score (if score
11278                   (prefix-numeric-value score)
11279                 (or gnus-summary-default-score 0)))
11280   (with-current-buffer gnus-summary-buffer
11281     (goto-char (point-min))
11282     (while
11283         (progn
11284           (and (< (gnus-summary-article-score) score)
11285                (gnus-summary-mark-article nil mark))
11286           (gnus-summary-find-next)))))
11287
11288 (defun gnus-summary-kill-below (&optional score)
11289   "Mark articles with score below SCORE as read."
11290   (interactive "P")
11291   (gnus-summary-mark-below score gnus-killed-mark))
11292
11293 (defun gnus-summary-clear-above (&optional score)
11294   "Clear all marks from articles with score above SCORE."
11295   (interactive "P")
11296   (gnus-summary-mark-above score gnus-unread-mark))
11297
11298 (defun gnus-summary-tick-above (&optional score)
11299   "Tick all articles with score above SCORE."
11300   (interactive "P")
11301   (gnus-summary-mark-above score gnus-ticked-mark))
11302
11303 (defun gnus-summary-mark-above (score mark)
11304   "Mark articles with score over SCORE with MARK."
11305   (interactive "P\ncMark: ")
11306   (setq score (if score
11307                   (prefix-numeric-value score)
11308                 (or gnus-summary-default-score 0)))
11309   (with-current-buffer gnus-summary-buffer
11310     (goto-char (point-min))
11311     (while (and (progn
11312                   (when (> (gnus-summary-article-score) score)
11313                     (gnus-summary-mark-article nil mark))
11314                   t)
11315                 (gnus-summary-find-next)))))
11316
11317 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11318 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11319 (defun gnus-summary-limit-include-expunged (&optional no-error)
11320   "Display all the hidden articles that were expunged for low scores."
11321   (interactive)
11322   (let ((inhibit-read-only t))
11323     (let ((scored gnus-newsgroup-scored)
11324           headers h)
11325       (while scored
11326         (unless (gnus-summary-article-header (caar scored))
11327           (and (setq h (gnus-number-to-header (caar scored)))
11328                (< (cdar scored) gnus-summary-expunge-below)
11329                (push h headers)))
11330         (setq scored (cdr scored)))
11331       (if (not headers)
11332           (when (not no-error)
11333             (error "No expunged articles hidden"))
11334         (goto-char (point-min))
11335         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11336         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11337         (dolist (x headers)
11338           (push (mail-header-number x) gnus-newsgroup-limit))
11339         (gnus-summary-prepare-unthreaded (nreverse headers))
11340         (goto-char (point-min))
11341         (gnus-summary-position-point)
11342         t))))
11343
11344 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11345   "Mark all unread articles in this newsgroup as read.
11346 If prefix argument ALL is non-nil, ticked and dormant articles will
11347 also be marked as read.
11348 If QUIETLY is non-nil, no questions will be asked.
11349
11350 If TO-HERE is non-nil, it should be a point in the buffer.  All
11351 articles before (after, if REVERSE is set) this point will be marked
11352 as read.
11353
11354 Note that this function will only catch up the unread article
11355 in the current summary buffer limitation.
11356
11357 The number of articles marked as read is returned."
11358   (interactive "P")
11359   (prog1
11360       (save-excursion
11361         (when (or quietly
11362                   (not gnus-interactive-catchup) ;Without confirmation?
11363                   gnus-expert-user
11364                   (gnus-y-or-n-p
11365                    (if all
11366                        "Mark absolutely all articles as read? "
11367                      "Mark all unread articles as read? ")))
11368           (if (and not-mark
11369                    (not gnus-newsgroup-adaptive)
11370                    (not gnus-newsgroup-auto-expire)
11371                    (not gnus-suppress-duplicates)
11372                    (or (not gnus-use-cache)
11373                        (eq gnus-use-cache 'passive)))
11374               (progn
11375                 (when all
11376                   (setq gnus-newsgroup-marked nil
11377                         gnus-newsgroup-spam-marked nil
11378                         gnus-newsgroup-dormant nil))
11379                 (setq gnus-newsgroup-unreads
11380                       (gnus-sorted-nunion
11381                        (gnus-sorted-intersection gnus-newsgroup-unreads
11382                                                  gnus-newsgroup-downloadable)
11383                        (gnus-sorted-difference gnus-newsgroup-unfetched
11384                                                gnus-newsgroup-cached))))
11385             ;; We actually mark all articles as canceled, which we
11386             ;; have to do when using auto-expiry or adaptive scoring.
11387             (gnus-summary-show-all-threads)
11388             (if (and to-here reverse)
11389                 (progn
11390                   (goto-char to-here)
11391                   (gnus-summary-mark-current-read-and-unread-as-read
11392                    gnus-catchup-mark)
11393                   (while (gnus-summary-find-next (not all))
11394                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11395               (when (gnus-summary-first-subject (not all))
11396                 (while (and
11397                         (if to-here (< (point) to-here) t)
11398                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11399                         (gnus-summary-find-next (not all))))))
11400             (gnus-set-mode-line 'summary))
11401           t))
11402     (gnus-summary-position-point)))
11403
11404 (defun gnus-summary-catchup-to-here (&optional all)
11405   "Mark all unticked articles before the current one as read.
11406 If ALL is non-nil, also mark ticked and dormant articles as read."
11407   (interactive "P")
11408   (save-excursion
11409     (gnus-save-hidden-threads
11410       (let ((beg (point)))
11411         ;; We check that there are unread articles.
11412         (when (or all (gnus-summary-find-prev))
11413           (gnus-summary-catchup all t beg)))))
11414   (gnus-summary-position-point))
11415
11416 (defun gnus-summary-catchup-from-here (&optional all)
11417   "Mark all unticked articles after (and including) the current one as read.
11418 If ALL is non-nil, also mark ticked and dormant articles as read."
11419   (interactive "P")
11420   (save-excursion
11421     (gnus-save-hidden-threads
11422       (let ((beg (point)))
11423         ;; We check that there are unread articles.
11424         (when (or all (gnus-summary-find-next))
11425           (gnus-summary-catchup all t beg nil t)))))
11426   (gnus-summary-position-point))
11427
11428 (defun gnus-summary-catchup-all (&optional quietly)
11429   "Mark all articles in this newsgroup as read.
11430 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11431 instead, which marks only unread articles as read."
11432   (interactive "P")
11433   (gnus-summary-catchup t quietly))
11434
11435 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11436   "Mark all unread articles in this group as read, then exit.
11437 If prefix argument ALL is non-nil, all articles are marked as read.
11438 If QUIETLY is non-nil, no questions will be asked."
11439   (interactive "P")
11440   (when (gnus-summary-catchup all quietly nil 'fast)
11441     ;; Select next newsgroup or exit.
11442     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11443              (eq gnus-auto-select-next 'quietly))
11444         (gnus-summary-next-group nil)
11445       (gnus-summary-exit))))
11446
11447 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11448   "Mark all articles in this newsgroup as read, and then exit.
11449 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11450 instead, which marks only unread articles as read."
11451   (interactive "P")
11452   (gnus-summary-catchup-and-exit t quietly))
11453
11454 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11455   "Mark all articles in this group as read and select the next group.
11456 If given a prefix, mark all articles, unread as well as ticked, as
11457 read."
11458   (interactive "P")
11459   (save-excursion
11460     (gnus-summary-catchup all))
11461   (gnus-summary-next-group))
11462
11463 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11464   "Mark all articles in this group as read and select the previous group.
11465 If given a prefix, mark all articles, unread as well as ticked, as
11466 read."
11467   (interactive "P")
11468   (save-excursion
11469     (gnus-summary-catchup all))
11470   (gnus-summary-next-group nil nil t))
11471
11472 ;;;
11473 ;;; with article
11474 ;;;
11475
11476 (defmacro gnus-with-article (article &rest forms)
11477   "Select ARTICLE and perform FORMS in the original article buffer.
11478 Then replace the article with the result."
11479   `(progn
11480      ;; We don't want the article to be marked as read.
11481      (let (gnus-mark-article-hook)
11482        (gnus-summary-select-article t t nil ,article))
11483      (set-buffer gnus-original-article-buffer)
11484      ,@forms
11485      (if (not (gnus-check-backend-function
11486                'request-replace-article (car gnus-article-current)))
11487          (gnus-message 5 "Read-only group; not replacing")
11488        (unless (gnus-request-replace-article
11489                 ,article (car gnus-article-current)
11490                 (current-buffer) t)
11491          (error "Couldn't replace article")))
11492      ;; The cache and backlog have to be flushed somewhat.
11493      (when gnus-keep-backlog
11494        (gnus-backlog-remove-article
11495         (car gnus-article-current) (cdr gnus-article-current)))
11496      (when gnus-use-cache
11497        (gnus-cache-update-article
11498         (car gnus-article-current) (cdr gnus-article-current)))))
11499
11500 (put 'gnus-with-article 'lisp-indent-function 1)
11501 (put 'gnus-with-article 'edebug-form-spec '(form body))
11502
11503 ;; Thread-based commands.
11504
11505 (defun gnus-summary-articles-in-thread (&optional article)
11506   "Return a list of all articles in the current thread.
11507 If ARTICLE is non-nil, return all articles in the thread that starts
11508 with that article."
11509   (let* ((article (or article (gnus-summary-article-number)))
11510          (data (gnus-data-find-list article))
11511          (top-level (gnus-data-level (car data)))
11512          (top-subject
11513           (cond ((null gnus-thread-operation-ignore-subject)
11514                  (gnus-simplify-subject-re
11515                   (mail-header-subject (gnus-data-header (car data)))))
11516                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11517                  (gnus-simplify-subject-fuzzy
11518                   (mail-header-subject (gnus-data-header (car data)))))
11519                 (t nil)))
11520          (end-point (save-excursion
11521                       (goto-char (gnus-data-pos (car data)))
11522                       (if (gnus-summary-go-to-next-thread)
11523                           (point) (point-max))))
11524          articles)
11525     (while (and data
11526                 (< (gnus-data-pos (car data)) end-point))
11527       (when (or (not top-subject)
11528                 (string= top-subject
11529                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11530                              (gnus-simplify-subject-fuzzy
11531                               (mail-header-subject
11532                                (gnus-data-header (car data))))
11533                            (gnus-simplify-subject-re
11534                             (mail-header-subject
11535                              (gnus-data-header (car data)))))))
11536         (push (gnus-data-number (car data)) articles))
11537       (unless (and (setq data (cdr data))
11538                    (> (gnus-data-level (car data)) top-level))
11539         (setq data nil)))
11540     ;; Return the list of articles.
11541     (nreverse articles)))
11542
11543 (defun gnus-summary-rethread-current ()
11544   "Rethread the thread the current article is part of."
11545   (interactive)
11546   (let* ((gnus-show-threads t)
11547          (article (gnus-summary-article-number))
11548          (id (mail-header-id (gnus-summary-article-header)))
11549          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11550     (unless id
11551       (error "No article on the current line"))
11552     (gnus-rebuild-thread id)
11553     (gnus-summary-goto-subject article)))
11554
11555 (defun gnus-summary-reparent-thread ()
11556   "Make the current article child of the marked (or previous) article.
11557
11558 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11559 is non-nil or the Subject: of both articles are the same."
11560   (interactive)
11561   (unless (not (gnus-group-read-only-p))
11562     (error "The current newsgroup does not support article editing"))
11563   (unless (<= (length gnus-newsgroup-processable) 1)
11564     (error "No more than one article may be marked"))
11565   (let ((child (gnus-summary-article-number))
11566         ;; First grab the marked article, otherwise one line up.
11567         (parent (if (not (null gnus-newsgroup-processable))
11568                     (car gnus-newsgroup-processable)
11569                   (save-excursion
11570                     (if (eq (forward-line -1) 0)
11571                         (gnus-summary-article-number)
11572                       (error "Beginning of summary buffer"))))))
11573     (gnus-summary-reparent-children parent (list child))))
11574
11575 (defun gnus-summary-reparent-children (parent children)
11576   "Make PARENT the parent of CHILDREN.
11577 When called interactively, PARENT is the current article and CHILDREN
11578 are the process-marked articles."
11579   (interactive
11580    (list (gnus-summary-article-number)
11581          (gnus-summary-work-articles nil)))
11582   (dolist (child children)
11583     (save-window-excursion
11584       (let ((gnus-article-buffer " *reparent*"))
11585         (unless (not (eq parent child))
11586           (error "An article may not be self-referential"))
11587         (let ((message-id (mail-header-id
11588                            (gnus-summary-article-header parent))))
11589           (unless (and message-id (not (equal message-id "")))
11590             (error "No message-id in desired parent"))
11591           (gnus-with-article child
11592             (save-restriction
11593               (goto-char (point-min))
11594               (message-narrow-to-head)
11595               (if (re-search-forward "^References: " nil t)
11596                   (progn
11597                     (re-search-forward "^[^ \t]" nil t)
11598                     (forward-line -1)
11599                     (end-of-line)
11600                     (insert " " message-id))
11601                 (insert "References: " message-id "\n"))))
11602           (set-buffer gnus-summary-buffer)
11603           (gnus-summary-unmark-all-processable)
11604           (gnus-summary-update-article child)
11605           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11606             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11607           (gnus-summary-rethread-current)
11608           (gnus-message 3 "Article %d is now the child of article %d"
11609                         child parent))))))
11610
11611 (defun gnus-summary-toggle-threads (&optional arg)
11612   "Toggle showing conversation threads.
11613 If ARG is positive number, turn showing conversation threads on."
11614   (interactive "P")
11615   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11616     (setq gnus-show-threads
11617           (if (null arg) (not gnus-show-threads)
11618             (> (prefix-numeric-value arg) 0)))
11619     (gnus-summary-prepare)
11620     (gnus-summary-goto-subject current)
11621     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11622     (gnus-summary-position-point)))
11623
11624 (eval-and-compile
11625   (if (fboundp 'remove-overlays)
11626       (defalias 'gnus-remove-overlays 'remove-overlays)
11627     (defun gnus-remove-overlays (beg end name val)
11628       "Clear BEG and END of overlays whose property NAME has value VAL.
11629 For compatibility with XEmacs."
11630       (dolist (ov (gnus-overlays-in beg end))
11631         (when (eq (gnus-overlay-get ov name) val)
11632           (gnus-delete-overlay ov))))))
11633
11634 (defun gnus-summary-show-all-threads ()
11635   "Show all threads."
11636   (interactive)
11637   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11638   (gnus-summary-position-point))
11639
11640 (defsubst gnus-summary--inv (p)
11641   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11642
11643 (defun gnus-summary-show-thread ()
11644   "Show thread subtrees.
11645 Returns nil if no thread was there to be shown."
11646   (interactive)
11647   (let* ((orig (point))
11648          (end (point-at-eol))
11649          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11650          ;; Leave point at bol
11651          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11652          (eoi (when end
11653                 (if (fboundp 'next-single-char-property-change)
11654                     ;; Note: XEmacs version of n-s-c-p-c may return nil
11655                     (or (next-single-char-property-change end 'invisible)
11656                         (point-max))
11657                   (while (progn
11658                            (end-of-line 2)
11659                            (and (not (eobp))
11660                                 (eq (get-char-property (point) 'invisible)
11661                                     'gnus-sum))))
11662                   (point)))))
11663     (when eoi
11664       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11665       (goto-char orig)
11666       (gnus-summary-position-point)
11667       eoi)))
11668
11669 (defun gnus-summary-maybe-hide-threads ()
11670   "If requested, hide the threads that should be hidden."
11671   (when (and gnus-show-threads
11672              gnus-thread-hide-subtree)
11673     (gnus-summary-hide-all-threads
11674      (if (or (consp gnus-thread-hide-subtree)
11675              (functionp gnus-thread-hide-subtree))
11676          (gnus-make-predicate gnus-thread-hide-subtree)
11677        nil))))
11678
11679 ;;; Hiding predicates.
11680
11681 (defun gnus-article-unread-p (header)
11682   (memq (mail-header-number header) gnus-newsgroup-unreads))
11683
11684 (defun gnus-article-unseen-p (header)
11685   (memq (mail-header-number header) gnus-newsgroup-unseen))
11686
11687 (defun gnus-map-articles (predicate articles)
11688   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11689   (apply 'gnus-or (mapcar predicate
11690                           (mapcar (lambda (number)
11691                                     (gnus-summary-article-header number))
11692                                   articles))))
11693
11694 (defun gnus-summary-hide-all-threads (&optional predicate)
11695   "Hide all thread subtrees.
11696 If PREDICATE is supplied, threads that satisfy this predicate
11697 will not be hidden."
11698   (interactive)
11699   (save-excursion
11700     (goto-char (point-min))
11701     (let ((end nil)
11702           (count 0))
11703       (while (not end)
11704         (incf count)
11705         (when (zerop (mod count 1000))
11706           (message "Hiding all threads... %d" count))
11707         (when (or (not predicate)
11708                   (gnus-map-articles
11709                    predicate (gnus-summary-article-children)))
11710             (gnus-summary-hide-thread))
11711         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11712   (gnus-summary-position-point))
11713
11714 (defun gnus-summary-hide-thread ()
11715   "Hide thread subtrees.
11716 If PREDICATE is supplied, threads that satisfy this predicate
11717 will not be hidden.
11718 Returns nil if no threads were there to be hidden."
11719   (interactive)
11720   (beginning-of-line)
11721   (let ((start (point))
11722         (starteol (line-end-position))
11723         (article (gnus-summary-article-number)))
11724     ;; Go forward until either the buffer ends or the subthread ends.
11725     (when (and (not (eobp))
11726                (or (zerop (gnus-summary-next-thread 1 t))
11727                    (goto-char (point-max))))
11728       (if (and (> (point) start)
11729                ;; FIXME: this should actually search for a non-invisible \n.
11730                (search-backward "\n" start t))
11731           (progn
11732             (when (> (point) starteol)
11733               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11734               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11735                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11736                 (gnus-overlay-put ol 'evaporate t)))
11737             (gnus-summary-goto-subject article)
11738             (when (> start (point))
11739               (message "Hiding the thread moved us backwards, aborting!")
11740               (goto-char (point-max))))
11741         (goto-char start)
11742         nil))))
11743
11744 (defun gnus-summary-go-to-next-thread (&optional previous)
11745   "Go to the same level (or less) next thread.
11746 If PREVIOUS is non-nil, go to previous thread instead.
11747 Return the article number moved to, or nil if moving was impossible."
11748   (let ((level (gnus-summary-thread-level))
11749         (way (if previous -1 1))
11750         (beg (point)))
11751     (forward-line way)
11752     (while (and (not (eobp))
11753                 (< level (gnus-summary-thread-level)))
11754       (forward-line way))
11755     (if (eobp)
11756         (progn
11757           (goto-char beg)
11758           nil)
11759       (setq beg (point))
11760       (prog1
11761           (gnus-summary-article-number)
11762         (goto-char beg)))))
11763
11764 (defun gnus-summary-next-thread (n &optional silent)
11765   "Go to the same level next N'th thread.
11766 If N is negative, search backward instead.
11767 Returns the difference between N and the number of skips actually
11768 done.
11769
11770 If SILENT, don't output messages."
11771   (interactive "p")
11772   (let ((backward (< n 0))
11773         (n (abs n)))
11774     (while (and (> n 0)
11775                 (gnus-summary-go-to-next-thread backward))
11776       (decf n))
11777     (unless silent
11778       (gnus-summary-position-point))
11779     (when (and (not silent) (/= 0 n))
11780       (gnus-message 7 "No more threads"))
11781     n))
11782
11783 (defun gnus-summary-prev-thread (n)
11784   "Go to the same level previous N'th thread.
11785 Returns the difference between N and the number of skips actually
11786 done."
11787   (interactive "p")
11788   (gnus-summary-next-thread (- n)))
11789
11790 (defun gnus-summary-go-down-thread ()
11791   "Go down one level in the current thread."
11792   (let ((children (gnus-summary-article-children)))
11793     (when children
11794       (gnus-summary-goto-subject (car children)))))
11795
11796 (defun gnus-summary-go-up-thread ()
11797   "Go up one level in the current thread."
11798   (let ((parent (gnus-summary-article-parent)))
11799     (when parent
11800       (gnus-summary-goto-subject parent))))
11801
11802 (defun gnus-summary-down-thread (n)
11803   "Go down thread N steps.
11804 If N is negative, go up instead.
11805 Returns the difference between N and how many steps down that were
11806 taken."
11807   (interactive "p")
11808   (let ((up (< n 0))
11809         (n (abs n)))
11810     (while (and (> n 0)
11811                 (if up (gnus-summary-go-up-thread)
11812                   (gnus-summary-go-down-thread)))
11813       (setq n (1- n)))
11814     (gnus-summary-position-point)
11815     (when (/= 0 n)
11816       (gnus-message 7 "Can't go further"))
11817     n))
11818
11819 (defun gnus-summary-up-thread (n)
11820   "Go up thread N steps.
11821 If N is negative, go down instead.
11822 Returns the difference between N and how many steps down that were
11823 taken."
11824   (interactive "p")
11825   (gnus-summary-down-thread (- n)))
11826
11827 (defun gnus-summary-top-thread ()
11828   "Go to the top of the thread."
11829   (interactive)
11830   (while (gnus-summary-go-up-thread))
11831   (gnus-summary-article-number))
11832
11833 (defun gnus-summary-expire-thread ()
11834   "Mark articles under current thread as expired."
11835   (interactive)
11836   (gnus-summary-kill-thread 0))
11837
11838 (defun gnus-summary-kill-thread (&optional unmark)
11839   "Mark articles under current thread as read.
11840 If the prefix argument is positive, remove any kinds of marks.
11841 If the prefix argument is zero, mark thread as expired.
11842 If the prefix argument is negative, tick articles instead."
11843   (interactive "P")
11844   (when unmark
11845     (setq unmark (prefix-numeric-value unmark)))
11846   (let ((articles (gnus-summary-articles-in-thread))
11847         (hide (or (null unmark) (= unmark 0))))
11848     (save-excursion
11849       ;; Expand the thread.
11850       (gnus-summary-show-thread)
11851       ;; Mark all the articles.
11852       (while articles
11853         (gnus-summary-goto-subject (car articles))
11854         (cond ((null unmark)
11855                (gnus-summary-mark-article-as-read gnus-killed-mark))
11856               ((> unmark 0)
11857                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11858               ((= unmark 0)
11859                (gnus-summary-mark-article nil gnus-expirable-mark))
11860               (t
11861                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11862         (setq articles (cdr articles))))
11863     ;; Hide killed subtrees when hide is true.
11864     (and hide
11865          gnus-thread-hide-killed
11866          (gnus-summary-hide-thread))
11867     ;; If hide is t, go to next unread subject.
11868     (when hide
11869       ;; Go to next unread subject.
11870       (gnus-summary-next-subject 1 t)))
11871   (gnus-set-mode-line 'summary))
11872
11873 ;; Summary sorting commands
11874
11875 (defun gnus-summary-sort-by-number (&optional reverse)
11876   "Sort the summary buffer by article number.
11877 Argument REVERSE means reverse order."
11878   (interactive "P")
11879   (gnus-summary-sort 'number reverse))
11880
11881 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11882   "Sort the summary buffer by most recent article number.
11883 Argument REVERSE means reverse order."
11884   (interactive "P")
11885   (gnus-summary-sort 'most-recent-number reverse))
11886
11887 (defun gnus-summary-sort-by-random (&optional reverse)
11888   "Randomize the order in the summary buffer.
11889 Argument REVERSE means to randomize in reverse order."
11890   (interactive "P")
11891   (gnus-summary-sort 'random reverse))
11892
11893 (defun gnus-summary-sort-by-author (&optional reverse)
11894   "Sort the summary buffer by author name alphabetically.
11895 If `case-fold-search' is non-nil, case of letters is ignored.
11896 Argument REVERSE means reverse order."
11897   (interactive "P")
11898   (gnus-summary-sort 'author reverse))
11899
11900 (defun gnus-summary-sort-by-recipient (&optional reverse)
11901   "Sort the summary buffer by recipient name alphabetically.
11902 If `case-fold-search' is non-nil, case of letters is ignored.
11903 Argument REVERSE means reverse order."
11904   (interactive "P")
11905   (gnus-summary-sort 'recipient reverse))
11906
11907 (defun gnus-summary-sort-by-subject (&optional reverse)
11908   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11909 If `case-fold-search' is non-nil, case of letters is ignored.
11910 Argument REVERSE means reverse order."
11911   (interactive "P")
11912   (gnus-summary-sort 'subject reverse))
11913
11914 (defun gnus-summary-sort-by-date (&optional reverse)
11915   "Sort the summary buffer by date.
11916 Argument REVERSE means reverse order."
11917   (interactive "P")
11918   (gnus-summary-sort 'date reverse))
11919
11920 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11921   "Sort the summary buffer by most recent date.
11922 Argument REVERSE means reverse order."
11923   (interactive "P")
11924   (gnus-summary-sort 'most-recent-date reverse))
11925
11926 (defun gnus-summary-sort-by-score (&optional reverse)
11927   "Sort the summary buffer by score.
11928 Argument REVERSE means reverse order."
11929   (interactive "P")
11930   (gnus-summary-sort 'score reverse))
11931
11932 (defun gnus-summary-sort-by-lines (&optional reverse)
11933   "Sort the summary buffer by the number of lines.
11934 Argument REVERSE means reverse order."
11935   (interactive "P")
11936   (gnus-summary-sort 'lines reverse))
11937
11938 (defun gnus-summary-sort-by-chars (&optional reverse)
11939   "Sort the summary buffer by article length.
11940 Argument REVERSE means reverse order."
11941   (interactive "P")
11942   (gnus-summary-sort 'chars reverse))
11943
11944 (defun gnus-summary-sort-by-original (&optional reverse)
11945   "Sort the summary buffer using the default sorting method.
11946 Argument REVERSE means reverse order."
11947   (interactive "P")
11948   (let* ((inhibit-read-only t)
11949          (gnus-summary-prepare-hook nil))
11950     ;; We do the sorting by regenerating the threads.
11951     (gnus-summary-prepare)
11952     ;; Hide subthreads if needed.
11953     (gnus-summary-maybe-hide-threads)))
11954
11955 (defun gnus-summary-sort (predicate reverse)
11956   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11957   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11958          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11959          (gnus-thread-sort-functions
11960           (if (not reverse)
11961               thread
11962             `(lambda (t1 t2)
11963                (,thread t2 t1))))
11964          (gnus-sort-gathered-threads-function
11965           gnus-thread-sort-functions)
11966          (gnus-article-sort-functions
11967           (if (not reverse)
11968               article
11969             `(lambda (t1 t2)
11970                (,article t2 t1))))
11971          (inhibit-read-only t)
11972          (gnus-summary-prepare-hook nil))
11973     ;; We do the sorting by regenerating the threads.
11974     (gnus-summary-prepare)
11975     ;; Hide subthreads if needed.
11976     (gnus-summary-maybe-hide-threads)))
11977
11978 ;; Summary saving commands.
11979
11980 (defun gnus-summary-save-article (&optional n not-saved)
11981   "Save the current article using the default saver function.
11982 If N is a positive number, save the N next articles.
11983 If N is a negative number, save the N previous articles.
11984 If N is nil and any articles have been marked with the process mark,
11985 save those articles instead.
11986 The variable `gnus-default-article-saver' specifies the saver function.
11987
11988 If the optional second argument NOT-SAVED is non-nil, articles saved
11989 will not be marked as saved."
11990   (interactive "P")
11991   (require 'gnus-art)
11992   (let* ((articles (gnus-summary-work-articles n))
11993          (save-buffer (save-excursion
11994                         (nnheader-set-temp-buffer " *Gnus Save*")))
11995          (num (length articles))
11996          ;; Whether to save decoded articles or raw articles.
11997          (decode (when gnus-article-save-coding-system
11998                    (get gnus-default-article-saver :decode)))
11999          ;; When saving many articles in a single file, use the other
12000          ;; function to save articles other than the first one.
12001          (saver2 (get gnus-default-article-saver :function))
12002          (gnus-prompt-before-saving (if saver2
12003                                         t
12004                                       gnus-prompt-before-saving))
12005          (gnus-default-article-saver gnus-default-article-saver)
12006          header file)
12007     (dolist (article articles)
12008       (setq header (gnus-summary-article-header article))
12009       (if (not (vectorp header))
12010           ;; This is a pseudo-article.
12011           (if (assq 'name header)
12012               (gnus-copy-file (cdr (assq 'name header)))
12013             (gnus-message 1 "Article %d is unsavable" article))
12014         ;; This is a real article.
12015         (save-window-excursion
12016           (gnus-summary-select-article decode decode nil article)
12017           (gnus-summary-goto-subject article))
12018         (with-current-buffer save-buffer
12019           (erase-buffer)
12020           (insert-buffer-substring (if decode
12021                                        gnus-article-buffer
12022                                      gnus-original-article-buffer)))
12023         (setq file (gnus-article-save save-buffer file num))
12024         (gnus-summary-remove-process-mark article)
12025         (unless not-saved
12026           (gnus-summary-set-saved-mark article)))
12027       (when saver2
12028         (setq gnus-default-article-saver saver2
12029               saver2 nil)))
12030     (gnus-kill-buffer save-buffer)
12031     (gnus-summary-position-point)
12032     (gnus-set-mode-line 'summary)
12033     n))
12034
12035 (declare-function gnus-summary-save-in-pipe "gnus-art" (&optional command raw))
12036
12037 (defun gnus-summary-pipe-output (&optional n sym)
12038   "Pipe the current article to a subprocess.
12039 If N is a positive number, pipe the N next articles.
12040 If N is a negative number, pipe the N previous articles.
12041 If N is nil and any articles have been marked with the process mark,
12042 pipe those articles instead.
12043 The default command to which articles are piped is specified by the
12044 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
12045 will be prompted for the command.
12046
12047 The properties `:decode' and `:headers' that are put to the function
12048 symbol `gnus-summary-save-in-pipe' control whether this function
12049 decodes articles and what headers to keep (see the doc string for the
12050 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
12051 is neither omitted nor the symbol `r', force including all headers
12052 regardless of the `:headers' property.  If it is the symbol `r',
12053 articles that are not decoded and include all headers will be piped
12054 no matter what the properties `:decode' and `:headers' are."
12055   (interactive (gnus-interactive "P\ny"))
12056   (require 'gnus-art)
12057   (let* ((articles (gnus-summary-work-articles n))
12058          (result-buffer "*Shell Command Output*")
12059          (all-headers (not (memq sym '(nil r))))
12060          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
12061          (raw (eq sym 'r))
12062          (headers (get 'gnus-summary-save-in-pipe :headers))
12063          command result)
12064     (unless (numberp (car articles))
12065       (error "No article to pipe"))
12066     (setq command (gnus-read-shell-command
12067                    (concat "Shell command on "
12068                            (if (cdr articles)
12069                                (format "these %d articles" (length articles))
12070                              "this article")
12071                            ": ")
12072                    gnus-summary-pipe-output-default-command))
12073     (when (string-equal command "")
12074       (error "A command is required"))
12075     (when all-headers
12076       (put 'gnus-summary-save-in-pipe :headers nil))
12077     (unwind-protect
12078         (while articles
12079           (gnus-summary-goto-subject (pop articles))
12080           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12081           (when (and (get-buffer result-buffer)
12082                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12083             (setq result (concat result (with-current-buffer result-buffer
12084                                           (buffer-string))))))
12085       (put 'gnus-summary-save-in-pipe :headers headers))
12086     (unless (zerop (length result))
12087       (if (with-current-buffer (get-buffer-create result-buffer)
12088             (erase-buffer)
12089             (insert result)
12090             (prog1
12091                 (and (= (count-lines (point-min) (point)) 1)
12092                      (progn
12093                        (end-of-line 0)
12094                        (<= (current-column)
12095                            (window-width (minibuffer-window)))))
12096               (goto-char (point-min))))
12097           (message "%s" (substring result 0 -1))
12098         (message nil)
12099         (gnus-configure-windows 'pipe)))))
12100
12101 (defun gnus-summary-save-article-mail (&optional arg)
12102   "Append the current article to a Unix mail box file.
12103 If N is a positive number, save the N next articles.
12104 If N is a negative number, save the N previous articles.
12105 If N is nil and any articles have been marked with the process mark,
12106 save those articles instead."
12107   (interactive "P")
12108   (require 'gnus-art)
12109   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12110     (gnus-summary-save-article arg)))
12111
12112 (defun gnus-summary-save-article-rmail (&optional arg)
12113   "Append the current article to an rmail file.
12114 If N is a positive number, save the N next articles.
12115 If N is a negative number, save the N previous articles.
12116 If N is nil and any articles have been marked with the process mark,
12117 save those articles instead."
12118   (interactive "P")
12119   (require 'gnus-art)
12120   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12121     (gnus-summary-save-article arg)))
12122
12123 (defun gnus-summary-save-article-file (&optional arg)
12124   "Append the current article to a file.
12125 If N is a positive number, save the N next articles.
12126 If N is a negative number, save the N previous articles.
12127 If N is nil and any articles have been marked with the process mark,
12128 save those articles instead."
12129   (interactive "P")
12130   (require 'gnus-art)
12131   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12132     (gnus-summary-save-article arg)))
12133
12134 (defun gnus-summary-write-article-file (&optional arg)
12135   "Write the current article to a file, deleting the previous file.
12136 If N is a positive number, save the N next articles.
12137 If N is a negative number, save the N previous articles.
12138 If N is nil and any articles have been marked with the process mark,
12139 save those articles instead."
12140   (interactive "P")
12141   (require 'gnus-art)
12142   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12143     (gnus-summary-save-article arg)))
12144
12145 (defun gnus-summary-save-article-body-file (&optional arg)
12146   "Append the current article body to a file.
12147 If N is a positive number, save the N next articles.
12148 If N is a negative number, save the N previous articles.
12149 If N is nil and any articles have been marked with the process mark,
12150 save those articles instead."
12151   (interactive "P")
12152   (require 'gnus-art)
12153   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12154     (gnus-summary-save-article arg)))
12155
12156 (defun gnus-summary-write-article-body-file (&optional arg)
12157   "Write the current article body to a file, deleting the previous file.
12158 If N is a positive number, save the N next articles.
12159 If N is a negative number, save the N previous articles.
12160 If N is nil and any articles have been marked with the process mark,
12161 save those articles instead."
12162   (interactive "P")
12163   (require 'gnus-art)
12164   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12165     (gnus-summary-save-article arg)))
12166
12167 (defun gnus-summary-muttprint (&optional arg)
12168   "Print the current article using Muttprint.
12169 If N is a positive number, save the N next articles.
12170 If N is a negative number, save the N previous articles.
12171 If N is nil and any articles have been marked with the process mark,
12172 save those articles instead."
12173   (interactive "P")
12174   (require 'gnus-art)
12175   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12176     (gnus-summary-save-article arg t)))
12177
12178 (defun gnus-summary-pipe-message (program)
12179   "Pipe the current article through PROGRAM."
12180   (interactive "sProgram: ")
12181   (gnus-summary-select-article)
12182   (let ((mail-header-separator ""))
12183     (gnus-eval-in-buffer-window gnus-article-buffer
12184       (save-restriction
12185         (widen)
12186         (let ((start (window-start))
12187               (inhibit-read-only t))
12188           (message-pipe-buffer-body program)
12189           (set-window-start (get-buffer-window (current-buffer)) start))))))
12190
12191 (defun gnus-get-split-value (methods)
12192   "Return a value based on the split METHODS."
12193   (let (split-name method result match)
12194     (when methods
12195       (with-current-buffer gnus-original-article-buffer
12196         (save-restriction
12197           (nnheader-narrow-to-headers)
12198           (while (and methods (not split-name))
12199             (goto-char (point-min))
12200             (setq method (pop methods))
12201             (setq match (car method))
12202             (when (cond
12203                    ((stringp match)
12204                     ;; Regular expression.
12205                     (ignore-errors
12206                       (re-search-forward match nil t)))
12207                    ((functionp match)
12208                     ;; Function.
12209                     (save-restriction
12210                       (widen)
12211                       (setq result (funcall match gnus-newsgroup-name))))
12212                    ((consp match)
12213                     ;; Form.
12214                     (save-restriction
12215                       (widen)
12216                       (setq result (eval match)))))
12217               (setq split-name (cdr method))
12218               (cond ((stringp result)
12219                      (push (expand-file-name
12220                             result gnus-article-save-directory)
12221                            split-name))
12222                     ((consp result)
12223                      (setq split-name (append result split-name)))))))))
12224     (nreverse split-name)))
12225
12226 (defun gnus-valid-move-group-p (group)
12227   (and (symbolp group)
12228        (boundp group)
12229        (symbol-name group)
12230        (symbol-value group)
12231        (gnus-get-function (gnus-find-method-for-group
12232                            (symbol-name group)) 'request-accept-article t)))
12233
12234 (defun gnus-read-move-group-name (prompt default articles prefix)
12235   "Read a group name."
12236   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12237          (minibuffer-confirm-incomplete nil) ; XEmacs
12238          (prom
12239           (format "%s %s to"
12240                   prompt
12241                   (if (> (length articles) 1)
12242                       (format "these %d articles" (length articles))
12243                     "this article")))
12244          (to-newsgroup
12245           (cond
12246            ((null split-name)
12247             (gnus-group-completing-read
12248              prom
12249              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12250              nil prefix nil default))
12251            ((= 1 (length split-name))
12252             (gnus-group-completing-read
12253              prom
12254              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12255              nil prefix 'gnus-group-history (car split-name)))
12256            (t
12257             (gnus-completing-read
12258              prom (nreverse split-name) nil nil 'gnus-group-history))))
12259          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12260          encoded)
12261     (when to-newsgroup
12262       (if (or (string= to-newsgroup "")
12263               (string= to-newsgroup prefix))
12264           (setq to-newsgroup default))
12265       (unless to-newsgroup
12266         (error "No group name entered"))
12267       (setq encoded (mm-encode-coding-string
12268                      to-newsgroup
12269                      (gnus-group-name-charset to-method to-newsgroup)))
12270       (or (gnus-active encoded)
12271           (gnus-activate-group encoded nil nil to-method)
12272           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12273                                      to-newsgroup))
12274               (or (and (gnus-request-create-group encoded to-method)
12275                        (gnus-activate-group encoded nil nil to-method)
12276                        (gnus-subscribe-group encoded))
12277                   (error "Couldn't create group %s" to-newsgroup)))
12278           (error "No such group: %s" to-newsgroup))
12279       encoded)))
12280
12281 (defvar gnus-summary-save-parts-counter)
12282 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12283
12284 (defun gnus-summary-save-parts (type dir n &optional reverse)
12285   "Save parts matching TYPE to DIR.
12286 If REVERSE, save parts that do not match TYPE."
12287   (interactive
12288    (list (read-string "Save parts of type: "
12289                       (or (car gnus-summary-save-parts-type-history)
12290                           gnus-summary-save-parts-default-mime)
12291                       'gnus-summary-save-parts-type-history)
12292          (setq gnus-summary-save-parts-last-directory
12293                (read-directory-name "Save to directory: "
12294                                     gnus-summary-save-parts-last-directory
12295                                     nil t))
12296          current-prefix-arg))
12297   (gnus-summary-iterate n
12298     (let ((gnus-display-mime-function nil)
12299           gnus-article-prepare-hook
12300           gnus-article-decode-hook
12301           gnus-display-mime-function
12302           gnus-break-pages
12303           (gnus-inhibit-treatment t))
12304       (gnus-summary-select-article))
12305     (with-current-buffer gnus-article-buffer
12306       (let ((handles (or gnus-article-mime-handles
12307                          (mm-dissect-buffer nil gnus-article-loose-mime)
12308                          (and gnus-article-emulate-mime
12309                               (mm-uu-dissect))))
12310             (gnus-summary-save-parts-counter 1))
12311         (when handles
12312           (gnus-summary-save-parts-1 type dir handles reverse)
12313           (unless gnus-article-mime-handles ;; Don't destroy this case.
12314             (mm-destroy-parts handles)))))))
12315
12316 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12317   (if (stringp (car handle))
12318       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12319               (cdr handle))
12320     (when (if reverse
12321               (not (string-match type (mm-handle-media-type handle)))
12322             (string-match type (mm-handle-media-type handle)))
12323       (let ((file (expand-file-name
12324                    (gnus-map-function
12325                     mm-file-name-rewrite-functions
12326                     (file-name-nondirectory
12327                      (or
12328                       (mm-handle-filename handle)
12329                       (format "%s.%d.%d" gnus-newsgroup-name
12330                               (cdr gnus-article-current)
12331                               gnus-summary-save-parts-counter))))
12332                    dir)))
12333         (incf gnus-summary-save-parts-counter)
12334         (unless (file-exists-p file)
12335           (mm-save-part-to-file handle file))))))
12336
12337 ;; Summary extract commands
12338
12339 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12340   (let ((inhibit-read-only t)
12341         (article (gnus-summary-article-number))
12342         after-article b e)
12343     (unless (gnus-summary-goto-subject article)
12344       (error "No such article: %d" article))
12345     (gnus-summary-position-point)
12346     ;; If all commands are to be bunched up on one line, we collect
12347     ;; them here.
12348     (unless gnus-view-pseudos-separately
12349       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12350             files action)
12351         (while ps
12352           (setq action (cdr (assq 'action (car ps))))
12353           (setq files (list (cdr (assq 'name (car ps)))))
12354           (while (and ps (cdr ps)
12355                       (string= (or action "1")
12356                                (or (cdr (assq 'action (cadr ps))) "2")))
12357             (push (cdr (assq 'name (cadr ps))) files)
12358             (setcdr ps (cddr ps)))
12359           (when files
12360             (when (not (string-match "%s" action))
12361               (push " " files))
12362             (push " " files)
12363             (when (assq 'execute (car ps))
12364               (setcdr (assq 'execute (car ps))
12365                       (funcall (if (string-match "%s" action)
12366                                    'format 'concat)
12367                                action
12368                                (mapconcat
12369                                 (lambda (f)
12370                                   (if (equal f " ")
12371                                       f
12372                                     (shell-quote-argument f)))
12373                                 files " ")))))
12374           (setq ps (cdr ps)))))
12375     (if (and gnus-view-pseudos (not not-view))
12376         (while pslist
12377           (when (assq 'execute (car pslist))
12378             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12379                                   (eq gnus-view-pseudos 'not-confirm)))
12380           (setq pslist (cdr pslist)))
12381       (save-excursion
12382         (while pslist
12383           (setq after-article (or (cdr (assq 'article (car pslist)))
12384                                   (gnus-summary-article-number)))
12385           (gnus-summary-goto-subject after-article)
12386           (forward-line 1)
12387           (setq b (point))
12388           (insert "    " (file-name-nondirectory
12389                           (cdr (assq 'name (car pslist))))
12390                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12391           (setq e (point))
12392           (forward-line -1)             ; back to `b'
12393           (gnus-add-text-properties
12394            b (1- e) (list 'gnus-number gnus-reffed-article-number
12395                           gnus-mouse-face-prop gnus-mouse-face))
12396           (gnus-data-enter
12397            after-article gnus-reffed-article-number
12398            gnus-unread-mark b (car pslist) 0 (- e b))
12399           (setq gnus-newsgroup-unreads
12400                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12401                                          gnus-reffed-article-number))
12402           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12403           (setq pslist (cdr pslist)))))))
12404
12405 (defun gnus-pseudos< (p1 p2)
12406   (let ((c1 (cdr (assq 'action p1)))
12407         (c2 (cdr (assq 'action p2))))
12408     (and c1 c2 (string< c1 c2))))
12409
12410 (defun gnus-request-pseudo-article (props)
12411   (cond ((assq 'execute props)
12412          (gnus-execute-command (cdr (assq 'execute props)))))
12413   (let ((gnus-current-article (gnus-summary-article-number)))
12414     (gnus-run-hooks 'gnus-mark-article-hook)))
12415
12416 (defun gnus-execute-command (command &optional automatic)
12417   (save-excursion
12418     (gnus-article-setup-buffer)
12419     (set-buffer gnus-article-buffer)
12420     (setq buffer-read-only nil)
12421     (let ((command (if automatic command
12422                      (read-string "Command: " (cons command 0)))))
12423       (erase-buffer)
12424       (insert "$ " command "\n\n")
12425       (if gnus-view-pseudo-asynchronously
12426           (start-process "gnus-execute" (current-buffer) shell-file-name
12427                          shell-command-switch command)
12428         (call-process shell-file-name nil t nil
12429                       shell-command-switch command)))))
12430
12431 ;; Summary kill commands.
12432
12433 (defun gnus-summary-edit-global-kill (article)
12434   "Edit the \"global\" kill file."
12435   (interactive (list (gnus-summary-article-number)))
12436   (gnus-group-edit-global-kill article))
12437
12438 (defun gnus-summary-edit-local-kill ()
12439   "Edit a local kill file applied to the current newsgroup."
12440   (interactive)
12441   (setq gnus-current-headers (gnus-summary-article-header))
12442   (gnus-group-edit-local-kill
12443    (gnus-summary-article-number) gnus-newsgroup-name))
12444
12445 ;;; Header reading.
12446
12447 (defun gnus-read-header (id &optional header)
12448   "Read the headers of article ID and enter them into the Gnus system."
12449   (let ((group gnus-newsgroup-name)
12450         (gnus-override-method
12451          (or
12452           gnus-override-method
12453           (and (gnus-news-group-p gnus-newsgroup-name)
12454                (car (gnus-refer-article-methods)))))
12455         where)
12456     ;; First we check to see whether the header in question is already
12457     ;; fetched.
12458     (if (stringp id)
12459         ;; This is a Message-ID.
12460         (setq header (or header (gnus-id-to-header id)))
12461       ;; This is an article number.
12462       (setq header (or header (gnus-summary-article-header id))))
12463     (if (and header
12464              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12465         ;; We have found the header.
12466         header
12467       ;; We have to really fetch the header to this article.
12468       (with-current-buffer nntp-server-buffer
12469         (when (setq where (gnus-request-head id group))
12470           (nnheader-fold-continuation-lines)
12471           (goto-char (point-max))
12472           (insert ".\n")
12473           (goto-char (point-min))
12474           (insert "211 ")
12475           (princ (cond
12476                   ((numberp id) id)
12477                   ((cdr where) (cdr where))
12478                   (header (mail-header-number header))
12479                   (t gnus-reffed-article-number))
12480                  (current-buffer))
12481           (insert " Article retrieved.\n"))
12482         (if (or (not where)
12483                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12484             ()                          ; Malformed head.
12485           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12486             (when (and (bound-and-true-p gnus-registry-enabled)
12487                        (not (gnus-ephemeral-group-p (car where))))
12488               (gnus-registry-handle-action
12489                (mail-header-id header) nil
12490                (gnus-group-prefixed-name
12491                 (car where)
12492                 (or gnus-override-method (gnus-find-method-for-group group)))
12493                (mail-header-subject header)
12494                (mail-header-from header)))
12495             (when (and (stringp id)
12496                        (or
12497                         (not (string= (gnus-group-real-name group)
12498                                       (car where)))
12499                         (not (gnus-server-equal gnus-override-method
12500                                                 (gnus-group-method group)))))
12501               ;; If we fetched by Message-ID and the article came from
12502               ;; a different group (or server), we fudge some bogus
12503               ;; article numbers for this article.
12504               (mail-header-set-number header gnus-reffed-article-number))
12505             (with-current-buffer gnus-summary-buffer
12506               (decf gnus-reffed-article-number)
12507               (gnus-remove-header (mail-header-number header))
12508               (push header gnus-newsgroup-headers)
12509               (setq gnus-current-headers header)
12510               (push (mail-header-number header) gnus-newsgroup-limit)))
12511           header)))))
12512
12513 (defun gnus-remove-header (number)
12514   "Remove header NUMBER from `gnus-newsgroup-headers'."
12515   (if (and gnus-newsgroup-headers
12516            (= number (mail-header-number (car gnus-newsgroup-headers))))
12517       (pop gnus-newsgroup-headers)
12518     (let ((headers gnus-newsgroup-headers))
12519       (while (and (cdr headers)
12520                   (not (= number (mail-header-number (cadr headers)))))
12521         (pop headers))
12522       (when (cdr headers)
12523         (setcdr headers (cddr headers))))))
12524
12525 ;;;
12526 ;;; summary highlights
12527 ;;;
12528
12529 (defun gnus-highlight-selected-summary ()
12530   "Highlight selected article in summary buffer."
12531   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12532   (when gnus-summary-selected-face
12533     (save-excursion
12534       (let* ((beg (point-at-bol))
12535              (end (point-at-eol))
12536              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12537              (from (if (get-text-property beg gnus-mouse-face-prop)
12538                        beg
12539                      (or (next-single-property-change
12540                           beg gnus-mouse-face-prop nil end)
12541                          beg)))
12542              (to
12543               (if (= from end)
12544                   (- from 2)
12545                 (or (next-single-property-change
12546                      from gnus-mouse-face-prop nil end)
12547                     end))))
12548         ;; If no mouse-face prop on line we will have to = from = end,
12549         ;; so we highlight the entire line instead.
12550         (when (= (+ to 2) from)
12551           (setq from beg)
12552           (setq to end))
12553         (if gnus-newsgroup-selected-overlay
12554             ;; Move old overlay.
12555             (gnus-move-overlay
12556              gnus-newsgroup-selected-overlay from to (current-buffer))
12557           ;; Create new overlay.
12558           (gnus-overlay-put
12559            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12560            'face gnus-summary-selected-face))))))
12561
12562 (defvar gnus-summary-highlight-line-cached nil)
12563 (defvar gnus-summary-highlight-line-trigger nil)
12564
12565 (defun gnus-summary-highlight-line-0 ()
12566   (if (and (eq gnus-summary-highlight-line-trigger
12567                gnus-summary-highlight)
12568            gnus-summary-highlight-line-cached)
12569       gnus-summary-highlight-line-cached
12570     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12571           gnus-summary-highlight-line-cached
12572           (let* ((cond (list 'cond))
12573                  (c cond)
12574                  (list gnus-summary-highlight))
12575             (while list
12576               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12577                               nil))
12578               (setq c (cdr c)
12579                     list (cdr list)))
12580             (gnus-byte-compile (list 'lambda nil cond))))))
12581
12582 (defun gnus-summary-highlight-line ()
12583   "Highlight current line according to `gnus-summary-highlight'."
12584   (let* ((beg (point-at-bol))
12585          (article (or (gnus-summary-article-number) gnus-current-article))
12586          (score (or (cdr (assq article
12587                                gnus-newsgroup-scored))
12588                     gnus-summary-default-score 0))
12589          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12590          (inhibit-read-only t)
12591          (default gnus-summary-default-score)
12592          (default-high gnus-summary-default-high-score)
12593          (default-low gnus-summary-default-low-score)
12594          (uncached (and gnus-summary-use-undownloaded-faces
12595                         (memq article gnus-newsgroup-undownloaded)
12596                         (not (memq article gnus-newsgroup-cached)))))
12597     (let ((face (funcall (gnus-summary-highlight-line-0))))
12598       (unless (eq face (gnus-get-text-property-excluding-characters-with-faces beg 'face))
12599         (gnus-put-text-property-excluding-characters-with-faces
12600          beg (point-at-eol) 'face
12601          (setq face (if (boundp face) (symbol-value face) face)))
12602         (when gnus-summary-highlight-line-function
12603           (funcall gnus-summary-highlight-line-function article face))))))
12604
12605 (defun gnus-update-read-articles (group unread &optional compute)
12606   "Update the list of read articles in GROUP.
12607 UNREAD is a sorted list."
12608   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12609         (info (gnus-get-info group))
12610         (prev 1)
12611         read)
12612     (if (or (not info) (not active))
12613         ;; There is no info on this group if it was, in fact,
12614         ;; killed.  Gnus stores no information on killed groups, so
12615         ;; there's nothing to be done.
12616         ;; One could store the information somewhere temporarily,
12617         ;; perhaps...  Hmmm...
12618         ()
12619       ;; Remove any negative articles numbers.
12620       (while (and unread (< (car unread) 0))
12621         (setq unread (cdr unread)))
12622       ;; Remove any expired article numbers
12623       (while (and unread (< (car unread) (car active)))
12624         (setq unread (cdr unread)))
12625       ;; Compute the ranges of read articles by looking at the list of
12626       ;; unread articles.
12627       (while unread
12628         (when (/= (car unread) prev)
12629           (push (if (= prev (1- (car unread))) prev
12630                   (cons prev (1- (car unread))))
12631                 read))
12632         (setq prev (1+ (car unread)))
12633         (setq unread (cdr unread)))
12634       (when (<= prev (cdr active))
12635         (push (cons prev (cdr active)) read))
12636       (setq read (if (> (length read) 1) (nreverse read) read))
12637       (if compute
12638           read
12639         (save-excursion
12640           (let (setmarkundo)
12641             ;; Propagate the read marks to the backend.
12642             (when (and (gnus-method-option-p
12643                         (gnus-find-method-for-group group)
12644                         'server-marks)
12645                        (gnus-check-backend-function 'request-set-mark group))
12646               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12647                     (add (gnus-remove-from-range read (gnus-info-read info))))
12648                 (when (or add del)
12649                   (unless (gnus-check-group group)
12650                     (error "Can't open server for %s" group))
12651                   (gnus-request-set-mark
12652                    group (delq nil (list (if add (list add 'add '(read)))
12653                                          (if del (list del 'del '(read))))))
12654                   (setq setmarkundo
12655                         `(gnus-request-set-mark
12656                           ,group
12657                           ',(delq nil (list
12658                                        (if del (list del 'add '(read)))
12659                                        (if add (list add 'del '(read))))))))))
12660             (set-buffer gnus-group-buffer)
12661             (gnus-undo-register
12662               `(progn
12663                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12664                  (gnus-info-set-read ',info ',(gnus-info-read info))
12665                  (gnus-get-unread-articles-in-group ',info
12666                                                     (gnus-active ,group))
12667                  (gnus-group-update-group ,group t)
12668                  ,setmarkundo))))
12669         ;; Enter this list into the group info.
12670         (gnus-info-set-read info read)
12671         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12672         (gnus-get-unread-articles-in-group info (gnus-active group))
12673         t))))
12674
12675 (defun gnus-offer-save-summaries ()
12676   "Offer to save all active summary buffers."
12677   (let (buffers)
12678     ;; Go through all buffers and find all summaries.
12679     (dolist (buffer (buffer-list))
12680       (when (and (setq buffer (buffer-name buffer))
12681                  (string-match "Summary" buffer)
12682                  (with-current-buffer buffer
12683                    ;; We check that this is, indeed, a summary buffer.
12684                    (and (derived-mode-p 'gnus-summary-mode)
12685                         ;; Also make sure this isn't bogus.
12686                         gnus-newsgroup-prepared
12687                         ;; Also make sure that this isn't a
12688                         ;; dead summary buffer.
12689                         (not gnus-dead-summary-mode))))
12690         (push buffer buffers)))
12691     ;; Go through all these summary buffers and offer to save them.
12692     (when buffers
12693       (save-excursion
12694         (if (eq gnus-interactive-exit 'quiet)
12695             (dolist (buffer buffers)
12696               (switch-to-buffer buffer)
12697               (gnus-summary-exit))
12698           (map-y-or-n-p
12699            "Update summary buffer %s? "
12700            (lambda (buf)
12701              (switch-to-buffer buf)
12702              (gnus-summary-exit))
12703            buffers))))))
12704
12705 (defun gnus-summary-setup-default-charset ()
12706   "Setup newsgroup default charset."
12707   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12708       (setq gnus-newsgroup-charset nil)
12709     (let* ((ignored-charsets
12710             (or gnus-newsgroup-ephemeral-ignored-charsets
12711                 (append
12712                  (and gnus-newsgroup-name
12713                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12714                  gnus-newsgroup-ignored-charsets))))
12715       (setq gnus-newsgroup-charset
12716             (or gnus-newsgroup-ephemeral-charset
12717                 (and gnus-newsgroup-name
12718                      (gnus-parameter-charset gnus-newsgroup-name))
12719                 gnus-default-charset))
12720       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12721            ignored-charsets))))
12722
12723 ;;;
12724 ;;; Mime Commands
12725 ;;;
12726
12727 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12728   "Display the current article buffer fully MIME-buttonized.
12729 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12730 treated as multipart/mixed."
12731   (interactive "P")
12732   (require 'gnus-art)
12733   (let ((gnus-unbuttonized-mime-types nil)
12734         (gnus-mime-display-multipart-as-mixed show-all-parts))
12735     (gnus-summary-show-article)))
12736
12737 (defun gnus-summary-repair-multipart (article)
12738   "Add a Content-Type header to a multipart article without one."
12739   (interactive (list (gnus-summary-article-number)))
12740   (gnus-with-article article
12741     (message-narrow-to-head)
12742     (message-remove-header "Mime-Version")
12743     (goto-char (point-max))
12744     (insert "Mime-Version: 1.0\n")
12745     (widen)
12746     (when (search-forward "\n--" nil t)
12747       (let ((separator (buffer-substring (point) (point-at-eol))))
12748         (message-narrow-to-head)
12749         (message-remove-header "Content-Type")
12750         (goto-char (point-max))
12751         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12752                         separator))
12753         (widen))))
12754   (let (gnus-mark-article-hook)
12755     (gnus-summary-select-article t t nil article)))
12756
12757 (defun gnus-summary-toggle-display-buttonized ()
12758   "Toggle the buttonizing of the article buffer."
12759   (interactive)
12760   (require 'gnus-art)
12761   (if (setq gnus-inhibit-mime-unbuttonizing
12762             (not gnus-inhibit-mime-unbuttonizing))
12763       (let ((gnus-unbuttonized-mime-types nil))
12764         (gnus-summary-show-article))
12765     (gnus-summary-show-article)))
12766
12767 ;;;
12768 ;;; Generic summary marking commands
12769 ;;;
12770
12771 (defvar gnus-summary-marking-alist
12772   '((read gnus-del-mark "d")
12773     (unread gnus-unread-mark "u")
12774     (ticked gnus-ticked-mark "!")
12775     (dormant gnus-dormant-mark "?")
12776     (expirable gnus-expirable-mark "e"))
12777   "An alist of names/marks/keystrokes.")
12778
12779 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12780 (defvar gnus-summary-mark-map)
12781
12782 (defun gnus-summary-make-all-marking-commands ()
12783   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12784   (dolist (elem gnus-summary-marking-alist)
12785     (apply 'gnus-summary-make-marking-command elem)))
12786
12787 (defun gnus-summary-make-marking-command (name mark keystroke)
12788   (let ((map (make-sparse-keymap)))
12789     (define-key gnus-summary-generic-mark-map keystroke map)
12790     (dolist (lway `((next "next" next nil "n")
12791                     (next-unread "next unread" next t "N")
12792                     (prev "previous" prev nil "p")
12793                     (prev-unread "previous unread" prev t "P")
12794                     (nomove "" nil nil ,keystroke)))
12795       (let ((func (gnus-summary-make-marking-command-1
12796                    mark (car lway) lway name)))
12797         (setq func (eval func))
12798         (define-key map (nth 4 lway) func)))))
12799
12800 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12801   `(defun ,(intern
12802             (format "gnus-summary-put-mark-as-%s%s"
12803                     name (if (eq way 'nomove)
12804                              ""
12805                            (concat "-" (symbol-name way)))))
12806      (n)
12807      ,(format
12808        "Mark the current article as %s%s.
12809 If N, the prefix, then repeat N times.
12810 If N is negative, move in reverse order.
12811 The difference between N and the actual number of articles marked is
12812 returned."
12813        name (cadr lway))
12814      (interactive "p")
12815      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12816
12817 (defun gnus-summary-generic-mark (n mark move unread)
12818   "Mark N articles with MARK."
12819   (unless (derived-mode-p 'gnus-summary-mode)
12820     (error "This command can only be used in the summary buffer"))
12821   (gnus-summary-show-thread)
12822   (let ((nummove
12823          (cond
12824           ((eq move 'next) 1)
12825           ((eq move 'prev) -1)
12826           (t 0))))
12827     (if (zerop nummove)
12828         (setq n 1)
12829       (when (< n 0)
12830         (setq n (abs n)
12831               nummove (* -1 nummove))))
12832     (while (and (> n 0)
12833                 (gnus-summary-mark-article nil mark)
12834                 (zerop (gnus-summary-next-subject nummove unread t)))
12835       (setq n (1- n)))
12836     (when (/= 0 n)
12837       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12838     (gnus-summary-recenter)
12839     (gnus-summary-position-point)
12840     (gnus-set-mode-line 'summary)
12841     n))
12842
12843 (defun gnus-summary-insert-articles (articles)
12844   (when (setq articles
12845               (gnus-sorted-difference articles
12846                                       (mapcar (lambda (h)
12847                                                 (mail-header-number h))
12848                                               gnus-newsgroup-headers)))
12849     (setq gnus-newsgroup-headers
12850           (gnus-merge 'list
12851                       gnus-newsgroup-headers
12852                       (gnus-fetch-headers articles nil t)
12853                       'gnus-article-sort-by-number))
12854     (setq gnus-newsgroup-articles
12855           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12856     ;; Suppress duplicates?
12857     (when gnus-suppress-duplicates
12858       (gnus-dup-suppress-articles))
12859
12860     (if (and gnus-fetch-old-headers
12861              (eq gnus-headers-retrieved-by 'nov))
12862         ;; We might want to build some more threads first.
12863         (if (eq gnus-fetch-old-headers 'invisible)
12864             (gnus-build-all-threads)
12865           (gnus-build-old-threads))
12866       ;; Mark the inserted articles that are unread as unread.
12867       (setq gnus-newsgroup-unreads
12868             (gnus-sorted-nunion
12869              gnus-newsgroup-unreads
12870              (gnus-sorted-nintersection
12871               (gnus-list-of-unread-articles gnus-newsgroup-name)
12872               articles)))
12873       ;; Mark the inserted articles as selected so that the information
12874       ;; of the marks having been changed by a user may be updated when
12875       ;; exiting this group.  See `gnus-summary-update-info'.
12876       (dolist (art articles)
12877         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12878     ;; Let the Gnus agent mark articles as read.
12879     (when gnus-agent
12880       (gnus-agent-get-undownloaded-list))
12881     ;; Remove list identifiers from subject
12882     (gnus-summary-remove-list-identifiers)
12883     ;; First and last article in this newsgroup.
12884     (when gnus-newsgroup-headers
12885       (setq gnus-newsgroup-begin
12886             (mail-header-number (car gnus-newsgroup-headers))
12887             gnus-newsgroup-end
12888             (mail-header-number
12889              (gnus-last-element gnus-newsgroup-headers))))
12890     (when gnus-use-scoring
12891       (gnus-possibly-score-headers))))
12892
12893 (defun gnus-summary-insert-old-articles (&optional all)
12894   "Insert all old articles in this group.
12895 If ALL is non-nil, already read articles become readable.
12896 If ALL is a number, fetch this number of articles."
12897   (interactive "P")
12898   (prog1
12899       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12900             older len)
12901         (setq older
12902               ;; Some nntp servers lie about their active range.  When
12903               ;; this happens, the active range can be in the millions.
12904               ;; Use a compressed range to avoid creating a huge list.
12905               (gnus-range-difference
12906                (gnus-range-difference (list gnus-newsgroup-active) old)
12907                gnus-newsgroup-unexist))
12908         (setq len (gnus-range-length older))
12909         (cond
12910          ((null older) nil)
12911          ((numberp all)
12912           (if (< all len)
12913               (let ((older-range (nreverse older)))
12914                 (setq older nil)
12915
12916                 (while (> all 0)
12917                   (let* ((r (pop older-range))
12918                          (min (if (numberp r) r (car r)))
12919                          (max (if (numberp r) r (cdr r))))
12920                     (while (and (<= min max)
12921                                 (> all 0))
12922                       (push max older)
12923                       (setq all (1- all)
12924                             max (1- max))))))
12925             (setq older (gnus-uncompress-range older))))
12926          (all
12927           (setq older (gnus-uncompress-range older)))
12928          (t
12929           (when (and (numberp gnus-large-newsgroup)
12930                    (> len gnus-large-newsgroup))
12931               (let* ((cursor-in-echo-area nil)
12932                      (initial (gnus-parameter-large-newsgroup-initial
12933                                gnus-newsgroup-name))
12934                      (input
12935                       (read-string
12936                        (format
12937                         "How many articles from %s (%s %d): "
12938                         (gnus-group-decoded-name gnus-newsgroup-name)
12939                         (if initial "max" "default")
12940                         len)
12941                        nil nil
12942                        (and initial
12943                             (number-to-string initial)))))
12944                 (unless (string-match "^[ \t]*$" input)
12945                   (setq all (string-to-number input))
12946                   (if (< all len)
12947                       (let ((older-range (nreverse older)))
12948                         (setq older nil)
12949
12950                         (while (> all 0)
12951                           (let* ((r (pop older-range))
12952                                  (min (if (numberp r) r (car r)))
12953                                  (max (if (numberp r) r (cdr r))))
12954                             (while (and (<= min max)
12955                                         (> all 0))
12956                               (push max older)
12957                               (setq all (1- all)
12958                                     max (1- max))))))))))
12959           (setq older (gnus-uncompress-range older))))
12960         (if (not older)
12961             (message "No old news.")
12962           (gnus-summary-insert-articles older)
12963           (gnus-summary-limit (gnus-sorted-nunion old older))))
12964     (gnus-summary-position-point)))
12965
12966 (defun gnus-summary-insert-new-articles ()
12967   "Insert all new articles in this group."
12968   (interactive)
12969   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12970         (old-high gnus-newsgroup-highest)
12971         (nnmail-fetched-sources (list t))
12972         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12973         i new)
12974     (unless new-active
12975       (error "Couldn't fetch new data"))
12976     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12977     (setq i (cdr gnus-newsgroup-active)
12978           gnus-newsgroup-highest i)
12979     (while (> i old-high)
12980       (push i new)
12981       (decf i))
12982     (if (not new)
12983         (message "No gnus is bad news")
12984       (gnus-summary-insert-articles new)
12985       (setq gnus-newsgroup-unreads
12986             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12987       (gnus-summary-limit (gnus-sorted-nunion old new))))
12988   (gnus-summary-position-point))
12989
12990 ;;; Bookmark support for Gnus.
12991 (declare-function gnus-article-show-summary "gnus-art" ())
12992 (declare-function bookmark-make-record-default
12993                   "bookmark" (&optional no-file no-context posn))
12994 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12995 (declare-function bookmark-default-handler "bookmark" (bmk))
12996 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12997 (defvar bookmark-yank-point)
12998 (defvar bookmark-current-buffer)
12999
13000 (defun gnus-summary-bookmark-make-record ()
13001   "Make a bookmark entry for a Gnus summary buffer."
13002   (let (pos buf)
13003     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
13004       (save-restriction              ; FIXME is it necessary to widen?
13005         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
13006       (setq buf "art") ; We are recording bookmark from article buffer.
13007       (setq bookmark-yank-point (point))
13008       (setq bookmark-current-buffer (current-buffer))
13009       (gnus-article-show-summary))      ; Go back in summary buffer.
13010     ;; We are now recording bookmark from summary buffer.
13011     (unless buf (setq buf "sum"))
13012     (let* ((subject (elt (gnus-summary-article-header) 1))
13013            (grp     (car gnus-article-current))
13014            (art     (cdr gnus-article-current))
13015            (head    (gnus-summary-article-header art))
13016            (id      (mail-header-id head)))
13017       `(,subject
13018         ,@(condition-case nil
13019               (bookmark-make-record-default 'no-file 'no-context pos)
13020             (wrong-number-of-arguments
13021              (bookmark-make-record-default 'point-only)))
13022         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
13023         (group . ,grp) (article . ,art)
13024         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
13025
13026 ;;;###autoload
13027 (defun gnus-summary-bookmark-jump (bookmark)
13028   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
13029 BOOKMARK is a bookmark name or a bookmark record."
13030   (let ((group    (bookmark-prop-get bookmark 'group))
13031         (article  (bookmark-prop-get bookmark 'article))
13032         (id       (bookmark-prop-get bookmark 'message-id))
13033         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
13034     (gnus-fetch-group group (list article))
13035     (gnus-summary-insert-cached-articles)
13036     (gnus-summary-goto-article id nil 'force)
13037     ;; FIXME we have to wait article buffer is ready (only large buffer)
13038     ;; Is there a better solution to know that?
13039     ;; If we don't wait `bookmark-default-handler' will have no chance
13040     ;; to set position. However there is no error, just wrong pos.
13041     (sit-for 1)
13042     (when (string= buf "Gnus-art")
13043       (other-window 1))
13044     (bookmark-default-handler
13045      `(""
13046        (buffer . ,(current-buffer))
13047        . ,(bookmark-get-bookmark-record bookmark)))))
13048
13049 (gnus-summary-make-all-marking-commands)
13050
13051 (gnus-ems-redefine)
13052
13053 (provide 'gnus-sum)
13054
13055 (run-hooks 'gnus-sum-load-hook)
13056
13057 ;; Local Variables:
13058 ;; coding: utf-8
13059 ;; End:
13060
13061 ;;; gnus-sum.el ends here