Convert consecutive FSF copyright years to ranges.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996-2011 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (eval-when-compile
33   (when (featurep 'xemacs)
34     (require 'easy-mmode))) ; for `define-minor-mode'
35
36 (defvar tool-bar-mode)
37 (defvar gnus-tmp-header)
38
39 (require 'gnus)
40 (require 'gnus-group)
41 (require 'gnus-spec)
42 (require 'gnus-range)
43 (require 'gnus-int)
44 (require 'gnus-undo)
45 (require 'gnus-util)
46 (require 'gmm-utils)
47 (require 'mm-decode)
48 (require 'nnoo)
49
50 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
51 (autoload 'gnus-cache-write-active "gnus-cache")
52 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
53 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
54 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
55 (autoload 'mm-uu-dissect "mm-uu")
56 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
57   "Deuglify broken Outlook (Express) articles and redisplay."
58   t)
59 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
60 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
61 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
62 (autoload 'nnir-article-rsv "nnir" nil nil 'macro)
63 (autoload 'nnir-article-group "nnir" nil nil 'macro)
64
65 (defcustom gnus-kill-summary-on-exit t
66   "*If non-nil, kill the summary buffer when you exit from it.
67 If nil, the summary will become a \"*Dead Summary*\" buffer, and
68 it will be killed sometime later."
69   :group 'gnus-summary-exit
70   :type 'boolean)
71
72 (defcustom gnus-summary-next-group-on-exit t
73   "If non-nil, go to the next unread newsgroup on summary exit.
74 See `gnus-group-goto-unread'."
75   :link '(custom-manual "(gnus)Group Maneuvering")
76   :group 'gnus-summary-exit
77   :version "23.1" ;; No Gnus
78   :type 'boolean)
79
80 (defcustom gnus-summary-stop-at-end-of-message nil
81   "If non-nil, don't select the next message when using `SPC'."
82   :link '(custom-manual "(gnus)Group Maneuvering")
83   :group 'gnus-summary-maneuvering
84   :version "24.1"
85   :type 'boolean)
86
87 (defcustom gnus-fetch-old-headers nil
88   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
89 If an unread article in the group refers to an older, already
90 read (or just marked as read) article, the old article will not
91 normally be displayed in the Summary buffer.  If this variable is
92 t, Gnus will attempt to grab the headers to the old articles, and
93 thereby build complete threads.  If it has the value `some', all
94 old headers will be fetched but only enough headers to connect
95 otherwise loose threads will be displayed.  This variable can
96 also be a number.  In that case, no more than that number of old
97 headers will be fetched.  If it has the value `invisible', all
98 old headers will be fetched, but none will be displayed.
99
100 The server has to support NOV for any of this to work.
101
102 This feature can seriously impact performance it ignores all
103 locally cached header entries.  Setting it to t for groups for a
104 server that doesn't expire articles (such as news.gmane.org),
105 leads to very slow summary generation."
106   :group 'gnus-thread
107   :type '(choice (const :tag "off" nil)
108                  (const :tag "on" t)
109                  (const some)
110                  (const invisible)
111                  number
112                  (sexp :menu-tag "other" t)))
113
114 (defcustom gnus-refer-thread-limit 500
115   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
116 If t, fetch all the available old headers."
117   :group 'gnus-thread
118   :type '(choice number
119                  (sexp :menu-tag "other" t)))
120
121 (defcustom gnus-summary-make-false-root 'adopt
122   "*nil means that Gnus won't gather loose threads.
123 If the root of a thread has expired or been read in a previous
124 session, the information necessary to build a complete thread has been
125 lost.  Instead of having many small sub-threads from this original thread
126 scattered all over the summary buffer, Gnus can gather them.
127
128 If non-nil, Gnus will try to gather all loose sub-threads from an
129 original thread into one large thread.
130
131 If this variable is non-nil, it should be one of `none', `adopt',
132 `dummy' or `empty'.
133
134 If this variable is `none', Gnus will not make a false root, but just
135 present the sub-threads after another.
136 If this variable is `dummy', Gnus will create a dummy root that will
137 have all the sub-threads as children.
138 If this variable is `adopt', Gnus will make one of the \"children\"
139 the parent and mark all the step-children as such.
140 If this variable is `empty', the \"children\" are printed with empty
141 subject fields.  (Or rather, they will be printed with a string
142 given by the `gnus-summary-same-subject' variable.)"
143   :group 'gnus-thread
144   :type '(choice (const :tag "off" nil)
145                  (const none)
146                  (const dummy)
147                  (const adopt)
148                  (const empty)))
149
150 (defcustom gnus-summary-make-false-root-always nil
151   "Always make a false dummy root."
152   :version "22.1"
153   :group 'gnus-thread
154   :type 'boolean)
155
156 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
157   "*A regexp to match subjects to be excluded from loose thread gathering.
158 As loose thread gathering is done on subjects only, that means that
159 there can be many false gatherings performed.  By rooting out certain
160 common subjects, gathering might become saner."
161   :group 'gnus-thread
162   :type 'regexp)
163
164 (defcustom gnus-summary-gather-subject-limit nil
165   "*Maximum length of subject comparisons when gathering loose threads.
166 Use nil to compare full subjects.  Setting this variable to a low
167 number will help gather threads that have been corrupted by
168 newsreaders chopping off subject lines, but it might also mean that
169 unrelated articles that have subject that happen to begin with the
170 same few characters will be incorrectly gathered.
171
172 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
173 comparing subjects."
174   :group 'gnus-thread
175   :type '(choice (const :tag "off" nil)
176                  (const fuzzy)
177                  (sexp :menu-tag "on" t)))
178
179 (defcustom gnus-simplify-subject-functions nil
180   "List of functions taking a string argument that simplify subjects.
181 The functions are applied recursively.
182
183 Useful functions to put in this list include:
184 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
185 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
186   :group 'gnus-thread
187   :type '(repeat function))
188
189 (defcustom gnus-simplify-ignored-prefixes nil
190   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
191   :group 'gnus-thread
192   :type '(choice (const :tag "off" nil)
193                  regexp))
194
195 (defcustom gnus-build-sparse-threads nil
196   "*If non-nil, fill in the gaps in threads.
197 If `some', only fill in the gaps that are needed to tie loose threads
198 together.  If `more', fill in all leaf nodes that Gnus can find.  If
199 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
200   :group 'gnus-thread
201   :type '(choice (const :tag "off" nil)
202                  (const some)
203                  (const more)
204                  (sexp :menu-tag "all" t)))
205
206 (defcustom gnus-summary-thread-gathering-function
207   'gnus-gather-threads-by-subject
208   "*Function used for gathering loose threads.
209 There are two pre-defined functions: `gnus-gather-threads-by-subject',
210 which only takes Subjects into consideration; and
211 `gnus-gather-threads-by-references', which compared the References
212 headers of the articles to find matches."
213   :group 'gnus-thread
214   :type '(radio (function-item gnus-gather-threads-by-subject)
215                 (function-item gnus-gather-threads-by-references)
216                 (function :tag "other")))
217
218 (defcustom gnus-summary-same-subject ""
219   "*String indicating that the current article has the same subject as the previous.
220 This variable will only be used if the value of
221 `gnus-summary-make-false-root' is `empty'."
222   :group 'gnus-summary-format
223   :type 'string)
224
225 (defcustom gnus-summary-goto-unread nil
226   "*If t, many commands will go to the next unread article.
227 This applies to marking commands as well as other commands that
228 \"naturally\" select the next article, like, for instance, `SPC' at
229 the end of an article.
230
231 If nil, the marking commands do NOT go to the next unread article
232 \(they go to the next article instead).  If `never', commands that
233 usually go to the next unread article, will go to the next article,
234 whether it is read or not."
235   :version "24.1"
236   :group 'gnus-summary-marks
237   :link '(custom-manual "(gnus)Setting Marks")
238   :type '(choice (const :tag "off" nil)
239                  (const never)
240                  (sexp :menu-tag "on" t)))
241
242 (defcustom gnus-summary-default-score 0
243   "*Default article score level.
244 All scores generated by the score files will be added to this score.
245 If this variable is nil, scoring will be disabled."
246   :group 'gnus-score-default
247   :type '(choice (const :tag "disable")
248                  integer))
249
250 (defcustom gnus-summary-default-high-score 0
251   "*Default threshold for a high scored article.
252 An article will be highlighted as high scored if its score is greater
253 than this score."
254   :version "22.1"
255   :group 'gnus-score-default
256   :type 'integer)
257
258 (defcustom gnus-summary-default-low-score 0
259   "*Default threshold for a low scored article.
260 An article will be highlighted as low scored if its score is smaller
261 than this score."
262   :version "22.1"
263   :group 'gnus-score-default
264   :type 'integer)
265
266 (defcustom gnus-summary-zcore-fuzz 0
267   "*Fuzziness factor for the zcore in the summary buffer.
268 Articles with scores closer than this to `gnus-summary-default-score'
269 will not be marked."
270   :group 'gnus-summary-format
271   :type 'integer)
272
273 (defcustom gnus-simplify-subject-fuzzy-regexp nil
274   "*Strings to be removed when doing fuzzy matches.
275 This can either be a regular expression or list of regular expressions
276 that will be removed from subject strings if fuzzy subject
277 simplification is selected."
278   :group 'gnus-thread
279   :type '(repeat regexp))
280
281 (defcustom gnus-show-threads t
282   "*If non-nil, display threads in summary mode."
283   :group 'gnus-thread
284   :type 'boolean)
285
286 (defcustom gnus-thread-hide-subtree nil
287   "*If non-nil, hide all threads initially.
288 This can be a predicate specifier which says which threads to hide.
289 If threads are hidden, you have to run the command
290 `gnus-summary-show-thread' by hand or select an article."
291   :group 'gnus-thread
292   :type '(radio (sexp :format "Non-nil\n"
293                       :match (lambda (widget value)
294                                (not (or (consp value) (functionp value))))
295                       :value t)
296                 (const nil)
297                 (sexp :tag "Predicate specifier")))
298
299 (defcustom gnus-thread-hide-killed t
300   "*If non-nil, hide killed threads automatically."
301   :group 'gnus-thread
302   :type 'boolean)
303
304 (defcustom gnus-thread-ignore-subject t
305   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
306 If nil, articles that have different subjects from their parents will
307 start separate threads."
308   :group 'gnus-thread
309   :type 'boolean)
310
311 (defcustom gnus-thread-operation-ignore-subject t
312   "*If non-nil, subjects will be ignored when doing thread commands.
313 This affects commands like `gnus-summary-kill-thread' and
314 `gnus-summary-lower-thread'.
315
316 If this variable is nil, articles in the same thread with different
317 subjects will not be included in the operation in question.  If this
318 variable is `fuzzy', only articles that have subjects that are fuzzily
319 equal will be included."
320   :group 'gnus-thread
321   :type '(choice (const :tag "off" nil)
322                  (const fuzzy)
323                  (sexp :tag "on" t)))
324
325 (defcustom gnus-thread-indent-level 4
326   "*Number that says how much each sub-thread should be indented."
327   :group 'gnus-thread
328   :type 'integer)
329
330 (defcustom gnus-auto-extend-newsgroup t
331   "*If non-nil, extend newsgroup forward and backward when requested."
332   :group 'gnus-summary-choose
333   :type 'boolean)
334
335 (defcustom gnus-auto-select-first t
336   "If non-nil, select an article on group entry.
337 An article is selected automatically when entering a group
338 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
339 `gnus-summary-catchup-and-goto-next-group'.
340
341 Which article is selected is controlled by the variable
342 `gnus-auto-select-subject'.
343
344 If you want to prevent automatic selection of articles in some
345 newsgroups, set the variable to nil in `gnus-select-group-hook'."
346   ;; Commands include...
347   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
348   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
349   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
350   :group 'gnus-group-select
351   :type '(choice (const :tag "none" nil)
352                  (sexp :menu-tag "first" t)))
353
354 (defcustom gnus-auto-select-subject 'unseen-or-unread
355   "*Says what subject to place under point when entering a group.
356
357 This variable can either be the symbols `first' (place point on the
358 first subject), `unread' (place point on the subject line of the first
359 unread article), `best' (place point on the subject line of the
360 higest-scored article), `unseen' (place point on the subject line of
361 the first unseen article), `unseen-or-unread' (place point on the subject
362 line of the first unseen article or, if all article have been seen, on the
363 subject line of the first unread article), or a function to be called to
364 place point on some subject line."
365   :version "24.1"
366   :group 'gnus-group-select
367   :type '(choice (const best)
368                  (const unread)
369                  (const first)
370                  (const unseen)
371                  (const unseen-or-unread)))
372
373 (defcustom gnus-auto-select-next t
374   "*If non-nil, offer to go to the next group from the end of the previous.
375 If the value is t and the next newsgroup is empty, Gnus will exit
376 summary mode and go back to group mode.  If the value is neither nil
377 nor t, Gnus will select the following unread newsgroup.  In
378 particular, if the value is the symbol `quietly', the next unread
379 newsgroup will be selected without any confirmation, and if it is
380 `almost-quietly', the next group will be selected without any
381 confirmation if you are located on the last article in the group.
382 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
383 will go to the next group without confirmation."
384   :group 'gnus-summary-maneuvering
385   :type '(choice (const :tag "off" nil)
386                  (const quietly)
387                  (const almost-quietly)
388                  (const slightly-quietly)
389                  (sexp :menu-tag "on" t)))
390
391 (defcustom gnus-auto-select-same nil
392   "*If non-nil, select the next article with the same subject.
393 If there are no more articles with the same subject, go to
394 the first unread article."
395   :group 'gnus-summary-maneuvering
396   :type 'boolean)
397
398 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
399   "What article should be selected after exiting an ephemeral group.
400 Valid values include:
401
402 `next'
403   Select the next article.
404 `next-unread'
405   Select the next unread article.
406 `next-noselect'
407   Move the cursor to the next article.  This is the default.
408 `next-unread-noselect'
409   Move the cursor to the next unread article.
410
411 If it has any other value or there is no next (unread) article, the
412 article selected before entering to the ephemeral group will appear."
413   :version "23.1" ;; No Gnus
414   :group 'gnus-summary-maneuvering
415   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
416                  (const next) (const next-unread)
417                  (const next-noselect) (const next-unread-noselect)
418                  (sexp :tag "other" :value nil)))
419
420 (defcustom gnus-auto-goto-ignores 'unfetched
421   "*Says how to handle unfetched articles when maneuvering.
422
423 This variable can either be the symbols nil (maneuver to any
424 article), `undownloaded' (maneuvering while unplugged ignores articles
425 that have not been fetched), `always-undownloaded' (maneuvering always
426 ignores articles that have not been fetched), `unfetched' (maneuvering
427 ignores articles whose headers have not been fetched).
428
429 NOTE: The list of unfetched articles will always be nil when plugged
430 and, when unplugged, a subset of the undownloaded article list."
431   :version "22.1"
432   :group 'gnus-summary-maneuvering
433   :type '(choice (const :tag "None" nil)
434                  (const :tag "Undownloaded when unplugged" undownloaded)
435                  (const :tag "Undownloaded" always-undownloaded)
436                  (const :tag "Unfetched" unfetched)))
437
438 (defcustom gnus-summary-check-current nil
439   "*If non-nil, consider the current article when moving.
440 The \"unread\" movement commands will stay on the same line if the
441 current article is unread."
442   :group 'gnus-summary-maneuvering
443   :type 'boolean)
444
445 (defcustom gnus-auto-center-summary 2
446   "*If non-nil, always center the current summary buffer.
447 In particular, if `vertical' do only vertical recentering.  If non-nil
448 and non-`vertical', do both horizontal and vertical recentering."
449   :group 'gnus-summary-maneuvering
450   :type '(choice (const :tag "none" nil)
451                  (const vertical)
452                  (integer :tag "height")
453                  (sexp :menu-tag "both" t)))
454
455 (defcustom gnus-auto-center-group t
456   "If non-nil, always center the group buffer."
457   :group 'gnus-summary-maneuvering
458   :type 'boolean)
459
460 (defcustom gnus-show-all-headers nil
461   "*If non-nil, don't hide any headers."
462   :group 'gnus-article-hiding
463   :group 'gnus-article-headers
464   :type 'boolean)
465
466 (defcustom gnus-summary-ignore-duplicates nil
467   "*If non-nil, ignore articles with identical Message-ID headers."
468   :group 'gnus-summary
469   :type 'boolean)
470
471 (defcustom gnus-single-article-buffer nil
472   "*If non-nil, display all articles in the same buffer.
473 If nil, each group will get its own article buffer."
474   :version "24.1"
475   :group 'gnus-article-various
476   :type 'boolean)
477
478 (defcustom gnus-widen-article-window nil
479   "If non-nil, selecting the article buffer will display only the article buffer."
480   :version "24.1"
481   :group 'gnus-article-various
482   :type 'boolean)
483
484 (defcustom gnus-break-pages t
485   "*If non-nil, do page breaking on articles.
486 The page delimiter is specified by the `gnus-page-delimiter'
487 variable."
488   :group 'gnus-article-various
489   :type 'boolean)
490
491 (defcustom gnus-move-split-methods nil
492   "*Variable used to suggest where articles are to be moved to.
493 It uses the same syntax as the `gnus-split-methods' variable.
494 However, whereas `gnus-split-methods' specifies file names as targets,
495 this variable specifies group names."
496   :group 'gnus-summary-mail
497   :type '(repeat (choice (list :value (fun) function)
498                          (cons :value ("" "") regexp (repeat string))
499                          (sexp :value nil))))
500
501 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
502   "Function used to compute default prefix for article move/copy/etc prompts.
503 The function should take one argument, a group name, and return a
504 string with the suggested prefix."
505   :group 'gnus-summary-mail
506   :type 'function)
507
508 ;; FIXME: Although the custom type is `character' for the following variables,
509 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
510
511 (defcustom gnus-unread-mark ?           ;Whitespace
512   "*Mark used for unread articles."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-ticked-mark ?!
517   "*Mark used for ticked articles."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-dormant-mark ??
522   "*Mark used for dormant articles."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-del-mark ?r
527   "*Mark used for del'd articles."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-read-mark ?R
532   "*Mark used for read articles."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-expirable-mark ?E
537   "*Mark used for expirable articles."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-killed-mark ?K
542   "*Mark used for killed articles."
543   :group 'gnus-summary-marks
544   :type 'character)
545
546 (defcustom gnus-spam-mark ?$
547   "*Mark used for spam articles."
548   :version "22.1"
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-kill-file-mark ?X
553   "*Mark used for articles killed by kill files."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-low-score-mark ?Y
558   "*Mark used for articles with a low score."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-catchup-mark ?C
563   "*Mark used for articles that are caught up."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-replied-mark ?A
568   "*Mark used for articles that have been replied to."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-forwarded-mark ?F
573   "*Mark used for articles that have been forwarded."
574   :version "22.1"
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-recent-mark ?N
579   "*Mark used for articles that are recent."
580   :version "22.1"
581   :group 'gnus-summary-marks
582   :type 'character)
583
584 (defcustom gnus-cached-mark ?*
585   "*Mark used for articles that are in the cache."
586   :group 'gnus-summary-marks
587   :type 'character)
588
589 (defcustom gnus-saved-mark ?S
590   "*Mark used for articles that have been saved."
591   :group 'gnus-summary-marks
592   :type 'character)
593
594 (defcustom gnus-unseen-mark ?.
595   "*Mark used for articles that haven't been seen."
596   :version "22.1"
597   :group 'gnus-summary-marks
598   :type 'character)
599
600 (defcustom gnus-no-mark ?               ;Whitespace
601   "*Mark used for articles that have no other secondary mark."
602   :version "22.1"
603   :group 'gnus-summary-marks
604   :type 'character)
605
606 (defcustom gnus-ancient-mark ?O
607   "*Mark used for ancient articles."
608   :group 'gnus-summary-marks
609   :type 'character)
610
611 (defcustom gnus-sparse-mark ?Q
612   "*Mark used for sparsely reffed articles."
613   :group 'gnus-summary-marks
614   :type 'character)
615
616 (defcustom gnus-canceled-mark ?G
617   "*Mark used for canceled articles."
618   :group 'gnus-summary-marks
619   :type 'character)
620
621 (defcustom gnus-duplicate-mark ?M
622   "*Mark used for duplicate articles."
623   :group 'gnus-summary-marks
624   :type 'character)
625
626 (defcustom gnus-undownloaded-mark ?-
627   "*Mark used for articles that weren't downloaded."
628   :version "22.1"
629   :group 'gnus-summary-marks
630   :type 'character)
631
632 (defcustom gnus-downloaded-mark ?+
633   "*Mark used for articles that were downloaded."
634   :group 'gnus-summary-marks
635   :type 'character)
636
637 (defcustom gnus-downloadable-mark ?%
638   "*Mark used for articles that are to be downloaded."
639   :group 'gnus-summary-marks
640   :type 'character)
641
642 (defcustom gnus-unsendable-mark ?=
643   "*Mark used for articles that won't be sent."
644   :group 'gnus-summary-marks
645   :type 'character)
646
647 (defcustom gnus-score-over-mark ?+
648   "*Score mark used for articles with high scores."
649   :group 'gnus-summary-marks
650   :type 'character)
651
652 (defcustom gnus-score-below-mark ?-
653   "*Score mark used for articles with low scores."
654   :group 'gnus-summary-marks
655   :type 'character)
656
657 (defcustom gnus-empty-thread-mark ?     ;Whitespace
658   "*There is no thread under the article."
659   :group 'gnus-summary-marks
660   :type 'character)
661
662 (defcustom gnus-not-empty-thread-mark ?=
663   "*There is a thread under the article."
664   :group 'gnus-summary-marks
665   :type 'character)
666
667 (defcustom gnus-view-pseudo-asynchronously nil
668   "*If non-nil, Gnus will view pseudo-articles asynchronously."
669   :group 'gnus-extract-view
670   :type 'boolean)
671
672 (defcustom gnus-auto-expirable-marks
673   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
674         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
675         gnus-duplicate-mark)
676   "*The list of marks converted into expiration if a group is auto-expirable."
677   :version "24.1"
678   :group 'gnus-summary
679   :type '(repeat character))
680
681 (defcustom gnus-inhibit-user-auto-expire t
682   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
683   :version "21.1"
684   :group 'gnus-summary
685   :type 'boolean)
686
687 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
688   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
689 If nil, the expirable marks will be unchanged except that the marks
690 will be removed when copying or moving articles to a group that has
691 not turned auto-expire on.  If non-nil, articles that have been read
692 will be marked as expirable when being copied or moved to a group in
693 which auto-expire is turned on."
694   :version "23.2"
695   :type 'boolean
696   :group 'gnus-summary-marks)
697
698 (defcustom gnus-view-pseudos nil
699   "*If `automatic', pseudo-articles will be viewed automatically.
700 If `not-confirm', pseudos will be viewed automatically, and the user
701 will not be asked to confirm the command."
702   :group 'gnus-extract-view
703   :type '(choice (const :tag "off" nil)
704                  (const automatic)
705                  (const not-confirm)))
706
707 (defcustom gnus-view-pseudos-separately t
708   "*If non-nil, one pseudo-article will be created for each file to be viewed.
709 If nil, all files that use the same viewing command will be given as a
710 list of parameters to that command."
711   :group 'gnus-extract-view
712   :type 'boolean)
713
714 (defcustom gnus-insert-pseudo-articles t
715   "*If non-nil, insert pseudo-articles when decoding articles."
716   :group 'gnus-extract-view
717   :type 'boolean)
718
719 (defcustom gnus-summary-dummy-line-format
720   "   %(:                             :%) %S\n"
721   "*The format specification for the dummy roots in the summary buffer.
722 It works along the same lines as a normal formatting string,
723 with some simple extensions.
724
725 %S  The subject
726
727 General format specifiers can also be used.
728 See `(gnus)Formatting Variables'."
729   :link '(custom-manual "(gnus)Formatting Variables")
730   :group 'gnus-threading
731   :type 'string)
732
733 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
734   "*The format specification for the summary mode line.
735 It works along the same lines as a normal formatting string,
736 with some simple extensions:
737
738 %G  Group name
739 %p  Unprefixed group name
740 %A  Current article number
741 %z  Current article score
742 %V  Gnus version
743 %U  Number of unread articles in the group
744 %e  Number of unselected articles in the group
745 %Z  A string with unread/unselected article counts
746 %g  Shortish group name
747 %S  Subject of the current article
748 %u  User-defined spec
749 %s  Current score file name
750 %d  Number of dormant articles
751 %r  Number of articles that have been marked as read in this session
752 %E  Number of articles expunged by the score files"
753   :group 'gnus-summary-format
754   :type 'string)
755
756 (defcustom gnus-list-identifiers nil
757   "Regexp that matches list identifiers to be removed from subject.
758 This can also be a list of regexps."
759   :version "21.1"
760   :group 'gnus-summary-format
761   :group 'gnus-article-hiding
762   :type '(choice (const :tag "none" nil)
763                  (regexp :value ".*")
764                  (repeat :value (".*") regexp)))
765
766 (defcustom gnus-summary-mark-below 0
767   "*Mark all articles with a score below this variable as read.
768 This variable is local to each summary buffer and usually set by the
769 score file."
770   :group 'gnus-score-default
771   :type 'integer)
772
773 (defun gnus-widget-reversible-match (widget value)
774   "Ignoring WIDGET, convert VALUE to internal form.
775 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
776   ;; (debug value)
777   (or (symbolp value)
778       (and (listp value)
779            (eq (length value) 2)
780            (eq (nth 0 value) 'not)
781            (symbolp (nth 1 value)))))
782
783 (defun gnus-widget-reversible-to-internal (widget value)
784   "Ignoring WIDGET, convert VALUE to internal form.
785 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
786 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
787   ;; (debug value)
788   (if (atom value)
789       (list value nil)
790     (list (nth 1 value) t)))
791
792 (defun gnus-widget-reversible-to-external (widget value)
793   "Ignoring WIDGET, convert VALUE to external form.
794 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
795 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
796   ;; (debug value)
797   (if (nth 1 value)
798       (list 'not (nth 0 value))
799     (nth 0 value)))
800
801 (define-widget 'gnus-widget-reversible 'group
802   "A `group' that convert values."
803   :match 'gnus-widget-reversible-match
804   :value-to-internal 'gnus-widget-reversible-to-internal
805   :value-to-external 'gnus-widget-reversible-to-external)
806
807 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
808   "*List of functions used for sorting articles in the summary buffer.
809
810 Each function takes two articles and returns non-nil if the first
811 article should be sorted before the other.  If you use more than one
812 function, the primary sort function should be the last.  You should
813 probably always include `gnus-article-sort-by-number' in the list of
814 sorting functions -- preferably first.  Also note that sorting by date
815 is often much slower than sorting by number, and the sorting order is
816 very similar.  (Sorting by date means sorting by the time the message
817 was sent, sorting by number means sorting by arrival time.)
818
819 Each item can also be a list `(not F)' where F is a function;
820 this reverses the sort order.
821
822 Ready-made functions include `gnus-article-sort-by-number',
823 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
824 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
825 and `gnus-article-sort-by-score'.
826
827 When threading is turned on, the variable `gnus-thread-sort-functions'
828 controls how articles are sorted."
829   :group 'gnus-summary-sort
830   :type '(repeat (gnus-widget-reversible
831                   (choice (function-item gnus-article-sort-by-number)
832                           (function-item gnus-article-sort-by-author)
833                           (function-item gnus-article-sort-by-subject)
834                           (function-item gnus-article-sort-by-date)
835                           (function-item gnus-article-sort-by-score)
836                           (function-item gnus-article-sort-by-random)
837                           (function :tag "other"))
838                   (boolean :tag "Reverse order"))))
839
840
841 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
842   "*List of functions used for sorting threads in the summary buffer.
843 By default, threads are sorted by article number.
844
845 Each function takes two threads and returns non-nil if the first
846 thread should be sorted before the other.  If you use more than one
847 function, the primary sort function should be the last.  You should
848 probably always include `gnus-thread-sort-by-number' in the list of
849 sorting functions -- preferably first.  Also note that sorting by date
850 is often much slower than sorting by number, and the sorting order is
851 very similar.  (Sorting by date means sorting by the time the message
852 was sent, sorting by number means sorting by arrival time.)
853
854 Each list item can also be a list `(not F)' where F is a
855 function; this specifies reversed sort order.
856
857 Ready-made functions include `gnus-thread-sort-by-number',
858 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
859 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
860 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
861 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
862 and `gnus-thread-sort-by-total-score' (see
863 `gnus-thread-score-function').
864
865 When threading is turned off, the variable
866 `gnus-article-sort-functions' controls how articles are sorted."
867   :group 'gnus-summary-sort
868   :type '(repeat
869           (gnus-widget-reversible
870            (choice (function-item gnus-thread-sort-by-number)
871                    (function-item gnus-thread-sort-by-author)
872                    (function-item gnus-thread-sort-by-recipient)
873                    (function-item gnus-thread-sort-by-subject)
874                    (function-item gnus-thread-sort-by-date)
875                    (function-item gnus-thread-sort-by-score)
876                    (function-item gnus-thread-sort-by-most-recent-number)
877                    (function-item gnus-thread-sort-by-most-recent-date)
878                    (function-item gnus-thread-sort-by-random)
879                    (function-item gnus-thread-sort-by-total-score)
880                    (function :tag "other"))
881            (boolean :tag "Reverse order"))))
882
883 (defcustom gnus-thread-score-function '+
884   "*Function used for calculating the total score of a thread.
885
886 The function is called with the scores of the article and each
887 subthread and should then return the score of the thread.
888
889 Some functions you can use are `+', `max', or `min'."
890   :group 'gnus-summary-sort
891   :type 'function)
892
893 (defcustom gnus-summary-expunge-below nil
894   "All articles that have a score less than this variable will be expunged.
895 This variable is local to the summary buffers."
896   :group 'gnus-score-default
897   :type '(choice (const :tag "off" nil)
898                  integer))
899
900 (defcustom gnus-thread-expunge-below nil
901   "All threads that have a total score less than this variable will be expunged.
902 See `gnus-thread-score-function' for en explanation of what a
903 \"thread score\" is.
904
905 This variable is local to the summary buffers."
906   :group 'gnus-threading
907   :group 'gnus-score-default
908   :type '(choice (const :tag "off" nil)
909                  integer))
910
911 (defcustom gnus-summary-mode-hook nil
912   "*A hook for Gnus summary mode.
913 This hook is run before any variables are set in the summary buffer."
914   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
915   :group 'gnus-summary-various
916   :type 'hook)
917
918 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
919 (when (featurep 'xemacs)
920   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
921   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
922   (add-hook 'gnus-summary-mode-hook
923             'gnus-xmas-switch-horizontal-scrollbar-off))
924
925 (defcustom gnus-summary-menu-hook nil
926   "*Hook run after the creation of the summary mode menu."
927   :group 'gnus-summary-visual
928   :type 'hook)
929
930 (defcustom gnus-summary-exit-hook nil
931   "*A hook called on exit from the summary buffer.
932 It will be called with point in the group buffer."
933   :group 'gnus-summary-exit
934   :type 'hook)
935
936 (defcustom gnus-summary-prepare-hook nil
937   "*A hook called after the summary buffer has been generated.
938 If you want to modify the summary buffer, you can use this hook."
939   :group 'gnus-summary-various
940   :type 'hook)
941
942 (defcustom gnus-summary-prepared-hook nil
943   "*A hook called as the last thing after the summary buffer has been generated."
944   :group 'gnus-summary-various
945   :type 'hook)
946
947 (defcustom gnus-summary-generate-hook nil
948   "*A hook run just before generating the summary buffer.
949 This hook is commonly used to customize threading variables and the
950 like."
951   :group 'gnus-summary-various
952   :type 'hook)
953
954 (defcustom gnus-select-group-hook nil
955   "*A hook called when a newsgroup is selected.
956
957 If you'd like to simplify subjects like the
958 `gnus-summary-next-same-subject' command does, you can use the
959 following hook:
960
961  (add-hook gnus-select-group-hook
962            (lambda ()
963              (mapcar (lambda (header)
964                        (mail-header-set-subject
965                         header
966                         (gnus-simplify-subject
967                          (mail-header-subject header) 're-only)))
968                      gnus-newsgroup-headers)))"
969   :group 'gnus-group-select
970   :type 'hook)
971
972 (defcustom gnus-select-article-hook nil
973   "*A hook called when an article is selected."
974   :group 'gnus-summary-choose
975   :options '(gnus-agent-fetch-selected-article)
976   :type 'hook)
977
978 (defcustom gnus-visual-mark-article-hook
979   (list 'gnus-highlight-selected-summary)
980   "*Hook run after selecting an article in the summary buffer.
981 It is meant to be used for highlighting the article in some way.  It
982 is not run if `gnus-visual' is nil."
983   :group 'gnus-summary-visual
984   :type 'hook)
985
986 (defcustom gnus-parse-headers-hook nil
987   "*A hook called before parsing the headers."
988   :group 'gnus-various
989   :type 'hook)
990
991 (defcustom gnus-exit-group-hook nil
992   "*A hook called when exiting summary mode.
993 This hook is not called from the non-updating exit commands like `Q'."
994   :group 'gnus-various
995   :type 'hook)
996
997 (defcustom gnus-summary-update-hook nil
998   "*A hook called when a summary line is changed.
999 The hook will not be called if `gnus-visual' is nil.
1000
1001 The default function `gnus-summary-highlight-line' will
1002 highlight the line according to the `gnus-summary-highlight'
1003 variable."
1004   :group 'gnus-summary-visual
1005   :type 'hook)
1006
1007 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1008   "*A hook called when an article is selected for the first time.
1009 The hook is intended to mark an article as read (or unread)
1010 automatically when it is selected."
1011   :group 'gnus-summary-choose
1012   :type 'hook)
1013
1014 (defcustom gnus-group-no-more-groups-hook nil
1015   "*A hook run when returning to group mode having no more (unread) groups."
1016   :group 'gnus-group-select
1017   :type 'hook)
1018
1019 (defcustom gnus-ps-print-hook nil
1020   "*A hook run before ps-printing something from Gnus."
1021   :group 'gnus-summary
1022   :type 'hook)
1023
1024 (defcustom gnus-summary-article-move-hook nil
1025   "*A hook called after an article is moved, copied, respooled, or crossposted."
1026   :version "22.1"
1027   :group 'gnus-summary
1028   :type 'hook)
1029
1030 (defcustom gnus-summary-article-delete-hook nil
1031   "*A hook called after an article is deleted."
1032   :version "22.1"
1033   :group 'gnus-summary
1034   :type 'hook)
1035
1036 (defcustom gnus-summary-article-expire-hook nil
1037   "*A hook called after an article is expired."
1038   :version "22.1"
1039   :group 'gnus-summary
1040   :type 'hook)
1041
1042 (defcustom gnus-summary-display-arrow
1043   (and (fboundp 'display-graphic-p)
1044        (display-graphic-p))
1045   "*If non-nil, display an arrow highlighting the current article."
1046   :version "22.1"
1047   :group 'gnus-summary
1048   :type 'boolean)
1049
1050 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1051   "Face used for highlighting the current article in the summary buffer."
1052   :group 'gnus-summary-visual
1053   :type 'face)
1054
1055 (defvar gnus-tmp-downloaded nil)
1056
1057 (defcustom gnus-summary-highlight
1058   '(((eq mark gnus-canceled-mark)
1059      . gnus-summary-cancelled)
1060     ((and uncached (> score default-high))
1061      . gnus-summary-high-undownloaded)
1062     ((and uncached (< score default-low))
1063      . gnus-summary-low-undownloaded)
1064     (uncached
1065      . gnus-summary-normal-undownloaded)
1066     ((and (> score default-high)
1067           (or (eq mark gnus-dormant-mark)
1068               (eq mark gnus-ticked-mark)))
1069      . gnus-summary-high-ticked)
1070     ((and (< score default-low)
1071           (or (eq mark gnus-dormant-mark)
1072               (eq mark gnus-ticked-mark)))
1073      . gnus-summary-low-ticked)
1074     ((or (eq mark gnus-dormant-mark)
1075          (eq mark gnus-ticked-mark))
1076      . gnus-summary-normal-ticked)
1077     ((and (> score default-high) (eq mark gnus-ancient-mark))
1078      . gnus-summary-high-ancient)
1079     ((and (< score default-low) (eq mark gnus-ancient-mark))
1080      . gnus-summary-low-ancient)
1081     ((eq mark gnus-ancient-mark)
1082      . gnus-summary-normal-ancient)
1083     ((and (> score default-high) (eq mark gnus-unread-mark))
1084      . gnus-summary-high-unread)
1085     ((and (< score default-low) (eq mark gnus-unread-mark))
1086      . gnus-summary-low-unread)
1087     ((eq mark gnus-unread-mark)
1088      . gnus-summary-normal-unread)
1089     ((> score default-high)
1090      . gnus-summary-high-read)
1091     ((< score default-low)
1092      . gnus-summary-low-read)
1093     (t
1094      . gnus-summary-normal-read))
1095   "*Controls the highlighting of summary buffer lines.
1096
1097 A list of (FORM . FACE) pairs.  When deciding how a particular
1098 summary line should be displayed, each form is evaluated.  The content
1099 of the face field after the first true form is used.  You can change
1100 how those summary lines are displayed, by editing the face field.
1101
1102 You can use the following variables in the FORM field.
1103
1104 score:        The article's score.
1105 default:      The default article score.
1106 default-high: The default score for high scored articles.
1107 default-low:  The default score for low scored articles.
1108 below:        The score below which articles are automatically marked as read.
1109 mark:         The article's mark.
1110 uncached:     Non-nil if the article is uncached."
1111   :group 'gnus-summary-visual
1112   :type '(repeat (cons (sexp :tag "Form" nil)
1113                        face)))
1114 (put 'gnus-summary-highlight 'risky-local-variable t)
1115
1116 (defcustom gnus-alter-header-function nil
1117   "Function called to allow alteration of article header structures.
1118 The function is called with one parameter, the article header vector,
1119 which it may alter in any way."
1120   :type '(choice (const :tag "None" nil)
1121                  function)
1122   :group 'gnus-summary)
1123
1124 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1125   "Function used to decode a string with encoded words.")
1126
1127 (defvar gnus-decode-encoded-address-function
1128   'mail-decode-encoded-address-string
1129   "Function used to decode addresses with encoded words.")
1130
1131 (defcustom gnus-extra-headers '(To Newsgroups)
1132   "*Extra headers to parse."
1133   :version "21.1"
1134   :group 'gnus-summary
1135   :type '(repeat symbol))
1136
1137 (defcustom gnus-ignored-from-addresses
1138   (and user-mail-address
1139        (not (string= user-mail-address ""))
1140        (regexp-quote user-mail-address))
1141   "*From headers that may be suppressed in favor of To headers.
1142 This can be a regexp or a list of regexps."
1143   :version "21.1"
1144   :group 'gnus-summary
1145   :type '(choice regexp
1146                  (repeat :tag "Regexp List" regexp)))
1147
1148 (defsubst gnus-ignored-from-addresses ()
1149   (gmm-regexp-concat gnus-ignored-from-addresses))
1150
1151 (defcustom gnus-summary-to-prefix "-> "
1152   "*String prefixed to the To field in the summary line when
1153 using `gnus-ignored-from-addresses'."
1154   :version "22.1"
1155   :group 'gnus-summary
1156   :type 'string)
1157
1158 (defcustom gnus-summary-newsgroup-prefix "=> "
1159   "*String prefixed to the Newsgroup field in the summary
1160 line when using `gnus-ignored-from-addresses'."
1161   :version "22.1"
1162   :group 'gnus-summary
1163   :type 'string)
1164
1165 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1166   "List of charsets that should be ignored.
1167 When these charsets are used in the \"charset\" parameter, the
1168 default charset will be used instead."
1169   :version "21.1"
1170   :type '(repeat symbol)
1171   :group 'gnus-charset)
1172
1173 (defcustom gnus-newsgroup-maximum-articles nil
1174   "The maximum number of articles a newsgroup.
1175 If this is a number, old articles in a newsgroup exceeding this number
1176 are silently ignored.  If it is nil, no article is ignored.  Note that
1177 setting this variable to a number might prevent you from reading very
1178 old articles."
1179   :group 'gnus-group-select
1180   :version "22.2"
1181   :type '(choice (const :tag "No limit" nil)
1182                  integer))
1183
1184 (gnus-define-group-parameter
1185  ignored-charsets
1186  :type list
1187  :function-document
1188  "Return the ignored charsets of GROUP."
1189  :variable gnus-group-ignored-charsets-alist
1190  :variable-default
1191  '(("alt\\.chinese\\.text" iso-8859-1))
1192  :variable-document
1193  "Alist of regexps (to match group names) and charsets that should be ignored.
1194 When these charsets are used in the \"charset\" parameter, the
1195 default charset will be used instead."
1196  :variable-group gnus-charset
1197  :variable-type '(repeat (cons (regexp :tag "Group")
1198                                (repeat symbol)))
1199  :parameter-type '(choice :tag "Ignored charsets"
1200                           :value nil
1201                           (repeat (symbol)))
1202  :parameter-document       "\
1203 List of charsets that should be ignored.
1204
1205 When these charsets are used in the \"charset\" parameter, the
1206 default charset will be used instead.")
1207
1208 (defcustom gnus-group-highlight-words-alist nil
1209   "Alist of group regexps and highlight regexps.
1210 This variable uses the same syntax as `gnus-emphasis-alist'."
1211   :version "21.1"
1212   :type '(repeat (cons (regexp :tag "Group")
1213                        (repeat (list (regexp :tag "Highlight regexp")
1214                                      (number :tag "Group for entire word" 0)
1215                                      (number :tag "Group for displayed part" 0)
1216                                      (symbol :tag "Face"
1217                                              gnus-emphasis-highlight-words)))))
1218   :group 'gnus-summary-visual)
1219
1220 (defcustom gnus-summary-show-article-charset-alist
1221   nil
1222   "Alist of number and charset.
1223 The article will be shown with the charset corresponding to the
1224 numbered argument.
1225 For example: ((1 . cn-gb-2312) (2 . big5))."
1226   :version "21.1"
1227   :type '(repeat (cons (number :tag "Argument" 1)
1228                        (symbol :tag "Charset")))
1229   :group 'gnus-charset)
1230
1231 (defcustom gnus-preserve-marks t
1232   "Whether marks are preserved when moving, copying and respooling messages."
1233   :version "21.1"
1234   :type 'boolean
1235   :group 'gnus-summary-marks)
1236
1237 (defcustom gnus-propagate-marks t
1238   "If non-nil, do not propagate marks to the backends."
1239   :version "23.1" ;; No Gnus
1240   :type 'boolean
1241   :group 'gnus-summary-marks)
1242
1243 (defcustom gnus-alter-articles-to-read-function nil
1244   "Function to be called to alter the list of articles to be selected."
1245   :type '(choice (const nil) function)
1246   :group 'gnus-summary)
1247
1248 (defcustom gnus-orphan-score nil
1249   "*All orphans get this score added.  Set in the score file."
1250   :group 'gnus-score-default
1251   :type '(choice (const nil)
1252                  integer))
1253
1254 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1255   "*A regexp to match MIME parts when saving multiple parts of a
1256 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1257 This regexp will be used by default when prompting the user for which
1258 type of files to save."
1259   :group 'gnus-summary
1260   :type 'regexp)
1261
1262 (defcustom gnus-read-all-available-headers nil
1263   "Whether Gnus should parse all headers made available to it.
1264 This is mostly relevant for slow back ends where the user may
1265 wish to widen the summary buffer to include all headers
1266 that were fetched."
1267   :version "22.1"
1268   :group 'gnus-summary
1269   :type '(choice boolean regexp))
1270
1271 (defcustom gnus-summary-pipe-output-default-command nil
1272   "Command (and optional arguments) used to pipe article to subprocess.
1273 This will be used as the default command if it is non-nil.  The value
1274 will be updated if you modify it when executing the command
1275 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1276   :version "23.1" ;; No Gnus
1277   :group 'gnus-summary
1278   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1279
1280 (defcustom gnus-summary-muttprint-program "muttprint"
1281   "Command (and optional arguments) used to run Muttprint.
1282 The value will be updated if you modify it when executing the command
1283 `gnus-summary-muttprint'."
1284   :version "22.1"
1285   :group 'gnus-summary
1286   :type 'string)
1287
1288 (defcustom gnus-article-loose-mime t
1289   "If non-nil, don't require MIME-Version header.
1290 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1291 supply the MIME-Version header or deliberately strip it from the mail.
1292 If non-nil (the default), Gnus will treat some articles as MIME
1293 even if the MIME-Version header is missing."
1294   :version "22.1"
1295   :type 'boolean
1296   :group 'gnus-article-mime)
1297
1298 (defcustom gnus-article-emulate-mime t
1299   "If non-nil, use MIME emulation for uuencode and the like.
1300 This means that Gnus will search message bodies for text that look
1301 like uuencoded bits, yEncoded bits, and so on, and present that using
1302 the normal Gnus MIME machinery."
1303   :version "22.1"
1304   :type 'boolean
1305   :group 'gnus-article-mime)
1306
1307 ;;; Internal variables
1308
1309 (defvar gnus-summary-display-cache nil)
1310 (defvar gnus-article-mime-handles nil)
1311 (defvar gnus-article-decoded-p nil)
1312 (defvar gnus-article-charset nil)
1313 (defvar gnus-article-ignored-charsets nil)
1314 (defvar gnus-scores-exclude-files nil)
1315 (defvar gnus-page-broken nil)
1316
1317 (defvar gnus-original-article nil)
1318 (defvar gnus-article-internal-prepare-hook nil)
1319 (defvar gnus-newsgroup-process-stack nil)
1320
1321 (defvar gnus-thread-indent-array nil)
1322 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1323 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1324   "Function called to sort the articles within a thread after it has been gathered together.")
1325
1326 (defvar gnus-summary-save-parts-type-history nil)
1327 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1328
1329 ;; Avoid highlighting in kill files.
1330 (defvar gnus-summary-inhibit-highlight nil)
1331 (defvar gnus-newsgroup-selected-overlay nil)
1332 (defvar gnus-inhibit-limiting nil)
1333 (defvar gnus-newsgroup-adaptive-score-file nil)
1334 (defvar gnus-current-score-file nil)
1335 (defvar gnus-current-move-group nil)
1336 (defvar gnus-current-copy-group nil)
1337 (defvar gnus-current-crosspost-group nil)
1338 (defvar gnus-newsgroup-display nil)
1339
1340 (defvar gnus-newsgroup-dependencies nil)
1341 (defvar gnus-newsgroup-adaptive nil)
1342 (defvar gnus-summary-display-article-function nil)
1343 (defvar gnus-summary-highlight-line-function nil
1344   "Function called after highlighting a summary line.")
1345
1346 (defvar gnus-summary-line-format-alist
1347   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1348     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1349     (?s gnus-tmp-subject-or-nil ?s)
1350     (?n gnus-tmp-name ?s)
1351     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1352         ?s)
1353     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1354             gnus-tmp-from) ?s)
1355     (?F gnus-tmp-from ?s)
1356     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1357     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1358     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1359     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1360     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1361     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1362     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1363     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1364     (?L gnus-tmp-lines ?s)
1365     (?Z (or ,(gnus-macroexpand-all
1366               '(nnir-article-rsv (mail-header-number gnus-tmp-header)))
1367             0) ?d)
1368     (?G (or ,(gnus-macroexpand-all
1369               '(nnir-article-group (mail-header-number gnus-tmp-header)))
1370             "") ?s)
1371     (?g (or ,(gnus-macroexpand-all
1372               '(gnus-group-short-name
1373                 (nnir-article-group (mail-header-number gnus-tmp-header))))
1374             "") ?s)
1375     (?O gnus-tmp-downloaded ?c)
1376     (?I gnus-tmp-indentation ?s)
1377     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1378     (?R gnus-tmp-replied ?c)
1379     (?\[ gnus-tmp-opening-bracket ?c)
1380     (?\] gnus-tmp-closing-bracket ?c)
1381     (?\> (make-string gnus-tmp-level ? ) ?s)
1382     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1383     (?i gnus-tmp-score ?d)
1384     (?z gnus-tmp-score-char ?c)
1385     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1386     (?U gnus-tmp-unread ?c)
1387     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1388         ?s)
1389     (?t (gnus-summary-number-of-articles-in-thread
1390          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1391         ?d)
1392     (?e (gnus-summary-number-of-articles-in-thread
1393          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1394         ?c)
1395     (?u gnus-tmp-user-defined ?s)
1396     (?P (gnus-pick-line-number) ?d)
1397     (?B gnus-tmp-thread-tree-header-string ?s)
1398     (user-date (gnus-user-date
1399                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1400   "An alist of format specifications that can appear in summary lines.
1401 These are paired with what variables they correspond with, along with
1402 the type of the variable (string, integer, character, etc).")
1403
1404 (defvar gnus-summary-dummy-line-format-alist
1405   `((?S gnus-tmp-subject ?s)
1406     (?N gnus-tmp-number ?d)
1407     (?u gnus-tmp-user-defined ?s)))
1408
1409 (defvar gnus-summary-mode-line-format-alist
1410   `((?G gnus-tmp-group-name ?s)
1411     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1412     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1413     (?A gnus-tmp-article-number ?d)
1414     (?Z gnus-tmp-unread-and-unselected ?s)
1415     (?V gnus-version ?s)
1416     (?U gnus-tmp-unread-and-unticked ?d)
1417     (?S gnus-tmp-subject ?s)
1418     (?e gnus-tmp-unselected ?d)
1419     (?u gnus-tmp-user-defined ?s)
1420     (?d (length gnus-newsgroup-dormant) ?d)
1421     (?t (length gnus-newsgroup-marked) ?d)
1422     (?h (length gnus-newsgroup-spam-marked) ?d)
1423     (?r (length gnus-newsgroup-reads) ?d)
1424     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1425     (?E gnus-newsgroup-expunged-tally ?d)
1426     (?s (gnus-current-score-file-nondirectory) ?s)))
1427
1428 ;; This is here rather than in gnus-art for compilation reasons.
1429 (defvar gnus-article-mode-line-format-alist
1430   (nconc '((?w (gnus-article-wash-status) ?s)
1431            (?m (gnus-article-mime-part-status) ?s))
1432          gnus-summary-mode-line-format-alist))
1433
1434 (defvar gnus-last-search-regexp nil
1435   "Default regexp for article search command.")
1436
1437 (defvar gnus-last-shell-command nil
1438   "Default shell command on article.")
1439
1440 (defvar gnus-newsgroup-agentized nil
1441   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1442 (defvar gnus-newsgroup-begin nil)
1443 (defvar gnus-newsgroup-end nil)
1444 (defvar gnus-newsgroup-last-rmail nil)
1445 (defvar gnus-newsgroup-last-mail nil)
1446 (defvar gnus-newsgroup-last-folder nil)
1447 (defvar gnus-newsgroup-last-file nil)
1448 (defvar gnus-newsgroup-last-directory nil)
1449 (defvar gnus-newsgroup-auto-expire nil)
1450 (defvar gnus-newsgroup-active nil)
1451 (defvar gnus-newsgroup-highest nil)
1452
1453 (defvar gnus-newsgroup-data nil)
1454 (defvar gnus-newsgroup-data-reverse nil)
1455 (defvar gnus-newsgroup-limit nil)
1456 (defvar gnus-newsgroup-limits nil)
1457 (defvar gnus-summary-use-undownloaded-faces nil)
1458
1459 (defvar gnus-newsgroup-unreads nil
1460   "Sorted list of unread articles in the current newsgroup.")
1461
1462 (defvar gnus-newsgroup-unselected nil
1463   "Sorted list of unselected unread articles in the current newsgroup.")
1464
1465 (defvar gnus-newsgroup-reads nil
1466   "Alist of read articles and article marks in the current newsgroup.")
1467
1468 (defvar gnus-newsgroup-expunged-tally nil)
1469
1470 (defvar gnus-newsgroup-marked nil
1471   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1472
1473 (defvar gnus-newsgroup-spam-marked nil
1474   "List of ranges of articles that have been marked as spam.")
1475
1476 (defvar gnus-newsgroup-killed nil
1477   "List of ranges of articles that have been through the scoring process.")
1478
1479 (defvar gnus-newsgroup-cached nil
1480   "Sorted list of articles that come from the article cache.")
1481
1482 (defvar gnus-newsgroup-saved nil
1483   "List of articles that have been saved.")
1484
1485 (defvar gnus-newsgroup-kill-headers nil)
1486
1487 (defvar gnus-newsgroup-replied nil
1488   "List of articles that have been replied to in the current newsgroup.")
1489
1490 (defvar gnus-newsgroup-forwarded nil
1491   "List of articles that have been forwarded in the current newsgroup.")
1492
1493 (defvar gnus-newsgroup-recent nil
1494   "List of articles that have are recent in the current newsgroup.")
1495
1496 (defvar gnus-newsgroup-expirable nil
1497   "Sorted list of articles in the current newsgroup that can be expired.")
1498
1499 (defvar gnus-newsgroup-processable nil
1500   "List of articles in the current newsgroup that can be processed.")
1501
1502 (defvar gnus-newsgroup-downloadable nil
1503   "Sorted list of articles in the current newsgroup that can be processed.")
1504
1505 (defvar gnus-newsgroup-unfetched nil
1506   "Sorted list of articles in the current newsgroup whose headers have
1507 not been fetched into the agent.
1508
1509 This list will always be a subset of gnus-newsgroup-undownloaded.")
1510
1511 (defvar gnus-newsgroup-undownloaded nil
1512   "List of articles in the current newsgroup that haven't been downloaded.")
1513
1514 (defvar gnus-newsgroup-unsendable nil
1515   "List of articles in the current newsgroup that won't be sent.")
1516
1517 (defvar gnus-newsgroup-bookmarks nil
1518   "List of articles in the current newsgroup that have bookmarks.")
1519
1520 (defvar gnus-newsgroup-dormant nil
1521   "Sorted list of dormant articles in the current newsgroup.")
1522
1523 (defvar gnus-newsgroup-unseen nil
1524   "List of unseen articles in the current newsgroup.")
1525
1526 (defvar gnus-newsgroup-seen nil
1527   "Range of seen articles in the current newsgroup.")
1528
1529 (defvar gnus-newsgroup-articles nil
1530   "List of articles in the current newsgroup.")
1531
1532 (defvar gnus-newsgroup-scored nil
1533   "List of scored articles in the current newsgroup.")
1534
1535 (defvar gnus-newsgroup-headers nil
1536   "List of article headers in the current newsgroup.")
1537
1538 (defvar gnus-newsgroup-threads nil)
1539
1540 (defvar gnus-newsgroup-prepared nil
1541   "Whether the current group has been prepared properly.")
1542
1543 (defvar gnus-newsgroup-ancient nil
1544   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1545
1546 (defvar gnus-newsgroup-sparse nil)
1547
1548 (defvar gnus-current-article nil)
1549 (defvar gnus-article-current nil)
1550 (defvar gnus-current-headers nil)
1551 (defvar gnus-have-all-headers nil)
1552 (defvar gnus-last-article nil)
1553 (defvar gnus-newsgroup-history nil)
1554 (defvar gnus-newsgroup-charset nil)
1555 (defvar gnus-newsgroup-ephemeral-charset nil)
1556 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1557
1558 (defvar gnus-article-before-search nil)
1559
1560 (defvar gnus-summary-local-variables
1561   '(gnus-newsgroup-name
1562
1563     ;; Marks lists
1564     gnus-newsgroup-unreads
1565     gnus-newsgroup-unselected
1566     gnus-newsgroup-marked
1567     gnus-newsgroup-spam-marked
1568     gnus-newsgroup-reads
1569     gnus-newsgroup-saved
1570     gnus-newsgroup-replied
1571     gnus-newsgroup-forwarded
1572     gnus-newsgroup-recent
1573     gnus-newsgroup-expirable
1574     gnus-newsgroup-killed
1575     gnus-newsgroup-unseen
1576     gnus-newsgroup-seen
1577     gnus-newsgroup-cached
1578     gnus-newsgroup-downloadable
1579     gnus-newsgroup-undownloaded
1580     gnus-newsgroup-unsendable
1581
1582     gnus-newsgroup-begin gnus-newsgroup-end
1583     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1584     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1585     gnus-newsgroup-last-directory
1586     gnus-newsgroup-auto-expire
1587     gnus-newsgroup-processable
1588     gnus-newsgroup-unfetched
1589     gnus-newsgroup-articles
1590     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1591     gnus-newsgroup-headers gnus-newsgroup-threads
1592     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1593     gnus-current-article gnus-current-headers gnus-have-all-headers
1594     gnus-last-article gnus-article-internal-prepare-hook
1595     (gnus-summary-article-delete-hook . global)
1596     (gnus-summary-article-move-hook . global)
1597     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1598     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1599     gnus-thread-expunge-below
1600     gnus-score-alist gnus-current-score-file
1601     (gnus-summary-expunge-below . global)
1602     (gnus-summary-mark-below . global)
1603     (gnus-orphan-score . global)
1604     gnus-newsgroup-active gnus-scores-exclude-files
1605     gnus-newsgroup-highest
1606     gnus-newsgroup-history gnus-newsgroup-ancient
1607     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1608     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1609     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1610     (gnus-newsgroup-expunged-tally . 0)
1611     gnus-cache-removable-articles
1612     gnus-newsgroup-data gnus-newsgroup-data-reverse
1613     gnus-newsgroup-limit gnus-newsgroup-limits
1614     gnus-newsgroup-charset gnus-newsgroup-display
1615     gnus-summary-use-undownloaded-faces)
1616   "Variables that are buffer-local to the summary buffers.")
1617
1618 (defvar gnus-newsgroup-variables nil
1619   "A list of variables that have separate values in different newsgroups.
1620 A list of newsgroup (summary buffer) local variables, or cons of
1621 variables and their default expressions to be evalled (when the default
1622 values are not nil), that should be made global while the summary buffer
1623 is active.
1624
1625 Note: The default expressions will be evaluated (using function `eval')
1626 before assignment to the local variable rather than just assigned to it.
1627 If the default expression is the symbol `global', that symbol will not
1628 be evaluated but the global value of the local variable will be used
1629 instead.
1630
1631 These variables can be used to set variables in the group parameters
1632 while still allowing them to affect operations done in other buffers.
1633 For example:
1634
1635 \(setq gnus-newsgroup-variables
1636      '(message-use-followup-to
1637        (gnus-visible-headers .
1638          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1639 ")
1640
1641 (eval-when-compile
1642   ;; Bind features so that require will believe that gnus-sum has
1643   ;; already been loaded (avoids infinite recursion)
1644   (let ((features (cons 'gnus-sum features)))
1645     (require 'gnus-art)))
1646
1647 ;; MIME stuff.
1648
1649 (defvar gnus-decode-encoded-word-methods
1650   '(mail-decode-encoded-word-string)
1651   "List of methods used to decode encoded words.
1652
1653 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1654 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1655 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1656 whose names match REGEXP.
1657
1658 For example:
1659 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1660  mail-decode-encoded-word-string
1661  (\"chinese\" . rfc1843-decode-string))")
1662
1663 (defvar gnus-decode-encoded-word-methods-cache nil)
1664
1665 (defun gnus-multi-decode-encoded-word-string (string)
1666   "Apply the functions from `gnus-encoded-word-methods' that match."
1667   (unless (and gnus-decode-encoded-word-methods-cache
1668                (eq gnus-newsgroup-name
1669                    (car gnus-decode-encoded-word-methods-cache)))
1670     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1671     (dolist (method gnus-decode-encoded-word-methods)
1672       (if (symbolp method)
1673           (nconc gnus-decode-encoded-word-methods-cache (list method))
1674         (if (and gnus-newsgroup-name
1675                  (string-match (car method) gnus-newsgroup-name))
1676             (nconc gnus-decode-encoded-word-methods-cache
1677                    (list (cdr method)))))))
1678   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1679     (setq string (funcall method string))))
1680
1681 ;; Subject simplification.
1682
1683 (defun gnus-simplify-whitespace (str)
1684   "Remove excessive whitespace from STR."
1685   ;; Multiple spaces.
1686   (while (string-match "[ \t][ \t]+" str)
1687     (setq str (concat (substring str 0 (match-beginning 0))
1688                         " "
1689                         (substring str (match-end 0)))))
1690   ;; Leading spaces.
1691   (when (string-match "^[ \t]+" str)
1692     (setq str (substring str (match-end 0))))
1693   ;; Trailing spaces.
1694   (when (string-match "[ \t]+$" str)
1695     (setq str (substring str 0 (match-beginning 0))))
1696   str)
1697
1698 (defun gnus-simplify-all-whitespace (str)
1699   "Remove all whitespace from STR."
1700   (while (string-match "[ \t\n]+" str)
1701     (setq str (replace-match "" nil nil str)))
1702   str)
1703
1704 (defsubst gnus-simplify-subject-re (subject)
1705   "Remove \"Re:\" from subject lines."
1706   (if (string-match message-subject-re-regexp subject)
1707       (substring subject (match-end 0))
1708     subject))
1709
1710 (defun gnus-simplify-subject (subject &optional re-only)
1711   "Remove `Re:' and words in parentheses.
1712 If RE-ONLY is non-nil, strip leading `Re:'s only."
1713   (let ((case-fold-search t))           ;Ignore case.
1714     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1715     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1716       (setq subject (substring subject (match-end 0))))
1717     ;; Remove uninteresting prefixes.
1718     (when (and (not re-only)
1719                gnus-simplify-ignored-prefixes
1720                (string-match gnus-simplify-ignored-prefixes subject))
1721       (setq subject (substring subject (match-end 0))))
1722     ;; Remove words in parentheses from end.
1723     (unless re-only
1724       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1725         (setq subject (substring subject 0 (match-beginning 0)))))
1726     ;; Return subject string.
1727     subject))
1728
1729 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1730 ;; all whitespace.
1731 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1732   (goto-char (point-min))
1733   (while (re-search-forward regexp nil t)
1734     (replace-match (or newtext ""))))
1735
1736 (defun gnus-simplify-buffer-fuzzy ()
1737   "Simplify string in the buffer fuzzily.
1738 The string in the accessible portion of the current buffer is simplified.
1739 It is assumed to be a single-line subject.
1740 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1741 matter is removed.  Additional things can be deleted by setting
1742 `gnus-simplify-subject-fuzzy-regexp'."
1743   (let ((case-fold-search t)
1744         (modified-tick))
1745     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1746
1747     (while (not (eq modified-tick (buffer-modified-tick)))
1748       (setq modified-tick (buffer-modified-tick))
1749       (cond
1750        ((listp gnus-simplify-subject-fuzzy-regexp)
1751         (mapc 'gnus-simplify-buffer-fuzzy-step
1752               gnus-simplify-subject-fuzzy-regexp))
1753        (gnus-simplify-subject-fuzzy-regexp
1754         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-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     (gnus-set-work-buffer)
1770     (let ((case-fold-search t))
1771       ;; Remove uninteresting prefixes.
1772       (when (and gnus-simplify-ignored-prefixes
1773                  (string-match gnus-simplify-ignored-prefixes subject))
1774         (setq subject (substring subject (match-end 0))))
1775       (insert subject)
1776       (inline (gnus-simplify-buffer-fuzzy))
1777       (buffer-string))))
1778
1779 (defsubst gnus-simplify-subject-fully (subject)
1780   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1781   (cond
1782    (gnus-simplify-subject-functions
1783     (gnus-map-function gnus-simplify-subject-functions subject))
1784    ((null gnus-summary-gather-subject-limit)
1785     (gnus-simplify-subject-re subject))
1786    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1787     (gnus-simplify-subject-fuzzy subject))
1788    ((numberp gnus-summary-gather-subject-limit)
1789     (truncate-string-to-width (gnus-simplify-subject-re subject)
1790                               gnus-summary-gather-subject-limit))
1791    (t
1792     subject)))
1793
1794 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1795   "Check whether two subjects are equal.
1796 If optional argument SIMPLE-FIRST is t, first argument is already
1797 simplified."
1798   (cond
1799    ((null simple-first)
1800     (equal (gnus-simplify-subject-fully s1)
1801            (gnus-simplify-subject-fully s2)))
1802    (t
1803     (equal s1
1804            (gnus-simplify-subject-fully s2)))))
1805
1806 (defun gnus-summary-bubble-group ()
1807   "Increase the score of the current group.
1808 This is a handy function to add to `gnus-summary-exit-hook' to
1809 increase the score of each group you read."
1810   (gnus-group-add-score gnus-newsgroup-name))
1811
1812 \f
1813 ;;;
1814 ;;; Gnus summary mode
1815 ;;;
1816
1817 (put 'gnus-summary-mode 'mode-class 'special)
1818
1819 (defvar gnus-article-commands-menu)
1820
1821 ;; Non-orthogonal keys
1822
1823 (gnus-define-keys gnus-summary-mode-map
1824   " " gnus-summary-next-page
1825   "\177" gnus-summary-prev-page
1826   [delete] gnus-summary-prev-page
1827   [backspace] gnus-summary-prev-page
1828   "\r" gnus-summary-scroll-up
1829   "\M-\r" gnus-summary-scroll-down
1830   "n" gnus-summary-next-unread-article
1831   "p" gnus-summary-prev-unread-article
1832   "N" gnus-summary-next-article
1833   "P" gnus-summary-prev-article
1834   "\M-\C-n" gnus-summary-next-same-subject
1835   "\M-\C-p" gnus-summary-prev-same-subject
1836   "\M-n" gnus-summary-next-unread-subject
1837   "\M-p" gnus-summary-prev-unread-subject
1838   "." gnus-summary-first-unread-article
1839   "," gnus-summary-best-unread-article
1840   "\M-s" gnus-summary-search-article-forward
1841   "\M-r" gnus-summary-search-article-backward
1842   "\M-S" gnus-summary-repeat-search-article-forward
1843   "\M-R" gnus-summary-repeat-search-article-backward
1844   "<" gnus-summary-beginning-of-article
1845   ">" gnus-summary-end-of-article
1846   "j" gnus-summary-goto-article
1847   "^" gnus-summary-refer-parent-article
1848   "\M-^" gnus-summary-refer-article
1849   "u" gnus-summary-tick-article-forward
1850   "!" gnus-summary-tick-article-forward
1851   "U" gnus-summary-tick-article-backward
1852   "d" gnus-summary-mark-as-read-forward
1853   "D" gnus-summary-mark-as-read-backward
1854   "E" gnus-summary-mark-as-expirable
1855   "\M-u" gnus-summary-clear-mark-forward
1856   "\M-U" gnus-summary-clear-mark-backward
1857   "k" gnus-summary-kill-same-subject-and-select
1858   "\C-k" gnus-summary-kill-same-subject
1859   "\M-\C-k" gnus-summary-kill-thread
1860   "\M-\C-l" gnus-summary-lower-thread
1861   "e" gnus-summary-edit-article
1862   "#" gnus-summary-mark-as-processable
1863   "\M-#" gnus-summary-unmark-as-processable
1864   "\M-\C-t" gnus-summary-toggle-threads
1865   "\M-\C-s" gnus-summary-show-thread
1866   "\M-\C-h" gnus-summary-hide-thread
1867   "\M-\C-f" gnus-summary-next-thread
1868   "\M-\C-b" gnus-summary-prev-thread
1869   [(meta down)] gnus-summary-next-thread
1870   [(meta up)] gnus-summary-prev-thread
1871   "\M-\C-u" gnus-summary-up-thread
1872   "\M-\C-d" gnus-summary-down-thread
1873   "&" gnus-summary-execute-command
1874   "c" gnus-summary-catchup-and-exit
1875   "\C-w" gnus-summary-mark-region-as-read
1876   "\C-t" gnus-summary-toggle-truncation
1877   "?" gnus-summary-mark-as-dormant
1878   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1879   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1880   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1881   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1882   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1883   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1884   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1885   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1886   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1887   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1888   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1889   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1890   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1891   "=" gnus-summary-expand-window
1892   "\C-x\C-s" gnus-summary-reselect-current-group
1893   "\M-g" gnus-summary-rescan-group
1894   "\C-c\C-r" gnus-summary-caesar-message
1895   "f" gnus-summary-followup
1896   "F" gnus-summary-followup-with-original
1897   "C" gnus-summary-cancel-article
1898   "r" gnus-summary-reply
1899   "R" gnus-summary-reply-with-original
1900   "\C-c\C-f" gnus-summary-mail-forward
1901   "o" gnus-summary-save-article
1902   "\C-o" gnus-summary-save-article-mail
1903   "|" gnus-summary-pipe-output
1904   "\M-k" gnus-summary-edit-local-kill
1905   "\M-K" gnus-summary-edit-global-kill
1906   ;; "V" gnus-version
1907   "\C-c\C-d" gnus-summary-describe-group
1908   "q" gnus-summary-exit
1909   "Q" gnus-summary-exit-no-update
1910   "\C-c\C-i" gnus-info-find-node
1911   gnus-mouse-2 gnus-mouse-pick-article
1912   [follow-link] mouse-face
1913   "m" gnus-summary-mail-other-window
1914   "a" gnus-summary-post-news
1915   "x" gnus-summary-limit-to-unread
1916   "s" gnus-summary-isearch-article
1917   [tab] gnus-summary-widget-forward
1918   "t" gnus-summary-toggle-header
1919   "g" gnus-summary-show-article
1920   "l" gnus-summary-goto-last-article
1921   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1922   "\C-d" gnus-summary-enter-digest-group
1923   "\M-\C-d" gnus-summary-read-document
1924   "\M-\C-e" gnus-summary-edit-parameters
1925   "\M-\C-a" gnus-summary-customize-parameters
1926   "\C-c\C-b" gnus-bug
1927   "*" gnus-cache-enter-article
1928   "\M-*" gnus-cache-remove-article
1929   "\M-&" gnus-summary-universal-argument
1930   "\C-l" gnus-recenter
1931   "I" gnus-summary-increase-score
1932   "L" gnus-summary-lower-score
1933   "\M-i" gnus-symbolic-argument
1934   "h" gnus-summary-select-article-buffer
1935
1936   "b" gnus-article-view-part
1937   "\M-t" gnus-summary-toggle-display-buttonized
1938
1939   "V" gnus-summary-score-map
1940   "X" gnus-uu-extract-map
1941   "S" gnus-summary-send-map)
1942
1943 ;; Sort of orthogonal keymap
1944 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1945   "t" gnus-summary-tick-article-forward
1946   "!" gnus-summary-tick-article-forward
1947   "d" gnus-summary-mark-as-read-forward
1948   "r" gnus-summary-mark-as-read-forward
1949   "c" gnus-summary-clear-mark-forward
1950   " " gnus-summary-clear-mark-forward
1951   "e" gnus-summary-mark-as-expirable
1952   "x" gnus-summary-mark-as-expirable
1953   "?" gnus-summary-mark-as-dormant
1954   "b" gnus-summary-set-bookmark
1955   "B" gnus-summary-remove-bookmark
1956   "#" gnus-summary-mark-as-processable
1957   "\M-#" gnus-summary-unmark-as-processable
1958   "S" gnus-summary-limit-include-expunged
1959   "C" gnus-summary-catchup
1960   "H" gnus-summary-catchup-to-here
1961   "h" gnus-summary-catchup-from-here
1962   "\C-c" gnus-summary-catchup-all
1963   "k" gnus-summary-kill-same-subject-and-select
1964   "K" gnus-summary-kill-same-subject
1965   "P" gnus-uu-mark-map)
1966
1967 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1968   "c" gnus-summary-clear-above
1969   "u" gnus-summary-tick-above
1970   "m" gnus-summary-mark-above
1971   "k" gnus-summary-kill-below)
1972
1973 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1974   "/" gnus-summary-limit-to-subject
1975   "n" gnus-summary-limit-to-articles
1976   "b" gnus-summary-limit-to-bodies
1977   "h" gnus-summary-limit-to-headers
1978   "w" gnus-summary-pop-limit
1979   "s" gnus-summary-limit-to-subject
1980   "a" gnus-summary-limit-to-author
1981   "u" gnus-summary-limit-to-unread
1982   "m" gnus-summary-limit-to-marks
1983   "M" gnus-summary-limit-exclude-marks
1984   "v" gnus-summary-limit-to-score
1985   "*" gnus-summary-limit-include-cached
1986   "D" gnus-summary-limit-include-dormant
1987   "T" gnus-summary-limit-include-thread
1988   "d" gnus-summary-limit-exclude-dormant
1989   "t" gnus-summary-limit-to-age
1990   "." gnus-summary-limit-to-unseen
1991   "x" gnus-summary-limit-to-extra
1992   "p" gnus-summary-limit-to-display-predicate
1993   "E" gnus-summary-limit-include-expunged
1994   "c" gnus-summary-limit-exclude-childless-dormant
1995   "C" gnus-summary-limit-mark-excluded-as-read
1996   "o" gnus-summary-insert-old-articles
1997   "N" gnus-summary-insert-new-articles
1998   "S" gnus-summary-limit-to-singletons
1999   "r" gnus-summary-limit-to-replied
2000   "R" gnus-summary-limit-to-recipient
2001   "A" gnus-summary-limit-to-address)
2002
2003 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2004   "n" gnus-summary-next-unread-article
2005   "p" gnus-summary-prev-unread-article
2006   "N" gnus-summary-next-article
2007   "P" gnus-summary-prev-article
2008   "\C-n" gnus-summary-next-same-subject
2009   "\C-p" gnus-summary-prev-same-subject
2010   "\M-n" gnus-summary-next-unread-subject
2011   "\M-p" gnus-summary-prev-unread-subject
2012   "f" gnus-summary-first-unread-article
2013   "b" gnus-summary-best-unread-article
2014   "j" gnus-summary-goto-article
2015   "g" gnus-summary-goto-subject
2016   "l" gnus-summary-goto-last-article
2017   "o" gnus-summary-pop-article)
2018
2019 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2020   "k" gnus-summary-kill-thread
2021   "E" gnus-summary-expire-thread
2022   "l" gnus-summary-lower-thread
2023   "i" gnus-summary-raise-thread
2024   "T" gnus-summary-toggle-threads
2025   "t" gnus-summary-rethread-current
2026   "^" gnus-summary-reparent-thread
2027   "\M-^" gnus-summary-reparent-children
2028   "s" gnus-summary-show-thread
2029   "S" gnus-summary-show-all-threads
2030   "h" gnus-summary-hide-thread
2031   "H" gnus-summary-hide-all-threads
2032   "n" gnus-summary-next-thread
2033   "p" gnus-summary-prev-thread
2034   "u" gnus-summary-up-thread
2035   "o" gnus-summary-top-thread
2036   "d" gnus-summary-down-thread
2037   "#" gnus-uu-mark-thread
2038   "\M-#" gnus-uu-unmark-thread)
2039
2040 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2041   "g" gnus-summary-prepare
2042   "c" gnus-summary-insert-cached-articles
2043   "d" gnus-summary-insert-dormant-articles
2044   "t" gnus-summary-insert-ticked-articles)
2045
2046 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2047   "c" gnus-summary-catchup-and-exit
2048   "C" gnus-summary-catchup-all-and-exit
2049   "E" gnus-summary-exit-no-update
2050   "Q" gnus-summary-exit
2051   "Z" gnus-summary-exit
2052   "n" gnus-summary-catchup-and-goto-next-group
2053   "p" gnus-summary-catchup-and-goto-prev-group
2054   "R" gnus-summary-reselect-current-group
2055   "G" gnus-summary-rescan-group
2056   "N" gnus-summary-next-group
2057   "s" gnus-summary-save-newsrc
2058   "P" gnus-summary-prev-group)
2059
2060 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2061   " " gnus-summary-next-page
2062   "n" gnus-summary-next-page
2063   "\177" gnus-summary-prev-page
2064   [delete] gnus-summary-prev-page
2065   "p" gnus-summary-prev-page
2066   "\r" gnus-summary-scroll-up
2067   "\M-\r" gnus-summary-scroll-down
2068   "<" gnus-summary-beginning-of-article
2069   ">" gnus-summary-end-of-article
2070   "b" gnus-summary-beginning-of-article
2071   "e" gnus-summary-end-of-article
2072   "^" gnus-summary-refer-parent-article
2073   "r" gnus-summary-refer-parent-article
2074   "C" gnus-summary-show-complete-article
2075   "D" gnus-summary-enter-digest-group
2076   "R" gnus-summary-refer-references
2077   "T" gnus-summary-refer-thread
2078   "W" gnus-warp-to-article
2079   "g" gnus-summary-show-article
2080   "s" gnus-summary-isearch-article
2081   [tab] gnus-summary-widget-forward
2082   "P" gnus-summary-print-article
2083   "S" gnus-sticky-article
2084   "M" gnus-mailing-list-insinuate
2085   "t" gnus-article-babel)
2086
2087 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2088   "b" gnus-article-add-buttons
2089   "B" gnus-article-add-buttons-to-head
2090   "o" gnus-article-treat-overstrike
2091   "e" gnus-article-emphasize
2092   "w" gnus-article-fill-cited-article
2093   "Q" gnus-article-fill-long-lines
2094   "L" gnus-article-toggle-truncate-lines
2095   "C" gnus-article-capitalize-sentences
2096   "c" gnus-article-remove-cr
2097   "q" gnus-article-de-quoted-unreadable
2098   "6" gnus-article-de-base64-unreadable
2099   "Z" gnus-article-decode-HZ
2100   "A" gnus-article-treat-ansi-sequences
2101   "h" gnus-article-wash-html
2102   "u" gnus-article-unsplit-urls
2103   "s" gnus-summary-force-verify-and-decrypt
2104   "f" gnus-article-display-x-face
2105   "l" gnus-summary-stop-page-breaking
2106   "r" gnus-summary-caesar-message
2107   "m" gnus-summary-morse-message
2108   "t" gnus-summary-toggle-header
2109   "g" gnus-treat-smiley
2110   "v" gnus-summary-verbose-headers
2111   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2112   "p" gnus-article-verify-x-pgp-sig
2113   "d" gnus-article-treat-dumbquotes
2114   "U" gnus-article-treat-non-ascii
2115   "i" gnus-summary-idna-message)
2116
2117 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2118   ;; mnemonic: deuglif*Y*
2119   "u" gnus-article-outlook-unwrap-lines
2120   "a" gnus-article-outlook-repair-attribution
2121   "c" gnus-article-outlook-rearrange-citation
2122   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2123
2124 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2125   "a" gnus-article-hide
2126   "h" gnus-article-hide-headers
2127   "b" gnus-article-hide-boring-headers
2128   "s" gnus-article-hide-signature
2129   "c" gnus-article-hide-citation
2130   "C" gnus-article-hide-citation-in-followups
2131   "l" gnus-article-hide-list-identifiers
2132   "B" gnus-article-strip-banner
2133   "P" gnus-article-hide-pem
2134   "\C-c" gnus-article-hide-citation-maybe)
2135
2136 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2137   "a" gnus-article-highlight
2138   "h" gnus-article-highlight-headers
2139   "c" gnus-article-highlight-citation
2140   "s" gnus-article-highlight-signature)
2141
2142 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2143   "f" gnus-article-treat-fold-headers
2144   "u" gnus-article-treat-unfold-headers
2145   "n" gnus-article-treat-fold-newsgroups)
2146
2147 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2148   "x" gnus-article-display-x-face
2149   "d" gnus-article-display-face
2150   "s" gnus-treat-smiley
2151   "D" gnus-article-remove-images
2152   "W" gnus-article-show-images
2153   "f" gnus-treat-from-picon
2154   "m" gnus-treat-mail-picon
2155   "n" gnus-treat-newsgroups-picon
2156   "g" gnus-treat-from-gravatar
2157   "h" gnus-treat-mail-gravatar)
2158
2159 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2160   "w" gnus-article-decode-mime-words
2161   "c" gnus-article-decode-charset
2162   "v" gnus-mime-view-all-parts
2163   "b" gnus-article-view-part)
2164
2165 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2166   "z" gnus-article-date-ut
2167   "u" gnus-article-date-ut
2168   "l" gnus-article-date-local
2169   "p" gnus-article-date-english
2170   "e" gnus-article-date-lapsed
2171   "o" gnus-article-date-original
2172   "i" gnus-article-date-iso8601
2173   "s" gnus-article-date-user)
2174
2175 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2176   "t" gnus-article-remove-trailing-blank-lines
2177   "l" gnus-article-strip-leading-blank-lines
2178   "m" gnus-article-strip-multiple-blank-lines
2179   "a" gnus-article-strip-blank-lines
2180   "A" gnus-article-strip-all-blank-lines
2181   "s" gnus-article-strip-leading-space
2182   "e" gnus-article-strip-trailing-space
2183   "w" gnus-article-remove-leading-whitespace)
2184
2185 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2186   "v" gnus-version
2187   "d" gnus-summary-describe-group
2188   "h" gnus-summary-describe-briefly
2189   "i" gnus-info-find-node)
2190
2191 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2192   "e" gnus-summary-expire-articles
2193   "\M-\C-e" gnus-summary-expire-articles-now
2194   "\177" gnus-summary-delete-article
2195   [delete] gnus-summary-delete-article
2196   [backspace] gnus-summary-delete-article
2197   "m" gnus-summary-move-article
2198   "r" gnus-summary-respool-article
2199   "w" gnus-summary-edit-article
2200   "c" gnus-summary-copy-article
2201   "B" gnus-summary-crosspost-article
2202   "q" gnus-summary-respool-query
2203   "t" gnus-summary-respool-trace
2204   "i" gnus-summary-import-article
2205   "I" gnus-summary-create-article
2206   "p" gnus-summary-article-posted-p)
2207
2208 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2209   "o" gnus-summary-save-article
2210   "m" gnus-summary-save-article-mail
2211   "F" gnus-summary-write-article-file
2212   "r" gnus-summary-save-article-rmail
2213   "f" gnus-summary-save-article-file
2214   "b" gnus-summary-save-article-body-file
2215   "B" gnus-summary-write-article-body-file
2216   "h" gnus-summary-save-article-folder
2217   "v" gnus-summary-save-article-vm
2218   "p" gnus-summary-pipe-output
2219   "P" gnus-summary-muttprint)
2220
2221 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2222   "b" gnus-summary-display-buttonized
2223   "m" gnus-summary-repair-multipart
2224   "v" gnus-article-view-part
2225   "o" gnus-article-save-part
2226   "O" gnus-article-save-part-and-strip
2227   "r" gnus-article-replace-part
2228   "d" gnus-article-delete-part
2229   "t" gnus-article-view-part-as-type
2230   "j" gnus-article-jump-to-part
2231   "c" gnus-article-copy-part
2232   "C" gnus-article-view-part-as-charset
2233   "e" gnus-article-view-part-externally
2234   "H" gnus-article-browse-html-article
2235   "E" gnus-article-encrypt-body
2236   "i" gnus-article-inline-part
2237   "|" gnus-article-pipe-part)
2238
2239 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2240   "p" gnus-summary-mark-as-processable
2241   "u" gnus-summary-unmark-as-processable
2242   "U" gnus-summary-unmark-all-processable
2243   "v" gnus-uu-mark-over
2244   "s" gnus-uu-mark-series
2245   "r" gnus-uu-mark-region
2246   "g" gnus-uu-unmark-region
2247   "R" gnus-uu-mark-by-regexp
2248   "G" gnus-uu-unmark-by-regexp
2249   "t" gnus-uu-mark-thread
2250   "T" gnus-uu-unmark-thread
2251   "a" gnus-uu-mark-all
2252   "b" gnus-uu-mark-buffer
2253   "S" gnus-uu-mark-sparse
2254   "k" gnus-summary-kill-process-mark
2255   "y" gnus-summary-yank-process-mark
2256   "w" gnus-summary-save-process-mark
2257   "i" gnus-uu-invert-processable)
2258
2259 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2260   ;;"x" gnus-uu-extract-any
2261   "m" gnus-summary-save-parts
2262   "u" gnus-uu-decode-uu
2263   "U" gnus-uu-decode-uu-and-save
2264   "s" gnus-uu-decode-unshar
2265   "S" gnus-uu-decode-unshar-and-save
2266   "o" gnus-uu-decode-save
2267   "O" gnus-uu-decode-save
2268   "b" gnus-uu-decode-binhex
2269   "B" gnus-uu-decode-binhex
2270   "Y" gnus-uu-decode-yenc
2271   "p" gnus-uu-decode-postscript
2272   "P" gnus-uu-decode-postscript-and-save)
2273
2274 (gnus-define-keys
2275     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2276   "u" gnus-uu-decode-uu-view
2277   "U" gnus-uu-decode-uu-and-save-view
2278   "s" gnus-uu-decode-unshar-view
2279   "S" gnus-uu-decode-unshar-and-save-view
2280   "o" gnus-uu-decode-save-view
2281   "O" gnus-uu-decode-save-view
2282   "b" gnus-uu-decode-binhex-view
2283   "B" gnus-uu-decode-binhex-view
2284   "p" gnus-uu-decode-postscript-view
2285   "P" gnus-uu-decode-postscript-and-save-view)
2286
2287 (defvar gnus-article-post-menu nil)
2288
2289 (defconst gnus-summary-menu-maxlen 20)
2290
2291 (defun gnus-summary-menu-split (menu)
2292   ;; If we have lots of elements, divide them into groups of 20
2293   ;; and make a pane (or submenu) for each one.
2294   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2295       (let ((menu menu) sublists next
2296             (i 1))
2297         (while menu
2298           ;; Pull off the next gnus-summary-menu-maxlen elements
2299           ;; and make them the next element of sublist.
2300           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2301           (if next
2302               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2303                       nil))
2304           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2305                                              (aref (car (last menu)) 0)) menu)
2306                                sublists))
2307           (setq i (1+ i))
2308           (setq menu next))
2309         (nreverse sublists))
2310     ;; Few elements--put them all in one pane.
2311     menu))
2312
2313 (defun gnus-summary-make-menu-bar ()
2314   (gnus-turn-off-edit-menu 'summary)
2315
2316   (unless (boundp 'gnus-summary-misc-menu)
2317
2318     (easy-menu-define
2319       gnus-summary-kill-menu gnus-summary-mode-map ""
2320       (cons
2321        "Score"
2322        (nconc
2323         (list
2324          ["Customize" gnus-score-customize t])
2325         (gnus-make-score-map 'increase)
2326         (gnus-make-score-map 'lower)
2327         '(("Mark"
2328            ["Kill below" gnus-summary-kill-below t]
2329            ["Mark above" gnus-summary-mark-above t]
2330            ["Tick above" gnus-summary-tick-above t]
2331            ["Clear above" gnus-summary-clear-above t])
2332           ["Current score" gnus-summary-current-score t]
2333           ["Set score" gnus-summary-set-score t]
2334           ["Switch current score file..." gnus-score-change-score-file t]
2335           ["Set mark below..." gnus-score-set-mark-below t]
2336           ["Set expunge below..." gnus-score-set-expunge-below t]
2337           ["Edit current score file" gnus-score-edit-current-scores t]
2338           ["Edit score file..." gnus-score-edit-file t]
2339           ["Trace score" gnus-score-find-trace t]
2340           ["Find words" gnus-score-find-favourite-words t]
2341           ["Rescore buffer" gnus-summary-rescore t]
2342           ["Increase score..." gnus-summary-increase-score t]
2343           ["Lower score..." gnus-summary-lower-score t]))))
2344
2345     ;; Define both the Article menu in the summary buffer and the
2346     ;; equivalent Commands menu in the article buffer here for
2347     ;; consistency.
2348     (let ((innards
2349            `(("Hide"
2350               ["All" gnus-article-hide t]
2351               ["Headers" gnus-article-hide-headers t]
2352               ["Signature" gnus-article-hide-signature t]
2353               ["Citation" gnus-article-hide-citation t]
2354               ["List identifiers" gnus-article-hide-list-identifiers t]
2355               ["Banner" gnus-article-strip-banner t]
2356               ["Boring headers" gnus-article-hide-boring-headers t])
2357              ("Highlight"
2358               ["All" gnus-article-highlight t]
2359               ["Headers" gnus-article-highlight-headers t]
2360               ["Signature" gnus-article-highlight-signature t]
2361               ["Citation" gnus-article-highlight-citation t])
2362              ("MIME"
2363               ["Words" gnus-article-decode-mime-words t]
2364               ["Charset" gnus-article-decode-charset t]
2365               ["QP" gnus-article-de-quoted-unreadable t]
2366               ["Base64" gnus-article-de-base64-unreadable t]
2367               ["View MIME buttons" gnus-summary-display-buttonized t]
2368               ["View all" gnus-mime-view-all-parts t]
2369               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2370               ["Encrypt body" gnus-article-encrypt-body
2371                :active (not (gnus-group-read-only-p))
2372                ,@(if (featurep 'xemacs) nil
2373                    '(:help "Encrypt the message body on disk"))]
2374               ["Extract all parts..." gnus-summary-save-parts t]
2375               ("Multipart"
2376                ["Repair multipart" gnus-summary-repair-multipart t]
2377                ["Pipe part..." gnus-article-pipe-part t]
2378                ["Inline part" gnus-article-inline-part t]
2379                ["View part as type..." gnus-article-view-part-as-type t]
2380                ["Encrypt body" gnus-article-encrypt-body
2381                 :active (not (gnus-group-read-only-p))
2382                ,@(if (featurep 'xemacs) nil
2383                    '(:help "Encrypt the message body on disk"))]
2384                ["View part externally" gnus-article-view-part-externally t]
2385                ["View HTML parts in browser" gnus-article-browse-html-article t]
2386                ["View part with charset..." gnus-article-view-part-as-charset t]
2387                ["Copy part" gnus-article-copy-part t]
2388                ["Save part..." gnus-article-save-part t]
2389                ["View part" gnus-article-view-part t]))
2390              ("Date"
2391               ["Local" gnus-article-date-local t]
2392               ["ISO8601" gnus-article-date-iso8601 t]
2393               ["UT" gnus-article-date-ut t]
2394               ["Original" gnus-article-date-original t]
2395               ["Lapsed" gnus-article-date-lapsed t]
2396               ["User-defined" gnus-article-date-user t])
2397              ("Display"
2398               ["Remove images" gnus-article-remove-images t]
2399               ["Toggle smiley" gnus-treat-smiley t]
2400               ["Show X-Face" gnus-article-display-x-face t]
2401               ["Show picons in From" gnus-treat-from-picon t]
2402               ["Show picons in mail headers" gnus-treat-mail-picon t]
2403               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2404               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2405               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2406               ("View as different encoding"
2407                ,@(gnus-summary-menu-split
2408                   (mapcar
2409                    (lambda (cs)
2410                      ;; Since easymenu under Emacs doesn't allow
2411                      ;; lambda forms for menu commands, we should
2412                      ;; provide intern'ed function symbols.
2413                      (let ((command (intern (format "\
2414 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2415                        (fset command
2416                              `(lambda ()
2417                                 (interactive)
2418                                 (let ((gnus-summary-show-article-charset-alist
2419                                        '((1 . ,cs))))
2420                                   (gnus-summary-show-article 1))))
2421                        `[,(symbol-name cs) ,command t]))
2422                    (sort (if (fboundp 'coding-system-list)
2423                              (coding-system-list)
2424                            (mapcar 'car mm-mime-mule-charset-alist))
2425                          'string<)))))
2426              ("Washing"
2427               ("Remove Blanks"
2428                ["Leading" gnus-article-strip-leading-blank-lines t]
2429                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2430                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2431                ["All of the above" gnus-article-strip-blank-lines t]
2432                ["All" gnus-article-strip-all-blank-lines t]
2433                ["Leading space" gnus-article-strip-leading-space t]
2434                ["Trailing space" gnus-article-strip-trailing-space t]
2435                ["Leading space in headers"
2436                 gnus-article-remove-leading-whitespace t])
2437               ["Overstrike" gnus-article-treat-overstrike t]
2438               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2439               ["Non-ASCII" gnus-article-treat-non-ascii t]
2440               ["Emphasis" gnus-article-emphasize t]
2441               ["Word wrap" gnus-article-fill-cited-article t]
2442               ["Fill long lines" gnus-article-fill-long-lines t]
2443               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2444               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2445               ["Remove CR" gnus-article-remove-cr t]
2446               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2447               ["Base64" gnus-article-de-base64-unreadable t]
2448               ["Rot 13" gnus-summary-caesar-message
2449                ,@(if (featurep 'xemacs) '(t)
2450                    '(:help "\"Caesar rotate\" article by 13"))]
2451               ["De-IDNA" gnus-summary-idna-message t]
2452               ["Morse decode" gnus-summary-morse-message t]
2453               ["Unix pipe..." gnus-summary-pipe-message t]
2454               ["Add buttons" gnus-article-add-buttons t]
2455               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2456               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2457               ["Verbose header" gnus-summary-verbose-headers t]
2458               ["Toggle header" gnus-summary-toggle-header t]
2459               ["Unfold headers" gnus-article-treat-unfold-headers t]
2460               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2461               ["Html" gnus-article-wash-html t]
2462               ["Unsplit URLs" gnus-article-unsplit-urls t]
2463               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2464               ["Decode HZ" gnus-article-decode-HZ t]
2465               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2466               ("(Outlook) Deuglify"
2467                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2468                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2469                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2470                ["Full (Outlook) deuglify"
2471                 gnus-article-outlook-deuglify-article t])
2472               )
2473              ("Output"
2474               ["Save in default format..." gnus-summary-save-article
2475                ,@(if (featurep 'xemacs) '(t)
2476                    '(:help "Save article using default method"))]
2477               ["Save in file..." gnus-summary-save-article-file
2478                ,@(if (featurep 'xemacs) '(t)
2479                    '(:help "Save article in file"))]
2480               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2481               ["Save in MH folder..." gnus-summary-save-article-folder t]
2482               ["Save in VM folder..." gnus-summary-save-article-vm t]
2483               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2484               ["Save body in file..." gnus-summary-save-article-body-file t]
2485               ["Pipe through a filter..." gnus-summary-pipe-output t]
2486               ["Print with Muttprint..." gnus-summary-muttprint t]
2487               ["Print" gnus-summary-print-article
2488                ,@(if (featurep 'xemacs) '(t)
2489                    '(:help "Generate and print a PostScript image"))])
2490              ("Copy, move,... (Backend)"
2491               ,@(if (featurep 'xemacs) nil
2492                   '(:help "Copying, moving, expiring articles..."))
2493               ["Respool article..." gnus-summary-respool-article t]
2494               ["Move article..." gnus-summary-move-article
2495                (gnus-check-backend-function
2496                 'request-move-article gnus-newsgroup-name)]
2497               ["Copy article..." gnus-summary-copy-article t]
2498               ["Crosspost article..." gnus-summary-crosspost-article
2499                (gnus-check-backend-function
2500                 'request-replace-article gnus-newsgroup-name)]
2501               ["Import file..." gnus-summary-import-article
2502                (gnus-check-backend-function
2503                 'request-accept-article gnus-newsgroup-name)]
2504               ["Create article..." gnus-summary-create-article
2505                (gnus-check-backend-function
2506                 'request-accept-article gnus-newsgroup-name)]
2507               ["Check if posted" gnus-summary-article-posted-p t]
2508               ["Edit article" gnus-summary-edit-article
2509                (not (gnus-group-read-only-p))]
2510               ["Delete article" gnus-summary-delete-article
2511                (gnus-check-backend-function
2512                 'request-expire-articles gnus-newsgroup-name)]
2513               ["Query respool" gnus-summary-respool-query t]
2514               ["Trace respool" gnus-summary-respool-trace t]
2515               ["Delete expirable articles" gnus-summary-expire-articles-now
2516                (gnus-check-backend-function
2517                 'request-expire-articles gnus-newsgroup-name)])
2518              ("Extract"
2519               ["Uudecode" gnus-uu-decode-uu
2520                ,@(if (featurep 'xemacs) '(t)
2521                    '(:help "Decode uuencoded article(s)"))]
2522               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2523               ["Unshar" gnus-uu-decode-unshar t]
2524               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2525               ["Save" gnus-uu-decode-save t]
2526               ["Binhex" gnus-uu-decode-binhex t]
2527               ["Postscript" gnus-uu-decode-postscript t]
2528               ["All MIME parts" gnus-summary-save-parts t])
2529              ("Cache"
2530               ["Enter article" gnus-cache-enter-article t]
2531               ["Remove article" gnus-cache-remove-article t])
2532              ["Translate" gnus-article-babel t]
2533              ["Select article buffer" gnus-summary-select-article-buffer t]
2534              ["Make article buffer sticky" gnus-sticky-article t]
2535              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2536              ["Isearch article..." gnus-summary-isearch-article t]
2537              ["Beginning of the article" gnus-summary-beginning-of-article t]
2538              ["End of the article" gnus-summary-end-of-article t]
2539              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2540              ["Fetch referenced articles" gnus-summary-refer-references t]
2541              ["Fetch current thread" gnus-summary-refer-thread t]
2542              ["Fetch article with id..." gnus-summary-refer-article t]
2543              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2544              ["Redisplay" gnus-summary-show-article t]
2545              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2546       (easy-menu-define
2547         gnus-summary-article-menu gnus-summary-mode-map ""
2548         (cons "Article" innards))
2549
2550       (if (not (keymapp gnus-summary-article-menu))
2551           (easy-menu-define
2552             gnus-article-commands-menu gnus-article-mode-map ""
2553             (cons "Commands" innards))
2554         ;; in Emacs, don't share menu.
2555         (setq gnus-article-commands-menu
2556               (copy-keymap gnus-summary-article-menu))
2557         (define-key gnus-article-mode-map [menu-bar commands]
2558           (cons "Commands" gnus-article-commands-menu))))
2559
2560     (easy-menu-define
2561       gnus-summary-thread-menu gnus-summary-mode-map ""
2562       '("Threads"
2563         ["Find all messages in thread" gnus-summary-refer-thread t]
2564         ["Toggle threading" gnus-summary-toggle-threads t]
2565         ["Hide threads" gnus-summary-hide-all-threads t]
2566         ["Show threads" gnus-summary-show-all-threads t]
2567         ["Hide thread" gnus-summary-hide-thread t]
2568         ["Show thread" gnus-summary-show-thread t]
2569         ["Go to next thread" gnus-summary-next-thread t]
2570         ["Go to previous thread" gnus-summary-prev-thread t]
2571         ["Go down thread" gnus-summary-down-thread t]
2572         ["Go up thread" gnus-summary-up-thread t]
2573         ["Top of thread" gnus-summary-top-thread t]
2574         ["Mark thread as read" gnus-summary-kill-thread t]
2575         ["Mark thread as expired" gnus-summary-expire-thread t]
2576         ["Lower thread score" gnus-summary-lower-thread t]
2577         ["Raise thread score" gnus-summary-raise-thread t]
2578         ["Rethread current" gnus-summary-rethread-current t]))
2579
2580     (easy-menu-define
2581       gnus-summary-post-menu gnus-summary-mode-map ""
2582       `("Post"
2583         ["Send a message (mail or news)" gnus-summary-post-news
2584          ,@(if (featurep 'xemacs) '(t)
2585              '(:help "Compose a new message (mail or news)"))]
2586         ["Followup" gnus-summary-followup
2587          ,@(if (featurep 'xemacs) '(t)
2588              '(:help "Post followup to this article"))]
2589         ["Followup and yank" gnus-summary-followup-with-original
2590          ,@(if (featurep 'xemacs) '(t)
2591              '(:help "Post followup to this article, quoting its contents"))]
2592         ["Supersede article" gnus-summary-supersede-article t]
2593         ["Cancel article" gnus-summary-cancel-article
2594          ,@(if (featurep 'xemacs) '(t)
2595              '(:help "Cancel an article you posted"))]
2596         ["Reply" gnus-summary-reply t]
2597         ["Reply and yank" gnus-summary-reply-with-original t]
2598         ["Wide reply" gnus-summary-wide-reply t]
2599         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2600          ,@(if (featurep 'xemacs) '(t)
2601              '(:help "Mail a reply, quoting this article"))]
2602         ["Very wide reply" gnus-summary-very-wide-reply t]
2603         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2604          ,@(if (featurep 'xemacs) '(t)
2605              '(:help "Mail a very wide reply, quoting this article"))]
2606         ["Mail forward" gnus-summary-mail-forward t]
2607         ["Post forward" gnus-summary-post-forward t]
2608         ["Digest and mail" gnus-uu-digest-mail-forward t]
2609         ["Digest and post" gnus-uu-digest-post-forward t]
2610         ["Resend message" gnus-summary-resend-message t]
2611         ["Resend message edit" gnus-summary-resend-message-edit t]
2612         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2613         ["Send a mail" gnus-summary-mail-other-window t]
2614         ["Create a local message" gnus-summary-news-other-window t]
2615         ["Uuencode and post" gnus-uu-post-news
2616          ,@(if (featurep 'xemacs) '(t)
2617              '(:help "Post a uuencoded article"))]
2618         ["Followup via news" gnus-summary-followup-to-mail t]
2619         ["Followup via news and yank"
2620          gnus-summary-followup-to-mail-with-original t]
2621         ["Strip signature on reply"
2622          (lambda ()
2623            (interactive)
2624            (if (not (memq message-cite-function
2625                           '(message-cite-original-without-signature
2626                             message-cite-original)))
2627                ;; Stupid workaround for XEmacs not honoring :visible.
2628                (message "Can't toggle this value of `message-cite-function'")
2629              (setq message-cite-function
2630                    (if (eq message-cite-function
2631                            'message-cite-original-without-signature)
2632                        'message-cite-original
2633                      'message-cite-original-without-signature))))
2634          ;; XEmacs barfs on :visible.
2635          ,@(if (featurep 'xemacs) nil
2636              '(:visible (memq message-cite-function
2637                               '(message-cite-original-without-signature
2638                                 message-cite-original))))
2639          :style toggle
2640          :selected (eq message-cite-function
2641                        'message-cite-original-without-signature)
2642          ,@(if (featurep 'xemacs) nil
2643              '(:help "Strip signature from cited article when replying."))]
2644         ;;("Draft"
2645         ;;["Send" gnus-summary-send-draft t]
2646         ;;["Send bounced" gnus-resend-bounced-mail t])
2647         ))
2648
2649     (cond
2650      ((not (keymapp gnus-summary-post-menu))
2651       (setq gnus-article-post-menu gnus-summary-post-menu))
2652      ((not gnus-article-post-menu)
2653       ;; Don't share post menu.
2654       (setq gnus-article-post-menu
2655             (copy-keymap gnus-summary-post-menu))))
2656     (define-key gnus-article-mode-map [menu-bar post]
2657       (cons "Post" gnus-article-post-menu))
2658
2659     (easy-menu-define
2660       gnus-summary-misc-menu gnus-summary-mode-map ""
2661       `("Gnus"
2662         ("Mark Read"
2663          ["Mark as read" gnus-summary-mark-as-read-forward t]
2664          ["Mark same subject and select"
2665           gnus-summary-kill-same-subject-and-select t]
2666          ["Mark same subject" gnus-summary-kill-same-subject t]
2667          ["Catchup" gnus-summary-catchup
2668           ,@(if (featurep 'xemacs) '(t)
2669               '(:help "Mark unread articles in this group as read"))]
2670          ["Catchup all" gnus-summary-catchup-all t]
2671          ["Catchup to here" gnus-summary-catchup-to-here t]
2672          ["Catchup from here" gnus-summary-catchup-from-here t]
2673          ["Catchup region" gnus-summary-mark-region-as-read
2674           (gnus-mark-active-p)]
2675          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2676         ("Mark Various"
2677          ["Tick" gnus-summary-tick-article-forward t]
2678          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2679          ["Remove marks" gnus-summary-clear-mark-forward t]
2680          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2681          ["Set bookmark" gnus-summary-set-bookmark t]
2682          ["Remove bookmark" gnus-summary-remove-bookmark t])
2683         ("Limit to"
2684          ["Marks..." gnus-summary-limit-to-marks t]
2685          ["Subject..." gnus-summary-limit-to-subject t]
2686          ["Author..." gnus-summary-limit-to-author t]
2687          ["Recipient..." gnus-summary-limit-to-recipient t]
2688          ["Address..." gnus-summary-limit-to-address t]
2689          ["Age..." gnus-summary-limit-to-age t]
2690          ["Extra..." gnus-summary-limit-to-extra t]
2691          ["Score..." gnus-summary-limit-to-score t]
2692          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2693          ["Unread" gnus-summary-limit-to-unread t]
2694          ["Unseen" gnus-summary-limit-to-unseen t]
2695          ["Singletons" gnus-summary-limit-to-singletons t]
2696          ["Replied" gnus-summary-limit-to-replied t]
2697          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2698          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2699          ["Pop limit" gnus-summary-pop-limit t]
2700          ["Show dormant" gnus-summary-limit-include-dormant t]
2701          ["Hide childless dormant"
2702           gnus-summary-limit-exclude-childless-dormant t]
2703          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2704          ["Hide marked" gnus-summary-limit-exclude-marks t]
2705          ["Show expunged" gnus-summary-limit-include-expunged t])
2706         ("Process Mark"
2707          ["Set mark" gnus-summary-mark-as-processable t]
2708          ["Remove mark" gnus-summary-unmark-as-processable t]
2709          ["Remove all marks" gnus-summary-unmark-all-processable t]
2710          ["Invert marks" gnus-uu-invert-processable t]
2711          ["Mark above" gnus-uu-mark-over t]
2712          ["Mark series" gnus-uu-mark-series t]
2713          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2714          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2715          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2716          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2717          ["Mark all" gnus-uu-mark-all t]
2718          ["Mark buffer" gnus-uu-mark-buffer t]
2719          ["Mark sparse" gnus-uu-mark-sparse t]
2720          ["Mark thread" gnus-uu-mark-thread t]
2721          ["Unmark thread" gnus-uu-unmark-thread t]
2722          ("Process Mark Sets"
2723           ["Kill" gnus-summary-kill-process-mark t]
2724           ["Yank" gnus-summary-yank-process-mark
2725            gnus-newsgroup-process-stack]
2726           ["Save" gnus-summary-save-process-mark t]
2727           ["Run command on marked..." gnus-summary-universal-argument t]))
2728         ("Registry Marks")
2729         ("Scroll article"
2730          ["Page forward" gnus-summary-next-page
2731           ,@(if (featurep 'xemacs) '(t)
2732               '(:help "Show next page of article"))]
2733          ["Page backward" gnus-summary-prev-page
2734           ,@(if (featurep 'xemacs) '(t)
2735               '(:help "Show previous page of article"))]
2736          ["Line forward" gnus-summary-scroll-up t])
2737         ("Move"
2738          ["Next unread article" gnus-summary-next-unread-article t]
2739          ["Previous unread article" gnus-summary-prev-unread-article t]
2740          ["Next article" gnus-summary-next-article t]
2741          ["Previous article" gnus-summary-prev-article t]
2742          ["Next unread subject" gnus-summary-next-unread-subject t]
2743          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2744          ["Next article same subject" gnus-summary-next-same-subject t]
2745          ["Previous article same subject" gnus-summary-prev-same-subject t]
2746          ["First unread article" gnus-summary-first-unread-article t]
2747          ["Best unread article" gnus-summary-best-unread-article t]
2748          ["Go to subject number..." gnus-summary-goto-subject t]
2749          ["Go to article number..." gnus-summary-goto-article t]
2750          ["Go to the last article" gnus-summary-goto-last-article t]
2751          ["Pop article off history" gnus-summary-pop-article t])
2752         ("Sort"
2753          ["Sort by number" gnus-summary-sort-by-number t]
2754          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2755          ["Sort by author" gnus-summary-sort-by-author t]
2756          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2757          ["Sort by subject" gnus-summary-sort-by-subject t]
2758          ["Sort by date" gnus-summary-sort-by-date t]
2759          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2760          ["Sort by score" gnus-summary-sort-by-score t]
2761          ["Sort by lines" gnus-summary-sort-by-lines t]
2762          ["Sort by characters" gnus-summary-sort-by-chars t]
2763          ["Randomize" gnus-summary-sort-by-random t]
2764          ["Original sort" gnus-summary-sort-by-original t])
2765         ("Help"
2766          ["Describe group" gnus-summary-describe-group t]
2767          ["Read manual" gnus-info-find-node t])
2768         ("Modes"
2769          ["Pick and read" gnus-pick-mode t]
2770          ["Binary" gnus-binary-mode t])
2771         ("Regeneration"
2772          ["Regenerate" gnus-summary-prepare t]
2773          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2774          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2775          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2776          ["Toggle threading" gnus-summary-toggle-threads t])
2777         ["See old articles" gnus-summary-insert-old-articles t]
2778         ["See new articles" gnus-summary-insert-new-articles t]
2779         ["Filter articles..." gnus-summary-execute-command t]
2780         ["Run command on articles..." gnus-summary-universal-argument t]
2781         ["Search articles forward..." gnus-summary-search-article-forward t]
2782         ["Search articles backward..." gnus-summary-search-article-backward t]
2783         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2784         ["Expand window" gnus-summary-expand-window t]
2785         ["Expire expirable articles" gnus-summary-expire-articles
2786          (gnus-check-backend-function
2787           'request-expire-articles gnus-newsgroup-name)]
2788         ["Edit local kill file" gnus-summary-edit-local-kill t]
2789         ["Edit main kill file" gnus-summary-edit-global-kill t]
2790         ["Edit group parameters" gnus-summary-edit-parameters t]
2791         ["Customize group parameters" gnus-summary-customize-parameters t]
2792         ["Send a bug report" gnus-bug t]
2793         ("Exit"
2794          ["Catchup and exit" gnus-summary-catchup-and-exit
2795           ,@(if (featurep 'xemacs) '(t)
2796               '(:help "Mark unread articles in this group as read, then exit"))]
2797          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2798          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2799          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2800          ["Exit group" gnus-summary-exit
2801           ,@(if (featurep 'xemacs) '(t)
2802               '(:help "Exit current group, return to group selection mode"))]
2803          ["Exit group without updating" gnus-summary-exit-no-update t]
2804          ["Exit and goto next group" gnus-summary-next-group t]
2805          ["Exit and goto prev group" gnus-summary-prev-group t]
2806          ["Reselect group" gnus-summary-reselect-current-group t]
2807          ["Rescan group" gnus-summary-rescan-group t]
2808          ["Update dribble" gnus-summary-save-newsrc t])))
2809
2810     (gnus-run-hooks 'gnus-summary-menu-hook)))
2811
2812 (defvar gnus-summary-tool-bar-map nil)
2813
2814 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2815 ;; affect _new_ message buffers.  We might add a function that walks thru all
2816 ;; summary-mode buffers and force the update.
2817 (defun gnus-summary-tool-bar-update (&optional symbol value)
2818   "Update summary mode toolbar.
2819 Setter function for custom variables."
2820   (setq-default gnus-summary-tool-bar-map nil)
2821   (when symbol
2822     ;; When used as ":set" function:
2823     (set-default symbol value))
2824   (when (gnus-buffer-live-p gnus-summary-buffer)
2825     (with-current-buffer gnus-summary-buffer
2826       (gnus-summary-make-tool-bar))))
2827
2828 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2829                                      'gnus-summary-tool-bar-gnome
2830                                    'gnus-summary-tool-bar-retro)
2831   "Specifies the Gnus summary tool bar.
2832
2833 It can be either a list or a symbol refering to a list.  See
2834 `gmm-tool-bar-from-list' for the format of the list.  The
2835 default key map is `gnus-summary-mode-map'.
2836
2837 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2838 `gnus-summary-tool-bar-retro'."
2839   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2840                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2841                  (repeat :tag "User defined list" gmm-tool-bar-item)
2842                  (symbol))
2843   :version "23.1" ;; No Gnus
2844   :initialize 'custom-initialize-default
2845   :set 'gnus-summary-tool-bar-update
2846   :group 'gnus-summary)
2847
2848 (defcustom gnus-summary-tool-bar-gnome
2849   '((gnus-summary-post-news "mail/compose" nil)
2850     (gnus-summary-insert-new-articles "mail/inbox" nil
2851                                       :visible (or (not gnus-agent)
2852                                                    gnus-plugged))
2853     (gnus-summary-reply-with-original "mail/reply")
2854     (gnus-summary-reply "mail/reply" nil :visible nil)
2855     (gnus-summary-followup-with-original "mail/reply-all")
2856     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2857     (gnus-summary-mail-forward "mail/forward")
2858     (gnus-summary-save-article "mail/save")
2859     (gnus-summary-search-article-forward "search" nil :visible nil)
2860     (gnus-summary-print-article "print")
2861     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2862     ;; Some new commands that may need more suitable icons:
2863     (gnus-summary-save-newsrc "save" nil :visible nil)
2864     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2865     (gnus-summary-prev-article "left-arrow")
2866     (gnus-summary-next-article "right-arrow")
2867     (gnus-summary-next-page "next-page")
2868     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2869     ;;
2870     ;; Maybe some sort-by-... could be added:
2871     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2872     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2873     (gnus-summary-mark-as-expirable
2874      "delete" nil
2875      :visible (gnus-check-backend-function 'request-expire-articles
2876                                            gnus-newsgroup-name))
2877     (gnus-summary-mark-as-spam
2878      "mail/spam" t
2879      :visible (and (fboundp 'spam-group-ham-contents-p)
2880                    (spam-group-ham-contents-p gnus-newsgroup-name))
2881      :help "Mark as spam")
2882     (gnus-summary-mark-as-read-forward
2883      "mail/not-spam" nil
2884      :visible (and (fboundp 'spam-group-spam-contents-p)
2885                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2886     ;;
2887     (gnus-summary-exit "exit")
2888     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2889     (gnus-info-find-node "help"))
2890   "List of functions for the summary tool bar (GNOME style).
2891
2892 See `gmm-tool-bar-from-list' for the format of the list."
2893   :type '(repeat gmm-tool-bar-item)
2894   :version "23.1" ;; No Gnus
2895   :initialize 'custom-initialize-default
2896   :set 'gnus-summary-tool-bar-update
2897   :group 'gnus-summary)
2898
2899 (defcustom gnus-summary-tool-bar-retro
2900   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2901     (gnus-summary-next-unread-article "gnus/next-ur")
2902     (gnus-summary-post-news "gnus/post")
2903     (gnus-summary-followup-with-original "gnus/fuwo")
2904     (gnus-summary-followup "gnus/followup")
2905     (gnus-summary-reply-with-original "gnus/reply-wo")
2906     (gnus-summary-reply "gnus/reply")
2907     (gnus-summary-caesar-message "gnus/rot13")
2908     (gnus-uu-decode-uu "gnus/uu-decode")
2909     (gnus-summary-save-article-file "gnus/save-aif")
2910     (gnus-summary-save-article "gnus/save-art")
2911     (gnus-uu-post-news "gnus/uu-post")
2912     (gnus-summary-catchup "gnus/catchup")
2913     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2914     (gnus-summary-exit "gnus/exit-summ")
2915     ;; Some new command that may need more suitable icons:
2916     (gnus-summary-print-article "gnus/print" nil :visible nil)
2917     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2918     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2919     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2920     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2921     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2922     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2923     ;;
2924     (gnus-info-find-node "gnus/help" nil :visible nil))
2925   "List of functions for the summary tool bar (retro look).
2926
2927 See `gmm-tool-bar-from-list' for the format of the list."
2928   :type '(repeat gmm-tool-bar-item)
2929   :version "23.1" ;; No Gnus
2930   :initialize 'custom-initialize-default
2931   :set 'gnus-summary-tool-bar-update
2932   :group 'gnus-summary)
2933
2934 (defcustom gnus-summary-tool-bar-zap-list t
2935   "List of icon items from the global tool bar.
2936 These items are not displayed in the Gnus summary mode tool bar.
2937
2938 See `gmm-tool-bar-from-list' for the format of the list."
2939   :type 'gmm-tool-bar-zap-list
2940   :version "23.1" ;; No Gnus
2941   :initialize 'custom-initialize-default
2942   :set 'gnus-summary-tool-bar-update
2943   :group 'gnus-summary)
2944
2945 (defvar image-load-path)
2946 (defvar tool-bar-map)
2947
2948 (defun gnus-summary-make-tool-bar (&optional force)
2949   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2950 When FORCE, rebuild the tool bar."
2951   (when (and (not (featurep 'xemacs))
2952              (boundp 'tool-bar-mode)
2953              tool-bar-mode
2954              (or (not gnus-summary-tool-bar-map) force))
2955     (let* ((load-path
2956             (gmm-image-load-path-for-library "gnus"
2957                                              "mail/save.xpm"
2958                                              nil t))
2959            (image-load-path (cons (car load-path)
2960                                   (when (boundp 'image-load-path)
2961                                     image-load-path)))
2962            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2963                                         gnus-summary-tool-bar-zap-list
2964                                         'gnus-summary-mode-map)))
2965       (when map
2966         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2967         ;; uses it's value.
2968         (setq gnus-summary-tool-bar-map map))))
2969   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2970
2971 (defun gnus-score-set-default (var value)
2972   "A version of set that updates the GNU Emacs menu-bar."
2973   (set var value)
2974   ;; It is the message that forces the active status to be updated.
2975   (message ""))
2976
2977 (defun gnus-make-score-map (type)
2978   "Make a summary score map of type TYPE."
2979   (if t
2980       nil
2981     (let ((headers '(("author" "from" string)
2982                      ("subject" "subject" string)
2983                      ("article body" "body" string)
2984                      ("article head" "head" string)
2985                      ("xref" "xref" string)
2986                      ("extra header" "extra" string)
2987                      ("lines" "lines" number)
2988                      ("followups to author" "followup" string)))
2989           (types '((number ("less than" <)
2990                            ("greater than" >)
2991                            ("equal" =))
2992                    (string ("substring" s)
2993                            ("exact string" e)
2994                            ("fuzzy string" f)
2995                            ("regexp" r))))
2996           (perms '(("temporary" (current-time-string))
2997                    ("permanent" nil)
2998                    ("immediate" now)))
2999           header)
3000       (list
3001        (apply
3002         'nconc
3003         (list
3004          (if (eq type 'lower)
3005              "Lower score"
3006            "Increase score"))
3007         (let (outh)
3008           (while headers
3009             (setq header (car headers))
3010             (setq outh
3011                   (cons
3012                    (apply
3013                     'nconc
3014                     (list (car header))
3015                     (let ((ts (cdr (assoc (nth 2 header) types)))
3016                           outt)
3017                       (while ts
3018                         (setq outt
3019                               (cons
3020                                (apply
3021                                 'nconc
3022                                 (list (caar ts))
3023                                 (let ((ps perms)
3024                                       outp)
3025                                   (while ps
3026                                     (setq outp
3027                                           (cons
3028                                            (vector
3029                                             (caar ps)
3030                                             (list
3031                                              'gnus-summary-score-entry
3032                                              (nth 1 header)
3033                                              (if (or (string= (nth 1 header)
3034                                                               "head")
3035                                                      (string= (nth 1 header)
3036                                                               "body"))
3037                                                  ""
3038                                                (list 'gnus-summary-header
3039                                                      (nth 1 header)))
3040                                              (list 'quote (nth 1 (car ts)))
3041                                              (list 'gnus-score-delta-default
3042                                                    nil)
3043                                              (nth 1 (car ps))
3044                                              t)
3045                                             t)
3046                                            outp))
3047                                     (setq ps (cdr ps)))
3048                                   (list (nreverse outp))))
3049                                outt))
3050                         (setq ts (cdr ts)))
3051                       (list (nreverse outt))))
3052                    outh))
3053             (setq headers (cdr headers)))
3054           (list (nreverse outh))))))))
3055
3056
3057 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3058 (defvar bookmark-make-record-function)
3059 \f
3060
3061 (defun gnus-summary-mode (&optional group)
3062   "Major mode for reading articles.
3063
3064 All normal editing commands are switched off.
3065 \\<gnus-summary-mode-map>
3066 Each line in this buffer represents one article.  To read an
3067 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3068 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3069 respectively.
3070
3071 You can also post articles and send mail from this buffer.  To
3072 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3073 of an article, type `\\[gnus-summary-reply]'.
3074
3075 There are approx. one gazillion commands you can execute in this
3076 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3077
3078 The following commands are available:
3079
3080 \\{gnus-summary-mode-map}"
3081   (interactive)
3082   (kill-all-local-variables)
3083   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3084     (gnus-summary-make-local-variables))
3085   (gnus-summary-make-local-variables)
3086   (setq gnus-newsgroup-name group)
3087   (when (gnus-visual-p 'summary-menu 'menu)
3088     (gnus-summary-make-menu-bar)
3089     (gnus-summary-make-tool-bar))
3090   (gnus-make-thread-indent-array)
3091   (gnus-simplify-mode-line)
3092   (setq major-mode 'gnus-summary-mode)
3093   (setq mode-name "Summary")
3094   (use-local-map gnus-summary-mode-map)
3095   (buffer-disable-undo)
3096   (setq buffer-read-only t              ;Disable modification
3097         show-trailing-whitespace nil)
3098   (setq truncate-lines t)
3099   (add-to-invisibility-spec '(gnus-sum . t))
3100   (gnus-summary-set-display-table)
3101   (gnus-set-default-directory)
3102   (make-local-variable 'gnus-summary-line-format)
3103   (make-local-variable 'gnus-summary-line-format-spec)
3104   (make-local-variable 'gnus-summary-dummy-line-format)
3105   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3106   (make-local-variable 'gnus-summary-mark-positions)
3107   (gnus-make-local-hook 'pre-command-hook)
3108   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3109   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3110   (turn-on-gnus-mailing-list-mode)
3111   (mm-enable-multibyte)
3112   (set (make-local-variable 'bookmark-make-record-function)
3113        'gnus-summary-bookmark-make-record)
3114   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3115   (gnus-update-summary-mark-positions))
3116
3117 (defun gnus-summary-make-local-variables ()
3118   "Make all the local summary buffer variables."
3119   (let (global)
3120     (dolist (local gnus-summary-local-variables)
3121       (if (consp local)
3122           (progn
3123             (if (eq (cdr local) 'global)
3124                 ;; Copy the global value of the variable.
3125                 (setq global (symbol-value (car local)))
3126               ;; Use the value from the list.
3127               (setq global (eval (cdr local))))
3128             (set (make-local-variable (car local)) global))
3129         ;; Simple nil-valued local variable.
3130         (set (make-local-variable local) nil)))))
3131
3132 ;; Summary data functions.
3133
3134 (defmacro gnus-data-number (data)
3135   `(car ,data))
3136
3137 (defmacro gnus-data-set-number (data number)
3138   `(setcar ,data ,number))
3139
3140 (defmacro gnus-data-mark (data)
3141   `(nth 1 ,data))
3142
3143 (defmacro gnus-data-set-mark (data mark)
3144   `(setcar (nthcdr 1 ,data) ,mark))
3145
3146 (defmacro gnus-data-pos (data)
3147   `(nth 2 ,data))
3148
3149 (defmacro gnus-data-set-pos (data pos)
3150   `(setcar (nthcdr 2 ,data) ,pos))
3151
3152 (defmacro gnus-data-header (data)
3153   `(nth 3 ,data))
3154
3155 (defmacro gnus-data-set-header (data header)
3156   `(setf (nth 3 ,data) ,header))
3157
3158 (defmacro gnus-data-level (data)
3159   `(nth 4 ,data))
3160
3161 (defmacro gnus-data-unread-p (data)
3162   `(= (nth 1 ,data) gnus-unread-mark))
3163
3164 (defmacro gnus-data-read-p (data)
3165   `(/= (nth 1 ,data) gnus-unread-mark))
3166
3167 (defmacro gnus-data-pseudo-p (data)
3168   `(consp (nth 3 ,data)))
3169
3170 (defmacro gnus-data-find (number)
3171   `(assq ,number gnus-newsgroup-data))
3172
3173 (defmacro gnus-data-find-list (number &optional data)
3174   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3175      (memq (assq ,number bdata)
3176            bdata)))
3177
3178 (defmacro gnus-data-make (number mark pos header level)
3179   `(list ,number ,mark ,pos ,header ,level))
3180
3181 (defun gnus-data-enter (after-article number mark pos header level offset)
3182   (let ((data (gnus-data-find-list after-article)))
3183     (unless data
3184       (error "No such article: %d" after-article))
3185     (setcdr data (cons (gnus-data-make number mark pos header level)
3186                        (cdr data)))
3187     (setq gnus-newsgroup-data-reverse nil)
3188     (gnus-data-update-list (cddr data) offset)))
3189
3190 (defun gnus-data-enter-list (after-article list &optional offset)
3191   (when list
3192     (let ((data (and after-article (gnus-data-find-list after-article)))
3193           (ilist list))
3194       (if (not (or data
3195                    after-article))
3196           (let ((odata gnus-newsgroup-data))
3197             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3198             (when offset
3199               (gnus-data-update-list odata offset)))
3200         ;; Find the last element in the list to be spliced into the main
3201         ;; list.
3202         (setq list (last list))
3203         (if (not data)
3204             (progn
3205               (setcdr list gnus-newsgroup-data)
3206               (setq gnus-newsgroup-data ilist)
3207               (when offset
3208                 (gnus-data-update-list (cdr list) offset)))
3209           (setcdr list (cdr data))
3210           (setcdr data ilist)
3211           (when offset
3212             (gnus-data-update-list (cdr list) offset))))
3213       (setq gnus-newsgroup-data-reverse nil))))
3214
3215 (defun gnus-data-remove (article &optional offset)
3216   (let ((data gnus-newsgroup-data))
3217     (if (= (gnus-data-number (car data)) article)
3218         (progn
3219           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3220                 gnus-newsgroup-data-reverse nil)
3221           (when offset
3222             (gnus-data-update-list gnus-newsgroup-data offset)))
3223       (while (cdr data)
3224         (when (= (gnus-data-number (cadr data)) article)
3225           (setcdr data (cddr data))
3226           (when offset
3227             (gnus-data-update-list (cdr data) offset))
3228           (setq data nil
3229                 gnus-newsgroup-data-reverse nil))
3230         (setq data (cdr data))))))
3231
3232 (defmacro gnus-data-list (backward)
3233   `(if ,backward
3234        (or gnus-newsgroup-data-reverse
3235            (setq gnus-newsgroup-data-reverse
3236                  (reverse gnus-newsgroup-data)))
3237      gnus-newsgroup-data))
3238
3239 (defun gnus-data-update-list (data offset)
3240   "Add OFFSET to the POS of all data entries in DATA."
3241   (setq gnus-newsgroup-data-reverse nil)
3242   (while data
3243     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3244     (setq data (cdr data))))
3245
3246 (defun gnus-summary-article-pseudo-p (article)
3247   "Say whether this article is a pseudo article or not."
3248   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3249
3250 (defmacro gnus-summary-article-sparse-p (article)
3251   "Say whether this article is a sparse article or not."
3252   `(memq ,article gnus-newsgroup-sparse))
3253
3254 (defmacro gnus-summary-article-ancient-p (article)
3255   "Say whether this article is a sparse article or not."
3256   `(memq ,article gnus-newsgroup-ancient))
3257
3258 (defun gnus-article-parent-p (number)
3259   "Say whether this article is a parent or not."
3260   (let ((data (gnus-data-find-list number)))
3261     (and (cdr data)              ; There has to be an article after...
3262          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3263             (gnus-data-level (nth 1 data))))))
3264
3265 (defun gnus-article-children (number)
3266   "Return a list of all children to NUMBER."
3267   (let* ((data (gnus-data-find-list number))
3268          (level (gnus-data-level (car data)))
3269          children)
3270     (setq data (cdr data))
3271     (while (and data
3272                 (= (gnus-data-level (car data)) (1+ level)))
3273       (push (gnus-data-number (car data)) children)
3274       (setq data (cdr data)))
3275     children))
3276
3277 (defmacro gnus-summary-skip-intangible ()
3278   "If the current article is intangible, then jump to a different article."
3279   '(let ((to (get-text-property (point) 'gnus-intangible)))
3280      (and to (gnus-summary-goto-subject to))))
3281
3282 (defmacro gnus-summary-article-intangible-p ()
3283   "Say whether this article is intangible or not."
3284   '(get-text-property (point) 'gnus-intangible))
3285
3286 (defun gnus-article-read-p (article)
3287   "Say whether ARTICLE is read or not."
3288   (not (or (memq article gnus-newsgroup-marked)
3289            (memq article gnus-newsgroup-spam-marked)
3290            (memq article gnus-newsgroup-unreads)
3291            (memq article gnus-newsgroup-unselected)
3292            (memq article gnus-newsgroup-dormant))))
3293
3294 ;; Some summary mode macros.
3295
3296 (defmacro gnus-summary-article-number ()
3297   "The article number of the article on the current line.
3298 If there isn't an article number here, then we return the current
3299 article number."
3300   '(progn
3301      (gnus-summary-skip-intangible)
3302      (or (get-text-property (point) 'gnus-number)
3303          (gnus-summary-last-subject))))
3304
3305 (defmacro gnus-summary-article-header (&optional number)
3306   "Return the header of article NUMBER."
3307   `(gnus-data-header (gnus-data-find
3308                       ,(or number '(gnus-summary-article-number)))))
3309
3310 (defmacro gnus-summary-thread-level (&optional number)
3311   "Return the level of thread that starts with article NUMBER."
3312   `(if (and (eq gnus-summary-make-false-root 'dummy)
3313             (get-text-property (point) 'gnus-intangible))
3314        0
3315      (gnus-data-level (gnus-data-find
3316                        ,(or number '(gnus-summary-article-number))))))
3317
3318 (defmacro gnus-summary-article-mark (&optional number)
3319   "Return the mark of article NUMBER."
3320   `(gnus-data-mark (gnus-data-find
3321                     ,(or number '(gnus-summary-article-number)))))
3322
3323 (defmacro gnus-summary-article-pos (&optional number)
3324   "Return the position of the line of article NUMBER."
3325   `(gnus-data-pos (gnus-data-find
3326                    ,(or number '(gnus-summary-article-number)))))
3327
3328 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3329 (defmacro gnus-summary-article-subject (&optional number)
3330   "Return current subject string or nil if nothing."
3331   `(let ((headers
3332           ,(if number
3333                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3334              '(gnus-data-header (assq (gnus-summary-article-number)
3335                                       gnus-newsgroup-data)))))
3336      (and headers
3337           (vectorp headers)
3338           (mail-header-subject headers))))
3339
3340 (defmacro gnus-summary-article-score (&optional number)
3341   "Return current article score."
3342   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3343                   gnus-newsgroup-scored))
3344        gnus-summary-default-score 0))
3345
3346 (defun gnus-summary-article-children (&optional number)
3347   "Return a list of article numbers that are children of article NUMBER."
3348   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3349          (level (gnus-data-level (car data)))
3350          l children)
3351     (while (and (setq data (cdr data))
3352                 (> (setq l (gnus-data-level (car data))) level))
3353       (and (= (1+ level) l)
3354            (push (gnus-data-number (car data))
3355                  children)))
3356     (nreverse children)))
3357
3358 (defun gnus-summary-article-parent (&optional number)
3359   "Return the article number of the parent of article NUMBER."
3360   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3361                                     (gnus-data-list t)))
3362          (level (gnus-data-level (car data))))
3363     (if (zerop level)
3364         ()                              ; This is a root.
3365       ;; We search until we find an article with a level less than
3366       ;; this one.  That function has to be the parent.
3367       (while (and (setq data (cdr data))
3368                   (not (< (gnus-data-level (car data)) level))))
3369       (and data (gnus-data-number (car data))))))
3370
3371 (defun gnus-unread-mark-p (mark)
3372   "Say whether MARK is the unread mark."
3373   (= mark gnus-unread-mark))
3374
3375 (defun gnus-read-mark-p (mark)
3376   "Say whether MARK is one of the marks that mark as read.
3377 This is all marks except unread, ticked, dormant, and expirable."
3378   (not (or (= mark gnus-unread-mark)
3379            (= mark gnus-ticked-mark)
3380            (= mark gnus-spam-mark)
3381            (= mark gnus-dormant-mark)
3382            (= mark gnus-expirable-mark))))
3383
3384 (defmacro gnus-article-mark (number)
3385   "Return the MARK of article NUMBER.
3386 This macro should only be used when computing the mark the \"first\"
3387 time; i.e., when generating the summary lines.  After that,
3388 `gnus-summary-article-mark' should be used to examine the
3389 marks of articles."
3390   `(cond
3391     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3392     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3393     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3394     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3395     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3396     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3397     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3398     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3399            gnus-ancient-mark))))
3400
3401 ;; Saving hidden threads.
3402
3403 (defmacro gnus-save-hidden-threads (&rest forms)
3404   "Save hidden threads, eval FORMS, and restore the hidden threads."
3405   (let ((config (make-symbol "config")))
3406     `(let ((,config (gnus-hidden-threads-configuration)))
3407        (unwind-protect
3408            (save-excursion
3409              ,@forms)
3410          (gnus-restore-hidden-threads-configuration ,config)))))
3411 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3412 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3413
3414 (defun gnus-data-compute-positions ()
3415   "Compute the positions of all articles."
3416   (setq gnus-newsgroup-data-reverse nil)
3417   (let ((data gnus-newsgroup-data))
3418     (save-excursion
3419       (gnus-save-hidden-threads
3420         (gnus-summary-show-all-threads)
3421         (goto-char (point-min))
3422         (while data
3423           (while (get-text-property (point) 'gnus-intangible)
3424             (forward-line 1))
3425           (gnus-data-set-pos (car data) (+ (point) 3))
3426           (setq data (cdr data))
3427           (forward-line 1))))))
3428
3429 (defun gnus-hidden-threads-configuration ()
3430   "Return the current hidden threads configuration."
3431   (save-excursion
3432     (let (config)
3433       (goto-char (point-min))
3434       (while (not (eobp))
3435         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3436           (push (save-excursion (forward-line 0) (point)) config))
3437         (forward-line 1))
3438       config)))
3439
3440 (defun gnus-restore-hidden-threads-configuration (config)
3441   "Restore hidden threads configuration from CONFIG."
3442   (save-excursion
3443     (let (point (inhibit-read-only t))
3444       (while (setq point (pop config))
3445         (goto-char point)
3446         (gnus-summary-hide-thread)))))
3447
3448 ;; Various summary mode internalish functions.
3449
3450 (defun gnus-mouse-pick-article (e)
3451   (interactive "e")
3452   (mouse-set-point e)
3453   (gnus-summary-next-page nil t))
3454
3455 (defun gnus-summary-set-display-table ()
3456   "Change the display table.
3457 Odd characters have a tendency to mess
3458 up nicely formatted displays - we make all possible glyphs
3459 display only a single character."
3460
3461   ;; We start from the standard display table, if any.
3462   (let ((table (or (copy-sequence standard-display-table)
3463                    (make-display-table)))
3464         (i 32))
3465     ;; Nix out all the control chars...
3466     (while (>= (setq i (1- i)) 0)
3467       (gnus-put-display-table i [??] table))
3468    ;; ... but not newline and cr, of course.  (cr is necessary for the
3469     ;; selective display).
3470     (gnus-put-display-table ?\n nil table)
3471     (gnus-put-display-table ?\r nil table)
3472     ;; We keep TAB as well.
3473     (gnus-put-display-table ?\t nil table)
3474     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3475     ;; Emacs 23 (unicode), that are not set already.
3476     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3477                  160
3478                256)))
3479       (while (>= (setq i (1- i)) 127)
3480         ;; Only modify if the entry is nil.
3481         (unless (gnus-get-display-table i table)
3482           (gnus-put-display-table i [??] table))))
3483     (setq buffer-display-table table)))
3484
3485 (defun gnus-summary-set-article-display-arrow (pos)
3486   "Update the overlay arrow to point to line at position POS."
3487   (when gnus-summary-display-arrow
3488     (make-local-variable 'overlay-arrow-position)
3489     (make-local-variable 'overlay-arrow-string)
3490     (save-excursion
3491       (goto-char pos)
3492       (beginning-of-line)
3493       (unless overlay-arrow-position
3494         (setq overlay-arrow-position (make-marker)))
3495       (setq overlay-arrow-string "=>"
3496             overlay-arrow-position (set-marker overlay-arrow-position
3497                                                (point)
3498                                                (current-buffer))))))
3499
3500 (defun gnus-summary-setup-buffer (group)
3501   "Initialize summary buffer."
3502   (let ((buffer (gnus-summary-buffer-name group))
3503         (dead-name (concat "*Dead Summary "
3504                            (gnus-group-decoded-name group) "*")))
3505     ;; If a dead summary buffer exists, we kill it.
3506     (when (gnus-buffer-live-p dead-name)
3507       (gnus-kill-buffer dead-name))
3508     (if (get-buffer buffer)
3509         (progn
3510           (set-buffer buffer)
3511           (setq gnus-summary-buffer (current-buffer))
3512           (not gnus-newsgroup-prepared))
3513       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3514       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3515       (gnus-summary-mode group)
3516       (when (gnus-group-quit-config group)
3517         (set (make-local-variable 'gnus-single-article-buffer) nil))
3518       (make-local-variable 'gnus-article-buffer)
3519       (make-local-variable 'gnus-article-current)
3520       (make-local-variable 'gnus-original-article-buffer)
3521       (setq gnus-newsgroup-name group)
3522       ;; Set any local variables in the group parameters.
3523       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3524       t)))
3525
3526 (defun gnus-set-global-variables ()
3527   "Set the global equivalents of the buffer-local variables.
3528 They are set to the latest values they had.  These reflect the summary
3529 buffer that was in action when the last article was fetched."
3530   (when (eq major-mode 'gnus-summary-mode)
3531     (setq gnus-summary-buffer (current-buffer))
3532     (let ((name gnus-newsgroup-name)
3533           (marked gnus-newsgroup-marked)
3534           (spam gnus-newsgroup-spam-marked)
3535           (unread gnus-newsgroup-unreads)
3536           (headers gnus-current-headers)
3537           (data gnus-newsgroup-data)
3538           (summary gnus-summary-buffer)
3539           (article-buffer gnus-article-buffer)
3540           (original gnus-original-article-buffer)
3541           (gac gnus-article-current)
3542           (reffed gnus-reffed-article-number)
3543           (score-file gnus-current-score-file)
3544           (default-charset gnus-newsgroup-charset)
3545           vlist)
3546       (let ((locals gnus-newsgroup-variables))
3547         (while locals
3548           (if (consp (car locals))
3549               (push (eval (caar locals)) vlist)
3550             (push (eval (car locals)) vlist))
3551           (setq locals (cdr locals)))
3552         (setq vlist (nreverse vlist)))
3553       (with-current-buffer gnus-group-buffer
3554         (setq gnus-newsgroup-name name
3555               gnus-newsgroup-marked marked
3556               gnus-newsgroup-spam-marked spam
3557               gnus-newsgroup-unreads unread
3558               gnus-current-headers headers
3559               gnus-newsgroup-data data
3560               gnus-article-current gac
3561               gnus-summary-buffer summary
3562               gnus-article-buffer article-buffer
3563               gnus-original-article-buffer original
3564               gnus-reffed-article-number reffed
3565               gnus-current-score-file score-file
3566               gnus-newsgroup-charset default-charset)
3567         (let ((locals gnus-newsgroup-variables))
3568           (while locals
3569             (if (consp (car locals))
3570                 (set (caar locals) (pop vlist))
3571               (set (car locals) (pop vlist)))
3572             (setq locals (cdr locals))))
3573         ;; The article buffer also has local variables.
3574         (when (gnus-buffer-live-p gnus-article-buffer)
3575           (set-buffer gnus-article-buffer)
3576           (setq gnus-summary-buffer summary))))))
3577
3578 (defun gnus-summary-article-unread-p (article)
3579   "Say whether ARTICLE is unread or not."
3580   (memq article gnus-newsgroup-unreads))
3581
3582 (defun gnus-summary-first-article-p (&optional article)
3583   "Return whether ARTICLE is the first article in the buffer."
3584   (if (not (setq article (or article (gnus-summary-article-number))))
3585       nil
3586     (eq article (caar gnus-newsgroup-data))))
3587
3588 (defun gnus-summary-last-article-p (&optional article)
3589   "Return whether ARTICLE is the last article in the buffer."
3590   (if (not (setq article (or article (gnus-summary-article-number))))
3591       ;; All non-existent numbers are the last article.  :-)
3592       t
3593     (not (cdr (gnus-data-find-list article)))))
3594
3595 (defun gnus-make-thread-indent-array (&optional n)
3596   (when (or n
3597             (progn (setq n 200) nil)
3598             (null gnus-thread-indent-array)
3599             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3600     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3601           gnus-thread-indent-array-level gnus-thread-indent-level)
3602     (while (>= n 0)
3603       (aset gnus-thread-indent-array n
3604             (make-string (* n gnus-thread-indent-level) ? ))
3605       (setq n (1- n)))))
3606
3607 (defun gnus-update-summary-mark-positions ()
3608   "Compute where the summary marks are to go."
3609   (save-excursion
3610     (when (gnus-buffer-exists-p gnus-summary-buffer)
3611       (set-buffer gnus-summary-buffer))
3612     (let ((spec gnus-summary-line-format-spec)
3613           pos)
3614       (save-excursion
3615         (gnus-set-work-buffer)
3616         (let ((gnus-tmp-unread ?Z)
3617               (gnus-replied-mark ?Z)
3618               (gnus-score-below-mark ?Z)
3619               (gnus-score-over-mark ?Z)
3620               (gnus-undownloaded-mark ?Z)
3621               (gnus-summary-line-format-spec spec)
3622               (gnus-newsgroup-downloadable '(0))
3623               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3624               case-fold-search ignores)
3625           ;; Here, all marks are bound to Z.
3626           (gnus-summary-insert-line header
3627                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3628           (goto-char (point-min))
3629           ;; Memorize the positions of the same characters as dummy marks.
3630           (while (re-search-forward "[A-D]" nil t)
3631             (push (point) ignores))
3632           (erase-buffer)
3633           ;; We use A-D as dummy marks in order to know column positions
3634           ;; where marks should be inserted.
3635           (setq gnus-tmp-unread ?A
3636                 gnus-replied-mark ?B
3637                 gnus-score-below-mark ?C
3638                 gnus-score-over-mark ?C
3639                 gnus-undownloaded-mark ?D)
3640           (gnus-summary-insert-line header
3641                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3642           ;; Ignore characters which aren't dummy marks.
3643           (dolist (p ignores)
3644             (delete-region (goto-char (1- p)) p)
3645             (insert ?Z))
3646           (goto-char (point-min))
3647           (setq pos (list (cons 'unread
3648                                 (and (search-forward "A" nil t)
3649                                      (- (point) (point-min) 1)))))
3650           (goto-char (point-min))
3651           (push (cons 'replied (and (search-forward "B" nil t)
3652                                     (- (point) (point-min) 1)))
3653                 pos)
3654           (goto-char (point-min))
3655           (push (cons 'score (and (search-forward "C" nil t)
3656                                   (- (point) (point-min) 1)))
3657                 pos)
3658           (goto-char (point-min))
3659           (push (cons 'download (and (search-forward "D" nil t)
3660                                      (- (point) (point-min) 1)))
3661                 pos)))
3662       (setq gnus-summary-mark-positions pos))))
3663
3664 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3665   "Insert a dummy root in the summary buffer."
3666   (beginning-of-line)
3667   (gnus-add-text-properties
3668    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3669    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3670
3671 (defun gnus-summary-extract-address-component (from)
3672   (or (car (funcall gnus-extract-address-components from))
3673       from))
3674
3675 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3676   (let ((mail-parse-charset gnus-newsgroup-charset)
3677         (ignored-from-addresses (gnus-ignored-from-addresses))
3678         ; Is it really necessary to do this next part for each summary line?
3679         ; Luckily, doesn't seem to slow things down much.
3680         (mail-parse-ignored-charsets
3681          (with-current-buffer gnus-summary-buffer
3682            gnus-newsgroup-ignored-charsets)))
3683     (or
3684      (and ignored-from-addresses
3685           (string-match ignored-from-addresses gnus-tmp-from)
3686           (let ((extra-headers (mail-header-extra header))
3687                 to
3688                 newsgroups)
3689             (cond
3690              ((setq to (cdr (assq 'To extra-headers)))
3691               (concat gnus-summary-to-prefix
3692                       (inline
3693                         (gnus-summary-extract-address-component
3694                          (funcall gnus-decode-encoded-address-function to)))))
3695              ((setq newsgroups
3696                     (or
3697                      (cdr (assq 'Newsgroups extra-headers))
3698                      (and
3699                       (memq 'Newsgroups gnus-extra-headers)
3700                       (eq (car (gnus-find-method-for-group
3701                                 gnus-newsgroup-name)) 'nntp)
3702                       (gnus-group-real-name gnus-newsgroup-name))))
3703               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3704      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3705
3706 (defun gnus-summary-insert-line (gnus-tmp-header
3707                                  gnus-tmp-level gnus-tmp-current
3708                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3709                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3710                                  &optional gnus-tmp-dummy gnus-tmp-score
3711                                  gnus-tmp-process)
3712   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3713       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3714                                           gnus-tmp-level)))
3715   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3716          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3717          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3718          (gnus-tmp-score-char
3719           (if (or (null gnus-summary-default-score)
3720                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3721                       gnus-summary-zcore-fuzz))
3722               ?                         ;Whitespace
3723             (if (< gnus-tmp-score gnus-summary-default-score)
3724                 gnus-score-below-mark gnus-score-over-mark)))
3725          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3726          (gnus-tmp-replied
3727           (cond (gnus-tmp-process gnus-process-mark)
3728                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3729                  gnus-cached-mark)
3730                 (gnus-tmp-replied gnus-replied-mark)
3731                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3732                  gnus-forwarded-mark)
3733                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3734                  gnus-saved-mark)
3735                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3736                  gnus-recent-mark)
3737                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3738                  gnus-unseen-mark)
3739                 (t gnus-no-mark)))
3740          (gnus-tmp-downloaded
3741           (cond (undownloaded
3742                  gnus-undownloaded-mark)
3743                 (gnus-newsgroup-agentized
3744                  gnus-downloaded-mark)
3745                 (t
3746                  gnus-no-mark)))
3747          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3748          (gnus-tmp-name
3749           (cond
3750            ((string-match "<[^>]+> *$" gnus-tmp-from)
3751             (let ((beg (match-beginning 0)))
3752               (or (and (string-match "^\".+\"" gnus-tmp-from)
3753                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3754                   (substring gnus-tmp-from 0 beg))))
3755            ((string-match "(.+)" gnus-tmp-from)
3756             (substring gnus-tmp-from
3757                        (1+ (match-beginning 0)) (1- (match-end 0))))
3758            (t gnus-tmp-from)))
3759          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3760          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3761          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3762          (inhibit-read-only t))
3763     (when (string= gnus-tmp-name "")
3764       (setq gnus-tmp-name gnus-tmp-from))
3765     (unless (numberp gnus-tmp-lines)
3766       (setq gnus-tmp-lines -1))
3767     (if (= gnus-tmp-lines -1)
3768         (setq gnus-tmp-lines "?")
3769       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3770     (condition-case ()
3771         (gnus-put-text-property
3772          (point)
3773          (progn (eval gnus-summary-line-format-spec) (point))
3774          'gnus-number gnus-tmp-number)
3775       (error (gnus-message 5 "Error updating the summary line")))
3776     (when (gnus-visual-p 'summary-highlight 'highlight)
3777       (forward-line -1)
3778       (gnus-summary-highlight-line)
3779       (gnus-run-hooks 'gnus-summary-update-hook)
3780       (forward-line 1))))
3781
3782 (defun gnus-summary-update-line (&optional dont-update)
3783   "Update summary line after change."
3784   (when (and gnus-summary-default-score
3785              (not gnus-summary-inhibit-highlight))
3786     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3787            (article (gnus-summary-article-number))
3788            (score (gnus-summary-article-score article)))
3789       (unless dont-update
3790         (if (and gnus-summary-mark-below
3791                  (< (gnus-summary-article-score)
3792                     gnus-summary-mark-below))
3793             ;; This article has a low score, so we mark it as read.
3794             (when (memq article gnus-newsgroup-unreads)
3795               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3796           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3797             ;; This article was previously marked as read on account
3798             ;; of a low score, but now it has risen, so we mark it as
3799             ;; unread.
3800             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3801         (gnus-summary-update-mark
3802          (if (or (null gnus-summary-default-score)
3803                  (<= (abs (- score gnus-summary-default-score))
3804                      gnus-summary-zcore-fuzz))
3805              ?                          ;Whitespace
3806            (if (< score gnus-summary-default-score)
3807                gnus-score-below-mark gnus-score-over-mark))
3808          'score))
3809       ;; Do visual highlighting.
3810       (when (gnus-visual-p 'summary-highlight 'highlight)
3811         (gnus-summary-highlight-line)
3812         (gnus-run-hooks 'gnus-summary-update-hook)))))
3813
3814 (defvar gnus-tmp-new-adopts nil)
3815
3816 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3817   "Return the number of articles in THREAD.
3818 This may be 0 in some cases -- if none of the articles in
3819 the thread are to be displayed."
3820   (let* ((number
3821          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3822           (cond
3823            ((not (listp thread))
3824             1)
3825            ((and (consp thread) (cdr thread))
3826             (apply
3827              '+ 1 (mapcar
3828                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3829            ((null thread)
3830             1)
3831            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3832             1)
3833            (t 0))))
3834     (when (and level (zerop level) gnus-tmp-new-adopts)
3835       (incf number
3836             (apply '+ (mapcar
3837                        'gnus-summary-number-of-articles-in-thread
3838                        gnus-tmp-new-adopts))))
3839     (if char
3840         (if (> number 1) gnus-not-empty-thread-mark
3841           gnus-empty-thread-mark)
3842       number)))
3843
3844 (defsubst gnus-summary-line-message-size (head)
3845   "Return pretty-printed version of message size.
3846 This function is intended to be used in
3847 `gnus-summary-line-format-alist'."
3848   (let ((c (or (mail-header-chars head) -1)))
3849     (cond ((< c 0) "n/a")               ; chars not available
3850           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3851           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3852           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3853           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3854
3855
3856 (defun gnus-summary-set-local-parameters (group)
3857   "Go through the local params of GROUP and set all variable specs in that list."
3858   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3859                                         ; really variables.
3860     (dolist (elem (gnus-group-find-parameter group))
3861       (and (consp elem)                 ; Has to be a cons.
3862            (consp (cdr elem))           ; The cdr has to be a list.
3863            (symbolp (car elem))         ; Has to be a symbol in there.
3864            (not (memq (car elem) vars))
3865            (ignore-errors
3866              (push (car elem) vars)
3867              ;; Variables like `gnus-show-threads' that are globally
3868              ;; bound, if used as group parameters, need to get to be
3869              ;; buffer-local, whereas just parameters like `gcc-self',
3870              ;; `timestamp', etc. should not be bound as variables.
3871              (if (boundp (car elem))
3872                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3873                (eval (nth 1 elem))))))))
3874
3875 (defun gnus-summary-read-group (group &optional show-all no-article
3876                                       kill-buffer no-display backward
3877                                       select-articles)
3878   "Start reading news in newsgroup GROUP.
3879 If SHOW-ALL is non-nil, already read articles are also listed.
3880 If NO-ARTICLE is non-nil, no article is selected initially.
3881 If NO-DISPLAY, don't generate a summary buffer."
3882   (let (result)
3883     (while (and group
3884                 (null (setq result
3885                             (let ((gnus-auto-select-next nil))
3886                               (or (gnus-summary-read-group-1
3887                                    group show-all no-article
3888                                    kill-buffer no-display
3889                                    select-articles)
3890                                   (setq show-all nil
3891                                         select-articles nil)))))
3892                 (eq gnus-auto-select-next 'quietly))
3893       (set-buffer gnus-group-buffer)
3894       ;; The entry function called above goes to the next
3895       ;; group automatically, so we go two groups back
3896       ;; if we are searching for the previous group.
3897       (when backward
3898         (gnus-group-prev-unread-group 2))
3899       (if (not (equal group (gnus-group-group-name)))
3900           (setq group (gnus-group-group-name))
3901         (setq group nil)))
3902     result))
3903
3904 (defun gnus-summary-read-group-1 (group show-all no-article
3905                                         kill-buffer no-display
3906                                         &optional select-articles)
3907   ;; Killed foreign groups can't be entered.
3908   ;;  (when (and (not (gnus-group-native-p group))
3909   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3910   ;;    (error "Dead non-native groups can't be entered"))
3911   (gnus-message 5 "Retrieving newsgroup: %s..."
3912                 (gnus-group-decoded-name group))
3913   (let* ((new-group (gnus-summary-setup-buffer group))
3914          (quit-config (gnus-group-quit-config group))
3915          (did-select (and new-group (gnus-select-newsgroup
3916                                      group show-all select-articles))))
3917     (cond
3918      ;; This summary buffer exists already, so we just select it.
3919      ((not new-group)
3920       (gnus-set-global-variables)
3921       (when kill-buffer
3922         (gnus-kill-or-deaden-summary kill-buffer))
3923       (gnus-configure-windows 'summary 'force)
3924       (gnus-set-mode-line 'summary)
3925       (gnus-summary-position-point)
3926       (message "")
3927       t)
3928      ;; We couldn't select this group.
3929      ((null did-select)
3930       (when (and (eq major-mode 'gnus-summary-mode)
3931                  (not (equal (current-buffer) kill-buffer)))
3932         (kill-buffer (current-buffer))
3933         (if (not quit-config)
3934             (progn
3935               ;; Update the info -- marks might need to be removed,
3936               ;; for instance.
3937               (gnus-summary-update-info)
3938               (set-buffer gnus-group-buffer)
3939               (gnus-group-jump-to-group group)
3940               (gnus-group-next-unread-group 1))
3941           (gnus-handle-ephemeral-exit quit-config)))
3942       (let ((grpinfo (gnus-get-info group)))
3943         (if (null (gnus-info-read grpinfo))
3944             (gnus-message 3 "Group %s contains no messages"
3945                           (gnus-group-decoded-name group))
3946           (gnus-message 3 "Can't select group")))
3947       nil)
3948      ;; The user did a `C-g' while prompting for number of articles,
3949      ;; so we exit this group.
3950      ((eq did-select 'quit)
3951       (and (eq major-mode 'gnus-summary-mode)
3952            (not (equal (current-buffer) kill-buffer))
3953            (kill-buffer (current-buffer)))
3954       (when kill-buffer
3955         (gnus-kill-or-deaden-summary kill-buffer))
3956       (if (not quit-config)
3957           (progn
3958             (set-buffer gnus-group-buffer)
3959             (gnus-group-jump-to-group group)
3960             (gnus-configure-windows 'group 'force))
3961         (gnus-handle-ephemeral-exit quit-config))
3962       ;; Finally signal the quit.
3963       (signal 'quit nil))
3964      ;; The group was successfully selected.
3965      (t
3966       (gnus-set-global-variables)
3967       ;; Save the active value in effect when the group was entered.
3968       (setq gnus-newsgroup-active
3969             (gnus-copy-sequence
3970              (gnus-active gnus-newsgroup-name)))
3971       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
3972       ;; You can change the summary buffer in some way with this hook.
3973       (gnus-run-hooks 'gnus-select-group-hook)
3974       (when (memq 'summary (gnus-update-format-specifications
3975                             nil 'summary 'summary-mode 'summary-dummy))
3976         ;; The format specification for the summary line was updated,
3977         ;; so we need to update the mark positions as well.
3978         (gnus-update-summary-mark-positions))
3979       ;; Do score processing.
3980       (when gnus-use-scoring
3981         (gnus-possibly-score-headers))
3982       ;; Check whether to fill in the gaps in the threads.
3983       (when gnus-build-sparse-threads
3984         (gnus-build-sparse-threads))
3985       ;; Find the initial limit.
3986       (if show-all
3987           (let ((gnus-newsgroup-dormant nil))
3988             (gnus-summary-initial-limit show-all))
3989         (gnus-summary-initial-limit show-all))
3990       ;; Generate the summary buffer.
3991       (unless no-display
3992         (gnus-summary-prepare))
3993       (when gnus-use-trees
3994         (gnus-tree-open group)
3995         (setq gnus-summary-highlight-line-function
3996               'gnus-tree-highlight-article))
3997       ;; If the summary buffer is empty, but there are some low-scored
3998       ;; articles or some excluded dormants, we include these in the
3999       ;; buffer.
4000       (when (and (zerop (buffer-size))
4001                  (not no-display))
4002         (cond (gnus-newsgroup-dormant
4003                (gnus-summary-limit-include-dormant))
4004               ((and gnus-newsgroup-scored show-all)
4005                (gnus-summary-limit-include-expunged t))))
4006       ;; Function `gnus-apply-kill-file' must be called in this hook.
4007       (gnus-run-hooks 'gnus-apply-kill-hook)
4008       (if (and (zerop (buffer-size))
4009                (not no-display))
4010           (progn
4011             ;; This newsgroup is empty.
4012             (gnus-summary-catchup-and-exit nil t)
4013             (gnus-message 6 "No unread news")
4014             (when kill-buffer
4015               (gnus-kill-or-deaden-summary kill-buffer))
4016             ;; Return nil from this function.
4017             nil)
4018         ;; Hide conversation thread subtrees.  We cannot do this in
4019         ;; gnus-summary-prepare-hook since kill processing may not
4020         ;; work with hidden articles.
4021         (gnus-summary-maybe-hide-threads)
4022         (when kill-buffer
4023           (gnus-kill-or-deaden-summary kill-buffer))
4024         (gnus-summary-auto-select-subject)
4025         ;; Show first unread article if requested.
4026         (if (and (not no-article)
4027                  (not no-display)
4028                  gnus-newsgroup-unreads
4029                  gnus-auto-select-first)
4030             (progn
4031               (gnus-configure-windows 'summary)
4032               (let ((art (gnus-summary-article-number)))
4033                 (unless (and (not gnus-plugged)
4034                              (or (memq art gnus-newsgroup-undownloaded)
4035                                  (memq art gnus-newsgroup-downloadable)))
4036                   (gnus-summary-goto-article art))))
4037           ;; Don't select any articles.
4038           (gnus-summary-position-point)
4039           (gnus-configure-windows 'summary 'force)
4040           (gnus-set-mode-line 'summary))
4041         (when (and gnus-auto-center-group
4042                    (get-buffer-window gnus-group-buffer t))
4043           ;; Gotta use windows, because recenter does weird stuff if
4044           ;; the current buffer ain't the displayed window.
4045           (let ((owin (selected-window)))
4046             (select-window (get-buffer-window gnus-group-buffer t))
4047             (when (gnus-group-goto-group group)
4048               (recenter))
4049             (select-window owin)))
4050         ;; Mark this buffer as "prepared".
4051         (setq gnus-newsgroup-prepared t)
4052         (gnus-run-hooks 'gnus-summary-prepared-hook)
4053         (unless (gnus-ephemeral-group-p group)
4054           (gnus-group-update-group group))
4055         t)))))
4056
4057 (defun gnus-summary-auto-select-subject ()
4058   "Select the subject line on initial group entry."
4059   (goto-char (point-min))
4060   (cond
4061    ((eq gnus-auto-select-subject 'best)
4062     (gnus-summary-best-unread-subject))
4063    ((eq gnus-auto-select-subject 'unread)
4064     (gnus-summary-first-unread-subject))
4065    ((eq gnus-auto-select-subject 'unseen)
4066     (gnus-summary-first-unseen-subject))
4067    ((eq gnus-auto-select-subject 'unseen-or-unread)
4068     (gnus-summary-first-unseen-or-unread-subject))
4069    ((eq gnus-auto-select-subject 'first)
4070     ;; Do nothing.
4071     )
4072    ((functionp gnus-auto-select-subject)
4073     (funcall gnus-auto-select-subject))))
4074
4075 (defun gnus-summary-prepare ()
4076   "Generate the summary buffer."
4077   (interactive)
4078   (let ((inhibit-read-only t))
4079     (erase-buffer)
4080     (setq gnus-newsgroup-data nil
4081           gnus-newsgroup-data-reverse nil)
4082     (gnus-run-hooks 'gnus-summary-generate-hook)
4083     ;; Generate the buffer, either with threads or without.
4084     (when gnus-newsgroup-headers
4085       (gnus-summary-prepare-threads
4086        (if gnus-show-threads
4087            (gnus-sort-gathered-threads
4088             (funcall gnus-summary-thread-gathering-function
4089                      (gnus-sort-threads
4090                       (gnus-cut-threads (gnus-make-threads)))))
4091          ;; Unthreaded display.
4092          (gnus-sort-articles gnus-newsgroup-headers))))
4093     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4094     ;; Call hooks for modifying summary buffer.
4095     (goto-char (point-min))
4096     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4097
4098 (defsubst gnus-general-simplify-subject (subject)
4099   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4100   (setq subject
4101         (cond
4102          ;; Truncate the subject.
4103          (gnus-simplify-subject-functions
4104           (gnus-map-function gnus-simplify-subject-functions subject))
4105          ((numberp gnus-summary-gather-subject-limit)
4106           (setq subject (gnus-simplify-subject-re subject))
4107           (if (> (length subject) gnus-summary-gather-subject-limit)
4108               (substring subject 0 gnus-summary-gather-subject-limit)
4109             subject))
4110          ;; Fuzzily simplify it.
4111          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4112           (gnus-simplify-subject-fuzzy subject))
4113          ;; Just remove the leading "Re:".
4114          (t
4115           (gnus-simplify-subject-re subject))))
4116
4117   (if (and gnus-summary-gather-exclude-subject
4118            (string-match gnus-summary-gather-exclude-subject subject))
4119       nil                         ; This article shouldn't be gathered
4120     subject))
4121
4122 (defun gnus-summary-simplify-subject-query ()
4123   "Query where the respool algorithm would put this article."
4124   (interactive)
4125   (gnus-summary-select-article)
4126   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4127
4128 (defun gnus-gather-threads-by-subject (threads)
4129   "Gather threads by looking at Subject headers."
4130   (if (not gnus-summary-make-false-root)
4131       threads
4132     (let ((hashtb (gnus-make-hashtable 1024))
4133           (prev threads)
4134           (result threads)
4135           subject hthread whole-subject)
4136       (while threads
4137         (setq subject (gnus-general-simplify-subject
4138                        (setq whole-subject (mail-header-subject
4139                                             (caar threads)))))
4140         (when subject
4141           (if (setq hthread (gnus-gethash subject hashtb))
4142               (progn
4143                 ;; We enter a dummy root into the thread, if we
4144                 ;; haven't done that already.
4145                 (unless (stringp (caar hthread))
4146                   (setcar hthread (list whole-subject (car hthread))))
4147                 ;; We add this new gathered thread to this gathered
4148                 ;; thread.
4149                 (setcdr (car hthread)
4150                         (nconc (cdar hthread) (list (car threads))))
4151                 ;; Remove it from the list of threads.
4152                 (setcdr prev (cdr threads))
4153                 (setq threads prev))
4154             ;; Enter this thread into the hash table.
4155             (gnus-sethash subject
4156                           (if gnus-summary-make-false-root-always
4157                               (progn
4158                                 ;; If you want a dummy root above all
4159                                 ;; threads...
4160                                 (setcar threads (list whole-subject
4161                                                       (car threads)))
4162                                 threads)
4163                             threads)
4164                           hashtb)))
4165         (setq prev threads)
4166         (setq threads (cdr threads)))
4167       result)))
4168
4169 (defun gnus-gather-threads-by-references (threads)
4170   "Gather threads by looking at References headers."
4171   (let ((idhashtb (gnus-make-hashtable 1024))
4172         (thhashtb (gnus-make-hashtable 1024))
4173         (prev threads)
4174         (result threads)
4175         ids references id gthread gid entered ref)
4176     (while threads
4177       (when (setq references (mail-header-references (caar threads)))
4178         (setq id (mail-header-id (caar threads))
4179               ids (inline (gnus-split-references references))
4180               entered nil)
4181         (while (setq ref (pop ids))
4182           (setq ids (delete ref ids))
4183           (if (not (setq gid (gnus-gethash ref idhashtb)))
4184               (progn
4185                 (gnus-sethash ref id idhashtb)
4186                 (gnus-sethash id threads thhashtb))
4187             (setq gthread (gnus-gethash gid thhashtb))
4188             (unless entered
4189               ;; We enter a dummy root into the thread, if we
4190               ;; haven't done that already.
4191               (unless (stringp (caar gthread))
4192                 (setcar gthread (list (mail-header-subject (caar gthread))
4193                                       (car gthread))))
4194               ;; We add this new gathered thread to this gathered
4195               ;; thread.
4196               (setcdr (car gthread)
4197                       (nconc (cdar gthread) (list (car threads)))))
4198             ;; Add it into the thread hash table.
4199             (gnus-sethash id gthread thhashtb)
4200             (setq entered t)
4201             ;; Remove it from the list of threads.
4202             (setcdr prev (cdr threads))
4203             (setq threads prev))))
4204       (setq prev threads)
4205       (setq threads (cdr threads)))
4206     result))
4207
4208 (defun gnus-sort-gathered-threads (threads)
4209   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4210   (let ((result threads))
4211     (while threads
4212       (when (stringp (caar threads))
4213         (setcdr (car threads)
4214                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4215       (setq threads (cdr threads)))
4216     result))
4217
4218 (defun gnus-thread-loop-p (root thread)
4219   "Say whether ROOT is in THREAD."
4220   (let ((stack (list thread))
4221         (infloop 0)
4222         th)
4223     (while (setq thread (pop stack))
4224       (setq th (cdr thread))
4225       (while (and th
4226                   (not (eq (caar th) root)))
4227         (pop th))
4228       (if th
4229           ;; We have found a loop.
4230           (let (ref-dep)
4231             (setcdr thread (delq (car th) (cdr thread)))
4232             (if (boundp (setq ref-dep (intern "none"
4233                                               gnus-newsgroup-dependencies)))
4234                 (setcdr (symbol-value ref-dep)
4235                         (nconc (cdr (symbol-value ref-dep))
4236                                (list (car th))))
4237               (set ref-dep (list nil (car th))))
4238             (setq infloop 1
4239                   stack nil))
4240         ;; Push all the subthreads onto the stack.
4241         (push (cdr thread) stack)))
4242     infloop))
4243
4244 (defun gnus-make-threads ()
4245   "Go through the dependency hashtb and find the roots.  Return all threads."
4246   (let (threads)
4247     (while (catch 'infloop
4248              (mapatoms
4249               (lambda (refs)
4250                 ;; Deal with self-referencing References loops.
4251                 (when (and (car (symbol-value refs))
4252                            (not (zerop
4253                                  (apply
4254                                   '+
4255                                   (mapcar
4256                                    (lambda (thread)
4257                                      (gnus-thread-loop-p
4258                                       (car (symbol-value refs)) thread))
4259                                    (cdr (symbol-value refs)))))))
4260                   (setq threads nil)
4261                   (throw 'infloop t))
4262                 (unless (car (symbol-value refs))
4263                   ;; These threads do not refer back to any other
4264                   ;; articles, so they're roots.
4265                   (setq threads (append (cdr (symbol-value refs)) threads))))
4266               gnus-newsgroup-dependencies)))
4267     threads))
4268
4269 ;; Build the thread tree.
4270 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4271   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4272
4273 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4274 if it was already present.
4275
4276 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4277 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4278 Message-IDs will be renamed to a unique Message-ID before being
4279 entered.
4280
4281 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4282   (let* ((id (mail-header-id header))
4283          (id-dep (and id (intern id dependencies)))
4284          parent-id ref ref-dep ref-header replaced)
4285     ;; Enter this `header' in the `dependencies' table.
4286     (cond
4287      ((not id-dep)
4288       (setq header nil))
4289      ;; The first two cases do the normal part: enter a new `header'
4290      ;; in the `dependencies' table.
4291      ((not (boundp id-dep))
4292       (set id-dep (list header)))
4293      ((null (car (symbol-value id-dep)))
4294       (setcar (symbol-value id-dep) header))
4295
4296      ;; From here the `header' was already present in the
4297      ;; `dependencies' table.
4298      (force-new
4299       ;; Overrides an existing entry;
4300       ;; just set the header part of the entry.
4301       (setcar (symbol-value id-dep) header)
4302       (setq replaced t))
4303
4304      ;; Renames the existing `header' to a unique Message-ID.
4305      ((not gnus-summary-ignore-duplicates)
4306       ;; An article with this Message-ID has already been seen.
4307       ;; We rename the Message-ID.
4308       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4309            (list header))
4310       (mail-header-set-id header id))
4311
4312      ;; The last case ignores an existing entry, except it adds any
4313      ;; additional Xrefs (in case the two articles came from different
4314      ;; servers.
4315      ;; Also sets `header' to `nil' meaning that the `dependencies'
4316      ;; table was *not* modified.
4317      (t
4318       (mail-header-set-xref
4319        (car (symbol-value id-dep))
4320        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4321                    "")
4322                (or (mail-header-xref header) "")))
4323       (setq header nil)))
4324
4325     (when (and header (not replaced))
4326       ;; First check that we are not creating a References loop.
4327       (setq parent-id (gnus-parent-id (mail-header-references header)))
4328       (setq ref parent-id)
4329       (while (and ref
4330                   (setq ref-dep (intern-soft ref dependencies))
4331                   (boundp ref-dep)
4332                   (setq ref-header (car (symbol-value ref-dep))))
4333         (if (string= id ref)
4334             ;; Yuk!  This is a reference loop.  Make the article be a
4335             ;; root article.
4336             (progn
4337               (mail-header-set-references (car (symbol-value id-dep)) "none")
4338               (setq ref nil)
4339               (setq parent-id nil))
4340           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4341       (setq ref-dep (intern (or parent-id "none") dependencies))
4342       (if (boundp ref-dep)
4343           (setcdr (symbol-value ref-dep)
4344                   (nconc (cdr (symbol-value ref-dep))
4345                          (list (symbol-value id-dep))))
4346         (set ref-dep (list nil (symbol-value id-dep)))))
4347     header))
4348
4349 (defun gnus-extract-message-id-from-in-reply-to (string)
4350   (if (string-match "<[^>]+>" string)
4351       (substring string (match-beginning 0) (match-end 0))
4352     nil))
4353
4354 (defun gnus-build-sparse-threads ()
4355   (let ((headers gnus-newsgroup-headers)
4356         (mail-parse-charset gnus-newsgroup-charset)
4357         (gnus-summary-ignore-duplicates t)
4358         header references generation relations
4359         subject child end new-child date)
4360     ;; First we create an alist of generations/relations, where
4361     ;; generations is how much we trust the relation, and the relation
4362     ;; is parent/child.
4363     (gnus-message 7 "Making sparse threads...")
4364     (save-excursion
4365       (nnheader-set-temp-buffer " *gnus sparse threads*")
4366       (while (setq header (pop headers))
4367         (when (and (setq references (mail-header-references header))
4368                    (not (string= references "")))
4369           (insert references)
4370           (setq child (mail-header-id header)
4371                 subject (mail-header-subject header)
4372                 date (mail-header-date header)
4373                 generation 0)
4374           (while (search-backward ">" nil t)
4375             (setq end (1+ (point)))
4376             (when (search-backward "<" nil t)
4377               (setq new-child (buffer-substring (point) end))
4378               (push (list (incf generation)
4379                           child (setq child new-child)
4380                           subject date)
4381                     relations)))
4382           (when child
4383             (push (list (1+ generation) child nil subject) relations))
4384           (erase-buffer)))
4385       (kill-buffer (current-buffer)))
4386     ;; Sort over trustworthiness.
4387     (dolist (relation (sort relations 'car-less-than-car))
4388       (when (gnus-dependencies-add-header
4389              (make-full-mail-header
4390               gnus-reffed-article-number
4391               (nth 3 relation) "" (or (nth 4 relation) "")
4392               (nth 1 relation)
4393               (or (nth 2 relation) "") 0 0 "")
4394              gnus-newsgroup-dependencies nil)
4395         (push gnus-reffed-article-number gnus-newsgroup-limit)
4396         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4397         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4398               gnus-newsgroup-reads)
4399         (decf gnus-reffed-article-number)))
4400     (gnus-message 7 "Making sparse threads...done")))
4401
4402 (defun gnus-build-old-threads ()
4403   ;; Look at all the articles that refer back to old articles, and
4404   ;; fetch the headers for the articles that aren't there.  This will
4405   ;; build complete threads - if the roots haven't been expired by the
4406   ;; server, that is.
4407   (let ((mail-parse-charset gnus-newsgroup-charset)
4408         id heads)
4409     (mapatoms
4410      (lambda (refs)
4411        (when (not (car (symbol-value refs)))
4412          (setq heads (cdr (symbol-value refs)))
4413          (while heads
4414            (if (memq (mail-header-number (caar heads))
4415                      gnus-newsgroup-dormant)
4416                (setq heads (cdr heads))
4417              (setq id (symbol-name refs))
4418              (while (and (setq id (gnus-build-get-header id))
4419                          (not (car (gnus-id-to-thread id)))))
4420              (setq heads nil)))))
4421      gnus-newsgroup-dependencies)))
4422
4423 (defsubst gnus-remove-odd-characters (string)
4424   "Translate STRING into something that doesn't contain weird characters."
4425   (mm-subst-char-in-string
4426    ?\r ?\-
4427    (mm-subst-char-in-string ?\n ?\- string t) t))
4428
4429 ;; This function has to be called with point after the article number
4430 ;; on the beginning of the line.
4431 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4432   (let ((eol (point-at-eol))
4433         (buffer (current-buffer))
4434         header references in-reply-to)
4435
4436     ;; overview: [num subject from date id refs chars lines misc]
4437     (unwind-protect
4438         (let (x)
4439           (narrow-to-region (point) eol)
4440           (unless (eobp)
4441             (forward-char))
4442
4443           (setq header
4444                 (make-full-mail-header
4445                  number                 ; number
4446                  (condition-case ()     ; subject
4447                      (gnus-remove-odd-characters
4448                       (funcall gnus-decode-encoded-word-function
4449                                (setq x (nnheader-nov-field))))
4450                    (error x))
4451                  (condition-case ()     ; from
4452                      (gnus-remove-odd-characters
4453                       (funcall gnus-decode-encoded-address-function
4454                                (setq x (nnheader-nov-field))))
4455                    (error x))
4456                  (nnheader-nov-field)   ; date
4457                  (nnheader-nov-read-message-id number)  ; id
4458                  (setq references (nnheader-nov-field)) ; refs
4459                  (nnheader-nov-read-integer) ; chars
4460                  (nnheader-nov-read-integer) ; lines
4461                  (unless (eobp)
4462                    (if (looking-at "Xref: ")
4463                        (goto-char (match-end 0)))
4464                    (nnheader-nov-field)) ; Xref
4465                  (nnheader-nov-parse-extra)))) ; extra
4466
4467       (widen))
4468
4469     (when (and (string= references "")
4470                (setq in-reply-to (mail-header-extra header))
4471                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4472       (mail-header-set-references
4473        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4474
4475     (when gnus-alter-header-function
4476       (funcall gnus-alter-header-function header))
4477     (gnus-dependencies-add-header header dependencies force-new)))
4478
4479 (defun gnus-build-get-header (id)
4480   "Look through the buffer of NOV lines and find the header to ID.
4481 Enter this line into the dependencies hash table, and return
4482 the id of the parent article (if any)."
4483   (let ((deps gnus-newsgroup-dependencies)
4484         found header)
4485     (prog1
4486         (with-current-buffer nntp-server-buffer
4487           (let ((case-fold-search nil))
4488             (goto-char (point-min))
4489             (while (and (not found)
4490                         (search-forward id nil t))
4491               (beginning-of-line)
4492               (setq found (looking-at
4493                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4494                                    (regexp-quote id))))
4495               (or found (beginning-of-line 2)))
4496             (when found
4497               (beginning-of-line)
4498               (and
4499                (setq header (gnus-nov-parse-line
4500                              (read (current-buffer)) deps))
4501                (gnus-parent-id (mail-header-references header))))))
4502       (when header
4503         (let ((number (mail-header-number header)))
4504           (push number gnus-newsgroup-limit)
4505           (push header gnus-newsgroup-headers)
4506           (if (memq number gnus-newsgroup-unselected)
4507               (progn
4508                 (setq gnus-newsgroup-unreads
4509                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4510                                                number))
4511                 (setq gnus-newsgroup-unselected
4512                       (delq number gnus-newsgroup-unselected)))
4513             (push number gnus-newsgroup-ancient)))))))
4514
4515 (defun gnus-build-all-threads ()
4516   "Read all the headers."
4517   (let ((gnus-summary-ignore-duplicates t)
4518         (mail-parse-charset gnus-newsgroup-charset)
4519         (dependencies gnus-newsgroup-dependencies)
4520         header article)
4521     (with-current-buffer nntp-server-buffer
4522       (let ((case-fold-search nil))
4523         (goto-char (point-min))
4524         (while (not (eobp))
4525           (ignore-errors
4526             (setq article (read (current-buffer))
4527                   header (gnus-nov-parse-line article dependencies t)))
4528           (when header
4529             (with-current-buffer gnus-summary-buffer
4530               (push header gnus-newsgroup-headers)
4531               (if (memq (setq article (mail-header-number header))
4532                         gnus-newsgroup-unselected)
4533                   (progn
4534                     (setq gnus-newsgroup-unreads
4535                           (gnus-add-to-sorted-list
4536                            gnus-newsgroup-unreads article))
4537                     (setq gnus-newsgroup-unselected
4538                           (delq article gnus-newsgroup-unselected)))
4539                 (push article gnus-newsgroup-ancient)))
4540             (forward-line 1)))))))
4541
4542 (defun gnus-summary-update-article-line (article header)
4543   "Update the line for ARTICLE using HEADER."
4544   (let* ((id (mail-header-id header))
4545          (thread (gnus-id-to-thread id)))
4546     (unless thread
4547       (error "Article in no thread"))
4548     ;; Update the thread.
4549     (setcar thread header)
4550     (gnus-summary-goto-subject article)
4551     (let* ((datal (gnus-data-find-list article))
4552            (data (car datal))
4553            (inhibit-read-only t)
4554            (level (gnus-summary-thread-level)))
4555       (gnus-delete-line)
4556       (let ((inserted (- (point)
4557                          (progn
4558                            (gnus-summary-insert-line
4559                             header level nil
4560                             (memq article gnus-newsgroup-undownloaded)
4561                             (gnus-article-mark article)
4562                             (memq article gnus-newsgroup-replied)
4563                             (memq article gnus-newsgroup-expirable)
4564                             ;; Only insert the Subject string when it's different
4565                             ;; from the previous Subject string.
4566                             (if (and
4567                                  gnus-show-threads
4568                                  (gnus-subject-equal
4569                                   (condition-case ()
4570                                       (mail-header-subject
4571                                        (gnus-data-header
4572                                         (cadr
4573                                          (gnus-data-find-list
4574                                           article
4575                                           (gnus-data-list t)))))
4576                                     ;; Error on the side of excessive subjects.
4577                                     (error ""))
4578                                   (mail-header-subject header)))
4579                                 ""
4580                               (mail-header-subject header))
4581                             nil (cdr (assq article gnus-newsgroup-scored))
4582                             (memq article gnus-newsgroup-processable))
4583                            (point)))))
4584         (when (cdr datal)
4585           (gnus-data-update-list
4586            (cdr datal)
4587            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4588
4589 (defun gnus-summary-update-article (article &optional iheader)
4590   "Update ARTICLE in the summary buffer."
4591   (set-buffer gnus-summary-buffer)
4592   (let* ((header (gnus-summary-article-header article))
4593          (id (mail-header-id header))
4594          (data (gnus-data-find article))
4595          (thread (gnus-id-to-thread id))
4596          (references (mail-header-references header))
4597          (parent
4598           (gnus-id-to-thread
4599            (or (gnus-parent-id
4600                 (when (and references
4601                            (not (equal "" references)))
4602                   references))
4603                "none")))
4604          (inhibit-read-only t)
4605          (old (car thread)))
4606     (when thread
4607       (unless iheader
4608         (setcar thread nil)
4609         (when parent
4610           (delq thread parent)))
4611       (if (gnus-summary-insert-subject id header)
4612           ;; Set the (possibly) new article number in the data structure.
4613           (gnus-data-set-number data (gnus-id-to-article id))
4614         (setcar thread old)
4615         nil))))
4616
4617 (defun gnus-rebuild-thread (id &optional line)
4618   "Rebuild the thread containing ID.
4619 If LINE, insert the rebuilt thread starting on line LINE."
4620   (let ((inhibit-read-only t)
4621         old-pos current thread data)
4622     (if (not gnus-show-threads)
4623         (setq thread (list (car (gnus-id-to-thread id))))
4624       ;; Get the thread this article is part of.
4625       (setq thread (gnus-remove-thread id)))
4626     (setq old-pos (point-at-bol))
4627     (setq current (save-excursion
4628                     (and (re-search-backward "[\r\n]" nil t)
4629                          (gnus-summary-article-number))))
4630     ;; If this is a gathered thread, we have to go some re-gathering.
4631     (when (stringp (car thread))
4632       (let ((subject (car thread))
4633             roots thr)
4634         (setq thread (cdr thread))
4635         (while thread
4636           (unless (memq (setq thr (gnus-id-to-thread
4637                                    (gnus-root-id
4638                                     (mail-header-id (caar thread)))))
4639                         roots)
4640             (push thr roots))
4641           (setq thread (cdr thread)))
4642         ;; We now have all (unique) roots.
4643         (if (= (length roots) 1)
4644             ;; All the loose roots are now one solid root.
4645             (setq thread (car roots))
4646           (setq thread (cons subject (gnus-sort-threads roots))))))
4647     (let (threads)
4648       ;; We then insert this thread into the summary buffer.
4649       (when line
4650         (goto-char (point-min))
4651         (forward-line (1- line)))
4652       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4653         (if gnus-show-threads
4654             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4655           (gnus-summary-prepare-unthreaded thread))
4656         (setq data (nreverse gnus-newsgroup-data))
4657         (setq threads gnus-newsgroup-threads))
4658       ;; We splice the new data into the data structure.
4659       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4660       ;;!!! then we want to insert at the beginning of the buffer.
4661       ;;!!! That happens to be true with Gnus now, but that may
4662       ;;!!! change in the future.  Perhaps.
4663       (gnus-data-enter-list
4664        (if line nil current) data (- (point) old-pos))
4665       (setq gnus-newsgroup-threads
4666             (nconc threads gnus-newsgroup-threads))
4667       (gnus-data-compute-positions))))
4668
4669 (defun gnus-number-to-header (number)
4670   "Return the header for article NUMBER."
4671   (let ((headers gnus-newsgroup-headers))
4672     (while (and headers
4673                 (not (= number (mail-header-number (car headers)))))
4674       (pop headers))
4675     (when headers
4676       (car headers))))
4677
4678 (defun gnus-parent-headers (in-headers &optional generation)
4679   "Return the headers of the GENERATIONeth parent of HEADERS."
4680   (unless generation
4681     (setq generation 1))
4682   (let ((parent t)
4683         (headers in-headers)
4684         references)
4685     (while (and parent
4686                 (not (zerop generation))
4687                 (setq references (mail-header-references headers)))
4688       (setq headers (if (and references
4689                              (setq parent (gnus-parent-id references)))
4690                         (car (gnus-id-to-thread parent))
4691                       nil))
4692       (decf generation))
4693     (and (not (eq headers in-headers))
4694          headers)))
4695
4696 (defun gnus-id-to-thread (id)
4697   "Return the (sub-)thread where ID appears."
4698   (gnus-gethash id gnus-newsgroup-dependencies))
4699
4700 (defun gnus-id-to-article (id)
4701   "Return the article number of ID."
4702   (let ((thread (gnus-id-to-thread id)))
4703     (when (and thread
4704                (car thread))
4705       (mail-header-number (car thread)))))
4706
4707 (defun gnus-id-to-header (id)
4708   "Return the article headers of ID."
4709   (car (gnus-id-to-thread id)))
4710
4711 (defun gnus-article-displayed-root-p (article)
4712   "Say whether ARTICLE is a root(ish) article."
4713   (let ((level (gnus-summary-thread-level article))
4714         (refs (mail-header-references  (gnus-summary-article-header article)))
4715         particle)
4716     (cond
4717      ((null level) nil)
4718      ((zerop level) t)
4719      ((null refs) t)
4720      ((null (gnus-parent-id refs)) t)
4721      ((and (= 1 level)
4722            (null (setq particle (gnus-id-to-article
4723                                  (gnus-parent-id refs))))
4724            (null (gnus-summary-thread-level particle)))))))
4725
4726 (defun gnus-root-id (id)
4727   "Return the id of the root of the thread where ID appears."
4728   (let (last-id prev)
4729     (while (and id (setq prev (car (gnus-id-to-thread id))))
4730       (setq last-id id
4731             id (gnus-parent-id (mail-header-references prev))))
4732     last-id))
4733
4734 (defun gnus-articles-in-thread (thread)
4735   "Return the list of articles in THREAD."
4736   (cons (mail-header-number (car thread))
4737         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4738
4739 (defun gnus-remove-thread (id &optional dont-remove)
4740   "Remove the thread that has ID in it."
4741   (let (headers thread last-id)
4742     ;; First go up in this thread until we find the root.
4743     (setq last-id (gnus-root-id id)
4744           headers (message-flatten-list (gnus-id-to-thread last-id)))
4745     ;; We have now found the real root of this thread.  It might have
4746     ;; been gathered into some loose thread, so we have to search
4747     ;; through the threads to find the thread we wanted.
4748     (let ((threads gnus-newsgroup-threads)
4749           sub)
4750       (while threads
4751         (setq sub (car threads))
4752         (if (stringp (car sub))
4753             ;; This is a gathered thread, so we look at the roots
4754             ;; below it to find whether this article is in this
4755             ;; gathered root.
4756             (progn
4757               (setq sub (cdr sub))
4758               (while sub
4759                 (when (member (caar sub) headers)
4760                   (setq thread (car threads)
4761                         threads nil
4762                         sub nil))
4763                 (setq sub (cdr sub))))
4764           ;; It's an ordinary thread, so we check it.
4765           (when (eq (car sub) (car headers))
4766             (setq thread sub
4767                   threads nil)))
4768         (setq threads (cdr threads)))
4769       ;; If this article is in no thread, then it's a root.
4770       (if thread
4771           (unless dont-remove
4772             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4773         (setq thread (gnus-id-to-thread last-id)))
4774       (when thread
4775         (prog1
4776             thread                      ; We return this thread.
4777           (unless dont-remove
4778             (if (stringp (car thread))
4779                 (progn
4780                   ;; If we use dummy roots, then we have to remove the
4781                   ;; dummy root as well.
4782                   (when (eq gnus-summary-make-false-root 'dummy)
4783                     ;; We go to the dummy root by going to
4784                     ;; the first sub-"thread", and then one line up.
4785                     (gnus-summary-goto-article
4786                      (mail-header-number (caadr thread)))
4787                     (forward-line -1)
4788                     (gnus-delete-line)
4789                     (gnus-data-compute-positions))
4790                   (setq thread (cdr thread))
4791                   (while thread
4792                     (gnus-remove-thread-1 (car thread))
4793                     (setq thread (cdr thread))))
4794               (gnus-remove-thread-1 thread))))))))
4795
4796 (defun gnus-remove-thread-1 (thread)
4797   "Remove the thread THREAD recursively."
4798   (let ((number (mail-header-number (pop thread)))
4799         d)
4800     (setq thread (reverse thread))
4801     (while thread
4802       (gnus-remove-thread-1 (pop thread)))
4803     (when (setq d (gnus-data-find number))
4804       (goto-char (gnus-data-pos d))
4805       (gnus-summary-show-thread)
4806       (gnus-data-remove
4807        number
4808        (- (point-at-bol)
4809           (prog1
4810               (1+ (point-at-eol))
4811             (gnus-delete-line)))))))
4812
4813 (defun gnus-sort-threads-recursive (threads func)
4814   (sort (mapcar (lambda (thread)
4815                   (cons (car thread)
4816                         (and (cdr thread)
4817                              (gnus-sort-threads-recursive (cdr thread) func))))
4818                 threads) func))
4819
4820 (defun gnus-sort-threads-loop (threads func)
4821   (let* ((superthread (cons nil threads))
4822          (stack (list (cons superthread threads)))
4823          remaining-threads thread)
4824     (while stack
4825       (setq remaining-threads (cdr (car stack)))
4826       (if remaining-threads
4827           (progn (setq thread (car remaining-threads))
4828                  (setcdr (car stack) (cdr remaining-threads))
4829                  (if (cdr thread)
4830                      (push (cons thread (cdr thread)) stack)))
4831         (setq thread (caar stack))
4832         (setcdr thread (sort (cdr thread) func))
4833         (pop stack)))
4834     (cdr superthread)))
4835
4836 (defun gnus-sort-threads (threads)
4837   "Sort THREADS."
4838   (if (not gnus-thread-sort-functions)
4839       threads
4840     (gnus-message 8 "Sorting threads...")
4841     (prog1
4842         (condition-case nil
4843             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4844               (gnus-sort-threads-recursive
4845                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4846           ;; Even after binding max-lisp-eval-depth, the recursive
4847           ;; sorter might fail for very long threads.  In that case,
4848           ;; try using a (less well-tested) non-recursive sorter.
4849           (error (gnus-message 9 "Sorting threads with loop...")
4850                  (gnus-sort-threads-loop
4851                   threads (gnus-make-sort-function
4852                            gnus-thread-sort-functions))))
4853       (gnus-message 8 "Sorting threads...done"))))
4854
4855 (defun gnus-sort-articles (articles)
4856   "Sort ARTICLES."
4857   (when gnus-article-sort-functions
4858     (gnus-message 7 "Sorting articles...")
4859     (prog1
4860         (setq gnus-newsgroup-headers
4861               (sort articles (gnus-make-sort-function
4862                               gnus-article-sort-functions)))
4863       (gnus-message 7 "Sorting articles...done"))))
4864
4865 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4866 (defmacro gnus-thread-header (thread)
4867   "Return header of first article in THREAD.
4868 Note that THREAD must never, ever be anything else than a variable -
4869 using some other form will lead to serious barfage."
4870   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4871   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4872   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4873         (vector thread) 2))
4874
4875 (defsubst gnus-article-sort-by-number (h1 h2)
4876   "Sort articles by article number."
4877   (< (mail-header-number h1)
4878      (mail-header-number h2)))
4879
4880 (defun gnus-thread-sort-by-number (h1 h2)
4881   "Sort threads by root article number."
4882   (gnus-article-sort-by-number
4883    (gnus-thread-header h1) (gnus-thread-header h2)))
4884
4885 (defsubst gnus-article-sort-by-random (h1 h2)
4886   "Sort articles randomly."
4887   (zerop (random 2)))
4888
4889 (defun gnus-thread-sort-by-random (h1 h2)
4890   "Sort threads randomly."
4891   (gnus-article-sort-by-random
4892    (gnus-thread-header h1) (gnus-thread-header h2)))
4893
4894 (defsubst gnus-article-sort-by-lines (h1 h2)
4895   "Sort articles by article Lines header."
4896   (< (mail-header-lines h1)
4897      (mail-header-lines h2)))
4898
4899 (defun gnus-thread-sort-by-lines (h1 h2)
4900   "Sort threads by root article Lines header."
4901   (gnus-article-sort-by-lines
4902    (gnus-thread-header h1) (gnus-thread-header h2)))
4903
4904 (defsubst gnus-article-sort-by-chars (h1 h2)
4905   "Sort articles by octet length."
4906   (< (mail-header-chars h1)
4907      (mail-header-chars h2)))
4908
4909 (defun gnus-thread-sort-by-chars (h1 h2)
4910   "Sort threads by root article octet length."
4911   (gnus-article-sort-by-chars
4912    (gnus-thread-header h1) (gnus-thread-header h2)))
4913
4914 (defsubst gnus-article-sort-by-author (h1 h2)
4915   "Sort articles by root author."
4916   (gnus-string<
4917    (let ((extract (funcall
4918                    gnus-extract-address-components
4919                    (mail-header-from h1))))
4920      (or (car extract) (cadr extract) ""))
4921    (let ((extract (funcall
4922                    gnus-extract-address-components
4923                    (mail-header-from h2))))
4924      (or (car extract) (cadr extract) ""))))
4925
4926 (defun gnus-thread-sort-by-author (h1 h2)
4927   "Sort threads by root author."
4928   (gnus-article-sort-by-author
4929    (gnus-thread-header h1)  (gnus-thread-header h2)))
4930
4931 (defsubst gnus-article-sort-by-recipient (h1 h2)
4932   "Sort articles by recipient."
4933   (gnus-string<
4934    (let ((extract (funcall
4935                    gnus-extract-address-components
4936                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4937      (or (car extract) (cadr extract)))
4938    (let ((extract (funcall
4939                    gnus-extract-address-components
4940                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4941      (or (car extract) (cadr extract)))))
4942
4943 (defun gnus-thread-sort-by-recipient (h1 h2)
4944   "Sort threads by root recipient."
4945   (gnus-article-sort-by-recipient
4946    (gnus-thread-header h1) (gnus-thread-header h2)))
4947
4948 (defsubst gnus-article-sort-by-subject (h1 h2)
4949   "Sort articles by root subject."
4950   (gnus-string<
4951    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4952    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4953
4954 (defun gnus-thread-sort-by-subject (h1 h2)
4955   "Sort threads by root subject."
4956   (gnus-article-sort-by-subject
4957    (gnus-thread-header h1) (gnus-thread-header h2)))
4958
4959 (defsubst gnus-article-sort-by-date (h1 h2)
4960   "Sort articles by root article date."
4961   (time-less-p
4962    (gnus-date-get-time (mail-header-date h1))
4963    (gnus-date-get-time (mail-header-date h2))))
4964
4965 (defun gnus-thread-sort-by-date (h1 h2)
4966   "Sort threads by root article date."
4967   (gnus-article-sort-by-date
4968    (gnus-thread-header h1) (gnus-thread-header h2)))
4969
4970 (defsubst gnus-article-sort-by-score (h1 h2)
4971   "Sort articles by root article score.
4972 Unscored articles will be counted as having a score of zero."
4973   (> (or (cdr (assq (mail-header-number h1)
4974                     gnus-newsgroup-scored))
4975          gnus-summary-default-score 0)
4976      (or (cdr (assq (mail-header-number h2)
4977                     gnus-newsgroup-scored))
4978          gnus-summary-default-score 0)))
4979
4980 (defun gnus-thread-sort-by-score (h1 h2)
4981   "Sort threads by root article score."
4982   (gnus-article-sort-by-score
4983    (gnus-thread-header h1) (gnus-thread-header h2)))
4984
4985 (defun gnus-thread-sort-by-total-score (h1 h2)
4986   "Sort threads by the sum of all scores in the thread.
4987 Unscored articles will be counted as having a score of zero."
4988   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4989
4990 (defun gnus-thread-total-score (thread)
4991   ;; This function find the total score of THREAD.
4992   (cond
4993    ((null thread)
4994     0)
4995    ((consp thread)
4996     (if (stringp (car thread))
4997         (apply gnus-thread-score-function 0
4998                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4999       (gnus-thread-total-score-1 thread)))
5000    (t
5001     (gnus-thread-total-score-1 (list thread)))))
5002
5003 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5004   "Sort articles by number."
5005   (gnus-article-sort-by-number h1 h2))
5006
5007 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5008   "Sort threads such that the thread with the most recently arrived article comes first."
5009   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5010
5011 (defun gnus-thread-highest-number (thread)
5012   "Return the highest article number in THREAD."
5013   (apply 'max (mapcar (lambda (header)
5014                         (mail-header-number header))
5015                       (message-flatten-list thread))))
5016
5017 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5018   "Sort articles by number."
5019   (gnus-article-sort-by-date h1 h2))
5020
5021 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5022   "Sort threads such that the thread with the most recently dated article comes first."
5023   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5024
5025 ; Since this is called not only to sort the top-level threads, but
5026 ; also in recursive sorts to order the articles within a thread, each
5027 ; article will be processed many times.  Thus it speeds things up
5028 ; quite a bit to use gnus-date-get-time, which caches the time value.
5029 (defun gnus-thread-latest-date (thread)
5030   "Return the highest article date in THREAD."
5031   (apply 'max
5032          (mapcar (lambda (header) (gnus-float-time
5033                                    (gnus-date-get-time
5034                                     (mail-header-date header))))
5035                  (message-flatten-list thread))))
5036
5037 (defun gnus-thread-total-score-1 (root)
5038   ;; This function find the total score of the thread below ROOT.
5039   (setq root (car root))
5040   (apply gnus-thread-score-function
5041          (or (append
5042               (mapcar 'gnus-thread-total-score
5043                       (cdr (gnus-id-to-thread (mail-header-id root))))
5044               (when (> (mail-header-number root) 0)
5045                 (list (or (cdr (assq (mail-header-number root)
5046                                      gnus-newsgroup-scored))
5047                           gnus-summary-default-score 0))))
5048              (list gnus-summary-default-score)
5049              '(0))))
5050
5051 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5052 (defvar gnus-tmp-prev-subject nil)
5053 (defvar gnus-tmp-false-parent nil)
5054 (defvar gnus-tmp-root-expunged nil)
5055 (defvar gnus-tmp-dummy-line nil)
5056
5057 (defun gnus-extra-header (type &optional header)
5058   "Return the extra header of TYPE."
5059   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5060       ""))
5061
5062 (defvar gnus-tmp-thread-tree-header-string "")
5063
5064 (defcustom gnus-sum-thread-tree-root "> "
5065   "With %B spec, used for the root of a thread.
5066 If nil, use subject instead."
5067   :version "22.1"
5068   :type '(radio (const :format "%v  " nil) string)
5069   :group 'gnus-thread)
5070
5071 (defcustom gnus-sum-thread-tree-false-root "> "
5072   "With %B spec, used for a false root of a thread.
5073 If nil, use subject instead."
5074   :version "22.1"
5075   :type '(radio (const :format "%v  " nil) string)
5076   :group 'gnus-thread)
5077
5078 (defcustom gnus-sum-thread-tree-single-indent ""
5079   "With %B spec, used for a thread with just one message.
5080 If nil, use subject instead."
5081   :version "22.1"
5082   :type '(radio (const :format "%v  " nil) string)
5083   :group 'gnus-thread)
5084
5085 (defcustom gnus-sum-thread-tree-vertical "| "
5086   "With %B spec, used for drawing a vertical line."
5087   :version "22.1"
5088   :type 'string
5089   :group 'gnus-thread)
5090
5091 (defcustom gnus-sum-thread-tree-indent "  "
5092   "With %B spec, used for indenting."
5093   :version "22.1"
5094   :type 'string
5095   :group 'gnus-thread)
5096
5097 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5098   "With %B spec, used for a leaf with brothers."
5099   :version "22.1"
5100   :type 'string
5101   :group 'gnus-thread)
5102
5103 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5104   "With %B spec, used for a leaf without brothers."
5105   :version "22.1"
5106   :type 'string
5107   :group 'gnus-thread)
5108
5109 (defcustom gnus-summary-display-while-building nil
5110   "If non-nil, show and update the summary buffer as it's being built.
5111 If the value is t, update the buffer after every line is inserted.  If
5112 the value is an integer (N), update the display every N lines."
5113   :version "22.1"
5114   :group 'gnus-thread
5115   :type '(choice (const :tag "off" nil)
5116                  number
5117                  (const :tag "frequently" t)))
5118
5119 (defun gnus-summary-prepare-threads (threads)
5120   "Prepare summary buffer from THREADS and indentation LEVEL.
5121 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5122 or a straight list of headers."
5123   (gnus-message 7 "Generating summary...")
5124
5125   (setq gnus-newsgroup-threads threads)
5126   (beginning-of-line)
5127
5128   (let ((gnus-tmp-level 0)
5129         (default-score (or gnus-summary-default-score 0))
5130         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5131         (building-line-count gnus-summary-display-while-building)
5132         (building-count (integerp gnus-summary-display-while-building))
5133         thread number subject stack state gnus-tmp-gathered beg-match
5134         new-roots gnus-tmp-new-adopts thread-end simp-subject
5135         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5136         gnus-tmp-replied gnus-tmp-subject-or-nil
5137         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5138         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5139         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5140         tree-stack)
5141
5142     (setq gnus-tmp-prev-subject nil
5143           gnus-tmp-thread-tree-header-string "")
5144
5145     (if (vectorp (car threads))
5146         ;; If this is a straight (sic) list of headers, then a
5147         ;; threaded summary display isn't required, so we just create
5148         ;; an unthreaded one.
5149         (gnus-summary-prepare-unthreaded threads)
5150
5151       ;; Do the threaded display.
5152
5153       (if gnus-summary-display-while-building
5154           (switch-to-buffer (buffer-name)))
5155       (while (or threads stack gnus-tmp-new-adopts new-roots)
5156
5157         (if (and (= gnus-tmp-level 0)
5158                  (or (not stack)
5159                      (= (caar stack) 0))
5160                  (not gnus-tmp-false-parent)
5161                  (or gnus-tmp-new-adopts new-roots))
5162             (if gnus-tmp-new-adopts
5163                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5164                       thread (list (car gnus-tmp-new-adopts))
5165                       gnus-tmp-header (caar thread)
5166                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5167               (when new-roots
5168                 (setq thread (list (car new-roots))
5169                       gnus-tmp-header (caar thread)
5170                       new-roots (cdr new-roots))))
5171
5172           (if threads
5173               ;; If there are some threads, we do them before the
5174               ;; threads on the stack.
5175               (setq thread threads
5176                     gnus-tmp-header (caar thread))
5177             ;; There were no current threads, so we pop something off
5178             ;; the stack.
5179             (setq state (car stack)
5180                   gnus-tmp-level (car state)
5181                   tree-stack (cadr state)
5182                   thread (caddr state)
5183                   stack (cdr stack)
5184                   gnus-tmp-header (caar thread))))
5185
5186         (setq gnus-tmp-false-parent nil)
5187         (setq gnus-tmp-root-expunged nil)
5188         (setq thread-end nil)
5189
5190         (if (stringp gnus-tmp-header)
5191             ;; The header is a dummy root.
5192             (cond
5193              ((eq gnus-summary-make-false-root 'adopt)
5194               ;; We let the first article adopt the rest.
5195               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5196                                                (cddar thread)))
5197               (setq gnus-tmp-gathered
5198                     (nconc (mapcar
5199                             (lambda (h) (mail-header-number (car h)))
5200                             (cddar thread))
5201                            gnus-tmp-gathered))
5202               (setq thread (cons (list (caar thread)
5203                                        (cadar thread))
5204                                  (cdr thread)))
5205               (setq gnus-tmp-level -1
5206                     gnus-tmp-false-parent t))
5207              ((eq gnus-summary-make-false-root 'empty)
5208               ;; We print adopted articles with empty subject fields.
5209               (setq gnus-tmp-gathered
5210                     (nconc (mapcar
5211                             (lambda (h) (mail-header-number (car h)))
5212                             (cddar thread))
5213                            gnus-tmp-gathered))
5214               (setq gnus-tmp-level -1))
5215              ((eq gnus-summary-make-false-root 'dummy)
5216               ;; We remember that we probably want to output a dummy
5217               ;; root.
5218               (setq gnus-tmp-dummy-line gnus-tmp-header)
5219               (setq gnus-tmp-prev-subject gnus-tmp-header))
5220              (t
5221               ;; We do not make a root for the gathered
5222               ;; sub-threads at all.
5223               (setq gnus-tmp-level -1)))
5224
5225           (setq number (mail-header-number gnus-tmp-header)
5226                 subject (mail-header-subject gnus-tmp-header)
5227                 simp-subject (gnus-simplify-subject-fully subject))
5228
5229           (cond
5230            ;; If the thread has changed subject, we might want to make
5231            ;; this subthread into a root.
5232            ((and (null gnus-thread-ignore-subject)
5233                  (not (zerop gnus-tmp-level))
5234                  gnus-tmp-prev-subject
5235                  (not (string= gnus-tmp-prev-subject simp-subject)))
5236             (setq new-roots (nconc new-roots (list (car thread)))
5237                   thread-end t
5238                   gnus-tmp-header nil))
5239            ;; If the article lies outside the current limit,
5240            ;; then we do not display it.
5241            ((not (memq number gnus-newsgroup-limit))
5242             (setq gnus-tmp-gathered
5243                   (nconc (mapcar
5244                           (lambda (h) (mail-header-number (car h)))
5245                           (cdar thread))
5246                          gnus-tmp-gathered))
5247             (setq gnus-tmp-new-adopts (if (cdar thread)
5248                                           (append gnus-tmp-new-adopts
5249                                                   (cdar thread))
5250                                         gnus-tmp-new-adopts)
5251                   thread-end t
5252                   gnus-tmp-header nil)
5253             (when (zerop gnus-tmp-level)
5254               (setq gnus-tmp-root-expunged t)))
5255            ;; Perhaps this article is to be marked as read?
5256            ((and gnus-summary-mark-below
5257                  (< (or (cdr (assq number gnus-newsgroup-scored))
5258                         default-score)
5259                     gnus-summary-mark-below)
5260                  ;; Don't touch sparse articles.
5261                  (not (gnus-summary-article-sparse-p number))
5262                  (not (gnus-summary-article-ancient-p number)))
5263             (setq gnus-newsgroup-unreads
5264                   (delq number gnus-newsgroup-unreads))
5265             (if gnus-newsgroup-auto-expire
5266                 (setq gnus-newsgroup-expirable
5267                       (gnus-add-to-sorted-list
5268                        gnus-newsgroup-expirable number))
5269               (push (cons number gnus-low-score-mark)
5270                     gnus-newsgroup-reads))))
5271
5272           (when gnus-tmp-header
5273             ;; We may have an old dummy line to output before this
5274             ;; article.
5275             (when (and gnus-tmp-dummy-line
5276                        (gnus-subject-equal
5277                         gnus-tmp-dummy-line
5278                         (mail-header-subject gnus-tmp-header)))
5279               (gnus-summary-insert-dummy-line
5280                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5281               (setq gnus-tmp-dummy-line nil))
5282
5283             ;; Compute the mark.
5284             (setq gnus-tmp-unread (gnus-article-mark number))
5285
5286             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5287                                   gnus-tmp-header gnus-tmp-level)
5288                   gnus-newsgroup-data)
5289
5290             ;; Actually insert the line.
5291             (setq
5292              gnus-tmp-subject-or-nil
5293              (cond
5294               ((and gnus-thread-ignore-subject
5295                     gnus-tmp-prev-subject
5296                     (not (string= gnus-tmp-prev-subject simp-subject)))
5297                subject)
5298               ((zerop gnus-tmp-level)
5299                (if (and (eq gnus-summary-make-false-root 'empty)
5300                         (memq number gnus-tmp-gathered)
5301                         gnus-tmp-prev-subject
5302                         (string= gnus-tmp-prev-subject simp-subject))
5303                    gnus-summary-same-subject
5304                  subject))
5305               (t gnus-summary-same-subject)))
5306             (if (and (eq gnus-summary-make-false-root 'adopt)
5307                      (= gnus-tmp-level 1)
5308                      (memq number gnus-tmp-gathered))
5309                 (setq gnus-tmp-opening-bracket ?\<
5310                       gnus-tmp-closing-bracket ?\>)
5311               (setq gnus-tmp-opening-bracket ?\[
5312                     gnus-tmp-closing-bracket ?\]))
5313             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5314                 (gnus-make-thread-indent-array
5315                  (max (* 2 (length gnus-thread-indent-array))
5316                       gnus-tmp-level)))
5317             (setq
5318              gnus-tmp-indentation
5319              (aref gnus-thread-indent-array gnus-tmp-level)
5320              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5321              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5322                                 gnus-summary-default-score 0)
5323              gnus-tmp-score-char
5324              (if (or (null gnus-summary-default-score)
5325                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5326                          gnus-summary-zcore-fuzz))
5327                  ?                      ;Whitespace
5328                (if (< gnus-tmp-score gnus-summary-default-score)
5329                    gnus-score-below-mark gnus-score-over-mark))
5330              gnus-tmp-replied
5331              (cond ((memq number gnus-newsgroup-processable)
5332                     gnus-process-mark)
5333                    ((memq number gnus-newsgroup-cached)
5334                     gnus-cached-mark)
5335                    ((memq number gnus-newsgroup-replied)
5336                     gnus-replied-mark)
5337                    ((memq number gnus-newsgroup-forwarded)
5338                     gnus-forwarded-mark)
5339                    ((memq number gnus-newsgroup-saved)
5340                     gnus-saved-mark)
5341                    ((memq number gnus-newsgroup-recent)
5342                     gnus-recent-mark)
5343                    ((memq number gnus-newsgroup-unseen)
5344                     gnus-unseen-mark)
5345                    (t gnus-no-mark))
5346              gnus-tmp-downloaded
5347              (cond ((memq number gnus-newsgroup-undownloaded)
5348                     gnus-undownloaded-mark)
5349                    (gnus-newsgroup-agentized
5350                     gnus-downloaded-mark)
5351                    (t
5352                     gnus-no-mark))
5353              gnus-tmp-from (mail-header-from gnus-tmp-header)
5354              gnus-tmp-name
5355              (cond
5356               ((string-match "<[^>]+> *$" gnus-tmp-from)
5357                (setq beg-match (match-beginning 0))
5358                (or (and (string-match "^\".+\"" gnus-tmp-from)
5359                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5360                    (substring gnus-tmp-from 0 beg-match)))
5361               ((string-match "(.+)" gnus-tmp-from)
5362                (substring gnus-tmp-from
5363                           (1+ (match-beginning 0)) (1- (match-end 0))))
5364               (t gnus-tmp-from))
5365
5366              ;; Do the %B string
5367              gnus-tmp-thread-tree-header-string
5368              (cond
5369               ((not gnus-show-threads) "")
5370               ((zerop gnus-tmp-level)
5371                (cond ((cdar thread)
5372                       (or gnus-sum-thread-tree-root subject))
5373                      (gnus-tmp-new-adopts
5374                       (or gnus-sum-thread-tree-false-root subject))
5375                      (t
5376                       (or gnus-sum-thread-tree-single-indent subject))))
5377               (t
5378                (concat (apply 'concat
5379                               (mapcar (lambda (item)
5380                                         (if (= item 1)
5381                                             gnus-sum-thread-tree-vertical
5382                                           gnus-sum-thread-tree-indent))
5383                                       (cdr (reverse tree-stack))))
5384                        (if (nth 1 thread)
5385                            gnus-sum-thread-tree-leaf-with-other
5386                          gnus-sum-thread-tree-single-leaf)))))
5387             (when (string= gnus-tmp-name "")
5388               (setq gnus-tmp-name gnus-tmp-from))
5389             (unless (numberp gnus-tmp-lines)
5390               (setq gnus-tmp-lines -1))
5391             (if (= gnus-tmp-lines -1)
5392                 (setq gnus-tmp-lines "?")
5393               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5394             (gnus-put-text-property
5395              (point)
5396              (progn (eval gnus-summary-line-format-spec) (point))
5397              'gnus-number number)
5398             (when gnus-visual-p
5399               (forward-line -1)
5400               (gnus-summary-highlight-line)
5401               (when gnus-summary-update-hook
5402                 (gnus-run-hooks 'gnus-summary-update-hook))
5403               (forward-line 1))
5404
5405             (setq gnus-tmp-prev-subject simp-subject)))
5406
5407         (when (nth 1 thread)
5408           (push (list (max 0 gnus-tmp-level)
5409                       (copy-sequence tree-stack)
5410                       (nthcdr 1 thread))
5411                 stack))
5412         (push (if (nth 1 thread) 1 0) tree-stack)
5413         (incf gnus-tmp-level)
5414         (setq threads (if thread-end nil (cdar thread)))
5415         (if gnus-summary-display-while-building
5416             (if building-count
5417                 (progn
5418                   ;; use a set frequency
5419                   (setq building-line-count (1- building-line-count))
5420                   (when (= building-line-count 0)
5421                     (sit-for 0)
5422                     (setq building-line-count
5423                           gnus-summary-display-while-building)))
5424               ;; always
5425               (sit-for 0)))
5426         (unless threads
5427           (setq gnus-tmp-level 0)))))
5428   (gnus-message 7 "Generating summary...done"))
5429
5430 (defun gnus-summary-prepare-unthreaded (headers)
5431   "Generate an unthreaded summary buffer based on HEADERS."
5432   (let (header number mark)
5433
5434     (beginning-of-line)
5435
5436     (while headers
5437       ;; We may have to root out some bad articles...
5438       (when (memq (setq number (mail-header-number
5439                                 (setq header (pop headers))))
5440                   gnus-newsgroup-limit)
5441         ;; Mark article as read when it has a low score.
5442         (when (and gnus-summary-mark-below
5443                    (< (or (cdr (assq number gnus-newsgroup-scored))
5444                           gnus-summary-default-score 0)
5445                       gnus-summary-mark-below)
5446                    (not (gnus-summary-article-ancient-p number)))
5447           (setq gnus-newsgroup-unreads
5448                 (delq number gnus-newsgroup-unreads))
5449           (if gnus-newsgroup-auto-expire
5450               (push number gnus-newsgroup-expirable)
5451             (push (cons number gnus-low-score-mark)
5452                   gnus-newsgroup-reads)))
5453
5454         (setq mark (gnus-article-mark number))
5455         (push (gnus-data-make number mark (1+ (point)) header 0)
5456               gnus-newsgroup-data)
5457         (gnus-summary-insert-line
5458          header 0 number
5459          (memq number gnus-newsgroup-undownloaded)
5460          mark (memq number gnus-newsgroup-replied)
5461          (memq number gnus-newsgroup-expirable)
5462          (mail-header-subject header) nil
5463          (cdr (assq number gnus-newsgroup-scored))
5464          (memq number gnus-newsgroup-processable))))))
5465
5466 (defun gnus-summary-remove-list-identifiers ()
5467   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5468   (let ((regexp (if (consp gnus-list-identifiers)
5469                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5470                   gnus-list-identifiers))
5471         changed subject)
5472     (when regexp
5473       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5474       (dolist (header gnus-newsgroup-headers)
5475         (setq subject (mail-header-subject header)
5476               changed nil)
5477         (while (string-match regexp subject)
5478           (setq subject
5479                 (concat (substring subject 0 (match-beginning 1))
5480                         (substring subject (match-end 0)))
5481                 changed t))
5482         (when changed
5483           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5484             (setq subject
5485                   (concat (substring subject 0 (match-beginning 1))
5486                           (substring subject (match-end 1)))))
5487           (mail-header-set-subject header subject))))))
5488
5489 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5490   "Fetch headers of ARTICLES."
5491   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5492     (gnus-message 5 "Fetching headers for %s..." name)
5493     (prog1
5494         (if (eq 'nov
5495                 (setq gnus-headers-retrieved-by
5496                       (gnus-retrieve-headers
5497                        articles gnus-newsgroup-name
5498                        (or limit
5499                            ;; We might want to fetch old headers, but
5500                            ;; not if there is only 1 article.
5501                            (and (or (and
5502                                      (not (eq gnus-fetch-old-headers 'some))
5503                                      (not (numberp gnus-fetch-old-headers)))
5504                                     (> (length articles) 1))
5505                                 gnus-fetch-old-headers)))))
5506             (gnus-get-newsgroup-headers-xover
5507              articles force-new dependencies gnus-newsgroup-name t)
5508           (gnus-get-newsgroup-headers dependencies force-new))
5509       (gnus-message 5 "Fetching headers for %s...done" name))))
5510
5511 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5512   "Select newsgroup GROUP.
5513 If READ-ALL is non-nil, all articles in the group are selected.
5514 If SELECT-ARTICLES, only select those articles from GROUP."
5515   (let* ((entry (gnus-group-entry group))
5516          ;;!!! Dirty hack; should be removed.
5517          (gnus-summary-ignore-duplicates
5518           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5519               t
5520             gnus-summary-ignore-duplicates))
5521          (info (nth 2 entry))
5522          charset articles fetched-articles cached)
5523
5524     (unless (gnus-check-server
5525              (set (make-local-variable 'gnus-current-select-method)
5526                   (gnus-find-method-for-group group)))
5527       (error "Couldn't open server"))
5528     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5529
5530     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5531         (gnus-activate-group group)     ; Or we can activate it...
5532         (progn                          ; Or we bug out.
5533           (when (equal major-mode 'gnus-summary-mode)
5534             (gnus-kill-buffer (current-buffer)))
5535           (error
5536            "Couldn't activate group %s: %s"
5537            (mm-decode-coding-string group charset)
5538            (mm-decode-coding-string (gnus-status-message group) charset))))
5539
5540     (unless (gnus-request-group group t)
5541       (when (equal major-mode 'gnus-summary-mode)
5542         (gnus-kill-buffer (current-buffer)))
5543       (error "Couldn't request group %s: %s"
5544              (mm-decode-coding-string group charset)
5545              (mm-decode-coding-string (gnus-status-message group) charset)))
5546
5547     (when gnus-agent
5548       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5549
5550       (setq gnus-summary-use-undownloaded-faces
5551             (gnus-agent-find-parameter
5552              group
5553              'agent-enable-undownloaded-faces)))
5554
5555     (setq gnus-newsgroup-name group
5556           gnus-newsgroup-unselected nil
5557           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5558
5559     (let ((display (gnus-group-find-parameter group 'display)))
5560       (setq gnus-newsgroup-display
5561             (cond
5562              ((not (zerop (or (car-safe read-all) 0)))
5563               ;; The user entered the group with C-u SPC/RET, let's show
5564               ;; all articles.
5565               'gnus-not-ignore)
5566              ((eq display 'all)
5567               'gnus-not-ignore)
5568              ((arrayp display)
5569               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5570              ((numberp display)
5571               ;; The following is probably the "correct" solution, but
5572               ;; it makes Gnus fetch all headers and then limit the
5573               ;; articles (which is slow), so instead we hack the
5574               ;; select-articles parameter instead. -- Simon Josefsson
5575               ;; <jas@kth.se>
5576               ;;
5577               ;; (gnus-byte-compile
5578               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5579               ;;                         display)))))
5580               (setq select-articles
5581                     (gnus-uncompress-range
5582                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5583                              (if (> tmp 0)
5584                                  tmp
5585                                1))
5586                            (cdr (gnus-active group)))))
5587               nil)
5588              (t
5589               nil))))
5590
5591     (gnus-summary-setup-default-charset)
5592
5593     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5594     (when (gnus-virtual-group-p group)
5595       (setq cached gnus-newsgroup-cached))
5596
5597     (setq gnus-newsgroup-unreads
5598           (gnus-sorted-ndifference
5599            (gnus-sorted-ndifference gnus-newsgroup-unreads
5600                                     gnus-newsgroup-marked)
5601            gnus-newsgroup-dormant))
5602
5603     (setq gnus-newsgroup-processable nil)
5604
5605     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5606
5607     ;; Adjust and set lists of article marks.
5608     (when info
5609       (gnus-adjust-marked-articles info))
5610     (if (setq articles select-articles)
5611         (setq gnus-newsgroup-unselected
5612               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5613       (setq articles (gnus-articles-to-read group read-all)))
5614
5615     (cond
5616      ((null articles)
5617       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5618       'quit)
5619      ((eq articles 0) nil)
5620      (t
5621       ;; Init the dependencies hash table.
5622       (setq gnus-newsgroup-dependencies
5623             (gnus-make-hashtable (length articles)))
5624       (gnus-set-global-variables)
5625       ;; Retrieve the headers and read them in.
5626
5627       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5628
5629       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5630       (when cached
5631         (setq gnus-newsgroup-cached cached))
5632
5633       ;; Suppress duplicates?
5634       (when gnus-suppress-duplicates
5635         (gnus-dup-suppress-articles))
5636
5637       ;; Set the initial limit.
5638       (setq gnus-newsgroup-limit (copy-sequence articles))
5639       ;; Remove canceled articles from the list of unread articles.
5640       (setq fetched-articles
5641             (mapcar (lambda (headers) (mail-header-number headers))
5642                     gnus-newsgroup-headers))
5643       (setq gnus-newsgroup-articles fetched-articles)
5644       (setq gnus-newsgroup-unreads
5645             (gnus-sorted-nintersection
5646              gnus-newsgroup-unreads fetched-articles))
5647       (gnus-compute-unseen-list)
5648
5649       ;; Removed marked articles that do not exist.
5650       (gnus-update-missing-marks
5651        (gnus-sorted-difference articles fetched-articles))
5652       ;; We might want to build some more threads first.
5653       (when (and gnus-fetch-old-headers
5654                  (eq gnus-headers-retrieved-by 'nov))
5655         (if (eq gnus-fetch-old-headers 'invisible)
5656             (gnus-build-all-threads)
5657           (gnus-build-old-threads)))
5658       ;; Let the Gnus agent mark articles as read.
5659       (when gnus-agent
5660         (gnus-agent-get-undownloaded-list))
5661       ;; Remove list identifiers from subject
5662       (when gnus-list-identifiers
5663         (gnus-summary-remove-list-identifiers))
5664       ;; Check whether auto-expire is to be done in this group.
5665       (setq gnus-newsgroup-auto-expire
5666             (gnus-group-auto-expirable-p group))
5667       ;; Set up the article buffer now, if necessary.
5668       (unless (and gnus-single-article-buffer
5669                    (equal gnus-article-buffer "*Article*"))
5670         (gnus-article-setup-buffer))
5671       ;; First and last article in this newsgroup.
5672       (when gnus-newsgroup-headers
5673         (setq gnus-newsgroup-begin
5674               (mail-header-number (car gnus-newsgroup-headers))
5675               gnus-newsgroup-end
5676               (mail-header-number
5677                (gnus-last-element gnus-newsgroup-headers))))
5678       ;; GROUP is successfully selected.
5679       (or gnus-newsgroup-headers t)))))
5680
5681 (defun gnus-compute-unseen-list ()
5682   ;; The `seen' marks are treated specially.
5683   (if (not gnus-newsgroup-seen)
5684       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5685     (setq gnus-newsgroup-unseen
5686           (gnus-inverse-list-range-intersection
5687            gnus-newsgroup-articles gnus-newsgroup-seen))))
5688
5689 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5690
5691 (defun gnus-summary-display-make-predicate (display)
5692   (require 'gnus-agent)
5693   (when (= (length display) 1)
5694     (setq display (car display)))
5695   (unless gnus-summary-display-cache
5696     (dolist (elem (append '((unread . unread)
5697                             (read . read)
5698                             (unseen . unseen))
5699                           gnus-article-mark-lists))
5700       (push (cons (cdr elem)
5701                   (gnus-byte-compile    ;Why bother?
5702                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5703             gnus-summary-display-cache)))
5704   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5705         (gnus-category-predicate-cache gnus-summary-display-cache))
5706     (gnus-get-predicate display)))
5707
5708 ;; Uses the dynamically bound `gnus-number' variable.
5709 (defvar gnus-number)
5710 (defun gnus-article-marked-p (type &optional article)
5711   (let ((article (or article gnus-number)))
5712     (cond
5713      ((eq type 'tick)
5714       (memq article gnus-newsgroup-marked))
5715      ((eq type 'spam)
5716       (memq article gnus-newsgroup-spam-marked))
5717      ((eq type 'unsend)
5718       (memq article gnus-newsgroup-unsendable))
5719      ((eq type 'undownload)
5720       (memq article gnus-newsgroup-undownloaded))
5721      ((eq type 'download)
5722       (memq article gnus-newsgroup-downloadable))
5723      ((eq type 'unread)
5724       (memq article gnus-newsgroup-unreads))
5725      ((eq type 'read)
5726       (memq article gnus-newsgroup-reads))
5727      ((eq type 'dormant)
5728       (memq article gnus-newsgroup-dormant) )
5729      ((eq type 'expire)
5730       (memq article gnus-newsgroup-expirable))
5731      ((eq type 'reply)
5732       (memq article gnus-newsgroup-replied))
5733      ((eq type 'killed)
5734       (memq article gnus-newsgroup-killed))
5735      ((eq type 'bookmark)
5736       (assq article gnus-newsgroup-bookmarks))
5737      ((eq type 'score)
5738       (assq article gnus-newsgroup-scored))
5739      ((eq type 'save)
5740       (memq article gnus-newsgroup-saved))
5741      ((eq type 'cache)
5742       (memq article gnus-newsgroup-cached))
5743      ((eq type 'forward)
5744       (memq article gnus-newsgroup-forwarded))
5745      ((eq type 'seen)
5746       (not (memq article gnus-newsgroup-unseen)))
5747      ((eq type 'recent)
5748       (memq article gnus-newsgroup-recent))
5749      (t t))))
5750
5751 (defun gnus-articles-to-read (group &optional read-all)
5752   "Find out what articles the user wants to read."
5753   (let* ((articles
5754           ;; Select all articles if `read-all' is non-nil, or if there
5755           ;; are no unread articles.
5756           (if (or read-all
5757                   (and (zerop (length gnus-newsgroup-marked))
5758                        (zerop (length gnus-newsgroup-unreads)))
5759                   ;; Fetch all if the predicate is non-nil.
5760                   gnus-newsgroup-display)
5761               ;; We want to select the headers for all the articles in
5762               ;; the group, so we select either all the active
5763               ;; articles in the group, or (if that's nil), the
5764               ;; articles in the cache.
5765               (or
5766                (if gnus-newsgroup-maximum-articles
5767                    (let ((active (gnus-active group)))
5768                      (gnus-uncompress-range
5769                       (cons (max (car active)
5770                                  (- (cdr active)
5771                                     gnus-newsgroup-maximum-articles
5772                                     -1))
5773                             (cdr active))))
5774                  (gnus-uncompress-range (gnus-active group)))
5775                (gnus-cache-articles-in-group group))
5776             ;; Select only the "normal" subset of articles.
5777             (gnus-sorted-nunion
5778              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5779              gnus-newsgroup-unreads)))
5780          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5781          (scored (length scored-list))
5782          (number (length articles))
5783          (marked (+ (length gnus-newsgroup-marked)
5784                     (length gnus-newsgroup-dormant)))
5785          (select
5786           (cond
5787            ((numberp read-all)
5788             read-all)
5789            ((numberp gnus-newsgroup-display)
5790             gnus-newsgroup-display)
5791            (t
5792             (condition-case ()
5793                 (cond
5794                  ((and (or (<= scored marked) (= scored number))
5795                        (numberp gnus-large-newsgroup)
5796                        (> number gnus-large-newsgroup))
5797                   (let* ((cursor-in-echo-area nil)
5798                          (initial (gnus-parameter-large-newsgroup-initial
5799                                    gnus-newsgroup-name))
5800                          (input
5801                           (read-string
5802                            (format
5803                             "How many articles from %s (%s %d): "
5804                             (gnus-group-decoded-name gnus-newsgroup-name)
5805                             (if initial "max" "default")
5806                             number)
5807                            (if initial
5808                                (cons (number-to-string initial)
5809                                      0)))))
5810                     (if (string-match "^[ \t]*$" input) number input)))
5811                  ((and (> scored marked) (< scored number)
5812                        (> (- scored number) 20))
5813                   (let ((input
5814                          (read-string
5815                           (format "%s %s (%d scored, %d total): "
5816                                   "How many articles from"
5817                                   (gnus-group-decoded-name group)
5818                                   scored number))))
5819                     (if (string-match "^[ \t]*$" input)
5820                         number input)))
5821                  (t number))
5822               (quit
5823                (message "Quit getting the articles to read")
5824                nil))))))
5825     (setq select (if (stringp select) (string-to-number select) select))
5826     (if (or (null select) (zerop select))
5827         select
5828       (if (and (not (zerop scored)) (<= (abs select) scored))
5829           (progn
5830             (setq articles (sort scored-list '<))
5831             (setq number (length articles)))
5832         (setq articles (copy-sequence articles)))
5833
5834       (when (< (abs select) number)
5835         (if (< select 0)
5836             ;; Select the N oldest articles.
5837             (setcdr (nthcdr (1- (abs select)) articles) nil)
5838           ;; Select the N most recent articles.
5839           (setq articles (nthcdr (- number select) articles))))
5840       (setq gnus-newsgroup-unselected
5841             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5842       (when gnus-alter-articles-to-read-function
5843         (setq articles
5844               (sort
5845                (funcall gnus-alter-articles-to-read-function
5846                         gnus-newsgroup-name articles)
5847                '<)))
5848       articles)))
5849
5850 (defun gnus-killed-articles (killed articles)
5851   (let (out)
5852     (while articles
5853       (when (inline (gnus-member-of-range (car articles) killed))
5854         (push (car articles) out))
5855       (setq articles (cdr articles)))
5856     out))
5857
5858 (defun gnus-uncompress-marks (marks)
5859   "Uncompress the mark ranges in MARKS."
5860   (let ((uncompressed '(score bookmark))
5861         out)
5862     (while marks
5863       (if (memq (caar marks) uncompressed)
5864           (push (car marks) out)
5865         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5866       (setq marks (cdr marks)))
5867     out))
5868
5869 (defun gnus-article-mark-to-type (mark)
5870   "Return the type of MARK."
5871   (or (cadr (assq mark gnus-article-special-mark-lists))
5872       'list))
5873
5874 (defun gnus-article-unpropagatable-p (mark)
5875   "Return whether MARK should be propagated to back end."
5876   (memq mark gnus-article-unpropagated-mark-lists))
5877
5878 (defun gnus-adjust-marked-articles (info)
5879   "Set all article lists and remove all marks that are no longer valid."
5880   (let* ((marked-lists (gnus-info-marks info))
5881          (active (gnus-active (gnus-info-group info)))
5882          (min (car active))
5883          (max (cdr active))
5884          (types gnus-article-mark-lists)
5885          marks var articles article mark mark-type
5886          bgn end)
5887     ;; Hack to avoid adjusting marks for imap.
5888     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5889               'nnimap)
5890       (setq min 1))
5891
5892     (dolist (marks marked-lists)
5893       (setq mark (car marks)
5894             mark-type (gnus-article-mark-to-type mark)
5895             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5896
5897       ;; We set the variable according to the type of the marks list,
5898       ;; and then adjust the marks to a subset of the active articles.
5899       (cond
5900        ;; Adjust "simple" lists - compressed yet unsorted
5901        ((eq mark-type 'list)
5902         ;; Simultaneously uncompress and clip to active range
5903         ;; See gnus-uncompress-range for a description of possible marks
5904         (let (l lh)
5905           (if (not (cadr marks))
5906               (set var nil)
5907             (setq articles (if (numberp (cddr marks))
5908                                (list (cdr marks))
5909                              (cdr marks))
5910                   lh (cons nil nil)
5911                   l lh)
5912
5913             (while (setq article (pop articles))
5914               (cond ((consp article)
5915                      (setq bgn (max (car article) min)
5916                            end (min (cdr article) max))
5917                      (while (<= bgn end)
5918                        (setq l (setcdr l (cons bgn nil))
5919                              bgn (1+ bgn))))
5920                     ((and (<= min article)
5921                           (>= max article))
5922                      (setq l (setcdr l (cons article nil))))))
5923             (set var (cdr lh)))))
5924        ;; Adjust assocs.
5925        ((eq mark-type 'tuple)
5926         (set var (setq articles (cdr marks)))
5927         (when (not (listp (cdr (symbol-value var))))
5928           (set var (list (symbol-value var))))
5929         (when (not (listp (cdr articles)))
5930           (setq articles (list articles)))
5931         (while articles
5932           (when (or (not (consp (setq article (pop articles))))
5933                     (< (car article) min)
5934                     (> (car article) max))
5935             (set var (delq article (symbol-value var))))))
5936        ;; Adjust ranges (sloppily).
5937        ((eq mark-type 'range)
5938         (cond
5939          ((eq mark 'seen)
5940           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5941           ;; It should be (seen (NUM1 . NUM2)).
5942           (when (numberp (cddr marks))
5943             (setcdr marks (list (cdr marks))))
5944           (setq articles (cdr marks))
5945           (while (and articles
5946                       (or (and (consp (car articles))
5947                                (> min (cdar articles)))
5948                           (and (numberp (car articles))
5949                                (> min (car articles)))))
5950             (pop articles))
5951           (set var articles))))))))
5952
5953 (defun gnus-update-missing-marks (missing)
5954   "Go through the list of MISSING articles and remove them from the mark lists."
5955   (when missing
5956     (let (var m)
5957       ;; Go through all types.
5958       (dolist (elem gnus-article-mark-lists)
5959         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5960           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5961           (when (symbol-value var)
5962             ;; This list has articles.  So we delete all missing
5963             ;; articles from it.
5964             (setq m missing)
5965             (while m
5966               (set var (delq (pop m) (symbol-value var))))))))))
5967
5968 (defun gnus-update-marks ()
5969   "Enter the various lists of marked articles into the newsgroup info list."
5970   (let ((types gnus-article-mark-lists)
5971         (info (gnus-get-info gnus-newsgroup-name))
5972         type list newmarked symbol delta-marks)
5973     (when info
5974       ;; Add all marks lists to the list of marks lists.
5975       (while (setq type (pop types))
5976         (setq list (symbol-value
5977                     (setq symbol
5978                           (intern (format "gnus-newsgroup-%s" (car type))))))
5979
5980         (when list
5981           ;; Get rid of the entries of the articles that have the
5982           ;; default score.
5983           (when (and (eq (cdr type) 'score)
5984                      gnus-save-score
5985                      list)
5986             (let* ((arts list)
5987                    (prev (cons nil list))
5988                    (all prev))
5989               (while arts
5990                 (if (or (not (consp (car arts)))
5991                         (= (cdar arts) gnus-summary-default-score))
5992                     (setcdr prev (cdr arts))
5993                   (setq prev arts))
5994                 (setq arts (cdr arts)))
5995               (setq list (cdr all)))))
5996
5997         (when (eq (cdr type) 'seen)
5998           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5999
6000         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6001           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6002
6003         (when (and (gnus-check-backend-function
6004                     'request-set-mark gnus-newsgroup-name)
6005                    (not (gnus-article-unpropagatable-p (cdr type))))
6006           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6007                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
6008                  (add (gnus-remove-from-range
6009                        (gnus-copy-sequence list) old)))
6010             (when add
6011               (push (list add 'add (list (cdr type))) delta-marks))
6012             (when del
6013               ;; Don't delete marks from outside the active range.  This
6014               ;; shouldn't happen, but is a sanity check.
6015               (setq del (gnus-sorted-range-intersection
6016                          (gnus-active gnus-newsgroup-name) del))
6017               (push (list del 'del (list (cdr type))) delta-marks))))
6018
6019         (when list
6020           (push (cons (cdr type) list) newmarked)))
6021
6022       (when delta-marks
6023         (unless (gnus-check-group gnus-newsgroup-name)
6024           (error "Can't open server for %s" gnus-newsgroup-name))
6025         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6026
6027       ;; Enter these new marks into the info of the group.
6028       (if (nthcdr 3 info)
6029           (setcar (nthcdr 3 info) newmarked)
6030         ;; Add the marks lists to the end of the info.
6031         (when newmarked
6032           (setcdr (nthcdr 2 info) (list newmarked))))
6033
6034       ;; Cut off the end of the info if there's nothing else there.
6035       (let ((i 5))
6036         (while (and (> i 2)
6037                     (not (nth i info)))
6038           (when (nthcdr (decf i) info)
6039             (setcdr (nthcdr i info) nil)))))))
6040
6041 (defun gnus-set-mode-line (where)
6042   "Set the mode line of the article or summary buffers.
6043 If WHERE is `summary', the summary mode line format will be used."
6044   ;; Is this mode line one we keep updated?
6045   (when (and (memq where gnus-updated-mode-lines)
6046              (symbol-value
6047               (intern (format "gnus-%s-mode-line-format-spec" where))))
6048     (let (mode-string)
6049       ;; We evaluate this in the summary buffer since these
6050       ;; variables are buffer-local to that buffer.
6051       (with-current-buffer gnus-summary-buffer
6052         ;; We bind all these variables that are used in the `eval' form
6053         ;; below.
6054         (let* ((mformat (symbol-value
6055                          (intern
6056                           (format "gnus-%s-mode-line-format-spec" where))))
6057                (gnus-tmp-group-name (gnus-mode-string-quote
6058                                      (gnus-group-decoded-name
6059                                       gnus-newsgroup-name)))
6060                (gnus-tmp-article-number (or gnus-current-article 0))
6061                (gnus-tmp-unread gnus-newsgroup-unreads)
6062                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6063                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6064                (gnus-tmp-unread-and-unselected
6065                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6066                             (zerop gnus-tmp-unselected))
6067                        "")
6068                       ((zerop gnus-tmp-unselected)
6069                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6070                       (t (format "{%d(+%d) more}"
6071                                  gnus-tmp-unread-and-unticked
6072                                  gnus-tmp-unselected))))
6073                (gnus-tmp-subject
6074                 (if (and gnus-current-headers
6075                          (vectorp gnus-current-headers))
6076                     (gnus-mode-string-quote
6077                      (mail-header-subject gnus-current-headers))
6078                   ""))
6079                bufname-length max-len
6080                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6081           (setq mode-string (eval mformat))
6082           (setq bufname-length (if (string-match "%b" mode-string)
6083                                    (- (length
6084                                        (buffer-name
6085                                         (if (eq where 'summary)
6086                                             nil
6087                                           (get-buffer gnus-article-buffer))))
6088                                       2)
6089                                  0))
6090           (setq max-len (max 4 (if gnus-mode-non-string-length
6091                                    (- (window-width)
6092                                       gnus-mode-non-string-length
6093                                       bufname-length)
6094                                  (length mode-string))))
6095           ;; We might have to chop a bit of the string off...
6096           (when (> (length mode-string) max-len)
6097             (setq mode-string
6098                   (concat (truncate-string-to-width mode-string (- max-len 3))
6099                           "...")))))
6100       ;; Update the mode line.
6101       (setq mode-line-buffer-identification
6102             (gnus-mode-line-buffer-identification (list mode-string)))
6103       (set-buffer-modified-p t))))
6104
6105 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6106   "Go through the HEADERS list and add all Xrefs to a hash table.
6107 The resulting hash table is returned, or nil if no Xrefs were found."
6108   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6109          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6110          (xref-hashtb (gnus-make-hashtable))
6111          start group entry number xrefs header)
6112     (while headers
6113       (setq header (pop headers))
6114       (when (and (setq xrefs (mail-header-xref header))
6115                  (not (memq (setq number (mail-header-number header))
6116                             unreads)))
6117         (setq start 0)
6118         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6119           (setq start (match-end 0))
6120           (setq group (if prefix
6121                           (concat prefix (substring xrefs (match-beginning 1)
6122                                                     (match-end 1)))
6123                         (substring xrefs (match-beginning 1) (match-end 1))))
6124           (setq number
6125                 (string-to-number (substring xrefs (match-beginning 2)
6126                                           (match-end 2))))
6127           (if (setq entry (gnus-gethash group xref-hashtb))
6128               (setcdr entry (cons number (cdr entry)))
6129             (gnus-sethash group (cons number nil) xref-hashtb)))))
6130     (and start xref-hashtb)))
6131
6132 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6133   "Look through all the headers and mark the Xrefs as read."
6134   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6135         name info xref-hashtb idlist method nth4)
6136     (with-current-buffer gnus-group-buffer
6137       (when (setq xref-hashtb
6138                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6139         (mapatoms
6140          (lambda (group)
6141            (unless (string= from-newsgroup (setq name (symbol-name group)))
6142              (setq idlist (symbol-value group))
6143              ;; Dead groups are not updated.
6144              (and (prog1
6145                       (setq info (gnus-get-info name))
6146                     (when (stringp (setq nth4 (gnus-info-method info)))
6147                       (setq nth4 (gnus-server-to-method nth4))))
6148                   ;; Only do the xrefs if the group has the same
6149                   ;; select method as the group we have just read.
6150                   (or (gnus-methods-equal-p
6151                        nth4 (gnus-find-method-for-group from-newsgroup))
6152                       virtual
6153                       (equal nth4 (setq method (gnus-find-method-for-group
6154                                                 from-newsgroup)))
6155                       (and (equal (car nth4) (car method))
6156                            (equal (nth 1 nth4) (nth 1 method))))
6157                   gnus-use-cross-reference
6158                   (or (not (eq gnus-use-cross-reference t))
6159                       virtual
6160                       ;; Only do cross-references on subscribed
6161                       ;; groups, if that is what is wanted.
6162                       (<= (gnus-info-level info) gnus-level-subscribed))
6163                   (gnus-group-make-articles-read name idlist))))
6164          xref-hashtb)))))
6165
6166 (defun gnus-compute-read-articles (group articles)
6167   (let* ((entry (gnus-group-entry group))
6168          (info (nth 2 entry))
6169          (active (gnus-active group))
6170          ninfo)
6171     (when entry
6172       ;; First peel off all invalid article numbers.
6173       (when active
6174         (let ((ids articles)
6175               id first)
6176           (while (setq id (pop ids))
6177             (when (and first (> id (cdr active)))
6178               ;; We'll end up in this situation in one particular
6179               ;; obscure situation.  If you re-scan a group and get
6180               ;; a new article that is cross-posted to a different
6181               ;; group that has not been re-scanned, you might get
6182               ;; crossposted article that has a higher number than
6183               ;; Gnus believes possible.  So we re-activate this
6184               ;; group as well.  This might mean doing the
6185               ;; crossposting thingy will *increase* the number
6186               ;; of articles in some groups.  Tsk, tsk.
6187               (setq active (or (gnus-activate-group group) active)))
6188             (when (or (> id (cdr active))
6189                       (< id (car active)))
6190               (setq articles (delq id articles))))))
6191       ;; If the read list is nil, we init it.
6192       (if (and active
6193                (null (gnus-info-read info))
6194                (> (car active) 1))
6195           (setq ninfo (cons 1 (1- (car active))))
6196         (setq ninfo (gnus-info-read info)))
6197       ;; Then we add the read articles to the range.
6198       (gnus-add-to-range
6199        ninfo (setq articles (sort articles '<))))))
6200
6201 (defun gnus-group-make-articles-read (group articles)
6202   "Update the info of GROUP to say that ARTICLES are read."
6203   (let* ((num 0)
6204          (entry (gnus-group-entry group))
6205          (info (nth 2 entry))
6206          (active (gnus-active group))
6207          range)
6208     (if (not entry)
6209         ;; Group that Gnus doesn't know exists, but still allow the
6210         ;; backend to set marks.
6211         (gnus-request-set-mark
6212          group (list (list (gnus-compress-sequence (sort articles #'<))
6213                            'add '(read))))
6214       ;; Normal, subscribed groups.
6215       (setq range (gnus-compute-read-articles group articles))
6216       (with-current-buffer gnus-group-buffer
6217         (gnus-undo-register
6218           `(progn
6219              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6220              (gnus-info-set-read ',info ',(gnus-info-read info))
6221              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6222              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6223              (gnus-group-update-group ,group t))))
6224       ;; Add the read articles to the range.
6225       (gnus-info-set-read info range)
6226       (gnus-request-set-mark group (list (list range 'add '(read))))
6227       ;; Then we have to re-compute how many unread
6228       ;; articles there are in this group.
6229       (when active
6230         (cond
6231          ((not range)
6232           (setq num (- (1+ (cdr active)) (car active))))
6233          ((not (listp (cdr range)))
6234           (setq num (- (cdr active) (- (1+ (cdr range))
6235                                        (car range)))))
6236          (t
6237           (while range
6238             (if (numberp (car range))
6239                 (setq num (1+ num))
6240               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6241             (setq range (cdr range)))
6242           (setq num (- (cdr active) num))))
6243         ;; Update the number of unread articles.
6244         (setcar entry num)
6245         ;; Update the group buffer.
6246         (unless (gnus-ephemeral-group-p group)
6247           (gnus-group-update-group group t))))))
6248
6249 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6250   (let ((cur nntp-server-buffer)
6251         (dependencies
6252          (or dependencies
6253              (with-current-buffer gnus-summary-buffer
6254                gnus-newsgroup-dependencies)))
6255         headers id end ref number
6256         (mail-parse-charset gnus-newsgroup-charset)
6257         (mail-parse-ignored-charsets
6258          (save-current-buffer (condition-case nil
6259                                   (set-buffer gnus-summary-buffer)
6260                                 (error))
6261                               gnus-newsgroup-ignored-charsets)))
6262     (with-current-buffer nntp-server-buffer
6263       ;; Translate all TAB characters into SPACE characters.
6264       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6265       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6266       (ietf-drums-unfold-fws)
6267       (gnus-run-hooks 'gnus-parse-headers-hook)
6268       (let ((case-fold-search t)
6269             in-reply-to header p lines chars)
6270         (goto-char (point-min))
6271         ;; Search to the beginning of the next header.  Error messages
6272         ;; do not begin with 2 or 3.
6273         (while (re-search-forward "^[23][0-9]+ " nil t)
6274           (setq id nil
6275                 ref nil)
6276           ;; This implementation of this function, with nine
6277           ;; search-forwards instead of the one re-search-forward and
6278           ;; a case (which basically was the old function) is actually
6279           ;; about twice as fast, even though it looks messier.  You
6280           ;; can't have everything, I guess.  Speed and elegance
6281           ;; doesn't always go hand in hand.
6282           (setq
6283            header
6284            (vector
6285             ;; Number.
6286             (prog1
6287                 (setq number (read cur))
6288               (end-of-line)
6289               (setq p (point))
6290               (narrow-to-region (point)
6291                                 (or (and (search-forward "\n.\n" nil t)
6292                                          (- (point) 2))
6293                                     (point))))
6294             ;; Subject.
6295             (progn
6296               (goto-char p)
6297               (if (search-forward "\nsubject:" nil t)
6298                   (funcall gnus-decode-encoded-word-function
6299                            (nnheader-header-value))
6300                 "(none)"))
6301             ;; From.
6302             (progn
6303               (goto-char p)
6304               (if (search-forward "\nfrom:" nil t)
6305                   (funcall gnus-decode-encoded-address-function
6306                            (nnheader-header-value))
6307                 "(nobody)"))
6308             ;; Date.
6309             (progn
6310               (goto-char p)
6311               (if (search-forward "\ndate:" nil t)
6312                   (nnheader-header-value) ""))
6313             ;; Message-ID.
6314             (progn
6315               (goto-char p)
6316               (setq id (if (re-search-forward
6317                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6318                            ;; We do it this way to make sure the Message-ID
6319                            ;; is (somewhat) syntactically valid.
6320                            (buffer-substring (match-beginning 1)
6321                                              (match-end 1))
6322                          ;; If there was no message-id, we just fake one
6323                          ;; to make subsequent routines simpler.
6324                          (nnheader-generate-fake-message-id number))))
6325             ;; References.
6326             (progn
6327               (goto-char p)
6328               (if (search-forward "\nreferences:" nil t)
6329                   (progn
6330                     (setq end (point))
6331                     (prog1
6332                         (nnheader-header-value)
6333                       (setq ref
6334                             (buffer-substring
6335                              (progn
6336                                (end-of-line)
6337                                (search-backward ">" end t)
6338                                (1+ (point)))
6339                              (progn
6340                                (search-backward "<" end t)
6341                                (point))))))
6342                 ;; Get the references from the in-reply-to header if there
6343                 ;; were no references and the in-reply-to header looks
6344                 ;; promising.
6345                 (if (and (search-forward "\nin-reply-to:" nil t)
6346                          (setq in-reply-to (nnheader-header-value))
6347                          (string-match "<[^>]+>" in-reply-to))
6348                     (let (ref2)
6349                       (setq ref (substring in-reply-to (match-beginning 0)
6350                                            (match-end 0)))
6351                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6352                         (setq ref2 (substring in-reply-to (match-beginning 0)
6353                                               (match-end 0)))
6354                         (when (> (length ref2) (length ref))
6355                           (setq ref ref2)))
6356                       ref)
6357                   (setq ref nil))))
6358             ;; Chars.
6359             (progn
6360               (goto-char p)
6361               (if (search-forward "\nchars: " nil t)
6362                   (if (numberp (setq chars (ignore-errors (read cur))))
6363                       chars -1)
6364                 -1))
6365             ;; Lines.
6366             (progn
6367               (goto-char p)
6368               (if (search-forward "\nlines: " nil t)
6369                   (if (numberp (setq lines (ignore-errors (read cur))))
6370                       lines -1)
6371                 -1))
6372             ;; Xref.
6373             (progn
6374               (goto-char p)
6375               (and (search-forward "\nxref:" nil t)
6376                    (nnheader-header-value)))
6377             ;; Extra.
6378             (when gnus-extra-headers
6379               (let ((extra gnus-extra-headers)
6380                     out)
6381                 (while extra
6382                   (goto-char p)
6383                   (when (search-forward
6384                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6385                     (push (cons (car extra) (nnheader-header-value))
6386                           out))
6387                   (pop extra))
6388                 out))))
6389           (when (equal id ref)
6390             (setq ref nil))
6391
6392           (when gnus-alter-header-function
6393             (funcall gnus-alter-header-function header)
6394             (setq id (mail-header-id header)
6395                   ref (gnus-parent-id (mail-header-references header))))
6396
6397           (when (setq header
6398                       (gnus-dependencies-add-header
6399                        header dependencies force-new))
6400             (push header headers))
6401           (goto-char (point-max))
6402           (widen))
6403         (nreverse headers)))))
6404
6405 ;; Goes through the xover lines and returns a list of vectors
6406 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6407                                                   force-new dependencies
6408                                                   group also-fetch-heads)
6409   "Parse the news overview data in the server buffer.
6410 Return a list of headers that match SEQUENCE (see
6411 `nntp-retrieve-headers')."
6412   ;; Get the Xref when the users reads the articles since most/some
6413   ;; NNTP servers do not include Xrefs when using XOVER.
6414   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6415   (let ((mail-parse-charset gnus-newsgroup-charset)
6416         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6417         (cur nntp-server-buffer)
6418         (dependencies (or dependencies gnus-newsgroup-dependencies))
6419         (allp (cond
6420                ((eq gnus-read-all-available-headers t)
6421                 t)
6422                ((and (stringp gnus-read-all-available-headers)
6423                      group)
6424                 (string-match gnus-read-all-available-headers group))
6425                (t
6426                 nil)))
6427         number headers header)
6428     (with-current-buffer nntp-server-buffer
6429       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6430       ;; Allow the user to mangle the headers before parsing them.
6431       (gnus-run-hooks 'gnus-parse-headers-hook)
6432       (goto-char (point-min))
6433       (gnus-parse-without-error
6434         (while (and (or sequence allp)
6435                     (not (eobp)))
6436           (setq number (read cur))
6437           (when (not allp)
6438             (while (and sequence
6439                         (< (car sequence) number))
6440               (setq sequence (cdr sequence))))
6441           (when (and (or allp
6442                          (and sequence
6443                               (eq number (car sequence))))
6444                      (progn
6445                        (setq sequence (cdr sequence))
6446                        (setq header (inline
6447                                       (gnus-nov-parse-line
6448                                        number dependencies force-new)))))
6449             (push header headers))
6450           (forward-line 1)))
6451       ;; A common bug in inn is that if you have posted an article and
6452       ;; then retrieves the active file, it will answer correctly --
6453       ;; the new article is included.  However, a NOV entry for the
6454       ;; article may not have been generated yet, so this may fail.
6455       ;; We work around this problem by retrieving the last few
6456       ;; headers using HEAD.
6457       (if (or (not also-fetch-heads)
6458               (not sequence))
6459           ;; We (probably) got all the headers.
6460           (nreverse headers)
6461         (let ((gnus-nov-is-evil t))
6462           (nconc
6463            (nreverse headers)
6464            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6465              (gnus-get-newsgroup-headers))))))))
6466
6467 (defun gnus-article-get-xrefs ()
6468   "Fill in the Xref value in `gnus-current-headers', if necessary.
6469 This is meant to be called in `gnus-article-internal-prepare-hook'."
6470   (let ((headers (with-current-buffer gnus-summary-buffer
6471                    gnus-current-headers)))
6472     (or (not gnus-use-cross-reference)
6473         (not headers)
6474         (and (mail-header-xref headers)
6475              (not (string= (mail-header-xref headers) "")))
6476         (let ((case-fold-search t)
6477               xref)
6478           (save-restriction
6479             (nnheader-narrow-to-headers)
6480             (goto-char (point-min))
6481             (when (or (and (not (eobp))
6482                            (eq (downcase (char-after)) ?x)
6483                            (looking-at "Xref:"))
6484                       (search-forward "\nXref:" nil t))
6485               (goto-char (1+ (match-end 0)))
6486               (setq xref (buffer-substring (point) (point-at-eol)))
6487               (mail-header-set-xref headers xref)))))))
6488
6489 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6490   "Find article ID and insert the summary line for that article.
6491 OLD-HEADER can either be a header or a line number to insert
6492 the subject line on."
6493   (let* ((line (and (numberp old-header) old-header))
6494          (old-header (and (vectorp old-header) old-header))
6495          (header (cond ((and old-header use-old-header)
6496                         old-header)
6497                        ((and (numberp id)
6498                              (gnus-number-to-header id))
6499                         (gnus-number-to-header id))
6500                        (t
6501                         (gnus-read-header id))))
6502          (number (and (numberp id) id))
6503          d)
6504     (when header
6505       ;; Rebuild the thread that this article is part of and go to the
6506       ;; article we have fetched.
6507       (when (and (not gnus-show-threads)
6508                  old-header)
6509         (when (and number
6510                    (setq d (gnus-data-find (mail-header-number old-header))))
6511           (goto-char (gnus-data-pos d))
6512           (gnus-data-remove
6513            number
6514            (- (point-at-bol)
6515               (prog1
6516                   (1+ (point-at-eol))
6517                 (gnus-delete-line))))))
6518       ;; Remove list identifiers from subject.
6519       (when gnus-list-identifiers
6520         (let ((gnus-newsgroup-headers (list header)))
6521           (gnus-summary-remove-list-identifiers)))
6522       (when old-header
6523         (mail-header-set-number header (mail-header-number old-header)))
6524       (setq gnus-newsgroup-sparse
6525             (delq (setq number (mail-header-number header))
6526                   gnus-newsgroup-sparse))
6527       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6528       (push number gnus-newsgroup-limit)
6529       (gnus-rebuild-thread (mail-header-id header) line)
6530       (gnus-summary-goto-subject number nil t))
6531     (when (and (numberp number)
6532                (> number 0))
6533       ;; We have to update the boundaries even if we can't fetch the
6534       ;; article if ID is a number -- so that the next `P' or `N'
6535       ;; command will fetch the previous (or next) article even
6536       ;; if the one we tried to fetch this time has been canceled.
6537       (when (> number gnus-newsgroup-end)
6538         (setq gnus-newsgroup-end number))
6539       (when (< number gnus-newsgroup-begin)
6540         (setq gnus-newsgroup-begin number))
6541       (setq gnus-newsgroup-unselected
6542             (delq number gnus-newsgroup-unselected)))
6543     ;; Report back a success?
6544     (and header (mail-header-number header))))
6545
6546 ;;; Process/prefix in the summary buffer
6547
6548 (defun gnus-summary-work-articles (n)
6549   "Return a list of articles to be worked upon.
6550 The prefix argument, the list of process marked articles, and the
6551 current article will be taken into consideration."
6552   (with-current-buffer gnus-summary-buffer
6553     (cond
6554      (n
6555       ;; A numerical prefix has been given.
6556       (setq n (prefix-numeric-value n))
6557       (let ((backward (< n 0))
6558             (n (abs (prefix-numeric-value n)))
6559             articles article)
6560         (save-excursion
6561           (while
6562               (and (> n 0)
6563                    (push (setq article (gnus-summary-article-number))
6564                          articles)
6565                    (if backward
6566                        (gnus-summary-find-prev nil article)
6567                      (gnus-summary-find-next nil article)))
6568             (decf n)))
6569         (nreverse articles)))
6570      ((and (gnus-region-active-p) (mark))
6571       (message "region active")
6572       ;; Work on the region between point and mark.
6573       (let ((max (max (point) (mark)))
6574             articles article)
6575         (save-excursion
6576           (goto-char (min (point) (mark)))
6577           (while
6578               (and
6579                (push (setq article (gnus-summary-article-number)) articles)
6580                (gnus-summary-find-next nil article)
6581                (< (point) max)))
6582           (nreverse articles))))
6583      (gnus-newsgroup-processable
6584       ;; There are process-marked articles present.
6585       ;; Save current state.
6586       (gnus-summary-save-process-mark)
6587       ;; Return the list.
6588       (reverse gnus-newsgroup-processable))
6589      (t
6590       ;; Just return the current article.
6591       (list (gnus-summary-article-number))))))
6592
6593 (defmacro gnus-summary-iterate (arg &rest forms)
6594   "Iterate over the process/prefixed articles and do FORMS.
6595 ARG is the interactive prefix given to the command.  FORMS will be
6596 executed with point over the summary line of the articles."
6597   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6598     `(let ((,articles (gnus-summary-work-articles ,arg)))
6599        (while ,articles
6600          (gnus-summary-goto-subject (car ,articles))
6601          ,@forms
6602          (pop ,articles)))))
6603
6604 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6605 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6606
6607 (defun gnus-summary-save-process-mark ()
6608   "Push the current set of process marked articles on the stack."
6609   (interactive)
6610   (push (copy-sequence gnus-newsgroup-processable)
6611         gnus-newsgroup-process-stack))
6612
6613 (defun gnus-summary-kill-process-mark ()
6614   "Push the current set of process marked articles on the stack and unmark."
6615   (interactive)
6616   (gnus-summary-save-process-mark)
6617   (gnus-summary-unmark-all-processable))
6618
6619 (defun gnus-summary-yank-process-mark ()
6620   "Pop the last process mark state off the stack and restore it."
6621   (interactive)
6622   (unless gnus-newsgroup-process-stack
6623     (error "Empty mark stack"))
6624   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6625
6626 (defun gnus-summary-process-mark-set (set)
6627   "Make SET into the current process marked articles."
6628   (gnus-summary-unmark-all-processable)
6629   (mapc 'gnus-summary-set-process-mark set))
6630
6631 ;;; Searching and stuff
6632
6633 (defun gnus-summary-search-group (&optional backward use-level)
6634   "Search for next unread newsgroup.
6635 If optional argument BACKWARD is non-nil, search backward instead."
6636   (with-current-buffer gnus-group-buffer
6637     (when (gnus-group-search-forward
6638            backward nil (if use-level (gnus-group-group-level) nil))
6639       (gnus-group-group-name))))
6640
6641 (defun gnus-summary-best-group (&optional exclude-group)
6642   "Find the name of the best unread group.
6643 If EXCLUDE-GROUP, do not go to this group."
6644   (with-current-buffer gnus-group-buffer
6645     (save-excursion
6646       (gnus-group-best-unread-group exclude-group))))
6647
6648 (defun gnus-summary-find-next (&optional unread article backward)
6649   (if backward
6650       (gnus-summary-find-prev unread article)
6651     (let* ((dummy (gnus-summary-article-intangible-p))
6652            (article (or article (gnus-summary-article-number)))
6653            (data (gnus-data-find-list article))
6654            result)
6655       (when (and (not dummy)
6656                  (or (not gnus-summary-check-current)
6657                      (not unread)
6658                      (not (gnus-data-unread-p (car data)))))
6659         (setq data (cdr data)))
6660       (when (setq result
6661                   (if unread
6662                       (progn
6663                         (while data
6664                           (unless (memq (gnus-data-number (car data))
6665                                         (cond
6666                                          ((eq gnus-auto-goto-ignores
6667                                               'always-undownloaded)
6668                                           gnus-newsgroup-undownloaded)
6669                                          (gnus-plugged
6670                                           nil)
6671                                          ((eq gnus-auto-goto-ignores
6672                                               'unfetched)
6673                                           gnus-newsgroup-unfetched)
6674                                          ((eq gnus-auto-goto-ignores
6675                                               'undownloaded)
6676                                           gnus-newsgroup-undownloaded)))
6677                             (when (gnus-data-unread-p (car data))
6678                               (setq result (car data)
6679                                     data nil)))
6680                           (setq data (cdr data)))
6681                         result)
6682                     (car data)))
6683         (goto-char (gnus-data-pos result))
6684         (gnus-data-number result)))))
6685
6686 (defun gnus-summary-find-prev (&optional unread article)
6687   (let* ((eobp (eobp))
6688          (article (or article (gnus-summary-article-number)))
6689          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6690          result)
6691     (when (and (not eobp)
6692                (or (not gnus-summary-check-current)
6693                    (not unread)
6694                    (not (gnus-data-unread-p (car data)))))
6695       (setq data (cdr data)))
6696     (when (setq result
6697                 (if unread
6698                     (progn
6699                       (while data
6700                         (unless (memq (gnus-data-number (car data))
6701                                       (cond
6702                                        ((eq gnus-auto-goto-ignores
6703                                             'always-undownloaded)
6704                                         gnus-newsgroup-undownloaded)
6705                                        (gnus-plugged
6706                                         nil)
6707                                        ((eq gnus-auto-goto-ignores
6708                                             'unfetched)
6709                                         gnus-newsgroup-unfetched)
6710                                        ((eq gnus-auto-goto-ignores
6711                                             'undownloaded)
6712                                         gnus-newsgroup-undownloaded)))
6713                           (when (gnus-data-unread-p (car data))
6714                             (setq result (car data)
6715                                   data nil)))
6716                         (setq data (cdr data)))
6717                       result)
6718                   (car data)))
6719       (goto-char (gnus-data-pos result))
6720       (gnus-data-number result))))
6721
6722 (defun gnus-summary-find-subject (subject &optional unread backward article)
6723   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6724          (article (or article (gnus-summary-article-number)))
6725          (articles (gnus-data-list backward))
6726          (arts (gnus-data-find-list article articles))
6727          result)
6728     (when (or (not gnus-summary-check-current)
6729               (not unread)
6730               (not (gnus-data-unread-p (car arts))))
6731       (setq arts (cdr arts)))
6732     (while arts
6733       (and (or (not unread)
6734                (gnus-data-unread-p (car arts)))
6735            (vectorp (gnus-data-header (car arts)))
6736            (gnus-subject-equal
6737             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6738            (setq result (car arts)
6739                  arts nil))
6740       (setq arts (cdr arts)))
6741     (and result
6742          (goto-char (gnus-data-pos result))
6743          (gnus-data-number result))))
6744
6745 (defun gnus-summary-search-forward (&optional unread subject backward)
6746   "Search forward for an article.
6747 If UNREAD, look for unread articles.  If SUBJECT, look for
6748 articles with that subject.  If BACKWARD, search backward instead."
6749   (cond (subject (gnus-summary-find-subject subject unread backward))
6750         (backward (gnus-summary-find-prev unread))
6751         (t (gnus-summary-find-next unread))))
6752
6753 (defun gnus-recenter (&optional n)
6754   "Center point in window and redisplay frame.
6755 Also do horizontal recentering."
6756   (interactive "P")
6757   (when (and gnus-auto-center-summary
6758              (not (eq gnus-auto-center-summary 'vertical)))
6759     (gnus-horizontal-recenter))
6760   (if (fboundp 'recenter-top-bottom)
6761       (recenter-top-bottom n)
6762     (recenter n)))
6763
6764 (put 'gnus-recenter 'isearch-scroll t)
6765
6766 (defun gnus-forward-line-ignore-invisible (n)
6767   "Move N lines forward (backward if N is negative).
6768 Like forward-line, but skip over (and don't count) invisible lines."
6769   (let (done)
6770     (while (and (> n 0) (not done))
6771       ;; If the following character is currently invisible,
6772       ;; skip all characters with that same `invisible' property value.
6773       (while (gnus-invisible-p (point))
6774         (goto-char (gnus-next-char-property-change (point))))
6775       (forward-line 1)
6776       (if (eobp)
6777           (setq done t)
6778         (setq n (1- n))))
6779     (while (and (< n 0) (not done))
6780       (forward-line -1)
6781       (if (bobp) (setq done t)
6782         (setq n (1+ n))
6783         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6784           (goto-char (gnus-previous-char-property-change (point))))))))
6785
6786 (defun gnus-summary-recenter ()
6787   "Center point in the summary window.
6788 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6789 displayed, no centering will be performed."
6790   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6791   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6792   (interactive)
6793   ;; The user has to want it.
6794   (when gnus-auto-center-summary
6795     (let* ((top (cond ((< (window-height) 4) 0)
6796                       ((< (window-height) 7) 1)
6797                       (t (if (numberp gnus-auto-center-summary)
6798                              gnus-auto-center-summary
6799                            (/ (1- (window-height)) 2)))))
6800            (height (1- (window-height)))
6801            (bottom (save-excursion
6802                      (goto-char (point-max))
6803                      (gnus-forward-line-ignore-invisible (- height))
6804                      (point)))
6805            (window (get-buffer-window (current-buffer))))
6806       (when (get-buffer-window gnus-article-buffer)
6807         ;; Only do recentering when the article buffer is displayed,
6808         ;; Set the window start to either `bottom', which is the biggest
6809         ;; possible valid number, or the second line from the top,
6810         ;; whichever is the least.
6811         (let ((top-pos (save-excursion
6812                          (gnus-forward-line-ignore-invisible (- top))
6813                          (point))))
6814           (if (> bottom top-pos)
6815               ;; Keep the second line from the top visible
6816               (set-window-start window top-pos)
6817             ;; Try to keep the bottom line visible; if it's partially
6818             ;; obscured, either scroll one more line to make it fully
6819             ;; visible, or revert to using TOP-POS.
6820             (save-excursion
6821               (goto-char (point-max))
6822               (gnus-forward-line-ignore-invisible -1)
6823               (let ((last-line-start (point)))
6824                 (goto-char bottom)
6825                 (set-window-start window (point) t)
6826                 (when (not (pos-visible-in-window-p last-line-start window))
6827                   (gnus-forward-line-ignore-invisible 1)
6828                   (set-window-start window (min (point) top-pos) t)))))))
6829       ;; Do horizontal recentering while we're at it.
6830       (when (and (get-buffer-window (current-buffer) t)
6831                  (not (eq gnus-auto-center-summary 'vertical)))
6832         (let ((selected (selected-window)))
6833           (select-window (get-buffer-window (current-buffer) t))
6834           (gnus-summary-position-point)
6835           (gnus-horizontal-recenter)
6836           (select-window selected))))))
6837
6838 (defun gnus-summary-jump-to-group (newsgroup)
6839   "Move point to NEWSGROUP in group mode buffer."
6840   ;; Keep update point of group mode buffer if visible.
6841   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6842       (save-window-excursion
6843         ;; Take care of tree window mode.
6844         (when (get-buffer-window gnus-group-buffer)
6845           (pop-to-buffer gnus-group-buffer))
6846         (gnus-group-jump-to-group newsgroup))
6847     (save-excursion
6848       ;; Take care of tree window mode.
6849       (if (get-buffer-window gnus-group-buffer 0)
6850           (pop-to-buffer gnus-group-buffer)
6851         (set-buffer gnus-group-buffer))
6852       (gnus-group-jump-to-group newsgroup))))
6853
6854 ;; This function returns a list of article numbers based on the
6855 ;; difference between the ranges of read articles in this group and
6856 ;; the range of active articles.
6857 (defun gnus-list-of-unread-articles (group)
6858   (let* ((read (gnus-info-read (gnus-get-info group)))
6859          (active (or (gnus-active group) (gnus-activate-group group)))
6860          (last (or (cdr active)
6861                    (error "Group %s couldn't be activated " group)))
6862          (bottom (if gnus-newsgroup-maximum-articles
6863                      (max (car active)
6864                           (- last gnus-newsgroup-maximum-articles -1))
6865                    (car active)))
6866          first nlast unread)
6867     ;; If none are read, then all are unread.
6868     (if (not read)
6869         (setq first bottom)
6870       ;; If the range of read articles is a single range, then the
6871       ;; first unread article is the article after the last read
6872       ;; article.  Sounds logical, doesn't it?
6873       (if (and (not (listp (cdr read)))
6874                (or (< (car read) bottom)
6875                    (progn (setq read (list read))
6876                           nil)))
6877           (setq first (max bottom (1+ (cdr read))))
6878         ;; `read' is a list of ranges.
6879         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6880                                   (caar read)))
6881                   1)
6882           (setq first bottom))
6883         (while read
6884           (when first
6885             (while (< first nlast)
6886               (setq unread (cons first unread)
6887                     first (1+ first))))
6888           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6889           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6890           (setq read (cdr read)))))
6891     ;; And add the last unread articles.
6892     (while (<= first last)
6893       (setq unread (cons first unread)
6894             first (1+ first)))
6895     ;; Return the list of unread articles.
6896     (delq 0 (nreverse unread))))
6897
6898 (defun gnus-list-of-read-articles (group)
6899   "Return a list of unread, unticked and non-dormant articles."
6900   (let* ((info (gnus-get-info group))
6901          (marked (gnus-info-marks info))
6902          (active (gnus-active group)))
6903     (and info active
6904          (gnus-list-range-difference
6905           (gnus-list-range-difference
6906            (gnus-sorted-complement
6907             (gnus-uncompress-range
6908              (if gnus-newsgroup-maximum-articles
6909                  (cons (max (car active)
6910                             (- (cdr active)
6911                                gnus-newsgroup-maximum-articles
6912                                -1))
6913                        (cdr active))
6914                active))
6915             (gnus-list-of-unread-articles group))
6916            (cdr (assq 'dormant marked)))
6917           (cdr (assq 'tick marked))))))
6918
6919 ;; This function returns a sequence of article numbers based on the
6920 ;; difference between the ranges of read articles in this group and
6921 ;; the range of active articles.
6922 (defun gnus-sequence-of-unread-articles (group)
6923   (let* ((read (gnus-info-read (gnus-get-info group)))
6924          (active (or (gnus-active group) (gnus-activate-group group)))
6925          (last (cdr active))
6926          (bottom (if gnus-newsgroup-maximum-articles
6927                      (max (car active)
6928                           (- last gnus-newsgroup-maximum-articles -1))
6929                    (car active)))
6930          first nlast unread)
6931     ;; If none are read, then all are unread.
6932     (if (not read)
6933         (setq first bottom)
6934       ;; If the range of read articles is a single range, then the
6935       ;; first unread article is the article after the last read
6936       ;; article.  Sounds logical, doesn't it?
6937       (if (and (not (listp (cdr read)))
6938                (or (< (car read) bottom)
6939                    (progn (setq read (list read))
6940                           nil)))
6941           (setq first (max bottom (1+ (cdr read))))
6942         ;; `read' is a list of ranges.
6943         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6944                                   (caar read)))
6945                   1)
6946           (setq first bottom))
6947         (while read
6948           (when first
6949             (push (cons first nlast) unread))
6950           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6951           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6952           (setq read (cdr read)))))
6953     ;; And add the last unread articles.
6954     (cond ((not (and first last))
6955            nil)
6956           ((< first last)
6957            (push (cons first last) unread))
6958           ((= first last)
6959            (push first unread)))
6960     ;; Return the sequence of unread articles.
6961     (delq 0 (nreverse unread))))
6962
6963 ;; Various summary commands
6964
6965 (defun gnus-summary-select-article-buffer ()
6966   "Reconfigure windows to show the article buffer.
6967 If `gnus-widen-article-buffer' is set, show only the article
6968 buffer."
6969   (interactive)
6970   (if (not (gnus-buffer-live-p gnus-article-buffer))
6971       (error "There is no article buffer for this summary buffer")
6972     (unless (get-buffer-window gnus-article-buffer)
6973       (gnus-summary-show-article))
6974     (gnus-configure-windows
6975      (if gnus-widen-article-window
6976          'only-article
6977        'article)
6978      t)
6979     (select-window (get-buffer-window gnus-article-buffer))))
6980
6981 (defun gnus-summary-universal-argument (arg)
6982   "Perform any operation on all articles that are process/prefixed."
6983   (interactive "P")
6984   (let ((articles (gnus-summary-work-articles arg))
6985         func article)
6986     (if (eq
6987          (setq
6988           func
6989           (key-binding
6990            (read-key-sequence
6991             (substitute-command-keys
6992              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6993          'undefined)
6994         (gnus-error 1 "Undefined key")
6995       (save-excursion
6996         (while articles
6997           (gnus-summary-goto-subject (setq article (pop articles)))
6998           (let (gnus-newsgroup-processable)
6999             (command-execute func))
7000           (gnus-summary-remove-process-mark article)))))
7001   (gnus-summary-position-point))
7002
7003 (defun gnus-summary-toggle-truncation (&optional arg)
7004   "Toggle truncation of summary lines.
7005 With ARG, turn line truncation on if ARG is positive."
7006   (interactive "P")
7007   (setq truncate-lines
7008         (if (null arg) (not truncate-lines)
7009           (> (prefix-numeric-value arg) 0)))
7010   (redraw-display))
7011
7012 (defun gnus-summary-find-for-reselect ()
7013   "Return the number of an article to stay on across a reselect.
7014 The current article is considered, then following articles, then previous
7015 articles.  An article is sought which is not cancelled and isn't a temporary
7016 insertion from another group.  If there's no such then return a dummy 0."
7017   (let (found)
7018     (dolist (rev '(nil t))
7019       (unless found      ; don't demand the reverse list if we don't need it
7020         (let ((data (gnus-data-find-list
7021                      (gnus-summary-article-number) (gnus-data-list rev))))
7022           (while (and data (not found))
7023             (if (and (< 0 (gnus-data-number (car data)))
7024                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7025                 (setq found (gnus-data-number (car data))))
7026             (setq data (cdr data))))))
7027     (or found 0)))
7028
7029 (defun gnus-summary-reselect-current-group (&optional all rescan)
7030   "Exit and then reselect the current newsgroup.
7031 The prefix argument ALL means to select all articles."
7032   (interactive "P")
7033   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7034     (error "Ephemeral groups can't be reselected"))
7035   (let ((current-subject (gnus-summary-find-for-reselect))
7036         (group gnus-newsgroup-name))
7037     (setq gnus-newsgroup-begin nil)
7038     (gnus-summary-exit nil 'leave-hidden)
7039     ;; We have to adjust the point of group mode buffer because
7040     ;; point was moved to the next unread newsgroup by exiting.
7041     (gnus-summary-jump-to-group group)
7042     (when rescan
7043       (save-excursion
7044         (gnus-group-get-new-news-this-group 1)))
7045     (gnus-group-read-group all t)
7046     (gnus-summary-goto-subject current-subject nil t)))
7047
7048 (defun gnus-summary-rescan-group (&optional all)
7049   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7050   (interactive "P")
7051   (let ((config gnus-current-window-configuration))
7052     (gnus-summary-reselect-current-group all t)
7053     (gnus-configure-windows config)
7054     (when (eq config 'article)
7055       (gnus-summary-select-article))))
7056
7057 (defun gnus-summary-update-info (&optional non-destructive)
7058   (save-excursion
7059     (let ((group gnus-newsgroup-name))
7060       (when group
7061         (when gnus-newsgroup-kill-headers
7062           (setq gnus-newsgroup-killed
7063                 (gnus-compress-sequence
7064                  (gnus-sorted-union
7065                   (gnus-list-range-intersection
7066                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7067                   gnus-newsgroup-unreads)
7068                  t)))
7069         (unless (listp (cdr gnus-newsgroup-killed))
7070           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7071         (let ((headers gnus-newsgroup-headers))
7072           ;; Set the new ranges of read articles.
7073           (with-current-buffer gnus-group-buffer
7074             (gnus-undo-force-boundary))
7075           (gnus-update-read-articles
7076            group (gnus-sorted-union
7077                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7078           ;; Set the current article marks.
7079           (let ((gnus-newsgroup-scored
7080                  (if (and (not gnus-save-score)
7081                           (not non-destructive))
7082                      nil
7083                    gnus-newsgroup-scored)))
7084             (save-excursion
7085               (gnus-update-marks)))
7086           ;; Do the cross-ref thing.
7087           (when gnus-use-cross-reference
7088             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7089           ;; Do not switch windows but change the buffer to work.
7090           (set-buffer gnus-group-buffer)
7091           (unless (gnus-ephemeral-group-p group)
7092             (gnus-group-update-group group)))))))
7093
7094 (defun gnus-summary-save-newsrc (&optional force)
7095   "Save the current number of read/marked articles in the dribble buffer.
7096 The dribble buffer will then be saved.
7097 If FORCE (the prefix), also save the .newsrc file(s)."
7098   (interactive "P")
7099   (gnus-summary-update-info t)
7100   (if force
7101       (gnus-save-newsrc-file)
7102     (gnus-dribble-save)))
7103
7104 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7105
7106 (defun gnus-summary-exit (&optional temporary leave-hidden)
7107   "Exit reading current newsgroup, and then return to group selection mode.
7108 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7109   (interactive)
7110   (gnus-set-global-variables)
7111   (when (gnus-buffer-live-p gnus-article-buffer)
7112     (with-current-buffer gnus-article-buffer
7113       (mm-destroy-parts gnus-article-mime-handles)
7114       ;; Set it to nil for safety reason.
7115       (setq gnus-article-mime-handle-alist nil)
7116       (setq gnus-article-mime-handles nil)))
7117   (gnus-kill-save-kill-buffer)
7118   (gnus-async-halt-prefetch)
7119   (let* ((group gnus-newsgroup-name)
7120          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7121          (gnus-group-is-exiting-p t)
7122          (mode major-mode)
7123          (group-point nil)
7124          (buf (current-buffer)))
7125     (unless quit-config
7126       ;; Do adaptive scoring, and possibly save score files.
7127       (when gnus-newsgroup-adaptive
7128         (gnus-score-adaptive))
7129       (when gnus-use-scoring
7130         (gnus-score-save)))
7131     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7132     (when gnus-use-cache
7133       (gnus-cache-possibly-remove-articles)
7134       (gnus-cache-save-buffers))
7135     (gnus-async-prefetch-remove-group group)
7136     (when gnus-suppress-duplicates
7137       (gnus-dup-enter-articles))
7138     (when gnus-use-trees
7139       (gnus-tree-close group))
7140     (when gnus-use-cache
7141       (gnus-cache-write-active))
7142     ;; Remove entries for this group.
7143     (nnmail-purge-split-history (gnus-group-real-name group))
7144     ;; Make all changes in this group permanent.
7145     (unless quit-config
7146       (gnus-run-hooks 'gnus-exit-group-hook)
7147       (gnus-summary-update-info))
7148     (gnus-close-group group)
7149     ;; Make sure where we were, and go to next newsgroup.
7150     (set-buffer gnus-group-buffer)
7151     (unless quit-config
7152       (gnus-group-jump-to-group group))
7153     (gnus-run-hooks 'gnus-summary-exit-hook)
7154     (unless (or quit-config
7155                 (not gnus-summary-next-group-on-exit)
7156                 ;; If this group has disappeared from the summary
7157                 ;; buffer, don't skip forwards.
7158                 (not (string= group (gnus-group-group-name))))
7159       (gnus-group-next-unread-group 1))
7160     (setq group-point (point))
7161     (if temporary
7162         nil                             ;Nothing to do.
7163       (set-buffer buf)
7164       (if (not gnus-kill-summary-on-exit)
7165           (progn
7166             (gnus-deaden-summary)
7167             (setq mode nil))
7168         (when (get-buffer gnus-article-buffer)
7169           (bury-buffer gnus-article-buffer))
7170         ;; Return to group mode buffer.
7171         (when (eq mode 'gnus-summary-mode)
7172           (gnus-kill-buffer buf)))
7173
7174       ;; If we have several article buffers, we kill them at exit.
7175       (unless gnus-single-article-buffer
7176         (when (gnus-buffer-live-p gnus-article-buffer)
7177           (with-current-buffer gnus-article-buffer
7178             ;; Don't kill sticky article buffers
7179             (unless (eq major-mode 'gnus-sticky-article-mode)
7180               (gnus-kill-buffer gnus-article-buffer)
7181               (setq gnus-article-current nil))))
7182         (gnus-kill-buffer gnus-original-article-buffer))
7183
7184       (setq gnus-current-select-method gnus-select-method)
7185       (set-buffer gnus-group-buffer)
7186       (if quit-config
7187           (gnus-handle-ephemeral-exit quit-config)
7188         (goto-char group-point)
7189         ;; If gnus-group-buffer is already displayed, make sure we also move
7190         ;; the cursor in the window that displays it.
7191         (let ((win (get-buffer-window (current-buffer) 0)))
7192           (if win (set-window-point win (point))))
7193         (unless leave-hidden
7194           (gnus-configure-windows 'group 'force)))
7195       ;; Clear the current group name.
7196       (unless quit-config
7197         (setq gnus-newsgroup-name nil)))))
7198
7199 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7200 (defun gnus-summary-exit-no-update (&optional no-questions)
7201   "Quit reading current newsgroup without updating read article info."
7202   (interactive)
7203   (let* ((group gnus-newsgroup-name)
7204          (gnus-group-is-exiting-p t)
7205          (gnus-group-is-exiting-without-update-p t)
7206          (quit-config (gnus-group-quit-config group)))
7207     (when (or no-questions
7208               gnus-expert-user
7209               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7210       (gnus-async-halt-prefetch)
7211       (run-hooks 'gnus-summary-prepare-exit-hook)
7212       (when (gnus-buffer-live-p gnus-article-buffer)
7213         (with-current-buffer gnus-article-buffer
7214           (mm-destroy-parts gnus-article-mime-handles)
7215           ;; Set it to nil for safety reason.
7216           (setq gnus-article-mime-handle-alist nil)
7217           (setq gnus-article-mime-handles nil)))
7218       ;; If we have several article buffers, we kill them at exit.
7219       (unless gnus-single-article-buffer
7220         (gnus-kill-buffer gnus-article-buffer)
7221         (gnus-kill-buffer gnus-original-article-buffer)
7222         (setq gnus-article-current nil))
7223       (if (not gnus-kill-summary-on-exit)
7224           (gnus-deaden-summary)
7225         (gnus-close-group group)
7226         (gnus-kill-buffer gnus-summary-buffer))
7227       (unless gnus-single-article-buffer
7228         (setq gnus-article-current nil))
7229       (when gnus-use-trees
7230         (gnus-tree-close group))
7231       (gnus-async-prefetch-remove-group group)
7232       (when (get-buffer gnus-article-buffer)
7233         (bury-buffer gnus-article-buffer))
7234       ;; Return to the group buffer.
7235       (gnus-configure-windows 'group 'force)
7236       ;; Clear the current group name.
7237       (setq gnus-newsgroup-name nil)
7238       (unless (gnus-ephemeral-group-p group)
7239         (gnus-group-update-group group))
7240       (when (equal (gnus-group-group-name) group)
7241         (gnus-group-next-unread-group 1))
7242       (when quit-config
7243         (gnus-handle-ephemeral-exit quit-config)))))
7244
7245 (defun gnus-handle-ephemeral-exit (quit-config)
7246   "Handle movement when leaving an ephemeral group.
7247 The state which existed when entering the ephemeral is reset."
7248   (if (not (buffer-name (car quit-config)))
7249       (gnus-configure-windows 'group 'force)
7250     (set-buffer (car quit-config))
7251     (cond ((eq major-mode 'gnus-summary-mode)
7252            (gnus-set-global-variables))
7253           ((eq major-mode 'gnus-article-mode)
7254            (save-current-buffer
7255              ;; The `gnus-summary-buffer' variable may point
7256              ;; to the old summary buffer when using a single
7257              ;; article buffer.
7258              (unless (gnus-buffer-live-p gnus-summary-buffer)
7259                (set-buffer gnus-group-buffer))
7260              (set-buffer gnus-summary-buffer)
7261              (gnus-set-global-variables))))
7262     (if (or (eq (cdr quit-config) 'article)
7263             (eq (cdr quit-config) 'pick))
7264         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7265             (gnus-configure-windows 'pick 'force)
7266           (gnus-configure-windows (cdr quit-config) 'force))
7267       (gnus-configure-windows (cdr quit-config) 'force))
7268     (when (eq major-mode 'gnus-summary-mode)
7269       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7270                                                      next-unread-noselect))
7271           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7272                                   'next-noselect)
7273                               (gnus-summary-next-subject 1 nil t))
7274                              ((eq gnus-auto-select-on-ephemeral-exit
7275                                   'next-unread-noselect)
7276                               (gnus-summary-next-subject 1 t t))))
7277             ;; Hide the article buffer which displays the article different
7278             ;; from the one that the cursor points to in the summary buffer.
7279             (gnus-configure-windows 'summary 'force))
7280         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7281                (gnus-summary-next-subject 1))
7282               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7283                (gnus-summary-next-subject 1 t))))
7284       (gnus-summary-recenter)
7285       (gnus-summary-position-point))))
7286
7287 ;;; Dead summaries.
7288
7289 (defvar gnus-dead-summary-mode-map
7290   (let ((map (make-keymap)))
7291     (suppress-keymap map)
7292     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7293     (dolist (key '("\C-d" "\r" "\177" [delete]))
7294       (define-key map key 'gnus-summary-wake-up-the-dead))
7295     (dolist (key '("q" "Q"))
7296       (define-key map key 'bury-buffer))
7297     map))
7298
7299 (define-minor-mode gnus-dead-summary-mode
7300   "Minor mode for Gnus summary buffers."
7301   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7302   (unless (derived-mode-p 'gnus-summary-mode)
7303     (setq gnus-dead-summary-mode nil)))
7304
7305 (defun gnus-deaden-summary ()
7306   "Make the current summary buffer into a dead summary buffer."
7307   ;; Kill any previous dead summary buffer.
7308   (when (and gnus-dead-summary
7309              (buffer-name gnus-dead-summary))
7310     (with-current-buffer gnus-dead-summary
7311       (when gnus-dead-summary-mode
7312         (kill-buffer (current-buffer)))))
7313   ;; Make this the current dead summary.
7314   (setq gnus-dead-summary (current-buffer))
7315   (gnus-dead-summary-mode 1)
7316   (let ((name (buffer-name)))
7317     (when (string-match "Summary" name)
7318       (rename-buffer
7319        (concat (substring name 0 (match-beginning 0)) "Dead "
7320                (substring name (match-beginning 0)))
7321        t)
7322       (bury-buffer))))
7323
7324 (defun gnus-kill-or-deaden-summary (buffer)
7325   "Kill or deaden the summary BUFFER."
7326   (save-excursion
7327     (when (and (buffer-name buffer)
7328                (not gnus-single-article-buffer))
7329       (with-current-buffer buffer
7330         (gnus-kill-buffer gnus-article-buffer)
7331         (gnus-kill-buffer gnus-original-article-buffer)))
7332     (cond
7333      ;; Kill the buffer.
7334      (gnus-kill-summary-on-exit
7335       (when (and gnus-use-trees
7336                  (gnus-buffer-exists-p buffer))
7337         (with-current-buffer buffer
7338           (gnus-tree-close gnus-newsgroup-name)))
7339       (gnus-kill-buffer buffer))
7340      ;; Deaden the buffer.
7341      ((gnus-buffer-exists-p buffer)
7342       (with-current-buffer buffer
7343         (gnus-deaden-summary))))))
7344
7345 (defun gnus-summary-wake-up-the-dead (&rest args)
7346   "Wake up the dead summary buffer."
7347   (interactive)
7348   (gnus-dead-summary-mode -1)
7349   (let ((name (buffer-name)))
7350     (when (string-match "Dead " name)
7351       (rename-buffer
7352        (concat (substring name 0 (match-beginning 0))
7353                (substring name (match-end 0)))
7354        t)))
7355   (gnus-message 3 "This dead summary is now alive again"))
7356
7357 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7358 (defun gnus-summary-describe-group (&optional force)
7359   "Describe the current newsgroup."
7360   (interactive "P")
7361   (gnus-group-describe-group force gnus-newsgroup-name))
7362
7363 (defun gnus-summary-describe-briefly ()
7364   "Describe summary mode commands briefly."
7365   (interactive)
7366   (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")))
7367
7368 ;; Walking around group mode buffer from summary mode.
7369
7370 (defun gnus-summary-next-group (&optional no-article target-group backward)
7371   "Exit current newsgroup and then select next unread newsgroup.
7372 If prefix argument NO-ARTICLE is non-nil, no article is selected
7373 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7374 previous group instead."
7375   (interactive "P")
7376   ;; Stop pre-fetching.
7377   (gnus-async-halt-prefetch)
7378   (let ((current-group gnus-newsgroup-name)
7379         (current-buffer (current-buffer))
7380         entered)
7381     ;; First we semi-exit this group to update Xrefs and all variables.
7382     ;; We can't do a real exit, because the window conf must remain
7383     ;; the same in case the user is prompted for info, and we don't
7384     ;; want the window conf to change before that...
7385     (gnus-summary-exit t)
7386     (while (not entered)
7387       ;; Then we find what group we are supposed to enter.
7388       (set-buffer gnus-group-buffer)
7389       (gnus-group-jump-to-group current-group)
7390       (setq target-group
7391             (or target-group
7392                 (if (eq gnus-keep-same-level 'best)
7393                     (gnus-summary-best-group gnus-newsgroup-name)
7394                   (gnus-summary-search-group backward gnus-keep-same-level))))
7395       (if (not target-group)
7396           ;; There are no further groups, so we return to the group
7397           ;; buffer.
7398           (progn
7399             (gnus-message 5 "Returning to the group buffer")
7400             (setq entered t)
7401             (when (gnus-buffer-live-p current-buffer)
7402               (set-buffer current-buffer)
7403               (gnus-summary-exit))
7404             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7405         ;; We try to enter the target group.
7406         (gnus-group-jump-to-group target-group)
7407         (let ((unreads (gnus-group-group-unread)))
7408           (if (and (or (eq t unreads)
7409                        (and unreads (not (zerop unreads))))
7410                    (gnus-summary-read-group
7411                     target-group nil no-article
7412                     (and (buffer-name current-buffer) current-buffer)
7413                     nil backward))
7414               (setq entered t)
7415             (setq current-group target-group
7416                   target-group nil)))))))
7417
7418 (defun gnus-summary-prev-group (&optional no-article)
7419   "Exit current newsgroup and then select previous unread newsgroup.
7420 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7421   (interactive "P")
7422   (gnus-summary-next-group no-article nil t))
7423
7424 ;; Walking around summary lines.
7425
7426 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7427   "Go to the first subject satisfying any non-nil constraint.
7428 If UNREAD is non-nil, the article should be unread.
7429 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7430 If UNSEEN is non-nil, the article should be unseen as well as unread.
7431 Returns the article selected or nil if there are no matching articles."
7432   (interactive "P")
7433   (cond
7434    ;; Empty summary.
7435    ((null gnus-newsgroup-data)
7436     (gnus-message 3 "No articles in the group")
7437     nil)
7438    ;; Pick the first article.
7439    ((not (or unread undownloaded unseen))
7440     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7441     (gnus-data-number (car gnus-newsgroup-data)))
7442    ;; Find the first unread article.
7443    (t
7444     (let ((data gnus-newsgroup-data))
7445       (while (and data
7446                   (let ((num (gnus-data-number (car data))))
7447                     (or (memq num gnus-newsgroup-unfetched)
7448                         (not (or (and unread
7449                                       (memq num gnus-newsgroup-unreads))
7450                                  (and undownloaded
7451                                       (memq num gnus-newsgroup-undownloaded))
7452                                  (and unseen
7453                                       (memq num gnus-newsgroup-unseen)
7454                                       (memq num gnus-newsgroup-unreads)))))))
7455         (setq data (cdr data)))
7456       (prog1
7457           (if data
7458               (progn
7459                 (goto-char (gnus-data-pos (car data)))
7460                 (gnus-data-number (car data)))
7461             (gnus-message 3 "No more%s articles"
7462                           (let* ((r (when unread " unread"))
7463                                  (d (when undownloaded " undownloaded"))
7464                                  (s (when unseen " unseen"))
7465                                  (l (delq nil (list r d s))))
7466                             (cond ((= 3 (length l))
7467                                    (concat r "," d ", or" s))
7468                                   ((= 2 (length l))
7469                                    (concat (car l) ", or" (cadr l)))
7470                                   ((= 1 (length l))
7471                                    (car l))
7472                                   (t
7473                                    ""))))
7474             nil
7475             )
7476         (gnus-summary-position-point))))))
7477
7478 (defun gnus-summary-next-subject (n &optional unread dont-display)
7479   "Go to next N'th summary line.
7480 If N is negative, go to the previous N'th subject line.
7481 If UNREAD is non-nil, only unread articles are selected.
7482 The difference between N and the actual number of steps taken is
7483 returned."
7484   (interactive "p")
7485   (let ((backward (< n 0))
7486         (n (abs n)))
7487     (while (and (> n 0)
7488                 (if backward
7489                     (gnus-summary-find-prev unread)
7490                   (gnus-summary-find-next unread)))
7491       (unless (zerop (setq n (1- n)))
7492         (gnus-summary-show-thread)))
7493     (when (/= 0 n)
7494       (gnus-message 7 "No more%s articles"
7495                     (if unread " unread" "")))
7496     (unless dont-display
7497       (gnus-summary-recenter)
7498       (gnus-summary-position-point))
7499     n))
7500
7501 (defun gnus-summary-next-unread-subject (n)
7502   "Go to next N'th unread summary line."
7503   (interactive "p")
7504   (gnus-summary-next-subject n t))
7505
7506 (defun gnus-summary-prev-subject (n &optional unread)
7507   "Go to previous N'th summary line.
7508 If optional argument UNREAD is non-nil, only unread article is selected."
7509   (interactive "p")
7510   (gnus-summary-next-subject (- n) unread))
7511
7512 (defun gnus-summary-prev-unread-subject (n)
7513   "Go to previous N'th unread summary line."
7514   (interactive "p")
7515   (gnus-summary-next-subject (- n) t))
7516
7517 (defun gnus-summary-goto-subjects (articles)
7518   "Insert the subject header for ARTICLES in the current buffer."
7519   (save-excursion
7520     (dolist (article articles)
7521       (gnus-summary-goto-subject article t)))
7522   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7523   (gnus-summary-position-point))
7524
7525 (defun gnus-summary-goto-subject (article &optional force silent)
7526   "Go to the subject line of ARTICLE.
7527 If FORCE, also allow jumping to articles not currently shown."
7528   (interactive "nArticle number: ")
7529   (unless (numberp article)
7530     (error "Article %s is not a number" article))
7531   (let ((b (point))
7532         (data (gnus-data-find article)))
7533     ;; We read in the article if we have to.
7534     (and (not data)
7535          force
7536          (gnus-summary-insert-subject
7537           article
7538           (if (or (numberp force) (vectorp force)) force)
7539           t)
7540          (setq data (gnus-data-find article)))
7541     (goto-char b)
7542     (if (not data)
7543         (progn
7544           (unless silent
7545             (gnus-message 3 "Can't find article %d" article))
7546           nil)
7547       (let ((pt (gnus-data-pos data)))
7548         (goto-char pt)
7549         (gnus-summary-set-article-display-arrow pt))
7550       (gnus-summary-position-point)
7551       article)))
7552
7553 ;; Walking around summary lines with displaying articles.
7554
7555 (defun gnus-summary-expand-window (&optional arg)
7556   "Make the summary buffer take up the entire Emacs frame.
7557 Given a prefix, will force an `article' buffer configuration."
7558   (interactive "P")
7559   (if arg
7560       (gnus-configure-windows 'article 'force)
7561     (gnus-configure-windows 'summary 'force)))
7562
7563 (defun gnus-summary-display-article (article &optional all-header)
7564   "Display ARTICLE in article buffer."
7565   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7566                (with-current-buffer gnus-article-buffer
7567                  (eq major-mode 'gnus-article-mode)))
7568     (gnus-article-setup-buffer))
7569   (gnus-set-global-variables)
7570   (with-current-buffer gnus-article-buffer
7571     (setq gnus-article-charset gnus-newsgroup-charset)
7572     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7573     (mm-enable-multibyte))
7574   (if (null article)
7575       nil
7576     (prog1
7577         (if gnus-summary-display-article-function
7578             (funcall gnus-summary-display-article-function article all-header)
7579           (gnus-article-prepare article all-header))
7580       (gnus-run-hooks 'gnus-select-article-hook)
7581       (when (and gnus-current-article
7582                  (not (zerop gnus-current-article)))
7583         (gnus-summary-goto-subject gnus-current-article))
7584       (gnus-summary-recenter)
7585       (when (and gnus-use-trees gnus-show-threads)
7586         (gnus-possibly-generate-tree article)
7587         (gnus-highlight-selected-tree article))
7588       ;; Successfully display article.
7589       (gnus-article-set-window-start
7590        (cdr (assq article gnus-newsgroup-bookmarks))))))
7591
7592 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7593   "Select the current article.
7594 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7595 non-nil, the article will be re-fetched even if it already present in
7596 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7597 be displayed."
7598   ;; Make sure we are in the summary buffer to work around bbdb bug.
7599   (unless (eq major-mode 'gnus-summary-mode)
7600     (set-buffer gnus-summary-buffer))
7601   (let ((article (or article (gnus-summary-article-number)))
7602         (all-headers (not (not all-headers))) ;Must be t or nil.
7603         gnus-summary-display-article-function)
7604     (and (not pseudo)
7605          (gnus-summary-article-pseudo-p article)
7606          (error "This is a pseudo-article"))
7607     (with-current-buffer gnus-summary-buffer
7608       (if (or (and gnus-single-article-buffer
7609                    (or (null gnus-current-article)
7610                        (null gnus-article-current)
7611                        (null (get-buffer gnus-article-buffer))
7612                        (not (eq article (cdr gnus-article-current)))
7613                        (not (equal (car gnus-article-current)
7614                                    gnus-newsgroup-name))
7615                        (not (get-buffer gnus-original-article-buffer))))
7616               (and (not gnus-single-article-buffer)
7617                    (or (null gnus-current-article)
7618                        (not (get-buffer gnus-original-article-buffer))
7619                        (not (eq gnus-current-article article))))
7620               force)
7621           ;; The requested article is different from the current article.
7622           (progn
7623             (gnus-summary-display-article article all-headers)
7624             (when (gnus-buffer-live-p gnus-article-buffer)
7625               (with-current-buffer gnus-article-buffer
7626                 (if (not gnus-article-decoded-p) ;; a local variable
7627                     (mm-disable-multibyte))))
7628             (gnus-article-set-window-start
7629              (cdr (assq article gnus-newsgroup-bookmarks)))
7630             article)
7631         'old))))
7632
7633 (defun gnus-summary-force-verify-and-decrypt ()
7634   "Display buttons for signed/encrypted parts and verify/decrypt them."
7635   (interactive)
7636   (let ((mm-verify-option 'known)
7637         (mm-decrypt-option 'known)
7638         (gnus-article-emulate-mime t)
7639         (gnus-buttonized-mime-types (append (list "multipart/signed"
7640                                                   "multipart/encrypted")
7641                                             gnus-buttonized-mime-types)))
7642     (gnus-summary-select-article nil 'force)))
7643
7644 (defun gnus-summary-set-current-mark (&optional current-mark)
7645   "Obsolete function."
7646   nil)
7647
7648 (defun gnus-summary-next-article (&optional unread subject backward push)
7649   "Select the next article.
7650 If UNREAD, only unread articles are selected.
7651 If SUBJECT, only articles with SUBJECT are selected.
7652 If BACKWARD, the previous article is selected instead of the next."
7653   (interactive "P")
7654   ;; Make sure we are in the summary buffer.
7655   (unless (eq major-mode 'gnus-summary-mode)
7656     (set-buffer gnus-summary-buffer))
7657   (cond
7658    ;; Is there such an article?
7659    ((and (gnus-summary-search-forward unread subject backward)
7660          (or (gnus-summary-display-article (gnus-summary-article-number))
7661              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7662     (gnus-summary-position-point))
7663    ;; If not, we try the first unread, if that is wanted.
7664    ((and subject
7665          gnus-auto-select-same
7666          (gnus-summary-first-unread-article))
7667     (gnus-summary-position-point)
7668     (gnus-message 6 "Wrapped"))
7669    ;; Try to get next/previous article not displayed in this group.
7670    ((and gnus-auto-extend-newsgroup
7671          (not unread) (not subject))
7672     (gnus-summary-goto-article
7673      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7674      nil (count-lines (point-min) (point))))
7675    ;; Go to next/previous group.
7676    (t
7677     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7678       (gnus-summary-jump-to-group gnus-newsgroup-name))
7679     (let ((cmd (if (featurep 'xemacs)
7680                    last-command-char
7681                  last-command-event))
7682           (point
7683            (with-current-buffer gnus-group-buffer
7684              (point)))
7685           (group
7686            (if (eq gnus-keep-same-level 'best)
7687                (gnus-summary-best-group gnus-newsgroup-name)
7688              (gnus-summary-search-group backward gnus-keep-same-level))))
7689       ;; Select next unread newsgroup automagically.
7690       (cond
7691        ((or (not gnus-auto-select-next)
7692             (not cmd))
7693         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7694        ((or (eq gnus-auto-select-next 'quietly)
7695             (and (eq gnus-auto-select-next 'slightly-quietly)
7696                  push)
7697             (and (eq gnus-auto-select-next 'almost-quietly)
7698                  (gnus-summary-last-article-p)))
7699         ;; Select quietly.
7700         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7701             (gnus-summary-exit)
7702           (gnus-message 7 "No more%s articles (%s)..."
7703                         (if unread " unread" "")
7704                         (if group (concat "selecting " group)
7705                           "exiting"))
7706           (gnus-summary-next-group nil group backward)))
7707        (t
7708         (when (gnus-key-press-event-p last-input-event)
7709           (gnus-summary-walk-group-buffer
7710            gnus-newsgroup-name cmd unread backward point))))))))
7711
7712 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7713   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7714                       (?\C-p (gnus-group-prev-unread-group 1))))
7715         (cursor-in-echo-area t)
7716         keve key group ended prompt)
7717     (with-current-buffer gnus-group-buffer
7718       (goto-char start)
7719       (setq group
7720             (if (eq gnus-keep-same-level 'best)
7721                 (gnus-summary-best-group gnus-newsgroup-name)
7722               (gnus-summary-search-group backward gnus-keep-same-level))))
7723     (while (not ended)
7724       (setq prompt
7725             (format
7726              "No more%s articles%s " (if unread " unread" "")
7727              (if (and group
7728                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7729                  (format " (Type %s for %s [%s])"
7730                          (single-key-description cmd)
7731                          (gnus-group-decoded-name group)
7732                          (gnus-group-unread group))
7733                (format " (Type %s to exit %s)"
7734                        (single-key-description cmd)
7735                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7736       ;; Confirm auto selection.
7737       (setq key (car (setq keve (gnus-read-event-char prompt)))
7738             ended t)
7739       (cond
7740        ((assq key keystrokes)
7741         (let ((obuf (current-buffer)))
7742           (switch-to-buffer gnus-group-buffer)
7743           (when group
7744             (gnus-group-jump-to-group group))
7745           (eval (cadr (assq key keystrokes)))
7746           (setq group (gnus-group-group-name))
7747           (switch-to-buffer obuf))
7748         (setq ended nil))
7749        ((equal key cmd)
7750         (if (or (not group)
7751                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7752             (gnus-summary-exit)
7753           (gnus-summary-next-group nil group backward)))
7754        (t
7755         (push (cdr keve) unread-command-events))))))
7756
7757 (defun gnus-summary-next-unread-article ()
7758   "Select unread article after current one."
7759   (interactive)
7760   (gnus-summary-next-article
7761    (or (not (eq gnus-summary-goto-unread 'never))
7762        (gnus-summary-last-article-p (gnus-summary-article-number)))
7763    (and gnus-auto-select-same
7764         (gnus-summary-article-subject))))
7765
7766 (defun gnus-summary-prev-article (&optional unread subject)
7767   "Select the article before the current one.
7768 If UNREAD is non-nil, only unread articles are selected."
7769   (interactive "P")
7770   (gnus-summary-next-article unread subject t))
7771
7772 (defun gnus-summary-prev-unread-article ()
7773   "Select unread article before current one."
7774   (interactive)
7775   (gnus-summary-prev-article
7776    (or (not (eq gnus-summary-goto-unread 'never))
7777        (gnus-summary-first-article-p (gnus-summary-article-number)))
7778    (and gnus-auto-select-same
7779         (gnus-summary-article-subject))))
7780
7781 (defun gnus-summary-next-page (&optional lines circular stop)
7782   "Show next page of the selected article.
7783 If at the end of the current article, select the next article.
7784 LINES says how many lines should be scrolled up.
7785
7786 If CIRCULAR is non-nil, go to the start of the article instead of
7787 selecting the next article when reaching the end of the current
7788 article.
7789
7790 If STOP is non-nil, just stop when reaching the end of the message.
7791
7792 Also see the variable `gnus-article-skip-boring'."
7793   (interactive "P")
7794   (setq gnus-summary-buffer (current-buffer))
7795   (gnus-set-global-variables)
7796   (let ((article (gnus-summary-article-number))
7797         (article-window (get-buffer-window gnus-article-buffer t))
7798         endp)
7799     ;; If the buffer is empty, we have no article.
7800     (unless article
7801       (error "No article to select"))
7802     (gnus-configure-windows 'article)
7803     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7804         (if (and (eq gnus-summary-goto-unread 'never)
7805                  (not (gnus-summary-last-article-p article)))
7806             (gnus-summary-next-article)
7807           (gnus-summary-next-unread-article))
7808       (if (or (null gnus-current-article)
7809               (null gnus-article-current)
7810               (/= article (cdr gnus-article-current))
7811               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7812           ;; Selected subject is different from current article's.
7813           (gnus-summary-display-article article)
7814         (when article-window
7815           (gnus-eval-in-buffer-window gnus-article-buffer
7816             (setq endp (or (gnus-article-next-page lines)
7817                            (gnus-article-only-boring-p))))
7818           (when endp
7819             (cond ((or stop gnus-summary-stop-at-end-of-message)
7820                    (gnus-message 3 "End of message"))
7821                   (circular
7822                    (gnus-summary-beginning-of-article))
7823                   (lines
7824                    (gnus-message 3 "End of message"))
7825                   ((null lines)
7826                    (if (and (eq gnus-summary-goto-unread 'never)
7827                             (not (gnus-summary-last-article-p article)))
7828                        (gnus-summary-next-article)
7829                      (gnus-summary-next-unread-article))))))))
7830     (gnus-summary-recenter)
7831     (gnus-summary-position-point)))
7832
7833 (defun gnus-summary-prev-page (&optional lines move)
7834   "Show previous page of selected article.
7835 Argument LINES specifies lines to be scrolled down.
7836 If MOVE, move to the previous unread article if point is at
7837 the beginning of the buffer."
7838   (interactive "P")
7839   (let ((article (gnus-summary-article-number))
7840         (article-window (get-buffer-window gnus-article-buffer t))
7841         endp)
7842     (gnus-configure-windows 'article)
7843     (if (or (null gnus-current-article)
7844             (null gnus-article-current)
7845             (/= article (cdr gnus-article-current))
7846             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7847         ;; Selected subject is different from current article's.
7848         (gnus-summary-display-article article)
7849       (gnus-summary-recenter)
7850       (when article-window
7851         (gnus-eval-in-buffer-window gnus-article-buffer
7852           (setq endp (gnus-article-prev-page lines)))
7853         (when (and move endp)
7854           (cond (lines
7855                  (gnus-message 3 "Beginning of message"))
7856                 ((null lines)
7857                  (if (and (eq gnus-summary-goto-unread 'never)
7858                           (not (gnus-summary-first-article-p article)))
7859                      (gnus-summary-prev-article)
7860                    (gnus-summary-prev-unread-article))))))))
7861   (gnus-summary-position-point))
7862
7863 (defun gnus-summary-prev-page-or-article (&optional lines)
7864   "Show previous page of selected article.
7865 Argument LINES specifies lines to be scrolled down.
7866 If at the beginning of the article, go to the next article."
7867   (interactive "P")
7868   (gnus-summary-prev-page lines t))
7869
7870 (defun gnus-summary-scroll-up (lines)
7871   "Scroll up (or down) one line current article.
7872 Argument LINES specifies lines to be scrolled up (or down if negative).
7873 If no article is selected, then the current article will be selected first."
7874   (interactive "p")
7875   (gnus-configure-windows 'article)
7876   (gnus-summary-show-thread)
7877   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7878     (gnus-eval-in-buffer-window gnus-article-buffer
7879       (cond ((> lines 0)
7880              (when (gnus-article-next-page lines)
7881                (gnus-message 3 "End of message")))
7882             ((< lines 0)
7883              (gnus-article-prev-page (- lines))))))
7884   (gnus-summary-recenter)
7885   (gnus-summary-position-point))
7886
7887 (defun gnus-summary-scroll-down (lines)
7888   "Scroll down (or up) one line current article.
7889 Argument LINES specifies lines to be scrolled down (or up if negative).
7890 If no article is selected, then the current article will be selected first."
7891   (interactive "p")
7892   (gnus-summary-scroll-up (- lines)))
7893
7894 (defun gnus-summary-next-same-subject ()
7895   "Select next article which has the same subject as current one."
7896   (interactive)
7897   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7898
7899 (defun gnus-summary-prev-same-subject ()
7900   "Select previous article which has the same subject as current one."
7901   (interactive)
7902   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7903
7904 (defun gnus-summary-next-unread-same-subject ()
7905   "Select next unread article which has the same subject as current one."
7906   (interactive)
7907   (gnus-summary-next-article t (gnus-summary-article-subject)))
7908
7909 (defun gnus-summary-prev-unread-same-subject ()
7910   "Select previous unread article which has the same subject as current one."
7911   (interactive)
7912   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7913
7914 (defun gnus-summary-first-unread-article ()
7915   "Select the first unread article.
7916 Return nil if there are no unread articles."
7917   (interactive)
7918   (prog1
7919       (when (gnus-summary-first-subject t)
7920         (gnus-summary-show-thread)
7921         (gnus-summary-first-subject t)
7922         (gnus-summary-display-article (gnus-summary-article-number)))
7923     (gnus-summary-position-point)))
7924
7925 (defun gnus-summary-first-unread-subject ()
7926   "Place the point on the subject line of the first unread article.
7927 Return nil if there are no unread articles."
7928   (interactive)
7929   (prog1
7930       (when (gnus-summary-first-subject t)
7931         (gnus-summary-show-thread)
7932         (gnus-summary-first-subject t))
7933     (gnus-summary-position-point)))
7934
7935 (defun gnus-summary-first-unseen-subject ()
7936   "Place the point on the subject line of the first unseen article.
7937 Return nil if there are no unseen articles."
7938   (interactive)
7939   (prog1
7940       (when (gnus-summary-first-subject nil nil t)
7941         (gnus-summary-show-thread)
7942         (gnus-summary-first-subject nil nil t))
7943     (gnus-summary-position-point)))
7944
7945 (defun gnus-summary-first-unseen-or-unread-subject ()
7946   "Place the point on the subject line of the first unseen and unread article.
7947 If all article have been seen, on the subject line of the first unread
7948 article."
7949   (interactive)
7950   (prog1
7951       (unless (when (gnus-summary-first-subject nil nil t)
7952                 (gnus-summary-show-thread)
7953                 (gnus-summary-first-subject nil nil t))
7954         (when (gnus-summary-first-subject t)
7955           (gnus-summary-show-thread)
7956           (gnus-summary-first-subject t)))
7957     (gnus-summary-position-point)))
7958
7959 (defun gnus-summary-first-article ()
7960   "Select the first article.
7961 Return nil if there are no articles."
7962   (interactive)
7963   (prog1
7964       (when (gnus-summary-first-subject)
7965         (gnus-summary-show-thread)
7966         (gnus-summary-first-subject)
7967         (gnus-summary-display-article (gnus-summary-article-number)))
7968     (gnus-summary-position-point)))
7969
7970 (defun gnus-summary-best-unread-article (&optional arg)
7971   "Select the unread article with the highest score.
7972 If given a prefix argument, select the next unread article that has a
7973 score higher than the default score."
7974   (interactive "P")
7975   (let ((article (if arg
7976                      (gnus-summary-better-unread-subject)
7977                    (gnus-summary-best-unread-subject))))
7978     (if article
7979         (gnus-summary-goto-article article)
7980       (error "No unread articles"))))
7981
7982 (defun gnus-summary-best-unread-subject ()
7983   "Select the unread subject with the highest score."
7984   (interactive)
7985   (let ((best -1000000)
7986         (data gnus-newsgroup-data)
7987         article score)
7988     (while data
7989       (and (gnus-data-unread-p (car data))
7990            (> (setq score
7991                     (gnus-summary-article-score (gnus-data-number (car data))))
7992               best)
7993            (setq best score
7994                  article (gnus-data-number (car data))))
7995       (setq data (cdr data)))
7996     (when article
7997       (gnus-summary-goto-subject article))
7998     (gnus-summary-position-point)
7999     article))
8000
8001 (defun gnus-summary-better-unread-subject ()
8002   "Select the first unread subject that has a score over the default score."
8003   (interactive)
8004   (let ((data gnus-newsgroup-data)
8005         article score)
8006     (while (and (setq article (gnus-data-number (car data)))
8007                 (or (gnus-data-read-p (car data))
8008                     (not (> (gnus-summary-article-score article)
8009                             gnus-summary-default-score))))
8010       (setq data (cdr data)))
8011     (when article
8012       (gnus-summary-goto-subject article))
8013     (gnus-summary-position-point)
8014     article))
8015
8016 (defun gnus-summary-last-subject ()
8017   "Go to the last displayed subject line in the group."
8018   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8019     (when article
8020       (gnus-summary-goto-subject article))))
8021
8022 (defun gnus-summary-goto-article (article &optional all-headers force)
8023   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8024 If ALL-HEADERS is non-nil, no header lines are hidden.
8025 If FORCE, go to the article even if it isn't displayed.  If FORCE
8026 is a number, it is the line the article is to be displayed on."
8027   (interactive
8028    (list
8029     (gnus-completing-read
8030      "Article number or Message-ID"
8031      (mapcar 'int-to-string gnus-newsgroup-limit))
8032     current-prefix-arg
8033     t))
8034   (prog1
8035       (if (and (stringp article)
8036                (string-match "@\\|%40" article))
8037           (gnus-summary-refer-article article)
8038         (when (stringp article)
8039           (setq article (string-to-number article)))
8040         (if (gnus-summary-goto-subject article force)
8041             (gnus-summary-display-article article all-headers)
8042           (gnus-message 4 "Couldn't go to article %s" article) nil))
8043     (gnus-summary-position-point)))
8044
8045 (defun gnus-summary-goto-last-article ()
8046   "Go to the previously read article."
8047   (interactive)
8048   (prog1
8049       (when gnus-last-article
8050         (gnus-summary-goto-article gnus-last-article nil t))
8051     (gnus-summary-position-point)))
8052
8053 (defun gnus-summary-pop-article (number)
8054   "Pop one article off the history and go to the previous.
8055 NUMBER articles will be popped off."
8056   (interactive "p")
8057   (let (to)
8058     (setq gnus-newsgroup-history
8059           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8060     (if to
8061         (gnus-summary-goto-article (car to) nil t)
8062       (error "Article history empty")))
8063   (gnus-summary-position-point))
8064
8065 ;; Summary commands and functions for limiting the summary buffer.
8066
8067 (defun gnus-summary-limit-to-articles (n)
8068   "Limit the summary buffer to the next N articles.
8069 If not given a prefix, use the process marked articles instead."
8070   (interactive "P")
8071   (prog1
8072       (let ((articles (gnus-summary-work-articles n)))
8073         (setq gnus-newsgroup-processable nil)
8074         (gnus-summary-limit articles))
8075     (gnus-summary-position-point)))
8076
8077 (defun gnus-summary-pop-limit (&optional total)
8078   "Restore the previous limit.
8079 If given a prefix, remove all limits."
8080   (interactive "P")
8081   (when total
8082     (setq gnus-newsgroup-limits
8083           (list (mapcar (lambda (h) (mail-header-number h))
8084                         gnus-newsgroup-headers))))
8085   (unless gnus-newsgroup-limits
8086     (error "No limit to pop"))
8087   (prog1
8088       (gnus-summary-limit nil 'pop)
8089     (gnus-summary-position-point)))
8090
8091 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8092   "Limit the summary buffer to articles that have subjects that match a regexp.
8093 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8094   (interactive
8095    (list (read-string (if current-prefix-arg
8096                           "Exclude subject (regexp): "
8097                         "Limit to subject (regexp): "))
8098          nil current-prefix-arg))
8099   (unless header
8100     (setq header "subject"))
8101   (when (not (equal "" subject))
8102     (prog1
8103         (let ((articles (gnus-summary-find-matching
8104                          (or header "subject") subject 'all nil nil
8105                          not-matching)))
8106           (unless articles
8107             (error "Found no matches for \"%s\"" subject))
8108           (gnus-summary-limit articles))
8109       (gnus-summary-position-point))))
8110
8111 (defun gnus-summary-limit-to-author (from &optional not-matching)
8112   "Limit the summary buffer to articles that have authors that match a regexp.
8113 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8114   (interactive
8115    (list (read-string (if current-prefix-arg
8116                           "Exclude author (regexp): "
8117                         "Limit to author (regexp): "))
8118          current-prefix-arg))
8119   (gnus-summary-limit-to-subject from "from" not-matching))
8120
8121 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8122   "Limit the summary buffer to articles with the given RECIPIENT.
8123
8124 If NOT-MATCHING, exclude RECIPIENT.
8125
8126 To and Cc headers are checked.  You need to include them in
8127 `nnmail-extra-headers'."
8128   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8129   (interactive
8130    (list (read-string (format "%s recipient (regexp): "
8131                               (if current-prefix-arg "Exclude" "Limit to")))
8132          current-prefix-arg))
8133   (when (not (equal "" recipient))
8134     (prog1 (let* ((to
8135                    (if (memq 'To nnmail-extra-headers)
8136                        (gnus-summary-find-matching
8137                         (cons 'extra 'To) recipient 'all nil nil
8138                         not-matching)
8139                      (gnus-message
8140                       1 "`To' isn't present in `nnmail-extra-headers'")
8141                      (sit-for 1)
8142                      nil))
8143                   (cc
8144                    (if (memq 'Cc nnmail-extra-headers)
8145                        (gnus-summary-find-matching
8146                         (cons 'extra 'Cc) recipient 'all nil nil
8147                         not-matching)
8148                      (gnus-message
8149                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8150                      (sit-for 1)
8151                      nil))
8152                   (articles
8153                    (if not-matching
8154                        ;; We need the numbers that are in both lists:
8155                        (mapcar (lambda (a)
8156                                  (and (memq a to) a))
8157                                cc)
8158                      (nconc to cc))))
8159              (unless articles
8160                (error "Found no matches for \"%s\"" recipient))
8161              (gnus-summary-limit articles))
8162       (gnus-summary-position-point))))
8163
8164 (defun gnus-summary-limit-to-address (address &optional not-matching)
8165   "Limit the summary buffer to articles with the given ADDRESS.
8166
8167 If NOT-MATCHING, exclude ADDRESS.
8168
8169 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8170 in `nnmail-extra-headers'."
8171   (interactive
8172    (list (read-string (format "%s address (regexp): "
8173                               (if current-prefix-arg "Exclude" "Limit to")))
8174          current-prefix-arg))
8175   (when (not (equal "" address))
8176     (prog1 (let* ((to
8177                    (if (memq 'To nnmail-extra-headers)
8178                        (gnus-summary-find-matching
8179                         (cons 'extra 'To) address 'all nil nil
8180                         not-matching)
8181                      (gnus-message
8182                       1 "`To' isn't present in `nnmail-extra-headers'")
8183                      (sit-for 1)
8184                      t))
8185                   (cc
8186                    (if (memq 'Cc nnmail-extra-headers)
8187                        (gnus-summary-find-matching
8188                         (cons 'extra 'Cc) address 'all nil nil
8189                         not-matching)
8190                      (gnus-message
8191                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8192                      (sit-for 1)
8193                      t))
8194                   (from
8195                    (gnus-summary-find-matching "from" address
8196                                                'all nil nil not-matching))
8197                   (articles
8198                    (if not-matching
8199                        ;; We need the numbers that are in all lists:
8200                        (if (eq cc t)
8201                            (if (eq to t)
8202                                from
8203                              (mapcar (lambda (a) (car (memq a from))) to))
8204                          (if (eq to t)
8205                              (mapcar (lambda (a) (car (memq a from))) cc)
8206                            (mapcar (lambda (a) (car (memq a from)))
8207                                    (mapcar (lambda (a) (car (memq a to)))
8208                                            cc))))
8209                      (nconc (if (eq to t) nil to)
8210                             (if (eq cc t) nil cc)
8211                             from))))
8212              (unless articles
8213                (error "Found no matches for \"%s\"" address))
8214              (gnus-summary-limit articles))
8215       (gnus-summary-position-point))))
8216
8217 (defun gnus-summary-limit-strange-charsets-predicate (header)
8218   (when (fboundp 'char-charset)
8219     (let ((string (concat (mail-header-subject header)
8220                           (mail-header-from header)))
8221           charset found)
8222       (dotimes (i (1- (length string)))
8223         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8224         (when (string-match "unicode\\|big\\|japanese" charset)
8225           (setq found t)))
8226       found)))
8227
8228 (defun gnus-summary-limit-to-predicate (predicate)
8229   "Limit to articles where PREDICATE returns non-nil.
8230 PREDICATE will be called with the header structures of the
8231 articles."
8232   (let ((articles nil)
8233         (case-fold-search t))
8234     (dolist (header gnus-newsgroup-headers)
8235       (when (funcall predicate header)
8236         (push (mail-header-number header) articles)))
8237     (gnus-summary-limit (nreverse articles))))
8238
8239 (defun gnus-summary-limit-to-age (age &optional younger-p)
8240   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8241 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8242 articles that are younger than AGE days."
8243   (interactive
8244    (let ((younger current-prefix-arg)
8245          (days-got nil)
8246          days)
8247      (while (not days-got)
8248        (setq days (if younger
8249                       (read-string "Limit to articles younger than (in days, older when negative): ")
8250                     (read-string
8251                      "Limit to articles older than (in days, younger when negative): ")))
8252        (when (> (length days) 0)
8253          (setq days (read days)))
8254        (if (numberp days)
8255            (progn
8256              (setq days-got t)
8257              (when (< days 0)
8258                (setq younger (not younger))
8259                (setq days (* days -1))))
8260          (message "Please enter a number.")
8261          (sleep-for 1)))
8262      (list days younger)))
8263   (prog1
8264       (let ((data gnus-newsgroup-data)
8265             (cutoff (days-to-time age))
8266             articles d date is-younger)
8267         (while (setq d (pop data))
8268           (when (and (vectorp (gnus-data-header d))
8269                      (setq date (mail-header-date (gnus-data-header d))))
8270             (setq is-younger (time-less-p
8271                               (time-since (gnus-date-get-time date))
8272                               cutoff))
8273             (when (if younger-p
8274                       is-younger
8275                     (not is-younger))
8276               (push (gnus-data-number d) articles))))
8277         (gnus-summary-limit (nreverse articles)))
8278     (gnus-summary-position-point)))
8279
8280 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8281   "Limit the summary buffer to articles that match an 'extra' header."
8282   (interactive
8283    (let ((header
8284           (intern
8285            (gnus-completing-read
8286             (if current-prefix-arg
8287                 "Exclude extra header"
8288               "Limit extra header")
8289             (mapcar 'symbol-name gnus-extra-headers)
8290             t nil nil
8291             (symbol-name (car gnus-extra-headers))))))
8292      (list header
8293            (read-string (format "%s header %s (regexp): "
8294                                 (if current-prefix-arg "Exclude" "Limit to")
8295                                 header))
8296            current-prefix-arg)))
8297   (when (not (equal "" regexp))
8298     (prog1
8299         (let ((articles (gnus-summary-find-matching
8300                          (cons 'extra header) regexp 'all nil nil
8301                          not-matching)))
8302           (unless articles
8303             (error "Found no matches for \"%s\"" regexp))
8304           (gnus-summary-limit articles))
8305       (gnus-summary-position-point))))
8306
8307 (defun gnus-summary-limit-to-display-predicate ()
8308   "Limit the summary buffer to the predicated in the `display' group parameter."
8309   (interactive)
8310   (unless gnus-newsgroup-display
8311     (error "There is no `display' group parameter"))
8312   (let (articles)
8313     (dolist (gnus-number gnus-newsgroup-articles)
8314       (when (funcall gnus-newsgroup-display)
8315         (push gnus-number articles)))
8316     (gnus-summary-limit articles))
8317   (gnus-summary-position-point))
8318
8319 (defun gnus-summary-limit-to-unread (&optional all)
8320   "Limit the summary buffer to articles that are not marked as read.
8321 If ALL is non-nil, limit strictly to unread articles."
8322   (interactive "P")
8323   (if all
8324       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8325     (gnus-summary-limit-to-marks
8326      ;; Concat all the marks that say that an article is read and have
8327      ;; those removed.
8328      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8329            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8330            gnus-low-score-mark gnus-expirable-mark
8331            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8332            gnus-duplicate-mark)
8333      'reverse)))
8334
8335 (defun gnus-summary-limit-to-headers (match &optional reverse)
8336   "Limit the summary buffer to articles that have headers that match MATCH.
8337 If REVERSE (the prefix), limit to articles that don't match."
8338   (interactive "sMatch headers (regexp): \nP")
8339   (gnus-summary-limit-to-bodies match reverse t))
8340
8341 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8342   "Limit the summary buffer to articles that have bodies that match MATCH.
8343 If REVERSE (the prefix), limit to articles that don't match."
8344   (interactive "sMatch body (regexp): \nP")
8345   (let ((articles nil)
8346         (gnus-select-article-hook nil)  ;Disable hook.
8347         (gnus-article-prepare-hook nil)
8348         (gnus-use-article-prefetch nil)
8349         (gnus-keep-backlog nil)
8350         (gnus-break-pages nil)
8351         (gnus-summary-display-arrow nil)
8352         (gnus-updated-mode-lines nil)
8353         (gnus-auto-center-summary nil)
8354         (gnus-display-mime-function nil))
8355     (dolist (data gnus-newsgroup-data)
8356       (let (gnus-mark-article-hook)
8357         (gnus-summary-select-article t t nil (gnus-data-number data)))
8358       (with-current-buffer gnus-article-buffer
8359         (article-goto-body)
8360         (let* ((case-fold-search t)
8361                (found (if headersp
8362                           (re-search-backward match nil t)
8363                         (re-search-forward match nil t))))
8364           (when (or (and found
8365                          (not reverse))
8366                     (and (not found)
8367                          reverse))
8368             (push (gnus-data-number data) articles)))))
8369     (if (not articles)
8370         (message "No messages matched")
8371       (gnus-summary-limit articles)))
8372   (gnus-summary-position-point))
8373
8374 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8375   "Limit the summary buffer to articles that aren't part on any thread.
8376 If THREADSP (the prefix), limit to articles that are in threads."
8377   (interactive "P")
8378   (let ((articles nil)
8379         thread-articles
8380         threads)
8381     (dolist (thread gnus-newsgroup-threads)
8382       (if (stringp (car thread))
8383           (dolist (thread (cdr thread))
8384             (push thread threads))
8385         (push thread threads)))
8386     (dolist (thread threads)
8387       (setq thread-articles (gnus-articles-in-thread thread))
8388       (when (or (and threadsp
8389                      (> (length thread-articles) 1))
8390                 (and (not threadsp)
8391                      (= (length thread-articles) 1)))
8392         (setq articles (nconc thread-articles articles))))
8393     (if (not articles)
8394         (message "No messages matched")
8395       (gnus-summary-limit articles))
8396     (gnus-summary-position-point)))
8397
8398 (defun gnus-summary-limit-to-replied (&optional unreplied)
8399   "Limit the summary buffer to replied articles.
8400 If UNREPLIED (the prefix), limit to unreplied articles."
8401   (interactive "P")
8402   (if unreplied
8403       (gnus-summary-limit
8404        (gnus-set-difference gnus-newsgroup-articles
8405         gnus-newsgroup-replied))
8406     (gnus-summary-limit gnus-newsgroup-replied))
8407   (gnus-summary-position-point))
8408
8409 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8410   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8411 If REVERSE, limit the summary buffer to articles that are marked
8412 with MARKS.  MARKS can either be a string of marks or a list of marks.
8413 Returns how many articles were removed."
8414   (interactive "sMarks: ")
8415   (gnus-summary-limit-to-marks marks t))
8416
8417 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8418   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8419 If REVERSE (the prefix), limit the summary buffer to articles that are
8420 not marked with MARKS.  MARKS can either be a string of marks or a
8421 list of marks.
8422 Returns how many articles were removed."
8423   (interactive "sMarks: \nP")
8424   (prog1
8425       (let ((data gnus-newsgroup-data)
8426             (marks (if (listp marks) marks
8427                      (append marks nil))) ; Transform to list.
8428             articles)
8429         (while data
8430           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8431                   (memq (gnus-data-mark (car data)) marks))
8432             (push (gnus-data-number (car data)) articles))
8433           (setq data (cdr data)))
8434         (gnus-summary-limit articles))
8435     (gnus-summary-position-point)))
8436
8437 (defun gnus-summary-limit-to-score (score)
8438   "Limit to articles with score at or above SCORE."
8439   (interactive "NLimit to articles with score of at least: ")
8440   (let ((data gnus-newsgroup-data)
8441         articles)
8442     (while data
8443       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8444                 score)
8445         (push (gnus-data-number (car data)) articles))
8446       (setq data (cdr data)))
8447     (prog1
8448         (gnus-summary-limit articles)
8449       (gnus-summary-position-point))))
8450
8451 (defun gnus-summary-limit-to-unseen ()
8452   "Limit to unseen articles."
8453   (interactive)
8454   (prog1
8455       (gnus-summary-limit gnus-newsgroup-unseen)
8456     (gnus-summary-position-point)))
8457
8458 (defun gnus-summary-limit-include-thread (id)
8459   "Display all the hidden articles that is in the thread with ID in it.
8460 When called interactively, ID is the Message-ID of the current
8461 article."
8462   (interactive (list (mail-header-id (gnus-summary-article-header))))
8463   (let ((articles (gnus-articles-in-thread
8464                    (gnus-id-to-thread (gnus-root-id id))))
8465         ;;we REALLY want the whole thread---this prevents cut-threads
8466         ;;from removing the thread we want to include.
8467         (gnus-fetch-old-headers nil)
8468         (gnus-build-sparse-threads nil))
8469     (prog1
8470         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8471       (gnus-summary-limit-include-matching-articles
8472        "subject"
8473        (regexp-quote (gnus-simplify-subject-re
8474                       (mail-header-subject (gnus-id-to-header id)))))
8475       (gnus-summary-position-point))))
8476
8477 (defun gnus-summary-limit-include-matching-articles (header regexp)
8478   "Display all the hidden articles that have HEADERs that match REGEXP."
8479   (interactive (list (read-string "Match on header: ")
8480                      (read-string "Regexp: ")))
8481   (let ((articles (gnus-find-matching-articles header regexp)))
8482     (prog1
8483         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8484       (gnus-summary-position-point))))
8485
8486 (defun gnus-summary-insert-dormant-articles ()
8487   "Insert all the dormant articles for this group into the current buffer."
8488   (interactive)
8489   (let ((gnus-verbose (max 6 gnus-verbose)))
8490     (if (not gnus-newsgroup-dormant)
8491         (gnus-message 3 "No dormant articles for this group")
8492       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8493
8494 (defun gnus-summary-insert-ticked-articles ()
8495   "Insert ticked articles for this group into the current buffer."
8496   (interactive)
8497   (let ((gnus-verbose (max 6 gnus-verbose)))
8498     (if (not gnus-newsgroup-marked)
8499         (gnus-message 3 "No ticked articles for this group")
8500       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8501
8502 (defun gnus-summary-limit-include-dormant ()
8503   "Display all the hidden articles that are marked as dormant.
8504 Note that this command only works on a subset of the articles currently
8505 fetched for this group."
8506   (interactive)
8507   (unless gnus-newsgroup-dormant
8508     (error "There are no dormant articles in this group"))
8509   (prog1
8510       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8511     (gnus-summary-position-point)))
8512
8513 (defun gnus-summary-include-articles (articles)
8514   "Fetch the headers for ARTICLES and then display the summary lines."
8515   (let ((gnus-inhibit-demon t)
8516         (gnus-agent nil)
8517         (gnus-read-all-available-headers t))
8518     (setq gnus-newsgroup-headers
8519           (gnus-merge
8520            'list gnus-newsgroup-headers
8521            (gnus-fetch-headers articles nil t)
8522            'gnus-article-sort-by-number))
8523     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8524
8525 (defun gnus-summary-limit-exclude-dormant ()
8526   "Hide all dormant articles."
8527   (interactive)
8528   (prog1
8529       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8530     (gnus-summary-position-point)))
8531
8532 (defun gnus-summary-limit-exclude-childless-dormant ()
8533   "Hide all dormant articles that have no children."
8534   (interactive)
8535   (let ((data (gnus-data-list t))
8536         articles d children)
8537     ;; Find all articles that are either not dormant or have
8538     ;; children.
8539     (while (setq d (pop data))
8540       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8541                 (and (setq children
8542                            (gnus-article-children (gnus-data-number d)))
8543                      (let (found)
8544                        (while children
8545                          (when (memq (car children) articles)
8546                            (setq children nil
8547                                  found t))
8548                          (pop children))
8549                        found)))
8550         (push (gnus-data-number d) articles)))
8551     ;; Do the limiting.
8552     (prog1
8553         (gnus-summary-limit articles)
8554       (gnus-summary-position-point))))
8555
8556 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8557   "Mark all unread excluded articles as read.
8558 If ALL, mark even excluded ticked and dormants as read."
8559   (interactive "P")
8560   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8561   (let ((articles (gnus-sorted-ndifference
8562                    (sort
8563                     (mapcar (lambda (h) (mail-header-number h))
8564                             gnus-newsgroup-headers)
8565                     '<)
8566                    gnus-newsgroup-limit))
8567         article)
8568     (setq gnus-newsgroup-unreads
8569           (gnus-sorted-intersection gnus-newsgroup-unreads
8570                                     gnus-newsgroup-limit))
8571     (if all
8572         (setq gnus-newsgroup-dormant nil
8573               gnus-newsgroup-marked nil
8574               gnus-newsgroup-reads
8575               (nconc
8576                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8577                gnus-newsgroup-reads))
8578       (while (setq article (pop articles))
8579         (unless (or (memq article gnus-newsgroup-dormant)
8580                     (memq article gnus-newsgroup-marked))
8581           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8582
8583 (defun gnus-summary-limit (articles &optional pop)
8584   (if pop
8585       ;; We pop the previous limit off the stack and use that.
8586       (setq articles (car gnus-newsgroup-limits)
8587             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8588     ;; We use the new limit, so we push the old limit on the stack.
8589     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8590   ;; Set the limit.
8591   (setq gnus-newsgroup-limit articles)
8592   (let ((total (length gnus-newsgroup-data))
8593         (data (gnus-data-find-list (gnus-summary-article-number)))
8594         (gnus-summary-mark-below nil)   ; Inhibit this.
8595         found)
8596     ;; This will do all the work of generating the new summary buffer
8597     ;; according to the new limit.
8598     (gnus-summary-prepare)
8599     ;; Hide any threads, possibly.
8600     (gnus-summary-maybe-hide-threads)
8601     ;; Try to return to the article you were at, or one in the
8602     ;; neighborhood.
8603     (when data
8604       ;; We try to find some article after the current one.
8605       (while data
8606         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8607           (setq data nil
8608                 found t))
8609         (setq data (cdr data))))
8610     (unless found
8611       ;; If there is no data, that means that we were after the last
8612       ;; article.  The same goes when we can't find any articles
8613       ;; after the current one.
8614       (goto-char (point-max))
8615       (gnus-summary-find-prev))
8616     (gnus-set-mode-line 'summary)
8617     ;; We return how many articles were removed from the summary
8618     ;; buffer as a result of the new limit.
8619     (- total (length gnus-newsgroup-data))))
8620
8621 (defsubst gnus-invisible-cut-children (threads)
8622   (let ((num 0))
8623     (while threads
8624       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8625         (incf num))
8626       (pop threads))
8627     (< num 2)))
8628
8629 (defsubst gnus-cut-thread (thread)
8630   "Go forwards in the thread until we find an article that we want to display."
8631   (when (or (eq gnus-fetch-old-headers 'some)
8632             (eq gnus-fetch-old-headers 'invisible)
8633             (numberp gnus-fetch-old-headers)
8634             (eq gnus-build-sparse-threads 'some)
8635             (eq gnus-build-sparse-threads 'more))
8636     ;; Deal with old-fetched headers and sparse threads.
8637     (while (and
8638             thread
8639             (or
8640              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8641              (gnus-summary-article-ancient-p
8642               (mail-header-number (car thread))))
8643             (if (or (<= (length (cdr thread)) 1)
8644                     (eq gnus-fetch-old-headers 'invisible))
8645                 (setq gnus-newsgroup-limit
8646                       (delq (mail-header-number (car thread))
8647                             gnus-newsgroup-limit)
8648                       thread (cadr thread))
8649               (when (gnus-invisible-cut-children (cdr thread))
8650                 (let ((th (cdr thread)))
8651                   (while th
8652                     (if (memq (mail-header-number (caar th))
8653                               gnus-newsgroup-limit)
8654                         (setq thread (car th)
8655                               th nil)
8656                       (setq th (cdr th))))))))))
8657   thread)
8658
8659 (defun gnus-cut-threads (threads)
8660   "Cut off all uninteresting articles from the beginning of THREADS."
8661   (when (or (eq gnus-fetch-old-headers 'some)
8662             (eq gnus-fetch-old-headers 'invisible)
8663             (numberp gnus-fetch-old-headers)
8664             (eq gnus-build-sparse-threads 'some)
8665             (eq gnus-build-sparse-threads 'more))
8666     (let ((th threads))
8667       (while th
8668         (setcar th (gnus-cut-thread (car th)))
8669         (setq th (cdr th)))))
8670   ;; Remove nixed out threads.
8671   (delq nil threads))
8672
8673 (defun gnus-summary-initial-limit (&optional show-if-empty)
8674   "Figure out what the initial limit is supposed to be on group entry.
8675 This entails weeding out unwanted dormants, low-scored articles,
8676 fetch-old-headers verbiage, and so on."
8677   ;; Most groups have nothing to remove.
8678   (unless (or gnus-inhibit-limiting
8679               (and (null gnus-newsgroup-dormant)
8680                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8681                    (not (eq gnus-fetch-old-headers 'some))
8682                    (not (numberp gnus-fetch-old-headers))
8683                    (not (eq gnus-fetch-old-headers 'invisible))
8684                    (null gnus-summary-expunge-below)
8685                    (not (eq gnus-build-sparse-threads 'some))
8686                    (not (eq gnus-build-sparse-threads 'more))
8687                    (null gnus-thread-expunge-below)))
8688     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8689     (setq gnus-newsgroup-limit nil)
8690     (mapatoms
8691      (lambda (node)
8692        (unless (car (symbol-value node))
8693          ;; These threads have no parents -- they are roots.
8694          (let ((nodes (cdr (symbol-value node)))
8695                thread)
8696            (while nodes
8697              (if (and gnus-thread-expunge-below
8698                       (< (gnus-thread-total-score (car nodes))
8699                          gnus-thread-expunge-below))
8700                  (gnus-expunge-thread (pop nodes))
8701                (setq thread (pop nodes))
8702                (gnus-summary-limit-children thread))))))
8703      gnus-newsgroup-dependencies)
8704     ;; If this limitation resulted in an empty group, we might
8705     ;; pop the previous limit and use it instead.
8706     (when (and (not gnus-newsgroup-limit)
8707                show-if-empty)
8708       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8709     gnus-newsgroup-limit))
8710
8711 (defun gnus-summary-limit-children (thread)
8712   "Return 1 if this subthread is visible and 0 if it is not."
8713   ;; First we get the number of visible children to this thread.  This
8714   ;; is done by recursing down the thread using this function, so this
8715   ;; will really go down to a leaf article first, before slowly
8716   ;; working its way up towards the root.
8717   (when thread
8718     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8719            (children
8720            (if (cdr thread)
8721                (apply '+ (mapcar 'gnus-summary-limit-children
8722                                  (cdr thread)))
8723              0))
8724            (number (mail-header-number (car thread)))
8725            score)
8726       (if (and
8727            (not (memq number gnus-newsgroup-marked))
8728            (or
8729             ;; If this article is dormant and has absolutely no visible
8730             ;; children, then this article isn't visible.
8731             (and (memq number gnus-newsgroup-dormant)
8732                  (zerop children))
8733             ;; If this is "fetch-old-headered" and there is no
8734             ;; visible children, then we don't want this article.
8735             (and (or (eq gnus-fetch-old-headers 'some)
8736                      (numberp gnus-fetch-old-headers))
8737                  (gnus-summary-article-ancient-p number)
8738                  (zerop children))
8739             ;; If this is "fetch-old-headered" and `invisible', then
8740             ;; we don't want this article.
8741             (and (eq gnus-fetch-old-headers 'invisible)
8742                  (gnus-summary-article-ancient-p number))
8743             ;; If this is a sparsely inserted article with no children,
8744             ;; we don't want it.
8745             (and (eq gnus-build-sparse-threads 'some)
8746                  (gnus-summary-article-sparse-p number)
8747                  (zerop children))
8748             ;; If we use expunging, and this article is really
8749             ;; low-scored, then we don't want this article.
8750             (when (and gnus-summary-expunge-below
8751                        (< (setq score
8752                                 (or (cdr (assq number gnus-newsgroup-scored))
8753                                     gnus-summary-default-score))
8754                           gnus-summary-expunge-below))
8755               ;; We increase the expunge-tally here, but that has
8756               ;; nothing to do with the limits, really.
8757               (incf gnus-newsgroup-expunged-tally)
8758               ;; We also mark as read here, if that's wanted.
8759               (when (and gnus-summary-mark-below
8760                          (< score gnus-summary-mark-below))
8761                 (setq gnus-newsgroup-unreads
8762                       (delq number gnus-newsgroup-unreads))
8763                 (if gnus-newsgroup-auto-expire
8764                     (push number gnus-newsgroup-expirable)
8765                   (push (cons number gnus-low-score-mark)
8766                         gnus-newsgroup-reads)))
8767               t)
8768             ;; Do the `display' group parameter.
8769             (and gnus-newsgroup-display
8770                  (let ((gnus-number number))
8771                    (not (funcall gnus-newsgroup-display))))))
8772           ;; Nope, invisible article.
8773           0
8774         ;; Ok, this article is to be visible, so we add it to the limit
8775         ;; and return 1.
8776         (push number gnus-newsgroup-limit)
8777         1))))
8778
8779 (defun gnus-expunge-thread (thread)
8780   "Mark all articles in THREAD as read."
8781   (let* ((number (mail-header-number (car thread))))
8782     (incf gnus-newsgroup-expunged-tally)
8783     ;; We also mark as read here, if that's wanted.
8784     (setq gnus-newsgroup-unreads
8785           (delq number gnus-newsgroup-unreads))
8786     (if gnus-newsgroup-auto-expire
8787         (push number gnus-newsgroup-expirable)
8788       (push (cons number gnus-low-score-mark)
8789             gnus-newsgroup-reads)))
8790   ;; Go recursively through all subthreads.
8791   (mapcar 'gnus-expunge-thread (cdr thread)))
8792
8793 ;; Summary article oriented commands
8794
8795 (defun gnus-summary-refer-parent-article (n)
8796   "Refer parent article N times.
8797 If N is negative, go to ancestor -N instead.
8798 The difference between N and the number of articles fetched is returned."
8799   (interactive "p")
8800   (let ((skip 1)
8801         error header ref)
8802     (when (not (natnump n))
8803       (setq skip (abs n)
8804             n 1))
8805     (while (and (> n 0)
8806                 (not error))
8807       (setq header (gnus-summary-article-header))
8808       (if (and (eq (mail-header-number header)
8809                    (cdr gnus-article-current))
8810                (equal gnus-newsgroup-name
8811                       (car gnus-article-current)))
8812           ;; If we try to find the parent of the currently
8813           ;; displayed article, then we take a look at the actual
8814           ;; References header, since this is slightly more
8815           ;; reliable than the References field we got from the
8816           ;; server.
8817           (with-current-buffer gnus-original-article-buffer
8818             (nnheader-narrow-to-headers)
8819             (unless (setq ref (message-fetch-field "references"))
8820               (when (setq ref (message-fetch-field "in-reply-to"))
8821                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8822             (widen))
8823         (setq ref
8824               ;; It's not the current article, so we take a bet on
8825               ;; the value we got from the server.
8826               (mail-header-references header)))
8827       (if (and ref
8828                (not (equal ref "")))
8829           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8830             (gnus-message 1 "Couldn't find parent"))
8831         (gnus-message 1 "No references in article %d"
8832                       (gnus-summary-article-number))
8833         (setq error t))
8834       (decf n))
8835     (gnus-summary-position-point)
8836     n))
8837
8838 (defun gnus-summary-refer-references ()
8839   "Fetch all articles mentioned in the References header.
8840 Return the number of articles fetched."
8841   (interactive)
8842   (let ((ref (mail-header-references (gnus-summary-article-header)))
8843         (current (gnus-summary-article-number))
8844         (n 0))
8845     (if (or (not ref)
8846             (equal ref ""))
8847         (error "No References in the current article")
8848       ;; For each Message-ID in the References header...
8849       (while (string-match "<[^>]*>" ref)
8850         (incf n)
8851         ;; ... fetch that article.
8852         (gnus-summary-refer-article
8853          (prog1 (match-string 0 ref)
8854            (setq ref (substring ref (match-end 0))))))
8855       (gnus-summary-goto-subject current)
8856       (gnus-summary-position-point)
8857       n)))
8858
8859 (defun gnus-summary-refer-thread (&optional limit)
8860   "Fetch all articles in the current thread.
8861 If no backend-specific 'request-thread function is available
8862 fetch LIMIT (the numerical prefix) old headers. If LIMIT is nil
8863 fetch what's specified by the `gnus-refer-thread-limit'
8864 variable."
8865   (interactive "P")
8866   (gnus-warp-to-article)
8867   (let* ((header (gnus-summary-article-header))
8868          (id (mail-header-id header))
8869          (gnus-inhibit-demon t)
8870          (gnus-summary-ignore-duplicates t)
8871          (gnus-read-all-available-headers t)
8872          (limit (if limit (prefix-numeric-value limit)
8873                   gnus-refer-thread-limit)))
8874     (setq gnus-newsgroup-headers
8875           (gnus-merge
8876            'list gnus-newsgroup-headers
8877            (if (gnus-check-backend-function
8878                 'request-thread gnus-newsgroup-name)
8879                (gnus-request-thread header)
8880              (let* ((last (if (numberp limit)
8881                               (min (+ (mail-header-number header)
8882                                       limit)
8883                                    gnus-newsgroup-highest)
8884                             gnus-newsgroup-highest))
8885                     (subject (gnus-simplify-subject
8886                               (mail-header-subject header)))
8887                     (refs (split-string (or (mail-header-references header)
8888                                             "")))
8889                     (gnus-parse-headers-hook
8890                      (lambda () (goto-char (point-min))
8891                        (keep-lines
8892                         (regexp-opt (append refs (list id subject)))))))
8893                (gnus-fetch-headers (list last) (if (numberp limit)
8894                                                    (* 2 limit) limit) t)))
8895            'gnus-article-sort-by-number))
8896     (gnus-summary-limit-include-thread id)))
8897
8898 (defun gnus-summary-refer-article (message-id)
8899   "Fetch an article specified by MESSAGE-ID."
8900   (interactive "sMessage-ID: ")
8901   (when (and (stringp message-id)
8902              (not (zerop (length message-id))))
8903     (setq message-id (gnus-replace-in-string message-id " " ""))
8904     ;; Construct the correct Message-ID if necessary.
8905     ;; Suggested by tale@pawl.rpi.edu.
8906     (unless (string-match "^<" message-id)
8907       (setq message-id (concat "<" message-id)))
8908     (unless (string-match ">$" message-id)
8909       (setq message-id (concat message-id ">")))
8910     ;; People often post MIDs from URLs, so unhex it:
8911     (unless (string-match "@" message-id)
8912       (setq message-id (gnus-url-unhex-string message-id)))
8913     (let* ((header (gnus-id-to-header message-id))
8914            (sparse (and header
8915                         (gnus-summary-article-sparse-p
8916                          (mail-header-number header))
8917                         (memq (mail-header-number header)
8918                               gnus-newsgroup-limit)))
8919            number)
8920       (cond
8921        ;; If the article is present in the buffer we just go to it.
8922        ((and header
8923              (or (not (gnus-summary-article-sparse-p
8924                        (mail-header-number header)))
8925                  sparse))
8926         (prog1
8927             (gnus-summary-goto-article
8928              (mail-header-number header) nil t)
8929           (when sparse
8930             (gnus-summary-update-article (mail-header-number header)))))
8931        (t
8932         ;; We fetch the article.
8933         (catch 'found
8934           (dolist (gnus-override-method (gnus-refer-article-methods))
8935             (when (and (gnus-check-server gnus-override-method)
8936                        ;; Fetch the header,
8937                        (setq number (gnus-summary-insert-subject message-id)))
8938               ;; and display the article.
8939               (gnus-summary-select-article nil nil nil number)
8940               (throw 'found t)))
8941           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8942
8943 (defun gnus-refer-article-methods ()
8944   "Return a list of referable methods."
8945   (cond
8946    ;; No method, so we default to current and native.
8947    ((null gnus-refer-article-method)
8948     (list gnus-current-select-method gnus-select-method))
8949    ;; Current.
8950    ((eq 'current gnus-refer-article-method)
8951     (list gnus-current-select-method))
8952    ;; List of select methods.
8953    ((not (and (symbolp (car gnus-refer-article-method))
8954               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8955     (let (out)
8956       (dolist (method gnus-refer-article-method)
8957         (push (if (eq 'current method)
8958                   gnus-current-select-method
8959                 method)
8960               out))
8961       (nreverse out)))
8962    ;; One single select method.
8963    (t
8964     (list gnus-refer-article-method))))
8965
8966 (defun gnus-summary-edit-parameters ()
8967   "Edit the group parameters of the current group."
8968   (interactive)
8969   (gnus-group-edit-group gnus-newsgroup-name 'params))
8970
8971 (defun gnus-summary-customize-parameters ()
8972   "Customize the group parameters of the current group."
8973   (interactive)
8974   (gnus-group-customize gnus-newsgroup-name))
8975
8976 (defun gnus-summary-enter-digest-group (&optional force)
8977   "Enter an nndoc group based on the current article.
8978 If FORCE, force a digest interpretation.  If not, try to guess
8979 what the document format is.
8980
8981 To control what happens when you exit the group, see the
8982 `gnus-auto-select-on-ephemeral-exit' variable."
8983   (interactive "P")
8984   (let ((conf gnus-current-window-configuration))
8985     (save-window-excursion
8986       (save-excursion
8987         (let (gnus-article-prepare-hook
8988               gnus-display-mime-function
8989               gnus-break-pages)
8990           (gnus-summary-select-article))))
8991     (setq gnus-current-window-configuration conf)
8992     (let* ((name (format "%s-%d"
8993                          (gnus-group-prefixed-name
8994                           gnus-newsgroup-name (list 'nndoc ""))
8995                          (with-current-buffer gnus-summary-buffer
8996                            gnus-current-article)))
8997            (ogroup gnus-newsgroup-name)
8998            (params (append (gnus-info-params (gnus-get-info ogroup))
8999                            (list (cons 'to-group ogroup))
9000                            (list (cons 'parent-group ogroup))
9001                            (list (cons 'save-article-group ogroup))))
9002            (case-fold-search t)
9003            (buf (current-buffer))
9004            dig to-address)
9005       (with-current-buffer gnus-original-article-buffer
9006         ;; Have the digest group inherit the main mail address of
9007         ;; the parent article.
9008         (when (setq to-address (or (gnus-fetch-field "reply-to")
9009                                    (gnus-fetch-field "from")))
9010           (setq params
9011                 (append
9012                  (list (cons 'to-address
9013                              (funcall gnus-decode-encoded-address-function
9014                                       to-address))))))
9015         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9016         (insert-buffer-substring gnus-original-article-buffer)
9017         ;; Remove lines that may lead nndoc to misinterpret the
9018         ;; document type.
9019         (narrow-to-region
9020          (goto-char (point-min))
9021          (or (search-forward "\n\n" nil t) (point)))
9022         (goto-char (point-min))
9023         (delete-matching-lines "^Path:\\|^From ")
9024         (widen))
9025       (unwind-protect
9026           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9027                     (gnus-newsgroup-ephemeral-ignored-charsets
9028                      gnus-newsgroup-ignored-charsets))
9029                 (gnus-group-read-ephemeral-group
9030                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9031                               (nndoc-article-type
9032                                ,(if force 'mbox 'guess)))
9033                  t nil nil nil
9034                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9035                                                         "ADAPT")))))
9036               ;; Make all postings to this group go to the parent group.
9037               (nconc (gnus-info-params (gnus-get-info name))
9038                      params)
9039             ;; Couldn't select this doc group.
9040             (switch-to-buffer buf)
9041             (gnus-set-global-variables)
9042             (gnus-configure-windows 'summary)
9043             (gnus-message 3 "Article couldn't be entered?"))
9044         (kill-buffer dig)))))
9045
9046 (defun gnus-summary-read-document (n)
9047   "Open a new group based on the current article(s).
9048 This will allow you to read digests and other similar
9049 documents as newsgroups.
9050 Obeys the standard process/prefix convention."
9051   (interactive "P")
9052   (let* ((ogroup gnus-newsgroup-name)
9053          (params (append (gnus-info-params (gnus-get-info ogroup))
9054                          (list (cons 'to-group ogroup))))
9055          group egroup groups vgroup)
9056     (dolist (article (gnus-summary-work-articles n))
9057       (setq group (format "%s-%d" gnus-newsgroup-name article))
9058       (gnus-summary-remove-process-mark article)
9059       (when (gnus-summary-display-article article)
9060         (save-excursion ;;What for?
9061           (with-temp-buffer
9062             (insert-buffer-substring gnus-original-article-buffer)
9063             ;; Remove some headers that may lead nndoc to make
9064             ;; the wrong guess.
9065             (message-narrow-to-head)
9066             (goto-char (point-min))
9067             (delete-matching-lines "^Path:\\|^From ")
9068             (widen)
9069             (if (setq egroup
9070                       (gnus-group-read-ephemeral-group
9071                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9072                                      (nndoc-article-type guess))
9073                        t nil t))
9074                 (progn
9075                   ;; Make all postings to this group go to the parent group.
9076                   (nconc (gnus-info-params (gnus-get-info egroup))
9077                          params)
9078                   (push egroup groups))
9079               ;; Couldn't select this doc group.
9080               (gnus-error 3 "Article couldn't be entered"))))))
9081     ;; Now we have selected all the documents.
9082     (cond
9083      ((not groups)
9084       (error "None of the articles could be interpreted as documents"))
9085      ((gnus-group-read-ephemeral-group
9086        (setq vgroup (format
9087                      "nnvirtual:%s-%s" gnus-newsgroup-name
9088                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9089        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9090        t
9091        (cons (current-buffer) 'summary)))
9092      (t
9093       (error "Couldn't select virtual nndoc group")))))
9094
9095 (defun gnus-summary-widget-forward (arg)
9096   "Move point to the next field or button in the article.
9097 With optional ARG, move across that many fields."
9098   (interactive "p")
9099   (gnus-summary-select-article)
9100   (gnus-configure-windows 'article)
9101   (select-window (gnus-get-buffer-window gnus-article-buffer))
9102   (widget-forward arg))
9103
9104 (defun gnus-summary-isearch-article (&optional regexp-p)
9105   "Do incremental search forward on the current article.
9106 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9107   (interactive "P")
9108   (gnus-summary-select-article)
9109   (gnus-configure-windows 'article)
9110   (gnus-eval-in-buffer-window gnus-article-buffer
9111     (save-restriction
9112       (widen)
9113       (isearch-forward regexp-p))))
9114
9115 (defun gnus-summary-repeat-search-article-forward ()
9116   "Repeat the previous search forwards."
9117   (interactive)
9118   (unless gnus-last-search-regexp
9119     (error "No previous search"))
9120   (gnus-summary-search-article-forward gnus-last-search-regexp))
9121
9122 (defun gnus-summary-repeat-search-article-backward ()
9123   "Repeat the previous search backwards."
9124   (interactive)
9125   (unless gnus-last-search-regexp
9126     (error "No previous search"))
9127   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9128
9129 (defun gnus-summary-search-article-forward (regexp &optional backward)
9130   "Search for an article containing REGEXP forward.
9131 If BACKWARD, search backward instead."
9132   (interactive
9133    (list (read-string
9134           (format "Search article %s (regexp%s): "
9135                   (if current-prefix-arg "backward" "forward")
9136                   (if gnus-last-search-regexp
9137                       (concat ", default " gnus-last-search-regexp)
9138                     "")))
9139          current-prefix-arg))
9140   (if (string-equal regexp "")
9141       (setq regexp (or gnus-last-search-regexp ""))
9142     (setq gnus-last-search-regexp regexp)
9143     (setq gnus-article-before-search gnus-current-article))
9144   ;; Intentionally set gnus-last-article.
9145   (setq gnus-last-article gnus-article-before-search)
9146   (let ((gnus-last-article gnus-last-article))
9147     (if (gnus-summary-search-article regexp backward)
9148         (gnus-summary-show-thread)
9149       (signal 'search-failed (list regexp)))))
9150
9151 (defun gnus-summary-search-article-backward (regexp)
9152   "Search for an article containing REGEXP backward."
9153   (interactive
9154    (list (read-string
9155           (format "Search article backward (regexp%s): "
9156                   (if gnus-last-search-regexp
9157                       (concat ", default " gnus-last-search-regexp)
9158                     "")))))
9159   (gnus-summary-search-article-forward regexp 'backward))
9160
9161 (defun gnus-summary-search-article (regexp &optional backward)
9162   "Search for an article containing REGEXP.
9163 Optional argument BACKWARD means do search for backward.
9164 `gnus-select-article-hook' is not called during the search."
9165   ;; We have to require this here to make sure that the following
9166   ;; dynamic binding isn't shadowed by autoloading.
9167   (require 'gnus-async)
9168   (require 'gnus-art)
9169   (let ((gnus-select-article-hook nil)  ;Disable hook.
9170         (gnus-article-prepare-hook nil)
9171         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9172         (gnus-use-article-prefetch nil)
9173         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9174         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9175         (gnus-visual nil)
9176         (gnus-keep-backlog nil)
9177         (gnus-break-pages nil)
9178         (gnus-summary-display-arrow nil)
9179         (gnus-updated-mode-lines nil)
9180         (gnus-auto-center-summary nil)
9181         (sum (current-buffer))
9182         (gnus-display-mime-function nil)
9183         (found nil)
9184         point)
9185     (gnus-save-hidden-threads
9186       (gnus-summary-select-article)
9187       (set-buffer gnus-article-buffer)
9188       (goto-char (window-point (get-buffer-window (current-buffer))))
9189       (when backward
9190         (forward-line -1))
9191       (while (not found)
9192         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9193         (if (if backward
9194                 (re-search-backward regexp nil t)
9195               (re-search-forward regexp nil t))
9196             ;; We found the regexp.
9197             (progn
9198               (setq found 'found)
9199               (beginning-of-line)
9200               (set-window-start
9201                (get-buffer-window (current-buffer))
9202                (point))
9203               (forward-line 1)
9204               (set-window-point
9205                (get-buffer-window (current-buffer))
9206                (point))
9207               (set-buffer sum)
9208               (setq point (point)))
9209           ;; We didn't find it, so we go to the next article.
9210           (set-buffer sum)
9211           (setq found 'not)
9212           (while (eq found 'not)
9213             (if (not (if backward (gnus-summary-find-prev)
9214                        (gnus-summary-find-next)))
9215                 ;; No more articles.
9216                 (setq found t)
9217               ;; Select the next article and adjust point.
9218               (unless (gnus-summary-article-sparse-p
9219                        (gnus-summary-article-number))
9220                 (setq found nil)
9221                 (gnus-summary-select-article)
9222                 (set-buffer gnus-article-buffer)
9223                 (widen)
9224                 (goto-char (if backward (point-max) (point-min))))))))
9225       (gnus-message 7 ""))
9226     ;; Return whether we found the regexp.
9227     (when (eq found 'found)
9228       (goto-char point)
9229       (gnus-summary-show-thread)
9230       (gnus-summary-goto-subject gnus-current-article)
9231       (gnus-summary-position-point)
9232       t)))
9233
9234 (defun gnus-find-matching-articles (header regexp)
9235   "Return a list of all articles that match REGEXP on HEADER.
9236 This search includes all articles in the current group that Gnus has
9237 fetched headers for, whether they are displayed or not."
9238   (let ((articles nil)
9239         ;; Can't eta-reduce because it's a macro.
9240         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9241         (case-fold-search t))
9242     (dolist (header gnus-newsgroup-headers)
9243       (when (string-match regexp (funcall func header))
9244         (push (mail-header-number header) articles)))
9245     (nreverse articles)))
9246
9247 (defun gnus-summary-find-matching (header regexp &optional backward unread
9248                                           not-case-fold not-matching)
9249   "Return a list of all articles that match REGEXP on HEADER.
9250 The search stars on the current article and goes forwards unless
9251 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9252 If UNREAD is non-nil, only unread articles will
9253 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9254 in the comparisons. If NOT-MATCHING, return a list of all articles that
9255 not match REGEXP on HEADER."
9256   (let ((case-fold-search (not not-case-fold))
9257         articles d func)
9258     (if (consp header)
9259         (if (eq (car header) 'extra)
9260             (setq func
9261                   `(lambda (h)
9262                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9263                          "")))
9264           (error "%s is an invalid header" header))
9265       (unless (fboundp (intern (concat "mail-header-" header)))
9266         (error "%s is not a valid header" header))
9267       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9268     (dolist (d (if (eq backward 'all)
9269                    gnus-newsgroup-data
9270                  (gnus-data-find-list
9271                   (gnus-summary-article-number)
9272                   (gnus-data-list backward))))
9273       (when (and (or (not unread)       ; We want all articles...
9274                      (gnus-data-unread-p d)) ; Or just unreads.
9275                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9276                  (if not-matching
9277                      (not (string-match
9278                            regexp
9279                            (funcall func (gnus-data-header d))))
9280                    (string-match regexp
9281                                  (funcall func (gnus-data-header d)))))
9282         (push (gnus-data-number d) articles))) ; Success!
9283     (nreverse articles)))
9284
9285 (defun gnus-summary-execute-command (header regexp command &optional backward)
9286   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9287 If HEADER is an empty string (or nil), the match is done on the entire
9288 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9289   (interactive
9290    (list (let ((completion-ignore-case t))
9291            (gnus-completing-read
9292             "Header name"
9293             (mapcar 'symbol-name
9294                     (append
9295                      '(Number Subject From Lines Date
9296                        Message-ID Xref References Body)
9297                      gnus-extra-headers))
9298             'require-match))
9299          (read-string "Regexp: ")
9300          (read-key-sequence "Command: ")
9301          current-prefix-arg))
9302   (when (equal header "Body")
9303     (setq header ""))
9304   ;; Hidden thread subtrees must be searched as well.
9305   (gnus-summary-show-all-threads)
9306   ;; We don't want to change current point nor window configuration.
9307   (save-excursion
9308     (save-window-excursion
9309       (let (gnus-visual
9310             gnus-treat-strip-trailing-blank-lines
9311             gnus-treat-strip-leading-blank-lines
9312             gnus-treat-strip-multiple-blank-lines
9313             gnus-treat-hide-boring-headers
9314             gnus-treat-fold-newsgroups
9315             gnus-article-prepare-hook)
9316         (gnus-message 6 "Executing %s..." (key-description command))
9317         ;; We'd like to execute COMMAND interactively so as to give arguments.
9318         (gnus-execute header regexp
9319                       `(call-interactively ',(key-binding command))
9320                       backward)
9321         (gnus-message 6 "Executing %s...done" (key-description command))))))
9322
9323 (defun gnus-summary-beginning-of-article ()
9324   "Scroll the article back to the beginning."
9325   (interactive)
9326   (gnus-summary-select-article)
9327   (gnus-configure-windows 'article)
9328   (gnus-eval-in-buffer-window gnus-article-buffer
9329     (widen)
9330     (goto-char (point-min))
9331     (when gnus-break-pages
9332       (gnus-narrow-to-page))))
9333
9334 (defun gnus-summary-end-of-article ()
9335   "Scroll to the end of the article."
9336   (interactive)
9337   (gnus-summary-select-article)
9338   (gnus-configure-windows 'article)
9339   (gnus-eval-in-buffer-window gnus-article-buffer
9340     (widen)
9341     (goto-char (point-max))
9342     (recenter -3)
9343     (when gnus-break-pages
9344       (gnus-narrow-to-page))))
9345
9346 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9347   "Truncate to LEN and quote all \"(\"'s in STRING."
9348   (gnus-replace-in-string (if (and len (> (length string) len))
9349                               (substring string 0 len)
9350                             string)
9351                           "[()]" "\\\\\\&"))
9352
9353 (defun gnus-summary-print-article (&optional filename n)
9354   "Generate and print a PostScript image of the process-marked (mail) articles.
9355
9356 If used interactively, print the current article if none are
9357 process-marked.  With prefix arg, prompt the user for the name of the
9358 file to save in.
9359
9360 When used from Lisp, accept two optional args FILENAME and N.  N means
9361 to print the next N articles.  If N is negative, print the N previous
9362 articles.  If N is nil and articles have been marked with the process
9363 mark, print these instead.
9364
9365 If the optional first argument FILENAME is nil, send the image to the
9366 printer.  If FILENAME is a string, save the PostScript image in a file with
9367 that name.  If FILENAME is a number, prompt the user for the name of the file
9368 to save in."
9369   (interactive (list (ps-print-preprint current-prefix-arg)))
9370   (dolist (article (gnus-summary-work-articles n))
9371     (gnus-summary-select-article nil nil 'pseudo article)
9372     (gnus-eval-in-buffer-window gnus-article-buffer
9373       (gnus-print-buffer))
9374     (gnus-summary-remove-process-mark article))
9375   (ps-despool filename))
9376
9377 (defun gnus-print-buffer ()
9378   (let ((ps-left-header
9379          (list
9380           (concat "("
9381                   (gnus-summary-print-truncate-and-quote
9382                    (mail-header-subject gnus-current-headers)
9383                    66) ")")
9384           (concat "("
9385                   (gnus-summary-print-truncate-and-quote
9386                    (mail-header-from gnus-current-headers)
9387                    45) ")")))
9388         (ps-right-header
9389          (list
9390           "/pagenumberstring load"
9391           (concat "("
9392                   (mail-header-date gnus-current-headers) ")"))))
9393     (gnus-run-hooks 'gnus-ps-print-hook)
9394     (save-excursion
9395       (if ps-print-color-p
9396           (ps-spool-buffer-with-faces)
9397         (ps-spool-buffer)))))
9398
9399 (defun gnus-summary-show-complete-article ()
9400   "Show a complete version of the current article.
9401 This is only useful if you're looking at a partial version of the
9402 article currently."
9403   (interactive)
9404   (let ((gnus-keep-backlog nil)
9405         (gnus-use-cache nil)
9406         (gnus-agent nil)
9407         (variable (intern
9408                    (format "%s-fetch-partial-articles"
9409                            (car (gnus-find-method-for-group
9410                                  gnus-newsgroup-name)))
9411                    obarray))
9412         old-val)
9413     (unwind-protect
9414         (progn
9415           (setq old-val (symbol-value variable))
9416           (set variable nil)
9417           (gnus-flush-original-article-buffer)
9418           (gnus-summary-show-article))
9419       (set variable old-val))))
9420
9421 (defun gnus-summary-show-article (&optional arg)
9422   "Force redisplaying of the current article.
9423 If ARG (the prefix) is a number, show the article with the charset
9424 defined in `gnus-summary-show-article-charset-alist', or the charset
9425 input.
9426 If ARG (the prefix) is non-nil and not a number, show the article,
9427 but without running any of the article treatment functions
9428 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9429 C-u g', show the raw article."
9430   (interactive "P")
9431   (cond
9432    ((numberp arg)
9433     (gnus-summary-show-article t)
9434     (let ((gnus-newsgroup-charset
9435            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9436                (mm-read-coding-system
9437                 "View as charset: " ;; actually it is coding system.
9438                 (with-current-buffer gnus-article-buffer
9439                   (mm-detect-coding-region (point) (point-max))))))
9440           (gnus-newsgroup-ignored-charsets 'gnus-all))
9441       (gnus-summary-select-article nil 'force)
9442       (let ((deps gnus-newsgroup-dependencies)
9443             head header lines)
9444         (with-current-buffer gnus-original-article-buffer
9445           (save-restriction
9446             (message-narrow-to-head)
9447             (setq head (buffer-string))
9448             (goto-char (point-min))
9449             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9450               (goto-char (point-max))
9451               (widen)
9452               (setq lines (1- (count-lines (point) (point-max))))))
9453           (with-temp-buffer
9454             (insert (format "211 %d Article retrieved.\n"
9455                             (cdr gnus-article-current)))
9456             (insert head)
9457             (if lines (insert (format "Lines: %d\n" lines)))
9458             (insert ".\n")
9459             (let ((nntp-server-buffer (current-buffer)))
9460               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9461         (gnus-data-set-header
9462          (gnus-data-find (cdr gnus-article-current))
9463          header)
9464         (gnus-summary-update-article-line
9465          (cdr gnus-article-current) header)
9466         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9467           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9468    ((not arg)
9469     ;; Select the article the normal way.
9470     (gnus-summary-select-article nil 'force))
9471    ((or (equal arg '(16))
9472         (eq arg t))
9473     ;; C-u C-u g
9474     (let ((gnus-inhibit-article-treatments t))
9475       (gnus-summary-select-article nil 'force)))
9476    (t
9477     ;; We have to require this here to make sure that the following
9478     ;; dynamic binding isn't shadowed by autoloading.
9479     (require 'gnus-async)
9480     (require 'gnus-art)
9481     ;; Bind the article treatment functions to nil.
9482     (let ((gnus-have-all-headers t)
9483           gnus-article-prepare-hook
9484           gnus-article-decode-hook
9485           gnus-display-mime-function
9486           gnus-break-pages)
9487       ;; Destroy any MIME parts.
9488       (when (gnus-buffer-live-p gnus-article-buffer)
9489         (with-current-buffer gnus-article-buffer
9490           (mm-destroy-parts gnus-article-mime-handles)
9491           ;; Set it to nil for safety reason.
9492           (setq gnus-article-mime-handle-alist nil)
9493           (setq gnus-article-mime-handles nil)))
9494       (gnus-summary-select-article nil 'force))))
9495   (gnus-summary-goto-subject gnus-current-article)
9496   (gnus-summary-position-point))
9497
9498 (defun gnus-summary-show-raw-article ()
9499   "Show the raw article without any article massaging functions being run."
9500   (interactive)
9501   (gnus-summary-show-article t))
9502
9503 (defun gnus-summary-verbose-headers (&optional arg)
9504   "Toggle permanent full header display.
9505 If ARG is a positive number, turn header display on.
9506 If ARG is a negative number, turn header display off."
9507   (interactive "P")
9508   (setq gnus-show-all-headers
9509         (cond ((or (not (numberp arg))
9510                    (zerop arg))
9511                (not gnus-show-all-headers))
9512               ((natnump arg)
9513                t)))
9514   (gnus-summary-show-article))
9515
9516 (defun gnus-summary-toggle-header (&optional arg)
9517   "Show the headers if they are hidden, or hide them if they are shown.
9518 If ARG is a positive number, show the entire header.
9519 If ARG is a negative number, hide the unwanted header lines."
9520   (interactive "P")
9521   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9522                      (get-buffer-window gnus-article-buffer t))))
9523     (with-current-buffer gnus-article-buffer
9524       (widen)
9525       (article-narrow-to-head)
9526       (let* ((inhibit-read-only t)
9527              (inhibit-point-motion-hooks t)
9528              (hidden (if (numberp arg)
9529                          (>= arg 0)
9530                        (or (not (looking-at "[^ \t\n]+:"))
9531                            (gnus-article-hidden-text-p 'headers))))
9532              s e)
9533         (delete-region (point-min) (point-max))
9534         (with-current-buffer gnus-original-article-buffer
9535           (goto-char (setq s (point-min)))
9536           (setq e (if (search-forward "\n\n" nil t)
9537                       (1- (point))
9538                     (point-max))))
9539         (insert-buffer-substring gnus-original-article-buffer s e)
9540         (run-hooks 'gnus-article-decode-hook)
9541         (if hidden
9542             (let ((gnus-treat-hide-headers nil)
9543                   (gnus-treat-hide-boring-headers nil))
9544               (gnus-delete-wash-type 'headers)
9545               (gnus-treat-article 'head))
9546           (gnus-treat-article 'head))
9547         (widen)
9548         (if window
9549             (set-window-start window (goto-char (point-min))))
9550         (if gnus-break-pages
9551             (gnus-narrow-to-page)
9552           (when (gnus-visual-p 'page-marker)
9553             (let ((inhibit-read-only t))
9554               (gnus-remove-text-with-property 'gnus-prev)
9555               (gnus-remove-text-with-property 'gnus-next))))
9556         (gnus-set-mode-line 'article)))))
9557
9558 (defun gnus-summary-show-all-headers ()
9559   "Make all header lines visible."
9560   (interactive)
9561   (gnus-summary-toggle-header 1))
9562
9563 (defun gnus-summary-caesar-message (&optional arg)
9564   "Caesar rotate the current article by 13.
9565 With a non-numerical prefix, also rotate headers.  A numerical
9566 prefix specifies how many places to rotate each letter forward."
9567   (interactive "P")
9568   (gnus-summary-select-article)
9569   (let ((mail-header-separator ""))
9570     (gnus-eval-in-buffer-window gnus-article-buffer
9571       (save-restriction
9572         (widen)
9573         (let ((start (window-start))
9574               (inhibit-read-only t))
9575           (if (equal arg '(4))
9576               (message-caesar-buffer-body nil t)
9577             (message-caesar-buffer-body arg))
9578           (set-window-start (get-buffer-window (current-buffer)) start)))))
9579   ;; Create buttons and stuff...
9580   (gnus-treat-article nil))
9581
9582 (declare-function idna-to-unicode "ext:idna" (str))
9583
9584 (defun gnus-summary-idna-message (&optional arg)
9585   "Decode IDNA encoded domain names in the current articles.
9586 IDNA encoded domain names looks like `xn--bar'.  If a string
9587 remain unencoded after running this function, it is likely an
9588 invalid IDNA string (`xn--bar' is invalid).
9589
9590 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9591 installed for this command to work."
9592   (interactive "P")
9593   (if (not (and (condition-case nil (require 'idna)
9594                   (file-error))
9595                 (mm-coding-system-p 'utf-8)
9596                 (executable-find (symbol-value 'idna-program))))
9597       (gnus-message
9598        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9599     (gnus-summary-select-article)
9600     (let ((mail-header-separator ""))
9601       (gnus-eval-in-buffer-window gnus-article-buffer
9602         (save-restriction
9603           (widen)
9604           (let ((start (window-start))
9605                 buffer-read-only)
9606             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9607               (replace-match (idna-to-unicode (match-string 1))))
9608             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9609
9610 (defun gnus-summary-morse-message (&optional arg)
9611   "Morse decode the current article."
9612   (interactive "P")
9613   (gnus-summary-select-article)
9614   (let ((mail-header-separator ""))
9615     (gnus-eval-in-buffer-window gnus-article-buffer
9616       (save-excursion
9617         (save-restriction
9618           (widen)
9619           (let ((pos (window-start))
9620                 (inhibit-read-only t))
9621             (goto-char (point-min))
9622             (when (message-goto-body)
9623               (gnus-narrow-to-body))
9624             (goto-char (point-min))
9625             (while (search-forward "·" (point-max) t)
9626               (replace-match "."))
9627             (unmorse-region (point-min) (point-max))
9628             (widen)
9629             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9630
9631 (defun gnus-summary-stop-page-breaking ()
9632   "Stop page breaking in the current article."
9633   (interactive)
9634   (gnus-summary-select-article)
9635   (gnus-eval-in-buffer-window gnus-article-buffer
9636     (widen)
9637     (when (gnus-visual-p 'page-marker)
9638       (let ((inhibit-read-only t))
9639         (gnus-remove-text-with-property 'gnus-prev)
9640         (gnus-remove-text-with-property 'gnus-next))
9641       (setq gnus-page-broken nil))))
9642
9643 (defun gnus-summary-move-article (&optional n to-newsgroup
9644                                             select-method action)
9645   "Move the current article to a different newsgroup.
9646 If N is a positive number, move the N next articles.
9647 If N is a negative number, move the N previous articles.
9648 If N is nil and any articles have been marked with the process mark,
9649 move those articles instead.
9650 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9651 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9652 re-spool using this method.
9653
9654 When called interactively with TO-NEWSGROUP being nil, the value of
9655 the variable `gnus-move-split-methods' is used for finding a default
9656 for the target newsgroup.
9657
9658 For this function to work, both the current newsgroup and the
9659 newsgroup that you want to move to have to support the `request-move'
9660 and `request-accept' functions.
9661
9662 ACTION can be either `move' (the default), `crosspost' or `copy'."
9663   (interactive "P")
9664   (unless action
9665     (setq action 'move))
9666   ;; Check whether the source group supports the required functions.
9667   (cond ((and (eq action 'move)
9668               (not (gnus-check-backend-function
9669                     'request-move-article gnus-newsgroup-name)))
9670          (error "The current group does not support article moving"))
9671         ((and (eq action 'crosspost)
9672               (not (gnus-check-backend-function
9673                     'request-replace-article gnus-newsgroup-name)))
9674          (error "The current group does not support article editing")))
9675   (let ((articles (gnus-summary-work-articles n))
9676         (prefix (if (gnus-check-backend-function
9677                      'request-move-article gnus-newsgroup-name)
9678                     (funcall gnus-move-group-prefix-function
9679                              gnus-newsgroup-name)
9680                   ""))
9681         (names '((move "Move" "Moving")
9682                  (copy "Copy" "Copying")
9683                  (crosspost "Crosspost" "Crossposting")))
9684         (copy-buf (save-excursion
9685                     (nnheader-set-temp-buffer " *copy article*")))
9686         art-group to-method new-xref article to-groups
9687         articles-to-update-marks encoded)
9688     (unless (assq action names)
9689       (error "Unknown action %s" action))
9690     ;; Read the newsgroup name.
9691     (when (and (not to-newsgroup)
9692                (not select-method))
9693       (if (and gnus-move-split-methods
9694                (not
9695                 (and (memq gnus-current-article articles)
9696                      (gnus-buffer-live-p gnus-original-article-buffer))))
9697           ;; When `gnus-move-split-methods' is non-nil, we have to
9698           ;; select an article to give `gnus-read-move-group-name' an
9699           ;; opportunity to suggest an appropriate default.  However,
9700           ;; we needn't render or mark the article.
9701           (let ((gnus-display-mime-function nil)
9702                 (gnus-article-prepare-hook nil)
9703                 (gnus-mark-article-hook nil))
9704             (gnus-summary-select-article nil nil nil (car articles))))
9705       (setq to-newsgroup (gnus-read-move-group-name
9706                           (cadr (assq action names))
9707                           (symbol-value
9708                            (intern (format "gnus-current-%s-group" action)))
9709                           articles prefix)
9710             encoded to-newsgroup
9711             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9712       (set (intern (format "gnus-current-%s-group" action))
9713            (mm-decode-coding-string
9714             to-newsgroup
9715             (gnus-group-name-charset to-method to-newsgroup))))
9716     (unless to-method
9717       (setq to-method (or select-method
9718                           (gnus-server-to-method
9719                            (gnus-group-method to-newsgroup)))))
9720     (setq to-newsgroup
9721           (or encoded
9722               (and to-newsgroup
9723                    (mm-encode-coding-string
9724                     to-newsgroup
9725                     (gnus-group-name-charset to-method to-newsgroup)))))
9726     ;; Check the method we are to move this article to...
9727     (unless (gnus-check-backend-function
9728              'request-accept-article (car to-method))
9729       (error "%s does not support article copying" (car to-method)))
9730     (unless (gnus-check-server to-method)
9731       (error "Can't open server %s" (car to-method)))
9732     (gnus-message 6 "%s to %s: %s..."
9733                   (caddr (assq action names))
9734                   (or (car select-method)
9735                       (gnus-group-decoded-name to-newsgroup))
9736                   articles)
9737     (while articles
9738       (setq article (pop articles))
9739       ;; Set any marks that may have changed in the summary buffer.
9740       (when gnus-preserve-marks
9741         (gnus-summary-push-marks-to-backend article))
9742       (setq
9743        art-group
9744        (cond
9745         ;; Move the article.
9746         ((eq action 'move)
9747          ;; Remove this article from future suppression.
9748          (gnus-dup-unsuppress-article article)
9749          (let* ((from-method (gnus-find-method-for-group
9750                               gnus-newsgroup-name))
9751                 (to-method (or select-method
9752                                (gnus-find-method-for-group to-newsgroup)))
9753                 (move-is-internal (gnus-server-equal from-method to-method)))
9754            (gnus-request-move-article
9755             article                     ; Article to move
9756             gnus-newsgroup-name         ; From newsgroup
9757             (nth 1 (gnus-find-method-for-group
9758                     gnus-newsgroup-name)) ; Server
9759             (list 'gnus-request-accept-article
9760                   to-newsgroup (list 'quote select-method)
9761                   (not articles) t)     ; Accept form
9762             (not articles)              ; Only save nov last time
9763             (and move-is-internal
9764                  to-newsgroup           ; Not respooling
9765                                         ; Is this move internal?
9766                  (gnus-group-real-name to-newsgroup)))))
9767         ;; Copy the article.
9768         ((eq action 'copy)
9769          (with-current-buffer copy-buf
9770            (when (gnus-request-article-this-buffer article
9771                                                    gnus-newsgroup-name)
9772              (save-restriction
9773                (nnheader-narrow-to-headers)
9774                (dolist (hdr gnus-copy-article-ignored-headers)
9775                  (message-remove-header hdr t)))
9776              (gnus-request-accept-article
9777               to-newsgroup select-method (not articles) t))))
9778         ;; Crosspost the article.
9779         ((eq action 'crosspost)
9780          (let ((xref (message-tokenize-header
9781                       (mail-header-xref (gnus-summary-article-header
9782                                          article))
9783                       " ")))
9784            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9785                                   ":" (number-to-string article)))
9786            (unless xref
9787              (setq xref (list (system-name))))
9788            (setq new-xref
9789                  (concat
9790                   (mapconcat 'identity
9791                              (delete "Xref:" (delete new-xref xref))
9792                              " ")
9793                   " " new-xref))
9794            (with-current-buffer copy-buf
9795              ;; First put the article in the destination group.
9796              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9797              (when (consp (setq art-group
9798                                 (gnus-request-accept-article
9799                                  to-newsgroup select-method (not articles)
9800                                  t)))
9801                (setq new-xref (concat new-xref " " (car art-group)
9802                                       ":"
9803                                       (number-to-string (cdr art-group))))
9804                ;; Now we have the new Xrefs header, so we insert
9805                ;; it and replace the new article.
9806                (nnheader-replace-header "Xref" new-xref)
9807                (gnus-request-replace-article
9808                 (cdr art-group) to-newsgroup (current-buffer) t)
9809                art-group))))))
9810       (cond
9811        ((not art-group)
9812         (gnus-message 1 "Couldn't %s article %s: %s"
9813                       (cadr (assq action names)) article
9814                       (nnheader-get-report (car to-method))))
9815        ((eq art-group 'junk)
9816         (when (eq action 'move)
9817           (gnus-summary-mark-article article gnus-canceled-mark)
9818           (gnus-message 4 "Deleted article %s" article)
9819           ;; run the delete hook
9820           (run-hook-with-args 'gnus-summary-article-delete-hook
9821                               action
9822                               (gnus-data-header
9823                                (assoc article (gnus-data-list nil)))
9824                               gnus-newsgroup-name nil
9825                               select-method)))
9826        (t
9827         (let* ((pto-group (gnus-group-prefixed-name
9828                            (car art-group) to-method))
9829                (info (gnus-get-info pto-group))
9830                (to-group (gnus-info-group info))
9831                to-marks)
9832           ;; Update the group that has been moved to.
9833           (when (and info
9834                      (memq action '(move copy)))
9835             (unless (member to-group to-groups)
9836               (push to-group to-groups))
9837
9838             (when (and (not (memq article gnus-newsgroup-unreads))
9839                        (cdr art-group))
9840               (push 'read to-marks)
9841               (gnus-info-set-read
9842                info (gnus-add-to-range (gnus-info-read info)
9843                                        (list (cdr art-group)))))
9844
9845             ;; See whether the article is to be put in the cache.
9846             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9847                    (marks (if expirable
9848                               gnus-article-mark-lists
9849                             (delete '(expirable . expire)
9850                                     (copy-sequence
9851                                      gnus-article-mark-lists))))
9852                    (to-article (cdr art-group)))
9853
9854               ;; Enter the article into the cache in the new group,
9855               ;; if that is required.
9856               (when (and to-article
9857                          gnus-use-cache)
9858                 (gnus-cache-possibly-enter-article
9859                  to-group to-article
9860                  (memq article gnus-newsgroup-marked)
9861                  (memq article gnus-newsgroup-dormant)
9862                  (memq article gnus-newsgroup-unreads)))
9863
9864               (when (and gnus-preserve-marks
9865                          to-article)
9866                 ;; Copy any marks over to the new group.
9867                 (when (and (equal to-group gnus-newsgroup-name)
9868                            (not (memq article gnus-newsgroup-unreads)))
9869                   ;; Mark this article as read in this group.
9870                   (push (cons to-article gnus-read-mark)
9871                         gnus-newsgroup-reads)
9872                   ;; Increase the active status of this group.
9873                   (setcdr (gnus-active to-group) to-article)
9874                   (setcdr gnus-newsgroup-active to-article))
9875
9876                 (while marks
9877                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9878                     (when (memq article (symbol-value
9879                                          (intern (format "gnus-newsgroup-%s"
9880                                                          (caar marks)))))
9881                       (push (cdar marks) to-marks)
9882                       ;; If the other group is the same as this group,
9883                       ;; then we have to add the mark to the list.
9884                       (when (equal to-group gnus-newsgroup-name)
9885                         (set (intern (format "gnus-newsgroup-%s"
9886                                              (caar marks)))
9887                              (cons to-article
9888                                    (symbol-value
9889                                     (intern (format "gnus-newsgroup-%s"
9890                                                     (caar marks)))))))
9891                       ;; Copy the marks to other group.
9892                       (gnus-add-marked-articles
9893                        to-group (cdar marks) (list to-article) info)))
9894                   (setq marks (cdr marks)))
9895
9896                 (when (and expirable
9897                            gnus-mark-copied-or-moved-articles-as-expirable
9898                            (not (memq 'expire to-marks)))
9899                   ;; Mark this article as expirable.
9900                   (push 'expire to-marks)
9901                   (when (equal to-group gnus-newsgroup-name)
9902                     (push to-article gnus-newsgroup-expirable))
9903                   ;; Copy the expirable mark to other group.
9904                   (gnus-add-marked-articles
9905                    to-group 'expire (list to-article) info))
9906
9907                 (when to-marks
9908                   (gnus-request-set-mark
9909                    to-group (list (list (list to-article) 'add to-marks)))))
9910
9911               (gnus-dribble-enter
9912                (concat "(gnus-group-set-info '"
9913                        (gnus-prin1-to-string (gnus-get-info to-group))
9914                        ")"))))
9915
9916           ;; Update the Xref header in this article to point to
9917           ;; the new crossposted article we have just created.
9918           (when (eq action 'crosspost)
9919             (with-current-buffer copy-buf
9920               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9921               (nnheader-replace-header "Xref" new-xref)
9922               (gnus-request-replace-article
9923                article gnus-newsgroup-name (current-buffer) t)))
9924
9925           ;; run the move/copy/crosspost/respool hook
9926           (run-hook-with-args 'gnus-summary-article-move-hook
9927                               action
9928                               (gnus-data-header
9929                                (assoc article (gnus-data-list nil)))
9930                               gnus-newsgroup-name
9931                               to-newsgroup
9932                               select-method))
9933
9934         ;;;!!!Why is this necessary?
9935         (set-buffer gnus-summary-buffer)
9936
9937         (when (eq action 'move)
9938           (save-excursion
9939             (gnus-summary-goto-subject article)
9940             (gnus-summary-mark-article article gnus-canceled-mark)))))
9941       (push article articles-to-update-marks))
9942
9943     (save-excursion
9944       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9945     ;; Re-activate all groups that have been moved to.
9946     (with-current-buffer gnus-group-buffer
9947       (let ((gnus-group-marked to-groups))
9948         (gnus-group-get-new-news-this-group nil t)))
9949
9950     (gnus-kill-buffer copy-buf)
9951     (gnus-summary-position-point)
9952     (gnus-set-mode-line 'summary)))
9953
9954 (defun gnus-summary-push-marks-to-backend (article)
9955   (let ((set nil)
9956         (marks gnus-article-mark-lists))
9957     (unless (memq article gnus-newsgroup-unreads)
9958       (push 'read set))
9959     (while marks
9960       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9961                  (memq article (symbol-value
9962                                 (intern (format "gnus-newsgroup-%s"
9963                                                 (caar marks))))))
9964         (push (cdar marks) set))
9965       (pop marks))
9966     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
9967
9968 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9969   "Copy the current article to some other group.
9970 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9971 When called interactively, if TO-NEWSGROUP is nil, use the value of
9972 the variable `gnus-move-split-methods' for finding a default target
9973 newsgroup.
9974 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9975 re-spool using this method."
9976   (interactive "P")
9977   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9978
9979 (defun gnus-summary-crosspost-article (&optional n)
9980   "Crosspost the current article to some other group."
9981   (interactive "P")
9982   (gnus-summary-move-article n nil nil 'crosspost))
9983
9984 (defcustom gnus-summary-respool-default-method nil
9985   "Default method type for respooling an article.
9986 If nil, use to the current newsgroup method."
9987   :type 'symbol
9988   :group 'gnus-summary-mail)
9989
9990 (defun gnus-summary-respool-article (&optional n method)
9991   "Respool the current article.
9992 The article will be squeezed through the mail spooling process again,
9993 which means that it will be put in some mail newsgroup or other
9994 depending on `nnmail-split-methods'.
9995 If N is a positive number, respool the N next articles.
9996 If N is a negative number, respool the N previous articles.
9997 If N is nil and any articles have been marked with the process mark,
9998 respool those articles instead.
9999
10000 Respooling can be done both from mail groups and \"real\" newsgroups.
10001 In the former case, the articles in question will be moved from the
10002 current group into whatever groups they are destined to.  In the
10003 latter case, they will be copied into the relevant groups."
10004   (interactive
10005    (list current-prefix-arg
10006          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10007                 (methname
10008                  (symbol-name (or gnus-summary-respool-default-method
10009                                   (car (gnus-find-method-for-group
10010                                         gnus-newsgroup-name)))))
10011                 (method
10012                  (gnus-completing-read
10013                   "Backend to use when respooling"
10014                   methods t nil 'gnus-mail-method-history methname))
10015                 ms)
10016            (cond
10017             ((zerop (length (setq ms (gnus-servers-using-backend
10018                                       (intern method)))))
10019              (list (intern method) ""))
10020             ((= 1 (length ms))
10021              (car ms))
10022             (t
10023              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10024                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10025                            ms-alist))))))))
10026   (unless method
10027     (error "No method given for respooling"))
10028   (if (assoc (symbol-name
10029               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10030              (gnus-methods-using 'respool))
10031       (gnus-summary-move-article n nil method)
10032     (gnus-summary-copy-article n nil method)))
10033
10034 (defun gnus-summary-import-article (file &optional edit)
10035   "Import an arbitrary file into a mail newsgroup."
10036   (interactive "fImport file: \nP")
10037   (let ((group gnus-newsgroup-name)
10038         (now (current-time))
10039         atts lines group-art)
10040     (unless (gnus-check-backend-function 'request-accept-article group)
10041       (error "%s does not support article importing" group))
10042     (or (file-readable-p file)
10043         (not (file-regular-p file))
10044         (error "Can't read %s" file))
10045     (with-current-buffer (gnus-get-buffer-create " *import file*")
10046       (erase-buffer)
10047       (nnheader-insert-file-contents file)
10048       (goto-char (point-min))
10049       (if (nnheader-article-p)
10050           (save-restriction
10051             (goto-char (point-min))
10052             (search-forward "\n\n" nil t)
10053             (narrow-to-region (point-min) (1- (point)))
10054             (goto-char (point-min))
10055             (unless (re-search-forward "^date:" nil t)
10056               (goto-char (point-max))
10057               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10058        ;; This doesn't look like an article, so we fudge some headers.
10059         (setq atts (file-attributes file)
10060               lines (count-lines (point-min) (point-max)))
10061         (insert "From: " (read-string "From: ") "\n"
10062                 "Subject: " (read-string "Subject: ") "\n"
10063                 "Date: " (message-make-date (nth 5 atts)) "\n"
10064                 "Message-ID: " (message-make-message-id) "\n"
10065                 "Lines: " (int-to-string lines) "\n"
10066                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10067       (setq group-art (gnus-request-accept-article group nil t))
10068       (kill-buffer (current-buffer)))
10069     (setq gnus-newsgroup-active (gnus-activate-group group))
10070     (forward-line 1)
10071     (gnus-summary-goto-article (cdr group-art) nil t)
10072     (when edit
10073       (gnus-summary-edit-article))))
10074
10075 (defun gnus-summary-create-article ()
10076   "Create an article in a mail newsgroup."
10077   (interactive)
10078   (let ((group gnus-newsgroup-name)
10079         (now (current-time))
10080         group-art)
10081     (unless (gnus-check-backend-function 'request-accept-article group)
10082       (error "%s does not support article importing" group))
10083     (with-current-buffer (gnus-get-buffer-create " *import file*")
10084       (erase-buffer)
10085       (goto-char (point-min))
10086       ;; This doesn't look like an article, so we fudge some headers.
10087       (insert "From: " (read-string "From: ") "\n"
10088               "Subject: " (read-string "Subject: ") "\n"
10089               "Date: " (message-make-date now) "\n"
10090               "Message-ID: " (message-make-message-id) "\n")
10091       (setq group-art (gnus-request-accept-article group nil t))
10092       (kill-buffer (current-buffer)))
10093     (setq gnus-newsgroup-active (gnus-activate-group group))
10094     (forward-line 1)
10095     (gnus-summary-goto-article (cdr group-art) nil t)
10096     (gnus-summary-edit-article)))
10097
10098 (defun gnus-summary-article-posted-p ()
10099   "Say whether the current (mail) article is available from news as well.
10100 This will be the case if the article has both been mailed and posted."
10101   (interactive)
10102   (let ((id (mail-header-references (gnus-summary-article-header)))
10103         (gnus-override-method (car (gnus-refer-article-methods))))
10104     (if (gnus-request-head id "")
10105         (gnus-message 2 "The current message was found on %s"
10106                       gnus-override-method)
10107       (gnus-message 2 "The current message couldn't be found on %s"
10108                     gnus-override-method)
10109       nil)))
10110
10111 (defun gnus-summary-expire-articles (&optional now)
10112   "Expire all articles that are marked as expirable in the current group."
10113   (interactive)
10114   (when (and (not gnus-group-is-exiting-without-update-p)
10115              (gnus-check-backend-function
10116               'request-expire-articles gnus-newsgroup-name))
10117     ;; This backend supports expiry.
10118     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10119            (expirable (if total
10120                           (progn
10121                             ;; We need to update the info for
10122                             ;; this group for `gnus-list-of-read-articles'
10123                             ;; to give us the right answer.
10124                             (gnus-run-hooks 'gnus-exit-group-hook)
10125                             (gnus-summary-update-info)
10126                             (gnus-list-of-read-articles gnus-newsgroup-name))
10127                         (setq gnus-newsgroup-expirable
10128                               (sort gnus-newsgroup-expirable '<))))
10129            (expiry-wait (if now 'immediate
10130                           (gnus-group-find-parameter
10131                            gnus-newsgroup-name 'expiry-wait)))
10132            (nnmail-expiry-target
10133             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10134                 nnmail-expiry-target))
10135            es)
10136       (when expirable
10137         ;; There are expirable articles in this group, so we run them
10138         ;; through the expiry process.
10139         (gnus-message 6 "Expiring articles...")
10140         (unless (gnus-check-group gnus-newsgroup-name)
10141           (error "Can't open server for %s" gnus-newsgroup-name))
10142         ;; The list of articles that weren't expired is returned.
10143         (save-excursion
10144           (if expiry-wait
10145               (let ((nnmail-expiry-wait-function nil)
10146                     (nnmail-expiry-wait expiry-wait))
10147                 (setq es (gnus-request-expire-articles
10148                           expirable gnus-newsgroup-name)))
10149             (setq es (gnus-request-expire-articles
10150                       expirable gnus-newsgroup-name)))
10151           (unless total
10152             (setq gnus-newsgroup-expirable es))
10153           ;; We go through the old list of expirable, and mark all
10154           ;; really expired articles as nonexistent.
10155           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10156             (let ((gnus-use-cache nil))
10157               (dolist (article expirable)
10158                 (when (and (not (memq article es))
10159                            (gnus-data-find article))
10160                   (gnus-summary-mark-article article gnus-canceled-mark)
10161                   (run-hook-with-args 'gnus-summary-article-expire-hook
10162                                       'delete
10163                                       (gnus-data-header
10164                                        (assoc article (gnus-data-list nil)))
10165                                       gnus-newsgroup-name
10166                                       nil
10167                                       nil))))))
10168         (gnus-message 6 "Expiring articles...done")))))
10169
10170 (defun gnus-summary-expire-articles-now ()
10171   "Expunge all expirable articles in the current group.
10172 This means that *all* articles that are marked as expirable will be
10173 deleted forever, right now."
10174   (interactive)
10175   (or gnus-expert-user
10176       (gnus-yes-or-no-p
10177        "Are you really, really sure you want to delete all expirable messages? ")
10178       (error "Phew!"))
10179   (gnus-summary-expire-articles t))
10180
10181 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10182 (defun gnus-summary-delete-article (&optional n)
10183   "Delete the N next (mail) articles.
10184 This command actually deletes articles.  This is not a marking
10185 command.  The article will disappear forever from your life, never to
10186 return.
10187
10188 If N is negative, delete backwards.
10189 If N is nil and articles have been marked with the process mark,
10190 delete these instead.
10191
10192 If `gnus-novice-user' is non-nil you will be asked for
10193 confirmation before the articles are deleted."
10194   (interactive "P")
10195   (unless (gnus-check-backend-function 'request-expire-articles
10196                                        gnus-newsgroup-name)
10197     (error "The current newsgroup does not support article deletion"))
10198   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10199     (error "Couldn't open server"))
10200   ;; Compute the list of articles to delete.
10201   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10202         (nnmail-expiry-target 'delete)
10203         not-deleted)
10204     (if (and gnus-novice-user
10205              (not (gnus-yes-or-no-p
10206                    (format "Do you really want to delete %s forever? "
10207                            (if (> (length articles) 1)
10208                                (format "these %s articles" (length articles))
10209                              "this article")))))
10210         ()
10211       ;; Delete the articles.
10212       (setq not-deleted (gnus-request-expire-articles
10213                          articles gnus-newsgroup-name 'force))
10214       (save-excursion
10215         (while articles
10216           (gnus-summary-remove-process-mark (car articles))
10217           ;; The backend might not have been able to delete the article
10218           ;; after all.
10219           (unless (memq (car articles) not-deleted)
10220             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10221             (let* ((article (car articles))
10222                    (ghead  (gnus-data-header
10223                             (assoc article (gnus-data-list nil)))))
10224               (run-hook-with-args 'gnus-summary-article-delete-hook
10225                                   'delete ghead gnus-newsgroup-name nil
10226                                   nil)))
10227           (setq articles (cdr articles))))
10228       (when not-deleted
10229         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10230     (gnus-summary-position-point)
10231     (gnus-set-mode-line 'summary)
10232     not-deleted))
10233
10234 (defun gnus-summary-edit-article (&optional arg)
10235   "Edit the current article.
10236 This will have permanent effect only in mail groups.
10237 If ARG is nil, edit the decoded articles.
10238 If ARG is 1, edit the raw articles.
10239 If ARG is 2, edit the raw articles even in read-only groups.
10240 If ARG is 3, edit the articles with the current handles.
10241 Otherwise, allow editing of articles even in read-only
10242 groups."
10243   (interactive "P")
10244   (let (force raw current-handles)
10245     (cond
10246      ((null arg))
10247      ((eq arg 1)
10248       (setq raw t))
10249      ((eq arg 2)
10250       (setq raw t
10251             force t))
10252      ((eq arg 3)
10253       (setq current-handles
10254             (and (gnus-buffer-live-p gnus-article-buffer)
10255                  (with-current-buffer gnus-article-buffer
10256                    (prog1
10257                        gnus-article-mime-handles
10258                      (setq gnus-article-mime-handles nil))))))
10259      (t
10260       (setq force t)))
10261     (when (and raw (not force)
10262                (member gnus-newsgroup-name '("nndraft:delayed"
10263                                              "nndraft:drafts"
10264                                              "nndraft:queue")))
10265       (error "Can't edit the raw article in group %s"
10266              gnus-newsgroup-name))
10267     (with-current-buffer gnus-summary-buffer
10268       (let ((mail-parse-charset gnus-newsgroup-charset)
10269             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10270         (gnus-set-global-variables)
10271         (when (and (not force)
10272                    (gnus-group-read-only-p))
10273           (error "The current newsgroup does not support article editing"))
10274         (gnus-summary-show-article t)
10275         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10276           (with-current-buffer gnus-article-buffer
10277             (mm-enable-multibyte)))
10278         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10279             (setq raw t))
10280         (gnus-article-edit-article
10281          (if raw 'ignore
10282            `(lambda ()
10283               (let ((mbl mml-buffer-list))
10284                 (setq mml-buffer-list nil)
10285                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10286                   (mime-to-mml ,'current-handles))
10287                 (let ((mbl1 mml-buffer-list))
10288                   (setq mml-buffer-list mbl)
10289                   (set (make-local-variable 'mml-buffer-list) mbl1))
10290                 (gnus-make-local-hook 'kill-buffer-hook)
10291                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10292          `(lambda (no-highlight)
10293             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10294                   (message-options message-options)
10295                   (message-options-set-recipient)
10296                   (mail-parse-ignored-charsets
10297                    ',gnus-newsgroup-ignored-charsets)
10298                   (rfc2047-header-encoding-alist
10299                    ',(let ((charset (gnus-group-name-charset
10300                                      (gnus-find-method-for-group
10301                                       gnus-newsgroup-name)
10302                                      gnus-newsgroup-name)))
10303                        (append (list (cons "Newsgroups" charset)
10304                                      (cons "Followup-To" charset)
10305                                      (cons "Xref" charset))
10306                                rfc2047-header-encoding-alist))))
10307               ,(if (not raw) '(progn
10308                                 (mml-to-mime)
10309                                 (mml-destroy-buffers)
10310                                 (remove-hook 'kill-buffer-hook
10311                                              'mml-destroy-buffers t)
10312                                 (kill-local-variable 'mml-buffer-list)))
10313               (gnus-summary-edit-article-done
10314                ,(or (mail-header-references gnus-current-headers) "")
10315                ,(gnus-group-read-only-p)
10316                ,gnus-summary-buffer no-highlight))))))))
10317
10318 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10319
10320 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10321                                                  no-highlight)
10322   "Make edits to the current article permanent."
10323   (interactive)
10324   (save-excursion
10325     ;; The buffer restriction contains the entire article if it exists.
10326     (when (article-goto-body)
10327       (let ((lines (count-lines (point) (point-max)))
10328             (length (- (point-max) (point)))
10329             (case-fold-search t)
10330             (body (copy-marker (point))))
10331         (goto-char (point-min))
10332         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10333           (delete-region (match-beginning 1) (match-end 1))
10334           (insert (number-to-string length)))
10335         (goto-char (point-min))
10336         (when (re-search-forward
10337                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10338           (delete-region (match-beginning 1) (match-end 1))
10339           (insert (number-to-string length)))
10340         (goto-char (point-min))
10341         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10342           (delete-region (match-beginning 1) (match-end 1))
10343           (insert (number-to-string lines))))))
10344   ;; Replace the article.
10345   (let ((buf (current-buffer))
10346         (article (cdr gnus-article-current))
10347         replace-result)
10348     (with-temp-buffer
10349       (insert-buffer-substring buf)
10350       (if (and (not read-only)
10351                (not (setq replace-result
10352                           (gnus-request-replace-article
10353                            article (car gnus-article-current)
10354                            (current-buffer) t))))
10355           (error "Couldn't replace article")
10356         ;; If we got a number back, then that's the new article number
10357         ;; for this article.  Otherwise, the article number didn't change.
10358         (when (numberp replace-result)
10359           (with-current-buffer gnus-summary-buffer
10360             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10361             (gnus-summary-limit gnus-newsgroup-limit)
10362             (setq article replace-result)
10363             (gnus-summary-goto-subject article t)))
10364         ;; Update the summary buffer.
10365         (if (and references
10366                  (equal (message-tokenize-header references " ")
10367                         (message-tokenize-header
10368                          (or (message-fetch-field "references") "") " ")))
10369             ;; We only have to update this line.
10370             (save-excursion
10371               (save-restriction
10372                 (message-narrow-to-head)
10373                 (let ((head (buffer-substring-no-properties
10374                              (point-min) (point-max)))
10375                       header)
10376                   (with-temp-buffer
10377                     (insert (format "211 %d Article retrieved.\n" article))
10378                     (insert head)
10379                     (insert ".\n")
10380                     (let ((nntp-server-buffer (current-buffer)))
10381                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10382                     (with-current-buffer gnus-summary-buffer
10383                       (gnus-data-set-header (gnus-data-find article) header)
10384                       (gnus-summary-update-article-line article header)
10385                       (if (gnus-summary-goto-subject article nil t)
10386                           (gnus-summary-update-secondary-mark article)))))))
10387           ;; Update threads.
10388           (set-buffer (or buffer gnus-summary-buffer))
10389           (gnus-summary-update-article article)
10390           (if (gnus-summary-goto-subject article nil t)
10391               (gnus-summary-update-secondary-mark article)))
10392         ;; Prettify the article buffer again.
10393         (unless no-highlight
10394           (with-current-buffer gnus-article-buffer
10395             ;;!!! Fix this -- article should be rehighlighted.
10396             ;;(gnus-run-hooks 'gnus-article-display-hook)
10397             (set-buffer gnus-original-article-buffer)
10398             (gnus-request-article
10399              article (car gnus-article-current) (current-buffer))))
10400         ;; Prettify the summary buffer line.
10401         (when (gnus-visual-p 'summary-highlight 'highlight)
10402           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10403
10404 (defun gnus-summary-edit-wash (key)
10405   "Perform editing command KEY in the article buffer."
10406   (interactive
10407    (list
10408     (progn
10409       (message "%s" (concat (this-command-keys) "- "))
10410       (read-char))))
10411   (message "")
10412   (gnus-summary-edit-article)
10413   (execute-kbd-macro (concat (this-command-keys) key))
10414   (gnus-article-edit-done))
10415
10416 ;;; Respooling
10417
10418 (defun gnus-summary-respool-query (&optional silent trace)
10419   "Query where the respool algorithm would put this article."
10420   (interactive)
10421   (let (gnus-mark-article-hook)
10422     (gnus-summary-select-article)
10423     (with-current-buffer gnus-original-article-buffer
10424       (let ((groups (nnmail-article-group 'identity trace)))
10425         (unless silent
10426           (if groups
10427               (message "This message would go to %s"
10428                        (mapconcat 'car groups ", "))
10429             (message "This message would go to no groups"))
10430           groups)))))
10431
10432 (defun gnus-summary-respool-trace ()
10433   "Trace where the respool algorithm would put this article.
10434 Display a buffer showing all fancy splitting patterns which matched."
10435   (interactive)
10436   (gnus-summary-respool-query nil t))
10437
10438 ;; Summary marking commands.
10439
10440 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10441   "Mark articles which has the same subject as read, and then select the next.
10442 If UNMARK is positive, remove any kind of mark.
10443 If UNMARK is negative, tick articles."
10444   (interactive "P")
10445   (when unmark
10446     (setq unmark (prefix-numeric-value unmark)))
10447   (let ((count
10448          (gnus-summary-mark-same-subject
10449           (gnus-summary-article-subject) unmark)))
10450     ;; Select next unread article.  If auto-select-same mode, should
10451     ;; select the first unread article.
10452     (gnus-summary-next-article t (and gnus-auto-select-same
10453                                       (gnus-summary-article-subject)))
10454     (gnus-message 7 "%d article%s marked as %s"
10455                   count (if (= count 1) " is" "s are")
10456                   (if unmark "unread" "read"))))
10457
10458 (defun gnus-summary-kill-same-subject (&optional unmark)
10459   "Mark articles which has the same subject as read.
10460 If UNMARK is positive, remove any kind of mark.
10461 If UNMARK is negative, tick articles."
10462   (interactive "P")
10463   (when unmark
10464     (setq unmark (prefix-numeric-value unmark)))
10465   (let ((count
10466          (gnus-summary-mark-same-subject
10467           (gnus-summary-article-subject) unmark)))
10468     ;; If marked as read, go to next unread subject.
10469     (when (null unmark)
10470       ;; Go to next unread subject.
10471       (gnus-summary-next-subject 1 t))
10472     (gnus-message 7 "%d articles are marked as %s"
10473                   count (if unmark "unread" "read"))))
10474
10475 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10476   "Mark articles with same SUBJECT as read, and return marked number.
10477 If optional argument UNMARK is positive, remove any kinds of marks.
10478 If optional argument UNMARK is negative, mark articles as unread instead."
10479   (let ((count 1))
10480     (save-excursion
10481       (cond
10482        ((null unmark)                   ; Mark as read.
10483         (while (and
10484                 (progn
10485                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10486                   (gnus-summary-show-thread) t)
10487                 (gnus-summary-find-subject subject))
10488           (setq count (1+ count))))
10489        ((> unmark 0)                    ; Tick.
10490         (while (and
10491                 (progn
10492                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10493                   (gnus-summary-show-thread) t)
10494                 (gnus-summary-find-subject subject))
10495           (setq count (1+ count))))
10496        (t                               ; Mark as unread.
10497         (while (and
10498                 (progn
10499                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10500                   (gnus-summary-show-thread) t)
10501                 (gnus-summary-find-subject subject))
10502           (setq count (1+ count)))))
10503       (gnus-set-mode-line 'summary)
10504       ;; Return the number of marked articles.
10505       count)))
10506
10507 (defun gnus-summary-mark-as-processable (n &optional unmark)
10508   "Set the process mark on the next N articles.
10509 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10510 the process mark instead.  The difference between N and the actual
10511 number of articles marked is returned."
10512   (interactive "P")
10513   (if (and (null n) (gnus-region-active-p))
10514       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10515     (setq n (prefix-numeric-value n))
10516     (let ((backward (< n 0))
10517           (n (abs n)))
10518       (while (and
10519               (> n 0)
10520               (if unmark
10521                   (gnus-summary-remove-process-mark
10522                    (gnus-summary-article-number))
10523                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10524               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10525         (setq n (1- n)))
10526       (when (/= 0 n)
10527         (gnus-message 7 "No more articles"))
10528       (gnus-summary-recenter)
10529       (gnus-summary-position-point)
10530       n)))
10531
10532 (defun gnus-summary-unmark-as-processable (n)
10533   "Remove the process mark from the next N articles.
10534 If N is negative, unmark backward instead.  The difference between N and
10535 the actual number of articles unmarked is returned."
10536   (interactive "P")
10537   (gnus-summary-mark-as-processable n t))
10538
10539 (defun gnus-summary-unmark-all-processable ()
10540   "Remove the process mark from all articles."
10541   (interactive)
10542   (save-excursion
10543     (while gnus-newsgroup-processable
10544       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10545   (gnus-summary-position-point))
10546
10547 (defun gnus-summary-add-mark (article type)
10548   "Mark ARTICLE with a mark of TYPE."
10549   (let ((vtype (car (assq type gnus-article-mark-lists)))
10550         var)
10551     (if (not vtype)
10552         (error "No such mark type: %s" type)
10553       (setq var (intern (format "gnus-newsgroup-%s" type)))
10554       (set var (cons article (symbol-value var)))
10555       (if (memq type '(processable cached replied forwarded recent saved))
10556           (gnus-summary-update-secondary-mark article)
10557         ;;; !!! This is bogus.  We should find out what primary
10558         ;;; !!! mark we want to set.
10559         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10560
10561 (defun gnus-summary-mark-as-expirable (n)
10562   "Mark N articles forward as expirable.
10563 If N is negative, mark backward instead.  The difference between N and
10564 the actual number of articles marked is returned."
10565   (interactive "p")
10566   (gnus-summary-mark-forward n gnus-expirable-mark))
10567
10568 (defun gnus-summary-mark-as-spam (n)
10569   "Mark N articles forward as spam.
10570 If N is negative, mark backward instead.  The difference between N and
10571 the actual number of articles marked is returned."
10572   (interactive "p")
10573   (gnus-summary-mark-forward n gnus-spam-mark))
10574
10575 (defun gnus-summary-mark-article-as-replied (article)
10576   "Mark ARTICLE as replied to and update the summary line.
10577 ARTICLE can also be a list of articles."
10578   (interactive (list (gnus-summary-article-number)))
10579   (let ((articles (if (listp article) article (list article))))
10580     (dolist (article articles)
10581       (unless (numberp article)
10582         (error "%s is not a number" article))
10583       (push article gnus-newsgroup-replied)
10584       (let ((inhibit-read-only t))
10585         (when (gnus-summary-goto-subject article nil t)
10586           (gnus-summary-update-secondary-mark article))))))
10587
10588 (defun gnus-summary-mark-article-as-forwarded (article)
10589   "Mark ARTICLE as forwarded and update the summary line.
10590 ARTICLE can also be a list of articles."
10591   (let ((articles (if (listp article) article (list article))))
10592     (dolist (article articles)
10593       (push article gnus-newsgroup-forwarded)
10594       (let ((inhibit-read-only t))
10595         (when (gnus-summary-goto-subject article nil t)
10596           (gnus-summary-update-secondary-mark article))))))
10597
10598 (defun gnus-summary-set-bookmark (article)
10599   "Set a bookmark in current article."
10600   (interactive (list (gnus-summary-article-number)))
10601   (when (or (not (get-buffer gnus-article-buffer))
10602             (not gnus-current-article)
10603             (not gnus-article-current)
10604             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10605     (error "No current article selected"))
10606   ;; Remove old bookmark, if one exists.
10607   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10608   ;; Set the new bookmark, which is on the form
10609   ;; (article-number . line-number-in-body).
10610   (push
10611    (cons article
10612          (with-current-buffer gnus-article-buffer
10613            (count-lines
10614             (min (point)
10615                  (save-excursion
10616                    (article-goto-body)
10617                    (point)))
10618             (point))))
10619    gnus-newsgroup-bookmarks)
10620   (gnus-message 6 "A bookmark has been added to the current article."))
10621
10622 (defun gnus-summary-remove-bookmark (article)
10623   "Remove the bookmark from the current article."
10624   (interactive (list (gnus-summary-article-number)))
10625   ;; Remove old bookmark, if one exists.
10626   (if (not (assq article gnus-newsgroup-bookmarks))
10627       (gnus-message 6 "No bookmark in current article.")
10628     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10629     (gnus-message 6 "Removed bookmark.")))
10630
10631 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10632 (defun gnus-summary-mark-as-dormant (n)
10633   "Mark N articles forward as dormant.
10634 If N is negative, mark backward instead.  The difference between N and
10635 the actual number of articles marked is returned."
10636   (interactive "p")
10637   (gnus-summary-mark-forward n gnus-dormant-mark))
10638
10639 (defun gnus-summary-set-process-mark (article)
10640   "Set the process mark on ARTICLE and update the summary line."
10641   (setq gnus-newsgroup-processable
10642         (cons article
10643               (delq article gnus-newsgroup-processable)))
10644   (when (gnus-summary-goto-subject article)
10645     (gnus-summary-show-thread)
10646     (gnus-summary-goto-subject article)
10647     (gnus-summary-update-secondary-mark article)))
10648
10649 (defun gnus-summary-remove-process-mark (&rest articles)
10650   "Remove the process mark from ARTICLES and update the summary line."
10651   (dolist (article articles)
10652     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10653     (when (gnus-summary-goto-subject article)
10654       (gnus-summary-show-thread)
10655       (gnus-summary-goto-subject article)
10656       (gnus-summary-update-secondary-mark article)))
10657   t)
10658
10659 (defun gnus-summary-set-saved-mark (article)
10660   "Set the process mark on ARTICLE and update the summary line."
10661   (push article gnus-newsgroup-saved)
10662   (when (gnus-summary-goto-subject article)
10663     (gnus-summary-update-secondary-mark article)))
10664
10665 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10666   "Mark N articles as read forwards.
10667 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10668 The difference between N and the actual number of articles marked is
10669 returned.
10670 If NO-EXPIRE, auto-expiry will be inhibited."
10671   (interactive "p")
10672   (gnus-summary-show-thread)
10673   (let ((backward (< n 0))
10674         (gnus-summary-goto-unread
10675          (and gnus-summary-goto-unread
10676               (not (eq gnus-summary-goto-unread 'never))
10677               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10678                                     gnus-ticked-mark gnus-dormant-mark)))))
10679         (n (abs n))
10680         (mark (or mark gnus-del-mark)))
10681     (while (and (> n 0)
10682                 (gnus-summary-mark-article nil mark no-expire)
10683                 (zerop (gnus-summary-next-subject
10684                         (if backward -1 1)
10685                         (and gnus-summary-goto-unread
10686                              (not (eq gnus-summary-goto-unread 'never)))
10687                         t)))
10688       (setq n (1- n)))
10689     (when (/= 0 n)
10690       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10691     (gnus-summary-recenter)
10692     (gnus-summary-position-point)
10693     (gnus-set-mode-line 'summary)
10694     n))
10695
10696 (defun gnus-summary-mark-article-as-read (mark)
10697   "Mark the current article quickly as read with MARK."
10698   (let ((article (gnus-summary-article-number)))
10699     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10700     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10701     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10702     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10703     (push (cons article mark) gnus-newsgroup-reads)
10704     ;; Possibly remove from cache, if that is used.
10705     (when gnus-use-cache
10706       (gnus-cache-enter-remove-article article))
10707     ;; Allow the backend to change the mark.
10708     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10709     ;; Check for auto-expiry.
10710     (when (and gnus-newsgroup-auto-expire
10711                (memq mark gnus-auto-expirable-marks))
10712       (setq mark gnus-expirable-mark)
10713       ;; Let the backend know about the mark change.
10714       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10715       (push article gnus-newsgroup-expirable))
10716     ;; Set the mark in the buffer.
10717     (gnus-summary-update-mark mark 'unread)
10718     t))
10719
10720 (defun gnus-summary-mark-article-as-unread (mark)
10721   "Mark the current article quickly as unread with MARK."
10722   (let* ((article (gnus-summary-article-number))
10723          (old-mark (gnus-summary-article-mark article)))
10724     ;; Allow the backend to change the mark.
10725     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10726     (if (eq mark old-mark)
10727         t
10728       (if (<= article 0)
10729           (progn
10730             (gnus-error 1 "Can't mark negative article numbers")
10731             nil)
10732         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10733         (setq gnus-newsgroup-spam-marked
10734               (delq article gnus-newsgroup-spam-marked))
10735         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10736         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10737         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10738         (cond ((= mark gnus-ticked-mark)
10739                (setq gnus-newsgroup-marked
10740                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10741                                               article)))
10742               ((= mark gnus-spam-mark)
10743                (setq gnus-newsgroup-spam-marked
10744                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10745                                               article)))
10746               ((= mark gnus-dormant-mark)
10747                (setq gnus-newsgroup-dormant
10748                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10749                                               article)))
10750               (t
10751                (setq gnus-newsgroup-unreads
10752                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10753                                               article))))
10754         (gnus-alist-pull article gnus-newsgroup-reads)
10755
10756         ;; See whether the article is to be put in the cache.
10757         (and gnus-use-cache
10758              (vectorp (gnus-summary-article-header article))
10759              (save-excursion
10760                (gnus-cache-possibly-enter-article
10761                 gnus-newsgroup-name article
10762                 (= mark gnus-ticked-mark)
10763                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10764
10765         ;; Fix the mark.
10766         (gnus-summary-update-mark mark 'unread)
10767         t))))
10768
10769 (defun gnus-summary-mark-article (&optional article mark no-expire)
10770   "Mark ARTICLE with MARK.  MARK can be any character.
10771 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10772 `??' (dormant) and `?E' (expirable).
10773 If MARK is nil, then the default character `?r' is used.
10774 If ARTICLE is nil, then the article on the current line will be
10775 marked.
10776 If NO-EXPIRE, auto-expiry will be inhibited."
10777   ;; The mark might be a string.
10778   (when (stringp mark)
10779     (setq mark (aref mark 0)))
10780   ;; If no mark is given, then we check auto-expiring.
10781   (when (null mark)
10782     (setq mark gnus-del-mark))
10783   (when (and (not no-expire)
10784              gnus-newsgroup-auto-expire
10785              (memq mark gnus-auto-expirable-marks))
10786     (setq mark gnus-expirable-mark))
10787   (let ((article (or article (gnus-summary-article-number)))
10788         (old-mark (gnus-summary-article-mark article)))
10789     ;; Allow the backend to change the mark.
10790     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10791     (if (eq mark old-mark)
10792         t
10793       (unless article
10794         (error "No article on current line"))
10795       (if (not (if (or (= mark gnus-unread-mark)
10796                        (= mark gnus-ticked-mark)
10797                        (= mark gnus-spam-mark)
10798                        (= mark gnus-dormant-mark))
10799                    (gnus-mark-article-as-unread article mark)
10800                  (gnus-mark-article-as-read article mark)))
10801           t
10802         ;; See whether the article is to be put in the cache.
10803         (and gnus-use-cache
10804              (not (= mark gnus-canceled-mark))
10805              (vectorp (gnus-summary-article-header article))
10806              (save-excursion
10807                (gnus-cache-possibly-enter-article
10808                 gnus-newsgroup-name article
10809                 (= mark gnus-ticked-mark)
10810                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10811
10812         (when (gnus-summary-goto-subject article nil t)
10813           (let ((inhibit-read-only t))
10814             (gnus-summary-show-thread)
10815             ;; Fix the mark.
10816             (gnus-summary-update-mark mark 'unread)
10817             t))))))
10818
10819 (defun gnus-summary-update-secondary-mark (article)
10820   "Update the secondary (read, process, cache) mark."
10821   (gnus-summary-update-mark
10822    (cond ((memq article gnus-newsgroup-processable)
10823           gnus-process-mark)
10824          ((memq article gnus-newsgroup-cached)
10825           gnus-cached-mark)
10826          ((memq article gnus-newsgroup-replied)
10827           gnus-replied-mark)
10828          ((memq article gnus-newsgroup-forwarded)
10829           gnus-forwarded-mark)
10830          ((memq article gnus-newsgroup-saved)
10831           gnus-saved-mark)
10832          ((memq article gnus-newsgroup-recent)
10833           gnus-recent-mark)
10834          ((memq article gnus-newsgroup-unseen)
10835           gnus-unseen-mark)
10836          (t gnus-no-mark))
10837    'replied)
10838   (when (gnus-visual-p 'summary-highlight 'highlight)
10839     (gnus-summary-highlight-line)
10840     (gnus-run-hooks 'gnus-summary-update-hook))
10841   t)
10842
10843 (defun gnus-summary-update-download-mark (article)
10844   "Update the download mark."
10845   (gnus-summary-update-mark
10846    (cond ((memq article gnus-newsgroup-undownloaded)
10847           gnus-undownloaded-mark)
10848          (gnus-newsgroup-agentized
10849           gnus-downloaded-mark)
10850          (t
10851           gnus-no-mark))
10852    'download)
10853   (gnus-summary-update-line t)
10854   t)
10855
10856 (defun gnus-summary-update-mark (mark type)
10857   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10858         (inhibit-read-only t))
10859     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10860     (when forward
10861       (when (looking-at "\r")
10862         (incf forward))
10863       (when (<= (+ forward (point)) (point-max))
10864         ;; Go to the right position on the line.
10865         (goto-char (+ forward (point)))
10866         ;; Replace the old mark with the new mark.
10867         (let ((to-insert
10868                (mm-subst-char-in-string
10869                 (char-after) mark
10870                 (buffer-substring (point) (1+ (point))))))
10871           (delete-region (point) (1+ (point)))
10872           (insert to-insert))
10873         ;; Optionally update the marks by some user rule.
10874         (when (eq type 'unread)
10875           (gnus-data-set-mark
10876            (gnus-data-find (gnus-summary-article-number)) mark)
10877           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10878
10879 (defun gnus-mark-article-as-read (article &optional mark)
10880   "Enter ARTICLE in the pertinent lists and remove it from others."
10881   ;; Make the article expirable.
10882   (let ((mark (or mark gnus-del-mark)))
10883     (setq gnus-newsgroup-expirable
10884           (if (= mark gnus-expirable-mark)
10885               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10886             (delq article gnus-newsgroup-expirable)))
10887     ;; Remove from unread and marked lists.
10888     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10889     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10890     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10891     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10892     (push (cons article mark) gnus-newsgroup-reads)
10893     ;; Possibly remove from cache, if that is used.
10894     (when gnus-use-cache
10895       (gnus-cache-enter-remove-article article))
10896     t))
10897
10898 (defun gnus-mark-article-as-unread (article &optional mark)
10899   "Enter ARTICLE in the pertinent lists and remove it from others."
10900   (let ((mark (or mark gnus-ticked-mark)))
10901     (if (<= article 0)
10902         (progn
10903           (gnus-error 1 "Can't mark negative article numbers")
10904           nil)
10905       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10906             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10907             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10908             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10909             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10910
10911       ;; Unsuppress duplicates?
10912       (when gnus-suppress-duplicates
10913         (gnus-dup-unsuppress-article article))
10914
10915       (cond ((= mark gnus-ticked-mark)
10916              (setq gnus-newsgroup-marked
10917                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10918             ((= mark gnus-spam-mark)
10919              (setq gnus-newsgroup-spam-marked
10920                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10921                                             article)))
10922             ((= mark gnus-dormant-mark)
10923              (setq gnus-newsgroup-dormant
10924                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10925             (t
10926              (setq gnus-newsgroup-unreads
10927                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10928       (gnus-alist-pull article gnus-newsgroup-reads)
10929       t)))
10930
10931 (defun gnus-summary-tick-article-forward (n)
10932   "Tick N articles forwards.
10933 If N is negative, tick backwards instead.
10934 The difference between N and the number of articles ticked is returned."
10935   (interactive "p")
10936   (gnus-summary-mark-forward n gnus-ticked-mark))
10937
10938 (defun gnus-summary-tick-article-backward (n)
10939   "Tick N articles backwards.
10940 The difference between N and the number of articles ticked is returned."
10941   (interactive "p")
10942   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10943
10944 (defun gnus-summary-tick-article (&optional article clear-mark)
10945   "Mark current article as unread.
10946 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10947 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10948   (interactive)
10949   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10950                                        gnus-ticked-mark)))
10951
10952 (defun gnus-summary-mark-as-read-forward (n)
10953   "Mark N articles as read forwards.
10954 If N is negative, mark backwards instead.
10955 The difference between N and the actual number of articles marked is
10956 returned."
10957   (interactive "p")
10958   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10959
10960 (defun gnus-summary-mark-as-read-backward (n)
10961   "Mark the N articles as read backwards.
10962 The difference between N and the actual number of articles marked is
10963 returned."
10964   (interactive "p")
10965   (gnus-summary-mark-forward
10966    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10967
10968 (defun gnus-summary-mark-as-read (&optional article mark)
10969   "Mark current article as read.
10970 ARTICLE specifies the article to be marked as read.
10971 MARK specifies a string to be inserted at the beginning of the line."
10972   (gnus-summary-mark-article article mark))
10973
10974 (defun gnus-summary-clear-mark-forward (n)
10975   "Clear marks from N articles forward.
10976 If N is negative, clear backward instead.
10977 The difference between N and the number of marks cleared is returned."
10978   (interactive "p")
10979   (gnus-summary-mark-forward n gnus-unread-mark))
10980
10981 (defun gnus-summary-clear-mark-backward (n)
10982   "Clear marks from N articles backward.
10983 The difference between N and the number of marks cleared is returned."
10984   (interactive "p")
10985   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10986
10987 (defun gnus-summary-mark-unread-as-read ()
10988   "Intended to be used by `gnus-mark-article-hook'."
10989   (when (memq gnus-current-article gnus-newsgroup-unreads)
10990     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10991
10992 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10993   "Intended to be used by `gnus-mark-article-hook'."
10994   (let ((mark (gnus-summary-article-mark)))
10995     (when (or (gnus-unread-mark-p mark)
10996               (gnus-read-mark-p mark))
10997       (gnus-summary-mark-article gnus-current-article
10998                                  (or new-mark gnus-read-mark)))))
10999
11000 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11001   "Intended to be used by `gnus-mark-article-hook'."
11002   (let ((mark (gnus-summary-article-mark)))
11003     (when (or (gnus-unread-mark-p mark)
11004               (gnus-read-mark-p mark))
11005       (gnus-summary-mark-article (gnus-summary-article-number)
11006                                  (or new-mark gnus-read-mark)))))
11007
11008 (defun gnus-summary-mark-unread-as-ticked ()
11009   "Intended to be used by `gnus-mark-article-hook'."
11010   (when (memq gnus-current-article gnus-newsgroup-unreads)
11011     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11012
11013 (defun gnus-summary-mark-region-as-read (point mark all)
11014   "Mark all unread articles between point and mark as read.
11015 If given a prefix, mark all articles between point and mark as read,
11016 even ticked and dormant ones."
11017   (interactive "r\nP")
11018   (save-excursion
11019     (let (article)
11020       (goto-char point)
11021       (beginning-of-line)
11022       (while (and
11023               (< (point) mark)
11024               (progn
11025                 (when (or all
11026                           (memq (setq article (gnus-summary-article-number))
11027                                 gnus-newsgroup-unreads))
11028                   (gnus-summary-mark-article article gnus-del-mark))
11029                 t)
11030               (gnus-summary-find-next))))))
11031
11032 (defun gnus-summary-mark-below (score mark)
11033   "Mark articles with score less than SCORE with MARK."
11034   (interactive "P\ncMark: ")
11035   (setq score (if score
11036                   (prefix-numeric-value score)
11037                 (or gnus-summary-default-score 0)))
11038   (with-current-buffer gnus-summary-buffer
11039     (goto-char (point-min))
11040     (while
11041         (progn
11042           (and (< (gnus-summary-article-score) score)
11043                (gnus-summary-mark-article nil mark))
11044           (gnus-summary-find-next)))))
11045
11046 (defun gnus-summary-kill-below (&optional score)
11047   "Mark articles with score below SCORE as read."
11048   (interactive "P")
11049   (gnus-summary-mark-below score gnus-killed-mark))
11050
11051 (defun gnus-summary-clear-above (&optional score)
11052   "Clear all marks from articles with score above SCORE."
11053   (interactive "P")
11054   (gnus-summary-mark-above score gnus-unread-mark))
11055
11056 (defun gnus-summary-tick-above (&optional score)
11057   "Tick all articles with score above SCORE."
11058   (interactive "P")
11059   (gnus-summary-mark-above score gnus-ticked-mark))
11060
11061 (defun gnus-summary-mark-above (score mark)
11062   "Mark articles with score over SCORE with MARK."
11063   (interactive "P\ncMark: ")
11064   (setq score (if score
11065                   (prefix-numeric-value score)
11066                 (or gnus-summary-default-score 0)))
11067   (with-current-buffer gnus-summary-buffer
11068     (goto-char (point-min))
11069     (while (and (progn
11070                   (when (> (gnus-summary-article-score) score)
11071                     (gnus-summary-mark-article nil mark))
11072                   t)
11073                 (gnus-summary-find-next)))))
11074
11075 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11076 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11077 (defun gnus-summary-limit-include-expunged (&optional no-error)
11078   "Display all the hidden articles that were expunged for low scores."
11079   (interactive)
11080   (let ((inhibit-read-only t))
11081     (let ((scored gnus-newsgroup-scored)
11082           headers h)
11083       (while scored
11084         (unless (gnus-summary-article-header (caar scored))
11085           (and (setq h (gnus-number-to-header (caar scored)))
11086                (< (cdar scored) gnus-summary-expunge-below)
11087                (push h headers)))
11088         (setq scored (cdr scored)))
11089       (if (not headers)
11090           (when (not no-error)
11091             (error "No expunged articles hidden"))
11092         (goto-char (point-min))
11093         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11094         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11095         (dolist (x headers)
11096           (push (mail-header-number x) gnus-newsgroup-limit))
11097         (gnus-summary-prepare-unthreaded (nreverse headers))
11098         (goto-char (point-min))
11099         (gnus-summary-position-point)
11100         t))))
11101
11102 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11103   "Mark all unread articles in this newsgroup as read.
11104 If prefix argument ALL is non-nil, ticked and dormant articles will
11105 also be marked as read.
11106 If QUIETLY is non-nil, no questions will be asked.
11107
11108 If TO-HERE is non-nil, it should be a point in the buffer.  All
11109 articles before (after, if REVERSE is set) this point will be marked
11110 as read.
11111
11112 Note that this function will only catch up the unread article
11113 in the current summary buffer limitation.
11114
11115 The number of articles marked as read is returned."
11116   (interactive "P")
11117   (prog1
11118       (save-excursion
11119         (when (or quietly
11120                   (not gnus-interactive-catchup) ;Without confirmation?
11121                   gnus-expert-user
11122                   (gnus-y-or-n-p
11123                    (if all
11124                        "Mark absolutely all articles as read? "
11125                      "Mark all unread articles as read? ")))
11126           (if (and not-mark
11127                    (not gnus-newsgroup-adaptive)
11128                    (not gnus-newsgroup-auto-expire)
11129                    (not gnus-suppress-duplicates)
11130                    (or (not gnus-use-cache)
11131                        (eq gnus-use-cache 'passive)))
11132               (progn
11133                 (when all
11134                   (setq gnus-newsgroup-marked nil
11135                         gnus-newsgroup-spam-marked nil
11136                         gnus-newsgroup-dormant nil))
11137                 (setq gnus-newsgroup-unreads
11138                       (gnus-sorted-nunion
11139                        (gnus-sorted-intersection gnus-newsgroup-unreads
11140                                                  gnus-newsgroup-downloadable)
11141                        (gnus-sorted-difference gnus-newsgroup-unfetched
11142                                                gnus-newsgroup-cached))))
11143             ;; We actually mark all articles as canceled, which we
11144             ;; have to do when using auto-expiry or adaptive scoring.
11145             (gnus-summary-show-all-threads)
11146             (if (and to-here reverse)
11147                 (progn
11148                   (goto-char to-here)
11149                   (gnus-summary-mark-current-read-and-unread-as-read
11150                    gnus-catchup-mark)
11151                   (while (gnus-summary-find-next (not all))
11152                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11153               (when (gnus-summary-first-subject (not all))
11154                 (while (and
11155                         (if to-here (< (point) to-here) t)
11156                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11157                         (gnus-summary-find-next (not all))))))
11158             (gnus-set-mode-line 'summary))
11159           t))
11160     (gnus-summary-position-point)))
11161
11162 (defun gnus-summary-catchup-to-here (&optional all)
11163   "Mark all unticked articles before the current one as read.
11164 If ALL is non-nil, also mark ticked and dormant articles as read."
11165   (interactive "P")
11166   (save-excursion
11167     (gnus-save-hidden-threads
11168       (let ((beg (point)))
11169         ;; We check that there are unread articles.
11170         (when (or all (gnus-summary-find-prev))
11171           (gnus-summary-catchup all t beg)))))
11172   (gnus-summary-position-point))
11173
11174 (defun gnus-summary-catchup-from-here (&optional all)
11175   "Mark all unticked articles after (and including) the current one as read.
11176 If ALL is non-nil, also mark ticked and dormant articles as read."
11177   (interactive "P")
11178   (save-excursion
11179     (gnus-save-hidden-threads
11180       (let ((beg (point)))
11181         ;; We check that there are unread articles.
11182         (when (or all (gnus-summary-find-next))
11183           (gnus-summary-catchup all t beg nil t)))))
11184   (gnus-summary-position-point))
11185
11186 (defun gnus-summary-catchup-all (&optional quietly)
11187   "Mark all articles in this newsgroup as read.
11188 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11189 instead, which marks only unread articles as read."
11190   (interactive "P")
11191   (gnus-summary-catchup t quietly))
11192
11193 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11194   "Mark all unread articles in this group as read, then exit.
11195 If prefix argument ALL is non-nil, all articles are marked as read.
11196 If QUIETLY is non-nil, no questions will be asked."
11197   (interactive "P")
11198   (when (gnus-summary-catchup all quietly nil 'fast)
11199     ;; Select next newsgroup or exit.
11200     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11201              (eq gnus-auto-select-next 'quietly))
11202         (gnus-summary-next-group nil)
11203       (gnus-summary-exit))))
11204
11205 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11206   "Mark all articles in this newsgroup as read, and then exit.
11207 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11208 instead, which marks only unread articles as read."
11209   (interactive "P")
11210   (gnus-summary-catchup-and-exit t quietly))
11211
11212 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11213   "Mark all articles in this group as read and select the next group.
11214 If given a prefix, mark all articles, unread as well as ticked, as
11215 read."
11216   (interactive "P")
11217   (save-excursion
11218     (gnus-summary-catchup all))
11219   (gnus-summary-next-group))
11220
11221 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11222   "Mark all articles in this group as read and select the previous group.
11223 If given a prefix, mark all articles, unread as well as ticked, as
11224 read."
11225   (interactive "P")
11226   (save-excursion
11227     (gnus-summary-catchup all))
11228   (gnus-summary-next-group nil nil t))
11229
11230 ;;;
11231 ;;; with article
11232 ;;;
11233
11234 (defmacro gnus-with-article (article &rest forms)
11235   "Select ARTICLE and perform FORMS in the original article buffer.
11236 Then replace the article with the result."
11237   `(progn
11238      ;; We don't want the article to be marked as read.
11239      (let (gnus-mark-article-hook)
11240        (gnus-summary-select-article t t nil ,article))
11241      (set-buffer gnus-original-article-buffer)
11242      ,@forms
11243      (if (not (gnus-check-backend-function
11244                'request-replace-article (car gnus-article-current)))
11245          (gnus-message 5 "Read-only group; not replacing")
11246        (unless (gnus-request-replace-article
11247                 ,article (car gnus-article-current)
11248                 (current-buffer) t)
11249          (error "Couldn't replace article")))
11250      ;; The cache and backlog have to be flushed somewhat.
11251      (when gnus-keep-backlog
11252        (gnus-backlog-remove-article
11253         (car gnus-article-current) (cdr gnus-article-current)))
11254      (when gnus-use-cache
11255        (gnus-cache-update-article
11256         (car gnus-article-current) (cdr gnus-article-current)))))
11257
11258 (put 'gnus-with-article 'lisp-indent-function 1)
11259 (put 'gnus-with-article 'edebug-form-spec '(form body))
11260
11261 ;; Thread-based commands.
11262
11263 (defun gnus-summary-articles-in-thread (&optional article)
11264   "Return a list of all articles in the current thread.
11265 If ARTICLE is non-nil, return all articles in the thread that starts
11266 with that article."
11267   (let* ((article (or article (gnus-summary-article-number)))
11268          (data (gnus-data-find-list article))
11269          (top-level (gnus-data-level (car data)))
11270          (top-subject
11271           (cond ((null gnus-thread-operation-ignore-subject)
11272                  (gnus-simplify-subject-re
11273                   (mail-header-subject (gnus-data-header (car data)))))
11274                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11275                  (gnus-simplify-subject-fuzzy
11276                   (mail-header-subject (gnus-data-header (car data)))))
11277                 (t nil)))
11278          (end-point (save-excursion
11279                       (goto-char (gnus-data-pos (car data)))
11280                       (if (gnus-summary-go-to-next-thread)
11281                           (point) (point-max))))
11282          articles)
11283     (while (and data
11284                 (< (gnus-data-pos (car data)) end-point))
11285       (when (or (not top-subject)
11286                 (string= top-subject
11287                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11288                              (gnus-simplify-subject-fuzzy
11289                               (mail-header-subject
11290                                (gnus-data-header (car data))))
11291                            (gnus-simplify-subject-re
11292                             (mail-header-subject
11293                              (gnus-data-header (car data)))))))
11294         (push (gnus-data-number (car data)) articles))
11295       (unless (and (setq data (cdr data))
11296                    (> (gnus-data-level (car data)) top-level))
11297         (setq data nil)))
11298     ;; Return the list of articles.
11299     (nreverse articles)))
11300
11301 (defun gnus-summary-rethread-current ()
11302   "Rethread the thread the current article is part of."
11303   (interactive)
11304   (let* ((gnus-show-threads t)
11305          (article (gnus-summary-article-number))
11306          (id (mail-header-id (gnus-summary-article-header)))
11307          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11308     (unless id
11309       (error "No article on the current line"))
11310     (gnus-rebuild-thread id)
11311     (gnus-summary-goto-subject article)))
11312
11313 (defun gnus-summary-reparent-thread ()
11314   "Make the current article child of the marked (or previous) article.
11315
11316 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11317 is non-nil or the Subject: of both articles are the same."
11318   (interactive)
11319   (unless (not (gnus-group-read-only-p))
11320     (error "The current newsgroup does not support article editing"))
11321   (unless (<= (length gnus-newsgroup-processable) 1)
11322     (error "No more than one article may be marked"))
11323   (let ((child (gnus-summary-article-number))
11324         ;; First grab the marked article, otherwise one line up.
11325         (parent (if (not (null gnus-newsgroup-processable))
11326                     (car gnus-newsgroup-processable)
11327                   (save-excursion
11328                     (if (eq (forward-line -1) 0)
11329                         (gnus-summary-article-number)
11330                       (error "Beginning of summary buffer"))))))
11331     (gnus-summary-reparent-children parent (list child))))
11332
11333 (defun gnus-summary-reparent-children (parent children)
11334   "Make PARENT the parent of CHILDREN.
11335 When called interactively, PARENT is the current article and CHILDREN
11336 are the process-marked articles."
11337   (interactive
11338    (list (gnus-summary-article-number)
11339          (gnus-summary-work-articles nil)))
11340   (dolist (child children)
11341     (save-window-excursion
11342       (let ((gnus-article-buffer " *reparent*"))
11343         (unless (not (eq parent child))
11344           (error "An article may not be self-referential"))
11345         (let ((message-id (mail-header-id
11346                            (gnus-summary-article-header parent))))
11347           (unless (and message-id (not (equal message-id "")))
11348             (error "No message-id in desired parent"))
11349           (gnus-with-article child
11350             (save-restriction
11351               (goto-char (point-min))
11352               (message-narrow-to-head)
11353               (if (re-search-forward "^References: " nil t)
11354                   (progn
11355                     (re-search-forward "^[^ \t]" nil t)
11356                     (forward-line -1)
11357                     (end-of-line)
11358                     (insert " " message-id))
11359                 (insert "References: " message-id "\n"))))
11360           (set-buffer gnus-summary-buffer)
11361           (gnus-summary-unmark-all-processable)
11362           (gnus-summary-update-article child)
11363           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11364             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11365           (gnus-summary-rethread-current)
11366           (gnus-message 3 "Article %d is now the child of article %d"
11367                         child parent))))))
11368
11369 (defun gnus-summary-toggle-threads (&optional arg)
11370   "Toggle showing conversation threads.
11371 If ARG is positive number, turn showing conversation threads on."
11372   (interactive "P")
11373   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11374     (setq gnus-show-threads
11375           (if (null arg) (not gnus-show-threads)
11376             (> (prefix-numeric-value arg) 0)))
11377     (gnus-summary-prepare)
11378     (gnus-summary-goto-subject current)
11379     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11380     (gnus-summary-position-point)))
11381
11382 (eval-and-compile
11383   (if (fboundp 'remove-overlays)
11384       (defalias 'gnus-remove-overlays 'remove-overlays)
11385     (defun gnus-remove-overlays (beg end name val)
11386       "Clear BEG and END of overlays whose property NAME has value VAL.
11387 For compatibility with XEmacs."
11388       (dolist (ov (gnus-overlays-in beg end))
11389         (when (eq (gnus-overlay-get ov name) val)
11390           (gnus-delete-overlay ov))))))
11391
11392 (defun gnus-summary-show-all-threads ()
11393   "Show all threads."
11394   (interactive)
11395   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11396   (gnus-summary-position-point))
11397
11398 (defsubst gnus-summary--inv (p)
11399   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11400
11401 (defun gnus-summary-show-thread ()
11402   "Show thread subtrees.
11403 Returns nil if no thread was there to be shown."
11404   (interactive)
11405   (let* ((orig (point))
11406          (end (point-at-eol))
11407          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11408          ;; Leave point at bol
11409          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11410          (eoi (when end
11411                 (if (fboundp 'next-single-char-property-change)
11412                     (or (next-single-char-property-change end 'invisible)
11413                         (point-max))
11414                   (while (progn
11415                            (end-of-line 2)
11416                            (and (not (eobp))
11417                                 (eq (get-char-property (point) 'invisible)
11418                                     'gnus-sum))))
11419                   (point)))))
11420     (when eoi
11421       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11422       (goto-char orig)
11423       (gnus-summary-position-point)
11424       eoi)))
11425
11426 (defun gnus-summary-maybe-hide-threads ()
11427   "If requested, hide the threads that should be hidden."
11428   (when (and gnus-show-threads
11429              gnus-thread-hide-subtree)
11430     (gnus-summary-hide-all-threads
11431      (if (or (consp gnus-thread-hide-subtree)
11432              (functionp gnus-thread-hide-subtree))
11433          (gnus-make-predicate gnus-thread-hide-subtree)
11434        nil))))
11435
11436 ;;; Hiding predicates.
11437
11438 (defun gnus-article-unread-p (header)
11439   (memq (mail-header-number header) gnus-newsgroup-unreads))
11440
11441 (defun gnus-article-unseen-p (header)
11442   (memq (mail-header-number header) gnus-newsgroup-unseen))
11443
11444 (defun gnus-map-articles (predicate articles)
11445   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11446   (apply 'gnus-or (mapcar predicate
11447                           (mapcar (lambda (number)
11448                                     (gnus-summary-article-header number))
11449                                   articles))))
11450
11451 (defun gnus-summary-hide-all-threads (&optional predicate)
11452   "Hide all thread subtrees.
11453 If PREDICATE is supplied, threads that satisfy this predicate
11454 will not be hidden."
11455   (interactive)
11456   (save-excursion
11457     (goto-char (point-min))
11458     (let ((end nil))
11459       (while (not end)
11460         (when (or (not predicate)
11461                   (gnus-map-articles
11462                    predicate (gnus-summary-article-children)))
11463             (gnus-summary-hide-thread))
11464         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11465   (gnus-summary-position-point))
11466
11467 (defun gnus-summary-hide-thread ()
11468   "Hide thread subtrees.
11469 If PREDICATE is supplied, threads that satisfy this predicate
11470 will not be hidden.
11471 Returns nil if no threads were there to be hidden."
11472   (interactive)
11473   (let ((start (point))
11474         (starteol (line-end-position))
11475         (article (gnus-summary-article-number)))
11476     (goto-char start)
11477     ;; Go forward until either the buffer ends or the subthread ends.
11478     (when (and (not (eobp))
11479                (or (zerop (gnus-summary-next-thread 1 t))
11480                    (goto-char (point-max))))
11481       (if (and (> (point) start)
11482                ;; FIXME: this should actually search for a non-invisible \n.
11483                (search-backward "\n" start t))
11484           (progn
11485             (when (> (point) starteol)
11486               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11487               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11488                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11489                 (gnus-overlay-put ol 'evaporate t)))
11490             (gnus-summary-goto-subject article))
11491         (goto-char start)
11492         nil))))
11493
11494 (defun gnus-summary-go-to-next-thread (&optional previous)
11495   "Go to the same level (or less) next thread.
11496 If PREVIOUS is non-nil, go to previous thread instead.
11497 Return the article number moved to, or nil if moving was impossible."
11498   (let ((level (gnus-summary-thread-level))
11499         (way (if previous -1 1))
11500         (beg (point)))
11501     (forward-line way)
11502     (while (and (not (eobp))
11503                 (< level (gnus-summary-thread-level)))
11504       (forward-line way))
11505     (if (eobp)
11506         (progn
11507           (goto-char beg)
11508           nil)
11509       (setq beg (point))
11510       (prog1
11511           (gnus-summary-article-number)
11512         (goto-char beg)))))
11513
11514 (defun gnus-summary-next-thread (n &optional silent)
11515   "Go to the same level next N'th thread.
11516 If N is negative, search backward instead.
11517 Returns the difference between N and the number of skips actually
11518 done.
11519
11520 If SILENT, don't output messages."
11521   (interactive "p")
11522   (let ((backward (< n 0))
11523         (n (abs n)))
11524     (while (and (> n 0)
11525                 (gnus-summary-go-to-next-thread backward))
11526       (decf n))
11527     (unless silent
11528       (gnus-summary-position-point))
11529     (when (and (not silent) (/= 0 n))
11530       (gnus-message 7 "No more threads"))
11531     n))
11532
11533 (defun gnus-summary-prev-thread (n)
11534   "Go to the same level previous N'th thread.
11535 Returns the difference between N and the number of skips actually
11536 done."
11537   (interactive "p")
11538   (gnus-summary-next-thread (- n)))
11539
11540 (defun gnus-summary-go-down-thread ()
11541   "Go down one level in the current thread."
11542   (let ((children (gnus-summary-article-children)))
11543     (when children
11544       (gnus-summary-goto-subject (car children)))))
11545
11546 (defun gnus-summary-go-up-thread ()
11547   "Go up one level in the current thread."
11548   (let ((parent (gnus-summary-article-parent)))
11549     (when parent
11550       (gnus-summary-goto-subject parent))))
11551
11552 (defun gnus-summary-down-thread (n)
11553   "Go down thread N steps.
11554 If N is negative, go up instead.
11555 Returns the difference between N and how many steps down that were
11556 taken."
11557   (interactive "p")
11558   (let ((up (< n 0))
11559         (n (abs n)))
11560     (while (and (> n 0)
11561                 (if up (gnus-summary-go-up-thread)
11562                   (gnus-summary-go-down-thread)))
11563       (setq n (1- n)))
11564     (gnus-summary-position-point)
11565     (when (/= 0 n)
11566       (gnus-message 7 "Can't go further"))
11567     n))
11568
11569 (defun gnus-summary-up-thread (n)
11570   "Go up thread N steps.
11571 If N is negative, go down instead.
11572 Returns the difference between N and how many steps down that were
11573 taken."
11574   (interactive "p")
11575   (gnus-summary-down-thread (- n)))
11576
11577 (defun gnus-summary-top-thread ()
11578   "Go to the top of the thread."
11579   (interactive)
11580   (while (gnus-summary-go-up-thread))
11581   (gnus-summary-article-number))
11582
11583 (defun gnus-summary-expire-thread ()
11584   "Mark articles under current thread as expired."
11585   (interactive)
11586   (gnus-summary-kill-thread 0))
11587
11588 (defun gnus-summary-kill-thread (&optional unmark)
11589   "Mark articles under current thread as read.
11590 If the prefix argument is positive, remove any kinds of marks.
11591 If the prefix argument is zero, mark thread as expired.
11592 If the prefix argument is negative, tick articles instead."
11593   (interactive "P")
11594   (when unmark
11595     (setq unmark (prefix-numeric-value unmark)))
11596   (let ((articles (gnus-summary-articles-in-thread))
11597         (hide (or (null unmark) (= unmark 0))))
11598     (save-excursion
11599       ;; Expand the thread.
11600       (gnus-summary-show-thread)
11601       ;; Mark all the articles.
11602       (while articles
11603         (gnus-summary-goto-subject (car articles))
11604         (cond ((null unmark)
11605                (gnus-summary-mark-article-as-read gnus-killed-mark))
11606               ((> unmark 0)
11607                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11608               ((= unmark 0)
11609                (gnus-summary-mark-article nil gnus-expirable-mark))
11610               (t
11611                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11612         (setq articles (cdr articles))))
11613     ;; Hide killed subtrees when hide is true.
11614     (and hide
11615          gnus-thread-hide-killed
11616          (gnus-summary-hide-thread))
11617     ;; If hide is t, go to next unread subject.
11618     (when hide
11619       ;; Go to next unread subject.
11620       (gnus-summary-next-subject 1 t)))
11621   (gnus-set-mode-line 'summary))
11622
11623 ;; Summary sorting commands
11624
11625 (defun gnus-summary-sort-by-number (&optional reverse)
11626   "Sort the summary buffer by article number.
11627 Argument REVERSE means reverse order."
11628   (interactive "P")
11629   (gnus-summary-sort 'number reverse))
11630
11631 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11632   "Sort the summary buffer by most recent article number.
11633 Argument REVERSE means reverse order."
11634   (interactive "P")
11635   (gnus-summary-sort 'most-recent-number reverse))
11636
11637 (defun gnus-summary-sort-by-random (&optional reverse)
11638   "Randomize the order in the summary buffer.
11639 Argument REVERSE means to randomize in reverse order."
11640   (interactive "P")
11641   (gnus-summary-sort 'random reverse))
11642
11643 (defun gnus-summary-sort-by-author (&optional reverse)
11644   "Sort the summary buffer by author name alphabetically.
11645 If `case-fold-search' is non-nil, case of letters is ignored.
11646 Argument REVERSE means reverse order."
11647   (interactive "P")
11648   (gnus-summary-sort 'author reverse))
11649
11650 (defun gnus-summary-sort-by-recipient (&optional reverse)
11651   "Sort the summary buffer by recipient name alphabetically.
11652 If `case-fold-search' is non-nil, case of letters is ignored.
11653 Argument REVERSE means reverse order."
11654   (interactive "P")
11655   (gnus-summary-sort 'recipient reverse))
11656
11657 (defun gnus-summary-sort-by-subject (&optional reverse)
11658   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11659 If `case-fold-search' is non-nil, case of letters is ignored.
11660 Argument REVERSE means reverse order."
11661   (interactive "P")
11662   (gnus-summary-sort 'subject reverse))
11663
11664 (defun gnus-summary-sort-by-date (&optional reverse)
11665   "Sort the summary buffer by date.
11666 Argument REVERSE means reverse order."
11667   (interactive "P")
11668   (gnus-summary-sort 'date reverse))
11669
11670 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11671   "Sort the summary buffer by most recent date.
11672 Argument REVERSE means reverse order."
11673   (interactive "P")
11674   (gnus-summary-sort 'most-recent-date reverse))
11675
11676 (defun gnus-summary-sort-by-score (&optional reverse)
11677   "Sort the summary buffer by score.
11678 Argument REVERSE means reverse order."
11679   (interactive "P")
11680   (gnus-summary-sort 'score reverse))
11681
11682 (defun gnus-summary-sort-by-lines (&optional reverse)
11683   "Sort the summary buffer by the number of lines.
11684 Argument REVERSE means reverse order."
11685   (interactive "P")
11686   (gnus-summary-sort 'lines reverse))
11687
11688 (defun gnus-summary-sort-by-chars (&optional reverse)
11689   "Sort the summary buffer by article length.
11690 Argument REVERSE means reverse order."
11691   (interactive "P")
11692   (gnus-summary-sort 'chars reverse))
11693
11694 (defun gnus-summary-sort-by-original (&optional reverse)
11695   "Sort the summary buffer using the default sorting method.
11696 Argument REVERSE means reverse order."
11697   (interactive "P")
11698   (let* ((inhibit-read-only t)
11699          (gnus-summary-prepare-hook nil))
11700     ;; We do the sorting by regenerating the threads.
11701     (gnus-summary-prepare)
11702     ;; Hide subthreads if needed.
11703     (gnus-summary-maybe-hide-threads)))
11704
11705 (defun gnus-summary-sort (predicate reverse)
11706   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11707   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11708          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11709          (gnus-thread-sort-functions
11710           (if (not reverse)
11711               thread
11712             `(lambda (t1 t2)
11713                (,thread t2 t1))))
11714          (gnus-sort-gathered-threads-function
11715           gnus-thread-sort-functions)
11716          (gnus-article-sort-functions
11717           (if (not reverse)
11718               article
11719             `(lambda (t1 t2)
11720                (,article t2 t1))))
11721          (inhibit-read-only t)
11722          (gnus-summary-prepare-hook nil))
11723     ;; We do the sorting by regenerating the threads.
11724     (gnus-summary-prepare)
11725     ;; Hide subthreads if needed.
11726     (gnus-summary-maybe-hide-threads)))
11727
11728 ;; Summary saving commands.
11729
11730 (defun gnus-summary-save-article (&optional n not-saved)
11731   "Save the current article using the default saver function.
11732 If N is a positive number, save the N next articles.
11733 If N is a negative number, save the N previous articles.
11734 If N is nil and any articles have been marked with the process mark,
11735 save those articles instead.
11736 The variable `gnus-default-article-saver' specifies the saver function.
11737
11738 If the optional second argument NOT-SAVED is non-nil, articles saved
11739 will not be marked as saved."
11740   (interactive "P")
11741   (require 'gnus-art)
11742   (let* ((articles (gnus-summary-work-articles n))
11743          (save-buffer (save-excursion
11744                         (nnheader-set-temp-buffer " *Gnus Save*")))
11745          (num (length articles))
11746          ;; Whether to save decoded articles or raw articles.
11747          (decode (when gnus-article-save-coding-system
11748                    (get gnus-default-article-saver :decode)))
11749          ;; When saving many articles in a single file, use the other
11750          ;; function to save articles other than the first one.
11751          (saver2 (get gnus-default-article-saver :function))
11752          (gnus-prompt-before-saving (if saver2
11753                                         t
11754                                       gnus-prompt-before-saving))
11755          (gnus-default-article-saver gnus-default-article-saver)
11756          header file)
11757     (dolist (article articles)
11758       (setq header (gnus-summary-article-header article))
11759       (if (not (vectorp header))
11760           ;; This is a pseudo-article.
11761           (if (assq 'name header)
11762               (gnus-copy-file (cdr (assq 'name header)))
11763             (gnus-message 1 "Article %d is unsaveable" article))
11764         ;; This is a real article.
11765         (save-window-excursion
11766           (gnus-summary-select-article decode decode nil article)
11767           (gnus-summary-goto-subject article))
11768         (with-current-buffer save-buffer
11769           (erase-buffer)
11770           (insert-buffer-substring (if decode
11771                                        gnus-article-buffer
11772                                      gnus-original-article-buffer)))
11773         (setq file (gnus-article-save save-buffer file num))
11774         (gnus-summary-remove-process-mark article)
11775         (unless not-saved
11776           (gnus-summary-set-saved-mark article)))
11777       (when saver2
11778         (setq gnus-default-article-saver saver2
11779               saver2 nil)))
11780     (gnus-kill-buffer save-buffer)
11781     (gnus-summary-position-point)
11782     (gnus-set-mode-line 'summary)
11783     n))
11784
11785 (defun gnus-summary-pipe-output (&optional n sym)
11786   "Pipe the current article to a subprocess.
11787 If N is a positive number, pipe the N next articles.
11788 If N is a negative number, pipe the N previous articles.
11789 If N is nil and any articles have been marked with the process mark,
11790 pipe those articles instead.
11791 The default command to which articles are piped is specified by the
11792 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11793 will be prompted for the command.
11794
11795 The properties `:decode' and `:headers' that are put to the function
11796 symbol `gnus-summary-save-in-pipe' control whether this function
11797 decodes articles and what headers to keep (see the doc string for the
11798 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11799 is neither omitted nor the symbol `r', force including all headers
11800 regardless of the `:headers' property.  If it is the symbol `r',
11801 articles that are not decoded and include all headers will be piped
11802 no matter what the properties `:decode' and `:headers' are."
11803   (interactive (gnus-interactive "P\ny"))
11804   (require 'gnus-art)
11805   (let* ((articles (gnus-summary-work-articles n))
11806          (result-buffer "*Shell Command Output*")
11807          (all-headers (not (memq sym '(nil r))))
11808          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11809          (raw (eq sym 'r))
11810          (headers (get 'gnus-summary-save-in-pipe :headers))
11811          command result)
11812     (unless (numberp (car articles))
11813       (error "No article to pipe"))
11814     (setq command (gnus-read-shell-command
11815                    (concat "Shell command on "
11816                            (if (cdr articles)
11817                                (format "these %d articles" (length articles))
11818                              "this article")
11819                            ": ")
11820                    gnus-summary-pipe-output-default-command))
11821     (when (string-equal command "")
11822       (error "A command is required"))
11823     (when all-headers
11824       (put 'gnus-summary-save-in-pipe :headers nil))
11825     (unwind-protect
11826         (while articles
11827           (gnus-summary-goto-subject (pop articles))
11828           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11829           (when (and (get-buffer result-buffer)
11830                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11831             (setq result (concat result (with-current-buffer result-buffer
11832                                           (buffer-string))))))
11833       (put 'gnus-summary-save-in-pipe :headers headers))
11834     (unless (zerop (length result))
11835       (if (with-current-buffer (get-buffer-create result-buffer)
11836             (erase-buffer)
11837             (insert result)
11838             (prog1
11839                 (and (= (count-lines (point-min) (point)) 1)
11840                      (progn
11841                        (end-of-line 0)
11842                        (<= (current-column)
11843                            (window-width (minibuffer-window)))))
11844               (goto-char (point-min))))
11845           (message "%s" (substring result 0 -1))
11846         (message nil)
11847         (gnus-configure-windows 'pipe)))))
11848
11849 (defun gnus-summary-save-article-mail (&optional arg)
11850   "Append the current article to a Unix mail box file.
11851 If N is a positive number, save the N next articles.
11852 If N is a negative number, save the N previous articles.
11853 If N is nil and any articles have been marked with the process mark,
11854 save those articles instead."
11855   (interactive "P")
11856   (require 'gnus-art)
11857   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11858     (gnus-summary-save-article arg)))
11859
11860 (defun gnus-summary-save-article-rmail (&optional arg)
11861   "Append the current article to an rmail file.
11862 If N is a positive number, save the N next articles.
11863 If N is a negative number, save the N previous articles.
11864 If N is nil and any articles have been marked with the process mark,
11865 save those articles instead."
11866   (interactive "P")
11867   (require 'gnus-art)
11868   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11869     (gnus-summary-save-article arg)))
11870
11871 (defun gnus-summary-save-article-file (&optional arg)
11872   "Append the current article to a file.
11873 If N is a positive number, save the N next articles.
11874 If N is a negative number, save the N previous articles.
11875 If N is nil and any articles have been marked with the process mark,
11876 save those articles instead."
11877   (interactive "P")
11878   (require 'gnus-art)
11879   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11880     (gnus-summary-save-article arg)))
11881
11882 (defun gnus-summary-write-article-file (&optional arg)
11883   "Write the current article to a file, deleting the previous file.
11884 If N is a positive number, save the N next articles.
11885 If N is a negative number, save the N previous articles.
11886 If N is nil and any articles have been marked with the process mark,
11887 save those articles instead."
11888   (interactive "P")
11889   (require 'gnus-art)
11890   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11891     (gnus-summary-save-article arg)))
11892
11893 (defun gnus-summary-save-article-body-file (&optional arg)
11894   "Append the current article body to a file.
11895 If N is a positive number, save the N next articles.
11896 If N is a negative number, save the N previous articles.
11897 If N is nil and any articles have been marked with the process mark,
11898 save those articles instead."
11899   (interactive "P")
11900   (require 'gnus-art)
11901   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11902     (gnus-summary-save-article arg)))
11903
11904 (defun gnus-summary-write-article-body-file (&optional arg)
11905   "Write the current article body to a file, deleting the previous file.
11906 If N is a positive number, save the N next articles.
11907 If N is a negative number, save the N previous articles.
11908 If N is nil and any articles have been marked with the process mark,
11909 save those articles instead."
11910   (interactive "P")
11911   (require 'gnus-art)
11912   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11913     (gnus-summary-save-article arg)))
11914
11915 (defun gnus-summary-muttprint (&optional arg)
11916   "Print the current article using Muttprint.
11917 If N is a positive number, save the N next articles.
11918 If N is a negative number, save the N previous articles.
11919 If N is nil and any articles have been marked with the process mark,
11920 save those articles instead."
11921   (interactive "P")
11922   (require 'gnus-art)
11923   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11924     (gnus-summary-save-article arg t)))
11925
11926 (defun gnus-summary-pipe-message (program)
11927   "Pipe the current article through PROGRAM."
11928   (interactive "sProgram: ")
11929   (gnus-summary-select-article)
11930   (let ((mail-header-separator ""))
11931     (gnus-eval-in-buffer-window gnus-article-buffer
11932       (save-restriction
11933         (widen)
11934         (let ((start (window-start))
11935               (inhibit-read-only t))
11936           (message-pipe-buffer-body program)
11937           (set-window-start (get-buffer-window (current-buffer)) start))))))
11938
11939 (defun gnus-get-split-value (methods)
11940   "Return a value based on the split METHODS."
11941   (let (split-name method result match)
11942     (when methods
11943       (with-current-buffer gnus-original-article-buffer
11944         (save-restriction
11945           (nnheader-narrow-to-headers)
11946           (while (and methods (not split-name))
11947             (goto-char (point-min))
11948             (setq method (pop methods))
11949             (setq match (car method))
11950             (when (cond
11951                    ((stringp match)
11952                     ;; Regular expression.
11953                     (ignore-errors
11954                       (re-search-forward match nil t)))
11955                    ((functionp match)
11956                     ;; Function.
11957                     (save-restriction
11958                       (widen)
11959                       (setq result (funcall match gnus-newsgroup-name))))
11960                    ((consp match)
11961                     ;; Form.
11962                     (save-restriction
11963                       (widen)
11964                       (setq result (eval match)))))
11965               (setq split-name (cdr method))
11966               (cond ((stringp result)
11967                      (push (expand-file-name
11968                             result gnus-article-save-directory)
11969                            split-name))
11970                     ((consp result)
11971                      (setq split-name (append result split-name)))))))))
11972     (nreverse split-name)))
11973
11974 (defun gnus-valid-move-group-p (group)
11975   (and (symbolp group)
11976        (boundp group)
11977        (symbol-name group)
11978        (symbol-value group)
11979        (gnus-get-function (gnus-find-method-for-group
11980                            (symbol-name group)) 'request-accept-article t)))
11981
11982 (defun gnus-read-move-group-name (prompt default articles prefix)
11983   "Read a group name."
11984   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11985          (minibuffer-confirm-incomplete nil) ; XEmacs
11986          (prom
11987           (format "%s %s to"
11988                   prompt
11989                   (if (> (length articles) 1)
11990                       (format "these %d articles" (length articles))
11991                     "this article")))
11992          (to-newsgroup
11993           (cond
11994            ((null split-name)
11995             (gnus-group-completing-read
11996              prom
11997              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
11998              nil prefix nil default))
11999            ((= 1 (length split-name))
12000             (gnus-group-completing-read
12001              prom
12002              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12003              nil prefix 'gnus-group-history (car split-name)))
12004            (t
12005             (gnus-completing-read
12006              prom (nreverse split-name) nil nil 'gnus-group-history))))
12007          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12008          encoded)
12009     (when to-newsgroup
12010       (if (or (string= to-newsgroup "")
12011               (string= to-newsgroup prefix))
12012           (setq to-newsgroup default))
12013       (unless to-newsgroup
12014         (error "No group name entered"))
12015       (setq encoded (mm-encode-coding-string
12016                      to-newsgroup
12017                      (gnus-group-name-charset to-method to-newsgroup)))
12018       (or (gnus-active encoded)
12019           (gnus-activate-group encoded nil nil to-method)
12020           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12021                                      to-newsgroup))
12022               (or (and (gnus-request-create-group encoded to-method)
12023                        (gnus-activate-group encoded nil nil to-method)
12024                        (gnus-subscribe-group encoded))
12025                   (error "Couldn't create group %s" to-newsgroup)))
12026           (error "No such group: %s" to-newsgroup))
12027       encoded)))
12028
12029 (defvar gnus-summary-save-parts-counter)
12030 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12031
12032 (defun gnus-summary-save-parts (type dir n &optional reverse)
12033   "Save parts matching TYPE to DIR.
12034 If REVERSE, save parts that do not match TYPE."
12035   (interactive
12036    (list (read-string "Save parts of type: "
12037                       (or (car gnus-summary-save-parts-type-history)
12038                           gnus-summary-save-parts-default-mime)
12039                       'gnus-summary-save-parts-type-history)
12040          (setq gnus-summary-save-parts-last-directory
12041                (read-file-name "Save to directory: "
12042                                gnus-summary-save-parts-last-directory
12043                                nil t))
12044          current-prefix-arg))
12045   (gnus-summary-iterate n
12046     (let ((gnus-display-mime-function nil)
12047           gnus-article-prepare-hook
12048           gnus-article-decode-hook
12049           gnus-display-mime-function
12050           gnus-break-pages
12051           (gnus-inhibit-treatment t))
12052       (gnus-summary-select-article))
12053     (with-current-buffer gnus-article-buffer
12054       (let ((handles (or gnus-article-mime-handles
12055                          (mm-dissect-buffer nil gnus-article-loose-mime)
12056                          (and gnus-article-emulate-mime
12057                               (mm-uu-dissect))))
12058             (gnus-summary-save-parts-counter 1))
12059         (when handles
12060           (gnus-summary-save-parts-1 type dir handles reverse)
12061           (unless gnus-article-mime-handles ;; Don't destroy this case.
12062             (mm-destroy-parts handles)))))))
12063
12064 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12065   (if (stringp (car handle))
12066       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12067               (cdr handle))
12068     (when (if reverse
12069               (not (string-match type (mm-handle-media-type handle)))
12070             (string-match type (mm-handle-media-type handle)))
12071       (let ((file (expand-file-name
12072                    (gnus-map-function
12073                     mm-file-name-rewrite-functions
12074                     (file-name-nondirectory
12075                      (or
12076                       (mail-content-type-get
12077                        (mm-handle-disposition handle) 'filename)
12078                       (mail-content-type-get
12079                        (mm-handle-type handle) 'name)
12080                       (format "%s.%d.%d" gnus-newsgroup-name
12081                               (cdr gnus-article-current)
12082                               gnus-summary-save-parts-counter))))
12083                    dir)))
12084         (incf gnus-summary-save-parts-counter)
12085         (unless (file-exists-p file)
12086           (mm-save-part-to-file handle file))))))
12087
12088 ;; Summary extract commands
12089
12090 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12091   (let ((inhibit-read-only t)
12092         (article (gnus-summary-article-number))
12093         after-article b e)
12094     (unless (gnus-summary-goto-subject article)
12095       (error "No such article: %d" article))
12096     (gnus-summary-position-point)
12097     ;; If all commands are to be bunched up on one line, we collect
12098     ;; them here.
12099     (unless gnus-view-pseudos-separately
12100       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12101             files action)
12102         (while ps
12103           (setq action (cdr (assq 'action (car ps))))
12104           (setq files (list (cdr (assq 'name (car ps)))))
12105           (while (and ps (cdr ps)
12106                       (string= (or action "1")
12107                                (or (cdr (assq 'action (cadr ps))) "2")))
12108             (push (cdr (assq 'name (cadr ps))) files)
12109             (setcdr ps (cddr ps)))
12110           (when files
12111             (when (not (string-match "%s" action))
12112               (push " " files))
12113             (push " " files)
12114             (when (assq 'execute (car ps))
12115               (setcdr (assq 'execute (car ps))
12116                       (funcall (if (string-match "%s" action)
12117                                    'format 'concat)
12118                                action
12119                                (mapconcat
12120                                 (lambda (f)
12121                                   (if (equal f " ")
12122                                       f
12123                                     (shell-quote-argument f)))
12124                                 files " ")))))
12125           (setq ps (cdr ps)))))
12126     (if (and gnus-view-pseudos (not not-view))
12127         (while pslist
12128           (when (assq 'execute (car pslist))
12129             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12130                                   (eq gnus-view-pseudos 'not-confirm)))
12131           (setq pslist (cdr pslist)))
12132       (save-excursion
12133         (while pslist
12134           (setq after-article (or (cdr (assq 'article (car pslist)))
12135                                   (gnus-summary-article-number)))
12136           (gnus-summary-goto-subject after-article)
12137           (forward-line 1)
12138           (setq b (point))
12139           (insert "    " (file-name-nondirectory
12140                           (cdr (assq 'name (car pslist))))
12141                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12142           (setq e (point))
12143           (forward-line -1)             ; back to `b'
12144           (gnus-add-text-properties
12145            b (1- e) (list 'gnus-number gnus-reffed-article-number
12146                           gnus-mouse-face-prop gnus-mouse-face))
12147           (gnus-data-enter
12148            after-article gnus-reffed-article-number
12149            gnus-unread-mark b (car pslist) 0 (- e b))
12150           (setq gnus-newsgroup-unreads
12151                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12152                                          gnus-reffed-article-number))
12153           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12154           (setq pslist (cdr pslist)))))))
12155
12156 (defun gnus-pseudos< (p1 p2)
12157   (let ((c1 (cdr (assq 'action p1)))
12158         (c2 (cdr (assq 'action p2))))
12159     (and c1 c2 (string< c1 c2))))
12160
12161 (defun gnus-request-pseudo-article (props)
12162   (cond ((assq 'execute props)
12163          (gnus-execute-command (cdr (assq 'execute props)))))
12164   (let ((gnus-current-article (gnus-summary-article-number)))
12165     (gnus-run-hooks 'gnus-mark-article-hook)))
12166
12167 (defun gnus-execute-command (command &optional automatic)
12168   (save-excursion
12169     (gnus-article-setup-buffer)
12170     (set-buffer gnus-article-buffer)
12171     (setq buffer-read-only nil)
12172     (let ((command (if automatic command
12173                      (read-string "Command: " (cons command 0)))))
12174       (erase-buffer)
12175       (insert "$ " command "\n\n")
12176       (if gnus-view-pseudo-asynchronously
12177           (start-process "gnus-execute" (current-buffer) shell-file-name
12178                          shell-command-switch command)
12179         (call-process shell-file-name nil t nil
12180                       shell-command-switch command)))))
12181
12182 ;; Summary kill commands.
12183
12184 (defun gnus-summary-edit-global-kill (article)
12185   "Edit the \"global\" kill file."
12186   (interactive (list (gnus-summary-article-number)))
12187   (gnus-group-edit-global-kill article))
12188
12189 (defun gnus-summary-edit-local-kill ()
12190   "Edit a local kill file applied to the current newsgroup."
12191   (interactive)
12192   (setq gnus-current-headers (gnus-summary-article-header))
12193   (gnus-group-edit-local-kill
12194    (gnus-summary-article-number) gnus-newsgroup-name))
12195
12196 ;;; Header reading.
12197
12198 (defun gnus-read-header (id &optional header)
12199   "Read the headers of article ID and enter them into the Gnus system."
12200   (let ((group gnus-newsgroup-name)
12201         (gnus-override-method
12202          (or
12203           gnus-override-method
12204           (and (gnus-news-group-p gnus-newsgroup-name)
12205                (car (gnus-refer-article-methods)))))
12206         where)
12207     ;; First we check to see whether the header in question is already
12208     ;; fetched.
12209     (if (stringp id)
12210         ;; This is a Message-ID.
12211         (setq header (or header (gnus-id-to-header id)))
12212       ;; This is an article number.
12213       (setq header (or header (gnus-summary-article-header id))))
12214     (if (and header
12215              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12216         ;; We have found the header.
12217         header
12218       ;; We have to really fetch the header to this article.
12219       (with-current-buffer nntp-server-buffer
12220         (when (setq where (gnus-request-head id group))
12221           (nnheader-fold-continuation-lines)
12222           (goto-char (point-max))
12223           (insert ".\n")
12224           (goto-char (point-min))
12225           (insert "211 ")
12226           (princ (cond
12227                   ((numberp id) id)
12228                   ((cdr where) (cdr where))
12229                   (header (mail-header-number header))
12230                   (t gnus-reffed-article-number))
12231                  (current-buffer))
12232           (insert " Article retrieved.\n"))
12233         (if (or (not where)
12234                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12235             ()                          ; Malformed head.
12236           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12237             (when (and (stringp id)
12238                        (or
12239                         (not (string= (gnus-group-real-name group)
12240                                       (car where)))
12241                         (not (gnus-server-equal gnus-override-method
12242                                                 (gnus-group-method group)))))
12243               ;; If we fetched by Message-ID and the article came from
12244               ;; a different group (or server), we fudge some bogus
12245               ;; article numbers for this article.
12246               (mail-header-set-number header gnus-reffed-article-number))
12247             (with-current-buffer gnus-summary-buffer
12248               (decf gnus-reffed-article-number)
12249               (gnus-remove-header (mail-header-number header))
12250               (push header gnus-newsgroup-headers)
12251               (setq gnus-current-headers header)
12252               (push (mail-header-number header) gnus-newsgroup-limit)))
12253           header)))))
12254
12255 (defun gnus-remove-header (number)
12256   "Remove header NUMBER from `gnus-newsgroup-headers'."
12257   (if (and gnus-newsgroup-headers
12258            (= number (mail-header-number (car gnus-newsgroup-headers))))
12259       (pop gnus-newsgroup-headers)
12260     (let ((headers gnus-newsgroup-headers))
12261       (while (and (cdr headers)
12262                   (not (= number (mail-header-number (cadr headers)))))
12263         (pop headers))
12264       (when (cdr headers)
12265         (setcdr headers (cddr headers))))))
12266
12267 ;;;
12268 ;;; summary highlights
12269 ;;;
12270
12271 (defun gnus-highlight-selected-summary ()
12272   "Highlight selected article in summary buffer."
12273   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12274   (when gnus-summary-selected-face
12275     (save-excursion
12276       (let* ((beg (point-at-bol))
12277              (end (point-at-eol))
12278              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12279              (from (if (get-text-property beg gnus-mouse-face-prop)
12280                        beg
12281                      (or (next-single-property-change
12282                           beg gnus-mouse-face-prop nil end)
12283                          beg)))
12284              (to
12285               (if (= from end)
12286                   (- from 2)
12287                 (or (next-single-property-change
12288                      from gnus-mouse-face-prop nil end)
12289                     end))))
12290         ;; If no mouse-face prop on line we will have to = from = end,
12291         ;; so we highlight the entire line instead.
12292         (when (= (+ to 2) from)
12293           (setq from beg)
12294           (setq to end))
12295         (if gnus-newsgroup-selected-overlay
12296             ;; Move old overlay.
12297             (gnus-move-overlay
12298              gnus-newsgroup-selected-overlay from to (current-buffer))
12299           ;; Create new overlay.
12300           (gnus-overlay-put
12301            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12302            'face gnus-summary-selected-face))))))
12303
12304 (defvar gnus-summary-highlight-line-cached nil)
12305 (defvar gnus-summary-highlight-line-trigger nil)
12306
12307 (defun gnus-summary-highlight-line-0 ()
12308   (if (and (eq gnus-summary-highlight-line-trigger
12309                gnus-summary-highlight)
12310            gnus-summary-highlight-line-cached)
12311       gnus-summary-highlight-line-cached
12312     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12313           gnus-summary-highlight-line-cached
12314           (let* ((cond (list 'cond))
12315                  (c cond)
12316                  (list gnus-summary-highlight))
12317             (while list
12318               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12319                               nil))
12320               (setq c (cdr c)
12321                     list (cdr list)))
12322             (gnus-byte-compile (list 'lambda nil cond))))))
12323
12324 (defun gnus-summary-highlight-line ()
12325   "Highlight current line according to `gnus-summary-highlight'."
12326   (let* ((beg (point-at-bol))
12327          (article (or (gnus-summary-article-number) gnus-current-article))
12328          (score (or (cdr (assq article
12329                                gnus-newsgroup-scored))
12330                     gnus-summary-default-score 0))
12331          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12332          (inhibit-read-only t)
12333          (default gnus-summary-default-score)
12334          (default-high gnus-summary-default-high-score)
12335          (default-low gnus-summary-default-low-score)
12336          (uncached (and gnus-summary-use-undownloaded-faces
12337                         (memq article gnus-newsgroup-undownloaded)
12338                         (not (memq article gnus-newsgroup-cached)))))
12339     (let ((face (funcall (gnus-summary-highlight-line-0))))
12340       (unless (eq face (get-text-property beg 'face))
12341         (gnus-put-text-property-excluding-characters-with-faces
12342          beg (point-at-eol) 'face
12343          (setq face (if (boundp face) (symbol-value face) face)))
12344         (when gnus-summary-highlight-line-function
12345           (funcall gnus-summary-highlight-line-function article face))))))
12346
12347 (defun gnus-update-read-articles (group unread &optional compute)
12348   "Update the list of read articles in GROUP.
12349 UNREAD is a sorted list."
12350   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12351         (info (gnus-get-info group))
12352         (prev 1)
12353         read)
12354     (if (or (not info) (not active))
12355         ;; There is no info on this group if it was, in fact,
12356         ;; killed.  Gnus stores no information on killed groups, so
12357         ;; there's nothing to be done.
12358         ;; One could store the information somewhere temporarily,
12359         ;; perhaps...  Hmmm...
12360         ()
12361       ;; Remove any negative articles numbers.
12362       (while (and unread (< (car unread) 0))
12363         (setq unread (cdr unread)))
12364       ;; Remove any expired article numbers
12365       (while (and unread (< (car unread) (car active)))
12366         (setq unread (cdr unread)))
12367       ;; Compute the ranges of read articles by looking at the list of
12368       ;; unread articles.
12369       (while unread
12370         (when (/= (car unread) prev)
12371           (push (if (= prev (1- (car unread))) prev
12372                   (cons prev (1- (car unread))))
12373                 read))
12374         (setq prev (1+ (car unread)))
12375         (setq unread (cdr unread)))
12376       (when (<= prev (cdr active))
12377         (push (cons prev (cdr active)) read))
12378       (setq read (if (> (length read) 1) (nreverse read) read))
12379       (if compute
12380           read
12381         (save-excursion
12382           (let (setmarkundo)
12383             ;; Propagate the read marks to the backend.
12384             (when (and gnus-propagate-marks
12385                        (gnus-check-backend-function 'request-set-mark group))
12386               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12387                     (add (gnus-remove-from-range read (gnus-info-read info))))
12388                 (when (or add del)
12389                   (unless (gnus-check-group group)
12390                     (error "Can't open server for %s" group))
12391                   (gnus-request-set-mark
12392                    group (delq nil (list (if add (list add 'add '(read)))
12393                                          (if del (list del 'del '(read))))))
12394                   (setq setmarkundo
12395                         `(gnus-request-set-mark
12396                           ,group
12397                           ',(delq nil (list
12398                                        (if del (list del 'add '(read)))
12399                                        (if add (list add 'del '(read))))))))))
12400             (set-buffer gnus-group-buffer)
12401             (gnus-undo-register
12402               `(progn
12403                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12404                  (gnus-info-set-read ',info ',(gnus-info-read info))
12405                  (gnus-get-unread-articles-in-group ',info
12406                                                     (gnus-active ,group))
12407                  (gnus-group-update-group ,group t)
12408                  ,setmarkundo))))
12409         ;; Enter this list into the group info.
12410         (gnus-info-set-read info read)
12411         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12412         (gnus-get-unread-articles-in-group info (gnus-active group))
12413         t))))
12414
12415 (defun gnus-offer-save-summaries ()
12416   "Offer to save all active summary buffers."
12417   (let (buffers)
12418     ;; Go through all buffers and find all summaries.
12419     (dolist (buffer (buffer-list))
12420       (when (and (setq buffer (buffer-name buffer))
12421                  (string-match "Summary" buffer)
12422                  (with-current-buffer buffer
12423                    ;; We check that this is, indeed, a summary buffer.
12424                    (and (eq major-mode 'gnus-summary-mode)
12425                         ;; Also make sure this isn't bogus.
12426                         gnus-newsgroup-prepared
12427                         ;; Also make sure that this isn't a
12428                         ;; dead summary buffer.
12429                         (not gnus-dead-summary-mode))))
12430         (push buffer buffers)))
12431     ;; Go through all these summary buffers and offer to save them.
12432     (when buffers
12433       (save-excursion
12434         (map-y-or-n-p
12435          "Update summary buffer %s? "
12436          (lambda (buf)
12437            (switch-to-buffer buf)
12438            (gnus-summary-exit))
12439          buffers)))))
12440
12441 (defun gnus-summary-setup-default-charset ()
12442   "Setup newsgroup default charset."
12443   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12444       (setq gnus-newsgroup-charset nil)
12445     (let* ((ignored-charsets
12446             (or gnus-newsgroup-ephemeral-ignored-charsets
12447                 (append
12448                  (and gnus-newsgroup-name
12449                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12450                  gnus-newsgroup-ignored-charsets))))
12451       (setq gnus-newsgroup-charset
12452             (or gnus-newsgroup-ephemeral-charset
12453                 (and gnus-newsgroup-name
12454                      (gnus-parameter-charset gnus-newsgroup-name))
12455                 gnus-default-charset))
12456       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12457            ignored-charsets))))
12458
12459 ;;;
12460 ;;; Mime Commands
12461 ;;;
12462
12463 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12464   "Display the current article buffer fully MIME-buttonized.
12465 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12466 treated as multipart/mixed."
12467   (interactive "P")
12468   (require 'gnus-art)
12469   (let ((gnus-unbuttonized-mime-types nil)
12470         (gnus-mime-display-multipart-as-mixed show-all-parts))
12471     (gnus-summary-show-article)))
12472
12473 (defun gnus-summary-repair-multipart (article)
12474   "Add a Content-Type header to a multipart article without one."
12475   (interactive (list (gnus-summary-article-number)))
12476   (gnus-with-article article
12477     (message-narrow-to-head)
12478     (message-remove-header "Mime-Version")
12479     (goto-char (point-max))
12480     (insert "Mime-Version: 1.0\n")
12481     (widen)
12482     (when (search-forward "\n--" nil t)
12483       (let ((separator (buffer-substring (point) (point-at-eol))))
12484         (message-narrow-to-head)
12485         (message-remove-header "Content-Type")
12486         (goto-char (point-max))
12487         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12488                         separator))
12489         (widen))))
12490   (let (gnus-mark-article-hook)
12491     (gnus-summary-select-article t t nil article)))
12492
12493 (defun gnus-summary-toggle-display-buttonized ()
12494   "Toggle the buttonizing of the article buffer."
12495   (interactive)
12496   (require 'gnus-art)
12497   (if (setq gnus-inhibit-mime-unbuttonizing
12498             (not gnus-inhibit-mime-unbuttonizing))
12499       (let ((gnus-unbuttonized-mime-types nil))
12500         (gnus-summary-show-article))
12501     (gnus-summary-show-article)))
12502
12503 ;;;
12504 ;;; Generic summary marking commands
12505 ;;;
12506
12507 (defvar gnus-summary-marking-alist
12508   '((read gnus-del-mark "d")
12509     (unread gnus-unread-mark "u")
12510     (ticked gnus-ticked-mark "!")
12511     (dormant gnus-dormant-mark "?")
12512     (expirable gnus-expirable-mark "e"))
12513   "An alist of names/marks/keystrokes.")
12514
12515 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12516 (defvar gnus-summary-mark-map)
12517
12518 (defun gnus-summary-make-all-marking-commands ()
12519   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12520   (dolist (elem gnus-summary-marking-alist)
12521     (apply 'gnus-summary-make-marking-command elem)))
12522
12523 (defun gnus-summary-make-marking-command (name mark keystroke)
12524   (let ((map (make-sparse-keymap)))
12525     (define-key gnus-summary-generic-mark-map keystroke map)
12526     (dolist (lway `((next "next" next nil "n")
12527                     (next-unread "next unread" next t "N")
12528                     (prev "previous" prev nil "p")
12529                     (prev-unread "previous unread" prev t "P")
12530                     (nomove "" nil nil ,keystroke)))
12531       (let ((func (gnus-summary-make-marking-command-1
12532                    mark (car lway) lway name)))
12533         (setq func (eval func))
12534         (define-key map (nth 4 lway) func)))))
12535
12536 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12537   `(defun ,(intern
12538             (format "gnus-summary-put-mark-as-%s%s"
12539                     name (if (eq way 'nomove)
12540                              ""
12541                            (concat "-" (symbol-name way)))))
12542      (n)
12543      ,(format
12544        "Mark the current article as %s%s.
12545 If N, the prefix, then repeat N times.
12546 If N is negative, move in reverse order.
12547 The difference between N and the actual number of articles marked is
12548 returned."
12549        name (cadr lway))
12550      (interactive "p")
12551      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12552
12553 (defun gnus-summary-generic-mark (n mark move unread)
12554   "Mark N articles with MARK."
12555   (unless (eq major-mode 'gnus-summary-mode)
12556     (error "This command can only be used in the summary buffer"))
12557   (gnus-summary-show-thread)
12558   (let ((nummove
12559          (cond
12560           ((eq move 'next) 1)
12561           ((eq move 'prev) -1)
12562           (t 0))))
12563     (if (zerop nummove)
12564         (setq n 1)
12565       (when (< n 0)
12566         (setq n (abs n)
12567               nummove (* -1 nummove))))
12568     (while (and (> n 0)
12569                 (gnus-summary-mark-article nil mark)
12570                 (zerop (gnus-summary-next-subject nummove unread t)))
12571       (setq n (1- n)))
12572     (when (/= 0 n)
12573       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12574     (gnus-summary-recenter)
12575     (gnus-summary-position-point)
12576     (gnus-set-mode-line 'summary)
12577     n))
12578
12579 (defun gnus-summary-insert-articles (articles)
12580   (when (setq articles
12581               (gnus-sorted-difference articles
12582                                       (mapcar (lambda (h)
12583                                                 (mail-header-number h))
12584                                               gnus-newsgroup-headers)))
12585     (setq gnus-newsgroup-headers
12586           (gnus-merge 'list
12587                       gnus-newsgroup-headers
12588                       (gnus-fetch-headers articles)
12589                       'gnus-article-sort-by-number))
12590     ;; Suppress duplicates?
12591     (when gnus-suppress-duplicates
12592       (gnus-dup-suppress-articles))
12593
12594     (if (and gnus-fetch-old-headers
12595              (eq gnus-headers-retrieved-by 'nov))
12596         ;; We might want to build some more threads first.
12597         (if (eq gnus-fetch-old-headers 'invisible)
12598             (gnus-build-all-threads)
12599           (gnus-build-old-threads))
12600       ;; Mark the inserted articles that are unread as unread.
12601       (setq gnus-newsgroup-unreads
12602             (gnus-sorted-nunion
12603              gnus-newsgroup-unreads
12604              (gnus-sorted-nintersection
12605               (gnus-list-of-unread-articles gnus-newsgroup-name)
12606               articles)))
12607       ;; Mark the inserted articles as selected so that the information
12608       ;; of the marks having been changed by a user may be updated when
12609       ;; exiting this group.  See `gnus-summary-update-info'.
12610       (dolist (art articles)
12611         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12612     ;; Let the Gnus agent mark articles as read.
12613     (when gnus-agent
12614       (gnus-agent-get-undownloaded-list))
12615     ;; Remove list identifiers from subject
12616     (when gnus-list-identifiers
12617       (gnus-summary-remove-list-identifiers))
12618     ;; First and last article in this newsgroup.
12619     (when gnus-newsgroup-headers
12620       (setq gnus-newsgroup-begin
12621             (mail-header-number (car gnus-newsgroup-headers))
12622             gnus-newsgroup-end
12623             (mail-header-number
12624              (gnus-last-element gnus-newsgroup-headers))))
12625     (when gnus-use-scoring
12626       (gnus-possibly-score-headers))))
12627
12628 (defun gnus-summary-insert-old-articles (&optional all)
12629   "Insert all old articles in this group.
12630 If ALL is non-nil, already read articles become readable.
12631 If ALL is a number, fetch this number of articles."
12632   (interactive "P")
12633   (prog1
12634       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12635             older len)
12636         (setq older
12637               ;; Some nntp servers lie about their active range.  When
12638               ;; this happens, the active range can be in the millions.
12639               ;; Use a compressed range to avoid creating a huge list.
12640               (gnus-range-difference (list gnus-newsgroup-active) old))
12641         (setq len (gnus-range-length older))
12642         (cond
12643          ((null older) nil)
12644          ((numberp all)
12645           (if (< all len)
12646               (let ((older-range (nreverse older)))
12647                 (setq older nil)
12648
12649                 (while (> all 0)
12650                   (let* ((r (pop older-range))
12651                          (min (if (numberp r) r (car r)))
12652                          (max (if (numberp r) r (cdr r))))
12653                     (while (and (<= min max)
12654                                 (> all 0))
12655                       (push max older)
12656                       (setq all (1- all)
12657                             max (1- max))))))
12658             (setq older (gnus-uncompress-range older))))
12659          (all
12660           (setq older (gnus-uncompress-range older)))
12661          (t
12662           (when (and (numberp gnus-large-newsgroup)
12663                    (> len gnus-large-newsgroup))
12664               (let* ((cursor-in-echo-area nil)
12665                      (initial (gnus-parameter-large-newsgroup-initial
12666                                gnus-newsgroup-name))
12667                      (input
12668                       (read-string
12669                        (format
12670                         "How many articles from %s (%s %d): "
12671                         (gnus-group-decoded-name gnus-newsgroup-name)
12672                         (if initial "max" "default")
12673                         len)
12674                        (if initial
12675                            (cons (number-to-string initial)
12676                                  0)))))
12677                 (unless (string-match "^[ \t]*$" input)
12678                   (setq all (string-to-number input))
12679                   (if (< all len)
12680                       (let ((older-range (nreverse older)))
12681                         (setq older nil)
12682
12683                         (while (> all 0)
12684                           (let* ((r (pop older-range))
12685                                  (min (if (numberp r) r (car r)))
12686                                  (max (if (numberp r) r (cdr r))))
12687                             (while (and (<= min max)
12688                                         (> all 0))
12689                               (push max older)
12690                               (setq all (1- all)
12691                                     max (1- max))))))))))
12692           (setq older (gnus-uncompress-range older))))
12693         (if (not older)
12694             (message "No old news.")
12695           (gnus-summary-insert-articles older)
12696           (gnus-summary-limit (gnus-sorted-nunion old older))))
12697     (gnus-summary-position-point)))
12698
12699 (defun gnus-summary-insert-new-articles ()
12700   "Insert all new articles in this group."
12701   (interactive)
12702   (prog1
12703       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12704             (old-high gnus-newsgroup-highest)
12705             (nnmail-fetched-sources (list t))
12706             i new)
12707         (setq gnus-newsgroup-active
12708               (gnus-copy-sequence
12709                (gnus-activate-group gnus-newsgroup-name 'scan)))
12710         (setq i (cdr gnus-newsgroup-active)
12711               gnus-newsgroup-highest i)
12712         (while (> i old-high)
12713           (push i new)
12714           (decf i))
12715         (if (not new)
12716             (message "No gnus is bad news")
12717           (gnus-summary-insert-articles new)
12718           (setq gnus-newsgroup-unreads
12719                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12720           (gnus-summary-limit (gnus-sorted-nunion old new))))
12721     (gnus-summary-position-point)))
12722
12723 ;;; Bookmark support for Gnus.
12724 (declare-function bookmark-make-record-default
12725                   "bookmark" (&optional no-file no-context posn))
12726 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12727 (declare-function bookmark-default-handler "bookmark" (bmk))
12728 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12729 (defvar bookmark-yank-point)
12730 (defvar bookmark-current-buffer)
12731
12732 (defun gnus-summary-bookmark-make-record ()
12733   "Make a bookmark entry for a Gnus summary buffer."
12734   (let (pos buf)
12735     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12736       (save-restriction              ; FIXME is it necessary to widen?
12737         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12738       (setq buf "art") ; We are recording bookmark from article buffer.
12739       (setq bookmark-yank-point (point))
12740       (setq bookmark-current-buffer (current-buffer))
12741       (gnus-article-show-summary))      ; Go back in summary buffer.
12742     ;; We are now recording bookmark from summary buffer.
12743     (unless buf (setq buf "sum"))
12744     (let* ((subject (elt (gnus-summary-article-header) 1))
12745            (grp     (car gnus-article-current))
12746            (art     (cdr gnus-article-current))
12747            (head    (gnus-summary-article-header art))
12748            (id      (mail-header-id head)))
12749       `(,subject
12750         ,@(condition-case nil
12751               (bookmark-make-record-default 'no-file 'no-context pos)
12752             (wrong-number-of-arguments
12753              (bookmark-make-record-default 'point-only)))
12754         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12755         (group . ,grp) (article . ,art)
12756         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12757
12758 ;;;###autoload
12759 (defun gnus-summary-bookmark-jump (bookmark)
12760   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12761 BOOKMARK is a bookmark name or a bookmark record."
12762   (let ((group    (bookmark-prop-get bookmark 'group))
12763         (article  (bookmark-prop-get bookmark 'article))
12764         (id       (bookmark-prop-get bookmark 'message-id))
12765         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12766     (gnus-fetch-group group (list article))
12767     (gnus-summary-insert-cached-articles)
12768     (gnus-summary-goto-article id nil 'force)
12769     ;; FIXME we have to wait article buffer is ready (only large buffer)
12770     ;; Is there a better solution to know that?
12771     ;; If we don't wait `bookmark-default-handler' will have no chance
12772     ;; to set position. However there is no error, just wrong pos.
12773     (sit-for 1)
12774     (when (string= buf "Gnus-art")
12775       (other-window 1))
12776     (bookmark-default-handler
12777      `(""
12778        (buffer . ,(current-buffer))
12779        . ,(bookmark-get-bookmark-record bookmark)))))
12780
12781 (gnus-summary-make-all-marking-commands)
12782
12783 (gnus-ems-redefine)
12784
12785 (provide 'gnus-sum)
12786
12787 (run-hooks 'gnus-sum-load-hook)
12788
12789 ;; Local Variables:
12790 ;; coding: iso-8859-1
12791 ;; End:
12792
12793 ;;; gnus-sum.el ends here