Merge remote branch 'origin/no-gnus'
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996-2012 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
851 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
852   "*List of functions used for sorting threads in the summary buffer.
853 By default, threads are sorted by article number.
854
855 Each function takes two threads and returns non-nil if the first
856 thread should be sorted before the other.  If you use more than one
857 function, the primary sort function should be the last.  You should
858 probably always include `gnus-thread-sort-by-number' in the list of
859 sorting functions -- preferably first.  Also note that sorting by date
860 is often much slower than sorting by number, and the sorting order is
861 very similar.  (Sorting by date means sorting by the time the message
862 was sent, sorting by number means sorting by arrival time.)
863
864 Each list item can also be a list `(not F)' where F is a
865 function; this specifies reversed sort order.
866
867 Ready-made functions include `gnus-thread-sort-by-number',
868 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
869 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
870 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
871 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
872 and `gnus-thread-sort-by-total-score' (see
873 `gnus-thread-score-function').
874
875 When threading is turned off, the variable
876 `gnus-article-sort-functions' controls how articles are sorted."
877   :group 'gnus-summary-sort
878   :type '(repeat
879           (gnus-widget-reversible
880            (choice (function-item gnus-thread-sort-by-number)
881                    (function-item gnus-thread-sort-by-author)
882                    (function-item gnus-thread-sort-by-recipient)
883                    (function-item gnus-thread-sort-by-subject)
884                    (function-item gnus-thread-sort-by-date)
885                    (function-item gnus-thread-sort-by-score)
886                    (function-item gnus-thread-sort-by-most-recent-number)
887                    (function-item gnus-thread-sort-by-most-recent-date)
888                    (function-item gnus-thread-sort-by-random)
889                    (function-item gnus-thread-sort-by-total-score)
890                    (function :tag "other"))
891            (boolean :tag "Reverse order"))))
892
893 (defcustom gnus-thread-score-function '+
894   "*Function used for calculating the total score of a thread.
895
896 The function is called with the scores of the article and each
897 subthread and should then return the score of the thread.
898
899 Some functions you can use are `+', `max', or `min'."
900   :group 'gnus-summary-sort
901   :type 'function)
902
903 (defcustom gnus-summary-expunge-below nil
904   "All articles that have a score less than this variable will be expunged.
905 This variable is local to the summary buffers."
906   :group 'gnus-score-default
907   :type '(choice (const :tag "off" nil)
908                  integer))
909
910 (defcustom gnus-thread-expunge-below nil
911   "All threads that have a total score less than this variable will be expunged.
912 See `gnus-thread-score-function' for en explanation of what a
913 \"thread score\" is.
914
915 This variable is local to the summary buffers."
916   :group 'gnus-threading
917   :group 'gnus-score-default
918   :type '(choice (const :tag "off" nil)
919                  integer))
920
921 (defcustom gnus-summary-mode-hook nil
922   "*A hook for Gnus summary mode.
923 This hook is run before any variables are set in the summary buffer."
924   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
925   :group 'gnus-summary-various
926   :type 'hook)
927
928 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
929 (when (featurep 'xemacs)
930   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
931   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
932   (add-hook 'gnus-summary-mode-hook
933             'gnus-xmas-switch-horizontal-scrollbar-off))
934
935 (defcustom gnus-summary-menu-hook nil
936   "*Hook run after the creation of the summary mode menu."
937   :group 'gnus-summary-visual
938   :type 'hook)
939
940 (defcustom gnus-summary-exit-hook nil
941   "*A hook called on exit from the summary buffer.
942 It will be called with point in the group buffer."
943   :group 'gnus-summary-exit
944   :type 'hook)
945
946 (defcustom gnus-summary-prepare-hook nil
947   "*A hook called after the summary buffer has been generated.
948 If you want to modify the summary buffer, you can use this hook."
949   :group 'gnus-summary-various
950   :type 'hook)
951
952 (defcustom gnus-summary-prepared-hook nil
953   "*A hook called as the last thing after the summary buffer has been generated."
954   :group 'gnus-summary-various
955   :type 'hook)
956
957 (defcustom gnus-summary-generate-hook nil
958   "*A hook run just before generating the summary buffer.
959 This hook is commonly used to customize threading variables and the
960 like."
961   :group 'gnus-summary-various
962   :type 'hook)
963
964 (defcustom gnus-select-group-hook nil
965   "*A hook called when a newsgroup is selected.
966
967 If you'd like to simplify subjects like the
968 `gnus-summary-next-same-subject' command does, you can use the
969 following hook:
970
971  (add-hook gnus-select-group-hook
972            (lambda ()
973              (mapcar (lambda (header)
974                        (mail-header-set-subject
975                         header
976                         (gnus-simplify-subject
977                          (mail-header-subject header) 're-only)))
978                      gnus-newsgroup-headers)))"
979   :group 'gnus-group-select
980   :type 'hook)
981
982 (defcustom gnus-select-article-hook nil
983   "*A hook called when an article is selected."
984   :group 'gnus-summary-choose
985   :options '(gnus-agent-fetch-selected-article)
986   :type 'hook)
987
988 (defcustom gnus-visual-mark-article-hook
989   (list 'gnus-highlight-selected-summary)
990   "*Hook run after selecting an article in the summary buffer.
991 It is meant to be used for highlighting the article in some way.  It
992 is not run if `gnus-visual' is nil."
993   :group 'gnus-summary-visual
994   :type 'hook)
995
996 (defcustom gnus-parse-headers-hook nil
997   "*A hook called before parsing the headers."
998   :group 'gnus-various
999   :type 'hook)
1000
1001 (defcustom gnus-exit-group-hook nil
1002   "*A hook called when exiting summary mode.
1003 This hook is not called from the non-updating exit commands like `Q'."
1004   :group 'gnus-various
1005   :type 'hook)
1006
1007 (defcustom gnus-summary-update-hook nil
1008   "*A hook called when a summary line is changed.
1009 The hook will not be called if `gnus-visual' is nil.
1010
1011 The default function `gnus-summary-highlight-line' will
1012 highlight the line according to the `gnus-summary-highlight'
1013 variable."
1014   :group 'gnus-summary-visual
1015   :type 'hook)
1016
1017 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1018   "*A hook called when an article is selected for the first time.
1019 The hook is intended to mark an article as read (or unread)
1020 automatically when it is selected."
1021   :group 'gnus-summary-choose
1022   :type 'hook)
1023
1024 (defcustom gnus-group-no-more-groups-hook nil
1025   "*A hook run when returning to group mode having no more (unread) groups."
1026   :group 'gnus-group-select
1027   :type 'hook)
1028
1029 (defcustom gnus-ps-print-hook nil
1030   "*A hook run before ps-printing something from Gnus."
1031   :group 'gnus-summary
1032   :type 'hook)
1033
1034 (defcustom gnus-summary-article-move-hook nil
1035   "*A hook called after an article is moved, copied, respooled, or crossposted."
1036   :version "22.1"
1037   :group 'gnus-summary
1038   :type 'hook)
1039
1040 (defcustom gnus-summary-article-delete-hook nil
1041   "*A hook called after an article is deleted."
1042   :version "22.1"
1043   :group 'gnus-summary
1044   :type 'hook)
1045
1046 (defcustom gnus-summary-article-expire-hook nil
1047   "*A hook called after an article is expired."
1048   :version "22.1"
1049   :group 'gnus-summary
1050   :type 'hook)
1051
1052 (defcustom gnus-summary-display-arrow
1053   (and (fboundp 'display-graphic-p)
1054        (display-graphic-p))
1055   "*If non-nil, display an arrow highlighting the current article."
1056   :version "22.1"
1057   :group 'gnus-summary
1058   :type 'boolean)
1059
1060 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1061   "Face used for highlighting the current article in the summary buffer."
1062   :group 'gnus-summary-visual
1063   :type 'face)
1064
1065 (defvar gnus-tmp-downloaded nil)
1066
1067 (defcustom gnus-summary-highlight
1068   '(((eq mark gnus-canceled-mark)
1069      . gnus-summary-cancelled)
1070     ((and uncached (> score default-high))
1071      . gnus-summary-high-undownloaded)
1072     ((and uncached (< score default-low))
1073      . gnus-summary-low-undownloaded)
1074     (uncached
1075      . gnus-summary-normal-undownloaded)
1076     ((and (> score default-high)
1077           (or (eq mark gnus-dormant-mark)
1078               (eq mark gnus-ticked-mark)))
1079      . gnus-summary-high-ticked)
1080     ((and (< score default-low)
1081           (or (eq mark gnus-dormant-mark)
1082               (eq mark gnus-ticked-mark)))
1083      . gnus-summary-low-ticked)
1084     ((or (eq mark gnus-dormant-mark)
1085          (eq mark gnus-ticked-mark))
1086      . gnus-summary-normal-ticked)
1087     ((and (> score default-high) (eq mark gnus-ancient-mark))
1088      . gnus-summary-high-ancient)
1089     ((and (< score default-low) (eq mark gnus-ancient-mark))
1090      . gnus-summary-low-ancient)
1091     ((eq mark gnus-ancient-mark)
1092      . gnus-summary-normal-ancient)
1093     ((and (> score default-high) (eq mark gnus-unread-mark))
1094      . gnus-summary-high-unread)
1095     ((and (< score default-low) (eq mark gnus-unread-mark))
1096      . gnus-summary-low-unread)
1097     ((eq mark gnus-unread-mark)
1098      . gnus-summary-normal-unread)
1099     ((> score default-high)
1100      . gnus-summary-high-read)
1101     ((< score default-low)
1102      . gnus-summary-low-read)
1103     (t
1104      . gnus-summary-normal-read))
1105   "*Controls the highlighting of summary buffer lines.
1106
1107 A list of (FORM . FACE) pairs.  When deciding how a particular
1108 summary line should be displayed, each form is evaluated.  The content
1109 of the face field after the first true form is used.  You can change
1110 how those summary lines are displayed, by editing the face field.
1111
1112 You can use the following variables in the FORM field.
1113
1114 score:        The article's score.
1115 default:      The default article score.
1116 default-high: The default score for high scored articles.
1117 default-low:  The default score for low scored articles.
1118 below:        The score below which articles are automatically marked as read.
1119 mark:         The article's mark.
1120 uncached:     Non-nil if the article is uncached."
1121   :group 'gnus-summary-visual
1122   :type '(repeat (cons (sexp :tag "Form" nil)
1123                        face)))
1124 (put 'gnus-summary-highlight 'risky-local-variable t)
1125
1126 (defcustom gnus-alter-header-function nil
1127   "Function called to allow alteration of article header structures.
1128 The function is called with one parameter, the article header vector,
1129 which it may alter in any way."
1130   :type '(choice (const :tag "None" nil)
1131                  function)
1132   :group 'gnus-summary)
1133
1134 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1135   "Function used to decode a string with encoded words.")
1136
1137 (defvar gnus-decode-encoded-address-function
1138   'mail-decode-encoded-address-string
1139   "Function used to decode addresses with encoded words.")
1140
1141 (defcustom gnus-extra-headers '(To Cc Keywords Gcc Newsgroups)
1142   "*Extra headers to parse."
1143   :version "24.1"                       ; added Cc Keywords Gcc
1144   :group 'gnus-summary
1145   :type '(repeat symbol))
1146
1147 (defcustom gnus-ignored-from-addresses
1148   (and user-mail-address
1149        (not (string= user-mail-address ""))
1150        (regexp-quote user-mail-address))
1151   "*From headers that may be suppressed in favor of To headers.
1152 This can be a regexp or a list of regexps."
1153   :version "21.1"
1154   :group 'gnus-summary
1155   :type '(choice regexp
1156                  (repeat :tag "Regexp List" regexp)))
1157
1158 (defsubst gnus-ignored-from-addresses ()
1159   (gmm-regexp-concat gnus-ignored-from-addresses))
1160
1161 (defcustom gnus-summary-to-prefix "-> "
1162   "*String prefixed to the To field in the summary line when
1163 using `gnus-ignored-from-addresses'."
1164   :version "22.1"
1165   :group 'gnus-summary
1166   :type 'string)
1167
1168 (defcustom gnus-summary-newsgroup-prefix "=> "
1169   "*String prefixed to the Newsgroup field in the summary
1170 line when using `gnus-ignored-from-addresses'."
1171   :version "22.1"
1172   :group 'gnus-summary
1173   :type 'string)
1174
1175 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1176   "List of charsets that should be ignored.
1177 When these charsets are used in the \"charset\" parameter, the
1178 default charset will be used instead."
1179   :version "21.1"
1180   :type '(repeat symbol)
1181   :group 'gnus-charset)
1182
1183 (defcustom gnus-newsgroup-maximum-articles nil
1184   "The maximum number of articles a newsgroup.
1185 If this is a number, old articles in a newsgroup exceeding this number
1186 are silently ignored.  If it is nil, no article is ignored.  Note that
1187 setting this variable to a number might prevent you from reading very
1188 old articles."
1189   :group 'gnus-group-select
1190   :version "22.2"
1191   :type '(choice (const :tag "No limit" nil)
1192                  integer))
1193
1194 (gnus-define-group-parameter
1195  ignored-charsets
1196  :type list
1197  :function-document
1198  "Return the ignored charsets of GROUP."
1199  :variable gnus-group-ignored-charsets-alist
1200  :variable-default
1201  '(("alt\\.chinese\\.text" iso-8859-1))
1202  :variable-document
1203  "Alist of regexps (to match group names) and charsets that should be ignored.
1204 When these charsets are used in the \"charset\" parameter, the
1205 default charset will be used instead."
1206  :variable-group gnus-charset
1207  :variable-type '(repeat (cons (regexp :tag "Group")
1208                                (repeat symbol)))
1209  :parameter-type '(choice :tag "Ignored charsets"
1210                           :value nil
1211                           (repeat (symbol)))
1212  :parameter-document       "\
1213 List of charsets that should be ignored.
1214
1215 When these charsets are used in the \"charset\" parameter, the
1216 default charset will be used instead.")
1217
1218 (defcustom gnus-group-highlight-words-alist nil
1219   "Alist of group regexps and highlight regexps.
1220 This variable uses the same syntax as `gnus-emphasis-alist'."
1221   :version "21.1"
1222   :type '(repeat (cons (regexp :tag "Group")
1223                        (repeat (list (regexp :tag "Highlight regexp")
1224                                      (number :tag "Group for entire word" 0)
1225                                      (number :tag "Group for displayed part" 0)
1226                                      (symbol :tag "Face"
1227                                              gnus-emphasis-highlight-words)))))
1228   :group 'gnus-summary-visual)
1229
1230 (defcustom gnus-summary-show-article-charset-alist
1231   nil
1232   "Alist of number and charset.
1233 The article will be shown with the charset corresponding to the
1234 numbered argument.
1235 For example: ((1 . cn-gb-2312) (2 . big5))."
1236   :version "21.1"
1237   :type '(repeat (cons (number :tag "Argument" 1)
1238                        (symbol :tag "Charset")))
1239   :group 'gnus-charset)
1240
1241 (defcustom gnus-preserve-marks t
1242   "Whether marks are preserved when moving, copying and respooling messages."
1243   :version "21.1"
1244   :type 'boolean
1245   :group 'gnus-summary-marks)
1246
1247 (defcustom gnus-alter-articles-to-read-function nil
1248   "Function to be called to alter the list of articles to be selected."
1249   :type '(choice (const nil) function)
1250   :group 'gnus-summary)
1251
1252 (defcustom gnus-orphan-score nil
1253   "*All orphans get this score added.  Set in the score file."
1254   :group 'gnus-score-default
1255   :type '(choice (const nil)
1256                  integer))
1257
1258 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1259   "*A regexp to match MIME parts when saving multiple parts of a
1260 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1261 This regexp will be used by default when prompting the user for which
1262 type of files to save."
1263   :group 'gnus-summary
1264   :type 'regexp)
1265
1266 (defcustom gnus-read-all-available-headers nil
1267   "Whether Gnus should parse all headers made available to it.
1268 This is mostly relevant for slow back ends where the user may
1269 wish to widen the summary buffer to include all headers
1270 that were fetched."
1271   :version "22.1"
1272   :group 'gnus-summary
1273   :type '(choice boolean regexp))
1274
1275 (defcustom gnus-summary-pipe-output-default-command nil
1276   "Command (and optional arguments) used to pipe article to subprocess.
1277 This will be used as the default command if it is non-nil.  The value
1278 will be updated if you modify it when executing the command
1279 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1280   :version "23.1" ;; No Gnus
1281   :group 'gnus-summary
1282   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1283
1284 (defcustom gnus-summary-muttprint-program "muttprint"
1285   "Command (and optional arguments) used to run Muttprint.
1286 The value will be updated if you modify it when executing the command
1287 `gnus-summary-muttprint'."
1288   :version "22.1"
1289   :group 'gnus-summary
1290   :type 'string)
1291
1292 (defcustom gnus-article-loose-mime t
1293   "If non-nil, don't require MIME-Version header.
1294 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1295 supply the MIME-Version header or deliberately strip it from the mail.
1296 If non-nil (the default), Gnus will treat some articles as MIME
1297 even if the MIME-Version header is missing."
1298   :version "22.1"
1299   :type 'boolean
1300   :group 'gnus-article-mime)
1301
1302 (defcustom gnus-article-emulate-mime t
1303   "If non-nil, use MIME emulation for uuencode and the like.
1304 This means that Gnus will search message bodies for text that look
1305 like uuencoded bits, yEncoded bits, and so on, and present that using
1306 the normal Gnus MIME machinery."
1307   :version "22.1"
1308   :type 'boolean
1309   :group 'gnus-article-mime)
1310
1311 ;;; Internal variables
1312
1313 (defvar gnus-summary-display-cache nil)
1314 (defvar gnus-article-mime-handles nil)
1315 (defvar gnus-article-decoded-p nil)
1316 (defvar gnus-article-charset nil)
1317 (defvar gnus-article-ignored-charsets nil)
1318 (defvar gnus-scores-exclude-files nil)
1319 (defvar gnus-page-broken nil)
1320
1321 (defvar gnus-original-article nil)
1322 (defvar gnus-article-internal-prepare-hook nil)
1323 (defvar gnus-newsgroup-process-stack nil)
1324
1325 (defvar gnus-thread-indent-array nil)
1326 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1327 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1328   "Function called to sort the articles within a thread after it has been gathered together.")
1329
1330 (defvar gnus-summary-save-parts-type-history nil)
1331 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1332
1333 ;; Avoid highlighting in kill files.
1334 (defvar gnus-summary-inhibit-highlight nil)
1335 (defvar gnus-newsgroup-selected-overlay nil)
1336 (defvar gnus-inhibit-limiting nil)
1337 (defvar gnus-newsgroup-adaptive-score-file nil)
1338 (defvar gnus-current-score-file nil)
1339 (defvar gnus-current-move-group nil)
1340 (defvar gnus-current-copy-group nil)
1341 (defvar gnus-current-crosspost-group nil)
1342 (defvar gnus-newsgroup-display nil)
1343
1344 (defvar gnus-newsgroup-dependencies nil)
1345 (defvar gnus-newsgroup-adaptive nil)
1346 (defvar gnus-summary-display-article-function nil)
1347 (defvar gnus-summary-highlight-line-function nil
1348   "Function called after highlighting a summary line.")
1349
1350 (defvar gnus-summary-line-format-alist
1351   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1352     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1353     (?s gnus-tmp-subject-or-nil ?s)
1354     (?n gnus-tmp-name ?s)
1355     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1356         ?s)
1357     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1358             gnus-tmp-from) ?s)
1359     (?F gnus-tmp-from ?s)
1360     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1361     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1362     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1363     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1364     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1365     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1366     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1367     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1368     (?L gnus-tmp-lines ?s)
1369     (?Z (or (nnir-article-rsv (mail-header-number gnus-tmp-header))
1370             0) ?d)
1371     (?G (or (nnir-article-group (mail-header-number gnus-tmp-header))
1372             "") ?s)
1373     (?g (or (gnus-group-short-name
1374              (nnir-article-group (mail-header-number gnus-tmp-header)))
1375             "") ?s)
1376     (?O gnus-tmp-downloaded ?c)
1377     (?I gnus-tmp-indentation ?s)
1378     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1379     (?R gnus-tmp-replied ?c)
1380     (?\[ gnus-tmp-opening-bracket ?c)
1381     (?\] gnus-tmp-closing-bracket ?c)
1382     (?\> (make-string gnus-tmp-level ? ) ?s)
1383     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1384     (?i gnus-tmp-score ?d)
1385     (?z gnus-tmp-score-char ?c)
1386     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1387     (?U gnus-tmp-unread ?c)
1388     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1389         ?s)
1390     (?t (gnus-summary-number-of-articles-in-thread
1391          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1392         ?d)
1393     (?e (gnus-summary-number-of-articles-in-thread
1394          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1395         ?c)
1396     (?u gnus-tmp-user-defined ?s)
1397     (?P (gnus-pick-line-number) ?d)
1398     (?B gnus-tmp-thread-tree-header-string ?s)
1399     (user-date (gnus-user-date
1400                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1401   "An alist of format specifications that can appear in summary lines.
1402 These are paired with what variables they correspond with, along with
1403 the type of the variable (string, integer, character, etc).")
1404
1405 (defvar gnus-summary-dummy-line-format-alist
1406   `((?S gnus-tmp-subject ?s)
1407     (?N gnus-tmp-number ?d)
1408     (?u gnus-tmp-user-defined ?s)))
1409
1410 (defvar gnus-summary-mode-line-format-alist
1411   `((?G gnus-tmp-group-name ?s)
1412     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1413     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1414     (?A gnus-tmp-article-number ?d)
1415     (?Z gnus-tmp-unread-and-unselected ?s)
1416     (?V gnus-version ?s)
1417     (?U gnus-tmp-unread-and-unticked ?d)
1418     (?S gnus-tmp-subject ?s)
1419     (?e gnus-tmp-unselected ?d)
1420     (?u gnus-tmp-user-defined ?s)
1421     (?d (length gnus-newsgroup-dormant) ?d)
1422     (?t (length gnus-newsgroup-marked) ?d)
1423     (?h (length gnus-newsgroup-spam-marked) ?d)
1424     (?r (length gnus-newsgroup-reads) ?d)
1425     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1426     (?E gnus-newsgroup-expunged-tally ?d)
1427     (?s (gnus-current-score-file-nondirectory) ?s)))
1428
1429 ;; This is here rather than in gnus-art for compilation reasons.
1430 (defvar gnus-article-mode-line-format-alist
1431   (nconc '((?w (gnus-article-wash-status) ?s)
1432            (?m (gnus-article-mime-part-status) ?s))
1433          gnus-summary-mode-line-format-alist))
1434
1435 (defvar gnus-last-search-regexp nil
1436   "Default regexp for article search command.")
1437
1438 (defvar gnus-last-shell-command nil
1439   "Default shell command on article.")
1440
1441 (defvar gnus-newsgroup-agentized nil
1442   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1443 (defvar gnus-newsgroup-begin nil)
1444 (defvar gnus-newsgroup-end nil)
1445 (defvar gnus-newsgroup-last-rmail nil)
1446 (defvar gnus-newsgroup-last-mail nil)
1447 (defvar gnus-newsgroup-last-folder nil)
1448 (defvar gnus-newsgroup-last-file nil)
1449 (defvar gnus-newsgroup-last-directory nil)
1450 (defvar gnus-newsgroup-auto-expire nil)
1451 (defvar gnus-newsgroup-active nil)
1452 (defvar gnus-newsgroup-highest nil)
1453
1454 (defvar gnus-newsgroup-data nil)
1455 (defvar gnus-newsgroup-data-reverse nil)
1456 (defvar gnus-newsgroup-limit nil)
1457 (defvar gnus-newsgroup-limits nil)
1458 (defvar gnus-summary-use-undownloaded-faces nil)
1459
1460 (defvar gnus-newsgroup-unreads nil
1461   "Sorted list of unread articles in the current newsgroup.")
1462
1463 (defvar gnus-newsgroup-unselected nil
1464   "Sorted list of unselected unread articles in the current newsgroup.")
1465
1466 (defvar gnus-newsgroup-reads nil
1467   "Alist of read articles and article marks in the current newsgroup.")
1468
1469 (defvar gnus-newsgroup-expunged-tally nil)
1470
1471 (defvar gnus-newsgroup-marked nil
1472   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1473
1474 (defvar gnus-newsgroup-spam-marked nil
1475   "List of ranges of articles that have been marked as spam.")
1476
1477 (defvar gnus-newsgroup-killed nil
1478   "List of ranges of articles that have been through the scoring process.")
1479
1480 (defvar gnus-newsgroup-cached nil
1481   "Sorted list of articles that come from the article cache.")
1482
1483 (defvar gnus-newsgroup-saved nil
1484   "List of articles that have been saved.")
1485
1486 (defvar gnus-newsgroup-kill-headers nil)
1487
1488 (defvar gnus-newsgroup-replied nil
1489   "List of articles that have been replied to in the current newsgroup.")
1490
1491 (defvar gnus-newsgroup-forwarded nil
1492   "List of articles that have been forwarded in the current newsgroup.")
1493
1494 (defvar gnus-newsgroup-expirable nil
1495   "Sorted list of articles in the current newsgroup that can be expired.")
1496
1497 (defvar gnus-newsgroup-processable nil
1498   "List of articles in the current newsgroup that can be processed.")
1499
1500 (defvar gnus-newsgroup-downloadable nil
1501   "Sorted list of articles in the current newsgroup that can be processed.")
1502
1503 (defvar gnus-newsgroup-unfetched nil
1504   "Sorted list of articles in the current newsgroup whose headers have
1505 not been fetched into the agent.
1506
1507 This list will always be a subset of gnus-newsgroup-undownloaded.")
1508
1509 (defvar gnus-newsgroup-undownloaded nil
1510   "List of articles in the current newsgroup that haven't been downloaded.")
1511
1512 (defvar gnus-newsgroup-unsendable nil
1513   "List of articles in the current newsgroup that won't be sent.")
1514
1515 (defvar gnus-newsgroup-bookmarks nil
1516   "List of articles in the current newsgroup that have bookmarks.")
1517
1518 (defvar gnus-newsgroup-dormant nil
1519   "Sorted list of dormant articles in the current newsgroup.")
1520
1521 (defvar gnus-newsgroup-unseen nil
1522   "List of unseen articles in the current newsgroup.")
1523
1524 (defvar gnus-newsgroup-seen nil
1525   "Range of seen articles in the current newsgroup.")
1526
1527 (defvar gnus-newsgroup-unexist nil
1528   "Range of unexistent articles in the current newsgroup.")
1529
1530 (defvar gnus-newsgroup-articles nil
1531   "List of articles in the current newsgroup.")
1532
1533 (defvar gnus-newsgroup-scored nil
1534   "List of scored articles in the current newsgroup.")
1535
1536 (defvar gnus-newsgroup-headers nil
1537   "List of article headers in the current newsgroup.")
1538
1539 (defvar gnus-newsgroup-threads nil)
1540
1541 (defvar gnus-newsgroup-prepared nil
1542   "Whether the current group has been prepared properly.")
1543
1544 (defvar gnus-newsgroup-ancient nil
1545   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1546
1547 (defvar gnus-newsgroup-sparse nil)
1548
1549 (defvar gnus-current-article nil)
1550 (defvar gnus-article-current nil)
1551 (defvar gnus-current-headers nil)
1552 (defvar gnus-have-all-headers nil)
1553 (defvar gnus-last-article nil)
1554 (defvar gnus-newsgroup-history nil)
1555 (defvar gnus-newsgroup-charset nil)
1556 (defvar gnus-newsgroup-ephemeral-charset nil)
1557 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1558
1559 (defvar gnus-article-before-search nil)
1560
1561 (defvar gnus-summary-local-variables
1562   '(gnus-newsgroup-name
1563
1564     ;; Marks lists
1565     gnus-newsgroup-unreads
1566     gnus-newsgroup-unselected
1567     gnus-newsgroup-marked
1568     gnus-newsgroup-spam-marked
1569     gnus-newsgroup-reads
1570     gnus-newsgroup-saved
1571     gnus-newsgroup-replied
1572     gnus-newsgroup-forwarded
1573     gnus-newsgroup-expirable
1574     gnus-newsgroup-killed
1575     gnus-newsgroup-unseen
1576     gnus-newsgroup-seen
1577     gnus-newsgroup-unexist
1578     gnus-newsgroup-cached
1579     gnus-newsgroup-downloadable
1580     gnus-newsgroup-undownloaded
1581     gnus-newsgroup-unsendable
1582
1583     gnus-newsgroup-begin gnus-newsgroup-end
1584     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1585     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1586     gnus-newsgroup-last-directory
1587     gnus-newsgroup-auto-expire
1588     gnus-newsgroup-processable
1589     gnus-newsgroup-unfetched
1590     gnus-newsgroup-articles
1591     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1592     gnus-newsgroup-headers gnus-newsgroup-threads
1593     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1594     gnus-current-article gnus-current-headers gnus-have-all-headers
1595     gnus-last-article gnus-article-internal-prepare-hook
1596     (gnus-summary-article-delete-hook . global)
1597     (gnus-summary-article-move-hook . global)
1598     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1599     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1600     gnus-thread-expunge-below
1601     gnus-score-alist gnus-current-score-file
1602     (gnus-summary-expunge-below . global)
1603     (gnus-summary-mark-below . global)
1604     (gnus-orphan-score . global)
1605     gnus-newsgroup-active gnus-scores-exclude-files
1606     gnus-newsgroup-highest
1607     gnus-newsgroup-history gnus-newsgroup-ancient
1608     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1609     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1610     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1611     (gnus-newsgroup-expunged-tally . 0)
1612     gnus-cache-removable-articles
1613     gnus-newsgroup-data gnus-newsgroup-data-reverse
1614     gnus-newsgroup-limit gnus-newsgroup-limits
1615     gnus-newsgroup-charset gnus-newsgroup-display
1616     gnus-summary-use-undownloaded-faces)
1617   "Variables that are buffer-local to the summary buffers.")
1618
1619 (defvar gnus-newsgroup-variables nil
1620   "A list of variables that have separate values in different newsgroups.
1621 A list of newsgroup (summary buffer) local variables, or cons of
1622 variables and their default expressions to be evalled (when the default
1623 values are not nil), that should be made global while the summary buffer
1624 is active.
1625
1626 Note: The default expressions will be evaluated (using function `eval')
1627 before assignment to the local variable rather than just assigned to it.
1628 If the default expression is the symbol `global', that symbol will not
1629 be evaluated but the global value of the local variable will be used
1630 instead.
1631
1632 These variables can be used to set variables in the group parameters
1633 while still allowing them to affect operations done in other buffers.
1634 For example:
1635
1636 \(setq gnus-newsgroup-variables
1637      '(message-use-followup-to
1638        (gnus-visible-headers .
1639          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1640 ")
1641
1642 (eval-when-compile
1643   ;; Bind features so that require will believe that gnus-sum has
1644   ;; already been loaded (avoids infinite recursion)
1645   (let ((features (cons 'gnus-sum features)))
1646     (require 'gnus-art)))
1647
1648 ;; MIME stuff.
1649
1650 (defvar gnus-decode-encoded-word-methods
1651   '(mail-decode-encoded-word-string)
1652   "List of methods used to decode encoded words.
1653
1654 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1655 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1656 \(REGEXP . FUNCTION), FUNCTION will be applied only to the newsgroups
1657 whose names match REGEXP.
1658
1659 For example:
1660 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1661  mail-decode-encoded-word-string
1662  (\"chinese\" . rfc1843-decode-string))")
1663
1664 (defvar gnus-decode-encoded-word-methods-cache nil)
1665
1666 (defun gnus-multi-decode-encoded-word-string (string)
1667   "Apply the functions from `gnus-encoded-word-methods' that match."
1668   (unless (and gnus-decode-encoded-word-methods-cache
1669                (eq gnus-newsgroup-name
1670                    (car gnus-decode-encoded-word-methods-cache)))
1671     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1672     (dolist (method gnus-decode-encoded-word-methods)
1673       (if (symbolp method)
1674           (nconc gnus-decode-encoded-word-methods-cache (list method))
1675         (if (and gnus-newsgroup-name
1676                  (string-match (car method) gnus-newsgroup-name))
1677             (nconc gnus-decode-encoded-word-methods-cache
1678                    (list (cdr method)))))))
1679   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1680     (setq string (funcall method string))))
1681
1682 ;; Subject simplification.
1683
1684 (defun gnus-simplify-whitespace (str)
1685   "Remove excessive whitespace from STR."
1686   ;; Multiple spaces.
1687   (while (string-match "[ \t][ \t]+" str)
1688     (setq str (concat (substring str 0 (match-beginning 0))
1689                         " "
1690                         (substring str (match-end 0)))))
1691   ;; Leading spaces.
1692   (when (string-match "^[ \t]+" str)
1693     (setq str (substring str (match-end 0))))
1694   ;; Trailing spaces.
1695   (when (string-match "[ \t]+$" str)
1696     (setq str (substring str 0 (match-beginning 0))))
1697   str)
1698
1699 (defun gnus-simplify-all-whitespace (str)
1700   "Remove all whitespace from STR."
1701   (while (string-match "[ \t\n]+" str)
1702     (setq str (replace-match "" nil nil str)))
1703   str)
1704
1705 (defsubst gnus-simplify-subject-re (subject)
1706   "Remove \"Re:\" from subject lines."
1707   (if (string-match message-subject-re-regexp subject)
1708       (substring subject (match-end 0))
1709     subject))
1710
1711 (defun gnus-simplify-subject (subject &optional re-only)
1712   "Remove `Re:' and words in parentheses.
1713 If RE-ONLY is non-nil, strip leading `Re:'s only."
1714   (let ((case-fold-search t))           ;Ignore case.
1715     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1716     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1717       (setq subject (substring subject (match-end 0))))
1718     ;; Remove uninteresting prefixes.
1719     (when (and (not re-only)
1720                gnus-simplify-ignored-prefixes
1721                (string-match gnus-simplify-ignored-prefixes subject))
1722       (setq subject (substring subject (match-end 0))))
1723     ;; Remove words in parentheses from end.
1724     (unless re-only
1725       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1726         (setq subject (substring subject 0 (match-beginning 0)))))
1727     ;; Return subject string.
1728     subject))
1729
1730 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1731 ;; all whitespace.
1732 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1733   (goto-char (point-min))
1734   (while (re-search-forward regexp nil t)
1735     (replace-match (or newtext ""))))
1736
1737 (defun gnus-simplify-buffer-fuzzy (regexp)
1738   "Simplify string in the buffer fuzzily.
1739 The string in the accessible portion of the current buffer is simplified.
1740 It is assumed to be a single-line subject.
1741 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1742 matter is removed.  Additional things can be deleted by setting
1743 `gnus-simplify-subject-fuzzy-regexp'."
1744   (let ((case-fold-search t)
1745         (modified-tick))
1746     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1747
1748     (while (not (eq modified-tick (buffer-modified-tick)))
1749       (setq modified-tick (buffer-modified-tick))
1750       (cond
1751        ((listp regexp)
1752         (mapc 'gnus-simplify-buffer-fuzzy-step regexp))
1753        (regexp
1754         (gnus-simplify-buffer-fuzzy-step regexp)))
1755       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1756       (gnus-simplify-buffer-fuzzy-step
1757        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1758       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1759
1760     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1761     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1762     (gnus-simplify-buffer-fuzzy-step " $")
1763     (gnus-simplify-buffer-fuzzy-step "^ +")))
1764
1765 (defun gnus-simplify-subject-fuzzy (subject)
1766   "Simplify a subject string fuzzily.
1767 See `gnus-simplify-buffer-fuzzy' for details."
1768   (save-excursion
1769     (let ((regexp gnus-simplify-subject-fuzzy-regexp))
1770       (gnus-set-work-buffer)
1771       (let ((case-fold-search t))
1772         ;; Remove uninteresting prefixes.
1773         (when (and gnus-simplify-ignored-prefixes
1774                    (string-match gnus-simplify-ignored-prefixes subject))
1775           (setq subject (substring subject (match-end 0))))
1776         (insert subject)
1777         (inline (gnus-simplify-buffer-fuzzy regexp))
1778         (buffer-string)))))
1779
1780 (defsubst gnus-simplify-subject-fully (subject)
1781   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1782   (cond
1783    (gnus-simplify-subject-functions
1784     (gnus-map-function gnus-simplify-subject-functions subject))
1785    ((null gnus-summary-gather-subject-limit)
1786     (gnus-simplify-subject-re subject))
1787    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1788     (gnus-simplify-subject-fuzzy subject))
1789    ((numberp gnus-summary-gather-subject-limit)
1790     (truncate-string-to-width (gnus-simplify-subject-re subject)
1791                               gnus-summary-gather-subject-limit))
1792    (t
1793     subject)))
1794
1795 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1796   "Check whether two subjects are equal.
1797 If optional argument SIMPLE-FIRST is t, first argument is already
1798 simplified."
1799   (cond
1800    ((null simple-first)
1801     (equal (gnus-simplify-subject-fully s1)
1802            (gnus-simplify-subject-fully s2)))
1803    (t
1804     (equal s1
1805            (gnus-simplify-subject-fully s2)))))
1806
1807 (defun gnus-summary-bubble-group ()
1808   "Increase the score of the current group.
1809 This is a handy function to add to `gnus-summary-exit-hook' to
1810 increase the score of each group you read."
1811   (gnus-group-add-score gnus-newsgroup-name))
1812
1813 \f
1814 ;;;
1815 ;;; Gnus summary mode
1816 ;;;
1817
1818 (put 'gnus-summary-mode 'mode-class 'special)
1819
1820 (defvar gnus-article-commands-menu)
1821
1822 ;; Non-orthogonal keys
1823
1824 (gnus-define-keys gnus-summary-mode-map
1825   " " gnus-summary-next-page
1826   "\177" gnus-summary-prev-page
1827   [delete] gnus-summary-prev-page
1828   [backspace] gnus-summary-prev-page
1829   "\r" gnus-summary-scroll-up
1830   "\M-\r" gnus-summary-scroll-down
1831   "n" gnus-summary-next-unread-article
1832   "p" gnus-summary-prev-unread-article
1833   "N" gnus-summary-next-article
1834   "P" gnus-summary-prev-article
1835   "\M-\C-n" gnus-summary-next-same-subject
1836   "\M-\C-p" gnus-summary-prev-same-subject
1837   "\M-n" gnus-summary-next-unread-subject
1838   "\M-p" gnus-summary-prev-unread-subject
1839   "." gnus-summary-first-unread-article
1840   "," gnus-summary-best-unread-article
1841   "\M-s" gnus-summary-search-article-forward
1842   "\M-r" gnus-summary-search-article-backward
1843   "\M-S" gnus-summary-repeat-search-article-forward
1844   "\M-R" gnus-summary-repeat-search-article-backward
1845   "<" gnus-summary-beginning-of-article
1846   ">" gnus-summary-end-of-article
1847   "j" gnus-summary-goto-article
1848   "^" gnus-summary-refer-parent-article
1849   "\M-^" gnus-summary-refer-article
1850   "u" gnus-summary-tick-article-forward
1851   "!" gnus-summary-tick-article-forward
1852   "U" gnus-summary-tick-article-backward
1853   "d" gnus-summary-mark-as-read-forward
1854   "D" gnus-summary-mark-as-read-backward
1855   "E" gnus-summary-mark-as-expirable
1856   "\M-u" gnus-summary-clear-mark-forward
1857   "\M-U" gnus-summary-clear-mark-backward
1858   "k" gnus-summary-kill-same-subject-and-select
1859   "\C-k" gnus-summary-kill-same-subject
1860   "\M-\C-k" gnus-summary-kill-thread
1861   "\M-\C-l" gnus-summary-lower-thread
1862   "e" gnus-summary-edit-article
1863   "#" gnus-summary-mark-as-processable
1864   "\M-#" gnus-summary-unmark-as-processable
1865   "\M-\C-t" gnus-summary-toggle-threads
1866   "\M-\C-s" gnus-summary-show-thread
1867   "\M-\C-h" gnus-summary-hide-thread
1868   "\M-\C-f" gnus-summary-next-thread
1869   "\M-\C-b" gnus-summary-prev-thread
1870   [(meta down)] gnus-summary-next-thread
1871   [(meta up)] gnus-summary-prev-thread
1872   "\M-\C-u" gnus-summary-up-thread
1873   "\M-\C-d" gnus-summary-down-thread
1874   "&" gnus-summary-execute-command
1875   "c" gnus-summary-catchup-and-exit
1876   "\C-w" gnus-summary-mark-region-as-read
1877   "\C-t" gnus-summary-toggle-truncation
1878   "?" gnus-summary-mark-as-dormant
1879   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1880   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1881   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1882   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1883   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1884   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1885   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1886   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1887   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1888   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1889   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1890   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1891   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1892   "=" gnus-summary-expand-window
1893   "\C-x\C-s" gnus-summary-reselect-current-group
1894   "\M-g" gnus-summary-rescan-group
1895   "\C-c\C-r" gnus-summary-caesar-message
1896   "f" gnus-summary-followup
1897   "F" gnus-summary-followup-with-original
1898   "C" gnus-summary-cancel-article
1899   "r" gnus-summary-reply
1900   "R" gnus-summary-reply-with-original
1901   "\C-c\C-f" gnus-summary-mail-forward
1902   "o" gnus-summary-save-article
1903   "\C-o" gnus-summary-save-article-mail
1904   "|" gnus-summary-pipe-output
1905   "\M-k" gnus-summary-edit-local-kill
1906   "\M-K" gnus-summary-edit-global-kill
1907   ;; "V" gnus-version
1908   "\C-c\C-d" gnus-summary-describe-group
1909   "q" gnus-summary-exit
1910   "Q" gnus-summary-exit-no-update
1911   "\C-c\C-i" gnus-info-find-node
1912   gnus-mouse-2 gnus-mouse-pick-article
1913   [follow-link] mouse-face
1914   "m" gnus-summary-mail-other-window
1915   "a" gnus-summary-post-news
1916   "x" gnus-summary-limit-to-unread
1917   "s" gnus-summary-isearch-article
1918   [tab] gnus-summary-widget-forward
1919   [backtab] gnus-summary-widget-backward
1920   "t" gnus-summary-toggle-header
1921   "g" gnus-summary-show-article
1922   "l" gnus-summary-goto-last-article
1923   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1924   "\C-d" gnus-summary-enter-digest-group
1925   "\M-\C-d" gnus-summary-read-document
1926   "\M-\C-e" gnus-summary-edit-parameters
1927   "\M-\C-a" gnus-summary-customize-parameters
1928   "\C-c\C-b" gnus-bug
1929   "*" gnus-cache-enter-article
1930   "\M-*" gnus-cache-remove-article
1931   "\M-&" gnus-summary-universal-argument
1932   "\C-l" gnus-recenter
1933   "I" gnus-summary-increase-score
1934   "L" gnus-summary-lower-score
1935   "\M-i" gnus-symbolic-argument
1936   "h" gnus-summary-select-article-buffer
1937
1938   "b" gnus-article-view-part
1939   "\M-t" gnus-summary-toggle-display-buttonized
1940
1941   "V" gnus-summary-score-map
1942   "X" gnus-uu-extract-map
1943   "S" gnus-summary-send-map)
1944
1945 ;; Sort of orthogonal keymap
1946 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1947   "t" gnus-summary-tick-article-forward
1948   "!" gnus-summary-tick-article-forward
1949   "d" gnus-summary-mark-as-read-forward
1950   "r" gnus-summary-mark-as-read-forward
1951   "c" gnus-summary-clear-mark-forward
1952   " " gnus-summary-clear-mark-forward
1953   "e" gnus-summary-mark-as-expirable
1954   "x" gnus-summary-mark-as-expirable
1955   "?" gnus-summary-mark-as-dormant
1956   "b" gnus-summary-set-bookmark
1957   "B" gnus-summary-remove-bookmark
1958   "#" gnus-summary-mark-as-processable
1959   "\M-#" gnus-summary-unmark-as-processable
1960   "S" gnus-summary-limit-include-expunged
1961   "C" gnus-summary-catchup
1962   "H" gnus-summary-catchup-to-here
1963   "h" gnus-summary-catchup-from-here
1964   "\C-c" gnus-summary-catchup-all
1965   "k" gnus-summary-kill-same-subject-and-select
1966   "K" gnus-summary-kill-same-subject
1967   "P" gnus-uu-mark-map)
1968
1969 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1970   "c" gnus-summary-clear-above
1971   "u" gnus-summary-tick-above
1972   "m" gnus-summary-mark-above
1973   "k" gnus-summary-kill-below)
1974
1975 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1976   "/" gnus-summary-limit-to-subject
1977   "n" gnus-summary-limit-to-articles
1978   "b" gnus-summary-limit-to-bodies
1979   "h" gnus-summary-limit-to-headers
1980   "w" gnus-summary-pop-limit
1981   "s" gnus-summary-limit-to-subject
1982   "a" gnus-summary-limit-to-author
1983   "u" gnus-summary-limit-to-unread
1984   "m" gnus-summary-limit-to-marks
1985   "M" gnus-summary-limit-exclude-marks
1986   "v" gnus-summary-limit-to-score
1987   "*" gnus-summary-limit-include-cached
1988   "D" gnus-summary-limit-include-dormant
1989   "T" gnus-summary-limit-include-thread
1990   "d" gnus-summary-limit-exclude-dormant
1991   "t" gnus-summary-limit-to-age
1992   "." gnus-summary-limit-to-unseen
1993   "x" gnus-summary-limit-to-extra
1994   "p" gnus-summary-limit-to-display-predicate
1995   "E" gnus-summary-limit-include-expunged
1996   "c" gnus-summary-limit-exclude-childless-dormant
1997   "C" gnus-summary-limit-mark-excluded-as-read
1998   "o" gnus-summary-insert-old-articles
1999   "N" gnus-summary-insert-new-articles
2000   "S" gnus-summary-limit-to-singletons
2001   "r" gnus-summary-limit-to-replied
2002   "R" gnus-summary-limit-to-recipient
2003   "A" gnus-summary-limit-to-address)
2004
2005 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2006   "n" gnus-summary-next-unread-article
2007   "p" gnus-summary-prev-unread-article
2008   "N" gnus-summary-next-article
2009   "P" gnus-summary-prev-article
2010   "\C-n" gnus-summary-next-same-subject
2011   "\C-p" gnus-summary-prev-same-subject
2012   "\M-n" gnus-summary-next-unread-subject
2013   "\M-p" gnus-summary-prev-unread-subject
2014   "f" gnus-summary-first-unread-article
2015   "b" gnus-summary-best-unread-article
2016   "j" gnus-summary-goto-article
2017   "g" gnus-summary-goto-subject
2018   "l" gnus-summary-goto-last-article
2019   "o" gnus-summary-pop-article)
2020
2021 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2022   "k" gnus-summary-kill-thread
2023   "E" gnus-summary-expire-thread
2024   "l" gnus-summary-lower-thread
2025   "i" gnus-summary-raise-thread
2026   "T" gnus-summary-toggle-threads
2027   "t" gnus-summary-rethread-current
2028   "^" gnus-summary-reparent-thread
2029   "\M-^" gnus-summary-reparent-children
2030   "s" gnus-summary-show-thread
2031   "S" gnus-summary-show-all-threads
2032   "h" gnus-summary-hide-thread
2033   "H" gnus-summary-hide-all-threads
2034   "n" gnus-summary-next-thread
2035   "p" gnus-summary-prev-thread
2036   "u" gnus-summary-up-thread
2037   "o" gnus-summary-top-thread
2038   "d" gnus-summary-down-thread
2039   "#" gnus-uu-mark-thread
2040   "\M-#" gnus-uu-unmark-thread)
2041
2042 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2043   "g" gnus-summary-prepare
2044   "c" gnus-summary-insert-cached-articles
2045   "d" gnus-summary-insert-dormant-articles
2046   "t" gnus-summary-insert-ticked-articles)
2047
2048 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2049   "c" gnus-summary-catchup-and-exit
2050   "C" gnus-summary-catchup-all-and-exit
2051   "E" gnus-summary-exit-no-update
2052   "Q" gnus-summary-exit
2053   "Z" gnus-summary-exit
2054   "n" gnus-summary-catchup-and-goto-next-group
2055   "p" gnus-summary-catchup-and-goto-prev-group
2056   "R" gnus-summary-reselect-current-group
2057   "G" gnus-summary-rescan-group
2058   "N" gnus-summary-next-group
2059   "s" gnus-summary-save-newsrc
2060   "P" gnus-summary-prev-group)
2061
2062 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2063   " " gnus-summary-next-page
2064   "n" gnus-summary-next-page
2065   "\177" gnus-summary-prev-page
2066   [delete] gnus-summary-prev-page
2067   "p" gnus-summary-prev-page
2068   "\r" gnus-summary-scroll-up
2069   "\M-\r" gnus-summary-scroll-down
2070   "<" gnus-summary-beginning-of-article
2071   ">" gnus-summary-end-of-article
2072   "b" gnus-summary-beginning-of-article
2073   "e" gnus-summary-end-of-article
2074   "^" gnus-summary-refer-parent-article
2075   "r" gnus-summary-refer-parent-article
2076   "C" gnus-summary-show-complete-article
2077   "D" gnus-summary-enter-digest-group
2078   "R" gnus-summary-refer-references
2079   "T" gnus-summary-refer-thread
2080   "W" gnus-warp-to-article
2081   "g" gnus-summary-show-article
2082   "s" gnus-summary-isearch-article
2083   [tab] gnus-summary-widget-forward
2084   [backtab] gnus-summary-widget-backward
2085   "P" gnus-summary-print-article
2086   "S" gnus-sticky-article
2087   "M" gnus-mailing-list-insinuate
2088   "t" gnus-article-babel)
2089
2090 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2091   "b" gnus-article-add-buttons
2092   "B" gnus-article-add-buttons-to-head
2093   "o" gnus-article-treat-overstrike
2094   "e" gnus-article-emphasize
2095   "w" gnus-article-fill-cited-article
2096   "Q" gnus-article-fill-long-lines
2097   "L" gnus-article-toggle-truncate-lines
2098   "C" gnus-article-capitalize-sentences
2099   "c" gnus-article-remove-cr
2100   "q" gnus-article-de-quoted-unreadable
2101   "6" gnus-article-de-base64-unreadable
2102   "Z" gnus-article-decode-HZ
2103   "A" gnus-article-treat-ansi-sequences
2104   "h" gnus-article-wash-html
2105   "u" gnus-article-unsplit-urls
2106   "s" gnus-summary-force-verify-and-decrypt
2107   "f" gnus-article-display-x-face
2108   "l" gnus-summary-stop-page-breaking
2109   "r" gnus-summary-caesar-message
2110   "m" gnus-summary-morse-message
2111   "t" gnus-summary-toggle-header
2112   "g" gnus-treat-smiley
2113   "v" gnus-summary-verbose-headers
2114   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2115   "p" gnus-article-verify-x-pgp-sig
2116   "d" gnus-article-treat-dumbquotes
2117   "U" gnus-article-treat-non-ascii
2118   "i" gnus-summary-idna-message)
2119
2120 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2121   ;; mnemonic: deuglif*Y*
2122   "u" gnus-article-outlook-unwrap-lines
2123   "a" gnus-article-outlook-repair-attribution
2124   "c" gnus-article-outlook-rearrange-citation
2125   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2126
2127 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2128   "a" gnus-article-hide
2129   "h" gnus-article-hide-headers
2130   "b" gnus-article-hide-boring-headers
2131   "s" gnus-article-hide-signature
2132   "c" gnus-article-hide-citation
2133   "C" gnus-article-hide-citation-in-followups
2134   "l" gnus-article-hide-list-identifiers
2135   "B" gnus-article-strip-banner
2136   "P" gnus-article-hide-pem
2137   "\C-c" gnus-article-hide-citation-maybe)
2138
2139 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2140   "a" gnus-article-highlight
2141   "h" gnus-article-highlight-headers
2142   "c" gnus-article-highlight-citation
2143   "s" gnus-article-highlight-signature)
2144
2145 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2146   "f" gnus-article-treat-fold-headers
2147   "u" gnus-article-treat-unfold-headers
2148   "n" gnus-article-treat-fold-newsgroups)
2149
2150 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2151   "x" gnus-article-display-x-face
2152   "d" gnus-article-display-face
2153   "s" gnus-treat-smiley
2154   "D" gnus-article-remove-images
2155   "W" gnus-article-show-images
2156   "f" gnus-treat-from-picon
2157   "m" gnus-treat-mail-picon
2158   "n" gnus-treat-newsgroups-picon
2159   "g" gnus-treat-from-gravatar
2160   "h" gnus-treat-mail-gravatar)
2161
2162 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2163   "w" gnus-article-decode-mime-words
2164   "c" gnus-article-decode-charset
2165   "v" gnus-mime-view-all-parts
2166   "b" gnus-article-view-part)
2167
2168 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2169   "z" gnus-article-date-ut
2170   "u" gnus-article-date-ut
2171   "l" gnus-article-date-local
2172   "p" gnus-article-date-english
2173   "e" gnus-article-date-lapsed
2174   "o" gnus-article-date-original
2175   "i" gnus-article-date-iso8601
2176   "s" gnus-article-date-user)
2177
2178 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2179   "t" gnus-article-remove-trailing-blank-lines
2180   "l" gnus-article-strip-leading-blank-lines
2181   "m" gnus-article-strip-multiple-blank-lines
2182   "a" gnus-article-strip-blank-lines
2183   "A" gnus-article-strip-all-blank-lines
2184   "s" gnus-article-strip-leading-space
2185   "e" gnus-article-strip-trailing-space
2186   "w" gnus-article-remove-leading-whitespace)
2187
2188 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2189   "v" gnus-version
2190   "d" gnus-summary-describe-group
2191   "h" gnus-summary-describe-briefly
2192   "i" gnus-info-find-node)
2193
2194 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2195   "e" gnus-summary-expire-articles
2196   "\M-\C-e" gnus-summary-expire-articles-now
2197   "\177" gnus-summary-delete-article
2198   [delete] gnus-summary-delete-article
2199   [backspace] gnus-summary-delete-article
2200   "m" gnus-summary-move-article
2201   "r" gnus-summary-respool-article
2202   "w" gnus-summary-edit-article
2203   "c" gnus-summary-copy-article
2204   "B" gnus-summary-crosspost-article
2205   "q" gnus-summary-respool-query
2206   "t" gnus-summary-respool-trace
2207   "i" gnus-summary-import-article
2208   "I" gnus-summary-create-article
2209   "p" gnus-summary-article-posted-p)
2210
2211 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2212   "o" gnus-summary-save-article
2213   "m" gnus-summary-save-article-mail
2214   "F" gnus-summary-write-article-file
2215   "r" gnus-summary-save-article-rmail
2216   "f" gnus-summary-save-article-file
2217   "b" gnus-summary-save-article-body-file
2218   "B" gnus-summary-write-article-body-file
2219   "h" gnus-summary-save-article-folder
2220   "v" gnus-summary-save-article-vm
2221   "p" gnus-summary-pipe-output
2222   "P" gnus-summary-muttprint)
2223
2224 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2225   "b" gnus-summary-display-buttonized
2226   "m" gnus-summary-repair-multipart
2227   "v" gnus-article-view-part
2228   "o" gnus-article-save-part
2229   "O" gnus-article-save-part-and-strip
2230   "r" gnus-article-replace-part
2231   "d" gnus-article-delete-part
2232   "t" gnus-article-view-part-as-type
2233   "j" gnus-article-jump-to-part
2234   "c" gnus-article-copy-part
2235   "C" gnus-article-view-part-as-charset
2236   "e" gnus-article-view-part-externally
2237   "H" gnus-article-browse-html-article
2238   "E" gnus-article-encrypt-body
2239   "i" gnus-article-inline-part
2240   "|" gnus-article-pipe-part)
2241
2242 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2243   "p" gnus-summary-mark-as-processable
2244   "u" gnus-summary-unmark-as-processable
2245   "U" gnus-summary-unmark-all-processable
2246   "v" gnus-uu-mark-over
2247   "s" gnus-uu-mark-series
2248   "r" gnus-uu-mark-region
2249   "g" gnus-uu-unmark-region
2250   "R" gnus-uu-mark-by-regexp
2251   "G" gnus-uu-unmark-by-regexp
2252   "t" gnus-uu-mark-thread
2253   "T" gnus-uu-unmark-thread
2254   "a" gnus-uu-mark-all
2255   "b" gnus-uu-mark-buffer
2256   "S" gnus-uu-mark-sparse
2257   "k" gnus-summary-kill-process-mark
2258   "y" gnus-summary-yank-process-mark
2259   "w" gnus-summary-save-process-mark
2260   "i" gnus-uu-invert-processable)
2261
2262 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2263   ;;"x" gnus-uu-extract-any
2264   "m" gnus-summary-save-parts
2265   "u" gnus-uu-decode-uu
2266   "U" gnus-uu-decode-uu-and-save
2267   "s" gnus-uu-decode-unshar
2268   "S" gnus-uu-decode-unshar-and-save
2269   "o" gnus-uu-decode-save
2270   "O" gnus-uu-decode-save
2271   "b" gnus-uu-decode-binhex
2272   "B" gnus-uu-decode-binhex
2273   "Y" gnus-uu-decode-yenc
2274   "p" gnus-uu-decode-postscript
2275   "P" gnus-uu-decode-postscript-and-save)
2276
2277 (gnus-define-keys
2278     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2279   "u" gnus-uu-decode-uu-view
2280   "U" gnus-uu-decode-uu-and-save-view
2281   "s" gnus-uu-decode-unshar-view
2282   "S" gnus-uu-decode-unshar-and-save-view
2283   "o" gnus-uu-decode-save-view
2284   "O" gnus-uu-decode-save-view
2285   "b" gnus-uu-decode-binhex-view
2286   "B" gnus-uu-decode-binhex-view
2287   "p" gnus-uu-decode-postscript-view
2288   "P" gnus-uu-decode-postscript-and-save-view)
2289
2290 (defvar gnus-article-post-menu nil)
2291
2292 (defconst gnus-summary-menu-maxlen 20)
2293
2294 (defun gnus-summary-menu-split (menu)
2295   ;; If we have lots of elements, divide them into groups of 20
2296   ;; and make a pane (or submenu) for each one.
2297   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2298       (let ((menu menu) sublists next
2299             (i 1))
2300         (while menu
2301           ;; Pull off the next gnus-summary-menu-maxlen elements
2302           ;; and make them the next element of sublist.
2303           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2304           (if next
2305               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2306                       nil))
2307           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2308                                              (aref (car (last menu)) 0)) menu)
2309                                sublists))
2310           (setq i (1+ i))
2311           (setq menu next))
2312         (nreverse sublists))
2313     ;; Few elements--put them all in one pane.
2314     menu))
2315
2316 (defun gnus-summary-make-menu-bar ()
2317   (gnus-turn-off-edit-menu 'summary)
2318
2319   (unless (boundp 'gnus-summary-misc-menu)
2320
2321     (easy-menu-define
2322       gnus-summary-kill-menu gnus-summary-mode-map ""
2323       (cons
2324        "Score"
2325        (nconc
2326         (list
2327          ["Customize" gnus-score-customize t])
2328         (gnus-make-score-map 'increase)
2329         (gnus-make-score-map 'lower)
2330         '(("Mark"
2331            ["Kill below" gnus-summary-kill-below t]
2332            ["Mark above" gnus-summary-mark-above t]
2333            ["Tick above" gnus-summary-tick-above t]
2334            ["Clear above" gnus-summary-clear-above t])
2335           ["Current score" gnus-summary-current-score t]
2336           ["Set score" gnus-summary-set-score t]
2337           ["Switch current score file..." gnus-score-change-score-file t]
2338           ["Set mark below..." gnus-score-set-mark-below t]
2339           ["Set expunge below..." gnus-score-set-expunge-below t]
2340           ["Edit current score file" gnus-score-edit-current-scores t]
2341           ["Edit score file..." gnus-score-edit-file t]
2342           ["Trace score" gnus-score-find-trace t]
2343           ["Find words" gnus-score-find-favourite-words t]
2344           ["Rescore buffer" gnus-summary-rescore t]
2345           ["Increase score..." gnus-summary-increase-score t]
2346           ["Lower score..." gnus-summary-lower-score t]))))
2347
2348     ;; Define both the Article menu in the summary buffer and the
2349     ;; equivalent Commands menu in the article buffer here for
2350     ;; consistency.
2351     (let ((innards
2352            `(("Hide"
2353               ["All" gnus-article-hide t]
2354               ["Headers" gnus-article-hide-headers t]
2355               ["Signature" gnus-article-hide-signature t]
2356               ["Citation" gnus-article-hide-citation t]
2357               ["List identifiers" gnus-article-hide-list-identifiers t]
2358               ["Banner" gnus-article-strip-banner t]
2359               ["Boring headers" gnus-article-hide-boring-headers t])
2360              ("Highlight"
2361               ["All" gnus-article-highlight t]
2362               ["Headers" gnus-article-highlight-headers t]
2363               ["Signature" gnus-article-highlight-signature t]
2364               ["Citation" gnus-article-highlight-citation t])
2365              ("MIME"
2366               ["Words" gnus-article-decode-mime-words t]
2367               ["Charset" gnus-article-decode-charset t]
2368               ["QP" gnus-article-de-quoted-unreadable t]
2369               ["Base64" gnus-article-de-base64-unreadable t]
2370               ["View MIME buttons" gnus-summary-display-buttonized t]
2371               ["View all" gnus-mime-view-all-parts t]
2372               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2373               ["Encrypt body" gnus-article-encrypt-body
2374                :active (not (gnus-group-read-only-p))
2375                ,@(if (featurep 'xemacs) nil
2376                    '(:help "Encrypt the message body on disk"))]
2377               ["Extract all parts..." gnus-summary-save-parts t]
2378               ("Multipart"
2379                ["Repair multipart" gnus-summary-repair-multipart t]
2380                ["Pipe part..." gnus-article-pipe-part t]
2381                ["Inline part" gnus-article-inline-part t]
2382                ["View part as type..." gnus-article-view-part-as-type t]
2383                ["Encrypt body" gnus-article-encrypt-body
2384                 :active (not (gnus-group-read-only-p))
2385                ,@(if (featurep 'xemacs) nil
2386                    '(:help "Encrypt the message body on disk"))]
2387                ["View part externally" gnus-article-view-part-externally t]
2388                ["View HTML parts in browser" gnus-article-browse-html-article t]
2389                ["View part with charset..." gnus-article-view-part-as-charset t]
2390                ["Copy part" gnus-article-copy-part t]
2391                ["Save part..." gnus-article-save-part t]
2392                ["View part" gnus-article-view-part t]))
2393              ("Date"
2394               ["Local" gnus-article-date-local t]
2395               ["ISO8601" gnus-article-date-iso8601 t]
2396               ["UT" gnus-article-date-ut t]
2397               ["Original" gnus-article-date-original t]
2398               ["Lapsed" gnus-article-date-lapsed t]
2399               ["User-defined" gnus-article-date-user t])
2400              ("Display"
2401               ["Remove images" gnus-article-remove-images t]
2402               ["Toggle smiley" gnus-treat-smiley t]
2403               ["Show X-Face" gnus-article-display-x-face t]
2404               ["Show picons in From" gnus-treat-from-picon t]
2405               ["Show picons in mail headers" gnus-treat-mail-picon t]
2406               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2407               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2408               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2409               ("View as different encoding"
2410                ,@(gnus-summary-menu-split
2411                   (mapcar
2412                    (lambda (cs)
2413                      ;; Since easymenu under Emacs doesn't allow
2414                      ;; lambda forms for menu commands, we should
2415                      ;; provide intern'ed function symbols.
2416                      (let ((command (intern (format "\
2417 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2418                        (fset command
2419                              `(lambda ()
2420                                 (interactive)
2421                                 (let ((gnus-summary-show-article-charset-alist
2422                                        '((1 . ,cs))))
2423                                   (gnus-summary-show-article 1))))
2424                        `[,(symbol-name cs) ,command t]))
2425                    (sort (if (fboundp 'coding-system-list)
2426                              (coding-system-list)
2427                            (mapcar 'car mm-mime-mule-charset-alist))
2428                          'string<)))))
2429              ("Washing"
2430               ("Remove Blanks"
2431                ["Leading" gnus-article-strip-leading-blank-lines t]
2432                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2433                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2434                ["All of the above" gnus-article-strip-blank-lines t]
2435                ["All" gnus-article-strip-all-blank-lines t]
2436                ["Leading space" gnus-article-strip-leading-space t]
2437                ["Trailing space" gnus-article-strip-trailing-space t]
2438                ["Leading space in headers"
2439                 gnus-article-remove-leading-whitespace t])
2440               ["Overstrike" gnus-article-treat-overstrike t]
2441               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2442               ["Non-ASCII" gnus-article-treat-non-ascii t]
2443               ["Emphasis" gnus-article-emphasize t]
2444               ["Word wrap" gnus-article-fill-cited-article t]
2445               ["Fill long lines" gnus-article-fill-long-lines t]
2446               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2447               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2448               ["Remove CR" gnus-article-remove-cr t]
2449               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2450               ["Base64" gnus-article-de-base64-unreadable t]
2451               ["Rot 13" gnus-summary-caesar-message
2452                ,@(if (featurep 'xemacs) '(t)
2453                    '(:help "\"Caesar rotate\" article by 13"))]
2454               ["De-IDNA" gnus-summary-idna-message t]
2455               ["Morse decode" gnus-summary-morse-message t]
2456               ["Unix pipe..." gnus-summary-pipe-message t]
2457               ["Add buttons" gnus-article-add-buttons t]
2458               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2459               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2460               ["Verbose header" gnus-summary-verbose-headers t]
2461               ["Toggle header" gnus-summary-toggle-header t]
2462               ["Unfold headers" gnus-article-treat-unfold-headers t]
2463               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2464               ["Html" gnus-article-wash-html t]
2465               ["Unsplit URLs" gnus-article-unsplit-urls t]
2466               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2467               ["Decode HZ" gnus-article-decode-HZ t]
2468               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2469               ("(Outlook) Deuglify"
2470                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2471                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2472                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2473                ["Full (Outlook) deuglify"
2474                 gnus-article-outlook-deuglify-article t])
2475               )
2476              ("Output"
2477               ["Save in default format..." gnus-summary-save-article
2478                ,@(if (featurep 'xemacs) '(t)
2479                    '(:help "Save article using default method"))]
2480               ["Save in file..." gnus-summary-save-article-file
2481                ,@(if (featurep 'xemacs) '(t)
2482                    '(:help "Save article in file"))]
2483               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2484               ["Save in MH folder..." gnus-summary-save-article-folder t]
2485               ["Save in VM folder..." gnus-summary-save-article-vm t]
2486               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2487               ["Save body in file..." gnus-summary-save-article-body-file t]
2488               ["Pipe through a filter..." gnus-summary-pipe-output t]
2489               ["Print with Muttprint..." gnus-summary-muttprint t]
2490               ["Print" gnus-summary-print-article
2491                ,@(if (featurep 'xemacs) '(t)
2492                    '(:help "Generate and print a PostScript image"))])
2493              ("Copy, move,... (Backend)"
2494               ,@(if (featurep 'xemacs) nil
2495                   '(:help "Copying, moving, expiring articles..."))
2496               ["Respool article..." gnus-summary-respool-article t]
2497               ["Move article..." gnus-summary-move-article
2498                (gnus-check-backend-function
2499                 'request-move-article gnus-newsgroup-name)]
2500               ["Copy article..." gnus-summary-copy-article t]
2501               ["Crosspost article..." gnus-summary-crosspost-article
2502                (gnus-check-backend-function
2503                 'request-replace-article gnus-newsgroup-name)]
2504               ["Import file..." gnus-summary-import-article
2505                (gnus-check-backend-function
2506                 'request-accept-article gnus-newsgroup-name)]
2507               ["Create article..." gnus-summary-create-article
2508                (gnus-check-backend-function
2509                 'request-accept-article gnus-newsgroup-name)]
2510               ["Check if posted" gnus-summary-article-posted-p t]
2511               ["Edit article" gnus-summary-edit-article
2512                (not (gnus-group-read-only-p))]
2513               ["Delete article" gnus-summary-delete-article
2514                (gnus-check-backend-function
2515                 'request-expire-articles gnus-newsgroup-name)]
2516               ["Query respool" gnus-summary-respool-query t]
2517               ["Trace respool" gnus-summary-respool-trace t]
2518               ["Delete expirable articles" gnus-summary-expire-articles-now
2519                (gnus-check-backend-function
2520                 'request-expire-articles gnus-newsgroup-name)])
2521              ("Extract"
2522               ["Uudecode" gnus-uu-decode-uu
2523                ,@(if (featurep 'xemacs) '(t)
2524                    '(:help "Decode uuencoded article(s)"))]
2525               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2526               ["Unshar" gnus-uu-decode-unshar t]
2527               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2528               ["Save" gnus-uu-decode-save t]
2529               ["Binhex" gnus-uu-decode-binhex t]
2530               ["PostScript" gnus-uu-decode-postscript t]
2531               ["All MIME parts" gnus-summary-save-parts t])
2532              ("Cache"
2533               ["Enter article" gnus-cache-enter-article t]
2534               ["Remove article" gnus-cache-remove-article t])
2535              ["Translate" gnus-article-babel t]
2536              ["Select article buffer" gnus-summary-select-article-buffer t]
2537              ["Make article buffer sticky" gnus-sticky-article t]
2538              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2539              ["Isearch article..." gnus-summary-isearch-article t]
2540              ["Beginning of the article" gnus-summary-beginning-of-article t]
2541              ["End of the article" gnus-summary-end-of-article t]
2542              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2543              ["Fetch referenced articles" gnus-summary-refer-references t]
2544              ["Fetch current thread" gnus-summary-refer-thread t]
2545              ["Fetch article with id..." gnus-summary-refer-article t]
2546              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2547              ["Redisplay" gnus-summary-show-article t]
2548              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2549       (easy-menu-define
2550         gnus-summary-article-menu gnus-summary-mode-map ""
2551         (cons "Article" innards))
2552
2553       (if (not (keymapp gnus-summary-article-menu))
2554           (easy-menu-define
2555             gnus-article-commands-menu gnus-article-mode-map ""
2556             (cons "Commands" innards))
2557         ;; in Emacs, don't share menu.
2558         (setq gnus-article-commands-menu
2559               (copy-keymap gnus-summary-article-menu))
2560         (define-key gnus-article-mode-map [menu-bar commands]
2561           (cons "Commands" gnus-article-commands-menu))))
2562
2563     (easy-menu-define
2564       gnus-summary-thread-menu gnus-summary-mode-map ""
2565       '("Threads"
2566         ["Find all messages in thread" gnus-summary-refer-thread t]
2567         ["Toggle threading" gnus-summary-toggle-threads t]
2568         ["Hide threads" gnus-summary-hide-all-threads t]
2569         ["Show threads" gnus-summary-show-all-threads t]
2570         ["Hide thread" gnus-summary-hide-thread t]
2571         ["Show thread" gnus-summary-show-thread t]
2572         ["Go to next thread" gnus-summary-next-thread t]
2573         ["Go to previous thread" gnus-summary-prev-thread t]
2574         ["Go down thread" gnus-summary-down-thread t]
2575         ["Go up thread" gnus-summary-up-thread t]
2576         ["Top of thread" gnus-summary-top-thread t]
2577         ["Mark thread as read" gnus-summary-kill-thread t]
2578         ["Mark thread as expired" gnus-summary-expire-thread t]
2579         ["Lower thread score" gnus-summary-lower-thread t]
2580         ["Raise thread score" gnus-summary-raise-thread t]
2581         ["Rethread current" gnus-summary-rethread-current t]))
2582
2583     (easy-menu-define
2584       gnus-summary-post-menu gnus-summary-mode-map ""
2585       `("Post"
2586         ["Send a message (mail or news)" gnus-summary-post-news
2587          ,@(if (featurep 'xemacs) '(t)
2588              '(:help "Compose a new message (mail or news)"))]
2589         ["Followup" gnus-summary-followup
2590          ,@(if (featurep 'xemacs) '(t)
2591              '(:help "Post followup to this article"))]
2592         ["Followup and yank" gnus-summary-followup-with-original
2593          ,@(if (featurep 'xemacs) '(t)
2594              '(:help "Post followup to this article, quoting its contents"))]
2595         ["Supersede article" gnus-summary-supersede-article t]
2596         ["Cancel article" gnus-summary-cancel-article
2597          ,@(if (featurep 'xemacs) '(t)
2598              '(:help "Cancel an article you posted"))]
2599         ["Reply" gnus-summary-reply t]
2600         ["Reply and yank" gnus-summary-reply-with-original t]
2601         ["Wide reply" gnus-summary-wide-reply t]
2602         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2603          ,@(if (featurep 'xemacs) '(t)
2604              '(:help "Mail a reply, quoting this article"))]
2605         ["Very wide reply" gnus-summary-very-wide-reply t]
2606         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2607          ,@(if (featurep 'xemacs) '(t)
2608              '(:help "Mail a very wide reply, quoting this article"))]
2609         ["Mail forward" gnus-summary-mail-forward t]
2610         ["Post forward" gnus-summary-post-forward t]
2611         ["Digest and mail" gnus-uu-digest-mail-forward t]
2612         ["Digest and post" gnus-uu-digest-post-forward t]
2613         ["Resend message" gnus-summary-resend-message t]
2614         ["Resend message edit" gnus-summary-resend-message-edit t]
2615         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2616         ["Send a mail" gnus-summary-mail-other-window t]
2617         ["Create a local message" gnus-summary-news-other-window t]
2618         ["Uuencode and post" gnus-uu-post-news
2619          ,@(if (featurep 'xemacs) '(t)
2620              '(:help "Post a uuencoded article"))]
2621         ["Followup via news" gnus-summary-followup-to-mail t]
2622         ["Followup via news and yank"
2623          gnus-summary-followup-to-mail-with-original t]
2624         ["Strip signature on reply"
2625          (lambda ()
2626            (interactive)
2627            (if (not (memq message-cite-function
2628                           '(message-cite-original-without-signature
2629                             message-cite-original)))
2630                ;; Stupid workaround for XEmacs not honoring :visible.
2631                (message "Can't toggle this value of `message-cite-function'")
2632              (setq message-cite-function
2633                    (if (eq message-cite-function
2634                            'message-cite-original-without-signature)
2635                        'message-cite-original
2636                      'message-cite-original-without-signature))))
2637          ;; XEmacs barfs on :visible.
2638          ,@(if (featurep 'xemacs) nil
2639              '(:visible (memq message-cite-function
2640                               '(message-cite-original-without-signature
2641                                 message-cite-original))))
2642          :style toggle
2643          :selected (eq message-cite-function
2644                        'message-cite-original-without-signature)
2645          ,@(if (featurep 'xemacs) nil
2646              '(:help "Strip signature from cited article when replying."))]
2647         ;;("Draft"
2648         ;;["Send" gnus-summary-send-draft t]
2649         ;;["Send bounced" gnus-resend-bounced-mail t])
2650         ))
2651
2652     (cond
2653      ((not (keymapp gnus-summary-post-menu))
2654       (setq gnus-article-post-menu gnus-summary-post-menu))
2655      ((not gnus-article-post-menu)
2656       ;; Don't share post menu.
2657       (setq gnus-article-post-menu
2658             (copy-keymap gnus-summary-post-menu))))
2659     (define-key gnus-article-mode-map [menu-bar post]
2660       (cons "Post" gnus-article-post-menu))
2661
2662     (easy-menu-define
2663       gnus-summary-misc-menu gnus-summary-mode-map ""
2664       `("Gnus"
2665         ("Mark Read"
2666          ["Mark as read" gnus-summary-mark-as-read-forward t]
2667          ["Mark same subject and select"
2668           gnus-summary-kill-same-subject-and-select t]
2669          ["Mark same subject" gnus-summary-kill-same-subject t]
2670          ["Catchup" gnus-summary-catchup
2671           ,@(if (featurep 'xemacs) '(t)
2672               '(:help "Mark unread articles in this group as read"))]
2673          ["Catchup all" gnus-summary-catchup-all t]
2674          ["Catchup to here" gnus-summary-catchup-to-here t]
2675          ["Catchup from here" gnus-summary-catchup-from-here t]
2676          ["Catchup region" gnus-summary-mark-region-as-read
2677           (gnus-mark-active-p)]
2678          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2679         ("Mark Various"
2680          ["Tick" gnus-summary-tick-article-forward t]
2681          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2682          ["Remove marks" gnus-summary-clear-mark-forward t]
2683          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2684          ["Set bookmark" gnus-summary-set-bookmark t]
2685          ["Remove bookmark" gnus-summary-remove-bookmark t])
2686         ("Limit to"
2687          ["Marks..." gnus-summary-limit-to-marks t]
2688          ["Subject..." gnus-summary-limit-to-subject t]
2689          ["Author..." gnus-summary-limit-to-author t]
2690          ["Recipient..." gnus-summary-limit-to-recipient t]
2691          ["Address..." gnus-summary-limit-to-address t]
2692          ["Age..." gnus-summary-limit-to-age t]
2693          ["Extra..." gnus-summary-limit-to-extra t]
2694          ["Score..." gnus-summary-limit-to-score t]
2695          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2696          ["Unread" gnus-summary-limit-to-unread t]
2697          ["Unseen" gnus-summary-limit-to-unseen t]
2698          ["Singletons" gnus-summary-limit-to-singletons t]
2699          ["Replied" gnus-summary-limit-to-replied t]
2700          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2701          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2702          ["Pop limit" gnus-summary-pop-limit t]
2703          ["Show dormant" gnus-summary-limit-include-dormant t]
2704          ["Hide childless dormant"
2705           gnus-summary-limit-exclude-childless-dormant t]
2706          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2707          ["Hide marked" gnus-summary-limit-exclude-marks t]
2708          ["Show expunged" gnus-summary-limit-include-expunged t])
2709         ("Process Mark"
2710          ["Set mark" gnus-summary-mark-as-processable t]
2711          ["Remove mark" gnus-summary-unmark-as-processable t]
2712          ["Remove all marks" gnus-summary-unmark-all-processable t]
2713          ["Invert marks" gnus-uu-invert-processable t]
2714          ["Mark above" gnus-uu-mark-over t]
2715          ["Mark series" gnus-uu-mark-series t]
2716          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2717          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2718          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2719          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2720          ["Mark all" gnus-uu-mark-all t]
2721          ["Mark buffer" gnus-uu-mark-buffer t]
2722          ["Mark sparse" gnus-uu-mark-sparse t]
2723          ["Mark thread" gnus-uu-mark-thread t]
2724          ["Unmark thread" gnus-uu-unmark-thread t]
2725          ("Process Mark Sets"
2726           ["Kill" gnus-summary-kill-process-mark t]
2727           ["Yank" gnus-summary-yank-process-mark
2728            gnus-newsgroup-process-stack]
2729           ["Save" gnus-summary-save-process-mark t]
2730           ["Run command on marked..." gnus-summary-universal-argument t]))
2731         ("Registry Marks")
2732         ("Scroll article"
2733          ["Page forward" gnus-summary-next-page
2734           ,@(if (featurep 'xemacs) '(t)
2735               '(:help "Show next page of article"))]
2736          ["Page backward" gnus-summary-prev-page
2737           ,@(if (featurep 'xemacs) '(t)
2738               '(:help "Show previous page of article"))]
2739          ["Line forward" gnus-summary-scroll-up t])
2740         ("Move"
2741          ["Next unread article" gnus-summary-next-unread-article t]
2742          ["Previous unread article" gnus-summary-prev-unread-article t]
2743          ["Next article" gnus-summary-next-article t]
2744          ["Previous article" gnus-summary-prev-article t]
2745          ["Next unread subject" gnus-summary-next-unread-subject t]
2746          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2747          ["Next article same subject" gnus-summary-next-same-subject t]
2748          ["Previous article same subject" gnus-summary-prev-same-subject t]
2749          ["First unread article" gnus-summary-first-unread-article t]
2750          ["Best unread article" gnus-summary-best-unread-article t]
2751          ["Go to subject number..." gnus-summary-goto-subject t]
2752          ["Go to article number..." gnus-summary-goto-article t]
2753          ["Go to the last article" gnus-summary-goto-last-article t]
2754          ["Pop article off history" gnus-summary-pop-article t])
2755         ("Sort"
2756          ["Sort by number" gnus-summary-sort-by-number t]
2757          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2758          ["Sort by author" gnus-summary-sort-by-author t]
2759          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2760          ["Sort by subject" gnus-summary-sort-by-subject t]
2761          ["Sort by date" gnus-summary-sort-by-date t]
2762          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2763          ["Sort by score" gnus-summary-sort-by-score t]
2764          ["Sort by lines" gnus-summary-sort-by-lines t]
2765          ["Sort by characters" gnus-summary-sort-by-chars t]
2766          ["Randomize" gnus-summary-sort-by-random t]
2767          ["Original sort" gnus-summary-sort-by-original t])
2768         ("Help"
2769          ["Describe group" gnus-summary-describe-group t]
2770          ["Read manual" gnus-info-find-node t])
2771         ("Modes"
2772          ["Pick and read" gnus-pick-mode t]
2773          ["Binary" gnus-binary-mode t])
2774         ("Regeneration"
2775          ["Regenerate" gnus-summary-prepare t]
2776          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2777          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2778          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2779          ["Toggle threading" gnus-summary-toggle-threads t])
2780         ["See old articles" gnus-summary-insert-old-articles t]
2781         ["See new articles" gnus-summary-insert-new-articles t]
2782         ["Filter articles..." gnus-summary-execute-command t]
2783         ["Run command on articles..." gnus-summary-universal-argument t]
2784         ["Search articles forward..." gnus-summary-search-article-forward t]
2785         ["Search articles backward..." gnus-summary-search-article-backward t]
2786         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2787         ["Expand window" gnus-summary-expand-window t]
2788         ["Expire expirable articles" gnus-summary-expire-articles
2789          (gnus-check-backend-function
2790           'request-expire-articles gnus-newsgroup-name)]
2791         ["Edit local kill file" gnus-summary-edit-local-kill t]
2792         ["Edit main kill file" gnus-summary-edit-global-kill t]
2793         ["Edit group parameters" gnus-summary-edit-parameters t]
2794         ["Customize group parameters" gnus-summary-customize-parameters t]
2795         ["Send a bug report" gnus-bug t]
2796         ("Exit"
2797          ["Catchup and exit" gnus-summary-catchup-and-exit
2798           ,@(if (featurep 'xemacs) '(t)
2799               '(:help "Mark unread articles in this group as read, then exit"))]
2800          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2801          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2802          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2803          ["Exit group" gnus-summary-exit
2804           ,@(if (featurep 'xemacs) '(t)
2805               '(:help "Exit current group, return to group selection mode"))]
2806          ["Exit group without updating" gnus-summary-exit-no-update t]
2807          ["Exit and goto next group" gnus-summary-next-group t]
2808          ["Exit and goto prev group" gnus-summary-prev-group t]
2809          ["Reselect group" gnus-summary-reselect-current-group t]
2810          ["Rescan group" gnus-summary-rescan-group t]
2811          ["Update dribble" gnus-summary-save-newsrc t])))
2812
2813     (gnus-run-hooks 'gnus-summary-menu-hook)))
2814
2815 (defvar gnus-summary-tool-bar-map nil)
2816
2817 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2818 ;; affect _new_ message buffers.  We might add a function that walks thru all
2819 ;; summary-mode buffers and force the update.
2820 (defun gnus-summary-tool-bar-update (&optional symbol value)
2821   "Update summary mode toolbar.
2822 Setter function for custom variables."
2823   (setq-default gnus-summary-tool-bar-map nil)
2824   (when symbol
2825     ;; When used as ":set" function:
2826     (set-default symbol value))
2827   (when (gnus-buffer-live-p gnus-summary-buffer)
2828     (with-current-buffer gnus-summary-buffer
2829       (gnus-summary-make-tool-bar))))
2830
2831 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2832                                      'gnus-summary-tool-bar-gnome
2833                                    'gnus-summary-tool-bar-retro)
2834   "Specifies the Gnus summary tool bar.
2835
2836 It can be either a list or a symbol referring to a list.  See
2837 `gmm-tool-bar-from-list' for the format of the list.  The
2838 default key map is `gnus-summary-mode-map'.
2839
2840 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2841 `gnus-summary-tool-bar-retro'."
2842   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2843                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2844                  (repeat :tag "User defined list" gmm-tool-bar-item)
2845                  (symbol))
2846   :version "23.1" ;; No Gnus
2847   :initialize 'custom-initialize-default
2848   :set 'gnus-summary-tool-bar-update
2849   :group 'gnus-summary)
2850
2851 (defcustom gnus-summary-tool-bar-gnome
2852   '((gnus-summary-post-news "mail/compose" nil)
2853     (gnus-summary-insert-new-articles "mail/inbox" nil
2854                                       :visible (or (not gnus-agent)
2855                                                    gnus-plugged))
2856     (gnus-summary-reply-with-original "mail/reply")
2857     (gnus-summary-reply "mail/reply" nil :visible nil)
2858     (gnus-summary-followup-with-original "mail/reply-all")
2859     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2860     (gnus-summary-mail-forward "mail/forward")
2861     (gnus-summary-save-article "mail/save")
2862     (gnus-summary-search-article-forward "search" nil :visible nil)
2863     (gnus-summary-print-article "print")
2864     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2865     ;; Some new commands that may need more suitable icons:
2866     (gnus-summary-save-newsrc "save" nil :visible nil)
2867     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2868     (gnus-summary-prev-article "left-arrow")
2869     (gnus-summary-next-article "right-arrow")
2870     (gnus-summary-next-page "next-page")
2871     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2872     ;;
2873     ;; Maybe some sort-by-... could be added:
2874     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2875     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2876     (gnus-summary-mark-as-expirable
2877      "delete" nil
2878      :visible (gnus-check-backend-function 'request-expire-articles
2879                                            gnus-newsgroup-name))
2880     (gnus-summary-mark-as-spam
2881      "mail/spam" t
2882      :visible (and (fboundp 'spam-group-ham-contents-p)
2883                    (spam-group-ham-contents-p gnus-newsgroup-name))
2884      :help "Mark as spam")
2885     (gnus-summary-mark-as-read-forward
2886      "mail/not-spam" nil
2887      :visible (and (fboundp 'spam-group-spam-contents-p)
2888                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2889     ;;
2890     (gnus-summary-exit "exit")
2891     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2892     (gnus-info-find-node "help"))
2893   "List of functions for the summary tool bar (GNOME style).
2894
2895 See `gmm-tool-bar-from-list' for the format of the list."
2896   :type '(repeat gmm-tool-bar-item)
2897   :version "23.1" ;; No Gnus
2898   :initialize 'custom-initialize-default
2899   :set 'gnus-summary-tool-bar-update
2900   :group 'gnus-summary)
2901
2902 (defcustom gnus-summary-tool-bar-retro
2903   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2904     (gnus-summary-next-unread-article "gnus/next-ur")
2905     (gnus-summary-post-news "gnus/post")
2906     (gnus-summary-followup-with-original "gnus/fuwo")
2907     (gnus-summary-followup "gnus/followup")
2908     (gnus-summary-reply-with-original "gnus/reply-wo")
2909     (gnus-summary-reply "gnus/reply")
2910     (gnus-summary-caesar-message "gnus/rot13")
2911     (gnus-uu-decode-uu "gnus/uu-decode")
2912     (gnus-summary-save-article-file "gnus/save-aif")
2913     (gnus-summary-save-article "gnus/save-art")
2914     (gnus-uu-post-news "gnus/uu-post")
2915     (gnus-summary-catchup "gnus/catchup")
2916     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2917     (gnus-summary-exit "gnus/exit-summ")
2918     ;; Some new command that may need more suitable icons:
2919     (gnus-summary-print-article "gnus/print" nil :visible nil)
2920     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2921     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2922     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2923     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2924     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2925     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2926     ;;
2927     (gnus-info-find-node "gnus/help" nil :visible nil))
2928   "List of functions for the summary tool bar (retro look).
2929
2930 See `gmm-tool-bar-from-list' for the format of the list."
2931   :type '(repeat gmm-tool-bar-item)
2932   :version "23.1" ;; No Gnus
2933   :initialize 'custom-initialize-default
2934   :set 'gnus-summary-tool-bar-update
2935   :group 'gnus-summary)
2936
2937 (defcustom gnus-summary-tool-bar-zap-list t
2938   "List of icon items from the global tool bar.
2939 These items are not displayed in the Gnus summary mode tool bar.
2940
2941 See `gmm-tool-bar-from-list' for the format of the list."
2942   :type 'gmm-tool-bar-zap-list
2943   :version "23.1" ;; No Gnus
2944   :initialize 'custom-initialize-default
2945   :set 'gnus-summary-tool-bar-update
2946   :group 'gnus-summary)
2947
2948 (defvar image-load-path)
2949 (defvar tool-bar-map)
2950
2951 (defun gnus-summary-make-tool-bar (&optional force)
2952   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2953 When FORCE, rebuild the tool bar."
2954   (when (and (not (featurep 'xemacs))
2955              (boundp 'tool-bar-mode)
2956              tool-bar-mode
2957              (or (not gnus-summary-tool-bar-map) force))
2958     (let* ((load-path
2959             (gmm-image-load-path-for-library "gnus"
2960                                              "mail/save.xpm"
2961                                              nil t))
2962            (image-load-path (cons (car load-path)
2963                                   (when (boundp 'image-load-path)
2964                                     image-load-path)))
2965            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2966                                         gnus-summary-tool-bar-zap-list
2967                                         'gnus-summary-mode-map)))
2968       (when map
2969         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2970         ;; uses its value.
2971         (setq gnus-summary-tool-bar-map map))))
2972   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2973
2974 (defun gnus-score-set-default (var value)
2975   "A version of set that updates the GNU Emacs menu-bar."
2976   (set var value)
2977   ;; It is the message that forces the active status to be updated.
2978   (message ""))
2979
2980 (defun gnus-make-score-map (type)
2981   "Make a summary score map of type TYPE."
2982   (if t
2983       nil
2984     (let ((headers '(("author" "from" string)
2985                      ("subject" "subject" string)
2986                      ("article body" "body" string)
2987                      ("article head" "head" string)
2988                      ("xref" "xref" string)
2989                      ("extra header" "extra" string)
2990                      ("lines" "lines" number)
2991                      ("followups to author" "followup" string)))
2992           (types '((number ("less than" <)
2993                            ("greater than" >)
2994                            ("equal" =))
2995                    (string ("substring" s)
2996                            ("exact string" e)
2997                            ("fuzzy string" f)
2998                            ("regexp" r))))
2999           (perms '(("temporary" (current-time-string))
3000                    ("permanent" nil)
3001                    ("immediate" now)))
3002           header)
3003       (list
3004        (apply
3005         'nconc
3006         (list
3007          (if (eq type 'lower)
3008              "Lower score"
3009            "Increase score"))
3010         (let (outh)
3011           (while headers
3012             (setq header (car headers))
3013             (setq outh
3014                   (cons
3015                    (apply
3016                     'nconc
3017                     (list (car header))
3018                     (let ((ts (cdr (assoc (nth 2 header) types)))
3019                           outt)
3020                       (while ts
3021                         (setq outt
3022                               (cons
3023                                (apply
3024                                 'nconc
3025                                 (list (caar ts))
3026                                 (let ((ps perms)
3027                                       outp)
3028                                   (while ps
3029                                     (setq outp
3030                                           (cons
3031                                            (vector
3032                                             (caar ps)
3033                                             (list
3034                                              'gnus-summary-score-entry
3035                                              (nth 1 header)
3036                                              (if (or (string= (nth 1 header)
3037                                                               "head")
3038                                                      (string= (nth 1 header)
3039                                                               "body"))
3040                                                  ""
3041                                                (list 'gnus-summary-header
3042                                                      (nth 1 header)))
3043                                              (list 'quote (nth 1 (car ts)))
3044                                              (list 'gnus-score-delta-default
3045                                                    nil)
3046                                              (nth 1 (car ps))
3047                                              t)
3048                                             t)
3049                                            outp))
3050                                     (setq ps (cdr ps)))
3051                                   (list (nreverse outp))))
3052                                outt))
3053                         (setq ts (cdr ts)))
3054                       (list (nreverse outt))))
3055                    outh))
3056             (setq headers (cdr headers)))
3057           (list (nreverse outh))))))))
3058
3059
3060 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3061 (defvar bookmark-make-record-function)
3062 \f
3063 (defvar bidi-paragraph-direction)
3064
3065 (defun gnus-summary-mode (&optional group)
3066   "Major mode for reading articles.
3067
3068 All normal editing commands are switched off.
3069 \\<gnus-summary-mode-map>
3070 Each line in this buffer represents one article.  To read an
3071 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3072 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3073 respectively.
3074
3075 You can also post articles and send mail from this buffer.  To
3076 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3077 of an article, type `\\[gnus-summary-reply]'.
3078
3079 There are approx. one gazillion commands you can execute in this
3080 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3081
3082 The following commands are available:
3083
3084 \\{gnus-summary-mode-map}"
3085   (interactive)
3086   (kill-all-local-variables)
3087   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3088     (gnus-summary-make-local-variables))
3089   (gnus-summary-make-local-variables)
3090   (setq gnus-newsgroup-name group)
3091   (when (gnus-visual-p 'summary-menu 'menu)
3092     (gnus-summary-make-menu-bar)
3093     (gnus-summary-make-tool-bar))
3094   (gnus-make-thread-indent-array)
3095   (gnus-simplify-mode-line)
3096   (setq major-mode 'gnus-summary-mode)
3097   (setq mode-name "Summary")
3098   (use-local-map gnus-summary-mode-map)
3099   (buffer-disable-undo)
3100   (setq buffer-read-only t              ;Disable modification
3101         show-trailing-whitespace nil)
3102   (setq truncate-lines t)
3103   ;; Force paragraph direction to be left-to-right.  Don't make it
3104   ;; bound globally in old Emacsen and XEmacsen.
3105   (set (make-local-variable 'bidi-paragraph-direction) 'left-to-right)
3106   (add-to-invisibility-spec '(gnus-sum . t))
3107   (gnus-summary-set-display-table)
3108   (gnus-set-default-directory)
3109   (make-local-variable 'gnus-summary-line-format)
3110   (make-local-variable 'gnus-summary-line-format-spec)
3111   (make-local-variable 'gnus-summary-dummy-line-format)
3112   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3113   (make-local-variable 'gnus-summary-mark-positions)
3114   (gnus-make-local-hook 'pre-command-hook)
3115   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3116   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3117   (turn-on-gnus-mailing-list-mode)
3118   (mm-enable-multibyte)
3119   (set (make-local-variable 'bookmark-make-record-function)
3120        'gnus-summary-bookmark-make-record)
3121   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3122   (gnus-update-summary-mark-positions))
3123
3124 (defun gnus-summary-make-local-variables ()
3125   "Make all the local summary buffer variables."
3126   (let (global)
3127     (dolist (local gnus-summary-local-variables)
3128       (if (consp local)
3129           (progn
3130             (if (eq (cdr local) 'global)
3131                 ;; Copy the global value of the variable.
3132                 (setq global (symbol-value (car local)))
3133               ;; Use the value from the list.
3134               (setq global (eval (cdr local))))
3135             (set (make-local-variable (car local)) global))
3136         ;; Simple nil-valued local variable.
3137         (set (make-local-variable local) nil)))))
3138
3139 ;; Summary data functions.
3140
3141 (defmacro gnus-data-number (data)
3142   `(car ,data))
3143
3144 (defmacro gnus-data-set-number (data number)
3145   `(setcar ,data ,number))
3146
3147 (defmacro gnus-data-mark (data)
3148   `(nth 1 ,data))
3149
3150 (defmacro gnus-data-set-mark (data mark)
3151   `(setcar (nthcdr 1 ,data) ,mark))
3152
3153 (defmacro gnus-data-pos (data)
3154   `(nth 2 ,data))
3155
3156 (defmacro gnus-data-set-pos (data pos)
3157   `(setcar (nthcdr 2 ,data) ,pos))
3158
3159 (defmacro gnus-data-header (data)
3160   `(nth 3 ,data))
3161
3162 (defmacro gnus-data-set-header (data header)
3163   `(setf (nth 3 ,data) ,header))
3164
3165 (defmacro gnus-data-level (data)
3166   `(nth 4 ,data))
3167
3168 (defmacro gnus-data-unread-p (data)
3169   `(= (nth 1 ,data) gnus-unread-mark))
3170
3171 (defmacro gnus-data-read-p (data)
3172   `(/= (nth 1 ,data) gnus-unread-mark))
3173
3174 (defmacro gnus-data-pseudo-p (data)
3175   `(consp (nth 3 ,data)))
3176
3177 (defmacro gnus-data-find (number)
3178   `(assq ,number gnus-newsgroup-data))
3179
3180 (defmacro gnus-data-find-list (number &optional data)
3181   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3182      (memq (assq ,number bdata)
3183            bdata)))
3184
3185 (defmacro gnus-data-make (number mark pos header level)
3186   `(list ,number ,mark ,pos ,header ,level))
3187
3188 (defun gnus-data-enter (after-article number mark pos header level offset)
3189   (let ((data (gnus-data-find-list after-article)))
3190     (unless data
3191       (error "No such article: %d" after-article))
3192     (setcdr data (cons (gnus-data-make number mark pos header level)
3193                        (cdr data)))
3194     (setq gnus-newsgroup-data-reverse nil)
3195     (gnus-data-update-list (cddr data) offset)))
3196
3197 (defun gnus-data-enter-list (after-article list &optional offset)
3198   (when list
3199     (let ((data (and after-article (gnus-data-find-list after-article)))
3200           (ilist list))
3201       (if (not (or data
3202                    after-article))
3203           (let ((odata gnus-newsgroup-data))
3204             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3205             (when offset
3206               (gnus-data-update-list odata offset)))
3207         ;; Find the last element in the list to be spliced into the main
3208         ;; list.
3209         (setq list (last list))
3210         (if (not data)
3211             (progn
3212               (setcdr list gnus-newsgroup-data)
3213               (setq gnus-newsgroup-data ilist)
3214               (when offset
3215                 (gnus-data-update-list (cdr list) offset)))
3216           (setcdr list (cdr data))
3217           (setcdr data ilist)
3218           (when offset
3219             (gnus-data-update-list (cdr list) offset))))
3220       (setq gnus-newsgroup-data-reverse nil))))
3221
3222 (defun gnus-data-remove (article &optional offset)
3223   (let ((data gnus-newsgroup-data))
3224     (if (= (gnus-data-number (car data)) article)
3225         (progn
3226           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3227                 gnus-newsgroup-data-reverse nil)
3228           (when offset
3229             (gnus-data-update-list gnus-newsgroup-data offset)))
3230       (while (cdr data)
3231         (when (= (gnus-data-number (cadr data)) article)
3232           (setcdr data (cddr data))
3233           (when offset
3234             (gnus-data-update-list (cdr data) offset))
3235           (setq data nil
3236                 gnus-newsgroup-data-reverse nil))
3237         (setq data (cdr data))))))
3238
3239 (defmacro gnus-data-list (backward)
3240   `(if ,backward
3241        (or gnus-newsgroup-data-reverse
3242            (setq gnus-newsgroup-data-reverse
3243                  (reverse gnus-newsgroup-data)))
3244      gnus-newsgroup-data))
3245
3246 (defun gnus-data-update-list (data offset)
3247   "Add OFFSET to the POS of all data entries in DATA."
3248   (setq gnus-newsgroup-data-reverse nil)
3249   (while data
3250     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3251     (setq data (cdr data))))
3252
3253 (defun gnus-summary-article-pseudo-p (article)
3254   "Say whether this article is a pseudo article or not."
3255   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3256
3257 (defmacro gnus-summary-article-sparse-p (article)
3258   "Say whether this article is a sparse article or not."
3259   `(memq ,article gnus-newsgroup-sparse))
3260
3261 (defmacro gnus-summary-article-ancient-p (article)
3262   "Say whether this article is a sparse article or not."
3263   `(memq ,article gnus-newsgroup-ancient))
3264
3265 (defun gnus-article-parent-p (number)
3266   "Say whether this article is a parent or not."
3267   (let ((data (gnus-data-find-list number)))
3268     (and (cdr data)              ; There has to be an article after...
3269          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3270             (gnus-data-level (nth 1 data))))))
3271
3272 (defun gnus-article-children (number)
3273   "Return a list of all children to NUMBER."
3274   (let* ((data (gnus-data-find-list number))
3275          (level (gnus-data-level (car data)))
3276          children)
3277     (setq data (cdr data))
3278     (while (and data
3279                 (= (gnus-data-level (car data)) (1+ level)))
3280       (push (gnus-data-number (car data)) children)
3281       (setq data (cdr data)))
3282     children))
3283
3284 (defmacro gnus-summary-skip-intangible ()
3285   "If the current article is intangible, then jump to a different article."
3286   '(let ((to (get-text-property (point) 'gnus-intangible)))
3287      (and to (gnus-summary-goto-subject to))))
3288
3289 (defmacro gnus-summary-article-intangible-p ()
3290   "Say whether this article is intangible or not."
3291   '(get-text-property (point) 'gnus-intangible))
3292
3293 (defun gnus-article-read-p (article)
3294   "Say whether ARTICLE is read or not."
3295   (not (or (memq article gnus-newsgroup-marked)
3296            (memq article gnus-newsgroup-spam-marked)
3297            (memq article gnus-newsgroup-unreads)
3298            (memq article gnus-newsgroup-unselected)
3299            (memq article gnus-newsgroup-dormant))))
3300
3301 ;; Some summary mode macros.
3302
3303 (defmacro gnus-summary-article-number ()
3304   "The article number of the article on the current line.
3305 If there isn't an article number here, then we return the current
3306 article number."
3307   '(progn
3308      (gnus-summary-skip-intangible)
3309      (or (get-text-property (point) 'gnus-number)
3310          (gnus-summary-last-subject))))
3311
3312 (defmacro gnus-summary-article-header (&optional number)
3313   "Return the header of article NUMBER."
3314   `(gnus-data-header (gnus-data-find
3315                       ,(or number '(gnus-summary-article-number)))))
3316
3317 (defmacro gnus-summary-thread-level (&optional number)
3318   "Return the level of thread that starts with article NUMBER."
3319   `(if (and (eq gnus-summary-make-false-root 'dummy)
3320             (get-text-property (point) 'gnus-intangible))
3321        0
3322      (gnus-data-level (gnus-data-find
3323                        ,(or number '(gnus-summary-article-number))))))
3324
3325 (defmacro gnus-summary-article-mark (&optional number)
3326   "Return the mark of article NUMBER."
3327   `(gnus-data-mark (gnus-data-find
3328                     ,(or number '(gnus-summary-article-number)))))
3329
3330 (defmacro gnus-summary-article-pos (&optional number)
3331   "Return the position of the line of article NUMBER."
3332   `(gnus-data-pos (gnus-data-find
3333                    ,(or number '(gnus-summary-article-number)))))
3334
3335 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3336 (defmacro gnus-summary-article-subject (&optional number)
3337   "Return current subject string or nil if nothing."
3338   `(let ((headers
3339           ,(if number
3340                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3341              '(gnus-data-header (assq (gnus-summary-article-number)
3342                                       gnus-newsgroup-data)))))
3343      (and headers
3344           (vectorp headers)
3345           (mail-header-subject headers))))
3346
3347 (defmacro gnus-summary-article-score (&optional number)
3348   "Return current article score."
3349   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3350                   gnus-newsgroup-scored))
3351        gnus-summary-default-score 0))
3352
3353 (defun gnus-summary-article-children (&optional number)
3354   "Return a list of article numbers that are children of article NUMBER."
3355   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3356          (level (gnus-data-level (car data)))
3357          l children)
3358     (while (and (setq data (cdr data))
3359                 (> (setq l (gnus-data-level (car data))) level))
3360       (and (= (1+ level) l)
3361            (push (gnus-data-number (car data))
3362                  children)))
3363     (nreverse children)))
3364
3365 (defun gnus-summary-article-parent (&optional number)
3366   "Return the article number of the parent of article NUMBER."
3367   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3368                                     (gnus-data-list t)))
3369          (level (gnus-data-level (car data))))
3370     (if (zerop level)
3371         ()                              ; This is a root.
3372       ;; We search until we find an article with a level less than
3373       ;; this one.  That function has to be the parent.
3374       (while (and (setq data (cdr data))
3375                   (not (< (gnus-data-level (car data)) level))))
3376       (and data (gnus-data-number (car data))))))
3377
3378 (defun gnus-unread-mark-p (mark)
3379   "Say whether MARK is the unread mark."
3380   (= mark gnus-unread-mark))
3381
3382 (defun gnus-read-mark-p (mark)
3383   "Say whether MARK is one of the marks that mark as read.
3384 This is all marks except unread, ticked, dormant, and expirable."
3385   (not (or (= mark gnus-unread-mark)
3386            (= mark gnus-ticked-mark)
3387            (= mark gnus-spam-mark)
3388            (= mark gnus-dormant-mark)
3389            (= mark gnus-expirable-mark))))
3390
3391 (defmacro gnus-article-mark (number)
3392   "Return the MARK of article NUMBER.
3393 This macro should only be used when computing the mark the \"first\"
3394 time; i.e., when generating the summary lines.  After that,
3395 `gnus-summary-article-mark' should be used to examine the
3396 marks of articles."
3397   `(cond
3398     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3399     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3400     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3401     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3402     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3403     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3404     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3405     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3406            gnus-ancient-mark))))
3407
3408 ;; Saving hidden threads.
3409
3410 (defmacro gnus-save-hidden-threads (&rest forms)
3411   "Save hidden threads, eval FORMS, and restore the hidden threads."
3412   (let ((config (make-symbol "config")))
3413     `(let ((,config (gnus-hidden-threads-configuration)))
3414        (unwind-protect
3415            (save-excursion
3416              ,@forms)
3417          (gnus-restore-hidden-threads-configuration ,config)))))
3418 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3419 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3420
3421 (defun gnus-data-compute-positions ()
3422   "Compute the positions of all articles."
3423   (setq gnus-newsgroup-data-reverse nil)
3424   (let ((data gnus-newsgroup-data))
3425     (save-excursion
3426       (gnus-save-hidden-threads
3427         (gnus-summary-show-all-threads)
3428         (goto-char (point-min))
3429         (while data
3430           (while (get-text-property (point) 'gnus-intangible)
3431             (forward-line 1))
3432           (gnus-data-set-pos (car data) (+ (point) 3))
3433           (setq data (cdr data))
3434           (forward-line 1))))))
3435
3436 (defun gnus-hidden-threads-configuration ()
3437   "Return the current hidden threads configuration."
3438   (save-excursion
3439     (let (config)
3440       (goto-char (point-min))
3441       (while (not (eobp))
3442         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3443           (push (save-excursion (forward-line 0) (point)) config))
3444         (forward-line 1))
3445       config)))
3446
3447 (defun gnus-restore-hidden-threads-configuration (config)
3448   "Restore hidden threads configuration from CONFIG."
3449   (save-excursion
3450     (let (point (inhibit-read-only t))
3451       (while (setq point (pop config))
3452         (goto-char point)
3453         (gnus-summary-hide-thread)))))
3454
3455 ;; Various summary mode internalish functions.
3456
3457 (defun gnus-mouse-pick-article (e)
3458   (interactive "e")
3459   (mouse-set-point e)
3460   (gnus-summary-next-page nil t))
3461
3462 (defun gnus-summary-set-display-table ()
3463   "Change the display table.
3464 Odd characters have a tendency to mess
3465 up nicely formatted displays - we make all possible glyphs
3466 display only a single character."
3467
3468   ;; We start from the standard display table, if any.
3469   (let ((table (or (copy-sequence standard-display-table)
3470                    (make-display-table)))
3471         (i 32))
3472     ;; Nix out all the control chars...
3473     (while (>= (setq i (1- i)) 0)
3474       (gnus-put-display-table i [??] table))
3475    ;; ... but not newline and cr, of course.  (cr is necessary for the
3476     ;; selective display).
3477     (gnus-put-display-table ?\n nil table)
3478     (gnus-put-display-table ?\r nil table)
3479     ;; We keep TAB as well.
3480     (gnus-put-display-table ?\t nil table)
3481     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3482     ;; Emacs 23 (unicode), that are not set already.
3483     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3484                  160
3485                256)))
3486       (while (>= (setq i (1- i)) 127)
3487         ;; Only modify if the entry is nil.
3488         (unless (gnus-get-display-table i table)
3489           (gnus-put-display-table i [??] table))))
3490     (setq buffer-display-table table)))
3491
3492 (defun gnus-summary-set-article-display-arrow (pos)
3493   "Update the overlay arrow to point to line at position POS."
3494   (when gnus-summary-display-arrow
3495     (make-local-variable 'overlay-arrow-position)
3496     (make-local-variable 'overlay-arrow-string)
3497     (save-excursion
3498       (goto-char pos)
3499       (beginning-of-line)
3500       (unless overlay-arrow-position
3501         (setq overlay-arrow-position (make-marker)))
3502       (setq overlay-arrow-string "=>"
3503             overlay-arrow-position (set-marker overlay-arrow-position
3504                                                (point)
3505                                                (current-buffer))))))
3506
3507 (defun gnus-summary-setup-buffer (group)
3508   "Initialize summary buffer.
3509 If the setup was successful, non-nil is returned."
3510   (let ((buffer (gnus-summary-buffer-name group))
3511         (dead-name (concat "*Dead Summary "
3512                            (gnus-group-decoded-name group) "*")))
3513     ;; If a dead summary buffer exists, we kill it.
3514     (when (gnus-buffer-live-p dead-name)
3515       (gnus-kill-buffer dead-name))
3516     (if (get-buffer buffer)
3517         (progn
3518           (set-buffer buffer)
3519           (setq gnus-summary-buffer (current-buffer))
3520           (not gnus-newsgroup-prepared))
3521       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3522       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3523       (gnus-summary-mode group)
3524       (when (gnus-group-quit-config group)
3525         (set (make-local-variable 'gnus-single-article-buffer) nil))
3526       (make-local-variable 'gnus-article-buffer)
3527       (make-local-variable 'gnus-article-current)
3528       (make-local-variable 'gnus-original-article-buffer)
3529       (setq gnus-newsgroup-name group)
3530       ;; Set any local variables in the group parameters.
3531       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3532       t)))
3533
3534 (defun gnus-set-global-variables ()
3535   "Set the global equivalents of the buffer-local variables.
3536 They are set to the latest values they had.  These reflect the summary
3537 buffer that was in action when the last article was fetched."
3538   (when (eq major-mode 'gnus-summary-mode)
3539     (setq gnus-summary-buffer (current-buffer))
3540     (let ((name gnus-newsgroup-name)
3541           (marked gnus-newsgroup-marked)
3542           (spam gnus-newsgroup-spam-marked)
3543           (unread gnus-newsgroup-unreads)
3544           (headers gnus-current-headers)
3545           (data gnus-newsgroup-data)
3546           (summary gnus-summary-buffer)
3547           (article-buffer gnus-article-buffer)
3548           (original gnus-original-article-buffer)
3549           (gac gnus-article-current)
3550           (reffed gnus-reffed-article-number)
3551           (score-file gnus-current-score-file)
3552           (default-charset gnus-newsgroup-charset)
3553           vlist)
3554       (let ((locals gnus-newsgroup-variables))
3555         (while locals
3556           (if (consp (car locals))
3557               (push (eval (caar locals)) vlist)
3558             (push (eval (car locals)) vlist))
3559           (setq locals (cdr locals)))
3560         (setq vlist (nreverse vlist)))
3561       (with-temp-buffer
3562         (setq gnus-newsgroup-name name
3563               gnus-newsgroup-marked marked
3564               gnus-newsgroup-spam-marked spam
3565               gnus-newsgroup-unreads unread
3566               gnus-current-headers headers
3567               gnus-newsgroup-data data
3568               gnus-article-current gac
3569               gnus-summary-buffer summary
3570               gnus-article-buffer article-buffer
3571               gnus-original-article-buffer original
3572               gnus-reffed-article-number reffed
3573               gnus-current-score-file score-file
3574               gnus-newsgroup-charset default-charset)
3575         (let ((locals gnus-newsgroup-variables))
3576           (while locals
3577             (if (consp (car locals))
3578                 (set (caar locals) (pop vlist))
3579               (set (car locals) (pop vlist)))
3580             (setq locals (cdr locals))))
3581         ;; The article buffer also has local variables.
3582         (when (gnus-buffer-live-p gnus-article-buffer)
3583           (set-buffer gnus-article-buffer)
3584           (setq gnus-summary-buffer summary))))))
3585
3586 (defun gnus-summary-article-unread-p (article)
3587   "Say whether ARTICLE is unread or not."
3588   (memq article gnus-newsgroup-unreads))
3589
3590 (defun gnus-summary-first-article-p (&optional article)
3591   "Return whether ARTICLE is the first article in the buffer."
3592   (if (not (setq article (or article (gnus-summary-article-number))))
3593       nil
3594     (eq article (caar gnus-newsgroup-data))))
3595
3596 (defun gnus-summary-last-article-p (&optional article)
3597   "Return whether ARTICLE is the last article in the buffer."
3598   (if (not (setq article (or article (gnus-summary-article-number))))
3599       ;; All non-existent numbers are the last article.  :-)
3600       t
3601     (not (cdr (gnus-data-find-list article)))))
3602
3603 (defun gnus-make-thread-indent-array (&optional n)
3604   (when (or n
3605             (progn (setq n 200) nil)
3606             (null gnus-thread-indent-array)
3607             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3608     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3609           gnus-thread-indent-array-level gnus-thread-indent-level)
3610     (while (>= n 0)
3611       (aset gnus-thread-indent-array n
3612             (make-string (* n gnus-thread-indent-level) ? ))
3613       (setq n (1- n)))))
3614
3615 (defun gnus-update-summary-mark-positions ()
3616   "Compute where the summary marks are to go."
3617   (save-excursion
3618     (when (gnus-buffer-exists-p gnus-summary-buffer)
3619       (set-buffer gnus-summary-buffer))
3620     (let ((spec gnus-summary-line-format-spec)
3621           pos)
3622       (save-excursion
3623         (gnus-set-work-buffer)
3624         (let ((gnus-tmp-unread ?Z)
3625               (gnus-replied-mark ?Z)
3626               (gnus-score-below-mark ?Z)
3627               (gnus-score-over-mark ?Z)
3628               (gnus-undownloaded-mark ?Z)
3629               (gnus-summary-line-format-spec spec)
3630               (gnus-newsgroup-downloadable '(0))
3631               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3632               case-fold-search ignores)
3633           ;; Here, all marks are bound to Z.
3634           (gnus-summary-insert-line header
3635                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3636           (goto-char (point-min))
3637           ;; Memorize the positions of the same characters as dummy marks.
3638           (while (re-search-forward "[A-D]" nil t)
3639             (push (point) ignores))
3640           (erase-buffer)
3641           ;; We use A-D as dummy marks in order to know column positions
3642           ;; where marks should be inserted.
3643           (setq gnus-tmp-unread ?A
3644                 gnus-replied-mark ?B
3645                 gnus-score-below-mark ?C
3646                 gnus-score-over-mark ?C
3647                 gnus-undownloaded-mark ?D)
3648           (gnus-summary-insert-line header
3649                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3650           ;; Ignore characters which aren't dummy marks.
3651           (dolist (p ignores)
3652             (delete-region (goto-char (1- p)) p)
3653             (insert ?Z))
3654           (goto-char (point-min))
3655           (setq pos (list (cons 'unread
3656                                 (and (search-forward "A" nil t)
3657                                      (- (point) (point-min) 1)))))
3658           (goto-char (point-min))
3659           (push (cons 'replied (and (search-forward "B" nil t)
3660                                     (- (point) (point-min) 1)))
3661                 pos)
3662           (goto-char (point-min))
3663           (push (cons 'score (and (search-forward "C" nil t)
3664                                   (- (point) (point-min) 1)))
3665                 pos)
3666           (goto-char (point-min))
3667           (push (cons 'download (and (search-forward "D" nil t)
3668                                      (- (point) (point-min) 1)))
3669                 pos)))
3670       (setq gnus-summary-mark-positions pos))))
3671
3672 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3673   "Insert a dummy root in the summary buffer."
3674   (beginning-of-line)
3675   (gnus-add-text-properties
3676    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3677    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3678
3679 (defun gnus-summary-extract-address-component (from)
3680   (or (car (funcall gnus-extract-address-components from))
3681       from))
3682
3683 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3684   (let ((mail-parse-charset gnus-newsgroup-charset)
3685         (ignored-from-addresses (gnus-ignored-from-addresses))
3686         ; Is it really necessary to do this next part for each summary line?
3687         ; Luckily, doesn't seem to slow things down much.
3688         (mail-parse-ignored-charsets
3689          (with-current-buffer gnus-summary-buffer
3690            gnus-newsgroup-ignored-charsets)))
3691     (or
3692      (and ignored-from-addresses
3693           (string-match ignored-from-addresses gnus-tmp-from)
3694           (let ((extra-headers (mail-header-extra header))
3695                 to
3696                 newsgroups)
3697             (cond
3698              ((setq to (cdr (assq 'To extra-headers)))
3699               (concat gnus-summary-to-prefix
3700                       (inline
3701                         (gnus-summary-extract-address-component
3702                          (funcall gnus-decode-encoded-address-function to)))))
3703              ((setq newsgroups
3704                     (or
3705                      (cdr (assq 'Newsgroups extra-headers))
3706                      (and
3707                       (memq 'Newsgroups gnus-extra-headers)
3708                       (eq (car (gnus-find-method-for-group
3709                                 gnus-newsgroup-name)) 'nntp)
3710                       (gnus-group-real-name gnus-newsgroup-name))))
3711               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3712      (gnus-string-mark-left-to-right
3713       (inline
3714        (gnus-summary-extract-address-component gnus-tmp-from))))))
3715
3716 (defun gnus-summary-insert-line (gnus-tmp-header
3717                                  gnus-tmp-level gnus-tmp-current
3718                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3719                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3720                                  &optional gnus-tmp-dummy gnus-tmp-score
3721                                  gnus-tmp-process)
3722   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3723       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3724                                           gnus-tmp-level)))
3725   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3726          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3727          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3728          (gnus-tmp-score-char
3729           (if (or (null gnus-summary-default-score)
3730                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3731                       gnus-summary-zcore-fuzz))
3732               ?                         ;Whitespace
3733             (if (< gnus-tmp-score gnus-summary-default-score)
3734                 gnus-score-below-mark gnus-score-over-mark)))
3735          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3736          (gnus-tmp-replied
3737           (cond (gnus-tmp-process gnus-process-mark)
3738                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3739                  gnus-cached-mark)
3740                 (gnus-tmp-replied gnus-replied-mark)
3741                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3742                  gnus-forwarded-mark)
3743                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3744                  gnus-saved-mark)
3745                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3746                  gnus-unseen-mark)
3747                 (t gnus-no-mark)))
3748          (gnus-tmp-downloaded
3749           (cond (undownloaded
3750                  gnus-undownloaded-mark)
3751                 (gnus-newsgroup-agentized
3752                  gnus-downloaded-mark)
3753                 (t
3754                  gnus-no-mark)))
3755          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3756          (gnus-tmp-name
3757           (cond
3758            ((string-match "<[^>]+> *$" gnus-tmp-from)
3759             (let ((beg (match-beginning 0)))
3760               (or (and (string-match "^\".+\"" gnus-tmp-from)
3761                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3762                   (substring gnus-tmp-from 0 beg))))
3763            ((string-match "(.+)" gnus-tmp-from)
3764             (substring gnus-tmp-from
3765                        (1+ (match-beginning 0)) (1- (match-end 0))))
3766            (t gnus-tmp-from)))
3767          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3768          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3769          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3770          (inhibit-read-only t))
3771     (when (string= gnus-tmp-name "")
3772       (setq gnus-tmp-name gnus-tmp-from))
3773     (unless (numberp gnus-tmp-lines)
3774       (setq gnus-tmp-lines -1))
3775     (if (= gnus-tmp-lines -1)
3776         (setq gnus-tmp-lines "?")
3777       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3778     (condition-case ()
3779         (gnus-put-text-property
3780          (point)
3781          (progn (eval gnus-summary-line-format-spec) (point))
3782          'gnus-number gnus-tmp-number)
3783       (error (gnus-message 5 "Error updating the summary line")))
3784     (when (gnus-visual-p 'summary-highlight 'highlight)
3785       (forward-line -1)
3786       (gnus-summary-highlight-line)
3787       (gnus-run-hooks 'gnus-summary-update-hook)
3788       (forward-line 1))))
3789
3790 (defun gnus-summary-update-line (&optional dont-update)
3791   "Update summary line after change."
3792   (when (and gnus-summary-default-score
3793              (not gnus-summary-inhibit-highlight))
3794     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3795            (article (gnus-summary-article-number))
3796            (score (gnus-summary-article-score article)))
3797       (unless dont-update
3798         (if (and gnus-summary-mark-below
3799                  (< (gnus-summary-article-score)
3800                     gnus-summary-mark-below))
3801             ;; This article has a low score, so we mark it as read.
3802             (when (memq article gnus-newsgroup-unreads)
3803               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3804           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3805             ;; This article was previously marked as read on account
3806             ;; of a low score, but now it has risen, so we mark it as
3807             ;; unread.
3808             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3809         (gnus-summary-update-mark
3810          (if (or (null gnus-summary-default-score)
3811                  (<= (abs (- score gnus-summary-default-score))
3812                      gnus-summary-zcore-fuzz))
3813              ?                          ;Whitespace
3814            (if (< score gnus-summary-default-score)
3815                gnus-score-below-mark gnus-score-over-mark))
3816          'score))
3817       ;; Do visual highlighting.
3818       (when (gnus-visual-p 'summary-highlight 'highlight)
3819         (gnus-summary-highlight-line)
3820         (gnus-run-hooks 'gnus-summary-update-hook)))))
3821
3822 (defvar gnus-tmp-new-adopts nil)
3823
3824 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3825   "Return the number of articles in THREAD.
3826 This may be 0 in some cases -- if none of the articles in
3827 the thread are to be displayed."
3828   (let* ((number
3829          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3830           (cond
3831            ((not (listp thread))
3832             1)
3833            ((and (consp thread) (cdr thread))
3834             (apply
3835              '+ 1 (mapcar
3836                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3837            ((null thread)
3838             1)
3839            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3840             1)
3841            (t 0))))
3842     (when (and level (zerop level) gnus-tmp-new-adopts)
3843       (incf number
3844             (apply '+ (mapcar
3845                        'gnus-summary-number-of-articles-in-thread
3846                        gnus-tmp-new-adopts))))
3847     (if char
3848         (if (> number 1) gnus-not-empty-thread-mark
3849           gnus-empty-thread-mark)
3850       number)))
3851
3852 (defsubst gnus-summary-line-message-size (head)
3853   "Return pretty-printed version of message size.
3854 This function is intended to be used in
3855 `gnus-summary-line-format-alist'."
3856   (let ((c (or (mail-header-chars head) -1)))
3857     (cond ((< c 0) "n/a")               ; chars not available
3858           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3859           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3860           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3861           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3862
3863 (defcustom gnus-user-date-format-alist
3864   '(((gnus-seconds-today) . "Today, %H:%M")
3865     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3866     (604800 . "%A %H:%M")               ; That's one week
3867     ((gnus-seconds-month) . "%A %d")
3868     ((gnus-seconds-year) . "%B %d")
3869     (t . "%b %d %Y"))                   ; This one is used when no other
3870                                         ; does match
3871   "Specifies date format depending on age of article.
3872 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3873 seconds) or a Lisp expression evaluating to a number.  When the age of
3874 the article is less than this number, then use `format-time-string'
3875 with the corresponding FORMAT for displaying the date of the article.
3876 If AGE is not a number or a Lisp expression evaluating to a
3877 non-number, then the corresponding FORMAT is used as a default value.
3878
3879 Note that the list is processed from the beginning, so it should be
3880 sorted by ascending AGE.  Also note that items following the first
3881 non-number AGE will be ignored.
3882
3883 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3884 and `gnus-seconds-year' in the AGE spec.  They return the number of
3885 seconds passed since the start of today, of this month, of this year,
3886 respectively."
3887   :version "24.1"
3888   :group 'gnus-summary-format
3889   :type '(alist :key-type sexp :value-type string))
3890
3891 (defun gnus-user-date (messy-date)
3892   "Format the messy-date according to `gnus-user-date-format-alist'.
3893 Returns \"  ?  \" if there's bad input or if another error occurs.
3894 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3895   (condition-case ()
3896       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3897              (now (gnus-float-time))
3898              ;;If we don't find something suitable we'll use this one
3899              (my-format "%b %d '%y"))
3900         (let* ((difference (- now messy-date))
3901                (templist gnus-user-date-format-alist)
3902                (top (eval (caar templist))))
3903           (while (if (numberp top) (< top difference) (not top))
3904             (progn
3905               (setq templist (cdr templist))
3906               (setq top (eval (caar templist)))))
3907           (if (stringp (cdr (car templist)))
3908               (setq my-format (cdr (car templist)))))
3909         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3910     (error "  ?   ")))
3911
3912 (defun gnus-summary-set-local-parameters (group)
3913   "Go through the local params of GROUP and set all variable specs in that list."
3914   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3915                                         ; really variables.
3916     (dolist (elem (gnus-group-find-parameter group))
3917       (and (consp elem)                 ; Has to be a cons.
3918            (consp (cdr elem))           ; The cdr has to be a list.
3919            (symbolp (car elem))         ; Has to be a symbol in there.
3920            (not (memq (car elem) vars))
3921            (ignore-errors
3922              (push (car elem) vars)
3923              ;; Variables like `gnus-show-threads' that are globally
3924              ;; bound, if used as group parameters, need to get to be
3925              ;; buffer-local, whereas just parameters like `gcc-self',
3926              ;; `timestamp', etc. should not be bound as variables.
3927              (if (boundp (car elem))
3928                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3929                (eval (nth 1 elem))))))))
3930
3931 (defun gnus-summary-read-group (group &optional show-all no-article
3932                                       kill-buffer no-display backward
3933                                       select-articles)
3934   "Start reading news in newsgroup GROUP.
3935 If SHOW-ALL is non-nil, already read articles are also listed.
3936 If NO-ARTICLE is non-nil, no article is selected initially.
3937 If NO-DISPLAY, don't generate the summary buffer contents.
3938 If KILL-BUFFER, it should be a buffer that's killed once the new
3939 summary buffer has been generated.
3940 If BACKWARD, move point to the previous group in the group buffer
3941 If SELECT-ARTICLES, only select those articles from GROUP."
3942   (let (result)
3943     (while (and group
3944                 (null (setq result
3945                             (let ((gnus-auto-select-next nil))
3946                               (or (gnus-summary-read-group-1
3947                                    group show-all no-article
3948                                    kill-buffer no-display
3949                                    select-articles)
3950                                   (setq show-all nil
3951                                         select-articles nil)))))
3952                 (eq gnus-auto-select-next 'quietly))
3953       (set-buffer gnus-group-buffer)
3954       ;; The entry function called above goes to the next
3955       ;; group automatically, so we go two groups back
3956       ;; if we are searching for the previous group.
3957       (when backward
3958         (gnus-group-prev-unread-group 2))
3959       (if (not (equal group (gnus-group-group-name)))
3960           (setq group (gnus-group-group-name))
3961         (setq group nil)))
3962     result))
3963
3964 (defun gnus-summary-read-group-1 (group show-all no-article
3965                                         kill-buffer no-display
3966                                         &optional select-articles)
3967   ;; Killed foreign groups can't be entered.
3968   ;;  (when (and (not (gnus-group-native-p group))
3969   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3970   ;;    (error "Dead non-native groups can't be entered"))
3971   (gnus-message 7 "Retrieving newsgroup: %s..."
3972                 (gnus-group-decoded-name group))
3973   (let* ((new-group (gnus-summary-setup-buffer group))
3974          (quit-config (gnus-group-quit-config group))
3975          (did-select (and new-group (gnus-select-newsgroup
3976                                      group show-all select-articles))))
3977     (cond
3978      ;; This summary buffer exists already, so we just select it.
3979      ((not new-group)
3980       (gnus-set-global-variables)
3981       (when kill-buffer
3982         (gnus-kill-or-deaden-summary kill-buffer))
3983       (gnus-configure-windows 'summary 'force)
3984       (gnus-set-mode-line 'summary)
3985       (gnus-summary-position-point)
3986       (message "")
3987       t)
3988      ;; We couldn't select this group.
3989      ((null did-select)
3990       (when (and (eq major-mode 'gnus-summary-mode)
3991                  (not (equal (current-buffer) kill-buffer)))
3992         (kill-buffer (current-buffer))
3993         (if (not quit-config)
3994             (progn
3995               ;; Update the info -- marks might need to be removed,
3996               ;; for instance.
3997               (gnus-summary-update-info)
3998               (set-buffer gnus-group-buffer)
3999               (gnus-group-jump-to-group group)
4000               (gnus-group-next-unread-group 1))
4001           (gnus-handle-ephemeral-exit quit-config)))
4002       (if (null (gnus-list-of-unread-articles group))
4003           (gnus-message 3 "Group %s contains no messages" group)
4004         (gnus-message 3 "Can't select group"))
4005       nil)
4006      ;; The user did a `C-g' while prompting for number of articles,
4007      ;; so we exit this group.
4008      ((eq did-select 'quit)
4009       (and (eq major-mode 'gnus-summary-mode)
4010            (not (equal (current-buffer) kill-buffer))
4011            (kill-buffer (current-buffer)))
4012       (when kill-buffer
4013         (gnus-kill-or-deaden-summary kill-buffer))
4014       (if (not quit-config)
4015           (progn
4016             (set-buffer gnus-group-buffer)
4017             (gnus-group-jump-to-group group)
4018             (gnus-configure-windows 'group 'force))
4019         (gnus-handle-ephemeral-exit quit-config))
4020       ;; Finally signal the quit.
4021       (signal 'quit nil))
4022      ;; The group was successfully selected.
4023      (t
4024       (gnus-set-global-variables)
4025       ;; Save the active value in effect when the group was entered.
4026       (setq gnus-newsgroup-active
4027             (gnus-copy-sequence
4028              (gnus-active gnus-newsgroup-name)))
4029       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4030       ;; You can change the summary buffer in some way with this hook.
4031       (gnus-run-hooks 'gnus-select-group-hook)
4032       (when (memq 'summary (gnus-update-format-specifications
4033                             nil 'summary 'summary-mode 'summary-dummy))
4034         ;; The format specification for the summary line was updated,
4035         ;; so we need to update the mark positions as well.
4036         (gnus-update-summary-mark-positions))
4037       ;; Do score processing.
4038       (when gnus-use-scoring
4039         (gnus-possibly-score-headers))
4040       ;; Check whether to fill in the gaps in the threads.
4041       (when gnus-build-sparse-threads
4042         (gnus-build-sparse-threads))
4043       ;; Find the initial limit.
4044       (if show-all
4045           (let ((gnus-newsgroup-dormant nil))
4046             (gnus-summary-initial-limit show-all))
4047         (gnus-summary-initial-limit show-all))
4048       ;; Generate the summary buffer.
4049       (unless no-display
4050         (gnus-summary-prepare))
4051       (when gnus-use-trees
4052         (gnus-tree-open group)
4053         (setq gnus-summary-highlight-line-function
4054               'gnus-tree-highlight-article))
4055       ;; If the summary buffer is empty, but there are some low-scored
4056       ;; articles or some excluded dormants, we include these in the
4057       ;; buffer.
4058       (when (and (zerop (buffer-size))
4059                  (not no-display))
4060         (cond (gnus-newsgroup-dormant
4061                (gnus-summary-limit-include-dormant))
4062               ((and gnus-newsgroup-scored show-all)
4063                (gnus-summary-limit-include-expunged t))))
4064       ;; Function `gnus-apply-kill-file' must be called in this hook.
4065       (gnus-run-hooks 'gnus-apply-kill-hook)
4066       (if (and (zerop (buffer-size))
4067                (not no-display))
4068           (progn
4069             ;; This newsgroup is empty.
4070             (gnus-summary-catchup-and-exit nil t)
4071             (gnus-message 6 "No unread news")
4072             (when kill-buffer
4073               (gnus-kill-or-deaden-summary kill-buffer))
4074             ;; Return nil from this function.
4075             nil)
4076         ;; Hide conversation thread subtrees.  We cannot do this in
4077         ;; gnus-summary-prepare-hook since kill processing may not
4078         ;; work with hidden articles.
4079         (gnus-summary-maybe-hide-threads)
4080         (gnus-configure-windows 'summary)
4081         (when kill-buffer
4082           (gnus-kill-or-deaden-summary kill-buffer))
4083         (gnus-summary-auto-select-subject)
4084         ;; Show first unread article if requested.
4085         (if (and (not no-article)
4086                  (not no-display)
4087                  gnus-newsgroup-unreads
4088                  gnus-auto-select-first)
4089             (progn
4090               (let ((art (gnus-summary-article-number)))
4091                 (unless (and (not gnus-plugged)
4092                              (or (memq art gnus-newsgroup-undownloaded)
4093                                  (memq art gnus-newsgroup-downloadable)))
4094                   (gnus-summary-goto-article art))))
4095           ;; Don't select any articles.
4096           (gnus-summary-position-point)
4097           (gnus-configure-windows 'summary 'force)
4098           (gnus-set-mode-line 'summary))
4099         (when (and gnus-auto-center-group
4100                    (get-buffer-window gnus-group-buffer t))
4101           ;; Gotta use windows, because recenter does weird stuff if
4102           ;; the current buffer ain't the displayed window.
4103           (let ((owin (selected-window)))
4104             (select-window (get-buffer-window gnus-group-buffer t))
4105             (when (gnus-group-goto-group group)
4106               (recenter))
4107             (select-window owin)))
4108         ;; Mark this buffer as "prepared".
4109         (setq gnus-newsgroup-prepared t)
4110         (gnus-run-hooks 'gnus-summary-prepared-hook)
4111         (unless (gnus-ephemeral-group-p group)
4112           (gnus-group-update-group group nil t))
4113         t)))))
4114
4115 (defun gnus-summary-auto-select-subject ()
4116   "Select the subject line on initial group entry."
4117   (goto-char (point-min))
4118   (cond
4119    ((eq gnus-auto-select-subject 'best)
4120     (gnus-summary-best-unread-subject))
4121    ((eq gnus-auto-select-subject 'unread)
4122     (gnus-summary-first-unread-subject))
4123    ((eq gnus-auto-select-subject 'unseen)
4124     (gnus-summary-first-unseen-subject))
4125    ((eq gnus-auto-select-subject 'unseen-or-unread)
4126     (gnus-summary-first-unseen-or-unread-subject))
4127    ((eq gnus-auto-select-subject 'first)
4128     ;; Do nothing.
4129     )
4130    ((functionp gnus-auto-select-subject)
4131     (funcall gnus-auto-select-subject))))
4132
4133 (defun gnus-summary-prepare ()
4134   "Generate the summary buffer."
4135   (interactive)
4136   (let ((inhibit-read-only t))
4137     (erase-buffer)
4138     (setq gnus-newsgroup-data nil
4139           gnus-newsgroup-data-reverse nil)
4140     (gnus-run-hooks 'gnus-summary-generate-hook)
4141     ;; Generate the buffer, either with threads or without.
4142     (when gnus-newsgroup-headers
4143       (gnus-summary-prepare-threads
4144        (if gnus-show-threads
4145            (gnus-sort-gathered-threads
4146             (funcall gnus-summary-thread-gathering-function
4147                      (gnus-sort-threads
4148                       (gnus-cut-threads (gnus-make-threads)))))
4149          ;; Unthreaded display.
4150          (gnus-sort-articles gnus-newsgroup-headers))))
4151     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4152     ;; Call hooks for modifying summary buffer.
4153     (goto-char (point-min))
4154     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4155
4156 (defsubst gnus-general-simplify-subject (subject)
4157   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4158   (setq subject
4159         (cond
4160          ;; Truncate the subject.
4161          (gnus-simplify-subject-functions
4162           (gnus-map-function gnus-simplify-subject-functions subject))
4163          ((numberp gnus-summary-gather-subject-limit)
4164           (setq subject (gnus-simplify-subject-re subject))
4165           (if (> (length subject) gnus-summary-gather-subject-limit)
4166               (substring subject 0 gnus-summary-gather-subject-limit)
4167             subject))
4168          ;; Fuzzily simplify it.
4169          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4170           (gnus-simplify-subject-fuzzy subject))
4171          ;; Just remove the leading "Re:".
4172          (t
4173           (gnus-simplify-subject-re subject))))
4174
4175   (if (and gnus-summary-gather-exclude-subject
4176            (string-match gnus-summary-gather-exclude-subject subject))
4177       nil                         ; This article shouldn't be gathered
4178     subject))
4179
4180 (defun gnus-summary-simplify-subject-query ()
4181   "Query where the respool algorithm would put this article."
4182   (interactive)
4183   (gnus-summary-select-article)
4184   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4185
4186 (defun gnus-gather-threads-by-subject (threads)
4187   "Gather threads by looking at Subject headers."
4188   (if (not gnus-summary-make-false-root)
4189       threads
4190     (let ((hashtb (gnus-make-hashtable 1024))
4191           (prev threads)
4192           (result threads)
4193           subject hthread whole-subject)
4194       (while threads
4195         (setq subject (gnus-general-simplify-subject
4196                        (setq whole-subject (mail-header-subject
4197                                             (caar threads)))))
4198         (when subject
4199           (if (setq hthread (gnus-gethash subject hashtb))
4200               (progn
4201                 ;; We enter a dummy root into the thread, if we
4202                 ;; haven't done that already.
4203                 (unless (stringp (caar hthread))
4204                   (setcar hthread (list whole-subject (car hthread))))
4205                 ;; We add this new gathered thread to this gathered
4206                 ;; thread.
4207                 (setcdr (car hthread)
4208                         (nconc (cdar hthread) (list (car threads))))
4209                 ;; Remove it from the list of threads.
4210                 (setcdr prev (cdr threads))
4211                 (setq threads prev))
4212             ;; Enter this thread into the hash table.
4213             (gnus-sethash subject
4214                           (if gnus-summary-make-false-root-always
4215                               (progn
4216                                 ;; If you want a dummy root above all
4217                                 ;; threads...
4218                                 (setcar threads (list whole-subject
4219                                                       (car threads)))
4220                                 threads)
4221                             threads)
4222                           hashtb)))
4223         (setq prev threads)
4224         (setq threads (cdr threads)))
4225       result)))
4226
4227 (defun gnus-gather-threads-by-references (threads)
4228   "Gather threads by looking at References headers."
4229   (let ((idhashtb (gnus-make-hashtable 1024))
4230         (thhashtb (gnus-make-hashtable 1024))
4231         (prev threads)
4232         (result threads)
4233         ids references id gthread gid entered ref)
4234     (while threads
4235       (when (setq references (mail-header-references (caar threads)))
4236         (setq id (mail-header-id (caar threads))
4237               ids (inline (gnus-split-references references))
4238               entered nil)
4239         (while (setq ref (pop ids))
4240           (setq ids (delete ref ids))
4241           (if (not (setq gid (gnus-gethash ref idhashtb)))
4242               (progn
4243                 (gnus-sethash ref id idhashtb)
4244                 (gnus-sethash id threads thhashtb))
4245             (setq gthread (gnus-gethash gid thhashtb))
4246             (unless entered
4247               ;; We enter a dummy root into the thread, if we
4248               ;; haven't done that already.
4249               (unless (stringp (caar gthread))
4250                 (setcar gthread (list (mail-header-subject (caar gthread))
4251                                       (car gthread))))
4252               ;; We add this new gathered thread to this gathered
4253               ;; thread.
4254               (setcdr (car gthread)
4255                       (nconc (cdar gthread) (list (car threads)))))
4256             ;; Add it into the thread hash table.
4257             (gnus-sethash id gthread thhashtb)
4258             (setq entered t)
4259             ;; Remove it from the list of threads.
4260             (setcdr prev (cdr threads))
4261             (setq threads prev))))
4262       (setq prev threads)
4263       (setq threads (cdr threads)))
4264     result))
4265
4266 (defun gnus-sort-gathered-threads (threads)
4267   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4268   (let ((result threads))
4269     (while threads
4270       (when (stringp (caar threads))
4271         (setcdr (car threads)
4272                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4273       (setq threads (cdr threads)))
4274     result))
4275
4276 (defun gnus-thread-loop-p (root thread)
4277   "Say whether ROOT is in THREAD."
4278   (let ((stack (list thread))
4279         (infloop 0)
4280         th)
4281     (while (setq thread (pop stack))
4282       (setq th (cdr thread))
4283       (while (and th
4284                   (not (eq (caar th) root)))
4285         (pop th))
4286       (if th
4287           ;; We have found a loop.
4288           (let (ref-dep)
4289             (setcdr thread (delq (car th) (cdr thread)))
4290             (if (boundp (setq ref-dep (intern "none"
4291                                               gnus-newsgroup-dependencies)))
4292                 (setcdr (symbol-value ref-dep)
4293                         (nconc (cdr (symbol-value ref-dep))
4294                                (list (car th))))
4295               (set ref-dep (list nil (car th))))
4296             (setq infloop 1
4297                   stack nil))
4298         ;; Push all the subthreads onto the stack.
4299         (push (cdr thread) stack)))
4300     infloop))
4301
4302 (defun gnus-make-threads ()
4303   "Go through the dependency hashtb and find the roots.  Return all threads."
4304   (let (threads)
4305     (while (catch 'infloop
4306              (mapatoms
4307               (lambda (refs)
4308                 ;; Deal with self-referencing References loops.
4309                 (when (and (car (symbol-value refs))
4310                            (not (zerop
4311                                  (apply
4312                                   '+
4313                                   (mapcar
4314                                    (lambda (thread)
4315                                      (gnus-thread-loop-p
4316                                       (car (symbol-value refs)) thread))
4317                                    (cdr (symbol-value refs)))))))
4318                   (setq threads nil)
4319                   (throw 'infloop t))
4320                 (unless (car (symbol-value refs))
4321                   ;; These threads do not refer back to any other
4322                   ;; articles, so they're roots.
4323                   (setq threads (append (cdr (symbol-value refs)) threads))))
4324               gnus-newsgroup-dependencies)))
4325     threads))
4326
4327 ;; Build the thread tree.
4328 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4329   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4330
4331 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4332 if it was already present.
4333
4334 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4335 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4336 Message-IDs will be renamed to a unique Message-ID before being
4337 entered.
4338
4339 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4340   (let* ((id (mail-header-id header))
4341          (id-dep (and id (intern id dependencies)))
4342          parent-id ref ref-dep ref-header replaced)
4343     ;; Enter this `header' in the `dependencies' table.
4344     (cond
4345      ((not id-dep)
4346       (setq header nil))
4347      ;; The first two cases do the normal part: enter a new `header'
4348      ;; in the `dependencies' table.
4349      ((not (boundp id-dep))
4350       (set id-dep (list header)))
4351      ((null (car (symbol-value id-dep)))
4352       (setcar (symbol-value id-dep) header))
4353
4354      ;; From here the `header' was already present in the
4355      ;; `dependencies' table.
4356      (force-new
4357       ;; Overrides an existing entry;
4358       ;; just set the header part of the entry.
4359       (setcar (symbol-value id-dep) header)
4360       (setq replaced t))
4361
4362      ;; Renames the existing `header' to a unique Message-ID.
4363      ((not gnus-summary-ignore-duplicates)
4364       ;; An article with this Message-ID has already been seen.
4365       ;; We rename the Message-ID.
4366       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4367            (list header))
4368       (mail-header-set-id header id))
4369
4370      ;; The last case ignores an existing entry, except it adds any
4371      ;; additional Xrefs (in case the two articles came from different
4372      ;; servers.
4373      ;; Also sets `header' to `nil' meaning that the `dependencies'
4374      ;; table was *not* modified.
4375      (t
4376       (mail-header-set-xref
4377        (car (symbol-value id-dep))
4378        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4379                    "")
4380                (or (mail-header-xref header) "")))
4381       (setq header nil)))
4382
4383     (when (and header (not replaced))
4384       ;; First check that we are not creating a References loop.
4385       (setq parent-id (gnus-parent-id (mail-header-references header)))
4386       (setq ref parent-id)
4387       (while (and ref
4388                   (setq ref-dep (intern-soft ref dependencies))
4389                   (boundp ref-dep)
4390                   (setq ref-header (car (symbol-value ref-dep))))
4391         (if (string= id ref)
4392             ;; Yuk!  This is a reference loop.  Make the article be a
4393             ;; root article.
4394             (progn
4395               (mail-header-set-references (car (symbol-value id-dep)) "none")
4396               (setq ref nil)
4397               (setq parent-id nil))
4398           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4399       (setq ref-dep (intern (or parent-id "none") dependencies))
4400       (if (boundp ref-dep)
4401           (setcdr (symbol-value ref-dep)
4402                   (nconc (cdr (symbol-value ref-dep))
4403                          (list (symbol-value id-dep))))
4404         (set ref-dep (list nil (symbol-value id-dep)))))
4405     header))
4406
4407 (defun gnus-extract-message-id-from-in-reply-to (string)
4408   (if (string-match "<[^>]+>" string)
4409       (substring string (match-beginning 0) (match-end 0))
4410     nil))
4411
4412 (defun gnus-build-sparse-threads ()
4413   (let ((headers gnus-newsgroup-headers)
4414         (mail-parse-charset gnus-newsgroup-charset)
4415         (gnus-summary-ignore-duplicates t)
4416         header references generation relations
4417         subject child end new-child date)
4418     ;; First we create an alist of generations/relations, where
4419     ;; generations is how much we trust the relation, and the relation
4420     ;; is parent/child.
4421     (gnus-message 7 "Making sparse threads...")
4422     (save-excursion
4423       (nnheader-set-temp-buffer " *gnus sparse threads*")
4424       (while (setq header (pop headers))
4425         (when (and (setq references (mail-header-references header))
4426                    (not (string= references "")))
4427           (insert references)
4428           (setq child (mail-header-id header)
4429                 subject (mail-header-subject header)
4430                 date (mail-header-date header)
4431                 generation 0)
4432           (while (search-backward ">" nil t)
4433             (setq end (1+ (point)))
4434             (when (search-backward "<" nil t)
4435               (setq new-child (buffer-substring (point) end))
4436               (push (list (incf generation)
4437                           child (setq child new-child)
4438                           subject date)
4439                     relations)))
4440           (when child
4441             (push (list (1+ generation) child nil subject) relations))
4442           (erase-buffer)))
4443       (kill-buffer (current-buffer)))
4444     ;; Sort over trustworthiness.
4445     (dolist (relation (sort relations 'car-less-than-car))
4446       (when (gnus-dependencies-add-header
4447              (make-full-mail-header
4448               gnus-reffed-article-number
4449               (nth 3 relation) "" (or (nth 4 relation) "")
4450               (nth 1 relation)
4451               (or (nth 2 relation) "") 0 0 "")
4452              gnus-newsgroup-dependencies nil)
4453         (push gnus-reffed-article-number gnus-newsgroup-limit)
4454         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4455         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4456               gnus-newsgroup-reads)
4457         (decf gnus-reffed-article-number)))
4458     (gnus-message 7 "Making sparse threads...done")))
4459
4460 (defun gnus-build-old-threads ()
4461   ;; Look at all the articles that refer back to old articles, and
4462   ;; fetch the headers for the articles that aren't there.  This will
4463   ;; build complete threads - if the roots haven't been expired by the
4464   ;; server, that is.
4465   (let ((mail-parse-charset gnus-newsgroup-charset)
4466         id heads)
4467     (mapatoms
4468      (lambda (refs)
4469        (when (not (car (symbol-value refs)))
4470          (setq heads (cdr (symbol-value refs)))
4471          (while heads
4472            (if (memq (mail-header-number (caar heads))
4473                      gnus-newsgroup-dormant)
4474                (setq heads (cdr heads))
4475              (setq id (symbol-name refs))
4476              (while (and (setq id (gnus-build-get-header id))
4477                          (not (car (gnus-id-to-thread id)))))
4478              (setq heads nil)))))
4479      gnus-newsgroup-dependencies)))
4480
4481 (defsubst gnus-remove-odd-characters (string)
4482   "Translate STRING into something that doesn't contain weird characters."
4483   (mm-subst-char-in-string
4484    ?\r ?\-
4485    (mm-subst-char-in-string ?\n ?\- string t) t))
4486
4487 ;; This function has to be called with point after the article number
4488 ;; on the beginning of the line.
4489 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4490   (let ((eol (point-at-eol))
4491         (buffer (current-buffer))
4492         header references in-reply-to)
4493
4494     ;; overview: [num subject from date id refs chars lines misc]
4495     (unwind-protect
4496         (let (x)
4497           (narrow-to-region (point) eol)
4498           (unless (eobp)
4499             (forward-char))
4500
4501           (setq header
4502                 (make-full-mail-header
4503                  number                 ; number
4504                  (condition-case ()     ; subject
4505                      (gnus-remove-odd-characters
4506                       (funcall gnus-decode-encoded-word-function
4507                                (setq x (nnheader-nov-field))))
4508                    (error x))
4509                  (condition-case ()     ; from
4510                      (gnus-remove-odd-characters
4511                       (funcall gnus-decode-encoded-address-function
4512                                (setq x (nnheader-nov-field))))
4513                    (error x))
4514                  (nnheader-nov-field)   ; date
4515                  (nnheader-nov-read-message-id number)  ; id
4516                  (setq references (nnheader-nov-field)) ; refs
4517                  (nnheader-nov-read-integer) ; chars
4518                  (nnheader-nov-read-integer) ; lines
4519                  (unless (eobp)
4520                    (if (looking-at "Xref: ")
4521                        (goto-char (match-end 0)))
4522                    (nnheader-nov-field)) ; Xref
4523                  (nnheader-nov-parse-extra)))) ; extra
4524
4525       (widen))
4526
4527     (when (and (string= references "")
4528                (setq in-reply-to (mail-header-extra header))
4529                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4530       (mail-header-set-references
4531        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4532
4533     (when gnus-alter-header-function
4534       (funcall gnus-alter-header-function header))
4535     (gnus-dependencies-add-header header dependencies force-new)))
4536
4537 (defun gnus-build-get-header (id)
4538   "Look through the buffer of NOV lines and find the header to ID.
4539 Enter this line into the dependencies hash table, and return
4540 the id of the parent article (if any)."
4541   (let ((deps gnus-newsgroup-dependencies)
4542         found header)
4543     (prog1
4544         (with-current-buffer nntp-server-buffer
4545           (let ((case-fold-search nil))
4546             (goto-char (point-min))
4547             (while (and (not found)
4548                         (search-forward id nil t))
4549               (beginning-of-line)
4550               (setq found (looking-at
4551                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4552                                    (regexp-quote id))))
4553               (or found (beginning-of-line 2)))
4554             (when found
4555               (beginning-of-line)
4556               (and
4557                (setq header (gnus-nov-parse-line
4558                              (read (current-buffer)) deps))
4559                (gnus-parent-id (mail-header-references header))))))
4560       (when header
4561         (let ((number (mail-header-number header)))
4562           (push number gnus-newsgroup-limit)
4563           (push header gnus-newsgroup-headers)
4564           (if (memq number gnus-newsgroup-unselected)
4565               (progn
4566                 (setq gnus-newsgroup-unreads
4567                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4568                                                number))
4569                 (setq gnus-newsgroup-unselected
4570                       (delq number gnus-newsgroup-unselected)))
4571             (push number gnus-newsgroup-ancient)))))))
4572
4573 (defun gnus-build-all-threads ()
4574   "Read all the headers."
4575   (let ((gnus-summary-ignore-duplicates t)
4576         (mail-parse-charset gnus-newsgroup-charset)
4577         (dependencies gnus-newsgroup-dependencies)
4578         header article)
4579     (with-current-buffer nntp-server-buffer
4580       (let ((case-fold-search nil))
4581         (goto-char (point-min))
4582         (while (not (eobp))
4583           (ignore-errors
4584             (setq article (read (current-buffer))
4585                   header (gnus-nov-parse-line article dependencies t)))
4586           (when header
4587             (with-current-buffer gnus-summary-buffer
4588               (push header gnus-newsgroup-headers)
4589               (if (memq (setq article (mail-header-number header))
4590                         gnus-newsgroup-unselected)
4591                   (progn
4592                     (setq gnus-newsgroup-unreads
4593                           (gnus-add-to-sorted-list
4594                            gnus-newsgroup-unreads article))
4595                     (setq gnus-newsgroup-unselected
4596                           (delq article gnus-newsgroup-unselected)))
4597                 (push article gnus-newsgroup-ancient)))
4598             (forward-line 1)))))))
4599
4600 (defun gnus-summary-update-article-line (article header)
4601   "Update the line for ARTICLE using HEADER."
4602   (let* ((id (mail-header-id header))
4603          (thread (gnus-id-to-thread id)))
4604     (unless thread
4605       (error "Article in no thread"))
4606     ;; Update the thread.
4607     (setcar thread header)
4608     (gnus-summary-goto-subject article)
4609     (let* ((datal (gnus-data-find-list article))
4610            (data (car datal))
4611            (inhibit-read-only t)
4612            (level (gnus-summary-thread-level)))
4613       (gnus-delete-line)
4614       (let ((inserted (- (point)
4615                          (progn
4616                            (gnus-summary-insert-line
4617                             header level nil
4618                             (memq article gnus-newsgroup-undownloaded)
4619                             (gnus-article-mark article)
4620                             (memq article gnus-newsgroup-replied)
4621                             (memq article gnus-newsgroup-expirable)
4622                             ;; Only insert the Subject string when it's different
4623                             ;; from the previous Subject string.
4624                             (if (and
4625                                  gnus-show-threads
4626                                  (gnus-subject-equal
4627                                   (condition-case ()
4628                                       (mail-header-subject
4629                                        (gnus-data-header
4630                                         (cadr
4631                                          (gnus-data-find-list
4632                                           article
4633                                           (gnus-data-list t)))))
4634                                     ;; Error on the side of excessive subjects.
4635                                     (error ""))
4636                                   (mail-header-subject header)))
4637                                 ""
4638                               (mail-header-subject header))
4639                             nil (cdr (assq article gnus-newsgroup-scored))
4640                             (memq article gnus-newsgroup-processable))
4641                            (point)))))
4642         (when (cdr datal)
4643           (gnus-data-update-list
4644            (cdr datal)
4645            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4646
4647 (defun gnus-summary-update-article (article &optional iheader)
4648   "Update ARTICLE in the summary buffer."
4649   (set-buffer gnus-summary-buffer)
4650   (let* ((header (gnus-summary-article-header article))
4651          (id (mail-header-id header))
4652          (data (gnus-data-find article))
4653          (thread (gnus-id-to-thread id))
4654          (references (mail-header-references header))
4655          (parent
4656           (gnus-id-to-thread
4657            (or (gnus-parent-id
4658                 (when (and references
4659                            (not (equal "" references)))
4660                   references))
4661                "none")))
4662          (inhibit-read-only t)
4663          (old (car thread)))
4664     (when thread
4665       (unless iheader
4666         (setcar thread nil)
4667         (when parent
4668           (delq thread parent)))
4669       (if (gnus-summary-insert-subject id header)
4670           ;; Set the (possibly) new article number in the data structure.
4671           (gnus-data-set-number data (gnus-id-to-article id))
4672         (setcar thread old)
4673         nil))))
4674
4675 (defun gnus-rebuild-thread (id &optional line)
4676   "Rebuild the thread containing ID.
4677 If LINE, insert the rebuilt thread starting on line LINE."
4678   (let ((inhibit-read-only t)
4679         old-pos current thread data)
4680     (if (not gnus-show-threads)
4681         (setq thread (list (car (gnus-id-to-thread id))))
4682       ;; Get the thread this article is part of.
4683       (setq thread (gnus-remove-thread id)))
4684     (setq old-pos (point-at-bol))
4685     (setq current (save-excursion
4686                     (and (re-search-backward "[\r\n]" nil t)
4687                          (gnus-summary-article-number))))
4688     ;; If this is a gathered thread, we have to go some re-gathering.
4689     (when (stringp (car thread))
4690       (let ((subject (car thread))
4691             roots thr)
4692         (setq thread (cdr thread))
4693         (while thread
4694           (unless (memq (setq thr (gnus-id-to-thread
4695                                    (gnus-root-id
4696                                     (mail-header-id (caar thread)))))
4697                         roots)
4698             (push thr roots))
4699           (setq thread (cdr thread)))
4700         ;; We now have all (unique) roots.
4701         (if (= (length roots) 1)
4702             ;; All the loose roots are now one solid root.
4703             (setq thread (car roots))
4704           (setq thread (cons subject (gnus-sort-threads roots))))))
4705     (let (threads)
4706       ;; We then insert this thread into the summary buffer.
4707       (when line
4708         (goto-char (point-min))
4709         (forward-line (1- line)))
4710       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4711         (if gnus-show-threads
4712             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4713           (gnus-summary-prepare-unthreaded thread))
4714         (setq data (nreverse gnus-newsgroup-data))
4715         (setq threads gnus-newsgroup-threads))
4716       ;; We splice the new data into the data structure.
4717       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4718       ;;!!! then we want to insert at the beginning of the buffer.
4719       ;;!!! That happens to be true with Gnus now, but that may
4720       ;;!!! change in the future.  Perhaps.
4721       (gnus-data-enter-list
4722        (if line nil current) data (- (point) old-pos))
4723       (setq gnus-newsgroup-threads
4724             (nconc threads gnus-newsgroup-threads))
4725       (gnus-data-compute-positions))))
4726
4727 (defun gnus-number-to-header (number)
4728   "Return the header for article NUMBER."
4729   (let ((headers gnus-newsgroup-headers))
4730     (while (and headers
4731                 (not (= number (mail-header-number (car headers)))))
4732       (pop headers))
4733     (when headers
4734       (car headers))))
4735
4736 (defun gnus-parent-headers (in-headers &optional generation)
4737   "Return the headers of the GENERATIONth parent of HEADERS."
4738   (unless generation
4739     (setq generation 1))
4740   (let ((parent t)
4741         (headers in-headers)
4742         references)
4743     (while (and parent
4744                 (not (zerop generation))
4745                 (setq references (mail-header-references headers)))
4746       (setq headers (if (and references
4747                              (setq parent (gnus-parent-id references)))
4748                         (car (gnus-id-to-thread parent))
4749                       nil))
4750       (decf generation))
4751     (and (not (eq headers in-headers))
4752          headers)))
4753
4754 (defun gnus-id-to-thread (id)
4755   "Return the (sub-)thread where ID appears."
4756   (gnus-gethash id gnus-newsgroup-dependencies))
4757
4758 (defun gnus-id-to-article (id)
4759   "Return the article number of ID."
4760   (let ((thread (gnus-id-to-thread id)))
4761     (when (and thread
4762                (car thread))
4763       (mail-header-number (car thread)))))
4764
4765 (defun gnus-id-to-header (id)
4766   "Return the article headers of ID."
4767   (car (gnus-id-to-thread id)))
4768
4769 (defun gnus-article-displayed-root-p (article)
4770   "Say whether ARTICLE is a root(ish) article."
4771   (let ((level (gnus-summary-thread-level article))
4772         (refs (mail-header-references  (gnus-summary-article-header article)))
4773         particle)
4774     (cond
4775      ((null level) nil)
4776      ((zerop level) t)
4777      ((null refs) t)
4778      ((null (gnus-parent-id refs)) t)
4779      ((and (= 1 level)
4780            (null (setq particle (gnus-id-to-article
4781                                  (gnus-parent-id refs))))
4782            (null (gnus-summary-thread-level particle)))))))
4783
4784 (defun gnus-root-id (id)
4785   "Return the id of the root of the thread where ID appears."
4786   (let (last-id prev)
4787     (while (and id (setq prev (car (gnus-id-to-thread id))))
4788       (setq last-id id
4789             id (gnus-parent-id (mail-header-references prev))))
4790     last-id))
4791
4792 (defun gnus-articles-in-thread (thread)
4793   "Return the list of articles in THREAD."
4794   (cons (mail-header-number (car thread))
4795         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4796
4797 (defun gnus-remove-thread (id &optional dont-remove)
4798   "Remove the thread that has ID in it."
4799   (let (headers thread last-id)
4800     ;; First go up in this thread until we find the root.
4801     (setq last-id (gnus-root-id id)
4802           headers (message-flatten-list (gnus-id-to-thread last-id)))
4803     ;; We have now found the real root of this thread.  It might have
4804     ;; been gathered into some loose thread, so we have to search
4805     ;; through the threads to find the thread we wanted.
4806     (let ((threads gnus-newsgroup-threads)
4807           sub)
4808       (while threads
4809         (setq sub (car threads))
4810         (if (stringp (car sub))
4811             ;; This is a gathered thread, so we look at the roots
4812             ;; below it to find whether this article is in this
4813             ;; gathered root.
4814             (progn
4815               (setq sub (cdr sub))
4816               (while sub
4817                 (when (member (caar sub) headers)
4818                   (setq thread (car threads)
4819                         threads nil
4820                         sub nil))
4821                 (setq sub (cdr sub))))
4822           ;; It's an ordinary thread, so we check it.
4823           (when (eq (car sub) (car headers))
4824             (setq thread sub
4825                   threads nil)))
4826         (setq threads (cdr threads)))
4827       ;; If this article is in no thread, then it's a root.
4828       (if thread
4829           (unless dont-remove
4830             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4831         (setq thread (gnus-id-to-thread last-id)))
4832       (when thread
4833         (prog1
4834             thread                      ; We return this thread.
4835           (unless dont-remove
4836             (if (stringp (car thread))
4837                 (progn
4838                   ;; If we use dummy roots, then we have to remove the
4839                   ;; dummy root as well.
4840                   (when (eq gnus-summary-make-false-root 'dummy)
4841                     ;; We go to the dummy root by going to
4842                     ;; the first sub-"thread", and then one line up.
4843                     (gnus-summary-goto-article
4844                      (mail-header-number (caadr thread)))
4845                     (forward-line -1)
4846                     (gnus-delete-line)
4847                     (gnus-data-compute-positions))
4848                   (setq thread (cdr thread))
4849                   (while thread
4850                     (gnus-remove-thread-1 (car thread))
4851                     (setq thread (cdr thread))))
4852               (gnus-remove-thread-1 thread))))))))
4853
4854 (defun gnus-remove-thread-1 (thread)
4855   "Remove the thread THREAD recursively."
4856   (let ((number (mail-header-number (pop thread)))
4857         d)
4858     (setq thread (reverse thread))
4859     (while thread
4860       (gnus-remove-thread-1 (pop thread)))
4861     (when (setq d (gnus-data-find number))
4862       (goto-char (gnus-data-pos d))
4863       (gnus-summary-show-thread)
4864       (gnus-data-remove
4865        number
4866        (- (point-at-bol)
4867           (prog1
4868               (1+ (point-at-eol))
4869             (gnus-delete-line)))))))
4870
4871 (defun gnus-sort-threads-recursive (threads func)
4872   (sort (mapcar (lambda (thread)
4873                   (cons (car thread)
4874                         (and (cdr thread)
4875                              (gnus-sort-threads-recursive (cdr thread) func))))
4876                 threads) func))
4877
4878 (defun gnus-sort-threads-loop (threads func)
4879   (let* ((superthread (cons nil threads))
4880          (stack (list (cons superthread threads)))
4881          remaining-threads thread)
4882     (while stack
4883       (setq remaining-threads (cdr (car stack)))
4884       (if remaining-threads
4885           (progn (setq thread (car remaining-threads))
4886                  (setcdr (car stack) (cdr remaining-threads))
4887                  (if (cdr thread)
4888                      (push (cons thread (cdr thread)) stack)))
4889         (setq thread (caar stack))
4890         (setcdr thread (sort (cdr thread) func))
4891         (pop stack)))
4892     (cdr superthread)))
4893
4894 (defun gnus-sort-threads (threads)
4895   "Sort THREADS."
4896   (if (not gnus-thread-sort-functions)
4897       threads
4898     (gnus-message 8 "Sorting threads...")
4899     (prog1
4900         (condition-case nil
4901             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4902               (gnus-sort-threads-recursive
4903                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4904           ;; Even after binding max-lisp-eval-depth, the recursive
4905           ;; sorter might fail for very long threads.  In that case,
4906           ;; try using a (less well-tested) non-recursive sorter.
4907           (error (gnus-message 9 "Sorting threads with loop...")
4908                  (gnus-sort-threads-loop
4909                   threads (gnus-make-sort-function
4910                            gnus-thread-sort-functions))))
4911       (gnus-message 8 "Sorting threads...done"))))
4912
4913 (defun gnus-sort-articles (articles)
4914   "Sort ARTICLES."
4915   (when gnus-article-sort-functions
4916     (gnus-message 7 "Sorting articles...")
4917     (prog1
4918         (setq gnus-newsgroup-headers
4919               (sort articles (gnus-make-sort-function
4920                               gnus-article-sort-functions)))
4921       (gnus-message 7 "Sorting articles...done"))))
4922
4923 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4924 (defmacro gnus-thread-header (thread)
4925   "Return header of first article in THREAD.
4926 Note that THREAD must never, ever be anything else than a variable -
4927 using some other form will lead to serious barfage."
4928   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4929   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4930   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4931         (vector thread) 2))
4932
4933 (defsubst gnus-article-sort-by-number (h1 h2)
4934   "Sort articles by article number."
4935   (< (mail-header-number h1)
4936      (mail-header-number h2)))
4937
4938 (defun gnus-thread-sort-by-number (h1 h2)
4939   "Sort threads by root article number."
4940   (gnus-article-sort-by-number
4941    (gnus-thread-header h1) (gnus-thread-header h2)))
4942
4943 (defsubst gnus-article-sort-by-random (h1 h2)
4944   "Sort articles randomly."
4945   (zerop (random 2)))
4946
4947 (defun gnus-thread-sort-by-random (h1 h2)
4948   "Sort threads randomly."
4949   (gnus-article-sort-by-random
4950    (gnus-thread-header h1) (gnus-thread-header h2)))
4951
4952 (defsubst gnus-article-sort-by-lines (h1 h2)
4953   "Sort articles by article Lines header."
4954   (< (mail-header-lines h1)
4955      (mail-header-lines h2)))
4956
4957 (defun gnus-thread-sort-by-lines (h1 h2)
4958   "Sort threads by root article Lines header."
4959   (gnus-article-sort-by-lines
4960    (gnus-thread-header h1) (gnus-thread-header h2)))
4961
4962 (defsubst gnus-article-sort-by-chars (h1 h2)
4963   "Sort articles by octet length."
4964   (< (mail-header-chars h1)
4965      (mail-header-chars h2)))
4966
4967 (defun gnus-thread-sort-by-chars (h1 h2)
4968   "Sort threads by root article octet length."
4969   (gnus-article-sort-by-chars
4970    (gnus-thread-header h1) (gnus-thread-header h2)))
4971
4972 (defsubst gnus-article-sort-by-author (h1 h2)
4973   "Sort articles by root author."
4974   (gnus-string<
4975    (let ((extract (funcall
4976                    gnus-extract-address-components
4977                    (mail-header-from h1))))
4978      (or (car extract) (cadr extract) ""))
4979    (let ((extract (funcall
4980                    gnus-extract-address-components
4981                    (mail-header-from h2))))
4982      (or (car extract) (cadr extract) ""))))
4983
4984 (defun gnus-thread-sort-by-author (h1 h2)
4985   "Sort threads by root author."
4986   (gnus-article-sort-by-author
4987    (gnus-thread-header h1)  (gnus-thread-header h2)))
4988
4989 (defsubst gnus-article-sort-by-recipient (h1 h2)
4990   "Sort articles by recipient."
4991   (gnus-string<
4992    (let ((extract (funcall
4993                    gnus-extract-address-components
4994                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4995      (or (car extract) (cadr extract)))
4996    (let ((extract (funcall
4997                    gnus-extract-address-components
4998                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4999      (or (car extract) (cadr extract)))))
5000
5001 (defun gnus-thread-sort-by-recipient (h1 h2)
5002   "Sort threads by root recipient."
5003   (gnus-article-sort-by-recipient
5004    (gnus-thread-header h1) (gnus-thread-header h2)))
5005
5006 (defsubst gnus-article-sort-by-subject (h1 h2)
5007   "Sort articles by root subject."
5008   (gnus-string<
5009    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5010    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5011
5012 (defun gnus-thread-sort-by-subject (h1 h2)
5013   "Sort threads by root subject."
5014   (gnus-article-sort-by-subject
5015    (gnus-thread-header h1) (gnus-thread-header h2)))
5016
5017 (defsubst gnus-article-sort-by-date (h1 h2)
5018   "Sort articles by root article date."
5019   (time-less-p
5020    (gnus-date-get-time (mail-header-date h1))
5021    (gnus-date-get-time (mail-header-date h2))))
5022
5023 (defun gnus-thread-sort-by-date (h1 h2)
5024   "Sort threads by root article date."
5025   (gnus-article-sort-by-date
5026    (gnus-thread-header h1) (gnus-thread-header h2)))
5027
5028 (defsubst gnus-article-sort-by-score (h1 h2)
5029   "Sort articles by root article score.
5030 Unscored articles will be counted as having a score of zero."
5031   (> (or (cdr (assq (mail-header-number h1)
5032                     gnus-newsgroup-scored))
5033          gnus-summary-default-score 0)
5034      (or (cdr (assq (mail-header-number h2)
5035                     gnus-newsgroup-scored))
5036          gnus-summary-default-score 0)))
5037
5038 (defun gnus-thread-sort-by-score (h1 h2)
5039   "Sort threads by root article score."
5040   (gnus-article-sort-by-score
5041    (gnus-thread-header h1) (gnus-thread-header h2)))
5042
5043 (defun gnus-thread-sort-by-total-score (h1 h2)
5044   "Sort threads by the sum of all scores in the thread.
5045 Unscored articles will be counted as having a score of zero."
5046   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5047
5048 (defun gnus-thread-total-score (thread)
5049   ;; This function find the total score of THREAD.
5050   (cond
5051    ((null thread)
5052     0)
5053    ((consp thread)
5054     (if (stringp (car thread))
5055         (apply gnus-thread-score-function 0
5056                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5057       (gnus-thread-total-score-1 thread)))
5058    (t
5059     (gnus-thread-total-score-1 (list thread)))))
5060
5061 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5062   "Sort articles by number."
5063   (gnus-article-sort-by-number h1 h2))
5064
5065 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5066   "Sort threads such that the thread with the most recently arrived article comes first."
5067   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5068
5069 (defun gnus-thread-highest-number (thread)
5070   "Return the highest article number in THREAD."
5071   (apply 'max (mapcar (lambda (header)
5072                         (mail-header-number header))
5073                       (message-flatten-list thread))))
5074
5075 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5076   "Sort articles by number."
5077   (gnus-article-sort-by-date h1 h2))
5078
5079 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5080   "Sort threads such that the thread with the most recently dated article comes first."
5081   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5082
5083 ; Since this is called not only to sort the top-level threads, but
5084 ; also in recursive sorts to order the articles within a thread, each
5085 ; article will be processed many times.  Thus it speeds things up
5086 ; quite a bit to use gnus-date-get-time, which caches the time value.
5087 (defun gnus-thread-latest-date (thread)
5088   "Return the highest article date in THREAD."
5089   (apply 'max
5090          (mapcar (lambda (header) (gnus-float-time
5091                                    (gnus-date-get-time
5092                                     (mail-header-date header))))
5093                  (message-flatten-list thread))))
5094
5095 (defun gnus-thread-total-score-1 (root)
5096   ;; This function find the total score of the thread below ROOT.
5097   (setq root (car root))
5098   (apply gnus-thread-score-function
5099          (or (append
5100               (mapcar 'gnus-thread-total-score
5101                       (cdr (gnus-id-to-thread (mail-header-id root))))
5102               (when (> (mail-header-number root) 0)
5103                 (list (or (cdr (assq (mail-header-number root)
5104                                      gnus-newsgroup-scored))
5105                           gnus-summary-default-score 0))))
5106              (list gnus-summary-default-score)
5107              '(0))))
5108
5109 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5110 (defvar gnus-tmp-prev-subject nil)
5111 (defvar gnus-tmp-false-parent nil)
5112 (defvar gnus-tmp-root-expunged nil)
5113 (defvar gnus-tmp-dummy-line nil)
5114
5115 (defun gnus-extra-header (type &optional header)
5116   "Return the extra header of TYPE."
5117   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5118       ""))
5119
5120 (defvar gnus-tmp-thread-tree-header-string "")
5121
5122 (defcustom gnus-sum-thread-tree-root "> "
5123   "With %B spec, used for the root of a thread.
5124 If nil, use subject instead."
5125   :version "22.1"
5126   :type '(radio (const :format "%v  " nil) string)
5127   :group 'gnus-thread)
5128
5129 (defcustom gnus-sum-thread-tree-false-root "> "
5130   "With %B spec, used for a false root of a thread.
5131 If nil, use subject instead."
5132   :version "22.1"
5133   :type '(radio (const :format "%v  " nil) string)
5134   :group 'gnus-thread)
5135
5136 (defcustom gnus-sum-thread-tree-single-indent ""
5137   "With %B spec, used for a thread with just one message.
5138 If nil, use subject instead."
5139   :version "22.1"
5140   :type '(radio (const :format "%v  " nil) string)
5141   :group 'gnus-thread)
5142
5143 (defcustom gnus-sum-thread-tree-vertical "| "
5144   "With %B spec, used for drawing a vertical line."
5145   :version "22.1"
5146   :type 'string
5147   :group 'gnus-thread)
5148
5149 (defcustom gnus-sum-thread-tree-indent "  "
5150   "With %B spec, used for indenting."
5151   :version "22.1"
5152   :type 'string
5153   :group 'gnus-thread)
5154
5155 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5156   "With %B spec, used for a leaf with brothers."
5157   :version "22.1"
5158   :type 'string
5159   :group 'gnus-thread)
5160
5161 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5162   "With %B spec, used for a leaf without brothers."
5163   :version "22.1"
5164   :type 'string
5165   :group 'gnus-thread)
5166
5167 (defcustom gnus-summary-display-while-building nil
5168   "If non-nil, show and update the summary buffer as it's being built.
5169 If the value is t, update the buffer after every line is inserted.  If
5170 the value is an integer (N), update the display every N lines."
5171   :version "22.1"
5172   :group 'gnus-thread
5173   :type '(choice (const :tag "off" nil)
5174                  number
5175                  (const :tag "frequently" t)))
5176
5177 (defun gnus-summary-prepare-threads (threads)
5178   "Prepare summary buffer from THREADS and indentation LEVEL.
5179 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5180 or a straight list of headers."
5181   (gnus-message 7 "Generating summary...")
5182
5183   (setq gnus-newsgroup-threads threads)
5184   (beginning-of-line)
5185
5186   (let ((gnus-tmp-level 0)
5187         (default-score (or gnus-summary-default-score 0))
5188         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5189         (building-line-count gnus-summary-display-while-building)
5190         (building-count (integerp gnus-summary-display-while-building))
5191         thread number subject stack state gnus-tmp-gathered beg-match
5192         new-roots gnus-tmp-new-adopts thread-end simp-subject
5193         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5194         gnus-tmp-replied gnus-tmp-subject-or-nil
5195         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5196         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5197         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5198         tree-stack)
5199
5200     (setq gnus-tmp-prev-subject nil
5201           gnus-tmp-thread-tree-header-string "")
5202
5203     (if (vectorp (car threads))
5204         ;; If this is a straight (sic) list of headers, then a
5205         ;; threaded summary display isn't required, so we just create
5206         ;; an unthreaded one.
5207         (gnus-summary-prepare-unthreaded threads)
5208
5209       ;; Do the threaded display.
5210
5211       (if gnus-summary-display-while-building
5212           (switch-to-buffer (buffer-name)))
5213       (while (or threads stack gnus-tmp-new-adopts new-roots)
5214
5215         (if (and (= gnus-tmp-level 0)
5216                  (or (not stack)
5217                      (= (caar stack) 0))
5218                  (not gnus-tmp-false-parent)
5219                  (or gnus-tmp-new-adopts new-roots))
5220             (if gnus-tmp-new-adopts
5221                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5222                       thread (list (car gnus-tmp-new-adopts))
5223                       gnus-tmp-header (caar thread)
5224                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5225               (when new-roots
5226                 (setq thread (list (car new-roots))
5227                       gnus-tmp-header (caar thread)
5228                       new-roots (cdr new-roots))))
5229
5230           (if threads
5231               ;; If there are some threads, we do them before the
5232               ;; threads on the stack.
5233               (setq thread threads
5234                     gnus-tmp-header (caar thread))
5235             ;; There were no current threads, so we pop something off
5236             ;; the stack.
5237             (setq state (car stack)
5238                   gnus-tmp-level (car state)
5239                   tree-stack (cadr state)
5240                   thread (caddr state)
5241                   stack (cdr stack)
5242                   gnus-tmp-header (caar thread))))
5243
5244         (setq gnus-tmp-false-parent nil)
5245         (setq gnus-tmp-root-expunged nil)
5246         (setq thread-end nil)
5247
5248         (if (stringp gnus-tmp-header)
5249             ;; The header is a dummy root.
5250             (cond
5251              ((eq gnus-summary-make-false-root 'adopt)
5252               ;; We let the first article adopt the rest.
5253               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5254                                                (cddar thread)))
5255               (setq gnus-tmp-gathered
5256                     (nconc (mapcar
5257                             (lambda (h) (mail-header-number (car h)))
5258                             (cddar thread))
5259                            gnus-tmp-gathered))
5260               (setq thread (cons (list (caar thread)
5261                                        (cadar thread))
5262                                  (cdr thread)))
5263               (setq gnus-tmp-level -1
5264                     gnus-tmp-false-parent t))
5265              ((eq gnus-summary-make-false-root 'empty)
5266               ;; We print adopted articles with empty subject fields.
5267               (setq gnus-tmp-gathered
5268                     (nconc (mapcar
5269                             (lambda (h) (mail-header-number (car h)))
5270                             (cddar thread))
5271                            gnus-tmp-gathered))
5272               (setq gnus-tmp-level -1))
5273              ((eq gnus-summary-make-false-root 'dummy)
5274               ;; We remember that we probably want to output a dummy
5275               ;; root.
5276               (setq gnus-tmp-dummy-line gnus-tmp-header)
5277               (setq gnus-tmp-prev-subject gnus-tmp-header))
5278              (t
5279               ;; We do not make a root for the gathered
5280               ;; sub-threads at all.
5281               (setq gnus-tmp-level -1)))
5282
5283           (setq number (mail-header-number gnus-tmp-header)
5284                 subject (mail-header-subject gnus-tmp-header)
5285                 simp-subject (gnus-simplify-subject-fully subject))
5286
5287           (cond
5288            ;; If the thread has changed subject, we might want to make
5289            ;; this subthread into a root.
5290            ((and (null gnus-thread-ignore-subject)
5291                  (not (zerop gnus-tmp-level))
5292                  gnus-tmp-prev-subject
5293                  (not (string= gnus-tmp-prev-subject simp-subject)))
5294             (setq new-roots (nconc new-roots (list (car thread)))
5295                   thread-end t
5296                   gnus-tmp-header nil))
5297            ;; If the article lies outside the current limit,
5298            ;; then we do not display it.
5299            ((not (memq number gnus-newsgroup-limit))
5300             (setq gnus-tmp-gathered
5301                   (nconc (mapcar
5302                           (lambda (h) (mail-header-number (car h)))
5303                           (cdar thread))
5304                          gnus-tmp-gathered))
5305             (setq gnus-tmp-new-adopts (if (cdar thread)
5306                                           (append gnus-tmp-new-adopts
5307                                                   (cdar thread))
5308                                         gnus-tmp-new-adopts)
5309                   thread-end t
5310                   gnus-tmp-header nil)
5311             (when (zerop gnus-tmp-level)
5312               (setq gnus-tmp-root-expunged t)))
5313            ;; Perhaps this article is to be marked as read?
5314            ((and gnus-summary-mark-below
5315                  (< (or (cdr (assq number gnus-newsgroup-scored))
5316                         default-score)
5317                     gnus-summary-mark-below)
5318                  ;; Don't touch sparse articles.
5319                  (not (gnus-summary-article-sparse-p number))
5320                  (not (gnus-summary-article-ancient-p number)))
5321             (setq gnus-newsgroup-unreads
5322                   (delq number gnus-newsgroup-unreads))
5323             (if gnus-newsgroup-auto-expire
5324                 (setq gnus-newsgroup-expirable
5325                       (gnus-add-to-sorted-list
5326                        gnus-newsgroup-expirable number))
5327               (push (cons number gnus-low-score-mark)
5328                     gnus-newsgroup-reads))))
5329
5330           (when gnus-tmp-header
5331             ;; We may have an old dummy line to output before this
5332             ;; article.
5333             (when (and gnus-tmp-dummy-line
5334                        (gnus-subject-equal
5335                         gnus-tmp-dummy-line
5336                         (mail-header-subject gnus-tmp-header)))
5337               (gnus-summary-insert-dummy-line
5338                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5339               (setq gnus-tmp-dummy-line nil))
5340
5341             ;; Compute the mark.
5342             (setq gnus-tmp-unread (gnus-article-mark number))
5343
5344             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5345                                   gnus-tmp-header gnus-tmp-level)
5346                   gnus-newsgroup-data)
5347
5348             ;; Actually insert the line.
5349             (setq
5350              gnus-tmp-subject-or-nil
5351              (cond
5352               ((and gnus-thread-ignore-subject
5353                     gnus-tmp-prev-subject
5354                     (not (string= gnus-tmp-prev-subject simp-subject)))
5355                subject)
5356               ((zerop gnus-tmp-level)
5357                (if (and (eq gnus-summary-make-false-root 'empty)
5358                         (memq number gnus-tmp-gathered)
5359                         gnus-tmp-prev-subject
5360                         (string= gnus-tmp-prev-subject simp-subject))
5361                    gnus-summary-same-subject
5362                  subject))
5363               (t gnus-summary-same-subject)))
5364             (if (and (eq gnus-summary-make-false-root 'adopt)
5365                      (= gnus-tmp-level 1)
5366                      (memq number gnus-tmp-gathered))
5367                 (setq gnus-tmp-opening-bracket ?\<
5368                       gnus-tmp-closing-bracket ?\>)
5369               (setq gnus-tmp-opening-bracket ?\[
5370                     gnus-tmp-closing-bracket ?\]))
5371             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5372                 (gnus-make-thread-indent-array
5373                  (max (* 2 (length gnus-thread-indent-array))
5374                       gnus-tmp-level)))
5375             (setq
5376              gnus-tmp-indentation
5377              (aref gnus-thread-indent-array gnus-tmp-level)
5378              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5379              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5380                                 gnus-summary-default-score 0)
5381              gnus-tmp-score-char
5382              (if (or (null gnus-summary-default-score)
5383                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5384                          gnus-summary-zcore-fuzz))
5385                  ?                      ;Whitespace
5386                (if (< gnus-tmp-score gnus-summary-default-score)
5387                    gnus-score-below-mark gnus-score-over-mark))
5388              gnus-tmp-replied
5389              (cond ((memq number gnus-newsgroup-processable)
5390                     gnus-process-mark)
5391                    ((memq number gnus-newsgroup-cached)
5392                     gnus-cached-mark)
5393                    ((memq number gnus-newsgroup-replied)
5394                     gnus-replied-mark)
5395                    ((memq number gnus-newsgroup-forwarded)
5396                     gnus-forwarded-mark)
5397                    ((memq number gnus-newsgroup-saved)
5398                     gnus-saved-mark)
5399                    ((memq number gnus-newsgroup-unseen)
5400                     gnus-unseen-mark)
5401                    (t gnus-no-mark))
5402              gnus-tmp-downloaded
5403              (cond ((memq number gnus-newsgroup-undownloaded)
5404                     gnus-undownloaded-mark)
5405                    (gnus-newsgroup-agentized
5406                     gnus-downloaded-mark)
5407                    (t
5408                     gnus-no-mark))
5409              gnus-tmp-from (mail-header-from gnus-tmp-header)
5410              gnus-tmp-name
5411              (cond
5412               ((string-match "<[^>]+> *$" gnus-tmp-from)
5413                (setq beg-match (match-beginning 0))
5414                (or (and (string-match "^\".+\"" gnus-tmp-from)
5415                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5416                    (substring gnus-tmp-from 0 beg-match)))
5417               ((string-match "(.+)" gnus-tmp-from)
5418                (substring gnus-tmp-from
5419                           (1+ (match-beginning 0)) (1- (match-end 0))))
5420               (t gnus-tmp-from))
5421
5422              ;; Do the %B string
5423              gnus-tmp-thread-tree-header-string
5424              (cond
5425               ((not gnus-show-threads) "")
5426               ((zerop gnus-tmp-level)
5427                (cond ((cdar thread)
5428                       (or gnus-sum-thread-tree-root subject))
5429                      (gnus-tmp-new-adopts
5430                       (or gnus-sum-thread-tree-false-root subject))
5431                      (t
5432                       (or gnus-sum-thread-tree-single-indent subject))))
5433               (t
5434                (concat (apply 'concat
5435                               (mapcar (lambda (item)
5436                                         (if (= item 1)
5437                                             gnus-sum-thread-tree-vertical
5438                                           gnus-sum-thread-tree-indent))
5439                                       (cdr (reverse tree-stack))))
5440                        (if (nth 1 thread)
5441                            gnus-sum-thread-tree-leaf-with-other
5442                          gnus-sum-thread-tree-single-leaf)))))
5443             (when (string= gnus-tmp-name "")
5444               (setq gnus-tmp-name gnus-tmp-from))
5445             (unless (numberp gnus-tmp-lines)
5446               (setq gnus-tmp-lines -1))
5447             (if (= gnus-tmp-lines -1)
5448                 (setq gnus-tmp-lines "?")
5449               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5450             (gnus-put-text-property
5451              (point)
5452              (progn (eval gnus-summary-line-format-spec) (point))
5453              'gnus-number number)
5454             (when gnus-visual-p
5455               (forward-line -1)
5456               (gnus-summary-highlight-line)
5457               (when gnus-summary-update-hook
5458                 (gnus-run-hooks 'gnus-summary-update-hook))
5459               (forward-line 1))
5460
5461             (setq gnus-tmp-prev-subject simp-subject)))
5462
5463         (when (nth 1 thread)
5464           (push (list (max 0 gnus-tmp-level)
5465                       (copy-sequence tree-stack)
5466                       (nthcdr 1 thread))
5467                 stack))
5468         (push (if (nth 1 thread) 1 0) tree-stack)
5469         (incf gnus-tmp-level)
5470         (setq threads (if thread-end nil (cdar thread)))
5471         (if gnus-summary-display-while-building
5472             (if building-count
5473                 (progn
5474                   ;; use a set frequency
5475                   (setq building-line-count (1- building-line-count))
5476                   (when (= building-line-count 0)
5477                     (sit-for 0)
5478                     (setq building-line-count
5479                           gnus-summary-display-while-building)))
5480               ;; always
5481               (sit-for 0)))
5482         (unless threads
5483           (setq gnus-tmp-level 0)))))
5484   (gnus-message 7 "Generating summary...done"))
5485
5486 (defun gnus-summary-prepare-unthreaded (headers)
5487   "Generate an unthreaded summary buffer based on HEADERS."
5488   (let (header number mark)
5489
5490     (beginning-of-line)
5491
5492     (while headers
5493       ;; We may have to root out some bad articles...
5494       (when (memq (setq number (mail-header-number
5495                                 (setq header (pop headers))))
5496                   gnus-newsgroup-limit)
5497         ;; Mark article as read when it has a low score.
5498         (when (and gnus-summary-mark-below
5499                    (< (or (cdr (assq number gnus-newsgroup-scored))
5500                           gnus-summary-default-score 0)
5501                       gnus-summary-mark-below)
5502                    (not (gnus-summary-article-ancient-p number)))
5503           (setq gnus-newsgroup-unreads
5504                 (delq number gnus-newsgroup-unreads))
5505           (if gnus-newsgroup-auto-expire
5506               (push number gnus-newsgroup-expirable)
5507             (push (cons number gnus-low-score-mark)
5508                   gnus-newsgroup-reads)))
5509
5510         (setq mark (gnus-article-mark number))
5511         (push (gnus-data-make number mark (1+ (point)) header 0)
5512               gnus-newsgroup-data)
5513         (gnus-summary-insert-line
5514          header 0 number
5515          (memq number gnus-newsgroup-undownloaded)
5516          mark (memq number gnus-newsgroup-replied)
5517          (memq number gnus-newsgroup-expirable)
5518          (mail-header-subject header) nil
5519          (cdr (assq number gnus-newsgroup-scored))
5520          (memq number gnus-newsgroup-processable))))))
5521
5522 (defun gnus-group-get-list-identifiers (group)
5523   "Get list identifier regexp for GROUP."
5524   (or (gnus-parameter-list-identifier group)
5525       (if (consp gnus-list-identifiers)
5526           (mapconcat 'identity gnus-list-identifiers " *\\|")
5527         gnus-list-identifiers)))
5528
5529 (defun gnus-summary-remove-list-identifiers ()
5530   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5531   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5532         changed subject)
5533     (when regexp
5534       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5535       (dolist (header gnus-newsgroup-headers)
5536         (setq subject (mail-header-subject header)
5537               changed nil)
5538         (while (string-match regexp subject)
5539           (setq subject
5540                 (concat (substring subject 0 (match-beginning 1))
5541                         (substring subject (match-end 0)))
5542                 changed t))
5543         (when changed
5544           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5545             (setq subject
5546                   (concat (substring subject 0 (match-beginning 1))
5547                           (substring subject (match-end 1)))))
5548           (mail-header-set-subject header subject))))))
5549
5550 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5551   "Fetch headers of ARTICLES."
5552   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5553     (gnus-message 7 "Fetching headers for %s..." name)
5554     (prog1
5555         (if (eq 'nov
5556                 (setq gnus-headers-retrieved-by
5557                       (gnus-retrieve-headers
5558                        articles gnus-newsgroup-name
5559                        (or limit
5560                            ;; We might want to fetch old headers, but
5561                            ;; not if there is only 1 article.
5562                            (and (or (and
5563                                      (not (eq gnus-fetch-old-headers 'some))
5564                                      (not (numberp gnus-fetch-old-headers)))
5565                                     (> (length articles) 1))
5566                                 gnus-fetch-old-headers)))))
5567             (gnus-get-newsgroup-headers-xover
5568              articles force-new dependencies gnus-newsgroup-name t)
5569           (gnus-get-newsgroup-headers dependencies force-new))
5570       (gnus-message 7 "Fetching headers for %s...done" name))))
5571
5572 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5573   "Select newsgroup GROUP.
5574 If READ-ALL is non-nil, all articles in the group are selected.
5575 If SELECT-ARTICLES, only select those articles from GROUP."
5576   (let* ((entry (gnus-group-entry group))
5577          ;;!!! Dirty hack; should be removed.
5578          (gnus-summary-ignore-duplicates
5579           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5580               t
5581             gnus-summary-ignore-duplicates))
5582          (info (nth 2 entry))
5583          charset articles fetched-articles cached)
5584
5585     (unless (gnus-check-server
5586              (set (make-local-variable 'gnus-current-select-method)
5587                   (gnus-find-method-for-group group)))
5588       (error "Couldn't open server"))
5589     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5590
5591     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5592         (gnus-activate-group group)     ; Or we can activate it...
5593         (progn                          ; Or we bug out.
5594           (when (equal major-mode 'gnus-summary-mode)
5595             (gnus-kill-buffer (current-buffer)))
5596           (error
5597            "Couldn't activate group %s: %s"
5598            (mm-decode-coding-string group charset)
5599            (mm-decode-coding-string (gnus-status-message group) charset))))
5600
5601     (unless (gnus-request-group group t)
5602       (when (equal major-mode 'gnus-summary-mode)
5603         (gnus-kill-buffer (current-buffer)))
5604       (error "Couldn't request group %s: %s"
5605              (mm-decode-coding-string group charset)
5606              (mm-decode-coding-string (gnus-status-message group) charset)))
5607
5608     (when (and gnus-agent
5609                (gnus-active group))
5610       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5611
5612       (setq gnus-summary-use-undownloaded-faces
5613             (gnus-agent-find-parameter
5614              group
5615              'agent-enable-undownloaded-faces)))
5616
5617     (setq gnus-newsgroup-name group
5618           gnus-newsgroup-unselected nil
5619           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5620
5621     (let ((display (gnus-group-find-parameter group 'display)))
5622       (setq gnus-newsgroup-display
5623             (cond
5624              ((not (zerop (or (car-safe read-all) 0)))
5625               ;; The user entered the group with C-u SPC/RET, let's show
5626               ;; all articles.
5627               'gnus-not-ignore)
5628              ((eq display 'all)
5629               'gnus-not-ignore)
5630              ((arrayp display)
5631               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5632              ((numberp display)
5633               ;; The following is probably the "correct" solution, but
5634               ;; it makes Gnus fetch all headers and then limit the
5635               ;; articles (which is slow), so instead we hack the
5636               ;; select-articles parameter instead. -- Simon Josefsson
5637               ;; <jas@kth.se>
5638               ;;
5639               ;; (gnus-byte-compile
5640               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5641               ;;                         display)))))
5642               (setq select-articles
5643                     (gnus-uncompress-range
5644                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5645                              (if (> tmp 0)
5646                                  tmp
5647                                1))
5648                            (cdr (gnus-active group)))))
5649               nil)
5650              (t
5651               nil))))
5652
5653     (gnus-summary-setup-default-charset)
5654
5655     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5656     (when (gnus-virtual-group-p group)
5657       (setq cached gnus-newsgroup-cached))
5658
5659     (setq gnus-newsgroup-unreads
5660           (gnus-sorted-ndifference
5661            (gnus-sorted-ndifference gnus-newsgroup-unreads
5662                                     gnus-newsgroup-marked)
5663            gnus-newsgroup-dormant))
5664
5665     (setq gnus-newsgroup-processable nil)
5666
5667     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5668
5669     ;; Adjust and set lists of article marks.
5670     (when info
5671       (gnus-adjust-marked-articles info))
5672     (if (setq articles select-articles)
5673         (setq gnus-newsgroup-unselected
5674               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5675       (setq articles (gnus-articles-to-read group read-all)))
5676
5677     (cond
5678      ((null articles)
5679       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5680       'quit)
5681      ((eq articles 0) nil)
5682      (t
5683       ;; Init the dependencies hash table.
5684       (setq gnus-newsgroup-dependencies
5685             (gnus-make-hashtable (length articles)))
5686       (if (gnus-buffer-live-p gnus-group-buffer)
5687           (gnus-set-global-variables)
5688         (set-default 'gnus-newsgroup-name gnus-newsgroup-name))
5689       ;; Retrieve the headers and read them in.
5690
5691       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5692
5693       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5694       (when cached
5695         (setq gnus-newsgroup-cached cached))
5696
5697       ;; Suppress duplicates?
5698       (when gnus-suppress-duplicates
5699         (gnus-dup-suppress-articles))
5700
5701       ;; Set the initial limit.
5702       (setq gnus-newsgroup-limit (copy-sequence articles))
5703       ;; Remove canceled articles from the list of unread articles.
5704       (setq fetched-articles
5705             (mapcar (lambda (headers) (mail-header-number headers))
5706                     gnus-newsgroup-headers))
5707       (setq gnus-newsgroup-articles fetched-articles)
5708       (setq gnus-newsgroup-unreads
5709             (gnus-sorted-nintersection
5710              gnus-newsgroup-unreads fetched-articles))
5711       (gnus-compute-unseen-list)
5712
5713       ;; Removed marked articles that do not exist.
5714       (gnus-update-missing-marks
5715        (gnus-sorted-difference articles fetched-articles))
5716       ;; We might want to build some more threads first.
5717       (when (and gnus-fetch-old-headers
5718                  (eq gnus-headers-retrieved-by 'nov))
5719         (if (eq gnus-fetch-old-headers 'invisible)
5720             (gnus-build-all-threads)
5721           (gnus-build-old-threads)))
5722       ;; Let the Gnus agent mark articles as read.
5723       (when gnus-agent
5724         (gnus-agent-get-undownloaded-list))
5725       ;; Remove list identifiers from subject
5726       (gnus-summary-remove-list-identifiers)
5727       ;; Check whether auto-expire is to be done in this group.
5728       (setq gnus-newsgroup-auto-expire
5729             (and (gnus-group-auto-expirable-p group)
5730                  (not (gnus-group-read-only-p group))))
5731       ;; Set up the article buffer now, if necessary.
5732       (unless (and gnus-single-article-buffer
5733                    (equal gnus-article-buffer "*Article*"))
5734         (gnus-article-setup-buffer))
5735       ;; First and last article in this newsgroup.
5736       (when gnus-newsgroup-headers
5737         (setq gnus-newsgroup-begin
5738               (mail-header-number (car gnus-newsgroup-headers))
5739               gnus-newsgroup-end
5740               (mail-header-number
5741                (gnus-last-element gnus-newsgroup-headers))))
5742       ;; GROUP is successfully selected.
5743       (or gnus-newsgroup-headers t)))))
5744
5745 (defun gnus-compute-unseen-list ()
5746   ;; The `seen' marks are treated specially.
5747   (if (not gnus-newsgroup-seen)
5748       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5749     (setq gnus-newsgroup-unseen
5750           (gnus-inverse-list-range-intersection
5751            gnus-newsgroup-articles gnus-newsgroup-seen))))
5752
5753 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5754
5755 (defun gnus-summary-display-make-predicate (display)
5756   (require 'gnus-agent)
5757   (when (= (length display) 1)
5758     (setq display (car display)))
5759   (unless gnus-summary-display-cache
5760     (dolist (elem (append '((unread . unread)
5761                             (read . read)
5762                             (unseen . unseen))
5763                           gnus-article-mark-lists))
5764       (push (cons (cdr elem)
5765                   (gnus-byte-compile    ;Why bother?
5766                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5767             gnus-summary-display-cache)))
5768   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5769         (gnus-category-predicate-cache gnus-summary-display-cache))
5770     (gnus-get-predicate display)))
5771
5772 ;; Uses the dynamically bound `gnus-number' variable.
5773 (defvar gnus-number)
5774 (defun gnus-article-marked-p (type &optional article)
5775   (let ((article (or article gnus-number)))
5776     (cond
5777      ((eq type 'tick)
5778       (memq article gnus-newsgroup-marked))
5779      ((eq type 'spam)
5780       (memq article gnus-newsgroup-spam-marked))
5781      ((eq type 'unsend)
5782       (memq article gnus-newsgroup-unsendable))
5783      ((eq type 'undownload)
5784       (memq article gnus-newsgroup-undownloaded))
5785      ((eq type 'download)
5786       (memq article gnus-newsgroup-downloadable))
5787      ((eq type 'unread)
5788       (memq article gnus-newsgroup-unreads))
5789      ((eq type 'read)
5790       (memq article gnus-newsgroup-reads))
5791      ((eq type 'dormant)
5792       (memq article gnus-newsgroup-dormant) )
5793      ((eq type 'expire)
5794       (memq article gnus-newsgroup-expirable))
5795      ((eq type 'reply)
5796       (memq article gnus-newsgroup-replied))
5797      ((eq type 'killed)
5798       (memq article gnus-newsgroup-killed))
5799      ((eq type 'bookmark)
5800       (assq article gnus-newsgroup-bookmarks))
5801      ((eq type 'score)
5802       (assq article gnus-newsgroup-scored))
5803      ((eq type 'save)
5804       (memq article gnus-newsgroup-saved))
5805      ((eq type 'cache)
5806       (memq article gnus-newsgroup-cached))
5807      ((eq type 'forward)
5808       (memq article gnus-newsgroup-forwarded))
5809      ((eq type 'seen)
5810       (not (memq article gnus-newsgroup-unseen)))
5811      (t t))))
5812
5813 (defun gnus-articles-to-read (group &optional read-all)
5814   "Find out what articles the user wants to read."
5815   (let* ((only-read-p t)
5816          (articles
5817           (gnus-list-range-difference
5818           ;; Select all articles if `read-all' is non-nil, or if there
5819           ;; are no unread articles.
5820           (if (or read-all
5821                   (and (zerop (length gnus-newsgroup-marked))
5822                        (zerop (length gnus-newsgroup-unreads)))
5823                   ;; Fetch all if the predicate is non-nil.
5824                   gnus-newsgroup-display)
5825               ;; We want to select the headers for all the articles in
5826               ;; the group, so we select either all the active
5827               ;; articles in the group, or (if that's nil), the
5828               ;; articles in the cache.
5829               (or
5830                (if gnus-newsgroup-maximum-articles
5831                    (let ((active (gnus-active group)))
5832                      (gnus-uncompress-range
5833                       (cons (max (car active)
5834                                  (- (cdr active)
5835                                     gnus-newsgroup-maximum-articles
5836                                     -1))
5837                             (cdr active))))
5838                  (gnus-uncompress-range (gnus-active group)))
5839                (gnus-cache-articles-in-group group))
5840             ;; Select only the "normal" subset of articles.
5841             (setq only-read-p nil)
5842             (gnus-sorted-nunion
5843              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5844              gnus-newsgroup-unreads))
5845           (cdr (assq 'unexist (gnus-info-marks (gnus-get-info group))))))
5846          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5847          (scored (length scored-list))
5848          (number (length articles))
5849          (marked (+ (length gnus-newsgroup-marked)
5850                     (length gnus-newsgroup-dormant)))
5851          (select
5852           (cond
5853            ((numberp read-all)
5854             read-all)
5855            ((numberp gnus-newsgroup-display)
5856             gnus-newsgroup-display)
5857            (t
5858             (condition-case ()
5859                 (cond
5860                  ((and (or (<= scored marked) (= scored number))
5861                        (numberp gnus-large-newsgroup)
5862                        (> number gnus-large-newsgroup))
5863                   (let* ((cursor-in-echo-area nil)
5864                          (initial (gnus-parameter-large-newsgroup-initial
5865                                    gnus-newsgroup-name))
5866                          (default (if only-read-p
5867                                       (or initial gnus-large-newsgroup)
5868                                     number))
5869                          (input
5870                           (read-string
5871                            (if only-read-p
5872                                (format
5873                                 "How many articles from %s (available %d, default %d): "
5874                                 (gnus-group-decoded-name
5875                                  (gnus-group-real-name gnus-newsgroup-name))
5876                                 number default)
5877                              (format
5878                               "How many articles from %s (%d default): "
5879                               (gnus-group-decoded-name
5880                                (gnus-group-real-name gnus-newsgroup-name))
5881                               default))
5882                            nil
5883                            nil
5884                            (number-to-string default))))
5885                     (if (string-match "^[ \t]*$" input) number input)))
5886                  ((and (> scored marked) (< scored number)
5887                        (> (- scored number) 20))
5888                   (let ((input
5889                          (read-string
5890                           (format "%s %s (%d scored, %d total): "
5891                                   "How many articles from"
5892                                   (gnus-group-decoded-name
5893                                    (gnus-group-real-name gnus-newsgroup-name))
5894                                   scored number))))
5895                     (if (string-match "^[ \t]*$" input)
5896                         number input)))
5897                  (t number))
5898               (quit
5899                (message "Quit getting the articles to read")
5900                nil))))))
5901     (setq select (if (stringp select) (string-to-number select) select))
5902     (if (or (null select) (zerop select))
5903         select
5904       (if (and (not (zerop scored)) (<= (abs select) scored))
5905           (progn
5906             (setq articles (sort scored-list '<))
5907             (setq number (length articles)))
5908         (setq articles (copy-sequence articles)))
5909
5910       (when (< (abs select) number)
5911         (if (< select 0)
5912             ;; Select the N oldest articles.
5913             (setcdr (nthcdr (1- (abs select)) articles) nil)
5914           ;; Select the N most recent articles.
5915           (setq articles (nthcdr (- number select) articles))))
5916       (setq gnus-newsgroup-unselected
5917             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5918       (when gnus-alter-articles-to-read-function
5919         (setq articles
5920               (sort
5921                (funcall gnus-alter-articles-to-read-function
5922                         gnus-newsgroup-name articles)
5923                '<)))
5924       articles)))
5925
5926 (defun gnus-killed-articles (killed articles)
5927   (let (out)
5928     (while articles
5929       (when (inline (gnus-member-of-range (car articles) killed))
5930         (push (car articles) out))
5931       (setq articles (cdr articles)))
5932     out))
5933
5934 (defun gnus-uncompress-marks (marks)
5935   "Uncompress the mark ranges in MARKS."
5936   (let ((uncompressed '(score bookmark))
5937         out)
5938     (while marks
5939       (if (memq (caar marks) uncompressed)
5940           (push (car marks) out)
5941         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5942       (setq marks (cdr marks)))
5943     out))
5944
5945 (defun gnus-article-mark-to-type (mark)
5946   "Return the type of MARK."
5947   (or (cadr (assq mark gnus-article-special-mark-lists))
5948       'list))
5949
5950 (defun gnus-article-unpropagatable-p (mark)
5951   "Return whether MARK should be propagated to back end."
5952   (memq mark gnus-article-unpropagated-mark-lists))
5953
5954 (defun gnus-adjust-marked-articles (info)
5955   "Set all article lists and remove all marks that are no longer valid."
5956   (let* ((marked-lists (gnus-info-marks info))
5957          (active (gnus-active (gnus-info-group info)))
5958          (min (car active))
5959          (max (cdr active))
5960          (types gnus-article-mark-lists)
5961          marks var articles article mark mark-type
5962          bgn end)
5963     ;; Hack to avoid adjusting marks for imap.
5964     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5965               'nnimap)
5966       (setq min 1))
5967
5968     (dolist (marks marked-lists)
5969       (setq mark (car marks)
5970             mark-type (gnus-article-mark-to-type mark)
5971             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5972       ;; We set the variable according to the type of the marks list,
5973       ;; and then adjust the marks to a subset of the active articles.
5974       (cond
5975        ;; Adjust "simple" lists - compressed yet unsorted
5976        ((eq mark-type 'list)
5977         ;; Simultaneously uncompress and clip to active range
5978         ;; See gnus-uncompress-range for a description of possible marks
5979         (let (l lh)
5980           (if (not (cadr marks))
5981               (set var nil)
5982             (setq articles (if (numberp (cddr marks))
5983                                (list (cdr marks))
5984                              (cdr marks))
5985                   lh (cons nil nil)
5986                   l lh)
5987
5988             (while (setq article (pop articles))
5989               (cond ((consp article)
5990                      (setq bgn (max (car article) min)
5991                            end (min (cdr article) max))
5992                      (while (<= bgn end)
5993                        (setq l (setcdr l (cons bgn nil))
5994                              bgn (1+ bgn))))
5995                     ((and (<= min article)
5996                           (>= max article))
5997                      (setq l (setcdr l (cons article nil))))))
5998             (set var (cdr lh)))))
5999        ;; Adjust assocs.
6000        ((eq mark-type 'tuple)
6001         (set var (setq articles (cdr marks)))
6002         (when (not (listp (cdr (symbol-value var))))
6003           (set var (list (symbol-value var))))
6004         (when (not (listp (cdr articles)))
6005           (setq articles (list articles)))
6006         (while articles
6007           (when (or (not (consp (setq article (pop articles))))
6008                     (< (car article) min)
6009                     (> (car article) max))
6010             (set var (delq article (symbol-value var))))))
6011        ;; Adjust ranges (sloppily).
6012        ((eq mark-type 'range)
6013         (cond
6014          ((eq mark 'seen)
6015           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
6016           ;; It should be (seen (NUM1 . NUM2)).
6017           (when (numberp (cddr marks))
6018             (setcdr marks (list (cdr marks))))
6019           (setq articles (cdr marks))
6020           (while (and articles
6021                       (or (and (consp (car articles))
6022                                (> min (cdar articles)))
6023                           (and (numberp (car articles))
6024                                (> min (car articles)))))
6025             (pop articles))
6026           (set var articles))
6027          ((eq mark 'unexist)
6028           (set var (cdr marks)))))))))
6029
6030 (defun gnus-update-missing-marks (missing)
6031   "Go through the list of MISSING articles and remove them from the mark lists."
6032   (when missing
6033     (let (var m)
6034       ;; Go through all types.
6035       (dolist (elem gnus-article-mark-lists)
6036         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6037           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6038           (when (symbol-value var)
6039             ;; This list has articles.  So we delete all missing
6040             ;; articles from it.
6041             (setq m missing)
6042             (while m
6043               (set var (delq (pop m) (symbol-value var))))))))))
6044
6045 (defun gnus-update-marks ()
6046   "Enter the various lists of marked articles into the newsgroup info list."
6047   (let ((types gnus-article-mark-lists)
6048         (info (gnus-get-info gnus-newsgroup-name))
6049         type list newmarked symbol delta-marks)
6050     (when info
6051       ;; Add all marks lists to the list of marks lists.
6052       (while (setq type (pop types))
6053         (setq list (symbol-value
6054                     (setq symbol
6055                           (intern (format "gnus-newsgroup-%s" (car type))))))
6056
6057         (when list
6058           ;; Get rid of the entries of the articles that have the
6059           ;; default score.
6060           (when (and (eq (cdr type) 'score)
6061                      gnus-save-score
6062                      list)
6063             (let* ((arts list)
6064                    (prev (cons nil list))
6065                    (all prev))
6066               (while arts
6067                 (if (or (not (consp (car arts)))
6068                         (= (cdar arts) gnus-summary-default-score))
6069                     (setcdr prev (cdr arts))
6070                   (setq prev arts))
6071                 (setq arts (cdr arts)))
6072               (setq list (cdr all)))))
6073
6074         (when (eq (cdr type) 'seen)
6075           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6076
6077         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6078           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6079
6080         (when (and (gnus-check-backend-function
6081                     'request-set-mark gnus-newsgroup-name)
6082                    (not (gnus-article-unpropagatable-p (cdr type))))
6083           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6084                  ;; Don't do anything about marks for articles we
6085                  ;; didn't actually get any headers for.
6086                  (del
6087                   (gnus-list-range-intersection
6088                    gnus-newsgroup-articles
6089                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6090                  (add
6091                   (gnus-list-range-intersection
6092                    gnus-newsgroup-articles
6093                    (gnus-remove-from-range
6094                     (gnus-copy-sequence list) old))))
6095             (when add
6096               (push (list add 'add (list (cdr type))) delta-marks))
6097             (when del
6098               ;; Don't delete marks from outside the active range.
6099               ;; This shouldn't happen, but is a sanity check.
6100               (setq del (gnus-sorted-range-intersection
6101                          (gnus-active gnus-newsgroup-name) del))
6102               (push (list del 'del (list (cdr type))) delta-marks))))
6103
6104         (when list
6105           (push (cons (cdr type) list) newmarked)))
6106
6107       (when delta-marks
6108         (unless (gnus-check-group gnus-newsgroup-name)
6109           (error "Can't open server for %s" gnus-newsgroup-name))
6110         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6111
6112       ;; Enter these new marks into the info of the group.
6113       (if (nthcdr 3 info)
6114           (setcar (nthcdr 3 info) newmarked)
6115         ;; Add the marks lists to the end of the info.
6116         (when newmarked
6117           (setcdr (nthcdr 2 info) (list newmarked))))
6118
6119       ;; Cut off the end of the info if there's nothing else there.
6120       (let ((i 5))
6121         (while (and (> i 2)
6122                     (not (nth i info)))
6123           (when (nthcdr (decf i) info)
6124             (setcdr (nthcdr i info) nil)))))))
6125
6126 (defun gnus-set-mode-line (where)
6127   "Set the mode line of the article or summary buffers.
6128 If WHERE is `summary', the summary mode line format will be used."
6129   ;; Is this mode line one we keep updated?
6130   (when (and (memq where gnus-updated-mode-lines)
6131              (symbol-value
6132               (intern (format "gnus-%s-mode-line-format-spec" where))))
6133     (let (mode-string)
6134       ;; We evaluate this in the summary buffer since these
6135       ;; variables are buffer-local to that buffer.
6136       (with-current-buffer gnus-summary-buffer
6137         ;; We bind all these variables that are used in the `eval' form
6138         ;; below.
6139         (let* ((mformat (symbol-value
6140                          (intern
6141                           (format "gnus-%s-mode-line-format-spec" where))))
6142                (gnus-tmp-group-name (gnus-mode-string-quote
6143                                      (gnus-group-decoded-name
6144                                       gnus-newsgroup-name)))
6145                (gnus-tmp-article-number (or gnus-current-article 0))
6146                (gnus-tmp-unread gnus-newsgroup-unreads)
6147                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6148                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6149                (gnus-tmp-unread-and-unselected
6150                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6151                             (zerop gnus-tmp-unselected))
6152                        "")
6153                       ((zerop gnus-tmp-unselected)
6154                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6155                       (t (format "{%d(+%d) more}"
6156                                  gnus-tmp-unread-and-unticked
6157                                  gnus-tmp-unselected))))
6158                (gnus-tmp-subject
6159                 (if (and gnus-current-headers
6160                          (vectorp gnus-current-headers))
6161                     (gnus-mode-string-quote
6162                      (mail-header-subject gnus-current-headers))
6163                   ""))
6164                bufname-length max-len
6165                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6166           (setq mode-string (eval mformat))
6167           (setq bufname-length (if (string-match "%b" mode-string)
6168                                    (- (length
6169                                        (buffer-name
6170                                         (if (eq where 'summary)
6171                                             nil
6172                                           (get-buffer gnus-article-buffer))))
6173                                       2)
6174                                  0))
6175           (setq max-len (max 4 (if gnus-mode-non-string-length
6176                                    (- (window-width)
6177                                       gnus-mode-non-string-length
6178                                       bufname-length)
6179                                  (length mode-string))))
6180           ;; We might have to chop a bit of the string off...
6181           (when (> (length mode-string) max-len)
6182             (setq mode-string
6183                   (concat (truncate-string-to-width mode-string (- max-len 3))
6184                           "...")))))
6185       ;; Update the mode line.
6186       (setq mode-line-buffer-identification
6187             (gnus-mode-line-buffer-identification (list mode-string)))
6188       (set-buffer-modified-p t))))
6189
6190 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6191   "Go through the HEADERS list and add all Xrefs to a hash table.
6192 The resulting hash table is returned, or nil if no Xrefs were found."
6193   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6194          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6195          (xref-hashtb (gnus-make-hashtable))
6196          start group entry number xrefs header)
6197     (while headers
6198       (setq header (pop headers))
6199       (when (and (setq xrefs (mail-header-xref header))
6200                  (not (memq (setq number (mail-header-number header))
6201                             unreads)))
6202         (setq start 0)
6203         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6204           (setq start (match-end 0))
6205           (setq group (if prefix
6206                           (concat prefix (substring xrefs (match-beginning 1)
6207                                                     (match-end 1)))
6208                         (substring xrefs (match-beginning 1) (match-end 1))))
6209           (setq number
6210                 (string-to-number (substring xrefs (match-beginning 2)
6211                                           (match-end 2))))
6212           (if (setq entry (gnus-gethash group xref-hashtb))
6213               (setcdr entry (cons number (cdr entry)))
6214             (gnus-sethash group (cons number nil) xref-hashtb)))))
6215     (and start xref-hashtb)))
6216
6217 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6218   "Look through all the headers and mark the Xrefs as read."
6219   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6220         name info xref-hashtb idlist method nth4)
6221     (with-current-buffer gnus-group-buffer
6222       (when (setq xref-hashtb
6223                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6224         (mapatoms
6225          (lambda (group)
6226            (unless (string= from-newsgroup (setq name (symbol-name group)))
6227              (setq idlist (symbol-value group))
6228              ;; Dead groups are not updated.
6229              (and (prog1
6230                       (setq info (gnus-get-info name))
6231                     (when (stringp (setq nth4 (gnus-info-method info)))
6232                       (setq nth4 (gnus-server-to-method nth4))))
6233                   ;; Only do the xrefs if the group has the same
6234                   ;; select method as the group we have just read.
6235                   (or (gnus-methods-equal-p
6236                        nth4 (gnus-find-method-for-group from-newsgroup))
6237                       virtual
6238                       (equal nth4 (setq method (gnus-find-method-for-group
6239                                                 from-newsgroup)))
6240                       (and (equal (car nth4) (car method))
6241                            (equal (nth 1 nth4) (nth 1 method))))
6242                   gnus-use-cross-reference
6243                   (or (not (eq gnus-use-cross-reference t))
6244                       virtual
6245                       ;; Only do cross-references on subscribed
6246                       ;; groups, if that is what is wanted.
6247                       (<= (gnus-info-level info) gnus-level-subscribed))
6248                   (gnus-group-make-articles-read name idlist))))
6249          xref-hashtb)))))
6250
6251 (defun gnus-compute-read-articles (group articles)
6252   (let* ((entry (gnus-group-entry group))
6253          (info (nth 2 entry))
6254          (active (gnus-active group))
6255          ninfo)
6256     (when entry
6257       ;; First peel off all invalid article numbers.
6258       (when active
6259         (let ((ids articles)
6260               id first)
6261           (while (setq id (pop ids))
6262             (when (and first (> id (cdr active)))
6263               ;; We'll end up in this situation in one particular
6264               ;; obscure situation.  If you re-scan a group and get
6265               ;; a new article that is cross-posted to a different
6266               ;; group that has not been re-scanned, you might get
6267               ;; crossposted article that has a higher number than
6268               ;; Gnus believes possible.  So we re-activate this
6269               ;; group as well.  This might mean doing the
6270               ;; crossposting thingy will *increase* the number
6271               ;; of articles in some groups.  Tsk, tsk.
6272               (setq active (or (gnus-activate-group group) active)))
6273             (when (or (> id (cdr active))
6274                       (< id (car active)))
6275               (setq articles (delq id articles))))))
6276       ;; If the read list is nil, we init it.
6277       (if (and active
6278                (null (gnus-info-read info))
6279                (> (car active) 1))
6280           (setq ninfo (cons 1 (1- (car active))))
6281         (setq ninfo (gnus-info-read info)))
6282       ;; Then we add the read articles to the range.
6283       (gnus-add-to-range
6284        ninfo (setq articles (sort articles '<))))))
6285
6286 (defun gnus-group-make-articles-read (group articles)
6287   "Update the info of GROUP to say that ARTICLES are read."
6288   (let* ((num 0)
6289          (entry (gnus-group-entry group))
6290          (info (nth 2 entry))
6291          (active (gnus-active group))
6292          (set-marks
6293           (gnus-method-option-p
6294            (gnus-find-method-for-group group)
6295            'server-marks))
6296          range)
6297     (if (not entry)
6298         ;; Group that Gnus doesn't know exists, but still allow the
6299         ;; backend to set marks.
6300         (when set-marks
6301           (gnus-request-set-mark
6302            group (list (list (gnus-compress-sequence (sort articles #'<))
6303                              'add '(read)))))
6304       ;; Normal, subscribed groups.
6305       (setq range (gnus-compute-read-articles group articles))
6306       (with-current-buffer gnus-group-buffer
6307         (gnus-undo-register
6308           `(progn
6309              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6310              (gnus-info-set-read ',info ',(gnus-info-read info))
6311              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6312              (when ,set-marks
6313                (gnus-request-set-mark
6314                 ,group (list (list ',range 'del '(read)))))
6315              (gnus-group-update-group ,group t))))
6316       ;; Add the read articles to the range.
6317       (gnus-info-set-read info range)
6318       (when set-marks
6319         (gnus-request-set-mark group (list (list range 'add '(read)))))
6320       ;; Then we have to re-compute how many unread
6321       ;; articles there are in this group.
6322       (when active
6323         (cond
6324          ((not range)
6325           (setq num (- (1+ (cdr active)) (car active))))
6326          ((not (listp (cdr range)))
6327           (setq num (- (cdr active) (- (1+ (cdr range))
6328                                        (car range)))))
6329          (t
6330           (while range
6331             (if (numberp (car range))
6332                 (setq num (1+ num))
6333               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6334             (setq range (cdr range)))
6335           (setq num (- (cdr active) num))))
6336         ;; Update the number of unread articles.
6337         (setcar entry num)
6338         ;; Update the group buffer.
6339         (unless (gnus-ephemeral-group-p group)
6340           (gnus-group-update-group group t))))))
6341
6342 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6343   (let ((cur nntp-server-buffer)
6344         (dependencies
6345          (or dependencies
6346              (with-current-buffer gnus-summary-buffer
6347                gnus-newsgroup-dependencies)))
6348         headers id end ref number
6349         (mail-parse-charset gnus-newsgroup-charset)
6350         (mail-parse-ignored-charsets
6351          (save-current-buffer (condition-case nil
6352                                   (set-buffer gnus-summary-buffer)
6353                                 (error))
6354                               gnus-newsgroup-ignored-charsets)))
6355     (with-current-buffer nntp-server-buffer
6356       ;; Translate all TAB characters into SPACE characters.
6357       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6358       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6359       (ietf-drums-unfold-fws)
6360       (gnus-run-hooks 'gnus-parse-headers-hook)
6361       (let ((case-fold-search t)
6362             in-reply-to header p lines chars)
6363         (goto-char (point-min))
6364         ;; Search to the beginning of the next header.  Error messages
6365         ;; do not begin with 2 or 3.
6366         (while (re-search-forward "^[23][0-9]+ " nil t)
6367           (setq id nil
6368                 ref nil)
6369           ;; This implementation of this function, with nine
6370           ;; search-forwards instead of the one re-search-forward and
6371           ;; a case (which basically was the old function) is actually
6372           ;; about twice as fast, even though it looks messier.  You
6373           ;; can't have everything, I guess.  Speed and elegance
6374           ;; doesn't always go hand in hand.
6375           (setq
6376            header
6377            (vector
6378             ;; Number.
6379             (prog1
6380                 (setq number (read cur))
6381               (end-of-line)
6382               (setq p (point))
6383               (narrow-to-region (point)
6384                                 (or (and (search-forward "\n.\n" nil t)
6385                                          (- (point) 2))
6386                                     (point))))
6387             ;; Subject.
6388             (progn
6389               (goto-char p)
6390               (if (search-forward "\nsubject:" nil t)
6391                   (funcall gnus-decode-encoded-word-function
6392                            (nnheader-header-value))
6393                 "(none)"))
6394             ;; From.
6395             (progn
6396               (goto-char p)
6397               (if (search-forward "\nfrom:" nil t)
6398                   (funcall gnus-decode-encoded-address-function
6399                            (nnheader-header-value))
6400                 "(nobody)"))
6401             ;; Date.
6402             (progn
6403               (goto-char p)
6404               (if (search-forward "\ndate:" nil t)
6405                   (nnheader-header-value) ""))
6406             ;; Message-ID.
6407             (progn
6408               (goto-char p)
6409               (setq id (if (re-search-forward
6410                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6411                            ;; We do it this way to make sure the Message-ID
6412                            ;; is (somewhat) syntactically valid.
6413                            (buffer-substring (match-beginning 1)
6414                                              (match-end 1))
6415                          ;; If there was no message-id, we just fake one
6416                          ;; to make subsequent routines simpler.
6417                          (nnheader-generate-fake-message-id number))))
6418             ;; References.
6419             (progn
6420               (goto-char p)
6421               (if (search-forward "\nreferences:" nil t)
6422                   (progn
6423                     (setq end (point))
6424                     (prog1
6425                         (nnheader-header-value)
6426                       (setq ref
6427                             (buffer-substring
6428                              (progn
6429                                (end-of-line)
6430                                (search-backward ">" end t)
6431                                (1+ (point)))
6432                              (progn
6433                                (search-backward "<" end t)
6434                                (point))))))
6435                 ;; Get the references from the in-reply-to header if there
6436                 ;; were no references and the in-reply-to header looks
6437                 ;; promising.
6438                 (if (and (search-forward "\nin-reply-to:" nil t)
6439                          (setq in-reply-to (nnheader-header-value))
6440                          (string-match "<[^>]+>" in-reply-to))
6441                     (let (ref2)
6442                       (setq ref (substring in-reply-to (match-beginning 0)
6443                                            (match-end 0)))
6444                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6445                         (setq ref2 (substring in-reply-to (match-beginning 0)
6446                                               (match-end 0)))
6447                         (when (> (length ref2) (length ref))
6448                           (setq ref ref2)))
6449                       ref)
6450                   (setq ref nil))))
6451             ;; Chars.
6452             (progn
6453               (goto-char p)
6454               (if (search-forward "\nchars: " nil t)
6455                   (if (numberp (setq chars (ignore-errors (read cur))))
6456                       chars -1)
6457                 -1))
6458             ;; Lines.
6459             (progn
6460               (goto-char p)
6461               (if (search-forward "\nlines: " nil t)
6462                   (if (numberp (setq lines (ignore-errors (read cur))))
6463                       lines -1)
6464                 -1))
6465             ;; Xref.
6466             (progn
6467               (goto-char p)
6468               (and (search-forward "\nxref:" nil t)
6469                    (nnheader-header-value)))
6470             ;; Extra.
6471             (when gnus-extra-headers
6472               (let ((extra gnus-extra-headers)
6473                     out)
6474                 (while extra
6475                   (goto-char p)
6476                   (when (search-forward
6477                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6478                     (push (cons (car extra) (nnheader-header-value))
6479                           out))
6480                   (pop extra))
6481                 out))))
6482           (when (equal id ref)
6483             (setq ref nil))
6484
6485           (when gnus-alter-header-function
6486             (funcall gnus-alter-header-function header)
6487             (setq id (mail-header-id header)
6488                   ref (gnus-parent-id (mail-header-references header))))
6489
6490           (when (setq header
6491                       (gnus-dependencies-add-header
6492                        header dependencies force-new))
6493             (push header headers))
6494           (goto-char (point-max))
6495           (widen))
6496         (nreverse headers)))))
6497
6498 ;; Goes through the xover lines and returns a list of vectors
6499 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6500                                                   force-new dependencies
6501                                                   group also-fetch-heads)
6502   "Parse the news overview data in the server buffer.
6503 Return a list of headers that match SEQUENCE (see
6504 `nntp-retrieve-headers')."
6505   ;; Get the Xref when the users reads the articles since most/some
6506   ;; NNTP servers do not include Xrefs when using XOVER.
6507   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6508   (let ((mail-parse-charset gnus-newsgroup-charset)
6509         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6510         (cur nntp-server-buffer)
6511         (dependencies (or dependencies gnus-newsgroup-dependencies))
6512         (allp (cond
6513                ((eq gnus-read-all-available-headers t)
6514                 t)
6515                ((and (stringp gnus-read-all-available-headers)
6516                      group)
6517                 (string-match gnus-read-all-available-headers group))
6518                (t
6519                 nil)))
6520         number headers header)
6521     (with-current-buffer nntp-server-buffer
6522       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6523       ;; Allow the user to mangle the headers before parsing them.
6524       (gnus-run-hooks 'gnus-parse-headers-hook)
6525       (goto-char (point-min))
6526       (gnus-parse-without-error
6527         (while (and (or sequence allp)
6528                     (not (eobp)))
6529           (setq number (read cur))
6530           (when (not allp)
6531             (while (and sequence
6532                         (< (car sequence) number))
6533               (setq sequence (cdr sequence))))
6534           (when (and (or allp
6535                          (and sequence
6536                               (eq number (car sequence))))
6537                      (progn
6538                        (setq sequence (cdr sequence))
6539                        (setq header (inline
6540                                       (gnus-nov-parse-line
6541                                        number dependencies force-new)))))
6542             (push header headers))
6543           (forward-line 1)))
6544       ;; A common bug in inn is that if you have posted an article and
6545       ;; then retrieves the active file, it will answer correctly --
6546       ;; the new article is included.  However, a NOV entry for the
6547       ;; article may not have been generated yet, so this may fail.
6548       ;; We work around this problem by retrieving the last few
6549       ;; headers using HEAD.
6550       (if (or (not also-fetch-heads)
6551               (not sequence))
6552           ;; We (probably) got all the headers.
6553           (nreverse headers)
6554         (let ((gnus-nov-is-evil t))
6555           (nconc
6556            (nreverse headers)
6557            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6558              (gnus-get-newsgroup-headers))))))))
6559
6560 (defun gnus-article-get-xrefs ()
6561   "Fill in the Xref value in `gnus-current-headers', if necessary.
6562 This is meant to be called in `gnus-article-internal-prepare-hook'."
6563   (let ((headers (with-current-buffer gnus-summary-buffer
6564                    gnus-current-headers)))
6565     (or (not gnus-use-cross-reference)
6566         (not headers)
6567         (and (mail-header-xref headers)
6568              (not (string= (mail-header-xref headers) "")))
6569         (let ((case-fold-search t)
6570               xref)
6571           (save-restriction
6572             (nnheader-narrow-to-headers)
6573             (goto-char (point-min))
6574             (when (or (and (not (eobp))
6575                            (eq (downcase (char-after)) ?x)
6576                            (looking-at "Xref:"))
6577                       (search-forward "\nXref:" nil t))
6578               (goto-char (1+ (match-end 0)))
6579               (setq xref (buffer-substring (point) (point-at-eol)))
6580               (mail-header-set-xref headers xref)))))))
6581
6582 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6583   "Find article ID and insert the summary line for that article.
6584 OLD-HEADER can either be a header or a line number to insert
6585 the subject line on.
6586 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6587 and OLD-HEADER will be used when the summary line is inserted,
6588 too, instead of trying to fetch new headers."
6589   (let* ((line (and (numberp old-header) old-header))
6590          (old-header (and (vectorp old-header) old-header))
6591          (header (cond ((and old-header use-old-header)
6592                         old-header)
6593                        ((and (numberp id)
6594                              (gnus-number-to-header id))
6595                         (gnus-number-to-header id))
6596                        (t
6597                         (gnus-read-header id))))
6598          (number (and (numberp id) id))
6599          d)
6600     (when header
6601       ;; Rebuild the thread that this article is part of and go to the
6602       ;; article we have fetched.
6603       (when (and (not gnus-show-threads)
6604                  old-header)
6605         (when (and number
6606                    (setq d (gnus-data-find (mail-header-number old-header))))
6607           (goto-char (gnus-data-pos d))
6608           (gnus-data-remove
6609            number
6610            (- (point-at-bol)
6611               (prog1
6612                   (1+ (point-at-eol))
6613                 (gnus-delete-line))))))
6614       ;; Remove list identifiers from subject.
6615       (let ((gnus-newsgroup-headers (list header)))
6616         (gnus-summary-remove-list-identifiers))
6617       (when old-header
6618         (mail-header-set-number header (mail-header-number old-header)))
6619       (setq gnus-newsgroup-sparse
6620             (delq (setq number (mail-header-number header))
6621                   gnus-newsgroup-sparse))
6622       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6623       (push number gnus-newsgroup-limit)
6624       (gnus-rebuild-thread (mail-header-id header) line)
6625       (gnus-summary-goto-subject number nil t))
6626     (when (and (numberp number)
6627                (> number 0))
6628       ;; We have to update the boundaries even if we can't fetch the
6629       ;; article if ID is a number -- so that the next `P' or `N'
6630       ;; command will fetch the previous (or next) article even
6631       ;; if the one we tried to fetch this time has been canceled.
6632       (unless (and gnus-newsgroup-end (< number gnus-newsgroup-end))
6633         (setq gnus-newsgroup-end number))
6634       (unless (and gnus-newsgroup-begin (> number gnus-newsgroup-begin))
6635         (setq gnus-newsgroup-begin number))
6636       (setq gnus-newsgroup-unselected
6637             (delq number gnus-newsgroup-unselected)))
6638     ;; Report back a success?
6639     (and header (mail-header-number header))))
6640
6641 ;;; Process/prefix in the summary buffer
6642
6643 (defun gnus-summary-work-articles (n)
6644   "Return a list of articles to be worked upon.
6645 The prefix argument, the list of process marked articles, and the
6646 current article will be taken into consideration."
6647   (with-current-buffer gnus-summary-buffer
6648     (cond
6649      (n
6650       ;; A numerical prefix has been given.
6651       (setq n (prefix-numeric-value n))
6652       (let ((backward (< n 0))
6653             (n (abs (prefix-numeric-value n)))
6654             articles article)
6655         (save-excursion
6656           (while
6657               (and (> n 0)
6658                    (push (setq article (gnus-summary-article-number))
6659                          articles)
6660                    (if backward
6661                        (gnus-summary-find-prev nil article)
6662                      (gnus-summary-find-next nil article)))
6663             (decf n)))
6664         (nreverse articles)))
6665      ((and (gnus-region-active-p) (mark))
6666       (message "region active")
6667       ;; Work on the region between point and mark.
6668       (let ((max (max (point) (mark)))
6669             articles article)
6670         (save-excursion
6671           (goto-char (min (point) (mark)))
6672           (while
6673               (and
6674                (push (setq article (gnus-summary-article-number)) articles)
6675                (gnus-summary-find-next nil article)
6676                (< (point) max)))
6677           (nreverse articles))))
6678      (gnus-newsgroup-processable
6679       ;; There are process-marked articles present.
6680       ;; Save current state.
6681       (gnus-summary-save-process-mark)
6682       ;; Return the list.
6683       (reverse gnus-newsgroup-processable))
6684      (t
6685       ;; Just return the current article.
6686       (list (gnus-summary-article-number))))))
6687
6688 (defmacro gnus-summary-iterate (arg &rest forms)
6689   "Iterate over the process/prefixed articles and do FORMS.
6690 ARG is the interactive prefix given to the command.  FORMS will be
6691 executed with point over the summary line of the articles."
6692   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6693     `(let ((,articles (gnus-summary-work-articles ,arg)))
6694        (while ,articles
6695          (gnus-summary-goto-subject (car ,articles))
6696          ,@forms
6697          (pop ,articles)))))
6698
6699 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6700 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6701
6702 (defun gnus-summary-save-process-mark ()
6703   "Push the current set of process marked articles on the stack."
6704   (interactive)
6705   (push (copy-sequence gnus-newsgroup-processable)
6706         gnus-newsgroup-process-stack))
6707
6708 (defun gnus-summary-kill-process-mark ()
6709   "Push the current set of process marked articles on the stack and unmark."
6710   (interactive)
6711   (gnus-summary-save-process-mark)
6712   (gnus-summary-unmark-all-processable))
6713
6714 (defun gnus-summary-yank-process-mark ()
6715   "Pop the last process mark state off the stack and restore it."
6716   (interactive)
6717   (unless gnus-newsgroup-process-stack
6718     (error "Empty mark stack"))
6719   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6720
6721 (defun gnus-summary-process-mark-set (set)
6722   "Make SET into the current process marked articles."
6723   (gnus-summary-unmark-all-processable)
6724   (mapc 'gnus-summary-set-process-mark set))
6725
6726 ;;; Searching and stuff
6727
6728 (defun gnus-summary-search-group (&optional backward use-level)
6729   "Search for next unread newsgroup.
6730 If optional argument BACKWARD is non-nil, search backward instead."
6731   (with-current-buffer gnus-group-buffer
6732     (when (gnus-group-search-forward
6733            backward nil (if use-level (gnus-group-group-level) nil))
6734       (gnus-group-group-name))))
6735
6736 (defun gnus-summary-best-group (&optional exclude-group)
6737   "Find the name of the best unread group.
6738 If EXCLUDE-GROUP, do not go to this group."
6739   (with-current-buffer gnus-group-buffer
6740     (save-excursion
6741       (gnus-group-best-unread-group exclude-group))))
6742
6743 (defun gnus-summary-find-next (&optional unread article backward)
6744   (if backward
6745       (gnus-summary-find-prev unread article)
6746     (let* ((dummy (gnus-summary-article-intangible-p))
6747            (article (or article (gnus-summary-article-number)))
6748            (data (gnus-data-find-list article))
6749            result)
6750       (when (and (not dummy)
6751                  (or (not gnus-summary-check-current)
6752                      (not unread)
6753                      (not (gnus-data-unread-p (car data)))))
6754         (setq data (cdr data)))
6755       (when (setq result
6756                   (if unread
6757                       (progn
6758                         (while data
6759                           (unless (memq (gnus-data-number (car data))
6760                                         (cond
6761                                          ((eq gnus-auto-goto-ignores
6762                                               'always-undownloaded)
6763                                           gnus-newsgroup-undownloaded)
6764                                          (gnus-plugged
6765                                           nil)
6766                                          ((eq gnus-auto-goto-ignores
6767                                               'unfetched)
6768                                           gnus-newsgroup-unfetched)
6769                                          ((eq gnus-auto-goto-ignores
6770                                               'undownloaded)
6771                                           gnus-newsgroup-undownloaded)))
6772                             (when (gnus-data-unread-p (car data))
6773                               (setq result (car data)
6774                                     data nil)))
6775                           (setq data (cdr data)))
6776                         result)
6777                     (car data)))
6778         (goto-char (gnus-data-pos result))
6779         (gnus-data-number result)))))
6780
6781 (defun gnus-summary-find-prev (&optional unread article)
6782   (let* ((eobp (eobp))
6783          (article (or article (gnus-summary-article-number)))
6784          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6785          result)
6786     (when (and (not eobp)
6787                (or (not gnus-summary-check-current)
6788                    (not unread)
6789                    (not (gnus-data-unread-p (car data)))))
6790       (setq data (cdr data)))
6791     (when (setq result
6792                 (if unread
6793                     (progn
6794                       (while data
6795                         (unless (memq (gnus-data-number (car data))
6796                                       (cond
6797                                        ((eq gnus-auto-goto-ignores
6798                                             'always-undownloaded)
6799                                         gnus-newsgroup-undownloaded)
6800                                        (gnus-plugged
6801                                         nil)
6802                                        ((eq gnus-auto-goto-ignores
6803                                             'unfetched)
6804                                         gnus-newsgroup-unfetched)
6805                                        ((eq gnus-auto-goto-ignores
6806                                             'undownloaded)
6807                                         gnus-newsgroup-undownloaded)))
6808                           (when (gnus-data-unread-p (car data))
6809                             (setq result (car data)
6810                                   data nil)))
6811                         (setq data (cdr data)))
6812                       result)
6813                   (car data)))
6814       (goto-char (gnus-data-pos result))
6815       (gnus-data-number result))))
6816
6817 (defun gnus-summary-find-subject (subject &optional unread backward article)
6818   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6819          (article (or article (gnus-summary-article-number)))
6820          (articles (gnus-data-list backward))
6821          (arts (gnus-data-find-list article articles))
6822          result)
6823     (when (or (not gnus-summary-check-current)
6824               (not unread)
6825               (not (gnus-data-unread-p (car arts))))
6826       (setq arts (cdr arts)))
6827     (while arts
6828       (and (or (not unread)
6829                (gnus-data-unread-p (car arts)))
6830            (vectorp (gnus-data-header (car arts)))
6831            (gnus-subject-equal
6832             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6833            (setq result (car arts)
6834                  arts nil))
6835       (setq arts (cdr arts)))
6836     (and result
6837          (goto-char (gnus-data-pos result))
6838          (gnus-data-number result))))
6839
6840 (defun gnus-summary-search-forward (&optional unread subject backward)
6841   "Search forward for an article.
6842 If UNREAD, look for unread articles.  If SUBJECT, look for
6843 articles with that subject.  If BACKWARD, search backward instead."
6844   (cond (subject (gnus-summary-find-subject subject unread backward))
6845         (backward (gnus-summary-find-prev unread))
6846         (t (gnus-summary-find-next unread))))
6847
6848 (defun gnus-recenter (&optional n)
6849   "Center point in window and redisplay frame.
6850 Also do horizontal recentering."
6851   (interactive "P")
6852   (when (and gnus-auto-center-summary
6853              (not (eq gnus-auto-center-summary 'vertical)))
6854     (gnus-horizontal-recenter))
6855   (if (fboundp 'recenter-top-bottom)
6856       (recenter-top-bottom n)
6857     (recenter n)))
6858
6859 (put 'gnus-recenter 'isearch-scroll t)
6860
6861 (defun gnus-forward-line-ignore-invisible (n)
6862   "Move N lines forward (backward if N is negative).
6863 Like forward-line, but skip over (and don't count) invisible lines."
6864   (let (done)
6865     (while (and (> n 0) (not done))
6866       ;; If the following character is currently invisible,
6867       ;; skip all characters with that same `invisible' property value.
6868       (while (gnus-invisible-p (point))
6869         (goto-char (gnus-next-char-property-change (point))))
6870       (forward-line 1)
6871       (if (eobp)
6872           (setq done t)
6873         (setq n (1- n))))
6874     (while (and (< n 0) (not done))
6875       (forward-line -1)
6876       (if (bobp) (setq done t)
6877         (setq n (1+ n))
6878         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6879           (goto-char (gnus-previous-char-property-change (point))))))))
6880
6881 (defun gnus-summary-recenter ()
6882   "Center point in the summary window.
6883 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6884 displayed, no centering will be performed."
6885   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6886   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6887   (interactive)
6888   ;; The user has to want it.
6889   (when gnus-auto-center-summary
6890     (let* ((top (cond ((< (window-height) 4) 0)
6891                       ((< (window-height) 7) 1)
6892                       (t (if (numberp gnus-auto-center-summary)
6893                              gnus-auto-center-summary
6894                            (/ (1- (window-height)) 2)))))
6895            (height (1- (window-height)))
6896            (bottom (save-excursion
6897                      (goto-char (point-max))
6898                      (gnus-forward-line-ignore-invisible (- height))
6899                      (point)))
6900            (window (get-buffer-window (current-buffer))))
6901       (when (get-buffer-window gnus-article-buffer)
6902         ;; Only do recentering when the article buffer is displayed,
6903         ;; Set the window start to either `bottom', which is the biggest
6904         ;; possible valid number, or the second line from the top,
6905         ;; whichever is the least.
6906         (let ((top-pos (save-excursion
6907                          (gnus-forward-line-ignore-invisible (- top))
6908                          (point))))
6909           (if (> bottom top-pos)
6910               ;; Keep the second line from the top visible
6911               (set-window-start window top-pos)
6912             ;; Try to keep the bottom line visible; if it's partially
6913             ;; obscured, either scroll one more line to make it fully
6914             ;; visible, or revert to using TOP-POS.
6915             (save-excursion
6916               (goto-char (point-max))
6917               (gnus-forward-line-ignore-invisible -1)
6918               (let ((last-line-start (point)))
6919                 (goto-char bottom)
6920                 (set-window-start window (point) t)
6921                 (when (not (pos-visible-in-window-p last-line-start window))
6922                   (gnus-forward-line-ignore-invisible 1)
6923                   (set-window-start window (min (point) top-pos) t)))))))
6924       ;; Do horizontal recentering while we're at it.
6925       (when (and (get-buffer-window (current-buffer) t)
6926                  (not (eq gnus-auto-center-summary 'vertical)))
6927         (let ((selected (selected-window)))
6928           (select-window (get-buffer-window (current-buffer) t))
6929           (gnus-summary-position-point)
6930           (gnus-horizontal-recenter)
6931           (select-window selected))))))
6932
6933 (defun gnus-summary-jump-to-group (newsgroup)
6934   "Move point to NEWSGROUP in group mode buffer."
6935   ;; Keep update point of group mode buffer if visible.
6936   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6937       (save-window-excursion
6938         ;; Take care of tree window mode.
6939         (when (get-buffer-window gnus-group-buffer)
6940           (pop-to-buffer gnus-group-buffer))
6941         (gnus-group-jump-to-group newsgroup))
6942     (save-excursion
6943       ;; Take care of tree window mode.
6944       (if (get-buffer-window gnus-group-buffer 0)
6945           (pop-to-buffer gnus-group-buffer)
6946         (set-buffer gnus-group-buffer))
6947       (gnus-group-jump-to-group newsgroup))))
6948
6949 ;; This function returns a list of article numbers based on the
6950 ;; difference between the ranges of read articles in this group and
6951 ;; the range of active articles.
6952 (defun gnus-list-of-unread-articles (group)
6953   (let* ((read (gnus-info-read (gnus-get-info group)))
6954          (active (or (gnus-active group) (gnus-activate-group group)))
6955          (last (or (cdr active)
6956                    (error "Group %s couldn't be activated " group)))
6957          (bottom (if gnus-newsgroup-maximum-articles
6958                      (max (car active)
6959                           (- last gnus-newsgroup-maximum-articles -1))
6960                    (car active)))
6961          first nlast unread)
6962     ;; If none are read, then all are unread.
6963     (if (not read)
6964         (setq first bottom)
6965       ;; If the range of read articles is a single range, then the
6966       ;; first unread article is the article after the last read
6967       ;; article.  Sounds logical, doesn't it?
6968       (if (and (not (listp (cdr read)))
6969                (or (< (car read) bottom)
6970                    (progn (setq read (list read))
6971                           nil)))
6972           (setq first (max bottom (1+ (cdr read))))
6973         ;; `read' is a list of ranges.
6974         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6975                                   (caar read)))
6976                   1)
6977           (setq first bottom))
6978         (while read
6979           (when first
6980             (while (< first nlast)
6981               (setq unread (cons first unread)
6982                     first (1+ first))))
6983           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6984           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6985           (setq read (cdr read)))))
6986     ;; And add the last unread articles.
6987     (while (<= first last)
6988       (setq unread (cons first unread)
6989             first (1+ first)))
6990     ;; Return the list of unread articles.
6991     (delq 0 (nreverse unread))))
6992
6993 (defun gnus-list-of-read-articles (group)
6994   "Return a list of unread, unticked and non-dormant articles."
6995   (let* ((info (gnus-get-info group))
6996          (marked (gnus-info-marks info))
6997          (active (gnus-active group)))
6998     (and info active
6999          (gnus-list-range-difference
7000           (gnus-list-range-difference
7001            (gnus-sorted-complement
7002             (gnus-uncompress-range
7003              (if gnus-newsgroup-maximum-articles
7004                  (cons (max (car active)
7005                             (- (cdr active)
7006                                gnus-newsgroup-maximum-articles
7007                                -1))
7008                        (cdr active))
7009                active))
7010             (gnus-list-of-unread-articles group))
7011            (cdr (assq 'dormant marked)))
7012           (cdr (assq 'tick marked))))))
7013
7014 ;; This function returns a sequence of article numbers based on the
7015 ;; difference between the ranges of read articles in this group and
7016 ;; the range of active articles.
7017 (defun gnus-sequence-of-unread-articles (group)
7018   (let* ((read (gnus-info-read (gnus-get-info group)))
7019          (active (or (gnus-active group) (gnus-activate-group group)))
7020          (last (cdr active))
7021          (bottom (if gnus-newsgroup-maximum-articles
7022                      (max (car active)
7023                           (- last gnus-newsgroup-maximum-articles -1))
7024                    (car active)))
7025          first nlast unread)
7026     ;; If none are read, then all are unread.
7027     (if (not read)
7028         (setq first bottom)
7029       ;; If the range of read articles is a single range, then the
7030       ;; first unread article is the article after the last read
7031       ;; article.  Sounds logical, doesn't it?
7032       (if (and (not (listp (cdr read)))
7033                (or (< (car read) bottom)
7034                    (progn (setq read (list read))
7035                           nil)))
7036           (setq first (max bottom (1+ (cdr read))))
7037         ;; `read' is a list of ranges.
7038         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7039                                   (caar read)))
7040                   1)
7041           (setq first bottom))
7042         (while read
7043           (when first
7044             (push (cons first nlast) unread))
7045           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7046           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7047           (setq read (cdr read)))))
7048     ;; And add the last unread articles.
7049     (cond ((not (and first last))
7050            nil)
7051           ((< first last)
7052            (push (cons first last) unread))
7053           ((= first last)
7054            (push first unread)))
7055     ;; Return the sequence of unread articles.
7056     (delq 0 (nreverse unread))))
7057
7058 ;; Various summary commands
7059
7060 (defun gnus-summary-select-article-buffer ()
7061   "Reconfigure windows to show the article buffer.
7062 If `gnus-widen-article-window' is set, show only the article
7063 buffer."
7064   (interactive)
7065   (if (not (gnus-buffer-live-p gnus-article-buffer))
7066       (error "There is no article buffer for this summary buffer")
7067     (unless (get-buffer-window gnus-article-buffer)
7068       (gnus-summary-show-article))
7069     (gnus-configure-windows
7070      (if gnus-widen-article-window
7071          'only-article
7072        'article)
7073      t)
7074     (select-window (get-buffer-window gnus-article-buffer))))
7075
7076 (defun gnus-summary-universal-argument (arg)
7077   "Perform any operation on all articles that are process/prefixed."
7078   (interactive "P")
7079   (let ((articles (gnus-summary-work-articles arg))
7080         func article)
7081     (if (eq
7082          (setq
7083           func
7084           (key-binding
7085            (read-key-sequence
7086             (substitute-command-keys
7087              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7088          'undefined)
7089         (gnus-error 1 "Undefined key")
7090       (save-excursion
7091         (while articles
7092           (gnus-summary-goto-subject (setq article (pop articles)))
7093           (let (gnus-newsgroup-processable)
7094             (command-execute func))
7095           (gnus-summary-remove-process-mark article)))))
7096   (gnus-summary-position-point))
7097
7098 (defun gnus-summary-toggle-truncation (&optional arg)
7099   "Toggle truncation of summary lines.
7100 With ARG, turn line truncation on if ARG is positive."
7101   (interactive "P")
7102   (setq truncate-lines
7103         (if (null arg) (not truncate-lines)
7104           (> (prefix-numeric-value arg) 0)))
7105   (redraw-display))
7106
7107 (defun gnus-summary-find-for-reselect ()
7108   "Return the number of an article to stay on across a reselect.
7109 The current article is considered, then following articles, then previous
7110 articles.  An article is sought which is not canceled and isn't a temporary
7111 insertion from another group.  If there's no such then return a dummy 0."
7112   (let (found)
7113     (dolist (rev '(nil t))
7114       (unless found      ; don't demand the reverse list if we don't need it
7115         (let ((data (gnus-data-find-list
7116                      (gnus-summary-article-number) (gnus-data-list rev))))
7117           (while (and data (not found))
7118             (if (and (< 0 (gnus-data-number (car data)))
7119                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7120                 (setq found (gnus-data-number (car data))))
7121             (setq data (cdr data))))))
7122     (or found 0)))
7123
7124 (defun gnus-summary-reselect-current-group (&optional all rescan)
7125   "Exit and then reselect the current newsgroup.
7126 The prefix argument ALL means to select all articles."
7127   (interactive "P")
7128   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7129     (error "Ephemeral groups can't be reselected"))
7130   (let ((current-subject (gnus-summary-find-for-reselect))
7131         (group gnus-newsgroup-name))
7132     (setq gnus-newsgroup-begin nil)
7133     (gnus-summary-exit nil 'leave-hidden)
7134     ;; We have to adjust the point of group mode buffer because
7135     ;; point was moved to the next unread newsgroup by exiting.
7136     (gnus-summary-jump-to-group group)
7137     (when rescan
7138       (save-excursion
7139         (gnus-group-get-new-news-this-group 1)))
7140     (gnus-group-read-group all t)
7141     (gnus-summary-goto-subject current-subject nil t)))
7142
7143 (defun gnus-summary-rescan-group (&optional all)
7144   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7145   (interactive "P")
7146   (let ((config gnus-current-window-configuration))
7147     (gnus-summary-reselect-current-group all t)
7148     (gnus-configure-windows config)
7149     (when (eq config 'article)
7150       (gnus-summary-select-article))))
7151
7152 (defun gnus-summary-update-info (&optional non-destructive)
7153   (save-excursion
7154     (let ((group gnus-newsgroup-name))
7155       (when group
7156         (when gnus-newsgroup-kill-headers
7157           (setq gnus-newsgroup-killed
7158                 (gnus-compress-sequence
7159                  (gnus-sorted-union
7160                   (gnus-list-range-intersection
7161                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7162                   gnus-newsgroup-unreads)
7163                  t)))
7164         (unless (listp (cdr gnus-newsgroup-killed))
7165           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7166         (let ((headers gnus-newsgroup-headers)
7167               (ephemeral-p (gnus-ephemeral-group-p group))
7168               info)
7169           (unless ephemeral-p
7170             (setq info (copy-sequence (gnus-get-info group))
7171                   info (delq (gnus-info-params info) info)))
7172           ;; Set the new ranges of read articles.
7173           (with-current-buffer gnus-group-buffer
7174             (gnus-undo-force-boundary))
7175           (gnus-update-read-articles
7176            group (gnus-sorted-union
7177                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7178           ;; Set the current article marks.
7179           (let ((gnus-newsgroup-scored
7180                  (if (and (not gnus-save-score)
7181                           (not non-destructive))
7182                      nil
7183                    gnus-newsgroup-scored)))
7184             (save-excursion
7185               (gnus-update-marks)))
7186           ;; Do the cross-ref thing.
7187           (when gnus-use-cross-reference
7188             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7189           ;; Do not switch windows but change the buffer to work.
7190           (set-buffer gnus-group-buffer)
7191           (unless ephemeral-p
7192             (gnus-group-update-group
7193              group nil
7194              (equal info
7195                     (setq info (copy-sequence (gnus-get-info group))
7196                           info (delq (gnus-info-params info) info))))))))))
7197
7198 (defun gnus-summary-save-newsrc (&optional force)
7199   "Save the current number of read/marked articles in the dribble buffer.
7200 The dribble buffer will then be saved.
7201 If FORCE (the prefix), also save the .newsrc file(s)."
7202   (interactive "P")
7203   (gnus-summary-update-info t)
7204   (if force
7205       (gnus-save-newsrc-file)
7206     (gnus-dribble-save)))
7207
7208 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7209
7210 (defun gnus-summary-exit (&optional temporary leave-hidden)
7211   "Exit reading current newsgroup, and then return to group selection mode.
7212 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7213   (interactive)
7214   (gnus-set-global-variables)
7215   (when (gnus-buffer-live-p gnus-article-buffer)
7216     (with-current-buffer gnus-article-buffer
7217       (mm-destroy-parts gnus-article-mime-handles)
7218       ;; Set it to nil for safety reason.
7219       (setq gnus-article-mime-handle-alist nil)
7220       (setq gnus-article-mime-handles nil)))
7221   (gnus-kill-save-kill-buffer)
7222   (gnus-async-halt-prefetch)
7223   (let* ((group gnus-newsgroup-name)
7224          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7225          (gnus-group-is-exiting-p t)
7226          (article-buffer gnus-article-buffer)
7227          (original-article-buffer gnus-original-article-buffer)
7228          (mode major-mode)
7229          (group-point nil)
7230          (buf (current-buffer))
7231          ;; `gnus-single-article-buffer' is nil buffer-locally in
7232          ;; ephemeral group of which summary buffer will be killed,
7233          ;; but the global value may be non-nil.
7234          (single-article-buffer gnus-single-article-buffer))
7235     (unless quit-config
7236       ;; Do adaptive scoring, and possibly save score files.
7237       (when gnus-newsgroup-adaptive
7238         (gnus-score-adaptive))
7239       (when gnus-use-scoring
7240         (gnus-score-save)))
7241     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7242     (when gnus-use-cache
7243       (gnus-cache-possibly-remove-articles)
7244       (gnus-cache-save-buffers))
7245     (gnus-async-prefetch-remove-group group)
7246     (when gnus-suppress-duplicates
7247       (gnus-dup-enter-articles))
7248     (when gnus-use-trees
7249       (gnus-tree-close group))
7250     (when gnus-use-cache
7251       (gnus-cache-write-active))
7252     ;; Remove entries for this group.
7253     (nnmail-purge-split-history (gnus-group-real-name group))
7254     ;; Make all changes in this group permanent.
7255     (unless quit-config
7256       (gnus-run-hooks 'gnus-exit-group-hook)
7257       (gnus-summary-update-info))
7258     (gnus-close-group group)
7259     ;; Make sure where we were, and go to next newsgroup.
7260     (when (buffer-live-p (get-buffer gnus-group-buffer))
7261       (set-buffer gnus-group-buffer))
7262     (unless quit-config
7263       (gnus-group-jump-to-group group))
7264     (gnus-run-hooks 'gnus-summary-exit-hook)
7265     (unless (or quit-config
7266                 (not gnus-summary-next-group-on-exit)
7267                 ;; If this group has disappeared from the summary
7268                 ;; buffer, don't skip forwards.
7269                 (not (string= group (gnus-group-group-name))))
7270       (gnus-group-next-unread-group 1))
7271     (setq group-point (point))
7272     (if temporary
7273         nil                             ;Nothing to do.
7274       (set-buffer buf)
7275       (if (not gnus-kill-summary-on-exit)
7276           (progn
7277             (gnus-deaden-summary)
7278             (setq mode nil))
7279         (when (get-buffer gnus-article-buffer)
7280           (bury-buffer gnus-article-buffer))
7281         ;; Return to group mode buffer.
7282         (when (eq mode 'gnus-summary-mode)
7283           (gnus-kill-buffer buf)))
7284
7285       (setq gnus-current-select-method gnus-select-method)
7286       (when (gnus-buffer-live-p gnus-group-buffer)
7287         (set-buffer gnus-group-buffer))
7288       (if quit-config
7289           (gnus-handle-ephemeral-exit quit-config)
7290         (goto-char group-point)
7291         ;; If gnus-group-buffer is already displayed, make sure we also move
7292         ;; the cursor in the window that displays it.
7293         (let ((win (get-buffer-window (current-buffer) 0)))
7294           (if win (set-window-point win (point))))
7295         (unless leave-hidden
7296           (gnus-configure-windows 'group 'force)))
7297
7298       ;; If we have several article buffers, we kill them at exit.
7299       (unless single-article-buffer
7300         (when (gnus-buffer-live-p article-buffer)
7301           (with-current-buffer article-buffer
7302             ;; Don't kill sticky article buffers
7303             (unless (eq major-mode 'gnus-sticky-article-mode)
7304               (gnus-kill-buffer article-buffer)
7305               (setq gnus-article-current nil))))
7306         (gnus-kill-buffer original-article-buffer))
7307
7308       ;; Clear the current group name.
7309       (unless quit-config
7310         (setq gnus-newsgroup-name nil)))))
7311
7312 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7313 (defun gnus-summary-exit-no-update (&optional no-questions)
7314   "Quit reading current newsgroup without updating read article info."
7315   (interactive)
7316   (let* ((group gnus-newsgroup-name)
7317          (gnus-group-is-exiting-p t)
7318          (gnus-group-is-exiting-without-update-p t)
7319          (quit-config (gnus-group-quit-config group)))
7320     (when (or no-questions
7321               gnus-expert-user
7322               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7323       (gnus-async-halt-prefetch)
7324       (run-hooks 'gnus-summary-prepare-exit-hook)
7325       (when (gnus-buffer-live-p gnus-article-buffer)
7326         (with-current-buffer gnus-article-buffer
7327           (gnus-article-stop-animations)
7328           (gnus-stop-downloads)
7329           (mm-destroy-parts gnus-article-mime-handles)
7330           ;; Set it to nil for safety reason.
7331           (setq gnus-article-mime-handle-alist nil)
7332           (setq gnus-article-mime-handles nil)))
7333       ;; If we have several article buffers, we kill them at exit.
7334       (unless gnus-single-article-buffer
7335         (gnus-kill-buffer gnus-article-buffer)
7336         (gnus-kill-buffer gnus-original-article-buffer)
7337         (setq gnus-article-current nil))
7338       ;; Return to the group buffer.
7339       (if (not gnus-kill-summary-on-exit)
7340           (progn
7341             (gnus-deaden-summary)
7342             (gnus-configure-windows 'group 'force))
7343         (gnus-configure-windows 'group 'force)
7344         (gnus-close-group group)
7345         (gnus-kill-buffer gnus-summary-buffer))
7346       (unless gnus-single-article-buffer
7347         (setq gnus-article-current nil))
7348       (when gnus-use-trees
7349         (gnus-tree-close group))
7350       (gnus-async-prefetch-remove-group group)
7351       (when (get-buffer gnus-article-buffer)
7352         (bury-buffer gnus-article-buffer))
7353       ;; Clear the current group name.
7354       (setq gnus-newsgroup-name nil)
7355       (unless (gnus-ephemeral-group-p group)
7356         (gnus-group-update-group group nil t))
7357       (when (equal (gnus-group-group-name) group)
7358         (gnus-group-next-unread-group 1))
7359       (when quit-config
7360         (gnus-handle-ephemeral-exit quit-config)))))
7361
7362 (defun gnus-handle-ephemeral-exit (quit-config)
7363   "Handle movement when leaving an ephemeral group.
7364 The state which existed when entering the ephemeral is reset."
7365   (if (not (buffer-live-p (car quit-config)))
7366       (when (gnus-buffer-live-p gnus-group-buffer)
7367         (gnus-configure-windows 'group 'force))
7368     (set-buffer (car quit-config))
7369     (unless (eq (cdr quit-config) 'group)
7370       (setq gnus-current-select-method
7371             (gnus-find-method-for-group gnus-newsgroup-name)))
7372     (cond ((eq major-mode 'gnus-summary-mode)
7373            (gnus-set-global-variables))
7374           ((eq major-mode 'gnus-article-mode)
7375            (save-current-buffer
7376              ;; The `gnus-summary-buffer' variable may point
7377              ;; to the old summary buffer when using a single
7378              ;; article buffer.
7379              (unless (gnus-buffer-live-p gnus-summary-buffer)
7380                (set-buffer gnus-group-buffer))
7381              (set-buffer gnus-summary-buffer)
7382              (gnus-set-global-variables))))
7383     (if (or (eq (cdr quit-config) 'article)
7384             (eq (cdr quit-config) 'pick))
7385         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7386             (gnus-configure-windows 'pick 'force)
7387           (gnus-configure-windows (cdr quit-config) 'force))
7388       (gnus-configure-windows (cdr quit-config) 'force))
7389     (when (eq major-mode 'gnus-summary-mode)
7390       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7391                                                      next-unread-noselect))
7392           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7393                                   'next-noselect)
7394                               (gnus-summary-next-subject 1 nil t))
7395                              ((eq gnus-auto-select-on-ephemeral-exit
7396                                   'next-unread-noselect)
7397                               (gnus-summary-next-subject 1 t t))))
7398             ;; Hide the article buffer which displays the article different
7399             ;; from the one that the cursor points to in the summary buffer.
7400             (gnus-configure-windows 'summary 'force))
7401         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7402                (gnus-summary-next-subject 1))
7403               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7404                (gnus-summary-next-subject 1 t))))
7405       (gnus-summary-recenter)
7406       (gnus-summary-position-point))))
7407
7408 ;;; Dead summaries.
7409
7410 (defvar gnus-dead-summary-mode-map
7411   (let ((map (make-keymap)))
7412     (suppress-keymap map)
7413     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7414     (dolist (key '("\C-d" "\r" "\177" [delete]))
7415       (define-key map key 'gnus-summary-wake-up-the-dead))
7416     (dolist (key '("q" "Q"))
7417       (define-key map key 'bury-buffer))
7418     map))
7419
7420 (define-minor-mode gnus-dead-summary-mode
7421   "Minor mode for Gnus summary buffers."
7422   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7423   (unless (derived-mode-p 'gnus-summary-mode)
7424     (setq gnus-dead-summary-mode nil)))
7425
7426 (defun gnus-deaden-summary ()
7427   "Make the current summary buffer into a dead summary buffer."
7428   ;; Kill any previous dead summary buffer.
7429   (when (and gnus-dead-summary
7430              (buffer-name gnus-dead-summary))
7431     (with-current-buffer gnus-dead-summary
7432       (when gnus-dead-summary-mode
7433         (kill-buffer (current-buffer)))))
7434   ;; Make this the current dead summary.
7435   (setq gnus-dead-summary (current-buffer))
7436   (gnus-dead-summary-mode 1)
7437   (let ((name (buffer-name)))
7438     (when (string-match "Summary" name)
7439       (rename-buffer
7440        (concat (substring name 0 (match-beginning 0)) "Dead "
7441                (substring name (match-beginning 0)))
7442        t)
7443       (bury-buffer))))
7444
7445 (defun gnus-kill-or-deaden-summary (buffer)
7446   "Kill or deaden the summary BUFFER."
7447   (save-excursion
7448     (when (and (buffer-name buffer)
7449                (not gnus-single-article-buffer))
7450       (with-current-buffer buffer
7451         (gnus-kill-buffer gnus-article-buffer)
7452         (gnus-kill-buffer gnus-original-article-buffer)))
7453     (cond
7454      ;; Kill the buffer.
7455      (gnus-kill-summary-on-exit
7456       (when (and gnus-use-trees
7457                  (gnus-buffer-exists-p buffer))
7458         (with-current-buffer buffer
7459           (gnus-tree-close gnus-newsgroup-name)))
7460       (gnus-kill-buffer buffer))
7461      ;; Deaden the buffer.
7462      ((gnus-buffer-exists-p buffer)
7463       (with-current-buffer buffer
7464         (gnus-deaden-summary))))))
7465
7466 (defun gnus-summary-wake-up-the-dead (&rest args)
7467   "Wake up the dead summary buffer."
7468   (interactive)
7469   (gnus-dead-summary-mode -1)
7470   (let ((name (buffer-name)))
7471     (when (string-match "Dead " name)
7472       (rename-buffer
7473        (concat (substring name 0 (match-beginning 0))
7474                (substring name (match-end 0)))
7475        t)))
7476   (gnus-message 3 "This dead summary is now alive again"))
7477
7478 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7479 (defun gnus-summary-describe-group (&optional force)
7480   "Describe the current newsgroup."
7481   (interactive "P")
7482   (gnus-group-describe-group force gnus-newsgroup-name))
7483
7484 (defun gnus-summary-describe-briefly ()
7485   "Describe summary mode commands briefly."
7486   (interactive)
7487   (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")))
7488
7489 ;; Walking around group mode buffer from summary mode.
7490
7491 (defun gnus-summary-next-group (&optional no-article target-group backward)
7492   "Exit current newsgroup and then select next unread newsgroup.
7493 If prefix argument NO-ARTICLE is non-nil, no article is selected
7494 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7495 previous group instead."
7496   (interactive "P")
7497   ;; Stop pre-fetching.
7498   (gnus-async-halt-prefetch)
7499   (let ((current-group gnus-newsgroup-name)
7500         (current-buffer (current-buffer))
7501         entered)
7502     ;; First we semi-exit this group to update Xrefs and all variables.
7503     ;; We can't do a real exit, because the window conf must remain
7504     ;; the same in case the user is prompted for info, and we don't
7505     ;; want the window conf to change before that...
7506     (gnus-summary-exit t)
7507     (while (not entered)
7508       ;; Then we find what group we are supposed to enter.
7509       (set-buffer gnus-group-buffer)
7510       (gnus-group-jump-to-group current-group)
7511       (setq target-group
7512             (or target-group
7513                 (if (eq gnus-keep-same-level 'best)
7514                     (gnus-summary-best-group gnus-newsgroup-name)
7515                   (gnus-summary-search-group backward gnus-keep-same-level))))
7516       (if (not target-group)
7517           ;; There are no further groups, so we return to the group
7518           ;; buffer.
7519           (progn
7520             (gnus-message 5 "Returning to the group buffer")
7521             (setq entered t)
7522             (when (gnus-buffer-live-p current-buffer)
7523               (set-buffer current-buffer)
7524               (gnus-summary-exit))
7525             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7526         ;; We try to enter the target group.
7527         (gnus-group-jump-to-group target-group)
7528         (let ((unreads (gnus-group-group-unread)))
7529           (if (and (or (eq t unreads)
7530                        (and unreads (not (zerop unreads))))
7531                    (gnus-summary-read-group
7532                     target-group nil no-article
7533                     (and (buffer-name current-buffer) current-buffer)
7534                     nil backward))
7535               (setq entered t)
7536             (setq current-group target-group
7537                   target-group nil)))))))
7538
7539 (defun gnus-summary-prev-group (&optional no-article)
7540   "Exit current newsgroup and then select previous unread newsgroup.
7541 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7542   (interactive "P")
7543   (gnus-summary-next-group no-article nil t))
7544
7545 ;; Walking around summary lines.
7546
7547 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7548   "Go to the first subject satisfying any non-nil constraint.
7549 If UNREAD is non-nil, the article should be unread.
7550 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7551 If UNSEEN is non-nil, the article should be unseen as well as unread.
7552 Returns the article selected or nil if there are no matching articles."
7553   (interactive "P")
7554   (cond
7555    ;; Empty summary.
7556    ((null gnus-newsgroup-data)
7557     (gnus-message 3 "No articles in the group")
7558     nil)
7559    ;; Pick the first article.
7560    ((not (or unread undownloaded unseen))
7561     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7562     (gnus-data-number (car gnus-newsgroup-data)))
7563    ;; Find the first unread article.
7564    (t
7565     (let ((data gnus-newsgroup-data))
7566       (while (and data
7567                   (let ((num (gnus-data-number (car data))))
7568                     (or (memq num gnus-newsgroup-unfetched)
7569                         (not (or (and unread
7570                                       (memq num gnus-newsgroup-unreads))
7571                                  (and undownloaded
7572                                       (memq num gnus-newsgroup-undownloaded))
7573                                  (and unseen
7574                                       (memq num gnus-newsgroup-unseen)
7575                                       (memq num gnus-newsgroup-unreads)))))))
7576         (setq data (cdr data)))
7577       (prog1
7578           (if data
7579               (progn
7580                 (goto-char (gnus-data-pos (car data)))
7581                 (gnus-data-number (car data)))
7582             (gnus-message 3 "No more%s articles"
7583                           (let* ((r (when unread " unread"))
7584                                  (d (when undownloaded " undownloaded"))
7585                                  (s (when unseen " unseen"))
7586                                  (l (delq nil (list r d s))))
7587                             (cond ((= 3 (length l))
7588                                    (concat r "," d ", or" s))
7589                                   ((= 2 (length l))
7590                                    (concat (car l) ", or" (cadr l)))
7591                                   ((= 1 (length l))
7592                                    (car l))
7593                                   (t
7594                                    ""))))
7595             nil
7596             )
7597         (gnus-summary-position-point))))))
7598
7599 (defun gnus-summary-next-subject (n &optional unread dont-display)
7600   "Go to next N'th summary line.
7601 If N is negative, go to the previous N'th subject line.
7602 If UNREAD is non-nil, only unread articles are selected.
7603 The difference between N and the actual number of steps taken is
7604 returned."
7605   (interactive "p")
7606   (let ((backward (< n 0))
7607         (n (abs n)))
7608     (while (and (> n 0)
7609                 (if backward
7610                     (gnus-summary-find-prev unread)
7611                   (gnus-summary-find-next unread)))
7612       (unless (zerop (setq n (1- n)))
7613         (gnus-summary-show-thread)))
7614     (when (/= 0 n)
7615       (gnus-message 7 "No more%s articles"
7616                     (if unread " unread" "")))
7617     (unless dont-display
7618       (gnus-summary-recenter)
7619       (gnus-summary-position-point))
7620     n))
7621
7622 (defun gnus-summary-next-unread-subject (n)
7623   "Go to next N'th unread summary line."
7624   (interactive "p")
7625   (gnus-summary-next-subject n t))
7626
7627 (defun gnus-summary-prev-subject (n &optional unread)
7628   "Go to previous N'th summary line.
7629 If optional argument UNREAD is non-nil, only unread article is selected."
7630   (interactive "p")
7631   (gnus-summary-next-subject (- n) unread))
7632
7633 (defun gnus-summary-prev-unread-subject (n)
7634   "Go to previous N'th unread summary line."
7635   (interactive "p")
7636   (gnus-summary-next-subject (- n) t))
7637
7638 (defun gnus-summary-goto-subjects (articles)
7639   "Insert the subject header for ARTICLES in the current buffer."
7640   (save-excursion
7641     (dolist (article articles)
7642       (gnus-summary-goto-subject article t)))
7643   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7644   (gnus-summary-position-point))
7645
7646 (defun gnus-summary-goto-subject (article &optional force silent)
7647   "Go to the subject line of ARTICLE.
7648 If FORCE, also allow jumping to articles not currently shown."
7649   (interactive "nArticle number: ")
7650   (unless (numberp article)
7651     (error "Article %s is not a number" article))
7652   (let ((b (point))
7653         (data (gnus-data-find article)))
7654     ;; We read in the article if we have to.
7655     (and (not data)
7656          force
7657          (gnus-summary-insert-subject
7658           article
7659           (if (or (numberp force) (vectorp force)) force)
7660           t)
7661          (setq data (gnus-data-find article)))
7662     (goto-char b)
7663     (if (not data)
7664         (progn
7665           (unless silent
7666             (gnus-message 3 "Can't find article %d" article))
7667           nil)
7668       (let ((pt (gnus-data-pos data)))
7669         (goto-char pt)
7670         (gnus-summary-set-article-display-arrow pt))
7671       (gnus-summary-position-point)
7672       article)))
7673
7674 ;; Walking around summary lines with displaying articles.
7675
7676 (defun gnus-summary-expand-window (&optional arg)
7677   "Make the summary buffer take up the entire Emacs frame.
7678 Given a prefix, will force an `article' buffer configuration."
7679   (interactive "P")
7680   (if arg
7681       (gnus-configure-windows 'article 'force)
7682     (gnus-configure-windows 'summary 'force)))
7683
7684 (defun gnus-summary-display-article (article &optional all-header)
7685   "Display ARTICLE in article buffer."
7686   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7687                (with-current-buffer gnus-article-buffer
7688                  (eq major-mode 'gnus-article-mode)))
7689     (gnus-article-setup-buffer))
7690   (gnus-set-global-variables)
7691   (with-current-buffer gnus-article-buffer
7692     (setq gnus-article-charset gnus-newsgroup-charset)
7693     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7694     (mm-enable-multibyte))
7695   (if (null article)
7696       nil
7697     (prog1
7698         (if gnus-summary-display-article-function
7699             (funcall gnus-summary-display-article-function article all-header)
7700           (gnus-article-prepare article all-header))
7701       (gnus-run-hooks 'gnus-select-article-hook)
7702       (when (and gnus-current-article
7703                  (not (zerop gnus-current-article)))
7704         (gnus-summary-goto-subject gnus-current-article))
7705       (gnus-summary-recenter)
7706       (when (and gnus-use-trees gnus-show-threads)
7707         (gnus-possibly-generate-tree article)
7708         (gnus-highlight-selected-tree article))
7709       ;; Successfully display article.
7710       (gnus-article-set-window-start
7711        (cdr (assq article gnus-newsgroup-bookmarks))))))
7712
7713 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7714   "Select the current article.
7715 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7716 non-nil, the article will be re-fetched even if it already present in
7717 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7718 be displayed."
7719   ;; Make sure we are in the summary buffer to work around bbdb bug.
7720   (unless (eq major-mode 'gnus-summary-mode)
7721     (set-buffer gnus-summary-buffer))
7722   (let ((article (or article (gnus-summary-article-number)))
7723         (all-headers (not (not all-headers))) ;Must be t or nil.
7724         gnus-summary-display-article-function)
7725     (and (not pseudo)
7726          (gnus-summary-article-pseudo-p article)
7727          (error "This is a pseudo-article"))
7728     (with-current-buffer gnus-summary-buffer
7729       (if (or (and gnus-single-article-buffer
7730                    (or (null gnus-current-article)
7731                        (null gnus-article-current)
7732                        (null (get-buffer gnus-article-buffer))
7733                        (not (eq article (cdr gnus-article-current)))
7734                        (not (equal (car gnus-article-current)
7735                                    gnus-newsgroup-name))
7736                        (not (get-buffer gnus-original-article-buffer))))
7737               (and (not gnus-single-article-buffer)
7738                    (or (null gnus-current-article)
7739                        (not (get-buffer gnus-original-article-buffer))
7740                        (not (eq gnus-current-article article))))
7741               force)
7742           ;; The requested article is different from the current article.
7743           (progn
7744             (gnus-summary-display-article article all-headers)
7745             (when (gnus-buffer-live-p gnus-article-buffer)
7746               (with-current-buffer gnus-article-buffer
7747                 (if (not gnus-article-decoded-p) ;; a local variable
7748                     (mm-disable-multibyte))))
7749             (gnus-article-set-window-start
7750              (cdr (assq article gnus-newsgroup-bookmarks)))
7751             article)
7752         'old))))
7753
7754 (defun gnus-summary-force-verify-and-decrypt ()
7755   "Display buttons for signed/encrypted parts and verify/decrypt them."
7756   (interactive)
7757   (let ((mm-verify-option 'known)
7758         (mm-decrypt-option 'known)
7759         (gnus-article-emulate-mime t)
7760         (gnus-buttonized-mime-types (append (list "multipart/signed"
7761                                                   "multipart/encrypted")
7762                                             gnus-buttonized-mime-types)))
7763     (gnus-summary-select-article nil 'force)))
7764
7765 (defun gnus-summary-set-current-mark (&optional current-mark)
7766   "Obsolete function."
7767   nil)
7768
7769 (defun gnus-summary-next-article (&optional unread subject backward push)
7770   "Select the next article.
7771 If UNREAD, only unread articles are selected.
7772 If SUBJECT, only articles with SUBJECT are selected.
7773 If BACKWARD, the previous article is selected instead of the next."
7774   (interactive "P")
7775   ;; Make sure we are in the summary buffer.
7776   (unless (eq major-mode 'gnus-summary-mode)
7777     (set-buffer gnus-summary-buffer))
7778   (cond
7779    ;; Is there such an article?
7780    ((and (gnus-summary-search-forward unread subject backward)
7781          (or (gnus-summary-display-article (gnus-summary-article-number))
7782              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7783     (gnus-summary-position-point))
7784    ;; If not, we try the first unread, if that is wanted.
7785    ((and subject
7786          gnus-auto-select-same
7787          (gnus-summary-first-unread-article))
7788     (gnus-summary-position-point)
7789     (gnus-message 6 "Wrapped"))
7790    ;; Try to get next/previous article not displayed in this group.
7791    ((and gnus-auto-extend-newsgroup
7792          (not unread) (not subject))
7793     (gnus-summary-goto-article
7794      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7795      nil (count-lines (point-min) (point))))
7796    ;; Go to next/previous group.
7797    (t
7798     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7799       (gnus-summary-jump-to-group gnus-newsgroup-name))
7800     (let ((cmd (if (featurep 'xemacs)
7801                    last-command-char
7802                  last-command-event))
7803           (point
7804            (with-current-buffer gnus-group-buffer
7805              (point)))
7806           (current-summary (current-buffer))
7807           (group
7808            (if (eq gnus-keep-same-level 'best)
7809                (gnus-summary-best-group gnus-newsgroup-name)
7810              (gnus-summary-search-group backward gnus-keep-same-level))))
7811       ;; Select next unread newsgroup automagically.
7812       (cond
7813        ((or (not gnus-auto-select-next)
7814             (not cmd))
7815         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7816        ((or (eq gnus-auto-select-next 'quietly)
7817             (and (eq gnus-auto-select-next 'slightly-quietly)
7818                  push)
7819             (and (eq gnus-auto-select-next 'almost-quietly)
7820                  (gnus-summary-last-article-p)))
7821         ;; Select quietly.
7822         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7823             (gnus-summary-exit)
7824           (gnus-message 7 "No more%s articles (%s)..."
7825                         (if unread " unread" "")
7826                         (if group (concat "selecting " group)
7827                           "exiting"))
7828           (gnus-summary-next-group nil group backward)))
7829        (t
7830         (when (gnus-key-press-event-p last-input-event)
7831           ;; Somehow or other, we may now have selected a different
7832           ;; window.  Make point go back to the summary buffer.
7833           (when (eq current-summary (current-buffer))
7834             ;; FIXME: This burps when get-buffer-window returns nil.
7835             (select-window (get-buffer-window current-summary 0)))
7836           (gnus-summary-walk-group-buffer
7837            gnus-newsgroup-name cmd unread backward point))))))))
7838
7839 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7840   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7841                       (?\C-p (gnus-group-prev-unread-group 1))))
7842         (cursor-in-echo-area t)
7843         keve key group ended prompt)
7844     (with-current-buffer gnus-group-buffer
7845       (goto-char start)
7846       (setq group
7847             (if (eq gnus-keep-same-level 'best)
7848                 (gnus-summary-best-group gnus-newsgroup-name)
7849               (gnus-summary-search-group backward gnus-keep-same-level))))
7850     (while (not ended)
7851       (setq prompt
7852             (format
7853              "No more%s articles%s " (if unread " unread" "")
7854              (if (and group
7855                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7856                  (format " (Type %s for %s [%s])"
7857                          (single-key-description cmd)
7858                          (gnus-group-decoded-name group)
7859                          (gnus-group-unread group))
7860                (format " (Type %s to exit %s)"
7861                        (single-key-description cmd)
7862                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7863       ;; Confirm auto selection.
7864       (setq key (car (setq keve (gnus-read-event-char prompt)))
7865             ended t)
7866       (cond
7867        ((assq key keystrokes)
7868         (let ((obuf (current-buffer)))
7869           (switch-to-buffer gnus-group-buffer)
7870           (when group
7871             (gnus-group-jump-to-group group))
7872           (eval (cadr (assq key keystrokes)))
7873           (setq group (gnus-group-group-name))
7874           (switch-to-buffer obuf))
7875         (setq ended nil))
7876        ((equal key cmd)
7877         (if (or (not group)
7878                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7879             (gnus-summary-exit)
7880           (gnus-summary-next-group nil group backward)))
7881        (t
7882         (push (cdr keve) unread-command-events))))))
7883
7884 (defun gnus-summary-next-unread-article ()
7885   "Select unread article after current one."
7886   (interactive)
7887   (gnus-summary-next-article
7888    (or (not (eq gnus-summary-goto-unread 'never))
7889        (gnus-summary-last-article-p (gnus-summary-article-number)))
7890    (and gnus-auto-select-same
7891         (gnus-summary-article-subject))))
7892
7893 (defun gnus-summary-prev-article (&optional unread subject)
7894   "Select the article before the current one.
7895 If UNREAD is non-nil, only unread articles are selected."
7896   (interactive "P")
7897   (gnus-summary-next-article unread subject t))
7898
7899 (defun gnus-summary-prev-unread-article ()
7900   "Select unread article before current one."
7901   (interactive)
7902   (gnus-summary-prev-article
7903    (or (not (eq gnus-summary-goto-unread 'never))
7904        (gnus-summary-first-article-p (gnus-summary-article-number)))
7905    (and gnus-auto-select-same
7906         (gnus-summary-article-subject))))
7907
7908 (defun gnus-summary-next-page (&optional lines circular stop)
7909   "Show next page of the selected article.
7910 If at the end of the current article, select the next article.
7911 LINES says how many lines should be scrolled up.
7912
7913 If CIRCULAR is non-nil, go to the start of the article instead of
7914 selecting the next article when reaching the end of the current
7915 article.
7916
7917 If STOP is non-nil, just stop when reaching the end of the message.
7918
7919 Also see the variable `gnus-article-skip-boring'."
7920   (interactive "P")
7921   (setq gnus-summary-buffer (current-buffer))
7922   (gnus-set-global-variables)
7923   (let ((article (gnus-summary-article-number))
7924         (article-window (get-buffer-window gnus-article-buffer t))
7925         endp)
7926     ;; If the buffer is empty, we have no article.
7927     (unless article
7928       (error "No article to select"))
7929     (gnus-configure-windows 'article)
7930     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7931         (if (and (eq gnus-summary-goto-unread 'never)
7932                  (not (gnus-summary-last-article-p article)))
7933             (gnus-summary-next-article)
7934           (gnus-summary-next-unread-article))
7935       (if (or (null gnus-current-article)
7936               (null gnus-article-current)
7937               (/= article (cdr gnus-article-current))
7938               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7939           ;; Selected subject is different from current article's.
7940           (gnus-summary-display-article article)
7941         (when article-window
7942           (gnus-eval-in-buffer-window gnus-article-buffer
7943             (setq endp (or (gnus-article-next-page lines)
7944                            (gnus-article-only-boring-p))))
7945           (when endp
7946             (cond ((or stop gnus-summary-stop-at-end-of-message)
7947                    (gnus-message 3 "End of message"))
7948                   (circular
7949                    (gnus-summary-beginning-of-article))
7950                   (lines
7951                    (gnus-message 3 "End of message"))
7952                   ((null lines)
7953                    (if (and (eq gnus-summary-goto-unread 'never)
7954                             (not (gnus-summary-last-article-p article)))
7955                        (gnus-summary-next-article)
7956                      (gnus-summary-next-unread-article))))))))
7957     (gnus-summary-recenter)
7958     (gnus-summary-position-point)))
7959
7960 (defun gnus-summary-prev-page (&optional lines move)
7961   "Show previous page of selected article.
7962 Argument LINES specifies lines to be scrolled down.
7963 If MOVE, move to the previous unread article if point is at
7964 the beginning of the buffer."
7965   (interactive "P")
7966   (let ((article (gnus-summary-article-number))
7967         (article-window (get-buffer-window gnus-article-buffer t))
7968         endp)
7969     (gnus-configure-windows 'article)
7970     (if (or (null gnus-current-article)
7971             (null gnus-article-current)
7972             (/= article (cdr gnus-article-current))
7973             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7974         ;; Selected subject is different from current article's.
7975         (gnus-summary-display-article article)
7976       (gnus-summary-recenter)
7977       (when article-window
7978         (gnus-eval-in-buffer-window gnus-article-buffer
7979           (setq endp (gnus-article-prev-page lines)))
7980         (when (and move endp)
7981           (cond (lines
7982                  (gnus-message 3 "Beginning of message"))
7983                 ((null lines)
7984                  (if (and (eq gnus-summary-goto-unread 'never)
7985                           (not (gnus-summary-first-article-p article)))
7986                      (gnus-summary-prev-article)
7987                    (gnus-summary-prev-unread-article))))))))
7988   (gnus-summary-position-point))
7989
7990 (defun gnus-summary-prev-page-or-article (&optional lines)
7991   "Show previous page of selected article.
7992 Argument LINES specifies lines to be scrolled down.
7993 If at the beginning of the article, go to the next article."
7994   (interactive "P")
7995   (gnus-summary-prev-page lines t))
7996
7997 (defun gnus-summary-scroll-up (lines)
7998   "Scroll up (or down) one line current article.
7999 Argument LINES specifies lines to be scrolled up (or down if negative).
8000 If no article is selected, then the current article will be selected first."
8001   (interactive "p")
8002   (gnus-configure-windows 'article)
8003   (gnus-summary-show-thread)
8004   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
8005     (gnus-eval-in-buffer-window gnus-article-buffer
8006       (cond ((> lines 0)
8007              (when (gnus-article-next-page lines)
8008                (gnus-message 3 "End of message")))
8009             ((< lines 0)
8010              (gnus-article-prev-page (- lines))))))
8011   (gnus-summary-recenter)
8012   (gnus-summary-position-point))
8013
8014 (defun gnus-summary-scroll-down (lines)
8015   "Scroll down (or up) one line current article.
8016 Argument LINES specifies lines to be scrolled down (or up if negative).
8017 If no article is selected, then the current article will be selected first."
8018   (interactive "p")
8019   (gnus-summary-scroll-up (- lines)))
8020
8021 (defun gnus-summary-next-same-subject ()
8022   "Select next article which has the same subject as current one."
8023   (interactive)
8024   (gnus-summary-next-article nil (gnus-summary-article-subject)))
8025
8026 (defun gnus-summary-prev-same-subject ()
8027   "Select previous article which has the same subject as current one."
8028   (interactive)
8029   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8030
8031 (defun gnus-summary-next-unread-same-subject ()
8032   "Select next unread article which has the same subject as current one."
8033   (interactive)
8034   (gnus-summary-next-article t (gnus-summary-article-subject)))
8035
8036 (defun gnus-summary-prev-unread-same-subject ()
8037   "Select previous unread article which has the same subject as current one."
8038   (interactive)
8039   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8040
8041 (defun gnus-summary-first-unread-article ()
8042   "Select the first unread article.
8043 Return nil if there are no unread articles."
8044   (interactive)
8045   (prog1
8046       (when (gnus-summary-first-subject t)
8047         (gnus-summary-show-thread)
8048         (gnus-summary-first-subject t)
8049         (gnus-summary-display-article (gnus-summary-article-number)))
8050     (gnus-summary-position-point)))
8051
8052 (defun gnus-summary-first-unread-subject ()
8053   "Place the point on the subject line of 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-position-point)))
8061
8062 (defun gnus-summary-first-unseen-subject ()
8063   "Place the point on the subject line of the first unseen article.
8064 Return nil if there are no unseen articles."
8065   (interactive)
8066   (prog1
8067       (when (gnus-summary-first-subject nil nil t)
8068         (gnus-summary-show-thread)
8069         (gnus-summary-first-subject nil nil t))
8070     (gnus-summary-position-point)))
8071
8072 (defun gnus-summary-first-unseen-or-unread-subject ()
8073   "Place the point on the subject line of the first unseen and unread article.
8074 If all article have been seen, on the subject line of the first unread
8075 article."
8076   (interactive)
8077   (prog1
8078       (unless (when (gnus-summary-first-subject nil nil t)
8079                 (gnus-summary-show-thread)
8080                 (gnus-summary-first-subject nil nil t))
8081         (when (gnus-summary-first-subject t)
8082           (gnus-summary-show-thread)
8083           (gnus-summary-first-subject t)))
8084     (gnus-summary-position-point)))
8085
8086 (defun gnus-summary-first-article ()
8087   "Select the first article.
8088 Return nil if there are no articles."
8089   (interactive)
8090   (prog1
8091       (when (gnus-summary-first-subject)
8092         (gnus-summary-show-thread)
8093         (gnus-summary-first-subject)
8094         (gnus-summary-display-article (gnus-summary-article-number)))
8095     (gnus-summary-position-point)))
8096
8097 (defun gnus-summary-best-unread-article (&optional arg)
8098   "Select the unread article with the highest score.
8099 If given a prefix argument, select the next unread article that has a
8100 score higher than the default score."
8101   (interactive "P")
8102   (let ((article (if arg
8103                      (gnus-summary-better-unread-subject)
8104                    (gnus-summary-best-unread-subject))))
8105     (if article
8106         (gnus-summary-goto-article article)
8107       (error "No unread articles"))))
8108
8109 (defun gnus-summary-best-unread-subject ()
8110   "Select the unread subject with the highest score."
8111   (interactive)
8112   (let ((best -1000000)
8113         (data gnus-newsgroup-data)
8114         article score)
8115     (while data
8116       (and (gnus-data-unread-p (car data))
8117            (> (setq score
8118                     (gnus-summary-article-score (gnus-data-number (car data))))
8119               best)
8120            (setq best score
8121                  article (gnus-data-number (car data))))
8122       (setq data (cdr data)))
8123     (when article
8124       (gnus-summary-goto-subject article))
8125     (gnus-summary-position-point)
8126     article))
8127
8128 (defun gnus-summary-better-unread-subject ()
8129   "Select the first unread subject that has a score over the default score."
8130   (interactive)
8131   (let ((data gnus-newsgroup-data)
8132         article score)
8133     (while (and (setq article (gnus-data-number (car data)))
8134                 (or (gnus-data-read-p (car data))
8135                     (not (> (gnus-summary-article-score article)
8136                             gnus-summary-default-score))))
8137       (setq data (cdr data)))
8138     (when article
8139       (gnus-summary-goto-subject article))
8140     (gnus-summary-position-point)
8141     article))
8142
8143 (defun gnus-summary-last-subject ()
8144   "Go to the last displayed subject line in the group."
8145   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8146     (when article
8147       (gnus-summary-goto-subject article))))
8148
8149 (defun gnus-summary-goto-article (article &optional all-headers force)
8150   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8151 If ALL-HEADERS is non-nil, no header lines are hidden.
8152 If FORCE, go to the article even if it isn't displayed.  If FORCE
8153 is a number, it is the line the article is to be displayed on."
8154   (interactive
8155    (list
8156     (gnus-completing-read
8157      "Article number or Message-ID"
8158      (mapcar 'int-to-string gnus-newsgroup-limit))
8159     current-prefix-arg
8160     t))
8161   (prog1
8162       (if (and (stringp article)
8163                (string-match "@\\|%40" article))
8164           (gnus-summary-refer-article article)
8165         (when (stringp article)
8166           (setq article (string-to-number article)))
8167         (if (gnus-summary-goto-subject article force)
8168             (gnus-summary-display-article article all-headers)
8169           (gnus-message 4 "Couldn't go to article %s" article) nil))
8170     (gnus-summary-position-point)))
8171
8172 (defun gnus-summary-goto-last-article ()
8173   "Go to the previously read article."
8174   (interactive)
8175   (prog1
8176       (when gnus-last-article
8177         (gnus-summary-goto-article gnus-last-article nil t))
8178     (gnus-summary-position-point)))
8179
8180 (defun gnus-summary-pop-article (number)
8181   "Pop one article off the history and go to the previous.
8182 NUMBER articles will be popped off."
8183   (interactive "p")
8184   (let (to)
8185     (setq gnus-newsgroup-history
8186           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8187     (if to
8188         (gnus-summary-goto-article (car to) nil t)
8189       (error "Article history empty")))
8190   (gnus-summary-position-point))
8191
8192 ;; Summary commands and functions for limiting the summary buffer.
8193
8194 (defun gnus-summary-limit-to-articles (n)
8195   "Limit the summary buffer to the next N articles.
8196 If not given a prefix, use the process marked articles instead."
8197   (interactive "P")
8198   (prog1
8199       (let ((articles (gnus-summary-work-articles n)))
8200         (setq gnus-newsgroup-processable nil)
8201         (gnus-summary-limit articles))
8202     (gnus-summary-position-point)))
8203
8204 (defun gnus-summary-pop-limit (&optional total)
8205   "Restore the previous limit.
8206 If given a prefix, remove all limits."
8207   (interactive "P")
8208   (when total
8209     (setq gnus-newsgroup-limits
8210           (list (mapcar (lambda (h) (mail-header-number h))
8211                         gnus-newsgroup-headers))))
8212   (unless gnus-newsgroup-limits
8213     (error "No limit to pop"))
8214   (prog1
8215       (gnus-summary-limit nil 'pop)
8216     (gnus-summary-position-point)))
8217
8218 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8219   "Limit the summary buffer to articles that have subjects that match a regexp.
8220 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8221   (interactive
8222    (list (read-string (if current-prefix-arg
8223                           "Exclude subject (regexp): "
8224                         "Limit to subject (regexp): "))
8225          nil current-prefix-arg))
8226   (unless header
8227     (setq header "subject"))
8228   (when (not (equal "" subject))
8229     (prog1
8230         (let ((articles (gnus-summary-find-matching
8231                          (or header "subject") subject 'all nil nil
8232                          not-matching)))
8233           (unless articles
8234             (error "Found no matches for \"%s\"" subject))
8235           (gnus-summary-limit articles))
8236       (gnus-summary-position-point))))
8237
8238 (defun gnus-summary-limit-to-author (from &optional not-matching)
8239   "Limit the summary buffer to articles that have authors that match a regexp.
8240 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8241   (interactive
8242    (list (read-string (if current-prefix-arg
8243                           "Exclude author (regexp): "
8244                         "Limit to author (regexp): "))
8245          current-prefix-arg))
8246   (gnus-summary-limit-to-subject from "from" not-matching))
8247
8248 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8249   "Limit the summary buffer to articles with the given RECIPIENT.
8250
8251 If NOT-MATCHING, exclude RECIPIENT.
8252
8253 To and Cc headers are checked.  You need to include them in
8254 `nnmail-extra-headers'."
8255   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8256   (interactive
8257    (list (read-string (format "%s recipient (regexp): "
8258                               (if current-prefix-arg "Exclude" "Limit to")))
8259          current-prefix-arg))
8260   (when (not (equal "" recipient))
8261     (prog1 (let* ((to
8262                    (if (memq 'To nnmail-extra-headers)
8263                        (gnus-summary-find-matching
8264                         (cons 'extra 'To) recipient 'all nil nil
8265                         not-matching)
8266                      (gnus-message
8267                       1 "`To' isn't present in `nnmail-extra-headers'")
8268                      (sit-for 1)
8269                      nil))
8270                   (cc
8271                    (if (memq 'Cc nnmail-extra-headers)
8272                        (gnus-summary-find-matching
8273                         (cons 'extra 'Cc) recipient 'all nil nil
8274                         not-matching)
8275                      (gnus-message
8276                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8277                      (sit-for 1)
8278                      nil))
8279                   (articles
8280                    (if not-matching
8281                        ;; We need the numbers that are in both lists:
8282                        (mapcar (lambda (a)
8283                                  (and (memq a to) a))
8284                                cc)
8285                      (nconc to cc))))
8286              (unless articles
8287                (error "Found no matches for \"%s\"" recipient))
8288              (gnus-summary-limit articles))
8289       (gnus-summary-position-point))))
8290
8291 (defun gnus-summary-limit-to-address (address &optional not-matching)
8292   "Limit the summary buffer to articles with the given ADDRESS.
8293
8294 If NOT-MATCHING, exclude ADDRESS.
8295
8296 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8297 in `nnmail-extra-headers'."
8298   (interactive
8299    (list (read-string (format "%s address (regexp): "
8300                               (if current-prefix-arg "Exclude" "Limit to")))
8301          current-prefix-arg))
8302   (when (not (equal "" address))
8303     (prog1 (let* ((to
8304                    (if (memq 'To nnmail-extra-headers)
8305                        (gnus-summary-find-matching
8306                         (cons 'extra 'To) address 'all nil nil
8307                         not-matching)
8308                      (gnus-message
8309                       1 "`To' isn't present in `nnmail-extra-headers'")
8310                      (sit-for 1)
8311                      t))
8312                   (cc
8313                    (if (memq 'Cc nnmail-extra-headers)
8314                        (gnus-summary-find-matching
8315                         (cons 'extra 'Cc) address 'all nil nil
8316                         not-matching)
8317                      (gnus-message
8318                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8319                      (sit-for 1)
8320                      t))
8321                   (from
8322                    (gnus-summary-find-matching "from" address
8323                                                'all nil nil not-matching))
8324                   (articles
8325                    (if not-matching
8326                        ;; We need the numbers that are in all lists:
8327                        (if (eq cc t)
8328                            (if (eq to t)
8329                                from
8330                              (mapcar (lambda (a) (car (memq a from))) to))
8331                          (if (eq to t)
8332                              (mapcar (lambda (a) (car (memq a from))) cc)
8333                            (mapcar (lambda (a) (car (memq a from)))
8334                                    (mapcar (lambda (a) (car (memq a to)))
8335                                            cc))))
8336                      (nconc (if (eq to t) nil to)
8337                             (if (eq cc t) nil cc)
8338                             from))))
8339              (unless articles
8340                (error "Found no matches for \"%s\"" address))
8341              (gnus-summary-limit articles))
8342       (gnus-summary-position-point))))
8343
8344 (defun gnus-summary-limit-strange-charsets-predicate (header)
8345   (when (fboundp 'char-charset)
8346     (let ((string (concat (mail-header-subject header)
8347                           (mail-header-from header)))
8348           charset found)
8349       (dotimes (i (1- (length string)))
8350         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8351         (when (string-match "unicode\\|big\\|japanese" charset)
8352           (setq found t)))
8353       found)))
8354
8355 (defun gnus-summary-limit-to-predicate (predicate)
8356   "Limit to articles where PREDICATE returns non-nil.
8357 PREDICATE will be called with the header structures of the
8358 articles."
8359   (let ((articles nil)
8360         (case-fold-search t))
8361     (dolist (header gnus-newsgroup-headers)
8362       (when (funcall predicate header)
8363         (push (mail-header-number header) articles)))
8364     (gnus-summary-limit (nreverse articles))))
8365
8366 (defun gnus-summary-limit-to-age (age &optional younger-p)
8367   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8368 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8369 articles that are younger than AGE days."
8370   (interactive
8371    (let ((younger current-prefix-arg)
8372          (days-got nil)
8373          days)
8374      (while (not days-got)
8375        (setq days (if younger
8376                       (read-string "Limit to articles younger than (in days, older when negative): ")
8377                     (read-string
8378                      "Limit to articles older than (in days, younger when negative): ")))
8379        (when (> (length days) 0)
8380          (setq days (read days)))
8381        (if (numberp days)
8382            (progn
8383              (setq days-got t)
8384              (when (< days 0)
8385                (setq younger (not younger))
8386                (setq days (* days -1))))
8387          (message "Please enter a number.")
8388          (sleep-for 1)))
8389      (list days younger)))
8390   (prog1
8391       (let ((data gnus-newsgroup-data)
8392             (cutoff (days-to-time age))
8393             articles d date is-younger)
8394         (while (setq d (pop data))
8395           (when (and (vectorp (gnus-data-header d))
8396                      (setq date (mail-header-date (gnus-data-header d))))
8397             (setq is-younger (time-less-p
8398                               (time-since (gnus-date-get-time date))
8399                               cutoff))
8400             (when (if younger-p
8401                       is-younger
8402                     (not is-younger))
8403               (push (gnus-data-number d) articles))))
8404         (gnus-summary-limit (nreverse articles)))
8405     (gnus-summary-position-point)))
8406
8407 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8408   "Limit the summary buffer to articles that match an 'extra' header."
8409   (interactive
8410    (let ((header
8411           (intern
8412            (gnus-completing-read
8413             (if current-prefix-arg
8414                 "Exclude extra header"
8415               "Limit extra header")
8416             (mapcar 'symbol-name gnus-extra-headers)
8417             t nil nil
8418             (symbol-name (car gnus-extra-headers))))))
8419      (list header
8420            (read-string (format "%s header %s (regexp): "
8421                                 (if current-prefix-arg "Exclude" "Limit to")
8422                                 header))
8423            current-prefix-arg)))
8424   (when (not (equal "" regexp))
8425     (prog1
8426         (let ((articles (gnus-summary-find-matching
8427                          (cons 'extra header) regexp 'all nil nil
8428                          not-matching)))
8429           (unless articles
8430             (error "Found no matches for \"%s\"" regexp))
8431           (gnus-summary-limit articles))
8432       (gnus-summary-position-point))))
8433
8434 (defun gnus-summary-limit-to-display-predicate ()
8435   "Limit the summary buffer to the predicated in the `display' group parameter."
8436   (interactive)
8437   (unless gnus-newsgroup-display
8438     (error "There is no `display' group parameter"))
8439   (let (articles)
8440     (dolist (gnus-number gnus-newsgroup-articles)
8441       (when (funcall gnus-newsgroup-display)
8442         (push gnus-number articles)))
8443     (gnus-summary-limit articles))
8444   (gnus-summary-position-point))
8445
8446 (defun gnus-summary-limit-to-unread (&optional all)
8447   "Limit the summary buffer to articles that are not marked as read.
8448 If ALL is non-nil, limit strictly to unread articles."
8449   (interactive "P")
8450   (if all
8451       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8452     (gnus-summary-limit-to-marks
8453      ;; Concat all the marks that say that an article is read and have
8454      ;; those removed.
8455      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8456            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8457            gnus-low-score-mark gnus-expirable-mark
8458            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8459            gnus-duplicate-mark)
8460      'reverse)))
8461
8462 (defun gnus-summary-limit-to-headers (match &optional reverse)
8463   "Limit the summary buffer to articles that have headers that match MATCH.
8464 If REVERSE (the prefix), limit to articles that don't match."
8465   (interactive "sMatch headers (regexp): \nP")
8466   (gnus-summary-limit-to-bodies match reverse t))
8467
8468 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8469   "Limit the summary buffer to articles that have bodies that match MATCH.
8470 If REVERSE (the prefix), limit to articles that don't match."
8471   (interactive "sMatch body (regexp): \nP")
8472   (let ((articles nil)
8473         (gnus-select-article-hook nil)  ;Disable hook.
8474         (gnus-article-prepare-hook nil)
8475         (gnus-use-article-prefetch nil)
8476         (gnus-keep-backlog nil)
8477         (gnus-break-pages nil)
8478         (gnus-summary-display-arrow nil)
8479         (gnus-updated-mode-lines nil)
8480         (gnus-auto-center-summary nil)
8481         (gnus-display-mime-function nil))
8482     (dolist (data gnus-newsgroup-data)
8483       (let (gnus-mark-article-hook)
8484         (gnus-summary-select-article t t nil (gnus-data-number data)))
8485       (with-current-buffer gnus-article-buffer
8486         (article-goto-body)
8487         (let* ((case-fold-search t)
8488                (found (if headersp
8489                           (re-search-backward match nil t)
8490                         (re-search-forward match nil t))))
8491           (when (or (and found
8492                          (not reverse))
8493                     (and (not found)
8494                          reverse))
8495             (push (gnus-data-number data) articles)))))
8496     (if (not articles)
8497         (message "No messages matched")
8498       (gnus-summary-limit articles)))
8499   (gnus-summary-position-point))
8500
8501 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8502   "Limit the summary buffer to articles that aren't part on any thread.
8503 If THREADSP (the prefix), limit to articles that are in threads."
8504   (interactive "P")
8505   (let ((articles nil)
8506         thread-articles
8507         threads)
8508     (dolist (thread gnus-newsgroup-threads)
8509       (if (stringp (car thread))
8510           (dolist (thread (cdr thread))
8511             (push thread threads))
8512         (push thread threads)))
8513     (dolist (thread threads)
8514       (setq thread-articles (gnus-articles-in-thread thread))
8515       (when (or (and threadsp
8516                      (> (length thread-articles) 1))
8517                 (and (not threadsp)
8518                      (= (length thread-articles) 1)))
8519         (setq articles (nconc thread-articles articles))))
8520     (if (not articles)
8521         (message "No messages matched")
8522       (gnus-summary-limit articles))
8523     (gnus-summary-position-point)))
8524
8525 (defun gnus-summary-limit-to-replied (&optional unreplied)
8526   "Limit the summary buffer to replied articles.
8527 If UNREPLIED (the prefix), limit to unreplied articles."
8528   (interactive "P")
8529   (if unreplied
8530       (gnus-summary-limit
8531        (gnus-set-difference gnus-newsgroup-articles
8532         gnus-newsgroup-replied))
8533     (gnus-summary-limit gnus-newsgroup-replied))
8534   (gnus-summary-position-point))
8535
8536 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8537   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8538 If REVERSE, limit the summary buffer to articles that are marked
8539 with MARKS.  MARKS can either be a string of marks or a list of marks.
8540 Returns how many articles were removed."
8541   (interactive "sMarks: ")
8542   (gnus-summary-limit-to-marks marks t))
8543
8544 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8545   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8546 If REVERSE (the prefix), limit the summary buffer to articles that are
8547 not marked with MARKS.  MARKS can either be a string of marks or a
8548 list of marks.
8549 Returns how many articles were removed."
8550   (interactive "sMarks: \nP")
8551   (prog1
8552       (let ((data gnus-newsgroup-data)
8553             (marks (if (listp marks) marks
8554                      (append marks nil))) ; Transform to list.
8555             articles)
8556         (while data
8557           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8558                   (memq (gnus-data-mark (car data)) marks))
8559             (push (gnus-data-number (car data)) articles))
8560           (setq data (cdr data)))
8561         (gnus-summary-limit articles))
8562     (gnus-summary-position-point)))
8563
8564 (defun gnus-summary-limit-to-score (score)
8565   "Limit to articles with score at or above SCORE."
8566   (interactive "NLimit to articles with score of at least: ")
8567   (let ((data gnus-newsgroup-data)
8568         articles)
8569     (while data
8570       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8571                 score)
8572         (push (gnus-data-number (car data)) articles))
8573       (setq data (cdr data)))
8574     (prog1
8575         (gnus-summary-limit articles)
8576       (gnus-summary-position-point))))
8577
8578 (defun gnus-summary-limit-to-unseen ()
8579   "Limit to unseen articles."
8580   (interactive)
8581   (prog1
8582       (gnus-summary-limit gnus-newsgroup-unseen)
8583     (gnus-summary-position-point)))
8584
8585 (defun gnus-summary-limit-include-thread (id)
8586   "Display all the hidden articles that is in the thread with ID in it.
8587 When called interactively, ID is the Message-ID of the current
8588 article."
8589   (interactive (list (mail-header-id (gnus-summary-article-header))))
8590   (let ((articles (gnus-articles-in-thread
8591                    (gnus-id-to-thread (gnus-root-id id))))
8592         ;;we REALLY want the whole thread---this prevents cut-threads
8593         ;;from removing the thread we want to include.
8594         (gnus-fetch-old-headers nil)
8595         (gnus-build-sparse-threads nil))
8596     (prog1
8597         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8598       (gnus-summary-limit-include-matching-articles
8599        "subject"
8600        (regexp-quote (gnus-simplify-subject-re
8601                       (mail-header-subject (gnus-id-to-header id)))))
8602       (gnus-summary-position-point))))
8603
8604 (defun gnus-summary-limit-include-matching-articles (header regexp)
8605   "Display all the hidden articles that have HEADERs that match REGEXP."
8606   (interactive (list (read-string "Match on header: ")
8607                      (read-string "Regexp: ")))
8608   (let ((articles (gnus-find-matching-articles header regexp)))
8609     (prog1
8610         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8611       (gnus-summary-position-point))))
8612
8613 (defun gnus-summary-insert-dormant-articles ()
8614   "Insert all the dormant articles for this group into the current buffer."
8615   (interactive)
8616   (let ((gnus-verbose (max 6 gnus-verbose)))
8617     (if (not gnus-newsgroup-dormant)
8618         (gnus-message 3 "No dormant articles for this group")
8619       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8620
8621 (defun gnus-summary-insert-ticked-articles ()
8622   "Insert ticked articles for this group into the current buffer."
8623   (interactive)
8624   (let ((gnus-verbose (max 6 gnus-verbose)))
8625     (if (not gnus-newsgroup-marked)
8626         (gnus-message 3 "No ticked articles for this group")
8627       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8628
8629 (defun gnus-summary-limit-include-dormant ()
8630   "Display all the hidden articles that are marked as dormant.
8631 Note that this command only works on a subset of the articles currently
8632 fetched for this group."
8633   (interactive)
8634   (unless gnus-newsgroup-dormant
8635     (error "There are no dormant articles in this group"))
8636   (prog1
8637       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8638     (gnus-summary-position-point)))
8639
8640 (defun gnus-summary-include-articles (articles)
8641   "Fetch the headers for ARTICLES and then display the summary lines."
8642   (let ((gnus-inhibit-demon t)
8643         (gnus-agent nil)
8644         (gnus-read-all-available-headers t))
8645     (setq gnus-newsgroup-headers
8646           (gnus-merge
8647            'list gnus-newsgroup-headers
8648            (gnus-fetch-headers articles nil t)
8649            'gnus-article-sort-by-number))
8650     (setq gnus-newsgroup-articles
8651           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8652     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8653
8654 (defun gnus-summary-limit-exclude-dormant ()
8655   "Hide all dormant articles."
8656   (interactive)
8657   (prog1
8658       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8659     (gnus-summary-position-point)))
8660
8661 (defun gnus-summary-limit-exclude-childless-dormant ()
8662   "Hide all dormant articles that have no children."
8663   (interactive)
8664   (let ((data (gnus-data-list t))
8665         articles d children)
8666     ;; Find all articles that are either not dormant or have
8667     ;; children.
8668     (while (setq d (pop data))
8669       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8670                 (and (setq children
8671                            (gnus-article-children (gnus-data-number d)))
8672                      (let (found)
8673                        (while children
8674                          (when (memq (car children) articles)
8675                            (setq children nil
8676                                  found t))
8677                          (pop children))
8678                        found)))
8679         (push (gnus-data-number d) articles)))
8680     ;; Do the limiting.
8681     (prog1
8682         (gnus-summary-limit articles)
8683       (gnus-summary-position-point))))
8684
8685 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8686   "Mark all unread excluded articles as read.
8687 If ALL, mark even excluded ticked and dormants as read."
8688   (interactive "P")
8689   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8690   (let ((articles (gnus-sorted-ndifference
8691                    (sort
8692                     (mapcar (lambda (h) (mail-header-number h))
8693                             gnus-newsgroup-headers)
8694                     '<)
8695                    gnus-newsgroup-limit))
8696         article)
8697     (setq gnus-newsgroup-unreads
8698           (gnus-sorted-intersection gnus-newsgroup-unreads
8699                                     gnus-newsgroup-limit))
8700     (if all
8701         (setq gnus-newsgroup-dormant nil
8702               gnus-newsgroup-marked nil
8703               gnus-newsgroup-reads
8704               (nconc
8705                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8706                gnus-newsgroup-reads))
8707       (while (setq article (pop articles))
8708         (unless (or (memq article gnus-newsgroup-dormant)
8709                     (memq article gnus-newsgroup-marked))
8710           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8711
8712 (defun gnus-summary-limit (articles &optional pop)
8713   (if pop
8714       ;; We pop the previous limit off the stack and use that.
8715       (setq articles (car gnus-newsgroup-limits)
8716             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8717     ;; We use the new limit, so we push the old limit on the stack.
8718     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8719   ;; Set the limit.
8720   (setq gnus-newsgroup-limit articles)
8721   (let ((total (length gnus-newsgroup-data))
8722         (data (gnus-data-find-list (gnus-summary-article-number)))
8723         (gnus-summary-mark-below nil)   ; Inhibit this.
8724         found)
8725     ;; This will do all the work of generating the new summary buffer
8726     ;; according to the new limit.
8727     (gnus-summary-prepare)
8728     ;; Hide any threads, possibly.
8729     (gnus-summary-maybe-hide-threads)
8730     ;; Try to return to the article you were at, or one in the
8731     ;; neighborhood.
8732     (when data
8733       ;; We try to find some article after the current one.
8734       (while data
8735         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8736           (setq data nil
8737                 found t))
8738         (setq data (cdr data))))
8739     (unless found
8740       ;; If there is no data, that means that we were after the last
8741       ;; article.  The same goes when we can't find any articles
8742       ;; after the current one.
8743       (goto-char (point-max))
8744       (gnus-summary-find-prev))
8745     (gnus-set-mode-line 'summary)
8746     ;; We return how many articles were removed from the summary
8747     ;; buffer as a result of the new limit.
8748     (- total (length gnus-newsgroup-data))))
8749
8750 (defsubst gnus-invisible-cut-children (threads)
8751   (let ((num 0))
8752     (while threads
8753       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8754         (incf num))
8755       (pop threads))
8756     (< num 2)))
8757
8758 (defsubst gnus-cut-thread (thread)
8759   "Go forwards in the thread until we find an article that we want to display."
8760   (when (or (eq gnus-fetch-old-headers 'some)
8761             (eq gnus-fetch-old-headers 'invisible)
8762             (numberp gnus-fetch-old-headers)
8763             (eq gnus-build-sparse-threads 'some)
8764             (eq gnus-build-sparse-threads 'more))
8765     ;; Deal with old-fetched headers and sparse threads.
8766     (while (and
8767             thread
8768             (or
8769              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8770              (gnus-summary-article-ancient-p
8771               (mail-header-number (car thread))))
8772             (if (or (<= (length (cdr thread)) 1)
8773                     (eq gnus-fetch-old-headers 'invisible))
8774                 (setq gnus-newsgroup-limit
8775                       (delq (mail-header-number (car thread))
8776                             gnus-newsgroup-limit)
8777                       thread (cadr thread))
8778               (when (gnus-invisible-cut-children (cdr thread))
8779                 (let ((th (cdr thread)))
8780                   (while th
8781                     (if (memq (mail-header-number (caar th))
8782                               gnus-newsgroup-limit)
8783                         (setq thread (car th)
8784                               th nil)
8785                       (setq th (cdr th))))))))))
8786   thread)
8787
8788 (defun gnus-cut-threads (threads)
8789   "Cut off all uninteresting articles from the beginning of THREADS."
8790   (when (or (eq gnus-fetch-old-headers 'some)
8791             (eq gnus-fetch-old-headers 'invisible)
8792             (numberp gnus-fetch-old-headers)
8793             (eq gnus-build-sparse-threads 'some)
8794             (eq gnus-build-sparse-threads 'more))
8795     (let ((th threads))
8796       (while th
8797         (setcar th (gnus-cut-thread (car th)))
8798         (setq th (cdr th)))))
8799   ;; Remove nixed out threads.
8800   (delq nil threads))
8801
8802 (defun gnus-summary-initial-limit (&optional show-if-empty)
8803   "Figure out what the initial limit is supposed to be on group entry.
8804 This entails weeding out unwanted dormants, low-scored articles,
8805 fetch-old-headers verbiage, and so on."
8806   ;; Most groups have nothing to remove.
8807   (unless (or gnus-inhibit-limiting
8808               (and (null gnus-newsgroup-dormant)
8809                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8810                    (not (eq gnus-fetch-old-headers 'some))
8811                    (not (numberp gnus-fetch-old-headers))
8812                    (not (eq gnus-fetch-old-headers 'invisible))
8813                    (null gnus-summary-expunge-below)
8814                    (not (eq gnus-build-sparse-threads 'some))
8815                    (not (eq gnus-build-sparse-threads 'more))
8816                    (null gnus-thread-expunge-below)))
8817     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8818     (setq gnus-newsgroup-limit nil)
8819     (mapatoms
8820      (lambda (node)
8821        (unless (car (symbol-value node))
8822          ;; These threads have no parents -- they are roots.
8823          (let ((nodes (cdr (symbol-value node)))
8824                thread)
8825            (while nodes
8826              (if (and gnus-thread-expunge-below
8827                       (< (gnus-thread-total-score (car nodes))
8828                          gnus-thread-expunge-below))
8829                  (gnus-expunge-thread (pop nodes))
8830                (setq thread (pop nodes))
8831                (gnus-summary-limit-children thread))))))
8832      gnus-newsgroup-dependencies)
8833     ;; If this limitation resulted in an empty group, we might
8834     ;; pop the previous limit and use it instead.
8835     (when (and (not gnus-newsgroup-limit)
8836                show-if-empty)
8837       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8838     gnus-newsgroup-limit))
8839
8840 (defun gnus-summary-limit-children (thread)
8841   "Return 1 if this subthread is visible and 0 if it is not."
8842   ;; First we get the number of visible children to this thread.  This
8843   ;; is done by recursing down the thread using this function, so this
8844   ;; will really go down to a leaf article first, before slowly
8845   ;; working its way up towards the root.
8846   (when thread
8847     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8848            (children
8849            (if (cdr thread)
8850                (apply '+ (mapcar 'gnus-summary-limit-children
8851                                  (cdr thread)))
8852              0))
8853            (number (mail-header-number (car thread)))
8854            score)
8855       (if (and
8856            (not (memq number gnus-newsgroup-marked))
8857            (or
8858             ;; If this article is dormant and has absolutely no visible
8859             ;; children, then this article isn't visible.
8860             (and (memq number gnus-newsgroup-dormant)
8861                  (zerop children))
8862             ;; If this is "fetch-old-headered" and there is no
8863             ;; visible children, then we don't want this article.
8864             (and (or (eq gnus-fetch-old-headers 'some)
8865                      (numberp gnus-fetch-old-headers))
8866                  (gnus-summary-article-ancient-p number)
8867                  (zerop children))
8868             ;; If this is "fetch-old-headered" and `invisible', then
8869             ;; we don't want this article.
8870             (and (eq gnus-fetch-old-headers 'invisible)
8871                  (gnus-summary-article-ancient-p number))
8872             ;; If this is a sparsely inserted article with no children,
8873             ;; we don't want it.
8874             (and (eq gnus-build-sparse-threads 'some)
8875                  (gnus-summary-article-sparse-p number)
8876                  (zerop children))
8877             ;; If we use expunging, and this article is really
8878             ;; low-scored, then we don't want this article.
8879             (when (and gnus-summary-expunge-below
8880                        (< (setq score
8881                                 (or (cdr (assq number gnus-newsgroup-scored))
8882                                     gnus-summary-default-score))
8883                           gnus-summary-expunge-below))
8884               ;; We increase the expunge-tally here, but that has
8885               ;; nothing to do with the limits, really.
8886               (incf gnus-newsgroup-expunged-tally)
8887               ;; We also mark as read here, if that's wanted.
8888               (when (and gnus-summary-mark-below
8889                          (< score gnus-summary-mark-below))
8890                 (setq gnus-newsgroup-unreads
8891                       (delq number gnus-newsgroup-unreads))
8892                 (if gnus-newsgroup-auto-expire
8893                     (push number gnus-newsgroup-expirable)
8894                   (push (cons number gnus-low-score-mark)
8895                         gnus-newsgroup-reads)))
8896               t)
8897             ;; Do the `display' group parameter.
8898             (and gnus-newsgroup-display
8899                  (let ((gnus-number number))
8900                    (not (funcall gnus-newsgroup-display))))))
8901           ;; Nope, invisible article.
8902           0
8903         ;; Ok, this article is to be visible, so we add it to the limit
8904         ;; and return 1.
8905         (push number gnus-newsgroup-limit)
8906         1))))
8907
8908 (defun gnus-expunge-thread (thread)
8909   "Mark all articles in THREAD as read."
8910   (let* ((number (mail-header-number (car thread))))
8911     (incf gnus-newsgroup-expunged-tally)
8912     ;; We also mark as read here, if that's wanted.
8913     (setq gnus-newsgroup-unreads
8914           (delq number gnus-newsgroup-unreads))
8915     (if gnus-newsgroup-auto-expire
8916         (push number gnus-newsgroup-expirable)
8917       (push (cons number gnus-low-score-mark)
8918             gnus-newsgroup-reads)))
8919   ;; Go recursively through all subthreads.
8920   (mapcar 'gnus-expunge-thread (cdr thread)))
8921
8922 ;; Summary article oriented commands
8923
8924 (defun gnus-summary-refer-parent-article (n)
8925   "Refer parent article N times.
8926 If N is negative, go to ancestor -N instead.
8927 The difference between N and the number of articles fetched is returned."
8928   (interactive "p")
8929   (let ((skip 1)
8930         error header ref)
8931     (when (not (natnump n))
8932       (setq skip (abs n)
8933             n 1))
8934     (while (and (> n 0)
8935                 (not error))
8936       (setq header (gnus-summary-article-header))
8937       (if (and (eq (mail-header-number header)
8938                    (cdr gnus-article-current))
8939                (equal gnus-newsgroup-name
8940                       (car gnus-article-current)))
8941           ;; If we try to find the parent of the currently
8942           ;; displayed article, then we take a look at the actual
8943           ;; References header, since this is slightly more
8944           ;; reliable than the References field we got from the
8945           ;; server.
8946           (with-current-buffer gnus-original-article-buffer
8947             (nnheader-narrow-to-headers)
8948             (unless (setq ref (message-fetch-field "references"))
8949               (when (setq ref (message-fetch-field "in-reply-to"))
8950                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8951             (widen))
8952         (setq ref
8953               ;; It's not the current article, so we take a bet on
8954               ;; the value we got from the server.
8955               (mail-header-references header)))
8956       (if (and ref
8957                (not (equal ref "")))
8958           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8959             (gnus-message 1 "Couldn't find parent"))
8960         (gnus-message 1 "No references in article %d"
8961                       (gnus-summary-article-number))
8962         (setq error t))
8963       (decf n))
8964     (gnus-summary-position-point)
8965     n))
8966
8967 (defun gnus-summary-refer-references ()
8968   "Fetch all articles mentioned in the References header.
8969 Return the number of articles fetched."
8970   (interactive)
8971   (let ((ref (mail-header-references (gnus-summary-article-header)))
8972         (current (gnus-summary-article-number))
8973         (n 0))
8974     (if (or (not ref)
8975             (equal ref ""))
8976         (error "No References in the current article")
8977       ;; For each Message-ID in the References header...
8978       (while (string-match "<[^>]*>" ref)
8979         (incf n)
8980         ;; ... fetch that article.
8981         (gnus-summary-refer-article
8982          (prog1 (match-string 0 ref)
8983            (setq ref (substring ref (match-end 0))))))
8984       (gnus-summary-goto-subject current)
8985       (gnus-summary-position-point)
8986       n)))
8987
8988 (defun gnus-delete-duplicate-headers (headers)
8989   ;; First remove leading duplicates.
8990   (while (and (> (length headers) 1)
8991               (= (mail-header-number (car headers))
8992                  (mail-header-number (cadr headers))))
8993     (pop headers))
8994   ;; Then the rest.
8995   (let ((result headers))
8996     (while (> (length headers) 1)
8997       (if (= (mail-header-number (car headers))
8998              (mail-header-number (cadr headers)))
8999           (setcdr headers (cddr headers))
9000         (pop headers)))
9001     result))
9002
9003 (defun gnus-summary-refer-thread (&optional limit)
9004   "Fetch all articles in the current thread. For backends that
9005 know how to search for threads (currently only 'nnimap) a
9006 non-numeric prefix arg will use nnir to search the entire
9007 server; without a prefix arg only the current group is
9008 searched. If the variable `gnus-refer-thread-use-nnir' is
9009 non-nil the prefix arg has the reverse meaning. If no
9010 backend-specific 'request-thread function is available fetch
9011 LIMIT (the numerical prefix) old headers. If LIMIT is
9012 non-numeric or nil fetch the number specified by the
9013 `gnus-refer-thread-limit' variable."
9014   (interactive "P")
9015   (gnus-warp-to-article)
9016   (let* ((header (gnus-summary-article-header))
9017          (id (mail-header-id header))
9018          (gnus-inhibit-demon t)
9019          (gnus-summary-ignore-duplicates t)
9020          (gnus-read-all-available-headers t)
9021          (gnus-refer-thread-use-nnir
9022           (if (and (not (null limit)) (listp limit))
9023               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9024          (new-headers
9025           (if (gnus-check-backend-function
9026                'request-thread gnus-newsgroup-name)
9027               (gnus-request-thread header gnus-newsgroup-name)
9028             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9029                             gnus-refer-thread-limit))
9030                    (last (if (numberp limit)
9031                              (min (+ (mail-header-number header)
9032                                      limit)
9033                                   gnus-newsgroup-highest)
9034                            gnus-newsgroup-highest))
9035                    (subject (gnus-simplify-subject
9036                              (mail-header-subject header)))
9037                    (refs (split-string (or (mail-header-references header)
9038                                            "")))
9039                    (gnus-parse-headers-hook
9040                     `(lambda () (goto-char (point-min))
9041                       (keep-lines
9042                        (regexp-opt ',(append refs (list id subject)))))))
9043               (gnus-fetch-headers (list last) (if (numberp limit)
9044                                                   (* 2 limit) limit) t))))
9045          article-ids)
9046     (when (listp new-headers)
9047       (dolist (header new-headers)
9048         (push (mail-header-number header) article-ids)
9049         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9050           (push (mail-header-number header) gnus-newsgroup-unreads)
9051           (setq gnus-newsgroup-unselected
9052                 (delete (mail-header-number header)
9053                         gnus-newsgroup-unselected))))
9054       (setq gnus-newsgroup-headers
9055             (gnus-delete-duplicate-headers
9056              (gnus-merge
9057               'list gnus-newsgroup-headers new-headers
9058               'gnus-article-sort-by-number)))
9059       (setq gnus-newsgroup-articles
9060             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9061       (gnus-summary-limit-include-thread id)))
9062   (gnus-summary-show-thread))
9063
9064 (defun gnus-summary-refer-article (message-id)
9065   "Fetch an article specified by MESSAGE-ID."
9066   (interactive "sMessage-ID: ")
9067   (gnus-warp-to-article)
9068   (when (and (stringp message-id)
9069              (not (zerop (length message-id))))
9070     (setq message-id (gnus-replace-in-string message-id " " ""))
9071     ;; Construct the correct Message-ID if necessary.
9072     ;; Suggested by tale@pawl.rpi.edu.
9073     (unless (string-match "^<" message-id)
9074       (setq message-id (concat "<" message-id)))
9075     (unless (string-match ">$" message-id)
9076       (setq message-id (concat message-id ">")))
9077     ;; People often post MIDs from URLs, so unhex it:
9078     (unless (string-match "@" message-id)
9079       (setq message-id (gnus-url-unhex-string message-id)))
9080     (let* ((header (gnus-id-to-header message-id))
9081            (sparse (and header
9082                         (gnus-summary-article-sparse-p
9083                          (mail-header-number header))
9084                         (memq (mail-header-number header)
9085                               gnus-newsgroup-limit)))
9086            number)
9087       (cond
9088        ;; If the article is present in the buffer we just go to it.
9089        ((and header
9090              (or (not (gnus-summary-article-sparse-p
9091                        (mail-header-number header)))
9092                  sparse))
9093         (prog1
9094             (gnus-summary-goto-article
9095              (mail-header-number header) nil t)
9096           (when sparse
9097             (gnus-summary-update-article (mail-header-number header)))))
9098        (t
9099         ;; We fetch the article.
9100         (catch 'found
9101           (dolist (gnus-override-method (gnus-refer-article-methods))
9102             (when (and (gnus-check-server gnus-override-method)
9103                        ;; Fetch the header,
9104                        (setq number (gnus-summary-insert-subject message-id)))
9105               ;; and display the article.
9106               (gnus-summary-select-article nil nil nil number)
9107               (throw 'found t)))
9108           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9109
9110 (defun gnus-refer-article-methods ()
9111   "Return a list of referable methods."
9112   (cond
9113    ;; No method, so we default to current and native.
9114    ((null gnus-refer-article-method)
9115     (list gnus-current-select-method gnus-select-method))
9116    ;; Current.
9117    ((eq 'current gnus-refer-article-method)
9118     (list gnus-current-select-method))
9119    ;; List of select methods.
9120    ((not (and (symbolp (car gnus-refer-article-method))
9121               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9122     (let (out)
9123       (dolist (method gnus-refer-article-method)
9124         (push (if (eq 'current method)
9125                   gnus-current-select-method
9126                 (if (eq 'nnir (car method))
9127                     (list
9128                      'nnir
9129                      (or (cadr method)
9130                          (gnus-method-to-server gnus-current-select-method)))
9131                   method))
9132               out))
9133       (nreverse out)))
9134    ;; One single select method.
9135    (t
9136     (list gnus-refer-article-method))))
9137
9138 (defun gnus-summary-edit-parameters ()
9139   "Edit the group parameters of the current group."
9140   (interactive)
9141   (gnus-group-edit-group gnus-newsgroup-name 'params))
9142
9143 (defun gnus-summary-customize-parameters ()
9144   "Customize the group parameters of the current group."
9145   (interactive)
9146   (gnus-group-customize gnus-newsgroup-name))
9147
9148 (defun gnus-summary-enter-digest-group (&optional force)
9149   "Enter an nndoc group based on the current article.
9150 If FORCE, force a digest interpretation.  If not, try to guess
9151 what the document format is.
9152
9153 To control what happens when you exit the group, see the
9154 `gnus-auto-select-on-ephemeral-exit' variable."
9155   (interactive "P")
9156   (let ((conf gnus-current-window-configuration))
9157     (save-window-excursion
9158       (save-excursion
9159         (let (gnus-article-prepare-hook
9160               gnus-display-mime-function
9161               gnus-break-pages)
9162           (gnus-summary-select-article))))
9163     (setq gnus-current-window-configuration conf)
9164     (let* ((name (format "%s-%d"
9165                          (gnus-group-prefixed-name
9166                           gnus-newsgroup-name (list 'nndoc ""))
9167                          (with-current-buffer gnus-summary-buffer
9168                            gnus-current-article)))
9169            (ogroup gnus-newsgroup-name)
9170            (params (append (gnus-info-params (gnus-get-info ogroup))
9171                            (list (cons 'to-group ogroup))
9172                            (list (cons 'parent-group ogroup))
9173                            (list (cons 'save-article-group ogroup))))
9174            (case-fold-search t)
9175            (buf (current-buffer))
9176            dig to-address)
9177       (with-current-buffer gnus-original-article-buffer
9178         ;; Have the digest group inherit the main mail address of
9179         ;; the parent article.
9180         (when (setq to-address (or (gnus-fetch-field "reply-to")
9181                                    (gnus-fetch-field "from")))
9182           (setq params
9183                 (append
9184                  (list (cons 'to-address
9185                              (funcall gnus-decode-encoded-address-function
9186                                       to-address))))))
9187         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9188         (insert-buffer-substring gnus-original-article-buffer)
9189         ;; Remove lines that may lead nndoc to misinterpret the
9190         ;; document type.
9191         (narrow-to-region
9192          (goto-char (point-min))
9193          (or (search-forward "\n\n" nil t) (point)))
9194         (goto-char (point-min))
9195         (delete-matching-lines "^Path:\\|^From ")
9196         (widen))
9197       (unwind-protect
9198           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9199                     (gnus-newsgroup-ephemeral-ignored-charsets
9200                      gnus-newsgroup-ignored-charsets))
9201                 (gnus-group-read-ephemeral-group
9202                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9203                               (nndoc-article-type
9204                                ,(if force 'mbox 'guess)))
9205                  t nil nil nil
9206                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9207                                                         "ADAPT")))))
9208               ;; Make all postings to this group go to the parent group.
9209               (nconc (gnus-info-params (gnus-get-info name))
9210                      params)
9211             ;; Couldn't select this doc group.
9212             (switch-to-buffer buf)
9213             (gnus-set-global-variables)
9214             (gnus-configure-windows 'summary)
9215             (gnus-message 3 "Article couldn't be entered?"))
9216         (kill-buffer dig)))))
9217
9218 (defun gnus-summary-read-document (n)
9219   "Open a new group based on the current article(s).
9220 This will allow you to read digests and other similar
9221 documents as newsgroups.
9222 Obeys the standard process/prefix convention."
9223   (interactive "P")
9224   (let* ((ogroup gnus-newsgroup-name)
9225          (params (append (gnus-info-params (gnus-get-info ogroup))
9226                          (list (cons 'to-group ogroup))))
9227          group egroup groups vgroup)
9228     (dolist (article (gnus-summary-work-articles n))
9229       (setq group (format "%s-%d" gnus-newsgroup-name article))
9230       (gnus-summary-remove-process-mark article)
9231       (when (gnus-summary-display-article article)
9232         (save-excursion ;;What for?
9233           (with-temp-buffer
9234             (insert-buffer-substring gnus-original-article-buffer)
9235             ;; Remove some headers that may lead nndoc to make
9236             ;; the wrong guess.
9237             (message-narrow-to-head)
9238             (goto-char (point-min))
9239             (delete-matching-lines "^Path:\\|^From ")
9240             (widen)
9241             (if (setq egroup
9242                       (gnus-group-read-ephemeral-group
9243                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9244                                      (nndoc-article-type guess))
9245                        t nil t))
9246                 (progn
9247                   ;; Make all postings to this group go to the parent group.
9248                   (nconc (gnus-info-params (gnus-get-info egroup))
9249                          params)
9250                   (push egroup groups))
9251               ;; Couldn't select this doc group.
9252               (gnus-error 3 "Article couldn't be entered"))))))
9253     ;; Now we have selected all the documents.
9254     (cond
9255      ((not groups)
9256       (error "None of the articles could be interpreted as documents"))
9257      ((gnus-group-read-ephemeral-group
9258        (setq vgroup (format
9259                      "nnvirtual:%s-%s" gnus-newsgroup-name
9260                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9261        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9262        t
9263        (cons (current-buffer) 'summary)))
9264      (t
9265       (error "Couldn't select virtual nndoc group")))))
9266
9267 (defun gnus-summary-widget-forward (arg)
9268   "Move point to the next field or button in the article.
9269 With optional ARG, move across that many fields."
9270   (interactive "p")
9271   (gnus-summary-select-article)
9272   (gnus-configure-windows 'article)
9273   (select-window (gnus-get-buffer-window gnus-article-buffer))
9274   (widget-forward arg))
9275
9276 (defun gnus-summary-widget-backward (arg)
9277   "Move point to the previous field or button in the article.
9278 With optional ARG, move across that many fields."
9279   (interactive "p")
9280   (gnus-summary-select-article)
9281   (gnus-configure-windows 'article)
9282   (select-window (gnus-get-buffer-window gnus-article-buffer))
9283   (unless (widget-at (point))
9284     (goto-char (point-max)))
9285   (widget-backward arg))
9286
9287 (defun gnus-summary-isearch-article (&optional regexp-p)
9288   "Do incremental search forward on the current article.
9289 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9290   (interactive "P")
9291   (gnus-summary-select-article)
9292   (gnus-configure-windows 'article)
9293   (gnus-eval-in-buffer-window gnus-article-buffer
9294     (save-restriction
9295       (widen)
9296       (isearch-forward regexp-p))))
9297
9298 (defun gnus-summary-repeat-search-article-forward ()
9299   "Repeat the previous search forwards."
9300   (interactive)
9301   (unless gnus-last-search-regexp
9302     (error "No previous search"))
9303   (gnus-summary-search-article-forward gnus-last-search-regexp))
9304
9305 (defun gnus-summary-repeat-search-article-backward ()
9306   "Repeat the previous search backwards."
9307   (interactive)
9308   (unless gnus-last-search-regexp
9309     (error "No previous search"))
9310   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9311
9312 (defun gnus-summary-search-article-forward (regexp &optional backward)
9313   "Search for an article containing REGEXP forward.
9314 If BACKWARD, search backward instead."
9315   (interactive
9316    (list (read-string
9317           (format "Search article %s (regexp%s): "
9318                   (if current-prefix-arg "backward" "forward")
9319                   (if gnus-last-search-regexp
9320                       (concat ", default " gnus-last-search-regexp)
9321                     "")))
9322          current-prefix-arg))
9323   (if (string-equal regexp "")
9324       (setq regexp (or gnus-last-search-regexp ""))
9325     (setq gnus-last-search-regexp regexp)
9326     (setq gnus-article-before-search gnus-current-article))
9327   ;; Intentionally set gnus-last-article.
9328   (setq gnus-last-article gnus-article-before-search)
9329   (let ((gnus-last-article gnus-last-article))
9330     (if (gnus-summary-search-article regexp backward)
9331         (gnus-summary-show-thread)
9332       (signal 'search-failed (list regexp)))))
9333
9334 (defun gnus-summary-search-article-backward (regexp)
9335   "Search for an article containing REGEXP backward."
9336   (interactive
9337    (list (read-string
9338           (format "Search article backward (regexp%s): "
9339                   (if gnus-last-search-regexp
9340                       (concat ", default " gnus-last-search-regexp)
9341                     "")))))
9342   (gnus-summary-search-article-forward regexp 'backward))
9343
9344 (defun gnus-summary-search-article (regexp &optional backward)
9345   "Search for an article containing REGEXP.
9346 Optional argument BACKWARD means do search for backward.
9347 `gnus-select-article-hook' is not called during the search."
9348   ;; We have to require this here to make sure that the following
9349   ;; dynamic binding isn't shadowed by autoloading.
9350   (require 'gnus-async)
9351   (require 'gnus-art)
9352   (let ((gnus-select-article-hook nil)  ;Disable hook.
9353         (gnus-article-prepare-hook nil)
9354         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9355         (gnus-use-article-prefetch nil)
9356         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9357         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9358         (gnus-visual nil)
9359         (gnus-keep-backlog nil)
9360         (gnus-break-pages nil)
9361         (gnus-summary-display-arrow nil)
9362         (gnus-updated-mode-lines nil)
9363         (gnus-auto-center-summary nil)
9364         (sum (current-buffer))
9365         (gnus-display-mime-function nil)
9366         (found nil)
9367         point)
9368     (gnus-save-hidden-threads
9369       (gnus-summary-select-article)
9370       (set-buffer gnus-article-buffer)
9371       (goto-char (window-point (get-buffer-window (current-buffer))))
9372       (when backward
9373         (forward-line -1))
9374       (while (not found)
9375         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9376         (if (if backward
9377                 (re-search-backward regexp nil t)
9378               (re-search-forward regexp nil t))
9379             ;; We found the regexp.
9380             (progn
9381               (setq found 'found)
9382               (beginning-of-line)
9383               (set-window-start
9384                (get-buffer-window (current-buffer))
9385                (point))
9386               (forward-line 1)
9387               (set-window-point
9388                (get-buffer-window (current-buffer))
9389                (point))
9390               (set-buffer sum)
9391               (setq point (point)))
9392           ;; We didn't find it, so we go to the next article.
9393           (set-buffer sum)
9394           (setq found 'not)
9395           (while (eq found 'not)
9396             (if (not (if backward (gnus-summary-find-prev)
9397                        (gnus-summary-find-next)))
9398                 ;; No more articles.
9399                 (setq found t)
9400               ;; Select the next article and adjust point.
9401               (unless (gnus-summary-article-sparse-p
9402                        (gnus-summary-article-number))
9403                 (setq found nil)
9404                 (gnus-summary-select-article)
9405                 (set-buffer gnus-article-buffer)
9406                 (widen)
9407                 (goto-char (if backward (point-max) (point-min))))))))
9408       (gnus-message 7 ""))
9409     ;; Return whether we found the regexp.
9410     (when (eq found 'found)
9411       (goto-char point)
9412       (gnus-summary-show-thread)
9413       (gnus-summary-goto-subject gnus-current-article)
9414       (gnus-summary-position-point)
9415       t)))
9416
9417 (defun gnus-find-matching-articles (header regexp)
9418   "Return a list of all articles that match REGEXP on HEADER.
9419 This search includes all articles in the current group that Gnus has
9420 fetched headers for, whether they are displayed or not."
9421   (let ((articles nil)
9422         ;; Can't eta-reduce because it's a macro.
9423         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9424         (case-fold-search t))
9425     (dolist (header gnus-newsgroup-headers)
9426       (when (string-match regexp (funcall func header))
9427         (push (mail-header-number header) articles)))
9428     (nreverse articles)))
9429
9430 (defun gnus-summary-find-matching (header regexp &optional backward unread
9431                                           not-case-fold not-matching)
9432   "Return a list of all articles that match REGEXP on HEADER.
9433 The search stars on the current article and goes forwards unless
9434 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9435 If UNREAD is non-nil, only unread articles will
9436 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9437 in the comparisons. If NOT-MATCHING, return a list of all articles that
9438 not match REGEXP on HEADER."
9439   (let ((case-fold-search (not not-case-fold))
9440         articles d func)
9441     (if (consp header)
9442         (if (eq (car header) 'extra)
9443             (setq func
9444                   `(lambda (h)
9445                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9446                          "")))
9447           (error "%s is an invalid header" header))
9448       (unless (fboundp (intern (concat "mail-header-" header)))
9449         (error "%s is not a valid header" header))
9450       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9451     (dolist (d (if (eq backward 'all)
9452                    gnus-newsgroup-data
9453                  (gnus-data-find-list
9454                   (gnus-summary-article-number)
9455                   (gnus-data-list backward))))
9456       (when (and (or (not unread)       ; We want all articles...
9457                      (gnus-data-unread-p d)) ; Or just unreads.
9458                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9459                  (if not-matching
9460                      (not (string-match
9461                            regexp
9462                            (funcall func (gnus-data-header d))))
9463                    (string-match regexp
9464                                  (funcall func (gnus-data-header d)))))
9465         (push (gnus-data-number d) articles))) ; Success!
9466     (nreverse articles)))
9467
9468 (defun gnus-summary-execute-command (header regexp command &optional backward)
9469   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9470 If HEADER is an empty string (or nil), the match is done on the entire
9471 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9472   (interactive
9473    (list (let ((completion-ignore-case t))
9474            (gnus-completing-read
9475             "Header name"
9476             (mapcar 'symbol-name
9477                     (append
9478                      '(Number Subject From Lines Date
9479                        Message-ID Xref References Body)
9480                      gnus-extra-headers))
9481             'require-match))
9482          (read-string "Regexp: ")
9483          (read-key-sequence "Command: ")
9484          current-prefix-arg))
9485   (when (equal header "Body")
9486     (setq header ""))
9487   ;; Hidden thread subtrees must be searched as well.
9488   (gnus-summary-show-all-threads)
9489   ;; We don't want to change current point nor window configuration.
9490   (save-excursion
9491     (save-window-excursion
9492       (let (gnus-visual
9493             gnus-treat-strip-trailing-blank-lines
9494             gnus-treat-strip-leading-blank-lines
9495             gnus-treat-strip-multiple-blank-lines
9496             gnus-treat-hide-boring-headers
9497             gnus-treat-fold-newsgroups
9498             gnus-article-prepare-hook)
9499         (gnus-message 6 "Executing %s..." (key-description command))
9500         ;; We'd like to execute COMMAND interactively so as to give arguments.
9501         (gnus-execute header regexp
9502                       `(call-interactively ',(key-binding command))
9503                       backward)
9504         (gnus-message 6 "Executing %s...done" (key-description command))))))
9505
9506 (defun gnus-summary-beginning-of-article ()
9507   "Scroll the article back to the beginning."
9508   (interactive)
9509   (gnus-summary-select-article)
9510   (gnus-configure-windows 'article)
9511   (gnus-eval-in-buffer-window gnus-article-buffer
9512     (widen)
9513     (goto-char (point-min))
9514     (when gnus-break-pages
9515       (gnus-narrow-to-page))))
9516
9517 (defun gnus-summary-end-of-article ()
9518   "Scroll to the end of the article."
9519   (interactive)
9520   (gnus-summary-select-article)
9521   (gnus-configure-windows 'article)
9522   (gnus-eval-in-buffer-window gnus-article-buffer
9523     (widen)
9524     (goto-char (point-max))
9525     (recenter -3)
9526     (when gnus-break-pages
9527       (gnus-narrow-to-page))))
9528
9529 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9530   "Truncate to LEN and quote all \"(\"'s in STRING."
9531   (gnus-replace-in-string (if (and len (> (length string) len))
9532                               (substring string 0 len)
9533                             string)
9534                           "[()]" "\\\\\\&"))
9535
9536 (defun gnus-summary-print-article (&optional filename n)
9537   "Generate and print a PostScript image of the process-marked (mail) articles.
9538
9539 If used interactively, print the current article if none are
9540 process-marked.  With prefix arg, prompt the user for the name of the
9541 file to save in.
9542
9543 When used from Lisp, accept two optional args FILENAME and N.  N means
9544 to print the next N articles.  If N is negative, print the N previous
9545 articles.  If N is nil and articles have been marked with the process
9546 mark, print these instead.
9547
9548 If the optional first argument FILENAME is nil, send the image to the
9549 printer.  If FILENAME is a string, save the PostScript image in a file with
9550 that name.  If FILENAME is a number, prompt the user for the name of the file
9551 to save in."
9552   (interactive (list (ps-print-preprint current-prefix-arg)))
9553   (dolist (article (gnus-summary-work-articles n))
9554     (gnus-summary-select-article nil nil 'pseudo article)
9555     (gnus-eval-in-buffer-window gnus-article-buffer
9556       (gnus-print-buffer))
9557     (gnus-summary-remove-process-mark article))
9558   (ps-despool filename))
9559
9560 (defun gnus-print-buffer ()
9561   (let ((ps-left-header
9562          (list
9563           (concat "("
9564                   (gnus-summary-print-truncate-and-quote
9565                    (mail-header-subject gnus-current-headers)
9566                    66) ")")
9567           (concat "("
9568                   (gnus-summary-print-truncate-and-quote
9569                    (mail-header-from gnus-current-headers)
9570                    45) ")")))
9571         (ps-right-header
9572          (list
9573           "/pagenumberstring load"
9574           (concat "("
9575                   (mail-header-date gnus-current-headers) ")"))))
9576     (gnus-run-hooks 'gnus-ps-print-hook)
9577     (save-excursion
9578       (if ps-print-color-p
9579           (ps-spool-buffer-with-faces)
9580         (ps-spool-buffer)))))
9581
9582 (defun gnus-summary-show-complete-article ()
9583   "Show a complete version of the current article.
9584 This is only useful if you're looking at a partial version of the
9585 article currently."
9586   (interactive)
9587   (let ((gnus-keep-backlog nil)
9588         (gnus-use-cache nil)
9589         (gnus-agent nil)
9590         (variable (intern
9591                    (format "%s-fetch-partial-articles"
9592                            (car (gnus-find-method-for-group
9593                                  gnus-newsgroup-name)))
9594                    obarray))
9595         old-val)
9596     (unwind-protect
9597         (progn
9598           (setq old-val (symbol-value variable))
9599           (set variable nil)
9600           (gnus-flush-original-article-buffer)
9601           (gnus-summary-show-article))
9602       (set variable old-val))))
9603
9604 (defun gnus-summary-show-article (&optional arg)
9605   "Force redisplaying of the current article.
9606 If ARG (the prefix) is a number, show the article with the charset
9607 defined in `gnus-summary-show-article-charset-alist', or the charset
9608 input.
9609 If ARG (the prefix) is non-nil and not a number, show the article,
9610 but without running any of the article treatment functions
9611 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9612 C-u g', show the raw article."
9613   (interactive "P")
9614   (cond
9615    ((numberp arg)
9616     (gnus-summary-show-article t)
9617     (let ((gnus-newsgroup-charset
9618            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9619                (mm-read-coding-system
9620                 "View as charset: " ;; actually it is coding system.
9621                 (with-current-buffer gnus-article-buffer
9622                   (mm-detect-coding-region (point) (point-max))))))
9623           (gnus-newsgroup-ignored-charsets 'gnus-all))
9624       (gnus-summary-select-article nil 'force)
9625       (let ((deps gnus-newsgroup-dependencies)
9626             head header lines)
9627         (with-current-buffer gnus-original-article-buffer
9628           (save-restriction
9629             (message-narrow-to-head)
9630             (setq head (buffer-string))
9631             (goto-char (point-min))
9632             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9633               (goto-char (point-max))
9634               (widen)
9635               (setq lines (1- (count-lines (point) (point-max))))))
9636           (with-temp-buffer
9637             (insert (format "211 %d Article retrieved.\n"
9638                             (cdr gnus-article-current)))
9639             (insert head)
9640             (if lines (insert (format "Lines: %d\n" lines)))
9641             (insert ".\n")
9642             (let ((nntp-server-buffer (current-buffer)))
9643               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9644         (gnus-data-set-header
9645          (gnus-data-find (cdr gnus-article-current))
9646          header)
9647         (gnus-summary-update-article-line
9648          (cdr gnus-article-current) header)
9649         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9650           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9651    ((not arg)
9652     ;; Select the article the normal way.
9653     (if (eq mm-text-html-renderer 'shr)
9654         (progn
9655           (require 'shr)
9656           (let ((shr-ignore-cache t))
9657             (gnus-summary-select-article nil 'force)))
9658       (gnus-summary-select-article nil 'force)))
9659    ((equal arg '(16))
9660     ;; C-u C-u g
9661     (let ((gnus-inhibit-article-treatments t))
9662       (gnus-summary-select-article nil 'force)))
9663    (t
9664     ;; We have to require this here to make sure that the following
9665     ;; dynamic binding isn't shadowed by autoloading.
9666     (require 'gnus-async)
9667     (require 'gnus-art)
9668     ;; Bind the article treatment functions to nil.
9669     (let ((gnus-have-all-headers t)
9670           gnus-article-prepare-hook
9671           gnus-article-decode-hook
9672           gnus-display-mime-function
9673           gnus-break-pages)
9674       ;; Destroy any MIME parts.
9675       (when (gnus-buffer-live-p gnus-article-buffer)
9676         (with-current-buffer gnus-article-buffer
9677           (gnus-article-stop-animations)
9678           (gnus-stop-downloads)
9679           (mm-destroy-parts gnus-article-mime-handles)
9680           ;; Set it to nil for safety reason.
9681           (setq gnus-article-mime-handle-alist nil)
9682           (setq gnus-article-mime-handles nil)))
9683       (gnus-summary-select-article nil 'force))))
9684   (gnus-summary-goto-subject gnus-current-article)
9685   (gnus-summary-position-point))
9686
9687 (defun gnus-summary-show-raw-article ()
9688   "Show the raw article without any article massaging functions being run."
9689   (interactive)
9690   (gnus-summary-show-article t))
9691
9692 (defun gnus-summary-verbose-headers (&optional arg)
9693   "Toggle permanent full header display.
9694 If ARG is a positive number, turn header display on.
9695 If ARG is a negative number, turn header display off."
9696   (interactive "P")
9697   (setq gnus-show-all-headers
9698         (cond ((or (not (numberp arg))
9699                    (zerop arg))
9700                (not gnus-show-all-headers))
9701               ((natnump arg)
9702                t)))
9703   (gnus-summary-show-article))
9704
9705 (defun gnus-summary-toggle-header (&optional arg)
9706   "Show the headers if they are hidden, or hide them if they are shown.
9707 If ARG is a positive number, show the entire header.
9708 If ARG is a negative number, hide the unwanted header lines."
9709   (interactive "P")
9710   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9711                      (get-buffer-window gnus-article-buffer t))))
9712     (with-current-buffer gnus-article-buffer
9713       (widen)
9714       (article-narrow-to-head)
9715       (let* ((inhibit-read-only t)
9716              (inhibit-point-motion-hooks t)
9717              (hidden (if (numberp arg)
9718                          (>= arg 0)
9719                        (or (not (looking-at "[^ \t\n]+:"))
9720                            (gnus-article-hidden-text-p 'headers))))
9721              s e)
9722         (delete-region (point-min) (point-max))
9723         (with-current-buffer gnus-original-article-buffer
9724           (goto-char (setq s (point-min)))
9725           (setq e (if (search-forward "\n\n" nil t)
9726                       (1- (point))
9727                     (point-max))))
9728         (insert-buffer-substring gnus-original-article-buffer s e)
9729         (run-hooks 'gnus-article-decode-hook)
9730         (if hidden
9731             (let ((gnus-treat-hide-headers nil)
9732                   (gnus-treat-hide-boring-headers nil))
9733               (gnus-delete-wash-type 'headers)
9734               (gnus-treat-article 'head))
9735           (gnus-treat-article 'head))
9736         (widen)
9737         (if window
9738             (set-window-start window (goto-char (point-min))))
9739         (if gnus-break-pages
9740             (gnus-narrow-to-page)
9741           (when (gnus-visual-p 'page-marker)
9742             (let ((inhibit-read-only t))
9743               (gnus-remove-text-with-property 'gnus-prev)
9744               (gnus-remove-text-with-property 'gnus-next))))
9745         (gnus-set-mode-line 'article)))))
9746
9747 (defun gnus-summary-show-all-headers ()
9748   "Make all header lines visible."
9749   (interactive)
9750   (gnus-summary-toggle-header 1))
9751
9752 (defun gnus-summary-caesar-message (&optional arg)
9753   "Caesar rotate the current article by 13.
9754 With a non-numerical prefix, also rotate headers.  A numerical
9755 prefix specifies how many places to rotate each letter forward."
9756   (interactive "P")
9757   (gnus-summary-select-article)
9758   (let ((mail-header-separator ""))
9759     (gnus-eval-in-buffer-window gnus-article-buffer
9760       (save-restriction
9761         (widen)
9762         (let ((start (window-start))
9763               (inhibit-read-only t))
9764           (if (equal arg '(4))
9765               (message-caesar-buffer-body nil t)
9766             (message-caesar-buffer-body arg))
9767           (set-window-start (get-buffer-window (current-buffer)) start)))))
9768   ;; Create buttons and stuff...
9769   (gnus-treat-article nil))
9770
9771 (declare-function idna-to-unicode "ext:idna" (str))
9772
9773 (defun gnus-summary-idna-message (&optional arg)
9774   "Decode IDNA encoded domain names in the current articles.
9775 IDNA encoded domain names looks like `xn--bar'.  If a string
9776 remain unencoded after running this function, it is likely an
9777 invalid IDNA string (`xn--bar' is invalid).
9778
9779 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9780 installed for this command to work."
9781   (interactive "P")
9782   (if (not (and (condition-case nil (require 'idna)
9783                   (file-error))
9784                 (mm-coding-system-p 'utf-8)
9785                 (executable-find (symbol-value 'idna-program))))
9786       (gnus-message
9787        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9788     (gnus-summary-select-article)
9789     (let ((mail-header-separator ""))
9790       (gnus-eval-in-buffer-window gnus-article-buffer
9791         (save-restriction
9792           (widen)
9793           (let ((start (window-start))
9794                 buffer-read-only)
9795             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9796               (replace-match (idna-to-unicode (match-string 1))))
9797             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9798
9799 (defun gnus-summary-morse-message (&optional arg)
9800   "Morse decode the current article."
9801   (interactive "P")
9802   (gnus-summary-select-article)
9803   (let ((mail-header-separator ""))
9804     (gnus-eval-in-buffer-window gnus-article-buffer
9805       (save-excursion
9806         (save-restriction
9807           (widen)
9808           (let ((pos (window-start))
9809                 (inhibit-read-only t))
9810             (goto-char (point-min))
9811             (when (message-goto-body)
9812               (gnus-narrow-to-body))
9813             (goto-char (point-min))
9814             (while (search-forward "·" (point-max) t)
9815               (replace-match "."))
9816             (unmorse-region (point-min) (point-max))
9817             (widen)
9818             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9819
9820 (defun gnus-summary-stop-page-breaking ()
9821   "Stop page breaking in the current article."
9822   (interactive)
9823   (gnus-summary-select-article)
9824   (gnus-eval-in-buffer-window gnus-article-buffer
9825     (widen)
9826     (when (gnus-visual-p 'page-marker)
9827       (let ((inhibit-read-only t))
9828         (gnus-remove-text-with-property 'gnus-prev)
9829         (gnus-remove-text-with-property 'gnus-next))
9830       (setq gnus-page-broken nil))))
9831
9832 (defun gnus-summary-move-article (&optional n to-newsgroup
9833                                             select-method action)
9834   "Move the current article to a different newsgroup.
9835 If N is a positive number, move the N next articles.
9836 If N is a negative number, move the N previous articles.
9837 If N is nil and any articles have been marked with the process mark,
9838 move those articles instead.
9839 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9840 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9841 re-spool using this method.
9842
9843 When called interactively with TO-NEWSGROUP being nil, the value of
9844 the variable `gnus-move-split-methods' is used for finding a default
9845 for the target newsgroup.
9846
9847 For this function to work, both the current newsgroup and the
9848 newsgroup that you want to move to have to support the `request-move'
9849 and `request-accept' functions.
9850
9851 ACTION can be either `move' (the default), `crosspost' or `copy'."
9852   (interactive "P")
9853   (unless action
9854     (setq action 'move))
9855   ;; Check whether the source group supports the required functions.
9856   (cond ((and (eq action 'move)
9857               (not (gnus-check-backend-function
9858                     'request-move-article gnus-newsgroup-name)))
9859          (error "The current group does not support article moving"))
9860         ((and (eq action 'crosspost)
9861               (not (gnus-check-backend-function
9862                     'request-replace-article gnus-newsgroup-name)))
9863          (error "The current group does not support article editing")))
9864   (let ((articles (gnus-summary-work-articles n))
9865         (prefix (if (gnus-check-backend-function
9866                      'request-move-article gnus-newsgroup-name)
9867                     (funcall gnus-move-group-prefix-function
9868                              gnus-newsgroup-name)
9869                   ""))
9870         (names '((move "Move" "Moving")
9871                  (copy "Copy" "Copying")
9872                  (crosspost "Crosspost" "Crossposting")))
9873         (copy-buf (save-excursion
9874                     (nnheader-set-temp-buffer " *copy article*")))
9875         art-group to-method new-xref article to-groups
9876         articles-to-update-marks encoded)
9877     (unless (assq action names)
9878       (error "Unknown action %s" action))
9879     ;; Read the newsgroup name.
9880     (when (and (not to-newsgroup)
9881                (not select-method))
9882       (if (and gnus-move-split-methods
9883                (not
9884                 (and (memq gnus-current-article articles)
9885                      (gnus-buffer-live-p gnus-original-article-buffer))))
9886           ;; When `gnus-move-split-methods' is non-nil, we have to
9887           ;; select an article to give `gnus-read-move-group-name' an
9888           ;; opportunity to suggest an appropriate default.  However,
9889           ;; we needn't render or mark the article.
9890           (let ((gnus-display-mime-function nil)
9891                 (gnus-article-prepare-hook nil)
9892                 (gnus-mark-article-hook nil))
9893             (gnus-summary-select-article nil nil nil (car articles))))
9894       (setq to-newsgroup (gnus-read-move-group-name
9895                           (cadr (assq action names))
9896                           (symbol-value
9897                            (intern (format "gnus-current-%s-group" action)))
9898                           articles prefix)
9899             encoded to-newsgroup
9900             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9901       (set (intern (format "gnus-current-%s-group" action))
9902            (mm-decode-coding-string
9903             to-newsgroup
9904             (gnus-group-name-charset to-method to-newsgroup))))
9905     (unless to-method
9906       (setq to-method (or select-method
9907                           (gnus-server-to-method
9908                            (gnus-group-method to-newsgroup)))))
9909     (setq to-newsgroup
9910           (or encoded
9911               (and to-newsgroup
9912                    (mm-encode-coding-string
9913                     to-newsgroup
9914                     (gnus-group-name-charset to-method to-newsgroup)))))
9915     ;; Check the method we are to move this article to...
9916     (unless (gnus-check-backend-function
9917              'request-accept-article (car to-method))
9918       (error "%s does not support article copying" (car to-method)))
9919     (unless (gnus-check-server to-method)
9920       (error "Can't open server %s" (car to-method)))
9921     (gnus-message 6 "%s to %s: %s..."
9922                   (caddr (assq action names))
9923                   (or (car select-method)
9924                       (gnus-group-decoded-name to-newsgroup))
9925                   articles)
9926     (while articles
9927       (setq article (pop articles))
9928       ;; Set any marks that may have changed in the summary buffer.
9929       (when gnus-preserve-marks
9930         (gnus-summary-push-marks-to-backend article))
9931       (setq
9932        art-group
9933        (cond
9934         ;; Move the article.
9935         ((eq action 'move)
9936          ;; Remove this article from future suppression.
9937          (gnus-dup-unsuppress-article article)
9938          (let* ((from-method (gnus-find-method-for-group
9939                               gnus-newsgroup-name))
9940                 (to-method (or select-method
9941                                (gnus-find-method-for-group to-newsgroup)))
9942                 (move-is-internal (gnus-server-equal from-method to-method)))
9943            (gnus-request-move-article
9944             article                     ; Article to move
9945             gnus-newsgroup-name         ; From newsgroup
9946             (nth 1 (gnus-find-method-for-group
9947                     gnus-newsgroup-name)) ; Server
9948             (list 'gnus-request-accept-article
9949                   to-newsgroup (list 'quote select-method)
9950                   (not articles) t)     ; Accept form
9951             (not articles)              ; Only save nov last time
9952             (and move-is-internal
9953                  to-newsgroup           ; Not respooling
9954                                         ; Is this move internal?
9955                  (gnus-group-real-name to-newsgroup)))))
9956         ;; Copy the article.
9957         ((eq action 'copy)
9958          (with-current-buffer copy-buf
9959            (when (gnus-request-article-this-buffer article
9960                                                    gnus-newsgroup-name)
9961              (save-restriction
9962                (nnheader-narrow-to-headers)
9963                (dolist (hdr gnus-copy-article-ignored-headers)
9964                  (message-remove-header hdr t)))
9965              (gnus-request-accept-article
9966               to-newsgroup select-method (not articles) t))))
9967         ;; Crosspost the article.
9968         ((eq action 'crosspost)
9969          (let ((xref (message-tokenize-header
9970                       (mail-header-xref (gnus-summary-article-header
9971                                          article))
9972                       " ")))
9973            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9974                                   ":" (number-to-string article)))
9975            (unless xref
9976              (setq xref (list (system-name))))
9977            (setq new-xref
9978                  (concat
9979                   (mapconcat 'identity
9980                              (delete "Xref:" (delete new-xref xref))
9981                              " ")
9982                   " " new-xref))
9983            (with-current-buffer copy-buf
9984              ;; First put the article in the destination group.
9985              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9986              (when (consp (setq art-group
9987                                 (gnus-request-accept-article
9988                                  to-newsgroup select-method (not articles)
9989                                  t)))
9990                (setq new-xref (concat new-xref " " (car art-group)
9991                                       ":"
9992                                       (number-to-string (cdr art-group))))
9993                ;; Now we have the new Xrefs header, so we insert
9994                ;; it and replace the new article.
9995                (nnheader-replace-header "Xref" new-xref)
9996                (gnus-request-replace-article
9997                 (cdr art-group) to-newsgroup (current-buffer) t)
9998                art-group))))))
9999       (cond
10000        ((not art-group)
10001         (gnus-message 1 "Couldn't %s article %s: %s"
10002                       (cadr (assq action names)) article
10003                       (nnheader-get-report (car to-method))))
10004        ((eq art-group 'junk)
10005         (when (eq action 'move)
10006           (gnus-summary-mark-article article gnus-canceled-mark)
10007           (gnus-message 4 "Deleted article %s" article)
10008           ;; run the delete hook
10009           (run-hook-with-args 'gnus-summary-article-delete-hook
10010                               action
10011                               (gnus-data-header
10012                                (assoc article (gnus-data-list nil)))
10013                               gnus-newsgroup-name nil
10014                               select-method)))
10015        (t
10016         (let* ((pto-group (gnus-group-prefixed-name
10017                            (car art-group) to-method))
10018                (info (gnus-get-info pto-group))
10019                (to-group (gnus-info-group info))
10020                to-marks)
10021           ;; Update the group that has been moved to.
10022           (when (and info
10023                      (memq action '(move copy)))
10024             (unless (member to-group to-groups)
10025               (push to-group to-groups))
10026
10027             (when (and (not (memq article gnus-newsgroup-unreads))
10028                        (cdr art-group))
10029               (push 'read to-marks)
10030               (gnus-info-set-read
10031                info (gnus-add-to-range (gnus-info-read info)
10032                                        (list (cdr art-group)))))
10033
10034             ;; See whether the article is to be put in the cache.
10035             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10036                    (marks (if expirable
10037                               gnus-article-mark-lists
10038                             (delete '(expirable . expire)
10039                                     (copy-sequence
10040                                      gnus-article-mark-lists))))
10041                    (to-article (cdr art-group)))
10042
10043               ;; Enter the article into the cache in the new group,
10044               ;; if that is required.
10045               (when (and to-article
10046                          gnus-use-cache)
10047                 (gnus-cache-possibly-enter-article
10048                  to-group to-article
10049                  (memq article gnus-newsgroup-marked)
10050                  (memq article gnus-newsgroup-dormant)
10051                  (memq article gnus-newsgroup-unreads)))
10052
10053               (when (and gnus-preserve-marks
10054                          to-article)
10055                 ;; Copy any marks over to the new group.
10056                 (when (and (equal to-group gnus-newsgroup-name)
10057                            (not (memq article gnus-newsgroup-unreads)))
10058                   ;; Mark this article as read in this group.
10059                   (push (cons to-article gnus-read-mark)
10060                         gnus-newsgroup-reads)
10061                   ;; Increase the active status of this group.
10062                   (setcdr (gnus-active to-group) to-article)
10063                   (setcdr gnus-newsgroup-active to-article))
10064
10065                 (while marks
10066                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10067                     (when (memq article (symbol-value
10068                                          (intern (format "gnus-newsgroup-%s"
10069                                                          (caar marks)))))
10070                       (push (cdar marks) to-marks)
10071                       ;; If the other group is the same as this group,
10072                       ;; then we have to add the mark to the list.
10073                       (when (equal to-group gnus-newsgroup-name)
10074                         (set (intern (format "gnus-newsgroup-%s"
10075                                              (caar marks)))
10076                              (cons to-article
10077                                    (symbol-value
10078                                     (intern (format "gnus-newsgroup-%s"
10079                                                     (caar marks)))))))
10080                       ;; Copy the marks to other group.
10081                       (gnus-add-marked-articles
10082                        to-group (cdar marks) (list to-article) info)))
10083                   (setq marks (cdr marks)))
10084
10085                 (when (and expirable
10086                            gnus-mark-copied-or-moved-articles-as-expirable
10087                            (not (memq 'expire to-marks)))
10088                   ;; Mark this article as expirable.
10089                   (push 'expire to-marks)
10090                   (when (equal to-group gnus-newsgroup-name)
10091                     (push to-article gnus-newsgroup-expirable))
10092                   ;; Copy the expirable mark to other group.
10093                   (gnus-add-marked-articles
10094                    to-group 'expire (list to-article) info))
10095
10096                 (when (and to-marks
10097                            (gnus-method-option-p
10098                             (gnus-find-method-for-group to-group)
10099                             'server-marks))
10100                   (gnus-request-set-mark
10101                    to-group (list (list (list to-article) 'add to-marks)))))
10102
10103               (gnus-dribble-enter
10104                (concat "(gnus-group-set-info '"
10105                        (gnus-prin1-to-string (gnus-get-info to-group))
10106                        ")")
10107                (concat "^(gnus-group-set-info '(\""
10108                        (regexp-quote to-group) "\""))))
10109
10110           ;; Update the Xref header in this article to point to
10111           ;; the new crossposted article we have just created.
10112           (when (eq action 'crosspost)
10113             (with-current-buffer copy-buf
10114               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10115               (nnheader-replace-header "Xref" new-xref)
10116               (gnus-request-replace-article
10117                article gnus-newsgroup-name (current-buffer) t)))
10118
10119           ;; run the move/copy/crosspost/respool hook
10120           (run-hook-with-args 'gnus-summary-article-move-hook
10121                               action
10122                               (gnus-data-header
10123                                (assoc article (gnus-data-list nil)))
10124                               gnus-newsgroup-name
10125                               to-newsgroup
10126                               select-method))
10127
10128         ;;;!!!Why is this necessary?
10129         (set-buffer gnus-summary-buffer)
10130
10131         (when (eq action 'move)
10132           (save-excursion
10133             (gnus-summary-goto-subject article)
10134             (gnus-summary-mark-article article gnus-canceled-mark)))))
10135       (push article articles-to-update-marks))
10136
10137     (save-excursion
10138       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10139     ;; Re-activate all groups that have been moved to.
10140     (with-current-buffer gnus-group-buffer
10141       (let ((gnus-group-marked to-groups))
10142         (gnus-group-get-new-news-this-group nil t)))
10143
10144     (gnus-kill-buffer copy-buf)
10145     (gnus-summary-position-point)
10146     (gnus-set-mode-line 'summary)))
10147
10148 (defun gnus-summary-push-marks-to-backend (article)
10149   (let ((set nil)
10150         (marks gnus-article-mark-lists))
10151     (unless (memq article gnus-newsgroup-unreads)
10152       (push 'read set))
10153     (while marks
10154       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10155                  (memq article (symbol-value
10156                                 (intern (format "gnus-newsgroup-%s"
10157                                                 (caar marks))))))
10158         (push (cdar marks) set))
10159       (pop marks))
10160     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
10161
10162 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10163   "Copy the current article to some other group.
10164 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10165 When called interactively, if TO-NEWSGROUP is nil, use the value of
10166 the variable `gnus-move-split-methods' for finding a default target
10167 newsgroup.
10168 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10169 re-spool using this method."
10170   (interactive "P")
10171   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10172
10173 (defun gnus-summary-crosspost-article (&optional n)
10174   "Crosspost the current article to some other group."
10175   (interactive "P")
10176   (gnus-summary-move-article n nil nil 'crosspost))
10177
10178 (defcustom gnus-summary-respool-default-method nil
10179   "Default method type for respooling an article.
10180 If nil, use to the current newsgroup method."
10181   :type 'symbol
10182   :group 'gnus-summary-mail)
10183
10184 (defun gnus-summary-respool-article (&optional n method)
10185   "Respool the current article.
10186 The article will be squeezed through the mail spooling process again,
10187 which means that it will be put in some mail newsgroup or other
10188 depending on `nnmail-split-methods'.
10189 If N is a positive number, respool the N next articles.
10190 If N is a negative number, respool the N previous articles.
10191 If N is nil and any articles have been marked with the process mark,
10192 respool those articles instead.
10193
10194 Respooling can be done both from mail groups and \"real\" newsgroups.
10195 In the former case, the articles in question will be moved from the
10196 current group into whatever groups they are destined to.  In the
10197 latter case, they will be copied into the relevant groups."
10198   (interactive
10199    (list current-prefix-arg
10200          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10201                 (methname
10202                  (symbol-name (or gnus-summary-respool-default-method
10203                                   (car (gnus-find-method-for-group
10204                                         gnus-newsgroup-name)))))
10205                 (method
10206                  (gnus-completing-read
10207                   "Backend to use when respooling"
10208                   methods t nil 'gnus-mail-method-history methname))
10209                 ms)
10210            (cond
10211             ((zerop (length (setq ms (gnus-servers-using-backend
10212                                       (intern method)))))
10213              (list (intern method) ""))
10214             ((= 1 (length ms))
10215              (car ms))
10216             (t
10217              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10218                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10219                            ms-alist))))))))
10220   (unless method
10221     (error "No method given for respooling"))
10222   (if (assoc (symbol-name
10223               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10224              (gnus-methods-using 'respool))
10225       (gnus-summary-move-article n nil method)
10226     (gnus-summary-copy-article n nil method)))
10227
10228 (defun gnus-summary-import-article (file &optional edit)
10229   "Import an arbitrary file into a mail newsgroup."
10230   (interactive "fImport file: \nP")
10231   (let ((group gnus-newsgroup-name)
10232         (now (current-time))
10233         atts lines group-art)
10234     (unless (gnus-check-backend-function 'request-accept-article group)
10235       (error "%s does not support article importing" group))
10236     (or (file-readable-p file)
10237         (not (file-regular-p file))
10238         (error "Can't read %s" file))
10239     (with-current-buffer (gnus-get-buffer-create " *import file*")
10240       (erase-buffer)
10241       (nnheader-insert-file-contents file)
10242       (goto-char (point-min))
10243       (if (nnheader-article-p)
10244           (save-restriction
10245             (goto-char (point-min))
10246             (search-forward "\n\n" nil t)
10247             (narrow-to-region (point-min) (1- (point)))
10248             (goto-char (point-min))
10249             (unless (re-search-forward "^date:" nil t)
10250               (goto-char (point-max))
10251               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10252        ;; This doesn't look like an article, so we fudge some headers.
10253         (setq atts (file-attributes file)
10254               lines (count-lines (point-min) (point-max)))
10255         (insert "From: " (read-string "From: ") "\n"
10256                 "Subject: " (read-string "Subject: ") "\n"
10257                 "Date: " (message-make-date (nth 5 atts)) "\n"
10258                 "Message-ID: " (message-make-message-id) "\n"
10259                 "Lines: " (int-to-string lines) "\n"
10260                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10261       (setq group-art (gnus-request-accept-article group nil t))
10262       (kill-buffer (current-buffer)))
10263     (setq gnus-newsgroup-active (gnus-activate-group group))
10264     (forward-line 1)
10265     (gnus-summary-goto-article (cdr group-art) nil t)
10266     (when edit
10267       (gnus-summary-edit-article))))
10268
10269 (defun gnus-summary-create-article ()
10270   "Create an article in a mail newsgroup."
10271   (interactive)
10272   (let ((group gnus-newsgroup-name)
10273         (now (current-time))
10274         group-art)
10275     (unless (gnus-check-backend-function 'request-accept-article group)
10276       (error "%s does not support article importing" group))
10277     (with-current-buffer (gnus-get-buffer-create " *import file*")
10278       (erase-buffer)
10279       (goto-char (point-min))
10280       ;; This doesn't look like an article, so we fudge some headers.
10281       (insert "From: " (read-string "From: ") "\n"
10282               "Subject: " (read-string "Subject: ") "\n"
10283               "Date: " (message-make-date now) "\n"
10284               "Message-ID: " (message-make-message-id) "\n")
10285       (setq group-art (gnus-request-accept-article group nil t))
10286       (kill-buffer (current-buffer)))
10287     (setq gnus-newsgroup-active (gnus-activate-group group))
10288     (forward-line 1)
10289     (gnus-summary-goto-article (cdr group-art) nil t)
10290     (gnus-summary-edit-article)))
10291
10292 (defun gnus-summary-article-posted-p ()
10293   "Say whether the current (mail) article is available from news as well.
10294 This will be the case if the article has both been mailed and posted."
10295   (interactive)
10296   (let ((id (mail-header-references (gnus-summary-article-header)))
10297         (gnus-override-method (car (gnus-refer-article-methods))))
10298     (if (gnus-request-head id "")
10299         (gnus-message 2 "The current message was found on %s"
10300                       gnus-override-method)
10301       (gnus-message 2 "The current message couldn't be found on %s"
10302                     gnus-override-method)
10303       nil)))
10304
10305 (defun gnus-summary-expire-articles (&optional now)
10306   "Expire all articles that are marked as expirable in the current group."
10307   (interactive)
10308   (when (and (not gnus-group-is-exiting-without-update-p)
10309              (gnus-check-backend-function
10310               'request-expire-articles gnus-newsgroup-name))
10311     ;; This backend supports expiry.
10312     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10313            (expirable
10314             (gnus-list-range-difference
10315              (if total
10316                  (progn
10317                    ;; We need to update the info for
10318                    ;; this group for `gnus-list-of-read-articles'
10319                    ;; to give us the right answer.
10320                    (gnus-run-hooks 'gnus-exit-group-hook)
10321                    (gnus-summary-update-info)
10322                    (gnus-list-of-read-articles gnus-newsgroup-name))
10323                (setq gnus-newsgroup-expirable
10324                      (sort gnus-newsgroup-expirable '<)))
10325              gnus-newsgroup-unexist))
10326            (expiry-wait (if now 'immediate
10327                           (gnus-group-find-parameter
10328                            gnus-newsgroup-name 'expiry-wait)))
10329            (nnmail-expiry-target
10330             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10331                 nnmail-expiry-target))
10332            es)
10333       (when expirable
10334         ;; There are expirable articles in this group, so we run them
10335         ;; through the expiry process.
10336         (gnus-message 6 "Expiring articles...")
10337         (when (gnus-check-group gnus-newsgroup-name)
10338           ;; The list of articles that weren't expired is returned.
10339           (save-excursion
10340             (if expiry-wait
10341                 (let ((nnmail-expiry-wait-function nil)
10342                       (nnmail-expiry-wait expiry-wait))
10343                   (setq es (gnus-request-expire-articles
10344                             expirable gnus-newsgroup-name)))
10345               (setq es (gnus-request-expire-articles
10346                         expirable gnus-newsgroup-name)))
10347             (unless total
10348               (setq gnus-newsgroup-expirable es))
10349             ;; We go through the old list of expirable, and mark all
10350             ;; really expired articles as nonexistent.
10351             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10352               (let ((gnus-use-cache nil))
10353                 (dolist (article expirable)
10354                   (when (and (not (memq article es))
10355                              (gnus-data-find article))
10356                     (gnus-summary-mark-article article gnus-canceled-mark)
10357                     (run-hook-with-args 'gnus-summary-article-expire-hook
10358                                         'delete
10359                                         (gnus-data-header
10360                                          (assoc article (gnus-data-list nil)))
10361                                         gnus-newsgroup-name
10362                                         nil
10363                                         nil)))))))
10364         (gnus-message 6 "Expiring articles...done")))))
10365
10366 (defun gnus-summary-expire-articles-now ()
10367   "Expunge all expirable articles in the current group.
10368 This means that *all* articles that are marked as expirable will be
10369 deleted forever, right now."
10370   (interactive)
10371   (or gnus-expert-user
10372       (gnus-yes-or-no-p
10373        "Are you really, really sure you want to delete all expirable messages? ")
10374       (error "Phew!"))
10375   (gnus-summary-expire-articles t))
10376
10377 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10378 (defun gnus-summary-delete-article (&optional n)
10379   "Delete the N next (mail) articles.
10380 This command actually deletes articles.  This is not a marking
10381 command.  The article will disappear forever from your life, never to
10382 return.
10383
10384 If N is negative, delete backwards.
10385 If N is nil and articles have been marked with the process mark,
10386 delete these instead.
10387
10388 If `gnus-novice-user' is non-nil you will be asked for
10389 confirmation before the articles are deleted."
10390   (interactive "P")
10391   (unless (gnus-check-backend-function 'request-expire-articles
10392                                        gnus-newsgroup-name)
10393     (error "The current newsgroup does not support article deletion"))
10394   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10395     (error "Couldn't open server"))
10396   ;; Compute the list of articles to delete.
10397   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10398         (nnmail-expiry-target 'delete)
10399         not-deleted)
10400     (if (and gnus-novice-user
10401              (not (gnus-yes-or-no-p
10402                    (format "Do you really want to delete %s forever? "
10403                            (if (> (length articles) 1)
10404                                (format "these %s articles" (length articles))
10405                              "this article")))))
10406         ()
10407       ;; Delete the articles.
10408       (setq not-deleted (gnus-request-expire-articles
10409                          articles gnus-newsgroup-name 'force))
10410       (save-excursion
10411         (while articles
10412           (gnus-summary-remove-process-mark (car articles))
10413           ;; The backend might not have been able to delete the article
10414           ;; after all.
10415           (unless (memq (car articles) not-deleted)
10416             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10417             (let* ((article (car articles))
10418                    (ghead  (gnus-data-header
10419                             (assoc article (gnus-data-list nil)))))
10420               (run-hook-with-args 'gnus-summary-article-delete-hook
10421                                   'delete ghead gnus-newsgroup-name nil
10422                                   nil)))
10423           (setq articles (cdr articles))))
10424       (when not-deleted
10425         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10426     (gnus-summary-position-point)
10427     (gnus-set-mode-line 'summary)
10428     not-deleted))
10429
10430 (defun gnus-summary-edit-article (&optional arg)
10431   "Edit the current article.
10432 This will have permanent effect only in mail groups.
10433 If ARG is nil, edit the decoded articles.
10434 If ARG is 1, edit the raw articles.
10435 If ARG is 2, edit the raw articles even in read-only groups.
10436 If ARG is 3, edit the articles with the current handles.
10437 Otherwise, allow editing of articles even in read-only
10438 groups."
10439   (interactive "P")
10440   (let (force raw current-handles)
10441     (cond
10442      ((null arg))
10443      ((eq arg 1)
10444       (setq raw t))
10445      ((eq arg 2)
10446       (setq raw t
10447             force t))
10448      ((eq arg 3)
10449       (setq current-handles
10450             (and (gnus-buffer-live-p gnus-article-buffer)
10451                  (with-current-buffer gnus-article-buffer
10452                    (prog1
10453                        gnus-article-mime-handles
10454                      (setq gnus-article-mime-handles nil))))))
10455      (t
10456       (setq force t)))
10457     (when (and raw (not force)
10458                (member gnus-newsgroup-name '("nndraft:delayed"
10459                                              "nndraft:drafts"
10460                                              "nndraft:queue")))
10461       (error "Can't edit the raw article in group %s"
10462              gnus-newsgroup-name))
10463     (with-current-buffer gnus-summary-buffer
10464       (let ((mail-parse-charset gnus-newsgroup-charset)
10465             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10466         (gnus-set-global-variables)
10467         (when (and (not force)
10468                    (gnus-group-read-only-p))
10469           (error "The current newsgroup does not support article editing"))
10470         (gnus-summary-show-article t)
10471         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10472           (with-current-buffer gnus-article-buffer
10473             (mm-enable-multibyte)))
10474         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10475             (setq raw t))
10476         (gnus-article-edit-article
10477          (if raw 'ignore
10478            `(lambda ()
10479               (let ((mbl mml-buffer-list))
10480                 (setq mml-buffer-list nil)
10481                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10482                   (mime-to-mml ,'current-handles))
10483                 (let ((mbl1 mml-buffer-list))
10484                   (setq mml-buffer-list mbl)
10485                   (set (make-local-variable 'mml-buffer-list) mbl1))
10486                 (gnus-make-local-hook 'kill-buffer-hook)
10487                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10488          `(lambda (no-highlight)
10489             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10490                   (message-options message-options)
10491                   (message-options-set-recipient)
10492                   (mail-parse-ignored-charsets
10493                    ',gnus-newsgroup-ignored-charsets)
10494                   (rfc2047-header-encoding-alist
10495                    ',(let ((charset (gnus-group-name-charset
10496                                      (gnus-find-method-for-group
10497                                       gnus-newsgroup-name)
10498                                      gnus-newsgroup-name)))
10499                        (append (list (cons "Newsgroups" charset)
10500                                      (cons "Followup-To" charset)
10501                                      (cons "Xref" charset))
10502                                rfc2047-header-encoding-alist))))
10503               ,(if (not raw) '(progn
10504                                 (mml-to-mime)
10505                                 (mml-destroy-buffers)
10506                                 (remove-hook 'kill-buffer-hook
10507                                              'mml-destroy-buffers t)
10508                                 (kill-local-variable 'mml-buffer-list)))
10509               (gnus-summary-edit-article-done
10510                ,(or (mail-header-references gnus-current-headers) "")
10511                ,(gnus-group-read-only-p)
10512                ,gnus-summary-buffer no-highlight))))))))
10513
10514 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10515
10516 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10517                                                  no-highlight)
10518   "Make edits to the current article permanent."
10519   (interactive)
10520   (save-excursion
10521     ;; The buffer restriction contains the entire article if it exists.
10522     (when (article-goto-body)
10523       (let ((lines (count-lines (point) (point-max)))
10524             (length (- (point-max) (point)))
10525             (case-fold-search t)
10526             (body (copy-marker (point))))
10527         (goto-char (point-min))
10528         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10529           (delete-region (match-beginning 1) (match-end 1))
10530           (insert (number-to-string length)))
10531         (goto-char (point-min))
10532         (when (re-search-forward
10533                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10534           (delete-region (match-beginning 1) (match-end 1))
10535           (insert (number-to-string length)))
10536         (goto-char (point-min))
10537         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10538           (delete-region (match-beginning 1) (match-end 1))
10539           (insert (number-to-string lines))))))
10540   ;; Replace the article.
10541   (let ((buf (current-buffer))
10542         (article (cdr gnus-article-current))
10543         replace-result)
10544     (with-temp-buffer
10545       (insert-buffer-substring buf)
10546       (if (and (not read-only)
10547                (not (setq replace-result
10548                           (gnus-request-replace-article
10549                            article (car gnus-article-current)
10550                            (current-buffer) t))))
10551           (error "Couldn't replace article")
10552         ;; If we got a number back, then that's the new article number
10553         ;; for this article.  Otherwise, the article number didn't change.
10554         (when (numberp replace-result)
10555           (with-current-buffer gnus-summary-buffer
10556             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10557             (gnus-summary-limit gnus-newsgroup-limit)
10558             (setq article replace-result)
10559             (gnus-summary-goto-subject article t)))
10560         ;; Update the summary buffer.
10561         (if (and references
10562                  (equal (message-tokenize-header references " ")
10563                         (message-tokenize-header
10564                          (or (message-fetch-field "references") "") " ")))
10565             ;; We only have to update this line.
10566             (save-excursion
10567               (save-restriction
10568                 (message-narrow-to-head)
10569                 (let ((head (buffer-substring-no-properties
10570                              (point-min) (point-max)))
10571                       header)
10572                   (with-temp-buffer
10573                     (insert (format "211 %d Article retrieved.\n" article))
10574                     (insert head)
10575                     (insert ".\n")
10576                     (let ((nntp-server-buffer (current-buffer)))
10577                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10578                     (with-current-buffer gnus-summary-buffer
10579                       (gnus-data-set-header (gnus-data-find article) header)
10580                       (gnus-summary-update-article-line article header)
10581                       (if (gnus-summary-goto-subject article nil t)
10582                           (gnus-summary-update-secondary-mark article)))))))
10583           ;; Update threads.
10584           (set-buffer (or buffer gnus-summary-buffer))
10585           (gnus-summary-update-article article)
10586           (if (gnus-summary-goto-subject article nil t)
10587               (gnus-summary-update-secondary-mark article)))
10588         ;; Prettify the article buffer again.
10589         (unless no-highlight
10590           (with-current-buffer gnus-article-buffer
10591             ;;!!! Fix this -- article should be rehighlighted.
10592             ;;(gnus-run-hooks 'gnus-article-display-hook)
10593             (set-buffer gnus-original-article-buffer)
10594             (gnus-request-article
10595              article (car gnus-article-current) (current-buffer))))
10596         ;; Prettify the summary buffer line.
10597         (when (gnus-visual-p 'summary-highlight 'highlight)
10598           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10599
10600 (defun gnus-summary-edit-wash (key)
10601   "Perform editing command KEY in the article buffer."
10602   (interactive
10603    (list
10604     (progn
10605       (message "%s" (concat (this-command-keys) "- "))
10606       (read-char))))
10607   (message "")
10608   (gnus-summary-edit-article)
10609   (execute-kbd-macro (concat (this-command-keys) key))
10610   (gnus-article-edit-done))
10611
10612 ;;; Respooling
10613
10614 (defun gnus-summary-respool-query (&optional silent trace)
10615   "Query where the respool algorithm would put this article."
10616   (interactive)
10617   (let (gnus-mark-article-hook)
10618     (gnus-summary-select-article)
10619     (with-current-buffer gnus-original-article-buffer
10620       (let ((groups (nnmail-article-group 'identity trace)))
10621         (unless silent
10622           (if groups
10623               (message "This message would go to %s"
10624                        (mapconcat 'car groups ", "))
10625             (message "This message would go to no groups"))
10626           groups)))))
10627
10628 (defun gnus-summary-respool-trace ()
10629   "Trace where the respool algorithm would put this article.
10630 Display a buffer showing all fancy splitting patterns which matched."
10631   (interactive)
10632   (gnus-summary-respool-query nil t))
10633
10634 ;; Summary marking commands.
10635
10636 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10637   "Mark articles which has the same subject as read, and then select the next.
10638 If UNMARK is positive, remove any kind of mark.
10639 If UNMARK is negative, tick articles."
10640   (interactive "P")
10641   (when unmark
10642     (setq unmark (prefix-numeric-value unmark)))
10643   (let ((count
10644          (gnus-summary-mark-same-subject
10645           (gnus-summary-article-subject) unmark)))
10646     ;; Select next unread article.  If auto-select-same mode, should
10647     ;; select the first unread article.
10648     (gnus-summary-next-article t (and gnus-auto-select-same
10649                                       (gnus-summary-article-subject)))
10650     (gnus-message 7 "%d article%s marked as %s"
10651                   count (if (= count 1) " is" "s are")
10652                   (if unmark "unread" "read"))))
10653
10654 (defun gnus-summary-kill-same-subject (&optional unmark)
10655   "Mark articles which has the same subject as read.
10656 If UNMARK is positive, remove any kind of mark.
10657 If UNMARK is negative, tick articles."
10658   (interactive "P")
10659   (when unmark
10660     (setq unmark (prefix-numeric-value unmark)))
10661   (let ((count
10662          (gnus-summary-mark-same-subject
10663           (gnus-summary-article-subject) unmark)))
10664     ;; If marked as read, go to next unread subject.
10665     (when (null unmark)
10666       ;; Go to next unread subject.
10667       (gnus-summary-next-subject 1 t))
10668     (gnus-message 7 "%d articles are marked as %s"
10669                   count (if unmark "unread" "read"))))
10670
10671 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10672   "Mark articles with same SUBJECT as read, and return marked number.
10673 If optional argument UNMARK is positive, remove any kinds of marks.
10674 If optional argument UNMARK is negative, mark articles as unread instead."
10675   (let ((count 1))
10676     (save-excursion
10677       (cond
10678        ((null unmark)                   ; Mark as read.
10679         (while (and
10680                 (progn
10681                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10682                   (gnus-summary-show-thread) t)
10683                 (gnus-summary-find-subject subject))
10684           (setq count (1+ count))))
10685        ((> unmark 0)                    ; Tick.
10686         (while (and
10687                 (progn
10688                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10689                   (gnus-summary-show-thread) t)
10690                 (gnus-summary-find-subject subject))
10691           (setq count (1+ count))))
10692        (t                               ; Mark as unread.
10693         (while (and
10694                 (progn
10695                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10696                   (gnus-summary-show-thread) t)
10697                 (gnus-summary-find-subject subject))
10698           (setq count (1+ count)))))
10699       (gnus-set-mode-line 'summary)
10700       ;; Return the number of marked articles.
10701       count)))
10702
10703 (defun gnus-summary-mark-as-processable (n &optional unmark)
10704   "Set the process mark on the next N articles.
10705 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10706 the process mark instead.  The difference between N and the actual
10707 number of articles marked is returned."
10708   (interactive "P")
10709   (if (and (null n) (gnus-region-active-p))
10710       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10711     (setq n (prefix-numeric-value n))
10712     (let ((backward (< n 0))
10713           (n (abs n)))
10714       (while (and
10715               (> n 0)
10716               (if unmark
10717                   (gnus-summary-remove-process-mark
10718                    (gnus-summary-article-number))
10719                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10720               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10721         (setq n (1- n)))
10722       (when (/= 0 n)
10723         (gnus-message 7 "No more articles"))
10724       (gnus-summary-recenter)
10725       (gnus-summary-position-point)
10726       n)))
10727
10728 (defun gnus-summary-unmark-as-processable (n)
10729   "Remove the process mark from the next N articles.
10730 If N is negative, unmark backward instead.  The difference between N and
10731 the actual number of articles unmarked is returned."
10732   (interactive "P")
10733   (gnus-summary-mark-as-processable n t))
10734
10735 (defun gnus-summary-unmark-all-processable ()
10736   "Remove the process mark from all articles."
10737   (interactive)
10738   (save-excursion
10739     (while gnus-newsgroup-processable
10740       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10741   (gnus-summary-position-point))
10742
10743 (defun gnus-summary-add-mark (article type)
10744   "Mark ARTICLE with a mark of TYPE."
10745   (let ((vtype (car (assq type gnus-article-mark-lists)))
10746         var)
10747     (if (not vtype)
10748         (error "No such mark type: %s" type)
10749       (setq var (intern (format "gnus-newsgroup-%s" type)))
10750       (set var (cons article (symbol-value var)))
10751       (if (memq type '(processable cached replied forwarded recent saved))
10752           (gnus-summary-update-secondary-mark article)
10753         ;;; !!! This is bogus.  We should find out what primary
10754         ;;; !!! mark we want to set.
10755         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10756
10757 (defun gnus-summary-mark-as-expirable (n)
10758   "Mark N articles forward as expirable.
10759 If N is negative, mark backward instead.  The difference between N and
10760 the actual number of articles marked is returned."
10761   (interactive "p")
10762   (gnus-summary-mark-forward n gnus-expirable-mark))
10763
10764 (defun gnus-summary-mark-as-spam (n)
10765   "Mark N articles forward as spam.
10766 If N is negative, mark backward instead.  The difference between N and
10767 the actual number of articles marked is returned."
10768   (interactive "p")
10769   (gnus-summary-mark-forward n gnus-spam-mark))
10770
10771 (defun gnus-summary-mark-article-as-replied (article)
10772   "Mark ARTICLE as replied to and update the summary line.
10773 ARTICLE can also be a list of articles."
10774   (interactive (list (gnus-summary-article-number)))
10775   (let ((articles (if (listp article) article (list article))))
10776     (dolist (article articles)
10777       (unless (numberp article)
10778         (error "%s is not a number" article))
10779       (push article gnus-newsgroup-replied)
10780       (let ((inhibit-read-only t))
10781         (when (gnus-summary-goto-subject article nil t)
10782           (gnus-summary-update-secondary-mark article))))))
10783
10784 (defun gnus-summary-mark-article-as-forwarded (article)
10785   "Mark ARTICLE as forwarded and update the summary line.
10786 ARTICLE can also be a list of articles."
10787   (let ((articles (if (listp article) article (list article))))
10788     (dolist (article articles)
10789       (push article gnus-newsgroup-forwarded)
10790       (let ((inhibit-read-only t))
10791         (when (gnus-summary-goto-subject article nil t)
10792           (gnus-summary-update-secondary-mark article))))))
10793
10794 (defun gnus-summary-set-bookmark (article)
10795   "Set a bookmark in current article."
10796   (interactive (list (gnus-summary-article-number)))
10797   (when (or (not (get-buffer gnus-article-buffer))
10798             (not gnus-current-article)
10799             (not gnus-article-current)
10800             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10801     (error "No current article selected"))
10802   ;; Remove old bookmark, if one exists.
10803   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10804   ;; Set the new bookmark, which is on the form
10805   ;; (article-number . line-number-in-body).
10806   (push
10807    (cons article
10808          (with-current-buffer gnus-article-buffer
10809            (count-lines
10810             (min (point)
10811                  (save-excursion
10812                    (article-goto-body)
10813                    (point)))
10814             (point))))
10815    gnus-newsgroup-bookmarks)
10816   (gnus-message 6 "A bookmark has been added to the current article."))
10817
10818 (defun gnus-summary-remove-bookmark (article)
10819   "Remove the bookmark from the current article."
10820   (interactive (list (gnus-summary-article-number)))
10821   ;; Remove old bookmark, if one exists.
10822   (if (not (assq article gnus-newsgroup-bookmarks))
10823       (gnus-message 6 "No bookmark in current article.")
10824     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10825     (gnus-message 6 "Removed bookmark.")))
10826
10827 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10828 (defun gnus-summary-mark-as-dormant (n)
10829   "Mark N articles forward as dormant.
10830 If N is negative, mark backward instead.  The difference between N and
10831 the actual number of articles marked is returned."
10832   (interactive "p")
10833   (gnus-summary-mark-forward n gnus-dormant-mark))
10834
10835 (defun gnus-summary-set-process-mark (article)
10836   "Set the process mark on ARTICLE and update the summary line."
10837   (setq gnus-newsgroup-processable
10838         (cons article
10839               (delq article gnus-newsgroup-processable)))
10840   (when (gnus-summary-goto-subject article)
10841     (gnus-summary-show-thread)
10842     (gnus-summary-goto-subject article)
10843     (gnus-summary-update-secondary-mark article)))
10844
10845 (defun gnus-summary-remove-process-mark (&rest articles)
10846   "Remove the process mark from ARTICLES and update the summary line."
10847   (dolist (article articles)
10848     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10849     (when (gnus-summary-goto-subject article)
10850       (gnus-summary-show-thread)
10851       (gnus-summary-goto-subject article)
10852       (gnus-summary-update-secondary-mark article)))
10853   t)
10854
10855 (defun gnus-summary-set-saved-mark (article)
10856   "Set the process mark on ARTICLE and update the summary line."
10857   (push article gnus-newsgroup-saved)
10858   (when (gnus-summary-goto-subject article)
10859     (gnus-summary-update-secondary-mark article)))
10860
10861 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10862   "Mark N articles as read forwards.
10863 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10864 The difference between N and the actual number of articles marked is
10865 returned.
10866 If NO-EXPIRE, auto-expiry will be inhibited."
10867   (interactive "p")
10868   (gnus-summary-show-thread)
10869   (let ((backward (< n 0))
10870         (gnus-summary-goto-unread
10871          (and gnus-summary-goto-unread
10872               (not (eq gnus-summary-goto-unread 'never))
10873               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10874                                     gnus-ticked-mark gnus-dormant-mark)))))
10875         (n (abs n))
10876         (mark (or mark gnus-del-mark)))
10877     (while (and (> n 0)
10878                 (gnus-summary-mark-article nil mark no-expire)
10879                 (zerop (gnus-summary-next-subject
10880                         (if backward -1 1)
10881                         (and gnus-summary-goto-unread
10882                              (not (eq gnus-summary-goto-unread 'never)))
10883                         t)))
10884       (setq n (1- n)))
10885     (when (/= 0 n)
10886       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10887     (gnus-summary-recenter)
10888     (gnus-summary-position-point)
10889     (gnus-set-mode-line 'summary)
10890     n))
10891
10892 (defun gnus-summary-mark-article-as-read (mark)
10893   "Mark the current article quickly as read with MARK."
10894   (let ((article (gnus-summary-article-number)))
10895     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10896     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10897     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10898     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10899     (push (cons article mark) gnus-newsgroup-reads)
10900     ;; Possibly remove from cache, if that is used.
10901     (when gnus-use-cache
10902       (gnus-cache-enter-remove-article article))
10903     ;; Allow the backend to change the mark.
10904     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10905     ;; Check for auto-expiry.
10906     (when (and gnus-newsgroup-auto-expire
10907                (memq mark gnus-auto-expirable-marks))
10908       (setq mark gnus-expirable-mark)
10909       ;; Let the backend know about the mark change.
10910       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10911       (push article gnus-newsgroup-expirable))
10912     ;; Set the mark in the buffer.
10913     (gnus-summary-update-mark mark 'unread)
10914     t))
10915
10916 (defun gnus-summary-mark-article-as-unread (mark)
10917   "Mark the current article quickly as unread with MARK."
10918   (let* ((article (gnus-summary-article-number))
10919          (old-mark (gnus-summary-article-mark article)))
10920     ;; Allow the backend to change the mark.
10921     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10922     (if (eq mark old-mark)
10923         t
10924       (if (<= article 0)
10925           (progn
10926             (gnus-error 1 "Can't mark negative article numbers")
10927             nil)
10928         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10929         (setq gnus-newsgroup-spam-marked
10930               (delq article gnus-newsgroup-spam-marked))
10931         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10932         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10933         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10934         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10935         (cond ((= mark gnus-ticked-mark)
10936                (setq gnus-newsgroup-marked
10937                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10938                                               article)))
10939               ((= mark gnus-spam-mark)
10940                (setq gnus-newsgroup-spam-marked
10941                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10942                                               article)))
10943               ((= mark gnus-dormant-mark)
10944                (setq gnus-newsgroup-dormant
10945                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10946                                               article)))
10947               (t
10948                (setq gnus-newsgroup-unreads
10949                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10950                                               article))))
10951         (gnus-alist-pull article gnus-newsgroup-reads)
10952
10953         ;; See whether the article is to be put in the cache.
10954         (and gnus-use-cache
10955              (vectorp (gnus-summary-article-header article))
10956              (save-excursion
10957                (gnus-cache-possibly-enter-article
10958                 gnus-newsgroup-name article
10959                 (= mark gnus-ticked-mark)
10960                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10961
10962         ;; Fix the mark.
10963         (gnus-summary-update-mark mark 'unread)
10964         t))))
10965
10966 (defun gnus-summary-mark-article (&optional article mark no-expire)
10967   "Mark ARTICLE with MARK.  MARK can be any character.
10968 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10969 `??' (dormant) and `?E' (expirable).
10970 If MARK is nil, then the default character `?r' is used.
10971 If ARTICLE is nil, then the article on the current line will be
10972 marked.
10973 If NO-EXPIRE, auto-expiry will be inhibited."
10974   ;; The mark might be a string.
10975   (when (stringp mark)
10976     (setq mark (aref mark 0)))
10977   ;; If no mark is given, then we check auto-expiring.
10978   (when (null mark)
10979     (setq mark gnus-del-mark))
10980   (when (and (not no-expire)
10981              gnus-newsgroup-auto-expire
10982              (memq mark gnus-auto-expirable-marks))
10983     (setq mark gnus-expirable-mark))
10984   (let ((article (or article (gnus-summary-article-number)))
10985         (old-mark (gnus-summary-article-mark article)))
10986     ;; Allow the backend to change the mark.
10987     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10988     (if (eq mark old-mark)
10989         t
10990       (unless article
10991         (error "No article on current line"))
10992       (if (not (if (or (= mark gnus-unread-mark)
10993                        (= mark gnus-ticked-mark)
10994                        (= mark gnus-spam-mark)
10995                        (= mark gnus-dormant-mark))
10996                    (gnus-mark-article-as-unread article mark)
10997                  (gnus-mark-article-as-read article mark)))
10998           t
10999         ;; See whether the article is to be put in the cache.
11000         (and gnus-use-cache
11001              (not (= mark gnus-canceled-mark))
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         (when (gnus-summary-goto-subject article nil t)
11010           (let ((inhibit-read-only t))
11011             (gnus-summary-show-thread)
11012             ;; Fix the mark.
11013             (gnus-summary-update-mark mark 'unread)
11014             t))))))
11015
11016 (defun gnus-summary-update-secondary-mark (article)
11017   "Update the secondary (read, process, cache) mark."
11018   (gnus-summary-update-mark
11019    (cond ((memq article gnus-newsgroup-processable)
11020           gnus-process-mark)
11021          ((memq article gnus-newsgroup-cached)
11022           gnus-cached-mark)
11023          ((memq article gnus-newsgroup-replied)
11024           gnus-replied-mark)
11025          ((memq article gnus-newsgroup-forwarded)
11026           gnus-forwarded-mark)
11027          ((memq article gnus-newsgroup-saved)
11028           gnus-saved-mark)
11029          ((memq article gnus-newsgroup-unseen)
11030           gnus-unseen-mark)
11031          (t gnus-no-mark))
11032    'replied)
11033   (when (gnus-visual-p 'summary-highlight 'highlight)
11034     (gnus-summary-highlight-line)
11035     (gnus-run-hooks 'gnus-summary-update-hook))
11036   t)
11037
11038 (defun gnus-summary-update-download-mark (article)
11039   "Update the download mark."
11040   (gnus-summary-update-mark
11041    (cond ((memq article gnus-newsgroup-undownloaded)
11042           gnus-undownloaded-mark)
11043          (gnus-newsgroup-agentized
11044           gnus-downloaded-mark)
11045          (t
11046           gnus-no-mark))
11047    'download)
11048   (gnus-summary-update-line t)
11049   t)
11050
11051 (defun gnus-summary-update-mark (mark type)
11052   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11053         (inhibit-read-only t))
11054     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11055     (when forward
11056       (when (looking-at "\r")
11057         (incf forward))
11058       (when (<= (+ forward (point)) (point-max))
11059         ;; Go to the right position on the line.
11060         (goto-char (+ forward (point)))
11061         ;; Replace the old mark with the new mark.
11062         (let ((to-insert
11063                (mm-subst-char-in-string
11064                 (char-after) mark
11065                 (buffer-substring (point) (1+ (point))))))
11066           (delete-region (point) (1+ (point)))
11067           (insert to-insert))
11068         ;; Optionally update the marks by some user rule.
11069         (when (eq type 'unread)
11070           (gnus-data-set-mark
11071            (gnus-data-find (gnus-summary-article-number)) mark)
11072           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11073
11074 (defun gnus-mark-article-as-read (article &optional mark)
11075   "Enter ARTICLE in the pertinent lists and remove it from others."
11076   ;; Make the article expirable.
11077   (let ((mark (or mark gnus-del-mark)))
11078     (setq gnus-newsgroup-expirable
11079           (if (= mark gnus-expirable-mark)
11080               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11081             (delq article gnus-newsgroup-expirable)))
11082     ;; Remove from unread and marked lists.
11083     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11084     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11085     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11086     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11087     (push (cons article mark) gnus-newsgroup-reads)
11088     ;; Possibly remove from cache, if that is used.
11089     (when gnus-use-cache
11090       (gnus-cache-enter-remove-article article))
11091     t))
11092
11093 (defun gnus-mark-article-as-unread (article &optional mark)
11094   "Enter ARTICLE in the pertinent lists and remove it from others."
11095   (let ((mark (or mark gnus-ticked-mark)))
11096     (if (<= article 0)
11097         (progn
11098           (gnus-error 1 "Can't mark negative article numbers")
11099           nil)
11100       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11101             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11102             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11103             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11104             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11105
11106       ;; Unsuppress duplicates?
11107       (when gnus-suppress-duplicates
11108         (gnus-dup-unsuppress-article article))
11109
11110       (cond ((= mark gnus-ticked-mark)
11111              (setq gnus-newsgroup-marked
11112                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11113             ((= mark gnus-spam-mark)
11114              (setq gnus-newsgroup-spam-marked
11115                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11116                                             article)))
11117             ((= mark gnus-dormant-mark)
11118              (setq gnus-newsgroup-dormant
11119                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11120             (t
11121              (setq gnus-newsgroup-unreads
11122                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11123       (gnus-alist-pull article gnus-newsgroup-reads)
11124       t)))
11125
11126 (defun gnus-summary-tick-article-forward (n)
11127   "Tick N articles forwards.
11128 If N is negative, tick backwards instead.
11129 The difference between N and the number of articles ticked is returned."
11130   (interactive "p")
11131   (gnus-summary-mark-forward n gnus-ticked-mark))
11132
11133 (defun gnus-summary-tick-article-backward (n)
11134   "Tick N articles backwards.
11135 The difference between N and the number of articles ticked is returned."
11136   (interactive "p")
11137   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11138
11139 (defun gnus-summary-tick-article (&optional article clear-mark)
11140   "Mark current article as unread.
11141 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11142 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11143   (interactive)
11144   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11145                                        gnus-ticked-mark)))
11146
11147 (defun gnus-summary-mark-as-read-forward (n)
11148   "Mark N articles as read forwards.
11149 If N is negative, mark backwards instead.
11150 The difference between N and the actual number of articles marked is
11151 returned."
11152   (interactive "p")
11153   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11154
11155 (defun gnus-summary-mark-as-read-backward (n)
11156   "Mark the N articles as read backwards.
11157 The difference between N and the actual number of articles marked is
11158 returned."
11159   (interactive "p")
11160   (gnus-summary-mark-forward
11161    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11162
11163 (defun gnus-summary-mark-as-read (&optional article mark)
11164   "Mark current article as read.
11165 ARTICLE specifies the article to be marked as read.
11166 MARK specifies a string to be inserted at the beginning of the line."
11167   (gnus-summary-mark-article article mark))
11168
11169 (defun gnus-summary-clear-mark-forward (n)
11170   "Clear marks from N articles forward.
11171 If N is negative, clear backward instead.
11172 The difference between N and the number of marks cleared is returned."
11173   (interactive "p")
11174   (gnus-summary-mark-forward n gnus-unread-mark))
11175
11176 (defun gnus-summary-clear-mark-backward (n)
11177   "Clear marks from N articles backward.
11178 The difference between N and the number of marks cleared is returned."
11179   (interactive "p")
11180   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11181
11182 (defun gnus-summary-mark-unread-as-read ()
11183   "Intended to be used by `gnus-mark-article-hook'."
11184   (when (memq gnus-current-article gnus-newsgroup-unreads)
11185     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11186
11187 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11188   "Intended to be used by `gnus-mark-article-hook'."
11189   (let ((mark (gnus-summary-article-mark)))
11190     (when (or (gnus-unread-mark-p mark)
11191               (gnus-read-mark-p mark))
11192       (gnus-summary-mark-article gnus-current-article
11193                                  (or new-mark gnus-read-mark)))))
11194
11195 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11196   "Intended to be used by `gnus-mark-article-hook'."
11197   (let ((mark (gnus-summary-article-mark)))
11198     (when (or (gnus-unread-mark-p mark)
11199               (gnus-read-mark-p mark))
11200       (gnus-summary-mark-article (gnus-summary-article-number)
11201                                  (or new-mark gnus-read-mark)))))
11202
11203 (defun gnus-summary-mark-unread-as-ticked ()
11204   "Intended to be used by `gnus-mark-article-hook'."
11205   (when (memq gnus-current-article gnus-newsgroup-unreads)
11206     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11207
11208 (defun gnus-summary-mark-region-as-read (point mark all)
11209   "Mark all unread articles between point and mark as read.
11210 If given a prefix, mark all articles between point and mark as read,
11211 even ticked and dormant ones."
11212   (interactive "r\nP")
11213   (save-excursion
11214     (let (article)
11215       (goto-char point)
11216       (beginning-of-line)
11217       (while (and
11218               (< (point) mark)
11219               (progn
11220                 (when (or all
11221                           (memq (setq article (gnus-summary-article-number))
11222                                 gnus-newsgroup-unreads))
11223                   (gnus-summary-mark-article article gnus-del-mark))
11224                 t)
11225               (gnus-summary-find-next))))))
11226
11227 (defun gnus-summary-mark-below (score mark)
11228   "Mark articles with score less than SCORE with MARK."
11229   (interactive "P\ncMark: ")
11230   (setq score (if score
11231                   (prefix-numeric-value score)
11232                 (or gnus-summary-default-score 0)))
11233   (with-current-buffer gnus-summary-buffer
11234     (goto-char (point-min))
11235     (while
11236         (progn
11237           (and (< (gnus-summary-article-score) score)
11238                (gnus-summary-mark-article nil mark))
11239           (gnus-summary-find-next)))))
11240
11241 (defun gnus-summary-kill-below (&optional score)
11242   "Mark articles with score below SCORE as read."
11243   (interactive "P")
11244   (gnus-summary-mark-below score gnus-killed-mark))
11245
11246 (defun gnus-summary-clear-above (&optional score)
11247   "Clear all marks from articles with score above SCORE."
11248   (interactive "P")
11249   (gnus-summary-mark-above score gnus-unread-mark))
11250
11251 (defun gnus-summary-tick-above (&optional score)
11252   "Tick all articles with score above SCORE."
11253   (interactive "P")
11254   (gnus-summary-mark-above score gnus-ticked-mark))
11255
11256 (defun gnus-summary-mark-above (score mark)
11257   "Mark articles with score over SCORE with MARK."
11258   (interactive "P\ncMark: ")
11259   (setq score (if score
11260                   (prefix-numeric-value score)
11261                 (or gnus-summary-default-score 0)))
11262   (with-current-buffer gnus-summary-buffer
11263     (goto-char (point-min))
11264     (while (and (progn
11265                   (when (> (gnus-summary-article-score) score)
11266                     (gnus-summary-mark-article nil mark))
11267                   t)
11268                 (gnus-summary-find-next)))))
11269
11270 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11271 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11272 (defun gnus-summary-limit-include-expunged (&optional no-error)
11273   "Display all the hidden articles that were expunged for low scores."
11274   (interactive)
11275   (let ((inhibit-read-only t))
11276     (let ((scored gnus-newsgroup-scored)
11277           headers h)
11278       (while scored
11279         (unless (gnus-summary-article-header (caar scored))
11280           (and (setq h (gnus-number-to-header (caar scored)))
11281                (< (cdar scored) gnus-summary-expunge-below)
11282                (push h headers)))
11283         (setq scored (cdr scored)))
11284       (if (not headers)
11285           (when (not no-error)
11286             (error "No expunged articles hidden"))
11287         (goto-char (point-min))
11288         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11289         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11290         (dolist (x headers)
11291           (push (mail-header-number x) gnus-newsgroup-limit))
11292         (gnus-summary-prepare-unthreaded (nreverse headers))
11293         (goto-char (point-min))
11294         (gnus-summary-position-point)
11295         t))))
11296
11297 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11298   "Mark all unread articles in this newsgroup as read.
11299 If prefix argument ALL is non-nil, ticked and dormant articles will
11300 also be marked as read.
11301 If QUIETLY is non-nil, no questions will be asked.
11302
11303 If TO-HERE is non-nil, it should be a point in the buffer.  All
11304 articles before (after, if REVERSE is set) this point will be marked
11305 as read.
11306
11307 Note that this function will only catch up the unread article
11308 in the current summary buffer limitation.
11309
11310 The number of articles marked as read is returned."
11311   (interactive "P")
11312   (prog1
11313       (save-excursion
11314         (when (or quietly
11315                   (not gnus-interactive-catchup) ;Without confirmation?
11316                   gnus-expert-user
11317                   (gnus-y-or-n-p
11318                    (if all
11319                        "Mark absolutely all articles as read? "
11320                      "Mark all unread articles as read? ")))
11321           (if (and not-mark
11322                    (not gnus-newsgroup-adaptive)
11323                    (not gnus-newsgroup-auto-expire)
11324                    (not gnus-suppress-duplicates)
11325                    (or (not gnus-use-cache)
11326                        (eq gnus-use-cache 'passive)))
11327               (progn
11328                 (when all
11329                   (setq gnus-newsgroup-marked nil
11330                         gnus-newsgroup-spam-marked nil
11331                         gnus-newsgroup-dormant nil))
11332                 (setq gnus-newsgroup-unreads
11333                       (gnus-sorted-nunion
11334                        (gnus-sorted-intersection gnus-newsgroup-unreads
11335                                                  gnus-newsgroup-downloadable)
11336                        (gnus-sorted-difference gnus-newsgroup-unfetched
11337                                                gnus-newsgroup-cached))))
11338             ;; We actually mark all articles as canceled, which we
11339             ;; have to do when using auto-expiry or adaptive scoring.
11340             (gnus-summary-show-all-threads)
11341             (if (and to-here reverse)
11342                 (progn
11343                   (goto-char to-here)
11344                   (gnus-summary-mark-current-read-and-unread-as-read
11345                    gnus-catchup-mark)
11346                   (while (gnus-summary-find-next (not all))
11347                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11348               (when (gnus-summary-first-subject (not all))
11349                 (while (and
11350                         (if to-here (< (point) to-here) t)
11351                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11352                         (gnus-summary-find-next (not all))))))
11353             (gnus-set-mode-line 'summary))
11354           t))
11355     (gnus-summary-position-point)))
11356
11357 (defun gnus-summary-catchup-to-here (&optional all)
11358   "Mark all unticked articles before the current one as read.
11359 If ALL is non-nil, also mark ticked and dormant articles as read."
11360   (interactive "P")
11361   (save-excursion
11362     (gnus-save-hidden-threads
11363       (let ((beg (point)))
11364         ;; We check that there are unread articles.
11365         (when (or all (gnus-summary-find-prev))
11366           (gnus-summary-catchup all t beg)))))
11367   (gnus-summary-position-point))
11368
11369 (defun gnus-summary-catchup-from-here (&optional all)
11370   "Mark all unticked articles after (and including) the current one as read.
11371 If ALL is non-nil, also mark ticked and dormant articles as read."
11372   (interactive "P")
11373   (save-excursion
11374     (gnus-save-hidden-threads
11375       (let ((beg (point)))
11376         ;; We check that there are unread articles.
11377         (when (or all (gnus-summary-find-next))
11378           (gnus-summary-catchup all t beg nil t)))))
11379   (gnus-summary-position-point))
11380
11381 (defun gnus-summary-catchup-all (&optional quietly)
11382   "Mark all articles in this newsgroup as read.
11383 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11384 instead, which marks only unread articles as read."
11385   (interactive "P")
11386   (gnus-summary-catchup t quietly))
11387
11388 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11389   "Mark all unread articles in this group as read, then exit.
11390 If prefix argument ALL is non-nil, all articles are marked as read.
11391 If QUIETLY is non-nil, no questions will be asked."
11392   (interactive "P")
11393   (when (gnus-summary-catchup all quietly nil 'fast)
11394     ;; Select next newsgroup or exit.
11395     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11396              (eq gnus-auto-select-next 'quietly))
11397         (gnus-summary-next-group nil)
11398       (gnus-summary-exit))))
11399
11400 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11401   "Mark all articles in this newsgroup as read, and then exit.
11402 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11403 instead, which marks only unread articles as read."
11404   (interactive "P")
11405   (gnus-summary-catchup-and-exit t quietly))
11406
11407 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11408   "Mark all articles in this group as read and select the next group.
11409 If given a prefix, mark all articles, unread as well as ticked, as
11410 read."
11411   (interactive "P")
11412   (save-excursion
11413     (gnus-summary-catchup all))
11414   (gnus-summary-next-group))
11415
11416 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11417   "Mark all articles in this group as read and select the previous group.
11418 If given a prefix, mark all articles, unread as well as ticked, as
11419 read."
11420   (interactive "P")
11421   (save-excursion
11422     (gnus-summary-catchup all))
11423   (gnus-summary-next-group nil nil t))
11424
11425 ;;;
11426 ;;; with article
11427 ;;;
11428
11429 (defmacro gnus-with-article (article &rest forms)
11430   "Select ARTICLE and perform FORMS in the original article buffer.
11431 Then replace the article with the result."
11432   `(progn
11433      ;; We don't want the article to be marked as read.
11434      (let (gnus-mark-article-hook)
11435        (gnus-summary-select-article t t nil ,article))
11436      (set-buffer gnus-original-article-buffer)
11437      ,@forms
11438      (if (not (gnus-check-backend-function
11439                'request-replace-article (car gnus-article-current)))
11440          (gnus-message 5 "Read-only group; not replacing")
11441        (unless (gnus-request-replace-article
11442                 ,article (car gnus-article-current)
11443                 (current-buffer) t)
11444          (error "Couldn't replace article")))
11445      ;; The cache and backlog have to be flushed somewhat.
11446      (when gnus-keep-backlog
11447        (gnus-backlog-remove-article
11448         (car gnus-article-current) (cdr gnus-article-current)))
11449      (when gnus-use-cache
11450        (gnus-cache-update-article
11451         (car gnus-article-current) (cdr gnus-article-current)))))
11452
11453 (put 'gnus-with-article 'lisp-indent-function 1)
11454 (put 'gnus-with-article 'edebug-form-spec '(form body))
11455
11456 ;; Thread-based commands.
11457
11458 (defun gnus-summary-articles-in-thread (&optional article)
11459   "Return a list of all articles in the current thread.
11460 If ARTICLE is non-nil, return all articles in the thread that starts
11461 with that article."
11462   (let* ((article (or article (gnus-summary-article-number)))
11463          (data (gnus-data-find-list article))
11464          (top-level (gnus-data-level (car data)))
11465          (top-subject
11466           (cond ((null gnus-thread-operation-ignore-subject)
11467                  (gnus-simplify-subject-re
11468                   (mail-header-subject (gnus-data-header (car data)))))
11469                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11470                  (gnus-simplify-subject-fuzzy
11471                   (mail-header-subject (gnus-data-header (car data)))))
11472                 (t nil)))
11473          (end-point (save-excursion
11474                       (goto-char (gnus-data-pos (car data)))
11475                       (if (gnus-summary-go-to-next-thread)
11476                           (point) (point-max))))
11477          articles)
11478     (while (and data
11479                 (< (gnus-data-pos (car data)) end-point))
11480       (when (or (not top-subject)
11481                 (string= top-subject
11482                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11483                              (gnus-simplify-subject-fuzzy
11484                               (mail-header-subject
11485                                (gnus-data-header (car data))))
11486                            (gnus-simplify-subject-re
11487                             (mail-header-subject
11488                              (gnus-data-header (car data)))))))
11489         (push (gnus-data-number (car data)) articles))
11490       (unless (and (setq data (cdr data))
11491                    (> (gnus-data-level (car data)) top-level))
11492         (setq data nil)))
11493     ;; Return the list of articles.
11494     (nreverse articles)))
11495
11496 (defun gnus-summary-rethread-current ()
11497   "Rethread the thread the current article is part of."
11498   (interactive)
11499   (let* ((gnus-show-threads t)
11500          (article (gnus-summary-article-number))
11501          (id (mail-header-id (gnus-summary-article-header)))
11502          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11503     (unless id
11504       (error "No article on the current line"))
11505     (gnus-rebuild-thread id)
11506     (gnus-summary-goto-subject article)))
11507
11508 (defun gnus-summary-reparent-thread ()
11509   "Make the current article child of the marked (or previous) article.
11510
11511 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11512 is non-nil or the Subject: of both articles are the same."
11513   (interactive)
11514   (unless (not (gnus-group-read-only-p))
11515     (error "The current newsgroup does not support article editing"))
11516   (unless (<= (length gnus-newsgroup-processable) 1)
11517     (error "No more than one article may be marked"))
11518   (let ((child (gnus-summary-article-number))
11519         ;; First grab the marked article, otherwise one line up.
11520         (parent (if (not (null gnus-newsgroup-processable))
11521                     (car gnus-newsgroup-processable)
11522                   (save-excursion
11523                     (if (eq (forward-line -1) 0)
11524                         (gnus-summary-article-number)
11525                       (error "Beginning of summary buffer"))))))
11526     (gnus-summary-reparent-children parent (list child))))
11527
11528 (defun gnus-summary-reparent-children (parent children)
11529   "Make PARENT the parent of CHILDREN.
11530 When called interactively, PARENT is the current article and CHILDREN
11531 are the process-marked articles."
11532   (interactive
11533    (list (gnus-summary-article-number)
11534          (gnus-summary-work-articles nil)))
11535   (dolist (child children)
11536     (save-window-excursion
11537       (let ((gnus-article-buffer " *reparent*"))
11538         (unless (not (eq parent child))
11539           (error "An article may not be self-referential"))
11540         (let ((message-id (mail-header-id
11541                            (gnus-summary-article-header parent))))
11542           (unless (and message-id (not (equal message-id "")))
11543             (error "No message-id in desired parent"))
11544           (gnus-with-article child
11545             (save-restriction
11546               (goto-char (point-min))
11547               (message-narrow-to-head)
11548               (if (re-search-forward "^References: " nil t)
11549                   (progn
11550                     (re-search-forward "^[^ \t]" nil t)
11551                     (forward-line -1)
11552                     (end-of-line)
11553                     (insert " " message-id))
11554                 (insert "References: " message-id "\n"))))
11555           (set-buffer gnus-summary-buffer)
11556           (gnus-summary-unmark-all-processable)
11557           (gnus-summary-update-article child)
11558           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11559             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11560           (gnus-summary-rethread-current)
11561           (gnus-message 3 "Article %d is now the child of article %d"
11562                         child parent))))))
11563
11564 (defun gnus-summary-toggle-threads (&optional arg)
11565   "Toggle showing conversation threads.
11566 If ARG is positive number, turn showing conversation threads on."
11567   (interactive "P")
11568   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11569     (setq gnus-show-threads
11570           (if (null arg) (not gnus-show-threads)
11571             (> (prefix-numeric-value arg) 0)))
11572     (gnus-summary-prepare)
11573     (gnus-summary-goto-subject current)
11574     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11575     (gnus-summary-position-point)))
11576
11577 (eval-and-compile
11578   (if (fboundp 'remove-overlays)
11579       (defalias 'gnus-remove-overlays 'remove-overlays)
11580     (defun gnus-remove-overlays (beg end name val)
11581       "Clear BEG and END of overlays whose property NAME has value VAL.
11582 For compatibility with XEmacs."
11583       (dolist (ov (gnus-overlays-in beg end))
11584         (when (eq (gnus-overlay-get ov name) val)
11585           (gnus-delete-overlay ov))))))
11586
11587 (defun gnus-summary-show-all-threads ()
11588   "Show all threads."
11589   (interactive)
11590   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11591   (gnus-summary-position-point))
11592
11593 (defsubst gnus-summary--inv (p)
11594   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11595
11596 (defun gnus-summary-show-thread ()
11597   "Show thread subtrees.
11598 Returns nil if no thread was there to be shown."
11599   (interactive)
11600   (let* ((orig (point))
11601          (end (point-at-eol))
11602          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11603          ;; Leave point at bol
11604          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11605          (eoi (when end
11606                 (if (fboundp 'next-single-char-property-change)
11607                     ;; Note: XEmacs version of n-s-c-p-c may return nil
11608                     (or (next-single-char-property-change end 'invisible)
11609                         (point-max))
11610                   (while (progn
11611                            (end-of-line 2)
11612                            (and (not (eobp))
11613                                 (eq (get-char-property (point) 'invisible)
11614                                     'gnus-sum))))
11615                   (point)))))
11616     (when eoi
11617       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11618       (goto-char orig)
11619       (gnus-summary-position-point)
11620       eoi)))
11621
11622 (defun gnus-summary-maybe-hide-threads ()
11623   "If requested, hide the threads that should be hidden."
11624   (when (and gnus-show-threads
11625              gnus-thread-hide-subtree)
11626     (gnus-summary-hide-all-threads
11627      (if (or (consp gnus-thread-hide-subtree)
11628              (functionp gnus-thread-hide-subtree))
11629          (gnus-make-predicate gnus-thread-hide-subtree)
11630        nil))))
11631
11632 ;;; Hiding predicates.
11633
11634 (defun gnus-article-unread-p (header)
11635   (memq (mail-header-number header) gnus-newsgroup-unreads))
11636
11637 (defun gnus-article-unseen-p (header)
11638   (memq (mail-header-number header) gnus-newsgroup-unseen))
11639
11640 (defun gnus-map-articles (predicate articles)
11641   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11642   (apply 'gnus-or (mapcar predicate
11643                           (mapcar (lambda (number)
11644                                     (gnus-summary-article-header number))
11645                                   articles))))
11646
11647 (defun gnus-summary-hide-all-threads (&optional predicate)
11648   "Hide all thread subtrees.
11649 If PREDICATE is supplied, threads that satisfy this predicate
11650 will not be hidden."
11651   (interactive)
11652   (save-excursion
11653     (goto-char (point-min))
11654     (let ((end nil)
11655           (count 0))
11656       (while (not end)
11657         (incf count)
11658         (when (zerop (mod count 1000))
11659           (message "Hiding all threads... %d" count))
11660         (when (or (not predicate)
11661                   (gnus-map-articles
11662                    predicate (gnus-summary-article-children)))
11663             (gnus-summary-hide-thread))
11664         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11665   (gnus-summary-position-point))
11666
11667 (defun gnus-summary-hide-thread ()
11668   "Hide thread subtrees.
11669 If PREDICATE is supplied, threads that satisfy this predicate
11670 will not be hidden.
11671 Returns nil if no threads were there to be hidden."
11672   (interactive)
11673   (let ((start (point))
11674         (starteol (line-end-position))
11675         (article (gnus-summary-article-number)))
11676     (goto-char start)
11677     ;; Go forward until either the buffer ends or the subthread ends.
11678     (when (and (not (eobp))
11679                (or (zerop (gnus-summary-next-thread 1 t))
11680                    (goto-char (point-max))))
11681       (if (and (> (point) start)
11682                ;; FIXME: this should actually search for a non-invisible \n.
11683                (search-backward "\n" start t))
11684           (progn
11685             (when (> (point) starteol)
11686               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11687               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11688                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11689                 (gnus-overlay-put ol 'evaporate t)))
11690             (gnus-summary-goto-subject article)
11691             (when (> start (point))
11692               (message "Hiding the thread moved us backwards, aborting!")
11693               (goto-char (point-max))))
11694         (goto-char start)
11695         nil))))
11696
11697 (defun gnus-summary-go-to-next-thread (&optional previous)
11698   "Go to the same level (or less) next thread.
11699 If PREVIOUS is non-nil, go to previous thread instead.
11700 Return the article number moved to, or nil if moving was impossible."
11701   (let ((level (gnus-summary-thread-level))
11702         (way (if previous -1 1))
11703         (beg (point)))
11704     (forward-line way)
11705     (while (and (not (eobp))
11706                 (< level (gnus-summary-thread-level)))
11707       (forward-line way))
11708     (if (eobp)
11709         (progn
11710           (goto-char beg)
11711           nil)
11712       (setq beg (point))
11713       (prog1
11714           (gnus-summary-article-number)
11715         (goto-char beg)))))
11716
11717 (defun gnus-summary-next-thread (n &optional silent)
11718   "Go to the same level next N'th thread.
11719 If N is negative, search backward instead.
11720 Returns the difference between N and the number of skips actually
11721 done.
11722
11723 If SILENT, don't output messages."
11724   (interactive "p")
11725   (let ((backward (< n 0))
11726         (n (abs n)))
11727     (while (and (> n 0)
11728                 (gnus-summary-go-to-next-thread backward))
11729       (decf n))
11730     (unless silent
11731       (gnus-summary-position-point))
11732     (when (and (not silent) (/= 0 n))
11733       (gnus-message 7 "No more threads"))
11734     n))
11735
11736 (defun gnus-summary-prev-thread (n)
11737   "Go to the same level previous N'th thread.
11738 Returns the difference between N and the number of skips actually
11739 done."
11740   (interactive "p")
11741   (gnus-summary-next-thread (- n)))
11742
11743 (defun gnus-summary-go-down-thread ()
11744   "Go down one level in the current thread."
11745   (let ((children (gnus-summary-article-children)))
11746     (when children
11747       (gnus-summary-goto-subject (car children)))))
11748
11749 (defun gnus-summary-go-up-thread ()
11750   "Go up one level in the current thread."
11751   (let ((parent (gnus-summary-article-parent)))
11752     (when parent
11753       (gnus-summary-goto-subject parent))))
11754
11755 (defun gnus-summary-down-thread (n)
11756   "Go down thread N steps.
11757 If N is negative, go up instead.
11758 Returns the difference between N and how many steps down that were
11759 taken."
11760   (interactive "p")
11761   (let ((up (< n 0))
11762         (n (abs n)))
11763     (while (and (> n 0)
11764                 (if up (gnus-summary-go-up-thread)
11765                   (gnus-summary-go-down-thread)))
11766       (setq n (1- n)))
11767     (gnus-summary-position-point)
11768     (when (/= 0 n)
11769       (gnus-message 7 "Can't go further"))
11770     n))
11771
11772 (defun gnus-summary-up-thread (n)
11773   "Go up thread N steps.
11774 If N is negative, go down instead.
11775 Returns the difference between N and how many steps down that were
11776 taken."
11777   (interactive "p")
11778   (gnus-summary-down-thread (- n)))
11779
11780 (defun gnus-summary-top-thread ()
11781   "Go to the top of the thread."
11782   (interactive)
11783   (while (gnus-summary-go-up-thread))
11784   (gnus-summary-article-number))
11785
11786 (defun gnus-summary-expire-thread ()
11787   "Mark articles under current thread as expired."
11788   (interactive)
11789   (gnus-summary-kill-thread 0))
11790
11791 (defun gnus-summary-kill-thread (&optional unmark)
11792   "Mark articles under current thread as read.
11793 If the prefix argument is positive, remove any kinds of marks.
11794 If the prefix argument is zero, mark thread as expired.
11795 If the prefix argument is negative, tick articles instead."
11796   (interactive "P")
11797   (when unmark
11798     (setq unmark (prefix-numeric-value unmark)))
11799   (let ((articles (gnus-summary-articles-in-thread))
11800         (hide (or (null unmark) (= unmark 0))))
11801     (save-excursion
11802       ;; Expand the thread.
11803       (gnus-summary-show-thread)
11804       ;; Mark all the articles.
11805       (while articles
11806         (gnus-summary-goto-subject (car articles))
11807         (cond ((null unmark)
11808                (gnus-summary-mark-article-as-read gnus-killed-mark))
11809               ((> unmark 0)
11810                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11811               ((= unmark 0)
11812                (gnus-summary-mark-article nil gnus-expirable-mark))
11813               (t
11814                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11815         (setq articles (cdr articles))))
11816     ;; Hide killed subtrees when hide is true.
11817     (and hide
11818          gnus-thread-hide-killed
11819          (gnus-summary-hide-thread))
11820     ;; If hide is t, go to next unread subject.
11821     (when hide
11822       ;; Go to next unread subject.
11823       (gnus-summary-next-subject 1 t)))
11824   (gnus-set-mode-line 'summary))
11825
11826 ;; Summary sorting commands
11827
11828 (defun gnus-summary-sort-by-number (&optional reverse)
11829   "Sort the summary buffer by article number.
11830 Argument REVERSE means reverse order."
11831   (interactive "P")
11832   (gnus-summary-sort 'number reverse))
11833
11834 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11835   "Sort the summary buffer by most recent article number.
11836 Argument REVERSE means reverse order."
11837   (interactive "P")
11838   (gnus-summary-sort 'most-recent-number reverse))
11839
11840 (defun gnus-summary-sort-by-random (&optional reverse)
11841   "Randomize the order in the summary buffer.
11842 Argument REVERSE means to randomize in reverse order."
11843   (interactive "P")
11844   (gnus-summary-sort 'random reverse))
11845
11846 (defun gnus-summary-sort-by-author (&optional reverse)
11847   "Sort the summary buffer by author name alphabetically.
11848 If `case-fold-search' is non-nil, case of letters is ignored.
11849 Argument REVERSE means reverse order."
11850   (interactive "P")
11851   (gnus-summary-sort 'author reverse))
11852
11853 (defun gnus-summary-sort-by-recipient (&optional reverse)
11854   "Sort the summary buffer by recipient name alphabetically.
11855 If `case-fold-search' is non-nil, case of letters is ignored.
11856 Argument REVERSE means reverse order."
11857   (interactive "P")
11858   (gnus-summary-sort 'recipient reverse))
11859
11860 (defun gnus-summary-sort-by-subject (&optional reverse)
11861   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11862 If `case-fold-search' is non-nil, case of letters is ignored.
11863 Argument REVERSE means reverse order."
11864   (interactive "P")
11865   (gnus-summary-sort 'subject reverse))
11866
11867 (defun gnus-summary-sort-by-date (&optional reverse)
11868   "Sort the summary buffer by date.
11869 Argument REVERSE means reverse order."
11870   (interactive "P")
11871   (gnus-summary-sort 'date reverse))
11872
11873 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11874   "Sort the summary buffer by most recent date.
11875 Argument REVERSE means reverse order."
11876   (interactive "P")
11877   (gnus-summary-sort 'most-recent-date reverse))
11878
11879 (defun gnus-summary-sort-by-score (&optional reverse)
11880   "Sort the summary buffer by score.
11881 Argument REVERSE means reverse order."
11882   (interactive "P")
11883   (gnus-summary-sort 'score reverse))
11884
11885 (defun gnus-summary-sort-by-lines (&optional reverse)
11886   "Sort the summary buffer by the number of lines.
11887 Argument REVERSE means reverse order."
11888   (interactive "P")
11889   (gnus-summary-sort 'lines reverse))
11890
11891 (defun gnus-summary-sort-by-chars (&optional reverse)
11892   "Sort the summary buffer by article length.
11893 Argument REVERSE means reverse order."
11894   (interactive "P")
11895   (gnus-summary-sort 'chars reverse))
11896
11897 (defun gnus-summary-sort-by-original (&optional reverse)
11898   "Sort the summary buffer using the default sorting method.
11899 Argument REVERSE means reverse order."
11900   (interactive "P")
11901   (let* ((inhibit-read-only t)
11902          (gnus-summary-prepare-hook nil))
11903     ;; We do the sorting by regenerating the threads.
11904     (gnus-summary-prepare)
11905     ;; Hide subthreads if needed.
11906     (gnus-summary-maybe-hide-threads)))
11907
11908 (defun gnus-summary-sort (predicate reverse)
11909   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11910   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11911          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11912          (gnus-thread-sort-functions
11913           (if (not reverse)
11914               thread
11915             `(lambda (t1 t2)
11916                (,thread t2 t1))))
11917          (gnus-sort-gathered-threads-function
11918           gnus-thread-sort-functions)
11919          (gnus-article-sort-functions
11920           (if (not reverse)
11921               article
11922             `(lambda (t1 t2)
11923                (,article t2 t1))))
11924          (inhibit-read-only t)
11925          (gnus-summary-prepare-hook nil))
11926     ;; We do the sorting by regenerating the threads.
11927     (gnus-summary-prepare)
11928     ;; Hide subthreads if needed.
11929     (gnus-summary-maybe-hide-threads)))
11930
11931 ;; Summary saving commands.
11932
11933 (defun gnus-summary-save-article (&optional n not-saved)
11934   "Save the current article using the default saver function.
11935 If N is a positive number, save the N next articles.
11936 If N is a negative number, save the N previous articles.
11937 If N is nil and any articles have been marked with the process mark,
11938 save those articles instead.
11939 The variable `gnus-default-article-saver' specifies the saver function.
11940
11941 If the optional second argument NOT-SAVED is non-nil, articles saved
11942 will not be marked as saved."
11943   (interactive "P")
11944   (require 'gnus-art)
11945   (let* ((articles (gnus-summary-work-articles n))
11946          (save-buffer (save-excursion
11947                         (nnheader-set-temp-buffer " *Gnus Save*")))
11948          (num (length articles))
11949          ;; Whether to save decoded articles or raw articles.
11950          (decode (when gnus-article-save-coding-system
11951                    (get gnus-default-article-saver :decode)))
11952          ;; When saving many articles in a single file, use the other
11953          ;; function to save articles other than the first one.
11954          (saver2 (get gnus-default-article-saver :function))
11955          (gnus-prompt-before-saving (if saver2
11956                                         t
11957                                       gnus-prompt-before-saving))
11958          (gnus-default-article-saver gnus-default-article-saver)
11959          header file)
11960     (dolist (article articles)
11961       (setq header (gnus-summary-article-header article))
11962       (if (not (vectorp header))
11963           ;; This is a pseudo-article.
11964           (if (assq 'name header)
11965               (gnus-copy-file (cdr (assq 'name header)))
11966             (gnus-message 1 "Article %d is unsavable" article))
11967         ;; This is a real article.
11968         (save-window-excursion
11969           (gnus-summary-select-article decode decode nil article)
11970           (gnus-summary-goto-subject article))
11971         (with-current-buffer save-buffer
11972           (erase-buffer)
11973           (insert-buffer-substring (if decode
11974                                        gnus-article-buffer
11975                                      gnus-original-article-buffer)))
11976         (setq file (gnus-article-save save-buffer file num))
11977         (gnus-summary-remove-process-mark article)
11978         (unless not-saved
11979           (gnus-summary-set-saved-mark article)))
11980       (when saver2
11981         (setq gnus-default-article-saver saver2
11982               saver2 nil)))
11983     (gnus-kill-buffer save-buffer)
11984     (gnus-summary-position-point)
11985     (gnus-set-mode-line 'summary)
11986     n))
11987
11988 (defun gnus-summary-pipe-output (&optional n sym)
11989   "Pipe the current article to a subprocess.
11990 If N is a positive number, pipe the N next articles.
11991 If N is a negative number, pipe the N previous articles.
11992 If N is nil and any articles have been marked with the process mark,
11993 pipe those articles instead.
11994 The default command to which articles are piped is specified by the
11995 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11996 will be prompted for the command.
11997
11998 The properties `:decode' and `:headers' that are put to the function
11999 symbol `gnus-summary-save-in-pipe' control whether this function
12000 decodes articles and what headers to keep (see the doc string for the
12001 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
12002 is neither omitted nor the symbol `r', force including all headers
12003 regardless of the `:headers' property.  If it is the symbol `r',
12004 articles that are not decoded and include all headers will be piped
12005 no matter what the properties `:decode' and `:headers' are."
12006   (interactive (gnus-interactive "P\ny"))
12007   (require 'gnus-art)
12008   (let* ((articles (gnus-summary-work-articles n))
12009          (result-buffer "*Shell Command Output*")
12010          (all-headers (not (memq sym '(nil r))))
12011          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
12012          (raw (eq sym 'r))
12013          (headers (get 'gnus-summary-save-in-pipe :headers))
12014          command result)
12015     (unless (numberp (car articles))
12016       (error "No article to pipe"))
12017     (setq command (gnus-read-shell-command
12018                    (concat "Shell command on "
12019                            (if (cdr articles)
12020                                (format "these %d articles" (length articles))
12021                              "this article")
12022                            ": ")
12023                    gnus-summary-pipe-output-default-command))
12024     (when (string-equal command "")
12025       (error "A command is required"))
12026     (when all-headers
12027       (put 'gnus-summary-save-in-pipe :headers nil))
12028     (unwind-protect
12029         (while articles
12030           (gnus-summary-goto-subject (pop articles))
12031           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12032           (when (and (get-buffer result-buffer)
12033                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12034             (setq result (concat result (with-current-buffer result-buffer
12035                                           (buffer-string))))))
12036       (put 'gnus-summary-save-in-pipe :headers headers))
12037     (unless (zerop (length result))
12038       (if (with-current-buffer (get-buffer-create result-buffer)
12039             (erase-buffer)
12040             (insert result)
12041             (prog1
12042                 (and (= (count-lines (point-min) (point)) 1)
12043                      (progn
12044                        (end-of-line 0)
12045                        (<= (current-column)
12046                            (window-width (minibuffer-window)))))
12047               (goto-char (point-min))))
12048           (message "%s" (substring result 0 -1))
12049         (message nil)
12050         (gnus-configure-windows 'pipe)))))
12051
12052 (defun gnus-summary-save-article-mail (&optional arg)
12053   "Append the current article to a Unix mail box file.
12054 If N is a positive number, save the N next articles.
12055 If N is a negative number, save the N previous articles.
12056 If N is nil and any articles have been marked with the process mark,
12057 save those articles instead."
12058   (interactive "P")
12059   (require 'gnus-art)
12060   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12061     (gnus-summary-save-article arg)))
12062
12063 (defun gnus-summary-save-article-rmail (&optional arg)
12064   "Append the current article to an rmail file.
12065 If N is a positive number, save the N next articles.
12066 If N is a negative number, save the N previous articles.
12067 If N is nil and any articles have been marked with the process mark,
12068 save those articles instead."
12069   (interactive "P")
12070   (require 'gnus-art)
12071   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12072     (gnus-summary-save-article arg)))
12073
12074 (defun gnus-summary-save-article-file (&optional arg)
12075   "Append the current article to a file.
12076 If N is a positive number, save the N next articles.
12077 If N is a negative number, save the N previous articles.
12078 If N is nil and any articles have been marked with the process mark,
12079 save those articles instead."
12080   (interactive "P")
12081   (require 'gnus-art)
12082   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12083     (gnus-summary-save-article arg)))
12084
12085 (defun gnus-summary-write-article-file (&optional arg)
12086   "Write the current article to a file, deleting the previous file.
12087 If N is a positive number, save the N next articles.
12088 If N is a negative number, save the N previous articles.
12089 If N is nil and any articles have been marked with the process mark,
12090 save those articles instead."
12091   (interactive "P")
12092   (require 'gnus-art)
12093   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12094     (gnus-summary-save-article arg)))
12095
12096 (defun gnus-summary-save-article-body-file (&optional arg)
12097   "Append the current article body to a file.
12098 If N is a positive number, save the N next articles.
12099 If N is a negative number, save the N previous articles.
12100 If N is nil and any articles have been marked with the process mark,
12101 save those articles instead."
12102   (interactive "P")
12103   (require 'gnus-art)
12104   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12105     (gnus-summary-save-article arg)))
12106
12107 (defun gnus-summary-write-article-body-file (&optional arg)
12108   "Write the current article body to a file, deleting the previous file.
12109 If N is a positive number, save the N next articles.
12110 If N is a negative number, save the N previous articles.
12111 If N is nil and any articles have been marked with the process mark,
12112 save those articles instead."
12113   (interactive "P")
12114   (require 'gnus-art)
12115   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12116     (gnus-summary-save-article arg)))
12117
12118 (defun gnus-summary-muttprint (&optional arg)
12119   "Print the current article using Muttprint.
12120 If N is a positive number, save the N next articles.
12121 If N is a negative number, save the N previous articles.
12122 If N is nil and any articles have been marked with the process mark,
12123 save those articles instead."
12124   (interactive "P")
12125   (require 'gnus-art)
12126   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12127     (gnus-summary-save-article arg t)))
12128
12129 (defun gnus-summary-pipe-message (program)
12130   "Pipe the current article through PROGRAM."
12131   (interactive "sProgram: ")
12132   (gnus-summary-select-article)
12133   (let ((mail-header-separator ""))
12134     (gnus-eval-in-buffer-window gnus-article-buffer
12135       (save-restriction
12136         (widen)
12137         (let ((start (window-start))
12138               (inhibit-read-only t))
12139           (message-pipe-buffer-body program)
12140           (set-window-start (get-buffer-window (current-buffer)) start))))))
12141
12142 (defun gnus-get-split-value (methods)
12143   "Return a value based on the split METHODS."
12144   (let (split-name method result match)
12145     (when methods
12146       (with-current-buffer gnus-original-article-buffer
12147         (save-restriction
12148           (nnheader-narrow-to-headers)
12149           (while (and methods (not split-name))
12150             (goto-char (point-min))
12151             (setq method (pop methods))
12152             (setq match (car method))
12153             (when (cond
12154                    ((stringp match)
12155                     ;; Regular expression.
12156                     (ignore-errors
12157                       (re-search-forward match nil t)))
12158                    ((functionp match)
12159                     ;; Function.
12160                     (save-restriction
12161                       (widen)
12162                       (setq result (funcall match gnus-newsgroup-name))))
12163                    ((consp match)
12164                     ;; Form.
12165                     (save-restriction
12166                       (widen)
12167                       (setq result (eval match)))))
12168               (setq split-name (cdr method))
12169               (cond ((stringp result)
12170                      (push (expand-file-name
12171                             result gnus-article-save-directory)
12172                            split-name))
12173                     ((consp result)
12174                      (setq split-name (append result split-name)))))))))
12175     (nreverse split-name)))
12176
12177 (defun gnus-valid-move-group-p (group)
12178   (and (symbolp group)
12179        (boundp group)
12180        (symbol-name group)
12181        (symbol-value group)
12182        (gnus-get-function (gnus-find-method-for-group
12183                            (symbol-name group)) 'request-accept-article t)))
12184
12185 (defun gnus-read-move-group-name (prompt default articles prefix)
12186   "Read a group name."
12187   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12188          (minibuffer-confirm-incomplete nil) ; XEmacs
12189          (prom
12190           (format "%s %s to"
12191                   prompt
12192                   (if (> (length articles) 1)
12193                       (format "these %d articles" (length articles))
12194                     "this article")))
12195          (to-newsgroup
12196           (cond
12197            ((null split-name)
12198             (gnus-group-completing-read
12199              prom
12200              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12201              nil prefix nil default))
12202            ((= 1 (length split-name))
12203             (gnus-group-completing-read
12204              prom
12205              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12206              nil prefix 'gnus-group-history (car split-name)))
12207            (t
12208             (gnus-completing-read
12209              prom (nreverse split-name) nil nil 'gnus-group-history))))
12210          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12211          encoded)
12212     (when to-newsgroup
12213       (if (or (string= to-newsgroup "")
12214               (string= to-newsgroup prefix))
12215           (setq to-newsgroup default))
12216       (unless to-newsgroup
12217         (error "No group name entered"))
12218       (setq encoded (mm-encode-coding-string
12219                      to-newsgroup
12220                      (gnus-group-name-charset to-method to-newsgroup)))
12221       (or (gnus-active encoded)
12222           (gnus-activate-group encoded nil nil to-method)
12223           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12224                                      to-newsgroup))
12225               (or (and (gnus-request-create-group encoded to-method)
12226                        (gnus-activate-group encoded nil nil to-method)
12227                        (gnus-subscribe-group encoded))
12228                   (error "Couldn't create group %s" to-newsgroup)))
12229           (error "No such group: %s" to-newsgroup))
12230       encoded)))
12231
12232 (defvar gnus-summary-save-parts-counter)
12233 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12234
12235 (defun gnus-summary-save-parts (type dir n &optional reverse)
12236   "Save parts matching TYPE to DIR.
12237 If REVERSE, save parts that do not match TYPE."
12238   (interactive
12239    (list (read-string "Save parts of type: "
12240                       (or (car gnus-summary-save-parts-type-history)
12241                           gnus-summary-save-parts-default-mime)
12242                       'gnus-summary-save-parts-type-history)
12243          (setq gnus-summary-save-parts-last-directory
12244                (read-directory-name "Save to directory: "
12245                                     gnus-summary-save-parts-last-directory
12246                                     nil t))
12247          current-prefix-arg))
12248   (gnus-summary-iterate n
12249     (let ((gnus-display-mime-function nil)
12250           gnus-article-prepare-hook
12251           gnus-article-decode-hook
12252           gnus-display-mime-function
12253           gnus-break-pages
12254           (gnus-inhibit-treatment t))
12255       (gnus-summary-select-article))
12256     (with-current-buffer gnus-article-buffer
12257       (let ((handles (or gnus-article-mime-handles
12258                          (mm-dissect-buffer nil gnus-article-loose-mime)
12259                          (and gnus-article-emulate-mime
12260                               (mm-uu-dissect))))
12261             (gnus-summary-save-parts-counter 1))
12262         (when handles
12263           (gnus-summary-save-parts-1 type dir handles reverse)
12264           (unless gnus-article-mime-handles ;; Don't destroy this case.
12265             (mm-destroy-parts handles)))))))
12266
12267 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12268   (if (stringp (car handle))
12269       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12270               (cdr handle))
12271     (when (if reverse
12272               (not (string-match type (mm-handle-media-type handle)))
12273             (string-match type (mm-handle-media-type handle)))
12274       (let ((file (expand-file-name
12275                    (gnus-map-function
12276                     mm-file-name-rewrite-functions
12277                     (file-name-nondirectory
12278                      (or
12279                       (mm-handle-filename handle)
12280                       (format "%s.%d.%d" gnus-newsgroup-name
12281                               (cdr gnus-article-current)
12282                               gnus-summary-save-parts-counter))))
12283                    dir)))
12284         (incf gnus-summary-save-parts-counter)
12285         (unless (file-exists-p file)
12286           (mm-save-part-to-file handle file))))))
12287
12288 ;; Summary extract commands
12289
12290 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12291   (let ((inhibit-read-only t)
12292         (article (gnus-summary-article-number))
12293         after-article b e)
12294     (unless (gnus-summary-goto-subject article)
12295       (error "No such article: %d" article))
12296     (gnus-summary-position-point)
12297     ;; If all commands are to be bunched up on one line, we collect
12298     ;; them here.
12299     (unless gnus-view-pseudos-separately
12300       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12301             files action)
12302         (while ps
12303           (setq action (cdr (assq 'action (car ps))))
12304           (setq files (list (cdr (assq 'name (car ps)))))
12305           (while (and ps (cdr ps)
12306                       (string= (or action "1")
12307                                (or (cdr (assq 'action (cadr ps))) "2")))
12308             (push (cdr (assq 'name (cadr ps))) files)
12309             (setcdr ps (cddr ps)))
12310           (when files
12311             (when (not (string-match "%s" action))
12312               (push " " files))
12313             (push " " files)
12314             (when (assq 'execute (car ps))
12315               (setcdr (assq 'execute (car ps))
12316                       (funcall (if (string-match "%s" action)
12317                                    'format 'concat)
12318                                action
12319                                (mapconcat
12320                                 (lambda (f)
12321                                   (if (equal f " ")
12322                                       f
12323                                     (shell-quote-argument f)))
12324                                 files " ")))))
12325           (setq ps (cdr ps)))))
12326     (if (and gnus-view-pseudos (not not-view))
12327         (while pslist
12328           (when (assq 'execute (car pslist))
12329             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12330                                   (eq gnus-view-pseudos 'not-confirm)))
12331           (setq pslist (cdr pslist)))
12332       (save-excursion
12333         (while pslist
12334           (setq after-article (or (cdr (assq 'article (car pslist)))
12335                                   (gnus-summary-article-number)))
12336           (gnus-summary-goto-subject after-article)
12337           (forward-line 1)
12338           (setq b (point))
12339           (insert "    " (file-name-nondirectory
12340                           (cdr (assq 'name (car pslist))))
12341                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12342           (setq e (point))
12343           (forward-line -1)             ; back to `b'
12344           (gnus-add-text-properties
12345            b (1- e) (list 'gnus-number gnus-reffed-article-number
12346                           gnus-mouse-face-prop gnus-mouse-face))
12347           (gnus-data-enter
12348            after-article gnus-reffed-article-number
12349            gnus-unread-mark b (car pslist) 0 (- e b))
12350           (setq gnus-newsgroup-unreads
12351                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12352                                          gnus-reffed-article-number))
12353           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12354           (setq pslist (cdr pslist)))))))
12355
12356 (defun gnus-pseudos< (p1 p2)
12357   (let ((c1 (cdr (assq 'action p1)))
12358         (c2 (cdr (assq 'action p2))))
12359     (and c1 c2 (string< c1 c2))))
12360
12361 (defun gnus-request-pseudo-article (props)
12362   (cond ((assq 'execute props)
12363          (gnus-execute-command (cdr (assq 'execute props)))))
12364   (let ((gnus-current-article (gnus-summary-article-number)))
12365     (gnus-run-hooks 'gnus-mark-article-hook)))
12366
12367 (defun gnus-execute-command (command &optional automatic)
12368   (save-excursion
12369     (gnus-article-setup-buffer)
12370     (set-buffer gnus-article-buffer)
12371     (setq buffer-read-only nil)
12372     (let ((command (if automatic command
12373                      (read-string "Command: " (cons command 0)))))
12374       (erase-buffer)
12375       (insert "$ " command "\n\n")
12376       (if gnus-view-pseudo-asynchronously
12377           (start-process "gnus-execute" (current-buffer) shell-file-name
12378                          shell-command-switch command)
12379         (call-process shell-file-name nil t nil
12380                       shell-command-switch command)))))
12381
12382 ;; Summary kill commands.
12383
12384 (defun gnus-summary-edit-global-kill (article)
12385   "Edit the \"global\" kill file."
12386   (interactive (list (gnus-summary-article-number)))
12387   (gnus-group-edit-global-kill article))
12388
12389 (defun gnus-summary-edit-local-kill ()
12390   "Edit a local kill file applied to the current newsgroup."
12391   (interactive)
12392   (setq gnus-current-headers (gnus-summary-article-header))
12393   (gnus-group-edit-local-kill
12394    (gnus-summary-article-number) gnus-newsgroup-name))
12395
12396 ;;; Header reading.
12397
12398 (defun gnus-read-header (id &optional header)
12399   "Read the headers of article ID and enter them into the Gnus system."
12400   (let ((group gnus-newsgroup-name)
12401         (gnus-override-method
12402          (or
12403           gnus-override-method
12404           (and (gnus-news-group-p gnus-newsgroup-name)
12405                (car (gnus-refer-article-methods)))))
12406         where)
12407     ;; First we check to see whether the header in question is already
12408     ;; fetched.
12409     (if (stringp id)
12410         ;; This is a Message-ID.
12411         (setq header (or header (gnus-id-to-header id)))
12412       ;; This is an article number.
12413       (setq header (or header (gnus-summary-article-header id))))
12414     (if (and header
12415              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12416         ;; We have found the header.
12417         header
12418       ;; We have to really fetch the header to this article.
12419       (with-current-buffer nntp-server-buffer
12420         (when (setq where (gnus-request-head id group))
12421           (nnheader-fold-continuation-lines)
12422           (goto-char (point-max))
12423           (insert ".\n")
12424           (goto-char (point-min))
12425           (insert "211 ")
12426           (princ (cond
12427                   ((numberp id) id)
12428                   ((cdr where) (cdr where))
12429                   (header (mail-header-number header))
12430                   (t gnus-reffed-article-number))
12431                  (current-buffer))
12432           (insert " Article retrieved.\n"))
12433         (if (or (not where)
12434                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12435             ()                          ; Malformed head.
12436           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12437             (when (and (bound-and-true-p gnus-registry-enabled)
12438                        (not (gnus-ephemeral-group-p (car where))))
12439               (gnus-registry-handle-action
12440                (mail-header-id header) nil
12441                (gnus-group-prefixed-name (car where) gnus-override-method)
12442                (mail-header-subject header)
12443                (mail-header-from header)))
12444             (when (and (stringp id)
12445                        (or
12446                         (not (string= (gnus-group-real-name group)
12447                                       (car where)))
12448                         (not (gnus-server-equal gnus-override-method
12449                                                 (gnus-group-method group)))))
12450               ;; If we fetched by Message-ID and the article came from
12451               ;; a different group (or server), we fudge some bogus
12452               ;; article numbers for this article.
12453               (mail-header-set-number header gnus-reffed-article-number))
12454             (with-current-buffer gnus-summary-buffer
12455               (decf gnus-reffed-article-number)
12456               (gnus-remove-header (mail-header-number header))
12457               (push header gnus-newsgroup-headers)
12458               (setq gnus-current-headers header)
12459               (push (mail-header-number header) gnus-newsgroup-limit)))
12460           header)))))
12461
12462 (defun gnus-remove-header (number)
12463   "Remove header NUMBER from `gnus-newsgroup-headers'."
12464   (if (and gnus-newsgroup-headers
12465            (= number (mail-header-number (car gnus-newsgroup-headers))))
12466       (pop gnus-newsgroup-headers)
12467     (let ((headers gnus-newsgroup-headers))
12468       (while (and (cdr headers)
12469                   (not (= number (mail-header-number (cadr headers)))))
12470         (pop headers))
12471       (when (cdr headers)
12472         (setcdr headers (cddr headers))))))
12473
12474 ;;;
12475 ;;; summary highlights
12476 ;;;
12477
12478 (defun gnus-highlight-selected-summary ()
12479   "Highlight selected article in summary buffer."
12480   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12481   (when gnus-summary-selected-face
12482     (save-excursion
12483       (let* ((beg (point-at-bol))
12484              (end (point-at-eol))
12485              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12486              (from (if (get-text-property beg gnus-mouse-face-prop)
12487                        beg
12488                      (or (next-single-property-change
12489                           beg gnus-mouse-face-prop nil end)
12490                          beg)))
12491              (to
12492               (if (= from end)
12493                   (- from 2)
12494                 (or (next-single-property-change
12495                      from gnus-mouse-face-prop nil end)
12496                     end))))
12497         ;; If no mouse-face prop on line we will have to = from = end,
12498         ;; so we highlight the entire line instead.
12499         (when (= (+ to 2) from)
12500           (setq from beg)
12501           (setq to end))
12502         (if gnus-newsgroup-selected-overlay
12503             ;; Move old overlay.
12504             (gnus-move-overlay
12505              gnus-newsgroup-selected-overlay from to (current-buffer))
12506           ;; Create new overlay.
12507           (gnus-overlay-put
12508            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12509            'face gnus-summary-selected-face))))))
12510
12511 (defvar gnus-summary-highlight-line-cached nil)
12512 (defvar gnus-summary-highlight-line-trigger nil)
12513
12514 (defun gnus-summary-highlight-line-0 ()
12515   (if (and (eq gnus-summary-highlight-line-trigger
12516                gnus-summary-highlight)
12517            gnus-summary-highlight-line-cached)
12518       gnus-summary-highlight-line-cached
12519     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12520           gnus-summary-highlight-line-cached
12521           (let* ((cond (list 'cond))
12522                  (c cond)
12523                  (list gnus-summary-highlight))
12524             (while list
12525               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12526                               nil))
12527               (setq c (cdr c)
12528                     list (cdr list)))
12529             (gnus-byte-compile (list 'lambda nil cond))))))
12530
12531 (defun gnus-summary-highlight-line ()
12532   "Highlight current line according to `gnus-summary-highlight'."
12533   (let* ((beg (point-at-bol))
12534          (article (or (gnus-summary-article-number) gnus-current-article))
12535          (score (or (cdr (assq article
12536                                gnus-newsgroup-scored))
12537                     gnus-summary-default-score 0))
12538          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12539          (inhibit-read-only t)
12540          (default gnus-summary-default-score)
12541          (default-high gnus-summary-default-high-score)
12542          (default-low gnus-summary-default-low-score)
12543          (uncached (and gnus-summary-use-undownloaded-faces
12544                         (memq article gnus-newsgroup-undownloaded)
12545                         (not (memq article gnus-newsgroup-cached)))))
12546     (let ((face (funcall (gnus-summary-highlight-line-0))))
12547       (unless (eq face (get-text-property beg 'face))
12548         (gnus-put-text-property-excluding-characters-with-faces
12549          beg (point-at-eol) 'face
12550          (setq face (if (boundp face) (symbol-value face) face)))
12551         (when gnus-summary-highlight-line-function
12552           (funcall gnus-summary-highlight-line-function article face))))))
12553
12554 (defun gnus-update-read-articles (group unread &optional compute)
12555   "Update the list of read articles in GROUP.
12556 UNREAD is a sorted list."
12557   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12558         (info (gnus-get-info group))
12559         (prev 1)
12560         read)
12561     (if (or (not info) (not active))
12562         ;; There is no info on this group if it was, in fact,
12563         ;; killed.  Gnus stores no information on killed groups, so
12564         ;; there's nothing to be done.
12565         ;; One could store the information somewhere temporarily,
12566         ;; perhaps...  Hmmm...
12567         ()
12568       ;; Remove any negative articles numbers.
12569       (while (and unread (< (car unread) 0))
12570         (setq unread (cdr unread)))
12571       ;; Remove any expired article numbers
12572       (while (and unread (< (car unread) (car active)))
12573         (setq unread (cdr unread)))
12574       ;; Compute the ranges of read articles by looking at the list of
12575       ;; unread articles.
12576       (while unread
12577         (when (/= (car unread) prev)
12578           (push (if (= prev (1- (car unread))) prev
12579                   (cons prev (1- (car unread))))
12580                 read))
12581         (setq prev (1+ (car unread)))
12582         (setq unread (cdr unread)))
12583       (when (<= prev (cdr active))
12584         (push (cons prev (cdr active)) read))
12585       (setq read (if (> (length read) 1) (nreverse read) read))
12586       (if compute
12587           read
12588         (save-excursion
12589           (let (setmarkundo)
12590             ;; Propagate the read marks to the backend.
12591             (when (and (gnus-method-option-p
12592                         (gnus-find-method-for-group group)
12593                         'server-marks)
12594                        (gnus-check-backend-function 'request-set-mark group))
12595               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12596                     (add (gnus-remove-from-range read (gnus-info-read info))))
12597                 (when (or add del)
12598                   (unless (gnus-check-group group)
12599                     (error "Can't open server for %s" group))
12600                   (gnus-request-set-mark
12601                    group (delq nil (list (if add (list add 'add '(read)))
12602                                          (if del (list del 'del '(read))))))
12603                   (setq setmarkundo
12604                         `(gnus-request-set-mark
12605                           ,group
12606                           ',(delq nil (list
12607                                        (if del (list del 'add '(read)))
12608                                        (if add (list add 'del '(read))))))))))
12609             (set-buffer gnus-group-buffer)
12610             (gnus-undo-register
12611               `(progn
12612                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12613                  (gnus-info-set-read ',info ',(gnus-info-read info))
12614                  (gnus-get-unread-articles-in-group ',info
12615                                                     (gnus-active ,group))
12616                  (gnus-group-update-group ,group t)
12617                  ,setmarkundo))))
12618         ;; Enter this list into the group info.
12619         (gnus-info-set-read info read)
12620         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12621         (gnus-get-unread-articles-in-group info (gnus-active group))
12622         t))))
12623
12624 (defun gnus-offer-save-summaries ()
12625   "Offer to save all active summary buffers."
12626   (let (buffers)
12627     ;; Go through all buffers and find all summaries.
12628     (dolist (buffer (buffer-list))
12629       (when (and (setq buffer (buffer-name buffer))
12630                  (string-match "Summary" buffer)
12631                  (with-current-buffer buffer
12632                    ;; We check that this is, indeed, a summary buffer.
12633                    (and (eq major-mode 'gnus-summary-mode)
12634                         ;; Also make sure this isn't bogus.
12635                         gnus-newsgroup-prepared
12636                         ;; Also make sure that this isn't a
12637                         ;; dead summary buffer.
12638                         (not gnus-dead-summary-mode))))
12639         (push buffer buffers)))
12640     ;; Go through all these summary buffers and offer to save them.
12641     (when buffers
12642       (save-excursion
12643         (if (eq gnus-interactive-exit 'quiet)
12644             (dolist (buffer buffers)
12645               (switch-to-buffer buffer)
12646               (gnus-summary-exit))
12647           (map-y-or-n-p
12648            "Update summary buffer %s? "
12649            (lambda (buf)
12650              (switch-to-buffer buf)
12651              (gnus-summary-exit))
12652            buffers))))))
12653
12654 (defun gnus-summary-setup-default-charset ()
12655   "Setup newsgroup default charset."
12656   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12657       (setq gnus-newsgroup-charset nil)
12658     (let* ((ignored-charsets
12659             (or gnus-newsgroup-ephemeral-ignored-charsets
12660                 (append
12661                  (and gnus-newsgroup-name
12662                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12663                  gnus-newsgroup-ignored-charsets))))
12664       (setq gnus-newsgroup-charset
12665             (or gnus-newsgroup-ephemeral-charset
12666                 (and gnus-newsgroup-name
12667                      (gnus-parameter-charset gnus-newsgroup-name))
12668                 gnus-default-charset))
12669       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12670            ignored-charsets))))
12671
12672 ;;;
12673 ;;; Mime Commands
12674 ;;;
12675
12676 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12677   "Display the current article buffer fully MIME-buttonized.
12678 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12679 treated as multipart/mixed."
12680   (interactive "P")
12681   (require 'gnus-art)
12682   (let ((gnus-unbuttonized-mime-types nil)
12683         (gnus-mime-display-multipart-as-mixed show-all-parts))
12684     (gnus-summary-show-article)))
12685
12686 (defun gnus-summary-repair-multipart (article)
12687   "Add a Content-Type header to a multipart article without one."
12688   (interactive (list (gnus-summary-article-number)))
12689   (gnus-with-article article
12690     (message-narrow-to-head)
12691     (message-remove-header "Mime-Version")
12692     (goto-char (point-max))
12693     (insert "Mime-Version: 1.0\n")
12694     (widen)
12695     (when (search-forward "\n--" nil t)
12696       (let ((separator (buffer-substring (point) (point-at-eol))))
12697         (message-narrow-to-head)
12698         (message-remove-header "Content-Type")
12699         (goto-char (point-max))
12700         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12701                         separator))
12702         (widen))))
12703   (let (gnus-mark-article-hook)
12704     (gnus-summary-select-article t t nil article)))
12705
12706 (defun gnus-summary-toggle-display-buttonized ()
12707   "Toggle the buttonizing of the article buffer."
12708   (interactive)
12709   (require 'gnus-art)
12710   (if (setq gnus-inhibit-mime-unbuttonizing
12711             (not gnus-inhibit-mime-unbuttonizing))
12712       (let ((gnus-unbuttonized-mime-types nil))
12713         (gnus-summary-show-article))
12714     (gnus-summary-show-article)))
12715
12716 ;;;
12717 ;;; Generic summary marking commands
12718 ;;;
12719
12720 (defvar gnus-summary-marking-alist
12721   '((read gnus-del-mark "d")
12722     (unread gnus-unread-mark "u")
12723     (ticked gnus-ticked-mark "!")
12724     (dormant gnus-dormant-mark "?")
12725     (expirable gnus-expirable-mark "e"))
12726   "An alist of names/marks/keystrokes.")
12727
12728 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12729 (defvar gnus-summary-mark-map)
12730
12731 (defun gnus-summary-make-all-marking-commands ()
12732   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12733   (dolist (elem gnus-summary-marking-alist)
12734     (apply 'gnus-summary-make-marking-command elem)))
12735
12736 (defun gnus-summary-make-marking-command (name mark keystroke)
12737   (let ((map (make-sparse-keymap)))
12738     (define-key gnus-summary-generic-mark-map keystroke map)
12739     (dolist (lway `((next "next" next nil "n")
12740                     (next-unread "next unread" next t "N")
12741                     (prev "previous" prev nil "p")
12742                     (prev-unread "previous unread" prev t "P")
12743                     (nomove "" nil nil ,keystroke)))
12744       (let ((func (gnus-summary-make-marking-command-1
12745                    mark (car lway) lway name)))
12746         (setq func (eval func))
12747         (define-key map (nth 4 lway) func)))))
12748
12749 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12750   `(defun ,(intern
12751             (format "gnus-summary-put-mark-as-%s%s"
12752                     name (if (eq way 'nomove)
12753                              ""
12754                            (concat "-" (symbol-name way)))))
12755      (n)
12756      ,(format
12757        "Mark the current article as %s%s.
12758 If N, the prefix, then repeat N times.
12759 If N is negative, move in reverse order.
12760 The difference between N and the actual number of articles marked is
12761 returned."
12762        name (cadr lway))
12763      (interactive "p")
12764      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12765
12766 (defun gnus-summary-generic-mark (n mark move unread)
12767   "Mark N articles with MARK."
12768   (unless (eq major-mode 'gnus-summary-mode)
12769     (error "This command can only be used in the summary buffer"))
12770   (gnus-summary-show-thread)
12771   (let ((nummove
12772          (cond
12773           ((eq move 'next) 1)
12774           ((eq move 'prev) -1)
12775           (t 0))))
12776     (if (zerop nummove)
12777         (setq n 1)
12778       (when (< n 0)
12779         (setq n (abs n)
12780               nummove (* -1 nummove))))
12781     (while (and (> n 0)
12782                 (gnus-summary-mark-article nil mark)
12783                 (zerop (gnus-summary-next-subject nummove unread t)))
12784       (setq n (1- n)))
12785     (when (/= 0 n)
12786       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12787     (gnus-summary-recenter)
12788     (gnus-summary-position-point)
12789     (gnus-set-mode-line 'summary)
12790     n))
12791
12792 (defun gnus-summary-insert-articles (articles)
12793   (when (setq articles
12794               (gnus-sorted-difference articles
12795                                       (mapcar (lambda (h)
12796                                                 (mail-header-number h))
12797                                               gnus-newsgroup-headers)))
12798     (setq gnus-newsgroup-headers
12799           (gnus-merge 'list
12800                       gnus-newsgroup-headers
12801                       (gnus-fetch-headers articles)
12802                       'gnus-article-sort-by-number))
12803     (setq gnus-newsgroup-articles
12804           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12805     ;; Suppress duplicates?
12806     (when gnus-suppress-duplicates
12807       (gnus-dup-suppress-articles))
12808
12809     (if (and gnus-fetch-old-headers
12810              (eq gnus-headers-retrieved-by 'nov))
12811         ;; We might want to build some more threads first.
12812         (if (eq gnus-fetch-old-headers 'invisible)
12813             (gnus-build-all-threads)
12814           (gnus-build-old-threads))
12815       ;; Mark the inserted articles that are unread as unread.
12816       (setq gnus-newsgroup-unreads
12817             (gnus-sorted-nunion
12818              gnus-newsgroup-unreads
12819              (gnus-sorted-nintersection
12820               (gnus-list-of-unread-articles gnus-newsgroup-name)
12821               articles)))
12822       ;; Mark the inserted articles as selected so that the information
12823       ;; of the marks having been changed by a user may be updated when
12824       ;; exiting this group.  See `gnus-summary-update-info'.
12825       (dolist (art articles)
12826         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12827     ;; Let the Gnus agent mark articles as read.
12828     (when gnus-agent
12829       (gnus-agent-get-undownloaded-list))
12830     ;; Remove list identifiers from subject
12831     (gnus-summary-remove-list-identifiers)
12832     ;; First and last article in this newsgroup.
12833     (when gnus-newsgroup-headers
12834       (setq gnus-newsgroup-begin
12835             (mail-header-number (car gnus-newsgroup-headers))
12836             gnus-newsgroup-end
12837             (mail-header-number
12838              (gnus-last-element gnus-newsgroup-headers))))
12839     (when gnus-use-scoring
12840       (gnus-possibly-score-headers))))
12841
12842 (defun gnus-summary-insert-old-articles (&optional all)
12843   "Insert all old articles in this group.
12844 If ALL is non-nil, already read articles become readable.
12845 If ALL is a number, fetch this number of articles."
12846   (interactive "P")
12847   (prog1
12848       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12849             older len)
12850         (setq older
12851               ;; Some nntp servers lie about their active range.  When
12852               ;; this happens, the active range can be in the millions.
12853               ;; Use a compressed range to avoid creating a huge list.
12854               (gnus-range-difference (list gnus-newsgroup-active) old))
12855         (setq len (gnus-range-length older))
12856         (cond
12857          ((null older) nil)
12858          ((numberp all)
12859           (if (< all len)
12860               (let ((older-range (nreverse older)))
12861                 (setq older nil)
12862
12863                 (while (> all 0)
12864                   (let* ((r (pop older-range))
12865                          (min (if (numberp r) r (car r)))
12866                          (max (if (numberp r) r (cdr r))))
12867                     (while (and (<= min max)
12868                                 (> all 0))
12869                       (push max older)
12870                       (setq all (1- all)
12871                             max (1- max))))))
12872             (setq older (gnus-uncompress-range older))))
12873          (all
12874           (setq older (gnus-uncompress-range older)))
12875          (t
12876           (when (and (numberp gnus-large-newsgroup)
12877                    (> len gnus-large-newsgroup))
12878               (let* ((cursor-in-echo-area nil)
12879                      (initial (gnus-parameter-large-newsgroup-initial
12880                                gnus-newsgroup-name))
12881                      (input
12882                       (read-string
12883                        (format
12884                         "How many articles from %s (%s %d): "
12885                         (gnus-group-decoded-name gnus-newsgroup-name)
12886                         (if initial "max" "default")
12887                         len)
12888                        nil nil
12889                        (and initial
12890                             (number-to-string initial)))))
12891                 (unless (string-match "^[ \t]*$" input)
12892                   (setq all (string-to-number input))
12893                   (if (< all len)
12894                       (let ((older-range (nreverse older)))
12895                         (setq older nil)
12896
12897                         (while (> all 0)
12898                           (let* ((r (pop older-range))
12899                                  (min (if (numberp r) r (car r)))
12900                                  (max (if (numberp r) r (cdr r))))
12901                             (while (and (<= min max)
12902                                         (> all 0))
12903                               (push max older)
12904                               (setq all (1- all)
12905                                     max (1- max))))))))))
12906           (setq older (gnus-uncompress-range older))))
12907         (if (not older)
12908             (message "No old news.")
12909           (gnus-summary-insert-articles older)
12910           (gnus-summary-limit (gnus-sorted-nunion old older))))
12911     (gnus-summary-position-point)))
12912
12913 (defun gnus-summary-insert-new-articles ()
12914   "Insert all new articles in this group."
12915   (interactive)
12916   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12917         (old-high gnus-newsgroup-highest)
12918         (nnmail-fetched-sources (list t))
12919         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12920         i new)
12921     (unless new-active
12922       (error "Couldn't fetch new data"))
12923     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12924     (setq i (cdr gnus-newsgroup-active)
12925           gnus-newsgroup-highest i)
12926     (while (> i old-high)
12927       (push i new)
12928       (decf i))
12929     (if (not new)
12930         (message "No gnus is bad news")
12931       (gnus-summary-insert-articles new)
12932       (setq gnus-newsgroup-unreads
12933             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12934       (gnus-summary-limit (gnus-sorted-nunion old new))))
12935   (gnus-summary-position-point))
12936
12937 ;;; Bookmark support for Gnus.
12938 (declare-function bookmark-make-record-default
12939                   "bookmark" (&optional no-file no-context posn))
12940 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12941 (declare-function bookmark-default-handler "bookmark" (bmk))
12942 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12943 (defvar bookmark-yank-point)
12944 (defvar bookmark-current-buffer)
12945
12946 (defun gnus-summary-bookmark-make-record ()
12947   "Make a bookmark entry for a Gnus summary buffer."
12948   (let (pos buf)
12949     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12950       (save-restriction              ; FIXME is it necessary to widen?
12951         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12952       (setq buf "art") ; We are recording bookmark from article buffer.
12953       (setq bookmark-yank-point (point))
12954       (setq bookmark-current-buffer (current-buffer))
12955       (gnus-article-show-summary))      ; Go back in summary buffer.
12956     ;; We are now recording bookmark from summary buffer.
12957     (unless buf (setq buf "sum"))
12958     (let* ((subject (elt (gnus-summary-article-header) 1))
12959            (grp     (car gnus-article-current))
12960            (art     (cdr gnus-article-current))
12961            (head    (gnus-summary-article-header art))
12962            (id      (mail-header-id head)))
12963       `(,subject
12964         ,@(condition-case nil
12965               (bookmark-make-record-default 'no-file 'no-context pos)
12966             (wrong-number-of-arguments
12967              (bookmark-make-record-default 'point-only)))
12968         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12969         (group . ,grp) (article . ,art)
12970         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12971
12972 ;;;###autoload
12973 (defun gnus-summary-bookmark-jump (bookmark)
12974   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12975 BOOKMARK is a bookmark name or a bookmark record."
12976   (let ((group    (bookmark-prop-get bookmark 'group))
12977         (article  (bookmark-prop-get bookmark 'article))
12978         (id       (bookmark-prop-get bookmark 'message-id))
12979         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12980     (gnus-fetch-group group (list article))
12981     (gnus-summary-insert-cached-articles)
12982     (gnus-summary-goto-article id nil 'force)
12983     ;; FIXME we have to wait article buffer is ready (only large buffer)
12984     ;; Is there a better solution to know that?
12985     ;; If we don't wait `bookmark-default-handler' will have no chance
12986     ;; to set position. However there is no error, just wrong pos.
12987     (sit-for 1)
12988     (when (string= buf "Gnus-art")
12989       (other-window 1))
12990     (bookmark-default-handler
12991      `(""
12992        (buffer . ,(current-buffer))
12993        . ,(bookmark-get-bookmark-record bookmark)))))
12994
12995 (gnus-summary-make-all-marking-commands)
12996
12997 (gnus-ems-redefine)
12998
12999 (provide 'gnus-sum)
13000
13001 (run-hooks 'gnus-sum-load-hook)
13002
13003 ;; Local Variables:
13004 ;; coding: iso-8859-1
13005 ;; End:
13006
13007 ;;; gnus-sum.el ends here