614859bd7f40cba388c12389b091430779ca41cd
[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 (defcustom gnus-sort-threads-recursively t
851   "If non-nil, `gnus-thread-sort-functions' are applied recursively.
852 Setting this to nil allows sorting high-score, recent,
853 etc. threads to the top of the summary buffer while still
854 retaining chronological old to new sorting order inside threads."
855   :group 'gnus-summary-sort
856   :type 'boolean)
857
858 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
859   "*List of functions used for sorting threads in the summary buffer.
860 By default, threads are sorted by article number.
861
862 Each function takes two threads and returns non-nil if the first
863 thread should be sorted before the other.  If you use more than one
864 function, the primary sort function should be the last.  You should
865 probably always include `gnus-thread-sort-by-number' in the list of
866 sorting functions -- preferably first.  Also note that sorting by date
867 is often much slower than sorting by number, and the sorting order is
868 very similar.  (Sorting by date means sorting by the time the message
869 was sent, sorting by number means sorting by arrival time.)
870
871 Each list item can also be a list `(not F)' where F is a
872 function; this specifies reversed sort order.
873
874 Ready-made functions include `gnus-thread-sort-by-number',
875 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
876 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
877 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
878 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
879 and `gnus-thread-sort-by-total-score' (see
880 `gnus-thread-score-function').
881
882 When threading is turned off, the variable
883 `gnus-article-sort-functions' controls how articles are sorted."
884   :group 'gnus-summary-sort
885   :type '(repeat
886           (gnus-widget-reversible
887            (choice (function-item gnus-thread-sort-by-number)
888                    (function-item gnus-thread-sort-by-author)
889                    (function-item gnus-thread-sort-by-recipient)
890                    (function-item gnus-thread-sort-by-subject)
891                    (function-item gnus-thread-sort-by-date)
892                    (function-item gnus-thread-sort-by-score)
893                    (function-item gnus-thread-sort-by-most-recent-number)
894                    (function-item gnus-thread-sort-by-most-recent-date)
895                    (function-item gnus-thread-sort-by-random)
896                    (function-item gnus-thread-sort-by-total-score)
897                    (function :tag "other"))
898            (boolean :tag "Reverse order"))))
899
900 (defcustom gnus-thread-score-function '+
901   "*Function used for calculating the total score of a thread.
902
903 The function is called with the scores of the article and each
904 subthread and should then return the score of the thread.
905
906 Some functions you can use are `+', `max', or `min'."
907   :group 'gnus-summary-sort
908   :type 'function)
909
910 (defcustom gnus-summary-expunge-below nil
911   "All articles that have a score less than this variable will be expunged.
912 This variable is local to the summary buffers."
913   :group 'gnus-score-default
914   :type '(choice (const :tag "off" nil)
915                  integer))
916
917 (defcustom gnus-thread-expunge-below nil
918   "All threads that have a total score less than this variable will be expunged.
919 See `gnus-thread-score-function' for en explanation of what a
920 \"thread score\" is.
921
922 This variable is local to the summary buffers."
923   :group 'gnus-threading
924   :group 'gnus-score-default
925   :type '(choice (const :tag "off" nil)
926                  integer))
927
928 (defcustom gnus-summary-mode-hook nil
929   "*A hook for Gnus summary mode.
930 This hook is run before any variables are set in the summary buffer."
931   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
932   :group 'gnus-summary-various
933   :type 'hook)
934
935 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
936 (when (featurep 'xemacs)
937   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
938   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
939   (add-hook 'gnus-summary-mode-hook
940             'gnus-xmas-switch-horizontal-scrollbar-off))
941
942 (defcustom gnus-summary-menu-hook nil
943   "*Hook run after the creation of the summary mode menu."
944   :group 'gnus-summary-visual
945   :type 'hook)
946
947 (defcustom gnus-summary-exit-hook nil
948   "*A hook called on exit from the summary buffer.
949 It will be called with point in the group buffer."
950   :group 'gnus-summary-exit
951   :type 'hook)
952
953 (defcustom gnus-summary-prepare-hook nil
954   "*A hook called after the summary buffer has been generated.
955 If you want to modify the summary buffer, you can use this hook."
956   :group 'gnus-summary-various
957   :type 'hook)
958
959 (defcustom gnus-summary-prepared-hook nil
960   "*A hook called as the last thing after the summary buffer has been generated."
961   :group 'gnus-summary-various
962   :type 'hook)
963
964 (defcustom gnus-summary-generate-hook nil
965   "*A hook run just before generating the summary buffer.
966 This hook is commonly used to customize threading variables and the
967 like."
968   :group 'gnus-summary-various
969   :type 'hook)
970
971 (defcustom gnus-select-group-hook nil
972   "*A hook called when a newsgroup is selected.
973
974 If you'd like to simplify subjects like the
975 `gnus-summary-next-same-subject' command does, you can use the
976 following hook:
977
978  (add-hook gnus-select-group-hook
979            (lambda ()
980              (mapcar (lambda (header)
981                        (mail-header-set-subject
982                         header
983                         (gnus-simplify-subject
984                          (mail-header-subject header) 're-only)))
985                      gnus-newsgroup-headers)))"
986   :group 'gnus-group-select
987   :type 'hook)
988
989 (defcustom gnus-select-article-hook nil
990   "*A hook called when an article is selected."
991   :group 'gnus-summary-choose
992   :options '(gnus-agent-fetch-selected-article)
993   :type 'hook)
994
995 (defcustom gnus-visual-mark-article-hook
996   (list 'gnus-highlight-selected-summary)
997   "*Hook run after selecting an article in the summary buffer.
998 It is meant to be used for highlighting the article in some way.  It
999 is not run if `gnus-visual' is nil."
1000   :group 'gnus-summary-visual
1001   :type 'hook)
1002
1003 (defcustom gnus-parse-headers-hook nil
1004   "*A hook called before parsing the headers."
1005   :group 'gnus-various
1006   :type 'hook)
1007
1008 (defcustom gnus-exit-group-hook nil
1009   "*A hook called when exiting summary mode.
1010 This hook is not called from the non-updating exit commands like `Q'."
1011   :group 'gnus-various
1012   :type 'hook)
1013
1014 (defcustom gnus-summary-update-hook nil
1015   "*A hook called when a summary line is changed.
1016 The hook will not be called if `gnus-visual' is nil.
1017
1018 The default function `gnus-summary-highlight-line' will
1019 highlight the line according to the `gnus-summary-highlight'
1020 variable."
1021   :group 'gnus-summary-visual
1022   :type 'hook)
1023
1024 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1025   "*A hook called when an article is selected for the first time.
1026 The hook is intended to mark an article as read (or unread)
1027 automatically when it is selected."
1028   :group 'gnus-summary-choose
1029   :type 'hook)
1030
1031 (defcustom gnus-group-no-more-groups-hook nil
1032   "*A hook run when returning to group mode having no more (unread) groups."
1033   :group 'gnus-group-select
1034   :type 'hook)
1035
1036 (defcustom gnus-ps-print-hook nil
1037   "*A hook run before ps-printing something from Gnus."
1038   :group 'gnus-summary
1039   :type 'hook)
1040
1041 (defcustom gnus-summary-article-move-hook nil
1042   "*A hook called after an article is moved, copied, respooled, or crossposted."
1043   :version "22.1"
1044   :group 'gnus-summary
1045   :type 'hook)
1046
1047 (defcustom gnus-summary-article-delete-hook nil
1048   "*A hook called after an article is deleted."
1049   :version "22.1"
1050   :group 'gnus-summary
1051   :type 'hook)
1052
1053 (defcustom gnus-summary-article-expire-hook nil
1054   "*A hook called after an article is expired."
1055   :version "22.1"
1056   :group 'gnus-summary
1057   :type 'hook)
1058
1059 (defcustom gnus-summary-display-arrow
1060   (and (fboundp 'display-graphic-p)
1061        (display-graphic-p))
1062   "*If non-nil, display an arrow highlighting the current article."
1063   :version "22.1"
1064   :group 'gnus-summary
1065   :type 'boolean)
1066
1067 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1068   "Face used for highlighting the current article in the summary buffer."
1069   :group 'gnus-summary-visual
1070   :type 'face)
1071
1072 (defvar gnus-tmp-downloaded nil)
1073
1074 (defcustom gnus-summary-highlight
1075   '(((eq mark gnus-canceled-mark)
1076      . gnus-summary-cancelled)
1077     ((and uncached (> score default-high))
1078      . gnus-summary-high-undownloaded)
1079     ((and uncached (< score default-low))
1080      . gnus-summary-low-undownloaded)
1081     (uncached
1082      . gnus-summary-normal-undownloaded)
1083     ((and (> score default-high)
1084           (or (eq mark gnus-dormant-mark)
1085               (eq mark gnus-ticked-mark)))
1086      . gnus-summary-high-ticked)
1087     ((and (< score default-low)
1088           (or (eq mark gnus-dormant-mark)
1089               (eq mark gnus-ticked-mark)))
1090      . gnus-summary-low-ticked)
1091     ((or (eq mark gnus-dormant-mark)
1092          (eq mark gnus-ticked-mark))
1093      . gnus-summary-normal-ticked)
1094     ((and (> score default-high) (eq mark gnus-ancient-mark))
1095      . gnus-summary-high-ancient)
1096     ((and (< score default-low) (eq mark gnus-ancient-mark))
1097      . gnus-summary-low-ancient)
1098     ((eq mark gnus-ancient-mark)
1099      . gnus-summary-normal-ancient)
1100     ((and (> score default-high) (eq mark gnus-unread-mark))
1101      . gnus-summary-high-unread)
1102     ((and (< score default-low) (eq mark gnus-unread-mark))
1103      . gnus-summary-low-unread)
1104     ((eq mark gnus-unread-mark)
1105      . gnus-summary-normal-unread)
1106     ((> score default-high)
1107      . gnus-summary-high-read)
1108     ((< score default-low)
1109      . gnus-summary-low-read)
1110     (t
1111      . gnus-summary-normal-read))
1112   "*Controls the highlighting of summary buffer lines.
1113
1114 A list of (FORM . FACE) pairs.  When deciding how a particular
1115 summary line should be displayed, each form is evaluated.  The content
1116 of the face field after the first true form is used.  You can change
1117 how those summary lines are displayed, by editing the face field.
1118
1119 You can use the following variables in the FORM field.
1120
1121 score:        The article's score.
1122 default:      The default article score.
1123 default-high: The default score for high scored articles.
1124 default-low:  The default score for low scored articles.
1125 below:        The score below which articles are automatically marked as read.
1126 mark:         The article's mark.
1127 uncached:     Non-nil if the article is uncached."
1128   :group 'gnus-summary-visual
1129   :type '(repeat (cons (sexp :tag "Form" nil)
1130                        face)))
1131 (put 'gnus-summary-highlight 'risky-local-variable t)
1132
1133 (defcustom gnus-alter-header-function nil
1134   "Function called to allow alteration of article header structures.
1135 The function is called with one parameter, the article header vector,
1136 which it may alter in any way."
1137   :type '(choice (const :tag "None" nil)
1138                  function)
1139   :group 'gnus-summary)
1140
1141 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1142   "Function used to decode a string with encoded words.")
1143
1144 (defvar gnus-decode-encoded-address-function
1145   'mail-decode-encoded-address-string
1146   "Function used to decode addresses with encoded words.")
1147
1148 (defcustom gnus-extra-headers '(To Cc Keywords Gcc Newsgroups)
1149   "*Extra headers to parse."
1150   :version "24.1"                       ; added Cc Keywords Gcc
1151   :group 'gnus-summary
1152   :type '(repeat symbol))
1153
1154 (defcustom gnus-ignored-from-addresses
1155   (and user-mail-address
1156        (not (string= user-mail-address ""))
1157        (regexp-quote user-mail-address))
1158   "*From headers that may be suppressed in favor of To headers.
1159 This can be a regexp or a list of regexps."
1160   :version "21.1"
1161   :group 'gnus-summary
1162   :type '(choice regexp
1163                  (repeat :tag "Regexp List" regexp)))
1164
1165 (defsubst gnus-ignored-from-addresses ()
1166   (gmm-regexp-concat gnus-ignored-from-addresses))
1167
1168 (defcustom gnus-summary-to-prefix "-> "
1169   "*String prefixed to the To field in the summary line when
1170 using `gnus-ignored-from-addresses'."
1171   :version "22.1"
1172   :group 'gnus-summary
1173   :type 'string)
1174
1175 (defcustom gnus-summary-newsgroup-prefix "=> "
1176   "*String prefixed to the Newsgroup field in the summary
1177 line when using the option `gnus-ignored-from-addresses'."
1178   :version "22.1"
1179   :group 'gnus-summary
1180   :type 'string)
1181
1182 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1183   "List of charsets that should be ignored.
1184 When these charsets are used in the \"charset\" parameter, the
1185 default charset will be used instead."
1186   :version "21.1"
1187   :type '(repeat symbol)
1188   :group 'gnus-charset)
1189
1190 (defcustom gnus-newsgroup-maximum-articles nil
1191   "The maximum number of articles a newsgroup.
1192 If this is a number, old articles in a newsgroup exceeding this number
1193 are silently ignored.  If it is nil, no article is ignored.  Note that
1194 setting this variable to a number might prevent you from reading very
1195 old articles."
1196   :group 'gnus-group-select
1197   :version "22.2"
1198   :type '(choice (const :tag "No limit" nil)
1199                  integer))
1200
1201 (gnus-define-group-parameter
1202  ignored-charsets
1203  :type list
1204  :function-document
1205  "Return the ignored charsets of GROUP."
1206  :variable gnus-group-ignored-charsets-alist
1207  :variable-default
1208  '(("alt\\.chinese\\.text" iso-8859-1))
1209  :variable-document
1210  "Alist of regexps (to match group names) and charsets that should be ignored.
1211 When these charsets are used in the \"charset\" parameter, the
1212 default charset will be used instead."
1213  :variable-group gnus-charset
1214  :variable-type '(repeat (cons (regexp :tag "Group")
1215                                (repeat symbol)))
1216  :parameter-type '(choice :tag "Ignored charsets"
1217                           :value nil
1218                           (repeat (symbol)))
1219  :parameter-document       "\
1220 List of charsets that should be ignored.
1221
1222 When these charsets are used in the \"charset\" parameter, the
1223 default charset will be used instead.")
1224
1225 (defcustom gnus-group-highlight-words-alist nil
1226   "Alist of group regexps and highlight regexps.
1227 This variable uses the same syntax as `gnus-emphasis-alist'."
1228   :version "21.1"
1229   :type '(repeat (cons (regexp :tag "Group")
1230                        (repeat (list (regexp :tag "Highlight regexp")
1231                                      (number :tag "Group for entire word" 0)
1232                                      (number :tag "Group for displayed part" 0)
1233                                      (symbol :tag "Face"
1234                                              gnus-emphasis-highlight-words)))))
1235   :group 'gnus-summary-visual)
1236
1237 (defcustom gnus-summary-show-article-charset-alist
1238   nil
1239   "Alist of number and charset.
1240 The article will be shown with the charset corresponding to the
1241 numbered argument.
1242 For example: ((1 . cn-gb-2312) (2 . big5))."
1243   :version "21.1"
1244   :type '(repeat (cons (number :tag "Argument" 1)
1245                        (symbol :tag "Charset")))
1246   :group 'gnus-charset)
1247
1248 (defcustom gnus-preserve-marks t
1249   "Whether marks are preserved when moving, copying and respooling messages."
1250   :version "21.1"
1251   :type 'boolean
1252   :group 'gnus-summary-marks)
1253
1254 (defcustom gnus-alter-articles-to-read-function nil
1255   "Function to be called to alter the list of articles to be selected."
1256   :type '(choice (const nil) function)
1257   :group 'gnus-summary)
1258
1259 (defcustom gnus-orphan-score nil
1260   "*All orphans get this score added.  Set in the score file."
1261   :group 'gnus-score-default
1262   :type '(choice (const nil)
1263                  integer))
1264
1265 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1266   "*A regexp to match MIME parts when saving multiple parts of a
1267 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1268 This regexp will be used by default when prompting the user for which
1269 type of files to save."
1270   :group 'gnus-summary
1271   :type 'regexp)
1272
1273 (defcustom gnus-read-all-available-headers nil
1274   "Whether Gnus should parse all headers made available to it.
1275 This is mostly relevant for slow back ends where the user may
1276 wish to widen the summary buffer to include all headers
1277 that were fetched."
1278   :version "22.1"
1279   :group 'gnus-summary
1280   :type '(choice boolean regexp))
1281
1282 (defcustom gnus-summary-pipe-output-default-command nil
1283   "Command (and optional arguments) used to pipe article to subprocess.
1284 This will be used as the default command if it is non-nil.  The value
1285 will be updated if you modify it when executing the command
1286 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1287   :version "23.1" ;; No Gnus
1288   :group 'gnus-summary
1289   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1290
1291 (defcustom gnus-summary-muttprint-program "muttprint"
1292   "Command (and optional arguments) used to run Muttprint.
1293 The value will be updated if you modify it when executing the command
1294 `gnus-summary-muttprint'."
1295   :version "22.1"
1296   :group 'gnus-summary
1297   :type 'string)
1298
1299 (defcustom gnus-article-loose-mime t
1300   "If non-nil, don't require MIME-Version header.
1301 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1302 supply the MIME-Version header or deliberately strip it from the mail.
1303 If non-nil (the default), Gnus will treat some articles as MIME
1304 even if the MIME-Version header is missing."
1305   :version "22.1"
1306   :type 'boolean
1307   :group 'gnus-article-mime)
1308
1309 (defcustom gnus-article-emulate-mime t
1310   "If non-nil, use MIME emulation for uuencode and the like.
1311 This means that Gnus will search message bodies for text that look
1312 like uuencoded bits, yEncoded bits, and so on, and present that using
1313 the normal Gnus MIME machinery."
1314   :version "22.1"
1315   :type 'boolean
1316   :group 'gnus-article-mime)
1317
1318 ;;; Internal variables
1319
1320 (defvar gnus-summary-display-cache nil)
1321 (defvar gnus-article-mime-handles nil)
1322 (defvar gnus-article-decoded-p nil)
1323 (defvar gnus-article-charset nil)
1324 (defvar gnus-article-ignored-charsets nil)
1325 (defvar gnus-scores-exclude-files nil)
1326 (defvar gnus-page-broken nil)
1327
1328 (defvar gnus-original-article nil)
1329 (defvar gnus-article-internal-prepare-hook nil)
1330 (defvar gnus-newsgroup-process-stack nil)
1331
1332 (defvar gnus-thread-indent-array nil)
1333 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1334 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1335   "Function called to sort the articles within a thread after it has been gathered together.")
1336
1337 (defvar gnus-summary-save-parts-type-history nil)
1338 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1339
1340 ;; Avoid highlighting in kill files.
1341 (defvar gnus-summary-inhibit-highlight nil)
1342 (defvar gnus-newsgroup-selected-overlay nil)
1343 (defvar gnus-inhibit-limiting nil)
1344 (defvar gnus-newsgroup-adaptive-score-file nil)
1345 (defvar gnus-current-score-file nil)
1346 (defvar gnus-current-move-group nil)
1347 (defvar gnus-current-copy-group nil)
1348 (defvar gnus-current-crosspost-group nil)
1349 (defvar gnus-newsgroup-display nil)
1350
1351 (defvar gnus-newsgroup-dependencies nil)
1352 (defvar gnus-newsgroup-adaptive nil)
1353 (defvar gnus-summary-display-article-function nil)
1354 (defvar gnus-summary-highlight-line-function nil
1355   "Function called after highlighting a summary line.")
1356
1357 (defvar gnus-summary-line-format-alist
1358   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1359     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1360     (?s gnus-tmp-subject-or-nil ?s)
1361     (?n gnus-tmp-name ?s)
1362     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1363         ?s)
1364     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1365             gnus-tmp-from) ?s)
1366     (?F gnus-tmp-from ?s)
1367     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1368     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1369     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1370     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1371     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1372     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1373     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1374     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1375     (?L gnus-tmp-lines ?s)
1376     (?Z (or (nnir-article-rsv (mail-header-number gnus-tmp-header))
1377             0) ?d)
1378     (?G (or (nnir-article-group (mail-header-number gnus-tmp-header))
1379             "") ?s)
1380     (?g (or (gnus-group-short-name
1381              (nnir-article-group (mail-header-number gnus-tmp-header)))
1382             "") ?s)
1383     (?O gnus-tmp-downloaded ?c)
1384     (?I gnus-tmp-indentation ?s)
1385     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1386     (?R gnus-tmp-replied ?c)
1387     (?\[ gnus-tmp-opening-bracket ?c)
1388     (?\] gnus-tmp-closing-bracket ?c)
1389     (?\> (make-string gnus-tmp-level ? ) ?s)
1390     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1391     (?i gnus-tmp-score ?d)
1392     (?z gnus-tmp-score-char ?c)
1393     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1394     (?U gnus-tmp-unread ?c)
1395     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1396         ?s)
1397     (?t (gnus-summary-number-of-articles-in-thread
1398          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1399         ?d)
1400     (?e (gnus-summary-number-of-articles-in-thread
1401          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1402         ?c)
1403     (?u gnus-tmp-user-defined ?s)
1404     (?P (gnus-pick-line-number) ?d)
1405     (?B gnus-tmp-thread-tree-header-string ?s)
1406     (user-date (gnus-user-date
1407                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1408   "An alist of format specifications that can appear in summary lines.
1409 These are paired with what variables they correspond with, along with
1410 the type of the variable (string, integer, character, etc).")
1411
1412 (defvar gnus-summary-dummy-line-format-alist
1413   `((?S gnus-tmp-subject ?s)
1414     (?N gnus-tmp-number ?d)
1415     (?u gnus-tmp-user-defined ?s)))
1416
1417 (defvar gnus-summary-mode-line-format-alist
1418   `((?G gnus-tmp-group-name ?s)
1419     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1420     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1421     (?A gnus-tmp-article-number ?d)
1422     (?Z gnus-tmp-unread-and-unselected ?s)
1423     (?V gnus-version ?s)
1424     (?U gnus-tmp-unread-and-unticked ?d)
1425     (?S gnus-tmp-subject ?s)
1426     (?e gnus-tmp-unselected ?d)
1427     (?u gnus-tmp-user-defined ?s)
1428     (?d (length gnus-newsgroup-dormant) ?d)
1429     (?t (length gnus-newsgroup-marked) ?d)
1430     (?h (length gnus-newsgroup-spam-marked) ?d)
1431     (?r (length gnus-newsgroup-reads) ?d)
1432     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1433     (?E gnus-newsgroup-expunged-tally ?d)
1434     (?s (gnus-current-score-file-nondirectory) ?s)))
1435
1436 ;; This is here rather than in gnus-art for compilation reasons.
1437 (defvar gnus-article-mode-line-format-alist
1438   (nconc '((?w (gnus-article-wash-status) ?s)
1439            (?m (gnus-article-mime-part-status) ?s))
1440          gnus-summary-mode-line-format-alist))
1441
1442 (defvar gnus-last-search-regexp nil
1443   "Default regexp for article search command.")
1444
1445 (defvar gnus-last-shell-command nil
1446   "Default shell command on article.")
1447
1448 (defvar gnus-newsgroup-agentized nil
1449   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1450 (defvar gnus-newsgroup-begin nil)
1451 (defvar gnus-newsgroup-end nil)
1452 (defvar gnus-newsgroup-last-rmail nil)
1453 (defvar gnus-newsgroup-last-mail nil)
1454 (defvar gnus-newsgroup-last-folder nil)
1455 (defvar gnus-newsgroup-last-file nil)
1456 (defvar gnus-newsgroup-last-directory nil)
1457 (defvar gnus-newsgroup-auto-expire nil)
1458 (defvar gnus-newsgroup-active nil)
1459 (defvar gnus-newsgroup-highest nil)
1460
1461 (defvar gnus-newsgroup-data nil)
1462 (defvar gnus-newsgroup-data-reverse nil)
1463 (defvar gnus-newsgroup-limit nil)
1464 (defvar gnus-newsgroup-limits nil)
1465 (defvar gnus-summary-use-undownloaded-faces nil)
1466
1467 (defvar gnus-newsgroup-unreads nil
1468   "Sorted list of unread articles in the current newsgroup.")
1469
1470 (defvar gnus-newsgroup-unselected nil
1471   "Sorted list of unselected unread articles in the current newsgroup.")
1472
1473 (defvar gnus-newsgroup-reads nil
1474   "Alist of read articles and article marks in the current newsgroup.")
1475
1476 (defvar gnus-newsgroup-expunged-tally nil)
1477
1478 (defvar gnus-newsgroup-marked nil
1479   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1480
1481 (defvar gnus-newsgroup-spam-marked nil
1482   "List of ranges of articles that have been marked as spam.")
1483
1484 (defvar gnus-newsgroup-killed nil
1485   "List of ranges of articles that have been through the scoring process.")
1486
1487 (defvar gnus-newsgroup-cached nil
1488   "Sorted list of articles that come from the article cache.")
1489
1490 (defvar gnus-newsgroup-saved nil
1491   "List of articles that have been saved.")
1492
1493 (defvar gnus-newsgroup-kill-headers nil)
1494
1495 (defvar gnus-newsgroup-replied nil
1496   "List of articles that have been replied to in the current newsgroup.")
1497
1498 (defvar gnus-newsgroup-forwarded nil
1499   "List of articles that have been forwarded in the current newsgroup.")
1500
1501 (defvar gnus-newsgroup-expirable nil
1502   "Sorted list of articles in the current newsgroup that can be expired.")
1503
1504 (defvar gnus-newsgroup-processable nil
1505   "List of articles in the current newsgroup that can be processed.")
1506
1507 (defvar gnus-newsgroup-downloadable nil
1508   "Sorted list of articles in the current newsgroup that can be processed.")
1509
1510 (defvar gnus-newsgroup-unfetched nil
1511   "Sorted list of articles in the current newsgroup whose headers have
1512 not been fetched into the agent.
1513
1514 This list will always be a subset of gnus-newsgroup-undownloaded.")
1515
1516 (defvar gnus-newsgroup-undownloaded nil
1517   "List of articles in the current newsgroup that haven't been downloaded.")
1518
1519 (defvar gnus-newsgroup-unsendable nil
1520   "List of articles in the current newsgroup that won't be sent.")
1521
1522 (defvar gnus-newsgroup-bookmarks nil
1523   "List of articles in the current newsgroup that have bookmarks.")
1524
1525 (defvar gnus-newsgroup-dormant nil
1526   "Sorted list of dormant articles in the current newsgroup.")
1527
1528 (defvar gnus-newsgroup-unseen nil
1529   "List of unseen articles in the current newsgroup.")
1530
1531 (defvar gnus-newsgroup-seen nil
1532   "Range of seen articles in the current newsgroup.")
1533
1534 (defvar gnus-newsgroup-unexist nil
1535   "Range of unexisting articles in the current newsgroup.")
1536
1537 (defvar gnus-newsgroup-articles nil
1538   "List of articles in the current newsgroup.")
1539
1540 (defvar gnus-newsgroup-scored nil
1541   "List of scored articles in the current newsgroup.")
1542
1543 (defvar gnus-newsgroup-headers nil
1544   "List of article headers in the current newsgroup.")
1545
1546 (defvar gnus-newsgroup-threads nil)
1547
1548 (defvar gnus-newsgroup-prepared nil
1549   "Whether the current group has been prepared properly.")
1550
1551 (defvar gnus-newsgroup-ancient nil
1552   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1553
1554 (defvar gnus-newsgroup-sparse nil)
1555
1556 (defvar gnus-current-article nil)
1557 (defvar gnus-article-current nil)
1558 (defvar gnus-current-headers nil)
1559 (defvar gnus-have-all-headers nil)
1560 (defvar gnus-last-article nil)
1561 (defvar gnus-newsgroup-history nil)
1562 (defvar gnus-newsgroup-charset nil)
1563 (defvar gnus-newsgroup-ephemeral-charset nil)
1564 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1565
1566 (defvar gnus-article-before-search nil)
1567
1568 (defvar gnus-summary-local-variables
1569   '(gnus-newsgroup-name
1570
1571     ;; Marks lists
1572     gnus-newsgroup-unreads
1573     gnus-newsgroup-unselected
1574     gnus-newsgroup-marked
1575     gnus-newsgroup-spam-marked
1576     gnus-newsgroup-reads
1577     gnus-newsgroup-saved
1578     gnus-newsgroup-replied
1579     gnus-newsgroup-forwarded
1580     gnus-newsgroup-expirable
1581     gnus-newsgroup-killed
1582     gnus-newsgroup-unseen
1583     gnus-newsgroup-seen
1584     gnus-newsgroup-unexist
1585     gnus-newsgroup-cached
1586     gnus-newsgroup-downloadable
1587     gnus-newsgroup-undownloaded
1588     gnus-newsgroup-unsendable
1589
1590     gnus-newsgroup-begin gnus-newsgroup-end
1591     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1592     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1593     gnus-newsgroup-last-directory
1594     gnus-newsgroup-auto-expire
1595     gnus-newsgroup-processable
1596     gnus-newsgroup-unfetched
1597     gnus-newsgroup-articles
1598     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1599     gnus-newsgroup-headers gnus-newsgroup-threads
1600     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1601     gnus-current-article gnus-current-headers gnus-have-all-headers
1602     gnus-last-article gnus-article-internal-prepare-hook
1603     (gnus-summary-article-delete-hook . global)
1604     (gnus-summary-article-move-hook . global)
1605     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1606     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1607     gnus-thread-expunge-below
1608     gnus-score-alist gnus-current-score-file
1609     (gnus-summary-expunge-below . global)
1610     (gnus-summary-mark-below . global)
1611     (gnus-orphan-score . global)
1612     gnus-newsgroup-active gnus-scores-exclude-files
1613     gnus-newsgroup-highest
1614     gnus-newsgroup-history gnus-newsgroup-ancient
1615     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1616     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1617     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1618     (gnus-newsgroup-expunged-tally . 0)
1619     gnus-cache-removable-articles
1620     gnus-newsgroup-data gnus-newsgroup-data-reverse
1621     gnus-newsgroup-limit gnus-newsgroup-limits
1622     gnus-newsgroup-charset gnus-newsgroup-display
1623     gnus-summary-use-undownloaded-faces)
1624   "Variables that are buffer-local to the summary buffers.")
1625
1626 (defvar gnus-newsgroup-variables nil
1627   "A list of variables that have separate values in different newsgroups.
1628 A list of newsgroup (summary buffer) local variables, or cons of
1629 variables and their default expressions to be evalled (when the default
1630 values are not nil), that should be made global while the summary buffer
1631 is active.
1632
1633 Note: The default expressions will be evaluated (using function `eval')
1634 before assignment to the local variable rather than just assigned to it.
1635 If the default expression is the symbol `global', that symbol will not
1636 be evaluated but the global value of the local variable will be used
1637 instead.
1638
1639 These variables can be used to set variables in the group parameters
1640 while still allowing them to affect operations done in other buffers.
1641 For example:
1642
1643 \(setq gnus-newsgroup-variables
1644      '(message-use-followup-to
1645        (gnus-visible-headers .
1646          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1647 ")
1648
1649 (eval-when-compile
1650   ;; Bind features so that require will believe that gnus-sum has
1651   ;; already been loaded (avoids infinite recursion)
1652   (let ((features (cons 'gnus-sum features)))
1653     (require 'gnus-art)))
1654
1655 ;; MIME stuff.
1656
1657 (defvar gnus-decode-encoded-word-methods
1658   '(mail-decode-encoded-word-string)
1659   "List of methods used to decode encoded words.
1660
1661 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1662 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1663 \(REGEXP . FUNCTION), FUNCTION will be applied only to the newsgroups
1664 whose names match REGEXP.
1665
1666 For example:
1667 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1668  mail-decode-encoded-word-string
1669  (\"chinese\" . rfc1843-decode-string))")
1670
1671 (defvar gnus-decode-encoded-word-methods-cache nil)
1672
1673 (defun gnus-multi-decode-encoded-word-string (string)
1674   "Apply the functions from `gnus-encoded-word-methods' that match."
1675   (unless (and gnus-decode-encoded-word-methods-cache
1676                (eq gnus-newsgroup-name
1677                    (car gnus-decode-encoded-word-methods-cache)))
1678     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1679     (dolist (method gnus-decode-encoded-word-methods)
1680       (if (symbolp method)
1681           (nconc gnus-decode-encoded-word-methods-cache (list method))
1682         (if (and gnus-newsgroup-name
1683                  (string-match (car method) gnus-newsgroup-name))
1684             (nconc gnus-decode-encoded-word-methods-cache
1685                    (list (cdr method)))))))
1686   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1687     (setq string (funcall method string))))
1688
1689 ;; Subject simplification.
1690
1691 (defun gnus-simplify-whitespace (str)
1692   "Remove excessive whitespace from STR."
1693   ;; Multiple spaces.
1694   (while (string-match "[ \t][ \t]+" str)
1695     (setq str (concat (substring str 0 (match-beginning 0))
1696                         " "
1697                         (substring str (match-end 0)))))
1698   ;; Leading spaces.
1699   (when (string-match "^[ \t]+" str)
1700     (setq str (substring str (match-end 0))))
1701   ;; Trailing spaces.
1702   (when (string-match "[ \t]+$" str)
1703     (setq str (substring str 0 (match-beginning 0))))
1704   str)
1705
1706 (defun gnus-simplify-all-whitespace (str)
1707   "Remove all whitespace from STR."
1708   (while (string-match "[ \t\n]+" str)
1709     (setq str (replace-match "" nil nil str)))
1710   str)
1711
1712 (defsubst gnus-simplify-subject-re (subject)
1713   "Remove \"Re:\" from subject lines."
1714   (if (string-match message-subject-re-regexp subject)
1715       (substring subject (match-end 0))
1716     subject))
1717
1718 (defun gnus-simplify-subject (subject &optional re-only)
1719   "Remove `Re:' and words in parentheses.
1720 If RE-ONLY is non-nil, strip leading `Re:'s only."
1721   (let ((case-fold-search t))           ;Ignore case.
1722     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1723     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1724       (setq subject (substring subject (match-end 0))))
1725     ;; Remove uninteresting prefixes.
1726     (when (and (not re-only)
1727                gnus-simplify-ignored-prefixes
1728                (string-match gnus-simplify-ignored-prefixes subject))
1729       (setq subject (substring subject (match-end 0))))
1730     ;; Remove words in parentheses from end.
1731     (unless re-only
1732       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1733         (setq subject (substring subject 0 (match-beginning 0)))))
1734     ;; Return subject string.
1735     subject))
1736
1737 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1738 ;; all whitespace.
1739 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1740   (goto-char (point-min))
1741   (while (re-search-forward regexp nil t)
1742     (replace-match (or newtext ""))))
1743
1744 (defun gnus-simplify-buffer-fuzzy (regexp)
1745   "Simplify string in the buffer fuzzily.
1746 The string in the accessible portion of the current buffer is simplified.
1747 It is assumed to be a single-line subject.
1748 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1749 matter is removed.  Additional things can be deleted by setting
1750 `gnus-simplify-subject-fuzzy-regexp'."
1751   (let ((case-fold-search t)
1752         (modified-tick))
1753     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1754
1755     (while (not (eq modified-tick (buffer-modified-tick)))
1756       (setq modified-tick (buffer-modified-tick))
1757       (cond
1758        ((listp regexp)
1759         (mapc 'gnus-simplify-buffer-fuzzy-step regexp))
1760        (regexp
1761         (gnus-simplify-buffer-fuzzy-step regexp)))
1762       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1763       (gnus-simplify-buffer-fuzzy-step
1764        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1765       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1766
1767     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1768     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1769     (gnus-simplify-buffer-fuzzy-step " $")
1770     (gnus-simplify-buffer-fuzzy-step "^ +")))
1771
1772 (defun gnus-simplify-subject-fuzzy (subject)
1773   "Simplify a subject string fuzzily.
1774 See `gnus-simplify-buffer-fuzzy' for details."
1775   (save-excursion
1776     (let ((regexp gnus-simplify-subject-fuzzy-regexp))
1777       (gnus-set-work-buffer)
1778       (let ((case-fold-search t))
1779         ;; Remove uninteresting prefixes.
1780         (when (and gnus-simplify-ignored-prefixes
1781                    (string-match gnus-simplify-ignored-prefixes subject))
1782           (setq subject (substring subject (match-end 0))))
1783         (insert subject)
1784         (inline (gnus-simplify-buffer-fuzzy regexp))
1785         (buffer-string)))))
1786
1787 (defsubst gnus-simplify-subject-fully (subject)
1788   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1789   (cond
1790    (gnus-simplify-subject-functions
1791     (gnus-map-function gnus-simplify-subject-functions subject))
1792    ((null gnus-summary-gather-subject-limit)
1793     (gnus-simplify-subject-re subject))
1794    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1795     (gnus-simplify-subject-fuzzy subject))
1796    ((numberp gnus-summary-gather-subject-limit)
1797     (truncate-string-to-width (gnus-simplify-subject-re subject)
1798                               gnus-summary-gather-subject-limit))
1799    (t
1800     subject)))
1801
1802 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1803   "Check whether two subjects are equal.
1804 If optional argument SIMPLE-FIRST is t, first argument is already
1805 simplified."
1806   (cond
1807    ((null simple-first)
1808     (equal (gnus-simplify-subject-fully s1)
1809            (gnus-simplify-subject-fully s2)))
1810    (t
1811     (equal s1
1812            (gnus-simplify-subject-fully s2)))))
1813
1814 (defun gnus-summary-bubble-group ()
1815   "Increase the score of the current group.
1816 This is a handy function to add to `gnus-summary-exit-hook' to
1817 increase the score of each group you read."
1818   (gnus-group-add-score gnus-newsgroup-name))
1819
1820 \f
1821 ;;;
1822 ;;; Gnus summary mode
1823 ;;;
1824
1825 (put 'gnus-summary-mode 'mode-class 'special)
1826
1827 (defvar gnus-article-commands-menu)
1828
1829 ;; Non-orthogonal keys
1830
1831 (gnus-define-keys gnus-summary-mode-map
1832   " " gnus-summary-next-page
1833   [?\S-\ ] gnus-summary-prev-page
1834   "\177" gnus-summary-prev-page
1835   [delete] gnus-summary-prev-page
1836   [backspace] gnus-summary-prev-page
1837   "\r" gnus-summary-scroll-up
1838   "\M-\r" gnus-summary-scroll-down
1839   "n" gnus-summary-next-unread-article
1840   "p" gnus-summary-prev-unread-article
1841   "N" gnus-summary-next-article
1842   "P" gnus-summary-prev-article
1843   "\M-\C-n" gnus-summary-next-same-subject
1844   "\M-\C-p" gnus-summary-prev-same-subject
1845   "\M-n" gnus-summary-next-unread-subject
1846   "\M-p" gnus-summary-prev-unread-subject
1847   "." gnus-summary-first-unread-article
1848   "," gnus-summary-best-unread-article
1849   "\M-s" gnus-summary-search-article-forward
1850   "\M-r" gnus-summary-search-article-backward
1851   "\M-S" gnus-summary-repeat-search-article-forward
1852   "\M-R" gnus-summary-repeat-search-article-backward
1853   "<" gnus-summary-beginning-of-article
1854   ">" gnus-summary-end-of-article
1855   "j" gnus-summary-goto-article
1856   "^" gnus-summary-refer-parent-article
1857   "\M-^" gnus-summary-refer-article
1858   "u" gnus-summary-tick-article-forward
1859   "!" gnus-summary-tick-article-forward
1860   "U" gnus-summary-tick-article-backward
1861   "d" gnus-summary-mark-as-read-forward
1862   "D" gnus-summary-mark-as-read-backward
1863   "E" gnus-summary-mark-as-expirable
1864   "\M-u" gnus-summary-clear-mark-forward
1865   "\M-U" gnus-summary-clear-mark-backward
1866   "k" gnus-summary-kill-same-subject-and-select
1867   "\C-k" gnus-summary-kill-same-subject
1868   "\M-\C-k" gnus-summary-kill-thread
1869   "\M-\C-l" gnus-summary-lower-thread
1870   "e" gnus-summary-edit-article
1871   "#" gnus-summary-mark-as-processable
1872   "\M-#" gnus-summary-unmark-as-processable
1873   "\M-\C-t" gnus-summary-toggle-threads
1874   "\M-\C-s" gnus-summary-show-thread
1875   "\M-\C-h" gnus-summary-hide-thread
1876   "\M-\C-f" gnus-summary-next-thread
1877   "\M-\C-b" gnus-summary-prev-thread
1878   [(meta down)] gnus-summary-next-thread
1879   [(meta up)] gnus-summary-prev-thread
1880   "\M-\C-u" gnus-summary-up-thread
1881   "\M-\C-d" gnus-summary-down-thread
1882   "&" gnus-summary-execute-command
1883   "c" gnus-summary-catchup-and-exit
1884   "\C-w" gnus-summary-mark-region-as-read
1885   "\C-t" gnus-summary-toggle-truncation
1886   "?" gnus-summary-mark-as-dormant
1887   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1888   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1889   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1890   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1891   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1892   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1893   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1894   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1895   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1896   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1897   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1898   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1899   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1900   "=" gnus-summary-expand-window
1901   "\C-x\C-s" gnus-summary-reselect-current-group
1902   "\M-g" gnus-summary-rescan-group
1903   "\C-c\C-r" gnus-summary-caesar-message
1904   "f" gnus-summary-followup
1905   "F" gnus-summary-followup-with-original
1906   "C" gnus-summary-cancel-article
1907   "r" gnus-summary-reply
1908   "R" gnus-summary-reply-with-original
1909   "\C-c\C-f" gnus-summary-mail-forward
1910   "o" gnus-summary-save-article
1911   "\C-o" gnus-summary-save-article-mail
1912   "|" gnus-summary-pipe-output
1913   "\M-k" gnus-summary-edit-local-kill
1914   "\M-K" gnus-summary-edit-global-kill
1915   ;; "V" gnus-version
1916   "\C-c\C-d" gnus-summary-describe-group
1917   "q" gnus-summary-exit
1918   "Q" gnus-summary-exit-no-update
1919   "\C-c\C-i" gnus-info-find-node
1920   gnus-mouse-2 gnus-mouse-pick-article
1921   [follow-link] mouse-face
1922   "m" gnus-summary-mail-other-window
1923   "a" gnus-summary-post-news
1924   "x" gnus-summary-limit-to-unread
1925   "s" gnus-summary-isearch-article
1926   "\t" gnus-summary-widget-forward
1927   [backtab] gnus-summary-widget-backward
1928   "t" gnus-summary-toggle-header
1929   "g" gnus-summary-show-article
1930   "l" gnus-summary-goto-last-article
1931   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1932   "\C-d" gnus-summary-enter-digest-group
1933   "\M-\C-d" gnus-summary-read-document
1934   "\M-\C-e" gnus-summary-edit-parameters
1935   "\M-\C-a" gnus-summary-customize-parameters
1936   "\C-c\C-b" gnus-bug
1937   "*" gnus-cache-enter-article
1938   "\M-*" gnus-cache-remove-article
1939   "\M-&" gnus-summary-universal-argument
1940   "\C-l" gnus-recenter
1941   "I" gnus-summary-increase-score
1942   "L" gnus-summary-lower-score
1943   "\M-i" gnus-symbolic-argument
1944   "h" gnus-summary-select-article-buffer
1945
1946   "b" gnus-article-view-part
1947   "\M-t" gnus-summary-toggle-display-buttonized
1948
1949   "V" gnus-summary-score-map
1950   "X" gnus-uu-extract-map
1951   "S" gnus-summary-send-map)
1952
1953 ;; Sort of orthogonal keymap
1954 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1955   "t" gnus-summary-tick-article-forward
1956   "!" gnus-summary-tick-article-forward
1957   "d" gnus-summary-mark-as-read-forward
1958   "r" gnus-summary-mark-as-read-forward
1959   "c" gnus-summary-clear-mark-forward
1960   " " gnus-summary-clear-mark-forward
1961   "e" gnus-summary-mark-as-expirable
1962   "x" gnus-summary-mark-as-expirable
1963   "?" gnus-summary-mark-as-dormant
1964   "b" gnus-summary-set-bookmark
1965   "B" gnus-summary-remove-bookmark
1966   "#" gnus-summary-mark-as-processable
1967   "\M-#" gnus-summary-unmark-as-processable
1968   "S" gnus-summary-limit-include-expunged
1969   "C" gnus-summary-catchup
1970   "H" gnus-summary-catchup-to-here
1971   "h" gnus-summary-catchup-from-here
1972   "\C-c" gnus-summary-catchup-all
1973   "k" gnus-summary-kill-same-subject-and-select
1974   "K" gnus-summary-kill-same-subject
1975   "P" gnus-uu-mark-map)
1976
1977 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1978   "c" gnus-summary-clear-above
1979   "u" gnus-summary-tick-above
1980   "m" gnus-summary-mark-above
1981   "k" gnus-summary-kill-below)
1982
1983 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1984   "/" gnus-summary-limit-to-subject
1985   "n" gnus-summary-limit-to-articles
1986   "b" gnus-summary-limit-to-bodies
1987   "h" gnus-summary-limit-to-headers
1988   "w" gnus-summary-pop-limit
1989   "s" gnus-summary-limit-to-subject
1990   "a" gnus-summary-limit-to-author
1991   "u" gnus-summary-limit-to-unread
1992   "m" gnus-summary-limit-to-marks
1993   "M" gnus-summary-limit-exclude-marks
1994   "v" gnus-summary-limit-to-score
1995   "*" gnus-summary-limit-include-cached
1996   "D" gnus-summary-limit-include-dormant
1997   "T" gnus-summary-limit-include-thread
1998   "d" gnus-summary-limit-exclude-dormant
1999   "t" gnus-summary-limit-to-age
2000   "." gnus-summary-limit-to-unseen
2001   "x" gnus-summary-limit-to-extra
2002   "p" gnus-summary-limit-to-display-predicate
2003   "E" gnus-summary-limit-include-expunged
2004   "c" gnus-summary-limit-exclude-childless-dormant
2005   "C" gnus-summary-limit-mark-excluded-as-read
2006   "o" gnus-summary-insert-old-articles
2007   "N" gnus-summary-insert-new-articles
2008   "S" gnus-summary-limit-to-singletons
2009   "r" gnus-summary-limit-to-replied
2010   "R" gnus-summary-limit-to-recipient
2011   "A" gnus-summary-limit-to-address)
2012
2013 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2014   "n" gnus-summary-next-unread-article
2015   "p" gnus-summary-prev-unread-article
2016   "N" gnus-summary-next-article
2017   "P" gnus-summary-prev-article
2018   "\C-n" gnus-summary-next-same-subject
2019   "\C-p" gnus-summary-prev-same-subject
2020   "\M-n" gnus-summary-next-unread-subject
2021   "\M-p" gnus-summary-prev-unread-subject
2022   "f" gnus-summary-first-unread-article
2023   "b" gnus-summary-best-unread-article
2024   "j" gnus-summary-goto-article
2025   "g" gnus-summary-goto-subject
2026   "l" gnus-summary-goto-last-article
2027   "o" gnus-summary-pop-article)
2028
2029 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2030   "k" gnus-summary-kill-thread
2031   "E" gnus-summary-expire-thread
2032   "l" gnus-summary-lower-thread
2033   "i" gnus-summary-raise-thread
2034   "T" gnus-summary-toggle-threads
2035   "t" gnus-summary-rethread-current
2036   "^" gnus-summary-reparent-thread
2037   "\M-^" gnus-summary-reparent-children
2038   "s" gnus-summary-show-thread
2039   "S" gnus-summary-show-all-threads
2040   "h" gnus-summary-hide-thread
2041   "H" gnus-summary-hide-all-threads
2042   "n" gnus-summary-next-thread
2043   "p" gnus-summary-prev-thread
2044   "u" gnus-summary-up-thread
2045   "o" gnus-summary-top-thread
2046   "d" gnus-summary-down-thread
2047   "#" gnus-uu-mark-thread
2048   "\M-#" gnus-uu-unmark-thread)
2049
2050 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2051   "g" gnus-summary-prepare
2052   "c" gnus-summary-insert-cached-articles
2053   "d" gnus-summary-insert-dormant-articles
2054   "t" gnus-summary-insert-ticked-articles)
2055
2056 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2057   "c" gnus-summary-catchup-and-exit
2058   "C" gnus-summary-catchup-all-and-exit
2059   "E" gnus-summary-exit-no-update
2060   "Q" gnus-summary-exit
2061   "Z" gnus-summary-exit
2062   "n" gnus-summary-catchup-and-goto-next-group
2063   "p" gnus-summary-catchup-and-goto-prev-group
2064   "R" gnus-summary-reselect-current-group
2065   "G" gnus-summary-rescan-group
2066   "N" gnus-summary-next-group
2067   "s" gnus-summary-save-newsrc
2068   "P" gnus-summary-prev-group)
2069
2070 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2071   " " gnus-summary-next-page
2072   "n" gnus-summary-next-page
2073   [?\S-\ ] gnus-summary-prev-page
2074   "\177" gnus-summary-prev-page
2075   [delete] gnus-summary-prev-page
2076   "p" gnus-summary-prev-page
2077   "\r" gnus-summary-scroll-up
2078   "\M-\r" gnus-summary-scroll-down
2079   "<" gnus-summary-beginning-of-article
2080   ">" gnus-summary-end-of-article
2081   "b" gnus-summary-beginning-of-article
2082   "e" gnus-summary-end-of-article
2083   "^" gnus-summary-refer-parent-article
2084   "r" gnus-summary-refer-parent-article
2085   "C" gnus-summary-show-complete-article
2086   "D" gnus-summary-enter-digest-group
2087   "R" gnus-summary-refer-references
2088   "T" gnus-summary-refer-thread
2089   "W" gnus-warp-to-article
2090   "g" gnus-summary-show-article
2091   "s" gnus-summary-isearch-article
2092   "\t" gnus-summary-widget-forward
2093   [backtab] gnus-summary-widget-backward
2094   "P" gnus-summary-print-article
2095   "S" gnus-sticky-article
2096   "M" gnus-mailing-list-insinuate
2097   "t" gnus-article-babel)
2098
2099 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2100   "b" gnus-article-add-buttons
2101   "B" gnus-article-add-buttons-to-head
2102   "o" gnus-article-treat-overstrike
2103   "e" gnus-article-emphasize
2104   "w" gnus-article-fill-cited-article
2105   "Q" gnus-article-fill-long-lines
2106   "L" gnus-article-toggle-truncate-lines
2107   "C" gnus-article-capitalize-sentences
2108   "c" gnus-article-remove-cr
2109   "q" gnus-article-de-quoted-unreadable
2110   "6" gnus-article-de-base64-unreadable
2111   "Z" gnus-article-decode-HZ
2112   "A" gnus-article-treat-ansi-sequences
2113   "h" gnus-article-wash-html
2114   "u" gnus-article-unsplit-urls
2115   "s" gnus-summary-force-verify-and-decrypt
2116   "f" gnus-article-display-x-face
2117   "l" gnus-summary-stop-page-breaking
2118   "r" gnus-summary-caesar-message
2119   "m" gnus-summary-morse-message
2120   "t" gnus-summary-toggle-header
2121   "g" gnus-treat-smiley
2122   "v" gnus-summary-verbose-headers
2123   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2124   "p" gnus-article-verify-x-pgp-sig
2125   "d" gnus-article-treat-dumbquotes
2126   "U" gnus-article-treat-non-ascii
2127   "i" gnus-summary-idna-message)
2128
2129 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2130   ;; mnemonic: deuglif*Y*
2131   "u" gnus-article-outlook-unwrap-lines
2132   "a" gnus-article-outlook-repair-attribution
2133   "c" gnus-article-outlook-rearrange-citation
2134   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2135
2136 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2137   "a" gnus-article-hide
2138   "h" gnus-article-hide-headers
2139   "b" gnus-article-hide-boring-headers
2140   "s" gnus-article-hide-signature
2141   "c" gnus-article-hide-citation
2142   "C" gnus-article-hide-citation-in-followups
2143   "l" gnus-article-hide-list-identifiers
2144   "B" gnus-article-strip-banner
2145   "P" gnus-article-hide-pem
2146   "\C-c" gnus-article-hide-citation-maybe)
2147
2148 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2149   "a" gnus-article-highlight
2150   "h" gnus-article-highlight-headers
2151   "c" gnus-article-highlight-citation
2152   "s" gnus-article-highlight-signature)
2153
2154 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2155   "f" gnus-article-treat-fold-headers
2156   "u" gnus-article-treat-unfold-headers
2157   "n" gnus-article-treat-fold-newsgroups)
2158
2159 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2160   "x" gnus-article-display-x-face
2161   "d" gnus-article-display-face
2162   "s" gnus-treat-smiley
2163   "D" gnus-article-remove-images
2164   "W" gnus-article-show-images
2165   "f" gnus-treat-from-picon
2166   "m" gnus-treat-mail-picon
2167   "n" gnus-treat-newsgroups-picon
2168   "g" gnus-treat-from-gravatar
2169   "h" gnus-treat-mail-gravatar)
2170
2171 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2172   "w" gnus-article-decode-mime-words
2173   "c" gnus-article-decode-charset
2174   "v" gnus-mime-view-all-parts
2175   "b" gnus-article-view-part)
2176
2177 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2178   "z" gnus-article-date-ut
2179   "u" gnus-article-date-ut
2180   "l" gnus-article-date-local
2181   "p" gnus-article-date-english
2182   "e" gnus-article-date-lapsed
2183   "o" gnus-article-date-original
2184   "i" gnus-article-date-iso8601
2185   "s" gnus-article-date-user)
2186
2187 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2188   "t" gnus-article-remove-trailing-blank-lines
2189   "l" gnus-article-strip-leading-blank-lines
2190   "m" gnus-article-strip-multiple-blank-lines
2191   "a" gnus-article-strip-blank-lines
2192   "A" gnus-article-strip-all-blank-lines
2193   "s" gnus-article-strip-leading-space
2194   "e" gnus-article-strip-trailing-space
2195   "w" gnus-article-remove-leading-whitespace)
2196
2197 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2198   "v" gnus-version
2199   "d" gnus-summary-describe-group
2200   "h" gnus-summary-describe-briefly
2201   "i" gnus-info-find-node)
2202
2203 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2204   "e" gnus-summary-expire-articles
2205   "\M-\C-e" gnus-summary-expire-articles-now
2206   "\177" gnus-summary-delete-article
2207   [delete] gnus-summary-delete-article
2208   [backspace] gnus-summary-delete-article
2209   "m" gnus-summary-move-article
2210   "r" gnus-summary-respool-article
2211   "w" gnus-summary-edit-article
2212   "c" gnus-summary-copy-article
2213   "B" gnus-summary-crosspost-article
2214   "q" gnus-summary-respool-query
2215   "t" gnus-summary-respool-trace
2216   "i" gnus-summary-import-article
2217   "I" gnus-summary-create-article
2218   "p" gnus-summary-article-posted-p)
2219
2220 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2221   "o" gnus-summary-save-article
2222   "m" gnus-summary-save-article-mail
2223   "F" gnus-summary-write-article-file
2224   "r" gnus-summary-save-article-rmail
2225   "f" gnus-summary-save-article-file
2226   "b" gnus-summary-save-article-body-file
2227   "B" gnus-summary-write-article-body-file
2228   "h" gnus-summary-save-article-folder
2229   "v" gnus-summary-save-article-vm
2230   "p" gnus-summary-pipe-output
2231   "P" gnus-summary-muttprint)
2232
2233 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2234   "b" gnus-summary-display-buttonized
2235   "m" gnus-summary-repair-multipart
2236   "v" gnus-article-view-part
2237   "o" gnus-article-save-part
2238   "O" gnus-article-save-part-and-strip
2239   "r" gnus-article-replace-part
2240   "d" gnus-article-delete-part
2241   "t" gnus-article-view-part-as-type
2242   "j" gnus-article-jump-to-part
2243   "c" gnus-article-copy-part
2244   "C" gnus-article-view-part-as-charset
2245   "e" gnus-article-view-part-externally
2246   "H" gnus-article-browse-html-article
2247   "E" gnus-article-encrypt-body
2248   "i" gnus-article-inline-part
2249   "|" gnus-article-pipe-part)
2250
2251 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2252   "p" gnus-summary-mark-as-processable
2253   "u" gnus-summary-unmark-as-processable
2254   "U" gnus-summary-unmark-all-processable
2255   "v" gnus-uu-mark-over
2256   "s" gnus-uu-mark-series
2257   "r" gnus-uu-mark-region
2258   "g" gnus-uu-unmark-region
2259   "R" gnus-uu-mark-by-regexp
2260   "G" gnus-uu-unmark-by-regexp
2261   "t" gnus-uu-mark-thread
2262   "T" gnus-uu-unmark-thread
2263   "a" gnus-uu-mark-all
2264   "b" gnus-uu-mark-buffer
2265   "S" gnus-uu-mark-sparse
2266   "k" gnus-summary-kill-process-mark
2267   "y" gnus-summary-yank-process-mark
2268   "w" gnus-summary-save-process-mark
2269   "i" gnus-uu-invert-processable)
2270
2271 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2272   ;;"x" gnus-uu-extract-any
2273   "m" gnus-summary-save-parts
2274   "u" gnus-uu-decode-uu
2275   "U" gnus-uu-decode-uu-and-save
2276   "s" gnus-uu-decode-unshar
2277   "S" gnus-uu-decode-unshar-and-save
2278   "o" gnus-uu-decode-save
2279   "O" gnus-uu-decode-save
2280   "b" gnus-uu-decode-binhex
2281   "B" gnus-uu-decode-binhex
2282   "Y" gnus-uu-decode-yenc
2283   "p" gnus-uu-decode-postscript
2284   "P" gnus-uu-decode-postscript-and-save)
2285
2286 (gnus-define-keys
2287     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2288   "u" gnus-uu-decode-uu-view
2289   "U" gnus-uu-decode-uu-and-save-view
2290   "s" gnus-uu-decode-unshar-view
2291   "S" gnus-uu-decode-unshar-and-save-view
2292   "o" gnus-uu-decode-save-view
2293   "O" gnus-uu-decode-save-view
2294   "b" gnus-uu-decode-binhex-view
2295   "B" gnus-uu-decode-binhex-view
2296   "p" gnus-uu-decode-postscript-view
2297   "P" gnus-uu-decode-postscript-and-save-view)
2298
2299 (defvar gnus-article-post-menu nil)
2300
2301 (defconst gnus-summary-menu-maxlen 20)
2302
2303 (defun gnus-summary-menu-split (menu)
2304   ;; If we have lots of elements, divide them into groups of 20
2305   ;; and make a pane (or submenu) for each one.
2306   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2307       (let ((menu menu) sublists next
2308             (i 1))
2309         (while menu
2310           ;; Pull off the next gnus-summary-menu-maxlen elements
2311           ;; and make them the next element of sublist.
2312           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2313           (if next
2314               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2315                       nil))
2316           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2317                                              (aref (car (last menu)) 0)) menu)
2318                                sublists))
2319           (setq i (1+ i))
2320           (setq menu next))
2321         (nreverse sublists))
2322     ;; Few elements--put them all in one pane.
2323     menu))
2324
2325 (defun gnus-summary-make-menu-bar ()
2326   (gnus-turn-off-edit-menu 'summary)
2327
2328   (unless (boundp 'gnus-summary-misc-menu)
2329
2330     (easy-menu-define
2331       gnus-summary-kill-menu gnus-summary-mode-map ""
2332       (cons
2333        "Score"
2334        (nconc
2335         (list
2336          ["Customize" gnus-score-customize t])
2337         (gnus-make-score-map 'increase)
2338         (gnus-make-score-map 'lower)
2339         '(("Mark"
2340            ["Kill below" gnus-summary-kill-below t]
2341            ["Mark above" gnus-summary-mark-above t]
2342            ["Tick above" gnus-summary-tick-above t]
2343            ["Clear above" gnus-summary-clear-above t])
2344           ["Current score" gnus-summary-current-score t]
2345           ["Set score" gnus-summary-set-score t]
2346           ["Switch current score file..." gnus-score-change-score-file t]
2347           ["Set mark below..." gnus-score-set-mark-below t]
2348           ["Set expunge below..." gnus-score-set-expunge-below t]
2349           ["Edit current score file" gnus-score-edit-current-scores t]
2350           ["Edit score file..." gnus-score-edit-file t]
2351           ["Trace score" gnus-score-find-trace t]
2352           ["Find words" gnus-score-find-favourite-words t]
2353           ["Rescore buffer" gnus-summary-rescore t]
2354           ["Increase score..." gnus-summary-increase-score t]
2355           ["Lower score..." gnus-summary-lower-score t]))))
2356
2357     ;; Define both the Article menu in the summary buffer and the
2358     ;; equivalent Commands menu in the article buffer here for
2359     ;; consistency.
2360     (let ((innards
2361            `(("Hide"
2362               ["All" gnus-article-hide t]
2363               ["Headers" gnus-article-hide-headers t]
2364               ["Signature" gnus-article-hide-signature t]
2365               ["Citation" gnus-article-hide-citation t]
2366               ["List identifiers" gnus-article-hide-list-identifiers t]
2367               ["Banner" gnus-article-strip-banner t]
2368               ["Boring headers" gnus-article-hide-boring-headers t])
2369              ("Highlight"
2370               ["All" gnus-article-highlight t]
2371               ["Headers" gnus-article-highlight-headers t]
2372               ["Signature" gnus-article-highlight-signature t]
2373               ["Citation" gnus-article-highlight-citation t])
2374              ("MIME"
2375               ["Words" gnus-article-decode-mime-words t]
2376               ["Charset" gnus-article-decode-charset t]
2377               ["QP" gnus-article-de-quoted-unreadable t]
2378               ["Base64" gnus-article-de-base64-unreadable t]
2379               ["View MIME buttons" gnus-summary-display-buttonized t]
2380               ["View all" gnus-mime-view-all-parts t]
2381               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2382               ["Encrypt body" gnus-article-encrypt-body
2383                :active (not (gnus-group-read-only-p))
2384                ,@(if (featurep 'xemacs) nil
2385                    '(:help "Encrypt the message body on disk"))]
2386               ["Extract all parts..." gnus-summary-save-parts t]
2387               ("Multipart"
2388                ["Repair multipart" gnus-summary-repair-multipart t]
2389                ["Pipe part..." gnus-article-pipe-part t]
2390                ["Inline part" gnus-article-inline-part t]
2391                ["View part as type..." gnus-article-view-part-as-type t]
2392                ["Encrypt body" gnus-article-encrypt-body
2393                 :active (not (gnus-group-read-only-p))
2394                ,@(if (featurep 'xemacs) nil
2395                    '(:help "Encrypt the message body on disk"))]
2396                ["View part externally" gnus-article-view-part-externally t]
2397                ["View HTML parts in browser" gnus-article-browse-html-article t]
2398                ["View part with charset..." gnus-article-view-part-as-charset t]
2399                ["Copy part" gnus-article-copy-part t]
2400                ["Save part..." gnus-article-save-part t]
2401                ["View part" gnus-article-view-part t]))
2402              ("Date"
2403               ["Local" gnus-article-date-local t]
2404               ["ISO8601" gnus-article-date-iso8601 t]
2405               ["UT" gnus-article-date-ut t]
2406               ["Original" gnus-article-date-original t]
2407               ["Lapsed" gnus-article-date-lapsed t]
2408               ["User-defined" gnus-article-date-user t])
2409              ("Display"
2410               ["Remove images" gnus-article-remove-images t]
2411               ["Toggle smiley" gnus-treat-smiley t]
2412               ["Show X-Face" gnus-article-display-x-face t]
2413               ["Show picons in From" gnus-treat-from-picon t]
2414               ["Show picons in mail headers" gnus-treat-mail-picon t]
2415               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2416               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2417               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2418               ("View as different encoding"
2419                ,@(gnus-summary-menu-split
2420                   (mapcar
2421                    (lambda (cs)
2422                      ;; Since easymenu under Emacs doesn't allow
2423                      ;; lambda forms for menu commands, we should
2424                      ;; provide intern'ed function symbols.
2425                      (let ((command (intern (format "\
2426 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2427                        (fset command
2428                              `(lambda ()
2429                                 (interactive)
2430                                 (let ((gnus-summary-show-article-charset-alist
2431                                        '((1 . ,cs))))
2432                                   (gnus-summary-show-article 1))))
2433                        `[,(symbol-name cs) ,command t]))
2434                    (sort (if (fboundp 'coding-system-list)
2435                              (coding-system-list)
2436                            (mapcar 'car mm-mime-mule-charset-alist))
2437                          'string<)))))
2438              ("Washing"
2439               ("Remove Blanks"
2440                ["Leading" gnus-article-strip-leading-blank-lines t]
2441                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2442                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2443                ["All of the above" gnus-article-strip-blank-lines t]
2444                ["All" gnus-article-strip-all-blank-lines t]
2445                ["Leading space" gnus-article-strip-leading-space t]
2446                ["Trailing space" gnus-article-strip-trailing-space t]
2447                ["Leading space in headers"
2448                 gnus-article-remove-leading-whitespace t])
2449               ["Overstrike" gnus-article-treat-overstrike t]
2450               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2451               ["Non-ASCII" gnus-article-treat-non-ascii t]
2452               ["Emphasis" gnus-article-emphasize t]
2453               ["Word wrap" gnus-article-fill-cited-article t]
2454               ["Fill long lines" gnus-article-fill-long-lines t]
2455               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2456               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2457               ["Remove CR" gnus-article-remove-cr t]
2458               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2459               ["Base64" gnus-article-de-base64-unreadable t]
2460               ["Rot 13" gnus-summary-caesar-message
2461                ,@(if (featurep 'xemacs) '(t)
2462                    '(:help "\"Caesar rotate\" article by 13"))]
2463               ["De-IDNA" gnus-summary-idna-message t]
2464               ["Morse decode" gnus-summary-morse-message t]
2465               ["Unix pipe..." gnus-summary-pipe-message t]
2466               ["Add buttons" gnus-article-add-buttons t]
2467               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2468               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2469               ["Verbose header" gnus-summary-verbose-headers t]
2470               ["Toggle header" gnus-summary-toggle-header t]
2471               ["Unfold headers" gnus-article-treat-unfold-headers t]
2472               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2473               ["Html" gnus-article-wash-html t]
2474               ["Unsplit URLs" gnus-article-unsplit-urls t]
2475               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2476               ["Decode HZ" gnus-article-decode-HZ t]
2477               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2478               ("(Outlook) Deuglify"
2479                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2480                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2481                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2482                ["Full (Outlook) deuglify"
2483                 gnus-article-outlook-deuglify-article t])
2484               )
2485              ("Output"
2486               ["Save in default format..." gnus-summary-save-article
2487                ,@(if (featurep 'xemacs) '(t)
2488                    '(:help "Save article using default method"))]
2489               ["Save in file..." gnus-summary-save-article-file
2490                ,@(if (featurep 'xemacs) '(t)
2491                    '(:help "Save article in file"))]
2492               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2493               ["Save in MH folder..." gnus-summary-save-article-folder t]
2494               ["Save in VM folder..." gnus-summary-save-article-vm t]
2495               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2496               ["Save body in file..." gnus-summary-save-article-body-file t]
2497               ["Pipe through a filter..." gnus-summary-pipe-output t]
2498               ["Print with Muttprint..." gnus-summary-muttprint t]
2499               ["Print" gnus-summary-print-article
2500                ,@(if (featurep 'xemacs) '(t)
2501                    '(:help "Generate and print a PostScript image"))])
2502              ("Copy, move,... (Backend)"
2503               ,@(if (featurep 'xemacs) nil
2504                   '(:help "Copying, moving, expiring articles..."))
2505               ["Respool article..." gnus-summary-respool-article t]
2506               ["Move article..." gnus-summary-move-article
2507                (gnus-check-backend-function
2508                 'request-move-article gnus-newsgroup-name)]
2509               ["Copy article..." gnus-summary-copy-article t]
2510               ["Crosspost article..." gnus-summary-crosspost-article
2511                (gnus-check-backend-function
2512                 'request-replace-article gnus-newsgroup-name)]
2513               ["Import file..." gnus-summary-import-article
2514                (gnus-check-backend-function
2515                 'request-accept-article gnus-newsgroup-name)]
2516               ["Create article..." gnus-summary-create-article
2517                (gnus-check-backend-function
2518                 'request-accept-article gnus-newsgroup-name)]
2519               ["Check if posted" gnus-summary-article-posted-p t]
2520               ["Edit article" gnus-summary-edit-article
2521                (not (gnus-group-read-only-p))]
2522               ["Delete article" gnus-summary-delete-article
2523                (gnus-check-backend-function
2524                 'request-expire-articles gnus-newsgroup-name)]
2525               ["Query respool" gnus-summary-respool-query t]
2526               ["Trace respool" gnus-summary-respool-trace t]
2527               ["Delete expirable articles" gnus-summary-expire-articles-now
2528                (gnus-check-backend-function
2529                 'request-expire-articles gnus-newsgroup-name)])
2530              ("Extract"
2531               ["Uudecode" gnus-uu-decode-uu
2532                ,@(if (featurep 'xemacs) '(t)
2533                    '(:help "Decode uuencoded article(s)"))]
2534               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2535               ["Unshar" gnus-uu-decode-unshar t]
2536               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2537               ["Save" gnus-uu-decode-save t]
2538               ["Binhex" gnus-uu-decode-binhex t]
2539               ["PostScript" gnus-uu-decode-postscript t]
2540               ["All MIME parts" gnus-summary-save-parts t])
2541              ("Cache"
2542               ["Enter article" gnus-cache-enter-article t]
2543               ["Remove article" gnus-cache-remove-article t])
2544              ["Translate" gnus-article-babel t]
2545              ["Select article buffer" gnus-summary-select-article-buffer t]
2546              ["Make article buffer sticky" gnus-sticky-article t]
2547              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2548              ["Isearch article..." gnus-summary-isearch-article t]
2549              ["Beginning of the article" gnus-summary-beginning-of-article t]
2550              ["End of the article" gnus-summary-end-of-article t]
2551              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2552              ["Fetch referenced articles" gnus-summary-refer-references t]
2553              ["Fetch current thread" gnus-summary-refer-thread t]
2554              ["Fetch article with id..." gnus-summary-refer-article t]
2555              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2556              ["Redisplay" gnus-summary-show-article t]
2557              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2558       (easy-menu-define
2559         gnus-summary-article-menu gnus-summary-mode-map ""
2560         (cons "Article" innards))
2561
2562       (if (not (keymapp gnus-summary-article-menu))
2563           (easy-menu-define
2564             gnus-article-commands-menu gnus-article-mode-map ""
2565             (cons "Commands" innards))
2566         ;; in Emacs, don't share menu.
2567         (setq gnus-article-commands-menu
2568               (copy-keymap gnus-summary-article-menu))
2569         (define-key gnus-article-mode-map [menu-bar commands]
2570           (cons "Commands" gnus-article-commands-menu))))
2571
2572     (easy-menu-define
2573       gnus-summary-thread-menu gnus-summary-mode-map ""
2574       '("Threads"
2575         ["Find all messages in thread" gnus-summary-refer-thread t]
2576         ["Toggle threading" gnus-summary-toggle-threads t]
2577         ["Hide threads" gnus-summary-hide-all-threads t]
2578         ["Show threads" gnus-summary-show-all-threads t]
2579         ["Hide thread" gnus-summary-hide-thread t]
2580         ["Show thread" gnus-summary-show-thread t]
2581         ["Go to next thread" gnus-summary-next-thread t]
2582         ["Go to previous thread" gnus-summary-prev-thread t]
2583         ["Go down thread" gnus-summary-down-thread t]
2584         ["Go up thread" gnus-summary-up-thread t]
2585         ["Top of thread" gnus-summary-top-thread t]
2586         ["Mark thread as read" gnus-summary-kill-thread t]
2587         ["Mark thread as expired" gnus-summary-expire-thread t]
2588         ["Lower thread score" gnus-summary-lower-thread t]
2589         ["Raise thread score" gnus-summary-raise-thread t]
2590         ["Rethread current" gnus-summary-rethread-current t]))
2591
2592     (easy-menu-define
2593       gnus-summary-post-menu gnus-summary-mode-map ""
2594       `("Post"
2595         ["Send a message (mail or news)" gnus-summary-post-news
2596          ,@(if (featurep 'xemacs) '(t)
2597              '(:help "Compose a new message (mail or news)"))]
2598         ["Followup" gnus-summary-followup
2599          ,@(if (featurep 'xemacs) '(t)
2600              '(:help "Post followup to this article"))]
2601         ["Followup and yank" gnus-summary-followup-with-original
2602          ,@(if (featurep 'xemacs) '(t)
2603              '(:help "Post followup to this article, quoting its contents"))]
2604         ["Supersede article" gnus-summary-supersede-article t]
2605         ["Cancel article" gnus-summary-cancel-article
2606          ,@(if (featurep 'xemacs) '(t)
2607              '(:help "Cancel an article you posted"))]
2608         ["Reply" gnus-summary-reply t]
2609         ["Reply and yank" gnus-summary-reply-with-original t]
2610         ["Wide reply" gnus-summary-wide-reply t]
2611         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2612          ,@(if (featurep 'xemacs) '(t)
2613              '(:help "Mail a reply, quoting this article"))]
2614         ["Very wide reply" gnus-summary-very-wide-reply t]
2615         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2616          ,@(if (featurep 'xemacs) '(t)
2617              '(:help "Mail a very wide reply, quoting this article"))]
2618         ["Mail forward" gnus-summary-mail-forward t]
2619         ["Post forward" gnus-summary-post-forward t]
2620         ["Digest and mail" gnus-uu-digest-mail-forward t]
2621         ["Digest and post" gnus-uu-digest-post-forward t]
2622         ["Resend message" gnus-summary-resend-message t]
2623         ["Resend message edit" gnus-summary-resend-message-edit t]
2624         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2625         ["Send a mail" gnus-summary-mail-other-window t]
2626         ["Create a local message" gnus-summary-news-other-window t]
2627         ["Uuencode and post" gnus-uu-post-news
2628          ,@(if (featurep 'xemacs) '(t)
2629              '(:help "Post a uuencoded article"))]
2630         ["Followup via news" gnus-summary-followup-to-mail t]
2631         ["Followup via news and yank"
2632          gnus-summary-followup-to-mail-with-original t]
2633         ["Strip signature on reply"
2634          (lambda ()
2635            (interactive)
2636            (if (not (memq message-cite-function
2637                           '(message-cite-original-without-signature
2638                             message-cite-original)))
2639                ;; Stupid workaround for XEmacs not honoring :visible.
2640                (message "Can't toggle this value of `message-cite-function'")
2641              (setq message-cite-function
2642                    (if (eq message-cite-function
2643                            'message-cite-original-without-signature)
2644                        'message-cite-original
2645                      'message-cite-original-without-signature))))
2646          ;; XEmacs barfs on :visible.
2647          ,@(if (featurep 'xemacs) nil
2648              '(:visible (memq message-cite-function
2649                               '(message-cite-original-without-signature
2650                                 message-cite-original))))
2651          :style toggle
2652          :selected (eq message-cite-function
2653                        'message-cite-original-without-signature)
2654          ,@(if (featurep 'xemacs) nil
2655              '(:help "Strip signature from cited article when replying."))]
2656         ;;("Draft"
2657         ;;["Send" gnus-summary-send-draft t]
2658         ;;["Send bounced" gnus-resend-bounced-mail t])
2659         ))
2660
2661     (cond
2662      ((not (keymapp gnus-summary-post-menu))
2663       (setq gnus-article-post-menu gnus-summary-post-menu))
2664      ((not gnus-article-post-menu)
2665       ;; Don't share post menu.
2666       (setq gnus-article-post-menu
2667             (copy-keymap gnus-summary-post-menu))))
2668     (define-key gnus-article-mode-map [menu-bar post]
2669       (cons "Post" gnus-article-post-menu))
2670
2671     (easy-menu-define
2672       gnus-summary-misc-menu gnus-summary-mode-map ""
2673       `("Gnus"
2674         ("Mark Read"
2675          ["Mark as read" gnus-summary-mark-as-read-forward t]
2676          ["Mark same subject and select"
2677           gnus-summary-kill-same-subject-and-select t]
2678          ["Mark same subject" gnus-summary-kill-same-subject t]
2679          ["Catchup" gnus-summary-catchup
2680           ,@(if (featurep 'xemacs) '(t)
2681               '(:help "Mark unread articles in this group as read"))]
2682          ["Catchup all" gnus-summary-catchup-all t]
2683          ["Catchup to here" gnus-summary-catchup-to-here t]
2684          ["Catchup from here" gnus-summary-catchup-from-here t]
2685          ["Catchup region" gnus-summary-mark-region-as-read
2686           (gnus-mark-active-p)]
2687          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2688         ("Mark Various"
2689          ["Tick" gnus-summary-tick-article-forward t]
2690          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2691          ["Remove marks" gnus-summary-clear-mark-forward t]
2692          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2693          ["Set bookmark" gnus-summary-set-bookmark t]
2694          ["Remove bookmark" gnus-summary-remove-bookmark t])
2695         ("Limit to"
2696          ["Marks..." gnus-summary-limit-to-marks t]
2697          ["Subject..." gnus-summary-limit-to-subject t]
2698          ["Author..." gnus-summary-limit-to-author t]
2699          ["Recipient..." gnus-summary-limit-to-recipient t]
2700          ["Address..." gnus-summary-limit-to-address t]
2701          ["Age..." gnus-summary-limit-to-age t]
2702          ["Extra..." gnus-summary-limit-to-extra t]
2703          ["Score..." gnus-summary-limit-to-score t]
2704          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2705          ["Unread" gnus-summary-limit-to-unread t]
2706          ["Unseen" gnus-summary-limit-to-unseen t]
2707          ["Singletons" gnus-summary-limit-to-singletons t]
2708          ["Replied" gnus-summary-limit-to-replied t]
2709          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2710          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2711          ["Pop limit" gnus-summary-pop-limit t]
2712          ["Show dormant" gnus-summary-limit-include-dormant t]
2713          ["Hide childless dormant"
2714           gnus-summary-limit-exclude-childless-dormant t]
2715          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2716          ["Hide marked" gnus-summary-limit-exclude-marks t]
2717          ["Show expunged" gnus-summary-limit-include-expunged t])
2718         ("Process Mark"
2719          ["Set mark" gnus-summary-mark-as-processable t]
2720          ["Remove mark" gnus-summary-unmark-as-processable t]
2721          ["Remove all marks" gnus-summary-unmark-all-processable t]
2722          ["Invert marks" gnus-uu-invert-processable t]
2723          ["Mark above" gnus-uu-mark-over t]
2724          ["Mark series" gnus-uu-mark-series t]
2725          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2726          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2727          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2728          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2729          ["Mark all" gnus-uu-mark-all t]
2730          ["Mark buffer" gnus-uu-mark-buffer t]
2731          ["Mark sparse" gnus-uu-mark-sparse t]
2732          ["Mark thread" gnus-uu-mark-thread t]
2733          ["Unmark thread" gnus-uu-unmark-thread t]
2734          ("Process Mark Sets"
2735           ["Kill" gnus-summary-kill-process-mark t]
2736           ["Yank" gnus-summary-yank-process-mark
2737            gnus-newsgroup-process-stack]
2738           ["Save" gnus-summary-save-process-mark t]
2739           ["Run command on marked..." gnus-summary-universal-argument t]))
2740         ("Registry Marks")
2741         ("Scroll article"
2742          ["Page forward" gnus-summary-next-page
2743           ,@(if (featurep 'xemacs) '(t)
2744               '(:help "Show next page of article"))]
2745          ["Page backward" gnus-summary-prev-page
2746           ,@(if (featurep 'xemacs) '(t)
2747               '(:help "Show previous page of article"))]
2748          ["Line forward" gnus-summary-scroll-up t])
2749         ("Move"
2750          ["Next unread article" gnus-summary-next-unread-article t]
2751          ["Previous unread article" gnus-summary-prev-unread-article t]
2752          ["Next article" gnus-summary-next-article t]
2753          ["Previous article" gnus-summary-prev-article t]
2754          ["Next unread subject" gnus-summary-next-unread-subject t]
2755          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2756          ["Next article same subject" gnus-summary-next-same-subject t]
2757          ["Previous article same subject" gnus-summary-prev-same-subject t]
2758          ["First unread article" gnus-summary-first-unread-article t]
2759          ["Best unread article" gnus-summary-best-unread-article t]
2760          ["Go to subject number..." gnus-summary-goto-subject t]
2761          ["Go to article number..." gnus-summary-goto-article t]
2762          ["Go to the last article" gnus-summary-goto-last-article t]
2763          ["Pop article off history" gnus-summary-pop-article t])
2764         ("Sort"
2765          ["Sort by number" gnus-summary-sort-by-number t]
2766          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2767          ["Sort by author" gnus-summary-sort-by-author t]
2768          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2769          ["Sort by subject" gnus-summary-sort-by-subject t]
2770          ["Sort by date" gnus-summary-sort-by-date t]
2771          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2772          ["Sort by score" gnus-summary-sort-by-score t]
2773          ["Sort by lines" gnus-summary-sort-by-lines t]
2774          ["Sort by characters" gnus-summary-sort-by-chars t]
2775          ["Randomize" gnus-summary-sort-by-random t]
2776          ["Original sort" gnus-summary-sort-by-original t])
2777         ("Help"
2778          ["Describe group" gnus-summary-describe-group t]
2779          ["Read manual" gnus-info-find-node t])
2780         ("Modes"
2781          ["Pick and read" gnus-pick-mode t]
2782          ["Binary" gnus-binary-mode t])
2783         ("Regeneration"
2784          ["Regenerate" gnus-summary-prepare t]
2785          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2786          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2787          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2788          ["Toggle threading" gnus-summary-toggle-threads t])
2789         ["See old articles" gnus-summary-insert-old-articles t]
2790         ["See new articles" gnus-summary-insert-new-articles t]
2791         ["Filter articles..." gnus-summary-execute-command t]
2792         ["Run command on articles..." gnus-summary-universal-argument t]
2793         ["Search articles forward..." gnus-summary-search-article-forward t]
2794         ["Search articles backward..." gnus-summary-search-article-backward t]
2795         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2796         ["Expand window" gnus-summary-expand-window t]
2797         ["Expire expirable articles" gnus-summary-expire-articles
2798          (gnus-check-backend-function
2799           'request-expire-articles gnus-newsgroup-name)]
2800         ["Edit local kill file" gnus-summary-edit-local-kill t]
2801         ["Edit main kill file" gnus-summary-edit-global-kill t]
2802         ["Edit group parameters" gnus-summary-edit-parameters t]
2803         ["Customize group parameters" gnus-summary-customize-parameters t]
2804         ["Send a bug report" gnus-bug t]
2805         ("Exit"
2806          ["Catchup and exit" gnus-summary-catchup-and-exit
2807           ,@(if (featurep 'xemacs) '(t)
2808               '(:help "Mark unread articles in this group as read, then exit"))]
2809          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2810          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2811          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2812          ["Exit group" gnus-summary-exit
2813           ,@(if (featurep 'xemacs) '(t)
2814               '(:help "Exit current group, return to group selection mode"))]
2815          ["Exit group without updating" gnus-summary-exit-no-update t]
2816          ["Exit and goto next group" gnus-summary-next-group t]
2817          ["Exit and goto prev group" gnus-summary-prev-group t]
2818          ["Reselect group" gnus-summary-reselect-current-group t]
2819          ["Rescan group" gnus-summary-rescan-group t]
2820          ["Update dribble" gnus-summary-save-newsrc t])))
2821
2822     (gnus-run-hooks 'gnus-summary-menu-hook)))
2823
2824 (defvar gnus-summary-tool-bar-map nil)
2825
2826 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2827 ;; affect _new_ message buffers.  We might add a function that walks thru all
2828 ;; summary-mode buffers and force the update.
2829 (defun gnus-summary-tool-bar-update (&optional symbol value)
2830   "Update summary mode toolbar.
2831 Setter function for custom variables."
2832   (setq-default gnus-summary-tool-bar-map nil)
2833   (when symbol
2834     ;; When used as ":set" function:
2835     (set-default symbol value))
2836   (when (gnus-buffer-live-p gnus-summary-buffer)
2837     (with-current-buffer gnus-summary-buffer
2838       (gnus-summary-make-tool-bar))))
2839
2840 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2841                                      'gnus-summary-tool-bar-gnome
2842                                    'gnus-summary-tool-bar-retro)
2843   "Specifies the Gnus summary tool bar.
2844
2845 It can be either a list or a symbol referring to a list.  See
2846 `gmm-tool-bar-from-list' for the format of the list.  The
2847 default key map is `gnus-summary-mode-map'.
2848
2849 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2850 `gnus-summary-tool-bar-retro'."
2851   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2852                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2853                  (repeat :tag "User defined list" gmm-tool-bar-item)
2854                  (symbol))
2855   :version "23.1" ;; No Gnus
2856   :initialize 'custom-initialize-default
2857   :set 'gnus-summary-tool-bar-update
2858   :group 'gnus-summary)
2859
2860 (defcustom gnus-summary-tool-bar-gnome
2861   '((gnus-summary-post-news "mail/compose" nil)
2862     (gnus-summary-insert-new-articles "mail/inbox" nil
2863                                       :visible (or (not gnus-agent)
2864                                                    gnus-plugged))
2865     (gnus-summary-reply-with-original "mail/reply")
2866     (gnus-summary-reply "mail/reply" nil :visible nil)
2867     (gnus-summary-followup-with-original "mail/reply-all")
2868     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2869     (gnus-summary-mail-forward "mail/forward")
2870     (gnus-summary-save-article "mail/save")
2871     (gnus-summary-search-article-forward "search" nil :visible nil)
2872     (gnus-summary-print-article "print")
2873     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2874     ;; Some new commands that may need more suitable icons:
2875     (gnus-summary-save-newsrc "save" nil :visible nil)
2876     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2877     (gnus-summary-prev-article "left-arrow")
2878     (gnus-summary-next-article "right-arrow")
2879     (gnus-summary-next-page "next-page")
2880     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2881     ;;
2882     ;; Maybe some sort-by-... could be added:
2883     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2884     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2885     (gnus-summary-mark-as-expirable
2886      "delete" nil
2887      :visible (gnus-check-backend-function 'request-expire-articles
2888                                            gnus-newsgroup-name))
2889     (gnus-summary-mark-as-spam
2890      "mail/spam" t
2891      :visible (and (fboundp 'spam-group-ham-contents-p)
2892                    (spam-group-ham-contents-p gnus-newsgroup-name))
2893      :help "Mark as spam")
2894     (gnus-summary-mark-as-read-forward
2895      "mail/not-spam" nil
2896      :visible (and (fboundp 'spam-group-spam-contents-p)
2897                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2898     ;;
2899     (gnus-summary-exit "exit")
2900     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2901     (gnus-info-find-node "help"))
2902   "List of functions for the summary tool bar (GNOME style).
2903
2904 See `gmm-tool-bar-from-list' for the format of the list."
2905   :type '(repeat gmm-tool-bar-item)
2906   :version "23.1" ;; No Gnus
2907   :initialize 'custom-initialize-default
2908   :set 'gnus-summary-tool-bar-update
2909   :group 'gnus-summary)
2910
2911 (defcustom gnus-summary-tool-bar-retro
2912   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2913     (gnus-summary-next-unread-article "gnus/next-ur")
2914     (gnus-summary-post-news "gnus/post")
2915     (gnus-summary-followup-with-original "gnus/fuwo")
2916     (gnus-summary-followup "gnus/followup")
2917     (gnus-summary-reply-with-original "gnus/reply-wo")
2918     (gnus-summary-reply "gnus/reply")
2919     (gnus-summary-caesar-message "gnus/rot13")
2920     (gnus-uu-decode-uu "gnus/uu-decode")
2921     (gnus-summary-save-article-file "gnus/save-aif")
2922     (gnus-summary-save-article "gnus/save-art")
2923     (gnus-uu-post-news "gnus/uu-post")
2924     (gnus-summary-catchup "gnus/catchup")
2925     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2926     (gnus-summary-exit "gnus/exit-summ")
2927     ;; Some new command that may need more suitable icons:
2928     (gnus-summary-print-article "gnus/print" nil :visible nil)
2929     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2930     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2931     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2932     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2933     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2934     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2935     ;;
2936     (gnus-info-find-node "gnus/help" nil :visible nil))
2937   "List of functions for the summary tool bar (retro look).
2938
2939 See `gmm-tool-bar-from-list' for the format of the list."
2940   :type '(repeat gmm-tool-bar-item)
2941   :version "23.1" ;; No Gnus
2942   :initialize 'custom-initialize-default
2943   :set 'gnus-summary-tool-bar-update
2944   :group 'gnus-summary)
2945
2946 (defcustom gnus-summary-tool-bar-zap-list t
2947   "List of icon items from the global tool bar.
2948 These items are not displayed in the Gnus summary mode tool bar.
2949
2950 See `gmm-tool-bar-from-list' for the format of the list."
2951   :type 'gmm-tool-bar-zap-list
2952   :version "23.1" ;; No Gnus
2953   :initialize 'custom-initialize-default
2954   :set 'gnus-summary-tool-bar-update
2955   :group 'gnus-summary)
2956
2957 (defvar image-load-path)
2958 (defvar tool-bar-map)
2959
2960 (defun gnus-summary-make-tool-bar (&optional force)
2961   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2962 When FORCE, rebuild the tool bar."
2963   (when (and (not (featurep 'xemacs))
2964              (boundp 'tool-bar-mode)
2965              tool-bar-mode
2966              (or (not gnus-summary-tool-bar-map) force))
2967     (let* ((load-path
2968             (gmm-image-load-path-for-library "gnus"
2969                                              "mail/save.xpm"
2970                                              nil t))
2971            (image-load-path (cons (car load-path)
2972                                   (when (boundp 'image-load-path)
2973                                     image-load-path)))
2974            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2975                                         gnus-summary-tool-bar-zap-list
2976                                         'gnus-summary-mode-map)))
2977       (when map
2978         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2979         ;; uses its value.
2980         (setq gnus-summary-tool-bar-map map))))
2981   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2982
2983 (defun gnus-make-score-map (type)
2984   "Make a summary score map of type TYPE."
2985   (if t
2986       nil
2987     (let ((headers '(("author" "from" string)
2988                      ("subject" "subject" string)
2989                      ("article body" "body" string)
2990                      ("article head" "head" string)
2991                      ("xref" "xref" string)
2992                      ("extra header" "extra" string)
2993                      ("lines" "lines" number)
2994                      ("followups to author" "followup" string)))
2995           (types '((number ("less than" <)
2996                            ("greater than" >)
2997                            ("equal" =))
2998                    (string ("substring" s)
2999                            ("exact string" e)
3000                            ("fuzzy string" f)
3001                            ("regexp" r))))
3002           (perms '(("temporary" (current-time-string))
3003                    ("permanent" nil)
3004                    ("immediate" now)))
3005           header)
3006       (list
3007        (apply
3008         'nconc
3009         (list
3010          (if (eq type 'lower)
3011              "Lower score"
3012            "Increase score"))
3013         (let (outh)
3014           (while headers
3015             (setq header (car headers))
3016             (setq outh
3017                   (cons
3018                    (apply
3019                     'nconc
3020                     (list (car header))
3021                     (let ((ts (cdr (assoc (nth 2 header) types)))
3022                           outt)
3023                       (while ts
3024                         (setq outt
3025                               (cons
3026                                (apply
3027                                 'nconc
3028                                 (list (caar ts))
3029                                 (let ((ps perms)
3030                                       outp)
3031                                   (while ps
3032                                     (setq outp
3033                                           (cons
3034                                            (vector
3035                                             (caar ps)
3036                                             (list
3037                                              'gnus-summary-score-entry
3038                                              (nth 1 header)
3039                                              (if (or (string= (nth 1 header)
3040                                                               "head")
3041                                                      (string= (nth 1 header)
3042                                                               "body"))
3043                                                  ""
3044                                                (list 'gnus-summary-header
3045                                                      (nth 1 header)))
3046                                              (list 'quote (nth 1 (car ts)))
3047                                              (list 'gnus-score-delta-default
3048                                                    nil)
3049                                              (nth 1 (car ps))
3050                                              t)
3051                                             t)
3052                                            outp))
3053                                     (setq ps (cdr ps)))
3054                                   (list (nreverse outp))))
3055                                outt))
3056                         (setq ts (cdr ts)))
3057                       (list (nreverse outt))))
3058                    outh))
3059             (setq headers (cdr headers)))
3060           (list (nreverse outh))))))))
3061
3062
3063 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3064 (defvar bookmark-make-record-function)
3065 \f
3066 (defvar bidi-paragraph-direction)
3067
3068 (defun gnus-summary-mode (&optional group)
3069   "Major mode for reading articles.
3070
3071 All normal editing commands are switched off.
3072 \\<gnus-summary-mode-map>
3073 Each line in this buffer represents one article.  To read an
3074 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3075 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3076 respectively.
3077
3078 You can also post articles and send mail from this buffer.  To
3079 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3080 of an article, type `\\[gnus-summary-reply]'.
3081
3082 There are approx. one gazillion commands you can execute in this
3083 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3084
3085 The following commands are available:
3086
3087 \\{gnus-summary-mode-map}"
3088   (interactive)
3089   (kill-all-local-variables)
3090   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3091     (gnus-summary-make-local-variables))
3092   (gnus-summary-make-local-variables)
3093   (setq gnus-newsgroup-name group)
3094   (when (gnus-visual-p 'summary-menu 'menu)
3095     (gnus-summary-make-menu-bar)
3096     (gnus-summary-make-tool-bar))
3097   (gnus-make-thread-indent-array)
3098   (gnus-simplify-mode-line)
3099   (setq major-mode 'gnus-summary-mode)
3100   (setq mode-name "Summary")
3101   (use-local-map gnus-summary-mode-map)
3102   (buffer-disable-undo)
3103   (setq buffer-read-only t              ;Disable modification
3104         show-trailing-whitespace nil)
3105   (setq truncate-lines t)
3106   ;; Force paragraph direction to be left-to-right.  Don't make it
3107   ;; bound globally in old Emacsen and XEmacsen.
3108   (set (make-local-variable 'bidi-paragraph-direction) 'left-to-right)
3109   (add-to-invisibility-spec '(gnus-sum . t))
3110   (gnus-summary-set-display-table)
3111   (gnus-set-default-directory)
3112   (make-local-variable 'gnus-summary-line-format)
3113   (make-local-variable 'gnus-summary-line-format-spec)
3114   (make-local-variable 'gnus-summary-dummy-line-format)
3115   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3116   (make-local-variable 'gnus-summary-mark-positions)
3117   (gnus-make-local-hook 'pre-command-hook)
3118   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3119   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3120   (turn-on-gnus-mailing-list-mode)
3121   (mm-enable-multibyte)
3122   (set (make-local-variable 'bookmark-make-record-function)
3123        'gnus-summary-bookmark-make-record)
3124   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3125   (gnus-update-summary-mark-positions))
3126
3127 (defun gnus-summary-make-local-variables ()
3128   "Make all the local summary buffer variables."
3129   (let (global)
3130     (dolist (local gnus-summary-local-variables)
3131       (if (consp local)
3132           (progn
3133             (if (eq (cdr local) 'global)
3134                 ;; Copy the global value of the variable.
3135                 (setq global (symbol-value (car local)))
3136               ;; Use the value from the list.
3137               (setq global (eval (cdr local))))
3138             (set (make-local-variable (car local)) global))
3139         ;; Simple nil-valued local variable.
3140         (set (make-local-variable local) nil)))))
3141
3142 ;; Summary data functions.
3143
3144 (defmacro gnus-data-number (data)
3145   `(car ,data))
3146
3147 (defmacro gnus-data-set-number (data number)
3148   `(setcar ,data ,number))
3149
3150 (defmacro gnus-data-mark (data)
3151   `(nth 1 ,data))
3152
3153 (defmacro gnus-data-set-mark (data mark)
3154   `(setcar (nthcdr 1 ,data) ,mark))
3155
3156 (defmacro gnus-data-pos (data)
3157   `(nth 2 ,data))
3158
3159 (defmacro gnus-data-set-pos (data pos)
3160   `(setcar (nthcdr 2 ,data) ,pos))
3161
3162 (defmacro gnus-data-header (data)
3163   `(nth 3 ,data))
3164
3165 (defmacro gnus-data-set-header (data header)
3166   `(setf (nth 3 ,data) ,header))
3167
3168 (defmacro gnus-data-level (data)
3169   `(nth 4 ,data))
3170
3171 (defmacro gnus-data-unread-p (data)
3172   `(= (nth 1 ,data) gnus-unread-mark))
3173
3174 (defmacro gnus-data-read-p (data)
3175   `(/= (nth 1 ,data) gnus-unread-mark))
3176
3177 (defmacro gnus-data-pseudo-p (data)
3178   `(consp (nth 3 ,data)))
3179
3180 (defmacro gnus-data-find (number)
3181   `(assq ,number gnus-newsgroup-data))
3182
3183 (defmacro gnus-data-find-list (number &optional data)
3184   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3185      (memq (assq ,number bdata)
3186            bdata)))
3187
3188 (defmacro gnus-data-make (number mark pos header level)
3189   `(list ,number ,mark ,pos ,header ,level))
3190
3191 (defun gnus-data-enter (after-article number mark pos header level offset)
3192   (let ((data (gnus-data-find-list after-article)))
3193     (unless data
3194       (error "No such article: %d" after-article))
3195     (setcdr data (cons (gnus-data-make number mark pos header level)
3196                        (cdr data)))
3197     (setq gnus-newsgroup-data-reverse nil)
3198     (gnus-data-update-list (cddr data) offset)))
3199
3200 (defun gnus-data-enter-list (after-article list &optional offset)
3201   (when list
3202     (let ((data (and after-article (gnus-data-find-list after-article)))
3203           (ilist list))
3204       (if (not (or data
3205                    after-article))
3206           (let ((odata gnus-newsgroup-data))
3207             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3208             (when offset
3209               (gnus-data-update-list odata offset)))
3210         ;; Find the last element in the list to be spliced into the main
3211         ;; list.
3212         (setq list (last list))
3213         (if (not data)
3214             (progn
3215               (setcdr list gnus-newsgroup-data)
3216               (setq gnus-newsgroup-data ilist)
3217               (when offset
3218                 (gnus-data-update-list (cdr list) offset)))
3219           (setcdr list (cdr data))
3220           (setcdr data ilist)
3221           (when offset
3222             (gnus-data-update-list (cdr list) offset))))
3223       (setq gnus-newsgroup-data-reverse nil))))
3224
3225 (defun gnus-data-remove (article &optional offset)
3226   (let ((data gnus-newsgroup-data))
3227     (if (= (gnus-data-number (car data)) article)
3228         (progn
3229           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3230                 gnus-newsgroup-data-reverse nil)
3231           (when offset
3232             (gnus-data-update-list gnus-newsgroup-data offset)))
3233       (while (cdr data)
3234         (when (= (gnus-data-number (cadr data)) article)
3235           (setcdr data (cddr data))
3236           (when offset
3237             (gnus-data-update-list (cdr data) offset))
3238           (setq data nil
3239                 gnus-newsgroup-data-reverse nil))
3240         (setq data (cdr data))))))
3241
3242 (defmacro gnus-data-list (backward)
3243   `(if ,backward
3244        (or gnus-newsgroup-data-reverse
3245            (setq gnus-newsgroup-data-reverse
3246                  (reverse gnus-newsgroup-data)))
3247      gnus-newsgroup-data))
3248
3249 (defun gnus-data-update-list (data offset)
3250   "Add OFFSET to the POS of all data entries in DATA."
3251   (setq gnus-newsgroup-data-reverse nil)
3252   (while data
3253     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3254     (setq data (cdr data))))
3255
3256 (defun gnus-summary-article-pseudo-p (article)
3257   "Say whether this article is a pseudo article or not."
3258   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3259
3260 (defmacro gnus-summary-article-sparse-p (article)
3261   "Say whether this article is a sparse article or not."
3262   `(memq ,article gnus-newsgroup-sparse))
3263
3264 (defmacro gnus-summary-article-ancient-p (article)
3265   "Say whether this article is a sparse article or not."
3266   `(memq ,article gnus-newsgroup-ancient))
3267
3268 (defun gnus-article-children (number)
3269   "Return a list of all children to NUMBER."
3270   (let* ((data (gnus-data-find-list number))
3271          (level (gnus-data-level (car data)))
3272          children)
3273     (setq data (cdr data))
3274     (while (and data
3275                 (= (gnus-data-level (car data)) (1+ level)))
3276       (push (gnus-data-number (car data)) children)
3277       (setq data (cdr data)))
3278     children))
3279
3280 (defmacro gnus-summary-skip-intangible ()
3281   "If the current article is intangible, then jump to a different article."
3282   '(let ((to (get-text-property (point) 'gnus-intangible)))
3283      (and to (gnus-summary-goto-subject to))))
3284
3285 (defmacro gnus-summary-article-intangible-p ()
3286   "Say whether this article is intangible or not."
3287   '(get-text-property (point) 'gnus-intangible))
3288
3289 ;; Some summary mode macros.
3290
3291 (defmacro gnus-summary-article-number ()
3292   "The article number of the article on the current line.
3293 If there isn't an article number here, then we return the current
3294 article number."
3295   '(progn
3296      (gnus-summary-skip-intangible)
3297      (or (get-text-property (point) 'gnus-number)
3298          (gnus-summary-last-subject))))
3299
3300 (defmacro gnus-summary-article-header (&optional number)
3301   "Return the header of article NUMBER."
3302   `(gnus-data-header (gnus-data-find
3303                       ,(or number '(gnus-summary-article-number)))))
3304
3305 (defmacro gnus-summary-thread-level (&optional number)
3306   "Return the level of thread that starts with article NUMBER."
3307   `(if (and (eq gnus-summary-make-false-root 'dummy)
3308             (get-text-property (point) 'gnus-intangible))
3309        0
3310      (gnus-data-level (gnus-data-find
3311                        ,(or number '(gnus-summary-article-number))))))
3312
3313 (defmacro gnus-summary-article-mark (&optional number)
3314   "Return the mark of article NUMBER."
3315   `(gnus-data-mark (gnus-data-find
3316                     ,(or number '(gnus-summary-article-number)))))
3317
3318 (defmacro gnus-summary-article-pos (&optional number)
3319   "Return the position of the line of article NUMBER."
3320   `(gnus-data-pos (gnus-data-find
3321                    ,(or number '(gnus-summary-article-number)))))
3322
3323 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3324 (defmacro gnus-summary-article-subject (&optional number)
3325   "Return current subject string or nil if nothing."
3326   `(let ((headers
3327           ,(if number
3328                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3329              '(gnus-data-header (assq (gnus-summary-article-number)
3330                                       gnus-newsgroup-data)))))
3331      (and headers
3332           (vectorp headers)
3333           (mail-header-subject headers))))
3334
3335 (defmacro gnus-summary-article-score (&optional number)
3336   "Return current article score."
3337   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3338                   gnus-newsgroup-scored))
3339        gnus-summary-default-score 0))
3340
3341 (defun gnus-summary-article-children (&optional number)
3342   "Return a list of article numbers that are children of article NUMBER."
3343   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3344          (level (gnus-data-level (car data)))
3345          l children)
3346     (while (and (setq data (cdr data))
3347                 (> (setq l (gnus-data-level (car data))) level))
3348       (and (= (1+ level) l)
3349            (push (gnus-data-number (car data))
3350                  children)))
3351     (nreverse children)))
3352
3353 (defun gnus-summary-article-parent (&optional number)
3354   "Return the article number of the parent of article NUMBER."
3355   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3356                                     (gnus-data-list t)))
3357          (level (gnus-data-level (car data))))
3358     (if (zerop level)
3359         ()                              ; This is a root.
3360       ;; We search until we find an article with a level less than
3361       ;; this one.  That function has to be the parent.
3362       (while (and (setq data (cdr data))
3363                   (not (< (gnus-data-level (car data)) level))))
3364       (and data (gnus-data-number (car data))))))
3365
3366 (defun gnus-unread-mark-p (mark)
3367   "Say whether MARK is the unread mark."
3368   (= mark gnus-unread-mark))
3369
3370 (defun gnus-read-mark-p (mark)
3371   "Say whether MARK is one of the marks that mark as read.
3372 This is all marks except unread, ticked, dormant, and expirable."
3373   (not (or (= mark gnus-unread-mark)
3374            (= mark gnus-ticked-mark)
3375            (= mark gnus-spam-mark)
3376            (= mark gnus-dormant-mark)
3377            (= mark gnus-expirable-mark))))
3378
3379 (defmacro gnus-article-mark (number)
3380   "Return the MARK of article NUMBER.
3381 This macro should only be used when computing the mark the \"first\"
3382 time; i.e., when generating the summary lines.  After that,
3383 `gnus-summary-article-mark' should be used to examine the
3384 marks of articles."
3385   `(cond
3386     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3387     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3388     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3389     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3390     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3391     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3392     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3393     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3394            gnus-ancient-mark))))
3395
3396 ;; Saving hidden threads.
3397
3398 (defmacro gnus-save-hidden-threads (&rest forms)
3399   "Save hidden threads, eval FORMS, and restore the hidden threads."
3400   (let ((config (make-symbol "config")))
3401     `(let ((,config (gnus-hidden-threads-configuration)))
3402        (unwind-protect
3403            (save-excursion
3404              ,@forms)
3405          (gnus-restore-hidden-threads-configuration ,config)))))
3406 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3407 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3408
3409 (defun gnus-data-compute-positions ()
3410   "Compute the positions of all articles."
3411   (setq gnus-newsgroup-data-reverse nil)
3412   (let ((data gnus-newsgroup-data))
3413     (save-excursion
3414       (gnus-save-hidden-threads
3415         (gnus-summary-show-all-threads)
3416         (goto-char (point-min))
3417         (while data
3418           (while (get-text-property (point) 'gnus-intangible)
3419             (forward-line 1))
3420           (gnus-data-set-pos (car data) (+ (point) 3))
3421           (setq data (cdr data))
3422           (forward-line 1))))))
3423
3424 (defun gnus-hidden-threads-configuration ()
3425   "Return the current hidden threads configuration."
3426   (save-excursion
3427     (let (config)
3428       (goto-char (point-min))
3429       (while (not (eobp))
3430         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3431           (push (save-excursion (forward-line 0) (point)) config))
3432         (forward-line 1))
3433       config)))
3434
3435 (defun gnus-restore-hidden-threads-configuration (config)
3436   "Restore hidden threads configuration from CONFIG."
3437   (save-excursion
3438     (let (point (inhibit-read-only t))
3439       (while (setq point (pop config))
3440         (goto-char point)
3441         (gnus-summary-hide-thread)))))
3442
3443 ;; Various summary mode internalish functions.
3444
3445 (defun gnus-mouse-pick-article (e)
3446   (interactive "e")
3447   (mouse-set-point e)
3448   (gnus-summary-next-page nil t))
3449
3450 (defun gnus-summary-set-display-table ()
3451   "Change the display table.
3452 Odd characters have a tendency to mess
3453 up nicely formatted displays - we make all possible glyphs
3454 display only a single character."
3455
3456   ;; We start from the standard display table, if any.
3457   (let ((table (or (copy-sequence standard-display-table)
3458                    (make-display-table)))
3459         (i 32))
3460     ;; Nix out all the control chars...
3461     (while (>= (setq i (1- i)) 0)
3462       (gnus-put-display-table i [??] table))
3463    ;; ... but not newline and cr, of course.  (cr is necessary for the
3464     ;; selective display).
3465     (gnus-put-display-table ?\n nil table)
3466     (gnus-put-display-table ?\r nil table)
3467     ;; We keep TAB as well.
3468     (gnus-put-display-table ?\t nil table)
3469     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3470     ;; Emacs 23 (unicode), that are not set already.
3471     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3472                  160
3473                256)))
3474       (while (>= (setq i (1- i)) 127)
3475         ;; Only modify if the entry is nil.
3476         (unless (gnus-get-display-table i table)
3477           (gnus-put-display-table i [??] table))))
3478     (setq buffer-display-table table)))
3479
3480 (defun gnus-summary-set-article-display-arrow (pos)
3481   "Update the overlay arrow to point to line at position POS."
3482   (when gnus-summary-display-arrow
3483     (make-local-variable 'overlay-arrow-position)
3484     (make-local-variable 'overlay-arrow-string)
3485     (save-excursion
3486       (goto-char pos)
3487       (beginning-of-line)
3488       (unless overlay-arrow-position
3489         (setq overlay-arrow-position (make-marker)))
3490       (setq overlay-arrow-string "=>"
3491             overlay-arrow-position (set-marker overlay-arrow-position
3492                                                (point)
3493                                                (current-buffer))))))
3494
3495 (defun gnus-summary-setup-buffer (group)
3496   "Initialize summary buffer.
3497 If the setup was successful, non-nil is returned."
3498   (let ((buffer (gnus-summary-buffer-name group))
3499         (dead-name (concat "*Dead Summary "
3500                            (gnus-group-decoded-name group) "*")))
3501     ;; If a dead summary buffer exists, we kill it.
3502     (when (gnus-buffer-live-p dead-name)
3503       (gnus-kill-buffer dead-name))
3504     (if (get-buffer buffer)
3505         (progn
3506           (set-buffer buffer)
3507           (setq gnus-summary-buffer (current-buffer))
3508           (not gnus-newsgroup-prepared))
3509       (set-buffer (gnus-get-buffer-create buffer))
3510       (setq gnus-summary-buffer (current-buffer))
3511       (gnus-summary-mode group)
3512       (when (gnus-group-quit-config group)
3513         (set (make-local-variable 'gnus-single-article-buffer) nil))
3514       (make-local-variable 'gnus-article-buffer)
3515       (make-local-variable 'gnus-article-current)
3516       (make-local-variable 'gnus-original-article-buffer)
3517       (setq gnus-newsgroup-name group)
3518       ;; Set any local variables in the group parameters.
3519       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3520       t)))
3521
3522 (defun gnus-set-global-variables ()
3523   "Set the global equivalents of the buffer-local variables.
3524 They are set to the latest values they had.  These reflect the summary
3525 buffer that was in action when the last article was fetched."
3526   (when (eq major-mode 'gnus-summary-mode)
3527     (setq gnus-summary-buffer (current-buffer))
3528     (let ((name gnus-newsgroup-name)
3529           (marked gnus-newsgroup-marked)
3530           (spam gnus-newsgroup-spam-marked)
3531           (unread gnus-newsgroup-unreads)
3532           (headers gnus-current-headers)
3533           (data gnus-newsgroup-data)
3534           (summary gnus-summary-buffer)
3535           (article-buffer gnus-article-buffer)
3536           (original gnus-original-article-buffer)
3537           (gac gnus-article-current)
3538           (reffed gnus-reffed-article-number)
3539           (score-file gnus-current-score-file)
3540           (default-charset gnus-newsgroup-charset)
3541           vlist)
3542       (let ((locals gnus-newsgroup-variables))
3543         (while locals
3544           (if (consp (car locals))
3545               (push (eval (caar locals)) vlist)
3546             (push (eval (car locals)) vlist))
3547           (setq locals (cdr locals)))
3548         (setq vlist (nreverse vlist)))
3549       (with-temp-buffer
3550         (setq gnus-newsgroup-name name
3551               gnus-newsgroup-marked marked
3552               gnus-newsgroup-spam-marked spam
3553               gnus-newsgroup-unreads unread
3554               gnus-current-headers headers
3555               gnus-newsgroup-data data
3556               gnus-article-current gac
3557               gnus-summary-buffer summary
3558               gnus-article-buffer article-buffer
3559               gnus-original-article-buffer original
3560               gnus-reffed-article-number reffed
3561               gnus-current-score-file score-file
3562               gnus-newsgroup-charset default-charset)
3563         (let ((locals gnus-newsgroup-variables))
3564           (while locals
3565             (if (consp (car locals))
3566                 (set (caar locals) (pop vlist))
3567               (set (car locals) (pop vlist)))
3568             (setq locals (cdr locals))))))))
3569
3570 (defun gnus-summary-article-unread-p (article)
3571   "Say whether ARTICLE is unread or not."
3572   (memq article gnus-newsgroup-unreads))
3573
3574 (defun gnus-summary-first-article-p (&optional article)
3575   "Return whether ARTICLE is the first article in the buffer."
3576   (if (not (setq article (or article (gnus-summary-article-number))))
3577       nil
3578     (eq article (caar gnus-newsgroup-data))))
3579
3580 (defun gnus-summary-last-article-p (&optional article)
3581   "Return whether ARTICLE is the last article in the buffer."
3582   (if (not (setq article (or article (gnus-summary-article-number))))
3583       ;; All non-existent numbers are the last article.  :-)
3584       t
3585     (not (cdr (gnus-data-find-list article)))))
3586
3587 (defun gnus-make-thread-indent-array (&optional n)
3588   (when (or n
3589             (progn (setq n 200) nil)
3590             (null gnus-thread-indent-array)
3591             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3592     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3593           gnus-thread-indent-array-level gnus-thread-indent-level)
3594     (while (>= n 0)
3595       (aset gnus-thread-indent-array n
3596             (make-string (* n gnus-thread-indent-level) ? ))
3597       (setq n (1- n)))))
3598
3599 (defun gnus-update-summary-mark-positions ()
3600   "Compute where the summary marks are to go."
3601   (save-excursion
3602     (when (gnus-buffer-exists-p gnus-summary-buffer)
3603       (set-buffer gnus-summary-buffer))
3604     (let ((spec gnus-summary-line-format-spec)
3605           pos)
3606       (save-excursion
3607         (gnus-set-work-buffer)
3608         (let ((gnus-tmp-unread ?Z)
3609               (gnus-replied-mark ?Z)
3610               (gnus-score-below-mark ?Z)
3611               (gnus-score-over-mark ?Z)
3612               (gnus-undownloaded-mark ?Z)
3613               (gnus-summary-line-format-spec spec)
3614               (gnus-newsgroup-downloadable '(0))
3615               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3616               case-fold-search ignores)
3617           ;; Here, all marks are bound to Z.
3618           (gnus-summary-insert-line header
3619                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3620           (goto-char (point-min))
3621           ;; Memorize the positions of the same characters as dummy marks.
3622           (while (re-search-forward "[A-D]" nil t)
3623             (push (point) ignores))
3624           (erase-buffer)
3625           ;; We use A-D as dummy marks in order to know column positions
3626           ;; where marks should be inserted.
3627           (setq gnus-tmp-unread ?A
3628                 gnus-replied-mark ?B
3629                 gnus-score-below-mark ?C
3630                 gnus-score-over-mark ?C
3631                 gnus-undownloaded-mark ?D)
3632           (gnus-summary-insert-line header
3633                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3634           ;; Ignore characters which aren't dummy marks.
3635           (dolist (p ignores)
3636             (delete-region (goto-char (1- p)) p)
3637             (insert ?Z))
3638           (goto-char (point-min))
3639           (setq pos (list (cons 'unread
3640                                 (and (search-forward "A" nil t)
3641                                      (- (point) (point-min) 1)))))
3642           (goto-char (point-min))
3643           (push (cons 'replied (and (search-forward "B" nil t)
3644                                     (- (point) (point-min) 1)))
3645                 pos)
3646           (goto-char (point-min))
3647           (push (cons 'score (and (search-forward "C" nil t)
3648                                   (- (point) (point-min) 1)))
3649                 pos)
3650           (goto-char (point-min))
3651           (push (cons 'download (and (search-forward "D" nil t)
3652                                      (- (point) (point-min) 1)))
3653                 pos)))
3654       (setq gnus-summary-mark-positions pos))))
3655
3656 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3657   "Insert a dummy root in the summary buffer."
3658   (beginning-of-line)
3659   (gnus-add-text-properties
3660    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3661    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3662
3663 (defun gnus-summary-extract-address-component (from)
3664   (or (car (funcall gnus-extract-address-components from))
3665       from))
3666
3667 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3668   (let ((mail-parse-charset gnus-newsgroup-charset)
3669         (ignored-from-addresses (gnus-ignored-from-addresses))
3670         ;; Is it really necessary to do this next part for each summary line?
3671         ;; Luckily, doesn't seem to slow things down much.
3672         (mail-parse-ignored-charsets
3673          (with-current-buffer gnus-summary-buffer
3674            gnus-newsgroup-ignored-charsets)))
3675     (or
3676      (and ignored-from-addresses
3677           (string-match ignored-from-addresses gnus-tmp-from)
3678           (let ((extra-headers (mail-header-extra header))
3679                 to
3680                 newsgroups)
3681             (cond
3682              ((setq to (cdr (assq 'To extra-headers)))
3683               (concat gnus-summary-to-prefix
3684                       (inline
3685                         (gnus-summary-extract-address-component
3686                          (funcall gnus-decode-encoded-address-function to)))))
3687              ((setq newsgroups
3688                     (or
3689                      (cdr (assq 'Newsgroups extra-headers))
3690                      (and
3691                       (memq 'Newsgroups gnus-extra-headers)
3692                       (eq (car (gnus-find-method-for-group
3693                                 gnus-newsgroup-name)) 'nntp)
3694                       (gnus-group-real-name gnus-newsgroup-name))))
3695               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3696      (gnus-string-mark-left-to-right
3697       (inline
3698         (gnus-summary-extract-address-component gnus-tmp-from))))))
3699
3700 (defun gnus-summary-insert-line (gnus-tmp-header
3701                                  gnus-tmp-level gnus-tmp-current
3702                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3703                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3704                                  &optional gnus-tmp-dummy gnus-tmp-score
3705                                  gnus-tmp-process)
3706   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3707       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3708                                           gnus-tmp-level)))
3709   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3710          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3711          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3712          (gnus-tmp-score-char
3713           (if (or (null gnus-summary-default-score)
3714                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3715                       gnus-summary-zcore-fuzz))
3716               ?                         ;Whitespace
3717             (if (< gnus-tmp-score gnus-summary-default-score)
3718                 gnus-score-below-mark gnus-score-over-mark)))
3719          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3720          (gnus-tmp-replied
3721           (cond (gnus-tmp-process gnus-process-mark)
3722                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3723                  gnus-cached-mark)
3724                 (gnus-tmp-replied gnus-replied-mark)
3725                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3726                  gnus-forwarded-mark)
3727                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3728                  gnus-saved-mark)
3729                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3730                  gnus-unseen-mark)
3731                 (t gnus-no-mark)))
3732          (gnus-tmp-downloaded
3733           (cond (undownloaded
3734                  gnus-undownloaded-mark)
3735                 (gnus-newsgroup-agentized
3736                  gnus-downloaded-mark)
3737                 (t
3738                  gnus-no-mark)))
3739          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3740          (gnus-tmp-name
3741           (cond
3742            ((string-match "<[^>]+> *$" gnus-tmp-from)
3743             (let ((beg (match-beginning 0)))
3744               (or (and (string-match "^\".+\"" gnus-tmp-from)
3745                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3746                   (substring gnus-tmp-from 0 beg))))
3747            ((string-match "(.+)" gnus-tmp-from)
3748             (substring gnus-tmp-from
3749                        (1+ (match-beginning 0)) (1- (match-end 0))))
3750            (t gnus-tmp-from)))
3751          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3752          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3753          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3754          (inhibit-read-only t))
3755     (when (string= gnus-tmp-name "")
3756       (setq gnus-tmp-name gnus-tmp-from))
3757     (unless (numberp gnus-tmp-lines)
3758       (setq gnus-tmp-lines -1))
3759     (if (= gnus-tmp-lines -1)
3760         (setq gnus-tmp-lines "?")
3761       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3762     (condition-case ()
3763         (gnus-put-text-property
3764          (point)
3765          (progn (eval gnus-summary-line-format-spec) (point))
3766          'gnus-number gnus-tmp-number)
3767       (error (gnus-message 5 "Error updating the summary line")))
3768     (when (gnus-visual-p 'summary-highlight 'highlight)
3769       (forward-line -1)
3770       (gnus-summary-highlight-line)
3771       (gnus-run-hooks 'gnus-summary-update-hook)
3772       (forward-line 1))))
3773
3774 (defun gnus-summary-update-line (&optional dont-update)
3775   "Update summary line after change."
3776   (when (and gnus-summary-default-score
3777              (not gnus-summary-inhibit-highlight))
3778     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3779            (article (gnus-summary-article-number))
3780            (score (gnus-summary-article-score article)))
3781       (unless dont-update
3782         (if (and gnus-summary-mark-below
3783                  (< (gnus-summary-article-score)
3784                     gnus-summary-mark-below))
3785             ;; This article has a low score, so we mark it as read.
3786             (when (memq article gnus-newsgroup-unreads)
3787               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3788           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3789             ;; This article was previously marked as read on account
3790             ;; of a low score, but now it has risen, so we mark it as
3791             ;; unread.
3792             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3793         (gnus-summary-update-mark
3794          (if (or (null gnus-summary-default-score)
3795                  (<= (abs (- score gnus-summary-default-score))
3796                      gnus-summary-zcore-fuzz))
3797              ?                          ;Whitespace
3798            (if (< score gnus-summary-default-score)
3799                gnus-score-below-mark gnus-score-over-mark))
3800          'score))
3801       ;; Do visual highlighting.
3802       (when (gnus-visual-p 'summary-highlight 'highlight)
3803         (gnus-summary-highlight-line)
3804         (gnus-run-hooks 'gnus-summary-update-hook)))))
3805
3806 (defvar gnus-tmp-new-adopts nil)
3807
3808 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3809   "Return the number of articles in THREAD.
3810 This may be 0 in some cases -- if none of the articles in
3811 the thread are to be displayed."
3812   (let* ((number
3813          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3814           (cond
3815            ((not (listp thread))
3816             1)
3817            ((and (consp thread) (cdr thread))
3818             (apply
3819              '+ 1 (mapcar
3820                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3821            ((null thread)
3822             1)
3823            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3824             1)
3825            (t 0))))
3826     (when (and level (zerop level) gnus-tmp-new-adopts)
3827       (incf number
3828             (apply '+ (mapcar
3829                        'gnus-summary-number-of-articles-in-thread
3830                        gnus-tmp-new-adopts))))
3831     (if char
3832         (if (> number 1) gnus-not-empty-thread-mark
3833           gnus-empty-thread-mark)
3834       number)))
3835
3836 (defsubst gnus-summary-line-message-size (head)
3837   "Return pretty-printed version of message size.
3838 This function is intended to be used in
3839 `gnus-summary-line-format-alist'."
3840   (let ((c (or (mail-header-chars head) -1)))
3841     (cond ((< c 0) "n/a")               ; chars not available
3842           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3843           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3844           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3845           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3846
3847 (defcustom gnus-user-date-format-alist
3848   '(((gnus-seconds-today) . "Today, %H:%M")
3849     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3850     (604800 . "%A %H:%M")               ; That's one week
3851     ((gnus-seconds-month) . "%A %d")
3852     ((gnus-seconds-year) . "%B %d")
3853     (t . "%b %d %Y"))                   ; This one is used when no other
3854                                         ; does match
3855   "Specifies date format depending on age of article.
3856 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3857 seconds) or a Lisp expression evaluating to a number.  When the age of
3858 the article is less than this number, then use `format-time-string'
3859 with the corresponding FORMAT for displaying the date of the article.
3860 If AGE is not a number or a Lisp expression evaluating to a
3861 non-number, then the corresponding FORMAT is used as a default value.
3862
3863 Note that the list is processed from the beginning, so it should be
3864 sorted by ascending AGE.  Also note that items following the first
3865 non-number AGE will be ignored.
3866
3867 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3868 and `gnus-seconds-year' in the AGE spec.  They return the number of
3869 seconds passed since the start of today, of this month, of this year,
3870 respectively."
3871   :version "24.1"
3872   :group 'gnus-summary-format
3873   :type '(alist :key-type sexp :value-type string))
3874
3875 (defun gnus-user-date (messy-date)
3876   "Format the messy-date according to `gnus-user-date-format-alist'.
3877 Returns \"  ?  \" if there's bad input or if another error occurs.
3878 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3879   (condition-case ()
3880       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3881              (now (gnus-float-time))
3882              ;;If we don't find something suitable we'll use this one
3883              (my-format "%b %d '%y"))
3884         (let* ((difference (- now messy-date))
3885                (templist gnus-user-date-format-alist)
3886                (top (eval (caar templist))))
3887           (while (if (numberp top) (< top difference) (not top))
3888             (progn
3889               (setq templist (cdr templist))
3890               (setq top (eval (caar templist)))))
3891           (if (stringp (cdr (car templist)))
3892               (setq my-format (cdr (car templist)))))
3893         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3894     (error "  ?   ")))
3895
3896 (defun gnus-summary-set-local-parameters (group)
3897   "Go through the local params of GROUP and set all variable specs in that list."
3898   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3899                                         ; really variables.
3900     (dolist (elem (gnus-group-find-parameter group))
3901       (and (consp elem)                 ; Has to be a cons.
3902            (consp (cdr elem))           ; The cdr has to be a list.
3903            (symbolp (car elem))         ; Has to be a symbol in there.
3904            (not (memq (car elem) vars))
3905            (ignore-errors
3906              (push (car elem) vars)
3907              ;; Variables like `gnus-show-threads' that are globally
3908              ;; bound, if used as group parameters, need to get to be
3909              ;; buffer-local, whereas just parameters like `gcc-self',
3910              ;; `timestamp', etc. should not be bound as variables.
3911              (if (boundp (car elem))
3912                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3913                (eval (nth 1 elem))))))))
3914
3915 (defun gnus-summary-read-group (group &optional show-all no-article
3916                                       kill-buffer no-display backward
3917                                       select-articles)
3918   "Start reading news in newsgroup GROUP.
3919 If SHOW-ALL is non-nil, already read articles are also listed.
3920 If NO-ARTICLE is non-nil, no article is selected initially.
3921 If NO-DISPLAY, don't generate the summary buffer contents.
3922 If KILL-BUFFER, it should be a buffer that's killed once the new
3923 summary buffer has been generated.
3924 If BACKWARD, move point to the previous group in the group buffer
3925 If SELECT-ARTICLES, only select those articles from GROUP."
3926   (let (result)
3927     (while (and group
3928                 (null (setq result
3929                             (let ((gnus-auto-select-next nil))
3930                               (or (gnus-summary-read-group-1
3931                                    group show-all no-article
3932                                    kill-buffer no-display
3933                                    select-articles)
3934                                   (setq show-all nil
3935                                         select-articles nil)))))
3936                 (eq gnus-auto-select-next 'quietly))
3937       (set-buffer gnus-group-buffer)
3938       ;; The entry function called above goes to the next
3939       ;; group automatically, so we go two groups back
3940       ;; if we are searching for the previous group.
3941       (when backward
3942         (gnus-group-prev-unread-group 2))
3943       (if (not (equal group (gnus-group-group-name)))
3944           (setq group (gnus-group-group-name))
3945         (setq group nil)))
3946     result))
3947
3948 (defun gnus-summary-read-group-1 (group show-all no-article
3949                                         kill-buffer no-display
3950                                         &optional select-articles)
3951   ;; Killed foreign groups can't be entered.
3952   ;;  (when (and (not (gnus-group-native-p group))
3953   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3954   ;;    (error "Dead non-native groups can't be entered"))
3955   (gnus-message 7 "Retrieving newsgroup: %s..."
3956                 (gnus-group-decoded-name group))
3957   (let* ((new-group (gnus-summary-setup-buffer group))
3958          (quit-config (gnus-group-quit-config group))
3959          (did-select (and new-group (gnus-select-newsgroup
3960                                      group show-all select-articles))))
3961     (cond
3962      ;; This summary buffer exists already, so we just select it.
3963      ((not new-group)
3964       (gnus-set-global-variables)
3965       (when kill-buffer
3966         (gnus-kill-or-deaden-summary kill-buffer))
3967       (gnus-configure-windows 'summary 'force)
3968       (gnus-set-mode-line 'summary)
3969       (gnus-summary-position-point)
3970       (message "")
3971       t)
3972      ;; We couldn't select this group.
3973      ((null did-select)
3974       (when (and (eq major-mode 'gnus-summary-mode)
3975                  (not (equal (current-buffer) kill-buffer)))
3976         (kill-buffer (current-buffer))
3977         (if (not quit-config)
3978             (progn
3979               ;; Update the info -- marks might need to be removed,
3980               ;; for instance.
3981               (gnus-summary-update-info)
3982               (set-buffer gnus-group-buffer)
3983               (gnus-group-jump-to-group group)
3984               (gnus-group-next-unread-group 1))
3985           (gnus-handle-ephemeral-exit quit-config)))
3986       (if (null (gnus-list-of-unread-articles group))
3987           (gnus-message 3 "Group %s contains no messages" group)
3988         (gnus-message 3 "Can't select group"))
3989       nil)
3990      ;; The user did a `C-g' while prompting for number of articles,
3991      ;; so we exit this group.
3992      ((eq did-select 'quit)
3993       (and (eq major-mode 'gnus-summary-mode)
3994            (not (equal (current-buffer) kill-buffer))
3995            (kill-buffer (current-buffer)))
3996       (when kill-buffer
3997         (gnus-kill-or-deaden-summary kill-buffer))
3998       (if (not quit-config)
3999           (progn
4000             (set-buffer gnus-group-buffer)
4001             (gnus-group-jump-to-group group)
4002             (gnus-configure-windows 'group 'force))
4003         (gnus-handle-ephemeral-exit quit-config))
4004       ;; Finally signal the quit.
4005       (signal 'quit nil))
4006      ;; The group was successfully selected.
4007      (t
4008       (gnus-set-global-variables)
4009       ;; Save the active value in effect when the group was entered.
4010       (setq gnus-newsgroup-active
4011             (gnus-copy-sequence
4012              (gnus-active gnus-newsgroup-name)))
4013       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4014       ;; You can change the summary buffer in some way with this hook.
4015       (gnus-run-hooks 'gnus-select-group-hook)
4016       (when (memq 'summary (gnus-update-format-specifications
4017                             nil 'summary 'summary-mode 'summary-dummy))
4018         ;; The format specification for the summary line was updated,
4019         ;; so we need to update the mark positions as well.
4020         (gnus-update-summary-mark-positions))
4021       ;; Do score processing.
4022       (when gnus-use-scoring
4023         (gnus-possibly-score-headers))
4024       ;; Check whether to fill in the gaps in the threads.
4025       (when gnus-build-sparse-threads
4026         (gnus-build-sparse-threads))
4027       ;; Find the initial limit.
4028       (if show-all
4029           (let ((gnus-newsgroup-dormant nil))
4030             (gnus-summary-initial-limit show-all))
4031         (gnus-summary-initial-limit show-all))
4032       ;; Generate the summary buffer.
4033       (unless no-display
4034         (gnus-summary-prepare))
4035       (when gnus-use-trees
4036         (gnus-tree-open group)
4037         (setq gnus-summary-highlight-line-function
4038               'gnus-tree-highlight-article))
4039       ;; If the summary buffer is empty, but there are some low-scored
4040       ;; articles or some excluded dormants, we include these in the
4041       ;; buffer.
4042       (when (and (zerop (buffer-size))
4043                  (not no-display))
4044         (cond (gnus-newsgroup-dormant
4045                (gnus-summary-limit-include-dormant))
4046               ((and gnus-newsgroup-scored show-all)
4047                (gnus-summary-limit-include-expunged t))))
4048       ;; Function `gnus-apply-kill-file' must be called in this hook.
4049       (gnus-run-hooks 'gnus-apply-kill-hook)
4050       (if (and (zerop (buffer-size))
4051                (not no-display))
4052           (progn
4053             ;; This newsgroup is empty.
4054             (gnus-summary-catchup-and-exit nil t)
4055             (gnus-message 6 "No unread news")
4056             (when kill-buffer
4057               (gnus-kill-or-deaden-summary kill-buffer))
4058             ;; Return nil from this function.
4059             nil)
4060         ;; Hide conversation thread subtrees.  We cannot do this in
4061         ;; gnus-summary-prepare-hook since kill processing may not
4062         ;; work with hidden articles.
4063         (gnus-summary-maybe-hide-threads)
4064         (gnus-configure-windows 'summary)
4065         (when kill-buffer
4066           (gnus-kill-or-deaden-summary kill-buffer))
4067         (gnus-summary-auto-select-subject)
4068         ;; Show first unread article if requested.
4069         (if (and (not no-article)
4070                  (not no-display)
4071                  gnus-newsgroup-unreads
4072                  gnus-auto-select-first)
4073             (progn
4074               (let ((art (gnus-summary-article-number)))
4075                 (when (and art
4076                            gnus-plugged
4077                            (not (memq art gnus-newsgroup-undownloaded))
4078                            (not (memq art gnus-newsgroup-downloadable)))
4079                   (gnus-summary-goto-article art))))
4080           ;; Don't select any articles.
4081           (gnus-summary-position-point)
4082           (gnus-configure-windows 'summary 'force)
4083           (gnus-set-mode-line 'summary))
4084         (when (and gnus-auto-center-group
4085                    (get-buffer-window gnus-group-buffer t))
4086           ;; Gotta use windows, because recenter does weird stuff if
4087           ;; the current buffer ain't the displayed window.
4088           (let ((owin (selected-window)))
4089             (select-window (get-buffer-window gnus-group-buffer t))
4090             (when (gnus-group-goto-group group)
4091               (recenter))
4092             (select-window owin)))
4093         ;; Mark this buffer as "prepared".
4094         (setq gnus-newsgroup-prepared t)
4095         (gnus-run-hooks 'gnus-summary-prepared-hook)
4096         (unless (gnus-ephemeral-group-p group)
4097           (gnus-group-update-group group nil t))
4098         t)))))
4099
4100 (defun gnus-summary-auto-select-subject ()
4101   "Select the subject line on initial group entry."
4102   (goto-char (point-min))
4103   (cond
4104    ((eq gnus-auto-select-subject 'best)
4105     (gnus-summary-best-unread-subject))
4106    ((eq gnus-auto-select-subject 'unread)
4107     (gnus-summary-first-unread-subject))
4108    ((eq gnus-auto-select-subject 'unseen)
4109     (gnus-summary-first-unseen-subject))
4110    ((eq gnus-auto-select-subject 'unseen-or-unread)
4111     (gnus-summary-first-unseen-or-unread-subject))
4112    ((eq gnus-auto-select-subject 'first)
4113     ;; Do nothing.
4114     )
4115    ((functionp gnus-auto-select-subject)
4116     (funcall gnus-auto-select-subject))))
4117
4118 (defun gnus-summary-prepare ()
4119   "Generate the summary buffer."
4120   (interactive)
4121   (let ((inhibit-read-only t))
4122     (erase-buffer)
4123     (setq gnus-newsgroup-data nil
4124           gnus-newsgroup-data-reverse nil)
4125     (gnus-run-hooks 'gnus-summary-generate-hook)
4126     ;; Generate the buffer, either with threads or without.
4127     (when gnus-newsgroup-headers
4128       (gnus-summary-prepare-threads
4129        (if gnus-show-threads
4130            (gnus-sort-gathered-threads
4131             (funcall gnus-summary-thread-gathering-function
4132                      (gnus-sort-threads
4133                       (gnus-cut-threads (gnus-make-threads)))))
4134          ;; Unthreaded display.
4135          (gnus-sort-articles gnus-newsgroup-headers))))
4136     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4137     ;; Call hooks for modifying summary buffer.
4138     (goto-char (point-min))
4139     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4140
4141 (defsubst gnus-general-simplify-subject (subject)
4142   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4143   (setq subject
4144         (cond
4145          ;; Truncate the subject.
4146          (gnus-simplify-subject-functions
4147           (gnus-map-function gnus-simplify-subject-functions subject))
4148          ((numberp gnus-summary-gather-subject-limit)
4149           (setq subject (gnus-simplify-subject-re subject))
4150           (if (> (length subject) gnus-summary-gather-subject-limit)
4151               (substring subject 0 gnus-summary-gather-subject-limit)
4152             subject))
4153          ;; Fuzzily simplify it.
4154          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4155           (gnus-simplify-subject-fuzzy subject))
4156          ;; Just remove the leading "Re:".
4157          (t
4158           (gnus-simplify-subject-re subject))))
4159
4160   (if (and gnus-summary-gather-exclude-subject
4161            (string-match gnus-summary-gather-exclude-subject subject))
4162       nil                         ; This article shouldn't be gathered
4163     subject))
4164
4165 (defun gnus-summary-simplify-subject-query ()
4166   "Query where the respool algorithm would put this article."
4167   (interactive)
4168   (gnus-summary-select-article)
4169   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4170
4171 (defun gnus-gather-threads-by-subject (threads)
4172   "Gather threads by looking at Subject headers."
4173   (if (not gnus-summary-make-false-root)
4174       threads
4175     (let ((hashtb (gnus-make-hashtable 1024))
4176           (prev threads)
4177           (result threads)
4178           subject hthread whole-subject)
4179       (while threads
4180         (setq subject (gnus-general-simplify-subject
4181                        (setq whole-subject (mail-header-subject
4182                                             (caar threads)))))
4183         (when subject
4184           (if (setq hthread (gnus-gethash subject hashtb))
4185               (progn
4186                 ;; We enter a dummy root into the thread, if we
4187                 ;; haven't done that already.
4188                 (unless (stringp (caar hthread))
4189                   (setcar hthread (list whole-subject (car hthread))))
4190                 ;; We add this new gathered thread to this gathered
4191                 ;; thread.
4192                 (setcdr (car hthread)
4193                         (nconc (cdar hthread) (list (car threads))))
4194                 ;; Remove it from the list of threads.
4195                 (setcdr prev (cdr threads))
4196                 (setq threads prev))
4197             ;; Enter this thread into the hash table.
4198             (gnus-sethash subject
4199                           (if gnus-summary-make-false-root-always
4200                               (progn
4201                                 ;; If you want a dummy root above all
4202                                 ;; threads...
4203                                 (setcar threads (list whole-subject
4204                                                       (car threads)))
4205                                 threads)
4206                             threads)
4207                           hashtb)))
4208         (setq prev threads)
4209         (setq threads (cdr threads)))
4210       result)))
4211
4212 (defun gnus-gather-threads-by-references (threads)
4213   "Gather threads by looking at References headers."
4214   (let ((idhashtb (gnus-make-hashtable 1024))
4215         (thhashtb (gnus-make-hashtable 1024))
4216         (prev threads)
4217         (result threads)
4218         ids references id gthread gid entered ref)
4219     (while threads
4220       (when (setq references (mail-header-references (caar threads)))
4221         (setq id (mail-header-id (caar threads))
4222               ids (inline (gnus-split-references references))
4223               entered nil)
4224         (while (setq ref (pop ids))
4225           (setq ids (delete ref ids))
4226           (if (not (setq gid (gnus-gethash ref idhashtb)))
4227               (progn
4228                 (gnus-sethash ref id idhashtb)
4229                 (gnus-sethash id threads thhashtb))
4230             (setq gthread (gnus-gethash gid thhashtb))
4231             (unless entered
4232               ;; We enter a dummy root into the thread, if we
4233               ;; haven't done that already.
4234               (unless (stringp (caar gthread))
4235                 (setcar gthread (list (mail-header-subject (caar gthread))
4236                                       (car gthread))))
4237               ;; We add this new gathered thread to this gathered
4238               ;; thread.
4239               (setcdr (car gthread)
4240                       (nconc (cdar gthread) (list (car threads)))))
4241             ;; Add it into the thread hash table.
4242             (gnus-sethash id gthread thhashtb)
4243             (setq entered t)
4244             ;; Remove it from the list of threads.
4245             (setcdr prev (cdr threads))
4246             (setq threads prev))))
4247       (setq prev threads)
4248       (setq threads (cdr threads)))
4249     result))
4250
4251 (defun gnus-sort-gathered-threads (threads)
4252   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4253   (let ((result threads))
4254     (while threads
4255       (when (stringp (caar threads))
4256         (setcdr (car threads)
4257                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4258       (setq threads (cdr threads)))
4259     result))
4260
4261 (defun gnus-thread-loop-p (root thread)
4262   "Say whether ROOT is in THREAD."
4263   (let ((stack (list thread))
4264         (infloop 0)
4265         th)
4266     (while (setq thread (pop stack))
4267       (setq th (cdr thread))
4268       (while (and th
4269                   (not (eq (caar th) root)))
4270         (pop th))
4271       (if th
4272           ;; We have found a loop.
4273           (let (ref-dep)
4274             (setcdr thread (delq (car th) (cdr thread)))
4275             (if (boundp (setq ref-dep (intern "none"
4276                                               gnus-newsgroup-dependencies)))
4277                 (setcdr (symbol-value ref-dep)
4278                         (nconc (cdr (symbol-value ref-dep))
4279                                (list (car th))))
4280               (set ref-dep (list nil (car th))))
4281             (setq infloop 1
4282                   stack nil))
4283         ;; Push all the subthreads onto the stack.
4284         (push (cdr thread) stack)))
4285     infloop))
4286
4287 (defun gnus-make-threads ()
4288   "Go through the dependency hashtb and find the roots.  Return all threads."
4289   (let (threads)
4290     (while (catch 'infloop
4291              (mapatoms
4292               (lambda (refs)
4293                 ;; Deal with self-referencing References loops.
4294                 (when (and (car (symbol-value refs))
4295                            (not (zerop
4296                                  (apply
4297                                   '+
4298                                   (mapcar
4299                                    (lambda (thread)
4300                                      (gnus-thread-loop-p
4301                                       (car (symbol-value refs)) thread))
4302                                    (cdr (symbol-value refs)))))))
4303                   (setq threads nil)
4304                   (throw 'infloop t))
4305                 (unless (car (symbol-value refs))
4306                   ;; These threads do not refer back to any other
4307                   ;; articles, so they're roots.
4308                   (setq threads (append (cdr (symbol-value refs)) threads))))
4309               gnus-newsgroup-dependencies)))
4310     threads))
4311
4312 ;; Build the thread tree.
4313 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4314   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4315
4316 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4317 if it was already present.
4318
4319 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4320 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4321 Message-IDs will be renamed to a unique Message-ID before being
4322 entered.
4323
4324 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4325   (let* ((id (mail-header-id header))
4326          (id-dep (and id (intern id dependencies)))
4327          parent-id ref ref-dep ref-header replaced)
4328     ;; Enter this `header' in the `dependencies' table.
4329     (cond
4330      ((not id-dep)
4331       (setq header nil))
4332      ;; The first two cases do the normal part: enter a new `header'
4333      ;; in the `dependencies' table.
4334      ((not (boundp id-dep))
4335       (set id-dep (list header)))
4336      ((null (car (symbol-value id-dep)))
4337       (setcar (symbol-value id-dep) header))
4338
4339      ;; From here the `header' was already present in the
4340      ;; `dependencies' table.
4341      (force-new
4342       ;; Overrides an existing entry;
4343       ;; just set the header part of the entry.
4344       (setcar (symbol-value id-dep) header)
4345       (setq replaced t))
4346
4347      ;; Renames the existing `header' to a unique Message-ID.
4348      ((not gnus-summary-ignore-duplicates)
4349       ;; An article with this Message-ID has already been seen.
4350       ;; We rename the Message-ID.
4351       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4352            (list header))
4353       (mail-header-set-id header id))
4354
4355      ;; The last case ignores an existing entry, except it adds any
4356      ;; additional Xrefs (in case the two articles came from different
4357      ;; servers.
4358      ;; Also sets `header' to `nil' meaning that the `dependencies'
4359      ;; table was *not* modified.
4360      (t
4361       (mail-header-set-xref
4362        (car (symbol-value id-dep))
4363        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4364                    "")
4365                (or (mail-header-xref header) "")))
4366       (setq header nil)))
4367
4368     (when (and header (not replaced))
4369       ;; First check that we are not creating a References loop.
4370       (setq parent-id (gnus-parent-id (mail-header-references header)))
4371       (setq ref parent-id)
4372       (while (and ref
4373                   (setq ref-dep (intern-soft ref dependencies))
4374                   (boundp ref-dep)
4375                   (setq ref-header (car (symbol-value ref-dep))))
4376         (if (string= id ref)
4377             ;; Yuk!  This is a reference loop.  Make the article be a
4378             ;; root article.
4379             (progn
4380               (mail-header-set-references (car (symbol-value id-dep)) "none")
4381               (setq ref nil)
4382               (setq parent-id nil))
4383           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4384       (setq ref-dep (intern (or parent-id "none") dependencies))
4385       (if (boundp ref-dep)
4386           (setcdr (symbol-value ref-dep)
4387                   (nconc (cdr (symbol-value ref-dep))
4388                          (list (symbol-value id-dep))))
4389         (set ref-dep (list nil (symbol-value id-dep)))))
4390     header))
4391
4392 (defun gnus-extract-message-id-from-in-reply-to (string)
4393   (if (string-match "<[^>]+>" string)
4394       (substring string (match-beginning 0) (match-end 0))
4395     nil))
4396
4397 (defun gnus-build-sparse-threads ()
4398   (let ((headers gnus-newsgroup-headers)
4399         (mail-parse-charset gnus-newsgroup-charset)
4400         (gnus-summary-ignore-duplicates t)
4401         header references generation relations
4402         subject child end new-child date)
4403     ;; First we create an alist of generations/relations, where
4404     ;; generations is how much we trust the relation, and the relation
4405     ;; is parent/child.
4406     (gnus-message 7 "Making sparse threads...")
4407     (save-excursion
4408       (nnheader-set-temp-buffer " *gnus sparse threads*")
4409       (while (setq header (pop headers))
4410         (when (and (setq references (mail-header-references header))
4411                    (not (string= references "")))
4412           (insert references)
4413           (setq child (mail-header-id header)
4414                 subject (mail-header-subject header)
4415                 date (mail-header-date header)
4416                 generation 0)
4417           (while (search-backward ">" nil t)
4418             (setq end (1+ (point)))
4419             (when (search-backward "<" nil t)
4420               (setq new-child (buffer-substring (point) end))
4421               (push (list (incf generation)
4422                           child (setq child new-child)
4423                           subject date)
4424                     relations)))
4425           (when child
4426             (push (list (1+ generation) child nil subject) relations))
4427           (erase-buffer)))
4428       (kill-buffer (current-buffer)))
4429     ;; Sort over trustworthiness.
4430     (dolist (relation (sort relations 'car-less-than-car))
4431       (when (gnus-dependencies-add-header
4432              (make-full-mail-header
4433               gnus-reffed-article-number
4434               (nth 3 relation) "" (or (nth 4 relation) "")
4435               (nth 1 relation)
4436               (or (nth 2 relation) "") 0 0 "")
4437              gnus-newsgroup-dependencies nil)
4438         (push gnus-reffed-article-number gnus-newsgroup-limit)
4439         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4440         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4441               gnus-newsgroup-reads)
4442         (decf gnus-reffed-article-number)))
4443     (gnus-message 7 "Making sparse threads...done")))
4444
4445 (defun gnus-build-old-threads ()
4446   ;; Look at all the articles that refer back to old articles, and
4447   ;; fetch the headers for the articles that aren't there.  This will
4448   ;; build complete threads - if the roots haven't been expired by the
4449   ;; server, that is.
4450   (let ((mail-parse-charset gnus-newsgroup-charset)
4451         id heads)
4452     (mapatoms
4453      (lambda (refs)
4454        (when (not (car (symbol-value refs)))
4455          (setq heads (cdr (symbol-value refs)))
4456          (while heads
4457            (if (memq (mail-header-number (caar heads))
4458                      gnus-newsgroup-dormant)
4459                (setq heads (cdr heads))
4460              (setq id (symbol-name refs))
4461              (while (and (setq id (gnus-build-get-header id))
4462                          (not (car (gnus-id-to-thread id)))))
4463              (setq heads nil)))))
4464      gnus-newsgroup-dependencies)))
4465
4466 (defsubst gnus-remove-odd-characters (string)
4467   "Translate STRING into something that doesn't contain weird characters."
4468   (mm-subst-char-in-string
4469    ?\r ?\-
4470    (mm-subst-char-in-string ?\n ?\- string t) t))
4471
4472 ;; This function has to be called with point after the article number
4473 ;; on the beginning of the line.
4474 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4475   (let ((eol (point-at-eol))
4476         (buffer (current-buffer))
4477         header references in-reply-to)
4478
4479     ;; overview: [num subject from date id refs chars lines misc]
4480     (unwind-protect
4481         (let (x)
4482           (narrow-to-region (point) eol)
4483           (unless (eobp)
4484             (forward-char))
4485
4486           (setq header
4487                 (make-full-mail-header
4488                  number                 ; number
4489                  (condition-case ()     ; subject
4490                      (gnus-remove-odd-characters
4491                       (funcall gnus-decode-encoded-word-function
4492                                (setq x (nnheader-nov-field))))
4493                    (error x))
4494                  (condition-case ()     ; from
4495                      (gnus-remove-odd-characters
4496                       (funcall gnus-decode-encoded-address-function
4497                                (setq x (nnheader-nov-field))))
4498                    (error x))
4499                  (nnheader-nov-field)   ; date
4500                  (nnheader-nov-read-message-id number)  ; id
4501                  (setq references (nnheader-nov-field)) ; refs
4502                  (nnheader-nov-read-integer) ; chars
4503                  (nnheader-nov-read-integer) ; lines
4504                  (unless (eobp)
4505                    (if (looking-at "Xref: ")
4506                        (goto-char (match-end 0)))
4507                    (nnheader-nov-field)) ; Xref
4508                  (nnheader-nov-parse-extra)))) ; extra
4509
4510       (widen))
4511
4512     (when (and (string= references "")
4513                (setq in-reply-to (mail-header-extra header))
4514                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4515       (mail-header-set-references
4516        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4517
4518     (when gnus-alter-header-function
4519       (funcall gnus-alter-header-function header))
4520     (gnus-dependencies-add-header header dependencies force-new)))
4521
4522 (defun gnus-build-get-header (id)
4523   "Look through the buffer of NOV lines and find the header to ID.
4524 Enter this line into the dependencies hash table, and return
4525 the id of the parent article (if any)."
4526   (let ((deps gnus-newsgroup-dependencies)
4527         found header)
4528     (prog1
4529         (with-current-buffer nntp-server-buffer
4530           (let ((case-fold-search nil))
4531             (goto-char (point-min))
4532             (while (and (not found)
4533                         (search-forward id nil t))
4534               (beginning-of-line)
4535               (setq found (looking-at
4536                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4537                                    (regexp-quote id))))
4538               (or found (beginning-of-line 2)))
4539             (when found
4540               (beginning-of-line)
4541               (and
4542                (setq header (gnus-nov-parse-line
4543                              (read (current-buffer)) deps))
4544                (gnus-parent-id (mail-header-references header))))))
4545       (when header
4546         (let ((number (mail-header-number header)))
4547           (push number gnus-newsgroup-limit)
4548           (push header gnus-newsgroup-headers)
4549           (if (memq number gnus-newsgroup-unselected)
4550               (progn
4551                 (setq gnus-newsgroup-unreads
4552                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4553                                                number))
4554                 (setq gnus-newsgroup-unselected
4555                       (delq number gnus-newsgroup-unselected)))
4556             (push number gnus-newsgroup-ancient)))))))
4557
4558 (defun gnus-build-all-threads ()
4559   "Read all the headers."
4560   (let ((gnus-summary-ignore-duplicates t)
4561         (mail-parse-charset gnus-newsgroup-charset)
4562         (dependencies gnus-newsgroup-dependencies)
4563         header article)
4564     (with-current-buffer nntp-server-buffer
4565       (let ((case-fold-search nil))
4566         (goto-char (point-min))
4567         (while (not (eobp))
4568           (ignore-errors
4569             (setq article (read (current-buffer))
4570                   header (gnus-nov-parse-line article dependencies t)))
4571           (when header
4572             (with-current-buffer gnus-summary-buffer
4573               (push header gnus-newsgroup-headers)
4574               (if (memq (setq article (mail-header-number header))
4575                         gnus-newsgroup-unselected)
4576                   (progn
4577                     (setq gnus-newsgroup-unreads
4578                           (gnus-add-to-sorted-list
4579                            gnus-newsgroup-unreads article))
4580                     (setq gnus-newsgroup-unselected
4581                           (delq article gnus-newsgroup-unselected)))
4582                 (push article gnus-newsgroup-ancient)))
4583             (forward-line 1)))))))
4584
4585 (defun gnus-summary-update-article-line (article header)
4586   "Update the line for ARTICLE using HEADER."
4587   (let* ((id (mail-header-id header))
4588          (thread (gnus-id-to-thread id)))
4589     (unless thread
4590       (error "Article in no thread"))
4591     ;; Update the thread.
4592     (setcar thread header)
4593     (gnus-summary-goto-subject article)
4594     (let* ((datal (gnus-data-find-list article))
4595            (data (car datal))
4596            (inhibit-read-only t)
4597            (level (gnus-summary-thread-level)))
4598       (gnus-delete-line)
4599       (let ((inserted (- (point)
4600                          (progn
4601                            (gnus-summary-insert-line
4602                             header level nil
4603                             (memq article gnus-newsgroup-undownloaded)
4604                             (gnus-article-mark article)
4605                             (memq article gnus-newsgroup-replied)
4606                             (memq article gnus-newsgroup-expirable)
4607                             ;; Only insert the Subject string when it's different
4608                             ;; from the previous Subject string.
4609                             (if (and
4610                                  gnus-show-threads
4611                                  (gnus-subject-equal
4612                                   (condition-case ()
4613                                       (mail-header-subject
4614                                        (gnus-data-header
4615                                         (cadr
4616                                          (gnus-data-find-list
4617                                           article
4618                                           (gnus-data-list t)))))
4619                                     ;; Error on the side of excessive subjects.
4620                                     (error ""))
4621                                   (mail-header-subject header)))
4622                                 ""
4623                               (mail-header-subject header))
4624                             nil (cdr (assq article gnus-newsgroup-scored))
4625                             (memq article gnus-newsgroup-processable))
4626                            (point)))))
4627         (when (cdr datal)
4628           (gnus-data-update-list
4629            (cdr datal)
4630            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4631
4632 (defun gnus-summary-update-article (article &optional iheader)
4633   "Update ARTICLE in the summary buffer."
4634   (set-buffer gnus-summary-buffer)
4635   (let* ((header (gnus-summary-article-header article))
4636          (id (mail-header-id header))
4637          (data (gnus-data-find article))
4638          (thread (gnus-id-to-thread id))
4639          (references (mail-header-references header))
4640          (parent
4641           (gnus-id-to-thread
4642            (or (gnus-parent-id
4643                 (when (and references
4644                            (not (equal "" references)))
4645                   references))
4646                "none")))
4647          (inhibit-read-only t)
4648          (old (car thread)))
4649     (when thread
4650       (unless iheader
4651         (setcar thread nil)
4652         (when parent
4653           (delq thread parent)))
4654       (if (gnus-summary-insert-subject id header)
4655           ;; Set the (possibly) new article number in the data structure.
4656           (gnus-data-set-number data (gnus-id-to-article id))
4657         (setcar thread old)
4658         nil))))
4659
4660 (defun gnus-rebuild-thread (id &optional line)
4661   "Rebuild the thread containing ID.
4662 If LINE, insert the rebuilt thread starting on line LINE."
4663   (let ((inhibit-read-only t)
4664         old-pos current thread data)
4665     (if (not gnus-show-threads)
4666         (setq thread (list (car (gnus-id-to-thread id))))
4667       ;; Get the thread this article is part of.
4668       (setq thread (gnus-remove-thread id)))
4669     (setq old-pos (point-at-bol))
4670     (setq current (save-excursion
4671                     (and (re-search-backward "[\r\n]" nil t)
4672                          (gnus-summary-article-number))))
4673     ;; If this is a gathered thread, we have to go some re-gathering.
4674     (when (stringp (car thread))
4675       (let ((subject (car thread))
4676             roots thr)
4677         (setq thread (cdr thread))
4678         (while thread
4679           (unless (memq (setq thr (gnus-id-to-thread
4680                                    (gnus-root-id
4681                                     (mail-header-id (caar thread)))))
4682                         roots)
4683             (push thr roots))
4684           (setq thread (cdr thread)))
4685         ;; We now have all (unique) roots.
4686         (if (= (length roots) 1)
4687             ;; All the loose roots are now one solid root.
4688             (setq thread (car roots))
4689           (setq thread (cons subject (gnus-sort-threads roots))))))
4690     (let (threads)
4691       ;; We then insert this thread into the summary buffer.
4692       (when line
4693         (goto-char (point-min))
4694         (forward-line (1- line)))
4695       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4696         (if gnus-show-threads
4697             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4698           (gnus-summary-prepare-unthreaded thread))
4699         (setq data (nreverse gnus-newsgroup-data))
4700         (setq threads gnus-newsgroup-threads))
4701       ;; We splice the new data into the data structure.
4702       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4703       ;;!!! then we want to insert at the beginning of the buffer.
4704       ;;!!! That happens to be true with Gnus now, but that may
4705       ;;!!! change in the future.  Perhaps.
4706       (gnus-data-enter-list
4707        (if line nil current) data (- (point) old-pos))
4708       (setq gnus-newsgroup-threads
4709             (nconc threads gnus-newsgroup-threads))
4710       (gnus-data-compute-positions))))
4711
4712 (defun gnus-number-to-header (number)
4713   "Return the header for article NUMBER."
4714   (let ((headers gnus-newsgroup-headers))
4715     (while (and headers
4716                 (not (= number (mail-header-number (car headers)))))
4717       (pop headers))
4718     (when headers
4719       (car headers))))
4720
4721 (defun gnus-parent-headers (in-headers &optional generation)
4722   "Return the headers of the GENERATIONth parent of HEADERS."
4723   (unless generation
4724     (setq generation 1))
4725   (let ((parent t)
4726         (headers in-headers)
4727         references)
4728     (while (and parent
4729                 (not (zerop generation))
4730                 (setq references (mail-header-references headers)))
4731       (setq headers (if (and references
4732                              (setq parent (gnus-parent-id references)))
4733                         (car (gnus-id-to-thread parent))
4734                       nil))
4735       (decf generation))
4736     (and (not (eq headers in-headers))
4737          headers)))
4738
4739 (defun gnus-id-to-thread (id)
4740   "Return the (sub-)thread where ID appears."
4741   (gnus-gethash id gnus-newsgroup-dependencies))
4742
4743 (defun gnus-id-to-article (id)
4744   "Return the article number of ID."
4745   (let ((thread (gnus-id-to-thread id)))
4746     (when (and thread
4747                (car thread))
4748       (mail-header-number (car thread)))))
4749
4750 (defun gnus-id-to-header (id)
4751   "Return the article headers of ID."
4752   (car (gnus-id-to-thread id)))
4753
4754 (defun gnus-article-displayed-root-p (article)
4755   "Say whether ARTICLE is a root(ish) article."
4756   (let ((level (gnus-summary-thread-level article))
4757         (refs (mail-header-references  (gnus-summary-article-header article)))
4758         particle)
4759     (cond
4760      ((null level) nil)
4761      ((zerop level) t)
4762      ((null refs) t)
4763      ((null (gnus-parent-id refs)) t)
4764      ((and (= 1 level)
4765            (null (setq particle (gnus-id-to-article
4766                                  (gnus-parent-id refs))))
4767            (null (gnus-summary-thread-level particle)))))))
4768
4769 (defun gnus-root-id (id)
4770   "Return the id of the root of the thread where ID appears."
4771   (let (last-id prev)
4772     (while (and id (setq prev (car (gnus-id-to-thread id))))
4773       (setq last-id id
4774             id (gnus-parent-id (mail-header-references prev))))
4775     last-id))
4776
4777 (defun gnus-articles-in-thread (thread)
4778   "Return the list of articles in THREAD."
4779   (cons (mail-header-number (car thread))
4780         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4781
4782 (defun gnus-remove-thread (id &optional dont-remove)
4783   "Remove the thread that has ID in it."
4784   (let (headers thread last-id)
4785     ;; First go up in this thread until we find the root.
4786     (setq last-id (gnus-root-id id)
4787           headers (message-flatten-list (gnus-id-to-thread last-id)))
4788     ;; We have now found the real root of this thread.  It might have
4789     ;; been gathered into some loose thread, so we have to search
4790     ;; through the threads to find the thread we wanted.
4791     (let ((threads gnus-newsgroup-threads)
4792           sub)
4793       (while threads
4794         (setq sub (car threads))
4795         (if (stringp (car sub))
4796             ;; This is a gathered thread, so we look at the roots
4797             ;; below it to find whether this article is in this
4798             ;; gathered root.
4799             (progn
4800               (setq sub (cdr sub))
4801               (while sub
4802                 (when (member (caar sub) headers)
4803                   (setq thread (car threads)
4804                         threads nil
4805                         sub nil))
4806                 (setq sub (cdr sub))))
4807           ;; It's an ordinary thread, so we check it.
4808           (when (eq (car sub) (car headers))
4809             (setq thread sub
4810                   threads nil)))
4811         (setq threads (cdr threads)))
4812       ;; If this article is in no thread, then it's a root.
4813       (if thread
4814           (unless dont-remove
4815             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4816         (setq thread (gnus-id-to-thread last-id)))
4817       (when thread
4818         (prog1
4819             thread                      ; We return this thread.
4820           (unless dont-remove
4821             (if (stringp (car thread))
4822                 (progn
4823                   ;; If we use dummy roots, then we have to remove the
4824                   ;; dummy root as well.
4825                   (when (eq gnus-summary-make-false-root 'dummy)
4826                     ;; We go to the dummy root by going to
4827                     ;; the first sub-"thread", and then one line up.
4828                     (gnus-summary-goto-article
4829                      (mail-header-number (caadr thread)))
4830                     (forward-line -1)
4831                     (gnus-delete-line)
4832                     (gnus-data-compute-positions))
4833                   (setq thread (cdr thread))
4834                   (while thread
4835                     (gnus-remove-thread-1 (car thread))
4836                     (setq thread (cdr thread))))
4837               (gnus-remove-thread-1 thread))))))))
4838
4839 (defun gnus-remove-thread-1 (thread)
4840   "Remove the thread THREAD recursively."
4841   (let ((number (mail-header-number (pop thread)))
4842         d)
4843     (setq thread (reverse thread))
4844     (while thread
4845       (gnus-remove-thread-1 (pop thread)))
4846     (when (setq d (gnus-data-find number))
4847       (goto-char (gnus-data-pos d))
4848       (gnus-summary-show-thread)
4849       (gnus-data-remove
4850        number
4851        (- (point-at-bol)
4852           (prog1
4853               (1+ (point-at-eol))
4854             (gnus-delete-line)))))))
4855
4856 (defun gnus-sort-threads-recursive (threads func)
4857   ;; Responsible for sorting the root articles of threads.
4858   (let ((subthread-sort-func (if gnus-sort-threads-recursively
4859                                  func
4860                                #'gnus-thread-sort-by-date)))
4861     (sort (mapcar (lambda (thread)
4862                     (cons (car thread)
4863                           (and (cdr thread)
4864                                (gnus-sort-subthreads-recursive
4865                                 (cdr thread) subthread-sort-func))))
4866                   threads) func)))
4867
4868 (defun gnus-sort-subthreads-recursive (threads func)
4869   ;; Responsible for sorting subthreads.
4870   (sort (mapcar (lambda (thread)
4871                   (cons (car thread)
4872                         (and (cdr thread)
4873                              (gnus-sort-subthreads-recursive (cdr thread) func))))
4874                 threads) func))
4875
4876 (defun gnus-sort-threads-loop (threads func)
4877   (let* ((superthread (cons nil threads))
4878          (stack (list (cons superthread threads)))
4879          remaining-threads thread)
4880     (while stack
4881       (setq remaining-threads (cdr (car stack)))
4882       (if remaining-threads
4883           (progn (setq thread (car remaining-threads))
4884                  (setcdr (car stack) (cdr remaining-threads))
4885                  (if (cdr thread)
4886                      (push (cons thread (cdr thread)) stack)))
4887         (setq thread (caar stack))
4888         (setcdr thread (sort (cdr thread) func))
4889         (pop stack)))
4890     (cdr superthread)))
4891
4892 (defun gnus-sort-threads (threads)
4893   "Sort THREADS."
4894   (if (not gnus-thread-sort-functions)
4895       threads
4896     (gnus-message 8 "Sorting threads...")
4897     (prog1
4898         (condition-case nil
4899             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000))
4900                   (sort-func (gnus-make-sort-function gnus-thread-sort-functions)))
4901               (gnus-sort-threads-recursive threads sort-func))
4902           ;; Even after binding max-lisp-eval-depth, the recursive
4903           ;; sorter might fail for very long threads.  In that case,
4904           ;; try using a (less well-tested) non-recursive sorter.
4905           (error (gnus-message 9 "Sorting threads with loop...")
4906                  (gnus-sort-threads-loop
4907                   threads (gnus-make-sort-function
4908                            gnus-thread-sort-functions))))
4909       (gnus-message 8 "Sorting threads...done"))))
4910
4911 (defun gnus-sort-articles (articles)
4912   "Sort ARTICLES."
4913   (when gnus-article-sort-functions
4914     (gnus-message 7 "Sorting articles...")
4915     (prog1
4916         (setq gnus-newsgroup-headers
4917               (sort articles (gnus-make-sort-function
4918                               gnus-article-sort-functions)))
4919       (gnus-message 7 "Sorting articles...done"))))
4920
4921 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4922 (defmacro gnus-thread-header (thread)
4923   "Return header of first article in THREAD.
4924 Note that THREAD must never, ever be anything else than a variable -
4925 using some other form will lead to serious barfage."
4926   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4927   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4928   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4929         (vector thread) 2))
4930
4931 (defsubst gnus-article-sort-by-number (h1 h2)
4932   "Sort articles by article number."
4933   (< (mail-header-number h1)
4934      (mail-header-number h2)))
4935
4936 (defun gnus-thread-sort-by-number (h1 h2)
4937   "Sort threads by root article number."
4938   (gnus-article-sort-by-number
4939    (gnus-thread-header h1) (gnus-thread-header h2)))
4940
4941 (defsubst gnus-article-sort-by-random (h1 h2)
4942   "Sort articles randomly."
4943   (zerop (random 2)))
4944
4945 (defun gnus-thread-sort-by-random (h1 h2)
4946   "Sort threads randomly."
4947   (gnus-article-sort-by-random
4948    (gnus-thread-header h1) (gnus-thread-header h2)))
4949
4950 (defsubst gnus-article-sort-by-lines (h1 h2)
4951   "Sort articles by article Lines header."
4952   (< (mail-header-lines h1)
4953      (mail-header-lines h2)))
4954
4955 (defun gnus-thread-sort-by-lines (h1 h2)
4956   "Sort threads by root article Lines header."
4957   (gnus-article-sort-by-lines
4958    (gnus-thread-header h1) (gnus-thread-header h2)))
4959
4960 (defsubst gnus-article-sort-by-chars (h1 h2)
4961   "Sort articles by octet length."
4962   (< (mail-header-chars h1)
4963      (mail-header-chars h2)))
4964
4965 (defun gnus-thread-sort-by-chars (h1 h2)
4966   "Sort threads by root article octet length."
4967   (gnus-article-sort-by-chars
4968    (gnus-thread-header h1) (gnus-thread-header h2)))
4969
4970 (defsubst gnus-article-sort-by-author (h1 h2)
4971   "Sort articles by root author."
4972   (gnus-string<
4973    (let ((extract (funcall
4974                    gnus-extract-address-components
4975                    (mail-header-from h1))))
4976      (or (car extract) (cadr extract) ""))
4977    (let ((extract (funcall
4978                    gnus-extract-address-components
4979                    (mail-header-from h2))))
4980      (or (car extract) (cadr extract) ""))))
4981
4982 (defun gnus-thread-sort-by-author (h1 h2)
4983   "Sort threads by root author."
4984   (gnus-article-sort-by-author
4985    (gnus-thread-header h1)  (gnus-thread-header h2)))
4986
4987 (defsubst gnus-article-sort-by-recipient (h1 h2)
4988   "Sort articles by recipient."
4989   (gnus-string<
4990    (let ((extract (funcall
4991                    gnus-extract-address-components
4992                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4993      (or (car extract) (cadr extract)))
4994    (let ((extract (funcall
4995                    gnus-extract-address-components
4996                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4997      (or (car extract) (cadr extract)))))
4998
4999 (defun gnus-thread-sort-by-recipient (h1 h2)
5000   "Sort threads by root recipient."
5001   (gnus-article-sort-by-recipient
5002    (gnus-thread-header h1) (gnus-thread-header h2)))
5003
5004 (defsubst gnus-article-sort-by-subject (h1 h2)
5005   "Sort articles by root subject."
5006   (gnus-string<
5007    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5008    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5009
5010 (defun gnus-thread-sort-by-subject (h1 h2)
5011   "Sort threads by root subject."
5012   (gnus-article-sort-by-subject
5013    (gnus-thread-header h1) (gnus-thread-header h2)))
5014
5015 (defsubst gnus-article-sort-by-date (h1 h2)
5016   "Sort articles by root article date."
5017   (time-less-p
5018    (gnus-date-get-time (mail-header-date h1))
5019    (gnus-date-get-time (mail-header-date h2))))
5020
5021 (defun gnus-thread-sort-by-date (h1 h2)
5022   "Sort threads by root article date."
5023   (gnus-article-sort-by-date
5024    (gnus-thread-header h1) (gnus-thread-header h2)))
5025
5026 (defsubst gnus-article-sort-by-score (h1 h2)
5027   "Sort articles by root article score.
5028 Unscored articles will be counted as having a score of zero."
5029   (> (or (cdr (assq (mail-header-number h1)
5030                     gnus-newsgroup-scored))
5031          gnus-summary-default-score 0)
5032      (or (cdr (assq (mail-header-number h2)
5033                     gnus-newsgroup-scored))
5034          gnus-summary-default-score 0)))
5035
5036 (defun gnus-thread-sort-by-score (h1 h2)
5037   "Sort threads by root article score."
5038   (gnus-article-sort-by-score
5039    (gnus-thread-header h1) (gnus-thread-header h2)))
5040
5041 (defun gnus-thread-sort-by-total-score (h1 h2)
5042   "Sort threads by the sum of all scores in the thread.
5043 Unscored articles will be counted as having a score of zero."
5044   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5045
5046 (defun gnus-thread-total-score (thread)
5047   ;; This function find the total score of THREAD.
5048   (cond
5049    ((null thread)
5050     0)
5051    ((consp thread)
5052     (if (stringp (car thread))
5053         (apply gnus-thread-score-function 0
5054                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5055       (gnus-thread-total-score-1 thread)))
5056    (t
5057     (gnus-thread-total-score-1 (list thread)))))
5058
5059 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5060   "Sort articles by number."
5061   (gnus-article-sort-by-number h1 h2))
5062
5063 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5064   "Sort threads such that the thread with the most recently arrived article comes first."
5065   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5066
5067 (defun gnus-thread-highest-number (thread)
5068   "Return the highest article number in THREAD."
5069   (apply 'max (mapcar (lambda (header)
5070                         (mail-header-number header))
5071                       (message-flatten-list thread))))
5072
5073 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5074   "Sort articles by number."
5075   (gnus-article-sort-by-date h1 h2))
5076
5077 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5078   "Sort threads such that the thread with the most recently dated article comes first."
5079   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5080
5081 ; Since this is called not only to sort the top-level threads, but
5082 ; also in recursive sorts to order the articles within a thread, each
5083 ; article will be processed many times.  Thus it speeds things up
5084 ; quite a bit to use gnus-date-get-time, which caches the time value.
5085 (defun gnus-thread-latest-date (thread)
5086   "Return the highest article date in THREAD."
5087   (apply 'max
5088          (mapcar (lambda (header) (gnus-float-time
5089                                    (gnus-date-get-time
5090                                     (mail-header-date header))))
5091                  (message-flatten-list thread))))
5092
5093 (defun gnus-thread-total-score-1 (root)
5094   ;; This function find the total score of the thread below ROOT.
5095   (setq root (car root))
5096   (apply gnus-thread-score-function
5097          (or (append
5098               (mapcar 'gnus-thread-total-score
5099                       (cdr (gnus-id-to-thread (mail-header-id root))))
5100               (when (> (mail-header-number root) 0)
5101                 (list (or (cdr (assq (mail-header-number root)
5102                                      gnus-newsgroup-scored))
5103                           gnus-summary-default-score 0))))
5104              (list gnus-summary-default-score)
5105              '(0))))
5106
5107 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5108 (defvar gnus-tmp-prev-subject nil)
5109 (defvar gnus-tmp-false-parent nil)
5110 (defvar gnus-tmp-root-expunged nil)
5111 (defvar gnus-tmp-dummy-line nil)
5112
5113 (defun gnus-extra-header (type &optional header)
5114   "Return the extra header of TYPE."
5115   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5116       ""))
5117
5118 (defvar gnus-tmp-thread-tree-header-string "")
5119
5120 (defcustom gnus-sum-thread-tree-root "> "
5121   "With %B spec, used for the root of a thread.
5122 If nil, use subject instead."
5123   :version "22.1"
5124   :type '(radio (const :format "%v  " nil) string)
5125   :group 'gnus-thread)
5126
5127 (defcustom gnus-sum-thread-tree-false-root "> "
5128   "With %B spec, used for a false root of a thread.
5129 If nil, use subject instead."
5130   :version "22.1"
5131   :type '(radio (const :format "%v  " nil) string)
5132   :group 'gnus-thread)
5133
5134 (defcustom gnus-sum-thread-tree-single-indent ""
5135   "With %B spec, used for a thread with just one message.
5136 If nil, use subject instead."
5137   :version "22.1"
5138   :type '(radio (const :format "%v  " nil) string)
5139   :group 'gnus-thread)
5140
5141 (defcustom gnus-sum-thread-tree-vertical "| "
5142   "With %B spec, used for drawing a vertical line."
5143   :version "22.1"
5144   :type 'string
5145   :group 'gnus-thread)
5146
5147 (defcustom gnus-sum-thread-tree-indent "  "
5148   "With %B spec, used for indenting."
5149   :version "22.1"
5150   :type 'string
5151   :group 'gnus-thread)
5152
5153 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5154   "With %B spec, used for a leaf with brothers."
5155   :version "22.1"
5156   :type 'string
5157   :group 'gnus-thread)
5158
5159 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5160   "With %B spec, used for a leaf without brothers."
5161   :version "22.1"
5162   :type 'string
5163   :group 'gnus-thread)
5164
5165 (defcustom gnus-summary-display-while-building nil
5166   "If non-nil, show and update the summary buffer as it's being built.
5167 If the value is t, update the buffer after every line is inserted.  If
5168 the value is an integer (N), update the display every N lines."
5169   :version "22.1"
5170   :group 'gnus-thread
5171   :type '(choice (const :tag "off" nil)
5172                  number
5173                  (const :tag "frequently" t)))
5174
5175 (defun gnus-summary-prepare-threads (threads)
5176   "Prepare summary buffer from THREADS and indentation LEVEL.
5177 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5178 or a straight list of headers."
5179   (gnus-message 7 "Generating summary...")
5180
5181   (setq gnus-newsgroup-threads threads)
5182   (beginning-of-line)
5183
5184   (let ((gnus-tmp-level 0)
5185         (default-score (or gnus-summary-default-score 0))
5186         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5187         (building-line-count gnus-summary-display-while-building)
5188         (building-count (integerp gnus-summary-display-while-building))
5189         thread number subject stack state gnus-tmp-gathered beg-match
5190         new-roots gnus-tmp-new-adopts thread-end simp-subject
5191         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5192         gnus-tmp-replied gnus-tmp-subject-or-nil
5193         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5194         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5195         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5196         tree-stack)
5197
5198     (setq gnus-tmp-prev-subject nil
5199           gnus-tmp-thread-tree-header-string "")
5200
5201     (if (vectorp (car threads))
5202         ;; If this is a straight (sic) list of headers, then a
5203         ;; threaded summary display isn't required, so we just create
5204         ;; an unthreaded one.
5205         (gnus-summary-prepare-unthreaded threads)
5206
5207       ;; Do the threaded display.
5208
5209       (if gnus-summary-display-while-building
5210           (switch-to-buffer (buffer-name)))
5211       (while (or threads stack gnus-tmp-new-adopts new-roots)
5212
5213         (if (and (= gnus-tmp-level 0)
5214                  (or (not stack)
5215                      (= (caar stack) 0))
5216                  (not gnus-tmp-false-parent)
5217                  (or gnus-tmp-new-adopts new-roots))
5218             (if gnus-tmp-new-adopts
5219                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5220                       thread (list (car gnus-tmp-new-adopts))
5221                       gnus-tmp-header (caar thread)
5222                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5223               (when new-roots
5224                 (setq thread (list (car new-roots))
5225                       gnus-tmp-header (caar thread)
5226                       new-roots (cdr new-roots))))
5227
5228           (if threads
5229               ;; If there are some threads, we do them before the
5230               ;; threads on the stack.
5231               (setq thread threads
5232                     gnus-tmp-header (caar thread))
5233             ;; There were no current threads, so we pop something off
5234             ;; the stack.
5235             (setq state (car stack)
5236                   gnus-tmp-level (car state)
5237                   tree-stack (cadr state)
5238                   thread (caddr state)
5239                   stack (cdr stack)
5240                   gnus-tmp-header (caar thread))))
5241
5242         (setq gnus-tmp-false-parent nil)
5243         (setq gnus-tmp-root-expunged nil)
5244         (setq thread-end nil)
5245
5246         (if (stringp gnus-tmp-header)
5247             ;; The header is a dummy root.
5248             (cond
5249              ((eq gnus-summary-make-false-root 'adopt)
5250               ;; We let the first article adopt the rest.
5251               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5252                                                (cddar thread)))
5253               (setq gnus-tmp-gathered
5254                     (nconc (mapcar
5255                             (lambda (h) (mail-header-number (car h)))
5256                             (cddar thread))
5257                            gnus-tmp-gathered))
5258               (setq thread (cons (list (caar thread)
5259                                        (cadar thread))
5260                                  (cdr thread)))
5261               (setq gnus-tmp-level -1
5262                     gnus-tmp-false-parent t))
5263              ((eq gnus-summary-make-false-root 'empty)
5264               ;; We print adopted articles with empty subject fields.
5265               (setq gnus-tmp-gathered
5266                     (nconc (mapcar
5267                             (lambda (h) (mail-header-number (car h)))
5268                             (cddar thread))
5269                            gnus-tmp-gathered))
5270               (setq gnus-tmp-level -1))
5271              ((eq gnus-summary-make-false-root 'dummy)
5272               ;; We remember that we probably want to output a dummy
5273               ;; root.
5274               (setq gnus-tmp-dummy-line gnus-tmp-header)
5275               (setq gnus-tmp-prev-subject gnus-tmp-header))
5276              (t
5277               ;; We do not make a root for the gathered
5278               ;; sub-threads at all.
5279               (setq gnus-tmp-level -1)))
5280
5281           (setq number (mail-header-number gnus-tmp-header)
5282                 subject (mail-header-subject gnus-tmp-header)
5283                 simp-subject (gnus-simplify-subject-fully subject))
5284
5285           (cond
5286            ;; If the thread has changed subject, we might want to make
5287            ;; this subthread into a root.
5288            ((and (null gnus-thread-ignore-subject)
5289                  (not (zerop gnus-tmp-level))
5290                  gnus-tmp-prev-subject
5291                  (not (string= gnus-tmp-prev-subject simp-subject)))
5292             (setq new-roots (nconc new-roots (list (car thread)))
5293                   thread-end t
5294                   gnus-tmp-header nil))
5295            ;; If the article lies outside the current limit,
5296            ;; then we do not display it.
5297            ((not (memq number gnus-newsgroup-limit))
5298             (setq gnus-tmp-gathered
5299                   (nconc (mapcar
5300                           (lambda (h) (mail-header-number (car h)))
5301                           (cdar thread))
5302                          gnus-tmp-gathered))
5303             (setq gnus-tmp-new-adopts (if (cdar thread)
5304                                           (append gnus-tmp-new-adopts
5305                                                   (cdar thread))
5306                                         gnus-tmp-new-adopts)
5307                   thread-end t
5308                   gnus-tmp-header nil)
5309             (when (zerop gnus-tmp-level)
5310               (setq gnus-tmp-root-expunged t)))
5311            ;; Perhaps this article is to be marked as read?
5312            ((and gnus-summary-mark-below
5313                  (< (or (cdr (assq number gnus-newsgroup-scored))
5314                         default-score)
5315                     gnus-summary-mark-below)
5316                  ;; Don't touch sparse articles.
5317                  (not (gnus-summary-article-sparse-p number))
5318                  (not (gnus-summary-article-ancient-p number)))
5319             (setq gnus-newsgroup-unreads
5320                   (delq number gnus-newsgroup-unreads))
5321             (if gnus-newsgroup-auto-expire
5322                 (setq gnus-newsgroup-expirable
5323                       (gnus-add-to-sorted-list
5324                        gnus-newsgroup-expirable number))
5325               (push (cons number gnus-low-score-mark)
5326                     gnus-newsgroup-reads))))
5327
5328           (when gnus-tmp-header
5329             ;; We may have an old dummy line to output before this
5330             ;; article.
5331             (when (and gnus-tmp-dummy-line
5332                        (gnus-subject-equal
5333                         gnus-tmp-dummy-line
5334                         (mail-header-subject gnus-tmp-header)))
5335               (gnus-summary-insert-dummy-line
5336                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5337               (setq gnus-tmp-dummy-line nil))
5338
5339             ;; Compute the mark.
5340             (setq gnus-tmp-unread (gnus-article-mark number))
5341
5342             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5343                                   gnus-tmp-header gnus-tmp-level)
5344                   gnus-newsgroup-data)
5345
5346             ;; Actually insert the line.
5347             (setq
5348              gnus-tmp-subject-or-nil
5349              (cond
5350               ((and gnus-thread-ignore-subject
5351                     gnus-tmp-prev-subject
5352                     (not (string= gnus-tmp-prev-subject simp-subject)))
5353                subject)
5354               ((zerop gnus-tmp-level)
5355                (if (and (eq gnus-summary-make-false-root 'empty)
5356                         (memq number gnus-tmp-gathered)
5357                         gnus-tmp-prev-subject
5358                         (string= gnus-tmp-prev-subject simp-subject))
5359                    gnus-summary-same-subject
5360                  subject))
5361               (t gnus-summary-same-subject)))
5362             (if (and (eq gnus-summary-make-false-root 'adopt)
5363                      (= gnus-tmp-level 1)
5364                      (memq number gnus-tmp-gathered))
5365                 (setq gnus-tmp-opening-bracket ?\<
5366                       gnus-tmp-closing-bracket ?\>)
5367               (setq gnus-tmp-opening-bracket ?\[
5368                     gnus-tmp-closing-bracket ?\]))
5369             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5370                 (gnus-make-thread-indent-array
5371                  (max (* 2 (length gnus-thread-indent-array))
5372                       gnus-tmp-level)))
5373             (setq
5374              gnus-tmp-indentation
5375              (aref gnus-thread-indent-array gnus-tmp-level)
5376              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5377              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5378                                 gnus-summary-default-score 0)
5379              gnus-tmp-score-char
5380              (if (or (null gnus-summary-default-score)
5381                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5382                          gnus-summary-zcore-fuzz))
5383                  ?                      ;Whitespace
5384                (if (< gnus-tmp-score gnus-summary-default-score)
5385                    gnus-score-below-mark gnus-score-over-mark))
5386              gnus-tmp-replied
5387              (cond ((memq number gnus-newsgroup-processable)
5388                     gnus-process-mark)
5389                    ((memq number gnus-newsgroup-cached)
5390                     gnus-cached-mark)
5391                    ((memq number gnus-newsgroup-replied)
5392                     gnus-replied-mark)
5393                    ((memq number gnus-newsgroup-forwarded)
5394                     gnus-forwarded-mark)
5395                    ((memq number gnus-newsgroup-saved)
5396                     gnus-saved-mark)
5397                    ((memq number gnus-newsgroup-unseen)
5398                     gnus-unseen-mark)
5399                    (t gnus-no-mark))
5400              gnus-tmp-downloaded
5401              (cond ((memq number gnus-newsgroup-undownloaded)
5402                     gnus-undownloaded-mark)
5403                    (gnus-newsgroup-agentized
5404                     gnus-downloaded-mark)
5405                    (t
5406                     gnus-no-mark))
5407              gnus-tmp-from (mail-header-from gnus-tmp-header)
5408              gnus-tmp-name
5409              (cond
5410               ((string-match "<[^>]+> *$" gnus-tmp-from)
5411                (setq beg-match (match-beginning 0))
5412                (or (and (string-match "^\".+\"" gnus-tmp-from)
5413                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5414                    (substring gnus-tmp-from 0 beg-match)))
5415               ((string-match "(.+)" gnus-tmp-from)
5416                (substring gnus-tmp-from
5417                           (1+ (match-beginning 0)) (1- (match-end 0))))
5418               (t gnus-tmp-from))
5419
5420              ;; Do the %B string
5421              gnus-tmp-thread-tree-header-string
5422              (cond
5423               ((not gnus-show-threads) "")
5424               ((zerop gnus-tmp-level)
5425                (cond ((cdar thread)
5426                       (or gnus-sum-thread-tree-root subject))
5427                      (gnus-tmp-new-adopts
5428                       (or gnus-sum-thread-tree-false-root subject))
5429                      (t
5430                       (or gnus-sum-thread-tree-single-indent subject))))
5431               (t
5432                (concat (apply 'concat
5433                               (mapcar (lambda (item)
5434                                         (if (= item 1)
5435                                             gnus-sum-thread-tree-vertical
5436                                           gnus-sum-thread-tree-indent))
5437                                       (cdr (reverse tree-stack))))
5438                        (if (nth 1 thread)
5439                            gnus-sum-thread-tree-leaf-with-other
5440                          gnus-sum-thread-tree-single-leaf)))))
5441             (when (string= gnus-tmp-name "")
5442               (setq gnus-tmp-name gnus-tmp-from))
5443             (unless (numberp gnus-tmp-lines)
5444               (setq gnus-tmp-lines -1))
5445             (if (= gnus-tmp-lines -1)
5446                 (setq gnus-tmp-lines "?")
5447               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5448             (gnus-put-text-property
5449              (point)
5450              (progn (eval gnus-summary-line-format-spec) (point))
5451              'gnus-number number)
5452             (when gnus-visual-p
5453               (forward-line -1)
5454               (gnus-summary-highlight-line)
5455               (when gnus-summary-update-hook
5456                 (gnus-run-hooks 'gnus-summary-update-hook))
5457               (forward-line 1))
5458
5459             (setq gnus-tmp-prev-subject simp-subject)))
5460
5461         (when (nth 1 thread)
5462           (push (list (max 0 gnus-tmp-level)
5463                       (copy-sequence tree-stack)
5464                       (nthcdr 1 thread))
5465                 stack))
5466         (push (if (nth 1 thread) 1 0) tree-stack)
5467         (incf gnus-tmp-level)
5468         (setq threads (if thread-end nil (cdar thread)))
5469         (if gnus-summary-display-while-building
5470             (if building-count
5471                 (progn
5472                   ;; use a set frequency
5473                   (setq building-line-count (1- building-line-count))
5474                   (when (= building-line-count 0)
5475                     (sit-for 0)
5476                     (setq building-line-count
5477                           gnus-summary-display-while-building)))
5478               ;; always
5479               (sit-for 0)))
5480         (unless threads
5481           (setq gnus-tmp-level 0)))))
5482   (gnus-message 7 "Generating summary...done"))
5483
5484 (defun gnus-summary-prepare-unthreaded (headers)
5485   "Generate an unthreaded summary buffer based on HEADERS."
5486   (let (header number mark)
5487
5488     (beginning-of-line)
5489
5490     (while headers
5491       ;; We may have to root out some bad articles...
5492       (when (memq (setq number (mail-header-number
5493                                 (setq header (pop headers))))
5494                   gnus-newsgroup-limit)
5495         ;; Mark article as read when it has a low score.
5496         (when (and gnus-summary-mark-below
5497                    (< (or (cdr (assq number gnus-newsgroup-scored))
5498                           gnus-summary-default-score 0)
5499                       gnus-summary-mark-below)
5500                    (not (gnus-summary-article-ancient-p number)))
5501           (setq gnus-newsgroup-unreads
5502                 (delq number gnus-newsgroup-unreads))
5503           (if gnus-newsgroup-auto-expire
5504               (push number gnus-newsgroup-expirable)
5505             (push (cons number gnus-low-score-mark)
5506                   gnus-newsgroup-reads)))
5507
5508         (setq mark (gnus-article-mark number))
5509         (push (gnus-data-make number mark (1+ (point)) header 0)
5510               gnus-newsgroup-data)
5511         (gnus-summary-insert-line
5512          header 0 number
5513          (memq number gnus-newsgroup-undownloaded)
5514          mark (memq number gnus-newsgroup-replied)
5515          (memq number gnus-newsgroup-expirable)
5516          (mail-header-subject header) nil
5517          (cdr (assq number gnus-newsgroup-scored))
5518          (memq number gnus-newsgroup-processable))))))
5519
5520 (declare-function gnus-parameter-list-identifier "gnus-art" (name) t)
5521
5522 (defun gnus-group-get-list-identifiers (group)
5523   "Get list identifier regexp for GROUP."
5524   (or (gnus-parameter-list-identifier group)
5525       (if (consp gnus-list-identifiers)
5526           (mapconcat 'identity gnus-list-identifiers " *\\|")
5527         gnus-list-identifiers)))
5528
5529 (defun gnus-summary-remove-list-identifiers ()
5530   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5531   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5532         changed subject)
5533     (when regexp
5534       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5535       (dolist (header gnus-newsgroup-headers)
5536         (setq subject (mail-header-subject header)
5537               changed nil)
5538         (while (string-match regexp subject)
5539           (setq subject
5540                 (concat (substring subject 0 (match-beginning 1))
5541                         (substring subject (match-end 0)))
5542                 changed t))
5543         (when changed
5544           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5545             (setq subject
5546                   (concat (substring subject 0 (match-beginning 1))
5547                           (substring subject (match-end 1)))))
5548           (mail-header-set-subject header subject))))))
5549
5550 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5551   "Fetch headers of ARTICLES."
5552   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5553     (gnus-message 7 "Fetching headers for %s..." name)
5554     (prog1
5555         (if (eq 'nov
5556                 (setq gnus-headers-retrieved-by
5557                       (gnus-retrieve-headers
5558                        articles gnus-newsgroup-name
5559                        (or limit
5560                            ;; We might want to fetch old headers, but
5561                            ;; not if there is only 1 article.
5562                            (and (or (and
5563                                      (not (eq gnus-fetch-old-headers 'some))
5564                                      (not (numberp gnus-fetch-old-headers)))
5565                                     (> (length articles) 1))
5566                                 gnus-fetch-old-headers)))))
5567             (gnus-get-newsgroup-headers-xover
5568              articles force-new dependencies gnus-newsgroup-name t)
5569           (gnus-get-newsgroup-headers dependencies force-new))
5570       (gnus-message 7 "Fetching headers for %s...done" name))))
5571
5572 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5573   "Select newsgroup GROUP.
5574 If READ-ALL is non-nil, all articles in the group are selected.
5575 If SELECT-ARTICLES, only select those articles from GROUP."
5576   (let* ((entry (gnus-group-entry group))
5577          ;;!!! Dirty hack; should be removed.
5578          (gnus-summary-ignore-duplicates
5579           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5580               t
5581             gnus-summary-ignore-duplicates))
5582          (info (nth 2 entry))
5583          charset articles fetched-articles cached)
5584
5585     (unless (gnus-check-server
5586              (set (make-local-variable 'gnus-current-select-method)
5587                   (gnus-find-method-for-group group)))
5588       (error "Couldn't open server"))
5589     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5590
5591     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5592         (gnus-activate-group group)     ; Or we can activate it...
5593         (progn                          ; Or we bug out.
5594           (when (equal major-mode 'gnus-summary-mode)
5595             (gnus-kill-buffer (current-buffer)))
5596           (error
5597            "Couldn't activate group %s: %s"
5598            (mm-decode-coding-string group charset)
5599            (mm-decode-coding-string (gnus-status-message group) charset))))
5600
5601     (unless (gnus-request-group group t)
5602       (when (equal major-mode 'gnus-summary-mode)
5603         (gnus-kill-buffer (current-buffer)))
5604       (error "Couldn't request group %s: %s"
5605              (mm-decode-coding-string group charset)
5606              (mm-decode-coding-string (gnus-status-message group) charset)))
5607
5608     (when (and gnus-agent
5609                (gnus-active group))
5610       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5611
5612       (setq gnus-summary-use-undownloaded-faces
5613             (gnus-agent-find-parameter
5614              group
5615              'agent-enable-undownloaded-faces)))
5616
5617     (setq gnus-newsgroup-name group
5618           gnus-newsgroup-unselected nil
5619           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5620
5621     (let ((display (gnus-group-find-parameter group 'display)))
5622       (setq gnus-newsgroup-display
5623             (cond
5624              ((not (zerop (or (car-safe read-all) 0)))
5625               ;; The user entered the group with C-u SPC/RET, let's show
5626               ;; all articles.
5627               'gnus-not-ignore)
5628              ((eq display 'all)
5629               'gnus-not-ignore)
5630              ((arrayp display)
5631               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5632              ((numberp display)
5633               ;; The following is probably the "correct" solution, but
5634               ;; it makes Gnus fetch all headers and then limit the
5635               ;; articles (which is slow), so instead we hack the
5636               ;; select-articles parameter instead. -- Simon Josefsson
5637               ;; <jas@kth.se>
5638               ;;
5639               ;; (gnus-byte-compile
5640               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5641               ;;                         display)))))
5642               (setq select-articles
5643                     (gnus-uncompress-range
5644                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5645                              (if (> tmp 0)
5646                                  tmp
5647                                1))
5648                            (cdr (gnus-active group)))))
5649               nil)
5650              (t
5651               nil))))
5652
5653     (gnus-summary-setup-default-charset)
5654
5655     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5656     (when (gnus-virtual-group-p group)
5657       (setq cached gnus-newsgroup-cached))
5658
5659     (setq gnus-newsgroup-unreads
5660           (gnus-sorted-ndifference
5661            (gnus-sorted-ndifference gnus-newsgroup-unreads
5662                                     gnus-newsgroup-marked)
5663            gnus-newsgroup-dormant))
5664
5665     (setq gnus-newsgroup-processable nil)
5666
5667     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5668
5669     ;; Adjust and set lists of article marks.
5670     (when info
5671       (gnus-adjust-marked-articles info))
5672     (if (setq articles select-articles)
5673         (setq gnus-newsgroup-unselected
5674               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5675       (setq articles (gnus-articles-to-read group read-all)))
5676
5677     (cond
5678      ((null articles)
5679       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5680       'quit)
5681      ((eq articles 0) nil)
5682      (t
5683       ;; Init the dependencies hash table.
5684       (setq gnus-newsgroup-dependencies
5685             (gnus-make-hashtable (length articles)))
5686       (if (gnus-buffer-live-p gnus-group-buffer)
5687           (gnus-set-global-variables)
5688         (set-default 'gnus-newsgroup-name gnus-newsgroup-name))
5689       ;; Retrieve the headers and read them in.
5690
5691       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5692
5693       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5694       (when cached
5695         (setq gnus-newsgroup-cached cached))
5696
5697       ;; Suppress duplicates?
5698       (when gnus-suppress-duplicates
5699         (gnus-dup-suppress-articles))
5700
5701       ;; Set the initial limit.
5702       (setq gnus-newsgroup-limit (copy-sequence articles))
5703       ;; Remove canceled articles from the list of unread articles.
5704       (setq fetched-articles
5705             (mapcar (lambda (headers) (mail-header-number headers))
5706                     gnus-newsgroup-headers))
5707       (setq gnus-newsgroup-articles fetched-articles)
5708       (setq gnus-newsgroup-unreads
5709             (gnus-sorted-nintersection
5710              gnus-newsgroup-unreads fetched-articles))
5711       (gnus-compute-unseen-list)
5712
5713       ;; Removed marked articles that do not exist.
5714       (gnus-update-missing-marks
5715        (gnus-sorted-difference articles fetched-articles))
5716       ;; We might want to build some more threads first.
5717       (when (and gnus-fetch-old-headers
5718                  (eq gnus-headers-retrieved-by 'nov))
5719         (if (eq gnus-fetch-old-headers 'invisible)
5720             (gnus-build-all-threads)
5721           (gnus-build-old-threads)))
5722       ;; Let the Gnus agent mark articles as read.
5723       (when gnus-agent
5724         (gnus-agent-get-undownloaded-list))
5725       ;; Remove list identifiers from subject
5726       (gnus-summary-remove-list-identifiers)
5727       ;; Check whether auto-expire is to be done in this group.
5728       (setq gnus-newsgroup-auto-expire
5729             (and (gnus-group-auto-expirable-p group)
5730                  (not (gnus-group-read-only-p group))))
5731       ;; Set up the article buffer now, if necessary.
5732       (unless (and gnus-single-article-buffer
5733                    (equal gnus-article-buffer "*Article*"))
5734         (gnus-article-setup-buffer))
5735       ;; First and last article in this newsgroup.
5736       (when gnus-newsgroup-headers
5737         (setq gnus-newsgroup-begin
5738               (mail-header-number (car gnus-newsgroup-headers))
5739               gnus-newsgroup-end
5740               (mail-header-number
5741                (gnus-last-element gnus-newsgroup-headers))))
5742       ;; GROUP is successfully selected.
5743       (or gnus-newsgroup-headers t)))))
5744
5745 (defun gnus-compute-unseen-list ()
5746   ;; The `seen' marks are treated specially.
5747   (if (not gnus-newsgroup-seen)
5748       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5749     (setq gnus-newsgroup-unseen
5750           (gnus-inverse-list-range-intersection
5751            gnus-newsgroup-articles gnus-newsgroup-seen))))
5752
5753 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5754
5755 (defun gnus-summary-display-make-predicate (display)
5756   (require 'gnus-agent)
5757   (when (= (length display) 1)
5758     (setq display (car display)))
5759   (unless gnus-summary-display-cache
5760     (dolist (elem (append '((unread . unread)
5761                             (read . read)
5762                             (unseen . unseen))
5763                           gnus-article-mark-lists))
5764       (push (cons (cdr elem)
5765                   (gnus-byte-compile    ;Why bother?
5766                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5767             gnus-summary-display-cache)))
5768   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5769         (gnus-category-predicate-cache gnus-summary-display-cache))
5770     (gnus-get-predicate display)))
5771
5772 ;; Uses the dynamically bound `gnus-number' variable.
5773 (defvar gnus-number)
5774 (defun gnus-article-marked-p (type &optional article)
5775   (let ((article (or article gnus-number)))
5776     (cond
5777      ((eq type 'tick)
5778       (memq article gnus-newsgroup-marked))
5779      ((eq type 'spam)
5780       (memq article gnus-newsgroup-spam-marked))
5781      ((eq type 'unsend)
5782       (memq article gnus-newsgroup-unsendable))
5783      ((eq type 'undownload)
5784       (memq article gnus-newsgroup-undownloaded))
5785      ((eq type 'download)
5786       (memq article gnus-newsgroup-downloadable))
5787      ((eq type 'unread)
5788       (memq article gnus-newsgroup-unreads))
5789      ((eq type 'read)
5790       (memq article gnus-newsgroup-reads))
5791      ((eq type 'dormant)
5792       (memq article gnus-newsgroup-dormant) )
5793      ((eq type 'expire)
5794       (memq article gnus-newsgroup-expirable))
5795      ((eq type 'reply)
5796       (memq article gnus-newsgroup-replied))
5797      ((eq type 'killed)
5798       (memq article gnus-newsgroup-killed))
5799      ((eq type 'bookmark)
5800       (assq article gnus-newsgroup-bookmarks))
5801      ((eq type 'score)
5802       (assq article gnus-newsgroup-scored))
5803      ((eq type 'save)
5804       (memq article gnus-newsgroup-saved))
5805      ((eq type 'cache)
5806       (memq article gnus-newsgroup-cached))
5807      ((eq type 'forward)
5808       (memq article gnus-newsgroup-forwarded))
5809      ((eq type 'seen)
5810       (not (memq article gnus-newsgroup-unseen)))
5811      (t t))))
5812
5813 (defun gnus-articles-to-read (group &optional read-all)
5814   "Find out what articles the user wants to read."
5815   (let* ((only-read-p t)
5816          (articles
5817           (gnus-list-range-difference
5818           ;; Select all articles if `read-all' is non-nil, or if there
5819           ;; are no unread articles.
5820           (if (or read-all
5821                   (and (zerop (length gnus-newsgroup-marked))
5822                        (zerop (length gnus-newsgroup-unreads)))
5823                   ;; Fetch all if the predicate is non-nil.
5824                   gnus-newsgroup-display)
5825               ;; We want to select the headers for all the articles in
5826               ;; the group, so we select either all the active
5827               ;; articles in the group, or (if that's nil), the
5828               ;; articles in the cache.
5829               (or
5830                (if gnus-newsgroup-maximum-articles
5831                    (let ((active (gnus-active group)))
5832                      (gnus-uncompress-range
5833                       (cons (max (car active)
5834                                  (- (cdr active)
5835                                     gnus-newsgroup-maximum-articles
5836                                     -1))
5837                             (cdr active))))
5838                  (gnus-uncompress-range (gnus-active group)))
5839                (gnus-cache-articles-in-group group))
5840             ;; Select only the "normal" subset of articles.
5841             (setq only-read-p nil)
5842             (gnus-sorted-nunion
5843              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5844              gnus-newsgroup-unreads))
5845           (cdr (assq 'unexist (gnus-info-marks (gnus-get-info group))))))
5846          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5847          (scored (length scored-list))
5848          (number (length articles))
5849          (marked (+ (length gnus-newsgroup-marked)
5850                     (length gnus-newsgroup-dormant)))
5851          (select
5852           (cond
5853            ((numberp read-all)
5854             read-all)
5855            ((numberp gnus-newsgroup-display)
5856             gnus-newsgroup-display)
5857            (t
5858             (condition-case ()
5859                 (cond
5860                  ((and (or (<= scored marked) (= scored number))
5861                        (numberp gnus-large-newsgroup)
5862                        (> number gnus-large-newsgroup))
5863                   (let* ((cursor-in-echo-area nil)
5864                          (initial (gnus-parameter-large-newsgroup-initial
5865                                    gnus-newsgroup-name))
5866                          (default (if only-read-p
5867                                       (or initial gnus-large-newsgroup)
5868                                     number))
5869                          (input
5870                           (read-string
5871                            (if only-read-p
5872                                (format
5873                                 "How many articles from %s (available %d, default %d): "
5874                                 (gnus-group-decoded-name
5875                                  (gnus-group-real-name gnus-newsgroup-name))
5876                                 number default)
5877                              (format
5878                               "How many articles from %s (%d default): "
5879                               (gnus-group-decoded-name
5880                                (gnus-group-real-name gnus-newsgroup-name))
5881                               default))
5882                            nil
5883                            nil
5884                            (number-to-string default))))
5885                     (if (string-match "^[ \t]*$" input) number input)))
5886                  ((and (> scored marked) (< scored number)
5887                        (> (- scored number) 20))
5888                   (let ((input
5889                          (read-string
5890                           (format "%s %s (%d scored, %d total): "
5891                                   "How many articles from"
5892                                   (gnus-group-decoded-name
5893                                    (gnus-group-real-name gnus-newsgroup-name))
5894                                   scored number))))
5895                     (if (string-match "^[ \t]*$" input)
5896                         number input)))
5897                  (t number))
5898               (quit
5899                (message "Quit getting the articles to read")
5900                nil))))))
5901     (setq select (if (stringp select) (string-to-number select) select))
5902     (if (or (null select) (zerop select))
5903         select
5904       (if (and (not (zerop scored)) (<= (abs select) scored))
5905           (progn
5906             (setq articles (sort scored-list '<))
5907             (setq number (length articles)))
5908         (setq articles (copy-sequence articles)))
5909
5910       (when (< (abs select) number)
5911         (if (< select 0)
5912             ;; Select the N oldest articles.
5913             (setcdr (nthcdr (1- (abs select)) articles) nil)
5914           ;; Select the N most recent articles.
5915           (setq articles (nthcdr (- number select) articles))))
5916       (setq gnus-newsgroup-unselected
5917             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5918       (when gnus-alter-articles-to-read-function
5919         (setq articles
5920               (sort
5921                (funcall gnus-alter-articles-to-read-function
5922                         gnus-newsgroup-name articles)
5923                '<)))
5924       articles)))
5925
5926 (defun gnus-killed-articles (killed articles)
5927   (let (out)
5928     (while articles
5929       (when (inline (gnus-member-of-range (car articles) killed))
5930         (push (car articles) out))
5931       (setq articles (cdr articles)))
5932     out))
5933
5934 (defun gnus-article-mark-to-type (mark)
5935   "Return the type of MARK."
5936   (or (cadr (assq mark gnus-article-special-mark-lists))
5937       'list))
5938
5939 (defun gnus-article-unpropagatable-p (mark)
5940   "Return whether MARK should be propagated to back end."
5941   (memq mark gnus-article-unpropagated-mark-lists))
5942
5943 (defun gnus-adjust-marked-articles (info)
5944   "Set all article lists and remove all marks that are no longer valid."
5945   (let* ((marked-lists (gnus-info-marks info))
5946          (active (gnus-active (gnus-info-group info)))
5947          (min (car active))
5948          (max (cdr active))
5949          (types gnus-article-mark-lists)
5950          marks var articles article mark mark-type
5951          bgn end)
5952     ;; Hack to avoid adjusting marks for imap.
5953     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5954               'nnimap)
5955       (setq min 1))
5956
5957     (dolist (marks marked-lists)
5958       (setq mark (car marks)
5959             mark-type (gnus-article-mark-to-type mark)
5960             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5961       ;; We set the variable according to the type of the marks list,
5962       ;; and then adjust the marks to a subset of the active articles.
5963       (cond
5964        ;; Adjust "simple" lists - compressed yet unsorted
5965        ((eq mark-type 'list)
5966         ;; Simultaneously uncompress and clip to active range
5967         ;; See gnus-uncompress-range for a description of possible marks
5968         (let (l lh)
5969           (if (not (cadr marks))
5970               (set var nil)
5971             (setq articles (if (numberp (cddr marks))
5972                                (list (cdr marks))
5973                              (cdr marks))
5974                   lh (cons nil nil)
5975                   l lh)
5976
5977             (while (setq article (pop articles))
5978               (cond ((consp article)
5979                      (setq bgn (max (car article) min)
5980                            end (min (cdr article) max))
5981                      (while (<= bgn end)
5982                        (setq l (setcdr l (cons bgn nil))
5983                              bgn (1+ bgn))))
5984                     ((and (<= min article)
5985                           (>= max article))
5986                      (setq l (setcdr l (cons article nil))))))
5987             (set var (cdr lh)))))
5988        ;; Adjust assocs.
5989        ((eq mark-type 'tuple)
5990         (set var (setq articles (cdr marks)))
5991         (when (not (listp (cdr (symbol-value var))))
5992           (set var (list (symbol-value var))))
5993         (when (not (listp (cdr articles)))
5994           (setq articles (list articles)))
5995         (while articles
5996           (when (or (not (consp (setq article (pop articles))))
5997                     (< (car article) min)
5998                     (> (car article) max))
5999             (set var (delq article (symbol-value var))))))
6000        ;; Adjust ranges (sloppily).
6001        ((eq mark-type 'range)
6002         (cond
6003          ((eq mark 'seen)
6004           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
6005           ;; It should be (seen (NUM1 . NUM2)).
6006           (when (numberp (cddr marks))
6007             (setcdr marks (list (cdr marks))))
6008           (setq articles (cdr marks))
6009           (while (and articles
6010                       (or (and (consp (car articles))
6011                                (> min (cdar articles)))
6012                           (and (numberp (car articles))
6013                                (> min (car articles)))))
6014             (pop articles))
6015           (set var articles))
6016          ((eq mark 'unexist)
6017           (set var (cdr marks)))))))))
6018
6019 (defun gnus-update-missing-marks (missing)
6020   "Go through the list of MISSING articles and remove them from the mark lists."
6021   (when missing
6022     (let (var m)
6023       ;; Go through all types.
6024       (dolist (elem gnus-article-mark-lists)
6025         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6026           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6027           (when (symbol-value var)
6028             ;; This list has articles.  So we delete all missing
6029             ;; articles from it.
6030             (setq m missing)
6031             (while m
6032               (set var (delq (pop m) (symbol-value var))))))))))
6033
6034 (defun gnus-update-marks ()
6035   "Enter the various lists of marked articles into the newsgroup info list."
6036   (let ((types gnus-article-mark-lists)
6037         (info (gnus-get-info gnus-newsgroup-name))
6038         type list newmarked symbol delta-marks)
6039     (when info
6040       ;; Add all marks lists to the list of marks lists.
6041       (while (setq type (pop types))
6042         (setq list (symbol-value
6043                     (setq symbol
6044                           (intern (format "gnus-newsgroup-%s" (car type))))))
6045
6046         (when list
6047           ;; Get rid of the entries of the articles that have the
6048           ;; default score.
6049           (when (and (eq (cdr type) 'score)
6050                      gnus-save-score
6051                      list)
6052             (let* ((arts list)
6053                    (prev (cons nil list))
6054                    (all prev))
6055               (while arts
6056                 (if (or (not (consp (car arts)))
6057                         (= (cdar arts) gnus-summary-default-score))
6058                     (setcdr prev (cdr arts))
6059                   (setq prev arts))
6060                 (setq arts (cdr arts)))
6061               (setq list (cdr all)))))
6062
6063         (when (eq (cdr type) 'seen)
6064           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6065
6066         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6067           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6068
6069         (when (and (gnus-check-backend-function
6070                     'request-set-mark gnus-newsgroup-name)
6071                    (not (gnus-article-unpropagatable-p (cdr type))))
6072           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6073                  ;; Don't do anything about marks for articles we
6074                  ;; didn't actually get any headers for.
6075                  (del
6076                   (gnus-list-range-intersection
6077                    gnus-newsgroup-articles
6078                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6079                  (add
6080                   (gnus-list-range-intersection
6081                    gnus-newsgroup-articles
6082                    (gnus-remove-from-range
6083                     (gnus-copy-sequence list) old))))
6084             (when add
6085               (push (list add 'add (list (cdr type))) delta-marks))
6086             (when del
6087               ;; Don't delete marks from outside the active range.
6088               ;; This shouldn't happen, but is a sanity check.
6089               (setq del (gnus-sorted-range-intersection
6090                          (gnus-active gnus-newsgroup-name) del))
6091               (push (list del 'del (list (cdr type))) delta-marks))))
6092
6093         (when (or list
6094                   (eq (cdr type) 'unexist))
6095           (push (cons (cdr type) list) newmarked)))
6096
6097       (when delta-marks
6098         (unless (gnus-check-group gnus-newsgroup-name)
6099           (error "Can't open server for %s" gnus-newsgroup-name))
6100         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6101
6102       ;; Enter these new marks into the info of the group.
6103       (if (nthcdr 3 info)
6104           (setcar (nthcdr 3 info) newmarked)
6105         ;; Add the marks lists to the end of the info.
6106         (when newmarked
6107           (setcdr (nthcdr 2 info) (list newmarked))))
6108
6109       ;; Cut off the end of the info if there's nothing else there.
6110       (let ((i 5))
6111         (while (and (> i 2)
6112                     (not (nth i info)))
6113           (when (nthcdr (decf i) info)
6114             (setcdr (nthcdr i info) nil)))))))
6115
6116 (defun gnus-set-mode-line (where)
6117   "Set the mode line of the article or summary buffers.
6118 If WHERE is `summary', the summary mode line format will be used."
6119   ;; Is this mode line one we keep updated?
6120   (when (and (memq where gnus-updated-mode-lines)
6121              (symbol-value
6122               (intern (format "gnus-%s-mode-line-format-spec" where))))
6123     (let (mode-string)
6124       ;; We evaluate this in the summary buffer since these
6125       ;; variables are buffer-local to that buffer.
6126       (with-current-buffer gnus-summary-buffer
6127         ;; We bind all these variables that are used in the `eval' form
6128         ;; below.
6129         (let* ((mformat (symbol-value
6130                          (intern
6131                           (format "gnus-%s-mode-line-format-spec" where))))
6132                (gnus-tmp-group-name (gnus-mode-string-quote
6133                                      (gnus-group-decoded-name
6134                                       gnus-newsgroup-name)))
6135                (gnus-tmp-article-number (or gnus-current-article 0))
6136                (gnus-tmp-unread gnus-newsgroup-unreads)
6137                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6138                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6139                (gnus-tmp-unread-and-unselected
6140                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6141                             (zerop gnus-tmp-unselected))
6142                        "")
6143                       ((zerop gnus-tmp-unselected)
6144                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6145                       (t (format "{%d(+%d) more}"
6146                                  gnus-tmp-unread-and-unticked
6147                                  gnus-tmp-unselected))))
6148                (gnus-tmp-subject
6149                 (if (and gnus-current-headers
6150                          (vectorp gnus-current-headers))
6151                     (gnus-mode-string-quote
6152                      (mail-header-subject gnus-current-headers))
6153                   ""))
6154                bufname-length max-len
6155                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6156           (setq mode-string (eval mformat))
6157           (setq bufname-length (if (string-match "%b" mode-string)
6158                                    (- (length
6159                                        (buffer-name
6160                                         (if (eq where 'summary)
6161                                             nil
6162                                           (get-buffer gnus-article-buffer))))
6163                                       2)
6164                                  0))
6165           (setq max-len (max 4 (if gnus-mode-non-string-length
6166                                    (- (window-width)
6167                                       gnus-mode-non-string-length
6168                                       bufname-length)
6169                                  (length mode-string))))
6170           ;; We might have to chop a bit of the string off...
6171           (when (> (length mode-string) max-len)
6172             (setq mode-string
6173                   (concat (truncate-string-to-width mode-string (- max-len 3))
6174                           "...")))))
6175       ;; Update the mode line.
6176       (setq mode-line-buffer-identification
6177             (gnus-mode-line-buffer-identification (list mode-string)))
6178       (set-buffer-modified-p t))))
6179
6180 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6181   "Go through the HEADERS list and add all Xrefs to a hash table.
6182 The resulting hash table is returned, or nil if no Xrefs were found."
6183   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6184          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6185          (xref-hashtb (gnus-make-hashtable))
6186          start group entry number xrefs header)
6187     (while headers
6188       (setq header (pop headers))
6189       (when (and (setq xrefs (mail-header-xref header))
6190                  (not (memq (setq number (mail-header-number header))
6191                             unreads)))
6192         (setq start 0)
6193         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6194           (setq start (match-end 0))
6195           (setq group (if prefix
6196                           (concat prefix (substring xrefs (match-beginning 1)
6197                                                     (match-end 1)))
6198                         (substring xrefs (match-beginning 1) (match-end 1))))
6199           (setq number
6200                 (string-to-number (substring xrefs (match-beginning 2)
6201                                           (match-end 2))))
6202           (if (setq entry (gnus-gethash group xref-hashtb))
6203               (setcdr entry (cons number (cdr entry)))
6204             (gnus-sethash group (cons number nil) xref-hashtb)))))
6205     (and start xref-hashtb)))
6206
6207 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6208   "Look through all the headers and mark the Xrefs as read."
6209   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6210         name info xref-hashtb idlist method nth4)
6211     (with-current-buffer gnus-group-buffer
6212       (when (setq xref-hashtb
6213                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6214         (mapatoms
6215          (lambda (group)
6216            (unless (string= from-newsgroup (setq name (symbol-name group)))
6217              (setq idlist (symbol-value group))
6218              ;; Dead groups are not updated.
6219              (and (prog1
6220                       (setq info (gnus-get-info name))
6221                     (when (stringp (setq nth4 (gnus-info-method info)))
6222                       (setq nth4 (gnus-server-to-method nth4))))
6223                   ;; Only do the xrefs if the group has the same
6224                   ;; select method as the group we have just read.
6225                   (or (gnus-methods-equal-p
6226                        nth4 (gnus-find-method-for-group from-newsgroup))
6227                       virtual
6228                       (equal nth4 (setq method (gnus-find-method-for-group
6229                                                 from-newsgroup)))
6230                       (and (equal (car nth4) (car method))
6231                            (equal (nth 1 nth4) (nth 1 method))))
6232                   gnus-use-cross-reference
6233                   (or (not (eq gnus-use-cross-reference t))
6234                       virtual
6235                       ;; Only do cross-references on subscribed
6236                       ;; groups, if that is what is wanted.
6237                       (<= (gnus-info-level info) gnus-level-subscribed))
6238                   (gnus-group-make-articles-read name idlist))))
6239          xref-hashtb)))))
6240
6241 (defun gnus-compute-read-articles (group articles)
6242   (let* ((entry (gnus-group-entry group))
6243          (info (nth 2 entry))
6244          (active (gnus-active group))
6245          ninfo)
6246     (when entry
6247       ;; First peel off all invalid article numbers.
6248       (when active
6249         (let ((ids articles)
6250               id first)
6251           (while (setq id (pop ids))
6252             (when (and first (> id (cdr active)))
6253               ;; We'll end up in this situation in one particular
6254               ;; obscure situation.  If you re-scan a group and get
6255               ;; a new article that is cross-posted to a different
6256               ;; group that has not been re-scanned, you might get
6257               ;; crossposted article that has a higher number than
6258               ;; Gnus believes possible.  So we re-activate this
6259               ;; group as well.  This might mean doing the
6260               ;; crossposting thingy will *increase* the number
6261               ;; of articles in some groups.  Tsk, tsk.
6262               (setq active (or (gnus-activate-group group) active)))
6263             (when (or (> id (cdr active))
6264                       (< id (car active)))
6265               (setq articles (delq id articles))))))
6266       ;; If the read list is nil, we init it.
6267       (if (and active
6268                (null (gnus-info-read info))
6269                (> (car active) 1))
6270           (setq ninfo (cons 1 (1- (car active))))
6271         (setq ninfo (gnus-info-read info)))
6272       ;; Then we add the read articles to the range.
6273       (gnus-add-to-range
6274        ninfo (setq articles (sort articles '<))))))
6275
6276 (defun gnus-group-make-articles-read (group articles)
6277   "Update the info of GROUP to say that ARTICLES are read."
6278   (let* ((num 0)
6279          (entry (gnus-group-entry group))
6280          (info (nth 2 entry))
6281          (active (gnus-active group))
6282          (set-marks
6283           (gnus-method-option-p
6284            (gnus-find-method-for-group group)
6285            'server-marks))
6286          range)
6287     (if (not entry)
6288         ;; Group that Gnus doesn't know exists, but still allow the
6289         ;; backend to set marks.
6290         (when set-marks
6291           (gnus-request-set-mark
6292            group (list (list (gnus-compress-sequence (sort articles #'<))
6293                              'add '(read)))))
6294       ;; Normal, subscribed groups.
6295       (setq range (gnus-compute-read-articles group articles))
6296       (with-current-buffer gnus-group-buffer
6297         (gnus-undo-register
6298           `(progn
6299              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6300              (gnus-info-set-read ',info ',(gnus-info-read info))
6301              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6302              (when ,set-marks
6303                (gnus-request-set-mark
6304                 ,group (list (list ',range 'del '(read)))))
6305              (gnus-group-update-group ,group t))))
6306       ;; Add the read articles to the range.
6307       (gnus-info-set-read info range)
6308       (when set-marks
6309         (gnus-request-set-mark group (list (list range 'add '(read)))))
6310       ;; Then we have to re-compute how many unread
6311       ;; articles there are in this group.
6312       (when active
6313         (cond
6314          ((not range)
6315           (setq num (- (1+ (cdr active)) (car active))))
6316          ((not (listp (cdr range)))
6317           (setq num (- (cdr active) (- (1+ (cdr range))
6318                                        (car range)))))
6319          (t
6320           (while range
6321             (if (numberp (car range))
6322                 (setq num (1+ num))
6323               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6324             (setq range (cdr range)))
6325           (setq num (- (cdr active) num))))
6326         ;; Update the number of unread articles.
6327         (setcar entry num)
6328         ;; Update the group buffer.
6329         (unless (gnus-ephemeral-group-p group)
6330           (gnus-group-update-group group t))))))
6331
6332 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6333   (let ((cur nntp-server-buffer)
6334         (dependencies
6335          (or dependencies
6336              (with-current-buffer gnus-summary-buffer
6337                gnus-newsgroup-dependencies)))
6338         headers id end ref number
6339         (mail-parse-charset gnus-newsgroup-charset)
6340         (mail-parse-ignored-charsets
6341          (save-current-buffer (condition-case nil
6342                                   (set-buffer gnus-summary-buffer)
6343                                 (error))
6344                               gnus-newsgroup-ignored-charsets)))
6345     (with-current-buffer nntp-server-buffer
6346       ;; Translate all TAB characters into SPACE characters.
6347       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6348       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6349       (ietf-drums-unfold-fws)
6350       (gnus-run-hooks 'gnus-parse-headers-hook)
6351       (let ((case-fold-search t)
6352             in-reply-to header p lines chars)
6353         (goto-char (point-min))
6354         ;; Search to the beginning of the next header.  Error messages
6355         ;; do not begin with 2 or 3.
6356         (while (re-search-forward "^[23][0-9]+ " nil t)
6357           (setq id nil
6358                 ref nil)
6359           ;; This implementation of this function, with nine
6360           ;; search-forwards instead of the one re-search-forward and
6361           ;; a case (which basically was the old function) is actually
6362           ;; about twice as fast, even though it looks messier.  You
6363           ;; can't have everything, I guess.  Speed and elegance
6364           ;; doesn't always go hand in hand.
6365           (setq
6366            header
6367            (vector
6368             ;; Number.
6369             (prog1
6370                 (setq number (read cur))
6371               (end-of-line)
6372               (setq p (point))
6373               (narrow-to-region (point)
6374                                 (or (and (search-forward "\n.\n" nil t)
6375                                          (- (point) 2))
6376                                     (point))))
6377             ;; Subject.
6378             (progn
6379               (goto-char p)
6380               (if (search-forward "\nsubject:" nil t)
6381                   (funcall gnus-decode-encoded-word-function
6382                            (nnheader-header-value))
6383                 "(none)"))
6384             ;; From.
6385             (progn
6386               (goto-char p)
6387               (if (search-forward "\nfrom:" nil t)
6388                   (funcall gnus-decode-encoded-address-function
6389                            (nnheader-header-value))
6390                 "(nobody)"))
6391             ;; Date.
6392             (progn
6393               (goto-char p)
6394               (if (search-forward "\ndate:" nil t)
6395                   (nnheader-header-value) ""))
6396             ;; Message-ID.
6397             (progn
6398               (goto-char p)
6399               (setq id (if (re-search-forward
6400                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6401                            ;; We do it this way to make sure the Message-ID
6402                            ;; is (somewhat) syntactically valid.
6403                            (buffer-substring (match-beginning 1)
6404                                              (match-end 1))
6405                          ;; If there was no message-id, we just fake one
6406                          ;; to make subsequent routines simpler.
6407                          (nnheader-generate-fake-message-id number))))
6408             ;; References.
6409             (progn
6410               (goto-char p)
6411               (if (search-forward "\nreferences:" nil t)
6412                   (progn
6413                     (setq end (point))
6414                     (prog1
6415                         (nnheader-header-value)
6416                       (setq ref
6417                             (buffer-substring
6418                              (progn
6419                                (end-of-line)
6420                                (search-backward ">" end t)
6421                                (1+ (point)))
6422                              (progn
6423                                (search-backward "<" end t)
6424                                (point))))))
6425                 ;; Get the references from the in-reply-to header if there
6426                 ;; were no references and the in-reply-to header looks
6427                 ;; promising.
6428                 (if (and (search-forward "\nin-reply-to:" nil t)
6429                          (setq in-reply-to (nnheader-header-value))
6430                          (string-match "<[^>]+>" in-reply-to))
6431                     (let (ref2)
6432                       (setq ref (substring in-reply-to (match-beginning 0)
6433                                            (match-end 0)))
6434                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6435                         (setq ref2 (substring in-reply-to (match-beginning 0)
6436                                               (match-end 0)))
6437                         (when (> (length ref2) (length ref))
6438                           (setq ref ref2)))
6439                       ref)
6440                   (setq ref nil))))
6441             ;; Chars.
6442             (progn
6443               (goto-char p)
6444               (if (search-forward "\nchars: " nil t)
6445                   (if (numberp (setq chars (ignore-errors (read cur))))
6446                       chars -1)
6447                 -1))
6448             ;; Lines.
6449             (progn
6450               (goto-char p)
6451               (if (search-forward "\nlines: " nil t)
6452                   (if (numberp (setq lines (ignore-errors (read cur))))
6453                       lines -1)
6454                 -1))
6455             ;; Xref.
6456             (progn
6457               (goto-char p)
6458               (and (search-forward "\nxref:" nil t)
6459                    (nnheader-header-value)))
6460             ;; Extra.
6461             (when gnus-extra-headers
6462               (let ((extra gnus-extra-headers)
6463                     out)
6464                 (while extra
6465                   (goto-char p)
6466                   (when (search-forward
6467                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6468                     (push (cons (car extra) (nnheader-header-value))
6469                           out))
6470                   (pop extra))
6471                 out))))
6472           (when (equal id ref)
6473             (setq ref nil))
6474
6475           (when gnus-alter-header-function
6476             (funcall gnus-alter-header-function header)
6477             (setq id (mail-header-id header)
6478                   ref (gnus-parent-id (mail-header-references header))))
6479
6480           (when (setq header
6481                       (gnus-dependencies-add-header
6482                        header dependencies force-new))
6483             (push header headers))
6484           (goto-char (point-max))
6485           (widen))
6486         (nreverse headers)))))
6487
6488 ;; Goes through the xover lines and returns a list of vectors
6489 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6490                                                   force-new dependencies
6491                                                   group also-fetch-heads)
6492   "Parse the news overview data in the server buffer.
6493 Return a list of headers that match SEQUENCE (see
6494 `nntp-retrieve-headers')."
6495   ;; Get the Xref when the users reads the articles since most/some
6496   ;; NNTP servers do not include Xrefs when using XOVER.
6497   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6498   (let ((mail-parse-charset gnus-newsgroup-charset)
6499         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6500         (cur nntp-server-buffer)
6501         (dependencies (or dependencies gnus-newsgroup-dependencies))
6502         (allp (cond
6503                ((eq gnus-read-all-available-headers t)
6504                 t)
6505                ((and (stringp gnus-read-all-available-headers)
6506                      group)
6507                 (string-match gnus-read-all-available-headers group))
6508                (t
6509                 nil)))
6510         number headers header)
6511     (with-current-buffer nntp-server-buffer
6512       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6513       ;; Allow the user to mangle the headers before parsing them.
6514       (gnus-run-hooks 'gnus-parse-headers-hook)
6515       (goto-char (point-min))
6516       (gnus-parse-without-error
6517         (while (and (or sequence allp)
6518                     (not (eobp)))
6519           (setq number (read cur))
6520           (when (not allp)
6521             (while (and sequence
6522                         (< (car sequence) number))
6523               (setq sequence (cdr sequence))))
6524           (when (and (or allp
6525                          (and sequence
6526                               (eq number (car sequence))))
6527                      (progn
6528                        (setq sequence (cdr sequence))
6529                        (setq header (inline
6530                                       (gnus-nov-parse-line
6531                                        number dependencies force-new)))))
6532             (push header headers))
6533           (forward-line 1)))
6534       ;; A common bug in inn is that if you have posted an article and
6535       ;; then retrieves the active file, it will answer correctly --
6536       ;; the new article is included.  However, a NOV entry for the
6537       ;; article may not have been generated yet, so this may fail.
6538       ;; We work around this problem by retrieving the last few
6539       ;; headers using HEAD.
6540       (if (or (not also-fetch-heads)
6541               (not sequence))
6542           ;; We (probably) got all the headers.
6543           (nreverse headers)
6544         (let ((gnus-nov-is-evil t))
6545           (nconc
6546            (nreverse headers)
6547            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6548              (gnus-get-newsgroup-headers))))))))
6549
6550 (defun gnus-article-get-xrefs ()
6551   "Fill in the Xref value in `gnus-current-headers', if necessary.
6552 This is meant to be called in `gnus-article-internal-prepare-hook'."
6553   (let ((headers (with-current-buffer gnus-summary-buffer
6554                    gnus-current-headers)))
6555     (or (not gnus-use-cross-reference)
6556         (not headers)
6557         (and (mail-header-xref headers)
6558              (not (string= (mail-header-xref headers) "")))
6559         (let ((case-fold-search t)
6560               xref)
6561           (save-restriction
6562             (nnheader-narrow-to-headers)
6563             (goto-char (point-min))
6564             (when (or (and (not (eobp))
6565                            (eq (downcase (char-after)) ?x)
6566                            (looking-at "Xref:"))
6567                       (search-forward "\nXref:" nil t))
6568               (goto-char (1+ (match-end 0)))
6569               (setq xref (buffer-substring (point) (point-at-eol)))
6570               (mail-header-set-xref headers xref)))))))
6571
6572 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6573   "Find article ID and insert the summary line for that article.
6574 OLD-HEADER can either be a header or a line number to insert
6575 the subject line on.
6576 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6577 and OLD-HEADER will be used when the summary line is inserted,
6578 too, instead of trying to fetch new headers."
6579   (let* ((line (and (numberp old-header) old-header))
6580          (old-header (and (vectorp old-header) old-header))
6581          (header (cond ((and old-header use-old-header)
6582                         old-header)
6583                        ((and (numberp id)
6584                              (gnus-number-to-header id))
6585                         (gnus-number-to-header id))
6586                        (t
6587                         (gnus-read-header id))))
6588          (number (and (numberp id) id))
6589          d)
6590     (when header
6591       ;; Rebuild the thread that this article is part of and go to the
6592       ;; article we have fetched.
6593       (when (and (not gnus-show-threads)
6594                  old-header)
6595         (when (and number
6596                    (setq d (gnus-data-find (mail-header-number old-header))))
6597           (goto-char (gnus-data-pos d))
6598           (gnus-data-remove
6599            number
6600            (- (point-at-bol)
6601               (prog1
6602                   (1+ (point-at-eol))
6603                 (gnus-delete-line))))))
6604       ;; Remove list identifiers from subject.
6605       (let ((gnus-newsgroup-headers (list header)))
6606         (gnus-summary-remove-list-identifiers))
6607       (when old-header
6608         (mail-header-set-number header (mail-header-number old-header)))
6609       (setq gnus-newsgroup-sparse
6610             (delq (setq number (mail-header-number header))
6611                   gnus-newsgroup-sparse))
6612       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6613       (push number gnus-newsgroup-limit)
6614       (gnus-rebuild-thread (mail-header-id header) line)
6615       (gnus-summary-goto-subject number nil t))
6616     (when (and (numberp number)
6617                (> number 0))
6618       ;; We have to update the boundaries even if we can't fetch the
6619       ;; article if ID is a number -- so that the next `P' or `N'
6620       ;; command will fetch the previous (or next) article even
6621       ;; if the one we tried to fetch this time has been canceled.
6622       (unless (and gnus-newsgroup-end (< number gnus-newsgroup-end))
6623         (setq gnus-newsgroup-end number))
6624       (unless (and gnus-newsgroup-begin (> number gnus-newsgroup-begin))
6625         (setq gnus-newsgroup-begin number))
6626       (setq gnus-newsgroup-unselected
6627             (delq number gnus-newsgroup-unselected)))
6628     ;; Report back a success?
6629     (and header (mail-header-number header))))
6630
6631 ;;; Process/prefix in the summary buffer
6632
6633 (defun gnus-summary-work-articles (n)
6634   "Return a list of articles to be worked upon.
6635 The prefix argument, the list of process marked articles, and the
6636 current article will be taken into consideration."
6637   (with-current-buffer gnus-summary-buffer
6638     (cond
6639      (n
6640       ;; A numerical prefix has been given.
6641       (setq n (prefix-numeric-value n))
6642       (let ((backward (< n 0))
6643             (n (abs (prefix-numeric-value n)))
6644             articles article)
6645         (save-excursion
6646           (while
6647               (and (> n 0)
6648                    (push (setq article (gnus-summary-article-number))
6649                          articles)
6650                    (if backward
6651                        (gnus-summary-find-prev nil article)
6652                      (gnus-summary-find-next nil article)))
6653             (decf n)))
6654         (nreverse articles)))
6655      ((and (gnus-region-active-p) (mark))
6656       (message "region active")
6657       ;; Work on the region between point and mark.
6658       (let ((max (max (point) (mark)))
6659             articles article)
6660         (save-excursion
6661           (goto-char (min (point) (mark)))
6662           (while
6663               (and
6664                (push (setq article (gnus-summary-article-number)) articles)
6665                (gnus-summary-find-next nil article)
6666                (< (point) max)))
6667           (nreverse articles))))
6668      (gnus-newsgroup-processable
6669       ;; There are process-marked articles present.
6670       ;; Save current state.
6671       (gnus-summary-save-process-mark)
6672       ;; Return the list.
6673       (reverse gnus-newsgroup-processable))
6674      (t
6675       ;; Just return the current article.
6676       (list (gnus-summary-article-number))))))
6677
6678 (defmacro gnus-summary-iterate (arg &rest forms)
6679   "Iterate over the process/prefixed articles and do FORMS.
6680 ARG is the interactive prefix given to the command.  FORMS will be
6681 executed with point over the summary line of the articles."
6682   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6683     `(let ((,articles (gnus-summary-work-articles ,arg)))
6684        (while ,articles
6685          (gnus-summary-goto-subject (car ,articles))
6686          ,@forms
6687          (pop ,articles)))))
6688
6689 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6690 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6691
6692 (defun gnus-summary-save-process-mark ()
6693   "Push the current set of process marked articles on the stack."
6694   (interactive)
6695   (push (copy-sequence gnus-newsgroup-processable)
6696         gnus-newsgroup-process-stack))
6697
6698 (defun gnus-summary-kill-process-mark ()
6699   "Push the current set of process marked articles on the stack and unmark."
6700   (interactive)
6701   (gnus-summary-save-process-mark)
6702   (gnus-summary-unmark-all-processable))
6703
6704 (defun gnus-summary-yank-process-mark ()
6705   "Pop the last process mark state off the stack and restore it."
6706   (interactive)
6707   (unless gnus-newsgroup-process-stack
6708     (error "Empty mark stack"))
6709   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6710
6711 (defun gnus-summary-process-mark-set (set)
6712   "Make SET into the current process marked articles."
6713   (gnus-summary-unmark-all-processable)
6714   (mapc 'gnus-summary-set-process-mark set))
6715
6716 ;;; Searching and stuff
6717
6718 (defun gnus-summary-search-group (&optional backward use-level)
6719   "Search for next unread newsgroup.
6720 If optional argument BACKWARD is non-nil, search backward instead."
6721   (with-current-buffer gnus-group-buffer
6722     (when (gnus-group-search-forward
6723            backward nil (if use-level (gnus-group-group-level) nil))
6724       (gnus-group-group-name))))
6725
6726 (defun gnus-summary-best-group (&optional exclude-group)
6727   "Find the name of the best unread group.
6728 If EXCLUDE-GROUP, do not go to this group."
6729   (with-current-buffer gnus-group-buffer
6730     (save-excursion
6731       (gnus-group-best-unread-group exclude-group))))
6732
6733 (defun gnus-summary-find-next (&optional unread article backward)
6734   (if backward
6735       (gnus-summary-find-prev unread article)
6736     (let* ((dummy (gnus-summary-article-intangible-p))
6737            (article (or article (gnus-summary-article-number)))
6738            (data (gnus-data-find-list article))
6739            result)
6740       (when (and (not dummy)
6741                  (or (not gnus-summary-check-current)
6742                      (not unread)
6743                      (not (gnus-data-unread-p (car data)))))
6744         (setq data (cdr data)))
6745       (when (setq result
6746                   (if unread
6747                       (progn
6748                         (while data
6749                           (unless (memq (gnus-data-number (car data))
6750                                         (cond
6751                                          ((eq gnus-auto-goto-ignores
6752                                               'always-undownloaded)
6753                                           gnus-newsgroup-undownloaded)
6754                                          (gnus-plugged
6755                                           nil)
6756                                          ((eq gnus-auto-goto-ignores
6757                                               'unfetched)
6758                                           gnus-newsgroup-unfetched)
6759                                          ((eq gnus-auto-goto-ignores
6760                                               'undownloaded)
6761                                           gnus-newsgroup-undownloaded)))
6762                             (when (gnus-data-unread-p (car data))
6763                               (setq result (car data)
6764                                     data nil)))
6765                           (setq data (cdr data)))
6766                         result)
6767                     (car data)))
6768         (goto-char (gnus-data-pos result))
6769         (gnus-data-number result)))))
6770
6771 (defun gnus-summary-find-prev (&optional unread article)
6772   (let* ((eobp (eobp))
6773          (article (or article (gnus-summary-article-number)))
6774          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6775          result)
6776     (when (and (not eobp)
6777                (or (not gnus-summary-check-current)
6778                    (not unread)
6779                    (not (gnus-data-unread-p (car data)))))
6780       (setq data (cdr data)))
6781     (when (setq result
6782                 (if unread
6783                     (progn
6784                       (while data
6785                         (unless (memq (gnus-data-number (car data))
6786                                       (cond
6787                                        ((eq gnus-auto-goto-ignores
6788                                             'always-undownloaded)
6789                                         gnus-newsgroup-undownloaded)
6790                                        (gnus-plugged
6791                                         nil)
6792                                        ((eq gnus-auto-goto-ignores
6793                                             'unfetched)
6794                                         gnus-newsgroup-unfetched)
6795                                        ((eq gnus-auto-goto-ignores
6796                                             'undownloaded)
6797                                         gnus-newsgroup-undownloaded)))
6798                           (when (gnus-data-unread-p (car data))
6799                             (setq result (car data)
6800                                   data nil)))
6801                         (setq data (cdr data)))
6802                       result)
6803                   (car data)))
6804       (goto-char (gnus-data-pos result))
6805       (gnus-data-number result))))
6806
6807 (defun gnus-summary-find-subject (subject &optional unread backward article)
6808   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6809          (article (or article (gnus-summary-article-number)))
6810          (articles (gnus-data-list backward))
6811          (arts (gnus-data-find-list article articles))
6812          result)
6813     (when (or (not gnus-summary-check-current)
6814               (not unread)
6815               (not (gnus-data-unread-p (car arts))))
6816       (setq arts (cdr arts)))
6817     (while arts
6818       (and (or (not unread)
6819                (gnus-data-unread-p (car arts)))
6820            (vectorp (gnus-data-header (car arts)))
6821            (gnus-subject-equal
6822             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6823            (setq result (car arts)
6824                  arts nil))
6825       (setq arts (cdr arts)))
6826     (and result
6827          (goto-char (gnus-data-pos result))
6828          (gnus-data-number result))))
6829
6830 (defun gnus-summary-search-forward (&optional unread subject backward)
6831   "Search forward for an article.
6832 If UNREAD, look for unread articles.  If SUBJECT, look for
6833 articles with that subject.  If BACKWARD, search backward instead."
6834   (cond (subject (gnus-summary-find-subject subject unread backward))
6835         (backward (gnus-summary-find-prev unread))
6836         (t (gnus-summary-find-next unread))))
6837
6838 (defun gnus-recenter (&optional n)
6839   "Center point in window and redisplay frame.
6840 Also do horizontal recentering."
6841   (interactive "P")
6842   (when (and gnus-auto-center-summary
6843              (not (eq gnus-auto-center-summary 'vertical)))
6844     (gnus-horizontal-recenter))
6845   (if (fboundp 'recenter-top-bottom)
6846       (recenter-top-bottom n)
6847     (recenter n)))
6848
6849 (put 'gnus-recenter 'isearch-scroll t)
6850
6851 (defun gnus-forward-line-ignore-invisible (n)
6852   "Move N lines forward (backward if N is negative).
6853 Like forward-line, but skip over (and don't count) invisible lines."
6854   (let (done)
6855     (while (and (> n 0) (not done))
6856       ;; If the following character is currently invisible,
6857       ;; skip all characters with that same `invisible' property value.
6858       (while (gnus-invisible-p (point))
6859         (goto-char (gnus-next-char-property-change (point))))
6860       (forward-line 1)
6861       (if (eobp)
6862           (setq done t)
6863         (setq n (1- n))))
6864     (while (and (< n 0) (not done))
6865       (forward-line -1)
6866       (if (bobp) (setq done t)
6867         (setq n (1+ n))
6868         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6869           (goto-char (gnus-previous-char-property-change (point))))))))
6870
6871 (defun gnus-summary-recenter ()
6872   "Center point in the summary window.
6873 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6874 displayed, no centering will be performed."
6875   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6876   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6877   (interactive)
6878   ;; The user has to want it.
6879   (when gnus-auto-center-summary
6880     (let* ((top (cond ((< (window-height) 4) 0)
6881                       ((< (window-height) 7) 1)
6882                       (t (if (numberp gnus-auto-center-summary)
6883                              gnus-auto-center-summary
6884                            (/ (1- (window-height)) 2)))))
6885            (height (1- (window-height)))
6886            (bottom (save-excursion
6887                      (goto-char (point-max))
6888                      (gnus-forward-line-ignore-invisible (- height))
6889                      (point)))
6890            (window (get-buffer-window (current-buffer))))
6891       (when (get-buffer-window gnus-article-buffer)
6892         ;; Only do recentering when the article buffer is displayed,
6893         ;; Set the window start to either `bottom', which is the biggest
6894         ;; possible valid number, or the second line from the top,
6895         ;; whichever is the least.
6896         (let ((top-pos (save-excursion
6897                          (gnus-forward-line-ignore-invisible (- top))
6898                          (point))))
6899           (if (> bottom top-pos)
6900               ;; Keep the second line from the top visible
6901               (set-window-start window top-pos)
6902             ;; Try to keep the bottom line visible; if it's partially
6903             ;; obscured, either scroll one more line to make it fully
6904             ;; visible, or revert to using TOP-POS.
6905             (save-excursion
6906               (goto-char (point-max))
6907               (gnus-forward-line-ignore-invisible -1)
6908               (let ((last-line-start (point)))
6909                 (goto-char bottom)
6910                 (set-window-start window (point) t)
6911                 (when (not (pos-visible-in-window-p last-line-start window))
6912                   (gnus-forward-line-ignore-invisible 1)
6913                   (set-window-start window (min (point) top-pos) t)))))))
6914       ;; Do horizontal recentering while we're at it.
6915       (when (and (get-buffer-window (current-buffer) t)
6916                  (not (eq gnus-auto-center-summary 'vertical)))
6917         (let ((selected (selected-window)))
6918           (select-window (get-buffer-window (current-buffer) t))
6919           (gnus-summary-position-point)
6920           (gnus-horizontal-recenter)
6921           (select-window selected))))))
6922
6923 (defun gnus-summary-jump-to-group (newsgroup)
6924   "Move point to NEWSGROUP in group mode buffer."
6925   ;; Keep update point of group mode buffer if visible.
6926   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6927       (save-window-excursion
6928         ;; Take care of tree window mode.
6929         (when (get-buffer-window gnus-group-buffer)
6930           (pop-to-buffer gnus-group-buffer))
6931         (gnus-group-jump-to-group newsgroup))
6932     (save-excursion
6933       ;; Take care of tree window mode.
6934       (if (get-buffer-window gnus-group-buffer 0)
6935           (pop-to-buffer gnus-group-buffer)
6936         (set-buffer gnus-group-buffer))
6937       (gnus-group-jump-to-group newsgroup))))
6938
6939 ;; This function returns a list of article numbers based on the
6940 ;; difference between the ranges of read articles in this group and
6941 ;; the range of active articles.
6942 (defun gnus-list-of-unread-articles (group)
6943   (let* ((read (gnus-info-read (gnus-get-info group)))
6944          (active (or (gnus-active group) (gnus-activate-group group)))
6945          (last (or (cdr active)
6946                    (error "Group %s couldn't be activated " group)))
6947          (bottom (if gnus-newsgroup-maximum-articles
6948                      (max (car active)
6949                           (- last gnus-newsgroup-maximum-articles -1))
6950                    (car active)))
6951          first nlast unread)
6952     ;; If none are read, then all are unread.
6953     (if (not read)
6954         (setq first bottom)
6955       ;; If the range of read articles is a single range, then the
6956       ;; first unread article is the article after the last read
6957       ;; article.  Sounds logical, doesn't it?
6958       (if (and (not (listp (cdr read)))
6959                (or (< (car read) bottom)
6960                    (progn (setq read (list read))
6961                           nil)))
6962           (setq first (max bottom (1+ (cdr read))))
6963         ;; `read' is a list of ranges.
6964         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6965                                   (caar read)))
6966                   1)
6967           (setq first bottom))
6968         (while read
6969           (when first
6970             (while (< first nlast)
6971               (setq unread (cons first unread)
6972                     first (1+ first))))
6973           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6974           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6975           (setq read (cdr read)))))
6976     ;; And add the last unread articles.
6977     (while (<= first last)
6978       (setq unread (cons first unread)
6979             first (1+ first)))
6980     ;; Return the list of unread articles.
6981     (delq 0 (nreverse unread))))
6982
6983 (defun gnus-list-of-read-articles (group)
6984   "Return a list of unread, unticked and non-dormant articles."
6985   (let* ((info (gnus-get-info group))
6986          (marked (gnus-info-marks info))
6987          (active (gnus-active group)))
6988     (and info active
6989          (gnus-list-range-difference
6990           (gnus-list-range-difference
6991            (gnus-sorted-complement
6992             (gnus-uncompress-range
6993              (if gnus-newsgroup-maximum-articles
6994                  (cons (max (car active)
6995                             (- (cdr active)
6996                                gnus-newsgroup-maximum-articles
6997                                -1))
6998                        (cdr active))
6999                active))
7000             (gnus-list-of-unread-articles group))
7001            (cdr (assq 'dormant marked)))
7002           (cdr (assq 'tick marked))))))
7003
7004 ;; This function returns a sequence of article numbers based on the
7005 ;; difference between the ranges of read articles in this group and
7006 ;; the range of active articles.
7007 (defun gnus-sequence-of-unread-articles (group)
7008   (let* ((read (gnus-info-read (gnus-get-info group)))
7009          (active (or (gnus-active group) (gnus-activate-group group)))
7010          (last (cdr active))
7011          (bottom (if gnus-newsgroup-maximum-articles
7012                      (max (car active)
7013                           (- last gnus-newsgroup-maximum-articles -1))
7014                    (car active)))
7015          first nlast unread)
7016     ;; If none are read, then all are unread.
7017     (if (not read)
7018         (setq first bottom)
7019       ;; If the range of read articles is a single range, then the
7020       ;; first unread article is the article after the last read
7021       ;; article.  Sounds logical, doesn't it?
7022       (if (and (not (listp (cdr read)))
7023                (or (< (car read) bottom)
7024                    (progn (setq read (list read))
7025                           nil)))
7026           (setq first (max bottom (1+ (cdr read))))
7027         ;; `read' is a list of ranges.
7028         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7029                                   (caar read)))
7030                   1)
7031           (setq first bottom))
7032         (while read
7033           (when first
7034             (push (cons first nlast) unread))
7035           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7036           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7037           (setq read (cdr read)))))
7038     ;; And add the last unread articles.
7039     (cond ((not (and first last))
7040            nil)
7041           ((< first last)
7042            (push (cons first last) unread))
7043           ((= first last)
7044            (push first unread)))
7045     ;; Return the sequence of unread articles.
7046     (delq 0 (nreverse unread))))
7047
7048 ;; Various summary commands
7049
7050 (defun gnus-summary-select-article-buffer ()
7051   "Reconfigure windows to show the article buffer.
7052 If `gnus-widen-article-window' is set, show only the article
7053 buffer."
7054   (interactive)
7055   (if (not (gnus-buffer-live-p gnus-article-buffer))
7056       (error "There is no article buffer for this summary buffer")
7057     (unless (get-buffer-window gnus-article-buffer)
7058       (gnus-summary-show-article))
7059     (gnus-configure-windows
7060      (if gnus-widen-article-window
7061          'only-article
7062        'article)
7063      t)
7064     (select-window (get-buffer-window gnus-article-buffer))))
7065
7066 (defun gnus-summary-universal-argument (arg)
7067   "Perform any operation on all articles that are process/prefixed."
7068   (interactive "P")
7069   (let ((articles (gnus-summary-work-articles arg))
7070         func article)
7071     (if (eq
7072          (setq
7073           func
7074           (key-binding
7075            (read-key-sequence
7076             (substitute-command-keys
7077              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7078          'undefined)
7079         (gnus-error 1 "Undefined key")
7080       (save-excursion
7081         (while articles
7082           (gnus-summary-goto-subject (setq article (pop articles)))
7083           (let (gnus-newsgroup-processable)
7084             (command-execute func))
7085           (gnus-summary-remove-process-mark article)))))
7086   (gnus-summary-position-point))
7087
7088 (defun gnus-summary-toggle-truncation (&optional arg)
7089   "Toggle truncation of summary lines.
7090 With ARG, turn line truncation on if ARG is positive."
7091   (interactive "P")
7092   (setq truncate-lines
7093         (if (null arg) (not truncate-lines)
7094           (> (prefix-numeric-value arg) 0)))
7095   (redraw-display))
7096
7097 (defun gnus-summary-find-for-reselect ()
7098   "Return the number of an article to stay on across a reselect.
7099 The current article is considered, then following articles, then previous
7100 articles.  An article is sought which is not canceled and isn't a temporary
7101 insertion from another group.  If there's no such then return a dummy 0."
7102   (let (found)
7103     (dolist (rev '(nil t))
7104       (unless found      ; don't demand the reverse list if we don't need it
7105         (let ((data (gnus-data-find-list
7106                      (gnus-summary-article-number) (gnus-data-list rev))))
7107           (while (and data (not found))
7108             (if (and (< 0 (gnus-data-number (car data)))
7109                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7110                 (setq found (gnus-data-number (car data))))
7111             (setq data (cdr data))))))
7112     (or found 0)))
7113
7114 (defun gnus-summary-reselect-current-group (&optional all rescan)
7115   "Exit and then reselect the current newsgroup.
7116 The prefix argument ALL means to select all articles."
7117   (interactive "P")
7118   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7119     (error "Ephemeral groups can't be reselected"))
7120   (let ((current-subject (gnus-summary-find-for-reselect))
7121         (group gnus-newsgroup-name))
7122     (setq gnus-newsgroup-begin nil)
7123     (gnus-summary-exit nil 'leave-hidden)
7124     ;; We have to adjust the point of group mode buffer because
7125     ;; point was moved to the next unread newsgroup by exiting.
7126     (gnus-summary-jump-to-group group)
7127     (when rescan
7128       (save-excursion
7129         (gnus-group-get-new-news-this-group 1)))
7130     (gnus-group-read-group all t)
7131     (gnus-summary-goto-subject current-subject nil t)))
7132
7133 (defun gnus-summary-rescan-group (&optional all)
7134   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7135   (interactive "P")
7136   (let ((config gnus-current-window-configuration))
7137     (gnus-summary-reselect-current-group all t)
7138     (gnus-configure-windows config)
7139     (when (eq config 'article)
7140       (gnus-summary-select-article))))
7141
7142 (defun gnus-summary-update-info (&optional non-destructive)
7143   (save-excursion
7144     (let ((group gnus-newsgroup-name))
7145       (when group
7146         (when gnus-newsgroup-kill-headers
7147           (setq gnus-newsgroup-killed
7148                 (gnus-compress-sequence
7149                  (gnus-sorted-union
7150                   (gnus-list-range-intersection
7151                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7152                   gnus-newsgroup-unreads)
7153                  t)))
7154         (unless (listp (cdr gnus-newsgroup-killed))
7155           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7156         (let ((headers gnus-newsgroup-headers)
7157               (ephemeral-p (gnus-ephemeral-group-p group))
7158               info)
7159           (unless ephemeral-p
7160             (setq info (copy-sequence (gnus-get-info group))
7161                   info (delq (gnus-info-params info) info)))
7162           ;; Set the new ranges of read articles.
7163           (with-current-buffer gnus-group-buffer
7164             (gnus-undo-force-boundary))
7165           (gnus-update-read-articles
7166            group (gnus-sorted-union
7167                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7168           ;; Set the current article marks.
7169           (let ((gnus-newsgroup-scored
7170                  (if (and (not gnus-save-score)
7171                           (not non-destructive))
7172                      nil
7173                    gnus-newsgroup-scored)))
7174             (save-excursion
7175               (gnus-update-marks)))
7176           ;; Do the cross-ref thing.
7177           (when gnus-use-cross-reference
7178             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7179           ;; Do not switch windows but change the buffer to work.
7180           (set-buffer gnus-group-buffer)
7181           (unless ephemeral-p
7182             (gnus-group-update-group
7183              group nil
7184              (equal info
7185                     (setq info (copy-sequence (gnus-get-info group))
7186                           info (delq (gnus-info-params info) info))))))))))
7187
7188 (defun gnus-summary-save-newsrc (&optional force)
7189   "Save the current number of read/marked articles in the dribble buffer.
7190 The dribble buffer will then be saved.
7191 If FORCE (the prefix), also save the .newsrc file(s)."
7192   (interactive "P")
7193   (gnus-summary-update-info t)
7194   (if force
7195       (gnus-save-newsrc-file)
7196     (gnus-dribble-save)))
7197
7198 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7199
7200 (defun gnus-summary-exit (&optional temporary leave-hidden)
7201   "Exit reading current newsgroup, and then return to group selection mode.
7202 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7203   (interactive)
7204   (gnus-set-global-variables)
7205   (when (gnus-buffer-live-p gnus-article-buffer)
7206     (with-current-buffer gnus-article-buffer
7207       (mm-destroy-parts gnus-article-mime-handles)
7208       ;; Set it to nil for safety reason.
7209       (setq gnus-article-mime-handle-alist nil)
7210       (setq gnus-article-mime-handles nil)))
7211   (gnus-kill-save-kill-buffer)
7212   (gnus-async-halt-prefetch)
7213   (let* ((group gnus-newsgroup-name)
7214          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7215          (gnus-group-is-exiting-p t)
7216          (article-buffer gnus-article-buffer)
7217          (original-article-buffer gnus-original-article-buffer)
7218          (mode major-mode)
7219          (group-point nil)
7220          (buf (current-buffer))
7221          ;; `gnus-single-article-buffer' is nil buffer-locally in
7222          ;; ephemeral group of which summary buffer will be killed,
7223          ;; but the global value may be non-nil.
7224          (single-article-buffer gnus-single-article-buffer))
7225     (unless quit-config
7226       ;; Do adaptive scoring, and possibly save score files.
7227       (when gnus-newsgroup-adaptive
7228         (gnus-score-adaptive))
7229       (when gnus-use-scoring
7230         (gnus-score-save)))
7231     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7232     (when gnus-use-cache
7233       (gnus-cache-possibly-remove-articles)
7234       (gnus-cache-save-buffers))
7235     (gnus-async-prefetch-remove-group group)
7236     (when gnus-suppress-duplicates
7237       (gnus-dup-enter-articles))
7238     (when gnus-use-trees
7239       (gnus-tree-close group))
7240     (when gnus-use-cache
7241       (gnus-cache-write-active))
7242     ;; Remove entries for this group.
7243     (nnmail-purge-split-history (gnus-group-real-name group))
7244     ;; Make all changes in this group permanent.
7245     (unless quit-config
7246       (gnus-run-hooks 'gnus-exit-group-hook)
7247       (gnus-summary-update-info))
7248     (gnus-close-group group)
7249     ;; Make sure where we were, and go to next newsgroup.
7250     (when (buffer-live-p (get-buffer gnus-group-buffer))
7251       (set-buffer gnus-group-buffer))
7252     (unless quit-config
7253       (gnus-group-jump-to-group group))
7254     (gnus-run-hooks 'gnus-summary-exit-hook)
7255     (unless (or quit-config
7256                 (not gnus-summary-next-group-on-exit)
7257                 ;; If this group has disappeared from the summary
7258                 ;; buffer, don't skip forwards.
7259                 (not (string= group (gnus-group-group-name))))
7260       (gnus-group-next-unread-group 1))
7261     (setq group-point (point))
7262     (if temporary
7263         nil                             ;Nothing to do.
7264       (set-buffer buf)
7265       (if (not gnus-kill-summary-on-exit)
7266           (progn
7267             (gnus-deaden-summary)
7268             (setq mode nil))
7269         (when (get-buffer gnus-article-buffer)
7270           (bury-buffer gnus-article-buffer))
7271         ;; Return to group mode buffer.
7272         (when (eq mode 'gnus-summary-mode)
7273           (gnus-kill-buffer buf)))
7274
7275       (setq gnus-current-select-method gnus-select-method)
7276       (when (gnus-buffer-live-p gnus-group-buffer)
7277         (set-buffer gnus-group-buffer))
7278       (if quit-config
7279           (gnus-handle-ephemeral-exit quit-config)
7280         (goto-char group-point)
7281         ;; If gnus-group-buffer is already displayed, make sure we also move
7282         ;; the cursor in the window that displays it.
7283         (let ((win (get-buffer-window (current-buffer) 0)))
7284           (if win (set-window-point win (point))))
7285         (unless leave-hidden
7286           (gnus-configure-windows 'group 'force)))
7287
7288       ;; If we have several article buffers, we kill them at exit.
7289       (unless single-article-buffer
7290         (when (gnus-buffer-live-p article-buffer)
7291           (with-current-buffer article-buffer
7292             ;; Don't kill sticky article buffers
7293             (unless (eq major-mode 'gnus-sticky-article-mode)
7294               (gnus-kill-buffer article-buffer)
7295               (setq gnus-article-current nil))))
7296         (gnus-kill-buffer original-article-buffer))
7297
7298       ;; Clear the current group name.
7299       (unless quit-config
7300         (setq gnus-newsgroup-name nil)))))
7301
7302 (declare-function gnus-article-stop-animations "gnus-art" ())
7303 (declare-function gnus-stop-downloads "gnus-art" ())
7304
7305 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7306 (defun gnus-summary-exit-no-update (&optional no-questions)
7307   "Quit reading current newsgroup without updating read article info."
7308   (interactive)
7309   (let* ((group gnus-newsgroup-name)
7310          (gnus-group-is-exiting-p t)
7311          (gnus-group-is-exiting-without-update-p t)
7312          (quit-config (gnus-group-quit-config group)))
7313     (when (or no-questions
7314               gnus-expert-user
7315               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7316       (gnus-async-halt-prefetch)
7317       (run-hooks 'gnus-summary-prepare-exit-hook)
7318       (when (gnus-buffer-live-p gnus-article-buffer)
7319         (with-current-buffer gnus-article-buffer
7320           (gnus-article-stop-animations)
7321           (gnus-stop-downloads)
7322           (mm-destroy-parts gnus-article-mime-handles)
7323           ;; Set it to nil for safety reason.
7324           (setq gnus-article-mime-handle-alist nil)
7325           (setq gnus-article-mime-handles nil)))
7326       ;; If we have several article buffers, we kill them at exit.
7327       (unless gnus-single-article-buffer
7328         (gnus-kill-buffer gnus-article-buffer)
7329         (gnus-kill-buffer gnus-original-article-buffer)
7330         (setq gnus-article-current nil))
7331       ;; Return to the group buffer.
7332       (if (not gnus-kill-summary-on-exit)
7333           (progn
7334             (gnus-deaden-summary)
7335             (gnus-configure-windows 'group 'force))
7336         (gnus-configure-windows 'group 'force)
7337         (gnus-close-group group)
7338         (gnus-kill-buffer gnus-summary-buffer))
7339       (unless gnus-single-article-buffer
7340         (setq gnus-article-current nil))
7341       (when gnus-use-trees
7342         (gnus-tree-close group))
7343       (gnus-async-prefetch-remove-group group)
7344       (when (get-buffer gnus-article-buffer)
7345         (bury-buffer gnus-article-buffer))
7346       ;; Clear the current group name.
7347       (setq gnus-newsgroup-name nil)
7348       (unless (gnus-ephemeral-group-p group)
7349         (gnus-group-update-group group nil t))
7350       (when (equal (gnus-group-group-name) group)
7351         (gnus-group-next-unread-group 1))
7352       (when quit-config
7353         (gnus-handle-ephemeral-exit quit-config)))))
7354
7355 (defun gnus-handle-ephemeral-exit (quit-config)
7356   "Handle movement when leaving an ephemeral group.
7357 The state which existed when entering the ephemeral is reset."
7358   (if (not (buffer-live-p (car quit-config)))
7359       (when (gnus-buffer-live-p gnus-group-buffer)
7360         (gnus-configure-windows 'group 'force))
7361     (set-buffer (car quit-config))
7362     (unless (eq (cdr quit-config) 'group)
7363       (setq gnus-current-select-method
7364             (gnus-find-method-for-group gnus-newsgroup-name)))
7365     (cond ((eq major-mode 'gnus-summary-mode)
7366            (gnus-set-global-variables))
7367           ((eq major-mode 'gnus-article-mode)
7368            (save-current-buffer
7369              ;; The `gnus-summary-buffer' variable may point
7370              ;; to the old summary buffer when using a single
7371              ;; article buffer.
7372              (unless (gnus-buffer-live-p gnus-summary-buffer)
7373                (set-buffer gnus-group-buffer))
7374              (set-buffer gnus-summary-buffer)
7375              (gnus-set-global-variables))))
7376     (if (or (eq (cdr quit-config) 'article)
7377             (eq (cdr quit-config) 'pick))
7378         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7379             (gnus-configure-windows 'pick 'force)
7380           (gnus-configure-windows (cdr quit-config) 'force))
7381       (gnus-configure-windows (cdr quit-config) 'force))
7382     (when (eq major-mode 'gnus-summary-mode)
7383       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7384                                                      next-unread-noselect))
7385           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7386                                   'next-noselect)
7387                               (gnus-summary-next-subject 1 nil t))
7388                              ((eq gnus-auto-select-on-ephemeral-exit
7389                                   'next-unread-noselect)
7390                               (gnus-summary-next-subject 1 t t))))
7391             ;; Hide the article buffer which displays the article different
7392             ;; from the one that the cursor points to in the summary buffer.
7393             (gnus-configure-windows 'summary 'force))
7394         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7395                (gnus-summary-next-subject 1))
7396               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7397                (gnus-summary-next-subject 1 t))))
7398       (gnus-summary-recenter)
7399       (gnus-summary-position-point))))
7400
7401 ;;; Dead summaries.
7402
7403 (defvar gnus-dead-summary-mode-map
7404   (let ((map (make-keymap)))
7405     (suppress-keymap map)
7406     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7407     (dolist (key '("\C-d" "\r" "\177" [delete]))
7408       (define-key map key 'gnus-summary-wake-up-the-dead))
7409     (dolist (key '("q" "Q"))
7410       (define-key map key 'bury-buffer))
7411     map))
7412
7413 (define-minor-mode gnus-dead-summary-mode
7414   "Minor mode for Gnus summary buffers."
7415   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7416   (unless (derived-mode-p 'gnus-summary-mode)
7417     (setq gnus-dead-summary-mode nil)))
7418
7419 (defun gnus-deaden-summary ()
7420   "Make the current summary buffer into a dead summary buffer."
7421   ;; Kill any previous dead summary buffer.
7422   (when (and gnus-dead-summary
7423              (buffer-name gnus-dead-summary))
7424     (with-current-buffer gnus-dead-summary
7425       (when gnus-dead-summary-mode
7426         (kill-buffer (current-buffer)))))
7427   ;; Make this the current dead summary.
7428   (setq gnus-dead-summary (current-buffer))
7429   (gnus-dead-summary-mode 1)
7430   (let ((name (buffer-name)))
7431     (when (string-match "Summary" name)
7432       (rename-buffer
7433        (concat (substring name 0 (match-beginning 0)) "Dead "
7434                (substring name (match-beginning 0)))
7435        t)
7436       (bury-buffer))))
7437
7438 (defun gnus-kill-or-deaden-summary (buffer)
7439   "Kill or deaden the summary BUFFER."
7440   (save-excursion
7441     (when (and (buffer-name buffer)
7442                (not gnus-single-article-buffer))
7443       (with-current-buffer buffer
7444         (gnus-kill-buffer gnus-article-buffer)
7445         (gnus-kill-buffer gnus-original-article-buffer)))
7446     (cond
7447      ;; Kill the buffer.
7448      (gnus-kill-summary-on-exit
7449       (when (and gnus-use-trees
7450                  (gnus-buffer-exists-p buffer))
7451         (with-current-buffer buffer
7452           (gnus-tree-close gnus-newsgroup-name)))
7453       (gnus-kill-buffer buffer))
7454      ;; Deaden the buffer.
7455      ((gnus-buffer-exists-p buffer)
7456       (with-current-buffer buffer
7457         (gnus-deaden-summary))))))
7458
7459 (defun gnus-summary-wake-up-the-dead (&rest args)
7460   "Wake up the dead summary buffer."
7461   (interactive)
7462   (gnus-dead-summary-mode -1)
7463   (let ((name (buffer-name)))
7464     (when (string-match "Dead " name)
7465       (rename-buffer
7466        (concat (substring name 0 (match-beginning 0))
7467                (substring name (match-end 0)))
7468        t)))
7469   (gnus-message 3 "This dead summary is now alive again"))
7470
7471 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7472 (defun gnus-summary-describe-group (&optional force)
7473   "Describe the current newsgroup."
7474   (interactive "P")
7475   (gnus-group-describe-group force gnus-newsgroup-name))
7476
7477 (defun gnus-summary-describe-briefly ()
7478   "Describe summary mode commands briefly."
7479   (interactive)
7480   (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")))
7481
7482 ;; Walking around group mode buffer from summary mode.
7483
7484 (defun gnus-summary-next-group (&optional no-article target-group backward)
7485   "Exit current newsgroup and then select next unread newsgroup.
7486 If prefix argument NO-ARTICLE is non-nil, no article is selected
7487 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7488 previous group instead."
7489   (interactive "P")
7490   ;; Stop pre-fetching.
7491   (gnus-async-halt-prefetch)
7492   (let ((current-group gnus-newsgroup-name)
7493         (current-buffer (current-buffer))
7494         entered)
7495     ;; First we semi-exit this group to update Xrefs and all variables.
7496     ;; We can't do a real exit, because the window conf must remain
7497     ;; the same in case the user is prompted for info, and we don't
7498     ;; want the window conf to change before that...
7499     (gnus-summary-exit t)
7500     (while (not entered)
7501       ;; Then we find what group we are supposed to enter.
7502       (set-buffer gnus-group-buffer)
7503       (gnus-group-jump-to-group current-group)
7504       (setq target-group
7505             (or target-group
7506                 (if (eq gnus-keep-same-level 'best)
7507                     (gnus-summary-best-group gnus-newsgroup-name)
7508                   (gnus-summary-search-group backward gnus-keep-same-level))))
7509       (if (not target-group)
7510           ;; There are no further groups, so we return to the group
7511           ;; buffer.
7512           (progn
7513             (gnus-message 5 "Returning to the group buffer")
7514             (setq entered t)
7515             (when (gnus-buffer-live-p current-buffer)
7516               (set-buffer current-buffer)
7517               (gnus-summary-exit))
7518             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7519         ;; We try to enter the target group.
7520         (gnus-group-jump-to-group target-group)
7521         (let ((unreads (gnus-group-group-unread)))
7522           (if (and (or (eq t unreads)
7523                        (and unreads (not (zerop unreads))))
7524                    (gnus-summary-read-group
7525                     target-group nil no-article
7526                     (and (buffer-name current-buffer) current-buffer)
7527                     nil backward))
7528               (setq entered t)
7529             (setq current-group target-group
7530                   target-group nil)))))))
7531
7532 (defun gnus-summary-prev-group (&optional no-article)
7533   "Exit current newsgroup and then select previous unread newsgroup.
7534 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7535   (interactive "P")
7536   (gnus-summary-next-group no-article nil t))
7537
7538 ;; Walking around summary lines.
7539
7540 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7541   "Go to the first subject satisfying any non-nil constraint.
7542 If UNREAD is non-nil, the article should be unread.
7543 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7544 If UNSEEN is non-nil, the article should be unseen as well as unread.
7545 Returns the article selected or nil if there are no matching articles."
7546   (interactive "P")
7547   (cond
7548    ;; Empty summary.
7549    ((null gnus-newsgroup-data)
7550     (gnus-message 3 "No articles in the group")
7551     nil)
7552    ;; Pick the first article.
7553    ((not (or unread undownloaded unseen))
7554     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7555     (gnus-data-number (car gnus-newsgroup-data)))
7556    ;; Find the first unread article.
7557    (t
7558     (let ((data gnus-newsgroup-data))
7559       (while (and data
7560                   (let ((num (gnus-data-number (car data))))
7561                     (or (memq num gnus-newsgroup-unfetched)
7562                         (not (or (and unread
7563                                       (memq num gnus-newsgroup-unreads))
7564                                  (and undownloaded
7565                                       (memq num gnus-newsgroup-undownloaded))
7566                                  (and unseen
7567                                       (memq num gnus-newsgroup-unseen)
7568                                       (memq num gnus-newsgroup-unreads)))))))
7569         (setq data (cdr data)))
7570       (prog1
7571           (if data
7572               (progn
7573                 (goto-char (gnus-data-pos (car data)))
7574                 (gnus-data-number (car data)))
7575             (gnus-message 3 "No more%s articles"
7576                           (let* ((r (when unread " unread"))
7577                                  (d (when undownloaded " undownloaded"))
7578                                  (s (when unseen " unseen"))
7579                                  (l (delq nil (list r d s))))
7580                             (cond ((= 3 (length l))
7581                                    (concat r "," d ", or" s))
7582                                   ((= 2 (length l))
7583                                    (concat (car l) ", or" (cadr l)))
7584                                   ((= 1 (length l))
7585                                    (car l))
7586                                   (t
7587                                    ""))))
7588             nil
7589             )
7590         (gnus-summary-position-point))))))
7591
7592 (defun gnus-summary-next-subject (n &optional unread dont-display)
7593   "Go to next N'th summary line.
7594 If N is negative, go to the previous N'th subject line.
7595 If UNREAD is non-nil, only unread articles are selected.
7596 The difference between N and the actual number of steps taken is
7597 returned."
7598   (interactive "p")
7599   (let ((backward (< n 0))
7600         (n (abs n)))
7601     (while (and (> n 0)
7602                 (if backward
7603                     (gnus-summary-find-prev unread)
7604                   (gnus-summary-find-next unread)))
7605       (unless (zerop (setq n (1- n)))
7606         (gnus-summary-show-thread)))
7607     (when (/= 0 n)
7608       (gnus-message 7 "No more%s articles"
7609                     (if unread " unread" "")))
7610     (unless dont-display
7611       (gnus-summary-recenter)
7612       (gnus-summary-position-point))
7613     n))
7614
7615 (defun gnus-summary-next-unread-subject (n)
7616   "Go to next N'th unread summary line."
7617   (interactive "p")
7618   (gnus-summary-next-subject n t))
7619
7620 (defun gnus-summary-prev-subject (n &optional unread)
7621   "Go to previous N'th summary line.
7622 If optional argument UNREAD is non-nil, only unread article is selected."
7623   (interactive "p")
7624   (gnus-summary-next-subject (- n) unread))
7625
7626 (defun gnus-summary-prev-unread-subject (n)
7627   "Go to previous N'th unread summary line."
7628   (interactive "p")
7629   (gnus-summary-next-subject (- n) t))
7630
7631 (defun gnus-summary-goto-subjects (articles)
7632   "Insert the subject header for ARTICLES in the current buffer."
7633   (save-excursion
7634     (dolist (article articles)
7635       (gnus-summary-goto-subject article t)))
7636   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7637   (gnus-summary-position-point))
7638
7639 (defun gnus-summary-goto-subject (article &optional force silent)
7640   "Go to the subject line of ARTICLE.
7641 If FORCE, also allow jumping to articles not currently shown."
7642   (interactive "nArticle number: ")
7643   (unless (numberp article)
7644     (error "Article %s is not a number" article))
7645   (let ((b (point))
7646         (data (gnus-data-find article)))
7647     ;; We read in the article if we have to.
7648     (and (not data)
7649          force
7650          (gnus-summary-insert-subject
7651           article
7652           (if (or (numberp force) (vectorp force)) force)
7653           t)
7654          (setq data (gnus-data-find article)))
7655     (goto-char b)
7656     (if (not data)
7657         (progn
7658           (unless silent
7659             (gnus-message 3 "Can't find article %d" article))
7660           nil)
7661       (let ((pt (gnus-data-pos data)))
7662         (goto-char pt)
7663         (gnus-summary-set-article-display-arrow pt))
7664       (gnus-summary-position-point)
7665       article)))
7666
7667 ;; Walking around summary lines with displaying articles.
7668
7669 (defun gnus-summary-expand-window (&optional arg)
7670   "Make the summary buffer take up the entire Emacs frame.
7671 Given a prefix, will force an `article' buffer configuration."
7672   (interactive "P")
7673   (if arg
7674       (gnus-configure-windows 'article 'force)
7675     (gnus-configure-windows 'summary 'force)))
7676
7677 (defun gnus-summary-display-article (article &optional all-header)
7678   "Display ARTICLE in article buffer."
7679   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7680                (with-current-buffer gnus-article-buffer
7681                  (eq major-mode 'gnus-article-mode)))
7682     (gnus-article-setup-buffer))
7683   (gnus-set-global-variables)
7684   (with-current-buffer gnus-article-buffer
7685     (setq gnus-article-charset gnus-newsgroup-charset)
7686     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7687     (mm-enable-multibyte))
7688   (if (null article)
7689       nil
7690     (prog1
7691         (if gnus-summary-display-article-function
7692             (funcall gnus-summary-display-article-function article all-header)
7693           (gnus-article-prepare article all-header))
7694       (gnus-run-hooks 'gnus-select-article-hook)
7695       (when (and gnus-current-article
7696                  (not (zerop gnus-current-article)))
7697         (gnus-summary-goto-subject gnus-current-article))
7698       (gnus-summary-recenter)
7699       (when (and gnus-use-trees gnus-show-threads)
7700         (gnus-possibly-generate-tree article)
7701         (gnus-highlight-selected-tree article))
7702       ;; Successfully display article.
7703       (gnus-article-set-window-start
7704        (cdr (assq article gnus-newsgroup-bookmarks))))))
7705
7706 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7707   "Select the current article.
7708 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7709 non-nil, the article will be re-fetched even if it already present in
7710 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7711 be displayed."
7712   ;; Make sure we are in the summary buffer to work around bbdb bug.
7713   (unless (eq major-mode 'gnus-summary-mode)
7714     (set-buffer gnus-summary-buffer))
7715   (let ((article (or article (gnus-summary-article-number)))
7716         (all-headers (not (not all-headers))) ;Must be t or nil.
7717         gnus-summary-display-article-function)
7718     (and (not pseudo)
7719          (gnus-summary-article-pseudo-p article)
7720          (error "This is a pseudo-article"))
7721     (with-current-buffer gnus-summary-buffer
7722       (if (or (and gnus-single-article-buffer
7723                    (or (null gnus-current-article)
7724                        (null gnus-article-current)
7725                        (null (get-buffer gnus-article-buffer))
7726                        (not (eq article (cdr gnus-article-current)))
7727                        (not (equal (car gnus-article-current)
7728                                    gnus-newsgroup-name))
7729                        (not (get-buffer gnus-original-article-buffer))))
7730               (and (not gnus-single-article-buffer)
7731                    (or (null gnus-current-article)
7732                        (not (get-buffer gnus-original-article-buffer))
7733                        (not (eq gnus-current-article article))))
7734               force)
7735           ;; The requested article is different from the current article.
7736           (progn
7737             (gnus-summary-display-article article all-headers)
7738             (when (gnus-buffer-live-p gnus-article-buffer)
7739               (with-current-buffer gnus-article-buffer
7740                 (if (not gnus-article-decoded-p) ;; a local variable
7741                     (mm-disable-multibyte))))
7742             (gnus-article-set-window-start
7743              (cdr (assq article gnus-newsgroup-bookmarks)))
7744             article)
7745         'old))))
7746
7747 (defun gnus-summary-force-verify-and-decrypt ()
7748   "Display buttons for signed/encrypted parts and verify/decrypt them."
7749   (interactive)
7750   (let ((mm-verify-option 'known)
7751         (mm-decrypt-option 'known)
7752         (gnus-article-emulate-mime t)
7753         (gnus-buttonized-mime-types (append (list "multipart/signed"
7754                                                   "multipart/encrypted")
7755                                             gnus-buttonized-mime-types)))
7756     (gnus-summary-select-article nil 'force)))
7757
7758 (defun gnus-summary-next-article (&optional unread subject backward push)
7759   "Select the next article.
7760 If UNREAD, only unread articles are selected.
7761 If SUBJECT, only articles with SUBJECT are selected.
7762 If BACKWARD, the previous article is selected instead of the next."
7763   (interactive "P")
7764   ;; Make sure we are in the summary buffer.
7765   (unless (eq major-mode 'gnus-summary-mode)
7766     (set-buffer gnus-summary-buffer))
7767   (cond
7768    ;; Is there such an article?
7769    ((and (gnus-summary-search-forward unread subject backward)
7770          (or (gnus-summary-display-article (gnus-summary-article-number))
7771              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7772     (gnus-summary-position-point))
7773    ;; If not, we try the first unread, if that is wanted.
7774    ((and subject
7775          gnus-auto-select-same
7776          (gnus-summary-first-unread-article))
7777     (gnus-summary-position-point)
7778     (gnus-message 6 "Wrapped"))
7779    ;; Try to get next/previous article not displayed in this group.
7780    ((and gnus-auto-extend-newsgroup
7781          (not unread) (not subject))
7782     (gnus-summary-goto-article
7783      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7784      nil (count-lines (point-min) (point))))
7785    ;; Go to next/previous group.
7786    (t
7787     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7788       (gnus-summary-jump-to-group gnus-newsgroup-name))
7789     (let ((cmd (if (featurep 'xemacs)
7790                    last-command-char
7791                  last-command-event))
7792           (point
7793            (with-current-buffer gnus-group-buffer
7794              (point)))
7795           (current-summary (current-buffer))
7796           (group
7797            (if (eq gnus-keep-same-level 'best)
7798                (gnus-summary-best-group gnus-newsgroup-name)
7799              (gnus-summary-search-group backward gnus-keep-same-level))))
7800       ;; Select next unread newsgroup automagically.
7801       (cond
7802        ((or (not gnus-auto-select-next)
7803             (not cmd))
7804         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7805        ((or (eq gnus-auto-select-next 'quietly)
7806             (and (eq gnus-auto-select-next 'slightly-quietly)
7807                  push)
7808             (and (eq gnus-auto-select-next 'almost-quietly)
7809                  (gnus-summary-last-article-p)))
7810         ;; Select quietly.
7811         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7812             (gnus-summary-exit)
7813           (gnus-message 7 "No more%s articles (%s)..."
7814                         (if unread " unread" "")
7815                         (if group (concat "selecting " group)
7816                           "exiting"))
7817           (gnus-summary-next-group nil group backward)))
7818        (t
7819         (when (gnus-key-press-event-p last-input-event)
7820           ;; Somehow or other, we may now have selected a different
7821           ;; window.  Make point go back to the summary buffer.
7822           (when (eq current-summary (current-buffer))
7823             ;; FIXME: This burps when get-buffer-window returns nil.
7824             (select-window (get-buffer-window current-summary 0)))
7825           (gnus-summary-walk-group-buffer
7826            gnus-newsgroup-name cmd unread backward point))))))))
7827
7828 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7829   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7830                       (?\C-p (gnus-group-prev-unread-group 1))))
7831         (cursor-in-echo-area t)
7832         keve key group ended prompt)
7833     (with-current-buffer gnus-group-buffer
7834       (goto-char start)
7835       (setq group
7836             (if (eq gnus-keep-same-level 'best)
7837                 (gnus-summary-best-group gnus-newsgroup-name)
7838               (gnus-summary-search-group backward gnus-keep-same-level))))
7839     (while (not ended)
7840       (setq prompt
7841             (format
7842              "No more%s articles%s " (if unread " unread" "")
7843              (if (and group
7844                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7845                  (format " (Type %s for %s [%s])"
7846                          (single-key-description cmd)
7847                          (gnus-group-decoded-name group)
7848                          (gnus-group-unread group))
7849                (format " (Type %s to exit %s)"
7850                        (single-key-description cmd)
7851                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7852       ;; Confirm auto selection.
7853       (setq key (car (setq keve (gnus-read-event-char prompt)))
7854             ended t)
7855       (cond
7856        ((assq key keystrokes)
7857         (let ((obuf (current-buffer)))
7858           (switch-to-buffer gnus-group-buffer)
7859           (when group
7860             (gnus-group-jump-to-group group))
7861           (eval (cadr (assq key keystrokes)))
7862           (setq group (gnus-group-group-name))
7863           (switch-to-buffer obuf))
7864         (setq ended nil))
7865        ((equal key cmd)
7866         (if (or (not group)
7867                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7868             (gnus-summary-exit)
7869           (gnus-summary-next-group nil group backward)))
7870        (t
7871         (push (cdr keve) unread-command-events))))))
7872
7873 (defun gnus-summary-next-unread-article ()
7874   "Select unread article after current one."
7875   (interactive)
7876   (gnus-summary-next-article
7877    (or (not (eq gnus-summary-goto-unread 'never))
7878        (gnus-summary-last-article-p (gnus-summary-article-number)))
7879    (and gnus-auto-select-same
7880         (gnus-summary-article-subject))))
7881
7882 (defun gnus-summary-prev-article (&optional unread subject)
7883   "Select the article before the current one.
7884 If UNREAD is non-nil, only unread articles are selected."
7885   (interactive "P")
7886   (gnus-summary-next-article unread subject t))
7887
7888 (defun gnus-summary-prev-unread-article ()
7889   "Select unread article before current one."
7890   (interactive)
7891   (gnus-summary-prev-article
7892    (or (not (eq gnus-summary-goto-unread 'never))
7893        (gnus-summary-first-article-p (gnus-summary-article-number)))
7894    (and gnus-auto-select-same
7895         (gnus-summary-article-subject))))
7896
7897 (declare-function gnus-article-only-boring-p "gnus-art" ())
7898
7899 (defun gnus-summary-next-page (&optional lines circular stop)
7900   "Show next page of the selected article.
7901 If at the end of the current article, select the next article.
7902 LINES says how many lines should be scrolled up.
7903
7904 If CIRCULAR is non-nil, go to the start of the article instead of
7905 selecting the next article when reaching the end of the current
7906 article.
7907
7908 If STOP is non-nil, just stop when reaching the end of the message.
7909
7910 Also see the variable `gnus-article-skip-boring'."
7911   (interactive "P")
7912   (gnus-set-global-variables)
7913   (let ((article (gnus-summary-article-number))
7914         (article-window (get-buffer-window gnus-article-buffer t))
7915         endp)
7916     ;; If the buffer is empty, we have no article.
7917     (unless article
7918       (error "No article to select"))
7919     (gnus-configure-windows 'article)
7920     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7921         (if (and (eq gnus-summary-goto-unread 'never)
7922                  (not (gnus-summary-last-article-p article)))
7923             (gnus-summary-next-article)
7924           (gnus-summary-next-unread-article))
7925       (if (or (null gnus-current-article)
7926               (null gnus-article-current)
7927               (/= article (cdr gnus-article-current))
7928               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7929           ;; Selected subject is different from current article's.
7930           (gnus-summary-display-article article)
7931         (when article-window
7932           (gnus-eval-in-buffer-window gnus-article-buffer
7933             (setq endp (or (gnus-article-next-page lines)
7934                            (gnus-article-only-boring-p))))
7935           (when endp
7936             (cond ((or stop gnus-summary-stop-at-end-of-message)
7937                    (gnus-message 3 "End of message"))
7938                   (circular
7939                    (gnus-summary-beginning-of-article))
7940                   (lines
7941                    (gnus-message 3 "End of message"))
7942                   ((null lines)
7943                    (if (and (eq gnus-summary-goto-unread 'never)
7944                             (not (gnus-summary-last-article-p article)))
7945                        (gnus-summary-next-article)
7946                      (gnus-summary-next-unread-article))))))))
7947     (gnus-summary-recenter)
7948     (gnus-summary-position-point)))
7949
7950 (defun gnus-summary-prev-page (&optional lines move)
7951   "Show previous page of selected article.
7952 Argument LINES specifies lines to be scrolled down.
7953 If MOVE, move to the previous unread article if point is at
7954 the beginning of the buffer."
7955   (interactive "P")
7956   (let ((article (gnus-summary-article-number))
7957         (article-window (get-buffer-window gnus-article-buffer t))
7958         endp)
7959     (gnus-configure-windows 'article)
7960     (if (or (null gnus-current-article)
7961             (null gnus-article-current)
7962             (/= article (cdr gnus-article-current))
7963             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7964         ;; Selected subject is different from current article's.
7965         (gnus-summary-display-article article)
7966       (gnus-summary-recenter)
7967       (when article-window
7968         (gnus-eval-in-buffer-window gnus-article-buffer
7969           (setq endp (gnus-article-prev-page lines)))
7970         (when (and move endp)
7971           (cond (lines
7972                  (gnus-message 3 "Beginning of message"))
7973                 ((null lines)
7974                  (if (and (eq gnus-summary-goto-unread 'never)
7975                           (not (gnus-summary-first-article-p article)))
7976                      (gnus-summary-prev-article)
7977                    (gnus-summary-prev-unread-article))))))))
7978   (gnus-summary-position-point))
7979
7980 (defun gnus-summary-prev-page-or-article (&optional lines)
7981   "Show previous page of selected article.
7982 Argument LINES specifies lines to be scrolled down.
7983 If at the beginning of the article, go to the next article."
7984   (interactive "P")
7985   (gnus-summary-prev-page lines t))
7986
7987 (defun gnus-summary-scroll-up (lines)
7988   "Scroll up (or down) one line current article.
7989 Argument LINES specifies lines to be scrolled up (or down if negative).
7990 If no article is selected, then the current article will be selected first."
7991   (interactive "p")
7992   (gnus-configure-windows 'article)
7993   (gnus-summary-show-thread)
7994   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7995     (gnus-eval-in-buffer-window gnus-article-buffer
7996       (cond ((> lines 0)
7997              (when (gnus-article-next-page lines)
7998                (gnus-message 3 "End of message")))
7999             ((< lines 0)
8000              (gnus-article-prev-page (- lines))))))
8001   (gnus-summary-recenter)
8002   (gnus-summary-position-point))
8003
8004 (defun gnus-summary-scroll-down (lines)
8005   "Scroll down (or up) one line current article.
8006 Argument LINES specifies lines to be scrolled down (or up if negative).
8007 If no article is selected, then the current article will be selected first."
8008   (interactive "p")
8009   (gnus-summary-scroll-up (- lines)))
8010
8011 (defun gnus-summary-next-same-subject ()
8012   "Select next article which has the same subject as current one."
8013   (interactive)
8014   (gnus-summary-next-article nil (gnus-summary-article-subject)))
8015
8016 (defun gnus-summary-prev-same-subject ()
8017   "Select previous article which has the same subject as current one."
8018   (interactive)
8019   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8020
8021 (defun gnus-summary-next-unread-same-subject ()
8022   "Select next unread article which has the same subject as current one."
8023   (interactive)
8024   (gnus-summary-next-article t (gnus-summary-article-subject)))
8025
8026 (defun gnus-summary-prev-unread-same-subject ()
8027   "Select previous unread article which has the same subject as current one."
8028   (interactive)
8029   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8030
8031 (defun gnus-summary-first-unread-article ()
8032   "Select the first unread article.
8033 Return nil if there are no unread articles."
8034   (interactive)
8035   (prog1
8036       (when (gnus-summary-first-subject t)
8037         (gnus-summary-show-thread)
8038         (gnus-summary-first-subject t)
8039         (gnus-summary-display-article (gnus-summary-article-number)))
8040     (gnus-summary-position-point)))
8041
8042 (defun gnus-summary-first-unread-subject ()
8043   "Place the point on the subject line of the first unread article.
8044 Return nil if there are no unread articles."
8045   (interactive)
8046   (prog1
8047       (when (gnus-summary-first-subject t)
8048         (gnus-summary-show-thread)
8049         (gnus-summary-first-subject t))
8050     (gnus-summary-position-point)))
8051
8052 (defun gnus-summary-first-unseen-subject ()
8053   "Place the point on the subject line of the first unseen article.
8054 Return nil if there are no unseen articles."
8055   (interactive)
8056   (prog1
8057       (when (gnus-summary-first-subject nil nil t)
8058         (gnus-summary-show-thread)
8059         (gnus-summary-first-subject nil nil t))
8060     (gnus-summary-position-point)))
8061
8062 (defun gnus-summary-first-unseen-or-unread-subject ()
8063   "Place the point on the subject line of the first unseen and unread article.
8064 If all article have been seen, on the subject line of the first unread
8065 article."
8066   (interactive)
8067   (prog1
8068       (unless (when (gnus-summary-first-subject nil nil t)
8069                 (gnus-summary-show-thread)
8070                 (gnus-summary-first-subject nil nil t))
8071         (when (gnus-summary-first-subject t)
8072           (gnus-summary-show-thread)
8073           (gnus-summary-first-subject t)))
8074     (gnus-summary-position-point)))
8075
8076 (defun gnus-summary-first-article ()
8077   "Select the first article.
8078 Return nil if there are no articles."
8079   (interactive)
8080   (prog1
8081       (when (gnus-summary-first-subject)
8082         (gnus-summary-show-thread)
8083         (gnus-summary-first-subject)
8084         (gnus-summary-display-article (gnus-summary-article-number)))
8085     (gnus-summary-position-point)))
8086
8087 (defun gnus-summary-best-unread-article (&optional arg)
8088   "Select the unread article with the highest score.
8089 If given a prefix argument, select the next unread article that has a
8090 score higher than the default score."
8091   (interactive "P")
8092   (let ((article (if arg
8093                      (gnus-summary-better-unread-subject)
8094                    (gnus-summary-best-unread-subject))))
8095     (if article
8096         (gnus-summary-goto-article article)
8097       (error "No unread articles"))))
8098
8099 (defun gnus-summary-best-unread-subject ()
8100   "Select the unread subject with the highest score."
8101   (interactive)
8102   (let ((best -1000000)
8103         (data gnus-newsgroup-data)
8104         article score)
8105     (while data
8106       (and (gnus-data-unread-p (car data))
8107            (> (setq score
8108                     (gnus-summary-article-score (gnus-data-number (car data))))
8109               best)
8110            (setq best score
8111                  article (gnus-data-number (car data))))
8112       (setq data (cdr data)))
8113     (when article
8114       (gnus-summary-goto-subject article))
8115     (gnus-summary-position-point)
8116     article))
8117
8118 (defun gnus-summary-better-unread-subject ()
8119   "Select the first unread subject that has a score over the default score."
8120   (interactive)
8121   (let ((data gnus-newsgroup-data)
8122         article score)
8123     (while (and (setq article (gnus-data-number (car data)))
8124                 (or (gnus-data-read-p (car data))
8125                     (not (> (gnus-summary-article-score article)
8126                             gnus-summary-default-score))))
8127       (setq data (cdr data)))
8128     (when article
8129       (gnus-summary-goto-subject article))
8130     (gnus-summary-position-point)
8131     article))
8132
8133 (defun gnus-summary-last-subject ()
8134   "Go to the last displayed subject line in the group."
8135   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8136     (when article
8137       (gnus-summary-goto-subject article))))
8138
8139 (defun gnus-summary-goto-article (article &optional all-headers force)
8140   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8141 If ALL-HEADERS is non-nil, no header lines are hidden.
8142 If FORCE, go to the article even if it isn't displayed.  If FORCE
8143 is a number, it is the line the article is to be displayed on."
8144   (interactive
8145    (list
8146     (gnus-completing-read
8147      "Article number or Message-ID"
8148      (mapcar 'int-to-string gnus-newsgroup-limit))
8149     current-prefix-arg
8150     t))
8151   (prog1
8152       (if (and (stringp article)
8153                (string-match "@\\|%40" article))
8154           (gnus-summary-refer-article article)
8155         (when (stringp article)
8156           (setq article (string-to-number article)))
8157         (if (gnus-summary-goto-subject article force)
8158             (gnus-summary-display-article article all-headers)
8159           (gnus-message 4 "Couldn't go to article %s" article) nil))
8160     (gnus-summary-position-point)))
8161
8162 (defun gnus-summary-goto-last-article ()
8163   "Go to the previously read article."
8164   (interactive)
8165   (prog1
8166       (when gnus-last-article
8167         (gnus-summary-goto-article gnus-last-article nil t))
8168     (gnus-summary-position-point)))
8169
8170 (defun gnus-summary-pop-article (number)
8171   "Pop one article off the history and go to the previous.
8172 NUMBER articles will be popped off."
8173   (interactive "p")
8174   (let (to)
8175     (setq gnus-newsgroup-history
8176           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8177     (if to
8178         (gnus-summary-goto-article (car to) nil t)
8179       (error "Article history empty")))
8180   (gnus-summary-position-point))
8181
8182 ;; Summary commands and functions for limiting the summary buffer.
8183
8184 (defun gnus-summary-limit-to-articles (n)
8185   "Limit the summary buffer to the next N articles.
8186 If not given a prefix, use the process marked articles instead."
8187   (interactive "P")
8188   (prog1
8189       (let ((articles (gnus-summary-work-articles n)))
8190         (setq gnus-newsgroup-processable nil)
8191         (gnus-summary-limit articles))
8192     (gnus-summary-position-point)))
8193
8194 (defun gnus-summary-pop-limit (&optional total)
8195   "Restore the previous limit.
8196 If given a prefix, remove all limits."
8197   (interactive "P")
8198   (when total
8199     (setq gnus-newsgroup-limits
8200           (list (mapcar (lambda (h) (mail-header-number h))
8201                         gnus-newsgroup-headers))))
8202   (unless gnus-newsgroup-limits
8203     (error "No limit to pop"))
8204   (prog1
8205       (gnus-summary-limit nil 'pop)
8206     (gnus-summary-position-point)))
8207
8208 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8209   "Limit the summary buffer to articles that have subjects that match a regexp.
8210 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8211   (interactive
8212    (list (read-string (if current-prefix-arg
8213                           "Exclude subject (regexp): "
8214                         "Limit to subject (regexp): "))
8215          nil current-prefix-arg))
8216   (unless header
8217     (setq header "subject"))
8218   (when (not (equal "" subject))
8219     (prog1
8220         (let ((articles (gnus-summary-find-matching
8221                          (or header "subject") subject 'all nil nil
8222                          not-matching)))
8223           (unless articles
8224             (error "Found no matches for \"%s\"" subject))
8225           (gnus-summary-limit articles))
8226       (gnus-summary-position-point))))
8227
8228 (defun gnus-summary-limit-to-author (from &optional not-matching)
8229   "Limit the summary buffer to articles that have authors that match a regexp.
8230 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8231   (interactive
8232    (list (let* ((header (gnus-summary-article-header))
8233                 (default (and header (car (mail-header-parse-address
8234                                            (mail-header-from header))))))
8235            (read-string (concat (if current-prefix-arg
8236                                     "Exclude author (regexp"
8237                                   "Limit to author (regexp")
8238                                 (if default
8239                                     (concat ", default \"" default "\"): ")
8240                                   "): "))
8241                         nil nil
8242                         default))
8243          current-prefix-arg))
8244   (gnus-summary-limit-to-subject from "from" not-matching))
8245
8246 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8247   "Limit the summary buffer to articles with the given RECIPIENT.
8248
8249 If NOT-MATCHING, exclude RECIPIENT.
8250
8251 To and Cc headers are checked.  You need to include them in
8252 `nnmail-extra-headers'."
8253   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8254   (interactive
8255    (list (read-string (format "%s recipient (regexp): "
8256                               (if current-prefix-arg "Exclude" "Limit to")))
8257          current-prefix-arg))
8258   (when (not (equal "" recipient))
8259     (prog1 (let* ((to
8260                    (if (memq 'To nnmail-extra-headers)
8261                        (gnus-summary-find-matching
8262                         (cons 'extra 'To) recipient 'all nil nil
8263                         not-matching)
8264                      (gnus-message
8265                       1 "`To' isn't present in `nnmail-extra-headers'")
8266                      (sit-for 1)
8267                      nil))
8268                   (cc
8269                    (if (memq 'Cc nnmail-extra-headers)
8270                        (gnus-summary-find-matching
8271                         (cons 'extra 'Cc) recipient 'all nil nil
8272                         not-matching)
8273                      (gnus-message
8274                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8275                      (sit-for 1)
8276                      nil))
8277                   (articles
8278                    (if not-matching
8279                        ;; We need the numbers that are in both lists:
8280                        (mapcar (lambda (a)
8281                                  (and (memq a to) a))
8282                                cc)
8283                      (nconc to cc))))
8284              (unless articles
8285                (error "Found no matches for \"%s\"" recipient))
8286              (gnus-summary-limit articles))
8287       (gnus-summary-position-point))))
8288
8289 (defun gnus-summary-limit-to-address (address &optional not-matching)
8290   "Limit the summary buffer to articles with the given ADDRESS.
8291
8292 If NOT-MATCHING, exclude ADDRESS.
8293
8294 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8295 in `nnmail-extra-headers'."
8296   (interactive
8297    (list (read-string (format "%s address (regexp): "
8298                               (if current-prefix-arg "Exclude" "Limit to")))
8299          current-prefix-arg))
8300   (when (not (equal "" address))
8301     (prog1 (let* ((to
8302                    (if (memq 'To nnmail-extra-headers)
8303                        (gnus-summary-find-matching
8304                         (cons 'extra 'To) address 'all nil nil
8305                         not-matching)
8306                      (gnus-message
8307                       1 "`To' isn't present in `nnmail-extra-headers'")
8308                      (sit-for 1)
8309                      t))
8310                   (cc
8311                    (if (memq 'Cc nnmail-extra-headers)
8312                        (gnus-summary-find-matching
8313                         (cons 'extra 'Cc) address 'all nil nil
8314                         not-matching)
8315                      (gnus-message
8316                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8317                      (sit-for 1)
8318                      t))
8319                   (from
8320                    (gnus-summary-find-matching "from" address
8321                                                'all nil nil not-matching))
8322                   (articles
8323                    (if not-matching
8324                        ;; We need the numbers that are in all lists:
8325                        (if (eq cc t)
8326                            (if (eq to t)
8327                                from
8328                              (mapcar (lambda (a) (car (memq a from))) to))
8329                          (if (eq to t)
8330                              (mapcar (lambda (a) (car (memq a from))) cc)
8331                            (mapcar (lambda (a) (car (memq a from)))
8332                                    (mapcar (lambda (a) (car (memq a to)))
8333                                            cc))))
8334                      (nconc (if (eq to t) nil to)
8335                             (if (eq cc t) nil cc)
8336                             from))))
8337              (unless articles
8338                (error "Found no matches for \"%s\"" address))
8339              (gnus-summary-limit articles))
8340       (gnus-summary-position-point))))
8341
8342 (defun gnus-summary-limit-strange-charsets-predicate (header)
8343   (when (fboundp 'char-charset)
8344     (let ((string (concat (mail-header-subject header)
8345                           (mail-header-from header)))
8346           charset found)
8347       (dotimes (i (1- (length string)))
8348         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8349         (when (string-match "unicode\\|big\\|japanese" charset)
8350           (setq found t)))
8351       found)))
8352
8353 (defun gnus-summary-limit-to-predicate (predicate)
8354   "Limit to articles where PREDICATE returns non-nil.
8355 PREDICATE will be called with the header structures of the
8356 articles."
8357   (let ((articles nil)
8358         (case-fold-search t))
8359     (dolist (header gnus-newsgroup-headers)
8360       (when (funcall predicate header)
8361         (push (mail-header-number header) articles)))
8362     (gnus-summary-limit (nreverse articles))))
8363
8364 (defun gnus-summary-limit-to-age (age &optional younger-p)
8365   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8366 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8367 articles that are younger than AGE days."
8368   (interactive
8369    (let ((younger current-prefix-arg)
8370          (days-got nil)
8371          days)
8372      (while (not days-got)
8373        (setq days (if younger
8374                       (read-string "Limit to articles younger than (in days, older when negative): ")
8375                     (read-string
8376                      "Limit to articles older than (in days, younger when negative): ")))
8377        (when (> (length days) 0)
8378          (setq days (read days)))
8379        (if (numberp days)
8380            (progn
8381              (setq days-got t)
8382              (when (< days 0)
8383                (setq younger (not younger))
8384                (setq days (* days -1))))
8385          (message "Please enter a number.")
8386          (sleep-for 1)))
8387      (list days younger)))
8388   (prog1
8389       (let ((data gnus-newsgroup-data)
8390             (cutoff (days-to-time age))
8391             articles d date is-younger)
8392         (while (setq d (pop data))
8393           (when (and (vectorp (gnus-data-header d))
8394                      (setq date (mail-header-date (gnus-data-header d))))
8395             (setq is-younger (time-less-p
8396                               (time-since (gnus-date-get-time date))
8397                               cutoff))
8398             (when (if younger-p
8399                       is-younger
8400                     (not is-younger))
8401               (push (gnus-data-number d) articles))))
8402         (gnus-summary-limit (nreverse articles)))
8403     (gnus-summary-position-point)))
8404
8405 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8406   "Limit the summary buffer to articles that match an 'extra' header."
8407   (interactive
8408    (let ((header
8409           (intern
8410            (gnus-completing-read
8411             (if current-prefix-arg
8412                 "Exclude extra header"
8413               "Limit extra header")
8414             (mapcar 'symbol-name gnus-extra-headers)
8415             t nil nil
8416             (symbol-name (car gnus-extra-headers))))))
8417      (list header
8418            (read-string (format "%s header %s (regexp): "
8419                                 (if current-prefix-arg "Exclude" "Limit to")
8420                                 header))
8421            current-prefix-arg)))
8422   (when (not (equal "" regexp))
8423     (prog1
8424         (let ((articles (gnus-summary-find-matching
8425                          (cons 'extra header) regexp 'all nil nil
8426                          not-matching)))
8427           (unless articles
8428             (error "Found no matches for \"%s\"" regexp))
8429           (gnus-summary-limit articles))
8430       (gnus-summary-position-point))))
8431
8432 (defun gnus-summary-limit-to-display-predicate ()
8433   "Limit the summary buffer to the predicated in the `display' group parameter."
8434   (interactive)
8435   (unless gnus-newsgroup-display
8436     (error "There is no `display' group parameter"))
8437   (let (articles)
8438     (dolist (gnus-number gnus-newsgroup-articles)
8439       (when (funcall gnus-newsgroup-display)
8440         (push gnus-number articles)))
8441     (gnus-summary-limit articles))
8442   (gnus-summary-position-point))
8443
8444 (defun gnus-summary-limit-to-unread (&optional all)
8445   "Limit the summary buffer to articles that are not marked as read.
8446 If ALL is non-nil, limit strictly to unread articles."
8447   (interactive "P")
8448   (if all
8449       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8450     (gnus-summary-limit-to-marks
8451      ;; Concat all the marks that say that an article is read and have
8452      ;; those removed.
8453      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8454            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8455            gnus-low-score-mark gnus-expirable-mark
8456            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8457            gnus-duplicate-mark)
8458      'reverse)))
8459
8460 (defun gnus-summary-limit-to-headers (match &optional reverse)
8461   "Limit the summary buffer to articles that have headers that match MATCH.
8462 If REVERSE (the prefix), limit to articles that don't match."
8463   (interactive "sMatch headers (regexp): \nP")
8464   (gnus-summary-limit-to-bodies match reverse t))
8465
8466 (declare-function article-goto-body "gnus-art" ())
8467
8468 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8469   "Limit the summary buffer to articles that have bodies that match MATCH.
8470 If REVERSE (the prefix), limit to articles that don't match."
8471   (interactive "sMatch body (regexp): \nP")
8472   (let ((articles nil)
8473         (gnus-select-article-hook nil)  ;Disable hook.
8474         (gnus-article-prepare-hook nil)
8475         (gnus-use-article-prefetch nil)
8476         (gnus-keep-backlog nil)
8477         (gnus-break-pages nil)
8478         (gnus-summary-display-arrow nil)
8479         (gnus-updated-mode-lines nil)
8480         (gnus-auto-center-summary nil)
8481         (gnus-display-mime-function nil))
8482     (dolist (data gnus-newsgroup-data)
8483       (let (gnus-mark-article-hook)
8484         (gnus-summary-select-article t t nil (gnus-data-number data)))
8485       (with-current-buffer gnus-article-buffer
8486         (article-goto-body)
8487         (let* ((case-fold-search t)
8488                (found (if headersp
8489                           (re-search-backward match nil t)
8490                         (re-search-forward match nil t))))
8491           (when (or (and found
8492                          (not reverse))
8493                     (and (not found)
8494                          reverse))
8495             (push (gnus-data-number data) articles)))))
8496     (if (not articles)
8497         (message "No messages matched")
8498       (gnus-summary-limit articles)))
8499   (gnus-summary-position-point))
8500
8501 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8502   "Limit the summary buffer to articles that aren't part on any thread.
8503 If THREADSP (the prefix), limit to articles that are in threads."
8504   (interactive "P")
8505   (let ((articles nil)
8506         thread-articles
8507         threads)
8508     (dolist (thread gnus-newsgroup-threads)
8509       (if (stringp (car thread))
8510           (dolist (thread (cdr thread))
8511             (push thread threads))
8512         (push thread threads)))
8513     (dolist (thread threads)
8514       (setq thread-articles (gnus-articles-in-thread thread))
8515       (when (or (and threadsp
8516                      (> (length thread-articles) 1))
8517                 (and (not threadsp)
8518                      (= (length thread-articles) 1)))
8519         (setq articles (nconc thread-articles articles))))
8520     (if (not articles)
8521         (message "No messages matched")
8522       (gnus-summary-limit articles))
8523     (gnus-summary-position-point)))
8524
8525 (defun gnus-summary-limit-to-replied (&optional unreplied)
8526   "Limit the summary buffer to replied articles.
8527 If UNREPLIED (the prefix), limit to unreplied articles."
8528   (interactive "P")
8529   (if unreplied
8530       (gnus-summary-limit
8531        (gnus-set-difference gnus-newsgroup-articles
8532         gnus-newsgroup-replied))
8533     (gnus-summary-limit gnus-newsgroup-replied))
8534   (gnus-summary-position-point))
8535
8536 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8537   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8538 If REVERSE, limit the summary buffer to articles that are marked
8539 with MARKS.  MARKS can either be a string of marks or a list of marks.
8540 Returns how many articles were removed."
8541   (interactive "sMarks: ")
8542   (gnus-summary-limit-to-marks marks t))
8543
8544 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8545   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8546 If REVERSE (the prefix), limit the summary buffer to articles that are
8547 not marked with MARKS.  MARKS can either be a string of marks or a
8548 list of marks.
8549 Returns how many articles were removed."
8550   (interactive "sMarks: \nP")
8551   (prog1
8552       (let ((data gnus-newsgroup-data)
8553             (marks (if (listp marks) marks
8554                      (append marks nil))) ; Transform to list.
8555             articles)
8556         (while data
8557           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8558                   (memq (gnus-data-mark (car data)) marks))
8559             (push (gnus-data-number (car data)) articles))
8560           (setq data (cdr data)))
8561         (gnus-summary-limit articles))
8562     (gnus-summary-position-point)))
8563
8564 (defun gnus-summary-limit-to-score (score)
8565   "Limit to articles with score at or above SCORE."
8566   (interactive "NLimit to articles with score of at least: ")
8567   (let ((data gnus-newsgroup-data)
8568         articles)
8569     (while data
8570       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8571                 score)
8572         (push (gnus-data-number (car data)) articles))
8573       (setq data (cdr data)))
8574     (prog1
8575         (gnus-summary-limit articles)
8576       (gnus-summary-position-point))))
8577
8578 (defun gnus-summary-limit-to-unseen ()
8579   "Limit to unseen articles."
8580   (interactive)
8581   (prog1
8582       (gnus-summary-limit gnus-newsgroup-unseen)
8583     (gnus-summary-position-point)))
8584
8585 (defun gnus-summary-limit-include-thread (id)
8586   "Display all the hidden articles that is in the thread with ID in it.
8587 When called interactively, ID is the Message-ID of the current
8588 article."
8589   (interactive (list (mail-header-id (gnus-summary-article-header))))
8590   (let ((articles (gnus-articles-in-thread
8591                    (gnus-id-to-thread (gnus-root-id id))))
8592         ;;we REALLY want the whole thread---this prevents cut-threads
8593         ;;from removing the thread we want to include.
8594         (gnus-fetch-old-headers nil)
8595         (gnus-build-sparse-threads nil))
8596     (prog1
8597         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8598       (gnus-summary-limit-include-matching-articles
8599        "subject"
8600        (regexp-quote (gnus-simplify-subject-re
8601                       (mail-header-subject (gnus-id-to-header id)))))
8602       (gnus-summary-position-point))))
8603
8604 (defun gnus-summary-limit-include-matching-articles (header regexp)
8605   "Display all the hidden articles that have HEADERs that match REGEXP."
8606   (interactive (list (read-string "Match on header: ")
8607                      (read-string "Regexp: ")))
8608   (let ((articles (gnus-find-matching-articles header regexp)))
8609     (prog1
8610         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8611       (gnus-summary-position-point))))
8612
8613 (defun gnus-summary-insert-dormant-articles ()
8614   "Insert all the dormant articles for this group into the current buffer."
8615   (interactive)
8616   (let ((gnus-verbose (max 6 gnus-verbose)))
8617     (if (not gnus-newsgroup-dormant)
8618         (gnus-message 3 "No dormant articles for this group")
8619       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8620
8621 (defun gnus-summary-insert-ticked-articles ()
8622   "Insert ticked articles for this group into the current buffer."
8623   (interactive)
8624   (let ((gnus-verbose (max 6 gnus-verbose)))
8625     (if (not gnus-newsgroup-marked)
8626         (gnus-message 3 "No ticked articles for this group")
8627       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8628
8629 (defun gnus-summary-limit-include-dormant ()
8630   "Display all the hidden articles that are marked as dormant.
8631 Note that this command only works on a subset of the articles currently
8632 fetched for this group."
8633   (interactive)
8634   (unless gnus-newsgroup-dormant
8635     (error "There are no dormant articles in this group"))
8636   (prog1
8637       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8638     (gnus-summary-position-point)))
8639
8640 (defun gnus-summary-include-articles (articles)
8641   "Fetch the headers for ARTICLES and then display the summary lines."
8642   (let ((gnus-inhibit-demon t)
8643         (gnus-agent nil)
8644         (gnus-read-all-available-headers t))
8645     (setq gnus-newsgroup-headers
8646           (gnus-merge
8647            'list gnus-newsgroup-headers
8648            (gnus-fetch-headers articles nil t)
8649            'gnus-article-sort-by-number))
8650     (setq gnus-newsgroup-articles
8651           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8652     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8653
8654 (defun gnus-summary-limit-exclude-dormant ()
8655   "Hide all dormant articles."
8656   (interactive)
8657   (prog1
8658       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8659     (gnus-summary-position-point)))
8660
8661 (defun gnus-summary-limit-exclude-childless-dormant ()
8662   "Hide all dormant articles that have no children."
8663   (interactive)
8664   (let ((data (gnus-data-list t))
8665         articles d children)
8666     ;; Find all articles that are either not dormant or have
8667     ;; children.
8668     (while (setq d (pop data))
8669       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8670                 (and (setq children
8671                            (gnus-article-children (gnus-data-number d)))
8672                      (let (found)
8673                        (while children
8674                          (when (memq (car children) articles)
8675                            (setq children nil
8676                                  found t))
8677                          (pop children))
8678                        found)))
8679         (push (gnus-data-number d) articles)))
8680     ;; Do the limiting.
8681     (prog1
8682         (gnus-summary-limit articles)
8683       (gnus-summary-position-point))))
8684
8685 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8686   "Mark all unread excluded articles as read.
8687 If ALL, mark even excluded ticked and dormants as read."
8688   (interactive "P")
8689   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8690   (let ((articles (gnus-sorted-ndifference
8691                    (sort
8692                     (mapcar (lambda (h) (mail-header-number h))
8693                             gnus-newsgroup-headers)
8694                     '<)
8695                    gnus-newsgroup-limit))
8696         article)
8697     (setq gnus-newsgroup-unreads
8698           (gnus-sorted-intersection gnus-newsgroup-unreads
8699                                     gnus-newsgroup-limit))
8700     (if all
8701         (setq gnus-newsgroup-dormant nil
8702               gnus-newsgroup-marked nil
8703               gnus-newsgroup-reads
8704               (nconc
8705                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8706                gnus-newsgroup-reads))
8707       (while (setq article (pop articles))
8708         (unless (or (memq article gnus-newsgroup-dormant)
8709                     (memq article gnus-newsgroup-marked))
8710           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8711
8712 (defun gnus-summary-limit (articles &optional pop)
8713   (if pop
8714       ;; We pop the previous limit off the stack and use that.
8715       (setq articles (car gnus-newsgroup-limits)
8716             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8717     ;; We use the new limit, so we push the old limit on the stack.
8718     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8719   ;; Set the limit.
8720   (setq gnus-newsgroup-limit articles)
8721   (let ((total (length gnus-newsgroup-data))
8722         (data (gnus-data-find-list (gnus-summary-article-number)))
8723         (gnus-summary-mark-below nil)   ; Inhibit this.
8724         found)
8725     ;; This will do all the work of generating the new summary buffer
8726     ;; according to the new limit.
8727     (gnus-summary-prepare)
8728     ;; Hide any threads, possibly.
8729     (gnus-summary-maybe-hide-threads)
8730     ;; Try to return to the article you were at, or one in the
8731     ;; neighborhood.
8732     (when data
8733       ;; We try to find some article after the current one.
8734       (while data
8735         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8736           (setq data nil
8737                 found t))
8738         (setq data (cdr data))))
8739     (unless found
8740       ;; If there is no data, that means that we were after the last
8741       ;; article.  The same goes when we can't find any articles
8742       ;; after the current one.
8743       (goto-char (point-max))
8744       (gnus-summary-find-prev))
8745     (gnus-set-mode-line 'summary)
8746     ;; We return how many articles were removed from the summary
8747     ;; buffer as a result of the new limit.
8748     (- total (length gnus-newsgroup-data))))
8749
8750 (defsubst gnus-invisible-cut-children (threads)
8751   (let ((num 0))
8752     (while threads
8753       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8754         (incf num))
8755       (pop threads))
8756     (< num 2)))
8757
8758 (defsubst gnus-cut-thread (thread)
8759   "Go forwards in the thread until we find an article that we want to display."
8760   (when (or (eq gnus-fetch-old-headers 'some)
8761             (eq gnus-fetch-old-headers 'invisible)
8762             (numberp gnus-fetch-old-headers)
8763             (eq gnus-build-sparse-threads 'some)
8764             (eq gnus-build-sparse-threads 'more))
8765     ;; Deal with old-fetched headers and sparse threads.
8766     (while (and
8767             thread
8768             (or
8769              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8770              (gnus-summary-article-ancient-p
8771               (mail-header-number (car thread))))
8772             (if (or (<= (length (cdr thread)) 1)
8773                     (eq gnus-fetch-old-headers 'invisible))
8774                 (setq gnus-newsgroup-limit
8775                       (delq (mail-header-number (car thread))
8776                             gnus-newsgroup-limit)
8777                       thread (cadr thread))
8778               (when (gnus-invisible-cut-children (cdr thread))
8779                 (let ((th (cdr thread)))
8780                   (while th
8781                     (if (memq (mail-header-number (caar th))
8782                               gnus-newsgroup-limit)
8783                         (setq thread (car th)
8784                               th nil)
8785                       (setq th (cdr th))))))))))
8786   thread)
8787
8788 (defun gnus-cut-threads (threads)
8789   "Cut off all uninteresting articles from the beginning of THREADS."
8790   (when (or (eq gnus-fetch-old-headers 'some)
8791             (eq gnus-fetch-old-headers 'invisible)
8792             (numberp gnus-fetch-old-headers)
8793             (eq gnus-build-sparse-threads 'some)
8794             (eq gnus-build-sparse-threads 'more))
8795     (let ((th threads))
8796       (while th
8797         (setcar th (gnus-cut-thread (car th)))
8798         (setq th (cdr th)))))
8799   ;; Remove nixed out threads.
8800   (delq nil threads))
8801
8802 (defun gnus-summary-initial-limit (&optional show-if-empty)
8803   "Figure out what the initial limit is supposed to be on group entry.
8804 This entails weeding out unwanted dormants, low-scored articles,
8805 fetch-old-headers verbiage, and so on."
8806   ;; Most groups have nothing to remove.
8807   (unless (or gnus-inhibit-limiting
8808               (and (null gnus-newsgroup-dormant)
8809                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8810                    (not (eq gnus-fetch-old-headers 'some))
8811                    (not (numberp gnus-fetch-old-headers))
8812                    (not (eq gnus-fetch-old-headers 'invisible))
8813                    (null gnus-summary-expunge-below)
8814                    (not (eq gnus-build-sparse-threads 'some))
8815                    (not (eq gnus-build-sparse-threads 'more))
8816                    (null gnus-thread-expunge-below)))
8817     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8818     (setq gnus-newsgroup-limit nil)
8819     (mapatoms
8820      (lambda (node)
8821        (unless (car (symbol-value node))
8822          ;; These threads have no parents -- they are roots.
8823          (let ((nodes (cdr (symbol-value node)))
8824                thread)
8825            (while nodes
8826              (if (and gnus-thread-expunge-below
8827                       (< (gnus-thread-total-score (car nodes))
8828                          gnus-thread-expunge-below))
8829                  (gnus-expunge-thread (pop nodes))
8830                (setq thread (pop nodes))
8831                (gnus-summary-limit-children thread))))))
8832      gnus-newsgroup-dependencies)
8833     ;; If this limitation resulted in an empty group, we might
8834     ;; pop the previous limit and use it instead.
8835     (when (and (not gnus-newsgroup-limit)
8836                show-if-empty)
8837       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8838     gnus-newsgroup-limit))
8839
8840 (defun gnus-summary-limit-children (thread)
8841   "Return 1 if this subthread is visible and 0 if it is not."
8842   ;; First we get the number of visible children to this thread.  This
8843   ;; is done by recursing down the thread using this function, so this
8844   ;; will really go down to a leaf article first, before slowly
8845   ;; working its way up towards the root.
8846   (when thread
8847     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8848            (children
8849            (if (cdr thread)
8850                (apply '+ (mapcar 'gnus-summary-limit-children
8851                                  (cdr thread)))
8852              0))
8853            (number (mail-header-number (car thread)))
8854            score)
8855       (if (and
8856            (not (memq number gnus-newsgroup-marked))
8857            (or
8858             ;; If this article is dormant and has absolutely no visible
8859             ;; children, then this article isn't visible.
8860             (and (memq number gnus-newsgroup-dormant)
8861                  (zerop children))
8862             ;; If this is "fetch-old-headered" and there is no
8863             ;; visible children, then we don't want this article.
8864             (and (or (eq gnus-fetch-old-headers 'some)
8865                      (numberp gnus-fetch-old-headers))
8866                  (gnus-summary-article-ancient-p number)
8867                  (zerop children))
8868             ;; If this is "fetch-old-headered" and `invisible', then
8869             ;; we don't want this article.
8870             (and (eq gnus-fetch-old-headers 'invisible)
8871                  (gnus-summary-article-ancient-p number))
8872             ;; If this is a sparsely inserted article with no children,
8873             ;; we don't want it.
8874             (and (eq gnus-build-sparse-threads 'some)
8875                  (gnus-summary-article-sparse-p number)
8876                  (zerop children))
8877             ;; If we use expunging, and this article is really
8878             ;; low-scored, then we don't want this article.
8879             (when (and gnus-summary-expunge-below
8880                        (< (setq score
8881                                 (or (cdr (assq number gnus-newsgroup-scored))
8882                                     gnus-summary-default-score))
8883                           gnus-summary-expunge-below))
8884               ;; We increase the expunge-tally here, but that has
8885               ;; nothing to do with the limits, really.
8886               (incf gnus-newsgroup-expunged-tally)
8887               ;; We also mark as read here, if that's wanted.
8888               (when (and gnus-summary-mark-below
8889                          (< score gnus-summary-mark-below))
8890                 (setq gnus-newsgroup-unreads
8891                       (delq number gnus-newsgroup-unreads))
8892                 (if gnus-newsgroup-auto-expire
8893                     (push number gnus-newsgroup-expirable)
8894                   (push (cons number gnus-low-score-mark)
8895                         gnus-newsgroup-reads)))
8896               t)
8897             ;; Do the `display' group parameter.
8898             (and gnus-newsgroup-display
8899                  (let ((gnus-number number))
8900                    (not (funcall gnus-newsgroup-display))))))
8901           ;; Nope, invisible article.
8902           0
8903         ;; Ok, this article is to be visible, so we add it to the limit
8904         ;; and return 1.
8905         (push number gnus-newsgroup-limit)
8906         1))))
8907
8908 (defun gnus-expunge-thread (thread)
8909   "Mark all articles in THREAD as read."
8910   (let* ((number (mail-header-number (car thread))))
8911     (incf gnus-newsgroup-expunged-tally)
8912     ;; We also mark as read here, if that's wanted.
8913     (setq gnus-newsgroup-unreads
8914           (delq number gnus-newsgroup-unreads))
8915     (if gnus-newsgroup-auto-expire
8916         (push number gnus-newsgroup-expirable)
8917       (push (cons number gnus-low-score-mark)
8918             gnus-newsgroup-reads)))
8919   ;; Go recursively through all subthreads.
8920   (mapcar 'gnus-expunge-thread (cdr thread)))
8921
8922 ;; Summary article oriented commands
8923
8924 (defun gnus-summary-refer-parent-article (n)
8925   "Refer parent article N times.
8926 If N is negative, go to ancestor -N instead.
8927 The difference between N and the number of articles fetched is returned."
8928   (interactive "p")
8929   (let ((skip 1)
8930         error header ref)
8931     (when (not (natnump n))
8932       (setq skip (abs n)
8933             n 1))
8934     (while (and (> n 0)
8935                 (not error))
8936       (setq header (gnus-summary-article-header))
8937       (if (and (eq (mail-header-number header)
8938                    (cdr gnus-article-current))
8939                (equal gnus-newsgroup-name
8940                       (car gnus-article-current)))
8941           ;; If we try to find the parent of the currently
8942           ;; displayed article, then we take a look at the actual
8943           ;; References header, since this is slightly more
8944           ;; reliable than the References field we got from the
8945           ;; server.
8946           (with-current-buffer gnus-original-article-buffer
8947             (nnheader-narrow-to-headers)
8948             (unless (setq ref (message-fetch-field "references"))
8949               (when (setq ref (message-fetch-field "in-reply-to"))
8950                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8951             (widen))
8952         (setq ref
8953               ;; It's not the current article, so we take a bet on
8954               ;; the value we got from the server.
8955               (mail-header-references header)))
8956       (if (and ref
8957                (not (equal ref "")))
8958           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8959             (gnus-message 1 "Couldn't find parent"))
8960         (gnus-message 1 "No references in article %d"
8961                       (gnus-summary-article-number))
8962         (setq error t))
8963       (decf n))
8964     (gnus-summary-position-point)
8965     n))
8966
8967 (defun gnus-summary-refer-references ()
8968   "Fetch all articles mentioned in the References header.
8969 Return the number of articles fetched."
8970   (interactive)
8971   (let ((ref (mail-header-references (gnus-summary-article-header)))
8972         (current (gnus-summary-article-number))
8973         (n 0))
8974     (if (or (not ref)
8975             (equal ref ""))
8976         (error "No References in the current article")
8977       ;; For each Message-ID in the References header...
8978       (while (string-match "<[^>]*>" ref)
8979         (incf n)
8980         ;; ... fetch that article.
8981         (gnus-summary-refer-article
8982          (prog1 (match-string 0 ref)
8983            (setq ref (substring ref (match-end 0))))))
8984       (gnus-summary-goto-subject current)
8985       (gnus-summary-position-point)
8986       n)))
8987
8988 (defun gnus-delete-duplicate-headers (headers)
8989   ;; First remove leading duplicates.
8990   (while (and (> (length headers) 1)
8991               (= (mail-header-number (car headers))
8992                  (mail-header-number (cadr headers))))
8993     (pop headers))
8994   ;; Then the rest.
8995   (let ((result headers))
8996     (while (> (length headers) 1)
8997       (if (= (mail-header-number (car headers))
8998              (mail-header-number (cadr headers)))
8999           (setcdr headers (cddr headers))
9000         (pop headers)))
9001     result))
9002
9003 (defun gnus-summary-refer-thread (&optional limit)
9004   "Fetch all articles in the current thread. For backends that
9005 know how to search for threads (currently only 'nnimap) a
9006 non-numeric prefix arg will use nnir to search the entire
9007 server; without a prefix arg only the current group is
9008 searched. If the variable `gnus-refer-thread-use-nnir' is
9009 non-nil the prefix arg has the reverse meaning. If no
9010 backend-specific 'request-thread function is available fetch
9011 LIMIT (the numerical prefix) old headers. If LIMIT is
9012 non-numeric or nil fetch the number specified by the
9013 `gnus-refer-thread-limit' variable."
9014   (interactive "P")
9015   (gnus-warp-to-article)
9016   (let* ((header (gnus-summary-article-header))
9017          (id (mail-header-id header))
9018          (gnus-inhibit-demon t)
9019          (gnus-summary-ignore-duplicates t)
9020          (gnus-read-all-available-headers t)
9021          (gnus-refer-thread-use-nnir
9022           (if (and (not (null limit)) (listp limit))
9023               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9024          (new-headers
9025           (if (gnus-check-backend-function
9026                'request-thread gnus-newsgroup-name)
9027               (gnus-request-thread header gnus-newsgroup-name)
9028             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9029                             gnus-refer-thread-limit))
9030                    (last (if (numberp limit)
9031                              (min (+ (mail-header-number header)
9032                                      limit)
9033                                   gnus-newsgroup-highest)
9034                            gnus-newsgroup-highest))
9035                    (subject (gnus-simplify-subject
9036                              (mail-header-subject header)))
9037                    (refs (split-string (or (mail-header-references header)
9038                                            "")))
9039                    (gnus-parse-headers-hook
9040                     `(lambda () (goto-char (point-min))
9041                       (keep-lines
9042                        (regexp-opt ',(append refs (list id subject)))))))
9043               (gnus-fetch-headers (list last) (if (numberp limit)
9044                                                   (* 2 limit) limit) t))))
9045          article-ids)
9046     (when (listp new-headers)
9047       (dolist (header new-headers)
9048         (push (mail-header-number header) article-ids)
9049         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9050           (push (mail-header-number header) gnus-newsgroup-unreads)
9051           (setq gnus-newsgroup-unselected
9052                 (delete (mail-header-number header)
9053                         gnus-newsgroup-unselected))))
9054       (setq gnus-newsgroup-headers
9055             (gnus-delete-duplicate-headers
9056              (gnus-merge
9057               'list gnus-newsgroup-headers new-headers
9058               'gnus-article-sort-by-number)))
9059       (setq gnus-newsgroup-articles
9060             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9061       (gnus-summary-limit-include-thread id)))
9062   (gnus-summary-show-thread))
9063
9064 (defun gnus-summary-refer-article (message-id)
9065   "Fetch an article specified by MESSAGE-ID."
9066   (interactive "sMessage-ID: ")
9067   (gnus-warp-to-article)
9068   (when (and (stringp message-id)
9069              (not (zerop (length message-id))))
9070     (setq message-id (gnus-replace-in-string message-id " " ""))
9071     ;; Construct the correct Message-ID if necessary.
9072     ;; Suggested by tale@pawl.rpi.edu.
9073     (unless (string-match "^<" message-id)
9074       (setq message-id (concat "<" message-id)))
9075     (unless (string-match ">$" message-id)
9076       (setq message-id (concat message-id ">")))
9077     ;; People often post MIDs from URLs, so unhex it:
9078     (unless (string-match "@" message-id)
9079       (setq message-id (gnus-url-unhex-string message-id)))
9080     (let* ((header (gnus-id-to-header message-id))
9081            (sparse (and header
9082                         (gnus-summary-article-sparse-p
9083                          (mail-header-number header))
9084                         (memq (mail-header-number header)
9085                               gnus-newsgroup-limit)))
9086            number)
9087       (cond
9088        ;; If the article is present in the buffer we just go to it.
9089        ((and header
9090              (or (not (gnus-summary-article-sparse-p
9091                        (mail-header-number header)))
9092                  sparse))
9093         (prog1
9094             (gnus-summary-goto-article
9095              (mail-header-number header) nil t)
9096           (when sparse
9097             (gnus-summary-update-article (mail-header-number header)))))
9098        (t
9099         ;; We fetch the article.
9100         (catch 'found
9101           (dolist (gnus-override-method (gnus-refer-article-methods))
9102             (when (and (gnus-check-server gnus-override-method)
9103                        ;; Fetch the header,
9104                        (setq number (gnus-summary-insert-subject message-id)))
9105               ;; and display the article.
9106               (gnus-summary-select-article nil nil nil number)
9107               (throw 'found t)))
9108           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9109
9110 (defun gnus-refer-article-methods ()
9111   "Return a list of referable methods."
9112   (cond
9113    ;; No method, so we default to current and native.
9114    ((null gnus-refer-article-method)
9115     (list gnus-current-select-method gnus-select-method))
9116    ;; Current.
9117    ((eq 'current gnus-refer-article-method)
9118     (list gnus-current-select-method))
9119    ;; List of select methods.
9120    ((not (and (symbolp (car gnus-refer-article-method))
9121               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9122     (let (out)
9123       (dolist (method gnus-refer-article-method)
9124         (push (if (eq 'current method)
9125                   gnus-current-select-method
9126                 (if (eq 'nnir (car method))
9127                     (list
9128                      'nnir
9129                      (or (cadr method)
9130                          (gnus-method-to-server gnus-current-select-method)))
9131                   method))
9132               out))
9133       (nreverse out)))
9134    ;; One single select method.
9135    (t
9136     (list gnus-refer-article-method))))
9137
9138 (defun gnus-summary-edit-parameters ()
9139   "Edit the group parameters of the current group."
9140   (interactive)
9141   (gnus-group-edit-group gnus-newsgroup-name 'params))
9142
9143 (defun gnus-summary-customize-parameters ()
9144   "Customize the group parameters of the current group."
9145   (interactive)
9146   (gnus-group-customize gnus-newsgroup-name))
9147
9148 (defun gnus-summary-enter-digest-group (&optional force)
9149   "Enter an nndoc group based on the current article.
9150 If FORCE, force a digest interpretation.  If not, try to guess
9151 what the document format is.
9152
9153 To control what happens when you exit the group, see the
9154 `gnus-auto-select-on-ephemeral-exit' variable."
9155   (interactive "P")
9156   (let ((conf gnus-current-window-configuration))
9157     (save-window-excursion
9158       (save-excursion
9159         (let (gnus-article-prepare-hook
9160               gnus-display-mime-function
9161               gnus-break-pages)
9162           (gnus-summary-select-article))))
9163     (setq gnus-current-window-configuration conf)
9164     (let* ((name (format "%s-%d"
9165                          (gnus-group-prefixed-name
9166                           gnus-newsgroup-name (list 'nndoc ""))
9167                          (with-current-buffer gnus-summary-buffer
9168                            gnus-current-article)))
9169            (ogroup gnus-newsgroup-name)
9170            (params (append (gnus-info-params (gnus-get-info ogroup))
9171                            (list (cons 'to-group ogroup))
9172                            (list (cons 'parent-group ogroup))
9173                            (list (cons 'save-article-group ogroup))))
9174            (case-fold-search t)
9175            (buf (current-buffer))
9176            dig to-address charset)
9177       (with-current-buffer gnus-original-article-buffer
9178         ;; Have the digest group inherit the main mail address of
9179         ;; the parent article.
9180         (when (setq to-address (or (gnus-fetch-field "reply-to")
9181                                    (gnus-fetch-field "from")))
9182           (setq params
9183                 (append
9184                  (list (cons 'to-address
9185                              (funcall gnus-decode-encoded-address-function
9186                                       to-address))))))
9187         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9188         (insert-buffer-substring gnus-original-article-buffer)
9189         (narrow-to-region
9190          (goto-char (point-min))
9191          (or (search-forward "\n\n" nil t) (point)))
9192         ;; Remove lines that may lead nndoc to misinterpret the
9193         ;; document type.
9194         (goto-char (point-min))
9195         (delete-matching-lines "^Path:\\|^From ")
9196         ;; Parse charset, and decode content transfer encoding.
9197         (setq charset (mail-content-type-get
9198                        (mail-header-parse-content-type
9199                         (or (gnus-fetch-field "content-type") ""))
9200                        'charset))
9201         (let ((encoding (gnus-fetch-field "content-transfer-encoding")))
9202           (when encoding
9203             (message-remove-header "content-transfer-encoding")
9204             (goto-char (point-max))
9205             (widen)
9206             (narrow-to-region (point) (point-max))
9207             (mm-decode-content-transfer-encoding
9208              (intern (downcase (mail-header-strip encoding))))))
9209         (widen))
9210       (unwind-protect
9211           (if (let ((gnus-newsgroup-ephemeral-charset
9212                      (if charset
9213                          (intern (downcase (gnus-strip-whitespace charset)))
9214                        gnus-newsgroup-charset))
9215                     (gnus-newsgroup-ephemeral-ignored-charsets
9216                      gnus-newsgroup-ignored-charsets))
9217                 (gnus-group-read-ephemeral-group
9218                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9219                               (nndoc-article-type
9220                                ,(if force 'mbox 'guess)))
9221                  t nil nil nil
9222                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9223                                                         "ADAPT")))))
9224               ;; Make all postings to this group go to the parent group.
9225               (nconc (gnus-info-params (gnus-get-info name))
9226                      params)
9227             ;; Couldn't select this doc group.
9228             (switch-to-buffer buf)
9229             (gnus-set-global-variables)
9230             (gnus-configure-windows 'summary)
9231             (gnus-message 3 "Article couldn't be entered?"))
9232         (kill-buffer dig)))))
9233
9234 (defun gnus-summary-read-document (n)
9235   "Open a new group based on the current article(s).
9236 This will allow you to read digests and other similar
9237 documents as newsgroups.
9238 Obeys the standard process/prefix convention."
9239   (interactive "P")
9240   (let* ((ogroup gnus-newsgroup-name)
9241          (params (append (gnus-info-params (gnus-get-info ogroup))
9242                          (list (cons 'to-group ogroup))))
9243          group egroup groups vgroup)
9244     (dolist (article (gnus-summary-work-articles n))
9245       (setq group (format "%s-%d" gnus-newsgroup-name article))
9246       (gnus-summary-remove-process-mark article)
9247       (when (gnus-summary-display-article article)
9248         (save-excursion ;;What for?
9249           (with-temp-buffer
9250             (insert-buffer-substring gnus-original-article-buffer)
9251             ;; Remove some headers that may lead nndoc to make
9252             ;; the wrong guess.
9253             (message-narrow-to-head)
9254             (goto-char (point-min))
9255             (delete-matching-lines "^Path:\\|^From ")
9256             (widen)
9257             (if (setq egroup
9258                       (gnus-group-read-ephemeral-group
9259                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9260                                      (nndoc-article-type guess))
9261                        t nil t))
9262                 (progn
9263                   ;; Make all postings to this group go to the parent group.
9264                   (nconc (gnus-info-params (gnus-get-info egroup))
9265                          params)
9266                   (push egroup groups))
9267               ;; Couldn't select this doc group.
9268               (gnus-error 3 "Article couldn't be entered"))))))
9269     ;; Now we have selected all the documents.
9270     (cond
9271      ((not groups)
9272       (error "None of the articles could be interpreted as documents"))
9273      ((gnus-group-read-ephemeral-group
9274        (setq vgroup (format
9275                      "nnvirtual:%s-%s" gnus-newsgroup-name
9276                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9277        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9278        t
9279        (cons (current-buffer) 'summary)))
9280      (t
9281       (error "Couldn't select virtual nndoc group")))))
9282
9283 (defun gnus-summary-widget-forward (arg)
9284   "Move point to the next field or button in the article.
9285 With optional ARG, move across that many fields."
9286   (interactive "p")
9287   (gnus-summary-select-article)
9288   (gnus-configure-windows 'article)
9289   (select-window (gnus-get-buffer-window gnus-article-buffer))
9290   (widget-forward arg))
9291
9292 (defun gnus-summary-widget-backward (arg)
9293   "Move point to the previous field or button in the article.
9294 With optional ARG, move across that many fields."
9295   (interactive "p")
9296   (gnus-summary-select-article)
9297   (gnus-configure-windows 'article)
9298   (select-window (gnus-get-buffer-window gnus-article-buffer))
9299   (unless (widget-at (point))
9300     (goto-char (point-max)))
9301   (widget-backward arg))
9302
9303 (defun gnus-summary-isearch-article (&optional regexp-p)
9304   "Do incremental search forward on the current article.
9305 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9306   (interactive "P")
9307   (gnus-summary-select-article)
9308   (gnus-configure-windows 'article)
9309   (gnus-eval-in-buffer-window gnus-article-buffer
9310     (save-restriction
9311       (widen)
9312       (isearch-forward regexp-p))))
9313
9314 (defun gnus-summary-repeat-search-article-forward ()
9315   "Repeat the previous search forwards."
9316   (interactive)
9317   (unless gnus-last-search-regexp
9318     (error "No previous search"))
9319   (gnus-summary-search-article-forward gnus-last-search-regexp))
9320
9321 (defun gnus-summary-repeat-search-article-backward ()
9322   "Repeat the previous search backwards."
9323   (interactive)
9324   (unless gnus-last-search-regexp
9325     (error "No previous search"))
9326   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9327
9328 (defun gnus-summary-search-article-forward (regexp &optional backward)
9329   "Search for an article containing REGEXP forward.
9330 If BACKWARD, search backward instead."
9331   (interactive
9332    (list (read-string
9333           (format "Search article %s (regexp%s): "
9334                   (if current-prefix-arg "backward" "forward")
9335                   (if gnus-last-search-regexp
9336                       (concat ", default " gnus-last-search-regexp)
9337                     "")))
9338          current-prefix-arg))
9339   (if (string-equal regexp "")
9340       (setq regexp (or gnus-last-search-regexp ""))
9341     (setq gnus-last-search-regexp regexp)
9342     (setq gnus-article-before-search gnus-current-article))
9343   ;; Intentionally set gnus-last-article.
9344   (setq gnus-last-article gnus-article-before-search)
9345   (let ((gnus-last-article gnus-last-article))
9346     (if (gnus-summary-search-article regexp backward)
9347         (gnus-summary-show-thread)
9348       (signal 'search-failed (list regexp)))))
9349
9350 (defun gnus-summary-search-article-backward (regexp)
9351   "Search for an article containing REGEXP backward."
9352   (interactive
9353    (list (read-string
9354           (format "Search article backward (regexp%s): "
9355                   (if gnus-last-search-regexp
9356                       (concat ", default " gnus-last-search-regexp)
9357                     "")))))
9358   (gnus-summary-search-article-forward regexp 'backward))
9359
9360 (defun gnus-summary-search-article (regexp &optional backward)
9361   "Search for an article containing REGEXP.
9362 Optional argument BACKWARD means do search for backward.
9363 `gnus-select-article-hook' is not called during the search."
9364   ;; We have to require this here to make sure that the following
9365   ;; dynamic binding isn't shadowed by autoloading.
9366   (require 'gnus-async)
9367   (require 'gnus-art)
9368   (let ((gnus-select-article-hook nil)  ;Disable hook.
9369         (gnus-article-prepare-hook nil)
9370         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9371         (gnus-use-article-prefetch nil)
9372         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9373         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9374         (gnus-visual nil)
9375         (gnus-keep-backlog nil)
9376         (gnus-break-pages nil)
9377         (gnus-summary-display-arrow nil)
9378         (gnus-updated-mode-lines nil)
9379         (gnus-auto-center-summary nil)
9380         (sum (current-buffer))
9381         (gnus-display-mime-function nil)
9382         (found nil)
9383         point)
9384     (gnus-save-hidden-threads
9385       (gnus-summary-select-article)
9386       (set-buffer gnus-article-buffer)
9387       (goto-char (window-point (get-buffer-window (current-buffer))))
9388       (when backward
9389         (forward-line -1))
9390       (while (not found)
9391         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9392         (if (if backward
9393                 (re-search-backward regexp nil t)
9394               (re-search-forward regexp nil t))
9395             ;; We found the regexp.
9396             (progn
9397               (setq found 'found)
9398               (beginning-of-line)
9399               (set-window-start
9400                (get-buffer-window (current-buffer))
9401                (point))
9402               (forward-line 1)
9403               (set-window-point
9404                (get-buffer-window (current-buffer))
9405                (point))
9406               (set-buffer sum)
9407               (setq point (point)))
9408           ;; We didn't find it, so we go to the next article.
9409           (set-buffer sum)
9410           (setq found 'not)
9411           (while (eq found 'not)
9412             (if (not (if backward (gnus-summary-find-prev)
9413                        (gnus-summary-find-next)))
9414                 ;; No more articles.
9415                 (setq found t)
9416               ;; Select the next article and adjust point.
9417               (unless (gnus-summary-article-sparse-p
9418                        (gnus-summary-article-number))
9419                 (setq found nil)
9420                 (gnus-summary-select-article)
9421                 (set-buffer gnus-article-buffer)
9422                 (widen)
9423                 (goto-char (if backward (point-max) (point-min))))))))
9424       (gnus-message 7 ""))
9425     ;; Return whether we found the regexp.
9426     (when (eq found 'found)
9427       (goto-char point)
9428       (gnus-summary-show-thread)
9429       (gnus-summary-goto-subject gnus-current-article)
9430       (gnus-summary-position-point)
9431       t)))
9432
9433 (defun gnus-find-matching-articles (header regexp)
9434   "Return a list of all articles that match REGEXP on HEADER.
9435 This search includes all articles in the current group that Gnus has
9436 fetched headers for, whether they are displayed or not."
9437   (let ((articles nil)
9438         ;; Can't eta-reduce because it's a macro.
9439         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9440         (case-fold-search t))
9441     (dolist (header gnus-newsgroup-headers)
9442       (when (string-match regexp (funcall func header))
9443         (push (mail-header-number header) articles)))
9444     (nreverse articles)))
9445
9446 (defun gnus-summary-find-matching (header regexp &optional backward unread
9447                                           not-case-fold not-matching)
9448   "Return a list of all articles that match REGEXP on HEADER.
9449 The search stars on the current article and goes forwards unless
9450 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9451 If UNREAD is non-nil, only unread articles will
9452 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9453 in the comparisons. If NOT-MATCHING, return a list of all articles that
9454 not match REGEXP on HEADER."
9455   (let ((case-fold-search (not not-case-fold))
9456         articles d func)
9457     (if (consp header)
9458         (if (eq (car header) 'extra)
9459             (setq func
9460                   `(lambda (h)
9461                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9462                          "")))
9463           (error "%s is an invalid header" header))
9464       (unless (fboundp (intern (concat "mail-header-" header)))
9465         (error "%s is not a valid header" header))
9466       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9467     (dolist (d (if (eq backward 'all)
9468                    gnus-newsgroup-data
9469                  (gnus-data-find-list
9470                   (gnus-summary-article-number)
9471                   (gnus-data-list backward))))
9472       (when (and (or (not unread)       ; We want all articles...
9473                      (gnus-data-unread-p d)) ; Or just unreads.
9474                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9475                  (if not-matching
9476                      (not (string-match
9477                            regexp
9478                            (funcall func (gnus-data-header d))))
9479                    (string-match regexp
9480                                  (funcall func (gnus-data-header d)))))
9481         (push (gnus-data-number d) articles))) ; Success!
9482     (nreverse articles)))
9483
9484 (defun gnus-summary-execute-command (header regexp command &optional backward)
9485   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9486 If HEADER is an empty string (or nil), the match is done on the entire
9487 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9488   (interactive
9489    (list (let ((completion-ignore-case t))
9490            (gnus-completing-read
9491             "Header name"
9492             (mapcar 'symbol-name
9493                     (append
9494                      '(Number Subject From Lines Date
9495                        Message-ID Xref References Body)
9496                      gnus-extra-headers))
9497             'require-match))
9498          (read-string "Regexp: ")
9499          (read-key-sequence "Command: ")
9500          current-prefix-arg))
9501   (when (equal header "Body")
9502     (setq header ""))
9503   ;; Hidden thread subtrees must be searched as well.
9504   (gnus-summary-show-all-threads)
9505   ;; We don't want to change current point nor window configuration.
9506   (save-excursion
9507     (save-window-excursion
9508       (let (gnus-visual
9509             gnus-treat-strip-trailing-blank-lines
9510             gnus-treat-strip-leading-blank-lines
9511             gnus-treat-strip-multiple-blank-lines
9512             gnus-treat-hide-boring-headers
9513             gnus-treat-fold-newsgroups
9514             gnus-article-prepare-hook)
9515         (gnus-message 6 "Executing %s..." (key-description command))
9516         ;; We'd like to execute COMMAND interactively so as to give arguments.
9517         (gnus-execute header regexp
9518                       `(call-interactively ',(key-binding command))
9519                       backward)
9520         (gnus-message 6 "Executing %s...done" (key-description command))))))
9521
9522 (defun gnus-summary-beginning-of-article ()
9523   "Scroll the article back to the beginning."
9524   (interactive)
9525   (gnus-summary-select-article)
9526   (gnus-configure-windows 'article)
9527   (gnus-eval-in-buffer-window gnus-article-buffer
9528     (widen)
9529     (goto-char (point-min))
9530     (when gnus-break-pages
9531       (gnus-narrow-to-page))))
9532
9533 (defun gnus-summary-end-of-article ()
9534   "Scroll to the end of the article."
9535   (interactive)
9536   (gnus-summary-select-article)
9537   (gnus-configure-windows 'article)
9538   (gnus-eval-in-buffer-window gnus-article-buffer
9539     (widen)
9540     (goto-char (point-max))
9541     (recenter -3)
9542     (when gnus-break-pages
9543       (gnus-narrow-to-page))))
9544
9545 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9546   "Truncate to LEN and quote all \"(\"'s in STRING."
9547   (gnus-replace-in-string (if (and len (> (length string) len))
9548                               (substring string 0 len)
9549                             string)
9550                           "[()]" "\\\\\\&"))
9551
9552 (defun gnus-summary-print-article (&optional filename n)
9553   "Generate and print a PostScript image of the process-marked (mail) articles.
9554
9555 If used interactively, print the current article if none are
9556 process-marked.  With prefix arg, prompt the user for the name of the
9557 file to save in.
9558
9559 When used from Lisp, accept two optional args FILENAME and N.  N means
9560 to print the next N articles.  If N is negative, print the N previous
9561 articles.  If N is nil and articles have been marked with the process
9562 mark, print these instead.
9563
9564 If the optional first argument FILENAME is nil, send the image to the
9565 printer.  If FILENAME is a string, save the PostScript image in a file with
9566 that name.  If FILENAME is a number, prompt the user for the name of the file
9567 to save in."
9568   (interactive (list (ps-print-preprint current-prefix-arg)))
9569   (dolist (article (gnus-summary-work-articles n))
9570     (gnus-summary-select-article nil nil 'pseudo article)
9571     (gnus-eval-in-buffer-window gnus-article-buffer
9572       (gnus-print-buffer))
9573     (gnus-summary-remove-process-mark article))
9574   (ps-despool filename))
9575
9576 (defun gnus-print-buffer ()
9577   (let ((ps-left-header
9578          (list
9579           (concat "("
9580                   (gnus-summary-print-truncate-and-quote
9581                    (mail-header-subject gnus-current-headers)
9582                    66) ")")
9583           (concat "("
9584                   (gnus-summary-print-truncate-and-quote
9585                    (mail-header-from gnus-current-headers)
9586                    45) ")")))
9587         (ps-right-header
9588          (list
9589           "/pagenumberstring load"
9590           (concat "("
9591                   (mail-header-date gnus-current-headers) ")"))))
9592     (gnus-run-hooks 'gnus-ps-print-hook)
9593     (save-excursion
9594       (if ps-print-color-p
9595           (ps-spool-buffer-with-faces)
9596         (ps-spool-buffer)))))
9597
9598 (declare-function gnus-flush-original-article-buffer "gnus-art" ())
9599
9600 (defun gnus-summary-show-complete-article ()
9601   "Show a complete version of the current article.
9602 This is only useful if you're looking at a partial version of the
9603 article currently."
9604   (interactive)
9605   (let ((gnus-keep-backlog nil)
9606         (gnus-use-cache nil)
9607         (gnus-agent nil)
9608         (variable (intern
9609                    (format "%s-fetch-partial-articles"
9610                            (car (gnus-find-method-for-group
9611                                  gnus-newsgroup-name)))
9612                    obarray))
9613         old-val)
9614     (unwind-protect
9615         (progn
9616           (setq old-val (symbol-value variable))
9617           (set variable nil)
9618           (gnus-flush-original-article-buffer)
9619           (gnus-summary-show-article))
9620       (set variable old-val))))
9621
9622 (defun gnus-summary-show-article (&optional arg)
9623   "Force redisplaying of the current article.
9624 If ARG (the prefix) is a number, show the article with the charset
9625 defined in `gnus-summary-show-article-charset-alist', or the charset
9626 input.
9627 If ARG (the prefix) is non-nil and not a number, show the article,
9628 but without running any of the article treatment functions
9629 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9630 C-u g', show the raw article."
9631   (interactive "P")
9632   (cond
9633    ((numberp arg)
9634     (gnus-summary-show-article t)
9635     (let ((gnus-newsgroup-charset
9636            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9637                (mm-read-coding-system
9638                 "View as charset: " ;; actually it is coding system.
9639                 (with-current-buffer gnus-article-buffer
9640                   (mm-detect-coding-region (point) (point-max))))))
9641           (gnus-newsgroup-ignored-charsets 'gnus-all))
9642       (gnus-summary-select-article nil 'force)
9643       (let ((deps gnus-newsgroup-dependencies)
9644             head header lines)
9645         (with-current-buffer gnus-original-article-buffer
9646           (save-restriction
9647             (message-narrow-to-head)
9648             (setq head (buffer-string))
9649             (goto-char (point-min))
9650             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9651               (goto-char (point-max))
9652               (widen)
9653               (setq lines (1- (count-lines (point) (point-max))))))
9654           (with-temp-buffer
9655             (insert (format "211 %d Article retrieved.\n"
9656                             (cdr gnus-article-current)))
9657             (insert head)
9658             (if lines (insert (format "Lines: %d\n" lines)))
9659             (insert ".\n")
9660             (let ((nntp-server-buffer (current-buffer)))
9661               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9662         (gnus-data-set-header
9663          (gnus-data-find (cdr gnus-article-current))
9664          header)
9665         (gnus-summary-update-article-line
9666          (cdr gnus-article-current) header)
9667         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9668           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9669    ((not arg)
9670     ;; Select the article the normal way.
9671     (if (eq mm-text-html-renderer 'shr)
9672         (progn
9673           (require 'shr)
9674           (let ((shr-ignore-cache t))
9675             (gnus-summary-select-article nil 'force)))
9676       (gnus-summary-select-article nil 'force)))
9677    ((equal arg '(16))
9678     ;; C-u C-u g
9679     (let ((gnus-inhibit-article-treatments t))
9680       (gnus-summary-select-article nil 'force)))
9681    (t
9682     ;; We have to require this here to make sure that the following
9683     ;; dynamic binding isn't shadowed by autoloading.
9684     (require 'gnus-async)
9685     (require 'gnus-art)
9686     ;; Bind the article treatment functions to nil.
9687     (let ((gnus-have-all-headers t)
9688           gnus-article-prepare-hook
9689           gnus-article-decode-hook
9690           gnus-display-mime-function
9691           gnus-break-pages)
9692       ;; Destroy any MIME parts.
9693       (when (gnus-buffer-live-p gnus-article-buffer)
9694         (with-current-buffer gnus-article-buffer
9695           (gnus-article-stop-animations)
9696           (gnus-stop-downloads)
9697           (mm-destroy-parts gnus-article-mime-handles)
9698           ;; Set it to nil for safety reason.
9699           (setq gnus-article-mime-handle-alist nil)
9700           (setq gnus-article-mime-handles nil)))
9701       (gnus-summary-select-article nil 'force))))
9702   (gnus-summary-goto-subject gnus-current-article)
9703   (gnus-summary-position-point))
9704
9705 (defun gnus-summary-show-raw-article ()
9706   "Show the raw article without any article massaging functions being run."
9707   (interactive)
9708   (gnus-summary-show-article t))
9709
9710 (defun gnus-summary-verbose-headers (&optional arg)
9711   "Toggle permanent full header display.
9712 If ARG is a positive number, turn header display on.
9713 If ARG is a negative number, turn header display off."
9714   (interactive "P")
9715   (setq gnus-show-all-headers
9716         (cond ((or (not (numberp arg))
9717                    (zerop arg))
9718                (not gnus-show-all-headers))
9719               ((natnump arg)
9720                t)))
9721   (gnus-summary-show-article))
9722
9723 (declare-function article-narrow-to-head "gnus-art" ())
9724 (declare-function gnus-article-hidden-text-p "gnus-art" (type))
9725 (declare-function gnus-delete-wash-type "gnus-art" (type))
9726
9727 (defun gnus-summary-toggle-header (&optional arg)
9728   "Show the headers if they are hidden, or hide them if they are shown.
9729 If ARG is a positive number, show the entire header.
9730 If ARG is a negative number, hide the unwanted header lines."
9731   (interactive "P")
9732   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9733                      (get-buffer-window gnus-article-buffer t))))
9734     (with-current-buffer gnus-article-buffer
9735       (widen)
9736       (article-narrow-to-head)
9737       (let* ((inhibit-read-only t)
9738              (inhibit-point-motion-hooks t)
9739              (hidden (if (numberp arg)
9740                          (>= arg 0)
9741                        (or (not (looking-at "[^ \t\n]+:"))
9742                            (gnus-article-hidden-text-p 'headers))))
9743              s e)
9744         (delete-region (point-min) (point-max))
9745         (with-current-buffer gnus-original-article-buffer
9746           (goto-char (setq s (point-min)))
9747           (setq e (if (search-forward "\n\n" nil t)
9748                       (1- (point))
9749                     (point-max))))
9750         (insert-buffer-substring gnus-original-article-buffer s e)
9751         (run-hooks 'gnus-article-decode-hook)
9752         (if hidden
9753             (let ((gnus-treat-hide-headers nil)
9754                   (gnus-treat-hide-boring-headers nil))
9755               (gnus-delete-wash-type 'headers)
9756               (gnus-treat-article 'head))
9757           (gnus-treat-article 'head))
9758         (widen)
9759         (if window
9760             (set-window-start window (goto-char (point-min))))
9761         (if gnus-break-pages
9762             (gnus-narrow-to-page)
9763           (when (gnus-visual-p 'page-marker)
9764             (let ((inhibit-read-only t))
9765               (gnus-remove-text-with-property 'gnus-prev)
9766               (gnus-remove-text-with-property 'gnus-next))))
9767         (gnus-set-mode-line 'article)))))
9768
9769 (defun gnus-summary-show-all-headers ()
9770   "Make all header lines visible."
9771   (interactive)
9772   (gnus-summary-toggle-header 1))
9773
9774 (defun gnus-summary-caesar-message (&optional arg)
9775   "Caesar rotate the current article by 13.
9776 With a non-numerical prefix, also rotate headers.  A numerical
9777 prefix specifies how many places to rotate each letter forward."
9778   (interactive "P")
9779   (gnus-summary-select-article)
9780   (let ((mail-header-separator ""))
9781     (gnus-eval-in-buffer-window gnus-article-buffer
9782       (save-restriction
9783         (widen)
9784         (let ((start (window-start))
9785               (inhibit-read-only t))
9786           (if (equal arg '(4))
9787               (message-caesar-buffer-body nil t)
9788             (message-caesar-buffer-body arg))
9789           (set-window-start (get-buffer-window (current-buffer)) start)))))
9790   ;; Create buttons and stuff...
9791   (gnus-treat-article nil))
9792
9793 (declare-function idna-to-unicode "ext:idna" (str))
9794
9795 (defun gnus-summary-idna-message (&optional arg)
9796   "Decode IDNA encoded domain names in the current articles.
9797 IDNA encoded domain names looks like `xn--bar'.  If a string
9798 remain unencoded after running this function, it is likely an
9799 invalid IDNA string (`xn--bar' is invalid).
9800
9801 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9802 installed for this command to work."
9803   (interactive "P")
9804   (if (not (and (condition-case nil (require 'idna)
9805                   (file-error))
9806                 (mm-coding-system-p 'utf-8)
9807                 (executable-find (symbol-value 'idna-program))))
9808       (gnus-message
9809        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9810     (gnus-summary-select-article)
9811     (let ((mail-header-separator ""))
9812       (gnus-eval-in-buffer-window gnus-article-buffer
9813         (save-restriction
9814           (widen)
9815           (let ((start (window-start))
9816                 buffer-read-only)
9817             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9818               (replace-match (idna-to-unicode (match-string 1))))
9819             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9820
9821 (defun gnus-summary-morse-message (&optional arg)
9822   "Morse decode the current article."
9823   (interactive "P")
9824   (gnus-summary-select-article)
9825   (let ((mail-header-separator ""))
9826     (gnus-eval-in-buffer-window gnus-article-buffer
9827       (save-excursion
9828         (save-restriction
9829           (widen)
9830           (let ((pos (window-start))
9831                 (inhibit-read-only t))
9832             (goto-char (point-min))
9833             (when (message-goto-body)
9834               (gnus-narrow-to-body))
9835             (goto-char (point-min))
9836             (while (search-forward "·" (point-max) t)
9837               (replace-match "."))
9838             (unmorse-region (point-min) (point-max))
9839             (widen)
9840             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9841
9842 (defun gnus-summary-stop-page-breaking ()
9843   "Stop page breaking in the current article."
9844   (interactive)
9845   (gnus-summary-select-article)
9846   (gnus-eval-in-buffer-window gnus-article-buffer
9847     (widen)
9848     (when (gnus-visual-p 'page-marker)
9849       (let ((inhibit-read-only t))
9850         (gnus-remove-text-with-property 'gnus-prev)
9851         (gnus-remove-text-with-property 'gnus-next))
9852       (setq gnus-page-broken nil))))
9853
9854 (defun gnus-summary-move-article (&optional n to-newsgroup
9855                                             select-method action)
9856   "Move the current article to a different newsgroup.
9857 If N is a positive number, move the N next articles.
9858 If N is a negative number, move the N previous articles.
9859 If N is nil and any articles have been marked with the process mark,
9860 move those articles instead.
9861 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9862 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9863 re-spool using this method.
9864
9865 When called interactively with TO-NEWSGROUP being nil, the value of
9866 the variable `gnus-move-split-methods' is used for finding a default
9867 for the target newsgroup.
9868
9869 For this function to work, both the current newsgroup and the
9870 newsgroup that you want to move to have to support the `request-move'
9871 and `request-accept' functions.
9872
9873 ACTION can be either `move' (the default), `crosspost' or `copy'."
9874   (interactive "P")
9875   (unless action
9876     (setq action 'move))
9877   ;; Check whether the source group supports the required functions.
9878   (cond ((and (eq action 'move)
9879               (not (gnus-check-backend-function
9880                     'request-move-article gnus-newsgroup-name)))
9881          (error "The current group does not support article moving"))
9882         ((and (eq action 'crosspost)
9883               (not (gnus-check-backend-function
9884                     'request-replace-article gnus-newsgroup-name)))
9885          (error "The current group does not support article editing")))
9886   (let ((articles (gnus-summary-work-articles n))
9887         (prefix (if (gnus-check-backend-function
9888                      'request-move-article gnus-newsgroup-name)
9889                     (funcall gnus-move-group-prefix-function
9890                              gnus-newsgroup-name)
9891                   ""))
9892         (names '((move "Move" "Moving")
9893                  (copy "Copy" "Copying")
9894                  (crosspost "Crosspost" "Crossposting")))
9895         (copy-buf (save-excursion
9896                     (nnheader-set-temp-buffer " *copy article*")))
9897         art-group to-method new-xref article to-groups
9898         articles-to-update-marks encoded)
9899     (unless (assq action names)
9900       (error "Unknown action %s" action))
9901     ;; Read the newsgroup name.
9902     (when (and (not to-newsgroup)
9903                (not select-method))
9904       (if (and gnus-move-split-methods
9905                (not
9906                 (and (memq gnus-current-article articles)
9907                      (gnus-buffer-live-p gnus-original-article-buffer))))
9908           ;; When `gnus-move-split-methods' is non-nil, we have to
9909           ;; select an article to give `gnus-read-move-group-name' an
9910           ;; opportunity to suggest an appropriate default.  However,
9911           ;; we needn't render or mark the article.
9912           (let ((gnus-display-mime-function nil)
9913                 (gnus-article-prepare-hook nil)
9914                 (gnus-mark-article-hook nil))
9915             (gnus-summary-select-article nil nil nil (car articles))))
9916       (setq to-newsgroup (gnus-read-move-group-name
9917                           (cadr (assq action names))
9918                           (symbol-value
9919                            (intern (format "gnus-current-%s-group" action)))
9920                           articles prefix)
9921             encoded to-newsgroup
9922             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9923       (set (intern (format "gnus-current-%s-group" action))
9924            (mm-decode-coding-string
9925             to-newsgroup
9926             (gnus-group-name-charset to-method to-newsgroup))))
9927     (unless to-method
9928       (setq to-method (or select-method
9929                           (gnus-server-to-method
9930                            (gnus-group-method to-newsgroup)))))
9931     (setq to-newsgroup
9932           (or encoded
9933               (and to-newsgroup
9934                    (mm-encode-coding-string
9935                     to-newsgroup
9936                     (gnus-group-name-charset to-method to-newsgroup)))))
9937     ;; Check the method we are to move this article to...
9938     (unless (gnus-check-backend-function
9939              'request-accept-article (car to-method))
9940       (error "%s does not support article copying" (car to-method)))
9941     (unless (gnus-check-server to-method)
9942       (error "Can't open server %s" (car to-method)))
9943     (gnus-message 6 "%s to %s: %s..."
9944                   (caddr (assq action names))
9945                   (or (car select-method)
9946                       (gnus-group-decoded-name to-newsgroup))
9947                   articles)
9948     (while articles
9949       (setq article (pop articles))
9950       ;; Set any marks that may have changed in the summary buffer.
9951       (when gnus-preserve-marks
9952         (gnus-summary-push-marks-to-backend article))
9953       (setq
9954        art-group
9955        (cond
9956         ;; Move the article.
9957         ((eq action 'move)
9958          ;; Remove this article from future suppression.
9959          (gnus-dup-unsuppress-article article)
9960          (let* ((from-method (gnus-find-method-for-group
9961                               gnus-newsgroup-name))
9962                 (to-method (or select-method
9963                                (gnus-find-method-for-group to-newsgroup)))
9964                 (move-is-internal (gnus-server-equal from-method to-method)))
9965            (gnus-request-move-article
9966             article                     ; Article to move
9967             gnus-newsgroup-name         ; From newsgroup
9968             (nth 1 (gnus-find-method-for-group
9969                     gnus-newsgroup-name)) ; Server
9970             (list 'gnus-request-accept-article
9971                   to-newsgroup (list 'quote select-method)
9972                   (not articles) t)     ; Accept form
9973             (not articles)              ; Only save nov last time
9974             (and move-is-internal
9975                  to-newsgroup           ; Not respooling
9976                                         ; Is this move internal?
9977                  (gnus-group-real-name to-newsgroup)))))
9978         ;; Copy the article.
9979         ((eq action 'copy)
9980          (with-current-buffer copy-buf
9981            (when (gnus-request-article-this-buffer article
9982                                                    gnus-newsgroup-name)
9983              (save-restriction
9984                (nnheader-narrow-to-headers)
9985                (dolist (hdr gnus-copy-article-ignored-headers)
9986                  (message-remove-header hdr t)))
9987              (gnus-request-accept-article
9988               to-newsgroup select-method (not articles) t))))
9989         ;; Crosspost the article.
9990         ((eq action 'crosspost)
9991          (let ((xref (message-tokenize-header
9992                       (mail-header-xref (gnus-summary-article-header
9993                                          article))
9994                       " ")))
9995            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9996                                   ":" (number-to-string article)))
9997            (unless xref
9998              (setq xref (list (system-name))))
9999            (setq new-xref
10000                  (concat
10001                   (mapconcat 'identity
10002                              (delete "Xref:" (delete new-xref xref))
10003                              " ")
10004                   " " new-xref))
10005            (with-current-buffer copy-buf
10006              ;; First put the article in the destination group.
10007              (gnus-request-article-this-buffer article gnus-newsgroup-name)
10008              (when (consp (setq art-group
10009                                 (gnus-request-accept-article
10010                                  to-newsgroup select-method (not articles)
10011                                  t)))
10012                (setq new-xref (concat new-xref " " (car art-group)
10013                                       ":"
10014                                       (number-to-string (cdr art-group))))
10015                ;; Now we have the new Xrefs header, so we insert
10016                ;; it and replace the new article.
10017                (nnheader-replace-header "Xref" new-xref)
10018                (gnus-request-replace-article
10019                 (cdr art-group) to-newsgroup (current-buffer) t)
10020                art-group))))))
10021       (cond
10022        ((not art-group)
10023         (gnus-message 1 "Couldn't %s article %s: %s"
10024                       (cadr (assq action names)) article
10025                       (nnheader-get-report (car to-method))))
10026        ((eq art-group 'junk)
10027         (when (eq action 'move)
10028           (gnus-summary-mark-article article gnus-canceled-mark)
10029           (gnus-message 4 "Deleted article %s" article)
10030           ;; run the delete hook
10031           (run-hook-with-args 'gnus-summary-article-delete-hook
10032                               action
10033                               (gnus-data-header
10034                                (assoc article (gnus-data-list nil)))
10035                               gnus-newsgroup-name nil
10036                               select-method)))
10037        (t
10038         (let* ((pto-group (gnus-group-prefixed-name
10039                            (car art-group) to-method))
10040                (info (gnus-get-info pto-group))
10041                (to-group (gnus-info-group info))
10042                to-marks)
10043           ;; Update the group that has been moved to.
10044           (when (and info
10045                      (memq action '(move copy)))
10046             (unless (member to-group to-groups)
10047               (push to-group to-groups))
10048
10049             (when (and (not (memq article gnus-newsgroup-unreads))
10050                        (cdr art-group))
10051               (push 'read to-marks)
10052               (gnus-info-set-read
10053                info (gnus-add-to-range (gnus-info-read info)
10054                                        (list (cdr art-group)))))
10055
10056             ;; See whether the article is to be put in the cache.
10057             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10058                    (marks (if expirable
10059                               gnus-article-mark-lists
10060                             (delete '(expirable . expire)
10061                                     (copy-sequence
10062                                      gnus-article-mark-lists))))
10063                    (to-article (cdr art-group)))
10064
10065               ;; Enter the article into the cache in the new group,
10066               ;; if that is required.
10067               (when (and to-article
10068                          gnus-use-cache)
10069                 (gnus-cache-possibly-enter-article
10070                  to-group to-article
10071                  (memq article gnus-newsgroup-marked)
10072                  (memq article gnus-newsgroup-dormant)
10073                  (memq article gnus-newsgroup-unreads)))
10074
10075               (when (and gnus-preserve-marks
10076                          to-article)
10077                 ;; Copy any marks over to the new group.
10078                 (when (and (equal to-group gnus-newsgroup-name)
10079                            (not (memq article gnus-newsgroup-unreads)))
10080                   ;; Mark this article as read in this group.
10081                   (push (cons to-article gnus-read-mark)
10082                         gnus-newsgroup-reads)
10083                   ;; Increase the active status of this group.
10084                   (setcdr (gnus-active to-group) to-article)
10085                   (setcdr gnus-newsgroup-active to-article))
10086
10087                 (while marks
10088                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10089                     (when (memq article (symbol-value
10090                                          (intern (format "gnus-newsgroup-%s"
10091                                                          (caar marks)))))
10092                       (push (cdar marks) to-marks)
10093                       ;; If the other group is the same as this group,
10094                       ;; then we have to add the mark to the list.
10095                       (when (equal to-group gnus-newsgroup-name)
10096                         (set (intern (format "gnus-newsgroup-%s"
10097                                              (caar marks)))
10098                              (cons to-article
10099                                    (symbol-value
10100                                     (intern (format "gnus-newsgroup-%s"
10101                                                     (caar marks)))))))
10102                       ;; Copy the marks to other group.
10103                       (gnus-add-marked-articles
10104                        to-group (cdar marks) (list to-article) info)))
10105                   (setq marks (cdr marks)))
10106
10107                 (when (and expirable
10108                            gnus-mark-copied-or-moved-articles-as-expirable
10109                            (not (memq 'expire to-marks)))
10110                   ;; Mark this article as expirable.
10111                   (push 'expire to-marks)
10112                   (when (equal to-group gnus-newsgroup-name)
10113                     (push to-article gnus-newsgroup-expirable))
10114                   ;; Copy the expirable mark to other group.
10115                   (gnus-add-marked-articles
10116                    to-group 'expire (list to-article) info))
10117
10118                 (when (and to-marks
10119                            (gnus-method-option-p
10120                             (gnus-find-method-for-group to-group)
10121                             'server-marks))
10122                   (gnus-request-set-mark
10123                    to-group (list (list (list to-article) 'add to-marks)))))
10124
10125               (gnus-dribble-enter
10126                (concat "(gnus-group-set-info '"
10127                        (gnus-prin1-to-string (gnus-get-info to-group))
10128                        ")")
10129                (concat "^(gnus-group-set-info '(\""
10130                        (regexp-quote to-group) "\""))))
10131
10132           ;; Update the Xref header in this article to point to
10133           ;; the new crossposted article we have just created.
10134           (when (eq action 'crosspost)
10135             (with-current-buffer copy-buf
10136               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10137               (nnheader-replace-header "Xref" new-xref)
10138               (gnus-request-replace-article
10139                article gnus-newsgroup-name (current-buffer) t)))
10140
10141           ;; run the move/copy/crosspost/respool hook
10142           (run-hook-with-args 'gnus-summary-article-move-hook
10143                               action
10144                               (gnus-data-header
10145                                (assoc article (gnus-data-list nil)))
10146                               gnus-newsgroup-name
10147                               to-newsgroup
10148                               select-method))
10149
10150         ;;;!!!Why is this necessary?
10151         (set-buffer gnus-summary-buffer)
10152
10153         (when (eq action 'move)
10154           (save-excursion
10155             (gnus-summary-goto-subject article)
10156             (gnus-summary-mark-article article gnus-canceled-mark)))))
10157       (push article articles-to-update-marks))
10158
10159     (save-excursion
10160       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10161     ;; Re-activate all groups that have been moved to.
10162     (with-current-buffer gnus-group-buffer
10163       (let ((gnus-group-marked to-groups))
10164         (gnus-group-get-new-news-this-group nil t)))
10165
10166     (gnus-kill-buffer copy-buf)
10167     (gnus-summary-position-point)
10168     (gnus-set-mode-line 'summary)))
10169
10170 (defun gnus-summary-push-marks-to-backend (article)
10171   (let ((set nil)
10172         (del nil)
10173         (marks gnus-article-mark-lists))
10174     (unless (memq article gnus-newsgroup-unreads)
10175       (push 'read set))
10176     (while marks
10177       (if (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10178                (memq article (symbol-value
10179                               (intern (format "gnus-newsgroup-%s"
10180                                               (caar marks))))))
10181           (push (cdar marks) set)
10182         (push (cdar marks) del))
10183       (pop marks))
10184     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)
10185                                                  ((,article) del ,del)))))
10186
10187 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10188   "Copy the current article to some other group.
10189 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10190 When called interactively, if TO-NEWSGROUP is nil, use the value of
10191 the variable `gnus-move-split-methods' for finding a default target
10192 newsgroup.
10193 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10194 re-spool using this method."
10195   (interactive "P")
10196   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10197
10198 (defun gnus-summary-crosspost-article (&optional n)
10199   "Crosspost the current article to some other group."
10200   (interactive "P")
10201   (gnus-summary-move-article n nil nil 'crosspost))
10202
10203 (defcustom gnus-summary-respool-default-method nil
10204   "Default method type for respooling an article.
10205 If nil, use to the current newsgroup method."
10206   :type 'symbol
10207   :group 'gnus-summary-mail)
10208
10209 (defun gnus-summary-respool-article (&optional n method)
10210   "Respool the current article.
10211 The article will be squeezed through the mail spooling process again,
10212 which means that it will be put in some mail newsgroup or other
10213 depending on `nnmail-split-methods'.
10214 If N is a positive number, respool the N next articles.
10215 If N is a negative number, respool the N previous articles.
10216 If N is nil and any articles have been marked with the process mark,
10217 respool those articles instead.
10218
10219 Respooling can be done both from mail groups and \"real\" newsgroups.
10220 In the former case, the articles in question will be moved from the
10221 current group into whatever groups they are destined to.  In the
10222 latter case, they will be copied into the relevant groups."
10223   (interactive
10224    (list current-prefix-arg
10225          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10226                 (methname
10227                  (symbol-name (or gnus-summary-respool-default-method
10228                                   (car (gnus-find-method-for-group
10229                                         gnus-newsgroup-name)))))
10230                 (method
10231                  (gnus-completing-read
10232                   "Backend to use when respooling"
10233                   methods t nil 'gnus-mail-method-history methname))
10234                 ms)
10235            (cond
10236             ((zerop (length (setq ms (gnus-servers-using-backend
10237                                       (intern method)))))
10238              (list (intern method) ""))
10239             ((= 1 (length ms))
10240              (car ms))
10241             (t
10242              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10243                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10244                            ms-alist))))))))
10245   (unless method
10246     (error "No method given for respooling"))
10247   (if (assoc (symbol-name
10248               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10249              (gnus-methods-using 'respool))
10250       (gnus-summary-move-article n nil method)
10251     (gnus-summary-copy-article n nil method)))
10252
10253 (defun gnus-summary-import-article (file &optional edit)
10254   "Import an arbitrary file into a mail newsgroup."
10255   (interactive "fImport file: \nP")
10256   (let ((group gnus-newsgroup-name)
10257         (now (current-time))
10258         atts lines group-art)
10259     (unless (gnus-check-backend-function 'request-accept-article group)
10260       (error "%s does not support article importing" group))
10261     (or (file-readable-p file)
10262         (not (file-regular-p file))
10263         (error "Can't read %s" file))
10264     (with-current-buffer (gnus-get-buffer-create " *import file*")
10265       (erase-buffer)
10266       (nnheader-insert-file-contents file)
10267       (goto-char (point-min))
10268       (if (nnheader-article-p)
10269           (save-restriction
10270             (goto-char (point-min))
10271             (search-forward "\n\n" nil t)
10272             (narrow-to-region (point-min) (1- (point)))
10273             (goto-char (point-min))
10274             (unless (re-search-forward "^date:" nil t)
10275               (goto-char (point-max))
10276               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10277        ;; This doesn't look like an article, so we fudge some headers.
10278         (setq atts (file-attributes file)
10279               lines (count-lines (point-min) (point-max)))
10280         (insert "From: " (read-string "From: ") "\n"
10281                 "Subject: " (read-string "Subject: ") "\n"
10282                 "Date: " (message-make-date (nth 5 atts)) "\n"
10283                 "Message-ID: " (message-make-message-id) "\n"
10284                 "Lines: " (int-to-string lines) "\n"
10285                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10286       (setq group-art (gnus-request-accept-article group nil t))
10287       (kill-buffer (current-buffer)))
10288     (setq gnus-newsgroup-active (gnus-activate-group group))
10289     (forward-line 1)
10290     (gnus-summary-goto-article (cdr group-art) nil t)
10291     (when edit
10292       (gnus-summary-edit-article))))
10293
10294 (defun gnus-summary-create-article ()
10295   "Create an article in a mail newsgroup."
10296   (interactive)
10297   (let ((group gnus-newsgroup-name)
10298         (now (current-time))
10299         group-art)
10300     (unless (gnus-check-backend-function 'request-accept-article group)
10301       (error "%s does not support article importing" group))
10302     (with-current-buffer (gnus-get-buffer-create " *import file*")
10303       (erase-buffer)
10304       (goto-char (point-min))
10305       ;; This doesn't look like an article, so we fudge some headers.
10306       (insert "From: " (read-string "From: ") "\n"
10307               "Subject: " (read-string "Subject: ") "\n"
10308               "Date: " (message-make-date now) "\n"
10309               "Message-ID: " (message-make-message-id) "\n")
10310       (setq group-art (gnus-request-accept-article group nil t))
10311       (kill-buffer (current-buffer)))
10312     (setq gnus-newsgroup-active (gnus-activate-group group))
10313     (forward-line 1)
10314     (gnus-summary-goto-article (cdr group-art) nil t)
10315     (gnus-summary-edit-article)))
10316
10317 (defun gnus-summary-article-posted-p ()
10318   "Say whether the current (mail) article is available from news as well.
10319 This will be the case if the article has both been mailed and posted."
10320   (interactive)
10321   (let ((id (mail-header-references (gnus-summary-article-header)))
10322         (gnus-override-method (car (gnus-refer-article-methods))))
10323     (if (gnus-request-head id "")
10324         (gnus-message 2 "The current message was found on %s"
10325                       gnus-override-method)
10326       (gnus-message 2 "The current message couldn't be found on %s"
10327                     gnus-override-method)
10328       nil)))
10329
10330 (defun gnus-summary-expire-articles (&optional now)
10331   "Expire all articles that are marked as expirable in the current group."
10332   (interactive)
10333   (when (and (not gnus-group-is-exiting-without-update-p)
10334              (gnus-check-backend-function
10335               'request-expire-articles gnus-newsgroup-name))
10336     ;; This backend supports expiry.
10337     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10338            (expirable
10339             (gnus-list-range-difference
10340              (if total
10341                  (progn
10342                    ;; We need to update the info for
10343                    ;; this group for `gnus-list-of-read-articles'
10344                    ;; to give us the right answer.
10345                    (gnus-run-hooks 'gnus-exit-group-hook)
10346                    (gnus-summary-update-info)
10347                    (gnus-list-of-read-articles gnus-newsgroup-name))
10348                (setq gnus-newsgroup-expirable
10349                      (sort gnus-newsgroup-expirable '<)))
10350              gnus-newsgroup-unexist))
10351            (expiry-wait (if now 'immediate
10352                           (gnus-group-find-parameter
10353                            gnus-newsgroup-name 'expiry-wait)))
10354            (nnmail-expiry-target
10355             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10356                 nnmail-expiry-target))
10357            es)
10358       (when expirable
10359         ;; There are expirable articles in this group, so we run them
10360         ;; through the expiry process.
10361         (gnus-message 6 "Expiring articles...")
10362         (when (gnus-check-group gnus-newsgroup-name)
10363           ;; The list of articles that weren't expired is returned.
10364           (save-excursion
10365             (if expiry-wait
10366                 (let ((nnmail-expiry-wait-function nil)
10367                       (nnmail-expiry-wait expiry-wait))
10368                   (setq es (gnus-request-expire-articles
10369                             expirable gnus-newsgroup-name)))
10370               (setq es (gnus-request-expire-articles
10371                         expirable gnus-newsgroup-name)))
10372             (unless total
10373               (setq gnus-newsgroup-expirable es))
10374             ;; We go through the old list of expirable, and mark all
10375             ;; really expired articles as nonexistent.
10376             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10377               (let ((gnus-use-cache nil))
10378                 (dolist (article expirable)
10379                   (when (and (not (memq article es))
10380                              (gnus-data-find article))
10381                     (gnus-summary-mark-article article gnus-canceled-mark)
10382                     (run-hook-with-args 'gnus-summary-article-expire-hook
10383                                         'delete
10384                                         (gnus-data-header
10385                                          (assoc article (gnus-data-list nil)))
10386                                         gnus-newsgroup-name
10387                                         nil
10388                                         nil)))))))
10389         (gnus-message 6 "Expiring articles...done")))))
10390
10391 (defun gnus-summary-expire-articles-now ()
10392   "Expunge all expirable articles in the current group.
10393 This means that *all* articles that are marked as expirable will be
10394 deleted forever, right now."
10395   (interactive)
10396   (or gnus-expert-user
10397       (gnus-yes-or-no-p
10398        "Are you really, really sure you want to delete all expirable messages? ")
10399       (error "Phew!"))
10400   (gnus-summary-expire-articles t))
10401
10402 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10403 (defun gnus-summary-delete-article (&optional n)
10404   "Delete the N next (mail) articles.
10405 This command actually deletes articles.  This is not a marking
10406 command.  The article will disappear forever from your life, never to
10407 return.
10408
10409 If N is negative, delete backwards.
10410 If N is nil and articles have been marked with the process mark,
10411 delete these instead.
10412
10413 If `gnus-novice-user' is non-nil you will be asked for
10414 confirmation before the articles are deleted."
10415   (interactive "P")
10416   (unless (gnus-check-backend-function 'request-expire-articles
10417                                        gnus-newsgroup-name)
10418     (error "The current newsgroup does not support article deletion"))
10419   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10420     (error "Couldn't open server"))
10421   ;; Compute the list of articles to delete.
10422   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10423         (nnmail-expiry-target 'delete)
10424         not-deleted)
10425     (if (and gnus-novice-user
10426              (not (gnus-yes-or-no-p
10427                    (format "Do you really want to delete %s forever? "
10428                            (if (> (length articles) 1)
10429                                (format "these %s articles" (length articles))
10430                              "this article")))))
10431         ()
10432       ;; Delete the articles.
10433       (setq not-deleted (gnus-request-expire-articles
10434                          articles gnus-newsgroup-name 'force))
10435       (save-excursion
10436         (while articles
10437           (gnus-summary-remove-process-mark (car articles))
10438           ;; The backend might not have been able to delete the article
10439           ;; after all.
10440           (unless (memq (car articles) not-deleted)
10441             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10442             (let* ((article (car articles))
10443                    (ghead  (gnus-data-header
10444                             (assoc article (gnus-data-list nil)))))
10445               (run-hook-with-args 'gnus-summary-article-delete-hook
10446                                   'delete ghead gnus-newsgroup-name nil
10447                                   nil)))
10448           (setq articles (cdr articles))))
10449       (when not-deleted
10450         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10451     (gnus-summary-position-point)
10452     (gnus-set-mode-line 'summary)
10453     not-deleted))
10454
10455 (defun gnus-summary-edit-article (&optional arg)
10456   "Edit the current article.
10457 This will have permanent effect only in mail groups.
10458 If ARG is nil, edit the decoded articles.
10459 If ARG is 1, edit the raw articles.
10460 If ARG is 2, edit the raw articles even in read-only groups.
10461 If ARG is 3, edit the articles with the current handles.
10462 Otherwise, allow editing of articles even in read-only
10463 groups."
10464   (interactive "P")
10465   (let (force raw current-handles)
10466     (cond
10467      ((null arg))
10468      ((eq arg 1)
10469       (setq raw t))
10470      ((eq arg 2)
10471       (setq raw t
10472             force t))
10473      ((eq arg 3)
10474       (setq current-handles
10475             (and (gnus-buffer-live-p gnus-article-buffer)
10476                  (with-current-buffer gnus-article-buffer
10477                    (prog1
10478                        gnus-article-mime-handles
10479                      (setq gnus-article-mime-handles nil))))))
10480      (t
10481       (setq force t)))
10482     (when (and raw (not force)
10483                (member gnus-newsgroup-name '("nndraft:delayed"
10484                                              "nndraft:drafts"
10485                                              "nndraft:queue")))
10486       (error "Can't edit the raw article in group %s"
10487              gnus-newsgroup-name))
10488     (with-current-buffer gnus-summary-buffer
10489       (let ((mail-parse-charset gnus-newsgroup-charset)
10490             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10491         (gnus-set-global-variables)
10492         (when (and (not force)
10493                    (gnus-group-read-only-p))
10494           (error "The current newsgroup does not support article editing"))
10495         (gnus-summary-show-article t)
10496         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10497           (with-current-buffer gnus-article-buffer
10498             (mm-enable-multibyte)))
10499         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10500             (setq raw t))
10501         (gnus-article-edit-article
10502          (if raw 'ignore
10503            `(lambda ()
10504               (let ((mbl mml-buffer-list))
10505                 (setq mml-buffer-list nil)
10506                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10507                   (mime-to-mml ,'current-handles))
10508                 (let ((mbl1 mml-buffer-list))
10509                   (setq mml-buffer-list mbl)
10510                   (set (make-local-variable 'mml-buffer-list) mbl1))
10511                 (gnus-make-local-hook 'kill-buffer-hook)
10512                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10513          `(lambda (no-highlight)
10514             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10515                   (message-options message-options)
10516                   (message-options-set-recipient)
10517                   (mail-parse-ignored-charsets
10518                    ',gnus-newsgroup-ignored-charsets)
10519                   (rfc2047-header-encoding-alist
10520                    ',(let ((charset (gnus-group-name-charset
10521                                      (gnus-find-method-for-group
10522                                       gnus-newsgroup-name)
10523                                      gnus-newsgroup-name)))
10524                        (append (list (cons "Newsgroups" charset)
10525                                      (cons "Followup-To" charset)
10526                                      (cons "Xref" charset))
10527                                rfc2047-header-encoding-alist))))
10528               ,(if (not raw) '(progn
10529                                 (mml-to-mime)
10530                                 (mml-destroy-buffers)
10531                                 (remove-hook 'kill-buffer-hook
10532                                              'mml-destroy-buffers t)
10533                                 (kill-local-variable 'mml-buffer-list)))
10534               (gnus-summary-edit-article-done
10535                ,(or (mail-header-references gnus-current-headers) "")
10536                ,(gnus-group-read-only-p)
10537                ,gnus-summary-buffer no-highlight))))))))
10538
10539 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10540
10541 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10542                                                  no-highlight)
10543   "Make edits to the current article permanent."
10544   (interactive)
10545   (save-excursion
10546     ;; The buffer restriction contains the entire article if it exists.
10547     (when (article-goto-body)
10548       (let ((lines (count-lines (point) (point-max)))
10549             (length (- (point-max) (point)))
10550             (case-fold-search t)
10551             (body (copy-marker (point))))
10552         (goto-char (point-min))
10553         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10554           (delete-region (match-beginning 1) (match-end 1))
10555           (insert (number-to-string length)))
10556         (goto-char (point-min))
10557         (when (re-search-forward
10558                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10559           (delete-region (match-beginning 1) (match-end 1))
10560           (insert (number-to-string length)))
10561         (goto-char (point-min))
10562         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10563           (delete-region (match-beginning 1) (match-end 1))
10564           (insert (number-to-string lines))))))
10565   ;; Replace the article.
10566   (let ((buf (current-buffer))
10567         (article (cdr gnus-article-current))
10568         replace-result)
10569     (with-temp-buffer
10570       (insert-buffer-substring buf)
10571       (if (and (not read-only)
10572                (not (setq replace-result
10573                           (gnus-request-replace-article
10574                            article (car gnus-article-current)
10575                            (current-buffer) t))))
10576           (error "Couldn't replace article")
10577         ;; If we got a number back, then that's the new article number
10578         ;; for this article.  Otherwise, the article number didn't change.
10579         (when (numberp replace-result)
10580           (with-current-buffer gnus-summary-buffer
10581             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10582             (gnus-summary-limit gnus-newsgroup-limit)
10583             (setq article replace-result)
10584             (gnus-summary-goto-subject article t)))
10585         ;; Update the summary buffer.
10586         (if (and references
10587                  (equal (message-tokenize-header references " ")
10588                         (message-tokenize-header
10589                          (or (message-fetch-field "references") "") " ")))
10590             ;; We only have to update this line.
10591             (save-excursion
10592               (save-restriction
10593                 (message-narrow-to-head)
10594                 (let ((head (buffer-substring-no-properties
10595                              (point-min) (point-max)))
10596                       header)
10597                   (with-temp-buffer
10598                     (insert (format "211 %d Article retrieved.\n" article))
10599                     (insert head)
10600                     (insert ".\n")
10601                     (let ((nntp-server-buffer (current-buffer)))
10602                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10603                     (with-current-buffer gnus-summary-buffer
10604                       (gnus-data-set-header (gnus-data-find article) header)
10605                       (gnus-summary-update-article-line article header)
10606                       (if (gnus-summary-goto-subject article nil t)
10607                           (gnus-summary-update-secondary-mark article)))))))
10608           ;; Update threads.
10609           (set-buffer (or buffer gnus-summary-buffer))
10610           (gnus-summary-update-article article)
10611           (if (gnus-summary-goto-subject article nil t)
10612               (gnus-summary-update-secondary-mark article)))
10613         ;; Prettify the article buffer again.
10614         (unless no-highlight
10615           (with-current-buffer gnus-article-buffer
10616             ;;!!! Fix this -- article should be rehighlighted.
10617             ;;(gnus-run-hooks 'gnus-article-display-hook)
10618             (set-buffer gnus-original-article-buffer)
10619             (gnus-request-article
10620              article (car gnus-article-current) (current-buffer))))
10621         ;; Prettify the summary buffer line.
10622         (when (gnus-visual-p 'summary-highlight 'highlight)
10623           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10624
10625 (defun gnus-summary-edit-wash (key)
10626   "Perform editing command KEY in the article buffer."
10627   (interactive
10628    (list
10629     (progn
10630       (message "%s" (concat (this-command-keys) "- "))
10631       (read-char))))
10632   (message "")
10633   (gnus-summary-edit-article)
10634   (execute-kbd-macro (concat (this-command-keys) key))
10635   (gnus-article-edit-done))
10636
10637 ;;; Respooling
10638
10639 (defun gnus-summary-respool-query (&optional silent trace)
10640   "Query where the respool algorithm would put this article."
10641   (interactive)
10642   (let (gnus-mark-article-hook)
10643     (gnus-summary-select-article)
10644     (with-current-buffer gnus-original-article-buffer
10645       (let ((groups (nnmail-article-group 'identity trace)))
10646         (unless silent
10647           (if groups
10648               (message "This message would go to %s"
10649                        (mapconcat 'car groups ", "))
10650             (message "This message would go to no groups"))
10651           groups)))))
10652
10653 (defun gnus-summary-respool-trace ()
10654   "Trace where the respool algorithm would put this article.
10655 Display a buffer showing all fancy splitting patterns which matched."
10656   (interactive)
10657   (gnus-summary-respool-query nil t))
10658
10659 ;; Summary marking commands.
10660
10661 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10662   "Mark articles which has the same subject as read, and then select the next.
10663 If UNMARK is positive, remove any kind of mark.
10664 If UNMARK is negative, tick articles."
10665   (interactive "P")
10666   (when unmark
10667     (setq unmark (prefix-numeric-value unmark)))
10668   (let ((count
10669          (gnus-summary-mark-same-subject
10670           (gnus-summary-article-subject) unmark)))
10671     ;; Select next unread article.  If auto-select-same mode, should
10672     ;; select the first unread article.
10673     (gnus-summary-next-article t (and gnus-auto-select-same
10674                                       (gnus-summary-article-subject)))
10675     (gnus-message 7 "%d article%s marked as %s"
10676                   count (if (= count 1) " is" "s are")
10677                   (if unmark "unread" "read"))))
10678
10679 (defun gnus-summary-kill-same-subject (&optional unmark)
10680   "Mark articles which has the same subject as read.
10681 If UNMARK is positive, remove any kind of mark.
10682 If UNMARK is negative, tick articles."
10683   (interactive "P")
10684   (when unmark
10685     (setq unmark (prefix-numeric-value unmark)))
10686   (let ((count
10687          (gnus-summary-mark-same-subject
10688           (gnus-summary-article-subject) unmark)))
10689     ;; If marked as read, go to next unread subject.
10690     (when (null unmark)
10691       ;; Go to next unread subject.
10692       (gnus-summary-next-subject 1 t))
10693     (gnus-message 7 "%d articles are marked as %s"
10694                   count (if unmark "unread" "read"))))
10695
10696 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10697   "Mark articles with same SUBJECT as read, and return marked number.
10698 If optional argument UNMARK is positive, remove any kinds of marks.
10699 If optional argument UNMARK is negative, mark articles as unread instead."
10700   (let ((count 1))
10701     (save-excursion
10702       (cond
10703        ((null unmark)                   ; Mark as read.
10704         (while (and
10705                 (progn
10706                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10707                   (gnus-summary-show-thread) t)
10708                 (gnus-summary-find-subject subject))
10709           (setq count (1+ count))))
10710        ((> unmark 0)                    ; Tick.
10711         (while (and
10712                 (progn
10713                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10714                   (gnus-summary-show-thread) t)
10715                 (gnus-summary-find-subject subject))
10716           (setq count (1+ count))))
10717        (t                               ; Mark as unread.
10718         (while (and
10719                 (progn
10720                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10721                   (gnus-summary-show-thread) t)
10722                 (gnus-summary-find-subject subject))
10723           (setq count (1+ count)))))
10724       (gnus-set-mode-line 'summary)
10725       ;; Return the number of marked articles.
10726       count)))
10727
10728 (defun gnus-summary-mark-as-processable (n &optional unmark)
10729   "Set the process mark on the next N articles.
10730 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10731 the process mark instead.  The difference between N and the actual
10732 number of articles marked is returned."
10733   (interactive "P")
10734   (if (and (null n) (gnus-region-active-p))
10735       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10736     (setq n (prefix-numeric-value n))
10737     (let ((backward (< n 0))
10738           (n (abs n)))
10739       (while (and
10740               (> n 0)
10741               (if unmark
10742                   (gnus-summary-remove-process-mark
10743                    (gnus-summary-article-number))
10744                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10745               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10746         (setq n (1- n)))
10747       (when (/= 0 n)
10748         (gnus-message 7 "No more articles"))
10749       (gnus-summary-recenter)
10750       (gnus-summary-position-point)
10751       n)))
10752
10753 (defun gnus-summary-unmark-as-processable (n)
10754   "Remove the process mark from the next N articles.
10755 If N is negative, unmark backward instead.  The difference between N and
10756 the actual number of articles unmarked is returned."
10757   (interactive "P")
10758   (gnus-summary-mark-as-processable n t))
10759
10760 (defun gnus-summary-unmark-all-processable ()
10761   "Remove the process mark from all articles."
10762   (interactive)
10763   (save-excursion
10764     (while gnus-newsgroup-processable
10765       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10766   (gnus-summary-position-point))
10767
10768 (defun gnus-summary-add-mark (article type)
10769   "Mark ARTICLE with a mark of TYPE."
10770   (let ((vtype (car (assq type gnus-article-mark-lists)))
10771         var)
10772     (if (not vtype)
10773         (error "No such mark type: %s" type)
10774       (setq var (intern (format "gnus-newsgroup-%s" type)))
10775       (set var (cons article (symbol-value var)))
10776       (if (memq type '(processable cached replied forwarded recent saved))
10777           (gnus-summary-update-secondary-mark article)
10778         ;;; !!! This is bogus.  We should find out what primary
10779         ;;; !!! mark we want to set.
10780         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10781
10782 (defun gnus-summary-mark-as-expirable (n)
10783   "Mark N articles forward as expirable.
10784 If N is negative, mark backward instead.  The difference between N and
10785 the actual number of articles marked is returned."
10786   (interactive "p")
10787   (gnus-summary-mark-forward n gnus-expirable-mark))
10788
10789 (defun gnus-summary-mark-as-spam (n)
10790   "Mark N articles forward as spam.
10791 If N is negative, mark backward instead.  The difference between N and
10792 the actual number of articles marked is returned."
10793   (interactive "p")
10794   (gnus-summary-mark-forward n gnus-spam-mark))
10795
10796 (defun gnus-summary-mark-article-as-replied (article)
10797   "Mark ARTICLE as replied to and update the summary line.
10798 ARTICLE can also be a list of articles."
10799   (interactive (list (gnus-summary-article-number)))
10800   (let ((articles (if (listp article) article (list article))))
10801     (dolist (article articles)
10802       (unless (numberp article)
10803         (error "%s is not a number" article))
10804       (push article gnus-newsgroup-replied)
10805       (let ((inhibit-read-only t))
10806         (when (gnus-summary-goto-subject article nil t)
10807           (gnus-summary-update-secondary-mark article))))))
10808
10809 (defun gnus-summary-mark-article-as-forwarded (article)
10810   "Mark ARTICLE as forwarded and update the summary line.
10811 ARTICLE can also be a list of articles."
10812   (let ((articles (if (listp article) article (list article))))
10813     (dolist (article articles)
10814       (push article gnus-newsgroup-forwarded)
10815       (let ((inhibit-read-only t))
10816         (when (gnus-summary-goto-subject article nil t)
10817           (gnus-summary-update-secondary-mark article))))))
10818
10819 (defun gnus-summary-set-bookmark (article)
10820   "Set a bookmark in current article."
10821   (interactive (list (gnus-summary-article-number)))
10822   (when (or (not (get-buffer gnus-article-buffer))
10823             (not gnus-current-article)
10824             (not gnus-article-current)
10825             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10826     (error "No current article selected"))
10827   ;; Remove old bookmark, if one exists.
10828   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10829   ;; Set the new bookmark, which is on the form
10830   ;; (article-number . line-number-in-body).
10831   (push
10832    (cons article
10833          (with-current-buffer gnus-article-buffer
10834            (count-lines
10835             (min (point)
10836                  (save-excursion
10837                    (article-goto-body)
10838                    (point)))
10839             (point))))
10840    gnus-newsgroup-bookmarks)
10841   (gnus-message 6 "A bookmark has been added to the current article."))
10842
10843 (defun gnus-summary-remove-bookmark (article)
10844   "Remove the bookmark from the current article."
10845   (interactive (list (gnus-summary-article-number)))
10846   ;; Remove old bookmark, if one exists.
10847   (if (not (assq article gnus-newsgroup-bookmarks))
10848       (gnus-message 6 "No bookmark in current article.")
10849     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10850     (gnus-message 6 "Removed bookmark.")))
10851
10852 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10853 (defun gnus-summary-mark-as-dormant (n)
10854   "Mark N articles forward as dormant.
10855 If N is negative, mark backward instead.  The difference between N and
10856 the actual number of articles marked is returned."
10857   (interactive "p")
10858   (gnus-summary-mark-forward n gnus-dormant-mark))
10859
10860 (defun gnus-summary-set-process-mark (article)
10861   "Set the process mark on ARTICLE and update the summary line."
10862   (setq gnus-newsgroup-processable
10863         (cons article
10864               (delq article gnus-newsgroup-processable)))
10865   (when (gnus-summary-goto-subject article)
10866     (gnus-summary-show-thread)
10867     (gnus-summary-goto-subject article)
10868     (gnus-summary-update-secondary-mark article)))
10869
10870 (defun gnus-summary-remove-process-mark (&rest articles)
10871   "Remove the process mark from ARTICLES and update the summary line."
10872   (dolist (article articles)
10873     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10874     (when (gnus-summary-goto-subject article)
10875       (gnus-summary-show-thread)
10876       (gnus-summary-goto-subject article)
10877       (gnus-summary-update-secondary-mark article)))
10878   t)
10879
10880 (defun gnus-summary-set-saved-mark (article)
10881   "Set the process mark on ARTICLE and update the summary line."
10882   (push article gnus-newsgroup-saved)
10883   (when (gnus-summary-goto-subject article)
10884     (gnus-summary-update-secondary-mark article)))
10885
10886 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10887   "Mark N articles as read forwards.
10888 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10889 The difference between N and the actual number of articles marked is
10890 returned.
10891 If NO-EXPIRE, auto-expiry will be inhibited."
10892   (interactive "p")
10893   (gnus-summary-show-thread)
10894   (let ((backward (< n 0))
10895         (gnus-summary-goto-unread
10896          (and gnus-summary-goto-unread
10897               (not (eq gnus-summary-goto-unread 'never))
10898               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10899                                     gnus-ticked-mark gnus-dormant-mark)))))
10900         (n (abs n))
10901         (mark (or mark gnus-del-mark)))
10902     (while (and (> n 0)
10903                 (gnus-summary-mark-article nil mark no-expire)
10904                 (zerop (gnus-summary-next-subject
10905                         (if backward -1 1)
10906                         (and gnus-summary-goto-unread
10907                              (not (eq gnus-summary-goto-unread 'never)))
10908                         t)))
10909       (setq n (1- n)))
10910     (when (/= 0 n)
10911       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10912     (gnus-summary-recenter)
10913     (gnus-summary-position-point)
10914     (gnus-set-mode-line 'summary)
10915     n))
10916
10917 (defun gnus-summary-mark-article-as-read (mark)
10918   "Mark the current article quickly as read with MARK."
10919   (let ((article (gnus-summary-article-number)))
10920     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10921     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10922     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10923     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10924     (push (cons article mark) gnus-newsgroup-reads)
10925     ;; Possibly remove from cache, if that is used.
10926     (when gnus-use-cache
10927       (gnus-cache-enter-remove-article article))
10928     ;; Allow the backend to change the mark.
10929     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10930     ;; Check for auto-expiry.
10931     (when (and gnus-newsgroup-auto-expire
10932                (memq mark gnus-auto-expirable-marks))
10933       (setq mark gnus-expirable-mark)
10934       ;; Let the backend know about the mark change.
10935       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10936       (push article gnus-newsgroup-expirable))
10937     ;; Set the mark in the buffer.
10938     (gnus-summary-update-mark mark 'unread)
10939     t))
10940
10941 (defun gnus-summary-mark-article-as-unread (mark)
10942   "Mark the current article quickly as unread with MARK."
10943   (let* ((article (gnus-summary-article-number))
10944          (old-mark (gnus-summary-article-mark article)))
10945     ;; Allow the backend to change the mark.
10946     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10947     (if (eq mark old-mark)
10948         t
10949       (if (<= article 0)
10950           (progn
10951             (gnus-error 1 "Can't mark negative article numbers")
10952             nil)
10953         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10954         (setq gnus-newsgroup-spam-marked
10955               (delq article gnus-newsgroup-spam-marked))
10956         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10957         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10958         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10959         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10960         (cond ((= mark gnus-ticked-mark)
10961                (setq gnus-newsgroup-marked
10962                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10963                                               article)))
10964               ((= mark gnus-spam-mark)
10965                (setq gnus-newsgroup-spam-marked
10966                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10967                                               article)))
10968               ((= mark gnus-dormant-mark)
10969                (setq gnus-newsgroup-dormant
10970                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10971                                               article)))
10972               (t
10973                (setq gnus-newsgroup-unreads
10974                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10975                                               article))))
10976         (gnus-alist-pull article gnus-newsgroup-reads)
10977
10978         ;; See whether the article is to be put in the cache.
10979         (and gnus-use-cache
10980              (vectorp (gnus-summary-article-header article))
10981              (save-excursion
10982                (gnus-cache-possibly-enter-article
10983                 gnus-newsgroup-name article
10984                 (= mark gnus-ticked-mark)
10985                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10986
10987         ;; Fix the mark.
10988         (gnus-summary-update-mark mark 'unread)
10989         t))))
10990
10991 (defun gnus-summary-mark-article (&optional article mark no-expire)
10992   "Mark ARTICLE with MARK.  MARK can be any character.
10993 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10994 `??' (dormant) and `?E' (expirable).
10995 If MARK is nil, then the default character `?r' is used.
10996 If ARTICLE is nil, then the article on the current line will be
10997 marked.
10998 If NO-EXPIRE, auto-expiry will be inhibited."
10999   ;; The mark might be a string.
11000   (when (stringp mark)
11001     (setq mark (aref mark 0)))
11002   ;; If no mark is given, then we check auto-expiring.
11003   (when (null mark)
11004     (setq mark gnus-del-mark))
11005   (when (and (not no-expire)
11006              gnus-newsgroup-auto-expire
11007              (memq mark gnus-auto-expirable-marks))
11008     (setq mark gnus-expirable-mark))
11009   (let ((article (or article (gnus-summary-article-number)))
11010         (old-mark (gnus-summary-article-mark article)))
11011     ;; Allow the backend to change the mark.
11012     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11013     (if (eq mark old-mark)
11014         t
11015       (unless article
11016         (error "No article on current line"))
11017       (if (not (if (or (= mark gnus-unread-mark)
11018                        (= mark gnus-ticked-mark)
11019                        (= mark gnus-spam-mark)
11020                        (= mark gnus-dormant-mark))
11021                    (gnus-mark-article-as-unread article mark)
11022                  (gnus-mark-article-as-read article mark)))
11023           t
11024         ;; See whether the article is to be put in the cache.
11025         (and gnus-use-cache
11026              (not (= mark gnus-canceled-mark))
11027              (vectorp (gnus-summary-article-header article))
11028              (save-excursion
11029                (gnus-cache-possibly-enter-article
11030                 gnus-newsgroup-name article
11031                 (= mark gnus-ticked-mark)
11032                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11033
11034         (when (gnus-summary-goto-subject article nil t)
11035           (let ((inhibit-read-only t))
11036             (gnus-summary-show-thread)
11037             ;; Fix the mark.
11038             (gnus-summary-update-mark mark 'unread)
11039             t))))))
11040
11041 (defun gnus-summary-update-secondary-mark (article)
11042   "Update the secondary (read, process, cache) mark."
11043   (gnus-summary-update-mark
11044    (cond ((memq article gnus-newsgroup-processable)
11045           gnus-process-mark)
11046          ((memq article gnus-newsgroup-cached)
11047           gnus-cached-mark)
11048          ((memq article gnus-newsgroup-replied)
11049           gnus-replied-mark)
11050          ((memq article gnus-newsgroup-forwarded)
11051           gnus-forwarded-mark)
11052          ((memq article gnus-newsgroup-saved)
11053           gnus-saved-mark)
11054          ((memq article gnus-newsgroup-unseen)
11055           gnus-unseen-mark)
11056          (t gnus-no-mark))
11057    'replied)
11058   (when (gnus-visual-p 'summary-highlight 'highlight)
11059     (gnus-summary-highlight-line)
11060     (gnus-run-hooks 'gnus-summary-update-hook))
11061   t)
11062
11063 (defun gnus-summary-update-download-mark (article)
11064   "Update the download mark."
11065   (gnus-summary-update-mark
11066    (cond ((memq article gnus-newsgroup-undownloaded)
11067           gnus-undownloaded-mark)
11068          (gnus-newsgroup-agentized
11069           gnus-downloaded-mark)
11070          (t
11071           gnus-no-mark))
11072    'download)
11073   (gnus-summary-update-line t)
11074   t)
11075
11076 (defun gnus-summary-update-mark (mark type)
11077   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11078         (inhibit-read-only t))
11079     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11080     (when forward
11081       (when (looking-at "\r")
11082         (incf forward))
11083       (when (<= (+ forward (point)) (point-max))
11084         ;; Go to the right position on the line.
11085         (goto-char (+ forward (point)))
11086         ;; Replace the old mark with the new mark.
11087         (let ((to-insert
11088                (mm-subst-char-in-string
11089                 (char-after) mark
11090                 (buffer-substring (point) (1+ (point))))))
11091           (delete-region (point) (1+ (point)))
11092           (insert to-insert))
11093         ;; Optionally update the marks by some user rule.
11094         (when (eq type 'unread)
11095           (gnus-data-set-mark
11096            (gnus-data-find (gnus-summary-article-number)) mark)
11097           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11098
11099 (defun gnus-mark-article-as-read (article &optional mark)
11100   "Enter ARTICLE in the pertinent lists and remove it from others."
11101   ;; Make the article expirable.
11102   (let ((mark (or mark gnus-del-mark)))
11103     (setq gnus-newsgroup-expirable
11104           (if (= mark gnus-expirable-mark)
11105               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11106             (delq article gnus-newsgroup-expirable)))
11107     ;; Remove from unread and marked lists.
11108     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11109     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11110     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11111     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11112     (push (cons article mark) gnus-newsgroup-reads)
11113     ;; Possibly remove from cache, if that is used.
11114     (when gnus-use-cache
11115       (gnus-cache-enter-remove-article article))
11116     t))
11117
11118 (defun gnus-mark-article-as-unread (article &optional mark)
11119   "Enter ARTICLE in the pertinent lists and remove it from others."
11120   (let ((mark (or mark gnus-ticked-mark)))
11121     (if (<= article 0)
11122         (progn
11123           (gnus-error 1 "Can't mark negative article numbers")
11124           nil)
11125       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11126             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11127             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11128             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11129             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11130
11131       ;; Unsuppress duplicates?
11132       (when gnus-suppress-duplicates
11133         (gnus-dup-unsuppress-article article))
11134
11135       (cond ((= mark gnus-ticked-mark)
11136              (setq gnus-newsgroup-marked
11137                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11138             ((= mark gnus-spam-mark)
11139              (setq gnus-newsgroup-spam-marked
11140                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11141                                             article)))
11142             ((= mark gnus-dormant-mark)
11143              (setq gnus-newsgroup-dormant
11144                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11145             (t
11146              (setq gnus-newsgroup-unreads
11147                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11148       (gnus-alist-pull article gnus-newsgroup-reads)
11149       t)))
11150
11151 (defun gnus-summary-tick-article-forward (n)
11152   "Tick N articles forwards.
11153 If N is negative, tick backwards instead.
11154 The difference between N and the number of articles ticked is returned."
11155   (interactive "p")
11156   (gnus-summary-mark-forward n gnus-ticked-mark))
11157
11158 (defun gnus-summary-tick-article-backward (n)
11159   "Tick N articles backwards.
11160 The difference between N and the number of articles ticked is returned."
11161   (interactive "p")
11162   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11163
11164 (defun gnus-summary-tick-article (&optional article clear-mark)
11165   "Mark current article as unread.
11166 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11167 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11168   (interactive)
11169   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11170                                        gnus-ticked-mark)))
11171
11172 (defun gnus-summary-mark-as-read-forward (n)
11173   "Mark N articles as read forwards.
11174 If N is negative, mark backwards instead.
11175 The difference between N and the actual number of articles marked is
11176 returned."
11177   (interactive "p")
11178   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11179
11180 (defun gnus-summary-mark-as-read-backward (n)
11181   "Mark the N articles as read backwards.
11182 The difference between N and the actual number of articles marked is
11183 returned."
11184   (interactive "p")
11185   (gnus-summary-mark-forward
11186    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11187
11188 (defun gnus-summary-mark-as-read (&optional article mark)
11189   "Mark current article as read.
11190 ARTICLE specifies the article to be marked as read.
11191 MARK specifies a string to be inserted at the beginning of the line."
11192   (gnus-summary-mark-article article mark))
11193
11194 (defun gnus-summary-clear-mark-forward (n)
11195   "Clear marks from N articles forward.
11196 If N is negative, clear backward instead.
11197 The difference between N and the number of marks cleared is returned."
11198   (interactive "p")
11199   (gnus-summary-mark-forward n gnus-unread-mark))
11200
11201 (defun gnus-summary-clear-mark-backward (n)
11202   "Clear marks from N articles backward.
11203 The difference between N and the number of marks cleared is returned."
11204   (interactive "p")
11205   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11206
11207 (defun gnus-summary-mark-unread-as-read ()
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-read-mark)))
11211
11212 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11213   "Intended to be used by `gnus-mark-article-hook'."
11214   (let ((mark (gnus-summary-article-mark)))
11215     (when (or (gnus-unread-mark-p mark)
11216               (gnus-read-mark-p mark))
11217       (gnus-summary-mark-article gnus-current-article
11218                                  (or new-mark gnus-read-mark)))))
11219
11220 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11221   "Intended to be used by `gnus-mark-article-hook'."
11222   (let ((mark (gnus-summary-article-mark)))
11223     (when (or (gnus-unread-mark-p mark)
11224               (gnus-read-mark-p mark))
11225       (gnus-summary-mark-article (gnus-summary-article-number)
11226                                  (or new-mark gnus-read-mark)))))
11227
11228 (defun gnus-summary-mark-unread-as-ticked ()
11229   "Intended to be used by `gnus-mark-article-hook'."
11230   (when (memq gnus-current-article gnus-newsgroup-unreads)
11231     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11232
11233 (defun gnus-summary-mark-region-as-read (point mark all)
11234   "Mark all unread articles between point and mark as read.
11235 If given a prefix, mark all articles between point and mark as read,
11236 even ticked and dormant ones."
11237   (interactive "r\nP")
11238   (save-excursion
11239     (let (article)
11240       (goto-char point)
11241       (beginning-of-line)
11242       (while (and
11243               (< (point) mark)
11244               (progn
11245                 (when (or all
11246                           (memq (setq article (gnus-summary-article-number))
11247                                 gnus-newsgroup-unreads))
11248                   (gnus-summary-mark-article article gnus-del-mark))
11249                 t)
11250               (gnus-summary-find-next))))))
11251
11252 (defun gnus-summary-mark-below (score mark)
11253   "Mark articles with score less than SCORE with MARK."
11254   (interactive "P\ncMark: ")
11255   (setq score (if score
11256                   (prefix-numeric-value score)
11257                 (or gnus-summary-default-score 0)))
11258   (with-current-buffer gnus-summary-buffer
11259     (goto-char (point-min))
11260     (while
11261         (progn
11262           (and (< (gnus-summary-article-score) score)
11263                (gnus-summary-mark-article nil mark))
11264           (gnus-summary-find-next)))))
11265
11266 (defun gnus-summary-kill-below (&optional score)
11267   "Mark articles with score below SCORE as read."
11268   (interactive "P")
11269   (gnus-summary-mark-below score gnus-killed-mark))
11270
11271 (defun gnus-summary-clear-above (&optional score)
11272   "Clear all marks from articles with score above SCORE."
11273   (interactive "P")
11274   (gnus-summary-mark-above score gnus-unread-mark))
11275
11276 (defun gnus-summary-tick-above (&optional score)
11277   "Tick all articles with score above SCORE."
11278   (interactive "P")
11279   (gnus-summary-mark-above score gnus-ticked-mark))
11280
11281 (defun gnus-summary-mark-above (score mark)
11282   "Mark articles with score over SCORE with MARK."
11283   (interactive "P\ncMark: ")
11284   (setq score (if score
11285                   (prefix-numeric-value score)
11286                 (or gnus-summary-default-score 0)))
11287   (with-current-buffer gnus-summary-buffer
11288     (goto-char (point-min))
11289     (while (and (progn
11290                   (when (> (gnus-summary-article-score) score)
11291                     (gnus-summary-mark-article nil mark))
11292                   t)
11293                 (gnus-summary-find-next)))))
11294
11295 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11296 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11297 (defun gnus-summary-limit-include-expunged (&optional no-error)
11298   "Display all the hidden articles that were expunged for low scores."
11299   (interactive)
11300   (let ((inhibit-read-only t))
11301     (let ((scored gnus-newsgroup-scored)
11302           headers h)
11303       (while scored
11304         (unless (gnus-summary-article-header (caar scored))
11305           (and (setq h (gnus-number-to-header (caar scored)))
11306                (< (cdar scored) gnus-summary-expunge-below)
11307                (push h headers)))
11308         (setq scored (cdr scored)))
11309       (if (not headers)
11310           (when (not no-error)
11311             (error "No expunged articles hidden"))
11312         (goto-char (point-min))
11313         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11314         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11315         (dolist (x headers)
11316           (push (mail-header-number x) gnus-newsgroup-limit))
11317         (gnus-summary-prepare-unthreaded (nreverse headers))
11318         (goto-char (point-min))
11319         (gnus-summary-position-point)
11320         t))))
11321
11322 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11323   "Mark all unread articles in this newsgroup as read.
11324 If prefix argument ALL is non-nil, ticked and dormant articles will
11325 also be marked as read.
11326 If QUIETLY is non-nil, no questions will be asked.
11327
11328 If TO-HERE is non-nil, it should be a point in the buffer.  All
11329 articles before (after, if REVERSE is set) this point will be marked
11330 as read.
11331
11332 Note that this function will only catch up the unread article
11333 in the current summary buffer limitation.
11334
11335 The number of articles marked as read is returned."
11336   (interactive "P")
11337   (prog1
11338       (save-excursion
11339         (when (or quietly
11340                   (not gnus-interactive-catchup) ;Without confirmation?
11341                   gnus-expert-user
11342                   (gnus-y-or-n-p
11343                    (if all
11344                        "Mark absolutely all articles as read? "
11345                      "Mark all unread articles as read? ")))
11346           (if (and not-mark
11347                    (not gnus-newsgroup-adaptive)
11348                    (not gnus-newsgroup-auto-expire)
11349                    (not gnus-suppress-duplicates)
11350                    (or (not gnus-use-cache)
11351                        (eq gnus-use-cache 'passive)))
11352               (progn
11353                 (when all
11354                   (setq gnus-newsgroup-marked nil
11355                         gnus-newsgroup-spam-marked nil
11356                         gnus-newsgroup-dormant nil))
11357                 (setq gnus-newsgroup-unreads
11358                       (gnus-sorted-nunion
11359                        (gnus-sorted-intersection gnus-newsgroup-unreads
11360                                                  gnus-newsgroup-downloadable)
11361                        (gnus-sorted-difference gnus-newsgroup-unfetched
11362                                                gnus-newsgroup-cached))))
11363             ;; We actually mark all articles as canceled, which we
11364             ;; have to do when using auto-expiry or adaptive scoring.
11365             (gnus-summary-show-all-threads)
11366             (if (and to-here reverse)
11367                 (progn
11368                   (goto-char to-here)
11369                   (gnus-summary-mark-current-read-and-unread-as-read
11370                    gnus-catchup-mark)
11371                   (while (gnus-summary-find-next (not all))
11372                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11373               (when (gnus-summary-first-subject (not all))
11374                 (while (and
11375                         (if to-here (< (point) to-here) t)
11376                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11377                         (gnus-summary-find-next (not all))))))
11378             (gnus-set-mode-line 'summary))
11379           t))
11380     (gnus-summary-position-point)))
11381
11382 (defun gnus-summary-catchup-to-here (&optional all)
11383   "Mark all unticked articles before the current one as read.
11384 If ALL is non-nil, also mark ticked and dormant articles as read."
11385   (interactive "P")
11386   (save-excursion
11387     (gnus-save-hidden-threads
11388       (let ((beg (point)))
11389         ;; We check that there are unread articles.
11390         (when (or all (gnus-summary-find-prev))
11391           (gnus-summary-catchup all t beg)))))
11392   (gnus-summary-position-point))
11393
11394 (defun gnus-summary-catchup-from-here (&optional all)
11395   "Mark all unticked articles after (and including) the current one as read.
11396 If ALL is non-nil, also mark ticked and dormant articles as read."
11397   (interactive "P")
11398   (save-excursion
11399     (gnus-save-hidden-threads
11400       (let ((beg (point)))
11401         ;; We check that there are unread articles.
11402         (when (or all (gnus-summary-find-next))
11403           (gnus-summary-catchup all t beg nil t)))))
11404   (gnus-summary-position-point))
11405
11406 (defun gnus-summary-catchup-all (&optional quietly)
11407   "Mark all articles in this newsgroup as read.
11408 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11409 instead, which marks only unread articles as read."
11410   (interactive "P")
11411   (gnus-summary-catchup t quietly))
11412
11413 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11414   "Mark all unread articles in this group as read, then exit.
11415 If prefix argument ALL is non-nil, all articles are marked as read.
11416 If QUIETLY is non-nil, no questions will be asked."
11417   (interactive "P")
11418   (when (gnus-summary-catchup all quietly nil 'fast)
11419     ;; Select next newsgroup or exit.
11420     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11421              (eq gnus-auto-select-next 'quietly))
11422         (gnus-summary-next-group nil)
11423       (gnus-summary-exit))))
11424
11425 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11426   "Mark all articles in this newsgroup as read, and then exit.
11427 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11428 instead, which marks only unread articles as read."
11429   (interactive "P")
11430   (gnus-summary-catchup-and-exit t quietly))
11431
11432 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11433   "Mark all articles in this group as read and select the next group.
11434 If given a prefix, mark all articles, unread as well as ticked, as
11435 read."
11436   (interactive "P")
11437   (save-excursion
11438     (gnus-summary-catchup all))
11439   (gnus-summary-next-group))
11440
11441 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11442   "Mark all articles in this group as read and select the previous group.
11443 If given a prefix, mark all articles, unread as well as ticked, as
11444 read."
11445   (interactive "P")
11446   (save-excursion
11447     (gnus-summary-catchup all))
11448   (gnus-summary-next-group nil nil t))
11449
11450 ;;;
11451 ;;; with article
11452 ;;;
11453
11454 (defmacro gnus-with-article (article &rest forms)
11455   "Select ARTICLE and perform FORMS in the original article buffer.
11456 Then replace the article with the result."
11457   `(progn
11458      ;; We don't want the article to be marked as read.
11459      (let (gnus-mark-article-hook)
11460        (gnus-summary-select-article t t nil ,article))
11461      (set-buffer gnus-original-article-buffer)
11462      ,@forms
11463      (if (not (gnus-check-backend-function
11464                'request-replace-article (car gnus-article-current)))
11465          (gnus-message 5 "Read-only group; not replacing")
11466        (unless (gnus-request-replace-article
11467                 ,article (car gnus-article-current)
11468                 (current-buffer) t)
11469          (error "Couldn't replace article")))
11470      ;; The cache and backlog have to be flushed somewhat.
11471      (when gnus-keep-backlog
11472        (gnus-backlog-remove-article
11473         (car gnus-article-current) (cdr gnus-article-current)))
11474      (when gnus-use-cache
11475        (gnus-cache-update-article
11476         (car gnus-article-current) (cdr gnus-article-current)))))
11477
11478 (put 'gnus-with-article 'lisp-indent-function 1)
11479 (put 'gnus-with-article 'edebug-form-spec '(form body))
11480
11481 ;; Thread-based commands.
11482
11483 (defun gnus-summary-articles-in-thread (&optional article)
11484   "Return a list of all articles in the current thread.
11485 If ARTICLE is non-nil, return all articles in the thread that starts
11486 with that article."
11487   (let* ((article (or article (gnus-summary-article-number)))
11488          (data (gnus-data-find-list article))
11489          (top-level (gnus-data-level (car data)))
11490          (top-subject
11491           (cond ((null gnus-thread-operation-ignore-subject)
11492                  (gnus-simplify-subject-re
11493                   (mail-header-subject (gnus-data-header (car data)))))
11494                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11495                  (gnus-simplify-subject-fuzzy
11496                   (mail-header-subject (gnus-data-header (car data)))))
11497                 (t nil)))
11498          (end-point (save-excursion
11499                       (goto-char (gnus-data-pos (car data)))
11500                       (if (gnus-summary-go-to-next-thread)
11501                           (point) (point-max))))
11502          articles)
11503     (while (and data
11504                 (< (gnus-data-pos (car data)) end-point))
11505       (when (or (not top-subject)
11506                 (string= top-subject
11507                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11508                              (gnus-simplify-subject-fuzzy
11509                               (mail-header-subject
11510                                (gnus-data-header (car data))))
11511                            (gnus-simplify-subject-re
11512                             (mail-header-subject
11513                              (gnus-data-header (car data)))))))
11514         (push (gnus-data-number (car data)) articles))
11515       (unless (and (setq data (cdr data))
11516                    (> (gnus-data-level (car data)) top-level))
11517         (setq data nil)))
11518     ;; Return the list of articles.
11519     (nreverse articles)))
11520
11521 (defun gnus-summary-rethread-current ()
11522   "Rethread the thread the current article is part of."
11523   (interactive)
11524   (let* ((gnus-show-threads t)
11525          (article (gnus-summary-article-number))
11526          (id (mail-header-id (gnus-summary-article-header)))
11527          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11528     (unless id
11529       (error "No article on the current line"))
11530     (gnus-rebuild-thread id)
11531     (gnus-summary-goto-subject article)))
11532
11533 (defun gnus-summary-reparent-thread ()
11534   "Make the current article child of the marked (or previous) article.
11535
11536 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11537 is non-nil or the Subject: of both articles are the same."
11538   (interactive)
11539   (unless (not (gnus-group-read-only-p))
11540     (error "The current newsgroup does not support article editing"))
11541   (unless (<= (length gnus-newsgroup-processable) 1)
11542     (error "No more than one article may be marked"))
11543   (let ((child (gnus-summary-article-number))
11544         ;; First grab the marked article, otherwise one line up.
11545         (parent (if (not (null gnus-newsgroup-processable))
11546                     (car gnus-newsgroup-processable)
11547                   (save-excursion
11548                     (if (eq (forward-line -1) 0)
11549                         (gnus-summary-article-number)
11550                       (error "Beginning of summary buffer"))))))
11551     (gnus-summary-reparent-children parent (list child))))
11552
11553 (defun gnus-summary-reparent-children (parent children)
11554   "Make PARENT the parent of CHILDREN.
11555 When called interactively, PARENT is the current article and CHILDREN
11556 are the process-marked articles."
11557   (interactive
11558    (list (gnus-summary-article-number)
11559          (gnus-summary-work-articles nil)))
11560   (dolist (child children)
11561     (save-window-excursion
11562       (let ((gnus-article-buffer " *reparent*"))
11563         (unless (not (eq parent child))
11564           (error "An article may not be self-referential"))
11565         (let ((message-id (mail-header-id
11566                            (gnus-summary-article-header parent))))
11567           (unless (and message-id (not (equal message-id "")))
11568             (error "No message-id in desired parent"))
11569           (gnus-with-article child
11570             (save-restriction
11571               (goto-char (point-min))
11572               (message-narrow-to-head)
11573               (if (re-search-forward "^References: " nil t)
11574                   (progn
11575                     (re-search-forward "^[^ \t]" nil t)
11576                     (forward-line -1)
11577                     (end-of-line)
11578                     (insert " " message-id))
11579                 (insert "References: " message-id "\n"))))
11580           (set-buffer gnus-summary-buffer)
11581           (gnus-summary-unmark-all-processable)
11582           (gnus-summary-update-article child)
11583           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11584             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11585           (gnus-summary-rethread-current)
11586           (gnus-message 3 "Article %d is now the child of article %d"
11587                         child parent))))))
11588
11589 (defun gnus-summary-toggle-threads (&optional arg)
11590   "Toggle showing conversation threads.
11591 If ARG is positive number, turn showing conversation threads on."
11592   (interactive "P")
11593   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11594     (setq gnus-show-threads
11595           (if (null arg) (not gnus-show-threads)
11596             (> (prefix-numeric-value arg) 0)))
11597     (gnus-summary-prepare)
11598     (gnus-summary-goto-subject current)
11599     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11600     (gnus-summary-position-point)))
11601
11602 (eval-and-compile
11603   (if (fboundp 'remove-overlays)
11604       (defalias 'gnus-remove-overlays 'remove-overlays)
11605     (defun gnus-remove-overlays (beg end name val)
11606       "Clear BEG and END of overlays whose property NAME has value VAL.
11607 For compatibility with XEmacs."
11608       (dolist (ov (gnus-overlays-in beg end))
11609         (when (eq (gnus-overlay-get ov name) val)
11610           (gnus-delete-overlay ov))))))
11611
11612 (defun gnus-summary-show-all-threads ()
11613   "Show all threads."
11614   (interactive)
11615   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11616   (gnus-summary-position-point))
11617
11618 (defsubst gnus-summary--inv (p)
11619   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11620
11621 (defun gnus-summary-show-thread ()
11622   "Show thread subtrees.
11623 Returns nil if no thread was there to be shown."
11624   (interactive)
11625   (let* ((orig (point))
11626          (end (point-at-eol))
11627          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11628          ;; Leave point at bol
11629          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11630          (eoi (when end
11631                 (if (fboundp 'next-single-char-property-change)
11632                     ;; Note: XEmacs version of n-s-c-p-c may return nil
11633                     (or (next-single-char-property-change end 'invisible)
11634                         (point-max))
11635                   (while (progn
11636                            (end-of-line 2)
11637                            (and (not (eobp))
11638                                 (eq (get-char-property (point) 'invisible)
11639                                     'gnus-sum))))
11640                   (point)))))
11641     (when eoi
11642       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11643       (goto-char orig)
11644       (gnus-summary-position-point)
11645       eoi)))
11646
11647 (defun gnus-summary-maybe-hide-threads ()
11648   "If requested, hide the threads that should be hidden."
11649   (when (and gnus-show-threads
11650              gnus-thread-hide-subtree)
11651     (gnus-summary-hide-all-threads
11652      (if (or (consp gnus-thread-hide-subtree)
11653              (functionp gnus-thread-hide-subtree))
11654          (gnus-make-predicate gnus-thread-hide-subtree)
11655        nil))))
11656
11657 ;;; Hiding predicates.
11658
11659 (defun gnus-article-unread-p (header)
11660   (memq (mail-header-number header) gnus-newsgroup-unreads))
11661
11662 (defun gnus-article-unseen-p (header)
11663   (memq (mail-header-number header) gnus-newsgroup-unseen))
11664
11665 (defun gnus-map-articles (predicate articles)
11666   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11667   (apply 'gnus-or (mapcar predicate
11668                           (mapcar (lambda (number)
11669                                     (gnus-summary-article-header number))
11670                                   articles))))
11671
11672 (defun gnus-summary-hide-all-threads (&optional predicate)
11673   "Hide all thread subtrees.
11674 If PREDICATE is supplied, threads that satisfy this predicate
11675 will not be hidden."
11676   (interactive)
11677   (save-excursion
11678     (goto-char (point-min))
11679     (let ((end nil)
11680           (count 0))
11681       (while (not end)
11682         (incf count)
11683         (when (zerop (mod count 1000))
11684           (message "Hiding all threads... %d" count))
11685         (when (or (not predicate)
11686                   (gnus-map-articles
11687                    predicate (gnus-summary-article-children)))
11688             (gnus-summary-hide-thread))
11689         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11690   (gnus-summary-position-point))
11691
11692 (defun gnus-summary-hide-thread ()
11693   "Hide thread subtrees.
11694 If PREDICATE is supplied, threads that satisfy this predicate
11695 will not be hidden.
11696 Returns nil if no threads were there to be hidden."
11697   (interactive)
11698   (beginning-of-line)
11699   (let ((start (point))
11700         (starteol (line-end-position))
11701         (article (gnus-summary-article-number)))
11702     ;; Go forward until either the buffer ends or the subthread ends.
11703     (when (and (not (eobp))
11704                (or (zerop (gnus-summary-next-thread 1 t))
11705                    (goto-char (point-max))))
11706       (if (and (> (point) start)
11707                ;; FIXME: this should actually search for a non-invisible \n.
11708                (search-backward "\n" start t))
11709           (progn
11710             (when (> (point) starteol)
11711               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11712               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11713                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11714                 (gnus-overlay-put ol 'evaporate t)))
11715             (gnus-summary-goto-subject article)
11716             (when (> start (point))
11717               (message "Hiding the thread moved us backwards, aborting!")
11718               (goto-char (point-max))))
11719         (goto-char start)
11720         nil))))
11721
11722 (defun gnus-summary-go-to-next-thread (&optional previous)
11723   "Go to the same level (or less) next thread.
11724 If PREVIOUS is non-nil, go to previous thread instead.
11725 Return the article number moved to, or nil if moving was impossible."
11726   (let ((level (gnus-summary-thread-level))
11727         (way (if previous -1 1))
11728         (beg (point)))
11729     (forward-line way)
11730     (while (and (not (eobp))
11731                 (< level (gnus-summary-thread-level)))
11732       (forward-line way))
11733     (if (eobp)
11734         (progn
11735           (goto-char beg)
11736           nil)
11737       (setq beg (point))
11738       (prog1
11739           (gnus-summary-article-number)
11740         (goto-char beg)))))
11741
11742 (defun gnus-summary-next-thread (n &optional silent)
11743   "Go to the same level next N'th thread.
11744 If N is negative, search backward instead.
11745 Returns the difference between N and the number of skips actually
11746 done.
11747
11748 If SILENT, don't output messages."
11749   (interactive "p")
11750   (let ((backward (< n 0))
11751         (n (abs n)))
11752     (while (and (> n 0)
11753                 (gnus-summary-go-to-next-thread backward))
11754       (decf n))
11755     (unless silent
11756       (gnus-summary-position-point))
11757     (when (and (not silent) (/= 0 n))
11758       (gnus-message 7 "No more threads"))
11759     n))
11760
11761 (defun gnus-summary-prev-thread (n)
11762   "Go to the same level previous N'th thread.
11763 Returns the difference between N and the number of skips actually
11764 done."
11765   (interactive "p")
11766   (gnus-summary-next-thread (- n)))
11767
11768 (defun gnus-summary-go-down-thread ()
11769   "Go down one level in the current thread."
11770   (let ((children (gnus-summary-article-children)))
11771     (when children
11772       (gnus-summary-goto-subject (car children)))))
11773
11774 (defun gnus-summary-go-up-thread ()
11775   "Go up one level in the current thread."
11776   (let ((parent (gnus-summary-article-parent)))
11777     (when parent
11778       (gnus-summary-goto-subject parent))))
11779
11780 (defun gnus-summary-down-thread (n)
11781   "Go down thread N steps.
11782 If N is negative, go up instead.
11783 Returns the difference between N and how many steps down that were
11784 taken."
11785   (interactive "p")
11786   (let ((up (< n 0))
11787         (n (abs n)))
11788     (while (and (> n 0)
11789                 (if up (gnus-summary-go-up-thread)
11790                   (gnus-summary-go-down-thread)))
11791       (setq n (1- n)))
11792     (gnus-summary-position-point)
11793     (when (/= 0 n)
11794       (gnus-message 7 "Can't go further"))
11795     n))
11796
11797 (defun gnus-summary-up-thread (n)
11798   "Go up thread N steps.
11799 If N is negative, go down instead.
11800 Returns the difference between N and how many steps down that were
11801 taken."
11802   (interactive "p")
11803   (gnus-summary-down-thread (- n)))
11804
11805 (defun gnus-summary-top-thread ()
11806   "Go to the top of the thread."
11807   (interactive)
11808   (while (gnus-summary-go-up-thread))
11809   (gnus-summary-article-number))
11810
11811 (defun gnus-summary-expire-thread ()
11812   "Mark articles under current thread as expired."
11813   (interactive)
11814   (gnus-summary-kill-thread 0))
11815
11816 (defun gnus-summary-kill-thread (&optional unmark)
11817   "Mark articles under current thread as read.
11818 If the prefix argument is positive, remove any kinds of marks.
11819 If the prefix argument is zero, mark thread as expired.
11820 If the prefix argument is negative, tick articles instead."
11821   (interactive "P")
11822   (when unmark
11823     (setq unmark (prefix-numeric-value unmark)))
11824   (let ((articles (gnus-summary-articles-in-thread))
11825         (hide (or (null unmark) (= unmark 0))))
11826     (save-excursion
11827       ;; Expand the thread.
11828       (gnus-summary-show-thread)
11829       ;; Mark all the articles.
11830       (while articles
11831         (gnus-summary-goto-subject (car articles))
11832         (cond ((null unmark)
11833                (gnus-summary-mark-article-as-read gnus-killed-mark))
11834               ((> unmark 0)
11835                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11836               ((= unmark 0)
11837                (gnus-summary-mark-article nil gnus-expirable-mark))
11838               (t
11839                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11840         (setq articles (cdr articles))))
11841     ;; Hide killed subtrees when hide is true.
11842     (and hide
11843          gnus-thread-hide-killed
11844          (gnus-summary-hide-thread))
11845     ;; If hide is t, go to next unread subject.
11846     (when hide
11847       ;; Go to next unread subject.
11848       (gnus-summary-next-subject 1 t)))
11849   (gnus-set-mode-line 'summary))
11850
11851 ;; Summary sorting commands
11852
11853 (defun gnus-summary-sort-by-number (&optional reverse)
11854   "Sort the summary buffer by article number.
11855 Argument REVERSE means reverse order."
11856   (interactive "P")
11857   (gnus-summary-sort 'number reverse))
11858
11859 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11860   "Sort the summary buffer by most recent article number.
11861 Argument REVERSE means reverse order."
11862   (interactive "P")
11863   (gnus-summary-sort 'most-recent-number reverse))
11864
11865 (defun gnus-summary-sort-by-random (&optional reverse)
11866   "Randomize the order in the summary buffer.
11867 Argument REVERSE means to randomize in reverse order."
11868   (interactive "P")
11869   (gnus-summary-sort 'random reverse))
11870
11871 (defun gnus-summary-sort-by-author (&optional reverse)
11872   "Sort the summary buffer by author name alphabetically.
11873 If `case-fold-search' is non-nil, case of letters is ignored.
11874 Argument REVERSE means reverse order."
11875   (interactive "P")
11876   (gnus-summary-sort 'author reverse))
11877
11878 (defun gnus-summary-sort-by-recipient (&optional reverse)
11879   "Sort the summary buffer by recipient name alphabetically.
11880 If `case-fold-search' is non-nil, case of letters is ignored.
11881 Argument REVERSE means reverse order."
11882   (interactive "P")
11883   (gnus-summary-sort 'recipient reverse))
11884
11885 (defun gnus-summary-sort-by-subject (&optional reverse)
11886   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11887 If `case-fold-search' is non-nil, case of letters is ignored.
11888 Argument REVERSE means reverse order."
11889   (interactive "P")
11890   (gnus-summary-sort 'subject reverse))
11891
11892 (defun gnus-summary-sort-by-date (&optional reverse)
11893   "Sort the summary buffer by date.
11894 Argument REVERSE means reverse order."
11895   (interactive "P")
11896   (gnus-summary-sort 'date reverse))
11897
11898 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11899   "Sort the summary buffer by most recent date.
11900 Argument REVERSE means reverse order."
11901   (interactive "P")
11902   (gnus-summary-sort 'most-recent-date reverse))
11903
11904 (defun gnus-summary-sort-by-score (&optional reverse)
11905   "Sort the summary buffer by score.
11906 Argument REVERSE means reverse order."
11907   (interactive "P")
11908   (gnus-summary-sort 'score reverse))
11909
11910 (defun gnus-summary-sort-by-lines (&optional reverse)
11911   "Sort the summary buffer by the number of lines.
11912 Argument REVERSE means reverse order."
11913   (interactive "P")
11914   (gnus-summary-sort 'lines reverse))
11915
11916 (defun gnus-summary-sort-by-chars (&optional reverse)
11917   "Sort the summary buffer by article length.
11918 Argument REVERSE means reverse order."
11919   (interactive "P")
11920   (gnus-summary-sort 'chars reverse))
11921
11922 (defun gnus-summary-sort-by-original (&optional reverse)
11923   "Sort the summary buffer using the default sorting method.
11924 Argument REVERSE means reverse order."
11925   (interactive "P")
11926   (let* ((inhibit-read-only t)
11927          (gnus-summary-prepare-hook nil))
11928     ;; We do the sorting by regenerating the threads.
11929     (gnus-summary-prepare)
11930     ;; Hide subthreads if needed.
11931     (gnus-summary-maybe-hide-threads)))
11932
11933 (defun gnus-summary-sort (predicate reverse)
11934   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11935   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11936          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11937          (gnus-thread-sort-functions
11938           (if (not reverse)
11939               thread
11940             `(lambda (t1 t2)
11941                (,thread t2 t1))))
11942          (gnus-sort-gathered-threads-function
11943           gnus-thread-sort-functions)
11944          (gnus-article-sort-functions
11945           (if (not reverse)
11946               article
11947             `(lambda (t1 t2)
11948                (,article t2 t1))))
11949          (inhibit-read-only t)
11950          (gnus-summary-prepare-hook nil))
11951     ;; We do the sorting by regenerating the threads.
11952     (gnus-summary-prepare)
11953     ;; Hide subthreads if needed.
11954     (gnus-summary-maybe-hide-threads)))
11955
11956 ;; Summary saving commands.
11957
11958 (defun gnus-summary-save-article (&optional n not-saved)
11959   "Save the current article using the default saver function.
11960 If N is a positive number, save the N next articles.
11961 If N is a negative number, save the N previous articles.
11962 If N is nil and any articles have been marked with the process mark,
11963 save those articles instead.
11964 The variable `gnus-default-article-saver' specifies the saver function.
11965
11966 If the optional second argument NOT-SAVED is non-nil, articles saved
11967 will not be marked as saved."
11968   (interactive "P")
11969   (require 'gnus-art)
11970   (let* ((articles (gnus-summary-work-articles n))
11971          (save-buffer (save-excursion
11972                         (nnheader-set-temp-buffer " *Gnus Save*")))
11973          (num (length articles))
11974          ;; Whether to save decoded articles or raw articles.
11975          (decode (when gnus-article-save-coding-system
11976                    (get gnus-default-article-saver :decode)))
11977          ;; When saving many articles in a single file, use the other
11978          ;; function to save articles other than the first one.
11979          (saver2 (get gnus-default-article-saver :function))
11980          (gnus-prompt-before-saving (if saver2
11981                                         t
11982                                       gnus-prompt-before-saving))
11983          (gnus-default-article-saver gnus-default-article-saver)
11984          header file)
11985     (dolist (article articles)
11986       (setq header (gnus-summary-article-header article))
11987       (if (not (vectorp header))
11988           ;; This is a pseudo-article.
11989           (if (assq 'name header)
11990               (gnus-copy-file (cdr (assq 'name header)))
11991             (gnus-message 1 "Article %d is unsavable" article))
11992         ;; This is a real article.
11993         (save-window-excursion
11994           (gnus-summary-select-article decode decode nil article)
11995           (gnus-summary-goto-subject article))
11996         (with-current-buffer save-buffer
11997           (erase-buffer)
11998           (insert-buffer-substring (if decode
11999                                        gnus-article-buffer
12000                                      gnus-original-article-buffer)))
12001         (setq file (gnus-article-save save-buffer file num))
12002         (gnus-summary-remove-process-mark article)
12003         (unless not-saved
12004           (gnus-summary-set-saved-mark article)))
12005       (when saver2
12006         (setq gnus-default-article-saver saver2
12007               saver2 nil)))
12008     (gnus-kill-buffer save-buffer)
12009     (gnus-summary-position-point)
12010     (gnus-set-mode-line 'summary)
12011     n))
12012
12013 (declare-function gnus-summary-save-in-pipe "gnus-art" (&optional command raw))
12014
12015 (defun gnus-summary-pipe-output (&optional n sym)
12016   "Pipe the current article to a subprocess.
12017 If N is a positive number, pipe the N next articles.
12018 If N is a negative number, pipe the N previous articles.
12019 If N is nil and any articles have been marked with the process mark,
12020 pipe those articles instead.
12021 The default command to which articles are piped is specified by the
12022 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
12023 will be prompted for the command.
12024
12025 The properties `:decode' and `:headers' that are put to the function
12026 symbol `gnus-summary-save-in-pipe' control whether this function
12027 decodes articles and what headers to keep (see the doc string for the
12028 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
12029 is neither omitted nor the symbol `r', force including all headers
12030 regardless of the `:headers' property.  If it is the symbol `r',
12031 articles that are not decoded and include all headers will be piped
12032 no matter what the properties `:decode' and `:headers' are."
12033   (interactive (gnus-interactive "P\ny"))
12034   (require 'gnus-art)
12035   (let* ((articles (gnus-summary-work-articles n))
12036          (result-buffer "*Shell Command Output*")
12037          (all-headers (not (memq sym '(nil r))))
12038          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
12039          (raw (eq sym 'r))
12040          (headers (get 'gnus-summary-save-in-pipe :headers))
12041          command result)
12042     (unless (numberp (car articles))
12043       (error "No article to pipe"))
12044     (setq command (gnus-read-shell-command
12045                    (concat "Shell command on "
12046                            (if (cdr articles)
12047                                (format "these %d articles" (length articles))
12048                              "this article")
12049                            ": ")
12050                    gnus-summary-pipe-output-default-command))
12051     (when (string-equal command "")
12052       (error "A command is required"))
12053     (when all-headers
12054       (put 'gnus-summary-save-in-pipe :headers nil))
12055     (unwind-protect
12056         (while articles
12057           (gnus-summary-goto-subject (pop articles))
12058           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12059           (when (and (get-buffer result-buffer)
12060                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12061             (setq result (concat result (with-current-buffer result-buffer
12062                                           (buffer-string))))))
12063       (put 'gnus-summary-save-in-pipe :headers headers))
12064     (unless (zerop (length result))
12065       (if (with-current-buffer (get-buffer-create result-buffer)
12066             (erase-buffer)
12067             (insert result)
12068             (prog1
12069                 (and (= (count-lines (point-min) (point)) 1)
12070                      (progn
12071                        (end-of-line 0)
12072                        (<= (current-column)
12073                            (window-width (minibuffer-window)))))
12074               (goto-char (point-min))))
12075           (message "%s" (substring result 0 -1))
12076         (message nil)
12077         (gnus-configure-windows 'pipe)))))
12078
12079 (defun gnus-summary-save-article-mail (&optional arg)
12080   "Append the current article to a Unix mail box file.
12081 If N is a positive number, save the N next articles.
12082 If N is a negative number, save the N previous articles.
12083 If N is nil and any articles have been marked with the process mark,
12084 save those articles instead."
12085   (interactive "P")
12086   (require 'gnus-art)
12087   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12088     (gnus-summary-save-article arg)))
12089
12090 (defun gnus-summary-save-article-rmail (&optional arg)
12091   "Append the current article to an rmail file.
12092 If N is a positive number, save the N next articles.
12093 If N is a negative number, save the N previous articles.
12094 If N is nil and any articles have been marked with the process mark,
12095 save those articles instead."
12096   (interactive "P")
12097   (require 'gnus-art)
12098   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12099     (gnus-summary-save-article arg)))
12100
12101 (defun gnus-summary-save-article-file (&optional arg)
12102   "Append the current article to a file.
12103 If N is a positive number, save the N next articles.
12104 If N is a negative number, save the N previous articles.
12105 If N is nil and any articles have been marked with the process mark,
12106 save those articles instead."
12107   (interactive "P")
12108   (require 'gnus-art)
12109   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12110     (gnus-summary-save-article arg)))
12111
12112 (defun gnus-summary-write-article-file (&optional arg)
12113   "Write the current article to a file, deleting the previous file.
12114 If N is a positive number, save the N next articles.
12115 If N is a negative number, save the N previous articles.
12116 If N is nil and any articles have been marked with the process mark,
12117 save those articles instead."
12118   (interactive "P")
12119   (require 'gnus-art)
12120   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12121     (gnus-summary-save-article arg)))
12122
12123 (defun gnus-summary-save-article-body-file (&optional arg)
12124   "Append the current article body to a file.
12125 If N is a positive number, save the N next articles.
12126 If N is a negative number, save the N previous articles.
12127 If N is nil and any articles have been marked with the process mark,
12128 save those articles instead."
12129   (interactive "P")
12130   (require 'gnus-art)
12131   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12132     (gnus-summary-save-article arg)))
12133
12134 (defun gnus-summary-write-article-body-file (&optional arg)
12135   "Write the current article body to a file, deleting the previous file.
12136 If N is a positive number, save the N next articles.
12137 If N is a negative number, save the N previous articles.
12138 If N is nil and any articles have been marked with the process mark,
12139 save those articles instead."
12140   (interactive "P")
12141   (require 'gnus-art)
12142   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12143     (gnus-summary-save-article arg)))
12144
12145 (defun gnus-summary-muttprint (&optional arg)
12146   "Print the current article using Muttprint.
12147 If N is a positive number, save the N next articles.
12148 If N is a negative number, save the N previous articles.
12149 If N is nil and any articles have been marked with the process mark,
12150 save those articles instead."
12151   (interactive "P")
12152   (require 'gnus-art)
12153   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12154     (gnus-summary-save-article arg t)))
12155
12156 (defun gnus-summary-pipe-message (program)
12157   "Pipe the current article through PROGRAM."
12158   (interactive "sProgram: ")
12159   (gnus-summary-select-article)
12160   (let ((mail-header-separator ""))
12161     (gnus-eval-in-buffer-window gnus-article-buffer
12162       (save-restriction
12163         (widen)
12164         (let ((start (window-start))
12165               (inhibit-read-only t))
12166           (message-pipe-buffer-body program)
12167           (set-window-start (get-buffer-window (current-buffer)) start))))))
12168
12169 (defun gnus-get-split-value (methods)
12170   "Return a value based on the split METHODS."
12171   (let (split-name method result match)
12172     (when methods
12173       (with-current-buffer gnus-original-article-buffer
12174         (save-restriction
12175           (nnheader-narrow-to-headers)
12176           (while (and methods (not split-name))
12177             (goto-char (point-min))
12178             (setq method (pop methods))
12179             (setq match (car method))
12180             (when (cond
12181                    ((stringp match)
12182                     ;; Regular expression.
12183                     (ignore-errors
12184                       (re-search-forward match nil t)))
12185                    ((functionp match)
12186                     ;; Function.
12187                     (save-restriction
12188                       (widen)
12189                       (setq result (funcall match gnus-newsgroup-name))))
12190                    ((consp match)
12191                     ;; Form.
12192                     (save-restriction
12193                       (widen)
12194                       (setq result (eval match)))))
12195               (setq split-name (cdr method))
12196               (cond ((stringp result)
12197                      (push (expand-file-name
12198                             result gnus-article-save-directory)
12199                            split-name))
12200                     ((consp result)
12201                      (setq split-name (append result split-name)))))))))
12202     (nreverse split-name)))
12203
12204 (defun gnus-valid-move-group-p (group)
12205   (and (symbolp group)
12206        (boundp group)
12207        (symbol-name group)
12208        (symbol-value group)
12209        (gnus-get-function (gnus-find-method-for-group
12210                            (symbol-name group)) 'request-accept-article t)))
12211
12212 (defun gnus-read-move-group-name (prompt default articles prefix)
12213   "Read a group name."
12214   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12215          (minibuffer-confirm-incomplete nil) ; XEmacs
12216          (prom
12217           (format "%s %s to"
12218                   prompt
12219                   (if (> (length articles) 1)
12220                       (format "these %d articles" (length articles))
12221                     "this article")))
12222          (to-newsgroup
12223           (cond
12224            ((null split-name)
12225             (gnus-group-completing-read
12226              prom
12227              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12228              nil prefix nil default))
12229            ((= 1 (length split-name))
12230             (gnus-group-completing-read
12231              prom
12232              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12233              nil prefix 'gnus-group-history (car split-name)))
12234            (t
12235             (gnus-completing-read
12236              prom (nreverse split-name) nil nil 'gnus-group-history))))
12237          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12238          encoded)
12239     (when to-newsgroup
12240       (if (or (string= to-newsgroup "")
12241               (string= to-newsgroup prefix))
12242           (setq to-newsgroup default))
12243       (unless to-newsgroup
12244         (error "No group name entered"))
12245       (setq encoded (mm-encode-coding-string
12246                      to-newsgroup
12247                      (gnus-group-name-charset to-method to-newsgroup)))
12248       (or (gnus-active encoded)
12249           (gnus-activate-group encoded nil nil to-method)
12250           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12251                                      to-newsgroup))
12252               (or (and (gnus-request-create-group encoded to-method)
12253                        (gnus-activate-group encoded nil nil to-method)
12254                        (gnus-subscribe-group encoded))
12255                   (error "Couldn't create group %s" to-newsgroup)))
12256           (error "No such group: %s" to-newsgroup))
12257       encoded)))
12258
12259 (defvar gnus-summary-save-parts-counter)
12260 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12261
12262 (defun gnus-summary-save-parts (type dir n &optional reverse)
12263   "Save parts matching TYPE to DIR.
12264 If REVERSE, save parts that do not match TYPE."
12265   (interactive
12266    (list (read-string "Save parts of type: "
12267                       (or (car gnus-summary-save-parts-type-history)
12268                           gnus-summary-save-parts-default-mime)
12269                       'gnus-summary-save-parts-type-history)
12270          (setq gnus-summary-save-parts-last-directory
12271                (read-directory-name "Save to directory: "
12272                                     gnus-summary-save-parts-last-directory
12273                                     nil t))
12274          current-prefix-arg))
12275   (gnus-summary-iterate n
12276     (let ((gnus-display-mime-function nil)
12277           gnus-article-prepare-hook
12278           gnus-article-decode-hook
12279           gnus-display-mime-function
12280           gnus-break-pages
12281           (gnus-inhibit-treatment t))
12282       (gnus-summary-select-article))
12283     (with-current-buffer gnus-article-buffer
12284       (let ((handles (or gnus-article-mime-handles
12285                          (mm-dissect-buffer nil gnus-article-loose-mime)
12286                          (and gnus-article-emulate-mime
12287                               (mm-uu-dissect))))
12288             (gnus-summary-save-parts-counter 1))
12289         (when handles
12290           (gnus-summary-save-parts-1 type dir handles reverse)
12291           (unless gnus-article-mime-handles ;; Don't destroy this case.
12292             (mm-destroy-parts handles)))))))
12293
12294 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12295   (if (stringp (car handle))
12296       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12297               (cdr handle))
12298     (when (if reverse
12299               (not (string-match type (mm-handle-media-type handle)))
12300             (string-match type (mm-handle-media-type handle)))
12301       (let ((file (expand-file-name
12302                    (gnus-map-function
12303                     mm-file-name-rewrite-functions
12304                     (file-name-nondirectory
12305                      (or
12306                       (mm-handle-filename handle)
12307                       (format "%s.%d.%d" gnus-newsgroup-name
12308                               (cdr gnus-article-current)
12309                               gnus-summary-save-parts-counter))))
12310                    dir)))
12311         (incf gnus-summary-save-parts-counter)
12312         (unless (file-exists-p file)
12313           (mm-save-part-to-file handle file))))))
12314
12315 ;; Summary extract commands
12316
12317 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12318   (let ((inhibit-read-only t)
12319         (article (gnus-summary-article-number))
12320         after-article b e)
12321     (unless (gnus-summary-goto-subject article)
12322       (error "No such article: %d" article))
12323     (gnus-summary-position-point)
12324     ;; If all commands are to be bunched up on one line, we collect
12325     ;; them here.
12326     (unless gnus-view-pseudos-separately
12327       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12328             files action)
12329         (while ps
12330           (setq action (cdr (assq 'action (car ps))))
12331           (setq files (list (cdr (assq 'name (car ps)))))
12332           (while (and ps (cdr ps)
12333                       (string= (or action "1")
12334                                (or (cdr (assq 'action (cadr ps))) "2")))
12335             (push (cdr (assq 'name (cadr ps))) files)
12336             (setcdr ps (cddr ps)))
12337           (when files
12338             (when (not (string-match "%s" action))
12339               (push " " files))
12340             (push " " files)
12341             (when (assq 'execute (car ps))
12342               (setcdr (assq 'execute (car ps))
12343                       (funcall (if (string-match "%s" action)
12344                                    'format 'concat)
12345                                action
12346                                (mapconcat
12347                                 (lambda (f)
12348                                   (if (equal f " ")
12349                                       f
12350                                     (shell-quote-argument f)))
12351                                 files " ")))))
12352           (setq ps (cdr ps)))))
12353     (if (and gnus-view-pseudos (not not-view))
12354         (while pslist
12355           (when (assq 'execute (car pslist))
12356             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12357                                   (eq gnus-view-pseudos 'not-confirm)))
12358           (setq pslist (cdr pslist)))
12359       (save-excursion
12360         (while pslist
12361           (setq after-article (or (cdr (assq 'article (car pslist)))
12362                                   (gnus-summary-article-number)))
12363           (gnus-summary-goto-subject after-article)
12364           (forward-line 1)
12365           (setq b (point))
12366           (insert "    " (file-name-nondirectory
12367                           (cdr (assq 'name (car pslist))))
12368                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12369           (setq e (point))
12370           (forward-line -1)             ; back to `b'
12371           (gnus-add-text-properties
12372            b (1- e) (list 'gnus-number gnus-reffed-article-number
12373                           gnus-mouse-face-prop gnus-mouse-face))
12374           (gnus-data-enter
12375            after-article gnus-reffed-article-number
12376            gnus-unread-mark b (car pslist) 0 (- e b))
12377           (setq gnus-newsgroup-unreads
12378                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12379                                          gnus-reffed-article-number))
12380           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12381           (setq pslist (cdr pslist)))))))
12382
12383 (defun gnus-pseudos< (p1 p2)
12384   (let ((c1 (cdr (assq 'action p1)))
12385         (c2 (cdr (assq 'action p2))))
12386     (and c1 c2 (string< c1 c2))))
12387
12388 (defun gnus-request-pseudo-article (props)
12389   (cond ((assq 'execute props)
12390          (gnus-execute-command (cdr (assq 'execute props)))))
12391   (let ((gnus-current-article (gnus-summary-article-number)))
12392     (gnus-run-hooks 'gnus-mark-article-hook)))
12393
12394 (defun gnus-execute-command (command &optional automatic)
12395   (save-excursion
12396     (gnus-article-setup-buffer)
12397     (set-buffer gnus-article-buffer)
12398     (setq buffer-read-only nil)
12399     (let ((command (if automatic command
12400                      (read-string "Command: " (cons command 0)))))
12401       (erase-buffer)
12402       (insert "$ " command "\n\n")
12403       (if gnus-view-pseudo-asynchronously
12404           (start-process "gnus-execute" (current-buffer) shell-file-name
12405                          shell-command-switch command)
12406         (call-process shell-file-name nil t nil
12407                       shell-command-switch command)))))
12408
12409 ;; Summary kill commands.
12410
12411 (defun gnus-summary-edit-global-kill (article)
12412   "Edit the \"global\" kill file."
12413   (interactive (list (gnus-summary-article-number)))
12414   (gnus-group-edit-global-kill article))
12415
12416 (defun gnus-summary-edit-local-kill ()
12417   "Edit a local kill file applied to the current newsgroup."
12418   (interactive)
12419   (setq gnus-current-headers (gnus-summary-article-header))
12420   (gnus-group-edit-local-kill
12421    (gnus-summary-article-number) gnus-newsgroup-name))
12422
12423 ;;; Header reading.
12424
12425 (defun gnus-read-header (id &optional header)
12426   "Read the headers of article ID and enter them into the Gnus system."
12427   (let ((group gnus-newsgroup-name)
12428         (gnus-override-method
12429          (or
12430           gnus-override-method
12431           (and (gnus-news-group-p gnus-newsgroup-name)
12432                (car (gnus-refer-article-methods)))))
12433         where)
12434     ;; First we check to see whether the header in question is already
12435     ;; fetched.
12436     (if (stringp id)
12437         ;; This is a Message-ID.
12438         (setq header (or header (gnus-id-to-header id)))
12439       ;; This is an article number.
12440       (setq header (or header (gnus-summary-article-header id))))
12441     (if (and header
12442              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12443         ;; We have found the header.
12444         header
12445       ;; We have to really fetch the header to this article.
12446       (with-current-buffer nntp-server-buffer
12447         (when (setq where (gnus-request-head id group))
12448           (nnheader-fold-continuation-lines)
12449           (goto-char (point-max))
12450           (insert ".\n")
12451           (goto-char (point-min))
12452           (insert "211 ")
12453           (princ (cond
12454                   ((numberp id) id)
12455                   ((cdr where) (cdr where))
12456                   (header (mail-header-number header))
12457                   (t gnus-reffed-article-number))
12458                  (current-buffer))
12459           (insert " Article retrieved.\n"))
12460         (if (or (not where)
12461                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12462             ()                          ; Malformed head.
12463           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12464             (when (and (bound-and-true-p gnus-registry-enabled)
12465                        (not (gnus-ephemeral-group-p (car where))))
12466               (gnus-registry-handle-action
12467                (mail-header-id header) nil
12468                (gnus-group-prefixed-name
12469                 (car where)
12470                 (or gnus-override-method (gnus-find-method-for-group group)))
12471                (mail-header-subject header)
12472                (mail-header-from header)))
12473             (when (and (stringp id)
12474                        (or
12475                         (not (string= (gnus-group-real-name group)
12476                                       (car where)))
12477                         (not (gnus-server-equal gnus-override-method
12478                                                 (gnus-group-method group)))))
12479               ;; If we fetched by Message-ID and the article came from
12480               ;; a different group (or server), we fudge some bogus
12481               ;; article numbers for this article.
12482               (mail-header-set-number header gnus-reffed-article-number))
12483             (with-current-buffer gnus-summary-buffer
12484               (decf gnus-reffed-article-number)
12485               (gnus-remove-header (mail-header-number header))
12486               (push header gnus-newsgroup-headers)
12487               (setq gnus-current-headers header)
12488               (push (mail-header-number header) gnus-newsgroup-limit)))
12489           header)))))
12490
12491 (defun gnus-remove-header (number)
12492   "Remove header NUMBER from `gnus-newsgroup-headers'."
12493   (if (and gnus-newsgroup-headers
12494            (= number (mail-header-number (car gnus-newsgroup-headers))))
12495       (pop gnus-newsgroup-headers)
12496     (let ((headers gnus-newsgroup-headers))
12497       (while (and (cdr headers)
12498                   (not (= number (mail-header-number (cadr headers)))))
12499         (pop headers))
12500       (when (cdr headers)
12501         (setcdr headers (cddr headers))))))
12502
12503 ;;;
12504 ;;; summary highlights
12505 ;;;
12506
12507 (defun gnus-highlight-selected-summary ()
12508   "Highlight selected article in summary buffer."
12509   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12510   (when gnus-summary-selected-face
12511     (save-excursion
12512       (let* ((beg (point-at-bol))
12513              (end (point-at-eol))
12514              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12515              (from (if (get-text-property beg gnus-mouse-face-prop)
12516                        beg
12517                      (or (next-single-property-change
12518                           beg gnus-mouse-face-prop nil end)
12519                          beg)))
12520              (to
12521               (if (= from end)
12522                   (- from 2)
12523                 (or (next-single-property-change
12524                      from gnus-mouse-face-prop nil end)
12525                     end))))
12526         ;; If no mouse-face prop on line we will have to = from = end,
12527         ;; so we highlight the entire line instead.
12528         (when (= (+ to 2) from)
12529           (setq from beg)
12530           (setq to end))
12531         (if gnus-newsgroup-selected-overlay
12532             ;; Move old overlay.
12533             (gnus-move-overlay
12534              gnus-newsgroup-selected-overlay from to (current-buffer))
12535           ;; Create new overlay.
12536           (gnus-overlay-put
12537            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12538            'face gnus-summary-selected-face))))))
12539
12540 (defvar gnus-summary-highlight-line-cached nil)
12541 (defvar gnus-summary-highlight-line-trigger nil)
12542
12543 (defun gnus-summary-highlight-line-0 ()
12544   (if (and (eq gnus-summary-highlight-line-trigger
12545                gnus-summary-highlight)
12546            gnus-summary-highlight-line-cached)
12547       gnus-summary-highlight-line-cached
12548     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12549           gnus-summary-highlight-line-cached
12550           (let* ((cond (list 'cond))
12551                  (c cond)
12552                  (list gnus-summary-highlight))
12553             (while list
12554               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12555                               nil))
12556               (setq c (cdr c)
12557                     list (cdr list)))
12558             (gnus-byte-compile (list 'lambda nil cond))))))
12559
12560 (defun gnus-summary-highlight-line ()
12561   "Highlight current line according to `gnus-summary-highlight'."
12562   (let* ((beg (point-at-bol))
12563          (article (or (gnus-summary-article-number) gnus-current-article))
12564          (score (or (cdr (assq article
12565                                gnus-newsgroup-scored))
12566                     gnus-summary-default-score 0))
12567          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12568          (inhibit-read-only t)
12569          (default gnus-summary-default-score)
12570          (default-high gnus-summary-default-high-score)
12571          (default-low gnus-summary-default-low-score)
12572          (uncached (and gnus-summary-use-undownloaded-faces
12573                         (memq article gnus-newsgroup-undownloaded)
12574                         (not (memq article gnus-newsgroup-cached)))))
12575     (let ((face (funcall (gnus-summary-highlight-line-0))))
12576       (unless (eq face (gnus-get-text-property-excluding-characters-with-faces beg 'face))
12577         (gnus-put-text-property-excluding-characters-with-faces
12578          beg (point-at-eol) 'face
12579          (setq face (if (boundp face) (symbol-value face) face)))
12580         (when gnus-summary-highlight-line-function
12581           (funcall gnus-summary-highlight-line-function article face))))))
12582
12583 (defun gnus-update-read-articles (group unread &optional compute)
12584   "Update the list of read articles in GROUP.
12585 UNREAD is a sorted list."
12586   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12587         (info (gnus-get-info group))
12588         (prev 1)
12589         read)
12590     (if (or (not info) (not active))
12591         ;; There is no info on this group if it was, in fact,
12592         ;; killed.  Gnus stores no information on killed groups, so
12593         ;; there's nothing to be done.
12594         ;; One could store the information somewhere temporarily,
12595         ;; perhaps...  Hmmm...
12596         ()
12597       ;; Remove any negative articles numbers.
12598       (while (and unread (< (car unread) 0))
12599         (setq unread (cdr unread)))
12600       ;; Remove any expired article numbers
12601       (while (and unread (< (car unread) (car active)))
12602         (setq unread (cdr unread)))
12603       ;; Compute the ranges of read articles by looking at the list of
12604       ;; unread articles.
12605       (while unread
12606         (when (/= (car unread) prev)
12607           (push (if (= prev (1- (car unread))) prev
12608                   (cons prev (1- (car unread))))
12609                 read))
12610         (setq prev (1+ (car unread)))
12611         (setq unread (cdr unread)))
12612       (when (<= prev (cdr active))
12613         (push (cons prev (cdr active)) read))
12614       (setq read (if (> (length read) 1) (nreverse read) read))
12615       (if compute
12616           read
12617         (save-excursion
12618           (let (setmarkundo)
12619             ;; Propagate the read marks to the backend.
12620             (when (and (gnus-method-option-p
12621                         (gnus-find-method-for-group group)
12622                         'server-marks)
12623                        (gnus-check-backend-function 'request-set-mark group))
12624               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12625                     (add (gnus-remove-from-range read (gnus-info-read info))))
12626                 (when (or add del)
12627                   (unless (gnus-check-group group)
12628                     (error "Can't open server for %s" group))
12629                   (gnus-request-set-mark
12630                    group (delq nil (list (if add (list add 'add '(read)))
12631                                          (if del (list del 'del '(read))))))
12632                   (setq setmarkundo
12633                         `(gnus-request-set-mark
12634                           ,group
12635                           ',(delq nil (list
12636                                        (if del (list del 'add '(read)))
12637                                        (if add (list add 'del '(read))))))))))
12638             (set-buffer gnus-group-buffer)
12639             (gnus-undo-register
12640               `(progn
12641                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12642                  (gnus-info-set-read ',info ',(gnus-info-read info))
12643                  (gnus-get-unread-articles-in-group ',info
12644                                                     (gnus-active ,group))
12645                  (gnus-group-update-group ,group t)
12646                  ,setmarkundo))))
12647         ;; Enter this list into the group info.
12648         (gnus-info-set-read info read)
12649         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12650         (gnus-get-unread-articles-in-group info (gnus-active group))
12651         t))))
12652
12653 (defun gnus-offer-save-summaries ()
12654   "Offer to save all active summary buffers."
12655   (let (buffers)
12656     ;; Go through all buffers and find all summaries.
12657     (dolist (buffer (buffer-list))
12658       (when (and (setq buffer (buffer-name buffer))
12659                  (string-match "Summary" buffer)
12660                  (with-current-buffer buffer
12661                    ;; We check that this is, indeed, a summary buffer.
12662                    (and (eq major-mode 'gnus-summary-mode)
12663                         ;; Also make sure this isn't bogus.
12664                         gnus-newsgroup-prepared
12665                         ;; Also make sure that this isn't a
12666                         ;; dead summary buffer.
12667                         (not gnus-dead-summary-mode))))
12668         (push buffer buffers)))
12669     ;; Go through all these summary buffers and offer to save them.
12670     (when buffers
12671       (save-excursion
12672         (if (eq gnus-interactive-exit 'quiet)
12673             (dolist (buffer buffers)
12674               (switch-to-buffer buffer)
12675               (gnus-summary-exit))
12676           (map-y-or-n-p
12677            "Update summary buffer %s? "
12678            (lambda (buf)
12679              (switch-to-buffer buf)
12680              (gnus-summary-exit))
12681            buffers))))))
12682
12683 (defun gnus-summary-setup-default-charset ()
12684   "Setup newsgroup default charset."
12685   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12686       (setq gnus-newsgroup-charset nil)
12687     (let* ((ignored-charsets
12688             (or gnus-newsgroup-ephemeral-ignored-charsets
12689                 (append
12690                  (and gnus-newsgroup-name
12691                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12692                  gnus-newsgroup-ignored-charsets))))
12693       (setq gnus-newsgroup-charset
12694             (or gnus-newsgroup-ephemeral-charset
12695                 (and gnus-newsgroup-name
12696                      (gnus-parameter-charset gnus-newsgroup-name))
12697                 gnus-default-charset))
12698       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12699            ignored-charsets))))
12700
12701 ;;;
12702 ;;; Mime Commands
12703 ;;;
12704
12705 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12706   "Display the current article buffer fully MIME-buttonized.
12707 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12708 treated as multipart/mixed."
12709   (interactive "P")
12710   (require 'gnus-art)
12711   (let ((gnus-unbuttonized-mime-types nil)
12712         (gnus-mime-display-multipart-as-mixed show-all-parts))
12713     (gnus-summary-show-article)))
12714
12715 (defun gnus-summary-repair-multipart (article)
12716   "Add a Content-Type header to a multipart article without one."
12717   (interactive (list (gnus-summary-article-number)))
12718   (gnus-with-article article
12719     (message-narrow-to-head)
12720     (message-remove-header "Mime-Version")
12721     (goto-char (point-max))
12722     (insert "Mime-Version: 1.0\n")
12723     (widen)
12724     (when (search-forward "\n--" nil t)
12725       (let ((separator (buffer-substring (point) (point-at-eol))))
12726         (message-narrow-to-head)
12727         (message-remove-header "Content-Type")
12728         (goto-char (point-max))
12729         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12730                         separator))
12731         (widen))))
12732   (let (gnus-mark-article-hook)
12733     (gnus-summary-select-article t t nil article)))
12734
12735 (defun gnus-summary-toggle-display-buttonized ()
12736   "Toggle the buttonizing of the article buffer."
12737   (interactive)
12738   (require 'gnus-art)
12739   (if (setq gnus-inhibit-mime-unbuttonizing
12740             (not gnus-inhibit-mime-unbuttonizing))
12741       (let ((gnus-unbuttonized-mime-types nil))
12742         (gnus-summary-show-article))
12743     (gnus-summary-show-article)))
12744
12745 ;;;
12746 ;;; Generic summary marking commands
12747 ;;;
12748
12749 (defvar gnus-summary-marking-alist
12750   '((read gnus-del-mark "d")
12751     (unread gnus-unread-mark "u")
12752     (ticked gnus-ticked-mark "!")
12753     (dormant gnus-dormant-mark "?")
12754     (expirable gnus-expirable-mark "e"))
12755   "An alist of names/marks/keystrokes.")
12756
12757 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12758 (defvar gnus-summary-mark-map)
12759
12760 (defun gnus-summary-make-all-marking-commands ()
12761   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12762   (dolist (elem gnus-summary-marking-alist)
12763     (apply 'gnus-summary-make-marking-command elem)))
12764
12765 (defun gnus-summary-make-marking-command (name mark keystroke)
12766   (let ((map (make-sparse-keymap)))
12767     (define-key gnus-summary-generic-mark-map keystroke map)
12768     (dolist (lway `((next "next" next nil "n")
12769                     (next-unread "next unread" next t "N")
12770                     (prev "previous" prev nil "p")
12771                     (prev-unread "previous unread" prev t "P")
12772                     (nomove "" nil nil ,keystroke)))
12773       (let ((func (gnus-summary-make-marking-command-1
12774                    mark (car lway) lway name)))
12775         (setq func (eval func))
12776         (define-key map (nth 4 lway) func)))))
12777
12778 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12779   `(defun ,(intern
12780             (format "gnus-summary-put-mark-as-%s%s"
12781                     name (if (eq way 'nomove)
12782                              ""
12783                            (concat "-" (symbol-name way)))))
12784      (n)
12785      ,(format
12786        "Mark the current article as %s%s.
12787 If N, the prefix, then repeat N times.
12788 If N is negative, move in reverse order.
12789 The difference between N and the actual number of articles marked is
12790 returned."
12791        name (cadr lway))
12792      (interactive "p")
12793      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12794
12795 (defun gnus-summary-generic-mark (n mark move unread)
12796   "Mark N articles with MARK."
12797   (unless (eq major-mode 'gnus-summary-mode)
12798     (error "This command can only be used in the summary buffer"))
12799   (gnus-summary-show-thread)
12800   (let ((nummove
12801          (cond
12802           ((eq move 'next) 1)
12803           ((eq move 'prev) -1)
12804           (t 0))))
12805     (if (zerop nummove)
12806         (setq n 1)
12807       (when (< n 0)
12808         (setq n (abs n)
12809               nummove (* -1 nummove))))
12810     (while (and (> n 0)
12811                 (gnus-summary-mark-article nil mark)
12812                 (zerop (gnus-summary-next-subject nummove unread t)))
12813       (setq n (1- n)))
12814     (when (/= 0 n)
12815       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12816     (gnus-summary-recenter)
12817     (gnus-summary-position-point)
12818     (gnus-set-mode-line 'summary)
12819     n))
12820
12821 (defun gnus-summary-insert-articles (articles)
12822   (when (setq articles
12823               (gnus-sorted-difference articles
12824                                       (mapcar (lambda (h)
12825                                                 (mail-header-number h))
12826                                               gnus-newsgroup-headers)))
12827     (setq gnus-newsgroup-headers
12828           (gnus-merge 'list
12829                       gnus-newsgroup-headers
12830                       (gnus-fetch-headers articles nil t)
12831                       'gnus-article-sort-by-number))
12832     (setq gnus-newsgroup-articles
12833           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12834     ;; Suppress duplicates?
12835     (when gnus-suppress-duplicates
12836       (gnus-dup-suppress-articles))
12837
12838     (if (and gnus-fetch-old-headers
12839              (eq gnus-headers-retrieved-by 'nov))
12840         ;; We might want to build some more threads first.
12841         (if (eq gnus-fetch-old-headers 'invisible)
12842             (gnus-build-all-threads)
12843           (gnus-build-old-threads))
12844       ;; Mark the inserted articles that are unread as unread.
12845       (setq gnus-newsgroup-unreads
12846             (gnus-sorted-nunion
12847              gnus-newsgroup-unreads
12848              (gnus-sorted-nintersection
12849               (gnus-list-of-unread-articles gnus-newsgroup-name)
12850               articles)))
12851       ;; Mark the inserted articles as selected so that the information
12852       ;; of the marks having been changed by a user may be updated when
12853       ;; exiting this group.  See `gnus-summary-update-info'.
12854       (dolist (art articles)
12855         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12856     ;; Let the Gnus agent mark articles as read.
12857     (when gnus-agent
12858       (gnus-agent-get-undownloaded-list))
12859     ;; Remove list identifiers from subject
12860     (gnus-summary-remove-list-identifiers)
12861     ;; First and last article in this newsgroup.
12862     (when gnus-newsgroup-headers
12863       (setq gnus-newsgroup-begin
12864             (mail-header-number (car gnus-newsgroup-headers))
12865             gnus-newsgroup-end
12866             (mail-header-number
12867              (gnus-last-element gnus-newsgroup-headers))))
12868     (when gnus-use-scoring
12869       (gnus-possibly-score-headers))))
12870
12871 (defun gnus-summary-insert-old-articles (&optional all)
12872   "Insert all old articles in this group.
12873 If ALL is non-nil, already read articles become readable.
12874 If ALL is a number, fetch this number of articles."
12875   (interactive "P")
12876   (prog1
12877       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12878             older len)
12879         (setq older
12880               ;; Some nntp servers lie about their active range.  When
12881               ;; this happens, the active range can be in the millions.
12882               ;; Use a compressed range to avoid creating a huge list.
12883               (gnus-range-difference
12884                (gnus-range-difference (list gnus-newsgroup-active) old)
12885                gnus-newsgroup-unexist))
12886         (setq len (gnus-range-length older))
12887         (cond
12888          ((null older) nil)
12889          ((numberp all)
12890           (if (< all len)
12891               (let ((older-range (nreverse older)))
12892                 (setq older nil)
12893
12894                 (while (> all 0)
12895                   (let* ((r (pop older-range))
12896                          (min (if (numberp r) r (car r)))
12897                          (max (if (numberp r) r (cdr r))))
12898                     (while (and (<= min max)
12899                                 (> all 0))
12900                       (push max older)
12901                       (setq all (1- all)
12902                             max (1- max))))))
12903             (setq older (gnus-uncompress-range older))))
12904          (all
12905           (setq older (gnus-uncompress-range older)))
12906          (t
12907           (when (and (numberp gnus-large-newsgroup)
12908                    (> len gnus-large-newsgroup))
12909               (let* ((cursor-in-echo-area nil)
12910                      (initial (gnus-parameter-large-newsgroup-initial
12911                                gnus-newsgroup-name))
12912                      (input
12913                       (read-string
12914                        (format
12915                         "How many articles from %s (%s %d): "
12916                         (gnus-group-decoded-name gnus-newsgroup-name)
12917                         (if initial "max" "default")
12918                         len)
12919                        nil nil
12920                        (and initial
12921                             (number-to-string initial)))))
12922                 (unless (string-match "^[ \t]*$" input)
12923                   (setq all (string-to-number input))
12924                   (if (< all len)
12925                       (let ((older-range (nreverse older)))
12926                         (setq older nil)
12927
12928                         (while (> all 0)
12929                           (let* ((r (pop older-range))
12930                                  (min (if (numberp r) r (car r)))
12931                                  (max (if (numberp r) r (cdr r))))
12932                             (while (and (<= min max)
12933                                         (> all 0))
12934                               (push max older)
12935                               (setq all (1- all)
12936                                     max (1- max))))))))))
12937           (setq older (gnus-uncompress-range older))))
12938         (if (not older)
12939             (message "No old news.")
12940           (gnus-summary-insert-articles older)
12941           (gnus-summary-limit (gnus-sorted-nunion old older))))
12942     (gnus-summary-position-point)))
12943
12944 (defun gnus-summary-insert-new-articles ()
12945   "Insert all new articles in this group."
12946   (interactive)
12947   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12948         (old-high gnus-newsgroup-highest)
12949         (nnmail-fetched-sources (list t))
12950         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12951         i new)
12952     (unless new-active
12953       (error "Couldn't fetch new data"))
12954     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12955     (setq i (cdr gnus-newsgroup-active)
12956           gnus-newsgroup-highest i)
12957     (while (> i old-high)
12958       (push i new)
12959       (decf i))
12960     (if (not new)
12961         (message "No gnus is bad news")
12962       (gnus-summary-insert-articles new)
12963       (setq gnus-newsgroup-unreads
12964             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12965       (gnus-summary-limit (gnus-sorted-nunion old new))))
12966   (gnus-summary-position-point))
12967
12968 ;;; Bookmark support for Gnus.
12969 (declare-function gnus-article-show-summary "gnus-art" ())
12970 (declare-function bookmark-make-record-default
12971                   "bookmark" (&optional no-file no-context posn))
12972 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12973 (declare-function bookmark-default-handler "bookmark" (bmk))
12974 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12975 (defvar bookmark-yank-point)
12976 (defvar bookmark-current-buffer)
12977
12978 (defun gnus-summary-bookmark-make-record ()
12979   "Make a bookmark entry for a Gnus summary buffer."
12980   (let (pos buf)
12981     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12982       (save-restriction              ; FIXME is it necessary to widen?
12983         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12984       (setq buf "art") ; We are recording bookmark from article buffer.
12985       (setq bookmark-yank-point (point))
12986       (setq bookmark-current-buffer (current-buffer))
12987       (gnus-article-show-summary))      ; Go back in summary buffer.
12988     ;; We are now recording bookmark from summary buffer.
12989     (unless buf (setq buf "sum"))
12990     (let* ((subject (elt (gnus-summary-article-header) 1))
12991            (grp     (car gnus-article-current))
12992            (art     (cdr gnus-article-current))
12993            (head    (gnus-summary-article-header art))
12994            (id      (mail-header-id head)))
12995       `(,subject
12996         ,@(condition-case nil
12997               (bookmark-make-record-default 'no-file 'no-context pos)
12998             (wrong-number-of-arguments
12999              (bookmark-make-record-default 'point-only)))
13000         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
13001         (group . ,grp) (article . ,art)
13002         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
13003
13004 ;;;###autoload
13005 (defun gnus-summary-bookmark-jump (bookmark)
13006   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
13007 BOOKMARK is a bookmark name or a bookmark record."
13008   (let ((group    (bookmark-prop-get bookmark 'group))
13009         (article  (bookmark-prop-get bookmark 'article))
13010         (id       (bookmark-prop-get bookmark 'message-id))
13011         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
13012     (gnus-fetch-group group (list article))
13013     (gnus-summary-insert-cached-articles)
13014     (gnus-summary-goto-article id nil 'force)
13015     ;; FIXME we have to wait article buffer is ready (only large buffer)
13016     ;; Is there a better solution to know that?
13017     ;; If we don't wait `bookmark-default-handler' will have no chance
13018     ;; to set position. However there is no error, just wrong pos.
13019     (sit-for 1)
13020     (when (string= buf "Gnus-art")
13021       (other-window 1))
13022     (bookmark-default-handler
13023      `(""
13024        (buffer . ,(current-buffer))
13025        . ,(bookmark-get-bookmark-record bookmark)))))
13026
13027 (gnus-summary-make-all-marking-commands)
13028
13029 (gnus-ems-redefine)
13030
13031 (provide 'gnus-sum)
13032
13033 (run-hooks 'gnus-sum-load-hook)
13034
13035 ;; Local Variables:
13036 ;; coding: utf-8
13037 ;; End:
13038
13039 ;;; gnus-sum.el ends here