(gnus-request-marks): Call *-request-marks instead of the older request-update-info.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996-2011 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (eval-when-compile
33   (when (featurep 'xemacs)
34     (require 'easy-mmode))) ; for `define-minor-mode'
35
36 (defvar tool-bar-mode)
37 (defvar gnus-tmp-header)
38
39 (require 'gnus)
40 (require 'gnus-group)
41 (require 'gnus-spec)
42 (require 'gnus-range)
43 (require 'gnus-int)
44 (require 'gnus-undo)
45 (require 'gnus-util)
46 (require 'gmm-utils)
47 (require 'mm-decode)
48 (require 'nnoo)
49
50 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
51 (autoload 'gnus-cache-write-active "gnus-cache")
52 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
53 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
54 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
55 (autoload 'mm-uu-dissect "mm-uu")
56 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
57   "Deuglify broken Outlook (Express) articles and redisplay."
58   t)
59 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
60 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
61 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
62 (autoload 'nnir-article-rsv "nnir" nil nil 'macro)
63 (autoload 'nnir-article-group "nnir" nil nil 'macro)
64
65 (defcustom gnus-kill-summary-on-exit t
66   "*If non-nil, kill the summary buffer when you exit from it.
67 If nil, the summary will become a \"*Dead Summary*\" buffer, and
68 it will be killed sometime later."
69   :group 'gnus-summary-exit
70   :type 'boolean)
71
72 (defcustom gnus-summary-next-group-on-exit t
73   "If non-nil, go to the next unread newsgroup on summary exit.
74 See `gnus-group-goto-unread'."
75   :link '(custom-manual "(gnus)Group Maneuvering")
76   :group 'gnus-summary-exit
77   :version "23.1" ;; No Gnus
78   :type 'boolean)
79
80 (defcustom gnus-summary-stop-at-end-of-message nil
81   "If non-nil, don't select the next message when using `SPC'."
82   :link '(custom-manual "(gnus)Group Maneuvering")
83   :group 'gnus-summary-maneuvering
84   :version "24.1"
85   :type 'boolean)
86
87 (defcustom gnus-fetch-old-headers nil
88   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
89 If an unread article in the group refers to an older, already
90 read (or just marked as read) article, the old article will not
91 normally be displayed in the Summary buffer.  If this variable is
92 t, Gnus will attempt to grab the headers to the old articles, and
93 thereby build complete threads.  If it has the value `some', all
94 old headers will be fetched but only enough headers to connect
95 otherwise loose threads will be displayed.  This variable can
96 also be a number.  In that case, no more than that number of old
97 headers will be fetched.  If it has the value `invisible', all
98 old headers will be fetched, but none will be displayed.
99
100 The server has to support NOV for any of this to work.
101
102 This feature can seriously impact performance it ignores all
103 locally cached header entries.  Setting it to t for groups for a
104 server that doesn't expire articles (such as news.gmane.org),
105 leads to very slow summary generation."
106   :group 'gnus-thread
107   :type '(choice (const :tag "off" nil)
108                  (const :tag "on" t)
109                  (const some)
110                  (const invisible)
111                  number
112                  (sexp :menu-tag "other" t)))
113
114 (defcustom gnus-refer-thread-limit 500
115   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
116 If t, fetch all the available old headers."
117   :group 'gnus-thread
118   :type '(choice number
119                  (sexp :menu-tag "other" t)))
120
121 (defcustom gnus-summary-make-false-root 'adopt
122   "*nil means that Gnus won't gather loose threads.
123 If the root of a thread has expired or been read in a previous
124 session, the information necessary to build a complete thread has been
125 lost.  Instead of having many small sub-threads from this original thread
126 scattered all over the summary buffer, Gnus can gather them.
127
128 If non-nil, Gnus will try to gather all loose sub-threads from an
129 original thread into one large thread.
130
131 If this variable is non-nil, it should be one of `none', `adopt',
132 `dummy' or `empty'.
133
134 If this variable is `none', Gnus will not make a false root, but just
135 present the sub-threads after another.
136 If this variable is `dummy', Gnus will create a dummy root that will
137 have all the sub-threads as children.
138 If this variable is `adopt', Gnus will make one of the \"children\"
139 the parent and mark all the step-children as such.
140 If this variable is `empty', the \"children\" are printed with empty
141 subject fields.  (Or rather, they will be printed with a string
142 given by the `gnus-summary-same-subject' variable.)"
143   :group 'gnus-thread
144   :type '(choice (const :tag "off" nil)
145                  (const none)
146                  (const dummy)
147                  (const adopt)
148                  (const empty)))
149
150 (defcustom gnus-summary-make-false-root-always nil
151   "Always make a false dummy root."
152   :version "22.1"
153   :group 'gnus-thread
154   :type 'boolean)
155
156 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
157   "*A regexp to match subjects to be excluded from loose thread gathering.
158 As loose thread gathering is done on subjects only, that means that
159 there can be many false gatherings performed.  By rooting out certain
160 common subjects, gathering might become saner."
161   :group 'gnus-thread
162   :type 'regexp)
163
164 (defcustom gnus-summary-gather-subject-limit nil
165   "*Maximum length of subject comparisons when gathering loose threads.
166 Use nil to compare full subjects.  Setting this variable to a low
167 number will help gather threads that have been corrupted by
168 newsreaders chopping off subject lines, but it might also mean that
169 unrelated articles that have subject that happen to begin with the
170 same few characters will be incorrectly gathered.
171
172 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
173 comparing subjects."
174   :group 'gnus-thread
175   :type '(choice (const :tag "off" nil)
176                  (const fuzzy)
177                  (sexp :menu-tag "on" t)))
178
179 (defcustom gnus-simplify-subject-functions nil
180   "List of functions taking a string argument that simplify subjects.
181 The functions are applied recursively.
182
183 Useful functions to put in this list include:
184 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
185 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
186   :group 'gnus-thread
187   :type '(repeat function))
188
189 (defcustom gnus-simplify-ignored-prefixes nil
190   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
191   :group 'gnus-thread
192   :type '(choice (const :tag "off" nil)
193                  regexp))
194
195 (defcustom gnus-build-sparse-threads nil
196   "*If non-nil, fill in the gaps in threads.
197 If `some', only fill in the gaps that are needed to tie loose threads
198 together.  If `more', fill in all leaf nodes that Gnus can find.  If
199 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
200   :group 'gnus-thread
201   :type '(choice (const :tag "off" nil)
202                  (const some)
203                  (const more)
204                  (sexp :menu-tag "all" t)))
205
206 (defcustom gnus-summary-thread-gathering-function
207   'gnus-gather-threads-by-subject
208   "*Function used for gathering loose threads.
209 There are two pre-defined functions: `gnus-gather-threads-by-subject',
210 which only takes Subjects into consideration; and
211 `gnus-gather-threads-by-references', which compared the References
212 headers of the articles to find matches."
213   :group 'gnus-thread
214   :type '(radio (function-item gnus-gather-threads-by-subject)
215                 (function-item gnus-gather-threads-by-references)
216                 (function :tag "other")))
217
218 (defcustom gnus-summary-same-subject ""
219   "*String indicating that the current article has the same subject as the previous.
220 This variable will only be used if the value of
221 `gnus-summary-make-false-root' is `empty'."
222   :group 'gnus-summary-format
223   :type 'string)
224
225 (defcustom gnus-summary-goto-unread nil
226   "*If t, many commands will go to the next unread article.
227 This applies to marking commands as well as other commands that
228 \"naturally\" select the next article, like, for instance, `SPC' at
229 the end of an article.
230
231 If nil, the marking commands do NOT go to the next unread article
232 \(they go to the next article instead).  If `never', commands that
233 usually go to the next unread article, will go to the next article,
234 whether it is read or not."
235   :version "24.1"
236   :group 'gnus-summary-marks
237   :link '(custom-manual "(gnus)Setting Marks")
238   :type '(choice (const :tag "off" nil)
239                  (const never)
240                  (sexp :menu-tag "on" t)))
241
242 (defcustom gnus-summary-default-score 0
243   "*Default article score level.
244 All scores generated by the score files will be added to this score.
245 If this variable is nil, scoring will be disabled."
246   :group 'gnus-score-default
247   :type '(choice (const :tag "disable")
248                  integer))
249
250 (defcustom gnus-summary-default-high-score 0
251   "*Default threshold for a high scored article.
252 An article will be highlighted as high scored if its score is greater
253 than this score."
254   :version "22.1"
255   :group 'gnus-score-default
256   :type 'integer)
257
258 (defcustom gnus-summary-default-low-score 0
259   "*Default threshold for a low scored article.
260 An article will be highlighted as low scored if its score is smaller
261 than this score."
262   :version "22.1"
263   :group 'gnus-score-default
264   :type 'integer)
265
266 (defcustom gnus-summary-zcore-fuzz 0
267   "*Fuzziness factor for the zcore in the summary buffer.
268 Articles with scores closer than this to `gnus-summary-default-score'
269 will not be marked."
270   :group 'gnus-summary-format
271   :type 'integer)
272
273 (defcustom gnus-simplify-subject-fuzzy-regexp nil
274   "*Strings to be removed when doing fuzzy matches.
275 This can either be a regular expression or list of regular expressions
276 that will be removed from subject strings if fuzzy subject
277 simplification is selected."
278   :group 'gnus-thread
279   :type '(repeat regexp))
280
281 (defcustom gnus-show-threads t
282   "*If non-nil, display threads in summary mode."
283   :group 'gnus-thread
284   :type 'boolean)
285
286 (defcustom gnus-thread-hide-subtree nil
287   "*If non-nil, hide all threads initially.
288 This can be a predicate specifier which says which threads to hide.
289 If threads are hidden, you have to run the command
290 `gnus-summary-show-thread' by hand or select an article."
291   :group 'gnus-thread
292   :type '(radio (sexp :format "Non-nil\n"
293                       :match (lambda (widget value)
294                                (not (or (consp value) (functionp value))))
295                       :value t)
296                 (const nil)
297                 (sexp :tag "Predicate specifier")))
298
299 (defcustom gnus-thread-hide-killed t
300   "*If non-nil, hide killed threads automatically."
301   :group 'gnus-thread
302   :type 'boolean)
303
304 (defcustom gnus-thread-ignore-subject t
305   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
306 If nil, articles that have different subjects from their parents will
307 start separate threads."
308   :group 'gnus-thread
309   :type 'boolean)
310
311 (defcustom gnus-thread-operation-ignore-subject t
312   "*If non-nil, subjects will be ignored when doing thread commands.
313 This affects commands like `gnus-summary-kill-thread' and
314 `gnus-summary-lower-thread'.
315
316 If this variable is nil, articles in the same thread with different
317 subjects will not be included in the operation in question.  If this
318 variable is `fuzzy', only articles that have subjects that are fuzzily
319 equal will be included."
320   :group 'gnus-thread
321   :type '(choice (const :tag "off" nil)
322                  (const fuzzy)
323                  (sexp :tag "on" t)))
324
325 (defcustom gnus-thread-indent-level 4
326   "*Number that says how much each sub-thread should be indented."
327   :group 'gnus-thread
328   :type 'integer)
329
330 (defcustom gnus-auto-extend-newsgroup t
331   "*If non-nil, extend newsgroup forward and backward when requested."
332   :group 'gnus-summary-choose
333   :type 'boolean)
334
335 (defcustom gnus-auto-select-first t
336   "If non-nil, select an article on group entry.
337 An article is selected automatically when entering a group
338 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
339 `gnus-summary-catchup-and-goto-next-group'.
340
341 Which article is selected is controlled by the variable
342 `gnus-auto-select-subject'.
343
344 If you want to prevent automatic selection of articles in some
345 newsgroups, set the variable to nil in `gnus-select-group-hook'."
346   ;; Commands include...
347   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
348   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
349   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
350   :group 'gnus-group-select
351   :type '(choice (const :tag "none" nil)
352                  (sexp :menu-tag "first" t)))
353
354 (defcustom gnus-auto-select-subject 'unseen-or-unread
355   "*Says what subject to place under point when entering a group.
356
357 This variable can either be the symbols `first' (place point on the
358 first subject), `unread' (place point on the subject line of the first
359 unread article), `best' (place point on the subject line of the
360 higest-scored article), `unseen' (place point on the subject line of
361 the first unseen article), `unseen-or-unread' (place point on the subject
362 line of the first unseen article or, if all articles have been seen, on the
363 subject line of the first unread article), or a function to be called to
364 place point on some subject line."
365   :version "24.1"
366   :group 'gnus-group-select
367   :type '(choice (const best)
368                  (const unread)
369                  (const first)
370                  (const unseen)
371                  (const unseen-or-unread)))
372
373 (defcustom gnus-auto-select-next t
374   "*If non-nil, offer to go to the next group from the end of the previous.
375 If the value is t and the next newsgroup is empty, Gnus will exit
376 summary mode and go back to group mode.  If the value is neither nil
377 nor t, Gnus will select the following unread newsgroup.  In
378 particular, if the value is the symbol `quietly', the next unread
379 newsgroup will be selected without any confirmation, and if it is
380 `almost-quietly', the next group will be selected without any
381 confirmation if you are located on the last article in the group.
382 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
383 will go to the next group without confirmation."
384   :group 'gnus-summary-maneuvering
385   :type '(choice (const :tag "off" nil)
386                  (const quietly)
387                  (const almost-quietly)
388                  (const slightly-quietly)
389                  (sexp :menu-tag "on" t)))
390
391 (defcustom gnus-auto-select-same nil
392   "*If non-nil, select the next article with the same subject.
393 If there are no more articles with the same subject, go to
394 the first unread article."
395   :group 'gnus-summary-maneuvering
396   :type 'boolean)
397
398 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
399   "What article should be selected after exiting an ephemeral group.
400 Valid values include:
401
402 `next'
403   Select the next article.
404 `next-unread'
405   Select the next unread article.
406 `next-noselect'
407   Move the cursor to the next article.  This is the default.
408 `next-unread-noselect'
409   Move the cursor to the next unread article.
410
411 If it has any other value or there is no next (unread) article, the
412 article selected before entering to the ephemeral group will appear."
413   :version "23.1" ;; No Gnus
414   :group 'gnus-summary-maneuvering
415   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
416                  (const next) (const next-unread)
417                  (const next-noselect) (const next-unread-noselect)
418                  (sexp :tag "other" :value nil)))
419
420 (defcustom gnus-auto-goto-ignores 'unfetched
421   "*Says how to handle unfetched articles when maneuvering.
422
423 This variable can either be the symbols nil (maneuver to any
424 article), `undownloaded' (maneuvering while unplugged ignores articles
425 that have not been fetched), `always-undownloaded' (maneuvering always
426 ignores articles that have not been fetched), `unfetched' (maneuvering
427 ignores articles whose headers have not been fetched).
428
429 NOTE: The list of unfetched articles will always be nil when plugged
430 and, when unplugged, a subset of the undownloaded article list."
431   :version "22.1"
432   :group 'gnus-summary-maneuvering
433   :type '(choice (const :tag "None" nil)
434                  (const :tag "Undownloaded when unplugged" undownloaded)
435                  (const :tag "Undownloaded" always-undownloaded)
436                  (const :tag "Unfetched" unfetched)))
437
438 (defcustom gnus-summary-check-current nil
439   "*If non-nil, consider the current article when moving.
440 The \"unread\" movement commands will stay on the same line if the
441 current article is unread."
442   :group 'gnus-summary-maneuvering
443   :type 'boolean)
444
445 (defcustom gnus-auto-center-summary 2
446   "*If non-nil, always center the current summary buffer.
447 In particular, if `vertical' do only vertical recentering.  If non-nil
448 and non-`vertical', do both horizontal and vertical recentering."
449   :group 'gnus-summary-maneuvering
450   :type '(choice (const :tag "none" nil)
451                  (const vertical)
452                  (integer :tag "height")
453                  (sexp :menu-tag "both" t)))
454
455 (defcustom gnus-auto-center-group t
456   "If non-nil, always center the group buffer."
457   :group 'gnus-summary-maneuvering
458   :type 'boolean)
459
460 (defcustom gnus-show-all-headers nil
461   "*If non-nil, don't hide any headers."
462   :group 'gnus-article-hiding
463   :group 'gnus-article-headers
464   :type 'boolean)
465
466 (defcustom gnus-summary-ignore-duplicates nil
467   "*If non-nil, ignore articles with identical Message-ID headers."
468   :group 'gnus-summary
469   :type 'boolean)
470
471 (defcustom gnus-single-article-buffer nil
472   "*If non-nil, display all articles in the same buffer.
473 If nil, each group will get its own article buffer."
474   :version "24.1"
475   :group 'gnus-article-various
476   :type 'boolean)
477
478 (defcustom gnus-widen-article-window nil
479   "If non-nil, selecting the article buffer will display only the article buffer."
480   :version "24.1"
481   :group 'gnus-article-various
482   :type 'boolean)
483
484 (defcustom gnus-break-pages t
485   "*If non-nil, do page breaking on articles.
486 The page delimiter is specified by the `gnus-page-delimiter'
487 variable."
488   :group 'gnus-article-various
489   :type 'boolean)
490
491 (defcustom gnus-move-split-methods nil
492   "*Variable used to suggest where articles are to be moved to.
493 It uses the same syntax as the `gnus-split-methods' variable.
494 However, whereas `gnus-split-methods' specifies file names as targets,
495 this variable specifies group names."
496   :group 'gnus-summary-mail
497   :type '(repeat (choice (list :value (fun) function)
498                          (cons :value ("" "") regexp (repeat string))
499                          (sexp :value nil))))
500
501 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
502   "Function used to compute default prefix for article move/copy/etc prompts.
503 The function should take one argument, a group name, and return a
504 string with the suggested prefix."
505   :group 'gnus-summary-mail
506   :type 'function)
507
508 ;; FIXME: Although the custom type is `character' for the following variables,
509 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
510
511 (defcustom gnus-unread-mark ?           ;Whitespace
512   "*Mark used for unread articles."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-ticked-mark ?!
517   "*Mark used for ticked articles."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-dormant-mark ??
522   "*Mark used for dormant articles."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-del-mark ?r
527   "*Mark used for del'd articles."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-read-mark ?R
532   "*Mark used for read articles."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-expirable-mark ?E
537   "*Mark used for expirable articles."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-killed-mark ?K
542   "*Mark used for killed articles."
543   :group 'gnus-summary-marks
544   :type 'character)
545
546 (defcustom gnus-spam-mark ?$
547   "*Mark used for spam articles."
548   :version "22.1"
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-kill-file-mark ?X
553   "*Mark used for articles killed by kill files."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-low-score-mark ?Y
558   "*Mark used for articles with a low score."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-catchup-mark ?C
563   "*Mark used for articles that are caught up."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-replied-mark ?A
568   "*Mark used for articles that have been replied to."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-forwarded-mark ?F
573   "*Mark used for articles that have been forwarded."
574   :version "22.1"
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-recent-mark ?N
579   "*Mark used for articles that are recent."
580   :version "22.1"
581   :group 'gnus-summary-marks
582   :type 'character)
583
584 (defcustom gnus-cached-mark ?*
585   "*Mark used for articles that are in the cache."
586   :group 'gnus-summary-marks
587   :type 'character)
588
589 (defcustom gnus-saved-mark ?S
590   "*Mark used for articles that have been saved."
591   :group 'gnus-summary-marks
592   :type 'character)
593
594 (defcustom gnus-unseen-mark ?.
595   "*Mark used for articles that haven't been seen."
596   :version "22.1"
597   :group 'gnus-summary-marks
598   :type 'character)
599
600 (defcustom gnus-no-mark ?               ;Whitespace
601   "*Mark used for articles that have no other secondary mark."
602   :version "22.1"
603   :group 'gnus-summary-marks
604   :type 'character)
605
606 (defcustom gnus-ancient-mark ?O
607   "*Mark used for ancient articles."
608   :group 'gnus-summary-marks
609   :type 'character)
610
611 (defcustom gnus-sparse-mark ?Q
612   "*Mark used for sparsely reffed articles."
613   :group 'gnus-summary-marks
614   :type 'character)
615
616 (defcustom gnus-canceled-mark ?G
617   "*Mark used for canceled articles."
618   :group 'gnus-summary-marks
619   :type 'character)
620
621 (defcustom gnus-duplicate-mark ?M
622   "*Mark used for duplicate articles."
623   :group 'gnus-summary-marks
624   :type 'character)
625
626 (defcustom gnus-undownloaded-mark ?-
627   "*Mark used for articles that weren't downloaded."
628   :version "22.1"
629   :group 'gnus-summary-marks
630   :type 'character)
631
632 (defcustom gnus-downloaded-mark ?+
633   "*Mark used for articles that were downloaded."
634   :group 'gnus-summary-marks
635   :type 'character)
636
637 (defcustom gnus-downloadable-mark ?%
638   "*Mark used for articles that are to be downloaded."
639   :group 'gnus-summary-marks
640   :type 'character)
641
642 (defcustom gnus-unsendable-mark ?=
643   "*Mark used for articles that won't be sent."
644   :group 'gnus-summary-marks
645   :type 'character)
646
647 (defcustom gnus-score-over-mark ?+
648   "*Score mark used for articles with high scores."
649   :group 'gnus-summary-marks
650   :type 'character)
651
652 (defcustom gnus-score-below-mark ?-
653   "*Score mark used for articles with low scores."
654   :group 'gnus-summary-marks
655   :type 'character)
656
657 (defcustom gnus-empty-thread-mark ?     ;Whitespace
658   "*There is no thread under the article."
659   :group 'gnus-summary-marks
660   :type 'character)
661
662 (defcustom gnus-not-empty-thread-mark ?=
663   "*There is a thread under the article."
664   :group 'gnus-summary-marks
665   :type 'character)
666
667 (defcustom gnus-view-pseudo-asynchronously nil
668   "*If non-nil, Gnus will view pseudo-articles asynchronously."
669   :group 'gnus-extract-view
670   :type 'boolean)
671
672 (defcustom gnus-auto-expirable-marks
673   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
674         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
675         gnus-duplicate-mark)
676   "*The list of marks converted into expiration if a group is auto-expirable."
677   :version "24.1"
678   :group 'gnus-summary
679   :type '(repeat character))
680
681 (defcustom gnus-inhibit-user-auto-expire t
682   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
683   :version "21.1"
684   :group 'gnus-summary
685   :type 'boolean)
686
687 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
688   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
689 If nil, the expirable marks will be unchanged except that the marks
690 will be removed when copying or moving articles to a group that has
691 not turned auto-expire on.  If non-nil, articles that have been read
692 will be marked as expirable when being copied or moved to a group in
693 which auto-expire is turned on."
694   :version "23.2"
695   :type 'boolean
696   :group 'gnus-summary-marks)
697
698 (defcustom gnus-view-pseudos nil
699   "*If `automatic', pseudo-articles will be viewed automatically.
700 If `not-confirm', pseudos will be viewed automatically, and the user
701 will not be asked to confirm the command."
702   :group 'gnus-extract-view
703   :type '(choice (const :tag "off" nil)
704                  (const automatic)
705                  (const not-confirm)))
706
707 (defcustom gnus-view-pseudos-separately t
708   "*If non-nil, one pseudo-article will be created for each file to be viewed.
709 If nil, all files that use the same viewing command will be given as a
710 list of parameters to that command."
711   :group 'gnus-extract-view
712   :type 'boolean)
713
714 (defcustom gnus-insert-pseudo-articles t
715   "*If non-nil, insert pseudo-articles when decoding articles."
716   :group 'gnus-extract-view
717   :type 'boolean)
718
719 (defcustom gnus-summary-dummy-line-format
720   "   %(:                             :%) %S\n"
721   "*The format specification for the dummy roots in the summary buffer.
722 It works along the same lines as a normal formatting string,
723 with some simple extensions.
724
725 %S  The subject
726
727 General format specifiers can also be used.
728 See `(gnus)Formatting Variables'."
729   :link '(custom-manual "(gnus)Formatting Variables")
730   :group 'gnus-threading
731   :type 'string)
732
733 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
734   "*The format specification for the summary mode line.
735 It works along the same lines as a normal formatting string,
736 with some simple extensions:
737
738 %G  Group name
739 %p  Unprefixed group name
740 %A  Current article number
741 %z  Current article score
742 %V  Gnus version
743 %U  Number of unread articles in the group
744 %e  Number of unselected articles in the group
745 %Z  A string with unread/unselected article counts
746 %g  Shortish group name
747 %S  Subject of the current article
748 %u  User-defined spec
749 %s  Current score file name
750 %d  Number of dormant articles
751 %r  Number of articles that have been marked as read in this session
752 %E  Number of articles expunged by the score files"
753   :group 'gnus-summary-format
754   :type 'string)
755
756 (defcustom gnus-list-identifiers nil
757   "Regexp that matches list identifiers to be removed from subject.
758 This can also be a list of regexps."
759   :version "21.1"
760   :group 'gnus-summary-format
761   :group 'gnus-article-hiding
762   :type '(choice (const :tag "none" nil)
763                  (regexp :value ".*")
764                  (repeat :value (".*") regexp)))
765
766 (defcustom gnus-summary-mark-below 0
767   "*Mark all articles with a score below this variable as read.
768 This variable is local to each summary buffer and usually set by the
769 score file."
770   :group 'gnus-score-default
771   :type 'integer)
772
773 (defun gnus-widget-reversible-match (widget value)
774   "Ignoring WIDGET, convert VALUE to internal form.
775 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
776   ;; (debug value)
777   (or (symbolp value)
778       (and (listp value)
779            (eq (length value) 2)
780            (eq (nth 0 value) 'not)
781            (symbolp (nth 1 value)))))
782
783 (defun gnus-widget-reversible-to-internal (widget value)
784   "Ignoring WIDGET, convert VALUE to internal form.
785 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
786 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
787   ;; (debug value)
788   (if (atom value)
789       (list value nil)
790     (list (nth 1 value) t)))
791
792 (defun gnus-widget-reversible-to-external (widget value)
793   "Ignoring WIDGET, convert VALUE to external form.
794 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
795 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
796   ;; (debug value)
797   (if (nth 1 value)
798       (list 'not (nth 0 value))
799     (nth 0 value)))
800
801 (define-widget 'gnus-widget-reversible 'group
802   "A `group' that convert values."
803   :match 'gnus-widget-reversible-match
804   :value-to-internal 'gnus-widget-reversible-to-internal
805   :value-to-external 'gnus-widget-reversible-to-external)
806
807 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
808   "*List of functions used for sorting articles in the summary buffer.
809
810 Each function takes two articles and returns non-nil if the first
811 article should be sorted before the other.  If you use more than one
812 function, the primary sort function should be the last.  You should
813 probably always include `gnus-article-sort-by-number' in the list of
814 sorting functions -- preferably first.  Also note that sorting by date
815 is often much slower than sorting by number, and the sorting order is
816 very similar.  (Sorting by date means sorting by the time the message
817 was sent, sorting by number means sorting by arrival time.)
818
819 Each item can also be a list `(not F)' where F is a function;
820 this reverses the sort order.
821
822 Ready-made functions include `gnus-article-sort-by-number',
823 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
824 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
825 and `gnus-article-sort-by-score'.
826
827 When threading is turned on, the variable `gnus-thread-sort-functions'
828 controls how articles are sorted."
829   :group 'gnus-summary-sort
830   :type '(repeat (gnus-widget-reversible
831                   (choice (function-item gnus-article-sort-by-number)
832                           (function-item gnus-article-sort-by-author)
833                           (function-item gnus-article-sort-by-subject)
834                           (function-item gnus-article-sort-by-date)
835                           (function-item gnus-article-sort-by-score)
836                           (function-item gnus-article-sort-by-random)
837                           (function :tag "other"))
838                   (boolean :tag "Reverse order"))))
839
840
841 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
842   "*List of functions used for sorting threads in the summary buffer.
843 By default, threads are sorted by article number.
844
845 Each function takes two threads and returns non-nil if the first
846 thread should be sorted before the other.  If you use more than one
847 function, the primary sort function should be the last.  You should
848 probably always include `gnus-thread-sort-by-number' in the list of
849 sorting functions -- preferably first.  Also note that sorting by date
850 is often much slower than sorting by number, and the sorting order is
851 very similar.  (Sorting by date means sorting by the time the message
852 was sent, sorting by number means sorting by arrival time.)
853
854 Each list item can also be a list `(not F)' where F is a
855 function; this specifies reversed sort order.
856
857 Ready-made functions include `gnus-thread-sort-by-number',
858 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
859 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
860 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
861 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
862 and `gnus-thread-sort-by-total-score' (see
863 `gnus-thread-score-function').
864
865 When threading is turned off, the variable
866 `gnus-article-sort-functions' controls how articles are sorted."
867   :group 'gnus-summary-sort
868   :type '(repeat
869           (gnus-widget-reversible
870            (choice (function-item gnus-thread-sort-by-number)
871                    (function-item gnus-thread-sort-by-author)
872                    (function-item gnus-thread-sort-by-recipient)
873                    (function-item gnus-thread-sort-by-subject)
874                    (function-item gnus-thread-sort-by-date)
875                    (function-item gnus-thread-sort-by-score)
876                    (function-item gnus-thread-sort-by-most-recent-number)
877                    (function-item gnus-thread-sort-by-most-recent-date)
878                    (function-item gnus-thread-sort-by-random)
879                    (function-item gnus-thread-sort-by-total-score)
880                    (function :tag "other"))
881            (boolean :tag "Reverse order"))))
882
883 (defcustom gnus-thread-score-function '+
884   "*Function used for calculating the total score of a thread.
885
886 The function is called with the scores of the article and each
887 subthread and should then return the score of the thread.
888
889 Some functions you can use are `+', `max', or `min'."
890   :group 'gnus-summary-sort
891   :type 'function)
892
893 (defcustom gnus-summary-expunge-below nil
894   "All articles that have a score less than this variable will be expunged.
895 This variable is local to the summary buffers."
896   :group 'gnus-score-default
897   :type '(choice (const :tag "off" nil)
898                  integer))
899
900 (defcustom gnus-thread-expunge-below nil
901   "All threads that have a total score less than this variable will be expunged.
902 See `gnus-thread-score-function' for en explanation of what a
903 \"thread score\" is.
904
905 This variable is local to the summary buffers."
906   :group 'gnus-threading
907   :group 'gnus-score-default
908   :type '(choice (const :tag "off" nil)
909                  integer))
910
911 (defcustom gnus-summary-mode-hook nil
912   "*A hook for Gnus summary mode.
913 This hook is run before any variables are set in the summary buffer."
914   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
915   :group 'gnus-summary-various
916   :type 'hook)
917
918 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
919 (when (featurep 'xemacs)
920   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
921   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
922   (add-hook 'gnus-summary-mode-hook
923             'gnus-xmas-switch-horizontal-scrollbar-off))
924
925 (defcustom gnus-summary-menu-hook nil
926   "*Hook run after the creation of the summary mode menu."
927   :group 'gnus-summary-visual
928   :type 'hook)
929
930 (defcustom gnus-summary-exit-hook nil
931   "*A hook called on exit from the summary buffer.
932 It will be called with point in the group buffer."
933   :group 'gnus-summary-exit
934   :type 'hook)
935
936 (defcustom gnus-summary-prepare-hook nil
937   "*A hook called after the summary buffer has been generated.
938 If you want to modify the summary buffer, you can use this hook."
939   :group 'gnus-summary-various
940   :type 'hook)
941
942 (defcustom gnus-summary-prepared-hook nil
943   "*A hook called as the last thing after the summary buffer has been generated."
944   :group 'gnus-summary-various
945   :type 'hook)
946
947 (defcustom gnus-summary-generate-hook nil
948   "*A hook run just before generating the summary buffer.
949 This hook is commonly used to customize threading variables and the
950 like."
951   :group 'gnus-summary-various
952   :type 'hook)
953
954 (defcustom gnus-select-group-hook nil
955   "*A hook called when a newsgroup is selected.
956
957 If you'd like to simplify subjects like the
958 `gnus-summary-next-same-subject' command does, you can use the
959 following hook:
960
961  (add-hook gnus-select-group-hook
962            (lambda ()
963              (mapcar (lambda (header)
964                        (mail-header-set-subject
965                         header
966                         (gnus-simplify-subject
967                          (mail-header-subject header) 're-only)))
968                      gnus-newsgroup-headers)))"
969   :group 'gnus-group-select
970   :type 'hook)
971
972 (defcustom gnus-select-article-hook nil
973   "*A hook called when an article is selected."
974   :group 'gnus-summary-choose
975   :options '(gnus-agent-fetch-selected-article)
976   :type 'hook)
977
978 (defcustom gnus-visual-mark-article-hook
979   (list 'gnus-highlight-selected-summary)
980   "*Hook run after selecting an article in the summary buffer.
981 It is meant to be used for highlighting the article in some way.  It
982 is not run if `gnus-visual' is nil."
983   :group 'gnus-summary-visual
984   :type 'hook)
985
986 (defcustom gnus-parse-headers-hook nil
987   "*A hook called before parsing the headers."
988   :group 'gnus-various
989   :type 'hook)
990
991 (defcustom gnus-exit-group-hook nil
992   "*A hook called when exiting summary mode.
993 This hook is not called from the non-updating exit commands like `Q'."
994   :group 'gnus-various
995   :type 'hook)
996
997 (defcustom gnus-summary-update-hook nil
998   "*A hook called when a summary line is changed.
999 The hook will not be called if `gnus-visual' is nil.
1000
1001 The default function `gnus-summary-highlight-line' will
1002 highlight the line according to the `gnus-summary-highlight'
1003 variable."
1004   :group 'gnus-summary-visual
1005   :type 'hook)
1006
1007 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1008   "*A hook called when an article is selected for the first time.
1009 The hook is intended to mark an article as read (or unread)
1010 automatically when it is selected."
1011   :group 'gnus-summary-choose
1012   :type 'hook)
1013
1014 (defcustom gnus-group-no-more-groups-hook nil
1015   "*A hook run when returning to group mode having no more (unread) groups."
1016   :group 'gnus-group-select
1017   :type 'hook)
1018
1019 (defcustom gnus-ps-print-hook nil
1020   "*A hook run before ps-printing something from Gnus."
1021   :group 'gnus-summary
1022   :type 'hook)
1023
1024 (defcustom gnus-summary-article-move-hook nil
1025   "*A hook called after an article is moved, copied, respooled, or crossposted."
1026   :version "22.1"
1027   :group 'gnus-summary
1028   :type 'hook)
1029
1030 (defcustom gnus-summary-article-delete-hook nil
1031   "*A hook called after an article is deleted."
1032   :version "22.1"
1033   :group 'gnus-summary
1034   :type 'hook)
1035
1036 (defcustom gnus-summary-article-expire-hook nil
1037   "*A hook called after an article is expired."
1038   :version "22.1"
1039   :group 'gnus-summary
1040   :type 'hook)
1041
1042 (defcustom gnus-summary-display-arrow
1043   (and (fboundp 'display-graphic-p)
1044        (display-graphic-p))
1045   "*If non-nil, display an arrow highlighting the current article."
1046   :version "22.1"
1047   :group 'gnus-summary
1048   :type 'boolean)
1049
1050 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1051   "Face used for highlighting the current article in the summary buffer."
1052   :group 'gnus-summary-visual
1053   :type 'face)
1054
1055 (defvar gnus-tmp-downloaded nil)
1056
1057 (defcustom gnus-summary-highlight
1058   '(((eq mark gnus-canceled-mark)
1059      . gnus-summary-cancelled)
1060     ((and uncached (> score default-high))
1061      . gnus-summary-high-undownloaded)
1062     ((and uncached (< score default-low))
1063      . gnus-summary-low-undownloaded)
1064     (uncached
1065      . gnus-summary-normal-undownloaded)
1066     ((and (> score default-high)
1067           (or (eq mark gnus-dormant-mark)
1068               (eq mark gnus-ticked-mark)))
1069      . gnus-summary-high-ticked)
1070     ((and (< score default-low)
1071           (or (eq mark gnus-dormant-mark)
1072               (eq mark gnus-ticked-mark)))
1073      . gnus-summary-low-ticked)
1074     ((or (eq mark gnus-dormant-mark)
1075          (eq mark gnus-ticked-mark))
1076      . gnus-summary-normal-ticked)
1077     ((and (> score default-high) (eq mark gnus-ancient-mark))
1078      . gnus-summary-high-ancient)
1079     ((and (< score default-low) (eq mark gnus-ancient-mark))
1080      . gnus-summary-low-ancient)
1081     ((eq mark gnus-ancient-mark)
1082      . gnus-summary-normal-ancient)
1083     ((and (> score default-high) (eq mark gnus-unread-mark))
1084      . gnus-summary-high-unread)
1085     ((and (< score default-low) (eq mark gnus-unread-mark))
1086      . gnus-summary-low-unread)
1087     ((eq mark gnus-unread-mark)
1088      . gnus-summary-normal-unread)
1089     ((> score default-high)
1090      . gnus-summary-high-read)
1091     ((< score default-low)
1092      . gnus-summary-low-read)
1093     (t
1094      . gnus-summary-normal-read))
1095   "*Controls the highlighting of summary buffer lines.
1096
1097 A list of (FORM . FACE) pairs.  When deciding how a particular
1098 summary line should be displayed, each form is evaluated.  The content
1099 of the face field after the first true form is used.  You can change
1100 how those summary lines are displayed, by editing the face field.
1101
1102 You can use the following variables in the FORM field.
1103
1104 score:        The article's score.
1105 default:      The default article score.
1106 default-high: The default score for high scored articles.
1107 default-low:  The default score for low scored articles.
1108 below:        The score below which articles are automatically marked as read.
1109 mark:         The article's mark.
1110 uncached:     Non-nil if the article is uncached."
1111   :group 'gnus-summary-visual
1112   :type '(repeat (cons (sexp :tag "Form" nil)
1113                        face)))
1114 (put 'gnus-summary-highlight 'risky-local-variable t)
1115
1116 (defcustom gnus-alter-header-function nil
1117   "Function called to allow alteration of article header structures.
1118 The function is called with one parameter, the article header vector,
1119 which it may alter in any way."
1120   :type '(choice (const :tag "None" nil)
1121                  function)
1122   :group 'gnus-summary)
1123
1124 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1125   "Function used to decode a string with encoded words.")
1126
1127 (defvar gnus-decode-encoded-address-function
1128   'mail-decode-encoded-address-string
1129   "Function used to decode addresses with encoded words.")
1130
1131 (defcustom gnus-extra-headers '(To Newsgroups)
1132   "*Extra headers to parse."
1133   :version "21.1"
1134   :group 'gnus-summary
1135   :type '(repeat symbol))
1136
1137 (defcustom gnus-ignored-from-addresses
1138   (and user-mail-address
1139        (not (string= user-mail-address ""))
1140        (regexp-quote user-mail-address))
1141   "*From headers that may be suppressed in favor of To headers.
1142 This can be a regexp or a list of regexps."
1143   :version "21.1"
1144   :group 'gnus-summary
1145   :type '(choice regexp
1146                  (repeat :tag "Regexp List" regexp)))
1147
1148 (defsubst gnus-ignored-from-addresses ()
1149   (gmm-regexp-concat gnus-ignored-from-addresses))
1150
1151 (defcustom gnus-summary-to-prefix "-> "
1152   "*String prefixed to the To field in the summary line when
1153 using `gnus-ignored-from-addresses'."
1154   :version "22.1"
1155   :group 'gnus-summary
1156   :type 'string)
1157
1158 (defcustom gnus-summary-newsgroup-prefix "=> "
1159   "*String prefixed to the Newsgroup field in the summary
1160 line when using `gnus-ignored-from-addresses'."
1161   :version "22.1"
1162   :group 'gnus-summary
1163   :type 'string)
1164
1165 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1166   "List of charsets that should be ignored.
1167 When these charsets are used in the \"charset\" parameter, the
1168 default charset will be used instead."
1169   :version "21.1"
1170   :type '(repeat symbol)
1171   :group 'gnus-charset)
1172
1173 (defcustom gnus-newsgroup-maximum-articles nil
1174   "The maximum number of articles a newsgroup.
1175 If this is a number, old articles in a newsgroup exceeding this number
1176 are silently ignored.  If it is nil, no article is ignored.  Note that
1177 setting this variable to a number might prevent you from reading very
1178 old articles."
1179   :group 'gnus-group-select
1180   :version "22.2"
1181   :type '(choice (const :tag "No limit" nil)
1182                  integer))
1183
1184 (gnus-define-group-parameter
1185  ignored-charsets
1186  :type list
1187  :function-document
1188  "Return the ignored charsets of GROUP."
1189  :variable gnus-group-ignored-charsets-alist
1190  :variable-default
1191  '(("alt\\.chinese\\.text" iso-8859-1))
1192  :variable-document
1193  "Alist of regexps (to match group names) and charsets that should be ignored.
1194 When these charsets are used in the \"charset\" parameter, the
1195 default charset will be used instead."
1196  :variable-group gnus-charset
1197  :variable-type '(repeat (cons (regexp :tag "Group")
1198                                (repeat symbol)))
1199  :parameter-type '(choice :tag "Ignored charsets"
1200                           :value nil
1201                           (repeat (symbol)))
1202  :parameter-document       "\
1203 List of charsets that should be ignored.
1204
1205 When these charsets are used in the \"charset\" parameter, the
1206 default charset will be used instead.")
1207
1208 (defcustom gnus-group-highlight-words-alist nil
1209   "Alist of group regexps and highlight regexps.
1210 This variable uses the same syntax as `gnus-emphasis-alist'."
1211   :version "21.1"
1212   :type '(repeat (cons (regexp :tag "Group")
1213                        (repeat (list (regexp :tag "Highlight regexp")
1214                                      (number :tag "Group for entire word" 0)
1215                                      (number :tag "Group for displayed part" 0)
1216                                      (symbol :tag "Face"
1217                                              gnus-emphasis-highlight-words)))))
1218   :group 'gnus-summary-visual)
1219
1220 (defcustom gnus-summary-show-article-charset-alist
1221   nil
1222   "Alist of number and charset.
1223 The article will be shown with the charset corresponding to the
1224 numbered argument.
1225 For example: ((1 . cn-gb-2312) (2 . big5))."
1226   :version "21.1"
1227   :type '(repeat (cons (number :tag "Argument" 1)
1228                        (symbol :tag "Charset")))
1229   :group 'gnus-charset)
1230
1231 (defcustom gnus-preserve-marks t
1232   "Whether marks are preserved when moving, copying and respooling messages."
1233   :version "21.1"
1234   :type 'boolean
1235   :group 'gnus-summary-marks)
1236
1237 (defcustom gnus-propagate-marks 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       (if (null (gnus-list-of-unread-articles group))
3943           (gnus-message 3 "Group %s contains no messages" group)
3944         (gnus-message 3 "Can't select group"))
3945       nil)
3946      ;; The user did a `C-g' while prompting for number of articles,
3947      ;; so we exit this group.
3948      ((eq did-select 'quit)
3949       (and (eq major-mode 'gnus-summary-mode)
3950            (not (equal (current-buffer) kill-buffer))
3951            (kill-buffer (current-buffer)))
3952       (when kill-buffer
3953         (gnus-kill-or-deaden-summary kill-buffer))
3954       (if (not quit-config)
3955           (progn
3956             (set-buffer gnus-group-buffer)
3957             (gnus-group-jump-to-group group)
3958             (gnus-configure-windows 'group 'force))
3959         (gnus-handle-ephemeral-exit quit-config))
3960       ;; Finally signal the quit.
3961       (signal 'quit nil))
3962      ;; The group was successfully selected.
3963      (t
3964       (gnus-set-global-variables)
3965       ;; Save the active value in effect when the group was entered.
3966       (setq gnus-newsgroup-active
3967             (gnus-copy-sequence
3968              (gnus-active gnus-newsgroup-name)))
3969       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
3970       ;; You can change the summary buffer in some way with this hook.
3971       (gnus-run-hooks 'gnus-select-group-hook)
3972       (when (memq 'summary (gnus-update-format-specifications
3973                             nil 'summary 'summary-mode 'summary-dummy))
3974         ;; The format specification for the summary line was updated,
3975         ;; so we need to update the mark positions as well.
3976         (gnus-update-summary-mark-positions))
3977       ;; Do score processing.
3978       (when gnus-use-scoring
3979         (gnus-possibly-score-headers))
3980       ;; Check whether to fill in the gaps in the threads.
3981       (when gnus-build-sparse-threads
3982         (gnus-build-sparse-threads))
3983       ;; Find the initial limit.
3984       (if show-all
3985           (let ((gnus-newsgroup-dormant nil))
3986             (gnus-summary-initial-limit show-all))
3987         (gnus-summary-initial-limit show-all))
3988       ;; Generate the summary buffer.
3989       (unless no-display
3990         (gnus-summary-prepare))
3991       (when gnus-use-trees
3992         (gnus-tree-open group)
3993         (setq gnus-summary-highlight-line-function
3994               'gnus-tree-highlight-article))
3995       ;; If the summary buffer is empty, but there are some low-scored
3996       ;; articles or some excluded dormants, we include these in the
3997       ;; buffer.
3998       (when (and (zerop (buffer-size))
3999                  (not no-display))
4000         (cond (gnus-newsgroup-dormant
4001                (gnus-summary-limit-include-dormant))
4002               ((and gnus-newsgroup-scored show-all)
4003                (gnus-summary-limit-include-expunged t))))
4004       ;; Function `gnus-apply-kill-file' must be called in this hook.
4005       (gnus-run-hooks 'gnus-apply-kill-hook)
4006       (if (and (zerop (buffer-size))
4007                (not no-display))
4008           (progn
4009             ;; This newsgroup is empty.
4010             (gnus-summary-catchup-and-exit nil t)
4011             (gnus-message 6 "No unread news")
4012             (when kill-buffer
4013               (gnus-kill-or-deaden-summary kill-buffer))
4014             ;; Return nil from this function.
4015             nil)
4016         ;; Hide conversation thread subtrees.  We cannot do this in
4017         ;; gnus-summary-prepare-hook since kill processing may not
4018         ;; work with hidden articles.
4019         (gnus-summary-maybe-hide-threads)
4020         (when kill-buffer
4021           (gnus-kill-or-deaden-summary kill-buffer))
4022         (gnus-summary-auto-select-subject)
4023         ;; Show first unread article if requested.
4024         (if (and (not no-article)
4025                  (not no-display)
4026                  gnus-newsgroup-unreads
4027                  gnus-auto-select-first)
4028             (progn
4029               (gnus-configure-windows 'summary)
4030               (let ((art (gnus-summary-article-number)))
4031                 (unless (and (not gnus-plugged)
4032                              (or (memq art gnus-newsgroup-undownloaded)
4033                                  (memq art gnus-newsgroup-downloadable)))
4034                   (gnus-summary-goto-article art))))
4035           ;; Don't select any articles.
4036           (gnus-summary-position-point)
4037           (gnus-configure-windows 'summary 'force)
4038           (gnus-set-mode-line 'summary))
4039         (when (and gnus-auto-center-group
4040                    (get-buffer-window gnus-group-buffer t))
4041           ;; Gotta use windows, because recenter does weird stuff if
4042           ;; the current buffer ain't the displayed window.
4043           (let ((owin (selected-window)))
4044             (select-window (get-buffer-window gnus-group-buffer t))
4045             (when (gnus-group-goto-group group)
4046               (recenter))
4047             (select-window owin)))
4048         ;; Mark this buffer as "prepared".
4049         (setq gnus-newsgroup-prepared t)
4050         (gnus-run-hooks 'gnus-summary-prepared-hook)
4051         (unless (gnus-ephemeral-group-p group)
4052           (gnus-group-update-group group))
4053         t)))))
4054
4055 (defun gnus-summary-auto-select-subject ()
4056   "Select the subject line on initial group entry."
4057   (goto-char (point-min))
4058   (cond
4059    ((eq gnus-auto-select-subject 'best)
4060     (gnus-summary-best-unread-subject))
4061    ((eq gnus-auto-select-subject 'unread)
4062     (gnus-summary-first-unread-subject))
4063    ((eq gnus-auto-select-subject 'unseen)
4064     (gnus-summary-first-unseen-subject))
4065    ((eq gnus-auto-select-subject 'unseen-or-unread)
4066     (gnus-summary-first-unseen-or-unread-subject))
4067    ((eq gnus-auto-select-subject 'first)
4068     ;; Do nothing.
4069     )
4070    ((functionp gnus-auto-select-subject)
4071     (funcall gnus-auto-select-subject))))
4072
4073 (defun gnus-summary-prepare ()
4074   "Generate the summary buffer."
4075   (interactive)
4076   (let ((inhibit-read-only t))
4077     (erase-buffer)
4078     (setq gnus-newsgroup-data nil
4079           gnus-newsgroup-data-reverse nil)
4080     (gnus-run-hooks 'gnus-summary-generate-hook)
4081     ;; Generate the buffer, either with threads or without.
4082     (when gnus-newsgroup-headers
4083       (gnus-summary-prepare-threads
4084        (if gnus-show-threads
4085            (gnus-sort-gathered-threads
4086             (funcall gnus-summary-thread-gathering-function
4087                      (gnus-sort-threads
4088                       (gnus-cut-threads (gnus-make-threads)))))
4089          ;; Unthreaded display.
4090          (gnus-sort-articles gnus-newsgroup-headers))))
4091     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4092     ;; Call hooks for modifying summary buffer.
4093     (goto-char (point-min))
4094     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4095
4096 (defsubst gnus-general-simplify-subject (subject)
4097   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4098   (setq subject
4099         (cond
4100          ;; Truncate the subject.
4101          (gnus-simplify-subject-functions
4102           (gnus-map-function gnus-simplify-subject-functions subject))
4103          ((numberp gnus-summary-gather-subject-limit)
4104           (setq subject (gnus-simplify-subject-re subject))
4105           (if (> (length subject) gnus-summary-gather-subject-limit)
4106               (substring subject 0 gnus-summary-gather-subject-limit)
4107             subject))
4108          ;; Fuzzily simplify it.
4109          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4110           (gnus-simplify-subject-fuzzy subject))
4111          ;; Just remove the leading "Re:".
4112          (t
4113           (gnus-simplify-subject-re subject))))
4114
4115   (if (and gnus-summary-gather-exclude-subject
4116            (string-match gnus-summary-gather-exclude-subject subject))
4117       nil                         ; This article shouldn't be gathered
4118     subject))
4119
4120 (defun gnus-summary-simplify-subject-query ()
4121   "Query where the respool algorithm would put this article."
4122   (interactive)
4123   (gnus-summary-select-article)
4124   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4125
4126 (defun gnus-gather-threads-by-subject (threads)
4127   "Gather threads by looking at Subject headers."
4128   (if (not gnus-summary-make-false-root)
4129       threads
4130     (let ((hashtb (gnus-make-hashtable 1024))
4131           (prev threads)
4132           (result threads)
4133           subject hthread whole-subject)
4134       (while threads
4135         (setq subject (gnus-general-simplify-subject
4136                        (setq whole-subject (mail-header-subject
4137                                             (caar threads)))))
4138         (when subject
4139           (if (setq hthread (gnus-gethash subject hashtb))
4140               (progn
4141                 ;; We enter a dummy root into the thread, if we
4142                 ;; haven't done that already.
4143                 (unless (stringp (caar hthread))
4144                   (setcar hthread (list whole-subject (car hthread))))
4145                 ;; We add this new gathered thread to this gathered
4146                 ;; thread.
4147                 (setcdr (car hthread)
4148                         (nconc (cdar hthread) (list (car threads))))
4149                 ;; Remove it from the list of threads.
4150                 (setcdr prev (cdr threads))
4151                 (setq threads prev))
4152             ;; Enter this thread into the hash table.
4153             (gnus-sethash subject
4154                           (if gnus-summary-make-false-root-always
4155                               (progn
4156                                 ;; If you want a dummy root above all
4157                                 ;; threads...
4158                                 (setcar threads (list whole-subject
4159                                                       (car threads)))
4160                                 threads)
4161                             threads)
4162                           hashtb)))
4163         (setq prev threads)
4164         (setq threads (cdr threads)))
4165       result)))
4166
4167 (defun gnus-gather-threads-by-references (threads)
4168   "Gather threads by looking at References headers."
4169   (let ((idhashtb (gnus-make-hashtable 1024))
4170         (thhashtb (gnus-make-hashtable 1024))
4171         (prev threads)
4172         (result threads)
4173         ids references id gthread gid entered ref)
4174     (while threads
4175       (when (setq references (mail-header-references (caar threads)))
4176         (setq id (mail-header-id (caar threads))
4177               ids (inline (gnus-split-references references))
4178               entered nil)
4179         (while (setq ref (pop ids))
4180           (setq ids (delete ref ids))
4181           (if (not (setq gid (gnus-gethash ref idhashtb)))
4182               (progn
4183                 (gnus-sethash ref id idhashtb)
4184                 (gnus-sethash id threads thhashtb))
4185             (setq gthread (gnus-gethash gid thhashtb))
4186             (unless entered
4187               ;; We enter a dummy root into the thread, if we
4188               ;; haven't done that already.
4189               (unless (stringp (caar gthread))
4190                 (setcar gthread (list (mail-header-subject (caar gthread))
4191                                       (car gthread))))
4192               ;; We add this new gathered thread to this gathered
4193               ;; thread.
4194               (setcdr (car gthread)
4195                       (nconc (cdar gthread) (list (car threads)))))
4196             ;; Add it into the thread hash table.
4197             (gnus-sethash id gthread thhashtb)
4198             (setq entered t)
4199             ;; Remove it from the list of threads.
4200             (setcdr prev (cdr threads))
4201             (setq threads prev))))
4202       (setq prev threads)
4203       (setq threads (cdr threads)))
4204     result))
4205
4206 (defun gnus-sort-gathered-threads (threads)
4207   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4208   (let ((result threads))
4209     (while threads
4210       (when (stringp (caar threads))
4211         (setcdr (car threads)
4212                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4213       (setq threads (cdr threads)))
4214     result))
4215
4216 (defun gnus-thread-loop-p (root thread)
4217   "Say whether ROOT is in THREAD."
4218   (let ((stack (list thread))
4219         (infloop 0)
4220         th)
4221     (while (setq thread (pop stack))
4222       (setq th (cdr thread))
4223       (while (and th
4224                   (not (eq (caar th) root)))
4225         (pop th))
4226       (if th
4227           ;; We have found a loop.
4228           (let (ref-dep)
4229             (setcdr thread (delq (car th) (cdr thread)))
4230             (if (boundp (setq ref-dep (intern "none"
4231                                               gnus-newsgroup-dependencies)))
4232                 (setcdr (symbol-value ref-dep)
4233                         (nconc (cdr (symbol-value ref-dep))
4234                                (list (car th))))
4235               (set ref-dep (list nil (car th))))
4236             (setq infloop 1
4237                   stack nil))
4238         ;; Push all the subthreads onto the stack.
4239         (push (cdr thread) stack)))
4240     infloop))
4241
4242 (defun gnus-make-threads ()
4243   "Go through the dependency hashtb and find the roots.  Return all threads."
4244   (let (threads)
4245     (while (catch 'infloop
4246              (mapatoms
4247               (lambda (refs)
4248                 ;; Deal with self-referencing References loops.
4249                 (when (and (car (symbol-value refs))
4250                            (not (zerop
4251                                  (apply
4252                                   '+
4253                                   (mapcar
4254                                    (lambda (thread)
4255                                      (gnus-thread-loop-p
4256                                       (car (symbol-value refs)) thread))
4257                                    (cdr (symbol-value refs)))))))
4258                   (setq threads nil)
4259                   (throw 'infloop t))
4260                 (unless (car (symbol-value refs))
4261                   ;; These threads do not refer back to any other
4262                   ;; articles, so they're roots.
4263                   (setq threads (append (cdr (symbol-value refs)) threads))))
4264               gnus-newsgroup-dependencies)))
4265     threads))
4266
4267 ;; Build the thread tree.
4268 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4269   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4270
4271 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4272 if it was already present.
4273
4274 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4275 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4276 Message-IDs will be renamed to a unique Message-ID before being
4277 entered.
4278
4279 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4280   (let* ((id (mail-header-id header))
4281          (id-dep (and id (intern id dependencies)))
4282          parent-id ref ref-dep ref-header replaced)
4283     ;; Enter this `header' in the `dependencies' table.
4284     (cond
4285      ((not id-dep)
4286       (setq header nil))
4287      ;; The first two cases do the normal part: enter a new `header'
4288      ;; in the `dependencies' table.
4289      ((not (boundp id-dep))
4290       (set id-dep (list header)))
4291      ((null (car (symbol-value id-dep)))
4292       (setcar (symbol-value id-dep) header))
4293
4294      ;; From here the `header' was already present in the
4295      ;; `dependencies' table.
4296      (force-new
4297       ;; Overrides an existing entry;
4298       ;; just set the header part of the entry.
4299       (setcar (symbol-value id-dep) header)
4300       (setq replaced t))
4301
4302      ;; Renames the existing `header' to a unique Message-ID.
4303      ((not gnus-summary-ignore-duplicates)
4304       ;; An article with this Message-ID has already been seen.
4305       ;; We rename the Message-ID.
4306       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4307            (list header))
4308       (mail-header-set-id header id))
4309
4310      ;; The last case ignores an existing entry, except it adds any
4311      ;; additional Xrefs (in case the two articles came from different
4312      ;; servers.
4313      ;; Also sets `header' to `nil' meaning that the `dependencies'
4314      ;; table was *not* modified.
4315      (t
4316       (mail-header-set-xref
4317        (car (symbol-value id-dep))
4318        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4319                    "")
4320                (or (mail-header-xref header) "")))
4321       (setq header nil)))
4322
4323     (when (and header (not replaced))
4324       ;; First check that we are not creating a References loop.
4325       (setq parent-id (gnus-parent-id (mail-header-references header)))
4326       (setq ref parent-id)
4327       (while (and ref
4328                   (setq ref-dep (intern-soft ref dependencies))
4329                   (boundp ref-dep)
4330                   (setq ref-header (car (symbol-value ref-dep))))
4331         (if (string= id ref)
4332             ;; Yuk!  This is a reference loop.  Make the article be a
4333             ;; root article.
4334             (progn
4335               (mail-header-set-references (car (symbol-value id-dep)) "none")
4336               (setq ref nil)
4337               (setq parent-id nil))
4338           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4339       (setq ref-dep (intern (or parent-id "none") dependencies))
4340       (if (boundp ref-dep)
4341           (setcdr (symbol-value ref-dep)
4342                   (nconc (cdr (symbol-value ref-dep))
4343                          (list (symbol-value id-dep))))
4344         (set ref-dep (list nil (symbol-value id-dep)))))
4345     header))
4346
4347 (defun gnus-extract-message-id-from-in-reply-to (string)
4348   (if (string-match "<[^>]+>" string)
4349       (substring string (match-beginning 0) (match-end 0))
4350     nil))
4351
4352 (defun gnus-build-sparse-threads ()
4353   (let ((headers gnus-newsgroup-headers)
4354         (mail-parse-charset gnus-newsgroup-charset)
4355         (gnus-summary-ignore-duplicates t)
4356         header references generation relations
4357         subject child end new-child date)
4358     ;; First we create an alist of generations/relations, where
4359     ;; generations is how much we trust the relation, and the relation
4360     ;; is parent/child.
4361     (gnus-message 7 "Making sparse threads...")
4362     (save-excursion
4363       (nnheader-set-temp-buffer " *gnus sparse threads*")
4364       (while (setq header (pop headers))
4365         (when (and (setq references (mail-header-references header))
4366                    (not (string= references "")))
4367           (insert references)
4368           (setq child (mail-header-id header)
4369                 subject (mail-header-subject header)
4370                 date (mail-header-date header)
4371                 generation 0)
4372           (while (search-backward ">" nil t)
4373             (setq end (1+ (point)))
4374             (when (search-backward "<" nil t)
4375               (setq new-child (buffer-substring (point) end))
4376               (push (list (incf generation)
4377                           child (setq child new-child)
4378                           subject date)
4379                     relations)))
4380           (when child
4381             (push (list (1+ generation) child nil subject) relations))
4382           (erase-buffer)))
4383       (kill-buffer (current-buffer)))
4384     ;; Sort over trustworthiness.
4385     (dolist (relation (sort relations 'car-less-than-car))
4386       (when (gnus-dependencies-add-header
4387              (make-full-mail-header
4388               gnus-reffed-article-number
4389               (nth 3 relation) "" (or (nth 4 relation) "")
4390               (nth 1 relation)
4391               (or (nth 2 relation) "") 0 0 "")
4392              gnus-newsgroup-dependencies nil)
4393         (push gnus-reffed-article-number gnus-newsgroup-limit)
4394         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4395         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4396               gnus-newsgroup-reads)
4397         (decf gnus-reffed-article-number)))
4398     (gnus-message 7 "Making sparse threads...done")))
4399
4400 (defun gnus-build-old-threads ()
4401   ;; Look at all the articles that refer back to old articles, and
4402   ;; fetch the headers for the articles that aren't there.  This will
4403   ;; build complete threads - if the roots haven't been expired by the
4404   ;; server, that is.
4405   (let ((mail-parse-charset gnus-newsgroup-charset)
4406         id heads)
4407     (mapatoms
4408      (lambda (refs)
4409        (when (not (car (symbol-value refs)))
4410          (setq heads (cdr (symbol-value refs)))
4411          (while heads
4412            (if (memq (mail-header-number (caar heads))
4413                      gnus-newsgroup-dormant)
4414                (setq heads (cdr heads))
4415              (setq id (symbol-name refs))
4416              (while (and (setq id (gnus-build-get-header id))
4417                          (not (car (gnus-id-to-thread id)))))
4418              (setq heads nil)))))
4419      gnus-newsgroup-dependencies)))
4420
4421 (defsubst gnus-remove-odd-characters (string)
4422   "Translate STRING into something that doesn't contain weird characters."
4423   (mm-subst-char-in-string
4424    ?\r ?\-
4425    (mm-subst-char-in-string ?\n ?\- string t) t))
4426
4427 ;; This function has to be called with point after the article number
4428 ;; on the beginning of the line.
4429 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4430   (let ((eol (point-at-eol))
4431         (buffer (current-buffer))
4432         header references in-reply-to)
4433
4434     ;; overview: [num subject from date id refs chars lines misc]
4435     (unwind-protect
4436         (let (x)
4437           (narrow-to-region (point) eol)
4438           (unless (eobp)
4439             (forward-char))
4440
4441           (setq header
4442                 (make-full-mail-header
4443                  number                 ; number
4444                  (condition-case ()     ; subject
4445                      (gnus-remove-odd-characters
4446                       (funcall gnus-decode-encoded-word-function
4447                                (setq x (nnheader-nov-field))))
4448                    (error x))
4449                  (condition-case ()     ; from
4450                      (gnus-remove-odd-characters
4451                       (funcall gnus-decode-encoded-address-function
4452                                (setq x (nnheader-nov-field))))
4453                    (error x))
4454                  (nnheader-nov-field)   ; date
4455                  (nnheader-nov-read-message-id number)  ; id
4456                  (setq references (nnheader-nov-field)) ; refs
4457                  (nnheader-nov-read-integer) ; chars
4458                  (nnheader-nov-read-integer) ; lines
4459                  (unless (eobp)
4460                    (if (looking-at "Xref: ")
4461                        (goto-char (match-end 0)))
4462                    (nnheader-nov-field)) ; Xref
4463                  (nnheader-nov-parse-extra)))) ; extra
4464
4465       (widen))
4466
4467     (when (and (string= references "")
4468                (setq in-reply-to (mail-header-extra header))
4469                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4470       (mail-header-set-references
4471        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4472
4473     (when gnus-alter-header-function
4474       (funcall gnus-alter-header-function header))
4475     (gnus-dependencies-add-header header dependencies force-new)))
4476
4477 (defun gnus-build-get-header (id)
4478   "Look through the buffer of NOV lines and find the header to ID.
4479 Enter this line into the dependencies hash table, and return
4480 the id of the parent article (if any)."
4481   (let ((deps gnus-newsgroup-dependencies)
4482         found header)
4483     (prog1
4484         (with-current-buffer nntp-server-buffer
4485           (let ((case-fold-search nil))
4486             (goto-char (point-min))
4487             (while (and (not found)
4488                         (search-forward id nil t))
4489               (beginning-of-line)
4490               (setq found (looking-at
4491                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4492                                    (regexp-quote id))))
4493               (or found (beginning-of-line 2)))
4494             (when found
4495               (beginning-of-line)
4496               (and
4497                (setq header (gnus-nov-parse-line
4498                              (read (current-buffer)) deps))
4499                (gnus-parent-id (mail-header-references header))))))
4500       (when header
4501         (let ((number (mail-header-number header)))
4502           (push number gnus-newsgroup-limit)
4503           (push header gnus-newsgroup-headers)
4504           (if (memq number gnus-newsgroup-unselected)
4505               (progn
4506                 (setq gnus-newsgroup-unreads
4507                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4508                                                number))
4509                 (setq gnus-newsgroup-unselected
4510                       (delq number gnus-newsgroup-unselected)))
4511             (push number gnus-newsgroup-ancient)))))))
4512
4513 (defun gnus-build-all-threads ()
4514   "Read all the headers."
4515   (let ((gnus-summary-ignore-duplicates t)
4516         (mail-parse-charset gnus-newsgroup-charset)
4517         (dependencies gnus-newsgroup-dependencies)
4518         header article)
4519     (with-current-buffer nntp-server-buffer
4520       (let ((case-fold-search nil))
4521         (goto-char (point-min))
4522         (while (not (eobp))
4523           (ignore-errors
4524             (setq article (read (current-buffer))
4525                   header (gnus-nov-parse-line article dependencies t)))
4526           (when header
4527             (with-current-buffer gnus-summary-buffer
4528               (push header gnus-newsgroup-headers)
4529               (if (memq (setq article (mail-header-number header))
4530                         gnus-newsgroup-unselected)
4531                   (progn
4532                     (setq gnus-newsgroup-unreads
4533                           (gnus-add-to-sorted-list
4534                            gnus-newsgroup-unreads article))
4535                     (setq gnus-newsgroup-unselected
4536                           (delq article gnus-newsgroup-unselected)))
4537                 (push article gnus-newsgroup-ancient)))
4538             (forward-line 1)))))))
4539
4540 (defun gnus-summary-update-article-line (article header)
4541   "Update the line for ARTICLE using HEADER."
4542   (let* ((id (mail-header-id header))
4543          (thread (gnus-id-to-thread id)))
4544     (unless thread
4545       (error "Article in no thread"))
4546     ;; Update the thread.
4547     (setcar thread header)
4548     (gnus-summary-goto-subject article)
4549     (let* ((datal (gnus-data-find-list article))
4550            (data (car datal))
4551            (inhibit-read-only t)
4552            (level (gnus-summary-thread-level)))
4553       (gnus-delete-line)
4554       (let ((inserted (- (point)
4555                          (progn
4556                            (gnus-summary-insert-line
4557                             header level nil
4558                             (memq article gnus-newsgroup-undownloaded)
4559                             (gnus-article-mark article)
4560                             (memq article gnus-newsgroup-replied)
4561                             (memq article gnus-newsgroup-expirable)
4562                             ;; Only insert the Subject string when it's different
4563                             ;; from the previous Subject string.
4564                             (if (and
4565                                  gnus-show-threads
4566                                  (gnus-subject-equal
4567                                   (condition-case ()
4568                                       (mail-header-subject
4569                                        (gnus-data-header
4570                                         (cadr
4571                                          (gnus-data-find-list
4572                                           article
4573                                           (gnus-data-list t)))))
4574                                     ;; Error on the side of excessive subjects.
4575                                     (error ""))
4576                                   (mail-header-subject header)))
4577                                 ""
4578                               (mail-header-subject header))
4579                             nil (cdr (assq article gnus-newsgroup-scored))
4580                             (memq article gnus-newsgroup-processable))
4581                            (point)))))
4582         (when (cdr datal)
4583           (gnus-data-update-list
4584            (cdr datal)
4585            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4586
4587 (defun gnus-summary-update-article (article &optional iheader)
4588   "Update ARTICLE in the summary buffer."
4589   (set-buffer gnus-summary-buffer)
4590   (let* ((header (gnus-summary-article-header article))
4591          (id (mail-header-id header))
4592          (data (gnus-data-find article))
4593          (thread (gnus-id-to-thread id))
4594          (references (mail-header-references header))
4595          (parent
4596           (gnus-id-to-thread
4597            (or (gnus-parent-id
4598                 (when (and references
4599                            (not (equal "" references)))
4600                   references))
4601                "none")))
4602          (inhibit-read-only t)
4603          (old (car thread)))
4604     (when thread
4605       (unless iheader
4606         (setcar thread nil)
4607         (when parent
4608           (delq thread parent)))
4609       (if (gnus-summary-insert-subject id header)
4610           ;; Set the (possibly) new article number in the data structure.
4611           (gnus-data-set-number data (gnus-id-to-article id))
4612         (setcar thread old)
4613         nil))))
4614
4615 (defun gnus-rebuild-thread (id &optional line)
4616   "Rebuild the thread containing ID.
4617 If LINE, insert the rebuilt thread starting on line LINE."
4618   (let ((inhibit-read-only t)
4619         old-pos current thread data)
4620     (if (not gnus-show-threads)
4621         (setq thread (list (car (gnus-id-to-thread id))))
4622       ;; Get the thread this article is part of.
4623       (setq thread (gnus-remove-thread id)))
4624     (setq old-pos (point-at-bol))
4625     (setq current (save-excursion
4626                     (and (re-search-backward "[\r\n]" nil t)
4627                          (gnus-summary-article-number))))
4628     ;; If this is a gathered thread, we have to go some re-gathering.
4629     (when (stringp (car thread))
4630       (let ((subject (car thread))
4631             roots thr)
4632         (setq thread (cdr thread))
4633         (while thread
4634           (unless (memq (setq thr (gnus-id-to-thread
4635                                    (gnus-root-id
4636                                     (mail-header-id (caar thread)))))
4637                         roots)
4638             (push thr roots))
4639           (setq thread (cdr thread)))
4640         ;; We now have all (unique) roots.
4641         (if (= (length roots) 1)
4642             ;; All the loose roots are now one solid root.
4643             (setq thread (car roots))
4644           (setq thread (cons subject (gnus-sort-threads roots))))))
4645     (let (threads)
4646       ;; We then insert this thread into the summary buffer.
4647       (when line
4648         (goto-char (point-min))
4649         (forward-line (1- line)))
4650       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4651         (if gnus-show-threads
4652             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4653           (gnus-summary-prepare-unthreaded thread))
4654         (setq data (nreverse gnus-newsgroup-data))
4655         (setq threads gnus-newsgroup-threads))
4656       ;; We splice the new data into the data structure.
4657       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4658       ;;!!! then we want to insert at the beginning of the buffer.
4659       ;;!!! That happens to be true with Gnus now, but that may
4660       ;;!!! change in the future.  Perhaps.
4661       (gnus-data-enter-list
4662        (if line nil current) data (- (point) old-pos))
4663       (setq gnus-newsgroup-threads
4664             (nconc threads gnus-newsgroup-threads))
4665       (gnus-data-compute-positions))))
4666
4667 (defun gnus-number-to-header (number)
4668   "Return the header for article NUMBER."
4669   (let ((headers gnus-newsgroup-headers))
4670     (while (and headers
4671                 (not (= number (mail-header-number (car headers)))))
4672       (pop headers))
4673     (when headers
4674       (car headers))))
4675
4676 (defun gnus-parent-headers (in-headers &optional generation)
4677   "Return the headers of the GENERATIONeth parent of HEADERS."
4678   (unless generation
4679     (setq generation 1))
4680   (let ((parent t)
4681         (headers in-headers)
4682         references)
4683     (while (and parent
4684                 (not (zerop generation))
4685                 (setq references (mail-header-references headers)))
4686       (setq headers (if (and references
4687                              (setq parent (gnus-parent-id references)))
4688                         (car (gnus-id-to-thread parent))
4689                       nil))
4690       (decf generation))
4691     (and (not (eq headers in-headers))
4692          headers)))
4693
4694 (defun gnus-id-to-thread (id)
4695   "Return the (sub-)thread where ID appears."
4696   (gnus-gethash id gnus-newsgroup-dependencies))
4697
4698 (defun gnus-id-to-article (id)
4699   "Return the article number of ID."
4700   (let ((thread (gnus-id-to-thread id)))
4701     (when (and thread
4702                (car thread))
4703       (mail-header-number (car thread)))))
4704
4705 (defun gnus-id-to-header (id)
4706   "Return the article headers of ID."
4707   (car (gnus-id-to-thread id)))
4708
4709 (defun gnus-article-displayed-root-p (article)
4710   "Say whether ARTICLE is a root(ish) article."
4711   (let ((level (gnus-summary-thread-level article))
4712         (refs (mail-header-references  (gnus-summary-article-header article)))
4713         particle)
4714     (cond
4715      ((null level) nil)
4716      ((zerop level) t)
4717      ((null refs) t)
4718      ((null (gnus-parent-id refs)) t)
4719      ((and (= 1 level)
4720            (null (setq particle (gnus-id-to-article
4721                                  (gnus-parent-id refs))))
4722            (null (gnus-summary-thread-level particle)))))))
4723
4724 (defun gnus-root-id (id)
4725   "Return the id of the root of the thread where ID appears."
4726   (let (last-id prev)
4727     (while (and id (setq prev (car (gnus-id-to-thread id))))
4728       (setq last-id id
4729             id (gnus-parent-id (mail-header-references prev))))
4730     last-id))
4731
4732 (defun gnus-articles-in-thread (thread)
4733   "Return the list of articles in THREAD."
4734   (cons (mail-header-number (car thread))
4735         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4736
4737 (defun gnus-remove-thread (id &optional dont-remove)
4738   "Remove the thread that has ID in it."
4739   (let (headers thread last-id)
4740     ;; First go up in this thread until we find the root.
4741     (setq last-id (gnus-root-id id)
4742           headers (message-flatten-list (gnus-id-to-thread last-id)))
4743     ;; We have now found the real root of this thread.  It might have
4744     ;; been gathered into some loose thread, so we have to search
4745     ;; through the threads to find the thread we wanted.
4746     (let ((threads gnus-newsgroup-threads)
4747           sub)
4748       (while threads
4749         (setq sub (car threads))
4750         (if (stringp (car sub))
4751             ;; This is a gathered thread, so we look at the roots
4752             ;; below it to find whether this article is in this
4753             ;; gathered root.
4754             (progn
4755               (setq sub (cdr sub))
4756               (while sub
4757                 (when (member (caar sub) headers)
4758                   (setq thread (car threads)
4759                         threads nil
4760                         sub nil))
4761                 (setq sub (cdr sub))))
4762           ;; It's an ordinary thread, so we check it.
4763           (when (eq (car sub) (car headers))
4764             (setq thread sub
4765                   threads nil)))
4766         (setq threads (cdr threads)))
4767       ;; If this article is in no thread, then it's a root.
4768       (if thread
4769           (unless dont-remove
4770             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4771         (setq thread (gnus-id-to-thread last-id)))
4772       (when thread
4773         (prog1
4774             thread                      ; We return this thread.
4775           (unless dont-remove
4776             (if (stringp (car thread))
4777                 (progn
4778                   ;; If we use dummy roots, then we have to remove the
4779                   ;; dummy root as well.
4780                   (when (eq gnus-summary-make-false-root 'dummy)
4781                     ;; We go to the dummy root by going to
4782                     ;; the first sub-"thread", and then one line up.
4783                     (gnus-summary-goto-article
4784                      (mail-header-number (caadr thread)))
4785                     (forward-line -1)
4786                     (gnus-delete-line)
4787                     (gnus-data-compute-positions))
4788                   (setq thread (cdr thread))
4789                   (while thread
4790                     (gnus-remove-thread-1 (car thread))
4791                     (setq thread (cdr thread))))
4792               (gnus-remove-thread-1 thread))))))))
4793
4794 (defun gnus-remove-thread-1 (thread)
4795   "Remove the thread THREAD recursively."
4796   (let ((number (mail-header-number (pop thread)))
4797         d)
4798     (setq thread (reverse thread))
4799     (while thread
4800       (gnus-remove-thread-1 (pop thread)))
4801     (when (setq d (gnus-data-find number))
4802       (goto-char (gnus-data-pos d))
4803       (gnus-summary-show-thread)
4804       (gnus-data-remove
4805        number
4806        (- (point-at-bol)
4807           (prog1
4808               (1+ (point-at-eol))
4809             (gnus-delete-line)))))))
4810
4811 (defun gnus-sort-threads-recursive (threads func)
4812   (sort (mapcar (lambda (thread)
4813                   (cons (car thread)
4814                         (and (cdr thread)
4815                              (gnus-sort-threads-recursive (cdr thread) func))))
4816                 threads) func))
4817
4818 (defun gnus-sort-threads-loop (threads func)
4819   (let* ((superthread (cons nil threads))
4820          (stack (list (cons superthread threads)))
4821          remaining-threads thread)
4822     (while stack
4823       (setq remaining-threads (cdr (car stack)))
4824       (if remaining-threads
4825           (progn (setq thread (car remaining-threads))
4826                  (setcdr (car stack) (cdr remaining-threads))
4827                  (if (cdr thread)
4828                      (push (cons thread (cdr thread)) stack)))
4829         (setq thread (caar stack))
4830         (setcdr thread (sort (cdr thread) func))
4831         (pop stack)))
4832     (cdr superthread)))
4833
4834 (defun gnus-sort-threads (threads)
4835   "Sort THREADS."
4836   (if (not gnus-thread-sort-functions)
4837       threads
4838     (gnus-message 8 "Sorting threads...")
4839     (prog1
4840         (condition-case nil
4841             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4842               (gnus-sort-threads-recursive
4843                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4844           ;; Even after binding max-lisp-eval-depth, the recursive
4845           ;; sorter might fail for very long threads.  In that case,
4846           ;; try using a (less well-tested) non-recursive sorter.
4847           (error (gnus-message 9 "Sorting threads with loop...")
4848                  (gnus-sort-threads-loop
4849                   threads (gnus-make-sort-function
4850                            gnus-thread-sort-functions))))
4851       (gnus-message 8 "Sorting threads...done"))))
4852
4853 (defun gnus-sort-articles (articles)
4854   "Sort ARTICLES."
4855   (when gnus-article-sort-functions
4856     (gnus-message 7 "Sorting articles...")
4857     (prog1
4858         (setq gnus-newsgroup-headers
4859               (sort articles (gnus-make-sort-function
4860                               gnus-article-sort-functions)))
4861       (gnus-message 7 "Sorting articles...done"))))
4862
4863 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4864 (defmacro gnus-thread-header (thread)
4865   "Return header of first article in THREAD.
4866 Note that THREAD must never, ever be anything else than a variable -
4867 using some other form will lead to serious barfage."
4868   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4869   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4870   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4871         (vector thread) 2))
4872
4873 (defsubst gnus-article-sort-by-number (h1 h2)
4874   "Sort articles by article number."
4875   (< (mail-header-number h1)
4876      (mail-header-number h2)))
4877
4878 (defun gnus-thread-sort-by-number (h1 h2)
4879   "Sort threads by root article number."
4880   (gnus-article-sort-by-number
4881    (gnus-thread-header h1) (gnus-thread-header h2)))
4882
4883 (defsubst gnus-article-sort-by-random (h1 h2)
4884   "Sort articles randomly."
4885   (zerop (random 2)))
4886
4887 (defun gnus-thread-sort-by-random (h1 h2)
4888   "Sort threads randomly."
4889   (gnus-article-sort-by-random
4890    (gnus-thread-header h1) (gnus-thread-header h2)))
4891
4892 (defsubst gnus-article-sort-by-lines (h1 h2)
4893   "Sort articles by article Lines header."
4894   (< (mail-header-lines h1)
4895      (mail-header-lines h2)))
4896
4897 (defun gnus-thread-sort-by-lines (h1 h2)
4898   "Sort threads by root article Lines header."
4899   (gnus-article-sort-by-lines
4900    (gnus-thread-header h1) (gnus-thread-header h2)))
4901
4902 (defsubst gnus-article-sort-by-chars (h1 h2)
4903   "Sort articles by octet length."
4904   (< (mail-header-chars h1)
4905      (mail-header-chars h2)))
4906
4907 (defun gnus-thread-sort-by-chars (h1 h2)
4908   "Sort threads by root article octet length."
4909   (gnus-article-sort-by-chars
4910    (gnus-thread-header h1) (gnus-thread-header h2)))
4911
4912 (defsubst gnus-article-sort-by-author (h1 h2)
4913   "Sort articles by root author."
4914   (gnus-string<
4915    (let ((extract (funcall
4916                    gnus-extract-address-components
4917                    (mail-header-from h1))))
4918      (or (car extract) (cadr extract) ""))
4919    (let ((extract (funcall
4920                    gnus-extract-address-components
4921                    (mail-header-from h2))))
4922      (or (car extract) (cadr extract) ""))))
4923
4924 (defun gnus-thread-sort-by-author (h1 h2)
4925   "Sort threads by root author."
4926   (gnus-article-sort-by-author
4927    (gnus-thread-header h1)  (gnus-thread-header h2)))
4928
4929 (defsubst gnus-article-sort-by-recipient (h1 h2)
4930   "Sort articles by recipient."
4931   (gnus-string<
4932    (let ((extract (funcall
4933                    gnus-extract-address-components
4934                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4935      (or (car extract) (cadr extract)))
4936    (let ((extract (funcall
4937                    gnus-extract-address-components
4938                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4939      (or (car extract) (cadr extract)))))
4940
4941 (defun gnus-thread-sort-by-recipient (h1 h2)
4942   "Sort threads by root recipient."
4943   (gnus-article-sort-by-recipient
4944    (gnus-thread-header h1) (gnus-thread-header h2)))
4945
4946 (defsubst gnus-article-sort-by-subject (h1 h2)
4947   "Sort articles by root subject."
4948   (gnus-string<
4949    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4950    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4951
4952 (defun gnus-thread-sort-by-subject (h1 h2)
4953   "Sort threads by root subject."
4954   (gnus-article-sort-by-subject
4955    (gnus-thread-header h1) (gnus-thread-header h2)))
4956
4957 (defsubst gnus-article-sort-by-date (h1 h2)
4958   "Sort articles by root article date."
4959   (time-less-p
4960    (gnus-date-get-time (mail-header-date h1))
4961    (gnus-date-get-time (mail-header-date h2))))
4962
4963 (defun gnus-thread-sort-by-date (h1 h2)
4964   "Sort threads by root article date."
4965   (gnus-article-sort-by-date
4966    (gnus-thread-header h1) (gnus-thread-header h2)))
4967
4968 (defsubst gnus-article-sort-by-score (h1 h2)
4969   "Sort articles by root article score.
4970 Unscored articles will be counted as having a score of zero."
4971   (> (or (cdr (assq (mail-header-number h1)
4972                     gnus-newsgroup-scored))
4973          gnus-summary-default-score 0)
4974      (or (cdr (assq (mail-header-number h2)
4975                     gnus-newsgroup-scored))
4976          gnus-summary-default-score 0)))
4977
4978 (defun gnus-thread-sort-by-score (h1 h2)
4979   "Sort threads by root article score."
4980   (gnus-article-sort-by-score
4981    (gnus-thread-header h1) (gnus-thread-header h2)))
4982
4983 (defun gnus-thread-sort-by-total-score (h1 h2)
4984   "Sort threads by the sum of all scores in the thread.
4985 Unscored articles will be counted as having a score of zero."
4986   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4987
4988 (defun gnus-thread-total-score (thread)
4989   ;; This function find the total score of THREAD.
4990   (cond
4991    ((null thread)
4992     0)
4993    ((consp thread)
4994     (if (stringp (car thread))
4995         (apply gnus-thread-score-function 0
4996                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4997       (gnus-thread-total-score-1 thread)))
4998    (t
4999     (gnus-thread-total-score-1 (list thread)))))
5000
5001 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5002   "Sort articles by number."
5003   (gnus-article-sort-by-number h1 h2))
5004
5005 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5006   "Sort threads such that the thread with the most recently arrived article comes first."
5007   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5008
5009 (defun gnus-thread-highest-number (thread)
5010   "Return the highest article number in THREAD."
5011   (apply 'max (mapcar (lambda (header)
5012                         (mail-header-number header))
5013                       (message-flatten-list thread))))
5014
5015 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5016   "Sort articles by number."
5017   (gnus-article-sort-by-date h1 h2))
5018
5019 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5020   "Sort threads such that the thread with the most recently dated article comes first."
5021   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5022
5023 ; Since this is called not only to sort the top-level threads, but
5024 ; also in recursive sorts to order the articles within a thread, each
5025 ; article will be processed many times.  Thus it speeds things up
5026 ; quite a bit to use gnus-date-get-time, which caches the time value.
5027 (defun gnus-thread-latest-date (thread)
5028   "Return the highest article date in THREAD."
5029   (apply 'max
5030          (mapcar (lambda (header) (gnus-float-time
5031                                    (gnus-date-get-time
5032                                     (mail-header-date header))))
5033                  (message-flatten-list thread))))
5034
5035 (defun gnus-thread-total-score-1 (root)
5036   ;; This function find the total score of the thread below ROOT.
5037   (setq root (car root))
5038   (apply gnus-thread-score-function
5039          (or (append
5040               (mapcar 'gnus-thread-total-score
5041                       (cdr (gnus-id-to-thread (mail-header-id root))))
5042               (when (> (mail-header-number root) 0)
5043                 (list (or (cdr (assq (mail-header-number root)
5044                                      gnus-newsgroup-scored))
5045                           gnus-summary-default-score 0))))
5046              (list gnus-summary-default-score)
5047              '(0))))
5048
5049 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5050 (defvar gnus-tmp-prev-subject nil)
5051 (defvar gnus-tmp-false-parent nil)
5052 (defvar gnus-tmp-root-expunged nil)
5053 (defvar gnus-tmp-dummy-line nil)
5054
5055 (defun gnus-extra-header (type &optional header)
5056   "Return the extra header of TYPE."
5057   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5058       ""))
5059
5060 (defvar gnus-tmp-thread-tree-header-string "")
5061
5062 (defcustom gnus-sum-thread-tree-root "> "
5063   "With %B spec, used for the root of a thread.
5064 If nil, use subject instead."
5065   :version "22.1"
5066   :type '(radio (const :format "%v  " nil) string)
5067   :group 'gnus-thread)
5068
5069 (defcustom gnus-sum-thread-tree-false-root "> "
5070   "With %B spec, used for a false root of a thread.
5071 If nil, use subject instead."
5072   :version "22.1"
5073   :type '(radio (const :format "%v  " nil) string)
5074   :group 'gnus-thread)
5075
5076 (defcustom gnus-sum-thread-tree-single-indent ""
5077   "With %B spec, used for a thread with just one message.
5078 If nil, use subject instead."
5079   :version "22.1"
5080   :type '(radio (const :format "%v  " nil) string)
5081   :group 'gnus-thread)
5082
5083 (defcustom gnus-sum-thread-tree-vertical "| "
5084   "With %B spec, used for drawing a vertical line."
5085   :version "22.1"
5086   :type 'string
5087   :group 'gnus-thread)
5088
5089 (defcustom gnus-sum-thread-tree-indent "  "
5090   "With %B spec, used for indenting."
5091   :version "22.1"
5092   :type 'string
5093   :group 'gnus-thread)
5094
5095 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5096   "With %B spec, used for a leaf with brothers."
5097   :version "22.1"
5098   :type 'string
5099   :group 'gnus-thread)
5100
5101 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5102   "With %B spec, used for a leaf without brothers."
5103   :version "22.1"
5104   :type 'string
5105   :group 'gnus-thread)
5106
5107 (defcustom gnus-summary-display-while-building nil
5108   "If non-nil, show and update the summary buffer as it's being built.
5109 If the value is t, update the buffer after every line is inserted.  If
5110 the value is an integer (N), update the display every N lines."
5111   :version "22.1"
5112   :group 'gnus-thread
5113   :type '(choice (const :tag "off" nil)
5114                  number
5115                  (const :tag "frequently" t)))
5116
5117 (defun gnus-summary-prepare-threads (threads)
5118   "Prepare summary buffer from THREADS and indentation LEVEL.
5119 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5120 or a straight list of headers."
5121   (gnus-message 7 "Generating summary...")
5122
5123   (setq gnus-newsgroup-threads threads)
5124   (beginning-of-line)
5125
5126   (let ((gnus-tmp-level 0)
5127         (default-score (or gnus-summary-default-score 0))
5128         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5129         (building-line-count gnus-summary-display-while-building)
5130         (building-count (integerp gnus-summary-display-while-building))
5131         thread number subject stack state gnus-tmp-gathered beg-match
5132         new-roots gnus-tmp-new-adopts thread-end simp-subject
5133         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5134         gnus-tmp-replied gnus-tmp-subject-or-nil
5135         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5136         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5137         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5138         tree-stack)
5139
5140     (setq gnus-tmp-prev-subject nil
5141           gnus-tmp-thread-tree-header-string "")
5142
5143     (if (vectorp (car threads))
5144         ;; If this is a straight (sic) list of headers, then a
5145         ;; threaded summary display isn't required, so we just create
5146         ;; an unthreaded one.
5147         (gnus-summary-prepare-unthreaded threads)
5148
5149       ;; Do the threaded display.
5150
5151       (if gnus-summary-display-while-building
5152           (switch-to-buffer (buffer-name)))
5153       (while (or threads stack gnus-tmp-new-adopts new-roots)
5154
5155         (if (and (= gnus-tmp-level 0)
5156                  (or (not stack)
5157                      (= (caar stack) 0))
5158                  (not gnus-tmp-false-parent)
5159                  (or gnus-tmp-new-adopts new-roots))
5160             (if gnus-tmp-new-adopts
5161                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5162                       thread (list (car gnus-tmp-new-adopts))
5163                       gnus-tmp-header (caar thread)
5164                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5165               (when new-roots
5166                 (setq thread (list (car new-roots))
5167                       gnus-tmp-header (caar thread)
5168                       new-roots (cdr new-roots))))
5169
5170           (if threads
5171               ;; If there are some threads, we do them before the
5172               ;; threads on the stack.
5173               (setq thread threads
5174                     gnus-tmp-header (caar thread))
5175             ;; There were no current threads, so we pop something off
5176             ;; the stack.
5177             (setq state (car stack)
5178                   gnus-tmp-level (car state)
5179                   tree-stack (cadr state)
5180                   thread (caddr state)
5181                   stack (cdr stack)
5182                   gnus-tmp-header (caar thread))))
5183
5184         (setq gnus-tmp-false-parent nil)
5185         (setq gnus-tmp-root-expunged nil)
5186         (setq thread-end nil)
5187
5188         (if (stringp gnus-tmp-header)
5189             ;; The header is a dummy root.
5190             (cond
5191              ((eq gnus-summary-make-false-root 'adopt)
5192               ;; We let the first article adopt the rest.
5193               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5194                                                (cddar thread)))
5195               (setq gnus-tmp-gathered
5196                     (nconc (mapcar
5197                             (lambda (h) (mail-header-number (car h)))
5198                             (cddar thread))
5199                            gnus-tmp-gathered))
5200               (setq thread (cons (list (caar thread)
5201                                        (cadar thread))
5202                                  (cdr thread)))
5203               (setq gnus-tmp-level -1
5204                     gnus-tmp-false-parent t))
5205              ((eq gnus-summary-make-false-root 'empty)
5206               ;; We print adopted articles with empty subject fields.
5207               (setq gnus-tmp-gathered
5208                     (nconc (mapcar
5209                             (lambda (h) (mail-header-number (car h)))
5210                             (cddar thread))
5211                            gnus-tmp-gathered))
5212               (setq gnus-tmp-level -1))
5213              ((eq gnus-summary-make-false-root 'dummy)
5214               ;; We remember that we probably want to output a dummy
5215               ;; root.
5216               (setq gnus-tmp-dummy-line gnus-tmp-header)
5217               (setq gnus-tmp-prev-subject gnus-tmp-header))
5218              (t
5219               ;; We do not make a root for the gathered
5220               ;; sub-threads at all.
5221               (setq gnus-tmp-level -1)))
5222
5223           (setq number (mail-header-number gnus-tmp-header)
5224                 subject (mail-header-subject gnus-tmp-header)
5225                 simp-subject (gnus-simplify-subject-fully subject))
5226
5227           (cond
5228            ;; If the thread has changed subject, we might want to make
5229            ;; this subthread into a root.
5230            ((and (null gnus-thread-ignore-subject)
5231                  (not (zerop gnus-tmp-level))
5232                  gnus-tmp-prev-subject
5233                  (not (string= gnus-tmp-prev-subject simp-subject)))
5234             (setq new-roots (nconc new-roots (list (car thread)))
5235                   thread-end t
5236                   gnus-tmp-header nil))
5237            ;; If the article lies outside the current limit,
5238            ;; then we do not display it.
5239            ((not (memq number gnus-newsgroup-limit))
5240             (setq gnus-tmp-gathered
5241                   (nconc (mapcar
5242                           (lambda (h) (mail-header-number (car h)))
5243                           (cdar thread))
5244                          gnus-tmp-gathered))
5245             (setq gnus-tmp-new-adopts (if (cdar thread)
5246                                           (append gnus-tmp-new-adopts
5247                                                   (cdar thread))
5248                                         gnus-tmp-new-adopts)
5249                   thread-end t
5250                   gnus-tmp-header nil)
5251             (when (zerop gnus-tmp-level)
5252               (setq gnus-tmp-root-expunged t)))
5253            ;; Perhaps this article is to be marked as read?
5254            ((and gnus-summary-mark-below
5255                  (< (or (cdr (assq number gnus-newsgroup-scored))
5256                         default-score)
5257                     gnus-summary-mark-below)
5258                  ;; Don't touch sparse articles.
5259                  (not (gnus-summary-article-sparse-p number))
5260                  (not (gnus-summary-article-ancient-p number)))
5261             (setq gnus-newsgroup-unreads
5262                   (delq number gnus-newsgroup-unreads))
5263             (if gnus-newsgroup-auto-expire
5264                 (setq gnus-newsgroup-expirable
5265                       (gnus-add-to-sorted-list
5266                        gnus-newsgroup-expirable number))
5267               (push (cons number gnus-low-score-mark)
5268                     gnus-newsgroup-reads))))
5269
5270           (when gnus-tmp-header
5271             ;; We may have an old dummy line to output before this
5272             ;; article.
5273             (when (and gnus-tmp-dummy-line
5274                        (gnus-subject-equal
5275                         gnus-tmp-dummy-line
5276                         (mail-header-subject gnus-tmp-header)))
5277               (gnus-summary-insert-dummy-line
5278                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5279               (setq gnus-tmp-dummy-line nil))
5280
5281             ;; Compute the mark.
5282             (setq gnus-tmp-unread (gnus-article-mark number))
5283
5284             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5285                                   gnus-tmp-header gnus-tmp-level)
5286                   gnus-newsgroup-data)
5287
5288             ;; Actually insert the line.
5289             (setq
5290              gnus-tmp-subject-or-nil
5291              (cond
5292               ((and gnus-thread-ignore-subject
5293                     gnus-tmp-prev-subject
5294                     (not (string= gnus-tmp-prev-subject simp-subject)))
5295                subject)
5296               ((zerop gnus-tmp-level)
5297                (if (and (eq gnus-summary-make-false-root 'empty)
5298                         (memq number gnus-tmp-gathered)
5299                         gnus-tmp-prev-subject
5300                         (string= gnus-tmp-prev-subject simp-subject))
5301                    gnus-summary-same-subject
5302                  subject))
5303               (t gnus-summary-same-subject)))
5304             (if (and (eq gnus-summary-make-false-root 'adopt)
5305                      (= gnus-tmp-level 1)
5306                      (memq number gnus-tmp-gathered))
5307                 (setq gnus-tmp-opening-bracket ?\<
5308                       gnus-tmp-closing-bracket ?\>)
5309               (setq gnus-tmp-opening-bracket ?\[
5310                     gnus-tmp-closing-bracket ?\]))
5311             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5312                 (gnus-make-thread-indent-array
5313                  (max (* 2 (length gnus-thread-indent-array))
5314                       gnus-tmp-level)))
5315             (setq
5316              gnus-tmp-indentation
5317              (aref gnus-thread-indent-array gnus-tmp-level)
5318              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5319              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5320                                 gnus-summary-default-score 0)
5321              gnus-tmp-score-char
5322              (if (or (null gnus-summary-default-score)
5323                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5324                          gnus-summary-zcore-fuzz))
5325                  ?                      ;Whitespace
5326                (if (< gnus-tmp-score gnus-summary-default-score)
5327                    gnus-score-below-mark gnus-score-over-mark))
5328              gnus-tmp-replied
5329              (cond ((memq number gnus-newsgroup-processable)
5330                     gnus-process-mark)
5331                    ((memq number gnus-newsgroup-cached)
5332                     gnus-cached-mark)
5333                    ((memq number gnus-newsgroup-replied)
5334                     gnus-replied-mark)
5335                    ((memq number gnus-newsgroup-forwarded)
5336                     gnus-forwarded-mark)
5337                    ((memq number gnus-newsgroup-saved)
5338                     gnus-saved-mark)
5339                    ((memq number gnus-newsgroup-recent)
5340                     gnus-recent-mark)
5341                    ((memq number gnus-newsgroup-unseen)
5342                     gnus-unseen-mark)
5343                    (t gnus-no-mark))
5344              gnus-tmp-downloaded
5345              (cond ((memq number gnus-newsgroup-undownloaded)
5346                     gnus-undownloaded-mark)
5347                    (gnus-newsgroup-agentized
5348                     gnus-downloaded-mark)
5349                    (t
5350                     gnus-no-mark))
5351              gnus-tmp-from (mail-header-from gnus-tmp-header)
5352              gnus-tmp-name
5353              (cond
5354               ((string-match "<[^>]+> *$" gnus-tmp-from)
5355                (setq beg-match (match-beginning 0))
5356                (or (and (string-match "^\".+\"" gnus-tmp-from)
5357                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5358                    (substring gnus-tmp-from 0 beg-match)))
5359               ((string-match "(.+)" gnus-tmp-from)
5360                (substring gnus-tmp-from
5361                           (1+ (match-beginning 0)) (1- (match-end 0))))
5362               (t gnus-tmp-from))
5363
5364              ;; Do the %B string
5365              gnus-tmp-thread-tree-header-string
5366              (cond
5367               ((not gnus-show-threads) "")
5368               ((zerop gnus-tmp-level)
5369                (cond ((cdar thread)
5370                       (or gnus-sum-thread-tree-root subject))
5371                      (gnus-tmp-new-adopts
5372                       (or gnus-sum-thread-tree-false-root subject))
5373                      (t
5374                       (or gnus-sum-thread-tree-single-indent subject))))
5375               (t
5376                (concat (apply 'concat
5377                               (mapcar (lambda (item)
5378                                         (if (= item 1)
5379                                             gnus-sum-thread-tree-vertical
5380                                           gnus-sum-thread-tree-indent))
5381                                       (cdr (reverse tree-stack))))
5382                        (if (nth 1 thread)
5383                            gnus-sum-thread-tree-leaf-with-other
5384                          gnus-sum-thread-tree-single-leaf)))))
5385             (when (string= gnus-tmp-name "")
5386               (setq gnus-tmp-name gnus-tmp-from))
5387             (unless (numberp gnus-tmp-lines)
5388               (setq gnus-tmp-lines -1))
5389             (if (= gnus-tmp-lines -1)
5390                 (setq gnus-tmp-lines "?")
5391               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5392             (gnus-put-text-property
5393              (point)
5394              (progn (eval gnus-summary-line-format-spec) (point))
5395              'gnus-number number)
5396             (when gnus-visual-p
5397               (forward-line -1)
5398               (gnus-summary-highlight-line)
5399               (when gnus-summary-update-hook
5400                 (gnus-run-hooks 'gnus-summary-update-hook))
5401               (forward-line 1))
5402
5403             (setq gnus-tmp-prev-subject simp-subject)))
5404
5405         (when (nth 1 thread)
5406           (push (list (max 0 gnus-tmp-level)
5407                       (copy-sequence tree-stack)
5408                       (nthcdr 1 thread))
5409                 stack))
5410         (push (if (nth 1 thread) 1 0) tree-stack)
5411         (incf gnus-tmp-level)
5412         (setq threads (if thread-end nil (cdar thread)))
5413         (if gnus-summary-display-while-building
5414             (if building-count
5415                 (progn
5416                   ;; use a set frequency
5417                   (setq building-line-count (1- building-line-count))
5418                   (when (= building-line-count 0)
5419                     (sit-for 0)
5420                     (setq building-line-count
5421                           gnus-summary-display-while-building)))
5422               ;; always
5423               (sit-for 0)))
5424         (unless threads
5425           (setq gnus-tmp-level 0)))))
5426   (gnus-message 7 "Generating summary...done"))
5427
5428 (defun gnus-summary-prepare-unthreaded (headers)
5429   "Generate an unthreaded summary buffer based on HEADERS."
5430   (let (header number mark)
5431
5432     (beginning-of-line)
5433
5434     (while headers
5435       ;; We may have to root out some bad articles...
5436       (when (memq (setq number (mail-header-number
5437                                 (setq header (pop headers))))
5438                   gnus-newsgroup-limit)
5439         ;; Mark article as read when it has a low score.
5440         (when (and gnus-summary-mark-below
5441                    (< (or (cdr (assq number gnus-newsgroup-scored))
5442                           gnus-summary-default-score 0)
5443                       gnus-summary-mark-below)
5444                    (not (gnus-summary-article-ancient-p number)))
5445           (setq gnus-newsgroup-unreads
5446                 (delq number gnus-newsgroup-unreads))
5447           (if gnus-newsgroup-auto-expire
5448               (push number gnus-newsgroup-expirable)
5449             (push (cons number gnus-low-score-mark)
5450                   gnus-newsgroup-reads)))
5451
5452         (setq mark (gnus-article-mark number))
5453         (push (gnus-data-make number mark (1+ (point)) header 0)
5454               gnus-newsgroup-data)
5455         (gnus-summary-insert-line
5456          header 0 number
5457          (memq number gnus-newsgroup-undownloaded)
5458          mark (memq number gnus-newsgroup-replied)
5459          (memq number gnus-newsgroup-expirable)
5460          (mail-header-subject header) nil
5461          (cdr (assq number gnus-newsgroup-scored))
5462          (memq number gnus-newsgroup-processable))))))
5463
5464 (defun gnus-summary-remove-list-identifiers ()
5465   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5466   (let ((regexp (if (consp gnus-list-identifiers)
5467                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5468                   gnus-list-identifiers))
5469         changed subject)
5470     (when regexp
5471       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5472       (dolist (header gnus-newsgroup-headers)
5473         (setq subject (mail-header-subject header)
5474               changed nil)
5475         (while (string-match regexp subject)
5476           (setq subject
5477                 (concat (substring subject 0 (match-beginning 1))
5478                         (substring subject (match-end 0)))
5479                 changed t))
5480         (when changed
5481           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5482             (setq subject
5483                   (concat (substring subject 0 (match-beginning 1))
5484                           (substring subject (match-end 1)))))
5485           (mail-header-set-subject header subject))))))
5486
5487 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5488   "Fetch headers of ARTICLES."
5489   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5490     (gnus-message 5 "Fetching headers for %s..." name)
5491     (prog1
5492         (if (eq 'nov
5493                 (setq gnus-headers-retrieved-by
5494                       (gnus-retrieve-headers
5495                        articles gnus-newsgroup-name
5496                        (or limit
5497                            ;; We might want to fetch old headers, but
5498                            ;; not if there is only 1 article.
5499                            (and (or (and
5500                                      (not (eq gnus-fetch-old-headers 'some))
5501                                      (not (numberp gnus-fetch-old-headers)))
5502                                     (> (length articles) 1))
5503                                 gnus-fetch-old-headers)))))
5504             (gnus-get-newsgroup-headers-xover
5505              articles force-new dependencies gnus-newsgroup-name t)
5506           (gnus-get-newsgroup-headers dependencies force-new))
5507       (gnus-message 5 "Fetching headers for %s...done" name))))
5508
5509 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5510   "Select newsgroup GROUP.
5511 If READ-ALL is non-nil, all articles in the group are selected.
5512 If SELECT-ARTICLES, only select those articles from GROUP."
5513   (let* ((entry (gnus-group-entry group))
5514          ;;!!! Dirty hack; should be removed.
5515          (gnus-summary-ignore-duplicates
5516           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5517               t
5518             gnus-summary-ignore-duplicates))
5519          (info (nth 2 entry))
5520          charset articles fetched-articles cached)
5521
5522     (unless (gnus-check-server
5523              (set (make-local-variable 'gnus-current-select-method)
5524                   (gnus-find-method-for-group group)))
5525       (error "Couldn't open server"))
5526     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5527
5528     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5529         (gnus-activate-group group)     ; Or we can activate it...
5530         (progn                          ; Or we bug out.
5531           (when (equal major-mode 'gnus-summary-mode)
5532             (gnus-kill-buffer (current-buffer)))
5533           (error
5534            "Couldn't activate group %s: %s"
5535            (mm-decode-coding-string group charset)
5536            (mm-decode-coding-string (gnus-status-message group) charset))))
5537
5538     (unless (gnus-request-group group t)
5539       (when (equal major-mode 'gnus-summary-mode)
5540         (gnus-kill-buffer (current-buffer)))
5541       (error "Couldn't request group %s: %s"
5542              (mm-decode-coding-string group charset)
5543              (mm-decode-coding-string (gnus-status-message group) charset)))
5544
5545     (when gnus-agent
5546       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5547
5548       (setq gnus-summary-use-undownloaded-faces
5549             (gnus-agent-find-parameter
5550              group
5551              'agent-enable-undownloaded-faces)))
5552
5553     (setq gnus-newsgroup-name group
5554           gnus-newsgroup-unselected nil
5555           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5556
5557     (let ((display (gnus-group-find-parameter group 'display)))
5558       (setq gnus-newsgroup-display
5559             (cond
5560              ((not (zerop (or (car-safe read-all) 0)))
5561               ;; The user entered the group with C-u SPC/RET, let's show
5562               ;; all articles.
5563               'gnus-not-ignore)
5564              ((eq display 'all)
5565               'gnus-not-ignore)
5566              ((arrayp display)
5567               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5568              ((numberp display)
5569               ;; The following is probably the "correct" solution, but
5570               ;; it makes Gnus fetch all headers and then limit the
5571               ;; articles (which is slow), so instead we hack the
5572               ;; select-articles parameter instead. -- Simon Josefsson
5573               ;; <jas@kth.se>
5574               ;;
5575               ;; (gnus-byte-compile
5576               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5577               ;;                         display)))))
5578               (setq select-articles
5579                     (gnus-uncompress-range
5580                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5581                              (if (> tmp 0)
5582                                  tmp
5583                                1))
5584                            (cdr (gnus-active group)))))
5585               nil)
5586              (t
5587               nil))))
5588
5589     (gnus-summary-setup-default-charset)
5590
5591     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5592     (when (gnus-virtual-group-p group)
5593       (setq cached gnus-newsgroup-cached))
5594
5595     (setq gnus-newsgroup-unreads
5596           (gnus-sorted-ndifference
5597            (gnus-sorted-ndifference gnus-newsgroup-unreads
5598                                     gnus-newsgroup-marked)
5599            gnus-newsgroup-dormant))
5600
5601     (setq gnus-newsgroup-processable nil)
5602
5603     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5604
5605     ;; Adjust and set lists of article marks.
5606     (when info
5607       (gnus-adjust-marked-articles info))
5608     (if (setq articles select-articles)
5609         (setq gnus-newsgroup-unselected
5610               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5611       (setq articles (gnus-articles-to-read group read-all)))
5612
5613     (cond
5614      ((null articles)
5615       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5616       'quit)
5617      ((eq articles 0) nil)
5618      (t
5619       ;; Init the dependencies hash table.
5620       (setq gnus-newsgroup-dependencies
5621             (gnus-make-hashtable (length articles)))
5622       (gnus-set-global-variables)
5623       ;; Retrieve the headers and read them in.
5624
5625       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5626
5627       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5628       (when cached
5629         (setq gnus-newsgroup-cached cached))
5630
5631       ;; Suppress duplicates?
5632       (when gnus-suppress-duplicates
5633         (gnus-dup-suppress-articles))
5634
5635       ;; Set the initial limit.
5636       (setq gnus-newsgroup-limit (copy-sequence articles))
5637       ;; Remove canceled articles from the list of unread articles.
5638       (setq fetched-articles
5639             (mapcar (lambda (headers) (mail-header-number headers))
5640                     gnus-newsgroup-headers))
5641       (setq gnus-newsgroup-articles fetched-articles)
5642       (setq gnus-newsgroup-unreads
5643             (gnus-sorted-nintersection
5644              gnus-newsgroup-unreads fetched-articles))
5645       (gnus-compute-unseen-list)
5646
5647       ;; Removed marked articles that do not exist.
5648       (gnus-update-missing-marks
5649        (gnus-sorted-difference articles fetched-articles))
5650       ;; We might want to build some more threads first.
5651       (when (and gnus-fetch-old-headers
5652                  (eq gnus-headers-retrieved-by 'nov))
5653         (if (eq gnus-fetch-old-headers 'invisible)
5654             (gnus-build-all-threads)
5655           (gnus-build-old-threads)))
5656       ;; Let the Gnus agent mark articles as read.
5657       (when gnus-agent
5658         (gnus-agent-get-undownloaded-list))
5659       ;; Remove list identifiers from subject
5660       (when gnus-list-identifiers
5661         (gnus-summary-remove-list-identifiers))
5662       ;; Check whether auto-expire is to be done in this group.
5663       (setq gnus-newsgroup-auto-expire
5664             (gnus-group-auto-expirable-p group))
5665       ;; Set up the article buffer now, if necessary.
5666       (unless (and gnus-single-article-buffer
5667                    (equal gnus-article-buffer "*Article*"))
5668         (gnus-article-setup-buffer))
5669       ;; First and last article in this newsgroup.
5670       (when gnus-newsgroup-headers
5671         (setq gnus-newsgroup-begin
5672               (mail-header-number (car gnus-newsgroup-headers))
5673               gnus-newsgroup-end
5674               (mail-header-number
5675                (gnus-last-element gnus-newsgroup-headers))))
5676       ;; GROUP is successfully selected.
5677       (or gnus-newsgroup-headers t)))))
5678
5679 (defun gnus-compute-unseen-list ()
5680   ;; The `seen' marks are treated specially.
5681   (if (not gnus-newsgroup-seen)
5682       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5683     (setq gnus-newsgroup-unseen
5684           (gnus-inverse-list-range-intersection
5685            gnus-newsgroup-articles gnus-newsgroup-seen))))
5686
5687 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5688
5689 (defun gnus-summary-display-make-predicate (display)
5690   (require 'gnus-agent)
5691   (when (= (length display) 1)
5692     (setq display (car display)))
5693   (unless gnus-summary-display-cache
5694     (dolist (elem (append '((unread . unread)
5695                             (read . read)
5696                             (unseen . unseen))
5697                           gnus-article-mark-lists))
5698       (push (cons (cdr elem)
5699                   (gnus-byte-compile    ;Why bother?
5700                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5701             gnus-summary-display-cache)))
5702   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5703         (gnus-category-predicate-cache gnus-summary-display-cache))
5704     (gnus-get-predicate display)))
5705
5706 ;; Uses the dynamically bound `gnus-number' variable.
5707 (defvar gnus-number)
5708 (defun gnus-article-marked-p (type &optional article)
5709   (let ((article (or article gnus-number)))
5710     (cond
5711      ((eq type 'tick)
5712       (memq article gnus-newsgroup-marked))
5713      ((eq type 'spam)
5714       (memq article gnus-newsgroup-spam-marked))
5715      ((eq type 'unsend)
5716       (memq article gnus-newsgroup-unsendable))
5717      ((eq type 'undownload)
5718       (memq article gnus-newsgroup-undownloaded))
5719      ((eq type 'download)
5720       (memq article gnus-newsgroup-downloadable))
5721      ((eq type 'unread)
5722       (memq article gnus-newsgroup-unreads))
5723      ((eq type 'read)
5724       (memq article gnus-newsgroup-reads))
5725      ((eq type 'dormant)
5726       (memq article gnus-newsgroup-dormant) )
5727      ((eq type 'expire)
5728       (memq article gnus-newsgroup-expirable))
5729      ((eq type 'reply)
5730       (memq article gnus-newsgroup-replied))
5731      ((eq type 'killed)
5732       (memq article gnus-newsgroup-killed))
5733      ((eq type 'bookmark)
5734       (assq article gnus-newsgroup-bookmarks))
5735      ((eq type 'score)
5736       (assq article gnus-newsgroup-scored))
5737      ((eq type 'save)
5738       (memq article gnus-newsgroup-saved))
5739      ((eq type 'cache)
5740       (memq article gnus-newsgroup-cached))
5741      ((eq type 'forward)
5742       (memq article gnus-newsgroup-forwarded))
5743      ((eq type 'seen)
5744       (not (memq article gnus-newsgroup-unseen)))
5745      ((eq type 'recent)
5746       (memq article gnus-newsgroup-recent))
5747      (t t))))
5748
5749 (defun gnus-articles-to-read (group &optional read-all)
5750   "Find out what articles the user wants to read."
5751   (let* ((articles
5752           ;; Select all articles if `read-all' is non-nil, or if there
5753           ;; are no unread articles.
5754           (if (or read-all
5755                   (and (zerop (length gnus-newsgroup-marked))
5756                        (zerop (length gnus-newsgroup-unreads)))
5757                   ;; Fetch all if the predicate is non-nil.
5758                   gnus-newsgroup-display)
5759               ;; We want to select the headers for all the articles in
5760               ;; the group, so we select either all the active
5761               ;; articles in the group, or (if that's nil), the
5762               ;; articles in the cache.
5763               (or
5764                (if gnus-newsgroup-maximum-articles
5765                    (let ((active (gnus-active group)))
5766                      (gnus-uncompress-range
5767                       (cons (max (car active)
5768                                  (- (cdr active)
5769                                     gnus-newsgroup-maximum-articles
5770                                     -1))
5771                             (cdr active))))
5772                  (gnus-uncompress-range (gnus-active group)))
5773                (gnus-cache-articles-in-group group))
5774             ;; Select only the "normal" subset of articles.
5775             (gnus-sorted-nunion
5776              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5777              gnus-newsgroup-unreads)))
5778          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5779          (scored (length scored-list))
5780          (number (length articles))
5781          (marked (+ (length gnus-newsgroup-marked)
5782                     (length gnus-newsgroup-dormant)))
5783          (select
5784           (cond
5785            ((numberp read-all)
5786             read-all)
5787            ((numberp gnus-newsgroup-display)
5788             gnus-newsgroup-display)
5789            (t
5790             (condition-case ()
5791                 (cond
5792                  ((and (or (<= scored marked) (= scored number))
5793                        (numberp gnus-large-newsgroup)
5794                        (> number gnus-large-newsgroup))
5795                   (let* ((cursor-in-echo-area nil)
5796                          (initial (gnus-parameter-large-newsgroup-initial
5797                                    gnus-newsgroup-name))
5798                          (input
5799                           (read-string
5800                            (format
5801                             "How many articles from %s (%s %d): "
5802                             (gnus-group-decoded-name gnus-newsgroup-name)
5803                             (if initial "max" "default")
5804                             number)
5805                            (if initial
5806                                (cons (number-to-string initial)
5807                                      0)))))
5808                     (if (string-match "^[ \t]*$" input) number input)))
5809                  ((and (> scored marked) (< scored number)
5810                        (> (- scored number) 20))
5811                   (let ((input
5812                          (read-string
5813                           (format "%s %s (%d scored, %d total): "
5814                                   "How many articles from"
5815                                   (gnus-group-decoded-name group)
5816                                   scored number))))
5817                     (if (string-match "^[ \t]*$" input)
5818                         number input)))
5819                  (t number))
5820               (quit
5821                (message "Quit getting the articles to read")
5822                nil))))))
5823     (setq select (if (stringp select) (string-to-number select) select))
5824     (if (or (null select) (zerop select))
5825         select
5826       (if (and (not (zerop scored)) (<= (abs select) scored))
5827           (progn
5828             (setq articles (sort scored-list '<))
5829             (setq number (length articles)))
5830         (setq articles (copy-sequence articles)))
5831
5832       (when (< (abs select) number)
5833         (if (< select 0)
5834             ;; Select the N oldest articles.
5835             (setcdr (nthcdr (1- (abs select)) articles) nil)
5836           ;; Select the N most recent articles.
5837           (setq articles (nthcdr (- number select) articles))))
5838       (setq gnus-newsgroup-unselected
5839             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5840       (when gnus-alter-articles-to-read-function
5841         (setq articles
5842               (sort
5843                (funcall gnus-alter-articles-to-read-function
5844                         gnus-newsgroup-name articles)
5845                '<)))
5846       articles)))
5847
5848 (defun gnus-killed-articles (killed articles)
5849   (let (out)
5850     (while articles
5851       (when (inline (gnus-member-of-range (car articles) killed))
5852         (push (car articles) out))
5853       (setq articles (cdr articles)))
5854     out))
5855
5856 (defun gnus-uncompress-marks (marks)
5857   "Uncompress the mark ranges in MARKS."
5858   (let ((uncompressed '(score bookmark))
5859         out)
5860     (while marks
5861       (if (memq (caar marks) uncompressed)
5862           (push (car marks) out)
5863         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5864       (setq marks (cdr marks)))
5865     out))
5866
5867 (defun gnus-article-mark-to-type (mark)
5868   "Return the type of MARK."
5869   (or (cadr (assq mark gnus-article-special-mark-lists))
5870       'list))
5871
5872 (defun gnus-article-unpropagatable-p (mark)
5873   "Return whether MARK should be propagated to back end."
5874   (memq mark gnus-article-unpropagated-mark-lists))
5875
5876 (defun gnus-adjust-marked-articles (info)
5877   "Set all article lists and remove all marks that are no longer valid."
5878   (let* ((marked-lists (gnus-info-marks info))
5879          (active (gnus-active (gnus-info-group info)))
5880          (min (car active))
5881          (max (cdr active))
5882          (types gnus-article-mark-lists)
5883          marks var articles article mark mark-type
5884          bgn end)
5885     ;; Hack to avoid adjusting marks for imap.
5886     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5887               'nnimap)
5888       (setq min 1))
5889
5890     (dolist (marks marked-lists)
5891       (setq mark (car marks)
5892             mark-type (gnus-article-mark-to-type mark)
5893             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5894
5895       ;; We set the variable according to the type of the marks list,
5896       ;; and then adjust the marks to a subset of the active articles.
5897       (cond
5898        ;; Adjust "simple" lists - compressed yet unsorted
5899        ((eq mark-type 'list)
5900         ;; Simultaneously uncompress and clip to active range
5901         ;; See gnus-uncompress-range for a description of possible marks
5902         (let (l lh)
5903           (if (not (cadr marks))
5904               (set var nil)
5905             (setq articles (if (numberp (cddr marks))
5906                                (list (cdr marks))
5907                              (cdr marks))
5908                   lh (cons nil nil)
5909                   l lh)
5910
5911             (while (setq article (pop articles))
5912               (cond ((consp article)
5913                      (setq bgn (max (car article) min)
5914                            end (min (cdr article) max))
5915                      (while (<= bgn end)
5916                        (setq l (setcdr l (cons bgn nil))
5917                              bgn (1+ bgn))))
5918                     ((and (<= min article)
5919                           (>= max article))
5920                      (setq l (setcdr l (cons article nil))))))
5921             (set var (cdr lh)))))
5922        ;; Adjust assocs.
5923        ((eq mark-type 'tuple)
5924         (set var (setq articles (cdr marks)))
5925         (when (not (listp (cdr (symbol-value var))))
5926           (set var (list (symbol-value var))))
5927         (when (not (listp (cdr articles)))
5928           (setq articles (list articles)))
5929         (while articles
5930           (when (or (not (consp (setq article (pop articles))))
5931                     (< (car article) min)
5932                     (> (car article) max))
5933             (set var (delq article (symbol-value var))))))
5934        ;; Adjust ranges (sloppily).
5935        ((eq mark-type 'range)
5936         (cond
5937          ((eq mark 'seen)
5938           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5939           ;; It should be (seen (NUM1 . NUM2)).
5940           (when (numberp (cddr marks))
5941             (setcdr marks (list (cdr marks))))
5942           (setq articles (cdr marks))
5943           (while (and articles
5944                       (or (and (consp (car articles))
5945                                (> min (cdar articles)))
5946                           (and (numberp (car articles))
5947                                (> min (car articles)))))
5948             (pop articles))
5949           (set var articles))))))))
5950
5951 (defun gnus-update-missing-marks (missing)
5952   "Go through the list of MISSING articles and remove them from the mark lists."
5953   (when missing
5954     (let (var m)
5955       ;; Go through all types.
5956       (dolist (elem gnus-article-mark-lists)
5957         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5958           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5959           (when (symbol-value var)
5960             ;; This list has articles.  So we delete all missing
5961             ;; articles from it.
5962             (setq m missing)
5963             (while m
5964               (set var (delq (pop m) (symbol-value var))))))))))
5965
5966 (defun gnus-update-marks ()
5967   "Enter the various lists of marked articles into the newsgroup info list."
5968   (let ((types gnus-article-mark-lists)
5969         (info (gnus-get-info gnus-newsgroup-name))
5970         type list newmarked symbol delta-marks)
5971     (when info
5972       ;; Add all marks lists to the list of marks lists.
5973       (while (setq type (pop types))
5974         (setq list (symbol-value
5975                     (setq symbol
5976                           (intern (format "gnus-newsgroup-%s" (car type))))))
5977
5978         (when list
5979           ;; Get rid of the entries of the articles that have the
5980           ;; default score.
5981           (when (and (eq (cdr type) 'score)
5982                      gnus-save-score
5983                      list)
5984             (let* ((arts list)
5985                    (prev (cons nil list))
5986                    (all prev))
5987               (while arts
5988                 (if (or (not (consp (car arts)))
5989                         (= (cdar arts) gnus-summary-default-score))
5990                     (setcdr prev (cdr arts))
5991                   (setq prev arts))
5992                 (setq arts (cdr arts)))
5993               (setq list (cdr all)))))
5994
5995         (when (eq (cdr type) 'seen)
5996           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5997
5998         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5999           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6000
6001         (when (and (gnus-check-backend-function
6002                     'request-set-mark gnus-newsgroup-name)
6003                    (not (gnus-article-unpropagatable-p (cdr type))))
6004           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6005                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
6006                  (add (gnus-remove-from-range
6007                        (gnus-copy-sequence list) old)))
6008             (when add
6009               (push (list add 'add (list (cdr type))) delta-marks))
6010             (when del
6011               ;; Don't delete marks from outside the active range.  This
6012               ;; shouldn't happen, but is a sanity check.
6013               (setq del (gnus-sorted-range-intersection
6014                          (gnus-active gnus-newsgroup-name) del))
6015               (push (list del 'del (list (cdr type))) delta-marks))))
6016
6017         (when list
6018           (push (cons (cdr type) list) newmarked)))
6019
6020       (when delta-marks
6021         (unless (gnus-check-group gnus-newsgroup-name)
6022           (error "Can't open server for %s" gnus-newsgroup-name))
6023         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6024
6025       ;; Enter these new marks into the info of the group.
6026       (if (nthcdr 3 info)
6027           (setcar (nthcdr 3 info) newmarked)
6028         ;; Add the marks lists to the end of the info.
6029         (when newmarked
6030           (setcdr (nthcdr 2 info) (list newmarked))))
6031
6032       ;; Cut off the end of the info if there's nothing else there.
6033       (let ((i 5))
6034         (while (and (> i 2)
6035                     (not (nth i info)))
6036           (when (nthcdr (decf i) info)
6037             (setcdr (nthcdr i info) nil)))))))
6038
6039 (defun gnus-set-mode-line (where)
6040   "Set the mode line of the article or summary buffers.
6041 If WHERE is `summary', the summary mode line format will be used."
6042   ;; Is this mode line one we keep updated?
6043   (when (and (memq where gnus-updated-mode-lines)
6044              (symbol-value
6045               (intern (format "gnus-%s-mode-line-format-spec" where))))
6046     (let (mode-string)
6047       ;; We evaluate this in the summary buffer since these
6048       ;; variables are buffer-local to that buffer.
6049       (with-current-buffer gnus-summary-buffer
6050         ;; We bind all these variables that are used in the `eval' form
6051         ;; below.
6052         (let* ((mformat (symbol-value
6053                          (intern
6054                           (format "gnus-%s-mode-line-format-spec" where))))
6055                (gnus-tmp-group-name (gnus-mode-string-quote
6056                                      (gnus-group-decoded-name
6057                                       gnus-newsgroup-name)))
6058                (gnus-tmp-article-number (or gnus-current-article 0))
6059                (gnus-tmp-unread gnus-newsgroup-unreads)
6060                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6061                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6062                (gnus-tmp-unread-and-unselected
6063                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6064                             (zerop gnus-tmp-unselected))
6065                        "")
6066                       ((zerop gnus-tmp-unselected)
6067                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6068                       (t (format "{%d(+%d) more}"
6069                                  gnus-tmp-unread-and-unticked
6070                                  gnus-tmp-unselected))))
6071                (gnus-tmp-subject
6072                 (if (and gnus-current-headers
6073                          (vectorp gnus-current-headers))
6074                     (gnus-mode-string-quote
6075                      (mail-header-subject gnus-current-headers))
6076                   ""))
6077                bufname-length max-len
6078                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6079           (setq mode-string (eval mformat))
6080           (setq bufname-length (if (string-match "%b" mode-string)
6081                                    (- (length
6082                                        (buffer-name
6083                                         (if (eq where 'summary)
6084                                             nil
6085                                           (get-buffer gnus-article-buffer))))
6086                                       2)
6087                                  0))
6088           (setq max-len (max 4 (if gnus-mode-non-string-length
6089                                    (- (window-width)
6090                                       gnus-mode-non-string-length
6091                                       bufname-length)
6092                                  (length mode-string))))
6093           ;; We might have to chop a bit of the string off...
6094           (when (> (length mode-string) max-len)
6095             (setq mode-string
6096                   (concat (truncate-string-to-width mode-string (- max-len 3))
6097                           "...")))))
6098       ;; Update the mode line.
6099       (setq mode-line-buffer-identification
6100             (gnus-mode-line-buffer-identification (list mode-string)))
6101       (set-buffer-modified-p t))))
6102
6103 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6104   "Go through the HEADERS list and add all Xrefs to a hash table.
6105 The resulting hash table is returned, or nil if no Xrefs were found."
6106   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6107          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6108          (xref-hashtb (gnus-make-hashtable))
6109          start group entry number xrefs header)
6110     (while headers
6111       (setq header (pop headers))
6112       (when (and (setq xrefs (mail-header-xref header))
6113                  (not (memq (setq number (mail-header-number header))
6114                             unreads)))
6115         (setq start 0)
6116         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6117           (setq start (match-end 0))
6118           (setq group (if prefix
6119                           (concat prefix (substring xrefs (match-beginning 1)
6120                                                     (match-end 1)))
6121                         (substring xrefs (match-beginning 1) (match-end 1))))
6122           (setq number
6123                 (string-to-number (substring xrefs (match-beginning 2)
6124                                           (match-end 2))))
6125           (if (setq entry (gnus-gethash group xref-hashtb))
6126               (setcdr entry (cons number (cdr entry)))
6127             (gnus-sethash group (cons number nil) xref-hashtb)))))
6128     (and start xref-hashtb)))
6129
6130 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6131   "Look through all the headers and mark the Xrefs as read."
6132   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6133         name info xref-hashtb idlist method nth4)
6134     (with-current-buffer gnus-group-buffer
6135       (when (setq xref-hashtb
6136                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6137         (mapatoms
6138          (lambda (group)
6139            (unless (string= from-newsgroup (setq name (symbol-name group)))
6140              (setq idlist (symbol-value group))
6141              ;; Dead groups are not updated.
6142              (and (prog1
6143                       (setq info (gnus-get-info name))
6144                     (when (stringp (setq nth4 (gnus-info-method info)))
6145                       (setq nth4 (gnus-server-to-method nth4))))
6146                   ;; Only do the xrefs if the group has the same
6147                   ;; select method as the group we have just read.
6148                   (or (gnus-methods-equal-p
6149                        nth4 (gnus-find-method-for-group from-newsgroup))
6150                       virtual
6151                       (equal nth4 (setq method (gnus-find-method-for-group
6152                                                 from-newsgroup)))
6153                       (and (equal (car nth4) (car method))
6154                            (equal (nth 1 nth4) (nth 1 method))))
6155                   gnus-use-cross-reference
6156                   (or (not (eq gnus-use-cross-reference t))
6157                       virtual
6158                       ;; Only do cross-references on subscribed
6159                       ;; groups, if that is what is wanted.
6160                       (<= (gnus-info-level info) gnus-level-subscribed))
6161                   (gnus-group-make-articles-read name idlist))))
6162          xref-hashtb)))))
6163
6164 (defun gnus-compute-read-articles (group articles)
6165   (let* ((entry (gnus-group-entry group))
6166          (info (nth 2 entry))
6167          (active (gnus-active group))
6168          ninfo)
6169     (when entry
6170       ;; First peel off all invalid article numbers.
6171       (when active
6172         (let ((ids articles)
6173               id first)
6174           (while (setq id (pop ids))
6175             (when (and first (> id (cdr active)))
6176               ;; We'll end up in this situation in one particular
6177               ;; obscure situation.  If you re-scan a group and get
6178               ;; a new article that is cross-posted to a different
6179               ;; group that has not been re-scanned, you might get
6180               ;; crossposted article that has a higher number than
6181               ;; Gnus believes possible.  So we re-activate this
6182               ;; group as well.  This might mean doing the
6183               ;; crossposting thingy will *increase* the number
6184               ;; of articles in some groups.  Tsk, tsk.
6185               (setq active (or (gnus-activate-group group) active)))
6186             (when (or (> id (cdr active))
6187                       (< id (car active)))
6188               (setq articles (delq id articles))))))
6189       ;; If the read list is nil, we init it.
6190       (if (and active
6191                (null (gnus-info-read info))
6192                (> (car active) 1))
6193           (setq ninfo (cons 1 (1- (car active))))
6194         (setq ninfo (gnus-info-read info)))
6195       ;; Then we add the read articles to the range.
6196       (gnus-add-to-range
6197        ninfo (setq articles (sort articles '<))))))
6198
6199 (defun gnus-group-make-articles-read (group articles)
6200   "Update the info of GROUP to say that ARTICLES are read."
6201   (let* ((num 0)
6202          (entry (gnus-group-entry group))
6203          (info (nth 2 entry))
6204          (active (gnus-active group))
6205          range)
6206     (if (not entry)
6207         ;; Group that Gnus doesn't know exists, but still allow the
6208         ;; backend to set marks.
6209         (gnus-request-set-mark
6210          group (list (list (gnus-compress-sequence (sort articles #'<))
6211                            'add '(read))))
6212       ;; Normal, subscribed groups.
6213       (setq range (gnus-compute-read-articles group articles))
6214       (with-current-buffer gnus-group-buffer
6215         (gnus-undo-register
6216           `(progn
6217              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6218              (gnus-info-set-read ',info ',(gnus-info-read info))
6219              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6220              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6221              (gnus-group-update-group ,group t))))
6222       ;; Add the read articles to the range.
6223       (gnus-info-set-read info range)
6224       (gnus-request-set-mark group (list (list range 'add '(read))))
6225       ;; Then we have to re-compute how many unread
6226       ;; articles there are in this group.
6227       (when active
6228         (cond
6229          ((not range)
6230           (setq num (- (1+ (cdr active)) (car active))))
6231          ((not (listp (cdr range)))
6232           (setq num (- (cdr active) (- (1+ (cdr range))
6233                                        (car range)))))
6234          (t
6235           (while range
6236             (if (numberp (car range))
6237                 (setq num (1+ num))
6238               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6239             (setq range (cdr range)))
6240           (setq num (- (cdr active) num))))
6241         ;; Update the number of unread articles.
6242         (setcar entry num)
6243         ;; Update the group buffer.
6244         (unless (gnus-ephemeral-group-p group)
6245           (gnus-group-update-group group t))))))
6246
6247 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6248   (let ((cur nntp-server-buffer)
6249         (dependencies
6250          (or dependencies
6251              (with-current-buffer gnus-summary-buffer
6252                gnus-newsgroup-dependencies)))
6253         headers id end ref number
6254         (mail-parse-charset gnus-newsgroup-charset)
6255         (mail-parse-ignored-charsets
6256          (save-current-buffer (condition-case nil
6257                                   (set-buffer gnus-summary-buffer)
6258                                 (error))
6259                               gnus-newsgroup-ignored-charsets)))
6260     (with-current-buffer nntp-server-buffer
6261       ;; Translate all TAB characters into SPACE characters.
6262       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6263       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6264       (ietf-drums-unfold-fws)
6265       (gnus-run-hooks 'gnus-parse-headers-hook)
6266       (let ((case-fold-search t)
6267             in-reply-to header p lines chars)
6268         (goto-char (point-min))
6269         ;; Search to the beginning of the next header.  Error messages
6270         ;; do not begin with 2 or 3.
6271         (while (re-search-forward "^[23][0-9]+ " nil t)
6272           (setq id nil
6273                 ref nil)
6274           ;; This implementation of this function, with nine
6275           ;; search-forwards instead of the one re-search-forward and
6276           ;; a case (which basically was the old function) is actually
6277           ;; about twice as fast, even though it looks messier.  You
6278           ;; can't have everything, I guess.  Speed and elegance
6279           ;; doesn't always go hand in hand.
6280           (setq
6281            header
6282            (vector
6283             ;; Number.
6284             (prog1
6285                 (setq number (read cur))
6286               (end-of-line)
6287               (setq p (point))
6288               (narrow-to-region (point)
6289                                 (or (and (search-forward "\n.\n" nil t)
6290                                          (- (point) 2))
6291                                     (point))))
6292             ;; Subject.
6293             (progn
6294               (goto-char p)
6295               (if (search-forward "\nsubject:" nil t)
6296                   (funcall gnus-decode-encoded-word-function
6297                            (nnheader-header-value))
6298                 "(none)"))
6299             ;; From.
6300             (progn
6301               (goto-char p)
6302               (if (search-forward "\nfrom:" nil t)
6303                   (funcall gnus-decode-encoded-address-function
6304                            (nnheader-header-value))
6305                 "(nobody)"))
6306             ;; Date.
6307             (progn
6308               (goto-char p)
6309               (if (search-forward "\ndate:" nil t)
6310                   (nnheader-header-value) ""))
6311             ;; Message-ID.
6312             (progn
6313               (goto-char p)
6314               (setq id (if (re-search-forward
6315                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6316                            ;; We do it this way to make sure the Message-ID
6317                            ;; is (somewhat) syntactically valid.
6318                            (buffer-substring (match-beginning 1)
6319                                              (match-end 1))
6320                          ;; If there was no message-id, we just fake one
6321                          ;; to make subsequent routines simpler.
6322                          (nnheader-generate-fake-message-id number))))
6323             ;; References.
6324             (progn
6325               (goto-char p)
6326               (if (search-forward "\nreferences:" nil t)
6327                   (progn
6328                     (setq end (point))
6329                     (prog1
6330                         (nnheader-header-value)
6331                       (setq ref
6332                             (buffer-substring
6333                              (progn
6334                                (end-of-line)
6335                                (search-backward ">" end t)
6336                                (1+ (point)))
6337                              (progn
6338                                (search-backward "<" end t)
6339                                (point))))))
6340                 ;; Get the references from the in-reply-to header if there
6341                 ;; were no references and the in-reply-to header looks
6342                 ;; promising.
6343                 (if (and (search-forward "\nin-reply-to:" nil t)
6344                          (setq in-reply-to (nnheader-header-value))
6345                          (string-match "<[^>]+>" in-reply-to))
6346                     (let (ref2)
6347                       (setq ref (substring in-reply-to (match-beginning 0)
6348                                            (match-end 0)))
6349                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6350                         (setq ref2 (substring in-reply-to (match-beginning 0)
6351                                               (match-end 0)))
6352                         (when (> (length ref2) (length ref))
6353                           (setq ref ref2)))
6354                       ref)
6355                   (setq ref nil))))
6356             ;; Chars.
6357             (progn
6358               (goto-char p)
6359               (if (search-forward "\nchars: " nil t)
6360                   (if (numberp (setq chars (ignore-errors (read cur))))
6361                       chars -1)
6362                 -1))
6363             ;; Lines.
6364             (progn
6365               (goto-char p)
6366               (if (search-forward "\nlines: " nil t)
6367                   (if (numberp (setq lines (ignore-errors (read cur))))
6368                       lines -1)
6369                 -1))
6370             ;; Xref.
6371             (progn
6372               (goto-char p)
6373               (and (search-forward "\nxref:" nil t)
6374                    (nnheader-header-value)))
6375             ;; Extra.
6376             (when gnus-extra-headers
6377               (let ((extra gnus-extra-headers)
6378                     out)
6379                 (while extra
6380                   (goto-char p)
6381                   (when (search-forward
6382                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6383                     (push (cons (car extra) (nnheader-header-value))
6384                           out))
6385                   (pop extra))
6386                 out))))
6387           (when (equal id ref)
6388             (setq ref nil))
6389
6390           (when gnus-alter-header-function
6391             (funcall gnus-alter-header-function header)
6392             (setq id (mail-header-id header)
6393                   ref (gnus-parent-id (mail-header-references header))))
6394
6395           (when (setq header
6396                       (gnus-dependencies-add-header
6397                        header dependencies force-new))
6398             (push header headers))
6399           (goto-char (point-max))
6400           (widen))
6401         (nreverse headers)))))
6402
6403 ;; Goes through the xover lines and returns a list of vectors
6404 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6405                                                   force-new dependencies
6406                                                   group also-fetch-heads)
6407   "Parse the news overview data in the server buffer.
6408 Return a list of headers that match SEQUENCE (see
6409 `nntp-retrieve-headers')."
6410   ;; Get the Xref when the users reads the articles since most/some
6411   ;; NNTP servers do not include Xrefs when using XOVER.
6412   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6413   (let ((mail-parse-charset gnus-newsgroup-charset)
6414         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6415         (cur nntp-server-buffer)
6416         (dependencies (or dependencies gnus-newsgroup-dependencies))
6417         (allp (cond
6418                ((eq gnus-read-all-available-headers t)
6419                 t)
6420                ((and (stringp gnus-read-all-available-headers)
6421                      group)
6422                 (string-match gnus-read-all-available-headers group))
6423                (t
6424                 nil)))
6425         number headers header)
6426     (with-current-buffer nntp-server-buffer
6427       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6428       ;; Allow the user to mangle the headers before parsing them.
6429       (gnus-run-hooks 'gnus-parse-headers-hook)
6430       (goto-char (point-min))
6431       (gnus-parse-without-error
6432         (while (and (or sequence allp)
6433                     (not (eobp)))
6434           (setq number (read cur))
6435           (when (not allp)
6436             (while (and sequence
6437                         (< (car sequence) number))
6438               (setq sequence (cdr sequence))))
6439           (when (and (or allp
6440                          (and sequence
6441                               (eq number (car sequence))))
6442                      (progn
6443                        (setq sequence (cdr sequence))
6444                        (setq header (inline
6445                                       (gnus-nov-parse-line
6446                                        number dependencies force-new)))))
6447             (push header headers))
6448           (forward-line 1)))
6449       ;; A common bug in inn is that if you have posted an article and
6450       ;; then retrieves the active file, it will answer correctly --
6451       ;; the new article is included.  However, a NOV entry for the
6452       ;; article may not have been generated yet, so this may fail.
6453       ;; We work around this problem by retrieving the last few
6454       ;; headers using HEAD.
6455       (if (or (not also-fetch-heads)
6456               (not sequence))
6457           ;; We (probably) got all the headers.
6458           (nreverse headers)
6459         (let ((gnus-nov-is-evil t))
6460           (nconc
6461            (nreverse headers)
6462            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6463              (gnus-get-newsgroup-headers))))))))
6464
6465 (defun gnus-article-get-xrefs ()
6466   "Fill in the Xref value in `gnus-current-headers', if necessary.
6467 This is meant to be called in `gnus-article-internal-prepare-hook'."
6468   (let ((headers (with-current-buffer gnus-summary-buffer
6469                    gnus-current-headers)))
6470     (or (not gnus-use-cross-reference)
6471         (not headers)
6472         (and (mail-header-xref headers)
6473              (not (string= (mail-header-xref headers) "")))
6474         (let ((case-fold-search t)
6475               xref)
6476           (save-restriction
6477             (nnheader-narrow-to-headers)
6478             (goto-char (point-min))
6479             (when (or (and (not (eobp))
6480                            (eq (downcase (char-after)) ?x)
6481                            (looking-at "Xref:"))
6482                       (search-forward "\nXref:" nil t))
6483               (goto-char (1+ (match-end 0)))
6484               (setq xref (buffer-substring (point) (point-at-eol)))
6485               (mail-header-set-xref headers xref)))))))
6486
6487 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6488   "Find article ID and insert the summary line for that article.
6489 OLD-HEADER can either be a header or a line number to insert
6490 the subject line on."
6491   (let* ((line (and (numberp old-header) old-header))
6492          (old-header (and (vectorp old-header) old-header))
6493          (header (cond ((and old-header use-old-header)
6494                         old-header)
6495                        ((and (numberp id)
6496                              (gnus-number-to-header id))
6497                         (gnus-number-to-header id))
6498                        (t
6499                         (gnus-read-header id))))
6500          (number (and (numberp id) id))
6501          d)
6502     (when header
6503       ;; Rebuild the thread that this article is part of and go to the
6504       ;; article we have fetched.
6505       (when (and (not gnus-show-threads)
6506                  old-header)
6507         (when (and number
6508                    (setq d (gnus-data-find (mail-header-number old-header))))
6509           (goto-char (gnus-data-pos d))
6510           (gnus-data-remove
6511            number
6512            (- (point-at-bol)
6513               (prog1
6514                   (1+ (point-at-eol))
6515                 (gnus-delete-line))))))
6516       ;; Remove list identifiers from subject.
6517       (when gnus-list-identifiers
6518         (let ((gnus-newsgroup-headers (list header)))
6519           (gnus-summary-remove-list-identifiers)))
6520       (when old-header
6521         (mail-header-set-number header (mail-header-number old-header)))
6522       (setq gnus-newsgroup-sparse
6523             (delq (setq number (mail-header-number header))
6524                   gnus-newsgroup-sparse))
6525       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6526       (push number gnus-newsgroup-limit)
6527       (gnus-rebuild-thread (mail-header-id header) line)
6528       (gnus-summary-goto-subject number nil t))
6529     (when (and (numberp number)
6530                (> number 0))
6531       ;; We have to update the boundaries even if we can't fetch the
6532       ;; article if ID is a number -- so that the next `P' or `N'
6533       ;; command will fetch the previous (or next) article even
6534       ;; if the one we tried to fetch this time has been canceled.
6535       (when (> number gnus-newsgroup-end)
6536         (setq gnus-newsgroup-end number))
6537       (when (< number gnus-newsgroup-begin)
6538         (setq gnus-newsgroup-begin number))
6539       (setq gnus-newsgroup-unselected
6540             (delq number gnus-newsgroup-unselected)))
6541     ;; Report back a success?
6542     (and header (mail-header-number header))))
6543
6544 ;;; Process/prefix in the summary buffer
6545
6546 (defun gnus-summary-work-articles (n)
6547   "Return a list of articles to be worked upon.
6548 The prefix argument, the list of process marked articles, and the
6549 current article will be taken into consideration."
6550   (with-current-buffer gnus-summary-buffer
6551     (cond
6552      (n
6553       ;; A numerical prefix has been given.
6554       (setq n (prefix-numeric-value n))
6555       (let ((backward (< n 0))
6556             (n (abs (prefix-numeric-value n)))
6557             articles article)
6558         (save-excursion
6559           (while
6560               (and (> n 0)
6561                    (push (setq article (gnus-summary-article-number))
6562                          articles)
6563                    (if backward
6564                        (gnus-summary-find-prev nil article)
6565                      (gnus-summary-find-next nil article)))
6566             (decf n)))
6567         (nreverse articles)))
6568      ((and (gnus-region-active-p) (mark))
6569       (message "region active")
6570       ;; Work on the region between point and mark.
6571       (let ((max (max (point) (mark)))
6572             articles article)
6573         (save-excursion
6574           (goto-char (min (point) (mark)))
6575           (while
6576               (and
6577                (push (setq article (gnus-summary-article-number)) articles)
6578                (gnus-summary-find-next nil article)
6579                (< (point) max)))
6580           (nreverse articles))))
6581      (gnus-newsgroup-processable
6582       ;; There are process-marked articles present.
6583       ;; Save current state.
6584       (gnus-summary-save-process-mark)
6585       ;; Return the list.
6586       (reverse gnus-newsgroup-processable))
6587      (t
6588       ;; Just return the current article.
6589       (list (gnus-summary-article-number))))))
6590
6591 (defmacro gnus-summary-iterate (arg &rest forms)
6592   "Iterate over the process/prefixed articles and do FORMS.
6593 ARG is the interactive prefix given to the command.  FORMS will be
6594 executed with point over the summary line of the articles."
6595   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6596     `(let ((,articles (gnus-summary-work-articles ,arg)))
6597        (while ,articles
6598          (gnus-summary-goto-subject (car ,articles))
6599          ,@forms
6600          (pop ,articles)))))
6601
6602 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6603 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6604
6605 (defun gnus-summary-save-process-mark ()
6606   "Push the current set of process marked articles on the stack."
6607   (interactive)
6608   (push (copy-sequence gnus-newsgroup-processable)
6609         gnus-newsgroup-process-stack))
6610
6611 (defun gnus-summary-kill-process-mark ()
6612   "Push the current set of process marked articles on the stack and unmark."
6613   (interactive)
6614   (gnus-summary-save-process-mark)
6615   (gnus-summary-unmark-all-processable))
6616
6617 (defun gnus-summary-yank-process-mark ()
6618   "Pop the last process mark state off the stack and restore it."
6619   (interactive)
6620   (unless gnus-newsgroup-process-stack
6621     (error "Empty mark stack"))
6622   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6623
6624 (defun gnus-summary-process-mark-set (set)
6625   "Make SET into the current process marked articles."
6626   (gnus-summary-unmark-all-processable)
6627   (mapc 'gnus-summary-set-process-mark set))
6628
6629 ;;; Searching and stuff
6630
6631 (defun gnus-summary-search-group (&optional backward use-level)
6632   "Search for next unread newsgroup.
6633 If optional argument BACKWARD is non-nil, search backward instead."
6634   (with-current-buffer gnus-group-buffer
6635     (when (gnus-group-search-forward
6636            backward nil (if use-level (gnus-group-group-level) nil))
6637       (gnus-group-group-name))))
6638
6639 (defun gnus-summary-best-group (&optional exclude-group)
6640   "Find the name of the best unread group.
6641 If EXCLUDE-GROUP, do not go to this group."
6642   (with-current-buffer gnus-group-buffer
6643     (save-excursion
6644       (gnus-group-best-unread-group exclude-group))))
6645
6646 (defun gnus-summary-find-next (&optional unread article backward)
6647   (if backward
6648       (gnus-summary-find-prev unread article)
6649     (let* ((dummy (gnus-summary-article-intangible-p))
6650            (article (or article (gnus-summary-article-number)))
6651            (data (gnus-data-find-list article))
6652            result)
6653       (when (and (not dummy)
6654                  (or (not gnus-summary-check-current)
6655                      (not unread)
6656                      (not (gnus-data-unread-p (car data)))))
6657         (setq data (cdr data)))
6658       (when (setq result
6659                   (if unread
6660                       (progn
6661                         (while data
6662                           (unless (memq (gnus-data-number (car data))
6663                                         (cond
6664                                          ((eq gnus-auto-goto-ignores
6665                                               'always-undownloaded)
6666                                           gnus-newsgroup-undownloaded)
6667                                          (gnus-plugged
6668                                           nil)
6669                                          ((eq gnus-auto-goto-ignores
6670                                               'unfetched)
6671                                           gnus-newsgroup-unfetched)
6672                                          ((eq gnus-auto-goto-ignores
6673                                               'undownloaded)
6674                                           gnus-newsgroup-undownloaded)))
6675                             (when (gnus-data-unread-p (car data))
6676                               (setq result (car data)
6677                                     data nil)))
6678                           (setq data (cdr data)))
6679                         result)
6680                     (car data)))
6681         (goto-char (gnus-data-pos result))
6682         (gnus-data-number result)))))
6683
6684 (defun gnus-summary-find-prev (&optional unread article)
6685   (let* ((eobp (eobp))
6686          (article (or article (gnus-summary-article-number)))
6687          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6688          result)
6689     (when (and (not eobp)
6690                (or (not gnus-summary-check-current)
6691                    (not unread)
6692                    (not (gnus-data-unread-p (car data)))))
6693       (setq data (cdr data)))
6694     (when (setq result
6695                 (if unread
6696                     (progn
6697                       (while data
6698                         (unless (memq (gnus-data-number (car data))
6699                                       (cond
6700                                        ((eq gnus-auto-goto-ignores
6701                                             'always-undownloaded)
6702                                         gnus-newsgroup-undownloaded)
6703                                        (gnus-plugged
6704                                         nil)
6705                                        ((eq gnus-auto-goto-ignores
6706                                             'unfetched)
6707                                         gnus-newsgroup-unfetched)
6708                                        ((eq gnus-auto-goto-ignores
6709                                             'undownloaded)
6710                                         gnus-newsgroup-undownloaded)))
6711                           (when (gnus-data-unread-p (car data))
6712                             (setq result (car data)
6713                                   data nil)))
6714                         (setq data (cdr data)))
6715                       result)
6716                   (car data)))
6717       (goto-char (gnus-data-pos result))
6718       (gnus-data-number result))))
6719
6720 (defun gnus-summary-find-subject (subject &optional unread backward article)
6721   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6722          (article (or article (gnus-summary-article-number)))
6723          (articles (gnus-data-list backward))
6724          (arts (gnus-data-find-list article articles))
6725          result)
6726     (when (or (not gnus-summary-check-current)
6727               (not unread)
6728               (not (gnus-data-unread-p (car arts))))
6729       (setq arts (cdr arts)))
6730     (while arts
6731       (and (or (not unread)
6732                (gnus-data-unread-p (car arts)))
6733            (vectorp (gnus-data-header (car arts)))
6734            (gnus-subject-equal
6735             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6736            (setq result (car arts)
6737                  arts nil))
6738       (setq arts (cdr arts)))
6739     (and result
6740          (goto-char (gnus-data-pos result))
6741          (gnus-data-number result))))
6742
6743 (defun gnus-summary-search-forward (&optional unread subject backward)
6744   "Search forward for an article.
6745 If UNREAD, look for unread articles.  If SUBJECT, look for
6746 articles with that subject.  If BACKWARD, search backward instead."
6747   (cond (subject (gnus-summary-find-subject subject unread backward))
6748         (backward (gnus-summary-find-prev unread))
6749         (t (gnus-summary-find-next unread))))
6750
6751 (defun gnus-recenter (&optional n)
6752   "Center point in window and redisplay frame.
6753 Also do horizontal recentering."
6754   (interactive "P")
6755   (when (and gnus-auto-center-summary
6756              (not (eq gnus-auto-center-summary 'vertical)))
6757     (gnus-horizontal-recenter))
6758   (if (fboundp 'recenter-top-bottom)
6759       (recenter-top-bottom n)
6760     (recenter n)))
6761
6762 (put 'gnus-recenter 'isearch-scroll t)
6763
6764 (defun gnus-forward-line-ignore-invisible (n)
6765   "Move N lines forward (backward if N is negative).
6766 Like forward-line, but skip over (and don't count) invisible lines."
6767   (let (done)
6768     (while (and (> n 0) (not done))
6769       ;; If the following character is currently invisible,
6770       ;; skip all characters with that same `invisible' property value.
6771       (while (gnus-invisible-p (point))
6772         (goto-char (gnus-next-char-property-change (point))))
6773       (forward-line 1)
6774       (if (eobp)
6775           (setq done t)
6776         (setq n (1- n))))
6777     (while (and (< n 0) (not done))
6778       (forward-line -1)
6779       (if (bobp) (setq done t)
6780         (setq n (1+ n))
6781         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6782           (goto-char (gnus-previous-char-property-change (point))))))))
6783
6784 (defun gnus-summary-recenter ()
6785   "Center point in the summary window.
6786 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6787 displayed, no centering will be performed."
6788   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6789   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6790   (interactive)
6791   ;; The user has to want it.
6792   (when gnus-auto-center-summary
6793     (let* ((top (cond ((< (window-height) 4) 0)
6794                       ((< (window-height) 7) 1)
6795                       (t (if (numberp gnus-auto-center-summary)
6796                              gnus-auto-center-summary
6797                            (/ (1- (window-height)) 2)))))
6798            (height (1- (window-height)))
6799            (bottom (save-excursion
6800                      (goto-char (point-max))
6801                      (gnus-forward-line-ignore-invisible (- height))
6802                      (point)))
6803            (window (get-buffer-window (current-buffer))))
6804       (when (get-buffer-window gnus-article-buffer)
6805         ;; Only do recentering when the article buffer is displayed,
6806         ;; Set the window start to either `bottom', which is the biggest
6807         ;; possible valid number, or the second line from the top,
6808         ;; whichever is the least.
6809         (let ((top-pos (save-excursion
6810                          (gnus-forward-line-ignore-invisible (- top))
6811                          (point))))
6812           (if (> bottom top-pos)
6813               ;; Keep the second line from the top visible
6814               (set-window-start window top-pos)
6815             ;; Try to keep the bottom line visible; if it's partially
6816             ;; obscured, either scroll one more line to make it fully
6817             ;; visible, or revert to using TOP-POS.
6818             (save-excursion
6819               (goto-char (point-max))
6820               (gnus-forward-line-ignore-invisible -1)
6821               (let ((last-line-start (point)))
6822                 (goto-char bottom)
6823                 (set-window-start window (point) t)
6824                 (when (not (pos-visible-in-window-p last-line-start window))
6825                   (gnus-forward-line-ignore-invisible 1)
6826                   (set-window-start window (min (point) top-pos) t)))))))
6827       ;; Do horizontal recentering while we're at it.
6828       (when (and (get-buffer-window (current-buffer) t)
6829                  (not (eq gnus-auto-center-summary 'vertical)))
6830         (let ((selected (selected-window)))
6831           (select-window (get-buffer-window (current-buffer) t))
6832           (gnus-summary-position-point)
6833           (gnus-horizontal-recenter)
6834           (select-window selected))))))
6835
6836 (defun gnus-summary-jump-to-group (newsgroup)
6837   "Move point to NEWSGROUP in group mode buffer."
6838   ;; Keep update point of group mode buffer if visible.
6839   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6840       (save-window-excursion
6841         ;; Take care of tree window mode.
6842         (when (get-buffer-window gnus-group-buffer)
6843           (pop-to-buffer gnus-group-buffer))
6844         (gnus-group-jump-to-group newsgroup))
6845     (save-excursion
6846       ;; Take care of tree window mode.
6847       (if (get-buffer-window gnus-group-buffer 0)
6848           (pop-to-buffer gnus-group-buffer)
6849         (set-buffer gnus-group-buffer))
6850       (gnus-group-jump-to-group newsgroup))))
6851
6852 ;; This function returns a list of article numbers based on the
6853 ;; difference between the ranges of read articles in this group and
6854 ;; the range of active articles.
6855 (defun gnus-list-of-unread-articles (group)
6856   (let* ((read (gnus-info-read (gnus-get-info group)))
6857          (active (or (gnus-active group) (gnus-activate-group group)))
6858          (last (or (cdr active)
6859                    (error "Group %s couldn't be activated " group)))
6860          (bottom (if gnus-newsgroup-maximum-articles
6861                      (max (car active)
6862                           (- last gnus-newsgroup-maximum-articles -1))
6863                    (car active)))
6864          first nlast unread)
6865     ;; If none are read, then all are unread.
6866     (if (not read)
6867         (setq first bottom)
6868       ;; If the range of read articles is a single range, then the
6869       ;; first unread article is the article after the last read
6870       ;; article.  Sounds logical, doesn't it?
6871       (if (and (not (listp (cdr read)))
6872                (or (< (car read) bottom)
6873                    (progn (setq read (list read))
6874                           nil)))
6875           (setq first (max bottom (1+ (cdr read))))
6876         ;; `read' is a list of ranges.
6877         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6878                                   (caar read)))
6879                   1)
6880           (setq first bottom))
6881         (while read
6882           (when first
6883             (while (< first nlast)
6884               (setq unread (cons first unread)
6885                     first (1+ first))))
6886           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6887           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6888           (setq read (cdr read)))))
6889     ;; And add the last unread articles.
6890     (while (<= first last)
6891       (setq unread (cons first unread)
6892             first (1+ first)))
6893     ;; Return the list of unread articles.
6894     (delq 0 (nreverse unread))))
6895
6896 (defun gnus-list-of-read-articles (group)
6897   "Return a list of unread, unticked and non-dormant articles."
6898   (let* ((info (gnus-get-info group))
6899          (marked (gnus-info-marks info))
6900          (active (gnus-active group)))
6901     (and info active
6902          (gnus-list-range-difference
6903           (gnus-list-range-difference
6904            (gnus-sorted-complement
6905             (gnus-uncompress-range
6906              (if gnus-newsgroup-maximum-articles
6907                  (cons (max (car active)
6908                             (- (cdr active)
6909                                gnus-newsgroup-maximum-articles
6910                                -1))
6911                        (cdr active))
6912                active))
6913             (gnus-list-of-unread-articles group))
6914            (cdr (assq 'dormant marked)))
6915           (cdr (assq 'tick marked))))))
6916
6917 ;; This function returns a sequence of article numbers based on the
6918 ;; difference between the ranges of read articles in this group and
6919 ;; the range of active articles.
6920 (defun gnus-sequence-of-unread-articles (group)
6921   (let* ((read (gnus-info-read (gnus-get-info group)))
6922          (active (or (gnus-active group) (gnus-activate-group group)))
6923          (last (cdr active))
6924          (bottom (if gnus-newsgroup-maximum-articles
6925                      (max (car active)
6926                           (- last gnus-newsgroup-maximum-articles -1))
6927                    (car active)))
6928          first nlast unread)
6929     ;; If none are read, then all are unread.
6930     (if (not read)
6931         (setq first bottom)
6932       ;; If the range of read articles is a single range, then the
6933       ;; first unread article is the article after the last read
6934       ;; article.  Sounds logical, doesn't it?
6935       (if (and (not (listp (cdr read)))
6936                (or (< (car read) bottom)
6937                    (progn (setq read (list read))
6938                           nil)))
6939           (setq first (max bottom (1+ (cdr read))))
6940         ;; `read' is a list of ranges.
6941         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6942                                   (caar read)))
6943                   1)
6944           (setq first bottom))
6945         (while read
6946           (when first
6947             (push (cons first nlast) unread))
6948           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6949           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6950           (setq read (cdr read)))))
6951     ;; And add the last unread articles.
6952     (cond ((not (and first last))
6953            nil)
6954           ((< first last)
6955            (push (cons first last) unread))
6956           ((= first last)
6957            (push first unread)))
6958     ;; Return the sequence of unread articles.
6959     (delq 0 (nreverse unread))))
6960
6961 ;; Various summary commands
6962
6963 (defun gnus-summary-select-article-buffer ()
6964   "Reconfigure windows to show the article buffer.
6965 If `gnus-widen-article-buffer' is set, show only the article
6966 buffer."
6967   (interactive)
6968   (if (not (gnus-buffer-live-p gnus-article-buffer))
6969       (error "There is no article buffer for this summary buffer")
6970     (unless (get-buffer-window gnus-article-buffer)
6971       (gnus-summary-show-article))
6972     (gnus-configure-windows
6973      (if gnus-widen-article-window
6974          'only-article
6975        'article)
6976      t)
6977     (select-window (get-buffer-window gnus-article-buffer))))
6978
6979 (defun gnus-summary-universal-argument (arg)
6980   "Perform any operation on all articles that are process/prefixed."
6981   (interactive "P")
6982   (let ((articles (gnus-summary-work-articles arg))
6983         func article)
6984     (if (eq
6985          (setq
6986           func
6987           (key-binding
6988            (read-key-sequence
6989             (substitute-command-keys
6990              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6991          'undefined)
6992         (gnus-error 1 "Undefined key")
6993       (save-excursion
6994         (while articles
6995           (gnus-summary-goto-subject (setq article (pop articles)))
6996           (let (gnus-newsgroup-processable)
6997             (command-execute func))
6998           (gnus-summary-remove-process-mark article)))))
6999   (gnus-summary-position-point))
7000
7001 (defun gnus-summary-toggle-truncation (&optional arg)
7002   "Toggle truncation of summary lines.
7003 With ARG, turn line truncation on if ARG is positive."
7004   (interactive "P")
7005   (setq truncate-lines
7006         (if (null arg) (not truncate-lines)
7007           (> (prefix-numeric-value arg) 0)))
7008   (redraw-display))
7009
7010 (defun gnus-summary-find-for-reselect ()
7011   "Return the number of an article to stay on across a reselect.
7012 The current article is considered, then following articles, then previous
7013 articles.  An article is sought which is not cancelled and isn't a temporary
7014 insertion from another group.  If there's no such then return a dummy 0."
7015   (let (found)
7016     (dolist (rev '(nil t))
7017       (unless found      ; don't demand the reverse list if we don't need it
7018         (let ((data (gnus-data-find-list
7019                      (gnus-summary-article-number) (gnus-data-list rev))))
7020           (while (and data (not found))
7021             (if (and (< 0 (gnus-data-number (car data)))
7022                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7023                 (setq found (gnus-data-number (car data))))
7024             (setq data (cdr data))))))
7025     (or found 0)))
7026
7027 (defun gnus-summary-reselect-current-group (&optional all rescan)
7028   "Exit and then reselect the current newsgroup.
7029 The prefix argument ALL means to select all articles."
7030   (interactive "P")
7031   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7032     (error "Ephemeral groups can't be reselected"))
7033   (let ((current-subject (gnus-summary-find-for-reselect))
7034         (group gnus-newsgroup-name))
7035     (setq gnus-newsgroup-begin nil)
7036     (gnus-summary-exit nil 'leave-hidden)
7037     ;; We have to adjust the point of group mode buffer because
7038     ;; point was moved to the next unread newsgroup by exiting.
7039     (gnus-summary-jump-to-group group)
7040     (when rescan
7041       (save-excursion
7042         (gnus-group-get-new-news-this-group 1)))
7043     (gnus-group-read-group all t)
7044     (gnus-summary-goto-subject current-subject nil t)))
7045
7046 (defun gnus-summary-rescan-group (&optional all)
7047   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7048   (interactive "P")
7049   (let ((config gnus-current-window-configuration))
7050     (gnus-summary-reselect-current-group all t)
7051     (gnus-configure-windows config)
7052     (when (eq config 'article)
7053       (gnus-summary-select-article))))
7054
7055 (defun gnus-summary-update-info (&optional non-destructive)
7056   (save-excursion
7057     (let ((group gnus-newsgroup-name))
7058       (when group
7059         (when gnus-newsgroup-kill-headers
7060           (setq gnus-newsgroup-killed
7061                 (gnus-compress-sequence
7062                  (gnus-sorted-union
7063                   (gnus-list-range-intersection
7064                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7065                   gnus-newsgroup-unreads)
7066                  t)))
7067         (unless (listp (cdr gnus-newsgroup-killed))
7068           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7069         (let ((headers gnus-newsgroup-headers))
7070           ;; Set the new ranges of read articles.
7071           (with-current-buffer gnus-group-buffer
7072             (gnus-undo-force-boundary))
7073           (gnus-update-read-articles
7074            group (gnus-sorted-union
7075                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7076           ;; Set the current article marks.
7077           (let ((gnus-newsgroup-scored
7078                  (if (and (not gnus-save-score)
7079                           (not non-destructive))
7080                      nil
7081                    gnus-newsgroup-scored)))
7082             (save-excursion
7083               (gnus-update-marks)))
7084           ;; Do the cross-ref thing.
7085           (when gnus-use-cross-reference
7086             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7087           ;; Do not switch windows but change the buffer to work.
7088           (set-buffer gnus-group-buffer)
7089           (unless (gnus-ephemeral-group-p group)
7090             (gnus-group-update-group group)))))))
7091
7092 (defun gnus-summary-save-newsrc (&optional force)
7093   "Save the current number of read/marked articles in the dribble buffer.
7094 The dribble buffer will then be saved.
7095 If FORCE (the prefix), also save the .newsrc file(s)."
7096   (interactive "P")
7097   (gnus-summary-update-info t)
7098   (if force
7099       (gnus-save-newsrc-file)
7100     (gnus-dribble-save)))
7101
7102 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7103
7104 (defun gnus-summary-exit (&optional temporary leave-hidden)
7105   "Exit reading current newsgroup, and then return to group selection mode.
7106 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7107   (interactive)
7108   (gnus-set-global-variables)
7109   (when (gnus-buffer-live-p gnus-article-buffer)
7110     (with-current-buffer gnus-article-buffer
7111       (mm-destroy-parts gnus-article-mime-handles)
7112       ;; Set it to nil for safety reason.
7113       (setq gnus-article-mime-handle-alist nil)
7114       (setq gnus-article-mime-handles nil)))
7115   (gnus-kill-save-kill-buffer)
7116   (gnus-async-halt-prefetch)
7117   (let* ((group gnus-newsgroup-name)
7118          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7119          (gnus-group-is-exiting-p t)
7120          (mode major-mode)
7121          (group-point nil)
7122          (buf (current-buffer)))
7123     (unless quit-config
7124       ;; Do adaptive scoring, and possibly save score files.
7125       (when gnus-newsgroup-adaptive
7126         (gnus-score-adaptive))
7127       (when gnus-use-scoring
7128         (gnus-score-save)))
7129     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7130     (when gnus-use-cache
7131       (gnus-cache-possibly-remove-articles)
7132       (gnus-cache-save-buffers))
7133     (gnus-async-prefetch-remove-group group)
7134     (when gnus-suppress-duplicates
7135       (gnus-dup-enter-articles))
7136     (when gnus-use-trees
7137       (gnus-tree-close group))
7138     (when gnus-use-cache
7139       (gnus-cache-write-active))
7140     ;; Remove entries for this group.
7141     (nnmail-purge-split-history (gnus-group-real-name group))
7142     ;; Make all changes in this group permanent.
7143     (unless quit-config
7144       (gnus-run-hooks 'gnus-exit-group-hook)
7145       (gnus-summary-update-info))
7146     (gnus-close-group group)
7147     ;; Make sure where we were, and go to next newsgroup.
7148     (set-buffer gnus-group-buffer)
7149     (unless quit-config
7150       (gnus-group-jump-to-group group))
7151     (gnus-run-hooks 'gnus-summary-exit-hook)
7152     (unless (or quit-config
7153                 (not gnus-summary-next-group-on-exit)
7154                 ;; If this group has disappeared from the summary
7155                 ;; buffer, don't skip forwards.
7156                 (not (string= group (gnus-group-group-name))))
7157       (gnus-group-next-unread-group 1))
7158     (setq group-point (point))
7159     (if temporary
7160         nil                             ;Nothing to do.
7161       (set-buffer buf)
7162       (if (not gnus-kill-summary-on-exit)
7163           (progn
7164             (gnus-deaden-summary)
7165             (setq mode nil))
7166         (when (get-buffer gnus-article-buffer)
7167           (bury-buffer gnus-article-buffer))
7168         ;; Return to group mode buffer.
7169         (when (eq mode 'gnus-summary-mode)
7170           (gnus-kill-buffer buf)))
7171
7172       ;; If we have several article buffers, we kill them at exit.
7173       (unless gnus-single-article-buffer
7174         (when (gnus-buffer-live-p gnus-article-buffer)
7175           (with-current-buffer gnus-article-buffer
7176             ;; Don't kill sticky article buffers
7177             (unless (eq major-mode 'gnus-sticky-article-mode)
7178               (gnus-kill-buffer gnus-article-buffer)
7179               (setq gnus-article-current nil))))
7180         (gnus-kill-buffer gnus-original-article-buffer))
7181
7182       (setq gnus-current-select-method gnus-select-method)
7183       (set-buffer gnus-group-buffer)
7184       (if quit-config
7185           (gnus-handle-ephemeral-exit quit-config)
7186         (goto-char group-point)
7187         ;; If gnus-group-buffer is already displayed, make sure we also move
7188         ;; the cursor in the window that displays it.
7189         (let ((win (get-buffer-window (current-buffer) 0)))
7190           (if win (set-window-point win (point))))
7191         (unless leave-hidden
7192           (gnus-configure-windows 'group 'force)))
7193       ;; Clear the current group name.
7194       (unless quit-config
7195         (setq gnus-newsgroup-name nil)))))
7196
7197 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7198 (defun gnus-summary-exit-no-update (&optional no-questions)
7199   "Quit reading current newsgroup without updating read article info."
7200   (interactive)
7201   (let* ((group gnus-newsgroup-name)
7202          (gnus-group-is-exiting-p t)
7203          (gnus-group-is-exiting-without-update-p t)
7204          (quit-config (gnus-group-quit-config group)))
7205     (when (or no-questions
7206               gnus-expert-user
7207               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7208       (gnus-async-halt-prefetch)
7209       (run-hooks 'gnus-summary-prepare-exit-hook)
7210       (when (gnus-buffer-live-p gnus-article-buffer)
7211         (with-current-buffer gnus-article-buffer
7212           (mm-destroy-parts gnus-article-mime-handles)
7213           ;; Set it to nil for safety reason.
7214           (setq gnus-article-mime-handle-alist nil)
7215           (setq gnus-article-mime-handles nil)))
7216       ;; If we have several article buffers, we kill them at exit.
7217       (unless gnus-single-article-buffer
7218         (gnus-kill-buffer gnus-article-buffer)
7219         (gnus-kill-buffer gnus-original-article-buffer)
7220         (setq gnus-article-current nil))
7221       (if (not gnus-kill-summary-on-exit)
7222           (gnus-deaden-summary)
7223         (gnus-close-group group)
7224         (gnus-kill-buffer gnus-summary-buffer))
7225       (unless gnus-single-article-buffer
7226         (setq gnus-article-current nil))
7227       (when gnus-use-trees
7228         (gnus-tree-close group))
7229       (gnus-async-prefetch-remove-group group)
7230       (when (get-buffer gnus-article-buffer)
7231         (bury-buffer gnus-article-buffer))
7232       ;; Return to the group buffer.
7233       (gnus-configure-windows 'group 'force)
7234       ;; Clear the current group name.
7235       (setq gnus-newsgroup-name nil)
7236       (unless (gnus-ephemeral-group-p group)
7237         (gnus-group-update-group group))
7238       (when (equal (gnus-group-group-name) group)
7239         (gnus-group-next-unread-group 1))
7240       (when quit-config
7241         (gnus-handle-ephemeral-exit quit-config)))))
7242
7243 (defun gnus-handle-ephemeral-exit (quit-config)
7244   "Handle movement when leaving an ephemeral group.
7245 The state which existed when entering the ephemeral is reset."
7246   (if (not (buffer-name (car quit-config)))
7247       (gnus-configure-windows 'group 'force)
7248     (set-buffer (car quit-config))
7249     (cond ((eq major-mode 'gnus-summary-mode)
7250            (gnus-set-global-variables))
7251           ((eq major-mode 'gnus-article-mode)
7252            (save-current-buffer
7253              ;; The `gnus-summary-buffer' variable may point
7254              ;; to the old summary buffer when using a single
7255              ;; article buffer.
7256              (unless (gnus-buffer-live-p gnus-summary-buffer)
7257                (set-buffer gnus-group-buffer))
7258              (set-buffer gnus-summary-buffer)
7259              (gnus-set-global-variables))))
7260     (if (or (eq (cdr quit-config) 'article)
7261             (eq (cdr quit-config) 'pick))
7262         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7263             (gnus-configure-windows 'pick 'force)
7264           (gnus-configure-windows (cdr quit-config) 'force))
7265       (gnus-configure-windows (cdr quit-config) 'force))
7266     (when (eq major-mode 'gnus-summary-mode)
7267       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7268                                                      next-unread-noselect))
7269           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7270                                   'next-noselect)
7271                               (gnus-summary-next-subject 1 nil t))
7272                              ((eq gnus-auto-select-on-ephemeral-exit
7273                                   'next-unread-noselect)
7274                               (gnus-summary-next-subject 1 t t))))
7275             ;; Hide the article buffer which displays the article different
7276             ;; from the one that the cursor points to in the summary buffer.
7277             (gnus-configure-windows 'summary 'force))
7278         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7279                (gnus-summary-next-subject 1))
7280               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7281                (gnus-summary-next-subject 1 t))))
7282       (gnus-summary-recenter)
7283       (gnus-summary-position-point))))
7284
7285 ;;; Dead summaries.
7286
7287 (defvar gnus-dead-summary-mode-map
7288   (let ((map (make-keymap)))
7289     (suppress-keymap map)
7290     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7291     (dolist (key '("\C-d" "\r" "\177" [delete]))
7292       (define-key map key 'gnus-summary-wake-up-the-dead))
7293     (dolist (key '("q" "Q"))
7294       (define-key map key 'bury-buffer))
7295     map))
7296
7297 (define-minor-mode gnus-dead-summary-mode
7298   "Minor mode for Gnus summary buffers."
7299   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7300   (unless (derived-mode-p 'gnus-summary-mode)
7301     (setq gnus-dead-summary-mode nil)))
7302
7303 (defun gnus-deaden-summary ()
7304   "Make the current summary buffer into a dead summary buffer."
7305   ;; Kill any previous dead summary buffer.
7306   (when (and gnus-dead-summary
7307              (buffer-name gnus-dead-summary))
7308     (with-current-buffer gnus-dead-summary
7309       (when gnus-dead-summary-mode
7310         (kill-buffer (current-buffer)))))
7311   ;; Make this the current dead summary.
7312   (setq gnus-dead-summary (current-buffer))
7313   (gnus-dead-summary-mode 1)
7314   (let ((name (buffer-name)))
7315     (when (string-match "Summary" name)
7316       (rename-buffer
7317        (concat (substring name 0 (match-beginning 0)) "Dead "
7318                (substring name (match-beginning 0)))
7319        t)
7320       (bury-buffer))))
7321
7322 (defun gnus-kill-or-deaden-summary (buffer)
7323   "Kill or deaden the summary BUFFER."
7324   (save-excursion
7325     (when (and (buffer-name buffer)
7326                (not gnus-single-article-buffer))
7327       (with-current-buffer buffer
7328         (gnus-kill-buffer gnus-article-buffer)
7329         (gnus-kill-buffer gnus-original-article-buffer)))
7330     (cond
7331      ;; Kill the buffer.
7332      (gnus-kill-summary-on-exit
7333       (when (and gnus-use-trees
7334                  (gnus-buffer-exists-p buffer))
7335         (with-current-buffer buffer
7336           (gnus-tree-close gnus-newsgroup-name)))
7337       (gnus-kill-buffer buffer))
7338      ;; Deaden the buffer.
7339      ((gnus-buffer-exists-p buffer)
7340       (with-current-buffer buffer
7341         (gnus-deaden-summary))))))
7342
7343 (defun gnus-summary-wake-up-the-dead (&rest args)
7344   "Wake up the dead summary buffer."
7345   (interactive)
7346   (gnus-dead-summary-mode -1)
7347   (let ((name (buffer-name)))
7348     (when (string-match "Dead " name)
7349       (rename-buffer
7350        (concat (substring name 0 (match-beginning 0))
7351                (substring name (match-end 0)))
7352        t)))
7353   (gnus-message 3 "This dead summary is now alive again"))
7354
7355 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7356 (defun gnus-summary-describe-group (&optional force)
7357   "Describe the current newsgroup."
7358   (interactive "P")
7359   (gnus-group-describe-group force gnus-newsgroup-name))
7360
7361 (defun gnus-summary-describe-briefly ()
7362   "Describe summary mode commands briefly."
7363   (interactive)
7364   (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")))
7365
7366 ;; Walking around group mode buffer from summary mode.
7367
7368 (defun gnus-summary-next-group (&optional no-article target-group backward)
7369   "Exit current newsgroup and then select next unread newsgroup.
7370 If prefix argument NO-ARTICLE is non-nil, no article is selected
7371 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7372 previous group instead."
7373   (interactive "P")
7374   ;; Stop pre-fetching.
7375   (gnus-async-halt-prefetch)
7376   (let ((current-group gnus-newsgroup-name)
7377         (current-buffer (current-buffer))
7378         entered)
7379     ;; First we semi-exit this group to update Xrefs and all variables.
7380     ;; We can't do a real exit, because the window conf must remain
7381     ;; the same in case the user is prompted for info, and we don't
7382     ;; want the window conf to change before that...
7383     (gnus-summary-exit t)
7384     (while (not entered)
7385       ;; Then we find what group we are supposed to enter.
7386       (set-buffer gnus-group-buffer)
7387       (gnus-group-jump-to-group current-group)
7388       (setq target-group
7389             (or target-group
7390                 (if (eq gnus-keep-same-level 'best)
7391                     (gnus-summary-best-group gnus-newsgroup-name)
7392                   (gnus-summary-search-group backward gnus-keep-same-level))))
7393       (if (not target-group)
7394           ;; There are no further groups, so we return to the group
7395           ;; buffer.
7396           (progn
7397             (gnus-message 5 "Returning to the group buffer")
7398             (setq entered t)
7399             (when (gnus-buffer-live-p current-buffer)
7400               (set-buffer current-buffer)
7401               (gnus-summary-exit))
7402             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7403         ;; We try to enter the target group.
7404         (gnus-group-jump-to-group target-group)
7405         (let ((unreads (gnus-group-group-unread)))
7406           (if (and (or (eq t unreads)
7407                        (and unreads (not (zerop unreads))))
7408                    (gnus-summary-read-group
7409                     target-group nil no-article
7410                     (and (buffer-name current-buffer) current-buffer)
7411                     nil backward))
7412               (setq entered t)
7413             (setq current-group target-group
7414                   target-group nil)))))))
7415
7416 (defun gnus-summary-prev-group (&optional no-article)
7417   "Exit current newsgroup and then select previous unread newsgroup.
7418 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7419   (interactive "P")
7420   (gnus-summary-next-group no-article nil t))
7421
7422 ;; Walking around summary lines.
7423
7424 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7425   "Go to the first subject satisfying any non-nil constraint.
7426 If UNREAD is non-nil, the article should be unread.
7427 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7428 If UNSEEN is non-nil, the article should be unseen as well as unread.
7429 Returns the article selected or nil if there are no matching articles."
7430   (interactive "P")
7431   (cond
7432    ;; Empty summary.
7433    ((null gnus-newsgroup-data)
7434     (gnus-message 3 "No articles in the group")
7435     nil)
7436    ;; Pick the first article.
7437    ((not (or unread undownloaded unseen))
7438     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7439     (gnus-data-number (car gnus-newsgroup-data)))
7440    ;; Find the first unread article.
7441    (t
7442     (let ((data gnus-newsgroup-data))
7443       (while (and data
7444                   (let ((num (gnus-data-number (car data))))
7445                     (or (memq num gnus-newsgroup-unfetched)
7446                         (not (or (and unread
7447                                       (memq num gnus-newsgroup-unreads))
7448                                  (and undownloaded
7449                                       (memq num gnus-newsgroup-undownloaded))
7450                                  (and unseen
7451                                       (memq num gnus-newsgroup-unseen)
7452                                       (memq num gnus-newsgroup-unreads)))))))
7453         (setq data (cdr data)))
7454       (prog1
7455           (if data
7456               (progn
7457                 (goto-char (gnus-data-pos (car data)))
7458                 (gnus-data-number (car data)))
7459             (gnus-message 3 "No more%s articles"
7460                           (let* ((r (when unread " unread"))
7461                                  (d (when undownloaded " undownloaded"))
7462                                  (s (when unseen " unseen"))
7463                                  (l (delq nil (list r d s))))
7464                             (cond ((= 3 (length l))
7465                                    (concat r "," d ", or" s))
7466                                   ((= 2 (length l))
7467                                    (concat (car l) ", or" (cadr l)))
7468                                   ((= 1 (length l))
7469                                    (car l))
7470                                   (t
7471                                    ""))))
7472             nil
7473             )
7474         (gnus-summary-position-point))))))
7475
7476 (defun gnus-summary-next-subject (n &optional unread dont-display)
7477   "Go to next N'th summary line.
7478 If N is negative, go to the previous N'th subject line.
7479 If UNREAD is non-nil, only unread articles are selected.
7480 The difference between N and the actual number of steps taken is
7481 returned."
7482   (interactive "p")
7483   (let ((backward (< n 0))
7484         (n (abs n)))
7485     (while (and (> n 0)
7486                 (if backward
7487                     (gnus-summary-find-prev unread)
7488                   (gnus-summary-find-next unread)))
7489       (unless (zerop (setq n (1- n)))
7490         (gnus-summary-show-thread)))
7491     (when (/= 0 n)
7492       (gnus-message 7 "No more%s articles"
7493                     (if unread " unread" "")))
7494     (unless dont-display
7495       (gnus-summary-recenter)
7496       (gnus-summary-position-point))
7497     n))
7498
7499 (defun gnus-summary-next-unread-subject (n)
7500   "Go to next N'th unread summary line."
7501   (interactive "p")
7502   (gnus-summary-next-subject n t))
7503
7504 (defun gnus-summary-prev-subject (n &optional unread)
7505   "Go to previous N'th summary line.
7506 If optional argument UNREAD is non-nil, only unread article is selected."
7507   (interactive "p")
7508   (gnus-summary-next-subject (- n) unread))
7509
7510 (defun gnus-summary-prev-unread-subject (n)
7511   "Go to previous N'th unread summary line."
7512   (interactive "p")
7513   (gnus-summary-next-subject (- n) t))
7514
7515 (defun gnus-summary-goto-subjects (articles)
7516   "Insert the subject header for ARTICLES in the current buffer."
7517   (save-excursion
7518     (dolist (article articles)
7519       (gnus-summary-goto-subject article t)))
7520   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7521   (gnus-summary-position-point))
7522
7523 (defun gnus-summary-goto-subject (article &optional force silent)
7524   "Go to the subject line of ARTICLE.
7525 If FORCE, also allow jumping to articles not currently shown."
7526   (interactive "nArticle number: ")
7527   (unless (numberp article)
7528     (error "Article %s is not a number" article))
7529   (let ((b (point))
7530         (data (gnus-data-find article)))
7531     ;; We read in the article if we have to.
7532     (and (not data)
7533          force
7534          (gnus-summary-insert-subject
7535           article
7536           (if (or (numberp force) (vectorp force)) force)
7537           t)
7538          (setq data (gnus-data-find article)))
7539     (goto-char b)
7540     (if (not data)
7541         (progn
7542           (unless silent
7543             (gnus-message 3 "Can't find article %d" article))
7544           nil)
7545       (let ((pt (gnus-data-pos data)))
7546         (goto-char pt)
7547         (gnus-summary-set-article-display-arrow pt))
7548       (gnus-summary-position-point)
7549       article)))
7550
7551 ;; Walking around summary lines with displaying articles.
7552
7553 (defun gnus-summary-expand-window (&optional arg)
7554   "Make the summary buffer take up the entire Emacs frame.
7555 Given a prefix, will force an `article' buffer configuration."
7556   (interactive "P")
7557   (if arg
7558       (gnus-configure-windows 'article 'force)
7559     (gnus-configure-windows 'summary 'force)))
7560
7561 (defun gnus-summary-display-article (article &optional all-header)
7562   "Display ARTICLE in article buffer."
7563   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7564                (with-current-buffer gnus-article-buffer
7565                  (eq major-mode 'gnus-article-mode)))
7566     (gnus-article-setup-buffer))
7567   (gnus-set-global-variables)
7568   (with-current-buffer gnus-article-buffer
7569     (setq gnus-article-charset gnus-newsgroup-charset)
7570     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7571     (mm-enable-multibyte))
7572   (if (null article)
7573       nil
7574     (prog1
7575         (if gnus-summary-display-article-function
7576             (funcall gnus-summary-display-article-function article all-header)
7577           (gnus-article-prepare article all-header))
7578       (gnus-run-hooks 'gnus-select-article-hook)
7579       (when (and gnus-current-article
7580                  (not (zerop gnus-current-article)))
7581         (gnus-summary-goto-subject gnus-current-article))
7582       (gnus-summary-recenter)
7583       (when (and gnus-use-trees gnus-show-threads)
7584         (gnus-possibly-generate-tree article)
7585         (gnus-highlight-selected-tree article))
7586       ;; Successfully display article.
7587       (gnus-article-set-window-start
7588        (cdr (assq article gnus-newsgroup-bookmarks))))))
7589
7590 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7591   "Select the current article.
7592 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7593 non-nil, the article will be re-fetched even if it already present in
7594 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7595 be displayed."
7596   ;; Make sure we are in the summary buffer to work around bbdb bug.
7597   (unless (eq major-mode 'gnus-summary-mode)
7598     (set-buffer gnus-summary-buffer))
7599   (let ((article (or article (gnus-summary-article-number)))
7600         (all-headers (not (not all-headers))) ;Must be t or nil.
7601         gnus-summary-display-article-function)
7602     (and (not pseudo)
7603          (gnus-summary-article-pseudo-p article)
7604          (error "This is a pseudo-article"))
7605     (with-current-buffer gnus-summary-buffer
7606       (if (or (and gnus-single-article-buffer
7607                    (or (null gnus-current-article)
7608                        (null gnus-article-current)
7609                        (null (get-buffer gnus-article-buffer))
7610                        (not (eq article (cdr gnus-article-current)))
7611                        (not (equal (car gnus-article-current)
7612                                    gnus-newsgroup-name))
7613                        (not (get-buffer gnus-original-article-buffer))))
7614               (and (not gnus-single-article-buffer)
7615                    (or (null gnus-current-article)
7616                        (not (get-buffer gnus-original-article-buffer))
7617                        (not (eq gnus-current-article article))))
7618               force)
7619           ;; The requested article is different from the current article.
7620           (progn
7621             (gnus-summary-display-article article all-headers)
7622             (when (gnus-buffer-live-p gnus-article-buffer)
7623               (with-current-buffer gnus-article-buffer
7624                 (if (not gnus-article-decoded-p) ;; a local variable
7625                     (mm-disable-multibyte))))
7626             (gnus-article-set-window-start
7627              (cdr (assq article gnus-newsgroup-bookmarks)))
7628             article)
7629         'old))))
7630
7631 (defun gnus-summary-force-verify-and-decrypt ()
7632   "Display buttons for signed/encrypted parts and verify/decrypt them."
7633   (interactive)
7634   (let ((mm-verify-option 'known)
7635         (mm-decrypt-option 'known)
7636         (gnus-article-emulate-mime t)
7637         (gnus-buttonized-mime-types (append (list "multipart/signed"
7638                                                   "multipart/encrypted")
7639                                             gnus-buttonized-mime-types)))
7640     (gnus-summary-select-article nil 'force)))
7641
7642 (defun gnus-summary-set-current-mark (&optional current-mark)
7643   "Obsolete function."
7644   nil)
7645
7646 (defun gnus-summary-next-article (&optional unread subject backward push)
7647   "Select the next article.
7648 If UNREAD, only unread articles are selected.
7649 If SUBJECT, only articles with SUBJECT are selected.
7650 If BACKWARD, the previous article is selected instead of the next."
7651   (interactive "P")
7652   ;; Make sure we are in the summary buffer.
7653   (unless (eq major-mode 'gnus-summary-mode)
7654     (set-buffer gnus-summary-buffer))
7655   (cond
7656    ;; Is there such an article?
7657    ((and (gnus-summary-search-forward unread subject backward)
7658          (or (gnus-summary-display-article (gnus-summary-article-number))
7659              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7660     (gnus-summary-position-point))
7661    ;; If not, we try the first unread, if that is wanted.
7662    ((and subject
7663          gnus-auto-select-same
7664          (gnus-summary-first-unread-article))
7665     (gnus-summary-position-point)
7666     (gnus-message 6 "Wrapped"))
7667    ;; Try to get next/previous article not displayed in this group.
7668    ((and gnus-auto-extend-newsgroup
7669          (not unread) (not subject))
7670     (gnus-summary-goto-article
7671      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7672      nil (count-lines (point-min) (point))))
7673    ;; Go to next/previous group.
7674    (t
7675     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7676       (gnus-summary-jump-to-group gnus-newsgroup-name))
7677     (let ((cmd (if (featurep 'xemacs)
7678                    last-command-char
7679                  last-command-event))
7680           (point
7681            (with-current-buffer gnus-group-buffer
7682              (point)))
7683           (group
7684            (if (eq gnus-keep-same-level 'best)
7685                (gnus-summary-best-group gnus-newsgroup-name)
7686              (gnus-summary-search-group backward gnus-keep-same-level))))
7687       ;; Select next unread newsgroup automagically.
7688       (cond
7689        ((or (not gnus-auto-select-next)
7690             (not cmd))
7691         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7692        ((or (eq gnus-auto-select-next 'quietly)
7693             (and (eq gnus-auto-select-next 'slightly-quietly)
7694                  push)
7695             (and (eq gnus-auto-select-next 'almost-quietly)
7696                  (gnus-summary-last-article-p)))
7697         ;; Select quietly.
7698         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7699             (gnus-summary-exit)
7700           (gnus-message 7 "No more%s articles (%s)..."
7701                         (if unread " unread" "")
7702                         (if group (concat "selecting " group)
7703                           "exiting"))
7704           (gnus-summary-next-group nil group backward)))
7705        (t
7706         (when (gnus-key-press-event-p last-input-event)
7707           (gnus-summary-walk-group-buffer
7708            gnus-newsgroup-name cmd unread backward point))))))))
7709
7710 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7711   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7712                       (?\C-p (gnus-group-prev-unread-group 1))))
7713         (cursor-in-echo-area t)
7714         keve key group ended prompt)
7715     (with-current-buffer gnus-group-buffer
7716       (goto-char start)
7717       (setq group
7718             (if (eq gnus-keep-same-level 'best)
7719                 (gnus-summary-best-group gnus-newsgroup-name)
7720               (gnus-summary-search-group backward gnus-keep-same-level))))
7721     (while (not ended)
7722       (setq prompt
7723             (format
7724              "No more%s articles%s " (if unread " unread" "")
7725              (if (and group
7726                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7727                  (format " (Type %s for %s [%s])"
7728                          (single-key-description cmd)
7729                          (gnus-group-decoded-name group)
7730                          (gnus-group-unread group))
7731                (format " (Type %s to exit %s)"
7732                        (single-key-description cmd)
7733                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7734       ;; Confirm auto selection.
7735       (setq key (car (setq keve (gnus-read-event-char prompt)))
7736             ended t)
7737       (cond
7738        ((assq key keystrokes)
7739         (let ((obuf (current-buffer)))
7740           (switch-to-buffer gnus-group-buffer)
7741           (when group
7742             (gnus-group-jump-to-group group))
7743           (eval (cadr (assq key keystrokes)))
7744           (setq group (gnus-group-group-name))
7745           (switch-to-buffer obuf))
7746         (setq ended nil))
7747        ((equal key cmd)
7748         (if (or (not group)
7749                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7750             (gnus-summary-exit)
7751           (gnus-summary-next-group nil group backward)))
7752        (t
7753         (push (cdr keve) unread-command-events))))))
7754
7755 (defun gnus-summary-next-unread-article ()
7756   "Select unread article after current one."
7757   (interactive)
7758   (gnus-summary-next-article
7759    (or (not (eq gnus-summary-goto-unread 'never))
7760        (gnus-summary-last-article-p (gnus-summary-article-number)))
7761    (and gnus-auto-select-same
7762         (gnus-summary-article-subject))))
7763
7764 (defun gnus-summary-prev-article (&optional unread subject)
7765   "Select the article before the current one.
7766 If UNREAD is non-nil, only unread articles are selected."
7767   (interactive "P")
7768   (gnus-summary-next-article unread subject t))
7769
7770 (defun gnus-summary-prev-unread-article ()
7771   "Select unread article before current one."
7772   (interactive)
7773   (gnus-summary-prev-article
7774    (or (not (eq gnus-summary-goto-unread 'never))
7775        (gnus-summary-first-article-p (gnus-summary-article-number)))
7776    (and gnus-auto-select-same
7777         (gnus-summary-article-subject))))
7778
7779 (defun gnus-summary-next-page (&optional lines circular stop)
7780   "Show next page of the selected article.
7781 If at the end of the current article, select the next article.
7782 LINES says how many lines should be scrolled up.
7783
7784 If CIRCULAR is non-nil, go to the start of the article instead of
7785 selecting the next article when reaching the end of the current
7786 article.
7787
7788 If STOP is non-nil, just stop when reaching the end of the message.
7789
7790 Also see the variable `gnus-article-skip-boring'."
7791   (interactive "P")
7792   (setq gnus-summary-buffer (current-buffer))
7793   (gnus-set-global-variables)
7794   (let ((article (gnus-summary-article-number))
7795         (article-window (get-buffer-window gnus-article-buffer t))
7796         endp)
7797     ;; If the buffer is empty, we have no article.
7798     (unless article
7799       (error "No article to select"))
7800     (gnus-configure-windows 'article)
7801     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7802         (if (and (eq gnus-summary-goto-unread 'never)
7803                  (not (gnus-summary-last-article-p article)))
7804             (gnus-summary-next-article)
7805           (gnus-summary-next-unread-article))
7806       (if (or (null gnus-current-article)
7807               (null gnus-article-current)
7808               (/= article (cdr gnus-article-current))
7809               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7810           ;; Selected subject is different from current article's.
7811           (gnus-summary-display-article article)
7812         (when article-window
7813           (gnus-eval-in-buffer-window gnus-article-buffer
7814             (setq endp (or (gnus-article-next-page lines)
7815                            (gnus-article-only-boring-p))))
7816           (when endp
7817             (cond ((or stop gnus-summary-stop-at-end-of-message)
7818                    (gnus-message 3 "End of message"))
7819                   (circular
7820                    (gnus-summary-beginning-of-article))
7821                   (lines
7822                    (gnus-message 3 "End of message"))
7823                   ((null lines)
7824                    (if (and (eq gnus-summary-goto-unread 'never)
7825                             (not (gnus-summary-last-article-p article)))
7826                        (gnus-summary-next-article)
7827                      (gnus-summary-next-unread-article))))))))
7828     (gnus-summary-recenter)
7829     (gnus-summary-position-point)))
7830
7831 (defun gnus-summary-prev-page (&optional lines move)
7832   "Show previous page of selected article.
7833 Argument LINES specifies lines to be scrolled down.
7834 If MOVE, move to the previous unread article if point is at
7835 the beginning of the buffer."
7836   (interactive "P")
7837   (let ((article (gnus-summary-article-number))
7838         (article-window (get-buffer-window gnus-article-buffer t))
7839         endp)
7840     (gnus-configure-windows 'article)
7841     (if (or (null gnus-current-article)
7842             (null gnus-article-current)
7843             (/= article (cdr gnus-article-current))
7844             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7845         ;; Selected subject is different from current article's.
7846         (gnus-summary-display-article article)
7847       (gnus-summary-recenter)
7848       (when article-window
7849         (gnus-eval-in-buffer-window gnus-article-buffer
7850           (setq endp (gnus-article-prev-page lines)))
7851         (when (and move endp)
7852           (cond (lines
7853                  (gnus-message 3 "Beginning of message"))
7854                 ((null lines)
7855                  (if (and (eq gnus-summary-goto-unread 'never)
7856                           (not (gnus-summary-first-article-p article)))
7857                      (gnus-summary-prev-article)
7858                    (gnus-summary-prev-unread-article))))))))
7859   (gnus-summary-position-point))
7860
7861 (defun gnus-summary-prev-page-or-article (&optional lines)
7862   "Show previous page of selected article.
7863 Argument LINES specifies lines to be scrolled down.
7864 If at the beginning of the article, go to the next article."
7865   (interactive "P")
7866   (gnus-summary-prev-page lines t))
7867
7868 (defun gnus-summary-scroll-up (lines)
7869   "Scroll up (or down) one line current article.
7870 Argument LINES specifies lines to be scrolled up (or down if negative).
7871 If no article is selected, then the current article will be selected first."
7872   (interactive "p")
7873   (gnus-configure-windows 'article)
7874   (gnus-summary-show-thread)
7875   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7876     (gnus-eval-in-buffer-window gnus-article-buffer
7877       (cond ((> lines 0)
7878              (when (gnus-article-next-page lines)
7879                (gnus-message 3 "End of message")))
7880             ((< lines 0)
7881              (gnus-article-prev-page (- lines))))))
7882   (gnus-summary-recenter)
7883   (gnus-summary-position-point))
7884
7885 (defun gnus-summary-scroll-down (lines)
7886   "Scroll down (or up) one line current article.
7887 Argument LINES specifies lines to be scrolled down (or up if negative).
7888 If no article is selected, then the current article will be selected first."
7889   (interactive "p")
7890   (gnus-summary-scroll-up (- lines)))
7891
7892 (defun gnus-summary-next-same-subject ()
7893   "Select next article which has the same subject as current one."
7894   (interactive)
7895   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7896
7897 (defun gnus-summary-prev-same-subject ()
7898   "Select previous article which has the same subject as current one."
7899   (interactive)
7900   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7901
7902 (defun gnus-summary-next-unread-same-subject ()
7903   "Select next unread article which has the same subject as current one."
7904   (interactive)
7905   (gnus-summary-next-article t (gnus-summary-article-subject)))
7906
7907 (defun gnus-summary-prev-unread-same-subject ()
7908   "Select previous unread article which has the same subject as current one."
7909   (interactive)
7910   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7911
7912 (defun gnus-summary-first-unread-article ()
7913   "Select the first unread article.
7914 Return nil if there are no unread articles."
7915   (interactive)
7916   (prog1
7917       (when (gnus-summary-first-subject t)
7918         (gnus-summary-show-thread)
7919         (gnus-summary-first-subject t)
7920         (gnus-summary-display-article (gnus-summary-article-number)))
7921     (gnus-summary-position-point)))
7922
7923 (defun gnus-summary-first-unread-subject ()
7924   "Place the point on the subject line of the first unread article.
7925 Return nil if there are no unread articles."
7926   (interactive)
7927   (prog1
7928       (when (gnus-summary-first-subject t)
7929         (gnus-summary-show-thread)
7930         (gnus-summary-first-subject t))
7931     (gnus-summary-position-point)))
7932
7933 (defun gnus-summary-first-unseen-subject ()
7934   "Place the point on the subject line of the first unseen article.
7935 Return nil if there are no unseen articles."
7936   (interactive)
7937   (prog1
7938       (when (gnus-summary-first-subject nil nil t)
7939         (gnus-summary-show-thread)
7940         (gnus-summary-first-subject nil nil t))
7941     (gnus-summary-position-point)))
7942
7943 (defun gnus-summary-first-unseen-or-unread-subject ()
7944   "Place the point on the subject line of the first unseen and unread article.
7945 If all article have been seen, on the subject line of the first unread
7946 article."
7947   (interactive)
7948   (prog1
7949       (unless (when (gnus-summary-first-subject nil nil t)
7950                 (gnus-summary-show-thread)
7951                 (gnus-summary-first-subject nil nil t))
7952         (when (gnus-summary-first-subject t)
7953           (gnus-summary-show-thread)
7954           (gnus-summary-first-subject t)))
7955     (gnus-summary-position-point)))
7956
7957 (defun gnus-summary-first-article ()
7958   "Select the first article.
7959 Return nil if there are no articles."
7960   (interactive)
7961   (prog1
7962       (when (gnus-summary-first-subject)
7963         (gnus-summary-show-thread)
7964         (gnus-summary-first-subject)
7965         (gnus-summary-display-article (gnus-summary-article-number)))
7966     (gnus-summary-position-point)))
7967
7968 (defun gnus-summary-best-unread-article (&optional arg)
7969   "Select the unread article with the highest score.
7970 If given a prefix argument, select the next unread article that has a
7971 score higher than the default score."
7972   (interactive "P")
7973   (let ((article (if arg
7974                      (gnus-summary-better-unread-subject)
7975                    (gnus-summary-best-unread-subject))))
7976     (if article
7977         (gnus-summary-goto-article article)
7978       (error "No unread articles"))))
7979
7980 (defun gnus-summary-best-unread-subject ()
7981   "Select the unread subject with the highest score."
7982   (interactive)
7983   (let ((best -1000000)
7984         (data gnus-newsgroup-data)
7985         article score)
7986     (while data
7987       (and (gnus-data-unread-p (car data))
7988            (> (setq score
7989                     (gnus-summary-article-score (gnus-data-number (car data))))
7990               best)
7991            (setq best score
7992                  article (gnus-data-number (car data))))
7993       (setq data (cdr data)))
7994     (when article
7995       (gnus-summary-goto-subject article))
7996     (gnus-summary-position-point)
7997     article))
7998
7999 (defun gnus-summary-better-unread-subject ()
8000   "Select the first unread subject that has a score over the default score."
8001   (interactive)
8002   (let ((data gnus-newsgroup-data)
8003         article score)
8004     (while (and (setq article (gnus-data-number (car data)))
8005                 (or (gnus-data-read-p (car data))
8006                     (not (> (gnus-summary-article-score article)
8007                             gnus-summary-default-score))))
8008       (setq data (cdr data)))
8009     (when article
8010       (gnus-summary-goto-subject article))
8011     (gnus-summary-position-point)
8012     article))
8013
8014 (defun gnus-summary-last-subject ()
8015   "Go to the last displayed subject line in the group."
8016   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8017     (when article
8018       (gnus-summary-goto-subject article))))
8019
8020 (defun gnus-summary-goto-article (article &optional all-headers force)
8021   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8022 If ALL-HEADERS is non-nil, no header lines are hidden.
8023 If FORCE, go to the article even if it isn't displayed.  If FORCE
8024 is a number, it is the line the article is to be displayed on."
8025   (interactive
8026    (list
8027     (gnus-completing-read
8028      "Article number or Message-ID"
8029      (mapcar 'int-to-string gnus-newsgroup-limit))
8030     current-prefix-arg
8031     t))
8032   (prog1
8033       (if (and (stringp article)
8034                (string-match "@\\|%40" article))
8035           (gnus-summary-refer-article article)
8036         (when (stringp article)
8037           (setq article (string-to-number article)))
8038         (if (gnus-summary-goto-subject article force)
8039             (gnus-summary-display-article article all-headers)
8040           (gnus-message 4 "Couldn't go to article %s" article) nil))
8041     (gnus-summary-position-point)))
8042
8043 (defun gnus-summary-goto-last-article ()
8044   "Go to the previously read article."
8045   (interactive)
8046   (prog1
8047       (when gnus-last-article
8048         (gnus-summary-goto-article gnus-last-article nil t))
8049     (gnus-summary-position-point)))
8050
8051 (defun gnus-summary-pop-article (number)
8052   "Pop one article off the history and go to the previous.
8053 NUMBER articles will be popped off."
8054   (interactive "p")
8055   (let (to)
8056     (setq gnus-newsgroup-history
8057           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8058     (if to
8059         (gnus-summary-goto-article (car to) nil t)
8060       (error "Article history empty")))
8061   (gnus-summary-position-point))
8062
8063 ;; Summary commands and functions for limiting the summary buffer.
8064
8065 (defun gnus-summary-limit-to-articles (n)
8066   "Limit the summary buffer to the next N articles.
8067 If not given a prefix, use the process marked articles instead."
8068   (interactive "P")
8069   (prog1
8070       (let ((articles (gnus-summary-work-articles n)))
8071         (setq gnus-newsgroup-processable nil)
8072         (gnus-summary-limit articles))
8073     (gnus-summary-position-point)))
8074
8075 (defun gnus-summary-pop-limit (&optional total)
8076   "Restore the previous limit.
8077 If given a prefix, remove all limits."
8078   (interactive "P")
8079   (when total
8080     (setq gnus-newsgroup-limits
8081           (list (mapcar (lambda (h) (mail-header-number h))
8082                         gnus-newsgroup-headers))))
8083   (unless gnus-newsgroup-limits
8084     (error "No limit to pop"))
8085   (prog1
8086       (gnus-summary-limit nil 'pop)
8087     (gnus-summary-position-point)))
8088
8089 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8090   "Limit the summary buffer to articles that have subjects that match a regexp.
8091 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8092   (interactive
8093    (list (read-string (if current-prefix-arg
8094                           "Exclude subject (regexp): "
8095                         "Limit to subject (regexp): "))
8096          nil current-prefix-arg))
8097   (unless header
8098     (setq header "subject"))
8099   (when (not (equal "" subject))
8100     (prog1
8101         (let ((articles (gnus-summary-find-matching
8102                          (or header "subject") subject 'all nil nil
8103                          not-matching)))
8104           (unless articles
8105             (error "Found no matches for \"%s\"" subject))
8106           (gnus-summary-limit articles))
8107       (gnus-summary-position-point))))
8108
8109 (defun gnus-summary-limit-to-author (from &optional not-matching)
8110   "Limit the summary buffer to articles that have authors that match a regexp.
8111 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8112   (interactive
8113    (list (read-string (if current-prefix-arg
8114                           "Exclude author (regexp): "
8115                         "Limit to author (regexp): "))
8116          current-prefix-arg))
8117   (gnus-summary-limit-to-subject from "from" not-matching))
8118
8119 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8120   "Limit the summary buffer to articles with the given RECIPIENT.
8121
8122 If NOT-MATCHING, exclude RECIPIENT.
8123
8124 To and Cc headers are checked.  You need to include them in
8125 `nnmail-extra-headers'."
8126   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8127   (interactive
8128    (list (read-string (format "%s recipient (regexp): "
8129                               (if current-prefix-arg "Exclude" "Limit to")))
8130          current-prefix-arg))
8131   (when (not (equal "" recipient))
8132     (prog1 (let* ((to
8133                    (if (memq 'To nnmail-extra-headers)
8134                        (gnus-summary-find-matching
8135                         (cons 'extra 'To) recipient 'all nil nil
8136                         not-matching)
8137                      (gnus-message
8138                       1 "`To' isn't present in `nnmail-extra-headers'")
8139                      (sit-for 1)
8140                      nil))
8141                   (cc
8142                    (if (memq 'Cc nnmail-extra-headers)
8143                        (gnus-summary-find-matching
8144                         (cons 'extra 'Cc) recipient 'all nil nil
8145                         not-matching)
8146                      (gnus-message
8147                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8148                      (sit-for 1)
8149                      nil))
8150                   (articles
8151                    (if not-matching
8152                        ;; We need the numbers that are in both lists:
8153                        (mapcar (lambda (a)
8154                                  (and (memq a to) a))
8155                                cc)
8156                      (nconc to cc))))
8157              (unless articles
8158                (error "Found no matches for \"%s\"" recipient))
8159              (gnus-summary-limit articles))
8160       (gnus-summary-position-point))))
8161
8162 (defun gnus-summary-limit-to-address (address &optional not-matching)
8163   "Limit the summary buffer to articles with the given ADDRESS.
8164
8165 If NOT-MATCHING, exclude ADDRESS.
8166
8167 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8168 in `nnmail-extra-headers'."
8169   (interactive
8170    (list (read-string (format "%s address (regexp): "
8171                               (if current-prefix-arg "Exclude" "Limit to")))
8172          current-prefix-arg))
8173   (when (not (equal "" address))
8174     (prog1 (let* ((to
8175                    (if (memq 'To nnmail-extra-headers)
8176                        (gnus-summary-find-matching
8177                         (cons 'extra 'To) address 'all nil nil
8178                         not-matching)
8179                      (gnus-message
8180                       1 "`To' isn't present in `nnmail-extra-headers'")
8181                      (sit-for 1)
8182                      t))
8183                   (cc
8184                    (if (memq 'Cc nnmail-extra-headers)
8185                        (gnus-summary-find-matching
8186                         (cons 'extra 'Cc) address 'all nil nil
8187                         not-matching)
8188                      (gnus-message
8189                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8190                      (sit-for 1)
8191                      t))
8192                   (from
8193                    (gnus-summary-find-matching "from" address
8194                                                'all nil nil not-matching))
8195                   (articles
8196                    (if not-matching
8197                        ;; We need the numbers that are in all lists:
8198                        (if (eq cc t)
8199                            (if (eq to t)
8200                                from
8201                              (mapcar (lambda (a) (car (memq a from))) to))
8202                          (if (eq to t)
8203                              (mapcar (lambda (a) (car (memq a from))) cc)
8204                            (mapcar (lambda (a) (car (memq a from)))
8205                                    (mapcar (lambda (a) (car (memq a to)))
8206                                            cc))))
8207                      (nconc (if (eq to t) nil to)
8208                             (if (eq cc t) nil cc)
8209                             from))))
8210              (unless articles
8211                (error "Found no matches for \"%s\"" address))
8212              (gnus-summary-limit articles))
8213       (gnus-summary-position-point))))
8214
8215 (defun gnus-summary-limit-strange-charsets-predicate (header)
8216   (when (fboundp 'char-charset)
8217     (let ((string (concat (mail-header-subject header)
8218                           (mail-header-from header)))
8219           charset found)
8220       (dotimes (i (1- (length string)))
8221         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8222         (when (string-match "unicode\\|big\\|japanese" charset)
8223           (setq found t)))
8224       found)))
8225
8226 (defun gnus-summary-limit-to-predicate (predicate)
8227   "Limit to articles where PREDICATE returns non-nil.
8228 PREDICATE will be called with the header structures of the
8229 articles."
8230   (let ((articles nil)
8231         (case-fold-search t))
8232     (dolist (header gnus-newsgroup-headers)
8233       (when (funcall predicate header)
8234         (push (mail-header-number header) articles)))
8235     (gnus-summary-limit (nreverse articles))))
8236
8237 (defun gnus-summary-limit-to-age (age &optional younger-p)
8238   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8239 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8240 articles that are younger than AGE days."
8241   (interactive
8242    (let ((younger current-prefix-arg)
8243          (days-got nil)
8244          days)
8245      (while (not days-got)
8246        (setq days (if younger
8247                       (read-string "Limit to articles younger than (in days, older when negative): ")
8248                     (read-string
8249                      "Limit to articles older than (in days, younger when negative): ")))
8250        (when (> (length days) 0)
8251          (setq days (read days)))
8252        (if (numberp days)
8253            (progn
8254              (setq days-got t)
8255              (when (< days 0)
8256                (setq younger (not younger))
8257                (setq days (* days -1))))
8258          (message "Please enter a number.")
8259          (sleep-for 1)))
8260      (list days younger)))
8261   (prog1
8262       (let ((data gnus-newsgroup-data)
8263             (cutoff (days-to-time age))
8264             articles d date is-younger)
8265         (while (setq d (pop data))
8266           (when (and (vectorp (gnus-data-header d))
8267                      (setq date (mail-header-date (gnus-data-header d))))
8268             (setq is-younger (time-less-p
8269                               (time-since (gnus-date-get-time date))
8270                               cutoff))
8271             (when (if younger-p
8272                       is-younger
8273                     (not is-younger))
8274               (push (gnus-data-number d) articles))))
8275         (gnus-summary-limit (nreverse articles)))
8276     (gnus-summary-position-point)))
8277
8278 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8279   "Limit the summary buffer to articles that match an 'extra' header."
8280   (interactive
8281    (let ((header
8282           (intern
8283            (gnus-completing-read
8284             (if current-prefix-arg
8285                 "Exclude extra header"
8286               "Limit extra header")
8287             (mapcar 'symbol-name gnus-extra-headers)
8288             t nil nil
8289             (symbol-name (car gnus-extra-headers))))))
8290      (list header
8291            (read-string (format "%s header %s (regexp): "
8292                                 (if current-prefix-arg "Exclude" "Limit to")
8293                                 header))
8294            current-prefix-arg)))
8295   (when (not (equal "" regexp))
8296     (prog1
8297         (let ((articles (gnus-summary-find-matching
8298                          (cons 'extra header) regexp 'all nil nil
8299                          not-matching)))
8300           (unless articles
8301             (error "Found no matches for \"%s\"" regexp))
8302           (gnus-summary-limit articles))
8303       (gnus-summary-position-point))))
8304
8305 (defun gnus-summary-limit-to-display-predicate ()
8306   "Limit the summary buffer to the predicated in the `display' group parameter."
8307   (interactive)
8308   (unless gnus-newsgroup-display
8309     (error "There is no `display' group parameter"))
8310   (let (articles)
8311     (dolist (gnus-number gnus-newsgroup-articles)
8312       (when (funcall gnus-newsgroup-display)
8313         (push gnus-number articles)))
8314     (gnus-summary-limit articles))
8315   (gnus-summary-position-point))
8316
8317 (defun gnus-summary-limit-to-unread (&optional all)
8318   "Limit the summary buffer to articles that are not marked as read.
8319 If ALL is non-nil, limit strictly to unread articles."
8320   (interactive "P")
8321   (if all
8322       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8323     (gnus-summary-limit-to-marks
8324      ;; Concat all the marks that say that an article is read and have
8325      ;; those removed.
8326      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8327            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8328            gnus-low-score-mark gnus-expirable-mark
8329            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8330            gnus-duplicate-mark)
8331      'reverse)))
8332
8333 (defun gnus-summary-limit-to-headers (match &optional reverse)
8334   "Limit the summary buffer to articles that have headers that match MATCH.
8335 If REVERSE (the prefix), limit to articles that don't match."
8336   (interactive "sMatch headers (regexp): \nP")
8337   (gnus-summary-limit-to-bodies match reverse t))
8338
8339 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8340   "Limit the summary buffer to articles that have bodies that match MATCH.
8341 If REVERSE (the prefix), limit to articles that don't match."
8342   (interactive "sMatch body (regexp): \nP")
8343   (let ((articles nil)
8344         (gnus-select-article-hook nil)  ;Disable hook.
8345         (gnus-article-prepare-hook nil)
8346         (gnus-use-article-prefetch nil)
8347         (gnus-keep-backlog nil)
8348         (gnus-break-pages nil)
8349         (gnus-summary-display-arrow nil)
8350         (gnus-updated-mode-lines nil)
8351         (gnus-auto-center-summary nil)
8352         (gnus-display-mime-function nil))
8353     (dolist (data gnus-newsgroup-data)
8354       (let (gnus-mark-article-hook)
8355         (gnus-summary-select-article t t nil (gnus-data-number data)))
8356       (with-current-buffer gnus-article-buffer
8357         (article-goto-body)
8358         (let* ((case-fold-search t)
8359                (found (if headersp
8360                           (re-search-backward match nil t)
8361                         (re-search-forward match nil t))))
8362           (when (or (and found
8363                          (not reverse))
8364                     (and (not found)
8365                          reverse))
8366             (push (gnus-data-number data) articles)))))
8367     (if (not articles)
8368         (message "No messages matched")
8369       (gnus-summary-limit articles)))
8370   (gnus-summary-position-point))
8371
8372 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8373   "Limit the summary buffer to articles that aren't part on any thread.
8374 If THREADSP (the prefix), limit to articles that are in threads."
8375   (interactive "P")
8376   (let ((articles nil)
8377         thread-articles
8378         threads)
8379     (dolist (thread gnus-newsgroup-threads)
8380       (if (stringp (car thread))
8381           (dolist (thread (cdr thread))
8382             (push thread threads))
8383         (push thread threads)))
8384     (dolist (thread threads)
8385       (setq thread-articles (gnus-articles-in-thread thread))
8386       (when (or (and threadsp
8387                      (> (length thread-articles) 1))
8388                 (and (not threadsp)
8389                      (= (length thread-articles) 1)))
8390         (setq articles (nconc thread-articles articles))))
8391     (if (not articles)
8392         (message "No messages matched")
8393       (gnus-summary-limit articles))
8394     (gnus-summary-position-point)))
8395
8396 (defun gnus-summary-limit-to-replied (&optional unreplied)
8397   "Limit the summary buffer to replied articles.
8398 If UNREPLIED (the prefix), limit to unreplied articles."
8399   (interactive "P")
8400   (if unreplied
8401       (gnus-summary-limit
8402        (gnus-set-difference gnus-newsgroup-articles
8403         gnus-newsgroup-replied))
8404     (gnus-summary-limit gnus-newsgroup-replied))
8405   (gnus-summary-position-point))
8406
8407 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8408   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8409 If REVERSE, limit the summary buffer to articles that are marked
8410 with MARKS.  MARKS can either be a string of marks or a list of marks.
8411 Returns how many articles were removed."
8412   (interactive "sMarks: ")
8413   (gnus-summary-limit-to-marks marks t))
8414
8415 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8416   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8417 If REVERSE (the prefix), limit the summary buffer to articles that are
8418 not marked with MARKS.  MARKS can either be a string of marks or a
8419 list of marks.
8420 Returns how many articles were removed."
8421   (interactive "sMarks: \nP")
8422   (prog1
8423       (let ((data gnus-newsgroup-data)
8424             (marks (if (listp marks) marks
8425                      (append marks nil))) ; Transform to list.
8426             articles)
8427         (while data
8428           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8429                   (memq (gnus-data-mark (car data)) marks))
8430             (push (gnus-data-number (car data)) articles))
8431           (setq data (cdr data)))
8432         (gnus-summary-limit articles))
8433     (gnus-summary-position-point)))
8434
8435 (defun gnus-summary-limit-to-score (score)
8436   "Limit to articles with score at or above SCORE."
8437   (interactive "NLimit to articles with score of at least: ")
8438   (let ((data gnus-newsgroup-data)
8439         articles)
8440     (while data
8441       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8442                 score)
8443         (push (gnus-data-number (car data)) articles))
8444       (setq data (cdr data)))
8445     (prog1
8446         (gnus-summary-limit articles)
8447       (gnus-summary-position-point))))
8448
8449 (defun gnus-summary-limit-to-unseen ()
8450   "Limit to unseen articles."
8451   (interactive)
8452   (prog1
8453       (gnus-summary-limit gnus-newsgroup-unseen)
8454     (gnus-summary-position-point)))
8455
8456 (defun gnus-summary-limit-include-thread (id)
8457   "Display all the hidden articles that is in the thread with ID in it.
8458 When called interactively, ID is the Message-ID of the current
8459 article."
8460   (interactive (list (mail-header-id (gnus-summary-article-header))))
8461   (let ((articles (gnus-articles-in-thread
8462                    (gnus-id-to-thread (gnus-root-id id))))
8463         ;;we REALLY want the whole thread---this prevents cut-threads
8464         ;;from removing the thread we want to include.
8465         (gnus-fetch-old-headers nil)
8466         (gnus-build-sparse-threads nil))
8467     (prog1
8468         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8469       (gnus-summary-limit-include-matching-articles
8470        "subject"
8471        (regexp-quote (gnus-simplify-subject-re
8472                       (mail-header-subject (gnus-id-to-header id)))))
8473       (gnus-summary-position-point))))
8474
8475 (defun gnus-summary-limit-include-matching-articles (header regexp)
8476   "Display all the hidden articles that have HEADERs that match REGEXP."
8477   (interactive (list (read-string "Match on header: ")
8478                      (read-string "Regexp: ")))
8479   (let ((articles (gnus-find-matching-articles header regexp)))
8480     (prog1
8481         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8482       (gnus-summary-position-point))))
8483
8484 (defun gnus-summary-insert-dormant-articles ()
8485   "Insert all the dormant articles for this group into the current buffer."
8486   (interactive)
8487   (let ((gnus-verbose (max 6 gnus-verbose)))
8488     (if (not gnus-newsgroup-dormant)
8489         (gnus-message 3 "No dormant articles for this group")
8490       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8491
8492 (defun gnus-summary-insert-ticked-articles ()
8493   "Insert ticked articles for this group into the current buffer."
8494   (interactive)
8495   (let ((gnus-verbose (max 6 gnus-verbose)))
8496     (if (not gnus-newsgroup-marked)
8497         (gnus-message 3 "No ticked articles for this group")
8498       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8499
8500 (defun gnus-summary-limit-include-dormant ()
8501   "Display all the hidden articles that are marked as dormant.
8502 Note that this command only works on a subset of the articles currently
8503 fetched for this group."
8504   (interactive)
8505   (unless gnus-newsgroup-dormant
8506     (error "There are no dormant articles in this group"))
8507   (prog1
8508       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8509     (gnus-summary-position-point)))
8510
8511 (defun gnus-summary-include-articles (articles)
8512   "Fetch the headers for ARTICLES and then display the summary lines."
8513   (let ((gnus-inhibit-demon t)
8514         (gnus-agent nil)
8515         (gnus-read-all-available-headers t))
8516     (setq gnus-newsgroup-headers
8517           (gnus-merge
8518            'list gnus-newsgroup-headers
8519            (gnus-fetch-headers articles nil t)
8520            'gnus-article-sort-by-number))
8521     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8522
8523 (defun gnus-summary-limit-exclude-dormant ()
8524   "Hide all dormant articles."
8525   (interactive)
8526   (prog1
8527       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8528     (gnus-summary-position-point)))
8529
8530 (defun gnus-summary-limit-exclude-childless-dormant ()
8531   "Hide all dormant articles that have no children."
8532   (interactive)
8533   (let ((data (gnus-data-list t))
8534         articles d children)
8535     ;; Find all articles that are either not dormant or have
8536     ;; children.
8537     (while (setq d (pop data))
8538       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8539                 (and (setq children
8540                            (gnus-article-children (gnus-data-number d)))
8541                      (let (found)
8542                        (while children
8543                          (when (memq (car children) articles)
8544                            (setq children nil
8545                                  found t))
8546                          (pop children))
8547                        found)))
8548         (push (gnus-data-number d) articles)))
8549     ;; Do the limiting.
8550     (prog1
8551         (gnus-summary-limit articles)
8552       (gnus-summary-position-point))))
8553
8554 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8555   "Mark all unread excluded articles as read.
8556 If ALL, mark even excluded ticked and dormants as read."
8557   (interactive "P")
8558   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8559   (let ((articles (gnus-sorted-ndifference
8560                    (sort
8561                     (mapcar (lambda (h) (mail-header-number h))
8562                             gnus-newsgroup-headers)
8563                     '<)
8564                    gnus-newsgroup-limit))
8565         article)
8566     (setq gnus-newsgroup-unreads
8567           (gnus-sorted-intersection gnus-newsgroup-unreads
8568                                     gnus-newsgroup-limit))
8569     (if all
8570         (setq gnus-newsgroup-dormant nil
8571               gnus-newsgroup-marked nil
8572               gnus-newsgroup-reads
8573               (nconc
8574                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8575                gnus-newsgroup-reads))
8576       (while (setq article (pop articles))
8577         (unless (or (memq article gnus-newsgroup-dormant)
8578                     (memq article gnus-newsgroup-marked))
8579           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8580
8581 (defun gnus-summary-limit (articles &optional pop)
8582   (if pop
8583       ;; We pop the previous limit off the stack and use that.
8584       (setq articles (car gnus-newsgroup-limits)
8585             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8586     ;; We use the new limit, so we push the old limit on the stack.
8587     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8588   ;; Set the limit.
8589   (setq gnus-newsgroup-limit articles)
8590   (let ((total (length gnus-newsgroup-data))
8591         (data (gnus-data-find-list (gnus-summary-article-number)))
8592         (gnus-summary-mark-below nil)   ; Inhibit this.
8593         found)
8594     ;; This will do all the work of generating the new summary buffer
8595     ;; according to the new limit.
8596     (gnus-summary-prepare)
8597     ;; Hide any threads, possibly.
8598     (gnus-summary-maybe-hide-threads)
8599     ;; Try to return to the article you were at, or one in the
8600     ;; neighborhood.
8601     (when data
8602       ;; We try to find some article after the current one.
8603       (while data
8604         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8605           (setq data nil
8606                 found t))
8607         (setq data (cdr data))))
8608     (unless found
8609       ;; If there is no data, that means that we were after the last
8610       ;; article.  The same goes when we can't find any articles
8611       ;; after the current one.
8612       (goto-char (point-max))
8613       (gnus-summary-find-prev))
8614     (gnus-set-mode-line 'summary)
8615     ;; We return how many articles were removed from the summary
8616     ;; buffer as a result of the new limit.
8617     (- total (length gnus-newsgroup-data))))
8618
8619 (defsubst gnus-invisible-cut-children (threads)
8620   (let ((num 0))
8621     (while threads
8622       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8623         (incf num))
8624       (pop threads))
8625     (< num 2)))
8626
8627 (defsubst gnus-cut-thread (thread)
8628   "Go forwards in the thread until we find an article that we want to display."
8629   (when (or (eq gnus-fetch-old-headers 'some)
8630             (eq gnus-fetch-old-headers 'invisible)
8631             (numberp gnus-fetch-old-headers)
8632             (eq gnus-build-sparse-threads 'some)
8633             (eq gnus-build-sparse-threads 'more))
8634     ;; Deal with old-fetched headers and sparse threads.
8635     (while (and
8636             thread
8637             (or
8638              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8639              (gnus-summary-article-ancient-p
8640               (mail-header-number (car thread))))
8641             (if (or (<= (length (cdr thread)) 1)
8642                     (eq gnus-fetch-old-headers 'invisible))
8643                 (setq gnus-newsgroup-limit
8644                       (delq (mail-header-number (car thread))
8645                             gnus-newsgroup-limit)
8646                       thread (cadr thread))
8647               (when (gnus-invisible-cut-children (cdr thread))
8648                 (let ((th (cdr thread)))
8649                   (while th
8650                     (if (memq (mail-header-number (caar th))
8651                               gnus-newsgroup-limit)
8652                         (setq thread (car th)
8653                               th nil)
8654                       (setq th (cdr th))))))))))
8655   thread)
8656
8657 (defun gnus-cut-threads (threads)
8658   "Cut off all uninteresting articles from the beginning of THREADS."
8659   (when (or (eq gnus-fetch-old-headers 'some)
8660             (eq gnus-fetch-old-headers 'invisible)
8661             (numberp gnus-fetch-old-headers)
8662             (eq gnus-build-sparse-threads 'some)
8663             (eq gnus-build-sparse-threads 'more))
8664     (let ((th threads))
8665       (while th
8666         (setcar th (gnus-cut-thread (car th)))
8667         (setq th (cdr th)))))
8668   ;; Remove nixed out threads.
8669   (delq nil threads))
8670
8671 (defun gnus-summary-initial-limit (&optional show-if-empty)
8672   "Figure out what the initial limit is supposed to be on group entry.
8673 This entails weeding out unwanted dormants, low-scored articles,
8674 fetch-old-headers verbiage, and so on."
8675   ;; Most groups have nothing to remove.
8676   (unless (or gnus-inhibit-limiting
8677               (and (null gnus-newsgroup-dormant)
8678                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8679                    (not (eq gnus-fetch-old-headers 'some))
8680                    (not (numberp gnus-fetch-old-headers))
8681                    (not (eq gnus-fetch-old-headers 'invisible))
8682                    (null gnus-summary-expunge-below)
8683                    (not (eq gnus-build-sparse-threads 'some))
8684                    (not (eq gnus-build-sparse-threads 'more))
8685                    (null gnus-thread-expunge-below)))
8686     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8687     (setq gnus-newsgroup-limit nil)
8688     (mapatoms
8689      (lambda (node)
8690        (unless (car (symbol-value node))
8691          ;; These threads have no parents -- they are roots.
8692          (let ((nodes (cdr (symbol-value node)))
8693                thread)
8694            (while nodes
8695              (if (and gnus-thread-expunge-below
8696                       (< (gnus-thread-total-score (car nodes))
8697                          gnus-thread-expunge-below))
8698                  (gnus-expunge-thread (pop nodes))
8699                (setq thread (pop nodes))
8700                (gnus-summary-limit-children thread))))))
8701      gnus-newsgroup-dependencies)
8702     ;; If this limitation resulted in an empty group, we might
8703     ;; pop the previous limit and use it instead.
8704     (when (and (not gnus-newsgroup-limit)
8705                show-if-empty)
8706       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8707     gnus-newsgroup-limit))
8708
8709 (defun gnus-summary-limit-children (thread)
8710   "Return 1 if this subthread is visible and 0 if it is not."
8711   ;; First we get the number of visible children to this thread.  This
8712   ;; is done by recursing down the thread using this function, so this
8713   ;; will really go down to a leaf article first, before slowly
8714   ;; working its way up towards the root.
8715   (when thread
8716     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8717            (children
8718            (if (cdr thread)
8719                (apply '+ (mapcar 'gnus-summary-limit-children
8720                                  (cdr thread)))
8721              0))
8722            (number (mail-header-number (car thread)))
8723            score)
8724       (if (and
8725            (not (memq number gnus-newsgroup-marked))
8726            (or
8727             ;; If this article is dormant and has absolutely no visible
8728             ;; children, then this article isn't visible.
8729             (and (memq number gnus-newsgroup-dormant)
8730                  (zerop children))
8731             ;; If this is "fetch-old-headered" and there is no
8732             ;; visible children, then we don't want this article.
8733             (and (or (eq gnus-fetch-old-headers 'some)
8734                      (numberp gnus-fetch-old-headers))
8735                  (gnus-summary-article-ancient-p number)
8736                  (zerop children))
8737             ;; If this is "fetch-old-headered" and `invisible', then
8738             ;; we don't want this article.
8739             (and (eq gnus-fetch-old-headers 'invisible)
8740                  (gnus-summary-article-ancient-p number))
8741             ;; If this is a sparsely inserted article with no children,
8742             ;; we don't want it.
8743             (and (eq gnus-build-sparse-threads 'some)
8744                  (gnus-summary-article-sparse-p number)
8745                  (zerop children))
8746             ;; If we use expunging, and this article is really
8747             ;; low-scored, then we don't want this article.
8748             (when (and gnus-summary-expunge-below
8749                        (< (setq score
8750                                 (or (cdr (assq number gnus-newsgroup-scored))
8751                                     gnus-summary-default-score))
8752                           gnus-summary-expunge-below))
8753               ;; We increase the expunge-tally here, but that has
8754               ;; nothing to do with the limits, really.
8755               (incf gnus-newsgroup-expunged-tally)
8756               ;; We also mark as read here, if that's wanted.
8757               (when (and gnus-summary-mark-below
8758                          (< score gnus-summary-mark-below))
8759                 (setq gnus-newsgroup-unreads
8760                       (delq number gnus-newsgroup-unreads))
8761                 (if gnus-newsgroup-auto-expire
8762                     (push number gnus-newsgroup-expirable)
8763                   (push (cons number gnus-low-score-mark)
8764                         gnus-newsgroup-reads)))
8765               t)
8766             ;; Do the `display' group parameter.
8767             (and gnus-newsgroup-display
8768                  (let ((gnus-number number))
8769                    (not (funcall gnus-newsgroup-display))))))
8770           ;; Nope, invisible article.
8771           0
8772         ;; Ok, this article is to be visible, so we add it to the limit
8773         ;; and return 1.
8774         (push number gnus-newsgroup-limit)
8775         1))))
8776
8777 (defun gnus-expunge-thread (thread)
8778   "Mark all articles in THREAD as read."
8779   (let* ((number (mail-header-number (car thread))))
8780     (incf gnus-newsgroup-expunged-tally)
8781     ;; We also mark as read here, if that's wanted.
8782     (setq gnus-newsgroup-unreads
8783           (delq number gnus-newsgroup-unreads))
8784     (if gnus-newsgroup-auto-expire
8785         (push number gnus-newsgroup-expirable)
8786       (push (cons number gnus-low-score-mark)
8787             gnus-newsgroup-reads)))
8788   ;; Go recursively through all subthreads.
8789   (mapcar 'gnus-expunge-thread (cdr thread)))
8790
8791 ;; Summary article oriented commands
8792
8793 (defun gnus-summary-refer-parent-article (n)
8794   "Refer parent article N times.
8795 If N is negative, go to ancestor -N instead.
8796 The difference between N and the number of articles fetched is returned."
8797   (interactive "p")
8798   (let ((skip 1)
8799         error header ref)
8800     (when (not (natnump n))
8801       (setq skip (abs n)
8802             n 1))
8803     (while (and (> n 0)
8804                 (not error))
8805       (setq header (gnus-summary-article-header))
8806       (if (and (eq (mail-header-number header)
8807                    (cdr gnus-article-current))
8808                (equal gnus-newsgroup-name
8809                       (car gnus-article-current)))
8810           ;; If we try to find the parent of the currently
8811           ;; displayed article, then we take a look at the actual
8812           ;; References header, since this is slightly more
8813           ;; reliable than the References field we got from the
8814           ;; server.
8815           (with-current-buffer gnus-original-article-buffer
8816             (nnheader-narrow-to-headers)
8817             (unless (setq ref (message-fetch-field "references"))
8818               (when (setq ref (message-fetch-field "in-reply-to"))
8819                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8820             (widen))
8821         (setq ref
8822               ;; It's not the current article, so we take a bet on
8823               ;; the value we got from the server.
8824               (mail-header-references header)))
8825       (if (and ref
8826                (not (equal ref "")))
8827           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8828             (gnus-message 1 "Couldn't find parent"))
8829         (gnus-message 1 "No references in article %d"
8830                       (gnus-summary-article-number))
8831         (setq error t))
8832       (decf n))
8833     (gnus-summary-position-point)
8834     n))
8835
8836 (defun gnus-summary-refer-references ()
8837   "Fetch all articles mentioned in the References header.
8838 Return the number of articles fetched."
8839   (interactive)
8840   (let ((ref (mail-header-references (gnus-summary-article-header)))
8841         (current (gnus-summary-article-number))
8842         (n 0))
8843     (if (or (not ref)
8844             (equal ref ""))
8845         (error "No References in the current article")
8846       ;; For each Message-ID in the References header...
8847       (while (string-match "<[^>]*>" ref)
8848         (incf n)
8849         ;; ... fetch that article.
8850         (gnus-summary-refer-article
8851          (prog1 (match-string 0 ref)
8852            (setq ref (substring ref (match-end 0))))))
8853       (gnus-summary-goto-subject current)
8854       (gnus-summary-position-point)
8855       n)))
8856
8857 (defun gnus-summary-refer-thread (&optional limit)
8858   "Fetch all articles in the current thread.
8859 If no backend-specific 'request-thread function is available
8860 fetch LIMIT (the numerical prefix) old headers. If LIMIT is nil
8861 fetch what's specified by the `gnus-refer-thread-limit'
8862 variable."
8863   (interactive "P")
8864   (gnus-warp-to-article)
8865   (let* ((header (gnus-summary-article-header))
8866          (id (mail-header-id header))
8867          (gnus-inhibit-demon t)
8868          (gnus-summary-ignore-duplicates t)
8869          (gnus-read-all-available-headers t)
8870          (limit (if limit (prefix-numeric-value limit)
8871                   gnus-refer-thread-limit)))
8872     (setq gnus-newsgroup-headers
8873           (gnus-merge
8874            'list gnus-newsgroup-headers
8875            (if (gnus-check-backend-function
8876                 'request-thread gnus-newsgroup-name)
8877                (gnus-request-thread header)
8878              (let* ((last (if (numberp limit)
8879                               (min (+ (mail-header-number header)
8880                                       limit)
8881                                    gnus-newsgroup-highest)
8882                             gnus-newsgroup-highest))
8883                     (subject (gnus-simplify-subject
8884                               (mail-header-subject header)))
8885                     (refs (split-string (or (mail-header-references header)
8886                                             "")))
8887                     (gnus-parse-headers-hook
8888                      (lambda () (goto-char (point-min))
8889                        (keep-lines
8890                         (regexp-opt (append refs (list id subject)))))))
8891                (gnus-fetch-headers (list last) (if (numberp limit)
8892                                                    (* 2 limit) limit) t)))
8893            'gnus-article-sort-by-number))
8894     (gnus-summary-limit-include-thread id)))
8895
8896 (defun gnus-summary-refer-article (message-id)
8897   "Fetch an article specified by MESSAGE-ID."
8898   (interactive "sMessage-ID: ")
8899   (when (and (stringp message-id)
8900              (not (zerop (length message-id))))
8901     (setq message-id (gnus-replace-in-string message-id " " ""))
8902     ;; Construct the correct Message-ID if necessary.
8903     ;; Suggested by tale@pawl.rpi.edu.
8904     (unless (string-match "^<" message-id)
8905       (setq message-id (concat "<" message-id)))
8906     (unless (string-match ">$" message-id)
8907       (setq message-id (concat message-id ">")))
8908     ;; People often post MIDs from URLs, so unhex it:
8909     (unless (string-match "@" message-id)
8910       (setq message-id (gnus-url-unhex-string message-id)))
8911     (let* ((header (gnus-id-to-header message-id))
8912            (sparse (and header
8913                         (gnus-summary-article-sparse-p
8914                          (mail-header-number header))
8915                         (memq (mail-header-number header)
8916                               gnus-newsgroup-limit)))
8917            number)
8918       (cond
8919        ;; If the article is present in the buffer we just go to it.
8920        ((and header
8921              (or (not (gnus-summary-article-sparse-p
8922                        (mail-header-number header)))
8923                  sparse))
8924         (prog1
8925             (gnus-summary-goto-article
8926              (mail-header-number header) nil t)
8927           (when sparse
8928             (gnus-summary-update-article (mail-header-number header)))))
8929        (t
8930         ;; We fetch the article.
8931         (catch 'found
8932           (dolist (gnus-override-method (gnus-refer-article-methods))
8933             (when (and (gnus-check-server gnus-override-method)
8934                        ;; Fetch the header,
8935                        (setq number (gnus-summary-insert-subject message-id)))
8936               ;; and display the article.
8937               (gnus-summary-select-article nil nil nil number)
8938               (throw 'found t)))
8939           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8940
8941 (defun gnus-refer-article-methods ()
8942   "Return a list of referable methods."
8943   (cond
8944    ;; No method, so we default to current and native.
8945    ((null gnus-refer-article-method)
8946     (list gnus-current-select-method gnus-select-method))
8947    ;; Current.
8948    ((eq 'current gnus-refer-article-method)
8949     (list gnus-current-select-method))
8950    ;; List of select methods.
8951    ((not (and (symbolp (car gnus-refer-article-method))
8952               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8953     (let (out)
8954       (dolist (method gnus-refer-article-method)
8955         (push (if (eq 'current method)
8956                   gnus-current-select-method
8957                 method)
8958               out))
8959       (nreverse out)))
8960    ;; One single select method.
8961    (t
8962     (list gnus-refer-article-method))))
8963
8964 (defun gnus-summary-edit-parameters ()
8965   "Edit the group parameters of the current group."
8966   (interactive)
8967   (gnus-group-edit-group gnus-newsgroup-name 'params))
8968
8969 (defun gnus-summary-customize-parameters ()
8970   "Customize the group parameters of the current group."
8971   (interactive)
8972   (gnus-group-customize gnus-newsgroup-name))
8973
8974 (defun gnus-summary-enter-digest-group (&optional force)
8975   "Enter an nndoc group based on the current article.
8976 If FORCE, force a digest interpretation.  If not, try to guess
8977 what the document format is.
8978
8979 To control what happens when you exit the group, see the
8980 `gnus-auto-select-on-ephemeral-exit' variable."
8981   (interactive "P")
8982   (let ((conf gnus-current-window-configuration))
8983     (save-window-excursion
8984       (save-excursion
8985         (let (gnus-article-prepare-hook
8986               gnus-display-mime-function
8987               gnus-break-pages)
8988           (gnus-summary-select-article))))
8989     (setq gnus-current-window-configuration conf)
8990     (let* ((name (format "%s-%d"
8991                          (gnus-group-prefixed-name
8992                           gnus-newsgroup-name (list 'nndoc ""))
8993                          (with-current-buffer gnus-summary-buffer
8994                            gnus-current-article)))
8995            (ogroup gnus-newsgroup-name)
8996            (params (append (gnus-info-params (gnus-get-info ogroup))
8997                            (list (cons 'to-group ogroup))
8998                            (list (cons 'parent-group ogroup))
8999                            (list (cons 'save-article-group ogroup))))
9000            (case-fold-search t)
9001            (buf (current-buffer))
9002            dig to-address)
9003       (with-current-buffer gnus-original-article-buffer
9004         ;; Have the digest group inherit the main mail address of
9005         ;; the parent article.
9006         (when (setq to-address (or (gnus-fetch-field "reply-to")
9007                                    (gnus-fetch-field "from")))
9008           (setq params
9009                 (append
9010                  (list (cons 'to-address
9011                              (funcall gnus-decode-encoded-address-function
9012                                       to-address))))))
9013         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9014         (insert-buffer-substring gnus-original-article-buffer)
9015         ;; Remove lines that may lead nndoc to misinterpret the
9016         ;; document type.
9017         (narrow-to-region
9018          (goto-char (point-min))
9019          (or (search-forward "\n\n" nil t) (point)))
9020         (goto-char (point-min))
9021         (delete-matching-lines "^Path:\\|^From ")
9022         (widen))
9023       (unwind-protect
9024           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9025                     (gnus-newsgroup-ephemeral-ignored-charsets
9026                      gnus-newsgroup-ignored-charsets))
9027                 (gnus-group-read-ephemeral-group
9028                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9029                               (nndoc-article-type
9030                                ,(if force 'mbox 'guess)))
9031                  t nil nil nil
9032                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9033                                                         "ADAPT")))))
9034               ;; Make all postings to this group go to the parent group.
9035               (nconc (gnus-info-params (gnus-get-info name))
9036                      params)
9037             ;; Couldn't select this doc group.
9038             (switch-to-buffer buf)
9039             (gnus-set-global-variables)
9040             (gnus-configure-windows 'summary)
9041             (gnus-message 3 "Article couldn't be entered?"))
9042         (kill-buffer dig)))))
9043
9044 (defun gnus-summary-read-document (n)
9045   "Open a new group based on the current article(s).
9046 This will allow you to read digests and other similar
9047 documents as newsgroups.
9048 Obeys the standard process/prefix convention."
9049   (interactive "P")
9050   (let* ((ogroup gnus-newsgroup-name)
9051          (params (append (gnus-info-params (gnus-get-info ogroup))
9052                          (list (cons 'to-group ogroup))))
9053          group egroup groups vgroup)
9054     (dolist (article (gnus-summary-work-articles n))
9055       (setq group (format "%s-%d" gnus-newsgroup-name article))
9056       (gnus-summary-remove-process-mark article)
9057       (when (gnus-summary-display-article article)
9058         (save-excursion ;;What for?
9059           (with-temp-buffer
9060             (insert-buffer-substring gnus-original-article-buffer)
9061             ;; Remove some headers that may lead nndoc to make
9062             ;; the wrong guess.
9063             (message-narrow-to-head)
9064             (goto-char (point-min))
9065             (delete-matching-lines "^Path:\\|^From ")
9066             (widen)
9067             (if (setq egroup
9068                       (gnus-group-read-ephemeral-group
9069                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9070                                      (nndoc-article-type guess))
9071                        t nil t))
9072                 (progn
9073                   ;; Make all postings to this group go to the parent group.
9074                   (nconc (gnus-info-params (gnus-get-info egroup))
9075                          params)
9076                   (push egroup groups))
9077               ;; Couldn't select this doc group.
9078               (gnus-error 3 "Article couldn't be entered"))))))
9079     ;; Now we have selected all the documents.
9080     (cond
9081      ((not groups)
9082       (error "None of the articles could be interpreted as documents"))
9083      ((gnus-group-read-ephemeral-group
9084        (setq vgroup (format
9085                      "nnvirtual:%s-%s" gnus-newsgroup-name
9086                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9087        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9088        t
9089        (cons (current-buffer) 'summary)))
9090      (t
9091       (error "Couldn't select virtual nndoc group")))))
9092
9093 (defun gnus-summary-widget-forward (arg)
9094   "Move point to the next field or button in the article.
9095 With optional ARG, move across that many fields."
9096   (interactive "p")
9097   (gnus-summary-select-article)
9098   (gnus-configure-windows 'article)
9099   (select-window (gnus-get-buffer-window gnus-article-buffer))
9100   (widget-forward arg))
9101
9102 (defun gnus-summary-isearch-article (&optional regexp-p)
9103   "Do incremental search forward on the current article.
9104 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9105   (interactive "P")
9106   (gnus-summary-select-article)
9107   (gnus-configure-windows 'article)
9108   (gnus-eval-in-buffer-window gnus-article-buffer
9109     (save-restriction
9110       (widen)
9111       (isearch-forward regexp-p))))
9112
9113 (defun gnus-summary-repeat-search-article-forward ()
9114   "Repeat the previous search forwards."
9115   (interactive)
9116   (unless gnus-last-search-regexp
9117     (error "No previous search"))
9118   (gnus-summary-search-article-forward gnus-last-search-regexp))
9119
9120 (defun gnus-summary-repeat-search-article-backward ()
9121   "Repeat the previous search backwards."
9122   (interactive)
9123   (unless gnus-last-search-regexp
9124     (error "No previous search"))
9125   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9126
9127 (defun gnus-summary-search-article-forward (regexp &optional backward)
9128   "Search for an article containing REGEXP forward.
9129 If BACKWARD, search backward instead."
9130   (interactive
9131    (list (read-string
9132           (format "Search article %s (regexp%s): "
9133                   (if current-prefix-arg "backward" "forward")
9134                   (if gnus-last-search-regexp
9135                       (concat ", default " gnus-last-search-regexp)
9136                     "")))
9137          current-prefix-arg))
9138   (if (string-equal regexp "")
9139       (setq regexp (or gnus-last-search-regexp ""))
9140     (setq gnus-last-search-regexp regexp)
9141     (setq gnus-article-before-search gnus-current-article))
9142   ;; Intentionally set gnus-last-article.
9143   (setq gnus-last-article gnus-article-before-search)
9144   (let ((gnus-last-article gnus-last-article))
9145     (if (gnus-summary-search-article regexp backward)
9146         (gnus-summary-show-thread)
9147       (signal 'search-failed (list regexp)))))
9148
9149 (defun gnus-summary-search-article-backward (regexp)
9150   "Search for an article containing REGEXP backward."
9151   (interactive
9152    (list (read-string
9153           (format "Search article backward (regexp%s): "
9154                   (if gnus-last-search-regexp
9155                       (concat ", default " gnus-last-search-regexp)
9156                     "")))))
9157   (gnus-summary-search-article-forward regexp 'backward))
9158
9159 (defun gnus-summary-search-article (regexp &optional backward)
9160   "Search for an article containing REGEXP.
9161 Optional argument BACKWARD means do search for backward.
9162 `gnus-select-article-hook' is not called during the search."
9163   ;; We have to require this here to make sure that the following
9164   ;; dynamic binding isn't shadowed by autoloading.
9165   (require 'gnus-async)
9166   (require 'gnus-art)
9167   (let ((gnus-select-article-hook nil)  ;Disable hook.
9168         (gnus-article-prepare-hook nil)
9169         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9170         (gnus-use-article-prefetch nil)
9171         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9172         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9173         (gnus-visual nil)
9174         (gnus-keep-backlog nil)
9175         (gnus-break-pages nil)
9176         (gnus-summary-display-arrow nil)
9177         (gnus-updated-mode-lines nil)
9178         (gnus-auto-center-summary nil)
9179         (sum (current-buffer))
9180         (gnus-display-mime-function nil)
9181         (found nil)
9182         point)
9183     (gnus-save-hidden-threads
9184       (gnus-summary-select-article)
9185       (set-buffer gnus-article-buffer)
9186       (goto-char (window-point (get-buffer-window (current-buffer))))
9187       (when backward
9188         (forward-line -1))
9189       (while (not found)
9190         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9191         (if (if backward
9192                 (re-search-backward regexp nil t)
9193               (re-search-forward regexp nil t))
9194             ;; We found the regexp.
9195             (progn
9196               (setq found 'found)
9197               (beginning-of-line)
9198               (set-window-start
9199                (get-buffer-window (current-buffer))
9200                (point))
9201               (forward-line 1)
9202               (set-window-point
9203                (get-buffer-window (current-buffer))
9204                (point))
9205               (set-buffer sum)
9206               (setq point (point)))
9207           ;; We didn't find it, so we go to the next article.
9208           (set-buffer sum)
9209           (setq found 'not)
9210           (while (eq found 'not)
9211             (if (not (if backward (gnus-summary-find-prev)
9212                        (gnus-summary-find-next)))
9213                 ;; No more articles.
9214                 (setq found t)
9215               ;; Select the next article and adjust point.
9216               (unless (gnus-summary-article-sparse-p
9217                        (gnus-summary-article-number))
9218                 (setq found nil)
9219                 (gnus-summary-select-article)
9220                 (set-buffer gnus-article-buffer)
9221                 (widen)
9222                 (goto-char (if backward (point-max) (point-min))))))))
9223       (gnus-message 7 ""))
9224     ;; Return whether we found the regexp.
9225     (when (eq found 'found)
9226       (goto-char point)
9227       (gnus-summary-show-thread)
9228       (gnus-summary-goto-subject gnus-current-article)
9229       (gnus-summary-position-point)
9230       t)))
9231
9232 (defun gnus-find-matching-articles (header regexp)
9233   "Return a list of all articles that match REGEXP on HEADER.
9234 This search includes all articles in the current group that Gnus has
9235 fetched headers for, whether they are displayed or not."
9236   (let ((articles nil)
9237         ;; Can't eta-reduce because it's a macro.
9238         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9239         (case-fold-search t))
9240     (dolist (header gnus-newsgroup-headers)
9241       (when (string-match regexp (funcall func header))
9242         (push (mail-header-number header) articles)))
9243     (nreverse articles)))
9244
9245 (defun gnus-summary-find-matching (header regexp &optional backward unread
9246                                           not-case-fold not-matching)
9247   "Return a list of all articles that match REGEXP on HEADER.
9248 The search stars on the current article and goes forwards unless
9249 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9250 If UNREAD is non-nil, only unread articles will
9251 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9252 in the comparisons. If NOT-MATCHING, return a list of all articles that
9253 not match REGEXP on HEADER."
9254   (let ((case-fold-search (not not-case-fold))
9255         articles d func)
9256     (if (consp header)
9257         (if (eq (car header) 'extra)
9258             (setq func
9259                   `(lambda (h)
9260                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9261                          "")))
9262           (error "%s is an invalid header" header))
9263       (unless (fboundp (intern (concat "mail-header-" header)))
9264         (error "%s is not a valid header" header))
9265       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9266     (dolist (d (if (eq backward 'all)
9267                    gnus-newsgroup-data
9268                  (gnus-data-find-list
9269                   (gnus-summary-article-number)
9270                   (gnus-data-list backward))))
9271       (when (and (or (not unread)       ; We want all articles...
9272                      (gnus-data-unread-p d)) ; Or just unreads.
9273                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9274                  (if not-matching
9275                      (not (string-match
9276                            regexp
9277                            (funcall func (gnus-data-header d))))
9278                    (string-match regexp
9279                                  (funcall func (gnus-data-header d)))))
9280         (push (gnus-data-number d) articles))) ; Success!
9281     (nreverse articles)))
9282
9283 (defun gnus-summary-execute-command (header regexp command &optional backward)
9284   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9285 If HEADER is an empty string (or nil), the match is done on the entire
9286 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9287   (interactive
9288    (list (let ((completion-ignore-case t))
9289            (gnus-completing-read
9290             "Header name"
9291             (mapcar 'symbol-name
9292                     (append
9293                      '(Number Subject From Lines Date
9294                        Message-ID Xref References Body)
9295                      gnus-extra-headers))
9296             'require-match))
9297          (read-string "Regexp: ")
9298          (read-key-sequence "Command: ")
9299          current-prefix-arg))
9300   (when (equal header "Body")
9301     (setq header ""))
9302   ;; Hidden thread subtrees must be searched as well.
9303   (gnus-summary-show-all-threads)
9304   ;; We don't want to change current point nor window configuration.
9305   (save-excursion
9306     (save-window-excursion
9307       (let (gnus-visual
9308             gnus-treat-strip-trailing-blank-lines
9309             gnus-treat-strip-leading-blank-lines
9310             gnus-treat-strip-multiple-blank-lines
9311             gnus-treat-hide-boring-headers
9312             gnus-treat-fold-newsgroups
9313             gnus-article-prepare-hook)
9314         (gnus-message 6 "Executing %s..." (key-description command))
9315         ;; We'd like to execute COMMAND interactively so as to give arguments.
9316         (gnus-execute header regexp
9317                       `(call-interactively ',(key-binding command))
9318                       backward)
9319         (gnus-message 6 "Executing %s...done" (key-description command))))))
9320
9321 (defun gnus-summary-beginning-of-article ()
9322   "Scroll the article back to the beginning."
9323   (interactive)
9324   (gnus-summary-select-article)
9325   (gnus-configure-windows 'article)
9326   (gnus-eval-in-buffer-window gnus-article-buffer
9327     (widen)
9328     (goto-char (point-min))
9329     (when gnus-break-pages
9330       (gnus-narrow-to-page))))
9331
9332 (defun gnus-summary-end-of-article ()
9333   "Scroll to the end of the article."
9334   (interactive)
9335   (gnus-summary-select-article)
9336   (gnus-configure-windows 'article)
9337   (gnus-eval-in-buffer-window gnus-article-buffer
9338     (widen)
9339     (goto-char (point-max))
9340     (recenter -3)
9341     (when gnus-break-pages
9342       (gnus-narrow-to-page))))
9343
9344 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9345   "Truncate to LEN and quote all \"(\"'s in STRING."
9346   (gnus-replace-in-string (if (and len (> (length string) len))
9347                               (substring string 0 len)
9348                             string)
9349                           "[()]" "\\\\\\&"))
9350
9351 (defun gnus-summary-print-article (&optional filename n)
9352   "Generate and print a PostScript image of the process-marked (mail) articles.
9353
9354 If used interactively, print the current article if none are
9355 process-marked.  With prefix arg, prompt the user for the name of the
9356 file to save in.
9357
9358 When used from Lisp, accept two optional args FILENAME and N.  N means
9359 to print the next N articles.  If N is negative, print the N previous
9360 articles.  If N is nil and articles have been marked with the process
9361 mark, print these instead.
9362
9363 If the optional first argument FILENAME is nil, send the image to the
9364 printer.  If FILENAME is a string, save the PostScript image in a file with
9365 that name.  If FILENAME is a number, prompt the user for the name of the file
9366 to save in."
9367   (interactive (list (ps-print-preprint current-prefix-arg)))
9368   (dolist (article (gnus-summary-work-articles n))
9369     (gnus-summary-select-article nil nil 'pseudo article)
9370     (gnus-eval-in-buffer-window gnus-article-buffer
9371       (gnus-print-buffer))
9372     (gnus-summary-remove-process-mark article))
9373   (ps-despool filename))
9374
9375 (defun gnus-print-buffer ()
9376   (let ((ps-left-header
9377          (list
9378           (concat "("
9379                   (gnus-summary-print-truncate-and-quote
9380                    (mail-header-subject gnus-current-headers)
9381                    66) ")")
9382           (concat "("
9383                   (gnus-summary-print-truncate-and-quote
9384                    (mail-header-from gnus-current-headers)
9385                    45) ")")))
9386         (ps-right-header
9387          (list
9388           "/pagenumberstring load"
9389           (concat "("
9390                   (mail-header-date gnus-current-headers) ")"))))
9391     (gnus-run-hooks 'gnus-ps-print-hook)
9392     (save-excursion
9393       (if ps-print-color-p
9394           (ps-spool-buffer-with-faces)
9395         (ps-spool-buffer)))))
9396
9397 (defun gnus-summary-show-complete-article ()
9398   "Show a complete version of the current article.
9399 This is only useful if you're looking at a partial version of the
9400 article currently."
9401   (interactive)
9402   (let ((gnus-keep-backlog nil)
9403         (gnus-use-cache nil)
9404         (gnus-agent nil)
9405         (variable (intern
9406                    (format "%s-fetch-partial-articles"
9407                            (car (gnus-find-method-for-group
9408                                  gnus-newsgroup-name)))
9409                    obarray))
9410         old-val)
9411     (unwind-protect
9412         (progn
9413           (setq old-val (symbol-value variable))
9414           (set variable nil)
9415           (gnus-flush-original-article-buffer)
9416           (gnus-summary-show-article))
9417       (set variable old-val))))
9418
9419 (defun gnus-summary-show-article (&optional arg)
9420   "Force redisplaying of the current article.
9421 If ARG (the prefix) is a number, show the article with the charset
9422 defined in `gnus-summary-show-article-charset-alist', or the charset
9423 input.
9424 If ARG (the prefix) is non-nil and not a number, show the article,
9425 but without running any of the article treatment functions
9426 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9427 C-u g', show the raw article."
9428   (interactive "P")
9429   (cond
9430    ((numberp arg)
9431     (gnus-summary-show-article t)
9432     (let ((gnus-newsgroup-charset
9433            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9434                (mm-read-coding-system
9435                 "View as charset: " ;; actually it is coding system.
9436                 (with-current-buffer gnus-article-buffer
9437                   (mm-detect-coding-region (point) (point-max))))))
9438           (gnus-newsgroup-ignored-charsets 'gnus-all))
9439       (gnus-summary-select-article nil 'force)
9440       (let ((deps gnus-newsgroup-dependencies)
9441             head header lines)
9442         (with-current-buffer gnus-original-article-buffer
9443           (save-restriction
9444             (message-narrow-to-head)
9445             (setq head (buffer-string))
9446             (goto-char (point-min))
9447             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9448               (goto-char (point-max))
9449               (widen)
9450               (setq lines (1- (count-lines (point) (point-max))))))
9451           (with-temp-buffer
9452             (insert (format "211 %d Article retrieved.\n"
9453                             (cdr gnus-article-current)))
9454             (insert head)
9455             (if lines (insert (format "Lines: %d\n" lines)))
9456             (insert ".\n")
9457             (let ((nntp-server-buffer (current-buffer)))
9458               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9459         (gnus-data-set-header
9460          (gnus-data-find (cdr gnus-article-current))
9461          header)
9462         (gnus-summary-update-article-line
9463          (cdr gnus-article-current) header)
9464         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9465           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9466    ((not arg)
9467     ;; Select the article the normal way.
9468     (gnus-summary-select-article nil 'force))
9469    ((or (equal arg '(16))
9470         (eq arg t))
9471     ;; C-u C-u g
9472     (let ((gnus-inhibit-article-treatments t))
9473       (gnus-summary-select-article nil 'force)))
9474    (t
9475     ;; We have to require this here to make sure that the following
9476     ;; dynamic binding isn't shadowed by autoloading.
9477     (require 'gnus-async)
9478     (require 'gnus-art)
9479     ;; Bind the article treatment functions to nil.
9480     (let ((gnus-have-all-headers t)
9481           gnus-article-prepare-hook
9482           gnus-article-decode-hook
9483           gnus-display-mime-function
9484           gnus-break-pages)
9485       ;; Destroy any MIME parts.
9486       (when (gnus-buffer-live-p gnus-article-buffer)
9487         (with-current-buffer gnus-article-buffer
9488           (mm-destroy-parts gnus-article-mime-handles)
9489           ;; Set it to nil for safety reason.
9490           (setq gnus-article-mime-handle-alist nil)
9491           (setq gnus-article-mime-handles nil)))
9492       (gnus-summary-select-article nil 'force))))
9493   (gnus-summary-goto-subject gnus-current-article)
9494   (gnus-summary-position-point))
9495
9496 (defun gnus-summary-show-raw-article ()
9497   "Show the raw article without any article massaging functions being run."
9498   (interactive)
9499   (gnus-summary-show-article t))
9500
9501 (defun gnus-summary-verbose-headers (&optional arg)
9502   "Toggle permanent full header display.
9503 If ARG is a positive number, turn header display on.
9504 If ARG is a negative number, turn header display off."
9505   (interactive "P")
9506   (setq gnus-show-all-headers
9507         (cond ((or (not (numberp arg))
9508                    (zerop arg))
9509                (not gnus-show-all-headers))
9510               ((natnump arg)
9511                t)))
9512   (gnus-summary-show-article))
9513
9514 (defun gnus-summary-toggle-header (&optional arg)
9515   "Show the headers if they are hidden, or hide them if they are shown.
9516 If ARG is a positive number, show the entire header.
9517 If ARG is a negative number, hide the unwanted header lines."
9518   (interactive "P")
9519   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9520                      (get-buffer-window gnus-article-buffer t))))
9521     (with-current-buffer gnus-article-buffer
9522       (widen)
9523       (article-narrow-to-head)
9524       (let* ((inhibit-read-only t)
9525              (inhibit-point-motion-hooks t)
9526              (hidden (if (numberp arg)
9527                          (>= arg 0)
9528                        (or (not (looking-at "[^ \t\n]+:"))
9529                            (gnus-article-hidden-text-p 'headers))))
9530              s e)
9531         (delete-region (point-min) (point-max))
9532         (with-current-buffer gnus-original-article-buffer
9533           (goto-char (setq s (point-min)))
9534           (setq e (if (search-forward "\n\n" nil t)
9535                       (1- (point))
9536                     (point-max))))
9537         (insert-buffer-substring gnus-original-article-buffer s e)
9538         (run-hooks 'gnus-article-decode-hook)
9539         (if hidden
9540             (let ((gnus-treat-hide-headers nil)
9541                   (gnus-treat-hide-boring-headers nil))
9542               (gnus-delete-wash-type 'headers)
9543               (gnus-treat-article 'head))
9544           (gnus-treat-article 'head))
9545         (widen)
9546         (if window
9547             (set-window-start window (goto-char (point-min))))
9548         (if gnus-break-pages
9549             (gnus-narrow-to-page)
9550           (when (gnus-visual-p 'page-marker)
9551             (let ((inhibit-read-only t))
9552               (gnus-remove-text-with-property 'gnus-prev)
9553               (gnus-remove-text-with-property 'gnus-next))))
9554         (gnus-set-mode-line 'article)))))
9555
9556 (defun gnus-summary-show-all-headers ()
9557   "Make all header lines visible."
9558   (interactive)
9559   (gnus-summary-toggle-header 1))
9560
9561 (defun gnus-summary-caesar-message (&optional arg)
9562   "Caesar rotate the current article by 13.
9563 With a non-numerical prefix, also rotate headers.  A numerical
9564 prefix specifies how many places to rotate each letter forward."
9565   (interactive "P")
9566   (gnus-summary-select-article)
9567   (let ((mail-header-separator ""))
9568     (gnus-eval-in-buffer-window gnus-article-buffer
9569       (save-restriction
9570         (widen)
9571         (let ((start (window-start))
9572               (inhibit-read-only t))
9573           (if (equal arg '(4))
9574               (message-caesar-buffer-body nil t)
9575             (message-caesar-buffer-body arg))
9576           (set-window-start (get-buffer-window (current-buffer)) start)))))
9577   ;; Create buttons and stuff...
9578   (gnus-treat-article nil))
9579
9580 (declare-function idna-to-unicode "ext:idna" (str))
9581
9582 (defun gnus-summary-idna-message (&optional arg)
9583   "Decode IDNA encoded domain names in the current articles.
9584 IDNA encoded domain names looks like `xn--bar'.  If a string
9585 remain unencoded after running this function, it is likely an
9586 invalid IDNA string (`xn--bar' is invalid).
9587
9588 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9589 installed for this command to work."
9590   (interactive "P")
9591   (if (not (and (condition-case nil (require 'idna)
9592                   (file-error))
9593                 (mm-coding-system-p 'utf-8)
9594                 (executable-find (symbol-value 'idna-program))))
9595       (gnus-message
9596        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9597     (gnus-summary-select-article)
9598     (let ((mail-header-separator ""))
9599       (gnus-eval-in-buffer-window gnus-article-buffer
9600         (save-restriction
9601           (widen)
9602           (let ((start (window-start))
9603                 buffer-read-only)
9604             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9605               (replace-match (idna-to-unicode (match-string 1))))
9606             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9607
9608 (defun gnus-summary-morse-message (&optional arg)
9609   "Morse decode the current article."
9610   (interactive "P")
9611   (gnus-summary-select-article)
9612   (let ((mail-header-separator ""))
9613     (gnus-eval-in-buffer-window gnus-article-buffer
9614       (save-excursion
9615         (save-restriction
9616           (widen)
9617           (let ((pos (window-start))
9618                 (inhibit-read-only t))
9619             (goto-char (point-min))
9620             (when (message-goto-body)
9621               (gnus-narrow-to-body))
9622             (goto-char (point-min))
9623             (while (search-forward "·" (point-max) t)
9624               (replace-match "."))
9625             (unmorse-region (point-min) (point-max))
9626             (widen)
9627             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9628
9629 (defun gnus-summary-stop-page-breaking ()
9630   "Stop page breaking in the current article."
9631   (interactive)
9632   (gnus-summary-select-article)
9633   (gnus-eval-in-buffer-window gnus-article-buffer
9634     (widen)
9635     (when (gnus-visual-p 'page-marker)
9636       (let ((inhibit-read-only t))
9637         (gnus-remove-text-with-property 'gnus-prev)
9638         (gnus-remove-text-with-property 'gnus-next))
9639       (setq gnus-page-broken nil))))
9640
9641 (defun gnus-summary-move-article (&optional n to-newsgroup
9642                                             select-method action)
9643   "Move the current article to a different newsgroup.
9644 If N is a positive number, move the N next articles.
9645 If N is a negative number, move the N previous articles.
9646 If N is nil and any articles have been marked with the process mark,
9647 move those articles instead.
9648 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9649 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9650 re-spool using this method.
9651
9652 When called interactively with TO-NEWSGROUP being nil, the value of
9653 the variable `gnus-move-split-methods' is used for finding a default
9654 for the target newsgroup.
9655
9656 For this function to work, both the current newsgroup and the
9657 newsgroup that you want to move to have to support the `request-move'
9658 and `request-accept' functions.
9659
9660 ACTION can be either `move' (the default), `crosspost' or `copy'."
9661   (interactive "P")
9662   (unless action
9663     (setq action 'move))
9664   ;; Check whether the source group supports the required functions.
9665   (cond ((and (eq action 'move)
9666               (not (gnus-check-backend-function
9667                     'request-move-article gnus-newsgroup-name)))
9668          (error "The current group does not support article moving"))
9669         ((and (eq action 'crosspost)
9670               (not (gnus-check-backend-function
9671                     'request-replace-article gnus-newsgroup-name)))
9672          (error "The current group does not support article editing")))
9673   (let ((articles (gnus-summary-work-articles n))
9674         (prefix (if (gnus-check-backend-function
9675                      'request-move-article gnus-newsgroup-name)
9676                     (funcall gnus-move-group-prefix-function
9677                              gnus-newsgroup-name)
9678                   ""))
9679         (names '((move "Move" "Moving")
9680                  (copy "Copy" "Copying")
9681                  (crosspost "Crosspost" "Crossposting")))
9682         (copy-buf (save-excursion
9683                     (nnheader-set-temp-buffer " *copy article*")))
9684         art-group to-method new-xref article to-groups
9685         articles-to-update-marks encoded)
9686     (unless (assq action names)
9687       (error "Unknown action %s" action))
9688     ;; Read the newsgroup name.
9689     (when (and (not to-newsgroup)
9690                (not select-method))
9691       (if (and gnus-move-split-methods
9692                (not
9693                 (and (memq gnus-current-article articles)
9694                      (gnus-buffer-live-p gnus-original-article-buffer))))
9695           ;; When `gnus-move-split-methods' is non-nil, we have to
9696           ;; select an article to give `gnus-read-move-group-name' an
9697           ;; opportunity to suggest an appropriate default.  However,
9698           ;; we needn't render or mark the article.
9699           (let ((gnus-display-mime-function nil)
9700                 (gnus-article-prepare-hook nil)
9701                 (gnus-mark-article-hook nil))
9702             (gnus-summary-select-article nil nil nil (car articles))))
9703       (setq to-newsgroup (gnus-read-move-group-name
9704                           (cadr (assq action names))
9705                           (symbol-value
9706                            (intern (format "gnus-current-%s-group" action)))
9707                           articles prefix)
9708             encoded to-newsgroup
9709             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9710       (set (intern (format "gnus-current-%s-group" action))
9711            (mm-decode-coding-string
9712             to-newsgroup
9713             (gnus-group-name-charset to-method to-newsgroup))))
9714     (unless to-method
9715       (setq to-method (or select-method
9716                           (gnus-server-to-method
9717                            (gnus-group-method to-newsgroup)))))
9718     (setq to-newsgroup
9719           (or encoded
9720               (and to-newsgroup
9721                    (mm-encode-coding-string
9722                     to-newsgroup
9723                     (gnus-group-name-charset to-method to-newsgroup)))))
9724     ;; Check the method we are to move this article to...
9725     (unless (gnus-check-backend-function
9726              'request-accept-article (car to-method))
9727       (error "%s does not support article copying" (car to-method)))
9728     (unless (gnus-check-server to-method)
9729       (error "Can't open server %s" (car to-method)))
9730     (gnus-message 6 "%s to %s: %s..."
9731                   (caddr (assq action names))
9732                   (or (car select-method)
9733                       (gnus-group-decoded-name to-newsgroup))
9734                   articles)
9735     (while articles
9736       (setq article (pop articles))
9737       ;; Set any marks that may have changed in the summary buffer.
9738       (when gnus-preserve-marks
9739         (gnus-summary-push-marks-to-backend article))
9740       (setq
9741        art-group
9742        (cond
9743         ;; Move the article.
9744         ((eq action 'move)
9745          ;; Remove this article from future suppression.
9746          (gnus-dup-unsuppress-article article)
9747          (let* ((from-method (gnus-find-method-for-group
9748                               gnus-newsgroup-name))
9749                 (to-method (or select-method
9750                                (gnus-find-method-for-group to-newsgroup)))
9751                 (move-is-internal (gnus-server-equal from-method to-method)))
9752            (gnus-request-move-article
9753             article                     ; Article to move
9754             gnus-newsgroup-name         ; From newsgroup
9755             (nth 1 (gnus-find-method-for-group
9756                     gnus-newsgroup-name)) ; Server
9757             (list 'gnus-request-accept-article
9758                   to-newsgroup (list 'quote select-method)
9759                   (not articles) t)     ; Accept form
9760             (not articles)              ; Only save nov last time
9761             (and move-is-internal
9762                  to-newsgroup           ; Not respooling
9763                                         ; Is this move internal?
9764                  (gnus-group-real-name to-newsgroup)))))
9765         ;; Copy the article.
9766         ((eq action 'copy)
9767          (with-current-buffer copy-buf
9768            (when (gnus-request-article-this-buffer article
9769                                                    gnus-newsgroup-name)
9770              (save-restriction
9771                (nnheader-narrow-to-headers)
9772                (dolist (hdr gnus-copy-article-ignored-headers)
9773                  (message-remove-header hdr t)))
9774              (gnus-request-accept-article
9775               to-newsgroup select-method (not articles) t))))
9776         ;; Crosspost the article.
9777         ((eq action 'crosspost)
9778          (let ((xref (message-tokenize-header
9779                       (mail-header-xref (gnus-summary-article-header
9780                                          article))
9781                       " ")))
9782            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9783                                   ":" (number-to-string article)))
9784            (unless xref
9785              (setq xref (list (system-name))))
9786            (setq new-xref
9787                  (concat
9788                   (mapconcat 'identity
9789                              (delete "Xref:" (delete new-xref xref))
9790                              " ")
9791                   " " new-xref))
9792            (with-current-buffer copy-buf
9793              ;; First put the article in the destination group.
9794              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9795              (when (consp (setq art-group
9796                                 (gnus-request-accept-article
9797                                  to-newsgroup select-method (not articles)
9798                                  t)))
9799                (setq new-xref (concat new-xref " " (car art-group)
9800                                       ":"
9801                                       (number-to-string (cdr art-group))))
9802                ;; Now we have the new Xrefs header, so we insert
9803                ;; it and replace the new article.
9804                (nnheader-replace-header "Xref" new-xref)
9805                (gnus-request-replace-article
9806                 (cdr art-group) to-newsgroup (current-buffer) t)
9807                art-group))))))
9808       (cond
9809        ((not art-group)
9810         (gnus-message 1 "Couldn't %s article %s: %s"
9811                       (cadr (assq action names)) article
9812                       (nnheader-get-report (car to-method))))
9813        ((eq art-group 'junk)
9814         (when (eq action 'move)
9815           (gnus-summary-mark-article article gnus-canceled-mark)
9816           (gnus-message 4 "Deleted article %s" article)
9817           ;; run the delete hook
9818           (run-hook-with-args 'gnus-summary-article-delete-hook
9819                               action
9820                               (gnus-data-header
9821                                (assoc article (gnus-data-list nil)))
9822                               gnus-newsgroup-name nil
9823                               select-method)))
9824        (t
9825         (let* ((pto-group (gnus-group-prefixed-name
9826                            (car art-group) to-method))
9827                (info (gnus-get-info pto-group))
9828                (to-group (gnus-info-group info))
9829                to-marks)
9830           ;; Update the group that has been moved to.
9831           (when (and info
9832                      (memq action '(move copy)))
9833             (unless (member to-group to-groups)
9834               (push to-group to-groups))
9835
9836             (when (and (not (memq article gnus-newsgroup-unreads))
9837                        (cdr art-group))
9838               (push 'read to-marks)
9839               (gnus-info-set-read
9840                info (gnus-add-to-range (gnus-info-read info)
9841                                        (list (cdr art-group)))))
9842
9843             ;; See whether the article is to be put in the cache.
9844             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9845                    (marks (if expirable
9846                               gnus-article-mark-lists
9847                             (delete '(expirable . expire)
9848                                     (copy-sequence
9849                                      gnus-article-mark-lists))))
9850                    (to-article (cdr art-group)))
9851
9852               ;; Enter the article into the cache in the new group,
9853               ;; if that is required.
9854               (when (and to-article
9855                          gnus-use-cache)
9856                 (gnus-cache-possibly-enter-article
9857                  to-group to-article
9858                  (memq article gnus-newsgroup-marked)
9859                  (memq article gnus-newsgroup-dormant)
9860                  (memq article gnus-newsgroup-unreads)))
9861
9862               (when (and gnus-preserve-marks
9863                          to-article)
9864                 ;; Copy any marks over to the new group.
9865                 (when (and (equal to-group gnus-newsgroup-name)
9866                            (not (memq article gnus-newsgroup-unreads)))
9867                   ;; Mark this article as read in this group.
9868                   (push (cons to-article gnus-read-mark)
9869                         gnus-newsgroup-reads)
9870                   ;; Increase the active status of this group.
9871                   (setcdr (gnus-active to-group) to-article)
9872                   (setcdr gnus-newsgroup-active to-article))
9873
9874                 (while marks
9875                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9876                     (when (memq article (symbol-value
9877                                          (intern (format "gnus-newsgroup-%s"
9878                                                          (caar marks)))))
9879                       (push (cdar marks) to-marks)
9880                       ;; If the other group is the same as this group,
9881                       ;; then we have to add the mark to the list.
9882                       (when (equal to-group gnus-newsgroup-name)
9883                         (set (intern (format "gnus-newsgroup-%s"
9884                                              (caar marks)))
9885                              (cons to-article
9886                                    (symbol-value
9887                                     (intern (format "gnus-newsgroup-%s"
9888                                                     (caar marks)))))))
9889                       ;; Copy the marks to other group.
9890                       (gnus-add-marked-articles
9891                        to-group (cdar marks) (list to-article) info)))
9892                   (setq marks (cdr marks)))
9893
9894                 (when (and expirable
9895                            gnus-mark-copied-or-moved-articles-as-expirable
9896                            (not (memq 'expire to-marks)))
9897                   ;; Mark this article as expirable.
9898                   (push 'expire to-marks)
9899                   (when (equal to-group gnus-newsgroup-name)
9900                     (push to-article gnus-newsgroup-expirable))
9901                   ;; Copy the expirable mark to other group.
9902                   (gnus-add-marked-articles
9903                    to-group 'expire (list to-article) info))
9904
9905                 (when to-marks
9906                   (gnus-request-set-mark
9907                    to-group (list (list (list to-article) 'add to-marks)))))
9908
9909               (gnus-dribble-enter
9910                (concat "(gnus-group-set-info '"
9911                        (gnus-prin1-to-string (gnus-get-info to-group))
9912                        ")"))))
9913
9914           ;; Update the Xref header in this article to point to
9915           ;; the new crossposted article we have just created.
9916           (when (eq action 'crosspost)
9917             (with-current-buffer copy-buf
9918               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9919               (nnheader-replace-header "Xref" new-xref)
9920               (gnus-request-replace-article
9921                article gnus-newsgroup-name (current-buffer) t)))
9922
9923           ;; run the move/copy/crosspost/respool hook
9924           (run-hook-with-args 'gnus-summary-article-move-hook
9925                               action
9926                               (gnus-data-header
9927                                (assoc article (gnus-data-list nil)))
9928                               gnus-newsgroup-name
9929                               to-newsgroup
9930                               select-method))
9931
9932         ;;;!!!Why is this necessary?
9933         (set-buffer gnus-summary-buffer)
9934
9935         (when (eq action 'move)
9936           (save-excursion
9937             (gnus-summary-goto-subject article)
9938             (gnus-summary-mark-article article gnus-canceled-mark)))))
9939       (push article articles-to-update-marks))
9940
9941     (save-excursion
9942       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9943     ;; Re-activate all groups that have been moved to.
9944     (with-current-buffer gnus-group-buffer
9945       (let ((gnus-group-marked to-groups))
9946         (gnus-group-get-new-news-this-group nil t)))
9947
9948     (gnus-kill-buffer copy-buf)
9949     (gnus-summary-position-point)
9950     (gnus-set-mode-line 'summary)))
9951
9952 (defun gnus-summary-push-marks-to-backend (article)
9953   (let ((set nil)
9954         (marks gnus-article-mark-lists))
9955     (unless (memq article gnus-newsgroup-unreads)
9956       (push 'read set))
9957     (while marks
9958       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9959                  (memq article (symbol-value
9960                                 (intern (format "gnus-newsgroup-%s"
9961                                                 (caar marks))))))
9962         (push (cdar marks) set))
9963       (pop marks))
9964     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
9965
9966 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9967   "Copy the current article to some other group.
9968 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9969 When called interactively, if TO-NEWSGROUP is nil, use the value of
9970 the variable `gnus-move-split-methods' for finding a default target
9971 newsgroup.
9972 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9973 re-spool using this method."
9974   (interactive "P")
9975   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9976
9977 (defun gnus-summary-crosspost-article (&optional n)
9978   "Crosspost the current article to some other group."
9979   (interactive "P")
9980   (gnus-summary-move-article n nil nil 'crosspost))
9981
9982 (defcustom gnus-summary-respool-default-method nil
9983   "Default method type for respooling an article.
9984 If nil, use to the current newsgroup method."
9985   :type 'symbol
9986   :group 'gnus-summary-mail)
9987
9988 (defun gnus-summary-respool-article (&optional n method)
9989   "Respool the current article.
9990 The article will be squeezed through the mail spooling process again,
9991 which means that it will be put in some mail newsgroup or other
9992 depending on `nnmail-split-methods'.
9993 If N is a positive number, respool the N next articles.
9994 If N is a negative number, respool the N previous articles.
9995 If N is nil and any articles have been marked with the process mark,
9996 respool those articles instead.
9997
9998 Respooling can be done both from mail groups and \"real\" newsgroups.
9999 In the former case, the articles in question will be moved from the
10000 current group into whatever groups they are destined to.  In the
10001 latter case, they will be copied into the relevant groups."
10002   (interactive
10003    (list current-prefix-arg
10004          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10005                 (methname
10006                  (symbol-name (or gnus-summary-respool-default-method
10007                                   (car (gnus-find-method-for-group
10008                                         gnus-newsgroup-name)))))
10009                 (method
10010                  (gnus-completing-read
10011                   "Backend to use when respooling"
10012                   methods t nil 'gnus-mail-method-history methname))
10013                 ms)
10014            (cond
10015             ((zerop (length (setq ms (gnus-servers-using-backend
10016                                       (intern method)))))
10017              (list (intern method) ""))
10018             ((= 1 (length ms))
10019              (car ms))
10020             (t
10021              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10022                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10023                            ms-alist))))))))
10024   (unless method
10025     (error "No method given for respooling"))
10026   (if (assoc (symbol-name
10027               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10028              (gnus-methods-using 'respool))
10029       (gnus-summary-move-article n nil method)
10030     (gnus-summary-copy-article n nil method)))
10031
10032 (defun gnus-summary-import-article (file &optional edit)
10033   "Import an arbitrary file into a mail newsgroup."
10034   (interactive "fImport file: \nP")
10035   (let ((group gnus-newsgroup-name)
10036         (now (current-time))
10037         atts lines group-art)
10038     (unless (gnus-check-backend-function 'request-accept-article group)
10039       (error "%s does not support article importing" group))
10040     (or (file-readable-p file)
10041         (not (file-regular-p file))
10042         (error "Can't read %s" file))
10043     (with-current-buffer (gnus-get-buffer-create " *import file*")
10044       (erase-buffer)
10045       (nnheader-insert-file-contents file)
10046       (goto-char (point-min))
10047       (if (nnheader-article-p)
10048           (save-restriction
10049             (goto-char (point-min))
10050             (search-forward "\n\n" nil t)
10051             (narrow-to-region (point-min) (1- (point)))
10052             (goto-char (point-min))
10053             (unless (re-search-forward "^date:" nil t)
10054               (goto-char (point-max))
10055               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10056        ;; This doesn't look like an article, so we fudge some headers.
10057         (setq atts (file-attributes file)
10058               lines (count-lines (point-min) (point-max)))
10059         (insert "From: " (read-string "From: ") "\n"
10060                 "Subject: " (read-string "Subject: ") "\n"
10061                 "Date: " (message-make-date (nth 5 atts)) "\n"
10062                 "Message-ID: " (message-make-message-id) "\n"
10063                 "Lines: " (int-to-string lines) "\n"
10064                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10065       (setq group-art (gnus-request-accept-article group nil t))
10066       (kill-buffer (current-buffer)))
10067     (setq gnus-newsgroup-active (gnus-activate-group group))
10068     (forward-line 1)
10069     (gnus-summary-goto-article (cdr group-art) nil t)
10070     (when edit
10071       (gnus-summary-edit-article))))
10072
10073 (defun gnus-summary-create-article ()
10074   "Create an article in a mail newsgroup."
10075   (interactive)
10076   (let ((group gnus-newsgroup-name)
10077         (now (current-time))
10078         group-art)
10079     (unless (gnus-check-backend-function 'request-accept-article group)
10080       (error "%s does not support article importing" group))
10081     (with-current-buffer (gnus-get-buffer-create " *import file*")
10082       (erase-buffer)
10083       (goto-char (point-min))
10084       ;; This doesn't look like an article, so we fudge some headers.
10085       (insert "From: " (read-string "From: ") "\n"
10086               "Subject: " (read-string "Subject: ") "\n"
10087               "Date: " (message-make-date now) "\n"
10088               "Message-ID: " (message-make-message-id) "\n")
10089       (setq group-art (gnus-request-accept-article group nil t))
10090       (kill-buffer (current-buffer)))
10091     (setq gnus-newsgroup-active (gnus-activate-group group))
10092     (forward-line 1)
10093     (gnus-summary-goto-article (cdr group-art) nil t)
10094     (gnus-summary-edit-article)))
10095
10096 (defun gnus-summary-article-posted-p ()
10097   "Say whether the current (mail) article is available from news as well.
10098 This will be the case if the article has both been mailed and posted."
10099   (interactive)
10100   (let ((id (mail-header-references (gnus-summary-article-header)))
10101         (gnus-override-method (car (gnus-refer-article-methods))))
10102     (if (gnus-request-head id "")
10103         (gnus-message 2 "The current message was found on %s"
10104                       gnus-override-method)
10105       (gnus-message 2 "The current message couldn't be found on %s"
10106                     gnus-override-method)
10107       nil)))
10108
10109 (defun gnus-summary-expire-articles (&optional now)
10110   "Expire all articles that are marked as expirable in the current group."
10111   (interactive)
10112   (when (and (not gnus-group-is-exiting-without-update-p)
10113              (gnus-check-backend-function
10114               'request-expire-articles gnus-newsgroup-name))
10115     ;; This backend supports expiry.
10116     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10117            (expirable (if total
10118                           (progn
10119                             ;; We need to update the info for
10120                             ;; this group for `gnus-list-of-read-articles'
10121                             ;; to give us the right answer.
10122                             (gnus-run-hooks 'gnus-exit-group-hook)
10123                             (gnus-summary-update-info)
10124                             (gnus-list-of-read-articles gnus-newsgroup-name))
10125                         (setq gnus-newsgroup-expirable
10126                               (sort gnus-newsgroup-expirable '<))))
10127            (expiry-wait (if now 'immediate
10128                           (gnus-group-find-parameter
10129                            gnus-newsgroup-name 'expiry-wait)))
10130            (nnmail-expiry-target
10131             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10132                 nnmail-expiry-target))
10133            es)
10134       (when expirable
10135         ;; There are expirable articles in this group, so we run them
10136         ;; through the expiry process.
10137         (gnus-message 6 "Expiring articles...")
10138         (unless (gnus-check-group gnus-newsgroup-name)
10139           (error "Can't open server for %s" gnus-newsgroup-name))
10140         ;; The list of articles that weren't expired is returned.
10141         (save-excursion
10142           (if expiry-wait
10143               (let ((nnmail-expiry-wait-function nil)
10144                     (nnmail-expiry-wait expiry-wait))
10145                 (setq es (gnus-request-expire-articles
10146                           expirable gnus-newsgroup-name)))
10147             (setq es (gnus-request-expire-articles
10148                       expirable gnus-newsgroup-name)))
10149           (unless total
10150             (setq gnus-newsgroup-expirable es))
10151           ;; We go through the old list of expirable, and mark all
10152           ;; really expired articles as nonexistent.
10153           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10154             (let ((gnus-use-cache nil))
10155               (dolist (article expirable)
10156                 (when (and (not (memq article es))
10157                            (gnus-data-find article))
10158                   (gnus-summary-mark-article article gnus-canceled-mark)
10159                   (run-hook-with-args 'gnus-summary-article-expire-hook
10160                                       'delete
10161                                       (gnus-data-header
10162                                        (assoc article (gnus-data-list nil)))
10163                                       gnus-newsgroup-name
10164                                       nil
10165                                       nil))))))
10166         (gnus-message 6 "Expiring articles...done")))))
10167
10168 (defun gnus-summary-expire-articles-now ()
10169   "Expunge all expirable articles in the current group.
10170 This means that *all* articles that are marked as expirable will be
10171 deleted forever, right now."
10172   (interactive)
10173   (or gnus-expert-user
10174       (gnus-yes-or-no-p
10175        "Are you really, really sure you want to delete all expirable messages? ")
10176       (error "Phew!"))
10177   (gnus-summary-expire-articles t))
10178
10179 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10180 (defun gnus-summary-delete-article (&optional n)
10181   "Delete the N next (mail) articles.
10182 This command actually deletes articles.  This is not a marking
10183 command.  The article will disappear forever from your life, never to
10184 return.
10185
10186 If N is negative, delete backwards.
10187 If N is nil and articles have been marked with the process mark,
10188 delete these instead.
10189
10190 If `gnus-novice-user' is non-nil you will be asked for
10191 confirmation before the articles are deleted."
10192   (interactive "P")
10193   (unless (gnus-check-backend-function 'request-expire-articles
10194                                        gnus-newsgroup-name)
10195     (error "The current newsgroup does not support article deletion"))
10196   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10197     (error "Couldn't open server"))
10198   ;; Compute the list of articles to delete.
10199   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10200         (nnmail-expiry-target 'delete)
10201         not-deleted)
10202     (if (and gnus-novice-user
10203              (not (gnus-yes-or-no-p
10204                    (format "Do you really want to delete %s forever? "
10205                            (if (> (length articles) 1)
10206                                (format "these %s articles" (length articles))
10207                              "this article")))))
10208         ()
10209       ;; Delete the articles.
10210       (setq not-deleted (gnus-request-expire-articles
10211                          articles gnus-newsgroup-name 'force))
10212       (save-excursion
10213         (while articles
10214           (gnus-summary-remove-process-mark (car articles))
10215           ;; The backend might not have been able to delete the article
10216           ;; after all.
10217           (unless (memq (car articles) not-deleted)
10218             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10219             (let* ((article (car articles))
10220                    (ghead  (gnus-data-header
10221                             (assoc article (gnus-data-list nil)))))
10222               (run-hook-with-args 'gnus-summary-article-delete-hook
10223                                   'delete ghead gnus-newsgroup-name nil
10224                                   nil)))
10225           (setq articles (cdr articles))))
10226       (when not-deleted
10227         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10228     (gnus-summary-position-point)
10229     (gnus-set-mode-line 'summary)
10230     not-deleted))
10231
10232 (defun gnus-summary-edit-article (&optional arg)
10233   "Edit the current article.
10234 This will have permanent effect only in mail groups.
10235 If ARG is nil, edit the decoded articles.
10236 If ARG is 1, edit the raw articles.
10237 If ARG is 2, edit the raw articles even in read-only groups.
10238 If ARG is 3, edit the articles with the current handles.
10239 Otherwise, allow editing of articles even in read-only
10240 groups."
10241   (interactive "P")
10242   (let (force raw current-handles)
10243     (cond
10244      ((null arg))
10245      ((eq arg 1)
10246       (setq raw t))
10247      ((eq arg 2)
10248       (setq raw t
10249             force t))
10250      ((eq arg 3)
10251       (setq current-handles
10252             (and (gnus-buffer-live-p gnus-article-buffer)
10253                  (with-current-buffer gnus-article-buffer
10254                    (prog1
10255                        gnus-article-mime-handles
10256                      (setq gnus-article-mime-handles nil))))))
10257      (t
10258       (setq force t)))
10259     (when (and raw (not force)
10260                (member gnus-newsgroup-name '("nndraft:delayed"
10261                                              "nndraft:drafts"
10262                                              "nndraft:queue")))
10263       (error "Can't edit the raw article in group %s"
10264              gnus-newsgroup-name))
10265     (with-current-buffer gnus-summary-buffer
10266       (let ((mail-parse-charset gnus-newsgroup-charset)
10267             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10268         (gnus-set-global-variables)
10269         (when (and (not force)
10270                    (gnus-group-read-only-p))
10271           (error "The current newsgroup does not support article editing"))
10272         (gnus-summary-show-article t)
10273         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10274           (with-current-buffer gnus-article-buffer
10275             (mm-enable-multibyte)))
10276         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10277             (setq raw t))
10278         (gnus-article-edit-article
10279          (if raw 'ignore
10280            `(lambda ()
10281               (let ((mbl mml-buffer-list))
10282                 (setq mml-buffer-list nil)
10283                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10284                   (mime-to-mml ,'current-handles))
10285                 (let ((mbl1 mml-buffer-list))
10286                   (setq mml-buffer-list mbl)
10287                   (set (make-local-variable 'mml-buffer-list) mbl1))
10288                 (gnus-make-local-hook 'kill-buffer-hook)
10289                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10290          `(lambda (no-highlight)
10291             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10292                   (message-options message-options)
10293                   (message-options-set-recipient)
10294                   (mail-parse-ignored-charsets
10295                    ',gnus-newsgroup-ignored-charsets)
10296                   (rfc2047-header-encoding-alist
10297                    ',(let ((charset (gnus-group-name-charset
10298                                      (gnus-find-method-for-group
10299                                       gnus-newsgroup-name)
10300                                      gnus-newsgroup-name)))
10301                        (append (list (cons "Newsgroups" charset)
10302                                      (cons "Followup-To" charset)
10303                                      (cons "Xref" charset))
10304                                rfc2047-header-encoding-alist))))
10305               ,(if (not raw) '(progn
10306                                 (mml-to-mime)
10307                                 (mml-destroy-buffers)
10308                                 (remove-hook 'kill-buffer-hook
10309                                              'mml-destroy-buffers t)
10310                                 (kill-local-variable 'mml-buffer-list)))
10311               (gnus-summary-edit-article-done
10312                ,(or (mail-header-references gnus-current-headers) "")
10313                ,(gnus-group-read-only-p)
10314                ,gnus-summary-buffer no-highlight))))))))
10315
10316 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10317
10318 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10319                                                  no-highlight)
10320   "Make edits to the current article permanent."
10321   (interactive)
10322   (save-excursion
10323     ;; The buffer restriction contains the entire article if it exists.
10324     (when (article-goto-body)
10325       (let ((lines (count-lines (point) (point-max)))
10326             (length (- (point-max) (point)))
10327             (case-fold-search t)
10328             (body (copy-marker (point))))
10329         (goto-char (point-min))
10330         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10331           (delete-region (match-beginning 1) (match-end 1))
10332           (insert (number-to-string length)))
10333         (goto-char (point-min))
10334         (when (re-search-forward
10335                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10336           (delete-region (match-beginning 1) (match-end 1))
10337           (insert (number-to-string length)))
10338         (goto-char (point-min))
10339         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10340           (delete-region (match-beginning 1) (match-end 1))
10341           (insert (number-to-string lines))))))
10342   ;; Replace the article.
10343   (let ((buf (current-buffer))
10344         (article (cdr gnus-article-current))
10345         replace-result)
10346     (with-temp-buffer
10347       (insert-buffer-substring buf)
10348       (if (and (not read-only)
10349                (not (setq replace-result
10350                           (gnus-request-replace-article
10351                            article (car gnus-article-current)
10352                            (current-buffer) t))))
10353           (error "Couldn't replace article")
10354         ;; If we got a number back, then that's the new article number
10355         ;; for this article.  Otherwise, the article number didn't change.
10356         (when (numberp replace-result)
10357           (with-current-buffer gnus-summary-buffer
10358             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10359             (gnus-summary-limit gnus-newsgroup-limit)
10360             (setq article replace-result)
10361             (gnus-summary-goto-subject article t)))
10362         ;; Update the summary buffer.
10363         (if (and references
10364                  (equal (message-tokenize-header references " ")
10365                         (message-tokenize-header
10366                          (or (message-fetch-field "references") "") " ")))
10367             ;; We only have to update this line.
10368             (save-excursion
10369               (save-restriction
10370                 (message-narrow-to-head)
10371                 (let ((head (buffer-substring-no-properties
10372                              (point-min) (point-max)))
10373                       header)
10374                   (with-temp-buffer
10375                     (insert (format "211 %d Article retrieved.\n" article))
10376                     (insert head)
10377                     (insert ".\n")
10378                     (let ((nntp-server-buffer (current-buffer)))
10379                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10380                     (with-current-buffer gnus-summary-buffer
10381                       (gnus-data-set-header (gnus-data-find article) header)
10382                       (gnus-summary-update-article-line article header)
10383                       (if (gnus-summary-goto-subject article nil t)
10384                           (gnus-summary-update-secondary-mark article)))))))
10385           ;; Update threads.
10386           (set-buffer (or buffer gnus-summary-buffer))
10387           (gnus-summary-update-article article)
10388           (if (gnus-summary-goto-subject article nil t)
10389               (gnus-summary-update-secondary-mark article)))
10390         ;; Prettify the article buffer again.
10391         (unless no-highlight
10392           (with-current-buffer gnus-article-buffer
10393             ;;!!! Fix this -- article should be rehighlighted.
10394             ;;(gnus-run-hooks 'gnus-article-display-hook)
10395             (set-buffer gnus-original-article-buffer)
10396             (gnus-request-article
10397              article (car gnus-article-current) (current-buffer))))
10398         ;; Prettify the summary buffer line.
10399         (when (gnus-visual-p 'summary-highlight 'highlight)
10400           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10401
10402 (defun gnus-summary-edit-wash (key)
10403   "Perform editing command KEY in the article buffer."
10404   (interactive
10405    (list
10406     (progn
10407       (message "%s" (concat (this-command-keys) "- "))
10408       (read-char))))
10409   (message "")
10410   (gnus-summary-edit-article)
10411   (execute-kbd-macro (concat (this-command-keys) key))
10412   (gnus-article-edit-done))
10413
10414 ;;; Respooling
10415
10416 (defun gnus-summary-respool-query (&optional silent trace)
10417   "Query where the respool algorithm would put this article."
10418   (interactive)
10419   (let (gnus-mark-article-hook)
10420     (gnus-summary-select-article)
10421     (with-current-buffer gnus-original-article-buffer
10422       (let ((groups (nnmail-article-group 'identity trace)))
10423         (unless silent
10424           (if groups
10425               (message "This message would go to %s"
10426                        (mapconcat 'car groups ", "))
10427             (message "This message would go to no groups"))
10428           groups)))))
10429
10430 (defun gnus-summary-respool-trace ()
10431   "Trace where the respool algorithm would put this article.
10432 Display a buffer showing all fancy splitting patterns which matched."
10433   (interactive)
10434   (gnus-summary-respool-query nil t))
10435
10436 ;; Summary marking commands.
10437
10438 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10439   "Mark articles which has the same subject as read, and then select the next.
10440 If UNMARK is positive, remove any kind of mark.
10441 If UNMARK is negative, tick articles."
10442   (interactive "P")
10443   (when unmark
10444     (setq unmark (prefix-numeric-value unmark)))
10445   (let ((count
10446          (gnus-summary-mark-same-subject
10447           (gnus-summary-article-subject) unmark)))
10448     ;; Select next unread article.  If auto-select-same mode, should
10449     ;; select the first unread article.
10450     (gnus-summary-next-article t (and gnus-auto-select-same
10451                                       (gnus-summary-article-subject)))
10452     (gnus-message 7 "%d article%s marked as %s"
10453                   count (if (= count 1) " is" "s are")
10454                   (if unmark "unread" "read"))))
10455
10456 (defun gnus-summary-kill-same-subject (&optional unmark)
10457   "Mark articles which has the same subject as read.
10458 If UNMARK is positive, remove any kind of mark.
10459 If UNMARK is negative, tick articles."
10460   (interactive "P")
10461   (when unmark
10462     (setq unmark (prefix-numeric-value unmark)))
10463   (let ((count
10464          (gnus-summary-mark-same-subject
10465           (gnus-summary-article-subject) unmark)))
10466     ;; If marked as read, go to next unread subject.
10467     (when (null unmark)
10468       ;; Go to next unread subject.
10469       (gnus-summary-next-subject 1 t))
10470     (gnus-message 7 "%d articles are marked as %s"
10471                   count (if unmark "unread" "read"))))
10472
10473 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10474   "Mark articles with same SUBJECT as read, and return marked number.
10475 If optional argument UNMARK is positive, remove any kinds of marks.
10476 If optional argument UNMARK is negative, mark articles as unread instead."
10477   (let ((count 1))
10478     (save-excursion
10479       (cond
10480        ((null unmark)                   ; Mark as read.
10481         (while (and
10482                 (progn
10483                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10484                   (gnus-summary-show-thread) t)
10485                 (gnus-summary-find-subject subject))
10486           (setq count (1+ count))))
10487        ((> unmark 0)                    ; Tick.
10488         (while (and
10489                 (progn
10490                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10491                   (gnus-summary-show-thread) t)
10492                 (gnus-summary-find-subject subject))
10493           (setq count (1+ count))))
10494        (t                               ; Mark as unread.
10495         (while (and
10496                 (progn
10497                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10498                   (gnus-summary-show-thread) t)
10499                 (gnus-summary-find-subject subject))
10500           (setq count (1+ count)))))
10501       (gnus-set-mode-line 'summary)
10502       ;; Return the number of marked articles.
10503       count)))
10504
10505 (defun gnus-summary-mark-as-processable (n &optional unmark)
10506   "Set the process mark on the next N articles.
10507 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10508 the process mark instead.  The difference between N and the actual
10509 number of articles marked is returned."
10510   (interactive "P")
10511   (if (and (null n) (gnus-region-active-p))
10512       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10513     (setq n (prefix-numeric-value n))
10514     (let ((backward (< n 0))
10515           (n (abs n)))
10516       (while (and
10517               (> n 0)
10518               (if unmark
10519                   (gnus-summary-remove-process-mark
10520                    (gnus-summary-article-number))
10521                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10522               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10523         (setq n (1- n)))
10524       (when (/= 0 n)
10525         (gnus-message 7 "No more articles"))
10526       (gnus-summary-recenter)
10527       (gnus-summary-position-point)
10528       n)))
10529
10530 (defun gnus-summary-unmark-as-processable (n)
10531   "Remove the process mark from the next N articles.
10532 If N is negative, unmark backward instead.  The difference between N and
10533 the actual number of articles unmarked is returned."
10534   (interactive "P")
10535   (gnus-summary-mark-as-processable n t))
10536
10537 (defun gnus-summary-unmark-all-processable ()
10538   "Remove the process mark from all articles."
10539   (interactive)
10540   (save-excursion
10541     (while gnus-newsgroup-processable
10542       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10543   (gnus-summary-position-point))
10544
10545 (defun gnus-summary-add-mark (article type)
10546   "Mark ARTICLE with a mark of TYPE."
10547   (let ((vtype (car (assq type gnus-article-mark-lists)))
10548         var)
10549     (if (not vtype)
10550         (error "No such mark type: %s" type)
10551       (setq var (intern (format "gnus-newsgroup-%s" type)))
10552       (set var (cons article (symbol-value var)))
10553       (if (memq type '(processable cached replied forwarded recent saved))
10554           (gnus-summary-update-secondary-mark article)
10555         ;;; !!! This is bogus.  We should find out what primary
10556         ;;; !!! mark we want to set.
10557         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10558
10559 (defun gnus-summary-mark-as-expirable (n)
10560   "Mark N articles forward as expirable.
10561 If N is negative, mark backward instead.  The difference between N and
10562 the actual number of articles marked is returned."
10563   (interactive "p")
10564   (gnus-summary-mark-forward n gnus-expirable-mark))
10565
10566 (defun gnus-summary-mark-as-spam (n)
10567   "Mark N articles forward as spam.
10568 If N is negative, mark backward instead.  The difference between N and
10569 the actual number of articles marked is returned."
10570   (interactive "p")
10571   (gnus-summary-mark-forward n gnus-spam-mark))
10572
10573 (defun gnus-summary-mark-article-as-replied (article)
10574   "Mark ARTICLE as replied to and update the summary line.
10575 ARTICLE can also be a list of articles."
10576   (interactive (list (gnus-summary-article-number)))
10577   (let ((articles (if (listp article) article (list article))))
10578     (dolist (article articles)
10579       (unless (numberp article)
10580         (error "%s is not a number" article))
10581       (push article gnus-newsgroup-replied)
10582       (let ((inhibit-read-only t))
10583         (when (gnus-summary-goto-subject article nil t)
10584           (gnus-summary-update-secondary-mark article))))))
10585
10586 (defun gnus-summary-mark-article-as-forwarded (article)
10587   "Mark ARTICLE as forwarded and update the summary line.
10588 ARTICLE can also be a list of articles."
10589   (let ((articles (if (listp article) article (list article))))
10590     (dolist (article articles)
10591       (push article gnus-newsgroup-forwarded)
10592       (let ((inhibit-read-only t))
10593         (when (gnus-summary-goto-subject article nil t)
10594           (gnus-summary-update-secondary-mark article))))))
10595
10596 (defun gnus-summary-set-bookmark (article)
10597   "Set a bookmark in current article."
10598   (interactive (list (gnus-summary-article-number)))
10599   (when (or (not (get-buffer gnus-article-buffer))
10600             (not gnus-current-article)
10601             (not gnus-article-current)
10602             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10603     (error "No current article selected"))
10604   ;; Remove old bookmark, if one exists.
10605   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10606   ;; Set the new bookmark, which is on the form
10607   ;; (article-number . line-number-in-body).
10608   (push
10609    (cons article
10610          (with-current-buffer gnus-article-buffer
10611            (count-lines
10612             (min (point)
10613                  (save-excursion
10614                    (article-goto-body)
10615                    (point)))
10616             (point))))
10617    gnus-newsgroup-bookmarks)
10618   (gnus-message 6 "A bookmark has been added to the current article."))
10619
10620 (defun gnus-summary-remove-bookmark (article)
10621   "Remove the bookmark from the current article."
10622   (interactive (list (gnus-summary-article-number)))
10623   ;; Remove old bookmark, if one exists.
10624   (if (not (assq article gnus-newsgroup-bookmarks))
10625       (gnus-message 6 "No bookmark in current article.")
10626     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10627     (gnus-message 6 "Removed bookmark.")))
10628
10629 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10630 (defun gnus-summary-mark-as-dormant (n)
10631   "Mark N articles forward as dormant.
10632 If N is negative, mark backward instead.  The difference between N and
10633 the actual number of articles marked is returned."
10634   (interactive "p")
10635   (gnus-summary-mark-forward n gnus-dormant-mark))
10636
10637 (defun gnus-summary-set-process-mark (article)
10638   "Set the process mark on ARTICLE and update the summary line."
10639   (setq gnus-newsgroup-processable
10640         (cons article
10641               (delq article gnus-newsgroup-processable)))
10642   (when (gnus-summary-goto-subject article)
10643     (gnus-summary-show-thread)
10644     (gnus-summary-goto-subject article)
10645     (gnus-summary-update-secondary-mark article)))
10646
10647 (defun gnus-summary-remove-process-mark (&rest articles)
10648   "Remove the process mark from ARTICLES and update the summary line."
10649   (dolist (article articles)
10650     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10651     (when (gnus-summary-goto-subject article)
10652       (gnus-summary-show-thread)
10653       (gnus-summary-goto-subject article)
10654       (gnus-summary-update-secondary-mark article)))
10655   t)
10656
10657 (defun gnus-summary-set-saved-mark (article)
10658   "Set the process mark on ARTICLE and update the summary line."
10659   (push article gnus-newsgroup-saved)
10660   (when (gnus-summary-goto-subject article)
10661     (gnus-summary-update-secondary-mark article)))
10662
10663 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10664   "Mark N articles as read forwards.
10665 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10666 The difference between N and the actual number of articles marked is
10667 returned.
10668 If NO-EXPIRE, auto-expiry will be inhibited."
10669   (interactive "p")
10670   (gnus-summary-show-thread)
10671   (let ((backward (< n 0))
10672         (gnus-summary-goto-unread
10673          (and gnus-summary-goto-unread
10674               (not (eq gnus-summary-goto-unread 'never))
10675               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10676                                     gnus-ticked-mark gnus-dormant-mark)))))
10677         (n (abs n))
10678         (mark (or mark gnus-del-mark)))
10679     (while (and (> n 0)
10680                 (gnus-summary-mark-article nil mark no-expire)
10681                 (zerop (gnus-summary-next-subject
10682                         (if backward -1 1)
10683                         (and gnus-summary-goto-unread
10684                              (not (eq gnus-summary-goto-unread 'never)))
10685                         t)))
10686       (setq n (1- n)))
10687     (when (/= 0 n)
10688       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10689     (gnus-summary-recenter)
10690     (gnus-summary-position-point)
10691     (gnus-set-mode-line 'summary)
10692     n))
10693
10694 (defun gnus-summary-mark-article-as-read (mark)
10695   "Mark the current article quickly as read with MARK."
10696   (let ((article (gnus-summary-article-number)))
10697     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10698     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10699     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10700     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10701     (push (cons article mark) gnus-newsgroup-reads)
10702     ;; Possibly remove from cache, if that is used.
10703     (when gnus-use-cache
10704       (gnus-cache-enter-remove-article article))
10705     ;; Allow the backend to change the mark.
10706     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10707     ;; Check for auto-expiry.
10708     (when (and gnus-newsgroup-auto-expire
10709                (memq mark gnus-auto-expirable-marks))
10710       (setq mark gnus-expirable-mark)
10711       ;; Let the backend know about the mark change.
10712       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10713       (push article gnus-newsgroup-expirable))
10714     ;; Set the mark in the buffer.
10715     (gnus-summary-update-mark mark 'unread)
10716     t))
10717
10718 (defun gnus-summary-mark-article-as-unread (mark)
10719   "Mark the current article quickly as unread with MARK."
10720   (let* ((article (gnus-summary-article-number))
10721          (old-mark (gnus-summary-article-mark article)))
10722     ;; Allow the backend to change the mark.
10723     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10724     (if (eq mark old-mark)
10725         t
10726       (if (<= article 0)
10727           (progn
10728             (gnus-error 1 "Can't mark negative article numbers")
10729             nil)
10730         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10731         (setq gnus-newsgroup-spam-marked
10732               (delq article gnus-newsgroup-spam-marked))
10733         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10734         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10735         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10736         (cond ((= mark gnus-ticked-mark)
10737                (setq gnus-newsgroup-marked
10738                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10739                                               article)))
10740               ((= mark gnus-spam-mark)
10741                (setq gnus-newsgroup-spam-marked
10742                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10743                                               article)))
10744               ((= mark gnus-dormant-mark)
10745                (setq gnus-newsgroup-dormant
10746                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10747                                               article)))
10748               (t
10749                (setq gnus-newsgroup-unreads
10750                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10751                                               article))))
10752         (gnus-alist-pull article gnus-newsgroup-reads)
10753
10754         ;; See whether the article is to be put in the cache.
10755         (and gnus-use-cache
10756              (vectorp (gnus-summary-article-header article))
10757              (save-excursion
10758                (gnus-cache-possibly-enter-article
10759                 gnus-newsgroup-name article
10760                 (= mark gnus-ticked-mark)
10761                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10762
10763         ;; Fix the mark.
10764         (gnus-summary-update-mark mark 'unread)
10765         t))))
10766
10767 (defun gnus-summary-mark-article (&optional article mark no-expire)
10768   "Mark ARTICLE with MARK.  MARK can be any character.
10769 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10770 `??' (dormant) and `?E' (expirable).
10771 If MARK is nil, then the default character `?r' is used.
10772 If ARTICLE is nil, then the article on the current line will be
10773 marked.
10774 If NO-EXPIRE, auto-expiry will be inhibited."
10775   ;; The mark might be a string.
10776   (when (stringp mark)
10777     (setq mark (aref mark 0)))
10778   ;; If no mark is given, then we check auto-expiring.
10779   (when (null mark)
10780     (setq mark gnus-del-mark))
10781   (when (and (not no-expire)
10782              gnus-newsgroup-auto-expire
10783              (memq mark gnus-auto-expirable-marks))
10784     (setq mark gnus-expirable-mark))
10785   (let ((article (or article (gnus-summary-article-number)))
10786         (old-mark (gnus-summary-article-mark article)))
10787     ;; Allow the backend to change the mark.
10788     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10789     (if (eq mark old-mark)
10790         t
10791       (unless article
10792         (error "No article on current line"))
10793       (if (not (if (or (= mark gnus-unread-mark)
10794                        (= mark gnus-ticked-mark)
10795                        (= mark gnus-spam-mark)
10796                        (= mark gnus-dormant-mark))
10797                    (gnus-mark-article-as-unread article mark)
10798                  (gnus-mark-article-as-read article mark)))
10799           t
10800         ;; See whether the article is to be put in the cache.
10801         (and gnus-use-cache
10802              (not (= mark gnus-canceled-mark))
10803              (vectorp (gnus-summary-article-header article))
10804              (save-excursion
10805                (gnus-cache-possibly-enter-article
10806                 gnus-newsgroup-name article
10807                 (= mark gnus-ticked-mark)
10808                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10809
10810         (when (gnus-summary-goto-subject article nil t)
10811           (let ((inhibit-read-only t))
10812             (gnus-summary-show-thread)
10813             ;; Fix the mark.
10814             (gnus-summary-update-mark mark 'unread)
10815             t))))))
10816
10817 (defun gnus-summary-update-secondary-mark (article)
10818   "Update the secondary (read, process, cache) mark."
10819   (gnus-summary-update-mark
10820    (cond ((memq article gnus-newsgroup-processable)
10821           gnus-process-mark)
10822          ((memq article gnus-newsgroup-cached)
10823           gnus-cached-mark)
10824          ((memq article gnus-newsgroup-replied)
10825           gnus-replied-mark)
10826          ((memq article gnus-newsgroup-forwarded)
10827           gnus-forwarded-mark)
10828          ((memq article gnus-newsgroup-saved)
10829           gnus-saved-mark)
10830          ((memq article gnus-newsgroup-recent)
10831           gnus-recent-mark)
10832          ((memq article gnus-newsgroup-unseen)
10833           gnus-unseen-mark)
10834          (t gnus-no-mark))
10835    'replied)
10836   (when (gnus-visual-p 'summary-highlight 'highlight)
10837     (gnus-summary-highlight-line)
10838     (gnus-run-hooks 'gnus-summary-update-hook))
10839   t)
10840
10841 (defun gnus-summary-update-download-mark (article)
10842   "Update the download mark."
10843   (gnus-summary-update-mark
10844    (cond ((memq article gnus-newsgroup-undownloaded)
10845           gnus-undownloaded-mark)
10846          (gnus-newsgroup-agentized
10847           gnus-downloaded-mark)
10848          (t
10849           gnus-no-mark))
10850    'download)
10851   (gnus-summary-update-line t)
10852   t)
10853
10854 (defun gnus-summary-update-mark (mark type)
10855   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10856         (inhibit-read-only t))
10857     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10858     (when forward
10859       (when (looking-at "\r")
10860         (incf forward))
10861       (when (<= (+ forward (point)) (point-max))
10862         ;; Go to the right position on the line.
10863         (goto-char (+ forward (point)))
10864         ;; Replace the old mark with the new mark.
10865         (let ((to-insert
10866                (mm-subst-char-in-string
10867                 (char-after) mark
10868                 (buffer-substring (point) (1+ (point))))))
10869           (delete-region (point) (1+ (point)))
10870           (insert to-insert))
10871         ;; Optionally update the marks by some user rule.
10872         (when (eq type 'unread)
10873           (gnus-data-set-mark
10874            (gnus-data-find (gnus-summary-article-number)) mark)
10875           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10876
10877 (defun gnus-mark-article-as-read (article &optional mark)
10878   "Enter ARTICLE in the pertinent lists and remove it from others."
10879   ;; Make the article expirable.
10880   (let ((mark (or mark gnus-del-mark)))
10881     (setq gnus-newsgroup-expirable
10882           (if (= mark gnus-expirable-mark)
10883               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10884             (delq article gnus-newsgroup-expirable)))
10885     ;; Remove from unread and marked lists.
10886     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10887     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10888     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10889     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10890     (push (cons article mark) gnus-newsgroup-reads)
10891     ;; Possibly remove from cache, if that is used.
10892     (when gnus-use-cache
10893       (gnus-cache-enter-remove-article article))
10894     t))
10895
10896 (defun gnus-mark-article-as-unread (article &optional mark)
10897   "Enter ARTICLE in the pertinent lists and remove it from others."
10898   (let ((mark (or mark gnus-ticked-mark)))
10899     (if (<= article 0)
10900         (progn
10901           (gnus-error 1 "Can't mark negative article numbers")
10902           nil)
10903       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10904             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10905             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10906             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10907             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10908
10909       ;; Unsuppress duplicates?
10910       (when gnus-suppress-duplicates
10911         (gnus-dup-unsuppress-article article))
10912
10913       (cond ((= mark gnus-ticked-mark)
10914              (setq gnus-newsgroup-marked
10915                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10916             ((= mark gnus-spam-mark)
10917              (setq gnus-newsgroup-spam-marked
10918                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10919                                             article)))
10920             ((= mark gnus-dormant-mark)
10921              (setq gnus-newsgroup-dormant
10922                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10923             (t
10924              (setq gnus-newsgroup-unreads
10925                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10926       (gnus-alist-pull article gnus-newsgroup-reads)
10927       t)))
10928
10929 (defun gnus-summary-tick-article-forward (n)
10930   "Tick N articles forwards.
10931 If N is negative, tick backwards instead.
10932 The difference between N and the number of articles ticked is returned."
10933   (interactive "p")
10934   (gnus-summary-mark-forward n gnus-ticked-mark))
10935
10936 (defun gnus-summary-tick-article-backward (n)
10937   "Tick N articles backwards.
10938 The difference between N and the number of articles ticked is returned."
10939   (interactive "p")
10940   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10941
10942 (defun gnus-summary-tick-article (&optional article clear-mark)
10943   "Mark current article as unread.
10944 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10945 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10946   (interactive)
10947   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10948                                        gnus-ticked-mark)))
10949
10950 (defun gnus-summary-mark-as-read-forward (n)
10951   "Mark N articles as read forwards.
10952 If N is negative, mark backwards instead.
10953 The difference between N and the actual number of articles marked is
10954 returned."
10955   (interactive "p")
10956   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10957
10958 (defun gnus-summary-mark-as-read-backward (n)
10959   "Mark the N articles as read backwards.
10960 The difference between N and the actual number of articles marked is
10961 returned."
10962   (interactive "p")
10963   (gnus-summary-mark-forward
10964    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10965
10966 (defun gnus-summary-mark-as-read (&optional article mark)
10967   "Mark current article as read.
10968 ARTICLE specifies the article to be marked as read.
10969 MARK specifies a string to be inserted at the beginning of the line."
10970   (gnus-summary-mark-article article mark))
10971
10972 (defun gnus-summary-clear-mark-forward (n)
10973   "Clear marks from N articles forward.
10974 If N is negative, clear backward instead.
10975 The difference between N and the number of marks cleared is returned."
10976   (interactive "p")
10977   (gnus-summary-mark-forward n gnus-unread-mark))
10978
10979 (defun gnus-summary-clear-mark-backward (n)
10980   "Clear marks from N articles backward.
10981 The difference between N and the number of marks cleared is returned."
10982   (interactive "p")
10983   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10984
10985 (defun gnus-summary-mark-unread-as-read ()
10986   "Intended to be used by `gnus-mark-article-hook'."
10987   (when (memq gnus-current-article gnus-newsgroup-unreads)
10988     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10989
10990 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10991   "Intended to be used by `gnus-mark-article-hook'."
10992   (let ((mark (gnus-summary-article-mark)))
10993     (when (or (gnus-unread-mark-p mark)
10994               (gnus-read-mark-p mark))
10995       (gnus-summary-mark-article gnus-current-article
10996                                  (or new-mark gnus-read-mark)))))
10997
10998 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10999   "Intended to be used by `gnus-mark-article-hook'."
11000   (let ((mark (gnus-summary-article-mark)))
11001     (when (or (gnus-unread-mark-p mark)
11002               (gnus-read-mark-p mark))
11003       (gnus-summary-mark-article (gnus-summary-article-number)
11004                                  (or new-mark gnus-read-mark)))))
11005
11006 (defun gnus-summary-mark-unread-as-ticked ()
11007   "Intended to be used by `gnus-mark-article-hook'."
11008   (when (memq gnus-current-article gnus-newsgroup-unreads)
11009     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11010
11011 (defun gnus-summary-mark-region-as-read (point mark all)
11012   "Mark all unread articles between point and mark as read.
11013 If given a prefix, mark all articles between point and mark as read,
11014 even ticked and dormant ones."
11015   (interactive "r\nP")
11016   (save-excursion
11017     (let (article)
11018       (goto-char point)
11019       (beginning-of-line)
11020       (while (and
11021               (< (point) mark)
11022               (progn
11023                 (when (or all
11024                           (memq (setq article (gnus-summary-article-number))
11025                                 gnus-newsgroup-unreads))
11026                   (gnus-summary-mark-article article gnus-del-mark))
11027                 t)
11028               (gnus-summary-find-next))))))
11029
11030 (defun gnus-summary-mark-below (score mark)
11031   "Mark articles with score less than SCORE with MARK."
11032   (interactive "P\ncMark: ")
11033   (setq score (if score
11034                   (prefix-numeric-value score)
11035                 (or gnus-summary-default-score 0)))
11036   (with-current-buffer gnus-summary-buffer
11037     (goto-char (point-min))
11038     (while
11039         (progn
11040           (and (< (gnus-summary-article-score) score)
11041                (gnus-summary-mark-article nil mark))
11042           (gnus-summary-find-next)))))
11043
11044 (defun gnus-summary-kill-below (&optional score)
11045   "Mark articles with score below SCORE as read."
11046   (interactive "P")
11047   (gnus-summary-mark-below score gnus-killed-mark))
11048
11049 (defun gnus-summary-clear-above (&optional score)
11050   "Clear all marks from articles with score above SCORE."
11051   (interactive "P")
11052   (gnus-summary-mark-above score gnus-unread-mark))
11053
11054 (defun gnus-summary-tick-above (&optional score)
11055   "Tick all articles with score above SCORE."
11056   (interactive "P")
11057   (gnus-summary-mark-above score gnus-ticked-mark))
11058
11059 (defun gnus-summary-mark-above (score mark)
11060   "Mark articles with score over SCORE with MARK."
11061   (interactive "P\ncMark: ")
11062   (setq score (if score
11063                   (prefix-numeric-value score)
11064                 (or gnus-summary-default-score 0)))
11065   (with-current-buffer gnus-summary-buffer
11066     (goto-char (point-min))
11067     (while (and (progn
11068                   (when (> (gnus-summary-article-score) score)
11069                     (gnus-summary-mark-article nil mark))
11070                   t)
11071                 (gnus-summary-find-next)))))
11072
11073 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11074 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11075 (defun gnus-summary-limit-include-expunged (&optional no-error)
11076   "Display all the hidden articles that were expunged for low scores."
11077   (interactive)
11078   (let ((inhibit-read-only t))
11079     (let ((scored gnus-newsgroup-scored)
11080           headers h)
11081       (while scored
11082         (unless (gnus-summary-article-header (caar scored))
11083           (and (setq h (gnus-number-to-header (caar scored)))
11084                (< (cdar scored) gnus-summary-expunge-below)
11085                (push h headers)))
11086         (setq scored (cdr scored)))
11087       (if (not headers)
11088           (when (not no-error)
11089             (error "No expunged articles hidden"))
11090         (goto-char (point-min))
11091         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11092         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11093         (dolist (x headers)
11094           (push (mail-header-number x) gnus-newsgroup-limit))
11095         (gnus-summary-prepare-unthreaded (nreverse headers))
11096         (goto-char (point-min))
11097         (gnus-summary-position-point)
11098         t))))
11099
11100 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11101   "Mark all unread articles in this newsgroup as read.
11102 If prefix argument ALL is non-nil, ticked and dormant articles will
11103 also be marked as read.
11104 If QUIETLY is non-nil, no questions will be asked.
11105
11106 If TO-HERE is non-nil, it should be a point in the buffer.  All
11107 articles before (after, if REVERSE is set) this point will be marked
11108 as read.
11109
11110 Note that this function will only catch up the unread article
11111 in the current summary buffer limitation.
11112
11113 The number of articles marked as read is returned."
11114   (interactive "P")
11115   (prog1
11116       (save-excursion
11117         (when (or quietly
11118                   (not gnus-interactive-catchup) ;Without confirmation?
11119                   gnus-expert-user
11120                   (gnus-y-or-n-p
11121                    (if all
11122                        "Mark absolutely all articles as read? "
11123                      "Mark all unread articles as read? ")))
11124           (if (and not-mark
11125                    (not gnus-newsgroup-adaptive)
11126                    (not gnus-newsgroup-auto-expire)
11127                    (not gnus-suppress-duplicates)
11128                    (or (not gnus-use-cache)
11129                        (eq gnus-use-cache 'passive)))
11130               (progn
11131                 (when all
11132                   (setq gnus-newsgroup-marked nil
11133                         gnus-newsgroup-spam-marked nil
11134                         gnus-newsgroup-dormant nil))
11135                 (setq gnus-newsgroup-unreads
11136                       (gnus-sorted-nunion
11137                        (gnus-sorted-intersection gnus-newsgroup-unreads
11138                                                  gnus-newsgroup-downloadable)
11139                        (gnus-sorted-difference gnus-newsgroup-unfetched
11140                                                gnus-newsgroup-cached))))
11141             ;; We actually mark all articles as canceled, which we
11142             ;; have to do when using auto-expiry or adaptive scoring.
11143             (gnus-summary-show-all-threads)
11144             (if (and to-here reverse)
11145                 (progn
11146                   (goto-char to-here)
11147                   (gnus-summary-mark-current-read-and-unread-as-read
11148                    gnus-catchup-mark)
11149                   (while (gnus-summary-find-next (not all))
11150                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11151               (when (gnus-summary-first-subject (not all))
11152                 (while (and
11153                         (if to-here (< (point) to-here) t)
11154                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11155                         (gnus-summary-find-next (not all))))))
11156             (gnus-set-mode-line 'summary))
11157           t))
11158     (gnus-summary-position-point)))
11159
11160 (defun gnus-summary-catchup-to-here (&optional all)
11161   "Mark all unticked articles before the current one as read.
11162 If ALL is non-nil, also mark ticked and dormant articles as read."
11163   (interactive "P")
11164   (save-excursion
11165     (gnus-save-hidden-threads
11166       (let ((beg (point)))
11167         ;; We check that there are unread articles.
11168         (when (or all (gnus-summary-find-prev))
11169           (gnus-summary-catchup all t beg)))))
11170   (gnus-summary-position-point))
11171
11172 (defun gnus-summary-catchup-from-here (&optional all)
11173   "Mark all unticked articles after (and including) the current one as read.
11174 If ALL is non-nil, also mark ticked and dormant articles as read."
11175   (interactive "P")
11176   (save-excursion
11177     (gnus-save-hidden-threads
11178       (let ((beg (point)))
11179         ;; We check that there are unread articles.
11180         (when (or all (gnus-summary-find-next))
11181           (gnus-summary-catchup all t beg nil t)))))
11182   (gnus-summary-position-point))
11183
11184 (defun gnus-summary-catchup-all (&optional quietly)
11185   "Mark all articles in this newsgroup as read.
11186 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11187 instead, which marks only unread articles as read."
11188   (interactive "P")
11189   (gnus-summary-catchup t quietly))
11190
11191 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11192   "Mark all unread articles in this group as read, then exit.
11193 If prefix argument ALL is non-nil, all articles are marked as read.
11194 If QUIETLY is non-nil, no questions will be asked."
11195   (interactive "P")
11196   (when (gnus-summary-catchup all quietly nil 'fast)
11197     ;; Select next newsgroup or exit.
11198     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11199              (eq gnus-auto-select-next 'quietly))
11200         (gnus-summary-next-group nil)
11201       (gnus-summary-exit))))
11202
11203 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11204   "Mark all articles in this newsgroup as read, and then exit.
11205 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11206 instead, which marks only unread articles as read."
11207   (interactive "P")
11208   (gnus-summary-catchup-and-exit t quietly))
11209
11210 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11211   "Mark all articles in this group as read and select the next group.
11212 If given a prefix, mark all articles, unread as well as ticked, as
11213 read."
11214   (interactive "P")
11215   (save-excursion
11216     (gnus-summary-catchup all))
11217   (gnus-summary-next-group))
11218
11219 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11220   "Mark all articles in this group as read and select the previous group.
11221 If given a prefix, mark all articles, unread as well as ticked, as
11222 read."
11223   (interactive "P")
11224   (save-excursion
11225     (gnus-summary-catchup all))
11226   (gnus-summary-next-group nil nil t))
11227
11228 ;;;
11229 ;;; with article
11230 ;;;
11231
11232 (defmacro gnus-with-article (article &rest forms)
11233   "Select ARTICLE and perform FORMS in the original article buffer.
11234 Then replace the article with the result."
11235   `(progn
11236      ;; We don't want the article to be marked as read.
11237      (let (gnus-mark-article-hook)
11238        (gnus-summary-select-article t t nil ,article))
11239      (set-buffer gnus-original-article-buffer)
11240      ,@forms
11241      (if (not (gnus-check-backend-function
11242                'request-replace-article (car gnus-article-current)))
11243          (gnus-message 5 "Read-only group; not replacing")
11244        (unless (gnus-request-replace-article
11245                 ,article (car gnus-article-current)
11246                 (current-buffer) t)
11247          (error "Couldn't replace article")))
11248      ;; The cache and backlog have to be flushed somewhat.
11249      (when gnus-keep-backlog
11250        (gnus-backlog-remove-article
11251         (car gnus-article-current) (cdr gnus-article-current)))
11252      (when gnus-use-cache
11253        (gnus-cache-update-article
11254         (car gnus-article-current) (cdr gnus-article-current)))))
11255
11256 (put 'gnus-with-article 'lisp-indent-function 1)
11257 (put 'gnus-with-article 'edebug-form-spec '(form body))
11258
11259 ;; Thread-based commands.
11260
11261 (defun gnus-summary-articles-in-thread (&optional article)
11262   "Return a list of all articles in the current thread.
11263 If ARTICLE is non-nil, return all articles in the thread that starts
11264 with that article."
11265   (let* ((article (or article (gnus-summary-article-number)))
11266          (data (gnus-data-find-list article))
11267          (top-level (gnus-data-level (car data)))
11268          (top-subject
11269           (cond ((null gnus-thread-operation-ignore-subject)
11270                  (gnus-simplify-subject-re
11271                   (mail-header-subject (gnus-data-header (car data)))))
11272                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11273                  (gnus-simplify-subject-fuzzy
11274                   (mail-header-subject (gnus-data-header (car data)))))
11275                 (t nil)))
11276          (end-point (save-excursion
11277                       (goto-char (gnus-data-pos (car data)))
11278                       (if (gnus-summary-go-to-next-thread)
11279                           (point) (point-max))))
11280          articles)
11281     (while (and data
11282                 (< (gnus-data-pos (car data)) end-point))
11283       (when (or (not top-subject)
11284                 (string= top-subject
11285                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11286                              (gnus-simplify-subject-fuzzy
11287                               (mail-header-subject
11288                                (gnus-data-header (car data))))
11289                            (gnus-simplify-subject-re
11290                             (mail-header-subject
11291                              (gnus-data-header (car data)))))))
11292         (push (gnus-data-number (car data)) articles))
11293       (unless (and (setq data (cdr data))
11294                    (> (gnus-data-level (car data)) top-level))
11295         (setq data nil)))
11296     ;; Return the list of articles.
11297     (nreverse articles)))
11298
11299 (defun gnus-summary-rethread-current ()
11300   "Rethread the thread the current article is part of."
11301   (interactive)
11302   (let* ((gnus-show-threads t)
11303          (article (gnus-summary-article-number))
11304          (id (mail-header-id (gnus-summary-article-header)))
11305          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11306     (unless id
11307       (error "No article on the current line"))
11308     (gnus-rebuild-thread id)
11309     (gnus-summary-goto-subject article)))
11310
11311 (defun gnus-summary-reparent-thread ()
11312   "Make the current article child of the marked (or previous) article.
11313
11314 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11315 is non-nil or the Subject: of both articles are the same."
11316   (interactive)
11317   (unless (not (gnus-group-read-only-p))
11318     (error "The current newsgroup does not support article editing"))
11319   (unless (<= (length gnus-newsgroup-processable) 1)
11320     (error "No more than one article may be marked"))
11321   (let ((child (gnus-summary-article-number))
11322         ;; First grab the marked article, otherwise one line up.
11323         (parent (if (not (null gnus-newsgroup-processable))
11324                     (car gnus-newsgroup-processable)
11325                   (save-excursion
11326                     (if (eq (forward-line -1) 0)
11327                         (gnus-summary-article-number)
11328                       (error "Beginning of summary buffer"))))))
11329     (gnus-summary-reparent-children parent (list child))))
11330
11331 (defun gnus-summary-reparent-children (parent children)
11332   "Make PARENT the parent of CHILDREN.
11333 When called interactively, PARENT is the current article and CHILDREN
11334 are the process-marked articles."
11335   (interactive
11336    (list (gnus-summary-article-number)
11337          (gnus-summary-work-articles nil)))
11338   (dolist (child children)
11339     (save-window-excursion
11340       (let ((gnus-article-buffer " *reparent*"))
11341         (unless (not (eq parent child))
11342           (error "An article may not be self-referential"))
11343         (let ((message-id (mail-header-id
11344                            (gnus-summary-article-header parent))))
11345           (unless (and message-id (not (equal message-id "")))
11346             (error "No message-id in desired parent"))
11347           (gnus-with-article child
11348             (save-restriction
11349               (goto-char (point-min))
11350               (message-narrow-to-head)
11351               (if (re-search-forward "^References: " nil t)
11352                   (progn
11353                     (re-search-forward "^[^ \t]" nil t)
11354                     (forward-line -1)
11355                     (end-of-line)
11356                     (insert " " message-id))
11357                 (insert "References: " message-id "\n"))))
11358           (set-buffer gnus-summary-buffer)
11359           (gnus-summary-unmark-all-processable)
11360           (gnus-summary-update-article child)
11361           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11362             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11363           (gnus-summary-rethread-current)
11364           (gnus-message 3 "Article %d is now the child of article %d"
11365                         child parent))))))
11366
11367 (defun gnus-summary-toggle-threads (&optional arg)
11368   "Toggle showing conversation threads.
11369 If ARG is positive number, turn showing conversation threads on."
11370   (interactive "P")
11371   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11372     (setq gnus-show-threads
11373           (if (null arg) (not gnus-show-threads)
11374             (> (prefix-numeric-value arg) 0)))
11375     (gnus-summary-prepare)
11376     (gnus-summary-goto-subject current)
11377     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11378     (gnus-summary-position-point)))
11379
11380 (eval-and-compile
11381   (if (fboundp 'remove-overlays)
11382       (defalias 'gnus-remove-overlays 'remove-overlays)
11383     (defun gnus-remove-overlays (beg end name val)
11384       "Clear BEG and END of overlays whose property NAME has value VAL.
11385 For compatibility with XEmacs."
11386       (dolist (ov (gnus-overlays-in beg end))
11387         (when (eq (gnus-overlay-get ov name) val)
11388           (gnus-delete-overlay ov))))))
11389
11390 (defun gnus-summary-show-all-threads ()
11391   "Show all threads."
11392   (interactive)
11393   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11394   (gnus-summary-position-point))
11395
11396 (defsubst gnus-summary--inv (p)
11397   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11398
11399 (defun gnus-summary-show-thread ()
11400   "Show thread subtrees.
11401 Returns nil if no thread was there to be shown."
11402   (interactive)
11403   (let* ((orig (point))
11404          (end (point-at-eol))
11405          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11406          ;; Leave point at bol
11407          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11408          (eoi (when end
11409                 (if (fboundp 'next-single-char-property-change)
11410                     (or (next-single-char-property-change end 'invisible)
11411                         (point-max))
11412                   (while (progn
11413                            (end-of-line 2)
11414                            (and (not (eobp))
11415                                 (eq (get-char-property (point) 'invisible)
11416                                     'gnus-sum))))
11417                   (point)))))
11418     (when eoi
11419       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11420       (goto-char orig)
11421       (gnus-summary-position-point)
11422       eoi)))
11423
11424 (defun gnus-summary-maybe-hide-threads ()
11425   "If requested, hide the threads that should be hidden."
11426   (when (and gnus-show-threads
11427              gnus-thread-hide-subtree)
11428     (gnus-summary-hide-all-threads
11429      (if (or (consp gnus-thread-hide-subtree)
11430              (functionp gnus-thread-hide-subtree))
11431          (gnus-make-predicate gnus-thread-hide-subtree)
11432        nil))))
11433
11434 ;;; Hiding predicates.
11435
11436 (defun gnus-article-unread-p (header)
11437   (memq (mail-header-number header) gnus-newsgroup-unreads))
11438
11439 (defun gnus-article-unseen-p (header)
11440   (memq (mail-header-number header) gnus-newsgroup-unseen))
11441
11442 (defun gnus-map-articles (predicate articles)
11443   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11444   (apply 'gnus-or (mapcar predicate
11445                           (mapcar (lambda (number)
11446                                     (gnus-summary-article-header number))
11447                                   articles))))
11448
11449 (defun gnus-summary-hide-all-threads (&optional predicate)
11450   "Hide all thread subtrees.
11451 If PREDICATE is supplied, threads that satisfy this predicate
11452 will not be hidden."
11453   (interactive)
11454   (save-excursion
11455     (goto-char (point-min))
11456     (let ((end nil))
11457       (while (not end)
11458         (when (or (not predicate)
11459                   (gnus-map-articles
11460                    predicate (gnus-summary-article-children)))
11461             (gnus-summary-hide-thread))
11462         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11463   (gnus-summary-position-point))
11464
11465 (defun gnus-summary-hide-thread ()
11466   "Hide thread subtrees.
11467 If PREDICATE is supplied, threads that satisfy this predicate
11468 will not be hidden.
11469 Returns nil if no threads were there to be hidden."
11470   (interactive)
11471   (let ((start (point))
11472         (starteol (line-end-position))
11473         (article (gnus-summary-article-number)))
11474     (goto-char start)
11475     ;; Go forward until either the buffer ends or the subthread ends.
11476     (when (and (not (eobp))
11477                (or (zerop (gnus-summary-next-thread 1 t))
11478                    (goto-char (point-max))))
11479       (if (and (> (point) start)
11480                ;; FIXME: this should actually search for a non-invisible \n.
11481                (search-backward "\n" start t))
11482           (progn
11483             (when (> (point) starteol)
11484               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11485               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11486                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11487                 (gnus-overlay-put ol 'evaporate t)))
11488             (gnus-summary-goto-subject article))
11489         (goto-char start)
11490         nil))))
11491
11492 (defun gnus-summary-go-to-next-thread (&optional previous)
11493   "Go to the same level (or less) next thread.
11494 If PREVIOUS is non-nil, go to previous thread instead.
11495 Return the article number moved to, or nil if moving was impossible."
11496   (let ((level (gnus-summary-thread-level))
11497         (way (if previous -1 1))
11498         (beg (point)))
11499     (forward-line way)
11500     (while (and (not (eobp))
11501                 (< level (gnus-summary-thread-level)))
11502       (forward-line way))
11503     (if (eobp)
11504         (progn
11505           (goto-char beg)
11506           nil)
11507       (setq beg (point))
11508       (prog1
11509           (gnus-summary-article-number)
11510         (goto-char beg)))))
11511
11512 (defun gnus-summary-next-thread (n &optional silent)
11513   "Go to the same level next N'th thread.
11514 If N is negative, search backward instead.
11515 Returns the difference between N and the number of skips actually
11516 done.
11517
11518 If SILENT, don't output messages."
11519   (interactive "p")
11520   (let ((backward (< n 0))
11521         (n (abs n)))
11522     (while (and (> n 0)
11523                 (gnus-summary-go-to-next-thread backward))
11524       (decf n))
11525     (unless silent
11526       (gnus-summary-position-point))
11527     (when (and (not silent) (/= 0 n))
11528       (gnus-message 7 "No more threads"))
11529     n))
11530
11531 (defun gnus-summary-prev-thread (n)
11532   "Go to the same level previous N'th thread.
11533 Returns the difference between N and the number of skips actually
11534 done."
11535   (interactive "p")
11536   (gnus-summary-next-thread (- n)))
11537
11538 (defun gnus-summary-go-down-thread ()
11539   "Go down one level in the current thread."
11540   (let ((children (gnus-summary-article-children)))
11541     (when children
11542       (gnus-summary-goto-subject (car children)))))
11543
11544 (defun gnus-summary-go-up-thread ()
11545   "Go up one level in the current thread."
11546   (let ((parent (gnus-summary-article-parent)))
11547     (when parent
11548       (gnus-summary-goto-subject parent))))
11549
11550 (defun gnus-summary-down-thread (n)
11551   "Go down thread N steps.
11552 If N is negative, go up instead.
11553 Returns the difference between N and how many steps down that were
11554 taken."
11555   (interactive "p")
11556   (let ((up (< n 0))
11557         (n (abs n)))
11558     (while (and (> n 0)
11559                 (if up (gnus-summary-go-up-thread)
11560                   (gnus-summary-go-down-thread)))
11561       (setq n (1- n)))
11562     (gnus-summary-position-point)
11563     (when (/= 0 n)
11564       (gnus-message 7 "Can't go further"))
11565     n))
11566
11567 (defun gnus-summary-up-thread (n)
11568   "Go up thread N steps.
11569 If N is negative, go down instead.
11570 Returns the difference between N and how many steps down that were
11571 taken."
11572   (interactive "p")
11573   (gnus-summary-down-thread (- n)))
11574
11575 (defun gnus-summary-top-thread ()
11576   "Go to the top of the thread."
11577   (interactive)
11578   (while (gnus-summary-go-up-thread))
11579   (gnus-summary-article-number))
11580
11581 (defun gnus-summary-expire-thread ()
11582   "Mark articles under current thread as expired."
11583   (interactive)
11584   (gnus-summary-kill-thread 0))
11585
11586 (defun gnus-summary-kill-thread (&optional unmark)
11587   "Mark articles under current thread as read.
11588 If the prefix argument is positive, remove any kinds of marks.
11589 If the prefix argument is zero, mark thread as expired.
11590 If the prefix argument is negative, tick articles instead."
11591   (interactive "P")
11592   (when unmark
11593     (setq unmark (prefix-numeric-value unmark)))
11594   (let ((articles (gnus-summary-articles-in-thread))
11595         (hide (or (null unmark) (= unmark 0))))
11596     (save-excursion
11597       ;; Expand the thread.
11598       (gnus-summary-show-thread)
11599       ;; Mark all the articles.
11600       (while articles
11601         (gnus-summary-goto-subject (car articles))
11602         (cond ((null unmark)
11603                (gnus-summary-mark-article-as-read gnus-killed-mark))
11604               ((> unmark 0)
11605                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11606               ((= unmark 0)
11607                (gnus-summary-mark-article nil gnus-expirable-mark))
11608               (t
11609                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11610         (setq articles (cdr articles))))
11611     ;; Hide killed subtrees when hide is true.
11612     (and hide
11613          gnus-thread-hide-killed
11614          (gnus-summary-hide-thread))
11615     ;; If hide is t, go to next unread subject.
11616     (when hide
11617       ;; Go to next unread subject.
11618       (gnus-summary-next-subject 1 t)))
11619   (gnus-set-mode-line 'summary))
11620
11621 ;; Summary sorting commands
11622
11623 (defun gnus-summary-sort-by-number (&optional reverse)
11624   "Sort the summary buffer by article number.
11625 Argument REVERSE means reverse order."
11626   (interactive "P")
11627   (gnus-summary-sort 'number reverse))
11628
11629 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11630   "Sort the summary buffer by most recent article number.
11631 Argument REVERSE means reverse order."
11632   (interactive "P")
11633   (gnus-summary-sort 'most-recent-number reverse))
11634
11635 (defun gnus-summary-sort-by-random (&optional reverse)
11636   "Randomize the order in the summary buffer.
11637 Argument REVERSE means to randomize in reverse order."
11638   (interactive "P")
11639   (gnus-summary-sort 'random reverse))
11640
11641 (defun gnus-summary-sort-by-author (&optional reverse)
11642   "Sort the summary buffer by author name alphabetically.
11643 If `case-fold-search' is non-nil, case of letters is ignored.
11644 Argument REVERSE means reverse order."
11645   (interactive "P")
11646   (gnus-summary-sort 'author reverse))
11647
11648 (defun gnus-summary-sort-by-recipient (&optional reverse)
11649   "Sort the summary buffer by recipient name alphabetically.
11650 If `case-fold-search' is non-nil, case of letters is ignored.
11651 Argument REVERSE means reverse order."
11652   (interactive "P")
11653   (gnus-summary-sort 'recipient reverse))
11654
11655 (defun gnus-summary-sort-by-subject (&optional reverse)
11656   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11657 If `case-fold-search' is non-nil, case of letters is ignored.
11658 Argument REVERSE means reverse order."
11659   (interactive "P")
11660   (gnus-summary-sort 'subject reverse))
11661
11662 (defun gnus-summary-sort-by-date (&optional reverse)
11663   "Sort the summary buffer by date.
11664 Argument REVERSE means reverse order."
11665   (interactive "P")
11666   (gnus-summary-sort 'date reverse))
11667
11668 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11669   "Sort the summary buffer by most recent date.
11670 Argument REVERSE means reverse order."
11671   (interactive "P")
11672   (gnus-summary-sort 'most-recent-date reverse))
11673
11674 (defun gnus-summary-sort-by-score (&optional reverse)
11675   "Sort the summary buffer by score.
11676 Argument REVERSE means reverse order."
11677   (interactive "P")
11678   (gnus-summary-sort 'score reverse))
11679
11680 (defun gnus-summary-sort-by-lines (&optional reverse)
11681   "Sort the summary buffer by the number of lines.
11682 Argument REVERSE means reverse order."
11683   (interactive "P")
11684   (gnus-summary-sort 'lines reverse))
11685
11686 (defun gnus-summary-sort-by-chars (&optional reverse)
11687   "Sort the summary buffer by article length.
11688 Argument REVERSE means reverse order."
11689   (interactive "P")
11690   (gnus-summary-sort 'chars reverse))
11691
11692 (defun gnus-summary-sort-by-original (&optional reverse)
11693   "Sort the summary buffer using the default sorting method.
11694 Argument REVERSE means reverse order."
11695   (interactive "P")
11696   (let* ((inhibit-read-only t)
11697          (gnus-summary-prepare-hook nil))
11698     ;; We do the sorting by regenerating the threads.
11699     (gnus-summary-prepare)
11700     ;; Hide subthreads if needed.
11701     (gnus-summary-maybe-hide-threads)))
11702
11703 (defun gnus-summary-sort (predicate reverse)
11704   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11705   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11706          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11707          (gnus-thread-sort-functions
11708           (if (not reverse)
11709               thread
11710             `(lambda (t1 t2)
11711                (,thread t2 t1))))
11712          (gnus-sort-gathered-threads-function
11713           gnus-thread-sort-functions)
11714          (gnus-article-sort-functions
11715           (if (not reverse)
11716               article
11717             `(lambda (t1 t2)
11718                (,article t2 t1))))
11719          (inhibit-read-only t)
11720          (gnus-summary-prepare-hook nil))
11721     ;; We do the sorting by regenerating the threads.
11722     (gnus-summary-prepare)
11723     ;; Hide subthreads if needed.
11724     (gnus-summary-maybe-hide-threads)))
11725
11726 ;; Summary saving commands.
11727
11728 (defun gnus-summary-save-article (&optional n not-saved)
11729   "Save the current article using the default saver function.
11730 If N is a positive number, save the N next articles.
11731 If N is a negative number, save the N previous articles.
11732 If N is nil and any articles have been marked with the process mark,
11733 save those articles instead.
11734 The variable `gnus-default-article-saver' specifies the saver function.
11735
11736 If the optional second argument NOT-SAVED is non-nil, articles saved
11737 will not be marked as saved."
11738   (interactive "P")
11739   (require 'gnus-art)
11740   (let* ((articles (gnus-summary-work-articles n))
11741          (save-buffer (save-excursion
11742                         (nnheader-set-temp-buffer " *Gnus Save*")))
11743          (num (length articles))
11744          ;; Whether to save decoded articles or raw articles.
11745          (decode (when gnus-article-save-coding-system
11746                    (get gnus-default-article-saver :decode)))
11747          ;; When saving many articles in a single file, use the other
11748          ;; function to save articles other than the first one.
11749          (saver2 (get gnus-default-article-saver :function))
11750          (gnus-prompt-before-saving (if saver2
11751                                         t
11752                                       gnus-prompt-before-saving))
11753          (gnus-default-article-saver gnus-default-article-saver)
11754          header file)
11755     (dolist (article articles)
11756       (setq header (gnus-summary-article-header article))
11757       (if (not (vectorp header))
11758           ;; This is a pseudo-article.
11759           (if (assq 'name header)
11760               (gnus-copy-file (cdr (assq 'name header)))
11761             (gnus-message 1 "Article %d is unsaveable" article))
11762         ;; This is a real article.
11763         (save-window-excursion
11764           (gnus-summary-select-article decode decode nil article)
11765           (gnus-summary-goto-subject article))
11766         (with-current-buffer save-buffer
11767           (erase-buffer)
11768           (insert-buffer-substring (if decode
11769                                        gnus-article-buffer
11770                                      gnus-original-article-buffer)))
11771         (setq file (gnus-article-save save-buffer file num))
11772         (gnus-summary-remove-process-mark article)
11773         (unless not-saved
11774           (gnus-summary-set-saved-mark article)))
11775       (when saver2
11776         (setq gnus-default-article-saver saver2
11777               saver2 nil)))
11778     (gnus-kill-buffer save-buffer)
11779     (gnus-summary-position-point)
11780     (gnus-set-mode-line 'summary)
11781     n))
11782
11783 (defun gnus-summary-pipe-output (&optional n sym)
11784   "Pipe the current article to a subprocess.
11785 If N is a positive number, pipe the N next articles.
11786 If N is a negative number, pipe the N previous articles.
11787 If N is nil and any articles have been marked with the process mark,
11788 pipe those articles instead.
11789 The default command to which articles are piped is specified by the
11790 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11791 will be prompted for the command.
11792
11793 The properties `:decode' and `:headers' that are put to the function
11794 symbol `gnus-summary-save-in-pipe' control whether this function
11795 decodes articles and what headers to keep (see the doc string for the
11796 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11797 is neither omitted nor the symbol `r', force including all headers
11798 regardless of the `:headers' property.  If it is the symbol `r',
11799 articles that are not decoded and include all headers will be piped
11800 no matter what the properties `:decode' and `:headers' are."
11801   (interactive (gnus-interactive "P\ny"))
11802   (require 'gnus-art)
11803   (let* ((articles (gnus-summary-work-articles n))
11804          (result-buffer "*Shell Command Output*")
11805          (all-headers (not (memq sym '(nil r))))
11806          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11807          (raw (eq sym 'r))
11808          (headers (get 'gnus-summary-save-in-pipe :headers))
11809          command result)
11810     (unless (numberp (car articles))
11811       (error "No article to pipe"))
11812     (setq command (gnus-read-shell-command
11813                    (concat "Shell command on "
11814                            (if (cdr articles)
11815                                (format "these %d articles" (length articles))
11816                              "this article")
11817                            ": ")
11818                    gnus-summary-pipe-output-default-command))
11819     (when (string-equal command "")
11820       (error "A command is required"))
11821     (when all-headers
11822       (put 'gnus-summary-save-in-pipe :headers nil))
11823     (unwind-protect
11824         (while articles
11825           (gnus-summary-goto-subject (pop articles))
11826           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11827           (when (and (get-buffer result-buffer)
11828                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11829             (setq result (concat result (with-current-buffer result-buffer
11830                                           (buffer-string))))))
11831       (put 'gnus-summary-save-in-pipe :headers headers))
11832     (unless (zerop (length result))
11833       (if (with-current-buffer (get-buffer-create result-buffer)
11834             (erase-buffer)
11835             (insert result)
11836             (prog1
11837                 (and (= (count-lines (point-min) (point)) 1)
11838                      (progn
11839                        (end-of-line 0)
11840                        (<= (current-column)
11841                            (window-width (minibuffer-window)))))
11842               (goto-char (point-min))))
11843           (message "%s" (substring result 0 -1))
11844         (message nil)
11845         (gnus-configure-windows 'pipe)))))
11846
11847 (defun gnus-summary-save-article-mail (&optional arg)
11848   "Append the current article to a Unix mail box file.
11849 If N is a positive number, save the N next articles.
11850 If N is a negative number, save the N previous articles.
11851 If N is nil and any articles have been marked with the process mark,
11852 save those articles instead."
11853   (interactive "P")
11854   (require 'gnus-art)
11855   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11856     (gnus-summary-save-article arg)))
11857
11858 (defun gnus-summary-save-article-rmail (&optional arg)
11859   "Append the current article to an rmail file.
11860 If N is a positive number, save the N next articles.
11861 If N is a negative number, save the N previous articles.
11862 If N is nil and any articles have been marked with the process mark,
11863 save those articles instead."
11864   (interactive "P")
11865   (require 'gnus-art)
11866   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11867     (gnus-summary-save-article arg)))
11868
11869 (defun gnus-summary-save-article-file (&optional arg)
11870   "Append the current article to a file.
11871 If N is a positive number, save the N next articles.
11872 If N is a negative number, save the N previous articles.
11873 If N is nil and any articles have been marked with the process mark,
11874 save those articles instead."
11875   (interactive "P")
11876   (require 'gnus-art)
11877   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11878     (gnus-summary-save-article arg)))
11879
11880 (defun gnus-summary-write-article-file (&optional arg)
11881   "Write the current article to a file, deleting the previous file.
11882 If N is a positive number, save the N next articles.
11883 If N is a negative number, save the N previous articles.
11884 If N is nil and any articles have been marked with the process mark,
11885 save those articles instead."
11886   (interactive "P")
11887   (require 'gnus-art)
11888   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11889     (gnus-summary-save-article arg)))
11890
11891 (defun gnus-summary-save-article-body-file (&optional arg)
11892   "Append the current article body to a file.
11893 If N is a positive number, save the N next articles.
11894 If N is a negative number, save the N previous articles.
11895 If N is nil and any articles have been marked with the process mark,
11896 save those articles instead."
11897   (interactive "P")
11898   (require 'gnus-art)
11899   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11900     (gnus-summary-save-article arg)))
11901
11902 (defun gnus-summary-write-article-body-file (&optional arg)
11903   "Write the current article body to a file, deleting the previous file.
11904 If N is a positive number, save the N next articles.
11905 If N is a negative number, save the N previous articles.
11906 If N is nil and any articles have been marked with the process mark,
11907 save those articles instead."
11908   (interactive "P")
11909   (require 'gnus-art)
11910   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11911     (gnus-summary-save-article arg)))
11912
11913 (defun gnus-summary-muttprint (&optional arg)
11914   "Print the current article using Muttprint.
11915 If N is a positive number, save the N next articles.
11916 If N is a negative number, save the N previous articles.
11917 If N is nil and any articles have been marked with the process mark,
11918 save those articles instead."
11919   (interactive "P")
11920   (require 'gnus-art)
11921   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11922     (gnus-summary-save-article arg t)))
11923
11924 (defun gnus-summary-pipe-message (program)
11925   "Pipe the current article through PROGRAM."
11926   (interactive "sProgram: ")
11927   (gnus-summary-select-article)
11928   (let ((mail-header-separator ""))
11929     (gnus-eval-in-buffer-window gnus-article-buffer
11930       (save-restriction
11931         (widen)
11932         (let ((start (window-start))
11933               (inhibit-read-only t))
11934           (message-pipe-buffer-body program)
11935           (set-window-start (get-buffer-window (current-buffer)) start))))))
11936
11937 (defun gnus-get-split-value (methods)
11938   "Return a value based on the split METHODS."
11939   (let (split-name method result match)
11940     (when methods
11941       (with-current-buffer gnus-original-article-buffer
11942         (save-restriction
11943           (nnheader-narrow-to-headers)
11944           (while (and methods (not split-name))
11945             (goto-char (point-min))
11946             (setq method (pop methods))
11947             (setq match (car method))
11948             (when (cond
11949                    ((stringp match)
11950                     ;; Regular expression.
11951                     (ignore-errors
11952                       (re-search-forward match nil t)))
11953                    ((functionp match)
11954                     ;; Function.
11955                     (save-restriction
11956                       (widen)
11957                       (setq result (funcall match gnus-newsgroup-name))))
11958                    ((consp match)
11959                     ;; Form.
11960                     (save-restriction
11961                       (widen)
11962                       (setq result (eval match)))))
11963               (setq split-name (cdr method))
11964               (cond ((stringp result)
11965                      (push (expand-file-name
11966                             result gnus-article-save-directory)
11967                            split-name))
11968                     ((consp result)
11969                      (setq split-name (append result split-name)))))))))
11970     (nreverse split-name)))
11971
11972 (defun gnus-valid-move-group-p (group)
11973   (and (symbolp group)
11974        (boundp group)
11975        (symbol-name group)
11976        (symbol-value group)
11977        (gnus-get-function (gnus-find-method-for-group
11978                            (symbol-name group)) 'request-accept-article t)))
11979
11980 (defun gnus-read-move-group-name (prompt default articles prefix)
11981   "Read a group name."
11982   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11983          (minibuffer-confirm-incomplete nil) ; XEmacs
11984          (prom
11985           (format "%s %s to"
11986                   prompt
11987                   (if (> (length articles) 1)
11988                       (format "these %d articles" (length articles))
11989                     "this article")))
11990          (to-newsgroup
11991           (cond
11992            ((null split-name)
11993             (gnus-group-completing-read
11994              prom
11995              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
11996              nil prefix nil default))
11997            ((= 1 (length split-name))
11998             (gnus-group-completing-read
11999              prom
12000              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12001              nil prefix 'gnus-group-history (car split-name)))
12002            (t
12003             (gnus-completing-read
12004              prom (nreverse split-name) nil nil 'gnus-group-history))))
12005          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12006          encoded)
12007     (when to-newsgroup
12008       (if (or (string= to-newsgroup "")
12009               (string= to-newsgroup prefix))
12010           (setq to-newsgroup default))
12011       (unless to-newsgroup
12012         (error "No group name entered"))
12013       (setq encoded (mm-encode-coding-string
12014                      to-newsgroup
12015                      (gnus-group-name-charset to-method to-newsgroup)))
12016       (or (gnus-active encoded)
12017           (gnus-activate-group encoded nil nil to-method)
12018           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12019                                      to-newsgroup))
12020               (or (and (gnus-request-create-group encoded to-method)
12021                        (gnus-activate-group encoded nil nil to-method)
12022                        (gnus-subscribe-group encoded))
12023                   (error "Couldn't create group %s" to-newsgroup)))
12024           (error "No such group: %s" to-newsgroup))
12025       encoded)))
12026
12027 (defvar gnus-summary-save-parts-counter)
12028 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12029
12030 (defun gnus-summary-save-parts (type dir n &optional reverse)
12031   "Save parts matching TYPE to DIR.
12032 If REVERSE, save parts that do not match TYPE."
12033   (interactive
12034    (list (read-string "Save parts of type: "
12035                       (or (car gnus-summary-save-parts-type-history)
12036                           gnus-summary-save-parts-default-mime)
12037                       'gnus-summary-save-parts-type-history)
12038          (setq gnus-summary-save-parts-last-directory
12039                (read-file-name "Save to directory: "
12040                                gnus-summary-save-parts-last-directory
12041                                nil t))
12042          current-prefix-arg))
12043   (gnus-summary-iterate n
12044     (let ((gnus-display-mime-function nil)
12045           gnus-article-prepare-hook
12046           gnus-article-decode-hook
12047           gnus-display-mime-function
12048           gnus-break-pages
12049           (gnus-inhibit-treatment t))
12050       (gnus-summary-select-article))
12051     (with-current-buffer gnus-article-buffer
12052       (let ((handles (or gnus-article-mime-handles
12053                          (mm-dissect-buffer nil gnus-article-loose-mime)
12054                          (and gnus-article-emulate-mime
12055                               (mm-uu-dissect))))
12056             (gnus-summary-save-parts-counter 1))
12057         (when handles
12058           (gnus-summary-save-parts-1 type dir handles reverse)
12059           (unless gnus-article-mime-handles ;; Don't destroy this case.
12060             (mm-destroy-parts handles)))))))
12061
12062 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12063   (if (stringp (car handle))
12064       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12065               (cdr handle))
12066     (when (if reverse
12067               (not (string-match type (mm-handle-media-type handle)))
12068             (string-match type (mm-handle-media-type handle)))
12069       (let ((file (expand-file-name
12070                    (gnus-map-function
12071                     mm-file-name-rewrite-functions
12072                     (file-name-nondirectory
12073                      (or
12074                       (mail-content-type-get
12075                        (mm-handle-disposition handle) 'filename)
12076                       (mail-content-type-get
12077                        (mm-handle-type handle) 'name)
12078                       (format "%s.%d.%d" gnus-newsgroup-name
12079                               (cdr gnus-article-current)
12080                               gnus-summary-save-parts-counter))))
12081                    dir)))
12082         (incf gnus-summary-save-parts-counter)
12083         (unless (file-exists-p file)
12084           (mm-save-part-to-file handle file))))))
12085
12086 ;; Summary extract commands
12087
12088 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12089   (let ((inhibit-read-only t)
12090         (article (gnus-summary-article-number))
12091         after-article b e)
12092     (unless (gnus-summary-goto-subject article)
12093       (error "No such article: %d" article))
12094     (gnus-summary-position-point)
12095     ;; If all commands are to be bunched up on one line, we collect
12096     ;; them here.
12097     (unless gnus-view-pseudos-separately
12098       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12099             files action)
12100         (while ps
12101           (setq action (cdr (assq 'action (car ps))))
12102           (setq files (list (cdr (assq 'name (car ps)))))
12103           (while (and ps (cdr ps)
12104                       (string= (or action "1")
12105                                (or (cdr (assq 'action (cadr ps))) "2")))
12106             (push (cdr (assq 'name (cadr ps))) files)
12107             (setcdr ps (cddr ps)))
12108           (when files
12109             (when (not (string-match "%s" action))
12110               (push " " files))
12111             (push " " files)
12112             (when (assq 'execute (car ps))
12113               (setcdr (assq 'execute (car ps))
12114                       (funcall (if (string-match "%s" action)
12115                                    'format 'concat)
12116                                action
12117                                (mapconcat
12118                                 (lambda (f)
12119                                   (if (equal f " ")
12120                                       f
12121                                     (shell-quote-argument f)))
12122                                 files " ")))))
12123           (setq ps (cdr ps)))))
12124     (if (and gnus-view-pseudos (not not-view))
12125         (while pslist
12126           (when (assq 'execute (car pslist))
12127             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12128                                   (eq gnus-view-pseudos 'not-confirm)))
12129           (setq pslist (cdr pslist)))
12130       (save-excursion
12131         (while pslist
12132           (setq after-article (or (cdr (assq 'article (car pslist)))
12133                                   (gnus-summary-article-number)))
12134           (gnus-summary-goto-subject after-article)
12135           (forward-line 1)
12136           (setq b (point))
12137           (insert "    " (file-name-nondirectory
12138                           (cdr (assq 'name (car pslist))))
12139                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12140           (setq e (point))
12141           (forward-line -1)             ; back to `b'
12142           (gnus-add-text-properties
12143            b (1- e) (list 'gnus-number gnus-reffed-article-number
12144                           gnus-mouse-face-prop gnus-mouse-face))
12145           (gnus-data-enter
12146            after-article gnus-reffed-article-number
12147            gnus-unread-mark b (car pslist) 0 (- e b))
12148           (setq gnus-newsgroup-unreads
12149                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12150                                          gnus-reffed-article-number))
12151           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12152           (setq pslist (cdr pslist)))))))
12153
12154 (defun gnus-pseudos< (p1 p2)
12155   (let ((c1 (cdr (assq 'action p1)))
12156         (c2 (cdr (assq 'action p2))))
12157     (and c1 c2 (string< c1 c2))))
12158
12159 (defun gnus-request-pseudo-article (props)
12160   (cond ((assq 'execute props)
12161          (gnus-execute-command (cdr (assq 'execute props)))))
12162   (let ((gnus-current-article (gnus-summary-article-number)))
12163     (gnus-run-hooks 'gnus-mark-article-hook)))
12164
12165 (defun gnus-execute-command (command &optional automatic)
12166   (save-excursion
12167     (gnus-article-setup-buffer)
12168     (set-buffer gnus-article-buffer)
12169     (setq buffer-read-only nil)
12170     (let ((command (if automatic command
12171                      (read-string "Command: " (cons command 0)))))
12172       (erase-buffer)
12173       (insert "$ " command "\n\n")
12174       (if gnus-view-pseudo-asynchronously
12175           (start-process "gnus-execute" (current-buffer) shell-file-name
12176                          shell-command-switch command)
12177         (call-process shell-file-name nil t nil
12178                       shell-command-switch command)))))
12179
12180 ;; Summary kill commands.
12181
12182 (defun gnus-summary-edit-global-kill (article)
12183   "Edit the \"global\" kill file."
12184   (interactive (list (gnus-summary-article-number)))
12185   (gnus-group-edit-global-kill article))
12186
12187 (defun gnus-summary-edit-local-kill ()
12188   "Edit a local kill file applied to the current newsgroup."
12189   (interactive)
12190   (setq gnus-current-headers (gnus-summary-article-header))
12191   (gnus-group-edit-local-kill
12192    (gnus-summary-article-number) gnus-newsgroup-name))
12193
12194 ;;; Header reading.
12195
12196 (defun gnus-read-header (id &optional header)
12197   "Read the headers of article ID and enter them into the Gnus system."
12198   (let ((group gnus-newsgroup-name)
12199         (gnus-override-method
12200          (or
12201           gnus-override-method
12202           (and (gnus-news-group-p gnus-newsgroup-name)
12203                (car (gnus-refer-article-methods)))))
12204         where)
12205     ;; First we check to see whether the header in question is already
12206     ;; fetched.
12207     (if (stringp id)
12208         ;; This is a Message-ID.
12209         (setq header (or header (gnus-id-to-header id)))
12210       ;; This is an article number.
12211       (setq header (or header (gnus-summary-article-header id))))
12212     (if (and header
12213              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12214         ;; We have found the header.
12215         header
12216       ;; We have to really fetch the header to this article.
12217       (with-current-buffer nntp-server-buffer
12218         (when (setq where (gnus-request-head id group))
12219           (nnheader-fold-continuation-lines)
12220           (goto-char (point-max))
12221           (insert ".\n")
12222           (goto-char (point-min))
12223           (insert "211 ")
12224           (princ (cond
12225                   ((numberp id) id)
12226                   ((cdr where) (cdr where))
12227                   (header (mail-header-number header))
12228                   (t gnus-reffed-article-number))
12229                  (current-buffer))
12230           (insert " Article retrieved.\n"))
12231         (if (or (not where)
12232                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12233             ()                          ; Malformed head.
12234           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12235             (when (and (stringp id)
12236                        (or
12237                         (not (string= (gnus-group-real-name group)
12238                                       (car where)))
12239                         (not (gnus-server-equal gnus-override-method
12240                                                 (gnus-group-method group)))))
12241               ;; If we fetched by Message-ID and the article came from
12242               ;; a different group (or server), we fudge some bogus
12243               ;; article numbers for this article.
12244               (mail-header-set-number header gnus-reffed-article-number))
12245             (with-current-buffer gnus-summary-buffer
12246               (decf gnus-reffed-article-number)
12247               (gnus-remove-header (mail-header-number header))
12248               (push header gnus-newsgroup-headers)
12249               (setq gnus-current-headers header)
12250               (push (mail-header-number header) gnus-newsgroup-limit)))
12251           header)))))
12252
12253 (defun gnus-remove-header (number)
12254   "Remove header NUMBER from `gnus-newsgroup-headers'."
12255   (if (and gnus-newsgroup-headers
12256            (= number (mail-header-number (car gnus-newsgroup-headers))))
12257       (pop gnus-newsgroup-headers)
12258     (let ((headers gnus-newsgroup-headers))
12259       (while (and (cdr headers)
12260                   (not (= number (mail-header-number (cadr headers)))))
12261         (pop headers))
12262       (when (cdr headers)
12263         (setcdr headers (cddr headers))))))
12264
12265 ;;;
12266 ;;; summary highlights
12267 ;;;
12268
12269 (defun gnus-highlight-selected-summary ()
12270   "Highlight selected article in summary buffer."
12271   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12272   (when gnus-summary-selected-face
12273     (save-excursion
12274       (let* ((beg (point-at-bol))
12275              (end (point-at-eol))
12276              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12277              (from (if (get-text-property beg gnus-mouse-face-prop)
12278                        beg
12279                      (or (next-single-property-change
12280                           beg gnus-mouse-face-prop nil end)
12281                          beg)))
12282              (to
12283               (if (= from end)
12284                   (- from 2)
12285                 (or (next-single-property-change
12286                      from gnus-mouse-face-prop nil end)
12287                     end))))
12288         ;; If no mouse-face prop on line we will have to = from = end,
12289         ;; so we highlight the entire line instead.
12290         (when (= (+ to 2) from)
12291           (setq from beg)
12292           (setq to end))
12293         (if gnus-newsgroup-selected-overlay
12294             ;; Move old overlay.
12295             (gnus-move-overlay
12296              gnus-newsgroup-selected-overlay from to (current-buffer))
12297           ;; Create new overlay.
12298           (gnus-overlay-put
12299            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12300            'face gnus-summary-selected-face))))))
12301
12302 (defvar gnus-summary-highlight-line-cached nil)
12303 (defvar gnus-summary-highlight-line-trigger nil)
12304
12305 (defun gnus-summary-highlight-line-0 ()
12306   (if (and (eq gnus-summary-highlight-line-trigger
12307                gnus-summary-highlight)
12308            gnus-summary-highlight-line-cached)
12309       gnus-summary-highlight-line-cached
12310     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12311           gnus-summary-highlight-line-cached
12312           (let* ((cond (list 'cond))
12313                  (c cond)
12314                  (list gnus-summary-highlight))
12315             (while list
12316               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12317                               nil))
12318               (setq c (cdr c)
12319                     list (cdr list)))
12320             (gnus-byte-compile (list 'lambda nil cond))))))
12321
12322 (defun gnus-summary-highlight-line ()
12323   "Highlight current line according to `gnus-summary-highlight'."
12324   (let* ((beg (point-at-bol))
12325          (article (or (gnus-summary-article-number) gnus-current-article))
12326          (score (or (cdr (assq article
12327                                gnus-newsgroup-scored))
12328                     gnus-summary-default-score 0))
12329          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12330          (inhibit-read-only t)
12331          (default gnus-summary-default-score)
12332          (default-high gnus-summary-default-high-score)
12333          (default-low gnus-summary-default-low-score)
12334          (uncached (and gnus-summary-use-undownloaded-faces
12335                         (memq article gnus-newsgroup-undownloaded)
12336                         (not (memq article gnus-newsgroup-cached)))))
12337     (let ((face (funcall (gnus-summary-highlight-line-0))))
12338       (unless (eq face (get-text-property beg 'face))
12339         (gnus-put-text-property-excluding-characters-with-faces
12340          beg (point-at-eol) 'face
12341          (setq face (if (boundp face) (symbol-value face) face)))
12342         (when gnus-summary-highlight-line-function
12343           (funcall gnus-summary-highlight-line-function article face))))))
12344
12345 (defun gnus-update-read-articles (group unread &optional compute)
12346   "Update the list of read articles in GROUP.
12347 UNREAD is a sorted list."
12348   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12349         (info (gnus-get-info group))
12350         (prev 1)
12351         read)
12352     (if (or (not info) (not active))
12353         ;; There is no info on this group if it was, in fact,
12354         ;; killed.  Gnus stores no information on killed groups, so
12355         ;; there's nothing to be done.
12356         ;; One could store the information somewhere temporarily,
12357         ;; perhaps...  Hmmm...
12358         ()
12359       ;; Remove any negative articles numbers.
12360       (while (and unread (< (car unread) 0))
12361         (setq unread (cdr unread)))
12362       ;; Remove any expired article numbers
12363       (while (and unread (< (car unread) (car active)))
12364         (setq unread (cdr unread)))
12365       ;; Compute the ranges of read articles by looking at the list of
12366       ;; unread articles.
12367       (while unread
12368         (when (/= (car unread) prev)
12369           (push (if (= prev (1- (car unread))) prev
12370                   (cons prev (1- (car unread))))
12371                 read))
12372         (setq prev (1+ (car unread)))
12373         (setq unread (cdr unread)))
12374       (when (<= prev (cdr active))
12375         (push (cons prev (cdr active)) read))
12376       (setq read (if (> (length read) 1) (nreverse read) read))
12377       (if compute
12378           read
12379         (save-excursion
12380           (let (setmarkundo)
12381             ;; Propagate the read marks to the backend.
12382             (when (and gnus-propagate-marks
12383                        (gnus-check-backend-function 'request-set-mark group))
12384               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12385                     (add (gnus-remove-from-range read (gnus-info-read info))))
12386                 (when (or add del)
12387                   (unless (gnus-check-group group)
12388                     (error "Can't open server for %s" group))
12389                   (gnus-request-set-mark
12390                    group (delq nil (list (if add (list add 'add '(read)))
12391                                          (if del (list del 'del '(read))))))
12392                   (setq setmarkundo
12393                         `(gnus-request-set-mark
12394                           ,group
12395                           ',(delq nil (list
12396                                        (if del (list del 'add '(read)))
12397                                        (if add (list add 'del '(read))))))))))
12398             (set-buffer gnus-group-buffer)
12399             (gnus-undo-register
12400               `(progn
12401                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12402                  (gnus-info-set-read ',info ',(gnus-info-read info))
12403                  (gnus-get-unread-articles-in-group ',info
12404                                                     (gnus-active ,group))
12405                  (gnus-group-update-group ,group t)
12406                  ,setmarkundo))))
12407         ;; Enter this list into the group info.
12408         (gnus-info-set-read info read)
12409         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12410         (gnus-get-unread-articles-in-group info (gnus-active group))
12411         t))))
12412
12413 (defun gnus-offer-save-summaries ()
12414   "Offer to save all active summary buffers."
12415   (let (buffers)
12416     ;; Go through all buffers and find all summaries.
12417     (dolist (buffer (buffer-list))
12418       (when (and (setq buffer (buffer-name buffer))
12419                  (string-match "Summary" buffer)
12420                  (with-current-buffer buffer
12421                    ;; We check that this is, indeed, a summary buffer.
12422                    (and (eq major-mode 'gnus-summary-mode)
12423                         ;; Also make sure this isn't bogus.
12424                         gnus-newsgroup-prepared
12425                         ;; Also make sure that this isn't a
12426                         ;; dead summary buffer.
12427                         (not gnus-dead-summary-mode))))
12428         (push buffer buffers)))
12429     ;; Go through all these summary buffers and offer to save them.
12430     (when buffers
12431       (save-excursion
12432         (map-y-or-n-p
12433          "Update summary buffer %s? "
12434          (lambda (buf)
12435            (switch-to-buffer buf)
12436            (gnus-summary-exit))
12437          buffers)))))
12438
12439 (defun gnus-summary-setup-default-charset ()
12440   "Setup newsgroup default charset."
12441   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12442       (setq gnus-newsgroup-charset nil)
12443     (let* ((ignored-charsets
12444             (or gnus-newsgroup-ephemeral-ignored-charsets
12445                 (append
12446                  (and gnus-newsgroup-name
12447                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12448                  gnus-newsgroup-ignored-charsets))))
12449       (setq gnus-newsgroup-charset
12450             (or gnus-newsgroup-ephemeral-charset
12451                 (and gnus-newsgroup-name
12452                      (gnus-parameter-charset gnus-newsgroup-name))
12453                 gnus-default-charset))
12454       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12455            ignored-charsets))))
12456
12457 ;;;
12458 ;;; Mime Commands
12459 ;;;
12460
12461 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12462   "Display the current article buffer fully MIME-buttonized.
12463 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12464 treated as multipart/mixed."
12465   (interactive "P")
12466   (require 'gnus-art)
12467   (let ((gnus-unbuttonized-mime-types nil)
12468         (gnus-mime-display-multipart-as-mixed show-all-parts))
12469     (gnus-summary-show-article)))
12470
12471 (defun gnus-summary-repair-multipart (article)
12472   "Add a Content-Type header to a multipart article without one."
12473   (interactive (list (gnus-summary-article-number)))
12474   (gnus-with-article article
12475     (message-narrow-to-head)
12476     (message-remove-header "Mime-Version")
12477     (goto-char (point-max))
12478     (insert "Mime-Version: 1.0\n")
12479     (widen)
12480     (when (search-forward "\n--" nil t)
12481       (let ((separator (buffer-substring (point) (point-at-eol))))
12482         (message-narrow-to-head)
12483         (message-remove-header "Content-Type")
12484         (goto-char (point-max))
12485         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12486                         separator))
12487         (widen))))
12488   (let (gnus-mark-article-hook)
12489     (gnus-summary-select-article t t nil article)))
12490
12491 (defun gnus-summary-toggle-display-buttonized ()
12492   "Toggle the buttonizing of the article buffer."
12493   (interactive)
12494   (require 'gnus-art)
12495   (if (setq gnus-inhibit-mime-unbuttonizing
12496             (not gnus-inhibit-mime-unbuttonizing))
12497       (let ((gnus-unbuttonized-mime-types nil))
12498         (gnus-summary-show-article))
12499     (gnus-summary-show-article)))
12500
12501 ;;;
12502 ;;; Generic summary marking commands
12503 ;;;
12504
12505 (defvar gnus-summary-marking-alist
12506   '((read gnus-del-mark "d")
12507     (unread gnus-unread-mark "u")
12508     (ticked gnus-ticked-mark "!")
12509     (dormant gnus-dormant-mark "?")
12510     (expirable gnus-expirable-mark "e"))
12511   "An alist of names/marks/keystrokes.")
12512
12513 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12514 (defvar gnus-summary-mark-map)
12515
12516 (defun gnus-summary-make-all-marking-commands ()
12517   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12518   (dolist (elem gnus-summary-marking-alist)
12519     (apply 'gnus-summary-make-marking-command elem)))
12520
12521 (defun gnus-summary-make-marking-command (name mark keystroke)
12522   (let ((map (make-sparse-keymap)))
12523     (define-key gnus-summary-generic-mark-map keystroke map)
12524     (dolist (lway `((next "next" next nil "n")
12525                     (next-unread "next unread" next t "N")
12526                     (prev "previous" prev nil "p")
12527                     (prev-unread "previous unread" prev t "P")
12528                     (nomove "" nil nil ,keystroke)))
12529       (let ((func (gnus-summary-make-marking-command-1
12530                    mark (car lway) lway name)))
12531         (setq func (eval func))
12532         (define-key map (nth 4 lway) func)))))
12533
12534 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12535   `(defun ,(intern
12536             (format "gnus-summary-put-mark-as-%s%s"
12537                     name (if (eq way 'nomove)
12538                              ""
12539                            (concat "-" (symbol-name way)))))
12540      (n)
12541      ,(format
12542        "Mark the current article as %s%s.
12543 If N, the prefix, then repeat N times.
12544 If N is negative, move in reverse order.
12545 The difference between N and the actual number of articles marked is
12546 returned."
12547        name (cadr lway))
12548      (interactive "p")
12549      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12550
12551 (defun gnus-summary-generic-mark (n mark move unread)
12552   "Mark N articles with MARK."
12553   (unless (eq major-mode 'gnus-summary-mode)
12554     (error "This command can only be used in the summary buffer"))
12555   (gnus-summary-show-thread)
12556   (let ((nummove
12557          (cond
12558           ((eq move 'next) 1)
12559           ((eq move 'prev) -1)
12560           (t 0))))
12561     (if (zerop nummove)
12562         (setq n 1)
12563       (when (< n 0)
12564         (setq n (abs n)
12565               nummove (* -1 nummove))))
12566     (while (and (> n 0)
12567                 (gnus-summary-mark-article nil mark)
12568                 (zerop (gnus-summary-next-subject nummove unread t)))
12569       (setq n (1- n)))
12570     (when (/= 0 n)
12571       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12572     (gnus-summary-recenter)
12573     (gnus-summary-position-point)
12574     (gnus-set-mode-line 'summary)
12575     n))
12576
12577 (defun gnus-summary-insert-articles (articles)
12578   (when (setq articles
12579               (gnus-sorted-difference articles
12580                                       (mapcar (lambda (h)
12581                                                 (mail-header-number h))
12582                                               gnus-newsgroup-headers)))
12583     (setq gnus-newsgroup-headers
12584           (gnus-merge 'list
12585                       gnus-newsgroup-headers
12586                       (gnus-fetch-headers articles)
12587                       'gnus-article-sort-by-number))
12588     ;; Suppress duplicates?
12589     (when gnus-suppress-duplicates
12590       (gnus-dup-suppress-articles))
12591
12592     (if (and gnus-fetch-old-headers
12593              (eq gnus-headers-retrieved-by 'nov))
12594         ;; We might want to build some more threads first.
12595         (if (eq gnus-fetch-old-headers 'invisible)
12596             (gnus-build-all-threads)
12597           (gnus-build-old-threads))
12598       ;; Mark the inserted articles that are unread as unread.
12599       (setq gnus-newsgroup-unreads
12600             (gnus-sorted-nunion
12601              gnus-newsgroup-unreads
12602              (gnus-sorted-nintersection
12603               (gnus-list-of-unread-articles gnus-newsgroup-name)
12604               articles)))
12605       ;; Mark the inserted articles as selected so that the information
12606       ;; of the marks having been changed by a user may be updated when
12607       ;; exiting this group.  See `gnus-summary-update-info'.
12608       (dolist (art articles)
12609         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12610     ;; Let the Gnus agent mark articles as read.
12611     (when gnus-agent
12612       (gnus-agent-get-undownloaded-list))
12613     ;; Remove list identifiers from subject
12614     (when gnus-list-identifiers
12615       (gnus-summary-remove-list-identifiers))
12616     ;; First and last article in this newsgroup.
12617     (when gnus-newsgroup-headers
12618       (setq gnus-newsgroup-begin
12619             (mail-header-number (car gnus-newsgroup-headers))
12620             gnus-newsgroup-end
12621             (mail-header-number
12622              (gnus-last-element gnus-newsgroup-headers))))
12623     (when gnus-use-scoring
12624       (gnus-possibly-score-headers))))
12625
12626 (defun gnus-summary-insert-old-articles (&optional all)
12627   "Insert all old articles in this group.
12628 If ALL is non-nil, already read articles become readable.
12629 If ALL is a number, fetch this number of articles."
12630   (interactive "P")
12631   (prog1
12632       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12633             older len)
12634         (setq older
12635               ;; Some nntp servers lie about their active range.  When
12636               ;; this happens, the active range can be in the millions.
12637               ;; Use a compressed range to avoid creating a huge list.
12638               (gnus-range-difference (list gnus-newsgroup-active) old))
12639         (setq len (gnus-range-length older))
12640         (cond
12641          ((null older) nil)
12642          ((numberp all)
12643           (if (< all len)
12644               (let ((older-range (nreverse older)))
12645                 (setq older nil)
12646
12647                 (while (> all 0)
12648                   (let* ((r (pop older-range))
12649                          (min (if (numberp r) r (car r)))
12650                          (max (if (numberp r) r (cdr r))))
12651                     (while (and (<= min max)
12652                                 (> all 0))
12653                       (push max older)
12654                       (setq all (1- all)
12655                             max (1- max))))))
12656             (setq older (gnus-uncompress-range older))))
12657          (all
12658           (setq older (gnus-uncompress-range older)))
12659          (t
12660           (when (and (numberp gnus-large-newsgroup)
12661                    (> len gnus-large-newsgroup))
12662               (let* ((cursor-in-echo-area nil)
12663                      (initial (gnus-parameter-large-newsgroup-initial
12664                                gnus-newsgroup-name))
12665                      (input
12666                       (read-string
12667                        (format
12668                         "How many articles from %s (%s %d): "
12669                         (gnus-group-decoded-name gnus-newsgroup-name)
12670                         (if initial "max" "default")
12671                         len)
12672                        (if initial
12673                            (cons (number-to-string initial)
12674                                  0)))))
12675                 (unless (string-match "^[ \t]*$" input)
12676                   (setq all (string-to-number input))
12677                   (if (< all len)
12678                       (let ((older-range (nreverse older)))
12679                         (setq older nil)
12680
12681                         (while (> all 0)
12682                           (let* ((r (pop older-range))
12683                                  (min (if (numberp r) r (car r)))
12684                                  (max (if (numberp r) r (cdr r))))
12685                             (while (and (<= min max)
12686                                         (> all 0))
12687                               (push max older)
12688                               (setq all (1- all)
12689                                     max (1- max))))))))))
12690           (setq older (gnus-uncompress-range older))))
12691         (if (not older)
12692             (message "No old news.")
12693           (gnus-summary-insert-articles older)
12694           (gnus-summary-limit (gnus-sorted-nunion old older))))
12695     (gnus-summary-position-point)))
12696
12697 (defun gnus-summary-insert-new-articles ()
12698   "Insert all new articles in this group."
12699   (interactive)
12700   (prog1
12701       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12702             (old-high gnus-newsgroup-highest)
12703             (nnmail-fetched-sources (list t))
12704             i new)
12705         (setq gnus-newsgroup-active
12706               (gnus-copy-sequence
12707                (gnus-activate-group gnus-newsgroup-name 'scan)))
12708         (setq i (cdr gnus-newsgroup-active)
12709               gnus-newsgroup-highest i)
12710         (while (> i old-high)
12711           (push i new)
12712           (decf i))
12713         (if (not new)
12714             (message "No gnus is bad news")
12715           (gnus-summary-insert-articles new)
12716           (setq gnus-newsgroup-unreads
12717                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12718           (gnus-summary-limit (gnus-sorted-nunion old new))))
12719     (gnus-summary-position-point)))
12720
12721 ;;; Bookmark support for Gnus.
12722 (declare-function bookmark-make-record-default
12723                   "bookmark" (&optional no-file no-context posn))
12724 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12725 (declare-function bookmark-default-handler "bookmark" (bmk))
12726 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12727 (defvar bookmark-yank-point)
12728 (defvar bookmark-current-buffer)
12729
12730 (defun gnus-summary-bookmark-make-record ()
12731   "Make a bookmark entry for a Gnus summary buffer."
12732   (let (pos buf)
12733     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12734       (save-restriction              ; FIXME is it necessary to widen?
12735         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12736       (setq buf "art") ; We are recording bookmark from article buffer.
12737       (setq bookmark-yank-point (point))
12738       (setq bookmark-current-buffer (current-buffer))
12739       (gnus-article-show-summary))      ; Go back in summary buffer.
12740     ;; We are now recording bookmark from summary buffer.
12741     (unless buf (setq buf "sum"))
12742     (let* ((subject (elt (gnus-summary-article-header) 1))
12743            (grp     (car gnus-article-current))
12744            (art     (cdr gnus-article-current))
12745            (head    (gnus-summary-article-header art))
12746            (id      (mail-header-id head)))
12747       `(,subject
12748         ,@(condition-case nil
12749               (bookmark-make-record-default 'no-file 'no-context pos)
12750             (wrong-number-of-arguments
12751              (bookmark-make-record-default 'point-only)))
12752         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12753         (group . ,grp) (article . ,art)
12754         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12755
12756 ;;;###autoload
12757 (defun gnus-summary-bookmark-jump (bookmark)
12758   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12759 BOOKMARK is a bookmark name or a bookmark record."
12760   (let ((group    (bookmark-prop-get bookmark 'group))
12761         (article  (bookmark-prop-get bookmark 'article))
12762         (id       (bookmark-prop-get bookmark 'message-id))
12763         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12764     (gnus-fetch-group group (list article))
12765     (gnus-summary-insert-cached-articles)
12766     (gnus-summary-goto-article id nil 'force)
12767     ;; FIXME we have to wait article buffer is ready (only large buffer)
12768     ;; Is there a better solution to know that?
12769     ;; If we don't wait `bookmark-default-handler' will have no chance
12770     ;; to set position. However there is no error, just wrong pos.
12771     (sit-for 1)
12772     (when (string= buf "Gnus-art")
12773       (other-window 1))
12774     (bookmark-default-handler
12775      `(""
12776        (buffer . ,(current-buffer))
12777        . ,(bookmark-get-bookmark-record bookmark)))))
12778
12779 (gnus-summary-make-all-marking-commands)
12780
12781 (gnus-ems-redefine)
12782
12783 (provide 'gnus-sum)
12784
12785 (run-hooks 'gnus-sum-load-hook)
12786
12787 ;; Local Variables:
12788 ;; coding: iso-8859-1
12789 ;; End:
12790
12791 ;;; gnus-sum.el ends here