234fef77b422345e42c74cfa5ac1a14c1e37eb11
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996-2013 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (eval-when-compile
33   (when (featurep 'xemacs)
34     (require 'easy-mmode))) ; for `define-minor-mode'
35
36 (defvar tool-bar-mode)
37 (defvar gnus-tmp-header)
38
39 (require 'gnus)
40 (require 'gnus-group)
41 (require 'gnus-spec)
42 (require 'gnus-range)
43 (require 'gnus-int)
44 (require 'gnus-undo)
45 (require 'gnus-util)
46 (require 'gmm-utils)
47 (require 'mm-decode)
48 (require 'nnoo)
49
50 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
51 (autoload 'gnus-cache-write-active "gnus-cache")
52 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
53 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
54 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
55 (autoload 'mm-uu-dissect "mm-uu")
56 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
57   "Deuglify broken Outlook (Express) articles and redisplay."
58   t)
59 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
60 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
61 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
62 (autoload 'nnir-article-rsv "nnir" nil nil 'macro)
63 (autoload 'nnir-article-group "nnir" nil nil 'macro)
64
65 (defcustom gnus-kill-summary-on-exit t
66   "*If non-nil, kill the summary buffer when you exit from it.
67 If nil, the summary will become a \"*Dead Summary*\" buffer, and
68 it will be killed sometime later."
69   :group 'gnus-summary-exit
70   :type 'boolean)
71
72 (defcustom gnus-summary-next-group-on-exit t
73   "If non-nil, go to the next unread newsgroup on summary exit.
74 See `gnus-group-goto-unread'."
75   :link '(custom-manual "(gnus)Group Maneuvering")
76   :group 'gnus-summary-exit
77   :version "23.1" ;; No Gnus
78   :type 'boolean)
79
80 (defcustom gnus-summary-stop-at-end-of-message nil
81   "If non-nil, don't select the next message when using `SPC'."
82   :link '(custom-manual "(gnus)Group Maneuvering")
83   :group 'gnus-summary-maneuvering
84   :version "24.1"
85   :type 'boolean)
86
87 (defcustom gnus-fetch-old-headers nil
88   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
89 If an unread article in the group refers to an older, already
90 read (or just marked as read) article, the old article will not
91 normally be displayed in the Summary buffer.  If this variable is
92 t, Gnus will attempt to grab the headers to the old articles, and
93 thereby build complete threads.  If it has the value `some', all
94 old headers will be fetched but only enough headers to connect
95 otherwise loose threads will be displayed.  This variable can
96 also be a number.  In that case, no more than that number of old
97 headers will be fetched.  If it has the value `invisible', all
98 old headers will be fetched, but none will be displayed.
99
100 The server has to support NOV for any of this to work.
101
102 This feature can seriously impact performance it ignores all
103 locally cached header entries.  Setting it to t for groups for a
104 server that doesn't expire articles (such as news.gmane.org),
105 leads to very slow summary generation."
106   :group 'gnus-thread
107   :type '(choice (const :tag "off" nil)
108                  (const :tag "on" t)
109                  (const some)
110                  (const invisible)
111                  number
112                  (sexp :menu-tag "other" t)))
113
114 (defcustom gnus-refer-thread-limit 500
115   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
116 If t, fetch all the available old headers."
117   :group 'gnus-thread
118   :type '(choice number
119                  (sexp :menu-tag "other" t)))
120
121 (defcustom gnus-refer-thread-use-nnir nil
122   "*Use nnir to search an entire server when referring threads. A
123 nil value will only search for thread-related articles in the
124 current group."
125   :version "24.1"
126   :group 'gnus-thread
127   :type 'boolean)
128
129 (defcustom gnus-summary-make-false-root 'adopt
130   "*nil means that Gnus won't gather loose threads.
131 If the root of a thread has expired or been read in a previous
132 session, the information necessary to build a complete thread has been
133 lost.  Instead of having many small sub-threads from this original thread
134 scattered all over the summary buffer, Gnus can gather them.
135
136 If non-nil, Gnus will try to gather all loose sub-threads from an
137 original thread into one large thread.
138
139 If this variable is non-nil, it should be one of `none', `adopt',
140 `dummy' or `empty'.
141
142 If this variable is `none', Gnus will not make a false root, but just
143 present the sub-threads after another.
144 If this variable is `dummy', Gnus will create a dummy root that will
145 have all the sub-threads as children.
146 If this variable is `adopt', Gnus will make one of the \"children\"
147 the parent and mark all the step-children as such.
148 If this variable is `empty', the \"children\" are printed with empty
149 subject fields.  (Or rather, they will be printed with a string
150 given by the `gnus-summary-same-subject' variable.)"
151   :group 'gnus-thread
152   :type '(choice (const :tag "off" nil)
153                  (const none)
154                  (const dummy)
155                  (const adopt)
156                  (const empty)))
157
158 (defcustom gnus-summary-make-false-root-always nil
159   "Always make a false dummy root."
160   :version "22.1"
161   :group 'gnus-thread
162   :type 'boolean)
163
164 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
165   "*A regexp to match subjects to be excluded from loose thread gathering.
166 As loose thread gathering is done on subjects only, that means that
167 there can be many false gatherings performed.  By rooting out certain
168 common subjects, gathering might become saner."
169   :group 'gnus-thread
170   :type 'regexp)
171
172 (defcustom gnus-summary-gather-subject-limit nil
173   "*Maximum length of subject comparisons when gathering loose threads.
174 Use nil to compare full subjects.  Setting this variable to a low
175 number will help gather threads that have been corrupted by
176 newsreaders chopping off subject lines, but it might also mean that
177 unrelated articles that have subject that happen to begin with the
178 same few characters will be incorrectly gathered.
179
180 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
181 comparing subjects."
182   :group 'gnus-thread
183   :type '(choice (const :tag "off" nil)
184                  (const fuzzy)
185                  (sexp :menu-tag "on" t)))
186
187 (defcustom gnus-simplify-subject-functions nil
188   "List of functions taking a string argument that simplify subjects.
189 The functions are applied recursively.
190
191 Useful functions to put in this list include:
192 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
193 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
194   :group 'gnus-thread
195   :type '(repeat function))
196
197 (defcustom gnus-simplify-ignored-prefixes nil
198   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
199   :group 'gnus-thread
200   :type '(choice (const :tag "off" nil)
201                  regexp))
202
203 (defcustom gnus-build-sparse-threads nil
204   "*If non-nil, fill in the gaps in threads.
205 If `some', only fill in the gaps that are needed to tie loose threads
206 together.  If `more', fill in all leaf nodes that Gnus can find.  If
207 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
208   :group 'gnus-thread
209   :type '(choice (const :tag "off" nil)
210                  (const some)
211                  (const more)
212                  (sexp :menu-tag "all" t)))
213
214 (defcustom gnus-summary-thread-gathering-function
215   'gnus-gather-threads-by-subject
216   "*Function used for gathering loose threads.
217 There are two pre-defined functions: `gnus-gather-threads-by-subject',
218 which only takes Subjects into consideration; and
219 `gnus-gather-threads-by-references', which compared the References
220 headers of the articles to find matches."
221   :group 'gnus-thread
222   :type '(radio (function-item gnus-gather-threads-by-subject)
223                 (function-item gnus-gather-threads-by-references)
224                 (function :tag "other")))
225
226 (defcustom gnus-summary-same-subject ""
227   "*String indicating that the current article has the same subject as the previous.
228 This variable will only be used if the value of
229 `gnus-summary-make-false-root' is `empty'."
230   :group 'gnus-summary-format
231   :type 'string)
232
233 (defcustom gnus-summary-goto-unread nil
234   "*If t, many commands will go to the next unread article.
235 This applies to marking commands as well as other commands that
236 \"naturally\" select the next article, like, for instance, `SPC' at
237 the end of an article.
238
239 If nil, the marking commands do NOT go to the next unread article
240 \(they go to the next article instead).  If `never', commands that
241 usually go to the next unread article, will go to the next article,
242 whether it is read or not."
243   :version "24.1"
244   :group 'gnus-summary-marks
245   :link '(custom-manual "(gnus)Setting Marks")
246   :type '(choice (const :tag "off" nil)
247                  (const never)
248                  (sexp :menu-tag "on" t)))
249
250 (defcustom gnus-summary-default-score 0
251   "*Default article score level.
252 All scores generated by the score files will be added to this score.
253 If this variable is nil, scoring will be disabled."
254   :group 'gnus-score-default
255   :type '(choice (const :tag "disable")
256                  integer))
257
258 (defcustom gnus-summary-default-high-score 0
259   "*Default threshold for a high scored article.
260 An article will be highlighted as high scored if its score is greater
261 than this score."
262   :version "22.1"
263   :group 'gnus-score-default
264   :type 'integer)
265
266 (defcustom gnus-summary-default-low-score 0
267   "*Default threshold for a low scored article.
268 An article will be highlighted as low scored if its score is smaller
269 than this score."
270   :version "22.1"
271   :group 'gnus-score-default
272   :type 'integer)
273
274 (defcustom gnus-summary-zcore-fuzz 0
275   "*Fuzziness factor for the zcore in the summary buffer.
276 Articles with scores closer than this to `gnus-summary-default-score'
277 will not be marked."
278   :group 'gnus-summary-format
279   :type 'integer)
280
281 (defcustom gnus-simplify-subject-fuzzy-regexp nil
282   "*Strings to be removed when doing fuzzy matches.
283 This can either be a regular expression or list of regular expressions
284 that will be removed from subject strings if fuzzy subject
285 simplification is selected."
286   :group 'gnus-thread
287   :type '(repeat regexp))
288
289 (defcustom gnus-show-threads t
290   "*If non-nil, display threads in summary mode."
291   :group 'gnus-thread
292   :type 'boolean)
293
294 (defcustom gnus-thread-hide-subtree nil
295   "*If non-nil, hide all threads initially.
296 This can be a predicate specifier which says which threads to hide.
297 If threads are hidden, you have to run the command
298 `gnus-summary-show-thread' by hand or select an article."
299   :group 'gnus-thread
300   :type '(radio (sexp :format "Non-nil\n"
301                       :match (lambda (widget value)
302                                (not (or (consp value) (functionp value))))
303                       :value t)
304                 (const nil)
305                 (sexp :tag "Predicate specifier")))
306
307 (defcustom gnus-thread-hide-killed t
308   "*If non-nil, hide killed threads automatically."
309   :group 'gnus-thread
310   :type 'boolean)
311
312 (defcustom gnus-thread-ignore-subject t
313   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
314 If nil, articles that have different subjects from their parents will
315 start separate threads."
316   :group 'gnus-thread
317   :type 'boolean)
318
319 (defcustom gnus-thread-operation-ignore-subject t
320   "*If non-nil, subjects will be ignored when doing thread commands.
321 This affects commands like `gnus-summary-kill-thread' and
322 `gnus-summary-lower-thread'.
323
324 If this variable is nil, articles in the same thread with different
325 subjects will not be included in the operation in question.  If this
326 variable is `fuzzy', only articles that have subjects that are fuzzily
327 equal will be included."
328   :group 'gnus-thread
329   :type '(choice (const :tag "off" nil)
330                  (const fuzzy)
331                  (sexp :tag "on" t)))
332
333 (defcustom gnus-thread-indent-level 4
334   "*Number that says how much each sub-thread should be indented."
335   :group 'gnus-thread
336   :type 'integer)
337
338 (defcustom gnus-auto-extend-newsgroup t
339   "*If non-nil, extend newsgroup forward and backward when requested."
340   :group 'gnus-summary-choose
341   :type 'boolean)
342
343 (defcustom gnus-auto-select-first t
344   "If non-nil, select an article on group entry.
345 An article is selected automatically when entering a group
346 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
347 `gnus-summary-catchup-and-goto-next-group'.
348
349 Which article is selected is controlled by the variable
350 `gnus-auto-select-subject'.
351
352 If you want to prevent automatic selection of articles in some
353 newsgroups, set the variable to nil in `gnus-select-group-hook'."
354   ;; Commands include...
355   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
356   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
357   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
358   :group 'gnus-group-select
359   :type '(choice (const :tag "none" nil)
360                  (sexp :menu-tag "first" t)))
361
362 (defcustom gnus-auto-select-subject 'unseen-or-unread
363   "*Says what subject to place under point when entering a group.
364
365 This variable can either be the symbols `first' (place point on the
366 first subject), `unread' (place point on the subject line of the first
367 unread article), `best' (place point on the subject line of the
368 highest-scored article), `unseen' (place point on the subject line of
369 the first unseen article), `unseen-or-unread' (place point on the subject
370 line of the first unseen article or, if all articles have been seen, on the
371 subject line of the first unread article), or a function to be called to
372 place point on some subject line."
373   :version "24.1"
374   :group 'gnus-group-select
375   :type '(choice (const best)
376                  (const unread)
377                  (const first)
378                  (const unseen)
379                  (const unseen-or-unread)
380                  (function :tag "Function to call")))
381
382 (defcustom gnus-auto-select-next t
383   "*If non-nil, offer to go to the next group from the end of the previous.
384 If the value is t and the next newsgroup is empty, Gnus will exit
385 summary mode and go back to group mode.  If the value is neither nil
386 nor t, Gnus will select the following unread newsgroup.  In
387 particular, if the value is the symbol `quietly', the next unread
388 newsgroup will be selected without any confirmation, and if it is
389 `almost-quietly', the next group will be selected without any
390 confirmation if you are located on the last article in the group.
391 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
392 will go to the next group without confirmation."
393   :group 'gnus-summary-maneuvering
394   :type '(choice (const :tag "off" nil)
395                  (const quietly)
396                  (const almost-quietly)
397                  (const slightly-quietly)
398                  (sexp :menu-tag "on" t)))
399
400 (defcustom gnus-auto-select-same nil
401   "*If non-nil, select the next article with the same subject.
402 If there are no more articles with the same subject, go to
403 the first unread article."
404   :group 'gnus-summary-maneuvering
405   :type 'boolean)
406
407 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
408   "What article should be selected after exiting an ephemeral group.
409 Valid values include:
410
411 `next'
412   Select the next article.
413 `next-unread'
414   Select the next unread article.
415 `next-noselect'
416   Move the cursor to the next article.  This is the default.
417 `next-unread-noselect'
418   Move the cursor to the next unread article.
419
420 If it has any other value or there is no next (unread) article, the
421 article selected before entering to the ephemeral group will appear."
422   :version "23.1" ;; No Gnus
423   :group 'gnus-summary-maneuvering
424   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
425                  (const next) (const next-unread)
426                  (const next-noselect) (const next-unread-noselect)
427                  (sexp :tag "other" :value nil)))
428
429 (defcustom gnus-auto-goto-ignores 'unfetched
430   "*Says how to handle unfetched articles when maneuvering.
431
432 This variable can either be the symbols nil (maneuver to any
433 article), `undownloaded' (maneuvering while unplugged ignores articles
434 that have not been fetched), `always-undownloaded' (maneuvering always
435 ignores articles that have not been fetched), `unfetched' (maneuvering
436 ignores articles whose headers have not been fetched).
437
438 NOTE: The list of unfetched articles will always be nil when plugged
439 and, when unplugged, a subset of the undownloaded article list."
440   :version "22.1"
441   :group 'gnus-summary-maneuvering
442   :type '(choice (const :tag "None" nil)
443                  (const :tag "Undownloaded when unplugged" undownloaded)
444                  (const :tag "Undownloaded" always-undownloaded)
445                  (const :tag "Unfetched" unfetched)))
446
447 (defcustom gnus-summary-check-current nil
448   "*If non-nil, consider the current article when moving.
449 The \"unread\" movement commands will stay on the same line if the
450 current article is unread."
451   :group 'gnus-summary-maneuvering
452   :type 'boolean)
453
454 (defcustom gnus-auto-center-summary
455   (max (or (bound-and-true-p scroll-margin) 0) 2)
456   "*If non-nil, always center the current summary buffer.
457 In particular, if `vertical' do only vertical recentering.  If non-nil
458 and non-`vertical', do both horizontal and vertical recentering."
459   :group 'gnus-summary-maneuvering
460   :type '(choice (const :tag "none" nil)
461                  (const vertical)
462                  (integer :tag "height")
463                  (sexp :menu-tag "both" t)))
464
465 (defcustom gnus-auto-center-group t
466   "If non-nil, always center the group buffer."
467   :group 'gnus-summary-maneuvering
468   :type 'boolean)
469
470 (defcustom gnus-show-all-headers nil
471   "*If non-nil, don't hide any headers."
472   :group 'gnus-article-hiding
473   :group 'gnus-article-headers
474   :type 'boolean)
475
476 (defcustom gnus-summary-ignore-duplicates nil
477   "*If non-nil, ignore articles with identical Message-ID headers."
478   :group 'gnus-summary
479   :type 'boolean)
480
481 (defcustom gnus-single-article-buffer nil
482   "*If non-nil, display all articles in the same buffer.
483 If nil, each group will get its own article buffer."
484   :version "24.1"
485   :group 'gnus-article-various
486   :type 'boolean)
487
488 (defcustom gnus-widen-article-window nil
489   "If non-nil, selecting the article buffer will display only the article buffer."
490   :version "24.1"
491   :group 'gnus-article-various
492   :type 'boolean)
493
494 (defcustom gnus-break-pages t
495   "*If non-nil, do page breaking on articles.
496 The page delimiter is specified by the `gnus-page-delimiter'
497 variable."
498   :group 'gnus-article-various
499   :type 'boolean)
500
501 (defcustom gnus-move-split-methods nil
502   "*Variable used to suggest where articles are to be moved to.
503 It uses the same syntax as the `gnus-split-methods' variable.
504 However, whereas `gnus-split-methods' specifies file names as targets,
505 this variable specifies group names."
506   :group 'gnus-summary-mail
507   :type '(repeat (choice (list :value (fun) function)
508                          (cons :value ("" "") regexp (repeat string))
509                          (sexp :value nil))))
510
511 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
512   "Function used to compute default prefix for article move/copy/etc prompts.
513 The function should take one argument, a group name, and return a
514 string with the suggested prefix."
515   :group 'gnus-summary-mail
516   :type 'function)
517
518 ;; FIXME: Although the custom type is `character' for the following variables,
519 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
520
521 (defcustom gnus-unread-mark ?           ;Whitespace
522   "*Mark used for unread articles."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-ticked-mark ?!
527   "*Mark used for ticked articles."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-dormant-mark ??
532   "*Mark used for dormant articles."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-del-mark ?r
537   "*Mark used for del'd articles."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-read-mark ?R
542   "*Mark used for read articles."
543   :group 'gnus-summary-marks
544   :type 'character)
545
546 (defcustom gnus-expirable-mark ?E
547   "*Mark used for expirable articles."
548   :group 'gnus-summary-marks
549   :type 'character)
550
551 (defcustom gnus-killed-mark ?K
552   "*Mark used for killed articles."
553   :group 'gnus-summary-marks
554   :type 'character)
555
556 (defcustom gnus-spam-mark ?$
557   "*Mark used for spam articles."
558   :version "22.1"
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-kill-file-mark ?X
563   "*Mark used for articles killed by kill files."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-low-score-mark ?Y
568   "*Mark used for articles with a low score."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-catchup-mark ?C
573   "*Mark used for articles that are caught up."
574   :group 'gnus-summary-marks
575   :type 'character)
576
577 (defcustom gnus-replied-mark ?A
578   "*Mark used for articles that have been replied to."
579   :group 'gnus-summary-marks
580   :type 'character)
581
582 (defcustom gnus-forwarded-mark ?F
583   "*Mark used for articles that have been forwarded."
584   :version "22.1"
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-recent-mark ?N
589   "*Mark used for articles that are recent."
590   :version "22.1"
591   :group 'gnus-summary-marks
592   :type 'character)
593
594 (defcustom gnus-cached-mark ?*
595   "*Mark used for articles that are in the cache."
596   :group 'gnus-summary-marks
597   :type 'character)
598
599 (defcustom gnus-saved-mark ?S
600   "*Mark used for articles that have been saved."
601   :group 'gnus-summary-marks
602   :type 'character)
603
604 (defcustom gnus-unseen-mark ?.
605   "*Mark used for articles that haven't been seen."
606   :version "22.1"
607   :group 'gnus-summary-marks
608   :type 'character)
609
610 (defcustom gnus-no-mark ?               ;Whitespace
611   "*Mark used for articles that have no other secondary mark."
612   :version "22.1"
613   :group 'gnus-summary-marks
614   :type 'character)
615
616 (defcustom gnus-ancient-mark ?O
617   "*Mark used for ancient articles."
618   :group 'gnus-summary-marks
619   :type 'character)
620
621 (defcustom gnus-sparse-mark ?Q
622   "*Mark used for sparsely reffed articles."
623   :group 'gnus-summary-marks
624   :type 'character)
625
626 (defcustom gnus-canceled-mark ?G
627   "*Mark used for canceled articles."
628   :group 'gnus-summary-marks
629   :type 'character)
630
631 (defcustom gnus-duplicate-mark ?M
632   "*Mark used for duplicate articles."
633   :group 'gnus-summary-marks
634   :type 'character)
635
636 (defcustom gnus-undownloaded-mark ?-
637   "*Mark used for articles that weren't downloaded."
638   :version "22.1"
639   :group 'gnus-summary-marks
640   :type 'character)
641
642 (defcustom gnus-downloaded-mark ?+
643   "*Mark used for articles that were downloaded."
644   :group 'gnus-summary-marks
645   :type 'character)
646
647 (defcustom gnus-downloadable-mark ?%
648   "*Mark used for articles that are to be downloaded."
649   :group 'gnus-summary-marks
650   :type 'character)
651
652 (defcustom gnus-unsendable-mark ?=
653   "*Mark used for articles that won't be sent."
654   :group 'gnus-summary-marks
655   :type 'character)
656
657 (defcustom gnus-score-over-mark ?+
658   "*Score mark used for articles with high scores."
659   :group 'gnus-summary-marks
660   :type 'character)
661
662 (defcustom gnus-score-below-mark ?-
663   "*Score mark used for articles with low scores."
664   :group 'gnus-summary-marks
665   :type 'character)
666
667 (defcustom gnus-empty-thread-mark ?     ;Whitespace
668   "*There is no thread under the article."
669   :group 'gnus-summary-marks
670   :type 'character)
671
672 (defcustom gnus-not-empty-thread-mark ?=
673   "*There is a thread under the article."
674   :group 'gnus-summary-marks
675   :type 'character)
676
677 (defcustom gnus-view-pseudo-asynchronously nil
678   "*If non-nil, Gnus will view pseudo-articles asynchronously."
679   :group 'gnus-extract-view
680   :type 'boolean)
681
682 (defcustom gnus-auto-expirable-marks
683   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
684         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
685         gnus-duplicate-mark)
686   "*The list of marks converted into expiration if a group is auto-expirable."
687   :version "24.1"
688   :group 'gnus-summary
689   :type '(repeat character))
690
691 (defcustom gnus-inhibit-user-auto-expire t
692   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
693   :version "21.1"
694   :group 'gnus-summary
695   :type 'boolean)
696
697 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
698   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
699 If nil, the expirable marks will be unchanged except that the marks
700 will be removed when copying or moving articles to a group that has
701 not turned auto-expire on.  If non-nil, articles that have been read
702 will be marked as expirable when being copied or moved to a group in
703 which auto-expire is turned on."
704   :version "23.2"
705   :type 'boolean
706   :group 'gnus-summary-marks)
707
708 (defcustom gnus-view-pseudos nil
709   "*If `automatic', pseudo-articles will be viewed automatically.
710 If `not-confirm', pseudos will be viewed automatically, and the user
711 will not be asked to confirm the command."
712   :group 'gnus-extract-view
713   :type '(choice (const :tag "off" nil)
714                  (const automatic)
715                  (const not-confirm)))
716
717 (defcustom gnus-view-pseudos-separately t
718   "*If non-nil, one pseudo-article will be created for each file to be viewed.
719 If nil, all files that use the same viewing command will be given as a
720 list of parameters to that command."
721   :group 'gnus-extract-view
722   :type 'boolean)
723
724 (defcustom gnus-insert-pseudo-articles t
725   "*If non-nil, insert pseudo-articles when decoding articles."
726   :group 'gnus-extract-view
727   :type 'boolean)
728
729 (defcustom gnus-summary-dummy-line-format
730   "   %(:                             :%) %S\n"
731   "*The format specification for the dummy roots in the summary buffer.
732 It works along the same lines as a normal formatting string,
733 with some simple extensions.
734
735 %S  The subject
736
737 General format specifiers can also be used.
738 See `(gnus)Formatting Variables'."
739   :link '(custom-manual "(gnus)Formatting Variables")
740   :group 'gnus-threading
741   :type 'string)
742
743 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
744   "*The format specification for the summary mode line.
745 It works along the same lines as a normal formatting string,
746 with some simple extensions:
747
748 %G  Group name
749 %p  Unprefixed group name
750 %A  Current article number
751 %z  Current article score
752 %V  Gnus version
753 %U  Number of unread articles in the group
754 %e  Number of unselected articles in the group
755 %Z  A string with unread/unselected article counts
756 %g  Shortish group name
757 %S  Subject of the current article
758 %u  User-defined spec
759 %s  Current score file name
760 %d  Number of dormant articles
761 %r  Number of articles that have been marked as read in this session
762 %E  Number of articles expunged by the score files"
763   :group 'gnus-summary-format
764   :type 'string)
765
766 (defcustom gnus-list-identifiers nil
767   "Regexp that matches list identifiers to be removed from subject.
768 This can also be a list of regexps."
769   :version "21.1"
770   :group 'gnus-summary-format
771   :group 'gnus-article-hiding
772   :type '(choice (const :tag "none" nil)
773                  (regexp :value ".*")
774                  (repeat :value (".*") regexp)))
775
776 (defcustom gnus-summary-mark-below 0
777   "*Mark all articles with a score below this variable as read.
778 This variable is local to each summary buffer and usually set by the
779 score file."
780   :group 'gnus-score-default
781   :type 'integer)
782
783 (defun gnus-widget-reversible-match (widget value)
784   "Ignoring WIDGET, convert VALUE to internal form.
785 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
786   ;; (debug value)
787   (or (symbolp value)
788       (and (listp value)
789            (eq (length value) 2)
790            (eq (nth 0 value) 'not)
791            (symbolp (nth 1 value)))))
792
793 (defun gnus-widget-reversible-to-internal (widget value)
794   "Ignoring WIDGET, convert VALUE to internal form.
795 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
796 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
797   ;; (debug value)
798   (if (atom value)
799       (list value nil)
800     (list (nth 1 value) t)))
801
802 (defun gnus-widget-reversible-to-external (widget value)
803   "Ignoring WIDGET, convert VALUE to external form.
804 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
805 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
806   ;; (debug value)
807   (if (nth 1 value)
808       (list 'not (nth 0 value))
809     (nth 0 value)))
810
811 (define-widget 'gnus-widget-reversible 'group
812   "A `group' that convert values."
813   :match 'gnus-widget-reversible-match
814   :value-to-internal 'gnus-widget-reversible-to-internal
815   :value-to-external 'gnus-widget-reversible-to-external)
816
817 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
818   "*List of functions used for sorting articles in the summary buffer.
819
820 Each function takes two articles and returns non-nil if the first
821 article should be sorted before the other.  If you use more than one
822 function, the primary sort function should be the last.  You should
823 probably always include `gnus-article-sort-by-number' in the list of
824 sorting functions -- preferably first.  Also note that sorting by date
825 is often much slower than sorting by number, and the sorting order is
826 very similar.  (Sorting by date means sorting by the time the message
827 was sent, sorting by number means sorting by arrival time.)
828
829 Each item can also be a list `(not F)' where F is a function;
830 this reverses the sort order.
831
832 Ready-made functions include `gnus-article-sort-by-number',
833 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
834 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
835 and `gnus-article-sort-by-score'.
836
837 When threading is turned on, the variable `gnus-thread-sort-functions'
838 controls how articles are sorted."
839   :group 'gnus-summary-sort
840   :type '(repeat (gnus-widget-reversible
841                   (choice (function-item gnus-article-sort-by-number)
842                           (function-item gnus-article-sort-by-author)
843                           (function-item gnus-article-sort-by-subject)
844                           (function-item gnus-article-sort-by-date)
845                           (function-item gnus-article-sort-by-score)
846                           (function-item gnus-article-sort-by-random)
847                           (function :tag "other"))
848                   (boolean :tag "Reverse order"))))
849
850
851 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
852   "*List of functions used for sorting threads in the summary buffer.
853 By default, threads are sorted by article number.
854
855 Each function takes two threads and returns non-nil if the first
856 thread should be sorted before the other.  If you use more than one
857 function, the primary sort function should be the last.  You should
858 probably always include `gnus-thread-sort-by-number' in the list of
859 sorting functions -- preferably first.  Also note that sorting by date
860 is often much slower than sorting by number, and the sorting order is
861 very similar.  (Sorting by date means sorting by the time the message
862 was sent, sorting by number means sorting by arrival time.)
863
864 Each list item can also be a list `(not F)' where F is a
865 function; this specifies reversed sort order.
866
867 Ready-made functions include `gnus-thread-sort-by-number',
868 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
869 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
870 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
871 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
872 and `gnus-thread-sort-by-total-score' (see
873 `gnus-thread-score-function').
874
875 When threading is turned off, the variable
876 `gnus-article-sort-functions' controls how articles are sorted."
877   :group 'gnus-summary-sort
878   :type '(repeat
879           (gnus-widget-reversible
880            (choice (function-item gnus-thread-sort-by-number)
881                    (function-item gnus-thread-sort-by-author)
882                    (function-item gnus-thread-sort-by-recipient)
883                    (function-item gnus-thread-sort-by-subject)
884                    (function-item gnus-thread-sort-by-date)
885                    (function-item gnus-thread-sort-by-score)
886                    (function-item gnus-thread-sort-by-most-recent-number)
887                    (function-item gnus-thread-sort-by-most-recent-date)
888                    (function-item gnus-thread-sort-by-random)
889                    (function-item gnus-thread-sort-by-total-score)
890                    (function :tag "other"))
891            (boolean :tag "Reverse order"))))
892
893 (defcustom gnus-thread-score-function '+
894   "*Function used for calculating the total score of a thread.
895
896 The function is called with the scores of the article and each
897 subthread and should then return the score of the thread.
898
899 Some functions you can use are `+', `max', or `min'."
900   :group 'gnus-summary-sort
901   :type 'function)
902
903 (defcustom gnus-summary-expunge-below nil
904   "All articles that have a score less than this variable will be expunged.
905 This variable is local to the summary buffers."
906   :group 'gnus-score-default
907   :type '(choice (const :tag "off" nil)
908                  integer))
909
910 (defcustom gnus-thread-expunge-below nil
911   "All threads that have a total score less than this variable will be expunged.
912 See `gnus-thread-score-function' for en explanation of what a
913 \"thread score\" is.
914
915 This variable is local to the summary buffers."
916   :group 'gnus-threading
917   :group 'gnus-score-default
918   :type '(choice (const :tag "off" nil)
919                  integer))
920
921 (defcustom gnus-summary-mode-hook nil
922   "*A hook for Gnus summary mode.
923 This hook is run before any variables are set in the summary buffer."
924   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
925   :group 'gnus-summary-various
926   :type 'hook)
927
928 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
929 (when (featurep 'xemacs)
930   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
931   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
932   (add-hook 'gnus-summary-mode-hook
933             'gnus-xmas-switch-horizontal-scrollbar-off))
934
935 (defcustom gnus-summary-menu-hook nil
936   "*Hook run after the creation of the summary mode menu."
937   :group 'gnus-summary-visual
938   :type 'hook)
939
940 (defcustom gnus-summary-exit-hook nil
941   "*A hook called on exit from the summary buffer.
942 It will be called with point in the group buffer."
943   :group 'gnus-summary-exit
944   :type 'hook)
945
946 (defcustom gnus-summary-prepare-hook nil
947   "*A hook called after the summary buffer has been generated.
948 If you want to modify the summary buffer, you can use this hook."
949   :group 'gnus-summary-various
950   :type 'hook)
951
952 (defcustom gnus-summary-prepared-hook nil
953   "*A hook called as the last thing after the summary buffer has been generated."
954   :group 'gnus-summary-various
955   :type 'hook)
956
957 (defcustom gnus-summary-generate-hook nil
958   "*A hook run just before generating the summary buffer.
959 This hook is commonly used to customize threading variables and the
960 like."
961   :group 'gnus-summary-various
962   :type 'hook)
963
964 (defcustom gnus-select-group-hook nil
965   "*A hook called when a newsgroup is selected.
966
967 If you'd like to simplify subjects like the
968 `gnus-summary-next-same-subject' command does, you can use the
969 following hook:
970
971  (add-hook gnus-select-group-hook
972            (lambda ()
973              (mapcar (lambda (header)
974                        (mail-header-set-subject
975                         header
976                         (gnus-simplify-subject
977                          (mail-header-subject header) 're-only)))
978                      gnus-newsgroup-headers)))"
979   :group 'gnus-group-select
980   :type 'hook)
981
982 (defcustom gnus-select-article-hook nil
983   "*A hook called when an article is selected."
984   :group 'gnus-summary-choose
985   :options '(gnus-agent-fetch-selected-article)
986   :type 'hook)
987
988 (defcustom gnus-visual-mark-article-hook
989   (list 'gnus-highlight-selected-summary)
990   "*Hook run after selecting an article in the summary buffer.
991 It is meant to be used for highlighting the article in some way.  It
992 is not run if `gnus-visual' is nil."
993   :group 'gnus-summary-visual
994   :type 'hook)
995
996 (defcustom gnus-parse-headers-hook nil
997   "*A hook called before parsing the headers."
998   :group 'gnus-various
999   :type 'hook)
1000
1001 (defcustom gnus-exit-group-hook nil
1002   "*A hook called when exiting summary mode.
1003 This hook is not called from the non-updating exit commands like `Q'."
1004   :group 'gnus-various
1005   :type 'hook)
1006
1007 (defcustom gnus-summary-update-hook nil
1008   "*A hook called when a summary line is changed.
1009 The hook will not be called if `gnus-visual' is nil.
1010
1011 The default function `gnus-summary-highlight-line' will
1012 highlight the line according to the `gnus-summary-highlight'
1013 variable."
1014   :group 'gnus-summary-visual
1015   :type 'hook)
1016
1017 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1018   "*A hook called when an article is selected for the first time.
1019 The hook is intended to mark an article as read (or unread)
1020 automatically when it is selected."
1021   :group 'gnus-summary-choose
1022   :type 'hook)
1023
1024 (defcustom gnus-group-no-more-groups-hook nil
1025   "*A hook run when returning to group mode having no more (unread) groups."
1026   :group 'gnus-group-select
1027   :type 'hook)
1028
1029 (defcustom gnus-ps-print-hook nil
1030   "*A hook run before ps-printing something from Gnus."
1031   :group 'gnus-summary
1032   :type 'hook)
1033
1034 (defcustom gnus-summary-article-move-hook nil
1035   "*A hook called after an article is moved, copied, respooled, or crossposted."
1036   :version "22.1"
1037   :group 'gnus-summary
1038   :type 'hook)
1039
1040 (defcustom gnus-summary-article-delete-hook nil
1041   "*A hook called after an article is deleted."
1042   :version "22.1"
1043   :group 'gnus-summary
1044   :type 'hook)
1045
1046 (defcustom gnus-summary-article-expire-hook nil
1047   "*A hook called after an article is expired."
1048   :version "22.1"
1049   :group 'gnus-summary
1050   :type 'hook)
1051
1052 (defcustom gnus-summary-display-arrow
1053   (and (fboundp 'display-graphic-p)
1054        (display-graphic-p))
1055   "*If non-nil, display an arrow highlighting the current article."
1056   :version "22.1"
1057   :group 'gnus-summary
1058   :type 'boolean)
1059
1060 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1061   "Face used for highlighting the current article in the summary buffer."
1062   :group 'gnus-summary-visual
1063   :type 'face)
1064
1065 (defvar gnus-tmp-downloaded nil)
1066
1067 (defcustom gnus-summary-highlight
1068   '(((eq mark gnus-canceled-mark)
1069      . gnus-summary-cancelled)
1070     ((and uncached (> score default-high))
1071      . gnus-summary-high-undownloaded)
1072     ((and uncached (< score default-low))
1073      . gnus-summary-low-undownloaded)
1074     (uncached
1075      . gnus-summary-normal-undownloaded)
1076     ((and (> score default-high)
1077           (or (eq mark gnus-dormant-mark)
1078               (eq mark gnus-ticked-mark)))
1079      . gnus-summary-high-ticked)
1080     ((and (< score default-low)
1081           (or (eq mark gnus-dormant-mark)
1082               (eq mark gnus-ticked-mark)))
1083      . gnus-summary-low-ticked)
1084     ((or (eq mark gnus-dormant-mark)
1085          (eq mark gnus-ticked-mark))
1086      . gnus-summary-normal-ticked)
1087     ((and (> score default-high) (eq mark gnus-ancient-mark))
1088      . gnus-summary-high-ancient)
1089     ((and (< score default-low) (eq mark gnus-ancient-mark))
1090      . gnus-summary-low-ancient)
1091     ((eq mark gnus-ancient-mark)
1092      . gnus-summary-normal-ancient)
1093     ((and (> score default-high) (eq mark gnus-unread-mark))
1094      . gnus-summary-high-unread)
1095     ((and (< score default-low) (eq mark gnus-unread-mark))
1096      . gnus-summary-low-unread)
1097     ((eq mark gnus-unread-mark)
1098      . gnus-summary-normal-unread)
1099     ((> score default-high)
1100      . gnus-summary-high-read)
1101     ((< score default-low)
1102      . gnus-summary-low-read)
1103     (t
1104      . gnus-summary-normal-read))
1105   "*Controls the highlighting of summary buffer lines.
1106
1107 A list of (FORM . FACE) pairs.  When deciding how a particular
1108 summary line should be displayed, each form is evaluated.  The content
1109 of the face field after the first true form is used.  You can change
1110 how those summary lines are displayed, by editing the face field.
1111
1112 You can use the following variables in the FORM field.
1113
1114 score:        The article's score.
1115 default:      The default article score.
1116 default-high: The default score for high scored articles.
1117 default-low:  The default score for low scored articles.
1118 below:        The score below which articles are automatically marked as read.
1119 mark:         The article's mark.
1120 uncached:     Non-nil if the article is uncached."
1121   :group 'gnus-summary-visual
1122   :type '(repeat (cons (sexp :tag "Form" nil)
1123                        face)))
1124 (put 'gnus-summary-highlight 'risky-local-variable t)
1125
1126 (defcustom gnus-alter-header-function nil
1127   "Function called to allow alteration of article header structures.
1128 The function is called with one parameter, the article header vector,
1129 which it may alter in any way."
1130   :type '(choice (const :tag "None" nil)
1131                  function)
1132   :group 'gnus-summary)
1133
1134 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1135   "Function used to decode a string with encoded words.")
1136
1137 (defvar gnus-decode-encoded-address-function
1138   'mail-decode-encoded-address-string
1139   "Function used to decode addresses with encoded words.")
1140
1141 (defcustom gnus-extra-headers '(To Cc Keywords Gcc Newsgroups)
1142   "*Extra headers to parse."
1143   :version "24.1"                       ; added Cc Keywords Gcc
1144   :group 'gnus-summary
1145   :type '(repeat symbol))
1146
1147 (defcustom gnus-ignored-from-addresses
1148   (and user-mail-address
1149        (not (string= user-mail-address ""))
1150        (regexp-quote user-mail-address))
1151   "*From headers that may be suppressed in favor of To headers.
1152 This can be a regexp or a list of regexps."
1153   :version "21.1"
1154   :group 'gnus-summary
1155   :type '(choice regexp
1156                  (repeat :tag "Regexp List" regexp)))
1157
1158 (defsubst gnus-ignored-from-addresses ()
1159   (gmm-regexp-concat gnus-ignored-from-addresses))
1160
1161 (defcustom gnus-summary-to-prefix "-> "
1162   "*String prefixed to the To field in the summary line when
1163 using `gnus-ignored-from-addresses'."
1164   :version "22.1"
1165   :group 'gnus-summary
1166   :type 'string)
1167
1168 (defcustom gnus-summary-newsgroup-prefix "=> "
1169   "*String prefixed to the Newsgroup field in the summary
1170 line when using the option `gnus-ignored-from-addresses'."
1171   :version "22.1"
1172   :group 'gnus-summary
1173   :type 'string)
1174
1175 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1176   "List of charsets that should be ignored.
1177 When these charsets are used in the \"charset\" parameter, the
1178 default charset will be used instead."
1179   :version "21.1"
1180   :type '(repeat symbol)
1181   :group 'gnus-charset)
1182
1183 (defcustom gnus-newsgroup-maximum-articles nil
1184   "The maximum number of articles a newsgroup.
1185 If this is a number, old articles in a newsgroup exceeding this number
1186 are silently ignored.  If it is nil, no article is ignored.  Note that
1187 setting this variable to a number might prevent you from reading very
1188 old articles."
1189   :group 'gnus-group-select
1190   :version "22.2"
1191   :type '(choice (const :tag "No limit" nil)
1192                  integer))
1193
1194 (gnus-define-group-parameter
1195  ignored-charsets
1196  :type list
1197  :function-document
1198  "Return the ignored charsets of GROUP."
1199  :variable gnus-group-ignored-charsets-alist
1200  :variable-default
1201  '(("alt\\.chinese\\.text" iso-8859-1))
1202  :variable-document
1203  "Alist of regexps (to match group names) and charsets that should be ignored.
1204 When these charsets are used in the \"charset\" parameter, the
1205 default charset will be used instead."
1206  :variable-group gnus-charset
1207  :variable-type '(repeat (cons (regexp :tag "Group")
1208                                (repeat symbol)))
1209  :parameter-type '(choice :tag "Ignored charsets"
1210                           :value nil
1211                           (repeat (symbol)))
1212  :parameter-document       "\
1213 List of charsets that should be ignored.
1214
1215 When these charsets are used in the \"charset\" parameter, the
1216 default charset will be used instead.")
1217
1218 (defcustom gnus-group-highlight-words-alist nil
1219   "Alist of group regexps and highlight regexps.
1220 This variable uses the same syntax as `gnus-emphasis-alist'."
1221   :version "21.1"
1222   :type '(repeat (cons (regexp :tag "Group")
1223                        (repeat (list (regexp :tag "Highlight regexp")
1224                                      (number :tag "Group for entire word" 0)
1225                                      (number :tag "Group for displayed part" 0)
1226                                      (symbol :tag "Face"
1227                                              gnus-emphasis-highlight-words)))))
1228   :group 'gnus-summary-visual)
1229
1230 (defcustom gnus-summary-show-article-charset-alist
1231   nil
1232   "Alist of number and charset.
1233 The article will be shown with the charset corresponding to the
1234 numbered argument.
1235 For example: ((1 . cn-gb-2312) (2 . big5))."
1236   :version "21.1"
1237   :type '(repeat (cons (number :tag "Argument" 1)
1238                        (symbol :tag "Charset")))
1239   :group 'gnus-charset)
1240
1241 (defcustom gnus-preserve-marks t
1242   "Whether marks are preserved when moving, copying and respooling messages."
1243   :version "21.1"
1244   :type 'boolean
1245   :group 'gnus-summary-marks)
1246
1247 (defcustom gnus-alter-articles-to-read-function nil
1248   "Function to be called to alter the list of articles to be selected."
1249   :type '(choice (const nil) function)
1250   :group 'gnus-summary)
1251
1252 (defcustom gnus-orphan-score nil
1253   "*All orphans get this score added.  Set in the score file."
1254   :group 'gnus-score-default
1255   :type '(choice (const nil)
1256                  integer))
1257
1258 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1259   "*A regexp to match MIME parts when saving multiple parts of a
1260 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1261 This regexp will be used by default when prompting the user for which
1262 type of files to save."
1263   :group 'gnus-summary
1264   :type 'regexp)
1265
1266 (defcustom gnus-read-all-available-headers nil
1267   "Whether Gnus should parse all headers made available to it.
1268 This is mostly relevant for slow back ends where the user may
1269 wish to widen the summary buffer to include all headers
1270 that were fetched."
1271   :version "22.1"
1272   :group 'gnus-summary
1273   :type '(choice boolean regexp))
1274
1275 (defcustom gnus-summary-pipe-output-default-command nil
1276   "Command (and optional arguments) used to pipe article to subprocess.
1277 This will be used as the default command if it is non-nil.  The value
1278 will be updated if you modify it when executing the command
1279 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1280   :version "23.1" ;; No Gnus
1281   :group 'gnus-summary
1282   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1283
1284 (defcustom gnus-summary-muttprint-program "muttprint"
1285   "Command (and optional arguments) used to run Muttprint.
1286 The value will be updated if you modify it when executing the command
1287 `gnus-summary-muttprint'."
1288   :version "22.1"
1289   :group 'gnus-summary
1290   :type 'string)
1291
1292 (defcustom gnus-article-loose-mime t
1293   "If non-nil, don't require MIME-Version header.
1294 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1295 supply the MIME-Version header or deliberately strip it from the mail.
1296 If non-nil (the default), Gnus will treat some articles as MIME
1297 even if the MIME-Version header is missing."
1298   :version "22.1"
1299   :type 'boolean
1300   :group 'gnus-article-mime)
1301
1302 (defcustom gnus-article-emulate-mime t
1303   "If non-nil, use MIME emulation for uuencode and the like.
1304 This means that Gnus will search message bodies for text that look
1305 like uuencoded bits, yEncoded bits, and so on, and present that using
1306 the normal Gnus MIME machinery."
1307   :version "22.1"
1308   :type 'boolean
1309   :group 'gnus-article-mime)
1310
1311 ;;; Internal variables
1312
1313 (defvar gnus-summary-display-cache nil)
1314 (defvar gnus-article-mime-handles nil)
1315 (defvar gnus-article-decoded-p nil)
1316 (defvar gnus-article-charset nil)
1317 (defvar gnus-article-ignored-charsets nil)
1318 (defvar gnus-scores-exclude-files nil)
1319 (defvar gnus-page-broken nil)
1320
1321 (defvar gnus-original-article nil)
1322 (defvar gnus-article-internal-prepare-hook nil)
1323 (defvar gnus-newsgroup-process-stack nil)
1324
1325 (defvar gnus-thread-indent-array nil)
1326 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1327 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1328   "Function called to sort the articles within a thread after it has been gathered together.")
1329
1330 (defvar gnus-summary-save-parts-type-history nil)
1331 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1332
1333 ;; Avoid highlighting in kill files.
1334 (defvar gnus-summary-inhibit-highlight nil)
1335 (defvar gnus-newsgroup-selected-overlay nil)
1336 (defvar gnus-inhibit-limiting nil)
1337 (defvar gnus-newsgroup-adaptive-score-file nil)
1338 (defvar gnus-current-score-file nil)
1339 (defvar gnus-current-move-group nil)
1340 (defvar gnus-current-copy-group nil)
1341 (defvar gnus-current-crosspost-group nil)
1342 (defvar gnus-newsgroup-display nil)
1343
1344 (defvar gnus-newsgroup-dependencies nil)
1345 (defvar gnus-newsgroup-adaptive nil)
1346 (defvar gnus-summary-display-article-function nil)
1347 (defvar gnus-summary-highlight-line-function nil
1348   "Function called after highlighting a summary line.")
1349
1350 (defvar gnus-summary-line-format-alist
1351   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1352     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1353     (?s gnus-tmp-subject-or-nil ?s)
1354     (?n gnus-tmp-name ?s)
1355     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1356         ?s)
1357     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1358             gnus-tmp-from) ?s)
1359     (?F gnus-tmp-from ?s)
1360     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1361     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1362     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1363     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1364     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1365     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1366     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1367     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1368     (?L gnus-tmp-lines ?s)
1369     (?Z (or (nnir-article-rsv (mail-header-number gnus-tmp-header))
1370             0) ?d)
1371     (?G (or (nnir-article-group (mail-header-number gnus-tmp-header))
1372             "") ?s)
1373     (?g (or (gnus-group-short-name
1374              (nnir-article-group (mail-header-number gnus-tmp-header)))
1375             "") ?s)
1376     (?O gnus-tmp-downloaded ?c)
1377     (?I gnus-tmp-indentation ?s)
1378     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1379     (?R gnus-tmp-replied ?c)
1380     (?\[ gnus-tmp-opening-bracket ?c)
1381     (?\] gnus-tmp-closing-bracket ?c)
1382     (?\> (make-string gnus-tmp-level ? ) ?s)
1383     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1384     (?i gnus-tmp-score ?d)
1385     (?z gnus-tmp-score-char ?c)
1386     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1387     (?U gnus-tmp-unread ?c)
1388     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1389         ?s)
1390     (?t (gnus-summary-number-of-articles-in-thread
1391          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1392         ?d)
1393     (?e (gnus-summary-number-of-articles-in-thread
1394          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1395         ?c)
1396     (?u gnus-tmp-user-defined ?s)
1397     (?P (gnus-pick-line-number) ?d)
1398     (?B gnus-tmp-thread-tree-header-string ?s)
1399     (user-date (gnus-user-date
1400                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1401   "An alist of format specifications that can appear in summary lines.
1402 These are paired with what variables they correspond with, along with
1403 the type of the variable (string, integer, character, etc).")
1404
1405 (defvar gnus-summary-dummy-line-format-alist
1406   `((?S gnus-tmp-subject ?s)
1407     (?N gnus-tmp-number ?d)
1408     (?u gnus-tmp-user-defined ?s)))
1409
1410 (defvar gnus-summary-mode-line-format-alist
1411   `((?G gnus-tmp-group-name ?s)
1412     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1413     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1414     (?A gnus-tmp-article-number ?d)
1415     (?Z gnus-tmp-unread-and-unselected ?s)
1416     (?V gnus-version ?s)
1417     (?U gnus-tmp-unread-and-unticked ?d)
1418     (?S gnus-tmp-subject ?s)
1419     (?e gnus-tmp-unselected ?d)
1420     (?u gnus-tmp-user-defined ?s)
1421     (?d (length gnus-newsgroup-dormant) ?d)
1422     (?t (length gnus-newsgroup-marked) ?d)
1423     (?h (length gnus-newsgroup-spam-marked) ?d)
1424     (?r (length gnus-newsgroup-reads) ?d)
1425     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1426     (?E gnus-newsgroup-expunged-tally ?d)
1427     (?s (gnus-current-score-file-nondirectory) ?s)))
1428
1429 ;; This is here rather than in gnus-art for compilation reasons.
1430 (defvar gnus-article-mode-line-format-alist
1431   (nconc '((?w (gnus-article-wash-status) ?s)
1432            (?m (gnus-article-mime-part-status) ?s))
1433          gnus-summary-mode-line-format-alist))
1434
1435 (defvar gnus-last-search-regexp nil
1436   "Default regexp for article search command.")
1437
1438 (defvar gnus-last-shell-command nil
1439   "Default shell command on article.")
1440
1441 (defvar gnus-newsgroup-agentized nil
1442   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1443 (defvar gnus-newsgroup-begin nil)
1444 (defvar gnus-newsgroup-end nil)
1445 (defvar gnus-newsgroup-last-rmail nil)
1446 (defvar gnus-newsgroup-last-mail nil)
1447 (defvar gnus-newsgroup-last-folder nil)
1448 (defvar gnus-newsgroup-last-file nil)
1449 (defvar gnus-newsgroup-last-directory nil)
1450 (defvar gnus-newsgroup-auto-expire nil)
1451 (defvar gnus-newsgroup-active nil)
1452 (defvar gnus-newsgroup-highest nil)
1453
1454 (defvar gnus-newsgroup-data nil)
1455 (defvar gnus-newsgroup-data-reverse nil)
1456 (defvar gnus-newsgroup-limit nil)
1457 (defvar gnus-newsgroup-limits nil)
1458 (defvar gnus-summary-use-undownloaded-faces nil)
1459
1460 (defvar gnus-newsgroup-unreads nil
1461   "Sorted list of unread articles in the current newsgroup.")
1462
1463 (defvar gnus-newsgroup-unselected nil
1464   "Sorted list of unselected unread articles in the current newsgroup.")
1465
1466 (defvar gnus-newsgroup-reads nil
1467   "Alist of read articles and article marks in the current newsgroup.")
1468
1469 (defvar gnus-newsgroup-expunged-tally nil)
1470
1471 (defvar gnus-newsgroup-marked nil
1472   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1473
1474 (defvar gnus-newsgroup-spam-marked nil
1475   "List of ranges of articles that have been marked as spam.")
1476
1477 (defvar gnus-newsgroup-killed nil
1478   "List of ranges of articles that have been through the scoring process.")
1479
1480 (defvar gnus-newsgroup-cached nil
1481   "Sorted list of articles that come from the article cache.")
1482
1483 (defvar gnus-newsgroup-saved nil
1484   "List of articles that have been saved.")
1485
1486 (defvar gnus-newsgroup-kill-headers nil)
1487
1488 (defvar gnus-newsgroup-replied nil
1489   "List of articles that have been replied to in the current newsgroup.")
1490
1491 (defvar gnus-newsgroup-forwarded nil
1492   "List of articles that have been forwarded in the current newsgroup.")
1493
1494 (defvar gnus-newsgroup-expirable nil
1495   "Sorted list of articles in the current newsgroup that can be expired.")
1496
1497 (defvar gnus-newsgroup-processable nil
1498   "List of articles in the current newsgroup that can be processed.")
1499
1500 (defvar gnus-newsgroup-downloadable nil
1501   "Sorted list of articles in the current newsgroup that can be processed.")
1502
1503 (defvar gnus-newsgroup-unfetched nil
1504   "Sorted list of articles in the current newsgroup whose headers have
1505 not been fetched into the agent.
1506
1507 This list will always be a subset of gnus-newsgroup-undownloaded.")
1508
1509 (defvar gnus-newsgroup-undownloaded nil
1510   "List of articles in the current newsgroup that haven't been downloaded.")
1511
1512 (defvar gnus-newsgroup-unsendable nil
1513   "List of articles in the current newsgroup that won't be sent.")
1514
1515 (defvar gnus-newsgroup-bookmarks nil
1516   "List of articles in the current newsgroup that have bookmarks.")
1517
1518 (defvar gnus-newsgroup-dormant nil
1519   "Sorted list of dormant articles in the current newsgroup.")
1520
1521 (defvar gnus-newsgroup-unseen nil
1522   "List of unseen articles in the current newsgroup.")
1523
1524 (defvar gnus-newsgroup-seen nil
1525   "Range of seen articles in the current newsgroup.")
1526
1527 (defvar gnus-newsgroup-unexist nil
1528   "Range of unexistent articles in the current newsgroup.")
1529
1530 (defvar gnus-newsgroup-articles nil
1531   "List of articles in the current newsgroup.")
1532
1533 (defvar gnus-newsgroup-scored nil
1534   "List of scored articles in the current newsgroup.")
1535
1536 (defvar gnus-newsgroup-headers nil
1537   "List of article headers in the current newsgroup.")
1538
1539 (defvar gnus-newsgroup-threads nil)
1540
1541 (defvar gnus-newsgroup-prepared nil
1542   "Whether the current group has been prepared properly.")
1543
1544 (defvar gnus-newsgroup-ancient nil
1545   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1546
1547 (defvar gnus-newsgroup-sparse nil)
1548
1549 (defvar gnus-current-article nil)
1550 (defvar gnus-article-current nil)
1551 (defvar gnus-current-headers nil)
1552 (defvar gnus-have-all-headers nil)
1553 (defvar gnus-last-article nil)
1554 (defvar gnus-newsgroup-history nil)
1555 (defvar gnus-newsgroup-charset nil)
1556 (defvar gnus-newsgroup-ephemeral-charset nil)
1557 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1558
1559 (defvar gnus-article-before-search nil)
1560
1561 (defvar gnus-summary-local-variables
1562   '(gnus-newsgroup-name
1563
1564     ;; Marks lists
1565     gnus-newsgroup-unreads
1566     gnus-newsgroup-unselected
1567     gnus-newsgroup-marked
1568     gnus-newsgroup-spam-marked
1569     gnus-newsgroup-reads
1570     gnus-newsgroup-saved
1571     gnus-newsgroup-replied
1572     gnus-newsgroup-forwarded
1573     gnus-newsgroup-expirable
1574     gnus-newsgroup-killed
1575     gnus-newsgroup-unseen
1576     gnus-newsgroup-seen
1577     gnus-newsgroup-unexist
1578     gnus-newsgroup-cached
1579     gnus-newsgroup-downloadable
1580     gnus-newsgroup-undownloaded
1581     gnus-newsgroup-unsendable
1582
1583     gnus-newsgroup-begin gnus-newsgroup-end
1584     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1585     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1586     gnus-newsgroup-last-directory
1587     gnus-newsgroup-auto-expire
1588     gnus-newsgroup-processable
1589     gnus-newsgroup-unfetched
1590     gnus-newsgroup-articles
1591     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1592     gnus-newsgroup-headers gnus-newsgroup-threads
1593     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1594     gnus-current-article gnus-current-headers gnus-have-all-headers
1595     gnus-last-article gnus-article-internal-prepare-hook
1596     (gnus-summary-article-delete-hook . global)
1597     (gnus-summary-article-move-hook . global)
1598     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1599     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1600     gnus-thread-expunge-below
1601     gnus-score-alist gnus-current-score-file
1602     (gnus-summary-expunge-below . global)
1603     (gnus-summary-mark-below . global)
1604     (gnus-orphan-score . global)
1605     gnus-newsgroup-active gnus-scores-exclude-files
1606     gnus-newsgroup-highest
1607     gnus-newsgroup-history gnus-newsgroup-ancient
1608     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1609     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1610     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1611     (gnus-newsgroup-expunged-tally . 0)
1612     gnus-cache-removable-articles
1613     gnus-newsgroup-data gnus-newsgroup-data-reverse
1614     gnus-newsgroup-limit gnus-newsgroup-limits
1615     gnus-newsgroup-charset gnus-newsgroup-display
1616     gnus-summary-use-undownloaded-faces)
1617   "Variables that are buffer-local to the summary buffers.")
1618
1619 (defvar gnus-newsgroup-variables nil
1620   "A list of variables that have separate values in different newsgroups.
1621 A list of newsgroup (summary buffer) local variables, or cons of
1622 variables and their default expressions to be evalled (when the default
1623 values are not nil), that should be made global while the summary buffer
1624 is active.
1625
1626 Note: The default expressions will be evaluated (using function `eval')
1627 before assignment to the local variable rather than just assigned to it.
1628 If the default expression is the symbol `global', that symbol will not
1629 be evaluated but the global value of the local variable will be used
1630 instead.
1631
1632 These variables can be used to set variables in the group parameters
1633 while still allowing them to affect operations done in other buffers.
1634 For example:
1635
1636 \(setq gnus-newsgroup-variables
1637      '(message-use-followup-to
1638        (gnus-visible-headers .
1639          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1640 ")
1641
1642 (eval-when-compile
1643   ;; Bind features so that require will believe that gnus-sum has
1644   ;; already been loaded (avoids infinite recursion)
1645   (let ((features (cons 'gnus-sum features)))
1646     (require 'gnus-art)))
1647
1648 ;; MIME stuff.
1649
1650 (defvar gnus-decode-encoded-word-methods
1651   '(mail-decode-encoded-word-string)
1652   "List of methods used to decode encoded words.
1653
1654 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1655 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1656 \(REGEXP . FUNCTION), FUNCTION will be applied only to the newsgroups
1657 whose names match REGEXP.
1658
1659 For example:
1660 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1661  mail-decode-encoded-word-string
1662  (\"chinese\" . rfc1843-decode-string))")
1663
1664 (defvar gnus-decode-encoded-word-methods-cache nil)
1665
1666 (defun gnus-multi-decode-encoded-word-string (string)
1667   "Apply the functions from `gnus-encoded-word-methods' that match."
1668   (unless (and gnus-decode-encoded-word-methods-cache
1669                (eq gnus-newsgroup-name
1670                    (car gnus-decode-encoded-word-methods-cache)))
1671     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1672     (dolist (method gnus-decode-encoded-word-methods)
1673       (if (symbolp method)
1674           (nconc gnus-decode-encoded-word-methods-cache (list method))
1675         (if (and gnus-newsgroup-name
1676                  (string-match (car method) gnus-newsgroup-name))
1677             (nconc gnus-decode-encoded-word-methods-cache
1678                    (list (cdr method)))))))
1679   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1680     (setq string (funcall method string))))
1681
1682 ;; Subject simplification.
1683
1684 (defun gnus-simplify-whitespace (str)
1685   "Remove excessive whitespace from STR."
1686   ;; Multiple spaces.
1687   (while (string-match "[ \t][ \t]+" str)
1688     (setq str (concat (substring str 0 (match-beginning 0))
1689                         " "
1690                         (substring str (match-end 0)))))
1691   ;; Leading spaces.
1692   (when (string-match "^[ \t]+" str)
1693     (setq str (substring str (match-end 0))))
1694   ;; Trailing spaces.
1695   (when (string-match "[ \t]+$" str)
1696     (setq str (substring str 0 (match-beginning 0))))
1697   str)
1698
1699 (defun gnus-simplify-all-whitespace (str)
1700   "Remove all whitespace from STR."
1701   (while (string-match "[ \t\n]+" str)
1702     (setq str (replace-match "" nil nil str)))
1703   str)
1704
1705 (defsubst gnus-simplify-subject-re (subject)
1706   "Remove \"Re:\" from subject lines."
1707   (if (string-match message-subject-re-regexp subject)
1708       (substring subject (match-end 0))
1709     subject))
1710
1711 (defun gnus-simplify-subject (subject &optional re-only)
1712   "Remove `Re:' and words in parentheses.
1713 If RE-ONLY is non-nil, strip leading `Re:'s only."
1714   (let ((case-fold-search t))           ;Ignore case.
1715     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1716     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1717       (setq subject (substring subject (match-end 0))))
1718     ;; Remove uninteresting prefixes.
1719     (when (and (not re-only)
1720                gnus-simplify-ignored-prefixes
1721                (string-match gnus-simplify-ignored-prefixes subject))
1722       (setq subject (substring subject (match-end 0))))
1723     ;; Remove words in parentheses from end.
1724     (unless re-only
1725       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1726         (setq subject (substring subject 0 (match-beginning 0)))))
1727     ;; Return subject string.
1728     subject))
1729
1730 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1731 ;; all whitespace.
1732 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1733   (goto-char (point-min))
1734   (while (re-search-forward regexp nil t)
1735     (replace-match (or newtext ""))))
1736
1737 (defun gnus-simplify-buffer-fuzzy (regexp)
1738   "Simplify string in the buffer fuzzily.
1739 The string in the accessible portion of the current buffer is simplified.
1740 It is assumed to be a single-line subject.
1741 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1742 matter is removed.  Additional things can be deleted by setting
1743 `gnus-simplify-subject-fuzzy-regexp'."
1744   (let ((case-fold-search t)
1745         (modified-tick))
1746     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1747
1748     (while (not (eq modified-tick (buffer-modified-tick)))
1749       (setq modified-tick (buffer-modified-tick))
1750       (cond
1751        ((listp regexp)
1752         (mapc 'gnus-simplify-buffer-fuzzy-step regexp))
1753        (regexp
1754         (gnus-simplify-buffer-fuzzy-step regexp)))
1755       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1756       (gnus-simplify-buffer-fuzzy-step
1757        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1758       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1759
1760     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1761     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1762     (gnus-simplify-buffer-fuzzy-step " $")
1763     (gnus-simplify-buffer-fuzzy-step "^ +")))
1764
1765 (defun gnus-simplify-subject-fuzzy (subject)
1766   "Simplify a subject string fuzzily.
1767 See `gnus-simplify-buffer-fuzzy' for details."
1768   (save-excursion
1769     (let ((regexp gnus-simplify-subject-fuzzy-regexp))
1770       (gnus-set-work-buffer)
1771       (let ((case-fold-search t))
1772         ;; Remove uninteresting prefixes.
1773         (when (and gnus-simplify-ignored-prefixes
1774                    (string-match gnus-simplify-ignored-prefixes subject))
1775           (setq subject (substring subject (match-end 0))))
1776         (insert subject)
1777         (inline (gnus-simplify-buffer-fuzzy regexp))
1778         (buffer-string)))))
1779
1780 (defsubst gnus-simplify-subject-fully (subject)
1781   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1782   (cond
1783    (gnus-simplify-subject-functions
1784     (gnus-map-function gnus-simplify-subject-functions subject))
1785    ((null gnus-summary-gather-subject-limit)
1786     (gnus-simplify-subject-re subject))
1787    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1788     (gnus-simplify-subject-fuzzy subject))
1789    ((numberp gnus-summary-gather-subject-limit)
1790     (truncate-string-to-width (gnus-simplify-subject-re subject)
1791                               gnus-summary-gather-subject-limit))
1792    (t
1793     subject)))
1794
1795 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1796   "Check whether two subjects are equal.
1797 If optional argument SIMPLE-FIRST is t, first argument is already
1798 simplified."
1799   (cond
1800    ((null simple-first)
1801     (equal (gnus-simplify-subject-fully s1)
1802            (gnus-simplify-subject-fully s2)))
1803    (t
1804     (equal s1
1805            (gnus-simplify-subject-fully s2)))))
1806
1807 (defun gnus-summary-bubble-group ()
1808   "Increase the score of the current group.
1809 This is a handy function to add to `gnus-summary-exit-hook' to
1810 increase the score of each group you read."
1811   (gnus-group-add-score gnus-newsgroup-name))
1812
1813 \f
1814 ;;;
1815 ;;; Gnus summary mode
1816 ;;;
1817
1818 (put 'gnus-summary-mode 'mode-class 'special)
1819
1820 (defvar gnus-article-commands-menu)
1821
1822 ;; Non-orthogonal keys
1823
1824 (gnus-define-keys gnus-summary-mode-map
1825   " " gnus-summary-next-page
1826   [?\S-\ ] gnus-summary-prev-page
1827   "\177" gnus-summary-prev-page
1828   [delete] gnus-summary-prev-page
1829   [backspace] gnus-summary-prev-page
1830   "\r" gnus-summary-scroll-up
1831   "\M-\r" gnus-summary-scroll-down
1832   "n" gnus-summary-next-unread-article
1833   "p" gnus-summary-prev-unread-article
1834   "N" gnus-summary-next-article
1835   "P" gnus-summary-prev-article
1836   "\M-\C-n" gnus-summary-next-same-subject
1837   "\M-\C-p" gnus-summary-prev-same-subject
1838   "\M-n" gnus-summary-next-unread-subject
1839   "\M-p" gnus-summary-prev-unread-subject
1840   "." gnus-summary-first-unread-article
1841   "," gnus-summary-best-unread-article
1842   "\M-s" gnus-summary-search-article-forward
1843   "\M-r" gnus-summary-search-article-backward
1844   "\M-S" gnus-summary-repeat-search-article-forward
1845   "\M-R" gnus-summary-repeat-search-article-backward
1846   "<" gnus-summary-beginning-of-article
1847   ">" gnus-summary-end-of-article
1848   "j" gnus-summary-goto-article
1849   "^" gnus-summary-refer-parent-article
1850   "\M-^" gnus-summary-refer-article
1851   "u" gnus-summary-tick-article-forward
1852   "!" gnus-summary-tick-article-forward
1853   "U" gnus-summary-tick-article-backward
1854   "d" gnus-summary-mark-as-read-forward
1855   "D" gnus-summary-mark-as-read-backward
1856   "E" gnus-summary-mark-as-expirable
1857   "\M-u" gnus-summary-clear-mark-forward
1858   "\M-U" gnus-summary-clear-mark-backward
1859   "k" gnus-summary-kill-same-subject-and-select
1860   "\C-k" gnus-summary-kill-same-subject
1861   "\M-\C-k" gnus-summary-kill-thread
1862   "\M-\C-l" gnus-summary-lower-thread
1863   "e" gnus-summary-edit-article
1864   "#" gnus-summary-mark-as-processable
1865   "\M-#" gnus-summary-unmark-as-processable
1866   "\M-\C-t" gnus-summary-toggle-threads
1867   "\M-\C-s" gnus-summary-show-thread
1868   "\M-\C-h" gnus-summary-hide-thread
1869   "\M-\C-f" gnus-summary-next-thread
1870   "\M-\C-b" gnus-summary-prev-thread
1871   [(meta down)] gnus-summary-next-thread
1872   [(meta up)] gnus-summary-prev-thread
1873   "\M-\C-u" gnus-summary-up-thread
1874   "\M-\C-d" gnus-summary-down-thread
1875   "&" gnus-summary-execute-command
1876   "c" gnus-summary-catchup-and-exit
1877   "\C-w" gnus-summary-mark-region-as-read
1878   "\C-t" gnus-summary-toggle-truncation
1879   "?" gnus-summary-mark-as-dormant
1880   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1881   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1882   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1883   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1884   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1885   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1886   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1887   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1888   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1889   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1890   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1891   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1892   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1893   "=" gnus-summary-expand-window
1894   "\C-x\C-s" gnus-summary-reselect-current-group
1895   "\M-g" gnus-summary-rescan-group
1896   "\C-c\C-r" gnus-summary-caesar-message
1897   "f" gnus-summary-followup
1898   "F" gnus-summary-followup-with-original
1899   "C" gnus-summary-cancel-article
1900   "r" gnus-summary-reply
1901   "R" gnus-summary-reply-with-original
1902   "\C-c\C-f" gnus-summary-mail-forward
1903   "o" gnus-summary-save-article
1904   "\C-o" gnus-summary-save-article-mail
1905   "|" gnus-summary-pipe-output
1906   "\M-k" gnus-summary-edit-local-kill
1907   "\M-K" gnus-summary-edit-global-kill
1908   ;; "V" gnus-version
1909   "\C-c\C-d" gnus-summary-describe-group
1910   "q" gnus-summary-exit
1911   "Q" gnus-summary-exit-no-update
1912   "\C-c\C-i" gnus-info-find-node
1913   gnus-mouse-2 gnus-mouse-pick-article
1914   [follow-link] mouse-face
1915   "m" gnus-summary-mail-other-window
1916   "a" gnus-summary-post-news
1917   "x" gnus-summary-limit-to-unread
1918   "s" gnus-summary-isearch-article
1919   "\t" gnus-summary-widget-forward
1920   [backtab] gnus-summary-widget-backward
1921   "t" gnus-summary-toggle-header
1922   "g" gnus-summary-show-article
1923   "l" gnus-summary-goto-last-article
1924   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1925   "\C-d" gnus-summary-enter-digest-group
1926   "\M-\C-d" gnus-summary-read-document
1927   "\M-\C-e" gnus-summary-edit-parameters
1928   "\M-\C-a" gnus-summary-customize-parameters
1929   "\C-c\C-b" gnus-bug
1930   "*" gnus-cache-enter-article
1931   "\M-*" gnus-cache-remove-article
1932   "\M-&" gnus-summary-universal-argument
1933   "\C-l" gnus-recenter
1934   "I" gnus-summary-increase-score
1935   "L" gnus-summary-lower-score
1936   "\M-i" gnus-symbolic-argument
1937   "h" gnus-summary-select-article-buffer
1938
1939   "b" gnus-article-view-part
1940   "\M-t" gnus-summary-toggle-display-buttonized
1941
1942   "V" gnus-summary-score-map
1943   "X" gnus-uu-extract-map
1944   "S" gnus-summary-send-map)
1945
1946 ;; Sort of orthogonal keymap
1947 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1948   "t" gnus-summary-tick-article-forward
1949   "!" gnus-summary-tick-article-forward
1950   "d" gnus-summary-mark-as-read-forward
1951   "r" gnus-summary-mark-as-read-forward
1952   "c" gnus-summary-clear-mark-forward
1953   " " gnus-summary-clear-mark-forward
1954   "e" gnus-summary-mark-as-expirable
1955   "x" gnus-summary-mark-as-expirable
1956   "?" gnus-summary-mark-as-dormant
1957   "b" gnus-summary-set-bookmark
1958   "B" gnus-summary-remove-bookmark
1959   "#" gnus-summary-mark-as-processable
1960   "\M-#" gnus-summary-unmark-as-processable
1961   "S" gnus-summary-limit-include-expunged
1962   "C" gnus-summary-catchup
1963   "H" gnus-summary-catchup-to-here
1964   "h" gnus-summary-catchup-from-here
1965   "\C-c" gnus-summary-catchup-all
1966   "k" gnus-summary-kill-same-subject-and-select
1967   "K" gnus-summary-kill-same-subject
1968   "P" gnus-uu-mark-map)
1969
1970 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1971   "c" gnus-summary-clear-above
1972   "u" gnus-summary-tick-above
1973   "m" gnus-summary-mark-above
1974   "k" gnus-summary-kill-below)
1975
1976 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1977   "/" gnus-summary-limit-to-subject
1978   "n" gnus-summary-limit-to-articles
1979   "b" gnus-summary-limit-to-bodies
1980   "h" gnus-summary-limit-to-headers
1981   "w" gnus-summary-pop-limit
1982   "s" gnus-summary-limit-to-subject
1983   "a" gnus-summary-limit-to-author
1984   "u" gnus-summary-limit-to-unread
1985   "m" gnus-summary-limit-to-marks
1986   "M" gnus-summary-limit-exclude-marks
1987   "v" gnus-summary-limit-to-score
1988   "*" gnus-summary-limit-include-cached
1989   "D" gnus-summary-limit-include-dormant
1990   "T" gnus-summary-limit-include-thread
1991   "d" gnus-summary-limit-exclude-dormant
1992   "t" gnus-summary-limit-to-age
1993   "." gnus-summary-limit-to-unseen
1994   "x" gnus-summary-limit-to-extra
1995   "p" gnus-summary-limit-to-display-predicate
1996   "E" gnus-summary-limit-include-expunged
1997   "c" gnus-summary-limit-exclude-childless-dormant
1998   "C" gnus-summary-limit-mark-excluded-as-read
1999   "o" gnus-summary-insert-old-articles
2000   "N" gnus-summary-insert-new-articles
2001   "S" gnus-summary-limit-to-singletons
2002   "r" gnus-summary-limit-to-replied
2003   "R" gnus-summary-limit-to-recipient
2004   "A" gnus-summary-limit-to-address)
2005
2006 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2007   "n" gnus-summary-next-unread-article
2008   "p" gnus-summary-prev-unread-article
2009   "N" gnus-summary-next-article
2010   "P" gnus-summary-prev-article
2011   "\C-n" gnus-summary-next-same-subject
2012   "\C-p" gnus-summary-prev-same-subject
2013   "\M-n" gnus-summary-next-unread-subject
2014   "\M-p" gnus-summary-prev-unread-subject
2015   "f" gnus-summary-first-unread-article
2016   "b" gnus-summary-best-unread-article
2017   "j" gnus-summary-goto-article
2018   "g" gnus-summary-goto-subject
2019   "l" gnus-summary-goto-last-article
2020   "o" gnus-summary-pop-article)
2021
2022 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2023   "k" gnus-summary-kill-thread
2024   "E" gnus-summary-expire-thread
2025   "l" gnus-summary-lower-thread
2026   "i" gnus-summary-raise-thread
2027   "T" gnus-summary-toggle-threads
2028   "t" gnus-summary-rethread-current
2029   "^" gnus-summary-reparent-thread
2030   "\M-^" gnus-summary-reparent-children
2031   "s" gnus-summary-show-thread
2032   "S" gnus-summary-show-all-threads
2033   "h" gnus-summary-hide-thread
2034   "H" gnus-summary-hide-all-threads
2035   "n" gnus-summary-next-thread
2036   "p" gnus-summary-prev-thread
2037   "u" gnus-summary-up-thread
2038   "o" gnus-summary-top-thread
2039   "d" gnus-summary-down-thread
2040   "#" gnus-uu-mark-thread
2041   "\M-#" gnus-uu-unmark-thread)
2042
2043 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2044   "g" gnus-summary-prepare
2045   "c" gnus-summary-insert-cached-articles
2046   "d" gnus-summary-insert-dormant-articles
2047   "t" gnus-summary-insert-ticked-articles)
2048
2049 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2050   "c" gnus-summary-catchup-and-exit
2051   "C" gnus-summary-catchup-all-and-exit
2052   "E" gnus-summary-exit-no-update
2053   "Q" gnus-summary-exit
2054   "Z" gnus-summary-exit
2055   "n" gnus-summary-catchup-and-goto-next-group
2056   "p" gnus-summary-catchup-and-goto-prev-group
2057   "R" gnus-summary-reselect-current-group
2058   "G" gnus-summary-rescan-group
2059   "N" gnus-summary-next-group
2060   "s" gnus-summary-save-newsrc
2061   "P" gnus-summary-prev-group)
2062
2063 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2064   " " gnus-summary-next-page
2065   "n" gnus-summary-next-page
2066   [?\S-\ ] gnus-summary-prev-page
2067   "\177" gnus-summary-prev-page
2068   [delete] gnus-summary-prev-page
2069   "p" gnus-summary-prev-page
2070   "\r" gnus-summary-scroll-up
2071   "\M-\r" gnus-summary-scroll-down
2072   "<" gnus-summary-beginning-of-article
2073   ">" gnus-summary-end-of-article
2074   "b" gnus-summary-beginning-of-article
2075   "e" gnus-summary-end-of-article
2076   "^" gnus-summary-refer-parent-article
2077   "r" gnus-summary-refer-parent-article
2078   "C" gnus-summary-show-complete-article
2079   "D" gnus-summary-enter-digest-group
2080   "R" gnus-summary-refer-references
2081   "T" gnus-summary-refer-thread
2082   "W" gnus-warp-to-article
2083   "g" gnus-summary-show-article
2084   "s" gnus-summary-isearch-article
2085   "\t" gnus-summary-widget-forward
2086   [backtab] gnus-summary-widget-backward
2087   "P" gnus-summary-print-article
2088   "S" gnus-sticky-article
2089   "M" gnus-mailing-list-insinuate
2090   "t" gnus-article-babel)
2091
2092 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2093   "b" gnus-article-add-buttons
2094   "B" gnus-article-add-buttons-to-head
2095   "o" gnus-article-treat-overstrike
2096   "e" gnus-article-emphasize
2097   "w" gnus-article-fill-cited-article
2098   "Q" gnus-article-fill-long-lines
2099   "L" gnus-article-toggle-truncate-lines
2100   "C" gnus-article-capitalize-sentences
2101   "c" gnus-article-remove-cr
2102   "q" gnus-article-de-quoted-unreadable
2103   "6" gnus-article-de-base64-unreadable
2104   "Z" gnus-article-decode-HZ
2105   "A" gnus-article-treat-ansi-sequences
2106   "h" gnus-article-wash-html
2107   "u" gnus-article-unsplit-urls
2108   "s" gnus-summary-force-verify-and-decrypt
2109   "f" gnus-article-display-x-face
2110   "l" gnus-summary-stop-page-breaking
2111   "r" gnus-summary-caesar-message
2112   "m" gnus-summary-morse-message
2113   "t" gnus-summary-toggle-header
2114   "g" gnus-treat-smiley
2115   "v" gnus-summary-verbose-headers
2116   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2117   "p" gnus-article-verify-x-pgp-sig
2118   "d" gnus-article-treat-dumbquotes
2119   "U" gnus-article-treat-non-ascii
2120   "i" gnus-summary-idna-message)
2121
2122 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2123   ;; mnemonic: deuglif*Y*
2124   "u" gnus-article-outlook-unwrap-lines
2125   "a" gnus-article-outlook-repair-attribution
2126   "c" gnus-article-outlook-rearrange-citation
2127   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2128
2129 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2130   "a" gnus-article-hide
2131   "h" gnus-article-hide-headers
2132   "b" gnus-article-hide-boring-headers
2133   "s" gnus-article-hide-signature
2134   "c" gnus-article-hide-citation
2135   "C" gnus-article-hide-citation-in-followups
2136   "l" gnus-article-hide-list-identifiers
2137   "B" gnus-article-strip-banner
2138   "P" gnus-article-hide-pem
2139   "\C-c" gnus-article-hide-citation-maybe)
2140
2141 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2142   "a" gnus-article-highlight
2143   "h" gnus-article-highlight-headers
2144   "c" gnus-article-highlight-citation
2145   "s" gnus-article-highlight-signature)
2146
2147 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2148   "f" gnus-article-treat-fold-headers
2149   "u" gnus-article-treat-unfold-headers
2150   "n" gnus-article-treat-fold-newsgroups)
2151
2152 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2153   "x" gnus-article-display-x-face
2154   "d" gnus-article-display-face
2155   "s" gnus-treat-smiley
2156   "D" gnus-article-remove-images
2157   "W" gnus-article-show-images
2158   "f" gnus-treat-from-picon
2159   "m" gnus-treat-mail-picon
2160   "n" gnus-treat-newsgroups-picon
2161   "g" gnus-treat-from-gravatar
2162   "h" gnus-treat-mail-gravatar)
2163
2164 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2165   "w" gnus-article-decode-mime-words
2166   "c" gnus-article-decode-charset
2167   "v" gnus-mime-view-all-parts
2168   "b" gnus-article-view-part)
2169
2170 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2171   "z" gnus-article-date-ut
2172   "u" gnus-article-date-ut
2173   "l" gnus-article-date-local
2174   "p" gnus-article-date-english
2175   "e" gnus-article-date-lapsed
2176   "o" gnus-article-date-original
2177   "i" gnus-article-date-iso8601
2178   "s" gnus-article-date-user)
2179
2180 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2181   "t" gnus-article-remove-trailing-blank-lines
2182   "l" gnus-article-strip-leading-blank-lines
2183   "m" gnus-article-strip-multiple-blank-lines
2184   "a" gnus-article-strip-blank-lines
2185   "A" gnus-article-strip-all-blank-lines
2186   "s" gnus-article-strip-leading-space
2187   "e" gnus-article-strip-trailing-space
2188   "w" gnus-article-remove-leading-whitespace)
2189
2190 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2191   "v" gnus-version
2192   "d" gnus-summary-describe-group
2193   "h" gnus-summary-describe-briefly
2194   "i" gnus-info-find-node)
2195
2196 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2197   "e" gnus-summary-expire-articles
2198   "\M-\C-e" gnus-summary-expire-articles-now
2199   "\177" gnus-summary-delete-article
2200   [delete] gnus-summary-delete-article
2201   [backspace] gnus-summary-delete-article
2202   "m" gnus-summary-move-article
2203   "r" gnus-summary-respool-article
2204   "w" gnus-summary-edit-article
2205   "c" gnus-summary-copy-article
2206   "B" gnus-summary-crosspost-article
2207   "q" gnus-summary-respool-query
2208   "t" gnus-summary-respool-trace
2209   "i" gnus-summary-import-article
2210   "I" gnus-summary-create-article
2211   "p" gnus-summary-article-posted-p)
2212
2213 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2214   "o" gnus-summary-save-article
2215   "m" gnus-summary-save-article-mail
2216   "F" gnus-summary-write-article-file
2217   "r" gnus-summary-save-article-rmail
2218   "f" gnus-summary-save-article-file
2219   "b" gnus-summary-save-article-body-file
2220   "B" gnus-summary-write-article-body-file
2221   "h" gnus-summary-save-article-folder
2222   "v" gnus-summary-save-article-vm
2223   "p" gnus-summary-pipe-output
2224   "P" gnus-summary-muttprint)
2225
2226 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2227   "b" gnus-summary-display-buttonized
2228   "m" gnus-summary-repair-multipart
2229   "v" gnus-article-view-part
2230   "o" gnus-article-save-part
2231   "O" gnus-article-save-part-and-strip
2232   "r" gnus-article-replace-part
2233   "d" gnus-article-delete-part
2234   "t" gnus-article-view-part-as-type
2235   "j" gnus-article-jump-to-part
2236   "c" gnus-article-copy-part
2237   "C" gnus-article-view-part-as-charset
2238   "e" gnus-article-view-part-externally
2239   "H" gnus-article-browse-html-article
2240   "E" gnus-article-encrypt-body
2241   "i" gnus-article-inline-part
2242   "|" gnus-article-pipe-part)
2243
2244 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2245   "p" gnus-summary-mark-as-processable
2246   "u" gnus-summary-unmark-as-processable
2247   "U" gnus-summary-unmark-all-processable
2248   "v" gnus-uu-mark-over
2249   "s" gnus-uu-mark-series
2250   "r" gnus-uu-mark-region
2251   "g" gnus-uu-unmark-region
2252   "R" gnus-uu-mark-by-regexp
2253   "G" gnus-uu-unmark-by-regexp
2254   "t" gnus-uu-mark-thread
2255   "T" gnus-uu-unmark-thread
2256   "a" gnus-uu-mark-all
2257   "b" gnus-uu-mark-buffer
2258   "S" gnus-uu-mark-sparse
2259   "k" gnus-summary-kill-process-mark
2260   "y" gnus-summary-yank-process-mark
2261   "w" gnus-summary-save-process-mark
2262   "i" gnus-uu-invert-processable)
2263
2264 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2265   ;;"x" gnus-uu-extract-any
2266   "m" gnus-summary-save-parts
2267   "u" gnus-uu-decode-uu
2268   "U" gnus-uu-decode-uu-and-save
2269   "s" gnus-uu-decode-unshar
2270   "S" gnus-uu-decode-unshar-and-save
2271   "o" gnus-uu-decode-save
2272   "O" gnus-uu-decode-save
2273   "b" gnus-uu-decode-binhex
2274   "B" gnus-uu-decode-binhex
2275   "Y" gnus-uu-decode-yenc
2276   "p" gnus-uu-decode-postscript
2277   "P" gnus-uu-decode-postscript-and-save)
2278
2279 (gnus-define-keys
2280     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2281   "u" gnus-uu-decode-uu-view
2282   "U" gnus-uu-decode-uu-and-save-view
2283   "s" gnus-uu-decode-unshar-view
2284   "S" gnus-uu-decode-unshar-and-save-view
2285   "o" gnus-uu-decode-save-view
2286   "O" gnus-uu-decode-save-view
2287   "b" gnus-uu-decode-binhex-view
2288   "B" gnus-uu-decode-binhex-view
2289   "p" gnus-uu-decode-postscript-view
2290   "P" gnus-uu-decode-postscript-and-save-view)
2291
2292 (defvar gnus-article-post-menu nil)
2293
2294 (defconst gnus-summary-menu-maxlen 20)
2295
2296 (defun gnus-summary-menu-split (menu)
2297   ;; If we have lots of elements, divide them into groups of 20
2298   ;; and make a pane (or submenu) for each one.
2299   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2300       (let ((menu menu) sublists next
2301             (i 1))
2302         (while menu
2303           ;; Pull off the next gnus-summary-menu-maxlen elements
2304           ;; and make them the next element of sublist.
2305           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2306           (if next
2307               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2308                       nil))
2309           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2310                                              (aref (car (last menu)) 0)) menu)
2311                                sublists))
2312           (setq i (1+ i))
2313           (setq menu next))
2314         (nreverse sublists))
2315     ;; Few elements--put them all in one pane.
2316     menu))
2317
2318 (defun gnus-summary-make-menu-bar ()
2319   (gnus-turn-off-edit-menu 'summary)
2320
2321   (unless (boundp 'gnus-summary-misc-menu)
2322
2323     (easy-menu-define
2324       gnus-summary-kill-menu gnus-summary-mode-map ""
2325       (cons
2326        "Score"
2327        (nconc
2328         (list
2329          ["Customize" gnus-score-customize t])
2330         (gnus-make-score-map 'increase)
2331         (gnus-make-score-map 'lower)
2332         '(("Mark"
2333            ["Kill below" gnus-summary-kill-below t]
2334            ["Mark above" gnus-summary-mark-above t]
2335            ["Tick above" gnus-summary-tick-above t]
2336            ["Clear above" gnus-summary-clear-above t])
2337           ["Current score" gnus-summary-current-score t]
2338           ["Set score" gnus-summary-set-score t]
2339           ["Switch current score file..." gnus-score-change-score-file t]
2340           ["Set mark below..." gnus-score-set-mark-below t]
2341           ["Set expunge below..." gnus-score-set-expunge-below t]
2342           ["Edit current score file" gnus-score-edit-current-scores t]
2343           ["Edit score file..." gnus-score-edit-file t]
2344           ["Trace score" gnus-score-find-trace t]
2345           ["Find words" gnus-score-find-favourite-words t]
2346           ["Rescore buffer" gnus-summary-rescore t]
2347           ["Increase score..." gnus-summary-increase-score t]
2348           ["Lower score..." gnus-summary-lower-score t]))))
2349
2350     ;; Define both the Article menu in the summary buffer and the
2351     ;; equivalent Commands menu in the article buffer here for
2352     ;; consistency.
2353     (let ((innards
2354            `(("Hide"
2355               ["All" gnus-article-hide t]
2356               ["Headers" gnus-article-hide-headers t]
2357               ["Signature" gnus-article-hide-signature t]
2358               ["Citation" gnus-article-hide-citation t]
2359               ["List identifiers" gnus-article-hide-list-identifiers t]
2360               ["Banner" gnus-article-strip-banner t]
2361               ["Boring headers" gnus-article-hide-boring-headers t])
2362              ("Highlight"
2363               ["All" gnus-article-highlight t]
2364               ["Headers" gnus-article-highlight-headers t]
2365               ["Signature" gnus-article-highlight-signature t]
2366               ["Citation" gnus-article-highlight-citation t])
2367              ("MIME"
2368               ["Words" gnus-article-decode-mime-words t]
2369               ["Charset" gnus-article-decode-charset t]
2370               ["QP" gnus-article-de-quoted-unreadable t]
2371               ["Base64" gnus-article-de-base64-unreadable t]
2372               ["View MIME buttons" gnus-summary-display-buttonized t]
2373               ["View all" gnus-mime-view-all-parts t]
2374               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2375               ["Encrypt body" gnus-article-encrypt-body
2376                :active (not (gnus-group-read-only-p))
2377                ,@(if (featurep 'xemacs) nil
2378                    '(:help "Encrypt the message body on disk"))]
2379               ["Extract all parts..." gnus-summary-save-parts t]
2380               ("Multipart"
2381                ["Repair multipart" gnus-summary-repair-multipart t]
2382                ["Pipe part..." gnus-article-pipe-part t]
2383                ["Inline part" gnus-article-inline-part t]
2384                ["View part as type..." gnus-article-view-part-as-type t]
2385                ["Encrypt body" gnus-article-encrypt-body
2386                 :active (not (gnus-group-read-only-p))
2387                ,@(if (featurep 'xemacs) nil
2388                    '(:help "Encrypt the message body on disk"))]
2389                ["View part externally" gnus-article-view-part-externally t]
2390                ["View HTML parts in browser" gnus-article-browse-html-article t]
2391                ["View part with charset..." gnus-article-view-part-as-charset t]
2392                ["Copy part" gnus-article-copy-part t]
2393                ["Save part..." gnus-article-save-part t]
2394                ["View part" gnus-article-view-part t]))
2395              ("Date"
2396               ["Local" gnus-article-date-local t]
2397               ["ISO8601" gnus-article-date-iso8601 t]
2398               ["UT" gnus-article-date-ut t]
2399               ["Original" gnus-article-date-original t]
2400               ["Lapsed" gnus-article-date-lapsed t]
2401               ["User-defined" gnus-article-date-user t])
2402              ("Display"
2403               ["Remove images" gnus-article-remove-images t]
2404               ["Toggle smiley" gnus-treat-smiley t]
2405               ["Show X-Face" gnus-article-display-x-face t]
2406               ["Show picons in From" gnus-treat-from-picon t]
2407               ["Show picons in mail headers" gnus-treat-mail-picon t]
2408               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2409               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2410               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2411               ("View as different encoding"
2412                ,@(gnus-summary-menu-split
2413                   (mapcar
2414                    (lambda (cs)
2415                      ;; Since easymenu under Emacs doesn't allow
2416                      ;; lambda forms for menu commands, we should
2417                      ;; provide intern'ed function symbols.
2418                      (let ((command (intern (format "\
2419 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2420                        (fset command
2421                              `(lambda ()
2422                                 (interactive)
2423                                 (let ((gnus-summary-show-article-charset-alist
2424                                        '((1 . ,cs))))
2425                                   (gnus-summary-show-article 1))))
2426                        `[,(symbol-name cs) ,command t]))
2427                    (sort (if (fboundp 'coding-system-list)
2428                              (coding-system-list)
2429                            (mapcar 'car mm-mime-mule-charset-alist))
2430                          'string<)))))
2431              ("Washing"
2432               ("Remove Blanks"
2433                ["Leading" gnus-article-strip-leading-blank-lines t]
2434                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2435                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2436                ["All of the above" gnus-article-strip-blank-lines t]
2437                ["All" gnus-article-strip-all-blank-lines t]
2438                ["Leading space" gnus-article-strip-leading-space t]
2439                ["Trailing space" gnus-article-strip-trailing-space t]
2440                ["Leading space in headers"
2441                 gnus-article-remove-leading-whitespace t])
2442               ["Overstrike" gnus-article-treat-overstrike t]
2443               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2444               ["Non-ASCII" gnus-article-treat-non-ascii t]
2445               ["Emphasis" gnus-article-emphasize t]
2446               ["Word wrap" gnus-article-fill-cited-article t]
2447               ["Fill long lines" gnus-article-fill-long-lines t]
2448               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2449               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2450               ["Remove CR" gnus-article-remove-cr t]
2451               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2452               ["Base64" gnus-article-de-base64-unreadable t]
2453               ["Rot 13" gnus-summary-caesar-message
2454                ,@(if (featurep 'xemacs) '(t)
2455                    '(:help "\"Caesar rotate\" article by 13"))]
2456               ["De-IDNA" gnus-summary-idna-message t]
2457               ["Morse decode" gnus-summary-morse-message t]
2458               ["Unix pipe..." gnus-summary-pipe-message t]
2459               ["Add buttons" gnus-article-add-buttons t]
2460               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2461               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2462               ["Verbose header" gnus-summary-verbose-headers t]
2463               ["Toggle header" gnus-summary-toggle-header t]
2464               ["Unfold headers" gnus-article-treat-unfold-headers t]
2465               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2466               ["Html" gnus-article-wash-html t]
2467               ["Unsplit URLs" gnus-article-unsplit-urls t]
2468               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2469               ["Decode HZ" gnus-article-decode-HZ t]
2470               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2471               ("(Outlook) Deuglify"
2472                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2473                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2474                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2475                ["Full (Outlook) deuglify"
2476                 gnus-article-outlook-deuglify-article t])
2477               )
2478              ("Output"
2479               ["Save in default format..." gnus-summary-save-article
2480                ,@(if (featurep 'xemacs) '(t)
2481                    '(:help "Save article using default method"))]
2482               ["Save in file..." gnus-summary-save-article-file
2483                ,@(if (featurep 'xemacs) '(t)
2484                    '(:help "Save article in file"))]
2485               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2486               ["Save in MH folder..." gnus-summary-save-article-folder t]
2487               ["Save in VM folder..." gnus-summary-save-article-vm t]
2488               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2489               ["Save body in file..." gnus-summary-save-article-body-file t]
2490               ["Pipe through a filter..." gnus-summary-pipe-output t]
2491               ["Print with Muttprint..." gnus-summary-muttprint t]
2492               ["Print" gnus-summary-print-article
2493                ,@(if (featurep 'xemacs) '(t)
2494                    '(:help "Generate and print a PostScript image"))])
2495              ("Copy, move,... (Backend)"
2496               ,@(if (featurep 'xemacs) nil
2497                   '(:help "Copying, moving, expiring articles..."))
2498               ["Respool article..." gnus-summary-respool-article t]
2499               ["Move article..." gnus-summary-move-article
2500                (gnus-check-backend-function
2501                 'request-move-article gnus-newsgroup-name)]
2502               ["Copy article..." gnus-summary-copy-article t]
2503               ["Crosspost article..." gnus-summary-crosspost-article
2504                (gnus-check-backend-function
2505                 'request-replace-article gnus-newsgroup-name)]
2506               ["Import file..." gnus-summary-import-article
2507                (gnus-check-backend-function
2508                 'request-accept-article gnus-newsgroup-name)]
2509               ["Create article..." gnus-summary-create-article
2510                (gnus-check-backend-function
2511                 'request-accept-article gnus-newsgroup-name)]
2512               ["Check if posted" gnus-summary-article-posted-p t]
2513               ["Edit article" gnus-summary-edit-article
2514                (not (gnus-group-read-only-p))]
2515               ["Delete article" gnus-summary-delete-article
2516                (gnus-check-backend-function
2517                 'request-expire-articles gnus-newsgroup-name)]
2518               ["Query respool" gnus-summary-respool-query t]
2519               ["Trace respool" gnus-summary-respool-trace t]
2520               ["Delete expirable articles" gnus-summary-expire-articles-now
2521                (gnus-check-backend-function
2522                 'request-expire-articles gnus-newsgroup-name)])
2523              ("Extract"
2524               ["Uudecode" gnus-uu-decode-uu
2525                ,@(if (featurep 'xemacs) '(t)
2526                    '(:help "Decode uuencoded article(s)"))]
2527               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2528               ["Unshar" gnus-uu-decode-unshar t]
2529               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2530               ["Save" gnus-uu-decode-save t]
2531               ["Binhex" gnus-uu-decode-binhex t]
2532               ["PostScript" gnus-uu-decode-postscript t]
2533               ["All MIME parts" gnus-summary-save-parts t])
2534              ("Cache"
2535               ["Enter article" gnus-cache-enter-article t]
2536               ["Remove article" gnus-cache-remove-article t])
2537              ["Translate" gnus-article-babel t]
2538              ["Select article buffer" gnus-summary-select-article-buffer t]
2539              ["Make article buffer sticky" gnus-sticky-article t]
2540              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2541              ["Isearch article..." gnus-summary-isearch-article t]
2542              ["Beginning of the article" gnus-summary-beginning-of-article t]
2543              ["End of the article" gnus-summary-end-of-article t]
2544              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2545              ["Fetch referenced articles" gnus-summary-refer-references t]
2546              ["Fetch current thread" gnus-summary-refer-thread t]
2547              ["Fetch article with id..." gnus-summary-refer-article t]
2548              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2549              ["Redisplay" gnus-summary-show-article t]
2550              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2551       (easy-menu-define
2552         gnus-summary-article-menu gnus-summary-mode-map ""
2553         (cons "Article" innards))
2554
2555       (if (not (keymapp gnus-summary-article-menu))
2556           (easy-menu-define
2557             gnus-article-commands-menu gnus-article-mode-map ""
2558             (cons "Commands" innards))
2559         ;; in Emacs, don't share menu.
2560         (setq gnus-article-commands-menu
2561               (copy-keymap gnus-summary-article-menu))
2562         (define-key gnus-article-mode-map [menu-bar commands]
2563           (cons "Commands" gnus-article-commands-menu))))
2564
2565     (easy-menu-define
2566       gnus-summary-thread-menu gnus-summary-mode-map ""
2567       '("Threads"
2568         ["Find all messages in thread" gnus-summary-refer-thread t]
2569         ["Toggle threading" gnus-summary-toggle-threads t]
2570         ["Hide threads" gnus-summary-hide-all-threads t]
2571         ["Show threads" gnus-summary-show-all-threads t]
2572         ["Hide thread" gnus-summary-hide-thread t]
2573         ["Show thread" gnus-summary-show-thread t]
2574         ["Go to next thread" gnus-summary-next-thread t]
2575         ["Go to previous thread" gnus-summary-prev-thread t]
2576         ["Go down thread" gnus-summary-down-thread t]
2577         ["Go up thread" gnus-summary-up-thread t]
2578         ["Top of thread" gnus-summary-top-thread t]
2579         ["Mark thread as read" gnus-summary-kill-thread t]
2580         ["Mark thread as expired" gnus-summary-expire-thread t]
2581         ["Lower thread score" gnus-summary-lower-thread t]
2582         ["Raise thread score" gnus-summary-raise-thread t]
2583         ["Rethread current" gnus-summary-rethread-current t]))
2584
2585     (easy-menu-define
2586       gnus-summary-post-menu gnus-summary-mode-map ""
2587       `("Post"
2588         ["Send a message (mail or news)" gnus-summary-post-news
2589          ,@(if (featurep 'xemacs) '(t)
2590              '(:help "Compose a new message (mail or news)"))]
2591         ["Followup" gnus-summary-followup
2592          ,@(if (featurep 'xemacs) '(t)
2593              '(:help "Post followup to this article"))]
2594         ["Followup and yank" gnus-summary-followup-with-original
2595          ,@(if (featurep 'xemacs) '(t)
2596              '(:help "Post followup to this article, quoting its contents"))]
2597         ["Supersede article" gnus-summary-supersede-article t]
2598         ["Cancel article" gnus-summary-cancel-article
2599          ,@(if (featurep 'xemacs) '(t)
2600              '(:help "Cancel an article you posted"))]
2601         ["Reply" gnus-summary-reply t]
2602         ["Reply and yank" gnus-summary-reply-with-original t]
2603         ["Wide reply" gnus-summary-wide-reply t]
2604         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2605          ,@(if (featurep 'xemacs) '(t)
2606              '(:help "Mail a reply, quoting this article"))]
2607         ["Very wide reply" gnus-summary-very-wide-reply t]
2608         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2609          ,@(if (featurep 'xemacs) '(t)
2610              '(:help "Mail a very wide reply, quoting this article"))]
2611         ["Mail forward" gnus-summary-mail-forward t]
2612         ["Post forward" gnus-summary-post-forward t]
2613         ["Digest and mail" gnus-uu-digest-mail-forward t]
2614         ["Digest and post" gnus-uu-digest-post-forward t]
2615         ["Resend message" gnus-summary-resend-message t]
2616         ["Resend message edit" gnus-summary-resend-message-edit t]
2617         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2618         ["Send a mail" gnus-summary-mail-other-window t]
2619         ["Create a local message" gnus-summary-news-other-window t]
2620         ["Uuencode and post" gnus-uu-post-news
2621          ,@(if (featurep 'xemacs) '(t)
2622              '(:help "Post a uuencoded article"))]
2623         ["Followup via news" gnus-summary-followup-to-mail t]
2624         ["Followup via news and yank"
2625          gnus-summary-followup-to-mail-with-original t]
2626         ["Strip signature on reply"
2627          (lambda ()
2628            (interactive)
2629            (if (not (memq message-cite-function
2630                           '(message-cite-original-without-signature
2631                             message-cite-original)))
2632                ;; Stupid workaround for XEmacs not honoring :visible.
2633                (message "Can't toggle this value of `message-cite-function'")
2634              (setq message-cite-function
2635                    (if (eq message-cite-function
2636                            'message-cite-original-without-signature)
2637                        'message-cite-original
2638                      'message-cite-original-without-signature))))
2639          ;; XEmacs barfs on :visible.
2640          ,@(if (featurep 'xemacs) nil
2641              '(:visible (memq message-cite-function
2642                               '(message-cite-original-without-signature
2643                                 message-cite-original))))
2644          :style toggle
2645          :selected (eq message-cite-function
2646                        'message-cite-original-without-signature)
2647          ,@(if (featurep 'xemacs) nil
2648              '(:help "Strip signature from cited article when replying."))]
2649         ;;("Draft"
2650         ;;["Send" gnus-summary-send-draft t]
2651         ;;["Send bounced" gnus-resend-bounced-mail t])
2652         ))
2653
2654     (cond
2655      ((not (keymapp gnus-summary-post-menu))
2656       (setq gnus-article-post-menu gnus-summary-post-menu))
2657      ((not gnus-article-post-menu)
2658       ;; Don't share post menu.
2659       (setq gnus-article-post-menu
2660             (copy-keymap gnus-summary-post-menu))))
2661     (define-key gnus-article-mode-map [menu-bar post]
2662       (cons "Post" gnus-article-post-menu))
2663
2664     (easy-menu-define
2665       gnus-summary-misc-menu gnus-summary-mode-map ""
2666       `("Gnus"
2667         ("Mark Read"
2668          ["Mark as read" gnus-summary-mark-as-read-forward t]
2669          ["Mark same subject and select"
2670           gnus-summary-kill-same-subject-and-select t]
2671          ["Mark same subject" gnus-summary-kill-same-subject t]
2672          ["Catchup" gnus-summary-catchup
2673           ,@(if (featurep 'xemacs) '(t)
2674               '(:help "Mark unread articles in this group as read"))]
2675          ["Catchup all" gnus-summary-catchup-all t]
2676          ["Catchup to here" gnus-summary-catchup-to-here t]
2677          ["Catchup from here" gnus-summary-catchup-from-here t]
2678          ["Catchup region" gnus-summary-mark-region-as-read
2679           (gnus-mark-active-p)]
2680          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2681         ("Mark Various"
2682          ["Tick" gnus-summary-tick-article-forward t]
2683          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2684          ["Remove marks" gnus-summary-clear-mark-forward t]
2685          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2686          ["Set bookmark" gnus-summary-set-bookmark t]
2687          ["Remove bookmark" gnus-summary-remove-bookmark t])
2688         ("Limit to"
2689          ["Marks..." gnus-summary-limit-to-marks t]
2690          ["Subject..." gnus-summary-limit-to-subject t]
2691          ["Author..." gnus-summary-limit-to-author t]
2692          ["Recipient..." gnus-summary-limit-to-recipient t]
2693          ["Address..." gnus-summary-limit-to-address t]
2694          ["Age..." gnus-summary-limit-to-age t]
2695          ["Extra..." gnus-summary-limit-to-extra t]
2696          ["Score..." gnus-summary-limit-to-score t]
2697          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2698          ["Unread" gnus-summary-limit-to-unread t]
2699          ["Unseen" gnus-summary-limit-to-unseen t]
2700          ["Singletons" gnus-summary-limit-to-singletons t]
2701          ["Replied" gnus-summary-limit-to-replied t]
2702          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2703          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2704          ["Pop limit" gnus-summary-pop-limit t]
2705          ["Show dormant" gnus-summary-limit-include-dormant t]
2706          ["Hide childless dormant"
2707           gnus-summary-limit-exclude-childless-dormant t]
2708          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2709          ["Hide marked" gnus-summary-limit-exclude-marks t]
2710          ["Show expunged" gnus-summary-limit-include-expunged t])
2711         ("Process Mark"
2712          ["Set mark" gnus-summary-mark-as-processable t]
2713          ["Remove mark" gnus-summary-unmark-as-processable t]
2714          ["Remove all marks" gnus-summary-unmark-all-processable t]
2715          ["Invert marks" gnus-uu-invert-processable t]
2716          ["Mark above" gnus-uu-mark-over t]
2717          ["Mark series" gnus-uu-mark-series t]
2718          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2719          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2720          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2721          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2722          ["Mark all" gnus-uu-mark-all t]
2723          ["Mark buffer" gnus-uu-mark-buffer t]
2724          ["Mark sparse" gnus-uu-mark-sparse t]
2725          ["Mark thread" gnus-uu-mark-thread t]
2726          ["Unmark thread" gnus-uu-unmark-thread t]
2727          ("Process Mark Sets"
2728           ["Kill" gnus-summary-kill-process-mark t]
2729           ["Yank" gnus-summary-yank-process-mark
2730            gnus-newsgroup-process-stack]
2731           ["Save" gnus-summary-save-process-mark t]
2732           ["Run command on marked..." gnus-summary-universal-argument t]))
2733         ("Registry Marks")
2734         ("Scroll article"
2735          ["Page forward" gnus-summary-next-page
2736           ,@(if (featurep 'xemacs) '(t)
2737               '(:help "Show next page of article"))]
2738          ["Page backward" gnus-summary-prev-page
2739           ,@(if (featurep 'xemacs) '(t)
2740               '(:help "Show previous page of article"))]
2741          ["Line forward" gnus-summary-scroll-up t])
2742         ("Move"
2743          ["Next unread article" gnus-summary-next-unread-article t]
2744          ["Previous unread article" gnus-summary-prev-unread-article t]
2745          ["Next article" gnus-summary-next-article t]
2746          ["Previous article" gnus-summary-prev-article t]
2747          ["Next unread subject" gnus-summary-next-unread-subject t]
2748          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2749          ["Next article same subject" gnus-summary-next-same-subject t]
2750          ["Previous article same subject" gnus-summary-prev-same-subject t]
2751          ["First unread article" gnus-summary-first-unread-article t]
2752          ["Best unread article" gnus-summary-best-unread-article t]
2753          ["Go to subject number..." gnus-summary-goto-subject t]
2754          ["Go to article number..." gnus-summary-goto-article t]
2755          ["Go to the last article" gnus-summary-goto-last-article t]
2756          ["Pop article off history" gnus-summary-pop-article t])
2757         ("Sort"
2758          ["Sort by number" gnus-summary-sort-by-number t]
2759          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2760          ["Sort by author" gnus-summary-sort-by-author t]
2761          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2762          ["Sort by subject" gnus-summary-sort-by-subject t]
2763          ["Sort by date" gnus-summary-sort-by-date t]
2764          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2765          ["Sort by score" gnus-summary-sort-by-score t]
2766          ["Sort by lines" gnus-summary-sort-by-lines t]
2767          ["Sort by characters" gnus-summary-sort-by-chars t]
2768          ["Randomize" gnus-summary-sort-by-random t]
2769          ["Original sort" gnus-summary-sort-by-original t])
2770         ("Help"
2771          ["Describe group" gnus-summary-describe-group t]
2772          ["Read manual" gnus-info-find-node t])
2773         ("Modes"
2774          ["Pick and read" gnus-pick-mode t]
2775          ["Binary" gnus-binary-mode t])
2776         ("Regeneration"
2777          ["Regenerate" gnus-summary-prepare t]
2778          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2779          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2780          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2781          ["Toggle threading" gnus-summary-toggle-threads t])
2782         ["See old articles" gnus-summary-insert-old-articles t]
2783         ["See new articles" gnus-summary-insert-new-articles t]
2784         ["Filter articles..." gnus-summary-execute-command t]
2785         ["Run command on articles..." gnus-summary-universal-argument t]
2786         ["Search articles forward..." gnus-summary-search-article-forward t]
2787         ["Search articles backward..." gnus-summary-search-article-backward t]
2788         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2789         ["Expand window" gnus-summary-expand-window t]
2790         ["Expire expirable articles" gnus-summary-expire-articles
2791          (gnus-check-backend-function
2792           'request-expire-articles gnus-newsgroup-name)]
2793         ["Edit local kill file" gnus-summary-edit-local-kill t]
2794         ["Edit main kill file" gnus-summary-edit-global-kill t]
2795         ["Edit group parameters" gnus-summary-edit-parameters t]
2796         ["Customize group parameters" gnus-summary-customize-parameters t]
2797         ["Send a bug report" gnus-bug t]
2798         ("Exit"
2799          ["Catchup and exit" gnus-summary-catchup-and-exit
2800           ,@(if (featurep 'xemacs) '(t)
2801               '(:help "Mark unread articles in this group as read, then exit"))]
2802          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2803          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2804          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2805          ["Exit group" gnus-summary-exit
2806           ,@(if (featurep 'xemacs) '(t)
2807               '(:help "Exit current group, return to group selection mode"))]
2808          ["Exit group without updating" gnus-summary-exit-no-update t]
2809          ["Exit and goto next group" gnus-summary-next-group t]
2810          ["Exit and goto prev group" gnus-summary-prev-group t]
2811          ["Reselect group" gnus-summary-reselect-current-group t]
2812          ["Rescan group" gnus-summary-rescan-group t]
2813          ["Update dribble" gnus-summary-save-newsrc t])))
2814
2815     (gnus-run-hooks 'gnus-summary-menu-hook)))
2816
2817 (defvar gnus-summary-tool-bar-map nil)
2818
2819 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2820 ;; affect _new_ message buffers.  We might add a function that walks thru all
2821 ;; summary-mode buffers and force the update.
2822 (defun gnus-summary-tool-bar-update (&optional symbol value)
2823   "Update summary mode toolbar.
2824 Setter function for custom variables."
2825   (setq-default gnus-summary-tool-bar-map nil)
2826   (when symbol
2827     ;; When used as ":set" function:
2828     (set-default symbol value))
2829   (when (gnus-buffer-live-p gnus-summary-buffer)
2830     (with-current-buffer gnus-summary-buffer
2831       (gnus-summary-make-tool-bar))))
2832
2833 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2834                                      'gnus-summary-tool-bar-gnome
2835                                    'gnus-summary-tool-bar-retro)
2836   "Specifies the Gnus summary tool bar.
2837
2838 It can be either a list or a symbol referring to a list.  See
2839 `gmm-tool-bar-from-list' for the format of the list.  The
2840 default key map is `gnus-summary-mode-map'.
2841
2842 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2843 `gnus-summary-tool-bar-retro'."
2844   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2845                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2846                  (repeat :tag "User defined list" gmm-tool-bar-item)
2847                  (symbol))
2848   :version "23.1" ;; No Gnus
2849   :initialize 'custom-initialize-default
2850   :set 'gnus-summary-tool-bar-update
2851   :group 'gnus-summary)
2852
2853 (defcustom gnus-summary-tool-bar-gnome
2854   '((gnus-summary-post-news "mail/compose" nil)
2855     (gnus-summary-insert-new-articles "mail/inbox" nil
2856                                       :visible (or (not gnus-agent)
2857                                                    gnus-plugged))
2858     (gnus-summary-reply-with-original "mail/reply")
2859     (gnus-summary-reply "mail/reply" nil :visible nil)
2860     (gnus-summary-followup-with-original "mail/reply-all")
2861     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2862     (gnus-summary-mail-forward "mail/forward")
2863     (gnus-summary-save-article "mail/save")
2864     (gnus-summary-search-article-forward "search" nil :visible nil)
2865     (gnus-summary-print-article "print")
2866     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2867     ;; Some new commands that may need more suitable icons:
2868     (gnus-summary-save-newsrc "save" nil :visible nil)
2869     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2870     (gnus-summary-prev-article "left-arrow")
2871     (gnus-summary-next-article "right-arrow")
2872     (gnus-summary-next-page "next-page")
2873     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2874     ;;
2875     ;; Maybe some sort-by-... could be added:
2876     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2877     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2878     (gnus-summary-mark-as-expirable
2879      "delete" nil
2880      :visible (gnus-check-backend-function 'request-expire-articles
2881                                            gnus-newsgroup-name))
2882     (gnus-summary-mark-as-spam
2883      "mail/spam" t
2884      :visible (and (fboundp 'spam-group-ham-contents-p)
2885                    (spam-group-ham-contents-p gnus-newsgroup-name))
2886      :help "Mark as spam")
2887     (gnus-summary-mark-as-read-forward
2888      "mail/not-spam" nil
2889      :visible (and (fboundp 'spam-group-spam-contents-p)
2890                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2891     ;;
2892     (gnus-summary-exit "exit")
2893     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2894     (gnus-info-find-node "help"))
2895   "List of functions for the summary tool bar (GNOME style).
2896
2897 See `gmm-tool-bar-from-list' for the format of the list."
2898   :type '(repeat gmm-tool-bar-item)
2899   :version "23.1" ;; No Gnus
2900   :initialize 'custom-initialize-default
2901   :set 'gnus-summary-tool-bar-update
2902   :group 'gnus-summary)
2903
2904 (defcustom gnus-summary-tool-bar-retro
2905   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2906     (gnus-summary-next-unread-article "gnus/next-ur")
2907     (gnus-summary-post-news "gnus/post")
2908     (gnus-summary-followup-with-original "gnus/fuwo")
2909     (gnus-summary-followup "gnus/followup")
2910     (gnus-summary-reply-with-original "gnus/reply-wo")
2911     (gnus-summary-reply "gnus/reply")
2912     (gnus-summary-caesar-message "gnus/rot13")
2913     (gnus-uu-decode-uu "gnus/uu-decode")
2914     (gnus-summary-save-article-file "gnus/save-aif")
2915     (gnus-summary-save-article "gnus/save-art")
2916     (gnus-uu-post-news "gnus/uu-post")
2917     (gnus-summary-catchup "gnus/catchup")
2918     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2919     (gnus-summary-exit "gnus/exit-summ")
2920     ;; Some new command that may need more suitable icons:
2921     (gnus-summary-print-article "gnus/print" nil :visible nil)
2922     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2923     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2924     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2925     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2926     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2927     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2928     ;;
2929     (gnus-info-find-node "gnus/help" nil :visible nil))
2930   "List of functions for the summary tool bar (retro look).
2931
2932 See `gmm-tool-bar-from-list' for the format of the list."
2933   :type '(repeat gmm-tool-bar-item)
2934   :version "23.1" ;; No Gnus
2935   :initialize 'custom-initialize-default
2936   :set 'gnus-summary-tool-bar-update
2937   :group 'gnus-summary)
2938
2939 (defcustom gnus-summary-tool-bar-zap-list t
2940   "List of icon items from the global tool bar.
2941 These items are not displayed in the Gnus summary mode tool bar.
2942
2943 See `gmm-tool-bar-from-list' for the format of the list."
2944   :type 'gmm-tool-bar-zap-list
2945   :version "23.1" ;; No Gnus
2946   :initialize 'custom-initialize-default
2947   :set 'gnus-summary-tool-bar-update
2948   :group 'gnus-summary)
2949
2950 (defvar image-load-path)
2951 (defvar tool-bar-map)
2952
2953 (defun gnus-summary-make-tool-bar (&optional force)
2954   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2955 When FORCE, rebuild the tool bar."
2956   (when (and (not (featurep 'xemacs))
2957              (boundp 'tool-bar-mode)
2958              tool-bar-mode
2959              (or (not gnus-summary-tool-bar-map) force))
2960     (let* ((load-path
2961             (gmm-image-load-path-for-library "gnus"
2962                                              "mail/save.xpm"
2963                                              nil t))
2964            (image-load-path (cons (car load-path)
2965                                   (when (boundp 'image-load-path)
2966                                     image-load-path)))
2967            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2968                                         gnus-summary-tool-bar-zap-list
2969                                         'gnus-summary-mode-map)))
2970       (when map
2971         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2972         ;; uses its value.
2973         (setq gnus-summary-tool-bar-map map))))
2974   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2975
2976 (defun gnus-make-score-map (type)
2977   "Make a summary score map of type TYPE."
2978   (if t
2979       nil
2980     (let ((headers '(("author" "from" string)
2981                      ("subject" "subject" string)
2982                      ("article body" "body" string)
2983                      ("article head" "head" string)
2984                      ("xref" "xref" string)
2985                      ("extra header" "extra" string)
2986                      ("lines" "lines" number)
2987                      ("followups to author" "followup" string)))
2988           (types '((number ("less than" <)
2989                            ("greater than" >)
2990                            ("equal" =))
2991                    (string ("substring" s)
2992                            ("exact string" e)
2993                            ("fuzzy string" f)
2994                            ("regexp" r))))
2995           (perms '(("temporary" (current-time-string))
2996                    ("permanent" nil)
2997                    ("immediate" now)))
2998           header)
2999       (list
3000        (apply
3001         'nconc
3002         (list
3003          (if (eq type 'lower)
3004              "Lower score"
3005            "Increase score"))
3006         (let (outh)
3007           (while headers
3008             (setq header (car headers))
3009             (setq outh
3010                   (cons
3011                    (apply
3012                     'nconc
3013                     (list (car header))
3014                     (let ((ts (cdr (assoc (nth 2 header) types)))
3015                           outt)
3016                       (while ts
3017                         (setq outt
3018                               (cons
3019                                (apply
3020                                 'nconc
3021                                 (list (caar ts))
3022                                 (let ((ps perms)
3023                                       outp)
3024                                   (while ps
3025                                     (setq outp
3026                                           (cons
3027                                            (vector
3028                                             (caar ps)
3029                                             (list
3030                                              'gnus-summary-score-entry
3031                                              (nth 1 header)
3032                                              (if (or (string= (nth 1 header)
3033                                                               "head")
3034                                                      (string= (nth 1 header)
3035                                                               "body"))
3036                                                  ""
3037                                                (list 'gnus-summary-header
3038                                                      (nth 1 header)))
3039                                              (list 'quote (nth 1 (car ts)))
3040                                              (list 'gnus-score-delta-default
3041                                                    nil)
3042                                              (nth 1 (car ps))
3043                                              t)
3044                                             t)
3045                                            outp))
3046                                     (setq ps (cdr ps)))
3047                                   (list (nreverse outp))))
3048                                outt))
3049                         (setq ts (cdr ts)))
3050                       (list (nreverse outt))))
3051                    outh))
3052             (setq headers (cdr headers)))
3053           (list (nreverse outh))))))))
3054
3055
3056 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3057 (defvar bookmark-make-record-function)
3058 \f
3059 (defvar bidi-paragraph-direction)
3060
3061 (defun gnus-summary-mode (&optional group)
3062   "Major mode for reading articles.
3063
3064 All normal editing commands are switched off.
3065 \\<gnus-summary-mode-map>
3066 Each line in this buffer represents one article.  To read an
3067 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3068 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3069 respectively.
3070
3071 You can also post articles and send mail from this buffer.  To
3072 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3073 of an article, type `\\[gnus-summary-reply]'.
3074
3075 There are approx. one gazillion commands you can execute in this
3076 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3077
3078 The following commands are available:
3079
3080 \\{gnus-summary-mode-map}"
3081   (interactive)
3082   (kill-all-local-variables)
3083   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3084     (gnus-summary-make-local-variables))
3085   (gnus-summary-make-local-variables)
3086   (setq gnus-newsgroup-name group)
3087   (when (gnus-visual-p 'summary-menu 'menu)
3088     (gnus-summary-make-menu-bar)
3089     (gnus-summary-make-tool-bar))
3090   (gnus-make-thread-indent-array)
3091   (gnus-simplify-mode-line)
3092   (setq major-mode 'gnus-summary-mode)
3093   (setq mode-name "Summary")
3094   (use-local-map gnus-summary-mode-map)
3095   (buffer-disable-undo)
3096   (setq buffer-read-only t              ;Disable modification
3097         show-trailing-whitespace nil)
3098   (setq truncate-lines t)
3099   ;; Force paragraph direction to be left-to-right.  Don't make it
3100   ;; bound globally in old Emacsen and XEmacsen.
3101   (set (make-local-variable 'bidi-paragraph-direction) 'left-to-right)
3102   (add-to-invisibility-spec '(gnus-sum . t))
3103   (gnus-summary-set-display-table)
3104   (gnus-set-default-directory)
3105   (make-local-variable 'gnus-summary-line-format)
3106   (make-local-variable 'gnus-summary-line-format-spec)
3107   (make-local-variable 'gnus-summary-dummy-line-format)
3108   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3109   (make-local-variable 'gnus-summary-mark-positions)
3110   (gnus-make-local-hook 'pre-command-hook)
3111   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3112   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3113   (turn-on-gnus-mailing-list-mode)
3114   (mm-enable-multibyte)
3115   (set (make-local-variable 'bookmark-make-record-function)
3116        'gnus-summary-bookmark-make-record)
3117   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3118   (gnus-update-summary-mark-positions))
3119
3120 (defun gnus-summary-make-local-variables ()
3121   "Make all the local summary buffer variables."
3122   (let (global)
3123     (dolist (local gnus-summary-local-variables)
3124       (if (consp local)
3125           (progn
3126             (if (eq (cdr local) 'global)
3127                 ;; Copy the global value of the variable.
3128                 (setq global (symbol-value (car local)))
3129               ;; Use the value from the list.
3130               (setq global (eval (cdr local))))
3131             (set (make-local-variable (car local)) global))
3132         ;; Simple nil-valued local variable.
3133         (set (make-local-variable local) nil)))))
3134
3135 ;; Summary data functions.
3136
3137 (defmacro gnus-data-number (data)
3138   `(car ,data))
3139
3140 (defmacro gnus-data-set-number (data number)
3141   `(setcar ,data ,number))
3142
3143 (defmacro gnus-data-mark (data)
3144   `(nth 1 ,data))
3145
3146 (defmacro gnus-data-set-mark (data mark)
3147   `(setcar (nthcdr 1 ,data) ,mark))
3148
3149 (defmacro gnus-data-pos (data)
3150   `(nth 2 ,data))
3151
3152 (defmacro gnus-data-set-pos (data pos)
3153   `(setcar (nthcdr 2 ,data) ,pos))
3154
3155 (defmacro gnus-data-header (data)
3156   `(nth 3 ,data))
3157
3158 (defmacro gnus-data-set-header (data header)
3159   `(setf (nth 3 ,data) ,header))
3160
3161 (defmacro gnus-data-level (data)
3162   `(nth 4 ,data))
3163
3164 (defmacro gnus-data-unread-p (data)
3165   `(= (nth 1 ,data) gnus-unread-mark))
3166
3167 (defmacro gnus-data-read-p (data)
3168   `(/= (nth 1 ,data) gnus-unread-mark))
3169
3170 (defmacro gnus-data-pseudo-p (data)
3171   `(consp (nth 3 ,data)))
3172
3173 (defmacro gnus-data-find (number)
3174   `(assq ,number gnus-newsgroup-data))
3175
3176 (defmacro gnus-data-find-list (number &optional data)
3177   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3178      (memq (assq ,number bdata)
3179            bdata)))
3180
3181 (defmacro gnus-data-make (number mark pos header level)
3182   `(list ,number ,mark ,pos ,header ,level))
3183
3184 (defun gnus-data-enter (after-article number mark pos header level offset)
3185   (let ((data (gnus-data-find-list after-article)))
3186     (unless data
3187       (error "No such article: %d" after-article))
3188     (setcdr data (cons (gnus-data-make number mark pos header level)
3189                        (cdr data)))
3190     (setq gnus-newsgroup-data-reverse nil)
3191     (gnus-data-update-list (cddr data) offset)))
3192
3193 (defun gnus-data-enter-list (after-article list &optional offset)
3194   (when list
3195     (let ((data (and after-article (gnus-data-find-list after-article)))
3196           (ilist list))
3197       (if (not (or data
3198                    after-article))
3199           (let ((odata gnus-newsgroup-data))
3200             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3201             (when offset
3202               (gnus-data-update-list odata offset)))
3203         ;; Find the last element in the list to be spliced into the main
3204         ;; list.
3205         (setq list (last list))
3206         (if (not data)
3207             (progn
3208               (setcdr list gnus-newsgroup-data)
3209               (setq gnus-newsgroup-data ilist)
3210               (when offset
3211                 (gnus-data-update-list (cdr list) offset)))
3212           (setcdr list (cdr data))
3213           (setcdr data ilist)
3214           (when offset
3215             (gnus-data-update-list (cdr list) offset))))
3216       (setq gnus-newsgroup-data-reverse nil))))
3217
3218 (defun gnus-data-remove (article &optional offset)
3219   (let ((data gnus-newsgroup-data))
3220     (if (= (gnus-data-number (car data)) article)
3221         (progn
3222           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3223                 gnus-newsgroup-data-reverse nil)
3224           (when offset
3225             (gnus-data-update-list gnus-newsgroup-data offset)))
3226       (while (cdr data)
3227         (when (= (gnus-data-number (cadr data)) article)
3228           (setcdr data (cddr data))
3229           (when offset
3230             (gnus-data-update-list (cdr data) offset))
3231           (setq data nil
3232                 gnus-newsgroup-data-reverse nil))
3233         (setq data (cdr data))))))
3234
3235 (defmacro gnus-data-list (backward)
3236   `(if ,backward
3237        (or gnus-newsgroup-data-reverse
3238            (setq gnus-newsgroup-data-reverse
3239                  (reverse gnus-newsgroup-data)))
3240      gnus-newsgroup-data))
3241
3242 (defun gnus-data-update-list (data offset)
3243   "Add OFFSET to the POS of all data entries in DATA."
3244   (setq gnus-newsgroup-data-reverse nil)
3245   (while data
3246     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3247     (setq data (cdr data))))
3248
3249 (defun gnus-summary-article-pseudo-p (article)
3250   "Say whether this article is a pseudo article or not."
3251   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3252
3253 (defmacro gnus-summary-article-sparse-p (article)
3254   "Say whether this article is a sparse article or not."
3255   `(memq ,article gnus-newsgroup-sparse))
3256
3257 (defmacro gnus-summary-article-ancient-p (article)
3258   "Say whether this article is a sparse article or not."
3259   `(memq ,article gnus-newsgroup-ancient))
3260
3261 (defun gnus-article-children (number)
3262   "Return a list of all children to NUMBER."
3263   (let* ((data (gnus-data-find-list number))
3264          (level (gnus-data-level (car data)))
3265          children)
3266     (setq data (cdr data))
3267     (while (and data
3268                 (= (gnus-data-level (car data)) (1+ level)))
3269       (push (gnus-data-number (car data)) children)
3270       (setq data (cdr data)))
3271     children))
3272
3273 (defmacro gnus-summary-skip-intangible ()
3274   "If the current article is intangible, then jump to a different article."
3275   '(let ((to (get-text-property (point) 'gnus-intangible)))
3276      (and to (gnus-summary-goto-subject to))))
3277
3278 (defmacro gnus-summary-article-intangible-p ()
3279   "Say whether this article is intangible or not."
3280   '(get-text-property (point) 'gnus-intangible))
3281
3282 ;; Some summary mode macros.
3283
3284 (defmacro gnus-summary-article-number ()
3285   "The article number of the article on the current line.
3286 If there isn't an article number here, then we return the current
3287 article number."
3288   '(progn
3289      (gnus-summary-skip-intangible)
3290      (or (get-text-property (point) 'gnus-number)
3291          (gnus-summary-last-subject))))
3292
3293 (defmacro gnus-summary-article-header (&optional number)
3294   "Return the header of article NUMBER."
3295   `(gnus-data-header (gnus-data-find
3296                       ,(or number '(gnus-summary-article-number)))))
3297
3298 (defmacro gnus-summary-thread-level (&optional number)
3299   "Return the level of thread that starts with article NUMBER."
3300   `(if (and (eq gnus-summary-make-false-root 'dummy)
3301             (get-text-property (point) 'gnus-intangible))
3302        0
3303      (gnus-data-level (gnus-data-find
3304                        ,(or number '(gnus-summary-article-number))))))
3305
3306 (defmacro gnus-summary-article-mark (&optional number)
3307   "Return the mark of article NUMBER."
3308   `(gnus-data-mark (gnus-data-find
3309                     ,(or number '(gnus-summary-article-number)))))
3310
3311 (defmacro gnus-summary-article-pos (&optional number)
3312   "Return the position of the line of article NUMBER."
3313   `(gnus-data-pos (gnus-data-find
3314                    ,(or number '(gnus-summary-article-number)))))
3315
3316 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3317 (defmacro gnus-summary-article-subject (&optional number)
3318   "Return current subject string or nil if nothing."
3319   `(let ((headers
3320           ,(if number
3321                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3322              '(gnus-data-header (assq (gnus-summary-article-number)
3323                                       gnus-newsgroup-data)))))
3324      (and headers
3325           (vectorp headers)
3326           (mail-header-subject headers))))
3327
3328 (defmacro gnus-summary-article-score (&optional number)
3329   "Return current article score."
3330   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3331                   gnus-newsgroup-scored))
3332        gnus-summary-default-score 0))
3333
3334 (defun gnus-summary-article-children (&optional number)
3335   "Return a list of article numbers that are children of article NUMBER."
3336   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3337          (level (gnus-data-level (car data)))
3338          l children)
3339     (while (and (setq data (cdr data))
3340                 (> (setq l (gnus-data-level (car data))) level))
3341       (and (= (1+ level) l)
3342            (push (gnus-data-number (car data))
3343                  children)))
3344     (nreverse children)))
3345
3346 (defun gnus-summary-article-parent (&optional number)
3347   "Return the article number of the parent of article NUMBER."
3348   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3349                                     (gnus-data-list t)))
3350          (level (gnus-data-level (car data))))
3351     (if (zerop level)
3352         ()                              ; This is a root.
3353       ;; We search until we find an article with a level less than
3354       ;; this one.  That function has to be the parent.
3355       (while (and (setq data (cdr data))
3356                   (not (< (gnus-data-level (car data)) level))))
3357       (and data (gnus-data-number (car data))))))
3358
3359 (defun gnus-unread-mark-p (mark)
3360   "Say whether MARK is the unread mark."
3361   (= mark gnus-unread-mark))
3362
3363 (defun gnus-read-mark-p (mark)
3364   "Say whether MARK is one of the marks that mark as read.
3365 This is all marks except unread, ticked, dormant, and expirable."
3366   (not (or (= mark gnus-unread-mark)
3367            (= mark gnus-ticked-mark)
3368            (= mark gnus-spam-mark)
3369            (= mark gnus-dormant-mark)
3370            (= mark gnus-expirable-mark))))
3371
3372 (defmacro gnus-article-mark (number)
3373   "Return the MARK of article NUMBER.
3374 This macro should only be used when computing the mark the \"first\"
3375 time; i.e., when generating the summary lines.  After that,
3376 `gnus-summary-article-mark' should be used to examine the
3377 marks of articles."
3378   `(cond
3379     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3380     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3381     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3382     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3383     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3384     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3385     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3386     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3387            gnus-ancient-mark))))
3388
3389 ;; Saving hidden threads.
3390
3391 (defmacro gnus-save-hidden-threads (&rest forms)
3392   "Save hidden threads, eval FORMS, and restore the hidden threads."
3393   (let ((config (make-symbol "config")))
3394     `(let ((,config (gnus-hidden-threads-configuration)))
3395        (unwind-protect
3396            (save-excursion
3397              ,@forms)
3398          (gnus-restore-hidden-threads-configuration ,config)))))
3399 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3400 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3401
3402 (defun gnus-data-compute-positions ()
3403   "Compute the positions of all articles."
3404   (setq gnus-newsgroup-data-reverse nil)
3405   (let ((data gnus-newsgroup-data))
3406     (save-excursion
3407       (gnus-save-hidden-threads
3408         (gnus-summary-show-all-threads)
3409         (goto-char (point-min))
3410         (while data
3411           (while (get-text-property (point) 'gnus-intangible)
3412             (forward-line 1))
3413           (gnus-data-set-pos (car data) (+ (point) 3))
3414           (setq data (cdr data))
3415           (forward-line 1))))))
3416
3417 (defun gnus-hidden-threads-configuration ()
3418   "Return the current hidden threads configuration."
3419   (save-excursion
3420     (let (config)
3421       (goto-char (point-min))
3422       (while (not (eobp))
3423         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3424           (push (save-excursion (forward-line 0) (point)) config))
3425         (forward-line 1))
3426       config)))
3427
3428 (defun gnus-restore-hidden-threads-configuration (config)
3429   "Restore hidden threads configuration from CONFIG."
3430   (save-excursion
3431     (let (point (inhibit-read-only t))
3432       (while (setq point (pop config))
3433         (goto-char point)
3434         (gnus-summary-hide-thread)))))
3435
3436 ;; Various summary mode internalish functions.
3437
3438 (defun gnus-mouse-pick-article (e)
3439   (interactive "e")
3440   (mouse-set-point e)
3441   (gnus-summary-next-page nil t))
3442
3443 (defun gnus-summary-set-display-table ()
3444   "Change the display table.
3445 Odd characters have a tendency to mess
3446 up nicely formatted displays - we make all possible glyphs
3447 display only a single character."
3448
3449   ;; We start from the standard display table, if any.
3450   (let ((table (or (copy-sequence standard-display-table)
3451                    (make-display-table)))
3452         (i 32))
3453     ;; Nix out all the control chars...
3454     (while (>= (setq i (1- i)) 0)
3455       (gnus-put-display-table i [??] table))
3456    ;; ... but not newline and cr, of course.  (cr is necessary for the
3457     ;; selective display).
3458     (gnus-put-display-table ?\n nil table)
3459     (gnus-put-display-table ?\r nil table)
3460     ;; We keep TAB as well.
3461     (gnus-put-display-table ?\t nil table)
3462     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3463     ;; Emacs 23 (unicode), that are not set already.
3464     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3465                  160
3466                256)))
3467       (while (>= (setq i (1- i)) 127)
3468         ;; Only modify if the entry is nil.
3469         (unless (gnus-get-display-table i table)
3470           (gnus-put-display-table i [??] table))))
3471     (setq buffer-display-table table)))
3472
3473 (defun gnus-summary-set-article-display-arrow (pos)
3474   "Update the overlay arrow to point to line at position POS."
3475   (when gnus-summary-display-arrow
3476     (make-local-variable 'overlay-arrow-position)
3477     (make-local-variable 'overlay-arrow-string)
3478     (save-excursion
3479       (goto-char pos)
3480       (beginning-of-line)
3481       (unless overlay-arrow-position
3482         (setq overlay-arrow-position (make-marker)))
3483       (setq overlay-arrow-string "=>"
3484             overlay-arrow-position (set-marker overlay-arrow-position
3485                                                (point)
3486                                                (current-buffer))))))
3487
3488 (defun gnus-summary-setup-buffer (group)
3489   "Initialize summary buffer.
3490 If the setup was successful, non-nil is returned."
3491   (let ((buffer (gnus-summary-buffer-name group))
3492         (dead-name (concat "*Dead Summary "
3493                            (gnus-group-decoded-name group) "*")))
3494     ;; If a dead summary buffer exists, we kill it.
3495     (when (gnus-buffer-live-p dead-name)
3496       (gnus-kill-buffer dead-name))
3497     (if (get-buffer buffer)
3498         (progn
3499           (set-buffer buffer)
3500           (setq gnus-summary-buffer (current-buffer))
3501           (not gnus-newsgroup-prepared))
3502       (set-buffer (gnus-get-buffer-create buffer))
3503       (setq gnus-summary-buffer (current-buffer))
3504       (gnus-summary-mode group)
3505       (when (gnus-group-quit-config group)
3506         (set (make-local-variable 'gnus-single-article-buffer) nil))
3507       (make-local-variable 'gnus-article-buffer)
3508       (make-local-variable 'gnus-article-current)
3509       (make-local-variable 'gnus-original-article-buffer)
3510       (setq gnus-newsgroup-name group)
3511       ;; Set any local variables in the group parameters.
3512       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3513       t)))
3514
3515 (defun gnus-set-global-variables ()
3516   "Set the global equivalents of the buffer-local variables.
3517 They are set to the latest values they had.  These reflect the summary
3518 buffer that was in action when the last article was fetched."
3519   (when (eq major-mode 'gnus-summary-mode)
3520     (setq gnus-summary-buffer (current-buffer))
3521     (let ((name gnus-newsgroup-name)
3522           (marked gnus-newsgroup-marked)
3523           (spam gnus-newsgroup-spam-marked)
3524           (unread gnus-newsgroup-unreads)
3525           (headers gnus-current-headers)
3526           (data gnus-newsgroup-data)
3527           (summary gnus-summary-buffer)
3528           (article-buffer gnus-article-buffer)
3529           (original gnus-original-article-buffer)
3530           (gac gnus-article-current)
3531           (reffed gnus-reffed-article-number)
3532           (score-file gnus-current-score-file)
3533           (default-charset gnus-newsgroup-charset)
3534           vlist)
3535       (let ((locals gnus-newsgroup-variables))
3536         (while locals
3537           (if (consp (car locals))
3538               (push (eval (caar locals)) vlist)
3539             (push (eval (car locals)) vlist))
3540           (setq locals (cdr locals)))
3541         (setq vlist (nreverse vlist)))
3542       (with-temp-buffer
3543         (setq gnus-newsgroup-name name
3544               gnus-newsgroup-marked marked
3545               gnus-newsgroup-spam-marked spam
3546               gnus-newsgroup-unreads unread
3547               gnus-current-headers headers
3548               gnus-newsgroup-data data
3549               gnus-article-current gac
3550               gnus-summary-buffer summary
3551               gnus-article-buffer article-buffer
3552               gnus-original-article-buffer original
3553               gnus-reffed-article-number reffed
3554               gnus-current-score-file score-file
3555               gnus-newsgroup-charset default-charset)
3556         (let ((locals gnus-newsgroup-variables))
3557           (while locals
3558             (if (consp (car locals))
3559                 (set (caar locals) (pop vlist))
3560               (set (car locals) (pop vlist)))
3561             (setq locals (cdr locals))))))))
3562
3563 (defun gnus-summary-article-unread-p (article)
3564   "Say whether ARTICLE is unread or not."
3565   (memq article gnus-newsgroup-unreads))
3566
3567 (defun gnus-summary-first-article-p (&optional article)
3568   "Return whether ARTICLE is the first article in the buffer."
3569   (if (not (setq article (or article (gnus-summary-article-number))))
3570       nil
3571     (eq article (caar gnus-newsgroup-data))))
3572
3573 (defun gnus-summary-last-article-p (&optional article)
3574   "Return whether ARTICLE is the last article in the buffer."
3575   (if (not (setq article (or article (gnus-summary-article-number))))
3576       ;; All non-existent numbers are the last article.  :-)
3577       t
3578     (not (cdr (gnus-data-find-list article)))))
3579
3580 (defun gnus-make-thread-indent-array (&optional n)
3581   (when (or n
3582             (progn (setq n 200) nil)
3583             (null gnus-thread-indent-array)
3584             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3585     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3586           gnus-thread-indent-array-level gnus-thread-indent-level)
3587     (while (>= n 0)
3588       (aset gnus-thread-indent-array n
3589             (make-string (* n gnus-thread-indent-level) ? ))
3590       (setq n (1- n)))))
3591
3592 (defun gnus-update-summary-mark-positions ()
3593   "Compute where the summary marks are to go."
3594   (save-excursion
3595     (when (gnus-buffer-exists-p gnus-summary-buffer)
3596       (set-buffer gnus-summary-buffer))
3597     (let ((spec gnus-summary-line-format-spec)
3598           pos)
3599       (save-excursion
3600         (gnus-set-work-buffer)
3601         (let ((gnus-tmp-unread ?Z)
3602               (gnus-replied-mark ?Z)
3603               (gnus-score-below-mark ?Z)
3604               (gnus-score-over-mark ?Z)
3605               (gnus-undownloaded-mark ?Z)
3606               (gnus-summary-line-format-spec spec)
3607               (gnus-newsgroup-downloadable '(0))
3608               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3609               case-fold-search ignores)
3610           ;; Here, all marks are bound to Z.
3611           (gnus-summary-insert-line header
3612                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3613           (goto-char (point-min))
3614           ;; Memorize the positions of the same characters as dummy marks.
3615           (while (re-search-forward "[A-D]" nil t)
3616             (push (point) ignores))
3617           (erase-buffer)
3618           ;; We use A-D as dummy marks in order to know column positions
3619           ;; where marks should be inserted.
3620           (setq gnus-tmp-unread ?A
3621                 gnus-replied-mark ?B
3622                 gnus-score-below-mark ?C
3623                 gnus-score-over-mark ?C
3624                 gnus-undownloaded-mark ?D)
3625           (gnus-summary-insert-line header
3626                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3627           ;; Ignore characters which aren't dummy marks.
3628           (dolist (p ignores)
3629             (delete-region (goto-char (1- p)) p)
3630             (insert ?Z))
3631           (goto-char (point-min))
3632           (setq pos (list (cons 'unread
3633                                 (and (search-forward "A" nil t)
3634                                      (- (point) (point-min) 1)))))
3635           (goto-char (point-min))
3636           (push (cons 'replied (and (search-forward "B" nil t)
3637                                     (- (point) (point-min) 1)))
3638                 pos)
3639           (goto-char (point-min))
3640           (push (cons 'score (and (search-forward "C" nil t)
3641                                   (- (point) (point-min) 1)))
3642                 pos)
3643           (goto-char (point-min))
3644           (push (cons 'download (and (search-forward "D" nil t)
3645                                      (- (point) (point-min) 1)))
3646                 pos)))
3647       (setq gnus-summary-mark-positions pos))))
3648
3649 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3650   "Insert a dummy root in the summary buffer."
3651   (beginning-of-line)
3652   (gnus-add-text-properties
3653    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3654    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3655
3656 (defun gnus-summary-extract-address-component (from)
3657   (or (car (funcall gnus-extract-address-components from))
3658       from))
3659
3660 (defun gnus-summary-from-or-to-or-newsgroups (header from)
3661   (let ((mail-parse-charset gnus-newsgroup-charset)
3662         (ignored-from-addresses (gnus-ignored-from-addresses))
3663         ;; Is it really necessary to do this next part for each summary line?
3664         ;; Luckily, doesn't seem to slow things down much.
3665         (mail-parse-ignored-charsets
3666          (with-current-buffer gnus-summary-buffer
3667            gnus-newsgroup-ignored-charsets))
3668         (address (cadr (gnus-extract-address-components from))))
3669     (or
3670      (and ignored-from-addresses
3671           (string-match ignored-from-addresses address)
3672           (let ((extra-headers (mail-header-extra header))
3673                 to
3674                 newsgroups)
3675             (cond
3676              ((setq to (cdr (assq 'To extra-headers)))
3677               (concat gnus-summary-to-prefix
3678                       (inline
3679                         (gnus-summary-extract-address-component
3680                          (funcall gnus-decode-encoded-address-function to)))))
3681              ((setq newsgroups
3682                     (or
3683                      (cdr (assq 'Newsgroups extra-headers))
3684                      (and
3685                       (memq 'Newsgroups gnus-extra-headers)
3686                       (eq (car (gnus-find-method-for-group
3687                                 gnus-newsgroup-name)) 'nntp)
3688                       (gnus-group-real-name gnus-newsgroup-name))))
3689               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3690      (gnus-string-mark-left-to-right (gnus-summary-extract-address-component from)))))
3691
3692 (defun gnus-summary-insert-line (gnus-tmp-header
3693                                  gnus-tmp-level gnus-tmp-current
3694                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3695                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3696                                  &optional gnus-tmp-dummy gnus-tmp-score
3697                                  gnus-tmp-process)
3698   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3699       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3700                                           gnus-tmp-level)))
3701   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3702          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3703          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3704          (gnus-tmp-score-char
3705           (if (or (null gnus-summary-default-score)
3706                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3707                       gnus-summary-zcore-fuzz))
3708               ?                         ;Whitespace
3709             (if (< gnus-tmp-score gnus-summary-default-score)
3710                 gnus-score-below-mark gnus-score-over-mark)))
3711          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3712          (gnus-tmp-replied
3713           (cond (gnus-tmp-process gnus-process-mark)
3714                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3715                  gnus-cached-mark)
3716                 (gnus-tmp-replied gnus-replied-mark)
3717                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3718                  gnus-forwarded-mark)
3719                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3720                  gnus-saved-mark)
3721                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3722                  gnus-unseen-mark)
3723                 (t gnus-no-mark)))
3724          (gnus-tmp-downloaded
3725           (cond (undownloaded
3726                  gnus-undownloaded-mark)
3727                 (gnus-newsgroup-agentized
3728                  gnus-downloaded-mark)
3729                 (t
3730                  gnus-no-mark)))
3731          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3732          (gnus-tmp-name
3733           (cond
3734            ((string-match "<[^>]+> *$" gnus-tmp-from)
3735             (let ((beg (match-beginning 0)))
3736               (or (and (string-match "^\".+\"" gnus-tmp-from)
3737                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3738                   (substring gnus-tmp-from 0 beg))))
3739            ((string-match "(.+)" gnus-tmp-from)
3740             (substring gnus-tmp-from
3741                        (1+ (match-beginning 0)) (1- (match-end 0))))
3742            (t gnus-tmp-from)))
3743          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3744          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3745          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3746          (inhibit-read-only t))
3747     (when (string= gnus-tmp-name "")
3748       (setq gnus-tmp-name gnus-tmp-from))
3749     (unless (numberp gnus-tmp-lines)
3750       (setq gnus-tmp-lines -1))
3751     (if (= gnus-tmp-lines -1)
3752         (setq gnus-tmp-lines "?")
3753       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3754     (condition-case ()
3755         (gnus-put-text-property
3756          (point)
3757          (progn (eval gnus-summary-line-format-spec) (point))
3758          'gnus-number gnus-tmp-number)
3759       (error (gnus-message 5 "Error updating the summary line")))
3760     (when (gnus-visual-p 'summary-highlight 'highlight)
3761       (forward-line -1)
3762       (gnus-summary-highlight-line)
3763       (gnus-run-hooks 'gnus-summary-update-hook)
3764       (forward-line 1))))
3765
3766 (defun gnus-summary-update-line (&optional dont-update)
3767   "Update summary line after change."
3768   (when (and gnus-summary-default-score
3769              (not gnus-summary-inhibit-highlight))
3770     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3771            (article (gnus-summary-article-number))
3772            (score (gnus-summary-article-score article)))
3773       (unless dont-update
3774         (if (and gnus-summary-mark-below
3775                  (< (gnus-summary-article-score)
3776                     gnus-summary-mark-below))
3777             ;; This article has a low score, so we mark it as read.
3778             (when (memq article gnus-newsgroup-unreads)
3779               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3780           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3781             ;; This article was previously marked as read on account
3782             ;; of a low score, but now it has risen, so we mark it as
3783             ;; unread.
3784             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3785         (gnus-summary-update-mark
3786          (if (or (null gnus-summary-default-score)
3787                  (<= (abs (- score gnus-summary-default-score))
3788                      gnus-summary-zcore-fuzz))
3789              ?                          ;Whitespace
3790            (if (< score gnus-summary-default-score)
3791                gnus-score-below-mark gnus-score-over-mark))
3792          'score))
3793       ;; Do visual highlighting.
3794       (when (gnus-visual-p 'summary-highlight 'highlight)
3795         (gnus-summary-highlight-line)
3796         (gnus-run-hooks 'gnus-summary-update-hook)))))
3797
3798 (defvar gnus-tmp-new-adopts nil)
3799
3800 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3801   "Return the number of articles in THREAD.
3802 This may be 0 in some cases -- if none of the articles in
3803 the thread are to be displayed."
3804   (let* ((number
3805          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3806           (cond
3807            ((not (listp thread))
3808             1)
3809            ((and (consp thread) (cdr thread))
3810             (apply
3811              '+ 1 (mapcar
3812                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3813            ((null thread)
3814             1)
3815            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3816             1)
3817            (t 0))))
3818     (when (and level (zerop level) gnus-tmp-new-adopts)
3819       (incf number
3820             (apply '+ (mapcar
3821                        'gnus-summary-number-of-articles-in-thread
3822                        gnus-tmp-new-adopts))))
3823     (if char
3824         (if (> number 1) gnus-not-empty-thread-mark
3825           gnus-empty-thread-mark)
3826       number)))
3827
3828 (defsubst gnus-summary-line-message-size (head)
3829   "Return pretty-printed version of message size.
3830 This function is intended to be used in
3831 `gnus-summary-line-format-alist'."
3832   (let ((c (or (mail-header-chars head) -1)))
3833     (cond ((< c 0) "n/a")               ; chars not available
3834           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3835           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3836           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3837           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3838
3839 (defcustom gnus-user-date-format-alist
3840   '(((gnus-seconds-today) . "Today, %H:%M")
3841     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3842     (604800 . "%A %H:%M")               ; That's one week
3843     ((gnus-seconds-month) . "%A %d")
3844     ((gnus-seconds-year) . "%B %d")
3845     (t . "%b %d %Y"))                   ; This one is used when no other
3846                                         ; does match
3847   "Specifies date format depending on age of article.
3848 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3849 seconds) or a Lisp expression evaluating to a number.  When the age of
3850 the article is less than this number, then use `format-time-string'
3851 with the corresponding FORMAT for displaying the date of the article.
3852 If AGE is not a number or a Lisp expression evaluating to a
3853 non-number, then the corresponding FORMAT is used as a default value.
3854
3855 Note that the list is processed from the beginning, so it should be
3856 sorted by ascending AGE.  Also note that items following the first
3857 non-number AGE will be ignored.
3858
3859 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3860 and `gnus-seconds-year' in the AGE spec.  They return the number of
3861 seconds passed since the start of today, of this month, of this year,
3862 respectively."
3863   :version "24.1"
3864   :group 'gnus-summary-format
3865   :type '(alist :key-type sexp :value-type string))
3866
3867 (defun gnus-user-date (messy-date)
3868   "Format the messy-date according to `gnus-user-date-format-alist'.
3869 Returns \"  ?  \" if there's bad input or if another error occurs.
3870 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3871   (condition-case ()
3872       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3873              (now (gnus-float-time))
3874              ;;If we don't find something suitable we'll use this one
3875              (my-format "%b %d '%y"))
3876         (let* ((difference (- now messy-date))
3877                (templist gnus-user-date-format-alist)
3878                (top (eval (caar templist))))
3879           (while (if (numberp top) (< top difference) (not top))
3880             (progn
3881               (setq templist (cdr templist))
3882               (setq top (eval (caar templist)))))
3883           (if (stringp (cdr (car templist)))
3884               (setq my-format (cdr (car templist)))))
3885         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3886     (error "  ?   ")))
3887
3888 (defun gnus-summary-set-local-parameters (group)
3889   "Go through the local params of GROUP and set all variable specs in that list."
3890   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3891                                         ; really variables.
3892     (dolist (elem (gnus-group-find-parameter group))
3893       (and (consp elem)                 ; Has to be a cons.
3894            (consp (cdr elem))           ; The cdr has to be a list.
3895            (symbolp (car elem))         ; Has to be a symbol in there.
3896            (not (memq (car elem) vars))
3897            (ignore-errors
3898              (push (car elem) vars)
3899              ;; Variables like `gnus-show-threads' that are globally
3900              ;; bound, if used as group parameters, need to get to be
3901              ;; buffer-local, whereas just parameters like `gcc-self',
3902              ;; `timestamp', etc. should not be bound as variables.
3903              (if (boundp (car elem))
3904                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3905                (eval (nth 1 elem))))))))
3906
3907 (defun gnus-summary-read-group (group &optional show-all no-article
3908                                       kill-buffer no-display backward
3909                                       select-articles)
3910   "Start reading news in newsgroup GROUP.
3911 If SHOW-ALL is non-nil, already read articles are also listed.
3912 If NO-ARTICLE is non-nil, no article is selected initially.
3913 If NO-DISPLAY, don't generate the summary buffer contents.
3914 If KILL-BUFFER, it should be a buffer that's killed once the new
3915 summary buffer has been generated.
3916 If BACKWARD, move point to the previous group in the group buffer
3917 If SELECT-ARTICLES, only select those articles from GROUP."
3918   (let (result)
3919     (while (and group
3920                 (null (setq result
3921                             (let ((gnus-auto-select-next nil))
3922                               (or (gnus-summary-read-group-1
3923                                    group show-all no-article
3924                                    kill-buffer no-display
3925                                    select-articles)
3926                                   (setq show-all nil
3927                                         select-articles nil)))))
3928                 (eq gnus-auto-select-next 'quietly))
3929       (set-buffer gnus-group-buffer)
3930       ;; The entry function called above goes to the next
3931       ;; group automatically, so we go two groups back
3932       ;; if we are searching for the previous group.
3933       (when backward
3934         (gnus-group-prev-unread-group 2))
3935       (if (not (equal group (gnus-group-group-name)))
3936           (setq group (gnus-group-group-name))
3937         (setq group nil)))
3938     result))
3939
3940 (defun gnus-summary-read-group-1 (group show-all no-article
3941                                         kill-buffer no-display
3942                                         &optional select-articles)
3943   ;; Killed foreign groups can't be entered.
3944   ;;  (when (and (not (gnus-group-native-p group))
3945   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3946   ;;    (error "Dead non-native groups can't be entered"))
3947   (gnus-message 7 "Retrieving newsgroup: %s..."
3948                 (gnus-group-decoded-name group))
3949   (let* ((new-group (gnus-summary-setup-buffer group))
3950          (quit-config (gnus-group-quit-config group))
3951          (did-select (and new-group (gnus-select-newsgroup
3952                                      group show-all select-articles))))
3953     (cond
3954      ;; This summary buffer exists already, so we just select it.
3955      ((not new-group)
3956       (gnus-set-global-variables)
3957       (when kill-buffer
3958         (gnus-kill-or-deaden-summary kill-buffer))
3959       (gnus-configure-windows 'summary 'force)
3960       (gnus-set-mode-line 'summary)
3961       (gnus-summary-position-point)
3962       (message "")
3963       t)
3964      ;; We couldn't select this group.
3965      ((null did-select)
3966       (when (and (eq major-mode 'gnus-summary-mode)
3967                  (not (equal (current-buffer) kill-buffer)))
3968         (kill-buffer (current-buffer))
3969         (if (not quit-config)
3970             (progn
3971               ;; Update the info -- marks might need to be removed,
3972               ;; for instance.
3973               (gnus-summary-update-info)
3974               (set-buffer gnus-group-buffer)
3975               (gnus-group-jump-to-group group)
3976               (gnus-group-next-unread-group 1))
3977           (gnus-handle-ephemeral-exit quit-config)))
3978       (if (null (gnus-list-of-unread-articles group))
3979           (gnus-message 3 "Group %s contains no messages" group)
3980         (gnus-message 3 "Can't select group"))
3981       nil)
3982      ;; The user did a `C-g' while prompting for number of articles,
3983      ;; so we exit this group.
3984      ((eq did-select 'quit)
3985       (and (eq major-mode 'gnus-summary-mode)
3986            (not (equal (current-buffer) kill-buffer))
3987            (kill-buffer (current-buffer)))
3988       (when kill-buffer
3989         (gnus-kill-or-deaden-summary kill-buffer))
3990       (if (not quit-config)
3991           (progn
3992             (set-buffer gnus-group-buffer)
3993             (gnus-group-jump-to-group group)
3994             (gnus-configure-windows 'group 'force))
3995         (gnus-handle-ephemeral-exit quit-config))
3996       ;; Finally signal the quit.
3997       (signal 'quit nil))
3998      ;; The group was successfully selected.
3999      (t
4000       (gnus-set-global-variables)
4001       ;; Save the active value in effect when the group was entered.
4002       (setq gnus-newsgroup-active
4003             (gnus-copy-sequence
4004              (gnus-active gnus-newsgroup-name)))
4005       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4006       ;; You can change the summary buffer in some way with this hook.
4007       (gnus-run-hooks 'gnus-select-group-hook)
4008       (when (memq 'summary (gnus-update-format-specifications
4009                             nil 'summary 'summary-mode 'summary-dummy))
4010         ;; The format specification for the summary line was updated,
4011         ;; so we need to update the mark positions as well.
4012         (gnus-update-summary-mark-positions))
4013       ;; Do score processing.
4014       (when gnus-use-scoring
4015         (gnus-possibly-score-headers))
4016       ;; Check whether to fill in the gaps in the threads.
4017       (when gnus-build-sparse-threads
4018         (gnus-build-sparse-threads))
4019       ;; Find the initial limit.
4020       (if show-all
4021           (let ((gnus-newsgroup-dormant nil))
4022             (gnus-summary-initial-limit show-all))
4023         (gnus-summary-initial-limit show-all))
4024       ;; Generate the summary buffer.
4025       (unless no-display
4026         (gnus-summary-prepare))
4027       (when gnus-use-trees
4028         (gnus-tree-open group)
4029         (setq gnus-summary-highlight-line-function
4030               'gnus-tree-highlight-article))
4031       ;; If the summary buffer is empty, but there are some low-scored
4032       ;; articles or some excluded dormants, we include these in the
4033       ;; buffer.
4034       (when (and (zerop (buffer-size))
4035                  (not no-display))
4036         (cond (gnus-newsgroup-dormant
4037                (gnus-summary-limit-include-dormant))
4038               ((and gnus-newsgroup-scored show-all)
4039                (gnus-summary-limit-include-expunged t))))
4040       ;; Function `gnus-apply-kill-file' must be called in this hook.
4041       (gnus-run-hooks 'gnus-apply-kill-hook)
4042       (if (and (zerop (buffer-size))
4043                (not no-display))
4044           (progn
4045             ;; This newsgroup is empty.
4046             (gnus-summary-catchup-and-exit nil t)
4047             (gnus-message 6 "No unread news")
4048             (when kill-buffer
4049               (gnus-kill-or-deaden-summary kill-buffer))
4050             ;; Return nil from this function.
4051             nil)
4052         ;; Hide conversation thread subtrees.  We cannot do this in
4053         ;; gnus-summary-prepare-hook since kill processing may not
4054         ;; work with hidden articles.
4055         (gnus-summary-maybe-hide-threads)
4056         (gnus-configure-windows 'summary)
4057         (when kill-buffer
4058           (gnus-kill-or-deaden-summary kill-buffer))
4059         (gnus-summary-auto-select-subject)
4060         ;; Show first unread article if requested.
4061         (if (and (not no-article)
4062                  (not no-display)
4063                  gnus-newsgroup-unreads
4064                  gnus-auto-select-first)
4065             (progn
4066               (let ((art (gnus-summary-article-number)))
4067                 (when (and art
4068                            gnus-plugged
4069                            (not (memq art gnus-newsgroup-undownloaded))
4070                            (not (memq art gnus-newsgroup-downloadable)))
4071                   (gnus-summary-goto-article art))))
4072           ;; Don't select any articles.
4073           (gnus-summary-position-point)
4074           (gnus-configure-windows 'summary 'force)
4075           (gnus-set-mode-line 'summary))
4076         (when (and gnus-auto-center-group
4077                    (get-buffer-window gnus-group-buffer t))
4078           ;; Gotta use windows, because recenter does weird stuff if
4079           ;; the current buffer ain't the displayed window.
4080           (let ((owin (selected-window)))
4081             (select-window (get-buffer-window gnus-group-buffer t))
4082             (when (gnus-group-goto-group group)
4083               (recenter))
4084             (select-window owin)))
4085         ;; Mark this buffer as "prepared".
4086         (setq gnus-newsgroup-prepared t)
4087         (gnus-run-hooks 'gnus-summary-prepared-hook)
4088         (unless (gnus-ephemeral-group-p group)
4089           (gnus-group-update-group group nil t))
4090         t)))))
4091
4092 (defun gnus-summary-auto-select-subject ()
4093   "Select the subject line on initial group entry."
4094   (goto-char (point-min))
4095   (cond
4096    ((eq gnus-auto-select-subject 'best)
4097     (gnus-summary-best-unread-subject))
4098    ((eq gnus-auto-select-subject 'unread)
4099     (gnus-summary-first-unread-subject))
4100    ((eq gnus-auto-select-subject 'unseen)
4101     (gnus-summary-first-unseen-subject))
4102    ((eq gnus-auto-select-subject 'unseen-or-unread)
4103     (gnus-summary-first-unseen-or-unread-subject))
4104    ((eq gnus-auto-select-subject 'first)
4105     ;; Do nothing.
4106     )
4107    ((functionp gnus-auto-select-subject)
4108     (funcall gnus-auto-select-subject))))
4109
4110 (defun gnus-summary-prepare ()
4111   "Generate the summary buffer."
4112   (interactive)
4113   (let ((inhibit-read-only t))
4114     (erase-buffer)
4115     (setq gnus-newsgroup-data nil
4116           gnus-newsgroup-data-reverse nil)
4117     (gnus-run-hooks 'gnus-summary-generate-hook)
4118     ;; Generate the buffer, either with threads or without.
4119     (when gnus-newsgroup-headers
4120       (gnus-summary-prepare-threads
4121        (if gnus-show-threads
4122            (gnus-sort-gathered-threads
4123             (funcall gnus-summary-thread-gathering-function
4124                      (gnus-sort-threads
4125                       (gnus-cut-threads (gnus-make-threads)))))
4126          ;; Unthreaded display.
4127          (gnus-sort-articles gnus-newsgroup-headers))))
4128     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4129     ;; Call hooks for modifying summary buffer.
4130     (goto-char (point-min))
4131     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4132
4133 (defsubst gnus-general-simplify-subject (subject)
4134   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4135   (setq subject
4136         (cond
4137          ;; Truncate the subject.
4138          (gnus-simplify-subject-functions
4139           (gnus-map-function gnus-simplify-subject-functions subject))
4140          ((numberp gnus-summary-gather-subject-limit)
4141           (setq subject (gnus-simplify-subject-re subject))
4142           (if (> (length subject) gnus-summary-gather-subject-limit)
4143               (substring subject 0 gnus-summary-gather-subject-limit)
4144             subject))
4145          ;; Fuzzily simplify it.
4146          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4147           (gnus-simplify-subject-fuzzy subject))
4148          ;; Just remove the leading "Re:".
4149          (t
4150           (gnus-simplify-subject-re subject))))
4151
4152   (if (and gnus-summary-gather-exclude-subject
4153            (string-match gnus-summary-gather-exclude-subject subject))
4154       nil                         ; This article shouldn't be gathered
4155     subject))
4156
4157 (defun gnus-summary-simplify-subject-query ()
4158   "Query where the respool algorithm would put this article."
4159   (interactive)
4160   (gnus-summary-select-article)
4161   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4162
4163 (defun gnus-gather-threads-by-subject (threads)
4164   "Gather threads by looking at Subject headers."
4165   (if (not gnus-summary-make-false-root)
4166       threads
4167     (let ((hashtb (gnus-make-hashtable 1024))
4168           (prev threads)
4169           (result threads)
4170           subject hthread whole-subject)
4171       (while threads
4172         (setq subject (gnus-general-simplify-subject
4173                        (setq whole-subject (mail-header-subject
4174                                             (caar threads)))))
4175         (when subject
4176           (if (setq hthread (gnus-gethash subject hashtb))
4177               (progn
4178                 ;; We enter a dummy root into the thread, if we
4179                 ;; haven't done that already.
4180                 (unless (stringp (caar hthread))
4181                   (setcar hthread (list whole-subject (car hthread))))
4182                 ;; We add this new gathered thread to this gathered
4183                 ;; thread.
4184                 (setcdr (car hthread)
4185                         (nconc (cdar hthread) (list (car threads))))
4186                 ;; Remove it from the list of threads.
4187                 (setcdr prev (cdr threads))
4188                 (setq threads prev))
4189             ;; Enter this thread into the hash table.
4190             (gnus-sethash subject
4191                           (if gnus-summary-make-false-root-always
4192                               (progn
4193                                 ;; If you want a dummy root above all
4194                                 ;; threads...
4195                                 (setcar threads (list whole-subject
4196                                                       (car threads)))
4197                                 threads)
4198                             threads)
4199                           hashtb)))
4200         (setq prev threads)
4201         (setq threads (cdr threads)))
4202       result)))
4203
4204 (defun gnus-gather-threads-by-references (threads)
4205   "Gather threads by looking at References headers."
4206   (let ((idhashtb (gnus-make-hashtable 1024))
4207         (thhashtb (gnus-make-hashtable 1024))
4208         (prev threads)
4209         (result threads)
4210         ids references id gthread gid entered ref)
4211     (while threads
4212       (when (setq references (mail-header-references (caar threads)))
4213         (setq id (mail-header-id (caar threads))
4214               ids (inline (gnus-split-references references))
4215               entered nil)
4216         (while (setq ref (pop ids))
4217           (setq ids (delete ref ids))
4218           (if (not (setq gid (gnus-gethash ref idhashtb)))
4219               (progn
4220                 (gnus-sethash ref id idhashtb)
4221                 (gnus-sethash id threads thhashtb))
4222             (setq gthread (gnus-gethash gid thhashtb))
4223             (unless entered
4224               ;; We enter a dummy root into the thread, if we
4225               ;; haven't done that already.
4226               (unless (stringp (caar gthread))
4227                 (setcar gthread (list (mail-header-subject (caar gthread))
4228                                       (car gthread))))
4229               ;; We add this new gathered thread to this gathered
4230               ;; thread.
4231               (setcdr (car gthread)
4232                       (nconc (cdar gthread) (list (car threads)))))
4233             ;; Add it into the thread hash table.
4234             (gnus-sethash id gthread thhashtb)
4235             (setq entered t)
4236             ;; Remove it from the list of threads.
4237             (setcdr prev (cdr threads))
4238             (setq threads prev))))
4239       (setq prev threads)
4240       (setq threads (cdr threads)))
4241     result))
4242
4243 (defun gnus-sort-gathered-threads (threads)
4244   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4245   (let ((result threads))
4246     (while threads
4247       (when (stringp (caar threads))
4248         (setcdr (car threads)
4249                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4250       (setq threads (cdr threads)))
4251     result))
4252
4253 (defun gnus-thread-loop-p (root thread)
4254   "Say whether ROOT is in THREAD."
4255   (let ((stack (list thread))
4256         (infloop 0)
4257         th)
4258     (while (setq thread (pop stack))
4259       (setq th (cdr thread))
4260       (while (and th
4261                   (not (eq (caar th) root)))
4262         (pop th))
4263       (if th
4264           ;; We have found a loop.
4265           (let (ref-dep)
4266             (setcdr thread (delq (car th) (cdr thread)))
4267             (if (boundp (setq ref-dep (intern "none"
4268                                               gnus-newsgroup-dependencies)))
4269                 (setcdr (symbol-value ref-dep)
4270                         (nconc (cdr (symbol-value ref-dep))
4271                                (list (car th))))
4272               (set ref-dep (list nil (car th))))
4273             (setq infloop 1
4274                   stack nil))
4275         ;; Push all the subthreads onto the stack.
4276         (push (cdr thread) stack)))
4277     infloop))
4278
4279 (defun gnus-make-threads ()
4280   "Go through the dependency hashtb and find the roots.  Return all threads."
4281   (let (threads)
4282     (while (catch 'infloop
4283              (mapatoms
4284               (lambda (refs)
4285                 ;; Deal with self-referencing References loops.
4286                 (when (and (car (symbol-value refs))
4287                            (not (zerop
4288                                  (apply
4289                                   '+
4290                                   (mapcar
4291                                    (lambda (thread)
4292                                      (gnus-thread-loop-p
4293                                       (car (symbol-value refs)) thread))
4294                                    (cdr (symbol-value refs)))))))
4295                   (setq threads nil)
4296                   (throw 'infloop t))
4297                 (unless (car (symbol-value refs))
4298                   ;; These threads do not refer back to any other
4299                   ;; articles, so they're roots.
4300                   (setq threads (append (cdr (symbol-value refs)) threads))))
4301               gnus-newsgroup-dependencies)))
4302     threads))
4303
4304 ;; Build the thread tree.
4305 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4306   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4307
4308 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4309 if it was already present.
4310
4311 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4312 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4313 Message-IDs will be renamed to a unique Message-ID before being
4314 entered.
4315
4316 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4317   (let* ((id (mail-header-id header))
4318          (id-dep (and id (intern id dependencies)))
4319          parent-id ref ref-dep ref-header replaced)
4320     ;; Enter this `header' in the `dependencies' table.
4321     (cond
4322      ((not id-dep)
4323       (setq header nil))
4324      ;; The first two cases do the normal part: enter a new `header'
4325      ;; in the `dependencies' table.
4326      ((not (boundp id-dep))
4327       (set id-dep (list header)))
4328      ((null (car (symbol-value id-dep)))
4329       (setcar (symbol-value id-dep) header))
4330
4331      ;; From here the `header' was already present in the
4332      ;; `dependencies' table.
4333      (force-new
4334       ;; Overrides an existing entry;
4335       ;; just set the header part of the entry.
4336       (setcar (symbol-value id-dep) header)
4337       (setq replaced t))
4338
4339      ;; Renames the existing `header' to a unique Message-ID.
4340      ((not gnus-summary-ignore-duplicates)
4341       ;; An article with this Message-ID has already been seen.
4342       ;; We rename the Message-ID.
4343       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4344            (list header))
4345       (mail-header-set-id header id))
4346
4347      ;; The last case ignores an existing entry, except it adds any
4348      ;; additional Xrefs (in case the two articles came from different
4349      ;; servers.
4350      ;; Also sets `header' to `nil' meaning that the `dependencies'
4351      ;; table was *not* modified.
4352      (t
4353       (mail-header-set-xref
4354        (car (symbol-value id-dep))
4355        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4356                    "")
4357                (or (mail-header-xref header) "")))
4358       (setq header nil)))
4359
4360     (when (and header (not replaced))
4361       ;; First check that we are not creating a References loop.
4362       (setq parent-id (gnus-parent-id (mail-header-references header)))
4363       (setq ref parent-id)
4364       (while (and ref
4365                   (setq ref-dep (intern-soft ref dependencies))
4366                   (boundp ref-dep)
4367                   (setq ref-header (car (symbol-value ref-dep))))
4368         (if (string= id ref)
4369             ;; Yuk!  This is a reference loop.  Make the article be a
4370             ;; root article.
4371             (progn
4372               (mail-header-set-references (car (symbol-value id-dep)) "none")
4373               (setq ref nil)
4374               (setq parent-id nil))
4375           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4376       (setq ref-dep (intern (or parent-id "none") dependencies))
4377       (if (boundp ref-dep)
4378           (setcdr (symbol-value ref-dep)
4379                   (nconc (cdr (symbol-value ref-dep))
4380                          (list (symbol-value id-dep))))
4381         (set ref-dep (list nil (symbol-value id-dep)))))
4382     header))
4383
4384 (defun gnus-extract-message-id-from-in-reply-to (string)
4385   (if (string-match "<[^>]+>" string)
4386       (substring string (match-beginning 0) (match-end 0))
4387     nil))
4388
4389 (defun gnus-build-sparse-threads ()
4390   (let ((headers gnus-newsgroup-headers)
4391         (mail-parse-charset gnus-newsgroup-charset)
4392         (gnus-summary-ignore-duplicates t)
4393         header references generation relations
4394         subject child end new-child date)
4395     ;; First we create an alist of generations/relations, where
4396     ;; generations is how much we trust the relation, and the relation
4397     ;; is parent/child.
4398     (gnus-message 7 "Making sparse threads...")
4399     (save-excursion
4400       (nnheader-set-temp-buffer " *gnus sparse threads*")
4401       (while (setq header (pop headers))
4402         (when (and (setq references (mail-header-references header))
4403                    (not (string= references "")))
4404           (insert references)
4405           (setq child (mail-header-id header)
4406                 subject (mail-header-subject header)
4407                 date (mail-header-date header)
4408                 generation 0)
4409           (while (search-backward ">" nil t)
4410             (setq end (1+ (point)))
4411             (when (search-backward "<" nil t)
4412               (setq new-child (buffer-substring (point) end))
4413               (push (list (incf generation)
4414                           child (setq child new-child)
4415                           subject date)
4416                     relations)))
4417           (when child
4418             (push (list (1+ generation) child nil subject) relations))
4419           (erase-buffer)))
4420       (kill-buffer (current-buffer)))
4421     ;; Sort over trustworthiness.
4422     (dolist (relation (sort relations 'car-less-than-car))
4423       (when (gnus-dependencies-add-header
4424              (make-full-mail-header
4425               gnus-reffed-article-number
4426               (nth 3 relation) "" (or (nth 4 relation) "")
4427               (nth 1 relation)
4428               (or (nth 2 relation) "") 0 0 "")
4429              gnus-newsgroup-dependencies nil)
4430         (push gnus-reffed-article-number gnus-newsgroup-limit)
4431         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4432         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4433               gnus-newsgroup-reads)
4434         (decf gnus-reffed-article-number)))
4435     (gnus-message 7 "Making sparse threads...done")))
4436
4437 (defun gnus-build-old-threads ()
4438   ;; Look at all the articles that refer back to old articles, and
4439   ;; fetch the headers for the articles that aren't there.  This will
4440   ;; build complete threads - if the roots haven't been expired by the
4441   ;; server, that is.
4442   (let ((mail-parse-charset gnus-newsgroup-charset)
4443         id heads)
4444     (mapatoms
4445      (lambda (refs)
4446        (when (not (car (symbol-value refs)))
4447          (setq heads (cdr (symbol-value refs)))
4448          (while heads
4449            (if (memq (mail-header-number (caar heads))
4450                      gnus-newsgroup-dormant)
4451                (setq heads (cdr heads))
4452              (setq id (symbol-name refs))
4453              (while (and (setq id (gnus-build-get-header id))
4454                          (not (car (gnus-id-to-thread id)))))
4455              (setq heads nil)))))
4456      gnus-newsgroup-dependencies)))
4457
4458 (defsubst gnus-remove-odd-characters (string)
4459   "Translate STRING into something that doesn't contain weird characters."
4460   (mm-subst-char-in-string
4461    ?\r ?\-
4462    (mm-subst-char-in-string ?\n ?\- string t) t))
4463
4464 ;; This function has to be called with point after the article number
4465 ;; on the beginning of the line.
4466 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4467   (let ((eol (point-at-eol))
4468         (buffer (current-buffer))
4469         header references in-reply-to)
4470
4471     ;; overview: [num subject from date id refs chars lines misc]
4472     (unwind-protect
4473         (let (x)
4474           (narrow-to-region (point) eol)
4475           (unless (eobp)
4476             (forward-char))
4477
4478           (setq header
4479                 (make-full-mail-header
4480                  number                 ; number
4481                  (condition-case ()     ; subject
4482                      (gnus-remove-odd-characters
4483                       (funcall gnus-decode-encoded-word-function
4484                                (setq x (nnheader-nov-field))))
4485                    (error x))
4486                  (condition-case ()     ; from
4487                      (gnus-remove-odd-characters
4488                       (funcall gnus-decode-encoded-address-function
4489                                (setq x (nnheader-nov-field))))
4490                    (error x))
4491                  (nnheader-nov-field)   ; date
4492                  (nnheader-nov-read-message-id number)  ; id
4493                  (setq references (nnheader-nov-field)) ; refs
4494                  (nnheader-nov-read-integer) ; chars
4495                  (nnheader-nov-read-integer) ; lines
4496                  (unless (eobp)
4497                    (if (looking-at "Xref: ")
4498                        (goto-char (match-end 0)))
4499                    (nnheader-nov-field)) ; Xref
4500                  (nnheader-nov-parse-extra)))) ; extra
4501
4502       (widen))
4503
4504     (when (and (string= references "")
4505                (setq in-reply-to (mail-header-extra header))
4506                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4507       (mail-header-set-references
4508        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4509
4510     (when gnus-alter-header-function
4511       (funcall gnus-alter-header-function header))
4512     (gnus-dependencies-add-header header dependencies force-new)))
4513
4514 (defun gnus-build-get-header (id)
4515   "Look through the buffer of NOV lines and find the header to ID.
4516 Enter this line into the dependencies hash table, and return
4517 the id of the parent article (if any)."
4518   (let ((deps gnus-newsgroup-dependencies)
4519         found header)
4520     (prog1
4521         (with-current-buffer nntp-server-buffer
4522           (let ((case-fold-search nil))
4523             (goto-char (point-min))
4524             (while (and (not found)
4525                         (search-forward id nil t))
4526               (beginning-of-line)
4527               (setq found (looking-at
4528                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4529                                    (regexp-quote id))))
4530               (or found (beginning-of-line 2)))
4531             (when found
4532               (beginning-of-line)
4533               (and
4534                (setq header (gnus-nov-parse-line
4535                              (read (current-buffer)) deps))
4536                (gnus-parent-id (mail-header-references header))))))
4537       (when header
4538         (let ((number (mail-header-number header)))
4539           (push number gnus-newsgroup-limit)
4540           (push header gnus-newsgroup-headers)
4541           (if (memq number gnus-newsgroup-unselected)
4542               (progn
4543                 (setq gnus-newsgroup-unreads
4544                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4545                                                number))
4546                 (setq gnus-newsgroup-unselected
4547                       (delq number gnus-newsgroup-unselected)))
4548             (push number gnus-newsgroup-ancient)))))))
4549
4550 (defun gnus-build-all-threads ()
4551   "Read all the headers."
4552   (let ((gnus-summary-ignore-duplicates t)
4553         (mail-parse-charset gnus-newsgroup-charset)
4554         (dependencies gnus-newsgroup-dependencies)
4555         header article)
4556     (with-current-buffer nntp-server-buffer
4557       (let ((case-fold-search nil))
4558         (goto-char (point-min))
4559         (while (not (eobp))
4560           (ignore-errors
4561             (setq article (read (current-buffer))
4562                   header (gnus-nov-parse-line article dependencies t)))
4563           (when header
4564             (with-current-buffer gnus-summary-buffer
4565               (push header gnus-newsgroup-headers)
4566               (if (memq (setq article (mail-header-number header))
4567                         gnus-newsgroup-unselected)
4568                   (progn
4569                     (setq gnus-newsgroup-unreads
4570                           (gnus-add-to-sorted-list
4571                            gnus-newsgroup-unreads article))
4572                     (setq gnus-newsgroup-unselected
4573                           (delq article gnus-newsgroup-unselected)))
4574                 (push article gnus-newsgroup-ancient)))
4575             (forward-line 1)))))))
4576
4577 (defun gnus-summary-update-article-line (article header)
4578   "Update the line for ARTICLE using HEADER."
4579   (let* ((id (mail-header-id header))
4580          (thread (gnus-id-to-thread id)))
4581     (unless thread
4582       (error "Article in no thread"))
4583     ;; Update the thread.
4584     (setcar thread header)
4585     (gnus-summary-goto-subject article)
4586     (let* ((datal (gnus-data-find-list article))
4587            (data (car datal))
4588            (inhibit-read-only t)
4589            (level (gnus-summary-thread-level)))
4590       (gnus-delete-line)
4591       (let ((inserted (- (point)
4592                          (progn
4593                            (gnus-summary-insert-line
4594                             header level nil
4595                             (memq article gnus-newsgroup-undownloaded)
4596                             (gnus-article-mark article)
4597                             (memq article gnus-newsgroup-replied)
4598                             (memq article gnus-newsgroup-expirable)
4599                             ;; Only insert the Subject string when it's different
4600                             ;; from the previous Subject string.
4601                             (if (and
4602                                  gnus-show-threads
4603                                  (gnus-subject-equal
4604                                   (condition-case ()
4605                                       (mail-header-subject
4606                                        (gnus-data-header
4607                                         (cadr
4608                                          (gnus-data-find-list
4609                                           article
4610                                           (gnus-data-list t)))))
4611                                     ;; Error on the side of excessive subjects.
4612                                     (error ""))
4613                                   (mail-header-subject header)))
4614                                 ""
4615                               (mail-header-subject header))
4616                             nil (cdr (assq article gnus-newsgroup-scored))
4617                             (memq article gnus-newsgroup-processable))
4618                            (point)))))
4619         (when (cdr datal)
4620           (gnus-data-update-list
4621            (cdr datal)
4622            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4623
4624 (defun gnus-summary-update-article (article &optional iheader)
4625   "Update ARTICLE in the summary buffer."
4626   (set-buffer gnus-summary-buffer)
4627   (let* ((header (gnus-summary-article-header article))
4628          (id (mail-header-id header))
4629          (data (gnus-data-find article))
4630          (thread (gnus-id-to-thread id))
4631          (references (mail-header-references header))
4632          (parent
4633           (gnus-id-to-thread
4634            (or (gnus-parent-id
4635                 (when (and references
4636                            (not (equal "" references)))
4637                   references))
4638                "none")))
4639          (inhibit-read-only t)
4640          (old (car thread)))
4641     (when thread
4642       (unless iheader
4643         (setcar thread nil)
4644         (when parent
4645           (delq thread parent)))
4646       (if (gnus-summary-insert-subject id header)
4647           ;; Set the (possibly) new article number in the data structure.
4648           (gnus-data-set-number data (gnus-id-to-article id))
4649         (setcar thread old)
4650         nil))))
4651
4652 (defun gnus-rebuild-thread (id &optional line)
4653   "Rebuild the thread containing ID.
4654 If LINE, insert the rebuilt thread starting on line LINE."
4655   (let ((inhibit-read-only t)
4656         old-pos current thread data)
4657     (if (not gnus-show-threads)
4658         (setq thread (list (car (gnus-id-to-thread id))))
4659       ;; Get the thread this article is part of.
4660       (setq thread (gnus-remove-thread id)))
4661     (setq old-pos (point-at-bol))
4662     (setq current (save-excursion
4663                     (and (re-search-backward "[\r\n]" nil t)
4664                          (gnus-summary-article-number))))
4665     ;; If this is a gathered thread, we have to go some re-gathering.
4666     (when (stringp (car thread))
4667       (let ((subject (car thread))
4668             roots thr)
4669         (setq thread (cdr thread))
4670         (while thread
4671           (unless (memq (setq thr (gnus-id-to-thread
4672                                    (gnus-root-id
4673                                     (mail-header-id (caar thread)))))
4674                         roots)
4675             (push thr roots))
4676           (setq thread (cdr thread)))
4677         ;; We now have all (unique) roots.
4678         (if (= (length roots) 1)
4679             ;; All the loose roots are now one solid root.
4680             (setq thread (car roots))
4681           (setq thread (cons subject (gnus-sort-threads roots))))))
4682     (let (threads)
4683       ;; We then insert this thread into the summary buffer.
4684       (when line
4685         (goto-char (point-min))
4686         (forward-line (1- line)))
4687       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4688         (if gnus-show-threads
4689             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4690           (gnus-summary-prepare-unthreaded thread))
4691         (setq data (nreverse gnus-newsgroup-data))
4692         (setq threads gnus-newsgroup-threads))
4693       ;; We splice the new data into the data structure.
4694       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4695       ;;!!! then we want to insert at the beginning of the buffer.
4696       ;;!!! That happens to be true with Gnus now, but that may
4697       ;;!!! change in the future.  Perhaps.
4698       (gnus-data-enter-list
4699        (if line nil current) data (- (point) old-pos))
4700       (setq gnus-newsgroup-threads
4701             (nconc threads gnus-newsgroup-threads))
4702       (gnus-data-compute-positions))))
4703
4704 (defun gnus-number-to-header (number)
4705   "Return the header for article NUMBER."
4706   (let ((headers gnus-newsgroup-headers))
4707     (while (and headers
4708                 (not (= number (mail-header-number (car headers)))))
4709       (pop headers))
4710     (when headers
4711       (car headers))))
4712
4713 (defun gnus-parent-headers (in-headers &optional generation)
4714   "Return the headers of the GENERATIONth parent of HEADERS."
4715   (unless generation
4716     (setq generation 1))
4717   (let ((parent t)
4718         (headers in-headers)
4719         references)
4720     (while (and parent
4721                 (not (zerop generation))
4722                 (setq references (mail-header-references headers)))
4723       (setq headers (if (and references
4724                              (setq parent (gnus-parent-id references)))
4725                         (car (gnus-id-to-thread parent))
4726                       nil))
4727       (decf generation))
4728     (and (not (eq headers in-headers))
4729          headers)))
4730
4731 (defun gnus-id-to-thread (id)
4732   "Return the (sub-)thread where ID appears."
4733   (gnus-gethash id gnus-newsgroup-dependencies))
4734
4735 (defun gnus-id-to-article (id)
4736   "Return the article number of ID."
4737   (let ((thread (gnus-id-to-thread id)))
4738     (when (and thread
4739                (car thread))
4740       (mail-header-number (car thread)))))
4741
4742 (defun gnus-id-to-header (id)
4743   "Return the article headers of ID."
4744   (car (gnus-id-to-thread id)))
4745
4746 (defun gnus-article-displayed-root-p (article)
4747   "Say whether ARTICLE is a root(ish) article."
4748   (let ((level (gnus-summary-thread-level article))
4749         (refs (mail-header-references  (gnus-summary-article-header article)))
4750         particle)
4751     (cond
4752      ((null level) nil)
4753      ((zerop level) t)
4754      ((null refs) t)
4755      ((null (gnus-parent-id refs)) t)
4756      ((and (= 1 level)
4757            (null (setq particle (gnus-id-to-article
4758                                  (gnus-parent-id refs))))
4759            (null (gnus-summary-thread-level particle)))))))
4760
4761 (defun gnus-root-id (id)
4762   "Return the id of the root of the thread where ID appears."
4763   (let (last-id prev)
4764     (while (and id (setq prev (car (gnus-id-to-thread id))))
4765       (setq last-id id
4766             id (gnus-parent-id (mail-header-references prev))))
4767     last-id))
4768
4769 (defun gnus-articles-in-thread (thread)
4770   "Return the list of articles in THREAD."
4771   (cons (mail-header-number (car thread))
4772         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4773
4774 (defun gnus-remove-thread (id &optional dont-remove)
4775   "Remove the thread that has ID in it."
4776   (let (headers thread last-id)
4777     ;; First go up in this thread until we find the root.
4778     (setq last-id (gnus-root-id id)
4779           headers (message-flatten-list (gnus-id-to-thread last-id)))
4780     ;; We have now found the real root of this thread.  It might have
4781     ;; been gathered into some loose thread, so we have to search
4782     ;; through the threads to find the thread we wanted.
4783     (let ((threads gnus-newsgroup-threads)
4784           sub)
4785       (while threads
4786         (setq sub (car threads))
4787         (if (stringp (car sub))
4788             ;; This is a gathered thread, so we look at the roots
4789             ;; below it to find whether this article is in this
4790             ;; gathered root.
4791             (progn
4792               (setq sub (cdr sub))
4793               (while sub
4794                 (when (member (caar sub) headers)
4795                   (setq thread (car threads)
4796                         threads nil
4797                         sub nil))
4798                 (setq sub (cdr sub))))
4799           ;; It's an ordinary thread, so we check it.
4800           (when (eq (car sub) (car headers))
4801             (setq thread sub
4802                   threads nil)))
4803         (setq threads (cdr threads)))
4804       ;; If this article is in no thread, then it's a root.
4805       (if thread
4806           (unless dont-remove
4807             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4808         (setq thread (gnus-id-to-thread last-id)))
4809       (when thread
4810         (prog1
4811             thread                      ; We return this thread.
4812           (unless dont-remove
4813             (if (stringp (car thread))
4814                 (progn
4815                   ;; If we use dummy roots, then we have to remove the
4816                   ;; dummy root as well.
4817                   (when (eq gnus-summary-make-false-root 'dummy)
4818                     ;; We go to the dummy root by going to
4819                     ;; the first sub-"thread", and then one line up.
4820                     (gnus-summary-goto-article
4821                      (mail-header-number (caadr thread)))
4822                     (forward-line -1)
4823                     (gnus-delete-line)
4824                     (gnus-data-compute-positions))
4825                   (setq thread (cdr thread))
4826                   (while thread
4827                     (gnus-remove-thread-1 (car thread))
4828                     (setq thread (cdr thread))))
4829               (gnus-remove-thread-1 thread))))))))
4830
4831 (defun gnus-remove-thread-1 (thread)
4832   "Remove the thread THREAD recursively."
4833   (let ((number (mail-header-number (pop thread)))
4834         d)
4835     (setq thread (reverse thread))
4836     (while thread
4837       (gnus-remove-thread-1 (pop thread)))
4838     (when (setq d (gnus-data-find number))
4839       (goto-char (gnus-data-pos d))
4840       (gnus-summary-show-thread)
4841       (gnus-data-remove
4842        number
4843        (- (point-at-bol)
4844           (prog1
4845               (1+ (point-at-eol))
4846             (gnus-delete-line)))))))
4847
4848 (defun gnus-sort-threads-recursive (threads func)
4849   (sort (mapcar (lambda (thread)
4850                   (cons (car thread)
4851                         (and (cdr thread)
4852                              (gnus-sort-threads-recursive (cdr thread) func))))
4853                 threads) func))
4854
4855 (defun gnus-sort-threads-loop (threads func)
4856   (let* ((superthread (cons nil threads))
4857          (stack (list (cons superthread threads)))
4858          remaining-threads thread)
4859     (while stack
4860       (setq remaining-threads (cdr (car stack)))
4861       (if remaining-threads
4862           (progn (setq thread (car remaining-threads))
4863                  (setcdr (car stack) (cdr remaining-threads))
4864                  (if (cdr thread)
4865                      (push (cons thread (cdr thread)) stack)))
4866         (setq thread (caar stack))
4867         (setcdr thread (sort (cdr thread) func))
4868         (pop stack)))
4869     (cdr superthread)))
4870
4871 (defun gnus-sort-threads (threads)
4872   "Sort THREADS."
4873   (if (not gnus-thread-sort-functions)
4874       threads
4875     (gnus-message 8 "Sorting threads...")
4876     (prog1
4877         (condition-case nil
4878             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4879               (gnus-sort-threads-recursive
4880                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4881           ;; Even after binding max-lisp-eval-depth, the recursive
4882           ;; sorter might fail for very long threads.  In that case,
4883           ;; try using a (less well-tested) non-recursive sorter.
4884           (error (gnus-message 9 "Sorting threads with loop...")
4885                  (gnus-sort-threads-loop
4886                   threads (gnus-make-sort-function
4887                            gnus-thread-sort-functions))))
4888       (gnus-message 8 "Sorting threads...done"))))
4889
4890 (defun gnus-sort-articles (articles)
4891   "Sort ARTICLES."
4892   (when gnus-article-sort-functions
4893     (gnus-message 7 "Sorting articles...")
4894     (prog1
4895         (setq gnus-newsgroup-headers
4896               (sort articles (gnus-make-sort-function
4897                               gnus-article-sort-functions)))
4898       (gnus-message 7 "Sorting articles...done"))))
4899
4900 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4901 (defmacro gnus-thread-header (thread)
4902   "Return header of first article in THREAD.
4903 Note that THREAD must never, ever be anything else than a variable -
4904 using some other form will lead to serious barfage."
4905   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4906   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4907   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4908         (vector thread) 2))
4909
4910 (defsubst gnus-article-sort-by-number (h1 h2)
4911   "Sort articles by article number."
4912   (< (mail-header-number h1)
4913      (mail-header-number h2)))
4914
4915 (defun gnus-thread-sort-by-number (h1 h2)
4916   "Sort threads by root article number."
4917   (gnus-article-sort-by-number
4918    (gnus-thread-header h1) (gnus-thread-header h2)))
4919
4920 (defsubst gnus-article-sort-by-random (h1 h2)
4921   "Sort articles randomly."
4922   (zerop (random 2)))
4923
4924 (defun gnus-thread-sort-by-random (h1 h2)
4925   "Sort threads randomly."
4926   (gnus-article-sort-by-random
4927    (gnus-thread-header h1) (gnus-thread-header h2)))
4928
4929 (defsubst gnus-article-sort-by-lines (h1 h2)
4930   "Sort articles by article Lines header."
4931   (< (mail-header-lines h1)
4932      (mail-header-lines h2)))
4933
4934 (defun gnus-thread-sort-by-lines (h1 h2)
4935   "Sort threads by root article Lines header."
4936   (gnus-article-sort-by-lines
4937    (gnus-thread-header h1) (gnus-thread-header h2)))
4938
4939 (defsubst gnus-article-sort-by-chars (h1 h2)
4940   "Sort articles by octet length."
4941   (< (mail-header-chars h1)
4942      (mail-header-chars h2)))
4943
4944 (defun gnus-thread-sort-by-chars (h1 h2)
4945   "Sort threads by root article octet length."
4946   (gnus-article-sort-by-chars
4947    (gnus-thread-header h1) (gnus-thread-header h2)))
4948
4949 (defsubst gnus-article-sort-by-author (h1 h2)
4950   "Sort articles by root author."
4951   (gnus-string<
4952    (let ((extract (funcall
4953                    gnus-extract-address-components
4954                    (mail-header-from h1))))
4955      (or (car extract) (cadr extract) ""))
4956    (let ((extract (funcall
4957                    gnus-extract-address-components
4958                    (mail-header-from h2))))
4959      (or (car extract) (cadr extract) ""))))
4960
4961 (defun gnus-thread-sort-by-author (h1 h2)
4962   "Sort threads by root author."
4963   (gnus-article-sort-by-author
4964    (gnus-thread-header h1)  (gnus-thread-header h2)))
4965
4966 (defsubst gnus-article-sort-by-recipient (h1 h2)
4967   "Sort articles by recipient."
4968   (gnus-string<
4969    (let ((extract (funcall
4970                    gnus-extract-address-components
4971                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4972      (or (car extract) (cadr extract)))
4973    (let ((extract (funcall
4974                    gnus-extract-address-components
4975                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4976      (or (car extract) (cadr extract)))))
4977
4978 (defun gnus-thread-sort-by-recipient (h1 h2)
4979   "Sort threads by root recipient."
4980   (gnus-article-sort-by-recipient
4981    (gnus-thread-header h1) (gnus-thread-header h2)))
4982
4983 (defsubst gnus-article-sort-by-subject (h1 h2)
4984   "Sort articles by root subject."
4985   (gnus-string<
4986    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4987    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4988
4989 (defun gnus-thread-sort-by-subject (h1 h2)
4990   "Sort threads by root subject."
4991   (gnus-article-sort-by-subject
4992    (gnus-thread-header h1) (gnus-thread-header h2)))
4993
4994 (defsubst gnus-article-sort-by-date (h1 h2)
4995   "Sort articles by root article date."
4996   (time-less-p
4997    (gnus-date-get-time (mail-header-date h1))
4998    (gnus-date-get-time (mail-header-date h2))))
4999
5000 (defun gnus-thread-sort-by-date (h1 h2)
5001   "Sort threads by root article date."
5002   (gnus-article-sort-by-date
5003    (gnus-thread-header h1) (gnus-thread-header h2)))
5004
5005 (defsubst gnus-article-sort-by-score (h1 h2)
5006   "Sort articles by root article score.
5007 Unscored articles will be counted as having a score of zero."
5008   (> (or (cdr (assq (mail-header-number h1)
5009                     gnus-newsgroup-scored))
5010          gnus-summary-default-score 0)
5011      (or (cdr (assq (mail-header-number h2)
5012                     gnus-newsgroup-scored))
5013          gnus-summary-default-score 0)))
5014
5015 (defun gnus-thread-sort-by-score (h1 h2)
5016   "Sort threads by root article score."
5017   (gnus-article-sort-by-score
5018    (gnus-thread-header h1) (gnus-thread-header h2)))
5019
5020 (defun gnus-thread-sort-by-total-score (h1 h2)
5021   "Sort threads by the sum of all scores in the thread.
5022 Unscored articles will be counted as having a score of zero."
5023   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5024
5025 (defun gnus-thread-total-score (thread)
5026   ;; This function find the total score of THREAD.
5027   (cond
5028    ((null thread)
5029     0)
5030    ((consp thread)
5031     (if (stringp (car thread))
5032         (apply gnus-thread-score-function 0
5033                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5034       (gnus-thread-total-score-1 thread)))
5035    (t
5036     (gnus-thread-total-score-1 (list thread)))))
5037
5038 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5039   "Sort articles by number."
5040   (gnus-article-sort-by-number h1 h2))
5041
5042 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5043   "Sort threads such that the thread with the most recently arrived article comes first."
5044   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5045
5046 (defun gnus-thread-highest-number (thread)
5047   "Return the highest article number in THREAD."
5048   (apply 'max (mapcar (lambda (header)
5049                         (mail-header-number header))
5050                       (message-flatten-list thread))))
5051
5052 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5053   "Sort articles by number."
5054   (gnus-article-sort-by-date h1 h2))
5055
5056 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5057   "Sort threads such that the thread with the most recently dated article comes first."
5058   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5059
5060 ; Since this is called not only to sort the top-level threads, but
5061 ; also in recursive sorts to order the articles within a thread, each
5062 ; article will be processed many times.  Thus it speeds things up
5063 ; quite a bit to use gnus-date-get-time, which caches the time value.
5064 (defun gnus-thread-latest-date (thread)
5065   "Return the highest article date in THREAD."
5066   (apply 'max
5067          (mapcar (lambda (header) (gnus-float-time
5068                                    (gnus-date-get-time
5069                                     (mail-header-date header))))
5070                  (message-flatten-list thread))))
5071
5072 (defun gnus-thread-total-score-1 (root)
5073   ;; This function find the total score of the thread below ROOT.
5074   (setq root (car root))
5075   (apply gnus-thread-score-function
5076          (or (append
5077               (mapcar 'gnus-thread-total-score
5078                       (cdr (gnus-id-to-thread (mail-header-id root))))
5079               (when (> (mail-header-number root) 0)
5080                 (list (or (cdr (assq (mail-header-number root)
5081                                      gnus-newsgroup-scored))
5082                           gnus-summary-default-score 0))))
5083              (list gnus-summary-default-score)
5084              '(0))))
5085
5086 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5087 (defvar gnus-tmp-prev-subject nil)
5088 (defvar gnus-tmp-false-parent nil)
5089 (defvar gnus-tmp-root-expunged nil)
5090 (defvar gnus-tmp-dummy-line nil)
5091
5092 (defun gnus-extra-header (type &optional header)
5093   "Return the extra header of TYPE."
5094   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5095       ""))
5096
5097 (defvar gnus-tmp-thread-tree-header-string "")
5098
5099 (defcustom gnus-sum-thread-tree-root "> "
5100   "With %B spec, used for the root of a thread.
5101 If nil, use subject instead."
5102   :version "22.1"
5103   :type '(radio (const :format "%v  " nil) string)
5104   :group 'gnus-thread)
5105
5106 (defcustom gnus-sum-thread-tree-false-root "> "
5107   "With %B spec, used for a false root of a thread.
5108 If nil, use subject instead."
5109   :version "22.1"
5110   :type '(radio (const :format "%v  " nil) string)
5111   :group 'gnus-thread)
5112
5113 (defcustom gnus-sum-thread-tree-single-indent ""
5114   "With %B spec, used for a thread with just one message.
5115 If nil, use subject instead."
5116   :version "22.1"
5117   :type '(radio (const :format "%v  " nil) string)
5118   :group 'gnus-thread)
5119
5120 (defcustom gnus-sum-thread-tree-vertical "| "
5121   "With %B spec, used for drawing a vertical line."
5122   :version "22.1"
5123   :type 'string
5124   :group 'gnus-thread)
5125
5126 (defcustom gnus-sum-thread-tree-indent "  "
5127   "With %B spec, used for indenting."
5128   :version "22.1"
5129   :type 'string
5130   :group 'gnus-thread)
5131
5132 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5133   "With %B spec, used for a leaf with brothers."
5134   :version "22.1"
5135   :type 'string
5136   :group 'gnus-thread)
5137
5138 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5139   "With %B spec, used for a leaf without brothers."
5140   :version "22.1"
5141   :type 'string
5142   :group 'gnus-thread)
5143
5144 (defcustom gnus-summary-display-while-building nil
5145   "If non-nil, show and update the summary buffer as it's being built.
5146 If the value is t, update the buffer after every line is inserted.  If
5147 the value is an integer (N), update the display every N lines."
5148   :version "22.1"
5149   :group 'gnus-thread
5150   :type '(choice (const :tag "off" nil)
5151                  number
5152                  (const :tag "frequently" t)))
5153
5154 (defun gnus-summary-prepare-threads (threads)
5155   "Prepare summary buffer from THREADS and indentation LEVEL.
5156 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5157 or a straight list of headers."
5158   (gnus-message 7 "Generating summary...")
5159
5160   (setq gnus-newsgroup-threads threads)
5161   (beginning-of-line)
5162
5163   (let ((gnus-tmp-level 0)
5164         (default-score (or gnus-summary-default-score 0))
5165         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5166         (building-line-count gnus-summary-display-while-building)
5167         (building-count (integerp gnus-summary-display-while-building))
5168         thread number subject stack state gnus-tmp-gathered beg-match
5169         new-roots gnus-tmp-new-adopts thread-end simp-subject
5170         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5171         gnus-tmp-replied gnus-tmp-subject-or-nil
5172         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5173         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5174         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5175         tree-stack)
5176
5177     (setq gnus-tmp-prev-subject nil
5178           gnus-tmp-thread-tree-header-string "")
5179
5180     (if (vectorp (car threads))
5181         ;; If this is a straight (sic) list of headers, then a
5182         ;; threaded summary display isn't required, so we just create
5183         ;; an unthreaded one.
5184         (gnus-summary-prepare-unthreaded threads)
5185
5186       ;; Do the threaded display.
5187
5188       (if gnus-summary-display-while-building
5189           (switch-to-buffer (buffer-name)))
5190       (while (or threads stack gnus-tmp-new-adopts new-roots)
5191
5192         (if (and (= gnus-tmp-level 0)
5193                  (or (not stack)
5194                      (= (caar stack) 0))
5195                  (not gnus-tmp-false-parent)
5196                  (or gnus-tmp-new-adopts new-roots))
5197             (if gnus-tmp-new-adopts
5198                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5199                       thread (list (car gnus-tmp-new-adopts))
5200                       gnus-tmp-header (caar thread)
5201                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5202               (when new-roots
5203                 (setq thread (list (car new-roots))
5204                       gnus-tmp-header (caar thread)
5205                       new-roots (cdr new-roots))))
5206
5207           (if threads
5208               ;; If there are some threads, we do them before the
5209               ;; threads on the stack.
5210               (setq thread threads
5211                     gnus-tmp-header (caar thread))
5212             ;; There were no current threads, so we pop something off
5213             ;; the stack.
5214             (setq state (car stack)
5215                   gnus-tmp-level (car state)
5216                   tree-stack (cadr state)
5217                   thread (caddr state)
5218                   stack (cdr stack)
5219                   gnus-tmp-header (caar thread))))
5220
5221         (setq gnus-tmp-false-parent nil)
5222         (setq gnus-tmp-root-expunged nil)
5223         (setq thread-end nil)
5224
5225         (if (stringp gnus-tmp-header)
5226             ;; The header is a dummy root.
5227             (cond
5228              ((eq gnus-summary-make-false-root 'adopt)
5229               ;; We let the first article adopt the rest.
5230               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5231                                                (cddar thread)))
5232               (setq gnus-tmp-gathered
5233                     (nconc (mapcar
5234                             (lambda (h) (mail-header-number (car h)))
5235                             (cddar thread))
5236                            gnus-tmp-gathered))
5237               (setq thread (cons (list (caar thread)
5238                                        (cadar thread))
5239                                  (cdr thread)))
5240               (setq gnus-tmp-level -1
5241                     gnus-tmp-false-parent t))
5242              ((eq gnus-summary-make-false-root 'empty)
5243               ;; We print adopted articles with empty subject fields.
5244               (setq gnus-tmp-gathered
5245                     (nconc (mapcar
5246                             (lambda (h) (mail-header-number (car h)))
5247                             (cddar thread))
5248                            gnus-tmp-gathered))
5249               (setq gnus-tmp-level -1))
5250              ((eq gnus-summary-make-false-root 'dummy)
5251               ;; We remember that we probably want to output a dummy
5252               ;; root.
5253               (setq gnus-tmp-dummy-line gnus-tmp-header)
5254               (setq gnus-tmp-prev-subject gnus-tmp-header))
5255              (t
5256               ;; We do not make a root for the gathered
5257               ;; sub-threads at all.
5258               (setq gnus-tmp-level -1)))
5259
5260           (setq number (mail-header-number gnus-tmp-header)
5261                 subject (mail-header-subject gnus-tmp-header)
5262                 simp-subject (gnus-simplify-subject-fully subject))
5263
5264           (cond
5265            ;; If the thread has changed subject, we might want to make
5266            ;; this subthread into a root.
5267            ((and (null gnus-thread-ignore-subject)
5268                  (not (zerop gnus-tmp-level))
5269                  gnus-tmp-prev-subject
5270                  (not (string= gnus-tmp-prev-subject simp-subject)))
5271             (setq new-roots (nconc new-roots (list (car thread)))
5272                   thread-end t
5273                   gnus-tmp-header nil))
5274            ;; If the article lies outside the current limit,
5275            ;; then we do not display it.
5276            ((not (memq number gnus-newsgroup-limit))
5277             (setq gnus-tmp-gathered
5278                   (nconc (mapcar
5279                           (lambda (h) (mail-header-number (car h)))
5280                           (cdar thread))
5281                          gnus-tmp-gathered))
5282             (setq gnus-tmp-new-adopts (if (cdar thread)
5283                                           (append gnus-tmp-new-adopts
5284                                                   (cdar thread))
5285                                         gnus-tmp-new-adopts)
5286                   thread-end t
5287                   gnus-tmp-header nil)
5288             (when (zerop gnus-tmp-level)
5289               (setq gnus-tmp-root-expunged t)))
5290            ;; Perhaps this article is to be marked as read?
5291            ((and gnus-summary-mark-below
5292                  (< (or (cdr (assq number gnus-newsgroup-scored))
5293                         default-score)
5294                     gnus-summary-mark-below)
5295                  ;; Don't touch sparse articles.
5296                  (not (gnus-summary-article-sparse-p number))
5297                  (not (gnus-summary-article-ancient-p number)))
5298             (setq gnus-newsgroup-unreads
5299                   (delq number gnus-newsgroup-unreads))
5300             (if gnus-newsgroup-auto-expire
5301                 (setq gnus-newsgroup-expirable
5302                       (gnus-add-to-sorted-list
5303                        gnus-newsgroup-expirable number))
5304               (push (cons number gnus-low-score-mark)
5305                     gnus-newsgroup-reads))))
5306
5307           (when gnus-tmp-header
5308             ;; We may have an old dummy line to output before this
5309             ;; article.
5310             (when (and gnus-tmp-dummy-line
5311                        (gnus-subject-equal
5312                         gnus-tmp-dummy-line
5313                         (mail-header-subject gnus-tmp-header)))
5314               (gnus-summary-insert-dummy-line
5315                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5316               (setq gnus-tmp-dummy-line nil))
5317
5318             ;; Compute the mark.
5319             (setq gnus-tmp-unread (gnus-article-mark number))
5320
5321             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5322                                   gnus-tmp-header gnus-tmp-level)
5323                   gnus-newsgroup-data)
5324
5325             ;; Actually insert the line.
5326             (setq
5327              gnus-tmp-subject-or-nil
5328              (cond
5329               ((and gnus-thread-ignore-subject
5330                     gnus-tmp-prev-subject
5331                     (not (string= gnus-tmp-prev-subject simp-subject)))
5332                subject)
5333               ((zerop gnus-tmp-level)
5334                (if (and (eq gnus-summary-make-false-root 'empty)
5335                         (memq number gnus-tmp-gathered)
5336                         gnus-tmp-prev-subject
5337                         (string= gnus-tmp-prev-subject simp-subject))
5338                    gnus-summary-same-subject
5339                  subject))
5340               (t gnus-summary-same-subject)))
5341             (if (and (eq gnus-summary-make-false-root 'adopt)
5342                      (= gnus-tmp-level 1)
5343                      (memq number gnus-tmp-gathered))
5344                 (setq gnus-tmp-opening-bracket ?\<
5345                       gnus-tmp-closing-bracket ?\>)
5346               (setq gnus-tmp-opening-bracket ?\[
5347                     gnus-tmp-closing-bracket ?\]))
5348             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5349                 (gnus-make-thread-indent-array
5350                  (max (* 2 (length gnus-thread-indent-array))
5351                       gnus-tmp-level)))
5352             (setq
5353              gnus-tmp-indentation
5354              (aref gnus-thread-indent-array gnus-tmp-level)
5355              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5356              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5357                                 gnus-summary-default-score 0)
5358              gnus-tmp-score-char
5359              (if (or (null gnus-summary-default-score)
5360                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5361                          gnus-summary-zcore-fuzz))
5362                  ?                      ;Whitespace
5363                (if (< gnus-tmp-score gnus-summary-default-score)
5364                    gnus-score-below-mark gnus-score-over-mark))
5365              gnus-tmp-replied
5366              (cond ((memq number gnus-newsgroup-processable)
5367                     gnus-process-mark)
5368                    ((memq number gnus-newsgroup-cached)
5369                     gnus-cached-mark)
5370                    ((memq number gnus-newsgroup-replied)
5371                     gnus-replied-mark)
5372                    ((memq number gnus-newsgroup-forwarded)
5373                     gnus-forwarded-mark)
5374                    ((memq number gnus-newsgroup-saved)
5375                     gnus-saved-mark)
5376                    ((memq number gnus-newsgroup-unseen)
5377                     gnus-unseen-mark)
5378                    (t gnus-no-mark))
5379              gnus-tmp-downloaded
5380              (cond ((memq number gnus-newsgroup-undownloaded)
5381                     gnus-undownloaded-mark)
5382                    (gnus-newsgroup-agentized
5383                     gnus-downloaded-mark)
5384                    (t
5385                     gnus-no-mark))
5386              gnus-tmp-from (mail-header-from gnus-tmp-header)
5387              gnus-tmp-name
5388              (cond
5389               ((string-match "<[^>]+> *$" gnus-tmp-from)
5390                (setq beg-match (match-beginning 0))
5391                (or (and (string-match "^\".+\"" gnus-tmp-from)
5392                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5393                    (substring gnus-tmp-from 0 beg-match)))
5394               ((string-match "(.+)" gnus-tmp-from)
5395                (substring gnus-tmp-from
5396                           (1+ (match-beginning 0)) (1- (match-end 0))))
5397               (t gnus-tmp-from))
5398
5399              ;; Do the %B string
5400              gnus-tmp-thread-tree-header-string
5401              (cond
5402               ((not gnus-show-threads) "")
5403               ((zerop gnus-tmp-level)
5404                (cond ((cdar thread)
5405                       (or gnus-sum-thread-tree-root subject))
5406                      (gnus-tmp-new-adopts
5407                       (or gnus-sum-thread-tree-false-root subject))
5408                      (t
5409                       (or gnus-sum-thread-tree-single-indent subject))))
5410               (t
5411                (concat (apply 'concat
5412                               (mapcar (lambda (item)
5413                                         (if (= item 1)
5414                                             gnus-sum-thread-tree-vertical
5415                                           gnus-sum-thread-tree-indent))
5416                                       (cdr (reverse tree-stack))))
5417                        (if (nth 1 thread)
5418                            gnus-sum-thread-tree-leaf-with-other
5419                          gnus-sum-thread-tree-single-leaf)))))
5420             (when (string= gnus-tmp-name "")
5421               (setq gnus-tmp-name gnus-tmp-from))
5422             (unless (numberp gnus-tmp-lines)
5423               (setq gnus-tmp-lines -1))
5424             (if (= gnus-tmp-lines -1)
5425                 (setq gnus-tmp-lines "?")
5426               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5427             (gnus-put-text-property
5428              (point)
5429              (progn (eval gnus-summary-line-format-spec) (point))
5430              'gnus-number number)
5431             (when gnus-visual-p
5432               (forward-line -1)
5433               (gnus-summary-highlight-line)
5434               (when gnus-summary-update-hook
5435                 (gnus-run-hooks 'gnus-summary-update-hook))
5436               (forward-line 1))
5437
5438             (setq gnus-tmp-prev-subject simp-subject)))
5439
5440         (when (nth 1 thread)
5441           (push (list (max 0 gnus-tmp-level)
5442                       (copy-sequence tree-stack)
5443                       (nthcdr 1 thread))
5444                 stack))
5445         (push (if (nth 1 thread) 1 0) tree-stack)
5446         (incf gnus-tmp-level)
5447         (setq threads (if thread-end nil (cdar thread)))
5448         (if gnus-summary-display-while-building
5449             (if building-count
5450                 (progn
5451                   ;; use a set frequency
5452                   (setq building-line-count (1- building-line-count))
5453                   (when (= building-line-count 0)
5454                     (sit-for 0)
5455                     (setq building-line-count
5456                           gnus-summary-display-while-building)))
5457               ;; always
5458               (sit-for 0)))
5459         (unless threads
5460           (setq gnus-tmp-level 0)))))
5461   (gnus-message 7 "Generating summary...done"))
5462
5463 (defun gnus-summary-prepare-unthreaded (headers)
5464   "Generate an unthreaded summary buffer based on HEADERS."
5465   (let (header number mark)
5466
5467     (beginning-of-line)
5468
5469     (while headers
5470       ;; We may have to root out some bad articles...
5471       (when (memq (setq number (mail-header-number
5472                                 (setq header (pop headers))))
5473                   gnus-newsgroup-limit)
5474         ;; Mark article as read when it has a low score.
5475         (when (and gnus-summary-mark-below
5476                    (< (or (cdr (assq number gnus-newsgroup-scored))
5477                           gnus-summary-default-score 0)
5478                       gnus-summary-mark-below)
5479                    (not (gnus-summary-article-ancient-p number)))
5480           (setq gnus-newsgroup-unreads
5481                 (delq number gnus-newsgroup-unreads))
5482           (if gnus-newsgroup-auto-expire
5483               (push number gnus-newsgroup-expirable)
5484             (push (cons number gnus-low-score-mark)
5485                   gnus-newsgroup-reads)))
5486
5487         (setq mark (gnus-article-mark number))
5488         (push (gnus-data-make number mark (1+ (point)) header 0)
5489               gnus-newsgroup-data)
5490         (gnus-summary-insert-line
5491          header 0 number
5492          (memq number gnus-newsgroup-undownloaded)
5493          mark (memq number gnus-newsgroup-replied)
5494          (memq number gnus-newsgroup-expirable)
5495          (mail-header-subject header) nil
5496          (cdr (assq number gnus-newsgroup-scored))
5497          (memq number gnus-newsgroup-processable))))))
5498
5499 (declare-function gnus-parameter-list-identifier "gnus-art" (name) t)
5500
5501 (defun gnus-group-get-list-identifiers (group)
5502   "Get list identifier regexp for GROUP."
5503   (or (gnus-parameter-list-identifier group)
5504       (if (consp gnus-list-identifiers)
5505           (mapconcat 'identity gnus-list-identifiers " *\\|")
5506         gnus-list-identifiers)))
5507
5508 (defun gnus-summary-remove-list-identifiers ()
5509   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5510   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5511         changed subject)
5512     (when regexp
5513       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5514       (dolist (header gnus-newsgroup-headers)
5515         (setq subject (mail-header-subject header)
5516               changed nil)
5517         (while (string-match regexp subject)
5518           (setq subject
5519                 (concat (substring subject 0 (match-beginning 1))
5520                         (substring subject (match-end 0)))
5521                 changed t))
5522         (when changed
5523           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5524             (setq subject
5525                   (concat (substring subject 0 (match-beginning 1))
5526                           (substring subject (match-end 1)))))
5527           (mail-header-set-subject header subject))))))
5528
5529 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5530   "Fetch headers of ARTICLES."
5531   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5532     (gnus-message 7 "Fetching headers for %s..." name)
5533     (prog1
5534         (if (eq 'nov
5535                 (setq gnus-headers-retrieved-by
5536                       (gnus-retrieve-headers
5537                        articles gnus-newsgroup-name
5538                        (or limit
5539                            ;; We might want to fetch old headers, but
5540                            ;; not if there is only 1 article.
5541                            (and (or (and
5542                                      (not (eq gnus-fetch-old-headers 'some))
5543                                      (not (numberp gnus-fetch-old-headers)))
5544                                     (> (length articles) 1))
5545                                 gnus-fetch-old-headers)))))
5546             (gnus-get-newsgroup-headers-xover
5547              articles force-new dependencies gnus-newsgroup-name t)
5548           (gnus-get-newsgroup-headers dependencies force-new))
5549       (gnus-message 7 "Fetching headers for %s...done" name))))
5550
5551 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5552   "Select newsgroup GROUP.
5553 If READ-ALL is non-nil, all articles in the group are selected.
5554 If SELECT-ARTICLES, only select those articles from GROUP."
5555   (let* ((entry (gnus-group-entry group))
5556          ;;!!! Dirty hack; should be removed.
5557          (gnus-summary-ignore-duplicates
5558           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5559               t
5560             gnus-summary-ignore-duplicates))
5561          (info (nth 2 entry))
5562          charset articles fetched-articles cached)
5563
5564     (unless (gnus-check-server
5565              (set (make-local-variable 'gnus-current-select-method)
5566                   (gnus-find-method-for-group group)))
5567       (error "Couldn't open server"))
5568     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5569
5570     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5571         (gnus-activate-group group)     ; Or we can activate it...
5572         (progn                          ; Or we bug out.
5573           (when (equal major-mode 'gnus-summary-mode)
5574             (gnus-kill-buffer (current-buffer)))
5575           (error
5576            "Couldn't activate group %s: %s"
5577            (mm-decode-coding-string group charset)
5578            (mm-decode-coding-string (gnus-status-message group) charset))))
5579
5580     (unless (gnus-request-group group t)
5581       (when (equal major-mode 'gnus-summary-mode)
5582         (gnus-kill-buffer (current-buffer)))
5583       (error "Couldn't request group %s: %s"
5584              (mm-decode-coding-string group charset)
5585              (mm-decode-coding-string (gnus-status-message group) charset)))
5586
5587     (when (and gnus-agent
5588                (gnus-active group))
5589       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5590
5591       (setq gnus-summary-use-undownloaded-faces
5592             (gnus-agent-find-parameter
5593              group
5594              'agent-enable-undownloaded-faces)))
5595
5596     (setq gnus-newsgroup-name group
5597           gnus-newsgroup-unselected nil
5598           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5599
5600     (let ((display (gnus-group-find-parameter group 'display)))
5601       (setq gnus-newsgroup-display
5602             (cond
5603              ((not (zerop (or (car-safe read-all) 0)))
5604               ;; The user entered the group with C-u SPC/RET, let's show
5605               ;; all articles.
5606               'gnus-not-ignore)
5607              ((eq display 'all)
5608               'gnus-not-ignore)
5609              ((arrayp display)
5610               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5611              ((numberp display)
5612               ;; The following is probably the "correct" solution, but
5613               ;; it makes Gnus fetch all headers and then limit the
5614               ;; articles (which is slow), so instead we hack the
5615               ;; select-articles parameter instead. -- Simon Josefsson
5616               ;; <jas@kth.se>
5617               ;;
5618               ;; (gnus-byte-compile
5619               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5620               ;;                         display)))))
5621               (setq select-articles
5622                     (gnus-uncompress-range
5623                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5624                              (if (> tmp 0)
5625                                  tmp
5626                                1))
5627                            (cdr (gnus-active group)))))
5628               nil)
5629              (t
5630               nil))))
5631
5632     (gnus-summary-setup-default-charset)
5633
5634     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5635     (when (gnus-virtual-group-p group)
5636       (setq cached gnus-newsgroup-cached))
5637
5638     (setq gnus-newsgroup-unreads
5639           (gnus-sorted-ndifference
5640            (gnus-sorted-ndifference gnus-newsgroup-unreads
5641                                     gnus-newsgroup-marked)
5642            gnus-newsgroup-dormant))
5643
5644     (setq gnus-newsgroup-processable nil)
5645
5646     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5647
5648     ;; Adjust and set lists of article marks.
5649     (when info
5650       (gnus-adjust-marked-articles info))
5651     (if (setq articles select-articles)
5652         (setq gnus-newsgroup-unselected
5653               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5654       (setq articles (gnus-articles-to-read group read-all)))
5655
5656     (cond
5657      ((null articles)
5658       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5659       'quit)
5660      ((eq articles 0) nil)
5661      (t
5662       ;; Init the dependencies hash table.
5663       (setq gnus-newsgroup-dependencies
5664             (gnus-make-hashtable (length articles)))
5665       (if (gnus-buffer-live-p gnus-group-buffer)
5666           (gnus-set-global-variables)
5667         (set-default 'gnus-newsgroup-name gnus-newsgroup-name))
5668       ;; Retrieve the headers and read them in.
5669
5670       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5671
5672       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5673       (when cached
5674         (setq gnus-newsgroup-cached cached))
5675
5676       ;; Suppress duplicates?
5677       (when gnus-suppress-duplicates
5678         (gnus-dup-suppress-articles))
5679
5680       ;; Set the initial limit.
5681       (setq gnus-newsgroup-limit (copy-sequence articles))
5682       ;; Remove canceled articles from the list of unread articles.
5683       (setq fetched-articles
5684             (mapcar (lambda (headers) (mail-header-number headers))
5685                     gnus-newsgroup-headers))
5686       (setq gnus-newsgroup-articles fetched-articles)
5687       (setq gnus-newsgroup-unreads
5688             (gnus-sorted-nintersection
5689              gnus-newsgroup-unreads fetched-articles))
5690       (gnus-compute-unseen-list)
5691
5692       ;; Removed marked articles that do not exist.
5693       (gnus-update-missing-marks
5694        (gnus-sorted-difference articles fetched-articles))
5695       ;; We might want to build some more threads first.
5696       (when (and gnus-fetch-old-headers
5697                  (eq gnus-headers-retrieved-by 'nov))
5698         (if (eq gnus-fetch-old-headers 'invisible)
5699             (gnus-build-all-threads)
5700           (gnus-build-old-threads)))
5701       ;; Let the Gnus agent mark articles as read.
5702       (when gnus-agent
5703         (gnus-agent-get-undownloaded-list))
5704       ;; Remove list identifiers from subject
5705       (gnus-summary-remove-list-identifiers)
5706       ;; Check whether auto-expire is to be done in this group.
5707       (setq gnus-newsgroup-auto-expire
5708             (and (gnus-group-auto-expirable-p group)
5709                  (not (gnus-group-read-only-p group))))
5710       ;; Set up the article buffer now, if necessary.
5711       (unless (and gnus-single-article-buffer
5712                    (equal gnus-article-buffer "*Article*"))
5713         (gnus-article-setup-buffer))
5714       ;; First and last article in this newsgroup.
5715       (when gnus-newsgroup-headers
5716         (setq gnus-newsgroup-begin
5717               (mail-header-number (car gnus-newsgroup-headers))
5718               gnus-newsgroup-end
5719               (mail-header-number
5720                (gnus-last-element gnus-newsgroup-headers))))
5721       ;; GROUP is successfully selected.
5722       (or gnus-newsgroup-headers t)))))
5723
5724 (defun gnus-compute-unseen-list ()
5725   ;; The `seen' marks are treated specially.
5726   (if (not gnus-newsgroup-seen)
5727       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5728     (setq gnus-newsgroup-unseen
5729           (gnus-inverse-list-range-intersection
5730            gnus-newsgroup-articles gnus-newsgroup-seen))))
5731
5732 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5733
5734 (defun gnus-summary-display-make-predicate (display)
5735   (require 'gnus-agent)
5736   (when (= (length display) 1)
5737     (setq display (car display)))
5738   (unless gnus-summary-display-cache
5739     (dolist (elem (append '((unread . unread)
5740                             (read . read)
5741                             (unseen . unseen))
5742                           gnus-article-mark-lists))
5743       (push (cons (cdr elem)
5744                   (gnus-byte-compile    ;Why bother?
5745                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5746             gnus-summary-display-cache)))
5747   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5748         (gnus-category-predicate-cache gnus-summary-display-cache))
5749     (gnus-get-predicate display)))
5750
5751 ;; Uses the dynamically bound `gnus-number' variable.
5752 (defvar gnus-number)
5753 (defun gnus-article-marked-p (type &optional article)
5754   (let ((article (or article gnus-number)))
5755     (cond
5756      ((eq type 'tick)
5757       (memq article gnus-newsgroup-marked))
5758      ((eq type 'spam)
5759       (memq article gnus-newsgroup-spam-marked))
5760      ((eq type 'unsend)
5761       (memq article gnus-newsgroup-unsendable))
5762      ((eq type 'undownload)
5763       (memq article gnus-newsgroup-undownloaded))
5764      ((eq type 'download)
5765       (memq article gnus-newsgroup-downloadable))
5766      ((eq type 'unread)
5767       (memq article gnus-newsgroup-unreads))
5768      ((eq type 'read)
5769       (memq article gnus-newsgroup-reads))
5770      ((eq type 'dormant)
5771       (memq article gnus-newsgroup-dormant) )
5772      ((eq type 'expire)
5773       (memq article gnus-newsgroup-expirable))
5774      ((eq type 'reply)
5775       (memq article gnus-newsgroup-replied))
5776      ((eq type 'killed)
5777       (memq article gnus-newsgroup-killed))
5778      ((eq type 'bookmark)
5779       (assq article gnus-newsgroup-bookmarks))
5780      ((eq type 'score)
5781       (assq article gnus-newsgroup-scored))
5782      ((eq type 'save)
5783       (memq article gnus-newsgroup-saved))
5784      ((eq type 'cache)
5785       (memq article gnus-newsgroup-cached))
5786      ((eq type 'forward)
5787       (memq article gnus-newsgroup-forwarded))
5788      ((eq type 'seen)
5789       (not (memq article gnus-newsgroup-unseen)))
5790      (t t))))
5791
5792 (defun gnus-articles-to-read (group &optional read-all)
5793   "Find out what articles the user wants to read."
5794   (let* ((only-read-p t)
5795          (articles
5796           (gnus-list-range-difference
5797           ;; Select all articles if `read-all' is non-nil, or if there
5798           ;; are no unread articles.
5799           (if (or read-all
5800                   (and (zerop (length gnus-newsgroup-marked))
5801                        (zerop (length gnus-newsgroup-unreads)))
5802                   ;; Fetch all if the predicate is non-nil.
5803                   gnus-newsgroup-display)
5804               ;; We want to select the headers for all the articles in
5805               ;; the group, so we select either all the active
5806               ;; articles in the group, or (if that's nil), the
5807               ;; articles in the cache.
5808               (or
5809                (if gnus-newsgroup-maximum-articles
5810                    (let ((active (gnus-active group)))
5811                      (gnus-uncompress-range
5812                       (cons (max (car active)
5813                                  (- (cdr active)
5814                                     gnus-newsgroup-maximum-articles
5815                                     -1))
5816                             (cdr active))))
5817                  (gnus-uncompress-range (gnus-active group)))
5818                (gnus-cache-articles-in-group group))
5819             ;; Select only the "normal" subset of articles.
5820             (setq only-read-p nil)
5821             (gnus-sorted-nunion
5822              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5823              gnus-newsgroup-unreads))
5824           (cdr (assq 'unexist (gnus-info-marks (gnus-get-info group))))))
5825          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5826          (scored (length scored-list))
5827          (number (length articles))
5828          (marked (+ (length gnus-newsgroup-marked)
5829                     (length gnus-newsgroup-dormant)))
5830          (select
5831           (cond
5832            ((numberp read-all)
5833             read-all)
5834            ((numberp gnus-newsgroup-display)
5835             gnus-newsgroup-display)
5836            (t
5837             (condition-case ()
5838                 (cond
5839                  ((and (or (<= scored marked) (= scored number))
5840                        (numberp gnus-large-newsgroup)
5841                        (> number gnus-large-newsgroup))
5842                   (let* ((cursor-in-echo-area nil)
5843                          (initial (gnus-parameter-large-newsgroup-initial
5844                                    gnus-newsgroup-name))
5845                          (default (if only-read-p
5846                                       (or initial gnus-large-newsgroup)
5847                                     number))
5848                          (input
5849                           (read-string
5850                            (if only-read-p
5851                                (format
5852                                 "How many articles from %s (available %d, default %d): "
5853                                 (gnus-group-decoded-name
5854                                  (gnus-group-real-name gnus-newsgroup-name))
5855                                 number default)
5856                              (format
5857                               "How many articles from %s (%d default): "
5858                               (gnus-group-decoded-name
5859                                (gnus-group-real-name gnus-newsgroup-name))
5860                               default))
5861                            nil
5862                            nil
5863                            (number-to-string default))))
5864                     (if (string-match "^[ \t]*$" input) number input)))
5865                  ((and (> scored marked) (< scored number)
5866                        (> (- scored number) 20))
5867                   (let ((input
5868                          (read-string
5869                           (format "%s %s (%d scored, %d total): "
5870                                   "How many articles from"
5871                                   (gnus-group-decoded-name
5872                                    (gnus-group-real-name gnus-newsgroup-name))
5873                                   scored number))))
5874                     (if (string-match "^[ \t]*$" input)
5875                         number input)))
5876                  (t number))
5877               (quit
5878                (message "Quit getting the articles to read")
5879                nil))))))
5880     (setq select (if (stringp select) (string-to-number select) select))
5881     (if (or (null select) (zerop select))
5882         select
5883       (if (and (not (zerop scored)) (<= (abs select) scored))
5884           (progn
5885             (setq articles (sort scored-list '<))
5886             (setq number (length articles)))
5887         (setq articles (copy-sequence articles)))
5888
5889       (when (< (abs select) number)
5890         (if (< select 0)
5891             ;; Select the N oldest articles.
5892             (setcdr (nthcdr (1- (abs select)) articles) nil)
5893           ;; Select the N most recent articles.
5894           (setq articles (nthcdr (- number select) articles))))
5895       (setq gnus-newsgroup-unselected
5896             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5897       (when gnus-alter-articles-to-read-function
5898         (setq articles
5899               (sort
5900                (funcall gnus-alter-articles-to-read-function
5901                         gnus-newsgroup-name articles)
5902                '<)))
5903       articles)))
5904
5905 (defun gnus-killed-articles (killed articles)
5906   (let (out)
5907     (while articles
5908       (when (inline (gnus-member-of-range (car articles) killed))
5909         (push (car articles) out))
5910       (setq articles (cdr articles)))
5911     out))
5912
5913 (defun gnus-article-mark-to-type (mark)
5914   "Return the type of MARK."
5915   (or (cadr (assq mark gnus-article-special-mark-lists))
5916       'list))
5917
5918 (defun gnus-article-unpropagatable-p (mark)
5919   "Return whether MARK should be propagated to back end."
5920   (memq mark gnus-article-unpropagated-mark-lists))
5921
5922 (defun gnus-adjust-marked-articles (info)
5923   "Set all article lists and remove all marks that are no longer valid."
5924   (let* ((marked-lists (gnus-info-marks info))
5925          (active (gnus-active (gnus-info-group info)))
5926          (min (car active))
5927          (max (cdr active))
5928          (types gnus-article-mark-lists)
5929          marks var articles article mark mark-type
5930          bgn end)
5931     ;; Hack to avoid adjusting marks for imap.
5932     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5933               'nnimap)
5934       (setq min 1))
5935
5936     (dolist (marks marked-lists)
5937       (setq mark (car marks)
5938             mark-type (gnus-article-mark-to-type mark)
5939             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5940       ;; We set the variable according to the type of the marks list,
5941       ;; and then adjust the marks to a subset of the active articles.
5942       (cond
5943        ;; Adjust "simple" lists - compressed yet unsorted
5944        ((eq mark-type 'list)
5945         ;; Simultaneously uncompress and clip to active range
5946         ;; See gnus-uncompress-range for a description of possible marks
5947         (let (l lh)
5948           (if (not (cadr marks))
5949               (set var nil)
5950             (setq articles (if (numberp (cddr marks))
5951                                (list (cdr marks))
5952                              (cdr marks))
5953                   lh (cons nil nil)
5954                   l lh)
5955
5956             (while (setq article (pop articles))
5957               (cond ((consp article)
5958                      (setq bgn (max (car article) min)
5959                            end (min (cdr article) max))
5960                      (while (<= bgn end)
5961                        (setq l (setcdr l (cons bgn nil))
5962                              bgn (1+ bgn))))
5963                     ((and (<= min article)
5964                           (>= max article))
5965                      (setq l (setcdr l (cons article nil))))))
5966             (set var (cdr lh)))))
5967        ;; Adjust assocs.
5968        ((eq mark-type 'tuple)
5969         (set var (setq articles (cdr marks)))
5970         (when (not (listp (cdr (symbol-value var))))
5971           (set var (list (symbol-value var))))
5972         (when (not (listp (cdr articles)))
5973           (setq articles (list articles)))
5974         (while articles
5975           (when (or (not (consp (setq article (pop articles))))
5976                     (< (car article) min)
5977                     (> (car article) max))
5978             (set var (delq article (symbol-value var))))))
5979        ;; Adjust ranges (sloppily).
5980        ((eq mark-type 'range)
5981         (cond
5982          ((eq mark 'seen)
5983           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5984           ;; It should be (seen (NUM1 . NUM2)).
5985           (when (numberp (cddr marks))
5986             (setcdr marks (list (cdr marks))))
5987           (setq articles (cdr marks))
5988           (while (and articles
5989                       (or (and (consp (car articles))
5990                                (> min (cdar articles)))
5991                           (and (numberp (car articles))
5992                                (> min (car articles)))))
5993             (pop articles))
5994           (set var articles))
5995          ((eq mark 'unexist)
5996           (set var (cdr marks)))))))))
5997
5998 (defun gnus-update-missing-marks (missing)
5999   "Go through the list of MISSING articles and remove them from the mark lists."
6000   (when missing
6001     (let (var m)
6002       ;; Go through all types.
6003       (dolist (elem gnus-article-mark-lists)
6004         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6005           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6006           (when (symbol-value var)
6007             ;; This list has articles.  So we delete all missing
6008             ;; articles from it.
6009             (setq m missing)
6010             (while m
6011               (set var (delq (pop m) (symbol-value var))))))))))
6012
6013 (defun gnus-update-marks ()
6014   "Enter the various lists of marked articles into the newsgroup info list."
6015   (let ((types gnus-article-mark-lists)
6016         (info (gnus-get-info gnus-newsgroup-name))
6017         type list newmarked symbol delta-marks)
6018     (when info
6019       ;; Add all marks lists to the list of marks lists.
6020       (while (setq type (pop types))
6021         (setq list (symbol-value
6022                     (setq symbol
6023                           (intern (format "gnus-newsgroup-%s" (car type))))))
6024
6025         (when list
6026           ;; Get rid of the entries of the articles that have the
6027           ;; default score.
6028           (when (and (eq (cdr type) 'score)
6029                      gnus-save-score
6030                      list)
6031             (let* ((arts list)
6032                    (prev (cons nil list))
6033                    (all prev))
6034               (while arts
6035                 (if (or (not (consp (car arts)))
6036                         (= (cdar arts) gnus-summary-default-score))
6037                     (setcdr prev (cdr arts))
6038                   (setq prev arts))
6039                 (setq arts (cdr arts)))
6040               (setq list (cdr all)))))
6041
6042         (when (eq (cdr type) 'seen)
6043           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6044
6045         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6046           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6047
6048         (when (and (gnus-check-backend-function
6049                     'request-set-mark gnus-newsgroup-name)
6050                    (not (gnus-article-unpropagatable-p (cdr type))))
6051           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6052                  ;; Don't do anything about marks for articles we
6053                  ;; didn't actually get any headers for.
6054                  (del
6055                   (gnus-list-range-intersection
6056                    gnus-newsgroup-articles
6057                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6058                  (add
6059                   (gnus-list-range-intersection
6060                    gnus-newsgroup-articles
6061                    (gnus-remove-from-range
6062                     (gnus-copy-sequence list) old))))
6063             (when add
6064               (push (list add 'add (list (cdr type))) delta-marks))
6065             (when del
6066               ;; Don't delete marks from outside the active range.
6067               ;; This shouldn't happen, but is a sanity check.
6068               (setq del (gnus-sorted-range-intersection
6069                          (gnus-active gnus-newsgroup-name) del))
6070               (push (list del 'del (list (cdr type))) delta-marks))))
6071
6072         (when list
6073           (push (cons (cdr type) list) newmarked)))
6074
6075       (when delta-marks
6076         (unless (gnus-check-group gnus-newsgroup-name)
6077           (error "Can't open server for %s" gnus-newsgroup-name))
6078         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6079
6080       ;; Enter these new marks into the info of the group.
6081       (if (nthcdr 3 info)
6082           (setcar (nthcdr 3 info) newmarked)
6083         ;; Add the marks lists to the end of the info.
6084         (when newmarked
6085           (setcdr (nthcdr 2 info) (list newmarked))))
6086
6087       ;; Cut off the end of the info if there's nothing else there.
6088       (let ((i 5))
6089         (while (and (> i 2)
6090                     (not (nth i info)))
6091           (when (nthcdr (decf i) info)
6092             (setcdr (nthcdr i info) nil)))))))
6093
6094 (defun gnus-set-mode-line (where)
6095   "Set the mode line of the article or summary buffers.
6096 If WHERE is `summary', the summary mode line format will be used."
6097   ;; Is this mode line one we keep updated?
6098   (when (and (memq where gnus-updated-mode-lines)
6099              (symbol-value
6100               (intern (format "gnus-%s-mode-line-format-spec" where))))
6101     (let (mode-string)
6102       ;; We evaluate this in the summary buffer since these
6103       ;; variables are buffer-local to that buffer.
6104       (with-current-buffer gnus-summary-buffer
6105         ;; We bind all these variables that are used in the `eval' form
6106         ;; below.
6107         (let* ((mformat (symbol-value
6108                          (intern
6109                           (format "gnus-%s-mode-line-format-spec" where))))
6110                (gnus-tmp-group-name (gnus-mode-string-quote
6111                                      (gnus-group-decoded-name
6112                                       gnus-newsgroup-name)))
6113                (gnus-tmp-article-number (or gnus-current-article 0))
6114                (gnus-tmp-unread gnus-newsgroup-unreads)
6115                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6116                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6117                (gnus-tmp-unread-and-unselected
6118                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6119                             (zerop gnus-tmp-unselected))
6120                        "")
6121                       ((zerop gnus-tmp-unselected)
6122                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6123                       (t (format "{%d(+%d) more}"
6124                                  gnus-tmp-unread-and-unticked
6125                                  gnus-tmp-unselected))))
6126                (gnus-tmp-subject
6127                 (if (and gnus-current-headers
6128                          (vectorp gnus-current-headers))
6129                     (gnus-mode-string-quote
6130                      (mail-header-subject gnus-current-headers))
6131                   ""))
6132                bufname-length max-len
6133                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6134           (setq mode-string (eval mformat))
6135           (setq bufname-length (if (string-match "%b" mode-string)
6136                                    (- (length
6137                                        (buffer-name
6138                                         (if (eq where 'summary)
6139                                             nil
6140                                           (get-buffer gnus-article-buffer))))
6141                                       2)
6142                                  0))
6143           (setq max-len (max 4 (if gnus-mode-non-string-length
6144                                    (- (window-width)
6145                                       gnus-mode-non-string-length
6146                                       bufname-length)
6147                                  (length mode-string))))
6148           ;; We might have to chop a bit of the string off...
6149           (when (> (length mode-string) max-len)
6150             (setq mode-string
6151                   (concat (truncate-string-to-width mode-string (- max-len 3))
6152                           "...")))))
6153       ;; Update the mode line.
6154       (setq mode-line-buffer-identification
6155             (gnus-mode-line-buffer-identification (list mode-string)))
6156       (set-buffer-modified-p t))))
6157
6158 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6159   "Go through the HEADERS list and add all Xrefs to a hash table.
6160 The resulting hash table is returned, or nil if no Xrefs were found."
6161   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6162          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6163          (xref-hashtb (gnus-make-hashtable))
6164          start group entry number xrefs header)
6165     (while headers
6166       (setq header (pop headers))
6167       (when (and (setq xrefs (mail-header-xref header))
6168                  (not (memq (setq number (mail-header-number header))
6169                             unreads)))
6170         (setq start 0)
6171         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6172           (setq start (match-end 0))
6173           (setq group (if prefix
6174                           (concat prefix (substring xrefs (match-beginning 1)
6175                                                     (match-end 1)))
6176                         (substring xrefs (match-beginning 1) (match-end 1))))
6177           (setq number
6178                 (string-to-number (substring xrefs (match-beginning 2)
6179                                           (match-end 2))))
6180           (if (setq entry (gnus-gethash group xref-hashtb))
6181               (setcdr entry (cons number (cdr entry)))
6182             (gnus-sethash group (cons number nil) xref-hashtb)))))
6183     (and start xref-hashtb)))
6184
6185 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6186   "Look through all the headers and mark the Xrefs as read."
6187   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6188         name info xref-hashtb idlist method nth4)
6189     (with-current-buffer gnus-group-buffer
6190       (when (setq xref-hashtb
6191                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6192         (mapatoms
6193          (lambda (group)
6194            (unless (string= from-newsgroup (setq name (symbol-name group)))
6195              (setq idlist (symbol-value group))
6196              ;; Dead groups are not updated.
6197              (and (prog1
6198                       (setq info (gnus-get-info name))
6199                     (when (stringp (setq nth4 (gnus-info-method info)))
6200                       (setq nth4 (gnus-server-to-method nth4))))
6201                   ;; Only do the xrefs if the group has the same
6202                   ;; select method as the group we have just read.
6203                   (or (gnus-methods-equal-p
6204                        nth4 (gnus-find-method-for-group from-newsgroup))
6205                       virtual
6206                       (equal nth4 (setq method (gnus-find-method-for-group
6207                                                 from-newsgroup)))
6208                       (and (equal (car nth4) (car method))
6209                            (equal (nth 1 nth4) (nth 1 method))))
6210                   gnus-use-cross-reference
6211                   (or (not (eq gnus-use-cross-reference t))
6212                       virtual
6213                       ;; Only do cross-references on subscribed
6214                       ;; groups, if that is what is wanted.
6215                       (<= (gnus-info-level info) gnus-level-subscribed))
6216                   (gnus-group-make-articles-read name idlist))))
6217          xref-hashtb)))))
6218
6219 (defun gnus-compute-read-articles (group articles)
6220   (let* ((entry (gnus-group-entry group))
6221          (info (nth 2 entry))
6222          (active (gnus-active group))
6223          ninfo)
6224     (when entry
6225       ;; First peel off all invalid article numbers.
6226       (when active
6227         (let ((ids articles)
6228               id first)
6229           (while (setq id (pop ids))
6230             (when (and first (> id (cdr active)))
6231               ;; We'll end up in this situation in one particular
6232               ;; obscure situation.  If you re-scan a group and get
6233               ;; a new article that is cross-posted to a different
6234               ;; group that has not been re-scanned, you might get
6235               ;; crossposted article that has a higher number than
6236               ;; Gnus believes possible.  So we re-activate this
6237               ;; group as well.  This might mean doing the
6238               ;; crossposting thingy will *increase* the number
6239               ;; of articles in some groups.  Tsk, tsk.
6240               (setq active (or (gnus-activate-group group) active)))
6241             (when (or (> id (cdr active))
6242                       (< id (car active)))
6243               (setq articles (delq id articles))))))
6244       ;; If the read list is nil, we init it.
6245       (if (and active
6246                (null (gnus-info-read info))
6247                (> (car active) 1))
6248           (setq ninfo (cons 1 (1- (car active))))
6249         (setq ninfo (gnus-info-read info)))
6250       ;; Then we add the read articles to the range.
6251       (gnus-add-to-range
6252        ninfo (setq articles (sort articles '<))))))
6253
6254 (defun gnus-group-make-articles-read (group articles)
6255   "Update the info of GROUP to say that ARTICLES are read."
6256   (let* ((num 0)
6257          (entry (gnus-group-entry group))
6258          (info (nth 2 entry))
6259          (active (gnus-active group))
6260          (set-marks
6261           (gnus-method-option-p
6262            (gnus-find-method-for-group group)
6263            'server-marks))
6264          range)
6265     (if (not entry)
6266         ;; Group that Gnus doesn't know exists, but still allow the
6267         ;; backend to set marks.
6268         (when set-marks
6269           (gnus-request-set-mark
6270            group (list (list (gnus-compress-sequence (sort articles #'<))
6271                              'add '(read)))))
6272       ;; Normal, subscribed groups.
6273       (setq range (gnus-compute-read-articles group articles))
6274       (with-current-buffer gnus-group-buffer
6275         (gnus-undo-register
6276           `(progn
6277              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6278              (gnus-info-set-read ',info ',(gnus-info-read info))
6279              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6280              (when ,set-marks
6281                (gnus-request-set-mark
6282                 ,group (list (list ',range 'del '(read)))))
6283              (gnus-group-update-group ,group t))))
6284       ;; Add the read articles to the range.
6285       (gnus-info-set-read info range)
6286       (when set-marks
6287         (gnus-request-set-mark group (list (list range 'add '(read)))))
6288       ;; Then we have to re-compute how many unread
6289       ;; articles there are in this group.
6290       (when active
6291         (cond
6292          ((not range)
6293           (setq num (- (1+ (cdr active)) (car active))))
6294          ((not (listp (cdr range)))
6295           (setq num (- (cdr active) (- (1+ (cdr range))
6296                                        (car range)))))
6297          (t
6298           (while range
6299             (if (numberp (car range))
6300                 (setq num (1+ num))
6301               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6302             (setq range (cdr range)))
6303           (setq num (- (cdr active) num))))
6304         ;; Update the number of unread articles.
6305         (setcar entry num)
6306         ;; Update the group buffer.
6307         (unless (gnus-ephemeral-group-p group)
6308           (gnus-group-update-group group t))))))
6309
6310 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6311   (let ((cur nntp-server-buffer)
6312         (dependencies
6313          (or dependencies
6314              (with-current-buffer gnus-summary-buffer
6315                gnus-newsgroup-dependencies)))
6316         headers id end ref number
6317         (mail-parse-charset gnus-newsgroup-charset)
6318         (mail-parse-ignored-charsets
6319          (save-current-buffer (condition-case nil
6320                                   (set-buffer gnus-summary-buffer)
6321                                 (error))
6322                               gnus-newsgroup-ignored-charsets)))
6323     (with-current-buffer nntp-server-buffer
6324       ;; Translate all TAB characters into SPACE characters.
6325       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6326       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6327       (ietf-drums-unfold-fws)
6328       (gnus-run-hooks 'gnus-parse-headers-hook)
6329       (let ((case-fold-search t)
6330             in-reply-to header p lines chars)
6331         (goto-char (point-min))
6332         ;; Search to the beginning of the next header.  Error messages
6333         ;; do not begin with 2 or 3.
6334         (while (re-search-forward "^[23][0-9]+ " nil t)
6335           (setq id nil
6336                 ref nil)
6337           ;; This implementation of this function, with nine
6338           ;; search-forwards instead of the one re-search-forward and
6339           ;; a case (which basically was the old function) is actually
6340           ;; about twice as fast, even though it looks messier.  You
6341           ;; can't have everything, I guess.  Speed and elegance
6342           ;; doesn't always go hand in hand.
6343           (setq
6344            header
6345            (vector
6346             ;; Number.
6347             (prog1
6348                 (setq number (read cur))
6349               (end-of-line)
6350               (setq p (point))
6351               (narrow-to-region (point)
6352                                 (or (and (search-forward "\n.\n" nil t)
6353                                          (- (point) 2))
6354                                     (point))))
6355             ;; Subject.
6356             (progn
6357               (goto-char p)
6358               (if (search-forward "\nsubject:" nil t)
6359                   (funcall gnus-decode-encoded-word-function
6360                            (nnheader-header-value))
6361                 "(none)"))
6362             ;; From.
6363             (progn
6364               (goto-char p)
6365               (if (search-forward "\nfrom:" nil t)
6366                   (funcall gnus-decode-encoded-address-function
6367                            (nnheader-header-value))
6368                 "(nobody)"))
6369             ;; Date.
6370             (progn
6371               (goto-char p)
6372               (if (search-forward "\ndate:" nil t)
6373                   (nnheader-header-value) ""))
6374             ;; Message-ID.
6375             (progn
6376               (goto-char p)
6377               (setq id (if (re-search-forward
6378                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6379                            ;; We do it this way to make sure the Message-ID
6380                            ;; is (somewhat) syntactically valid.
6381                            (buffer-substring (match-beginning 1)
6382                                              (match-end 1))
6383                          ;; If there was no message-id, we just fake one
6384                          ;; to make subsequent routines simpler.
6385                          (nnheader-generate-fake-message-id number))))
6386             ;; References.
6387             (progn
6388               (goto-char p)
6389               (if (search-forward "\nreferences:" nil t)
6390                   (progn
6391                     (setq end (point))
6392                     (prog1
6393                         (nnheader-header-value)
6394                       (setq ref
6395                             (buffer-substring
6396                              (progn
6397                                (end-of-line)
6398                                (search-backward ">" end t)
6399                                (1+ (point)))
6400                              (progn
6401                                (search-backward "<" end t)
6402                                (point))))))
6403                 ;; Get the references from the in-reply-to header if there
6404                 ;; were no references and the in-reply-to header looks
6405                 ;; promising.
6406                 (if (and (search-forward "\nin-reply-to:" nil t)
6407                          (setq in-reply-to (nnheader-header-value))
6408                          (string-match "<[^>]+>" in-reply-to))
6409                     (let (ref2)
6410                       (setq ref (substring in-reply-to (match-beginning 0)
6411                                            (match-end 0)))
6412                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6413                         (setq ref2 (substring in-reply-to (match-beginning 0)
6414                                               (match-end 0)))
6415                         (when (> (length ref2) (length ref))
6416                           (setq ref ref2)))
6417                       ref)
6418                   (setq ref nil))))
6419             ;; Chars.
6420             (progn
6421               (goto-char p)
6422               (if (search-forward "\nchars: " nil t)
6423                   (if (numberp (setq chars (ignore-errors (read cur))))
6424                       chars -1)
6425                 -1))
6426             ;; Lines.
6427             (progn
6428               (goto-char p)
6429               (if (search-forward "\nlines: " nil t)
6430                   (if (numberp (setq lines (ignore-errors (read cur))))
6431                       lines -1)
6432                 -1))
6433             ;; Xref.
6434             (progn
6435               (goto-char p)
6436               (and (search-forward "\nxref:" nil t)
6437                    (nnheader-header-value)))
6438             ;; Extra.
6439             (when gnus-extra-headers
6440               (let ((extra gnus-extra-headers)
6441                     out)
6442                 (while extra
6443                   (goto-char p)
6444                   (when (search-forward
6445                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6446                     (push (cons (car extra) (nnheader-header-value))
6447                           out))
6448                   (pop extra))
6449                 out))))
6450           (when (equal id ref)
6451             (setq ref nil))
6452
6453           (when gnus-alter-header-function
6454             (funcall gnus-alter-header-function header)
6455             (setq id (mail-header-id header)
6456                   ref (gnus-parent-id (mail-header-references header))))
6457
6458           (when (setq header
6459                       (gnus-dependencies-add-header
6460                        header dependencies force-new))
6461             (push header headers))
6462           (goto-char (point-max))
6463           (widen))
6464         (nreverse headers)))))
6465
6466 ;; Goes through the xover lines and returns a list of vectors
6467 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6468                                                   force-new dependencies
6469                                                   group also-fetch-heads)
6470   "Parse the news overview data in the server buffer.
6471 Return a list of headers that match SEQUENCE (see
6472 `nntp-retrieve-headers')."
6473   ;; Get the Xref when the users reads the articles since most/some
6474   ;; NNTP servers do not include Xrefs when using XOVER.
6475   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6476   (let ((mail-parse-charset gnus-newsgroup-charset)
6477         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6478         (cur nntp-server-buffer)
6479         (dependencies (or dependencies gnus-newsgroup-dependencies))
6480         (allp (cond
6481                ((eq gnus-read-all-available-headers t)
6482                 t)
6483                ((and (stringp gnus-read-all-available-headers)
6484                      group)
6485                 (string-match gnus-read-all-available-headers group))
6486                (t
6487                 nil)))
6488         number headers header)
6489     (with-current-buffer nntp-server-buffer
6490       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6491       ;; Allow the user to mangle the headers before parsing them.
6492       (gnus-run-hooks 'gnus-parse-headers-hook)
6493       (goto-char (point-min))
6494       (gnus-parse-without-error
6495         (while (and (or sequence allp)
6496                     (not (eobp)))
6497           (setq number (read cur))
6498           (when (not allp)
6499             (while (and sequence
6500                         (< (car sequence) number))
6501               (setq sequence (cdr sequence))))
6502           (when (and (or allp
6503                          (and sequence
6504                               (eq number (car sequence))))
6505                      (progn
6506                        (setq sequence (cdr sequence))
6507                        (setq header (inline
6508                                       (gnus-nov-parse-line
6509                                        number dependencies force-new)))))
6510             (push header headers))
6511           (forward-line 1)))
6512       ;; A common bug in inn is that if you have posted an article and
6513       ;; then retrieves the active file, it will answer correctly --
6514       ;; the new article is included.  However, a NOV entry for the
6515       ;; article may not have been generated yet, so this may fail.
6516       ;; We work around this problem by retrieving the last few
6517       ;; headers using HEAD.
6518       (if (or (not also-fetch-heads)
6519               (not sequence))
6520           ;; We (probably) got all the headers.
6521           (nreverse headers)
6522         (let ((gnus-nov-is-evil t))
6523           (nconc
6524            (nreverse headers)
6525            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6526              (gnus-get-newsgroup-headers))))))))
6527
6528 (defun gnus-article-get-xrefs ()
6529   "Fill in the Xref value in `gnus-current-headers', if necessary.
6530 This is meant to be called in `gnus-article-internal-prepare-hook'."
6531   (let ((headers (with-current-buffer gnus-summary-buffer
6532                    gnus-current-headers)))
6533     (or (not gnus-use-cross-reference)
6534         (not headers)
6535         (and (mail-header-xref headers)
6536              (not (string= (mail-header-xref headers) "")))
6537         (let ((case-fold-search t)
6538               xref)
6539           (save-restriction
6540             (nnheader-narrow-to-headers)
6541             (goto-char (point-min))
6542             (when (or (and (not (eobp))
6543                            (eq (downcase (char-after)) ?x)
6544                            (looking-at "Xref:"))
6545                       (search-forward "\nXref:" nil t))
6546               (goto-char (1+ (match-end 0)))
6547               (setq xref (buffer-substring (point) (point-at-eol)))
6548               (mail-header-set-xref headers xref)))))))
6549
6550 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6551   "Find article ID and insert the summary line for that article.
6552 OLD-HEADER can either be a header or a line number to insert
6553 the subject line on.
6554 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6555 and OLD-HEADER will be used when the summary line is inserted,
6556 too, instead of trying to fetch new headers."
6557   (let* ((line (and (numberp old-header) old-header))
6558          (old-header (and (vectorp old-header) old-header))
6559          (header (cond ((and old-header use-old-header)
6560                         old-header)
6561                        ((and (numberp id)
6562                              (gnus-number-to-header id))
6563                         (gnus-number-to-header id))
6564                        (t
6565                         (gnus-read-header id))))
6566          (number (and (numberp id) id))
6567          d)
6568     (when header
6569       ;; Rebuild the thread that this article is part of and go to the
6570       ;; article we have fetched.
6571       (when (and (not gnus-show-threads)
6572                  old-header)
6573         (when (and number
6574                    (setq d (gnus-data-find (mail-header-number old-header))))
6575           (goto-char (gnus-data-pos d))
6576           (gnus-data-remove
6577            number
6578            (- (point-at-bol)
6579               (prog1
6580                   (1+ (point-at-eol))
6581                 (gnus-delete-line))))))
6582       ;; Remove list identifiers from subject.
6583       (let ((gnus-newsgroup-headers (list header)))
6584         (gnus-summary-remove-list-identifiers))
6585       (when old-header
6586         (mail-header-set-number header (mail-header-number old-header)))
6587       (setq gnus-newsgroup-sparse
6588             (delq (setq number (mail-header-number header))
6589                   gnus-newsgroup-sparse))
6590       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6591       (push number gnus-newsgroup-limit)
6592       (gnus-rebuild-thread (mail-header-id header) line)
6593       (gnus-summary-goto-subject number nil t))
6594     (when (and (numberp number)
6595                (> number 0))
6596       ;; We have to update the boundaries even if we can't fetch the
6597       ;; article if ID is a number -- so that the next `P' or `N'
6598       ;; command will fetch the previous (or next) article even
6599       ;; if the one we tried to fetch this time has been canceled.
6600       (unless (and gnus-newsgroup-end (< number gnus-newsgroup-end))
6601         (setq gnus-newsgroup-end number))
6602       (unless (and gnus-newsgroup-begin (> number gnus-newsgroup-begin))
6603         (setq gnus-newsgroup-begin number))
6604       (setq gnus-newsgroup-unselected
6605             (delq number gnus-newsgroup-unselected)))
6606     ;; Report back a success?
6607     (and header (mail-header-number header))))
6608
6609 ;;; Process/prefix in the summary buffer
6610
6611 (defun gnus-summary-work-articles (n)
6612   "Return a list of articles to be worked upon.
6613 The prefix argument, the list of process marked articles, and the
6614 current article will be taken into consideration."
6615   (with-current-buffer gnus-summary-buffer
6616     (cond
6617      (n
6618       ;; A numerical prefix has been given.
6619       (setq n (prefix-numeric-value n))
6620       (let ((backward (< n 0))
6621             (n (abs (prefix-numeric-value n)))
6622             articles article)
6623         (save-excursion
6624           (while
6625               (and (> n 0)
6626                    (push (setq article (gnus-summary-article-number))
6627                          articles)
6628                    (if backward
6629                        (gnus-summary-find-prev nil article)
6630                      (gnus-summary-find-next nil article)))
6631             (decf n)))
6632         (nreverse articles)))
6633      ((and (gnus-region-active-p) (mark))
6634       (message "region active")
6635       ;; Work on the region between point and mark.
6636       (let ((max (max (point) (mark)))
6637             articles article)
6638         (save-excursion
6639           (goto-char (min (point) (mark)))
6640           (while
6641               (and
6642                (push (setq article (gnus-summary-article-number)) articles)
6643                (gnus-summary-find-next nil article)
6644                (< (point) max)))
6645           (nreverse articles))))
6646      (gnus-newsgroup-processable
6647       ;; There are process-marked articles present.
6648       ;; Save current state.
6649       (gnus-summary-save-process-mark)
6650       ;; Return the list.
6651       (reverse gnus-newsgroup-processable))
6652      (t
6653       ;; Just return the current article.
6654       (list (gnus-summary-article-number))))))
6655
6656 (defmacro gnus-summary-iterate (arg &rest forms)
6657   "Iterate over the process/prefixed articles and do FORMS.
6658 ARG is the interactive prefix given to the command.  FORMS will be
6659 executed with point over the summary line of the articles."
6660   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6661     `(let ((,articles (gnus-summary-work-articles ,arg)))
6662        (while ,articles
6663          (gnus-summary-goto-subject (car ,articles))
6664          ,@forms
6665          (pop ,articles)))))
6666
6667 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6668 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6669
6670 (defun gnus-summary-save-process-mark ()
6671   "Push the current set of process marked articles on the stack."
6672   (interactive)
6673   (push (copy-sequence gnus-newsgroup-processable)
6674         gnus-newsgroup-process-stack))
6675
6676 (defun gnus-summary-kill-process-mark ()
6677   "Push the current set of process marked articles on the stack and unmark."
6678   (interactive)
6679   (gnus-summary-save-process-mark)
6680   (gnus-summary-unmark-all-processable))
6681
6682 (defun gnus-summary-yank-process-mark ()
6683   "Pop the last process mark state off the stack and restore it."
6684   (interactive)
6685   (unless gnus-newsgroup-process-stack
6686     (error "Empty mark stack"))
6687   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6688
6689 (defun gnus-summary-process-mark-set (set)
6690   "Make SET into the current process marked articles."
6691   (gnus-summary-unmark-all-processable)
6692   (mapc 'gnus-summary-set-process-mark set))
6693
6694 ;;; Searching and stuff
6695
6696 (defun gnus-summary-search-group (&optional backward use-level)
6697   "Search for next unread newsgroup.
6698 If optional argument BACKWARD is non-nil, search backward instead."
6699   (with-current-buffer gnus-group-buffer
6700     (when (gnus-group-search-forward
6701            backward nil (if use-level (gnus-group-group-level) nil))
6702       (gnus-group-group-name))))
6703
6704 (defun gnus-summary-best-group (&optional exclude-group)
6705   "Find the name of the best unread group.
6706 If EXCLUDE-GROUP, do not go to this group."
6707   (with-current-buffer gnus-group-buffer
6708     (save-excursion
6709       (gnus-group-best-unread-group exclude-group))))
6710
6711 (defun gnus-summary-find-next (&optional unread article backward)
6712   (if backward
6713       (gnus-summary-find-prev unread article)
6714     (let* ((dummy (gnus-summary-article-intangible-p))
6715            (article (or article (gnus-summary-article-number)))
6716            (data (gnus-data-find-list article))
6717            result)
6718       (when (and (not dummy)
6719                  (or (not gnus-summary-check-current)
6720                      (not unread)
6721                      (not (gnus-data-unread-p (car data)))))
6722         (setq data (cdr data)))
6723       (when (setq result
6724                   (if unread
6725                       (progn
6726                         (while data
6727                           (unless (memq (gnus-data-number (car data))
6728                                         (cond
6729                                          ((eq gnus-auto-goto-ignores
6730                                               'always-undownloaded)
6731                                           gnus-newsgroup-undownloaded)
6732                                          (gnus-plugged
6733                                           nil)
6734                                          ((eq gnus-auto-goto-ignores
6735                                               'unfetched)
6736                                           gnus-newsgroup-unfetched)
6737                                          ((eq gnus-auto-goto-ignores
6738                                               'undownloaded)
6739                                           gnus-newsgroup-undownloaded)))
6740                             (when (gnus-data-unread-p (car data))
6741                               (setq result (car data)
6742                                     data nil)))
6743                           (setq data (cdr data)))
6744                         result)
6745                     (car data)))
6746         (goto-char (gnus-data-pos result))
6747         (gnus-data-number result)))))
6748
6749 (defun gnus-summary-find-prev (&optional unread article)
6750   (let* ((eobp (eobp))
6751          (article (or article (gnus-summary-article-number)))
6752          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6753          result)
6754     (when (and (not eobp)
6755                (or (not gnus-summary-check-current)
6756                    (not unread)
6757                    (not (gnus-data-unread-p (car data)))))
6758       (setq data (cdr data)))
6759     (when (setq result
6760                 (if unread
6761                     (progn
6762                       (while data
6763                         (unless (memq (gnus-data-number (car data))
6764                                       (cond
6765                                        ((eq gnus-auto-goto-ignores
6766                                             'always-undownloaded)
6767                                         gnus-newsgroup-undownloaded)
6768                                        (gnus-plugged
6769                                         nil)
6770                                        ((eq gnus-auto-goto-ignores
6771                                             'unfetched)
6772                                         gnus-newsgroup-unfetched)
6773                                        ((eq gnus-auto-goto-ignores
6774                                             'undownloaded)
6775                                         gnus-newsgroup-undownloaded)))
6776                           (when (gnus-data-unread-p (car data))
6777                             (setq result (car data)
6778                                   data nil)))
6779                         (setq data (cdr data)))
6780                       result)
6781                   (car data)))
6782       (goto-char (gnus-data-pos result))
6783       (gnus-data-number result))))
6784
6785 (defun gnus-summary-find-subject (subject &optional unread backward article)
6786   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6787          (article (or article (gnus-summary-article-number)))
6788          (articles (gnus-data-list backward))
6789          (arts (gnus-data-find-list article articles))
6790          result)
6791     (when (or (not gnus-summary-check-current)
6792               (not unread)
6793               (not (gnus-data-unread-p (car arts))))
6794       (setq arts (cdr arts)))
6795     (while arts
6796       (and (or (not unread)
6797                (gnus-data-unread-p (car arts)))
6798            (vectorp (gnus-data-header (car arts)))
6799            (gnus-subject-equal
6800             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6801            (setq result (car arts)
6802                  arts nil))
6803       (setq arts (cdr arts)))
6804     (and result
6805          (goto-char (gnus-data-pos result))
6806          (gnus-data-number result))))
6807
6808 (defun gnus-summary-search-forward (&optional unread subject backward)
6809   "Search forward for an article.
6810 If UNREAD, look for unread articles.  If SUBJECT, look for
6811 articles with that subject.  If BACKWARD, search backward instead."
6812   (cond (subject (gnus-summary-find-subject subject unread backward))
6813         (backward (gnus-summary-find-prev unread))
6814         (t (gnus-summary-find-next unread))))
6815
6816 (defun gnus-recenter (&optional n)
6817   "Center point in window and redisplay frame.
6818 Also do horizontal recentering."
6819   (interactive "P")
6820   (when (and gnus-auto-center-summary
6821              (not (eq gnus-auto-center-summary 'vertical)))
6822     (gnus-horizontal-recenter))
6823   (if (fboundp 'recenter-top-bottom)
6824       (recenter-top-bottom n)
6825     (recenter n)))
6826
6827 (put 'gnus-recenter 'isearch-scroll t)
6828
6829 (defun gnus-forward-line-ignore-invisible (n)
6830   "Move N lines forward (backward if N is negative).
6831 Like forward-line, but skip over (and don't count) invisible lines."
6832   (let (done)
6833     (while (and (> n 0) (not done))
6834       ;; If the following character is currently invisible,
6835       ;; skip all characters with that same `invisible' property value.
6836       (while (gnus-invisible-p (point))
6837         (goto-char (gnus-next-char-property-change (point))))
6838       (forward-line 1)
6839       (if (eobp)
6840           (setq done t)
6841         (setq n (1- n))))
6842     (while (and (< n 0) (not done))
6843       (forward-line -1)
6844       (if (bobp) (setq done t)
6845         (setq n (1+ n))
6846         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6847           (goto-char (gnus-previous-char-property-change (point))))))))
6848
6849 (defun gnus-summary-recenter ()
6850   "Center point in the summary window.
6851 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6852 displayed, no centering will be performed."
6853   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6854   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6855   (interactive)
6856   ;; The user has to want it.
6857   (when gnus-auto-center-summary
6858     (let* ((top (cond ((< (window-height) 4) 0)
6859                       ((< (window-height) 7) 1)
6860                       (t (if (numberp gnus-auto-center-summary)
6861                              gnus-auto-center-summary
6862                            (/ (1- (window-height)) 2)))))
6863            (height (1- (window-height)))
6864            (bottom (save-excursion
6865                      (goto-char (point-max))
6866                      (gnus-forward-line-ignore-invisible (- height))
6867                      (point)))
6868            (window (get-buffer-window (current-buffer))))
6869       (when (get-buffer-window gnus-article-buffer)
6870         ;; Only do recentering when the article buffer is displayed,
6871         ;; Set the window start to either `bottom', which is the biggest
6872         ;; possible valid number, or the second line from the top,
6873         ;; whichever is the least.
6874         (let ((top-pos (save-excursion
6875                          (gnus-forward-line-ignore-invisible (- top))
6876                          (point))))
6877           (if (> bottom top-pos)
6878               ;; Keep the second line from the top visible
6879               (set-window-start window top-pos)
6880             ;; Try to keep the bottom line visible; if it's partially
6881             ;; obscured, either scroll one more line to make it fully
6882             ;; visible, or revert to using TOP-POS.
6883             (save-excursion
6884               (goto-char (point-max))
6885               (gnus-forward-line-ignore-invisible -1)
6886               (let ((last-line-start (point)))
6887                 (goto-char bottom)
6888                 (set-window-start window (point) t)
6889                 (when (not (pos-visible-in-window-p last-line-start window))
6890                   (gnus-forward-line-ignore-invisible 1)
6891                   (set-window-start window (min (point) top-pos) t)))))))
6892       ;; Do horizontal recentering while we're at it.
6893       (when (and (get-buffer-window (current-buffer) t)
6894                  (not (eq gnus-auto-center-summary 'vertical)))
6895         (let ((selected (selected-window)))
6896           (select-window (get-buffer-window (current-buffer) t))
6897           (gnus-summary-position-point)
6898           (gnus-horizontal-recenter)
6899           (select-window selected))))))
6900
6901 (defun gnus-summary-jump-to-group (newsgroup)
6902   "Move point to NEWSGROUP in group mode buffer."
6903   ;; Keep update point of group mode buffer if visible.
6904   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6905       (save-window-excursion
6906         ;; Take care of tree window mode.
6907         (when (get-buffer-window gnus-group-buffer)
6908           (pop-to-buffer gnus-group-buffer))
6909         (gnus-group-jump-to-group newsgroup))
6910     (save-excursion
6911       ;; Take care of tree window mode.
6912       (if (get-buffer-window gnus-group-buffer 0)
6913           (pop-to-buffer gnus-group-buffer)
6914         (set-buffer gnus-group-buffer))
6915       (gnus-group-jump-to-group newsgroup))))
6916
6917 ;; This function returns a list of article numbers based on the
6918 ;; difference between the ranges of read articles in this group and
6919 ;; the range of active articles.
6920 (defun gnus-list-of-unread-articles (group)
6921   (let* ((read (gnus-info-read (gnus-get-info group)))
6922          (active (or (gnus-active group) (gnus-activate-group group)))
6923          (last (or (cdr active)
6924                    (error "Group %s couldn't be activated " group)))
6925          (bottom (if gnus-newsgroup-maximum-articles
6926                      (max (car active)
6927                           (- last gnus-newsgroup-maximum-articles -1))
6928                    (car active)))
6929          first nlast unread)
6930     ;; If none are read, then all are unread.
6931     (if (not read)
6932         (setq first bottom)
6933       ;; If the range of read articles is a single range, then the
6934       ;; first unread article is the article after the last read
6935       ;; article.  Sounds logical, doesn't it?
6936       (if (and (not (listp (cdr read)))
6937                (or (< (car read) bottom)
6938                    (progn (setq read (list read))
6939                           nil)))
6940           (setq first (max bottom (1+ (cdr read))))
6941         ;; `read' is a list of ranges.
6942         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6943                                   (caar read)))
6944                   1)
6945           (setq first bottom))
6946         (while read
6947           (when first
6948             (while (< first nlast)
6949               (setq unread (cons first unread)
6950                     first (1+ first))))
6951           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6952           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6953           (setq read (cdr read)))))
6954     ;; And add the last unread articles.
6955     (while (<= first last)
6956       (setq unread (cons first unread)
6957             first (1+ first)))
6958     ;; Return the list of unread articles.
6959     (delq 0 (nreverse unread))))
6960
6961 (defun gnus-list-of-read-articles (group)
6962   "Return a list of unread, unticked and non-dormant articles."
6963   (let* ((info (gnus-get-info group))
6964          (marked (gnus-info-marks info))
6965          (active (gnus-active group)))
6966     (and info active
6967          (gnus-list-range-difference
6968           (gnus-list-range-difference
6969            (gnus-sorted-complement
6970             (gnus-uncompress-range
6971              (if gnus-newsgroup-maximum-articles
6972                  (cons (max (car active)
6973                             (- (cdr active)
6974                                gnus-newsgroup-maximum-articles
6975                                -1))
6976                        (cdr active))
6977                active))
6978             (gnus-list-of-unread-articles group))
6979            (cdr (assq 'dormant marked)))
6980           (cdr (assq 'tick marked))))))
6981
6982 ;; This function returns a sequence of article numbers based on the
6983 ;; difference between the ranges of read articles in this group and
6984 ;; the range of active articles.
6985 (defun gnus-sequence-of-unread-articles (group)
6986   (let* ((read (gnus-info-read (gnus-get-info group)))
6987          (active (or (gnus-active group) (gnus-activate-group group)))
6988          (last (cdr active))
6989          (bottom (if gnus-newsgroup-maximum-articles
6990                      (max (car active)
6991                           (- last gnus-newsgroup-maximum-articles -1))
6992                    (car active)))
6993          first nlast unread)
6994     ;; If none are read, then all are unread.
6995     (if (not read)
6996         (setq first bottom)
6997       ;; If the range of read articles is a single range, then the
6998       ;; first unread article is the article after the last read
6999       ;; article.  Sounds logical, doesn't it?
7000       (if (and (not (listp (cdr read)))
7001                (or (< (car read) bottom)
7002                    (progn (setq read (list read))
7003                           nil)))
7004           (setq first (max bottom (1+ (cdr read))))
7005         ;; `read' is a list of ranges.
7006         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7007                                   (caar read)))
7008                   1)
7009           (setq first bottom))
7010         (while read
7011           (when first
7012             (push (cons first nlast) unread))
7013           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7014           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7015           (setq read (cdr read)))))
7016     ;; And add the last unread articles.
7017     (cond ((not (and first last))
7018            nil)
7019           ((< first last)
7020            (push (cons first last) unread))
7021           ((= first last)
7022            (push first unread)))
7023     ;; Return the sequence of unread articles.
7024     (delq 0 (nreverse unread))))
7025
7026 ;; Various summary commands
7027
7028 (defun gnus-summary-select-article-buffer ()
7029   "Reconfigure windows to show the article buffer.
7030 If `gnus-widen-article-window' is set, show only the article
7031 buffer."
7032   (interactive)
7033   (if (not (gnus-buffer-live-p gnus-article-buffer))
7034       (error "There is no article buffer for this summary buffer")
7035     (unless (get-buffer-window gnus-article-buffer)
7036       (gnus-summary-show-article))
7037     (gnus-configure-windows
7038      (if gnus-widen-article-window
7039          'only-article
7040        'article)
7041      t)
7042     (select-window (get-buffer-window gnus-article-buffer))))
7043
7044 (defun gnus-summary-universal-argument (arg)
7045   "Perform any operation on all articles that are process/prefixed."
7046   (interactive "P")
7047   (let ((articles (gnus-summary-work-articles arg))
7048         func article)
7049     (if (eq
7050          (setq
7051           func
7052           (key-binding
7053            (read-key-sequence
7054             (substitute-command-keys
7055              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7056          'undefined)
7057         (gnus-error 1 "Undefined key")
7058       (save-excursion
7059         (while articles
7060           (gnus-summary-goto-subject (setq article (pop articles)))
7061           (let (gnus-newsgroup-processable)
7062             (command-execute func))
7063           (gnus-summary-remove-process-mark article)))))
7064   (gnus-summary-position-point))
7065
7066 (defun gnus-summary-toggle-truncation (&optional arg)
7067   "Toggle truncation of summary lines.
7068 With ARG, turn line truncation on if ARG is positive."
7069   (interactive "P")
7070   (setq truncate-lines
7071         (if (null arg) (not truncate-lines)
7072           (> (prefix-numeric-value arg) 0)))
7073   (redraw-display))
7074
7075 (defun gnus-summary-find-for-reselect ()
7076   "Return the number of an article to stay on across a reselect.
7077 The current article is considered, then following articles, then previous
7078 articles.  An article is sought which is not canceled and isn't a temporary
7079 insertion from another group.  If there's no such then return a dummy 0."
7080   (let (found)
7081     (dolist (rev '(nil t))
7082       (unless found      ; don't demand the reverse list if we don't need it
7083         (let ((data (gnus-data-find-list
7084                      (gnus-summary-article-number) (gnus-data-list rev))))
7085           (while (and data (not found))
7086             (if (and (< 0 (gnus-data-number (car data)))
7087                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7088                 (setq found (gnus-data-number (car data))))
7089             (setq data (cdr data))))))
7090     (or found 0)))
7091
7092 (defun gnus-summary-reselect-current-group (&optional all rescan)
7093   "Exit and then reselect the current newsgroup.
7094 The prefix argument ALL means to select all articles."
7095   (interactive "P")
7096   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7097     (error "Ephemeral groups can't be reselected"))
7098   (let ((current-subject (gnus-summary-find-for-reselect))
7099         (group gnus-newsgroup-name))
7100     (setq gnus-newsgroup-begin nil)
7101     (gnus-summary-exit nil 'leave-hidden)
7102     ;; We have to adjust the point of group mode buffer because
7103     ;; point was moved to the next unread newsgroup by exiting.
7104     (gnus-summary-jump-to-group group)
7105     (when rescan
7106       (save-excursion
7107         (gnus-group-get-new-news-this-group 1)))
7108     (gnus-group-read-group all t)
7109     (gnus-summary-goto-subject current-subject nil t)))
7110
7111 (defun gnus-summary-rescan-group (&optional all)
7112   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7113   (interactive "P")
7114   (let ((config gnus-current-window-configuration))
7115     (gnus-summary-reselect-current-group all t)
7116     (gnus-configure-windows config)
7117     (when (eq config 'article)
7118       (gnus-summary-select-article))))
7119
7120 (defun gnus-summary-update-info (&optional non-destructive)
7121   (save-excursion
7122     (let ((group gnus-newsgroup-name))
7123       (when group
7124         (when gnus-newsgroup-kill-headers
7125           (setq gnus-newsgroup-killed
7126                 (gnus-compress-sequence
7127                  (gnus-sorted-union
7128                   (gnus-list-range-intersection
7129                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7130                   gnus-newsgroup-unreads)
7131                  t)))
7132         (unless (listp (cdr gnus-newsgroup-killed))
7133           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7134         (let ((headers gnus-newsgroup-headers)
7135               (ephemeral-p (gnus-ephemeral-group-p group))
7136               info)
7137           (unless ephemeral-p
7138             (setq info (copy-sequence (gnus-get-info group))
7139                   info (delq (gnus-info-params info) info)))
7140           ;; Set the new ranges of read articles.
7141           (with-current-buffer gnus-group-buffer
7142             (gnus-undo-force-boundary))
7143           (gnus-update-read-articles
7144            group (gnus-sorted-union
7145                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7146           ;; Set the current article marks.
7147           (let ((gnus-newsgroup-scored
7148                  (if (and (not gnus-save-score)
7149                           (not non-destructive))
7150                      nil
7151                    gnus-newsgroup-scored)))
7152             (save-excursion
7153               (gnus-update-marks)))
7154           ;; Do the cross-ref thing.
7155           (when gnus-use-cross-reference
7156             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7157           ;; Do not switch windows but change the buffer to work.
7158           (set-buffer gnus-group-buffer)
7159           (unless ephemeral-p
7160             (gnus-group-update-group
7161              group nil
7162              (equal info
7163                     (setq info (copy-sequence (gnus-get-info group))
7164                           info (delq (gnus-info-params info) info))))))))))
7165
7166 (defun gnus-summary-save-newsrc (&optional force)
7167   "Save the current number of read/marked articles in the dribble buffer.
7168 The dribble buffer will then be saved.
7169 If FORCE (the prefix), also save the .newsrc file(s)."
7170   (interactive "P")
7171   (gnus-summary-update-info t)
7172   (if force
7173       (gnus-save-newsrc-file)
7174     (gnus-dribble-save)))
7175
7176 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7177
7178 (defun gnus-summary-exit (&optional temporary leave-hidden)
7179   "Exit reading current newsgroup, and then return to group selection mode.
7180 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7181   (interactive)
7182   (gnus-set-global-variables)
7183   (when (gnus-buffer-live-p gnus-article-buffer)
7184     (with-current-buffer gnus-article-buffer
7185       (mm-destroy-parts gnus-article-mime-handles)
7186       ;; Set it to nil for safety reason.
7187       (setq gnus-article-mime-handle-alist nil)
7188       (setq gnus-article-mime-handles nil)))
7189   (gnus-kill-save-kill-buffer)
7190   (gnus-async-halt-prefetch)
7191   (let* ((group gnus-newsgroup-name)
7192          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7193          (gnus-group-is-exiting-p t)
7194          (article-buffer gnus-article-buffer)
7195          (original-article-buffer gnus-original-article-buffer)
7196          (mode major-mode)
7197          (group-point nil)
7198          (buf (current-buffer))
7199          ;; `gnus-single-article-buffer' is nil buffer-locally in
7200          ;; ephemeral group of which summary buffer will be killed,
7201          ;; but the global value may be non-nil.
7202          (single-article-buffer gnus-single-article-buffer))
7203     (unless quit-config
7204       ;; Do adaptive scoring, and possibly save score files.
7205       (when gnus-newsgroup-adaptive
7206         (gnus-score-adaptive))
7207       (when gnus-use-scoring
7208         (gnus-score-save)))
7209     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7210     (when gnus-use-cache
7211       (gnus-cache-possibly-remove-articles)
7212       (gnus-cache-save-buffers))
7213     (gnus-async-prefetch-remove-group group)
7214     (when gnus-suppress-duplicates
7215       (gnus-dup-enter-articles))
7216     (when gnus-use-trees
7217       (gnus-tree-close group))
7218     (when gnus-use-cache
7219       (gnus-cache-write-active))
7220     ;; Remove entries for this group.
7221     (nnmail-purge-split-history (gnus-group-real-name group))
7222     ;; Make all changes in this group permanent.
7223     (unless quit-config
7224       (gnus-run-hooks 'gnus-exit-group-hook)
7225       (gnus-summary-update-info))
7226     (gnus-close-group group)
7227     ;; Make sure where we were, and go to next newsgroup.
7228     (when (buffer-live-p (get-buffer gnus-group-buffer))
7229       (set-buffer gnus-group-buffer))
7230     (unless quit-config
7231       (gnus-group-jump-to-group group))
7232     (gnus-run-hooks 'gnus-summary-exit-hook)
7233     (unless (or quit-config
7234                 (not gnus-summary-next-group-on-exit)
7235                 ;; If this group has disappeared from the summary
7236                 ;; buffer, don't skip forwards.
7237                 (not (string= group (gnus-group-group-name))))
7238       (gnus-group-next-unread-group 1))
7239     (setq group-point (point))
7240     (if temporary
7241         nil                             ;Nothing to do.
7242       (set-buffer buf)
7243       (if (not gnus-kill-summary-on-exit)
7244           (progn
7245             (gnus-deaden-summary)
7246             (setq mode nil))
7247         (when (get-buffer gnus-article-buffer)
7248           (bury-buffer gnus-article-buffer))
7249         ;; Return to group mode buffer.
7250         (when (eq mode 'gnus-summary-mode)
7251           (gnus-kill-buffer buf)))
7252
7253       (setq gnus-current-select-method gnus-select-method)
7254       (when (gnus-buffer-live-p gnus-group-buffer)
7255         (set-buffer gnus-group-buffer))
7256       (if quit-config
7257           (gnus-handle-ephemeral-exit quit-config)
7258         (goto-char group-point)
7259         ;; If gnus-group-buffer is already displayed, make sure we also move
7260         ;; the cursor in the window that displays it.
7261         (let ((win (get-buffer-window (current-buffer) 0)))
7262           (if win (set-window-point win (point))))
7263         (unless leave-hidden
7264           (gnus-configure-windows 'group 'force)))
7265
7266       ;; If we have several article buffers, we kill them at exit.
7267       (unless single-article-buffer
7268         (when (gnus-buffer-live-p article-buffer)
7269           (with-current-buffer article-buffer
7270             ;; Don't kill sticky article buffers
7271             (unless (eq major-mode 'gnus-sticky-article-mode)
7272               (gnus-kill-buffer article-buffer)
7273               (setq gnus-article-current nil))))
7274         (gnus-kill-buffer original-article-buffer))
7275
7276       ;; Clear the current group name.
7277       (unless quit-config
7278         (setq gnus-newsgroup-name nil)))))
7279
7280 (declare-function gnus-article-stop-animations "gnus-art" ())
7281 (declare-function gnus-stop-downloads "gnus-art" ())
7282
7283 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7284 (defun gnus-summary-exit-no-update (&optional no-questions)
7285   "Quit reading current newsgroup without updating read article info."
7286   (interactive)
7287   (let* ((group gnus-newsgroup-name)
7288          (gnus-group-is-exiting-p t)
7289          (gnus-group-is-exiting-without-update-p t)
7290          (quit-config (gnus-group-quit-config group)))
7291     (when (or no-questions
7292               gnus-expert-user
7293               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7294       (gnus-async-halt-prefetch)
7295       (run-hooks 'gnus-summary-prepare-exit-hook)
7296       (when (gnus-buffer-live-p gnus-article-buffer)
7297         (with-current-buffer gnus-article-buffer
7298           (gnus-article-stop-animations)
7299           (gnus-stop-downloads)
7300           (mm-destroy-parts gnus-article-mime-handles)
7301           ;; Set it to nil for safety reason.
7302           (setq gnus-article-mime-handle-alist nil)
7303           (setq gnus-article-mime-handles nil)))
7304       ;; If we have several article buffers, we kill them at exit.
7305       (unless gnus-single-article-buffer
7306         (gnus-kill-buffer gnus-article-buffer)
7307         (gnus-kill-buffer gnus-original-article-buffer)
7308         (setq gnus-article-current nil))
7309       ;; Return to the group buffer.
7310       (if (not gnus-kill-summary-on-exit)
7311           (progn
7312             (gnus-deaden-summary)
7313             (gnus-configure-windows 'group 'force))
7314         (gnus-configure-windows 'group 'force)
7315         (gnus-close-group group)
7316         (gnus-kill-buffer gnus-summary-buffer))
7317       (unless gnus-single-article-buffer
7318         (setq gnus-article-current nil))
7319       (when gnus-use-trees
7320         (gnus-tree-close group))
7321       (gnus-async-prefetch-remove-group group)
7322       (when (get-buffer gnus-article-buffer)
7323         (bury-buffer gnus-article-buffer))
7324       ;; Clear the current group name.
7325       (setq gnus-newsgroup-name nil)
7326       (unless (gnus-ephemeral-group-p group)
7327         (gnus-group-update-group group nil t))
7328       (when (equal (gnus-group-group-name) group)
7329         (gnus-group-next-unread-group 1))
7330       (when quit-config
7331         (gnus-handle-ephemeral-exit quit-config)))))
7332
7333 (defun gnus-handle-ephemeral-exit (quit-config)
7334   "Handle movement when leaving an ephemeral group.
7335 The state which existed when entering the ephemeral is reset."
7336   (if (not (buffer-live-p (car quit-config)))
7337       (when (gnus-buffer-live-p gnus-group-buffer)
7338         (gnus-configure-windows 'group 'force))
7339     (set-buffer (car quit-config))
7340     (unless (eq (cdr quit-config) 'group)
7341       (setq gnus-current-select-method
7342             (gnus-find-method-for-group gnus-newsgroup-name)))
7343     (cond ((eq major-mode 'gnus-summary-mode)
7344            (gnus-set-global-variables))
7345           ((eq major-mode 'gnus-article-mode)
7346            (save-current-buffer
7347              ;; The `gnus-summary-buffer' variable may point
7348              ;; to the old summary buffer when using a single
7349              ;; article buffer.
7350              (unless (gnus-buffer-live-p gnus-summary-buffer)
7351                (set-buffer gnus-group-buffer))
7352              (set-buffer gnus-summary-buffer)
7353              (gnus-set-global-variables))))
7354     (if (or (eq (cdr quit-config) 'article)
7355             (eq (cdr quit-config) 'pick))
7356         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7357             (gnus-configure-windows 'pick 'force)
7358           (gnus-configure-windows (cdr quit-config) 'force))
7359       (gnus-configure-windows (cdr quit-config) 'force))
7360     (when (eq major-mode 'gnus-summary-mode)
7361       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7362                                                      next-unread-noselect))
7363           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7364                                   'next-noselect)
7365                               (gnus-summary-next-subject 1 nil t))
7366                              ((eq gnus-auto-select-on-ephemeral-exit
7367                                   'next-unread-noselect)
7368                               (gnus-summary-next-subject 1 t t))))
7369             ;; Hide the article buffer which displays the article different
7370             ;; from the one that the cursor points to in the summary buffer.
7371             (gnus-configure-windows 'summary 'force))
7372         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7373                (gnus-summary-next-subject 1))
7374               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7375                (gnus-summary-next-subject 1 t))))
7376       (gnus-summary-recenter)
7377       (gnus-summary-position-point))))
7378
7379 ;;; Dead summaries.
7380
7381 (defvar gnus-dead-summary-mode-map
7382   (let ((map (make-keymap)))
7383     (suppress-keymap map)
7384     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7385     (dolist (key '("\C-d" "\r" "\177" [delete]))
7386       (define-key map key 'gnus-summary-wake-up-the-dead))
7387     (dolist (key '("q" "Q"))
7388       (define-key map key 'bury-buffer))
7389     map))
7390
7391 (define-minor-mode gnus-dead-summary-mode
7392   "Minor mode for Gnus summary buffers."
7393   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7394   (unless (derived-mode-p 'gnus-summary-mode)
7395     (setq gnus-dead-summary-mode nil)))
7396
7397 (defun gnus-deaden-summary ()
7398   "Make the current summary buffer into a dead summary buffer."
7399   ;; Kill any previous dead summary buffer.
7400   (when (and gnus-dead-summary
7401              (buffer-name gnus-dead-summary))
7402     (with-current-buffer gnus-dead-summary
7403       (when gnus-dead-summary-mode
7404         (kill-buffer (current-buffer)))))
7405   ;; Make this the current dead summary.
7406   (setq gnus-dead-summary (current-buffer))
7407   (gnus-dead-summary-mode 1)
7408   (let ((name (buffer-name)))
7409     (when (string-match "Summary" name)
7410       (rename-buffer
7411        (concat (substring name 0 (match-beginning 0)) "Dead "
7412                (substring name (match-beginning 0)))
7413        t)
7414       (bury-buffer))))
7415
7416 (defun gnus-kill-or-deaden-summary (buffer)
7417   "Kill or deaden the summary BUFFER."
7418   (save-excursion
7419     (when (and (buffer-name buffer)
7420                (not gnus-single-article-buffer))
7421       (with-current-buffer buffer
7422         (gnus-kill-buffer gnus-article-buffer)
7423         (gnus-kill-buffer gnus-original-article-buffer)))
7424     (cond
7425      ;; Kill the buffer.
7426      (gnus-kill-summary-on-exit
7427       (when (and gnus-use-trees
7428                  (gnus-buffer-exists-p buffer))
7429         (with-current-buffer buffer
7430           (gnus-tree-close gnus-newsgroup-name)))
7431       (gnus-kill-buffer buffer))
7432      ;; Deaden the buffer.
7433      ((gnus-buffer-exists-p buffer)
7434       (with-current-buffer buffer
7435         (gnus-deaden-summary))))))
7436
7437 (defun gnus-summary-wake-up-the-dead (&rest args)
7438   "Wake up the dead summary buffer."
7439   (interactive)
7440   (gnus-dead-summary-mode -1)
7441   (let ((name (buffer-name)))
7442     (when (string-match "Dead " name)
7443       (rename-buffer
7444        (concat (substring name 0 (match-beginning 0))
7445                (substring name (match-end 0)))
7446        t)))
7447   (gnus-message 3 "This dead summary is now alive again"))
7448
7449 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7450 (defun gnus-summary-describe-group (&optional force)
7451   "Describe the current newsgroup."
7452   (interactive "P")
7453   (gnus-group-describe-group force gnus-newsgroup-name))
7454
7455 (defun gnus-summary-describe-briefly ()
7456   "Describe summary mode commands briefly."
7457   (interactive)
7458   (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")))
7459
7460 ;; Walking around group mode buffer from summary mode.
7461
7462 (defun gnus-summary-next-group (&optional no-article target-group backward)
7463   "Exit current newsgroup and then select next unread newsgroup.
7464 If prefix argument NO-ARTICLE is non-nil, no article is selected
7465 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7466 previous group instead."
7467   (interactive "P")
7468   ;; Stop pre-fetching.
7469   (gnus-async-halt-prefetch)
7470   (let ((current-group gnus-newsgroup-name)
7471         (current-buffer (current-buffer))
7472         entered)
7473     ;; First we semi-exit this group to update Xrefs and all variables.
7474     ;; We can't do a real exit, because the window conf must remain
7475     ;; the same in case the user is prompted for info, and we don't
7476     ;; want the window conf to change before that...
7477     (gnus-summary-exit t)
7478     (while (not entered)
7479       ;; Then we find what group we are supposed to enter.
7480       (set-buffer gnus-group-buffer)
7481       (gnus-group-jump-to-group current-group)
7482       (setq target-group
7483             (or target-group
7484                 (if (eq gnus-keep-same-level 'best)
7485                     (gnus-summary-best-group gnus-newsgroup-name)
7486                   (gnus-summary-search-group backward gnus-keep-same-level))))
7487       (if (not target-group)
7488           ;; There are no further groups, so we return to the group
7489           ;; buffer.
7490           (progn
7491             (gnus-message 5 "Returning to the group buffer")
7492             (setq entered t)
7493             (when (gnus-buffer-live-p current-buffer)
7494               (set-buffer current-buffer)
7495               (gnus-summary-exit))
7496             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7497         ;; We try to enter the target group.
7498         (gnus-group-jump-to-group target-group)
7499         (let ((unreads (gnus-group-group-unread)))
7500           (if (and (or (eq t unreads)
7501                        (and unreads (not (zerop unreads))))
7502                    (gnus-summary-read-group
7503                     target-group nil no-article
7504                     (and (buffer-name current-buffer) current-buffer)
7505                     nil backward))
7506               (setq entered t)
7507             (setq current-group target-group
7508                   target-group nil)))))))
7509
7510 (defun gnus-summary-prev-group (&optional no-article)
7511   "Exit current newsgroup and then select previous unread newsgroup.
7512 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7513   (interactive "P")
7514   (gnus-summary-next-group no-article nil t))
7515
7516 ;; Walking around summary lines.
7517
7518 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7519   "Go to the first subject satisfying any non-nil constraint.
7520 If UNREAD is non-nil, the article should be unread.
7521 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7522 If UNSEEN is non-nil, the article should be unseen as well as unread.
7523 Returns the article selected or nil if there are no matching articles."
7524   (interactive "P")
7525   (cond
7526    ;; Empty summary.
7527    ((null gnus-newsgroup-data)
7528     (gnus-message 3 "No articles in the group")
7529     nil)
7530    ;; Pick the first article.
7531    ((not (or unread undownloaded unseen))
7532     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7533     (gnus-data-number (car gnus-newsgroup-data)))
7534    ;; Find the first unread article.
7535    (t
7536     (let ((data gnus-newsgroup-data))
7537       (while (and data
7538                   (let ((num (gnus-data-number (car data))))
7539                     (or (memq num gnus-newsgroup-unfetched)
7540                         (not (or (and unread
7541                                       (memq num gnus-newsgroup-unreads))
7542                                  (and undownloaded
7543                                       (memq num gnus-newsgroup-undownloaded))
7544                                  (and unseen
7545                                       (memq num gnus-newsgroup-unseen)
7546                                       (memq num gnus-newsgroup-unreads)))))))
7547         (setq data (cdr data)))
7548       (prog1
7549           (if data
7550               (progn
7551                 (goto-char (gnus-data-pos (car data)))
7552                 (gnus-data-number (car data)))
7553             (gnus-message 3 "No more%s articles"
7554                           (let* ((r (when unread " unread"))
7555                                  (d (when undownloaded " undownloaded"))
7556                                  (s (when unseen " unseen"))
7557                                  (l (delq nil (list r d s))))
7558                             (cond ((= 3 (length l))
7559                                    (concat r "," d ", or" s))
7560                                   ((= 2 (length l))
7561                                    (concat (car l) ", or" (cadr l)))
7562                                   ((= 1 (length l))
7563                                    (car l))
7564                                   (t
7565                                    ""))))
7566             nil
7567             )
7568         (gnus-summary-position-point))))))
7569
7570 (defun gnus-summary-next-subject (n &optional unread dont-display)
7571   "Go to next N'th summary line.
7572 If N is negative, go to the previous N'th subject line.
7573 If UNREAD is non-nil, only unread articles are selected.
7574 The difference between N and the actual number of steps taken is
7575 returned."
7576   (interactive "p")
7577   (let ((backward (< n 0))
7578         (n (abs n)))
7579     (while (and (> n 0)
7580                 (if backward
7581                     (gnus-summary-find-prev unread)
7582                   (gnus-summary-find-next unread)))
7583       (unless (zerop (setq n (1- n)))
7584         (gnus-summary-show-thread)))
7585     (when (/= 0 n)
7586       (gnus-message 7 "No more%s articles"
7587                     (if unread " unread" "")))
7588     (unless dont-display
7589       (gnus-summary-recenter)
7590       (gnus-summary-position-point))
7591     n))
7592
7593 (defun gnus-summary-next-unread-subject (n)
7594   "Go to next N'th unread summary line."
7595   (interactive "p")
7596   (gnus-summary-next-subject n t))
7597
7598 (defun gnus-summary-prev-subject (n &optional unread)
7599   "Go to previous N'th summary line.
7600 If optional argument UNREAD is non-nil, only unread article is selected."
7601   (interactive "p")
7602   (gnus-summary-next-subject (- n) unread))
7603
7604 (defun gnus-summary-prev-unread-subject (n)
7605   "Go to previous N'th unread summary line."
7606   (interactive "p")
7607   (gnus-summary-next-subject (- n) t))
7608
7609 (defun gnus-summary-goto-subjects (articles)
7610   "Insert the subject header for ARTICLES in the current buffer."
7611   (save-excursion
7612     (dolist (article articles)
7613       (gnus-summary-goto-subject article t)))
7614   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7615   (gnus-summary-position-point))
7616
7617 (defun gnus-summary-goto-subject (article &optional force silent)
7618   "Go to the subject line of ARTICLE.
7619 If FORCE, also allow jumping to articles not currently shown."
7620   (interactive "nArticle number: ")
7621   (unless (numberp article)
7622     (error "Article %s is not a number" article))
7623   (let ((b (point))
7624         (data (gnus-data-find article)))
7625     ;; We read in the article if we have to.
7626     (and (not data)
7627          force
7628          (gnus-summary-insert-subject
7629           article
7630           (if (or (numberp force) (vectorp force)) force)
7631           t)
7632          (setq data (gnus-data-find article)))
7633     (goto-char b)
7634     (if (not data)
7635         (progn
7636           (unless silent
7637             (gnus-message 3 "Can't find article %d" article))
7638           nil)
7639       (let ((pt (gnus-data-pos data)))
7640         (goto-char pt)
7641         (gnus-summary-set-article-display-arrow pt))
7642       (gnus-summary-position-point)
7643       article)))
7644
7645 ;; Walking around summary lines with displaying articles.
7646
7647 (defun gnus-summary-expand-window (&optional arg)
7648   "Make the summary buffer take up the entire Emacs frame.
7649 Given a prefix, will force an `article' buffer configuration."
7650   (interactive "P")
7651   (if arg
7652       (gnus-configure-windows 'article 'force)
7653     (gnus-configure-windows 'summary 'force)))
7654
7655 (defun gnus-summary-display-article (article &optional all-header)
7656   "Display ARTICLE in article buffer."
7657   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7658                (with-current-buffer gnus-article-buffer
7659                  (eq major-mode 'gnus-article-mode)))
7660     (gnus-article-setup-buffer))
7661   (gnus-set-global-variables)
7662   (with-current-buffer gnus-article-buffer
7663     (setq gnus-article-charset gnus-newsgroup-charset)
7664     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7665     (mm-enable-multibyte))
7666   (if (null article)
7667       nil
7668     (prog1
7669         (if gnus-summary-display-article-function
7670             (funcall gnus-summary-display-article-function article all-header)
7671           (gnus-article-prepare article all-header))
7672       (gnus-run-hooks 'gnus-select-article-hook)
7673       (when (and gnus-current-article
7674                  (not (zerop gnus-current-article)))
7675         (gnus-summary-goto-subject gnus-current-article))
7676       (gnus-summary-recenter)
7677       (when (and gnus-use-trees gnus-show-threads)
7678         (gnus-possibly-generate-tree article)
7679         (gnus-highlight-selected-tree article))
7680       ;; Successfully display article.
7681       (gnus-article-set-window-start
7682        (cdr (assq article gnus-newsgroup-bookmarks))))))
7683
7684 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7685   "Select the current article.
7686 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7687 non-nil, the article will be re-fetched even if it already present in
7688 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7689 be displayed."
7690   ;; Make sure we are in the summary buffer to work around bbdb bug.
7691   (unless (eq major-mode 'gnus-summary-mode)
7692     (set-buffer gnus-summary-buffer))
7693   (let ((article (or article (gnus-summary-article-number)))
7694         (all-headers (not (not all-headers))) ;Must be t or nil.
7695         gnus-summary-display-article-function)
7696     (and (not pseudo)
7697          (gnus-summary-article-pseudo-p article)
7698          (error "This is a pseudo-article"))
7699     (with-current-buffer gnus-summary-buffer
7700       (if (or (and gnus-single-article-buffer
7701                    (or (null gnus-current-article)
7702                        (null gnus-article-current)
7703                        (null (get-buffer gnus-article-buffer))
7704                        (not (eq article (cdr gnus-article-current)))
7705                        (not (equal (car gnus-article-current)
7706                                    gnus-newsgroup-name))
7707                        (not (get-buffer gnus-original-article-buffer))))
7708               (and (not gnus-single-article-buffer)
7709                    (or (null gnus-current-article)
7710                        (not (get-buffer gnus-original-article-buffer))
7711                        (not (eq gnus-current-article article))))
7712               force)
7713           ;; The requested article is different from the current article.
7714           (progn
7715             (gnus-summary-display-article article all-headers)
7716             (when (gnus-buffer-live-p gnus-article-buffer)
7717               (with-current-buffer gnus-article-buffer
7718                 (if (not gnus-article-decoded-p) ;; a local variable
7719                     (mm-disable-multibyte))))
7720             (gnus-article-set-window-start
7721              (cdr (assq article gnus-newsgroup-bookmarks)))
7722             article)
7723         'old))))
7724
7725 (defun gnus-summary-force-verify-and-decrypt ()
7726   "Display buttons for signed/encrypted parts and verify/decrypt them."
7727   (interactive)
7728   (let ((mm-verify-option 'known)
7729         (mm-decrypt-option 'known)
7730         (gnus-article-emulate-mime t)
7731         (gnus-buttonized-mime-types (append (list "multipart/signed"
7732                                                   "multipart/encrypted")
7733                                             gnus-buttonized-mime-types)))
7734     (gnus-summary-select-article nil 'force)))
7735
7736 (defun gnus-summary-next-article (&optional unread subject backward push)
7737   "Select the next article.
7738 If UNREAD, only unread articles are selected.
7739 If SUBJECT, only articles with SUBJECT are selected.
7740 If BACKWARD, the previous article is selected instead of the next."
7741   (interactive "P")
7742   ;; Make sure we are in the summary buffer.
7743   (unless (eq major-mode 'gnus-summary-mode)
7744     (set-buffer gnus-summary-buffer))
7745   (cond
7746    ;; Is there such an article?
7747    ((and (gnus-summary-search-forward unread subject backward)
7748          (or (gnus-summary-display-article (gnus-summary-article-number))
7749              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7750     (gnus-summary-position-point))
7751    ;; If not, we try the first unread, if that is wanted.
7752    ((and subject
7753          gnus-auto-select-same
7754          (gnus-summary-first-unread-article))
7755     (gnus-summary-position-point)
7756     (gnus-message 6 "Wrapped"))
7757    ;; Try to get next/previous article not displayed in this group.
7758    ((and gnus-auto-extend-newsgroup
7759          (not unread) (not subject))
7760     (gnus-summary-goto-article
7761      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7762      nil (count-lines (point-min) (point))))
7763    ;; Go to next/previous group.
7764    (t
7765     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7766       (gnus-summary-jump-to-group gnus-newsgroup-name))
7767     (let ((cmd (if (featurep 'xemacs)
7768                    last-command-char
7769                  last-command-event))
7770           (point
7771            (with-current-buffer gnus-group-buffer
7772              (point)))
7773           (current-summary (current-buffer))
7774           (group
7775            (if (eq gnus-keep-same-level 'best)
7776                (gnus-summary-best-group gnus-newsgroup-name)
7777              (gnus-summary-search-group backward gnus-keep-same-level))))
7778       ;; Select next unread newsgroup automagically.
7779       (cond
7780        ((or (not gnus-auto-select-next)
7781             (not cmd))
7782         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7783        ((or (eq gnus-auto-select-next 'quietly)
7784             (and (eq gnus-auto-select-next 'slightly-quietly)
7785                  push)
7786             (and (eq gnus-auto-select-next 'almost-quietly)
7787                  (gnus-summary-last-article-p)))
7788         ;; Select quietly.
7789         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7790             (gnus-summary-exit)
7791           (gnus-message 7 "No more%s articles (%s)..."
7792                         (if unread " unread" "")
7793                         (if group (concat "selecting " group)
7794                           "exiting"))
7795           (gnus-summary-next-group nil group backward)))
7796        (t
7797         (when (gnus-key-press-event-p last-input-event)
7798           ;; Somehow or other, we may now have selected a different
7799           ;; window.  Make point go back to the summary buffer.
7800           (when (eq current-summary (current-buffer))
7801             ;; FIXME: This burps when get-buffer-window returns nil.
7802             (select-window (get-buffer-window current-summary 0)))
7803           (gnus-summary-walk-group-buffer
7804            gnus-newsgroup-name cmd unread backward point))))))))
7805
7806 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7807   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7808                       (?\C-p (gnus-group-prev-unread-group 1))))
7809         (cursor-in-echo-area t)
7810         keve key group ended prompt)
7811     (with-current-buffer gnus-group-buffer
7812       (goto-char start)
7813       (setq group
7814             (if (eq gnus-keep-same-level 'best)
7815                 (gnus-summary-best-group gnus-newsgroup-name)
7816               (gnus-summary-search-group backward gnus-keep-same-level))))
7817     (while (not ended)
7818       (setq prompt
7819             (format
7820              "No more%s articles%s " (if unread " unread" "")
7821              (if (and group
7822                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7823                  (format " (Type %s for %s [%s])"
7824                          (single-key-description cmd)
7825                          (gnus-group-decoded-name group)
7826                          (gnus-group-unread group))
7827                (format " (Type %s to exit %s)"
7828                        (single-key-description cmd)
7829                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7830       ;; Confirm auto selection.
7831       (setq key (car (setq keve (gnus-read-event-char prompt)))
7832             ended t)
7833       (cond
7834        ((assq key keystrokes)
7835         (let ((obuf (current-buffer)))
7836           (switch-to-buffer gnus-group-buffer)
7837           (when group
7838             (gnus-group-jump-to-group group))
7839           (eval (cadr (assq key keystrokes)))
7840           (setq group (gnus-group-group-name))
7841           (switch-to-buffer obuf))
7842         (setq ended nil))
7843        ((equal key cmd)
7844         (if (or (not group)
7845                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7846             (gnus-summary-exit)
7847           (gnus-summary-next-group nil group backward)))
7848        (t
7849         (push (cdr keve) unread-command-events))))))
7850
7851 (defun gnus-summary-next-unread-article ()
7852   "Select unread article after current one."
7853   (interactive)
7854   (gnus-summary-next-article
7855    (or (not (eq gnus-summary-goto-unread 'never))
7856        (gnus-summary-last-article-p (gnus-summary-article-number)))
7857    (and gnus-auto-select-same
7858         (gnus-summary-article-subject))))
7859
7860 (defun gnus-summary-prev-article (&optional unread subject)
7861   "Select the article before the current one.
7862 If UNREAD is non-nil, only unread articles are selected."
7863   (interactive "P")
7864   (gnus-summary-next-article unread subject t))
7865
7866 (defun gnus-summary-prev-unread-article ()
7867   "Select unread article before current one."
7868   (interactive)
7869   (gnus-summary-prev-article
7870    (or (not (eq gnus-summary-goto-unread 'never))
7871        (gnus-summary-first-article-p (gnus-summary-article-number)))
7872    (and gnus-auto-select-same
7873         (gnus-summary-article-subject))))
7874
7875 (declare-function gnus-article-only-boring-p "gnus-art" ())
7876
7877 (defun gnus-summary-next-page (&optional lines circular stop)
7878   "Show next page of the selected article.
7879 If at the end of the current article, select the next article.
7880 LINES says how many lines should be scrolled up.
7881
7882 If CIRCULAR is non-nil, go to the start of the article instead of
7883 selecting the next article when reaching the end of the current
7884 article.
7885
7886 If STOP is non-nil, just stop when reaching the end of the message.
7887
7888 Also see the variable `gnus-article-skip-boring'."
7889   (interactive "P")
7890   (gnus-set-global-variables)
7891   (let ((article (gnus-summary-article-number))
7892         (article-window (get-buffer-window gnus-article-buffer t))
7893         endp)
7894     ;; If the buffer is empty, we have no article.
7895     (unless article
7896       (error "No article to select"))
7897     (gnus-configure-windows 'article)
7898     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7899         (if (and (eq gnus-summary-goto-unread 'never)
7900                  (not (gnus-summary-last-article-p article)))
7901             (gnus-summary-next-article)
7902           (gnus-summary-next-unread-article))
7903       (if (or (null gnus-current-article)
7904               (null gnus-article-current)
7905               (/= article (cdr gnus-article-current))
7906               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7907           ;; Selected subject is different from current article's.
7908           (gnus-summary-display-article article)
7909         (when article-window
7910           (gnus-eval-in-buffer-window gnus-article-buffer
7911             (setq endp (or (gnus-article-next-page lines)
7912                            (gnus-article-only-boring-p))))
7913           (when endp
7914             (cond ((or stop gnus-summary-stop-at-end-of-message)
7915                    (gnus-message 3 "End of message"))
7916                   (circular
7917                    (gnus-summary-beginning-of-article))
7918                   (lines
7919                    (gnus-message 3 "End of message"))
7920                   ((null lines)
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     (gnus-summary-recenter)
7926     (gnus-summary-position-point)))
7927
7928 (defun gnus-summary-prev-page (&optional lines move)
7929   "Show previous page of selected article.
7930 Argument LINES specifies lines to be scrolled down.
7931 If MOVE, move to the previous unread article if point is at
7932 the beginning of the buffer."
7933   (interactive "P")
7934   (let ((article (gnus-summary-article-number))
7935         (article-window (get-buffer-window gnus-article-buffer t))
7936         endp)
7937     (gnus-configure-windows 'article)
7938     (if (or (null gnus-current-article)
7939             (null gnus-article-current)
7940             (/= article (cdr gnus-article-current))
7941             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7942         ;; Selected subject is different from current article's.
7943         (gnus-summary-display-article article)
7944       (gnus-summary-recenter)
7945       (when article-window
7946         (gnus-eval-in-buffer-window gnus-article-buffer
7947           (setq endp (gnus-article-prev-page lines)))
7948         (when (and move endp)
7949           (cond (lines
7950                  (gnus-message 3 "Beginning of message"))
7951                 ((null lines)
7952                  (if (and (eq gnus-summary-goto-unread 'never)
7953                           (not (gnus-summary-first-article-p article)))
7954                      (gnus-summary-prev-article)
7955                    (gnus-summary-prev-unread-article))))))))
7956   (gnus-summary-position-point))
7957
7958 (defun gnus-summary-prev-page-or-article (&optional lines)
7959   "Show previous page of selected article.
7960 Argument LINES specifies lines to be scrolled down.
7961 If at the beginning of the article, go to the next article."
7962   (interactive "P")
7963   (gnus-summary-prev-page lines t))
7964
7965 (defun gnus-summary-scroll-up (lines)
7966   "Scroll up (or down) one line current article.
7967 Argument LINES specifies lines to be scrolled up (or down if negative).
7968 If no article is selected, then the current article will be selected first."
7969   (interactive "p")
7970   (gnus-configure-windows 'article)
7971   (gnus-summary-show-thread)
7972   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7973     (gnus-eval-in-buffer-window gnus-article-buffer
7974       (cond ((> lines 0)
7975              (when (gnus-article-next-page lines)
7976                (gnus-message 3 "End of message")))
7977             ((< lines 0)
7978              (gnus-article-prev-page (- lines))))))
7979   (gnus-summary-recenter)
7980   (gnus-summary-position-point))
7981
7982 (defun gnus-summary-scroll-down (lines)
7983   "Scroll down (or up) one line current article.
7984 Argument LINES specifies lines to be scrolled down (or up if negative).
7985 If no article is selected, then the current article will be selected first."
7986   (interactive "p")
7987   (gnus-summary-scroll-up (- lines)))
7988
7989 (defun gnus-summary-next-same-subject ()
7990   "Select next article which has the same subject as current one."
7991   (interactive)
7992   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7993
7994 (defun gnus-summary-prev-same-subject ()
7995   "Select previous article which has the same subject as current one."
7996   (interactive)
7997   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7998
7999 (defun gnus-summary-next-unread-same-subject ()
8000   "Select next unread article which has the same subject as current one."
8001   (interactive)
8002   (gnus-summary-next-article t (gnus-summary-article-subject)))
8003
8004 (defun gnus-summary-prev-unread-same-subject ()
8005   "Select previous unread article which has the same subject as current one."
8006   (interactive)
8007   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8008
8009 (defun gnus-summary-first-unread-article ()
8010   "Select the first unread article.
8011 Return nil if there are no unread articles."
8012   (interactive)
8013   (prog1
8014       (when (gnus-summary-first-subject t)
8015         (gnus-summary-show-thread)
8016         (gnus-summary-first-subject t)
8017         (gnus-summary-display-article (gnus-summary-article-number)))
8018     (gnus-summary-position-point)))
8019
8020 (defun gnus-summary-first-unread-subject ()
8021   "Place the point on the subject line of the first unread article.
8022 Return nil if there are no unread articles."
8023   (interactive)
8024   (prog1
8025       (when (gnus-summary-first-subject t)
8026         (gnus-summary-show-thread)
8027         (gnus-summary-first-subject t))
8028     (gnus-summary-position-point)))
8029
8030 (defun gnus-summary-first-unseen-subject ()
8031   "Place the point on the subject line of the first unseen article.
8032 Return nil if there are no unseen articles."
8033   (interactive)
8034   (prog1
8035       (when (gnus-summary-first-subject nil nil t)
8036         (gnus-summary-show-thread)
8037         (gnus-summary-first-subject nil nil t))
8038     (gnus-summary-position-point)))
8039
8040 (defun gnus-summary-first-unseen-or-unread-subject ()
8041   "Place the point on the subject line of the first unseen and unread article.
8042 If all article have been seen, on the subject line of the first unread
8043 article."
8044   (interactive)
8045   (prog1
8046       (unless (when (gnus-summary-first-subject nil nil t)
8047                 (gnus-summary-show-thread)
8048                 (gnus-summary-first-subject nil nil t))
8049         (when (gnus-summary-first-subject t)
8050           (gnus-summary-show-thread)
8051           (gnus-summary-first-subject t)))
8052     (gnus-summary-position-point)))
8053
8054 (defun gnus-summary-first-article ()
8055   "Select the first article.
8056 Return nil if there are no articles."
8057   (interactive)
8058   (prog1
8059       (when (gnus-summary-first-subject)
8060         (gnus-summary-show-thread)
8061         (gnus-summary-first-subject)
8062         (gnus-summary-display-article (gnus-summary-article-number)))
8063     (gnus-summary-position-point)))
8064
8065 (defun gnus-summary-best-unread-article (&optional arg)
8066   "Select the unread article with the highest score.
8067 If given a prefix argument, select the next unread article that has a
8068 score higher than the default score."
8069   (interactive "P")
8070   (let ((article (if arg
8071                      (gnus-summary-better-unread-subject)
8072                    (gnus-summary-best-unread-subject))))
8073     (if article
8074         (gnus-summary-goto-article article)
8075       (error "No unread articles"))))
8076
8077 (defun gnus-summary-best-unread-subject ()
8078   "Select the unread subject with the highest score."
8079   (interactive)
8080   (let ((best -1000000)
8081         (data gnus-newsgroup-data)
8082         article score)
8083     (while data
8084       (and (gnus-data-unread-p (car data))
8085            (> (setq score
8086                     (gnus-summary-article-score (gnus-data-number (car data))))
8087               best)
8088            (setq best score
8089                  article (gnus-data-number (car data))))
8090       (setq data (cdr data)))
8091     (when article
8092       (gnus-summary-goto-subject article))
8093     (gnus-summary-position-point)
8094     article))
8095
8096 (defun gnus-summary-better-unread-subject ()
8097   "Select the first unread subject that has a score over the default score."
8098   (interactive)
8099   (let ((data gnus-newsgroup-data)
8100         article score)
8101     (while (and (setq article (gnus-data-number (car data)))
8102                 (or (gnus-data-read-p (car data))
8103                     (not (> (gnus-summary-article-score article)
8104                             gnus-summary-default-score))))
8105       (setq data (cdr data)))
8106     (when article
8107       (gnus-summary-goto-subject article))
8108     (gnus-summary-position-point)
8109     article))
8110
8111 (defun gnus-summary-last-subject ()
8112   "Go to the last displayed subject line in the group."
8113   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8114     (when article
8115       (gnus-summary-goto-subject article))))
8116
8117 (defun gnus-summary-goto-article (article &optional all-headers force)
8118   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8119 If ALL-HEADERS is non-nil, no header lines are hidden.
8120 If FORCE, go to the article even if it isn't displayed.  If FORCE
8121 is a number, it is the line the article is to be displayed on."
8122   (interactive
8123    (list
8124     (gnus-completing-read
8125      "Article number or Message-ID"
8126      (mapcar 'int-to-string gnus-newsgroup-limit))
8127     current-prefix-arg
8128     t))
8129   (prog1
8130       (if (and (stringp article)
8131                (string-match "@\\|%40" article))
8132           (gnus-summary-refer-article article)
8133         (when (stringp article)
8134           (setq article (string-to-number article)))
8135         (if (gnus-summary-goto-subject article force)
8136             (gnus-summary-display-article article all-headers)
8137           (gnus-message 4 "Couldn't go to article %s" article) nil))
8138     (gnus-summary-position-point)))
8139
8140 (defun gnus-summary-goto-last-article ()
8141   "Go to the previously read article."
8142   (interactive)
8143   (prog1
8144       (when gnus-last-article
8145         (gnus-summary-goto-article gnus-last-article nil t))
8146     (gnus-summary-position-point)))
8147
8148 (defun gnus-summary-pop-article (number)
8149   "Pop one article off the history and go to the previous.
8150 NUMBER articles will be popped off."
8151   (interactive "p")
8152   (let (to)
8153     (setq gnus-newsgroup-history
8154           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8155     (if to
8156         (gnus-summary-goto-article (car to) nil t)
8157       (error "Article history empty")))
8158   (gnus-summary-position-point))
8159
8160 ;; Summary commands and functions for limiting the summary buffer.
8161
8162 (defun gnus-summary-limit-to-articles (n)
8163   "Limit the summary buffer to the next N articles.
8164 If not given a prefix, use the process marked articles instead."
8165   (interactive "P")
8166   (prog1
8167       (let ((articles (gnus-summary-work-articles n)))
8168         (setq gnus-newsgroup-processable nil)
8169         (gnus-summary-limit articles))
8170     (gnus-summary-position-point)))
8171
8172 (defun gnus-summary-pop-limit (&optional total)
8173   "Restore the previous limit.
8174 If given a prefix, remove all limits."
8175   (interactive "P")
8176   (when total
8177     (setq gnus-newsgroup-limits
8178           (list (mapcar (lambda (h) (mail-header-number h))
8179                         gnus-newsgroup-headers))))
8180   (unless gnus-newsgroup-limits
8181     (error "No limit to pop"))
8182   (prog1
8183       (gnus-summary-limit nil 'pop)
8184     (gnus-summary-position-point)))
8185
8186 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8187   "Limit the summary buffer to articles that have subjects that match a regexp.
8188 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8189   (interactive
8190    (list (read-string (if current-prefix-arg
8191                           "Exclude subject (regexp): "
8192                         "Limit to subject (regexp): "))
8193          nil current-prefix-arg))
8194   (unless header
8195     (setq header "subject"))
8196   (when (not (equal "" subject))
8197     (prog1
8198         (let ((articles (gnus-summary-find-matching
8199                          (or header "subject") subject 'all nil nil
8200                          not-matching)))
8201           (unless articles
8202             (error "Found no matches for \"%s\"" subject))
8203           (gnus-summary-limit articles))
8204       (gnus-summary-position-point))))
8205
8206 (defun gnus-summary-limit-to-author (from &optional not-matching)
8207   "Limit the summary buffer to articles that have authors that match a regexp.
8208 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8209   (interactive
8210    (list (let* ((header (gnus-summary-article-header))
8211                 (default (and header (car (mail-header-parse-address
8212                                            (mail-header-from header))))))
8213            (read-string (concat (if current-prefix-arg
8214                                     "Exclude author (regexp"
8215                                   "Limit to author (regexp")
8216                                 (if default
8217                                     (concat ", default \"" default "\"): ")
8218                                   "): "))
8219                         nil nil
8220                         default))
8221          current-prefix-arg))
8222   (gnus-summary-limit-to-subject from "from" not-matching))
8223
8224 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8225   "Limit the summary buffer to articles with the given RECIPIENT.
8226
8227 If NOT-MATCHING, exclude RECIPIENT.
8228
8229 To and Cc headers are checked.  You need to include them in
8230 `nnmail-extra-headers'."
8231   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8232   (interactive
8233    (list (read-string (format "%s recipient (regexp): "
8234                               (if current-prefix-arg "Exclude" "Limit to")))
8235          current-prefix-arg))
8236   (when (not (equal "" recipient))
8237     (prog1 (let* ((to
8238                    (if (memq 'To nnmail-extra-headers)
8239                        (gnus-summary-find-matching
8240                         (cons 'extra 'To) recipient 'all nil nil
8241                         not-matching)
8242                      (gnus-message
8243                       1 "`To' isn't present in `nnmail-extra-headers'")
8244                      (sit-for 1)
8245                      nil))
8246                   (cc
8247                    (if (memq 'Cc nnmail-extra-headers)
8248                        (gnus-summary-find-matching
8249                         (cons 'extra 'Cc) recipient 'all nil nil
8250                         not-matching)
8251                      (gnus-message
8252                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8253                      (sit-for 1)
8254                      nil))
8255                   (articles
8256                    (if not-matching
8257                        ;; We need the numbers that are in both lists:
8258                        (mapcar (lambda (a)
8259                                  (and (memq a to) a))
8260                                cc)
8261                      (nconc to cc))))
8262              (unless articles
8263                (error "Found no matches for \"%s\"" recipient))
8264              (gnus-summary-limit articles))
8265       (gnus-summary-position-point))))
8266
8267 (defun gnus-summary-limit-to-address (address &optional not-matching)
8268   "Limit the summary buffer to articles with the given ADDRESS.
8269
8270 If NOT-MATCHING, exclude ADDRESS.
8271
8272 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8273 in `nnmail-extra-headers'."
8274   (interactive
8275    (list (read-string (format "%s address (regexp): "
8276                               (if current-prefix-arg "Exclude" "Limit to")))
8277          current-prefix-arg))
8278   (when (not (equal "" address))
8279     (prog1 (let* ((to
8280                    (if (memq 'To nnmail-extra-headers)
8281                        (gnus-summary-find-matching
8282                         (cons 'extra 'To) address 'all nil nil
8283                         not-matching)
8284                      (gnus-message
8285                       1 "`To' isn't present in `nnmail-extra-headers'")
8286                      (sit-for 1)
8287                      t))
8288                   (cc
8289                    (if (memq 'Cc nnmail-extra-headers)
8290                        (gnus-summary-find-matching
8291                         (cons 'extra 'Cc) address 'all nil nil
8292                         not-matching)
8293                      (gnus-message
8294                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8295                      (sit-for 1)
8296                      t))
8297                   (from
8298                    (gnus-summary-find-matching "from" address
8299                                                'all nil nil not-matching))
8300                   (articles
8301                    (if not-matching
8302                        ;; We need the numbers that are in all lists:
8303                        (if (eq cc t)
8304                            (if (eq to t)
8305                                from
8306                              (mapcar (lambda (a) (car (memq a from))) to))
8307                          (if (eq to t)
8308                              (mapcar (lambda (a) (car (memq a from))) cc)
8309                            (mapcar (lambda (a) (car (memq a from)))
8310                                    (mapcar (lambda (a) (car (memq a to)))
8311                                            cc))))
8312                      (nconc (if (eq to t) nil to)
8313                             (if (eq cc t) nil cc)
8314                             from))))
8315              (unless articles
8316                (error "Found no matches for \"%s\"" address))
8317              (gnus-summary-limit articles))
8318       (gnus-summary-position-point))))
8319
8320 (defun gnus-summary-limit-strange-charsets-predicate (header)
8321   (when (fboundp 'char-charset)
8322     (let ((string (concat (mail-header-subject header)
8323                           (mail-header-from header)))
8324           charset found)
8325       (dotimes (i (1- (length string)))
8326         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8327         (when (string-match "unicode\\|big\\|japanese" charset)
8328           (setq found t)))
8329       found)))
8330
8331 (defun gnus-summary-limit-to-predicate (predicate)
8332   "Limit to articles where PREDICATE returns non-nil.
8333 PREDICATE will be called with the header structures of the
8334 articles."
8335   (let ((articles nil)
8336         (case-fold-search t))
8337     (dolist (header gnus-newsgroup-headers)
8338       (when (funcall predicate header)
8339         (push (mail-header-number header) articles)))
8340     (gnus-summary-limit (nreverse articles))))
8341
8342 (defun gnus-summary-limit-to-age (age &optional younger-p)
8343   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8344 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8345 articles that are younger than AGE days."
8346   (interactive
8347    (let ((younger current-prefix-arg)
8348          (days-got nil)
8349          days)
8350      (while (not days-got)
8351        (setq days (if younger
8352                       (read-string "Limit to articles younger than (in days, older when negative): ")
8353                     (read-string
8354                      "Limit to articles older than (in days, younger when negative): ")))
8355        (when (> (length days) 0)
8356          (setq days (read days)))
8357        (if (numberp days)
8358            (progn
8359              (setq days-got t)
8360              (when (< days 0)
8361                (setq younger (not younger))
8362                (setq days (* days -1))))
8363          (message "Please enter a number.")
8364          (sleep-for 1)))
8365      (list days younger)))
8366   (prog1
8367       (let ((data gnus-newsgroup-data)
8368             (cutoff (days-to-time age))
8369             articles d date is-younger)
8370         (while (setq d (pop data))
8371           (when (and (vectorp (gnus-data-header d))
8372                      (setq date (mail-header-date (gnus-data-header d))))
8373             (setq is-younger (time-less-p
8374                               (time-since (gnus-date-get-time date))
8375                               cutoff))
8376             (when (if younger-p
8377                       is-younger
8378                     (not is-younger))
8379               (push (gnus-data-number d) articles))))
8380         (gnus-summary-limit (nreverse articles)))
8381     (gnus-summary-position-point)))
8382
8383 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8384   "Limit the summary buffer to articles that match an 'extra' header."
8385   (interactive
8386    (let ((header
8387           (intern
8388            (gnus-completing-read
8389             (if current-prefix-arg
8390                 "Exclude extra header"
8391               "Limit extra header")
8392             (mapcar 'symbol-name gnus-extra-headers)
8393             t nil nil
8394             (symbol-name (car gnus-extra-headers))))))
8395      (list header
8396            (read-string (format "%s header %s (regexp): "
8397                                 (if current-prefix-arg "Exclude" "Limit to")
8398                                 header))
8399            current-prefix-arg)))
8400   (when (not (equal "" regexp))
8401     (prog1
8402         (let ((articles (gnus-summary-find-matching
8403                          (cons 'extra header) regexp 'all nil nil
8404                          not-matching)))
8405           (unless articles
8406             (error "Found no matches for \"%s\"" regexp))
8407           (gnus-summary-limit articles))
8408       (gnus-summary-position-point))))
8409
8410 (defun gnus-summary-limit-to-display-predicate ()
8411   "Limit the summary buffer to the predicated in the `display' group parameter."
8412   (interactive)
8413   (unless gnus-newsgroup-display
8414     (error "There is no `display' group parameter"))
8415   (let (articles)
8416     (dolist (gnus-number gnus-newsgroup-articles)
8417       (when (funcall gnus-newsgroup-display)
8418         (push gnus-number articles)))
8419     (gnus-summary-limit articles))
8420   (gnus-summary-position-point))
8421
8422 (defun gnus-summary-limit-to-unread (&optional all)
8423   "Limit the summary buffer to articles that are not marked as read.
8424 If ALL is non-nil, limit strictly to unread articles."
8425   (interactive "P")
8426   (if all
8427       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8428     (gnus-summary-limit-to-marks
8429      ;; Concat all the marks that say that an article is read and have
8430      ;; those removed.
8431      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8432            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8433            gnus-low-score-mark gnus-expirable-mark
8434            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8435            gnus-duplicate-mark)
8436      'reverse)))
8437
8438 (defun gnus-summary-limit-to-headers (match &optional reverse)
8439   "Limit the summary buffer to articles that have headers that match MATCH.
8440 If REVERSE (the prefix), limit to articles that don't match."
8441   (interactive "sMatch headers (regexp): \nP")
8442   (gnus-summary-limit-to-bodies match reverse t))
8443
8444 (declare-function article-goto-body "gnus-art" ())
8445
8446 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8447   "Limit the summary buffer to articles that have bodies that match MATCH.
8448 If REVERSE (the prefix), limit to articles that don't match."
8449   (interactive "sMatch body (regexp): \nP")
8450   (let ((articles nil)
8451         (gnus-select-article-hook nil)  ;Disable hook.
8452         (gnus-article-prepare-hook nil)
8453         (gnus-use-article-prefetch nil)
8454         (gnus-keep-backlog nil)
8455         (gnus-break-pages nil)
8456         (gnus-summary-display-arrow nil)
8457         (gnus-updated-mode-lines nil)
8458         (gnus-auto-center-summary nil)
8459         (gnus-display-mime-function nil))
8460     (dolist (data gnus-newsgroup-data)
8461       (let (gnus-mark-article-hook)
8462         (gnus-summary-select-article t t nil (gnus-data-number data)))
8463       (with-current-buffer gnus-article-buffer
8464         (article-goto-body)
8465         (let* ((case-fold-search t)
8466                (found (if headersp
8467                           (re-search-backward match nil t)
8468                         (re-search-forward match nil t))))
8469           (when (or (and found
8470                          (not reverse))
8471                     (and (not found)
8472                          reverse))
8473             (push (gnus-data-number data) articles)))))
8474     (if (not articles)
8475         (message "No messages matched")
8476       (gnus-summary-limit articles)))
8477   (gnus-summary-position-point))
8478
8479 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8480   "Limit the summary buffer to articles that aren't part on any thread.
8481 If THREADSP (the prefix), limit to articles that are in threads."
8482   (interactive "P")
8483   (let ((articles nil)
8484         thread-articles
8485         threads)
8486     (dolist (thread gnus-newsgroup-threads)
8487       (if (stringp (car thread))
8488           (dolist (thread (cdr thread))
8489             (push thread threads))
8490         (push thread threads)))
8491     (dolist (thread threads)
8492       (setq thread-articles (gnus-articles-in-thread thread))
8493       (when (or (and threadsp
8494                      (> (length thread-articles) 1))
8495                 (and (not threadsp)
8496                      (= (length thread-articles) 1)))
8497         (setq articles (nconc thread-articles articles))))
8498     (if (not articles)
8499         (message "No messages matched")
8500       (gnus-summary-limit articles))
8501     (gnus-summary-position-point)))
8502
8503 (defun gnus-summary-limit-to-replied (&optional unreplied)
8504   "Limit the summary buffer to replied articles.
8505 If UNREPLIED (the prefix), limit to unreplied articles."
8506   (interactive "P")
8507   (if unreplied
8508       (gnus-summary-limit
8509        (gnus-set-difference gnus-newsgroup-articles
8510         gnus-newsgroup-replied))
8511     (gnus-summary-limit gnus-newsgroup-replied))
8512   (gnus-summary-position-point))
8513
8514 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8515   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8516 If REVERSE, limit the summary buffer to articles that are marked
8517 with MARKS.  MARKS can either be a string of marks or a list of marks.
8518 Returns how many articles were removed."
8519   (interactive "sMarks: ")
8520   (gnus-summary-limit-to-marks marks t))
8521
8522 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8523   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8524 If REVERSE (the prefix), limit the summary buffer to articles that are
8525 not marked with MARKS.  MARKS can either be a string of marks or a
8526 list of marks.
8527 Returns how many articles were removed."
8528   (interactive "sMarks: \nP")
8529   (prog1
8530       (let ((data gnus-newsgroup-data)
8531             (marks (if (listp marks) marks
8532                      (append marks nil))) ; Transform to list.
8533             articles)
8534         (while data
8535           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8536                   (memq (gnus-data-mark (car data)) marks))
8537             (push (gnus-data-number (car data)) articles))
8538           (setq data (cdr data)))
8539         (gnus-summary-limit articles))
8540     (gnus-summary-position-point)))
8541
8542 (defun gnus-summary-limit-to-score (score)
8543   "Limit to articles with score at or above SCORE."
8544   (interactive "NLimit to articles with score of at least: ")
8545   (let ((data gnus-newsgroup-data)
8546         articles)
8547     (while data
8548       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8549                 score)
8550         (push (gnus-data-number (car data)) articles))
8551       (setq data (cdr data)))
8552     (prog1
8553         (gnus-summary-limit articles)
8554       (gnus-summary-position-point))))
8555
8556 (defun gnus-summary-limit-to-unseen ()
8557   "Limit to unseen articles."
8558   (interactive)
8559   (prog1
8560       (gnus-summary-limit gnus-newsgroup-unseen)
8561     (gnus-summary-position-point)))
8562
8563 (defun gnus-summary-limit-include-thread (id)
8564   "Display all the hidden articles that is in the thread with ID in it.
8565 When called interactively, ID is the Message-ID of the current
8566 article."
8567   (interactive (list (mail-header-id (gnus-summary-article-header))))
8568   (let ((articles (gnus-articles-in-thread
8569                    (gnus-id-to-thread (gnus-root-id id))))
8570         ;;we REALLY want the whole thread---this prevents cut-threads
8571         ;;from removing the thread we want to include.
8572         (gnus-fetch-old-headers nil)
8573         (gnus-build-sparse-threads nil))
8574     (prog1
8575         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8576       (gnus-summary-limit-include-matching-articles
8577        "subject"
8578        (regexp-quote (gnus-simplify-subject-re
8579                       (mail-header-subject (gnus-id-to-header id)))))
8580       (gnus-summary-position-point))))
8581
8582 (defun gnus-summary-limit-include-matching-articles (header regexp)
8583   "Display all the hidden articles that have HEADERs that match REGEXP."
8584   (interactive (list (read-string "Match on header: ")
8585                      (read-string "Regexp: ")))
8586   (let ((articles (gnus-find-matching-articles header regexp)))
8587     (prog1
8588         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8589       (gnus-summary-position-point))))
8590
8591 (defun gnus-summary-insert-dormant-articles ()
8592   "Insert all the dormant articles for this group into the current buffer."
8593   (interactive)
8594   (let ((gnus-verbose (max 6 gnus-verbose)))
8595     (if (not gnus-newsgroup-dormant)
8596         (gnus-message 3 "No dormant articles for this group")
8597       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8598
8599 (defun gnus-summary-insert-ticked-articles ()
8600   "Insert ticked articles for this group into the current buffer."
8601   (interactive)
8602   (let ((gnus-verbose (max 6 gnus-verbose)))
8603     (if (not gnus-newsgroup-marked)
8604         (gnus-message 3 "No ticked articles for this group")
8605       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8606
8607 (defun gnus-summary-limit-include-dormant ()
8608   "Display all the hidden articles that are marked as dormant.
8609 Note that this command only works on a subset of the articles currently
8610 fetched for this group."
8611   (interactive)
8612   (unless gnus-newsgroup-dormant
8613     (error "There are no dormant articles in this group"))
8614   (prog1
8615       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8616     (gnus-summary-position-point)))
8617
8618 (defun gnus-summary-include-articles (articles)
8619   "Fetch the headers for ARTICLES and then display the summary lines."
8620   (let ((gnus-inhibit-demon t)
8621         (gnus-agent nil)
8622         (gnus-read-all-available-headers t))
8623     (setq gnus-newsgroup-headers
8624           (gnus-merge
8625            'list gnus-newsgroup-headers
8626            (gnus-fetch-headers articles nil t)
8627            'gnus-article-sort-by-number))
8628     (setq gnus-newsgroup-articles
8629           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8630     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8631
8632 (defun gnus-summary-limit-exclude-dormant ()
8633   "Hide all dormant articles."
8634   (interactive)
8635   (prog1
8636       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8637     (gnus-summary-position-point)))
8638
8639 (defun gnus-summary-limit-exclude-childless-dormant ()
8640   "Hide all dormant articles that have no children."
8641   (interactive)
8642   (let ((data (gnus-data-list t))
8643         articles d children)
8644     ;; Find all articles that are either not dormant or have
8645     ;; children.
8646     (while (setq d (pop data))
8647       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8648                 (and (setq children
8649                            (gnus-article-children (gnus-data-number d)))
8650                      (let (found)
8651                        (while children
8652                          (when (memq (car children) articles)
8653                            (setq children nil
8654                                  found t))
8655                          (pop children))
8656                        found)))
8657         (push (gnus-data-number d) articles)))
8658     ;; Do the limiting.
8659     (prog1
8660         (gnus-summary-limit articles)
8661       (gnus-summary-position-point))))
8662
8663 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8664   "Mark all unread excluded articles as read.
8665 If ALL, mark even excluded ticked and dormants as read."
8666   (interactive "P")
8667   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8668   (let ((articles (gnus-sorted-ndifference
8669                    (sort
8670                     (mapcar (lambda (h) (mail-header-number h))
8671                             gnus-newsgroup-headers)
8672                     '<)
8673                    gnus-newsgroup-limit))
8674         article)
8675     (setq gnus-newsgroup-unreads
8676           (gnus-sorted-intersection gnus-newsgroup-unreads
8677                                     gnus-newsgroup-limit))
8678     (if all
8679         (setq gnus-newsgroup-dormant nil
8680               gnus-newsgroup-marked nil
8681               gnus-newsgroup-reads
8682               (nconc
8683                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8684                gnus-newsgroup-reads))
8685       (while (setq article (pop articles))
8686         (unless (or (memq article gnus-newsgroup-dormant)
8687                     (memq article gnus-newsgroup-marked))
8688           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8689
8690 (defun gnus-summary-limit (articles &optional pop)
8691   (if pop
8692       ;; We pop the previous limit off the stack and use that.
8693       (setq articles (car gnus-newsgroup-limits)
8694             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8695     ;; We use the new limit, so we push the old limit on the stack.
8696     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8697   ;; Set the limit.
8698   (setq gnus-newsgroup-limit articles)
8699   (let ((total (length gnus-newsgroup-data))
8700         (data (gnus-data-find-list (gnus-summary-article-number)))
8701         (gnus-summary-mark-below nil)   ; Inhibit this.
8702         found)
8703     ;; This will do all the work of generating the new summary buffer
8704     ;; according to the new limit.
8705     (gnus-summary-prepare)
8706     ;; Hide any threads, possibly.
8707     (gnus-summary-maybe-hide-threads)
8708     ;; Try to return to the article you were at, or one in the
8709     ;; neighborhood.
8710     (when data
8711       ;; We try to find some article after the current one.
8712       (while data
8713         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8714           (setq data nil
8715                 found t))
8716         (setq data (cdr data))))
8717     (unless found
8718       ;; If there is no data, that means that we were after the last
8719       ;; article.  The same goes when we can't find any articles
8720       ;; after the current one.
8721       (goto-char (point-max))
8722       (gnus-summary-find-prev))
8723     (gnus-set-mode-line 'summary)
8724     ;; We return how many articles were removed from the summary
8725     ;; buffer as a result of the new limit.
8726     (- total (length gnus-newsgroup-data))))
8727
8728 (defsubst gnus-invisible-cut-children (threads)
8729   (let ((num 0))
8730     (while threads
8731       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8732         (incf num))
8733       (pop threads))
8734     (< num 2)))
8735
8736 (defsubst gnus-cut-thread (thread)
8737   "Go forwards in the thread until we find an article that we want to display."
8738   (when (or (eq gnus-fetch-old-headers 'some)
8739             (eq gnus-fetch-old-headers 'invisible)
8740             (numberp gnus-fetch-old-headers)
8741             (eq gnus-build-sparse-threads 'some)
8742             (eq gnus-build-sparse-threads 'more))
8743     ;; Deal with old-fetched headers and sparse threads.
8744     (while (and
8745             thread
8746             (or
8747              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8748              (gnus-summary-article-ancient-p
8749               (mail-header-number (car thread))))
8750             (if (or (<= (length (cdr thread)) 1)
8751                     (eq gnus-fetch-old-headers 'invisible))
8752                 (setq gnus-newsgroup-limit
8753                       (delq (mail-header-number (car thread))
8754                             gnus-newsgroup-limit)
8755                       thread (cadr thread))
8756               (when (gnus-invisible-cut-children (cdr thread))
8757                 (let ((th (cdr thread)))
8758                   (while th
8759                     (if (memq (mail-header-number (caar th))
8760                               gnus-newsgroup-limit)
8761                         (setq thread (car th)
8762                               th nil)
8763                       (setq th (cdr th))))))))))
8764   thread)
8765
8766 (defun gnus-cut-threads (threads)
8767   "Cut off all uninteresting articles from the beginning of THREADS."
8768   (when (or (eq gnus-fetch-old-headers 'some)
8769             (eq gnus-fetch-old-headers 'invisible)
8770             (numberp gnus-fetch-old-headers)
8771             (eq gnus-build-sparse-threads 'some)
8772             (eq gnus-build-sparse-threads 'more))
8773     (let ((th threads))
8774       (while th
8775         (setcar th (gnus-cut-thread (car th)))
8776         (setq th (cdr th)))))
8777   ;; Remove nixed out threads.
8778   (delq nil threads))
8779
8780 (defun gnus-summary-initial-limit (&optional show-if-empty)
8781   "Figure out what the initial limit is supposed to be on group entry.
8782 This entails weeding out unwanted dormants, low-scored articles,
8783 fetch-old-headers verbiage, and so on."
8784   ;; Most groups have nothing to remove.
8785   (unless (or gnus-inhibit-limiting
8786               (and (null gnus-newsgroup-dormant)
8787                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8788                    (not (eq gnus-fetch-old-headers 'some))
8789                    (not (numberp gnus-fetch-old-headers))
8790                    (not (eq gnus-fetch-old-headers 'invisible))
8791                    (null gnus-summary-expunge-below)
8792                    (not (eq gnus-build-sparse-threads 'some))
8793                    (not (eq gnus-build-sparse-threads 'more))
8794                    (null gnus-thread-expunge-below)))
8795     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8796     (setq gnus-newsgroup-limit nil)
8797     (mapatoms
8798      (lambda (node)
8799        (unless (car (symbol-value node))
8800          ;; These threads have no parents -- they are roots.
8801          (let ((nodes (cdr (symbol-value node)))
8802                thread)
8803            (while nodes
8804              (if (and gnus-thread-expunge-below
8805                       (< (gnus-thread-total-score (car nodes))
8806                          gnus-thread-expunge-below))
8807                  (gnus-expunge-thread (pop nodes))
8808                (setq thread (pop nodes))
8809                (gnus-summary-limit-children thread))))))
8810      gnus-newsgroup-dependencies)
8811     ;; If this limitation resulted in an empty group, we might
8812     ;; pop the previous limit and use it instead.
8813     (when (and (not gnus-newsgroup-limit)
8814                show-if-empty)
8815       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8816     gnus-newsgroup-limit))
8817
8818 (defun gnus-summary-limit-children (thread)
8819   "Return 1 if this subthread is visible and 0 if it is not."
8820   ;; First we get the number of visible children to this thread.  This
8821   ;; is done by recursing down the thread using this function, so this
8822   ;; will really go down to a leaf article first, before slowly
8823   ;; working its way up towards the root.
8824   (when thread
8825     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8826            (children
8827            (if (cdr thread)
8828                (apply '+ (mapcar 'gnus-summary-limit-children
8829                                  (cdr thread)))
8830              0))
8831            (number (mail-header-number (car thread)))
8832            score)
8833       (if (and
8834            (not (memq number gnus-newsgroup-marked))
8835            (or
8836             ;; If this article is dormant and has absolutely no visible
8837             ;; children, then this article isn't visible.
8838             (and (memq number gnus-newsgroup-dormant)
8839                  (zerop children))
8840             ;; If this is "fetch-old-headered" and there is no
8841             ;; visible children, then we don't want this article.
8842             (and (or (eq gnus-fetch-old-headers 'some)
8843                      (numberp gnus-fetch-old-headers))
8844                  (gnus-summary-article-ancient-p number)
8845                  (zerop children))
8846             ;; If this is "fetch-old-headered" and `invisible', then
8847             ;; we don't want this article.
8848             (and (eq gnus-fetch-old-headers 'invisible)
8849                  (gnus-summary-article-ancient-p number))
8850             ;; If this is a sparsely inserted article with no children,
8851             ;; we don't want it.
8852             (and (eq gnus-build-sparse-threads 'some)
8853                  (gnus-summary-article-sparse-p number)
8854                  (zerop children))
8855             ;; If we use expunging, and this article is really
8856             ;; low-scored, then we don't want this article.
8857             (when (and gnus-summary-expunge-below
8858                        (< (setq score
8859                                 (or (cdr (assq number gnus-newsgroup-scored))
8860                                     gnus-summary-default-score))
8861                           gnus-summary-expunge-below))
8862               ;; We increase the expunge-tally here, but that has
8863               ;; nothing to do with the limits, really.
8864               (incf gnus-newsgroup-expunged-tally)
8865               ;; We also mark as read here, if that's wanted.
8866               (when (and gnus-summary-mark-below
8867                          (< score gnus-summary-mark-below))
8868                 (setq gnus-newsgroup-unreads
8869                       (delq number gnus-newsgroup-unreads))
8870                 (if gnus-newsgroup-auto-expire
8871                     (push number gnus-newsgroup-expirable)
8872                   (push (cons number gnus-low-score-mark)
8873                         gnus-newsgroup-reads)))
8874               t)
8875             ;; Do the `display' group parameter.
8876             (and gnus-newsgroup-display
8877                  (let ((gnus-number number))
8878                    (not (funcall gnus-newsgroup-display))))))
8879           ;; Nope, invisible article.
8880           0
8881         ;; Ok, this article is to be visible, so we add it to the limit
8882         ;; and return 1.
8883         (push number gnus-newsgroup-limit)
8884         1))))
8885
8886 (defun gnus-expunge-thread (thread)
8887   "Mark all articles in THREAD as read."
8888   (let* ((number (mail-header-number (car thread))))
8889     (incf gnus-newsgroup-expunged-tally)
8890     ;; We also mark as read here, if that's wanted.
8891     (setq gnus-newsgroup-unreads
8892           (delq number gnus-newsgroup-unreads))
8893     (if gnus-newsgroup-auto-expire
8894         (push number gnus-newsgroup-expirable)
8895       (push (cons number gnus-low-score-mark)
8896             gnus-newsgroup-reads)))
8897   ;; Go recursively through all subthreads.
8898   (mapcar 'gnus-expunge-thread (cdr thread)))
8899
8900 ;; Summary article oriented commands
8901
8902 (defun gnus-summary-refer-parent-article (n)
8903   "Refer parent article N times.
8904 If N is negative, go to ancestor -N instead.
8905 The difference between N and the number of articles fetched is returned."
8906   (interactive "p")
8907   (let ((skip 1)
8908         error header ref)
8909     (when (not (natnump n))
8910       (setq skip (abs n)
8911             n 1))
8912     (while (and (> n 0)
8913                 (not error))
8914       (setq header (gnus-summary-article-header))
8915       (if (and (eq (mail-header-number header)
8916                    (cdr gnus-article-current))
8917                (equal gnus-newsgroup-name
8918                       (car gnus-article-current)))
8919           ;; If we try to find the parent of the currently
8920           ;; displayed article, then we take a look at the actual
8921           ;; References header, since this is slightly more
8922           ;; reliable than the References field we got from the
8923           ;; server.
8924           (with-current-buffer gnus-original-article-buffer
8925             (nnheader-narrow-to-headers)
8926             (unless (setq ref (message-fetch-field "references"))
8927               (when (setq ref (message-fetch-field "in-reply-to"))
8928                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8929             (widen))
8930         (setq ref
8931               ;; It's not the current article, so we take a bet on
8932               ;; the value we got from the server.
8933               (mail-header-references header)))
8934       (if (and ref
8935                (not (equal ref "")))
8936           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8937             (gnus-message 1 "Couldn't find parent"))
8938         (gnus-message 1 "No references in article %d"
8939                       (gnus-summary-article-number))
8940         (setq error t))
8941       (decf n))
8942     (gnus-summary-position-point)
8943     n))
8944
8945 (defun gnus-summary-refer-references ()
8946   "Fetch all articles mentioned in the References header.
8947 Return the number of articles fetched."
8948   (interactive)
8949   (let ((ref (mail-header-references (gnus-summary-article-header)))
8950         (current (gnus-summary-article-number))
8951         (n 0))
8952     (if (or (not ref)
8953             (equal ref ""))
8954         (error "No References in the current article")
8955       ;; For each Message-ID in the References header...
8956       (while (string-match "<[^>]*>" ref)
8957         (incf n)
8958         ;; ... fetch that article.
8959         (gnus-summary-refer-article
8960          (prog1 (match-string 0 ref)
8961            (setq ref (substring ref (match-end 0))))))
8962       (gnus-summary-goto-subject current)
8963       (gnus-summary-position-point)
8964       n)))
8965
8966 (defun gnus-delete-duplicate-headers (headers)
8967   ;; First remove leading duplicates.
8968   (while (and (> (length headers) 1)
8969               (= (mail-header-number (car headers))
8970                  (mail-header-number (cadr headers))))
8971     (pop headers))
8972   ;; Then the rest.
8973   (let ((result headers))
8974     (while (> (length headers) 1)
8975       (if (= (mail-header-number (car headers))
8976              (mail-header-number (cadr headers)))
8977           (setcdr headers (cddr headers))
8978         (pop headers)))
8979     result))
8980
8981 (defun gnus-summary-refer-thread (&optional limit)
8982   "Fetch all articles in the current thread. For backends that
8983 know how to search for threads (currently only 'nnimap) a
8984 non-numeric prefix arg will use nnir to search the entire
8985 server; without a prefix arg only the current group is
8986 searched. If the variable `gnus-refer-thread-use-nnir' is
8987 non-nil the prefix arg has the reverse meaning. If no
8988 backend-specific 'request-thread function is available fetch
8989 LIMIT (the numerical prefix) old headers. If LIMIT is
8990 non-numeric or nil fetch the number specified by the
8991 `gnus-refer-thread-limit' variable."
8992   (interactive "P")
8993   (gnus-warp-to-article)
8994   (let* ((header (gnus-summary-article-header))
8995          (id (mail-header-id header))
8996          (gnus-inhibit-demon t)
8997          (gnus-summary-ignore-duplicates t)
8998          (gnus-read-all-available-headers t)
8999          (gnus-refer-thread-use-nnir
9000           (if (and (not (null limit)) (listp limit))
9001               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9002          (new-headers
9003           (if (gnus-check-backend-function
9004                'request-thread gnus-newsgroup-name)
9005               (gnus-request-thread header gnus-newsgroup-name)
9006             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9007                             gnus-refer-thread-limit))
9008                    (last (if (numberp limit)
9009                              (min (+ (mail-header-number header)
9010                                      limit)
9011                                   gnus-newsgroup-highest)
9012                            gnus-newsgroup-highest))
9013                    (subject (gnus-simplify-subject
9014                              (mail-header-subject header)))
9015                    (refs (split-string (or (mail-header-references header)
9016                                            "")))
9017                    (gnus-parse-headers-hook
9018                     `(lambda () (goto-char (point-min))
9019                       (keep-lines
9020                        (regexp-opt ',(append refs (list id subject)))))))
9021               (gnus-fetch-headers (list last) (if (numberp limit)
9022                                                   (* 2 limit) limit) t))))
9023          article-ids)
9024     (when (listp new-headers)
9025       (dolist (header new-headers)
9026         (push (mail-header-number header) article-ids)
9027         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9028           (push (mail-header-number header) gnus-newsgroup-unreads)
9029           (setq gnus-newsgroup-unselected
9030                 (delete (mail-header-number header)
9031                         gnus-newsgroup-unselected))))
9032       (setq gnus-newsgroup-headers
9033             (gnus-delete-duplicate-headers
9034              (gnus-merge
9035               'list gnus-newsgroup-headers new-headers
9036               'gnus-article-sort-by-number)))
9037       (setq gnus-newsgroup-articles
9038             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9039       (gnus-summary-limit-include-thread id)))
9040   (gnus-summary-show-thread))
9041
9042 (defun gnus-summary-refer-article (message-id)
9043   "Fetch an article specified by MESSAGE-ID."
9044   (interactive "sMessage-ID: ")
9045   (gnus-warp-to-article)
9046   (when (and (stringp message-id)
9047              (not (zerop (length message-id))))
9048     (setq message-id (gnus-replace-in-string message-id " " ""))
9049     ;; Construct the correct Message-ID if necessary.
9050     ;; Suggested by tale@pawl.rpi.edu.
9051     (unless (string-match "^<" message-id)
9052       (setq message-id (concat "<" message-id)))
9053     (unless (string-match ">$" message-id)
9054       (setq message-id (concat message-id ">")))
9055     ;; People often post MIDs from URLs, so unhex it:
9056     (unless (string-match "@" message-id)
9057       (setq message-id (gnus-url-unhex-string message-id)))
9058     (let* ((header (gnus-id-to-header message-id))
9059            (sparse (and header
9060                         (gnus-summary-article-sparse-p
9061                          (mail-header-number header))
9062                         (memq (mail-header-number header)
9063                               gnus-newsgroup-limit)))
9064            number)
9065       (cond
9066        ;; If the article is present in the buffer we just go to it.
9067        ((and header
9068              (or (not (gnus-summary-article-sparse-p
9069                        (mail-header-number header)))
9070                  sparse))
9071         (prog1
9072             (gnus-summary-goto-article
9073              (mail-header-number header) nil t)
9074           (when sparse
9075             (gnus-summary-update-article (mail-header-number header)))))
9076        (t
9077         ;; We fetch the article.
9078         (catch 'found
9079           (dolist (gnus-override-method (gnus-refer-article-methods))
9080             (when (and (gnus-check-server gnus-override-method)
9081                        ;; Fetch the header,
9082                        (setq number (gnus-summary-insert-subject message-id)))
9083               ;; and display the article.
9084               (gnus-summary-select-article nil nil nil number)
9085               (throw 'found t)))
9086           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9087
9088 (defun gnus-refer-article-methods ()
9089   "Return a list of referable methods."
9090   (cond
9091    ;; No method, so we default to current and native.
9092    ((null gnus-refer-article-method)
9093     (list gnus-current-select-method gnus-select-method))
9094    ;; Current.
9095    ((eq 'current gnus-refer-article-method)
9096     (list gnus-current-select-method))
9097    ;; List of select methods.
9098    ((not (and (symbolp (car gnus-refer-article-method))
9099               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9100     (let (out)
9101       (dolist (method gnus-refer-article-method)
9102         (push (if (eq 'current method)
9103                   gnus-current-select-method
9104                 (if (eq 'nnir (car method))
9105                     (list
9106                      'nnir
9107                      (or (cadr method)
9108                          (gnus-method-to-server gnus-current-select-method)))
9109                   method))
9110               out))
9111       (nreverse out)))
9112    ;; One single select method.
9113    (t
9114     (list gnus-refer-article-method))))
9115
9116 (defun gnus-summary-edit-parameters ()
9117   "Edit the group parameters of the current group."
9118   (interactive)
9119   (gnus-group-edit-group gnus-newsgroup-name 'params))
9120
9121 (defun gnus-summary-customize-parameters ()
9122   "Customize the group parameters of the current group."
9123   (interactive)
9124   (gnus-group-customize gnus-newsgroup-name))
9125
9126 (defun gnus-summary-enter-digest-group (&optional force)
9127   "Enter an nndoc group based on the current article.
9128 If FORCE, force a digest interpretation.  If not, try to guess
9129 what the document format is.
9130
9131 To control what happens when you exit the group, see the
9132 `gnus-auto-select-on-ephemeral-exit' variable."
9133   (interactive "P")
9134   (let ((conf gnus-current-window-configuration))
9135     (save-window-excursion
9136       (save-excursion
9137         (let (gnus-article-prepare-hook
9138               gnus-display-mime-function
9139               gnus-break-pages)
9140           (gnus-summary-select-article))))
9141     (setq gnus-current-window-configuration conf)
9142     (let* ((name (format "%s-%d"
9143                          (gnus-group-prefixed-name
9144                           gnus-newsgroup-name (list 'nndoc ""))
9145                          (with-current-buffer gnus-summary-buffer
9146                            gnus-current-article)))
9147            (ogroup gnus-newsgroup-name)
9148            (params (append (gnus-info-params (gnus-get-info ogroup))
9149                            (list (cons 'to-group ogroup))
9150                            (list (cons 'parent-group ogroup))
9151                            (list (cons 'save-article-group ogroup))))
9152            (case-fold-search t)
9153            (buf (current-buffer))
9154            dig to-address charset)
9155       (with-current-buffer gnus-original-article-buffer
9156         ;; Have the digest group inherit the main mail address of
9157         ;; the parent article.
9158         (when (setq to-address (or (gnus-fetch-field "reply-to")
9159                                    (gnus-fetch-field "from")))
9160           (setq params
9161                 (append
9162                  (list (cons 'to-address
9163                              (funcall gnus-decode-encoded-address-function
9164                                       to-address))))))
9165         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9166         (insert-buffer-substring gnus-original-article-buffer)
9167         (narrow-to-region
9168          (goto-char (point-min))
9169          (or (search-forward "\n\n" nil t) (point)))
9170         ;; Remove lines that may lead nndoc to misinterpret the
9171         ;; document type.
9172         (goto-char (point-min))
9173         (delete-matching-lines "^Path:\\|^From ")
9174         ;; Parse charset, and decode content transfer encoding.
9175         (setq charset (mail-content-type-get
9176                        (mail-header-parse-content-type
9177                         (or (gnus-fetch-field "content-type") ""))
9178                        'charset))
9179         (let ((encoding (gnus-fetch-field "content-transfer-encoding")))
9180           (when encoding
9181             (message-remove-header "content-transfer-encoding")
9182             (goto-char (point-max))
9183             (widen)
9184             (narrow-to-region (point) (point-max))
9185             (mm-decode-content-transfer-encoding
9186              (intern (downcase (mail-header-strip encoding))))))
9187         (widen))
9188       (unwind-protect
9189           (if (let ((gnus-newsgroup-ephemeral-charset
9190                      (if charset
9191                          (intern (downcase (gnus-strip-whitespace charset)))
9192                        gnus-newsgroup-charset))
9193                     (gnus-newsgroup-ephemeral-ignored-charsets
9194                      gnus-newsgroup-ignored-charsets))
9195                 (gnus-group-read-ephemeral-group
9196                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9197                               (nndoc-article-type
9198                                ,(if force 'mbox 'guess)))
9199                  t nil nil nil
9200                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9201                                                         "ADAPT")))))
9202               ;; Make all postings to this group go to the parent group.
9203               (nconc (gnus-info-params (gnus-get-info name))
9204                      params)
9205             ;; Couldn't select this doc group.
9206             (switch-to-buffer buf)
9207             (gnus-set-global-variables)
9208             (gnus-configure-windows 'summary)
9209             (gnus-message 3 "Article couldn't be entered?"))
9210         (kill-buffer dig)))))
9211
9212 (defun gnus-summary-read-document (n)
9213   "Open a new group based on the current article(s).
9214 This will allow you to read digests and other similar
9215 documents as newsgroups.
9216 Obeys the standard process/prefix convention."
9217   (interactive "P")
9218   (let* ((ogroup gnus-newsgroup-name)
9219          (params (append (gnus-info-params (gnus-get-info ogroup))
9220                          (list (cons 'to-group ogroup))))
9221          group egroup groups vgroup)
9222     (dolist (article (gnus-summary-work-articles n))
9223       (setq group (format "%s-%d" gnus-newsgroup-name article))
9224       (gnus-summary-remove-process-mark article)
9225       (when (gnus-summary-display-article article)
9226         (save-excursion ;;What for?
9227           (with-temp-buffer
9228             (insert-buffer-substring gnus-original-article-buffer)
9229             ;; Remove some headers that may lead nndoc to make
9230             ;; the wrong guess.
9231             (message-narrow-to-head)
9232             (goto-char (point-min))
9233             (delete-matching-lines "^Path:\\|^From ")
9234             (widen)
9235             (if (setq egroup
9236                       (gnus-group-read-ephemeral-group
9237                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9238                                      (nndoc-article-type guess))
9239                        t nil t))
9240                 (progn
9241                   ;; Make all postings to this group go to the parent group.
9242                   (nconc (gnus-info-params (gnus-get-info egroup))
9243                          params)
9244                   (push egroup groups))
9245               ;; Couldn't select this doc group.
9246               (gnus-error 3 "Article couldn't be entered"))))))
9247     ;; Now we have selected all the documents.
9248     (cond
9249      ((not groups)
9250       (error "None of the articles could be interpreted as documents"))
9251      ((gnus-group-read-ephemeral-group
9252        (setq vgroup (format
9253                      "nnvirtual:%s-%s" gnus-newsgroup-name
9254                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9255        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9256        t
9257        (cons (current-buffer) 'summary)))
9258      (t
9259       (error "Couldn't select virtual nndoc group")))))
9260
9261 (defun gnus-summary-widget-forward (arg)
9262   "Move point to the next field or button in the article.
9263 With optional ARG, move across that many fields."
9264   (interactive "p")
9265   (gnus-summary-select-article)
9266   (gnus-configure-windows 'article)
9267   (select-window (gnus-get-buffer-window gnus-article-buffer))
9268   (widget-forward arg))
9269
9270 (defun gnus-summary-widget-backward (arg)
9271   "Move point to the previous field or button in the article.
9272 With optional ARG, move across that many fields."
9273   (interactive "p")
9274   (gnus-summary-select-article)
9275   (gnus-configure-windows 'article)
9276   (select-window (gnus-get-buffer-window gnus-article-buffer))
9277   (unless (widget-at (point))
9278     (goto-char (point-max)))
9279   (widget-backward arg))
9280
9281 (defun gnus-summary-isearch-article (&optional regexp-p)
9282   "Do incremental search forward on the current article.
9283 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9284   (interactive "P")
9285   (gnus-summary-select-article)
9286   (gnus-configure-windows 'article)
9287   (gnus-eval-in-buffer-window gnus-article-buffer
9288     (save-restriction
9289       (widen)
9290       (isearch-forward regexp-p))))
9291
9292 (defun gnus-summary-repeat-search-article-forward ()
9293   "Repeat the previous search forwards."
9294   (interactive)
9295   (unless gnus-last-search-regexp
9296     (error "No previous search"))
9297   (gnus-summary-search-article-forward gnus-last-search-regexp))
9298
9299 (defun gnus-summary-repeat-search-article-backward ()
9300   "Repeat the previous search backwards."
9301   (interactive)
9302   (unless gnus-last-search-regexp
9303     (error "No previous search"))
9304   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9305
9306 (defun gnus-summary-search-article-forward (regexp &optional backward)
9307   "Search for an article containing REGEXP forward.
9308 If BACKWARD, search backward instead."
9309   (interactive
9310    (list (read-string
9311           (format "Search article %s (regexp%s): "
9312                   (if current-prefix-arg "backward" "forward")
9313                   (if gnus-last-search-regexp
9314                       (concat ", default " gnus-last-search-regexp)
9315                     "")))
9316          current-prefix-arg))
9317   (if (string-equal regexp "")
9318       (setq regexp (or gnus-last-search-regexp ""))
9319     (setq gnus-last-search-regexp regexp)
9320     (setq gnus-article-before-search gnus-current-article))
9321   ;; Intentionally set gnus-last-article.
9322   (setq gnus-last-article gnus-article-before-search)
9323   (let ((gnus-last-article gnus-last-article))
9324     (if (gnus-summary-search-article regexp backward)
9325         (gnus-summary-show-thread)
9326       (signal 'search-failed (list regexp)))))
9327
9328 (defun gnus-summary-search-article-backward (regexp)
9329   "Search for an article containing REGEXP backward."
9330   (interactive
9331    (list (read-string
9332           (format "Search article backward (regexp%s): "
9333                   (if gnus-last-search-regexp
9334                       (concat ", default " gnus-last-search-regexp)
9335                     "")))))
9336   (gnus-summary-search-article-forward regexp 'backward))
9337
9338 (defun gnus-summary-search-article (regexp &optional backward)
9339   "Search for an article containing REGEXP.
9340 Optional argument BACKWARD means do search for backward.
9341 `gnus-select-article-hook' is not called during the search."
9342   ;; We have to require this here to make sure that the following
9343   ;; dynamic binding isn't shadowed by autoloading.
9344   (require 'gnus-async)
9345   (require 'gnus-art)
9346   (let ((gnus-select-article-hook nil)  ;Disable hook.
9347         (gnus-article-prepare-hook nil)
9348         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9349         (gnus-use-article-prefetch nil)
9350         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9351         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9352         (gnus-visual nil)
9353         (gnus-keep-backlog nil)
9354         (gnus-break-pages nil)
9355         (gnus-summary-display-arrow nil)
9356         (gnus-updated-mode-lines nil)
9357         (gnus-auto-center-summary nil)
9358         (sum (current-buffer))
9359         (gnus-display-mime-function nil)
9360         (found nil)
9361         point)
9362     (gnus-save-hidden-threads
9363       (gnus-summary-select-article)
9364       (set-buffer gnus-article-buffer)
9365       (goto-char (window-point (get-buffer-window (current-buffer))))
9366       (when backward
9367         (forward-line -1))
9368       (while (not found)
9369         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9370         (if (if backward
9371                 (re-search-backward regexp nil t)
9372               (re-search-forward regexp nil t))
9373             ;; We found the regexp.
9374             (progn
9375               (setq found 'found)
9376               (beginning-of-line)
9377               (set-window-start
9378                (get-buffer-window (current-buffer))
9379                (point))
9380               (forward-line 1)
9381               (set-window-point
9382                (get-buffer-window (current-buffer))
9383                (point))
9384               (set-buffer sum)
9385               (setq point (point)))
9386           ;; We didn't find it, so we go to the next article.
9387           (set-buffer sum)
9388           (setq found 'not)
9389           (while (eq found 'not)
9390             (if (not (if backward (gnus-summary-find-prev)
9391                        (gnus-summary-find-next)))
9392                 ;; No more articles.
9393                 (setq found t)
9394               ;; Select the next article and adjust point.
9395               (unless (gnus-summary-article-sparse-p
9396                        (gnus-summary-article-number))
9397                 (setq found nil)
9398                 (gnus-summary-select-article)
9399                 (set-buffer gnus-article-buffer)
9400                 (widen)
9401                 (goto-char (if backward (point-max) (point-min))))))))
9402       (gnus-message 7 ""))
9403     ;; Return whether we found the regexp.
9404     (when (eq found 'found)
9405       (goto-char point)
9406       (gnus-summary-show-thread)
9407       (gnus-summary-goto-subject gnus-current-article)
9408       (gnus-summary-position-point)
9409       t)))
9410
9411 (defun gnus-find-matching-articles (header regexp)
9412   "Return a list of all articles that match REGEXP on HEADER.
9413 This search includes all articles in the current group that Gnus has
9414 fetched headers for, whether they are displayed or not."
9415   (let ((articles nil)
9416         ;; Can't eta-reduce because it's a macro.
9417         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9418         (case-fold-search t))
9419     (dolist (header gnus-newsgroup-headers)
9420       (when (string-match regexp (funcall func header))
9421         (push (mail-header-number header) articles)))
9422     (nreverse articles)))
9423
9424 (defun gnus-summary-find-matching (header regexp &optional backward unread
9425                                           not-case-fold not-matching)
9426   "Return a list of all articles that match REGEXP on HEADER.
9427 The search stars on the current article and goes forwards unless
9428 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9429 If UNREAD is non-nil, only unread articles will
9430 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9431 in the comparisons. If NOT-MATCHING, return a list of all articles that
9432 not match REGEXP on HEADER."
9433   (let ((case-fold-search (not not-case-fold))
9434         articles d func)
9435     (if (consp header)
9436         (if (eq (car header) 'extra)
9437             (setq func
9438                   `(lambda (h)
9439                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9440                          "")))
9441           (error "%s is an invalid header" header))
9442       (unless (fboundp (intern (concat "mail-header-" header)))
9443         (error "%s is not a valid header" header))
9444       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9445     (dolist (d (if (eq backward 'all)
9446                    gnus-newsgroup-data
9447                  (gnus-data-find-list
9448                   (gnus-summary-article-number)
9449                   (gnus-data-list backward))))
9450       (when (and (or (not unread)       ; We want all articles...
9451                      (gnus-data-unread-p d)) ; Or just unreads.
9452                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9453                  (if not-matching
9454                      (not (string-match
9455                            regexp
9456                            (funcall func (gnus-data-header d))))
9457                    (string-match regexp
9458                                  (funcall func (gnus-data-header d)))))
9459         (push (gnus-data-number d) articles))) ; Success!
9460     (nreverse articles)))
9461
9462 (defun gnus-summary-execute-command (header regexp command &optional backward)
9463   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9464 If HEADER is an empty string (or nil), the match is done on the entire
9465 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9466   (interactive
9467    (list (let ((completion-ignore-case t))
9468            (gnus-completing-read
9469             "Header name"
9470             (mapcar 'symbol-name
9471                     (append
9472                      '(Number Subject From Lines Date
9473                        Message-ID Xref References Body)
9474                      gnus-extra-headers))
9475             'require-match))
9476          (read-string "Regexp: ")
9477          (read-key-sequence "Command: ")
9478          current-prefix-arg))
9479   (when (equal header "Body")
9480     (setq header ""))
9481   ;; Hidden thread subtrees must be searched as well.
9482   (gnus-summary-show-all-threads)
9483   ;; We don't want to change current point nor window configuration.
9484   (save-excursion
9485     (save-window-excursion
9486       (let (gnus-visual
9487             gnus-treat-strip-trailing-blank-lines
9488             gnus-treat-strip-leading-blank-lines
9489             gnus-treat-strip-multiple-blank-lines
9490             gnus-treat-hide-boring-headers
9491             gnus-treat-fold-newsgroups
9492             gnus-article-prepare-hook)
9493         (gnus-message 6 "Executing %s..." (key-description command))
9494         ;; We'd like to execute COMMAND interactively so as to give arguments.
9495         (gnus-execute header regexp
9496                       `(call-interactively ',(key-binding command))
9497                       backward)
9498         (gnus-message 6 "Executing %s...done" (key-description command))))))
9499
9500 (defun gnus-summary-beginning-of-article ()
9501   "Scroll the article back to the beginning."
9502   (interactive)
9503   (gnus-summary-select-article)
9504   (gnus-configure-windows 'article)
9505   (gnus-eval-in-buffer-window gnus-article-buffer
9506     (widen)
9507     (goto-char (point-min))
9508     (when gnus-break-pages
9509       (gnus-narrow-to-page))))
9510
9511 (defun gnus-summary-end-of-article ()
9512   "Scroll to the end of the article."
9513   (interactive)
9514   (gnus-summary-select-article)
9515   (gnus-configure-windows 'article)
9516   (gnus-eval-in-buffer-window gnus-article-buffer
9517     (widen)
9518     (goto-char (point-max))
9519     (recenter -3)
9520     (when gnus-break-pages
9521       (gnus-narrow-to-page))))
9522
9523 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9524   "Truncate to LEN and quote all \"(\"'s in STRING."
9525   (gnus-replace-in-string (if (and len (> (length string) len))
9526                               (substring string 0 len)
9527                             string)
9528                           "[()]" "\\\\\\&"))
9529
9530 (defun gnus-summary-print-article (&optional filename n)
9531   "Generate and print a PostScript image of the process-marked (mail) articles.
9532
9533 If used interactively, print the current article if none are
9534 process-marked.  With prefix arg, prompt the user for the name of the
9535 file to save in.
9536
9537 When used from Lisp, accept two optional args FILENAME and N.  N means
9538 to print the next N articles.  If N is negative, print the N previous
9539 articles.  If N is nil and articles have been marked with the process
9540 mark, print these instead.
9541
9542 If the optional first argument FILENAME is nil, send the image to the
9543 printer.  If FILENAME is a string, save the PostScript image in a file with
9544 that name.  If FILENAME is a number, prompt the user for the name of the file
9545 to save in."
9546   (interactive (list (ps-print-preprint current-prefix-arg)))
9547   (dolist (article (gnus-summary-work-articles n))
9548     (gnus-summary-select-article nil nil 'pseudo article)
9549     (gnus-eval-in-buffer-window gnus-article-buffer
9550       (gnus-print-buffer))
9551     (gnus-summary-remove-process-mark article))
9552   (ps-despool filename))
9553
9554 (defun gnus-print-buffer ()
9555   (let ((ps-left-header
9556          (list
9557           (concat "("
9558                   (gnus-summary-print-truncate-and-quote
9559                    (mail-header-subject gnus-current-headers)
9560                    66) ")")
9561           (concat "("
9562                   (gnus-summary-print-truncate-and-quote
9563                    (mail-header-from gnus-current-headers)
9564                    45) ")")))
9565         (ps-right-header
9566          (list
9567           "/pagenumberstring load"
9568           (concat "("
9569                   (mail-header-date gnus-current-headers) ")"))))
9570     (gnus-run-hooks 'gnus-ps-print-hook)
9571     (save-excursion
9572       (if ps-print-color-p
9573           (ps-spool-buffer-with-faces)
9574         (ps-spool-buffer)))))
9575
9576 (declare-function gnus-flush-original-article-buffer "gnus-art" ())
9577
9578 (defun gnus-summary-show-complete-article ()
9579   "Show a complete version of the current article.
9580 This is only useful if you're looking at a partial version of the
9581 article currently."
9582   (interactive)
9583   (let ((gnus-keep-backlog nil)
9584         (gnus-use-cache nil)
9585         (gnus-agent nil)
9586         (variable (intern
9587                    (format "%s-fetch-partial-articles"
9588                            (car (gnus-find-method-for-group
9589                                  gnus-newsgroup-name)))
9590                    obarray))
9591         old-val)
9592     (unwind-protect
9593         (progn
9594           (setq old-val (symbol-value variable))
9595           (set variable nil)
9596           (gnus-flush-original-article-buffer)
9597           (gnus-summary-show-article))
9598       (set variable old-val))))
9599
9600 (defun gnus-summary-show-article (&optional arg)
9601   "Force redisplaying of the current article.
9602 If ARG (the prefix) is a number, show the article with the charset
9603 defined in `gnus-summary-show-article-charset-alist', or the charset
9604 input.
9605 If ARG (the prefix) is non-nil and not a number, show the article,
9606 but without running any of the article treatment functions
9607 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9608 C-u g', show the raw article."
9609   (interactive "P")
9610   (cond
9611    ((numberp arg)
9612     (gnus-summary-show-article t)
9613     (let ((gnus-newsgroup-charset
9614            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9615                (mm-read-coding-system
9616                 "View as charset: " ;; actually it is coding system.
9617                 (with-current-buffer gnus-article-buffer
9618                   (mm-detect-coding-region (point) (point-max))))))
9619           (gnus-newsgroup-ignored-charsets 'gnus-all))
9620       (gnus-summary-select-article nil 'force)
9621       (let ((deps gnus-newsgroup-dependencies)
9622             head header lines)
9623         (with-current-buffer gnus-original-article-buffer
9624           (save-restriction
9625             (message-narrow-to-head)
9626             (setq head (buffer-string))
9627             (goto-char (point-min))
9628             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9629               (goto-char (point-max))
9630               (widen)
9631               (setq lines (1- (count-lines (point) (point-max))))))
9632           (with-temp-buffer
9633             (insert (format "211 %d Article retrieved.\n"
9634                             (cdr gnus-article-current)))
9635             (insert head)
9636             (if lines (insert (format "Lines: %d\n" lines)))
9637             (insert ".\n")
9638             (let ((nntp-server-buffer (current-buffer)))
9639               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9640         (gnus-data-set-header
9641          (gnus-data-find (cdr gnus-article-current))
9642          header)
9643         (gnus-summary-update-article-line
9644          (cdr gnus-article-current) header)
9645         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9646           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9647    ((not arg)
9648     ;; Select the article the normal way.
9649     (if (eq mm-text-html-renderer 'shr)
9650         (progn
9651           (require 'shr)
9652           (let ((shr-ignore-cache t))
9653             (gnus-summary-select-article nil 'force)))
9654       (gnus-summary-select-article nil 'force)))
9655    ((equal arg '(16))
9656     ;; C-u C-u g
9657     (let ((gnus-inhibit-article-treatments t))
9658       (gnus-summary-select-article nil 'force)))
9659    (t
9660     ;; We have to require this here to make sure that the following
9661     ;; dynamic binding isn't shadowed by autoloading.
9662     (require 'gnus-async)
9663     (require 'gnus-art)
9664     ;; Bind the article treatment functions to nil.
9665     (let ((gnus-have-all-headers t)
9666           gnus-article-prepare-hook
9667           gnus-article-decode-hook
9668           gnus-display-mime-function
9669           gnus-break-pages)
9670       ;; Destroy any MIME parts.
9671       (when (gnus-buffer-live-p gnus-article-buffer)
9672         (with-current-buffer gnus-article-buffer
9673           (gnus-article-stop-animations)
9674           (gnus-stop-downloads)
9675           (mm-destroy-parts gnus-article-mime-handles)
9676           ;; Set it to nil for safety reason.
9677           (setq gnus-article-mime-handle-alist nil)
9678           (setq gnus-article-mime-handles nil)))
9679       (gnus-summary-select-article nil 'force))))
9680   (gnus-summary-goto-subject gnus-current-article)
9681   (gnus-summary-position-point))
9682
9683 (defun gnus-summary-show-raw-article ()
9684   "Show the raw article without any article massaging functions being run."
9685   (interactive)
9686   (gnus-summary-show-article t))
9687
9688 (defun gnus-summary-verbose-headers (&optional arg)
9689   "Toggle permanent full header display.
9690 If ARG is a positive number, turn header display on.
9691 If ARG is a negative number, turn header display off."
9692   (interactive "P")
9693   (setq gnus-show-all-headers
9694         (cond ((or (not (numberp arg))
9695                    (zerop arg))
9696                (not gnus-show-all-headers))
9697               ((natnump arg)
9698                t)))
9699   (gnus-summary-show-article))
9700
9701 (declare-function article-narrow-to-head "gnus-art" ())
9702 (declare-function gnus-article-hidden-text-p "gnus-art" (type))
9703 (declare-function gnus-delete-wash-type "gnus-art" (type))
9704
9705 (defun gnus-summary-toggle-header (&optional arg)
9706   "Show the headers if they are hidden, or hide them if they are shown.
9707 If ARG is a positive number, show the entire header.
9708 If ARG is a negative number, hide the unwanted header lines."
9709   (interactive "P")
9710   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9711                      (get-buffer-window gnus-article-buffer t))))
9712     (with-current-buffer gnus-article-buffer
9713       (widen)
9714       (article-narrow-to-head)
9715       (let* ((inhibit-read-only t)
9716              (inhibit-point-motion-hooks t)
9717              (hidden (if (numberp arg)
9718                          (>= arg 0)
9719                        (or (not (looking-at "[^ \t\n]+:"))
9720                            (gnus-article-hidden-text-p 'headers))))
9721              s e)
9722         (delete-region (point-min) (point-max))
9723         (with-current-buffer gnus-original-article-buffer
9724           (goto-char (setq s (point-min)))
9725           (setq e (if (search-forward "\n\n" nil t)
9726                       (1- (point))
9727                     (point-max))))
9728         (insert-buffer-substring gnus-original-article-buffer s e)
9729         (run-hooks 'gnus-article-decode-hook)
9730         (if hidden
9731             (let ((gnus-treat-hide-headers nil)
9732                   (gnus-treat-hide-boring-headers nil))
9733               (gnus-delete-wash-type 'headers)
9734               (gnus-treat-article 'head))
9735           (gnus-treat-article 'head))
9736         (widen)
9737         (if window
9738             (set-window-start window (goto-char (point-min))))
9739         (if gnus-break-pages
9740             (gnus-narrow-to-page)
9741           (when (gnus-visual-p 'page-marker)
9742             (let ((inhibit-read-only t))
9743               (gnus-remove-text-with-property 'gnus-prev)
9744               (gnus-remove-text-with-property 'gnus-next))))
9745         (gnus-set-mode-line 'article)))))
9746
9747 (defun gnus-summary-show-all-headers ()
9748   "Make all header lines visible."
9749   (interactive)
9750   (gnus-summary-toggle-header 1))
9751
9752 (defun gnus-summary-caesar-message (&optional arg)
9753   "Caesar rotate the current article by 13.
9754 With a non-numerical prefix, also rotate headers.  A numerical
9755 prefix specifies how many places to rotate each letter forward."
9756   (interactive "P")
9757   (gnus-summary-select-article)
9758   (let ((mail-header-separator ""))
9759     (gnus-eval-in-buffer-window gnus-article-buffer
9760       (save-restriction
9761         (widen)
9762         (let ((start (window-start))
9763               (inhibit-read-only t))
9764           (if (equal arg '(4))
9765               (message-caesar-buffer-body nil t)
9766             (message-caesar-buffer-body arg))
9767           (set-window-start (get-buffer-window (current-buffer)) start)))))
9768   ;; Create buttons and stuff...
9769   (gnus-treat-article nil))
9770
9771 (declare-function idna-to-unicode "ext:idna" (str))
9772
9773 (defun gnus-summary-idna-message (&optional arg)
9774   "Decode IDNA encoded domain names in the current articles.
9775 IDNA encoded domain names looks like `xn--bar'.  If a string
9776 remain unencoded after running this function, it is likely an
9777 invalid IDNA string (`xn--bar' is invalid).
9778
9779 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9780 installed for this command to work."
9781   (interactive "P")
9782   (if (not (and (condition-case nil (require 'idna)
9783                   (file-error))
9784                 (mm-coding-system-p 'utf-8)
9785                 (executable-find (symbol-value 'idna-program))))
9786       (gnus-message
9787        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9788     (gnus-summary-select-article)
9789     (let ((mail-header-separator ""))
9790       (gnus-eval-in-buffer-window gnus-article-buffer
9791         (save-restriction
9792           (widen)
9793           (let ((start (window-start))
9794                 buffer-read-only)
9795             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9796               (replace-match (idna-to-unicode (match-string 1))))
9797             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9798
9799 (defun gnus-summary-morse-message (&optional arg)
9800   "Morse decode the current article."
9801   (interactive "P")
9802   (gnus-summary-select-article)
9803   (let ((mail-header-separator ""))
9804     (gnus-eval-in-buffer-window gnus-article-buffer
9805       (save-excursion
9806         (save-restriction
9807           (widen)
9808           (let ((pos (window-start))
9809                 (inhibit-read-only t))
9810             (goto-char (point-min))
9811             (when (message-goto-body)
9812               (gnus-narrow-to-body))
9813             (goto-char (point-min))
9814             (while (search-forward "·" (point-max) t)
9815               (replace-match "."))
9816             (unmorse-region (point-min) (point-max))
9817             (widen)
9818             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9819
9820 (defun gnus-summary-stop-page-breaking ()
9821   "Stop page breaking in the current article."
9822   (interactive)
9823   (gnus-summary-select-article)
9824   (gnus-eval-in-buffer-window gnus-article-buffer
9825     (widen)
9826     (when (gnus-visual-p 'page-marker)
9827       (let ((inhibit-read-only t))
9828         (gnus-remove-text-with-property 'gnus-prev)
9829         (gnus-remove-text-with-property 'gnus-next))
9830       (setq gnus-page-broken nil))))
9831
9832 (defun gnus-summary-move-article (&optional n to-newsgroup
9833                                             select-method action)
9834   "Move the current article to a different newsgroup.
9835 If N is a positive number, move the N next articles.
9836 If N is a negative number, move the N previous articles.
9837 If N is nil and any articles have been marked with the process mark,
9838 move those articles instead.
9839 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9840 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9841 re-spool using this method.
9842
9843 When called interactively with TO-NEWSGROUP being nil, the value of
9844 the variable `gnus-move-split-methods' is used for finding a default
9845 for the target newsgroup.
9846
9847 For this function to work, both the current newsgroup and the
9848 newsgroup that you want to move to have to support the `request-move'
9849 and `request-accept' functions.
9850
9851 ACTION can be either `move' (the default), `crosspost' or `copy'."
9852   (interactive "P")
9853   (unless action
9854     (setq action 'move))
9855   ;; Check whether the source group supports the required functions.
9856   (cond ((and (eq action 'move)
9857               (not (gnus-check-backend-function
9858                     'request-move-article gnus-newsgroup-name)))
9859          (error "The current group does not support article moving"))
9860         ((and (eq action 'crosspost)
9861               (not (gnus-check-backend-function
9862                     'request-replace-article gnus-newsgroup-name)))
9863          (error "The current group does not support article editing")))
9864   (let ((articles (gnus-summary-work-articles n))
9865         (prefix (if (gnus-check-backend-function
9866                      'request-move-article gnus-newsgroup-name)
9867                     (funcall gnus-move-group-prefix-function
9868                              gnus-newsgroup-name)
9869                   ""))
9870         (names '((move "Move" "Moving")
9871                  (copy "Copy" "Copying")
9872                  (crosspost "Crosspost" "Crossposting")))
9873         (copy-buf (save-excursion
9874                     (nnheader-set-temp-buffer " *copy article*")))
9875         art-group to-method new-xref article to-groups
9876         articles-to-update-marks encoded)
9877     (unless (assq action names)
9878       (error "Unknown action %s" action))
9879     ;; Read the newsgroup name.
9880     (when (and (not to-newsgroup)
9881                (not select-method))
9882       (if (and gnus-move-split-methods
9883                (not
9884                 (and (memq gnus-current-article articles)
9885                      (gnus-buffer-live-p gnus-original-article-buffer))))
9886           ;; When `gnus-move-split-methods' is non-nil, we have to
9887           ;; select an article to give `gnus-read-move-group-name' an
9888           ;; opportunity to suggest an appropriate default.  However,
9889           ;; we needn't render or mark the article.
9890           (let ((gnus-display-mime-function nil)
9891                 (gnus-article-prepare-hook nil)
9892                 (gnus-mark-article-hook nil))
9893             (gnus-summary-select-article nil nil nil (car articles))))
9894       (setq to-newsgroup (gnus-read-move-group-name
9895                           (cadr (assq action names))
9896                           (symbol-value
9897                            (intern (format "gnus-current-%s-group" action)))
9898                           articles prefix)
9899             encoded to-newsgroup
9900             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9901       (set (intern (format "gnus-current-%s-group" action))
9902            (mm-decode-coding-string
9903             to-newsgroup
9904             (gnus-group-name-charset to-method to-newsgroup))))
9905     (unless to-method
9906       (setq to-method (or select-method
9907                           (gnus-server-to-method
9908                            (gnus-group-method to-newsgroup)))))
9909     (setq to-newsgroup
9910           (or encoded
9911               (and to-newsgroup
9912                    (mm-encode-coding-string
9913                     to-newsgroup
9914                     (gnus-group-name-charset to-method to-newsgroup)))))
9915     ;; Check the method we are to move this article to...
9916     (unless (gnus-check-backend-function
9917              'request-accept-article (car to-method))
9918       (error "%s does not support article copying" (car to-method)))
9919     (unless (gnus-check-server to-method)
9920       (error "Can't open server %s" (car to-method)))
9921     (gnus-message 6 "%s to %s: %s..."
9922                   (caddr (assq action names))
9923                   (or (car select-method)
9924                       (gnus-group-decoded-name to-newsgroup))
9925                   articles)
9926     (while articles
9927       (setq article (pop articles))
9928       ;; Set any marks that may have changed in the summary buffer.
9929       (when gnus-preserve-marks
9930         (gnus-summary-push-marks-to-backend article))
9931       (setq
9932        art-group
9933        (cond
9934         ;; Move the article.
9935         ((eq action 'move)
9936          ;; Remove this article from future suppression.
9937          (gnus-dup-unsuppress-article article)
9938          (let* ((from-method (gnus-find-method-for-group
9939                               gnus-newsgroup-name))
9940                 (to-method (or select-method
9941                                (gnus-find-method-for-group to-newsgroup)))
9942                 (move-is-internal (gnus-server-equal from-method to-method)))
9943            (gnus-request-move-article
9944             article                     ; Article to move
9945             gnus-newsgroup-name         ; From newsgroup
9946             (nth 1 (gnus-find-method-for-group
9947                     gnus-newsgroup-name)) ; Server
9948             (list 'gnus-request-accept-article
9949                   to-newsgroup (list 'quote select-method)
9950                   (not articles) t)     ; Accept form
9951             (not articles)              ; Only save nov last time
9952             (and move-is-internal
9953                  to-newsgroup           ; Not respooling
9954                                         ; Is this move internal?
9955                  (gnus-group-real-name to-newsgroup)))))
9956         ;; Copy the article.
9957         ((eq action 'copy)
9958          (with-current-buffer copy-buf
9959            (when (gnus-request-article-this-buffer article
9960                                                    gnus-newsgroup-name)
9961              (save-restriction
9962                (nnheader-narrow-to-headers)
9963                (dolist (hdr gnus-copy-article-ignored-headers)
9964                  (message-remove-header hdr t)))
9965              (gnus-request-accept-article
9966               to-newsgroup select-method (not articles) t))))
9967         ;; Crosspost the article.
9968         ((eq action 'crosspost)
9969          (let ((xref (message-tokenize-header
9970                       (mail-header-xref (gnus-summary-article-header
9971                                          article))
9972                       " ")))
9973            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9974                                   ":" (number-to-string article)))
9975            (unless xref
9976              (setq xref (list (system-name))))
9977            (setq new-xref
9978                  (concat
9979                   (mapconcat 'identity
9980                              (delete "Xref:" (delete new-xref xref))
9981                              " ")
9982                   " " new-xref))
9983            (with-current-buffer copy-buf
9984              ;; First put the article in the destination group.
9985              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9986              (when (consp (setq art-group
9987                                 (gnus-request-accept-article
9988                                  to-newsgroup select-method (not articles)
9989                                  t)))
9990                (setq new-xref (concat new-xref " " (car art-group)
9991                                       ":"
9992                                       (number-to-string (cdr art-group))))
9993                ;; Now we have the new Xrefs header, so we insert
9994                ;; it and replace the new article.
9995                (nnheader-replace-header "Xref" new-xref)
9996                (gnus-request-replace-article
9997                 (cdr art-group) to-newsgroup (current-buffer) t)
9998                art-group))))))
9999       (cond
10000        ((not art-group)
10001         (gnus-message 1 "Couldn't %s article %s: %s"
10002                       (cadr (assq action names)) article
10003                       (nnheader-get-report (car to-method))))
10004        ((eq art-group 'junk)
10005         (when (eq action 'move)
10006           (gnus-summary-mark-article article gnus-canceled-mark)
10007           (gnus-message 4 "Deleted article %s" article)
10008           ;; run the delete hook
10009           (run-hook-with-args 'gnus-summary-article-delete-hook
10010                               action
10011                               (gnus-data-header
10012                                (assoc article (gnus-data-list nil)))
10013                               gnus-newsgroup-name nil
10014                               select-method)))
10015        (t
10016         (let* ((pto-group (gnus-group-prefixed-name
10017                            (car art-group) to-method))
10018                (info (gnus-get-info pto-group))
10019                (to-group (gnus-info-group info))
10020                to-marks)
10021           ;; Update the group that has been moved to.
10022           (when (and info
10023                      (memq action '(move copy)))
10024             (unless (member to-group to-groups)
10025               (push to-group to-groups))
10026
10027             (when (and (not (memq article gnus-newsgroup-unreads))
10028                        (cdr art-group))
10029               (push 'read to-marks)
10030               (gnus-info-set-read
10031                info (gnus-add-to-range (gnus-info-read info)
10032                                        (list (cdr art-group)))))
10033
10034             ;; See whether the article is to be put in the cache.
10035             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10036                    (marks (if expirable
10037                               gnus-article-mark-lists
10038                             (delete '(expirable . expire)
10039                                     (copy-sequence
10040                                      gnus-article-mark-lists))))
10041                    (to-article (cdr art-group)))
10042
10043               ;; Enter the article into the cache in the new group,
10044               ;; if that is required.
10045               (when (and to-article
10046                          gnus-use-cache)
10047                 (gnus-cache-possibly-enter-article
10048                  to-group to-article
10049                  (memq article gnus-newsgroup-marked)
10050                  (memq article gnus-newsgroup-dormant)
10051                  (memq article gnus-newsgroup-unreads)))
10052
10053               (when (and gnus-preserve-marks
10054                          to-article)
10055                 ;; Copy any marks over to the new group.
10056                 (when (and (equal to-group gnus-newsgroup-name)
10057                            (not (memq article gnus-newsgroup-unreads)))
10058                   ;; Mark this article as read in this group.
10059                   (push (cons to-article gnus-read-mark)
10060                         gnus-newsgroup-reads)
10061                   ;; Increase the active status of this group.
10062                   (setcdr (gnus-active to-group) to-article)
10063                   (setcdr gnus-newsgroup-active to-article))
10064
10065                 (while marks
10066                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10067                     (when (memq article (symbol-value
10068                                          (intern (format "gnus-newsgroup-%s"
10069                                                          (caar marks)))))
10070                       (push (cdar marks) to-marks)
10071                       ;; If the other group is the same as this group,
10072                       ;; then we have to add the mark to the list.
10073                       (when (equal to-group gnus-newsgroup-name)
10074                         (set (intern (format "gnus-newsgroup-%s"
10075                                              (caar marks)))
10076                              (cons to-article
10077                                    (symbol-value
10078                                     (intern (format "gnus-newsgroup-%s"
10079                                                     (caar marks)))))))
10080                       ;; Copy the marks to other group.
10081                       (gnus-add-marked-articles
10082                        to-group (cdar marks) (list to-article) info)))
10083                   (setq marks (cdr marks)))
10084
10085                 (when (and expirable
10086                            gnus-mark-copied-or-moved-articles-as-expirable
10087                            (not (memq 'expire to-marks)))
10088                   ;; Mark this article as expirable.
10089                   (push 'expire to-marks)
10090                   (when (equal to-group gnus-newsgroup-name)
10091                     (push to-article gnus-newsgroup-expirable))
10092                   ;; Copy the expirable mark to other group.
10093                   (gnus-add-marked-articles
10094                    to-group 'expire (list to-article) info))
10095
10096                 (when (and to-marks
10097                            (gnus-method-option-p
10098                             (gnus-find-method-for-group to-group)
10099                             'server-marks))
10100                   (gnus-request-set-mark
10101                    to-group (list (list (list to-article) 'add to-marks)))))
10102
10103               (gnus-dribble-enter
10104                (concat "(gnus-group-set-info '"
10105                        (gnus-prin1-to-string (gnus-get-info to-group))
10106                        ")")
10107                (concat "^(gnus-group-set-info '(\""
10108                        (regexp-quote to-group) "\""))))
10109
10110           ;; Update the Xref header in this article to point to
10111           ;; the new crossposted article we have just created.
10112           (when (eq action 'crosspost)
10113             (with-current-buffer copy-buf
10114               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10115               (nnheader-replace-header "Xref" new-xref)
10116               (gnus-request-replace-article
10117                article gnus-newsgroup-name (current-buffer) t)))
10118
10119           ;; run the move/copy/crosspost/respool hook
10120           (run-hook-with-args 'gnus-summary-article-move-hook
10121                               action
10122                               (gnus-data-header
10123                                (assoc article (gnus-data-list nil)))
10124                               gnus-newsgroup-name
10125                               to-newsgroup
10126                               select-method))
10127
10128         ;;;!!!Why is this necessary?
10129         (set-buffer gnus-summary-buffer)
10130
10131         (when (eq action 'move)
10132           (save-excursion
10133             (gnus-summary-goto-subject article)
10134             (gnus-summary-mark-article article gnus-canceled-mark)))))
10135       (push article articles-to-update-marks))
10136
10137     (save-excursion
10138       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10139     ;; Re-activate all groups that have been moved to.
10140     (with-current-buffer gnus-group-buffer
10141       (let ((gnus-group-marked to-groups))
10142         (gnus-group-get-new-news-this-group nil t)))
10143
10144     (gnus-kill-buffer copy-buf)
10145     (gnus-summary-position-point)
10146     (gnus-set-mode-line 'summary)))
10147
10148 (defun gnus-summary-push-marks-to-backend (article)
10149   (let ((set nil)
10150         (del nil)
10151         (marks gnus-article-mark-lists))
10152     (unless (memq article gnus-newsgroup-unreads)
10153       (push 'read set))
10154     (while marks
10155       (if (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10156                (memq article (symbol-value
10157                               (intern (format "gnus-newsgroup-%s"
10158                                               (caar marks))))))
10159           (push (cdar marks) set)
10160         (push (cdar marks) del))
10161       (pop marks))
10162     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)
10163                                                  ((,article) del ,del)))))
10164
10165 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10166   "Copy the current article to some other group.
10167 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10168 When called interactively, if TO-NEWSGROUP is nil, use the value of
10169 the variable `gnus-move-split-methods' for finding a default target
10170 newsgroup.
10171 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10172 re-spool using this method."
10173   (interactive "P")
10174   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10175
10176 (defun gnus-summary-crosspost-article (&optional n)
10177   "Crosspost the current article to some other group."
10178   (interactive "P")
10179   (gnus-summary-move-article n nil nil 'crosspost))
10180
10181 (defcustom gnus-summary-respool-default-method nil
10182   "Default method type for respooling an article.
10183 If nil, use to the current newsgroup method."
10184   :type 'symbol
10185   :group 'gnus-summary-mail)
10186
10187 (defun gnus-summary-respool-article (&optional n method)
10188   "Respool the current article.
10189 The article will be squeezed through the mail spooling process again,
10190 which means that it will be put in some mail newsgroup or other
10191 depending on `nnmail-split-methods'.
10192 If N is a positive number, respool the N next articles.
10193 If N is a negative number, respool the N previous articles.
10194 If N is nil and any articles have been marked with the process mark,
10195 respool those articles instead.
10196
10197 Respooling can be done both from mail groups and \"real\" newsgroups.
10198 In the former case, the articles in question will be moved from the
10199 current group into whatever groups they are destined to.  In the
10200 latter case, they will be copied into the relevant groups."
10201   (interactive
10202    (list current-prefix-arg
10203          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10204                 (methname
10205                  (symbol-name (or gnus-summary-respool-default-method
10206                                   (car (gnus-find-method-for-group
10207                                         gnus-newsgroup-name)))))
10208                 (method
10209                  (gnus-completing-read
10210                   "Backend to use when respooling"
10211                   methods t nil 'gnus-mail-method-history methname))
10212                 ms)
10213            (cond
10214             ((zerop (length (setq ms (gnus-servers-using-backend
10215                                       (intern method)))))
10216              (list (intern method) ""))
10217             ((= 1 (length ms))
10218              (car ms))
10219             (t
10220              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10221                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10222                            ms-alist))))))))
10223   (unless method
10224     (error "No method given for respooling"))
10225   (if (assoc (symbol-name
10226               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10227              (gnus-methods-using 'respool))
10228       (gnus-summary-move-article n nil method)
10229     (gnus-summary-copy-article n nil method)))
10230
10231 (defun gnus-summary-import-article (file &optional edit)
10232   "Import an arbitrary file into a mail newsgroup."
10233   (interactive "fImport file: \nP")
10234   (let ((group gnus-newsgroup-name)
10235         (now (current-time))
10236         atts lines group-art)
10237     (unless (gnus-check-backend-function 'request-accept-article group)
10238       (error "%s does not support article importing" group))
10239     (or (file-readable-p file)
10240         (not (file-regular-p file))
10241         (error "Can't read %s" file))
10242     (with-current-buffer (gnus-get-buffer-create " *import file*")
10243       (erase-buffer)
10244       (nnheader-insert-file-contents file)
10245       (goto-char (point-min))
10246       (if (nnheader-article-p)
10247           (save-restriction
10248             (goto-char (point-min))
10249             (search-forward "\n\n" nil t)
10250             (narrow-to-region (point-min) (1- (point)))
10251             (goto-char (point-min))
10252             (unless (re-search-forward "^date:" nil t)
10253               (goto-char (point-max))
10254               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10255        ;; This doesn't look like an article, so we fudge some headers.
10256         (setq atts (file-attributes file)
10257               lines (count-lines (point-min) (point-max)))
10258         (insert "From: " (read-string "From: ") "\n"
10259                 "Subject: " (read-string "Subject: ") "\n"
10260                 "Date: " (message-make-date (nth 5 atts)) "\n"
10261                 "Message-ID: " (message-make-message-id) "\n"
10262                 "Lines: " (int-to-string lines) "\n"
10263                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10264       (setq group-art (gnus-request-accept-article group nil t))
10265       (kill-buffer (current-buffer)))
10266     (setq gnus-newsgroup-active (gnus-activate-group group))
10267     (forward-line 1)
10268     (gnus-summary-goto-article (cdr group-art) nil t)
10269     (when edit
10270       (gnus-summary-edit-article))))
10271
10272 (defun gnus-summary-create-article ()
10273   "Create an article in a mail newsgroup."
10274   (interactive)
10275   (let ((group gnus-newsgroup-name)
10276         (now (current-time))
10277         group-art)
10278     (unless (gnus-check-backend-function 'request-accept-article group)
10279       (error "%s does not support article importing" group))
10280     (with-current-buffer (gnus-get-buffer-create " *import file*")
10281       (erase-buffer)
10282       (goto-char (point-min))
10283       ;; This doesn't look like an article, so we fudge some headers.
10284       (insert "From: " (read-string "From: ") "\n"
10285               "Subject: " (read-string "Subject: ") "\n"
10286               "Date: " (message-make-date now) "\n"
10287               "Message-ID: " (message-make-message-id) "\n")
10288       (setq group-art (gnus-request-accept-article group nil t))
10289       (kill-buffer (current-buffer)))
10290     (setq gnus-newsgroup-active (gnus-activate-group group))
10291     (forward-line 1)
10292     (gnus-summary-goto-article (cdr group-art) nil t)
10293     (gnus-summary-edit-article)))
10294
10295 (defun gnus-summary-article-posted-p ()
10296   "Say whether the current (mail) article is available from news as well.
10297 This will be the case if the article has both been mailed and posted."
10298   (interactive)
10299   (let ((id (mail-header-references (gnus-summary-article-header)))
10300         (gnus-override-method (car (gnus-refer-article-methods))))
10301     (if (gnus-request-head id "")
10302         (gnus-message 2 "The current message was found on %s"
10303                       gnus-override-method)
10304       (gnus-message 2 "The current message couldn't be found on %s"
10305                     gnus-override-method)
10306       nil)))
10307
10308 (defun gnus-summary-expire-articles (&optional now)
10309   "Expire all articles that are marked as expirable in the current group."
10310   (interactive)
10311   (when (and (not gnus-group-is-exiting-without-update-p)
10312              (gnus-check-backend-function
10313               'request-expire-articles gnus-newsgroup-name))
10314     ;; This backend supports expiry.
10315     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10316            (expirable
10317             (gnus-list-range-difference
10318              (if total
10319                  (progn
10320                    ;; We need to update the info for
10321                    ;; this group for `gnus-list-of-read-articles'
10322                    ;; to give us the right answer.
10323                    (gnus-run-hooks 'gnus-exit-group-hook)
10324                    (gnus-summary-update-info)
10325                    (gnus-list-of-read-articles gnus-newsgroup-name))
10326                (setq gnus-newsgroup-expirable
10327                      (sort gnus-newsgroup-expirable '<)))
10328              gnus-newsgroup-unexist))
10329            (expiry-wait (if now 'immediate
10330                           (gnus-group-find-parameter
10331                            gnus-newsgroup-name 'expiry-wait)))
10332            (nnmail-expiry-target
10333             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10334                 nnmail-expiry-target))
10335            es)
10336       (when expirable
10337         ;; There are expirable articles in this group, so we run them
10338         ;; through the expiry process.
10339         (gnus-message 6 "Expiring articles...")
10340         (when (gnus-check-group gnus-newsgroup-name)
10341           ;; The list of articles that weren't expired is returned.
10342           (save-excursion
10343             (if expiry-wait
10344                 (let ((nnmail-expiry-wait-function nil)
10345                       (nnmail-expiry-wait expiry-wait))
10346                   (setq es (gnus-request-expire-articles
10347                             expirable gnus-newsgroup-name)))
10348               (setq es (gnus-request-expire-articles
10349                         expirable gnus-newsgroup-name)))
10350             (unless total
10351               (setq gnus-newsgroup-expirable es))
10352             ;; We go through the old list of expirable, and mark all
10353             ;; really expired articles as nonexistent.
10354             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10355               (let ((gnus-use-cache nil))
10356                 (dolist (article expirable)
10357                   (when (and (not (memq article es))
10358                              (gnus-data-find article))
10359                     (gnus-summary-mark-article article gnus-canceled-mark)
10360                     (run-hook-with-args 'gnus-summary-article-expire-hook
10361                                         'delete
10362                                         (gnus-data-header
10363                                          (assoc article (gnus-data-list nil)))
10364                                         gnus-newsgroup-name
10365                                         nil
10366                                         nil)))))))
10367         (gnus-message 6 "Expiring articles...done")))))
10368
10369 (defun gnus-summary-expire-articles-now ()
10370   "Expunge all expirable articles in the current group.
10371 This means that *all* articles that are marked as expirable will be
10372 deleted forever, right now."
10373   (interactive)
10374   (or gnus-expert-user
10375       (gnus-yes-or-no-p
10376        "Are you really, really sure you want to delete all expirable messages? ")
10377       (error "Phew!"))
10378   (gnus-summary-expire-articles t))
10379
10380 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10381 (defun gnus-summary-delete-article (&optional n)
10382   "Delete the N next (mail) articles.
10383 This command actually deletes articles.  This is not a marking
10384 command.  The article will disappear forever from your life, never to
10385 return.
10386
10387 If N is negative, delete backwards.
10388 If N is nil and articles have been marked with the process mark,
10389 delete these instead.
10390
10391 If `gnus-novice-user' is non-nil you will be asked for
10392 confirmation before the articles are deleted."
10393   (interactive "P")
10394   (unless (gnus-check-backend-function 'request-expire-articles
10395                                        gnus-newsgroup-name)
10396     (error "The current newsgroup does not support article deletion"))
10397   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10398     (error "Couldn't open server"))
10399   ;; Compute the list of articles to delete.
10400   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10401         (nnmail-expiry-target 'delete)
10402         not-deleted)
10403     (if (and gnus-novice-user
10404              (not (gnus-yes-or-no-p
10405                    (format "Do you really want to delete %s forever? "
10406                            (if (> (length articles) 1)
10407                                (format "these %s articles" (length articles))
10408                              "this article")))))
10409         ()
10410       ;; Delete the articles.
10411       (setq not-deleted (gnus-request-expire-articles
10412                          articles gnus-newsgroup-name 'force))
10413       (save-excursion
10414         (while articles
10415           (gnus-summary-remove-process-mark (car articles))
10416           ;; The backend might not have been able to delete the article
10417           ;; after all.
10418           (unless (memq (car articles) not-deleted)
10419             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10420             (let* ((article (car articles))
10421                    (ghead  (gnus-data-header
10422                             (assoc article (gnus-data-list nil)))))
10423               (run-hook-with-args 'gnus-summary-article-delete-hook
10424                                   'delete ghead gnus-newsgroup-name nil
10425                                   nil)))
10426           (setq articles (cdr articles))))
10427       (when not-deleted
10428         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10429     (gnus-summary-position-point)
10430     (gnus-set-mode-line 'summary)
10431     not-deleted))
10432
10433 (defun gnus-summary-edit-article (&optional arg)
10434   "Edit the current article.
10435 This will have permanent effect only in mail groups.
10436 If ARG is nil, edit the decoded articles.
10437 If ARG is 1, edit the raw articles.
10438 If ARG is 2, edit the raw articles even in read-only groups.
10439 If ARG is 3, edit the articles with the current handles.
10440 Otherwise, allow editing of articles even in read-only
10441 groups."
10442   (interactive "P")
10443   (let (force raw current-handles)
10444     (cond
10445      ((null arg))
10446      ((eq arg 1)
10447       (setq raw t))
10448      ((eq arg 2)
10449       (setq raw t
10450             force t))
10451      ((eq arg 3)
10452       (setq current-handles
10453             (and (gnus-buffer-live-p gnus-article-buffer)
10454                  (with-current-buffer gnus-article-buffer
10455                    (prog1
10456                        gnus-article-mime-handles
10457                      (setq gnus-article-mime-handles nil))))))
10458      (t
10459       (setq force t)))
10460     (when (and raw (not force)
10461                (member gnus-newsgroup-name '("nndraft:delayed"
10462                                              "nndraft:drafts"
10463                                              "nndraft:queue")))
10464       (error "Can't edit the raw article in group %s"
10465              gnus-newsgroup-name))
10466     (with-current-buffer gnus-summary-buffer
10467       (let ((mail-parse-charset gnus-newsgroup-charset)
10468             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10469         (gnus-set-global-variables)
10470         (when (and (not force)
10471                    (gnus-group-read-only-p))
10472           (error "The current newsgroup does not support article editing"))
10473         (gnus-summary-show-article t)
10474         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10475           (with-current-buffer gnus-article-buffer
10476             (mm-enable-multibyte)))
10477         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10478             (setq raw t))
10479         (gnus-article-edit-article
10480          (if raw 'ignore
10481            `(lambda ()
10482               (let ((mbl mml-buffer-list))
10483                 (setq mml-buffer-list nil)
10484                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10485                   (mime-to-mml ,'current-handles))
10486                 (let ((mbl1 mml-buffer-list))
10487                   (setq mml-buffer-list mbl)
10488                   (set (make-local-variable 'mml-buffer-list) mbl1))
10489                 (gnus-make-local-hook 'kill-buffer-hook)
10490                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10491          `(lambda (no-highlight)
10492             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10493                   (message-options message-options)
10494                   (message-options-set-recipient)
10495                   (mail-parse-ignored-charsets
10496                    ',gnus-newsgroup-ignored-charsets)
10497                   (rfc2047-header-encoding-alist
10498                    ',(let ((charset (gnus-group-name-charset
10499                                      (gnus-find-method-for-group
10500                                       gnus-newsgroup-name)
10501                                      gnus-newsgroup-name)))
10502                        (append (list (cons "Newsgroups" charset)
10503                                      (cons "Followup-To" charset)
10504                                      (cons "Xref" charset))
10505                                rfc2047-header-encoding-alist))))
10506               ,(if (not raw) '(progn
10507                                 (mml-to-mime)
10508                                 (mml-destroy-buffers)
10509                                 (remove-hook 'kill-buffer-hook
10510                                              'mml-destroy-buffers t)
10511                                 (kill-local-variable 'mml-buffer-list)))
10512               (gnus-summary-edit-article-done
10513                ,(or (mail-header-references gnus-current-headers) "")
10514                ,(gnus-group-read-only-p)
10515                ,gnus-summary-buffer no-highlight))))))))
10516
10517 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10518
10519 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10520                                                  no-highlight)
10521   "Make edits to the current article permanent."
10522   (interactive)
10523   (save-excursion
10524     ;; The buffer restriction contains the entire article if it exists.
10525     (when (article-goto-body)
10526       (let ((lines (count-lines (point) (point-max)))
10527             (length (- (point-max) (point)))
10528             (case-fold-search t)
10529             (body (copy-marker (point))))
10530         (goto-char (point-min))
10531         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10532           (delete-region (match-beginning 1) (match-end 1))
10533           (insert (number-to-string length)))
10534         (goto-char (point-min))
10535         (when (re-search-forward
10536                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10537           (delete-region (match-beginning 1) (match-end 1))
10538           (insert (number-to-string length)))
10539         (goto-char (point-min))
10540         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10541           (delete-region (match-beginning 1) (match-end 1))
10542           (insert (number-to-string lines))))))
10543   ;; Replace the article.
10544   (let ((buf (current-buffer))
10545         (article (cdr gnus-article-current))
10546         replace-result)
10547     (with-temp-buffer
10548       (insert-buffer-substring buf)
10549       (if (and (not read-only)
10550                (not (setq replace-result
10551                           (gnus-request-replace-article
10552                            article (car gnus-article-current)
10553                            (current-buffer) t))))
10554           (error "Couldn't replace article")
10555         ;; If we got a number back, then that's the new article number
10556         ;; for this article.  Otherwise, the article number didn't change.
10557         (when (numberp replace-result)
10558           (with-current-buffer gnus-summary-buffer
10559             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10560             (gnus-summary-limit gnus-newsgroup-limit)
10561             (setq article replace-result)
10562             (gnus-summary-goto-subject article t)))
10563         ;; Update the summary buffer.
10564         (if (and references
10565                  (equal (message-tokenize-header references " ")
10566                         (message-tokenize-header
10567                          (or (message-fetch-field "references") "") " ")))
10568             ;; We only have to update this line.
10569             (save-excursion
10570               (save-restriction
10571                 (message-narrow-to-head)
10572                 (let ((head (buffer-substring-no-properties
10573                              (point-min) (point-max)))
10574                       header)
10575                   (with-temp-buffer
10576                     (insert (format "211 %d Article retrieved.\n" article))
10577                     (insert head)
10578                     (insert ".\n")
10579                     (let ((nntp-server-buffer (current-buffer)))
10580                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10581                     (with-current-buffer gnus-summary-buffer
10582                       (gnus-data-set-header (gnus-data-find article) header)
10583                       (gnus-summary-update-article-line article header)
10584                       (if (gnus-summary-goto-subject article nil t)
10585                           (gnus-summary-update-secondary-mark article)))))))
10586           ;; Update threads.
10587           (set-buffer (or buffer gnus-summary-buffer))
10588           (gnus-summary-update-article article)
10589           (if (gnus-summary-goto-subject article nil t)
10590               (gnus-summary-update-secondary-mark article)))
10591         ;; Prettify the article buffer again.
10592         (unless no-highlight
10593           (with-current-buffer gnus-article-buffer
10594             ;;!!! Fix this -- article should be rehighlighted.
10595             ;;(gnus-run-hooks 'gnus-article-display-hook)
10596             (set-buffer gnus-original-article-buffer)
10597             (gnus-request-article
10598              article (car gnus-article-current) (current-buffer))))
10599         ;; Prettify the summary buffer line.
10600         (when (gnus-visual-p 'summary-highlight 'highlight)
10601           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10602
10603 (defun gnus-summary-edit-wash (key)
10604   "Perform editing command KEY in the article buffer."
10605   (interactive
10606    (list
10607     (progn
10608       (message "%s" (concat (this-command-keys) "- "))
10609       (read-char))))
10610   (message "")
10611   (gnus-summary-edit-article)
10612   (execute-kbd-macro (concat (this-command-keys) key))
10613   (gnus-article-edit-done))
10614
10615 ;;; Respooling
10616
10617 (defun gnus-summary-respool-query (&optional silent trace)
10618   "Query where the respool algorithm would put this article."
10619   (interactive)
10620   (let (gnus-mark-article-hook)
10621     (gnus-summary-select-article)
10622     (with-current-buffer gnus-original-article-buffer
10623       (let ((groups (nnmail-article-group 'identity trace)))
10624         (unless silent
10625           (if groups
10626               (message "This message would go to %s"
10627                        (mapconcat 'car groups ", "))
10628             (message "This message would go to no groups"))
10629           groups)))))
10630
10631 (defun gnus-summary-respool-trace ()
10632   "Trace where the respool algorithm would put this article.
10633 Display a buffer showing all fancy splitting patterns which matched."
10634   (interactive)
10635   (gnus-summary-respool-query nil t))
10636
10637 ;; Summary marking commands.
10638
10639 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10640   "Mark articles which has the same subject as read, and then select the next.
10641 If UNMARK is positive, remove any kind of mark.
10642 If UNMARK is negative, tick articles."
10643   (interactive "P")
10644   (when unmark
10645     (setq unmark (prefix-numeric-value unmark)))
10646   (let ((count
10647          (gnus-summary-mark-same-subject
10648           (gnus-summary-article-subject) unmark)))
10649     ;; Select next unread article.  If auto-select-same mode, should
10650     ;; select the first unread article.
10651     (gnus-summary-next-article t (and gnus-auto-select-same
10652                                       (gnus-summary-article-subject)))
10653     (gnus-message 7 "%d article%s marked as %s"
10654                   count (if (= count 1) " is" "s are")
10655                   (if unmark "unread" "read"))))
10656
10657 (defun gnus-summary-kill-same-subject (&optional unmark)
10658   "Mark articles which has the same subject as read.
10659 If UNMARK is positive, remove any kind of mark.
10660 If UNMARK is negative, tick articles."
10661   (interactive "P")
10662   (when unmark
10663     (setq unmark (prefix-numeric-value unmark)))
10664   (let ((count
10665          (gnus-summary-mark-same-subject
10666           (gnus-summary-article-subject) unmark)))
10667     ;; If marked as read, go to next unread subject.
10668     (when (null unmark)
10669       ;; Go to next unread subject.
10670       (gnus-summary-next-subject 1 t))
10671     (gnus-message 7 "%d articles are marked as %s"
10672                   count (if unmark "unread" "read"))))
10673
10674 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10675   "Mark articles with same SUBJECT as read, and return marked number.
10676 If optional argument UNMARK is positive, remove any kinds of marks.
10677 If optional argument UNMARK is negative, mark articles as unread instead."
10678   (let ((count 1))
10679     (save-excursion
10680       (cond
10681        ((null unmark)                   ; Mark as read.
10682         (while (and
10683                 (progn
10684                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10685                   (gnus-summary-show-thread) t)
10686                 (gnus-summary-find-subject subject))
10687           (setq count (1+ count))))
10688        ((> unmark 0)                    ; Tick.
10689         (while (and
10690                 (progn
10691                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10692                   (gnus-summary-show-thread) t)
10693                 (gnus-summary-find-subject subject))
10694           (setq count (1+ count))))
10695        (t                               ; Mark as unread.
10696         (while (and
10697                 (progn
10698                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10699                   (gnus-summary-show-thread) t)
10700                 (gnus-summary-find-subject subject))
10701           (setq count (1+ count)))))
10702       (gnus-set-mode-line 'summary)
10703       ;; Return the number of marked articles.
10704       count)))
10705
10706 (defun gnus-summary-mark-as-processable (n &optional unmark)
10707   "Set the process mark on the next N articles.
10708 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10709 the process mark instead.  The difference between N and the actual
10710 number of articles marked is returned."
10711   (interactive "P")
10712   (if (and (null n) (gnus-region-active-p))
10713       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10714     (setq n (prefix-numeric-value n))
10715     (let ((backward (< n 0))
10716           (n (abs n)))
10717       (while (and
10718               (> n 0)
10719               (if unmark
10720                   (gnus-summary-remove-process-mark
10721                    (gnus-summary-article-number))
10722                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10723               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10724         (setq n (1- n)))
10725       (when (/= 0 n)
10726         (gnus-message 7 "No more articles"))
10727       (gnus-summary-recenter)
10728       (gnus-summary-position-point)
10729       n)))
10730
10731 (defun gnus-summary-unmark-as-processable (n)
10732   "Remove the process mark from the next N articles.
10733 If N is negative, unmark backward instead.  The difference between N and
10734 the actual number of articles unmarked is returned."
10735   (interactive "P")
10736   (gnus-summary-mark-as-processable n t))
10737
10738 (defun gnus-summary-unmark-all-processable ()
10739   "Remove the process mark from all articles."
10740   (interactive)
10741   (save-excursion
10742     (while gnus-newsgroup-processable
10743       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10744   (gnus-summary-position-point))
10745
10746 (defun gnus-summary-add-mark (article type)
10747   "Mark ARTICLE with a mark of TYPE."
10748   (let ((vtype (car (assq type gnus-article-mark-lists)))
10749         var)
10750     (if (not vtype)
10751         (error "No such mark type: %s" type)
10752       (setq var (intern (format "gnus-newsgroup-%s" type)))
10753       (set var (cons article (symbol-value var)))
10754       (if (memq type '(processable cached replied forwarded recent saved))
10755           (gnus-summary-update-secondary-mark article)
10756         ;;; !!! This is bogus.  We should find out what primary
10757         ;;; !!! mark we want to set.
10758         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10759
10760 (defun gnus-summary-mark-as-expirable (n)
10761   "Mark N articles forward as expirable.
10762 If N is negative, mark backward instead.  The difference between N and
10763 the actual number of articles marked is returned."
10764   (interactive "p")
10765   (gnus-summary-mark-forward n gnus-expirable-mark))
10766
10767 (defun gnus-summary-mark-as-spam (n)
10768   "Mark N articles forward as spam.
10769 If N is negative, mark backward instead.  The difference between N and
10770 the actual number of articles marked is returned."
10771   (interactive "p")
10772   (gnus-summary-mark-forward n gnus-spam-mark))
10773
10774 (defun gnus-summary-mark-article-as-replied (article)
10775   "Mark ARTICLE as replied to and update the summary line.
10776 ARTICLE can also be a list of articles."
10777   (interactive (list (gnus-summary-article-number)))
10778   (let ((articles (if (listp article) article (list article))))
10779     (dolist (article articles)
10780       (unless (numberp article)
10781         (error "%s is not a number" article))
10782       (push article gnus-newsgroup-replied)
10783       (let ((inhibit-read-only t))
10784         (when (gnus-summary-goto-subject article nil t)
10785           (gnus-summary-update-secondary-mark article))))))
10786
10787 (defun gnus-summary-mark-article-as-forwarded (article)
10788   "Mark ARTICLE as forwarded and update the summary line.
10789 ARTICLE can also be a list of articles."
10790   (let ((articles (if (listp article) article (list article))))
10791     (dolist (article articles)
10792       (push article gnus-newsgroup-forwarded)
10793       (let ((inhibit-read-only t))
10794         (when (gnus-summary-goto-subject article nil t)
10795           (gnus-summary-update-secondary-mark article))))))
10796
10797 (defun gnus-summary-set-bookmark (article)
10798   "Set a bookmark in current article."
10799   (interactive (list (gnus-summary-article-number)))
10800   (when (or (not (get-buffer gnus-article-buffer))
10801             (not gnus-current-article)
10802             (not gnus-article-current)
10803             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10804     (error "No current article selected"))
10805   ;; Remove old bookmark, if one exists.
10806   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10807   ;; Set the new bookmark, which is on the form
10808   ;; (article-number . line-number-in-body).
10809   (push
10810    (cons article
10811          (with-current-buffer gnus-article-buffer
10812            (count-lines
10813             (min (point)
10814                  (save-excursion
10815                    (article-goto-body)
10816                    (point)))
10817             (point))))
10818    gnus-newsgroup-bookmarks)
10819   (gnus-message 6 "A bookmark has been added to the current article."))
10820
10821 (defun gnus-summary-remove-bookmark (article)
10822   "Remove the bookmark from the current article."
10823   (interactive (list (gnus-summary-article-number)))
10824   ;; Remove old bookmark, if one exists.
10825   (if (not (assq article gnus-newsgroup-bookmarks))
10826       (gnus-message 6 "No bookmark in current article.")
10827     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10828     (gnus-message 6 "Removed bookmark.")))
10829
10830 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10831 (defun gnus-summary-mark-as-dormant (n)
10832   "Mark N articles forward as dormant.
10833 If N is negative, mark backward instead.  The difference between N and
10834 the actual number of articles marked is returned."
10835   (interactive "p")
10836   (gnus-summary-mark-forward n gnus-dormant-mark))
10837
10838 (defun gnus-summary-set-process-mark (article)
10839   "Set the process mark on ARTICLE and update the summary line."
10840   (setq gnus-newsgroup-processable
10841         (cons article
10842               (delq article gnus-newsgroup-processable)))
10843   (when (gnus-summary-goto-subject article)
10844     (gnus-summary-show-thread)
10845     (gnus-summary-goto-subject article)
10846     (gnus-summary-update-secondary-mark article)))
10847
10848 (defun gnus-summary-remove-process-mark (&rest articles)
10849   "Remove the process mark from ARTICLES and update the summary line."
10850   (dolist (article articles)
10851     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10852     (when (gnus-summary-goto-subject article)
10853       (gnus-summary-show-thread)
10854       (gnus-summary-goto-subject article)
10855       (gnus-summary-update-secondary-mark article)))
10856   t)
10857
10858 (defun gnus-summary-set-saved-mark (article)
10859   "Set the process mark on ARTICLE and update the summary line."
10860   (push article gnus-newsgroup-saved)
10861   (when (gnus-summary-goto-subject article)
10862     (gnus-summary-update-secondary-mark article)))
10863
10864 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10865   "Mark N articles as read forwards.
10866 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10867 The difference between N and the actual number of articles marked is
10868 returned.
10869 If NO-EXPIRE, auto-expiry will be inhibited."
10870   (interactive "p")
10871   (gnus-summary-show-thread)
10872   (let ((backward (< n 0))
10873         (gnus-summary-goto-unread
10874          (and gnus-summary-goto-unread
10875               (not (eq gnus-summary-goto-unread 'never))
10876               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10877                                     gnus-ticked-mark gnus-dormant-mark)))))
10878         (n (abs n))
10879         (mark (or mark gnus-del-mark)))
10880     (while (and (> n 0)
10881                 (gnus-summary-mark-article nil mark no-expire)
10882                 (zerop (gnus-summary-next-subject
10883                         (if backward -1 1)
10884                         (and gnus-summary-goto-unread
10885                              (not (eq gnus-summary-goto-unread 'never)))
10886                         t)))
10887       (setq n (1- n)))
10888     (when (/= 0 n)
10889       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10890     (gnus-summary-recenter)
10891     (gnus-summary-position-point)
10892     (gnus-set-mode-line 'summary)
10893     n))
10894
10895 (defun gnus-summary-mark-article-as-read (mark)
10896   "Mark the current article quickly as read with MARK."
10897   (let ((article (gnus-summary-article-number)))
10898     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10899     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10900     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10901     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10902     (push (cons article mark) gnus-newsgroup-reads)
10903     ;; Possibly remove from cache, if that is used.
10904     (when gnus-use-cache
10905       (gnus-cache-enter-remove-article article))
10906     ;; Allow the backend to change the mark.
10907     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10908     ;; Check for auto-expiry.
10909     (when (and gnus-newsgroup-auto-expire
10910                (memq mark gnus-auto-expirable-marks))
10911       (setq mark gnus-expirable-mark)
10912       ;; Let the backend know about the mark change.
10913       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10914       (push article gnus-newsgroup-expirable))
10915     ;; Set the mark in the buffer.
10916     (gnus-summary-update-mark mark 'unread)
10917     t))
10918
10919 (defun gnus-summary-mark-article-as-unread (mark)
10920   "Mark the current article quickly as unread with MARK."
10921   (let* ((article (gnus-summary-article-number))
10922          (old-mark (gnus-summary-article-mark article)))
10923     ;; Allow the backend to change the mark.
10924     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10925     (if (eq mark old-mark)
10926         t
10927       (if (<= article 0)
10928           (progn
10929             (gnus-error 1 "Can't mark negative article numbers")
10930             nil)
10931         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10932         (setq gnus-newsgroup-spam-marked
10933               (delq article gnus-newsgroup-spam-marked))
10934         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10935         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10936         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10937         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10938         (cond ((= mark gnus-ticked-mark)
10939                (setq gnus-newsgroup-marked
10940                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10941                                               article)))
10942               ((= mark gnus-spam-mark)
10943                (setq gnus-newsgroup-spam-marked
10944                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10945                                               article)))
10946               ((= mark gnus-dormant-mark)
10947                (setq gnus-newsgroup-dormant
10948                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10949                                               article)))
10950               (t
10951                (setq gnus-newsgroup-unreads
10952                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10953                                               article))))
10954         (gnus-alist-pull article gnus-newsgroup-reads)
10955
10956         ;; See whether the article is to be put in the cache.
10957         (and gnus-use-cache
10958              (vectorp (gnus-summary-article-header article))
10959              (save-excursion
10960                (gnus-cache-possibly-enter-article
10961                 gnus-newsgroup-name article
10962                 (= mark gnus-ticked-mark)
10963                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10964
10965         ;; Fix the mark.
10966         (gnus-summary-update-mark mark 'unread)
10967         t))))
10968
10969 (defun gnus-summary-mark-article (&optional article mark no-expire)
10970   "Mark ARTICLE with MARK.  MARK can be any character.
10971 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10972 `??' (dormant) and `?E' (expirable).
10973 If MARK is nil, then the default character `?r' is used.
10974 If ARTICLE is nil, then the article on the current line will be
10975 marked.
10976 If NO-EXPIRE, auto-expiry will be inhibited."
10977   ;; The mark might be a string.
10978   (when (stringp mark)
10979     (setq mark (aref mark 0)))
10980   ;; If no mark is given, then we check auto-expiring.
10981   (when (null mark)
10982     (setq mark gnus-del-mark))
10983   (when (and (not no-expire)
10984              gnus-newsgroup-auto-expire
10985              (memq mark gnus-auto-expirable-marks))
10986     (setq mark gnus-expirable-mark))
10987   (let ((article (or article (gnus-summary-article-number)))
10988         (old-mark (gnus-summary-article-mark article)))
10989     ;; Allow the backend to change the mark.
10990     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10991     (if (eq mark old-mark)
10992         t
10993       (unless article
10994         (error "No article on current line"))
10995       (if (not (if (or (= mark gnus-unread-mark)
10996                        (= mark gnus-ticked-mark)
10997                        (= mark gnus-spam-mark)
10998                        (= mark gnus-dormant-mark))
10999                    (gnus-mark-article-as-unread article mark)
11000                  (gnus-mark-article-as-read article mark)))
11001           t
11002         ;; See whether the article is to be put in the cache.
11003         (and gnus-use-cache
11004              (not (= mark gnus-canceled-mark))
11005              (vectorp (gnus-summary-article-header article))
11006              (save-excursion
11007                (gnus-cache-possibly-enter-article
11008                 gnus-newsgroup-name article
11009                 (= mark gnus-ticked-mark)
11010                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11011
11012         (when (gnus-summary-goto-subject article nil t)
11013           (let ((inhibit-read-only t))
11014             (gnus-summary-show-thread)
11015             ;; Fix the mark.
11016             (gnus-summary-update-mark mark 'unread)
11017             t))))))
11018
11019 (defun gnus-summary-update-secondary-mark (article)
11020   "Update the secondary (read, process, cache) mark."
11021   (gnus-summary-update-mark
11022    (cond ((memq article gnus-newsgroup-processable)
11023           gnus-process-mark)
11024          ((memq article gnus-newsgroup-cached)
11025           gnus-cached-mark)
11026          ((memq article gnus-newsgroup-replied)
11027           gnus-replied-mark)
11028          ((memq article gnus-newsgroup-forwarded)
11029           gnus-forwarded-mark)
11030          ((memq article gnus-newsgroup-saved)
11031           gnus-saved-mark)
11032          ((memq article gnus-newsgroup-unseen)
11033           gnus-unseen-mark)
11034          (t gnus-no-mark))
11035    'replied)
11036   (when (gnus-visual-p 'summary-highlight 'highlight)
11037     (gnus-summary-highlight-line)
11038     (gnus-run-hooks 'gnus-summary-update-hook))
11039   t)
11040
11041 (defun gnus-summary-update-download-mark (article)
11042   "Update the download mark."
11043   (gnus-summary-update-mark
11044    (cond ((memq article gnus-newsgroup-undownloaded)
11045           gnus-undownloaded-mark)
11046          (gnus-newsgroup-agentized
11047           gnus-downloaded-mark)
11048          (t
11049           gnus-no-mark))
11050    'download)
11051   (gnus-summary-update-line t)
11052   t)
11053
11054 (defun gnus-summary-update-mark (mark type)
11055   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11056         (inhibit-read-only t))
11057     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11058     (when forward
11059       (when (looking-at "\r")
11060         (incf forward))
11061       (when (<= (+ forward (point)) (point-max))
11062         ;; Go to the right position on the line.
11063         (goto-char (+ forward (point)))
11064         ;; Replace the old mark with the new mark.
11065         (let ((to-insert
11066                (mm-subst-char-in-string
11067                 (char-after) mark
11068                 (buffer-substring (point) (1+ (point))))))
11069           (delete-region (point) (1+ (point)))
11070           (insert to-insert))
11071         ;; Optionally update the marks by some user rule.
11072         (when (eq type 'unread)
11073           (gnus-data-set-mark
11074            (gnus-data-find (gnus-summary-article-number)) mark)
11075           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11076
11077 (defun gnus-mark-article-as-read (article &optional mark)
11078   "Enter ARTICLE in the pertinent lists and remove it from others."
11079   ;; Make the article expirable.
11080   (let ((mark (or mark gnus-del-mark)))
11081     (setq gnus-newsgroup-expirable
11082           (if (= mark gnus-expirable-mark)
11083               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11084             (delq article gnus-newsgroup-expirable)))
11085     ;; Remove from unread and marked lists.
11086     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11087     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11088     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11089     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11090     (push (cons article mark) gnus-newsgroup-reads)
11091     ;; Possibly remove from cache, if that is used.
11092     (when gnus-use-cache
11093       (gnus-cache-enter-remove-article article))
11094     t))
11095
11096 (defun gnus-mark-article-as-unread (article &optional mark)
11097   "Enter ARTICLE in the pertinent lists and remove it from others."
11098   (let ((mark (or mark gnus-ticked-mark)))
11099     (if (<= article 0)
11100         (progn
11101           (gnus-error 1 "Can't mark negative article numbers")
11102           nil)
11103       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11104             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11105             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11106             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11107             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11108
11109       ;; Unsuppress duplicates?
11110       (when gnus-suppress-duplicates
11111         (gnus-dup-unsuppress-article article))
11112
11113       (cond ((= mark gnus-ticked-mark)
11114              (setq gnus-newsgroup-marked
11115                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11116             ((= mark gnus-spam-mark)
11117              (setq gnus-newsgroup-spam-marked
11118                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11119                                             article)))
11120             ((= mark gnus-dormant-mark)
11121              (setq gnus-newsgroup-dormant
11122                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11123             (t
11124              (setq gnus-newsgroup-unreads
11125                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11126       (gnus-alist-pull article gnus-newsgroup-reads)
11127       t)))
11128
11129 (defun gnus-summary-tick-article-forward (n)
11130   "Tick N articles forwards.
11131 If N is negative, tick backwards instead.
11132 The difference between N and the number of articles ticked is returned."
11133   (interactive "p")
11134   (gnus-summary-mark-forward n gnus-ticked-mark))
11135
11136 (defun gnus-summary-tick-article-backward (n)
11137   "Tick N articles backwards.
11138 The difference between N and the number of articles ticked is returned."
11139   (interactive "p")
11140   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11141
11142 (defun gnus-summary-tick-article (&optional article clear-mark)
11143   "Mark current article as unread.
11144 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11145 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11146   (interactive)
11147   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11148                                        gnus-ticked-mark)))
11149
11150 (defun gnus-summary-mark-as-read-forward (n)
11151   "Mark N articles as read forwards.
11152 If N is negative, mark backwards instead.
11153 The difference between N and the actual number of articles marked is
11154 returned."
11155   (interactive "p")
11156   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11157
11158 (defun gnus-summary-mark-as-read-backward (n)
11159   "Mark the N articles as read backwards.
11160 The difference between N and the actual number of articles marked is
11161 returned."
11162   (interactive "p")
11163   (gnus-summary-mark-forward
11164    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11165
11166 (defun gnus-summary-mark-as-read (&optional article mark)
11167   "Mark current article as read.
11168 ARTICLE specifies the article to be marked as read.
11169 MARK specifies a string to be inserted at the beginning of the line."
11170   (gnus-summary-mark-article article mark))
11171
11172 (defun gnus-summary-clear-mark-forward (n)
11173   "Clear marks from N articles forward.
11174 If N is negative, clear backward instead.
11175 The difference between N and the number of marks cleared is returned."
11176   (interactive "p")
11177   (gnus-summary-mark-forward n gnus-unread-mark))
11178
11179 (defun gnus-summary-clear-mark-backward (n)
11180   "Clear marks from N articles backward.
11181 The difference between N and the number of marks cleared is returned."
11182   (interactive "p")
11183   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11184
11185 (defun gnus-summary-mark-unread-as-read ()
11186   "Intended to be used by `gnus-mark-article-hook'."
11187   (when (memq gnus-current-article gnus-newsgroup-unreads)
11188     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11189
11190 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11191   "Intended to be used by `gnus-mark-article-hook'."
11192   (let ((mark (gnus-summary-article-mark)))
11193     (when (or (gnus-unread-mark-p mark)
11194               (gnus-read-mark-p mark))
11195       (gnus-summary-mark-article gnus-current-article
11196                                  (or new-mark gnus-read-mark)))))
11197
11198 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11199   "Intended to be used by `gnus-mark-article-hook'."
11200   (let ((mark (gnus-summary-article-mark)))
11201     (when (or (gnus-unread-mark-p mark)
11202               (gnus-read-mark-p mark))
11203       (gnus-summary-mark-article (gnus-summary-article-number)
11204                                  (or new-mark gnus-read-mark)))))
11205
11206 (defun gnus-summary-mark-unread-as-ticked ()
11207   "Intended to be used by `gnus-mark-article-hook'."
11208   (when (memq gnus-current-article gnus-newsgroup-unreads)
11209     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11210
11211 (defun gnus-summary-mark-region-as-read (point mark all)
11212   "Mark all unread articles between point and mark as read.
11213 If given a prefix, mark all articles between point and mark as read,
11214 even ticked and dormant ones."
11215   (interactive "r\nP")
11216   (save-excursion
11217     (let (article)
11218       (goto-char point)
11219       (beginning-of-line)
11220       (while (and
11221               (< (point) mark)
11222               (progn
11223                 (when (or all
11224                           (memq (setq article (gnus-summary-article-number))
11225                                 gnus-newsgroup-unreads))
11226                   (gnus-summary-mark-article article gnus-del-mark))
11227                 t)
11228               (gnus-summary-find-next))))))
11229
11230 (defun gnus-summary-mark-below (score mark)
11231   "Mark articles with score less than SCORE with MARK."
11232   (interactive "P\ncMark: ")
11233   (setq score (if score
11234                   (prefix-numeric-value score)
11235                 (or gnus-summary-default-score 0)))
11236   (with-current-buffer gnus-summary-buffer
11237     (goto-char (point-min))
11238     (while
11239         (progn
11240           (and (< (gnus-summary-article-score) score)
11241                (gnus-summary-mark-article nil mark))
11242           (gnus-summary-find-next)))))
11243
11244 (defun gnus-summary-kill-below (&optional score)
11245   "Mark articles with score below SCORE as read."
11246   (interactive "P")
11247   (gnus-summary-mark-below score gnus-killed-mark))
11248
11249 (defun gnus-summary-clear-above (&optional score)
11250   "Clear all marks from articles with score above SCORE."
11251   (interactive "P")
11252   (gnus-summary-mark-above score gnus-unread-mark))
11253
11254 (defun gnus-summary-tick-above (&optional score)
11255   "Tick all articles with score above SCORE."
11256   (interactive "P")
11257   (gnus-summary-mark-above score gnus-ticked-mark))
11258
11259 (defun gnus-summary-mark-above (score mark)
11260   "Mark articles with score over SCORE with MARK."
11261   (interactive "P\ncMark: ")
11262   (setq score (if score
11263                   (prefix-numeric-value score)
11264                 (or gnus-summary-default-score 0)))
11265   (with-current-buffer gnus-summary-buffer
11266     (goto-char (point-min))
11267     (while (and (progn
11268                   (when (> (gnus-summary-article-score) score)
11269                     (gnus-summary-mark-article nil mark))
11270                   t)
11271                 (gnus-summary-find-next)))))
11272
11273 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11274 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11275 (defun gnus-summary-limit-include-expunged (&optional no-error)
11276   "Display all the hidden articles that were expunged for low scores."
11277   (interactive)
11278   (let ((inhibit-read-only t))
11279     (let ((scored gnus-newsgroup-scored)
11280           headers h)
11281       (while scored
11282         (unless (gnus-summary-article-header (caar scored))
11283           (and (setq h (gnus-number-to-header (caar scored)))
11284                (< (cdar scored) gnus-summary-expunge-below)
11285                (push h headers)))
11286         (setq scored (cdr scored)))
11287       (if (not headers)
11288           (when (not no-error)
11289             (error "No expunged articles hidden"))
11290         (goto-char (point-min))
11291         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11292         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11293         (dolist (x headers)
11294           (push (mail-header-number x) gnus-newsgroup-limit))
11295         (gnus-summary-prepare-unthreaded (nreverse headers))
11296         (goto-char (point-min))
11297         (gnus-summary-position-point)
11298         t))))
11299
11300 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11301   "Mark all unread articles in this newsgroup as read.
11302 If prefix argument ALL is non-nil, ticked and dormant articles will
11303 also be marked as read.
11304 If QUIETLY is non-nil, no questions will be asked.
11305
11306 If TO-HERE is non-nil, it should be a point in the buffer.  All
11307 articles before (after, if REVERSE is set) this point will be marked
11308 as read.
11309
11310 Note that this function will only catch up the unread article
11311 in the current summary buffer limitation.
11312
11313 The number of articles marked as read is returned."
11314   (interactive "P")
11315   (prog1
11316       (save-excursion
11317         (when (or quietly
11318                   (not gnus-interactive-catchup) ;Without confirmation?
11319                   gnus-expert-user
11320                   (gnus-y-or-n-p
11321                    (if all
11322                        "Mark absolutely all articles as read? "
11323                      "Mark all unread articles as read? ")))
11324           (if (and not-mark
11325                    (not gnus-newsgroup-adaptive)
11326                    (not gnus-newsgroup-auto-expire)
11327                    (not gnus-suppress-duplicates)
11328                    (or (not gnus-use-cache)
11329                        (eq gnus-use-cache 'passive)))
11330               (progn
11331                 (when all
11332                   (setq gnus-newsgroup-marked nil
11333                         gnus-newsgroup-spam-marked nil
11334                         gnus-newsgroup-dormant nil))
11335                 (setq gnus-newsgroup-unreads
11336                       (gnus-sorted-nunion
11337                        (gnus-sorted-intersection gnus-newsgroup-unreads
11338                                                  gnus-newsgroup-downloadable)
11339                        (gnus-sorted-difference gnus-newsgroup-unfetched
11340                                                gnus-newsgroup-cached))))
11341             ;; We actually mark all articles as canceled, which we
11342             ;; have to do when using auto-expiry or adaptive scoring.
11343             (gnus-summary-show-all-threads)
11344             (if (and to-here reverse)
11345                 (progn
11346                   (goto-char to-here)
11347                   (gnus-summary-mark-current-read-and-unread-as-read
11348                    gnus-catchup-mark)
11349                   (while (gnus-summary-find-next (not all))
11350                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11351               (when (gnus-summary-first-subject (not all))
11352                 (while (and
11353                         (if to-here (< (point) to-here) t)
11354                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11355                         (gnus-summary-find-next (not all))))))
11356             (gnus-set-mode-line 'summary))
11357           t))
11358     (gnus-summary-position-point)))
11359
11360 (defun gnus-summary-catchup-to-here (&optional all)
11361   "Mark all unticked articles before the current one as read.
11362 If ALL is non-nil, also mark ticked and dormant articles as read."
11363   (interactive "P")
11364   (save-excursion
11365     (gnus-save-hidden-threads
11366       (let ((beg (point)))
11367         ;; We check that there are unread articles.
11368         (when (or all (gnus-summary-find-prev))
11369           (gnus-summary-catchup all t beg)))))
11370   (gnus-summary-position-point))
11371
11372 (defun gnus-summary-catchup-from-here (&optional all)
11373   "Mark all unticked articles after (and including) the current one as read.
11374 If ALL is non-nil, also mark ticked and dormant articles as read."
11375   (interactive "P")
11376   (save-excursion
11377     (gnus-save-hidden-threads
11378       (let ((beg (point)))
11379         ;; We check that there are unread articles.
11380         (when (or all (gnus-summary-find-next))
11381           (gnus-summary-catchup all t beg nil t)))))
11382   (gnus-summary-position-point))
11383
11384 (defun gnus-summary-catchup-all (&optional quietly)
11385   "Mark all articles in this newsgroup as read.
11386 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11387 instead, which marks only unread articles as read."
11388   (interactive "P")
11389   (gnus-summary-catchup t quietly))
11390
11391 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11392   "Mark all unread articles in this group as read, then exit.
11393 If prefix argument ALL is non-nil, all articles are marked as read.
11394 If QUIETLY is non-nil, no questions will be asked."
11395   (interactive "P")
11396   (when (gnus-summary-catchup all quietly nil 'fast)
11397     ;; Select next newsgroup or exit.
11398     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11399              (eq gnus-auto-select-next 'quietly))
11400         (gnus-summary-next-group nil)
11401       (gnus-summary-exit))))
11402
11403 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11404   "Mark all articles in this newsgroup as read, and then exit.
11405 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11406 instead, which marks only unread articles as read."
11407   (interactive "P")
11408   (gnus-summary-catchup-and-exit t quietly))
11409
11410 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11411   "Mark all articles in this group as read and select the next group.
11412 If given a prefix, mark all articles, unread as well as ticked, as
11413 read."
11414   (interactive "P")
11415   (save-excursion
11416     (gnus-summary-catchup all))
11417   (gnus-summary-next-group))
11418
11419 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11420   "Mark all articles in this group as read and select the previous group.
11421 If given a prefix, mark all articles, unread as well as ticked, as
11422 read."
11423   (interactive "P")
11424   (save-excursion
11425     (gnus-summary-catchup all))
11426   (gnus-summary-next-group nil nil t))
11427
11428 ;;;
11429 ;;; with article
11430 ;;;
11431
11432 (defmacro gnus-with-article (article &rest forms)
11433   "Select ARTICLE and perform FORMS in the original article buffer.
11434 Then replace the article with the result."
11435   `(progn
11436      ;; We don't want the article to be marked as read.
11437      (let (gnus-mark-article-hook)
11438        (gnus-summary-select-article t t nil ,article))
11439      (set-buffer gnus-original-article-buffer)
11440      ,@forms
11441      (if (not (gnus-check-backend-function
11442                'request-replace-article (car gnus-article-current)))
11443          (gnus-message 5 "Read-only group; not replacing")
11444        (unless (gnus-request-replace-article
11445                 ,article (car gnus-article-current)
11446                 (current-buffer) t)
11447          (error "Couldn't replace article")))
11448      ;; The cache and backlog have to be flushed somewhat.
11449      (when gnus-keep-backlog
11450        (gnus-backlog-remove-article
11451         (car gnus-article-current) (cdr gnus-article-current)))
11452      (when gnus-use-cache
11453        (gnus-cache-update-article
11454         (car gnus-article-current) (cdr gnus-article-current)))))
11455
11456 (put 'gnus-with-article 'lisp-indent-function 1)
11457 (put 'gnus-with-article 'edebug-form-spec '(form body))
11458
11459 ;; Thread-based commands.
11460
11461 (defun gnus-summary-articles-in-thread (&optional article)
11462   "Return a list of all articles in the current thread.
11463 If ARTICLE is non-nil, return all articles in the thread that starts
11464 with that article."
11465   (let* ((article (or article (gnus-summary-article-number)))
11466          (data (gnus-data-find-list article))
11467          (top-level (gnus-data-level (car data)))
11468          (top-subject
11469           (cond ((null gnus-thread-operation-ignore-subject)
11470                  (gnus-simplify-subject-re
11471                   (mail-header-subject (gnus-data-header (car data)))))
11472                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11473                  (gnus-simplify-subject-fuzzy
11474                   (mail-header-subject (gnus-data-header (car data)))))
11475                 (t nil)))
11476          (end-point (save-excursion
11477                       (goto-char (gnus-data-pos (car data)))
11478                       (if (gnus-summary-go-to-next-thread)
11479                           (point) (point-max))))
11480          articles)
11481     (while (and data
11482                 (< (gnus-data-pos (car data)) end-point))
11483       (when (or (not top-subject)
11484                 (string= top-subject
11485                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11486                              (gnus-simplify-subject-fuzzy
11487                               (mail-header-subject
11488                                (gnus-data-header (car data))))
11489                            (gnus-simplify-subject-re
11490                             (mail-header-subject
11491                              (gnus-data-header (car data)))))))
11492         (push (gnus-data-number (car data)) articles))
11493       (unless (and (setq data (cdr data))
11494                    (> (gnus-data-level (car data)) top-level))
11495         (setq data nil)))
11496     ;; Return the list of articles.
11497     (nreverse articles)))
11498
11499 (defun gnus-summary-rethread-current ()
11500   "Rethread the thread the current article is part of."
11501   (interactive)
11502   (let* ((gnus-show-threads t)
11503          (article (gnus-summary-article-number))
11504          (id (mail-header-id (gnus-summary-article-header)))
11505          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11506     (unless id
11507       (error "No article on the current line"))
11508     (gnus-rebuild-thread id)
11509     (gnus-summary-goto-subject article)))
11510
11511 (defun gnus-summary-reparent-thread ()
11512   "Make the current article child of the marked (or previous) article.
11513
11514 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11515 is non-nil or the Subject: of both articles are the same."
11516   (interactive)
11517   (unless (not (gnus-group-read-only-p))
11518     (error "The current newsgroup does not support article editing"))
11519   (unless (<= (length gnus-newsgroup-processable) 1)
11520     (error "No more than one article may be marked"))
11521   (let ((child (gnus-summary-article-number))
11522         ;; First grab the marked article, otherwise one line up.
11523         (parent (if (not (null gnus-newsgroup-processable))
11524                     (car gnus-newsgroup-processable)
11525                   (save-excursion
11526                     (if (eq (forward-line -1) 0)
11527                         (gnus-summary-article-number)
11528                       (error "Beginning of summary buffer"))))))
11529     (gnus-summary-reparent-children parent (list child))))
11530
11531 (defun gnus-summary-reparent-children (parent children)
11532   "Make PARENT the parent of CHILDREN.
11533 When called interactively, PARENT is the current article and CHILDREN
11534 are the process-marked articles."
11535   (interactive
11536    (list (gnus-summary-article-number)
11537          (gnus-summary-work-articles nil)))
11538   (dolist (child children)
11539     (save-window-excursion
11540       (let ((gnus-article-buffer " *reparent*"))
11541         (unless (not (eq parent child))
11542           (error "An article may not be self-referential"))
11543         (let ((message-id (mail-header-id
11544                            (gnus-summary-article-header parent))))
11545           (unless (and message-id (not (equal message-id "")))
11546             (error "No message-id in desired parent"))
11547           (gnus-with-article child
11548             (save-restriction
11549               (goto-char (point-min))
11550               (message-narrow-to-head)
11551               (if (re-search-forward "^References: " nil t)
11552                   (progn
11553                     (re-search-forward "^[^ \t]" nil t)
11554                     (forward-line -1)
11555                     (end-of-line)
11556                     (insert " " message-id))
11557                 (insert "References: " message-id "\n"))))
11558           (set-buffer gnus-summary-buffer)
11559           (gnus-summary-unmark-all-processable)
11560           (gnus-summary-update-article child)
11561           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11562             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11563           (gnus-summary-rethread-current)
11564           (gnus-message 3 "Article %d is now the child of article %d"
11565                         child parent))))))
11566
11567 (defun gnus-summary-toggle-threads (&optional arg)
11568   "Toggle showing conversation threads.
11569 If ARG is positive number, turn showing conversation threads on."
11570   (interactive "P")
11571   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11572     (setq gnus-show-threads
11573           (if (null arg) (not gnus-show-threads)
11574             (> (prefix-numeric-value arg) 0)))
11575     (gnus-summary-prepare)
11576     (gnus-summary-goto-subject current)
11577     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11578     (gnus-summary-position-point)))
11579
11580 (eval-and-compile
11581   (if (fboundp 'remove-overlays)
11582       (defalias 'gnus-remove-overlays 'remove-overlays)
11583     (defun gnus-remove-overlays (beg end name val)
11584       "Clear BEG and END of overlays whose property NAME has value VAL.
11585 For compatibility with XEmacs."
11586       (dolist (ov (gnus-overlays-in beg end))
11587         (when (eq (gnus-overlay-get ov name) val)
11588           (gnus-delete-overlay ov))))))
11589
11590 (defun gnus-summary-show-all-threads ()
11591   "Show all threads."
11592   (interactive)
11593   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11594   (gnus-summary-position-point))
11595
11596 (defsubst gnus-summary--inv (p)
11597   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11598
11599 (defun gnus-summary-show-thread ()
11600   "Show thread subtrees.
11601 Returns nil if no thread was there to be shown."
11602   (interactive)
11603   (let* ((orig (point))
11604          (end (point-at-eol))
11605          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11606          ;; Leave point at bol
11607          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11608          (eoi (when end
11609                 (if (fboundp 'next-single-char-property-change)
11610                     ;; Note: XEmacs version of n-s-c-p-c may return nil
11611                     (or (next-single-char-property-change end 'invisible)
11612                         (point-max))
11613                   (while (progn
11614                            (end-of-line 2)
11615                            (and (not (eobp))
11616                                 (eq (get-char-property (point) 'invisible)
11617                                     'gnus-sum))))
11618                   (point)))))
11619     (when eoi
11620       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11621       (goto-char orig)
11622       (gnus-summary-position-point)
11623       eoi)))
11624
11625 (defun gnus-summary-maybe-hide-threads ()
11626   "If requested, hide the threads that should be hidden."
11627   (when (and gnus-show-threads
11628              gnus-thread-hide-subtree)
11629     (gnus-summary-hide-all-threads
11630      (if (or (consp gnus-thread-hide-subtree)
11631              (functionp gnus-thread-hide-subtree))
11632          (gnus-make-predicate gnus-thread-hide-subtree)
11633        nil))))
11634
11635 ;;; Hiding predicates.
11636
11637 (defun gnus-article-unread-p (header)
11638   (memq (mail-header-number header) gnus-newsgroup-unreads))
11639
11640 (defun gnus-article-unseen-p (header)
11641   (memq (mail-header-number header) gnus-newsgroup-unseen))
11642
11643 (defun gnus-map-articles (predicate articles)
11644   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11645   (apply 'gnus-or (mapcar predicate
11646                           (mapcar (lambda (number)
11647                                     (gnus-summary-article-header number))
11648                                   articles))))
11649
11650 (defun gnus-summary-hide-all-threads (&optional predicate)
11651   "Hide all thread subtrees.
11652 If PREDICATE is supplied, threads that satisfy this predicate
11653 will not be hidden."
11654   (interactive)
11655   (save-excursion
11656     (goto-char (point-min))
11657     (let ((end nil)
11658           (count 0))
11659       (while (not end)
11660         (incf count)
11661         (when (zerop (mod count 1000))
11662           (message "Hiding all threads... %d" count))
11663         (when (or (not predicate)
11664                   (gnus-map-articles
11665                    predicate (gnus-summary-article-children)))
11666             (gnus-summary-hide-thread))
11667         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11668   (gnus-summary-position-point))
11669
11670 (defun gnus-summary-hide-thread ()
11671   "Hide thread subtrees.
11672 If PREDICATE is supplied, threads that satisfy this predicate
11673 will not be hidden.
11674 Returns nil if no threads were there to be hidden."
11675   (interactive)
11676   (beginning-of-line)
11677   (let ((start (point))
11678         (starteol (line-end-position))
11679         (article (gnus-summary-article-number)))
11680     ;; Go forward until either the buffer ends or the subthread ends.
11681     (when (and (not (eobp))
11682                (or (zerop (gnus-summary-next-thread 1 t))
11683                    (goto-char (point-max))))
11684       (if (and (> (point) start)
11685                ;; FIXME: this should actually search for a non-invisible \n.
11686                (search-backward "\n" start t))
11687           (progn
11688             (when (> (point) starteol)
11689               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11690               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11691                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11692                 (gnus-overlay-put ol 'evaporate t)))
11693             (gnus-summary-goto-subject article)
11694             (when (> start (point))
11695               (message "Hiding the thread moved us backwards, aborting!")
11696               (goto-char (point-max))))
11697         (goto-char start)
11698         nil))))
11699
11700 (defun gnus-summary-go-to-next-thread (&optional previous)
11701   "Go to the same level (or less) next thread.
11702 If PREVIOUS is non-nil, go to previous thread instead.
11703 Return the article number moved to, or nil if moving was impossible."
11704   (let ((level (gnus-summary-thread-level))
11705         (way (if previous -1 1))
11706         (beg (point)))
11707     (forward-line way)
11708     (while (and (not (eobp))
11709                 (< level (gnus-summary-thread-level)))
11710       (forward-line way))
11711     (if (eobp)
11712         (progn
11713           (goto-char beg)
11714           nil)
11715       (setq beg (point))
11716       (prog1
11717           (gnus-summary-article-number)
11718         (goto-char beg)))))
11719
11720 (defun gnus-summary-next-thread (n &optional silent)
11721   "Go to the same level next N'th thread.
11722 If N is negative, search backward instead.
11723 Returns the difference between N and the number of skips actually
11724 done.
11725
11726 If SILENT, don't output messages."
11727   (interactive "p")
11728   (let ((backward (< n 0))
11729         (n (abs n)))
11730     (while (and (> n 0)
11731                 (gnus-summary-go-to-next-thread backward))
11732       (decf n))
11733     (unless silent
11734       (gnus-summary-position-point))
11735     (when (and (not silent) (/= 0 n))
11736       (gnus-message 7 "No more threads"))
11737     n))
11738
11739 (defun gnus-summary-prev-thread (n)
11740   "Go to the same level previous N'th thread.
11741 Returns the difference between N and the number of skips actually
11742 done."
11743   (interactive "p")
11744   (gnus-summary-next-thread (- n)))
11745
11746 (defun gnus-summary-go-down-thread ()
11747   "Go down one level in the current thread."
11748   (let ((children (gnus-summary-article-children)))
11749     (when children
11750       (gnus-summary-goto-subject (car children)))))
11751
11752 (defun gnus-summary-go-up-thread ()
11753   "Go up one level in the current thread."
11754   (let ((parent (gnus-summary-article-parent)))
11755     (when parent
11756       (gnus-summary-goto-subject parent))))
11757
11758 (defun gnus-summary-down-thread (n)
11759   "Go down thread N steps.
11760 If N is negative, go up instead.
11761 Returns the difference between N and how many steps down that were
11762 taken."
11763   (interactive "p")
11764   (let ((up (< n 0))
11765         (n (abs n)))
11766     (while (and (> n 0)
11767                 (if up (gnus-summary-go-up-thread)
11768                   (gnus-summary-go-down-thread)))
11769       (setq n (1- n)))
11770     (gnus-summary-position-point)
11771     (when (/= 0 n)
11772       (gnus-message 7 "Can't go further"))
11773     n))
11774
11775 (defun gnus-summary-up-thread (n)
11776   "Go up thread N steps.
11777 If N is negative, go down instead.
11778 Returns the difference between N and how many steps down that were
11779 taken."
11780   (interactive "p")
11781   (gnus-summary-down-thread (- n)))
11782
11783 (defun gnus-summary-top-thread ()
11784   "Go to the top of the thread."
11785   (interactive)
11786   (while (gnus-summary-go-up-thread))
11787   (gnus-summary-article-number))
11788
11789 (defun gnus-summary-expire-thread ()
11790   "Mark articles under current thread as expired."
11791   (interactive)
11792   (gnus-summary-kill-thread 0))
11793
11794 (defun gnus-summary-kill-thread (&optional unmark)
11795   "Mark articles under current thread as read.
11796 If the prefix argument is positive, remove any kinds of marks.
11797 If the prefix argument is zero, mark thread as expired.
11798 If the prefix argument is negative, tick articles instead."
11799   (interactive "P")
11800   (when unmark
11801     (setq unmark (prefix-numeric-value unmark)))
11802   (let ((articles (gnus-summary-articles-in-thread))
11803         (hide (or (null unmark) (= unmark 0))))
11804     (save-excursion
11805       ;; Expand the thread.
11806       (gnus-summary-show-thread)
11807       ;; Mark all the articles.
11808       (while articles
11809         (gnus-summary-goto-subject (car articles))
11810         (cond ((null unmark)
11811                (gnus-summary-mark-article-as-read gnus-killed-mark))
11812               ((> unmark 0)
11813                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11814               ((= unmark 0)
11815                (gnus-summary-mark-article nil gnus-expirable-mark))
11816               (t
11817                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11818         (setq articles (cdr articles))))
11819     ;; Hide killed subtrees when hide is true.
11820     (and hide
11821          gnus-thread-hide-killed
11822          (gnus-summary-hide-thread))
11823     ;; If hide is t, go to next unread subject.
11824     (when hide
11825       ;; Go to next unread subject.
11826       (gnus-summary-next-subject 1 t)))
11827   (gnus-set-mode-line 'summary))
11828
11829 ;; Summary sorting commands
11830
11831 (defun gnus-summary-sort-by-number (&optional reverse)
11832   "Sort the summary buffer by article number.
11833 Argument REVERSE means reverse order."
11834   (interactive "P")
11835   (gnus-summary-sort 'number reverse))
11836
11837 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11838   "Sort the summary buffer by most recent article number.
11839 Argument REVERSE means reverse order."
11840   (interactive "P")
11841   (gnus-summary-sort 'most-recent-number reverse))
11842
11843 (defun gnus-summary-sort-by-random (&optional reverse)
11844   "Randomize the order in the summary buffer.
11845 Argument REVERSE means to randomize in reverse order."
11846   (interactive "P")
11847   (gnus-summary-sort 'random reverse))
11848
11849 (defun gnus-summary-sort-by-author (&optional reverse)
11850   "Sort the summary buffer by author name alphabetically.
11851 If `case-fold-search' is non-nil, case of letters is ignored.
11852 Argument REVERSE means reverse order."
11853   (interactive "P")
11854   (gnus-summary-sort 'author reverse))
11855
11856 (defun gnus-summary-sort-by-recipient (&optional reverse)
11857   "Sort the summary buffer by recipient name alphabetically.
11858 If `case-fold-search' is non-nil, case of letters is ignored.
11859 Argument REVERSE means reverse order."
11860   (interactive "P")
11861   (gnus-summary-sort 'recipient reverse))
11862
11863 (defun gnus-summary-sort-by-subject (&optional reverse)
11864   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11865 If `case-fold-search' is non-nil, case of letters is ignored.
11866 Argument REVERSE means reverse order."
11867   (interactive "P")
11868   (gnus-summary-sort 'subject reverse))
11869
11870 (defun gnus-summary-sort-by-date (&optional reverse)
11871   "Sort the summary buffer by date.
11872 Argument REVERSE means reverse order."
11873   (interactive "P")
11874   (gnus-summary-sort 'date reverse))
11875
11876 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11877   "Sort the summary buffer by most recent date.
11878 Argument REVERSE means reverse order."
11879   (interactive "P")
11880   (gnus-summary-sort 'most-recent-date reverse))
11881
11882 (defun gnus-summary-sort-by-score (&optional reverse)
11883   "Sort the summary buffer by score.
11884 Argument REVERSE means reverse order."
11885   (interactive "P")
11886   (gnus-summary-sort 'score reverse))
11887
11888 (defun gnus-summary-sort-by-lines (&optional reverse)
11889   "Sort the summary buffer by the number of lines.
11890 Argument REVERSE means reverse order."
11891   (interactive "P")
11892   (gnus-summary-sort 'lines reverse))
11893
11894 (defun gnus-summary-sort-by-chars (&optional reverse)
11895   "Sort the summary buffer by article length.
11896 Argument REVERSE means reverse order."
11897   (interactive "P")
11898   (gnus-summary-sort 'chars reverse))
11899
11900 (defun gnus-summary-sort-by-original (&optional reverse)
11901   "Sort the summary buffer using the default sorting method.
11902 Argument REVERSE means reverse order."
11903   (interactive "P")
11904   (let* ((inhibit-read-only t)
11905          (gnus-summary-prepare-hook nil))
11906     ;; We do the sorting by regenerating the threads.
11907     (gnus-summary-prepare)
11908     ;; Hide subthreads if needed.
11909     (gnus-summary-maybe-hide-threads)))
11910
11911 (defun gnus-summary-sort (predicate reverse)
11912   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11913   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11914          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11915          (gnus-thread-sort-functions
11916           (if (not reverse)
11917               thread
11918             `(lambda (t1 t2)
11919                (,thread t2 t1))))
11920          (gnus-sort-gathered-threads-function
11921           gnus-thread-sort-functions)
11922          (gnus-article-sort-functions
11923           (if (not reverse)
11924               article
11925             `(lambda (t1 t2)
11926                (,article t2 t1))))
11927          (inhibit-read-only t)
11928          (gnus-summary-prepare-hook nil))
11929     ;; We do the sorting by regenerating the threads.
11930     (gnus-summary-prepare)
11931     ;; Hide subthreads if needed.
11932     (gnus-summary-maybe-hide-threads)))
11933
11934 ;; Summary saving commands.
11935
11936 (defun gnus-summary-save-article (&optional n not-saved)
11937   "Save the current article using the default saver function.
11938 If N is a positive number, save the N next articles.
11939 If N is a negative number, save the N previous articles.
11940 If N is nil and any articles have been marked with the process mark,
11941 save those articles instead.
11942 The variable `gnus-default-article-saver' specifies the saver function.
11943
11944 If the optional second argument NOT-SAVED is non-nil, articles saved
11945 will not be marked as saved."
11946   (interactive "P")
11947   (require 'gnus-art)
11948   (let* ((articles (gnus-summary-work-articles n))
11949          (save-buffer (save-excursion
11950                         (nnheader-set-temp-buffer " *Gnus Save*")))
11951          (num (length articles))
11952          ;; Whether to save decoded articles or raw articles.
11953          (decode (when gnus-article-save-coding-system
11954                    (get gnus-default-article-saver :decode)))
11955          ;; When saving many articles in a single file, use the other
11956          ;; function to save articles other than the first one.
11957          (saver2 (get gnus-default-article-saver :function))
11958          (gnus-prompt-before-saving (if saver2
11959                                         t
11960                                       gnus-prompt-before-saving))
11961          (gnus-default-article-saver gnus-default-article-saver)
11962          header file)
11963     (dolist (article articles)
11964       (setq header (gnus-summary-article-header article))
11965       (if (not (vectorp header))
11966           ;; This is a pseudo-article.
11967           (if (assq 'name header)
11968               (gnus-copy-file (cdr (assq 'name header)))
11969             (gnus-message 1 "Article %d is unsavable" article))
11970         ;; This is a real article.
11971         (save-window-excursion
11972           (gnus-summary-select-article decode decode nil article)
11973           (gnus-summary-goto-subject article))
11974         (with-current-buffer save-buffer
11975           (erase-buffer)
11976           (insert-buffer-substring (if decode
11977                                        gnus-article-buffer
11978                                      gnus-original-article-buffer)))
11979         (setq file (gnus-article-save save-buffer file num))
11980         (gnus-summary-remove-process-mark article)
11981         (unless not-saved
11982           (gnus-summary-set-saved-mark article)))
11983       (when saver2
11984         (setq gnus-default-article-saver saver2
11985               saver2 nil)))
11986     (gnus-kill-buffer save-buffer)
11987     (gnus-summary-position-point)
11988     (gnus-set-mode-line 'summary)
11989     n))
11990
11991 (declare-function gnus-summary-save-in-pipe "gnus-art" (&optional command raw))
11992
11993 (defun gnus-summary-pipe-output (&optional n sym)
11994   "Pipe the current article to a subprocess.
11995 If N is a positive number, pipe the N next articles.
11996 If N is a negative number, pipe the N previous articles.
11997 If N is nil and any articles have been marked with the process mark,
11998 pipe those articles instead.
11999 The default command to which articles are piped is specified by the
12000 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
12001 will be prompted for the command.
12002
12003 The properties `:decode' and `:headers' that are put to the function
12004 symbol `gnus-summary-save-in-pipe' control whether this function
12005 decodes articles and what headers to keep (see the doc string for the
12006 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
12007 is neither omitted nor the symbol `r', force including all headers
12008 regardless of the `:headers' property.  If it is the symbol `r',
12009 articles that are not decoded and include all headers will be piped
12010 no matter what the properties `:decode' and `:headers' are."
12011   (interactive (gnus-interactive "P\ny"))
12012   (require 'gnus-art)
12013   (let* ((articles (gnus-summary-work-articles n))
12014          (result-buffer "*Shell Command Output*")
12015          (all-headers (not (memq sym '(nil r))))
12016          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
12017          (raw (eq sym 'r))
12018          (headers (get 'gnus-summary-save-in-pipe :headers))
12019          command result)
12020     (unless (numberp (car articles))
12021       (error "No article to pipe"))
12022     (setq command (gnus-read-shell-command
12023                    (concat "Shell command on "
12024                            (if (cdr articles)
12025                                (format "these %d articles" (length articles))
12026                              "this article")
12027                            ": ")
12028                    gnus-summary-pipe-output-default-command))
12029     (when (string-equal command "")
12030       (error "A command is required"))
12031     (when all-headers
12032       (put 'gnus-summary-save-in-pipe :headers nil))
12033     (unwind-protect
12034         (while articles
12035           (gnus-summary-goto-subject (pop articles))
12036           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12037           (when (and (get-buffer result-buffer)
12038                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12039             (setq result (concat result (with-current-buffer result-buffer
12040                                           (buffer-string))))))
12041       (put 'gnus-summary-save-in-pipe :headers headers))
12042     (unless (zerop (length result))
12043       (if (with-current-buffer (get-buffer-create result-buffer)
12044             (erase-buffer)
12045             (insert result)
12046             (prog1
12047                 (and (= (count-lines (point-min) (point)) 1)
12048                      (progn
12049                        (end-of-line 0)
12050                        (<= (current-column)
12051                            (window-width (minibuffer-window)))))
12052               (goto-char (point-min))))
12053           (message "%s" (substring result 0 -1))
12054         (message nil)
12055         (gnus-configure-windows 'pipe)))))
12056
12057 (defun gnus-summary-save-article-mail (&optional arg)
12058   "Append the current article to a Unix mail box file.
12059 If N is a positive number, save the N next articles.
12060 If N is a negative number, save the N previous articles.
12061 If N is nil and any articles have been marked with the process mark,
12062 save those articles instead."
12063   (interactive "P")
12064   (require 'gnus-art)
12065   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12066     (gnus-summary-save-article arg)))
12067
12068 (defun gnus-summary-save-article-rmail (&optional arg)
12069   "Append the current article to an rmail file.
12070 If N is a positive number, save the N next articles.
12071 If N is a negative number, save the N previous articles.
12072 If N is nil and any articles have been marked with the process mark,
12073 save those articles instead."
12074   (interactive "P")
12075   (require 'gnus-art)
12076   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12077     (gnus-summary-save-article arg)))
12078
12079 (defun gnus-summary-save-article-file (&optional arg)
12080   "Append the current article to a 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-file))
12088     (gnus-summary-save-article arg)))
12089
12090 (defun gnus-summary-write-article-file (&optional arg)
12091   "Write the current article to a file, deleting the previous 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-write-to-file))
12099     (gnus-summary-save-article arg)))
12100
12101 (defun gnus-summary-save-article-body-file (&optional arg)
12102   "Append the current article body 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-body-in-file))
12110     (gnus-summary-save-article arg)))
12111
12112 (defun gnus-summary-write-article-body-file (&optional arg)
12113   "Write the current article body 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-body-to-file))
12121     (gnus-summary-save-article arg)))
12122
12123 (defun gnus-summary-muttprint (&optional arg)
12124   "Print the current article using Muttprint.
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-pipe-to-muttprint))
12132     (gnus-summary-save-article arg t)))
12133
12134 (defun gnus-summary-pipe-message (program)
12135   "Pipe the current article through PROGRAM."
12136   (interactive "sProgram: ")
12137   (gnus-summary-select-article)
12138   (let ((mail-header-separator ""))
12139     (gnus-eval-in-buffer-window gnus-article-buffer
12140       (save-restriction
12141         (widen)
12142         (let ((start (window-start))
12143               (inhibit-read-only t))
12144           (message-pipe-buffer-body program)
12145           (set-window-start (get-buffer-window (current-buffer)) start))))))
12146
12147 (defun gnus-get-split-value (methods)
12148   "Return a value based on the split METHODS."
12149   (let (split-name method result match)
12150     (when methods
12151       (with-current-buffer gnus-original-article-buffer
12152         (save-restriction
12153           (nnheader-narrow-to-headers)
12154           (while (and methods (not split-name))
12155             (goto-char (point-min))
12156             (setq method (pop methods))
12157             (setq match (car method))
12158             (when (cond
12159                    ((stringp match)
12160                     ;; Regular expression.
12161                     (ignore-errors
12162                       (re-search-forward match nil t)))
12163                    ((functionp match)
12164                     ;; Function.
12165                     (save-restriction
12166                       (widen)
12167                       (setq result (funcall match gnus-newsgroup-name))))
12168                    ((consp match)
12169                     ;; Form.
12170                     (save-restriction
12171                       (widen)
12172                       (setq result (eval match)))))
12173               (setq split-name (cdr method))
12174               (cond ((stringp result)
12175                      (push (expand-file-name
12176                             result gnus-article-save-directory)
12177                            split-name))
12178                     ((consp result)
12179                      (setq split-name (append result split-name)))))))))
12180     (nreverse split-name)))
12181
12182 (defun gnus-valid-move-group-p (group)
12183   (and (symbolp group)
12184        (boundp group)
12185        (symbol-name group)
12186        (symbol-value group)
12187        (gnus-get-function (gnus-find-method-for-group
12188                            (symbol-name group)) 'request-accept-article t)))
12189
12190 (defun gnus-read-move-group-name (prompt default articles prefix)
12191   "Read a group name."
12192   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12193          (minibuffer-confirm-incomplete nil) ; XEmacs
12194          (prom
12195           (format "%s %s to"
12196                   prompt
12197                   (if (> (length articles) 1)
12198                       (format "these %d articles" (length articles))
12199                     "this article")))
12200          (to-newsgroup
12201           (cond
12202            ((null split-name)
12203             (gnus-group-completing-read
12204              prom
12205              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12206              nil prefix nil default))
12207            ((= 1 (length split-name))
12208             (gnus-group-completing-read
12209              prom
12210              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12211              nil prefix 'gnus-group-history (car split-name)))
12212            (t
12213             (gnus-completing-read
12214              prom (nreverse split-name) nil nil 'gnus-group-history))))
12215          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12216          encoded)
12217     (when to-newsgroup
12218       (if (or (string= to-newsgroup "")
12219               (string= to-newsgroup prefix))
12220           (setq to-newsgroup default))
12221       (unless to-newsgroup
12222         (error "No group name entered"))
12223       (setq encoded (mm-encode-coding-string
12224                      to-newsgroup
12225                      (gnus-group-name-charset to-method to-newsgroup)))
12226       (or (gnus-active encoded)
12227           (gnus-activate-group encoded nil nil to-method)
12228           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12229                                      to-newsgroup))
12230               (or (and (gnus-request-create-group encoded to-method)
12231                        (gnus-activate-group encoded nil nil to-method)
12232                        (gnus-subscribe-group encoded))
12233                   (error "Couldn't create group %s" to-newsgroup)))
12234           (error "No such group: %s" to-newsgroup))
12235       encoded)))
12236
12237 (defvar gnus-summary-save-parts-counter)
12238 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12239
12240 (defun gnus-summary-save-parts (type dir n &optional reverse)
12241   "Save parts matching TYPE to DIR.
12242 If REVERSE, save parts that do not match TYPE."
12243   (interactive
12244    (list (read-string "Save parts of type: "
12245                       (or (car gnus-summary-save-parts-type-history)
12246                           gnus-summary-save-parts-default-mime)
12247                       'gnus-summary-save-parts-type-history)
12248          (setq gnus-summary-save-parts-last-directory
12249                (read-directory-name "Save to directory: "
12250                                     gnus-summary-save-parts-last-directory
12251                                     nil t))
12252          current-prefix-arg))
12253   (gnus-summary-iterate n
12254     (let ((gnus-display-mime-function nil)
12255           gnus-article-prepare-hook
12256           gnus-article-decode-hook
12257           gnus-display-mime-function
12258           gnus-break-pages
12259           (gnus-inhibit-treatment t))
12260       (gnus-summary-select-article))
12261     (with-current-buffer gnus-article-buffer
12262       (let ((handles (or gnus-article-mime-handles
12263                          (mm-dissect-buffer nil gnus-article-loose-mime)
12264                          (and gnus-article-emulate-mime
12265                               (mm-uu-dissect))))
12266             (gnus-summary-save-parts-counter 1))
12267         (when handles
12268           (gnus-summary-save-parts-1 type dir handles reverse)
12269           (unless gnus-article-mime-handles ;; Don't destroy this case.
12270             (mm-destroy-parts handles)))))))
12271
12272 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12273   (if (stringp (car handle))
12274       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12275               (cdr handle))
12276     (when (if reverse
12277               (not (string-match type (mm-handle-media-type handle)))
12278             (string-match type (mm-handle-media-type handle)))
12279       (let ((file (expand-file-name
12280                    (gnus-map-function
12281                     mm-file-name-rewrite-functions
12282                     (file-name-nondirectory
12283                      (or
12284                       (mm-handle-filename handle)
12285                       (format "%s.%d.%d" gnus-newsgroup-name
12286                               (cdr gnus-article-current)
12287                               gnus-summary-save-parts-counter))))
12288                    dir)))
12289         (incf gnus-summary-save-parts-counter)
12290         (unless (file-exists-p file)
12291           (mm-save-part-to-file handle file))))))
12292
12293 ;; Summary extract commands
12294
12295 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12296   (let ((inhibit-read-only t)
12297         (article (gnus-summary-article-number))
12298         after-article b e)
12299     (unless (gnus-summary-goto-subject article)
12300       (error "No such article: %d" article))
12301     (gnus-summary-position-point)
12302     ;; If all commands are to be bunched up on one line, we collect
12303     ;; them here.
12304     (unless gnus-view-pseudos-separately
12305       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12306             files action)
12307         (while ps
12308           (setq action (cdr (assq 'action (car ps))))
12309           (setq files (list (cdr (assq 'name (car ps)))))
12310           (while (and ps (cdr ps)
12311                       (string= (or action "1")
12312                                (or (cdr (assq 'action (cadr ps))) "2")))
12313             (push (cdr (assq 'name (cadr ps))) files)
12314             (setcdr ps (cddr ps)))
12315           (when files
12316             (when (not (string-match "%s" action))
12317               (push " " files))
12318             (push " " files)
12319             (when (assq 'execute (car ps))
12320               (setcdr (assq 'execute (car ps))
12321                       (funcall (if (string-match "%s" action)
12322                                    'format 'concat)
12323                                action
12324                                (mapconcat
12325                                 (lambda (f)
12326                                   (if (equal f " ")
12327                                       f
12328                                     (shell-quote-argument f)))
12329                                 files " ")))))
12330           (setq ps (cdr ps)))))
12331     (if (and gnus-view-pseudos (not not-view))
12332         (while pslist
12333           (when (assq 'execute (car pslist))
12334             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12335                                   (eq gnus-view-pseudos 'not-confirm)))
12336           (setq pslist (cdr pslist)))
12337       (save-excursion
12338         (while pslist
12339           (setq after-article (or (cdr (assq 'article (car pslist)))
12340                                   (gnus-summary-article-number)))
12341           (gnus-summary-goto-subject after-article)
12342           (forward-line 1)
12343           (setq b (point))
12344           (insert "    " (file-name-nondirectory
12345                           (cdr (assq 'name (car pslist))))
12346                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12347           (setq e (point))
12348           (forward-line -1)             ; back to `b'
12349           (gnus-add-text-properties
12350            b (1- e) (list 'gnus-number gnus-reffed-article-number
12351                           gnus-mouse-face-prop gnus-mouse-face))
12352           (gnus-data-enter
12353            after-article gnus-reffed-article-number
12354            gnus-unread-mark b (car pslist) 0 (- e b))
12355           (setq gnus-newsgroup-unreads
12356                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12357                                          gnus-reffed-article-number))
12358           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12359           (setq pslist (cdr pslist)))))))
12360
12361 (defun gnus-pseudos< (p1 p2)
12362   (let ((c1 (cdr (assq 'action p1)))
12363         (c2 (cdr (assq 'action p2))))
12364     (and c1 c2 (string< c1 c2))))
12365
12366 (defun gnus-request-pseudo-article (props)
12367   (cond ((assq 'execute props)
12368          (gnus-execute-command (cdr (assq 'execute props)))))
12369   (let ((gnus-current-article (gnus-summary-article-number)))
12370     (gnus-run-hooks 'gnus-mark-article-hook)))
12371
12372 (defun gnus-execute-command (command &optional automatic)
12373   (save-excursion
12374     (gnus-article-setup-buffer)
12375     (set-buffer gnus-article-buffer)
12376     (setq buffer-read-only nil)
12377     (let ((command (if automatic command
12378                      (read-string "Command: " (cons command 0)))))
12379       (erase-buffer)
12380       (insert "$ " command "\n\n")
12381       (if gnus-view-pseudo-asynchronously
12382           (start-process "gnus-execute" (current-buffer) shell-file-name
12383                          shell-command-switch command)
12384         (call-process shell-file-name nil t nil
12385                       shell-command-switch command)))))
12386
12387 ;; Summary kill commands.
12388
12389 (defun gnus-summary-edit-global-kill (article)
12390   "Edit the \"global\" kill file."
12391   (interactive (list (gnus-summary-article-number)))
12392   (gnus-group-edit-global-kill article))
12393
12394 (defun gnus-summary-edit-local-kill ()
12395   "Edit a local kill file applied to the current newsgroup."
12396   (interactive)
12397   (setq gnus-current-headers (gnus-summary-article-header))
12398   (gnus-group-edit-local-kill
12399    (gnus-summary-article-number) gnus-newsgroup-name))
12400
12401 ;;; Header reading.
12402
12403 (defun gnus-read-header (id &optional header)
12404   "Read the headers of article ID and enter them into the Gnus system."
12405   (let ((group gnus-newsgroup-name)
12406         (gnus-override-method
12407          (or
12408           gnus-override-method
12409           (and (gnus-news-group-p gnus-newsgroup-name)
12410                (car (gnus-refer-article-methods)))))
12411         where)
12412     ;; First we check to see whether the header in question is already
12413     ;; fetched.
12414     (if (stringp id)
12415         ;; This is a Message-ID.
12416         (setq header (or header (gnus-id-to-header id)))
12417       ;; This is an article number.
12418       (setq header (or header (gnus-summary-article-header id))))
12419     (if (and header
12420              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12421         ;; We have found the header.
12422         header
12423       ;; We have to really fetch the header to this article.
12424       (with-current-buffer nntp-server-buffer
12425         (when (setq where (gnus-request-head id group))
12426           (nnheader-fold-continuation-lines)
12427           (goto-char (point-max))
12428           (insert ".\n")
12429           (goto-char (point-min))
12430           (insert "211 ")
12431           (princ (cond
12432                   ((numberp id) id)
12433                   ((cdr where) (cdr where))
12434                   (header (mail-header-number header))
12435                   (t gnus-reffed-article-number))
12436                  (current-buffer))
12437           (insert " Article retrieved.\n"))
12438         (if (or (not where)
12439                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12440             ()                          ; Malformed head.
12441           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12442             (when (and (bound-and-true-p gnus-registry-enabled)
12443                        (not (gnus-ephemeral-group-p (car where))))
12444               (gnus-registry-handle-action
12445                (mail-header-id header) nil
12446                (gnus-group-prefixed-name
12447                 (car where)
12448                 (or gnus-override-method (gnus-find-method-for-group group)))
12449                (mail-header-subject header)
12450                (mail-header-from header)))
12451             (when (and (stringp id)
12452                        (or
12453                         (not (string= (gnus-group-real-name group)
12454                                       (car where)))
12455                         (not (gnus-server-equal gnus-override-method
12456                                                 (gnus-group-method group)))))
12457               ;; If we fetched by Message-ID and the article came from
12458               ;; a different group (or server), we fudge some bogus
12459               ;; article numbers for this article.
12460               (mail-header-set-number header gnus-reffed-article-number))
12461             (with-current-buffer gnus-summary-buffer
12462               (decf gnus-reffed-article-number)
12463               (gnus-remove-header (mail-header-number header))
12464               (push header gnus-newsgroup-headers)
12465               (setq gnus-current-headers header)
12466               (push (mail-header-number header) gnus-newsgroup-limit)))
12467           header)))))
12468
12469 (defun gnus-remove-header (number)
12470   "Remove header NUMBER from `gnus-newsgroup-headers'."
12471   (if (and gnus-newsgroup-headers
12472            (= number (mail-header-number (car gnus-newsgroup-headers))))
12473       (pop gnus-newsgroup-headers)
12474     (let ((headers gnus-newsgroup-headers))
12475       (while (and (cdr headers)
12476                   (not (= number (mail-header-number (cadr headers)))))
12477         (pop headers))
12478       (when (cdr headers)
12479         (setcdr headers (cddr headers))))))
12480
12481 ;;;
12482 ;;; summary highlights
12483 ;;;
12484
12485 (defun gnus-highlight-selected-summary ()
12486   "Highlight selected article in summary buffer."
12487   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12488   (when gnus-summary-selected-face
12489     (save-excursion
12490       (let* ((beg (point-at-bol))
12491              (end (point-at-eol))
12492              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12493              (from (if (get-text-property beg gnus-mouse-face-prop)
12494                        beg
12495                      (or (next-single-property-change
12496                           beg gnus-mouse-face-prop nil end)
12497                          beg)))
12498              (to
12499               (if (= from end)
12500                   (- from 2)
12501                 (or (next-single-property-change
12502                      from gnus-mouse-face-prop nil end)
12503                     end))))
12504         ;; If no mouse-face prop on line we will have to = from = end,
12505         ;; so we highlight the entire line instead.
12506         (when (= (+ to 2) from)
12507           (setq from beg)
12508           (setq to end))
12509         (if gnus-newsgroup-selected-overlay
12510             ;; Move old overlay.
12511             (gnus-move-overlay
12512              gnus-newsgroup-selected-overlay from to (current-buffer))
12513           ;; Create new overlay.
12514           (gnus-overlay-put
12515            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12516            'face gnus-summary-selected-face))))))
12517
12518 (defvar gnus-summary-highlight-line-cached nil)
12519 (defvar gnus-summary-highlight-line-trigger nil)
12520
12521 (defun gnus-summary-highlight-line-0 ()
12522   (if (and (eq gnus-summary-highlight-line-trigger
12523                gnus-summary-highlight)
12524            gnus-summary-highlight-line-cached)
12525       gnus-summary-highlight-line-cached
12526     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12527           gnus-summary-highlight-line-cached
12528           (let* ((cond (list 'cond))
12529                  (c cond)
12530                  (list gnus-summary-highlight))
12531             (while list
12532               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12533                               nil))
12534               (setq c (cdr c)
12535                     list (cdr list)))
12536             (gnus-byte-compile (list 'lambda nil cond))))))
12537
12538 (defun gnus-summary-highlight-line ()
12539   "Highlight current line according to `gnus-summary-highlight'."
12540   (let* ((beg (point-at-bol))
12541          (article (or (gnus-summary-article-number) gnus-current-article))
12542          (score (or (cdr (assq article
12543                                gnus-newsgroup-scored))
12544                     gnus-summary-default-score 0))
12545          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12546          (inhibit-read-only t)
12547          (default gnus-summary-default-score)
12548          (default-high gnus-summary-default-high-score)
12549          (default-low gnus-summary-default-low-score)
12550          (uncached (and gnus-summary-use-undownloaded-faces
12551                         (memq article gnus-newsgroup-undownloaded)
12552                         (not (memq article gnus-newsgroup-cached)))))
12553     (let ((face (funcall (gnus-summary-highlight-line-0))))
12554       (unless (eq face (gnus-get-text-property-excluding-characters-with-faces beg 'face))
12555         (gnus-put-text-property-excluding-characters-with-faces
12556          beg (point-at-eol) 'face
12557          (setq face (if (boundp face) (symbol-value face) face)))
12558         (when gnus-summary-highlight-line-function
12559           (funcall gnus-summary-highlight-line-function article face))))))
12560
12561 (defun gnus-update-read-articles (group unread &optional compute)
12562   "Update the list of read articles in GROUP.
12563 UNREAD is a sorted list."
12564   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12565         (info (gnus-get-info group))
12566         (prev 1)
12567         read)
12568     (if (or (not info) (not active))
12569         ;; There is no info on this group if it was, in fact,
12570         ;; killed.  Gnus stores no information on killed groups, so
12571         ;; there's nothing to be done.
12572         ;; One could store the information somewhere temporarily,
12573         ;; perhaps...  Hmmm...
12574         ()
12575       ;; Remove any negative articles numbers.
12576       (while (and unread (< (car unread) 0))
12577         (setq unread (cdr unread)))
12578       ;; Remove any expired article numbers
12579       (while (and unread (< (car unread) (car active)))
12580         (setq unread (cdr unread)))
12581       ;; Compute the ranges of read articles by looking at the list of
12582       ;; unread articles.
12583       (while unread
12584         (when (/= (car unread) prev)
12585           (push (if (= prev (1- (car unread))) prev
12586                   (cons prev (1- (car unread))))
12587                 read))
12588         (setq prev (1+ (car unread)))
12589         (setq unread (cdr unread)))
12590       (when (<= prev (cdr active))
12591         (push (cons prev (cdr active)) read))
12592       (setq read (if (> (length read) 1) (nreverse read) read))
12593       (if compute
12594           read
12595         (save-excursion
12596           (let (setmarkundo)
12597             ;; Propagate the read marks to the backend.
12598             (when (and (gnus-method-option-p
12599                         (gnus-find-method-for-group group)
12600                         'server-marks)
12601                        (gnus-check-backend-function 'request-set-mark group))
12602               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12603                     (add (gnus-remove-from-range read (gnus-info-read info))))
12604                 (when (or add del)
12605                   (unless (gnus-check-group group)
12606                     (error "Can't open server for %s" group))
12607                   (gnus-request-set-mark
12608                    group (delq nil (list (if add (list add 'add '(read)))
12609                                          (if del (list del 'del '(read))))))
12610                   (setq setmarkundo
12611                         `(gnus-request-set-mark
12612                           ,group
12613                           ',(delq nil (list
12614                                        (if del (list del 'add '(read)))
12615                                        (if add (list add 'del '(read))))))))))
12616             (set-buffer gnus-group-buffer)
12617             (gnus-undo-register
12618               `(progn
12619                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12620                  (gnus-info-set-read ',info ',(gnus-info-read info))
12621                  (gnus-get-unread-articles-in-group ',info
12622                                                     (gnus-active ,group))
12623                  (gnus-group-update-group ,group t)
12624                  ,setmarkundo))))
12625         ;; Enter this list into the group info.
12626         (gnus-info-set-read info read)
12627         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12628         (gnus-get-unread-articles-in-group info (gnus-active group))
12629         t))))
12630
12631 (defun gnus-offer-save-summaries ()
12632   "Offer to save all active summary buffers."
12633   (let (buffers)
12634     ;; Go through all buffers and find all summaries.
12635     (dolist (buffer (buffer-list))
12636       (when (and (setq buffer (buffer-name buffer))
12637                  (string-match "Summary" buffer)
12638                  (with-current-buffer buffer
12639                    ;; We check that this is, indeed, a summary buffer.
12640                    (and (eq major-mode 'gnus-summary-mode)
12641                         ;; Also make sure this isn't bogus.
12642                         gnus-newsgroup-prepared
12643                         ;; Also make sure that this isn't a
12644                         ;; dead summary buffer.
12645                         (not gnus-dead-summary-mode))))
12646         (push buffer buffers)))
12647     ;; Go through all these summary buffers and offer to save them.
12648     (when buffers
12649       (save-excursion
12650         (if (eq gnus-interactive-exit 'quiet)
12651             (dolist (buffer buffers)
12652               (switch-to-buffer buffer)
12653               (gnus-summary-exit))
12654           (map-y-or-n-p
12655            "Update summary buffer %s? "
12656            (lambda (buf)
12657              (switch-to-buffer buf)
12658              (gnus-summary-exit))
12659            buffers))))))
12660
12661 (defun gnus-summary-setup-default-charset ()
12662   "Setup newsgroup default charset."
12663   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12664       (setq gnus-newsgroup-charset nil)
12665     (let* ((ignored-charsets
12666             (or gnus-newsgroup-ephemeral-ignored-charsets
12667                 (append
12668                  (and gnus-newsgroup-name
12669                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12670                  gnus-newsgroup-ignored-charsets))))
12671       (setq gnus-newsgroup-charset
12672             (or gnus-newsgroup-ephemeral-charset
12673                 (and gnus-newsgroup-name
12674                      (gnus-parameter-charset gnus-newsgroup-name))
12675                 gnus-default-charset))
12676       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12677            ignored-charsets))))
12678
12679 ;;;
12680 ;;; Mime Commands
12681 ;;;
12682
12683 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12684   "Display the current article buffer fully MIME-buttonized.
12685 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12686 treated as multipart/mixed."
12687   (interactive "P")
12688   (require 'gnus-art)
12689   (let ((gnus-unbuttonized-mime-types nil)
12690         (gnus-mime-display-multipart-as-mixed show-all-parts))
12691     (gnus-summary-show-article)))
12692
12693 (defun gnus-summary-repair-multipart (article)
12694   "Add a Content-Type header to a multipart article without one."
12695   (interactive (list (gnus-summary-article-number)))
12696   (gnus-with-article article
12697     (message-narrow-to-head)
12698     (message-remove-header "Mime-Version")
12699     (goto-char (point-max))
12700     (insert "Mime-Version: 1.0\n")
12701     (widen)
12702     (when (search-forward "\n--" nil t)
12703       (let ((separator (buffer-substring (point) (point-at-eol))))
12704         (message-narrow-to-head)
12705         (message-remove-header "Content-Type")
12706         (goto-char (point-max))
12707         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12708                         separator))
12709         (widen))))
12710   (let (gnus-mark-article-hook)
12711     (gnus-summary-select-article t t nil article)))
12712
12713 (defun gnus-summary-toggle-display-buttonized ()
12714   "Toggle the buttonizing of the article buffer."
12715   (interactive)
12716   (require 'gnus-art)
12717   (if (setq gnus-inhibit-mime-unbuttonizing
12718             (not gnus-inhibit-mime-unbuttonizing))
12719       (let ((gnus-unbuttonized-mime-types nil))
12720         (gnus-summary-show-article))
12721     (gnus-summary-show-article)))
12722
12723 ;;;
12724 ;;; Generic summary marking commands
12725 ;;;
12726
12727 (defvar gnus-summary-marking-alist
12728   '((read gnus-del-mark "d")
12729     (unread gnus-unread-mark "u")
12730     (ticked gnus-ticked-mark "!")
12731     (dormant gnus-dormant-mark "?")
12732     (expirable gnus-expirable-mark "e"))
12733   "An alist of names/marks/keystrokes.")
12734
12735 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12736 (defvar gnus-summary-mark-map)
12737
12738 (defun gnus-summary-make-all-marking-commands ()
12739   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12740   (dolist (elem gnus-summary-marking-alist)
12741     (apply 'gnus-summary-make-marking-command elem)))
12742
12743 (defun gnus-summary-make-marking-command (name mark keystroke)
12744   (let ((map (make-sparse-keymap)))
12745     (define-key gnus-summary-generic-mark-map keystroke map)
12746     (dolist (lway `((next "next" next nil "n")
12747                     (next-unread "next unread" next t "N")
12748                     (prev "previous" prev nil "p")
12749                     (prev-unread "previous unread" prev t "P")
12750                     (nomove "" nil nil ,keystroke)))
12751       (let ((func (gnus-summary-make-marking-command-1
12752                    mark (car lway) lway name)))
12753         (setq func (eval func))
12754         (define-key map (nth 4 lway) func)))))
12755
12756 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12757   `(defun ,(intern
12758             (format "gnus-summary-put-mark-as-%s%s"
12759                     name (if (eq way 'nomove)
12760                              ""
12761                            (concat "-" (symbol-name way)))))
12762      (n)
12763      ,(format
12764        "Mark the current article as %s%s.
12765 If N, the prefix, then repeat N times.
12766 If N is negative, move in reverse order.
12767 The difference between N and the actual number of articles marked is
12768 returned."
12769        name (cadr lway))
12770      (interactive "p")
12771      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12772
12773 (defun gnus-summary-generic-mark (n mark move unread)
12774   "Mark N articles with MARK."
12775   (unless (eq major-mode 'gnus-summary-mode)
12776     (error "This command can only be used in the summary buffer"))
12777   (gnus-summary-show-thread)
12778   (let ((nummove
12779          (cond
12780           ((eq move 'next) 1)
12781           ((eq move 'prev) -1)
12782           (t 0))))
12783     (if (zerop nummove)
12784         (setq n 1)
12785       (when (< n 0)
12786         (setq n (abs n)
12787               nummove (* -1 nummove))))
12788     (while (and (> n 0)
12789                 (gnus-summary-mark-article nil mark)
12790                 (zerop (gnus-summary-next-subject nummove unread t)))
12791       (setq n (1- n)))
12792     (when (/= 0 n)
12793       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12794     (gnus-summary-recenter)
12795     (gnus-summary-position-point)
12796     (gnus-set-mode-line 'summary)
12797     n))
12798
12799 (defun gnus-summary-insert-articles (articles)
12800   (when (setq articles
12801               (gnus-sorted-difference articles
12802                                       (mapcar (lambda (h)
12803                                                 (mail-header-number h))
12804                                               gnus-newsgroup-headers)))
12805     (setq gnus-newsgroup-headers
12806           (gnus-merge 'list
12807                       gnus-newsgroup-headers
12808                       (gnus-fetch-headers articles nil t)
12809                       'gnus-article-sort-by-number))
12810     (setq gnus-newsgroup-articles
12811           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12812     ;; Suppress duplicates?
12813     (when gnus-suppress-duplicates
12814       (gnus-dup-suppress-articles))
12815
12816     (if (and gnus-fetch-old-headers
12817              (eq gnus-headers-retrieved-by 'nov))
12818         ;; We might want to build some more threads first.
12819         (if (eq gnus-fetch-old-headers 'invisible)
12820             (gnus-build-all-threads)
12821           (gnus-build-old-threads))
12822       ;; Mark the inserted articles that are unread as unread.
12823       (setq gnus-newsgroup-unreads
12824             (gnus-sorted-nunion
12825              gnus-newsgroup-unreads
12826              (gnus-sorted-nintersection
12827               (gnus-list-of-unread-articles gnus-newsgroup-name)
12828               articles)))
12829       ;; Mark the inserted articles as selected so that the information
12830       ;; of the marks having been changed by a user may be updated when
12831       ;; exiting this group.  See `gnus-summary-update-info'.
12832       (dolist (art articles)
12833         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12834     ;; Let the Gnus agent mark articles as read.
12835     (when gnus-agent
12836       (gnus-agent-get-undownloaded-list))
12837     ;; Remove list identifiers from subject
12838     (gnus-summary-remove-list-identifiers)
12839     ;; First and last article in this newsgroup.
12840     (when gnus-newsgroup-headers
12841       (setq gnus-newsgroup-begin
12842             (mail-header-number (car gnus-newsgroup-headers))
12843             gnus-newsgroup-end
12844             (mail-header-number
12845              (gnus-last-element gnus-newsgroup-headers))))
12846     (when gnus-use-scoring
12847       (gnus-possibly-score-headers))))
12848
12849 (defun gnus-summary-insert-old-articles (&optional all)
12850   "Insert all old articles in this group.
12851 If ALL is non-nil, already read articles become readable.
12852 If ALL is a number, fetch this number of articles."
12853   (interactive "P")
12854   (prog1
12855       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12856             older len)
12857         (setq older
12858               ;; Some nntp servers lie about their active range.  When
12859               ;; this happens, the active range can be in the millions.
12860               ;; Use a compressed range to avoid creating a huge list.
12861               (gnus-range-difference
12862                (gnus-range-difference (list gnus-newsgroup-active) old)
12863                gnus-newsgroup-unexist))
12864         (setq len (gnus-range-length older))
12865         (cond
12866          ((null older) nil)
12867          ((numberp all)
12868           (if (< all len)
12869               (let ((older-range (nreverse older)))
12870                 (setq older nil)
12871
12872                 (while (> all 0)
12873                   (let* ((r (pop older-range))
12874                          (min (if (numberp r) r (car r)))
12875                          (max (if (numberp r) r (cdr r))))
12876                     (while (and (<= min max)
12877                                 (> all 0))
12878                       (push max older)
12879                       (setq all (1- all)
12880                             max (1- max))))))
12881             (setq older (gnus-uncompress-range older))))
12882          (all
12883           (setq older (gnus-uncompress-range older)))
12884          (t
12885           (when (and (numberp gnus-large-newsgroup)
12886                    (> len gnus-large-newsgroup))
12887               (let* ((cursor-in-echo-area nil)
12888                      (initial (gnus-parameter-large-newsgroup-initial
12889                                gnus-newsgroup-name))
12890                      (input
12891                       (read-string
12892                        (format
12893                         "How many articles from %s (%s %d): "
12894                         (gnus-group-decoded-name gnus-newsgroup-name)
12895                         (if initial "max" "default")
12896                         len)
12897                        nil nil
12898                        (and initial
12899                             (number-to-string initial)))))
12900                 (unless (string-match "^[ \t]*$" input)
12901                   (setq all (string-to-number input))
12902                   (if (< all len)
12903                       (let ((older-range (nreverse older)))
12904                         (setq older nil)
12905
12906                         (while (> all 0)
12907                           (let* ((r (pop older-range))
12908                                  (min (if (numberp r) r (car r)))
12909                                  (max (if (numberp r) r (cdr r))))
12910                             (while (and (<= min max)
12911                                         (> all 0))
12912                               (push max older)
12913                               (setq all (1- all)
12914                                     max (1- max))))))))))
12915           (setq older (gnus-uncompress-range older))))
12916         (if (not older)
12917             (message "No old news.")
12918           (gnus-summary-insert-articles older)
12919           (gnus-summary-limit (gnus-sorted-nunion old older))))
12920     (gnus-summary-position-point)))
12921
12922 (defun gnus-summary-insert-new-articles ()
12923   "Insert all new articles in this group."
12924   (interactive)
12925   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12926         (old-high gnus-newsgroup-highest)
12927         (nnmail-fetched-sources (list t))
12928         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12929         i new)
12930     (unless new-active
12931       (error "Couldn't fetch new data"))
12932     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12933     (setq i (cdr gnus-newsgroup-active)
12934           gnus-newsgroup-highest i)
12935     (while (> i old-high)
12936       (push i new)
12937       (decf i))
12938     (if (not new)
12939         (message "No gnus is bad news")
12940       (gnus-summary-insert-articles new)
12941       (setq gnus-newsgroup-unreads
12942             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12943       (gnus-summary-limit (gnus-sorted-nunion old new))))
12944   (gnus-summary-position-point))
12945
12946 ;;; Bookmark support for Gnus.
12947 (declare-function gnus-article-show-summary "gnus-art" ())
12948 (declare-function bookmark-make-record-default
12949                   "bookmark" (&optional no-file no-context posn))
12950 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12951 (declare-function bookmark-default-handler "bookmark" (bmk))
12952 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12953 (defvar bookmark-yank-point)
12954 (defvar bookmark-current-buffer)
12955
12956 (defun gnus-summary-bookmark-make-record ()
12957   "Make a bookmark entry for a Gnus summary buffer."
12958   (let (pos buf)
12959     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12960       (save-restriction              ; FIXME is it necessary to widen?
12961         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12962       (setq buf "art") ; We are recording bookmark from article buffer.
12963       (setq bookmark-yank-point (point))
12964       (setq bookmark-current-buffer (current-buffer))
12965       (gnus-article-show-summary))      ; Go back in summary buffer.
12966     ;; We are now recording bookmark from summary buffer.
12967     (unless buf (setq buf "sum"))
12968     (let* ((subject (elt (gnus-summary-article-header) 1))
12969            (grp     (car gnus-article-current))
12970            (art     (cdr gnus-article-current))
12971            (head    (gnus-summary-article-header art))
12972            (id      (mail-header-id head)))
12973       `(,subject
12974         ,@(condition-case nil
12975               (bookmark-make-record-default 'no-file 'no-context pos)
12976             (wrong-number-of-arguments
12977              (bookmark-make-record-default 'point-only)))
12978         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12979         (group . ,grp) (article . ,art)
12980         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12981
12982 ;;;###autoload
12983 (defun gnus-summary-bookmark-jump (bookmark)
12984   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12985 BOOKMARK is a bookmark name or a bookmark record."
12986   (let ((group    (bookmark-prop-get bookmark 'group))
12987         (article  (bookmark-prop-get bookmark 'article))
12988         (id       (bookmark-prop-get bookmark 'message-id))
12989         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12990     (gnus-fetch-group group (list article))
12991     (gnus-summary-insert-cached-articles)
12992     (gnus-summary-goto-article id nil 'force)
12993     ;; FIXME we have to wait article buffer is ready (only large buffer)
12994     ;; Is there a better solution to know that?
12995     ;; If we don't wait `bookmark-default-handler' will have no chance
12996     ;; to set position. However there is no error, just wrong pos.
12997     (sit-for 1)
12998     (when (string= buf "Gnus-art")
12999       (other-window 1))
13000     (bookmark-default-handler
13001      `(""
13002        (buffer . ,(current-buffer))
13003        . ,(bookmark-get-bookmark-record bookmark)))))
13004
13005 (gnus-summary-make-all-marking-commands)
13006
13007 (gnus-ems-redefine)
13008
13009 (provide 'gnus-sum)
13010
13011 (run-hooks 'gnus-sum-load-hook)
13012
13013 ;; Local Variables:
13014 ;; coding: utf-8
13015 ;; End:
13016
13017 ;;; gnus-sum.el ends here