* eww.el (eww-tag-select): Don't render totally empty <select> forms.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996-2013 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (eval-when-compile
33   (when (featurep 'xemacs)
34     (require 'easy-mmode))) ; for `define-minor-mode'
35
36 (defvar tool-bar-mode)
37 (defvar gnus-tmp-header)
38
39 (require 'gnus)
40 (require 'gnus-group)
41 (require 'gnus-spec)
42 (require 'gnus-range)
43 (require 'gnus-int)
44 (require 'gnus-undo)
45 (require 'gnus-util)
46 (require 'gmm-utils)
47 (require 'mm-decode)
48 (require 'nnoo)
49
50 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
51 (autoload 'gnus-cache-write-active "gnus-cache")
52 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
53 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
54 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
55 (autoload 'mm-uu-dissect "mm-uu")
56 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
57   "Deuglify broken Outlook (Express) articles and redisplay."
58   t)
59 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
60 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
61 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
62 (autoload 'nnir-article-rsv "nnir" nil nil 'macro)
63 (autoload 'nnir-article-group "nnir" nil nil 'macro)
64
65 (defcustom gnus-kill-summary-on-exit t
66   "*If non-nil, kill the summary buffer when you exit from it.
67 If nil, the summary will become a \"*Dead Summary*\" buffer, and
68 it will be killed sometime later."
69   :group 'gnus-summary-exit
70   :type 'boolean)
71
72 (defcustom gnus-summary-next-group-on-exit t
73   "If non-nil, go to the next unread newsgroup on summary exit.
74 See `gnus-group-goto-unread'."
75   :link '(custom-manual "(gnus)Group Maneuvering")
76   :group 'gnus-summary-exit
77   :version "23.1" ;; No Gnus
78   :type 'boolean)
79
80 (defcustom gnus-summary-stop-at-end-of-message nil
81   "If non-nil, don't select the next message when using `SPC'."
82   :link '(custom-manual "(gnus)Group Maneuvering")
83   :group 'gnus-summary-maneuvering
84   :version "24.1"
85   :type 'boolean)
86
87 (defcustom gnus-fetch-old-headers nil
88   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
89 If an unread article in the group refers to an older, already
90 read (or just marked as read) article, the old article will not
91 normally be displayed in the Summary buffer.  If this variable is
92 t, Gnus will attempt to grab the headers to the old articles, and
93 thereby build complete threads.  If it has the value `some', all
94 old headers will be fetched but only enough headers to connect
95 otherwise loose threads will be displayed.  This variable can
96 also be a number.  In that case, no more than that number of old
97 headers will be fetched.  If it has the value `invisible', all
98 old headers will be fetched, but none will be displayed.
99
100 The server has to support NOV for any of this to work.
101
102 This feature can seriously impact performance it ignores all
103 locally cached header entries.  Setting it to t for groups for a
104 server that doesn't expire articles (such as news.gmane.org),
105 leads to very slow summary generation."
106   :group 'gnus-thread
107   :type '(choice (const :tag "off" nil)
108                  (const :tag "on" t)
109                  (const some)
110                  (const invisible)
111                  number
112                  (sexp :menu-tag "other" t)))
113
114 (defcustom gnus-refer-thread-limit 500
115   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
116 If t, fetch all the available old headers."
117   :group 'gnus-thread
118   :type '(choice number
119                  (sexp :menu-tag "other" t)))
120
121 (defcustom gnus-refer-thread-use-nnir nil
122   "*Use nnir to search an entire server when referring threads. A
123 nil value will only search for thread-related articles in the
124 current group."
125   :version "24.1"
126   :group 'gnus-thread
127   :type 'boolean)
128
129 (defcustom gnus-summary-make-false-root 'adopt
130   "*nil means that Gnus won't gather loose threads.
131 If the root of a thread has expired or been read in a previous
132 session, the information necessary to build a complete thread has been
133 lost.  Instead of having many small sub-threads from this original thread
134 scattered all over the summary buffer, Gnus can gather them.
135
136 If non-nil, Gnus will try to gather all loose sub-threads from an
137 original thread into one large thread.
138
139 If this variable is non-nil, it should be one of `none', `adopt',
140 `dummy' or `empty'.
141
142 If this variable is `none', Gnus will not make a false root, but just
143 present the sub-threads after another.
144 If this variable is `dummy', Gnus will create a dummy root that will
145 have all the sub-threads as children.
146 If this variable is `adopt', Gnus will make one of the \"children\"
147 the parent and mark all the step-children as such.
148 If this variable is `empty', the \"children\" are printed with empty
149 subject fields.  (Or rather, they will be printed with a string
150 given by the `gnus-summary-same-subject' variable.)"
151   :group 'gnus-thread
152   :type '(choice (const :tag "off" nil)
153                  (const none)
154                  (const dummy)
155                  (const adopt)
156                  (const empty)))
157
158 (defcustom gnus-summary-make-false-root-always nil
159   "Always make a false dummy root."
160   :version "22.1"
161   :group 'gnus-thread
162   :type 'boolean)
163
164 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
165   "*A regexp to match subjects to be excluded from loose thread gathering.
166 As loose thread gathering is done on subjects only, that means that
167 there can be many false gatherings performed.  By rooting out certain
168 common subjects, gathering might become saner."
169   :group 'gnus-thread
170   :type 'regexp)
171
172 (defcustom gnus-summary-gather-subject-limit nil
173   "*Maximum length of subject comparisons when gathering loose threads.
174 Use nil to compare full subjects.  Setting this variable to a low
175 number will help gather threads that have been corrupted by
176 newsreaders chopping off subject lines, but it might also mean that
177 unrelated articles that have subject that happen to begin with the
178 same few characters will be incorrectly gathered.
179
180 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
181 comparing subjects."
182   :group 'gnus-thread
183   :type '(choice (const :tag "off" nil)
184                  (const fuzzy)
185                  (sexp :menu-tag "on" t)))
186
187 (defcustom gnus-simplify-subject-functions nil
188   "List of functions taking a string argument that simplify subjects.
189 The functions are applied recursively.
190
191 Useful functions to put in this list include:
192 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
193 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
194   :group 'gnus-thread
195   :type '(repeat function))
196
197 (defcustom gnus-simplify-ignored-prefixes nil
198   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
199   :group 'gnus-thread
200   :type '(choice (const :tag "off" nil)
201                  regexp))
202
203 (defcustom gnus-build-sparse-threads nil
204   "*If non-nil, fill in the gaps in threads.
205 If `some', only fill in the gaps that are needed to tie loose threads
206 together.  If `more', fill in all leaf nodes that Gnus can find.  If
207 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
208   :group 'gnus-thread
209   :type '(choice (const :tag "off" nil)
210                  (const some)
211                  (const more)
212                  (sexp :menu-tag "all" t)))
213
214 (defcustom gnus-summary-thread-gathering-function
215   'gnus-gather-threads-by-subject
216   "*Function used for gathering loose threads.
217 There are two pre-defined functions: `gnus-gather-threads-by-subject',
218 which only takes Subjects into consideration; and
219 `gnus-gather-threads-by-references', which compared the References
220 headers of the articles to find matches."
221   :group 'gnus-thread
222   :type '(radio (function-item gnus-gather-threads-by-subject)
223                 (function-item gnus-gather-threads-by-references)
224                 (function :tag "other")))
225
226 (defcustom gnus-summary-same-subject ""
227   "*String indicating that the current article has the same subject as the previous.
228 This variable will only be used if the value of
229 `gnus-summary-make-false-root' is `empty'."
230   :group 'gnus-summary-format
231   :type 'string)
232
233 (defcustom gnus-summary-goto-unread nil
234   "*If t, many commands will go to the next unread article.
235 This applies to marking commands as well as other commands that
236 \"naturally\" select the next article, like, for instance, `SPC' at
237 the end of an article.
238
239 If nil, the marking commands do NOT go to the next unread article
240 \(they go to the next article instead).  If `never', commands that
241 usually go to the next unread article, will go to the next article,
242 whether it is read or not."
243   :version "24.1"
244   :group 'gnus-summary-marks
245   :link '(custom-manual "(gnus)Setting Marks")
246   :type '(choice (const :tag "off" nil)
247                  (const never)
248                  (sexp :menu-tag "on" t)))
249
250 (defcustom gnus-summary-default-score 0
251   "*Default article score level.
252 All scores generated by the score files will be added to this score.
253 If this variable is nil, scoring will be disabled."
254   :group 'gnus-score-default
255   :type '(choice (const :tag "disable")
256                  integer))
257
258 (defcustom gnus-summary-default-high-score 0
259   "*Default threshold for a high scored article.
260 An article will be highlighted as high scored if its score is greater
261 than this score."
262   :version "22.1"
263   :group 'gnus-score-default
264   :type 'integer)
265
266 (defcustom gnus-summary-default-low-score 0
267   "*Default threshold for a low scored article.
268 An article will be highlighted as low scored if its score is smaller
269 than this score."
270   :version "22.1"
271   :group 'gnus-score-default
272   :type 'integer)
273
274 (defcustom gnus-summary-zcore-fuzz 0
275   "*Fuzziness factor for the zcore in the summary buffer.
276 Articles with scores closer than this to `gnus-summary-default-score'
277 will not be marked."
278   :group 'gnus-summary-format
279   :type 'integer)
280
281 (defcustom gnus-simplify-subject-fuzzy-regexp nil
282   "*Strings to be removed when doing fuzzy matches.
283 This can either be a regular expression or list of regular expressions
284 that will be removed from subject strings if fuzzy subject
285 simplification is selected."
286   :group 'gnus-thread
287   :type '(repeat regexp))
288
289 (defcustom gnus-show-threads t
290   "*If non-nil, display threads in summary mode."
291   :group 'gnus-thread
292   :type 'boolean)
293
294 (defcustom gnus-thread-hide-subtree nil
295   "*If non-nil, hide all threads initially.
296 This can be a predicate specifier which says which threads to hide.
297 If threads are hidden, you have to run the command
298 `gnus-summary-show-thread' by hand or select an article."
299   :group 'gnus-thread
300   :type '(radio (sexp :format "Non-nil\n"
301                       :match (lambda (widget value)
302                                (not (or (consp value) (functionp value))))
303                       :value t)
304                 (const nil)
305                 (sexp :tag "Predicate specifier")))
306
307 (defcustom gnus-thread-hide-killed t
308   "*If non-nil, hide killed threads automatically."
309   :group 'gnus-thread
310   :type 'boolean)
311
312 (defcustom gnus-thread-ignore-subject t
313   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
314 If nil, articles that have different subjects from their parents will
315 start separate threads."
316   :group 'gnus-thread
317   :type 'boolean)
318
319 (defcustom gnus-thread-operation-ignore-subject t
320   "*If non-nil, subjects will be ignored when doing thread commands.
321 This affects commands like `gnus-summary-kill-thread' and
322 `gnus-summary-lower-thread'.
323
324 If this variable is nil, articles in the same thread with different
325 subjects will not be included in the operation in question.  If this
326 variable is `fuzzy', only articles that have subjects that are fuzzily
327 equal will be included."
328   :group 'gnus-thread
329   :type '(choice (const :tag "off" nil)
330                  (const fuzzy)
331                  (sexp :tag "on" t)))
332
333 (defcustom gnus-thread-indent-level 4
334   "*Number that says how much each sub-thread should be indented."
335   :group 'gnus-thread
336   :type 'integer)
337
338 (defcustom gnus-auto-extend-newsgroup t
339   "*If non-nil, extend newsgroup forward and backward when requested."
340   :group 'gnus-summary-choose
341   :type 'boolean)
342
343 (defcustom gnus-auto-select-first t
344   "If non-nil, select an article on group entry.
345 An article is selected automatically when entering a group
346 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
347 `gnus-summary-catchup-and-goto-next-group'.
348
349 Which article is selected is controlled by the variable
350 `gnus-auto-select-subject'.
351
352 If you want to prevent automatic selection of articles in some
353 newsgroups, set the variable to nil in `gnus-select-group-hook'."
354   ;; Commands include...
355   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
356   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
357   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
358   :group 'gnus-group-select
359   :type '(choice (const :tag "none" nil)
360                  (sexp :menu-tag "first" t)))
361
362 (defcustom gnus-auto-select-subject 'unseen-or-unread
363   "*Says what subject to place under point when entering a group.
364
365 This variable can either be the symbols `first' (place point on the
366 first subject), `unread' (place point on the subject line of the first
367 unread article), `best' (place point on the subject line of the
368 highest-scored article), `unseen' (place point on the subject line of
369 the first unseen article), `unseen-or-unread' (place point on the subject
370 line of the first unseen article or, if all articles have been seen, on the
371 subject line of the first unread article), or a function to be called to
372 place point on some subject line."
373   :version "24.1"
374   :group 'gnus-group-select
375   :type '(choice (const best)
376                  (const unread)
377                  (const first)
378                  (const unseen)
379                  (const unseen-or-unread)
380                  (function :tag "Function to call")))
381
382 (defcustom gnus-auto-select-next t
383   "*If non-nil, offer to go to the next group from the end of the previous.
384 If the value is t and the next newsgroup is empty, Gnus will exit
385 summary mode and go back to group mode.  If the value is neither nil
386 nor t, Gnus will select the following unread newsgroup.  In
387 particular, if the value is the symbol `quietly', the next unread
388 newsgroup will be selected without any confirmation, and if it is
389 `almost-quietly', the next group will be selected without any
390 confirmation if you are located on the last article in the group.
391 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
392 will go to the next group without confirmation."
393   :group 'gnus-summary-maneuvering
394   :type '(choice (const :tag "off" nil)
395                  (const quietly)
396                  (const almost-quietly)
397                  (const slightly-quietly)
398                  (sexp :menu-tag "on" t)))
399
400 (defcustom gnus-auto-select-same nil
401   "*If non-nil, select the next article with the same subject.
402 If there are no more articles with the same subject, go to
403 the first unread article."
404   :group 'gnus-summary-maneuvering
405   :type 'boolean)
406
407 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
408   "What article should be selected after exiting an ephemeral group.
409 Valid values include:
410
411 `next'
412   Select the next article.
413 `next-unread'
414   Select the next unread article.
415 `next-noselect'
416   Move the cursor to the next article.  This is the default.
417 `next-unread-noselect'
418   Move the cursor to the next unread article.
419
420 If it has any other value or there is no next (unread) article, the
421 article selected before entering to the ephemeral group will appear."
422   :version "23.1" ;; No Gnus
423   :group 'gnus-summary-maneuvering
424   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
425                  (const next) (const next-unread)
426                  (const next-noselect) (const next-unread-noselect)
427                  (sexp :tag "other" :value nil)))
428
429 (defcustom gnus-auto-goto-ignores 'unfetched
430   "*Says how to handle unfetched articles when maneuvering.
431
432 This variable can either be the symbols nil (maneuver to any
433 article), `undownloaded' (maneuvering while unplugged ignores articles
434 that have not been fetched), `always-undownloaded' (maneuvering always
435 ignores articles that have not been fetched), `unfetched' (maneuvering
436 ignores articles whose headers have not been fetched).
437
438 NOTE: The list of unfetched articles will always be nil when plugged
439 and, when unplugged, a subset of the undownloaded article list."
440   :version "22.1"
441   :group 'gnus-summary-maneuvering
442   :type '(choice (const :tag "None" nil)
443                  (const :tag "Undownloaded when unplugged" undownloaded)
444                  (const :tag "Undownloaded" always-undownloaded)
445                  (const :tag "Unfetched" unfetched)))
446
447 (defcustom gnus-summary-check-current nil
448   "*If non-nil, consider the current article when moving.
449 The \"unread\" movement commands will stay on the same line if the
450 current article is unread."
451   :group 'gnus-summary-maneuvering
452   :type 'boolean)
453
454 (defcustom gnus-auto-center-summary
455   (max (or (bound-and-true-p scroll-margin) 0) 2)
456   "*If non-nil, always center the current summary buffer.
457 In particular, if `vertical' do only vertical recentering.  If non-nil
458 and non-`vertical', do both horizontal and vertical recentering."
459   :group 'gnus-summary-maneuvering
460   :type '(choice (const :tag "none" nil)
461                  (const vertical)
462                  (integer :tag "height")
463                  (sexp :menu-tag "both" t)))
464
465 (defcustom gnus-auto-center-group t
466   "If non-nil, always center the group buffer."
467   :group 'gnus-summary-maneuvering
468   :type 'boolean)
469
470 (defcustom gnus-show-all-headers nil
471   "*If non-nil, don't hide any headers."
472   :group 'gnus-article-hiding
473   :group 'gnus-article-headers
474   :type 'boolean)
475
476 (defcustom gnus-summary-ignore-duplicates nil
477   "*If non-nil, ignore articles with identical Message-ID headers."
478   :group 'gnus-summary
479   :type 'boolean)
480
481 (defcustom gnus-single-article-buffer nil
482   "*If non-nil, display all articles in the same buffer.
483 If nil, each group will get its own article buffer."
484   :version "24.1"
485   :group 'gnus-article-various
486   :type 'boolean)
487
488 (defcustom gnus-widen-article-window nil
489   "If non-nil, selecting the article buffer will display only the article buffer."
490   :version "24.1"
491   :group 'gnus-article-various
492   :type 'boolean)
493
494 (defcustom gnus-break-pages t
495   "*If non-nil, do page breaking on articles.
496 The page delimiter is specified by the `gnus-page-delimiter'
497 variable."
498   :group 'gnus-article-various
499   :type 'boolean)
500
501 (defcustom gnus-move-split-methods nil
502   "*Variable used to suggest where articles are to be moved to.
503 It uses the same syntax as the `gnus-split-methods' variable.
504 However, whereas `gnus-split-methods' specifies file names as targets,
505 this variable specifies group names."
506   :group 'gnus-summary-mail
507   :type '(repeat (choice (list :value (fun) function)
508                          (cons :value ("" "") regexp (repeat string))
509                          (sexp :value nil))))
510
511 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
512   "Function used to compute default prefix for article move/copy/etc prompts.
513 The function should take one argument, a group name, and return a
514 string with the suggested prefix."
515   :group 'gnus-summary-mail
516   :type 'function)
517
518 ;; FIXME: Although the custom type is `character' for the following variables,
519 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
520
521 (defcustom gnus-unread-mark ?           ;Whitespace
522   "*Mark used for unread articles."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-ticked-mark ?!
527   "*Mark used for ticked articles."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-dormant-mark ??
532   "*Mark used for dormant articles."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-del-mark ?r
537   "*Mark used for del'd articles."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-read-mark ?R
542   "*Mark used for read articles."
543   :group 'gnus-summary-marks
544   :type 'character)
545
546 (defcustom gnus-expirable-mark ?E
547   "*Mark used for expirable articles."
548   :group 'gnus-summary-marks
549   :type 'character)
550
551 (defcustom gnus-killed-mark ?K
552   "*Mark used for killed articles."
553   :group 'gnus-summary-marks
554   :type 'character)
555
556 (defcustom gnus-spam-mark ?$
557   "*Mark used for spam articles."
558   :version "22.1"
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-kill-file-mark ?X
563   "*Mark used for articles killed by kill files."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-low-score-mark ?Y
568   "*Mark used for articles with a low score."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-catchup-mark ?C
573   "*Mark used for articles that are caught up."
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-replied-mark ?A
578   "*Mark used for articles that have been replied to."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-forwarded-mark ?F
583   "*Mark used for articles that have been forwarded."
584   :version "22.1"
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-recent-mark ?N
589   "*Mark used for articles that are recent."
590   :version "22.1"
591   :group 'gnus-summary-marks
592   :type 'character)
593
594 (defcustom gnus-cached-mark ?*
595   "*Mark used for articles that are in the cache."
596   :group 'gnus-summary-marks
597   :type 'character)
598
599 (defcustom gnus-saved-mark ?S
600   "*Mark used for articles that have been saved."
601   :group 'gnus-summary-marks
602   :type 'character)
603
604 (defcustom gnus-unseen-mark ?.
605   "*Mark used for articles that haven't been seen."
606   :version "22.1"
607   :group 'gnus-summary-marks
608   :type 'character)
609
610 (defcustom gnus-no-mark ?               ;Whitespace
611   "*Mark used for articles that have no other secondary mark."
612   :version "22.1"
613   :group 'gnus-summary-marks
614   :type 'character)
615
616 (defcustom gnus-ancient-mark ?O
617   "*Mark used for ancient articles."
618   :group 'gnus-summary-marks
619   :type 'character)
620
621 (defcustom gnus-sparse-mark ?Q
622   "*Mark used for sparsely reffed articles."
623   :group 'gnus-summary-marks
624   :type 'character)
625
626 (defcustom gnus-canceled-mark ?G
627   "*Mark used for canceled articles."
628   :group 'gnus-summary-marks
629   :type 'character)
630
631 (defcustom gnus-duplicate-mark ?M
632   "*Mark used for duplicate articles."
633   :group 'gnus-summary-marks
634   :type 'character)
635
636 (defcustom gnus-undownloaded-mark ?-
637   "*Mark used for articles that weren't downloaded."
638   :version "22.1"
639   :group 'gnus-summary-marks
640   :type 'character)
641
642 (defcustom gnus-downloaded-mark ?+
643   "*Mark used for articles that were downloaded."
644   :group 'gnus-summary-marks
645   :type 'character)
646
647 (defcustom gnus-downloadable-mark ?%
648   "*Mark used for articles that are to be downloaded."
649   :group 'gnus-summary-marks
650   :type 'character)
651
652 (defcustom gnus-unsendable-mark ?=
653   "*Mark used for articles that won't be sent."
654   :group 'gnus-summary-marks
655   :type 'character)
656
657 (defcustom gnus-score-over-mark ?+
658   "*Score mark used for articles with high scores."
659   :group 'gnus-summary-marks
660   :type 'character)
661
662 (defcustom gnus-score-below-mark ?-
663   "*Score mark used for articles with low scores."
664   :group 'gnus-summary-marks
665   :type 'character)
666
667 (defcustom gnus-empty-thread-mark ?     ;Whitespace
668   "*There is no thread under the article."
669   :group 'gnus-summary-marks
670   :type 'character)
671
672 (defcustom gnus-not-empty-thread-mark ?=
673   "*There is a thread under the article."
674   :group 'gnus-summary-marks
675   :type 'character)
676
677 (defcustom gnus-view-pseudo-asynchronously nil
678   "*If non-nil, Gnus will view pseudo-articles asynchronously."
679   :group 'gnus-extract-view
680   :type 'boolean)
681
682 (defcustom gnus-auto-expirable-marks
683   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
684         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
685         gnus-duplicate-mark)
686   "*The list of marks converted into expiration if a group is auto-expirable."
687   :version "24.1"
688   :group 'gnus-summary
689   :type '(repeat character))
690
691 (defcustom gnus-inhibit-user-auto-expire t
692   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
693   :version "21.1"
694   :group 'gnus-summary
695   :type 'boolean)
696
697 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
698   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
699 If nil, the expirable marks will be unchanged except that the marks
700 will be removed when copying or moving articles to a group that has
701 not turned auto-expire on.  If non-nil, articles that have been read
702 will be marked as expirable when being copied or moved to a group in
703 which auto-expire is turned on."
704   :version "23.2"
705   :type 'boolean
706   :group 'gnus-summary-marks)
707
708 (defcustom gnus-view-pseudos nil
709   "*If `automatic', pseudo-articles will be viewed automatically.
710 If `not-confirm', pseudos will be viewed automatically, and the user
711 will not be asked to confirm the command."
712   :group 'gnus-extract-view
713   :type '(choice (const :tag "off" nil)
714                  (const automatic)
715                  (const not-confirm)))
716
717 (defcustom gnus-view-pseudos-separately t
718   "*If non-nil, one pseudo-article will be created for each file to be viewed.
719 If nil, all files that use the same viewing command will be given as a
720 list of parameters to that command."
721   :group 'gnus-extract-view
722   :type 'boolean)
723
724 (defcustom gnus-insert-pseudo-articles t
725   "*If non-nil, insert pseudo-articles when decoding articles."
726   :group 'gnus-extract-view
727   :type 'boolean)
728
729 (defcustom gnus-summary-dummy-line-format
730   "   %(:                             :%) %S\n"
731   "*The format specification for the dummy roots in the summary buffer.
732 It works along the same lines as a normal formatting string,
733 with some simple extensions.
734
735 %S  The subject
736
737 General format specifiers can also be used.
738 See `(gnus)Formatting Variables'."
739   :link '(custom-manual "(gnus)Formatting Variables")
740   :group 'gnus-threading
741   :type 'string)
742
743 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
744   "*The format specification for the summary mode line.
745 It works along the same lines as a normal formatting string,
746 with some simple extensions:
747
748 %G  Group name
749 %p  Unprefixed group name
750 %A  Current article number
751 %z  Current article score
752 %V  Gnus version
753 %U  Number of unread articles in the group
754 %e  Number of unselected articles in the group
755 %Z  A string with unread/unselected article counts
756 %g  Shortish group name
757 %S  Subject of the current article
758 %u  User-defined spec
759 %s  Current score file name
760 %d  Number of dormant articles
761 %r  Number of articles that have been marked as read in this session
762 %E  Number of articles expunged by the score files"
763   :group 'gnus-summary-format
764   :type 'string)
765
766 (defcustom gnus-list-identifiers nil
767   "Regexp that matches list identifiers to be removed from subject.
768 This can also be a list of regexps."
769   :version "21.1"
770   :group 'gnus-summary-format
771   :group 'gnus-article-hiding
772   :type '(choice (const :tag "none" nil)
773                  (regexp :value ".*")
774                  (repeat :value (".*") regexp)))
775
776 (defcustom gnus-summary-mark-below 0
777   "*Mark all articles with a score below this variable as read.
778 This variable is local to each summary buffer and usually set by the
779 score file."
780   :group 'gnus-score-default
781   :type 'integer)
782
783 (defun gnus-widget-reversible-match (widget value)
784   "Ignoring WIDGET, convert VALUE to internal form.
785 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
786   ;; (debug value)
787   (or (symbolp value)
788       (and (listp value)
789            (eq (length value) 2)
790            (eq (nth 0 value) 'not)
791            (symbolp (nth 1 value)))))
792
793 (defun gnus-widget-reversible-to-internal (widget value)
794   "Ignoring WIDGET, convert VALUE to internal form.
795 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
796 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
797   ;; (debug value)
798   (if (atom value)
799       (list value nil)
800     (list (nth 1 value) t)))
801
802 (defun gnus-widget-reversible-to-external (widget value)
803   "Ignoring WIDGET, convert VALUE to external form.
804 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
805 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
806   ;; (debug value)
807   (if (nth 1 value)
808       (list 'not (nth 0 value))
809     (nth 0 value)))
810
811 (define-widget 'gnus-widget-reversible 'group
812   "A `group' that convert values."
813   :match 'gnus-widget-reversible-match
814   :value-to-internal 'gnus-widget-reversible-to-internal
815   :value-to-external 'gnus-widget-reversible-to-external)
816
817 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
818   "*List of functions used for sorting articles in the summary buffer.
819
820 Each function takes two articles and returns non-nil if the first
821 article should be sorted before the other.  If you use more than one
822 function, the primary sort function should be the last.  You should
823 probably always include `gnus-article-sort-by-number' in the list of
824 sorting functions -- preferably first.  Also note that sorting by date
825 is often much slower than sorting by number, and the sorting order is
826 very similar.  (Sorting by date means sorting by the time the message
827 was sent, sorting by number means sorting by arrival time.)
828
829 Each item can also be a list `(not F)' where F is a function;
830 this reverses the sort order.
831
832 Ready-made functions include `gnus-article-sort-by-number',
833 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
834 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
835 and `gnus-article-sort-by-score'.
836
837 When threading is turned on, the variable `gnus-thread-sort-functions'
838 controls how articles are sorted."
839   :group 'gnus-summary-sort
840   :type '(repeat (gnus-widget-reversible
841                   (choice (function-item gnus-article-sort-by-number)
842                           (function-item gnus-article-sort-by-author)
843                           (function-item gnus-article-sort-by-subject)
844                           (function-item gnus-article-sort-by-date)
845                           (function-item gnus-article-sort-by-score)
846                           (function-item gnus-article-sort-by-random)
847                           (function :tag "other"))
848                   (boolean :tag "Reverse order"))))
849
850
851 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
852   "*List of functions used for sorting threads in the summary buffer.
853 By default, threads are sorted by article number.
854
855 Each function takes two threads and returns non-nil if the first
856 thread should be sorted before the other.  If you use more than one
857 function, the primary sort function should be the last.  You should
858 probably always include `gnus-thread-sort-by-number' in the list of
859 sorting functions -- preferably first.  Also note that sorting by date
860 is often much slower than sorting by number, and the sorting order is
861 very similar.  (Sorting by date means sorting by the time the message
862 was sent, sorting by number means sorting by arrival time.)
863
864 Each list item can also be a list `(not F)' where F is a
865 function; this specifies reversed sort order.
866
867 Ready-made functions include `gnus-thread-sort-by-number',
868 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
869 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
870 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
871 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
872 and `gnus-thread-sort-by-total-score' (see
873 `gnus-thread-score-function').
874
875 When threading is turned off, the variable
876 `gnus-article-sort-functions' controls how articles are sorted."
877   :group 'gnus-summary-sort
878   :type '(repeat
879           (gnus-widget-reversible
880            (choice (function-item gnus-thread-sort-by-number)
881                    (function-item gnus-thread-sort-by-author)
882                    (function-item gnus-thread-sort-by-recipient)
883                    (function-item gnus-thread-sort-by-subject)
884                    (function-item gnus-thread-sort-by-date)
885                    (function-item gnus-thread-sort-by-score)
886                    (function-item gnus-thread-sort-by-most-recent-number)
887                    (function-item gnus-thread-sort-by-most-recent-date)
888                    (function-item gnus-thread-sort-by-random)
889                    (function-item gnus-thread-sort-by-total-score)
890                    (function :tag "other"))
891            (boolean :tag "Reverse order"))))
892
893 (defcustom gnus-thread-score-function '+
894   "*Function used for calculating the total score of a thread.
895
896 The function is called with the scores of the article and each
897 subthread and should then return the score of the thread.
898
899 Some functions you can use are `+', `max', or `min'."
900   :group 'gnus-summary-sort
901   :type 'function)
902
903 (defcustom gnus-summary-expunge-below nil
904   "All articles that have a score less than this variable will be expunged.
905 This variable is local to the summary buffers."
906   :group 'gnus-score-default
907   :type '(choice (const :tag "off" nil)
908                  integer))
909
910 (defcustom gnus-thread-expunge-below nil
911   "All threads that have a total score less than this variable will be expunged.
912 See `gnus-thread-score-function' for en explanation of what a
913 \"thread score\" is.
914
915 This variable is local to the summary buffers."
916   :group 'gnus-threading
917   :group 'gnus-score-default
918   :type '(choice (const :tag "off" nil)
919                  integer))
920
921 (defcustom gnus-summary-mode-hook nil
922   "*A hook for Gnus summary mode.
923 This hook is run before any variables are set in the summary buffer."
924   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
925   :group 'gnus-summary-various
926   :type 'hook)
927
928 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
929 (when (featurep 'xemacs)
930   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
931   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
932   (add-hook 'gnus-summary-mode-hook
933             'gnus-xmas-switch-horizontal-scrollbar-off))
934
935 (defcustom gnus-summary-menu-hook nil
936   "*Hook run after the creation of the summary mode menu."
937   :group 'gnus-summary-visual
938   :type 'hook)
939
940 (defcustom gnus-summary-exit-hook nil
941   "*A hook called on exit from the summary buffer.
942 It will be called with point in the group buffer."
943   :group 'gnus-summary-exit
944   :type 'hook)
945
946 (defcustom gnus-summary-prepare-hook nil
947   "*A hook called after the summary buffer has been generated.
948 If you want to modify the summary buffer, you can use this hook."
949   :group 'gnus-summary-various
950   :type 'hook)
951
952 (defcustom gnus-summary-prepared-hook nil
953   "*A hook called as the last thing after the summary buffer has been generated."
954   :group 'gnus-summary-various
955   :type 'hook)
956
957 (defcustom gnus-summary-generate-hook nil
958   "*A hook run just before generating the summary buffer.
959 This hook is commonly used to customize threading variables and the
960 like."
961   :group 'gnus-summary-various
962   :type 'hook)
963
964 (defcustom gnus-select-group-hook nil
965   "*A hook called when a newsgroup is selected.
966
967 If you'd like to simplify subjects like the
968 `gnus-summary-next-same-subject' command does, you can use the
969 following hook:
970
971  (add-hook gnus-select-group-hook
972            (lambda ()
973              (mapcar (lambda (header)
974                        (mail-header-set-subject
975                         header
976                         (gnus-simplify-subject
977                          (mail-header-subject header) 're-only)))
978                      gnus-newsgroup-headers)))"
979   :group 'gnus-group-select
980   :type 'hook)
981
982 (defcustom gnus-select-article-hook nil
983   "*A hook called when an article is selected."
984   :group 'gnus-summary-choose
985   :options '(gnus-agent-fetch-selected-article)
986   :type 'hook)
987
988 (defcustom gnus-visual-mark-article-hook
989   (list 'gnus-highlight-selected-summary)
990   "*Hook run after selecting an article in the summary buffer.
991 It is meant to be used for highlighting the article in some way.  It
992 is not run if `gnus-visual' is nil."
993   :group 'gnus-summary-visual
994   :type 'hook)
995
996 (defcustom gnus-parse-headers-hook nil
997   "*A hook called before parsing the headers."
998   :group 'gnus-various
999   :type 'hook)
1000
1001 (defcustom gnus-exit-group-hook nil
1002   "*A hook called when exiting summary mode.
1003 This hook is not called from the non-updating exit commands like `Q'."
1004   :group 'gnus-various
1005   :type 'hook)
1006
1007 (defcustom gnus-summary-update-hook nil
1008   "*A hook called when a summary line is changed.
1009 The hook will not be called if `gnus-visual' is nil.
1010
1011 The default function `gnus-summary-highlight-line' will
1012 highlight the line according to the `gnus-summary-highlight'
1013 variable."
1014   :group 'gnus-summary-visual
1015   :type 'hook)
1016
1017 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1018   "*A hook called when an article is selected for the first time.
1019 The hook is intended to mark an article as read (or unread)
1020 automatically when it is selected."
1021   :group 'gnus-summary-choose
1022   :type 'hook)
1023
1024 (defcustom gnus-group-no-more-groups-hook nil
1025   "*A hook run when returning to group mode having no more (unread) groups."
1026   :group 'gnus-group-select
1027   :type 'hook)
1028
1029 (defcustom gnus-ps-print-hook nil
1030   "*A hook run before ps-printing something from Gnus."
1031   :group 'gnus-summary
1032   :type 'hook)
1033
1034 (defcustom gnus-summary-article-move-hook nil
1035   "*A hook called after an article is moved, copied, respooled, or crossposted."
1036   :version "22.1"
1037   :group 'gnus-summary
1038   :type 'hook)
1039
1040 (defcustom gnus-summary-article-delete-hook nil
1041   "*A hook called after an article is deleted."
1042   :version "22.1"
1043   :group 'gnus-summary
1044   :type 'hook)
1045
1046 (defcustom gnus-summary-article-expire-hook nil
1047   "*A hook called after an article is expired."
1048   :version "22.1"
1049   :group 'gnus-summary
1050   :type 'hook)
1051
1052 (defcustom gnus-summary-display-arrow
1053   (and (fboundp 'display-graphic-p)
1054        (display-graphic-p))
1055   "*If non-nil, display an arrow highlighting the current article."
1056   :version "22.1"
1057   :group 'gnus-summary
1058   :type 'boolean)
1059
1060 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1061   "Face used for highlighting the current article in the summary buffer."
1062   :group 'gnus-summary-visual
1063   :type 'face)
1064
1065 (defvar gnus-tmp-downloaded nil)
1066
1067 (defcustom gnus-summary-highlight
1068   '(((eq mark gnus-canceled-mark)
1069      . gnus-summary-cancelled)
1070     ((and uncached (> score default-high))
1071      . gnus-summary-high-undownloaded)
1072     ((and uncached (< score default-low))
1073      . gnus-summary-low-undownloaded)
1074     (uncached
1075      . gnus-summary-normal-undownloaded)
1076     ((and (> score default-high)
1077           (or (eq mark gnus-dormant-mark)
1078               (eq mark gnus-ticked-mark)))
1079      . gnus-summary-high-ticked)
1080     ((and (< score default-low)
1081           (or (eq mark gnus-dormant-mark)
1082               (eq mark gnus-ticked-mark)))
1083      . gnus-summary-low-ticked)
1084     ((or (eq mark gnus-dormant-mark)
1085          (eq mark gnus-ticked-mark))
1086      . gnus-summary-normal-ticked)
1087     ((and (> score default-high) (eq mark gnus-ancient-mark))
1088      . gnus-summary-high-ancient)
1089     ((and (< score default-low) (eq mark gnus-ancient-mark))
1090      . gnus-summary-low-ancient)
1091     ((eq mark gnus-ancient-mark)
1092      . gnus-summary-normal-ancient)
1093     ((and (> score default-high) (eq mark gnus-unread-mark))
1094      . gnus-summary-high-unread)
1095     ((and (< score default-low) (eq mark gnus-unread-mark))
1096      . gnus-summary-low-unread)
1097     ((eq mark gnus-unread-mark)
1098      . gnus-summary-normal-unread)
1099     ((> score default-high)
1100      . gnus-summary-high-read)
1101     ((< score default-low)
1102      . gnus-summary-low-read)
1103     (t
1104      . gnus-summary-normal-read))
1105   "*Controls the highlighting of summary buffer lines.
1106
1107 A list of (FORM . FACE) pairs.  When deciding how a particular
1108 summary line should be displayed, each form is evaluated.  The content
1109 of the face field after the first true form is used.  You can change
1110 how those summary lines are displayed, by editing the face field.
1111
1112 You can use the following variables in the FORM field.
1113
1114 score:        The article's score.
1115 default:      The default article score.
1116 default-high: The default score for high scored articles.
1117 default-low:  The default score for low scored articles.
1118 below:        The score below which articles are automatically marked as read.
1119 mark:         The article's mark.
1120 uncached:     Non-nil if the article is uncached."
1121   :group 'gnus-summary-visual
1122   :type '(repeat (cons (sexp :tag "Form" nil)
1123                        face)))
1124 (put 'gnus-summary-highlight 'risky-local-variable t)
1125
1126 (defcustom gnus-alter-header-function nil
1127   "Function called to allow alteration of article header structures.
1128 The function is called with one parameter, the article header vector,
1129 which it may alter in any way."
1130   :type '(choice (const :tag "None" nil)
1131                  function)
1132   :group 'gnus-summary)
1133
1134 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1135   "Function used to decode a string with encoded words.")
1136
1137 (defvar gnus-decode-encoded-address-function
1138   'mail-decode-encoded-address-string
1139   "Function used to decode addresses with encoded words.")
1140
1141 (defcustom gnus-extra-headers '(To Cc Keywords Gcc Newsgroups)
1142   "*Extra headers to parse."
1143   :version "24.1"                       ; added Cc Keywords Gcc
1144   :group 'gnus-summary
1145   :type '(repeat symbol))
1146
1147 (defcustom gnus-ignored-from-addresses
1148   (and user-mail-address
1149        (not (string= user-mail-address ""))
1150        (regexp-quote user-mail-address))
1151   "*From headers that may be suppressed in favor of To headers.
1152 This can be a regexp or a list of regexps."
1153   :version "21.1"
1154   :group 'gnus-summary
1155   :type '(choice regexp
1156                  (repeat :tag "Regexp List" regexp)))
1157
1158 (defsubst gnus-ignored-from-addresses ()
1159   (gmm-regexp-concat gnus-ignored-from-addresses))
1160
1161 (defcustom gnus-summary-to-prefix "-> "
1162   "*String prefixed to the To field in the summary line when
1163 using `gnus-ignored-from-addresses'."
1164   :version "22.1"
1165   :group 'gnus-summary
1166   :type 'string)
1167
1168 (defcustom gnus-summary-newsgroup-prefix "=> "
1169   "*String prefixed to the Newsgroup field in the summary
1170 line when using the option `gnus-ignored-from-addresses'."
1171   :version "22.1"
1172   :group 'gnus-summary
1173   :type 'string)
1174
1175 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1176   "List of charsets that should be ignored.
1177 When these charsets are used in the \"charset\" parameter, the
1178 default charset will be used instead."
1179   :version "21.1"
1180   :type '(repeat symbol)
1181   :group 'gnus-charset)
1182
1183 (defcustom gnus-newsgroup-maximum-articles nil
1184   "The maximum number of articles a newsgroup.
1185 If this is a number, old articles in a newsgroup exceeding this number
1186 are silently ignored.  If it is nil, no article is ignored.  Note that
1187 setting this variable to a number might prevent you from reading very
1188 old articles."
1189   :group 'gnus-group-select
1190   :version "22.2"
1191   :type '(choice (const :tag "No limit" nil)
1192                  integer))
1193
1194 (gnus-define-group-parameter
1195  ignored-charsets
1196  :type list
1197  :function-document
1198  "Return the ignored charsets of GROUP."
1199  :variable gnus-group-ignored-charsets-alist
1200  :variable-default
1201  '(("alt\\.chinese\\.text" iso-8859-1))
1202  :variable-document
1203  "Alist of regexps (to match group names) and charsets that should be ignored.
1204 When these charsets are used in the \"charset\" parameter, the
1205 default charset will be used instead."
1206  :variable-group gnus-charset
1207  :variable-type '(repeat (cons (regexp :tag "Group")
1208                                (repeat symbol)))
1209  :parameter-type '(choice :tag "Ignored charsets"
1210                           :value nil
1211                           (repeat (symbol)))
1212  :parameter-document       "\
1213 List of charsets that should be ignored.
1214
1215 When these charsets are used in the \"charset\" parameter, the
1216 default charset will be used instead.")
1217
1218 (defcustom gnus-group-highlight-words-alist nil
1219   "Alist of group regexps and highlight regexps.
1220 This variable uses the same syntax as `gnus-emphasis-alist'."
1221   :version "21.1"
1222   :type '(repeat (cons (regexp :tag "Group")
1223                        (repeat (list (regexp :tag "Highlight regexp")
1224                                      (number :tag "Group for entire word" 0)
1225                                      (number :tag "Group for displayed part" 0)
1226                                      (symbol :tag "Face"
1227                                              gnus-emphasis-highlight-words)))))
1228   :group 'gnus-summary-visual)
1229
1230 (defcustom gnus-summary-show-article-charset-alist
1231   nil
1232   "Alist of number and charset.
1233 The article will be shown with the charset corresponding to the
1234 numbered argument.
1235 For example: ((1 . cn-gb-2312) (2 . big5))."
1236   :version "21.1"
1237   :type '(repeat (cons (number :tag "Argument" 1)
1238                        (symbol :tag "Charset")))
1239   :group 'gnus-charset)
1240
1241 (defcustom gnus-preserve-marks t
1242   "Whether marks are preserved when moving, copying and respooling messages."
1243   :version "21.1"
1244   :type 'boolean
1245   :group 'gnus-summary-marks)
1246
1247 (defcustom gnus-alter-articles-to-read-function nil
1248   "Function to be called to alter the list of articles to be selected."
1249   :type '(choice (const nil) function)
1250   :group 'gnus-summary)
1251
1252 (defcustom gnus-orphan-score nil
1253   "*All orphans get this score added.  Set in the score file."
1254   :group 'gnus-score-default
1255   :type '(choice (const nil)
1256                  integer))
1257
1258 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1259   "*A regexp to match MIME parts when saving multiple parts of a
1260 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1261 This regexp will be used by default when prompting the user for which
1262 type of files to save."
1263   :group 'gnus-summary
1264   :type 'regexp)
1265
1266 (defcustom gnus-read-all-available-headers nil
1267   "Whether Gnus should parse all headers made available to it.
1268 This is mostly relevant for slow back ends where the user may
1269 wish to widen the summary buffer to include all headers
1270 that were fetched."
1271   :version "22.1"
1272   :group 'gnus-summary
1273   :type '(choice boolean regexp))
1274
1275 (defcustom gnus-summary-pipe-output-default-command nil
1276   "Command (and optional arguments) used to pipe article to subprocess.
1277 This will be used as the default command if it is non-nil.  The value
1278 will be updated if you modify it when executing the command
1279 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1280   :version "23.1" ;; No Gnus
1281   :group 'gnus-summary
1282   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1283
1284 (defcustom gnus-summary-muttprint-program "muttprint"
1285   "Command (and optional arguments) used to run Muttprint.
1286 The value will be updated if you modify it when executing the command
1287 `gnus-summary-muttprint'."
1288   :version "22.1"
1289   :group 'gnus-summary
1290   :type 'string)
1291
1292 (defcustom gnus-article-loose-mime t
1293   "If non-nil, don't require MIME-Version header.
1294 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1295 supply the MIME-Version header or deliberately strip it from the mail.
1296 If non-nil (the default), Gnus will treat some articles as MIME
1297 even if the MIME-Version header is missing."
1298   :version "22.1"
1299   :type 'boolean
1300   :group 'gnus-article-mime)
1301
1302 (defcustom gnus-article-emulate-mime t
1303   "If non-nil, use MIME emulation for uuencode and the like.
1304 This means that Gnus will search message bodies for text that look
1305 like uuencoded bits, yEncoded bits, and so on, and present that using
1306 the normal Gnus MIME machinery."
1307   :version "22.1"
1308   :type 'boolean
1309   :group 'gnus-article-mime)
1310
1311 ;;; Internal variables
1312
1313 (defvar gnus-summary-display-cache nil)
1314 (defvar gnus-article-mime-handles nil)
1315 (defvar gnus-article-decoded-p nil)
1316 (defvar gnus-article-charset nil)
1317 (defvar gnus-article-ignored-charsets nil)
1318 (defvar gnus-scores-exclude-files nil)
1319 (defvar gnus-page-broken nil)
1320
1321 (defvar gnus-original-article nil)
1322 (defvar gnus-article-internal-prepare-hook nil)
1323 (defvar gnus-newsgroup-process-stack nil)
1324
1325 (defvar gnus-thread-indent-array nil)
1326 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1327 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1328   "Function called to sort the articles within a thread after it has been gathered together.")
1329
1330 (defvar gnus-summary-save-parts-type-history nil)
1331 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1332
1333 ;; Avoid highlighting in kill files.
1334 (defvar gnus-summary-inhibit-highlight nil)
1335 (defvar gnus-newsgroup-selected-overlay nil)
1336 (defvar gnus-inhibit-limiting nil)
1337 (defvar gnus-newsgroup-adaptive-score-file nil)
1338 (defvar gnus-current-score-file nil)
1339 (defvar gnus-current-move-group nil)
1340 (defvar gnus-current-copy-group nil)
1341 (defvar gnus-current-crosspost-group nil)
1342 (defvar gnus-newsgroup-display nil)
1343
1344 (defvar gnus-newsgroup-dependencies nil)
1345 (defvar gnus-newsgroup-adaptive nil)
1346 (defvar gnus-summary-display-article-function nil)
1347 (defvar gnus-summary-highlight-line-function nil
1348   "Function called after highlighting a summary line.")
1349
1350 (defvar gnus-summary-line-format-alist
1351   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1352     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1353     (?s gnus-tmp-subject-or-nil ?s)
1354     (?n gnus-tmp-name ?s)
1355     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1356         ?s)
1357     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1358             gnus-tmp-from) ?s)
1359     (?F gnus-tmp-from ?s)
1360     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1361     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1362     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1363     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1364     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1365     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1366     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1367     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1368     (?L gnus-tmp-lines ?s)
1369     (?Z (or (nnir-article-rsv (mail-header-number gnus-tmp-header))
1370             0) ?d)
1371     (?G (or (nnir-article-group (mail-header-number gnus-tmp-header))
1372             "") ?s)
1373     (?g (or (gnus-group-short-name
1374              (nnir-article-group (mail-header-number gnus-tmp-header)))
1375             "") ?s)
1376     (?O gnus-tmp-downloaded ?c)
1377     (?I gnus-tmp-indentation ?s)
1378     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1379     (?R gnus-tmp-replied ?c)
1380     (?\[ gnus-tmp-opening-bracket ?c)
1381     (?\] gnus-tmp-closing-bracket ?c)
1382     (?\> (make-string gnus-tmp-level ? ) ?s)
1383     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1384     (?i gnus-tmp-score ?d)
1385     (?z gnus-tmp-score-char ?c)
1386     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1387     (?U gnus-tmp-unread ?c)
1388     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1389         ?s)
1390     (?t (gnus-summary-number-of-articles-in-thread
1391          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1392         ?d)
1393     (?e (gnus-summary-number-of-articles-in-thread
1394          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1395         ?c)
1396     (?u gnus-tmp-user-defined ?s)
1397     (?P (gnus-pick-line-number) ?d)
1398     (?B gnus-tmp-thread-tree-header-string ?s)
1399     (user-date (gnus-user-date
1400                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1401   "An alist of format specifications that can appear in summary lines.
1402 These are paired with what variables they correspond with, along with
1403 the type of the variable (string, integer, character, etc).")
1404
1405 (defvar gnus-summary-dummy-line-format-alist
1406   `((?S gnus-tmp-subject ?s)
1407     (?N gnus-tmp-number ?d)
1408     (?u gnus-tmp-user-defined ?s)))
1409
1410 (defvar gnus-summary-mode-line-format-alist
1411   `((?G gnus-tmp-group-name ?s)
1412     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1413     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1414     (?A gnus-tmp-article-number ?d)
1415     (?Z gnus-tmp-unread-and-unselected ?s)
1416     (?V gnus-version ?s)
1417     (?U gnus-tmp-unread-and-unticked ?d)
1418     (?S gnus-tmp-subject ?s)
1419     (?e gnus-tmp-unselected ?d)
1420     (?u gnus-tmp-user-defined ?s)
1421     (?d (length gnus-newsgroup-dormant) ?d)
1422     (?t (length gnus-newsgroup-marked) ?d)
1423     (?h (length gnus-newsgroup-spam-marked) ?d)
1424     (?r (length gnus-newsgroup-reads) ?d)
1425     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1426     (?E gnus-newsgroup-expunged-tally ?d)
1427     (?s (gnus-current-score-file-nondirectory) ?s)))
1428
1429 ;; This is here rather than in gnus-art for compilation reasons.
1430 (defvar gnus-article-mode-line-format-alist
1431   (nconc '((?w (gnus-article-wash-status) ?s)
1432            (?m (gnus-article-mime-part-status) ?s))
1433          gnus-summary-mode-line-format-alist))
1434
1435 (defvar gnus-last-search-regexp nil
1436   "Default regexp for article search command.")
1437
1438 (defvar gnus-last-shell-command nil
1439   "Default shell command on article.")
1440
1441 (defvar gnus-newsgroup-agentized nil
1442   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1443 (defvar gnus-newsgroup-begin nil)
1444 (defvar gnus-newsgroup-end nil)
1445 (defvar gnus-newsgroup-last-rmail nil)
1446 (defvar gnus-newsgroup-last-mail nil)
1447 (defvar gnus-newsgroup-last-folder nil)
1448 (defvar gnus-newsgroup-last-file nil)
1449 (defvar gnus-newsgroup-last-directory nil)
1450 (defvar gnus-newsgroup-auto-expire nil)
1451 (defvar gnus-newsgroup-active nil)
1452 (defvar gnus-newsgroup-highest nil)
1453
1454 (defvar gnus-newsgroup-data nil)
1455 (defvar gnus-newsgroup-data-reverse nil)
1456 (defvar gnus-newsgroup-limit nil)
1457 (defvar gnus-newsgroup-limits nil)
1458 (defvar gnus-summary-use-undownloaded-faces nil)
1459
1460 (defvar gnus-newsgroup-unreads nil
1461   "Sorted list of unread articles in the current newsgroup.")
1462
1463 (defvar gnus-newsgroup-unselected nil
1464   "Sorted list of unselected unread articles in the current newsgroup.")
1465
1466 (defvar gnus-newsgroup-reads nil
1467   "Alist of read articles and article marks in the current newsgroup.")
1468
1469 (defvar gnus-newsgroup-expunged-tally nil)
1470
1471 (defvar gnus-newsgroup-marked nil
1472   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1473
1474 (defvar gnus-newsgroup-spam-marked nil
1475   "List of ranges of articles that have been marked as spam.")
1476
1477 (defvar gnus-newsgroup-killed nil
1478   "List of ranges of articles that have been through the scoring process.")
1479
1480 (defvar gnus-newsgroup-cached nil
1481   "Sorted list of articles that come from the article cache.")
1482
1483 (defvar gnus-newsgroup-saved nil
1484   "List of articles that have been saved.")
1485
1486 (defvar gnus-newsgroup-kill-headers nil)
1487
1488 (defvar gnus-newsgroup-replied nil
1489   "List of articles that have been replied to in the current newsgroup.")
1490
1491 (defvar gnus-newsgroup-forwarded nil
1492   "List of articles that have been forwarded in the current newsgroup.")
1493
1494 (defvar gnus-newsgroup-expirable nil
1495   "Sorted list of articles in the current newsgroup that can be expired.")
1496
1497 (defvar gnus-newsgroup-processable nil
1498   "List of articles in the current newsgroup that can be processed.")
1499
1500 (defvar gnus-newsgroup-downloadable nil
1501   "Sorted list of articles in the current newsgroup that can be processed.")
1502
1503 (defvar gnus-newsgroup-unfetched nil
1504   "Sorted list of articles in the current newsgroup whose headers have
1505 not been fetched into the agent.
1506
1507 This list will always be a subset of gnus-newsgroup-undownloaded.")
1508
1509 (defvar gnus-newsgroup-undownloaded nil
1510   "List of articles in the current newsgroup that haven't been downloaded.")
1511
1512 (defvar gnus-newsgroup-unsendable nil
1513   "List of articles in the current newsgroup that won't be sent.")
1514
1515 (defvar gnus-newsgroup-bookmarks nil
1516   "List of articles in the current newsgroup that have bookmarks.")
1517
1518 (defvar gnus-newsgroup-dormant nil
1519   "Sorted list of dormant articles in the current newsgroup.")
1520
1521 (defvar gnus-newsgroup-unseen nil
1522   "List of unseen articles in the current newsgroup.")
1523
1524 (defvar gnus-newsgroup-seen nil
1525   "Range of seen articles in the current newsgroup.")
1526
1527 (defvar gnus-newsgroup-unexist nil
1528   "Range of unexistent articles in the current newsgroup.")
1529
1530 (defvar gnus-newsgroup-articles nil
1531   "List of articles in the current newsgroup.")
1532
1533 (defvar gnus-newsgroup-scored nil
1534   "List of scored articles in the current newsgroup.")
1535
1536 (defvar gnus-newsgroup-headers nil
1537   "List of article headers in the current newsgroup.")
1538
1539 (defvar gnus-newsgroup-threads nil)
1540
1541 (defvar gnus-newsgroup-prepared nil
1542   "Whether the current group has been prepared properly.")
1543
1544 (defvar gnus-newsgroup-ancient nil
1545   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1546
1547 (defvar gnus-newsgroup-sparse nil)
1548
1549 (defvar gnus-current-article nil)
1550 (defvar gnus-article-current nil)
1551 (defvar gnus-current-headers nil)
1552 (defvar gnus-have-all-headers nil)
1553 (defvar gnus-last-article nil)
1554 (defvar gnus-newsgroup-history nil)
1555 (defvar gnus-newsgroup-charset nil)
1556 (defvar gnus-newsgroup-ephemeral-charset nil)
1557 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1558
1559 (defvar gnus-article-before-search nil)
1560
1561 (defvar gnus-summary-local-variables
1562   '(gnus-newsgroup-name
1563
1564     ;; Marks lists
1565     gnus-newsgroup-unreads
1566     gnus-newsgroup-unselected
1567     gnus-newsgroup-marked
1568     gnus-newsgroup-spam-marked
1569     gnus-newsgroup-reads
1570     gnus-newsgroup-saved
1571     gnus-newsgroup-replied
1572     gnus-newsgroup-forwarded
1573     gnus-newsgroup-expirable
1574     gnus-newsgroup-killed
1575     gnus-newsgroup-unseen
1576     gnus-newsgroup-seen
1577     gnus-newsgroup-unexist
1578     gnus-newsgroup-cached
1579     gnus-newsgroup-downloadable
1580     gnus-newsgroup-undownloaded
1581     gnus-newsgroup-unsendable
1582
1583     gnus-newsgroup-begin gnus-newsgroup-end
1584     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1585     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1586     gnus-newsgroup-last-directory
1587     gnus-newsgroup-auto-expire
1588     gnus-newsgroup-processable
1589     gnus-newsgroup-unfetched
1590     gnus-newsgroup-articles
1591     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1592     gnus-newsgroup-headers gnus-newsgroup-threads
1593     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1594     gnus-current-article gnus-current-headers gnus-have-all-headers
1595     gnus-last-article gnus-article-internal-prepare-hook
1596     (gnus-summary-article-delete-hook . global)
1597     (gnus-summary-article-move-hook . global)
1598     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1599     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1600     gnus-thread-expunge-below
1601     gnus-score-alist gnus-current-score-file
1602     (gnus-summary-expunge-below . global)
1603     (gnus-summary-mark-below . global)
1604     (gnus-orphan-score . global)
1605     gnus-newsgroup-active gnus-scores-exclude-files
1606     gnus-newsgroup-highest
1607     gnus-newsgroup-history gnus-newsgroup-ancient
1608     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1609     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1610     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1611     (gnus-newsgroup-expunged-tally . 0)
1612     gnus-cache-removable-articles
1613     gnus-newsgroup-data gnus-newsgroup-data-reverse
1614     gnus-newsgroup-limit gnus-newsgroup-limits
1615     gnus-newsgroup-charset gnus-newsgroup-display
1616     gnus-summary-use-undownloaded-faces)
1617   "Variables that are buffer-local to the summary buffers.")
1618
1619 (defvar gnus-newsgroup-variables nil
1620   "A list of variables that have separate values in different newsgroups.
1621 A list of newsgroup (summary buffer) local variables, or cons of
1622 variables and their default expressions to be evalled (when the default
1623 values are not nil), that should be made global while the summary buffer
1624 is active.
1625
1626 Note: The default expressions will be evaluated (using function `eval')
1627 before assignment to the local variable rather than just assigned to it.
1628 If the default expression is the symbol `global', that symbol will not
1629 be evaluated but the global value of the local variable will be used
1630 instead.
1631
1632 These variables can be used to set variables in the group parameters
1633 while still allowing them to affect operations done in other buffers.
1634 For example:
1635
1636 \(setq gnus-newsgroup-variables
1637      '(message-use-followup-to
1638        (gnus-visible-headers .
1639          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1640 ")
1641
1642 (eval-when-compile
1643   ;; Bind features so that require will believe that gnus-sum has
1644   ;; already been loaded (avoids infinite recursion)
1645   (let ((features (cons 'gnus-sum features)))
1646     (require 'gnus-art)))
1647
1648 ;; MIME stuff.
1649
1650 (defvar gnus-decode-encoded-word-methods
1651   '(mail-decode-encoded-word-string)
1652   "List of methods used to decode encoded words.
1653
1654 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1655 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1656 \(REGEXP . FUNCTION), FUNCTION will be applied only to the newsgroups
1657 whose names match REGEXP.
1658
1659 For example:
1660 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1661  mail-decode-encoded-word-string
1662  (\"chinese\" . rfc1843-decode-string))")
1663
1664 (defvar gnus-decode-encoded-word-methods-cache nil)
1665
1666 (defun gnus-multi-decode-encoded-word-string (string)
1667   "Apply the functions from `gnus-encoded-word-methods' that match."
1668   (unless (and gnus-decode-encoded-word-methods-cache
1669                (eq gnus-newsgroup-name
1670                    (car gnus-decode-encoded-word-methods-cache)))
1671     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1672     (dolist (method gnus-decode-encoded-word-methods)
1673       (if (symbolp method)
1674           (nconc gnus-decode-encoded-word-methods-cache (list method))
1675         (if (and gnus-newsgroup-name
1676                  (string-match (car method) gnus-newsgroup-name))
1677             (nconc gnus-decode-encoded-word-methods-cache
1678                    (list (cdr method)))))))
1679   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1680     (setq string (funcall method string))))
1681
1682 ;; Subject simplification.
1683
1684 (defun gnus-simplify-whitespace (str)
1685   "Remove excessive whitespace from STR."
1686   ;; Multiple spaces.
1687   (while (string-match "[ \t][ \t]+" str)
1688     (setq str (concat (substring str 0 (match-beginning 0))
1689                         " "
1690                         (substring str (match-end 0)))))
1691   ;; Leading spaces.
1692   (when (string-match "^[ \t]+" str)
1693     (setq str (substring str (match-end 0))))
1694   ;; Trailing spaces.
1695   (when (string-match "[ \t]+$" str)
1696     (setq str (substring str 0 (match-beginning 0))))
1697   str)
1698
1699 (defun gnus-simplify-all-whitespace (str)
1700   "Remove all whitespace from STR."
1701   (while (string-match "[ \t\n]+" str)
1702     (setq str (replace-match "" nil nil str)))
1703   str)
1704
1705 (defsubst gnus-simplify-subject-re (subject)
1706   "Remove \"Re:\" from subject lines."
1707   (if (string-match message-subject-re-regexp subject)
1708       (substring subject (match-end 0))
1709     subject))
1710
1711 (defun gnus-simplify-subject (subject &optional re-only)
1712   "Remove `Re:' and words in parentheses.
1713 If RE-ONLY is non-nil, strip leading `Re:'s only."
1714   (let ((case-fold-search t))           ;Ignore case.
1715     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1716     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1717       (setq subject (substring subject (match-end 0))))
1718     ;; Remove uninteresting prefixes.
1719     (when (and (not re-only)
1720                gnus-simplify-ignored-prefixes
1721                (string-match gnus-simplify-ignored-prefixes subject))
1722       (setq subject (substring subject (match-end 0))))
1723     ;; Remove words in parentheses from end.
1724     (unless re-only
1725       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1726         (setq subject (substring subject 0 (match-beginning 0)))))
1727     ;; Return subject string.
1728     subject))
1729
1730 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1731 ;; all whitespace.
1732 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1733   (goto-char (point-min))
1734   (while (re-search-forward regexp nil t)
1735     (replace-match (or newtext ""))))
1736
1737 (defun gnus-simplify-buffer-fuzzy (regexp)
1738   "Simplify string in the buffer fuzzily.
1739 The string in the accessible portion of the current buffer is simplified.
1740 It is assumed to be a single-line subject.
1741 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1742 matter is removed.  Additional things can be deleted by setting
1743 `gnus-simplify-subject-fuzzy-regexp'."
1744   (let ((case-fold-search t)
1745         (modified-tick))
1746     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1747
1748     (while (not (eq modified-tick (buffer-modified-tick)))
1749       (setq modified-tick (buffer-modified-tick))
1750       (cond
1751        ((listp regexp)
1752         (mapc 'gnus-simplify-buffer-fuzzy-step regexp))
1753        (regexp
1754         (gnus-simplify-buffer-fuzzy-step regexp)))
1755       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1756       (gnus-simplify-buffer-fuzzy-step
1757        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1758       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1759
1760     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1761     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1762     (gnus-simplify-buffer-fuzzy-step " $")
1763     (gnus-simplify-buffer-fuzzy-step "^ +")))
1764
1765 (defun gnus-simplify-subject-fuzzy (subject)
1766   "Simplify a subject string fuzzily.
1767 See `gnus-simplify-buffer-fuzzy' for details."
1768   (save-excursion
1769     (let ((regexp gnus-simplify-subject-fuzzy-regexp))
1770       (gnus-set-work-buffer)
1771       (let ((case-fold-search t))
1772         ;; Remove uninteresting prefixes.
1773         (when (and gnus-simplify-ignored-prefixes
1774                    (string-match gnus-simplify-ignored-prefixes subject))
1775           (setq subject (substring subject (match-end 0))))
1776         (insert subject)
1777         (inline (gnus-simplify-buffer-fuzzy regexp))
1778         (buffer-string)))))
1779
1780 (defsubst gnus-simplify-subject-fully (subject)
1781   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1782   (cond
1783    (gnus-simplify-subject-functions
1784     (gnus-map-function gnus-simplify-subject-functions subject))
1785    ((null gnus-summary-gather-subject-limit)
1786     (gnus-simplify-subject-re subject))
1787    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1788     (gnus-simplify-subject-fuzzy subject))
1789    ((numberp gnus-summary-gather-subject-limit)
1790     (truncate-string-to-width (gnus-simplify-subject-re subject)
1791                               gnus-summary-gather-subject-limit))
1792    (t
1793     subject)))
1794
1795 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1796   "Check whether two subjects are equal.
1797 If optional argument SIMPLE-FIRST is t, first argument is already
1798 simplified."
1799   (cond
1800    ((null simple-first)
1801     (equal (gnus-simplify-subject-fully s1)
1802            (gnus-simplify-subject-fully s2)))
1803    (t
1804     (equal s1
1805            (gnus-simplify-subject-fully s2)))))
1806
1807 (defun gnus-summary-bubble-group ()
1808   "Increase the score of the current group.
1809 This is a handy function to add to `gnus-summary-exit-hook' to
1810 increase the score of each group you read."
1811   (gnus-group-add-score gnus-newsgroup-name))
1812
1813 \f
1814 ;;;
1815 ;;; Gnus summary mode
1816 ;;;
1817
1818 (put 'gnus-summary-mode 'mode-class 'special)
1819
1820 (defvar gnus-article-commands-menu)
1821
1822 ;; Non-orthogonal keys
1823
1824 (gnus-define-keys gnus-summary-mode-map
1825   " " gnus-summary-next-page
1826   [?\S-\ ] gnus-summary-prev-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   "\t" gnus-summary-widget-forward
1920   [backtab] gnus-summary-widget-backward
1921   "t" gnus-summary-toggle-header
1922   "g" gnus-summary-show-article
1923   "l" gnus-summary-goto-last-article
1924   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1925   "\C-d" gnus-summary-enter-digest-group
1926   "\M-\C-d" gnus-summary-read-document
1927   "\M-\C-e" gnus-summary-edit-parameters
1928   "\M-\C-a" gnus-summary-customize-parameters
1929   "\C-c\C-b" gnus-bug
1930   "*" gnus-cache-enter-article
1931   "\M-*" gnus-cache-remove-article
1932   "\M-&" gnus-summary-universal-argument
1933   "\C-l" gnus-recenter
1934   "I" gnus-summary-increase-score
1935   "L" gnus-summary-lower-score
1936   "\M-i" gnus-symbolic-argument
1937   "h" gnus-summary-select-article-buffer
1938
1939   "b" gnus-article-view-part
1940   "\M-t" gnus-summary-toggle-display-buttonized
1941
1942   "V" gnus-summary-score-map
1943   "X" gnus-uu-extract-map
1944   "S" gnus-summary-send-map)
1945
1946 ;; Sort of orthogonal keymap
1947 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1948   "t" gnus-summary-tick-article-forward
1949   "!" gnus-summary-tick-article-forward
1950   "d" gnus-summary-mark-as-read-forward
1951   "r" gnus-summary-mark-as-read-forward
1952   "c" gnus-summary-clear-mark-forward
1953   " " gnus-summary-clear-mark-forward
1954   "e" gnus-summary-mark-as-expirable
1955   "x" gnus-summary-mark-as-expirable
1956   "?" gnus-summary-mark-as-dormant
1957   "b" gnus-summary-set-bookmark
1958   "B" gnus-summary-remove-bookmark
1959   "#" gnus-summary-mark-as-processable
1960   "\M-#" gnus-summary-unmark-as-processable
1961   "S" gnus-summary-limit-include-expunged
1962   "C" gnus-summary-catchup
1963   "H" gnus-summary-catchup-to-here
1964   "h" gnus-summary-catchup-from-here
1965   "\C-c" gnus-summary-catchup-all
1966   "k" gnus-summary-kill-same-subject-and-select
1967   "K" gnus-summary-kill-same-subject
1968   "P" gnus-uu-mark-map)
1969
1970 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1971   "c" gnus-summary-clear-above
1972   "u" gnus-summary-tick-above
1973   "m" gnus-summary-mark-above
1974   "k" gnus-summary-kill-below)
1975
1976 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1977   "/" gnus-summary-limit-to-subject
1978   "n" gnus-summary-limit-to-articles
1979   "b" gnus-summary-limit-to-bodies
1980   "h" gnus-summary-limit-to-headers
1981   "w" gnus-summary-pop-limit
1982   "s" gnus-summary-limit-to-subject
1983   "a" gnus-summary-limit-to-author
1984   "u" gnus-summary-limit-to-unread
1985   "m" gnus-summary-limit-to-marks
1986   "M" gnus-summary-limit-exclude-marks
1987   "v" gnus-summary-limit-to-score
1988   "*" gnus-summary-limit-include-cached
1989   "D" gnus-summary-limit-include-dormant
1990   "T" gnus-summary-limit-include-thread
1991   "d" gnus-summary-limit-exclude-dormant
1992   "t" gnus-summary-limit-to-age
1993   "." gnus-summary-limit-to-unseen
1994   "x" gnus-summary-limit-to-extra
1995   "p" gnus-summary-limit-to-display-predicate
1996   "E" gnus-summary-limit-include-expunged
1997   "c" gnus-summary-limit-exclude-childless-dormant
1998   "C" gnus-summary-limit-mark-excluded-as-read
1999   "o" gnus-summary-insert-old-articles
2000   "N" gnus-summary-insert-new-articles
2001   "S" gnus-summary-limit-to-singletons
2002   "r" gnus-summary-limit-to-replied
2003   "R" gnus-summary-limit-to-recipient
2004   "A" gnus-summary-limit-to-address)
2005
2006 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2007   "n" gnus-summary-next-unread-article
2008   "p" gnus-summary-prev-unread-article
2009   "N" gnus-summary-next-article
2010   "P" gnus-summary-prev-article
2011   "\C-n" gnus-summary-next-same-subject
2012   "\C-p" gnus-summary-prev-same-subject
2013   "\M-n" gnus-summary-next-unread-subject
2014   "\M-p" gnus-summary-prev-unread-subject
2015   "f" gnus-summary-first-unread-article
2016   "b" gnus-summary-best-unread-article
2017   "j" gnus-summary-goto-article
2018   "g" gnus-summary-goto-subject
2019   "l" gnus-summary-goto-last-article
2020   "o" gnus-summary-pop-article)
2021
2022 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2023   "k" gnus-summary-kill-thread
2024   "E" gnus-summary-expire-thread
2025   "l" gnus-summary-lower-thread
2026   "i" gnus-summary-raise-thread
2027   "T" gnus-summary-toggle-threads
2028   "t" gnus-summary-rethread-current
2029   "^" gnus-summary-reparent-thread
2030   "\M-^" gnus-summary-reparent-children
2031   "s" gnus-summary-show-thread
2032   "S" gnus-summary-show-all-threads
2033   "h" gnus-summary-hide-thread
2034   "H" gnus-summary-hide-all-threads
2035   "n" gnus-summary-next-thread
2036   "p" gnus-summary-prev-thread
2037   "u" gnus-summary-up-thread
2038   "o" gnus-summary-top-thread
2039   "d" gnus-summary-down-thread
2040   "#" gnus-uu-mark-thread
2041   "\M-#" gnus-uu-unmark-thread)
2042
2043 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2044   "g" gnus-summary-prepare
2045   "c" gnus-summary-insert-cached-articles
2046   "d" gnus-summary-insert-dormant-articles
2047   "t" gnus-summary-insert-ticked-articles)
2048
2049 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2050   "c" gnus-summary-catchup-and-exit
2051   "C" gnus-summary-catchup-all-and-exit
2052   "E" gnus-summary-exit-no-update
2053   "Q" gnus-summary-exit
2054   "Z" gnus-summary-exit
2055   "n" gnus-summary-catchup-and-goto-next-group
2056   "p" gnus-summary-catchup-and-goto-prev-group
2057   "R" gnus-summary-reselect-current-group
2058   "G" gnus-summary-rescan-group
2059   "N" gnus-summary-next-group
2060   "s" gnus-summary-save-newsrc
2061   "P" gnus-summary-prev-group)
2062
2063 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2064   " " gnus-summary-next-page
2065   "n" gnus-summary-next-page
2066   [?\S-\ ] gnus-summary-prev-page
2067   "\177" gnus-summary-prev-page
2068   [delete] gnus-summary-prev-page
2069   "p" gnus-summary-prev-page
2070   "\r" gnus-summary-scroll-up
2071   "\M-\r" gnus-summary-scroll-down
2072   "<" gnus-summary-beginning-of-article
2073   ">" gnus-summary-end-of-article
2074   "b" gnus-summary-beginning-of-article
2075   "e" gnus-summary-end-of-article
2076   "^" gnus-summary-refer-parent-article
2077   "r" gnus-summary-refer-parent-article
2078   "C" gnus-summary-show-complete-article
2079   "D" gnus-summary-enter-digest-group
2080   "R" gnus-summary-refer-references
2081   "T" gnus-summary-refer-thread
2082   "W" gnus-warp-to-article
2083   "g" gnus-summary-show-article
2084   "s" gnus-summary-isearch-article
2085   "\t" gnus-summary-widget-forward
2086   [backtab] gnus-summary-widget-backward
2087   "P" gnus-summary-print-article
2088   "S" gnus-sticky-article
2089   "M" gnus-mailing-list-insinuate
2090   "t" gnus-article-babel)
2091
2092 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2093   "b" gnus-article-add-buttons
2094   "B" gnus-article-add-buttons-to-head
2095   "o" gnus-article-treat-overstrike
2096   "e" gnus-article-emphasize
2097   "w" gnus-article-fill-cited-article
2098   "Q" gnus-article-fill-long-lines
2099   "L" gnus-article-toggle-truncate-lines
2100   "C" gnus-article-capitalize-sentences
2101   "c" gnus-article-remove-cr
2102   "q" gnus-article-de-quoted-unreadable
2103   "6" gnus-article-de-base64-unreadable
2104   "Z" gnus-article-decode-HZ
2105   "A" gnus-article-treat-ansi-sequences
2106   "h" gnus-article-wash-html
2107   "u" gnus-article-unsplit-urls
2108   "s" gnus-summary-force-verify-and-decrypt
2109   "f" gnus-article-display-x-face
2110   "l" gnus-summary-stop-page-breaking
2111   "r" gnus-summary-caesar-message
2112   "m" gnus-summary-morse-message
2113   "t" gnus-summary-toggle-header
2114   "g" gnus-treat-smiley
2115   "v" gnus-summary-verbose-headers
2116   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2117   "p" gnus-article-verify-x-pgp-sig
2118   "d" gnus-article-treat-dumbquotes
2119   "U" gnus-article-treat-non-ascii
2120   "i" gnus-summary-idna-message)
2121
2122 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2123   ;; mnemonic: deuglif*Y*
2124   "u" gnus-article-outlook-unwrap-lines
2125   "a" gnus-article-outlook-repair-attribution
2126   "c" gnus-article-outlook-rearrange-citation
2127   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2128
2129 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2130   "a" gnus-article-hide
2131   "h" gnus-article-hide-headers
2132   "b" gnus-article-hide-boring-headers
2133   "s" gnus-article-hide-signature
2134   "c" gnus-article-hide-citation
2135   "C" gnus-article-hide-citation-in-followups
2136   "l" gnus-article-hide-list-identifiers
2137   "B" gnus-article-strip-banner
2138   "P" gnus-article-hide-pem
2139   "\C-c" gnus-article-hide-citation-maybe)
2140
2141 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2142   "a" gnus-article-highlight
2143   "h" gnus-article-highlight-headers
2144   "c" gnus-article-highlight-citation
2145   "s" gnus-article-highlight-signature)
2146
2147 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2148   "f" gnus-article-treat-fold-headers
2149   "u" gnus-article-treat-unfold-headers
2150   "n" gnus-article-treat-fold-newsgroups)
2151
2152 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2153   "x" gnus-article-display-x-face
2154   "d" gnus-article-display-face
2155   "s" gnus-treat-smiley
2156   "D" gnus-article-remove-images
2157   "W" gnus-article-show-images
2158   "f" gnus-treat-from-picon
2159   "m" gnus-treat-mail-picon
2160   "n" gnus-treat-newsgroups-picon
2161   "g" gnus-treat-from-gravatar
2162   "h" gnus-treat-mail-gravatar)
2163
2164 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2165   "w" gnus-article-decode-mime-words
2166   "c" gnus-article-decode-charset
2167   "v" gnus-mime-view-all-parts
2168   "b" gnus-article-view-part)
2169
2170 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2171   "z" gnus-article-date-ut
2172   "u" gnus-article-date-ut
2173   "l" gnus-article-date-local
2174   "p" gnus-article-date-english
2175   "e" gnus-article-date-lapsed
2176   "o" gnus-article-date-original
2177   "i" gnus-article-date-iso8601
2178   "s" gnus-article-date-user)
2179
2180 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2181   "t" gnus-article-remove-trailing-blank-lines
2182   "l" gnus-article-strip-leading-blank-lines
2183   "m" gnus-article-strip-multiple-blank-lines
2184   "a" gnus-article-strip-blank-lines
2185   "A" gnus-article-strip-all-blank-lines
2186   "s" gnus-article-strip-leading-space
2187   "e" gnus-article-strip-trailing-space
2188   "w" gnus-article-remove-leading-whitespace)
2189
2190 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2191   "v" gnus-version
2192   "d" gnus-summary-describe-group
2193   "h" gnus-summary-describe-briefly
2194   "i" gnus-info-find-node)
2195
2196 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2197   "e" gnus-summary-expire-articles
2198   "\M-\C-e" gnus-summary-expire-articles-now
2199   "\177" gnus-summary-delete-article
2200   [delete] gnus-summary-delete-article
2201   [backspace] gnus-summary-delete-article
2202   "m" gnus-summary-move-article
2203   "r" gnus-summary-respool-article
2204   "w" gnus-summary-edit-article
2205   "c" gnus-summary-copy-article
2206   "B" gnus-summary-crosspost-article
2207   "q" gnus-summary-respool-query
2208   "t" gnus-summary-respool-trace
2209   "i" gnus-summary-import-article
2210   "I" gnus-summary-create-article
2211   "p" gnus-summary-article-posted-p)
2212
2213 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2214   "o" gnus-summary-save-article
2215   "m" gnus-summary-save-article-mail
2216   "F" gnus-summary-write-article-file
2217   "r" gnus-summary-save-article-rmail
2218   "f" gnus-summary-save-article-file
2219   "b" gnus-summary-save-article-body-file
2220   "B" gnus-summary-write-article-body-file
2221   "h" gnus-summary-save-article-folder
2222   "v" gnus-summary-save-article-vm
2223   "p" gnus-summary-pipe-output
2224   "P" gnus-summary-muttprint)
2225
2226 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2227   "b" gnus-summary-display-buttonized
2228   "m" gnus-summary-repair-multipart
2229   "v" gnus-article-view-part
2230   "o" gnus-article-save-part
2231   "O" gnus-article-save-part-and-strip
2232   "r" gnus-article-replace-part
2233   "d" gnus-article-delete-part
2234   "t" gnus-article-view-part-as-type
2235   "j" gnus-article-jump-to-part
2236   "c" gnus-article-copy-part
2237   "C" gnus-article-view-part-as-charset
2238   "e" gnus-article-view-part-externally
2239   "H" gnus-article-browse-html-article
2240   "E" gnus-article-encrypt-body
2241   "i" gnus-article-inline-part
2242   "|" gnus-article-pipe-part)
2243
2244 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2245   "p" gnus-summary-mark-as-processable
2246   "u" gnus-summary-unmark-as-processable
2247   "U" gnus-summary-unmark-all-processable
2248   "v" gnus-uu-mark-over
2249   "s" gnus-uu-mark-series
2250   "r" gnus-uu-mark-region
2251   "g" gnus-uu-unmark-region
2252   "R" gnus-uu-mark-by-regexp
2253   "G" gnus-uu-unmark-by-regexp
2254   "t" gnus-uu-mark-thread
2255   "T" gnus-uu-unmark-thread
2256   "a" gnus-uu-mark-all
2257   "b" gnus-uu-mark-buffer
2258   "S" gnus-uu-mark-sparse
2259   "k" gnus-summary-kill-process-mark
2260   "y" gnus-summary-yank-process-mark
2261   "w" gnus-summary-save-process-mark
2262   "i" gnus-uu-invert-processable)
2263
2264 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2265   ;;"x" gnus-uu-extract-any
2266   "m" gnus-summary-save-parts
2267   "u" gnus-uu-decode-uu
2268   "U" gnus-uu-decode-uu-and-save
2269   "s" gnus-uu-decode-unshar
2270   "S" gnus-uu-decode-unshar-and-save
2271   "o" gnus-uu-decode-save
2272   "O" gnus-uu-decode-save
2273   "b" gnus-uu-decode-binhex
2274   "B" gnus-uu-decode-binhex
2275   "Y" gnus-uu-decode-yenc
2276   "p" gnus-uu-decode-postscript
2277   "P" gnus-uu-decode-postscript-and-save)
2278
2279 (gnus-define-keys
2280     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2281   "u" gnus-uu-decode-uu-view
2282   "U" gnus-uu-decode-uu-and-save-view
2283   "s" gnus-uu-decode-unshar-view
2284   "S" gnus-uu-decode-unshar-and-save-view
2285   "o" gnus-uu-decode-save-view
2286   "O" gnus-uu-decode-save-view
2287   "b" gnus-uu-decode-binhex-view
2288   "B" gnus-uu-decode-binhex-view
2289   "p" gnus-uu-decode-postscript-view
2290   "P" gnus-uu-decode-postscript-and-save-view)
2291
2292 (defvar gnus-article-post-menu nil)
2293
2294 (defconst gnus-summary-menu-maxlen 20)
2295
2296 (defun gnus-summary-menu-split (menu)
2297   ;; If we have lots of elements, divide them into groups of 20
2298   ;; and make a pane (or submenu) for each one.
2299   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2300       (let ((menu menu) sublists next
2301             (i 1))
2302         (while menu
2303           ;; Pull off the next gnus-summary-menu-maxlen elements
2304           ;; and make them the next element of sublist.
2305           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2306           (if next
2307               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2308                       nil))
2309           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2310                                              (aref (car (last menu)) 0)) menu)
2311                                sublists))
2312           (setq i (1+ i))
2313           (setq menu next))
2314         (nreverse sublists))
2315     ;; Few elements--put them all in one pane.
2316     menu))
2317
2318 (defun gnus-summary-make-menu-bar ()
2319   (gnus-turn-off-edit-menu 'summary)
2320
2321   (unless (boundp 'gnus-summary-misc-menu)
2322
2323     (easy-menu-define
2324       gnus-summary-kill-menu gnus-summary-mode-map ""
2325       (cons
2326        "Score"
2327        (nconc
2328         (list
2329          ["Customize" gnus-score-customize t])
2330         (gnus-make-score-map 'increase)
2331         (gnus-make-score-map 'lower)
2332         '(("Mark"
2333            ["Kill below" gnus-summary-kill-below t]
2334            ["Mark above" gnus-summary-mark-above t]
2335            ["Tick above" gnus-summary-tick-above t]
2336            ["Clear above" gnus-summary-clear-above t])
2337           ["Current score" gnus-summary-current-score t]
2338           ["Set score" gnus-summary-set-score t]
2339           ["Switch current score file..." gnus-score-change-score-file t]
2340           ["Set mark below..." gnus-score-set-mark-below t]
2341           ["Set expunge below..." gnus-score-set-expunge-below t]
2342           ["Edit current score file" gnus-score-edit-current-scores t]
2343           ["Edit score file..." gnus-score-edit-file t]
2344           ["Trace score" gnus-score-find-trace t]
2345           ["Find words" gnus-score-find-favourite-words t]
2346           ["Rescore buffer" gnus-summary-rescore t]
2347           ["Increase score..." gnus-summary-increase-score t]
2348           ["Lower score..." gnus-summary-lower-score t]))))
2349
2350     ;; Define both the Article menu in the summary buffer and the
2351     ;; equivalent Commands menu in the article buffer here for
2352     ;; consistency.
2353     (let ((innards
2354            `(("Hide"
2355               ["All" gnus-article-hide t]
2356               ["Headers" gnus-article-hide-headers t]
2357               ["Signature" gnus-article-hide-signature t]
2358               ["Citation" gnus-article-hide-citation t]
2359               ["List identifiers" gnus-article-hide-list-identifiers t]
2360               ["Banner" gnus-article-strip-banner t]
2361               ["Boring headers" gnus-article-hide-boring-headers t])
2362              ("Highlight"
2363               ["All" gnus-article-highlight t]
2364               ["Headers" gnus-article-highlight-headers t]
2365               ["Signature" gnus-article-highlight-signature t]
2366               ["Citation" gnus-article-highlight-citation t])
2367              ("MIME"
2368               ["Words" gnus-article-decode-mime-words t]
2369               ["Charset" gnus-article-decode-charset t]
2370               ["QP" gnus-article-de-quoted-unreadable t]
2371               ["Base64" gnus-article-de-base64-unreadable t]
2372               ["View MIME buttons" gnus-summary-display-buttonized t]
2373               ["View all" gnus-mime-view-all-parts t]
2374               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2375               ["Encrypt body" gnus-article-encrypt-body
2376                :active (not (gnus-group-read-only-p))
2377                ,@(if (featurep 'xemacs) nil
2378                    '(:help "Encrypt the message body on disk"))]
2379               ["Extract all parts..." gnus-summary-save-parts t]
2380               ("Multipart"
2381                ["Repair multipart" gnus-summary-repair-multipart t]
2382                ["Pipe part..." gnus-article-pipe-part t]
2383                ["Inline part" gnus-article-inline-part t]
2384                ["View part as type..." gnus-article-view-part-as-type t]
2385                ["Encrypt body" gnus-article-encrypt-body
2386                 :active (not (gnus-group-read-only-p))
2387                ,@(if (featurep 'xemacs) nil
2388                    '(:help "Encrypt the message body on disk"))]
2389                ["View part externally" gnus-article-view-part-externally t]
2390                ["View HTML parts in browser" gnus-article-browse-html-article t]
2391                ["View part with charset..." gnus-article-view-part-as-charset t]
2392                ["Copy part" gnus-article-copy-part t]
2393                ["Save part..." gnus-article-save-part t]
2394                ["View part" gnus-article-view-part t]))
2395              ("Date"
2396               ["Local" gnus-article-date-local t]
2397               ["ISO8601" gnus-article-date-iso8601 t]
2398               ["UT" gnus-article-date-ut t]
2399               ["Original" gnus-article-date-original t]
2400               ["Lapsed" gnus-article-date-lapsed t]
2401               ["User-defined" gnus-article-date-user t])
2402              ("Display"
2403               ["Remove images" gnus-article-remove-images t]
2404               ["Toggle smiley" gnus-treat-smiley t]
2405               ["Show X-Face" gnus-article-display-x-face t]
2406               ["Show picons in From" gnus-treat-from-picon t]
2407               ["Show picons in mail headers" gnus-treat-mail-picon t]
2408               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2409               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2410               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2411               ("View as different encoding"
2412                ,@(gnus-summary-menu-split
2413                   (mapcar
2414                    (lambda (cs)
2415                      ;; Since easymenu under Emacs doesn't allow
2416                      ;; lambda forms for menu commands, we should
2417                      ;; provide intern'ed function symbols.
2418                      (let ((command (intern (format "\
2419 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2420                        (fset command
2421                              `(lambda ()
2422                                 (interactive)
2423                                 (let ((gnus-summary-show-article-charset-alist
2424                                        '((1 . ,cs))))
2425                                   (gnus-summary-show-article 1))))
2426                        `[,(symbol-name cs) ,command t]))
2427                    (sort (if (fboundp 'coding-system-list)
2428                              (coding-system-list)
2429                            (mapcar 'car mm-mime-mule-charset-alist))
2430                          'string<)))))
2431              ("Washing"
2432               ("Remove Blanks"
2433                ["Leading" gnus-article-strip-leading-blank-lines t]
2434                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2435                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2436                ["All of the above" gnus-article-strip-blank-lines t]
2437                ["All" gnus-article-strip-all-blank-lines t]
2438                ["Leading space" gnus-article-strip-leading-space t]
2439                ["Trailing space" gnus-article-strip-trailing-space t]
2440                ["Leading space in headers"
2441                 gnus-article-remove-leading-whitespace t])
2442               ["Overstrike" gnus-article-treat-overstrike t]
2443               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2444               ["Non-ASCII" gnus-article-treat-non-ascii t]
2445               ["Emphasis" gnus-article-emphasize t]
2446               ["Word wrap" gnus-article-fill-cited-article t]
2447               ["Fill long lines" gnus-article-fill-long-lines t]
2448               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2449               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2450               ["Remove CR" gnus-article-remove-cr t]
2451               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2452               ["Base64" gnus-article-de-base64-unreadable t]
2453               ["Rot 13" gnus-summary-caesar-message
2454                ,@(if (featurep 'xemacs) '(t)
2455                    '(:help "\"Caesar rotate\" article by 13"))]
2456               ["De-IDNA" gnus-summary-idna-message t]
2457               ["Morse decode" gnus-summary-morse-message t]
2458               ["Unix pipe..." gnus-summary-pipe-message t]
2459               ["Add buttons" gnus-article-add-buttons t]
2460               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2461               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2462               ["Verbose header" gnus-summary-verbose-headers t]
2463               ["Toggle header" gnus-summary-toggle-header t]
2464               ["Unfold headers" gnus-article-treat-unfold-headers t]
2465               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2466               ["Html" gnus-article-wash-html t]
2467               ["Unsplit URLs" gnus-article-unsplit-urls t]
2468               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2469               ["Decode HZ" gnus-article-decode-HZ t]
2470               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2471               ("(Outlook) Deuglify"
2472                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2473                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2474                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2475                ["Full (Outlook) deuglify"
2476                 gnus-article-outlook-deuglify-article t])
2477               )
2478              ("Output"
2479               ["Save in default format..." gnus-summary-save-article
2480                ,@(if (featurep 'xemacs) '(t)
2481                    '(:help "Save article using default method"))]
2482               ["Save in file..." gnus-summary-save-article-file
2483                ,@(if (featurep 'xemacs) '(t)
2484                    '(:help "Save article in file"))]
2485               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2486               ["Save in MH folder..." gnus-summary-save-article-folder t]
2487               ["Save in VM folder..." gnus-summary-save-article-vm t]
2488               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2489               ["Save body in file..." gnus-summary-save-article-body-file t]
2490               ["Pipe through a filter..." gnus-summary-pipe-output t]
2491               ["Print with Muttprint..." gnus-summary-muttprint t]
2492               ["Print" gnus-summary-print-article
2493                ,@(if (featurep 'xemacs) '(t)
2494                    '(:help "Generate and print a PostScript image"))])
2495              ("Copy, move,... (Backend)"
2496               ,@(if (featurep 'xemacs) nil
2497                   '(:help "Copying, moving, expiring articles..."))
2498               ["Respool article..." gnus-summary-respool-article t]
2499               ["Move article..." gnus-summary-move-article
2500                (gnus-check-backend-function
2501                 'request-move-article gnus-newsgroup-name)]
2502               ["Copy article..." gnus-summary-copy-article t]
2503               ["Crosspost article..." gnus-summary-crosspost-article
2504                (gnus-check-backend-function
2505                 'request-replace-article gnus-newsgroup-name)]
2506               ["Import file..." gnus-summary-import-article
2507                (gnus-check-backend-function
2508                 'request-accept-article gnus-newsgroup-name)]
2509               ["Create article..." gnus-summary-create-article
2510                (gnus-check-backend-function
2511                 'request-accept-article gnus-newsgroup-name)]
2512               ["Check if posted" gnus-summary-article-posted-p t]
2513               ["Edit article" gnus-summary-edit-article
2514                (not (gnus-group-read-only-p))]
2515               ["Delete article" gnus-summary-delete-article
2516                (gnus-check-backend-function
2517                 'request-expire-articles gnus-newsgroup-name)]
2518               ["Query respool" gnus-summary-respool-query t]
2519               ["Trace respool" gnus-summary-respool-trace t]
2520               ["Delete expirable articles" gnus-summary-expire-articles-now
2521                (gnus-check-backend-function
2522                 'request-expire-articles gnus-newsgroup-name)])
2523              ("Extract"
2524               ["Uudecode" gnus-uu-decode-uu
2525                ,@(if (featurep 'xemacs) '(t)
2526                    '(:help "Decode uuencoded article(s)"))]
2527               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2528               ["Unshar" gnus-uu-decode-unshar t]
2529               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2530               ["Save" gnus-uu-decode-save t]
2531               ["Binhex" gnus-uu-decode-binhex t]
2532               ["PostScript" gnus-uu-decode-postscript t]
2533               ["All MIME parts" gnus-summary-save-parts t])
2534              ("Cache"
2535               ["Enter article" gnus-cache-enter-article t]
2536               ["Remove article" gnus-cache-remove-article t])
2537              ["Translate" gnus-article-babel t]
2538              ["Select article buffer" gnus-summary-select-article-buffer t]
2539              ["Make article buffer sticky" gnus-sticky-article t]
2540              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2541              ["Isearch article..." gnus-summary-isearch-article t]
2542              ["Beginning of the article" gnus-summary-beginning-of-article t]
2543              ["End of the article" gnus-summary-end-of-article t]
2544              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2545              ["Fetch referenced articles" gnus-summary-refer-references t]
2546              ["Fetch current thread" gnus-summary-refer-thread t]
2547              ["Fetch article with id..." gnus-summary-refer-article t]
2548              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2549              ["Redisplay" gnus-summary-show-article t]
2550              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2551       (easy-menu-define
2552         gnus-summary-article-menu gnus-summary-mode-map ""
2553         (cons "Article" innards))
2554
2555       (if (not (keymapp gnus-summary-article-menu))
2556           (easy-menu-define
2557             gnus-article-commands-menu gnus-article-mode-map ""
2558             (cons "Commands" innards))
2559         ;; in Emacs, don't share menu.
2560         (setq gnus-article-commands-menu
2561               (copy-keymap gnus-summary-article-menu))
2562         (define-key gnus-article-mode-map [menu-bar commands]
2563           (cons "Commands" gnus-article-commands-menu))))
2564
2565     (easy-menu-define
2566       gnus-summary-thread-menu gnus-summary-mode-map ""
2567       '("Threads"
2568         ["Find all messages in thread" gnus-summary-refer-thread t]
2569         ["Toggle threading" gnus-summary-toggle-threads t]
2570         ["Hide threads" gnus-summary-hide-all-threads t]
2571         ["Show threads" gnus-summary-show-all-threads t]
2572         ["Hide thread" gnus-summary-hide-thread t]
2573         ["Show thread" gnus-summary-show-thread t]
2574         ["Go to next thread" gnus-summary-next-thread t]
2575         ["Go to previous thread" gnus-summary-prev-thread t]
2576         ["Go down thread" gnus-summary-down-thread t]
2577         ["Go up thread" gnus-summary-up-thread t]
2578         ["Top of thread" gnus-summary-top-thread t]
2579         ["Mark thread as read" gnus-summary-kill-thread t]
2580         ["Mark thread as expired" gnus-summary-expire-thread t]
2581         ["Lower thread score" gnus-summary-lower-thread t]
2582         ["Raise thread score" gnus-summary-raise-thread t]
2583         ["Rethread current" gnus-summary-rethread-current t]))
2584
2585     (easy-menu-define
2586       gnus-summary-post-menu gnus-summary-mode-map ""
2587       `("Post"
2588         ["Send a message (mail or news)" gnus-summary-post-news
2589          ,@(if (featurep 'xemacs) '(t)
2590              '(:help "Compose a new message (mail or news)"))]
2591         ["Followup" gnus-summary-followup
2592          ,@(if (featurep 'xemacs) '(t)
2593              '(:help "Post followup to this article"))]
2594         ["Followup and yank" gnus-summary-followup-with-original
2595          ,@(if (featurep 'xemacs) '(t)
2596              '(:help "Post followup to this article, quoting its contents"))]
2597         ["Supersede article" gnus-summary-supersede-article t]
2598         ["Cancel article" gnus-summary-cancel-article
2599          ,@(if (featurep 'xemacs) '(t)
2600              '(:help "Cancel an article you posted"))]
2601         ["Reply" gnus-summary-reply t]
2602         ["Reply and yank" gnus-summary-reply-with-original t]
2603         ["Wide reply" gnus-summary-wide-reply t]
2604         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2605          ,@(if (featurep 'xemacs) '(t)
2606              '(:help "Mail a reply, quoting this article"))]
2607         ["Very wide reply" gnus-summary-very-wide-reply t]
2608         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2609          ,@(if (featurep 'xemacs) '(t)
2610              '(:help "Mail a very wide reply, quoting this article"))]
2611         ["Mail forward" gnus-summary-mail-forward t]
2612         ["Post forward" gnus-summary-post-forward t]
2613         ["Digest and mail" gnus-uu-digest-mail-forward t]
2614         ["Digest and post" gnus-uu-digest-post-forward t]
2615         ["Resend message" gnus-summary-resend-message t]
2616         ["Resend message edit" gnus-summary-resend-message-edit t]
2617         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2618         ["Send a mail" gnus-summary-mail-other-window t]
2619         ["Create a local message" gnus-summary-news-other-window t]
2620         ["Uuencode and post" gnus-uu-post-news
2621          ,@(if (featurep 'xemacs) '(t)
2622              '(:help "Post a uuencoded article"))]
2623         ["Followup via news" gnus-summary-followup-to-mail t]
2624         ["Followup via news and yank"
2625          gnus-summary-followup-to-mail-with-original t]
2626         ["Strip signature on reply"
2627          (lambda ()
2628            (interactive)
2629            (if (not (memq message-cite-function
2630                           '(message-cite-original-without-signature
2631                             message-cite-original)))
2632                ;; Stupid workaround for XEmacs not honoring :visible.
2633                (message "Can't toggle this value of `message-cite-function'")
2634              (setq message-cite-function
2635                    (if (eq message-cite-function
2636                            'message-cite-original-without-signature)
2637                        'message-cite-original
2638                      'message-cite-original-without-signature))))
2639          ;; XEmacs barfs on :visible.
2640          ,@(if (featurep 'xemacs) nil
2641              '(:visible (memq message-cite-function
2642                               '(message-cite-original-without-signature
2643                                 message-cite-original))))
2644          :style toggle
2645          :selected (eq message-cite-function
2646                        'message-cite-original-without-signature)
2647          ,@(if (featurep 'xemacs) nil
2648              '(:help "Strip signature from cited article when replying."))]
2649         ;;("Draft"
2650         ;;["Send" gnus-summary-send-draft t]
2651         ;;["Send bounced" gnus-resend-bounced-mail t])
2652         ))
2653
2654     (cond
2655      ((not (keymapp gnus-summary-post-menu))
2656       (setq gnus-article-post-menu gnus-summary-post-menu))
2657      ((not gnus-article-post-menu)
2658       ;; Don't share post menu.
2659       (setq gnus-article-post-menu
2660             (copy-keymap gnus-summary-post-menu))))
2661     (define-key gnus-article-mode-map [menu-bar post]
2662       (cons "Post" gnus-article-post-menu))
2663
2664     (easy-menu-define
2665       gnus-summary-misc-menu gnus-summary-mode-map ""
2666       `("Gnus"
2667         ("Mark Read"
2668          ["Mark as read" gnus-summary-mark-as-read-forward t]
2669          ["Mark same subject and select"
2670           gnus-summary-kill-same-subject-and-select t]
2671          ["Mark same subject" gnus-summary-kill-same-subject t]
2672          ["Catchup" gnus-summary-catchup
2673           ,@(if (featurep 'xemacs) '(t)
2674               '(:help "Mark unread articles in this group as read"))]
2675          ["Catchup all" gnus-summary-catchup-all t]
2676          ["Catchup to here" gnus-summary-catchup-to-here t]
2677          ["Catchup from here" gnus-summary-catchup-from-here t]
2678          ["Catchup region" gnus-summary-mark-region-as-read
2679           (gnus-mark-active-p)]
2680          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2681         ("Mark Various"
2682          ["Tick" gnus-summary-tick-article-forward t]
2683          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2684          ["Remove marks" gnus-summary-clear-mark-forward t]
2685          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2686          ["Set bookmark" gnus-summary-set-bookmark t]
2687          ["Remove bookmark" gnus-summary-remove-bookmark t])
2688         ("Limit to"
2689          ["Marks..." gnus-summary-limit-to-marks t]
2690          ["Subject..." gnus-summary-limit-to-subject t]
2691          ["Author..." gnus-summary-limit-to-author t]
2692          ["Recipient..." gnus-summary-limit-to-recipient t]
2693          ["Address..." gnus-summary-limit-to-address t]
2694          ["Age..." gnus-summary-limit-to-age t]
2695          ["Extra..." gnus-summary-limit-to-extra t]
2696          ["Score..." gnus-summary-limit-to-score t]
2697          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2698          ["Unread" gnus-summary-limit-to-unread t]
2699          ["Unseen" gnus-summary-limit-to-unseen t]
2700          ["Singletons" gnus-summary-limit-to-singletons t]
2701          ["Replied" gnus-summary-limit-to-replied t]
2702          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2703          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2704          ["Pop limit" gnus-summary-pop-limit t]
2705          ["Show dormant" gnus-summary-limit-include-dormant t]
2706          ["Hide childless dormant"
2707           gnus-summary-limit-exclude-childless-dormant t]
2708          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2709          ["Hide marked" gnus-summary-limit-exclude-marks t]
2710          ["Show expunged" gnus-summary-limit-include-expunged t])
2711         ("Process Mark"
2712          ["Set mark" gnus-summary-mark-as-processable t]
2713          ["Remove mark" gnus-summary-unmark-as-processable t]
2714          ["Remove all marks" gnus-summary-unmark-all-processable t]
2715          ["Invert marks" gnus-uu-invert-processable t]
2716          ["Mark above" gnus-uu-mark-over t]
2717          ["Mark series" gnus-uu-mark-series t]
2718          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2719          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2720          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2721          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2722          ["Mark all" gnus-uu-mark-all t]
2723          ["Mark buffer" gnus-uu-mark-buffer t]
2724          ["Mark sparse" gnus-uu-mark-sparse t]
2725          ["Mark thread" gnus-uu-mark-thread t]
2726          ["Unmark thread" gnus-uu-unmark-thread t]
2727          ("Process Mark Sets"
2728           ["Kill" gnus-summary-kill-process-mark t]
2729           ["Yank" gnus-summary-yank-process-mark
2730            gnus-newsgroup-process-stack]
2731           ["Save" gnus-summary-save-process-mark t]
2732           ["Run command on marked..." gnus-summary-universal-argument t]))
2733         ("Registry Marks")
2734         ("Scroll article"
2735          ["Page forward" gnus-summary-next-page
2736           ,@(if (featurep 'xemacs) '(t)
2737               '(:help "Show next page of article"))]
2738          ["Page backward" gnus-summary-prev-page
2739           ,@(if (featurep 'xemacs) '(t)
2740               '(:help "Show previous page of article"))]
2741          ["Line forward" gnus-summary-scroll-up t])
2742         ("Move"
2743          ["Next unread article" gnus-summary-next-unread-article t]
2744          ["Previous unread article" gnus-summary-prev-unread-article t]
2745          ["Next article" gnus-summary-next-article t]
2746          ["Previous article" gnus-summary-prev-article t]
2747          ["Next unread subject" gnus-summary-next-unread-subject t]
2748          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2749          ["Next article same subject" gnus-summary-next-same-subject t]
2750          ["Previous article same subject" gnus-summary-prev-same-subject t]
2751          ["First unread article" gnus-summary-first-unread-article t]
2752          ["Best unread article" gnus-summary-best-unread-article t]
2753          ["Go to subject number..." gnus-summary-goto-subject t]
2754          ["Go to article number..." gnus-summary-goto-article t]
2755          ["Go to the last article" gnus-summary-goto-last-article t]
2756          ["Pop article off history" gnus-summary-pop-article t])
2757         ("Sort"
2758          ["Sort by number" gnus-summary-sort-by-number t]
2759          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2760          ["Sort by author" gnus-summary-sort-by-author t]
2761          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2762          ["Sort by subject" gnus-summary-sort-by-subject t]
2763          ["Sort by date" gnus-summary-sort-by-date t]
2764          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2765          ["Sort by score" gnus-summary-sort-by-score t]
2766          ["Sort by lines" gnus-summary-sort-by-lines t]
2767          ["Sort by characters" gnus-summary-sort-by-chars t]
2768          ["Randomize" gnus-summary-sort-by-random t]
2769          ["Original sort" gnus-summary-sort-by-original t])
2770         ("Help"
2771          ["Describe group" gnus-summary-describe-group t]
2772          ["Read manual" gnus-info-find-node t])
2773         ("Modes"
2774          ["Pick and read" gnus-pick-mode t]
2775          ["Binary" gnus-binary-mode t])
2776         ("Regeneration"
2777          ["Regenerate" gnus-summary-prepare t]
2778          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2779          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2780          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2781          ["Toggle threading" gnus-summary-toggle-threads t])
2782         ["See old articles" gnus-summary-insert-old-articles t]
2783         ["See new articles" gnus-summary-insert-new-articles t]
2784         ["Filter articles..." gnus-summary-execute-command t]
2785         ["Run command on articles..." gnus-summary-universal-argument t]
2786         ["Search articles forward..." gnus-summary-search-article-forward t]
2787         ["Search articles backward..." gnus-summary-search-article-backward t]
2788         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2789         ["Expand window" gnus-summary-expand-window t]
2790         ["Expire expirable articles" gnus-summary-expire-articles
2791          (gnus-check-backend-function
2792           'request-expire-articles gnus-newsgroup-name)]
2793         ["Edit local kill file" gnus-summary-edit-local-kill t]
2794         ["Edit main kill file" gnus-summary-edit-global-kill t]
2795         ["Edit group parameters" gnus-summary-edit-parameters t]
2796         ["Customize group parameters" gnus-summary-customize-parameters t]
2797         ["Send a bug report" gnus-bug t]
2798         ("Exit"
2799          ["Catchup and exit" gnus-summary-catchup-and-exit
2800           ,@(if (featurep 'xemacs) '(t)
2801               '(:help "Mark unread articles in this group as read, then exit"))]
2802          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2803          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2804          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2805          ["Exit group" gnus-summary-exit
2806           ,@(if (featurep 'xemacs) '(t)
2807               '(:help "Exit current group, return to group selection mode"))]
2808          ["Exit group without updating" gnus-summary-exit-no-update t]
2809          ["Exit and goto next group" gnus-summary-next-group t]
2810          ["Exit and goto prev group" gnus-summary-prev-group t]
2811          ["Reselect group" gnus-summary-reselect-current-group t]
2812          ["Rescan group" gnus-summary-rescan-group t]
2813          ["Update dribble" gnus-summary-save-newsrc t])))
2814
2815     (gnus-run-hooks 'gnus-summary-menu-hook)))
2816
2817 (defvar gnus-summary-tool-bar-map nil)
2818
2819 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2820 ;; affect _new_ message buffers.  We might add a function that walks thru all
2821 ;; summary-mode buffers and force the update.
2822 (defun gnus-summary-tool-bar-update (&optional symbol value)
2823   "Update summary mode toolbar.
2824 Setter function for custom variables."
2825   (setq-default gnus-summary-tool-bar-map nil)
2826   (when symbol
2827     ;; When used as ":set" function:
2828     (set-default symbol value))
2829   (when (gnus-buffer-live-p gnus-summary-buffer)
2830     (with-current-buffer gnus-summary-buffer
2831       (gnus-summary-make-tool-bar))))
2832
2833 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2834                                      'gnus-summary-tool-bar-gnome
2835                                    'gnus-summary-tool-bar-retro)
2836   "Specifies the Gnus summary tool bar.
2837
2838 It can be either a list or a symbol referring to a list.  See
2839 `gmm-tool-bar-from-list' for the format of the list.  The
2840 default key map is `gnus-summary-mode-map'.
2841
2842 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2843 `gnus-summary-tool-bar-retro'."
2844   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2845                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2846                  (repeat :tag "User defined list" gmm-tool-bar-item)
2847                  (symbol))
2848   :version "23.1" ;; No Gnus
2849   :initialize 'custom-initialize-default
2850   :set 'gnus-summary-tool-bar-update
2851   :group 'gnus-summary)
2852
2853 (defcustom gnus-summary-tool-bar-gnome
2854   '((gnus-summary-post-news "mail/compose" nil)
2855     (gnus-summary-insert-new-articles "mail/inbox" nil
2856                                       :visible (or (not gnus-agent)
2857                                                    gnus-plugged))
2858     (gnus-summary-reply-with-original "mail/reply")
2859     (gnus-summary-reply "mail/reply" nil :visible nil)
2860     (gnus-summary-followup-with-original "mail/reply-all")
2861     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2862     (gnus-summary-mail-forward "mail/forward")
2863     (gnus-summary-save-article "mail/save")
2864     (gnus-summary-search-article-forward "search" nil :visible nil)
2865     (gnus-summary-print-article "print")
2866     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2867     ;; Some new commands that may need more suitable icons:
2868     (gnus-summary-save-newsrc "save" nil :visible nil)
2869     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2870     (gnus-summary-prev-article "left-arrow")
2871     (gnus-summary-next-article "right-arrow")
2872     (gnus-summary-next-page "next-page")
2873     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2874     ;;
2875     ;; Maybe some sort-by-... could be added:
2876     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2877     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2878     (gnus-summary-mark-as-expirable
2879      "delete" nil
2880      :visible (gnus-check-backend-function 'request-expire-articles
2881                                            gnus-newsgroup-name))
2882     (gnus-summary-mark-as-spam
2883      "mail/spam" t
2884      :visible (and (fboundp 'spam-group-ham-contents-p)
2885                    (spam-group-ham-contents-p gnus-newsgroup-name))
2886      :help "Mark as spam")
2887     (gnus-summary-mark-as-read-forward
2888      "mail/not-spam" nil
2889      :visible (and (fboundp 'spam-group-spam-contents-p)
2890                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2891     ;;
2892     (gnus-summary-exit "exit")
2893     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2894     (gnus-info-find-node "help"))
2895   "List of functions for the summary tool bar (GNOME style).
2896
2897 See `gmm-tool-bar-from-list' for the format of the list."
2898   :type '(repeat gmm-tool-bar-item)
2899   :version "23.1" ;; No Gnus
2900   :initialize 'custom-initialize-default
2901   :set 'gnus-summary-tool-bar-update
2902   :group 'gnus-summary)
2903
2904 (defcustom gnus-summary-tool-bar-retro
2905   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2906     (gnus-summary-next-unread-article "gnus/next-ur")
2907     (gnus-summary-post-news "gnus/post")
2908     (gnus-summary-followup-with-original "gnus/fuwo")
2909     (gnus-summary-followup "gnus/followup")
2910     (gnus-summary-reply-with-original "gnus/reply-wo")
2911     (gnus-summary-reply "gnus/reply")
2912     (gnus-summary-caesar-message "gnus/rot13")
2913     (gnus-uu-decode-uu "gnus/uu-decode")
2914     (gnus-summary-save-article-file "gnus/save-aif")
2915     (gnus-summary-save-article "gnus/save-art")
2916     (gnus-uu-post-news "gnus/uu-post")
2917     (gnus-summary-catchup "gnus/catchup")
2918     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2919     (gnus-summary-exit "gnus/exit-summ")
2920     ;; Some new command that may need more suitable icons:
2921     (gnus-summary-print-article "gnus/print" nil :visible nil)
2922     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2923     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2924     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2925     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2926     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2927     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2928     ;;
2929     (gnus-info-find-node "gnus/help" nil :visible nil))
2930   "List of functions for the summary tool bar (retro look).
2931
2932 See `gmm-tool-bar-from-list' for the format of the list."
2933   :type '(repeat gmm-tool-bar-item)
2934   :version "23.1" ;; No Gnus
2935   :initialize 'custom-initialize-default
2936   :set 'gnus-summary-tool-bar-update
2937   :group 'gnus-summary)
2938
2939 (defcustom gnus-summary-tool-bar-zap-list t
2940   "List of icon items from the global tool bar.
2941 These items are not displayed in the Gnus summary mode tool bar.
2942
2943 See `gmm-tool-bar-from-list' for the format of the list."
2944   :type 'gmm-tool-bar-zap-list
2945   :version "23.1" ;; No Gnus
2946   :initialize 'custom-initialize-default
2947   :set 'gnus-summary-tool-bar-update
2948   :group 'gnus-summary)
2949
2950 (defvar image-load-path)
2951 (defvar tool-bar-map)
2952
2953 (defun gnus-summary-make-tool-bar (&optional force)
2954   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2955 When FORCE, rebuild the tool bar."
2956   (when (and (not (featurep 'xemacs))
2957              (boundp 'tool-bar-mode)
2958              tool-bar-mode
2959              (or (not gnus-summary-tool-bar-map) force))
2960     (let* ((load-path
2961             (gmm-image-load-path-for-library "gnus"
2962                                              "mail/save.xpm"
2963                                              nil t))
2964            (image-load-path (cons (car load-path)
2965                                   (when (boundp 'image-load-path)
2966                                     image-load-path)))
2967            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2968                                         gnus-summary-tool-bar-zap-list
2969                                         'gnus-summary-mode-map)))
2970       (when map
2971         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2972         ;; uses its value.
2973         (setq gnus-summary-tool-bar-map map))))
2974   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2975
2976 (defun gnus-make-score-map (type)
2977   "Make a summary score map of type TYPE."
2978   (if t
2979       nil
2980     (let ((headers '(("author" "from" string)
2981                      ("subject" "subject" string)
2982                      ("article body" "body" string)
2983                      ("article head" "head" string)
2984                      ("xref" "xref" string)
2985                      ("extra header" "extra" string)
2986                      ("lines" "lines" number)
2987                      ("followups to author" "followup" string)))
2988           (types '((number ("less than" <)
2989                            ("greater than" >)
2990                            ("equal" =))
2991                    (string ("substring" s)
2992                            ("exact string" e)
2993                            ("fuzzy string" f)
2994                            ("regexp" r))))
2995           (perms '(("temporary" (current-time-string))
2996                    ("permanent" nil)
2997                    ("immediate" now)))
2998           header)
2999       (list
3000        (apply
3001         'nconc
3002         (list
3003          (if (eq type 'lower)
3004              "Lower score"
3005            "Increase score"))
3006         (let (outh)
3007           (while headers
3008             (setq header (car headers))
3009             (setq outh
3010                   (cons
3011                    (apply
3012                     'nconc
3013                     (list (car header))
3014                     (let ((ts (cdr (assoc (nth 2 header) types)))
3015                           outt)
3016                       (while ts
3017                         (setq outt
3018                               (cons
3019                                (apply
3020                                 'nconc
3021                                 (list (caar ts))
3022                                 (let ((ps perms)
3023                                       outp)
3024                                   (while ps
3025                                     (setq outp
3026                                           (cons
3027                                            (vector
3028                                             (caar ps)
3029                                             (list
3030                                              'gnus-summary-score-entry
3031                                              (nth 1 header)
3032                                              (if (or (string= (nth 1 header)
3033                                                               "head")
3034                                                      (string= (nth 1 header)
3035                                                               "body"))
3036                                                  ""
3037                                                (list 'gnus-summary-header
3038                                                      (nth 1 header)))
3039                                              (list 'quote (nth 1 (car ts)))
3040                                              (list 'gnus-score-delta-default
3041                                                    nil)
3042                                              (nth 1 (car ps))
3043                                              t)
3044                                             t)
3045                                            outp))
3046                                     (setq ps (cdr ps)))
3047                                   (list (nreverse outp))))
3048                                outt))
3049                         (setq ts (cdr ts)))
3050                       (list (nreverse outt))))
3051                    outh))
3052             (setq headers (cdr headers)))
3053           (list (nreverse outh))))))))
3054
3055
3056 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3057 (defvar bookmark-make-record-function)
3058 \f
3059 (defvar bidi-paragraph-direction)
3060
3061 (defun gnus-summary-mode (&optional group)
3062   "Major mode for reading articles.
3063
3064 All normal editing commands are switched off.
3065 \\<gnus-summary-mode-map>
3066 Each line in this buffer represents one article.  To read an
3067 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3068 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3069 respectively.
3070
3071 You can also post articles and send mail from this buffer.  To
3072 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3073 of an article, type `\\[gnus-summary-reply]'.
3074
3075 There are approx. one gazillion commands you can execute in this
3076 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3077
3078 The following commands are available:
3079
3080 \\{gnus-summary-mode-map}"
3081   (interactive)
3082   (kill-all-local-variables)
3083   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3084     (gnus-summary-make-local-variables))
3085   (gnus-summary-make-local-variables)
3086   (setq gnus-newsgroup-name group)
3087   (when (gnus-visual-p 'summary-menu 'menu)
3088     (gnus-summary-make-menu-bar)
3089     (gnus-summary-make-tool-bar))
3090   (gnus-make-thread-indent-array)
3091   (gnus-simplify-mode-line)
3092   (setq major-mode 'gnus-summary-mode)
3093   (setq mode-name "Summary")
3094   (use-local-map gnus-summary-mode-map)
3095   (buffer-disable-undo)
3096   (setq buffer-read-only t              ;Disable modification
3097         show-trailing-whitespace nil)
3098   (setq truncate-lines t)
3099   ;; Force paragraph direction to be left-to-right.  Don't make it
3100   ;; bound globally in old Emacsen and XEmacsen.
3101   (set (make-local-variable 'bidi-paragraph-direction) 'left-to-right)
3102   (add-to-invisibility-spec '(gnus-sum . t))
3103   (gnus-summary-set-display-table)
3104   (gnus-set-default-directory)
3105   (make-local-variable 'gnus-summary-line-format)
3106   (make-local-variable 'gnus-summary-line-format-spec)
3107   (make-local-variable 'gnus-summary-dummy-line-format)
3108   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3109   (make-local-variable 'gnus-summary-mark-positions)
3110   (gnus-make-local-hook 'pre-command-hook)
3111   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3112   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3113   (turn-on-gnus-mailing-list-mode)
3114   (mm-enable-multibyte)
3115   (set (make-local-variable 'bookmark-make-record-function)
3116        'gnus-summary-bookmark-make-record)
3117   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3118   (gnus-update-summary-mark-positions))
3119
3120 (defun gnus-summary-make-local-variables ()
3121   "Make all the local summary buffer variables."
3122   (let (global)
3123     (dolist (local gnus-summary-local-variables)
3124       (if (consp local)
3125           (progn
3126             (if (eq (cdr local) 'global)
3127                 ;; Copy the global value of the variable.
3128                 (setq global (symbol-value (car local)))
3129               ;; Use the value from the list.
3130               (setq global (eval (cdr local))))
3131             (set (make-local-variable (car local)) global))
3132         ;; Simple nil-valued local variable.
3133         (set (make-local-variable local) nil)))))
3134
3135 ;; Summary data functions.
3136
3137 (defmacro gnus-data-number (data)
3138   `(car ,data))
3139
3140 (defmacro gnus-data-set-number (data number)
3141   `(setcar ,data ,number))
3142
3143 (defmacro gnus-data-mark (data)
3144   `(nth 1 ,data))
3145
3146 (defmacro gnus-data-set-mark (data mark)
3147   `(setcar (nthcdr 1 ,data) ,mark))
3148
3149 (defmacro gnus-data-pos (data)
3150   `(nth 2 ,data))
3151
3152 (defmacro gnus-data-set-pos (data pos)
3153   `(setcar (nthcdr 2 ,data) ,pos))
3154
3155 (defmacro gnus-data-header (data)
3156   `(nth 3 ,data))
3157
3158 (defmacro gnus-data-set-header (data header)
3159   `(setf (nth 3 ,data) ,header))
3160
3161 (defmacro gnus-data-level (data)
3162   `(nth 4 ,data))
3163
3164 (defmacro gnus-data-unread-p (data)
3165   `(= (nth 1 ,data) gnus-unread-mark))
3166
3167 (defmacro gnus-data-read-p (data)
3168   `(/= (nth 1 ,data) gnus-unread-mark))
3169
3170 (defmacro gnus-data-pseudo-p (data)
3171   `(consp (nth 3 ,data)))
3172
3173 (defmacro gnus-data-find (number)
3174   `(assq ,number gnus-newsgroup-data))
3175
3176 (defmacro gnus-data-find-list (number &optional data)
3177   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3178      (memq (assq ,number bdata)
3179            bdata)))
3180
3181 (defmacro gnus-data-make (number mark pos header level)
3182   `(list ,number ,mark ,pos ,header ,level))
3183
3184 (defun gnus-data-enter (after-article number mark pos header level offset)
3185   (let ((data (gnus-data-find-list after-article)))
3186     (unless data
3187       (error "No such article: %d" after-article))
3188     (setcdr data (cons (gnus-data-make number mark pos header level)
3189                        (cdr data)))
3190     (setq gnus-newsgroup-data-reverse nil)
3191     (gnus-data-update-list (cddr data) offset)))
3192
3193 (defun gnus-data-enter-list (after-article list &optional offset)
3194   (when list
3195     (let ((data (and after-article (gnus-data-find-list after-article)))
3196           (ilist list))
3197       (if (not (or data
3198                    after-article))
3199           (let ((odata gnus-newsgroup-data))
3200             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3201             (when offset
3202               (gnus-data-update-list odata offset)))
3203         ;; Find the last element in the list to be spliced into the main
3204         ;; list.
3205         (setq list (last list))
3206         (if (not data)
3207             (progn
3208               (setcdr list gnus-newsgroup-data)
3209               (setq gnus-newsgroup-data ilist)
3210               (when offset
3211                 (gnus-data-update-list (cdr list) offset)))
3212           (setcdr list (cdr data))
3213           (setcdr data ilist)
3214           (when offset
3215             (gnus-data-update-list (cdr list) offset))))
3216       (setq gnus-newsgroup-data-reverse nil))))
3217
3218 (defun gnus-data-remove (article &optional offset)
3219   (let ((data gnus-newsgroup-data))
3220     (if (= (gnus-data-number (car data)) article)
3221         (progn
3222           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3223                 gnus-newsgroup-data-reverse nil)
3224           (when offset
3225             (gnus-data-update-list gnus-newsgroup-data offset)))
3226       (while (cdr data)
3227         (when (= (gnus-data-number (cadr data)) article)
3228           (setcdr data (cddr data))
3229           (when offset
3230             (gnus-data-update-list (cdr data) offset))
3231           (setq data nil
3232                 gnus-newsgroup-data-reverse nil))
3233         (setq data (cdr data))))))
3234
3235 (defmacro gnus-data-list (backward)
3236   `(if ,backward
3237        (or gnus-newsgroup-data-reverse
3238            (setq gnus-newsgroup-data-reverse
3239                  (reverse gnus-newsgroup-data)))
3240      gnus-newsgroup-data))
3241
3242 (defun gnus-data-update-list (data offset)
3243   "Add OFFSET to the POS of all data entries in DATA."
3244   (setq gnus-newsgroup-data-reverse nil)
3245   (while data
3246     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3247     (setq data (cdr data))))
3248
3249 (defun gnus-summary-article-pseudo-p (article)
3250   "Say whether this article is a pseudo article or not."
3251   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3252
3253 (defmacro gnus-summary-article-sparse-p (article)
3254   "Say whether this article is a sparse article or not."
3255   `(memq ,article gnus-newsgroup-sparse))
3256
3257 (defmacro gnus-summary-article-ancient-p (article)
3258   "Say whether this article is a sparse article or not."
3259   `(memq ,article gnus-newsgroup-ancient))
3260
3261 (defun gnus-article-children (number)
3262   "Return a list of all children to NUMBER."
3263   (let* ((data (gnus-data-find-list number))
3264          (level (gnus-data-level (car data)))
3265          children)
3266     (setq data (cdr data))
3267     (while (and data
3268                 (= (gnus-data-level (car data)) (1+ level)))
3269       (push (gnus-data-number (car data)) children)
3270       (setq data (cdr data)))
3271     children))
3272
3273 (defmacro gnus-summary-skip-intangible ()
3274   "If the current article is intangible, then jump to a different article."
3275   '(let ((to (get-text-property (point) 'gnus-intangible)))
3276      (and to (gnus-summary-goto-subject to))))
3277
3278 (defmacro gnus-summary-article-intangible-p ()
3279   "Say whether this article is intangible or not."
3280   '(get-text-property (point) 'gnus-intangible))
3281
3282 ;; Some summary mode macros.
3283
3284 (defmacro gnus-summary-article-number ()
3285   "The article number of the article on the current line.
3286 If there isn't an article number here, then we return the current
3287 article number."
3288   '(progn
3289      (gnus-summary-skip-intangible)
3290      (or (get-text-property (point) 'gnus-number)
3291          (gnus-summary-last-subject))))
3292
3293 (defmacro gnus-summary-article-header (&optional number)
3294   "Return the header of article NUMBER."
3295   `(gnus-data-header (gnus-data-find
3296                       ,(or number '(gnus-summary-article-number)))))
3297
3298 (defmacro gnus-summary-thread-level (&optional number)
3299   "Return the level of thread that starts with article NUMBER."
3300   `(if (and (eq gnus-summary-make-false-root 'dummy)
3301             (get-text-property (point) 'gnus-intangible))
3302        0
3303      (gnus-data-level (gnus-data-find
3304                        ,(or number '(gnus-summary-article-number))))))
3305
3306 (defmacro gnus-summary-article-mark (&optional number)
3307   "Return the mark of article NUMBER."
3308   `(gnus-data-mark (gnus-data-find
3309                     ,(or number '(gnus-summary-article-number)))))
3310
3311 (defmacro gnus-summary-article-pos (&optional number)
3312   "Return the position of the line of article NUMBER."
3313   `(gnus-data-pos (gnus-data-find
3314                    ,(or number '(gnus-summary-article-number)))))
3315
3316 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3317 (defmacro gnus-summary-article-subject (&optional number)
3318   "Return current subject string or nil if nothing."
3319   `(let ((headers
3320           ,(if number
3321                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3322              '(gnus-data-header (assq (gnus-summary-article-number)
3323                                       gnus-newsgroup-data)))))
3324      (and headers
3325           (vectorp headers)
3326           (mail-header-subject headers))))
3327
3328 (defmacro gnus-summary-article-score (&optional number)
3329   "Return current article score."
3330   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3331                   gnus-newsgroup-scored))
3332        gnus-summary-default-score 0))
3333
3334 (defun gnus-summary-article-children (&optional number)
3335   "Return a list of article numbers that are children of article NUMBER."
3336   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3337          (level (gnus-data-level (car data)))
3338          l children)
3339     (while (and (setq data (cdr data))
3340                 (> (setq l (gnus-data-level (car data))) level))
3341       (and (= (1+ level) l)
3342            (push (gnus-data-number (car data))
3343                  children)))
3344     (nreverse children)))
3345
3346 (defun gnus-summary-article-parent (&optional number)
3347   "Return the article number of the parent of article NUMBER."
3348   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3349                                     (gnus-data-list t)))
3350          (level (gnus-data-level (car data))))
3351     (if (zerop level)
3352         ()                              ; This is a root.
3353       ;; We search until we find an article with a level less than
3354       ;; this one.  That function has to be the parent.
3355       (while (and (setq data (cdr data))
3356                   (not (< (gnus-data-level (car data)) level))))
3357       (and data (gnus-data-number (car data))))))
3358
3359 (defun gnus-unread-mark-p (mark)
3360   "Say whether MARK is the unread mark."
3361   (= mark gnus-unread-mark))
3362
3363 (defun gnus-read-mark-p (mark)
3364   "Say whether MARK is one of the marks that mark as read.
3365 This is all marks except unread, ticked, dormant, and expirable."
3366   (not (or (= mark gnus-unread-mark)
3367            (= mark gnus-ticked-mark)
3368            (= mark gnus-spam-mark)
3369            (= mark gnus-dormant-mark)
3370            (= mark gnus-expirable-mark))))
3371
3372 (defmacro gnus-article-mark (number)
3373   "Return the MARK of article NUMBER.
3374 This macro should only be used when computing the mark the \"first\"
3375 time; i.e., when generating the summary lines.  After that,
3376 `gnus-summary-article-mark' should be used to examine the
3377 marks of articles."
3378   `(cond
3379     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3380     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3381     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3382     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3383     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3384     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3385     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3386     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3387            gnus-ancient-mark))))
3388
3389 ;; Saving hidden threads.
3390
3391 (defmacro gnus-save-hidden-threads (&rest forms)
3392   "Save hidden threads, eval FORMS, and restore the hidden threads."
3393   (let ((config (make-symbol "config")))
3394     `(let ((,config (gnus-hidden-threads-configuration)))
3395        (unwind-protect
3396            (save-excursion
3397              ,@forms)
3398          (gnus-restore-hidden-threads-configuration ,config)))))
3399 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3400 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3401
3402 (defun gnus-data-compute-positions ()
3403   "Compute the positions of all articles."
3404   (setq gnus-newsgroup-data-reverse nil)
3405   (let ((data gnus-newsgroup-data))
3406     (save-excursion
3407       (gnus-save-hidden-threads
3408         (gnus-summary-show-all-threads)
3409         (goto-char (point-min))
3410         (while data
3411           (while (get-text-property (point) 'gnus-intangible)
3412             (forward-line 1))
3413           (gnus-data-set-pos (car data) (+ (point) 3))
3414           (setq data (cdr data))
3415           (forward-line 1))))))
3416
3417 (defun gnus-hidden-threads-configuration ()
3418   "Return the current hidden threads configuration."
3419   (save-excursion
3420     (let (config)
3421       (goto-char (point-min))
3422       (while (not (eobp))
3423         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3424           (push (save-excursion (forward-line 0) (point)) config))
3425         (forward-line 1))
3426       config)))
3427
3428 (defun gnus-restore-hidden-threads-configuration (config)
3429   "Restore hidden threads configuration from CONFIG."
3430   (save-excursion
3431     (let (point (inhibit-read-only t))
3432       (while (setq point (pop config))
3433         (goto-char point)
3434         (gnus-summary-hide-thread)))))
3435
3436 ;; Various summary mode internalish functions.
3437
3438 (defun gnus-mouse-pick-article (e)
3439   (interactive "e")
3440   (mouse-set-point e)
3441   (gnus-summary-next-page nil t))
3442
3443 (defun gnus-summary-set-display-table ()
3444   "Change the display table.
3445 Odd characters have a tendency to mess
3446 up nicely formatted displays - we make all possible glyphs
3447 display only a single character."
3448
3449   ;; We start from the standard display table, if any.
3450   (let ((table (or (copy-sequence standard-display-table)
3451                    (make-display-table)))
3452         (i 32))
3453     ;; Nix out all the control chars...
3454     (while (>= (setq i (1- i)) 0)
3455       (gnus-put-display-table i [??] table))
3456    ;; ... but not newline and cr, of course.  (cr is necessary for the
3457     ;; selective display).
3458     (gnus-put-display-table ?\n nil table)
3459     (gnus-put-display-table ?\r nil table)
3460     ;; We keep TAB as well.
3461     (gnus-put-display-table ?\t nil table)
3462     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3463     ;; Emacs 23 (unicode), that are not set already.
3464     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3465                  160
3466                256)))
3467       (while (>= (setq i (1- i)) 127)
3468         ;; Only modify if the entry is nil.
3469         (unless (gnus-get-display-table i table)
3470           (gnus-put-display-table i [??] table))))
3471     (setq buffer-display-table table)))
3472
3473 (defun gnus-summary-set-article-display-arrow (pos)
3474   "Update the overlay arrow to point to line at position POS."
3475   (when gnus-summary-display-arrow
3476     (make-local-variable 'overlay-arrow-position)
3477     (make-local-variable 'overlay-arrow-string)
3478     (save-excursion
3479       (goto-char pos)
3480       (beginning-of-line)
3481       (unless overlay-arrow-position
3482         (setq overlay-arrow-position (make-marker)))
3483       (setq overlay-arrow-string "=>"
3484             overlay-arrow-position (set-marker overlay-arrow-position
3485                                                (point)
3486                                                (current-buffer))))))
3487
3488 (defun gnus-summary-setup-buffer (group)
3489   "Initialize summary buffer.
3490 If the setup was successful, non-nil is returned."
3491   (let ((buffer (gnus-summary-buffer-name group))
3492         (dead-name (concat "*Dead Summary "
3493                            (gnus-group-decoded-name group) "*")))
3494     ;; If a dead summary buffer exists, we kill it.
3495     (when (gnus-buffer-live-p dead-name)
3496       (gnus-kill-buffer dead-name))
3497     (if (get-buffer buffer)
3498         (progn
3499           (set-buffer buffer)
3500           (setq gnus-summary-buffer (current-buffer))
3501           (not gnus-newsgroup-prepared))
3502       (set-buffer (gnus-get-buffer-create buffer))
3503       (setq gnus-summary-buffer (current-buffer))
3504       (gnus-summary-mode group)
3505       (when (gnus-group-quit-config group)
3506         (set (make-local-variable 'gnus-single-article-buffer) nil))
3507       (make-local-variable 'gnus-article-buffer)
3508       (make-local-variable 'gnus-article-current)
3509       (make-local-variable 'gnus-original-article-buffer)
3510       (setq gnus-newsgroup-name group)
3511       ;; Set any local variables in the group parameters.
3512       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3513       t)))
3514
3515 (defun gnus-set-global-variables ()
3516   "Set the global equivalents of the buffer-local variables.
3517 They are set to the latest values they had.  These reflect the summary
3518 buffer that was in action when the last article was fetched."
3519   (when (eq major-mode 'gnus-summary-mode)
3520     (setq gnus-summary-buffer (current-buffer))
3521     (let ((name gnus-newsgroup-name)
3522           (marked gnus-newsgroup-marked)
3523           (spam gnus-newsgroup-spam-marked)
3524           (unread gnus-newsgroup-unreads)
3525           (headers gnus-current-headers)
3526           (data gnus-newsgroup-data)
3527           (summary gnus-summary-buffer)
3528           (article-buffer gnus-article-buffer)
3529           (original gnus-original-article-buffer)
3530           (gac gnus-article-current)
3531           (reffed gnus-reffed-article-number)
3532           (score-file gnus-current-score-file)
3533           (default-charset gnus-newsgroup-charset)
3534           vlist)
3535       (let ((locals gnus-newsgroup-variables))
3536         (while locals
3537           (if (consp (car locals))
3538               (push (eval (caar locals)) vlist)
3539             (push (eval (car locals)) vlist))
3540           (setq locals (cdr locals)))
3541         (setq vlist (nreverse vlist)))
3542       (with-temp-buffer
3543         (setq gnus-newsgroup-name name
3544               gnus-newsgroup-marked marked
3545               gnus-newsgroup-spam-marked spam
3546               gnus-newsgroup-unreads unread
3547               gnus-current-headers headers
3548               gnus-newsgroup-data data
3549               gnus-article-current gac
3550               gnus-summary-buffer summary
3551               gnus-article-buffer article-buffer
3552               gnus-original-article-buffer original
3553               gnus-reffed-article-number reffed
3554               gnus-current-score-file score-file
3555               gnus-newsgroup-charset default-charset)
3556         (let ((locals gnus-newsgroup-variables))
3557           (while locals
3558             (if (consp (car locals))
3559                 (set (caar locals) (pop vlist))
3560               (set (car locals) (pop vlist)))
3561             (setq locals (cdr locals))))))))
3562
3563 (defun gnus-summary-article-unread-p (article)
3564   "Say whether ARTICLE is unread or not."
3565   (memq article gnus-newsgroup-unreads))
3566
3567 (defun gnus-summary-first-article-p (&optional article)
3568   "Return whether ARTICLE is the first article in the buffer."
3569   (if (not (setq article (or article (gnus-summary-article-number))))
3570       nil
3571     (eq article (caar gnus-newsgroup-data))))
3572
3573 (defun gnus-summary-last-article-p (&optional article)
3574   "Return whether ARTICLE is the last article in the buffer."
3575   (if (not (setq article (or article (gnus-summary-article-number))))
3576       ;; All non-existent numbers are the last article.  :-)
3577       t
3578     (not (cdr (gnus-data-find-list article)))))
3579
3580 (defun gnus-make-thread-indent-array (&optional n)
3581   (when (or n
3582             (progn (setq n 200) nil)
3583             (null gnus-thread-indent-array)
3584             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3585     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3586           gnus-thread-indent-array-level gnus-thread-indent-level)
3587     (while (>= n 0)
3588       (aset gnus-thread-indent-array n
3589             (make-string (* n gnus-thread-indent-level) ? ))
3590       (setq n (1- n)))))
3591
3592 (defun gnus-update-summary-mark-positions ()
3593   "Compute where the summary marks are to go."
3594   (save-excursion
3595     (when (gnus-buffer-exists-p gnus-summary-buffer)
3596       (set-buffer gnus-summary-buffer))
3597     (let ((spec gnus-summary-line-format-spec)
3598           pos)
3599       (save-excursion
3600         (gnus-set-work-buffer)
3601         (let ((gnus-tmp-unread ?Z)
3602               (gnus-replied-mark ?Z)
3603               (gnus-score-below-mark ?Z)
3604               (gnus-score-over-mark ?Z)
3605               (gnus-undownloaded-mark ?Z)
3606               (gnus-summary-line-format-spec spec)
3607               (gnus-newsgroup-downloadable '(0))
3608               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3609               case-fold-search ignores)
3610           ;; Here, all marks are bound to Z.
3611           (gnus-summary-insert-line header
3612                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3613           (goto-char (point-min))
3614           ;; Memorize the positions of the same characters as dummy marks.
3615           (while (re-search-forward "[A-D]" nil t)
3616             (push (point) ignores))
3617           (erase-buffer)
3618           ;; We use A-D as dummy marks in order to know column positions
3619           ;; where marks should be inserted.
3620           (setq gnus-tmp-unread ?A
3621                 gnus-replied-mark ?B
3622                 gnus-score-below-mark ?C
3623                 gnus-score-over-mark ?C
3624                 gnus-undownloaded-mark ?D)
3625           (gnus-summary-insert-line header
3626                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3627           ;; Ignore characters which aren't dummy marks.
3628           (dolist (p ignores)
3629             (delete-region (goto-char (1- p)) p)
3630             (insert ?Z))
3631           (goto-char (point-min))
3632           (setq pos (list (cons 'unread
3633                                 (and (search-forward "A" nil t)
3634                                      (- (point) (point-min) 1)))))
3635           (goto-char (point-min))
3636           (push (cons 'replied (and (search-forward "B" nil t)
3637                                     (- (point) (point-min) 1)))
3638                 pos)
3639           (goto-char (point-min))
3640           (push (cons 'score (and (search-forward "C" nil t)
3641                                   (- (point) (point-min) 1)))
3642                 pos)
3643           (goto-char (point-min))
3644           (push (cons 'download (and (search-forward "D" nil t)
3645                                      (- (point) (point-min) 1)))
3646                 pos)))
3647       (setq gnus-summary-mark-positions pos))))
3648
3649 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3650   "Insert a dummy root in the summary buffer."
3651   (beginning-of-line)
3652   (gnus-add-text-properties
3653    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3654    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3655
3656 (defun gnus-summary-extract-address-component (from)
3657   (or (car (funcall gnus-extract-address-components from))
3658       from))
3659
3660 (defun gnus-summary-from-or-to-or-newsgroups (header from)
3661   (let ((mail-parse-charset gnus-newsgroup-charset)
3662         (ignored-from-addresses (gnus-ignored-from-addresses))
3663         ;; Is it really necessary to do this next part for each summary line?
3664         ;; Luckily, doesn't seem to slow things down much.
3665         (mail-parse-ignored-charsets
3666          (with-current-buffer gnus-summary-buffer
3667            gnus-newsgroup-ignored-charsets))
3668         (address (cadr (gnus-extract-address-components from))))
3669     (or
3670      (and ignored-from-addresses
3671           (string-match ignored-from-addresses address)
3672           (let ((extra-headers (mail-header-extra header))
3673                 to
3674                 newsgroups)
3675             (cond
3676              ((setq to (cdr (assq 'To extra-headers)))
3677               (concat gnus-summary-to-prefix
3678                       (inline
3679                         (gnus-summary-extract-address-component
3680                          (funcall gnus-decode-encoded-address-function to)))))
3681              ((setq newsgroups
3682                     (or
3683                      (cdr (assq 'Newsgroups extra-headers))
3684                      (and
3685                       (memq 'Newsgroups gnus-extra-headers)
3686                       (eq (car (gnus-find-method-for-group
3687                                 gnus-newsgroup-name)) 'nntp)
3688                       (gnus-group-real-name gnus-newsgroup-name))))
3689               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3690      (gnus-string-mark-left-to-right (gnus-summary-extract-address-component from)))))
3691
3692 (defun gnus-summary-insert-line (gnus-tmp-header
3693                                  gnus-tmp-level gnus-tmp-current
3694                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3695                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3696                                  &optional gnus-tmp-dummy gnus-tmp-score
3697                                  gnus-tmp-process)
3698   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3699       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3700                                           gnus-tmp-level)))
3701   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3702          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3703          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3704          (gnus-tmp-score-char
3705           (if (or (null gnus-summary-default-score)
3706                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3707                       gnus-summary-zcore-fuzz))
3708               ?                         ;Whitespace
3709             (if (< gnus-tmp-score gnus-summary-default-score)
3710                 gnus-score-below-mark gnus-score-over-mark)))
3711          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3712          (gnus-tmp-replied
3713           (cond (gnus-tmp-process gnus-process-mark)
3714                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3715                  gnus-cached-mark)
3716                 (gnus-tmp-replied gnus-replied-mark)
3717                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3718                  gnus-forwarded-mark)
3719                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3720                  gnus-saved-mark)
3721                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3722                  gnus-unseen-mark)
3723                 (t gnus-no-mark)))
3724          (gnus-tmp-downloaded
3725           (cond (undownloaded
3726                  gnus-undownloaded-mark)
3727                 (gnus-newsgroup-agentized
3728                  gnus-downloaded-mark)
3729                 (t
3730                  gnus-no-mark)))
3731          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3732          (gnus-tmp-name
3733           (cond
3734            ((string-match "<[^>]+> *$" gnus-tmp-from)
3735             (let ((beg (match-beginning 0)))
3736               (or (and (string-match "^\".+\"" gnus-tmp-from)
3737                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3738                   (substring gnus-tmp-from 0 beg))))
3739            ((string-match "(.+)" gnus-tmp-from)
3740             (substring gnus-tmp-from
3741                        (1+ (match-beginning 0)) (1- (match-end 0))))
3742            (t gnus-tmp-from)))
3743          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3744          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3745          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3746          (inhibit-read-only t))
3747     (when (string= gnus-tmp-name "")
3748       (setq gnus-tmp-name gnus-tmp-from))
3749     (unless (numberp gnus-tmp-lines)
3750       (setq gnus-tmp-lines -1))
3751     (if (= gnus-tmp-lines -1)
3752         (setq gnus-tmp-lines "?")
3753       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3754     (condition-case ()
3755         (gnus-put-text-property
3756          (point)
3757          (progn (eval gnus-summary-line-format-spec) (point))
3758          'gnus-number gnus-tmp-number)
3759       (error (gnus-message 5 "Error updating the summary line")))
3760     (when (gnus-visual-p 'summary-highlight 'highlight)
3761       (forward-line -1)
3762       (gnus-summary-highlight-line)
3763       (gnus-run-hooks 'gnus-summary-update-hook)
3764       (forward-line 1))))
3765
3766 (defun gnus-summary-update-line (&optional dont-update)
3767   "Update summary line after change."
3768   (when (and gnus-summary-default-score
3769              (not gnus-summary-inhibit-highlight))
3770     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3771            (article (gnus-summary-article-number))
3772            (score (gnus-summary-article-score article)))
3773       (unless dont-update
3774         (if (and gnus-summary-mark-below
3775                  (< (gnus-summary-article-score)
3776                     gnus-summary-mark-below))
3777             ;; This article has a low score, so we mark it as read.
3778             (when (memq article gnus-newsgroup-unreads)
3779               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3780           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3781             ;; This article was previously marked as read on account
3782             ;; of a low score, but now it has risen, so we mark it as
3783             ;; unread.
3784             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3785         (gnus-summary-update-mark
3786          (if (or (null gnus-summary-default-score)
3787                  (<= (abs (- score gnus-summary-default-score))
3788                      gnus-summary-zcore-fuzz))
3789              ?                          ;Whitespace
3790            (if (< score gnus-summary-default-score)
3791                gnus-score-below-mark gnus-score-over-mark))
3792          'score))
3793       ;; Do visual highlighting.
3794       (when (gnus-visual-p 'summary-highlight 'highlight)
3795         (gnus-summary-highlight-line)
3796         (gnus-run-hooks 'gnus-summary-update-hook)))))
3797
3798 (defvar gnus-tmp-new-adopts nil)
3799
3800 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3801   "Return the number of articles in THREAD.
3802 This may be 0 in some cases -- if none of the articles in
3803 the thread are to be displayed."
3804   (let* ((number
3805          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3806           (cond
3807            ((not (listp thread))
3808             1)
3809            ((and (consp thread) (cdr thread))
3810             (apply
3811              '+ 1 (mapcar
3812                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3813            ((null thread)
3814             1)
3815            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3816             1)
3817            (t 0))))
3818     (when (and level (zerop level) gnus-tmp-new-adopts)
3819       (incf number
3820             (apply '+ (mapcar
3821                        'gnus-summary-number-of-articles-in-thread
3822                        gnus-tmp-new-adopts))))
3823     (if char
3824         (if (> number 1) gnus-not-empty-thread-mark
3825           gnus-empty-thread-mark)
3826       number)))
3827
3828 (defsubst gnus-summary-line-message-size (head)
3829   "Return pretty-printed version of message size.
3830 This function is intended to be used in
3831 `gnus-summary-line-format-alist'."
3832   (let ((c (or (mail-header-chars head) -1)))
3833     (cond ((< c 0) "n/a")               ; chars not available
3834           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3835           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3836           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3837           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3838
3839 (defcustom gnus-user-date-format-alist
3840   '(((gnus-seconds-today) . "Today, %H:%M")
3841     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3842     (604800 . "%A %H:%M")               ; That's one week
3843     ((gnus-seconds-month) . "%A %d")
3844     ((gnus-seconds-year) . "%B %d")
3845     (t . "%b %d %Y"))                   ; This one is used when no other
3846                                         ; does match
3847   "Specifies date format depending on age of article.
3848 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3849 seconds) or a Lisp expression evaluating to a number.  When the age of
3850 the article is less than this number, then use `format-time-string'
3851 with the corresponding FORMAT for displaying the date of the article.
3852 If AGE is not a number or a Lisp expression evaluating to a
3853 non-number, then the corresponding FORMAT is used as a default value.
3854
3855 Note that the list is processed from the beginning, so it should be
3856 sorted by ascending AGE.  Also note that items following the first
3857 non-number AGE will be ignored.
3858
3859 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3860 and `gnus-seconds-year' in the AGE spec.  They return the number of
3861 seconds passed since the start of today, of this month, of this year,
3862 respectively."
3863   :version "24.1"
3864   :group 'gnus-summary-format
3865   :type '(alist :key-type sexp :value-type string))
3866
3867 (defun gnus-user-date (messy-date)
3868   "Format the messy-date according to `gnus-user-date-format-alist'.
3869 Returns \"  ?  \" if there's bad input or if another error occurs.
3870 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3871   (condition-case ()
3872       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3873              (now (gnus-float-time))
3874              ;;If we don't find something suitable we'll use this one
3875              (my-format "%b %d '%y"))
3876         (let* ((difference (- now messy-date))
3877                (templist gnus-user-date-format-alist)
3878                (top (eval (caar templist))))
3879           (while (if (numberp top) (< top difference) (not top))
3880             (progn
3881               (setq templist (cdr templist))
3882               (setq top (eval (caar templist)))))
3883           (if (stringp (cdr (car templist)))
3884               (setq my-format (cdr (car templist)))))
3885         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3886     (error "  ?   ")))
3887
3888 (defun gnus-summary-set-local-parameters (group)
3889   "Go through the local params of GROUP and set all variable specs in that list."
3890   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3891                                         ; really variables.
3892     (dolist (elem (gnus-group-find-parameter group))
3893       (and (consp elem)                 ; Has to be a cons.
3894            (consp (cdr elem))           ; The cdr has to be a list.
3895            (symbolp (car elem))         ; Has to be a symbol in there.
3896            (not (memq (car elem) vars))
3897            (ignore-errors
3898              (push (car elem) vars)
3899              ;; Variables like `gnus-show-threads' that are globally
3900              ;; bound, if used as group parameters, need to get to be
3901              ;; buffer-local, whereas just parameters like `gcc-self',
3902              ;; `timestamp', etc. should not be bound as variables.
3903              (if (boundp (car elem))
3904                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3905                (eval (nth 1 elem))))))))
3906
3907 (defun gnus-summary-read-group (group &optional show-all no-article
3908                                       kill-buffer no-display backward
3909                                       select-articles)
3910   "Start reading news in newsgroup GROUP.
3911 If SHOW-ALL is non-nil, already read articles are also listed.
3912 If NO-ARTICLE is non-nil, no article is selected initially.
3913 If NO-DISPLAY, don't generate the summary buffer contents.
3914 If KILL-BUFFER, it should be a buffer that's killed once the new
3915 summary buffer has been generated.
3916 If BACKWARD, move point to the previous group in the group buffer
3917 If SELECT-ARTICLES, only select those articles from GROUP."
3918   (let (result)
3919     (while (and group
3920                 (null (setq result
3921                             (let ((gnus-auto-select-next nil))
3922                               (or (gnus-summary-read-group-1
3923                                    group show-all no-article
3924                                    kill-buffer no-display
3925                                    select-articles)
3926                                   (setq show-all nil
3927                                         select-articles nil)))))
3928                 (eq gnus-auto-select-next 'quietly))
3929       (set-buffer gnus-group-buffer)
3930       ;; The entry function called above goes to the next
3931       ;; group automatically, so we go two groups back
3932       ;; if we are searching for the previous group.
3933       (when backward
3934         (gnus-group-prev-unread-group 2))
3935       (if (not (equal group (gnus-group-group-name)))
3936           (setq group (gnus-group-group-name))
3937         (setq group nil)))
3938     result))
3939
3940 (defun gnus-summary-read-group-1 (group show-all no-article
3941                                         kill-buffer no-display
3942                                         &optional select-articles)
3943   ;; Killed foreign groups can't be entered.
3944   ;;  (when (and (not (gnus-group-native-p group))
3945   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3946   ;;    (error "Dead non-native groups can't be entered"))
3947   (gnus-message 7 "Retrieving newsgroup: %s..."
3948                 (gnus-group-decoded-name group))
3949   (let* ((new-group (gnus-summary-setup-buffer group))
3950          (quit-config (gnus-group-quit-config group))
3951          (did-select (and new-group (gnus-select-newsgroup
3952                                      group show-all select-articles))))
3953     (cond
3954      ;; This summary buffer exists already, so we just select it.
3955      ((not new-group)
3956       (gnus-set-global-variables)
3957       (when kill-buffer
3958         (gnus-kill-or-deaden-summary kill-buffer))
3959       (gnus-configure-windows 'summary 'force)
3960       (gnus-set-mode-line 'summary)
3961       (gnus-summary-position-point)
3962       (message "")
3963       t)
3964      ;; We couldn't select this group.
3965      ((null did-select)
3966       (when (and (eq major-mode 'gnus-summary-mode)
3967                  (not (equal (current-buffer) kill-buffer)))
3968         (kill-buffer (current-buffer))
3969         (if (not quit-config)
3970             (progn
3971               ;; Update the info -- marks might need to be removed,
3972               ;; for instance.
3973               (gnus-summary-update-info)
3974               (set-buffer gnus-group-buffer)
3975               (gnus-group-jump-to-group group)
3976               (gnus-group-next-unread-group 1))
3977           (gnus-handle-ephemeral-exit quit-config)))
3978       (if (null (gnus-list-of-unread-articles group))
3979           (gnus-message 3 "Group %s contains no messages" group)
3980         (gnus-message 3 "Can't select group"))
3981       nil)
3982      ;; The user did a `C-g' while prompting for number of articles,
3983      ;; so we exit this group.
3984      ((eq did-select 'quit)
3985       (and (eq major-mode 'gnus-summary-mode)
3986            (not (equal (current-buffer) kill-buffer))
3987            (kill-buffer (current-buffer)))
3988       (when kill-buffer
3989         (gnus-kill-or-deaden-summary kill-buffer))
3990       (if (not quit-config)
3991           (progn
3992             (set-buffer gnus-group-buffer)
3993             (gnus-group-jump-to-group group)
3994             (gnus-configure-windows 'group 'force))
3995         (gnus-handle-ephemeral-exit quit-config))
3996       ;; Finally signal the quit.
3997       (signal 'quit nil))
3998      ;; The group was successfully selected.
3999      (t
4000       (gnus-set-global-variables)
4001       ;; Save the active value in effect when the group was entered.
4002       (setq gnus-newsgroup-active
4003             (gnus-copy-sequence
4004              (gnus-active gnus-newsgroup-name)))
4005       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4006       ;; You can change the summary buffer in some way with this hook.
4007       (gnus-run-hooks 'gnus-select-group-hook)
4008       (when (memq 'summary (gnus-update-format-specifications
4009                             nil 'summary 'summary-mode 'summary-dummy))
4010         ;; The format specification for the summary line was updated,
4011         ;; so we need to update the mark positions as well.
4012         (gnus-update-summary-mark-positions))
4013       ;; Do score processing.
4014       (when gnus-use-scoring
4015         (gnus-possibly-score-headers))
4016       ;; Check whether to fill in the gaps in the threads.
4017       (when gnus-build-sparse-threads
4018         (gnus-build-sparse-threads))
4019       ;; Find the initial limit.
4020       (if show-all
4021           (let ((gnus-newsgroup-dormant nil))
4022             (gnus-summary-initial-limit show-all))
4023         (gnus-summary-initial-limit show-all))
4024       ;; Generate the summary buffer.
4025       (unless no-display
4026         (gnus-summary-prepare))
4027       (when gnus-use-trees
4028         (gnus-tree-open group)
4029         (setq gnus-summary-highlight-line-function
4030               'gnus-tree-highlight-article))
4031       ;; If the summary buffer is empty, but there are some low-scored
4032       ;; articles or some excluded dormants, we include these in the
4033       ;; buffer.
4034       (when (and (zerop (buffer-size))
4035                  (not no-display))
4036         (cond (gnus-newsgroup-dormant
4037                (gnus-summary-limit-include-dormant))
4038               ((and gnus-newsgroup-scored show-all)
4039                (gnus-summary-limit-include-expunged t))))
4040       ;; Function `gnus-apply-kill-file' must be called in this hook.
4041       (gnus-run-hooks 'gnus-apply-kill-hook)
4042       (if (and (zerop (buffer-size))
4043                (not no-display))
4044           (progn
4045             ;; This newsgroup is empty.
4046             (gnus-summary-catchup-and-exit nil t)
4047             (gnus-message 6 "No unread news")
4048             (when kill-buffer
4049               (gnus-kill-or-deaden-summary kill-buffer))
4050             ;; Return nil from this function.
4051             nil)
4052         ;; Hide conversation thread subtrees.  We cannot do this in
4053         ;; gnus-summary-prepare-hook since kill processing may not
4054         ;; work with hidden articles.
4055         (gnus-summary-maybe-hide-threads)
4056         (gnus-configure-windows 'summary)
4057         (when kill-buffer
4058           (gnus-kill-or-deaden-summary kill-buffer))
4059         (gnus-summary-auto-select-subject)
4060         ;; Show first unread article if requested.
4061         (if (and (not no-article)
4062                  (not no-display)
4063                  gnus-newsgroup-unreads
4064                  gnus-auto-select-first)
4065             (progn
4066               (let ((art (gnus-summary-article-number)))
4067                 (when (and art
4068                            gnus-plugged
4069                            (not (memq art gnus-newsgroup-undownloaded))
4070                            (not (memq art gnus-newsgroup-downloadable)))
4071                   (gnus-summary-goto-article art))))
4072           ;; Don't select any articles.
4073           (gnus-summary-position-point)
4074           (gnus-configure-windows 'summary 'force)
4075           (gnus-set-mode-line 'summary))
4076         (when (and gnus-auto-center-group
4077                    (get-buffer-window gnus-group-buffer t))
4078           ;; Gotta use windows, because recenter does weird stuff if
4079           ;; the current buffer ain't the displayed window.
4080           (let ((owin (selected-window)))
4081             (select-window (get-buffer-window gnus-group-buffer t))
4082             (when (gnus-group-goto-group group)
4083               (recenter))
4084             (select-window owin)))
4085         ;; Mark this buffer as "prepared".
4086         (setq gnus-newsgroup-prepared t)
4087         (gnus-run-hooks 'gnus-summary-prepared-hook)
4088         (unless (gnus-ephemeral-group-p group)
4089           (gnus-group-update-group group nil t))
4090         t)))))
4091
4092 (defun gnus-summary-auto-select-subject ()
4093   "Select the subject line on initial group entry."
4094   (goto-char (point-min))
4095   (cond
4096    ((eq gnus-auto-select-subject 'best)
4097     (gnus-summary-best-unread-subject))
4098    ((eq gnus-auto-select-subject 'unread)
4099     (gnus-summary-first-unread-subject))
4100    ((eq gnus-auto-select-subject 'unseen)
4101     (gnus-summary-first-unseen-subject))
4102    ((eq gnus-auto-select-subject 'unseen-or-unread)
4103     (gnus-summary-first-unseen-or-unread-subject))
4104    ((eq gnus-auto-select-subject 'first)
4105     ;; Do nothing.
4106     )
4107    ((functionp gnus-auto-select-subject)
4108     (funcall gnus-auto-select-subject))))
4109
4110 (defun gnus-summary-prepare ()
4111   "Generate the summary buffer."
4112   (interactive)
4113   (let ((inhibit-read-only t))
4114     (erase-buffer)
4115     (setq gnus-newsgroup-data nil
4116           gnus-newsgroup-data-reverse nil)
4117     (gnus-run-hooks 'gnus-summary-generate-hook)
4118     ;; Generate the buffer, either with threads or without.
4119     (when gnus-newsgroup-headers
4120       (gnus-summary-prepare-threads
4121        (if gnus-show-threads
4122            (gnus-sort-gathered-threads
4123             (funcall gnus-summary-thread-gathering-function
4124                      (gnus-sort-threads
4125                       (gnus-cut-threads (gnus-make-threads)))))
4126          ;; Unthreaded display.
4127          (gnus-sort-articles gnus-newsgroup-headers))))
4128     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4129     ;; Call hooks for modifying summary buffer.
4130     (goto-char (point-min))
4131     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4132
4133 (defsubst gnus-general-simplify-subject (subject)
4134   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4135   (setq subject
4136         (cond
4137          ;; Truncate the subject.
4138          (gnus-simplify-subject-functions
4139           (gnus-map-function gnus-simplify-subject-functions subject))
4140          ((numberp gnus-summary-gather-subject-limit)
4141           (setq subject (gnus-simplify-subject-re subject))
4142           (if (> (length subject) gnus-summary-gather-subject-limit)
4143               (substring subject 0 gnus-summary-gather-subject-limit)
4144             subject))
4145          ;; Fuzzily simplify it.
4146          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4147           (gnus-simplify-subject-fuzzy subject))
4148          ;; Just remove the leading "Re:".
4149          (t
4150           (gnus-simplify-subject-re subject))))
4151
4152   (if (and gnus-summary-gather-exclude-subject
4153            (string-match gnus-summary-gather-exclude-subject subject))
4154       nil                         ; This article shouldn't be gathered
4155     subject))
4156
4157 (defun gnus-summary-simplify-subject-query ()
4158   "Query where the respool algorithm would put this article."
4159   (interactive)
4160   (gnus-summary-select-article)
4161   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4162
4163 (defun gnus-gather-threads-by-subject (threads)
4164   "Gather threads by looking at Subject headers."
4165   (if (not gnus-summary-make-false-root)
4166       threads
4167     (let ((hashtb (gnus-make-hashtable 1024))
4168           (prev threads)
4169           (result threads)
4170           subject hthread whole-subject)
4171       (while threads
4172         (setq subject (gnus-general-simplify-subject
4173                        (setq whole-subject (mail-header-subject
4174                                             (caar threads)))))
4175         (when subject
4176           (if (setq hthread (gnus-gethash subject hashtb))
4177               (progn
4178                 ;; We enter a dummy root into the thread, if we
4179                 ;; haven't done that already.
4180                 (unless (stringp (caar hthread))
4181                   (setcar hthread (list whole-subject (car hthread))))
4182                 ;; We add this new gathered thread to this gathered
4183                 ;; thread.
4184                 (setcdr (car hthread)
4185                         (nconc (cdar hthread) (list (car threads))))
4186                 ;; Remove it from the list of threads.
4187                 (setcdr prev (cdr threads))
4188                 (setq threads prev))
4189             ;; Enter this thread into the hash table.
4190             (gnus-sethash subject
4191                           (if gnus-summary-make-false-root-always
4192                               (progn
4193                                 ;; If you want a dummy root above all
4194                                 ;; threads...
4195                                 (setcar threads (list whole-subject
4196                                                       (car threads)))
4197                                 threads)
4198                             threads)
4199                           hashtb)))
4200         (setq prev threads)
4201         (setq threads (cdr threads)))
4202       result)))
4203
4204 (defun gnus-gather-threads-by-references (threads)
4205   "Gather threads by looking at References headers."
4206   (let ((idhashtb (gnus-make-hashtable 1024))
4207         (thhashtb (gnus-make-hashtable 1024))
4208         (prev threads)
4209         (result threads)
4210         ids references id gthread gid entered ref)
4211     (while threads
4212       (when (setq references (mail-header-references (caar threads)))
4213         (setq id (mail-header-id (caar threads))
4214               ids (inline (gnus-split-references references))
4215               entered nil)
4216         (while (setq ref (pop ids))
4217           (setq ids (delete ref ids))
4218           (if (not (setq gid (gnus-gethash ref idhashtb)))
4219               (progn
4220                 (gnus-sethash ref id idhashtb)
4221                 (gnus-sethash id threads thhashtb))
4222             (setq gthread (gnus-gethash gid thhashtb))
4223             (unless entered
4224               ;; We enter a dummy root into the thread, if we
4225               ;; haven't done that already.
4226               (unless (stringp (caar gthread))
4227                 (setcar gthread (list (mail-header-subject (caar gthread))
4228                                       (car gthread))))
4229               ;; We add this new gathered thread to this gathered
4230               ;; thread.
4231               (setcdr (car gthread)
4232                       (nconc (cdar gthread) (list (car threads)))))
4233             ;; Add it into the thread hash table.
4234             (gnus-sethash id gthread thhashtb)
4235             (setq entered t)
4236             ;; Remove it from the list of threads.
4237             (setcdr prev (cdr threads))
4238             (setq threads prev))))
4239       (setq prev threads)
4240       (setq threads (cdr threads)))
4241     result))
4242
4243 (defun gnus-sort-gathered-threads (threads)
4244   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4245   (let ((result threads))
4246     (while threads
4247       (when (stringp (caar threads))
4248         (setcdr (car threads)
4249                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4250       (setq threads (cdr threads)))
4251     result))
4252
4253 (defun gnus-thread-loop-p (root thread)
4254   "Say whether ROOT is in THREAD."
4255   (let ((stack (list thread))
4256         (infloop 0)
4257         th)
4258     (while (setq thread (pop stack))
4259       (setq th (cdr thread))
4260       (while (and th
4261                   (not (eq (caar th) root)))
4262         (pop th))
4263       (if th
4264           ;; We have found a loop.
4265           (let (ref-dep)
4266             (setcdr thread (delq (car th) (cdr thread)))
4267             (if (boundp (setq ref-dep (intern "none"
4268                                               gnus-newsgroup-dependencies)))
4269                 (setcdr (symbol-value ref-dep)
4270                         (nconc (cdr (symbol-value ref-dep))
4271                                (list (car th))))
4272               (set ref-dep (list nil (car th))))
4273             (setq infloop 1
4274                   stack nil))
4275         ;; Push all the subthreads onto the stack.
4276         (push (cdr thread) stack)))
4277     infloop))
4278
4279 (defun gnus-make-threads ()
4280   "Go through the dependency hashtb and find the roots.  Return all threads."
4281   (let (threads)
4282     (while (catch 'infloop
4283              (mapatoms
4284               (lambda (refs)
4285                 ;; Deal with self-referencing References loops.
4286                 (when (and (car (symbol-value refs))
4287                            (not (zerop
4288                                  (apply
4289                                   '+
4290                                   (mapcar
4291                                    (lambda (thread)
4292                                      (gnus-thread-loop-p
4293                                       (car (symbol-value refs)) thread))
4294                                    (cdr (symbol-value refs)))))))
4295                   (setq threads nil)
4296                   (throw 'infloop t))
4297                 (unless (car (symbol-value refs))
4298                   ;; These threads do not refer back to any other
4299                   ;; articles, so they're roots.
4300                   (setq threads (append (cdr (symbol-value refs)) threads))))
4301               gnus-newsgroup-dependencies)))
4302     threads))
4303
4304 ;; Build the thread tree.
4305 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4306   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4307
4308 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4309 if it was already present.
4310
4311 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4312 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4313 Message-IDs will be renamed to a unique Message-ID before being
4314 entered.
4315
4316 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4317   (let* ((id (mail-header-id header))
4318          (id-dep (and id (intern id dependencies)))
4319          parent-id ref ref-dep ref-header replaced)
4320     ;; Enter this `header' in the `dependencies' table.
4321     (cond
4322      ((not id-dep)
4323       (setq header nil))
4324      ;; The first two cases do the normal part: enter a new `header'
4325      ;; in the `dependencies' table.
4326      ((not (boundp id-dep))
4327       (set id-dep (list header)))
4328      ((null (car (symbol-value id-dep)))
4329       (setcar (symbol-value id-dep) header))
4330
4331      ;; From here the `header' was already present in the
4332      ;; `dependencies' table.
4333      (force-new
4334       ;; Overrides an existing entry;
4335       ;; just set the header part of the entry.
4336       (setcar (symbol-value id-dep) header)
4337       (setq replaced t))
4338
4339      ;; Renames the existing `header' to a unique Message-ID.
4340      ((not gnus-summary-ignore-duplicates)
4341       ;; An article with this Message-ID has already been seen.
4342       ;; We rename the Message-ID.
4343       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4344            (list header))
4345       (mail-header-set-id header id))
4346
4347      ;; The last case ignores an existing entry, except it adds any
4348      ;; additional Xrefs (in case the two articles came from different
4349      ;; servers.
4350      ;; Also sets `header' to `nil' meaning that the `dependencies'
4351      ;; table was *not* modified.
4352      (t
4353       (mail-header-set-xref
4354        (car (symbol-value id-dep))
4355        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4356                    "")
4357                (or (mail-header-xref header) "")))
4358       (setq header nil)))
4359
4360     (when (and header (not replaced))
4361       ;; First check that we are not creating a References loop.
4362       (setq parent-id (gnus-parent-id (mail-header-references header)))
4363       (setq ref parent-id)
4364       (while (and ref
4365                   (setq ref-dep (intern-soft ref dependencies))
4366                   (boundp ref-dep)
4367                   (setq ref-header (car (symbol-value ref-dep))))
4368         (if (string= id ref)
4369             ;; Yuk!  This is a reference loop.  Make the article be a
4370             ;; root article.
4371             (progn
4372               (mail-header-set-references (car (symbol-value id-dep)) "none")
4373               (setq ref nil)
4374               (setq parent-id nil))
4375           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4376       (setq ref-dep (intern (or parent-id "none") dependencies))
4377       (if (boundp ref-dep)
4378           (setcdr (symbol-value ref-dep)
4379                   (nconc (cdr (symbol-value ref-dep))
4380                          (list (symbol-value id-dep))))
4381         (set ref-dep (list nil (symbol-value id-dep)))))
4382     header))
4383
4384 (defun gnus-extract-message-id-from-in-reply-to (string)
4385   (if (string-match "<[^>]+>" string)
4386       (substring string (match-beginning 0) (match-end 0))
4387     nil))
4388
4389 (defun gnus-build-sparse-threads ()
4390   (let ((headers gnus-newsgroup-headers)
4391         (mail-parse-charset gnus-newsgroup-charset)
4392         (gnus-summary-ignore-duplicates t)
4393         header references generation relations
4394         subject child end new-child date)
4395     ;; First we create an alist of generations/relations, where
4396     ;; generations is how much we trust the relation, and the relation
4397     ;; is parent/child.
4398     (gnus-message 7 "Making sparse threads...")
4399     (save-excursion
4400       (nnheader-set-temp-buffer " *gnus sparse threads*")
4401       (while (setq header (pop headers))
4402         (when (and (setq references (mail-header-references header))
4403                    (not (string= references "")))
4404           (insert references)
4405           (setq child (mail-header-id header)
4406                 subject (mail-header-subject header)
4407                 date (mail-header-date header)
4408                 generation 0)
4409           (while (search-backward ">" nil t)
4410             (setq end (1+ (point)))
4411             (when (search-backward "<" nil t)
4412               (setq new-child (buffer-substring (point) end))
4413               (push (list (incf generation)
4414                           child (setq child new-child)
4415                           subject date)
4416                     relations)))
4417           (when child
4418             (push (list (1+ generation) child nil subject) relations))
4419           (erase-buffer)))
4420       (kill-buffer (current-buffer)))
4421     ;; Sort over trustworthiness.
4422     (dolist (relation (sort relations 'car-less-than-car))
4423       (when (gnus-dependencies-add-header
4424              (make-full-mail-header
4425               gnus-reffed-article-number
4426               (nth 3 relation) "" (or (nth 4 relation) "")
4427               (nth 1 relation)
4428               (or (nth 2 relation) "") 0 0 "")
4429              gnus-newsgroup-dependencies nil)
4430         (push gnus-reffed-article-number gnus-newsgroup-limit)
4431         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4432         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4433               gnus-newsgroup-reads)
4434         (decf gnus-reffed-article-number)))
4435     (gnus-message 7 "Making sparse threads...done")))
4436
4437 (defun gnus-build-old-threads ()
4438   ;; Look at all the articles that refer back to old articles, and
4439   ;; fetch the headers for the articles that aren't there.  This will
4440   ;; build complete threads - if the roots haven't been expired by the
4441   ;; server, that is.
4442   (let ((mail-parse-charset gnus-newsgroup-charset)
4443         id heads)
4444     (mapatoms
4445      (lambda (refs)
4446        (when (not (car (symbol-value refs)))
4447          (setq heads (cdr (symbol-value refs)))
4448          (while heads
4449            (if (memq (mail-header-number (caar heads))
4450                      gnus-newsgroup-dormant)
4451                (setq heads (cdr heads))
4452              (setq id (symbol-name refs))
4453              (while (and (setq id (gnus-build-get-header id))
4454                          (not (car (gnus-id-to-thread id)))))
4455              (setq heads nil)))))
4456      gnus-newsgroup-dependencies)))
4457
4458 (defsubst gnus-remove-odd-characters (string)
4459   "Translate STRING into something that doesn't contain weird characters."
4460   (mm-subst-char-in-string
4461    ?\r ?\-
4462    (mm-subst-char-in-string ?\n ?\- string t) t))
4463
4464 ;; This function has to be called with point after the article number
4465 ;; on the beginning of the line.
4466 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4467   (let ((eol (point-at-eol))
4468         (buffer (current-buffer))
4469         header references in-reply-to)
4470
4471     ;; overview: [num subject from date id refs chars lines misc]
4472     (unwind-protect
4473         (let (x)
4474           (narrow-to-region (point) eol)
4475           (unless (eobp)
4476             (forward-char))
4477
4478           (setq header
4479                 (make-full-mail-header
4480                  number                 ; number
4481                  (condition-case ()     ; subject
4482                      (gnus-remove-odd-characters
4483                       (funcall gnus-decode-encoded-word-function
4484                                (setq x (nnheader-nov-field))))
4485                    (error x))
4486                  (condition-case ()     ; from
4487                      (gnus-remove-odd-characters
4488                       (funcall gnus-decode-encoded-address-function
4489                                (setq x (nnheader-nov-field))))
4490                    (error x))
4491                  (nnheader-nov-field)   ; date
4492                  (nnheader-nov-read-message-id number)  ; id
4493                  (setq references (nnheader-nov-field)) ; refs
4494                  (nnheader-nov-read-integer) ; chars
4495                  (nnheader-nov-read-integer) ; lines
4496                  (unless (eobp)
4497                    (if (looking-at "Xref: ")
4498                        (goto-char (match-end 0)))
4499                    (nnheader-nov-field)) ; Xref
4500                  (nnheader-nov-parse-extra)))) ; extra
4501
4502       (widen))
4503
4504     (when (and (string= references "")
4505                (setq in-reply-to (mail-header-extra header))
4506                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4507       (mail-header-set-references
4508        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4509
4510     (when gnus-alter-header-function
4511       (funcall gnus-alter-header-function header))
4512     (gnus-dependencies-add-header header dependencies force-new)))
4513
4514 (defun gnus-build-get-header (id)
4515   "Look through the buffer of NOV lines and find the header to ID.
4516 Enter this line into the dependencies hash table, and return
4517 the id of the parent article (if any)."
4518   (let ((deps gnus-newsgroup-dependencies)
4519         found header)
4520     (prog1
4521         (with-current-buffer nntp-server-buffer
4522           (let ((case-fold-search nil))
4523             (goto-char (point-min))
4524             (while (and (not found)
4525                         (search-forward id nil t))
4526               (beginning-of-line)
4527               (setq found (looking-at
4528                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4529                                    (regexp-quote id))))
4530               (or found (beginning-of-line 2)))
4531             (when found
4532               (beginning-of-line)
4533               (and
4534                (setq header (gnus-nov-parse-line
4535                              (read (current-buffer)) deps))
4536                (gnus-parent-id (mail-header-references header))))))
4537       (when header
4538         (let ((number (mail-header-number header)))
4539           (push number gnus-newsgroup-limit)
4540           (push header gnus-newsgroup-headers)
4541           (if (memq number gnus-newsgroup-unselected)
4542               (progn
4543                 (setq gnus-newsgroup-unreads
4544                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4545                                                number))
4546                 (setq gnus-newsgroup-unselected
4547                       (delq number gnus-newsgroup-unselected)))
4548             (push number gnus-newsgroup-ancient)))))))
4549
4550 (defun gnus-build-all-threads ()
4551   "Read all the headers."
4552   (let ((gnus-summary-ignore-duplicates t)
4553         (mail-parse-charset gnus-newsgroup-charset)
4554         (dependencies gnus-newsgroup-dependencies)
4555         header article)
4556     (with-current-buffer nntp-server-buffer
4557       (let ((case-fold-search nil))
4558         (goto-char (point-min))
4559         (while (not (eobp))
4560           (ignore-errors
4561             (setq article (read (current-buffer))
4562                   header (gnus-nov-parse-line article dependencies t)))
4563           (when header
4564             (with-current-buffer gnus-summary-buffer
4565               (push header gnus-newsgroup-headers)
4566               (if (memq (setq article (mail-header-number header))
4567                         gnus-newsgroup-unselected)
4568                   (progn
4569                     (setq gnus-newsgroup-unreads
4570                           (gnus-add-to-sorted-list
4571                            gnus-newsgroup-unreads article))
4572                     (setq gnus-newsgroup-unselected
4573                           (delq article gnus-newsgroup-unselected)))
4574                 (push article gnus-newsgroup-ancient)))
4575             (forward-line 1)))))))
4576
4577 (defun gnus-summary-update-article-line (article header)
4578   "Update the line for ARTICLE using HEADER."
4579   (let* ((id (mail-header-id header))
4580          (thread (gnus-id-to-thread id)))
4581     (unless thread
4582       (error "Article in no thread"))
4583     ;; Update the thread.
4584     (setcar thread header)
4585     (gnus-summary-goto-subject article)
4586     (let* ((datal (gnus-data-find-list article))
4587            (data (car datal))
4588            (inhibit-read-only t)
4589            (level (gnus-summary-thread-level)))
4590       (gnus-delete-line)
4591       (let ((inserted (- (point)
4592                          (progn
4593                            (gnus-summary-insert-line
4594                             header level nil
4595                             (memq article gnus-newsgroup-undownloaded)
4596                             (gnus-article-mark article)
4597                             (memq article gnus-newsgroup-replied)
4598                             (memq article gnus-newsgroup-expirable)
4599                             ;; Only insert the Subject string when it's different
4600                             ;; from the previous Subject string.
4601                             (if (and
4602                                  gnus-show-threads
4603                                  (gnus-subject-equal
4604                                   (condition-case ()
4605                                       (mail-header-subject
4606                                        (gnus-data-header
4607                                         (cadr
4608                                          (gnus-data-find-list
4609                                           article
4610                                           (gnus-data-list t)))))
4611                                     ;; Error on the side of excessive subjects.
4612                                     (error ""))
4613                                   (mail-header-subject header)))
4614                                 ""
4615                               (mail-header-subject header))
4616                             nil (cdr (assq article gnus-newsgroup-scored))
4617                             (memq article gnus-newsgroup-processable))
4618                            (point)))))
4619         (when (cdr datal)
4620           (gnus-data-update-list
4621            (cdr datal)
4622            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4623
4624 (defun gnus-summary-update-article (article &optional iheader)
4625   "Update ARTICLE in the summary buffer."
4626   (set-buffer gnus-summary-buffer)
4627   (let* ((header (gnus-summary-article-header article))
4628          (id (mail-header-id header))
4629          (data (gnus-data-find article))
4630          (thread (gnus-id-to-thread id))
4631          (references (mail-header-references header))
4632          (parent
4633           (gnus-id-to-thread
4634            (or (gnus-parent-id
4635                 (when (and references
4636                            (not (equal "" references)))
4637                   references))
4638                "none")))
4639          (inhibit-read-only t)
4640          (old (car thread)))
4641     (when thread
4642       (unless iheader
4643         (setcar thread nil)
4644         (when parent
4645           (delq thread parent)))
4646       (if (gnus-summary-insert-subject id header)
4647           ;; Set the (possibly) new article number in the data structure.
4648           (gnus-data-set-number data (gnus-id-to-article id))
4649         (setcar thread old)
4650         nil))))
4651
4652 (defun gnus-rebuild-thread (id &optional line)
4653   "Rebuild the thread containing ID.
4654 If LINE, insert the rebuilt thread starting on line LINE."
4655   (let ((inhibit-read-only t)
4656         old-pos current thread data)
4657     (if (not gnus-show-threads)
4658         (setq thread (list (car (gnus-id-to-thread id))))
4659       ;; Get the thread this article is part of.
4660       (setq thread (gnus-remove-thread id)))
4661     (setq old-pos (point-at-bol))
4662     (setq current (save-excursion
4663                     (and (re-search-backward "[\r\n]" nil t)
4664                          (gnus-summary-article-number))))
4665     ;; If this is a gathered thread, we have to go some re-gathering.
4666     (when (stringp (car thread))
4667       (let ((subject (car thread))
4668             roots thr)
4669         (setq thread (cdr thread))
4670         (while thread
4671           (unless (memq (setq thr (gnus-id-to-thread
4672                                    (gnus-root-id
4673                                     (mail-header-id (caar thread)))))
4674                         roots)
4675             (push thr roots))
4676           (setq thread (cdr thread)))
4677         ;; We now have all (unique) roots.
4678         (if (= (length roots) 1)
4679             ;; All the loose roots are now one solid root.
4680             (setq thread (car roots))
4681           (setq thread (cons subject (gnus-sort-threads roots))))))
4682     (let (threads)
4683       ;; We then insert this thread into the summary buffer.
4684       (when line
4685         (goto-char (point-min))
4686         (forward-line (1- line)))
4687       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4688         (if gnus-show-threads
4689             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4690           (gnus-summary-prepare-unthreaded thread))
4691         (setq data (nreverse gnus-newsgroup-data))
4692         (setq threads gnus-newsgroup-threads))
4693       ;; We splice the new data into the data structure.
4694       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4695       ;;!!! then we want to insert at the beginning of the buffer.
4696       ;;!!! That happens to be true with Gnus now, but that may
4697       ;;!!! change in the future.  Perhaps.
4698       (gnus-data-enter-list
4699        (if line nil current) data (- (point) old-pos))
4700       (setq gnus-newsgroup-threads
4701             (nconc threads gnus-newsgroup-threads))
4702       (gnus-data-compute-positions))))
4703
4704 (defun gnus-number-to-header (number)
4705   "Return the header for article NUMBER."
4706   (let ((headers gnus-newsgroup-headers))
4707     (while (and headers
4708                 (not (= number (mail-header-number (car headers)))))
4709       (pop headers))
4710     (when headers
4711       (car headers))))
4712
4713 (defun gnus-parent-headers (in-headers &optional generation)
4714   "Return the headers of the GENERATIONth parent of HEADERS."
4715   (unless generation
4716     (setq generation 1))
4717   (let ((parent t)
4718         (headers in-headers)
4719         references)
4720     (while (and parent
4721                 (not (zerop generation))
4722                 (setq references (mail-header-references headers)))
4723       (setq headers (if (and references
4724                              (setq parent (gnus-parent-id references)))
4725                         (car (gnus-id-to-thread parent))
4726                       nil))
4727       (decf generation))
4728     (and (not (eq headers in-headers))
4729          headers)))
4730
4731 (defun gnus-id-to-thread (id)
4732   "Return the (sub-)thread where ID appears."
4733   (gnus-gethash id gnus-newsgroup-dependencies))
4734
4735 (defun gnus-id-to-article (id)
4736   "Return the article number of ID."
4737   (let ((thread (gnus-id-to-thread id)))
4738     (when (and thread
4739                (car thread))
4740       (mail-header-number (car thread)))))
4741
4742 (defun gnus-id-to-header (id)
4743   "Return the article headers of ID."
4744   (car (gnus-id-to-thread id)))
4745
4746 (defun gnus-article-displayed-root-p (article)
4747   "Say whether ARTICLE is a root(ish) article."
4748   (let ((level (gnus-summary-thread-level article))
4749         (refs (mail-header-references  (gnus-summary-article-header article)))
4750         particle)
4751     (cond
4752      ((null level) nil)
4753      ((zerop level) t)
4754      ((null refs) t)
4755      ((null (gnus-parent-id refs)) t)
4756      ((and (= 1 level)
4757            (null (setq particle (gnus-id-to-article
4758                                  (gnus-parent-id refs))))
4759            (null (gnus-summary-thread-level particle)))))))
4760
4761 (defun gnus-root-id (id)
4762   "Return the id of the root of the thread where ID appears."
4763   (let (last-id prev)
4764     (while (and id (setq prev (car (gnus-id-to-thread id))))
4765       (setq last-id id
4766             id (gnus-parent-id (mail-header-references prev))))
4767     last-id))
4768
4769 (defun gnus-articles-in-thread (thread)
4770   "Return the list of articles in THREAD."
4771   (cons (mail-header-number (car thread))
4772         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4773
4774 (defun gnus-remove-thread (id &optional dont-remove)
4775   "Remove the thread that has ID in it."
4776   (let (headers thread last-id)
4777     ;; First go up in this thread until we find the root.
4778     (setq last-id (gnus-root-id id)
4779           headers (message-flatten-list (gnus-id-to-thread last-id)))
4780     ;; We have now found the real root of this thread.  It might have
4781     ;; been gathered into some loose thread, so we have to search
4782     ;; through the threads to find the thread we wanted.
4783     (let ((threads gnus-newsgroup-threads)
4784           sub)
4785       (while threads
4786         (setq sub (car threads))
4787         (if (stringp (car sub))
4788             ;; This is a gathered thread, so we look at the roots
4789             ;; below it to find whether this article is in this
4790             ;; gathered root.
4791             (progn
4792               (setq sub (cdr sub))
4793               (while sub
4794                 (when (member (caar sub) headers)
4795                   (setq thread (car threads)
4796                         threads nil
4797                         sub nil))
4798                 (setq sub (cdr sub))))
4799           ;; It's an ordinary thread, so we check it.
4800           (when (eq (car sub) (car headers))
4801             (setq thread sub
4802                   threads nil)))
4803         (setq threads (cdr threads)))
4804       ;; If this article is in no thread, then it's a root.
4805       (if thread
4806           (unless dont-remove
4807             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4808         (setq thread (gnus-id-to-thread last-id)))
4809       (when thread
4810         (prog1
4811             thread                      ; We return this thread.
4812           (unless dont-remove
4813             (if (stringp (car thread))
4814                 (progn
4815                   ;; If we use dummy roots, then we have to remove the
4816                   ;; dummy root as well.
4817                   (when (eq gnus-summary-make-false-root 'dummy)
4818                     ;; We go to the dummy root by going to
4819                     ;; the first sub-"thread", and then one line up.
4820                     (gnus-summary-goto-article
4821                      (mail-header-number (caadr thread)))
4822                     (forward-line -1)
4823                     (gnus-delete-line)
4824                     (gnus-data-compute-positions))
4825                   (setq thread (cdr thread))
4826                   (while thread
4827                     (gnus-remove-thread-1 (car thread))
4828                     (setq thread (cdr thread))))
4829               (gnus-remove-thread-1 thread))))))))
4830
4831 (defun gnus-remove-thread-1 (thread)
4832   "Remove the thread THREAD recursively."
4833   (let ((number (mail-header-number (pop thread)))
4834         d)
4835     (setq thread (reverse thread))
4836     (while thread
4837       (gnus-remove-thread-1 (pop thread)))
4838     (when (setq d (gnus-data-find number))
4839       (goto-char (gnus-data-pos d))
4840       (gnus-summary-show-thread)
4841       (gnus-data-remove
4842        number
4843        (- (point-at-bol)
4844           (prog1
4845               (1+ (point-at-eol))
4846             (gnus-delete-line)))))))
4847
4848 (defun gnus-sort-threads-recursive (threads func)
4849   (sort (mapcar (lambda (thread)
4850                   (cons (car thread)
4851                         (and (cdr thread)
4852                              (gnus-sort-threads-recursive (cdr thread) func))))
4853                 threads) func))
4854
4855 (defun gnus-sort-threads-loop (threads func)
4856   (let* ((superthread (cons nil threads))
4857          (stack (list (cons superthread threads)))
4858          remaining-threads thread)
4859     (while stack
4860       (setq remaining-threads (cdr (car stack)))
4861       (if remaining-threads
4862           (progn (setq thread (car remaining-threads))
4863                  (setcdr (car stack) (cdr remaining-threads))
4864                  (if (cdr thread)
4865                      (push (cons thread (cdr thread)) stack)))
4866         (setq thread (caar stack))
4867         (setcdr thread (sort (cdr thread) func))
4868         (pop stack)))
4869     (cdr superthread)))
4870
4871 (defun gnus-sort-threads (threads)
4872   "Sort THREADS."
4873   (if (not gnus-thread-sort-functions)
4874       threads
4875     (gnus-message 8 "Sorting threads...")
4876     (prog1
4877         (condition-case nil
4878             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4879               (gnus-sort-threads-recursive
4880                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4881           ;; Even after binding max-lisp-eval-depth, the recursive
4882           ;; sorter might fail for very long threads.  In that case,
4883           ;; try using a (less well-tested) non-recursive sorter.
4884           (error (gnus-message 9 "Sorting threads with loop...")
4885                  (gnus-sort-threads-loop
4886                   threads (gnus-make-sort-function
4887                            gnus-thread-sort-functions))))
4888       (gnus-message 8 "Sorting threads...done"))))
4889
4890 (defun gnus-sort-articles (articles)
4891   "Sort ARTICLES."
4892   (when gnus-article-sort-functions
4893     (gnus-message 7 "Sorting articles...")
4894     (prog1
4895         (setq gnus-newsgroup-headers
4896               (sort articles (gnus-make-sort-function
4897                               gnus-article-sort-functions)))
4898       (gnus-message 7 "Sorting articles...done"))))
4899
4900 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4901 (defmacro gnus-thread-header (thread)
4902   "Return header of first article in THREAD.
4903 Note that THREAD must never, ever be anything else than a variable -
4904 using some other form will lead to serious barfage."
4905   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4906   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4907   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4908         (vector thread) 2))
4909
4910 (defsubst gnus-article-sort-by-number (h1 h2)
4911   "Sort articles by article number."
4912   (< (mail-header-number h1)
4913      (mail-header-number h2)))
4914
4915 (defun gnus-thread-sort-by-number (h1 h2)
4916   "Sort threads by root article number."
4917   (gnus-article-sort-by-number
4918    (gnus-thread-header h1) (gnus-thread-header h2)))
4919
4920 (defsubst gnus-article-sort-by-random (h1 h2)
4921   "Sort articles randomly."
4922   (zerop (random 2)))
4923
4924 (defun gnus-thread-sort-by-random (h1 h2)
4925   "Sort threads randomly."
4926   (gnus-article-sort-by-random
4927    (gnus-thread-header h1) (gnus-thread-header h2)))
4928
4929 (defsubst gnus-article-sort-by-lines (h1 h2)
4930   "Sort articles by article Lines header."
4931   (< (mail-header-lines h1)
4932      (mail-header-lines h2)))
4933
4934 (defun gnus-thread-sort-by-lines (h1 h2)
4935   "Sort threads by root article Lines header."
4936   (gnus-article-sort-by-lines
4937    (gnus-thread-header h1) (gnus-thread-header h2)))
4938
4939 (defsubst gnus-article-sort-by-chars (h1 h2)
4940   "Sort articles by octet length."
4941   (< (mail-header-chars h1)
4942      (mail-header-chars h2)))
4943
4944 (defun gnus-thread-sort-by-chars (h1 h2)
4945   "Sort threads by root article octet length."
4946   (gnus-article-sort-by-chars
4947    (gnus-thread-header h1) (gnus-thread-header h2)))
4948
4949 (defsubst gnus-article-sort-by-author (h1 h2)
4950   "Sort articles by root author."
4951   (gnus-string<
4952    (let ((extract (funcall
4953                    gnus-extract-address-components
4954                    (mail-header-from h1))))
4955      (or (car extract) (cadr extract) ""))
4956    (let ((extract (funcall
4957                    gnus-extract-address-components
4958                    (mail-header-from h2))))
4959      (or (car extract) (cadr extract) ""))))
4960
4961 (defun gnus-thread-sort-by-author (h1 h2)
4962   "Sort threads by root author."
4963   (gnus-article-sort-by-author
4964    (gnus-thread-header h1)  (gnus-thread-header h2)))
4965
4966 (defsubst gnus-article-sort-by-recipient (h1 h2)
4967   "Sort articles by recipient."
4968   (gnus-string<
4969    (let ((extract (funcall
4970                    gnus-extract-address-components
4971                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4972      (or (car extract) (cadr extract)))
4973    (let ((extract (funcall
4974                    gnus-extract-address-components
4975                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4976      (or (car extract) (cadr extract)))))
4977
4978 (defun gnus-thread-sort-by-recipient (h1 h2)
4979   "Sort threads by root recipient."
4980   (gnus-article-sort-by-recipient
4981    (gnus-thread-header h1) (gnus-thread-header h2)))
4982
4983 (defsubst gnus-article-sort-by-subject (h1 h2)
4984   "Sort articles by root subject."
4985   (gnus-string<
4986    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4987    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4988
4989 (defun gnus-thread-sort-by-subject (h1 h2)
4990   "Sort threads by root subject."
4991   (gnus-article-sort-by-subject
4992    (gnus-thread-header h1) (gnus-thread-header h2)))
4993
4994 (defsubst gnus-article-sort-by-date (h1 h2)
4995   "Sort articles by root article date."
4996   (time-less-p
4997    (gnus-date-get-time (mail-header-date h1))
4998    (gnus-date-get-time (mail-header-date h2))))
4999
5000 (defun gnus-thread-sort-by-date (h1 h2)
5001   "Sort threads by root article date."
5002   (gnus-article-sort-by-date
5003    (gnus-thread-header h1) (gnus-thread-header h2)))
5004
5005 (defsubst gnus-article-sort-by-score (h1 h2)
5006   "Sort articles by root article score.
5007 Unscored articles will be counted as having a score of zero."
5008   (> (or (cdr (assq (mail-header-number h1)
5009                     gnus-newsgroup-scored))
5010          gnus-summary-default-score 0)
5011      (or (cdr (assq (mail-header-number h2)
5012                     gnus-newsgroup-scored))
5013          gnus-summary-default-score 0)))
5014
5015 (defun gnus-thread-sort-by-score (h1 h2)
5016   "Sort threads by root article score."
5017   (gnus-article-sort-by-score
5018    (gnus-thread-header h1) (gnus-thread-header h2)))
5019
5020 (defun gnus-thread-sort-by-total-score (h1 h2)
5021   "Sort threads by the sum of all scores in the thread.
5022 Unscored articles will be counted as having a score of zero."
5023   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5024
5025 (defun gnus-thread-total-score (thread)
5026   ;; This function find the total score of THREAD.
5027   (cond
5028    ((null thread)
5029     0)
5030    ((consp thread)
5031     (if (stringp (car thread))
5032         (apply gnus-thread-score-function 0
5033                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5034       (gnus-thread-total-score-1 thread)))
5035    (t
5036     (gnus-thread-total-score-1 (list thread)))))
5037
5038 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5039   "Sort articles by number."
5040   (gnus-article-sort-by-number h1 h2))
5041
5042 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5043   "Sort threads such that the thread with the most recently arrived article comes first."
5044   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5045
5046 (defun gnus-thread-highest-number (thread)
5047   "Return the highest article number in THREAD."
5048   (apply 'max (mapcar (lambda (header)
5049                         (mail-header-number header))
5050                       (message-flatten-list thread))))
5051
5052 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5053   "Sort articles by number."
5054   (gnus-article-sort-by-date h1 h2))
5055
5056 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5057   "Sort threads such that the thread with the most recently dated article comes first."
5058   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5059
5060 ; Since this is called not only to sort the top-level threads, but
5061 ; also in recursive sorts to order the articles within a thread, each
5062 ; article will be processed many times.  Thus it speeds things up
5063 ; quite a bit to use gnus-date-get-time, which caches the time value.
5064 (defun gnus-thread-latest-date (thread)
5065   "Return the highest article date in THREAD."
5066   (apply 'max
5067          (mapcar (lambda (header) (gnus-float-time
5068                                    (gnus-date-get-time
5069                                     (mail-header-date header))))
5070                  (message-flatten-list thread))))
5071
5072 (defun gnus-thread-total-score-1 (root)
5073   ;; This function find the total score of the thread below ROOT.
5074   (setq root (car root))
5075   (apply gnus-thread-score-function
5076          (or (append
5077               (mapcar 'gnus-thread-total-score
5078                       (cdr (gnus-id-to-thread (mail-header-id root))))
5079               (when (> (mail-header-number root) 0)
5080                 (list (or (cdr (assq (mail-header-number root)
5081                                      gnus-newsgroup-scored))
5082                           gnus-summary-default-score 0))))
5083              (list gnus-summary-default-score)
5084              '(0))))
5085
5086 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5087 (defvar gnus-tmp-prev-subject nil)
5088 (defvar gnus-tmp-false-parent nil)
5089 (defvar gnus-tmp-root-expunged nil)
5090 (defvar gnus-tmp-dummy-line nil)
5091
5092 (defun gnus-extra-header (type &optional header)
5093   "Return the extra header of TYPE."
5094   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5095       ""))
5096
5097 (defvar gnus-tmp-thread-tree-header-string "")
5098
5099 (defcustom gnus-sum-thread-tree-root "> "
5100   "With %B spec, used for the root of a thread.
5101 If nil, use subject instead."
5102   :version "22.1"
5103   :type '(radio (const :format "%v  " nil) string)
5104   :group 'gnus-thread)
5105
5106 (defcustom gnus-sum-thread-tree-false-root "> "
5107   "With %B spec, used for a false root of a thread.
5108 If nil, use subject instead."
5109   :version "22.1"
5110   :type '(radio (const :format "%v  " nil) string)
5111   :group 'gnus-thread)
5112
5113 (defcustom gnus-sum-thread-tree-single-indent ""
5114   "With %B spec, used for a thread with just one message.
5115 If nil, use subject instead."
5116   :version "22.1"
5117   :type '(radio (const :format "%v  " nil) string)
5118   :group 'gnus-thread)
5119
5120 (defcustom gnus-sum-thread-tree-vertical "| "
5121   "With %B spec, used for drawing a vertical line."
5122   :version "22.1"
5123   :type 'string
5124   :group 'gnus-thread)
5125
5126 (defcustom gnus-sum-thread-tree-indent "  "
5127   "With %B spec, used for indenting."
5128   :version "22.1"
5129   :type 'string
5130   :group 'gnus-thread)
5131
5132 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5133   "With %B spec, used for a leaf with brothers."
5134   :version "22.1"
5135   :type 'string
5136   :group 'gnus-thread)
5137
5138 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5139   "With %B spec, used for a leaf without brothers."
5140   :version "22.1"
5141   :type 'string
5142   :group 'gnus-thread)
5143
5144 (defcustom gnus-summary-display-while-building nil
5145   "If non-nil, show and update the summary buffer as it's being built.
5146 If the value is t, update the buffer after every line is inserted.  If
5147 the value is an integer (N), update the display every N lines."
5148   :version "22.1"
5149   :group 'gnus-thread
5150   :type '(choice (const :tag "off" nil)
5151                  number
5152                  (const :tag "frequently" t)))
5153
5154 (defun gnus-summary-prepare-threads (threads)
5155   "Prepare summary buffer from THREADS and indentation LEVEL.
5156 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5157 or a straight list of headers."
5158   (gnus-message 7 "Generating summary...")
5159
5160   (setq gnus-newsgroup-threads threads)
5161   (beginning-of-line)
5162
5163   (let ((gnus-tmp-level 0)
5164         (default-score (or gnus-summary-default-score 0))
5165         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5166         (building-line-count gnus-summary-display-while-building)
5167         (building-count (integerp gnus-summary-display-while-building))
5168         thread number subject stack state gnus-tmp-gathered beg-match
5169         new-roots gnus-tmp-new-adopts thread-end simp-subject
5170         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5171         gnus-tmp-replied gnus-tmp-subject-or-nil
5172         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5173         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5174         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5175         tree-stack)
5176
5177     (setq gnus-tmp-prev-subject nil
5178           gnus-tmp-thread-tree-header-string "")
5179
5180     (if (vectorp (car threads))
5181         ;; If this is a straight (sic) list of headers, then a
5182         ;; threaded summary display isn't required, so we just create
5183         ;; an unthreaded one.
5184         (gnus-summary-prepare-unthreaded threads)
5185
5186       ;; Do the threaded display.
5187
5188       (if gnus-summary-display-while-building
5189           (switch-to-buffer (buffer-name)))
5190       (while (or threads stack gnus-tmp-new-adopts new-roots)
5191
5192         (if (and (= gnus-tmp-level 0)
5193                  (or (not stack)
5194                      (= (caar stack) 0))
5195                  (not gnus-tmp-false-parent)
5196                  (or gnus-tmp-new-adopts new-roots))
5197             (if gnus-tmp-new-adopts
5198                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5199                       thread (list (car gnus-tmp-new-adopts))
5200                       gnus-tmp-header (caar thread)
5201                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5202               (when new-roots
5203                 (setq thread (list (car new-roots))
5204                       gnus-tmp-header (caar thread)
5205                       new-roots (cdr new-roots))))
5206
5207           (if threads
5208               ;; If there are some threads, we do them before the
5209               ;; threads on the stack.
5210               (setq thread threads
5211                     gnus-tmp-header (caar thread))
5212             ;; There were no current threads, so we pop something off
5213             ;; the stack.
5214             (setq state (car stack)
5215                   gnus-tmp-level (car state)
5216                   tree-stack (cadr state)
5217                   thread (caddr state)
5218                   stack (cdr stack)
5219                   gnus-tmp-header (caar thread))))
5220
5221         (setq gnus-tmp-false-parent nil)
5222         (setq gnus-tmp-root-expunged nil)
5223         (setq thread-end nil)
5224
5225         (if (stringp gnus-tmp-header)
5226             ;; The header is a dummy root.
5227             (cond
5228              ((eq gnus-summary-make-false-root 'adopt)
5229               ;; We let the first article adopt the rest.
5230               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5231                                                (cddar thread)))
5232               (setq gnus-tmp-gathered
5233                     (nconc (mapcar
5234                             (lambda (h) (mail-header-number (car h)))
5235                             (cddar thread))
5236                            gnus-tmp-gathered))
5237               (setq thread (cons (list (caar thread)
5238                                        (cadar thread))
5239                                  (cdr thread)))
5240               (setq gnus-tmp-level -1
5241                     gnus-tmp-false-parent t))
5242              ((eq gnus-summary-make-false-root 'empty)
5243               ;; We print adopted articles with empty subject fields.
5244               (setq gnus-tmp-gathered
5245                     (nconc (mapcar
5246                             (lambda (h) (mail-header-number (car h)))
5247                             (cddar thread))
5248                            gnus-tmp-gathered))
5249               (setq gnus-tmp-level -1))
5250              ((eq gnus-summary-make-false-root 'dummy)
5251               ;; We remember that we probably want to output a dummy
5252               ;; root.
5253               (setq gnus-tmp-dummy-line gnus-tmp-header)
5254               (setq gnus-tmp-prev-subject gnus-tmp-header))
5255              (t
5256               ;; We do not make a root for the gathered
5257               ;; sub-threads at all.
5258               (setq gnus-tmp-level -1)))
5259
5260           (setq number (mail-header-number gnus-tmp-header)
5261                 subject (mail-header-subject gnus-tmp-header)
5262                 simp-subject (gnus-simplify-subject-fully subject))
5263
5264           (cond
5265            ;; If the thread has changed subject, we might want to make
5266            ;; this subthread into a root.
5267            ((and (null gnus-thread-ignore-subject)
5268                  (not (zerop gnus-tmp-level))
5269                  gnus-tmp-prev-subject
5270                  (not (string= gnus-tmp-prev-subject simp-subject)))
5271             (setq new-roots (nconc new-roots (list (car thread)))
5272                   thread-end t
5273                   gnus-tmp-header nil))
5274            ;; If the article lies outside the current limit,
5275            ;; then we do not display it.
5276            ((not (memq number gnus-newsgroup-limit))
5277             (setq gnus-tmp-gathered
5278                   (nconc (mapcar
5279                           (lambda (h) (mail-header-number (car h)))
5280                           (cdar thread))
5281                          gnus-tmp-gathered))
5282             (setq gnus-tmp-new-adopts (if (cdar thread)
5283                                           (append gnus-tmp-new-adopts
5284                                                   (cdar thread))
5285                                         gnus-tmp-new-adopts)
5286                   thread-end t
5287                   gnus-tmp-header nil)
5288             (when (zerop gnus-tmp-level)
5289               (setq gnus-tmp-root-expunged t)))
5290            ;; Perhaps this article is to be marked as read?
5291            ((and gnus-summary-mark-below
5292                  (< (or (cdr (assq number gnus-newsgroup-scored))
5293                         default-score)
5294                     gnus-summary-mark-below)
5295                  ;; Don't touch sparse articles.
5296                  (not (gnus-summary-article-sparse-p number))
5297                  (not (gnus-summary-article-ancient-p number)))
5298             (setq gnus-newsgroup-unreads
5299                   (delq number gnus-newsgroup-unreads))
5300             (if gnus-newsgroup-auto-expire
5301                 (setq gnus-newsgroup-expirable
5302                       (gnus-add-to-sorted-list
5303                        gnus-newsgroup-expirable number))
5304               (push (cons number gnus-low-score-mark)
5305                     gnus-newsgroup-reads))))
5306
5307           (when gnus-tmp-header
5308             ;; We may have an old dummy line to output before this
5309             ;; article.
5310             (when (and gnus-tmp-dummy-line
5311                        (gnus-subject-equal
5312                         gnus-tmp-dummy-line
5313                         (mail-header-subject gnus-tmp-header)))
5314               (gnus-summary-insert-dummy-line
5315                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5316               (setq gnus-tmp-dummy-line nil))
5317
5318             ;; Compute the mark.
5319             (setq gnus-tmp-unread (gnus-article-mark number))
5320
5321             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5322                                   gnus-tmp-header gnus-tmp-level)
5323                   gnus-newsgroup-data)
5324
5325             ;; Actually insert the line.
5326             (setq
5327              gnus-tmp-subject-or-nil
5328              (cond
5329               ((and gnus-thread-ignore-subject
5330                     gnus-tmp-prev-subject
5331                     (not (string= gnus-tmp-prev-subject simp-subject)))
5332                subject)
5333               ((zerop gnus-tmp-level)
5334                (if (and (eq gnus-summary-make-false-root 'empty)
5335                         (memq number gnus-tmp-gathered)
5336                         gnus-tmp-prev-subject
5337                         (string= gnus-tmp-prev-subject simp-subject))
5338                    gnus-summary-same-subject
5339                  subject))
5340               (t gnus-summary-same-subject)))
5341             (if (and (eq gnus-summary-make-false-root 'adopt)
5342                      (= gnus-tmp-level 1)
5343                      (memq number gnus-tmp-gathered))
5344                 (setq gnus-tmp-opening-bracket ?\<
5345                       gnus-tmp-closing-bracket ?\>)
5346               (setq gnus-tmp-opening-bracket ?\[
5347                     gnus-tmp-closing-bracket ?\]))
5348             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5349                 (gnus-make-thread-indent-array
5350                  (max (* 2 (length gnus-thread-indent-array))
5351                       gnus-tmp-level)))
5352             (setq
5353              gnus-tmp-indentation
5354              (aref gnus-thread-indent-array gnus-tmp-level)
5355              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5356              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5357                                 gnus-summary-default-score 0)
5358              gnus-tmp-score-char
5359              (if (or (null gnus-summary-default-score)
5360                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5361                          gnus-summary-zcore-fuzz))
5362                  ?                      ;Whitespace
5363                (if (< gnus-tmp-score gnus-summary-default-score)
5364                    gnus-score-below-mark gnus-score-over-mark))
5365              gnus-tmp-replied
5366              (cond ((memq number gnus-newsgroup-processable)
5367                     gnus-process-mark)
5368                    ((memq number gnus-newsgroup-cached)
5369                     gnus-cached-mark)
5370                    ((memq number gnus-newsgroup-replied)
5371                     gnus-replied-mark)
5372                    ((memq number gnus-newsgroup-forwarded)
5373                     gnus-forwarded-mark)
5374                    ((memq number gnus-newsgroup-saved)
5375                     gnus-saved-mark)
5376                    ((memq number gnus-newsgroup-unseen)
5377                     gnus-unseen-mark)
5378                    (t gnus-no-mark))
5379              gnus-tmp-downloaded
5380              (cond ((memq number gnus-newsgroup-undownloaded)
5381                     gnus-undownloaded-mark)
5382                    (gnus-newsgroup-agentized
5383                     gnus-downloaded-mark)
5384                    (t
5385                     gnus-no-mark))
5386              gnus-tmp-from (mail-header-from gnus-tmp-header)
5387              gnus-tmp-name
5388              (cond
5389               ((string-match "<[^>]+> *$" gnus-tmp-from)
5390                (setq beg-match (match-beginning 0))
5391                (or (and (string-match "^\".+\"" gnus-tmp-from)
5392                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5393                    (substring gnus-tmp-from 0 beg-match)))
5394               ((string-match "(.+)" gnus-tmp-from)
5395                (substring gnus-tmp-from
5396                           (1+ (match-beginning 0)) (1- (match-end 0))))
5397               (t gnus-tmp-from))
5398
5399              ;; Do the %B string
5400              gnus-tmp-thread-tree-header-string
5401              (cond
5402               ((not gnus-show-threads) "")
5403               ((zerop gnus-tmp-level)
5404                (cond ((cdar thread)
5405                       (or gnus-sum-thread-tree-root subject))
5406                      (gnus-tmp-new-adopts
5407                       (or gnus-sum-thread-tree-false-root subject))
5408                      (t
5409                       (or gnus-sum-thread-tree-single-indent subject))))
5410               (t
5411                (concat (apply 'concat
5412                               (mapcar (lambda (item)
5413                                         (if (= item 1)
5414                                             gnus-sum-thread-tree-vertical
5415                                           gnus-sum-thread-tree-indent))
5416                                       (cdr (reverse tree-stack))))
5417                        (if (nth 1 thread)
5418                            gnus-sum-thread-tree-leaf-with-other
5419                          gnus-sum-thread-tree-single-leaf)))))
5420             (when (string= gnus-tmp-name "")
5421               (setq gnus-tmp-name gnus-tmp-from))
5422             (unless (numberp gnus-tmp-lines)
5423               (setq gnus-tmp-lines -1))
5424             (if (= gnus-tmp-lines -1)
5425                 (setq gnus-tmp-lines "?")
5426               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5427             (gnus-put-text-property
5428              (point)
5429              (progn (eval gnus-summary-line-format-spec) (point))
5430              'gnus-number number)
5431             (when gnus-visual-p
5432               (forward-line -1)
5433               (gnus-summary-highlight-line)
5434               (when gnus-summary-update-hook
5435                 (gnus-run-hooks 'gnus-summary-update-hook))
5436               (forward-line 1))
5437
5438             (setq gnus-tmp-prev-subject simp-subject)))
5439
5440         (when (nth 1 thread)
5441           (push (list (max 0 gnus-tmp-level)
5442                       (copy-sequence tree-stack)
5443                       (nthcdr 1 thread))
5444                 stack))
5445         (push (if (nth 1 thread) 1 0) tree-stack)
5446         (incf gnus-tmp-level)
5447         (setq threads (if thread-end nil (cdar thread)))
5448         (if gnus-summary-display-while-building
5449             (if building-count
5450                 (progn
5451                   ;; use a set frequency
5452                   (setq building-line-count (1- building-line-count))
5453                   (when (= building-line-count 0)
5454                     (sit-for 0)
5455                     (setq building-line-count
5456                           gnus-summary-display-while-building)))
5457               ;; always
5458               (sit-for 0)))
5459         (unless threads
5460           (setq gnus-tmp-level 0)))))
5461   (gnus-message 7 "Generating summary...done"))
5462
5463 (defun gnus-summary-prepare-unthreaded (headers)
5464   "Generate an unthreaded summary buffer based on HEADERS."
5465   (let (header number mark)
5466
5467     (beginning-of-line)
5468
5469     (while headers
5470       ;; We may have to root out some bad articles...
5471       (when (memq (setq number (mail-header-number
5472                                 (setq header (pop headers))))
5473                   gnus-newsgroup-limit)
5474         ;; Mark article as read when it has a low score.
5475         (when (and gnus-summary-mark-below
5476                    (< (or (cdr (assq number gnus-newsgroup-scored))
5477                           gnus-summary-default-score 0)
5478                       gnus-summary-mark-below)
5479                    (not (gnus-summary-article-ancient-p number)))
5480           (setq gnus-newsgroup-unreads
5481                 (delq number gnus-newsgroup-unreads))
5482           (if gnus-newsgroup-auto-expire
5483               (push number gnus-newsgroup-expirable)
5484             (push (cons number gnus-low-score-mark)
5485                   gnus-newsgroup-reads)))
5486
5487         (setq mark (gnus-article-mark number))
5488         (push (gnus-data-make number mark (1+ (point)) header 0)
5489               gnus-newsgroup-data)
5490         (gnus-summary-insert-line
5491          header 0 number
5492          (memq number gnus-newsgroup-undownloaded)
5493          mark (memq number gnus-newsgroup-replied)
5494          (memq number gnus-newsgroup-expirable)
5495          (mail-header-subject header) nil
5496          (cdr (assq number gnus-newsgroup-scored))
5497          (memq number gnus-newsgroup-processable))))))
5498
5499 (declare-function gnus-parameter-list-identifier "gnus-art" (name) t)
5500
5501 (defun gnus-group-get-list-identifiers (group)
5502   "Get list identifier regexp for GROUP."
5503   (or (gnus-parameter-list-identifier group)
5504       (if (consp gnus-list-identifiers)
5505           (mapconcat 'identity gnus-list-identifiers " *\\|")
5506         gnus-list-identifiers)))
5507
5508 (defun gnus-summary-remove-list-identifiers ()
5509   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5510   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5511         changed subject)
5512     (when regexp
5513       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5514       (dolist (header gnus-newsgroup-headers)
5515         (setq subject (mail-header-subject header)
5516               changed nil)
5517         (while (string-match regexp subject)
5518           (setq subject
5519                 (concat (substring subject 0 (match-beginning 1))
5520                         (substring subject (match-end 0)))
5521                 changed t))
5522         (when changed
5523           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5524             (setq subject
5525                   (concat (substring subject 0 (match-beginning 1))
5526                           (substring subject (match-end 1)))))
5527           (mail-header-set-subject header subject))))))
5528
5529 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5530   "Fetch headers of ARTICLES."
5531   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5532     (gnus-message 7 "Fetching headers for %s..." name)
5533     (prog1
5534         (if (eq 'nov
5535                 (setq gnus-headers-retrieved-by
5536                       (gnus-retrieve-headers
5537                        articles gnus-newsgroup-name
5538                        (or limit
5539                            ;; We might want to fetch old headers, but
5540                            ;; not if there is only 1 article.
5541                            (and (or (and
5542                                      (not (eq gnus-fetch-old-headers 'some))
5543                                      (not (numberp gnus-fetch-old-headers)))
5544                                     (> (length articles) 1))
5545                                 gnus-fetch-old-headers)))))
5546             (gnus-get-newsgroup-headers-xover
5547              articles force-new dependencies gnus-newsgroup-name t)
5548           (gnus-get-newsgroup-headers dependencies force-new))
5549       (gnus-message 7 "Fetching headers for %s...done" name))))
5550
5551 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5552   "Select newsgroup GROUP.
5553 If READ-ALL is non-nil, all articles in the group are selected.
5554 If SELECT-ARTICLES, only select those articles from GROUP."
5555   (let* ((entry (gnus-group-entry group))
5556          ;;!!! Dirty hack; should be removed.
5557          (gnus-summary-ignore-duplicates
5558           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5559               t
5560             gnus-summary-ignore-duplicates))
5561          (info (nth 2 entry))
5562          charset articles fetched-articles cached)
5563
5564     (unless (gnus-check-server
5565              (set (make-local-variable 'gnus-current-select-method)
5566                   (gnus-find-method-for-group group)))
5567       (error "Couldn't open server"))
5568     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5569
5570     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5571         (gnus-activate-group group)     ; Or we can activate it...
5572         (progn                          ; Or we bug out.
5573           (when (equal major-mode 'gnus-summary-mode)
5574             (gnus-kill-buffer (current-buffer)))
5575           (error
5576            "Couldn't activate group %s: %s"
5577            (mm-decode-coding-string group charset)
5578            (mm-decode-coding-string (gnus-status-message group) charset))))
5579
5580     (unless (gnus-request-group group t)
5581       (when (equal major-mode 'gnus-summary-mode)
5582         (gnus-kill-buffer (current-buffer)))
5583       (error "Couldn't request group %s: %s"
5584              (mm-decode-coding-string group charset)
5585              (mm-decode-coding-string (gnus-status-message group) charset)))
5586
5587     (when (and gnus-agent
5588                (gnus-active group))
5589       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5590
5591       (setq gnus-summary-use-undownloaded-faces
5592             (gnus-agent-find-parameter
5593              group
5594              'agent-enable-undownloaded-faces)))
5595
5596     (setq gnus-newsgroup-name group
5597           gnus-newsgroup-unselected nil
5598           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5599
5600     (let ((display (gnus-group-find-parameter group 'display)))
5601       (setq gnus-newsgroup-display
5602             (cond
5603              ((not (zerop (or (car-safe read-all) 0)))
5604               ;; The user entered the group with C-u SPC/RET, let's show
5605               ;; all articles.
5606               'gnus-not-ignore)
5607              ((eq display 'all)
5608               'gnus-not-ignore)
5609              ((arrayp display)
5610               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5611              ((numberp display)
5612               ;; The following is probably the "correct" solution, but
5613               ;; it makes Gnus fetch all headers and then limit the
5614               ;; articles (which is slow), so instead we hack the
5615               ;; select-articles parameter instead. -- Simon Josefsson
5616               ;; <jas@kth.se>
5617               ;;
5618               ;; (gnus-byte-compile
5619               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5620               ;;                         display)))))
5621               (setq select-articles
5622                     (gnus-uncompress-range
5623                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5624                              (if (> tmp 0)
5625                                  tmp
5626                                1))
5627                            (cdr (gnus-active group)))))
5628               nil)
5629              (t
5630               nil))))
5631
5632     (gnus-summary-setup-default-charset)
5633
5634     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5635     (when (gnus-virtual-group-p group)
5636       (setq cached gnus-newsgroup-cached))
5637
5638     (setq gnus-newsgroup-unreads
5639           (gnus-sorted-ndifference
5640            (gnus-sorted-ndifference gnus-newsgroup-unreads
5641                                     gnus-newsgroup-marked)
5642            gnus-newsgroup-dormant))
5643
5644     (setq gnus-newsgroup-processable nil)
5645
5646     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5647
5648     ;; Adjust and set lists of article marks.
5649     (when info
5650       (gnus-adjust-marked-articles info))
5651     (if (setq articles select-articles)
5652         (setq gnus-newsgroup-unselected
5653               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5654       (setq articles (gnus-articles-to-read group read-all)))
5655
5656     (cond
5657      ((null articles)
5658       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5659       'quit)
5660      ((eq articles 0) nil)
5661      (t
5662       ;; Init the dependencies hash table.
5663       (setq gnus-newsgroup-dependencies
5664             (gnus-make-hashtable (length articles)))
5665       (if (gnus-buffer-live-p gnus-group-buffer)
5666           (gnus-set-global-variables)
5667         (set-default 'gnus-newsgroup-name gnus-newsgroup-name))
5668       ;; Retrieve the headers and read them in.
5669
5670       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5671
5672       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5673       (when cached
5674         (setq gnus-newsgroup-cached cached))
5675
5676       ;; Suppress duplicates?
5677       (when gnus-suppress-duplicates
5678         (gnus-dup-suppress-articles))
5679
5680       ;; Set the initial limit.
5681       (setq gnus-newsgroup-limit (copy-sequence articles))
5682       ;; Remove canceled articles from the list of unread articles.
5683       (setq fetched-articles
5684             (mapcar (lambda (headers) (mail-header-number headers))
5685                     gnus-newsgroup-headers))
5686       (setq gnus-newsgroup-articles fetched-articles)
5687       (setq gnus-newsgroup-unreads
5688             (gnus-sorted-nintersection
5689              gnus-newsgroup-unreads fetched-articles))
5690       (gnus-compute-unseen-list)
5691
5692       ;; Removed marked articles that do not exist.
5693       (gnus-update-missing-marks
5694        (gnus-sorted-difference articles fetched-articles))
5695       ;; We might want to build some more threads first.
5696       (when (and gnus-fetch-old-headers
5697                  (eq gnus-headers-retrieved-by 'nov))
5698         (if (eq gnus-fetch-old-headers 'invisible)
5699             (gnus-build-all-threads)
5700           (gnus-build-old-threads)))
5701       ;; Let the Gnus agent mark articles as read.
5702       (when gnus-agent
5703         (gnus-agent-get-undownloaded-list))
5704       ;; Remove list identifiers from subject
5705       (gnus-summary-remove-list-identifiers)
5706       ;; Check whether auto-expire is to be done in this group.
5707       (setq gnus-newsgroup-auto-expire
5708             (and (gnus-group-auto-expirable-p group)
5709                  (not (gnus-group-read-only-p group))))
5710       ;; Set up the article buffer now, if necessary.
5711       (unless (and gnus-single-article-buffer
5712                    (equal gnus-article-buffer "*Article*"))
5713         (gnus-article-setup-buffer))
5714       ;; First and last article in this newsgroup.
5715       (when gnus-newsgroup-headers
5716         (setq gnus-newsgroup-begin
5717               (mail-header-number (car gnus-newsgroup-headers))
5718               gnus-newsgroup-end
5719               (mail-header-number
5720                (gnus-last-element gnus-newsgroup-headers))))
5721       ;; GROUP is successfully selected.
5722       (or gnus-newsgroup-headers t)))))
5723
5724 (defun gnus-compute-unseen-list ()
5725   ;; The `seen' marks are treated specially.
5726   (if (not gnus-newsgroup-seen)
5727       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5728     (setq gnus-newsgroup-unseen
5729           (gnus-inverse-list-range-intersection
5730            gnus-newsgroup-articles gnus-newsgroup-seen))))
5731
5732 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5733
5734 (defun gnus-summary-display-make-predicate (display)
5735   (require 'gnus-agent)
5736   (when (= (length display) 1)
5737     (setq display (car display)))
5738   (unless gnus-summary-display-cache
5739     (dolist (elem (append '((unread . unread)
5740                             (read . read)
5741                             (unseen . unseen))
5742                           gnus-article-mark-lists))
5743       (push (cons (cdr elem)
5744                   (gnus-byte-compile    ;Why bother?
5745                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5746             gnus-summary-display-cache)))
5747   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5748         (gnus-category-predicate-cache gnus-summary-display-cache))
5749     (gnus-get-predicate display)))
5750
5751 ;; Uses the dynamically bound `gnus-number' variable.
5752 (defvar gnus-number)
5753 (defun gnus-article-marked-p (type &optional article)
5754   (let ((article (or article gnus-number)))
5755     (cond
5756      ((eq type 'tick)
5757       (memq article gnus-newsgroup-marked))
5758      ((eq type 'spam)
5759       (memq article gnus-newsgroup-spam-marked))
5760      ((eq type 'unsend)
5761       (memq article gnus-newsgroup-unsendable))
5762      ((eq type 'undownload)
5763       (memq article gnus-newsgroup-undownloaded))
5764      ((eq type 'download)
5765       (memq article gnus-newsgroup-downloadable))
5766      ((eq type 'unread)
5767       (memq article gnus-newsgroup-unreads))
5768      ((eq type 'read)
5769       (memq article gnus-newsgroup-reads))
5770      ((eq type 'dormant)
5771       (memq article gnus-newsgroup-dormant) )
5772      ((eq type 'expire)
5773       (memq article gnus-newsgroup-expirable))
5774      ((eq type 'reply)
5775       (memq article gnus-newsgroup-replied))
5776      ((eq type 'killed)
5777       (memq article gnus-newsgroup-killed))
5778      ((eq type 'bookmark)
5779       (assq article gnus-newsgroup-bookmarks))
5780      ((eq type 'score)
5781       (assq article gnus-newsgroup-scored))
5782      ((eq type 'save)
5783       (memq article gnus-newsgroup-saved))
5784      ((eq type 'cache)
5785       (memq article gnus-newsgroup-cached))
5786      ((eq type 'forward)
5787       (memq article gnus-newsgroup-forwarded))
5788      ((eq type 'seen)
5789       (not (memq article gnus-newsgroup-unseen)))
5790      (t t))))
5791
5792 (defun gnus-articles-to-read (group &optional read-all)
5793   "Find out what articles the user wants to read."
5794   (let* ((only-read-p t)
5795          (articles
5796           (gnus-list-range-difference
5797           ;; Select all articles if `read-all' is non-nil, or if there
5798           ;; are no unread articles.
5799           (if (or read-all
5800                   (and (zerop (length gnus-newsgroup-marked))
5801                        (zerop (length gnus-newsgroup-unreads)))
5802                   ;; Fetch all if the predicate is non-nil.
5803                   gnus-newsgroup-display)
5804               ;; We want to select the headers for all the articles in
5805               ;; the group, so we select either all the active
5806               ;; articles in the group, or (if that's nil), the
5807               ;; articles in the cache.
5808               (or
5809                (if gnus-newsgroup-maximum-articles
5810                    (let ((active (gnus-active group)))
5811                      (gnus-uncompress-range
5812                       (cons (max (car active)
5813                                  (- (cdr active)
5814                                     gnus-newsgroup-maximum-articles
5815                                     -1))
5816                             (cdr active))))
5817                  (gnus-uncompress-range (gnus-active group)))
5818                (gnus-cache-articles-in-group group))
5819             ;; Select only the "normal" subset of articles.
5820             (setq only-read-p nil)
5821             (gnus-sorted-nunion
5822              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5823              gnus-newsgroup-unreads))
5824           (cdr (assq 'unexist (gnus-info-marks (gnus-get-info group))))))
5825          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5826          (scored (length scored-list))
5827          (number (length articles))
5828          (marked (+ (length gnus-newsgroup-marked)
5829                     (length gnus-newsgroup-dormant)))
5830          (select
5831           (cond
5832            ((numberp read-all)
5833             read-all)
5834            ((numberp gnus-newsgroup-display)
5835             gnus-newsgroup-display)
5836            (t
5837             (condition-case ()
5838                 (cond
5839                  ((and (or (<= scored marked) (= scored number))
5840                        (numberp gnus-large-newsgroup)
5841                        (> number gnus-large-newsgroup))
5842                   (let* ((cursor-in-echo-area nil)
5843                          (initial (gnus-parameter-large-newsgroup-initial
5844                                    gnus-newsgroup-name))
5845                          (default (if only-read-p
5846                                       (or initial gnus-large-newsgroup)
5847                                     number))
5848                          (input
5849                           (read-string
5850                            (if only-read-p
5851                                (format
5852                                 "How many articles from %s (available %d, default %d): "
5853                                 (gnus-group-decoded-name
5854                                  (gnus-group-real-name gnus-newsgroup-name))
5855                                 number default)
5856                              (format
5857                               "How many articles from %s (%d default): "
5858                               (gnus-group-decoded-name
5859                                (gnus-group-real-name gnus-newsgroup-name))
5860                               default))
5861                            nil
5862                            nil
5863                            (number-to-string default))))
5864                     (if (string-match "^[ \t]*$" input) number input)))
5865                  ((and (> scored marked) (< scored number)
5866                        (> (- scored number) 20))
5867                   (let ((input
5868                          (read-string
5869                           (format "%s %s (%d scored, %d total): "
5870                                   "How many articles from"
5871                                   (gnus-group-decoded-name
5872                                    (gnus-group-real-name gnus-newsgroup-name))
5873                                   scored number))))
5874                     (if (string-match "^[ \t]*$" input)
5875                         number input)))
5876                  (t number))
5877               (quit
5878                (message "Quit getting the articles to read")
5879                nil))))))
5880     (setq select (if (stringp select) (string-to-number select) select))
5881     (if (or (null select) (zerop select))
5882         select
5883       (if (and (not (zerop scored)) (<= (abs select) scored))
5884           (progn
5885             (setq articles (sort scored-list '<))
5886             (setq number (length articles)))
5887         (setq articles (copy-sequence articles)))
5888
5889       (when (< (abs select) number)
5890         (if (< select 0)
5891             ;; Select the N oldest articles.
5892             (setcdr (nthcdr (1- (abs select)) articles) nil)
5893           ;; Select the N most recent articles.
5894           (setq articles (nthcdr (- number select) articles))))
5895       (setq gnus-newsgroup-unselected
5896             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5897       (when gnus-alter-articles-to-read-function
5898         (setq articles
5899               (sort
5900                (funcall gnus-alter-articles-to-read-function
5901                         gnus-newsgroup-name articles)
5902                '<)))
5903       articles)))
5904
5905 (defun gnus-killed-articles (killed articles)
5906   (let (out)
5907     (while articles
5908       (when (inline (gnus-member-of-range (car articles) killed))
5909         (push (car articles) out))
5910       (setq articles (cdr articles)))
5911     out))
5912
5913 (defun gnus-article-mark-to-type (mark)
5914   "Return the type of MARK."
5915   (or (cadr (assq mark gnus-article-special-mark-lists))
5916       'list))
5917
5918 (defun gnus-article-unpropagatable-p (mark)
5919   "Return whether MARK should be propagated to back end."
5920   (memq mark gnus-article-unpropagated-mark-lists))
5921
5922 (defun gnus-adjust-marked-articles (info)
5923   "Set all article lists and remove all marks that are no longer valid."
5924   (let* ((marked-lists (gnus-info-marks info))
5925          (active (gnus-active (gnus-info-group info)))
5926          (min (car active))
5927          (max (cdr active))
5928          (types gnus-article-mark-lists)
5929          marks var articles article mark mark-type
5930          bgn end)
5931     ;; Hack to avoid adjusting marks for imap.
5932     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5933               'nnimap)
5934       (setq min 1))
5935
5936     (dolist (marks marked-lists)
5937       (setq mark (car marks)
5938             mark-type (gnus-article-mark-to-type mark)
5939             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5940       ;; We set the variable according to the type of the marks list,
5941       ;; and then adjust the marks to a subset of the active articles.
5942       (cond
5943        ;; Adjust "simple" lists - compressed yet unsorted
5944        ((eq mark-type 'list)
5945         ;; Simultaneously uncompress and clip to active range
5946         ;; See gnus-uncompress-range for a description of possible marks
5947         (let (l lh)
5948           (if (not (cadr marks))
5949               (set var nil)
5950             (setq articles (if (numberp (cddr marks))
5951                                (list (cdr marks))
5952                              (cdr marks))
5953                   lh (cons nil nil)
5954                   l lh)
5955
5956             (while (setq article (pop articles))
5957               (cond ((consp article)
5958                      (setq bgn (max (car article) min)
5959                            end (min (cdr article) max))
5960                      (while (<= bgn end)
5961                        (setq l (setcdr l (cons bgn nil))
5962                              bgn (1+ bgn))))
5963                     ((and (<= min article)
5964                           (>= max article))
5965                      (setq l (setcdr l (cons article nil))))))
5966             (set var (cdr lh)))))
5967        ;; Adjust assocs.
5968        ((eq mark-type 'tuple)
5969         (set var (setq articles (cdr marks)))
5970         (when (not (listp (cdr (symbol-value var))))
5971           (set var (list (symbol-value var))))
5972         (when (not (listp (cdr articles)))
5973           (setq articles (list articles)))
5974         (while articles
5975           (when (or (not (consp (setq article (pop articles))))
5976                     (< (car article) min)
5977                     (> (car article) max))
5978             (set var (delq article (symbol-value var))))))
5979        ;; Adjust ranges (sloppily).
5980        ((eq mark-type 'range)
5981         (cond
5982          ((eq mark 'seen)
5983           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5984           ;; It should be (seen (NUM1 . NUM2)).
5985           (when (numberp (cddr marks))
5986             (setcdr marks (list (cdr marks))))
5987           (setq articles (cdr marks))
5988           (while (and articles
5989                       (or (and (consp (car articles))
5990                                (> min (cdar articles)))
5991                           (and (numberp (car articles))
5992                                (> min (car articles)))))
5993             (pop articles))
5994           (set var articles))
5995          ((eq mark 'unexist)
5996           (set var (cdr marks)))))))))
5997
5998 (defun gnus-update-missing-marks (missing)
5999   "Go through the list of MISSING articles and remove them from the mark lists."
6000   (when missing
6001     (let (var m)
6002       ;; Go through all types.
6003       (dolist (elem gnus-article-mark-lists)
6004         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6005           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6006           (when (symbol-value var)
6007             ;; This list has articles.  So we delete all missing
6008             ;; articles from it.
6009             (setq m missing)
6010             (while m
6011               (set var (delq (pop m) (symbol-value var))))))))))
6012
6013 (defun gnus-update-marks ()
6014   "Enter the various lists of marked articles into the newsgroup info list."
6015   (let ((types gnus-article-mark-lists)
6016         (info (gnus-get-info gnus-newsgroup-name))
6017         type list newmarked symbol delta-marks)
6018     (when info
6019       ;; Add all marks lists to the list of marks lists.
6020       (while (setq type (pop types))
6021         (setq list (symbol-value
6022                     (setq symbol
6023                           (intern (format "gnus-newsgroup-%s" (car type))))))
6024
6025         (when list
6026           ;; Get rid of the entries of the articles that have the
6027           ;; default score.
6028           (when (and (eq (cdr type) 'score)
6029                      gnus-save-score
6030                      list)
6031             (let* ((arts list)
6032                    (prev (cons nil list))
6033                    (all prev))
6034               (while arts
6035                 (if (or (not (consp (car arts)))
6036                         (= (cdar arts) gnus-summary-default-score))
6037                     (setcdr prev (cdr arts))
6038                   (setq prev arts))
6039                 (setq arts (cdr arts)))
6040               (setq list (cdr all)))))
6041
6042         (when (eq (cdr type) 'seen)
6043           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6044
6045         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6046           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6047
6048         (when (and (gnus-check-backend-function
6049                     'request-set-mark gnus-newsgroup-name)
6050                    (not (gnus-article-unpropagatable-p (cdr type))))
6051           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6052                  ;; Don't do anything about marks for articles we
6053                  ;; didn't actually get any headers for.
6054                  (del
6055                   (gnus-list-range-intersection
6056                    gnus-newsgroup-articles
6057                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6058                  (add
6059                   (gnus-list-range-intersection
6060                    gnus-newsgroup-articles
6061                    (gnus-remove-from-range
6062                     (gnus-copy-sequence list) old))))
6063             (when add
6064               (push (list add 'add (list (cdr type))) delta-marks))
6065             (when del
6066               ;; Don't delete marks from outside the active range.
6067               ;; This shouldn't happen, but is a sanity check.
6068               (setq del (gnus-sorted-range-intersection
6069                          (gnus-active gnus-newsgroup-name) del))
6070               (push (list del 'del (list (cdr type))) delta-marks))))
6071
6072         (when (or list
6073                   (eq (cdr type) 'unexist))
6074           (push (cons (cdr type) list) newmarked)))
6075
6076       (when delta-marks
6077         (unless (gnus-check-group gnus-newsgroup-name)
6078           (error "Can't open server for %s" gnus-newsgroup-name))
6079         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6080
6081       ;; Enter these new marks into the info of the group.
6082       (if (nthcdr 3 info)
6083           (setcar (nthcdr 3 info) newmarked)
6084         ;; Add the marks lists to the end of the info.
6085         (when newmarked
6086           (setcdr (nthcdr 2 info) (list newmarked))))
6087
6088       ;; Cut off the end of the info if there's nothing else there.
6089       (let ((i 5))
6090         (while (and (> i 2)
6091                     (not (nth i info)))
6092           (when (nthcdr (decf i) info)
6093             (setcdr (nthcdr i info) nil)))))))
6094
6095 (defun gnus-set-mode-line (where)
6096   "Set the mode line of the article or summary buffers.
6097 If WHERE is `summary', the summary mode line format will be used."
6098   ;; Is this mode line one we keep updated?
6099   (when (and (memq where gnus-updated-mode-lines)
6100              (symbol-value
6101               (intern (format "gnus-%s-mode-line-format-spec" where))))
6102     (let (mode-string)
6103       ;; We evaluate this in the summary buffer since these
6104       ;; variables are buffer-local to that buffer.
6105       (with-current-buffer gnus-summary-buffer
6106         ;; We bind all these variables that are used in the `eval' form
6107         ;; below.
6108         (let* ((mformat (symbol-value
6109                          (intern
6110                           (format "gnus-%s-mode-line-format-spec" where))))
6111                (gnus-tmp-group-name (gnus-mode-string-quote
6112                                      (gnus-group-decoded-name
6113                                       gnus-newsgroup-name)))
6114                (gnus-tmp-article-number (or gnus-current-article 0))
6115                (gnus-tmp-unread gnus-newsgroup-unreads)
6116                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6117                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6118                (gnus-tmp-unread-and-unselected
6119                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6120                             (zerop gnus-tmp-unselected))
6121                        "")
6122                       ((zerop gnus-tmp-unselected)
6123                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6124                       (t (format "{%d(+%d) more}"
6125                                  gnus-tmp-unread-and-unticked
6126                                  gnus-tmp-unselected))))
6127                (gnus-tmp-subject
6128                 (if (and gnus-current-headers
6129                          (vectorp gnus-current-headers))
6130                     (gnus-mode-string-quote
6131                      (mail-header-subject gnus-current-headers))
6132                   ""))
6133                bufname-length max-len
6134                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6135           (setq mode-string (eval mformat))
6136           (setq bufname-length (if (string-match "%b" mode-string)
6137                                    (- (length
6138                                        (buffer-name
6139                                         (if (eq where 'summary)
6140                                             nil
6141                                           (get-buffer gnus-article-buffer))))
6142                                       2)
6143                                  0))
6144           (setq max-len (max 4 (if gnus-mode-non-string-length
6145                                    (- (window-width)
6146                                       gnus-mode-non-string-length
6147                                       bufname-length)
6148                                  (length mode-string))))
6149           ;; We might have to chop a bit of the string off...
6150           (when (> (length mode-string) max-len)
6151             (setq mode-string
6152                   (concat (truncate-string-to-width mode-string (- max-len 3))
6153                           "...")))))
6154       ;; Update the mode line.
6155       (setq mode-line-buffer-identification
6156             (gnus-mode-line-buffer-identification (list mode-string)))
6157       (set-buffer-modified-p t))))
6158
6159 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6160   "Go through the HEADERS list and add all Xrefs to a hash table.
6161 The resulting hash table is returned, or nil if no Xrefs were found."
6162   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6163          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6164          (xref-hashtb (gnus-make-hashtable))
6165          start group entry number xrefs header)
6166     (while headers
6167       (setq header (pop headers))
6168       (when (and (setq xrefs (mail-header-xref header))
6169                  (not (memq (setq number (mail-header-number header))
6170                             unreads)))
6171         (setq start 0)
6172         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6173           (setq start (match-end 0))
6174           (setq group (if prefix
6175                           (concat prefix (substring xrefs (match-beginning 1)
6176                                                     (match-end 1)))
6177                         (substring xrefs (match-beginning 1) (match-end 1))))
6178           (setq number
6179                 (string-to-number (substring xrefs (match-beginning 2)
6180                                           (match-end 2))))
6181           (if (setq entry (gnus-gethash group xref-hashtb))
6182               (setcdr entry (cons number (cdr entry)))
6183             (gnus-sethash group (cons number nil) xref-hashtb)))))
6184     (and start xref-hashtb)))
6185
6186 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6187   "Look through all the headers and mark the Xrefs as read."
6188   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6189         name info xref-hashtb idlist method nth4)
6190     (with-current-buffer gnus-group-buffer
6191       (when (setq xref-hashtb
6192                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6193         (mapatoms
6194          (lambda (group)
6195            (unless (string= from-newsgroup (setq name (symbol-name group)))
6196              (setq idlist (symbol-value group))
6197              ;; Dead groups are not updated.
6198              (and (prog1
6199                       (setq info (gnus-get-info name))
6200                     (when (stringp (setq nth4 (gnus-info-method info)))
6201                       (setq nth4 (gnus-server-to-method nth4))))
6202                   ;; Only do the xrefs if the group has the same
6203                   ;; select method as the group we have just read.
6204                   (or (gnus-methods-equal-p
6205                        nth4 (gnus-find-method-for-group from-newsgroup))
6206                       virtual
6207                       (equal nth4 (setq method (gnus-find-method-for-group
6208                                                 from-newsgroup)))
6209                       (and (equal (car nth4) (car method))
6210                            (equal (nth 1 nth4) (nth 1 method))))
6211                   gnus-use-cross-reference
6212                   (or (not (eq gnus-use-cross-reference t))
6213                       virtual
6214                       ;; Only do cross-references on subscribed
6215                       ;; groups, if that is what is wanted.
6216                       (<= (gnus-info-level info) gnus-level-subscribed))
6217                   (gnus-group-make-articles-read name idlist))))
6218          xref-hashtb)))))
6219
6220 (defun gnus-compute-read-articles (group articles)
6221   (let* ((entry (gnus-group-entry group))
6222          (info (nth 2 entry))
6223          (active (gnus-active group))
6224          ninfo)
6225     (when entry
6226       ;; First peel off all invalid article numbers.
6227       (when active
6228         (let ((ids articles)
6229               id first)
6230           (while (setq id (pop ids))
6231             (when (and first (> id (cdr active)))
6232               ;; We'll end up in this situation in one particular
6233               ;; obscure situation.  If you re-scan a group and get
6234               ;; a new article that is cross-posted to a different
6235               ;; group that has not been re-scanned, you might get
6236               ;; crossposted article that has a higher number than
6237               ;; Gnus believes possible.  So we re-activate this
6238               ;; group as well.  This might mean doing the
6239               ;; crossposting thingy will *increase* the number
6240               ;; of articles in some groups.  Tsk, tsk.
6241               (setq active (or (gnus-activate-group group) active)))
6242             (when (or (> id (cdr active))
6243                       (< id (car active)))
6244               (setq articles (delq id articles))))))
6245       ;; If the read list is nil, we init it.
6246       (if (and active
6247                (null (gnus-info-read info))
6248                (> (car active) 1))
6249           (setq ninfo (cons 1 (1- (car active))))
6250         (setq ninfo (gnus-info-read info)))
6251       ;; Then we add the read articles to the range.
6252       (gnus-add-to-range
6253        ninfo (setq articles (sort articles '<))))))
6254
6255 (defun gnus-group-make-articles-read (group articles)
6256   "Update the info of GROUP to say that ARTICLES are read."
6257   (let* ((num 0)
6258          (entry (gnus-group-entry group))
6259          (info (nth 2 entry))
6260          (active (gnus-active group))
6261          (set-marks
6262           (gnus-method-option-p
6263            (gnus-find-method-for-group group)
6264            'server-marks))
6265          range)
6266     (if (not entry)
6267         ;; Group that Gnus doesn't know exists, but still allow the
6268         ;; backend to set marks.
6269         (when set-marks
6270           (gnus-request-set-mark
6271            group (list (list (gnus-compress-sequence (sort articles #'<))
6272                              'add '(read)))))
6273       ;; Normal, subscribed groups.
6274       (setq range (gnus-compute-read-articles group articles))
6275       (with-current-buffer gnus-group-buffer
6276         (gnus-undo-register
6277           `(progn
6278              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6279              (gnus-info-set-read ',info ',(gnus-info-read info))
6280              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6281              (when ,set-marks
6282                (gnus-request-set-mark
6283                 ,group (list (list ',range 'del '(read)))))
6284              (gnus-group-update-group ,group t))))
6285       ;; Add the read articles to the range.
6286       (gnus-info-set-read info range)
6287       (when set-marks
6288         (gnus-request-set-mark group (list (list range 'add '(read)))))
6289       ;; Then we have to re-compute how many unread
6290       ;; articles there are in this group.
6291       (when active
6292         (cond
6293          ((not range)
6294           (setq num (- (1+ (cdr active)) (car active))))
6295          ((not (listp (cdr range)))
6296           (setq num (- (cdr active) (- (1+ (cdr range))
6297                                        (car range)))))
6298          (t
6299           (while range
6300             (if (numberp (car range))
6301                 (setq num (1+ num))
6302               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6303             (setq range (cdr range)))
6304           (setq num (- (cdr active) num))))
6305         ;; Update the number of unread articles.
6306         (setcar entry num)
6307         ;; Update the group buffer.
6308         (unless (gnus-ephemeral-group-p group)
6309           (gnus-group-update-group group t))))))
6310
6311 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6312   (let ((cur nntp-server-buffer)
6313         (dependencies
6314          (or dependencies
6315              (with-current-buffer gnus-summary-buffer
6316                gnus-newsgroup-dependencies)))
6317         headers id end ref number
6318         (mail-parse-charset gnus-newsgroup-charset)
6319         (mail-parse-ignored-charsets
6320          (save-current-buffer (condition-case nil
6321                                   (set-buffer gnus-summary-buffer)
6322                                 (error))
6323                               gnus-newsgroup-ignored-charsets)))
6324     (with-current-buffer nntp-server-buffer
6325       ;; Translate all TAB characters into SPACE characters.
6326       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6327       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6328       (ietf-drums-unfold-fws)
6329       (gnus-run-hooks 'gnus-parse-headers-hook)
6330       (let ((case-fold-search t)
6331             in-reply-to header p lines chars)
6332         (goto-char (point-min))
6333         ;; Search to the beginning of the next header.  Error messages
6334         ;; do not begin with 2 or 3.
6335         (while (re-search-forward "^[23][0-9]+ " nil t)
6336           (setq id nil
6337                 ref nil)
6338           ;; This implementation of this function, with nine
6339           ;; search-forwards instead of the one re-search-forward and
6340           ;; a case (which basically was the old function) is actually
6341           ;; about twice as fast, even though it looks messier.  You
6342           ;; can't have everything, I guess.  Speed and elegance
6343           ;; doesn't always go hand in hand.
6344           (setq
6345            header
6346            (vector
6347             ;; Number.
6348             (prog1
6349                 (setq number (read cur))
6350               (end-of-line)
6351               (setq p (point))
6352               (narrow-to-region (point)
6353                                 (or (and (search-forward "\n.\n" nil t)
6354                                          (- (point) 2))
6355                                     (point))))
6356             ;; Subject.
6357             (progn
6358               (goto-char p)
6359               (if (search-forward "\nsubject:" nil t)
6360                   (funcall gnus-decode-encoded-word-function
6361                            (nnheader-header-value))
6362                 "(none)"))
6363             ;; From.
6364             (progn
6365               (goto-char p)
6366               (if (search-forward "\nfrom:" nil t)
6367                   (funcall gnus-decode-encoded-address-function
6368                            (nnheader-header-value))
6369                 "(nobody)"))
6370             ;; Date.
6371             (progn
6372               (goto-char p)
6373               (if (search-forward "\ndate:" nil t)
6374                   (nnheader-header-value) ""))
6375             ;; Message-ID.
6376             (progn
6377               (goto-char p)
6378               (setq id (if (re-search-forward
6379                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6380                            ;; We do it this way to make sure the Message-ID
6381                            ;; is (somewhat) syntactically valid.
6382                            (buffer-substring (match-beginning 1)
6383                                              (match-end 1))
6384                          ;; If there was no message-id, we just fake one
6385                          ;; to make subsequent routines simpler.
6386                          (nnheader-generate-fake-message-id number))))
6387             ;; References.
6388             (progn
6389               (goto-char p)
6390               (if (search-forward "\nreferences:" nil t)
6391                   (progn
6392                     (setq end (point))
6393                     (prog1
6394                         (nnheader-header-value)
6395                       (setq ref
6396                             (buffer-substring
6397                              (progn
6398                                (end-of-line)
6399                                (search-backward ">" end t)
6400                                (1+ (point)))
6401                              (progn
6402                                (search-backward "<" end t)
6403                                (point))))))
6404                 ;; Get the references from the in-reply-to header if there
6405                 ;; were no references and the in-reply-to header looks
6406                 ;; promising.
6407                 (if (and (search-forward "\nin-reply-to:" nil t)
6408                          (setq in-reply-to (nnheader-header-value))
6409                          (string-match "<[^>]+>" in-reply-to))
6410                     (let (ref2)
6411                       (setq ref (substring in-reply-to (match-beginning 0)
6412                                            (match-end 0)))
6413                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6414                         (setq ref2 (substring in-reply-to (match-beginning 0)
6415                                               (match-end 0)))
6416                         (when (> (length ref2) (length ref))
6417                           (setq ref ref2)))
6418                       ref)
6419                   (setq ref nil))))
6420             ;; Chars.
6421             (progn
6422               (goto-char p)
6423               (if (search-forward "\nchars: " nil t)
6424                   (if (numberp (setq chars (ignore-errors (read cur))))
6425                       chars -1)
6426                 -1))
6427             ;; Lines.
6428             (progn
6429               (goto-char p)
6430               (if (search-forward "\nlines: " nil t)
6431                   (if (numberp (setq lines (ignore-errors (read cur))))
6432                       lines -1)
6433                 -1))
6434             ;; Xref.
6435             (progn
6436               (goto-char p)
6437               (and (search-forward "\nxref:" nil t)
6438                    (nnheader-header-value)))
6439             ;; Extra.
6440             (when gnus-extra-headers
6441               (let ((extra gnus-extra-headers)
6442                     out)
6443                 (while extra
6444                   (goto-char p)
6445                   (when (search-forward
6446                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6447                     (push (cons (car extra) (nnheader-header-value))
6448                           out))
6449                   (pop extra))
6450                 out))))
6451           (when (equal id ref)
6452             (setq ref nil))
6453
6454           (when gnus-alter-header-function
6455             (funcall gnus-alter-header-function header)
6456             (setq id (mail-header-id header)
6457                   ref (gnus-parent-id (mail-header-references header))))
6458
6459           (when (setq header
6460                       (gnus-dependencies-add-header
6461                        header dependencies force-new))
6462             (push header headers))
6463           (goto-char (point-max))
6464           (widen))
6465         (nreverse headers)))))
6466
6467 ;; Goes through the xover lines and returns a list of vectors
6468 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6469                                                   force-new dependencies
6470                                                   group also-fetch-heads)
6471   "Parse the news overview data in the server buffer.
6472 Return a list of headers that match SEQUENCE (see
6473 `nntp-retrieve-headers')."
6474   ;; Get the Xref when the users reads the articles since most/some
6475   ;; NNTP servers do not include Xrefs when using XOVER.
6476   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6477   (let ((mail-parse-charset gnus-newsgroup-charset)
6478         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6479         (cur nntp-server-buffer)
6480         (dependencies (or dependencies gnus-newsgroup-dependencies))
6481         (allp (cond
6482                ((eq gnus-read-all-available-headers t)
6483                 t)
6484                ((and (stringp gnus-read-all-available-headers)
6485                      group)
6486                 (string-match gnus-read-all-available-headers group))
6487                (t
6488                 nil)))
6489         number headers header)
6490     (with-current-buffer nntp-server-buffer
6491       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6492       ;; Allow the user to mangle the headers before parsing them.
6493       (gnus-run-hooks 'gnus-parse-headers-hook)
6494       (goto-char (point-min))
6495       (gnus-parse-without-error
6496         (while (and (or sequence allp)
6497                     (not (eobp)))
6498           (setq number (read cur))
6499           (when (not allp)
6500             (while (and sequence
6501                         (< (car sequence) number))
6502               (setq sequence (cdr sequence))))
6503           (when (and (or allp
6504                          (and sequence
6505                               (eq number (car sequence))))
6506                      (progn
6507                        (setq sequence (cdr sequence))
6508                        (setq header (inline
6509                                       (gnus-nov-parse-line
6510                                        number dependencies force-new)))))
6511             (push header headers))
6512           (forward-line 1)))
6513       ;; A common bug in inn is that if you have posted an article and
6514       ;; then retrieves the active file, it will answer correctly --
6515       ;; the new article is included.  However, a NOV entry for the
6516       ;; article may not have been generated yet, so this may fail.
6517       ;; We work around this problem by retrieving the last few
6518       ;; headers using HEAD.
6519       (if (or (not also-fetch-heads)
6520               (not sequence))
6521           ;; We (probably) got all the headers.
6522           (nreverse headers)
6523         (let ((gnus-nov-is-evil t))
6524           (nconc
6525            (nreverse headers)
6526            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6527              (gnus-get-newsgroup-headers))))))))
6528
6529 (defun gnus-article-get-xrefs ()
6530   "Fill in the Xref value in `gnus-current-headers', if necessary.
6531 This is meant to be called in `gnus-article-internal-prepare-hook'."
6532   (let ((headers (with-current-buffer gnus-summary-buffer
6533                    gnus-current-headers)))
6534     (or (not gnus-use-cross-reference)
6535         (not headers)
6536         (and (mail-header-xref headers)
6537              (not (string= (mail-header-xref headers) "")))
6538         (let ((case-fold-search t)
6539               xref)
6540           (save-restriction
6541             (nnheader-narrow-to-headers)
6542             (goto-char (point-min))
6543             (when (or (and (not (eobp))
6544                            (eq (downcase (char-after)) ?x)
6545                            (looking-at "Xref:"))
6546                       (search-forward "\nXref:" nil t))
6547               (goto-char (1+ (match-end 0)))
6548               (setq xref (buffer-substring (point) (point-at-eol)))
6549               (mail-header-set-xref headers xref)))))))
6550
6551 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6552   "Find article ID and insert the summary line for that article.
6553 OLD-HEADER can either be a header or a line number to insert
6554 the subject line on.
6555 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6556 and OLD-HEADER will be used when the summary line is inserted,
6557 too, instead of trying to fetch new headers."
6558   (let* ((line (and (numberp old-header) old-header))
6559          (old-header (and (vectorp old-header) old-header))
6560          (header (cond ((and old-header use-old-header)
6561                         old-header)
6562                        ((and (numberp id)
6563                              (gnus-number-to-header id))
6564                         (gnus-number-to-header id))
6565                        (t
6566                         (gnus-read-header id))))
6567          (number (and (numberp id) id))
6568          d)
6569     (when header
6570       ;; Rebuild the thread that this article is part of and go to the
6571       ;; article we have fetched.
6572       (when (and (not gnus-show-threads)
6573                  old-header)
6574         (when (and number
6575                    (setq d (gnus-data-find (mail-header-number old-header))))
6576           (goto-char (gnus-data-pos d))
6577           (gnus-data-remove
6578            number
6579            (- (point-at-bol)
6580               (prog1
6581                   (1+ (point-at-eol))
6582                 (gnus-delete-line))))))
6583       ;; Remove list identifiers from subject.
6584       (let ((gnus-newsgroup-headers (list header)))
6585         (gnus-summary-remove-list-identifiers))
6586       (when old-header
6587         (mail-header-set-number header (mail-header-number old-header)))
6588       (setq gnus-newsgroup-sparse
6589             (delq (setq number (mail-header-number header))
6590                   gnus-newsgroup-sparse))
6591       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6592       (push number gnus-newsgroup-limit)
6593       (gnus-rebuild-thread (mail-header-id header) line)
6594       (gnus-summary-goto-subject number nil t))
6595     (when (and (numberp number)
6596                (> number 0))
6597       ;; We have to update the boundaries even if we can't fetch the
6598       ;; article if ID is a number -- so that the next `P' or `N'
6599       ;; command will fetch the previous (or next) article even
6600       ;; if the one we tried to fetch this time has been canceled.
6601       (unless (and gnus-newsgroup-end (< number gnus-newsgroup-end))
6602         (setq gnus-newsgroup-end number))
6603       (unless (and gnus-newsgroup-begin (> number gnus-newsgroup-begin))
6604         (setq gnus-newsgroup-begin number))
6605       (setq gnus-newsgroup-unselected
6606             (delq number gnus-newsgroup-unselected)))
6607     ;; Report back a success?
6608     (and header (mail-header-number header))))
6609
6610 ;;; Process/prefix in the summary buffer
6611
6612 (defun gnus-summary-work-articles (n)
6613   "Return a list of articles to be worked upon.
6614 The prefix argument, the list of process marked articles, and the
6615 current article will be taken into consideration."
6616   (with-current-buffer gnus-summary-buffer
6617     (cond
6618      (n
6619       ;; A numerical prefix has been given.
6620       (setq n (prefix-numeric-value n))
6621       (let ((backward (< n 0))
6622             (n (abs (prefix-numeric-value n)))
6623             articles article)
6624         (save-excursion
6625           (while
6626               (and (> n 0)
6627                    (push (setq article (gnus-summary-article-number))
6628                          articles)
6629                    (if backward
6630                        (gnus-summary-find-prev nil article)
6631                      (gnus-summary-find-next nil article)))
6632             (decf n)))
6633         (nreverse articles)))
6634      ((and (gnus-region-active-p) (mark))
6635       (message "region active")
6636       ;; Work on the region between point and mark.
6637       (let ((max (max (point) (mark)))
6638             articles article)
6639         (save-excursion
6640           (goto-char (min (point) (mark)))
6641           (while
6642               (and
6643                (push (setq article (gnus-summary-article-number)) articles)
6644                (gnus-summary-find-next nil article)
6645                (< (point) max)))
6646           (nreverse articles))))
6647      (gnus-newsgroup-processable
6648       ;; There are process-marked articles present.
6649       ;; Save current state.
6650       (gnus-summary-save-process-mark)
6651       ;; Return the list.
6652       (reverse gnus-newsgroup-processable))
6653      (t
6654       ;; Just return the current article.
6655       (list (gnus-summary-article-number))))))
6656
6657 (defmacro gnus-summary-iterate (arg &rest forms)
6658   "Iterate over the process/prefixed articles and do FORMS.
6659 ARG is the interactive prefix given to the command.  FORMS will be
6660 executed with point over the summary line of the articles."
6661   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6662     `(let ((,articles (gnus-summary-work-articles ,arg)))
6663        (while ,articles
6664          (gnus-summary-goto-subject (car ,articles))
6665          ,@forms
6666          (pop ,articles)))))
6667
6668 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6669 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6670
6671 (defun gnus-summary-save-process-mark ()
6672   "Push the current set of process marked articles on the stack."
6673   (interactive)
6674   (push (copy-sequence gnus-newsgroup-processable)
6675         gnus-newsgroup-process-stack))
6676
6677 (defun gnus-summary-kill-process-mark ()
6678   "Push the current set of process marked articles on the stack and unmark."
6679   (interactive)
6680   (gnus-summary-save-process-mark)
6681   (gnus-summary-unmark-all-processable))
6682
6683 (defun gnus-summary-yank-process-mark ()
6684   "Pop the last process mark state off the stack and restore it."
6685   (interactive)
6686   (unless gnus-newsgroup-process-stack
6687     (error "Empty mark stack"))
6688   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6689
6690 (defun gnus-summary-process-mark-set (set)
6691   "Make SET into the current process marked articles."
6692   (gnus-summary-unmark-all-processable)
6693   (mapc 'gnus-summary-set-process-mark set))
6694
6695 ;;; Searching and stuff
6696
6697 (defun gnus-summary-search-group (&optional backward use-level)
6698   "Search for next unread newsgroup.
6699 If optional argument BACKWARD is non-nil, search backward instead."
6700   (with-current-buffer gnus-group-buffer
6701     (when (gnus-group-search-forward
6702            backward nil (if use-level (gnus-group-group-level) nil))
6703       (gnus-group-group-name))))
6704
6705 (defun gnus-summary-best-group (&optional exclude-group)
6706   "Find the name of the best unread group.
6707 If EXCLUDE-GROUP, do not go to this group."
6708   (with-current-buffer gnus-group-buffer
6709     (save-excursion
6710       (gnus-group-best-unread-group exclude-group))))
6711
6712 (defun gnus-summary-find-next (&optional unread article backward)
6713   (if backward
6714       (gnus-summary-find-prev unread article)
6715     (let* ((dummy (gnus-summary-article-intangible-p))
6716            (article (or article (gnus-summary-article-number)))
6717            (data (gnus-data-find-list article))
6718            result)
6719       (when (and (not dummy)
6720                  (or (not gnus-summary-check-current)
6721                      (not unread)
6722                      (not (gnus-data-unread-p (car data)))))
6723         (setq data (cdr data)))
6724       (when (setq result
6725                   (if unread
6726                       (progn
6727                         (while data
6728                           (unless (memq (gnus-data-number (car data))
6729                                         (cond
6730                                          ((eq gnus-auto-goto-ignores
6731                                               'always-undownloaded)
6732                                           gnus-newsgroup-undownloaded)
6733                                          (gnus-plugged
6734                                           nil)
6735                                          ((eq gnus-auto-goto-ignores
6736                                               'unfetched)
6737                                           gnus-newsgroup-unfetched)
6738                                          ((eq gnus-auto-goto-ignores
6739                                               'undownloaded)
6740                                           gnus-newsgroup-undownloaded)))
6741                             (when (gnus-data-unread-p (car data))
6742                               (setq result (car data)
6743                                     data nil)))
6744                           (setq data (cdr data)))
6745                         result)
6746                     (car data)))
6747         (goto-char (gnus-data-pos result))
6748         (gnus-data-number result)))))
6749
6750 (defun gnus-summary-find-prev (&optional unread article)
6751   (let* ((eobp (eobp))
6752          (article (or article (gnus-summary-article-number)))
6753          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6754          result)
6755     (when (and (not eobp)
6756                (or (not gnus-summary-check-current)
6757                    (not unread)
6758                    (not (gnus-data-unread-p (car data)))))
6759       (setq data (cdr data)))
6760     (when (setq result
6761                 (if unread
6762                     (progn
6763                       (while data
6764                         (unless (memq (gnus-data-number (car data))
6765                                       (cond
6766                                        ((eq gnus-auto-goto-ignores
6767                                             'always-undownloaded)
6768                                         gnus-newsgroup-undownloaded)
6769                                        (gnus-plugged
6770                                         nil)
6771                                        ((eq gnus-auto-goto-ignores
6772                                             'unfetched)
6773                                         gnus-newsgroup-unfetched)
6774                                        ((eq gnus-auto-goto-ignores
6775                                             'undownloaded)
6776                                         gnus-newsgroup-undownloaded)))
6777                           (when (gnus-data-unread-p (car data))
6778                             (setq result (car data)
6779                                   data nil)))
6780                         (setq data (cdr data)))
6781                       result)
6782                   (car data)))
6783       (goto-char (gnus-data-pos result))
6784       (gnus-data-number result))))
6785
6786 (defun gnus-summary-find-subject (subject &optional unread backward article)
6787   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6788          (article (or article (gnus-summary-article-number)))
6789          (articles (gnus-data-list backward))
6790          (arts (gnus-data-find-list article articles))
6791          result)
6792     (when (or (not gnus-summary-check-current)
6793               (not unread)
6794               (not (gnus-data-unread-p (car arts))))
6795       (setq arts (cdr arts)))
6796     (while arts
6797       (and (or (not unread)
6798                (gnus-data-unread-p (car arts)))
6799            (vectorp (gnus-data-header (car arts)))
6800            (gnus-subject-equal
6801             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6802            (setq result (car arts)
6803                  arts nil))
6804       (setq arts (cdr arts)))
6805     (and result
6806          (goto-char (gnus-data-pos result))
6807          (gnus-data-number result))))
6808
6809 (defun gnus-summary-search-forward (&optional unread subject backward)
6810   "Search forward for an article.
6811 If UNREAD, look for unread articles.  If SUBJECT, look for
6812 articles with that subject.  If BACKWARD, search backward instead."
6813   (cond (subject (gnus-summary-find-subject subject unread backward))
6814         (backward (gnus-summary-find-prev unread))
6815         (t (gnus-summary-find-next unread))))
6816
6817 (defun gnus-recenter (&optional n)
6818   "Center point in window and redisplay frame.
6819 Also do horizontal recentering."
6820   (interactive "P")
6821   (when (and gnus-auto-center-summary
6822              (not (eq gnus-auto-center-summary 'vertical)))
6823     (gnus-horizontal-recenter))
6824   (if (fboundp 'recenter-top-bottom)
6825       (recenter-top-bottom n)
6826     (recenter n)))
6827
6828 (put 'gnus-recenter 'isearch-scroll t)
6829
6830 (defun gnus-forward-line-ignore-invisible (n)
6831   "Move N lines forward (backward if N is negative).
6832 Like forward-line, but skip over (and don't count) invisible lines."
6833   (let (done)
6834     (while (and (> n 0) (not done))
6835       ;; If the following character is currently invisible,
6836       ;; skip all characters with that same `invisible' property value.
6837       (while (gnus-invisible-p (point))
6838         (goto-char (gnus-next-char-property-change (point))))
6839       (forward-line 1)
6840       (if (eobp)
6841           (setq done t)
6842         (setq n (1- n))))
6843     (while (and (< n 0) (not done))
6844       (forward-line -1)
6845       (if (bobp) (setq done t)
6846         (setq n (1+ n))
6847         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6848           (goto-char (gnus-previous-char-property-change (point))))))))
6849
6850 (defun gnus-summary-recenter ()
6851   "Center point in the summary window.
6852 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6853 displayed, no centering will be performed."
6854   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6855   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6856   (interactive)
6857   ;; The user has to want it.
6858   (when gnus-auto-center-summary
6859     (let* ((top (cond ((< (window-height) 4) 0)
6860                       ((< (window-height) 7) 1)
6861                       (t (if (numberp gnus-auto-center-summary)
6862                              gnus-auto-center-summary
6863                            (/ (1- (window-height)) 2)))))
6864            (height (1- (window-height)))
6865            (bottom (save-excursion
6866                      (goto-char (point-max))
6867                      (gnus-forward-line-ignore-invisible (- height))
6868                      (point)))
6869            (window (get-buffer-window (current-buffer))))
6870       (when (get-buffer-window gnus-article-buffer)
6871         ;; Only do recentering when the article buffer is displayed,
6872         ;; Set the window start to either `bottom', which is the biggest
6873         ;; possible valid number, or the second line from the top,
6874         ;; whichever is the least.
6875         (let ((top-pos (save-excursion
6876                          (gnus-forward-line-ignore-invisible (- top))
6877                          (point))))
6878           (if (> bottom top-pos)
6879               ;; Keep the second line from the top visible
6880               (set-window-start window top-pos)
6881             ;; Try to keep the bottom line visible; if it's partially
6882             ;; obscured, either scroll one more line to make it fully
6883             ;; visible, or revert to using TOP-POS.
6884             (save-excursion
6885               (goto-char (point-max))
6886               (gnus-forward-line-ignore-invisible -1)
6887               (let ((last-line-start (point)))
6888                 (goto-char bottom)
6889                 (set-window-start window (point) t)
6890                 (when (not (pos-visible-in-window-p last-line-start window))
6891                   (gnus-forward-line-ignore-invisible 1)
6892                   (set-window-start window (min (point) top-pos) t)))))))
6893       ;; Do horizontal recentering while we're at it.
6894       (when (and (get-buffer-window (current-buffer) t)
6895                  (not (eq gnus-auto-center-summary 'vertical)))
6896         (let ((selected (selected-window)))
6897           (select-window (get-buffer-window (current-buffer) t))
6898           (gnus-summary-position-point)
6899           (gnus-horizontal-recenter)
6900           (select-window selected))))))
6901
6902 (defun gnus-summary-jump-to-group (newsgroup)
6903   "Move point to NEWSGROUP in group mode buffer."
6904   ;; Keep update point of group mode buffer if visible.
6905   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6906       (save-window-excursion
6907         ;; Take care of tree window mode.
6908         (when (get-buffer-window gnus-group-buffer)
6909           (pop-to-buffer gnus-group-buffer))
6910         (gnus-group-jump-to-group newsgroup))
6911     (save-excursion
6912       ;; Take care of tree window mode.
6913       (if (get-buffer-window gnus-group-buffer 0)
6914           (pop-to-buffer gnus-group-buffer)
6915         (set-buffer gnus-group-buffer))
6916       (gnus-group-jump-to-group newsgroup))))
6917
6918 ;; This function returns a list of article numbers based on the
6919 ;; difference between the ranges of read articles in this group and
6920 ;; the range of active articles.
6921 (defun gnus-list-of-unread-articles (group)
6922   (let* ((read (gnus-info-read (gnus-get-info group)))
6923          (active (or (gnus-active group) (gnus-activate-group group)))
6924          (last (or (cdr active)
6925                    (error "Group %s couldn't be activated " group)))
6926          (bottom (if gnus-newsgroup-maximum-articles
6927                      (max (car active)
6928                           (- last gnus-newsgroup-maximum-articles -1))
6929                    (car active)))
6930          first nlast unread)
6931     ;; If none are read, then all are unread.
6932     (if (not read)
6933         (setq first bottom)
6934       ;; If the range of read articles is a single range, then the
6935       ;; first unread article is the article after the last read
6936       ;; article.  Sounds logical, doesn't it?
6937       (if (and (not (listp (cdr read)))
6938                (or (< (car read) bottom)
6939                    (progn (setq read (list read))
6940                           nil)))
6941           (setq first (max bottom (1+ (cdr read))))
6942         ;; `read' is a list of ranges.
6943         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6944                                   (caar read)))
6945                   1)
6946           (setq first bottom))
6947         (while read
6948           (when first
6949             (while (< first nlast)
6950               (setq unread (cons first unread)
6951                     first (1+ first))))
6952           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6953           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6954           (setq read (cdr read)))))
6955     ;; And add the last unread articles.
6956     (while (<= first last)
6957       (setq unread (cons first unread)
6958             first (1+ first)))
6959     ;; Return the list of unread articles.
6960     (delq 0 (nreverse unread))))
6961
6962 (defun gnus-list-of-read-articles (group)
6963   "Return a list of unread, unticked and non-dormant articles."
6964   (let* ((info (gnus-get-info group))
6965          (marked (gnus-info-marks info))
6966          (active (gnus-active group)))
6967     (and info active
6968          (gnus-list-range-difference
6969           (gnus-list-range-difference
6970            (gnus-sorted-complement
6971             (gnus-uncompress-range
6972              (if gnus-newsgroup-maximum-articles
6973                  (cons (max (car active)
6974                             (- (cdr active)
6975                                gnus-newsgroup-maximum-articles
6976                                -1))
6977                        (cdr active))
6978                active))
6979             (gnus-list-of-unread-articles group))
6980            (cdr (assq 'dormant marked)))
6981           (cdr (assq 'tick marked))))))
6982
6983 ;; This function returns a sequence of article numbers based on the
6984 ;; difference between the ranges of read articles in this group and
6985 ;; the range of active articles.
6986 (defun gnus-sequence-of-unread-articles (group)
6987   (let* ((read (gnus-info-read (gnus-get-info group)))
6988          (active (or (gnus-active group) (gnus-activate-group group)))
6989          (last (cdr active))
6990          (bottom (if gnus-newsgroup-maximum-articles
6991                      (max (car active)
6992                           (- last gnus-newsgroup-maximum-articles -1))
6993                    (car active)))
6994          first nlast unread)
6995     ;; If none are read, then all are unread.
6996     (if (not read)
6997         (setq first bottom)
6998       ;; If the range of read articles is a single range, then the
6999       ;; first unread article is the article after the last read
7000       ;; article.  Sounds logical, doesn't it?
7001       (if (and (not (listp (cdr read)))
7002                (or (< (car read) bottom)
7003                    (progn (setq read (list read))
7004                           nil)))
7005           (setq first (max bottom (1+ (cdr read))))
7006         ;; `read' is a list of ranges.
7007         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7008                                   (caar read)))
7009                   1)
7010           (setq first bottom))
7011         (while read
7012           (when first
7013             (push (cons first nlast) unread))
7014           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7015           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7016           (setq read (cdr read)))))
7017     ;; And add the last unread articles.
7018     (cond ((not (and first last))
7019            nil)
7020           ((< first last)
7021            (push (cons first last) unread))
7022           ((= first last)
7023            (push first unread)))
7024     ;; Return the sequence of unread articles.
7025     (delq 0 (nreverse unread))))
7026
7027 ;; Various summary commands
7028
7029 (defun gnus-summary-select-article-buffer ()
7030   "Reconfigure windows to show the article buffer.
7031 If `gnus-widen-article-window' is set, show only the article
7032 buffer."
7033   (interactive)
7034   (if (not (gnus-buffer-live-p gnus-article-buffer))
7035       (error "There is no article buffer for this summary buffer")
7036     (unless (get-buffer-window gnus-article-buffer)
7037       (gnus-summary-show-article))
7038     (gnus-configure-windows
7039      (if gnus-widen-article-window
7040          'only-article
7041        'article)
7042      t)
7043     (select-window (get-buffer-window gnus-article-buffer))))
7044
7045 (defun gnus-summary-universal-argument (arg)
7046   "Perform any operation on all articles that are process/prefixed."
7047   (interactive "P")
7048   (let ((articles (gnus-summary-work-articles arg))
7049         func article)
7050     (if (eq
7051          (setq
7052           func
7053           (key-binding
7054            (read-key-sequence
7055             (substitute-command-keys
7056              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7057          'undefined)
7058         (gnus-error 1 "Undefined key")
7059       (save-excursion
7060         (while articles
7061           (gnus-summary-goto-subject (setq article (pop articles)))
7062           (let (gnus-newsgroup-processable)
7063             (command-execute func))
7064           (gnus-summary-remove-process-mark article)))))
7065   (gnus-summary-position-point))
7066
7067 (defun gnus-summary-toggle-truncation (&optional arg)
7068   "Toggle truncation of summary lines.
7069 With ARG, turn line truncation on if ARG is positive."
7070   (interactive "P")
7071   (setq truncate-lines
7072         (if (null arg) (not truncate-lines)
7073           (> (prefix-numeric-value arg) 0)))
7074   (redraw-display))
7075
7076 (defun gnus-summary-find-for-reselect ()
7077   "Return the number of an article to stay on across a reselect.
7078 The current article is considered, then following articles, then previous
7079 articles.  An article is sought which is not canceled and isn't a temporary
7080 insertion from another group.  If there's no such then return a dummy 0."
7081   (let (found)
7082     (dolist (rev '(nil t))
7083       (unless found      ; don't demand the reverse list if we don't need it
7084         (let ((data (gnus-data-find-list
7085                      (gnus-summary-article-number) (gnus-data-list rev))))
7086           (while (and data (not found))
7087             (if (and (< 0 (gnus-data-number (car data)))
7088                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7089                 (setq found (gnus-data-number (car data))))
7090             (setq data (cdr data))))))
7091     (or found 0)))
7092
7093 (defun gnus-summary-reselect-current-group (&optional all rescan)
7094   "Exit and then reselect the current newsgroup.
7095 The prefix argument ALL means to select all articles."
7096   (interactive "P")
7097   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7098     (error "Ephemeral groups can't be reselected"))
7099   (let ((current-subject (gnus-summary-find-for-reselect))
7100         (group gnus-newsgroup-name))
7101     (setq gnus-newsgroup-begin nil)
7102     (gnus-summary-exit nil 'leave-hidden)
7103     ;; We have to adjust the point of group mode buffer because
7104     ;; point was moved to the next unread newsgroup by exiting.
7105     (gnus-summary-jump-to-group group)
7106     (when rescan
7107       (save-excursion
7108         (gnus-group-get-new-news-this-group 1)))
7109     (gnus-group-read-group all t)
7110     (gnus-summary-goto-subject current-subject nil t)))
7111
7112 (defun gnus-summary-rescan-group (&optional all)
7113   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7114   (interactive "P")
7115   (let ((config gnus-current-window-configuration))
7116     (gnus-summary-reselect-current-group all t)
7117     (gnus-configure-windows config)
7118     (when (eq config 'article)
7119       (gnus-summary-select-article))))
7120
7121 (defun gnus-summary-update-info (&optional non-destructive)
7122   (save-excursion
7123     (let ((group gnus-newsgroup-name))
7124       (when group
7125         (when gnus-newsgroup-kill-headers
7126           (setq gnus-newsgroup-killed
7127                 (gnus-compress-sequence
7128                  (gnus-sorted-union
7129                   (gnus-list-range-intersection
7130                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7131                   gnus-newsgroup-unreads)
7132                  t)))
7133         (unless (listp (cdr gnus-newsgroup-killed))
7134           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7135         (let ((headers gnus-newsgroup-headers)
7136               (ephemeral-p (gnus-ephemeral-group-p group))
7137               info)
7138           (unless ephemeral-p
7139             (setq info (copy-sequence (gnus-get-info group))
7140                   info (delq (gnus-info-params info) info)))
7141           ;; Set the new ranges of read articles.
7142           (with-current-buffer gnus-group-buffer
7143             (gnus-undo-force-boundary))
7144           (gnus-update-read-articles
7145            group (gnus-sorted-union
7146                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7147           ;; Set the current article marks.
7148           (let ((gnus-newsgroup-scored
7149                  (if (and (not gnus-save-score)
7150                           (not non-destructive))
7151                      nil
7152                    gnus-newsgroup-scored)))
7153             (save-excursion
7154               (gnus-update-marks)))
7155           ;; Do the cross-ref thing.
7156           (when gnus-use-cross-reference
7157             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7158           ;; Do not switch windows but change the buffer to work.
7159           (set-buffer gnus-group-buffer)
7160           (unless ephemeral-p
7161             (gnus-group-update-group
7162              group nil
7163              (equal info
7164                     (setq info (copy-sequence (gnus-get-info group))
7165                           info (delq (gnus-info-params info) info))))))))))
7166
7167 (defun gnus-summary-save-newsrc (&optional force)
7168   "Save the current number of read/marked articles in the dribble buffer.
7169 The dribble buffer will then be saved.
7170 If FORCE (the prefix), also save the .newsrc file(s)."
7171   (interactive "P")
7172   (gnus-summary-update-info t)
7173   (if force
7174       (gnus-save-newsrc-file)
7175     (gnus-dribble-save)))
7176
7177 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7178
7179 (defun gnus-summary-exit (&optional temporary leave-hidden)
7180   "Exit reading current newsgroup, and then return to group selection mode.
7181 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7182   (interactive)
7183   (gnus-set-global-variables)
7184   (when (gnus-buffer-live-p gnus-article-buffer)
7185     (with-current-buffer gnus-article-buffer
7186       (mm-destroy-parts gnus-article-mime-handles)
7187       ;; Set it to nil for safety reason.
7188       (setq gnus-article-mime-handle-alist nil)
7189       (setq gnus-article-mime-handles nil)))
7190   (gnus-kill-save-kill-buffer)
7191   (gnus-async-halt-prefetch)
7192   (let* ((group gnus-newsgroup-name)
7193          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7194          (gnus-group-is-exiting-p t)
7195          (article-buffer gnus-article-buffer)
7196          (original-article-buffer gnus-original-article-buffer)
7197          (mode major-mode)
7198          (group-point nil)
7199          (buf (current-buffer))
7200          ;; `gnus-single-article-buffer' is nil buffer-locally in
7201          ;; ephemeral group of which summary buffer will be killed,
7202          ;; but the global value may be non-nil.
7203          (single-article-buffer gnus-single-article-buffer))
7204     (unless quit-config
7205       ;; Do adaptive scoring, and possibly save score files.
7206       (when gnus-newsgroup-adaptive
7207         (gnus-score-adaptive))
7208       (when gnus-use-scoring
7209         (gnus-score-save)))
7210     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7211     (when gnus-use-cache
7212       (gnus-cache-possibly-remove-articles)
7213       (gnus-cache-save-buffers))
7214     (gnus-async-prefetch-remove-group group)
7215     (when gnus-suppress-duplicates
7216       (gnus-dup-enter-articles))
7217     (when gnus-use-trees
7218       (gnus-tree-close group))
7219     (when gnus-use-cache
7220       (gnus-cache-write-active))
7221     ;; Remove entries for this group.
7222     (nnmail-purge-split-history (gnus-group-real-name group))
7223     ;; Make all changes in this group permanent.
7224     (unless quit-config
7225       (gnus-run-hooks 'gnus-exit-group-hook)
7226       (gnus-summary-update-info))
7227     (gnus-close-group group)
7228     ;; Make sure where we were, and go to next newsgroup.
7229     (when (buffer-live-p (get-buffer gnus-group-buffer))
7230       (set-buffer gnus-group-buffer))
7231     (unless quit-config
7232       (gnus-group-jump-to-group group))
7233     (gnus-run-hooks 'gnus-summary-exit-hook)
7234     (unless (or quit-config
7235                 (not gnus-summary-next-group-on-exit)
7236                 ;; If this group has disappeared from the summary
7237                 ;; buffer, don't skip forwards.
7238                 (not (string= group (gnus-group-group-name))))
7239       (gnus-group-next-unread-group 1))
7240     (setq group-point (point))
7241     (if temporary
7242         nil                             ;Nothing to do.
7243       (set-buffer buf)
7244       (if (not gnus-kill-summary-on-exit)
7245           (progn
7246             (gnus-deaden-summary)
7247             (setq mode nil))
7248         (when (get-buffer gnus-article-buffer)
7249           (bury-buffer gnus-article-buffer))
7250         ;; Return to group mode buffer.
7251         (when (eq mode 'gnus-summary-mode)
7252           (gnus-kill-buffer buf)))
7253
7254       (setq gnus-current-select-method gnus-select-method)
7255       (when (gnus-buffer-live-p gnus-group-buffer)
7256         (set-buffer gnus-group-buffer))
7257       (if quit-config
7258           (gnus-handle-ephemeral-exit quit-config)
7259         (goto-char group-point)
7260         ;; If gnus-group-buffer is already displayed, make sure we also move
7261         ;; the cursor in the window that displays it.
7262         (let ((win (get-buffer-window (current-buffer) 0)))
7263           (if win (set-window-point win (point))))
7264         (unless leave-hidden
7265           (gnus-configure-windows 'group 'force)))
7266
7267       ;; If we have several article buffers, we kill them at exit.
7268       (unless single-article-buffer
7269         (when (gnus-buffer-live-p article-buffer)
7270           (with-current-buffer article-buffer
7271             ;; Don't kill sticky article buffers
7272             (unless (eq major-mode 'gnus-sticky-article-mode)
7273               (gnus-kill-buffer article-buffer)
7274               (setq gnus-article-current nil))))
7275         (gnus-kill-buffer original-article-buffer))
7276
7277       ;; Clear the current group name.
7278       (unless quit-config
7279         (setq gnus-newsgroup-name nil)))))
7280
7281 (declare-function gnus-article-stop-animations "gnus-art" ())
7282 (declare-function gnus-stop-downloads "gnus-art" ())
7283
7284 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7285 (defun gnus-summary-exit-no-update (&optional no-questions)
7286   "Quit reading current newsgroup without updating read article info."
7287   (interactive)
7288   (let* ((group gnus-newsgroup-name)
7289          (gnus-group-is-exiting-p t)
7290          (gnus-group-is-exiting-without-update-p t)
7291          (quit-config (gnus-group-quit-config group)))
7292     (when (or no-questions
7293               gnus-expert-user
7294               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7295       (gnus-async-halt-prefetch)
7296       (run-hooks 'gnus-summary-prepare-exit-hook)
7297       (when (gnus-buffer-live-p gnus-article-buffer)
7298         (with-current-buffer gnus-article-buffer
7299           (gnus-article-stop-animations)
7300           (gnus-stop-downloads)
7301           (mm-destroy-parts gnus-article-mime-handles)
7302           ;; Set it to nil for safety reason.
7303           (setq gnus-article-mime-handle-alist nil)
7304           (setq gnus-article-mime-handles nil)))
7305       ;; If we have several article buffers, we kill them at exit.
7306       (unless gnus-single-article-buffer
7307         (gnus-kill-buffer gnus-article-buffer)
7308         (gnus-kill-buffer gnus-original-article-buffer)
7309         (setq gnus-article-current nil))
7310       ;; Return to the group buffer.
7311       (if (not gnus-kill-summary-on-exit)
7312           (progn
7313             (gnus-deaden-summary)
7314             (gnus-configure-windows 'group 'force))
7315         (gnus-configure-windows 'group 'force)
7316         (gnus-close-group group)
7317         (gnus-kill-buffer gnus-summary-buffer))
7318       (unless gnus-single-article-buffer
7319         (setq gnus-article-current nil))
7320       (when gnus-use-trees
7321         (gnus-tree-close group))
7322       (gnus-async-prefetch-remove-group group)
7323       (when (get-buffer gnus-article-buffer)
7324         (bury-buffer gnus-article-buffer))
7325       ;; Clear the current group name.
7326       (setq gnus-newsgroup-name nil)
7327       (unless (gnus-ephemeral-group-p group)
7328         (gnus-group-update-group group nil t))
7329       (when (equal (gnus-group-group-name) group)
7330         (gnus-group-next-unread-group 1))
7331       (when quit-config
7332         (gnus-handle-ephemeral-exit quit-config)))))
7333
7334 (defun gnus-handle-ephemeral-exit (quit-config)
7335   "Handle movement when leaving an ephemeral group.
7336 The state which existed when entering the ephemeral is reset."
7337   (if (not (buffer-live-p (car quit-config)))
7338       (when (gnus-buffer-live-p gnus-group-buffer)
7339         (gnus-configure-windows 'group 'force))
7340     (set-buffer (car quit-config))
7341     (unless (eq (cdr quit-config) 'group)
7342       (setq gnus-current-select-method
7343             (gnus-find-method-for-group gnus-newsgroup-name)))
7344     (cond ((eq major-mode 'gnus-summary-mode)
7345            (gnus-set-global-variables))
7346           ((eq major-mode 'gnus-article-mode)
7347            (save-current-buffer
7348              ;; The `gnus-summary-buffer' variable may point
7349              ;; to the old summary buffer when using a single
7350              ;; article buffer.
7351              (unless (gnus-buffer-live-p gnus-summary-buffer)
7352                (set-buffer gnus-group-buffer))
7353              (set-buffer gnus-summary-buffer)
7354              (gnus-set-global-variables))))
7355     (if (or (eq (cdr quit-config) 'article)
7356             (eq (cdr quit-config) 'pick))
7357         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7358             (gnus-configure-windows 'pick 'force)
7359           (gnus-configure-windows (cdr quit-config) 'force))
7360       (gnus-configure-windows (cdr quit-config) 'force))
7361     (when (eq major-mode 'gnus-summary-mode)
7362       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7363                                                      next-unread-noselect))
7364           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7365                                   'next-noselect)
7366                               (gnus-summary-next-subject 1 nil t))
7367                              ((eq gnus-auto-select-on-ephemeral-exit
7368                                   'next-unread-noselect)
7369                               (gnus-summary-next-subject 1 t t))))
7370             ;; Hide the article buffer which displays the article different
7371             ;; from the one that the cursor points to in the summary buffer.
7372             (gnus-configure-windows 'summary 'force))
7373         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7374                (gnus-summary-next-subject 1))
7375               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7376                (gnus-summary-next-subject 1 t))))
7377       (gnus-summary-recenter)
7378       (gnus-summary-position-point))))
7379
7380 ;;; Dead summaries.
7381
7382 (defvar gnus-dead-summary-mode-map
7383   (let ((map (make-keymap)))
7384     (suppress-keymap map)
7385     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7386     (dolist (key '("\C-d" "\r" "\177" [delete]))
7387       (define-key map key 'gnus-summary-wake-up-the-dead))
7388     (dolist (key '("q" "Q"))
7389       (define-key map key 'bury-buffer))
7390     map))
7391
7392 (define-minor-mode gnus-dead-summary-mode
7393   "Minor mode for Gnus summary buffers."
7394   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7395   (unless (derived-mode-p 'gnus-summary-mode)
7396     (setq gnus-dead-summary-mode nil)))
7397
7398 (defun gnus-deaden-summary ()
7399   "Make the current summary buffer into a dead summary buffer."
7400   ;; Kill any previous dead summary buffer.
7401   (when (and gnus-dead-summary
7402              (buffer-name gnus-dead-summary))
7403     (with-current-buffer gnus-dead-summary
7404       (when gnus-dead-summary-mode
7405         (kill-buffer (current-buffer)))))
7406   ;; Make this the current dead summary.
7407   (setq gnus-dead-summary (current-buffer))
7408   (gnus-dead-summary-mode 1)
7409   (let ((name (buffer-name)))
7410     (when (string-match "Summary" name)
7411       (rename-buffer
7412        (concat (substring name 0 (match-beginning 0)) "Dead "
7413                (substring name (match-beginning 0)))
7414        t)
7415       (bury-buffer))))
7416
7417 (defun gnus-kill-or-deaden-summary (buffer)
7418   "Kill or deaden the summary BUFFER."
7419   (save-excursion
7420     (when (and (buffer-name buffer)
7421                (not gnus-single-article-buffer))
7422       (with-current-buffer buffer
7423         (gnus-kill-buffer gnus-article-buffer)
7424         (gnus-kill-buffer gnus-original-article-buffer)))
7425     (cond
7426      ;; Kill the buffer.
7427      (gnus-kill-summary-on-exit
7428       (when (and gnus-use-trees
7429                  (gnus-buffer-exists-p buffer))
7430         (with-current-buffer buffer
7431           (gnus-tree-close gnus-newsgroup-name)))
7432       (gnus-kill-buffer buffer))
7433      ;; Deaden the buffer.
7434      ((gnus-buffer-exists-p buffer)
7435       (with-current-buffer buffer
7436         (gnus-deaden-summary))))))
7437
7438 (defun gnus-summary-wake-up-the-dead (&rest args)
7439   "Wake up the dead summary buffer."
7440   (interactive)
7441   (gnus-dead-summary-mode -1)
7442   (let ((name (buffer-name)))
7443     (when (string-match "Dead " name)
7444       (rename-buffer
7445        (concat (substring name 0 (match-beginning 0))
7446                (substring name (match-end 0)))
7447        t)))
7448   (gnus-message 3 "This dead summary is now alive again"))
7449
7450 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7451 (defun gnus-summary-describe-group (&optional force)
7452   "Describe the current newsgroup."
7453   (interactive "P")
7454   (gnus-group-describe-group force gnus-newsgroup-name))
7455
7456 (defun gnus-summary-describe-briefly ()
7457   "Describe summary mode commands briefly."
7458   (interactive)
7459   (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")))
7460
7461 ;; Walking around group mode buffer from summary mode.
7462
7463 (defun gnus-summary-next-group (&optional no-article target-group backward)
7464   "Exit current newsgroup and then select next unread newsgroup.
7465 If prefix argument NO-ARTICLE is non-nil, no article is selected
7466 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7467 previous group instead."
7468   (interactive "P")
7469   ;; Stop pre-fetching.
7470   (gnus-async-halt-prefetch)
7471   (let ((current-group gnus-newsgroup-name)
7472         (current-buffer (current-buffer))
7473         entered)
7474     ;; First we semi-exit this group to update Xrefs and all variables.
7475     ;; We can't do a real exit, because the window conf must remain
7476     ;; the same in case the user is prompted for info, and we don't
7477     ;; want the window conf to change before that...
7478     (gnus-summary-exit t)
7479     (while (not entered)
7480       ;; Then we find what group we are supposed to enter.
7481       (set-buffer gnus-group-buffer)
7482       (gnus-group-jump-to-group current-group)
7483       (setq target-group
7484             (or target-group
7485                 (if (eq gnus-keep-same-level 'best)
7486                     (gnus-summary-best-group gnus-newsgroup-name)
7487                   (gnus-summary-search-group backward gnus-keep-same-level))))
7488       (if (not target-group)
7489           ;; There are no further groups, so we return to the group
7490           ;; buffer.
7491           (progn
7492             (gnus-message 5 "Returning to the group buffer")
7493             (setq entered t)
7494             (when (gnus-buffer-live-p current-buffer)
7495               (set-buffer current-buffer)
7496               (gnus-summary-exit))
7497             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7498         ;; We try to enter the target group.
7499         (gnus-group-jump-to-group target-group)
7500         (let ((unreads (gnus-group-group-unread)))
7501           (if (and (or (eq t unreads)
7502                        (and unreads (not (zerop unreads))))
7503                    (gnus-summary-read-group
7504                     target-group nil no-article
7505                     (and (buffer-name current-buffer) current-buffer)
7506                     nil backward))
7507               (setq entered t)
7508             (setq current-group target-group
7509                   target-group nil)))))))
7510
7511 (defun gnus-summary-prev-group (&optional no-article)
7512   "Exit current newsgroup and then select previous unread newsgroup.
7513 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7514   (interactive "P")
7515   (gnus-summary-next-group no-article nil t))
7516
7517 ;; Walking around summary lines.
7518
7519 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7520   "Go to the first subject satisfying any non-nil constraint.
7521 If UNREAD is non-nil, the article should be unread.
7522 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7523 If UNSEEN is non-nil, the article should be unseen as well as unread.
7524 Returns the article selected or nil if there are no matching articles."
7525   (interactive "P")
7526   (cond
7527    ;; Empty summary.
7528    ((null gnus-newsgroup-data)
7529     (gnus-message 3 "No articles in the group")
7530     nil)
7531    ;; Pick the first article.
7532    ((not (or unread undownloaded unseen))
7533     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7534     (gnus-data-number (car gnus-newsgroup-data)))
7535    ;; Find the first unread article.
7536    (t
7537     (let ((data gnus-newsgroup-data))
7538       (while (and data
7539                   (let ((num (gnus-data-number (car data))))
7540                     (or (memq num gnus-newsgroup-unfetched)
7541                         (not (or (and unread
7542                                       (memq num gnus-newsgroup-unreads))
7543                                  (and undownloaded
7544                                       (memq num gnus-newsgroup-undownloaded))
7545                                  (and unseen
7546                                       (memq num gnus-newsgroup-unseen)
7547                                       (memq num gnus-newsgroup-unreads)))))))
7548         (setq data (cdr data)))
7549       (prog1
7550           (if data
7551               (progn
7552                 (goto-char (gnus-data-pos (car data)))
7553                 (gnus-data-number (car data)))
7554             (gnus-message 3 "No more%s articles"
7555                           (let* ((r (when unread " unread"))
7556                                  (d (when undownloaded " undownloaded"))
7557                                  (s (when unseen " unseen"))
7558                                  (l (delq nil (list r d s))))
7559                             (cond ((= 3 (length l))
7560                                    (concat r "," d ", or" s))
7561                                   ((= 2 (length l))
7562                                    (concat (car l) ", or" (cadr l)))
7563                                   ((= 1 (length l))
7564                                    (car l))
7565                                   (t
7566                                    ""))))
7567             nil
7568             )
7569         (gnus-summary-position-point))))))
7570
7571 (defun gnus-summary-next-subject (n &optional unread dont-display)
7572   "Go to next N'th summary line.
7573 If N is negative, go to the previous N'th subject line.
7574 If UNREAD is non-nil, only unread articles are selected.
7575 The difference between N and the actual number of steps taken is
7576 returned."
7577   (interactive "p")
7578   (let ((backward (< n 0))
7579         (n (abs n)))
7580     (while (and (> n 0)
7581                 (if backward
7582                     (gnus-summary-find-prev unread)
7583                   (gnus-summary-find-next unread)))
7584       (unless (zerop (setq n (1- n)))
7585         (gnus-summary-show-thread)))
7586     (when (/= 0 n)
7587       (gnus-message 7 "No more%s articles"
7588                     (if unread " unread" "")))
7589     (unless dont-display
7590       (gnus-summary-recenter)
7591       (gnus-summary-position-point))
7592     n))
7593
7594 (defun gnus-summary-next-unread-subject (n)
7595   "Go to next N'th unread summary line."
7596   (interactive "p")
7597   (gnus-summary-next-subject n t))
7598
7599 (defun gnus-summary-prev-subject (n &optional unread)
7600   "Go to previous N'th summary line.
7601 If optional argument UNREAD is non-nil, only unread article is selected."
7602   (interactive "p")
7603   (gnus-summary-next-subject (- n) unread))
7604
7605 (defun gnus-summary-prev-unread-subject (n)
7606   "Go to previous N'th unread summary line."
7607   (interactive "p")
7608   (gnus-summary-next-subject (- n) t))
7609
7610 (defun gnus-summary-goto-subjects (articles)
7611   "Insert the subject header for ARTICLES in the current buffer."
7612   (save-excursion
7613     (dolist (article articles)
7614       (gnus-summary-goto-subject article t)))
7615   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7616   (gnus-summary-position-point))
7617
7618 (defun gnus-summary-goto-subject (article &optional force silent)
7619   "Go to the subject line of ARTICLE.
7620 If FORCE, also allow jumping to articles not currently shown."
7621   (interactive "nArticle number: ")
7622   (unless (numberp article)
7623     (error "Article %s is not a number" article))
7624   (let ((b (point))
7625         (data (gnus-data-find article)))
7626     ;; We read in the article if we have to.
7627     (and (not data)
7628          force
7629          (gnus-summary-insert-subject
7630           article
7631           (if (or (numberp force) (vectorp force)) force)
7632           t)
7633          (setq data (gnus-data-find article)))
7634     (goto-char b)
7635     (if (not data)
7636         (progn
7637           (unless silent
7638             (gnus-message 3 "Can't find article %d" article))
7639           nil)
7640       (let ((pt (gnus-data-pos data)))
7641         (goto-char pt)
7642         (gnus-summary-set-article-display-arrow pt))
7643       (gnus-summary-position-point)
7644       article)))
7645
7646 ;; Walking around summary lines with displaying articles.
7647
7648 (defun gnus-summary-expand-window (&optional arg)
7649   "Make the summary buffer take up the entire Emacs frame.
7650 Given a prefix, will force an `article' buffer configuration."
7651   (interactive "P")
7652   (if arg
7653       (gnus-configure-windows 'article 'force)
7654     (gnus-configure-windows 'summary 'force)))
7655
7656 (defun gnus-summary-display-article (article &optional all-header)
7657   "Display ARTICLE in article buffer."
7658   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7659                (with-current-buffer gnus-article-buffer
7660                  (eq major-mode 'gnus-article-mode)))
7661     (gnus-article-setup-buffer))
7662   (gnus-set-global-variables)
7663   (with-current-buffer gnus-article-buffer
7664     (setq gnus-article-charset gnus-newsgroup-charset)
7665     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7666     (mm-enable-multibyte))
7667   (if (null article)
7668       nil
7669     (prog1
7670         (if gnus-summary-display-article-function
7671             (funcall gnus-summary-display-article-function article all-header)
7672           (gnus-article-prepare article all-header))
7673       (gnus-run-hooks 'gnus-select-article-hook)
7674       (when (and gnus-current-article
7675                  (not (zerop gnus-current-article)))
7676         (gnus-summary-goto-subject gnus-current-article))
7677       (gnus-summary-recenter)
7678       (when (and gnus-use-trees gnus-show-threads)
7679         (gnus-possibly-generate-tree article)
7680         (gnus-highlight-selected-tree article))
7681       ;; Successfully display article.
7682       (gnus-article-set-window-start
7683        (cdr (assq article gnus-newsgroup-bookmarks))))))
7684
7685 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7686   "Select the current article.
7687 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7688 non-nil, the article will be re-fetched even if it already present in
7689 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7690 be displayed."
7691   ;; Make sure we are in the summary buffer to work around bbdb bug.
7692   (unless (eq major-mode 'gnus-summary-mode)
7693     (set-buffer gnus-summary-buffer))
7694   (let ((article (or article (gnus-summary-article-number)))
7695         (all-headers (not (not all-headers))) ;Must be t or nil.
7696         gnus-summary-display-article-function)
7697     (and (not pseudo)
7698          (gnus-summary-article-pseudo-p article)
7699          (error "This is a pseudo-article"))
7700     (with-current-buffer gnus-summary-buffer
7701       (if (or (and gnus-single-article-buffer
7702                    (or (null gnus-current-article)
7703                        (null gnus-article-current)
7704                        (null (get-buffer gnus-article-buffer))
7705                        (not (eq article (cdr gnus-article-current)))
7706                        (not (equal (car gnus-article-current)
7707                                    gnus-newsgroup-name))
7708                        (not (get-buffer gnus-original-article-buffer))))
7709               (and (not gnus-single-article-buffer)
7710                    (or (null gnus-current-article)
7711                        (not (get-buffer gnus-original-article-buffer))
7712                        (not (eq gnus-current-article article))))
7713               force)
7714           ;; The requested article is different from the current article.
7715           (progn
7716             (gnus-summary-display-article article all-headers)
7717             (when (gnus-buffer-live-p gnus-article-buffer)
7718               (with-current-buffer gnus-article-buffer
7719                 (if (not gnus-article-decoded-p) ;; a local variable
7720                     (mm-disable-multibyte))))
7721             (gnus-article-set-window-start
7722              (cdr (assq article gnus-newsgroup-bookmarks)))
7723             article)
7724         'old))))
7725
7726 (defun gnus-summary-force-verify-and-decrypt ()
7727   "Display buttons for signed/encrypted parts and verify/decrypt them."
7728   (interactive)
7729   (let ((mm-verify-option 'known)
7730         (mm-decrypt-option 'known)
7731         (gnus-article-emulate-mime t)
7732         (gnus-buttonized-mime-types (append (list "multipart/signed"
7733                                                   "multipart/encrypted")
7734                                             gnus-buttonized-mime-types)))
7735     (gnus-summary-select-article nil 'force)))
7736
7737 (defun gnus-summary-next-article (&optional unread subject backward push)
7738   "Select the next article.
7739 If UNREAD, only unread articles are selected.
7740 If SUBJECT, only articles with SUBJECT are selected.
7741 If BACKWARD, the previous article is selected instead of the next."
7742   (interactive "P")
7743   ;; Make sure we are in the summary buffer.
7744   (unless (eq major-mode 'gnus-summary-mode)
7745     (set-buffer gnus-summary-buffer))
7746   (cond
7747    ;; Is there such an article?
7748    ((and (gnus-summary-search-forward unread subject backward)
7749          (or (gnus-summary-display-article (gnus-summary-article-number))
7750              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7751     (gnus-summary-position-point))
7752    ;; If not, we try the first unread, if that is wanted.
7753    ((and subject
7754          gnus-auto-select-same
7755          (gnus-summary-first-unread-article))
7756     (gnus-summary-position-point)
7757     (gnus-message 6 "Wrapped"))
7758    ;; Try to get next/previous article not displayed in this group.
7759    ((and gnus-auto-extend-newsgroup
7760          (not unread) (not subject))
7761     (gnus-summary-goto-article
7762      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7763      nil (count-lines (point-min) (point))))
7764    ;; Go to next/previous group.
7765    (t
7766     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7767       (gnus-summary-jump-to-group gnus-newsgroup-name))
7768     (let ((cmd (if (featurep 'xemacs)
7769                    last-command-char
7770                  last-command-event))
7771           (point
7772            (with-current-buffer gnus-group-buffer
7773              (point)))
7774           (current-summary (current-buffer))
7775           (group
7776            (if (eq gnus-keep-same-level 'best)
7777                (gnus-summary-best-group gnus-newsgroup-name)
7778              (gnus-summary-search-group backward gnus-keep-same-level))))
7779       ;; Select next unread newsgroup automagically.
7780       (cond
7781        ((or (not gnus-auto-select-next)
7782             (not cmd))
7783         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7784        ((or (eq gnus-auto-select-next 'quietly)
7785             (and (eq gnus-auto-select-next 'slightly-quietly)
7786                  push)
7787             (and (eq gnus-auto-select-next 'almost-quietly)
7788                  (gnus-summary-last-article-p)))
7789         ;; Select quietly.
7790         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7791             (gnus-summary-exit)
7792           (gnus-message 7 "No more%s articles (%s)..."
7793                         (if unread " unread" "")
7794                         (if group (concat "selecting " group)
7795                           "exiting"))
7796           (gnus-summary-next-group nil group backward)))
7797        (t
7798         (when (gnus-key-press-event-p last-input-event)
7799           ;; Somehow or other, we may now have selected a different
7800           ;; window.  Make point go back to the summary buffer.
7801           (when (eq current-summary (current-buffer))
7802             ;; FIXME: This burps when get-buffer-window returns nil.
7803             (select-window (get-buffer-window current-summary 0)))
7804           (gnus-summary-walk-group-buffer
7805            gnus-newsgroup-name cmd unread backward point))))))))
7806
7807 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7808   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7809                       (?\C-p (gnus-group-prev-unread-group 1))))
7810         (cursor-in-echo-area t)
7811         keve key group ended prompt)
7812     (with-current-buffer gnus-group-buffer
7813       (goto-char start)
7814       (setq group
7815             (if (eq gnus-keep-same-level 'best)
7816                 (gnus-summary-best-group gnus-newsgroup-name)
7817               (gnus-summary-search-group backward gnus-keep-same-level))))
7818     (while (not ended)
7819       (setq prompt
7820             (format
7821              "No more%s articles%s " (if unread " unread" "")
7822              (if (and group
7823                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7824                  (format " (Type %s for %s [%s])"
7825                          (single-key-description cmd)
7826                          (gnus-group-decoded-name group)
7827                          (gnus-group-unread group))
7828                (format " (Type %s to exit %s)"
7829                        (single-key-description cmd)
7830                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7831       ;; Confirm auto selection.
7832       (setq key (car (setq keve (gnus-read-event-char prompt)))
7833             ended t)
7834       (cond
7835        ((assq key keystrokes)
7836         (let ((obuf (current-buffer)))
7837           (switch-to-buffer gnus-group-buffer)
7838           (when group
7839             (gnus-group-jump-to-group group))
7840           (eval (cadr (assq key keystrokes)))
7841           (setq group (gnus-group-group-name))
7842           (switch-to-buffer obuf))
7843         (setq ended nil))
7844        ((equal key cmd)
7845         (if (or (not group)
7846                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7847             (gnus-summary-exit)
7848           (gnus-summary-next-group nil group backward)))
7849        (t
7850         (push (cdr keve) unread-command-events))))))
7851
7852 (defun gnus-summary-next-unread-article ()
7853   "Select unread article after current one."
7854   (interactive)
7855   (gnus-summary-next-article
7856    (or (not (eq gnus-summary-goto-unread 'never))
7857        (gnus-summary-last-article-p (gnus-summary-article-number)))
7858    (and gnus-auto-select-same
7859         (gnus-summary-article-subject))))
7860
7861 (defun gnus-summary-prev-article (&optional unread subject)
7862   "Select the article before the current one.
7863 If UNREAD is non-nil, only unread articles are selected."
7864   (interactive "P")
7865   (gnus-summary-next-article unread subject t))
7866
7867 (defun gnus-summary-prev-unread-article ()
7868   "Select unread article before current one."
7869   (interactive)
7870   (gnus-summary-prev-article
7871    (or (not (eq gnus-summary-goto-unread 'never))
7872        (gnus-summary-first-article-p (gnus-summary-article-number)))
7873    (and gnus-auto-select-same
7874         (gnus-summary-article-subject))))
7875
7876 (declare-function gnus-article-only-boring-p "gnus-art" ())
7877
7878 (defun gnus-summary-next-page (&optional lines circular stop)
7879   "Show next page of the selected article.
7880 If at the end of the current article, select the next article.
7881 LINES says how many lines should be scrolled up.
7882
7883 If CIRCULAR is non-nil, go to the start of the article instead of
7884 selecting the next article when reaching the end of the current
7885 article.
7886
7887 If STOP is non-nil, just stop when reaching the end of the message.
7888
7889 Also see the variable `gnus-article-skip-boring'."
7890   (interactive "P")
7891   (gnus-set-global-variables)
7892   (let ((article (gnus-summary-article-number))
7893         (article-window (get-buffer-window gnus-article-buffer t))
7894         endp)
7895     ;; If the buffer is empty, we have no article.
7896     (unless article
7897       (error "No article to select"))
7898     (gnus-configure-windows 'article)
7899     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7900         (if (and (eq gnus-summary-goto-unread 'never)
7901                  (not (gnus-summary-last-article-p article)))
7902             (gnus-summary-next-article)
7903           (gnus-summary-next-unread-article))
7904       (if (or (null gnus-current-article)
7905               (null gnus-article-current)
7906               (/= article (cdr gnus-article-current))
7907               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7908           ;; Selected subject is different from current article's.
7909           (gnus-summary-display-article article)
7910         (when article-window
7911           (gnus-eval-in-buffer-window gnus-article-buffer
7912             (setq endp (or (gnus-article-next-page lines)
7913                            (gnus-article-only-boring-p))))
7914           (when endp
7915             (cond ((or stop gnus-summary-stop-at-end-of-message)
7916                    (gnus-message 3 "End of message"))
7917                   (circular
7918                    (gnus-summary-beginning-of-article))
7919                   (lines
7920                    (gnus-message 3 "End of message"))
7921                   ((null lines)
7922                    (if (and (eq gnus-summary-goto-unread 'never)
7923                             (not (gnus-summary-last-article-p article)))
7924                        (gnus-summary-next-article)
7925                      (gnus-summary-next-unread-article))))))))
7926     (gnus-summary-recenter)
7927     (gnus-summary-position-point)))
7928
7929 (defun gnus-summary-prev-page (&optional lines move)
7930   "Show previous page of selected article.
7931 Argument LINES specifies lines to be scrolled down.
7932 If MOVE, move to the previous unread article if point is at
7933 the beginning of the buffer."
7934   (interactive "P")
7935   (let ((article (gnus-summary-article-number))
7936         (article-window (get-buffer-window gnus-article-buffer t))
7937         endp)
7938     (gnus-configure-windows 'article)
7939     (if (or (null gnus-current-article)
7940             (null gnus-article-current)
7941             (/= article (cdr gnus-article-current))
7942             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7943         ;; Selected subject is different from current article's.
7944         (gnus-summary-display-article article)
7945       (gnus-summary-recenter)
7946       (when article-window
7947         (gnus-eval-in-buffer-window gnus-article-buffer
7948           (setq endp (gnus-article-prev-page lines)))
7949         (when (and move endp)
7950           (cond (lines
7951                  (gnus-message 3 "Beginning of message"))
7952                 ((null lines)
7953                  (if (and (eq gnus-summary-goto-unread 'never)
7954                           (not (gnus-summary-first-article-p article)))
7955                      (gnus-summary-prev-article)
7956                    (gnus-summary-prev-unread-article))))))))
7957   (gnus-summary-position-point))
7958
7959 (defun gnus-summary-prev-page-or-article (&optional lines)
7960   "Show previous page of selected article.
7961 Argument LINES specifies lines to be scrolled down.
7962 If at the beginning of the article, go to the next article."
7963   (interactive "P")
7964   (gnus-summary-prev-page lines t))
7965
7966 (defun gnus-summary-scroll-up (lines)
7967   "Scroll up (or down) one line current article.
7968 Argument LINES specifies lines to be scrolled up (or down if negative).
7969 If no article is selected, then the current article will be selected first."
7970   (interactive "p")
7971   (gnus-configure-windows 'article)
7972   (gnus-summary-show-thread)
7973   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7974     (gnus-eval-in-buffer-window gnus-article-buffer
7975       (cond ((> lines 0)
7976              (when (gnus-article-next-page lines)
7977                (gnus-message 3 "End of message")))
7978             ((< lines 0)
7979              (gnus-article-prev-page (- lines))))))
7980   (gnus-summary-recenter)
7981   (gnus-summary-position-point))
7982
7983 (defun gnus-summary-scroll-down (lines)
7984   "Scroll down (or up) one line current article.
7985 Argument LINES specifies lines to be scrolled down (or up if negative).
7986 If no article is selected, then the current article will be selected first."
7987   (interactive "p")
7988   (gnus-summary-scroll-up (- lines)))
7989
7990 (defun gnus-summary-next-same-subject ()
7991   "Select next article which has the same subject as current one."
7992   (interactive)
7993   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7994
7995 (defun gnus-summary-prev-same-subject ()
7996   "Select previous article which has the same subject as current one."
7997   (interactive)
7998   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7999
8000 (defun gnus-summary-next-unread-same-subject ()
8001   "Select next unread article which has the same subject as current one."
8002   (interactive)
8003   (gnus-summary-next-article t (gnus-summary-article-subject)))
8004
8005 (defun gnus-summary-prev-unread-same-subject ()
8006   "Select previous unread article which has the same subject as current one."
8007   (interactive)
8008   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8009
8010 (defun gnus-summary-first-unread-article ()
8011   "Select the first unread article.
8012 Return nil if there are no unread articles."
8013   (interactive)
8014   (prog1
8015       (when (gnus-summary-first-subject t)
8016         (gnus-summary-show-thread)
8017         (gnus-summary-first-subject t)
8018         (gnus-summary-display-article (gnus-summary-article-number)))
8019     (gnus-summary-position-point)))
8020
8021 (defun gnus-summary-first-unread-subject ()
8022   "Place the point on the subject line of the first unread article.
8023 Return nil if there are no unread articles."
8024   (interactive)
8025   (prog1
8026       (when (gnus-summary-first-subject t)
8027         (gnus-summary-show-thread)
8028         (gnus-summary-first-subject t))
8029     (gnus-summary-position-point)))
8030
8031 (defun gnus-summary-first-unseen-subject ()
8032   "Place the point on the subject line of the first unseen article.
8033 Return nil if there are no unseen articles."
8034   (interactive)
8035   (prog1
8036       (when (gnus-summary-first-subject nil nil t)
8037         (gnus-summary-show-thread)
8038         (gnus-summary-first-subject nil nil t))
8039     (gnus-summary-position-point)))
8040
8041 (defun gnus-summary-first-unseen-or-unread-subject ()
8042   "Place the point on the subject line of the first unseen and unread article.
8043 If all article have been seen, on the subject line of the first unread
8044 article."
8045   (interactive)
8046   (prog1
8047       (unless (when (gnus-summary-first-subject nil nil t)
8048                 (gnus-summary-show-thread)
8049                 (gnus-summary-first-subject nil nil t))
8050         (when (gnus-summary-first-subject t)
8051           (gnus-summary-show-thread)
8052           (gnus-summary-first-subject t)))
8053     (gnus-summary-position-point)))
8054
8055 (defun gnus-summary-first-article ()
8056   "Select the first article.
8057 Return nil if there are no articles."
8058   (interactive)
8059   (prog1
8060       (when (gnus-summary-first-subject)
8061         (gnus-summary-show-thread)
8062         (gnus-summary-first-subject)
8063         (gnus-summary-display-article (gnus-summary-article-number)))
8064     (gnus-summary-position-point)))
8065
8066 (defun gnus-summary-best-unread-article (&optional arg)
8067   "Select the unread article with the highest score.
8068 If given a prefix argument, select the next unread article that has a
8069 score higher than the default score."
8070   (interactive "P")
8071   (let ((article (if arg
8072                      (gnus-summary-better-unread-subject)
8073                    (gnus-summary-best-unread-subject))))
8074     (if article
8075         (gnus-summary-goto-article article)
8076       (error "No unread articles"))))
8077
8078 (defun gnus-summary-best-unread-subject ()
8079   "Select the unread subject with the highest score."
8080   (interactive)
8081   (let ((best -1000000)
8082         (data gnus-newsgroup-data)
8083         article score)
8084     (while data
8085       (and (gnus-data-unread-p (car data))
8086            (> (setq score
8087                     (gnus-summary-article-score (gnus-data-number (car data))))
8088               best)
8089            (setq best score
8090                  article (gnus-data-number (car data))))
8091       (setq data (cdr data)))
8092     (when article
8093       (gnus-summary-goto-subject article))
8094     (gnus-summary-position-point)
8095     article))
8096
8097 (defun gnus-summary-better-unread-subject ()
8098   "Select the first unread subject that has a score over the default score."
8099   (interactive)
8100   (let ((data gnus-newsgroup-data)
8101         article score)
8102     (while (and (setq article (gnus-data-number (car data)))
8103                 (or (gnus-data-read-p (car data))
8104                     (not (> (gnus-summary-article-score article)
8105                             gnus-summary-default-score))))
8106       (setq data (cdr data)))
8107     (when article
8108       (gnus-summary-goto-subject article))
8109     (gnus-summary-position-point)
8110     article))
8111
8112 (defun gnus-summary-last-subject ()
8113   "Go to the last displayed subject line in the group."
8114   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8115     (when article
8116       (gnus-summary-goto-subject article))))
8117
8118 (defun gnus-summary-goto-article (article &optional all-headers force)
8119   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8120 If ALL-HEADERS is non-nil, no header lines are hidden.
8121 If FORCE, go to the article even if it isn't displayed.  If FORCE
8122 is a number, it is the line the article is to be displayed on."
8123   (interactive
8124    (list
8125     (gnus-completing-read
8126      "Article number or Message-ID"
8127      (mapcar 'int-to-string gnus-newsgroup-limit))
8128     current-prefix-arg
8129     t))
8130   (prog1
8131       (if (and (stringp article)
8132                (string-match "@\\|%40" article))
8133           (gnus-summary-refer-article article)
8134         (when (stringp article)
8135           (setq article (string-to-number article)))
8136         (if (gnus-summary-goto-subject article force)
8137             (gnus-summary-display-article article all-headers)
8138           (gnus-message 4 "Couldn't go to article %s" article) nil))
8139     (gnus-summary-position-point)))
8140
8141 (defun gnus-summary-goto-last-article ()
8142   "Go to the previously read article."
8143   (interactive)
8144   (prog1
8145       (when gnus-last-article
8146         (gnus-summary-goto-article gnus-last-article nil t))
8147     (gnus-summary-position-point)))
8148
8149 (defun gnus-summary-pop-article (number)
8150   "Pop one article off the history and go to the previous.
8151 NUMBER articles will be popped off."
8152   (interactive "p")
8153   (let (to)
8154     (setq gnus-newsgroup-history
8155           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8156     (if to
8157         (gnus-summary-goto-article (car to) nil t)
8158       (error "Article history empty")))
8159   (gnus-summary-position-point))
8160
8161 ;; Summary commands and functions for limiting the summary buffer.
8162
8163 (defun gnus-summary-limit-to-articles (n)
8164   "Limit the summary buffer to the next N articles.
8165 If not given a prefix, use the process marked articles instead."
8166   (interactive "P")
8167   (prog1
8168       (let ((articles (gnus-summary-work-articles n)))
8169         (setq gnus-newsgroup-processable nil)
8170         (gnus-summary-limit articles))
8171     (gnus-summary-position-point)))
8172
8173 (defun gnus-summary-pop-limit (&optional total)
8174   "Restore the previous limit.
8175 If given a prefix, remove all limits."
8176   (interactive "P")
8177   (when total
8178     (setq gnus-newsgroup-limits
8179           (list (mapcar (lambda (h) (mail-header-number h))
8180                         gnus-newsgroup-headers))))
8181   (unless gnus-newsgroup-limits
8182     (error "No limit to pop"))
8183   (prog1
8184       (gnus-summary-limit nil 'pop)
8185     (gnus-summary-position-point)))
8186
8187 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8188   "Limit the summary buffer to articles that have subjects that match a regexp.
8189 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8190   (interactive
8191    (list (read-string (if current-prefix-arg
8192                           "Exclude subject (regexp): "
8193                         "Limit to subject (regexp): "))
8194          nil current-prefix-arg))
8195   (unless header
8196     (setq header "subject"))
8197   (when (not (equal "" subject))
8198     (prog1
8199         (let ((articles (gnus-summary-find-matching
8200                          (or header "subject") subject 'all nil nil
8201                          not-matching)))
8202           (unless articles
8203             (error "Found no matches for \"%s\"" subject))
8204           (gnus-summary-limit articles))
8205       (gnus-summary-position-point))))
8206
8207 (defun gnus-summary-limit-to-author (from &optional not-matching)
8208   "Limit the summary buffer to articles that have authors that match a regexp.
8209 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8210   (interactive
8211    (list (let* ((header (gnus-summary-article-header))
8212                 (default (and header (car (mail-header-parse-address
8213                                            (mail-header-from header))))))
8214            (read-string (concat (if current-prefix-arg
8215                                     "Exclude author (regexp"
8216                                   "Limit to author (regexp")
8217                                 (if default
8218                                     (concat ", default \"" default "\"): ")
8219                                   "): "))
8220                         nil nil
8221                         default))
8222          current-prefix-arg))
8223   (gnus-summary-limit-to-subject from "from" not-matching))
8224
8225 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8226   "Limit the summary buffer to articles with the given RECIPIENT.
8227
8228 If NOT-MATCHING, exclude RECIPIENT.
8229
8230 To and Cc headers are checked.  You need to include them in
8231 `nnmail-extra-headers'."
8232   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8233   (interactive
8234    (list (read-string (format "%s recipient (regexp): "
8235                               (if current-prefix-arg "Exclude" "Limit to")))
8236          current-prefix-arg))
8237   (when (not (equal "" recipient))
8238     (prog1 (let* ((to
8239                    (if (memq 'To nnmail-extra-headers)
8240                        (gnus-summary-find-matching
8241                         (cons 'extra 'To) recipient 'all nil nil
8242                         not-matching)
8243                      (gnus-message
8244                       1 "`To' isn't present in `nnmail-extra-headers'")
8245                      (sit-for 1)
8246                      nil))
8247                   (cc
8248                    (if (memq 'Cc nnmail-extra-headers)
8249                        (gnus-summary-find-matching
8250                         (cons 'extra 'Cc) recipient 'all nil nil
8251                         not-matching)
8252                      (gnus-message
8253                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8254                      (sit-for 1)
8255                      nil))
8256                   (articles
8257                    (if not-matching
8258                        ;; We need the numbers that are in both lists:
8259                        (mapcar (lambda (a)
8260                                  (and (memq a to) a))
8261                                cc)
8262                      (nconc to cc))))
8263              (unless articles
8264                (error "Found no matches for \"%s\"" recipient))
8265              (gnus-summary-limit articles))
8266       (gnus-summary-position-point))))
8267
8268 (defun gnus-summary-limit-to-address (address &optional not-matching)
8269   "Limit the summary buffer to articles with the given ADDRESS.
8270
8271 If NOT-MATCHING, exclude ADDRESS.
8272
8273 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8274 in `nnmail-extra-headers'."
8275   (interactive
8276    (list (read-string (format "%s address (regexp): "
8277                               (if current-prefix-arg "Exclude" "Limit to")))
8278          current-prefix-arg))
8279   (when (not (equal "" address))
8280     (prog1 (let* ((to
8281                    (if (memq 'To nnmail-extra-headers)
8282                        (gnus-summary-find-matching
8283                         (cons 'extra 'To) address 'all nil nil
8284                         not-matching)
8285                      (gnus-message
8286                       1 "`To' isn't present in `nnmail-extra-headers'")
8287                      (sit-for 1)
8288                      t))
8289                   (cc
8290                    (if (memq 'Cc nnmail-extra-headers)
8291                        (gnus-summary-find-matching
8292                         (cons 'extra 'Cc) address 'all nil nil
8293                         not-matching)
8294                      (gnus-message
8295                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8296                      (sit-for 1)
8297                      t))
8298                   (from
8299                    (gnus-summary-find-matching "from" address
8300                                                'all nil nil not-matching))
8301                   (articles
8302                    (if not-matching
8303                        ;; We need the numbers that are in all lists:
8304                        (if (eq cc t)
8305                            (if (eq to t)
8306                                from
8307                              (mapcar (lambda (a) (car (memq a from))) to))
8308                          (if (eq to t)
8309                              (mapcar (lambda (a) (car (memq a from))) cc)
8310                            (mapcar (lambda (a) (car (memq a from)))
8311                                    (mapcar (lambda (a) (car (memq a to)))
8312                                            cc))))
8313                      (nconc (if (eq to t) nil to)
8314                             (if (eq cc t) nil cc)
8315                             from))))
8316              (unless articles
8317                (error "Found no matches for \"%s\"" address))
8318              (gnus-summary-limit articles))
8319       (gnus-summary-position-point))))
8320
8321 (defun gnus-summary-limit-strange-charsets-predicate (header)
8322   (when (fboundp 'char-charset)
8323     (let ((string (concat (mail-header-subject header)
8324                           (mail-header-from header)))
8325           charset found)
8326       (dotimes (i (1- (length string)))
8327         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8328         (when (string-match "unicode\\|big\\|japanese" charset)
8329           (setq found t)))
8330       found)))
8331
8332 (defun gnus-summary-limit-to-predicate (predicate)
8333   "Limit to articles where PREDICATE returns non-nil.
8334 PREDICATE will be called with the header structures of the
8335 articles."
8336   (let ((articles nil)
8337         (case-fold-search t))
8338     (dolist (header gnus-newsgroup-headers)
8339       (when (funcall predicate header)
8340         (push (mail-header-number header) articles)))
8341     (gnus-summary-limit (nreverse articles))))
8342
8343 (defun gnus-summary-limit-to-age (age &optional younger-p)
8344   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8345 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8346 articles that are younger than AGE days."
8347   (interactive
8348    (let ((younger current-prefix-arg)
8349          (days-got nil)
8350          days)
8351      (while (not days-got)
8352        (setq days (if younger
8353                       (read-string "Limit to articles younger than (in days, older when negative): ")
8354                     (read-string
8355                      "Limit to articles older than (in days, younger when negative): ")))
8356        (when (> (length days) 0)
8357          (setq days (read days)))
8358        (if (numberp days)
8359            (progn
8360              (setq days-got t)
8361              (when (< days 0)
8362                (setq younger (not younger))
8363                (setq days (* days -1))))
8364          (message "Please enter a number.")
8365          (sleep-for 1)))
8366      (list days younger)))
8367   (prog1
8368       (let ((data gnus-newsgroup-data)
8369             (cutoff (days-to-time age))
8370             articles d date is-younger)
8371         (while (setq d (pop data))
8372           (when (and (vectorp (gnus-data-header d))
8373                      (setq date (mail-header-date (gnus-data-header d))))
8374             (setq is-younger (time-less-p
8375                               (time-since (gnus-date-get-time date))
8376                               cutoff))
8377             (when (if younger-p
8378                       is-younger
8379                     (not is-younger))
8380               (push (gnus-data-number d) articles))))
8381         (gnus-summary-limit (nreverse articles)))
8382     (gnus-summary-position-point)))
8383
8384 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8385   "Limit the summary buffer to articles that match an 'extra' header."
8386   (interactive
8387    (let ((header
8388           (intern
8389            (gnus-completing-read
8390             (if current-prefix-arg
8391                 "Exclude extra header"
8392               "Limit extra header")
8393             (mapcar 'symbol-name gnus-extra-headers)
8394             t nil nil
8395             (symbol-name (car gnus-extra-headers))))))
8396      (list header
8397            (read-string (format "%s header %s (regexp): "
8398                                 (if current-prefix-arg "Exclude" "Limit to")
8399                                 header))
8400            current-prefix-arg)))
8401   (when (not (equal "" regexp))
8402     (prog1
8403         (let ((articles (gnus-summary-find-matching
8404                          (cons 'extra header) regexp 'all nil nil
8405                          not-matching)))
8406           (unless articles
8407             (error "Found no matches for \"%s\"" regexp))
8408           (gnus-summary-limit articles))
8409       (gnus-summary-position-point))))
8410
8411 (defun gnus-summary-limit-to-display-predicate ()
8412   "Limit the summary buffer to the predicated in the `display' group parameter."
8413   (interactive)
8414   (unless gnus-newsgroup-display
8415     (error "There is no `display' group parameter"))
8416   (let (articles)
8417     (dolist (gnus-number gnus-newsgroup-articles)
8418       (when (funcall gnus-newsgroup-display)
8419         (push gnus-number articles)))
8420     (gnus-summary-limit articles))
8421   (gnus-summary-position-point))
8422
8423 (defun gnus-summary-limit-to-unread (&optional all)
8424   "Limit the summary buffer to articles that are not marked as read.
8425 If ALL is non-nil, limit strictly to unread articles."
8426   (interactive "P")
8427   (if all
8428       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8429     (gnus-summary-limit-to-marks
8430      ;; Concat all the marks that say that an article is read and have
8431      ;; those removed.
8432      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8433            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8434            gnus-low-score-mark gnus-expirable-mark
8435            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8436            gnus-duplicate-mark)
8437      'reverse)))
8438
8439 (defun gnus-summary-limit-to-headers (match &optional reverse)
8440   "Limit the summary buffer to articles that have headers that match MATCH.
8441 If REVERSE (the prefix), limit to articles that don't match."
8442   (interactive "sMatch headers (regexp): \nP")
8443   (gnus-summary-limit-to-bodies match reverse t))
8444
8445 (declare-function article-goto-body "gnus-art" ())
8446
8447 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8448   "Limit the summary buffer to articles that have bodies that match MATCH.
8449 If REVERSE (the prefix), limit to articles that don't match."
8450   (interactive "sMatch body (regexp): \nP")
8451   (let ((articles nil)
8452         (gnus-select-article-hook nil)  ;Disable hook.
8453         (gnus-article-prepare-hook nil)
8454         (gnus-use-article-prefetch nil)
8455         (gnus-keep-backlog nil)
8456         (gnus-break-pages nil)
8457         (gnus-summary-display-arrow nil)
8458         (gnus-updated-mode-lines nil)
8459         (gnus-auto-center-summary nil)
8460         (gnus-display-mime-function nil))
8461     (dolist (data gnus-newsgroup-data)
8462       (let (gnus-mark-article-hook)
8463         (gnus-summary-select-article t t nil (gnus-data-number data)))
8464       (with-current-buffer gnus-article-buffer
8465         (article-goto-body)
8466         (let* ((case-fold-search t)
8467                (found (if headersp
8468                           (re-search-backward match nil t)
8469                         (re-search-forward match nil t))))
8470           (when (or (and found
8471                          (not reverse))
8472                     (and (not found)
8473                          reverse))
8474             (push (gnus-data-number data) articles)))))
8475     (if (not articles)
8476         (message "No messages matched")
8477       (gnus-summary-limit articles)))
8478   (gnus-summary-position-point))
8479
8480 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8481   "Limit the summary buffer to articles that aren't part on any thread.
8482 If THREADSP (the prefix), limit to articles that are in threads."
8483   (interactive "P")
8484   (let ((articles nil)
8485         thread-articles
8486         threads)
8487     (dolist (thread gnus-newsgroup-threads)
8488       (if (stringp (car thread))
8489           (dolist (thread (cdr thread))
8490             (push thread threads))
8491         (push thread threads)))
8492     (dolist (thread threads)
8493       (setq thread-articles (gnus-articles-in-thread thread))
8494       (when (or (and threadsp
8495                      (> (length thread-articles) 1))
8496                 (and (not threadsp)
8497                      (= (length thread-articles) 1)))
8498         (setq articles (nconc thread-articles articles))))
8499     (if (not articles)
8500         (message "No messages matched")
8501       (gnus-summary-limit articles))
8502     (gnus-summary-position-point)))
8503
8504 (defun gnus-summary-limit-to-replied (&optional unreplied)
8505   "Limit the summary buffer to replied articles.
8506 If UNREPLIED (the prefix), limit to unreplied articles."
8507   (interactive "P")
8508   (if unreplied
8509       (gnus-summary-limit
8510        (gnus-set-difference gnus-newsgroup-articles
8511         gnus-newsgroup-replied))
8512     (gnus-summary-limit gnus-newsgroup-replied))
8513   (gnus-summary-position-point))
8514
8515 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8516   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8517 If REVERSE, limit the summary buffer to articles that are marked
8518 with MARKS.  MARKS can either be a string of marks or a list of marks.
8519 Returns how many articles were removed."
8520   (interactive "sMarks: ")
8521   (gnus-summary-limit-to-marks marks t))
8522
8523 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8524   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8525 If REVERSE (the prefix), limit the summary buffer to articles that are
8526 not marked with MARKS.  MARKS can either be a string of marks or a
8527 list of marks.
8528 Returns how many articles were removed."
8529   (interactive "sMarks: \nP")
8530   (prog1
8531       (let ((data gnus-newsgroup-data)
8532             (marks (if (listp marks) marks
8533                      (append marks nil))) ; Transform to list.
8534             articles)
8535         (while data
8536           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8537                   (memq (gnus-data-mark (car data)) marks))
8538             (push (gnus-data-number (car data)) articles))
8539           (setq data (cdr data)))
8540         (gnus-summary-limit articles))
8541     (gnus-summary-position-point)))
8542
8543 (defun gnus-summary-limit-to-score (score)
8544   "Limit to articles with score at or above SCORE."
8545   (interactive "NLimit to articles with score of at least: ")
8546   (let ((data gnus-newsgroup-data)
8547         articles)
8548     (while data
8549       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8550                 score)
8551         (push (gnus-data-number (car data)) articles))
8552       (setq data (cdr data)))
8553     (prog1
8554         (gnus-summary-limit articles)
8555       (gnus-summary-position-point))))
8556
8557 (defun gnus-summary-limit-to-unseen ()
8558   "Limit to unseen articles."
8559   (interactive)
8560   (prog1
8561       (gnus-summary-limit gnus-newsgroup-unseen)
8562     (gnus-summary-position-point)))
8563
8564 (defun gnus-summary-limit-include-thread (id)
8565   "Display all the hidden articles that is in the thread with ID in it.
8566 When called interactively, ID is the Message-ID of the current
8567 article."
8568   (interactive (list (mail-header-id (gnus-summary-article-header))))
8569   (let ((articles (gnus-articles-in-thread
8570                    (gnus-id-to-thread (gnus-root-id id))))
8571         ;;we REALLY want the whole thread---this prevents cut-threads
8572         ;;from removing the thread we want to include.
8573         (gnus-fetch-old-headers nil)
8574         (gnus-build-sparse-threads nil))
8575     (prog1
8576         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8577       (gnus-summary-limit-include-matching-articles
8578        "subject"
8579        (regexp-quote (gnus-simplify-subject-re
8580                       (mail-header-subject (gnus-id-to-header id)))))
8581       (gnus-summary-position-point))))
8582
8583 (defun gnus-summary-limit-include-matching-articles (header regexp)
8584   "Display all the hidden articles that have HEADERs that match REGEXP."
8585   (interactive (list (read-string "Match on header: ")
8586                      (read-string "Regexp: ")))
8587   (let ((articles (gnus-find-matching-articles header regexp)))
8588     (prog1
8589         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8590       (gnus-summary-position-point))))
8591
8592 (defun gnus-summary-insert-dormant-articles ()
8593   "Insert all the dormant articles for this group into the current buffer."
8594   (interactive)
8595   (let ((gnus-verbose (max 6 gnus-verbose)))
8596     (if (not gnus-newsgroup-dormant)
8597         (gnus-message 3 "No dormant articles for this group")
8598       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8599
8600 (defun gnus-summary-insert-ticked-articles ()
8601   "Insert ticked articles for this group into the current buffer."
8602   (interactive)
8603   (let ((gnus-verbose (max 6 gnus-verbose)))
8604     (if (not gnus-newsgroup-marked)
8605         (gnus-message 3 "No ticked articles for this group")
8606       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8607
8608 (defun gnus-summary-limit-include-dormant ()
8609   "Display all the hidden articles that are marked as dormant.
8610 Note that this command only works on a subset of the articles currently
8611 fetched for this group."
8612   (interactive)
8613   (unless gnus-newsgroup-dormant
8614     (error "There are no dormant articles in this group"))
8615   (prog1
8616       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8617     (gnus-summary-position-point)))
8618
8619 (defun gnus-summary-include-articles (articles)
8620   "Fetch the headers for ARTICLES and then display the summary lines."
8621   (let ((gnus-inhibit-demon t)
8622         (gnus-agent nil)
8623         (gnus-read-all-available-headers t))
8624     (setq gnus-newsgroup-headers
8625           (gnus-merge
8626            'list gnus-newsgroup-headers
8627            (gnus-fetch-headers articles nil t)
8628            'gnus-article-sort-by-number))
8629     (setq gnus-newsgroup-articles
8630           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8631     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8632
8633 (defun gnus-summary-limit-exclude-dormant ()
8634   "Hide all dormant articles."
8635   (interactive)
8636   (prog1
8637       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8638     (gnus-summary-position-point)))
8639
8640 (defun gnus-summary-limit-exclude-childless-dormant ()
8641   "Hide all dormant articles that have no children."
8642   (interactive)
8643   (let ((data (gnus-data-list t))
8644         articles d children)
8645     ;; Find all articles that are either not dormant or have
8646     ;; children.
8647     (while (setq d (pop data))
8648       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8649                 (and (setq children
8650                            (gnus-article-children (gnus-data-number d)))
8651                      (let (found)
8652                        (while children
8653                          (when (memq (car children) articles)
8654                            (setq children nil
8655                                  found t))
8656                          (pop children))
8657                        found)))
8658         (push (gnus-data-number d) articles)))
8659     ;; Do the limiting.
8660     (prog1
8661         (gnus-summary-limit articles)
8662       (gnus-summary-position-point))))
8663
8664 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8665   "Mark all unread excluded articles as read.
8666 If ALL, mark even excluded ticked and dormants as read."
8667   (interactive "P")
8668   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8669   (let ((articles (gnus-sorted-ndifference
8670                    (sort
8671                     (mapcar (lambda (h) (mail-header-number h))
8672                             gnus-newsgroup-headers)
8673                     '<)
8674                    gnus-newsgroup-limit))
8675         article)
8676     (setq gnus-newsgroup-unreads
8677           (gnus-sorted-intersection gnus-newsgroup-unreads
8678                                     gnus-newsgroup-limit))
8679     (if all
8680         (setq gnus-newsgroup-dormant nil
8681               gnus-newsgroup-marked nil
8682               gnus-newsgroup-reads
8683               (nconc
8684                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8685                gnus-newsgroup-reads))
8686       (while (setq article (pop articles))
8687         (unless (or (memq article gnus-newsgroup-dormant)
8688                     (memq article gnus-newsgroup-marked))
8689           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8690
8691 (defun gnus-summary-limit (articles &optional pop)
8692   (if pop
8693       ;; We pop the previous limit off the stack and use that.
8694       (setq articles (car gnus-newsgroup-limits)
8695             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8696     ;; We use the new limit, so we push the old limit on the stack.
8697     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8698   ;; Set the limit.
8699   (setq gnus-newsgroup-limit articles)
8700   (let ((total (length gnus-newsgroup-data))
8701         (data (gnus-data-find-list (gnus-summary-article-number)))
8702         (gnus-summary-mark-below nil)   ; Inhibit this.
8703         found)
8704     ;; This will do all the work of generating the new summary buffer
8705     ;; according to the new limit.
8706     (gnus-summary-prepare)
8707     ;; Hide any threads, possibly.
8708     (gnus-summary-maybe-hide-threads)
8709     ;; Try to return to the article you were at, or one in the
8710     ;; neighborhood.
8711     (when data
8712       ;; We try to find some article after the current one.
8713       (while data
8714         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8715           (setq data nil
8716                 found t))
8717         (setq data (cdr data))))
8718     (unless found
8719       ;; If there is no data, that means that we were after the last
8720       ;; article.  The same goes when we can't find any articles
8721       ;; after the current one.
8722       (goto-char (point-max))
8723       (gnus-summary-find-prev))
8724     (gnus-set-mode-line 'summary)
8725     ;; We return how many articles were removed from the summary
8726     ;; buffer as a result of the new limit.
8727     (- total (length gnus-newsgroup-data))))
8728
8729 (defsubst gnus-invisible-cut-children (threads)
8730   (let ((num 0))
8731     (while threads
8732       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8733         (incf num))
8734       (pop threads))
8735     (< num 2)))
8736
8737 (defsubst gnus-cut-thread (thread)
8738   "Go forwards in the thread until we find an article that we want to display."
8739   (when (or (eq gnus-fetch-old-headers 'some)
8740             (eq gnus-fetch-old-headers 'invisible)
8741             (numberp gnus-fetch-old-headers)
8742             (eq gnus-build-sparse-threads 'some)
8743             (eq gnus-build-sparse-threads 'more))
8744     ;; Deal with old-fetched headers and sparse threads.
8745     (while (and
8746             thread
8747             (or
8748              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8749              (gnus-summary-article-ancient-p
8750               (mail-header-number (car thread))))
8751             (if (or (<= (length (cdr thread)) 1)
8752                     (eq gnus-fetch-old-headers 'invisible))
8753                 (setq gnus-newsgroup-limit
8754                       (delq (mail-header-number (car thread))
8755                             gnus-newsgroup-limit)
8756                       thread (cadr thread))
8757               (when (gnus-invisible-cut-children (cdr thread))
8758                 (let ((th (cdr thread)))
8759                   (while th
8760                     (if (memq (mail-header-number (caar th))
8761                               gnus-newsgroup-limit)
8762                         (setq thread (car th)
8763                               th nil)
8764                       (setq th (cdr th))))))))))
8765   thread)
8766
8767 (defun gnus-cut-threads (threads)
8768   "Cut off all uninteresting articles from the beginning of THREADS."
8769   (when (or (eq gnus-fetch-old-headers 'some)
8770             (eq gnus-fetch-old-headers 'invisible)
8771             (numberp gnus-fetch-old-headers)
8772             (eq gnus-build-sparse-threads 'some)
8773             (eq gnus-build-sparse-threads 'more))
8774     (let ((th threads))
8775       (while th
8776         (setcar th (gnus-cut-thread (car th)))
8777         (setq th (cdr th)))))
8778   ;; Remove nixed out threads.
8779   (delq nil threads))
8780
8781 (defun gnus-summary-initial-limit (&optional show-if-empty)
8782   "Figure out what the initial limit is supposed to be on group entry.
8783 This entails weeding out unwanted dormants, low-scored articles,
8784 fetch-old-headers verbiage, and so on."
8785   ;; Most groups have nothing to remove.
8786   (unless (or gnus-inhibit-limiting
8787               (and (null gnus-newsgroup-dormant)
8788                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8789                    (not (eq gnus-fetch-old-headers 'some))
8790                    (not (numberp gnus-fetch-old-headers))
8791                    (not (eq gnus-fetch-old-headers 'invisible))
8792                    (null gnus-summary-expunge-below)
8793                    (not (eq gnus-build-sparse-threads 'some))
8794                    (not (eq gnus-build-sparse-threads 'more))
8795                    (null gnus-thread-expunge-below)))
8796     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8797     (setq gnus-newsgroup-limit nil)
8798     (mapatoms
8799      (lambda (node)
8800        (unless (car (symbol-value node))
8801          ;; These threads have no parents -- they are roots.
8802          (let ((nodes (cdr (symbol-value node)))
8803                thread)
8804            (while nodes
8805              (if (and gnus-thread-expunge-below
8806                       (< (gnus-thread-total-score (car nodes))
8807                          gnus-thread-expunge-below))
8808                  (gnus-expunge-thread (pop nodes))
8809                (setq thread (pop nodes))
8810                (gnus-summary-limit-children thread))))))
8811      gnus-newsgroup-dependencies)
8812     ;; If this limitation resulted in an empty group, we might
8813     ;; pop the previous limit and use it instead.
8814     (when (and (not gnus-newsgroup-limit)
8815                show-if-empty)
8816       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8817     gnus-newsgroup-limit))
8818
8819 (defun gnus-summary-limit-children (thread)
8820   "Return 1 if this subthread is visible and 0 if it is not."
8821   ;; First we get the number of visible children to this thread.  This
8822   ;; is done by recursing down the thread using this function, so this
8823   ;; will really go down to a leaf article first, before slowly
8824   ;; working its way up towards the root.
8825   (when thread
8826     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8827            (children
8828            (if (cdr thread)
8829                (apply '+ (mapcar 'gnus-summary-limit-children
8830                                  (cdr thread)))
8831              0))
8832            (number (mail-header-number (car thread)))
8833            score)
8834       (if (and
8835            (not (memq number gnus-newsgroup-marked))
8836            (or
8837             ;; If this article is dormant and has absolutely no visible
8838             ;; children, then this article isn't visible.
8839             (and (memq number gnus-newsgroup-dormant)
8840                  (zerop children))
8841             ;; If this is "fetch-old-headered" and there is no
8842             ;; visible children, then we don't want this article.
8843             (and (or (eq gnus-fetch-old-headers 'some)
8844                      (numberp gnus-fetch-old-headers))
8845                  (gnus-summary-article-ancient-p number)
8846                  (zerop children))
8847             ;; If this is "fetch-old-headered" and `invisible', then
8848             ;; we don't want this article.
8849             (and (eq gnus-fetch-old-headers 'invisible)
8850                  (gnus-summary-article-ancient-p number))
8851             ;; If this is a sparsely inserted article with no children,
8852             ;; we don't want it.
8853             (and (eq gnus-build-sparse-threads 'some)
8854                  (gnus-summary-article-sparse-p number)
8855                  (zerop children))
8856             ;; If we use expunging, and this article is really
8857             ;; low-scored, then we don't want this article.
8858             (when (and gnus-summary-expunge-below
8859                        (< (setq score
8860                                 (or (cdr (assq number gnus-newsgroup-scored))
8861                                     gnus-summary-default-score))
8862                           gnus-summary-expunge-below))
8863               ;; We increase the expunge-tally here, but that has
8864               ;; nothing to do with the limits, really.
8865               (incf gnus-newsgroup-expunged-tally)
8866               ;; We also mark as read here, if that's wanted.
8867               (when (and gnus-summary-mark-below
8868                          (< score gnus-summary-mark-below))
8869                 (setq gnus-newsgroup-unreads
8870                       (delq number gnus-newsgroup-unreads))
8871                 (if gnus-newsgroup-auto-expire
8872                     (push number gnus-newsgroup-expirable)
8873                   (push (cons number gnus-low-score-mark)
8874                         gnus-newsgroup-reads)))
8875               t)
8876             ;; Do the `display' group parameter.
8877             (and gnus-newsgroup-display
8878                  (let ((gnus-number number))
8879                    (not (funcall gnus-newsgroup-display))))))
8880           ;; Nope, invisible article.
8881           0
8882         ;; Ok, this article is to be visible, so we add it to the limit
8883         ;; and return 1.
8884         (push number gnus-newsgroup-limit)
8885         1))))
8886
8887 (defun gnus-expunge-thread (thread)
8888   "Mark all articles in THREAD as read."
8889   (let* ((number (mail-header-number (car thread))))
8890     (incf gnus-newsgroup-expunged-tally)
8891     ;; We also mark as read here, if that's wanted.
8892     (setq gnus-newsgroup-unreads
8893           (delq number gnus-newsgroup-unreads))
8894     (if gnus-newsgroup-auto-expire
8895         (push number gnus-newsgroup-expirable)
8896       (push (cons number gnus-low-score-mark)
8897             gnus-newsgroup-reads)))
8898   ;; Go recursively through all subthreads.
8899   (mapcar 'gnus-expunge-thread (cdr thread)))
8900
8901 ;; Summary article oriented commands
8902
8903 (defun gnus-summary-refer-parent-article (n)
8904   "Refer parent article N times.
8905 If N is negative, go to ancestor -N instead.
8906 The difference between N and the number of articles fetched is returned."
8907   (interactive "p")
8908   (let ((skip 1)
8909         error header ref)
8910     (when (not (natnump n))
8911       (setq skip (abs n)
8912             n 1))
8913     (while (and (> n 0)
8914                 (not error))
8915       (setq header (gnus-summary-article-header))
8916       (if (and (eq (mail-header-number header)
8917                    (cdr gnus-article-current))
8918                (equal gnus-newsgroup-name
8919                       (car gnus-article-current)))
8920           ;; If we try to find the parent of the currently
8921           ;; displayed article, then we take a look at the actual
8922           ;; References header, since this is slightly more
8923           ;; reliable than the References field we got from the
8924           ;; server.
8925           (with-current-buffer gnus-original-article-buffer
8926             (nnheader-narrow-to-headers)
8927             (unless (setq ref (message-fetch-field "references"))
8928               (when (setq ref (message-fetch-field "in-reply-to"))
8929                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8930             (widen))
8931         (setq ref
8932               ;; It's not the current article, so we take a bet on
8933               ;; the value we got from the server.
8934               (mail-header-references header)))
8935       (if (and ref
8936                (not (equal ref "")))
8937           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8938             (gnus-message 1 "Couldn't find parent"))
8939         (gnus-message 1 "No references in article %d"
8940                       (gnus-summary-article-number))
8941         (setq error t))
8942       (decf n))
8943     (gnus-summary-position-point)
8944     n))
8945
8946 (defun gnus-summary-refer-references ()
8947   "Fetch all articles mentioned in the References header.
8948 Return the number of articles fetched."
8949   (interactive)
8950   (let ((ref (mail-header-references (gnus-summary-article-header)))
8951         (current (gnus-summary-article-number))
8952         (n 0))
8953     (if (or (not ref)
8954             (equal ref ""))
8955         (error "No References in the current article")
8956       ;; For each Message-ID in the References header...
8957       (while (string-match "<[^>]*>" ref)
8958         (incf n)
8959         ;; ... fetch that article.
8960         (gnus-summary-refer-article
8961          (prog1 (match-string 0 ref)
8962            (setq ref (substring ref (match-end 0))))))
8963       (gnus-summary-goto-subject current)
8964       (gnus-summary-position-point)
8965       n)))
8966
8967 (defun gnus-delete-duplicate-headers (headers)
8968   ;; First remove leading duplicates.
8969   (while (and (> (length headers) 1)
8970               (= (mail-header-number (car headers))
8971                  (mail-header-number (cadr headers))))
8972     (pop headers))
8973   ;; Then the rest.
8974   (let ((result headers))
8975     (while (> (length headers) 1)
8976       (if (= (mail-header-number (car headers))
8977              (mail-header-number (cadr headers)))
8978           (setcdr headers (cddr headers))
8979         (pop headers)))
8980     result))
8981
8982 (defun gnus-summary-refer-thread (&optional limit)
8983   "Fetch all articles in the current thread. For backends that
8984 know how to search for threads (currently only 'nnimap) a
8985 non-numeric prefix arg will use nnir to search the entire
8986 server; without a prefix arg only the current group is
8987 searched. If the variable `gnus-refer-thread-use-nnir' is
8988 non-nil the prefix arg has the reverse meaning. If no
8989 backend-specific 'request-thread function is available fetch
8990 LIMIT (the numerical prefix) old headers. If LIMIT is
8991 non-numeric or nil fetch the number specified by the
8992 `gnus-refer-thread-limit' variable."
8993   (interactive "P")
8994   (gnus-warp-to-article)
8995   (let* ((header (gnus-summary-article-header))
8996          (id (mail-header-id header))
8997          (gnus-inhibit-demon t)
8998          (gnus-summary-ignore-duplicates t)
8999          (gnus-read-all-available-headers t)
9000          (gnus-refer-thread-use-nnir
9001           (if (and (not (null limit)) (listp limit))
9002               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9003          (new-headers
9004           (if (gnus-check-backend-function
9005                'request-thread gnus-newsgroup-name)
9006               (gnus-request-thread header gnus-newsgroup-name)
9007             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9008                             gnus-refer-thread-limit))
9009                    (last (if (numberp limit)
9010                              (min (+ (mail-header-number header)
9011                                      limit)
9012                                   gnus-newsgroup-highest)
9013                            gnus-newsgroup-highest))
9014                    (subject (gnus-simplify-subject
9015                              (mail-header-subject header)))
9016                    (refs (split-string (or (mail-header-references header)
9017                                            "")))
9018                    (gnus-parse-headers-hook
9019                     `(lambda () (goto-char (point-min))
9020                       (keep-lines
9021                        (regexp-opt ',(append refs (list id subject)))))))
9022               (gnus-fetch-headers (list last) (if (numberp limit)
9023                                                   (* 2 limit) limit) t))))
9024          article-ids)
9025     (when (listp new-headers)
9026       (dolist (header new-headers)
9027         (push (mail-header-number header) article-ids)
9028         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9029           (push (mail-header-number header) gnus-newsgroup-unreads)
9030           (setq gnus-newsgroup-unselected
9031                 (delete (mail-header-number header)
9032                         gnus-newsgroup-unselected))))
9033       (setq gnus-newsgroup-headers
9034             (gnus-delete-duplicate-headers
9035              (gnus-merge
9036               'list gnus-newsgroup-headers new-headers
9037               'gnus-article-sort-by-number)))
9038       (setq gnus-newsgroup-articles
9039             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9040       (gnus-summary-limit-include-thread id)))
9041   (gnus-summary-show-thread))
9042
9043 (defun gnus-summary-refer-article (message-id)
9044   "Fetch an article specified by MESSAGE-ID."
9045   (interactive "sMessage-ID: ")
9046   (gnus-warp-to-article)
9047   (when (and (stringp message-id)
9048              (not (zerop (length message-id))))
9049     (setq message-id (gnus-replace-in-string message-id " " ""))
9050     ;; Construct the correct Message-ID if necessary.
9051     ;; Suggested by tale@pawl.rpi.edu.
9052     (unless (string-match "^<" message-id)
9053       (setq message-id (concat "<" message-id)))
9054     (unless (string-match ">$" message-id)
9055       (setq message-id (concat message-id ">")))
9056     ;; People often post MIDs from URLs, so unhex it:
9057     (unless (string-match "@" message-id)
9058       (setq message-id (gnus-url-unhex-string message-id)))
9059     (let* ((header (gnus-id-to-header message-id))
9060            (sparse (and header
9061                         (gnus-summary-article-sparse-p
9062                          (mail-header-number header))
9063                         (memq (mail-header-number header)
9064                               gnus-newsgroup-limit)))
9065            number)
9066       (cond
9067        ;; If the article is present in the buffer we just go to it.
9068        ((and header
9069              (or (not (gnus-summary-article-sparse-p
9070                        (mail-header-number header)))
9071                  sparse))
9072         (prog1
9073             (gnus-summary-goto-article
9074              (mail-header-number header) nil t)
9075           (when sparse
9076             (gnus-summary-update-article (mail-header-number header)))))
9077        (t
9078         ;; We fetch the article.
9079         (catch 'found
9080           (dolist (gnus-override-method (gnus-refer-article-methods))
9081             (when (and (gnus-check-server gnus-override-method)
9082                        ;; Fetch the header,
9083                        (setq number (gnus-summary-insert-subject message-id)))
9084               ;; and display the article.
9085               (gnus-summary-select-article nil nil nil number)
9086               (throw 'found t)))
9087           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9088
9089 (defun gnus-refer-article-methods ()
9090   "Return a list of referable methods."
9091   (cond
9092    ;; No method, so we default to current and native.
9093    ((null gnus-refer-article-method)
9094     (list gnus-current-select-method gnus-select-method))
9095    ;; Current.
9096    ((eq 'current gnus-refer-article-method)
9097     (list gnus-current-select-method))
9098    ;; List of select methods.
9099    ((not (and (symbolp (car gnus-refer-article-method))
9100               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9101     (let (out)
9102       (dolist (method gnus-refer-article-method)
9103         (push (if (eq 'current method)
9104                   gnus-current-select-method
9105                 (if (eq 'nnir (car method))
9106                     (list
9107                      'nnir
9108                      (or (cadr method)
9109                          (gnus-method-to-server gnus-current-select-method)))
9110                   method))
9111               out))
9112       (nreverse out)))
9113    ;; One single select method.
9114    (t
9115     (list gnus-refer-article-method))))
9116
9117 (defun gnus-summary-edit-parameters ()
9118   "Edit the group parameters of the current group."
9119   (interactive)
9120   (gnus-group-edit-group gnus-newsgroup-name 'params))
9121
9122 (defun gnus-summary-customize-parameters ()
9123   "Customize the group parameters of the current group."
9124   (interactive)
9125   (gnus-group-customize gnus-newsgroup-name))
9126
9127 (defun gnus-summary-enter-digest-group (&optional force)
9128   "Enter an nndoc group based on the current article.
9129 If FORCE, force a digest interpretation.  If not, try to guess
9130 what the document format is.
9131
9132 To control what happens when you exit the group, see the
9133 `gnus-auto-select-on-ephemeral-exit' variable."
9134   (interactive "P")
9135   (let ((conf gnus-current-window-configuration))
9136     (save-window-excursion
9137       (save-excursion
9138         (let (gnus-article-prepare-hook
9139               gnus-display-mime-function
9140               gnus-break-pages)
9141           (gnus-summary-select-article))))
9142     (setq gnus-current-window-configuration conf)
9143     (let* ((name (format "%s-%d"
9144                          (gnus-group-prefixed-name
9145                           gnus-newsgroup-name (list 'nndoc ""))
9146                          (with-current-buffer gnus-summary-buffer
9147                            gnus-current-article)))
9148            (ogroup gnus-newsgroup-name)
9149            (params (append (gnus-info-params (gnus-get-info ogroup))
9150                            (list (cons 'to-group ogroup))
9151                            (list (cons 'parent-group ogroup))
9152                            (list (cons 'save-article-group ogroup))))
9153            (case-fold-search t)
9154            (buf (current-buffer))
9155            dig to-address charset)
9156       (with-current-buffer gnus-original-article-buffer
9157         ;; Have the digest group inherit the main mail address of
9158         ;; the parent article.
9159         (when (setq to-address (or (gnus-fetch-field "reply-to")
9160                                    (gnus-fetch-field "from")))
9161           (setq params
9162                 (append
9163                  (list (cons 'to-address
9164                              (funcall gnus-decode-encoded-address-function
9165                                       to-address))))))
9166         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9167         (insert-buffer-substring gnus-original-article-buffer)
9168         (narrow-to-region
9169          (goto-char (point-min))
9170          (or (search-forward "\n\n" nil t) (point)))
9171         ;; Remove lines that may lead nndoc to misinterpret the
9172         ;; document type.
9173         (goto-char (point-min))
9174         (delete-matching-lines "^Path:\\|^From ")
9175         ;; Parse charset, and decode content transfer encoding.
9176         (setq charset (mail-content-type-get
9177                        (mail-header-parse-content-type
9178                         (or (gnus-fetch-field "content-type") ""))
9179                        'charset))
9180         (let ((encoding (gnus-fetch-field "content-transfer-encoding")))
9181           (when encoding
9182             (message-remove-header "content-transfer-encoding")
9183             (goto-char (point-max))
9184             (widen)
9185             (narrow-to-region (point) (point-max))
9186             (mm-decode-content-transfer-encoding
9187              (intern (downcase (mail-header-strip encoding))))))
9188         (widen))
9189       (unwind-protect
9190           (if (let ((gnus-newsgroup-ephemeral-charset
9191                      (if charset
9192                          (intern (downcase (gnus-strip-whitespace charset)))
9193                        gnus-newsgroup-charset))
9194                     (gnus-newsgroup-ephemeral-ignored-charsets
9195                      gnus-newsgroup-ignored-charsets))
9196                 (gnus-group-read-ephemeral-group
9197                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9198                               (nndoc-article-type
9199                                ,(if force 'mbox 'guess)))
9200                  t nil nil nil
9201                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9202                                                         "ADAPT")))))
9203               ;; Make all postings to this group go to the parent group.
9204               (nconc (gnus-info-params (gnus-get-info name))
9205                      params)
9206             ;; Couldn't select this doc group.
9207             (switch-to-buffer buf)
9208             (gnus-set-global-variables)
9209             (gnus-configure-windows 'summary)
9210             (gnus-message 3 "Article couldn't be entered?"))
9211         (kill-buffer dig)))))
9212
9213 (defun gnus-summary-read-document (n)
9214   "Open a new group based on the current article(s).
9215 This will allow you to read digests and other similar
9216 documents as newsgroups.
9217 Obeys the standard process/prefix convention."
9218   (interactive "P")
9219   (let* ((ogroup gnus-newsgroup-name)
9220          (params (append (gnus-info-params (gnus-get-info ogroup))
9221                          (list (cons 'to-group ogroup))))
9222          group egroup groups vgroup)
9223     (dolist (article (gnus-summary-work-articles n))
9224       (setq group (format "%s-%d" gnus-newsgroup-name article))
9225       (gnus-summary-remove-process-mark article)
9226       (when (gnus-summary-display-article article)
9227         (save-excursion ;;What for?
9228           (with-temp-buffer
9229             (insert-buffer-substring gnus-original-article-buffer)
9230             ;; Remove some headers that may lead nndoc to make
9231             ;; the wrong guess.
9232             (message-narrow-to-head)
9233             (goto-char (point-min))
9234             (delete-matching-lines "^Path:\\|^From ")
9235             (widen)
9236             (if (setq egroup
9237                       (gnus-group-read-ephemeral-group
9238                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9239                                      (nndoc-article-type guess))
9240                        t nil t))
9241                 (progn
9242                   ;; Make all postings to this group go to the parent group.
9243                   (nconc (gnus-info-params (gnus-get-info egroup))
9244                          params)
9245                   (push egroup groups))
9246               ;; Couldn't select this doc group.
9247               (gnus-error 3 "Article couldn't be entered"))))))
9248     ;; Now we have selected all the documents.
9249     (cond
9250      ((not groups)
9251       (error "None of the articles could be interpreted as documents"))
9252      ((gnus-group-read-ephemeral-group
9253        (setq vgroup (format
9254                      "nnvirtual:%s-%s" gnus-newsgroup-name
9255                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9256        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9257        t
9258        (cons (current-buffer) 'summary)))
9259      (t
9260       (error "Couldn't select virtual nndoc group")))))
9261
9262 (defun gnus-summary-widget-forward (arg)
9263   "Move point to the next field or button in the article.
9264 With optional ARG, move across that many fields."
9265   (interactive "p")
9266   (gnus-summary-select-article)
9267   (gnus-configure-windows 'article)
9268   (select-window (gnus-get-buffer-window gnus-article-buffer))
9269   (widget-forward arg))
9270
9271 (defun gnus-summary-widget-backward (arg)
9272   "Move point to the previous field or button in the article.
9273 With optional ARG, move across that many fields."
9274   (interactive "p")
9275   (gnus-summary-select-article)
9276   (gnus-configure-windows 'article)
9277   (select-window (gnus-get-buffer-window gnus-article-buffer))
9278   (unless (widget-at (point))
9279     (goto-char (point-max)))
9280   (widget-backward arg))
9281
9282 (defun gnus-summary-isearch-article (&optional regexp-p)
9283   "Do incremental search forward on the current article.
9284 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9285   (interactive "P")
9286   (gnus-summary-select-article)
9287   (gnus-configure-windows 'article)
9288   (gnus-eval-in-buffer-window gnus-article-buffer
9289     (save-restriction
9290       (widen)
9291       (isearch-forward regexp-p))))
9292
9293 (defun gnus-summary-repeat-search-article-forward ()
9294   "Repeat the previous search forwards."
9295   (interactive)
9296   (unless gnus-last-search-regexp
9297     (error "No previous search"))
9298   (gnus-summary-search-article-forward gnus-last-search-regexp))
9299
9300 (defun gnus-summary-repeat-search-article-backward ()
9301   "Repeat the previous search backwards."
9302   (interactive)
9303   (unless gnus-last-search-regexp
9304     (error "No previous search"))
9305   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9306
9307 (defun gnus-summary-search-article-forward (regexp &optional backward)
9308   "Search for an article containing REGEXP forward.
9309 If BACKWARD, search backward instead."
9310   (interactive
9311    (list (read-string
9312           (format "Search article %s (regexp%s): "
9313                   (if current-prefix-arg "backward" "forward")
9314                   (if gnus-last-search-regexp
9315                       (concat ", default " gnus-last-search-regexp)
9316                     "")))
9317          current-prefix-arg))
9318   (if (string-equal regexp "")
9319       (setq regexp (or gnus-last-search-regexp ""))
9320     (setq gnus-last-search-regexp regexp)
9321     (setq gnus-article-before-search gnus-current-article))
9322   ;; Intentionally set gnus-last-article.
9323   (setq gnus-last-article gnus-article-before-search)
9324   (let ((gnus-last-article gnus-last-article))
9325     (if (gnus-summary-search-article regexp backward)
9326         (gnus-summary-show-thread)
9327       (signal 'search-failed (list regexp)))))
9328
9329 (defun gnus-summary-search-article-backward (regexp)
9330   "Search for an article containing REGEXP backward."
9331   (interactive
9332    (list (read-string
9333           (format "Search article backward (regexp%s): "
9334                   (if gnus-last-search-regexp
9335                       (concat ", default " gnus-last-search-regexp)
9336                     "")))))
9337   (gnus-summary-search-article-forward regexp 'backward))
9338
9339 (defun gnus-summary-search-article (regexp &optional backward)
9340   "Search for an article containing REGEXP.
9341 Optional argument BACKWARD means do search for backward.
9342 `gnus-select-article-hook' is not called during the search."
9343   ;; We have to require this here to make sure that the following
9344   ;; dynamic binding isn't shadowed by autoloading.
9345   (require 'gnus-async)
9346   (require 'gnus-art)
9347   (let ((gnus-select-article-hook nil)  ;Disable hook.
9348         (gnus-article-prepare-hook nil)
9349         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9350         (gnus-use-article-prefetch nil)
9351         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9352         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9353         (gnus-visual nil)
9354         (gnus-keep-backlog nil)
9355         (gnus-break-pages nil)
9356         (gnus-summary-display-arrow nil)
9357         (gnus-updated-mode-lines nil)
9358         (gnus-auto-center-summary nil)
9359         (sum (current-buffer))
9360         (gnus-display-mime-function nil)
9361         (found nil)
9362         point)
9363     (gnus-save-hidden-threads
9364       (gnus-summary-select-article)
9365       (set-buffer gnus-article-buffer)
9366       (goto-char (window-point (get-buffer-window (current-buffer))))
9367       (when backward
9368         (forward-line -1))
9369       (while (not found)
9370         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9371         (if (if backward
9372                 (re-search-backward regexp nil t)
9373               (re-search-forward regexp nil t))
9374             ;; We found the regexp.
9375             (progn
9376               (setq found 'found)
9377               (beginning-of-line)
9378               (set-window-start
9379                (get-buffer-window (current-buffer))
9380                (point))
9381               (forward-line 1)
9382               (set-window-point
9383                (get-buffer-window (current-buffer))
9384                (point))
9385               (set-buffer sum)
9386               (setq point (point)))
9387           ;; We didn't find it, so we go to the next article.
9388           (set-buffer sum)
9389           (setq found 'not)
9390           (while (eq found 'not)
9391             (if (not (if backward (gnus-summary-find-prev)
9392                        (gnus-summary-find-next)))
9393                 ;; No more articles.
9394                 (setq found t)
9395               ;; Select the next article and adjust point.
9396               (unless (gnus-summary-article-sparse-p
9397                        (gnus-summary-article-number))
9398                 (setq found nil)
9399                 (gnus-summary-select-article)
9400                 (set-buffer gnus-article-buffer)
9401                 (widen)
9402                 (goto-char (if backward (point-max) (point-min))))))))
9403       (gnus-message 7 ""))
9404     ;; Return whether we found the regexp.
9405     (when (eq found 'found)
9406       (goto-char point)
9407       (gnus-summary-show-thread)
9408       (gnus-summary-goto-subject gnus-current-article)
9409       (gnus-summary-position-point)
9410       t)))
9411
9412 (defun gnus-find-matching-articles (header regexp)
9413   "Return a list of all articles that match REGEXP on HEADER.
9414 This search includes all articles in the current group that Gnus has
9415 fetched headers for, whether they are displayed or not."
9416   (let ((articles nil)
9417         ;; Can't eta-reduce because it's a macro.
9418         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9419         (case-fold-search t))
9420     (dolist (header gnus-newsgroup-headers)
9421       (when (string-match regexp (funcall func header))
9422         (push (mail-header-number header) articles)))
9423     (nreverse articles)))
9424
9425 (defun gnus-summary-find-matching (header regexp &optional backward unread
9426                                           not-case-fold not-matching)
9427   "Return a list of all articles that match REGEXP on HEADER.
9428 The search stars on the current article and goes forwards unless
9429 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9430 If UNREAD is non-nil, only unread articles will
9431 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9432 in the comparisons. If NOT-MATCHING, return a list of all articles that
9433 not match REGEXP on HEADER."
9434   (let ((case-fold-search (not not-case-fold))
9435         articles d func)
9436     (if (consp header)
9437         (if (eq (car header) 'extra)
9438             (setq func
9439                   `(lambda (h)
9440                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9441                          "")))
9442           (error "%s is an invalid header" header))
9443       (unless (fboundp (intern (concat "mail-header-" header)))
9444         (error "%s is not a valid header" header))
9445       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9446     (dolist (d (if (eq backward 'all)
9447                    gnus-newsgroup-data
9448                  (gnus-data-find-list
9449                   (gnus-summary-article-number)
9450                   (gnus-data-list backward))))
9451       (when (and (or (not unread)       ; We want all articles...
9452                      (gnus-data-unread-p d)) ; Or just unreads.
9453                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9454                  (if not-matching
9455                      (not (string-match
9456                            regexp
9457                            (funcall func (gnus-data-header d))))
9458                    (string-match regexp
9459                                  (funcall func (gnus-data-header d)))))
9460         (push (gnus-data-number d) articles))) ; Success!
9461     (nreverse articles)))
9462
9463 (defun gnus-summary-execute-command (header regexp command &optional backward)
9464   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9465 If HEADER is an empty string (or nil), the match is done on the entire
9466 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9467   (interactive
9468    (list (let ((completion-ignore-case t))
9469            (gnus-completing-read
9470             "Header name"
9471             (mapcar 'symbol-name
9472                     (append
9473                      '(Number Subject From Lines Date
9474                        Message-ID Xref References Body)
9475                      gnus-extra-headers))
9476             'require-match))
9477          (read-string "Regexp: ")
9478          (read-key-sequence "Command: ")
9479          current-prefix-arg))
9480   (when (equal header "Body")
9481     (setq header ""))
9482   ;; Hidden thread subtrees must be searched as well.
9483   (gnus-summary-show-all-threads)
9484   ;; We don't want to change current point nor window configuration.
9485   (save-excursion
9486     (save-window-excursion
9487       (let (gnus-visual
9488             gnus-treat-strip-trailing-blank-lines
9489             gnus-treat-strip-leading-blank-lines
9490             gnus-treat-strip-multiple-blank-lines
9491             gnus-treat-hide-boring-headers
9492             gnus-treat-fold-newsgroups
9493             gnus-article-prepare-hook)
9494         (gnus-message 6 "Executing %s..." (key-description command))
9495         ;; We'd like to execute COMMAND interactively so as to give arguments.
9496         (gnus-execute header regexp
9497                       `(call-interactively ',(key-binding command))
9498                       backward)
9499         (gnus-message 6 "Executing %s...done" (key-description command))))))
9500
9501 (defun gnus-summary-beginning-of-article ()
9502   "Scroll the article back to the beginning."
9503   (interactive)
9504   (gnus-summary-select-article)
9505   (gnus-configure-windows 'article)
9506   (gnus-eval-in-buffer-window gnus-article-buffer
9507     (widen)
9508     (goto-char (point-min))
9509     (when gnus-break-pages
9510       (gnus-narrow-to-page))))
9511
9512 (defun gnus-summary-end-of-article ()
9513   "Scroll to the end of the article."
9514   (interactive)
9515   (gnus-summary-select-article)
9516   (gnus-configure-windows 'article)
9517   (gnus-eval-in-buffer-window gnus-article-buffer
9518     (widen)
9519     (goto-char (point-max))
9520     (recenter -3)
9521     (when gnus-break-pages
9522       (gnus-narrow-to-page))))
9523
9524 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9525   "Truncate to LEN and quote all \"(\"'s in STRING."
9526   (gnus-replace-in-string (if (and len (> (length string) len))
9527                               (substring string 0 len)
9528                             string)
9529                           "[()]" "\\\\\\&"))
9530
9531 (defun gnus-summary-print-article (&optional filename n)
9532   "Generate and print a PostScript image of the process-marked (mail) articles.
9533
9534 If used interactively, print the current article if none are
9535 process-marked.  With prefix arg, prompt the user for the name of the
9536 file to save in.
9537
9538 When used from Lisp, accept two optional args FILENAME and N.  N means
9539 to print the next N articles.  If N is negative, print the N previous
9540 articles.  If N is nil and articles have been marked with the process
9541 mark, print these instead.
9542
9543 If the optional first argument FILENAME is nil, send the image to the
9544 printer.  If FILENAME is a string, save the PostScript image in a file with
9545 that name.  If FILENAME is a number, prompt the user for the name of the file
9546 to save in."
9547   (interactive (list (ps-print-preprint current-prefix-arg)))
9548   (dolist (article (gnus-summary-work-articles n))
9549     (gnus-summary-select-article nil nil 'pseudo article)
9550     (gnus-eval-in-buffer-window gnus-article-buffer
9551       (gnus-print-buffer))
9552     (gnus-summary-remove-process-mark article))
9553   (ps-despool filename))
9554
9555 (defun gnus-print-buffer ()
9556   (let ((ps-left-header
9557          (list
9558           (concat "("
9559                   (gnus-summary-print-truncate-and-quote
9560                    (mail-header-subject gnus-current-headers)
9561                    66) ")")
9562           (concat "("
9563                   (gnus-summary-print-truncate-and-quote
9564                    (mail-header-from gnus-current-headers)
9565                    45) ")")))
9566         (ps-right-header
9567          (list
9568           "/pagenumberstring load"
9569           (concat "("
9570                   (mail-header-date gnus-current-headers) ")"))))
9571     (gnus-run-hooks 'gnus-ps-print-hook)
9572     (save-excursion
9573       (if ps-print-color-p
9574           (ps-spool-buffer-with-faces)
9575         (ps-spool-buffer)))))
9576
9577 (declare-function gnus-flush-original-article-buffer "gnus-art" ())
9578
9579 (defun gnus-summary-show-complete-article ()
9580   "Show a complete version of the current article.
9581 This is only useful if you're looking at a partial version of the
9582 article currently."
9583   (interactive)
9584   (let ((gnus-keep-backlog nil)
9585         (gnus-use-cache nil)
9586         (gnus-agent nil)
9587         (variable (intern
9588                    (format "%s-fetch-partial-articles"
9589                            (car (gnus-find-method-for-group
9590                                  gnus-newsgroup-name)))
9591                    obarray))
9592         old-val)
9593     (unwind-protect
9594         (progn
9595           (setq old-val (symbol-value variable))
9596           (set variable nil)
9597           (gnus-flush-original-article-buffer)
9598           (gnus-summary-show-article))
9599       (set variable old-val))))
9600
9601 (defun gnus-summary-show-article (&optional arg)
9602   "Force redisplaying of the current article.
9603 If ARG (the prefix) is a number, show the article with the charset
9604 defined in `gnus-summary-show-article-charset-alist', or the charset
9605 input.
9606 If ARG (the prefix) is non-nil and not a number, show the article,
9607 but without running any of the article treatment functions
9608 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9609 C-u g', show the raw article."
9610   (interactive "P")
9611   (cond
9612    ((numberp arg)
9613     (gnus-summary-show-article t)
9614     (let ((gnus-newsgroup-charset
9615            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9616                (mm-read-coding-system
9617                 "View as charset: " ;; actually it is coding system.
9618                 (with-current-buffer gnus-article-buffer
9619                   (mm-detect-coding-region (point) (point-max))))))
9620           (gnus-newsgroup-ignored-charsets 'gnus-all))
9621       (gnus-summary-select-article nil 'force)
9622       (let ((deps gnus-newsgroup-dependencies)
9623             head header lines)
9624         (with-current-buffer gnus-original-article-buffer
9625           (save-restriction
9626             (message-narrow-to-head)
9627             (setq head (buffer-string))
9628             (goto-char (point-min))
9629             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9630               (goto-char (point-max))
9631               (widen)
9632               (setq lines (1- (count-lines (point) (point-max))))))
9633           (with-temp-buffer
9634             (insert (format "211 %d Article retrieved.\n"
9635                             (cdr gnus-article-current)))
9636             (insert head)
9637             (if lines (insert (format "Lines: %d\n" lines)))
9638             (insert ".\n")
9639             (let ((nntp-server-buffer (current-buffer)))
9640               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9641         (gnus-data-set-header
9642          (gnus-data-find (cdr gnus-article-current))
9643          header)
9644         (gnus-summary-update-article-line
9645          (cdr gnus-article-current) header)
9646         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9647           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9648    ((not arg)
9649     ;; Select the article the normal way.
9650     (if (eq mm-text-html-renderer 'shr)
9651         (progn
9652           (require 'shr)
9653           (let ((shr-ignore-cache t))
9654             (gnus-summary-select-article nil 'force)))
9655       (gnus-summary-select-article nil 'force)))
9656    ((equal arg '(16))
9657     ;; C-u C-u g
9658     (let ((gnus-inhibit-article-treatments t))
9659       (gnus-summary-select-article nil 'force)))
9660    (t
9661     ;; We have to require this here to make sure that the following
9662     ;; dynamic binding isn't shadowed by autoloading.
9663     (require 'gnus-async)
9664     (require 'gnus-art)
9665     ;; Bind the article treatment functions to nil.
9666     (let ((gnus-have-all-headers t)
9667           gnus-article-prepare-hook
9668           gnus-article-decode-hook
9669           gnus-display-mime-function
9670           gnus-break-pages)
9671       ;; Destroy any MIME parts.
9672       (when (gnus-buffer-live-p gnus-article-buffer)
9673         (with-current-buffer gnus-article-buffer
9674           (gnus-article-stop-animations)
9675           (gnus-stop-downloads)
9676           (mm-destroy-parts gnus-article-mime-handles)
9677           ;; Set it to nil for safety reason.
9678           (setq gnus-article-mime-handle-alist nil)
9679           (setq gnus-article-mime-handles nil)))
9680       (gnus-summary-select-article nil 'force))))
9681   (gnus-summary-goto-subject gnus-current-article)
9682   (gnus-summary-position-point))
9683
9684 (defun gnus-summary-show-raw-article ()
9685   "Show the raw article without any article massaging functions being run."
9686   (interactive)
9687   (gnus-summary-show-article t))
9688
9689 (defun gnus-summary-verbose-headers (&optional arg)
9690   "Toggle permanent full header display.
9691 If ARG is a positive number, turn header display on.
9692 If ARG is a negative number, turn header display off."
9693   (interactive "P")
9694   (setq gnus-show-all-headers
9695         (cond ((or (not (numberp arg))
9696                    (zerop arg))
9697                (not gnus-show-all-headers))
9698               ((natnump arg)
9699                t)))
9700   (gnus-summary-show-article))
9701
9702 (declare-function article-narrow-to-head "gnus-art" ())
9703 (declare-function gnus-article-hidden-text-p "gnus-art" (type))
9704 (declare-function gnus-delete-wash-type "gnus-art" (type))
9705
9706 (defun gnus-summary-toggle-header (&optional arg)
9707   "Show the headers if they are hidden, or hide them if they are shown.
9708 If ARG is a positive number, show the entire header.
9709 If ARG is a negative number, hide the unwanted header lines."
9710   (interactive "P")
9711   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9712                      (get-buffer-window gnus-article-buffer t))))
9713     (with-current-buffer gnus-article-buffer
9714       (widen)
9715       (article-narrow-to-head)
9716       (let* ((inhibit-read-only t)
9717              (inhibit-point-motion-hooks t)
9718              (hidden (if (numberp arg)
9719                          (>= arg 0)
9720                        (or (not (looking-at "[^ \t\n]+:"))
9721                            (gnus-article-hidden-text-p 'headers))))
9722              s e)
9723         (delete-region (point-min) (point-max))
9724         (with-current-buffer gnus-original-article-buffer
9725           (goto-char (setq s (point-min)))
9726           (setq e (if (search-forward "\n\n" nil t)
9727                       (1- (point))
9728                     (point-max))))
9729         (insert-buffer-substring gnus-original-article-buffer s e)
9730         (run-hooks 'gnus-article-decode-hook)
9731         (if hidden
9732             (let ((gnus-treat-hide-headers nil)
9733                   (gnus-treat-hide-boring-headers nil))
9734               (gnus-delete-wash-type 'headers)
9735               (gnus-treat-article 'head))
9736           (gnus-treat-article 'head))
9737         (widen)
9738         (if window
9739             (set-window-start window (goto-char (point-min))))
9740         (if gnus-break-pages
9741             (gnus-narrow-to-page)
9742           (when (gnus-visual-p 'page-marker)
9743             (let ((inhibit-read-only t))
9744               (gnus-remove-text-with-property 'gnus-prev)
9745               (gnus-remove-text-with-property 'gnus-next))))
9746         (gnus-set-mode-line 'article)))))
9747
9748 (defun gnus-summary-show-all-headers ()
9749   "Make all header lines visible."
9750   (interactive)
9751   (gnus-summary-toggle-header 1))
9752
9753 (defun gnus-summary-caesar-message (&optional arg)
9754   "Caesar rotate the current article by 13.
9755 With a non-numerical prefix, also rotate headers.  A numerical
9756 prefix specifies how many places to rotate each letter forward."
9757   (interactive "P")
9758   (gnus-summary-select-article)
9759   (let ((mail-header-separator ""))
9760     (gnus-eval-in-buffer-window gnus-article-buffer
9761       (save-restriction
9762         (widen)
9763         (let ((start (window-start))
9764               (inhibit-read-only t))
9765           (if (equal arg '(4))
9766               (message-caesar-buffer-body nil t)
9767             (message-caesar-buffer-body arg))
9768           (set-window-start (get-buffer-window (current-buffer)) start)))))
9769   ;; Create buttons and stuff...
9770   (gnus-treat-article nil))
9771
9772 (declare-function idna-to-unicode "ext:idna" (str))
9773
9774 (defun gnus-summary-idna-message (&optional arg)
9775   "Decode IDNA encoded domain names in the current articles.
9776 IDNA encoded domain names looks like `xn--bar'.  If a string
9777 remain unencoded after running this function, it is likely an
9778 invalid IDNA string (`xn--bar' is invalid).
9779
9780 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9781 installed for this command to work."
9782   (interactive "P")
9783   (if (not (and (condition-case nil (require 'idna)
9784                   (file-error))
9785                 (mm-coding-system-p 'utf-8)
9786                 (executable-find (symbol-value 'idna-program))))
9787       (gnus-message
9788        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9789     (gnus-summary-select-article)
9790     (let ((mail-header-separator ""))
9791       (gnus-eval-in-buffer-window gnus-article-buffer
9792         (save-restriction
9793           (widen)
9794           (let ((start (window-start))
9795                 buffer-read-only)
9796             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9797               (replace-match (idna-to-unicode (match-string 1))))
9798             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9799
9800 (defun gnus-summary-morse-message (&optional arg)
9801   "Morse decode the current article."
9802   (interactive "P")
9803   (gnus-summary-select-article)
9804   (let ((mail-header-separator ""))
9805     (gnus-eval-in-buffer-window gnus-article-buffer
9806       (save-excursion
9807         (save-restriction
9808           (widen)
9809           (let ((pos (window-start))
9810                 (inhibit-read-only t))
9811             (goto-char (point-min))
9812             (when (message-goto-body)
9813               (gnus-narrow-to-body))
9814             (goto-char (point-min))
9815             (while (search-forward "·" (point-max) t)
9816               (replace-match "."))
9817             (unmorse-region (point-min) (point-max))
9818             (widen)
9819             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9820
9821 (defun gnus-summary-stop-page-breaking ()
9822   "Stop page breaking in the current article."
9823   (interactive)
9824   (gnus-summary-select-article)
9825   (gnus-eval-in-buffer-window gnus-article-buffer
9826     (widen)
9827     (when (gnus-visual-p 'page-marker)
9828       (let ((inhibit-read-only t))
9829         (gnus-remove-text-with-property 'gnus-prev)
9830         (gnus-remove-text-with-property 'gnus-next))
9831       (setq gnus-page-broken nil))))
9832
9833 (defun gnus-summary-move-article (&optional n to-newsgroup
9834                                             select-method action)
9835   "Move the current article to a different newsgroup.
9836 If N is a positive number, move the N next articles.
9837 If N is a negative number, move the N previous articles.
9838 If N is nil and any articles have been marked with the process mark,
9839 move those articles instead.
9840 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9841 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9842 re-spool using this method.
9843
9844 When called interactively with TO-NEWSGROUP being nil, the value of
9845 the variable `gnus-move-split-methods' is used for finding a default
9846 for the target newsgroup.
9847
9848 For this function to work, both the current newsgroup and the
9849 newsgroup that you want to move to have to support the `request-move'
9850 and `request-accept' functions.
9851
9852 ACTION can be either `move' (the default), `crosspost' or `copy'."
9853   (interactive "P")
9854   (unless action
9855     (setq action 'move))
9856   ;; Check whether the source group supports the required functions.
9857   (cond ((and (eq action 'move)
9858               (not (gnus-check-backend-function
9859                     'request-move-article gnus-newsgroup-name)))
9860          (error "The current group does not support article moving"))
9861         ((and (eq action 'crosspost)
9862               (not (gnus-check-backend-function
9863                     'request-replace-article gnus-newsgroup-name)))
9864          (error "The current group does not support article editing")))
9865   (let ((articles (gnus-summary-work-articles n))
9866         (prefix (if (gnus-check-backend-function
9867                      'request-move-article gnus-newsgroup-name)
9868                     (funcall gnus-move-group-prefix-function
9869                              gnus-newsgroup-name)
9870                   ""))
9871         (names '((move "Move" "Moving")
9872                  (copy "Copy" "Copying")
9873                  (crosspost "Crosspost" "Crossposting")))
9874         (copy-buf (save-excursion
9875                     (nnheader-set-temp-buffer " *copy article*")))
9876         art-group to-method new-xref article to-groups
9877         articles-to-update-marks encoded)
9878     (unless (assq action names)
9879       (error "Unknown action %s" action))
9880     ;; Read the newsgroup name.
9881     (when (and (not to-newsgroup)
9882                (not select-method))
9883       (if (and gnus-move-split-methods
9884                (not
9885                 (and (memq gnus-current-article articles)
9886                      (gnus-buffer-live-p gnus-original-article-buffer))))
9887           ;; When `gnus-move-split-methods' is non-nil, we have to
9888           ;; select an article to give `gnus-read-move-group-name' an
9889           ;; opportunity to suggest an appropriate default.  However,
9890           ;; we needn't render or mark the article.
9891           (let ((gnus-display-mime-function nil)
9892                 (gnus-article-prepare-hook nil)
9893                 (gnus-mark-article-hook nil))
9894             (gnus-summary-select-article nil nil nil (car articles))))
9895       (setq to-newsgroup (gnus-read-move-group-name
9896                           (cadr (assq action names))
9897                           (symbol-value
9898                            (intern (format "gnus-current-%s-group" action)))
9899                           articles prefix)
9900             encoded to-newsgroup
9901             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9902       (set (intern (format "gnus-current-%s-group" action))
9903            (mm-decode-coding-string
9904             to-newsgroup
9905             (gnus-group-name-charset to-method to-newsgroup))))
9906     (unless to-method
9907       (setq to-method (or select-method
9908                           (gnus-server-to-method
9909                            (gnus-group-method to-newsgroup)))))
9910     (setq to-newsgroup
9911           (or encoded
9912               (and to-newsgroup
9913                    (mm-encode-coding-string
9914                     to-newsgroup
9915                     (gnus-group-name-charset to-method to-newsgroup)))))
9916     ;; Check the method we are to move this article to...
9917     (unless (gnus-check-backend-function
9918              'request-accept-article (car to-method))
9919       (error "%s does not support article copying" (car to-method)))
9920     (unless (gnus-check-server to-method)
9921       (error "Can't open server %s" (car to-method)))
9922     (gnus-message 6 "%s to %s: %s..."
9923                   (caddr (assq action names))
9924                   (or (car select-method)
9925                       (gnus-group-decoded-name to-newsgroup))
9926                   articles)
9927     (while articles
9928       (setq article (pop articles))
9929       ;; Set any marks that may have changed in the summary buffer.
9930       (when gnus-preserve-marks
9931         (gnus-summary-push-marks-to-backend article))
9932       (setq
9933        art-group
9934        (cond
9935         ;; Move the article.
9936         ((eq action 'move)
9937          ;; Remove this article from future suppression.
9938          (gnus-dup-unsuppress-article article)
9939          (let* ((from-method (gnus-find-method-for-group
9940                               gnus-newsgroup-name))
9941                 (to-method (or select-method
9942                                (gnus-find-method-for-group to-newsgroup)))
9943                 (move-is-internal (gnus-server-equal from-method to-method)))
9944            (gnus-request-move-article
9945             article                     ; Article to move
9946             gnus-newsgroup-name         ; From newsgroup
9947             (nth 1 (gnus-find-method-for-group
9948                     gnus-newsgroup-name)) ; Server
9949             (list 'gnus-request-accept-article
9950                   to-newsgroup (list 'quote select-method)
9951                   (not articles) t)     ; Accept form
9952             (not articles)              ; Only save nov last time
9953             (and move-is-internal
9954                  to-newsgroup           ; Not respooling
9955                                         ; Is this move internal?
9956                  (gnus-group-real-name to-newsgroup)))))
9957         ;; Copy the article.
9958         ((eq action 'copy)
9959          (with-current-buffer copy-buf
9960            (when (gnus-request-article-this-buffer article
9961                                                    gnus-newsgroup-name)
9962              (save-restriction
9963                (nnheader-narrow-to-headers)
9964                (dolist (hdr gnus-copy-article-ignored-headers)
9965                  (message-remove-header hdr t)))
9966              (gnus-request-accept-article
9967               to-newsgroup select-method (not articles) t))))
9968         ;; Crosspost the article.
9969         ((eq action 'crosspost)
9970          (let ((xref (message-tokenize-header
9971                       (mail-header-xref (gnus-summary-article-header
9972                                          article))
9973                       " ")))
9974            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9975                                   ":" (number-to-string article)))
9976            (unless xref
9977              (setq xref (list (system-name))))
9978            (setq new-xref
9979                  (concat
9980                   (mapconcat 'identity
9981                              (delete "Xref:" (delete new-xref xref))
9982                              " ")
9983                   " " new-xref))
9984            (with-current-buffer copy-buf
9985              ;; First put the article in the destination group.
9986              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9987              (when (consp (setq art-group
9988                                 (gnus-request-accept-article
9989                                  to-newsgroup select-method (not articles)
9990                                  t)))
9991                (setq new-xref (concat new-xref " " (car art-group)
9992                                       ":"
9993                                       (number-to-string (cdr art-group))))
9994                ;; Now we have the new Xrefs header, so we insert
9995                ;; it and replace the new article.
9996                (nnheader-replace-header "Xref" new-xref)
9997                (gnus-request-replace-article
9998                 (cdr art-group) to-newsgroup (current-buffer) t)
9999                art-group))))))
10000       (cond
10001        ((not art-group)
10002         (gnus-message 1 "Couldn't %s article %s: %s"
10003                       (cadr (assq action names)) article
10004                       (nnheader-get-report (car to-method))))
10005        ((eq art-group 'junk)
10006         (when (eq action 'move)
10007           (gnus-summary-mark-article article gnus-canceled-mark)
10008           (gnus-message 4 "Deleted article %s" article)
10009           ;; run the delete hook
10010           (run-hook-with-args 'gnus-summary-article-delete-hook
10011                               action
10012                               (gnus-data-header
10013                                (assoc article (gnus-data-list nil)))
10014                               gnus-newsgroup-name nil
10015                               select-method)))
10016        (t
10017         (let* ((pto-group (gnus-group-prefixed-name
10018                            (car art-group) to-method))
10019                (info (gnus-get-info pto-group))
10020                (to-group (gnus-info-group info))
10021                to-marks)
10022           ;; Update the group that has been moved to.
10023           (when (and info
10024                      (memq action '(move copy)))
10025             (unless (member to-group to-groups)
10026               (push to-group to-groups))
10027
10028             (when (and (not (memq article gnus-newsgroup-unreads))
10029                        (cdr art-group))
10030               (push 'read to-marks)
10031               (gnus-info-set-read
10032                info (gnus-add-to-range (gnus-info-read info)
10033                                        (list (cdr art-group)))))
10034
10035             ;; See whether the article is to be put in the cache.
10036             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10037                    (marks (if expirable
10038                               gnus-article-mark-lists
10039                             (delete '(expirable . expire)
10040                                     (copy-sequence
10041                                      gnus-article-mark-lists))))
10042                    (to-article (cdr art-group)))
10043
10044               ;; Enter the article into the cache in the new group,
10045               ;; if that is required.
10046               (when (and to-article
10047                          gnus-use-cache)
10048                 (gnus-cache-possibly-enter-article
10049                  to-group to-article
10050                  (memq article gnus-newsgroup-marked)
10051                  (memq article gnus-newsgroup-dormant)
10052                  (memq article gnus-newsgroup-unreads)))
10053
10054               (when (and gnus-preserve-marks
10055                          to-article)
10056                 ;; Copy any marks over to the new group.
10057                 (when (and (equal to-group gnus-newsgroup-name)
10058                            (not (memq article gnus-newsgroup-unreads)))
10059                   ;; Mark this article as read in this group.
10060                   (push (cons to-article gnus-read-mark)
10061                         gnus-newsgroup-reads)
10062                   ;; Increase the active status of this group.
10063                   (setcdr (gnus-active to-group) to-article)
10064                   (setcdr gnus-newsgroup-active to-article))
10065
10066                 (while marks
10067                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10068                     (when (memq article (symbol-value
10069                                          (intern (format "gnus-newsgroup-%s"
10070                                                          (caar marks)))))
10071                       (push (cdar marks) to-marks)
10072                       ;; If the other group is the same as this group,
10073                       ;; then we have to add the mark to the list.
10074                       (when (equal to-group gnus-newsgroup-name)
10075                         (set (intern (format "gnus-newsgroup-%s"
10076                                              (caar marks)))
10077                              (cons to-article
10078                                    (symbol-value
10079                                     (intern (format "gnus-newsgroup-%s"
10080                                                     (caar marks)))))))
10081                       ;; Copy the marks to other group.
10082                       (gnus-add-marked-articles
10083                        to-group (cdar marks) (list to-article) info)))
10084                   (setq marks (cdr marks)))
10085
10086                 (when (and expirable
10087                            gnus-mark-copied-or-moved-articles-as-expirable
10088                            (not (memq 'expire to-marks)))
10089                   ;; Mark this article as expirable.
10090                   (push 'expire to-marks)
10091                   (when (equal to-group gnus-newsgroup-name)
10092                     (push to-article gnus-newsgroup-expirable))
10093                   ;; Copy the expirable mark to other group.
10094                   (gnus-add-marked-articles
10095                    to-group 'expire (list to-article) info))
10096
10097                 (when (and to-marks
10098                            (gnus-method-option-p
10099                             (gnus-find-method-for-group to-group)
10100                             'server-marks))
10101                   (gnus-request-set-mark
10102                    to-group (list (list (list to-article) 'add to-marks)))))
10103
10104               (gnus-dribble-enter
10105                (concat "(gnus-group-set-info '"
10106                        (gnus-prin1-to-string (gnus-get-info to-group))
10107                        ")")
10108                (concat "^(gnus-group-set-info '(\""
10109                        (regexp-quote to-group) "\""))))
10110
10111           ;; Update the Xref header in this article to point to
10112           ;; the new crossposted article we have just created.
10113           (when (eq action 'crosspost)
10114             (with-current-buffer copy-buf
10115               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10116               (nnheader-replace-header "Xref" new-xref)
10117               (gnus-request-replace-article
10118                article gnus-newsgroup-name (current-buffer) t)))
10119
10120           ;; run the move/copy/crosspost/respool hook
10121           (run-hook-with-args 'gnus-summary-article-move-hook
10122                               action
10123                               (gnus-data-header
10124                                (assoc article (gnus-data-list nil)))
10125                               gnus-newsgroup-name
10126                               to-newsgroup
10127                               select-method))
10128
10129         ;;;!!!Why is this necessary?
10130         (set-buffer gnus-summary-buffer)
10131
10132         (when (eq action 'move)
10133           (save-excursion
10134             (gnus-summary-goto-subject article)
10135             (gnus-summary-mark-article article gnus-canceled-mark)))))
10136       (push article articles-to-update-marks))
10137
10138     (save-excursion
10139       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10140     ;; Re-activate all groups that have been moved to.
10141     (with-current-buffer gnus-group-buffer
10142       (let ((gnus-group-marked to-groups))
10143         (gnus-group-get-new-news-this-group nil t)))
10144
10145     (gnus-kill-buffer copy-buf)
10146     (gnus-summary-position-point)
10147     (gnus-set-mode-line 'summary)))
10148
10149 (defun gnus-summary-push-marks-to-backend (article)
10150   (let ((set nil)
10151         (del nil)
10152         (marks gnus-article-mark-lists))
10153     (unless (memq article gnus-newsgroup-unreads)
10154       (push 'read set))
10155     (while marks
10156       (if (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10157                (memq article (symbol-value
10158                               (intern (format "gnus-newsgroup-%s"
10159                                               (caar marks))))))
10160           (push (cdar marks) set)
10161         (push (cdar marks) del))
10162       (pop marks))
10163     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)
10164                                                  ((,article) del ,del)))))
10165
10166 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10167   "Copy the current article to some other group.
10168 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10169 When called interactively, if TO-NEWSGROUP is nil, use the value of
10170 the variable `gnus-move-split-methods' for finding a default target
10171 newsgroup.
10172 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10173 re-spool using this method."
10174   (interactive "P")
10175   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10176
10177 (defun gnus-summary-crosspost-article (&optional n)
10178   "Crosspost the current article to some other group."
10179   (interactive "P")
10180   (gnus-summary-move-article n nil nil 'crosspost))
10181
10182 (defcustom gnus-summary-respool-default-method nil
10183   "Default method type for respooling an article.
10184 If nil, use to the current newsgroup method."
10185   :type 'symbol
10186   :group 'gnus-summary-mail)
10187
10188 (defun gnus-summary-respool-article (&optional n method)
10189   "Respool the current article.
10190 The article will be squeezed through the mail spooling process again,
10191 which means that it will be put in some mail newsgroup or other
10192 depending on `nnmail-split-methods'.
10193 If N is a positive number, respool the N next articles.
10194 If N is a negative number, respool the N previous articles.
10195 If N is nil and any articles have been marked with the process mark,
10196 respool those articles instead.
10197
10198 Respooling can be done both from mail groups and \"real\" newsgroups.
10199 In the former case, the articles in question will be moved from the
10200 current group into whatever groups they are destined to.  In the
10201 latter case, they will be copied into the relevant groups."
10202   (interactive
10203    (list current-prefix-arg
10204          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10205                 (methname
10206                  (symbol-name (or gnus-summary-respool-default-method
10207                                   (car (gnus-find-method-for-group
10208                                         gnus-newsgroup-name)))))
10209                 (method
10210                  (gnus-completing-read
10211                   "Backend to use when respooling"
10212                   methods t nil 'gnus-mail-method-history methname))
10213                 ms)
10214            (cond
10215             ((zerop (length (setq ms (gnus-servers-using-backend
10216                                       (intern method)))))
10217              (list (intern method) ""))
10218             ((= 1 (length ms))
10219              (car ms))
10220             (t
10221              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10222                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10223                            ms-alist))))))))
10224   (unless method
10225     (error "No method given for respooling"))
10226   (if (assoc (symbol-name
10227               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10228              (gnus-methods-using 'respool))
10229       (gnus-summary-move-article n nil method)
10230     (gnus-summary-copy-article n nil method)))
10231
10232 (defun gnus-summary-import-article (file &optional edit)
10233   "Import an arbitrary file into a mail newsgroup."
10234   (interactive "fImport file: \nP")
10235   (let ((group gnus-newsgroup-name)
10236         (now (current-time))
10237         atts lines group-art)
10238     (unless (gnus-check-backend-function 'request-accept-article group)
10239       (error "%s does not support article importing" group))
10240     (or (file-readable-p file)
10241         (not (file-regular-p file))
10242         (error "Can't read %s" file))
10243     (with-current-buffer (gnus-get-buffer-create " *import file*")
10244       (erase-buffer)
10245       (nnheader-insert-file-contents file)
10246       (goto-char (point-min))
10247       (if (nnheader-article-p)
10248           (save-restriction
10249             (goto-char (point-min))
10250             (search-forward "\n\n" nil t)
10251             (narrow-to-region (point-min) (1- (point)))
10252             (goto-char (point-min))
10253             (unless (re-search-forward "^date:" nil t)
10254               (goto-char (point-max))
10255               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10256        ;; This doesn't look like an article, so we fudge some headers.
10257         (setq atts (file-attributes file)
10258               lines (count-lines (point-min) (point-max)))
10259         (insert "From: " (read-string "From: ") "\n"
10260                 "Subject: " (read-string "Subject: ") "\n"
10261                 "Date: " (message-make-date (nth 5 atts)) "\n"
10262                 "Message-ID: " (message-make-message-id) "\n"
10263                 "Lines: " (int-to-string lines) "\n"
10264                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10265       (setq group-art (gnus-request-accept-article group nil t))
10266       (kill-buffer (current-buffer)))
10267     (setq gnus-newsgroup-active (gnus-activate-group group))
10268     (forward-line 1)
10269     (gnus-summary-goto-article (cdr group-art) nil t)
10270     (when edit
10271       (gnus-summary-edit-article))))
10272
10273 (defun gnus-summary-create-article ()
10274   "Create an article in a mail newsgroup."
10275   (interactive)
10276   (let ((group gnus-newsgroup-name)
10277         (now (current-time))
10278         group-art)
10279     (unless (gnus-check-backend-function 'request-accept-article group)
10280       (error "%s does not support article importing" group))
10281     (with-current-buffer (gnus-get-buffer-create " *import file*")
10282       (erase-buffer)
10283       (goto-char (point-min))
10284       ;; This doesn't look like an article, so we fudge some headers.
10285       (insert "From: " (read-string "From: ") "\n"
10286               "Subject: " (read-string "Subject: ") "\n"
10287               "Date: " (message-make-date now) "\n"
10288               "Message-ID: " (message-make-message-id) "\n")
10289       (setq group-art (gnus-request-accept-article group nil t))
10290       (kill-buffer (current-buffer)))
10291     (setq gnus-newsgroup-active (gnus-activate-group group))
10292     (forward-line 1)
10293     (gnus-summary-goto-article (cdr group-art) nil t)
10294     (gnus-summary-edit-article)))
10295
10296 (defun gnus-summary-article-posted-p ()
10297   "Say whether the current (mail) article is available from news as well.
10298 This will be the case if the article has both been mailed and posted."
10299   (interactive)
10300   (let ((id (mail-header-references (gnus-summary-article-header)))
10301         (gnus-override-method (car (gnus-refer-article-methods))))
10302     (if (gnus-request-head id "")
10303         (gnus-message 2 "The current message was found on %s"
10304                       gnus-override-method)
10305       (gnus-message 2 "The current message couldn't be found on %s"
10306                     gnus-override-method)
10307       nil)))
10308
10309 (defun gnus-summary-expire-articles (&optional now)
10310   "Expire all articles that are marked as expirable in the current group."
10311   (interactive)
10312   (when (and (not gnus-group-is-exiting-without-update-p)
10313              (gnus-check-backend-function
10314               'request-expire-articles gnus-newsgroup-name))
10315     ;; This backend supports expiry.
10316     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10317            (expirable
10318             (gnus-list-range-difference
10319              (if total
10320                  (progn
10321                    ;; We need to update the info for
10322                    ;; this group for `gnus-list-of-read-articles'
10323                    ;; to give us the right answer.
10324                    (gnus-run-hooks 'gnus-exit-group-hook)
10325                    (gnus-summary-update-info)
10326                    (gnus-list-of-read-articles gnus-newsgroup-name))
10327                (setq gnus-newsgroup-expirable
10328                      (sort gnus-newsgroup-expirable '<)))
10329              gnus-newsgroup-unexist))
10330            (expiry-wait (if now 'immediate
10331                           (gnus-group-find-parameter
10332                            gnus-newsgroup-name 'expiry-wait)))
10333            (nnmail-expiry-target
10334             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10335                 nnmail-expiry-target))
10336            es)
10337       (when expirable
10338         ;; There are expirable articles in this group, so we run them
10339         ;; through the expiry process.
10340         (gnus-message 6 "Expiring articles...")
10341         (when (gnus-check-group gnus-newsgroup-name)
10342           ;; The list of articles that weren't expired is returned.
10343           (save-excursion
10344             (if expiry-wait
10345                 (let ((nnmail-expiry-wait-function nil)
10346                       (nnmail-expiry-wait expiry-wait))
10347                   (setq es (gnus-request-expire-articles
10348                             expirable gnus-newsgroup-name)))
10349               (setq es (gnus-request-expire-articles
10350                         expirable gnus-newsgroup-name)))
10351             (unless total
10352               (setq gnus-newsgroup-expirable es))
10353             ;; We go through the old list of expirable, and mark all
10354             ;; really expired articles as nonexistent.
10355             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10356               (let ((gnus-use-cache nil))
10357                 (dolist (article expirable)
10358                   (when (and (not (memq article es))
10359                              (gnus-data-find article))
10360                     (gnus-summary-mark-article article gnus-canceled-mark)
10361                     (run-hook-with-args 'gnus-summary-article-expire-hook
10362                                         'delete
10363                                         (gnus-data-header
10364                                          (assoc article (gnus-data-list nil)))
10365                                         gnus-newsgroup-name
10366                                         nil
10367                                         nil)))))))
10368         (gnus-message 6 "Expiring articles...done")))))
10369
10370 (defun gnus-summary-expire-articles-now ()
10371   "Expunge all expirable articles in the current group.
10372 This means that *all* articles that are marked as expirable will be
10373 deleted forever, right now."
10374   (interactive)
10375   (or gnus-expert-user
10376       (gnus-yes-or-no-p
10377        "Are you really, really sure you want to delete all expirable messages? ")
10378       (error "Phew!"))
10379   (gnus-summary-expire-articles t))
10380
10381 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10382 (defun gnus-summary-delete-article (&optional n)
10383   "Delete the N next (mail) articles.
10384 This command actually deletes articles.  This is not a marking
10385 command.  The article will disappear forever from your life, never to
10386 return.
10387
10388 If N is negative, delete backwards.
10389 If N is nil and articles have been marked with the process mark,
10390 delete these instead.
10391
10392 If `gnus-novice-user' is non-nil you will be asked for
10393 confirmation before the articles are deleted."
10394   (interactive "P")
10395   (unless (gnus-check-backend-function 'request-expire-articles
10396                                        gnus-newsgroup-name)
10397     (error "The current newsgroup does not support article deletion"))
10398   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10399     (error "Couldn't open server"))
10400   ;; Compute the list of articles to delete.
10401   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10402         (nnmail-expiry-target 'delete)
10403         not-deleted)
10404     (if (and gnus-novice-user
10405              (not (gnus-yes-or-no-p
10406                    (format "Do you really want to delete %s forever? "
10407                            (if (> (length articles) 1)
10408                                (format "these %s articles" (length articles))
10409                              "this article")))))
10410         ()
10411       ;; Delete the articles.
10412       (setq not-deleted (gnus-request-expire-articles
10413                          articles gnus-newsgroup-name 'force))
10414       (save-excursion
10415         (while articles
10416           (gnus-summary-remove-process-mark (car articles))
10417           ;; The backend might not have been able to delete the article
10418           ;; after all.
10419           (unless (memq (car articles) not-deleted)
10420             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10421             (let* ((article (car articles))
10422                    (ghead  (gnus-data-header
10423                             (assoc article (gnus-data-list nil)))))
10424               (run-hook-with-args 'gnus-summary-article-delete-hook
10425                                   'delete ghead gnus-newsgroup-name nil
10426                                   nil)))
10427           (setq articles (cdr articles))))
10428       (when not-deleted
10429         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10430     (gnus-summary-position-point)
10431     (gnus-set-mode-line 'summary)
10432     not-deleted))
10433
10434 (defun gnus-summary-edit-article (&optional arg)
10435   "Edit the current article.
10436 This will have permanent effect only in mail groups.
10437 If ARG is nil, edit the decoded articles.
10438 If ARG is 1, edit the raw articles.
10439 If ARG is 2, edit the raw articles even in read-only groups.
10440 If ARG is 3, edit the articles with the current handles.
10441 Otherwise, allow editing of articles even in read-only
10442 groups."
10443   (interactive "P")
10444   (let (force raw current-handles)
10445     (cond
10446      ((null arg))
10447      ((eq arg 1)
10448       (setq raw t))
10449      ((eq arg 2)
10450       (setq raw t
10451             force t))
10452      ((eq arg 3)
10453       (setq current-handles
10454             (and (gnus-buffer-live-p gnus-article-buffer)
10455                  (with-current-buffer gnus-article-buffer
10456                    (prog1
10457                        gnus-article-mime-handles
10458                      (setq gnus-article-mime-handles nil))))))
10459      (t
10460       (setq force t)))
10461     (when (and raw (not force)
10462                (member gnus-newsgroup-name '("nndraft:delayed"
10463                                              "nndraft:drafts"
10464                                              "nndraft:queue")))
10465       (error "Can't edit the raw article in group %s"
10466              gnus-newsgroup-name))
10467     (with-current-buffer gnus-summary-buffer
10468       (let ((mail-parse-charset gnus-newsgroup-charset)
10469             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10470         (gnus-set-global-variables)
10471         (when (and (not force)
10472                    (gnus-group-read-only-p))
10473           (error "The current newsgroup does not support article editing"))
10474         (gnus-summary-show-article t)
10475         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10476           (with-current-buffer gnus-article-buffer
10477             (mm-enable-multibyte)))
10478         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10479             (setq raw t))
10480         (gnus-article-edit-article
10481          (if raw 'ignore
10482            `(lambda ()
10483               (let ((mbl mml-buffer-list))
10484                 (setq mml-buffer-list nil)
10485                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10486                   (mime-to-mml ,'current-handles))
10487                 (let ((mbl1 mml-buffer-list))
10488                   (setq mml-buffer-list mbl)
10489                   (set (make-local-variable 'mml-buffer-list) mbl1))
10490                 (gnus-make-local-hook 'kill-buffer-hook)
10491                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10492          `(lambda (no-highlight)
10493             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10494                   (message-options message-options)
10495                   (message-options-set-recipient)
10496                   (mail-parse-ignored-charsets
10497                    ',gnus-newsgroup-ignored-charsets)
10498                   (rfc2047-header-encoding-alist
10499                    ',(let ((charset (gnus-group-name-charset
10500                                      (gnus-find-method-for-group
10501                                       gnus-newsgroup-name)
10502                                      gnus-newsgroup-name)))
10503                        (append (list (cons "Newsgroups" charset)
10504                                      (cons "Followup-To" charset)
10505                                      (cons "Xref" charset))
10506                                rfc2047-header-encoding-alist))))
10507               ,(if (not raw) '(progn
10508                                 (mml-to-mime)
10509                                 (mml-destroy-buffers)
10510                                 (remove-hook 'kill-buffer-hook
10511                                              'mml-destroy-buffers t)
10512                                 (kill-local-variable 'mml-buffer-list)))
10513               (gnus-summary-edit-article-done
10514                ,(or (mail-header-references gnus-current-headers) "")
10515                ,(gnus-group-read-only-p)
10516                ,gnus-summary-buffer no-highlight))))))))
10517
10518 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10519
10520 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10521                                                  no-highlight)
10522   "Make edits to the current article permanent."
10523   (interactive)
10524   (save-excursion
10525     ;; The buffer restriction contains the entire article if it exists.
10526     (when (article-goto-body)
10527       (let ((lines (count-lines (point) (point-max)))
10528             (length (- (point-max) (point)))
10529             (case-fold-search t)
10530             (body (copy-marker (point))))
10531         (goto-char (point-min))
10532         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10533           (delete-region (match-beginning 1) (match-end 1))
10534           (insert (number-to-string length)))
10535         (goto-char (point-min))
10536         (when (re-search-forward
10537                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10538           (delete-region (match-beginning 1) (match-end 1))
10539           (insert (number-to-string length)))
10540         (goto-char (point-min))
10541         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10542           (delete-region (match-beginning 1) (match-end 1))
10543           (insert (number-to-string lines))))))
10544   ;; Replace the article.
10545   (let ((buf (current-buffer))
10546         (article (cdr gnus-article-current))
10547         replace-result)
10548     (with-temp-buffer
10549       (insert-buffer-substring buf)
10550       (if (and (not read-only)
10551                (not (setq replace-result
10552                           (gnus-request-replace-article
10553                            article (car gnus-article-current)
10554                            (current-buffer) t))))
10555           (error "Couldn't replace article")
10556         ;; If we got a number back, then that's the new article number
10557         ;; for this article.  Otherwise, the article number didn't change.
10558         (when (numberp replace-result)
10559           (with-current-buffer gnus-summary-buffer
10560             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10561             (gnus-summary-limit gnus-newsgroup-limit)
10562             (setq article replace-result)
10563             (gnus-summary-goto-subject article t)))
10564         ;; Update the summary buffer.
10565         (if (and references
10566                  (equal (message-tokenize-header references " ")
10567                         (message-tokenize-header
10568                          (or (message-fetch-field "references") "") " ")))
10569             ;; We only have to update this line.
10570             (save-excursion
10571               (save-restriction
10572                 (message-narrow-to-head)
10573                 (let ((head (buffer-substring-no-properties
10574                              (point-min) (point-max)))
10575                       header)
10576                   (with-temp-buffer
10577                     (insert (format "211 %d Article retrieved.\n" article))
10578                     (insert head)
10579                     (insert ".\n")
10580                     (let ((nntp-server-buffer (current-buffer)))
10581                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10582                     (with-current-buffer gnus-summary-buffer
10583                       (gnus-data-set-header (gnus-data-find article) header)
10584                       (gnus-summary-update-article-line article header)
10585                       (if (gnus-summary-goto-subject article nil t)
10586                           (gnus-summary-update-secondary-mark article)))))))
10587           ;; Update threads.
10588           (set-buffer (or buffer gnus-summary-buffer))
10589           (gnus-summary-update-article article)
10590           (if (gnus-summary-goto-subject article nil t)
10591               (gnus-summary-update-secondary-mark article)))
10592         ;; Prettify the article buffer again.
10593         (unless no-highlight
10594           (with-current-buffer gnus-article-buffer
10595             ;;!!! Fix this -- article should be rehighlighted.
10596             ;;(gnus-run-hooks 'gnus-article-display-hook)
10597             (set-buffer gnus-original-article-buffer)
10598             (gnus-request-article
10599              article (car gnus-article-current) (current-buffer))))
10600         ;; Prettify the summary buffer line.
10601         (when (gnus-visual-p 'summary-highlight 'highlight)
10602           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10603
10604 (defun gnus-summary-edit-wash (key)
10605   "Perform editing command KEY in the article buffer."
10606   (interactive
10607    (list
10608     (progn
10609       (message "%s" (concat (this-command-keys) "- "))
10610       (read-char))))
10611   (message "")
10612   (gnus-summary-edit-article)
10613   (execute-kbd-macro (concat (this-command-keys) key))
10614   (gnus-article-edit-done))
10615
10616 ;;; Respooling
10617
10618 (defun gnus-summary-respool-query (&optional silent trace)
10619   "Query where the respool algorithm would put this article."
10620   (interactive)
10621   (let (gnus-mark-article-hook)
10622     (gnus-summary-select-article)
10623     (with-current-buffer gnus-original-article-buffer
10624       (let ((groups (nnmail-article-group 'identity trace)))
10625         (unless silent
10626           (if groups
10627               (message "This message would go to %s"
10628                        (mapconcat 'car groups ", "))
10629             (message "This message would go to no groups"))
10630           groups)))))
10631
10632 (defun gnus-summary-respool-trace ()
10633   "Trace where the respool algorithm would put this article.
10634 Display a buffer showing all fancy splitting patterns which matched."
10635   (interactive)
10636   (gnus-summary-respool-query nil t))
10637
10638 ;; Summary marking commands.
10639
10640 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10641   "Mark articles which has the same subject as read, and then select the next.
10642 If UNMARK is positive, remove any kind of mark.
10643 If UNMARK is negative, tick articles."
10644   (interactive "P")
10645   (when unmark
10646     (setq unmark (prefix-numeric-value unmark)))
10647   (let ((count
10648          (gnus-summary-mark-same-subject
10649           (gnus-summary-article-subject) unmark)))
10650     ;; Select next unread article.  If auto-select-same mode, should
10651     ;; select the first unread article.
10652     (gnus-summary-next-article t (and gnus-auto-select-same
10653                                       (gnus-summary-article-subject)))
10654     (gnus-message 7 "%d article%s marked as %s"
10655                   count (if (= count 1) " is" "s are")
10656                   (if unmark "unread" "read"))))
10657
10658 (defun gnus-summary-kill-same-subject (&optional unmark)
10659   "Mark articles which has the same subject as read.
10660 If UNMARK is positive, remove any kind of mark.
10661 If UNMARK is negative, tick articles."
10662   (interactive "P")
10663   (when unmark
10664     (setq unmark (prefix-numeric-value unmark)))
10665   (let ((count
10666          (gnus-summary-mark-same-subject
10667           (gnus-summary-article-subject) unmark)))
10668     ;; If marked as read, go to next unread subject.
10669     (when (null unmark)
10670       ;; Go to next unread subject.
10671       (gnus-summary-next-subject 1 t))
10672     (gnus-message 7 "%d articles are marked as %s"
10673                   count (if unmark "unread" "read"))))
10674
10675 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10676   "Mark articles with same SUBJECT as read, and return marked number.
10677 If optional argument UNMARK is positive, remove any kinds of marks.
10678 If optional argument UNMARK is negative, mark articles as unread instead."
10679   (let ((count 1))
10680     (save-excursion
10681       (cond
10682        ((null unmark)                   ; Mark as read.
10683         (while (and
10684                 (progn
10685                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10686                   (gnus-summary-show-thread) t)
10687                 (gnus-summary-find-subject subject))
10688           (setq count (1+ count))))
10689        ((> unmark 0)                    ; Tick.
10690         (while (and
10691                 (progn
10692                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10693                   (gnus-summary-show-thread) t)
10694                 (gnus-summary-find-subject subject))
10695           (setq count (1+ count))))
10696        (t                               ; Mark as unread.
10697         (while (and
10698                 (progn
10699                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10700                   (gnus-summary-show-thread) t)
10701                 (gnus-summary-find-subject subject))
10702           (setq count (1+ count)))))
10703       (gnus-set-mode-line 'summary)
10704       ;; Return the number of marked articles.
10705       count)))
10706
10707 (defun gnus-summary-mark-as-processable (n &optional unmark)
10708   "Set the process mark on the next N articles.
10709 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10710 the process mark instead.  The difference between N and the actual
10711 number of articles marked is returned."
10712   (interactive "P")
10713   (if (and (null n) (gnus-region-active-p))
10714       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10715     (setq n (prefix-numeric-value n))
10716     (let ((backward (< n 0))
10717           (n (abs n)))
10718       (while (and
10719               (> n 0)
10720               (if unmark
10721                   (gnus-summary-remove-process-mark
10722                    (gnus-summary-article-number))
10723                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10724               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10725         (setq n (1- n)))
10726       (when (/= 0 n)
10727         (gnus-message 7 "No more articles"))
10728       (gnus-summary-recenter)
10729       (gnus-summary-position-point)
10730       n)))
10731
10732 (defun gnus-summary-unmark-as-processable (n)
10733   "Remove the process mark from the next N articles.
10734 If N is negative, unmark backward instead.  The difference between N and
10735 the actual number of articles unmarked is returned."
10736   (interactive "P")
10737   (gnus-summary-mark-as-processable n t))
10738
10739 (defun gnus-summary-unmark-all-processable ()
10740   "Remove the process mark from all articles."
10741   (interactive)
10742   (save-excursion
10743     (while gnus-newsgroup-processable
10744       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10745   (gnus-summary-position-point))
10746
10747 (defun gnus-summary-add-mark (article type)
10748   "Mark ARTICLE with a mark of TYPE."
10749   (let ((vtype (car (assq type gnus-article-mark-lists)))
10750         var)
10751     (if (not vtype)
10752         (error "No such mark type: %s" type)
10753       (setq var (intern (format "gnus-newsgroup-%s" type)))
10754       (set var (cons article (symbol-value var)))
10755       (if (memq type '(processable cached replied forwarded recent saved))
10756           (gnus-summary-update-secondary-mark article)
10757         ;;; !!! This is bogus.  We should find out what primary
10758         ;;; !!! mark we want to set.
10759         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10760
10761 (defun gnus-summary-mark-as-expirable (n)
10762   "Mark N articles forward as expirable.
10763 If N is negative, mark backward instead.  The difference between N and
10764 the actual number of articles marked is returned."
10765   (interactive "p")
10766   (gnus-summary-mark-forward n gnus-expirable-mark))
10767
10768 (defun gnus-summary-mark-as-spam (n)
10769   "Mark N articles forward as spam.
10770 If N is negative, mark backward instead.  The difference between N and
10771 the actual number of articles marked is returned."
10772   (interactive "p")
10773   (gnus-summary-mark-forward n gnus-spam-mark))
10774
10775 (defun gnus-summary-mark-article-as-replied (article)
10776   "Mark ARTICLE as replied to and update the summary line.
10777 ARTICLE can also be a list of articles."
10778   (interactive (list (gnus-summary-article-number)))
10779   (let ((articles (if (listp article) article (list article))))
10780     (dolist (article articles)
10781       (unless (numberp article)
10782         (error "%s is not a number" article))
10783       (push article gnus-newsgroup-replied)
10784       (let ((inhibit-read-only t))
10785         (when (gnus-summary-goto-subject article nil t)
10786           (gnus-summary-update-secondary-mark article))))))
10787
10788 (defun gnus-summary-mark-article-as-forwarded (article)
10789   "Mark ARTICLE as forwarded and update the summary line.
10790 ARTICLE can also be a list of articles."
10791   (let ((articles (if (listp article) article (list article))))
10792     (dolist (article articles)
10793       (push article gnus-newsgroup-forwarded)
10794       (let ((inhibit-read-only t))
10795         (when (gnus-summary-goto-subject article nil t)
10796           (gnus-summary-update-secondary-mark article))))))
10797
10798 (defun gnus-summary-set-bookmark (article)
10799   "Set a bookmark in current article."
10800   (interactive (list (gnus-summary-article-number)))
10801   (when (or (not (get-buffer gnus-article-buffer))
10802             (not gnus-current-article)
10803             (not gnus-article-current)
10804             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10805     (error "No current article selected"))
10806   ;; Remove old bookmark, if one exists.
10807   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10808   ;; Set the new bookmark, which is on the form
10809   ;; (article-number . line-number-in-body).
10810   (push
10811    (cons article
10812          (with-current-buffer gnus-article-buffer
10813            (count-lines
10814             (min (point)
10815                  (save-excursion
10816                    (article-goto-body)
10817                    (point)))
10818             (point))))
10819    gnus-newsgroup-bookmarks)
10820   (gnus-message 6 "A bookmark has been added to the current article."))
10821
10822 (defun gnus-summary-remove-bookmark (article)
10823   "Remove the bookmark from the current article."
10824   (interactive (list (gnus-summary-article-number)))
10825   ;; Remove old bookmark, if one exists.
10826   (if (not (assq article gnus-newsgroup-bookmarks))
10827       (gnus-message 6 "No bookmark in current article.")
10828     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10829     (gnus-message 6 "Removed bookmark.")))
10830
10831 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10832 (defun gnus-summary-mark-as-dormant (n)
10833   "Mark N articles forward as dormant.
10834 If N is negative, mark backward instead.  The difference between N and
10835 the actual number of articles marked is returned."
10836   (interactive "p")
10837   (gnus-summary-mark-forward n gnus-dormant-mark))
10838
10839 (defun gnus-summary-set-process-mark (article)
10840   "Set the process mark on ARTICLE and update the summary line."
10841   (setq gnus-newsgroup-processable
10842         (cons article
10843               (delq article gnus-newsgroup-processable)))
10844   (when (gnus-summary-goto-subject article)
10845     (gnus-summary-show-thread)
10846     (gnus-summary-goto-subject article)
10847     (gnus-summary-update-secondary-mark article)))
10848
10849 (defun gnus-summary-remove-process-mark (&rest articles)
10850   "Remove the process mark from ARTICLES and update the summary line."
10851   (dolist (article articles)
10852     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10853     (when (gnus-summary-goto-subject article)
10854       (gnus-summary-show-thread)
10855       (gnus-summary-goto-subject article)
10856       (gnus-summary-update-secondary-mark article)))
10857   t)
10858
10859 (defun gnus-summary-set-saved-mark (article)
10860   "Set the process mark on ARTICLE and update the summary line."
10861   (push article gnus-newsgroup-saved)
10862   (when (gnus-summary-goto-subject article)
10863     (gnus-summary-update-secondary-mark article)))
10864
10865 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10866   "Mark N articles as read forwards.
10867 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10868 The difference between N and the actual number of articles marked is
10869 returned.
10870 If NO-EXPIRE, auto-expiry will be inhibited."
10871   (interactive "p")
10872   (gnus-summary-show-thread)
10873   (let ((backward (< n 0))
10874         (gnus-summary-goto-unread
10875          (and gnus-summary-goto-unread
10876               (not (eq gnus-summary-goto-unread 'never))
10877               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10878                                     gnus-ticked-mark gnus-dormant-mark)))))
10879         (n (abs n))
10880         (mark (or mark gnus-del-mark)))
10881     (while (and (> n 0)
10882                 (gnus-summary-mark-article nil mark no-expire)
10883                 (zerop (gnus-summary-next-subject
10884                         (if backward -1 1)
10885                         (and gnus-summary-goto-unread
10886                              (not (eq gnus-summary-goto-unread 'never)))
10887                         t)))
10888       (setq n (1- n)))
10889     (when (/= 0 n)
10890       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10891     (gnus-summary-recenter)
10892     (gnus-summary-position-point)
10893     (gnus-set-mode-line 'summary)
10894     n))
10895
10896 (defun gnus-summary-mark-article-as-read (mark)
10897   "Mark the current article quickly as read with MARK."
10898   (let ((article (gnus-summary-article-number)))
10899     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10900     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10901     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10902     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10903     (push (cons article mark) gnus-newsgroup-reads)
10904     ;; Possibly remove from cache, if that is used.
10905     (when gnus-use-cache
10906       (gnus-cache-enter-remove-article article))
10907     ;; Allow the backend to change the mark.
10908     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10909     ;; Check for auto-expiry.
10910     (when (and gnus-newsgroup-auto-expire
10911                (memq mark gnus-auto-expirable-marks))
10912       (setq mark gnus-expirable-mark)
10913       ;; Let the backend know about the mark change.
10914       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10915       (push article gnus-newsgroup-expirable))
10916     ;; Set the mark in the buffer.
10917     (gnus-summary-update-mark mark 'unread)
10918     t))
10919
10920 (defun gnus-summary-mark-article-as-unread (mark)
10921   "Mark the current article quickly as unread with MARK."
10922   (let* ((article (gnus-summary-article-number))
10923          (old-mark (gnus-summary-article-mark article)))
10924     ;; Allow the backend to change the mark.
10925     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10926     (if (eq mark old-mark)
10927         t
10928       (if (<= article 0)
10929           (progn
10930             (gnus-error 1 "Can't mark negative article numbers")
10931             nil)
10932         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10933         (setq gnus-newsgroup-spam-marked
10934               (delq article gnus-newsgroup-spam-marked))
10935         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10936         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10937         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10938         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10939         (cond ((= mark gnus-ticked-mark)
10940                (setq gnus-newsgroup-marked
10941                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10942                                               article)))
10943               ((= mark gnus-spam-mark)
10944                (setq gnus-newsgroup-spam-marked
10945                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10946                                               article)))
10947               ((= mark gnus-dormant-mark)
10948                (setq gnus-newsgroup-dormant
10949                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10950                                               article)))
10951               (t
10952                (setq gnus-newsgroup-unreads
10953                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10954                                               article))))
10955         (gnus-alist-pull article gnus-newsgroup-reads)
10956
10957         ;; See whether the article is to be put in the cache.
10958         (and gnus-use-cache
10959              (vectorp (gnus-summary-article-header article))
10960              (save-excursion
10961                (gnus-cache-possibly-enter-article
10962                 gnus-newsgroup-name article
10963                 (= mark gnus-ticked-mark)
10964                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10965
10966         ;; Fix the mark.
10967         (gnus-summary-update-mark mark 'unread)
10968         t))))
10969
10970 (defun gnus-summary-mark-article (&optional article mark no-expire)
10971   "Mark ARTICLE with MARK.  MARK can be any character.
10972 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10973 `??' (dormant) and `?E' (expirable).
10974 If MARK is nil, then the default character `?r' is used.
10975 If ARTICLE is nil, then the article on the current line will be
10976 marked.
10977 If NO-EXPIRE, auto-expiry will be inhibited."
10978   ;; The mark might be a string.
10979   (when (stringp mark)
10980     (setq mark (aref mark 0)))
10981   ;; If no mark is given, then we check auto-expiring.
10982   (when (null mark)
10983     (setq mark gnus-del-mark))
10984   (when (and (not no-expire)
10985              gnus-newsgroup-auto-expire
10986              (memq mark gnus-auto-expirable-marks))
10987     (setq mark gnus-expirable-mark))
10988   (let ((article (or article (gnus-summary-article-number)))
10989         (old-mark (gnus-summary-article-mark article)))
10990     ;; Allow the backend to change the mark.
10991     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10992     (if (eq mark old-mark)
10993         t
10994       (unless article
10995         (error "No article on current line"))
10996       (if (not (if (or (= mark gnus-unread-mark)
10997                        (= mark gnus-ticked-mark)
10998                        (= mark gnus-spam-mark)
10999                        (= mark gnus-dormant-mark))
11000                    (gnus-mark-article-as-unread article mark)
11001                  (gnus-mark-article-as-read article mark)))
11002           t
11003         ;; See whether the article is to be put in the cache.
11004         (and gnus-use-cache
11005              (not (= mark gnus-canceled-mark))
11006              (vectorp (gnus-summary-article-header article))
11007              (save-excursion
11008                (gnus-cache-possibly-enter-article
11009                 gnus-newsgroup-name article
11010                 (= mark gnus-ticked-mark)
11011                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11012
11013         (when (gnus-summary-goto-subject article nil t)
11014           (let ((inhibit-read-only t))
11015             (gnus-summary-show-thread)
11016             ;; Fix the mark.
11017             (gnus-summary-update-mark mark 'unread)
11018             t))))))
11019
11020 (defun gnus-summary-update-secondary-mark (article)
11021   "Update the secondary (read, process, cache) mark."
11022   (gnus-summary-update-mark
11023    (cond ((memq article gnus-newsgroup-processable)
11024           gnus-process-mark)
11025          ((memq article gnus-newsgroup-cached)
11026           gnus-cached-mark)
11027          ((memq article gnus-newsgroup-replied)
11028           gnus-replied-mark)
11029          ((memq article gnus-newsgroup-forwarded)
11030           gnus-forwarded-mark)
11031          ((memq article gnus-newsgroup-saved)
11032           gnus-saved-mark)
11033          ((memq article gnus-newsgroup-unseen)
11034           gnus-unseen-mark)
11035          (t gnus-no-mark))
11036    'replied)
11037   (when (gnus-visual-p 'summary-highlight 'highlight)
11038     (gnus-summary-highlight-line)
11039     (gnus-run-hooks 'gnus-summary-update-hook))
11040   t)
11041
11042 (defun gnus-summary-update-download-mark (article)
11043   "Update the download mark."
11044   (gnus-summary-update-mark
11045    (cond ((memq article gnus-newsgroup-undownloaded)
11046           gnus-undownloaded-mark)
11047          (gnus-newsgroup-agentized
11048           gnus-downloaded-mark)
11049          (t
11050           gnus-no-mark))
11051    'download)
11052   (gnus-summary-update-line t)
11053   t)
11054
11055 (defun gnus-summary-update-mark (mark type)
11056   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11057         (inhibit-read-only t))
11058     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11059     (when forward
11060       (when (looking-at "\r")
11061         (incf forward))
11062       (when (<= (+ forward (point)) (point-max))
11063         ;; Go to the right position on the line.
11064         (goto-char (+ forward (point)))
11065         ;; Replace the old mark with the new mark.
11066         (let ((to-insert
11067                (mm-subst-char-in-string
11068                 (char-after) mark
11069                 (buffer-substring (point) (1+ (point))))))
11070           (delete-region (point) (1+ (point)))
11071           (insert to-insert))
11072         ;; Optionally update the marks by some user rule.
11073         (when (eq type 'unread)
11074           (gnus-data-set-mark
11075            (gnus-data-find (gnus-summary-article-number)) mark)
11076           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11077
11078 (defun gnus-mark-article-as-read (article &optional mark)
11079   "Enter ARTICLE in the pertinent lists and remove it from others."
11080   ;; Make the article expirable.
11081   (let ((mark (or mark gnus-del-mark)))
11082     (setq gnus-newsgroup-expirable
11083           (if (= mark gnus-expirable-mark)
11084               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11085             (delq article gnus-newsgroup-expirable)))
11086     ;; Remove from unread and marked lists.
11087     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11088     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11089     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11090     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11091     (push (cons article mark) gnus-newsgroup-reads)
11092     ;; Possibly remove from cache, if that is used.
11093     (when gnus-use-cache
11094       (gnus-cache-enter-remove-article article))
11095     t))
11096
11097 (defun gnus-mark-article-as-unread (article &optional mark)
11098   "Enter ARTICLE in the pertinent lists and remove it from others."
11099   (let ((mark (or mark gnus-ticked-mark)))
11100     (if (<= article 0)
11101         (progn
11102           (gnus-error 1 "Can't mark negative article numbers")
11103           nil)
11104       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11105             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11106             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11107             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11108             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11109
11110       ;; Unsuppress duplicates?
11111       (when gnus-suppress-duplicates
11112         (gnus-dup-unsuppress-article article))
11113
11114       (cond ((= mark gnus-ticked-mark)
11115              (setq gnus-newsgroup-marked
11116                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11117             ((= mark gnus-spam-mark)
11118              (setq gnus-newsgroup-spam-marked
11119                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11120                                             article)))
11121             ((= mark gnus-dormant-mark)
11122              (setq gnus-newsgroup-dormant
11123                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11124             (t
11125              (setq gnus-newsgroup-unreads
11126                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11127       (gnus-alist-pull article gnus-newsgroup-reads)
11128       t)))
11129
11130 (defun gnus-summary-tick-article-forward (n)
11131   "Tick N articles forwards.
11132 If N is negative, tick backwards instead.
11133 The difference between N and the number of articles ticked is returned."
11134   (interactive "p")
11135   (gnus-summary-mark-forward n gnus-ticked-mark))
11136
11137 (defun gnus-summary-tick-article-backward (n)
11138   "Tick N articles backwards.
11139 The difference between N and the number of articles ticked is returned."
11140   (interactive "p")
11141   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11142
11143 (defun gnus-summary-tick-article (&optional article clear-mark)
11144   "Mark current article as unread.
11145 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11146 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11147   (interactive)
11148   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11149                                        gnus-ticked-mark)))
11150
11151 (defun gnus-summary-mark-as-read-forward (n)
11152   "Mark N articles as read forwards.
11153 If N is negative, mark backwards instead.
11154 The difference between N and the actual number of articles marked is
11155 returned."
11156   (interactive "p")
11157   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11158
11159 (defun gnus-summary-mark-as-read-backward (n)
11160   "Mark the N articles as read backwards.
11161 The difference between N and the actual number of articles marked is
11162 returned."
11163   (interactive "p")
11164   (gnus-summary-mark-forward
11165    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11166
11167 (defun gnus-summary-mark-as-read (&optional article mark)
11168   "Mark current article as read.
11169 ARTICLE specifies the article to be marked as read.
11170 MARK specifies a string to be inserted at the beginning of the line."
11171   (gnus-summary-mark-article article mark))
11172
11173 (defun gnus-summary-clear-mark-forward (n)
11174   "Clear marks from N articles forward.
11175 If N is negative, clear backward instead.
11176 The difference between N and the number of marks cleared is returned."
11177   (interactive "p")
11178   (gnus-summary-mark-forward n gnus-unread-mark))
11179
11180 (defun gnus-summary-clear-mark-backward (n)
11181   "Clear marks from N articles backward.
11182 The difference between N and the number of marks cleared is returned."
11183   (interactive "p")
11184   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11185
11186 (defun gnus-summary-mark-unread-as-read ()
11187   "Intended to be used by `gnus-mark-article-hook'."
11188   (when (memq gnus-current-article gnus-newsgroup-unreads)
11189     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11190
11191 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11192   "Intended to be used by `gnus-mark-article-hook'."
11193   (let ((mark (gnus-summary-article-mark)))
11194     (when (or (gnus-unread-mark-p mark)
11195               (gnus-read-mark-p mark))
11196       (gnus-summary-mark-article gnus-current-article
11197                                  (or new-mark gnus-read-mark)))))
11198
11199 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11200   "Intended to be used by `gnus-mark-article-hook'."
11201   (let ((mark (gnus-summary-article-mark)))
11202     (when (or (gnus-unread-mark-p mark)
11203               (gnus-read-mark-p mark))
11204       (gnus-summary-mark-article (gnus-summary-article-number)
11205                                  (or new-mark gnus-read-mark)))))
11206
11207 (defun gnus-summary-mark-unread-as-ticked ()
11208   "Intended to be used by `gnus-mark-article-hook'."
11209   (when (memq gnus-current-article gnus-newsgroup-unreads)
11210     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11211
11212 (defun gnus-summary-mark-region-as-read (point mark all)
11213   "Mark all unread articles between point and mark as read.
11214 If given a prefix, mark all articles between point and mark as read,
11215 even ticked and dormant ones."
11216   (interactive "r\nP")
11217   (save-excursion
11218     (let (article)
11219       (goto-char point)
11220       (beginning-of-line)
11221       (while (and
11222               (< (point) mark)
11223               (progn
11224                 (when (or all
11225                           (memq (setq article (gnus-summary-article-number))
11226                                 gnus-newsgroup-unreads))
11227                   (gnus-summary-mark-article article gnus-del-mark))
11228                 t)
11229               (gnus-summary-find-next))))))
11230
11231 (defun gnus-summary-mark-below (score mark)
11232   "Mark articles with score less than SCORE with MARK."
11233   (interactive "P\ncMark: ")
11234   (setq score (if score
11235                   (prefix-numeric-value score)
11236                 (or gnus-summary-default-score 0)))
11237   (with-current-buffer gnus-summary-buffer
11238     (goto-char (point-min))
11239     (while
11240         (progn
11241           (and (< (gnus-summary-article-score) score)
11242                (gnus-summary-mark-article nil mark))
11243           (gnus-summary-find-next)))))
11244
11245 (defun gnus-summary-kill-below (&optional score)
11246   "Mark articles with score below SCORE as read."
11247   (interactive "P")
11248   (gnus-summary-mark-below score gnus-killed-mark))
11249
11250 (defun gnus-summary-clear-above (&optional score)
11251   "Clear all marks from articles with score above SCORE."
11252   (interactive "P")
11253   (gnus-summary-mark-above score gnus-unread-mark))
11254
11255 (defun gnus-summary-tick-above (&optional score)
11256   "Tick all articles with score above SCORE."
11257   (interactive "P")
11258   (gnus-summary-mark-above score gnus-ticked-mark))
11259
11260 (defun gnus-summary-mark-above (score mark)
11261   "Mark articles with score over SCORE with MARK."
11262   (interactive "P\ncMark: ")
11263   (setq score (if score
11264                   (prefix-numeric-value score)
11265                 (or gnus-summary-default-score 0)))
11266   (with-current-buffer gnus-summary-buffer
11267     (goto-char (point-min))
11268     (while (and (progn
11269                   (when (> (gnus-summary-article-score) score)
11270                     (gnus-summary-mark-article nil mark))
11271                   t)
11272                 (gnus-summary-find-next)))))
11273
11274 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11275 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11276 (defun gnus-summary-limit-include-expunged (&optional no-error)
11277   "Display all the hidden articles that were expunged for low scores."
11278   (interactive)
11279   (let ((inhibit-read-only t))
11280     (let ((scored gnus-newsgroup-scored)
11281           headers h)
11282       (while scored
11283         (unless (gnus-summary-article-header (caar scored))
11284           (and (setq h (gnus-number-to-header (caar scored)))
11285                (< (cdar scored) gnus-summary-expunge-below)
11286                (push h headers)))
11287         (setq scored (cdr scored)))
11288       (if (not headers)
11289           (when (not no-error)
11290             (error "No expunged articles hidden"))
11291         (goto-char (point-min))
11292         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11293         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11294         (dolist (x headers)
11295           (push (mail-header-number x) gnus-newsgroup-limit))
11296         (gnus-summary-prepare-unthreaded (nreverse headers))
11297         (goto-char (point-min))
11298         (gnus-summary-position-point)
11299         t))))
11300
11301 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11302   "Mark all unread articles in this newsgroup as read.
11303 If prefix argument ALL is non-nil, ticked and dormant articles will
11304 also be marked as read.
11305 If QUIETLY is non-nil, no questions will be asked.
11306
11307 If TO-HERE is non-nil, it should be a point in the buffer.  All
11308 articles before (after, if REVERSE is set) this point will be marked
11309 as read.
11310
11311 Note that this function will only catch up the unread article
11312 in the current summary buffer limitation.
11313
11314 The number of articles marked as read is returned."
11315   (interactive "P")
11316   (prog1
11317       (save-excursion
11318         (when (or quietly
11319                   (not gnus-interactive-catchup) ;Without confirmation?
11320                   gnus-expert-user
11321                   (gnus-y-or-n-p
11322                    (if all
11323                        "Mark absolutely all articles as read? "
11324                      "Mark all unread articles as read? ")))
11325           (if (and not-mark
11326                    (not gnus-newsgroup-adaptive)
11327                    (not gnus-newsgroup-auto-expire)
11328                    (not gnus-suppress-duplicates)
11329                    (or (not gnus-use-cache)
11330                        (eq gnus-use-cache 'passive)))
11331               (progn
11332                 (when all
11333                   (setq gnus-newsgroup-marked nil
11334                         gnus-newsgroup-spam-marked nil
11335                         gnus-newsgroup-dormant nil))
11336                 (setq gnus-newsgroup-unreads
11337                       (gnus-sorted-nunion
11338                        (gnus-sorted-intersection gnus-newsgroup-unreads
11339                                                  gnus-newsgroup-downloadable)
11340                        (gnus-sorted-difference gnus-newsgroup-unfetched
11341                                                gnus-newsgroup-cached))))
11342             ;; We actually mark all articles as canceled, which we
11343             ;; have to do when using auto-expiry or adaptive scoring.
11344             (gnus-summary-show-all-threads)
11345             (if (and to-here reverse)
11346                 (progn
11347                   (goto-char to-here)
11348                   (gnus-summary-mark-current-read-and-unread-as-read
11349                    gnus-catchup-mark)
11350                   (while (gnus-summary-find-next (not all))
11351                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11352               (when (gnus-summary-first-subject (not all))
11353                 (while (and
11354                         (if to-here (< (point) to-here) t)
11355                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11356                         (gnus-summary-find-next (not all))))))
11357             (gnus-set-mode-line 'summary))
11358           t))
11359     (gnus-summary-position-point)))
11360
11361 (defun gnus-summary-catchup-to-here (&optional all)
11362   "Mark all unticked articles before the current one as read.
11363 If ALL is non-nil, also mark ticked and dormant articles as read."
11364   (interactive "P")
11365   (save-excursion
11366     (gnus-save-hidden-threads
11367       (let ((beg (point)))
11368         ;; We check that there are unread articles.
11369         (when (or all (gnus-summary-find-prev))
11370           (gnus-summary-catchup all t beg)))))
11371   (gnus-summary-position-point))
11372
11373 (defun gnus-summary-catchup-from-here (&optional all)
11374   "Mark all unticked articles after (and including) the current one as read.
11375 If ALL is non-nil, also mark ticked and dormant articles as read."
11376   (interactive "P")
11377   (save-excursion
11378     (gnus-save-hidden-threads
11379       (let ((beg (point)))
11380         ;; We check that there are unread articles.
11381         (when (or all (gnus-summary-find-next))
11382           (gnus-summary-catchup all t beg nil t)))))
11383   (gnus-summary-position-point))
11384
11385 (defun gnus-summary-catchup-all (&optional quietly)
11386   "Mark all articles in this newsgroup as read.
11387 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11388 instead, which marks only unread articles as read."
11389   (interactive "P")
11390   (gnus-summary-catchup t quietly))
11391
11392 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11393   "Mark all unread articles in this group as read, then exit.
11394 If prefix argument ALL is non-nil, all articles are marked as read.
11395 If QUIETLY is non-nil, no questions will be asked."
11396   (interactive "P")
11397   (when (gnus-summary-catchup all quietly nil 'fast)
11398     ;; Select next newsgroup or exit.
11399     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11400              (eq gnus-auto-select-next 'quietly))
11401         (gnus-summary-next-group nil)
11402       (gnus-summary-exit))))
11403
11404 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11405   "Mark all articles in this newsgroup as read, and then exit.
11406 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11407 instead, which marks only unread articles as read."
11408   (interactive "P")
11409   (gnus-summary-catchup-and-exit t quietly))
11410
11411 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11412   "Mark all articles in this group as read and select the next group.
11413 If given a prefix, mark all articles, unread as well as ticked, as
11414 read."
11415   (interactive "P")
11416   (save-excursion
11417     (gnus-summary-catchup all))
11418   (gnus-summary-next-group))
11419
11420 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11421   "Mark all articles in this group as read and select the previous group.
11422 If given a prefix, mark all articles, unread as well as ticked, as
11423 read."
11424   (interactive "P")
11425   (save-excursion
11426     (gnus-summary-catchup all))
11427   (gnus-summary-next-group nil nil t))
11428
11429 ;;;
11430 ;;; with article
11431 ;;;
11432
11433 (defmacro gnus-with-article (article &rest forms)
11434   "Select ARTICLE and perform FORMS in the original article buffer.
11435 Then replace the article with the result."
11436   `(progn
11437      ;; We don't want the article to be marked as read.
11438      (let (gnus-mark-article-hook)
11439        (gnus-summary-select-article t t nil ,article))
11440      (set-buffer gnus-original-article-buffer)
11441      ,@forms
11442      (if (not (gnus-check-backend-function
11443                'request-replace-article (car gnus-article-current)))
11444          (gnus-message 5 "Read-only group; not replacing")
11445        (unless (gnus-request-replace-article
11446                 ,article (car gnus-article-current)
11447                 (current-buffer) t)
11448          (error "Couldn't replace article")))
11449      ;; The cache and backlog have to be flushed somewhat.
11450      (when gnus-keep-backlog
11451        (gnus-backlog-remove-article
11452         (car gnus-article-current) (cdr gnus-article-current)))
11453      (when gnus-use-cache
11454        (gnus-cache-update-article
11455         (car gnus-article-current) (cdr gnus-article-current)))))
11456
11457 (put 'gnus-with-article 'lisp-indent-function 1)
11458 (put 'gnus-with-article 'edebug-form-spec '(form body))
11459
11460 ;; Thread-based commands.
11461
11462 (defun gnus-summary-articles-in-thread (&optional article)
11463   "Return a list of all articles in the current thread.
11464 If ARTICLE is non-nil, return all articles in the thread that starts
11465 with that article."
11466   (let* ((article (or article (gnus-summary-article-number)))
11467          (data (gnus-data-find-list article))
11468          (top-level (gnus-data-level (car data)))
11469          (top-subject
11470           (cond ((null gnus-thread-operation-ignore-subject)
11471                  (gnus-simplify-subject-re
11472                   (mail-header-subject (gnus-data-header (car data)))))
11473                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11474                  (gnus-simplify-subject-fuzzy
11475                   (mail-header-subject (gnus-data-header (car data)))))
11476                 (t nil)))
11477          (end-point (save-excursion
11478                       (goto-char (gnus-data-pos (car data)))
11479                       (if (gnus-summary-go-to-next-thread)
11480                           (point) (point-max))))
11481          articles)
11482     (while (and data
11483                 (< (gnus-data-pos (car data)) end-point))
11484       (when (or (not top-subject)
11485                 (string= top-subject
11486                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11487                              (gnus-simplify-subject-fuzzy
11488                               (mail-header-subject
11489                                (gnus-data-header (car data))))
11490                            (gnus-simplify-subject-re
11491                             (mail-header-subject
11492                              (gnus-data-header (car data)))))))
11493         (push (gnus-data-number (car data)) articles))
11494       (unless (and (setq data (cdr data))
11495                    (> (gnus-data-level (car data)) top-level))
11496         (setq data nil)))
11497     ;; Return the list of articles.
11498     (nreverse articles)))
11499
11500 (defun gnus-summary-rethread-current ()
11501   "Rethread the thread the current article is part of."
11502   (interactive)
11503   (let* ((gnus-show-threads t)
11504          (article (gnus-summary-article-number))
11505          (id (mail-header-id (gnus-summary-article-header)))
11506          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11507     (unless id
11508       (error "No article on the current line"))
11509     (gnus-rebuild-thread id)
11510     (gnus-summary-goto-subject article)))
11511
11512 (defun gnus-summary-reparent-thread ()
11513   "Make the current article child of the marked (or previous) article.
11514
11515 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11516 is non-nil or the Subject: of both articles are the same."
11517   (interactive)
11518   (unless (not (gnus-group-read-only-p))
11519     (error "The current newsgroup does not support article editing"))
11520   (unless (<= (length gnus-newsgroup-processable) 1)
11521     (error "No more than one article may be marked"))
11522   (let ((child (gnus-summary-article-number))
11523         ;; First grab the marked article, otherwise one line up.
11524         (parent (if (not (null gnus-newsgroup-processable))
11525                     (car gnus-newsgroup-processable)
11526                   (save-excursion
11527                     (if (eq (forward-line -1) 0)
11528                         (gnus-summary-article-number)
11529                       (error "Beginning of summary buffer"))))))
11530     (gnus-summary-reparent-children parent (list child))))
11531
11532 (defun gnus-summary-reparent-children (parent children)
11533   "Make PARENT the parent of CHILDREN.
11534 When called interactively, PARENT is the current article and CHILDREN
11535 are the process-marked articles."
11536   (interactive
11537    (list (gnus-summary-article-number)
11538          (gnus-summary-work-articles nil)))
11539   (dolist (child children)
11540     (save-window-excursion
11541       (let ((gnus-article-buffer " *reparent*"))
11542         (unless (not (eq parent child))
11543           (error "An article may not be self-referential"))
11544         (let ((message-id (mail-header-id
11545                            (gnus-summary-article-header parent))))
11546           (unless (and message-id (not (equal message-id "")))
11547             (error "No message-id in desired parent"))
11548           (gnus-with-article child
11549             (save-restriction
11550               (goto-char (point-min))
11551               (message-narrow-to-head)
11552               (if (re-search-forward "^References: " nil t)
11553                   (progn
11554                     (re-search-forward "^[^ \t]" nil t)
11555                     (forward-line -1)
11556                     (end-of-line)
11557                     (insert " " message-id))
11558                 (insert "References: " message-id "\n"))))
11559           (set-buffer gnus-summary-buffer)
11560           (gnus-summary-unmark-all-processable)
11561           (gnus-summary-update-article child)
11562           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11563             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11564           (gnus-summary-rethread-current)
11565           (gnus-message 3 "Article %d is now the child of article %d"
11566                         child parent))))))
11567
11568 (defun gnus-summary-toggle-threads (&optional arg)
11569   "Toggle showing conversation threads.
11570 If ARG is positive number, turn showing conversation threads on."
11571   (interactive "P")
11572   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11573     (setq gnus-show-threads
11574           (if (null arg) (not gnus-show-threads)
11575             (> (prefix-numeric-value arg) 0)))
11576     (gnus-summary-prepare)
11577     (gnus-summary-goto-subject current)
11578     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11579     (gnus-summary-position-point)))
11580
11581 (eval-and-compile
11582   (if (fboundp 'remove-overlays)
11583       (defalias 'gnus-remove-overlays 'remove-overlays)
11584     (defun gnus-remove-overlays (beg end name val)
11585       "Clear BEG and END of overlays whose property NAME has value VAL.
11586 For compatibility with XEmacs."
11587       (dolist (ov (gnus-overlays-in beg end))
11588         (when (eq (gnus-overlay-get ov name) val)
11589           (gnus-delete-overlay ov))))))
11590
11591 (defun gnus-summary-show-all-threads ()
11592   "Show all threads."
11593   (interactive)
11594   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11595   (gnus-summary-position-point))
11596
11597 (defsubst gnus-summary--inv (p)
11598   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11599
11600 (defun gnus-summary-show-thread ()
11601   "Show thread subtrees.
11602 Returns nil if no thread was there to be shown."
11603   (interactive)
11604   (let* ((orig (point))
11605          (end (point-at-eol))
11606          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11607          ;; Leave point at bol
11608          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11609          (eoi (when end
11610                 (if (fboundp 'next-single-char-property-change)
11611                     ;; Note: XEmacs version of n-s-c-p-c may return nil
11612                     (or (next-single-char-property-change end 'invisible)
11613                         (point-max))
11614                   (while (progn
11615                            (end-of-line 2)
11616                            (and (not (eobp))
11617                                 (eq (get-char-property (point) 'invisible)
11618                                     'gnus-sum))))
11619                   (point)))))
11620     (when eoi
11621       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11622       (goto-char orig)
11623       (gnus-summary-position-point)
11624       eoi)))
11625
11626 (defun gnus-summary-maybe-hide-threads ()
11627   "If requested, hide the threads that should be hidden."
11628   (when (and gnus-show-threads
11629              gnus-thread-hide-subtree)
11630     (gnus-summary-hide-all-threads
11631      (if (or (consp gnus-thread-hide-subtree)
11632              (functionp gnus-thread-hide-subtree))
11633          (gnus-make-predicate gnus-thread-hide-subtree)
11634        nil))))
11635
11636 ;;; Hiding predicates.
11637
11638 (defun gnus-article-unread-p (header)
11639   (memq (mail-header-number header) gnus-newsgroup-unreads))
11640
11641 (defun gnus-article-unseen-p (header)
11642   (memq (mail-header-number header) gnus-newsgroup-unseen))
11643
11644 (defun gnus-map-articles (predicate articles)
11645   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11646   (apply 'gnus-or (mapcar predicate
11647                           (mapcar (lambda (number)
11648                                     (gnus-summary-article-header number))
11649                                   articles))))
11650
11651 (defun gnus-summary-hide-all-threads (&optional predicate)
11652   "Hide all thread subtrees.
11653 If PREDICATE is supplied, threads that satisfy this predicate
11654 will not be hidden."
11655   (interactive)
11656   (save-excursion
11657     (goto-char (point-min))
11658     (let ((end nil)
11659           (count 0))
11660       (while (not end)
11661         (incf count)
11662         (when (zerop (mod count 1000))
11663           (message "Hiding all threads... %d" count))
11664         (when (or (not predicate)
11665                   (gnus-map-articles
11666                    predicate (gnus-summary-article-children)))
11667             (gnus-summary-hide-thread))
11668         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11669   (gnus-summary-position-point))
11670
11671 (defun gnus-summary-hide-thread ()
11672   "Hide thread subtrees.
11673 If PREDICATE is supplied, threads that satisfy this predicate
11674 will not be hidden.
11675 Returns nil if no threads were there to be hidden."
11676   (interactive)
11677   (beginning-of-line)
11678   (let ((start (point))
11679         (starteol (line-end-position))
11680         (article (gnus-summary-article-number)))
11681     ;; Go forward until either the buffer ends or the subthread ends.
11682     (when (and (not (eobp))
11683                (or (zerop (gnus-summary-next-thread 1 t))
11684                    (goto-char (point-max))))
11685       (if (and (> (point) start)
11686                ;; FIXME: this should actually search for a non-invisible \n.
11687                (search-backward "\n" start t))
11688           (progn
11689             (when (> (point) starteol)
11690               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11691               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11692                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11693                 (gnus-overlay-put ol 'evaporate t)))
11694             (gnus-summary-goto-subject article)
11695             (when (> start (point))
11696               (message "Hiding the thread moved us backwards, aborting!")
11697               (goto-char (point-max))))
11698         (goto-char start)
11699         nil))))
11700
11701 (defun gnus-summary-go-to-next-thread (&optional previous)
11702   "Go to the same level (or less) next thread.
11703 If PREVIOUS is non-nil, go to previous thread instead.
11704 Return the article number moved to, or nil if moving was impossible."
11705   (let ((level (gnus-summary-thread-level))
11706         (way (if previous -1 1))
11707         (beg (point)))
11708     (forward-line way)
11709     (while (and (not (eobp))
11710                 (< level (gnus-summary-thread-level)))
11711       (forward-line way))
11712     (if (eobp)
11713         (progn
11714           (goto-char beg)
11715           nil)
11716       (setq beg (point))
11717       (prog1
11718           (gnus-summary-article-number)
11719         (goto-char beg)))))
11720
11721 (defun gnus-summary-next-thread (n &optional silent)
11722   "Go to the same level next N'th thread.
11723 If N is negative, search backward instead.
11724 Returns the difference between N and the number of skips actually
11725 done.
11726
11727 If SILENT, don't output messages."
11728   (interactive "p")
11729   (let ((backward (< n 0))
11730         (n (abs n)))
11731     (while (and (> n 0)
11732                 (gnus-summary-go-to-next-thread backward))
11733       (decf n))
11734     (unless silent
11735       (gnus-summary-position-point))
11736     (when (and (not silent) (/= 0 n))
11737       (gnus-message 7 "No more threads"))
11738     n))
11739
11740 (defun gnus-summary-prev-thread (n)
11741   "Go to the same level previous N'th thread.
11742 Returns the difference between N and the number of skips actually
11743 done."
11744   (interactive "p")
11745   (gnus-summary-next-thread (- n)))
11746
11747 (defun gnus-summary-go-down-thread ()
11748   "Go down one level in the current thread."
11749   (let ((children (gnus-summary-article-children)))
11750     (when children
11751       (gnus-summary-goto-subject (car children)))))
11752
11753 (defun gnus-summary-go-up-thread ()
11754   "Go up one level in the current thread."
11755   (let ((parent (gnus-summary-article-parent)))
11756     (when parent
11757       (gnus-summary-goto-subject parent))))
11758
11759 (defun gnus-summary-down-thread (n)
11760   "Go down thread N steps.
11761 If N is negative, go up instead.
11762 Returns the difference between N and how many steps down that were
11763 taken."
11764   (interactive "p")
11765   (let ((up (< n 0))
11766         (n (abs n)))
11767     (while (and (> n 0)
11768                 (if up (gnus-summary-go-up-thread)
11769                   (gnus-summary-go-down-thread)))
11770       (setq n (1- n)))
11771     (gnus-summary-position-point)
11772     (when (/= 0 n)
11773       (gnus-message 7 "Can't go further"))
11774     n))
11775
11776 (defun gnus-summary-up-thread (n)
11777   "Go up thread N steps.
11778 If N is negative, go down instead.
11779 Returns the difference between N and how many steps down that were
11780 taken."
11781   (interactive "p")
11782   (gnus-summary-down-thread (- n)))
11783
11784 (defun gnus-summary-top-thread ()
11785   "Go to the top of the thread."
11786   (interactive)
11787   (while (gnus-summary-go-up-thread))
11788   (gnus-summary-article-number))
11789
11790 (defun gnus-summary-expire-thread ()
11791   "Mark articles under current thread as expired."
11792   (interactive)
11793   (gnus-summary-kill-thread 0))
11794
11795 (defun gnus-summary-kill-thread (&optional unmark)
11796   "Mark articles under current thread as read.
11797 If the prefix argument is positive, remove any kinds of marks.
11798 If the prefix argument is zero, mark thread as expired.
11799 If the prefix argument is negative, tick articles instead."
11800   (interactive "P")
11801   (when unmark
11802     (setq unmark (prefix-numeric-value unmark)))
11803   (let ((articles (gnus-summary-articles-in-thread))
11804         (hide (or (null unmark) (= unmark 0))))
11805     (save-excursion
11806       ;; Expand the thread.
11807       (gnus-summary-show-thread)
11808       ;; Mark all the articles.
11809       (while articles
11810         (gnus-summary-goto-subject (car articles))
11811         (cond ((null unmark)
11812                (gnus-summary-mark-article-as-read gnus-killed-mark))
11813               ((> unmark 0)
11814                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11815               ((= unmark 0)
11816                (gnus-summary-mark-article nil gnus-expirable-mark))
11817               (t
11818                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11819         (setq articles (cdr articles))))
11820     ;; Hide killed subtrees when hide is true.
11821     (and hide
11822          gnus-thread-hide-killed
11823          (gnus-summary-hide-thread))
11824     ;; If hide is t, go to next unread subject.
11825     (when hide
11826       ;; Go to next unread subject.
11827       (gnus-summary-next-subject 1 t)))
11828   (gnus-set-mode-line 'summary))
11829
11830 ;; Summary sorting commands
11831
11832 (defun gnus-summary-sort-by-number (&optional reverse)
11833   "Sort the summary buffer by article number.
11834 Argument REVERSE means reverse order."
11835   (interactive "P")
11836   (gnus-summary-sort 'number reverse))
11837
11838 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11839   "Sort the summary buffer by most recent article number.
11840 Argument REVERSE means reverse order."
11841   (interactive "P")
11842   (gnus-summary-sort 'most-recent-number reverse))
11843
11844 (defun gnus-summary-sort-by-random (&optional reverse)
11845   "Randomize the order in the summary buffer.
11846 Argument REVERSE means to randomize in reverse order."
11847   (interactive "P")
11848   (gnus-summary-sort 'random reverse))
11849
11850 (defun gnus-summary-sort-by-author (&optional reverse)
11851   "Sort the summary buffer by author name alphabetically.
11852 If `case-fold-search' is non-nil, case of letters is ignored.
11853 Argument REVERSE means reverse order."
11854   (interactive "P")
11855   (gnus-summary-sort 'author reverse))
11856
11857 (defun gnus-summary-sort-by-recipient (&optional reverse)
11858   "Sort the summary buffer by recipient name alphabetically.
11859 If `case-fold-search' is non-nil, case of letters is ignored.
11860 Argument REVERSE means reverse order."
11861   (interactive "P")
11862   (gnus-summary-sort 'recipient reverse))
11863
11864 (defun gnus-summary-sort-by-subject (&optional reverse)
11865   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11866 If `case-fold-search' is non-nil, case of letters is ignored.
11867 Argument REVERSE means reverse order."
11868   (interactive "P")
11869   (gnus-summary-sort 'subject reverse))
11870
11871 (defun gnus-summary-sort-by-date (&optional reverse)
11872   "Sort the summary buffer by date.
11873 Argument REVERSE means reverse order."
11874   (interactive "P")
11875   (gnus-summary-sort 'date reverse))
11876
11877 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11878   "Sort the summary buffer by most recent date.
11879 Argument REVERSE means reverse order."
11880   (interactive "P")
11881   (gnus-summary-sort 'most-recent-date reverse))
11882
11883 (defun gnus-summary-sort-by-score (&optional reverse)
11884   "Sort the summary buffer by score.
11885 Argument REVERSE means reverse order."
11886   (interactive "P")
11887   (gnus-summary-sort 'score reverse))
11888
11889 (defun gnus-summary-sort-by-lines (&optional reverse)
11890   "Sort the summary buffer by the number of lines.
11891 Argument REVERSE means reverse order."
11892   (interactive "P")
11893   (gnus-summary-sort 'lines reverse))
11894
11895 (defun gnus-summary-sort-by-chars (&optional reverse)
11896   "Sort the summary buffer by article length.
11897 Argument REVERSE means reverse order."
11898   (interactive "P")
11899   (gnus-summary-sort 'chars reverse))
11900
11901 (defun gnus-summary-sort-by-original (&optional reverse)
11902   "Sort the summary buffer using the default sorting method.
11903 Argument REVERSE means reverse order."
11904   (interactive "P")
11905   (let* ((inhibit-read-only t)
11906          (gnus-summary-prepare-hook nil))
11907     ;; We do the sorting by regenerating the threads.
11908     (gnus-summary-prepare)
11909     ;; Hide subthreads if needed.
11910     (gnus-summary-maybe-hide-threads)))
11911
11912 (defun gnus-summary-sort (predicate reverse)
11913   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11914   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11915          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11916          (gnus-thread-sort-functions
11917           (if (not reverse)
11918               thread
11919             `(lambda (t1 t2)
11920                (,thread t2 t1))))
11921          (gnus-sort-gathered-threads-function
11922           gnus-thread-sort-functions)
11923          (gnus-article-sort-functions
11924           (if (not reverse)
11925               article
11926             `(lambda (t1 t2)
11927                (,article t2 t1))))
11928          (inhibit-read-only t)
11929          (gnus-summary-prepare-hook nil))
11930     ;; We do the sorting by regenerating the threads.
11931     (gnus-summary-prepare)
11932     ;; Hide subthreads if needed.
11933     (gnus-summary-maybe-hide-threads)))
11934
11935 ;; Summary saving commands.
11936
11937 (defun gnus-summary-save-article (&optional n not-saved)
11938   "Save the current article using the default saver function.
11939 If N is a positive number, save the N next articles.
11940 If N is a negative number, save the N previous articles.
11941 If N is nil and any articles have been marked with the process mark,
11942 save those articles instead.
11943 The variable `gnus-default-article-saver' specifies the saver function.
11944
11945 If the optional second argument NOT-SAVED is non-nil, articles saved
11946 will not be marked as saved."
11947   (interactive "P")
11948   (require 'gnus-art)
11949   (let* ((articles (gnus-summary-work-articles n))
11950          (save-buffer (save-excursion
11951                         (nnheader-set-temp-buffer " *Gnus Save*")))
11952          (num (length articles))
11953          ;; Whether to save decoded articles or raw articles.
11954          (decode (when gnus-article-save-coding-system
11955                    (get gnus-default-article-saver :decode)))
11956          ;; When saving many articles in a single file, use the other
11957          ;; function to save articles other than the first one.
11958          (saver2 (get gnus-default-article-saver :function))
11959          (gnus-prompt-before-saving (if saver2
11960                                         t
11961                                       gnus-prompt-before-saving))
11962          (gnus-default-article-saver gnus-default-article-saver)
11963          header file)
11964     (dolist (article articles)
11965       (setq header (gnus-summary-article-header article))
11966       (if (not (vectorp header))
11967           ;; This is a pseudo-article.
11968           (if (assq 'name header)
11969               (gnus-copy-file (cdr (assq 'name header)))
11970             (gnus-message 1 "Article %d is unsavable" article))
11971         ;; This is a real article.
11972         (save-window-excursion
11973           (gnus-summary-select-article decode decode nil article)
11974           (gnus-summary-goto-subject article))
11975         (with-current-buffer save-buffer
11976           (erase-buffer)
11977           (insert-buffer-substring (if decode
11978                                        gnus-article-buffer
11979                                      gnus-original-article-buffer)))
11980         (setq file (gnus-article-save save-buffer file num))
11981         (gnus-summary-remove-process-mark article)
11982         (unless not-saved
11983           (gnus-summary-set-saved-mark article)))
11984       (when saver2
11985         (setq gnus-default-article-saver saver2
11986               saver2 nil)))
11987     (gnus-kill-buffer save-buffer)
11988     (gnus-summary-position-point)
11989     (gnus-set-mode-line 'summary)
11990     n))
11991
11992 (declare-function gnus-summary-save-in-pipe "gnus-art" (&optional command raw))
11993
11994 (defun gnus-summary-pipe-output (&optional n sym)
11995   "Pipe the current article to a subprocess.
11996 If N is a positive number, pipe the N next articles.
11997 If N is a negative number, pipe the N previous articles.
11998 If N is nil and any articles have been marked with the process mark,
11999 pipe those articles instead.
12000 The default command to which articles are piped is specified by the
12001 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
12002 will be prompted for the command.
12003
12004 The properties `:decode' and `:headers' that are put to the function
12005 symbol `gnus-summary-save-in-pipe' control whether this function
12006 decodes articles and what headers to keep (see the doc string for the
12007 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
12008 is neither omitted nor the symbol `r', force including all headers
12009 regardless of the `:headers' property.  If it is the symbol `r',
12010 articles that are not decoded and include all headers will be piped
12011 no matter what the properties `:decode' and `:headers' are."
12012   (interactive (gnus-interactive "P\ny"))
12013   (require 'gnus-art)
12014   (let* ((articles (gnus-summary-work-articles n))
12015          (result-buffer "*Shell Command Output*")
12016          (all-headers (not (memq sym '(nil r))))
12017          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
12018          (raw (eq sym 'r))
12019          (headers (get 'gnus-summary-save-in-pipe :headers))
12020          command result)
12021     (unless (numberp (car articles))
12022       (error "No article to pipe"))
12023     (setq command (gnus-read-shell-command
12024                    (concat "Shell command on "
12025                            (if (cdr articles)
12026                                (format "these %d articles" (length articles))
12027                              "this article")
12028                            ": ")
12029                    gnus-summary-pipe-output-default-command))
12030     (when (string-equal command "")
12031       (error "A command is required"))
12032     (when all-headers
12033       (put 'gnus-summary-save-in-pipe :headers nil))
12034     (unwind-protect
12035         (while articles
12036           (gnus-summary-goto-subject (pop articles))
12037           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12038           (when (and (get-buffer result-buffer)
12039                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12040             (setq result (concat result (with-current-buffer result-buffer
12041                                           (buffer-string))))))
12042       (put 'gnus-summary-save-in-pipe :headers headers))
12043     (unless (zerop (length result))
12044       (if (with-current-buffer (get-buffer-create result-buffer)
12045             (erase-buffer)
12046             (insert result)
12047             (prog1
12048                 (and (= (count-lines (point-min) (point)) 1)
12049                      (progn
12050                        (end-of-line 0)
12051                        (<= (current-column)
12052                            (window-width (minibuffer-window)))))
12053               (goto-char (point-min))))
12054           (message "%s" (substring result 0 -1))
12055         (message nil)
12056         (gnus-configure-windows 'pipe)))))
12057
12058 (defun gnus-summary-save-article-mail (&optional arg)
12059   "Append the current article to a Unix mail box file.
12060 If N is a positive number, save the N next articles.
12061 If N is a negative number, save the N previous articles.
12062 If N is nil and any articles have been marked with the process mark,
12063 save those articles instead."
12064   (interactive "P")
12065   (require 'gnus-art)
12066   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12067     (gnus-summary-save-article arg)))
12068
12069 (defun gnus-summary-save-article-rmail (&optional arg)
12070   "Append the current article to an rmail file.
12071 If N is a positive number, save the N next articles.
12072 If N is a negative number, save the N previous articles.
12073 If N is nil and any articles have been marked with the process mark,
12074 save those articles instead."
12075   (interactive "P")
12076   (require 'gnus-art)
12077   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12078     (gnus-summary-save-article arg)))
12079
12080 (defun gnus-summary-save-article-file (&optional arg)
12081   "Append the current article to a file.
12082 If N is a positive number, save the N next articles.
12083 If N is a negative number, save the N previous articles.
12084 If N is nil and any articles have been marked with the process mark,
12085 save those articles instead."
12086   (interactive "P")
12087   (require 'gnus-art)
12088   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12089     (gnus-summary-save-article arg)))
12090
12091 (defun gnus-summary-write-article-file (&optional arg)
12092   "Write the current article to a file, deleting the previous file.
12093 If N is a positive number, save the N next articles.
12094 If N is a negative number, save the N previous articles.
12095 If N is nil and any articles have been marked with the process mark,
12096 save those articles instead."
12097   (interactive "P")
12098   (require 'gnus-art)
12099   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12100     (gnus-summary-save-article arg)))
12101
12102 (defun gnus-summary-save-article-body-file (&optional arg)
12103   "Append the current article body to a file.
12104 If N is a positive number, save the N next articles.
12105 If N is a negative number, save the N previous articles.
12106 If N is nil and any articles have been marked with the process mark,
12107 save those articles instead."
12108   (interactive "P")
12109   (require 'gnus-art)
12110   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12111     (gnus-summary-save-article arg)))
12112
12113 (defun gnus-summary-write-article-body-file (&optional arg)
12114   "Write the current article body to a file, deleting the previous file.
12115 If N is a positive number, save the N next articles.
12116 If N is a negative number, save the N previous articles.
12117 If N is nil and any articles have been marked with the process mark,
12118 save those articles instead."
12119   (interactive "P")
12120   (require 'gnus-art)
12121   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12122     (gnus-summary-save-article arg)))
12123
12124 (defun gnus-summary-muttprint (&optional arg)
12125   "Print the current article using Muttprint.
12126 If N is a positive number, save the N next articles.
12127 If N is a negative number, save the N previous articles.
12128 If N is nil and any articles have been marked with the process mark,
12129 save those articles instead."
12130   (interactive "P")
12131   (require 'gnus-art)
12132   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12133     (gnus-summary-save-article arg t)))
12134
12135 (defun gnus-summary-pipe-message (program)
12136   "Pipe the current article through PROGRAM."
12137   (interactive "sProgram: ")
12138   (gnus-summary-select-article)
12139   (let ((mail-header-separator ""))
12140     (gnus-eval-in-buffer-window gnus-article-buffer
12141       (save-restriction
12142         (widen)
12143         (let ((start (window-start))
12144               (inhibit-read-only t))
12145           (message-pipe-buffer-body program)
12146           (set-window-start (get-buffer-window (current-buffer)) start))))))
12147
12148 (defun gnus-get-split-value (methods)
12149   "Return a value based on the split METHODS."
12150   (let (split-name method result match)
12151     (when methods
12152       (with-current-buffer gnus-original-article-buffer
12153         (save-restriction
12154           (nnheader-narrow-to-headers)
12155           (while (and methods (not split-name))
12156             (goto-char (point-min))
12157             (setq method (pop methods))
12158             (setq match (car method))
12159             (when (cond
12160                    ((stringp match)
12161                     ;; Regular expression.
12162                     (ignore-errors
12163                       (re-search-forward match nil t)))
12164                    ((functionp match)
12165                     ;; Function.
12166                     (save-restriction
12167                       (widen)
12168                       (setq result (funcall match gnus-newsgroup-name))))
12169                    ((consp match)
12170                     ;; Form.
12171                     (save-restriction
12172                       (widen)
12173                       (setq result (eval match)))))
12174               (setq split-name (cdr method))
12175               (cond ((stringp result)
12176                      (push (expand-file-name
12177                             result gnus-article-save-directory)
12178                            split-name))
12179                     ((consp result)
12180                      (setq split-name (append result split-name)))))))))
12181     (nreverse split-name)))
12182
12183 (defun gnus-valid-move-group-p (group)
12184   (and (symbolp group)
12185        (boundp group)
12186        (symbol-name group)
12187        (symbol-value group)
12188        (gnus-get-function (gnus-find-method-for-group
12189                            (symbol-name group)) 'request-accept-article t)))
12190
12191 (defun gnus-read-move-group-name (prompt default articles prefix)
12192   "Read a group name."
12193   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12194          (minibuffer-confirm-incomplete nil) ; XEmacs
12195          (prom
12196           (format "%s %s to"
12197                   prompt
12198                   (if (> (length articles) 1)
12199                       (format "these %d articles" (length articles))
12200                     "this article")))
12201          (to-newsgroup
12202           (cond
12203            ((null split-name)
12204             (gnus-group-completing-read
12205              prom
12206              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12207              nil prefix nil default))
12208            ((= 1 (length split-name))
12209             (gnus-group-completing-read
12210              prom
12211              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12212              nil prefix 'gnus-group-history (car split-name)))
12213            (t
12214             (gnus-completing-read
12215              prom (nreverse split-name) nil nil 'gnus-group-history))))
12216          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12217          encoded)
12218     (when to-newsgroup
12219       (if (or (string= to-newsgroup "")
12220               (string= to-newsgroup prefix))
12221           (setq to-newsgroup default))
12222       (unless to-newsgroup
12223         (error "No group name entered"))
12224       (setq encoded (mm-encode-coding-string
12225                      to-newsgroup
12226                      (gnus-group-name-charset to-method to-newsgroup)))
12227       (or (gnus-active encoded)
12228           (gnus-activate-group encoded nil nil to-method)
12229           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12230                                      to-newsgroup))
12231               (or (and (gnus-request-create-group encoded to-method)
12232                        (gnus-activate-group encoded nil nil to-method)
12233                        (gnus-subscribe-group encoded))
12234                   (error "Couldn't create group %s" to-newsgroup)))
12235           (error "No such group: %s" to-newsgroup))
12236       encoded)))
12237
12238 (defvar gnus-summary-save-parts-counter)
12239 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12240
12241 (defun gnus-summary-save-parts (type dir n &optional reverse)
12242   "Save parts matching TYPE to DIR.
12243 If REVERSE, save parts that do not match TYPE."
12244   (interactive
12245    (list (read-string "Save parts of type: "
12246                       (or (car gnus-summary-save-parts-type-history)
12247                           gnus-summary-save-parts-default-mime)
12248                       'gnus-summary-save-parts-type-history)
12249          (setq gnus-summary-save-parts-last-directory
12250                (read-directory-name "Save to directory: "
12251                                     gnus-summary-save-parts-last-directory
12252                                     nil t))
12253          current-prefix-arg))
12254   (gnus-summary-iterate n
12255     (let ((gnus-display-mime-function nil)
12256           gnus-article-prepare-hook
12257           gnus-article-decode-hook
12258           gnus-display-mime-function
12259           gnus-break-pages
12260           (gnus-inhibit-treatment t))
12261       (gnus-summary-select-article))
12262     (with-current-buffer gnus-article-buffer
12263       (let ((handles (or gnus-article-mime-handles
12264                          (mm-dissect-buffer nil gnus-article-loose-mime)
12265                          (and gnus-article-emulate-mime
12266                               (mm-uu-dissect))))
12267             (gnus-summary-save-parts-counter 1))
12268         (when handles
12269           (gnus-summary-save-parts-1 type dir handles reverse)
12270           (unless gnus-article-mime-handles ;; Don't destroy this case.
12271             (mm-destroy-parts handles)))))))
12272
12273 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12274   (if (stringp (car handle))
12275       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12276               (cdr handle))
12277     (when (if reverse
12278               (not (string-match type (mm-handle-media-type handle)))
12279             (string-match type (mm-handle-media-type handle)))
12280       (let ((file (expand-file-name
12281                    (gnus-map-function
12282                     mm-file-name-rewrite-functions
12283                     (file-name-nondirectory
12284                      (or
12285                       (mm-handle-filename handle)
12286                       (format "%s.%d.%d" gnus-newsgroup-name
12287                               (cdr gnus-article-current)
12288                               gnus-summary-save-parts-counter))))
12289                    dir)))
12290         (incf gnus-summary-save-parts-counter)
12291         (unless (file-exists-p file)
12292           (mm-save-part-to-file handle file))))))
12293
12294 ;; Summary extract commands
12295
12296 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12297   (let ((inhibit-read-only t)
12298         (article (gnus-summary-article-number))
12299         after-article b e)
12300     (unless (gnus-summary-goto-subject article)
12301       (error "No such article: %d" article))
12302     (gnus-summary-position-point)
12303     ;; If all commands are to be bunched up on one line, we collect
12304     ;; them here.
12305     (unless gnus-view-pseudos-separately
12306       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12307             files action)
12308         (while ps
12309           (setq action (cdr (assq 'action (car ps))))
12310           (setq files (list (cdr (assq 'name (car ps)))))
12311           (while (and ps (cdr ps)
12312                       (string= (or action "1")
12313                                (or (cdr (assq 'action (cadr ps))) "2")))
12314             (push (cdr (assq 'name (cadr ps))) files)
12315             (setcdr ps (cddr ps)))
12316           (when files
12317             (when (not (string-match "%s" action))
12318               (push " " files))
12319             (push " " files)
12320             (when (assq 'execute (car ps))
12321               (setcdr (assq 'execute (car ps))
12322                       (funcall (if (string-match "%s" action)
12323                                    'format 'concat)
12324                                action
12325                                (mapconcat
12326                                 (lambda (f)
12327                                   (if (equal f " ")
12328                                       f
12329                                     (shell-quote-argument f)))
12330                                 files " ")))))
12331           (setq ps (cdr ps)))))
12332     (if (and gnus-view-pseudos (not not-view))
12333         (while pslist
12334           (when (assq 'execute (car pslist))
12335             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12336                                   (eq gnus-view-pseudos 'not-confirm)))
12337           (setq pslist (cdr pslist)))
12338       (save-excursion
12339         (while pslist
12340           (setq after-article (or (cdr (assq 'article (car pslist)))
12341                                   (gnus-summary-article-number)))
12342           (gnus-summary-goto-subject after-article)
12343           (forward-line 1)
12344           (setq b (point))
12345           (insert "    " (file-name-nondirectory
12346                           (cdr (assq 'name (car pslist))))
12347                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12348           (setq e (point))
12349           (forward-line -1)             ; back to `b'
12350           (gnus-add-text-properties
12351            b (1- e) (list 'gnus-number gnus-reffed-article-number
12352                           gnus-mouse-face-prop gnus-mouse-face))
12353           (gnus-data-enter
12354            after-article gnus-reffed-article-number
12355            gnus-unread-mark b (car pslist) 0 (- e b))
12356           (setq gnus-newsgroup-unreads
12357                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12358                                          gnus-reffed-article-number))
12359           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12360           (setq pslist (cdr pslist)))))))
12361
12362 (defun gnus-pseudos< (p1 p2)
12363   (let ((c1 (cdr (assq 'action p1)))
12364         (c2 (cdr (assq 'action p2))))
12365     (and c1 c2 (string< c1 c2))))
12366
12367 (defun gnus-request-pseudo-article (props)
12368   (cond ((assq 'execute props)
12369          (gnus-execute-command (cdr (assq 'execute props)))))
12370   (let ((gnus-current-article (gnus-summary-article-number)))
12371     (gnus-run-hooks 'gnus-mark-article-hook)))
12372
12373 (defun gnus-execute-command (command &optional automatic)
12374   (save-excursion
12375     (gnus-article-setup-buffer)
12376     (set-buffer gnus-article-buffer)
12377     (setq buffer-read-only nil)
12378     (let ((command (if automatic command
12379                      (read-string "Command: " (cons command 0)))))
12380       (erase-buffer)
12381       (insert "$ " command "\n\n")
12382       (if gnus-view-pseudo-asynchronously
12383           (start-process "gnus-execute" (current-buffer) shell-file-name
12384                          shell-command-switch command)
12385         (call-process shell-file-name nil t nil
12386                       shell-command-switch command)))))
12387
12388 ;; Summary kill commands.
12389
12390 (defun gnus-summary-edit-global-kill (article)
12391   "Edit the \"global\" kill file."
12392   (interactive (list (gnus-summary-article-number)))
12393   (gnus-group-edit-global-kill article))
12394
12395 (defun gnus-summary-edit-local-kill ()
12396   "Edit a local kill file applied to the current newsgroup."
12397   (interactive)
12398   (setq gnus-current-headers (gnus-summary-article-header))
12399   (gnus-group-edit-local-kill
12400    (gnus-summary-article-number) gnus-newsgroup-name))
12401
12402 ;;; Header reading.
12403
12404 (defun gnus-read-header (id &optional header)
12405   "Read the headers of article ID and enter them into the Gnus system."
12406   (let ((group gnus-newsgroup-name)
12407         (gnus-override-method
12408          (or
12409           gnus-override-method
12410           (and (gnus-news-group-p gnus-newsgroup-name)
12411                (car (gnus-refer-article-methods)))))
12412         where)
12413     ;; First we check to see whether the header in question is already
12414     ;; fetched.
12415     (if (stringp id)
12416         ;; This is a Message-ID.
12417         (setq header (or header (gnus-id-to-header id)))
12418       ;; This is an article number.
12419       (setq header (or header (gnus-summary-article-header id))))
12420     (if (and header
12421              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12422         ;; We have found the header.
12423         header
12424       ;; We have to really fetch the header to this article.
12425       (with-current-buffer nntp-server-buffer
12426         (when (setq where (gnus-request-head id group))
12427           (nnheader-fold-continuation-lines)
12428           (goto-char (point-max))
12429           (insert ".\n")
12430           (goto-char (point-min))
12431           (insert "211 ")
12432           (princ (cond
12433                   ((numberp id) id)
12434                   ((cdr where) (cdr where))
12435                   (header (mail-header-number header))
12436                   (t gnus-reffed-article-number))
12437                  (current-buffer))
12438           (insert " Article retrieved.\n"))
12439         (if (or (not where)
12440                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12441             ()                          ; Malformed head.
12442           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12443             (when (and (bound-and-true-p gnus-registry-enabled)
12444                        (not (gnus-ephemeral-group-p (car where))))
12445               (gnus-registry-handle-action
12446                (mail-header-id header) nil
12447                (gnus-group-prefixed-name
12448                 (car where)
12449                 (or gnus-override-method (gnus-find-method-for-group group)))
12450                (mail-header-subject header)
12451                (mail-header-from header)))
12452             (when (and (stringp id)
12453                        (or
12454                         (not (string= (gnus-group-real-name group)
12455                                       (car where)))
12456                         (not (gnus-server-equal gnus-override-method
12457                                                 (gnus-group-method group)))))
12458               ;; If we fetched by Message-ID and the article came from
12459               ;; a different group (or server), we fudge some bogus
12460               ;; article numbers for this article.
12461               (mail-header-set-number header gnus-reffed-article-number))
12462             (with-current-buffer gnus-summary-buffer
12463               (decf gnus-reffed-article-number)
12464               (gnus-remove-header (mail-header-number header))
12465               (push header gnus-newsgroup-headers)
12466               (setq gnus-current-headers header)
12467               (push (mail-header-number header) gnus-newsgroup-limit)))
12468           header)))))
12469
12470 (defun gnus-remove-header (number)
12471   "Remove header NUMBER from `gnus-newsgroup-headers'."
12472   (if (and gnus-newsgroup-headers
12473            (= number (mail-header-number (car gnus-newsgroup-headers))))
12474       (pop gnus-newsgroup-headers)
12475     (let ((headers gnus-newsgroup-headers))
12476       (while (and (cdr headers)
12477                   (not (= number (mail-header-number (cadr headers)))))
12478         (pop headers))
12479       (when (cdr headers)
12480         (setcdr headers (cddr headers))))))
12481
12482 ;;;
12483 ;;; summary highlights
12484 ;;;
12485
12486 (defun gnus-highlight-selected-summary ()
12487   "Highlight selected article in summary buffer."
12488   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12489   (when gnus-summary-selected-face
12490     (save-excursion
12491       (let* ((beg (point-at-bol))
12492              (end (point-at-eol))
12493              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12494              (from (if (get-text-property beg gnus-mouse-face-prop)
12495                        beg
12496                      (or (next-single-property-change
12497                           beg gnus-mouse-face-prop nil end)
12498                          beg)))
12499              (to
12500               (if (= from end)
12501                   (- from 2)
12502                 (or (next-single-property-change
12503                      from gnus-mouse-face-prop nil end)
12504                     end))))
12505         ;; If no mouse-face prop on line we will have to = from = end,
12506         ;; so we highlight the entire line instead.
12507         (when (= (+ to 2) from)
12508           (setq from beg)
12509           (setq to end))
12510         (if gnus-newsgroup-selected-overlay
12511             ;; Move old overlay.
12512             (gnus-move-overlay
12513              gnus-newsgroup-selected-overlay from to (current-buffer))
12514           ;; Create new overlay.
12515           (gnus-overlay-put
12516            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12517            'face gnus-summary-selected-face))))))
12518
12519 (defvar gnus-summary-highlight-line-cached nil)
12520 (defvar gnus-summary-highlight-line-trigger nil)
12521
12522 (defun gnus-summary-highlight-line-0 ()
12523   (if (and (eq gnus-summary-highlight-line-trigger
12524                gnus-summary-highlight)
12525            gnus-summary-highlight-line-cached)
12526       gnus-summary-highlight-line-cached
12527     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12528           gnus-summary-highlight-line-cached
12529           (let* ((cond (list 'cond))
12530                  (c cond)
12531                  (list gnus-summary-highlight))
12532             (while list
12533               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12534                               nil))
12535               (setq c (cdr c)
12536                     list (cdr list)))
12537             (gnus-byte-compile (list 'lambda nil cond))))))
12538
12539 (defun gnus-summary-highlight-line ()
12540   "Highlight current line according to `gnus-summary-highlight'."
12541   (let* ((beg (point-at-bol))
12542          (article (or (gnus-summary-article-number) gnus-current-article))
12543          (score (or (cdr (assq article
12544                                gnus-newsgroup-scored))
12545                     gnus-summary-default-score 0))
12546          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12547          (inhibit-read-only t)
12548          (default gnus-summary-default-score)
12549          (default-high gnus-summary-default-high-score)
12550          (default-low gnus-summary-default-low-score)
12551          (uncached (and gnus-summary-use-undownloaded-faces
12552                         (memq article gnus-newsgroup-undownloaded)
12553                         (not (memq article gnus-newsgroup-cached)))))
12554     (let ((face (funcall (gnus-summary-highlight-line-0))))
12555       (unless (eq face (gnus-get-text-property-excluding-characters-with-faces beg 'face))
12556         (gnus-put-text-property-excluding-characters-with-faces
12557          beg (point-at-eol) 'face
12558          (setq face (if (boundp face) (symbol-value face) face)))
12559         (when gnus-summary-highlight-line-function
12560           (funcall gnus-summary-highlight-line-function article face))))))
12561
12562 (defun gnus-update-read-articles (group unread &optional compute)
12563   "Update the list of read articles in GROUP.
12564 UNREAD is a sorted list."
12565   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12566         (info (gnus-get-info group))
12567         (prev 1)
12568         read)
12569     (if (or (not info) (not active))
12570         ;; There is no info on this group if it was, in fact,
12571         ;; killed.  Gnus stores no information on killed groups, so
12572         ;; there's nothing to be done.
12573         ;; One could store the information somewhere temporarily,
12574         ;; perhaps...  Hmmm...
12575         ()
12576       ;; Remove any negative articles numbers.
12577       (while (and unread (< (car unread) 0))
12578         (setq unread (cdr unread)))
12579       ;; Remove any expired article numbers
12580       (while (and unread (< (car unread) (car active)))
12581         (setq unread (cdr unread)))
12582       ;; Compute the ranges of read articles by looking at the list of
12583       ;; unread articles.
12584       (while unread
12585         (when (/= (car unread) prev)
12586           (push (if (= prev (1- (car unread))) prev
12587                   (cons prev (1- (car unread))))
12588                 read))
12589         (setq prev (1+ (car unread)))
12590         (setq unread (cdr unread)))
12591       (when (<= prev (cdr active))
12592         (push (cons prev (cdr active)) read))
12593       (setq read (if (> (length read) 1) (nreverse read) read))
12594       (if compute
12595           read
12596         (save-excursion
12597           (let (setmarkundo)
12598             ;; Propagate the read marks to the backend.
12599             (when (and (gnus-method-option-p
12600                         (gnus-find-method-for-group group)
12601                         'server-marks)
12602                        (gnus-check-backend-function 'request-set-mark group))
12603               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12604                     (add (gnus-remove-from-range read (gnus-info-read info))))
12605                 (when (or add del)
12606                   (unless (gnus-check-group group)
12607                     (error "Can't open server for %s" group))
12608                   (gnus-request-set-mark
12609                    group (delq nil (list (if add (list add 'add '(read)))
12610                                          (if del (list del 'del '(read))))))
12611                   (setq setmarkundo
12612                         `(gnus-request-set-mark
12613                           ,group
12614                           ',(delq nil (list
12615                                        (if del (list del 'add '(read)))
12616                                        (if add (list add 'del '(read))))))))))
12617             (set-buffer gnus-group-buffer)
12618             (gnus-undo-register
12619               `(progn
12620                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12621                  (gnus-info-set-read ',info ',(gnus-info-read info))
12622                  (gnus-get-unread-articles-in-group ',info
12623                                                     (gnus-active ,group))
12624                  (gnus-group-update-group ,group t)
12625                  ,setmarkundo))))
12626         ;; Enter this list into the group info.
12627         (gnus-info-set-read info read)
12628         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12629         (gnus-get-unread-articles-in-group info (gnus-active group))
12630         t))))
12631
12632 (defun gnus-offer-save-summaries ()
12633   "Offer to save all active summary buffers."
12634   (let (buffers)
12635     ;; Go through all buffers and find all summaries.
12636     (dolist (buffer (buffer-list))
12637       (when (and (setq buffer (buffer-name buffer))
12638                  (string-match "Summary" buffer)
12639                  (with-current-buffer buffer
12640                    ;; We check that this is, indeed, a summary buffer.
12641                    (and (eq major-mode 'gnus-summary-mode)
12642                         ;; Also make sure this isn't bogus.
12643                         gnus-newsgroup-prepared
12644                         ;; Also make sure that this isn't a
12645                         ;; dead summary buffer.
12646                         (not gnus-dead-summary-mode))))
12647         (push buffer buffers)))
12648     ;; Go through all these summary buffers and offer to save them.
12649     (when buffers
12650       (save-excursion
12651         (if (eq gnus-interactive-exit 'quiet)
12652             (dolist (buffer buffers)
12653               (switch-to-buffer buffer)
12654               (gnus-summary-exit))
12655           (map-y-or-n-p
12656            "Update summary buffer %s? "
12657            (lambda (buf)
12658              (switch-to-buffer buf)
12659              (gnus-summary-exit))
12660            buffers))))))
12661
12662 (defun gnus-summary-setup-default-charset ()
12663   "Setup newsgroup default charset."
12664   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12665       (setq gnus-newsgroup-charset nil)
12666     (let* ((ignored-charsets
12667             (or gnus-newsgroup-ephemeral-ignored-charsets
12668                 (append
12669                  (and gnus-newsgroup-name
12670                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12671                  gnus-newsgroup-ignored-charsets))))
12672       (setq gnus-newsgroup-charset
12673             (or gnus-newsgroup-ephemeral-charset
12674                 (and gnus-newsgroup-name
12675                      (gnus-parameter-charset gnus-newsgroup-name))
12676                 gnus-default-charset))
12677       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12678            ignored-charsets))))
12679
12680 ;;;
12681 ;;; Mime Commands
12682 ;;;
12683
12684 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12685   "Display the current article buffer fully MIME-buttonized.
12686 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12687 treated as multipart/mixed."
12688   (interactive "P")
12689   (require 'gnus-art)
12690   (let ((gnus-unbuttonized-mime-types nil)
12691         (gnus-mime-display-multipart-as-mixed show-all-parts))
12692     (gnus-summary-show-article)))
12693
12694 (defun gnus-summary-repair-multipart (article)
12695   "Add a Content-Type header to a multipart article without one."
12696   (interactive (list (gnus-summary-article-number)))
12697   (gnus-with-article article
12698     (message-narrow-to-head)
12699     (message-remove-header "Mime-Version")
12700     (goto-char (point-max))
12701     (insert "Mime-Version: 1.0\n")
12702     (widen)
12703     (when (search-forward "\n--" nil t)
12704       (let ((separator (buffer-substring (point) (point-at-eol))))
12705         (message-narrow-to-head)
12706         (message-remove-header "Content-Type")
12707         (goto-char (point-max))
12708         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12709                         separator))
12710         (widen))))
12711   (let (gnus-mark-article-hook)
12712     (gnus-summary-select-article t t nil article)))
12713
12714 (defun gnus-summary-toggle-display-buttonized ()
12715   "Toggle the buttonizing of the article buffer."
12716   (interactive)
12717   (require 'gnus-art)
12718   (if (setq gnus-inhibit-mime-unbuttonizing
12719             (not gnus-inhibit-mime-unbuttonizing))
12720       (let ((gnus-unbuttonized-mime-types nil))
12721         (gnus-summary-show-article))
12722     (gnus-summary-show-article)))
12723
12724 ;;;
12725 ;;; Generic summary marking commands
12726 ;;;
12727
12728 (defvar gnus-summary-marking-alist
12729   '((read gnus-del-mark "d")
12730     (unread gnus-unread-mark "u")
12731     (ticked gnus-ticked-mark "!")
12732     (dormant gnus-dormant-mark "?")
12733     (expirable gnus-expirable-mark "e"))
12734   "An alist of names/marks/keystrokes.")
12735
12736 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12737 (defvar gnus-summary-mark-map)
12738
12739 (defun gnus-summary-make-all-marking-commands ()
12740   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12741   (dolist (elem gnus-summary-marking-alist)
12742     (apply 'gnus-summary-make-marking-command elem)))
12743
12744 (defun gnus-summary-make-marking-command (name mark keystroke)
12745   (let ((map (make-sparse-keymap)))
12746     (define-key gnus-summary-generic-mark-map keystroke map)
12747     (dolist (lway `((next "next" next nil "n")
12748                     (next-unread "next unread" next t "N")
12749                     (prev "previous" prev nil "p")
12750                     (prev-unread "previous unread" prev t "P")
12751                     (nomove "" nil nil ,keystroke)))
12752       (let ((func (gnus-summary-make-marking-command-1
12753                    mark (car lway) lway name)))
12754         (setq func (eval func))
12755         (define-key map (nth 4 lway) func)))))
12756
12757 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12758   `(defun ,(intern
12759             (format "gnus-summary-put-mark-as-%s%s"
12760                     name (if (eq way 'nomove)
12761                              ""
12762                            (concat "-" (symbol-name way)))))
12763      (n)
12764      ,(format
12765        "Mark the current article as %s%s.
12766 If N, the prefix, then repeat N times.
12767 If N is negative, move in reverse order.
12768 The difference between N and the actual number of articles marked is
12769 returned."
12770        name (cadr lway))
12771      (interactive "p")
12772      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12773
12774 (defun gnus-summary-generic-mark (n mark move unread)
12775   "Mark N articles with MARK."
12776   (unless (eq major-mode 'gnus-summary-mode)
12777     (error "This command can only be used in the summary buffer"))
12778   (gnus-summary-show-thread)
12779   (let ((nummove
12780          (cond
12781           ((eq move 'next) 1)
12782           ((eq move 'prev) -1)
12783           (t 0))))
12784     (if (zerop nummove)
12785         (setq n 1)
12786       (when (< n 0)
12787         (setq n (abs n)
12788               nummove (* -1 nummove))))
12789     (while (and (> n 0)
12790                 (gnus-summary-mark-article nil mark)
12791                 (zerop (gnus-summary-next-subject nummove unread t)))
12792       (setq n (1- n)))
12793     (when (/= 0 n)
12794       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12795     (gnus-summary-recenter)
12796     (gnus-summary-position-point)
12797     (gnus-set-mode-line 'summary)
12798     n))
12799
12800 (defun gnus-summary-insert-articles (articles)
12801   (when (setq articles
12802               (gnus-sorted-difference articles
12803                                       (mapcar (lambda (h)
12804                                                 (mail-header-number h))
12805                                               gnus-newsgroup-headers)))
12806     (setq gnus-newsgroup-headers
12807           (gnus-merge 'list
12808                       gnus-newsgroup-headers
12809                       (gnus-fetch-headers articles nil t)
12810                       'gnus-article-sort-by-number))
12811     (setq gnus-newsgroup-articles
12812           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12813     ;; Suppress duplicates?
12814     (when gnus-suppress-duplicates
12815       (gnus-dup-suppress-articles))
12816
12817     (if (and gnus-fetch-old-headers
12818              (eq gnus-headers-retrieved-by 'nov))
12819         ;; We might want to build some more threads first.
12820         (if (eq gnus-fetch-old-headers 'invisible)
12821             (gnus-build-all-threads)
12822           (gnus-build-old-threads))
12823       ;; Mark the inserted articles that are unread as unread.
12824       (setq gnus-newsgroup-unreads
12825             (gnus-sorted-nunion
12826              gnus-newsgroup-unreads
12827              (gnus-sorted-nintersection
12828               (gnus-list-of-unread-articles gnus-newsgroup-name)
12829               articles)))
12830       ;; Mark the inserted articles as selected so that the information
12831       ;; of the marks having been changed by a user may be updated when
12832       ;; exiting this group.  See `gnus-summary-update-info'.
12833       (dolist (art articles)
12834         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12835     ;; Let the Gnus agent mark articles as read.
12836     (when gnus-agent
12837       (gnus-agent-get-undownloaded-list))
12838     ;; Remove list identifiers from subject
12839     (gnus-summary-remove-list-identifiers)
12840     ;; First and last article in this newsgroup.
12841     (when gnus-newsgroup-headers
12842       (setq gnus-newsgroup-begin
12843             (mail-header-number (car gnus-newsgroup-headers))
12844             gnus-newsgroup-end
12845             (mail-header-number
12846              (gnus-last-element gnus-newsgroup-headers))))
12847     (when gnus-use-scoring
12848       (gnus-possibly-score-headers))))
12849
12850 (defun gnus-summary-insert-old-articles (&optional all)
12851   "Insert all old articles in this group.
12852 If ALL is non-nil, already read articles become readable.
12853 If ALL is a number, fetch this number of articles."
12854   (interactive "P")
12855   (prog1
12856       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12857             older len)
12858         (setq older
12859               ;; Some nntp servers lie about their active range.  When
12860               ;; this happens, the active range can be in the millions.
12861               ;; Use a compressed range to avoid creating a huge list.
12862               (gnus-range-difference
12863                (gnus-range-difference (list gnus-newsgroup-active) old)
12864                gnus-newsgroup-unexist))
12865         (setq len (gnus-range-length older))
12866         (cond
12867          ((null older) nil)
12868          ((numberp all)
12869           (if (< all len)
12870               (let ((older-range (nreverse older)))
12871                 (setq older nil)
12872
12873                 (while (> all 0)
12874                   (let* ((r (pop older-range))
12875                          (min (if (numberp r) r (car r)))
12876                          (max (if (numberp r) r (cdr r))))
12877                     (while (and (<= min max)
12878                                 (> all 0))
12879                       (push max older)
12880                       (setq all (1- all)
12881                             max (1- max))))))
12882             (setq older (gnus-uncompress-range older))))
12883          (all
12884           (setq older (gnus-uncompress-range older)))
12885          (t
12886           (when (and (numberp gnus-large-newsgroup)
12887                    (> len gnus-large-newsgroup))
12888               (let* ((cursor-in-echo-area nil)
12889                      (initial (gnus-parameter-large-newsgroup-initial
12890                                gnus-newsgroup-name))
12891                      (input
12892                       (read-string
12893                        (format
12894                         "How many articles from %s (%s %d): "
12895                         (gnus-group-decoded-name gnus-newsgroup-name)
12896                         (if initial "max" "default")
12897                         len)
12898                        nil nil
12899                        (and initial
12900                             (number-to-string initial)))))
12901                 (unless (string-match "^[ \t]*$" input)
12902                   (setq all (string-to-number input))
12903                   (if (< all len)
12904                       (let ((older-range (nreverse older)))
12905                         (setq older nil)
12906
12907                         (while (> all 0)
12908                           (let* ((r (pop older-range))
12909                                  (min (if (numberp r) r (car r)))
12910                                  (max (if (numberp r) r (cdr r))))
12911                             (while (and (<= min max)
12912                                         (> all 0))
12913                               (push max older)
12914                               (setq all (1- all)
12915                                     max (1- max))))))))))
12916           (setq older (gnus-uncompress-range older))))
12917         (if (not older)
12918             (message "No old news.")
12919           (gnus-summary-insert-articles older)
12920           (gnus-summary-limit (gnus-sorted-nunion old older))))
12921     (gnus-summary-position-point)))
12922
12923 (defun gnus-summary-insert-new-articles ()
12924   "Insert all new articles in this group."
12925   (interactive)
12926   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12927         (old-high gnus-newsgroup-highest)
12928         (nnmail-fetched-sources (list t))
12929         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12930         i new)
12931     (unless new-active
12932       (error "Couldn't fetch new data"))
12933     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12934     (setq i (cdr gnus-newsgroup-active)
12935           gnus-newsgroup-highest i)
12936     (while (> i old-high)
12937       (push i new)
12938       (decf i))
12939     (if (not new)
12940         (message "No gnus is bad news")
12941       (gnus-summary-insert-articles new)
12942       (setq gnus-newsgroup-unreads
12943             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12944       (gnus-summary-limit (gnus-sorted-nunion old new))))
12945   (gnus-summary-position-point))
12946
12947 ;;; Bookmark support for Gnus.
12948 (declare-function gnus-article-show-summary "gnus-art" ())
12949 (declare-function bookmark-make-record-default
12950                   "bookmark" (&optional no-file no-context posn))
12951 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12952 (declare-function bookmark-default-handler "bookmark" (bmk))
12953 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12954 (defvar bookmark-yank-point)
12955 (defvar bookmark-current-buffer)
12956
12957 (defun gnus-summary-bookmark-make-record ()
12958   "Make a bookmark entry for a Gnus summary buffer."
12959   (let (pos buf)
12960     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12961       (save-restriction              ; FIXME is it necessary to widen?
12962         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12963       (setq buf "art") ; We are recording bookmark from article buffer.
12964       (setq bookmark-yank-point (point))
12965       (setq bookmark-current-buffer (current-buffer))
12966       (gnus-article-show-summary))      ; Go back in summary buffer.
12967     ;; We are now recording bookmark from summary buffer.
12968     (unless buf (setq buf "sum"))
12969     (let* ((subject (elt (gnus-summary-article-header) 1))
12970            (grp     (car gnus-article-current))
12971            (art     (cdr gnus-article-current))
12972            (head    (gnus-summary-article-header art))
12973            (id      (mail-header-id head)))
12974       `(,subject
12975         ,@(condition-case nil
12976               (bookmark-make-record-default 'no-file 'no-context pos)
12977             (wrong-number-of-arguments
12978              (bookmark-make-record-default 'point-only)))
12979         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12980         (group . ,grp) (article . ,art)
12981         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12982
12983 ;;;###autoload
12984 (defun gnus-summary-bookmark-jump (bookmark)
12985   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12986 BOOKMARK is a bookmark name or a bookmark record."
12987   (let ((group    (bookmark-prop-get bookmark 'group))
12988         (article  (bookmark-prop-get bookmark 'article))
12989         (id       (bookmark-prop-get bookmark 'message-id))
12990         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12991     (gnus-fetch-group group (list article))
12992     (gnus-summary-insert-cached-articles)
12993     (gnus-summary-goto-article id nil 'force)
12994     ;; FIXME we have to wait article buffer is ready (only large buffer)
12995     ;; Is there a better solution to know that?
12996     ;; If we don't wait `bookmark-default-handler' will have no chance
12997     ;; to set position. However there is no error, just wrong pos.
12998     (sit-for 1)
12999     (when (string= buf "Gnus-art")
13000       (other-window 1))
13001     (bookmark-default-handler
13002      `(""
13003        (buffer . ,(current-buffer))
13004        . ,(bookmark-get-bookmark-record bookmark)))))
13005
13006 (gnus-summary-make-all-marking-commands)
13007
13008 (gnus-ems-redefine)
13009
13010 (provide 'gnus-sum)
13011
13012 (run-hooks 'gnus-sum-load-hook)
13013
13014 ;; Local Variables:
13015 ;; coding: utf-8
13016 ;; End:
13017
13018 ;;; gnus-sum.el ends here