(gnus-use-backend-marks): Removed, since it duplicates gnus-propagate-marks.
[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 nil
1238   "If non-nil, Gnus will store and retrieve marks from the backends.
1239 This means that marks will be stored both in .newsrc.eld and in
1240 the backend, and will slow operation down somewhat."
1241   :version "24.1"
1242   :type 'boolean
1243   :group 'gnus-summary-marks)
1244
1245 (defcustom gnus-alter-articles-to-read-function nil
1246   "Function to be called to alter the list of articles to be selected."
1247   :type '(choice (const nil) function)
1248   :group 'gnus-summary)
1249
1250 (defcustom gnus-orphan-score nil
1251   "*All orphans get this score added.  Set in the score file."
1252   :group 'gnus-score-default
1253   :type '(choice (const nil)
1254                  integer))
1255
1256 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1257   "*A regexp to match MIME parts when saving multiple parts of a
1258 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1259 This regexp will be used by default when prompting the user for which
1260 type of files to save."
1261   :group 'gnus-summary
1262   :type 'regexp)
1263
1264 (defcustom gnus-read-all-available-headers nil
1265   "Whether Gnus should parse all headers made available to it.
1266 This is mostly relevant for slow back ends where the user may
1267 wish to widen the summary buffer to include all headers
1268 that were fetched."
1269   :version "22.1"
1270   :group 'gnus-summary
1271   :type '(choice boolean regexp))
1272
1273 (defcustom gnus-summary-pipe-output-default-command nil
1274   "Command (and optional arguments) used to pipe article to subprocess.
1275 This will be used as the default command if it is non-nil.  The value
1276 will be updated if you modify it when executing the command
1277 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1278   :version "23.1" ;; No Gnus
1279   :group 'gnus-summary
1280   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1281
1282 (defcustom gnus-summary-muttprint-program "muttprint"
1283   "Command (and optional arguments) used to run Muttprint.
1284 The value will be updated if you modify it when executing the command
1285 `gnus-summary-muttprint'."
1286   :version "22.1"
1287   :group 'gnus-summary
1288   :type 'string)
1289
1290 (defcustom gnus-article-loose-mime t
1291   "If non-nil, don't require MIME-Version header.
1292 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1293 supply the MIME-Version header or deliberately strip it from the mail.
1294 If non-nil (the default), Gnus will treat some articles as MIME
1295 even if the MIME-Version header is missing."
1296   :version "22.1"
1297   :type 'boolean
1298   :group 'gnus-article-mime)
1299
1300 (defcustom gnus-article-emulate-mime t
1301   "If non-nil, use MIME emulation for uuencode and the like.
1302 This means that Gnus will search message bodies for text that look
1303 like uuencoded bits, yEncoded bits, and so on, and present that using
1304 the normal Gnus MIME machinery."
1305   :version "22.1"
1306   :type 'boolean
1307   :group 'gnus-article-mime)
1308
1309 ;;; Internal variables
1310
1311 (defvar gnus-summary-display-cache nil)
1312 (defvar gnus-article-mime-handles nil)
1313 (defvar gnus-article-decoded-p nil)
1314 (defvar gnus-article-charset nil)
1315 (defvar gnus-article-ignored-charsets nil)
1316 (defvar gnus-scores-exclude-files nil)
1317 (defvar gnus-page-broken nil)
1318
1319 (defvar gnus-original-article nil)
1320 (defvar gnus-article-internal-prepare-hook nil)
1321 (defvar gnus-newsgroup-process-stack nil)
1322
1323 (defvar gnus-thread-indent-array nil)
1324 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1325 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1326   "Function called to sort the articles within a thread after it has been gathered together.")
1327
1328 (defvar gnus-summary-save-parts-type-history nil)
1329 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1330
1331 ;; Avoid highlighting in kill files.
1332 (defvar gnus-summary-inhibit-highlight nil)
1333 (defvar gnus-newsgroup-selected-overlay nil)
1334 (defvar gnus-inhibit-limiting nil)
1335 (defvar gnus-newsgroup-adaptive-score-file nil)
1336 (defvar gnus-current-score-file nil)
1337 (defvar gnus-current-move-group nil)
1338 (defvar gnus-current-copy-group nil)
1339 (defvar gnus-current-crosspost-group nil)
1340 (defvar gnus-newsgroup-display nil)
1341
1342 (defvar gnus-newsgroup-dependencies nil)
1343 (defvar gnus-newsgroup-adaptive nil)
1344 (defvar gnus-summary-display-article-function nil)
1345 (defvar gnus-summary-highlight-line-function nil
1346   "Function called after highlighting a summary line.")
1347
1348 (defvar gnus-summary-line-format-alist
1349   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1350     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1351     (?s gnus-tmp-subject-or-nil ?s)
1352     (?n gnus-tmp-name ?s)
1353     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1354         ?s)
1355     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1356             gnus-tmp-from) ?s)
1357     (?F gnus-tmp-from ?s)
1358     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1359     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1360     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1361     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1362     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1363     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1364     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1365     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1366     (?L gnus-tmp-lines ?s)
1367     (?Z (or ,(gnus-macroexpand-all
1368               '(nnir-article-rsv (mail-header-number gnus-tmp-header)))
1369             0) ?d)
1370     (?G (or ,(gnus-macroexpand-all
1371               '(nnir-article-group (mail-header-number gnus-tmp-header)))
1372             "") ?s)
1373     (?g (or ,(gnus-macroexpand-all
1374               '(gnus-group-short-name
1375                 (nnir-article-group (mail-header-number gnus-tmp-header))))
1376             "") ?s)
1377     (?O gnus-tmp-downloaded ?c)
1378     (?I gnus-tmp-indentation ?s)
1379     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1380     (?R gnus-tmp-replied ?c)
1381     (?\[ gnus-tmp-opening-bracket ?c)
1382     (?\] gnus-tmp-closing-bracket ?c)
1383     (?\> (make-string gnus-tmp-level ? ) ?s)
1384     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1385     (?i gnus-tmp-score ?d)
1386     (?z gnus-tmp-score-char ?c)
1387     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1388     (?U gnus-tmp-unread ?c)
1389     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1390         ?s)
1391     (?t (gnus-summary-number-of-articles-in-thread
1392          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1393         ?d)
1394     (?e (gnus-summary-number-of-articles-in-thread
1395          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1396         ?c)
1397     (?u gnus-tmp-user-defined ?s)
1398     (?P (gnus-pick-line-number) ?d)
1399     (?B gnus-tmp-thread-tree-header-string ?s)
1400     (user-date (gnus-user-date
1401                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1402   "An alist of format specifications that can appear in summary lines.
1403 These are paired with what variables they correspond with, along with
1404 the type of the variable (string, integer, character, etc).")
1405
1406 (defvar gnus-summary-dummy-line-format-alist
1407   `((?S gnus-tmp-subject ?s)
1408     (?N gnus-tmp-number ?d)
1409     (?u gnus-tmp-user-defined ?s)))
1410
1411 (defvar gnus-summary-mode-line-format-alist
1412   `((?G gnus-tmp-group-name ?s)
1413     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1414     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1415     (?A gnus-tmp-article-number ?d)
1416     (?Z gnus-tmp-unread-and-unselected ?s)
1417     (?V gnus-version ?s)
1418     (?U gnus-tmp-unread-and-unticked ?d)
1419     (?S gnus-tmp-subject ?s)
1420     (?e gnus-tmp-unselected ?d)
1421     (?u gnus-tmp-user-defined ?s)
1422     (?d (length gnus-newsgroup-dormant) ?d)
1423     (?t (length gnus-newsgroup-marked) ?d)
1424     (?h (length gnus-newsgroup-spam-marked) ?d)
1425     (?r (length gnus-newsgroup-reads) ?d)
1426     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1427     (?E gnus-newsgroup-expunged-tally ?d)
1428     (?s (gnus-current-score-file-nondirectory) ?s)))
1429
1430 ;; This is here rather than in gnus-art for compilation reasons.
1431 (defvar gnus-article-mode-line-format-alist
1432   (nconc '((?w (gnus-article-wash-status) ?s)
1433            (?m (gnus-article-mime-part-status) ?s))
1434          gnus-summary-mode-line-format-alist))
1435
1436 (defvar gnus-last-search-regexp nil
1437   "Default regexp for article search command.")
1438
1439 (defvar gnus-last-shell-command nil
1440   "Default shell command on article.")
1441
1442 (defvar gnus-newsgroup-agentized nil
1443   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1444 (defvar gnus-newsgroup-begin nil)
1445 (defvar gnus-newsgroup-end nil)
1446 (defvar gnus-newsgroup-last-rmail nil)
1447 (defvar gnus-newsgroup-last-mail nil)
1448 (defvar gnus-newsgroup-last-folder nil)
1449 (defvar gnus-newsgroup-last-file nil)
1450 (defvar gnus-newsgroup-last-directory nil)
1451 (defvar gnus-newsgroup-auto-expire nil)
1452 (defvar gnus-newsgroup-active nil)
1453 (defvar gnus-newsgroup-highest nil)
1454
1455 (defvar gnus-newsgroup-data nil)
1456 (defvar gnus-newsgroup-data-reverse nil)
1457 (defvar gnus-newsgroup-limit nil)
1458 (defvar gnus-newsgroup-limits nil)
1459 (defvar gnus-summary-use-undownloaded-faces nil)
1460
1461 (defvar gnus-newsgroup-unreads nil
1462   "Sorted list of unread articles in the current newsgroup.")
1463
1464 (defvar gnus-newsgroup-unselected nil
1465   "Sorted list of unselected unread articles in the current newsgroup.")
1466
1467 (defvar gnus-newsgroup-reads nil
1468   "Alist of read articles and article marks in the current newsgroup.")
1469
1470 (defvar gnus-newsgroup-expunged-tally nil)
1471
1472 (defvar gnus-newsgroup-marked nil
1473   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1474
1475 (defvar gnus-newsgroup-spam-marked nil
1476   "List of ranges of articles that have been marked as spam.")
1477
1478 (defvar gnus-newsgroup-killed nil
1479   "List of ranges of articles that have been through the scoring process.")
1480
1481 (defvar gnus-newsgroup-cached nil
1482   "Sorted list of articles that come from the article cache.")
1483
1484 (defvar gnus-newsgroup-saved nil
1485   "List of articles that have been saved.")
1486
1487 (defvar gnus-newsgroup-kill-headers nil)
1488
1489 (defvar gnus-newsgroup-replied nil
1490   "List of articles that have been replied to in the current newsgroup.")
1491
1492 (defvar gnus-newsgroup-forwarded nil
1493   "List of articles that have been forwarded in the current newsgroup.")
1494
1495 (defvar gnus-newsgroup-recent nil
1496   "List of articles that have are recent in the current newsgroup.")
1497
1498 (defvar gnus-newsgroup-expirable nil
1499   "Sorted list of articles in the current newsgroup that can be expired.")
1500
1501 (defvar gnus-newsgroup-processable nil
1502   "List of articles in the current newsgroup that can be processed.")
1503
1504 (defvar gnus-newsgroup-downloadable nil
1505   "Sorted list of articles in the current newsgroup that can be processed.")
1506
1507 (defvar gnus-newsgroup-unfetched nil
1508   "Sorted list of articles in the current newsgroup whose headers have
1509 not been fetched into the agent.
1510
1511 This list will always be a subset of gnus-newsgroup-undownloaded.")
1512
1513 (defvar gnus-newsgroup-undownloaded nil
1514   "List of articles in the current newsgroup that haven't been downloaded.")
1515
1516 (defvar gnus-newsgroup-unsendable nil
1517   "List of articles in the current newsgroup that won't be sent.")
1518
1519 (defvar gnus-newsgroup-bookmarks nil
1520   "List of articles in the current newsgroup that have bookmarks.")
1521
1522 (defvar gnus-newsgroup-dormant nil
1523   "Sorted list of dormant articles in the current newsgroup.")
1524
1525 (defvar gnus-newsgroup-unseen nil
1526   "List of unseen articles in the current newsgroup.")
1527
1528 (defvar gnus-newsgroup-seen nil
1529   "Range of seen articles in the current newsgroup.")
1530
1531 (defvar gnus-newsgroup-articles nil
1532   "List of articles in the current newsgroup.")
1533
1534 (defvar gnus-newsgroup-scored nil
1535   "List of scored articles in the current newsgroup.")
1536
1537 (defvar gnus-newsgroup-headers nil
1538   "List of article headers in the current newsgroup.")
1539
1540 (defvar gnus-newsgroup-threads nil)
1541
1542 (defvar gnus-newsgroup-prepared nil
1543   "Whether the current group has been prepared properly.")
1544
1545 (defvar gnus-newsgroup-ancient nil
1546   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1547
1548 (defvar gnus-newsgroup-sparse nil)
1549
1550 (defvar gnus-current-article nil)
1551 (defvar gnus-article-current nil)
1552 (defvar gnus-current-headers nil)
1553 (defvar gnus-have-all-headers nil)
1554 (defvar gnus-last-article nil)
1555 (defvar gnus-newsgroup-history nil)
1556 (defvar gnus-newsgroup-charset nil)
1557 (defvar gnus-newsgroup-ephemeral-charset nil)
1558 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1559
1560 (defvar gnus-article-before-search nil)
1561
1562 (defvar gnus-summary-local-variables
1563   '(gnus-newsgroup-name
1564
1565     ;; Marks lists
1566     gnus-newsgroup-unreads
1567     gnus-newsgroup-unselected
1568     gnus-newsgroup-marked
1569     gnus-newsgroup-spam-marked
1570     gnus-newsgroup-reads
1571     gnus-newsgroup-saved
1572     gnus-newsgroup-replied
1573     gnus-newsgroup-forwarded
1574     gnus-newsgroup-recent
1575     gnus-newsgroup-expirable
1576     gnus-newsgroup-killed
1577     gnus-newsgroup-unseen
1578     gnus-newsgroup-seen
1579     gnus-newsgroup-cached
1580     gnus-newsgroup-downloadable
1581     gnus-newsgroup-undownloaded
1582     gnus-newsgroup-unsendable
1583
1584     gnus-newsgroup-begin gnus-newsgroup-end
1585     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1586     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1587     gnus-newsgroup-last-directory
1588     gnus-newsgroup-auto-expire
1589     gnus-newsgroup-processable
1590     gnus-newsgroup-unfetched
1591     gnus-newsgroup-articles
1592     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1593     gnus-newsgroup-headers gnus-newsgroup-threads
1594     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1595     gnus-current-article gnus-current-headers gnus-have-all-headers
1596     gnus-last-article gnus-article-internal-prepare-hook
1597     (gnus-summary-article-delete-hook . global)
1598     (gnus-summary-article-move-hook . global)
1599     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1600     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1601     gnus-thread-expunge-below
1602     gnus-score-alist gnus-current-score-file
1603     (gnus-summary-expunge-below . global)
1604     (gnus-summary-mark-below . global)
1605     (gnus-orphan-score . global)
1606     gnus-newsgroup-active gnus-scores-exclude-files
1607     gnus-newsgroup-highest
1608     gnus-newsgroup-history gnus-newsgroup-ancient
1609     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1610     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1611     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1612     (gnus-newsgroup-expunged-tally . 0)
1613     gnus-cache-removable-articles
1614     gnus-newsgroup-data gnus-newsgroup-data-reverse
1615     gnus-newsgroup-limit gnus-newsgroup-limits
1616     gnus-newsgroup-charset gnus-newsgroup-display
1617     gnus-summary-use-undownloaded-faces)
1618   "Variables that are buffer-local to the summary buffers.")
1619
1620 (defvar gnus-newsgroup-variables nil
1621   "A list of variables that have separate values in different newsgroups.
1622 A list of newsgroup (summary buffer) local variables, or cons of
1623 variables and their default expressions to be evalled (when the default
1624 values are not nil), that should be made global while the summary buffer
1625 is active.
1626
1627 Note: The default expressions will be evaluated (using function `eval')
1628 before assignment to the local variable rather than just assigned to it.
1629 If the default expression is the symbol `global', that symbol will not
1630 be evaluated but the global value of the local variable will be used
1631 instead.
1632
1633 These variables can be used to set variables in the group parameters
1634 while still allowing them to affect operations done in other buffers.
1635 For example:
1636
1637 \(setq gnus-newsgroup-variables
1638      '(message-use-followup-to
1639        (gnus-visible-headers .
1640          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1641 ")
1642
1643 (eval-when-compile
1644   ;; Bind features so that require will believe that gnus-sum has
1645   ;; already been loaded (avoids infinite recursion)
1646   (let ((features (cons 'gnus-sum features)))
1647     (require 'gnus-art)))
1648
1649 ;; MIME stuff.
1650
1651 (defvar gnus-decode-encoded-word-methods
1652   '(mail-decode-encoded-word-string)
1653   "List of methods used to decode encoded words.
1654
1655 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1656 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1657 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1658 whose names match REGEXP.
1659
1660 For example:
1661 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1662  mail-decode-encoded-word-string
1663  (\"chinese\" . rfc1843-decode-string))")
1664
1665 (defvar gnus-decode-encoded-word-methods-cache nil)
1666
1667 (defun gnus-multi-decode-encoded-word-string (string)
1668   "Apply the functions from `gnus-encoded-word-methods' that match."
1669   (unless (and gnus-decode-encoded-word-methods-cache
1670                (eq gnus-newsgroup-name
1671                    (car gnus-decode-encoded-word-methods-cache)))
1672     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1673     (dolist (method gnus-decode-encoded-word-methods)
1674       (if (symbolp method)
1675           (nconc gnus-decode-encoded-word-methods-cache (list method))
1676         (if (and gnus-newsgroup-name
1677                  (string-match (car method) gnus-newsgroup-name))
1678             (nconc gnus-decode-encoded-word-methods-cache
1679                    (list (cdr method)))))))
1680   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1681     (setq string (funcall method string))))
1682
1683 ;; Subject simplification.
1684
1685 (defun gnus-simplify-whitespace (str)
1686   "Remove excessive whitespace from STR."
1687   ;; Multiple spaces.
1688   (while (string-match "[ \t][ \t]+" str)
1689     (setq str (concat (substring str 0 (match-beginning 0))
1690                         " "
1691                         (substring str (match-end 0)))))
1692   ;; Leading spaces.
1693   (when (string-match "^[ \t]+" str)
1694     (setq str (substring str (match-end 0))))
1695   ;; Trailing spaces.
1696   (when (string-match "[ \t]+$" str)
1697     (setq str (substring str 0 (match-beginning 0))))
1698   str)
1699
1700 (defun gnus-simplify-all-whitespace (str)
1701   "Remove all whitespace from STR."
1702   (while (string-match "[ \t\n]+" str)
1703     (setq str (replace-match "" nil nil str)))
1704   str)
1705
1706 (defsubst gnus-simplify-subject-re (subject)
1707   "Remove \"Re:\" from subject lines."
1708   (if (string-match message-subject-re-regexp subject)
1709       (substring subject (match-end 0))
1710     subject))
1711
1712 (defun gnus-simplify-subject (subject &optional re-only)
1713   "Remove `Re:' and words in parentheses.
1714 If RE-ONLY is non-nil, strip leading `Re:'s only."
1715   (let ((case-fold-search t))           ;Ignore case.
1716     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1717     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1718       (setq subject (substring subject (match-end 0))))
1719     ;; Remove uninteresting prefixes.
1720     (when (and (not re-only)
1721                gnus-simplify-ignored-prefixes
1722                (string-match gnus-simplify-ignored-prefixes subject))
1723       (setq subject (substring subject (match-end 0))))
1724     ;; Remove words in parentheses from end.
1725     (unless re-only
1726       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1727         (setq subject (substring subject 0 (match-beginning 0)))))
1728     ;; Return subject string.
1729     subject))
1730
1731 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1732 ;; all whitespace.
1733 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1734   (goto-char (point-min))
1735   (while (re-search-forward regexp nil t)
1736     (replace-match (or newtext ""))))
1737
1738 (defun gnus-simplify-buffer-fuzzy ()
1739   "Simplify string in the buffer fuzzily.
1740 The string in the accessible portion of the current buffer is simplified.
1741 It is assumed to be a single-line subject.
1742 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1743 matter is removed.  Additional things can be deleted by setting
1744 `gnus-simplify-subject-fuzzy-regexp'."
1745   (let ((case-fold-search t)
1746         (modified-tick))
1747     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1748
1749     (while (not (eq modified-tick (buffer-modified-tick)))
1750       (setq modified-tick (buffer-modified-tick))
1751       (cond
1752        ((listp gnus-simplify-subject-fuzzy-regexp)
1753         (mapc 'gnus-simplify-buffer-fuzzy-step
1754               gnus-simplify-subject-fuzzy-regexp))
1755        (gnus-simplify-subject-fuzzy-regexp
1756         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1757       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1758       (gnus-simplify-buffer-fuzzy-step
1759        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1760       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1761
1762     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1763     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1764     (gnus-simplify-buffer-fuzzy-step " $")
1765     (gnus-simplify-buffer-fuzzy-step "^ +")))
1766
1767 (defun gnus-simplify-subject-fuzzy (subject)
1768   "Simplify a subject string fuzzily.
1769 See `gnus-simplify-buffer-fuzzy' for details."
1770   (save-excursion
1771     (gnus-set-work-buffer)
1772     (let ((case-fold-search t))
1773       ;; Remove uninteresting prefixes.
1774       (when (and gnus-simplify-ignored-prefixes
1775                  (string-match gnus-simplify-ignored-prefixes subject))
1776         (setq subject (substring subject (match-end 0))))
1777       (insert subject)
1778       (inline (gnus-simplify-buffer-fuzzy))
1779       (buffer-string))))
1780
1781 (defsubst gnus-simplify-subject-fully (subject)
1782   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1783   (cond
1784    (gnus-simplify-subject-functions
1785     (gnus-map-function gnus-simplify-subject-functions subject))
1786    ((null gnus-summary-gather-subject-limit)
1787     (gnus-simplify-subject-re subject))
1788    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1789     (gnus-simplify-subject-fuzzy subject))
1790    ((numberp gnus-summary-gather-subject-limit)
1791     (truncate-string-to-width (gnus-simplify-subject-re subject)
1792                               gnus-summary-gather-subject-limit))
1793    (t
1794     subject)))
1795
1796 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1797   "Check whether two subjects are equal.
1798 If optional argument SIMPLE-FIRST is t, first argument is already
1799 simplified."
1800   (cond
1801    ((null simple-first)
1802     (equal (gnus-simplify-subject-fully s1)
1803            (gnus-simplify-subject-fully s2)))
1804    (t
1805     (equal s1
1806            (gnus-simplify-subject-fully s2)))))
1807
1808 (defun gnus-summary-bubble-group ()
1809   "Increase the score of the current group.
1810 This is a handy function to add to `gnus-summary-exit-hook' to
1811 increase the score of each group you read."
1812   (gnus-group-add-score gnus-newsgroup-name))
1813
1814 \f
1815 ;;;
1816 ;;; Gnus summary mode
1817 ;;;
1818
1819 (put 'gnus-summary-mode 'mode-class 'special)
1820
1821 (defvar gnus-article-commands-menu)
1822
1823 ;; Non-orthogonal keys
1824
1825 (gnus-define-keys gnus-summary-mode-map
1826   " " gnus-summary-next-page
1827   "\177" gnus-summary-prev-page
1828   [delete] gnus-summary-prev-page
1829   [backspace] gnus-summary-prev-page
1830   "\r" gnus-summary-scroll-up
1831   "\M-\r" gnus-summary-scroll-down
1832   "n" gnus-summary-next-unread-article
1833   "p" gnus-summary-prev-unread-article
1834   "N" gnus-summary-next-article
1835   "P" gnus-summary-prev-article
1836   "\M-\C-n" gnus-summary-next-same-subject
1837   "\M-\C-p" gnus-summary-prev-same-subject
1838   "\M-n" gnus-summary-next-unread-subject
1839   "\M-p" gnus-summary-prev-unread-subject
1840   "." gnus-summary-first-unread-article
1841   "," gnus-summary-best-unread-article
1842   "\M-s" gnus-summary-search-article-forward
1843   "\M-r" gnus-summary-search-article-backward
1844   "\M-S" gnus-summary-repeat-search-article-forward
1845   "\M-R" gnus-summary-repeat-search-article-backward
1846   "<" gnus-summary-beginning-of-article
1847   ">" gnus-summary-end-of-article
1848   "j" gnus-summary-goto-article
1849   "^" gnus-summary-refer-parent-article
1850   "\M-^" gnus-summary-refer-article
1851   "u" gnus-summary-tick-article-forward
1852   "!" gnus-summary-tick-article-forward
1853   "U" gnus-summary-tick-article-backward
1854   "d" gnus-summary-mark-as-read-forward
1855   "D" gnus-summary-mark-as-read-backward
1856   "E" gnus-summary-mark-as-expirable
1857   "\M-u" gnus-summary-clear-mark-forward
1858   "\M-U" gnus-summary-clear-mark-backward
1859   "k" gnus-summary-kill-same-subject-and-select
1860   "\C-k" gnus-summary-kill-same-subject
1861   "\M-\C-k" gnus-summary-kill-thread
1862   "\M-\C-l" gnus-summary-lower-thread
1863   "e" gnus-summary-edit-article
1864   "#" gnus-summary-mark-as-processable
1865   "\M-#" gnus-summary-unmark-as-processable
1866   "\M-\C-t" gnus-summary-toggle-threads
1867   "\M-\C-s" gnus-summary-show-thread
1868   "\M-\C-h" gnus-summary-hide-thread
1869   "\M-\C-f" gnus-summary-next-thread
1870   "\M-\C-b" gnus-summary-prev-thread
1871   [(meta down)] gnus-summary-next-thread
1872   [(meta up)] gnus-summary-prev-thread
1873   "\M-\C-u" gnus-summary-up-thread
1874   "\M-\C-d" gnus-summary-down-thread
1875   "&" gnus-summary-execute-command
1876   "c" gnus-summary-catchup-and-exit
1877   "\C-w" gnus-summary-mark-region-as-read
1878   "\C-t" gnus-summary-toggle-truncation
1879   "?" gnus-summary-mark-as-dormant
1880   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1881   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1882   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1883   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1884   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1885   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1886   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1887   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1888   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1889   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1890   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1891   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1892   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1893   "=" gnus-summary-expand-window
1894   "\C-x\C-s" gnus-summary-reselect-current-group
1895   "\M-g" gnus-summary-rescan-group
1896   "\C-c\C-r" gnus-summary-caesar-message
1897   "f" gnus-summary-followup
1898   "F" gnus-summary-followup-with-original
1899   "C" gnus-summary-cancel-article
1900   "r" gnus-summary-reply
1901   "R" gnus-summary-reply-with-original
1902   "\C-c\C-f" gnus-summary-mail-forward
1903   "o" gnus-summary-save-article
1904   "\C-o" gnus-summary-save-article-mail
1905   "|" gnus-summary-pipe-output
1906   "\M-k" gnus-summary-edit-local-kill
1907   "\M-K" gnus-summary-edit-global-kill
1908   ;; "V" gnus-version
1909   "\C-c\C-d" gnus-summary-describe-group
1910   "q" gnus-summary-exit
1911   "Q" gnus-summary-exit-no-update
1912   "\C-c\C-i" gnus-info-find-node
1913   gnus-mouse-2 gnus-mouse-pick-article
1914   [follow-link] mouse-face
1915   "m" gnus-summary-mail-other-window
1916   "a" gnus-summary-post-news
1917   "x" gnus-summary-limit-to-unread
1918   "s" gnus-summary-isearch-article
1919   [tab] gnus-summary-widget-forward
1920   "t" gnus-summary-toggle-header
1921   "g" gnus-summary-show-article
1922   "l" gnus-summary-goto-last-article
1923   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1924   "\C-d" gnus-summary-enter-digest-group
1925   "\M-\C-d" gnus-summary-read-document
1926   "\M-\C-e" gnus-summary-edit-parameters
1927   "\M-\C-a" gnus-summary-customize-parameters
1928   "\C-c\C-b" gnus-bug
1929   "*" gnus-cache-enter-article
1930   "\M-*" gnus-cache-remove-article
1931   "\M-&" gnus-summary-universal-argument
1932   "\C-l" gnus-recenter
1933   "I" gnus-summary-increase-score
1934   "L" gnus-summary-lower-score
1935   "\M-i" gnus-symbolic-argument
1936   "h" gnus-summary-select-article-buffer
1937
1938   "b" gnus-article-view-part
1939   "\M-t" gnus-summary-toggle-display-buttonized
1940
1941   "V" gnus-summary-score-map
1942   "X" gnus-uu-extract-map
1943   "S" gnus-summary-send-map)
1944
1945 ;; Sort of orthogonal keymap
1946 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1947   "t" gnus-summary-tick-article-forward
1948   "!" gnus-summary-tick-article-forward
1949   "d" gnus-summary-mark-as-read-forward
1950   "r" gnus-summary-mark-as-read-forward
1951   "c" gnus-summary-clear-mark-forward
1952   " " gnus-summary-clear-mark-forward
1953   "e" gnus-summary-mark-as-expirable
1954   "x" gnus-summary-mark-as-expirable
1955   "?" gnus-summary-mark-as-dormant
1956   "b" gnus-summary-set-bookmark
1957   "B" gnus-summary-remove-bookmark
1958   "#" gnus-summary-mark-as-processable
1959   "\M-#" gnus-summary-unmark-as-processable
1960   "S" gnus-summary-limit-include-expunged
1961   "C" gnus-summary-catchup
1962   "H" gnus-summary-catchup-to-here
1963   "h" gnus-summary-catchup-from-here
1964   "\C-c" gnus-summary-catchup-all
1965   "k" gnus-summary-kill-same-subject-and-select
1966   "K" gnus-summary-kill-same-subject
1967   "P" gnus-uu-mark-map)
1968
1969 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1970   "c" gnus-summary-clear-above
1971   "u" gnus-summary-tick-above
1972   "m" gnus-summary-mark-above
1973   "k" gnus-summary-kill-below)
1974
1975 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1976   "/" gnus-summary-limit-to-subject
1977   "n" gnus-summary-limit-to-articles
1978   "b" gnus-summary-limit-to-bodies
1979   "h" gnus-summary-limit-to-headers
1980   "w" gnus-summary-pop-limit
1981   "s" gnus-summary-limit-to-subject
1982   "a" gnus-summary-limit-to-author
1983   "u" gnus-summary-limit-to-unread
1984   "m" gnus-summary-limit-to-marks
1985   "M" gnus-summary-limit-exclude-marks
1986   "v" gnus-summary-limit-to-score
1987   "*" gnus-summary-limit-include-cached
1988   "D" gnus-summary-limit-include-dormant
1989   "T" gnus-summary-limit-include-thread
1990   "d" gnus-summary-limit-exclude-dormant
1991   "t" gnus-summary-limit-to-age
1992   "." gnus-summary-limit-to-unseen
1993   "x" gnus-summary-limit-to-extra
1994   "p" gnus-summary-limit-to-display-predicate
1995   "E" gnus-summary-limit-include-expunged
1996   "c" gnus-summary-limit-exclude-childless-dormant
1997   "C" gnus-summary-limit-mark-excluded-as-read
1998   "o" gnus-summary-insert-old-articles
1999   "N" gnus-summary-insert-new-articles
2000   "S" gnus-summary-limit-to-singletons
2001   "r" gnus-summary-limit-to-replied
2002   "R" gnus-summary-limit-to-recipient
2003   "A" gnus-summary-limit-to-address)
2004
2005 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2006   "n" gnus-summary-next-unread-article
2007   "p" gnus-summary-prev-unread-article
2008   "N" gnus-summary-next-article
2009   "P" gnus-summary-prev-article
2010   "\C-n" gnus-summary-next-same-subject
2011   "\C-p" gnus-summary-prev-same-subject
2012   "\M-n" gnus-summary-next-unread-subject
2013   "\M-p" gnus-summary-prev-unread-subject
2014   "f" gnus-summary-first-unread-article
2015   "b" gnus-summary-best-unread-article
2016   "j" gnus-summary-goto-article
2017   "g" gnus-summary-goto-subject
2018   "l" gnus-summary-goto-last-article
2019   "o" gnus-summary-pop-article)
2020
2021 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2022   "k" gnus-summary-kill-thread
2023   "E" gnus-summary-expire-thread
2024   "l" gnus-summary-lower-thread
2025   "i" gnus-summary-raise-thread
2026   "T" gnus-summary-toggle-threads
2027   "t" gnus-summary-rethread-current
2028   "^" gnus-summary-reparent-thread
2029   "\M-^" gnus-summary-reparent-children
2030   "s" gnus-summary-show-thread
2031   "S" gnus-summary-show-all-threads
2032   "h" gnus-summary-hide-thread
2033   "H" gnus-summary-hide-all-threads
2034   "n" gnus-summary-next-thread
2035   "p" gnus-summary-prev-thread
2036   "u" gnus-summary-up-thread
2037   "o" gnus-summary-top-thread
2038   "d" gnus-summary-down-thread
2039   "#" gnus-uu-mark-thread
2040   "\M-#" gnus-uu-unmark-thread)
2041
2042 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2043   "g" gnus-summary-prepare
2044   "c" gnus-summary-insert-cached-articles
2045   "d" gnus-summary-insert-dormant-articles
2046   "t" gnus-summary-insert-ticked-articles)
2047
2048 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2049   "c" gnus-summary-catchup-and-exit
2050   "C" gnus-summary-catchup-all-and-exit
2051   "E" gnus-summary-exit-no-update
2052   "Q" gnus-summary-exit
2053   "Z" gnus-summary-exit
2054   "n" gnus-summary-catchup-and-goto-next-group
2055   "p" gnus-summary-catchup-and-goto-prev-group
2056   "R" gnus-summary-reselect-current-group
2057   "G" gnus-summary-rescan-group
2058   "N" gnus-summary-next-group
2059   "s" gnus-summary-save-newsrc
2060   "P" gnus-summary-prev-group)
2061
2062 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2063   " " gnus-summary-next-page
2064   "n" gnus-summary-next-page
2065   "\177" gnus-summary-prev-page
2066   [delete] gnus-summary-prev-page
2067   "p" gnus-summary-prev-page
2068   "\r" gnus-summary-scroll-up
2069   "\M-\r" gnus-summary-scroll-down
2070   "<" gnus-summary-beginning-of-article
2071   ">" gnus-summary-end-of-article
2072   "b" gnus-summary-beginning-of-article
2073   "e" gnus-summary-end-of-article
2074   "^" gnus-summary-refer-parent-article
2075   "r" gnus-summary-refer-parent-article
2076   "C" gnus-summary-show-complete-article
2077   "D" gnus-summary-enter-digest-group
2078   "R" gnus-summary-refer-references
2079   "T" gnus-summary-refer-thread
2080   "W" gnus-warp-to-article
2081   "g" gnus-summary-show-article
2082   "s" gnus-summary-isearch-article
2083   [tab] gnus-summary-widget-forward
2084   "P" gnus-summary-print-article
2085   "S" gnus-sticky-article
2086   "M" gnus-mailing-list-insinuate
2087   "t" gnus-article-babel)
2088
2089 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2090   "b" gnus-article-add-buttons
2091   "B" gnus-article-add-buttons-to-head
2092   "o" gnus-article-treat-overstrike
2093   "e" gnus-article-emphasize
2094   "w" gnus-article-fill-cited-article
2095   "Q" gnus-article-fill-long-lines
2096   "L" gnus-article-toggle-truncate-lines
2097   "C" gnus-article-capitalize-sentences
2098   "c" gnus-article-remove-cr
2099   "q" gnus-article-de-quoted-unreadable
2100   "6" gnus-article-de-base64-unreadable
2101   "Z" gnus-article-decode-HZ
2102   "A" gnus-article-treat-ansi-sequences
2103   "h" gnus-article-wash-html
2104   "u" gnus-article-unsplit-urls
2105   "s" gnus-summary-force-verify-and-decrypt
2106   "f" gnus-article-display-x-face
2107   "l" gnus-summary-stop-page-breaking
2108   "r" gnus-summary-caesar-message
2109   "m" gnus-summary-morse-message
2110   "t" gnus-summary-toggle-header
2111   "g" gnus-treat-smiley
2112   "v" gnus-summary-verbose-headers
2113   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2114   "p" gnus-article-verify-x-pgp-sig
2115   "d" gnus-article-treat-dumbquotes
2116   "U" gnus-article-treat-non-ascii
2117   "i" gnus-summary-idna-message)
2118
2119 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2120   ;; mnemonic: deuglif*Y*
2121   "u" gnus-article-outlook-unwrap-lines
2122   "a" gnus-article-outlook-repair-attribution
2123   "c" gnus-article-outlook-rearrange-citation
2124   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2125
2126 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2127   "a" gnus-article-hide
2128   "h" gnus-article-hide-headers
2129   "b" gnus-article-hide-boring-headers
2130   "s" gnus-article-hide-signature
2131   "c" gnus-article-hide-citation
2132   "C" gnus-article-hide-citation-in-followups
2133   "l" gnus-article-hide-list-identifiers
2134   "B" gnus-article-strip-banner
2135   "P" gnus-article-hide-pem
2136   "\C-c" gnus-article-hide-citation-maybe)
2137
2138 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2139   "a" gnus-article-highlight
2140   "h" gnus-article-highlight-headers
2141   "c" gnus-article-highlight-citation
2142   "s" gnus-article-highlight-signature)
2143
2144 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2145   "f" gnus-article-treat-fold-headers
2146   "u" gnus-article-treat-unfold-headers
2147   "n" gnus-article-treat-fold-newsgroups)
2148
2149 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2150   "x" gnus-article-display-x-face
2151   "d" gnus-article-display-face
2152   "s" gnus-treat-smiley
2153   "D" gnus-article-remove-images
2154   "W" gnus-article-show-images
2155   "f" gnus-treat-from-picon
2156   "m" gnus-treat-mail-picon
2157   "n" gnus-treat-newsgroups-picon
2158   "g" gnus-treat-from-gravatar
2159   "h" gnus-treat-mail-gravatar)
2160
2161 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2162   "w" gnus-article-decode-mime-words
2163   "c" gnus-article-decode-charset
2164   "v" gnus-mime-view-all-parts
2165   "b" gnus-article-view-part)
2166
2167 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2168   "z" gnus-article-date-ut
2169   "u" gnus-article-date-ut
2170   "l" gnus-article-date-local
2171   "p" gnus-article-date-english
2172   "e" gnus-article-date-lapsed
2173   "o" gnus-article-date-original
2174   "i" gnus-article-date-iso8601
2175   "s" gnus-article-date-user)
2176
2177 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2178   "t" gnus-article-remove-trailing-blank-lines
2179   "l" gnus-article-strip-leading-blank-lines
2180   "m" gnus-article-strip-multiple-blank-lines
2181   "a" gnus-article-strip-blank-lines
2182   "A" gnus-article-strip-all-blank-lines
2183   "s" gnus-article-strip-leading-space
2184   "e" gnus-article-strip-trailing-space
2185   "w" gnus-article-remove-leading-whitespace)
2186
2187 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2188   "v" gnus-version
2189   "d" gnus-summary-describe-group
2190   "h" gnus-summary-describe-briefly
2191   "i" gnus-info-find-node)
2192
2193 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2194   "e" gnus-summary-expire-articles
2195   "\M-\C-e" gnus-summary-expire-articles-now
2196   "\177" gnus-summary-delete-article
2197   [delete] gnus-summary-delete-article
2198   [backspace] gnus-summary-delete-article
2199   "m" gnus-summary-move-article
2200   "r" gnus-summary-respool-article
2201   "w" gnus-summary-edit-article
2202   "c" gnus-summary-copy-article
2203   "B" gnus-summary-crosspost-article
2204   "q" gnus-summary-respool-query
2205   "t" gnus-summary-respool-trace
2206   "i" gnus-summary-import-article
2207   "I" gnus-summary-create-article
2208   "p" gnus-summary-article-posted-p)
2209
2210 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2211   "o" gnus-summary-save-article
2212   "m" gnus-summary-save-article-mail
2213   "F" gnus-summary-write-article-file
2214   "r" gnus-summary-save-article-rmail
2215   "f" gnus-summary-save-article-file
2216   "b" gnus-summary-save-article-body-file
2217   "B" gnus-summary-write-article-body-file
2218   "h" gnus-summary-save-article-folder
2219   "v" gnus-summary-save-article-vm
2220   "p" gnus-summary-pipe-output
2221   "P" gnus-summary-muttprint)
2222
2223 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2224   "b" gnus-summary-display-buttonized
2225   "m" gnus-summary-repair-multipart
2226   "v" gnus-article-view-part
2227   "o" gnus-article-save-part
2228   "O" gnus-article-save-part-and-strip
2229   "r" gnus-article-replace-part
2230   "d" gnus-article-delete-part
2231   "t" gnus-article-view-part-as-type
2232   "j" gnus-article-jump-to-part
2233   "c" gnus-article-copy-part
2234   "C" gnus-article-view-part-as-charset
2235   "e" gnus-article-view-part-externally
2236   "H" gnus-article-browse-html-article
2237   "E" gnus-article-encrypt-body
2238   "i" gnus-article-inline-part
2239   "|" gnus-article-pipe-part)
2240
2241 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2242   "p" gnus-summary-mark-as-processable
2243   "u" gnus-summary-unmark-as-processable
2244   "U" gnus-summary-unmark-all-processable
2245   "v" gnus-uu-mark-over
2246   "s" gnus-uu-mark-series
2247   "r" gnus-uu-mark-region
2248   "g" gnus-uu-unmark-region
2249   "R" gnus-uu-mark-by-regexp
2250   "G" gnus-uu-unmark-by-regexp
2251   "t" gnus-uu-mark-thread
2252   "T" gnus-uu-unmark-thread
2253   "a" gnus-uu-mark-all
2254   "b" gnus-uu-mark-buffer
2255   "S" gnus-uu-mark-sparse
2256   "k" gnus-summary-kill-process-mark
2257   "y" gnus-summary-yank-process-mark
2258   "w" gnus-summary-save-process-mark
2259   "i" gnus-uu-invert-processable)
2260
2261 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2262   ;;"x" gnus-uu-extract-any
2263   "m" gnus-summary-save-parts
2264   "u" gnus-uu-decode-uu
2265   "U" gnus-uu-decode-uu-and-save
2266   "s" gnus-uu-decode-unshar
2267   "S" gnus-uu-decode-unshar-and-save
2268   "o" gnus-uu-decode-save
2269   "O" gnus-uu-decode-save
2270   "b" gnus-uu-decode-binhex
2271   "B" gnus-uu-decode-binhex
2272   "Y" gnus-uu-decode-yenc
2273   "p" gnus-uu-decode-postscript
2274   "P" gnus-uu-decode-postscript-and-save)
2275
2276 (gnus-define-keys
2277     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2278   "u" gnus-uu-decode-uu-view
2279   "U" gnus-uu-decode-uu-and-save-view
2280   "s" gnus-uu-decode-unshar-view
2281   "S" gnus-uu-decode-unshar-and-save-view
2282   "o" gnus-uu-decode-save-view
2283   "O" gnus-uu-decode-save-view
2284   "b" gnus-uu-decode-binhex-view
2285   "B" gnus-uu-decode-binhex-view
2286   "p" gnus-uu-decode-postscript-view
2287   "P" gnus-uu-decode-postscript-and-save-view)
2288
2289 (defvar gnus-article-post-menu nil)
2290
2291 (defconst gnus-summary-menu-maxlen 20)
2292
2293 (defun gnus-summary-menu-split (menu)
2294   ;; If we have lots of elements, divide them into groups of 20
2295   ;; and make a pane (or submenu) for each one.
2296   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2297       (let ((menu menu) sublists next
2298             (i 1))
2299         (while menu
2300           ;; Pull off the next gnus-summary-menu-maxlen elements
2301           ;; and make them the next element of sublist.
2302           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2303           (if next
2304               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2305                       nil))
2306           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2307                                              (aref (car (last menu)) 0)) menu)
2308                                sublists))
2309           (setq i (1+ i))
2310           (setq menu next))
2311         (nreverse sublists))
2312     ;; Few elements--put them all in one pane.
2313     menu))
2314
2315 (defun gnus-summary-make-menu-bar ()
2316   (gnus-turn-off-edit-menu 'summary)
2317
2318   (unless (boundp 'gnus-summary-misc-menu)
2319
2320     (easy-menu-define
2321       gnus-summary-kill-menu gnus-summary-mode-map ""
2322       (cons
2323        "Score"
2324        (nconc
2325         (list
2326          ["Customize" gnus-score-customize t])
2327         (gnus-make-score-map 'increase)
2328         (gnus-make-score-map 'lower)
2329         '(("Mark"
2330            ["Kill below" gnus-summary-kill-below t]
2331            ["Mark above" gnus-summary-mark-above t]
2332            ["Tick above" gnus-summary-tick-above t]
2333            ["Clear above" gnus-summary-clear-above t])
2334           ["Current score" gnus-summary-current-score t]
2335           ["Set score" gnus-summary-set-score t]
2336           ["Switch current score file..." gnus-score-change-score-file t]
2337           ["Set mark below..." gnus-score-set-mark-below t]
2338           ["Set expunge below..." gnus-score-set-expunge-below t]
2339           ["Edit current score file" gnus-score-edit-current-scores t]
2340           ["Edit score file..." gnus-score-edit-file t]
2341           ["Trace score" gnus-score-find-trace t]
2342           ["Find words" gnus-score-find-favourite-words t]
2343           ["Rescore buffer" gnus-summary-rescore t]
2344           ["Increase score..." gnus-summary-increase-score t]
2345           ["Lower score..." gnus-summary-lower-score t]))))
2346
2347     ;; Define both the Article menu in the summary buffer and the
2348     ;; equivalent Commands menu in the article buffer here for
2349     ;; consistency.
2350     (let ((innards
2351            `(("Hide"
2352               ["All" gnus-article-hide t]
2353               ["Headers" gnus-article-hide-headers t]
2354               ["Signature" gnus-article-hide-signature t]
2355               ["Citation" gnus-article-hide-citation t]
2356               ["List identifiers" gnus-article-hide-list-identifiers t]
2357               ["Banner" gnus-article-strip-banner t]
2358               ["Boring headers" gnus-article-hide-boring-headers t])
2359              ("Highlight"
2360               ["All" gnus-article-highlight t]
2361               ["Headers" gnus-article-highlight-headers t]
2362               ["Signature" gnus-article-highlight-signature t]
2363               ["Citation" gnus-article-highlight-citation t])
2364              ("MIME"
2365               ["Words" gnus-article-decode-mime-words t]
2366               ["Charset" gnus-article-decode-charset t]
2367               ["QP" gnus-article-de-quoted-unreadable t]
2368               ["Base64" gnus-article-de-base64-unreadable t]
2369               ["View MIME buttons" gnus-summary-display-buttonized t]
2370               ["View all" gnus-mime-view-all-parts t]
2371               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2372               ["Encrypt body" gnus-article-encrypt-body
2373                :active (not (gnus-group-read-only-p))
2374                ,@(if (featurep 'xemacs) nil
2375                    '(:help "Encrypt the message body on disk"))]
2376               ["Extract all parts..." gnus-summary-save-parts t]
2377               ("Multipart"
2378                ["Repair multipart" gnus-summary-repair-multipart t]
2379                ["Pipe part..." gnus-article-pipe-part t]
2380                ["Inline part" gnus-article-inline-part t]
2381                ["View part as type..." gnus-article-view-part-as-type t]
2382                ["Encrypt body" gnus-article-encrypt-body
2383                 :active (not (gnus-group-read-only-p))
2384                ,@(if (featurep 'xemacs) nil
2385                    '(:help "Encrypt the message body on disk"))]
2386                ["View part externally" gnus-article-view-part-externally t]
2387                ["View HTML parts in browser" gnus-article-browse-html-article t]
2388                ["View part with charset..." gnus-article-view-part-as-charset t]
2389                ["Copy part" gnus-article-copy-part t]
2390                ["Save part..." gnus-article-save-part t]
2391                ["View part" gnus-article-view-part t]))
2392              ("Date"
2393               ["Local" gnus-article-date-local t]
2394               ["ISO8601" gnus-article-date-iso8601 t]
2395               ["UT" gnus-article-date-ut t]
2396               ["Original" gnus-article-date-original t]
2397               ["Lapsed" gnus-article-date-lapsed t]
2398               ["User-defined" gnus-article-date-user t])
2399              ("Display"
2400               ["Remove images" gnus-article-remove-images t]
2401               ["Toggle smiley" gnus-treat-smiley t]
2402               ["Show X-Face" gnus-article-display-x-face t]
2403               ["Show picons in From" gnus-treat-from-picon t]
2404               ["Show picons in mail headers" gnus-treat-mail-picon t]
2405               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2406               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2407               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2408               ("View as different encoding"
2409                ,@(gnus-summary-menu-split
2410                   (mapcar
2411                    (lambda (cs)
2412                      ;; Since easymenu under Emacs doesn't allow
2413                      ;; lambda forms for menu commands, we should
2414                      ;; provide intern'ed function symbols.
2415                      (let ((command (intern (format "\
2416 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2417                        (fset command
2418                              `(lambda ()
2419                                 (interactive)
2420                                 (let ((gnus-summary-show-article-charset-alist
2421                                        '((1 . ,cs))))
2422                                   (gnus-summary-show-article 1))))
2423                        `[,(symbol-name cs) ,command t]))
2424                    (sort (if (fboundp 'coding-system-list)
2425                              (coding-system-list)
2426                            (mapcar 'car mm-mime-mule-charset-alist))
2427                          'string<)))))
2428              ("Washing"
2429               ("Remove Blanks"
2430                ["Leading" gnus-article-strip-leading-blank-lines t]
2431                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2432                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2433                ["All of the above" gnus-article-strip-blank-lines t]
2434                ["All" gnus-article-strip-all-blank-lines t]
2435                ["Leading space" gnus-article-strip-leading-space t]
2436                ["Trailing space" gnus-article-strip-trailing-space t]
2437                ["Leading space in headers"
2438                 gnus-article-remove-leading-whitespace t])
2439               ["Overstrike" gnus-article-treat-overstrike t]
2440               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2441               ["Non-ASCII" gnus-article-treat-non-ascii t]
2442               ["Emphasis" gnus-article-emphasize t]
2443               ["Word wrap" gnus-article-fill-cited-article t]
2444               ["Fill long lines" gnus-article-fill-long-lines t]
2445               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2446               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2447               ["Remove CR" gnus-article-remove-cr t]
2448               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2449               ["Base64" gnus-article-de-base64-unreadable t]
2450               ["Rot 13" gnus-summary-caesar-message
2451                ,@(if (featurep 'xemacs) '(t)
2452                    '(:help "\"Caesar rotate\" article by 13"))]
2453               ["De-IDNA" gnus-summary-idna-message t]
2454               ["Morse decode" gnus-summary-morse-message t]
2455               ["Unix pipe..." gnus-summary-pipe-message t]
2456               ["Add buttons" gnus-article-add-buttons t]
2457               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2458               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2459               ["Verbose header" gnus-summary-verbose-headers t]
2460               ["Toggle header" gnus-summary-toggle-header t]
2461               ["Unfold headers" gnus-article-treat-unfold-headers t]
2462               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2463               ["Html" gnus-article-wash-html t]
2464               ["Unsplit URLs" gnus-article-unsplit-urls t]
2465               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2466               ["Decode HZ" gnus-article-decode-HZ t]
2467               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2468               ("(Outlook) Deuglify"
2469                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2470                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2471                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2472                ["Full (Outlook) deuglify"
2473                 gnus-article-outlook-deuglify-article t])
2474               )
2475              ("Output"
2476               ["Save in default format..." gnus-summary-save-article
2477                ,@(if (featurep 'xemacs) '(t)
2478                    '(:help "Save article using default method"))]
2479               ["Save in file..." gnus-summary-save-article-file
2480                ,@(if (featurep 'xemacs) '(t)
2481                    '(:help "Save article in file"))]
2482               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2483               ["Save in MH folder..." gnus-summary-save-article-folder t]
2484               ["Save in VM folder..." gnus-summary-save-article-vm t]
2485               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2486               ["Save body in file..." gnus-summary-save-article-body-file t]
2487               ["Pipe through a filter..." gnus-summary-pipe-output t]
2488               ["Print with Muttprint..." gnus-summary-muttprint t]
2489               ["Print" gnus-summary-print-article
2490                ,@(if (featurep 'xemacs) '(t)
2491                    '(:help "Generate and print a PostScript image"))])
2492              ("Copy, move,... (Backend)"
2493               ,@(if (featurep 'xemacs) nil
2494                   '(:help "Copying, moving, expiring articles..."))
2495               ["Respool article..." gnus-summary-respool-article t]
2496               ["Move article..." gnus-summary-move-article
2497                (gnus-check-backend-function
2498                 'request-move-article gnus-newsgroup-name)]
2499               ["Copy article..." gnus-summary-copy-article t]
2500               ["Crosspost article..." gnus-summary-crosspost-article
2501                (gnus-check-backend-function
2502                 'request-replace-article gnus-newsgroup-name)]
2503               ["Import file..." gnus-summary-import-article
2504                (gnus-check-backend-function
2505                 'request-accept-article gnus-newsgroup-name)]
2506               ["Create article..." gnus-summary-create-article
2507                (gnus-check-backend-function
2508                 'request-accept-article gnus-newsgroup-name)]
2509               ["Check if posted" gnus-summary-article-posted-p t]
2510               ["Edit article" gnus-summary-edit-article
2511                (not (gnus-group-read-only-p))]
2512               ["Delete article" gnus-summary-delete-article
2513                (gnus-check-backend-function
2514                 'request-expire-articles gnus-newsgroup-name)]
2515               ["Query respool" gnus-summary-respool-query t]
2516               ["Trace respool" gnus-summary-respool-trace t]
2517               ["Delete expirable articles" gnus-summary-expire-articles-now
2518                (gnus-check-backend-function
2519                 'request-expire-articles gnus-newsgroup-name)])
2520              ("Extract"
2521               ["Uudecode" gnus-uu-decode-uu
2522                ,@(if (featurep 'xemacs) '(t)
2523                    '(:help "Decode uuencoded article(s)"))]
2524               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2525               ["Unshar" gnus-uu-decode-unshar t]
2526               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2527               ["Save" gnus-uu-decode-save t]
2528               ["Binhex" gnus-uu-decode-binhex t]
2529               ["Postscript" gnus-uu-decode-postscript t]
2530               ["All MIME parts" gnus-summary-save-parts t])
2531              ("Cache"
2532               ["Enter article" gnus-cache-enter-article t]
2533               ["Remove article" gnus-cache-remove-article t])
2534              ["Translate" gnus-article-babel t]
2535              ["Select article buffer" gnus-summary-select-article-buffer t]
2536              ["Make article buffer sticky" gnus-sticky-article t]
2537              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2538              ["Isearch article..." gnus-summary-isearch-article t]
2539              ["Beginning of the article" gnus-summary-beginning-of-article t]
2540              ["End of the article" gnus-summary-end-of-article t]
2541              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2542              ["Fetch referenced articles" gnus-summary-refer-references t]
2543              ["Fetch current thread" gnus-summary-refer-thread t]
2544              ["Fetch article with id..." gnus-summary-refer-article t]
2545              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2546              ["Redisplay" gnus-summary-show-article t]
2547              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2548       (easy-menu-define
2549         gnus-summary-article-menu gnus-summary-mode-map ""
2550         (cons "Article" innards))
2551
2552       (if (not (keymapp gnus-summary-article-menu))
2553           (easy-menu-define
2554             gnus-article-commands-menu gnus-article-mode-map ""
2555             (cons "Commands" innards))
2556         ;; in Emacs, don't share menu.
2557         (setq gnus-article-commands-menu
2558               (copy-keymap gnus-summary-article-menu))
2559         (define-key gnus-article-mode-map [menu-bar commands]
2560           (cons "Commands" gnus-article-commands-menu))))
2561
2562     (easy-menu-define
2563       gnus-summary-thread-menu gnus-summary-mode-map ""
2564       '("Threads"
2565         ["Find all messages in thread" gnus-summary-refer-thread t]
2566         ["Toggle threading" gnus-summary-toggle-threads t]
2567         ["Hide threads" gnus-summary-hide-all-threads t]
2568         ["Show threads" gnus-summary-show-all-threads t]
2569         ["Hide thread" gnus-summary-hide-thread t]
2570         ["Show thread" gnus-summary-show-thread t]
2571         ["Go to next thread" gnus-summary-next-thread t]
2572         ["Go to previous thread" gnus-summary-prev-thread t]
2573         ["Go down thread" gnus-summary-down-thread t]
2574         ["Go up thread" gnus-summary-up-thread t]
2575         ["Top of thread" gnus-summary-top-thread t]
2576         ["Mark thread as read" gnus-summary-kill-thread t]
2577         ["Mark thread as expired" gnus-summary-expire-thread t]
2578         ["Lower thread score" gnus-summary-lower-thread t]
2579         ["Raise thread score" gnus-summary-raise-thread t]
2580         ["Rethread current" gnus-summary-rethread-current t]))
2581
2582     (easy-menu-define
2583       gnus-summary-post-menu gnus-summary-mode-map ""
2584       `("Post"
2585         ["Send a message (mail or news)" gnus-summary-post-news
2586          ,@(if (featurep 'xemacs) '(t)
2587              '(:help "Compose a new message (mail or news)"))]
2588         ["Followup" gnus-summary-followup
2589          ,@(if (featurep 'xemacs) '(t)
2590              '(:help "Post followup to this article"))]
2591         ["Followup and yank" gnus-summary-followup-with-original
2592          ,@(if (featurep 'xemacs) '(t)
2593              '(:help "Post followup to this article, quoting its contents"))]
2594         ["Supersede article" gnus-summary-supersede-article t]
2595         ["Cancel article" gnus-summary-cancel-article
2596          ,@(if (featurep 'xemacs) '(t)
2597              '(:help "Cancel an article you posted"))]
2598         ["Reply" gnus-summary-reply t]
2599         ["Reply and yank" gnus-summary-reply-with-original t]
2600         ["Wide reply" gnus-summary-wide-reply t]
2601         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2602          ,@(if (featurep 'xemacs) '(t)
2603              '(:help "Mail a reply, quoting this article"))]
2604         ["Very wide reply" gnus-summary-very-wide-reply t]
2605         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2606          ,@(if (featurep 'xemacs) '(t)
2607              '(:help "Mail a very wide reply, quoting this article"))]
2608         ["Mail forward" gnus-summary-mail-forward t]
2609         ["Post forward" gnus-summary-post-forward t]
2610         ["Digest and mail" gnus-uu-digest-mail-forward t]
2611         ["Digest and post" gnus-uu-digest-post-forward t]
2612         ["Resend message" gnus-summary-resend-message t]
2613         ["Resend message edit" gnus-summary-resend-message-edit t]
2614         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2615         ["Send a mail" gnus-summary-mail-other-window t]
2616         ["Create a local message" gnus-summary-news-other-window t]
2617         ["Uuencode and post" gnus-uu-post-news
2618          ,@(if (featurep 'xemacs) '(t)
2619              '(:help "Post a uuencoded article"))]
2620         ["Followup via news" gnus-summary-followup-to-mail t]
2621         ["Followup via news and yank"
2622          gnus-summary-followup-to-mail-with-original t]
2623         ["Strip signature on reply"
2624          (lambda ()
2625            (interactive)
2626            (if (not (memq message-cite-function
2627                           '(message-cite-original-without-signature
2628                             message-cite-original)))
2629                ;; Stupid workaround for XEmacs not honoring :visible.
2630                (message "Can't toggle this value of `message-cite-function'")
2631              (setq message-cite-function
2632                    (if (eq message-cite-function
2633                            'message-cite-original-without-signature)
2634                        'message-cite-original
2635                      'message-cite-original-without-signature))))
2636          ;; XEmacs barfs on :visible.
2637          ,@(if (featurep 'xemacs) nil
2638              '(:visible (memq message-cite-function
2639                               '(message-cite-original-without-signature
2640                                 message-cite-original))))
2641          :style toggle
2642          :selected (eq message-cite-function
2643                        'message-cite-original-without-signature)
2644          ,@(if (featurep 'xemacs) nil
2645              '(:help "Strip signature from cited article when replying."))]
2646         ;;("Draft"
2647         ;;["Send" gnus-summary-send-draft t]
2648         ;;["Send bounced" gnus-resend-bounced-mail t])
2649         ))
2650
2651     (cond
2652      ((not (keymapp gnus-summary-post-menu))
2653       (setq gnus-article-post-menu gnus-summary-post-menu))
2654      ((not gnus-article-post-menu)
2655       ;; Don't share post menu.
2656       (setq gnus-article-post-menu
2657             (copy-keymap gnus-summary-post-menu))))
2658     (define-key gnus-article-mode-map [menu-bar post]
2659       (cons "Post" gnus-article-post-menu))
2660
2661     (easy-menu-define
2662       gnus-summary-misc-menu gnus-summary-mode-map ""
2663       `("Gnus"
2664         ("Mark Read"
2665          ["Mark as read" gnus-summary-mark-as-read-forward t]
2666          ["Mark same subject and select"
2667           gnus-summary-kill-same-subject-and-select t]
2668          ["Mark same subject" gnus-summary-kill-same-subject t]
2669          ["Catchup" gnus-summary-catchup
2670           ,@(if (featurep 'xemacs) '(t)
2671               '(:help "Mark unread articles in this group as read"))]
2672          ["Catchup all" gnus-summary-catchup-all t]
2673          ["Catchup to here" gnus-summary-catchup-to-here t]
2674          ["Catchup from here" gnus-summary-catchup-from-here t]
2675          ["Catchup region" gnus-summary-mark-region-as-read
2676           (gnus-mark-active-p)]
2677          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2678         ("Mark Various"
2679          ["Tick" gnus-summary-tick-article-forward t]
2680          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2681          ["Remove marks" gnus-summary-clear-mark-forward t]
2682          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2683          ["Set bookmark" gnus-summary-set-bookmark t]
2684          ["Remove bookmark" gnus-summary-remove-bookmark t])
2685         ("Limit to"
2686          ["Marks..." gnus-summary-limit-to-marks t]
2687          ["Subject..." gnus-summary-limit-to-subject t]
2688          ["Author..." gnus-summary-limit-to-author t]
2689          ["Recipient..." gnus-summary-limit-to-recipient t]
2690          ["Address..." gnus-summary-limit-to-address t]
2691          ["Age..." gnus-summary-limit-to-age t]
2692          ["Extra..." gnus-summary-limit-to-extra t]
2693          ["Score..." gnus-summary-limit-to-score t]
2694          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2695          ["Unread" gnus-summary-limit-to-unread t]
2696          ["Unseen" gnus-summary-limit-to-unseen t]
2697          ["Singletons" gnus-summary-limit-to-singletons t]
2698          ["Replied" gnus-summary-limit-to-replied t]
2699          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2700          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2701          ["Pop limit" gnus-summary-pop-limit t]
2702          ["Show dormant" gnus-summary-limit-include-dormant t]
2703          ["Hide childless dormant"
2704           gnus-summary-limit-exclude-childless-dormant t]
2705          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2706          ["Hide marked" gnus-summary-limit-exclude-marks t]
2707          ["Show expunged" gnus-summary-limit-include-expunged t])
2708         ("Process Mark"
2709          ["Set mark" gnus-summary-mark-as-processable t]
2710          ["Remove mark" gnus-summary-unmark-as-processable t]
2711          ["Remove all marks" gnus-summary-unmark-all-processable t]
2712          ["Invert marks" gnus-uu-invert-processable t]
2713          ["Mark above" gnus-uu-mark-over t]
2714          ["Mark series" gnus-uu-mark-series t]
2715          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2716          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2717          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2718          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2719          ["Mark all" gnus-uu-mark-all t]
2720          ["Mark buffer" gnus-uu-mark-buffer t]
2721          ["Mark sparse" gnus-uu-mark-sparse t]
2722          ["Mark thread" gnus-uu-mark-thread t]
2723          ["Unmark thread" gnus-uu-unmark-thread t]
2724          ("Process Mark Sets"
2725           ["Kill" gnus-summary-kill-process-mark t]
2726           ["Yank" gnus-summary-yank-process-mark
2727            gnus-newsgroup-process-stack]
2728           ["Save" gnus-summary-save-process-mark t]
2729           ["Run command on marked..." gnus-summary-universal-argument t]))
2730         ("Registry Marks")
2731         ("Scroll article"
2732          ["Page forward" gnus-summary-next-page
2733           ,@(if (featurep 'xemacs) '(t)
2734               '(:help "Show next page of article"))]
2735          ["Page backward" gnus-summary-prev-page
2736           ,@(if (featurep 'xemacs) '(t)
2737               '(:help "Show previous page of article"))]
2738          ["Line forward" gnus-summary-scroll-up t])
2739         ("Move"
2740          ["Next unread article" gnus-summary-next-unread-article t]
2741          ["Previous unread article" gnus-summary-prev-unread-article t]
2742          ["Next article" gnus-summary-next-article t]
2743          ["Previous article" gnus-summary-prev-article t]
2744          ["Next unread subject" gnus-summary-next-unread-subject t]
2745          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2746          ["Next article same subject" gnus-summary-next-same-subject t]
2747          ["Previous article same subject" gnus-summary-prev-same-subject t]
2748          ["First unread article" gnus-summary-first-unread-article t]
2749          ["Best unread article" gnus-summary-best-unread-article t]
2750          ["Go to subject number..." gnus-summary-goto-subject t]
2751          ["Go to article number..." gnus-summary-goto-article t]
2752          ["Go to the last article" gnus-summary-goto-last-article t]
2753          ["Pop article off history" gnus-summary-pop-article t])
2754         ("Sort"
2755          ["Sort by number" gnus-summary-sort-by-number t]
2756          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2757          ["Sort by author" gnus-summary-sort-by-author t]
2758          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2759          ["Sort by subject" gnus-summary-sort-by-subject t]
2760          ["Sort by date" gnus-summary-sort-by-date t]
2761          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2762          ["Sort by score" gnus-summary-sort-by-score t]
2763          ["Sort by lines" gnus-summary-sort-by-lines t]
2764          ["Sort by characters" gnus-summary-sort-by-chars t]
2765          ["Randomize" gnus-summary-sort-by-random t]
2766          ["Original sort" gnus-summary-sort-by-original t])
2767         ("Help"
2768          ["Describe group" gnus-summary-describe-group t]
2769          ["Read manual" gnus-info-find-node t])
2770         ("Modes"
2771          ["Pick and read" gnus-pick-mode t]
2772          ["Binary" gnus-binary-mode t])
2773         ("Regeneration"
2774          ["Regenerate" gnus-summary-prepare t]
2775          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2776          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2777          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2778          ["Toggle threading" gnus-summary-toggle-threads t])
2779         ["See old articles" gnus-summary-insert-old-articles t]
2780         ["See new articles" gnus-summary-insert-new-articles t]
2781         ["Filter articles..." gnus-summary-execute-command t]
2782         ["Run command on articles..." gnus-summary-universal-argument t]
2783         ["Search articles forward..." gnus-summary-search-article-forward t]
2784         ["Search articles backward..." gnus-summary-search-article-backward t]
2785         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2786         ["Expand window" gnus-summary-expand-window t]
2787         ["Expire expirable articles" gnus-summary-expire-articles
2788          (gnus-check-backend-function
2789           'request-expire-articles gnus-newsgroup-name)]
2790         ["Edit local kill file" gnus-summary-edit-local-kill t]
2791         ["Edit main kill file" gnus-summary-edit-global-kill t]
2792         ["Edit group parameters" gnus-summary-edit-parameters t]
2793         ["Customize group parameters" gnus-summary-customize-parameters t]
2794         ["Send a bug report" gnus-bug t]
2795         ("Exit"
2796          ["Catchup and exit" gnus-summary-catchup-and-exit
2797           ,@(if (featurep 'xemacs) '(t)
2798               '(:help "Mark unread articles in this group as read, then exit"))]
2799          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2800          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2801          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2802          ["Exit group" gnus-summary-exit
2803           ,@(if (featurep 'xemacs) '(t)
2804               '(:help "Exit current group, return to group selection mode"))]
2805          ["Exit group without updating" gnus-summary-exit-no-update t]
2806          ["Exit and goto next group" gnus-summary-next-group t]
2807          ["Exit and goto prev group" gnus-summary-prev-group t]
2808          ["Reselect group" gnus-summary-reselect-current-group t]
2809          ["Rescan group" gnus-summary-rescan-group t]
2810          ["Update dribble" gnus-summary-save-newsrc t])))
2811
2812     (gnus-run-hooks 'gnus-summary-menu-hook)))
2813
2814 (defvar gnus-summary-tool-bar-map nil)
2815
2816 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2817 ;; affect _new_ message buffers.  We might add a function that walks thru all
2818 ;; summary-mode buffers and force the update.
2819 (defun gnus-summary-tool-bar-update (&optional symbol value)
2820   "Update summary mode toolbar.
2821 Setter function for custom variables."
2822   (setq-default gnus-summary-tool-bar-map nil)
2823   (when symbol
2824     ;; When used as ":set" function:
2825     (set-default symbol value))
2826   (when (gnus-buffer-live-p gnus-summary-buffer)
2827     (with-current-buffer gnus-summary-buffer
2828       (gnus-summary-make-tool-bar))))
2829
2830 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2831                                      'gnus-summary-tool-bar-gnome
2832                                    'gnus-summary-tool-bar-retro)
2833   "Specifies the Gnus summary tool bar.
2834
2835 It can be either a list or a symbol refering to a list.  See
2836 `gmm-tool-bar-from-list' for the format of the list.  The
2837 default key map is `gnus-summary-mode-map'.
2838
2839 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2840 `gnus-summary-tool-bar-retro'."
2841   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2842                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2843                  (repeat :tag "User defined list" gmm-tool-bar-item)
2844                  (symbol))
2845   :version "23.1" ;; No Gnus
2846   :initialize 'custom-initialize-default
2847   :set 'gnus-summary-tool-bar-update
2848   :group 'gnus-summary)
2849
2850 (defcustom gnus-summary-tool-bar-gnome
2851   '((gnus-summary-post-news "mail/compose" nil)
2852     (gnus-summary-insert-new-articles "mail/inbox" nil
2853                                       :visible (or (not gnus-agent)
2854                                                    gnus-plugged))
2855     (gnus-summary-reply-with-original "mail/reply")
2856     (gnus-summary-reply "mail/reply" nil :visible nil)
2857     (gnus-summary-followup-with-original "mail/reply-all")
2858     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2859     (gnus-summary-mail-forward "mail/forward")
2860     (gnus-summary-save-article "mail/save")
2861     (gnus-summary-search-article-forward "search" nil :visible nil)
2862     (gnus-summary-print-article "print")
2863     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2864     ;; Some new commands that may need more suitable icons:
2865     (gnus-summary-save-newsrc "save" nil :visible nil)
2866     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2867     (gnus-summary-prev-article "left-arrow")
2868     (gnus-summary-next-article "right-arrow")
2869     (gnus-summary-next-page "next-page")
2870     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2871     ;;
2872     ;; Maybe some sort-by-... could be added:
2873     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2874     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2875     (gnus-summary-mark-as-expirable
2876      "delete" nil
2877      :visible (gnus-check-backend-function 'request-expire-articles
2878                                            gnus-newsgroup-name))
2879     (gnus-summary-mark-as-spam
2880      "mail/spam" t
2881      :visible (and (fboundp 'spam-group-ham-contents-p)
2882                    (spam-group-ham-contents-p gnus-newsgroup-name))
2883      :help "Mark as spam")
2884     (gnus-summary-mark-as-read-forward
2885      "mail/not-spam" nil
2886      :visible (and (fboundp 'spam-group-spam-contents-p)
2887                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2888     ;;
2889     (gnus-summary-exit "exit")
2890     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2891     (gnus-info-find-node "help"))
2892   "List of functions for the summary tool bar (GNOME style).
2893
2894 See `gmm-tool-bar-from-list' for the format of the list."
2895   :type '(repeat gmm-tool-bar-item)
2896   :version "23.1" ;; No Gnus
2897   :initialize 'custom-initialize-default
2898   :set 'gnus-summary-tool-bar-update
2899   :group 'gnus-summary)
2900
2901 (defcustom gnus-summary-tool-bar-retro
2902   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2903     (gnus-summary-next-unread-article "gnus/next-ur")
2904     (gnus-summary-post-news "gnus/post")
2905     (gnus-summary-followup-with-original "gnus/fuwo")
2906     (gnus-summary-followup "gnus/followup")
2907     (gnus-summary-reply-with-original "gnus/reply-wo")
2908     (gnus-summary-reply "gnus/reply")
2909     (gnus-summary-caesar-message "gnus/rot13")
2910     (gnus-uu-decode-uu "gnus/uu-decode")
2911     (gnus-summary-save-article-file "gnus/save-aif")
2912     (gnus-summary-save-article "gnus/save-art")
2913     (gnus-uu-post-news "gnus/uu-post")
2914     (gnus-summary-catchup "gnus/catchup")
2915     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2916     (gnus-summary-exit "gnus/exit-summ")
2917     ;; Some new command that may need more suitable icons:
2918     (gnus-summary-print-article "gnus/print" nil :visible nil)
2919     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2920     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2921     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2922     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2923     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2924     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2925     ;;
2926     (gnus-info-find-node "gnus/help" nil :visible nil))
2927   "List of functions for the summary tool bar (retro look).
2928
2929 See `gmm-tool-bar-from-list' for the format of the list."
2930   :type '(repeat gmm-tool-bar-item)
2931   :version "23.1" ;; No Gnus
2932   :initialize 'custom-initialize-default
2933   :set 'gnus-summary-tool-bar-update
2934   :group 'gnus-summary)
2935
2936 (defcustom gnus-summary-tool-bar-zap-list t
2937   "List of icon items from the global tool bar.
2938 These items are not displayed in the Gnus summary mode tool bar.
2939
2940 See `gmm-tool-bar-from-list' for the format of the list."
2941   :type 'gmm-tool-bar-zap-list
2942   :version "23.1" ;; No Gnus
2943   :initialize 'custom-initialize-default
2944   :set 'gnus-summary-tool-bar-update
2945   :group 'gnus-summary)
2946
2947 (defvar image-load-path)
2948 (defvar tool-bar-map)
2949
2950 (defun gnus-summary-make-tool-bar (&optional force)
2951   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2952 When FORCE, rebuild the tool bar."
2953   (when (and (not (featurep 'xemacs))
2954              (boundp 'tool-bar-mode)
2955              tool-bar-mode
2956              (or (not gnus-summary-tool-bar-map) force))
2957     (let* ((load-path
2958             (gmm-image-load-path-for-library "gnus"
2959                                              "mail/save.xpm"
2960                                              nil t))
2961            (image-load-path (cons (car load-path)
2962                                   (when (boundp 'image-load-path)
2963                                     image-load-path)))
2964            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2965                                         gnus-summary-tool-bar-zap-list
2966                                         'gnus-summary-mode-map)))
2967       (when map
2968         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2969         ;; uses it's value.
2970         (setq gnus-summary-tool-bar-map map))))
2971   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2972
2973 (defun gnus-score-set-default (var value)
2974   "A version of set that updates the GNU Emacs menu-bar."
2975   (set var value)
2976   ;; It is the message that forces the active status to be updated.
2977   (message ""))
2978
2979 (defun gnus-make-score-map (type)
2980   "Make a summary score map of type TYPE."
2981   (if t
2982       nil
2983     (let ((headers '(("author" "from" string)
2984                      ("subject" "subject" string)
2985                      ("article body" "body" string)
2986                      ("article head" "head" string)
2987                      ("xref" "xref" string)
2988                      ("extra header" "extra" string)
2989                      ("lines" "lines" number)
2990                      ("followups to author" "followup" string)))
2991           (types '((number ("less than" <)
2992                            ("greater than" >)
2993                            ("equal" =))
2994                    (string ("substring" s)
2995                            ("exact string" e)
2996                            ("fuzzy string" f)
2997                            ("regexp" r))))
2998           (perms '(("temporary" (current-time-string))
2999                    ("permanent" nil)
3000                    ("immediate" now)))
3001           header)
3002       (list
3003        (apply
3004         'nconc
3005         (list
3006          (if (eq type 'lower)
3007              "Lower score"
3008            "Increase score"))
3009         (let (outh)
3010           (while headers
3011             (setq header (car headers))
3012             (setq outh
3013                   (cons
3014                    (apply
3015                     'nconc
3016                     (list (car header))
3017                     (let ((ts (cdr (assoc (nth 2 header) types)))
3018                           outt)
3019                       (while ts
3020                         (setq outt
3021                               (cons
3022                                (apply
3023                                 'nconc
3024                                 (list (caar ts))
3025                                 (let ((ps perms)
3026                                       outp)
3027                                   (while ps
3028                                     (setq outp
3029                                           (cons
3030                                            (vector
3031                                             (caar ps)
3032                                             (list
3033                                              'gnus-summary-score-entry
3034                                              (nth 1 header)
3035                                              (if (or (string= (nth 1 header)
3036                                                               "head")
3037                                                      (string= (nth 1 header)
3038                                                               "body"))
3039                                                  ""
3040                                                (list 'gnus-summary-header
3041                                                      (nth 1 header)))
3042                                              (list 'quote (nth 1 (car ts)))
3043                                              (list 'gnus-score-delta-default
3044                                                    nil)
3045                                              (nth 1 (car ps))
3046                                              t)
3047                                             t)
3048                                            outp))
3049                                     (setq ps (cdr ps)))
3050                                   (list (nreverse outp))))
3051                                outt))
3052                         (setq ts (cdr ts)))
3053                       (list (nreverse outt))))
3054                    outh))
3055             (setq headers (cdr headers)))
3056           (list (nreverse outh))))))))
3057
3058
3059 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3060 (defvar bookmark-make-record-function)
3061 \f
3062
3063 (defun gnus-summary-mode (&optional group)
3064   "Major mode for reading articles.
3065
3066 All normal editing commands are switched off.
3067 \\<gnus-summary-mode-map>
3068 Each line in this buffer represents one article.  To read an
3069 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3070 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3071 respectively.
3072
3073 You can also post articles and send mail from this buffer.  To
3074 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3075 of an article, type `\\[gnus-summary-reply]'.
3076
3077 There are approx. one gazillion commands you can execute in this
3078 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3079
3080 The following commands are available:
3081
3082 \\{gnus-summary-mode-map}"
3083   (interactive)
3084   (kill-all-local-variables)
3085   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3086     (gnus-summary-make-local-variables))
3087   (gnus-summary-make-local-variables)
3088   (setq gnus-newsgroup-name group)
3089   (when (gnus-visual-p 'summary-menu 'menu)
3090     (gnus-summary-make-menu-bar)
3091     (gnus-summary-make-tool-bar))
3092   (gnus-make-thread-indent-array)
3093   (gnus-simplify-mode-line)
3094   (setq major-mode 'gnus-summary-mode)
3095   (setq mode-name "Summary")
3096   (use-local-map gnus-summary-mode-map)
3097   (buffer-disable-undo)
3098   (setq buffer-read-only t              ;Disable modification
3099         show-trailing-whitespace nil)
3100   (setq truncate-lines t)
3101   (add-to-invisibility-spec '(gnus-sum . t))
3102   (gnus-summary-set-display-table)
3103   (gnus-set-default-directory)
3104   (make-local-variable 'gnus-summary-line-format)
3105   (make-local-variable 'gnus-summary-line-format-spec)
3106   (make-local-variable 'gnus-summary-dummy-line-format)
3107   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3108   (make-local-variable 'gnus-summary-mark-positions)
3109   (gnus-make-local-hook 'pre-command-hook)
3110   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3111   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3112   (turn-on-gnus-mailing-list-mode)
3113   (mm-enable-multibyte)
3114   (set (make-local-variable 'bookmark-make-record-function)
3115        'gnus-summary-bookmark-make-record)
3116   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3117   (gnus-update-summary-mark-positions))
3118
3119 (defun gnus-summary-make-local-variables ()
3120   "Make all the local summary buffer variables."
3121   (let (global)
3122     (dolist (local gnus-summary-local-variables)
3123       (if (consp local)
3124           (progn
3125             (if (eq (cdr local) 'global)
3126                 ;; Copy the global value of the variable.
3127                 (setq global (symbol-value (car local)))
3128               ;; Use the value from the list.
3129               (setq global (eval (cdr local))))
3130             (set (make-local-variable (car local)) global))
3131         ;; Simple nil-valued local variable.
3132         (set (make-local-variable local) nil)))))
3133
3134 ;; Summary data functions.
3135
3136 (defmacro gnus-data-number (data)
3137   `(car ,data))
3138
3139 (defmacro gnus-data-set-number (data number)
3140   `(setcar ,data ,number))
3141
3142 (defmacro gnus-data-mark (data)
3143   `(nth 1 ,data))
3144
3145 (defmacro gnus-data-set-mark (data mark)
3146   `(setcar (nthcdr 1 ,data) ,mark))
3147
3148 (defmacro gnus-data-pos (data)
3149   `(nth 2 ,data))
3150
3151 (defmacro gnus-data-set-pos (data pos)
3152   `(setcar (nthcdr 2 ,data) ,pos))
3153
3154 (defmacro gnus-data-header (data)
3155   `(nth 3 ,data))
3156
3157 (defmacro gnus-data-set-header (data header)
3158   `(setf (nth 3 ,data) ,header))
3159
3160 (defmacro gnus-data-level (data)
3161   `(nth 4 ,data))
3162
3163 (defmacro gnus-data-unread-p (data)
3164   `(= (nth 1 ,data) gnus-unread-mark))
3165
3166 (defmacro gnus-data-read-p (data)
3167   `(/= (nth 1 ,data) gnus-unread-mark))
3168
3169 (defmacro gnus-data-pseudo-p (data)
3170   `(consp (nth 3 ,data)))
3171
3172 (defmacro gnus-data-find (number)
3173   `(assq ,number gnus-newsgroup-data))
3174
3175 (defmacro gnus-data-find-list (number &optional data)
3176   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3177      (memq (assq ,number bdata)
3178            bdata)))
3179
3180 (defmacro gnus-data-make (number mark pos header level)
3181   `(list ,number ,mark ,pos ,header ,level))
3182
3183 (defun gnus-data-enter (after-article number mark pos header level offset)
3184   (let ((data (gnus-data-find-list after-article)))
3185     (unless data
3186       (error "No such article: %d" after-article))
3187     (setcdr data (cons (gnus-data-make number mark pos header level)
3188                        (cdr data)))
3189     (setq gnus-newsgroup-data-reverse nil)
3190     (gnus-data-update-list (cddr data) offset)))
3191
3192 (defun gnus-data-enter-list (after-article list &optional offset)
3193   (when list
3194     (let ((data (and after-article (gnus-data-find-list after-article)))
3195           (ilist list))
3196       (if (not (or data
3197                    after-article))
3198           (let ((odata gnus-newsgroup-data))
3199             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3200             (when offset
3201               (gnus-data-update-list odata offset)))
3202         ;; Find the last element in the list to be spliced into the main
3203         ;; list.
3204         (setq list (last list))
3205         (if (not data)
3206             (progn
3207               (setcdr list gnus-newsgroup-data)
3208               (setq gnus-newsgroup-data ilist)
3209               (when offset
3210                 (gnus-data-update-list (cdr list) offset)))
3211           (setcdr list (cdr data))
3212           (setcdr data ilist)
3213           (when offset
3214             (gnus-data-update-list (cdr list) offset))))
3215       (setq gnus-newsgroup-data-reverse nil))))
3216
3217 (defun gnus-data-remove (article &optional offset)
3218   (let ((data gnus-newsgroup-data))
3219     (if (= (gnus-data-number (car data)) article)
3220         (progn
3221           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3222                 gnus-newsgroup-data-reverse nil)
3223           (when offset
3224             (gnus-data-update-list gnus-newsgroup-data offset)))
3225       (while (cdr data)
3226         (when (= (gnus-data-number (cadr data)) article)
3227           (setcdr data (cddr data))
3228           (when offset
3229             (gnus-data-update-list (cdr data) offset))
3230           (setq data nil
3231                 gnus-newsgroup-data-reverse nil))
3232         (setq data (cdr data))))))
3233
3234 (defmacro gnus-data-list (backward)
3235   `(if ,backward
3236        (or gnus-newsgroup-data-reverse
3237            (setq gnus-newsgroup-data-reverse
3238                  (reverse gnus-newsgroup-data)))
3239      gnus-newsgroup-data))
3240
3241 (defun gnus-data-update-list (data offset)
3242   "Add OFFSET to the POS of all data entries in DATA."
3243   (setq gnus-newsgroup-data-reverse nil)
3244   (while data
3245     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3246     (setq data (cdr data))))
3247
3248 (defun gnus-summary-article-pseudo-p (article)
3249   "Say whether this article is a pseudo article or not."
3250   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3251
3252 (defmacro gnus-summary-article-sparse-p (article)
3253   "Say whether this article is a sparse article or not."
3254   `(memq ,article gnus-newsgroup-sparse))
3255
3256 (defmacro gnus-summary-article-ancient-p (article)
3257   "Say whether this article is a sparse article or not."
3258   `(memq ,article gnus-newsgroup-ancient))
3259
3260 (defun gnus-article-parent-p (number)
3261   "Say whether this article is a parent or not."
3262   (let ((data (gnus-data-find-list number)))
3263     (and (cdr data)              ; There has to be an article after...
3264          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3265             (gnus-data-level (nth 1 data))))))
3266
3267 (defun gnus-article-children (number)
3268   "Return a list of all children to NUMBER."
3269   (let* ((data (gnus-data-find-list number))
3270          (level (gnus-data-level (car data)))
3271          children)
3272     (setq data (cdr data))
3273     (while (and data
3274                 (= (gnus-data-level (car data)) (1+ level)))
3275       (push (gnus-data-number (car data)) children)
3276       (setq data (cdr data)))
3277     children))
3278
3279 (defmacro gnus-summary-skip-intangible ()
3280   "If the current article is intangible, then jump to a different article."
3281   '(let ((to (get-text-property (point) 'gnus-intangible)))
3282      (and to (gnus-summary-goto-subject to))))
3283
3284 (defmacro gnus-summary-article-intangible-p ()
3285   "Say whether this article is intangible or not."
3286   '(get-text-property (point) 'gnus-intangible))
3287
3288 (defun gnus-article-read-p (article)
3289   "Say whether ARTICLE is read or not."
3290   (not (or (memq article gnus-newsgroup-marked)
3291            (memq article gnus-newsgroup-spam-marked)
3292            (memq article gnus-newsgroup-unreads)
3293            (memq article gnus-newsgroup-unselected)
3294            (memq article gnus-newsgroup-dormant))))
3295
3296 ;; Some summary mode macros.
3297
3298 (defmacro gnus-summary-article-number ()
3299   "The article number of the article on the current line.
3300 If there isn't an article number here, then we return the current
3301 article number."
3302   '(progn
3303      (gnus-summary-skip-intangible)
3304      (or (get-text-property (point) 'gnus-number)
3305          (gnus-summary-last-subject))))
3306
3307 (defmacro gnus-summary-article-header (&optional number)
3308   "Return the header of article NUMBER."
3309   `(gnus-data-header (gnus-data-find
3310                       ,(or number '(gnus-summary-article-number)))))
3311
3312 (defmacro gnus-summary-thread-level (&optional number)
3313   "Return the level of thread that starts with article NUMBER."
3314   `(if (and (eq gnus-summary-make-false-root 'dummy)
3315             (get-text-property (point) 'gnus-intangible))
3316        0
3317      (gnus-data-level (gnus-data-find
3318                        ,(or number '(gnus-summary-article-number))))))
3319
3320 (defmacro gnus-summary-article-mark (&optional number)
3321   "Return the mark of article NUMBER."
3322   `(gnus-data-mark (gnus-data-find
3323                     ,(or number '(gnus-summary-article-number)))))
3324
3325 (defmacro gnus-summary-article-pos (&optional number)
3326   "Return the position of the line of article NUMBER."
3327   `(gnus-data-pos (gnus-data-find
3328                    ,(or number '(gnus-summary-article-number)))))
3329
3330 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3331 (defmacro gnus-summary-article-subject (&optional number)
3332   "Return current subject string or nil if nothing."
3333   `(let ((headers
3334           ,(if number
3335                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3336              '(gnus-data-header (assq (gnus-summary-article-number)
3337                                       gnus-newsgroup-data)))))
3338      (and headers
3339           (vectorp headers)
3340           (mail-header-subject headers))))
3341
3342 (defmacro gnus-summary-article-score (&optional number)
3343   "Return current article score."
3344   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3345                   gnus-newsgroup-scored))
3346        gnus-summary-default-score 0))
3347
3348 (defun gnus-summary-article-children (&optional number)
3349   "Return a list of article numbers that are children of article NUMBER."
3350   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3351          (level (gnus-data-level (car data)))
3352          l children)
3353     (while (and (setq data (cdr data))
3354                 (> (setq l (gnus-data-level (car data))) level))
3355       (and (= (1+ level) l)
3356            (push (gnus-data-number (car data))
3357                  children)))
3358     (nreverse children)))
3359
3360 (defun gnus-summary-article-parent (&optional number)
3361   "Return the article number of the parent of article NUMBER."
3362   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3363                                     (gnus-data-list t)))
3364          (level (gnus-data-level (car data))))
3365     (if (zerop level)
3366         ()                              ; This is a root.
3367       ;; We search until we find an article with a level less than
3368       ;; this one.  That function has to be the parent.
3369       (while (and (setq data (cdr data))
3370                   (not (< (gnus-data-level (car data)) level))))
3371       (and data (gnus-data-number (car data))))))
3372
3373 (defun gnus-unread-mark-p (mark)
3374   "Say whether MARK is the unread mark."
3375   (= mark gnus-unread-mark))
3376
3377 (defun gnus-read-mark-p (mark)
3378   "Say whether MARK is one of the marks that mark as read.
3379 This is all marks except unread, ticked, dormant, and expirable."
3380   (not (or (= mark gnus-unread-mark)
3381            (= mark gnus-ticked-mark)
3382            (= mark gnus-spam-mark)
3383            (= mark gnus-dormant-mark)
3384            (= mark gnus-expirable-mark))))
3385
3386 (defmacro gnus-article-mark (number)
3387   "Return the MARK of article NUMBER.
3388 This macro should only be used when computing the mark the \"first\"
3389 time; i.e., when generating the summary lines.  After that,
3390 `gnus-summary-article-mark' should be used to examine the
3391 marks of articles."
3392   `(cond
3393     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3394     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3395     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3396     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3397     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3398     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3399     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3400     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3401            gnus-ancient-mark))))
3402
3403 ;; Saving hidden threads.
3404
3405 (defmacro gnus-save-hidden-threads (&rest forms)
3406   "Save hidden threads, eval FORMS, and restore the hidden threads."
3407   (let ((config (make-symbol "config")))
3408     `(let ((,config (gnus-hidden-threads-configuration)))
3409        (unwind-protect
3410            (save-excursion
3411              ,@forms)
3412          (gnus-restore-hidden-threads-configuration ,config)))))
3413 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3414 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3415
3416 (defun gnus-data-compute-positions ()
3417   "Compute the positions of all articles."
3418   (setq gnus-newsgroup-data-reverse nil)
3419   (let ((data gnus-newsgroup-data))
3420     (save-excursion
3421       (gnus-save-hidden-threads
3422         (gnus-summary-show-all-threads)
3423         (goto-char (point-min))
3424         (while data
3425           (while (get-text-property (point) 'gnus-intangible)
3426             (forward-line 1))
3427           (gnus-data-set-pos (car data) (+ (point) 3))
3428           (setq data (cdr data))
3429           (forward-line 1))))))
3430
3431 (defun gnus-hidden-threads-configuration ()
3432   "Return the current hidden threads configuration."
3433   (save-excursion
3434     (let (config)
3435       (goto-char (point-min))
3436       (while (not (eobp))
3437         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3438           (push (save-excursion (forward-line 0) (point)) config))
3439         (forward-line 1))
3440       config)))
3441
3442 (defun gnus-restore-hidden-threads-configuration (config)
3443   "Restore hidden threads configuration from CONFIG."
3444   (save-excursion
3445     (let (point (inhibit-read-only t))
3446       (while (setq point (pop config))
3447         (goto-char point)
3448         (gnus-summary-hide-thread)))))
3449
3450 ;; Various summary mode internalish functions.
3451
3452 (defun gnus-mouse-pick-article (e)
3453   (interactive "e")
3454   (mouse-set-point e)
3455   (gnus-summary-next-page nil t))
3456
3457 (defun gnus-summary-set-display-table ()
3458   "Change the display table.
3459 Odd characters have a tendency to mess
3460 up nicely formatted displays - we make all possible glyphs
3461 display only a single character."
3462
3463   ;; We start from the standard display table, if any.
3464   (let ((table (or (copy-sequence standard-display-table)
3465                    (make-display-table)))
3466         (i 32))
3467     ;; Nix out all the control chars...
3468     (while (>= (setq i (1- i)) 0)
3469       (gnus-put-display-table i [??] table))
3470    ;; ... but not newline and cr, of course.  (cr is necessary for the
3471     ;; selective display).
3472     (gnus-put-display-table ?\n nil table)
3473     (gnus-put-display-table ?\r nil table)
3474     ;; We keep TAB as well.
3475     (gnus-put-display-table ?\t nil table)
3476     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3477     ;; Emacs 23 (unicode), that are not set already.
3478     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3479                  160
3480                256)))
3481       (while (>= (setq i (1- i)) 127)
3482         ;; Only modify if the entry is nil.
3483         (unless (gnus-get-display-table i table)
3484           (gnus-put-display-table i [??] table))))
3485     (setq buffer-display-table table)))
3486
3487 (defun gnus-summary-set-article-display-arrow (pos)
3488   "Update the overlay arrow to point to line at position POS."
3489   (when gnus-summary-display-arrow
3490     (make-local-variable 'overlay-arrow-position)
3491     (make-local-variable 'overlay-arrow-string)
3492     (save-excursion
3493       (goto-char pos)
3494       (beginning-of-line)
3495       (unless overlay-arrow-position
3496         (setq overlay-arrow-position (make-marker)))
3497       (setq overlay-arrow-string "=>"
3498             overlay-arrow-position (set-marker overlay-arrow-position
3499                                                (point)
3500                                                (current-buffer))))))
3501
3502 (defun gnus-summary-setup-buffer (group)
3503   "Initialize summary buffer."
3504   (let ((buffer (gnus-summary-buffer-name group))
3505         (dead-name (concat "*Dead Summary "
3506                            (gnus-group-decoded-name group) "*")))
3507     ;; If a dead summary buffer exists, we kill it.
3508     (when (gnus-buffer-live-p dead-name)
3509       (gnus-kill-buffer dead-name))
3510     (if (get-buffer buffer)
3511         (progn
3512           (set-buffer buffer)
3513           (setq gnus-summary-buffer (current-buffer))
3514           (not gnus-newsgroup-prepared))
3515       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3516       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3517       (gnus-summary-mode group)
3518       (when (gnus-group-quit-config group)
3519         (set (make-local-variable 'gnus-single-article-buffer) nil))
3520       (make-local-variable 'gnus-article-buffer)
3521       (make-local-variable 'gnus-article-current)
3522       (make-local-variable 'gnus-original-article-buffer)
3523       (setq gnus-newsgroup-name group)
3524       ;; Set any local variables in the group parameters.
3525       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3526       t)))
3527
3528 (defun gnus-set-global-variables ()
3529   "Set the global equivalents of the buffer-local variables.
3530 They are set to the latest values they had.  These reflect the summary
3531 buffer that was in action when the last article was fetched."
3532   (when (eq major-mode 'gnus-summary-mode)
3533     (setq gnus-summary-buffer (current-buffer))
3534     (let ((name gnus-newsgroup-name)
3535           (marked gnus-newsgroup-marked)
3536           (spam gnus-newsgroup-spam-marked)
3537           (unread gnus-newsgroup-unreads)
3538           (headers gnus-current-headers)
3539           (data gnus-newsgroup-data)
3540           (summary gnus-summary-buffer)
3541           (article-buffer gnus-article-buffer)
3542           (original gnus-original-article-buffer)
3543           (gac gnus-article-current)
3544           (reffed gnus-reffed-article-number)
3545           (score-file gnus-current-score-file)
3546           (default-charset gnus-newsgroup-charset)
3547           vlist)
3548       (let ((locals gnus-newsgroup-variables))
3549         (while locals
3550           (if (consp (car locals))
3551               (push (eval (caar locals)) vlist)
3552             (push (eval (car locals)) vlist))
3553           (setq locals (cdr locals)))
3554         (setq vlist (nreverse vlist)))
3555       (with-current-buffer gnus-group-buffer
3556         (setq gnus-newsgroup-name name
3557               gnus-newsgroup-marked marked
3558               gnus-newsgroup-spam-marked spam
3559               gnus-newsgroup-unreads unread
3560               gnus-current-headers headers
3561               gnus-newsgroup-data data
3562               gnus-article-current gac
3563               gnus-summary-buffer summary
3564               gnus-article-buffer article-buffer
3565               gnus-original-article-buffer original
3566               gnus-reffed-article-number reffed
3567               gnus-current-score-file score-file
3568               gnus-newsgroup-charset default-charset)
3569         (let ((locals gnus-newsgroup-variables))
3570           (while locals
3571             (if (consp (car locals))
3572                 (set (caar locals) (pop vlist))
3573               (set (car locals) (pop vlist)))
3574             (setq locals (cdr locals))))
3575         ;; The article buffer also has local variables.
3576         (when (gnus-buffer-live-p gnus-article-buffer)
3577           (set-buffer gnus-article-buffer)
3578           (setq gnus-summary-buffer summary))))))
3579
3580 (defun gnus-summary-article-unread-p (article)
3581   "Say whether ARTICLE is unread or not."
3582   (memq article gnus-newsgroup-unreads))
3583
3584 (defun gnus-summary-first-article-p (&optional article)
3585   "Return whether ARTICLE is the first article in the buffer."
3586   (if (not (setq article (or article (gnus-summary-article-number))))
3587       nil
3588     (eq article (caar gnus-newsgroup-data))))
3589
3590 (defun gnus-summary-last-article-p (&optional article)
3591   "Return whether ARTICLE is the last article in the buffer."
3592   (if (not (setq article (or article (gnus-summary-article-number))))
3593       ;; All non-existent numbers are the last article.  :-)
3594       t
3595     (not (cdr (gnus-data-find-list article)))))
3596
3597 (defun gnus-make-thread-indent-array (&optional n)
3598   (when (or n
3599             (progn (setq n 200) nil)
3600             (null gnus-thread-indent-array)
3601             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3602     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3603           gnus-thread-indent-array-level gnus-thread-indent-level)
3604     (while (>= n 0)
3605       (aset gnus-thread-indent-array n
3606             (make-string (* n gnus-thread-indent-level) ? ))
3607       (setq n (1- n)))))
3608
3609 (defun gnus-update-summary-mark-positions ()
3610   "Compute where the summary marks are to go."
3611   (save-excursion
3612     (when (gnus-buffer-exists-p gnus-summary-buffer)
3613       (set-buffer gnus-summary-buffer))
3614     (let ((spec gnus-summary-line-format-spec)
3615           pos)
3616       (save-excursion
3617         (gnus-set-work-buffer)
3618         (let ((gnus-tmp-unread ?Z)
3619               (gnus-replied-mark ?Z)
3620               (gnus-score-below-mark ?Z)
3621               (gnus-score-over-mark ?Z)
3622               (gnus-undownloaded-mark ?Z)
3623               (gnus-summary-line-format-spec spec)
3624               (gnus-newsgroup-downloadable '(0))
3625               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3626               case-fold-search ignores)
3627           ;; Here, all marks are bound to Z.
3628           (gnus-summary-insert-line header
3629                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3630           (goto-char (point-min))
3631           ;; Memorize the positions of the same characters as dummy marks.
3632           (while (re-search-forward "[A-D]" nil t)
3633             (push (point) ignores))
3634           (erase-buffer)
3635           ;; We use A-D as dummy marks in order to know column positions
3636           ;; where marks should be inserted.
3637           (setq gnus-tmp-unread ?A
3638                 gnus-replied-mark ?B
3639                 gnus-score-below-mark ?C
3640                 gnus-score-over-mark ?C
3641                 gnus-undownloaded-mark ?D)
3642           (gnus-summary-insert-line header
3643                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3644           ;; Ignore characters which aren't dummy marks.
3645           (dolist (p ignores)
3646             (delete-region (goto-char (1- p)) p)
3647             (insert ?Z))
3648           (goto-char (point-min))
3649           (setq pos (list (cons 'unread
3650                                 (and (search-forward "A" nil t)
3651                                      (- (point) (point-min) 1)))))
3652           (goto-char (point-min))
3653           (push (cons 'replied (and (search-forward "B" nil t)
3654                                     (- (point) (point-min) 1)))
3655                 pos)
3656           (goto-char (point-min))
3657           (push (cons 'score (and (search-forward "C" nil t)
3658                                   (- (point) (point-min) 1)))
3659                 pos)
3660           (goto-char (point-min))
3661           (push (cons 'download (and (search-forward "D" nil t)
3662                                      (- (point) (point-min) 1)))
3663                 pos)))
3664       (setq gnus-summary-mark-positions pos))))
3665
3666 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3667   "Insert a dummy root in the summary buffer."
3668   (beginning-of-line)
3669   (gnus-add-text-properties
3670    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3671    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3672
3673 (defun gnus-summary-extract-address-component (from)
3674   (or (car (funcall gnus-extract-address-components from))
3675       from))
3676
3677 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3678   (let ((mail-parse-charset gnus-newsgroup-charset)
3679         (ignored-from-addresses (gnus-ignored-from-addresses))
3680         ; Is it really necessary to do this next part for each summary line?
3681         ; Luckily, doesn't seem to slow things down much.
3682         (mail-parse-ignored-charsets
3683          (with-current-buffer gnus-summary-buffer
3684            gnus-newsgroup-ignored-charsets)))
3685     (or
3686      (and ignored-from-addresses
3687           (string-match ignored-from-addresses gnus-tmp-from)
3688           (let ((extra-headers (mail-header-extra header))
3689                 to
3690                 newsgroups)
3691             (cond
3692              ((setq to (cdr (assq 'To extra-headers)))
3693               (concat gnus-summary-to-prefix
3694                       (inline
3695                         (gnus-summary-extract-address-component
3696                          (funcall gnus-decode-encoded-address-function to)))))
3697              ((setq newsgroups
3698                     (or
3699                      (cdr (assq 'Newsgroups extra-headers))
3700                      (and
3701                       (memq 'Newsgroups gnus-extra-headers)
3702                       (eq (car (gnus-find-method-for-group
3703                                 gnus-newsgroup-name)) 'nntp)
3704                       (gnus-group-real-name gnus-newsgroup-name))))
3705               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3706      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3707
3708 (defun gnus-summary-insert-line (gnus-tmp-header
3709                                  gnus-tmp-level gnus-tmp-current
3710                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3711                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3712                                  &optional gnus-tmp-dummy gnus-tmp-score
3713                                  gnus-tmp-process)
3714   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3715       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3716                                           gnus-tmp-level)))
3717   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3718          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3719          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3720          (gnus-tmp-score-char
3721           (if (or (null gnus-summary-default-score)
3722                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3723                       gnus-summary-zcore-fuzz))
3724               ?                         ;Whitespace
3725             (if (< gnus-tmp-score gnus-summary-default-score)
3726                 gnus-score-below-mark gnus-score-over-mark)))
3727          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3728          (gnus-tmp-replied
3729           (cond (gnus-tmp-process gnus-process-mark)
3730                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3731                  gnus-cached-mark)
3732                 (gnus-tmp-replied gnus-replied-mark)
3733                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3734                  gnus-forwarded-mark)
3735                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3736                  gnus-saved-mark)
3737                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3738                  gnus-recent-mark)
3739                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3740                  gnus-unseen-mark)
3741                 (t gnus-no-mark)))
3742          (gnus-tmp-downloaded
3743           (cond (undownloaded
3744                  gnus-undownloaded-mark)
3745                 (gnus-newsgroup-agentized
3746                  gnus-downloaded-mark)
3747                 (t
3748                  gnus-no-mark)))
3749          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3750          (gnus-tmp-name
3751           (cond
3752            ((string-match "<[^>]+> *$" gnus-tmp-from)
3753             (let ((beg (match-beginning 0)))
3754               (or (and (string-match "^\".+\"" gnus-tmp-from)
3755                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3756                   (substring gnus-tmp-from 0 beg))))
3757            ((string-match "(.+)" gnus-tmp-from)
3758             (substring gnus-tmp-from
3759                        (1+ (match-beginning 0)) (1- (match-end 0))))
3760            (t gnus-tmp-from)))
3761          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3762          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3763          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3764          (inhibit-read-only t))
3765     (when (string= gnus-tmp-name "")
3766       (setq gnus-tmp-name gnus-tmp-from))
3767     (unless (numberp gnus-tmp-lines)
3768       (setq gnus-tmp-lines -1))
3769     (if (= gnus-tmp-lines -1)
3770         (setq gnus-tmp-lines "?")
3771       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3772     (condition-case ()
3773         (gnus-put-text-property
3774          (point)
3775          (progn (eval gnus-summary-line-format-spec) (point))
3776          'gnus-number gnus-tmp-number)
3777       (error (gnus-message 5 "Error updating the summary line")))
3778     (when (gnus-visual-p 'summary-highlight 'highlight)
3779       (forward-line -1)
3780       (gnus-summary-highlight-line)
3781       (gnus-run-hooks 'gnus-summary-update-hook)
3782       (forward-line 1))))
3783
3784 (defun gnus-summary-update-line (&optional dont-update)
3785   "Update summary line after change."
3786   (when (and gnus-summary-default-score
3787              (not gnus-summary-inhibit-highlight))
3788     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3789            (article (gnus-summary-article-number))
3790            (score (gnus-summary-article-score article)))
3791       (unless dont-update
3792         (if (and gnus-summary-mark-below
3793                  (< (gnus-summary-article-score)
3794                     gnus-summary-mark-below))
3795             ;; This article has a low score, so we mark it as read.
3796             (when (memq article gnus-newsgroup-unreads)
3797               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3798           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3799             ;; This article was previously marked as read on account
3800             ;; of a low score, but now it has risen, so we mark it as
3801             ;; unread.
3802             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3803         (gnus-summary-update-mark
3804          (if (or (null gnus-summary-default-score)
3805                  (<= (abs (- score gnus-summary-default-score))
3806                      gnus-summary-zcore-fuzz))
3807              ?                          ;Whitespace
3808            (if (< score gnus-summary-default-score)
3809                gnus-score-below-mark gnus-score-over-mark))
3810          'score))
3811       ;; Do visual highlighting.
3812       (when (gnus-visual-p 'summary-highlight 'highlight)
3813         (gnus-summary-highlight-line)
3814         (gnus-run-hooks 'gnus-summary-update-hook)))))
3815
3816 (defvar gnus-tmp-new-adopts nil)
3817
3818 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3819   "Return the number of articles in THREAD.
3820 This may be 0 in some cases -- if none of the articles in
3821 the thread are to be displayed."
3822   (let* ((number
3823          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3824           (cond
3825            ((not (listp thread))
3826             1)
3827            ((and (consp thread) (cdr thread))
3828             (apply
3829              '+ 1 (mapcar
3830                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3831            ((null thread)
3832             1)
3833            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3834             1)
3835            (t 0))))
3836     (when (and level (zerop level) gnus-tmp-new-adopts)
3837       (incf number
3838             (apply '+ (mapcar
3839                        'gnus-summary-number-of-articles-in-thread
3840                        gnus-tmp-new-adopts))))
3841     (if char
3842         (if (> number 1) gnus-not-empty-thread-mark
3843           gnus-empty-thread-mark)
3844       number)))
3845
3846 (defsubst gnus-summary-line-message-size (head)
3847   "Return pretty-printed version of message size.
3848 This function is intended to be used in
3849 `gnus-summary-line-format-alist'."
3850   (let ((c (or (mail-header-chars head) -1)))
3851     (cond ((< c 0) "n/a")               ; chars not available
3852           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3853           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3854           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3855           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3856
3857 (defcustom gnus-summary-user-date-format-alist
3858   '(((gnus-seconds-today) . "Today, %H:%M")
3859     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3860     (604800 . "%A %H:%M")               ; That's one week
3861     ((gnus-seconds-month) . "%A %d")
3862     ((gnus-seconds-year) . "%B %d")
3863     (t . "%b %d %Y"))                   ; This one is used when no other
3864                                         ; does match
3865   "Specifies date format depending on age of article.
3866 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3867 seconds) or a Lisp expression evaluating to a number.  When the age of
3868 the article is less than this number, then use `format-time-string'
3869 with the corresponding FORMAT for displaying the date of the article.
3870 If AGE is not a number or a Lisp expression evaluating to a
3871 non-number, then the corresponding FORMAT is used as a default value.
3872
3873 Note that the list is processed from the beginning, so it should be
3874 sorted by ascending AGE.  Also note that items following the first
3875 non-number AGE will be ignored.
3876
3877 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3878 and `gnus-seconds-year' in the AGE spec.  They return the number of
3879 seconds passed since the start of today, of this month, of this year,
3880 respectively."
3881   :version "24.1"
3882   :group 'gnus-summary-format
3883   :type '(alist :key-type sexp :value-type string))
3884 (make-obsolete-variable 'gnus-user-date-format-alist
3885                         'gnus-summary-user-date-format-alist "24.1")
3886
3887 (defun gnus-user-date (messy-date)
3888   "Format the messy-date according to `gnus-summary-user-date-format-alist'.
3889 Returns \"  ?  \" if there's bad input or if another error occurs.
3890 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3891   (condition-case ()
3892       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3893              (now (gnus-float-time))
3894              ;;If we don't find something suitable we'll use this one
3895              (my-format "%b %d '%y"))
3896         (let* ((difference (- now messy-date))
3897                (templist gnus-summary-user-date-format-alist)
3898                (top (eval (caar templist))))
3899           (while (if (numberp top) (< top difference) (not top))
3900             (progn
3901               (setq templist (cdr templist))
3902               (setq top (eval (caar templist)))))
3903           (if (stringp (cdr (car templist)))
3904               (setq my-format (cdr (car templist)))))
3905         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3906     (error "  ?   ")))
3907
3908 (defun gnus-summary-set-local-parameters (group)
3909   "Go through the local params of GROUP and set all variable specs in that list."
3910   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3911                                         ; really variables.
3912     (dolist (elem (gnus-group-find-parameter group))
3913       (and (consp elem)                 ; Has to be a cons.
3914            (consp (cdr elem))           ; The cdr has to be a list.
3915            (symbolp (car elem))         ; Has to be a symbol in there.
3916            (not (memq (car elem) vars))
3917            (ignore-errors
3918              (push (car elem) vars)
3919              ;; Variables like `gnus-show-threads' that are globally
3920              ;; bound, if used as group parameters, need to get to be
3921              ;; buffer-local, whereas just parameters like `gcc-self',
3922              ;; `timestamp', etc. should not be bound as variables.
3923              (if (boundp (car elem))
3924                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3925                (eval (nth 1 elem))))))))
3926
3927 (defun gnus-summary-read-group (group &optional show-all no-article
3928                                       kill-buffer no-display backward
3929                                       select-articles)
3930   "Start reading news in newsgroup GROUP.
3931 If SHOW-ALL is non-nil, already read articles are also listed.
3932 If NO-ARTICLE is non-nil, no article is selected initially.
3933 If NO-DISPLAY, don't generate a summary buffer."
3934   (let (result)
3935     (while (and group
3936                 (null (setq result
3937                             (let ((gnus-auto-select-next nil))
3938                               (or (gnus-summary-read-group-1
3939                                    group show-all no-article
3940                                    kill-buffer no-display
3941                                    select-articles)
3942                                   (setq show-all nil
3943                                         select-articles nil)))))
3944                 (eq gnus-auto-select-next 'quietly))
3945       (set-buffer gnus-group-buffer)
3946       ;; The entry function called above goes to the next
3947       ;; group automatically, so we go two groups back
3948       ;; if we are searching for the previous group.
3949       (when backward
3950         (gnus-group-prev-unread-group 2))
3951       (if (not (equal group (gnus-group-group-name)))
3952           (setq group (gnus-group-group-name))
3953         (setq group nil)))
3954     result))
3955
3956 (defun gnus-summary-read-group-1 (group show-all no-article
3957                                         kill-buffer no-display
3958                                         &optional select-articles)
3959   ;; Killed foreign groups can't be entered.
3960   ;;  (when (and (not (gnus-group-native-p group))
3961   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3962   ;;    (error "Dead non-native groups can't be entered"))
3963   (gnus-message 5 "Retrieving newsgroup: %s..."
3964                 (gnus-group-decoded-name group))
3965   (let* ((new-group (gnus-summary-setup-buffer group))
3966          (quit-config (gnus-group-quit-config group))
3967          (did-select (and new-group (gnus-select-newsgroup
3968                                      group show-all select-articles))))
3969     (cond
3970      ;; This summary buffer exists already, so we just select it.
3971      ((not new-group)
3972       (gnus-set-global-variables)
3973       (when kill-buffer
3974         (gnus-kill-or-deaden-summary kill-buffer))
3975       (gnus-configure-windows 'summary 'force)
3976       (gnus-set-mode-line 'summary)
3977       (gnus-summary-position-point)
3978       (message "")
3979       t)
3980      ;; We couldn't select this group.
3981      ((null did-select)
3982       (when (and (eq major-mode 'gnus-summary-mode)
3983                  (not (equal (current-buffer) kill-buffer)))
3984         (kill-buffer (current-buffer))
3985         (if (not quit-config)
3986             (progn
3987               ;; Update the info -- marks might need to be removed,
3988               ;; for instance.
3989               (gnus-summary-update-info)
3990               (set-buffer gnus-group-buffer)
3991               (gnus-group-jump-to-group group)
3992               (gnus-group-next-unread-group 1))
3993           (gnus-handle-ephemeral-exit quit-config)))
3994       (if (null (gnus-list-of-unread-articles group))
3995           (gnus-message 3 "Group %s contains no messages" group)
3996         (gnus-message 3 "Can't select group"))
3997       nil)
3998      ;; The user did a `C-g' while prompting for number of articles,
3999      ;; so we exit this group.
4000      ((eq did-select 'quit)
4001       (and (eq major-mode 'gnus-summary-mode)
4002            (not (equal (current-buffer) kill-buffer))
4003            (kill-buffer (current-buffer)))
4004       (when kill-buffer
4005         (gnus-kill-or-deaden-summary kill-buffer))
4006       (if (not quit-config)
4007           (progn
4008             (set-buffer gnus-group-buffer)
4009             (gnus-group-jump-to-group group)
4010             (gnus-configure-windows 'group 'force))
4011         (gnus-handle-ephemeral-exit quit-config))
4012       ;; Finally signal the quit.
4013       (signal 'quit nil))
4014      ;; The group was successfully selected.
4015      (t
4016       (gnus-set-global-variables)
4017       ;; Save the active value in effect when the group was entered.
4018       (setq gnus-newsgroup-active
4019             (gnus-copy-sequence
4020              (gnus-active gnus-newsgroup-name)))
4021       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4022       ;; You can change the summary buffer in some way with this hook.
4023       (gnus-run-hooks 'gnus-select-group-hook)
4024       (when (memq 'summary (gnus-update-format-specifications
4025                             nil 'summary 'summary-mode 'summary-dummy))
4026         ;; The format specification for the summary line was updated,
4027         ;; so we need to update the mark positions as well.
4028         (gnus-update-summary-mark-positions))
4029       ;; Do score processing.
4030       (when gnus-use-scoring
4031         (gnus-possibly-score-headers))
4032       ;; Check whether to fill in the gaps in the threads.
4033       (when gnus-build-sparse-threads
4034         (gnus-build-sparse-threads))
4035       ;; Find the initial limit.
4036       (if show-all
4037           (let ((gnus-newsgroup-dormant nil))
4038             (gnus-summary-initial-limit show-all))
4039         (gnus-summary-initial-limit show-all))
4040       ;; Generate the summary buffer.
4041       (unless no-display
4042         (gnus-summary-prepare))
4043       (when gnus-use-trees
4044         (gnus-tree-open group)
4045         (setq gnus-summary-highlight-line-function
4046               'gnus-tree-highlight-article))
4047       ;; If the summary buffer is empty, but there are some low-scored
4048       ;; articles or some excluded dormants, we include these in the
4049       ;; buffer.
4050       (when (and (zerop (buffer-size))
4051                  (not no-display))
4052         (cond (gnus-newsgroup-dormant
4053                (gnus-summary-limit-include-dormant))
4054               ((and gnus-newsgroup-scored show-all)
4055                (gnus-summary-limit-include-expunged t))))
4056       ;; Function `gnus-apply-kill-file' must be called in this hook.
4057       (gnus-run-hooks 'gnus-apply-kill-hook)
4058       (if (and (zerop (buffer-size))
4059                (not no-display))
4060           (progn
4061             ;; This newsgroup is empty.
4062             (gnus-summary-catchup-and-exit nil t)
4063             (gnus-message 6 "No unread news")
4064             (when kill-buffer
4065               (gnus-kill-or-deaden-summary kill-buffer))
4066             ;; Return nil from this function.
4067             nil)
4068         ;; Hide conversation thread subtrees.  We cannot do this in
4069         ;; gnus-summary-prepare-hook since kill processing may not
4070         ;; work with hidden articles.
4071         (gnus-summary-maybe-hide-threads)
4072         (gnus-configure-windows 'summary)
4073         (when kill-buffer
4074           (gnus-kill-or-deaden-summary kill-buffer))
4075         (gnus-summary-auto-select-subject)
4076         ;; Show first unread article if requested.
4077         (if (and (not no-article)
4078                  (not no-display)
4079                  gnus-newsgroup-unreads
4080                  gnus-auto-select-first)
4081             (progn
4082               (let ((art (gnus-summary-article-number)))
4083                 (unless (and (not gnus-plugged)
4084                              (or (memq art gnus-newsgroup-undownloaded)
4085                                  (memq art gnus-newsgroup-downloadable)))
4086                   (gnus-summary-goto-article art))))
4087           ;; Don't select any articles.
4088           (gnus-summary-position-point)
4089           (gnus-configure-windows 'summary 'force)
4090           (gnus-set-mode-line 'summary))
4091         (when (and gnus-auto-center-group
4092                    (get-buffer-window gnus-group-buffer t))
4093           ;; Gotta use windows, because recenter does weird stuff if
4094           ;; the current buffer ain't the displayed window.
4095           (let ((owin (selected-window)))
4096             (select-window (get-buffer-window gnus-group-buffer t))
4097             (when (gnus-group-goto-group group)
4098               (recenter))
4099             (select-window owin)))
4100         ;; Mark this buffer as "prepared".
4101         (setq gnus-newsgroup-prepared t)
4102         (gnus-run-hooks 'gnus-summary-prepared-hook)
4103         (unless (gnus-ephemeral-group-p group)
4104           (gnus-group-update-group group))
4105         t)))))
4106
4107 (defun gnus-summary-auto-select-subject ()
4108   "Select the subject line on initial group entry."
4109   (goto-char (point-min))
4110   (cond
4111    ((eq gnus-auto-select-subject 'best)
4112     (gnus-summary-best-unread-subject))
4113    ((eq gnus-auto-select-subject 'unread)
4114     (gnus-summary-first-unread-subject))
4115    ((eq gnus-auto-select-subject 'unseen)
4116     (gnus-summary-first-unseen-subject))
4117    ((eq gnus-auto-select-subject 'unseen-or-unread)
4118     (gnus-summary-first-unseen-or-unread-subject))
4119    ((eq gnus-auto-select-subject 'first)
4120     ;; Do nothing.
4121     )
4122    ((functionp gnus-auto-select-subject)
4123     (funcall gnus-auto-select-subject))))
4124
4125 (defun gnus-summary-prepare ()
4126   "Generate the summary buffer."
4127   (interactive)
4128   (let ((inhibit-read-only t))
4129     (erase-buffer)
4130     (setq gnus-newsgroup-data nil
4131           gnus-newsgroup-data-reverse nil)
4132     (gnus-run-hooks 'gnus-summary-generate-hook)
4133     ;; Generate the buffer, either with threads or without.
4134     (when gnus-newsgroup-headers
4135       (gnus-summary-prepare-threads
4136        (if gnus-show-threads
4137            (gnus-sort-gathered-threads
4138             (funcall gnus-summary-thread-gathering-function
4139                      (gnus-sort-threads
4140                       (gnus-cut-threads (gnus-make-threads)))))
4141          ;; Unthreaded display.
4142          (gnus-sort-articles gnus-newsgroup-headers))))
4143     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4144     ;; Call hooks for modifying summary buffer.
4145     (goto-char (point-min))
4146     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4147
4148 (defsubst gnus-general-simplify-subject (subject)
4149   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4150   (setq subject
4151         (cond
4152          ;; Truncate the subject.
4153          (gnus-simplify-subject-functions
4154           (gnus-map-function gnus-simplify-subject-functions subject))
4155          ((numberp gnus-summary-gather-subject-limit)
4156           (setq subject (gnus-simplify-subject-re subject))
4157           (if (> (length subject) gnus-summary-gather-subject-limit)
4158               (substring subject 0 gnus-summary-gather-subject-limit)
4159             subject))
4160          ;; Fuzzily simplify it.
4161          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4162           (gnus-simplify-subject-fuzzy subject))
4163          ;; Just remove the leading "Re:".
4164          (t
4165           (gnus-simplify-subject-re subject))))
4166
4167   (if (and gnus-summary-gather-exclude-subject
4168            (string-match gnus-summary-gather-exclude-subject subject))
4169       nil                         ; This article shouldn't be gathered
4170     subject))
4171
4172 (defun gnus-summary-simplify-subject-query ()
4173   "Query where the respool algorithm would put this article."
4174   (interactive)
4175   (gnus-summary-select-article)
4176   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4177
4178 (defun gnus-gather-threads-by-subject (threads)
4179   "Gather threads by looking at Subject headers."
4180   (if (not gnus-summary-make-false-root)
4181       threads
4182     (let ((hashtb (gnus-make-hashtable 1024))
4183           (prev threads)
4184           (result threads)
4185           subject hthread whole-subject)
4186       (while threads
4187         (setq subject (gnus-general-simplify-subject
4188                        (setq whole-subject (mail-header-subject
4189                                             (caar threads)))))
4190         (when subject
4191           (if (setq hthread (gnus-gethash subject hashtb))
4192               (progn
4193                 ;; We enter a dummy root into the thread, if we
4194                 ;; haven't done that already.
4195                 (unless (stringp (caar hthread))
4196                   (setcar hthread (list whole-subject (car hthread))))
4197                 ;; We add this new gathered thread to this gathered
4198                 ;; thread.
4199                 (setcdr (car hthread)
4200                         (nconc (cdar hthread) (list (car threads))))
4201                 ;; Remove it from the list of threads.
4202                 (setcdr prev (cdr threads))
4203                 (setq threads prev))
4204             ;; Enter this thread into the hash table.
4205             (gnus-sethash subject
4206                           (if gnus-summary-make-false-root-always
4207                               (progn
4208                                 ;; If you want a dummy root above all
4209                                 ;; threads...
4210                                 (setcar threads (list whole-subject
4211                                                       (car threads)))
4212                                 threads)
4213                             threads)
4214                           hashtb)))
4215         (setq prev threads)
4216         (setq threads (cdr threads)))
4217       result)))
4218
4219 (defun gnus-gather-threads-by-references (threads)
4220   "Gather threads by looking at References headers."
4221   (let ((idhashtb (gnus-make-hashtable 1024))
4222         (thhashtb (gnus-make-hashtable 1024))
4223         (prev threads)
4224         (result threads)
4225         ids references id gthread gid entered ref)
4226     (while threads
4227       (when (setq references (mail-header-references (caar threads)))
4228         (setq id (mail-header-id (caar threads))
4229               ids (inline (gnus-split-references references))
4230               entered nil)
4231         (while (setq ref (pop ids))
4232           (setq ids (delete ref ids))
4233           (if (not (setq gid (gnus-gethash ref idhashtb)))
4234               (progn
4235                 (gnus-sethash ref id idhashtb)
4236                 (gnus-sethash id threads thhashtb))
4237             (setq gthread (gnus-gethash gid thhashtb))
4238             (unless entered
4239               ;; We enter a dummy root into the thread, if we
4240               ;; haven't done that already.
4241               (unless (stringp (caar gthread))
4242                 (setcar gthread (list (mail-header-subject (caar gthread))
4243                                       (car gthread))))
4244               ;; We add this new gathered thread to this gathered
4245               ;; thread.
4246               (setcdr (car gthread)
4247                       (nconc (cdar gthread) (list (car threads)))))
4248             ;; Add it into the thread hash table.
4249             (gnus-sethash id gthread thhashtb)
4250             (setq entered t)
4251             ;; Remove it from the list of threads.
4252             (setcdr prev (cdr threads))
4253             (setq threads prev))))
4254       (setq prev threads)
4255       (setq threads (cdr threads)))
4256     result))
4257
4258 (defun gnus-sort-gathered-threads (threads)
4259   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4260   (let ((result threads))
4261     (while threads
4262       (when (stringp (caar threads))
4263         (setcdr (car threads)
4264                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4265       (setq threads (cdr threads)))
4266     result))
4267
4268 (defun gnus-thread-loop-p (root thread)
4269   "Say whether ROOT is in THREAD."
4270   (let ((stack (list thread))
4271         (infloop 0)
4272         th)
4273     (while (setq thread (pop stack))
4274       (setq th (cdr thread))
4275       (while (and th
4276                   (not (eq (caar th) root)))
4277         (pop th))
4278       (if th
4279           ;; We have found a loop.
4280           (let (ref-dep)
4281             (setcdr thread (delq (car th) (cdr thread)))
4282             (if (boundp (setq ref-dep (intern "none"
4283                                               gnus-newsgroup-dependencies)))
4284                 (setcdr (symbol-value ref-dep)
4285                         (nconc (cdr (symbol-value ref-dep))
4286                                (list (car th))))
4287               (set ref-dep (list nil (car th))))
4288             (setq infloop 1
4289                   stack nil))
4290         ;; Push all the subthreads onto the stack.
4291         (push (cdr thread) stack)))
4292     infloop))
4293
4294 (defun gnus-make-threads ()
4295   "Go through the dependency hashtb and find the roots.  Return all threads."
4296   (let (threads)
4297     (while (catch 'infloop
4298              (mapatoms
4299               (lambda (refs)
4300                 ;; Deal with self-referencing References loops.
4301                 (when (and (car (symbol-value refs))
4302                            (not (zerop
4303                                  (apply
4304                                   '+
4305                                   (mapcar
4306                                    (lambda (thread)
4307                                      (gnus-thread-loop-p
4308                                       (car (symbol-value refs)) thread))
4309                                    (cdr (symbol-value refs)))))))
4310                   (setq threads nil)
4311                   (throw 'infloop t))
4312                 (unless (car (symbol-value refs))
4313                   ;; These threads do not refer back to any other
4314                   ;; articles, so they're roots.
4315                   (setq threads (append (cdr (symbol-value refs)) threads))))
4316               gnus-newsgroup-dependencies)))
4317     threads))
4318
4319 ;; Build the thread tree.
4320 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4321   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4322
4323 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4324 if it was already present.
4325
4326 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4327 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4328 Message-IDs will be renamed to a unique Message-ID before being
4329 entered.
4330
4331 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4332   (let* ((id (mail-header-id header))
4333          (id-dep (and id (intern id dependencies)))
4334          parent-id ref ref-dep ref-header replaced)
4335     ;; Enter this `header' in the `dependencies' table.
4336     (cond
4337      ((not id-dep)
4338       (setq header nil))
4339      ;; The first two cases do the normal part: enter a new `header'
4340      ;; in the `dependencies' table.
4341      ((not (boundp id-dep))
4342       (set id-dep (list header)))
4343      ((null (car (symbol-value id-dep)))
4344       (setcar (symbol-value id-dep) header))
4345
4346      ;; From here the `header' was already present in the
4347      ;; `dependencies' table.
4348      (force-new
4349       ;; Overrides an existing entry;
4350       ;; just set the header part of the entry.
4351       (setcar (symbol-value id-dep) header)
4352       (setq replaced t))
4353
4354      ;; Renames the existing `header' to a unique Message-ID.
4355      ((not gnus-summary-ignore-duplicates)
4356       ;; An article with this Message-ID has already been seen.
4357       ;; We rename the Message-ID.
4358       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4359            (list header))
4360       (mail-header-set-id header id))
4361
4362      ;; The last case ignores an existing entry, except it adds any
4363      ;; additional Xrefs (in case the two articles came from different
4364      ;; servers.
4365      ;; Also sets `header' to `nil' meaning that the `dependencies'
4366      ;; table was *not* modified.
4367      (t
4368       (mail-header-set-xref
4369        (car (symbol-value id-dep))
4370        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4371                    "")
4372                (or (mail-header-xref header) "")))
4373       (setq header nil)))
4374
4375     (when (and header (not replaced))
4376       ;; First check that we are not creating a References loop.
4377       (setq parent-id (gnus-parent-id (mail-header-references header)))
4378       (setq ref parent-id)
4379       (while (and ref
4380                   (setq ref-dep (intern-soft ref dependencies))
4381                   (boundp ref-dep)
4382                   (setq ref-header (car (symbol-value ref-dep))))
4383         (if (string= id ref)
4384             ;; Yuk!  This is a reference loop.  Make the article be a
4385             ;; root article.
4386             (progn
4387               (mail-header-set-references (car (symbol-value id-dep)) "none")
4388               (setq ref nil)
4389               (setq parent-id nil))
4390           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4391       (setq ref-dep (intern (or parent-id "none") dependencies))
4392       (if (boundp ref-dep)
4393           (setcdr (symbol-value ref-dep)
4394                   (nconc (cdr (symbol-value ref-dep))
4395                          (list (symbol-value id-dep))))
4396         (set ref-dep (list nil (symbol-value id-dep)))))
4397     header))
4398
4399 (defun gnus-extract-message-id-from-in-reply-to (string)
4400   (if (string-match "<[^>]+>" string)
4401       (substring string (match-beginning 0) (match-end 0))
4402     nil))
4403
4404 (defun gnus-build-sparse-threads ()
4405   (let ((headers gnus-newsgroup-headers)
4406         (mail-parse-charset gnus-newsgroup-charset)
4407         (gnus-summary-ignore-duplicates t)
4408         header references generation relations
4409         subject child end new-child date)
4410     ;; First we create an alist of generations/relations, where
4411     ;; generations is how much we trust the relation, and the relation
4412     ;; is parent/child.
4413     (gnus-message 7 "Making sparse threads...")
4414     (save-excursion
4415       (nnheader-set-temp-buffer " *gnus sparse threads*")
4416       (while (setq header (pop headers))
4417         (when (and (setq references (mail-header-references header))
4418                    (not (string= references "")))
4419           (insert references)
4420           (setq child (mail-header-id header)
4421                 subject (mail-header-subject header)
4422                 date (mail-header-date header)
4423                 generation 0)
4424           (while (search-backward ">" nil t)
4425             (setq end (1+ (point)))
4426             (when (search-backward "<" nil t)
4427               (setq new-child (buffer-substring (point) end))
4428               (push (list (incf generation)
4429                           child (setq child new-child)
4430                           subject date)
4431                     relations)))
4432           (when child
4433             (push (list (1+ generation) child nil subject) relations))
4434           (erase-buffer)))
4435       (kill-buffer (current-buffer)))
4436     ;; Sort over trustworthiness.
4437     (dolist (relation (sort relations 'car-less-than-car))
4438       (when (gnus-dependencies-add-header
4439              (make-full-mail-header
4440               gnus-reffed-article-number
4441               (nth 3 relation) "" (or (nth 4 relation) "")
4442               (nth 1 relation)
4443               (or (nth 2 relation) "") 0 0 "")
4444              gnus-newsgroup-dependencies nil)
4445         (push gnus-reffed-article-number gnus-newsgroup-limit)
4446         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4447         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4448               gnus-newsgroup-reads)
4449         (decf gnus-reffed-article-number)))
4450     (gnus-message 7 "Making sparse threads...done")))
4451
4452 (defun gnus-build-old-threads ()
4453   ;; Look at all the articles that refer back to old articles, and
4454   ;; fetch the headers for the articles that aren't there.  This will
4455   ;; build complete threads - if the roots haven't been expired by the
4456   ;; server, that is.
4457   (let ((mail-parse-charset gnus-newsgroup-charset)
4458         id heads)
4459     (mapatoms
4460      (lambda (refs)
4461        (when (not (car (symbol-value refs)))
4462          (setq heads (cdr (symbol-value refs)))
4463          (while heads
4464            (if (memq (mail-header-number (caar heads))
4465                      gnus-newsgroup-dormant)
4466                (setq heads (cdr heads))
4467              (setq id (symbol-name refs))
4468              (while (and (setq id (gnus-build-get-header id))
4469                          (not (car (gnus-id-to-thread id)))))
4470              (setq heads nil)))))
4471      gnus-newsgroup-dependencies)))
4472
4473 (defsubst gnus-remove-odd-characters (string)
4474   "Translate STRING into something that doesn't contain weird characters."
4475   (mm-subst-char-in-string
4476    ?\r ?\-
4477    (mm-subst-char-in-string ?\n ?\- string t) t))
4478
4479 ;; This function has to be called with point after the article number
4480 ;; on the beginning of the line.
4481 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4482   (let ((eol (point-at-eol))
4483         (buffer (current-buffer))
4484         header references in-reply-to)
4485
4486     ;; overview: [num subject from date id refs chars lines misc]
4487     (unwind-protect
4488         (let (x)
4489           (narrow-to-region (point) eol)
4490           (unless (eobp)
4491             (forward-char))
4492
4493           (setq header
4494                 (make-full-mail-header
4495                  number                 ; number
4496                  (condition-case ()     ; subject
4497                      (gnus-remove-odd-characters
4498                       (funcall gnus-decode-encoded-word-function
4499                                (setq x (nnheader-nov-field))))
4500                    (error x))
4501                  (condition-case ()     ; from
4502                      (gnus-remove-odd-characters
4503                       (funcall gnus-decode-encoded-address-function
4504                                (setq x (nnheader-nov-field))))
4505                    (error x))
4506                  (nnheader-nov-field)   ; date
4507                  (nnheader-nov-read-message-id number)  ; id
4508                  (setq references (nnheader-nov-field)) ; refs
4509                  (nnheader-nov-read-integer) ; chars
4510                  (nnheader-nov-read-integer) ; lines
4511                  (unless (eobp)
4512                    (if (looking-at "Xref: ")
4513                        (goto-char (match-end 0)))
4514                    (nnheader-nov-field)) ; Xref
4515                  (nnheader-nov-parse-extra)))) ; extra
4516
4517       (widen))
4518
4519     (when (and (string= references "")
4520                (setq in-reply-to (mail-header-extra header))
4521                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4522       (mail-header-set-references
4523        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4524
4525     (when gnus-alter-header-function
4526       (funcall gnus-alter-header-function header))
4527     (gnus-dependencies-add-header header dependencies force-new)))
4528
4529 (defun gnus-build-get-header (id)
4530   "Look through the buffer of NOV lines and find the header to ID.
4531 Enter this line into the dependencies hash table, and return
4532 the id of the parent article (if any)."
4533   (let ((deps gnus-newsgroup-dependencies)
4534         found header)
4535     (prog1
4536         (with-current-buffer nntp-server-buffer
4537           (let ((case-fold-search nil))
4538             (goto-char (point-min))
4539             (while (and (not found)
4540                         (search-forward id nil t))
4541               (beginning-of-line)
4542               (setq found (looking-at
4543                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4544                                    (regexp-quote id))))
4545               (or found (beginning-of-line 2)))
4546             (when found
4547               (beginning-of-line)
4548               (and
4549                (setq header (gnus-nov-parse-line
4550                              (read (current-buffer)) deps))
4551                (gnus-parent-id (mail-header-references header))))))
4552       (when header
4553         (let ((number (mail-header-number header)))
4554           (push number gnus-newsgroup-limit)
4555           (push header gnus-newsgroup-headers)
4556           (if (memq number gnus-newsgroup-unselected)
4557               (progn
4558                 (setq gnus-newsgroup-unreads
4559                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4560                                                number))
4561                 (setq gnus-newsgroup-unselected
4562                       (delq number gnus-newsgroup-unselected)))
4563             (push number gnus-newsgroup-ancient)))))))
4564
4565 (defun gnus-build-all-threads ()
4566   "Read all the headers."
4567   (let ((gnus-summary-ignore-duplicates t)
4568         (mail-parse-charset gnus-newsgroup-charset)
4569         (dependencies gnus-newsgroup-dependencies)
4570         header article)
4571     (with-current-buffer nntp-server-buffer
4572       (let ((case-fold-search nil))
4573         (goto-char (point-min))
4574         (while (not (eobp))
4575           (ignore-errors
4576             (setq article (read (current-buffer))
4577                   header (gnus-nov-parse-line article dependencies t)))
4578           (when header
4579             (with-current-buffer gnus-summary-buffer
4580               (push header gnus-newsgroup-headers)
4581               (if (memq (setq article (mail-header-number header))
4582                         gnus-newsgroup-unselected)
4583                   (progn
4584                     (setq gnus-newsgroup-unreads
4585                           (gnus-add-to-sorted-list
4586                            gnus-newsgroup-unreads article))
4587                     (setq gnus-newsgroup-unselected
4588                           (delq article gnus-newsgroup-unselected)))
4589                 (push article gnus-newsgroup-ancient)))
4590             (forward-line 1)))))))
4591
4592 (defun gnus-summary-update-article-line (article header)
4593   "Update the line for ARTICLE using HEADER."
4594   (let* ((id (mail-header-id header))
4595          (thread (gnus-id-to-thread id)))
4596     (unless thread
4597       (error "Article in no thread"))
4598     ;; Update the thread.
4599     (setcar thread header)
4600     (gnus-summary-goto-subject article)
4601     (let* ((datal (gnus-data-find-list article))
4602            (data (car datal))
4603            (inhibit-read-only t)
4604            (level (gnus-summary-thread-level)))
4605       (gnus-delete-line)
4606       (let ((inserted (- (point)
4607                          (progn
4608                            (gnus-summary-insert-line
4609                             header level nil
4610                             (memq article gnus-newsgroup-undownloaded)
4611                             (gnus-article-mark article)
4612                             (memq article gnus-newsgroup-replied)
4613                             (memq article gnus-newsgroup-expirable)
4614                             ;; Only insert the Subject string when it's different
4615                             ;; from the previous Subject string.
4616                             (if (and
4617                                  gnus-show-threads
4618                                  (gnus-subject-equal
4619                                   (condition-case ()
4620                                       (mail-header-subject
4621                                        (gnus-data-header
4622                                         (cadr
4623                                          (gnus-data-find-list
4624                                           article
4625                                           (gnus-data-list t)))))
4626                                     ;; Error on the side of excessive subjects.
4627                                     (error ""))
4628                                   (mail-header-subject header)))
4629                                 ""
4630                               (mail-header-subject header))
4631                             nil (cdr (assq article gnus-newsgroup-scored))
4632                             (memq article gnus-newsgroup-processable))
4633                            (point)))))
4634         (when (cdr datal)
4635           (gnus-data-update-list
4636            (cdr datal)
4637            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4638
4639 (defun gnus-summary-update-article (article &optional iheader)
4640   "Update ARTICLE in the summary buffer."
4641   (set-buffer gnus-summary-buffer)
4642   (let* ((header (gnus-summary-article-header article))
4643          (id (mail-header-id header))
4644          (data (gnus-data-find article))
4645          (thread (gnus-id-to-thread id))
4646          (references (mail-header-references header))
4647          (parent
4648           (gnus-id-to-thread
4649            (or (gnus-parent-id
4650                 (when (and references
4651                            (not (equal "" references)))
4652                   references))
4653                "none")))
4654          (inhibit-read-only t)
4655          (old (car thread)))
4656     (when thread
4657       (unless iheader
4658         (setcar thread nil)
4659         (when parent
4660           (delq thread parent)))
4661       (if (gnus-summary-insert-subject id header)
4662           ;; Set the (possibly) new article number in the data structure.
4663           (gnus-data-set-number data (gnus-id-to-article id))
4664         (setcar thread old)
4665         nil))))
4666
4667 (defun gnus-rebuild-thread (id &optional line)
4668   "Rebuild the thread containing ID.
4669 If LINE, insert the rebuilt thread starting on line LINE."
4670   (let ((inhibit-read-only t)
4671         old-pos current thread data)
4672     (if (not gnus-show-threads)
4673         (setq thread (list (car (gnus-id-to-thread id))))
4674       ;; Get the thread this article is part of.
4675       (setq thread (gnus-remove-thread id)))
4676     (setq old-pos (point-at-bol))
4677     (setq current (save-excursion
4678                     (and (re-search-backward "[\r\n]" nil t)
4679                          (gnus-summary-article-number))))
4680     ;; If this is a gathered thread, we have to go some re-gathering.
4681     (when (stringp (car thread))
4682       (let ((subject (car thread))
4683             roots thr)
4684         (setq thread (cdr thread))
4685         (while thread
4686           (unless (memq (setq thr (gnus-id-to-thread
4687                                    (gnus-root-id
4688                                     (mail-header-id (caar thread)))))
4689                         roots)
4690             (push thr roots))
4691           (setq thread (cdr thread)))
4692         ;; We now have all (unique) roots.
4693         (if (= (length roots) 1)
4694             ;; All the loose roots are now one solid root.
4695             (setq thread (car roots))
4696           (setq thread (cons subject (gnus-sort-threads roots))))))
4697     (let (threads)
4698       ;; We then insert this thread into the summary buffer.
4699       (when line
4700         (goto-char (point-min))
4701         (forward-line (1- line)))
4702       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4703         (if gnus-show-threads
4704             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4705           (gnus-summary-prepare-unthreaded thread))
4706         (setq data (nreverse gnus-newsgroup-data))
4707         (setq threads gnus-newsgroup-threads))
4708       ;; We splice the new data into the data structure.
4709       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4710       ;;!!! then we want to insert at the beginning of the buffer.
4711       ;;!!! That happens to be true with Gnus now, but that may
4712       ;;!!! change in the future.  Perhaps.
4713       (gnus-data-enter-list
4714        (if line nil current) data (- (point) old-pos))
4715       (setq gnus-newsgroup-threads
4716             (nconc threads gnus-newsgroup-threads))
4717       (gnus-data-compute-positions))))
4718
4719 (defun gnus-number-to-header (number)
4720   "Return the header for article NUMBER."
4721   (let ((headers gnus-newsgroup-headers))
4722     (while (and headers
4723                 (not (= number (mail-header-number (car headers)))))
4724       (pop headers))
4725     (when headers
4726       (car headers))))
4727
4728 (defun gnus-parent-headers (in-headers &optional generation)
4729   "Return the headers of the GENERATIONeth parent of HEADERS."
4730   (unless generation
4731     (setq generation 1))
4732   (let ((parent t)
4733         (headers in-headers)
4734         references)
4735     (while (and parent
4736                 (not (zerop generation))
4737                 (setq references (mail-header-references headers)))
4738       (setq headers (if (and references
4739                              (setq parent (gnus-parent-id references)))
4740                         (car (gnus-id-to-thread parent))
4741                       nil))
4742       (decf generation))
4743     (and (not (eq headers in-headers))
4744          headers)))
4745
4746 (defun gnus-id-to-thread (id)
4747   "Return the (sub-)thread where ID appears."
4748   (gnus-gethash id gnus-newsgroup-dependencies))
4749
4750 (defun gnus-id-to-article (id)
4751   "Return the article number of ID."
4752   (let ((thread (gnus-id-to-thread id)))
4753     (when (and thread
4754                (car thread))
4755       (mail-header-number (car thread)))))
4756
4757 (defun gnus-id-to-header (id)
4758   "Return the article headers of ID."
4759   (car (gnus-id-to-thread id)))
4760
4761 (defun gnus-article-displayed-root-p (article)
4762   "Say whether ARTICLE is a root(ish) article."
4763   (let ((level (gnus-summary-thread-level article))
4764         (refs (mail-header-references  (gnus-summary-article-header article)))
4765         particle)
4766     (cond
4767      ((null level) nil)
4768      ((zerop level) t)
4769      ((null refs) t)
4770      ((null (gnus-parent-id refs)) t)
4771      ((and (= 1 level)
4772            (null (setq particle (gnus-id-to-article
4773                                  (gnus-parent-id refs))))
4774            (null (gnus-summary-thread-level particle)))))))
4775
4776 (defun gnus-root-id (id)
4777   "Return the id of the root of the thread where ID appears."
4778   (let (last-id prev)
4779     (while (and id (setq prev (car (gnus-id-to-thread id))))
4780       (setq last-id id
4781             id (gnus-parent-id (mail-header-references prev))))
4782     last-id))
4783
4784 (defun gnus-articles-in-thread (thread)
4785   "Return the list of articles in THREAD."
4786   (cons (mail-header-number (car thread))
4787         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4788
4789 (defun gnus-remove-thread (id &optional dont-remove)
4790   "Remove the thread that has ID in it."
4791   (let (headers thread last-id)
4792     ;; First go up in this thread until we find the root.
4793     (setq last-id (gnus-root-id id)
4794           headers (message-flatten-list (gnus-id-to-thread last-id)))
4795     ;; We have now found the real root of this thread.  It might have
4796     ;; been gathered into some loose thread, so we have to search
4797     ;; through the threads to find the thread we wanted.
4798     (let ((threads gnus-newsgroup-threads)
4799           sub)
4800       (while threads
4801         (setq sub (car threads))
4802         (if (stringp (car sub))
4803             ;; This is a gathered thread, so we look at the roots
4804             ;; below it to find whether this article is in this
4805             ;; gathered root.
4806             (progn
4807               (setq sub (cdr sub))
4808               (while sub
4809                 (when (member (caar sub) headers)
4810                   (setq thread (car threads)
4811                         threads nil
4812                         sub nil))
4813                 (setq sub (cdr sub))))
4814           ;; It's an ordinary thread, so we check it.
4815           (when (eq (car sub) (car headers))
4816             (setq thread sub
4817                   threads nil)))
4818         (setq threads (cdr threads)))
4819       ;; If this article is in no thread, then it's a root.
4820       (if thread
4821           (unless dont-remove
4822             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4823         (setq thread (gnus-id-to-thread last-id)))
4824       (when thread
4825         (prog1
4826             thread                      ; We return this thread.
4827           (unless dont-remove
4828             (if (stringp (car thread))
4829                 (progn
4830                   ;; If we use dummy roots, then we have to remove the
4831                   ;; dummy root as well.
4832                   (when (eq gnus-summary-make-false-root 'dummy)
4833                     ;; We go to the dummy root by going to
4834                     ;; the first sub-"thread", and then one line up.
4835                     (gnus-summary-goto-article
4836                      (mail-header-number (caadr thread)))
4837                     (forward-line -1)
4838                     (gnus-delete-line)
4839                     (gnus-data-compute-positions))
4840                   (setq thread (cdr thread))
4841                   (while thread
4842                     (gnus-remove-thread-1 (car thread))
4843                     (setq thread (cdr thread))))
4844               (gnus-remove-thread-1 thread))))))))
4845
4846 (defun gnus-remove-thread-1 (thread)
4847   "Remove the thread THREAD recursively."
4848   (let ((number (mail-header-number (pop thread)))
4849         d)
4850     (setq thread (reverse thread))
4851     (while thread
4852       (gnus-remove-thread-1 (pop thread)))
4853     (when (setq d (gnus-data-find number))
4854       (goto-char (gnus-data-pos d))
4855       (gnus-summary-show-thread)
4856       (gnus-data-remove
4857        number
4858        (- (point-at-bol)
4859           (prog1
4860               (1+ (point-at-eol))
4861             (gnus-delete-line)))))))
4862
4863 (defun gnus-sort-threads-recursive (threads func)
4864   (sort (mapcar (lambda (thread)
4865                   (cons (car thread)
4866                         (and (cdr thread)
4867                              (gnus-sort-threads-recursive (cdr thread) func))))
4868                 threads) func))
4869
4870 (defun gnus-sort-threads-loop (threads func)
4871   (let* ((superthread (cons nil threads))
4872          (stack (list (cons superthread threads)))
4873          remaining-threads thread)
4874     (while stack
4875       (setq remaining-threads (cdr (car stack)))
4876       (if remaining-threads
4877           (progn (setq thread (car remaining-threads))
4878                  (setcdr (car stack) (cdr remaining-threads))
4879                  (if (cdr thread)
4880                      (push (cons thread (cdr thread)) stack)))
4881         (setq thread (caar stack))
4882         (setcdr thread (sort (cdr thread) func))
4883         (pop stack)))
4884     (cdr superthread)))
4885
4886 (defun gnus-sort-threads (threads)
4887   "Sort THREADS."
4888   (if (not gnus-thread-sort-functions)
4889       threads
4890     (gnus-message 8 "Sorting threads...")
4891     (prog1
4892         (condition-case nil
4893             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4894               (gnus-sort-threads-recursive
4895                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4896           ;; Even after binding max-lisp-eval-depth, the recursive
4897           ;; sorter might fail for very long threads.  In that case,
4898           ;; try using a (less well-tested) non-recursive sorter.
4899           (error (gnus-message 9 "Sorting threads with loop...")
4900                  (gnus-sort-threads-loop
4901                   threads (gnus-make-sort-function
4902                            gnus-thread-sort-functions))))
4903       (gnus-message 8 "Sorting threads...done"))))
4904
4905 (defun gnus-sort-articles (articles)
4906   "Sort ARTICLES."
4907   (when gnus-article-sort-functions
4908     (gnus-message 7 "Sorting articles...")
4909     (prog1
4910         (setq gnus-newsgroup-headers
4911               (sort articles (gnus-make-sort-function
4912                               gnus-article-sort-functions)))
4913       (gnus-message 7 "Sorting articles...done"))))
4914
4915 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4916 (defmacro gnus-thread-header (thread)
4917   "Return header of first article in THREAD.
4918 Note that THREAD must never, ever be anything else than a variable -
4919 using some other form will lead to serious barfage."
4920   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4921   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4922   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4923         (vector thread) 2))
4924
4925 (defsubst gnus-article-sort-by-number (h1 h2)
4926   "Sort articles by article number."
4927   (< (mail-header-number h1)
4928      (mail-header-number h2)))
4929
4930 (defun gnus-thread-sort-by-number (h1 h2)
4931   "Sort threads by root article number."
4932   (gnus-article-sort-by-number
4933    (gnus-thread-header h1) (gnus-thread-header h2)))
4934
4935 (defsubst gnus-article-sort-by-random (h1 h2)
4936   "Sort articles randomly."
4937   (zerop (random 2)))
4938
4939 (defun gnus-thread-sort-by-random (h1 h2)
4940   "Sort threads randomly."
4941   (gnus-article-sort-by-random
4942    (gnus-thread-header h1) (gnus-thread-header h2)))
4943
4944 (defsubst gnus-article-sort-by-lines (h1 h2)
4945   "Sort articles by article Lines header."
4946   (< (mail-header-lines h1)
4947      (mail-header-lines h2)))
4948
4949 (defun gnus-thread-sort-by-lines (h1 h2)
4950   "Sort threads by root article Lines header."
4951   (gnus-article-sort-by-lines
4952    (gnus-thread-header h1) (gnus-thread-header h2)))
4953
4954 (defsubst gnus-article-sort-by-chars (h1 h2)
4955   "Sort articles by octet length."
4956   (< (mail-header-chars h1)
4957      (mail-header-chars h2)))
4958
4959 (defun gnus-thread-sort-by-chars (h1 h2)
4960   "Sort threads by root article octet length."
4961   (gnus-article-sort-by-chars
4962    (gnus-thread-header h1) (gnus-thread-header h2)))
4963
4964 (defsubst gnus-article-sort-by-author (h1 h2)
4965   "Sort articles by root author."
4966   (gnus-string<
4967    (let ((extract (funcall
4968                    gnus-extract-address-components
4969                    (mail-header-from h1))))
4970      (or (car extract) (cadr extract) ""))
4971    (let ((extract (funcall
4972                    gnus-extract-address-components
4973                    (mail-header-from h2))))
4974      (or (car extract) (cadr extract) ""))))
4975
4976 (defun gnus-thread-sort-by-author (h1 h2)
4977   "Sort threads by root author."
4978   (gnus-article-sort-by-author
4979    (gnus-thread-header h1)  (gnus-thread-header h2)))
4980
4981 (defsubst gnus-article-sort-by-recipient (h1 h2)
4982   "Sort articles by recipient."
4983   (gnus-string<
4984    (let ((extract (funcall
4985                    gnus-extract-address-components
4986                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4987      (or (car extract) (cadr extract)))
4988    (let ((extract (funcall
4989                    gnus-extract-address-components
4990                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4991      (or (car extract) (cadr extract)))))
4992
4993 (defun gnus-thread-sort-by-recipient (h1 h2)
4994   "Sort threads by root recipient."
4995   (gnus-article-sort-by-recipient
4996    (gnus-thread-header h1) (gnus-thread-header h2)))
4997
4998 (defsubst gnus-article-sort-by-subject (h1 h2)
4999   "Sort articles by root subject."
5000   (gnus-string<
5001    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5002    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5003
5004 (defun gnus-thread-sort-by-subject (h1 h2)
5005   "Sort threads by root subject."
5006   (gnus-article-sort-by-subject
5007    (gnus-thread-header h1) (gnus-thread-header h2)))
5008
5009 (defsubst gnus-article-sort-by-date (h1 h2)
5010   "Sort articles by root article date."
5011   (time-less-p
5012    (gnus-date-get-time (mail-header-date h1))
5013    (gnus-date-get-time (mail-header-date h2))))
5014
5015 (defun gnus-thread-sort-by-date (h1 h2)
5016   "Sort threads by root article date."
5017   (gnus-article-sort-by-date
5018    (gnus-thread-header h1) (gnus-thread-header h2)))
5019
5020 (defsubst gnus-article-sort-by-score (h1 h2)
5021   "Sort articles by root article score.
5022 Unscored articles will be counted as having a score of zero."
5023   (> (or (cdr (assq (mail-header-number h1)
5024                     gnus-newsgroup-scored))
5025          gnus-summary-default-score 0)
5026      (or (cdr (assq (mail-header-number h2)
5027                     gnus-newsgroup-scored))
5028          gnus-summary-default-score 0)))
5029
5030 (defun gnus-thread-sort-by-score (h1 h2)
5031   "Sort threads by root article score."
5032   (gnus-article-sort-by-score
5033    (gnus-thread-header h1) (gnus-thread-header h2)))
5034
5035 (defun gnus-thread-sort-by-total-score (h1 h2)
5036   "Sort threads by the sum of all scores in the thread.
5037 Unscored articles will be counted as having a score of zero."
5038   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5039
5040 (defun gnus-thread-total-score (thread)
5041   ;; This function find the total score of THREAD.
5042   (cond
5043    ((null thread)
5044     0)
5045    ((consp thread)
5046     (if (stringp (car thread))
5047         (apply gnus-thread-score-function 0
5048                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5049       (gnus-thread-total-score-1 thread)))
5050    (t
5051     (gnus-thread-total-score-1 (list thread)))))
5052
5053 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5054   "Sort articles by number."
5055   (gnus-article-sort-by-number h1 h2))
5056
5057 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5058   "Sort threads such that the thread with the most recently arrived article comes first."
5059   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5060
5061 (defun gnus-thread-highest-number (thread)
5062   "Return the highest article number in THREAD."
5063   (apply 'max (mapcar (lambda (header)
5064                         (mail-header-number header))
5065                       (message-flatten-list thread))))
5066
5067 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5068   "Sort articles by number."
5069   (gnus-article-sort-by-date h1 h2))
5070
5071 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5072   "Sort threads such that the thread with the most recently dated article comes first."
5073   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5074
5075 ; Since this is called not only to sort the top-level threads, but
5076 ; also in recursive sorts to order the articles within a thread, each
5077 ; article will be processed many times.  Thus it speeds things up
5078 ; quite a bit to use gnus-date-get-time, which caches the time value.
5079 (defun gnus-thread-latest-date (thread)
5080   "Return the highest article date in THREAD."
5081   (apply 'max
5082          (mapcar (lambda (header) (gnus-float-time
5083                                    (gnus-date-get-time
5084                                     (mail-header-date header))))
5085                  (message-flatten-list thread))))
5086
5087 (defun gnus-thread-total-score-1 (root)
5088   ;; This function find the total score of the thread below ROOT.
5089   (setq root (car root))
5090   (apply gnus-thread-score-function
5091          (or (append
5092               (mapcar 'gnus-thread-total-score
5093                       (cdr (gnus-id-to-thread (mail-header-id root))))
5094               (when (> (mail-header-number root) 0)
5095                 (list (or (cdr (assq (mail-header-number root)
5096                                      gnus-newsgroup-scored))
5097                           gnus-summary-default-score 0))))
5098              (list gnus-summary-default-score)
5099              '(0))))
5100
5101 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5102 (defvar gnus-tmp-prev-subject nil)
5103 (defvar gnus-tmp-false-parent nil)
5104 (defvar gnus-tmp-root-expunged nil)
5105 (defvar gnus-tmp-dummy-line nil)
5106
5107 (defun gnus-extra-header (type &optional header)
5108   "Return the extra header of TYPE."
5109   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5110       ""))
5111
5112 (defvar gnus-tmp-thread-tree-header-string "")
5113
5114 (defcustom gnus-sum-thread-tree-root "> "
5115   "With %B spec, used for the root of a thread.
5116 If nil, use subject instead."
5117   :version "22.1"
5118   :type '(radio (const :format "%v  " nil) string)
5119   :group 'gnus-thread)
5120
5121 (defcustom gnus-sum-thread-tree-false-root "> "
5122   "With %B spec, used for a false root of a thread.
5123 If nil, use subject instead."
5124   :version "22.1"
5125   :type '(radio (const :format "%v  " nil) string)
5126   :group 'gnus-thread)
5127
5128 (defcustom gnus-sum-thread-tree-single-indent ""
5129   "With %B spec, used for a thread with just one message.
5130 If nil, use subject instead."
5131   :version "22.1"
5132   :type '(radio (const :format "%v  " nil) string)
5133   :group 'gnus-thread)
5134
5135 (defcustom gnus-sum-thread-tree-vertical "| "
5136   "With %B spec, used for drawing a vertical line."
5137   :version "22.1"
5138   :type 'string
5139   :group 'gnus-thread)
5140
5141 (defcustom gnus-sum-thread-tree-indent "  "
5142   "With %B spec, used for indenting."
5143   :version "22.1"
5144   :type 'string
5145   :group 'gnus-thread)
5146
5147 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5148   "With %B spec, used for a leaf with brothers."
5149   :version "22.1"
5150   :type 'string
5151   :group 'gnus-thread)
5152
5153 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5154   "With %B spec, used for a leaf without brothers."
5155   :version "22.1"
5156   :type 'string
5157   :group 'gnus-thread)
5158
5159 (defcustom gnus-summary-display-while-building nil
5160   "If non-nil, show and update the summary buffer as it's being built.
5161 If the value is t, update the buffer after every line is inserted.  If
5162 the value is an integer (N), update the display every N lines."
5163   :version "22.1"
5164   :group 'gnus-thread
5165   :type '(choice (const :tag "off" nil)
5166                  number
5167                  (const :tag "frequently" t)))
5168
5169 (defun gnus-summary-prepare-threads (threads)
5170   "Prepare summary buffer from THREADS and indentation LEVEL.
5171 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5172 or a straight list of headers."
5173   (gnus-message 7 "Generating summary...")
5174
5175   (setq gnus-newsgroup-threads threads)
5176   (beginning-of-line)
5177
5178   (let ((gnus-tmp-level 0)
5179         (default-score (or gnus-summary-default-score 0))
5180         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5181         (building-line-count gnus-summary-display-while-building)
5182         (building-count (integerp gnus-summary-display-while-building))
5183         thread number subject stack state gnus-tmp-gathered beg-match
5184         new-roots gnus-tmp-new-adopts thread-end simp-subject
5185         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5186         gnus-tmp-replied gnus-tmp-subject-or-nil
5187         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5188         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5189         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5190         tree-stack)
5191
5192     (setq gnus-tmp-prev-subject nil
5193           gnus-tmp-thread-tree-header-string "")
5194
5195     (if (vectorp (car threads))
5196         ;; If this is a straight (sic) list of headers, then a
5197         ;; threaded summary display isn't required, so we just create
5198         ;; an unthreaded one.
5199         (gnus-summary-prepare-unthreaded threads)
5200
5201       ;; Do the threaded display.
5202
5203       (if gnus-summary-display-while-building
5204           (switch-to-buffer (buffer-name)))
5205       (while (or threads stack gnus-tmp-new-adopts new-roots)
5206
5207         (if (and (= gnus-tmp-level 0)
5208                  (or (not stack)
5209                      (= (caar stack) 0))
5210                  (not gnus-tmp-false-parent)
5211                  (or gnus-tmp-new-adopts new-roots))
5212             (if gnus-tmp-new-adopts
5213                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5214                       thread (list (car gnus-tmp-new-adopts))
5215                       gnus-tmp-header (caar thread)
5216                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5217               (when new-roots
5218                 (setq thread (list (car new-roots))
5219                       gnus-tmp-header (caar thread)
5220                       new-roots (cdr new-roots))))
5221
5222           (if threads
5223               ;; If there are some threads, we do them before the
5224               ;; threads on the stack.
5225               (setq thread threads
5226                     gnus-tmp-header (caar thread))
5227             ;; There were no current threads, so we pop something off
5228             ;; the stack.
5229             (setq state (car stack)
5230                   gnus-tmp-level (car state)
5231                   tree-stack (cadr state)
5232                   thread (caddr state)
5233                   stack (cdr stack)
5234                   gnus-tmp-header (caar thread))))
5235
5236         (setq gnus-tmp-false-parent nil)
5237         (setq gnus-tmp-root-expunged nil)
5238         (setq thread-end nil)
5239
5240         (if (stringp gnus-tmp-header)
5241             ;; The header is a dummy root.
5242             (cond
5243              ((eq gnus-summary-make-false-root 'adopt)
5244               ;; We let the first article adopt the rest.
5245               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5246                                                (cddar thread)))
5247               (setq gnus-tmp-gathered
5248                     (nconc (mapcar
5249                             (lambda (h) (mail-header-number (car h)))
5250                             (cddar thread))
5251                            gnus-tmp-gathered))
5252               (setq thread (cons (list (caar thread)
5253                                        (cadar thread))
5254                                  (cdr thread)))
5255               (setq gnus-tmp-level -1
5256                     gnus-tmp-false-parent t))
5257              ((eq gnus-summary-make-false-root 'empty)
5258               ;; We print adopted articles with empty subject fields.
5259               (setq gnus-tmp-gathered
5260                     (nconc (mapcar
5261                             (lambda (h) (mail-header-number (car h)))
5262                             (cddar thread))
5263                            gnus-tmp-gathered))
5264               (setq gnus-tmp-level -1))
5265              ((eq gnus-summary-make-false-root 'dummy)
5266               ;; We remember that we probably want to output a dummy
5267               ;; root.
5268               (setq gnus-tmp-dummy-line gnus-tmp-header)
5269               (setq gnus-tmp-prev-subject gnus-tmp-header))
5270              (t
5271               ;; We do not make a root for the gathered
5272               ;; sub-threads at all.
5273               (setq gnus-tmp-level -1)))
5274
5275           (setq number (mail-header-number gnus-tmp-header)
5276                 subject (mail-header-subject gnus-tmp-header)
5277                 simp-subject (gnus-simplify-subject-fully subject))
5278
5279           (cond
5280            ;; If the thread has changed subject, we might want to make
5281            ;; this subthread into a root.
5282            ((and (null gnus-thread-ignore-subject)
5283                  (not (zerop gnus-tmp-level))
5284                  gnus-tmp-prev-subject
5285                  (not (string= gnus-tmp-prev-subject simp-subject)))
5286             (setq new-roots (nconc new-roots (list (car thread)))
5287                   thread-end t
5288                   gnus-tmp-header nil))
5289            ;; If the article lies outside the current limit,
5290            ;; then we do not display it.
5291            ((not (memq number gnus-newsgroup-limit))
5292             (setq gnus-tmp-gathered
5293                   (nconc (mapcar
5294                           (lambda (h) (mail-header-number (car h)))
5295                           (cdar thread))
5296                          gnus-tmp-gathered))
5297             (setq gnus-tmp-new-adopts (if (cdar thread)
5298                                           (append gnus-tmp-new-adopts
5299                                                   (cdar thread))
5300                                         gnus-tmp-new-adopts)
5301                   thread-end t
5302                   gnus-tmp-header nil)
5303             (when (zerop gnus-tmp-level)
5304               (setq gnus-tmp-root-expunged t)))
5305            ;; Perhaps this article is to be marked as read?
5306            ((and gnus-summary-mark-below
5307                  (< (or (cdr (assq number gnus-newsgroup-scored))
5308                         default-score)
5309                     gnus-summary-mark-below)
5310                  ;; Don't touch sparse articles.
5311                  (not (gnus-summary-article-sparse-p number))
5312                  (not (gnus-summary-article-ancient-p number)))
5313             (setq gnus-newsgroup-unreads
5314                   (delq number gnus-newsgroup-unreads))
5315             (if gnus-newsgroup-auto-expire
5316                 (setq gnus-newsgroup-expirable
5317                       (gnus-add-to-sorted-list
5318                        gnus-newsgroup-expirable number))
5319               (push (cons number gnus-low-score-mark)
5320                     gnus-newsgroup-reads))))
5321
5322           (when gnus-tmp-header
5323             ;; We may have an old dummy line to output before this
5324             ;; article.
5325             (when (and gnus-tmp-dummy-line
5326                        (gnus-subject-equal
5327                         gnus-tmp-dummy-line
5328                         (mail-header-subject gnus-tmp-header)))
5329               (gnus-summary-insert-dummy-line
5330                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5331               (setq gnus-tmp-dummy-line nil))
5332
5333             ;; Compute the mark.
5334             (setq gnus-tmp-unread (gnus-article-mark number))
5335
5336             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5337                                   gnus-tmp-header gnus-tmp-level)
5338                   gnus-newsgroup-data)
5339
5340             ;; Actually insert the line.
5341             (setq
5342              gnus-tmp-subject-or-nil
5343              (cond
5344               ((and gnus-thread-ignore-subject
5345                     gnus-tmp-prev-subject
5346                     (not (string= gnus-tmp-prev-subject simp-subject)))
5347                subject)
5348               ((zerop gnus-tmp-level)
5349                (if (and (eq gnus-summary-make-false-root 'empty)
5350                         (memq number gnus-tmp-gathered)
5351                         gnus-tmp-prev-subject
5352                         (string= gnus-tmp-prev-subject simp-subject))
5353                    gnus-summary-same-subject
5354                  subject))
5355               (t gnus-summary-same-subject)))
5356             (if (and (eq gnus-summary-make-false-root 'adopt)
5357                      (= gnus-tmp-level 1)
5358                      (memq number gnus-tmp-gathered))
5359                 (setq gnus-tmp-opening-bracket ?\<
5360                       gnus-tmp-closing-bracket ?\>)
5361               (setq gnus-tmp-opening-bracket ?\[
5362                     gnus-tmp-closing-bracket ?\]))
5363             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5364                 (gnus-make-thread-indent-array
5365                  (max (* 2 (length gnus-thread-indent-array))
5366                       gnus-tmp-level)))
5367             (setq
5368              gnus-tmp-indentation
5369              (aref gnus-thread-indent-array gnus-tmp-level)
5370              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5371              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5372                                 gnus-summary-default-score 0)
5373              gnus-tmp-score-char
5374              (if (or (null gnus-summary-default-score)
5375                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5376                          gnus-summary-zcore-fuzz))
5377                  ?                      ;Whitespace
5378                (if (< gnus-tmp-score gnus-summary-default-score)
5379                    gnus-score-below-mark gnus-score-over-mark))
5380              gnus-tmp-replied
5381              (cond ((memq number gnus-newsgroup-processable)
5382                     gnus-process-mark)
5383                    ((memq number gnus-newsgroup-cached)
5384                     gnus-cached-mark)
5385                    ((memq number gnus-newsgroup-replied)
5386                     gnus-replied-mark)
5387                    ((memq number gnus-newsgroup-forwarded)
5388                     gnus-forwarded-mark)
5389                    ((memq number gnus-newsgroup-saved)
5390                     gnus-saved-mark)
5391                    ((memq number gnus-newsgroup-recent)
5392                     gnus-recent-mark)
5393                    ((memq number gnus-newsgroup-unseen)
5394                     gnus-unseen-mark)
5395                    (t gnus-no-mark))
5396              gnus-tmp-downloaded
5397              (cond ((memq number gnus-newsgroup-undownloaded)
5398                     gnus-undownloaded-mark)
5399                    (gnus-newsgroup-agentized
5400                     gnus-downloaded-mark)
5401                    (t
5402                     gnus-no-mark))
5403              gnus-tmp-from (mail-header-from gnus-tmp-header)
5404              gnus-tmp-name
5405              (cond
5406               ((string-match "<[^>]+> *$" gnus-tmp-from)
5407                (setq beg-match (match-beginning 0))
5408                (or (and (string-match "^\".+\"" gnus-tmp-from)
5409                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5410                    (substring gnus-tmp-from 0 beg-match)))
5411               ((string-match "(.+)" gnus-tmp-from)
5412                (substring gnus-tmp-from
5413                           (1+ (match-beginning 0)) (1- (match-end 0))))
5414               (t gnus-tmp-from))
5415
5416              ;; Do the %B string
5417              gnus-tmp-thread-tree-header-string
5418              (cond
5419               ((not gnus-show-threads) "")
5420               ((zerop gnus-tmp-level)
5421                (cond ((cdar thread)
5422                       (or gnus-sum-thread-tree-root subject))
5423                      (gnus-tmp-new-adopts
5424                       (or gnus-sum-thread-tree-false-root subject))
5425                      (t
5426                       (or gnus-sum-thread-tree-single-indent subject))))
5427               (t
5428                (concat (apply 'concat
5429                               (mapcar (lambda (item)
5430                                         (if (= item 1)
5431                                             gnus-sum-thread-tree-vertical
5432                                           gnus-sum-thread-tree-indent))
5433                                       (cdr (reverse tree-stack))))
5434                        (if (nth 1 thread)
5435                            gnus-sum-thread-tree-leaf-with-other
5436                          gnus-sum-thread-tree-single-leaf)))))
5437             (when (string= gnus-tmp-name "")
5438               (setq gnus-tmp-name gnus-tmp-from))
5439             (unless (numberp gnus-tmp-lines)
5440               (setq gnus-tmp-lines -1))
5441             (if (= gnus-tmp-lines -1)
5442                 (setq gnus-tmp-lines "?")
5443               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5444             (gnus-put-text-property
5445              (point)
5446              (progn (eval gnus-summary-line-format-spec) (point))
5447              'gnus-number number)
5448             (when gnus-visual-p
5449               (forward-line -1)
5450               (gnus-summary-highlight-line)
5451               (when gnus-summary-update-hook
5452                 (gnus-run-hooks 'gnus-summary-update-hook))
5453               (forward-line 1))
5454
5455             (setq gnus-tmp-prev-subject simp-subject)))
5456
5457         (when (nth 1 thread)
5458           (push (list (max 0 gnus-tmp-level)
5459                       (copy-sequence tree-stack)
5460                       (nthcdr 1 thread))
5461                 stack))
5462         (push (if (nth 1 thread) 1 0) tree-stack)
5463         (incf gnus-tmp-level)
5464         (setq threads (if thread-end nil (cdar thread)))
5465         (if gnus-summary-display-while-building
5466             (if building-count
5467                 (progn
5468                   ;; use a set frequency
5469                   (setq building-line-count (1- building-line-count))
5470                   (when (= building-line-count 0)
5471                     (sit-for 0)
5472                     (setq building-line-count
5473                           gnus-summary-display-while-building)))
5474               ;; always
5475               (sit-for 0)))
5476         (unless threads
5477           (setq gnus-tmp-level 0)))))
5478   (gnus-message 7 "Generating summary...done"))
5479
5480 (defun gnus-summary-prepare-unthreaded (headers)
5481   "Generate an unthreaded summary buffer based on HEADERS."
5482   (let (header number mark)
5483
5484     (beginning-of-line)
5485
5486     (while headers
5487       ;; We may have to root out some bad articles...
5488       (when (memq (setq number (mail-header-number
5489                                 (setq header (pop headers))))
5490                   gnus-newsgroup-limit)
5491         ;; Mark article as read when it has a low score.
5492         (when (and gnus-summary-mark-below
5493                    (< (or (cdr (assq number gnus-newsgroup-scored))
5494                           gnus-summary-default-score 0)
5495                       gnus-summary-mark-below)
5496                    (not (gnus-summary-article-ancient-p number)))
5497           (setq gnus-newsgroup-unreads
5498                 (delq number gnus-newsgroup-unreads))
5499           (if gnus-newsgroup-auto-expire
5500               (push number gnus-newsgroup-expirable)
5501             (push (cons number gnus-low-score-mark)
5502                   gnus-newsgroup-reads)))
5503
5504         (setq mark (gnus-article-mark number))
5505         (push (gnus-data-make number mark (1+ (point)) header 0)
5506               gnus-newsgroup-data)
5507         (gnus-summary-insert-line
5508          header 0 number
5509          (memq number gnus-newsgroup-undownloaded)
5510          mark (memq number gnus-newsgroup-replied)
5511          (memq number gnus-newsgroup-expirable)
5512          (mail-header-subject header) nil
5513          (cdr (assq number gnus-newsgroup-scored))
5514          (memq number gnus-newsgroup-processable))))))
5515
5516 (defun gnus-summary-remove-list-identifiers ()
5517   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5518   (let ((regexp (if (consp gnus-list-identifiers)
5519                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5520                   gnus-list-identifiers))
5521         changed subject)
5522     (when regexp
5523       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5524       (dolist (header gnus-newsgroup-headers)
5525         (setq subject (mail-header-subject header)
5526               changed nil)
5527         (while (string-match regexp subject)
5528           (setq subject
5529                 (concat (substring subject 0 (match-beginning 1))
5530                         (substring subject (match-end 0)))
5531                 changed t))
5532         (when changed
5533           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5534             (setq subject
5535                   (concat (substring subject 0 (match-beginning 1))
5536                           (substring subject (match-end 1)))))
5537           (mail-header-set-subject header subject))))))
5538
5539 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5540   "Fetch headers of ARTICLES."
5541   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5542     (gnus-message 5 "Fetching headers for %s..." name)
5543     (prog1
5544         (if (eq 'nov
5545                 (setq gnus-headers-retrieved-by
5546                       (gnus-retrieve-headers
5547                        articles gnus-newsgroup-name
5548                        (or limit
5549                            ;; We might want to fetch old headers, but
5550                            ;; not if there is only 1 article.
5551                            (and (or (and
5552                                      (not (eq gnus-fetch-old-headers 'some))
5553                                      (not (numberp gnus-fetch-old-headers)))
5554                                     (> (length articles) 1))
5555                                 gnus-fetch-old-headers)))))
5556             (gnus-get-newsgroup-headers-xover
5557              articles force-new dependencies gnus-newsgroup-name t)
5558           (gnus-get-newsgroup-headers dependencies force-new))
5559       (gnus-message 5 "Fetching headers for %s...done" name))))
5560
5561 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5562   "Select newsgroup GROUP.
5563 If READ-ALL is non-nil, all articles in the group are selected.
5564 If SELECT-ARTICLES, only select those articles from GROUP."
5565   (let* ((entry (gnus-group-entry group))
5566          ;;!!! Dirty hack; should be removed.
5567          (gnus-summary-ignore-duplicates
5568           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5569               t
5570             gnus-summary-ignore-duplicates))
5571          (info (nth 2 entry))
5572          charset articles fetched-articles cached)
5573
5574     (unless (gnus-check-server
5575              (set (make-local-variable 'gnus-current-select-method)
5576                   (gnus-find-method-for-group group)))
5577       (error "Couldn't open server"))
5578     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5579
5580     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5581         (gnus-activate-group group)     ; Or we can activate it...
5582         (progn                          ; Or we bug out.
5583           (when (equal major-mode 'gnus-summary-mode)
5584             (gnus-kill-buffer (current-buffer)))
5585           (error
5586            "Couldn't activate group %s: %s"
5587            (mm-decode-coding-string group charset)
5588            (mm-decode-coding-string (gnus-status-message group) charset))))
5589
5590     (unless (gnus-request-group group t)
5591       (when (equal major-mode 'gnus-summary-mode)
5592         (gnus-kill-buffer (current-buffer)))
5593       (error "Couldn't request group %s: %s"
5594              (mm-decode-coding-string group charset)
5595              (mm-decode-coding-string (gnus-status-message group) charset)))
5596
5597     (when (and gnus-agent
5598                (gnus-active group))
5599       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5600
5601       (setq gnus-summary-use-undownloaded-faces
5602             (gnus-agent-find-parameter
5603              group
5604              'agent-enable-undownloaded-faces)))
5605
5606     (setq gnus-newsgroup-name group
5607           gnus-newsgroup-unselected nil
5608           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5609
5610     (let ((display (gnus-group-find-parameter group 'display)))
5611       (setq gnus-newsgroup-display
5612             (cond
5613              ((not (zerop (or (car-safe read-all) 0)))
5614               ;; The user entered the group with C-u SPC/RET, let's show
5615               ;; all articles.
5616               'gnus-not-ignore)
5617              ((eq display 'all)
5618               'gnus-not-ignore)
5619              ((arrayp display)
5620               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5621              ((numberp display)
5622               ;; The following is probably the "correct" solution, but
5623               ;; it makes Gnus fetch all headers and then limit the
5624               ;; articles (which is slow), so instead we hack the
5625               ;; select-articles parameter instead. -- Simon Josefsson
5626               ;; <jas@kth.se>
5627               ;;
5628               ;; (gnus-byte-compile
5629               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5630               ;;                         display)))))
5631               (setq select-articles
5632                     (gnus-uncompress-range
5633                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5634                              (if (> tmp 0)
5635                                  tmp
5636                                1))
5637                            (cdr (gnus-active group)))))
5638               nil)
5639              (t
5640               nil))))
5641
5642     (gnus-summary-setup-default-charset)
5643
5644     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5645     (when (gnus-virtual-group-p group)
5646       (setq cached gnus-newsgroup-cached))
5647
5648     (setq gnus-newsgroup-unreads
5649           (gnus-sorted-ndifference
5650            (gnus-sorted-ndifference gnus-newsgroup-unreads
5651                                     gnus-newsgroup-marked)
5652            gnus-newsgroup-dormant))
5653
5654     (setq gnus-newsgroup-processable nil)
5655
5656     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5657
5658     ;; Adjust and set lists of article marks.
5659     (when info
5660       (gnus-adjust-marked-articles info))
5661     (if (setq articles select-articles)
5662         (setq gnus-newsgroup-unselected
5663               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5664       (setq articles (gnus-articles-to-read group read-all)))
5665
5666     (cond
5667      ((null articles)
5668       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5669       'quit)
5670      ((eq articles 0) nil)
5671      (t
5672       ;; Init the dependencies hash table.
5673       (setq gnus-newsgroup-dependencies
5674             (gnus-make-hashtable (length articles)))
5675       (gnus-set-global-variables)
5676       ;; Retrieve the headers and read them in.
5677
5678       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5679
5680       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5681       (when cached
5682         (setq gnus-newsgroup-cached cached))
5683
5684       ;; Suppress duplicates?
5685       (when gnus-suppress-duplicates
5686         (gnus-dup-suppress-articles))
5687
5688       ;; Set the initial limit.
5689       (setq gnus-newsgroup-limit (copy-sequence articles))
5690       ;; Remove canceled articles from the list of unread articles.
5691       (setq fetched-articles
5692             (mapcar (lambda (headers) (mail-header-number headers))
5693                     gnus-newsgroup-headers))
5694       (setq gnus-newsgroup-articles fetched-articles)
5695       (setq gnus-newsgroup-unreads
5696             (gnus-sorted-nintersection
5697              gnus-newsgroup-unreads fetched-articles))
5698       (gnus-compute-unseen-list)
5699
5700       ;; Removed marked articles that do not exist.
5701       (gnus-update-missing-marks
5702        (gnus-sorted-difference articles fetched-articles))
5703       ;; We might want to build some more threads first.
5704       (when (and gnus-fetch-old-headers
5705                  (eq gnus-headers-retrieved-by 'nov))
5706         (if (eq gnus-fetch-old-headers 'invisible)
5707             (gnus-build-all-threads)
5708           (gnus-build-old-threads)))
5709       ;; Let the Gnus agent mark articles as read.
5710       (when gnus-agent
5711         (gnus-agent-get-undownloaded-list))
5712       ;; Remove list identifiers from subject
5713       (when gnus-list-identifiers
5714         (gnus-summary-remove-list-identifiers))
5715       ;; Check whether auto-expire is to be done in this group.
5716       (setq gnus-newsgroup-auto-expire
5717             (gnus-group-auto-expirable-p group))
5718       ;; Set up the article buffer now, if necessary.
5719       (unless (and gnus-single-article-buffer
5720                    (equal gnus-article-buffer "*Article*"))
5721         (gnus-article-setup-buffer))
5722       ;; First and last article in this newsgroup.
5723       (when gnus-newsgroup-headers
5724         (setq gnus-newsgroup-begin
5725               (mail-header-number (car gnus-newsgroup-headers))
5726               gnus-newsgroup-end
5727               (mail-header-number
5728                (gnus-last-element gnus-newsgroup-headers))))
5729       ;; GROUP is successfully selected.
5730       (or gnus-newsgroup-headers t)))))
5731
5732 (defun gnus-compute-unseen-list ()
5733   ;; The `seen' marks are treated specially.
5734   (if (not gnus-newsgroup-seen)
5735       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5736     (setq gnus-newsgroup-unseen
5737           (gnus-inverse-list-range-intersection
5738            gnus-newsgroup-articles gnus-newsgroup-seen))))
5739
5740 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5741
5742 (defun gnus-summary-display-make-predicate (display)
5743   (require 'gnus-agent)
5744   (when (= (length display) 1)
5745     (setq display (car display)))
5746   (unless gnus-summary-display-cache
5747     (dolist (elem (append '((unread . unread)
5748                             (read . read)
5749                             (unseen . unseen))
5750                           gnus-article-mark-lists))
5751       (push (cons (cdr elem)
5752                   (gnus-byte-compile    ;Why bother?
5753                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5754             gnus-summary-display-cache)))
5755   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5756         (gnus-category-predicate-cache gnus-summary-display-cache))
5757     (gnus-get-predicate display)))
5758
5759 ;; Uses the dynamically bound `gnus-number' variable.
5760 (defvar gnus-number)
5761 (defun gnus-article-marked-p (type &optional article)
5762   (let ((article (or article gnus-number)))
5763     (cond
5764      ((eq type 'tick)
5765       (memq article gnus-newsgroup-marked))
5766      ((eq type 'spam)
5767       (memq article gnus-newsgroup-spam-marked))
5768      ((eq type 'unsend)
5769       (memq article gnus-newsgroup-unsendable))
5770      ((eq type 'undownload)
5771       (memq article gnus-newsgroup-undownloaded))
5772      ((eq type 'download)
5773       (memq article gnus-newsgroup-downloadable))
5774      ((eq type 'unread)
5775       (memq article gnus-newsgroup-unreads))
5776      ((eq type 'read)
5777       (memq article gnus-newsgroup-reads))
5778      ((eq type 'dormant)
5779       (memq article gnus-newsgroup-dormant) )
5780      ((eq type 'expire)
5781       (memq article gnus-newsgroup-expirable))
5782      ((eq type 'reply)
5783       (memq article gnus-newsgroup-replied))
5784      ((eq type 'killed)
5785       (memq article gnus-newsgroup-killed))
5786      ((eq type 'bookmark)
5787       (assq article gnus-newsgroup-bookmarks))
5788      ((eq type 'score)
5789       (assq article gnus-newsgroup-scored))
5790      ((eq type 'save)
5791       (memq article gnus-newsgroup-saved))
5792      ((eq type 'cache)
5793       (memq article gnus-newsgroup-cached))
5794      ((eq type 'forward)
5795       (memq article gnus-newsgroup-forwarded))
5796      ((eq type 'seen)
5797       (not (memq article gnus-newsgroup-unseen)))
5798      ((eq type 'recent)
5799       (memq article gnus-newsgroup-recent))
5800      (t t))))
5801
5802 (defun gnus-articles-to-read (group &optional read-all)
5803   "Find out what articles the user wants to read."
5804   (let* ((articles
5805           ;; Select all articles if `read-all' is non-nil, or if there
5806           ;; are no unread articles.
5807           (if (or read-all
5808                   (and (zerop (length gnus-newsgroup-marked))
5809                        (zerop (length gnus-newsgroup-unreads)))
5810                   ;; Fetch all if the predicate is non-nil.
5811                   gnus-newsgroup-display)
5812               ;; We want to select the headers for all the articles in
5813               ;; the group, so we select either all the active
5814               ;; articles in the group, or (if that's nil), the
5815               ;; articles in the cache.
5816               (or
5817                (if gnus-newsgroup-maximum-articles
5818                    (let ((active (gnus-active group)))
5819                      (gnus-uncompress-range
5820                       (cons (max (car active)
5821                                  (- (cdr active)
5822                                     gnus-newsgroup-maximum-articles
5823                                     -1))
5824                             (cdr active))))
5825                  (gnus-uncompress-range (gnus-active group)))
5826                (gnus-cache-articles-in-group group))
5827             ;; Select only the "normal" subset of articles.
5828             (gnus-sorted-nunion
5829              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5830              gnus-newsgroup-unreads)))
5831          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5832          (scored (length scored-list))
5833          (number (length articles))
5834          (marked (+ (length gnus-newsgroup-marked)
5835                     (length gnus-newsgroup-dormant)))
5836          (select
5837           (cond
5838            ((numberp read-all)
5839             read-all)
5840            ((numberp gnus-newsgroup-display)
5841             gnus-newsgroup-display)
5842            (t
5843             (condition-case ()
5844                 (cond
5845                  ((and (or (<= scored marked) (= scored number))
5846                        (numberp gnus-large-newsgroup)
5847                        (> number gnus-large-newsgroup))
5848                   (let* ((cursor-in-echo-area nil)
5849                          (initial (gnus-parameter-large-newsgroup-initial
5850                                    gnus-newsgroup-name))
5851                          (input
5852                           (read-string
5853                            (format
5854                             "How many articles from %s (%s %d): "
5855                             (gnus-group-decoded-name gnus-newsgroup-name)
5856                             (if initial "max" "default")
5857                             number)
5858                            (if initial
5859                                (cons (number-to-string initial)
5860                                      0)))))
5861                     (if (string-match "^[ \t]*$" input) number input)))
5862                  ((and (> scored marked) (< scored number)
5863                        (> (- scored number) 20))
5864                   (let ((input
5865                          (read-string
5866                           (format "%s %s (%d scored, %d total): "
5867                                   "How many articles from"
5868                                   (gnus-group-decoded-name group)
5869                                   scored number))))
5870                     (if (string-match "^[ \t]*$" input)
5871                         number input)))
5872                  (t number))
5873               (quit
5874                (message "Quit getting the articles to read")
5875                nil))))))
5876     (setq select (if (stringp select) (string-to-number select) select))
5877     (if (or (null select) (zerop select))
5878         select
5879       (if (and (not (zerop scored)) (<= (abs select) scored))
5880           (progn
5881             (setq articles (sort scored-list '<))
5882             (setq number (length articles)))
5883         (setq articles (copy-sequence articles)))
5884
5885       (when (< (abs select) number)
5886         (if (< select 0)
5887             ;; Select the N oldest articles.
5888             (setcdr (nthcdr (1- (abs select)) articles) nil)
5889           ;; Select the N most recent articles.
5890           (setq articles (nthcdr (- number select) articles))))
5891       (setq gnus-newsgroup-unselected
5892             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5893       (when gnus-alter-articles-to-read-function
5894         (setq articles
5895               (sort
5896                (funcall gnus-alter-articles-to-read-function
5897                         gnus-newsgroup-name articles)
5898                '<)))
5899       articles)))
5900
5901 (defun gnus-killed-articles (killed articles)
5902   (let (out)
5903     (while articles
5904       (when (inline (gnus-member-of-range (car articles) killed))
5905         (push (car articles) out))
5906       (setq articles (cdr articles)))
5907     out))
5908
5909 (defun gnus-uncompress-marks (marks)
5910   "Uncompress the mark ranges in MARKS."
5911   (let ((uncompressed '(score bookmark))
5912         out)
5913     (while marks
5914       (if (memq (caar marks) uncompressed)
5915           (push (car marks) out)
5916         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5917       (setq marks (cdr marks)))
5918     out))
5919
5920 (defun gnus-article-mark-to-type (mark)
5921   "Return the type of MARK."
5922   (or (cadr (assq mark gnus-article-special-mark-lists))
5923       'list))
5924
5925 (defun gnus-article-unpropagatable-p (mark)
5926   "Return whether MARK should be propagated to back end."
5927   (memq mark gnus-article-unpropagated-mark-lists))
5928
5929 (defun gnus-adjust-marked-articles (info)
5930   "Set all article lists and remove all marks that are no longer valid."
5931   (let* ((marked-lists (gnus-info-marks info))
5932          (active (gnus-active (gnus-info-group info)))
5933          (min (car active))
5934          (max (cdr active))
5935          (types gnus-article-mark-lists)
5936          marks var articles article mark mark-type
5937          bgn end)
5938     ;; Hack to avoid adjusting marks for imap.
5939     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5940               'nnimap)
5941       (setq min 1))
5942
5943     (dolist (marks marked-lists)
5944       (setq mark (car marks)
5945             mark-type (gnus-article-mark-to-type mark)
5946             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5947
5948       ;; We set the variable according to the type of the marks list,
5949       ;; and then adjust the marks to a subset of the active articles.
5950       (cond
5951        ;; Adjust "simple" lists - compressed yet unsorted
5952        ((eq mark-type 'list)
5953         ;; Simultaneously uncompress and clip to active range
5954         ;; See gnus-uncompress-range for a description of possible marks
5955         (let (l lh)
5956           (if (not (cadr marks))
5957               (set var nil)
5958             (setq articles (if (numberp (cddr marks))
5959                                (list (cdr marks))
5960                              (cdr marks))
5961                   lh (cons nil nil)
5962                   l lh)
5963
5964             (while (setq article (pop articles))
5965               (cond ((consp article)
5966                      (setq bgn (max (car article) min)
5967                            end (min (cdr article) max))
5968                      (while (<= bgn end)
5969                        (setq l (setcdr l (cons bgn nil))
5970                              bgn (1+ bgn))))
5971                     ((and (<= min article)
5972                           (>= max article))
5973                      (setq l (setcdr l (cons article nil))))))
5974             (set var (cdr lh)))))
5975        ;; Adjust assocs.
5976        ((eq mark-type 'tuple)
5977         (set var (setq articles (cdr marks)))
5978         (when (not (listp (cdr (symbol-value var))))
5979           (set var (list (symbol-value var))))
5980         (when (not (listp (cdr articles)))
5981           (setq articles (list articles)))
5982         (while articles
5983           (when (or (not (consp (setq article (pop articles))))
5984                     (< (car article) min)
5985                     (> (car article) max))
5986             (set var (delq article (symbol-value var))))))
5987        ;; Adjust ranges (sloppily).
5988        ((eq mark-type 'range)
5989         (cond
5990          ((eq mark 'seen)
5991           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5992           ;; It should be (seen (NUM1 . NUM2)).
5993           (when (numberp (cddr marks))
5994             (setcdr marks (list (cdr marks))))
5995           (setq articles (cdr marks))
5996           (while (and articles
5997                       (or (and (consp (car articles))
5998                                (> min (cdar articles)))
5999                           (and (numberp (car articles))
6000                                (> min (car articles)))))
6001             (pop articles))
6002           (set var articles))))))))
6003
6004 (defun gnus-update-missing-marks (missing)
6005   "Go through the list of MISSING articles and remove them from the mark lists."
6006   (when missing
6007     (let (var m)
6008       ;; Go through all types.
6009       (dolist (elem gnus-article-mark-lists)
6010         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6011           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6012           (when (symbol-value var)
6013             ;; This list has articles.  So we delete all missing
6014             ;; articles from it.
6015             (setq m missing)
6016             (while m
6017               (set var (delq (pop m) (symbol-value var))))))))))
6018
6019 (defun gnus-update-marks ()
6020   "Enter the various lists of marked articles into the newsgroup info list."
6021   (let ((types gnus-article-mark-lists)
6022         (info (gnus-get-info gnus-newsgroup-name))
6023         type list newmarked symbol delta-marks)
6024     (when info
6025       ;; Add all marks lists to the list of marks lists.
6026       (while (setq type (pop types))
6027         (setq list (symbol-value
6028                     (setq symbol
6029                           (intern (format "gnus-newsgroup-%s" (car type))))))
6030
6031         (when list
6032           ;; Get rid of the entries of the articles that have the
6033           ;; default score.
6034           (when (and (eq (cdr type) 'score)
6035                      gnus-save-score
6036                      list)
6037             (let* ((arts list)
6038                    (prev (cons nil list))
6039                    (all prev))
6040               (while arts
6041                 (if (or (not (consp (car arts)))
6042                         (= (cdar arts) gnus-summary-default-score))
6043                     (setcdr prev (cdr arts))
6044                   (setq prev arts))
6045                 (setq arts (cdr arts)))
6046               (setq list (cdr all)))))
6047
6048         (when (eq (cdr type) 'seen)
6049           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6050
6051         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6052           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6053
6054         (when (and (gnus-check-backend-function
6055                     'request-set-mark gnus-newsgroup-name)
6056                    (not (gnus-article-unpropagatable-p (cdr type))))
6057           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6058                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
6059                  (add (gnus-remove-from-range
6060                        (gnus-copy-sequence list) old)))
6061             (when add
6062               (push (list add 'add (list (cdr type))) delta-marks))
6063             (when del
6064               ;; Don't delete marks from outside the active range.  This
6065               ;; shouldn't happen, but is a sanity check.
6066               (setq del (gnus-sorted-range-intersection
6067                          (gnus-active gnus-newsgroup-name) del))
6068               (push (list del 'del (list (cdr type))) delta-marks))))
6069
6070         (when list
6071           (push (cons (cdr type) list) newmarked)))
6072
6073       (when delta-marks
6074         (unless (gnus-check-group gnus-newsgroup-name)
6075           (error "Can't open server for %s" gnus-newsgroup-name))
6076         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6077
6078       ;; Enter these new marks into the info of the group.
6079       (if (nthcdr 3 info)
6080           (setcar (nthcdr 3 info) newmarked)
6081         ;; Add the marks lists to the end of the info.
6082         (when newmarked
6083           (setcdr (nthcdr 2 info) (list newmarked))))
6084
6085       ;; Cut off the end of the info if there's nothing else there.
6086       (let ((i 5))
6087         (while (and (> i 2)
6088                     (not (nth i info)))
6089           (when (nthcdr (decf i) info)
6090             (setcdr (nthcdr i info) nil)))))))
6091
6092 (defun gnus-set-mode-line (where)
6093   "Set the mode line of the article or summary buffers.
6094 If WHERE is `summary', the summary mode line format will be used."
6095   ;; Is this mode line one we keep updated?
6096   (when (and (memq where gnus-updated-mode-lines)
6097              (symbol-value
6098               (intern (format "gnus-%s-mode-line-format-spec" where))))
6099     (let (mode-string)
6100       ;; We evaluate this in the summary buffer since these
6101       ;; variables are buffer-local to that buffer.
6102       (with-current-buffer gnus-summary-buffer
6103         ;; We bind all these variables that are used in the `eval' form
6104         ;; below.
6105         (let* ((mformat (symbol-value
6106                          (intern
6107                           (format "gnus-%s-mode-line-format-spec" where))))
6108                (gnus-tmp-group-name (gnus-mode-string-quote
6109                                      (gnus-group-decoded-name
6110                                       gnus-newsgroup-name)))
6111                (gnus-tmp-article-number (or gnus-current-article 0))
6112                (gnus-tmp-unread gnus-newsgroup-unreads)
6113                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6114                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6115                (gnus-tmp-unread-and-unselected
6116                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6117                             (zerop gnus-tmp-unselected))
6118                        "")
6119                       ((zerop gnus-tmp-unselected)
6120                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6121                       (t (format "{%d(+%d) more}"
6122                                  gnus-tmp-unread-and-unticked
6123                                  gnus-tmp-unselected))))
6124                (gnus-tmp-subject
6125                 (if (and gnus-current-headers
6126                          (vectorp gnus-current-headers))
6127                     (gnus-mode-string-quote
6128                      (mail-header-subject gnus-current-headers))
6129                   ""))
6130                bufname-length max-len
6131                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6132           (setq mode-string (eval mformat))
6133           (setq bufname-length (if (string-match "%b" mode-string)
6134                                    (- (length
6135                                        (buffer-name
6136                                         (if (eq where 'summary)
6137                                             nil
6138                                           (get-buffer gnus-article-buffer))))
6139                                       2)
6140                                  0))
6141           (setq max-len (max 4 (if gnus-mode-non-string-length
6142                                    (- (window-width)
6143                                       gnus-mode-non-string-length
6144                                       bufname-length)
6145                                  (length mode-string))))
6146           ;; We might have to chop a bit of the string off...
6147           (when (> (length mode-string) max-len)
6148             (setq mode-string
6149                   (concat (truncate-string-to-width mode-string (- max-len 3))
6150                           "...")))))
6151       ;; Update the mode line.
6152       (setq mode-line-buffer-identification
6153             (gnus-mode-line-buffer-identification (list mode-string)))
6154       (set-buffer-modified-p t))))
6155
6156 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6157   "Go through the HEADERS list and add all Xrefs to a hash table.
6158 The resulting hash table is returned, or nil if no Xrefs were found."
6159   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6160          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6161          (xref-hashtb (gnus-make-hashtable))
6162          start group entry number xrefs header)
6163     (while headers
6164       (setq header (pop headers))
6165       (when (and (setq xrefs (mail-header-xref header))
6166                  (not (memq (setq number (mail-header-number header))
6167                             unreads)))
6168         (setq start 0)
6169         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6170           (setq start (match-end 0))
6171           (setq group (if prefix
6172                           (concat prefix (substring xrefs (match-beginning 1)
6173                                                     (match-end 1)))
6174                         (substring xrefs (match-beginning 1) (match-end 1))))
6175           (setq number
6176                 (string-to-number (substring xrefs (match-beginning 2)
6177                                           (match-end 2))))
6178           (if (setq entry (gnus-gethash group xref-hashtb))
6179               (setcdr entry (cons number (cdr entry)))
6180             (gnus-sethash group (cons number nil) xref-hashtb)))))
6181     (and start xref-hashtb)))
6182
6183 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6184   "Look through all the headers and mark the Xrefs as read."
6185   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6186         name info xref-hashtb idlist method nth4)
6187     (with-current-buffer gnus-group-buffer
6188       (when (setq xref-hashtb
6189                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6190         (mapatoms
6191          (lambda (group)
6192            (unless (string= from-newsgroup (setq name (symbol-name group)))
6193              (setq idlist (symbol-value group))
6194              ;; Dead groups are not updated.
6195              (and (prog1
6196                       (setq info (gnus-get-info name))
6197                     (when (stringp (setq nth4 (gnus-info-method info)))
6198                       (setq nth4 (gnus-server-to-method nth4))))
6199                   ;; Only do the xrefs if the group has the same
6200                   ;; select method as the group we have just read.
6201                   (or (gnus-methods-equal-p
6202                        nth4 (gnus-find-method-for-group from-newsgroup))
6203                       virtual
6204                       (equal nth4 (setq method (gnus-find-method-for-group
6205                                                 from-newsgroup)))
6206                       (and (equal (car nth4) (car method))
6207                            (equal (nth 1 nth4) (nth 1 method))))
6208                   gnus-use-cross-reference
6209                   (or (not (eq gnus-use-cross-reference t))
6210                       virtual
6211                       ;; Only do cross-references on subscribed
6212                       ;; groups, if that is what is wanted.
6213                       (<= (gnus-info-level info) gnus-level-subscribed))
6214                   (gnus-group-make-articles-read name idlist))))
6215          xref-hashtb)))))
6216
6217 (defun gnus-compute-read-articles (group articles)
6218   (let* ((entry (gnus-group-entry group))
6219          (info (nth 2 entry))
6220          (active (gnus-active group))
6221          ninfo)
6222     (when entry
6223       ;; First peel off all invalid article numbers.
6224       (when active
6225         (let ((ids articles)
6226               id first)
6227           (while (setq id (pop ids))
6228             (when (and first (> id (cdr active)))
6229               ;; We'll end up in this situation in one particular
6230               ;; obscure situation.  If you re-scan a group and get
6231               ;; a new article that is cross-posted to a different
6232               ;; group that has not been re-scanned, you might get
6233               ;; crossposted article that has a higher number than
6234               ;; Gnus believes possible.  So we re-activate this
6235               ;; group as well.  This might mean doing the
6236               ;; crossposting thingy will *increase* the number
6237               ;; of articles in some groups.  Tsk, tsk.
6238               (setq active (or (gnus-activate-group group) active)))
6239             (when (or (> id (cdr active))
6240                       (< id (car active)))
6241               (setq articles (delq id articles))))))
6242       ;; If the read list is nil, we init it.
6243       (if (and active
6244                (null (gnus-info-read info))
6245                (> (car active) 1))
6246           (setq ninfo (cons 1 (1- (car active))))
6247         (setq ninfo (gnus-info-read info)))
6248       ;; Then we add the read articles to the range.
6249       (gnus-add-to-range
6250        ninfo (setq articles (sort articles '<))))))
6251
6252 (defun gnus-group-make-articles-read (group articles)
6253   "Update the info of GROUP to say that ARTICLES are read."
6254   (let* ((num 0)
6255          (entry (gnus-group-entry group))
6256          (info (nth 2 entry))
6257          (active (gnus-active group))
6258          range)
6259     (if (not entry)
6260         ;; Group that Gnus doesn't know exists, but still allow the
6261         ;; backend to set marks.
6262         (gnus-request-set-mark
6263          group (list (list (gnus-compress-sequence (sort articles #'<))
6264                            'add '(read))))
6265       ;; Normal, subscribed groups.
6266       (setq range (gnus-compute-read-articles group articles))
6267       (with-current-buffer gnus-group-buffer
6268         (gnus-undo-register
6269           `(progn
6270              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6271              (gnus-info-set-read ',info ',(gnus-info-read info))
6272              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6273              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6274              (gnus-group-update-group ,group t))))
6275       ;; Add the read articles to the range.
6276       (gnus-info-set-read info range)
6277       (gnus-request-set-mark group (list (list range 'add '(read))))
6278       ;; Then we have to re-compute how many unread
6279       ;; articles there are in this group.
6280       (when active
6281         (cond
6282          ((not range)
6283           (setq num (- (1+ (cdr active)) (car active))))
6284          ((not (listp (cdr range)))
6285           (setq num (- (cdr active) (- (1+ (cdr range))
6286                                        (car range)))))
6287          (t
6288           (while range
6289             (if (numberp (car range))
6290                 (setq num (1+ num))
6291               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6292             (setq range (cdr range)))
6293           (setq num (- (cdr active) num))))
6294         ;; Update the number of unread articles.
6295         (setcar entry num)
6296         ;; Update the group buffer.
6297         (unless (gnus-ephemeral-group-p group)
6298           (gnus-group-update-group group t))))))
6299
6300 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6301   (let ((cur nntp-server-buffer)
6302         (dependencies
6303          (or dependencies
6304              (with-current-buffer gnus-summary-buffer
6305                gnus-newsgroup-dependencies)))
6306         headers id end ref number
6307         (mail-parse-charset gnus-newsgroup-charset)
6308         (mail-parse-ignored-charsets
6309          (save-current-buffer (condition-case nil
6310                                   (set-buffer gnus-summary-buffer)
6311                                 (error))
6312                               gnus-newsgroup-ignored-charsets)))
6313     (with-current-buffer nntp-server-buffer
6314       ;; Translate all TAB characters into SPACE characters.
6315       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6316       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6317       (ietf-drums-unfold-fws)
6318       (gnus-run-hooks 'gnus-parse-headers-hook)
6319       (let ((case-fold-search t)
6320             in-reply-to header p lines chars)
6321         (goto-char (point-min))
6322         ;; Search to the beginning of the next header.  Error messages
6323         ;; do not begin with 2 or 3.
6324         (while (re-search-forward "^[23][0-9]+ " nil t)
6325           (setq id nil
6326                 ref nil)
6327           ;; This implementation of this function, with nine
6328           ;; search-forwards instead of the one re-search-forward and
6329           ;; a case (which basically was the old function) is actually
6330           ;; about twice as fast, even though it looks messier.  You
6331           ;; can't have everything, I guess.  Speed and elegance
6332           ;; doesn't always go hand in hand.
6333           (setq
6334            header
6335            (vector
6336             ;; Number.
6337             (prog1
6338                 (setq number (read cur))
6339               (end-of-line)
6340               (setq p (point))
6341               (narrow-to-region (point)
6342                                 (or (and (search-forward "\n.\n" nil t)
6343                                          (- (point) 2))
6344                                     (point))))
6345             ;; Subject.
6346             (progn
6347               (goto-char p)
6348               (if (search-forward "\nsubject:" nil t)
6349                   (funcall gnus-decode-encoded-word-function
6350                            (nnheader-header-value))
6351                 "(none)"))
6352             ;; From.
6353             (progn
6354               (goto-char p)
6355               (if (search-forward "\nfrom:" nil t)
6356                   (funcall gnus-decode-encoded-address-function
6357                            (nnheader-header-value))
6358                 "(nobody)"))
6359             ;; Date.
6360             (progn
6361               (goto-char p)
6362               (if (search-forward "\ndate:" nil t)
6363                   (nnheader-header-value) ""))
6364             ;; Message-ID.
6365             (progn
6366               (goto-char p)
6367               (setq id (if (re-search-forward
6368                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6369                            ;; We do it this way to make sure the Message-ID
6370                            ;; is (somewhat) syntactically valid.
6371                            (buffer-substring (match-beginning 1)
6372                                              (match-end 1))
6373                          ;; If there was no message-id, we just fake one
6374                          ;; to make subsequent routines simpler.
6375                          (nnheader-generate-fake-message-id number))))
6376             ;; References.
6377             (progn
6378               (goto-char p)
6379               (if (search-forward "\nreferences:" nil t)
6380                   (progn
6381                     (setq end (point))
6382                     (prog1
6383                         (nnheader-header-value)
6384                       (setq ref
6385                             (buffer-substring
6386                              (progn
6387                                (end-of-line)
6388                                (search-backward ">" end t)
6389                                (1+ (point)))
6390                              (progn
6391                                (search-backward "<" end t)
6392                                (point))))))
6393                 ;; Get the references from the in-reply-to header if there
6394                 ;; were no references and the in-reply-to header looks
6395                 ;; promising.
6396                 (if (and (search-forward "\nin-reply-to:" nil t)
6397                          (setq in-reply-to (nnheader-header-value))
6398                          (string-match "<[^>]+>" in-reply-to))
6399                     (let (ref2)
6400                       (setq ref (substring in-reply-to (match-beginning 0)
6401                                            (match-end 0)))
6402                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6403                         (setq ref2 (substring in-reply-to (match-beginning 0)
6404                                               (match-end 0)))
6405                         (when (> (length ref2) (length ref))
6406                           (setq ref ref2)))
6407                       ref)
6408                   (setq ref nil))))
6409             ;; Chars.
6410             (progn
6411               (goto-char p)
6412               (if (search-forward "\nchars: " nil t)
6413                   (if (numberp (setq chars (ignore-errors (read cur))))
6414                       chars -1)
6415                 -1))
6416             ;; Lines.
6417             (progn
6418               (goto-char p)
6419               (if (search-forward "\nlines: " nil t)
6420                   (if (numberp (setq lines (ignore-errors (read cur))))
6421                       lines -1)
6422                 -1))
6423             ;; Xref.
6424             (progn
6425               (goto-char p)
6426               (and (search-forward "\nxref:" nil t)
6427                    (nnheader-header-value)))
6428             ;; Extra.
6429             (when gnus-extra-headers
6430               (let ((extra gnus-extra-headers)
6431                     out)
6432                 (while extra
6433                   (goto-char p)
6434                   (when (search-forward
6435                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6436                     (push (cons (car extra) (nnheader-header-value))
6437                           out))
6438                   (pop extra))
6439                 out))))
6440           (when (equal id ref)
6441             (setq ref nil))
6442
6443           (when gnus-alter-header-function
6444             (funcall gnus-alter-header-function header)
6445             (setq id (mail-header-id header)
6446                   ref (gnus-parent-id (mail-header-references header))))
6447
6448           (when (setq header
6449                       (gnus-dependencies-add-header
6450                        header dependencies force-new))
6451             (push header headers))
6452           (goto-char (point-max))
6453           (widen))
6454         (nreverse headers)))))
6455
6456 ;; Goes through the xover lines and returns a list of vectors
6457 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6458                                                   force-new dependencies
6459                                                   group also-fetch-heads)
6460   "Parse the news overview data in the server buffer.
6461 Return a list of headers that match SEQUENCE (see
6462 `nntp-retrieve-headers')."
6463   ;; Get the Xref when the users reads the articles since most/some
6464   ;; NNTP servers do not include Xrefs when using XOVER.
6465   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6466   (let ((mail-parse-charset gnus-newsgroup-charset)
6467         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6468         (cur nntp-server-buffer)
6469         (dependencies (or dependencies gnus-newsgroup-dependencies))
6470         (allp (cond
6471                ((eq gnus-read-all-available-headers t)
6472                 t)
6473                ((and (stringp gnus-read-all-available-headers)
6474                      group)
6475                 (string-match gnus-read-all-available-headers group))
6476                (t
6477                 nil)))
6478         number headers header)
6479     (with-current-buffer nntp-server-buffer
6480       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6481       ;; Allow the user to mangle the headers before parsing them.
6482       (gnus-run-hooks 'gnus-parse-headers-hook)
6483       (goto-char (point-min))
6484       (gnus-parse-without-error
6485         (while (and (or sequence allp)
6486                     (not (eobp)))
6487           (setq number (read cur))
6488           (when (not allp)
6489             (while (and sequence
6490                         (< (car sequence) number))
6491               (setq sequence (cdr sequence))))
6492           (when (and (or allp
6493                          (and sequence
6494                               (eq number (car sequence))))
6495                      (progn
6496                        (setq sequence (cdr sequence))
6497                        (setq header (inline
6498                                       (gnus-nov-parse-line
6499                                        number dependencies force-new)))))
6500             (push header headers))
6501           (forward-line 1)))
6502       ;; A common bug in inn is that if you have posted an article and
6503       ;; then retrieves the active file, it will answer correctly --
6504       ;; the new article is included.  However, a NOV entry for the
6505       ;; article may not have been generated yet, so this may fail.
6506       ;; We work around this problem by retrieving the last few
6507       ;; headers using HEAD.
6508       (if (or (not also-fetch-heads)
6509               (not sequence))
6510           ;; We (probably) got all the headers.
6511           (nreverse headers)
6512         (let ((gnus-nov-is-evil t))
6513           (nconc
6514            (nreverse headers)
6515            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6516              (gnus-get-newsgroup-headers))))))))
6517
6518 (defun gnus-article-get-xrefs ()
6519   "Fill in the Xref value in `gnus-current-headers', if necessary.
6520 This is meant to be called in `gnus-article-internal-prepare-hook'."
6521   (let ((headers (with-current-buffer gnus-summary-buffer
6522                    gnus-current-headers)))
6523     (or (not gnus-use-cross-reference)
6524         (not headers)
6525         (and (mail-header-xref headers)
6526              (not (string= (mail-header-xref headers) "")))
6527         (let ((case-fold-search t)
6528               xref)
6529           (save-restriction
6530             (nnheader-narrow-to-headers)
6531             (goto-char (point-min))
6532             (when (or (and (not (eobp))
6533                            (eq (downcase (char-after)) ?x)
6534                            (looking-at "Xref:"))
6535                       (search-forward "\nXref:" nil t))
6536               (goto-char (1+ (match-end 0)))
6537               (setq xref (buffer-substring (point) (point-at-eol)))
6538               (mail-header-set-xref headers xref)))))))
6539
6540 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6541   "Find article ID and insert the summary line for that article.
6542 OLD-HEADER can either be a header or a line number to insert
6543 the subject line on."
6544   (let* ((line (and (numberp old-header) old-header))
6545          (old-header (and (vectorp old-header) old-header))
6546          (header (cond ((and old-header use-old-header)
6547                         old-header)
6548                        ((and (numberp id)
6549                              (gnus-number-to-header id))
6550                         (gnus-number-to-header id))
6551                        (t
6552                         (gnus-read-header id))))
6553          (number (and (numberp id) id))
6554          d)
6555     (when header
6556       ;; Rebuild the thread that this article is part of and go to the
6557       ;; article we have fetched.
6558       (when (and (not gnus-show-threads)
6559                  old-header)
6560         (when (and number
6561                    (setq d (gnus-data-find (mail-header-number old-header))))
6562           (goto-char (gnus-data-pos d))
6563           (gnus-data-remove
6564            number
6565            (- (point-at-bol)
6566               (prog1
6567                   (1+ (point-at-eol))
6568                 (gnus-delete-line))))))
6569       ;; Remove list identifiers from subject.
6570       (when gnus-list-identifiers
6571         (let ((gnus-newsgroup-headers (list header)))
6572           (gnus-summary-remove-list-identifiers)))
6573       (when old-header
6574         (mail-header-set-number header (mail-header-number old-header)))
6575       (setq gnus-newsgroup-sparse
6576             (delq (setq number (mail-header-number header))
6577                   gnus-newsgroup-sparse))
6578       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6579       (push number gnus-newsgroup-limit)
6580       (gnus-rebuild-thread (mail-header-id header) line)
6581       (gnus-summary-goto-subject number nil t))
6582     (when (and (numberp number)
6583                (> number 0))
6584       ;; We have to update the boundaries even if we can't fetch the
6585       ;; article if ID is a number -- so that the next `P' or `N'
6586       ;; command will fetch the previous (or next) article even
6587       ;; if the one we tried to fetch this time has been canceled.
6588       (when (> number gnus-newsgroup-end)
6589         (setq gnus-newsgroup-end number))
6590       (when (< number gnus-newsgroup-begin)
6591         (setq gnus-newsgroup-begin number))
6592       (setq gnus-newsgroup-unselected
6593             (delq number gnus-newsgroup-unselected)))
6594     ;; Report back a success?
6595     (and header (mail-header-number header))))
6596
6597 ;;; Process/prefix in the summary buffer
6598
6599 (defun gnus-summary-work-articles (n)
6600   "Return a list of articles to be worked upon.
6601 The prefix argument, the list of process marked articles, and the
6602 current article will be taken into consideration."
6603   (with-current-buffer gnus-summary-buffer
6604     (cond
6605      (n
6606       ;; A numerical prefix has been given.
6607       (setq n (prefix-numeric-value n))
6608       (let ((backward (< n 0))
6609             (n (abs (prefix-numeric-value n)))
6610             articles article)
6611         (save-excursion
6612           (while
6613               (and (> n 0)
6614                    (push (setq article (gnus-summary-article-number))
6615                          articles)
6616                    (if backward
6617                        (gnus-summary-find-prev nil article)
6618                      (gnus-summary-find-next nil article)))
6619             (decf n)))
6620         (nreverse articles)))
6621      ((and (gnus-region-active-p) (mark))
6622       (message "region active")
6623       ;; Work on the region between point and mark.
6624       (let ((max (max (point) (mark)))
6625             articles article)
6626         (save-excursion
6627           (goto-char (min (point) (mark)))
6628           (while
6629               (and
6630                (push (setq article (gnus-summary-article-number)) articles)
6631                (gnus-summary-find-next nil article)
6632                (< (point) max)))
6633           (nreverse articles))))
6634      (gnus-newsgroup-processable
6635       ;; There are process-marked articles present.
6636       ;; Save current state.
6637       (gnus-summary-save-process-mark)
6638       ;; Return the list.
6639       (reverse gnus-newsgroup-processable))
6640      (t
6641       ;; Just return the current article.
6642       (list (gnus-summary-article-number))))))
6643
6644 (defmacro gnus-summary-iterate (arg &rest forms)
6645   "Iterate over the process/prefixed articles and do FORMS.
6646 ARG is the interactive prefix given to the command.  FORMS will be
6647 executed with point over the summary line of the articles."
6648   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6649     `(let ((,articles (gnus-summary-work-articles ,arg)))
6650        (while ,articles
6651          (gnus-summary-goto-subject (car ,articles))
6652          ,@forms
6653          (pop ,articles)))))
6654
6655 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6656 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6657
6658 (defun gnus-summary-save-process-mark ()
6659   "Push the current set of process marked articles on the stack."
6660   (interactive)
6661   (push (copy-sequence gnus-newsgroup-processable)
6662         gnus-newsgroup-process-stack))
6663
6664 (defun gnus-summary-kill-process-mark ()
6665   "Push the current set of process marked articles on the stack and unmark."
6666   (interactive)
6667   (gnus-summary-save-process-mark)
6668   (gnus-summary-unmark-all-processable))
6669
6670 (defun gnus-summary-yank-process-mark ()
6671   "Pop the last process mark state off the stack and restore it."
6672   (interactive)
6673   (unless gnus-newsgroup-process-stack
6674     (error "Empty mark stack"))
6675   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6676
6677 (defun gnus-summary-process-mark-set (set)
6678   "Make SET into the current process marked articles."
6679   (gnus-summary-unmark-all-processable)
6680   (mapc 'gnus-summary-set-process-mark set))
6681
6682 ;;; Searching and stuff
6683
6684 (defun gnus-summary-search-group (&optional backward use-level)
6685   "Search for next unread newsgroup.
6686 If optional argument BACKWARD is non-nil, search backward instead."
6687   (with-current-buffer gnus-group-buffer
6688     (when (gnus-group-search-forward
6689            backward nil (if use-level (gnus-group-group-level) nil))
6690       (gnus-group-group-name))))
6691
6692 (defun gnus-summary-best-group (&optional exclude-group)
6693   "Find the name of the best unread group.
6694 If EXCLUDE-GROUP, do not go to this group."
6695   (with-current-buffer gnus-group-buffer
6696     (save-excursion
6697       (gnus-group-best-unread-group exclude-group))))
6698
6699 (defun gnus-summary-find-next (&optional unread article backward)
6700   (if backward
6701       (gnus-summary-find-prev unread article)
6702     (let* ((dummy (gnus-summary-article-intangible-p))
6703            (article (or article (gnus-summary-article-number)))
6704            (data (gnus-data-find-list article))
6705            result)
6706       (when (and (not dummy)
6707                  (or (not gnus-summary-check-current)
6708                      (not unread)
6709                      (not (gnus-data-unread-p (car data)))))
6710         (setq data (cdr data)))
6711       (when (setq result
6712                   (if unread
6713                       (progn
6714                         (while data
6715                           (unless (memq (gnus-data-number (car data))
6716                                         (cond
6717                                          ((eq gnus-auto-goto-ignores
6718                                               'always-undownloaded)
6719                                           gnus-newsgroup-undownloaded)
6720                                          (gnus-plugged
6721                                           nil)
6722                                          ((eq gnus-auto-goto-ignores
6723                                               'unfetched)
6724                                           gnus-newsgroup-unfetched)
6725                                          ((eq gnus-auto-goto-ignores
6726                                               'undownloaded)
6727                                           gnus-newsgroup-undownloaded)))
6728                             (when (gnus-data-unread-p (car data))
6729                               (setq result (car data)
6730                                     data nil)))
6731                           (setq data (cdr data)))
6732                         result)
6733                     (car data)))
6734         (goto-char (gnus-data-pos result))
6735         (gnus-data-number result)))))
6736
6737 (defun gnus-summary-find-prev (&optional unread article)
6738   (let* ((eobp (eobp))
6739          (article (or article (gnus-summary-article-number)))
6740          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6741          result)
6742     (when (and (not eobp)
6743                (or (not gnus-summary-check-current)
6744                    (not unread)
6745                    (not (gnus-data-unread-p (car data)))))
6746       (setq data (cdr data)))
6747     (when (setq result
6748                 (if unread
6749                     (progn
6750                       (while data
6751                         (unless (memq (gnus-data-number (car data))
6752                                       (cond
6753                                        ((eq gnus-auto-goto-ignores
6754                                             'always-undownloaded)
6755                                         gnus-newsgroup-undownloaded)
6756                                        (gnus-plugged
6757                                         nil)
6758                                        ((eq gnus-auto-goto-ignores
6759                                             'unfetched)
6760                                         gnus-newsgroup-unfetched)
6761                                        ((eq gnus-auto-goto-ignores
6762                                             'undownloaded)
6763                                         gnus-newsgroup-undownloaded)))
6764                           (when (gnus-data-unread-p (car data))
6765                             (setq result (car data)
6766                                   data nil)))
6767                         (setq data (cdr data)))
6768                       result)
6769                   (car data)))
6770       (goto-char (gnus-data-pos result))
6771       (gnus-data-number result))))
6772
6773 (defun gnus-summary-find-subject (subject &optional unread backward article)
6774   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6775          (article (or article (gnus-summary-article-number)))
6776          (articles (gnus-data-list backward))
6777          (arts (gnus-data-find-list article articles))
6778          result)
6779     (when (or (not gnus-summary-check-current)
6780               (not unread)
6781               (not (gnus-data-unread-p (car arts))))
6782       (setq arts (cdr arts)))
6783     (while arts
6784       (and (or (not unread)
6785                (gnus-data-unread-p (car arts)))
6786            (vectorp (gnus-data-header (car arts)))
6787            (gnus-subject-equal
6788             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6789            (setq result (car arts)
6790                  arts nil))
6791       (setq arts (cdr arts)))
6792     (and result
6793          (goto-char (gnus-data-pos result))
6794          (gnus-data-number result))))
6795
6796 (defun gnus-summary-search-forward (&optional unread subject backward)
6797   "Search forward for an article.
6798 If UNREAD, look for unread articles.  If SUBJECT, look for
6799 articles with that subject.  If BACKWARD, search backward instead."
6800   (cond (subject (gnus-summary-find-subject subject unread backward))
6801         (backward (gnus-summary-find-prev unread))
6802         (t (gnus-summary-find-next unread))))
6803
6804 (defun gnus-recenter (&optional n)
6805   "Center point in window and redisplay frame.
6806 Also do horizontal recentering."
6807   (interactive "P")
6808   (when (and gnus-auto-center-summary
6809              (not (eq gnus-auto-center-summary 'vertical)))
6810     (gnus-horizontal-recenter))
6811   (if (fboundp 'recenter-top-bottom)
6812       (recenter-top-bottom n)
6813     (recenter n)))
6814
6815 (put 'gnus-recenter 'isearch-scroll t)
6816
6817 (defun gnus-forward-line-ignore-invisible (n)
6818   "Move N lines forward (backward if N is negative).
6819 Like forward-line, but skip over (and don't count) invisible lines."
6820   (let (done)
6821     (while (and (> n 0) (not done))
6822       ;; If the following character is currently invisible,
6823       ;; skip all characters with that same `invisible' property value.
6824       (while (gnus-invisible-p (point))
6825         (goto-char (gnus-next-char-property-change (point))))
6826       (forward-line 1)
6827       (if (eobp)
6828           (setq done t)
6829         (setq n (1- n))))
6830     (while (and (< n 0) (not done))
6831       (forward-line -1)
6832       (if (bobp) (setq done t)
6833         (setq n (1+ n))
6834         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6835           (goto-char (gnus-previous-char-property-change (point))))))))
6836
6837 (defun gnus-summary-recenter ()
6838   "Center point in the summary window.
6839 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6840 displayed, no centering will be performed."
6841   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6842   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6843   (interactive)
6844   ;; The user has to want it.
6845   (when gnus-auto-center-summary
6846     (let* ((top (cond ((< (window-height) 4) 0)
6847                       ((< (window-height) 7) 1)
6848                       (t (if (numberp gnus-auto-center-summary)
6849                              gnus-auto-center-summary
6850                            (/ (1- (window-height)) 2)))))
6851            (height (1- (window-height)))
6852            (bottom (save-excursion
6853                      (goto-char (point-max))
6854                      (gnus-forward-line-ignore-invisible (- height))
6855                      (point)))
6856            (window (get-buffer-window (current-buffer))))
6857       (when (get-buffer-window gnus-article-buffer)
6858         ;; Only do recentering when the article buffer is displayed,
6859         ;; Set the window start to either `bottom', which is the biggest
6860         ;; possible valid number, or the second line from the top,
6861         ;; whichever is the least.
6862         (let ((top-pos (save-excursion
6863                          (gnus-forward-line-ignore-invisible (- top))
6864                          (point))))
6865           (if (> bottom top-pos)
6866               ;; Keep the second line from the top visible
6867               (set-window-start window top-pos)
6868             ;; Try to keep the bottom line visible; if it's partially
6869             ;; obscured, either scroll one more line to make it fully
6870             ;; visible, or revert to using TOP-POS.
6871             (save-excursion
6872               (goto-char (point-max))
6873               (gnus-forward-line-ignore-invisible -1)
6874               (let ((last-line-start (point)))
6875                 (goto-char bottom)
6876                 (set-window-start window (point) t)
6877                 (when (not (pos-visible-in-window-p last-line-start window))
6878                   (gnus-forward-line-ignore-invisible 1)
6879                   (set-window-start window (min (point) top-pos) t)))))))
6880       ;; Do horizontal recentering while we're at it.
6881       (when (and (get-buffer-window (current-buffer) t)
6882                  (not (eq gnus-auto-center-summary 'vertical)))
6883         (let ((selected (selected-window)))
6884           (select-window (get-buffer-window (current-buffer) t))
6885           (gnus-summary-position-point)
6886           (gnus-horizontal-recenter)
6887           (select-window selected))))))
6888
6889 (defun gnus-summary-jump-to-group (newsgroup)
6890   "Move point to NEWSGROUP in group mode buffer."
6891   ;; Keep update point of group mode buffer if visible.
6892   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6893       (save-window-excursion
6894         ;; Take care of tree window mode.
6895         (when (get-buffer-window gnus-group-buffer)
6896           (pop-to-buffer gnus-group-buffer))
6897         (gnus-group-jump-to-group newsgroup))
6898     (save-excursion
6899       ;; Take care of tree window mode.
6900       (if (get-buffer-window gnus-group-buffer 0)
6901           (pop-to-buffer gnus-group-buffer)
6902         (set-buffer gnus-group-buffer))
6903       (gnus-group-jump-to-group newsgroup))))
6904
6905 ;; This function returns a list of article numbers based on the
6906 ;; difference between the ranges of read articles in this group and
6907 ;; the range of active articles.
6908 (defun gnus-list-of-unread-articles (group)
6909   (let* ((read (gnus-info-read (gnus-get-info group)))
6910          (active (or (gnus-active group) (gnus-activate-group group)))
6911          (last (or (cdr active)
6912                    (error "Group %s couldn't be activated " group)))
6913          (bottom (if gnus-newsgroup-maximum-articles
6914                      (max (car active)
6915                           (- last gnus-newsgroup-maximum-articles -1))
6916                    (car active)))
6917          first nlast unread)
6918     ;; If none are read, then all are unread.
6919     (if (not read)
6920         (setq first bottom)
6921       ;; If the range of read articles is a single range, then the
6922       ;; first unread article is the article after the last read
6923       ;; article.  Sounds logical, doesn't it?
6924       (if (and (not (listp (cdr read)))
6925                (or (< (car read) bottom)
6926                    (progn (setq read (list read))
6927                           nil)))
6928           (setq first (max bottom (1+ (cdr read))))
6929         ;; `read' is a list of ranges.
6930         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6931                                   (caar read)))
6932                   1)
6933           (setq first bottom))
6934         (while read
6935           (when first
6936             (while (< first nlast)
6937               (setq unread (cons first unread)
6938                     first (1+ first))))
6939           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6940           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6941           (setq read (cdr read)))))
6942     ;; And add the last unread articles.
6943     (while (<= first last)
6944       (setq unread (cons first unread)
6945             first (1+ first)))
6946     ;; Return the list of unread articles.
6947     (delq 0 (nreverse unread))))
6948
6949 (defun gnus-list-of-read-articles (group)
6950   "Return a list of unread, unticked and non-dormant articles."
6951   (let* ((info (gnus-get-info group))
6952          (marked (gnus-info-marks info))
6953          (active (gnus-active group)))
6954     (and info active
6955          (gnus-list-range-difference
6956           (gnus-list-range-difference
6957            (gnus-sorted-complement
6958             (gnus-uncompress-range
6959              (if gnus-newsgroup-maximum-articles
6960                  (cons (max (car active)
6961                             (- (cdr active)
6962                                gnus-newsgroup-maximum-articles
6963                                -1))
6964                        (cdr active))
6965                active))
6966             (gnus-list-of-unread-articles group))
6967            (cdr (assq 'dormant marked)))
6968           (cdr (assq 'tick marked))))))
6969
6970 ;; This function returns a sequence of article numbers based on the
6971 ;; difference between the ranges of read articles in this group and
6972 ;; the range of active articles.
6973 (defun gnus-sequence-of-unread-articles (group)
6974   (let* ((read (gnus-info-read (gnus-get-info group)))
6975          (active (or (gnus-active group) (gnus-activate-group group)))
6976          (last (cdr active))
6977          (bottom (if gnus-newsgroup-maximum-articles
6978                      (max (car active)
6979                           (- last gnus-newsgroup-maximum-articles -1))
6980                    (car active)))
6981          first nlast unread)
6982     ;; If none are read, then all are unread.
6983     (if (not read)
6984         (setq first bottom)
6985       ;; If the range of read articles is a single range, then the
6986       ;; first unread article is the article after the last read
6987       ;; article.  Sounds logical, doesn't it?
6988       (if (and (not (listp (cdr read)))
6989                (or (< (car read) bottom)
6990                    (progn (setq read (list read))
6991                           nil)))
6992           (setq first (max bottom (1+ (cdr read))))
6993         ;; `read' is a list of ranges.
6994         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6995                                   (caar read)))
6996                   1)
6997           (setq first bottom))
6998         (while read
6999           (when first
7000             (push (cons first nlast) unread))
7001           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7002           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7003           (setq read (cdr read)))))
7004     ;; And add the last unread articles.
7005     (cond ((not (and first last))
7006            nil)
7007           ((< first last)
7008            (push (cons first last) unread))
7009           ((= first last)
7010            (push first unread)))
7011     ;; Return the sequence of unread articles.
7012     (delq 0 (nreverse unread))))
7013
7014 ;; Various summary commands
7015
7016 (defun gnus-summary-select-article-buffer ()
7017   "Reconfigure windows to show the article buffer.
7018 If `gnus-widen-article-buffer' is set, show only the article
7019 buffer."
7020   (interactive)
7021   (if (not (gnus-buffer-live-p gnus-article-buffer))
7022       (error "There is no article buffer for this summary buffer")
7023     (unless (get-buffer-window gnus-article-buffer)
7024       (gnus-summary-show-article))
7025     (gnus-configure-windows
7026      (if gnus-widen-article-window
7027          'only-article
7028        'article)
7029      t)
7030     (select-window (get-buffer-window gnus-article-buffer))))
7031
7032 (defun gnus-summary-universal-argument (arg)
7033   "Perform any operation on all articles that are process/prefixed."
7034   (interactive "P")
7035   (let ((articles (gnus-summary-work-articles arg))
7036         func article)
7037     (if (eq
7038          (setq
7039           func
7040           (key-binding
7041            (read-key-sequence
7042             (substitute-command-keys
7043              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7044          'undefined)
7045         (gnus-error 1 "Undefined key")
7046       (save-excursion
7047         (while articles
7048           (gnus-summary-goto-subject (setq article (pop articles)))
7049           (let (gnus-newsgroup-processable)
7050             (command-execute func))
7051           (gnus-summary-remove-process-mark article)))))
7052   (gnus-summary-position-point))
7053
7054 (defun gnus-summary-toggle-truncation (&optional arg)
7055   "Toggle truncation of summary lines.
7056 With ARG, turn line truncation on if ARG is positive."
7057   (interactive "P")
7058   (setq truncate-lines
7059         (if (null arg) (not truncate-lines)
7060           (> (prefix-numeric-value arg) 0)))
7061   (redraw-display))
7062
7063 (defun gnus-summary-find-for-reselect ()
7064   "Return the number of an article to stay on across a reselect.
7065 The current article is considered, then following articles, then previous
7066 articles.  An article is sought which is not cancelled and isn't a temporary
7067 insertion from another group.  If there's no such then return a dummy 0."
7068   (let (found)
7069     (dolist (rev '(nil t))
7070       (unless found      ; don't demand the reverse list if we don't need it
7071         (let ((data (gnus-data-find-list
7072                      (gnus-summary-article-number) (gnus-data-list rev))))
7073           (while (and data (not found))
7074             (if (and (< 0 (gnus-data-number (car data)))
7075                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7076                 (setq found (gnus-data-number (car data))))
7077             (setq data (cdr data))))))
7078     (or found 0)))
7079
7080 (defun gnus-summary-reselect-current-group (&optional all rescan)
7081   "Exit and then reselect the current newsgroup.
7082 The prefix argument ALL means to select all articles."
7083   (interactive "P")
7084   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7085     (error "Ephemeral groups can't be reselected"))
7086   (let ((current-subject (gnus-summary-find-for-reselect))
7087         (group gnus-newsgroup-name))
7088     (setq gnus-newsgroup-begin nil)
7089     (gnus-summary-exit nil 'leave-hidden)
7090     ;; We have to adjust the point of group mode buffer because
7091     ;; point was moved to the next unread newsgroup by exiting.
7092     (gnus-summary-jump-to-group group)
7093     (when rescan
7094       (save-excursion
7095         (gnus-group-get-new-news-this-group 1)))
7096     (gnus-group-read-group all t)
7097     (gnus-summary-goto-subject current-subject nil t)))
7098
7099 (defun gnus-summary-rescan-group (&optional all)
7100   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7101   (interactive "P")
7102   (let ((config gnus-current-window-configuration))
7103     (gnus-summary-reselect-current-group all t)
7104     (gnus-configure-windows config)
7105     (when (eq config 'article)
7106       (gnus-summary-select-article))))
7107
7108 (defun gnus-summary-update-info (&optional non-destructive)
7109   (save-excursion
7110     (let ((group gnus-newsgroup-name))
7111       (when group
7112         (when gnus-newsgroup-kill-headers
7113           (setq gnus-newsgroup-killed
7114                 (gnus-compress-sequence
7115                  (gnus-sorted-union
7116                   (gnus-list-range-intersection
7117                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7118                   gnus-newsgroup-unreads)
7119                  t)))
7120         (unless (listp (cdr gnus-newsgroup-killed))
7121           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7122         (let ((headers gnus-newsgroup-headers))
7123           ;; Set the new ranges of read articles.
7124           (with-current-buffer gnus-group-buffer
7125             (gnus-undo-force-boundary))
7126           (gnus-update-read-articles
7127            group (gnus-sorted-union
7128                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7129           ;; Set the current article marks.
7130           (let ((gnus-newsgroup-scored
7131                  (if (and (not gnus-save-score)
7132                           (not non-destructive))
7133                      nil
7134                    gnus-newsgroup-scored)))
7135             (save-excursion
7136               (gnus-update-marks)))
7137           ;; Do the cross-ref thing.
7138           (when gnus-use-cross-reference
7139             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7140           ;; Do not switch windows but change the buffer to work.
7141           (set-buffer gnus-group-buffer)
7142           (unless (gnus-ephemeral-group-p group)
7143             (gnus-group-update-group group)))))))
7144
7145 (defun gnus-summary-save-newsrc (&optional force)
7146   "Save the current number of read/marked articles in the dribble buffer.
7147 The dribble buffer will then be saved.
7148 If FORCE (the prefix), also save the .newsrc file(s)."
7149   (interactive "P")
7150   (gnus-summary-update-info t)
7151   (if force
7152       (gnus-save-newsrc-file)
7153     (gnus-dribble-save)))
7154
7155 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7156
7157 (defun gnus-summary-exit (&optional temporary leave-hidden)
7158   "Exit reading current newsgroup, and then return to group selection mode.
7159 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7160   (interactive)
7161   (gnus-set-global-variables)
7162   (when (gnus-buffer-live-p gnus-article-buffer)
7163     (with-current-buffer gnus-article-buffer
7164       (mm-destroy-parts gnus-article-mime-handles)
7165       ;; Set it to nil for safety reason.
7166       (setq gnus-article-mime-handle-alist nil)
7167       (setq gnus-article-mime-handles nil)))
7168   (gnus-kill-save-kill-buffer)
7169   (gnus-async-halt-prefetch)
7170   (let* ((group gnus-newsgroup-name)
7171          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7172          (gnus-group-is-exiting-p t)
7173          (mode major-mode)
7174          (group-point nil)
7175          (buf (current-buffer)))
7176     (unless quit-config
7177       ;; Do adaptive scoring, and possibly save score files.
7178       (when gnus-newsgroup-adaptive
7179         (gnus-score-adaptive))
7180       (when gnus-use-scoring
7181         (gnus-score-save)))
7182     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7183     (when gnus-use-cache
7184       (gnus-cache-possibly-remove-articles)
7185       (gnus-cache-save-buffers))
7186     (gnus-async-prefetch-remove-group group)
7187     (when gnus-suppress-duplicates
7188       (gnus-dup-enter-articles))
7189     (when gnus-use-trees
7190       (gnus-tree-close group))
7191     (when gnus-use-cache
7192       (gnus-cache-write-active))
7193     ;; Remove entries for this group.
7194     (nnmail-purge-split-history (gnus-group-real-name group))
7195     ;; Make all changes in this group permanent.
7196     (unless quit-config
7197       (gnus-run-hooks 'gnus-exit-group-hook)
7198       (gnus-summary-update-info))
7199     (gnus-close-group group)
7200     ;; Make sure where we were, and go to next newsgroup.
7201     (set-buffer gnus-group-buffer)
7202     (unless quit-config
7203       (gnus-group-jump-to-group group))
7204     (gnus-run-hooks 'gnus-summary-exit-hook)
7205     (unless (or quit-config
7206                 (not gnus-summary-next-group-on-exit)
7207                 ;; If this group has disappeared from the summary
7208                 ;; buffer, don't skip forwards.
7209                 (not (string= group (gnus-group-group-name))))
7210       (gnus-group-next-unread-group 1))
7211     (setq group-point (point))
7212     (if temporary
7213         nil                             ;Nothing to do.
7214       (set-buffer buf)
7215       (if (not gnus-kill-summary-on-exit)
7216           (progn
7217             (gnus-deaden-summary)
7218             (setq mode nil))
7219         (when (get-buffer gnus-article-buffer)
7220           (bury-buffer gnus-article-buffer))
7221         ;; Return to group mode buffer.
7222         (when (eq mode 'gnus-summary-mode)
7223           (gnus-kill-buffer buf)))
7224
7225       (setq gnus-current-select-method gnus-select-method)
7226       (set-buffer gnus-group-buffer)
7227       (if quit-config
7228           (gnus-handle-ephemeral-exit quit-config)
7229         (goto-char group-point)
7230         ;; If gnus-group-buffer is already displayed, make sure we also move
7231         ;; the cursor in the window that displays it.
7232         (let ((win (get-buffer-window (current-buffer) 0)))
7233           (if win (set-window-point win (point))))
7234         (unless leave-hidden
7235           (gnus-configure-windows 'group 'force)))
7236
7237       ;; If we have several article buffers, we kill them at exit.
7238       (unless gnus-single-article-buffer
7239         (when (gnus-buffer-live-p gnus-article-buffer)
7240           (with-current-buffer gnus-article-buffer
7241             ;; Don't kill sticky article buffers
7242             (unless (eq major-mode 'gnus-sticky-article-mode)
7243               (gnus-kill-buffer gnus-article-buffer)
7244               (setq gnus-article-current nil))))
7245         (gnus-kill-buffer gnus-original-article-buffer))
7246
7247       ;; Clear the current group name.
7248       (unless quit-config
7249         (setq gnus-newsgroup-name nil)))))
7250
7251 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7252 (defun gnus-summary-exit-no-update (&optional no-questions)
7253   "Quit reading current newsgroup without updating read article info."
7254   (interactive)
7255   (let* ((group gnus-newsgroup-name)
7256          (gnus-group-is-exiting-p t)
7257          (gnus-group-is-exiting-without-update-p t)
7258          (quit-config (gnus-group-quit-config group)))
7259     (when (or no-questions
7260               gnus-expert-user
7261               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7262       (gnus-async-halt-prefetch)
7263       (run-hooks 'gnus-summary-prepare-exit-hook)
7264       (when (gnus-buffer-live-p gnus-article-buffer)
7265         (with-current-buffer gnus-article-buffer
7266           (mm-destroy-parts gnus-article-mime-handles)
7267           ;; Set it to nil for safety reason.
7268           (setq gnus-article-mime-handle-alist nil)
7269           (setq gnus-article-mime-handles nil)))
7270       ;; If we have several article buffers, we kill them at exit.
7271       (unless gnus-single-article-buffer
7272         (gnus-kill-buffer gnus-article-buffer)
7273         (gnus-kill-buffer gnus-original-article-buffer)
7274         (setq gnus-article-current nil))
7275       ;; Return to the group buffer.
7276       (gnus-configure-windows 'group 'force)
7277       (if (not gnus-kill-summary-on-exit)
7278           (gnus-deaden-summary)
7279         (gnus-close-group group)
7280         (gnus-kill-buffer gnus-summary-buffer))
7281       (unless gnus-single-article-buffer
7282         (setq gnus-article-current nil))
7283       (when gnus-use-trees
7284         (gnus-tree-close group))
7285       (gnus-async-prefetch-remove-group group)
7286       (when (get-buffer gnus-article-buffer)
7287         (bury-buffer gnus-article-buffer))
7288       ;; Clear the current group name.
7289       (setq gnus-newsgroup-name nil)
7290       (unless (gnus-ephemeral-group-p group)
7291         (gnus-group-update-group group))
7292       (when (equal (gnus-group-group-name) group)
7293         (gnus-group-next-unread-group 1))
7294       (when quit-config
7295         (gnus-handle-ephemeral-exit quit-config)))))
7296
7297 (defun gnus-handle-ephemeral-exit (quit-config)
7298   "Handle movement when leaving an ephemeral group.
7299 The state which existed when entering the ephemeral is reset."
7300   (if (not (buffer-name (car quit-config)))
7301       (gnus-configure-windows 'group 'force)
7302     (set-buffer (car quit-config))
7303     (cond ((eq major-mode 'gnus-summary-mode)
7304            (gnus-set-global-variables))
7305           ((eq major-mode 'gnus-article-mode)
7306            (save-current-buffer
7307              ;; The `gnus-summary-buffer' variable may point
7308              ;; to the old summary buffer when using a single
7309              ;; article buffer.
7310              (unless (gnus-buffer-live-p gnus-summary-buffer)
7311                (set-buffer gnus-group-buffer))
7312              (set-buffer gnus-summary-buffer)
7313              (gnus-set-global-variables))))
7314     (if (or (eq (cdr quit-config) 'article)
7315             (eq (cdr quit-config) 'pick))
7316         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7317             (gnus-configure-windows 'pick 'force)
7318           (gnus-configure-windows (cdr quit-config) 'force))
7319       (gnus-configure-windows (cdr quit-config) 'force))
7320     (when (eq major-mode 'gnus-summary-mode)
7321       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7322                                                      next-unread-noselect))
7323           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7324                                   'next-noselect)
7325                               (gnus-summary-next-subject 1 nil t))
7326                              ((eq gnus-auto-select-on-ephemeral-exit
7327                                   'next-unread-noselect)
7328                               (gnus-summary-next-subject 1 t t))))
7329             ;; Hide the article buffer which displays the article different
7330             ;; from the one that the cursor points to in the summary buffer.
7331             (gnus-configure-windows 'summary 'force))
7332         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7333                (gnus-summary-next-subject 1))
7334               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7335                (gnus-summary-next-subject 1 t))))
7336       (gnus-summary-recenter)
7337       (gnus-summary-position-point))))
7338
7339 ;;; Dead summaries.
7340
7341 (defvar gnus-dead-summary-mode-map
7342   (let ((map (make-keymap)))
7343     (suppress-keymap map)
7344     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7345     (dolist (key '("\C-d" "\r" "\177" [delete]))
7346       (define-key map key 'gnus-summary-wake-up-the-dead))
7347     (dolist (key '("q" "Q"))
7348       (define-key map key 'bury-buffer))
7349     map))
7350
7351 (define-minor-mode gnus-dead-summary-mode
7352   "Minor mode for Gnus summary buffers."
7353   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7354   (unless (derived-mode-p 'gnus-summary-mode)
7355     (setq gnus-dead-summary-mode nil)))
7356
7357 (defun gnus-deaden-summary ()
7358   "Make the current summary buffer into a dead summary buffer."
7359   ;; Kill any previous dead summary buffer.
7360   (when (and gnus-dead-summary
7361              (buffer-name gnus-dead-summary))
7362     (with-current-buffer gnus-dead-summary
7363       (when gnus-dead-summary-mode
7364         (kill-buffer (current-buffer)))))
7365   ;; Make this the current dead summary.
7366   (setq gnus-dead-summary (current-buffer))
7367   (gnus-dead-summary-mode 1)
7368   (let ((name (buffer-name)))
7369     (when (string-match "Summary" name)
7370       (rename-buffer
7371        (concat (substring name 0 (match-beginning 0)) "Dead "
7372                (substring name (match-beginning 0)))
7373        t)
7374       (bury-buffer))))
7375
7376 (defun gnus-kill-or-deaden-summary (buffer)
7377   "Kill or deaden the summary BUFFER."
7378   (save-excursion
7379     (when (and (buffer-name buffer)
7380                (not gnus-single-article-buffer))
7381       (with-current-buffer buffer
7382         (gnus-kill-buffer gnus-article-buffer)
7383         (gnus-kill-buffer gnus-original-article-buffer)))
7384     (cond
7385      ;; Kill the buffer.
7386      (gnus-kill-summary-on-exit
7387       (when (and gnus-use-trees
7388                  (gnus-buffer-exists-p buffer))
7389         (with-current-buffer buffer
7390           (gnus-tree-close gnus-newsgroup-name)))
7391       (gnus-kill-buffer buffer))
7392      ;; Deaden the buffer.
7393      ((gnus-buffer-exists-p buffer)
7394       (with-current-buffer buffer
7395         (gnus-deaden-summary))))))
7396
7397 (defun gnus-summary-wake-up-the-dead (&rest args)
7398   "Wake up the dead summary buffer."
7399   (interactive)
7400   (gnus-dead-summary-mode -1)
7401   (let ((name (buffer-name)))
7402     (when (string-match "Dead " name)
7403       (rename-buffer
7404        (concat (substring name 0 (match-beginning 0))
7405                (substring name (match-end 0)))
7406        t)))
7407   (gnus-message 3 "This dead summary is now alive again"))
7408
7409 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7410 (defun gnus-summary-describe-group (&optional force)
7411   "Describe the current newsgroup."
7412   (interactive "P")
7413   (gnus-group-describe-group force gnus-newsgroup-name))
7414
7415 (defun gnus-summary-describe-briefly ()
7416   "Describe summary mode commands briefly."
7417   (interactive)
7418   (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")))
7419
7420 ;; Walking around group mode buffer from summary mode.
7421
7422 (defun gnus-summary-next-group (&optional no-article target-group backward)
7423   "Exit current newsgroup and then select next unread newsgroup.
7424 If prefix argument NO-ARTICLE is non-nil, no article is selected
7425 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7426 previous group instead."
7427   (interactive "P")
7428   ;; Stop pre-fetching.
7429   (gnus-async-halt-prefetch)
7430   (let ((current-group gnus-newsgroup-name)
7431         (current-buffer (current-buffer))
7432         entered)
7433     ;; First we semi-exit this group to update Xrefs and all variables.
7434     ;; We can't do a real exit, because the window conf must remain
7435     ;; the same in case the user is prompted for info, and we don't
7436     ;; want the window conf to change before that...
7437     (gnus-summary-exit t)
7438     (while (not entered)
7439       ;; Then we find what group we are supposed to enter.
7440       (set-buffer gnus-group-buffer)
7441       (gnus-group-jump-to-group current-group)
7442       (setq target-group
7443             (or target-group
7444                 (if (eq gnus-keep-same-level 'best)
7445                     (gnus-summary-best-group gnus-newsgroup-name)
7446                   (gnus-summary-search-group backward gnus-keep-same-level))))
7447       (if (not target-group)
7448           ;; There are no further groups, so we return to the group
7449           ;; buffer.
7450           (progn
7451             (gnus-message 5 "Returning to the group buffer")
7452             (setq entered t)
7453             (when (gnus-buffer-live-p current-buffer)
7454               (set-buffer current-buffer)
7455               (gnus-summary-exit))
7456             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7457         ;; We try to enter the target group.
7458         (gnus-group-jump-to-group target-group)
7459         (let ((unreads (gnus-group-group-unread)))
7460           (if (and (or (eq t unreads)
7461                        (and unreads (not (zerop unreads))))
7462                    (gnus-summary-read-group
7463                     target-group nil no-article
7464                     (and (buffer-name current-buffer) current-buffer)
7465                     nil backward))
7466               (setq entered t)
7467             (setq current-group target-group
7468                   target-group nil)))))))
7469
7470 (defun gnus-summary-prev-group (&optional no-article)
7471   "Exit current newsgroup and then select previous unread newsgroup.
7472 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7473   (interactive "P")
7474   (gnus-summary-next-group no-article nil t))
7475
7476 ;; Walking around summary lines.
7477
7478 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7479   "Go to the first subject satisfying any non-nil constraint.
7480 If UNREAD is non-nil, the article should be unread.
7481 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7482 If UNSEEN is non-nil, the article should be unseen as well as unread.
7483 Returns the article selected or nil if there are no matching articles."
7484   (interactive "P")
7485   (cond
7486    ;; Empty summary.
7487    ((null gnus-newsgroup-data)
7488     (gnus-message 3 "No articles in the group")
7489     nil)
7490    ;; Pick the first article.
7491    ((not (or unread undownloaded unseen))
7492     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7493     (gnus-data-number (car gnus-newsgroup-data)))
7494    ;; Find the first unread article.
7495    (t
7496     (let ((data gnus-newsgroup-data))
7497       (while (and data
7498                   (let ((num (gnus-data-number (car data))))
7499                     (or (memq num gnus-newsgroup-unfetched)
7500                         (not (or (and unread
7501                                       (memq num gnus-newsgroup-unreads))
7502                                  (and undownloaded
7503                                       (memq num gnus-newsgroup-undownloaded))
7504                                  (and unseen
7505                                       (memq num gnus-newsgroup-unseen)
7506                                       (memq num gnus-newsgroup-unreads)))))))
7507         (setq data (cdr data)))
7508       (prog1
7509           (if data
7510               (progn
7511                 (goto-char (gnus-data-pos (car data)))
7512                 (gnus-data-number (car data)))
7513             (gnus-message 3 "No more%s articles"
7514                           (let* ((r (when unread " unread"))
7515                                  (d (when undownloaded " undownloaded"))
7516                                  (s (when unseen " unseen"))
7517                                  (l (delq nil (list r d s))))
7518                             (cond ((= 3 (length l))
7519                                    (concat r "," d ", or" s))
7520                                   ((= 2 (length l))
7521                                    (concat (car l) ", or" (cadr l)))
7522                                   ((= 1 (length l))
7523                                    (car l))
7524                                   (t
7525                                    ""))))
7526             nil
7527             )
7528         (gnus-summary-position-point))))))
7529
7530 (defun gnus-summary-next-subject (n &optional unread dont-display)
7531   "Go to next N'th summary line.
7532 If N is negative, go to the previous N'th subject line.
7533 If UNREAD is non-nil, only unread articles are selected.
7534 The difference between N and the actual number of steps taken is
7535 returned."
7536   (interactive "p")
7537   (let ((backward (< n 0))
7538         (n (abs n)))
7539     (while (and (> n 0)
7540                 (if backward
7541                     (gnus-summary-find-prev unread)
7542                   (gnus-summary-find-next unread)))
7543       (unless (zerop (setq n (1- n)))
7544         (gnus-summary-show-thread)))
7545     (when (/= 0 n)
7546       (gnus-message 7 "No more%s articles"
7547                     (if unread " unread" "")))
7548     (unless dont-display
7549       (gnus-summary-recenter)
7550       (gnus-summary-position-point))
7551     n))
7552
7553 (defun gnus-summary-next-unread-subject (n)
7554   "Go to next N'th unread summary line."
7555   (interactive "p")
7556   (gnus-summary-next-subject n t))
7557
7558 (defun gnus-summary-prev-subject (n &optional unread)
7559   "Go to previous N'th summary line.
7560 If optional argument UNREAD is non-nil, only unread article is selected."
7561   (interactive "p")
7562   (gnus-summary-next-subject (- n) unread))
7563
7564 (defun gnus-summary-prev-unread-subject (n)
7565   "Go to previous N'th unread summary line."
7566   (interactive "p")
7567   (gnus-summary-next-subject (- n) t))
7568
7569 (defun gnus-summary-goto-subjects (articles)
7570   "Insert the subject header for ARTICLES in the current buffer."
7571   (save-excursion
7572     (dolist (article articles)
7573       (gnus-summary-goto-subject article t)))
7574   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7575   (gnus-summary-position-point))
7576
7577 (defun gnus-summary-goto-subject (article &optional force silent)
7578   "Go to the subject line of ARTICLE.
7579 If FORCE, also allow jumping to articles not currently shown."
7580   (interactive "nArticle number: ")
7581   (unless (numberp article)
7582     (error "Article %s is not a number" article))
7583   (let ((b (point))
7584         (data (gnus-data-find article)))
7585     ;; We read in the article if we have to.
7586     (and (not data)
7587          force
7588          (gnus-summary-insert-subject
7589           article
7590           (if (or (numberp force) (vectorp force)) force)
7591           t)
7592          (setq data (gnus-data-find article)))
7593     (goto-char b)
7594     (if (not data)
7595         (progn
7596           (unless silent
7597             (gnus-message 3 "Can't find article %d" article))
7598           nil)
7599       (let ((pt (gnus-data-pos data)))
7600         (goto-char pt)
7601         (gnus-summary-set-article-display-arrow pt))
7602       (gnus-summary-position-point)
7603       article)))
7604
7605 ;; Walking around summary lines with displaying articles.
7606
7607 (defun gnus-summary-expand-window (&optional arg)
7608   "Make the summary buffer take up the entire Emacs frame.
7609 Given a prefix, will force an `article' buffer configuration."
7610   (interactive "P")
7611   (if arg
7612       (gnus-configure-windows 'article 'force)
7613     (gnus-configure-windows 'summary 'force)))
7614
7615 (defun gnus-summary-display-article (article &optional all-header)
7616   "Display ARTICLE in article buffer."
7617   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7618                (with-current-buffer gnus-article-buffer
7619                  (eq major-mode 'gnus-article-mode)))
7620     (gnus-article-setup-buffer))
7621   (gnus-set-global-variables)
7622   (with-current-buffer gnus-article-buffer
7623     (setq gnus-article-charset gnus-newsgroup-charset)
7624     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7625     (mm-enable-multibyte))
7626   (if (null article)
7627       nil
7628     (prog1
7629         (if gnus-summary-display-article-function
7630             (funcall gnus-summary-display-article-function article all-header)
7631           (gnus-article-prepare article all-header))
7632       (gnus-run-hooks 'gnus-select-article-hook)
7633       (when (and gnus-current-article
7634                  (not (zerop gnus-current-article)))
7635         (gnus-summary-goto-subject gnus-current-article))
7636       (gnus-summary-recenter)
7637       (when (and gnus-use-trees gnus-show-threads)
7638         (gnus-possibly-generate-tree article)
7639         (gnus-highlight-selected-tree article))
7640       ;; Successfully display article.
7641       (gnus-article-set-window-start
7642        (cdr (assq article gnus-newsgroup-bookmarks))))))
7643
7644 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7645   "Select the current article.
7646 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7647 non-nil, the article will be re-fetched even if it already present in
7648 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7649 be displayed."
7650   ;; Make sure we are in the summary buffer to work around bbdb bug.
7651   (unless (eq major-mode 'gnus-summary-mode)
7652     (set-buffer gnus-summary-buffer))
7653   (let ((article (or article (gnus-summary-article-number)))
7654         (all-headers (not (not all-headers))) ;Must be t or nil.
7655         gnus-summary-display-article-function)
7656     (and (not pseudo)
7657          (gnus-summary-article-pseudo-p article)
7658          (error "This is a pseudo-article"))
7659     (with-current-buffer gnus-summary-buffer
7660       (if (or (and gnus-single-article-buffer
7661                    (or (null gnus-current-article)
7662                        (null gnus-article-current)
7663                        (null (get-buffer gnus-article-buffer))
7664                        (not (eq article (cdr gnus-article-current)))
7665                        (not (equal (car gnus-article-current)
7666                                    gnus-newsgroup-name))
7667                        (not (get-buffer gnus-original-article-buffer))))
7668               (and (not gnus-single-article-buffer)
7669                    (or (null gnus-current-article)
7670                        (not (get-buffer gnus-original-article-buffer))
7671                        (not (eq gnus-current-article article))))
7672               force)
7673           ;; The requested article is different from the current article.
7674           (progn
7675             (gnus-summary-display-article article all-headers)
7676             (when (gnus-buffer-live-p gnus-article-buffer)
7677               (with-current-buffer gnus-article-buffer
7678                 (if (not gnus-article-decoded-p) ;; a local variable
7679                     (mm-disable-multibyte))))
7680             (gnus-article-set-window-start
7681              (cdr (assq article gnus-newsgroup-bookmarks)))
7682             article)
7683         'old))))
7684
7685 (defun gnus-summary-force-verify-and-decrypt ()
7686   "Display buttons for signed/encrypted parts and verify/decrypt them."
7687   (interactive)
7688   (let ((mm-verify-option 'known)
7689         (mm-decrypt-option 'known)
7690         (gnus-article-emulate-mime t)
7691         (gnus-buttonized-mime-types (append (list "multipart/signed"
7692                                                   "multipart/encrypted")
7693                                             gnus-buttonized-mime-types)))
7694     (gnus-summary-select-article nil 'force)))
7695
7696 (defun gnus-summary-set-current-mark (&optional current-mark)
7697   "Obsolete function."
7698   nil)
7699
7700 (defun gnus-summary-next-article (&optional unread subject backward push)
7701   "Select the next article.
7702 If UNREAD, only unread articles are selected.
7703 If SUBJECT, only articles with SUBJECT are selected.
7704 If BACKWARD, the previous article is selected instead of the next."
7705   (interactive "P")
7706   ;; Make sure we are in the summary buffer.
7707   (unless (eq major-mode 'gnus-summary-mode)
7708     (set-buffer gnus-summary-buffer))
7709   (cond
7710    ;; Is there such an article?
7711    ((and (gnus-summary-search-forward unread subject backward)
7712          (or (gnus-summary-display-article (gnus-summary-article-number))
7713              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7714     (gnus-summary-position-point))
7715    ;; If not, we try the first unread, if that is wanted.
7716    ((and subject
7717          gnus-auto-select-same
7718          (gnus-summary-first-unread-article))
7719     (gnus-summary-position-point)
7720     (gnus-message 6 "Wrapped"))
7721    ;; Try to get next/previous article not displayed in this group.
7722    ((and gnus-auto-extend-newsgroup
7723          (not unread) (not subject))
7724     (gnus-summary-goto-article
7725      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7726      nil (count-lines (point-min) (point))))
7727    ;; Go to next/previous group.
7728    (t
7729     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7730       (gnus-summary-jump-to-group gnus-newsgroup-name))
7731     (let ((cmd (if (featurep 'xemacs)
7732                    last-command-char
7733                  last-command-event))
7734           (point
7735            (with-current-buffer gnus-group-buffer
7736              (point)))
7737           (group
7738            (if (eq gnus-keep-same-level 'best)
7739                (gnus-summary-best-group gnus-newsgroup-name)
7740              (gnus-summary-search-group backward gnus-keep-same-level))))
7741       ;; Select next unread newsgroup automagically.
7742       (cond
7743        ((or (not gnus-auto-select-next)
7744             (not cmd))
7745         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7746        ((or (eq gnus-auto-select-next 'quietly)
7747             (and (eq gnus-auto-select-next 'slightly-quietly)
7748                  push)
7749             (and (eq gnus-auto-select-next 'almost-quietly)
7750                  (gnus-summary-last-article-p)))
7751         ;; Select quietly.
7752         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7753             (gnus-summary-exit)
7754           (gnus-message 7 "No more%s articles (%s)..."
7755                         (if unread " unread" "")
7756                         (if group (concat "selecting " group)
7757                           "exiting"))
7758           (gnus-summary-next-group nil group backward)))
7759        (t
7760         (when (gnus-key-press-event-p last-input-event)
7761           (gnus-summary-walk-group-buffer
7762            gnus-newsgroup-name cmd unread backward point))))))))
7763
7764 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7765   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7766                       (?\C-p (gnus-group-prev-unread-group 1))))
7767         (cursor-in-echo-area t)
7768         keve key group ended prompt)
7769     (with-current-buffer gnus-group-buffer
7770       (goto-char start)
7771       (setq group
7772             (if (eq gnus-keep-same-level 'best)
7773                 (gnus-summary-best-group gnus-newsgroup-name)
7774               (gnus-summary-search-group backward gnus-keep-same-level))))
7775     (while (not ended)
7776       (setq prompt
7777             (format
7778              "No more%s articles%s " (if unread " unread" "")
7779              (if (and group
7780                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7781                  (format " (Type %s for %s [%s])"
7782                          (single-key-description cmd)
7783                          (gnus-group-decoded-name group)
7784                          (gnus-group-unread group))
7785                (format " (Type %s to exit %s)"
7786                        (single-key-description cmd)
7787                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7788       ;; Confirm auto selection.
7789       (setq key (car (setq keve (gnus-read-event-char prompt)))
7790             ended t)
7791       (cond
7792        ((assq key keystrokes)
7793         (let ((obuf (current-buffer)))
7794           (switch-to-buffer gnus-group-buffer)
7795           (when group
7796             (gnus-group-jump-to-group group))
7797           (eval (cadr (assq key keystrokes)))
7798           (setq group (gnus-group-group-name))
7799           (switch-to-buffer obuf))
7800         (setq ended nil))
7801        ((equal key cmd)
7802         (if (or (not group)
7803                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7804             (gnus-summary-exit)
7805           (gnus-summary-next-group nil group backward)))
7806        (t
7807         (push (cdr keve) unread-command-events))))))
7808
7809 (defun gnus-summary-next-unread-article ()
7810   "Select unread article after current one."
7811   (interactive)
7812   (gnus-summary-next-article
7813    (or (not (eq gnus-summary-goto-unread 'never))
7814        (gnus-summary-last-article-p (gnus-summary-article-number)))
7815    (and gnus-auto-select-same
7816         (gnus-summary-article-subject))))
7817
7818 (defun gnus-summary-prev-article (&optional unread subject)
7819   "Select the article before the current one.
7820 If UNREAD is non-nil, only unread articles are selected."
7821   (interactive "P")
7822   (gnus-summary-next-article unread subject t))
7823
7824 (defun gnus-summary-prev-unread-article ()
7825   "Select unread article before current one."
7826   (interactive)
7827   (gnus-summary-prev-article
7828    (or (not (eq gnus-summary-goto-unread 'never))
7829        (gnus-summary-first-article-p (gnus-summary-article-number)))
7830    (and gnus-auto-select-same
7831         (gnus-summary-article-subject))))
7832
7833 (defun gnus-summary-next-page (&optional lines circular stop)
7834   "Show next page of the selected article.
7835 If at the end of the current article, select the next article.
7836 LINES says how many lines should be scrolled up.
7837
7838 If CIRCULAR is non-nil, go to the start of the article instead of
7839 selecting the next article when reaching the end of the current
7840 article.
7841
7842 If STOP is non-nil, just stop when reaching the end of the message.
7843
7844 Also see the variable `gnus-article-skip-boring'."
7845   (interactive "P")
7846   (setq gnus-summary-buffer (current-buffer))
7847   (gnus-set-global-variables)
7848   (let ((article (gnus-summary-article-number))
7849         (article-window (get-buffer-window gnus-article-buffer t))
7850         endp)
7851     ;; If the buffer is empty, we have no article.
7852     (unless article
7853       (error "No article to select"))
7854     (gnus-configure-windows 'article)
7855     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7856         (if (and (eq gnus-summary-goto-unread 'never)
7857                  (not (gnus-summary-last-article-p article)))
7858             (gnus-summary-next-article)
7859           (gnus-summary-next-unread-article))
7860       (if (or (null gnus-current-article)
7861               (null gnus-article-current)
7862               (/= article (cdr gnus-article-current))
7863               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7864           ;; Selected subject is different from current article's.
7865           (gnus-summary-display-article article)
7866         (when article-window
7867           (gnus-eval-in-buffer-window gnus-article-buffer
7868             (setq endp (or (gnus-article-next-page lines)
7869                            (gnus-article-only-boring-p))))
7870           (when endp
7871             (cond ((or stop gnus-summary-stop-at-end-of-message)
7872                    (gnus-message 3 "End of message"))
7873                   (circular
7874                    (gnus-summary-beginning-of-article))
7875                   (lines
7876                    (gnus-message 3 "End of message"))
7877                   ((null lines)
7878                    (if (and (eq gnus-summary-goto-unread 'never)
7879                             (not (gnus-summary-last-article-p article)))
7880                        (gnus-summary-next-article)
7881                      (gnus-summary-next-unread-article))))))))
7882     (gnus-summary-recenter)
7883     (gnus-summary-position-point)))
7884
7885 (defun gnus-summary-prev-page (&optional lines move)
7886   "Show previous page of selected article.
7887 Argument LINES specifies lines to be scrolled down.
7888 If MOVE, move to the previous unread article if point is at
7889 the beginning of the buffer."
7890   (interactive "P")
7891   (let ((article (gnus-summary-article-number))
7892         (article-window (get-buffer-window gnus-article-buffer t))
7893         endp)
7894     (gnus-configure-windows 'article)
7895     (if (or (null gnus-current-article)
7896             (null gnus-article-current)
7897             (/= article (cdr gnus-article-current))
7898             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7899         ;; Selected subject is different from current article's.
7900         (gnus-summary-display-article article)
7901       (gnus-summary-recenter)
7902       (when article-window
7903         (gnus-eval-in-buffer-window gnus-article-buffer
7904           (setq endp (gnus-article-prev-page lines)))
7905         (when (and move endp)
7906           (cond (lines
7907                  (gnus-message 3 "Beginning of message"))
7908                 ((null lines)
7909                  (if (and (eq gnus-summary-goto-unread 'never)
7910                           (not (gnus-summary-first-article-p article)))
7911                      (gnus-summary-prev-article)
7912                    (gnus-summary-prev-unread-article))))))))
7913   (gnus-summary-position-point))
7914
7915 (defun gnus-summary-prev-page-or-article (&optional lines)
7916   "Show previous page of selected article.
7917 Argument LINES specifies lines to be scrolled down.
7918 If at the beginning of the article, go to the next article."
7919   (interactive "P")
7920   (gnus-summary-prev-page lines t))
7921
7922 (defun gnus-summary-scroll-up (lines)
7923   "Scroll up (or down) one line current article.
7924 Argument LINES specifies lines to be scrolled up (or down if negative).
7925 If no article is selected, then the current article will be selected first."
7926   (interactive "p")
7927   (gnus-configure-windows 'article)
7928   (gnus-summary-show-thread)
7929   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7930     (gnus-eval-in-buffer-window gnus-article-buffer
7931       (cond ((> lines 0)
7932              (when (gnus-article-next-page lines)
7933                (gnus-message 3 "End of message")))
7934             ((< lines 0)
7935              (gnus-article-prev-page (- lines))))))
7936   (gnus-summary-recenter)
7937   (gnus-summary-position-point))
7938
7939 (defun gnus-summary-scroll-down (lines)
7940   "Scroll down (or up) one line current article.
7941 Argument LINES specifies lines to be scrolled down (or up if negative).
7942 If no article is selected, then the current article will be selected first."
7943   (interactive "p")
7944   (gnus-summary-scroll-up (- lines)))
7945
7946 (defun gnus-summary-next-same-subject ()
7947   "Select next article which has the same subject as current one."
7948   (interactive)
7949   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7950
7951 (defun gnus-summary-prev-same-subject ()
7952   "Select previous article which has the same subject as current one."
7953   (interactive)
7954   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7955
7956 (defun gnus-summary-next-unread-same-subject ()
7957   "Select next unread article which has the same subject as current one."
7958   (interactive)
7959   (gnus-summary-next-article t (gnus-summary-article-subject)))
7960
7961 (defun gnus-summary-prev-unread-same-subject ()
7962   "Select previous unread article which has the same subject as current one."
7963   (interactive)
7964   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7965
7966 (defun gnus-summary-first-unread-article ()
7967   "Select the first unread article.
7968 Return nil if there are no unread articles."
7969   (interactive)
7970   (prog1
7971       (when (gnus-summary-first-subject t)
7972         (gnus-summary-show-thread)
7973         (gnus-summary-first-subject t)
7974         (gnus-summary-display-article (gnus-summary-article-number)))
7975     (gnus-summary-position-point)))
7976
7977 (defun gnus-summary-first-unread-subject ()
7978   "Place the point on the subject line of the first unread article.
7979 Return nil if there are no unread articles."
7980   (interactive)
7981   (prog1
7982       (when (gnus-summary-first-subject t)
7983         (gnus-summary-show-thread)
7984         (gnus-summary-first-subject t))
7985     (gnus-summary-position-point)))
7986
7987 (defun gnus-summary-first-unseen-subject ()
7988   "Place the point on the subject line of the first unseen article.
7989 Return nil if there are no unseen articles."
7990   (interactive)
7991   (prog1
7992       (when (gnus-summary-first-subject nil nil t)
7993         (gnus-summary-show-thread)
7994         (gnus-summary-first-subject nil nil t))
7995     (gnus-summary-position-point)))
7996
7997 (defun gnus-summary-first-unseen-or-unread-subject ()
7998   "Place the point on the subject line of the first unseen and unread article.
7999 If all article have been seen, on the subject line of the first unread
8000 article."
8001   (interactive)
8002   (prog1
8003       (unless (when (gnus-summary-first-subject nil nil t)
8004                 (gnus-summary-show-thread)
8005                 (gnus-summary-first-subject nil nil t))
8006         (when (gnus-summary-first-subject t)
8007           (gnus-summary-show-thread)
8008           (gnus-summary-first-subject t)))
8009     (gnus-summary-position-point)))
8010
8011 (defun gnus-summary-first-article ()
8012   "Select the first article.
8013 Return nil if there are no articles."
8014   (interactive)
8015   (prog1
8016       (when (gnus-summary-first-subject)
8017         (gnus-summary-show-thread)
8018         (gnus-summary-first-subject)
8019         (gnus-summary-display-article (gnus-summary-article-number)))
8020     (gnus-summary-position-point)))
8021
8022 (defun gnus-summary-best-unread-article (&optional arg)
8023   "Select the unread article with the highest score.
8024 If given a prefix argument, select the next unread article that has a
8025 score higher than the default score."
8026   (interactive "P")
8027   (let ((article (if arg
8028                      (gnus-summary-better-unread-subject)
8029                    (gnus-summary-best-unread-subject))))
8030     (if article
8031         (gnus-summary-goto-article article)
8032       (error "No unread articles"))))
8033
8034 (defun gnus-summary-best-unread-subject ()
8035   "Select the unread subject with the highest score."
8036   (interactive)
8037   (let ((best -1000000)
8038         (data gnus-newsgroup-data)
8039         article score)
8040     (while data
8041       (and (gnus-data-unread-p (car data))
8042            (> (setq score
8043                     (gnus-summary-article-score (gnus-data-number (car data))))
8044               best)
8045            (setq best score
8046                  article (gnus-data-number (car data))))
8047       (setq data (cdr data)))
8048     (when article
8049       (gnus-summary-goto-subject article))
8050     (gnus-summary-position-point)
8051     article))
8052
8053 (defun gnus-summary-better-unread-subject ()
8054   "Select the first unread subject that has a score over the default score."
8055   (interactive)
8056   (let ((data gnus-newsgroup-data)
8057         article score)
8058     (while (and (setq article (gnus-data-number (car data)))
8059                 (or (gnus-data-read-p (car data))
8060                     (not (> (gnus-summary-article-score article)
8061                             gnus-summary-default-score))))
8062       (setq data (cdr data)))
8063     (when article
8064       (gnus-summary-goto-subject article))
8065     (gnus-summary-position-point)
8066     article))
8067
8068 (defun gnus-summary-last-subject ()
8069   "Go to the last displayed subject line in the group."
8070   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8071     (when article
8072       (gnus-summary-goto-subject article))))
8073
8074 (defun gnus-summary-goto-article (article &optional all-headers force)
8075   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8076 If ALL-HEADERS is non-nil, no header lines are hidden.
8077 If FORCE, go to the article even if it isn't displayed.  If FORCE
8078 is a number, it is the line the article is to be displayed on."
8079   (interactive
8080    (list
8081     (gnus-completing-read
8082      "Article number or Message-ID"
8083      (mapcar 'int-to-string gnus-newsgroup-limit))
8084     current-prefix-arg
8085     t))
8086   (prog1
8087       (if (and (stringp article)
8088                (string-match "@\\|%40" article))
8089           (gnus-summary-refer-article article)
8090         (when (stringp article)
8091           (setq article (string-to-number article)))
8092         (if (gnus-summary-goto-subject article force)
8093             (gnus-summary-display-article article all-headers)
8094           (gnus-message 4 "Couldn't go to article %s" article) nil))
8095     (gnus-summary-position-point)))
8096
8097 (defun gnus-summary-goto-last-article ()
8098   "Go to the previously read article."
8099   (interactive)
8100   (prog1
8101       (when gnus-last-article
8102         (gnus-summary-goto-article gnus-last-article nil t))
8103     (gnus-summary-position-point)))
8104
8105 (defun gnus-summary-pop-article (number)
8106   "Pop one article off the history and go to the previous.
8107 NUMBER articles will be popped off."
8108   (interactive "p")
8109   (let (to)
8110     (setq gnus-newsgroup-history
8111           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8112     (if to
8113         (gnus-summary-goto-article (car to) nil t)
8114       (error "Article history empty")))
8115   (gnus-summary-position-point))
8116
8117 ;; Summary commands and functions for limiting the summary buffer.
8118
8119 (defun gnus-summary-limit-to-articles (n)
8120   "Limit the summary buffer to the next N articles.
8121 If not given a prefix, use the process marked articles instead."
8122   (interactive "P")
8123   (prog1
8124       (let ((articles (gnus-summary-work-articles n)))
8125         (setq gnus-newsgroup-processable nil)
8126         (gnus-summary-limit articles))
8127     (gnus-summary-position-point)))
8128
8129 (defun gnus-summary-pop-limit (&optional total)
8130   "Restore the previous limit.
8131 If given a prefix, remove all limits."
8132   (interactive "P")
8133   (when total
8134     (setq gnus-newsgroup-limits
8135           (list (mapcar (lambda (h) (mail-header-number h))
8136                         gnus-newsgroup-headers))))
8137   (unless gnus-newsgroup-limits
8138     (error "No limit to pop"))
8139   (prog1
8140       (gnus-summary-limit nil 'pop)
8141     (gnus-summary-position-point)))
8142
8143 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8144   "Limit the summary buffer to articles that have subjects that match a regexp.
8145 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8146   (interactive
8147    (list (read-string (if current-prefix-arg
8148                           "Exclude subject (regexp): "
8149                         "Limit to subject (regexp): "))
8150          nil current-prefix-arg))
8151   (unless header
8152     (setq header "subject"))
8153   (when (not (equal "" subject))
8154     (prog1
8155         (let ((articles (gnus-summary-find-matching
8156                          (or header "subject") subject 'all nil nil
8157                          not-matching)))
8158           (unless articles
8159             (error "Found no matches for \"%s\"" subject))
8160           (gnus-summary-limit articles))
8161       (gnus-summary-position-point))))
8162
8163 (defun gnus-summary-limit-to-author (from &optional not-matching)
8164   "Limit the summary buffer to articles that have authors that match a regexp.
8165 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8166   (interactive
8167    (list (read-string (if current-prefix-arg
8168                           "Exclude author (regexp): "
8169                         "Limit to author (regexp): "))
8170          current-prefix-arg))
8171   (gnus-summary-limit-to-subject from "from" not-matching))
8172
8173 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8174   "Limit the summary buffer to articles with the given RECIPIENT.
8175
8176 If NOT-MATCHING, exclude RECIPIENT.
8177
8178 To and Cc headers are checked.  You need to include them in
8179 `nnmail-extra-headers'."
8180   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8181   (interactive
8182    (list (read-string (format "%s recipient (regexp): "
8183                               (if current-prefix-arg "Exclude" "Limit to")))
8184          current-prefix-arg))
8185   (when (not (equal "" recipient))
8186     (prog1 (let* ((to
8187                    (if (memq 'To nnmail-extra-headers)
8188                        (gnus-summary-find-matching
8189                         (cons 'extra 'To) recipient 'all nil nil
8190                         not-matching)
8191                      (gnus-message
8192                       1 "`To' isn't present in `nnmail-extra-headers'")
8193                      (sit-for 1)
8194                      nil))
8195                   (cc
8196                    (if (memq 'Cc nnmail-extra-headers)
8197                        (gnus-summary-find-matching
8198                         (cons 'extra 'Cc) recipient 'all nil nil
8199                         not-matching)
8200                      (gnus-message
8201                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8202                      (sit-for 1)
8203                      nil))
8204                   (articles
8205                    (if not-matching
8206                        ;; We need the numbers that are in both lists:
8207                        (mapcar (lambda (a)
8208                                  (and (memq a to) a))
8209                                cc)
8210                      (nconc to cc))))
8211              (unless articles
8212                (error "Found no matches for \"%s\"" recipient))
8213              (gnus-summary-limit articles))
8214       (gnus-summary-position-point))))
8215
8216 (defun gnus-summary-limit-to-address (address &optional not-matching)
8217   "Limit the summary buffer to articles with the given ADDRESS.
8218
8219 If NOT-MATCHING, exclude ADDRESS.
8220
8221 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8222 in `nnmail-extra-headers'."
8223   (interactive
8224    (list (read-string (format "%s address (regexp): "
8225                               (if current-prefix-arg "Exclude" "Limit to")))
8226          current-prefix-arg))
8227   (when (not (equal "" address))
8228     (prog1 (let* ((to
8229                    (if (memq 'To nnmail-extra-headers)
8230                        (gnus-summary-find-matching
8231                         (cons 'extra 'To) address 'all nil nil
8232                         not-matching)
8233                      (gnus-message
8234                       1 "`To' isn't present in `nnmail-extra-headers'")
8235                      (sit-for 1)
8236                      t))
8237                   (cc
8238                    (if (memq 'Cc nnmail-extra-headers)
8239                        (gnus-summary-find-matching
8240                         (cons 'extra 'Cc) address 'all nil nil
8241                         not-matching)
8242                      (gnus-message
8243                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8244                      (sit-for 1)
8245                      t))
8246                   (from
8247                    (gnus-summary-find-matching "from" address
8248                                                'all nil nil not-matching))
8249                   (articles
8250                    (if not-matching
8251                        ;; We need the numbers that are in all lists:
8252                        (if (eq cc t)
8253                            (if (eq to t)
8254                                from
8255                              (mapcar (lambda (a) (car (memq a from))) to))
8256                          (if (eq to t)
8257                              (mapcar (lambda (a) (car (memq a from))) cc)
8258                            (mapcar (lambda (a) (car (memq a from)))
8259                                    (mapcar (lambda (a) (car (memq a to)))
8260                                            cc))))
8261                      (nconc (if (eq to t) nil to)
8262                             (if (eq cc t) nil cc)
8263                             from))))
8264              (unless articles
8265                (error "Found no matches for \"%s\"" address))
8266              (gnus-summary-limit articles))
8267       (gnus-summary-position-point))))
8268
8269 (defun gnus-summary-limit-strange-charsets-predicate (header)
8270   (when (fboundp 'char-charset)
8271     (let ((string (concat (mail-header-subject header)
8272                           (mail-header-from header)))
8273           charset found)
8274       (dotimes (i (1- (length string)))
8275         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8276         (when (string-match "unicode\\|big\\|japanese" charset)
8277           (setq found t)))
8278       found)))
8279
8280 (defun gnus-summary-limit-to-predicate (predicate)
8281   "Limit to articles where PREDICATE returns non-nil.
8282 PREDICATE will be called with the header structures of the
8283 articles."
8284   (let ((articles nil)
8285         (case-fold-search t))
8286     (dolist (header gnus-newsgroup-headers)
8287       (when (funcall predicate header)
8288         (push (mail-header-number header) articles)))
8289     (gnus-summary-limit (nreverse articles))))
8290
8291 (defun gnus-summary-limit-to-age (age &optional younger-p)
8292   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8293 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8294 articles that are younger than AGE days."
8295   (interactive
8296    (let ((younger current-prefix-arg)
8297          (days-got nil)
8298          days)
8299      (while (not days-got)
8300        (setq days (if younger
8301                       (read-string "Limit to articles younger than (in days, older when negative): ")
8302                     (read-string
8303                      "Limit to articles older than (in days, younger when negative): ")))
8304        (when (> (length days) 0)
8305          (setq days (read days)))
8306        (if (numberp days)
8307            (progn
8308              (setq days-got t)
8309              (when (< days 0)
8310                (setq younger (not younger))
8311                (setq days (* days -1))))
8312          (message "Please enter a number.")
8313          (sleep-for 1)))
8314      (list days younger)))
8315   (prog1
8316       (let ((data gnus-newsgroup-data)
8317             (cutoff (days-to-time age))
8318             articles d date is-younger)
8319         (while (setq d (pop data))
8320           (when (and (vectorp (gnus-data-header d))
8321                      (setq date (mail-header-date (gnus-data-header d))))
8322             (setq is-younger (time-less-p
8323                               (time-since (gnus-date-get-time date))
8324                               cutoff))
8325             (when (if younger-p
8326                       is-younger
8327                     (not is-younger))
8328               (push (gnus-data-number d) articles))))
8329         (gnus-summary-limit (nreverse articles)))
8330     (gnus-summary-position-point)))
8331
8332 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8333   "Limit the summary buffer to articles that match an 'extra' header."
8334   (interactive
8335    (let ((header
8336           (intern
8337            (gnus-completing-read
8338             (if current-prefix-arg
8339                 "Exclude extra header"
8340               "Limit extra header")
8341             (mapcar 'symbol-name gnus-extra-headers)
8342             t nil nil
8343             (symbol-name (car gnus-extra-headers))))))
8344      (list header
8345            (read-string (format "%s header %s (regexp): "
8346                                 (if current-prefix-arg "Exclude" "Limit to")
8347                                 header))
8348            current-prefix-arg)))
8349   (when (not (equal "" regexp))
8350     (prog1
8351         (let ((articles (gnus-summary-find-matching
8352                          (cons 'extra header) regexp 'all nil nil
8353                          not-matching)))
8354           (unless articles
8355             (error "Found no matches for \"%s\"" regexp))
8356           (gnus-summary-limit articles))
8357       (gnus-summary-position-point))))
8358
8359 (defun gnus-summary-limit-to-display-predicate ()
8360   "Limit the summary buffer to the predicated in the `display' group parameter."
8361   (interactive)
8362   (unless gnus-newsgroup-display
8363     (error "There is no `display' group parameter"))
8364   (let (articles)
8365     (dolist (gnus-number gnus-newsgroup-articles)
8366       (when (funcall gnus-newsgroup-display)
8367         (push gnus-number articles)))
8368     (gnus-summary-limit articles))
8369   (gnus-summary-position-point))
8370
8371 (defun gnus-summary-limit-to-unread (&optional all)
8372   "Limit the summary buffer to articles that are not marked as read.
8373 If ALL is non-nil, limit strictly to unread articles."
8374   (interactive "P")
8375   (if all
8376       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8377     (gnus-summary-limit-to-marks
8378      ;; Concat all the marks that say that an article is read and have
8379      ;; those removed.
8380      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8381            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8382            gnus-low-score-mark gnus-expirable-mark
8383            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8384            gnus-duplicate-mark)
8385      'reverse)))
8386
8387 (defun gnus-summary-limit-to-headers (match &optional reverse)
8388   "Limit the summary buffer to articles that have headers that match MATCH.
8389 If REVERSE (the prefix), limit to articles that don't match."
8390   (interactive "sMatch headers (regexp): \nP")
8391   (gnus-summary-limit-to-bodies match reverse t))
8392
8393 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8394   "Limit the summary buffer to articles that have bodies that match MATCH.
8395 If REVERSE (the prefix), limit to articles that don't match."
8396   (interactive "sMatch body (regexp): \nP")
8397   (let ((articles nil)
8398         (gnus-select-article-hook nil)  ;Disable hook.
8399         (gnus-article-prepare-hook nil)
8400         (gnus-use-article-prefetch nil)
8401         (gnus-keep-backlog nil)
8402         (gnus-break-pages nil)
8403         (gnus-summary-display-arrow nil)
8404         (gnus-updated-mode-lines nil)
8405         (gnus-auto-center-summary nil)
8406         (gnus-display-mime-function nil))
8407     (dolist (data gnus-newsgroup-data)
8408       (let (gnus-mark-article-hook)
8409         (gnus-summary-select-article t t nil (gnus-data-number data)))
8410       (with-current-buffer gnus-article-buffer
8411         (article-goto-body)
8412         (let* ((case-fold-search t)
8413                (found (if headersp
8414                           (re-search-backward match nil t)
8415                         (re-search-forward match nil t))))
8416           (when (or (and found
8417                          (not reverse))
8418                     (and (not found)
8419                          reverse))
8420             (push (gnus-data-number data) articles)))))
8421     (if (not articles)
8422         (message "No messages matched")
8423       (gnus-summary-limit articles)))
8424   (gnus-summary-position-point))
8425
8426 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8427   "Limit the summary buffer to articles that aren't part on any thread.
8428 If THREADSP (the prefix), limit to articles that are in threads."
8429   (interactive "P")
8430   (let ((articles nil)
8431         thread-articles
8432         threads)
8433     (dolist (thread gnus-newsgroup-threads)
8434       (if (stringp (car thread))
8435           (dolist (thread (cdr thread))
8436             (push thread threads))
8437         (push thread threads)))
8438     (dolist (thread threads)
8439       (setq thread-articles (gnus-articles-in-thread thread))
8440       (when (or (and threadsp
8441                      (> (length thread-articles) 1))
8442                 (and (not threadsp)
8443                      (= (length thread-articles) 1)))
8444         (setq articles (nconc thread-articles articles))))
8445     (if (not articles)
8446         (message "No messages matched")
8447       (gnus-summary-limit articles))
8448     (gnus-summary-position-point)))
8449
8450 (defun gnus-summary-limit-to-replied (&optional unreplied)
8451   "Limit the summary buffer to replied articles.
8452 If UNREPLIED (the prefix), limit to unreplied articles."
8453   (interactive "P")
8454   (if unreplied
8455       (gnus-summary-limit
8456        (gnus-set-difference gnus-newsgroup-articles
8457         gnus-newsgroup-replied))
8458     (gnus-summary-limit gnus-newsgroup-replied))
8459   (gnus-summary-position-point))
8460
8461 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8462   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8463 If REVERSE, limit the summary buffer to articles that are marked
8464 with MARKS.  MARKS can either be a string of marks or a list of marks.
8465 Returns how many articles were removed."
8466   (interactive "sMarks: ")
8467   (gnus-summary-limit-to-marks marks t))
8468
8469 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8470   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8471 If REVERSE (the prefix), limit the summary buffer to articles that are
8472 not marked with MARKS.  MARKS can either be a string of marks or a
8473 list of marks.
8474 Returns how many articles were removed."
8475   (interactive "sMarks: \nP")
8476   (prog1
8477       (let ((data gnus-newsgroup-data)
8478             (marks (if (listp marks) marks
8479                      (append marks nil))) ; Transform to list.
8480             articles)
8481         (while data
8482           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8483                   (memq (gnus-data-mark (car data)) marks))
8484             (push (gnus-data-number (car data)) articles))
8485           (setq data (cdr data)))
8486         (gnus-summary-limit articles))
8487     (gnus-summary-position-point)))
8488
8489 (defun gnus-summary-limit-to-score (score)
8490   "Limit to articles with score at or above SCORE."
8491   (interactive "NLimit to articles with score of at least: ")
8492   (let ((data gnus-newsgroup-data)
8493         articles)
8494     (while data
8495       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8496                 score)
8497         (push (gnus-data-number (car data)) articles))
8498       (setq data (cdr data)))
8499     (prog1
8500         (gnus-summary-limit articles)
8501       (gnus-summary-position-point))))
8502
8503 (defun gnus-summary-limit-to-unseen ()
8504   "Limit to unseen articles."
8505   (interactive)
8506   (prog1
8507       (gnus-summary-limit gnus-newsgroup-unseen)
8508     (gnus-summary-position-point)))
8509
8510 (defun gnus-summary-limit-include-thread (id)
8511   "Display all the hidden articles that is in the thread with ID in it.
8512 When called interactively, ID is the Message-ID of the current
8513 article."
8514   (interactive (list (mail-header-id (gnus-summary-article-header))))
8515   (let ((articles (gnus-articles-in-thread
8516                    (gnus-id-to-thread (gnus-root-id id))))
8517         ;;we REALLY want the whole thread---this prevents cut-threads
8518         ;;from removing the thread we want to include.
8519         (gnus-fetch-old-headers nil)
8520         (gnus-build-sparse-threads nil))
8521     (prog1
8522         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8523       (gnus-summary-limit-include-matching-articles
8524        "subject"
8525        (regexp-quote (gnus-simplify-subject-re
8526                       (mail-header-subject (gnus-id-to-header id)))))
8527       (gnus-summary-position-point))))
8528
8529 (defun gnus-summary-limit-include-matching-articles (header regexp)
8530   "Display all the hidden articles that have HEADERs that match REGEXP."
8531   (interactive (list (read-string "Match on header: ")
8532                      (read-string "Regexp: ")))
8533   (let ((articles (gnus-find-matching-articles header regexp)))
8534     (prog1
8535         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8536       (gnus-summary-position-point))))
8537
8538 (defun gnus-summary-insert-dormant-articles ()
8539   "Insert all the dormant articles for this group into the current buffer."
8540   (interactive)
8541   (let ((gnus-verbose (max 6 gnus-verbose)))
8542     (if (not gnus-newsgroup-dormant)
8543         (gnus-message 3 "No dormant articles for this group")
8544       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8545
8546 (defun gnus-summary-insert-ticked-articles ()
8547   "Insert ticked articles for this group into the current buffer."
8548   (interactive)
8549   (let ((gnus-verbose (max 6 gnus-verbose)))
8550     (if (not gnus-newsgroup-marked)
8551         (gnus-message 3 "No ticked articles for this group")
8552       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8553
8554 (defun gnus-summary-limit-include-dormant ()
8555   "Display all the hidden articles that are marked as dormant.
8556 Note that this command only works on a subset of the articles currently
8557 fetched for this group."
8558   (interactive)
8559   (unless gnus-newsgroup-dormant
8560     (error "There are no dormant articles in this group"))
8561   (prog1
8562       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8563     (gnus-summary-position-point)))
8564
8565 (defun gnus-summary-include-articles (articles)
8566   "Fetch the headers for ARTICLES and then display the summary lines."
8567   (let ((gnus-inhibit-demon t)
8568         (gnus-agent nil)
8569         (gnus-read-all-available-headers t))
8570     (setq gnus-newsgroup-headers
8571           (gnus-merge
8572            'list gnus-newsgroup-headers
8573            (gnus-fetch-headers articles nil t)
8574            'gnus-article-sort-by-number))
8575     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8576
8577 (defun gnus-summary-limit-exclude-dormant ()
8578   "Hide all dormant articles."
8579   (interactive)
8580   (prog1
8581       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8582     (gnus-summary-position-point)))
8583
8584 (defun gnus-summary-limit-exclude-childless-dormant ()
8585   "Hide all dormant articles that have no children."
8586   (interactive)
8587   (let ((data (gnus-data-list t))
8588         articles d children)
8589     ;; Find all articles that are either not dormant or have
8590     ;; children.
8591     (while (setq d (pop data))
8592       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8593                 (and (setq children
8594                            (gnus-article-children (gnus-data-number d)))
8595                      (let (found)
8596                        (while children
8597                          (when (memq (car children) articles)
8598                            (setq children nil
8599                                  found t))
8600                          (pop children))
8601                        found)))
8602         (push (gnus-data-number d) articles)))
8603     ;; Do the limiting.
8604     (prog1
8605         (gnus-summary-limit articles)
8606       (gnus-summary-position-point))))
8607
8608 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8609   "Mark all unread excluded articles as read.
8610 If ALL, mark even excluded ticked and dormants as read."
8611   (interactive "P")
8612   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8613   (let ((articles (gnus-sorted-ndifference
8614                    (sort
8615                     (mapcar (lambda (h) (mail-header-number h))
8616                             gnus-newsgroup-headers)
8617                     '<)
8618                    gnus-newsgroup-limit))
8619         article)
8620     (setq gnus-newsgroup-unreads
8621           (gnus-sorted-intersection gnus-newsgroup-unreads
8622                                     gnus-newsgroup-limit))
8623     (if all
8624         (setq gnus-newsgroup-dormant nil
8625               gnus-newsgroup-marked nil
8626               gnus-newsgroup-reads
8627               (nconc
8628                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8629                gnus-newsgroup-reads))
8630       (while (setq article (pop articles))
8631         (unless (or (memq article gnus-newsgroup-dormant)
8632                     (memq article gnus-newsgroup-marked))
8633           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8634
8635 (defun gnus-summary-limit (articles &optional pop)
8636   (if pop
8637       ;; We pop the previous limit off the stack and use that.
8638       (setq articles (car gnus-newsgroup-limits)
8639             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8640     ;; We use the new limit, so we push the old limit on the stack.
8641     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8642   ;; Set the limit.
8643   (setq gnus-newsgroup-limit articles)
8644   (let ((total (length gnus-newsgroup-data))
8645         (data (gnus-data-find-list (gnus-summary-article-number)))
8646         (gnus-summary-mark-below nil)   ; Inhibit this.
8647         found)
8648     ;; This will do all the work of generating the new summary buffer
8649     ;; according to the new limit.
8650     (gnus-summary-prepare)
8651     ;; Hide any threads, possibly.
8652     (gnus-summary-maybe-hide-threads)
8653     ;; Try to return to the article you were at, or one in the
8654     ;; neighborhood.
8655     (when data
8656       ;; We try to find some article after the current one.
8657       (while data
8658         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8659           (setq data nil
8660                 found t))
8661         (setq data (cdr data))))
8662     (unless found
8663       ;; If there is no data, that means that we were after the last
8664       ;; article.  The same goes when we can't find any articles
8665       ;; after the current one.
8666       (goto-char (point-max))
8667       (gnus-summary-find-prev))
8668     (gnus-set-mode-line 'summary)
8669     ;; We return how many articles were removed from the summary
8670     ;; buffer as a result of the new limit.
8671     (- total (length gnus-newsgroup-data))))
8672
8673 (defsubst gnus-invisible-cut-children (threads)
8674   (let ((num 0))
8675     (while threads
8676       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8677         (incf num))
8678       (pop threads))
8679     (< num 2)))
8680
8681 (defsubst gnus-cut-thread (thread)
8682   "Go forwards in the thread until we find an article that we want to display."
8683   (when (or (eq gnus-fetch-old-headers 'some)
8684             (eq gnus-fetch-old-headers 'invisible)
8685             (numberp gnus-fetch-old-headers)
8686             (eq gnus-build-sparse-threads 'some)
8687             (eq gnus-build-sparse-threads 'more))
8688     ;; Deal with old-fetched headers and sparse threads.
8689     (while (and
8690             thread
8691             (or
8692              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8693              (gnus-summary-article-ancient-p
8694               (mail-header-number (car thread))))
8695             (if (or (<= (length (cdr thread)) 1)
8696                     (eq gnus-fetch-old-headers 'invisible))
8697                 (setq gnus-newsgroup-limit
8698                       (delq (mail-header-number (car thread))
8699                             gnus-newsgroup-limit)
8700                       thread (cadr thread))
8701               (when (gnus-invisible-cut-children (cdr thread))
8702                 (let ((th (cdr thread)))
8703                   (while th
8704                     (if (memq (mail-header-number (caar th))
8705                               gnus-newsgroup-limit)
8706                         (setq thread (car th)
8707                               th nil)
8708                       (setq th (cdr th))))))))))
8709   thread)
8710
8711 (defun gnus-cut-threads (threads)
8712   "Cut off all uninteresting articles from the beginning of THREADS."
8713   (when (or (eq gnus-fetch-old-headers 'some)
8714             (eq gnus-fetch-old-headers 'invisible)
8715             (numberp gnus-fetch-old-headers)
8716             (eq gnus-build-sparse-threads 'some)
8717             (eq gnus-build-sparse-threads 'more))
8718     (let ((th threads))
8719       (while th
8720         (setcar th (gnus-cut-thread (car th)))
8721         (setq th (cdr th)))))
8722   ;; Remove nixed out threads.
8723   (delq nil threads))
8724
8725 (defun gnus-summary-initial-limit (&optional show-if-empty)
8726   "Figure out what the initial limit is supposed to be on group entry.
8727 This entails weeding out unwanted dormants, low-scored articles,
8728 fetch-old-headers verbiage, and so on."
8729   ;; Most groups have nothing to remove.
8730   (unless (or gnus-inhibit-limiting
8731               (and (null gnus-newsgroup-dormant)
8732                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8733                    (not (eq gnus-fetch-old-headers 'some))
8734                    (not (numberp gnus-fetch-old-headers))
8735                    (not (eq gnus-fetch-old-headers 'invisible))
8736                    (null gnus-summary-expunge-below)
8737                    (not (eq gnus-build-sparse-threads 'some))
8738                    (not (eq gnus-build-sparse-threads 'more))
8739                    (null gnus-thread-expunge-below)))
8740     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8741     (setq gnus-newsgroup-limit nil)
8742     (mapatoms
8743      (lambda (node)
8744        (unless (car (symbol-value node))
8745          ;; These threads have no parents -- they are roots.
8746          (let ((nodes (cdr (symbol-value node)))
8747                thread)
8748            (while nodes
8749              (if (and gnus-thread-expunge-below
8750                       (< (gnus-thread-total-score (car nodes))
8751                          gnus-thread-expunge-below))
8752                  (gnus-expunge-thread (pop nodes))
8753                (setq thread (pop nodes))
8754                (gnus-summary-limit-children thread))))))
8755      gnus-newsgroup-dependencies)
8756     ;; If this limitation resulted in an empty group, we might
8757     ;; pop the previous limit and use it instead.
8758     (when (and (not gnus-newsgroup-limit)
8759                show-if-empty)
8760       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8761     gnus-newsgroup-limit))
8762
8763 (defun gnus-summary-limit-children (thread)
8764   "Return 1 if this subthread is visible and 0 if it is not."
8765   ;; First we get the number of visible children to this thread.  This
8766   ;; is done by recursing down the thread using this function, so this
8767   ;; will really go down to a leaf article first, before slowly
8768   ;; working its way up towards the root.
8769   (when thread
8770     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8771            (children
8772            (if (cdr thread)
8773                (apply '+ (mapcar 'gnus-summary-limit-children
8774                                  (cdr thread)))
8775              0))
8776            (number (mail-header-number (car thread)))
8777            score)
8778       (if (and
8779            (not (memq number gnus-newsgroup-marked))
8780            (or
8781             ;; If this article is dormant and has absolutely no visible
8782             ;; children, then this article isn't visible.
8783             (and (memq number gnus-newsgroup-dormant)
8784                  (zerop children))
8785             ;; If this is "fetch-old-headered" and there is no
8786             ;; visible children, then we don't want this article.
8787             (and (or (eq gnus-fetch-old-headers 'some)
8788                      (numberp gnus-fetch-old-headers))
8789                  (gnus-summary-article-ancient-p number)
8790                  (zerop children))
8791             ;; If this is "fetch-old-headered" and `invisible', then
8792             ;; we don't want this article.
8793             (and (eq gnus-fetch-old-headers 'invisible)
8794                  (gnus-summary-article-ancient-p number))
8795             ;; If this is a sparsely inserted article with no children,
8796             ;; we don't want it.
8797             (and (eq gnus-build-sparse-threads 'some)
8798                  (gnus-summary-article-sparse-p number)
8799                  (zerop children))
8800             ;; If we use expunging, and this article is really
8801             ;; low-scored, then we don't want this article.
8802             (when (and gnus-summary-expunge-below
8803                        (< (setq score
8804                                 (or (cdr (assq number gnus-newsgroup-scored))
8805                                     gnus-summary-default-score))
8806                           gnus-summary-expunge-below))
8807               ;; We increase the expunge-tally here, but that has
8808               ;; nothing to do with the limits, really.
8809               (incf gnus-newsgroup-expunged-tally)
8810               ;; We also mark as read here, if that's wanted.
8811               (when (and gnus-summary-mark-below
8812                          (< score gnus-summary-mark-below))
8813                 (setq gnus-newsgroup-unreads
8814                       (delq number gnus-newsgroup-unreads))
8815                 (if gnus-newsgroup-auto-expire
8816                     (push number gnus-newsgroup-expirable)
8817                   (push (cons number gnus-low-score-mark)
8818                         gnus-newsgroup-reads)))
8819               t)
8820             ;; Do the `display' group parameter.
8821             (and gnus-newsgroup-display
8822                  (let ((gnus-number number))
8823                    (not (funcall gnus-newsgroup-display))))))
8824           ;; Nope, invisible article.
8825           0
8826         ;; Ok, this article is to be visible, so we add it to the limit
8827         ;; and return 1.
8828         (push number gnus-newsgroup-limit)
8829         1))))
8830
8831 (defun gnus-expunge-thread (thread)
8832   "Mark all articles in THREAD as read."
8833   (let* ((number (mail-header-number (car thread))))
8834     (incf gnus-newsgroup-expunged-tally)
8835     ;; We also mark as read here, if that's wanted.
8836     (setq gnus-newsgroup-unreads
8837           (delq number gnus-newsgroup-unreads))
8838     (if gnus-newsgroup-auto-expire
8839         (push number gnus-newsgroup-expirable)
8840       (push (cons number gnus-low-score-mark)
8841             gnus-newsgroup-reads)))
8842   ;; Go recursively through all subthreads.
8843   (mapcar 'gnus-expunge-thread (cdr thread)))
8844
8845 ;; Summary article oriented commands
8846
8847 (defun gnus-summary-refer-parent-article (n)
8848   "Refer parent article N times.
8849 If N is negative, go to ancestor -N instead.
8850 The difference between N and the number of articles fetched is returned."
8851   (interactive "p")
8852   (let ((skip 1)
8853         error header ref)
8854     (when (not (natnump n))
8855       (setq skip (abs n)
8856             n 1))
8857     (while (and (> n 0)
8858                 (not error))
8859       (setq header (gnus-summary-article-header))
8860       (if (and (eq (mail-header-number header)
8861                    (cdr gnus-article-current))
8862                (equal gnus-newsgroup-name
8863                       (car gnus-article-current)))
8864           ;; If we try to find the parent of the currently
8865           ;; displayed article, then we take a look at the actual
8866           ;; References header, since this is slightly more
8867           ;; reliable than the References field we got from the
8868           ;; server.
8869           (with-current-buffer gnus-original-article-buffer
8870             (nnheader-narrow-to-headers)
8871             (unless (setq ref (message-fetch-field "references"))
8872               (when (setq ref (message-fetch-field "in-reply-to"))
8873                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8874             (widen))
8875         (setq ref
8876               ;; It's not the current article, so we take a bet on
8877               ;; the value we got from the server.
8878               (mail-header-references header)))
8879       (if (and ref
8880                (not (equal ref "")))
8881           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8882             (gnus-message 1 "Couldn't find parent"))
8883         (gnus-message 1 "No references in article %d"
8884                       (gnus-summary-article-number))
8885         (setq error t))
8886       (decf n))
8887     (gnus-summary-position-point)
8888     n))
8889
8890 (defun gnus-summary-refer-references ()
8891   "Fetch all articles mentioned in the References header.
8892 Return the number of articles fetched."
8893   (interactive)
8894   (let ((ref (mail-header-references (gnus-summary-article-header)))
8895         (current (gnus-summary-article-number))
8896         (n 0))
8897     (if (or (not ref)
8898             (equal ref ""))
8899         (error "No References in the current article")
8900       ;; For each Message-ID in the References header...
8901       (while (string-match "<[^>]*>" ref)
8902         (incf n)
8903         ;; ... fetch that article.
8904         (gnus-summary-refer-article
8905          (prog1 (match-string 0 ref)
8906            (setq ref (substring ref (match-end 0))))))
8907       (gnus-summary-goto-subject current)
8908       (gnus-summary-position-point)
8909       n)))
8910
8911 (defun gnus-summary-refer-thread (&optional limit)
8912   "Fetch all articles in the current thread.
8913 If no backend-specific 'request-thread function is available
8914 fetch LIMIT (the numerical prefix) old headers. If LIMIT is nil
8915 fetch what's specified by the `gnus-refer-thread-limit'
8916 variable."
8917   (interactive "P")
8918   (gnus-warp-to-article)
8919   (let* ((header (gnus-summary-article-header))
8920          (id (mail-header-id header))
8921          (gnus-inhibit-demon t)
8922          (gnus-summary-ignore-duplicates t)
8923          (gnus-read-all-available-headers t)
8924          (limit (if limit (prefix-numeric-value limit)
8925                   gnus-refer-thread-limit)))
8926     (setq gnus-newsgroup-headers
8927           (gnus-merge
8928            'list gnus-newsgroup-headers
8929            (if (gnus-check-backend-function
8930                 'request-thread gnus-newsgroup-name)
8931                (gnus-request-thread header)
8932              (let* ((last (if (numberp limit)
8933                               (min (+ (mail-header-number header)
8934                                       limit)
8935                                    gnus-newsgroup-highest)
8936                             gnus-newsgroup-highest))
8937                     (subject (gnus-simplify-subject
8938                               (mail-header-subject header)))
8939                     (refs (split-string (or (mail-header-references header)
8940                                             "")))
8941                     (gnus-parse-headers-hook
8942                      (lambda () (goto-char (point-min))
8943                        (keep-lines
8944                         (regexp-opt (append refs (list id subject)))))))
8945                (gnus-fetch-headers (list last) (if (numberp limit)
8946                                                    (* 2 limit) limit) t)))
8947            'gnus-article-sort-by-number))
8948     (gnus-summary-limit-include-thread id)))
8949
8950 (defun gnus-summary-refer-article (message-id)
8951   "Fetch an article specified by MESSAGE-ID."
8952   (interactive "sMessage-ID: ")
8953   (when (and (stringp message-id)
8954              (not (zerop (length message-id))))
8955     (setq message-id (gnus-replace-in-string message-id " " ""))
8956     ;; Construct the correct Message-ID if necessary.
8957     ;; Suggested by tale@pawl.rpi.edu.
8958     (unless (string-match "^<" message-id)
8959       (setq message-id (concat "<" message-id)))
8960     (unless (string-match ">$" message-id)
8961       (setq message-id (concat message-id ">")))
8962     ;; People often post MIDs from URLs, so unhex it:
8963     (unless (string-match "@" message-id)
8964       (setq message-id (gnus-url-unhex-string message-id)))
8965     (let* ((header (gnus-id-to-header message-id))
8966            (sparse (and header
8967                         (gnus-summary-article-sparse-p
8968                          (mail-header-number header))
8969                         (memq (mail-header-number header)
8970                               gnus-newsgroup-limit)))
8971            number)
8972       (cond
8973        ;; If the article is present in the buffer we just go to it.
8974        ((and header
8975              (or (not (gnus-summary-article-sparse-p
8976                        (mail-header-number header)))
8977                  sparse))
8978         (prog1
8979             (gnus-summary-goto-article
8980              (mail-header-number header) nil t)
8981           (when sparse
8982             (gnus-summary-update-article (mail-header-number header)))))
8983        (t
8984         ;; We fetch the article.
8985         (catch 'found
8986           (dolist (gnus-override-method (gnus-refer-article-methods))
8987             (when (and (gnus-check-server gnus-override-method)
8988                        ;; Fetch the header,
8989                        (setq number (gnus-summary-insert-subject message-id)))
8990               ;; and display the article.
8991               (gnus-summary-select-article nil nil nil number)
8992               (throw 'found t)))
8993           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8994
8995 (defun gnus-refer-article-methods ()
8996   "Return a list of referable methods."
8997   (cond
8998    ;; No method, so we default to current and native.
8999    ((null gnus-refer-article-method)
9000     (list gnus-current-select-method gnus-select-method))
9001    ;; Current.
9002    ((eq 'current gnus-refer-article-method)
9003     (list gnus-current-select-method))
9004    ;; List of select methods.
9005    ((not (and (symbolp (car gnus-refer-article-method))
9006               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9007     (let (out)
9008       (dolist (method gnus-refer-article-method)
9009         (push (if (eq 'current method)
9010                   gnus-current-select-method
9011                 method)
9012               out))
9013       (nreverse out)))
9014    ;; One single select method.
9015    (t
9016     (list gnus-refer-article-method))))
9017
9018 (defun gnus-summary-edit-parameters ()
9019   "Edit the group parameters of the current group."
9020   (interactive)
9021   (gnus-group-edit-group gnus-newsgroup-name 'params))
9022
9023 (defun gnus-summary-customize-parameters ()
9024   "Customize the group parameters of the current group."
9025   (interactive)
9026   (gnus-group-customize gnus-newsgroup-name))
9027
9028 (defun gnus-summary-enter-digest-group (&optional force)
9029   "Enter an nndoc group based on the current article.
9030 If FORCE, force a digest interpretation.  If not, try to guess
9031 what the document format is.
9032
9033 To control what happens when you exit the group, see the
9034 `gnus-auto-select-on-ephemeral-exit' variable."
9035   (interactive "P")
9036   (let ((conf gnus-current-window-configuration))
9037     (save-window-excursion
9038       (save-excursion
9039         (let (gnus-article-prepare-hook
9040               gnus-display-mime-function
9041               gnus-break-pages)
9042           (gnus-summary-select-article))))
9043     (setq gnus-current-window-configuration conf)
9044     (let* ((name (format "%s-%d"
9045                          (gnus-group-prefixed-name
9046                           gnus-newsgroup-name (list 'nndoc ""))
9047                          (with-current-buffer gnus-summary-buffer
9048                            gnus-current-article)))
9049            (ogroup gnus-newsgroup-name)
9050            (params (append (gnus-info-params (gnus-get-info ogroup))
9051                            (list (cons 'to-group ogroup))
9052                            (list (cons 'parent-group ogroup))
9053                            (list (cons 'save-article-group ogroup))))
9054            (case-fold-search t)
9055            (buf (current-buffer))
9056            dig to-address)
9057       (with-current-buffer gnus-original-article-buffer
9058         ;; Have the digest group inherit the main mail address of
9059         ;; the parent article.
9060         (when (setq to-address (or (gnus-fetch-field "reply-to")
9061                                    (gnus-fetch-field "from")))
9062           (setq params
9063                 (append
9064                  (list (cons 'to-address
9065                              (funcall gnus-decode-encoded-address-function
9066                                       to-address))))))
9067         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9068         (insert-buffer-substring gnus-original-article-buffer)
9069         ;; Remove lines that may lead nndoc to misinterpret the
9070         ;; document type.
9071         (narrow-to-region
9072          (goto-char (point-min))
9073          (or (search-forward "\n\n" nil t) (point)))
9074         (goto-char (point-min))
9075         (delete-matching-lines "^Path:\\|^From ")
9076         (widen))
9077       (unwind-protect
9078           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9079                     (gnus-newsgroup-ephemeral-ignored-charsets
9080                      gnus-newsgroup-ignored-charsets))
9081                 (gnus-group-read-ephemeral-group
9082                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9083                               (nndoc-article-type
9084                                ,(if force 'mbox 'guess)))
9085                  t nil nil nil
9086                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9087                                                         "ADAPT")))))
9088               ;; Make all postings to this group go to the parent group.
9089               (nconc (gnus-info-params (gnus-get-info name))
9090                      params)
9091             ;; Couldn't select this doc group.
9092             (switch-to-buffer buf)
9093             (gnus-set-global-variables)
9094             (gnus-configure-windows 'summary)
9095             (gnus-message 3 "Article couldn't be entered?"))
9096         (kill-buffer dig)))))
9097
9098 (defun gnus-summary-read-document (n)
9099   "Open a new group based on the current article(s).
9100 This will allow you to read digests and other similar
9101 documents as newsgroups.
9102 Obeys the standard process/prefix convention."
9103   (interactive "P")
9104   (let* ((ogroup gnus-newsgroup-name)
9105          (params (append (gnus-info-params (gnus-get-info ogroup))
9106                          (list (cons 'to-group ogroup))))
9107          group egroup groups vgroup)
9108     (dolist (article (gnus-summary-work-articles n))
9109       (setq group (format "%s-%d" gnus-newsgroup-name article))
9110       (gnus-summary-remove-process-mark article)
9111       (when (gnus-summary-display-article article)
9112         (save-excursion ;;What for?
9113           (with-temp-buffer
9114             (insert-buffer-substring gnus-original-article-buffer)
9115             ;; Remove some headers that may lead nndoc to make
9116             ;; the wrong guess.
9117             (message-narrow-to-head)
9118             (goto-char (point-min))
9119             (delete-matching-lines "^Path:\\|^From ")
9120             (widen)
9121             (if (setq egroup
9122                       (gnus-group-read-ephemeral-group
9123                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9124                                      (nndoc-article-type guess))
9125                        t nil t))
9126                 (progn
9127                   ;; Make all postings to this group go to the parent group.
9128                   (nconc (gnus-info-params (gnus-get-info egroup))
9129                          params)
9130                   (push egroup groups))
9131               ;; Couldn't select this doc group.
9132               (gnus-error 3 "Article couldn't be entered"))))))
9133     ;; Now we have selected all the documents.
9134     (cond
9135      ((not groups)
9136       (error "None of the articles could be interpreted as documents"))
9137      ((gnus-group-read-ephemeral-group
9138        (setq vgroup (format
9139                      "nnvirtual:%s-%s" gnus-newsgroup-name
9140                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9141        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9142        t
9143        (cons (current-buffer) 'summary)))
9144      (t
9145       (error "Couldn't select virtual nndoc group")))))
9146
9147 (defun gnus-summary-widget-forward (arg)
9148   "Move point to the next field or button in the article.
9149 With optional ARG, move across that many fields."
9150   (interactive "p")
9151   (gnus-summary-select-article)
9152   (gnus-configure-windows 'article)
9153   (select-window (gnus-get-buffer-window gnus-article-buffer))
9154   (widget-forward arg))
9155
9156 (defun gnus-summary-isearch-article (&optional regexp-p)
9157   "Do incremental search forward on the current article.
9158 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9159   (interactive "P")
9160   (gnus-summary-select-article)
9161   (gnus-configure-windows 'article)
9162   (gnus-eval-in-buffer-window gnus-article-buffer
9163     (save-restriction
9164       (widen)
9165       (isearch-forward regexp-p))))
9166
9167 (defun gnus-summary-repeat-search-article-forward ()
9168   "Repeat the previous search forwards."
9169   (interactive)
9170   (unless gnus-last-search-regexp
9171     (error "No previous search"))
9172   (gnus-summary-search-article-forward gnus-last-search-regexp))
9173
9174 (defun gnus-summary-repeat-search-article-backward ()
9175   "Repeat the previous search backwards."
9176   (interactive)
9177   (unless gnus-last-search-regexp
9178     (error "No previous search"))
9179   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9180
9181 (defun gnus-summary-search-article-forward (regexp &optional backward)
9182   "Search for an article containing REGEXP forward.
9183 If BACKWARD, search backward instead."
9184   (interactive
9185    (list (read-string
9186           (format "Search article %s (regexp%s): "
9187                   (if current-prefix-arg "backward" "forward")
9188                   (if gnus-last-search-regexp
9189                       (concat ", default " gnus-last-search-regexp)
9190                     "")))
9191          current-prefix-arg))
9192   (if (string-equal regexp "")
9193       (setq regexp (or gnus-last-search-regexp ""))
9194     (setq gnus-last-search-regexp regexp)
9195     (setq gnus-article-before-search gnus-current-article))
9196   ;; Intentionally set gnus-last-article.
9197   (setq gnus-last-article gnus-article-before-search)
9198   (let ((gnus-last-article gnus-last-article))
9199     (if (gnus-summary-search-article regexp backward)
9200         (gnus-summary-show-thread)
9201       (signal 'search-failed (list regexp)))))
9202
9203 (defun gnus-summary-search-article-backward (regexp)
9204   "Search for an article containing REGEXP backward."
9205   (interactive
9206    (list (read-string
9207           (format "Search article backward (regexp%s): "
9208                   (if gnus-last-search-regexp
9209                       (concat ", default " gnus-last-search-regexp)
9210                     "")))))
9211   (gnus-summary-search-article-forward regexp 'backward))
9212
9213 (defun gnus-summary-search-article (regexp &optional backward)
9214   "Search for an article containing REGEXP.
9215 Optional argument BACKWARD means do search for backward.
9216 `gnus-select-article-hook' is not called during the search."
9217   ;; We have to require this here to make sure that the following
9218   ;; dynamic binding isn't shadowed by autoloading.
9219   (require 'gnus-async)
9220   (require 'gnus-art)
9221   (let ((gnus-select-article-hook nil)  ;Disable hook.
9222         (gnus-article-prepare-hook nil)
9223         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9224         (gnus-use-article-prefetch nil)
9225         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9226         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9227         (gnus-visual nil)
9228         (gnus-keep-backlog nil)
9229         (gnus-break-pages nil)
9230         (gnus-summary-display-arrow nil)
9231         (gnus-updated-mode-lines nil)
9232         (gnus-auto-center-summary nil)
9233         (sum (current-buffer))
9234         (gnus-display-mime-function nil)
9235         (found nil)
9236         point)
9237     (gnus-save-hidden-threads
9238       (gnus-summary-select-article)
9239       (set-buffer gnus-article-buffer)
9240       (goto-char (window-point (get-buffer-window (current-buffer))))
9241       (when backward
9242         (forward-line -1))
9243       (while (not found)
9244         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9245         (if (if backward
9246                 (re-search-backward regexp nil t)
9247               (re-search-forward regexp nil t))
9248             ;; We found the regexp.
9249             (progn
9250               (setq found 'found)
9251               (beginning-of-line)
9252               (set-window-start
9253                (get-buffer-window (current-buffer))
9254                (point))
9255               (forward-line 1)
9256               (set-window-point
9257                (get-buffer-window (current-buffer))
9258                (point))
9259               (set-buffer sum)
9260               (setq point (point)))
9261           ;; We didn't find it, so we go to the next article.
9262           (set-buffer sum)
9263           (setq found 'not)
9264           (while (eq found 'not)
9265             (if (not (if backward (gnus-summary-find-prev)
9266                        (gnus-summary-find-next)))
9267                 ;; No more articles.
9268                 (setq found t)
9269               ;; Select the next article and adjust point.
9270               (unless (gnus-summary-article-sparse-p
9271                        (gnus-summary-article-number))
9272                 (setq found nil)
9273                 (gnus-summary-select-article)
9274                 (set-buffer gnus-article-buffer)
9275                 (widen)
9276                 (goto-char (if backward (point-max) (point-min))))))))
9277       (gnus-message 7 ""))
9278     ;; Return whether we found the regexp.
9279     (when (eq found 'found)
9280       (goto-char point)
9281       (gnus-summary-show-thread)
9282       (gnus-summary-goto-subject gnus-current-article)
9283       (gnus-summary-position-point)
9284       t)))
9285
9286 (defun gnus-find-matching-articles (header regexp)
9287   "Return a list of all articles that match REGEXP on HEADER.
9288 This search includes all articles in the current group that Gnus has
9289 fetched headers for, whether they are displayed or not."
9290   (let ((articles nil)
9291         ;; Can't eta-reduce because it's a macro.
9292         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9293         (case-fold-search t))
9294     (dolist (header gnus-newsgroup-headers)
9295       (when (string-match regexp (funcall func header))
9296         (push (mail-header-number header) articles)))
9297     (nreverse articles)))
9298
9299 (defun gnus-summary-find-matching (header regexp &optional backward unread
9300                                           not-case-fold not-matching)
9301   "Return a list of all articles that match REGEXP on HEADER.
9302 The search stars on the current article and goes forwards unless
9303 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9304 If UNREAD is non-nil, only unread articles will
9305 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9306 in the comparisons. If NOT-MATCHING, return a list of all articles that
9307 not match REGEXP on HEADER."
9308   (let ((case-fold-search (not not-case-fold))
9309         articles d func)
9310     (if (consp header)
9311         (if (eq (car header) 'extra)
9312             (setq func
9313                   `(lambda (h)
9314                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9315                          "")))
9316           (error "%s is an invalid header" header))
9317       (unless (fboundp (intern (concat "mail-header-" header)))
9318         (error "%s is not a valid header" header))
9319       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9320     (dolist (d (if (eq backward 'all)
9321                    gnus-newsgroup-data
9322                  (gnus-data-find-list
9323                   (gnus-summary-article-number)
9324                   (gnus-data-list backward))))
9325       (when (and (or (not unread)       ; We want all articles...
9326                      (gnus-data-unread-p d)) ; Or just unreads.
9327                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9328                  (if not-matching
9329                      (not (string-match
9330                            regexp
9331                            (funcall func (gnus-data-header d))))
9332                    (string-match regexp
9333                                  (funcall func (gnus-data-header d)))))
9334         (push (gnus-data-number d) articles))) ; Success!
9335     (nreverse articles)))
9336
9337 (defun gnus-summary-execute-command (header regexp command &optional backward)
9338   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9339 If HEADER is an empty string (or nil), the match is done on the entire
9340 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9341   (interactive
9342    (list (let ((completion-ignore-case t))
9343            (gnus-completing-read
9344             "Header name"
9345             (mapcar 'symbol-name
9346                     (append
9347                      '(Number Subject From Lines Date
9348                        Message-ID Xref References Body)
9349                      gnus-extra-headers))
9350             'require-match))
9351          (read-string "Regexp: ")
9352          (read-key-sequence "Command: ")
9353          current-prefix-arg))
9354   (when (equal header "Body")
9355     (setq header ""))
9356   ;; Hidden thread subtrees must be searched as well.
9357   (gnus-summary-show-all-threads)
9358   ;; We don't want to change current point nor window configuration.
9359   (save-excursion
9360     (save-window-excursion
9361       (let (gnus-visual
9362             gnus-treat-strip-trailing-blank-lines
9363             gnus-treat-strip-leading-blank-lines
9364             gnus-treat-strip-multiple-blank-lines
9365             gnus-treat-hide-boring-headers
9366             gnus-treat-fold-newsgroups
9367             gnus-article-prepare-hook)
9368         (gnus-message 6 "Executing %s..." (key-description command))
9369         ;; We'd like to execute COMMAND interactively so as to give arguments.
9370         (gnus-execute header regexp
9371                       `(call-interactively ',(key-binding command))
9372                       backward)
9373         (gnus-message 6 "Executing %s...done" (key-description command))))))
9374
9375 (defun gnus-summary-beginning-of-article ()
9376   "Scroll the article back to the beginning."
9377   (interactive)
9378   (gnus-summary-select-article)
9379   (gnus-configure-windows 'article)
9380   (gnus-eval-in-buffer-window gnus-article-buffer
9381     (widen)
9382     (goto-char (point-min))
9383     (when gnus-break-pages
9384       (gnus-narrow-to-page))))
9385
9386 (defun gnus-summary-end-of-article ()
9387   "Scroll to the end of the article."
9388   (interactive)
9389   (gnus-summary-select-article)
9390   (gnus-configure-windows 'article)
9391   (gnus-eval-in-buffer-window gnus-article-buffer
9392     (widen)
9393     (goto-char (point-max))
9394     (recenter -3)
9395     (when gnus-break-pages
9396       (gnus-narrow-to-page))))
9397
9398 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9399   "Truncate to LEN and quote all \"(\"'s in STRING."
9400   (gnus-replace-in-string (if (and len (> (length string) len))
9401                               (substring string 0 len)
9402                             string)
9403                           "[()]" "\\\\\\&"))
9404
9405 (defun gnus-summary-print-article (&optional filename n)
9406   "Generate and print a PostScript image of the process-marked (mail) articles.
9407
9408 If used interactively, print the current article if none are
9409 process-marked.  With prefix arg, prompt the user for the name of the
9410 file to save in.
9411
9412 When used from Lisp, accept two optional args FILENAME and N.  N means
9413 to print the next N articles.  If N is negative, print the N previous
9414 articles.  If N is nil and articles have been marked with the process
9415 mark, print these instead.
9416
9417 If the optional first argument FILENAME is nil, send the image to the
9418 printer.  If FILENAME is a string, save the PostScript image in a file with
9419 that name.  If FILENAME is a number, prompt the user for the name of the file
9420 to save in."
9421   (interactive (list (ps-print-preprint current-prefix-arg)))
9422   (dolist (article (gnus-summary-work-articles n))
9423     (gnus-summary-select-article nil nil 'pseudo article)
9424     (gnus-eval-in-buffer-window gnus-article-buffer
9425       (gnus-print-buffer))
9426     (gnus-summary-remove-process-mark article))
9427   (ps-despool filename))
9428
9429 (defun gnus-print-buffer ()
9430   (let ((ps-left-header
9431          (list
9432           (concat "("
9433                   (gnus-summary-print-truncate-and-quote
9434                    (mail-header-subject gnus-current-headers)
9435                    66) ")")
9436           (concat "("
9437                   (gnus-summary-print-truncate-and-quote
9438                    (mail-header-from gnus-current-headers)
9439                    45) ")")))
9440         (ps-right-header
9441          (list
9442           "/pagenumberstring load"
9443           (concat "("
9444                   (mail-header-date gnus-current-headers) ")"))))
9445     (gnus-run-hooks 'gnus-ps-print-hook)
9446     (save-excursion
9447       (if ps-print-color-p
9448           (ps-spool-buffer-with-faces)
9449         (ps-spool-buffer)))))
9450
9451 (defun gnus-summary-show-complete-article ()
9452   "Show a complete version of the current article.
9453 This is only useful if you're looking at a partial version of the
9454 article currently."
9455   (interactive)
9456   (let ((gnus-keep-backlog nil)
9457         (gnus-use-cache nil)
9458         (gnus-agent nil)
9459         (variable (intern
9460                    (format "%s-fetch-partial-articles"
9461                            (car (gnus-find-method-for-group
9462                                  gnus-newsgroup-name)))
9463                    obarray))
9464         old-val)
9465     (unwind-protect
9466         (progn
9467           (setq old-val (symbol-value variable))
9468           (set variable nil)
9469           (gnus-flush-original-article-buffer)
9470           (gnus-summary-show-article))
9471       (set variable old-val))))
9472
9473 (defun gnus-summary-show-article (&optional arg)
9474   "Force redisplaying of the current article.
9475 If ARG (the prefix) is a number, show the article with the charset
9476 defined in `gnus-summary-show-article-charset-alist', or the charset
9477 input.
9478 If ARG (the prefix) is non-nil and not a number, show the article,
9479 but without running any of the article treatment functions
9480 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9481 C-u g', show the raw article."
9482   (interactive "P")
9483   (cond
9484    ((numberp arg)
9485     (gnus-summary-show-article t)
9486     (let ((gnus-newsgroup-charset
9487            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9488                (mm-read-coding-system
9489                 "View as charset: " ;; actually it is coding system.
9490                 (with-current-buffer gnus-article-buffer
9491                   (mm-detect-coding-region (point) (point-max))))))
9492           (gnus-newsgroup-ignored-charsets 'gnus-all))
9493       (gnus-summary-select-article nil 'force)
9494       (let ((deps gnus-newsgroup-dependencies)
9495             head header lines)
9496         (with-current-buffer gnus-original-article-buffer
9497           (save-restriction
9498             (message-narrow-to-head)
9499             (setq head (buffer-string))
9500             (goto-char (point-min))
9501             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9502               (goto-char (point-max))
9503               (widen)
9504               (setq lines (1- (count-lines (point) (point-max))))))
9505           (with-temp-buffer
9506             (insert (format "211 %d Article retrieved.\n"
9507                             (cdr gnus-article-current)))
9508             (insert head)
9509             (if lines (insert (format "Lines: %d\n" lines)))
9510             (insert ".\n")
9511             (let ((nntp-server-buffer (current-buffer)))
9512               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9513         (gnus-data-set-header
9514          (gnus-data-find (cdr gnus-article-current))
9515          header)
9516         (gnus-summary-update-article-line
9517          (cdr gnus-article-current) header)
9518         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9519           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9520    ((not arg)
9521     ;; Select the article the normal way.
9522     (gnus-summary-select-article nil 'force))
9523    ((or (equal arg '(16))
9524         (eq arg t))
9525     ;; C-u C-u g
9526     (let ((gnus-inhibit-article-treatments t))
9527       (gnus-summary-select-article nil 'force)))
9528    (t
9529     ;; We have to require this here to make sure that the following
9530     ;; dynamic binding isn't shadowed by autoloading.
9531     (require 'gnus-async)
9532     (require 'gnus-art)
9533     ;; Bind the article treatment functions to nil.
9534     (let ((gnus-have-all-headers t)
9535           gnus-article-prepare-hook
9536           gnus-article-decode-hook
9537           gnus-display-mime-function
9538           gnus-break-pages)
9539       ;; Destroy any MIME parts.
9540       (when (gnus-buffer-live-p gnus-article-buffer)
9541         (with-current-buffer gnus-article-buffer
9542           (mm-destroy-parts gnus-article-mime-handles)
9543           ;; Set it to nil for safety reason.
9544           (setq gnus-article-mime-handle-alist nil)
9545           (setq gnus-article-mime-handles nil)))
9546       (gnus-summary-select-article nil 'force))))
9547   (gnus-summary-goto-subject gnus-current-article)
9548   (gnus-summary-position-point))
9549
9550 (defun gnus-summary-show-raw-article ()
9551   "Show the raw article without any article massaging functions being run."
9552   (interactive)
9553   (gnus-summary-show-article t))
9554
9555 (defun gnus-summary-verbose-headers (&optional arg)
9556   "Toggle permanent full header display.
9557 If ARG is a positive number, turn header display on.
9558 If ARG is a negative number, turn header display off."
9559   (interactive "P")
9560   (setq gnus-show-all-headers
9561         (cond ((or (not (numberp arg))
9562                    (zerop arg))
9563                (not gnus-show-all-headers))
9564               ((natnump arg)
9565                t)))
9566   (gnus-summary-show-article))
9567
9568 (defun gnus-summary-toggle-header (&optional arg)
9569   "Show the headers if they are hidden, or hide them if they are shown.
9570 If ARG is a positive number, show the entire header.
9571 If ARG is a negative number, hide the unwanted header lines."
9572   (interactive "P")
9573   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9574                      (get-buffer-window gnus-article-buffer t))))
9575     (with-current-buffer gnus-article-buffer
9576       (widen)
9577       (article-narrow-to-head)
9578       (let* ((inhibit-read-only t)
9579              (inhibit-point-motion-hooks t)
9580              (hidden (if (numberp arg)
9581                          (>= arg 0)
9582                        (or (not (looking-at "[^ \t\n]+:"))
9583                            (gnus-article-hidden-text-p 'headers))))
9584              s e)
9585         (delete-region (point-min) (point-max))
9586         (with-current-buffer gnus-original-article-buffer
9587           (goto-char (setq s (point-min)))
9588           (setq e (if (search-forward "\n\n" nil t)
9589                       (1- (point))
9590                     (point-max))))
9591         (insert-buffer-substring gnus-original-article-buffer s e)
9592         (run-hooks 'gnus-article-decode-hook)
9593         (if hidden
9594             (let ((gnus-treat-hide-headers nil)
9595                   (gnus-treat-hide-boring-headers nil))
9596               (gnus-delete-wash-type 'headers)
9597               (gnus-treat-article 'head))
9598           (gnus-treat-article 'head))
9599         (widen)
9600         (if window
9601             (set-window-start window (goto-char (point-min))))
9602         (if gnus-break-pages
9603             (gnus-narrow-to-page)
9604           (when (gnus-visual-p 'page-marker)
9605             (let ((inhibit-read-only t))
9606               (gnus-remove-text-with-property 'gnus-prev)
9607               (gnus-remove-text-with-property 'gnus-next))))
9608         (gnus-set-mode-line 'article)))))
9609
9610 (defun gnus-summary-show-all-headers ()
9611   "Make all header lines visible."
9612   (interactive)
9613   (gnus-summary-toggle-header 1))
9614
9615 (defun gnus-summary-caesar-message (&optional arg)
9616   "Caesar rotate the current article by 13.
9617 With a non-numerical prefix, also rotate headers.  A numerical
9618 prefix specifies how many places to rotate each letter forward."
9619   (interactive "P")
9620   (gnus-summary-select-article)
9621   (let ((mail-header-separator ""))
9622     (gnus-eval-in-buffer-window gnus-article-buffer
9623       (save-restriction
9624         (widen)
9625         (let ((start (window-start))
9626               (inhibit-read-only t))
9627           (if (equal arg '(4))
9628               (message-caesar-buffer-body nil t)
9629             (message-caesar-buffer-body arg))
9630           (set-window-start (get-buffer-window (current-buffer)) start)))))
9631   ;; Create buttons and stuff...
9632   (gnus-treat-article nil))
9633
9634 (declare-function idna-to-unicode "ext:idna" (str))
9635
9636 (defun gnus-summary-idna-message (&optional arg)
9637   "Decode IDNA encoded domain names in the current articles.
9638 IDNA encoded domain names looks like `xn--bar'.  If a string
9639 remain unencoded after running this function, it is likely an
9640 invalid IDNA string (`xn--bar' is invalid).
9641
9642 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9643 installed for this command to work."
9644   (interactive "P")
9645   (if (not (and (condition-case nil (require 'idna)
9646                   (file-error))
9647                 (mm-coding-system-p 'utf-8)
9648                 (executable-find (symbol-value 'idna-program))))
9649       (gnus-message
9650        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9651     (gnus-summary-select-article)
9652     (let ((mail-header-separator ""))
9653       (gnus-eval-in-buffer-window gnus-article-buffer
9654         (save-restriction
9655           (widen)
9656           (let ((start (window-start))
9657                 buffer-read-only)
9658             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9659               (replace-match (idna-to-unicode (match-string 1))))
9660             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9661
9662 (defun gnus-summary-morse-message (&optional arg)
9663   "Morse decode the current article."
9664   (interactive "P")
9665   (gnus-summary-select-article)
9666   (let ((mail-header-separator ""))
9667     (gnus-eval-in-buffer-window gnus-article-buffer
9668       (save-excursion
9669         (save-restriction
9670           (widen)
9671           (let ((pos (window-start))
9672                 (inhibit-read-only t))
9673             (goto-char (point-min))
9674             (when (message-goto-body)
9675               (gnus-narrow-to-body))
9676             (goto-char (point-min))
9677             (while (search-forward "·" (point-max) t)
9678               (replace-match "."))
9679             (unmorse-region (point-min) (point-max))
9680             (widen)
9681             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9682
9683 (defun gnus-summary-stop-page-breaking ()
9684   "Stop page breaking in the current article."
9685   (interactive)
9686   (gnus-summary-select-article)
9687   (gnus-eval-in-buffer-window gnus-article-buffer
9688     (widen)
9689     (when (gnus-visual-p 'page-marker)
9690       (let ((inhibit-read-only t))
9691         (gnus-remove-text-with-property 'gnus-prev)
9692         (gnus-remove-text-with-property 'gnus-next))
9693       (setq gnus-page-broken nil))))
9694
9695 (defun gnus-summary-move-article (&optional n to-newsgroup
9696                                             select-method action)
9697   "Move the current article to a different newsgroup.
9698 If N is a positive number, move the N next articles.
9699 If N is a negative number, move the N previous articles.
9700 If N is nil and any articles have been marked with the process mark,
9701 move those articles instead.
9702 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9703 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9704 re-spool using this method.
9705
9706 When called interactively with TO-NEWSGROUP being nil, the value of
9707 the variable `gnus-move-split-methods' is used for finding a default
9708 for the target newsgroup.
9709
9710 For this function to work, both the current newsgroup and the
9711 newsgroup that you want to move to have to support the `request-move'
9712 and `request-accept' functions.
9713
9714 ACTION can be either `move' (the default), `crosspost' or `copy'."
9715   (interactive "P")
9716   (unless action
9717     (setq action 'move))
9718   ;; Check whether the source group supports the required functions.
9719   (cond ((and (eq action 'move)
9720               (not (gnus-check-backend-function
9721                     'request-move-article gnus-newsgroup-name)))
9722          (error "The current group does not support article moving"))
9723         ((and (eq action 'crosspost)
9724               (not (gnus-check-backend-function
9725                     'request-replace-article gnus-newsgroup-name)))
9726          (error "The current group does not support article editing")))
9727   (let ((articles (gnus-summary-work-articles n))
9728         (prefix (if (gnus-check-backend-function
9729                      'request-move-article gnus-newsgroup-name)
9730                     (funcall gnus-move-group-prefix-function
9731                              gnus-newsgroup-name)
9732                   ""))
9733         (names '((move "Move" "Moving")
9734                  (copy "Copy" "Copying")
9735                  (crosspost "Crosspost" "Crossposting")))
9736         (copy-buf (save-excursion
9737                     (nnheader-set-temp-buffer " *copy article*")))
9738         art-group to-method new-xref article to-groups
9739         articles-to-update-marks encoded)
9740     (unless (assq action names)
9741       (error "Unknown action %s" action))
9742     ;; Read the newsgroup name.
9743     (when (and (not to-newsgroup)
9744                (not select-method))
9745       (if (and gnus-move-split-methods
9746                (not
9747                 (and (memq gnus-current-article articles)
9748                      (gnus-buffer-live-p gnus-original-article-buffer))))
9749           ;; When `gnus-move-split-methods' is non-nil, we have to
9750           ;; select an article to give `gnus-read-move-group-name' an
9751           ;; opportunity to suggest an appropriate default.  However,
9752           ;; we needn't render or mark the article.
9753           (let ((gnus-display-mime-function nil)
9754                 (gnus-article-prepare-hook nil)
9755                 (gnus-mark-article-hook nil))
9756             (gnus-summary-select-article nil nil nil (car articles))))
9757       (setq to-newsgroup (gnus-read-move-group-name
9758                           (cadr (assq action names))
9759                           (symbol-value
9760                            (intern (format "gnus-current-%s-group" action)))
9761                           articles prefix)
9762             encoded to-newsgroup
9763             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9764       (set (intern (format "gnus-current-%s-group" action))
9765            (mm-decode-coding-string
9766             to-newsgroup
9767             (gnus-group-name-charset to-method to-newsgroup))))
9768     (unless to-method
9769       (setq to-method (or select-method
9770                           (gnus-server-to-method
9771                            (gnus-group-method to-newsgroup)))))
9772     (setq to-newsgroup
9773           (or encoded
9774               (and to-newsgroup
9775                    (mm-encode-coding-string
9776                     to-newsgroup
9777                     (gnus-group-name-charset to-method to-newsgroup)))))
9778     ;; Check the method we are to move this article to...
9779     (unless (gnus-check-backend-function
9780              'request-accept-article (car to-method))
9781       (error "%s does not support article copying" (car to-method)))
9782     (unless (gnus-check-server to-method)
9783       (error "Can't open server %s" (car to-method)))
9784     (gnus-message 6 "%s to %s: %s..."
9785                   (caddr (assq action names))
9786                   (or (car select-method)
9787                       (gnus-group-decoded-name to-newsgroup))
9788                   articles)
9789     (while articles
9790       (setq article (pop articles))
9791       ;; Set any marks that may have changed in the summary buffer.
9792       (when gnus-preserve-marks
9793         (gnus-summary-push-marks-to-backend article))
9794       (setq
9795        art-group
9796        (cond
9797         ;; Move the article.
9798         ((eq action 'move)
9799          ;; Remove this article from future suppression.
9800          (gnus-dup-unsuppress-article article)
9801          (let* ((from-method (gnus-find-method-for-group
9802                               gnus-newsgroup-name))
9803                 (to-method (or select-method
9804                                (gnus-find-method-for-group to-newsgroup)))
9805                 (move-is-internal (gnus-server-equal from-method to-method)))
9806            (gnus-request-move-article
9807             article                     ; Article to move
9808             gnus-newsgroup-name         ; From newsgroup
9809             (nth 1 (gnus-find-method-for-group
9810                     gnus-newsgroup-name)) ; Server
9811             (list 'gnus-request-accept-article
9812                   to-newsgroup (list 'quote select-method)
9813                   (not articles) t)     ; Accept form
9814             (not articles)              ; Only save nov last time
9815             (and move-is-internal
9816                  to-newsgroup           ; Not respooling
9817                                         ; Is this move internal?
9818                  (gnus-group-real-name to-newsgroup)))))
9819         ;; Copy the article.
9820         ((eq action 'copy)
9821          (with-current-buffer copy-buf
9822            (when (gnus-request-article-this-buffer article
9823                                                    gnus-newsgroup-name)
9824              (save-restriction
9825                (nnheader-narrow-to-headers)
9826                (dolist (hdr gnus-copy-article-ignored-headers)
9827                  (message-remove-header hdr t)))
9828              (gnus-request-accept-article
9829               to-newsgroup select-method (not articles) t))))
9830         ;; Crosspost the article.
9831         ((eq action 'crosspost)
9832          (let ((xref (message-tokenize-header
9833                       (mail-header-xref (gnus-summary-article-header
9834                                          article))
9835                       " ")))
9836            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9837                                   ":" (number-to-string article)))
9838            (unless xref
9839              (setq xref (list (system-name))))
9840            (setq new-xref
9841                  (concat
9842                   (mapconcat 'identity
9843                              (delete "Xref:" (delete new-xref xref))
9844                              " ")
9845                   " " new-xref))
9846            (with-current-buffer copy-buf
9847              ;; First put the article in the destination group.
9848              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9849              (when (consp (setq art-group
9850                                 (gnus-request-accept-article
9851                                  to-newsgroup select-method (not articles)
9852                                  t)))
9853                (setq new-xref (concat new-xref " " (car art-group)
9854                                       ":"
9855                                       (number-to-string (cdr art-group))))
9856                ;; Now we have the new Xrefs header, so we insert
9857                ;; it and replace the new article.
9858                (nnheader-replace-header "Xref" new-xref)
9859                (gnus-request-replace-article
9860                 (cdr art-group) to-newsgroup (current-buffer) t)
9861                art-group))))))
9862       (cond
9863        ((not art-group)
9864         (gnus-message 1 "Couldn't %s article %s: %s"
9865                       (cadr (assq action names)) article
9866                       (nnheader-get-report (car to-method))))
9867        ((eq art-group 'junk)
9868         (when (eq action 'move)
9869           (gnus-summary-mark-article article gnus-canceled-mark)
9870           (gnus-message 4 "Deleted article %s" article)
9871           ;; run the delete hook
9872           (run-hook-with-args 'gnus-summary-article-delete-hook
9873                               action
9874                               (gnus-data-header
9875                                (assoc article (gnus-data-list nil)))
9876                               gnus-newsgroup-name nil
9877                               select-method)))
9878        (t
9879         (let* ((pto-group (gnus-group-prefixed-name
9880                            (car art-group) to-method))
9881                (info (gnus-get-info pto-group))
9882                (to-group (gnus-info-group info))
9883                to-marks)
9884           ;; Update the group that has been moved to.
9885           (when (and info
9886                      (memq action '(move copy)))
9887             (unless (member to-group to-groups)
9888               (push to-group to-groups))
9889
9890             (when (and (not (memq article gnus-newsgroup-unreads))
9891                        (cdr art-group))
9892               (push 'read to-marks)
9893               (gnus-info-set-read
9894                info (gnus-add-to-range (gnus-info-read info)
9895                                        (list (cdr art-group)))))
9896
9897             ;; See whether the article is to be put in the cache.
9898             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9899                    (marks (if expirable
9900                               gnus-article-mark-lists
9901                             (delete '(expirable . expire)
9902                                     (copy-sequence
9903                                      gnus-article-mark-lists))))
9904                    (to-article (cdr art-group)))
9905
9906               ;; Enter the article into the cache in the new group,
9907               ;; if that is required.
9908               (when (and to-article
9909                          gnus-use-cache)
9910                 (gnus-cache-possibly-enter-article
9911                  to-group to-article
9912                  (memq article gnus-newsgroup-marked)
9913                  (memq article gnus-newsgroup-dormant)
9914                  (memq article gnus-newsgroup-unreads)))
9915
9916               (when (and gnus-preserve-marks
9917                          to-article)
9918                 ;; Copy any marks over to the new group.
9919                 (when (and (equal to-group gnus-newsgroup-name)
9920                            (not (memq article gnus-newsgroup-unreads)))
9921                   ;; Mark this article as read in this group.
9922                   (push (cons to-article gnus-read-mark)
9923                         gnus-newsgroup-reads)
9924                   ;; Increase the active status of this group.
9925                   (setcdr (gnus-active to-group) to-article)
9926                   (setcdr gnus-newsgroup-active to-article))
9927
9928                 (while marks
9929                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9930                     (when (memq article (symbol-value
9931                                          (intern (format "gnus-newsgroup-%s"
9932                                                          (caar marks)))))
9933                       (push (cdar marks) to-marks)
9934                       ;; If the other group is the same as this group,
9935                       ;; then we have to add the mark to the list.
9936                       (when (equal to-group gnus-newsgroup-name)
9937                         (set (intern (format "gnus-newsgroup-%s"
9938                                              (caar marks)))
9939                              (cons to-article
9940                                    (symbol-value
9941                                     (intern (format "gnus-newsgroup-%s"
9942                                                     (caar marks)))))))
9943                       ;; Copy the marks to other group.
9944                       (gnus-add-marked-articles
9945                        to-group (cdar marks) (list to-article) info)))
9946                   (setq marks (cdr marks)))
9947
9948                 (when (and expirable
9949                            gnus-mark-copied-or-moved-articles-as-expirable
9950                            (not (memq 'expire to-marks)))
9951                   ;; Mark this article as expirable.
9952                   (push 'expire to-marks)
9953                   (when (equal to-group gnus-newsgroup-name)
9954                     (push to-article gnus-newsgroup-expirable))
9955                   ;; Copy the expirable mark to other group.
9956                   (gnus-add-marked-articles
9957                    to-group 'expire (list to-article) info))
9958
9959                 (when to-marks
9960                   (gnus-request-set-mark
9961                    to-group (list (list (list to-article) 'add to-marks)))))
9962
9963               (gnus-dribble-enter
9964                (concat "(gnus-group-set-info '"
9965                        (gnus-prin1-to-string (gnus-get-info to-group))
9966                        ")"))))
9967
9968           ;; Update the Xref header in this article to point to
9969           ;; the new crossposted article we have just created.
9970           (when (eq action 'crosspost)
9971             (with-current-buffer copy-buf
9972               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9973               (nnheader-replace-header "Xref" new-xref)
9974               (gnus-request-replace-article
9975                article gnus-newsgroup-name (current-buffer) t)))
9976
9977           ;; run the move/copy/crosspost/respool hook
9978           (run-hook-with-args 'gnus-summary-article-move-hook
9979                               action
9980                               (gnus-data-header
9981                                (assoc article (gnus-data-list nil)))
9982                               gnus-newsgroup-name
9983                               to-newsgroup
9984                               select-method))
9985
9986         ;;;!!!Why is this necessary?
9987         (set-buffer gnus-summary-buffer)
9988
9989         (when (eq action 'move)
9990           (save-excursion
9991             (gnus-summary-goto-subject article)
9992             (gnus-summary-mark-article article gnus-canceled-mark)))))
9993       (push article articles-to-update-marks))
9994
9995     (save-excursion
9996       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
9997     ;; Re-activate all groups that have been moved to.
9998     (with-current-buffer gnus-group-buffer
9999       (let ((gnus-group-marked to-groups))
10000         (gnus-group-get-new-news-this-group nil t)))
10001
10002     (gnus-kill-buffer copy-buf)
10003     (gnus-summary-position-point)
10004     (gnus-set-mode-line 'summary)))
10005
10006 (defun gnus-summary-push-marks-to-backend (article)
10007   (let ((set nil)
10008         (marks gnus-article-mark-lists))
10009     (unless (memq article gnus-newsgroup-unreads)
10010       (push 'read set))
10011     (while marks
10012       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10013                  (memq article (symbol-value
10014                                 (intern (format "gnus-newsgroup-%s"
10015                                                 (caar marks))))))
10016         (push (cdar marks) set))
10017       (pop marks))
10018     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
10019
10020 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10021   "Copy the current article to some other group.
10022 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10023 When called interactively, if TO-NEWSGROUP is nil, use the value of
10024 the variable `gnus-move-split-methods' for finding a default target
10025 newsgroup.
10026 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10027 re-spool using this method."
10028   (interactive "P")
10029   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10030
10031 (defun gnus-summary-crosspost-article (&optional n)
10032   "Crosspost the current article to some other group."
10033   (interactive "P")
10034   (gnus-summary-move-article n nil nil 'crosspost))
10035
10036 (defcustom gnus-summary-respool-default-method nil
10037   "Default method type for respooling an article.
10038 If nil, use to the current newsgroup method."
10039   :type 'symbol
10040   :group 'gnus-summary-mail)
10041
10042 (defun gnus-summary-respool-article (&optional n method)
10043   "Respool the current article.
10044 The article will be squeezed through the mail spooling process again,
10045 which means that it will be put in some mail newsgroup or other
10046 depending on `nnmail-split-methods'.
10047 If N is a positive number, respool the N next articles.
10048 If N is a negative number, respool the N previous articles.
10049 If N is nil and any articles have been marked with the process mark,
10050 respool those articles instead.
10051
10052 Respooling can be done both from mail groups and \"real\" newsgroups.
10053 In the former case, the articles in question will be moved from the
10054 current group into whatever groups they are destined to.  In the
10055 latter case, they will be copied into the relevant groups."
10056   (interactive
10057    (list current-prefix-arg
10058          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10059                 (methname
10060                  (symbol-name (or gnus-summary-respool-default-method
10061                                   (car (gnus-find-method-for-group
10062                                         gnus-newsgroup-name)))))
10063                 (method
10064                  (gnus-completing-read
10065                   "Backend to use when respooling"
10066                   methods t nil 'gnus-mail-method-history methname))
10067                 ms)
10068            (cond
10069             ((zerop (length (setq ms (gnus-servers-using-backend
10070                                       (intern method)))))
10071              (list (intern method) ""))
10072             ((= 1 (length ms))
10073              (car ms))
10074             (t
10075              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10076                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10077                            ms-alist))))))))
10078   (unless method
10079     (error "No method given for respooling"))
10080   (if (assoc (symbol-name
10081               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10082              (gnus-methods-using 'respool))
10083       (gnus-summary-move-article n nil method)
10084     (gnus-summary-copy-article n nil method)))
10085
10086 (defun gnus-summary-import-article (file &optional edit)
10087   "Import an arbitrary file into a mail newsgroup."
10088   (interactive "fImport file: \nP")
10089   (let ((group gnus-newsgroup-name)
10090         (now (current-time))
10091         atts lines group-art)
10092     (unless (gnus-check-backend-function 'request-accept-article group)
10093       (error "%s does not support article importing" group))
10094     (or (file-readable-p file)
10095         (not (file-regular-p file))
10096         (error "Can't read %s" file))
10097     (with-current-buffer (gnus-get-buffer-create " *import file*")
10098       (erase-buffer)
10099       (nnheader-insert-file-contents file)
10100       (goto-char (point-min))
10101       (if (nnheader-article-p)
10102           (save-restriction
10103             (goto-char (point-min))
10104             (search-forward "\n\n" nil t)
10105             (narrow-to-region (point-min) (1- (point)))
10106             (goto-char (point-min))
10107             (unless (re-search-forward "^date:" nil t)
10108               (goto-char (point-max))
10109               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10110        ;; This doesn't look like an article, so we fudge some headers.
10111         (setq atts (file-attributes file)
10112               lines (count-lines (point-min) (point-max)))
10113         (insert "From: " (read-string "From: ") "\n"
10114                 "Subject: " (read-string "Subject: ") "\n"
10115                 "Date: " (message-make-date (nth 5 atts)) "\n"
10116                 "Message-ID: " (message-make-message-id) "\n"
10117                 "Lines: " (int-to-string lines) "\n"
10118                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10119       (setq group-art (gnus-request-accept-article group nil t))
10120       (kill-buffer (current-buffer)))
10121     (setq gnus-newsgroup-active (gnus-activate-group group))
10122     (forward-line 1)
10123     (gnus-summary-goto-article (cdr group-art) nil t)
10124     (when edit
10125       (gnus-summary-edit-article))))
10126
10127 (defun gnus-summary-create-article ()
10128   "Create an article in a mail newsgroup."
10129   (interactive)
10130   (let ((group gnus-newsgroup-name)
10131         (now (current-time))
10132         group-art)
10133     (unless (gnus-check-backend-function 'request-accept-article group)
10134       (error "%s does not support article importing" group))
10135     (with-current-buffer (gnus-get-buffer-create " *import file*")
10136       (erase-buffer)
10137       (goto-char (point-min))
10138       ;; This doesn't look like an article, so we fudge some headers.
10139       (insert "From: " (read-string "From: ") "\n"
10140               "Subject: " (read-string "Subject: ") "\n"
10141               "Date: " (message-make-date now) "\n"
10142               "Message-ID: " (message-make-message-id) "\n")
10143       (setq group-art (gnus-request-accept-article group nil t))
10144       (kill-buffer (current-buffer)))
10145     (setq gnus-newsgroup-active (gnus-activate-group group))
10146     (forward-line 1)
10147     (gnus-summary-goto-article (cdr group-art) nil t)
10148     (gnus-summary-edit-article)))
10149
10150 (defun gnus-summary-article-posted-p ()
10151   "Say whether the current (mail) article is available from news as well.
10152 This will be the case if the article has both been mailed and posted."
10153   (interactive)
10154   (let ((id (mail-header-references (gnus-summary-article-header)))
10155         (gnus-override-method (car (gnus-refer-article-methods))))
10156     (if (gnus-request-head id "")
10157         (gnus-message 2 "The current message was found on %s"
10158                       gnus-override-method)
10159       (gnus-message 2 "The current message couldn't be found on %s"
10160                     gnus-override-method)
10161       nil)))
10162
10163 (defun gnus-summary-expire-articles (&optional now)
10164   "Expire all articles that are marked as expirable in the current group."
10165   (interactive)
10166   (when (and (not gnus-group-is-exiting-without-update-p)
10167              (gnus-check-backend-function
10168               'request-expire-articles gnus-newsgroup-name))
10169     ;; This backend supports expiry.
10170     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10171            (expirable (if total
10172                           (progn
10173                             ;; We need to update the info for
10174                             ;; this group for `gnus-list-of-read-articles'
10175                             ;; to give us the right answer.
10176                             (gnus-run-hooks 'gnus-exit-group-hook)
10177                             (gnus-summary-update-info)
10178                             (gnus-list-of-read-articles gnus-newsgroup-name))
10179                         (setq gnus-newsgroup-expirable
10180                               (sort gnus-newsgroup-expirable '<))))
10181            (expiry-wait (if now 'immediate
10182                           (gnus-group-find-parameter
10183                            gnus-newsgroup-name 'expiry-wait)))
10184            (nnmail-expiry-target
10185             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10186                 nnmail-expiry-target))
10187            es)
10188       (when expirable
10189         ;; There are expirable articles in this group, so we run them
10190         ;; through the expiry process.
10191         (gnus-message 6 "Expiring articles...")
10192         (unless (gnus-check-group gnus-newsgroup-name)
10193           (error "Can't open server for %s" gnus-newsgroup-name))
10194         ;; The list of articles that weren't expired is returned.
10195         (save-excursion
10196           (if expiry-wait
10197               (let ((nnmail-expiry-wait-function nil)
10198                     (nnmail-expiry-wait expiry-wait))
10199                 (setq es (gnus-request-expire-articles
10200                           expirable gnus-newsgroup-name)))
10201             (setq es (gnus-request-expire-articles
10202                       expirable gnus-newsgroup-name)))
10203           (unless total
10204             (setq gnus-newsgroup-expirable es))
10205           ;; We go through the old list of expirable, and mark all
10206           ;; really expired articles as nonexistent.
10207           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10208             (let ((gnus-use-cache nil))
10209               (dolist (article expirable)
10210                 (when (and (not (memq article es))
10211                            (gnus-data-find article))
10212                   (gnus-summary-mark-article article gnus-canceled-mark)
10213                   (run-hook-with-args 'gnus-summary-article-expire-hook
10214                                       'delete
10215                                       (gnus-data-header
10216                                        (assoc article (gnus-data-list nil)))
10217                                       gnus-newsgroup-name
10218                                       nil
10219                                       nil))))))
10220         (gnus-message 6 "Expiring articles...done")))))
10221
10222 (defun gnus-summary-expire-articles-now ()
10223   "Expunge all expirable articles in the current group.
10224 This means that *all* articles that are marked as expirable will be
10225 deleted forever, right now."
10226   (interactive)
10227   (or gnus-expert-user
10228       (gnus-yes-or-no-p
10229        "Are you really, really sure you want to delete all expirable messages? ")
10230       (error "Phew!"))
10231   (gnus-summary-expire-articles t))
10232
10233 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10234 (defun gnus-summary-delete-article (&optional n)
10235   "Delete the N next (mail) articles.
10236 This command actually deletes articles.  This is not a marking
10237 command.  The article will disappear forever from your life, never to
10238 return.
10239
10240 If N is negative, delete backwards.
10241 If N is nil and articles have been marked with the process mark,
10242 delete these instead.
10243
10244 If `gnus-novice-user' is non-nil you will be asked for
10245 confirmation before the articles are deleted."
10246   (interactive "P")
10247   (unless (gnus-check-backend-function 'request-expire-articles
10248                                        gnus-newsgroup-name)
10249     (error "The current newsgroup does not support article deletion"))
10250   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10251     (error "Couldn't open server"))
10252   ;; Compute the list of articles to delete.
10253   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10254         (nnmail-expiry-target 'delete)
10255         not-deleted)
10256     (if (and gnus-novice-user
10257              (not (gnus-yes-or-no-p
10258                    (format "Do you really want to delete %s forever? "
10259                            (if (> (length articles) 1)
10260                                (format "these %s articles" (length articles))
10261                              "this article")))))
10262         ()
10263       ;; Delete the articles.
10264       (setq not-deleted (gnus-request-expire-articles
10265                          articles gnus-newsgroup-name 'force))
10266       (save-excursion
10267         (while articles
10268           (gnus-summary-remove-process-mark (car articles))
10269           ;; The backend might not have been able to delete the article
10270           ;; after all.
10271           (unless (memq (car articles) not-deleted)
10272             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10273             (let* ((article (car articles))
10274                    (ghead  (gnus-data-header
10275                             (assoc article (gnus-data-list nil)))))
10276               (run-hook-with-args 'gnus-summary-article-delete-hook
10277                                   'delete ghead gnus-newsgroup-name nil
10278                                   nil)))
10279           (setq articles (cdr articles))))
10280       (when not-deleted
10281         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10282     (gnus-summary-position-point)
10283     (gnus-set-mode-line 'summary)
10284     not-deleted))
10285
10286 (defun gnus-summary-edit-article (&optional arg)
10287   "Edit the current article.
10288 This will have permanent effect only in mail groups.
10289 If ARG is nil, edit the decoded articles.
10290 If ARG is 1, edit the raw articles.
10291 If ARG is 2, edit the raw articles even in read-only groups.
10292 If ARG is 3, edit the articles with the current handles.
10293 Otherwise, allow editing of articles even in read-only
10294 groups."
10295   (interactive "P")
10296   (let (force raw current-handles)
10297     (cond
10298      ((null arg))
10299      ((eq arg 1)
10300       (setq raw t))
10301      ((eq arg 2)
10302       (setq raw t
10303             force t))
10304      ((eq arg 3)
10305       (setq current-handles
10306             (and (gnus-buffer-live-p gnus-article-buffer)
10307                  (with-current-buffer gnus-article-buffer
10308                    (prog1
10309                        gnus-article-mime-handles
10310                      (setq gnus-article-mime-handles nil))))))
10311      (t
10312       (setq force t)))
10313     (when (and raw (not force)
10314                (member gnus-newsgroup-name '("nndraft:delayed"
10315                                              "nndraft:drafts"
10316                                              "nndraft:queue")))
10317       (error "Can't edit the raw article in group %s"
10318              gnus-newsgroup-name))
10319     (with-current-buffer gnus-summary-buffer
10320       (let ((mail-parse-charset gnus-newsgroup-charset)
10321             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10322         (gnus-set-global-variables)
10323         (when (and (not force)
10324                    (gnus-group-read-only-p))
10325           (error "The current newsgroup does not support article editing"))
10326         (gnus-summary-show-article t)
10327         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10328           (with-current-buffer gnus-article-buffer
10329             (mm-enable-multibyte)))
10330         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10331             (setq raw t))
10332         (gnus-article-edit-article
10333          (if raw 'ignore
10334            `(lambda ()
10335               (let ((mbl mml-buffer-list))
10336                 (setq mml-buffer-list nil)
10337                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10338                   (mime-to-mml ,'current-handles))
10339                 (let ((mbl1 mml-buffer-list))
10340                   (setq mml-buffer-list mbl)
10341                   (set (make-local-variable 'mml-buffer-list) mbl1))
10342                 (gnus-make-local-hook 'kill-buffer-hook)
10343                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10344          `(lambda (no-highlight)
10345             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10346                   (message-options message-options)
10347                   (message-options-set-recipient)
10348                   (mail-parse-ignored-charsets
10349                    ',gnus-newsgroup-ignored-charsets)
10350                   (rfc2047-header-encoding-alist
10351                    ',(let ((charset (gnus-group-name-charset
10352                                      (gnus-find-method-for-group
10353                                       gnus-newsgroup-name)
10354                                      gnus-newsgroup-name)))
10355                        (append (list (cons "Newsgroups" charset)
10356                                      (cons "Followup-To" charset)
10357                                      (cons "Xref" charset))
10358                                rfc2047-header-encoding-alist))))
10359               ,(if (not raw) '(progn
10360                                 (mml-to-mime)
10361                                 (mml-destroy-buffers)
10362                                 (remove-hook 'kill-buffer-hook
10363                                              'mml-destroy-buffers t)
10364                                 (kill-local-variable 'mml-buffer-list)))
10365               (gnus-summary-edit-article-done
10366                ,(or (mail-header-references gnus-current-headers) "")
10367                ,(gnus-group-read-only-p)
10368                ,gnus-summary-buffer no-highlight))))))))
10369
10370 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10371
10372 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10373                                                  no-highlight)
10374   "Make edits to the current article permanent."
10375   (interactive)
10376   (save-excursion
10377     ;; The buffer restriction contains the entire article if it exists.
10378     (when (article-goto-body)
10379       (let ((lines (count-lines (point) (point-max)))
10380             (length (- (point-max) (point)))
10381             (case-fold-search t)
10382             (body (copy-marker (point))))
10383         (goto-char (point-min))
10384         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10385           (delete-region (match-beginning 1) (match-end 1))
10386           (insert (number-to-string length)))
10387         (goto-char (point-min))
10388         (when (re-search-forward
10389                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10390           (delete-region (match-beginning 1) (match-end 1))
10391           (insert (number-to-string length)))
10392         (goto-char (point-min))
10393         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10394           (delete-region (match-beginning 1) (match-end 1))
10395           (insert (number-to-string lines))))))
10396   ;; Replace the article.
10397   (let ((buf (current-buffer))
10398         (article (cdr gnus-article-current))
10399         replace-result)
10400     (with-temp-buffer
10401       (insert-buffer-substring buf)
10402       (if (and (not read-only)
10403                (not (setq replace-result
10404                           (gnus-request-replace-article
10405                            article (car gnus-article-current)
10406                            (current-buffer) t))))
10407           (error "Couldn't replace article")
10408         ;; If we got a number back, then that's the new article number
10409         ;; for this article.  Otherwise, the article number didn't change.
10410         (when (numberp replace-result)
10411           (with-current-buffer gnus-summary-buffer
10412             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10413             (gnus-summary-limit gnus-newsgroup-limit)
10414             (setq article replace-result)
10415             (gnus-summary-goto-subject article t)))
10416         ;; Update the summary buffer.
10417         (if (and references
10418                  (equal (message-tokenize-header references " ")
10419                         (message-tokenize-header
10420                          (or (message-fetch-field "references") "") " ")))
10421             ;; We only have to update this line.
10422             (save-excursion
10423               (save-restriction
10424                 (message-narrow-to-head)
10425                 (let ((head (buffer-substring-no-properties
10426                              (point-min) (point-max)))
10427                       header)
10428                   (with-temp-buffer
10429                     (insert (format "211 %d Article retrieved.\n" article))
10430                     (insert head)
10431                     (insert ".\n")
10432                     (let ((nntp-server-buffer (current-buffer)))
10433                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10434                     (with-current-buffer gnus-summary-buffer
10435                       (gnus-data-set-header (gnus-data-find article) header)
10436                       (gnus-summary-update-article-line article header)
10437                       (if (gnus-summary-goto-subject article nil t)
10438                           (gnus-summary-update-secondary-mark article)))))))
10439           ;; Update threads.
10440           (set-buffer (or buffer gnus-summary-buffer))
10441           (gnus-summary-update-article article)
10442           (if (gnus-summary-goto-subject article nil t)
10443               (gnus-summary-update-secondary-mark article)))
10444         ;; Prettify the article buffer again.
10445         (unless no-highlight
10446           (with-current-buffer gnus-article-buffer
10447             ;;!!! Fix this -- article should be rehighlighted.
10448             ;;(gnus-run-hooks 'gnus-article-display-hook)
10449             (set-buffer gnus-original-article-buffer)
10450             (gnus-request-article
10451              article (car gnus-article-current) (current-buffer))))
10452         ;; Prettify the summary buffer line.
10453         (when (gnus-visual-p 'summary-highlight 'highlight)
10454           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10455
10456 (defun gnus-summary-edit-wash (key)
10457   "Perform editing command KEY in the article buffer."
10458   (interactive
10459    (list
10460     (progn
10461       (message "%s" (concat (this-command-keys) "- "))
10462       (read-char))))
10463   (message "")
10464   (gnus-summary-edit-article)
10465   (execute-kbd-macro (concat (this-command-keys) key))
10466   (gnus-article-edit-done))
10467
10468 ;;; Respooling
10469
10470 (defun gnus-summary-respool-query (&optional silent trace)
10471   "Query where the respool algorithm would put this article."
10472   (interactive)
10473   (let (gnus-mark-article-hook)
10474     (gnus-summary-select-article)
10475     (with-current-buffer gnus-original-article-buffer
10476       (let ((groups (nnmail-article-group 'identity trace)))
10477         (unless silent
10478           (if groups
10479               (message "This message would go to %s"
10480                        (mapconcat 'car groups ", "))
10481             (message "This message would go to no groups"))
10482           groups)))))
10483
10484 (defun gnus-summary-respool-trace ()
10485   "Trace where the respool algorithm would put this article.
10486 Display a buffer showing all fancy splitting patterns which matched."
10487   (interactive)
10488   (gnus-summary-respool-query nil t))
10489
10490 ;; Summary marking commands.
10491
10492 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10493   "Mark articles which has the same subject as read, and then select the next.
10494 If UNMARK is positive, remove any kind of mark.
10495 If UNMARK is negative, tick articles."
10496   (interactive "P")
10497   (when unmark
10498     (setq unmark (prefix-numeric-value unmark)))
10499   (let ((count
10500          (gnus-summary-mark-same-subject
10501           (gnus-summary-article-subject) unmark)))
10502     ;; Select next unread article.  If auto-select-same mode, should
10503     ;; select the first unread article.
10504     (gnus-summary-next-article t (and gnus-auto-select-same
10505                                       (gnus-summary-article-subject)))
10506     (gnus-message 7 "%d article%s marked as %s"
10507                   count (if (= count 1) " is" "s are")
10508                   (if unmark "unread" "read"))))
10509
10510 (defun gnus-summary-kill-same-subject (&optional unmark)
10511   "Mark articles which has the same subject as read.
10512 If UNMARK is positive, remove any kind of mark.
10513 If UNMARK is negative, tick articles."
10514   (interactive "P")
10515   (when unmark
10516     (setq unmark (prefix-numeric-value unmark)))
10517   (let ((count
10518          (gnus-summary-mark-same-subject
10519           (gnus-summary-article-subject) unmark)))
10520     ;; If marked as read, go to next unread subject.
10521     (when (null unmark)
10522       ;; Go to next unread subject.
10523       (gnus-summary-next-subject 1 t))
10524     (gnus-message 7 "%d articles are marked as %s"
10525                   count (if unmark "unread" "read"))))
10526
10527 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10528   "Mark articles with same SUBJECT as read, and return marked number.
10529 If optional argument UNMARK is positive, remove any kinds of marks.
10530 If optional argument UNMARK is negative, mark articles as unread instead."
10531   (let ((count 1))
10532     (save-excursion
10533       (cond
10534        ((null unmark)                   ; Mark as read.
10535         (while (and
10536                 (progn
10537                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10538                   (gnus-summary-show-thread) t)
10539                 (gnus-summary-find-subject subject))
10540           (setq count (1+ count))))
10541        ((> unmark 0)                    ; Tick.
10542         (while (and
10543                 (progn
10544                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10545                   (gnus-summary-show-thread) t)
10546                 (gnus-summary-find-subject subject))
10547           (setq count (1+ count))))
10548        (t                               ; Mark as unread.
10549         (while (and
10550                 (progn
10551                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10552                   (gnus-summary-show-thread) t)
10553                 (gnus-summary-find-subject subject))
10554           (setq count (1+ count)))))
10555       (gnus-set-mode-line 'summary)
10556       ;; Return the number of marked articles.
10557       count)))
10558
10559 (defun gnus-summary-mark-as-processable (n &optional unmark)
10560   "Set the process mark on the next N articles.
10561 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10562 the process mark instead.  The difference between N and the actual
10563 number of articles marked is returned."
10564   (interactive "P")
10565   (if (and (null n) (gnus-region-active-p))
10566       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10567     (setq n (prefix-numeric-value n))
10568     (let ((backward (< n 0))
10569           (n (abs n)))
10570       (while (and
10571               (> n 0)
10572               (if unmark
10573                   (gnus-summary-remove-process-mark
10574                    (gnus-summary-article-number))
10575                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10576               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10577         (setq n (1- n)))
10578       (when (/= 0 n)
10579         (gnus-message 7 "No more articles"))
10580       (gnus-summary-recenter)
10581       (gnus-summary-position-point)
10582       n)))
10583
10584 (defun gnus-summary-unmark-as-processable (n)
10585   "Remove the process mark from the next N articles.
10586 If N is negative, unmark backward instead.  The difference between N and
10587 the actual number of articles unmarked is returned."
10588   (interactive "P")
10589   (gnus-summary-mark-as-processable n t))
10590
10591 (defun gnus-summary-unmark-all-processable ()
10592   "Remove the process mark from all articles."
10593   (interactive)
10594   (save-excursion
10595     (while gnus-newsgroup-processable
10596       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10597   (gnus-summary-position-point))
10598
10599 (defun gnus-summary-add-mark (article type)
10600   "Mark ARTICLE with a mark of TYPE."
10601   (let ((vtype (car (assq type gnus-article-mark-lists)))
10602         var)
10603     (if (not vtype)
10604         (error "No such mark type: %s" type)
10605       (setq var (intern (format "gnus-newsgroup-%s" type)))
10606       (set var (cons article (symbol-value var)))
10607       (if (memq type '(processable cached replied forwarded recent saved))
10608           (gnus-summary-update-secondary-mark article)
10609         ;;; !!! This is bogus.  We should find out what primary
10610         ;;; !!! mark we want to set.
10611         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10612
10613 (defun gnus-summary-mark-as-expirable (n)
10614   "Mark N articles forward as expirable.
10615 If N is negative, mark backward instead.  The difference between N and
10616 the actual number of articles marked is returned."
10617   (interactive "p")
10618   (gnus-summary-mark-forward n gnus-expirable-mark))
10619
10620 (defun gnus-summary-mark-as-spam (n)
10621   "Mark N articles forward as spam.
10622 If N is negative, mark backward instead.  The difference between N and
10623 the actual number of articles marked is returned."
10624   (interactive "p")
10625   (gnus-summary-mark-forward n gnus-spam-mark))
10626
10627 (defun gnus-summary-mark-article-as-replied (article)
10628   "Mark ARTICLE as replied to and update the summary line.
10629 ARTICLE can also be a list of articles."
10630   (interactive (list (gnus-summary-article-number)))
10631   (let ((articles (if (listp article) article (list article))))
10632     (dolist (article articles)
10633       (unless (numberp article)
10634         (error "%s is not a number" article))
10635       (push article gnus-newsgroup-replied)
10636       (let ((inhibit-read-only t))
10637         (when (gnus-summary-goto-subject article nil t)
10638           (gnus-summary-update-secondary-mark article))))))
10639
10640 (defun gnus-summary-mark-article-as-forwarded (article)
10641   "Mark ARTICLE as forwarded and update the summary line.
10642 ARTICLE can also be a list of articles."
10643   (let ((articles (if (listp article) article (list article))))
10644     (dolist (article articles)
10645       (push article gnus-newsgroup-forwarded)
10646       (let ((inhibit-read-only t))
10647         (when (gnus-summary-goto-subject article nil t)
10648           (gnus-summary-update-secondary-mark article))))))
10649
10650 (defun gnus-summary-set-bookmark (article)
10651   "Set a bookmark in current article."
10652   (interactive (list (gnus-summary-article-number)))
10653   (when (or (not (get-buffer gnus-article-buffer))
10654             (not gnus-current-article)
10655             (not gnus-article-current)
10656             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10657     (error "No current article selected"))
10658   ;; Remove old bookmark, if one exists.
10659   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10660   ;; Set the new bookmark, which is on the form
10661   ;; (article-number . line-number-in-body).
10662   (push
10663    (cons article
10664          (with-current-buffer gnus-article-buffer
10665            (count-lines
10666             (min (point)
10667                  (save-excursion
10668                    (article-goto-body)
10669                    (point)))
10670             (point))))
10671    gnus-newsgroup-bookmarks)
10672   (gnus-message 6 "A bookmark has been added to the current article."))
10673
10674 (defun gnus-summary-remove-bookmark (article)
10675   "Remove the bookmark from the current article."
10676   (interactive (list (gnus-summary-article-number)))
10677   ;; Remove old bookmark, if one exists.
10678   (if (not (assq article gnus-newsgroup-bookmarks))
10679       (gnus-message 6 "No bookmark in current article.")
10680     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10681     (gnus-message 6 "Removed bookmark.")))
10682
10683 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10684 (defun gnus-summary-mark-as-dormant (n)
10685   "Mark N articles forward as dormant.
10686 If N is negative, mark backward instead.  The difference between N and
10687 the actual number of articles marked is returned."
10688   (interactive "p")
10689   (gnus-summary-mark-forward n gnus-dormant-mark))
10690
10691 (defun gnus-summary-set-process-mark (article)
10692   "Set the process mark on ARTICLE and update the summary line."
10693   (setq gnus-newsgroup-processable
10694         (cons article
10695               (delq article gnus-newsgroup-processable)))
10696   (when (gnus-summary-goto-subject article)
10697     (gnus-summary-show-thread)
10698     (gnus-summary-goto-subject article)
10699     (gnus-summary-update-secondary-mark article)))
10700
10701 (defun gnus-summary-remove-process-mark (&rest articles)
10702   "Remove the process mark from ARTICLES and update the summary line."
10703   (dolist (article articles)
10704     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10705     (when (gnus-summary-goto-subject article)
10706       (gnus-summary-show-thread)
10707       (gnus-summary-goto-subject article)
10708       (gnus-summary-update-secondary-mark article)))
10709   t)
10710
10711 (defun gnus-summary-set-saved-mark (article)
10712   "Set the process mark on ARTICLE and update the summary line."
10713   (push article gnus-newsgroup-saved)
10714   (when (gnus-summary-goto-subject article)
10715     (gnus-summary-update-secondary-mark article)))
10716
10717 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10718   "Mark N articles as read forwards.
10719 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10720 The difference between N and the actual number of articles marked is
10721 returned.
10722 If NO-EXPIRE, auto-expiry will be inhibited."
10723   (interactive "p")
10724   (gnus-summary-show-thread)
10725   (let ((backward (< n 0))
10726         (gnus-summary-goto-unread
10727          (and gnus-summary-goto-unread
10728               (not (eq gnus-summary-goto-unread 'never))
10729               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10730                                     gnus-ticked-mark gnus-dormant-mark)))))
10731         (n (abs n))
10732         (mark (or mark gnus-del-mark)))
10733     (while (and (> n 0)
10734                 (gnus-summary-mark-article nil mark no-expire)
10735                 (zerop (gnus-summary-next-subject
10736                         (if backward -1 1)
10737                         (and gnus-summary-goto-unread
10738                              (not (eq gnus-summary-goto-unread 'never)))
10739                         t)))
10740       (setq n (1- n)))
10741     (when (/= 0 n)
10742       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10743     (gnus-summary-recenter)
10744     (gnus-summary-position-point)
10745     (gnus-set-mode-line 'summary)
10746     n))
10747
10748 (defun gnus-summary-mark-article-as-read (mark)
10749   "Mark the current article quickly as read with MARK."
10750   (let ((article (gnus-summary-article-number)))
10751     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10752     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10753     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10754     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10755     (push (cons article mark) gnus-newsgroup-reads)
10756     ;; Possibly remove from cache, if that is used.
10757     (when gnus-use-cache
10758       (gnus-cache-enter-remove-article article))
10759     ;; Allow the backend to change the mark.
10760     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10761     ;; Check for auto-expiry.
10762     (when (and gnus-newsgroup-auto-expire
10763                (memq mark gnus-auto-expirable-marks))
10764       (setq mark gnus-expirable-mark)
10765       ;; Let the backend know about the mark change.
10766       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10767       (push article gnus-newsgroup-expirable))
10768     ;; Set the mark in the buffer.
10769     (gnus-summary-update-mark mark 'unread)
10770     t))
10771
10772 (defun gnus-summary-mark-article-as-unread (mark)
10773   "Mark the current article quickly as unread with MARK."
10774   (let* ((article (gnus-summary-article-number))
10775          (old-mark (gnus-summary-article-mark article)))
10776     ;; Allow the backend to change the mark.
10777     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10778     (if (eq mark old-mark)
10779         t
10780       (if (<= article 0)
10781           (progn
10782             (gnus-error 1 "Can't mark negative article numbers")
10783             nil)
10784         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10785         (setq gnus-newsgroup-spam-marked
10786               (delq article gnus-newsgroup-spam-marked))
10787         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10788         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10789         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10790         (cond ((= mark gnus-ticked-mark)
10791                (setq gnus-newsgroup-marked
10792                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10793                                               article)))
10794               ((= mark gnus-spam-mark)
10795                (setq gnus-newsgroup-spam-marked
10796                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10797                                               article)))
10798               ((= mark gnus-dormant-mark)
10799                (setq gnus-newsgroup-dormant
10800                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10801                                               article)))
10802               (t
10803                (setq gnus-newsgroup-unreads
10804                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10805                                               article))))
10806         (gnus-alist-pull article gnus-newsgroup-reads)
10807
10808         ;; See whether the article is to be put in the cache.
10809         (and gnus-use-cache
10810              (vectorp (gnus-summary-article-header article))
10811              (save-excursion
10812                (gnus-cache-possibly-enter-article
10813                 gnus-newsgroup-name article
10814                 (= mark gnus-ticked-mark)
10815                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10816
10817         ;; Fix the mark.
10818         (gnus-summary-update-mark mark 'unread)
10819         t))))
10820
10821 (defun gnus-summary-mark-article (&optional article mark no-expire)
10822   "Mark ARTICLE with MARK.  MARK can be any character.
10823 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10824 `??' (dormant) and `?E' (expirable).
10825 If MARK is nil, then the default character `?r' is used.
10826 If ARTICLE is nil, then the article on the current line will be
10827 marked.
10828 If NO-EXPIRE, auto-expiry will be inhibited."
10829   ;; The mark might be a string.
10830   (when (stringp mark)
10831     (setq mark (aref mark 0)))
10832   ;; If no mark is given, then we check auto-expiring.
10833   (when (null mark)
10834     (setq mark gnus-del-mark))
10835   (when (and (not no-expire)
10836              gnus-newsgroup-auto-expire
10837              (memq mark gnus-auto-expirable-marks))
10838     (setq mark gnus-expirable-mark))
10839   (let ((article (or article (gnus-summary-article-number)))
10840         (old-mark (gnus-summary-article-mark article)))
10841     ;; Allow the backend to change the mark.
10842     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10843     (if (eq mark old-mark)
10844         t
10845       (unless article
10846         (error "No article on current line"))
10847       (if (not (if (or (= mark gnus-unread-mark)
10848                        (= mark gnus-ticked-mark)
10849                        (= mark gnus-spam-mark)
10850                        (= mark gnus-dormant-mark))
10851                    (gnus-mark-article-as-unread article mark)
10852                  (gnus-mark-article-as-read article mark)))
10853           t
10854         ;; See whether the article is to be put in the cache.
10855         (and gnus-use-cache
10856              (not (= mark gnus-canceled-mark))
10857              (vectorp (gnus-summary-article-header article))
10858              (save-excursion
10859                (gnus-cache-possibly-enter-article
10860                 gnus-newsgroup-name article
10861                 (= mark gnus-ticked-mark)
10862                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10863
10864         (when (gnus-summary-goto-subject article nil t)
10865           (let ((inhibit-read-only t))
10866             (gnus-summary-show-thread)
10867             ;; Fix the mark.
10868             (gnus-summary-update-mark mark 'unread)
10869             t))))))
10870
10871 (defun gnus-summary-update-secondary-mark (article)
10872   "Update the secondary (read, process, cache) mark."
10873   (gnus-summary-update-mark
10874    (cond ((memq article gnus-newsgroup-processable)
10875           gnus-process-mark)
10876          ((memq article gnus-newsgroup-cached)
10877           gnus-cached-mark)
10878          ((memq article gnus-newsgroup-replied)
10879           gnus-replied-mark)
10880          ((memq article gnus-newsgroup-forwarded)
10881           gnus-forwarded-mark)
10882          ((memq article gnus-newsgroup-saved)
10883           gnus-saved-mark)
10884          ((memq article gnus-newsgroup-recent)
10885           gnus-recent-mark)
10886          ((memq article gnus-newsgroup-unseen)
10887           gnus-unseen-mark)
10888          (t gnus-no-mark))
10889    'replied)
10890   (when (gnus-visual-p 'summary-highlight 'highlight)
10891     (gnus-summary-highlight-line)
10892     (gnus-run-hooks 'gnus-summary-update-hook))
10893   t)
10894
10895 (defun gnus-summary-update-download-mark (article)
10896   "Update the download mark."
10897   (gnus-summary-update-mark
10898    (cond ((memq article gnus-newsgroup-undownloaded)
10899           gnus-undownloaded-mark)
10900          (gnus-newsgroup-agentized
10901           gnus-downloaded-mark)
10902          (t
10903           gnus-no-mark))
10904    'download)
10905   (gnus-summary-update-line t)
10906   t)
10907
10908 (defun gnus-summary-update-mark (mark type)
10909   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10910         (inhibit-read-only t))
10911     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10912     (when forward
10913       (when (looking-at "\r")
10914         (incf forward))
10915       (when (<= (+ forward (point)) (point-max))
10916         ;; Go to the right position on the line.
10917         (goto-char (+ forward (point)))
10918         ;; Replace the old mark with the new mark.
10919         (let ((to-insert
10920                (mm-subst-char-in-string
10921                 (char-after) mark
10922                 (buffer-substring (point) (1+ (point))))))
10923           (delete-region (point) (1+ (point)))
10924           (insert to-insert))
10925         ;; Optionally update the marks by some user rule.
10926         (when (eq type 'unread)
10927           (gnus-data-set-mark
10928            (gnus-data-find (gnus-summary-article-number)) mark)
10929           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10930
10931 (defun gnus-mark-article-as-read (article &optional mark)
10932   "Enter ARTICLE in the pertinent lists and remove it from others."
10933   ;; Make the article expirable.
10934   (let ((mark (or mark gnus-del-mark)))
10935     (setq gnus-newsgroup-expirable
10936           (if (= mark gnus-expirable-mark)
10937               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10938             (delq article gnus-newsgroup-expirable)))
10939     ;; Remove from unread and marked lists.
10940     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10941     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10942     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10943     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10944     (push (cons article mark) gnus-newsgroup-reads)
10945     ;; Possibly remove from cache, if that is used.
10946     (when gnus-use-cache
10947       (gnus-cache-enter-remove-article article))
10948     t))
10949
10950 (defun gnus-mark-article-as-unread (article &optional mark)
10951   "Enter ARTICLE in the pertinent lists and remove it from others."
10952   (let ((mark (or mark gnus-ticked-mark)))
10953     (if (<= article 0)
10954         (progn
10955           (gnus-error 1 "Can't mark negative article numbers")
10956           nil)
10957       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10958             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10959             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10960             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10961             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10962
10963       ;; Unsuppress duplicates?
10964       (when gnus-suppress-duplicates
10965         (gnus-dup-unsuppress-article article))
10966
10967       (cond ((= mark gnus-ticked-mark)
10968              (setq gnus-newsgroup-marked
10969                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10970             ((= mark gnus-spam-mark)
10971              (setq gnus-newsgroup-spam-marked
10972                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10973                                             article)))
10974             ((= mark gnus-dormant-mark)
10975              (setq gnus-newsgroup-dormant
10976                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10977             (t
10978              (setq gnus-newsgroup-unreads
10979                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10980       (gnus-alist-pull article gnus-newsgroup-reads)
10981       t)))
10982
10983 (defun gnus-summary-tick-article-forward (n)
10984   "Tick N articles forwards.
10985 If N is negative, tick backwards instead.
10986 The difference between N and the number of articles ticked is returned."
10987   (interactive "p")
10988   (gnus-summary-mark-forward n gnus-ticked-mark))
10989
10990 (defun gnus-summary-tick-article-backward (n)
10991   "Tick N articles backwards.
10992 The difference between N and the number of articles ticked is returned."
10993   (interactive "p")
10994   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10995
10996 (defun gnus-summary-tick-article (&optional article clear-mark)
10997   "Mark current article as unread.
10998 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10999 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11000   (interactive)
11001   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11002                                        gnus-ticked-mark)))
11003
11004 (defun gnus-summary-mark-as-read-forward (n)
11005   "Mark N articles as read forwards.
11006 If N is negative, mark backwards instead.
11007 The difference between N and the actual number of articles marked is
11008 returned."
11009   (interactive "p")
11010   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11011
11012 (defun gnus-summary-mark-as-read-backward (n)
11013   "Mark the N articles as read backwards.
11014 The difference between N and the actual number of articles marked is
11015 returned."
11016   (interactive "p")
11017   (gnus-summary-mark-forward
11018    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11019
11020 (defun gnus-summary-mark-as-read (&optional article mark)
11021   "Mark current article as read.
11022 ARTICLE specifies the article to be marked as read.
11023 MARK specifies a string to be inserted at the beginning of the line."
11024   (gnus-summary-mark-article article mark))
11025
11026 (defun gnus-summary-clear-mark-forward (n)
11027   "Clear marks from N articles forward.
11028 If N is negative, clear backward instead.
11029 The difference between N and the number of marks cleared is returned."
11030   (interactive "p")
11031   (gnus-summary-mark-forward n gnus-unread-mark))
11032
11033 (defun gnus-summary-clear-mark-backward (n)
11034   "Clear marks from N articles backward.
11035 The difference between N and the number of marks cleared is returned."
11036   (interactive "p")
11037   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11038
11039 (defun gnus-summary-mark-unread-as-read ()
11040   "Intended to be used by `gnus-mark-article-hook'."
11041   (when (memq gnus-current-article gnus-newsgroup-unreads)
11042     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11043
11044 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11045   "Intended to be used by `gnus-mark-article-hook'."
11046   (let ((mark (gnus-summary-article-mark)))
11047     (when (or (gnus-unread-mark-p mark)
11048               (gnus-read-mark-p mark))
11049       (gnus-summary-mark-article gnus-current-article
11050                                  (or new-mark gnus-read-mark)))))
11051
11052 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11053   "Intended to be used by `gnus-mark-article-hook'."
11054   (let ((mark (gnus-summary-article-mark)))
11055     (when (or (gnus-unread-mark-p mark)
11056               (gnus-read-mark-p mark))
11057       (gnus-summary-mark-article (gnus-summary-article-number)
11058                                  (or new-mark gnus-read-mark)))))
11059
11060 (defun gnus-summary-mark-unread-as-ticked ()
11061   "Intended to be used by `gnus-mark-article-hook'."
11062   (when (memq gnus-current-article gnus-newsgroup-unreads)
11063     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11064
11065 (defun gnus-summary-mark-region-as-read (point mark all)
11066   "Mark all unread articles between point and mark as read.
11067 If given a prefix, mark all articles between point and mark as read,
11068 even ticked and dormant ones."
11069   (interactive "r\nP")
11070   (save-excursion
11071     (let (article)
11072       (goto-char point)
11073       (beginning-of-line)
11074       (while (and
11075               (< (point) mark)
11076               (progn
11077                 (when (or all
11078                           (memq (setq article (gnus-summary-article-number))
11079                                 gnus-newsgroup-unreads))
11080                   (gnus-summary-mark-article article gnus-del-mark))
11081                 t)
11082               (gnus-summary-find-next))))))
11083
11084 (defun gnus-summary-mark-below (score mark)
11085   "Mark articles with score less than SCORE with MARK."
11086   (interactive "P\ncMark: ")
11087   (setq score (if score
11088                   (prefix-numeric-value score)
11089                 (or gnus-summary-default-score 0)))
11090   (with-current-buffer gnus-summary-buffer
11091     (goto-char (point-min))
11092     (while
11093         (progn
11094           (and (< (gnus-summary-article-score) score)
11095                (gnus-summary-mark-article nil mark))
11096           (gnus-summary-find-next)))))
11097
11098 (defun gnus-summary-kill-below (&optional score)
11099   "Mark articles with score below SCORE as read."
11100   (interactive "P")
11101   (gnus-summary-mark-below score gnus-killed-mark))
11102
11103 (defun gnus-summary-clear-above (&optional score)
11104   "Clear all marks from articles with score above SCORE."
11105   (interactive "P")
11106   (gnus-summary-mark-above score gnus-unread-mark))
11107
11108 (defun gnus-summary-tick-above (&optional score)
11109   "Tick all articles with score above SCORE."
11110   (interactive "P")
11111   (gnus-summary-mark-above score gnus-ticked-mark))
11112
11113 (defun gnus-summary-mark-above (score mark)
11114   "Mark articles with score over SCORE with MARK."
11115   (interactive "P\ncMark: ")
11116   (setq score (if score
11117                   (prefix-numeric-value score)
11118                 (or gnus-summary-default-score 0)))
11119   (with-current-buffer gnus-summary-buffer
11120     (goto-char (point-min))
11121     (while (and (progn
11122                   (when (> (gnus-summary-article-score) score)
11123                     (gnus-summary-mark-article nil mark))
11124                   t)
11125                 (gnus-summary-find-next)))))
11126
11127 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11128 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11129 (defun gnus-summary-limit-include-expunged (&optional no-error)
11130   "Display all the hidden articles that were expunged for low scores."
11131   (interactive)
11132   (let ((inhibit-read-only t))
11133     (let ((scored gnus-newsgroup-scored)
11134           headers h)
11135       (while scored
11136         (unless (gnus-summary-article-header (caar scored))
11137           (and (setq h (gnus-number-to-header (caar scored)))
11138                (< (cdar scored) gnus-summary-expunge-below)
11139                (push h headers)))
11140         (setq scored (cdr scored)))
11141       (if (not headers)
11142           (when (not no-error)
11143             (error "No expunged articles hidden"))
11144         (goto-char (point-min))
11145         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11146         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11147         (dolist (x headers)
11148           (push (mail-header-number x) gnus-newsgroup-limit))
11149         (gnus-summary-prepare-unthreaded (nreverse headers))
11150         (goto-char (point-min))
11151         (gnus-summary-position-point)
11152         t))))
11153
11154 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11155   "Mark all unread articles in this newsgroup as read.
11156 If prefix argument ALL is non-nil, ticked and dormant articles will
11157 also be marked as read.
11158 If QUIETLY is non-nil, no questions will be asked.
11159
11160 If TO-HERE is non-nil, it should be a point in the buffer.  All
11161 articles before (after, if REVERSE is set) this point will be marked
11162 as read.
11163
11164 Note that this function will only catch up the unread article
11165 in the current summary buffer limitation.
11166
11167 The number of articles marked as read is returned."
11168   (interactive "P")
11169   (prog1
11170       (save-excursion
11171         (when (or quietly
11172                   (not gnus-interactive-catchup) ;Without confirmation?
11173                   gnus-expert-user
11174                   (gnus-y-or-n-p
11175                    (if all
11176                        "Mark absolutely all articles as read? "
11177                      "Mark all unread articles as read? ")))
11178           (if (and not-mark
11179                    (not gnus-newsgroup-adaptive)
11180                    (not gnus-newsgroup-auto-expire)
11181                    (not gnus-suppress-duplicates)
11182                    (or (not gnus-use-cache)
11183                        (eq gnus-use-cache 'passive)))
11184               (progn
11185                 (when all
11186                   (setq gnus-newsgroup-marked nil
11187                         gnus-newsgroup-spam-marked nil
11188                         gnus-newsgroup-dormant nil))
11189                 (setq gnus-newsgroup-unreads
11190                       (gnus-sorted-nunion
11191                        (gnus-sorted-intersection gnus-newsgroup-unreads
11192                                                  gnus-newsgroup-downloadable)
11193                        (gnus-sorted-difference gnus-newsgroup-unfetched
11194                                                gnus-newsgroup-cached))))
11195             ;; We actually mark all articles as canceled, which we
11196             ;; have to do when using auto-expiry or adaptive scoring.
11197             (gnus-summary-show-all-threads)
11198             (if (and to-here reverse)
11199                 (progn
11200                   (goto-char to-here)
11201                   (gnus-summary-mark-current-read-and-unread-as-read
11202                    gnus-catchup-mark)
11203                   (while (gnus-summary-find-next (not all))
11204                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11205               (when (gnus-summary-first-subject (not all))
11206                 (while (and
11207                         (if to-here (< (point) to-here) t)
11208                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11209                         (gnus-summary-find-next (not all))))))
11210             (gnus-set-mode-line 'summary))
11211           t))
11212     (gnus-summary-position-point)))
11213
11214 (defun gnus-summary-catchup-to-here (&optional all)
11215   "Mark all unticked articles before the current one as read.
11216 If ALL is non-nil, also mark ticked and dormant articles as read."
11217   (interactive "P")
11218   (save-excursion
11219     (gnus-save-hidden-threads
11220       (let ((beg (point)))
11221         ;; We check that there are unread articles.
11222         (when (or all (gnus-summary-find-prev))
11223           (gnus-summary-catchup all t beg)))))
11224   (gnus-summary-position-point))
11225
11226 (defun gnus-summary-catchup-from-here (&optional all)
11227   "Mark all unticked articles after (and including) the current one as read.
11228 If ALL is non-nil, also mark ticked and dormant articles as read."
11229   (interactive "P")
11230   (save-excursion
11231     (gnus-save-hidden-threads
11232       (let ((beg (point)))
11233         ;; We check that there are unread articles.
11234         (when (or all (gnus-summary-find-next))
11235           (gnus-summary-catchup all t beg nil t)))))
11236   (gnus-summary-position-point))
11237
11238 (defun gnus-summary-catchup-all (&optional quietly)
11239   "Mark all articles in this newsgroup as read.
11240 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11241 instead, which marks only unread articles as read."
11242   (interactive "P")
11243   (gnus-summary-catchup t quietly))
11244
11245 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11246   "Mark all unread articles in this group as read, then exit.
11247 If prefix argument ALL is non-nil, all articles are marked as read.
11248 If QUIETLY is non-nil, no questions will be asked."
11249   (interactive "P")
11250   (when (gnus-summary-catchup all quietly nil 'fast)
11251     ;; Select next newsgroup or exit.
11252     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11253              (eq gnus-auto-select-next 'quietly))
11254         (gnus-summary-next-group nil)
11255       (gnus-summary-exit))))
11256
11257 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11258   "Mark all articles in this newsgroup as read, and then exit.
11259 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11260 instead, which marks only unread articles as read."
11261   (interactive "P")
11262   (gnus-summary-catchup-and-exit t quietly))
11263
11264 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11265   "Mark all articles in this group as read and select the next group.
11266 If given a prefix, mark all articles, unread as well as ticked, as
11267 read."
11268   (interactive "P")
11269   (save-excursion
11270     (gnus-summary-catchup all))
11271   (gnus-summary-next-group))
11272
11273 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11274   "Mark all articles in this group as read and select the previous group.
11275 If given a prefix, mark all articles, unread as well as ticked, as
11276 read."
11277   (interactive "P")
11278   (save-excursion
11279     (gnus-summary-catchup all))
11280   (gnus-summary-next-group nil nil t))
11281
11282 ;;;
11283 ;;; with article
11284 ;;;
11285
11286 (defmacro gnus-with-article (article &rest forms)
11287   "Select ARTICLE and perform FORMS in the original article buffer.
11288 Then replace the article with the result."
11289   `(progn
11290      ;; We don't want the article to be marked as read.
11291      (let (gnus-mark-article-hook)
11292        (gnus-summary-select-article t t nil ,article))
11293      (set-buffer gnus-original-article-buffer)
11294      ,@forms
11295      (if (not (gnus-check-backend-function
11296                'request-replace-article (car gnus-article-current)))
11297          (gnus-message 5 "Read-only group; not replacing")
11298        (unless (gnus-request-replace-article
11299                 ,article (car gnus-article-current)
11300                 (current-buffer) t)
11301          (error "Couldn't replace article")))
11302      ;; The cache and backlog have to be flushed somewhat.
11303      (when gnus-keep-backlog
11304        (gnus-backlog-remove-article
11305         (car gnus-article-current) (cdr gnus-article-current)))
11306      (when gnus-use-cache
11307        (gnus-cache-update-article
11308         (car gnus-article-current) (cdr gnus-article-current)))))
11309
11310 (put 'gnus-with-article 'lisp-indent-function 1)
11311 (put 'gnus-with-article 'edebug-form-spec '(form body))
11312
11313 ;; Thread-based commands.
11314
11315 (defun gnus-summary-articles-in-thread (&optional article)
11316   "Return a list of all articles in the current thread.
11317 If ARTICLE is non-nil, return all articles in the thread that starts
11318 with that article."
11319   (let* ((article (or article (gnus-summary-article-number)))
11320          (data (gnus-data-find-list article))
11321          (top-level (gnus-data-level (car data)))
11322          (top-subject
11323           (cond ((null gnus-thread-operation-ignore-subject)
11324                  (gnus-simplify-subject-re
11325                   (mail-header-subject (gnus-data-header (car data)))))
11326                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11327                  (gnus-simplify-subject-fuzzy
11328                   (mail-header-subject (gnus-data-header (car data)))))
11329                 (t nil)))
11330          (end-point (save-excursion
11331                       (goto-char (gnus-data-pos (car data)))
11332                       (if (gnus-summary-go-to-next-thread)
11333                           (point) (point-max))))
11334          articles)
11335     (while (and data
11336                 (< (gnus-data-pos (car data)) end-point))
11337       (when (or (not top-subject)
11338                 (string= top-subject
11339                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11340                              (gnus-simplify-subject-fuzzy
11341                               (mail-header-subject
11342                                (gnus-data-header (car data))))
11343                            (gnus-simplify-subject-re
11344                             (mail-header-subject
11345                              (gnus-data-header (car data)))))))
11346         (push (gnus-data-number (car data)) articles))
11347       (unless (and (setq data (cdr data))
11348                    (> (gnus-data-level (car data)) top-level))
11349         (setq data nil)))
11350     ;; Return the list of articles.
11351     (nreverse articles)))
11352
11353 (defun gnus-summary-rethread-current ()
11354   "Rethread the thread the current article is part of."
11355   (interactive)
11356   (let* ((gnus-show-threads t)
11357          (article (gnus-summary-article-number))
11358          (id (mail-header-id (gnus-summary-article-header)))
11359          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11360     (unless id
11361       (error "No article on the current line"))
11362     (gnus-rebuild-thread id)
11363     (gnus-summary-goto-subject article)))
11364
11365 (defun gnus-summary-reparent-thread ()
11366   "Make the current article child of the marked (or previous) article.
11367
11368 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11369 is non-nil or the Subject: of both articles are the same."
11370   (interactive)
11371   (unless (not (gnus-group-read-only-p))
11372     (error "The current newsgroup does not support article editing"))
11373   (unless (<= (length gnus-newsgroup-processable) 1)
11374     (error "No more than one article may be marked"))
11375   (let ((child (gnus-summary-article-number))
11376         ;; First grab the marked article, otherwise one line up.
11377         (parent (if (not (null gnus-newsgroup-processable))
11378                     (car gnus-newsgroup-processable)
11379                   (save-excursion
11380                     (if (eq (forward-line -1) 0)
11381                         (gnus-summary-article-number)
11382                       (error "Beginning of summary buffer"))))))
11383     (gnus-summary-reparent-children parent (list child))))
11384
11385 (defun gnus-summary-reparent-children (parent children)
11386   "Make PARENT the parent of CHILDREN.
11387 When called interactively, PARENT is the current article and CHILDREN
11388 are the process-marked articles."
11389   (interactive
11390    (list (gnus-summary-article-number)
11391          (gnus-summary-work-articles nil)))
11392   (dolist (child children)
11393     (save-window-excursion
11394       (let ((gnus-article-buffer " *reparent*"))
11395         (unless (not (eq parent child))
11396           (error "An article may not be self-referential"))
11397         (let ((message-id (mail-header-id
11398                            (gnus-summary-article-header parent))))
11399           (unless (and message-id (not (equal message-id "")))
11400             (error "No message-id in desired parent"))
11401           (gnus-with-article child
11402             (save-restriction
11403               (goto-char (point-min))
11404               (message-narrow-to-head)
11405               (if (re-search-forward "^References: " nil t)
11406                   (progn
11407                     (re-search-forward "^[^ \t]" nil t)
11408                     (forward-line -1)
11409                     (end-of-line)
11410                     (insert " " message-id))
11411                 (insert "References: " message-id "\n"))))
11412           (set-buffer gnus-summary-buffer)
11413           (gnus-summary-unmark-all-processable)
11414           (gnus-summary-update-article child)
11415           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11416             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11417           (gnus-summary-rethread-current)
11418           (gnus-message 3 "Article %d is now the child of article %d"
11419                         child parent))))))
11420
11421 (defun gnus-summary-toggle-threads (&optional arg)
11422   "Toggle showing conversation threads.
11423 If ARG is positive number, turn showing conversation threads on."
11424   (interactive "P")
11425   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11426     (setq gnus-show-threads
11427           (if (null arg) (not gnus-show-threads)
11428             (> (prefix-numeric-value arg) 0)))
11429     (gnus-summary-prepare)
11430     (gnus-summary-goto-subject current)
11431     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11432     (gnus-summary-position-point)))
11433
11434 (eval-and-compile
11435   (if (fboundp 'remove-overlays)
11436       (defalias 'gnus-remove-overlays 'remove-overlays)
11437     (defun gnus-remove-overlays (beg end name val)
11438       "Clear BEG and END of overlays whose property NAME has value VAL.
11439 For compatibility with XEmacs."
11440       (dolist (ov (gnus-overlays-in beg end))
11441         (when (eq (gnus-overlay-get ov name) val)
11442           (gnus-delete-overlay ov))))))
11443
11444 (defun gnus-summary-show-all-threads ()
11445   "Show all threads."
11446   (interactive)
11447   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11448   (gnus-summary-position-point))
11449
11450 (defsubst gnus-summary--inv (p)
11451   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11452
11453 (defun gnus-summary-show-thread ()
11454   "Show thread subtrees.
11455 Returns nil if no thread was there to be shown."
11456   (interactive)
11457   (let* ((orig (point))
11458          (end (point-at-eol))
11459          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11460          ;; Leave point at bol
11461          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11462          (eoi (when end
11463                 (if (fboundp 'next-single-char-property-change)
11464                     (or (next-single-char-property-change end 'invisible)
11465                         (point-max))
11466                   (while (progn
11467                            (end-of-line 2)
11468                            (and (not (eobp))
11469                                 (eq (get-char-property (point) 'invisible)
11470                                     'gnus-sum))))
11471                   (point)))))
11472     (when eoi
11473       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11474       (goto-char orig)
11475       (gnus-summary-position-point)
11476       eoi)))
11477
11478 (defun gnus-summary-maybe-hide-threads ()
11479   "If requested, hide the threads that should be hidden."
11480   (when (and gnus-show-threads
11481              gnus-thread-hide-subtree)
11482     (gnus-summary-hide-all-threads
11483      (if (or (consp gnus-thread-hide-subtree)
11484              (functionp gnus-thread-hide-subtree))
11485          (gnus-make-predicate gnus-thread-hide-subtree)
11486        nil))))
11487
11488 ;;; Hiding predicates.
11489
11490 (defun gnus-article-unread-p (header)
11491   (memq (mail-header-number header) gnus-newsgroup-unreads))
11492
11493 (defun gnus-article-unseen-p (header)
11494   (memq (mail-header-number header) gnus-newsgroup-unseen))
11495
11496 (defun gnus-map-articles (predicate articles)
11497   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11498   (apply 'gnus-or (mapcar predicate
11499                           (mapcar (lambda (number)
11500                                     (gnus-summary-article-header number))
11501                                   articles))))
11502
11503 (defun gnus-summary-hide-all-threads (&optional predicate)
11504   "Hide all thread subtrees.
11505 If PREDICATE is supplied, threads that satisfy this predicate
11506 will not be hidden."
11507   (interactive)
11508   (save-excursion
11509     (goto-char (point-min))
11510     (let ((end nil))
11511       (while (not end)
11512         (when (or (not predicate)
11513                   (gnus-map-articles
11514                    predicate (gnus-summary-article-children)))
11515             (gnus-summary-hide-thread))
11516         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11517   (gnus-summary-position-point))
11518
11519 (defun gnus-summary-hide-thread ()
11520   "Hide thread subtrees.
11521 If PREDICATE is supplied, threads that satisfy this predicate
11522 will not be hidden.
11523 Returns nil if no threads were there to be hidden."
11524   (interactive)
11525   (let ((start (point))
11526         (starteol (line-end-position))
11527         (article (gnus-summary-article-number)))
11528     (goto-char start)
11529     ;; Go forward until either the buffer ends or the subthread ends.
11530     (when (and (not (eobp))
11531                (or (zerop (gnus-summary-next-thread 1 t))
11532                    (goto-char (point-max))))
11533       (if (and (> (point) start)
11534                ;; FIXME: this should actually search for a non-invisible \n.
11535                (search-backward "\n" start t))
11536           (progn
11537             (when (> (point) starteol)
11538               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11539               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11540                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11541                 (gnus-overlay-put ol 'evaporate t)))
11542             (gnus-summary-goto-subject article))
11543         (goto-char start)
11544         nil))))
11545
11546 (defun gnus-summary-go-to-next-thread (&optional previous)
11547   "Go to the same level (or less) next thread.
11548 If PREVIOUS is non-nil, go to previous thread instead.
11549 Return the article number moved to, or nil if moving was impossible."
11550   (let ((level (gnus-summary-thread-level))
11551         (way (if previous -1 1))
11552         (beg (point)))
11553     (forward-line way)
11554     (while (and (not (eobp))
11555                 (< level (gnus-summary-thread-level)))
11556       (forward-line way))
11557     (if (eobp)
11558         (progn
11559           (goto-char beg)
11560           nil)
11561       (setq beg (point))
11562       (prog1
11563           (gnus-summary-article-number)
11564         (goto-char beg)))))
11565
11566 (defun gnus-summary-next-thread (n &optional silent)
11567   "Go to the same level next N'th thread.
11568 If N is negative, search backward instead.
11569 Returns the difference between N and the number of skips actually
11570 done.
11571
11572 If SILENT, don't output messages."
11573   (interactive "p")
11574   (let ((backward (< n 0))
11575         (n (abs n)))
11576     (while (and (> n 0)
11577                 (gnus-summary-go-to-next-thread backward))
11578       (decf n))
11579     (unless silent
11580       (gnus-summary-position-point))
11581     (when (and (not silent) (/= 0 n))
11582       (gnus-message 7 "No more threads"))
11583     n))
11584
11585 (defun gnus-summary-prev-thread (n)
11586   "Go to the same level previous N'th thread.
11587 Returns the difference between N and the number of skips actually
11588 done."
11589   (interactive "p")
11590   (gnus-summary-next-thread (- n)))
11591
11592 (defun gnus-summary-go-down-thread ()
11593   "Go down one level in the current thread."
11594   (let ((children (gnus-summary-article-children)))
11595     (when children
11596       (gnus-summary-goto-subject (car children)))))
11597
11598 (defun gnus-summary-go-up-thread ()
11599   "Go up one level in the current thread."
11600   (let ((parent (gnus-summary-article-parent)))
11601     (when parent
11602       (gnus-summary-goto-subject parent))))
11603
11604 (defun gnus-summary-down-thread (n)
11605   "Go down thread N steps.
11606 If N is negative, go up instead.
11607 Returns the difference between N and how many steps down that were
11608 taken."
11609   (interactive "p")
11610   (let ((up (< n 0))
11611         (n (abs n)))
11612     (while (and (> n 0)
11613                 (if up (gnus-summary-go-up-thread)
11614                   (gnus-summary-go-down-thread)))
11615       (setq n (1- n)))
11616     (gnus-summary-position-point)
11617     (when (/= 0 n)
11618       (gnus-message 7 "Can't go further"))
11619     n))
11620
11621 (defun gnus-summary-up-thread (n)
11622   "Go up thread N steps.
11623 If N is negative, go down instead.
11624 Returns the difference between N and how many steps down that were
11625 taken."
11626   (interactive "p")
11627   (gnus-summary-down-thread (- n)))
11628
11629 (defun gnus-summary-top-thread ()
11630   "Go to the top of the thread."
11631   (interactive)
11632   (while (gnus-summary-go-up-thread))
11633   (gnus-summary-article-number))
11634
11635 (defun gnus-summary-expire-thread ()
11636   "Mark articles under current thread as expired."
11637   (interactive)
11638   (gnus-summary-kill-thread 0))
11639
11640 (defun gnus-summary-kill-thread (&optional unmark)
11641   "Mark articles under current thread as read.
11642 If the prefix argument is positive, remove any kinds of marks.
11643 If the prefix argument is zero, mark thread as expired.
11644 If the prefix argument is negative, tick articles instead."
11645   (interactive "P")
11646   (when unmark
11647     (setq unmark (prefix-numeric-value unmark)))
11648   (let ((articles (gnus-summary-articles-in-thread))
11649         (hide (or (null unmark) (= unmark 0))))
11650     (save-excursion
11651       ;; Expand the thread.
11652       (gnus-summary-show-thread)
11653       ;; Mark all the articles.
11654       (while articles
11655         (gnus-summary-goto-subject (car articles))
11656         (cond ((null unmark)
11657                (gnus-summary-mark-article-as-read gnus-killed-mark))
11658               ((> unmark 0)
11659                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11660               ((= unmark 0)
11661                (gnus-summary-mark-article nil gnus-expirable-mark))
11662               (t
11663                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11664         (setq articles (cdr articles))))
11665     ;; Hide killed subtrees when hide is true.
11666     (and hide
11667          gnus-thread-hide-killed
11668          (gnus-summary-hide-thread))
11669     ;; If hide is t, go to next unread subject.
11670     (when hide
11671       ;; Go to next unread subject.
11672       (gnus-summary-next-subject 1 t)))
11673   (gnus-set-mode-line 'summary))
11674
11675 ;; Summary sorting commands
11676
11677 (defun gnus-summary-sort-by-number (&optional reverse)
11678   "Sort the summary buffer by article number.
11679 Argument REVERSE means reverse order."
11680   (interactive "P")
11681   (gnus-summary-sort 'number reverse))
11682
11683 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11684   "Sort the summary buffer by most recent article number.
11685 Argument REVERSE means reverse order."
11686   (interactive "P")
11687   (gnus-summary-sort 'most-recent-number reverse))
11688
11689 (defun gnus-summary-sort-by-random (&optional reverse)
11690   "Randomize the order in the summary buffer.
11691 Argument REVERSE means to randomize in reverse order."
11692   (interactive "P")
11693   (gnus-summary-sort 'random reverse))
11694
11695 (defun gnus-summary-sort-by-author (&optional reverse)
11696   "Sort the summary buffer by author name alphabetically.
11697 If `case-fold-search' is non-nil, case of letters is ignored.
11698 Argument REVERSE means reverse order."
11699   (interactive "P")
11700   (gnus-summary-sort 'author reverse))
11701
11702 (defun gnus-summary-sort-by-recipient (&optional reverse)
11703   "Sort the summary buffer by recipient name alphabetically.
11704 If `case-fold-search' is non-nil, case of letters is ignored.
11705 Argument REVERSE means reverse order."
11706   (interactive "P")
11707   (gnus-summary-sort 'recipient reverse))
11708
11709 (defun gnus-summary-sort-by-subject (&optional reverse)
11710   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11711 If `case-fold-search' is non-nil, case of letters is ignored.
11712 Argument REVERSE means reverse order."
11713   (interactive "P")
11714   (gnus-summary-sort 'subject reverse))
11715
11716 (defun gnus-summary-sort-by-date (&optional reverse)
11717   "Sort the summary buffer by date.
11718 Argument REVERSE means reverse order."
11719   (interactive "P")
11720   (gnus-summary-sort 'date reverse))
11721
11722 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11723   "Sort the summary buffer by most recent date.
11724 Argument REVERSE means reverse order."
11725   (interactive "P")
11726   (gnus-summary-sort 'most-recent-date reverse))
11727
11728 (defun gnus-summary-sort-by-score (&optional reverse)
11729   "Sort the summary buffer by score.
11730 Argument REVERSE means reverse order."
11731   (interactive "P")
11732   (gnus-summary-sort 'score reverse))
11733
11734 (defun gnus-summary-sort-by-lines (&optional reverse)
11735   "Sort the summary buffer by the number of lines.
11736 Argument REVERSE means reverse order."
11737   (interactive "P")
11738   (gnus-summary-sort 'lines reverse))
11739
11740 (defun gnus-summary-sort-by-chars (&optional reverse)
11741   "Sort the summary buffer by article length.
11742 Argument REVERSE means reverse order."
11743   (interactive "P")
11744   (gnus-summary-sort 'chars reverse))
11745
11746 (defun gnus-summary-sort-by-original (&optional reverse)
11747   "Sort the summary buffer using the default sorting method.
11748 Argument REVERSE means reverse order."
11749   (interactive "P")
11750   (let* ((inhibit-read-only t)
11751          (gnus-summary-prepare-hook nil))
11752     ;; We do the sorting by regenerating the threads.
11753     (gnus-summary-prepare)
11754     ;; Hide subthreads if needed.
11755     (gnus-summary-maybe-hide-threads)))
11756
11757 (defun gnus-summary-sort (predicate reverse)
11758   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11759   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11760          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11761          (gnus-thread-sort-functions
11762           (if (not reverse)
11763               thread
11764             `(lambda (t1 t2)
11765                (,thread t2 t1))))
11766          (gnus-sort-gathered-threads-function
11767           gnus-thread-sort-functions)
11768          (gnus-article-sort-functions
11769           (if (not reverse)
11770               article
11771             `(lambda (t1 t2)
11772                (,article t2 t1))))
11773          (inhibit-read-only t)
11774          (gnus-summary-prepare-hook nil))
11775     ;; We do the sorting by regenerating the threads.
11776     (gnus-summary-prepare)
11777     ;; Hide subthreads if needed.
11778     (gnus-summary-maybe-hide-threads)))
11779
11780 ;; Summary saving commands.
11781
11782 (defun gnus-summary-save-article (&optional n not-saved)
11783   "Save the current article using the default saver function.
11784 If N is a positive number, save the N next articles.
11785 If N is a negative number, save the N previous articles.
11786 If N is nil and any articles have been marked with the process mark,
11787 save those articles instead.
11788 The variable `gnus-default-article-saver' specifies the saver function.
11789
11790 If the optional second argument NOT-SAVED is non-nil, articles saved
11791 will not be marked as saved."
11792   (interactive "P")
11793   (require 'gnus-art)
11794   (let* ((articles (gnus-summary-work-articles n))
11795          (save-buffer (save-excursion
11796                         (nnheader-set-temp-buffer " *Gnus Save*")))
11797          (num (length articles))
11798          ;; Whether to save decoded articles or raw articles.
11799          (decode (when gnus-article-save-coding-system
11800                    (get gnus-default-article-saver :decode)))
11801          ;; When saving many articles in a single file, use the other
11802          ;; function to save articles other than the first one.
11803          (saver2 (get gnus-default-article-saver :function))
11804          (gnus-prompt-before-saving (if saver2
11805                                         t
11806                                       gnus-prompt-before-saving))
11807          (gnus-default-article-saver gnus-default-article-saver)
11808          header file)
11809     (dolist (article articles)
11810       (setq header (gnus-summary-article-header article))
11811       (if (not (vectorp header))
11812           ;; This is a pseudo-article.
11813           (if (assq 'name header)
11814               (gnus-copy-file (cdr (assq 'name header)))
11815             (gnus-message 1 "Article %d is unsaveable" article))
11816         ;; This is a real article.
11817         (save-window-excursion
11818           (gnus-summary-select-article decode decode nil article)
11819           (gnus-summary-goto-subject article))
11820         (with-current-buffer save-buffer
11821           (erase-buffer)
11822           (insert-buffer-substring (if decode
11823                                        gnus-article-buffer
11824                                      gnus-original-article-buffer)))
11825         (setq file (gnus-article-save save-buffer file num))
11826         (gnus-summary-remove-process-mark article)
11827         (unless not-saved
11828           (gnus-summary-set-saved-mark article)))
11829       (when saver2
11830         (setq gnus-default-article-saver saver2
11831               saver2 nil)))
11832     (gnus-kill-buffer save-buffer)
11833     (gnus-summary-position-point)
11834     (gnus-set-mode-line 'summary)
11835     n))
11836
11837 (defun gnus-summary-pipe-output (&optional n sym)
11838   "Pipe the current article to a subprocess.
11839 If N is a positive number, pipe the N next articles.
11840 If N is a negative number, pipe the N previous articles.
11841 If N is nil and any articles have been marked with the process mark,
11842 pipe those articles instead.
11843 The default command to which articles are piped is specified by the
11844 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11845 will be prompted for the command.
11846
11847 The properties `:decode' and `:headers' that are put to the function
11848 symbol `gnus-summary-save-in-pipe' control whether this function
11849 decodes articles and what headers to keep (see the doc string for the
11850 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11851 is neither omitted nor the symbol `r', force including all headers
11852 regardless of the `:headers' property.  If it is the symbol `r',
11853 articles that are not decoded and include all headers will be piped
11854 no matter what the properties `:decode' and `:headers' are."
11855   (interactive (gnus-interactive "P\ny"))
11856   (require 'gnus-art)
11857   (let* ((articles (gnus-summary-work-articles n))
11858          (result-buffer "*Shell Command Output*")
11859          (all-headers (not (memq sym '(nil r))))
11860          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11861          (raw (eq sym 'r))
11862          (headers (get 'gnus-summary-save-in-pipe :headers))
11863          command result)
11864     (unless (numberp (car articles))
11865       (error "No article to pipe"))
11866     (setq command (gnus-read-shell-command
11867                    (concat "Shell command on "
11868                            (if (cdr articles)
11869                                (format "these %d articles" (length articles))
11870                              "this article")
11871                            ": ")
11872                    gnus-summary-pipe-output-default-command))
11873     (when (string-equal command "")
11874       (error "A command is required"))
11875     (when all-headers
11876       (put 'gnus-summary-save-in-pipe :headers nil))
11877     (unwind-protect
11878         (while articles
11879           (gnus-summary-goto-subject (pop articles))
11880           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11881           (when (and (get-buffer result-buffer)
11882                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11883             (setq result (concat result (with-current-buffer result-buffer
11884                                           (buffer-string))))))
11885       (put 'gnus-summary-save-in-pipe :headers headers))
11886     (unless (zerop (length result))
11887       (if (with-current-buffer (get-buffer-create result-buffer)
11888             (erase-buffer)
11889             (insert result)
11890             (prog1
11891                 (and (= (count-lines (point-min) (point)) 1)
11892                      (progn
11893                        (end-of-line 0)
11894                        (<= (current-column)
11895                            (window-width (minibuffer-window)))))
11896               (goto-char (point-min))))
11897           (message "%s" (substring result 0 -1))
11898         (message nil)
11899         (gnus-configure-windows 'pipe)))))
11900
11901 (defun gnus-summary-save-article-mail (&optional arg)
11902   "Append the current article to a Unix mail box file.
11903 If N is a positive number, save the N next articles.
11904 If N is a negative number, save the N previous articles.
11905 If N is nil and any articles have been marked with the process mark,
11906 save those articles instead."
11907   (interactive "P")
11908   (require 'gnus-art)
11909   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11910     (gnus-summary-save-article arg)))
11911
11912 (defun gnus-summary-save-article-rmail (&optional arg)
11913   "Append the current article to an rmail file.
11914 If N is a positive number, save the N next articles.
11915 If N is a negative number, save the N previous articles.
11916 If N is nil and any articles have been marked with the process mark,
11917 save those articles instead."
11918   (interactive "P")
11919   (require 'gnus-art)
11920   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11921     (gnus-summary-save-article arg)))
11922
11923 (defun gnus-summary-save-article-file (&optional arg)
11924   "Append the current article to a file.
11925 If N is a positive number, save the N next articles.
11926 If N is a negative number, save the N previous articles.
11927 If N is nil and any articles have been marked with the process mark,
11928 save those articles instead."
11929   (interactive "P")
11930   (require 'gnus-art)
11931   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11932     (gnus-summary-save-article arg)))
11933
11934 (defun gnus-summary-write-article-file (&optional arg)
11935   "Write the current article to a file, deleting the previous file.
11936 If N is a positive number, save the N next articles.
11937 If N is a negative number, save the N previous articles.
11938 If N is nil and any articles have been marked with the process mark,
11939 save those articles instead."
11940   (interactive "P")
11941   (require 'gnus-art)
11942   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11943     (gnus-summary-save-article arg)))
11944
11945 (defun gnus-summary-save-article-body-file (&optional arg)
11946   "Append the current article body to a file.
11947 If N is a positive number, save the N next articles.
11948 If N is a negative number, save the N previous articles.
11949 If N is nil and any articles have been marked with the process mark,
11950 save those articles instead."
11951   (interactive "P")
11952   (require 'gnus-art)
11953   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11954     (gnus-summary-save-article arg)))
11955
11956 (defun gnus-summary-write-article-body-file (&optional arg)
11957   "Write the current article body to a file, deleting the previous file.
11958 If N is a positive number, save the N next articles.
11959 If N is a negative number, save the N previous articles.
11960 If N is nil and any articles have been marked with the process mark,
11961 save those articles instead."
11962   (interactive "P")
11963   (require 'gnus-art)
11964   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11965     (gnus-summary-save-article arg)))
11966
11967 (defun gnus-summary-muttprint (&optional arg)
11968   "Print the current article using Muttprint.
11969 If N is a positive number, save the N next articles.
11970 If N is a negative number, save the N previous articles.
11971 If N is nil and any articles have been marked with the process mark,
11972 save those articles instead."
11973   (interactive "P")
11974   (require 'gnus-art)
11975   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11976     (gnus-summary-save-article arg t)))
11977
11978 (defun gnus-summary-pipe-message (program)
11979   "Pipe the current article through PROGRAM."
11980   (interactive "sProgram: ")
11981   (gnus-summary-select-article)
11982   (let ((mail-header-separator ""))
11983     (gnus-eval-in-buffer-window gnus-article-buffer
11984       (save-restriction
11985         (widen)
11986         (let ((start (window-start))
11987               (inhibit-read-only t))
11988           (message-pipe-buffer-body program)
11989           (set-window-start (get-buffer-window (current-buffer)) start))))))
11990
11991 (defun gnus-get-split-value (methods)
11992   "Return a value based on the split METHODS."
11993   (let (split-name method result match)
11994     (when methods
11995       (with-current-buffer gnus-original-article-buffer
11996         (save-restriction
11997           (nnheader-narrow-to-headers)
11998           (while (and methods (not split-name))
11999             (goto-char (point-min))
12000             (setq method (pop methods))
12001             (setq match (car method))
12002             (when (cond
12003                    ((stringp match)
12004                     ;; Regular expression.
12005                     (ignore-errors
12006                       (re-search-forward match nil t)))
12007                    ((functionp match)
12008                     ;; Function.
12009                     (save-restriction
12010                       (widen)
12011                       (setq result (funcall match gnus-newsgroup-name))))
12012                    ((consp match)
12013                     ;; Form.
12014                     (save-restriction
12015                       (widen)
12016                       (setq result (eval match)))))
12017               (setq split-name (cdr method))
12018               (cond ((stringp result)
12019                      (push (expand-file-name
12020                             result gnus-article-save-directory)
12021                            split-name))
12022                     ((consp result)
12023                      (setq split-name (append result split-name)))))))))
12024     (nreverse split-name)))
12025
12026 (defun gnus-valid-move-group-p (group)
12027   (and (symbolp group)
12028        (boundp group)
12029        (symbol-name group)
12030        (symbol-value group)
12031        (gnus-get-function (gnus-find-method-for-group
12032                            (symbol-name group)) 'request-accept-article t)))
12033
12034 (defun gnus-read-move-group-name (prompt default articles prefix)
12035   "Read a group name."
12036   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12037          (minibuffer-confirm-incomplete nil) ; XEmacs
12038          (prom
12039           (format "%s %s to"
12040                   prompt
12041                   (if (> (length articles) 1)
12042                       (format "these %d articles" (length articles))
12043                     "this article")))
12044          (to-newsgroup
12045           (cond
12046            ((null split-name)
12047             (gnus-group-completing-read
12048              prom
12049              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12050              nil prefix nil default))
12051            ((= 1 (length split-name))
12052             (gnus-group-completing-read
12053              prom
12054              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12055              nil prefix 'gnus-group-history (car split-name)))
12056            (t
12057             (gnus-completing-read
12058              prom (nreverse split-name) nil nil 'gnus-group-history))))
12059          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12060          encoded)
12061     (when to-newsgroup
12062       (if (or (string= to-newsgroup "")
12063               (string= to-newsgroup prefix))
12064           (setq to-newsgroup default))
12065       (unless to-newsgroup
12066         (error "No group name entered"))
12067       (setq encoded (mm-encode-coding-string
12068                      to-newsgroup
12069                      (gnus-group-name-charset to-method to-newsgroup)))
12070       (or (gnus-active encoded)
12071           (gnus-activate-group encoded nil nil to-method)
12072           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12073                                      to-newsgroup))
12074               (or (and (gnus-request-create-group encoded to-method)
12075                        (gnus-activate-group encoded nil nil to-method)
12076                        (gnus-subscribe-group encoded))
12077                   (error "Couldn't create group %s" to-newsgroup)))
12078           (error "No such group: %s" to-newsgroup))
12079       encoded)))
12080
12081 (defvar gnus-summary-save-parts-counter)
12082 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12083
12084 (defun gnus-summary-save-parts (type dir n &optional reverse)
12085   "Save parts matching TYPE to DIR.
12086 If REVERSE, save parts that do not match TYPE."
12087   (interactive
12088    (list (read-string "Save parts of type: "
12089                       (or (car gnus-summary-save-parts-type-history)
12090                           gnus-summary-save-parts-default-mime)
12091                       'gnus-summary-save-parts-type-history)
12092          (setq gnus-summary-save-parts-last-directory
12093                (read-directory-name "Save to directory: "
12094                                     gnus-summary-save-parts-last-directory
12095                                     nil t))
12096          current-prefix-arg))
12097   (gnus-summary-iterate n
12098     (let ((gnus-display-mime-function nil)
12099           gnus-article-prepare-hook
12100           gnus-article-decode-hook
12101           gnus-display-mime-function
12102           gnus-break-pages
12103           (gnus-inhibit-treatment t))
12104       (gnus-summary-select-article))
12105     (with-current-buffer gnus-article-buffer
12106       (let ((handles (or gnus-article-mime-handles
12107                          (mm-dissect-buffer nil gnus-article-loose-mime)
12108                          (and gnus-article-emulate-mime
12109                               (mm-uu-dissect))))
12110             (gnus-summary-save-parts-counter 1))
12111         (when handles
12112           (gnus-summary-save-parts-1 type dir handles reverse)
12113           (unless gnus-article-mime-handles ;; Don't destroy this case.
12114             (mm-destroy-parts handles)))))))
12115
12116 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12117   (if (stringp (car handle))
12118       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12119               (cdr handle))
12120     (when (if reverse
12121               (not (string-match type (mm-handle-media-type handle)))
12122             (string-match type (mm-handle-media-type handle)))
12123       (let ((file (expand-file-name
12124                    (gnus-map-function
12125                     mm-file-name-rewrite-functions
12126                     (file-name-nondirectory
12127                      (or
12128                       (mail-content-type-get
12129                        (mm-handle-disposition handle) 'filename)
12130                       (mail-content-type-get
12131                        (mm-handle-type handle) 'name)
12132                       (format "%s.%d.%d" gnus-newsgroup-name
12133                               (cdr gnus-article-current)
12134                               gnus-summary-save-parts-counter))))
12135                    dir)))
12136         (incf gnus-summary-save-parts-counter)
12137         (unless (file-exists-p file)
12138           (mm-save-part-to-file handle file))))))
12139
12140 ;; Summary extract commands
12141
12142 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12143   (let ((inhibit-read-only t)
12144         (article (gnus-summary-article-number))
12145         after-article b e)
12146     (unless (gnus-summary-goto-subject article)
12147       (error "No such article: %d" article))
12148     (gnus-summary-position-point)
12149     ;; If all commands are to be bunched up on one line, we collect
12150     ;; them here.
12151     (unless gnus-view-pseudos-separately
12152       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12153             files action)
12154         (while ps
12155           (setq action (cdr (assq 'action (car ps))))
12156           (setq files (list (cdr (assq 'name (car ps)))))
12157           (while (and ps (cdr ps)
12158                       (string= (or action "1")
12159                                (or (cdr (assq 'action (cadr ps))) "2")))
12160             (push (cdr (assq 'name (cadr ps))) files)
12161             (setcdr ps (cddr ps)))
12162           (when files
12163             (when (not (string-match "%s" action))
12164               (push " " files))
12165             (push " " files)
12166             (when (assq 'execute (car ps))
12167               (setcdr (assq 'execute (car ps))
12168                       (funcall (if (string-match "%s" action)
12169                                    'format 'concat)
12170                                action
12171                                (mapconcat
12172                                 (lambda (f)
12173                                   (if (equal f " ")
12174                                       f
12175                                     (shell-quote-argument f)))
12176                                 files " ")))))
12177           (setq ps (cdr ps)))))
12178     (if (and gnus-view-pseudos (not not-view))
12179         (while pslist
12180           (when (assq 'execute (car pslist))
12181             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12182                                   (eq gnus-view-pseudos 'not-confirm)))
12183           (setq pslist (cdr pslist)))
12184       (save-excursion
12185         (while pslist
12186           (setq after-article (or (cdr (assq 'article (car pslist)))
12187                                   (gnus-summary-article-number)))
12188           (gnus-summary-goto-subject after-article)
12189           (forward-line 1)
12190           (setq b (point))
12191           (insert "    " (file-name-nondirectory
12192                           (cdr (assq 'name (car pslist))))
12193                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12194           (setq e (point))
12195           (forward-line -1)             ; back to `b'
12196           (gnus-add-text-properties
12197            b (1- e) (list 'gnus-number gnus-reffed-article-number
12198                           gnus-mouse-face-prop gnus-mouse-face))
12199           (gnus-data-enter
12200            after-article gnus-reffed-article-number
12201            gnus-unread-mark b (car pslist) 0 (- e b))
12202           (setq gnus-newsgroup-unreads
12203                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12204                                          gnus-reffed-article-number))
12205           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12206           (setq pslist (cdr pslist)))))))
12207
12208 (defun gnus-pseudos< (p1 p2)
12209   (let ((c1 (cdr (assq 'action p1)))
12210         (c2 (cdr (assq 'action p2))))
12211     (and c1 c2 (string< c1 c2))))
12212
12213 (defun gnus-request-pseudo-article (props)
12214   (cond ((assq 'execute props)
12215          (gnus-execute-command (cdr (assq 'execute props)))))
12216   (let ((gnus-current-article (gnus-summary-article-number)))
12217     (gnus-run-hooks 'gnus-mark-article-hook)))
12218
12219 (defun gnus-execute-command (command &optional automatic)
12220   (save-excursion
12221     (gnus-article-setup-buffer)
12222     (set-buffer gnus-article-buffer)
12223     (setq buffer-read-only nil)
12224     (let ((command (if automatic command
12225                      (read-string "Command: " (cons command 0)))))
12226       (erase-buffer)
12227       (insert "$ " command "\n\n")
12228       (if gnus-view-pseudo-asynchronously
12229           (start-process "gnus-execute" (current-buffer) shell-file-name
12230                          shell-command-switch command)
12231         (call-process shell-file-name nil t nil
12232                       shell-command-switch command)))))
12233
12234 ;; Summary kill commands.
12235
12236 (defun gnus-summary-edit-global-kill (article)
12237   "Edit the \"global\" kill file."
12238   (interactive (list (gnus-summary-article-number)))
12239   (gnus-group-edit-global-kill article))
12240
12241 (defun gnus-summary-edit-local-kill ()
12242   "Edit a local kill file applied to the current newsgroup."
12243   (interactive)
12244   (setq gnus-current-headers (gnus-summary-article-header))
12245   (gnus-group-edit-local-kill
12246    (gnus-summary-article-number) gnus-newsgroup-name))
12247
12248 ;;; Header reading.
12249
12250 (defun gnus-read-header (id &optional header)
12251   "Read the headers of article ID and enter them into the Gnus system."
12252   (let ((group gnus-newsgroup-name)
12253         (gnus-override-method
12254          (or
12255           gnus-override-method
12256           (and (gnus-news-group-p gnus-newsgroup-name)
12257                (car (gnus-refer-article-methods)))))
12258         where)
12259     ;; First we check to see whether the header in question is already
12260     ;; fetched.
12261     (if (stringp id)
12262         ;; This is a Message-ID.
12263         (setq header (or header (gnus-id-to-header id)))
12264       ;; This is an article number.
12265       (setq header (or header (gnus-summary-article-header id))))
12266     (if (and header
12267              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12268         ;; We have found the header.
12269         header
12270       ;; We have to really fetch the header to this article.
12271       (with-current-buffer nntp-server-buffer
12272         (when (setq where (gnus-request-head id group))
12273           (nnheader-fold-continuation-lines)
12274           (goto-char (point-max))
12275           (insert ".\n")
12276           (goto-char (point-min))
12277           (insert "211 ")
12278           (princ (cond
12279                   ((numberp id) id)
12280                   ((cdr where) (cdr where))
12281                   (header (mail-header-number header))
12282                   (t gnus-reffed-article-number))
12283                  (current-buffer))
12284           (insert " Article retrieved.\n"))
12285         (if (or (not where)
12286                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12287             ()                          ; Malformed head.
12288           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12289             (when (and (stringp id)
12290                        (or
12291                         (not (string= (gnus-group-real-name group)
12292                                       (car where)))
12293                         (not (gnus-server-equal gnus-override-method
12294                                                 (gnus-group-method group)))))
12295               ;; If we fetched by Message-ID and the article came from
12296               ;; a different group (or server), we fudge some bogus
12297               ;; article numbers for this article.
12298               (mail-header-set-number header gnus-reffed-article-number))
12299             (with-current-buffer gnus-summary-buffer
12300               (decf gnus-reffed-article-number)
12301               (gnus-remove-header (mail-header-number header))
12302               (push header gnus-newsgroup-headers)
12303               (setq gnus-current-headers header)
12304               (push (mail-header-number header) gnus-newsgroup-limit)))
12305           header)))))
12306
12307 (defun gnus-remove-header (number)
12308   "Remove header NUMBER from `gnus-newsgroup-headers'."
12309   (if (and gnus-newsgroup-headers
12310            (= number (mail-header-number (car gnus-newsgroup-headers))))
12311       (pop gnus-newsgroup-headers)
12312     (let ((headers gnus-newsgroup-headers))
12313       (while (and (cdr headers)
12314                   (not (= number (mail-header-number (cadr headers)))))
12315         (pop headers))
12316       (when (cdr headers)
12317         (setcdr headers (cddr headers))))))
12318
12319 ;;;
12320 ;;; summary highlights
12321 ;;;
12322
12323 (defun gnus-highlight-selected-summary ()
12324   "Highlight selected article in summary buffer."
12325   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12326   (when gnus-summary-selected-face
12327     (save-excursion
12328       (let* ((beg (point-at-bol))
12329              (end (point-at-eol))
12330              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12331              (from (if (get-text-property beg gnus-mouse-face-prop)
12332                        beg
12333                      (or (next-single-property-change
12334                           beg gnus-mouse-face-prop nil end)
12335                          beg)))
12336              (to
12337               (if (= from end)
12338                   (- from 2)
12339                 (or (next-single-property-change
12340                      from gnus-mouse-face-prop nil end)
12341                     end))))
12342         ;; If no mouse-face prop on line we will have to = from = end,
12343         ;; so we highlight the entire line instead.
12344         (when (= (+ to 2) from)
12345           (setq from beg)
12346           (setq to end))
12347         (if gnus-newsgroup-selected-overlay
12348             ;; Move old overlay.
12349             (gnus-move-overlay
12350              gnus-newsgroup-selected-overlay from to (current-buffer))
12351           ;; Create new overlay.
12352           (gnus-overlay-put
12353            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12354            'face gnus-summary-selected-face))))))
12355
12356 (defvar gnus-summary-highlight-line-cached nil)
12357 (defvar gnus-summary-highlight-line-trigger nil)
12358
12359 (defun gnus-summary-highlight-line-0 ()
12360   (if (and (eq gnus-summary-highlight-line-trigger
12361                gnus-summary-highlight)
12362            gnus-summary-highlight-line-cached)
12363       gnus-summary-highlight-line-cached
12364     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12365           gnus-summary-highlight-line-cached
12366           (let* ((cond (list 'cond))
12367                  (c cond)
12368                  (list gnus-summary-highlight))
12369             (while list
12370               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12371                               nil))
12372               (setq c (cdr c)
12373                     list (cdr list)))
12374             (gnus-byte-compile (list 'lambda nil cond))))))
12375
12376 (defun gnus-summary-highlight-line ()
12377   "Highlight current line according to `gnus-summary-highlight'."
12378   (let* ((beg (point-at-bol))
12379          (article (or (gnus-summary-article-number) gnus-current-article))
12380          (score (or (cdr (assq article
12381                                gnus-newsgroup-scored))
12382                     gnus-summary-default-score 0))
12383          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12384          (inhibit-read-only t)
12385          (default gnus-summary-default-score)
12386          (default-high gnus-summary-default-high-score)
12387          (default-low gnus-summary-default-low-score)
12388          (uncached (and gnus-summary-use-undownloaded-faces
12389                         (memq article gnus-newsgroup-undownloaded)
12390                         (not (memq article gnus-newsgroup-cached)))))
12391     (let ((face (funcall (gnus-summary-highlight-line-0))))
12392       (unless (eq face (get-text-property beg 'face))
12393         (gnus-put-text-property-excluding-characters-with-faces
12394          beg (point-at-eol) 'face
12395          (setq face (if (boundp face) (symbol-value face) face)))
12396         (when gnus-summary-highlight-line-function
12397           (funcall gnus-summary-highlight-line-function article face))))))
12398
12399 (defun gnus-update-read-articles (group unread &optional compute)
12400   "Update the list of read articles in GROUP.
12401 UNREAD is a sorted list."
12402   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12403         (info (gnus-get-info group))
12404         (prev 1)
12405         read)
12406     (if (or (not info) (not active))
12407         ;; There is no info on this group if it was, in fact,
12408         ;; killed.  Gnus stores no information on killed groups, so
12409         ;; there's nothing to be done.
12410         ;; One could store the information somewhere temporarily,
12411         ;; perhaps...  Hmmm...
12412         ()
12413       ;; Remove any negative articles numbers.
12414       (while (and unread (< (car unread) 0))
12415         (setq unread (cdr unread)))
12416       ;; Remove any expired article numbers
12417       (while (and unread (< (car unread) (car active)))
12418         (setq unread (cdr unread)))
12419       ;; Compute the ranges of read articles by looking at the list of
12420       ;; unread articles.
12421       (while unread
12422         (when (/= (car unread) prev)
12423           (push (if (= prev (1- (car unread))) prev
12424                   (cons prev (1- (car unread))))
12425                 read))
12426         (setq prev (1+ (car unread)))
12427         (setq unread (cdr unread)))
12428       (when (<= prev (cdr active))
12429         (push (cons prev (cdr active)) read))
12430       (setq read (if (> (length read) 1) (nreverse read) read))
12431       (if compute
12432           read
12433         (save-excursion
12434           (let (setmarkundo)
12435             ;; Propagate the read marks to the backend.
12436             (when (and gnus-propagate-marks
12437                        (gnus-check-backend-function 'request-set-mark group))
12438               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12439                     (add (gnus-remove-from-range read (gnus-info-read info))))
12440                 (when (or add del)
12441                   (unless (gnus-check-group group)
12442                     (error "Can't open server for %s" group))
12443                   (gnus-request-set-mark
12444                    group (delq nil (list (if add (list add 'add '(read)))
12445                                          (if del (list del 'del '(read))))))
12446                   (setq setmarkundo
12447                         `(gnus-request-set-mark
12448                           ,group
12449                           ',(delq nil (list
12450                                        (if del (list del 'add '(read)))
12451                                        (if add (list add 'del '(read))))))))))
12452             (set-buffer gnus-group-buffer)
12453             (gnus-undo-register
12454               `(progn
12455                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12456                  (gnus-info-set-read ',info ',(gnus-info-read info))
12457                  (gnus-get-unread-articles-in-group ',info
12458                                                     (gnus-active ,group))
12459                  (gnus-group-update-group ,group t)
12460                  ,setmarkundo))))
12461         ;; Enter this list into the group info.
12462         (gnus-info-set-read info read)
12463         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12464         (gnus-get-unread-articles-in-group info (gnus-active group))
12465         t))))
12466
12467 (defun gnus-offer-save-summaries ()
12468   "Offer to save all active summary buffers."
12469   (let (buffers)
12470     ;; Go through all buffers and find all summaries.
12471     (dolist (buffer (buffer-list))
12472       (when (and (setq buffer (buffer-name buffer))
12473                  (string-match "Summary" buffer)
12474                  (with-current-buffer buffer
12475                    ;; We check that this is, indeed, a summary buffer.
12476                    (and (eq major-mode 'gnus-summary-mode)
12477                         ;; Also make sure this isn't bogus.
12478                         gnus-newsgroup-prepared
12479                         ;; Also make sure that this isn't a
12480                         ;; dead summary buffer.
12481                         (not gnus-dead-summary-mode))))
12482         (push buffer buffers)))
12483     ;; Go through all these summary buffers and offer to save them.
12484     (when buffers
12485       (save-excursion
12486         (map-y-or-n-p
12487          "Update summary buffer %s? "
12488          (lambda (buf)
12489            (switch-to-buffer buf)
12490            (gnus-summary-exit))
12491          buffers)))))
12492
12493 (defun gnus-summary-setup-default-charset ()
12494   "Setup newsgroup default charset."
12495   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12496       (setq gnus-newsgroup-charset nil)
12497     (let* ((ignored-charsets
12498             (or gnus-newsgroup-ephemeral-ignored-charsets
12499                 (append
12500                  (and gnus-newsgroup-name
12501                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12502                  gnus-newsgroup-ignored-charsets))))
12503       (setq gnus-newsgroup-charset
12504             (or gnus-newsgroup-ephemeral-charset
12505                 (and gnus-newsgroup-name
12506                      (gnus-parameter-charset gnus-newsgroup-name))
12507                 gnus-default-charset))
12508       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12509            ignored-charsets))))
12510
12511 ;;;
12512 ;;; Mime Commands
12513 ;;;
12514
12515 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12516   "Display the current article buffer fully MIME-buttonized.
12517 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12518 treated as multipart/mixed."
12519   (interactive "P")
12520   (require 'gnus-art)
12521   (let ((gnus-unbuttonized-mime-types nil)
12522         (gnus-mime-display-multipart-as-mixed show-all-parts))
12523     (gnus-summary-show-article)))
12524
12525 (defun gnus-summary-repair-multipart (article)
12526   "Add a Content-Type header to a multipart article without one."
12527   (interactive (list (gnus-summary-article-number)))
12528   (gnus-with-article article
12529     (message-narrow-to-head)
12530     (message-remove-header "Mime-Version")
12531     (goto-char (point-max))
12532     (insert "Mime-Version: 1.0\n")
12533     (widen)
12534     (when (search-forward "\n--" nil t)
12535       (let ((separator (buffer-substring (point) (point-at-eol))))
12536         (message-narrow-to-head)
12537         (message-remove-header "Content-Type")
12538         (goto-char (point-max))
12539         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12540                         separator))
12541         (widen))))
12542   (let (gnus-mark-article-hook)
12543     (gnus-summary-select-article t t nil article)))
12544
12545 (defun gnus-summary-toggle-display-buttonized ()
12546   "Toggle the buttonizing of the article buffer."
12547   (interactive)
12548   (require 'gnus-art)
12549   (if (setq gnus-inhibit-mime-unbuttonizing
12550             (not gnus-inhibit-mime-unbuttonizing))
12551       (let ((gnus-unbuttonized-mime-types nil))
12552         (gnus-summary-show-article))
12553     (gnus-summary-show-article)))
12554
12555 ;;;
12556 ;;; Generic summary marking commands
12557 ;;;
12558
12559 (defvar gnus-summary-marking-alist
12560   '((read gnus-del-mark "d")
12561     (unread gnus-unread-mark "u")
12562     (ticked gnus-ticked-mark "!")
12563     (dormant gnus-dormant-mark "?")
12564     (expirable gnus-expirable-mark "e"))
12565   "An alist of names/marks/keystrokes.")
12566
12567 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12568 (defvar gnus-summary-mark-map)
12569
12570 (defun gnus-summary-make-all-marking-commands ()
12571   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12572   (dolist (elem gnus-summary-marking-alist)
12573     (apply 'gnus-summary-make-marking-command elem)))
12574
12575 (defun gnus-summary-make-marking-command (name mark keystroke)
12576   (let ((map (make-sparse-keymap)))
12577     (define-key gnus-summary-generic-mark-map keystroke map)
12578     (dolist (lway `((next "next" next nil "n")
12579                     (next-unread "next unread" next t "N")
12580                     (prev "previous" prev nil "p")
12581                     (prev-unread "previous unread" prev t "P")
12582                     (nomove "" nil nil ,keystroke)))
12583       (let ((func (gnus-summary-make-marking-command-1
12584                    mark (car lway) lway name)))
12585         (setq func (eval func))
12586         (define-key map (nth 4 lway) func)))))
12587
12588 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12589   `(defun ,(intern
12590             (format "gnus-summary-put-mark-as-%s%s"
12591                     name (if (eq way 'nomove)
12592                              ""
12593                            (concat "-" (symbol-name way)))))
12594      (n)
12595      ,(format
12596        "Mark the current article as %s%s.
12597 If N, the prefix, then repeat N times.
12598 If N is negative, move in reverse order.
12599 The difference between N and the actual number of articles marked is
12600 returned."
12601        name (cadr lway))
12602      (interactive "p")
12603      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12604
12605 (defun gnus-summary-generic-mark (n mark move unread)
12606   "Mark N articles with MARK."
12607   (unless (eq major-mode 'gnus-summary-mode)
12608     (error "This command can only be used in the summary buffer"))
12609   (gnus-summary-show-thread)
12610   (let ((nummove
12611          (cond
12612           ((eq move 'next) 1)
12613           ((eq move 'prev) -1)
12614           (t 0))))
12615     (if (zerop nummove)
12616         (setq n 1)
12617       (when (< n 0)
12618         (setq n (abs n)
12619               nummove (* -1 nummove))))
12620     (while (and (> n 0)
12621                 (gnus-summary-mark-article nil mark)
12622                 (zerop (gnus-summary-next-subject nummove unread t)))
12623       (setq n (1- n)))
12624     (when (/= 0 n)
12625       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12626     (gnus-summary-recenter)
12627     (gnus-summary-position-point)
12628     (gnus-set-mode-line 'summary)
12629     n))
12630
12631 (defun gnus-summary-insert-articles (articles)
12632   (when (setq articles
12633               (gnus-sorted-difference articles
12634                                       (mapcar (lambda (h)
12635                                                 (mail-header-number h))
12636                                               gnus-newsgroup-headers)))
12637     (setq gnus-newsgroup-headers
12638           (gnus-merge 'list
12639                       gnus-newsgroup-headers
12640                       (gnus-fetch-headers articles)
12641                       'gnus-article-sort-by-number))
12642     ;; Suppress duplicates?
12643     (when gnus-suppress-duplicates
12644       (gnus-dup-suppress-articles))
12645
12646     (if (and gnus-fetch-old-headers
12647              (eq gnus-headers-retrieved-by 'nov))
12648         ;; We might want to build some more threads first.
12649         (if (eq gnus-fetch-old-headers 'invisible)
12650             (gnus-build-all-threads)
12651           (gnus-build-old-threads))
12652       ;; Mark the inserted articles that are unread as unread.
12653       (setq gnus-newsgroup-unreads
12654             (gnus-sorted-nunion
12655              gnus-newsgroup-unreads
12656              (gnus-sorted-nintersection
12657               (gnus-list-of-unread-articles gnus-newsgroup-name)
12658               articles)))
12659       ;; Mark the inserted articles as selected so that the information
12660       ;; of the marks having been changed by a user may be updated when
12661       ;; exiting this group.  See `gnus-summary-update-info'.
12662       (dolist (art articles)
12663         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12664     ;; Let the Gnus agent mark articles as read.
12665     (when gnus-agent
12666       (gnus-agent-get-undownloaded-list))
12667     ;; Remove list identifiers from subject
12668     (when gnus-list-identifiers
12669       (gnus-summary-remove-list-identifiers))
12670     ;; First and last article in this newsgroup.
12671     (when gnus-newsgroup-headers
12672       (setq gnus-newsgroup-begin
12673             (mail-header-number (car gnus-newsgroup-headers))
12674             gnus-newsgroup-end
12675             (mail-header-number
12676              (gnus-last-element gnus-newsgroup-headers))))
12677     (when gnus-use-scoring
12678       (gnus-possibly-score-headers))))
12679
12680 (defun gnus-summary-insert-old-articles (&optional all)
12681   "Insert all old articles in this group.
12682 If ALL is non-nil, already read articles become readable.
12683 If ALL is a number, fetch this number of articles."
12684   (interactive "P")
12685   (prog1
12686       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12687             older len)
12688         (setq older
12689               ;; Some nntp servers lie about their active range.  When
12690               ;; this happens, the active range can be in the millions.
12691               ;; Use a compressed range to avoid creating a huge list.
12692               (gnus-range-difference (list gnus-newsgroup-active) old))
12693         (setq len (gnus-range-length older))
12694         (cond
12695          ((null older) nil)
12696          ((numberp all)
12697           (if (< all len)
12698               (let ((older-range (nreverse older)))
12699                 (setq older nil)
12700
12701                 (while (> all 0)
12702                   (let* ((r (pop older-range))
12703                          (min (if (numberp r) r (car r)))
12704                          (max (if (numberp r) r (cdr r))))
12705                     (while (and (<= min max)
12706                                 (> all 0))
12707                       (push max older)
12708                       (setq all (1- all)
12709                             max (1- max))))))
12710             (setq older (gnus-uncompress-range older))))
12711          (all
12712           (setq older (gnus-uncompress-range older)))
12713          (t
12714           (when (and (numberp gnus-large-newsgroup)
12715                    (> len gnus-large-newsgroup))
12716               (let* ((cursor-in-echo-area nil)
12717                      (initial (gnus-parameter-large-newsgroup-initial
12718                                gnus-newsgroup-name))
12719                      (input
12720                       (read-string
12721                        (format
12722                         "How many articles from %s (%s %d): "
12723                         (gnus-group-decoded-name gnus-newsgroup-name)
12724                         (if initial "max" "default")
12725                         len)
12726                        (if initial
12727                            (cons (number-to-string initial)
12728                                  0)))))
12729                 (unless (string-match "^[ \t]*$" input)
12730                   (setq all (string-to-number input))
12731                   (if (< all len)
12732                       (let ((older-range (nreverse older)))
12733                         (setq older nil)
12734
12735                         (while (> all 0)
12736                           (let* ((r (pop older-range))
12737                                  (min (if (numberp r) r (car r)))
12738                                  (max (if (numberp r) r (cdr r))))
12739                             (while (and (<= min max)
12740                                         (> all 0))
12741                               (push max older)
12742                               (setq all (1- all)
12743                                     max (1- max))))))))))
12744           (setq older (gnus-uncompress-range older))))
12745         (if (not older)
12746             (message "No old news.")
12747           (gnus-summary-insert-articles older)
12748           (gnus-summary-limit (gnus-sorted-nunion old older))))
12749     (gnus-summary-position-point)))
12750
12751 (defun gnus-summary-insert-new-articles ()
12752   "Insert all new articles in this group."
12753   (interactive)
12754   (prog1
12755       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12756             (old-high gnus-newsgroup-highest)
12757             (nnmail-fetched-sources (list t))
12758             i new)
12759         (setq gnus-newsgroup-active
12760               (gnus-copy-sequence
12761                (gnus-activate-group gnus-newsgroup-name 'scan)))
12762         (setq i (cdr gnus-newsgroup-active)
12763               gnus-newsgroup-highest i)
12764         (while (> i old-high)
12765           (push i new)
12766           (decf i))
12767         (if (not new)
12768             (message "No gnus is bad news")
12769           (gnus-summary-insert-articles new)
12770           (setq gnus-newsgroup-unreads
12771                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12772           (gnus-summary-limit (gnus-sorted-nunion old new))))
12773     (gnus-summary-position-point)))
12774
12775 ;;; Bookmark support for Gnus.
12776 (declare-function bookmark-make-record-default
12777                   "bookmark" (&optional no-file no-context posn))
12778 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12779 (declare-function bookmark-default-handler "bookmark" (bmk))
12780 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12781 (defvar bookmark-yank-point)
12782 (defvar bookmark-current-buffer)
12783
12784 (defun gnus-summary-bookmark-make-record ()
12785   "Make a bookmark entry for a Gnus summary buffer."
12786   (let (pos buf)
12787     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12788       (save-restriction              ; FIXME is it necessary to widen?
12789         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12790       (setq buf "art") ; We are recording bookmark from article buffer.
12791       (setq bookmark-yank-point (point))
12792       (setq bookmark-current-buffer (current-buffer))
12793       (gnus-article-show-summary))      ; Go back in summary buffer.
12794     ;; We are now recording bookmark from summary buffer.
12795     (unless buf (setq buf "sum"))
12796     (let* ((subject (elt (gnus-summary-article-header) 1))
12797            (grp     (car gnus-article-current))
12798            (art     (cdr gnus-article-current))
12799            (head    (gnus-summary-article-header art))
12800            (id      (mail-header-id head)))
12801       `(,subject
12802         ,@(condition-case nil
12803               (bookmark-make-record-default 'no-file 'no-context pos)
12804             (wrong-number-of-arguments
12805              (bookmark-make-record-default 'point-only)))
12806         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12807         (group . ,grp) (article . ,art)
12808         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12809
12810 ;;;###autoload
12811 (defun gnus-summary-bookmark-jump (bookmark)
12812   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12813 BOOKMARK is a bookmark name or a bookmark record."
12814   (let ((group    (bookmark-prop-get bookmark 'group))
12815         (article  (bookmark-prop-get bookmark 'article))
12816         (id       (bookmark-prop-get bookmark 'message-id))
12817         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12818     (gnus-fetch-group group (list article))
12819     (gnus-summary-insert-cached-articles)
12820     (gnus-summary-goto-article id nil 'force)
12821     ;; FIXME we have to wait article buffer is ready (only large buffer)
12822     ;; Is there a better solution to know that?
12823     ;; If we don't wait `bookmark-default-handler' will have no chance
12824     ;; to set position. However there is no error, just wrong pos.
12825     (sit-for 1)
12826     (when (string= buf "Gnus-art")
12827       (other-window 1))
12828     (bookmark-default-handler
12829      `(""
12830        (buffer . ,(current-buffer))
12831        . ,(bookmark-get-bookmark-record bookmark)))))
12832
12833 (gnus-summary-make-all-marking-commands)
12834
12835 (gnus-ems-redefine)
12836
12837 (provide 'gnus-sum)
12838
12839 (run-hooks 'gnus-sum-load-hook)
12840
12841 ;; Local Variables:
12842 ;; coding: iso-8859-1
12843 ;; End:
12844
12845 ;;; gnus-sum.el ends here