Combine formatting faces with other highlighting
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996-2012 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 `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   "\177" gnus-summary-prev-page
1827   [delete] gnus-summary-prev-page
1828   [backspace] gnus-summary-prev-page
1829   "\r" gnus-summary-scroll-up
1830   "\M-\r" gnus-summary-scroll-down
1831   "n" gnus-summary-next-unread-article
1832   "p" gnus-summary-prev-unread-article
1833   "N" gnus-summary-next-article
1834   "P" gnus-summary-prev-article
1835   "\M-\C-n" gnus-summary-next-same-subject
1836   "\M-\C-p" gnus-summary-prev-same-subject
1837   "\M-n" gnus-summary-next-unread-subject
1838   "\M-p" gnus-summary-prev-unread-subject
1839   "." gnus-summary-first-unread-article
1840   "," gnus-summary-best-unread-article
1841   "\M-s" gnus-summary-search-article-forward
1842   "\M-r" gnus-summary-search-article-backward
1843   "\M-S" gnus-summary-repeat-search-article-forward
1844   "\M-R" gnus-summary-repeat-search-article-backward
1845   "<" gnus-summary-beginning-of-article
1846   ">" gnus-summary-end-of-article
1847   "j" gnus-summary-goto-article
1848   "^" gnus-summary-refer-parent-article
1849   "\M-^" gnus-summary-refer-article
1850   "u" gnus-summary-tick-article-forward
1851   "!" gnus-summary-tick-article-forward
1852   "U" gnus-summary-tick-article-backward
1853   "d" gnus-summary-mark-as-read-forward
1854   "D" gnus-summary-mark-as-read-backward
1855   "E" gnus-summary-mark-as-expirable
1856   "\M-u" gnus-summary-clear-mark-forward
1857   "\M-U" gnus-summary-clear-mark-backward
1858   "k" gnus-summary-kill-same-subject-and-select
1859   "\C-k" gnus-summary-kill-same-subject
1860   "\M-\C-k" gnus-summary-kill-thread
1861   "\M-\C-l" gnus-summary-lower-thread
1862   "e" gnus-summary-edit-article
1863   "#" gnus-summary-mark-as-processable
1864   "\M-#" gnus-summary-unmark-as-processable
1865   "\M-\C-t" gnus-summary-toggle-threads
1866   "\M-\C-s" gnus-summary-show-thread
1867   "\M-\C-h" gnus-summary-hide-thread
1868   "\M-\C-f" gnus-summary-next-thread
1869   "\M-\C-b" gnus-summary-prev-thread
1870   [(meta down)] gnus-summary-next-thread
1871   [(meta up)] gnus-summary-prev-thread
1872   "\M-\C-u" gnus-summary-up-thread
1873   "\M-\C-d" gnus-summary-down-thread
1874   "&" gnus-summary-execute-command
1875   "c" gnus-summary-catchup-and-exit
1876   "\C-w" gnus-summary-mark-region-as-read
1877   "\C-t" gnus-summary-toggle-truncation
1878   "?" gnus-summary-mark-as-dormant
1879   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1880   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1881   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1882   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1883   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1884   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1885   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1886   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1887   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1888   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1889   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1890   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1891   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1892   "=" gnus-summary-expand-window
1893   "\C-x\C-s" gnus-summary-reselect-current-group
1894   "\M-g" gnus-summary-rescan-group
1895   "\C-c\C-r" gnus-summary-caesar-message
1896   "f" gnus-summary-followup
1897   "F" gnus-summary-followup-with-original
1898   "C" gnus-summary-cancel-article
1899   "r" gnus-summary-reply
1900   "R" gnus-summary-reply-with-original
1901   "\C-c\C-f" gnus-summary-mail-forward
1902   "o" gnus-summary-save-article
1903   "\C-o" gnus-summary-save-article-mail
1904   "|" gnus-summary-pipe-output
1905   "\M-k" gnus-summary-edit-local-kill
1906   "\M-K" gnus-summary-edit-global-kill
1907   ;; "V" gnus-version
1908   "\C-c\C-d" gnus-summary-describe-group
1909   "q" gnus-summary-exit
1910   "Q" gnus-summary-exit-no-update
1911   "\C-c\C-i" gnus-info-find-node
1912   gnus-mouse-2 gnus-mouse-pick-article
1913   [follow-link] mouse-face
1914   "m" gnus-summary-mail-other-window
1915   "a" gnus-summary-post-news
1916   "x" gnus-summary-limit-to-unread
1917   "s" gnus-summary-isearch-article
1918   "\t" gnus-summary-widget-forward
1919   [backtab] gnus-summary-widget-backward
1920   "t" gnus-summary-toggle-header
1921   "g" gnus-summary-show-article
1922   "l" gnus-summary-goto-last-article
1923   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1924   "\C-d" gnus-summary-enter-digest-group
1925   "\M-\C-d" gnus-summary-read-document
1926   "\M-\C-e" gnus-summary-edit-parameters
1927   "\M-\C-a" gnus-summary-customize-parameters
1928   "\C-c\C-b" gnus-bug
1929   "*" gnus-cache-enter-article
1930   "\M-*" gnus-cache-remove-article
1931   "\M-&" gnus-summary-universal-argument
1932   "\C-l" gnus-recenter
1933   "I" gnus-summary-increase-score
1934   "L" gnus-summary-lower-score
1935   "\M-i" gnus-symbolic-argument
1936   "h" gnus-summary-select-article-buffer
1937
1938   "b" gnus-article-view-part
1939   "\M-t" gnus-summary-toggle-display-buttonized
1940
1941   "V" gnus-summary-score-map
1942   "X" gnus-uu-extract-map
1943   "S" gnus-summary-send-map)
1944
1945 ;; Sort of orthogonal keymap
1946 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1947   "t" gnus-summary-tick-article-forward
1948   "!" gnus-summary-tick-article-forward
1949   "d" gnus-summary-mark-as-read-forward
1950   "r" gnus-summary-mark-as-read-forward
1951   "c" gnus-summary-clear-mark-forward
1952   " " gnus-summary-clear-mark-forward
1953   "e" gnus-summary-mark-as-expirable
1954   "x" gnus-summary-mark-as-expirable
1955   "?" gnus-summary-mark-as-dormant
1956   "b" gnus-summary-set-bookmark
1957   "B" gnus-summary-remove-bookmark
1958   "#" gnus-summary-mark-as-processable
1959   "\M-#" gnus-summary-unmark-as-processable
1960   "S" gnus-summary-limit-include-expunged
1961   "C" gnus-summary-catchup
1962   "H" gnus-summary-catchup-to-here
1963   "h" gnus-summary-catchup-from-here
1964   "\C-c" gnus-summary-catchup-all
1965   "k" gnus-summary-kill-same-subject-and-select
1966   "K" gnus-summary-kill-same-subject
1967   "P" gnus-uu-mark-map)
1968
1969 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1970   "c" gnus-summary-clear-above
1971   "u" gnus-summary-tick-above
1972   "m" gnus-summary-mark-above
1973   "k" gnus-summary-kill-below)
1974
1975 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1976   "/" gnus-summary-limit-to-subject
1977   "n" gnus-summary-limit-to-articles
1978   "b" gnus-summary-limit-to-bodies
1979   "h" gnus-summary-limit-to-headers
1980   "w" gnus-summary-pop-limit
1981   "s" gnus-summary-limit-to-subject
1982   "a" gnus-summary-limit-to-author
1983   "u" gnus-summary-limit-to-unread
1984   "m" gnus-summary-limit-to-marks
1985   "M" gnus-summary-limit-exclude-marks
1986   "v" gnus-summary-limit-to-score
1987   "*" gnus-summary-limit-include-cached
1988   "D" gnus-summary-limit-include-dormant
1989   "T" gnus-summary-limit-include-thread
1990   "d" gnus-summary-limit-exclude-dormant
1991   "t" gnus-summary-limit-to-age
1992   "." gnus-summary-limit-to-unseen
1993   "x" gnus-summary-limit-to-extra
1994   "p" gnus-summary-limit-to-display-predicate
1995   "E" gnus-summary-limit-include-expunged
1996   "c" gnus-summary-limit-exclude-childless-dormant
1997   "C" gnus-summary-limit-mark-excluded-as-read
1998   "o" gnus-summary-insert-old-articles
1999   "N" gnus-summary-insert-new-articles
2000   "S" gnus-summary-limit-to-singletons
2001   "r" gnus-summary-limit-to-replied
2002   "R" gnus-summary-limit-to-recipient
2003   "A" gnus-summary-limit-to-address)
2004
2005 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2006   "n" gnus-summary-next-unread-article
2007   "p" gnus-summary-prev-unread-article
2008   "N" gnus-summary-next-article
2009   "P" gnus-summary-prev-article
2010   "\C-n" gnus-summary-next-same-subject
2011   "\C-p" gnus-summary-prev-same-subject
2012   "\M-n" gnus-summary-next-unread-subject
2013   "\M-p" gnus-summary-prev-unread-subject
2014   "f" gnus-summary-first-unread-article
2015   "b" gnus-summary-best-unread-article
2016   "j" gnus-summary-goto-article
2017   "g" gnus-summary-goto-subject
2018   "l" gnus-summary-goto-last-article
2019   "o" gnus-summary-pop-article)
2020
2021 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2022   "k" gnus-summary-kill-thread
2023   "E" gnus-summary-expire-thread
2024   "l" gnus-summary-lower-thread
2025   "i" gnus-summary-raise-thread
2026   "T" gnus-summary-toggle-threads
2027   "t" gnus-summary-rethread-current
2028   "^" gnus-summary-reparent-thread
2029   "\M-^" gnus-summary-reparent-children
2030   "s" gnus-summary-show-thread
2031   "S" gnus-summary-show-all-threads
2032   "h" gnus-summary-hide-thread
2033   "H" gnus-summary-hide-all-threads
2034   "n" gnus-summary-next-thread
2035   "p" gnus-summary-prev-thread
2036   "u" gnus-summary-up-thread
2037   "o" gnus-summary-top-thread
2038   "d" gnus-summary-down-thread
2039   "#" gnus-uu-mark-thread
2040   "\M-#" gnus-uu-unmark-thread)
2041
2042 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2043   "g" gnus-summary-prepare
2044   "c" gnus-summary-insert-cached-articles
2045   "d" gnus-summary-insert-dormant-articles
2046   "t" gnus-summary-insert-ticked-articles)
2047
2048 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2049   "c" gnus-summary-catchup-and-exit
2050   "C" gnus-summary-catchup-all-and-exit
2051   "E" gnus-summary-exit-no-update
2052   "Q" gnus-summary-exit
2053   "Z" gnus-summary-exit
2054   "n" gnus-summary-catchup-and-goto-next-group
2055   "p" gnus-summary-catchup-and-goto-prev-group
2056   "R" gnus-summary-reselect-current-group
2057   "G" gnus-summary-rescan-group
2058   "N" gnus-summary-next-group
2059   "s" gnus-summary-save-newsrc
2060   "P" gnus-summary-prev-group)
2061
2062 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2063   " " gnus-summary-next-page
2064   "n" gnus-summary-next-page
2065   "\177" gnus-summary-prev-page
2066   [delete] gnus-summary-prev-page
2067   "p" gnus-summary-prev-page
2068   "\r" gnus-summary-scroll-up
2069   "\M-\r" gnus-summary-scroll-down
2070   "<" gnus-summary-beginning-of-article
2071   ">" gnus-summary-end-of-article
2072   "b" gnus-summary-beginning-of-article
2073   "e" gnus-summary-end-of-article
2074   "^" gnus-summary-refer-parent-article
2075   "r" gnus-summary-refer-parent-article
2076   "C" gnus-summary-show-complete-article
2077   "D" gnus-summary-enter-digest-group
2078   "R" gnus-summary-refer-references
2079   "T" gnus-summary-refer-thread
2080   "W" gnus-warp-to-article
2081   "g" gnus-summary-show-article
2082   "s" gnus-summary-isearch-article
2083   "\t" gnus-summary-widget-forward
2084   [backtab] gnus-summary-widget-backward
2085   "P" gnus-summary-print-article
2086   "S" gnus-sticky-article
2087   "M" gnus-mailing-list-insinuate
2088   "t" gnus-article-babel)
2089
2090 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2091   "b" gnus-article-add-buttons
2092   "B" gnus-article-add-buttons-to-head
2093   "o" gnus-article-treat-overstrike
2094   "e" gnus-article-emphasize
2095   "w" gnus-article-fill-cited-article
2096   "Q" gnus-article-fill-long-lines
2097   "L" gnus-article-toggle-truncate-lines
2098   "C" gnus-article-capitalize-sentences
2099   "c" gnus-article-remove-cr
2100   "q" gnus-article-de-quoted-unreadable
2101   "6" gnus-article-de-base64-unreadable
2102   "Z" gnus-article-decode-HZ
2103   "A" gnus-article-treat-ansi-sequences
2104   "h" gnus-article-wash-html
2105   "u" gnus-article-unsplit-urls
2106   "s" gnus-summary-force-verify-and-decrypt
2107   "f" gnus-article-display-x-face
2108   "l" gnus-summary-stop-page-breaking
2109   "r" gnus-summary-caesar-message
2110   "m" gnus-summary-morse-message
2111   "t" gnus-summary-toggle-header
2112   "g" gnus-treat-smiley
2113   "v" gnus-summary-verbose-headers
2114   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2115   "p" gnus-article-verify-x-pgp-sig
2116   "d" gnus-article-treat-dumbquotes
2117   "U" gnus-article-treat-non-ascii
2118   "i" gnus-summary-idna-message)
2119
2120 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2121   ;; mnemonic: deuglif*Y*
2122   "u" gnus-article-outlook-unwrap-lines
2123   "a" gnus-article-outlook-repair-attribution
2124   "c" gnus-article-outlook-rearrange-citation
2125   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2126
2127 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2128   "a" gnus-article-hide
2129   "h" gnus-article-hide-headers
2130   "b" gnus-article-hide-boring-headers
2131   "s" gnus-article-hide-signature
2132   "c" gnus-article-hide-citation
2133   "C" gnus-article-hide-citation-in-followups
2134   "l" gnus-article-hide-list-identifiers
2135   "B" gnus-article-strip-banner
2136   "P" gnus-article-hide-pem
2137   "\C-c" gnus-article-hide-citation-maybe)
2138
2139 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2140   "a" gnus-article-highlight
2141   "h" gnus-article-highlight-headers
2142   "c" gnus-article-highlight-citation
2143   "s" gnus-article-highlight-signature)
2144
2145 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2146   "f" gnus-article-treat-fold-headers
2147   "u" gnus-article-treat-unfold-headers
2148   "n" gnus-article-treat-fold-newsgroups)
2149
2150 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2151   "x" gnus-article-display-x-face
2152   "d" gnus-article-display-face
2153   "s" gnus-treat-smiley
2154   "D" gnus-article-remove-images
2155   "W" gnus-article-show-images
2156   "f" gnus-treat-from-picon
2157   "m" gnus-treat-mail-picon
2158   "n" gnus-treat-newsgroups-picon
2159   "g" gnus-treat-from-gravatar
2160   "h" gnus-treat-mail-gravatar)
2161
2162 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2163   "w" gnus-article-decode-mime-words
2164   "c" gnus-article-decode-charset
2165   "v" gnus-mime-view-all-parts
2166   "b" gnus-article-view-part)
2167
2168 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2169   "z" gnus-article-date-ut
2170   "u" gnus-article-date-ut
2171   "l" gnus-article-date-local
2172   "p" gnus-article-date-english
2173   "e" gnus-article-date-lapsed
2174   "o" gnus-article-date-original
2175   "i" gnus-article-date-iso8601
2176   "s" gnus-article-date-user)
2177
2178 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2179   "t" gnus-article-remove-trailing-blank-lines
2180   "l" gnus-article-strip-leading-blank-lines
2181   "m" gnus-article-strip-multiple-blank-lines
2182   "a" gnus-article-strip-blank-lines
2183   "A" gnus-article-strip-all-blank-lines
2184   "s" gnus-article-strip-leading-space
2185   "e" gnus-article-strip-trailing-space
2186   "w" gnus-article-remove-leading-whitespace)
2187
2188 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2189   "v" gnus-version
2190   "d" gnus-summary-describe-group
2191   "h" gnus-summary-describe-briefly
2192   "i" gnus-info-find-node)
2193
2194 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2195   "e" gnus-summary-expire-articles
2196   "\M-\C-e" gnus-summary-expire-articles-now
2197   "\177" gnus-summary-delete-article
2198   [delete] gnus-summary-delete-article
2199   [backspace] gnus-summary-delete-article
2200   "m" gnus-summary-move-article
2201   "r" gnus-summary-respool-article
2202   "w" gnus-summary-edit-article
2203   "c" gnus-summary-copy-article
2204   "B" gnus-summary-crosspost-article
2205   "q" gnus-summary-respool-query
2206   "t" gnus-summary-respool-trace
2207   "i" gnus-summary-import-article
2208   "I" gnus-summary-create-article
2209   "p" gnus-summary-article-posted-p)
2210
2211 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2212   "o" gnus-summary-save-article
2213   "m" gnus-summary-save-article-mail
2214   "F" gnus-summary-write-article-file
2215   "r" gnus-summary-save-article-rmail
2216   "f" gnus-summary-save-article-file
2217   "b" gnus-summary-save-article-body-file
2218   "B" gnus-summary-write-article-body-file
2219   "h" gnus-summary-save-article-folder
2220   "v" gnus-summary-save-article-vm
2221   "p" gnus-summary-pipe-output
2222   "P" gnus-summary-muttprint)
2223
2224 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2225   "b" gnus-summary-display-buttonized
2226   "m" gnus-summary-repair-multipart
2227   "v" gnus-article-view-part
2228   "o" gnus-article-save-part
2229   "O" gnus-article-save-part-and-strip
2230   "r" gnus-article-replace-part
2231   "d" gnus-article-delete-part
2232   "t" gnus-article-view-part-as-type
2233   "j" gnus-article-jump-to-part
2234   "c" gnus-article-copy-part
2235   "C" gnus-article-view-part-as-charset
2236   "e" gnus-article-view-part-externally
2237   "H" gnus-article-browse-html-article
2238   "E" gnus-article-encrypt-body
2239   "i" gnus-article-inline-part
2240   "|" gnus-article-pipe-part)
2241
2242 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2243   "p" gnus-summary-mark-as-processable
2244   "u" gnus-summary-unmark-as-processable
2245   "U" gnus-summary-unmark-all-processable
2246   "v" gnus-uu-mark-over
2247   "s" gnus-uu-mark-series
2248   "r" gnus-uu-mark-region
2249   "g" gnus-uu-unmark-region
2250   "R" gnus-uu-mark-by-regexp
2251   "G" gnus-uu-unmark-by-regexp
2252   "t" gnus-uu-mark-thread
2253   "T" gnus-uu-unmark-thread
2254   "a" gnus-uu-mark-all
2255   "b" gnus-uu-mark-buffer
2256   "S" gnus-uu-mark-sparse
2257   "k" gnus-summary-kill-process-mark
2258   "y" gnus-summary-yank-process-mark
2259   "w" gnus-summary-save-process-mark
2260   "i" gnus-uu-invert-processable)
2261
2262 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2263   ;;"x" gnus-uu-extract-any
2264   "m" gnus-summary-save-parts
2265   "u" gnus-uu-decode-uu
2266   "U" gnus-uu-decode-uu-and-save
2267   "s" gnus-uu-decode-unshar
2268   "S" gnus-uu-decode-unshar-and-save
2269   "o" gnus-uu-decode-save
2270   "O" gnus-uu-decode-save
2271   "b" gnus-uu-decode-binhex
2272   "B" gnus-uu-decode-binhex
2273   "Y" gnus-uu-decode-yenc
2274   "p" gnus-uu-decode-postscript
2275   "P" gnus-uu-decode-postscript-and-save)
2276
2277 (gnus-define-keys
2278     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2279   "u" gnus-uu-decode-uu-view
2280   "U" gnus-uu-decode-uu-and-save-view
2281   "s" gnus-uu-decode-unshar-view
2282   "S" gnus-uu-decode-unshar-and-save-view
2283   "o" gnus-uu-decode-save-view
2284   "O" gnus-uu-decode-save-view
2285   "b" gnus-uu-decode-binhex-view
2286   "B" gnus-uu-decode-binhex-view
2287   "p" gnus-uu-decode-postscript-view
2288   "P" gnus-uu-decode-postscript-and-save-view)
2289
2290 (defvar gnus-article-post-menu nil)
2291
2292 (defconst gnus-summary-menu-maxlen 20)
2293
2294 (defun gnus-summary-menu-split (menu)
2295   ;; If we have lots of elements, divide them into groups of 20
2296   ;; and make a pane (or submenu) for each one.
2297   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2298       (let ((menu menu) sublists next
2299             (i 1))
2300         (while menu
2301           ;; Pull off the next gnus-summary-menu-maxlen elements
2302           ;; and make them the next element of sublist.
2303           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2304           (if next
2305               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2306                       nil))
2307           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2308                                              (aref (car (last menu)) 0)) menu)
2309                                sublists))
2310           (setq i (1+ i))
2311           (setq menu next))
2312         (nreverse sublists))
2313     ;; Few elements--put them all in one pane.
2314     menu))
2315
2316 (defun gnus-summary-make-menu-bar ()
2317   (gnus-turn-off-edit-menu 'summary)
2318
2319   (unless (boundp 'gnus-summary-misc-menu)
2320
2321     (easy-menu-define
2322       gnus-summary-kill-menu gnus-summary-mode-map ""
2323       (cons
2324        "Score"
2325        (nconc
2326         (list
2327          ["Customize" gnus-score-customize t])
2328         (gnus-make-score-map 'increase)
2329         (gnus-make-score-map 'lower)
2330         '(("Mark"
2331            ["Kill below" gnus-summary-kill-below t]
2332            ["Mark above" gnus-summary-mark-above t]
2333            ["Tick above" gnus-summary-tick-above t]
2334            ["Clear above" gnus-summary-clear-above t])
2335           ["Current score" gnus-summary-current-score t]
2336           ["Set score" gnus-summary-set-score t]
2337           ["Switch current score file..." gnus-score-change-score-file t]
2338           ["Set mark below..." gnus-score-set-mark-below t]
2339           ["Set expunge below..." gnus-score-set-expunge-below t]
2340           ["Edit current score file" gnus-score-edit-current-scores t]
2341           ["Edit score file..." gnus-score-edit-file t]
2342           ["Trace score" gnus-score-find-trace t]
2343           ["Find words" gnus-score-find-favourite-words t]
2344           ["Rescore buffer" gnus-summary-rescore t]
2345           ["Increase score..." gnus-summary-increase-score t]
2346           ["Lower score..." gnus-summary-lower-score t]))))
2347
2348     ;; Define both the Article menu in the summary buffer and the
2349     ;; equivalent Commands menu in the article buffer here for
2350     ;; consistency.
2351     (let ((innards
2352            `(("Hide"
2353               ["All" gnus-article-hide t]
2354               ["Headers" gnus-article-hide-headers t]
2355               ["Signature" gnus-article-hide-signature t]
2356               ["Citation" gnus-article-hide-citation t]
2357               ["List identifiers" gnus-article-hide-list-identifiers t]
2358               ["Banner" gnus-article-strip-banner t]
2359               ["Boring headers" gnus-article-hide-boring-headers t])
2360              ("Highlight"
2361               ["All" gnus-article-highlight t]
2362               ["Headers" gnus-article-highlight-headers t]
2363               ["Signature" gnus-article-highlight-signature t]
2364               ["Citation" gnus-article-highlight-citation t])
2365              ("MIME"
2366               ["Words" gnus-article-decode-mime-words t]
2367               ["Charset" gnus-article-decode-charset t]
2368               ["QP" gnus-article-de-quoted-unreadable t]
2369               ["Base64" gnus-article-de-base64-unreadable t]
2370               ["View MIME buttons" gnus-summary-display-buttonized t]
2371               ["View all" gnus-mime-view-all-parts t]
2372               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2373               ["Encrypt body" gnus-article-encrypt-body
2374                :active (not (gnus-group-read-only-p))
2375                ,@(if (featurep 'xemacs) nil
2376                    '(:help "Encrypt the message body on disk"))]
2377               ["Extract all parts..." gnus-summary-save-parts t]
2378               ("Multipart"
2379                ["Repair multipart" gnus-summary-repair-multipart t]
2380                ["Pipe part..." gnus-article-pipe-part t]
2381                ["Inline part" gnus-article-inline-part t]
2382                ["View part as type..." gnus-article-view-part-as-type t]
2383                ["Encrypt body" gnus-article-encrypt-body
2384                 :active (not (gnus-group-read-only-p))
2385                ,@(if (featurep 'xemacs) nil
2386                    '(:help "Encrypt the message body on disk"))]
2387                ["View part externally" gnus-article-view-part-externally t]
2388                ["View HTML parts in browser" gnus-article-browse-html-article t]
2389                ["View part with charset..." gnus-article-view-part-as-charset t]
2390                ["Copy part" gnus-article-copy-part t]
2391                ["Save part..." gnus-article-save-part t]
2392                ["View part" gnus-article-view-part t]))
2393              ("Date"
2394               ["Local" gnus-article-date-local t]
2395               ["ISO8601" gnus-article-date-iso8601 t]
2396               ["UT" gnus-article-date-ut t]
2397               ["Original" gnus-article-date-original t]
2398               ["Lapsed" gnus-article-date-lapsed t]
2399               ["User-defined" gnus-article-date-user t])
2400              ("Display"
2401               ["Remove images" gnus-article-remove-images t]
2402               ["Toggle smiley" gnus-treat-smiley t]
2403               ["Show X-Face" gnus-article-display-x-face t]
2404               ["Show picons in From" gnus-treat-from-picon t]
2405               ["Show picons in mail headers" gnus-treat-mail-picon t]
2406               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2407               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2408               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2409               ("View as different encoding"
2410                ,@(gnus-summary-menu-split
2411                   (mapcar
2412                    (lambda (cs)
2413                      ;; Since easymenu under Emacs doesn't allow
2414                      ;; lambda forms for menu commands, we should
2415                      ;; provide intern'ed function symbols.
2416                      (let ((command (intern (format "\
2417 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2418                        (fset command
2419                              `(lambda ()
2420                                 (interactive)
2421                                 (let ((gnus-summary-show-article-charset-alist
2422                                        '((1 . ,cs))))
2423                                   (gnus-summary-show-article 1))))
2424                        `[,(symbol-name cs) ,command t]))
2425                    (sort (if (fboundp 'coding-system-list)
2426                              (coding-system-list)
2427                            (mapcar 'car mm-mime-mule-charset-alist))
2428                          'string<)))))
2429              ("Washing"
2430               ("Remove Blanks"
2431                ["Leading" gnus-article-strip-leading-blank-lines t]
2432                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2433                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2434                ["All of the above" gnus-article-strip-blank-lines t]
2435                ["All" gnus-article-strip-all-blank-lines t]
2436                ["Leading space" gnus-article-strip-leading-space t]
2437                ["Trailing space" gnus-article-strip-trailing-space t]
2438                ["Leading space in headers"
2439                 gnus-article-remove-leading-whitespace t])
2440               ["Overstrike" gnus-article-treat-overstrike t]
2441               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2442               ["Non-ASCII" gnus-article-treat-non-ascii t]
2443               ["Emphasis" gnus-article-emphasize t]
2444               ["Word wrap" gnus-article-fill-cited-article t]
2445               ["Fill long lines" gnus-article-fill-long-lines t]
2446               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2447               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2448               ["Remove CR" gnus-article-remove-cr t]
2449               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2450               ["Base64" gnus-article-de-base64-unreadable t]
2451               ["Rot 13" gnus-summary-caesar-message
2452                ,@(if (featurep 'xemacs) '(t)
2453                    '(:help "\"Caesar rotate\" article by 13"))]
2454               ["De-IDNA" gnus-summary-idna-message t]
2455               ["Morse decode" gnus-summary-morse-message t]
2456               ["Unix pipe..." gnus-summary-pipe-message t]
2457               ["Add buttons" gnus-article-add-buttons t]
2458               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2459               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2460               ["Verbose header" gnus-summary-verbose-headers t]
2461               ["Toggle header" gnus-summary-toggle-header t]
2462               ["Unfold headers" gnus-article-treat-unfold-headers t]
2463               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2464               ["Html" gnus-article-wash-html t]
2465               ["Unsplit URLs" gnus-article-unsplit-urls t]
2466               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2467               ["Decode HZ" gnus-article-decode-HZ t]
2468               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2469               ("(Outlook) Deuglify"
2470                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2471                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2472                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2473                ["Full (Outlook) deuglify"
2474                 gnus-article-outlook-deuglify-article t])
2475               )
2476              ("Output"
2477               ["Save in default format..." gnus-summary-save-article
2478                ,@(if (featurep 'xemacs) '(t)
2479                    '(:help "Save article using default method"))]
2480               ["Save in file..." gnus-summary-save-article-file
2481                ,@(if (featurep 'xemacs) '(t)
2482                    '(:help "Save article in file"))]
2483               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2484               ["Save in MH folder..." gnus-summary-save-article-folder t]
2485               ["Save in VM folder..." gnus-summary-save-article-vm t]
2486               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2487               ["Save body in file..." gnus-summary-save-article-body-file t]
2488               ["Pipe through a filter..." gnus-summary-pipe-output t]
2489               ["Print with Muttprint..." gnus-summary-muttprint t]
2490               ["Print" gnus-summary-print-article
2491                ,@(if (featurep 'xemacs) '(t)
2492                    '(:help "Generate and print a PostScript image"))])
2493              ("Copy, move,... (Backend)"
2494               ,@(if (featurep 'xemacs) nil
2495                   '(:help "Copying, moving, expiring articles..."))
2496               ["Respool article..." gnus-summary-respool-article t]
2497               ["Move article..." gnus-summary-move-article
2498                (gnus-check-backend-function
2499                 'request-move-article gnus-newsgroup-name)]
2500               ["Copy article..." gnus-summary-copy-article t]
2501               ["Crosspost article..." gnus-summary-crosspost-article
2502                (gnus-check-backend-function
2503                 'request-replace-article gnus-newsgroup-name)]
2504               ["Import file..." gnus-summary-import-article
2505                (gnus-check-backend-function
2506                 'request-accept-article gnus-newsgroup-name)]
2507               ["Create article..." gnus-summary-create-article
2508                (gnus-check-backend-function
2509                 'request-accept-article gnus-newsgroup-name)]
2510               ["Check if posted" gnus-summary-article-posted-p t]
2511               ["Edit article" gnus-summary-edit-article
2512                (not (gnus-group-read-only-p))]
2513               ["Delete article" gnus-summary-delete-article
2514                (gnus-check-backend-function
2515                 'request-expire-articles gnus-newsgroup-name)]
2516               ["Query respool" gnus-summary-respool-query t]
2517               ["Trace respool" gnus-summary-respool-trace t]
2518               ["Delete expirable articles" gnus-summary-expire-articles-now
2519                (gnus-check-backend-function
2520                 'request-expire-articles gnus-newsgroup-name)])
2521              ("Extract"
2522               ["Uudecode" gnus-uu-decode-uu
2523                ,@(if (featurep 'xemacs) '(t)
2524                    '(:help "Decode uuencoded article(s)"))]
2525               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2526               ["Unshar" gnus-uu-decode-unshar t]
2527               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2528               ["Save" gnus-uu-decode-save t]
2529               ["Binhex" gnus-uu-decode-binhex t]
2530               ["PostScript" gnus-uu-decode-postscript t]
2531               ["All MIME parts" gnus-summary-save-parts t])
2532              ("Cache"
2533               ["Enter article" gnus-cache-enter-article t]
2534               ["Remove article" gnus-cache-remove-article t])
2535              ["Translate" gnus-article-babel t]
2536              ["Select article buffer" gnus-summary-select-article-buffer t]
2537              ["Make article buffer sticky" gnus-sticky-article t]
2538              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2539              ["Isearch article..." gnus-summary-isearch-article t]
2540              ["Beginning of the article" gnus-summary-beginning-of-article t]
2541              ["End of the article" gnus-summary-end-of-article t]
2542              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2543              ["Fetch referenced articles" gnus-summary-refer-references t]
2544              ["Fetch current thread" gnus-summary-refer-thread t]
2545              ["Fetch article with id..." gnus-summary-refer-article t]
2546              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2547              ["Redisplay" gnus-summary-show-article t]
2548              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2549       (easy-menu-define
2550         gnus-summary-article-menu gnus-summary-mode-map ""
2551         (cons "Article" innards))
2552
2553       (if (not (keymapp gnus-summary-article-menu))
2554           (easy-menu-define
2555             gnus-article-commands-menu gnus-article-mode-map ""
2556             (cons "Commands" innards))
2557         ;; in Emacs, don't share menu.
2558         (setq gnus-article-commands-menu
2559               (copy-keymap gnus-summary-article-menu))
2560         (define-key gnus-article-mode-map [menu-bar commands]
2561           (cons "Commands" gnus-article-commands-menu))))
2562
2563     (easy-menu-define
2564       gnus-summary-thread-menu gnus-summary-mode-map ""
2565       '("Threads"
2566         ["Find all messages in thread" gnus-summary-refer-thread t]
2567         ["Toggle threading" gnus-summary-toggle-threads t]
2568         ["Hide threads" gnus-summary-hide-all-threads t]
2569         ["Show threads" gnus-summary-show-all-threads t]
2570         ["Hide thread" gnus-summary-hide-thread t]
2571         ["Show thread" gnus-summary-show-thread t]
2572         ["Go to next thread" gnus-summary-next-thread t]
2573         ["Go to previous thread" gnus-summary-prev-thread t]
2574         ["Go down thread" gnus-summary-down-thread t]
2575         ["Go up thread" gnus-summary-up-thread t]
2576         ["Top of thread" gnus-summary-top-thread t]
2577         ["Mark thread as read" gnus-summary-kill-thread t]
2578         ["Mark thread as expired" gnus-summary-expire-thread t]
2579         ["Lower thread score" gnus-summary-lower-thread t]
2580         ["Raise thread score" gnus-summary-raise-thread t]
2581         ["Rethread current" gnus-summary-rethread-current t]))
2582
2583     (easy-menu-define
2584       gnus-summary-post-menu gnus-summary-mode-map ""
2585       `("Post"
2586         ["Send a message (mail or news)" gnus-summary-post-news
2587          ,@(if (featurep 'xemacs) '(t)
2588              '(:help "Compose a new message (mail or news)"))]
2589         ["Followup" gnus-summary-followup
2590          ,@(if (featurep 'xemacs) '(t)
2591              '(:help "Post followup to this article"))]
2592         ["Followup and yank" gnus-summary-followup-with-original
2593          ,@(if (featurep 'xemacs) '(t)
2594              '(:help "Post followup to this article, quoting its contents"))]
2595         ["Supersede article" gnus-summary-supersede-article t]
2596         ["Cancel article" gnus-summary-cancel-article
2597          ,@(if (featurep 'xemacs) '(t)
2598              '(:help "Cancel an article you posted"))]
2599         ["Reply" gnus-summary-reply t]
2600         ["Reply and yank" gnus-summary-reply-with-original t]
2601         ["Wide reply" gnus-summary-wide-reply t]
2602         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2603          ,@(if (featurep 'xemacs) '(t)
2604              '(:help "Mail a reply, quoting this article"))]
2605         ["Very wide reply" gnus-summary-very-wide-reply t]
2606         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2607          ,@(if (featurep 'xemacs) '(t)
2608              '(:help "Mail a very wide reply, quoting this article"))]
2609         ["Mail forward" gnus-summary-mail-forward t]
2610         ["Post forward" gnus-summary-post-forward t]
2611         ["Digest and mail" gnus-uu-digest-mail-forward t]
2612         ["Digest and post" gnus-uu-digest-post-forward t]
2613         ["Resend message" gnus-summary-resend-message t]
2614         ["Resend message edit" gnus-summary-resend-message-edit t]
2615         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2616         ["Send a mail" gnus-summary-mail-other-window t]
2617         ["Create a local message" gnus-summary-news-other-window t]
2618         ["Uuencode and post" gnus-uu-post-news
2619          ,@(if (featurep 'xemacs) '(t)
2620              '(:help "Post a uuencoded article"))]
2621         ["Followup via news" gnus-summary-followup-to-mail t]
2622         ["Followup via news and yank"
2623          gnus-summary-followup-to-mail-with-original t]
2624         ["Strip signature on reply"
2625          (lambda ()
2626            (interactive)
2627            (if (not (memq message-cite-function
2628                           '(message-cite-original-without-signature
2629                             message-cite-original)))
2630                ;; Stupid workaround for XEmacs not honoring :visible.
2631                (message "Can't toggle this value of `message-cite-function'")
2632              (setq message-cite-function
2633                    (if (eq message-cite-function
2634                            'message-cite-original-without-signature)
2635                        'message-cite-original
2636                      'message-cite-original-without-signature))))
2637          ;; XEmacs barfs on :visible.
2638          ,@(if (featurep 'xemacs) nil
2639              '(:visible (memq message-cite-function
2640                               '(message-cite-original-without-signature
2641                                 message-cite-original))))
2642          :style toggle
2643          :selected (eq message-cite-function
2644                        'message-cite-original-without-signature)
2645          ,@(if (featurep 'xemacs) nil
2646              '(:help "Strip signature from cited article when replying."))]
2647         ;;("Draft"
2648         ;;["Send" gnus-summary-send-draft t]
2649         ;;["Send bounced" gnus-resend-bounced-mail t])
2650         ))
2651
2652     (cond
2653      ((not (keymapp gnus-summary-post-menu))
2654       (setq gnus-article-post-menu gnus-summary-post-menu))
2655      ((not gnus-article-post-menu)
2656       ;; Don't share post menu.
2657       (setq gnus-article-post-menu
2658             (copy-keymap gnus-summary-post-menu))))
2659     (define-key gnus-article-mode-map [menu-bar post]
2660       (cons "Post" gnus-article-post-menu))
2661
2662     (easy-menu-define
2663       gnus-summary-misc-menu gnus-summary-mode-map ""
2664       `("Gnus"
2665         ("Mark Read"
2666          ["Mark as read" gnus-summary-mark-as-read-forward t]
2667          ["Mark same subject and select"
2668           gnus-summary-kill-same-subject-and-select t]
2669          ["Mark same subject" gnus-summary-kill-same-subject t]
2670          ["Catchup" gnus-summary-catchup
2671           ,@(if (featurep 'xemacs) '(t)
2672               '(:help "Mark unread articles in this group as read"))]
2673          ["Catchup all" gnus-summary-catchup-all t]
2674          ["Catchup to here" gnus-summary-catchup-to-here t]
2675          ["Catchup from here" gnus-summary-catchup-from-here t]
2676          ["Catchup region" gnus-summary-mark-region-as-read
2677           (gnus-mark-active-p)]
2678          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2679         ("Mark Various"
2680          ["Tick" gnus-summary-tick-article-forward t]
2681          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2682          ["Remove marks" gnus-summary-clear-mark-forward t]
2683          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2684          ["Set bookmark" gnus-summary-set-bookmark t]
2685          ["Remove bookmark" gnus-summary-remove-bookmark t])
2686         ("Limit to"
2687          ["Marks..." gnus-summary-limit-to-marks t]
2688          ["Subject..." gnus-summary-limit-to-subject t]
2689          ["Author..." gnus-summary-limit-to-author t]
2690          ["Recipient..." gnus-summary-limit-to-recipient t]
2691          ["Address..." gnus-summary-limit-to-address t]
2692          ["Age..." gnus-summary-limit-to-age t]
2693          ["Extra..." gnus-summary-limit-to-extra t]
2694          ["Score..." gnus-summary-limit-to-score t]
2695          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2696          ["Unread" gnus-summary-limit-to-unread t]
2697          ["Unseen" gnus-summary-limit-to-unseen t]
2698          ["Singletons" gnus-summary-limit-to-singletons t]
2699          ["Replied" gnus-summary-limit-to-replied t]
2700          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2701          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2702          ["Pop limit" gnus-summary-pop-limit t]
2703          ["Show dormant" gnus-summary-limit-include-dormant t]
2704          ["Hide childless dormant"
2705           gnus-summary-limit-exclude-childless-dormant t]
2706          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2707          ["Hide marked" gnus-summary-limit-exclude-marks t]
2708          ["Show expunged" gnus-summary-limit-include-expunged t])
2709         ("Process Mark"
2710          ["Set mark" gnus-summary-mark-as-processable t]
2711          ["Remove mark" gnus-summary-unmark-as-processable t]
2712          ["Remove all marks" gnus-summary-unmark-all-processable t]
2713          ["Invert marks" gnus-uu-invert-processable t]
2714          ["Mark above" gnus-uu-mark-over t]
2715          ["Mark series" gnus-uu-mark-series t]
2716          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2717          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2718          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2719          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2720          ["Mark all" gnus-uu-mark-all t]
2721          ["Mark buffer" gnus-uu-mark-buffer t]
2722          ["Mark sparse" gnus-uu-mark-sparse t]
2723          ["Mark thread" gnus-uu-mark-thread t]
2724          ["Unmark thread" gnus-uu-unmark-thread t]
2725          ("Process Mark Sets"
2726           ["Kill" gnus-summary-kill-process-mark t]
2727           ["Yank" gnus-summary-yank-process-mark
2728            gnus-newsgroup-process-stack]
2729           ["Save" gnus-summary-save-process-mark t]
2730           ["Run command on marked..." gnus-summary-universal-argument t]))
2731         ("Registry Marks")
2732         ("Scroll article"
2733          ["Page forward" gnus-summary-next-page
2734           ,@(if (featurep 'xemacs) '(t)
2735               '(:help "Show next page of article"))]
2736          ["Page backward" gnus-summary-prev-page
2737           ,@(if (featurep 'xemacs) '(t)
2738               '(:help "Show previous page of article"))]
2739          ["Line forward" gnus-summary-scroll-up t])
2740         ("Move"
2741          ["Next unread article" gnus-summary-next-unread-article t]
2742          ["Previous unread article" gnus-summary-prev-unread-article t]
2743          ["Next article" gnus-summary-next-article t]
2744          ["Previous article" gnus-summary-prev-article t]
2745          ["Next unread subject" gnus-summary-next-unread-subject t]
2746          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2747          ["Next article same subject" gnus-summary-next-same-subject t]
2748          ["Previous article same subject" gnus-summary-prev-same-subject t]
2749          ["First unread article" gnus-summary-first-unread-article t]
2750          ["Best unread article" gnus-summary-best-unread-article t]
2751          ["Go to subject number..." gnus-summary-goto-subject t]
2752          ["Go to article number..." gnus-summary-goto-article t]
2753          ["Go to the last article" gnus-summary-goto-last-article t]
2754          ["Pop article off history" gnus-summary-pop-article t])
2755         ("Sort"
2756          ["Sort by number" gnus-summary-sort-by-number t]
2757          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2758          ["Sort by author" gnus-summary-sort-by-author t]
2759          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2760          ["Sort by subject" gnus-summary-sort-by-subject t]
2761          ["Sort by date" gnus-summary-sort-by-date t]
2762          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2763          ["Sort by score" gnus-summary-sort-by-score t]
2764          ["Sort by lines" gnus-summary-sort-by-lines t]
2765          ["Sort by characters" gnus-summary-sort-by-chars t]
2766          ["Randomize" gnus-summary-sort-by-random t]
2767          ["Original sort" gnus-summary-sort-by-original t])
2768         ("Help"
2769          ["Describe group" gnus-summary-describe-group t]
2770          ["Read manual" gnus-info-find-node t])
2771         ("Modes"
2772          ["Pick and read" gnus-pick-mode t]
2773          ["Binary" gnus-binary-mode t])
2774         ("Regeneration"
2775          ["Regenerate" gnus-summary-prepare t]
2776          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2777          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2778          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2779          ["Toggle threading" gnus-summary-toggle-threads t])
2780         ["See old articles" gnus-summary-insert-old-articles t]
2781         ["See new articles" gnus-summary-insert-new-articles t]
2782         ["Filter articles..." gnus-summary-execute-command t]
2783         ["Run command on articles..." gnus-summary-universal-argument t]
2784         ["Search articles forward..." gnus-summary-search-article-forward t]
2785         ["Search articles backward..." gnus-summary-search-article-backward t]
2786         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2787         ["Expand window" gnus-summary-expand-window t]
2788         ["Expire expirable articles" gnus-summary-expire-articles
2789          (gnus-check-backend-function
2790           'request-expire-articles gnus-newsgroup-name)]
2791         ["Edit local kill file" gnus-summary-edit-local-kill t]
2792         ["Edit main kill file" gnus-summary-edit-global-kill t]
2793         ["Edit group parameters" gnus-summary-edit-parameters t]
2794         ["Customize group parameters" gnus-summary-customize-parameters t]
2795         ["Send a bug report" gnus-bug t]
2796         ("Exit"
2797          ["Catchup and exit" gnus-summary-catchup-and-exit
2798           ,@(if (featurep 'xemacs) '(t)
2799               '(:help "Mark unread articles in this group as read, then exit"))]
2800          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2801          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2802          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2803          ["Exit group" gnus-summary-exit
2804           ,@(if (featurep 'xemacs) '(t)
2805               '(:help "Exit current group, return to group selection mode"))]
2806          ["Exit group without updating" gnus-summary-exit-no-update t]
2807          ["Exit and goto next group" gnus-summary-next-group t]
2808          ["Exit and goto prev group" gnus-summary-prev-group t]
2809          ["Reselect group" gnus-summary-reselect-current-group t]
2810          ["Rescan group" gnus-summary-rescan-group t]
2811          ["Update dribble" gnus-summary-save-newsrc t])))
2812
2813     (gnus-run-hooks 'gnus-summary-menu-hook)))
2814
2815 (defvar gnus-summary-tool-bar-map nil)
2816
2817 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2818 ;; affect _new_ message buffers.  We might add a function that walks thru all
2819 ;; summary-mode buffers and force the update.
2820 (defun gnus-summary-tool-bar-update (&optional symbol value)
2821   "Update summary mode toolbar.
2822 Setter function for custom variables."
2823   (setq-default gnus-summary-tool-bar-map nil)
2824   (when symbol
2825     ;; When used as ":set" function:
2826     (set-default symbol value))
2827   (when (gnus-buffer-live-p gnus-summary-buffer)
2828     (with-current-buffer gnus-summary-buffer
2829       (gnus-summary-make-tool-bar))))
2830
2831 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2832                                      'gnus-summary-tool-bar-gnome
2833                                    'gnus-summary-tool-bar-retro)
2834   "Specifies the Gnus summary tool bar.
2835
2836 It can be either a list or a symbol referring to a list.  See
2837 `gmm-tool-bar-from-list' for the format of the list.  The
2838 default key map is `gnus-summary-mode-map'.
2839
2840 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2841 `gnus-summary-tool-bar-retro'."
2842   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2843                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2844                  (repeat :tag "User defined list" gmm-tool-bar-item)
2845                  (symbol))
2846   :version "23.1" ;; No Gnus
2847   :initialize 'custom-initialize-default
2848   :set 'gnus-summary-tool-bar-update
2849   :group 'gnus-summary)
2850
2851 (defcustom gnus-summary-tool-bar-gnome
2852   '((gnus-summary-post-news "mail/compose" nil)
2853     (gnus-summary-insert-new-articles "mail/inbox" nil
2854                                       :visible (or (not gnus-agent)
2855                                                    gnus-plugged))
2856     (gnus-summary-reply-with-original "mail/reply")
2857     (gnus-summary-reply "mail/reply" nil :visible nil)
2858     (gnus-summary-followup-with-original "mail/reply-all")
2859     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2860     (gnus-summary-mail-forward "mail/forward")
2861     (gnus-summary-save-article "mail/save")
2862     (gnus-summary-search-article-forward "search" nil :visible nil)
2863     (gnus-summary-print-article "print")
2864     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2865     ;; Some new commands that may need more suitable icons:
2866     (gnus-summary-save-newsrc "save" nil :visible nil)
2867     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2868     (gnus-summary-prev-article "left-arrow")
2869     (gnus-summary-next-article "right-arrow")
2870     (gnus-summary-next-page "next-page")
2871     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2872     ;;
2873     ;; Maybe some sort-by-... could be added:
2874     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2875     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2876     (gnus-summary-mark-as-expirable
2877      "delete" nil
2878      :visible (gnus-check-backend-function 'request-expire-articles
2879                                            gnus-newsgroup-name))
2880     (gnus-summary-mark-as-spam
2881      "mail/spam" t
2882      :visible (and (fboundp 'spam-group-ham-contents-p)
2883                    (spam-group-ham-contents-p gnus-newsgroup-name))
2884      :help "Mark as spam")
2885     (gnus-summary-mark-as-read-forward
2886      "mail/not-spam" nil
2887      :visible (and (fboundp 'spam-group-spam-contents-p)
2888                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2889     ;;
2890     (gnus-summary-exit "exit")
2891     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2892     (gnus-info-find-node "help"))
2893   "List of functions for the summary tool bar (GNOME style).
2894
2895 See `gmm-tool-bar-from-list' for the format of the list."
2896   :type '(repeat gmm-tool-bar-item)
2897   :version "23.1" ;; No Gnus
2898   :initialize 'custom-initialize-default
2899   :set 'gnus-summary-tool-bar-update
2900   :group 'gnus-summary)
2901
2902 (defcustom gnus-summary-tool-bar-retro
2903   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2904     (gnus-summary-next-unread-article "gnus/next-ur")
2905     (gnus-summary-post-news "gnus/post")
2906     (gnus-summary-followup-with-original "gnus/fuwo")
2907     (gnus-summary-followup "gnus/followup")
2908     (gnus-summary-reply-with-original "gnus/reply-wo")
2909     (gnus-summary-reply "gnus/reply")
2910     (gnus-summary-caesar-message "gnus/rot13")
2911     (gnus-uu-decode-uu "gnus/uu-decode")
2912     (gnus-summary-save-article-file "gnus/save-aif")
2913     (gnus-summary-save-article "gnus/save-art")
2914     (gnus-uu-post-news "gnus/uu-post")
2915     (gnus-summary-catchup "gnus/catchup")
2916     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2917     (gnus-summary-exit "gnus/exit-summ")
2918     ;; Some new command that may need more suitable icons:
2919     (gnus-summary-print-article "gnus/print" nil :visible nil)
2920     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2921     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2922     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2923     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2924     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2925     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2926     ;;
2927     (gnus-info-find-node "gnus/help" nil :visible nil))
2928   "List of functions for the summary tool bar (retro look).
2929
2930 See `gmm-tool-bar-from-list' for the format of the list."
2931   :type '(repeat gmm-tool-bar-item)
2932   :version "23.1" ;; No Gnus
2933   :initialize 'custom-initialize-default
2934   :set 'gnus-summary-tool-bar-update
2935   :group 'gnus-summary)
2936
2937 (defcustom gnus-summary-tool-bar-zap-list t
2938   "List of icon items from the global tool bar.
2939 These items are not displayed in the Gnus summary mode tool bar.
2940
2941 See `gmm-tool-bar-from-list' for the format of the list."
2942   :type 'gmm-tool-bar-zap-list
2943   :version "23.1" ;; No Gnus
2944   :initialize 'custom-initialize-default
2945   :set 'gnus-summary-tool-bar-update
2946   :group 'gnus-summary)
2947
2948 (defvar image-load-path)
2949 (defvar tool-bar-map)
2950
2951 (defun gnus-summary-make-tool-bar (&optional force)
2952   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2953 When FORCE, rebuild the tool bar."
2954   (when (and (not (featurep 'xemacs))
2955              (boundp 'tool-bar-mode)
2956              tool-bar-mode
2957              (or (not gnus-summary-tool-bar-map) force))
2958     (let* ((load-path
2959             (gmm-image-load-path-for-library "gnus"
2960                                              "mail/save.xpm"
2961                                              nil t))
2962            (image-load-path (cons (car load-path)
2963                                   (when (boundp 'image-load-path)
2964                                     image-load-path)))
2965            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2966                                         gnus-summary-tool-bar-zap-list
2967                                         'gnus-summary-mode-map)))
2968       (when map
2969         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2970         ;; uses its value.
2971         (setq gnus-summary-tool-bar-map map))))
2972   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2973
2974 (defun gnus-make-score-map (type)
2975   "Make a summary score map of type TYPE."
2976   (if t
2977       nil
2978     (let ((headers '(("author" "from" string)
2979                      ("subject" "subject" string)
2980                      ("article body" "body" string)
2981                      ("article head" "head" string)
2982                      ("xref" "xref" string)
2983                      ("extra header" "extra" string)
2984                      ("lines" "lines" number)
2985                      ("followups to author" "followup" string)))
2986           (types '((number ("less than" <)
2987                            ("greater than" >)
2988                            ("equal" =))
2989                    (string ("substring" s)
2990                            ("exact string" e)
2991                            ("fuzzy string" f)
2992                            ("regexp" r))))
2993           (perms '(("temporary" (current-time-string))
2994                    ("permanent" nil)
2995                    ("immediate" now)))
2996           header)
2997       (list
2998        (apply
2999         'nconc
3000         (list
3001          (if (eq type 'lower)
3002              "Lower score"
3003            "Increase score"))
3004         (let (outh)
3005           (while headers
3006             (setq header (car headers))
3007             (setq outh
3008                   (cons
3009                    (apply
3010                     'nconc
3011                     (list (car header))
3012                     (let ((ts (cdr (assoc (nth 2 header) types)))
3013                           outt)
3014                       (while ts
3015                         (setq outt
3016                               (cons
3017                                (apply
3018                                 'nconc
3019                                 (list (caar ts))
3020                                 (let ((ps perms)
3021                                       outp)
3022                                   (while ps
3023                                     (setq outp
3024                                           (cons
3025                                            (vector
3026                                             (caar ps)
3027                                             (list
3028                                              'gnus-summary-score-entry
3029                                              (nth 1 header)
3030                                              (if (or (string= (nth 1 header)
3031                                                               "head")
3032                                                      (string= (nth 1 header)
3033                                                               "body"))
3034                                                  ""
3035                                                (list 'gnus-summary-header
3036                                                      (nth 1 header)))
3037                                              (list 'quote (nth 1 (car ts)))
3038                                              (list 'gnus-score-delta-default
3039                                                    nil)
3040                                              (nth 1 (car ps))
3041                                              t)
3042                                             t)
3043                                            outp))
3044                                     (setq ps (cdr ps)))
3045                                   (list (nreverse outp))))
3046                                outt))
3047                         (setq ts (cdr ts)))
3048                       (list (nreverse outt))))
3049                    outh))
3050             (setq headers (cdr headers)))
3051           (list (nreverse outh))))))))
3052
3053
3054 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3055 (defvar bookmark-make-record-function)
3056 \f
3057 (defvar bidi-paragraph-direction)
3058
3059 (defun gnus-summary-mode (&optional group)
3060   "Major mode for reading articles.
3061
3062 All normal editing commands are switched off.
3063 \\<gnus-summary-mode-map>
3064 Each line in this buffer represents one article.  To read an
3065 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3066 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3067 respectively.
3068
3069 You can also post articles and send mail from this buffer.  To
3070 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3071 of an article, type `\\[gnus-summary-reply]'.
3072
3073 There are approx. one gazillion commands you can execute in this
3074 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3075
3076 The following commands are available:
3077
3078 \\{gnus-summary-mode-map}"
3079   (interactive)
3080   (kill-all-local-variables)
3081   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3082     (gnus-summary-make-local-variables))
3083   (gnus-summary-make-local-variables)
3084   (setq gnus-newsgroup-name group)
3085   (when (gnus-visual-p 'summary-menu 'menu)
3086     (gnus-summary-make-menu-bar)
3087     (gnus-summary-make-tool-bar))
3088   (gnus-make-thread-indent-array)
3089   (gnus-simplify-mode-line)
3090   (setq major-mode 'gnus-summary-mode)
3091   (setq mode-name "Summary")
3092   (use-local-map gnus-summary-mode-map)
3093   (buffer-disable-undo)
3094   (setq buffer-read-only t              ;Disable modification
3095         show-trailing-whitespace nil)
3096   (setq truncate-lines t)
3097   ;; Force paragraph direction to be left-to-right.  Don't make it
3098   ;; bound globally in old Emacsen and XEmacsen.
3099   (set (make-local-variable 'bidi-paragraph-direction) 'left-to-right)
3100   (add-to-invisibility-spec '(gnus-sum . t))
3101   (gnus-summary-set-display-table)
3102   (gnus-set-default-directory)
3103   (make-local-variable 'gnus-summary-line-format)
3104   (make-local-variable 'gnus-summary-line-format-spec)
3105   (make-local-variable 'gnus-summary-dummy-line-format)
3106   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3107   (make-local-variable 'gnus-summary-mark-positions)
3108   (gnus-make-local-hook 'pre-command-hook)
3109   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3110   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3111   (turn-on-gnus-mailing-list-mode)
3112   (mm-enable-multibyte)
3113   (set (make-local-variable 'bookmark-make-record-function)
3114        'gnus-summary-bookmark-make-record)
3115   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3116   (gnus-update-summary-mark-positions))
3117
3118 (defun gnus-summary-make-local-variables ()
3119   "Make all the local summary buffer variables."
3120   (let (global)
3121     (dolist (local gnus-summary-local-variables)
3122       (if (consp local)
3123           (progn
3124             (if (eq (cdr local) 'global)
3125                 ;; Copy the global value of the variable.
3126                 (setq global (symbol-value (car local)))
3127               ;; Use the value from the list.
3128               (setq global (eval (cdr local))))
3129             (set (make-local-variable (car local)) global))
3130         ;; Simple nil-valued local variable.
3131         (set (make-local-variable local) nil)))))
3132
3133 ;; Summary data functions.
3134
3135 (defmacro gnus-data-number (data)
3136   `(car ,data))
3137
3138 (defmacro gnus-data-set-number (data number)
3139   `(setcar ,data ,number))
3140
3141 (defmacro gnus-data-mark (data)
3142   `(nth 1 ,data))
3143
3144 (defmacro gnus-data-set-mark (data mark)
3145   `(setcar (nthcdr 1 ,data) ,mark))
3146
3147 (defmacro gnus-data-pos (data)
3148   `(nth 2 ,data))
3149
3150 (defmacro gnus-data-set-pos (data pos)
3151   `(setcar (nthcdr 2 ,data) ,pos))
3152
3153 (defmacro gnus-data-header (data)
3154   `(nth 3 ,data))
3155
3156 (defmacro gnus-data-set-header (data header)
3157   `(setf (nth 3 ,data) ,header))
3158
3159 (defmacro gnus-data-level (data)
3160   `(nth 4 ,data))
3161
3162 (defmacro gnus-data-unread-p (data)
3163   `(= (nth 1 ,data) gnus-unread-mark))
3164
3165 (defmacro gnus-data-read-p (data)
3166   `(/= (nth 1 ,data) gnus-unread-mark))
3167
3168 (defmacro gnus-data-pseudo-p (data)
3169   `(consp (nth 3 ,data)))
3170
3171 (defmacro gnus-data-find (number)
3172   `(assq ,number gnus-newsgroup-data))
3173
3174 (defmacro gnus-data-find-list (number &optional data)
3175   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3176      (memq (assq ,number bdata)
3177            bdata)))
3178
3179 (defmacro gnus-data-make (number mark pos header level)
3180   `(list ,number ,mark ,pos ,header ,level))
3181
3182 (defun gnus-data-enter (after-article number mark pos header level offset)
3183   (let ((data (gnus-data-find-list after-article)))
3184     (unless data
3185       (error "No such article: %d" after-article))
3186     (setcdr data (cons (gnus-data-make number mark pos header level)
3187                        (cdr data)))
3188     (setq gnus-newsgroup-data-reverse nil)
3189     (gnus-data-update-list (cddr data) offset)))
3190
3191 (defun gnus-data-enter-list (after-article list &optional offset)
3192   (when list
3193     (let ((data (and after-article (gnus-data-find-list after-article)))
3194           (ilist list))
3195       (if (not (or data
3196                    after-article))
3197           (let ((odata gnus-newsgroup-data))
3198             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3199             (when offset
3200               (gnus-data-update-list odata offset)))
3201         ;; Find the last element in the list to be spliced into the main
3202         ;; list.
3203         (setq list (last list))
3204         (if (not data)
3205             (progn
3206               (setcdr list gnus-newsgroup-data)
3207               (setq gnus-newsgroup-data ilist)
3208               (when offset
3209                 (gnus-data-update-list (cdr list) offset)))
3210           (setcdr list (cdr data))
3211           (setcdr data ilist)
3212           (when offset
3213             (gnus-data-update-list (cdr list) offset))))
3214       (setq gnus-newsgroup-data-reverse nil))))
3215
3216 (defun gnus-data-remove (article &optional offset)
3217   (let ((data gnus-newsgroup-data))
3218     (if (= (gnus-data-number (car data)) article)
3219         (progn
3220           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3221                 gnus-newsgroup-data-reverse nil)
3222           (when offset
3223             (gnus-data-update-list gnus-newsgroup-data offset)))
3224       (while (cdr data)
3225         (when (= (gnus-data-number (cadr data)) article)
3226           (setcdr data (cddr data))
3227           (when offset
3228             (gnus-data-update-list (cdr data) offset))
3229           (setq data nil
3230                 gnus-newsgroup-data-reverse nil))
3231         (setq data (cdr data))))))
3232
3233 (defmacro gnus-data-list (backward)
3234   `(if ,backward
3235        (or gnus-newsgroup-data-reverse
3236            (setq gnus-newsgroup-data-reverse
3237                  (reverse gnus-newsgroup-data)))
3238      gnus-newsgroup-data))
3239
3240 (defun gnus-data-update-list (data offset)
3241   "Add OFFSET to the POS of all data entries in DATA."
3242   (setq gnus-newsgroup-data-reverse nil)
3243   (while data
3244     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3245     (setq data (cdr data))))
3246
3247 (defun gnus-summary-article-pseudo-p (article)
3248   "Say whether this article is a pseudo article or not."
3249   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3250
3251 (defmacro gnus-summary-article-sparse-p (article)
3252   "Say whether this article is a sparse article or not."
3253   `(memq ,article gnus-newsgroup-sparse))
3254
3255 (defmacro gnus-summary-article-ancient-p (article)
3256   "Say whether this article is a sparse article or not."
3257   `(memq ,article gnus-newsgroup-ancient))
3258
3259 (defun gnus-article-children (number)
3260   "Return a list of all children to NUMBER."
3261   (let* ((data (gnus-data-find-list number))
3262          (level (gnus-data-level (car data)))
3263          children)
3264     (setq data (cdr data))
3265     (while (and data
3266                 (= (gnus-data-level (car data)) (1+ level)))
3267       (push (gnus-data-number (car data)) children)
3268       (setq data (cdr data)))
3269     children))
3270
3271 (defmacro gnus-summary-skip-intangible ()
3272   "If the current article is intangible, then jump to a different article."
3273   '(let ((to (get-text-property (point) 'gnus-intangible)))
3274      (and to (gnus-summary-goto-subject to))))
3275
3276 (defmacro gnus-summary-article-intangible-p ()
3277   "Say whether this article is intangible or not."
3278   '(get-text-property (point) 'gnus-intangible))
3279
3280 ;; Some summary mode macros.
3281
3282 (defmacro gnus-summary-article-number ()
3283   "The article number of the article on the current line.
3284 If there isn't an article number here, then we return the current
3285 article number."
3286   '(progn
3287      (gnus-summary-skip-intangible)
3288      (or (get-text-property (point) 'gnus-number)
3289          (gnus-summary-last-subject))))
3290
3291 (defmacro gnus-summary-article-header (&optional number)
3292   "Return the header of article NUMBER."
3293   `(gnus-data-header (gnus-data-find
3294                       ,(or number '(gnus-summary-article-number)))))
3295
3296 (defmacro gnus-summary-thread-level (&optional number)
3297   "Return the level of thread that starts with article NUMBER."
3298   `(if (and (eq gnus-summary-make-false-root 'dummy)
3299             (get-text-property (point) 'gnus-intangible))
3300        0
3301      (gnus-data-level (gnus-data-find
3302                        ,(or number '(gnus-summary-article-number))))))
3303
3304 (defmacro gnus-summary-article-mark (&optional number)
3305   "Return the mark of article NUMBER."
3306   `(gnus-data-mark (gnus-data-find
3307                     ,(or number '(gnus-summary-article-number)))))
3308
3309 (defmacro gnus-summary-article-pos (&optional number)
3310   "Return the position of the line of article NUMBER."
3311   `(gnus-data-pos (gnus-data-find
3312                    ,(or number '(gnus-summary-article-number)))))
3313
3314 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3315 (defmacro gnus-summary-article-subject (&optional number)
3316   "Return current subject string or nil if nothing."
3317   `(let ((headers
3318           ,(if number
3319                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3320              '(gnus-data-header (assq (gnus-summary-article-number)
3321                                       gnus-newsgroup-data)))))
3322      (and headers
3323           (vectorp headers)
3324           (mail-header-subject headers))))
3325
3326 (defmacro gnus-summary-article-score (&optional number)
3327   "Return current article score."
3328   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3329                   gnus-newsgroup-scored))
3330        gnus-summary-default-score 0))
3331
3332 (defun gnus-summary-article-children (&optional number)
3333   "Return a list of article numbers that are children of article NUMBER."
3334   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3335          (level (gnus-data-level (car data)))
3336          l children)
3337     (while (and (setq data (cdr data))
3338                 (> (setq l (gnus-data-level (car data))) level))
3339       (and (= (1+ level) l)
3340            (push (gnus-data-number (car data))
3341                  children)))
3342     (nreverse children)))
3343
3344 (defun gnus-summary-article-parent (&optional number)
3345   "Return the article number of the parent of article NUMBER."
3346   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3347                                     (gnus-data-list t)))
3348          (level (gnus-data-level (car data))))
3349     (if (zerop level)
3350         ()                              ; This is a root.
3351       ;; We search until we find an article with a level less than
3352       ;; this one.  That function has to be the parent.
3353       (while (and (setq data (cdr data))
3354                   (not (< (gnus-data-level (car data)) level))))
3355       (and data (gnus-data-number (car data))))))
3356
3357 (defun gnus-unread-mark-p (mark)
3358   "Say whether MARK is the unread mark."
3359   (= mark gnus-unread-mark))
3360
3361 (defun gnus-read-mark-p (mark)
3362   "Say whether MARK is one of the marks that mark as read.
3363 This is all marks except unread, ticked, dormant, and expirable."
3364   (not (or (= mark gnus-unread-mark)
3365            (= mark gnus-ticked-mark)
3366            (= mark gnus-spam-mark)
3367            (= mark gnus-dormant-mark)
3368            (= mark gnus-expirable-mark))))
3369
3370 (defmacro gnus-article-mark (number)
3371   "Return the MARK of article NUMBER.
3372 This macro should only be used when computing the mark the \"first\"
3373 time; i.e., when generating the summary lines.  After that,
3374 `gnus-summary-article-mark' should be used to examine the
3375 marks of articles."
3376   `(cond
3377     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3378     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3379     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3380     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3381     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3382     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3383     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3384     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3385            gnus-ancient-mark))))
3386
3387 ;; Saving hidden threads.
3388
3389 (defmacro gnus-save-hidden-threads (&rest forms)
3390   "Save hidden threads, eval FORMS, and restore the hidden threads."
3391   (let ((config (make-symbol "config")))
3392     `(let ((,config (gnus-hidden-threads-configuration)))
3393        (unwind-protect
3394            (save-excursion
3395              ,@forms)
3396          (gnus-restore-hidden-threads-configuration ,config)))))
3397 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3398 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3399
3400 (defun gnus-data-compute-positions ()
3401   "Compute the positions of all articles."
3402   (setq gnus-newsgroup-data-reverse nil)
3403   (let ((data gnus-newsgroup-data))
3404     (save-excursion
3405       (gnus-save-hidden-threads
3406         (gnus-summary-show-all-threads)
3407         (goto-char (point-min))
3408         (while data
3409           (while (get-text-property (point) 'gnus-intangible)
3410             (forward-line 1))
3411           (gnus-data-set-pos (car data) (+ (point) 3))
3412           (setq data (cdr data))
3413           (forward-line 1))))))
3414
3415 (defun gnus-hidden-threads-configuration ()
3416   "Return the current hidden threads configuration."
3417   (save-excursion
3418     (let (config)
3419       (goto-char (point-min))
3420       (while (not (eobp))
3421         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3422           (push (save-excursion (forward-line 0) (point)) config))
3423         (forward-line 1))
3424       config)))
3425
3426 (defun gnus-restore-hidden-threads-configuration (config)
3427   "Restore hidden threads configuration from CONFIG."
3428   (save-excursion
3429     (let (point (inhibit-read-only t))
3430       (while (setq point (pop config))
3431         (goto-char point)
3432         (gnus-summary-hide-thread)))))
3433
3434 ;; Various summary mode internalish functions.
3435
3436 (defun gnus-mouse-pick-article (e)
3437   (interactive "e")
3438   (mouse-set-point e)
3439   (gnus-summary-next-page nil t))
3440
3441 (defun gnus-summary-set-display-table ()
3442   "Change the display table.
3443 Odd characters have a tendency to mess
3444 up nicely formatted displays - we make all possible glyphs
3445 display only a single character."
3446
3447   ;; We start from the standard display table, if any.
3448   (let ((table (or (copy-sequence standard-display-table)
3449                    (make-display-table)))
3450         (i 32))
3451     ;; Nix out all the control chars...
3452     (while (>= (setq i (1- i)) 0)
3453       (gnus-put-display-table i [??] table))
3454    ;; ... but not newline and cr, of course.  (cr is necessary for the
3455     ;; selective display).
3456     (gnus-put-display-table ?\n nil table)
3457     (gnus-put-display-table ?\r nil table)
3458     ;; We keep TAB as well.
3459     (gnus-put-display-table ?\t nil table)
3460     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3461     ;; Emacs 23 (unicode), that are not set already.
3462     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3463                  160
3464                256)))
3465       (while (>= (setq i (1- i)) 127)
3466         ;; Only modify if the entry is nil.
3467         (unless (gnus-get-display-table i table)
3468           (gnus-put-display-table i [??] table))))
3469     (setq buffer-display-table table)))
3470
3471 (defun gnus-summary-set-article-display-arrow (pos)
3472   "Update the overlay arrow to point to line at position POS."
3473   (when gnus-summary-display-arrow
3474     (make-local-variable 'overlay-arrow-position)
3475     (make-local-variable 'overlay-arrow-string)
3476     (save-excursion
3477       (goto-char pos)
3478       (beginning-of-line)
3479       (unless overlay-arrow-position
3480         (setq overlay-arrow-position (make-marker)))
3481       (setq overlay-arrow-string "=>"
3482             overlay-arrow-position (set-marker overlay-arrow-position
3483                                                (point)
3484                                                (current-buffer))))))
3485
3486 (defun gnus-summary-setup-buffer (group)
3487   "Initialize summary buffer.
3488 If the setup was successful, non-nil is returned."
3489   (let ((buffer (gnus-summary-buffer-name group))
3490         (dead-name (concat "*Dead Summary "
3491                            (gnus-group-decoded-name group) "*")))
3492     ;; If a dead summary buffer exists, we kill it.
3493     (when (gnus-buffer-live-p dead-name)
3494       (gnus-kill-buffer dead-name))
3495     (if (get-buffer buffer)
3496         (progn
3497           (set-buffer buffer)
3498           (setq gnus-summary-buffer (current-buffer))
3499           (not gnus-newsgroup-prepared))
3500       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3501       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3502       (gnus-summary-mode group)
3503       (when (gnus-group-quit-config group)
3504         (set (make-local-variable 'gnus-single-article-buffer) nil))
3505       (make-local-variable 'gnus-article-buffer)
3506       (make-local-variable 'gnus-article-current)
3507       (make-local-variable 'gnus-original-article-buffer)
3508       (setq gnus-newsgroup-name group)
3509       ;; Set any local variables in the group parameters.
3510       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3511       t)))
3512
3513 (defun gnus-set-global-variables ()
3514   "Set the global equivalents of the buffer-local variables.
3515 They are set to the latest values they had.  These reflect the summary
3516 buffer that was in action when the last article was fetched."
3517   (when (eq major-mode 'gnus-summary-mode)
3518     (setq gnus-summary-buffer (current-buffer))
3519     (let ((name gnus-newsgroup-name)
3520           (marked gnus-newsgroup-marked)
3521           (spam gnus-newsgroup-spam-marked)
3522           (unread gnus-newsgroup-unreads)
3523           (headers gnus-current-headers)
3524           (data gnus-newsgroup-data)
3525           (summary gnus-summary-buffer)
3526           (article-buffer gnus-article-buffer)
3527           (original gnus-original-article-buffer)
3528           (gac gnus-article-current)
3529           (reffed gnus-reffed-article-number)
3530           (score-file gnus-current-score-file)
3531           (default-charset gnus-newsgroup-charset)
3532           vlist)
3533       (let ((locals gnus-newsgroup-variables))
3534         (while locals
3535           (if (consp (car locals))
3536               (push (eval (caar locals)) vlist)
3537             (push (eval (car locals)) vlist))
3538           (setq locals (cdr locals)))
3539         (setq vlist (nreverse vlist)))
3540       (with-temp-buffer
3541         (setq gnus-newsgroup-name name
3542               gnus-newsgroup-marked marked
3543               gnus-newsgroup-spam-marked spam
3544               gnus-newsgroup-unreads unread
3545               gnus-current-headers headers
3546               gnus-newsgroup-data data
3547               gnus-article-current gac
3548               gnus-summary-buffer summary
3549               gnus-article-buffer article-buffer
3550               gnus-original-article-buffer original
3551               gnus-reffed-article-number reffed
3552               gnus-current-score-file score-file
3553               gnus-newsgroup-charset default-charset)
3554         (let ((locals gnus-newsgroup-variables))
3555           (while locals
3556             (if (consp (car locals))
3557                 (set (caar locals) (pop vlist))
3558               (set (car locals) (pop vlist)))
3559             (setq locals (cdr locals))))
3560         ;; The article buffer also has local variables.
3561         (when (gnus-buffer-live-p gnus-article-buffer)
3562           (set-buffer gnus-article-buffer)
3563           (setq gnus-summary-buffer summary))))))
3564
3565 (defun gnus-summary-article-unread-p (article)
3566   "Say whether ARTICLE is unread or not."
3567   (memq article gnus-newsgroup-unreads))
3568
3569 (defun gnus-summary-first-article-p (&optional article)
3570   "Return whether ARTICLE is the first article in the buffer."
3571   (if (not (setq article (or article (gnus-summary-article-number))))
3572       nil
3573     (eq article (caar gnus-newsgroup-data))))
3574
3575 (defun gnus-summary-last-article-p (&optional article)
3576   "Return whether ARTICLE is the last article in the buffer."
3577   (if (not (setq article (or article (gnus-summary-article-number))))
3578       ;; All non-existent numbers are the last article.  :-)
3579       t
3580     (not (cdr (gnus-data-find-list article)))))
3581
3582 (defun gnus-make-thread-indent-array (&optional n)
3583   (when (or n
3584             (progn (setq n 200) nil)
3585             (null gnus-thread-indent-array)
3586             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3587     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3588           gnus-thread-indent-array-level gnus-thread-indent-level)
3589     (while (>= n 0)
3590       (aset gnus-thread-indent-array n
3591             (make-string (* n gnus-thread-indent-level) ? ))
3592       (setq n (1- n)))))
3593
3594 (defun gnus-update-summary-mark-positions ()
3595   "Compute where the summary marks are to go."
3596   (save-excursion
3597     (when (gnus-buffer-exists-p gnus-summary-buffer)
3598       (set-buffer gnus-summary-buffer))
3599     (let ((spec gnus-summary-line-format-spec)
3600           pos)
3601       (save-excursion
3602         (gnus-set-work-buffer)
3603         (let ((gnus-tmp-unread ?Z)
3604               (gnus-replied-mark ?Z)
3605               (gnus-score-below-mark ?Z)
3606               (gnus-score-over-mark ?Z)
3607               (gnus-undownloaded-mark ?Z)
3608               (gnus-summary-line-format-spec spec)
3609               (gnus-newsgroup-downloadable '(0))
3610               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3611               case-fold-search ignores)
3612           ;; Here, all marks are bound to Z.
3613           (gnus-summary-insert-line header
3614                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3615           (goto-char (point-min))
3616           ;; Memorize the positions of the same characters as dummy marks.
3617           (while (re-search-forward "[A-D]" nil t)
3618             (push (point) ignores))
3619           (erase-buffer)
3620           ;; We use A-D as dummy marks in order to know column positions
3621           ;; where marks should be inserted.
3622           (setq gnus-tmp-unread ?A
3623                 gnus-replied-mark ?B
3624                 gnus-score-below-mark ?C
3625                 gnus-score-over-mark ?C
3626                 gnus-undownloaded-mark ?D)
3627           (gnus-summary-insert-line header
3628                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3629           ;; Ignore characters which aren't dummy marks.
3630           (dolist (p ignores)
3631             (delete-region (goto-char (1- p)) p)
3632             (insert ?Z))
3633           (goto-char (point-min))
3634           (setq pos (list (cons 'unread
3635                                 (and (search-forward "A" nil t)
3636                                      (- (point) (point-min) 1)))))
3637           (goto-char (point-min))
3638           (push (cons 'replied (and (search-forward "B" nil t)
3639                                     (- (point) (point-min) 1)))
3640                 pos)
3641           (goto-char (point-min))
3642           (push (cons 'score (and (search-forward "C" nil t)
3643                                   (- (point) (point-min) 1)))
3644                 pos)
3645           (goto-char (point-min))
3646           (push (cons 'download (and (search-forward "D" nil t)
3647                                      (- (point) (point-min) 1)))
3648                 pos)))
3649       (setq gnus-summary-mark-positions pos))))
3650
3651 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3652   "Insert a dummy root in the summary buffer."
3653   (beginning-of-line)
3654   (gnus-add-text-properties
3655    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3656    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3657
3658 (defun gnus-summary-extract-address-component (from)
3659   (or (car (funcall gnus-extract-address-components from))
3660       from))
3661
3662 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3663   (let ((mail-parse-charset gnus-newsgroup-charset)
3664         (ignored-from-addresses (gnus-ignored-from-addresses))
3665         ; Is it really necessary to do this next part for each summary line?
3666         ; Luckily, doesn't seem to slow things down much.
3667         (mail-parse-ignored-charsets
3668          (with-current-buffer gnus-summary-buffer
3669            gnus-newsgroup-ignored-charsets)))
3670     (or
3671      (and ignored-from-addresses
3672           (string-match ignored-from-addresses gnus-tmp-from)
3673           (let ((extra-headers (mail-header-extra header))
3674                 to
3675                 newsgroups)
3676             (cond
3677              ((setq to (cdr (assq 'To extra-headers)))
3678               (concat gnus-summary-to-prefix
3679                       (inline
3680                         (gnus-summary-extract-address-component
3681                          (funcall gnus-decode-encoded-address-function to)))))
3682              ((setq newsgroups
3683                     (or
3684                      (cdr (assq 'Newsgroups extra-headers))
3685                      (and
3686                       (memq 'Newsgroups gnus-extra-headers)
3687                       (eq (car (gnus-find-method-for-group
3688                                 gnus-newsgroup-name)) 'nntp)
3689                       (gnus-group-real-name gnus-newsgroup-name))))
3690               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3691      (gnus-string-mark-left-to-right
3692       (inline
3693        (gnus-summary-extract-address-component gnus-tmp-from))))))
3694
3695 (defun gnus-summary-insert-line (gnus-tmp-header
3696                                  gnus-tmp-level gnus-tmp-current
3697                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3698                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3699                                  &optional gnus-tmp-dummy gnus-tmp-score
3700                                  gnus-tmp-process)
3701   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3702       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3703                                           gnus-tmp-level)))
3704   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3705          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3706          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3707          (gnus-tmp-score-char
3708           (if (or (null gnus-summary-default-score)
3709                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3710                       gnus-summary-zcore-fuzz))
3711               ?                         ;Whitespace
3712             (if (< gnus-tmp-score gnus-summary-default-score)
3713                 gnus-score-below-mark gnus-score-over-mark)))
3714          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3715          (gnus-tmp-replied
3716           (cond (gnus-tmp-process gnus-process-mark)
3717                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3718                  gnus-cached-mark)
3719                 (gnus-tmp-replied gnus-replied-mark)
3720                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3721                  gnus-forwarded-mark)
3722                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3723                  gnus-saved-mark)
3724                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3725                  gnus-unseen-mark)
3726                 (t gnus-no-mark)))
3727          (gnus-tmp-downloaded
3728           (cond (undownloaded
3729                  gnus-undownloaded-mark)
3730                 (gnus-newsgroup-agentized
3731                  gnus-downloaded-mark)
3732                 (t
3733                  gnus-no-mark)))
3734          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3735          (gnus-tmp-name
3736           (cond
3737            ((string-match "<[^>]+> *$" gnus-tmp-from)
3738             (let ((beg (match-beginning 0)))
3739               (or (and (string-match "^\".+\"" gnus-tmp-from)
3740                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3741                   (substring gnus-tmp-from 0 beg))))
3742            ((string-match "(.+)" gnus-tmp-from)
3743             (substring gnus-tmp-from
3744                        (1+ (match-beginning 0)) (1- (match-end 0))))
3745            (t gnus-tmp-from)))
3746          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3747          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3748          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3749          (inhibit-read-only t))
3750     (when (string= gnus-tmp-name "")
3751       (setq gnus-tmp-name gnus-tmp-from))
3752     (unless (numberp gnus-tmp-lines)
3753       (setq gnus-tmp-lines -1))
3754     (if (= gnus-tmp-lines -1)
3755         (setq gnus-tmp-lines "?")
3756       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3757     (condition-case ()
3758         (gnus-put-text-property
3759          (point)
3760          (progn (eval gnus-summary-line-format-spec) (point))
3761          'gnus-number gnus-tmp-number)
3762       (error (gnus-message 5 "Error updating the summary line")))
3763     (when (gnus-visual-p 'summary-highlight 'highlight)
3764       (forward-line -1)
3765       (gnus-summary-highlight-line)
3766       (gnus-run-hooks 'gnus-summary-update-hook)
3767       (forward-line 1))))
3768
3769 (defun gnus-summary-update-line (&optional dont-update)
3770   "Update summary line after change."
3771   (when (and gnus-summary-default-score
3772              (not gnus-summary-inhibit-highlight))
3773     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3774            (article (gnus-summary-article-number))
3775            (score (gnus-summary-article-score article)))
3776       (unless dont-update
3777         (if (and gnus-summary-mark-below
3778                  (< (gnus-summary-article-score)
3779                     gnus-summary-mark-below))
3780             ;; This article has a low score, so we mark it as read.
3781             (when (memq article gnus-newsgroup-unreads)
3782               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3783           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3784             ;; This article was previously marked as read on account
3785             ;; of a low score, but now it has risen, so we mark it as
3786             ;; unread.
3787             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3788         (gnus-summary-update-mark
3789          (if (or (null gnus-summary-default-score)
3790                  (<= (abs (- score gnus-summary-default-score))
3791                      gnus-summary-zcore-fuzz))
3792              ?                          ;Whitespace
3793            (if (< score gnus-summary-default-score)
3794                gnus-score-below-mark gnus-score-over-mark))
3795          'score))
3796       ;; Do visual highlighting.
3797       (when (gnus-visual-p 'summary-highlight 'highlight)
3798         (gnus-summary-highlight-line)
3799         (gnus-run-hooks 'gnus-summary-update-hook)))))
3800
3801 (defvar gnus-tmp-new-adopts nil)
3802
3803 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3804   "Return the number of articles in THREAD.
3805 This may be 0 in some cases -- if none of the articles in
3806 the thread are to be displayed."
3807   (let* ((number
3808          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3809           (cond
3810            ((not (listp thread))
3811             1)
3812            ((and (consp thread) (cdr thread))
3813             (apply
3814              '+ 1 (mapcar
3815                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3816            ((null thread)
3817             1)
3818            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3819             1)
3820            (t 0))))
3821     (when (and level (zerop level) gnus-tmp-new-adopts)
3822       (incf number
3823             (apply '+ (mapcar
3824                        'gnus-summary-number-of-articles-in-thread
3825                        gnus-tmp-new-adopts))))
3826     (if char
3827         (if (> number 1) gnus-not-empty-thread-mark
3828           gnus-empty-thread-mark)
3829       number)))
3830
3831 (defsubst gnus-summary-line-message-size (head)
3832   "Return pretty-printed version of message size.
3833 This function is intended to be used in
3834 `gnus-summary-line-format-alist'."
3835   (let ((c (or (mail-header-chars head) -1)))
3836     (cond ((< c 0) "n/a")               ; chars not available
3837           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3838           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3839           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3840           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3841
3842 (defcustom gnus-user-date-format-alist
3843   '(((gnus-seconds-today) . "Today, %H:%M")
3844     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3845     (604800 . "%A %H:%M")               ; That's one week
3846     ((gnus-seconds-month) . "%A %d")
3847     ((gnus-seconds-year) . "%B %d")
3848     (t . "%b %d %Y"))                   ; This one is used when no other
3849                                         ; does match
3850   "Specifies date format depending on age of article.
3851 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3852 seconds) or a Lisp expression evaluating to a number.  When the age of
3853 the article is less than this number, then use `format-time-string'
3854 with the corresponding FORMAT for displaying the date of the article.
3855 If AGE is not a number or a Lisp expression evaluating to a
3856 non-number, then the corresponding FORMAT is used as a default value.
3857
3858 Note that the list is processed from the beginning, so it should be
3859 sorted by ascending AGE.  Also note that items following the first
3860 non-number AGE will be ignored.
3861
3862 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3863 and `gnus-seconds-year' in the AGE spec.  They return the number of
3864 seconds passed since the start of today, of this month, of this year,
3865 respectively."
3866   :version "24.1"
3867   :group 'gnus-summary-format
3868   :type '(alist :key-type sexp :value-type string))
3869
3870 (defun gnus-user-date (messy-date)
3871   "Format the messy-date according to `gnus-user-date-format-alist'.
3872 Returns \"  ?  \" if there's bad input or if another error occurs.
3873 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3874   (condition-case ()
3875       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3876              (now (gnus-float-time))
3877              ;;If we don't find something suitable we'll use this one
3878              (my-format "%b %d '%y"))
3879         (let* ((difference (- now messy-date))
3880                (templist gnus-user-date-format-alist)
3881                (top (eval (caar templist))))
3882           (while (if (numberp top) (< top difference) (not top))
3883             (progn
3884               (setq templist (cdr templist))
3885               (setq top (eval (caar templist)))))
3886           (if (stringp (cdr (car templist)))
3887               (setq my-format (cdr (car templist)))))
3888         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3889     (error "  ?   ")))
3890
3891 (defun gnus-summary-set-local-parameters (group)
3892   "Go through the local params of GROUP and set all variable specs in that list."
3893   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3894                                         ; really variables.
3895     (dolist (elem (gnus-group-find-parameter group))
3896       (and (consp elem)                 ; Has to be a cons.
3897            (consp (cdr elem))           ; The cdr has to be a list.
3898            (symbolp (car elem))         ; Has to be a symbol in there.
3899            (not (memq (car elem) vars))
3900            (ignore-errors
3901              (push (car elem) vars)
3902              ;; Variables like `gnus-show-threads' that are globally
3903              ;; bound, if used as group parameters, need to get to be
3904              ;; buffer-local, whereas just parameters like `gcc-self',
3905              ;; `timestamp', etc. should not be bound as variables.
3906              (if (boundp (car elem))
3907                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3908                (eval (nth 1 elem))))))))
3909
3910 (defun gnus-summary-read-group (group &optional show-all no-article
3911                                       kill-buffer no-display backward
3912                                       select-articles)
3913   "Start reading news in newsgroup GROUP.
3914 If SHOW-ALL is non-nil, already read articles are also listed.
3915 If NO-ARTICLE is non-nil, no article is selected initially.
3916 If NO-DISPLAY, don't generate the summary buffer contents.
3917 If KILL-BUFFER, it should be a buffer that's killed once the new
3918 summary buffer has been generated.
3919 If BACKWARD, move point to the previous group in the group buffer
3920 If SELECT-ARTICLES, only select those articles from GROUP."
3921   (let (result)
3922     (while (and group
3923                 (null (setq result
3924                             (let ((gnus-auto-select-next nil))
3925                               (or (gnus-summary-read-group-1
3926                                    group show-all no-article
3927                                    kill-buffer no-display
3928                                    select-articles)
3929                                   (setq show-all nil
3930                                         select-articles nil)))))
3931                 (eq gnus-auto-select-next 'quietly))
3932       (set-buffer gnus-group-buffer)
3933       ;; The entry function called above goes to the next
3934       ;; group automatically, so we go two groups back
3935       ;; if we are searching for the previous group.
3936       (when backward
3937         (gnus-group-prev-unread-group 2))
3938       (if (not (equal group (gnus-group-group-name)))
3939           (setq group (gnus-group-group-name))
3940         (setq group nil)))
3941     result))
3942
3943 (defun gnus-summary-read-group-1 (group show-all no-article
3944                                         kill-buffer no-display
3945                                         &optional select-articles)
3946   ;; Killed foreign groups can't be entered.
3947   ;;  (when (and (not (gnus-group-native-p group))
3948   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3949   ;;    (error "Dead non-native groups can't be entered"))
3950   (gnus-message 7 "Retrieving newsgroup: %s..."
3951                 (gnus-group-decoded-name group))
3952   (let* ((new-group (gnus-summary-setup-buffer group))
3953          (quit-config (gnus-group-quit-config group))
3954          (did-select (and new-group (gnus-select-newsgroup
3955                                      group show-all select-articles))))
3956     (cond
3957      ;; This summary buffer exists already, so we just select it.
3958      ((not new-group)
3959       (gnus-set-global-variables)
3960       (when kill-buffer
3961         (gnus-kill-or-deaden-summary kill-buffer))
3962       (gnus-configure-windows 'summary 'force)
3963       (gnus-set-mode-line 'summary)
3964       (gnus-summary-position-point)
3965       (message "")
3966       t)
3967      ;; We couldn't select this group.
3968      ((null did-select)
3969       (when (and (eq major-mode 'gnus-summary-mode)
3970                  (not (equal (current-buffer) kill-buffer)))
3971         (kill-buffer (current-buffer))
3972         (if (not quit-config)
3973             (progn
3974               ;; Update the info -- marks might need to be removed,
3975               ;; for instance.
3976               (gnus-summary-update-info)
3977               (set-buffer gnus-group-buffer)
3978               (gnus-group-jump-to-group group)
3979               (gnus-group-next-unread-group 1))
3980           (gnus-handle-ephemeral-exit quit-config)))
3981       (if (null (gnus-list-of-unread-articles group))
3982           (gnus-message 3 "Group %s contains no messages" group)
3983         (gnus-message 3 "Can't select group"))
3984       nil)
3985      ;; The user did a `C-g' while prompting for number of articles,
3986      ;; so we exit this group.
3987      ((eq did-select 'quit)
3988       (and (eq major-mode 'gnus-summary-mode)
3989            (not (equal (current-buffer) kill-buffer))
3990            (kill-buffer (current-buffer)))
3991       (when kill-buffer
3992         (gnus-kill-or-deaden-summary kill-buffer))
3993       (if (not quit-config)
3994           (progn
3995             (set-buffer gnus-group-buffer)
3996             (gnus-group-jump-to-group group)
3997             (gnus-configure-windows 'group 'force))
3998         (gnus-handle-ephemeral-exit quit-config))
3999       ;; Finally signal the quit.
4000       (signal 'quit nil))
4001      ;; The group was successfully selected.
4002      (t
4003       (gnus-set-global-variables)
4004       ;; Save the active value in effect when the group was entered.
4005       (setq gnus-newsgroup-active
4006             (gnus-copy-sequence
4007              (gnus-active gnus-newsgroup-name)))
4008       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4009       ;; You can change the summary buffer in some way with this hook.
4010       (gnus-run-hooks 'gnus-select-group-hook)
4011       (when (memq 'summary (gnus-update-format-specifications
4012                             nil 'summary 'summary-mode 'summary-dummy))
4013         ;; The format specification for the summary line was updated,
4014         ;; so we need to update the mark positions as well.
4015         (gnus-update-summary-mark-positions))
4016       ;; Do score processing.
4017       (when gnus-use-scoring
4018         (gnus-possibly-score-headers))
4019       ;; Check whether to fill in the gaps in the threads.
4020       (when gnus-build-sparse-threads
4021         (gnus-build-sparse-threads))
4022       ;; Find the initial limit.
4023       (if show-all
4024           (let ((gnus-newsgroup-dormant nil))
4025             (gnus-summary-initial-limit show-all))
4026         (gnus-summary-initial-limit show-all))
4027       ;; Generate the summary buffer.
4028       (unless no-display
4029         (gnus-summary-prepare))
4030       (when gnus-use-trees
4031         (gnus-tree-open group)
4032         (setq gnus-summary-highlight-line-function
4033               'gnus-tree-highlight-article))
4034       ;; If the summary buffer is empty, but there are some low-scored
4035       ;; articles or some excluded dormants, we include these in the
4036       ;; buffer.
4037       (when (and (zerop (buffer-size))
4038                  (not no-display))
4039         (cond (gnus-newsgroup-dormant
4040                (gnus-summary-limit-include-dormant))
4041               ((and gnus-newsgroup-scored show-all)
4042                (gnus-summary-limit-include-expunged t))))
4043       ;; Function `gnus-apply-kill-file' must be called in this hook.
4044       (gnus-run-hooks 'gnus-apply-kill-hook)
4045       (if (and (zerop (buffer-size))
4046                (not no-display))
4047           (progn
4048             ;; This newsgroup is empty.
4049             (gnus-summary-catchup-and-exit nil t)
4050             (gnus-message 6 "No unread news")
4051             (when kill-buffer
4052               (gnus-kill-or-deaden-summary kill-buffer))
4053             ;; Return nil from this function.
4054             nil)
4055         ;; Hide conversation thread subtrees.  We cannot do this in
4056         ;; gnus-summary-prepare-hook since kill processing may not
4057         ;; work with hidden articles.
4058         (gnus-summary-maybe-hide-threads)
4059         (gnus-configure-windows 'summary)
4060         (when kill-buffer
4061           (gnus-kill-or-deaden-summary kill-buffer))
4062         (gnus-summary-auto-select-subject)
4063         ;; Show first unread article if requested.
4064         (if (and (not no-article)
4065                  (not no-display)
4066                  gnus-newsgroup-unreads
4067                  gnus-auto-select-first)
4068             (progn
4069               (let ((art (gnus-summary-article-number)))
4070                 (unless (and (not gnus-plugged)
4071                              (or (memq art gnus-newsgroup-undownloaded)
4072                                  (memq art gnus-newsgroup-downloadable)))
4073                   (gnus-summary-goto-article art))))
4074           ;; Don't select any articles.
4075           (gnus-summary-position-point)
4076           (gnus-configure-windows 'summary 'force)
4077           (gnus-set-mode-line 'summary))
4078         (when (and gnus-auto-center-group
4079                    (get-buffer-window gnus-group-buffer t))
4080           ;; Gotta use windows, because recenter does weird stuff if
4081           ;; the current buffer ain't the displayed window.
4082           (let ((owin (selected-window)))
4083             (select-window (get-buffer-window gnus-group-buffer t))
4084             (when (gnus-group-goto-group group)
4085               (recenter))
4086             (select-window owin)))
4087         ;; Mark this buffer as "prepared".
4088         (setq gnus-newsgroup-prepared t)
4089         (gnus-run-hooks 'gnus-summary-prepared-hook)
4090         (unless (gnus-ephemeral-group-p group)
4091           (gnus-group-update-group group nil t))
4092         t)))))
4093
4094 (defun gnus-summary-auto-select-subject ()
4095   "Select the subject line on initial group entry."
4096   (goto-char (point-min))
4097   (cond
4098    ((eq gnus-auto-select-subject 'best)
4099     (gnus-summary-best-unread-subject))
4100    ((eq gnus-auto-select-subject 'unread)
4101     (gnus-summary-first-unread-subject))
4102    ((eq gnus-auto-select-subject 'unseen)
4103     (gnus-summary-first-unseen-subject))
4104    ((eq gnus-auto-select-subject 'unseen-or-unread)
4105     (gnus-summary-first-unseen-or-unread-subject))
4106    ((eq gnus-auto-select-subject 'first)
4107     ;; Do nothing.
4108     )
4109    ((functionp gnus-auto-select-subject)
4110     (funcall gnus-auto-select-subject))))
4111
4112 (defun gnus-summary-prepare ()
4113   "Generate the summary buffer."
4114   (interactive)
4115   (let ((inhibit-read-only t))
4116     (erase-buffer)
4117     (setq gnus-newsgroup-data nil
4118           gnus-newsgroup-data-reverse nil)
4119     (gnus-run-hooks 'gnus-summary-generate-hook)
4120     ;; Generate the buffer, either with threads or without.
4121     (when gnus-newsgroup-headers
4122       (gnus-summary-prepare-threads
4123        (if gnus-show-threads
4124            (gnus-sort-gathered-threads
4125             (funcall gnus-summary-thread-gathering-function
4126                      (gnus-sort-threads
4127                       (gnus-cut-threads (gnus-make-threads)))))
4128          ;; Unthreaded display.
4129          (gnus-sort-articles gnus-newsgroup-headers))))
4130     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4131     ;; Call hooks for modifying summary buffer.
4132     (goto-char (point-min))
4133     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4134
4135 (defsubst gnus-general-simplify-subject (subject)
4136   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4137   (setq subject
4138         (cond
4139          ;; Truncate the subject.
4140          (gnus-simplify-subject-functions
4141           (gnus-map-function gnus-simplify-subject-functions subject))
4142          ((numberp gnus-summary-gather-subject-limit)
4143           (setq subject (gnus-simplify-subject-re subject))
4144           (if (> (length subject) gnus-summary-gather-subject-limit)
4145               (substring subject 0 gnus-summary-gather-subject-limit)
4146             subject))
4147          ;; Fuzzily simplify it.
4148          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4149           (gnus-simplify-subject-fuzzy subject))
4150          ;; Just remove the leading "Re:".
4151          (t
4152           (gnus-simplify-subject-re subject))))
4153
4154   (if (and gnus-summary-gather-exclude-subject
4155            (string-match gnus-summary-gather-exclude-subject subject))
4156       nil                         ; This article shouldn't be gathered
4157     subject))
4158
4159 (defun gnus-summary-simplify-subject-query ()
4160   "Query where the respool algorithm would put this article."
4161   (interactive)
4162   (gnus-summary-select-article)
4163   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4164
4165 (defun gnus-gather-threads-by-subject (threads)
4166   "Gather threads by looking at Subject headers."
4167   (if (not gnus-summary-make-false-root)
4168       threads
4169     (let ((hashtb (gnus-make-hashtable 1024))
4170           (prev threads)
4171           (result threads)
4172           subject hthread whole-subject)
4173       (while threads
4174         (setq subject (gnus-general-simplify-subject
4175                        (setq whole-subject (mail-header-subject
4176                                             (caar threads)))))
4177         (when subject
4178           (if (setq hthread (gnus-gethash subject hashtb))
4179               (progn
4180                 ;; We enter a dummy root into the thread, if we
4181                 ;; haven't done that already.
4182                 (unless (stringp (caar hthread))
4183                   (setcar hthread (list whole-subject (car hthread))))
4184                 ;; We add this new gathered thread to this gathered
4185                 ;; thread.
4186                 (setcdr (car hthread)
4187                         (nconc (cdar hthread) (list (car threads))))
4188                 ;; Remove it from the list of threads.
4189                 (setcdr prev (cdr threads))
4190                 (setq threads prev))
4191             ;; Enter this thread into the hash table.
4192             (gnus-sethash subject
4193                           (if gnus-summary-make-false-root-always
4194                               (progn
4195                                 ;; If you want a dummy root above all
4196                                 ;; threads...
4197                                 (setcar threads (list whole-subject
4198                                                       (car threads)))
4199                                 threads)
4200                             threads)
4201                           hashtb)))
4202         (setq prev threads)
4203         (setq threads (cdr threads)))
4204       result)))
4205
4206 (defun gnus-gather-threads-by-references (threads)
4207   "Gather threads by looking at References headers."
4208   (let ((idhashtb (gnus-make-hashtable 1024))
4209         (thhashtb (gnus-make-hashtable 1024))
4210         (prev threads)
4211         (result threads)
4212         ids references id gthread gid entered ref)
4213     (while threads
4214       (when (setq references (mail-header-references (caar threads)))
4215         (setq id (mail-header-id (caar threads))
4216               ids (inline (gnus-split-references references))
4217               entered nil)
4218         (while (setq ref (pop ids))
4219           (setq ids (delete ref ids))
4220           (if (not (setq gid (gnus-gethash ref idhashtb)))
4221               (progn
4222                 (gnus-sethash ref id idhashtb)
4223                 (gnus-sethash id threads thhashtb))
4224             (setq gthread (gnus-gethash gid thhashtb))
4225             (unless entered
4226               ;; We enter a dummy root into the thread, if we
4227               ;; haven't done that already.
4228               (unless (stringp (caar gthread))
4229                 (setcar gthread (list (mail-header-subject (caar gthread))
4230                                       (car gthread))))
4231               ;; We add this new gathered thread to this gathered
4232               ;; thread.
4233               (setcdr (car gthread)
4234                       (nconc (cdar gthread) (list (car threads)))))
4235             ;; Add it into the thread hash table.
4236             (gnus-sethash id gthread thhashtb)
4237             (setq entered t)
4238             ;; Remove it from the list of threads.
4239             (setcdr prev (cdr threads))
4240             (setq threads prev))))
4241       (setq prev threads)
4242       (setq threads (cdr threads)))
4243     result))
4244
4245 (defun gnus-sort-gathered-threads (threads)
4246   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4247   (let ((result threads))
4248     (while threads
4249       (when (stringp (caar threads))
4250         (setcdr (car threads)
4251                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4252       (setq threads (cdr threads)))
4253     result))
4254
4255 (defun gnus-thread-loop-p (root thread)
4256   "Say whether ROOT is in THREAD."
4257   (let ((stack (list thread))
4258         (infloop 0)
4259         th)
4260     (while (setq thread (pop stack))
4261       (setq th (cdr thread))
4262       (while (and th
4263                   (not (eq (caar th) root)))
4264         (pop th))
4265       (if th
4266           ;; We have found a loop.
4267           (let (ref-dep)
4268             (setcdr thread (delq (car th) (cdr thread)))
4269             (if (boundp (setq ref-dep (intern "none"
4270                                               gnus-newsgroup-dependencies)))
4271                 (setcdr (symbol-value ref-dep)
4272                         (nconc (cdr (symbol-value ref-dep))
4273                                (list (car th))))
4274               (set ref-dep (list nil (car th))))
4275             (setq infloop 1
4276                   stack nil))
4277         ;; Push all the subthreads onto the stack.
4278         (push (cdr thread) stack)))
4279     infloop))
4280
4281 (defun gnus-make-threads ()
4282   "Go through the dependency hashtb and find the roots.  Return all threads."
4283   (let (threads)
4284     (while (catch 'infloop
4285              (mapatoms
4286               (lambda (refs)
4287                 ;; Deal with self-referencing References loops.
4288                 (when (and (car (symbol-value refs))
4289                            (not (zerop
4290                                  (apply
4291                                   '+
4292                                   (mapcar
4293                                    (lambda (thread)
4294                                      (gnus-thread-loop-p
4295                                       (car (symbol-value refs)) thread))
4296                                    (cdr (symbol-value refs)))))))
4297                   (setq threads nil)
4298                   (throw 'infloop t))
4299                 (unless (car (symbol-value refs))
4300                   ;; These threads do not refer back to any other
4301                   ;; articles, so they're roots.
4302                   (setq threads (append (cdr (symbol-value refs)) threads))))
4303               gnus-newsgroup-dependencies)))
4304     threads))
4305
4306 ;; Build the thread tree.
4307 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4308   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4309
4310 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4311 if it was already present.
4312
4313 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4314 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4315 Message-IDs will be renamed to a unique Message-ID before being
4316 entered.
4317
4318 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4319   (let* ((id (mail-header-id header))
4320          (id-dep (and id (intern id dependencies)))
4321          parent-id ref ref-dep ref-header replaced)
4322     ;; Enter this `header' in the `dependencies' table.
4323     (cond
4324      ((not id-dep)
4325       (setq header nil))
4326      ;; The first two cases do the normal part: enter a new `header'
4327      ;; in the `dependencies' table.
4328      ((not (boundp id-dep))
4329       (set id-dep (list header)))
4330      ((null (car (symbol-value id-dep)))
4331       (setcar (symbol-value id-dep) header))
4332
4333      ;; From here the `header' was already present in the
4334      ;; `dependencies' table.
4335      (force-new
4336       ;; Overrides an existing entry;
4337       ;; just set the header part of the entry.
4338       (setcar (symbol-value id-dep) header)
4339       (setq replaced t))
4340
4341      ;; Renames the existing `header' to a unique Message-ID.
4342      ((not gnus-summary-ignore-duplicates)
4343       ;; An article with this Message-ID has already been seen.
4344       ;; We rename the Message-ID.
4345       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4346            (list header))
4347       (mail-header-set-id header id))
4348
4349      ;; The last case ignores an existing entry, except it adds any
4350      ;; additional Xrefs (in case the two articles came from different
4351      ;; servers.
4352      ;; Also sets `header' to `nil' meaning that the `dependencies'
4353      ;; table was *not* modified.
4354      (t
4355       (mail-header-set-xref
4356        (car (symbol-value id-dep))
4357        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4358                    "")
4359                (or (mail-header-xref header) "")))
4360       (setq header nil)))
4361
4362     (when (and header (not replaced))
4363       ;; First check that we are not creating a References loop.
4364       (setq parent-id (gnus-parent-id (mail-header-references header)))
4365       (setq ref parent-id)
4366       (while (and ref
4367                   (setq ref-dep (intern-soft ref dependencies))
4368                   (boundp ref-dep)
4369                   (setq ref-header (car (symbol-value ref-dep))))
4370         (if (string= id ref)
4371             ;; Yuk!  This is a reference loop.  Make the article be a
4372             ;; root article.
4373             (progn
4374               (mail-header-set-references (car (symbol-value id-dep)) "none")
4375               (setq ref nil)
4376               (setq parent-id nil))
4377           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4378       (setq ref-dep (intern (or parent-id "none") dependencies))
4379       (if (boundp ref-dep)
4380           (setcdr (symbol-value ref-dep)
4381                   (nconc (cdr (symbol-value ref-dep))
4382                          (list (symbol-value id-dep))))
4383         (set ref-dep (list nil (symbol-value id-dep)))))
4384     header))
4385
4386 (defun gnus-extract-message-id-from-in-reply-to (string)
4387   (if (string-match "<[^>]+>" string)
4388       (substring string (match-beginning 0) (match-end 0))
4389     nil))
4390
4391 (defun gnus-build-sparse-threads ()
4392   (let ((headers gnus-newsgroup-headers)
4393         (mail-parse-charset gnus-newsgroup-charset)
4394         (gnus-summary-ignore-duplicates t)
4395         header references generation relations
4396         subject child end new-child date)
4397     ;; First we create an alist of generations/relations, where
4398     ;; generations is how much we trust the relation, and the relation
4399     ;; is parent/child.
4400     (gnus-message 7 "Making sparse threads...")
4401     (save-excursion
4402       (nnheader-set-temp-buffer " *gnus sparse threads*")
4403       (while (setq header (pop headers))
4404         (when (and (setq references (mail-header-references header))
4405                    (not (string= references "")))
4406           (insert references)
4407           (setq child (mail-header-id header)
4408                 subject (mail-header-subject header)
4409                 date (mail-header-date header)
4410                 generation 0)
4411           (while (search-backward ">" nil t)
4412             (setq end (1+ (point)))
4413             (when (search-backward "<" nil t)
4414               (setq new-child (buffer-substring (point) end))
4415               (push (list (incf generation)
4416                           child (setq child new-child)
4417                           subject date)
4418                     relations)))
4419           (when child
4420             (push (list (1+ generation) child nil subject) relations))
4421           (erase-buffer)))
4422       (kill-buffer (current-buffer)))
4423     ;; Sort over trustworthiness.
4424     (dolist (relation (sort relations 'car-less-than-car))
4425       (when (gnus-dependencies-add-header
4426              (make-full-mail-header
4427               gnus-reffed-article-number
4428               (nth 3 relation) "" (or (nth 4 relation) "")
4429               (nth 1 relation)
4430               (or (nth 2 relation) "") 0 0 "")
4431              gnus-newsgroup-dependencies nil)
4432         (push gnus-reffed-article-number gnus-newsgroup-limit)
4433         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4434         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4435               gnus-newsgroup-reads)
4436         (decf gnus-reffed-article-number)))
4437     (gnus-message 7 "Making sparse threads...done")))
4438
4439 (defun gnus-build-old-threads ()
4440   ;; Look at all the articles that refer back to old articles, and
4441   ;; fetch the headers for the articles that aren't there.  This will
4442   ;; build complete threads - if the roots haven't been expired by the
4443   ;; server, that is.
4444   (let ((mail-parse-charset gnus-newsgroup-charset)
4445         id heads)
4446     (mapatoms
4447      (lambda (refs)
4448        (when (not (car (symbol-value refs)))
4449          (setq heads (cdr (symbol-value refs)))
4450          (while heads
4451            (if (memq (mail-header-number (caar heads))
4452                      gnus-newsgroup-dormant)
4453                (setq heads (cdr heads))
4454              (setq id (symbol-name refs))
4455              (while (and (setq id (gnus-build-get-header id))
4456                          (not (car (gnus-id-to-thread id)))))
4457              (setq heads nil)))))
4458      gnus-newsgroup-dependencies)))
4459
4460 (defsubst gnus-remove-odd-characters (string)
4461   "Translate STRING into something that doesn't contain weird characters."
4462   (mm-subst-char-in-string
4463    ?\r ?\-
4464    (mm-subst-char-in-string ?\n ?\- string t) t))
4465
4466 ;; This function has to be called with point after the article number
4467 ;; on the beginning of the line.
4468 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4469   (let ((eol (point-at-eol))
4470         (buffer (current-buffer))
4471         header references in-reply-to)
4472
4473     ;; overview: [num subject from date id refs chars lines misc]
4474     (unwind-protect
4475         (let (x)
4476           (narrow-to-region (point) eol)
4477           (unless (eobp)
4478             (forward-char))
4479
4480           (setq header
4481                 (make-full-mail-header
4482                  number                 ; number
4483                  (condition-case ()     ; subject
4484                      (gnus-remove-odd-characters
4485                       (funcall gnus-decode-encoded-word-function
4486                                (setq x (nnheader-nov-field))))
4487                    (error x))
4488                  (condition-case ()     ; from
4489                      (gnus-remove-odd-characters
4490                       (funcall gnus-decode-encoded-address-function
4491                                (setq x (nnheader-nov-field))))
4492                    (error x))
4493                  (nnheader-nov-field)   ; date
4494                  (nnheader-nov-read-message-id number)  ; id
4495                  (setq references (nnheader-nov-field)) ; refs
4496                  (nnheader-nov-read-integer) ; chars
4497                  (nnheader-nov-read-integer) ; lines
4498                  (unless (eobp)
4499                    (if (looking-at "Xref: ")
4500                        (goto-char (match-end 0)))
4501                    (nnheader-nov-field)) ; Xref
4502                  (nnheader-nov-parse-extra)))) ; extra
4503
4504       (widen))
4505
4506     (when (and (string= references "")
4507                (setq in-reply-to (mail-header-extra header))
4508                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4509       (mail-header-set-references
4510        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4511
4512     (when gnus-alter-header-function
4513       (funcall gnus-alter-header-function header))
4514     (gnus-dependencies-add-header header dependencies force-new)))
4515
4516 (defun gnus-build-get-header (id)
4517   "Look through the buffer of NOV lines and find the header to ID.
4518 Enter this line into the dependencies hash table, and return
4519 the id of the parent article (if any)."
4520   (let ((deps gnus-newsgroup-dependencies)
4521         found header)
4522     (prog1
4523         (with-current-buffer nntp-server-buffer
4524           (let ((case-fold-search nil))
4525             (goto-char (point-min))
4526             (while (and (not found)
4527                         (search-forward id nil t))
4528               (beginning-of-line)
4529               (setq found (looking-at
4530                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4531                                    (regexp-quote id))))
4532               (or found (beginning-of-line 2)))
4533             (when found
4534               (beginning-of-line)
4535               (and
4536                (setq header (gnus-nov-parse-line
4537                              (read (current-buffer)) deps))
4538                (gnus-parent-id (mail-header-references header))))))
4539       (when header
4540         (let ((number (mail-header-number header)))
4541           (push number gnus-newsgroup-limit)
4542           (push header gnus-newsgroup-headers)
4543           (if (memq number gnus-newsgroup-unselected)
4544               (progn
4545                 (setq gnus-newsgroup-unreads
4546                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4547                                                number))
4548                 (setq gnus-newsgroup-unselected
4549                       (delq number gnus-newsgroup-unselected)))
4550             (push number gnus-newsgroup-ancient)))))))
4551
4552 (defun gnus-build-all-threads ()
4553   "Read all the headers."
4554   (let ((gnus-summary-ignore-duplicates t)
4555         (mail-parse-charset gnus-newsgroup-charset)
4556         (dependencies gnus-newsgroup-dependencies)
4557         header article)
4558     (with-current-buffer nntp-server-buffer
4559       (let ((case-fold-search nil))
4560         (goto-char (point-min))
4561         (while (not (eobp))
4562           (ignore-errors
4563             (setq article (read (current-buffer))
4564                   header (gnus-nov-parse-line article dependencies t)))
4565           (when header
4566             (with-current-buffer gnus-summary-buffer
4567               (push header gnus-newsgroup-headers)
4568               (if (memq (setq article (mail-header-number header))
4569                         gnus-newsgroup-unselected)
4570                   (progn
4571                     (setq gnus-newsgroup-unreads
4572                           (gnus-add-to-sorted-list
4573                            gnus-newsgroup-unreads article))
4574                     (setq gnus-newsgroup-unselected
4575                           (delq article gnus-newsgroup-unselected)))
4576                 (push article gnus-newsgroup-ancient)))
4577             (forward-line 1)))))))
4578
4579 (defun gnus-summary-update-article-line (article header)
4580   "Update the line for ARTICLE using HEADER."
4581   (let* ((id (mail-header-id header))
4582          (thread (gnus-id-to-thread id)))
4583     (unless thread
4584       (error "Article in no thread"))
4585     ;; Update the thread.
4586     (setcar thread header)
4587     (gnus-summary-goto-subject article)
4588     (let* ((datal (gnus-data-find-list article))
4589            (data (car datal))
4590            (inhibit-read-only t)
4591            (level (gnus-summary-thread-level)))
4592       (gnus-delete-line)
4593       (let ((inserted (- (point)
4594                          (progn
4595                            (gnus-summary-insert-line
4596                             header level nil
4597                             (memq article gnus-newsgroup-undownloaded)
4598                             (gnus-article-mark article)
4599                             (memq article gnus-newsgroup-replied)
4600                             (memq article gnus-newsgroup-expirable)
4601                             ;; Only insert the Subject string when it's different
4602                             ;; from the previous Subject string.
4603                             (if (and
4604                                  gnus-show-threads
4605                                  (gnus-subject-equal
4606                                   (condition-case ()
4607                                       (mail-header-subject
4608                                        (gnus-data-header
4609                                         (cadr
4610                                          (gnus-data-find-list
4611                                           article
4612                                           (gnus-data-list t)))))
4613                                     ;; Error on the side of excessive subjects.
4614                                     (error ""))
4615                                   (mail-header-subject header)))
4616                                 ""
4617                               (mail-header-subject header))
4618                             nil (cdr (assq article gnus-newsgroup-scored))
4619                             (memq article gnus-newsgroup-processable))
4620                            (point)))))
4621         (when (cdr datal)
4622           (gnus-data-update-list
4623            (cdr datal)
4624            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4625
4626 (defun gnus-summary-update-article (article &optional iheader)
4627   "Update ARTICLE in the summary buffer."
4628   (set-buffer gnus-summary-buffer)
4629   (let* ((header (gnus-summary-article-header article))
4630          (id (mail-header-id header))
4631          (data (gnus-data-find article))
4632          (thread (gnus-id-to-thread id))
4633          (references (mail-header-references header))
4634          (parent
4635           (gnus-id-to-thread
4636            (or (gnus-parent-id
4637                 (when (and references
4638                            (not (equal "" references)))
4639                   references))
4640                "none")))
4641          (inhibit-read-only t)
4642          (old (car thread)))
4643     (when thread
4644       (unless iheader
4645         (setcar thread nil)
4646         (when parent
4647           (delq thread parent)))
4648       (if (gnus-summary-insert-subject id header)
4649           ;; Set the (possibly) new article number in the data structure.
4650           (gnus-data-set-number data (gnus-id-to-article id))
4651         (setcar thread old)
4652         nil))))
4653
4654 (defun gnus-rebuild-thread (id &optional line)
4655   "Rebuild the thread containing ID.
4656 If LINE, insert the rebuilt thread starting on line LINE."
4657   (let ((inhibit-read-only t)
4658         old-pos current thread data)
4659     (if (not gnus-show-threads)
4660         (setq thread (list (car (gnus-id-to-thread id))))
4661       ;; Get the thread this article is part of.
4662       (setq thread (gnus-remove-thread id)))
4663     (setq old-pos (point-at-bol))
4664     (setq current (save-excursion
4665                     (and (re-search-backward "[\r\n]" nil t)
4666                          (gnus-summary-article-number))))
4667     ;; If this is a gathered thread, we have to go some re-gathering.
4668     (when (stringp (car thread))
4669       (let ((subject (car thread))
4670             roots thr)
4671         (setq thread (cdr thread))
4672         (while thread
4673           (unless (memq (setq thr (gnus-id-to-thread
4674                                    (gnus-root-id
4675                                     (mail-header-id (caar thread)))))
4676                         roots)
4677             (push thr roots))
4678           (setq thread (cdr thread)))
4679         ;; We now have all (unique) roots.
4680         (if (= (length roots) 1)
4681             ;; All the loose roots are now one solid root.
4682             (setq thread (car roots))
4683           (setq thread (cons subject (gnus-sort-threads roots))))))
4684     (let (threads)
4685       ;; We then insert this thread into the summary buffer.
4686       (when line
4687         (goto-char (point-min))
4688         (forward-line (1- line)))
4689       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4690         (if gnus-show-threads
4691             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4692           (gnus-summary-prepare-unthreaded thread))
4693         (setq data (nreverse gnus-newsgroup-data))
4694         (setq threads gnus-newsgroup-threads))
4695       ;; We splice the new data into the data structure.
4696       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4697       ;;!!! then we want to insert at the beginning of the buffer.
4698       ;;!!! That happens to be true with Gnus now, but that may
4699       ;;!!! change in the future.  Perhaps.
4700       (gnus-data-enter-list
4701        (if line nil current) data (- (point) old-pos))
4702       (setq gnus-newsgroup-threads
4703             (nconc threads gnus-newsgroup-threads))
4704       (gnus-data-compute-positions))))
4705
4706 (defun gnus-number-to-header (number)
4707   "Return the header for article NUMBER."
4708   (let ((headers gnus-newsgroup-headers))
4709     (while (and headers
4710                 (not (= number (mail-header-number (car headers)))))
4711       (pop headers))
4712     (when headers
4713       (car headers))))
4714
4715 (defun gnus-parent-headers (in-headers &optional generation)
4716   "Return the headers of the GENERATIONth parent of HEADERS."
4717   (unless generation
4718     (setq generation 1))
4719   (let ((parent t)
4720         (headers in-headers)
4721         references)
4722     (while (and parent
4723                 (not (zerop generation))
4724                 (setq references (mail-header-references headers)))
4725       (setq headers (if (and references
4726                              (setq parent (gnus-parent-id references)))
4727                         (car (gnus-id-to-thread parent))
4728                       nil))
4729       (decf generation))
4730     (and (not (eq headers in-headers))
4731          headers)))
4732
4733 (defun gnus-id-to-thread (id)
4734   "Return the (sub-)thread where ID appears."
4735   (gnus-gethash id gnus-newsgroup-dependencies))
4736
4737 (defun gnus-id-to-article (id)
4738   "Return the article number of ID."
4739   (let ((thread (gnus-id-to-thread id)))
4740     (when (and thread
4741                (car thread))
4742       (mail-header-number (car thread)))))
4743
4744 (defun gnus-id-to-header (id)
4745   "Return the article headers of ID."
4746   (car (gnus-id-to-thread id)))
4747
4748 (defun gnus-article-displayed-root-p (article)
4749   "Say whether ARTICLE is a root(ish) article."
4750   (let ((level (gnus-summary-thread-level article))
4751         (refs (mail-header-references  (gnus-summary-article-header article)))
4752         particle)
4753     (cond
4754      ((null level) nil)
4755      ((zerop level) t)
4756      ((null refs) t)
4757      ((null (gnus-parent-id refs)) t)
4758      ((and (= 1 level)
4759            (null (setq particle (gnus-id-to-article
4760                                  (gnus-parent-id refs))))
4761            (null (gnus-summary-thread-level particle)))))))
4762
4763 (defun gnus-root-id (id)
4764   "Return the id of the root of the thread where ID appears."
4765   (let (last-id prev)
4766     (while (and id (setq prev (car (gnus-id-to-thread id))))
4767       (setq last-id id
4768             id (gnus-parent-id (mail-header-references prev))))
4769     last-id))
4770
4771 (defun gnus-articles-in-thread (thread)
4772   "Return the list of articles in THREAD."
4773   (cons (mail-header-number (car thread))
4774         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4775
4776 (defun gnus-remove-thread (id &optional dont-remove)
4777   "Remove the thread that has ID in it."
4778   (let (headers thread last-id)
4779     ;; First go up in this thread until we find the root.
4780     (setq last-id (gnus-root-id id)
4781           headers (message-flatten-list (gnus-id-to-thread last-id)))
4782     ;; We have now found the real root of this thread.  It might have
4783     ;; been gathered into some loose thread, so we have to search
4784     ;; through the threads to find the thread we wanted.
4785     (let ((threads gnus-newsgroup-threads)
4786           sub)
4787       (while threads
4788         (setq sub (car threads))
4789         (if (stringp (car sub))
4790             ;; This is a gathered thread, so we look at the roots
4791             ;; below it to find whether this article is in this
4792             ;; gathered root.
4793             (progn
4794               (setq sub (cdr sub))
4795               (while sub
4796                 (when (member (caar sub) headers)
4797                   (setq thread (car threads)
4798                         threads nil
4799                         sub nil))
4800                 (setq sub (cdr sub))))
4801           ;; It's an ordinary thread, so we check it.
4802           (when (eq (car sub) (car headers))
4803             (setq thread sub
4804                   threads nil)))
4805         (setq threads (cdr threads)))
4806       ;; If this article is in no thread, then it's a root.
4807       (if thread
4808           (unless dont-remove
4809             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4810         (setq thread (gnus-id-to-thread last-id)))
4811       (when thread
4812         (prog1
4813             thread                      ; We return this thread.
4814           (unless dont-remove
4815             (if (stringp (car thread))
4816                 (progn
4817                   ;; If we use dummy roots, then we have to remove the
4818                   ;; dummy root as well.
4819                   (when (eq gnus-summary-make-false-root 'dummy)
4820                     ;; We go to the dummy root by going to
4821                     ;; the first sub-"thread", and then one line up.
4822                     (gnus-summary-goto-article
4823                      (mail-header-number (caadr thread)))
4824                     (forward-line -1)
4825                     (gnus-delete-line)
4826                     (gnus-data-compute-positions))
4827                   (setq thread (cdr thread))
4828                   (while thread
4829                     (gnus-remove-thread-1 (car thread))
4830                     (setq thread (cdr thread))))
4831               (gnus-remove-thread-1 thread))))))))
4832
4833 (defun gnus-remove-thread-1 (thread)
4834   "Remove the thread THREAD recursively."
4835   (let ((number (mail-header-number (pop thread)))
4836         d)
4837     (setq thread (reverse thread))
4838     (while thread
4839       (gnus-remove-thread-1 (pop thread)))
4840     (when (setq d (gnus-data-find number))
4841       (goto-char (gnus-data-pos d))
4842       (gnus-summary-show-thread)
4843       (gnus-data-remove
4844        number
4845        (- (point-at-bol)
4846           (prog1
4847               (1+ (point-at-eol))
4848             (gnus-delete-line)))))))
4849
4850 (defun gnus-sort-threads-recursive (threads func)
4851   (sort (mapcar (lambda (thread)
4852                   (cons (car thread)
4853                         (and (cdr thread)
4854                              (gnus-sort-threads-recursive (cdr thread) func))))
4855                 threads) func))
4856
4857 (defun gnus-sort-threads-loop (threads func)
4858   (let* ((superthread (cons nil threads))
4859          (stack (list (cons superthread threads)))
4860          remaining-threads thread)
4861     (while stack
4862       (setq remaining-threads (cdr (car stack)))
4863       (if remaining-threads
4864           (progn (setq thread (car remaining-threads))
4865                  (setcdr (car stack) (cdr remaining-threads))
4866                  (if (cdr thread)
4867                      (push (cons thread (cdr thread)) stack)))
4868         (setq thread (caar stack))
4869         (setcdr thread (sort (cdr thread) func))
4870         (pop stack)))
4871     (cdr superthread)))
4872
4873 (defun gnus-sort-threads (threads)
4874   "Sort THREADS."
4875   (if (not gnus-thread-sort-functions)
4876       threads
4877     (gnus-message 8 "Sorting threads...")
4878     (prog1
4879         (condition-case nil
4880             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4881               (gnus-sort-threads-recursive
4882                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4883           ;; Even after binding max-lisp-eval-depth, the recursive
4884           ;; sorter might fail for very long threads.  In that case,
4885           ;; try using a (less well-tested) non-recursive sorter.
4886           (error (gnus-message 9 "Sorting threads with loop...")
4887                  (gnus-sort-threads-loop
4888                   threads (gnus-make-sort-function
4889                            gnus-thread-sort-functions))))
4890       (gnus-message 8 "Sorting threads...done"))))
4891
4892 (defun gnus-sort-articles (articles)
4893   "Sort ARTICLES."
4894   (when gnus-article-sort-functions
4895     (gnus-message 7 "Sorting articles...")
4896     (prog1
4897         (setq gnus-newsgroup-headers
4898               (sort articles (gnus-make-sort-function
4899                               gnus-article-sort-functions)))
4900       (gnus-message 7 "Sorting articles...done"))))
4901
4902 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4903 (defmacro gnus-thread-header (thread)
4904   "Return header of first article in THREAD.
4905 Note that THREAD must never, ever be anything else than a variable -
4906 using some other form will lead to serious barfage."
4907   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4908   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4909   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4910         (vector thread) 2))
4911
4912 (defsubst gnus-article-sort-by-number (h1 h2)
4913   "Sort articles by article number."
4914   (< (mail-header-number h1)
4915      (mail-header-number h2)))
4916
4917 (defun gnus-thread-sort-by-number (h1 h2)
4918   "Sort threads by root article number."
4919   (gnus-article-sort-by-number
4920    (gnus-thread-header h1) (gnus-thread-header h2)))
4921
4922 (defsubst gnus-article-sort-by-random (h1 h2)
4923   "Sort articles randomly."
4924   (zerop (random 2)))
4925
4926 (defun gnus-thread-sort-by-random (h1 h2)
4927   "Sort threads randomly."
4928   (gnus-article-sort-by-random
4929    (gnus-thread-header h1) (gnus-thread-header h2)))
4930
4931 (defsubst gnus-article-sort-by-lines (h1 h2)
4932   "Sort articles by article Lines header."
4933   (< (mail-header-lines h1)
4934      (mail-header-lines h2)))
4935
4936 (defun gnus-thread-sort-by-lines (h1 h2)
4937   "Sort threads by root article Lines header."
4938   (gnus-article-sort-by-lines
4939    (gnus-thread-header h1) (gnus-thread-header h2)))
4940
4941 (defsubst gnus-article-sort-by-chars (h1 h2)
4942   "Sort articles by octet length."
4943   (< (mail-header-chars h1)
4944      (mail-header-chars h2)))
4945
4946 (defun gnus-thread-sort-by-chars (h1 h2)
4947   "Sort threads by root article octet length."
4948   (gnus-article-sort-by-chars
4949    (gnus-thread-header h1) (gnus-thread-header h2)))
4950
4951 (defsubst gnus-article-sort-by-author (h1 h2)
4952   "Sort articles by root author."
4953   (gnus-string<
4954    (let ((extract (funcall
4955                    gnus-extract-address-components
4956                    (mail-header-from h1))))
4957      (or (car extract) (cadr extract) ""))
4958    (let ((extract (funcall
4959                    gnus-extract-address-components
4960                    (mail-header-from h2))))
4961      (or (car extract) (cadr extract) ""))))
4962
4963 (defun gnus-thread-sort-by-author (h1 h2)
4964   "Sort threads by root author."
4965   (gnus-article-sort-by-author
4966    (gnus-thread-header h1)  (gnus-thread-header h2)))
4967
4968 (defsubst gnus-article-sort-by-recipient (h1 h2)
4969   "Sort articles by recipient."
4970   (gnus-string<
4971    (let ((extract (funcall
4972                    gnus-extract-address-components
4973                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4974      (or (car extract) (cadr extract)))
4975    (let ((extract (funcall
4976                    gnus-extract-address-components
4977                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4978      (or (car extract) (cadr extract)))))
4979
4980 (defun gnus-thread-sort-by-recipient (h1 h2)
4981   "Sort threads by root recipient."
4982   (gnus-article-sort-by-recipient
4983    (gnus-thread-header h1) (gnus-thread-header h2)))
4984
4985 (defsubst gnus-article-sort-by-subject (h1 h2)
4986   "Sort articles by root subject."
4987   (gnus-string<
4988    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4989    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4990
4991 (defun gnus-thread-sort-by-subject (h1 h2)
4992   "Sort threads by root subject."
4993   (gnus-article-sort-by-subject
4994    (gnus-thread-header h1) (gnus-thread-header h2)))
4995
4996 (defsubst gnus-article-sort-by-date (h1 h2)
4997   "Sort articles by root article date."
4998   (time-less-p
4999    (gnus-date-get-time (mail-header-date h1))
5000    (gnus-date-get-time (mail-header-date h2))))
5001
5002 (defun gnus-thread-sort-by-date (h1 h2)
5003   "Sort threads by root article date."
5004   (gnus-article-sort-by-date
5005    (gnus-thread-header h1) (gnus-thread-header h2)))
5006
5007 (defsubst gnus-article-sort-by-score (h1 h2)
5008   "Sort articles by root article score.
5009 Unscored articles will be counted as having a score of zero."
5010   (> (or (cdr (assq (mail-header-number h1)
5011                     gnus-newsgroup-scored))
5012          gnus-summary-default-score 0)
5013      (or (cdr (assq (mail-header-number h2)
5014                     gnus-newsgroup-scored))
5015          gnus-summary-default-score 0)))
5016
5017 (defun gnus-thread-sort-by-score (h1 h2)
5018   "Sort threads by root article score."
5019   (gnus-article-sort-by-score
5020    (gnus-thread-header h1) (gnus-thread-header h2)))
5021
5022 (defun gnus-thread-sort-by-total-score (h1 h2)
5023   "Sort threads by the sum of all scores in the thread.
5024 Unscored articles will be counted as having a score of zero."
5025   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5026
5027 (defun gnus-thread-total-score (thread)
5028   ;; This function find the total score of THREAD.
5029   (cond
5030    ((null thread)
5031     0)
5032    ((consp thread)
5033     (if (stringp (car thread))
5034         (apply gnus-thread-score-function 0
5035                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5036       (gnus-thread-total-score-1 thread)))
5037    (t
5038     (gnus-thread-total-score-1 (list thread)))))
5039
5040 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5041   "Sort articles by number."
5042   (gnus-article-sort-by-number h1 h2))
5043
5044 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5045   "Sort threads such that the thread with the most recently arrived article comes first."
5046   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5047
5048 (defun gnus-thread-highest-number (thread)
5049   "Return the highest article number in THREAD."
5050   (apply 'max (mapcar (lambda (header)
5051                         (mail-header-number header))
5052                       (message-flatten-list thread))))
5053
5054 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5055   "Sort articles by number."
5056   (gnus-article-sort-by-date h1 h2))
5057
5058 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5059   "Sort threads such that the thread with the most recently dated article comes first."
5060   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5061
5062 ; Since this is called not only to sort the top-level threads, but
5063 ; also in recursive sorts to order the articles within a thread, each
5064 ; article will be processed many times.  Thus it speeds things up
5065 ; quite a bit to use gnus-date-get-time, which caches the time value.
5066 (defun gnus-thread-latest-date (thread)
5067   "Return the highest article date in THREAD."
5068   (apply 'max
5069          (mapcar (lambda (header) (gnus-float-time
5070                                    (gnus-date-get-time
5071                                     (mail-header-date header))))
5072                  (message-flatten-list thread))))
5073
5074 (defun gnus-thread-total-score-1 (root)
5075   ;; This function find the total score of the thread below ROOT.
5076   (setq root (car root))
5077   (apply gnus-thread-score-function
5078          (or (append
5079               (mapcar 'gnus-thread-total-score
5080                       (cdr (gnus-id-to-thread (mail-header-id root))))
5081               (when (> (mail-header-number root) 0)
5082                 (list (or (cdr (assq (mail-header-number root)
5083                                      gnus-newsgroup-scored))
5084                           gnus-summary-default-score 0))))
5085              (list gnus-summary-default-score)
5086              '(0))))
5087
5088 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5089 (defvar gnus-tmp-prev-subject nil)
5090 (defvar gnus-tmp-false-parent nil)
5091 (defvar gnus-tmp-root-expunged nil)
5092 (defvar gnus-tmp-dummy-line nil)
5093
5094 (defun gnus-extra-header (type &optional header)
5095   "Return the extra header of TYPE."
5096   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5097       ""))
5098
5099 (defvar gnus-tmp-thread-tree-header-string "")
5100
5101 (defcustom gnus-sum-thread-tree-root "> "
5102   "With %B spec, used for the root of a thread.
5103 If nil, use subject instead."
5104   :version "22.1"
5105   :type '(radio (const :format "%v  " nil) string)
5106   :group 'gnus-thread)
5107
5108 (defcustom gnus-sum-thread-tree-false-root "> "
5109   "With %B spec, used for a false root of a thread.
5110 If nil, use subject instead."
5111   :version "22.1"
5112   :type '(radio (const :format "%v  " nil) string)
5113   :group 'gnus-thread)
5114
5115 (defcustom gnus-sum-thread-tree-single-indent ""
5116   "With %B spec, used for a thread with just one message.
5117 If nil, use subject instead."
5118   :version "22.1"
5119   :type '(radio (const :format "%v  " nil) string)
5120   :group 'gnus-thread)
5121
5122 (defcustom gnus-sum-thread-tree-vertical "| "
5123   "With %B spec, used for drawing a vertical line."
5124   :version "22.1"
5125   :type 'string
5126   :group 'gnus-thread)
5127
5128 (defcustom gnus-sum-thread-tree-indent "  "
5129   "With %B spec, used for indenting."
5130   :version "22.1"
5131   :type 'string
5132   :group 'gnus-thread)
5133
5134 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5135   "With %B spec, used for a leaf with brothers."
5136   :version "22.1"
5137   :type 'string
5138   :group 'gnus-thread)
5139
5140 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5141   "With %B spec, used for a leaf without brothers."
5142   :version "22.1"
5143   :type 'string
5144   :group 'gnus-thread)
5145
5146 (defcustom gnus-summary-display-while-building nil
5147   "If non-nil, show and update the summary buffer as it's being built.
5148 If the value is t, update the buffer after every line is inserted.  If
5149 the value is an integer (N), update the display every N lines."
5150   :version "22.1"
5151   :group 'gnus-thread
5152   :type '(choice (const :tag "off" nil)
5153                  number
5154                  (const :tag "frequently" t)))
5155
5156 (defun gnus-summary-prepare-threads (threads)
5157   "Prepare summary buffer from THREADS and indentation LEVEL.
5158 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5159 or a straight list of headers."
5160   (gnus-message 7 "Generating summary...")
5161
5162   (setq gnus-newsgroup-threads threads)
5163   (beginning-of-line)
5164
5165   (let ((gnus-tmp-level 0)
5166         (default-score (or gnus-summary-default-score 0))
5167         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5168         (building-line-count gnus-summary-display-while-building)
5169         (building-count (integerp gnus-summary-display-while-building))
5170         thread number subject stack state gnus-tmp-gathered beg-match
5171         new-roots gnus-tmp-new-adopts thread-end simp-subject
5172         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5173         gnus-tmp-replied gnus-tmp-subject-or-nil
5174         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5175         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5176         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5177         tree-stack)
5178
5179     (setq gnus-tmp-prev-subject nil
5180           gnus-tmp-thread-tree-header-string "")
5181
5182     (if (vectorp (car threads))
5183         ;; If this is a straight (sic) list of headers, then a
5184         ;; threaded summary display isn't required, so we just create
5185         ;; an unthreaded one.
5186         (gnus-summary-prepare-unthreaded threads)
5187
5188       ;; Do the threaded display.
5189
5190       (if gnus-summary-display-while-building
5191           (switch-to-buffer (buffer-name)))
5192       (while (or threads stack gnus-tmp-new-adopts new-roots)
5193
5194         (if (and (= gnus-tmp-level 0)
5195                  (or (not stack)
5196                      (= (caar stack) 0))
5197                  (not gnus-tmp-false-parent)
5198                  (or gnus-tmp-new-adopts new-roots))
5199             (if gnus-tmp-new-adopts
5200                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5201                       thread (list (car gnus-tmp-new-adopts))
5202                       gnus-tmp-header (caar thread)
5203                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5204               (when new-roots
5205                 (setq thread (list (car new-roots))
5206                       gnus-tmp-header (caar thread)
5207                       new-roots (cdr new-roots))))
5208
5209           (if threads
5210               ;; If there are some threads, we do them before the
5211               ;; threads on the stack.
5212               (setq thread threads
5213                     gnus-tmp-header (caar thread))
5214             ;; There were no current threads, so we pop something off
5215             ;; the stack.
5216             (setq state (car stack)
5217                   gnus-tmp-level (car state)
5218                   tree-stack (cadr state)
5219                   thread (caddr state)
5220                   stack (cdr stack)
5221                   gnus-tmp-header (caar thread))))
5222
5223         (setq gnus-tmp-false-parent nil)
5224         (setq gnus-tmp-root-expunged nil)
5225         (setq thread-end nil)
5226
5227         (if (stringp gnus-tmp-header)
5228             ;; The header is a dummy root.
5229             (cond
5230              ((eq gnus-summary-make-false-root 'adopt)
5231               ;; We let the first article adopt the rest.
5232               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5233                                                (cddar thread)))
5234               (setq gnus-tmp-gathered
5235                     (nconc (mapcar
5236                             (lambda (h) (mail-header-number (car h)))
5237                             (cddar thread))
5238                            gnus-tmp-gathered))
5239               (setq thread (cons (list (caar thread)
5240                                        (cadar thread))
5241                                  (cdr thread)))
5242               (setq gnus-tmp-level -1
5243                     gnus-tmp-false-parent t))
5244              ((eq gnus-summary-make-false-root 'empty)
5245               ;; We print adopted articles with empty subject fields.
5246               (setq gnus-tmp-gathered
5247                     (nconc (mapcar
5248                             (lambda (h) (mail-header-number (car h)))
5249                             (cddar thread))
5250                            gnus-tmp-gathered))
5251               (setq gnus-tmp-level -1))
5252              ((eq gnus-summary-make-false-root 'dummy)
5253               ;; We remember that we probably want to output a dummy
5254               ;; root.
5255               (setq gnus-tmp-dummy-line gnus-tmp-header)
5256               (setq gnus-tmp-prev-subject gnus-tmp-header))
5257              (t
5258               ;; We do not make a root for the gathered
5259               ;; sub-threads at all.
5260               (setq gnus-tmp-level -1)))
5261
5262           (setq number (mail-header-number gnus-tmp-header)
5263                 subject (mail-header-subject gnus-tmp-header)
5264                 simp-subject (gnus-simplify-subject-fully subject))
5265
5266           (cond
5267            ;; If the thread has changed subject, we might want to make
5268            ;; this subthread into a root.
5269            ((and (null gnus-thread-ignore-subject)
5270                  (not (zerop gnus-tmp-level))
5271                  gnus-tmp-prev-subject
5272                  (not (string= gnus-tmp-prev-subject simp-subject)))
5273             (setq new-roots (nconc new-roots (list (car thread)))
5274                   thread-end t
5275                   gnus-tmp-header nil))
5276            ;; If the article lies outside the current limit,
5277            ;; then we do not display it.
5278            ((not (memq number gnus-newsgroup-limit))
5279             (setq gnus-tmp-gathered
5280                   (nconc (mapcar
5281                           (lambda (h) (mail-header-number (car h)))
5282                           (cdar thread))
5283                          gnus-tmp-gathered))
5284             (setq gnus-tmp-new-adopts (if (cdar thread)
5285                                           (append gnus-tmp-new-adopts
5286                                                   (cdar thread))
5287                                         gnus-tmp-new-adopts)
5288                   thread-end t
5289                   gnus-tmp-header nil)
5290             (when (zerop gnus-tmp-level)
5291               (setq gnus-tmp-root-expunged t)))
5292            ;; Perhaps this article is to be marked as read?
5293            ((and gnus-summary-mark-below
5294                  (< (or (cdr (assq number gnus-newsgroup-scored))
5295                         default-score)
5296                     gnus-summary-mark-below)
5297                  ;; Don't touch sparse articles.
5298                  (not (gnus-summary-article-sparse-p number))
5299                  (not (gnus-summary-article-ancient-p number)))
5300             (setq gnus-newsgroup-unreads
5301                   (delq number gnus-newsgroup-unreads))
5302             (if gnus-newsgroup-auto-expire
5303                 (setq gnus-newsgroup-expirable
5304                       (gnus-add-to-sorted-list
5305                        gnus-newsgroup-expirable number))
5306               (push (cons number gnus-low-score-mark)
5307                     gnus-newsgroup-reads))))
5308
5309           (when gnus-tmp-header
5310             ;; We may have an old dummy line to output before this
5311             ;; article.
5312             (when (and gnus-tmp-dummy-line
5313                        (gnus-subject-equal
5314                         gnus-tmp-dummy-line
5315                         (mail-header-subject gnus-tmp-header)))
5316               (gnus-summary-insert-dummy-line
5317                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5318               (setq gnus-tmp-dummy-line nil))
5319
5320             ;; Compute the mark.
5321             (setq gnus-tmp-unread (gnus-article-mark number))
5322
5323             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5324                                   gnus-tmp-header gnus-tmp-level)
5325                   gnus-newsgroup-data)
5326
5327             ;; Actually insert the line.
5328             (setq
5329              gnus-tmp-subject-or-nil
5330              (cond
5331               ((and gnus-thread-ignore-subject
5332                     gnus-tmp-prev-subject
5333                     (not (string= gnus-tmp-prev-subject simp-subject)))
5334                subject)
5335               ((zerop gnus-tmp-level)
5336                (if (and (eq gnus-summary-make-false-root 'empty)
5337                         (memq number gnus-tmp-gathered)
5338                         gnus-tmp-prev-subject
5339                         (string= gnus-tmp-prev-subject simp-subject))
5340                    gnus-summary-same-subject
5341                  subject))
5342               (t gnus-summary-same-subject)))
5343             (if (and (eq gnus-summary-make-false-root 'adopt)
5344                      (= gnus-tmp-level 1)
5345                      (memq number gnus-tmp-gathered))
5346                 (setq gnus-tmp-opening-bracket ?\<
5347                       gnus-tmp-closing-bracket ?\>)
5348               (setq gnus-tmp-opening-bracket ?\[
5349                     gnus-tmp-closing-bracket ?\]))
5350             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5351                 (gnus-make-thread-indent-array
5352                  (max (* 2 (length gnus-thread-indent-array))
5353                       gnus-tmp-level)))
5354             (setq
5355              gnus-tmp-indentation
5356              (aref gnus-thread-indent-array gnus-tmp-level)
5357              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5358              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5359                                 gnus-summary-default-score 0)
5360              gnus-tmp-score-char
5361              (if (or (null gnus-summary-default-score)
5362                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5363                          gnus-summary-zcore-fuzz))
5364                  ?                      ;Whitespace
5365                (if (< gnus-tmp-score gnus-summary-default-score)
5366                    gnus-score-below-mark gnus-score-over-mark))
5367              gnus-tmp-replied
5368              (cond ((memq number gnus-newsgroup-processable)
5369                     gnus-process-mark)
5370                    ((memq number gnus-newsgroup-cached)
5371                     gnus-cached-mark)
5372                    ((memq number gnus-newsgroup-replied)
5373                     gnus-replied-mark)
5374                    ((memq number gnus-newsgroup-forwarded)
5375                     gnus-forwarded-mark)
5376                    ((memq number gnus-newsgroup-saved)
5377                     gnus-saved-mark)
5378                    ((memq number gnus-newsgroup-unseen)
5379                     gnus-unseen-mark)
5380                    (t gnus-no-mark))
5381              gnus-tmp-downloaded
5382              (cond ((memq number gnus-newsgroup-undownloaded)
5383                     gnus-undownloaded-mark)
5384                    (gnus-newsgroup-agentized
5385                     gnus-downloaded-mark)
5386                    (t
5387                     gnus-no-mark))
5388              gnus-tmp-from (mail-header-from gnus-tmp-header)
5389              gnus-tmp-name
5390              (cond
5391               ((string-match "<[^>]+> *$" gnus-tmp-from)
5392                (setq beg-match (match-beginning 0))
5393                (or (and (string-match "^\".+\"" gnus-tmp-from)
5394                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5395                    (substring gnus-tmp-from 0 beg-match)))
5396               ((string-match "(.+)" gnus-tmp-from)
5397                (substring gnus-tmp-from
5398                           (1+ (match-beginning 0)) (1- (match-end 0))))
5399               (t gnus-tmp-from))
5400
5401              ;; Do the %B string
5402              gnus-tmp-thread-tree-header-string
5403              (cond
5404               ((not gnus-show-threads) "")
5405               ((zerop gnus-tmp-level)
5406                (cond ((cdar thread)
5407                       (or gnus-sum-thread-tree-root subject))
5408                      (gnus-tmp-new-adopts
5409                       (or gnus-sum-thread-tree-false-root subject))
5410                      (t
5411                       (or gnus-sum-thread-tree-single-indent subject))))
5412               (t
5413                (concat (apply 'concat
5414                               (mapcar (lambda (item)
5415                                         (if (= item 1)
5416                                             gnus-sum-thread-tree-vertical
5417                                           gnus-sum-thread-tree-indent))
5418                                       (cdr (reverse tree-stack))))
5419                        (if (nth 1 thread)
5420                            gnus-sum-thread-tree-leaf-with-other
5421                          gnus-sum-thread-tree-single-leaf)))))
5422             (when (string= gnus-tmp-name "")
5423               (setq gnus-tmp-name gnus-tmp-from))
5424             (unless (numberp gnus-tmp-lines)
5425               (setq gnus-tmp-lines -1))
5426             (if (= gnus-tmp-lines -1)
5427                 (setq gnus-tmp-lines "?")
5428               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5429             (gnus-put-text-property
5430              (point)
5431              (progn (eval gnus-summary-line-format-spec) (point))
5432              'gnus-number number)
5433             (when gnus-visual-p
5434               (forward-line -1)
5435               (gnus-summary-highlight-line)
5436               (when gnus-summary-update-hook
5437                 (gnus-run-hooks 'gnus-summary-update-hook))
5438               (forward-line 1))
5439
5440             (setq gnus-tmp-prev-subject simp-subject)))
5441
5442         (when (nth 1 thread)
5443           (push (list (max 0 gnus-tmp-level)
5444                       (copy-sequence tree-stack)
5445                       (nthcdr 1 thread))
5446                 stack))
5447         (push (if (nth 1 thread) 1 0) tree-stack)
5448         (incf gnus-tmp-level)
5449         (setq threads (if thread-end nil (cdar thread)))
5450         (if gnus-summary-display-while-building
5451             (if building-count
5452                 (progn
5453                   ;; use a set frequency
5454                   (setq building-line-count (1- building-line-count))
5455                   (when (= building-line-count 0)
5456                     (sit-for 0)
5457                     (setq building-line-count
5458                           gnus-summary-display-while-building)))
5459               ;; always
5460               (sit-for 0)))
5461         (unless threads
5462           (setq gnus-tmp-level 0)))))
5463   (gnus-message 7 "Generating summary...done"))
5464
5465 (defun gnus-summary-prepare-unthreaded (headers)
5466   "Generate an unthreaded summary buffer based on HEADERS."
5467   (let (header number mark)
5468
5469     (beginning-of-line)
5470
5471     (while headers
5472       ;; We may have to root out some bad articles...
5473       (when (memq (setq number (mail-header-number
5474                                 (setq header (pop headers))))
5475                   gnus-newsgroup-limit)
5476         ;; Mark article as read when it has a low score.
5477         (when (and gnus-summary-mark-below
5478                    (< (or (cdr (assq number gnus-newsgroup-scored))
5479                           gnus-summary-default-score 0)
5480                       gnus-summary-mark-below)
5481                    (not (gnus-summary-article-ancient-p number)))
5482           (setq gnus-newsgroup-unreads
5483                 (delq number gnus-newsgroup-unreads))
5484           (if gnus-newsgroup-auto-expire
5485               (push number gnus-newsgroup-expirable)
5486             (push (cons number gnus-low-score-mark)
5487                   gnus-newsgroup-reads)))
5488
5489         (setq mark (gnus-article-mark number))
5490         (push (gnus-data-make number mark (1+ (point)) header 0)
5491               gnus-newsgroup-data)
5492         (gnus-summary-insert-line
5493          header 0 number
5494          (memq number gnus-newsgroup-undownloaded)
5495          mark (memq number gnus-newsgroup-replied)
5496          (memq number gnus-newsgroup-expirable)
5497          (mail-header-subject header) nil
5498          (cdr (assq number gnus-newsgroup-scored))
5499          (memq number gnus-newsgroup-processable))))))
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 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7281 (defun gnus-summary-exit-no-update (&optional no-questions)
7282   "Quit reading current newsgroup without updating read article info."
7283   (interactive)
7284   (let* ((group gnus-newsgroup-name)
7285          (gnus-group-is-exiting-p t)
7286          (gnus-group-is-exiting-without-update-p t)
7287          (quit-config (gnus-group-quit-config group)))
7288     (when (or no-questions
7289               gnus-expert-user
7290               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7291       (gnus-async-halt-prefetch)
7292       (run-hooks 'gnus-summary-prepare-exit-hook)
7293       (when (gnus-buffer-live-p gnus-article-buffer)
7294         (with-current-buffer gnus-article-buffer
7295           (gnus-article-stop-animations)
7296           (gnus-stop-downloads)
7297           (mm-destroy-parts gnus-article-mime-handles)
7298           ;; Set it to nil for safety reason.
7299           (setq gnus-article-mime-handle-alist nil)
7300           (setq gnus-article-mime-handles nil)))
7301       ;; If we have several article buffers, we kill them at exit.
7302       (unless gnus-single-article-buffer
7303         (gnus-kill-buffer gnus-article-buffer)
7304         (gnus-kill-buffer gnus-original-article-buffer)
7305         (setq gnus-article-current nil))
7306       ;; Return to the group buffer.
7307       (if (not gnus-kill-summary-on-exit)
7308           (progn
7309             (gnus-deaden-summary)
7310             (gnus-configure-windows 'group 'force))
7311         (gnus-configure-windows 'group 'force)
7312         (gnus-close-group group)
7313         (gnus-kill-buffer gnus-summary-buffer))
7314       (unless gnus-single-article-buffer
7315         (setq gnus-article-current nil))
7316       (when gnus-use-trees
7317         (gnus-tree-close group))
7318       (gnus-async-prefetch-remove-group group)
7319       (when (get-buffer gnus-article-buffer)
7320         (bury-buffer gnus-article-buffer))
7321       ;; Clear the current group name.
7322       (setq gnus-newsgroup-name nil)
7323       (unless (gnus-ephemeral-group-p group)
7324         (gnus-group-update-group group nil t))
7325       (when (equal (gnus-group-group-name) group)
7326         (gnus-group-next-unread-group 1))
7327       (when quit-config
7328         (gnus-handle-ephemeral-exit quit-config)))))
7329
7330 (defun gnus-handle-ephemeral-exit (quit-config)
7331   "Handle movement when leaving an ephemeral group.
7332 The state which existed when entering the ephemeral is reset."
7333   (if (not (buffer-live-p (car quit-config)))
7334       (when (gnus-buffer-live-p gnus-group-buffer)
7335         (gnus-configure-windows 'group 'force))
7336     (set-buffer (car quit-config))
7337     (unless (eq (cdr quit-config) 'group)
7338       (setq gnus-current-select-method
7339             (gnus-find-method-for-group gnus-newsgroup-name)))
7340     (cond ((eq major-mode 'gnus-summary-mode)
7341            (gnus-set-global-variables))
7342           ((eq major-mode 'gnus-article-mode)
7343            (save-current-buffer
7344              ;; The `gnus-summary-buffer' variable may point
7345              ;; to the old summary buffer when using a single
7346              ;; article buffer.
7347              (unless (gnus-buffer-live-p gnus-summary-buffer)
7348                (set-buffer gnus-group-buffer))
7349              (set-buffer gnus-summary-buffer)
7350              (gnus-set-global-variables))))
7351     (if (or (eq (cdr quit-config) 'article)
7352             (eq (cdr quit-config) 'pick))
7353         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7354             (gnus-configure-windows 'pick 'force)
7355           (gnus-configure-windows (cdr quit-config) 'force))
7356       (gnus-configure-windows (cdr quit-config) 'force))
7357     (when (eq major-mode 'gnus-summary-mode)
7358       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7359                                                      next-unread-noselect))
7360           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7361                                   'next-noselect)
7362                               (gnus-summary-next-subject 1 nil t))
7363                              ((eq gnus-auto-select-on-ephemeral-exit
7364                                   'next-unread-noselect)
7365                               (gnus-summary-next-subject 1 t t))))
7366             ;; Hide the article buffer which displays the article different
7367             ;; from the one that the cursor points to in the summary buffer.
7368             (gnus-configure-windows 'summary 'force))
7369         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7370                (gnus-summary-next-subject 1))
7371               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7372                (gnus-summary-next-subject 1 t))))
7373       (gnus-summary-recenter)
7374       (gnus-summary-position-point))))
7375
7376 ;;; Dead summaries.
7377
7378 (defvar gnus-dead-summary-mode-map
7379   (let ((map (make-keymap)))
7380     (suppress-keymap map)
7381     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7382     (dolist (key '("\C-d" "\r" "\177" [delete]))
7383       (define-key map key 'gnus-summary-wake-up-the-dead))
7384     (dolist (key '("q" "Q"))
7385       (define-key map key 'bury-buffer))
7386     map))
7387
7388 (define-minor-mode gnus-dead-summary-mode
7389   "Minor mode for Gnus summary buffers."
7390   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7391   (unless (derived-mode-p 'gnus-summary-mode)
7392     (setq gnus-dead-summary-mode nil)))
7393
7394 (defun gnus-deaden-summary ()
7395   "Make the current summary buffer into a dead summary buffer."
7396   ;; Kill any previous dead summary buffer.
7397   (when (and gnus-dead-summary
7398              (buffer-name gnus-dead-summary))
7399     (with-current-buffer gnus-dead-summary
7400       (when gnus-dead-summary-mode
7401         (kill-buffer (current-buffer)))))
7402   ;; Make this the current dead summary.
7403   (setq gnus-dead-summary (current-buffer))
7404   (gnus-dead-summary-mode 1)
7405   (let ((name (buffer-name)))
7406     (when (string-match "Summary" name)
7407       (rename-buffer
7408        (concat (substring name 0 (match-beginning 0)) "Dead "
7409                (substring name (match-beginning 0)))
7410        t)
7411       (bury-buffer))))
7412
7413 (defun gnus-kill-or-deaden-summary (buffer)
7414   "Kill or deaden the summary BUFFER."
7415   (save-excursion
7416     (when (and (buffer-name buffer)
7417                (not gnus-single-article-buffer))
7418       (with-current-buffer buffer
7419         (gnus-kill-buffer gnus-article-buffer)
7420         (gnus-kill-buffer gnus-original-article-buffer)))
7421     (cond
7422      ;; Kill the buffer.
7423      (gnus-kill-summary-on-exit
7424       (when (and gnus-use-trees
7425                  (gnus-buffer-exists-p buffer))
7426         (with-current-buffer buffer
7427           (gnus-tree-close gnus-newsgroup-name)))
7428       (gnus-kill-buffer buffer))
7429      ;; Deaden the buffer.
7430      ((gnus-buffer-exists-p buffer)
7431       (with-current-buffer buffer
7432         (gnus-deaden-summary))))))
7433
7434 (defun gnus-summary-wake-up-the-dead (&rest args)
7435   "Wake up the dead summary buffer."
7436   (interactive)
7437   (gnus-dead-summary-mode -1)
7438   (let ((name (buffer-name)))
7439     (when (string-match "Dead " name)
7440       (rename-buffer
7441        (concat (substring name 0 (match-beginning 0))
7442                (substring name (match-end 0)))
7443        t)))
7444   (gnus-message 3 "This dead summary is now alive again"))
7445
7446 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7447 (defun gnus-summary-describe-group (&optional force)
7448   "Describe the current newsgroup."
7449   (interactive "P")
7450   (gnus-group-describe-group force gnus-newsgroup-name))
7451
7452 (defun gnus-summary-describe-briefly ()
7453   "Describe summary mode commands briefly."
7454   (interactive)
7455   (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")))
7456
7457 ;; Walking around group mode buffer from summary mode.
7458
7459 (defun gnus-summary-next-group (&optional no-article target-group backward)
7460   "Exit current newsgroup and then select next unread newsgroup.
7461 If prefix argument NO-ARTICLE is non-nil, no article is selected
7462 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7463 previous group instead."
7464   (interactive "P")
7465   ;; Stop pre-fetching.
7466   (gnus-async-halt-prefetch)
7467   (let ((current-group gnus-newsgroup-name)
7468         (current-buffer (current-buffer))
7469         entered)
7470     ;; First we semi-exit this group to update Xrefs and all variables.
7471     ;; We can't do a real exit, because the window conf must remain
7472     ;; the same in case the user is prompted for info, and we don't
7473     ;; want the window conf to change before that...
7474     (gnus-summary-exit t)
7475     (while (not entered)
7476       ;; Then we find what group we are supposed to enter.
7477       (set-buffer gnus-group-buffer)
7478       (gnus-group-jump-to-group current-group)
7479       (setq target-group
7480             (or target-group
7481                 (if (eq gnus-keep-same-level 'best)
7482                     (gnus-summary-best-group gnus-newsgroup-name)
7483                   (gnus-summary-search-group backward gnus-keep-same-level))))
7484       (if (not target-group)
7485           ;; There are no further groups, so we return to the group
7486           ;; buffer.
7487           (progn
7488             (gnus-message 5 "Returning to the group buffer")
7489             (setq entered t)
7490             (when (gnus-buffer-live-p current-buffer)
7491               (set-buffer current-buffer)
7492               (gnus-summary-exit))
7493             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7494         ;; We try to enter the target group.
7495         (gnus-group-jump-to-group target-group)
7496         (let ((unreads (gnus-group-group-unread)))
7497           (if (and (or (eq t unreads)
7498                        (and unreads (not (zerop unreads))))
7499                    (gnus-summary-read-group
7500                     target-group nil no-article
7501                     (and (buffer-name current-buffer) current-buffer)
7502                     nil backward))
7503               (setq entered t)
7504             (setq current-group target-group
7505                   target-group nil)))))))
7506
7507 (defun gnus-summary-prev-group (&optional no-article)
7508   "Exit current newsgroup and then select previous unread newsgroup.
7509 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7510   (interactive "P")
7511   (gnus-summary-next-group no-article nil t))
7512
7513 ;; Walking around summary lines.
7514
7515 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7516   "Go to the first subject satisfying any non-nil constraint.
7517 If UNREAD is non-nil, the article should be unread.
7518 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7519 If UNSEEN is non-nil, the article should be unseen as well as unread.
7520 Returns the article selected or nil if there are no matching articles."
7521   (interactive "P")
7522   (cond
7523    ;; Empty summary.
7524    ((null gnus-newsgroup-data)
7525     (gnus-message 3 "No articles in the group")
7526     nil)
7527    ;; Pick the first article.
7528    ((not (or unread undownloaded unseen))
7529     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7530     (gnus-data-number (car gnus-newsgroup-data)))
7531    ;; Find the first unread article.
7532    (t
7533     (let ((data gnus-newsgroup-data))
7534       (while (and data
7535                   (let ((num (gnus-data-number (car data))))
7536                     (or (memq num gnus-newsgroup-unfetched)
7537                         (not (or (and unread
7538                                       (memq num gnus-newsgroup-unreads))
7539                                  (and undownloaded
7540                                       (memq num gnus-newsgroup-undownloaded))
7541                                  (and unseen
7542                                       (memq num gnus-newsgroup-unseen)
7543                                       (memq num gnus-newsgroup-unreads)))))))
7544         (setq data (cdr data)))
7545       (prog1
7546           (if data
7547               (progn
7548                 (goto-char (gnus-data-pos (car data)))
7549                 (gnus-data-number (car data)))
7550             (gnus-message 3 "No more%s articles"
7551                           (let* ((r (when unread " unread"))
7552                                  (d (when undownloaded " undownloaded"))
7553                                  (s (when unseen " unseen"))
7554                                  (l (delq nil (list r d s))))
7555                             (cond ((= 3 (length l))
7556                                    (concat r "," d ", or" s))
7557                                   ((= 2 (length l))
7558                                    (concat (car l) ", or" (cadr l)))
7559                                   ((= 1 (length l))
7560                                    (car l))
7561                                   (t
7562                                    ""))))
7563             nil
7564             )
7565         (gnus-summary-position-point))))))
7566
7567 (defun gnus-summary-next-subject (n &optional unread dont-display)
7568   "Go to next N'th summary line.
7569 If N is negative, go to the previous N'th subject line.
7570 If UNREAD is non-nil, only unread articles are selected.
7571 The difference between N and the actual number of steps taken is
7572 returned."
7573   (interactive "p")
7574   (let ((backward (< n 0))
7575         (n (abs n)))
7576     (while (and (> n 0)
7577                 (if backward
7578                     (gnus-summary-find-prev unread)
7579                   (gnus-summary-find-next unread)))
7580       (unless (zerop (setq n (1- n)))
7581         (gnus-summary-show-thread)))
7582     (when (/= 0 n)
7583       (gnus-message 7 "No more%s articles"
7584                     (if unread " unread" "")))
7585     (unless dont-display
7586       (gnus-summary-recenter)
7587       (gnus-summary-position-point))
7588     n))
7589
7590 (defun gnus-summary-next-unread-subject (n)
7591   "Go to next N'th unread summary line."
7592   (interactive "p")
7593   (gnus-summary-next-subject n t))
7594
7595 (defun gnus-summary-prev-subject (n &optional unread)
7596   "Go to previous N'th summary line.
7597 If optional argument UNREAD is non-nil, only unread article is selected."
7598   (interactive "p")
7599   (gnus-summary-next-subject (- n) unread))
7600
7601 (defun gnus-summary-prev-unread-subject (n)
7602   "Go to previous N'th unread summary line."
7603   (interactive "p")
7604   (gnus-summary-next-subject (- n) t))
7605
7606 (defun gnus-summary-goto-subjects (articles)
7607   "Insert the subject header for ARTICLES in the current buffer."
7608   (save-excursion
7609     (dolist (article articles)
7610       (gnus-summary-goto-subject article t)))
7611   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7612   (gnus-summary-position-point))
7613
7614 (defun gnus-summary-goto-subject (article &optional force silent)
7615   "Go to the subject line of ARTICLE.
7616 If FORCE, also allow jumping to articles not currently shown."
7617   (interactive "nArticle number: ")
7618   (unless (numberp article)
7619     (error "Article %s is not a number" article))
7620   (let ((b (point))
7621         (data (gnus-data-find article)))
7622     ;; We read in the article if we have to.
7623     (and (not data)
7624          force
7625          (gnus-summary-insert-subject
7626           article
7627           (if (or (numberp force) (vectorp force)) force)
7628           t)
7629          (setq data (gnus-data-find article)))
7630     (goto-char b)
7631     (if (not data)
7632         (progn
7633           (unless silent
7634             (gnus-message 3 "Can't find article %d" article))
7635           nil)
7636       (let ((pt (gnus-data-pos data)))
7637         (goto-char pt)
7638         (gnus-summary-set-article-display-arrow pt))
7639       (gnus-summary-position-point)
7640       article)))
7641
7642 ;; Walking around summary lines with displaying articles.
7643
7644 (defun gnus-summary-expand-window (&optional arg)
7645   "Make the summary buffer take up the entire Emacs frame.
7646 Given a prefix, will force an `article' buffer configuration."
7647   (interactive "P")
7648   (if arg
7649       (gnus-configure-windows 'article 'force)
7650     (gnus-configure-windows 'summary 'force)))
7651
7652 (defun gnus-summary-display-article (article &optional all-header)
7653   "Display ARTICLE in article buffer."
7654   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7655                (with-current-buffer gnus-article-buffer
7656                  (eq major-mode 'gnus-article-mode)))
7657     (gnus-article-setup-buffer))
7658   (gnus-set-global-variables)
7659   (with-current-buffer gnus-article-buffer
7660     (setq gnus-article-charset gnus-newsgroup-charset)
7661     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7662     (mm-enable-multibyte))
7663   (if (null article)
7664       nil
7665     (prog1
7666         (if gnus-summary-display-article-function
7667             (funcall gnus-summary-display-article-function article all-header)
7668           (gnus-article-prepare article all-header))
7669       (gnus-run-hooks 'gnus-select-article-hook)
7670       (when (and gnus-current-article
7671                  (not (zerop gnus-current-article)))
7672         (gnus-summary-goto-subject gnus-current-article))
7673       (gnus-summary-recenter)
7674       (when (and gnus-use-trees gnus-show-threads)
7675         (gnus-possibly-generate-tree article)
7676         (gnus-highlight-selected-tree article))
7677       ;; Successfully display article.
7678       (gnus-article-set-window-start
7679        (cdr (assq article gnus-newsgroup-bookmarks))))))
7680
7681 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7682   "Select the current article.
7683 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7684 non-nil, the article will be re-fetched even if it already present in
7685 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7686 be displayed."
7687   ;; Make sure we are in the summary buffer to work around bbdb bug.
7688   (unless (eq major-mode 'gnus-summary-mode)
7689     (set-buffer gnus-summary-buffer))
7690   (let ((article (or article (gnus-summary-article-number)))
7691         (all-headers (not (not all-headers))) ;Must be t or nil.
7692         gnus-summary-display-article-function)
7693     (and (not pseudo)
7694          (gnus-summary-article-pseudo-p article)
7695          (error "This is a pseudo-article"))
7696     (with-current-buffer gnus-summary-buffer
7697       (if (or (and gnus-single-article-buffer
7698                    (or (null gnus-current-article)
7699                        (null gnus-article-current)
7700                        (null (get-buffer gnus-article-buffer))
7701                        (not (eq article (cdr gnus-article-current)))
7702                        (not (equal (car gnus-article-current)
7703                                    gnus-newsgroup-name))
7704                        (not (get-buffer gnus-original-article-buffer))))
7705               (and (not gnus-single-article-buffer)
7706                    (or (null gnus-current-article)
7707                        (not (get-buffer gnus-original-article-buffer))
7708                        (not (eq gnus-current-article article))))
7709               force)
7710           ;; The requested article is different from the current article.
7711           (progn
7712             (gnus-summary-display-article article all-headers)
7713             (when (gnus-buffer-live-p gnus-article-buffer)
7714               (with-current-buffer gnus-article-buffer
7715                 (if (not gnus-article-decoded-p) ;; a local variable
7716                     (mm-disable-multibyte))))
7717             (gnus-article-set-window-start
7718              (cdr (assq article gnus-newsgroup-bookmarks)))
7719             article)
7720         'old))))
7721
7722 (defun gnus-summary-force-verify-and-decrypt ()
7723   "Display buttons for signed/encrypted parts and verify/decrypt them."
7724   (interactive)
7725   (let ((mm-verify-option 'known)
7726         (mm-decrypt-option 'known)
7727         (gnus-article-emulate-mime t)
7728         (gnus-buttonized-mime-types (append (list "multipart/signed"
7729                                                   "multipart/encrypted")
7730                                             gnus-buttonized-mime-types)))
7731     (gnus-summary-select-article nil 'force)))
7732
7733 (defun gnus-summary-next-article (&optional unread subject backward push)
7734   "Select the next article.
7735 If UNREAD, only unread articles are selected.
7736 If SUBJECT, only articles with SUBJECT are selected.
7737 If BACKWARD, the previous article is selected instead of the next."
7738   (interactive "P")
7739   ;; Make sure we are in the summary buffer.
7740   (unless (eq major-mode 'gnus-summary-mode)
7741     (set-buffer gnus-summary-buffer))
7742   (cond
7743    ;; Is there such an article?
7744    ((and (gnus-summary-search-forward unread subject backward)
7745          (or (gnus-summary-display-article (gnus-summary-article-number))
7746              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7747     (gnus-summary-position-point))
7748    ;; If not, we try the first unread, if that is wanted.
7749    ((and subject
7750          gnus-auto-select-same
7751          (gnus-summary-first-unread-article))
7752     (gnus-summary-position-point)
7753     (gnus-message 6 "Wrapped"))
7754    ;; Try to get next/previous article not displayed in this group.
7755    ((and gnus-auto-extend-newsgroup
7756          (not unread) (not subject))
7757     (gnus-summary-goto-article
7758      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7759      nil (count-lines (point-min) (point))))
7760    ;; Go to next/previous group.
7761    (t
7762     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7763       (gnus-summary-jump-to-group gnus-newsgroup-name))
7764     (let ((cmd (if (featurep 'xemacs)
7765                    last-command-char
7766                  last-command-event))
7767           (point
7768            (with-current-buffer gnus-group-buffer
7769              (point)))
7770           (current-summary (current-buffer))
7771           (group
7772            (if (eq gnus-keep-same-level 'best)
7773                (gnus-summary-best-group gnus-newsgroup-name)
7774              (gnus-summary-search-group backward gnus-keep-same-level))))
7775       ;; Select next unread newsgroup automagically.
7776       (cond
7777        ((or (not gnus-auto-select-next)
7778             (not cmd))
7779         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7780        ((or (eq gnus-auto-select-next 'quietly)
7781             (and (eq gnus-auto-select-next 'slightly-quietly)
7782                  push)
7783             (and (eq gnus-auto-select-next 'almost-quietly)
7784                  (gnus-summary-last-article-p)))
7785         ;; Select quietly.
7786         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7787             (gnus-summary-exit)
7788           (gnus-message 7 "No more%s articles (%s)..."
7789                         (if unread " unread" "")
7790                         (if group (concat "selecting " group)
7791                           "exiting"))
7792           (gnus-summary-next-group nil group backward)))
7793        (t
7794         (when (gnus-key-press-event-p last-input-event)
7795           ;; Somehow or other, we may now have selected a different
7796           ;; window.  Make point go back to the summary buffer.
7797           (when (eq current-summary (current-buffer))
7798             ;; FIXME: This burps when get-buffer-window returns nil.
7799             (select-window (get-buffer-window current-summary 0)))
7800           (gnus-summary-walk-group-buffer
7801            gnus-newsgroup-name cmd unread backward point))))))))
7802
7803 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7804   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7805                       (?\C-p (gnus-group-prev-unread-group 1))))
7806         (cursor-in-echo-area t)
7807         keve key group ended prompt)
7808     (with-current-buffer gnus-group-buffer
7809       (goto-char start)
7810       (setq group
7811             (if (eq gnus-keep-same-level 'best)
7812                 (gnus-summary-best-group gnus-newsgroup-name)
7813               (gnus-summary-search-group backward gnus-keep-same-level))))
7814     (while (not ended)
7815       (setq prompt
7816             (format
7817              "No more%s articles%s " (if unread " unread" "")
7818              (if (and group
7819                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7820                  (format " (Type %s for %s [%s])"
7821                          (single-key-description cmd)
7822                          (gnus-group-decoded-name group)
7823                          (gnus-group-unread group))
7824                (format " (Type %s to exit %s)"
7825                        (single-key-description cmd)
7826                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7827       ;; Confirm auto selection.
7828       (setq key (car (setq keve (gnus-read-event-char prompt)))
7829             ended t)
7830       (cond
7831        ((assq key keystrokes)
7832         (let ((obuf (current-buffer)))
7833           (switch-to-buffer gnus-group-buffer)
7834           (when group
7835             (gnus-group-jump-to-group group))
7836           (eval (cadr (assq key keystrokes)))
7837           (setq group (gnus-group-group-name))
7838           (switch-to-buffer obuf))
7839         (setq ended nil))
7840        ((equal key cmd)
7841         (if (or (not group)
7842                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7843             (gnus-summary-exit)
7844           (gnus-summary-next-group nil group backward)))
7845        (t
7846         (push (cdr keve) unread-command-events))))))
7847
7848 (defun gnus-summary-next-unread-article ()
7849   "Select unread article after current one."
7850   (interactive)
7851   (gnus-summary-next-article
7852    (or (not (eq gnus-summary-goto-unread 'never))
7853        (gnus-summary-last-article-p (gnus-summary-article-number)))
7854    (and gnus-auto-select-same
7855         (gnus-summary-article-subject))))
7856
7857 (defun gnus-summary-prev-article (&optional unread subject)
7858   "Select the article before the current one.
7859 If UNREAD is non-nil, only unread articles are selected."
7860   (interactive "P")
7861   (gnus-summary-next-article unread subject t))
7862
7863 (defun gnus-summary-prev-unread-article ()
7864   "Select unread article before current one."
7865   (interactive)
7866   (gnus-summary-prev-article
7867    (or (not (eq gnus-summary-goto-unread 'never))
7868        (gnus-summary-first-article-p (gnus-summary-article-number)))
7869    (and gnus-auto-select-same
7870         (gnus-summary-article-subject))))
7871
7872 (defun gnus-summary-next-page (&optional lines circular stop)
7873   "Show next page of the selected article.
7874 If at the end of the current article, select the next article.
7875 LINES says how many lines should be scrolled up.
7876
7877 If CIRCULAR is non-nil, go to the start of the article instead of
7878 selecting the next article when reaching the end of the current
7879 article.
7880
7881 If STOP is non-nil, just stop when reaching the end of the message.
7882
7883 Also see the variable `gnus-article-skip-boring'."
7884   (interactive "P")
7885   (setq gnus-summary-buffer (current-buffer))
7886   (gnus-set-global-variables)
7887   (let ((article (gnus-summary-article-number))
7888         (article-window (get-buffer-window gnus-article-buffer t))
7889         endp)
7890     ;; If the buffer is empty, we have no article.
7891     (unless article
7892       (error "No article to select"))
7893     (gnus-configure-windows 'article)
7894     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7895         (if (and (eq gnus-summary-goto-unread 'never)
7896                  (not (gnus-summary-last-article-p article)))
7897             (gnus-summary-next-article)
7898           (gnus-summary-next-unread-article))
7899       (if (or (null gnus-current-article)
7900               (null gnus-article-current)
7901               (/= article (cdr gnus-article-current))
7902               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7903           ;; Selected subject is different from current article's.
7904           (gnus-summary-display-article article)
7905         (when article-window
7906           (gnus-eval-in-buffer-window gnus-article-buffer
7907             (setq endp (or (gnus-article-next-page lines)
7908                            (gnus-article-only-boring-p))))
7909           (when endp
7910             (cond ((or stop gnus-summary-stop-at-end-of-message)
7911                    (gnus-message 3 "End of message"))
7912                   (circular
7913                    (gnus-summary-beginning-of-article))
7914                   (lines
7915                    (gnus-message 3 "End of message"))
7916                   ((null lines)
7917                    (if (and (eq gnus-summary-goto-unread 'never)
7918                             (not (gnus-summary-last-article-p article)))
7919                        (gnus-summary-next-article)
7920                      (gnus-summary-next-unread-article))))))))
7921     (gnus-summary-recenter)
7922     (gnus-summary-position-point)))
7923
7924 (defun gnus-summary-prev-page (&optional lines move)
7925   "Show previous page of selected article.
7926 Argument LINES specifies lines to be scrolled down.
7927 If MOVE, move to the previous unread article if point is at
7928 the beginning of the buffer."
7929   (interactive "P")
7930   (let ((article (gnus-summary-article-number))
7931         (article-window (get-buffer-window gnus-article-buffer t))
7932         endp)
7933     (gnus-configure-windows 'article)
7934     (if (or (null gnus-current-article)
7935             (null gnus-article-current)
7936             (/= article (cdr gnus-article-current))
7937             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7938         ;; Selected subject is different from current article's.
7939         (gnus-summary-display-article article)
7940       (gnus-summary-recenter)
7941       (when article-window
7942         (gnus-eval-in-buffer-window gnus-article-buffer
7943           (setq endp (gnus-article-prev-page lines)))
7944         (when (and move endp)
7945           (cond (lines
7946                  (gnus-message 3 "Beginning of message"))
7947                 ((null lines)
7948                  (if (and (eq gnus-summary-goto-unread 'never)
7949                           (not (gnus-summary-first-article-p article)))
7950                      (gnus-summary-prev-article)
7951                    (gnus-summary-prev-unread-article))))))))
7952   (gnus-summary-position-point))
7953
7954 (defun gnus-summary-prev-page-or-article (&optional lines)
7955   "Show previous page of selected article.
7956 Argument LINES specifies lines to be scrolled down.
7957 If at the beginning of the article, go to the next article."
7958   (interactive "P")
7959   (gnus-summary-prev-page lines t))
7960
7961 (defun gnus-summary-scroll-up (lines)
7962   "Scroll up (or down) one line current article.
7963 Argument LINES specifies lines to be scrolled up (or down if negative).
7964 If no article is selected, then the current article will be selected first."
7965   (interactive "p")
7966   (gnus-configure-windows 'article)
7967   (gnus-summary-show-thread)
7968   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7969     (gnus-eval-in-buffer-window gnus-article-buffer
7970       (cond ((> lines 0)
7971              (when (gnus-article-next-page lines)
7972                (gnus-message 3 "End of message")))
7973             ((< lines 0)
7974              (gnus-article-prev-page (- lines))))))
7975   (gnus-summary-recenter)
7976   (gnus-summary-position-point))
7977
7978 (defun gnus-summary-scroll-down (lines)
7979   "Scroll down (or up) one line current article.
7980 Argument LINES specifies lines to be scrolled down (or up if negative).
7981 If no article is selected, then the current article will be selected first."
7982   (interactive "p")
7983   (gnus-summary-scroll-up (- lines)))
7984
7985 (defun gnus-summary-next-same-subject ()
7986   "Select next article which has the same subject as current one."
7987   (interactive)
7988   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7989
7990 (defun gnus-summary-prev-same-subject ()
7991   "Select previous article which has the same subject as current one."
7992   (interactive)
7993   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7994
7995 (defun gnus-summary-next-unread-same-subject ()
7996   "Select next unread article which has the same subject as current one."
7997   (interactive)
7998   (gnus-summary-next-article t (gnus-summary-article-subject)))
7999
8000 (defun gnus-summary-prev-unread-same-subject ()
8001   "Select previous unread article which has the same subject as current one."
8002   (interactive)
8003   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8004
8005 (defun gnus-summary-first-unread-article ()
8006   "Select the first unread article.
8007 Return nil if there are no unread articles."
8008   (interactive)
8009   (prog1
8010       (when (gnus-summary-first-subject t)
8011         (gnus-summary-show-thread)
8012         (gnus-summary-first-subject t)
8013         (gnus-summary-display-article (gnus-summary-article-number)))
8014     (gnus-summary-position-point)))
8015
8016 (defun gnus-summary-first-unread-subject ()
8017   "Place the point on the subject line of the first unread article.
8018 Return nil if there are no unread articles."
8019   (interactive)
8020   (prog1
8021       (when (gnus-summary-first-subject t)
8022         (gnus-summary-show-thread)
8023         (gnus-summary-first-subject t))
8024     (gnus-summary-position-point)))
8025
8026 (defun gnus-summary-first-unseen-subject ()
8027   "Place the point on the subject line of the first unseen article.
8028 Return nil if there are no unseen articles."
8029   (interactive)
8030   (prog1
8031       (when (gnus-summary-first-subject nil nil t)
8032         (gnus-summary-show-thread)
8033         (gnus-summary-first-subject nil nil t))
8034     (gnus-summary-position-point)))
8035
8036 (defun gnus-summary-first-unseen-or-unread-subject ()
8037   "Place the point on the subject line of the first unseen and unread article.
8038 If all article have been seen, on the subject line of the first unread
8039 article."
8040   (interactive)
8041   (prog1
8042       (unless (when (gnus-summary-first-subject nil nil t)
8043                 (gnus-summary-show-thread)
8044                 (gnus-summary-first-subject nil nil t))
8045         (when (gnus-summary-first-subject t)
8046           (gnus-summary-show-thread)
8047           (gnus-summary-first-subject t)))
8048     (gnus-summary-position-point)))
8049
8050 (defun gnus-summary-first-article ()
8051   "Select the first article.
8052 Return nil if there are no articles."
8053   (interactive)
8054   (prog1
8055       (when (gnus-summary-first-subject)
8056         (gnus-summary-show-thread)
8057         (gnus-summary-first-subject)
8058         (gnus-summary-display-article (gnus-summary-article-number)))
8059     (gnus-summary-position-point)))
8060
8061 (defun gnus-summary-best-unread-article (&optional arg)
8062   "Select the unread article with the highest score.
8063 If given a prefix argument, select the next unread article that has a
8064 score higher than the default score."
8065   (interactive "P")
8066   (let ((article (if arg
8067                      (gnus-summary-better-unread-subject)
8068                    (gnus-summary-best-unread-subject))))
8069     (if article
8070         (gnus-summary-goto-article article)
8071       (error "No unread articles"))))
8072
8073 (defun gnus-summary-best-unread-subject ()
8074   "Select the unread subject with the highest score."
8075   (interactive)
8076   (let ((best -1000000)
8077         (data gnus-newsgroup-data)
8078         article score)
8079     (while data
8080       (and (gnus-data-unread-p (car data))
8081            (> (setq score
8082                     (gnus-summary-article-score (gnus-data-number (car data))))
8083               best)
8084            (setq best score
8085                  article (gnus-data-number (car data))))
8086       (setq data (cdr data)))
8087     (when article
8088       (gnus-summary-goto-subject article))
8089     (gnus-summary-position-point)
8090     article))
8091
8092 (defun gnus-summary-better-unread-subject ()
8093   "Select the first unread subject that has a score over the default score."
8094   (interactive)
8095   (let ((data gnus-newsgroup-data)
8096         article score)
8097     (while (and (setq article (gnus-data-number (car data)))
8098                 (or (gnus-data-read-p (car data))
8099                     (not (> (gnus-summary-article-score article)
8100                             gnus-summary-default-score))))
8101       (setq data (cdr data)))
8102     (when article
8103       (gnus-summary-goto-subject article))
8104     (gnus-summary-position-point)
8105     article))
8106
8107 (defun gnus-summary-last-subject ()
8108   "Go to the last displayed subject line in the group."
8109   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8110     (when article
8111       (gnus-summary-goto-subject article))))
8112
8113 (defun gnus-summary-goto-article (article &optional all-headers force)
8114   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8115 If ALL-HEADERS is non-nil, no header lines are hidden.
8116 If FORCE, go to the article even if it isn't displayed.  If FORCE
8117 is a number, it is the line the article is to be displayed on."
8118   (interactive
8119    (list
8120     (gnus-completing-read
8121      "Article number or Message-ID"
8122      (mapcar 'int-to-string gnus-newsgroup-limit))
8123     current-prefix-arg
8124     t))
8125   (prog1
8126       (if (and (stringp article)
8127                (string-match "@\\|%40" article))
8128           (gnus-summary-refer-article article)
8129         (when (stringp article)
8130           (setq article (string-to-number article)))
8131         (if (gnus-summary-goto-subject article force)
8132             (gnus-summary-display-article article all-headers)
8133           (gnus-message 4 "Couldn't go to article %s" article) nil))
8134     (gnus-summary-position-point)))
8135
8136 (defun gnus-summary-goto-last-article ()
8137   "Go to the previously read article."
8138   (interactive)
8139   (prog1
8140       (when gnus-last-article
8141         (gnus-summary-goto-article gnus-last-article nil t))
8142     (gnus-summary-position-point)))
8143
8144 (defun gnus-summary-pop-article (number)
8145   "Pop one article off the history and go to the previous.
8146 NUMBER articles will be popped off."
8147   (interactive "p")
8148   (let (to)
8149     (setq gnus-newsgroup-history
8150           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8151     (if to
8152         (gnus-summary-goto-article (car to) nil t)
8153       (error "Article history empty")))
8154   (gnus-summary-position-point))
8155
8156 ;; Summary commands and functions for limiting the summary buffer.
8157
8158 (defun gnus-summary-limit-to-articles (n)
8159   "Limit the summary buffer to the next N articles.
8160 If not given a prefix, use the process marked articles instead."
8161   (interactive "P")
8162   (prog1
8163       (let ((articles (gnus-summary-work-articles n)))
8164         (setq gnus-newsgroup-processable nil)
8165         (gnus-summary-limit articles))
8166     (gnus-summary-position-point)))
8167
8168 (defun gnus-summary-pop-limit (&optional total)
8169   "Restore the previous limit.
8170 If given a prefix, remove all limits."
8171   (interactive "P")
8172   (when total
8173     (setq gnus-newsgroup-limits
8174           (list (mapcar (lambda (h) (mail-header-number h))
8175                         gnus-newsgroup-headers))))
8176   (unless gnus-newsgroup-limits
8177     (error "No limit to pop"))
8178   (prog1
8179       (gnus-summary-limit nil 'pop)
8180     (gnus-summary-position-point)))
8181
8182 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8183   "Limit the summary buffer to articles that have subjects that match a regexp.
8184 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8185   (interactive
8186    (list (read-string (if current-prefix-arg
8187                           "Exclude subject (regexp): "
8188                         "Limit to subject (regexp): "))
8189          nil current-prefix-arg))
8190   (unless header
8191     (setq header "subject"))
8192   (when (not (equal "" subject))
8193     (prog1
8194         (let ((articles (gnus-summary-find-matching
8195                          (or header "subject") subject 'all nil nil
8196                          not-matching)))
8197           (unless articles
8198             (error "Found no matches for \"%s\"" subject))
8199           (gnus-summary-limit articles))
8200       (gnus-summary-position-point))))
8201
8202 (defun gnus-summary-limit-to-author (from &optional not-matching)
8203   "Limit the summary buffer to articles that have authors that match a regexp.
8204 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8205   (interactive
8206    (list (let* ((header (gnus-summary-article-header))
8207                 (default (and header (car (mail-header-parse-address
8208                                            (mail-header-from header))))))
8209            (read-string (concat (if current-prefix-arg
8210                                     "Exclude author (regexp"
8211                                   "Limit to author (regexp")
8212                                 (if default
8213                                     (concat ", default \"" default "\"): ")
8214                                   "): "))
8215                         nil nil
8216                         default))
8217          current-prefix-arg))
8218   (gnus-summary-limit-to-subject from "from" not-matching))
8219
8220 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8221   "Limit the summary buffer to articles with the given RECIPIENT.
8222
8223 If NOT-MATCHING, exclude RECIPIENT.
8224
8225 To and Cc headers are checked.  You need to include them in
8226 `nnmail-extra-headers'."
8227   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8228   (interactive
8229    (list (read-string (format "%s recipient (regexp): "
8230                               (if current-prefix-arg "Exclude" "Limit to")))
8231          current-prefix-arg))
8232   (when (not (equal "" recipient))
8233     (prog1 (let* ((to
8234                    (if (memq 'To nnmail-extra-headers)
8235                        (gnus-summary-find-matching
8236                         (cons 'extra 'To) recipient 'all nil nil
8237                         not-matching)
8238                      (gnus-message
8239                       1 "`To' isn't present in `nnmail-extra-headers'")
8240                      (sit-for 1)
8241                      nil))
8242                   (cc
8243                    (if (memq 'Cc nnmail-extra-headers)
8244                        (gnus-summary-find-matching
8245                         (cons 'extra 'Cc) recipient 'all nil nil
8246                         not-matching)
8247                      (gnus-message
8248                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8249                      (sit-for 1)
8250                      nil))
8251                   (articles
8252                    (if not-matching
8253                        ;; We need the numbers that are in both lists:
8254                        (mapcar (lambda (a)
8255                                  (and (memq a to) a))
8256                                cc)
8257                      (nconc to cc))))
8258              (unless articles
8259                (error "Found no matches for \"%s\"" recipient))
8260              (gnus-summary-limit articles))
8261       (gnus-summary-position-point))))
8262
8263 (defun gnus-summary-limit-to-address (address &optional not-matching)
8264   "Limit the summary buffer to articles with the given ADDRESS.
8265
8266 If NOT-MATCHING, exclude ADDRESS.
8267
8268 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8269 in `nnmail-extra-headers'."
8270   (interactive
8271    (list (read-string (format "%s address (regexp): "
8272                               (if current-prefix-arg "Exclude" "Limit to")))
8273          current-prefix-arg))
8274   (when (not (equal "" address))
8275     (prog1 (let* ((to
8276                    (if (memq 'To nnmail-extra-headers)
8277                        (gnus-summary-find-matching
8278                         (cons 'extra 'To) address 'all nil nil
8279                         not-matching)
8280                      (gnus-message
8281                       1 "`To' isn't present in `nnmail-extra-headers'")
8282                      (sit-for 1)
8283                      t))
8284                   (cc
8285                    (if (memq 'Cc nnmail-extra-headers)
8286                        (gnus-summary-find-matching
8287                         (cons 'extra 'Cc) address 'all nil nil
8288                         not-matching)
8289                      (gnus-message
8290                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8291                      (sit-for 1)
8292                      t))
8293                   (from
8294                    (gnus-summary-find-matching "from" address
8295                                                'all nil nil not-matching))
8296                   (articles
8297                    (if not-matching
8298                        ;; We need the numbers that are in all lists:
8299                        (if (eq cc t)
8300                            (if (eq to t)
8301                                from
8302                              (mapcar (lambda (a) (car (memq a from))) to))
8303                          (if (eq to t)
8304                              (mapcar (lambda (a) (car (memq a from))) cc)
8305                            (mapcar (lambda (a) (car (memq a from)))
8306                                    (mapcar (lambda (a) (car (memq a to)))
8307                                            cc))))
8308                      (nconc (if (eq to t) nil to)
8309                             (if (eq cc t) nil cc)
8310                             from))))
8311              (unless articles
8312                (error "Found no matches for \"%s\"" address))
8313              (gnus-summary-limit articles))
8314       (gnus-summary-position-point))))
8315
8316 (defun gnus-summary-limit-strange-charsets-predicate (header)
8317   (when (fboundp 'char-charset)
8318     (let ((string (concat (mail-header-subject header)
8319                           (mail-header-from header)))
8320           charset found)
8321       (dotimes (i (1- (length string)))
8322         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8323         (when (string-match "unicode\\|big\\|japanese" charset)
8324           (setq found t)))
8325       found)))
8326
8327 (defun gnus-summary-limit-to-predicate (predicate)
8328   "Limit to articles where PREDICATE returns non-nil.
8329 PREDICATE will be called with the header structures of the
8330 articles."
8331   (let ((articles nil)
8332         (case-fold-search t))
8333     (dolist (header gnus-newsgroup-headers)
8334       (when (funcall predicate header)
8335         (push (mail-header-number header) articles)))
8336     (gnus-summary-limit (nreverse articles))))
8337
8338 (defun gnus-summary-limit-to-age (age &optional younger-p)
8339   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8340 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8341 articles that are younger than AGE days."
8342   (interactive
8343    (let ((younger current-prefix-arg)
8344          (days-got nil)
8345          days)
8346      (while (not days-got)
8347        (setq days (if younger
8348                       (read-string "Limit to articles younger than (in days, older when negative): ")
8349                     (read-string
8350                      "Limit to articles older than (in days, younger when negative): ")))
8351        (when (> (length days) 0)
8352          (setq days (read days)))
8353        (if (numberp days)
8354            (progn
8355              (setq days-got t)
8356              (when (< days 0)
8357                (setq younger (not younger))
8358                (setq days (* days -1))))
8359          (message "Please enter a number.")
8360          (sleep-for 1)))
8361      (list days younger)))
8362   (prog1
8363       (let ((data gnus-newsgroup-data)
8364             (cutoff (days-to-time age))
8365             articles d date is-younger)
8366         (while (setq d (pop data))
8367           (when (and (vectorp (gnus-data-header d))
8368                      (setq date (mail-header-date (gnus-data-header d))))
8369             (setq is-younger (time-less-p
8370                               (time-since (gnus-date-get-time date))
8371                               cutoff))
8372             (when (if younger-p
8373                       is-younger
8374                     (not is-younger))
8375               (push (gnus-data-number d) articles))))
8376         (gnus-summary-limit (nreverse articles)))
8377     (gnus-summary-position-point)))
8378
8379 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8380   "Limit the summary buffer to articles that match an 'extra' header."
8381   (interactive
8382    (let ((header
8383           (intern
8384            (gnus-completing-read
8385             (if current-prefix-arg
8386                 "Exclude extra header"
8387               "Limit extra header")
8388             (mapcar 'symbol-name gnus-extra-headers)
8389             t nil nil
8390             (symbol-name (car gnus-extra-headers))))))
8391      (list header
8392            (read-string (format "%s header %s (regexp): "
8393                                 (if current-prefix-arg "Exclude" "Limit to")
8394                                 header))
8395            current-prefix-arg)))
8396   (when (not (equal "" regexp))
8397     (prog1
8398         (let ((articles (gnus-summary-find-matching
8399                          (cons 'extra header) regexp 'all nil nil
8400                          not-matching)))
8401           (unless articles
8402             (error "Found no matches for \"%s\"" regexp))
8403           (gnus-summary-limit articles))
8404       (gnus-summary-position-point))))
8405
8406 (defun gnus-summary-limit-to-display-predicate ()
8407   "Limit the summary buffer to the predicated in the `display' group parameter."
8408   (interactive)
8409   (unless gnus-newsgroup-display
8410     (error "There is no `display' group parameter"))
8411   (let (articles)
8412     (dolist (gnus-number gnus-newsgroup-articles)
8413       (when (funcall gnus-newsgroup-display)
8414         (push gnus-number articles)))
8415     (gnus-summary-limit articles))
8416   (gnus-summary-position-point))
8417
8418 (defun gnus-summary-limit-to-unread (&optional all)
8419   "Limit the summary buffer to articles that are not marked as read.
8420 If ALL is non-nil, limit strictly to unread articles."
8421   (interactive "P")
8422   (if all
8423       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8424     (gnus-summary-limit-to-marks
8425      ;; Concat all the marks that say that an article is read and have
8426      ;; those removed.
8427      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8428            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8429            gnus-low-score-mark gnus-expirable-mark
8430            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8431            gnus-duplicate-mark)
8432      'reverse)))
8433
8434 (defun gnus-summary-limit-to-headers (match &optional reverse)
8435   "Limit the summary buffer to articles that have headers that match MATCH.
8436 If REVERSE (the prefix), limit to articles that don't match."
8437   (interactive "sMatch headers (regexp): \nP")
8438   (gnus-summary-limit-to-bodies match reverse t))
8439
8440 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8441   "Limit the summary buffer to articles that have bodies that match MATCH.
8442 If REVERSE (the prefix), limit to articles that don't match."
8443   (interactive "sMatch body (regexp): \nP")
8444   (let ((articles nil)
8445         (gnus-select-article-hook nil)  ;Disable hook.
8446         (gnus-article-prepare-hook nil)
8447         (gnus-use-article-prefetch nil)
8448         (gnus-keep-backlog nil)
8449         (gnus-break-pages nil)
8450         (gnus-summary-display-arrow nil)
8451         (gnus-updated-mode-lines nil)
8452         (gnus-auto-center-summary nil)
8453         (gnus-display-mime-function nil))
8454     (dolist (data gnus-newsgroup-data)
8455       (let (gnus-mark-article-hook)
8456         (gnus-summary-select-article t t nil (gnus-data-number data)))
8457       (with-current-buffer gnus-article-buffer
8458         (article-goto-body)
8459         (let* ((case-fold-search t)
8460                (found (if headersp
8461                           (re-search-backward match nil t)
8462                         (re-search-forward match nil t))))
8463           (when (or (and found
8464                          (not reverse))
8465                     (and (not found)
8466                          reverse))
8467             (push (gnus-data-number data) articles)))))
8468     (if (not articles)
8469         (message "No messages matched")
8470       (gnus-summary-limit articles)))
8471   (gnus-summary-position-point))
8472
8473 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8474   "Limit the summary buffer to articles that aren't part on any thread.
8475 If THREADSP (the prefix), limit to articles that are in threads."
8476   (interactive "P")
8477   (let ((articles nil)
8478         thread-articles
8479         threads)
8480     (dolist (thread gnus-newsgroup-threads)
8481       (if (stringp (car thread))
8482           (dolist (thread (cdr thread))
8483             (push thread threads))
8484         (push thread threads)))
8485     (dolist (thread threads)
8486       (setq thread-articles (gnus-articles-in-thread thread))
8487       (when (or (and threadsp
8488                      (> (length thread-articles) 1))
8489                 (and (not threadsp)
8490                      (= (length thread-articles) 1)))
8491         (setq articles (nconc thread-articles articles))))
8492     (if (not articles)
8493         (message "No messages matched")
8494       (gnus-summary-limit articles))
8495     (gnus-summary-position-point)))
8496
8497 (defun gnus-summary-limit-to-replied (&optional unreplied)
8498   "Limit the summary buffer to replied articles.
8499 If UNREPLIED (the prefix), limit to unreplied articles."
8500   (interactive "P")
8501   (if unreplied
8502       (gnus-summary-limit
8503        (gnus-set-difference gnus-newsgroup-articles
8504         gnus-newsgroup-replied))
8505     (gnus-summary-limit gnus-newsgroup-replied))
8506   (gnus-summary-position-point))
8507
8508 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8509   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8510 If REVERSE, limit the summary buffer to articles that are marked
8511 with MARKS.  MARKS can either be a string of marks or a list of marks.
8512 Returns how many articles were removed."
8513   (interactive "sMarks: ")
8514   (gnus-summary-limit-to-marks marks t))
8515
8516 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8517   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8518 If REVERSE (the prefix), limit the summary buffer to articles that are
8519 not marked with MARKS.  MARKS can either be a string of marks or a
8520 list of marks.
8521 Returns how many articles were removed."
8522   (interactive "sMarks: \nP")
8523   (prog1
8524       (let ((data gnus-newsgroup-data)
8525             (marks (if (listp marks) marks
8526                      (append marks nil))) ; Transform to list.
8527             articles)
8528         (while data
8529           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8530                   (memq (gnus-data-mark (car data)) marks))
8531             (push (gnus-data-number (car data)) articles))
8532           (setq data (cdr data)))
8533         (gnus-summary-limit articles))
8534     (gnus-summary-position-point)))
8535
8536 (defun gnus-summary-limit-to-score (score)
8537   "Limit to articles with score at or above SCORE."
8538   (interactive "NLimit to articles with score of at least: ")
8539   (let ((data gnus-newsgroup-data)
8540         articles)
8541     (while data
8542       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8543                 score)
8544         (push (gnus-data-number (car data)) articles))
8545       (setq data (cdr data)))
8546     (prog1
8547         (gnus-summary-limit articles)
8548       (gnus-summary-position-point))))
8549
8550 (defun gnus-summary-limit-to-unseen ()
8551   "Limit to unseen articles."
8552   (interactive)
8553   (prog1
8554       (gnus-summary-limit gnus-newsgroup-unseen)
8555     (gnus-summary-position-point)))
8556
8557 (defun gnus-summary-limit-include-thread (id)
8558   "Display all the hidden articles that is in the thread with ID in it.
8559 When called interactively, ID is the Message-ID of the current
8560 article."
8561   (interactive (list (mail-header-id (gnus-summary-article-header))))
8562   (let ((articles (gnus-articles-in-thread
8563                    (gnus-id-to-thread (gnus-root-id id))))
8564         ;;we REALLY want the whole thread---this prevents cut-threads
8565         ;;from removing the thread we want to include.
8566         (gnus-fetch-old-headers nil)
8567         (gnus-build-sparse-threads nil))
8568     (prog1
8569         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8570       (gnus-summary-limit-include-matching-articles
8571        "subject"
8572        (regexp-quote (gnus-simplify-subject-re
8573                       (mail-header-subject (gnus-id-to-header id)))))
8574       (gnus-summary-position-point))))
8575
8576 (defun gnus-summary-limit-include-matching-articles (header regexp)
8577   "Display all the hidden articles that have HEADERs that match REGEXP."
8578   (interactive (list (read-string "Match on header: ")
8579                      (read-string "Regexp: ")))
8580   (let ((articles (gnus-find-matching-articles header regexp)))
8581     (prog1
8582         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8583       (gnus-summary-position-point))))
8584
8585 (defun gnus-summary-insert-dormant-articles ()
8586   "Insert all the dormant articles for this group into the current buffer."
8587   (interactive)
8588   (let ((gnus-verbose (max 6 gnus-verbose)))
8589     (if (not gnus-newsgroup-dormant)
8590         (gnus-message 3 "No dormant articles for this group")
8591       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8592
8593 (defun gnus-summary-insert-ticked-articles ()
8594   "Insert ticked articles for this group into the current buffer."
8595   (interactive)
8596   (let ((gnus-verbose (max 6 gnus-verbose)))
8597     (if (not gnus-newsgroup-marked)
8598         (gnus-message 3 "No ticked articles for this group")
8599       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8600
8601 (defun gnus-summary-limit-include-dormant ()
8602   "Display all the hidden articles that are marked as dormant.
8603 Note that this command only works on a subset of the articles currently
8604 fetched for this group."
8605   (interactive)
8606   (unless gnus-newsgroup-dormant
8607     (error "There are no dormant articles in this group"))
8608   (prog1
8609       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8610     (gnus-summary-position-point)))
8611
8612 (defun gnus-summary-include-articles (articles)
8613   "Fetch the headers for ARTICLES and then display the summary lines."
8614   (let ((gnus-inhibit-demon t)
8615         (gnus-agent nil)
8616         (gnus-read-all-available-headers t))
8617     (setq gnus-newsgroup-headers
8618           (gnus-merge
8619            'list gnus-newsgroup-headers
8620            (gnus-fetch-headers articles nil t)
8621            'gnus-article-sort-by-number))
8622     (setq gnus-newsgroup-articles
8623           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8624     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8625
8626 (defun gnus-summary-limit-exclude-dormant ()
8627   "Hide all dormant articles."
8628   (interactive)
8629   (prog1
8630       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8631     (gnus-summary-position-point)))
8632
8633 (defun gnus-summary-limit-exclude-childless-dormant ()
8634   "Hide all dormant articles that have no children."
8635   (interactive)
8636   (let ((data (gnus-data-list t))
8637         articles d children)
8638     ;; Find all articles that are either not dormant or have
8639     ;; children.
8640     (while (setq d (pop data))
8641       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8642                 (and (setq children
8643                            (gnus-article-children (gnus-data-number d)))
8644                      (let (found)
8645                        (while children
8646                          (when (memq (car children) articles)
8647                            (setq children nil
8648                                  found t))
8649                          (pop children))
8650                        found)))
8651         (push (gnus-data-number d) articles)))
8652     ;; Do the limiting.
8653     (prog1
8654         (gnus-summary-limit articles)
8655       (gnus-summary-position-point))))
8656
8657 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8658   "Mark all unread excluded articles as read.
8659 If ALL, mark even excluded ticked and dormants as read."
8660   (interactive "P")
8661   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8662   (let ((articles (gnus-sorted-ndifference
8663                    (sort
8664                     (mapcar (lambda (h) (mail-header-number h))
8665                             gnus-newsgroup-headers)
8666                     '<)
8667                    gnus-newsgroup-limit))
8668         article)
8669     (setq gnus-newsgroup-unreads
8670           (gnus-sorted-intersection gnus-newsgroup-unreads
8671                                     gnus-newsgroup-limit))
8672     (if all
8673         (setq gnus-newsgroup-dormant nil
8674               gnus-newsgroup-marked nil
8675               gnus-newsgroup-reads
8676               (nconc
8677                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8678                gnus-newsgroup-reads))
8679       (while (setq article (pop articles))
8680         (unless (or (memq article gnus-newsgroup-dormant)
8681                     (memq article gnus-newsgroup-marked))
8682           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8683
8684 (defun gnus-summary-limit (articles &optional pop)
8685   (if pop
8686       ;; We pop the previous limit off the stack and use that.
8687       (setq articles (car gnus-newsgroup-limits)
8688             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8689     ;; We use the new limit, so we push the old limit on the stack.
8690     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8691   ;; Set the limit.
8692   (setq gnus-newsgroup-limit articles)
8693   (let ((total (length gnus-newsgroup-data))
8694         (data (gnus-data-find-list (gnus-summary-article-number)))
8695         (gnus-summary-mark-below nil)   ; Inhibit this.
8696         found)
8697     ;; This will do all the work of generating the new summary buffer
8698     ;; according to the new limit.
8699     (gnus-summary-prepare)
8700     ;; Hide any threads, possibly.
8701     (gnus-summary-maybe-hide-threads)
8702     ;; Try to return to the article you were at, or one in the
8703     ;; neighborhood.
8704     (when data
8705       ;; We try to find some article after the current one.
8706       (while data
8707         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8708           (setq data nil
8709                 found t))
8710         (setq data (cdr data))))
8711     (unless found
8712       ;; If there is no data, that means that we were after the last
8713       ;; article.  The same goes when we can't find any articles
8714       ;; after the current one.
8715       (goto-char (point-max))
8716       (gnus-summary-find-prev))
8717     (gnus-set-mode-line 'summary)
8718     ;; We return how many articles were removed from the summary
8719     ;; buffer as a result of the new limit.
8720     (- total (length gnus-newsgroup-data))))
8721
8722 (defsubst gnus-invisible-cut-children (threads)
8723   (let ((num 0))
8724     (while threads
8725       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8726         (incf num))
8727       (pop threads))
8728     (< num 2)))
8729
8730 (defsubst gnus-cut-thread (thread)
8731   "Go forwards in the thread until we find an article that we want to display."
8732   (when (or (eq gnus-fetch-old-headers 'some)
8733             (eq gnus-fetch-old-headers 'invisible)
8734             (numberp gnus-fetch-old-headers)
8735             (eq gnus-build-sparse-threads 'some)
8736             (eq gnus-build-sparse-threads 'more))
8737     ;; Deal with old-fetched headers and sparse threads.
8738     (while (and
8739             thread
8740             (or
8741              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8742              (gnus-summary-article-ancient-p
8743               (mail-header-number (car thread))))
8744             (if (or (<= (length (cdr thread)) 1)
8745                     (eq gnus-fetch-old-headers 'invisible))
8746                 (setq gnus-newsgroup-limit
8747                       (delq (mail-header-number (car thread))
8748                             gnus-newsgroup-limit)
8749                       thread (cadr thread))
8750               (when (gnus-invisible-cut-children (cdr thread))
8751                 (let ((th (cdr thread)))
8752                   (while th
8753                     (if (memq (mail-header-number (caar th))
8754                               gnus-newsgroup-limit)
8755                         (setq thread (car th)
8756                               th nil)
8757                       (setq th (cdr th))))))))))
8758   thread)
8759
8760 (defun gnus-cut-threads (threads)
8761   "Cut off all uninteresting articles from the beginning of THREADS."
8762   (when (or (eq gnus-fetch-old-headers 'some)
8763             (eq gnus-fetch-old-headers 'invisible)
8764             (numberp gnus-fetch-old-headers)
8765             (eq gnus-build-sparse-threads 'some)
8766             (eq gnus-build-sparse-threads 'more))
8767     (let ((th threads))
8768       (while th
8769         (setcar th (gnus-cut-thread (car th)))
8770         (setq th (cdr th)))))
8771   ;; Remove nixed out threads.
8772   (delq nil threads))
8773
8774 (defun gnus-summary-initial-limit (&optional show-if-empty)
8775   "Figure out what the initial limit is supposed to be on group entry.
8776 This entails weeding out unwanted dormants, low-scored articles,
8777 fetch-old-headers verbiage, and so on."
8778   ;; Most groups have nothing to remove.
8779   (unless (or gnus-inhibit-limiting
8780               (and (null gnus-newsgroup-dormant)
8781                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8782                    (not (eq gnus-fetch-old-headers 'some))
8783                    (not (numberp gnus-fetch-old-headers))
8784                    (not (eq gnus-fetch-old-headers 'invisible))
8785                    (null gnus-summary-expunge-below)
8786                    (not (eq gnus-build-sparse-threads 'some))
8787                    (not (eq gnus-build-sparse-threads 'more))
8788                    (null gnus-thread-expunge-below)))
8789     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8790     (setq gnus-newsgroup-limit nil)
8791     (mapatoms
8792      (lambda (node)
8793        (unless (car (symbol-value node))
8794          ;; These threads have no parents -- they are roots.
8795          (let ((nodes (cdr (symbol-value node)))
8796                thread)
8797            (while nodes
8798              (if (and gnus-thread-expunge-below
8799                       (< (gnus-thread-total-score (car nodes))
8800                          gnus-thread-expunge-below))
8801                  (gnus-expunge-thread (pop nodes))
8802                (setq thread (pop nodes))
8803                (gnus-summary-limit-children thread))))))
8804      gnus-newsgroup-dependencies)
8805     ;; If this limitation resulted in an empty group, we might
8806     ;; pop the previous limit and use it instead.
8807     (when (and (not gnus-newsgroup-limit)
8808                show-if-empty)
8809       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8810     gnus-newsgroup-limit))
8811
8812 (defun gnus-summary-limit-children (thread)
8813   "Return 1 if this subthread is visible and 0 if it is not."
8814   ;; First we get the number of visible children to this thread.  This
8815   ;; is done by recursing down the thread using this function, so this
8816   ;; will really go down to a leaf article first, before slowly
8817   ;; working its way up towards the root.
8818   (when thread
8819     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8820            (children
8821            (if (cdr thread)
8822                (apply '+ (mapcar 'gnus-summary-limit-children
8823                                  (cdr thread)))
8824              0))
8825            (number (mail-header-number (car thread)))
8826            score)
8827       (if (and
8828            (not (memq number gnus-newsgroup-marked))
8829            (or
8830             ;; If this article is dormant and has absolutely no visible
8831             ;; children, then this article isn't visible.
8832             (and (memq number gnus-newsgroup-dormant)
8833                  (zerop children))
8834             ;; If this is "fetch-old-headered" and there is no
8835             ;; visible children, then we don't want this article.
8836             (and (or (eq gnus-fetch-old-headers 'some)
8837                      (numberp gnus-fetch-old-headers))
8838                  (gnus-summary-article-ancient-p number)
8839                  (zerop children))
8840             ;; If this is "fetch-old-headered" and `invisible', then
8841             ;; we don't want this article.
8842             (and (eq gnus-fetch-old-headers 'invisible)
8843                  (gnus-summary-article-ancient-p number))
8844             ;; If this is a sparsely inserted article with no children,
8845             ;; we don't want it.
8846             (and (eq gnus-build-sparse-threads 'some)
8847                  (gnus-summary-article-sparse-p number)
8848                  (zerop children))
8849             ;; If we use expunging, and this article is really
8850             ;; low-scored, then we don't want this article.
8851             (when (and gnus-summary-expunge-below
8852                        (< (setq score
8853                                 (or (cdr (assq number gnus-newsgroup-scored))
8854                                     gnus-summary-default-score))
8855                           gnus-summary-expunge-below))
8856               ;; We increase the expunge-tally here, but that has
8857               ;; nothing to do with the limits, really.
8858               (incf gnus-newsgroup-expunged-tally)
8859               ;; We also mark as read here, if that's wanted.
8860               (when (and gnus-summary-mark-below
8861                          (< score gnus-summary-mark-below))
8862                 (setq gnus-newsgroup-unreads
8863                       (delq number gnus-newsgroup-unreads))
8864                 (if gnus-newsgroup-auto-expire
8865                     (push number gnus-newsgroup-expirable)
8866                   (push (cons number gnus-low-score-mark)
8867                         gnus-newsgroup-reads)))
8868               t)
8869             ;; Do the `display' group parameter.
8870             (and gnus-newsgroup-display
8871                  (let ((gnus-number number))
8872                    (not (funcall gnus-newsgroup-display))))))
8873           ;; Nope, invisible article.
8874           0
8875         ;; Ok, this article is to be visible, so we add it to the limit
8876         ;; and return 1.
8877         (push number gnus-newsgroup-limit)
8878         1))))
8879
8880 (defun gnus-expunge-thread (thread)
8881   "Mark all articles in THREAD as read."
8882   (let* ((number (mail-header-number (car thread))))
8883     (incf gnus-newsgroup-expunged-tally)
8884     ;; We also mark as read here, if that's wanted.
8885     (setq gnus-newsgroup-unreads
8886           (delq number gnus-newsgroup-unreads))
8887     (if gnus-newsgroup-auto-expire
8888         (push number gnus-newsgroup-expirable)
8889       (push (cons number gnus-low-score-mark)
8890             gnus-newsgroup-reads)))
8891   ;; Go recursively through all subthreads.
8892   (mapcar 'gnus-expunge-thread (cdr thread)))
8893
8894 ;; Summary article oriented commands
8895
8896 (defun gnus-summary-refer-parent-article (n)
8897   "Refer parent article N times.
8898 If N is negative, go to ancestor -N instead.
8899 The difference between N and the number of articles fetched is returned."
8900   (interactive "p")
8901   (let ((skip 1)
8902         error header ref)
8903     (when (not (natnump n))
8904       (setq skip (abs n)
8905             n 1))
8906     (while (and (> n 0)
8907                 (not error))
8908       (setq header (gnus-summary-article-header))
8909       (if (and (eq (mail-header-number header)
8910                    (cdr gnus-article-current))
8911                (equal gnus-newsgroup-name
8912                       (car gnus-article-current)))
8913           ;; If we try to find the parent of the currently
8914           ;; displayed article, then we take a look at the actual
8915           ;; References header, since this is slightly more
8916           ;; reliable than the References field we got from the
8917           ;; server.
8918           (with-current-buffer gnus-original-article-buffer
8919             (nnheader-narrow-to-headers)
8920             (unless (setq ref (message-fetch-field "references"))
8921               (when (setq ref (message-fetch-field "in-reply-to"))
8922                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8923             (widen))
8924         (setq ref
8925               ;; It's not the current article, so we take a bet on
8926               ;; the value we got from the server.
8927               (mail-header-references header)))
8928       (if (and ref
8929                (not (equal ref "")))
8930           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8931             (gnus-message 1 "Couldn't find parent"))
8932         (gnus-message 1 "No references in article %d"
8933                       (gnus-summary-article-number))
8934         (setq error t))
8935       (decf n))
8936     (gnus-summary-position-point)
8937     n))
8938
8939 (defun gnus-summary-refer-references ()
8940   "Fetch all articles mentioned in the References header.
8941 Return the number of articles fetched."
8942   (interactive)
8943   (let ((ref (mail-header-references (gnus-summary-article-header)))
8944         (current (gnus-summary-article-number))
8945         (n 0))
8946     (if (or (not ref)
8947             (equal ref ""))
8948         (error "No References in the current article")
8949       ;; For each Message-ID in the References header...
8950       (while (string-match "<[^>]*>" ref)
8951         (incf n)
8952         ;; ... fetch that article.
8953         (gnus-summary-refer-article
8954          (prog1 (match-string 0 ref)
8955            (setq ref (substring ref (match-end 0))))))
8956       (gnus-summary-goto-subject current)
8957       (gnus-summary-position-point)
8958       n)))
8959
8960 (defun gnus-delete-duplicate-headers (headers)
8961   ;; First remove leading duplicates.
8962   (while (and (> (length headers) 1)
8963               (= (mail-header-number (car headers))
8964                  (mail-header-number (cadr headers))))
8965     (pop headers))
8966   ;; Then the rest.
8967   (let ((result headers))
8968     (while (> (length headers) 1)
8969       (if (= (mail-header-number (car headers))
8970              (mail-header-number (cadr headers)))
8971           (setcdr headers (cddr headers))
8972         (pop headers)))
8973     result))
8974
8975 (defun gnus-summary-refer-thread (&optional limit)
8976   "Fetch all articles in the current thread. For backends that
8977 know how to search for threads (currently only 'nnimap) a
8978 non-numeric prefix arg will use nnir to search the entire
8979 server; without a prefix arg only the current group is
8980 searched. If the variable `gnus-refer-thread-use-nnir' is
8981 non-nil the prefix arg has the reverse meaning. If no
8982 backend-specific 'request-thread function is available fetch
8983 LIMIT (the numerical prefix) old headers. If LIMIT is
8984 non-numeric or nil fetch the number specified by the
8985 `gnus-refer-thread-limit' variable."
8986   (interactive "P")
8987   (gnus-warp-to-article)
8988   (let* ((header (gnus-summary-article-header))
8989          (id (mail-header-id header))
8990          (gnus-inhibit-demon t)
8991          (gnus-summary-ignore-duplicates t)
8992          (gnus-read-all-available-headers t)
8993          (gnus-refer-thread-use-nnir
8994           (if (and (not (null limit)) (listp limit))
8995               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
8996          (new-headers
8997           (if (gnus-check-backend-function
8998                'request-thread gnus-newsgroup-name)
8999               (gnus-request-thread header gnus-newsgroup-name)
9000             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9001                             gnus-refer-thread-limit))
9002                    (last (if (numberp limit)
9003                              (min (+ (mail-header-number header)
9004                                      limit)
9005                                   gnus-newsgroup-highest)
9006                            gnus-newsgroup-highest))
9007                    (subject (gnus-simplify-subject
9008                              (mail-header-subject header)))
9009                    (refs (split-string (or (mail-header-references header)
9010                                            "")))
9011                    (gnus-parse-headers-hook
9012                     `(lambda () (goto-char (point-min))
9013                       (keep-lines
9014                        (regexp-opt ',(append refs (list id subject)))))))
9015               (gnus-fetch-headers (list last) (if (numberp limit)
9016                                                   (* 2 limit) limit) t))))
9017          article-ids)
9018     (when (listp new-headers)
9019       (dolist (header new-headers)
9020         (push (mail-header-number header) article-ids)
9021         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9022           (push (mail-header-number header) gnus-newsgroup-unreads)
9023           (setq gnus-newsgroup-unselected
9024                 (delete (mail-header-number header)
9025                         gnus-newsgroup-unselected))))
9026       (setq gnus-newsgroup-headers
9027             (gnus-delete-duplicate-headers
9028              (gnus-merge
9029               'list gnus-newsgroup-headers new-headers
9030               'gnus-article-sort-by-number)))
9031       (setq gnus-newsgroup-articles
9032             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9033       (gnus-summary-limit-include-thread id)))
9034   (gnus-summary-show-thread))
9035
9036 (defun gnus-summary-refer-article (message-id)
9037   "Fetch an article specified by MESSAGE-ID."
9038   (interactive "sMessage-ID: ")
9039   (gnus-warp-to-article)
9040   (when (and (stringp message-id)
9041              (not (zerop (length message-id))))
9042     (setq message-id (gnus-replace-in-string message-id " " ""))
9043     ;; Construct the correct Message-ID if necessary.
9044     ;; Suggested by tale@pawl.rpi.edu.
9045     (unless (string-match "^<" message-id)
9046       (setq message-id (concat "<" message-id)))
9047     (unless (string-match ">$" message-id)
9048       (setq message-id (concat message-id ">")))
9049     ;; People often post MIDs from URLs, so unhex it:
9050     (unless (string-match "@" message-id)
9051       (setq message-id (gnus-url-unhex-string message-id)))
9052     (let* ((header (gnus-id-to-header message-id))
9053            (sparse (and header
9054                         (gnus-summary-article-sparse-p
9055                          (mail-header-number header))
9056                         (memq (mail-header-number header)
9057                               gnus-newsgroup-limit)))
9058            number)
9059       (cond
9060        ;; If the article is present in the buffer we just go to it.
9061        ((and header
9062              (or (not (gnus-summary-article-sparse-p
9063                        (mail-header-number header)))
9064                  sparse))
9065         (prog1
9066             (gnus-summary-goto-article
9067              (mail-header-number header) nil t)
9068           (when sparse
9069             (gnus-summary-update-article (mail-header-number header)))))
9070        (t
9071         ;; We fetch the article.
9072         (catch 'found
9073           (dolist (gnus-override-method (gnus-refer-article-methods))
9074             (when (and (gnus-check-server gnus-override-method)
9075                        ;; Fetch the header,
9076                        (setq number (gnus-summary-insert-subject message-id)))
9077               ;; and display the article.
9078               (gnus-summary-select-article nil nil nil number)
9079               (throw 'found t)))
9080           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9081
9082 (defun gnus-refer-article-methods ()
9083   "Return a list of referable methods."
9084   (cond
9085    ;; No method, so we default to current and native.
9086    ((null gnus-refer-article-method)
9087     (list gnus-current-select-method gnus-select-method))
9088    ;; Current.
9089    ((eq 'current gnus-refer-article-method)
9090     (list gnus-current-select-method))
9091    ;; List of select methods.
9092    ((not (and (symbolp (car gnus-refer-article-method))
9093               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9094     (let (out)
9095       (dolist (method gnus-refer-article-method)
9096         (push (if (eq 'current method)
9097                   gnus-current-select-method
9098                 (if (eq 'nnir (car method))
9099                     (list
9100                      'nnir
9101                      (or (cadr method)
9102                          (gnus-method-to-server gnus-current-select-method)))
9103                   method))
9104               out))
9105       (nreverse out)))
9106    ;; One single select method.
9107    (t
9108     (list gnus-refer-article-method))))
9109
9110 (defun gnus-summary-edit-parameters ()
9111   "Edit the group parameters of the current group."
9112   (interactive)
9113   (gnus-group-edit-group gnus-newsgroup-name 'params))
9114
9115 (defun gnus-summary-customize-parameters ()
9116   "Customize the group parameters of the current group."
9117   (interactive)
9118   (gnus-group-customize gnus-newsgroup-name))
9119
9120 (defun gnus-summary-enter-digest-group (&optional force)
9121   "Enter an nndoc group based on the current article.
9122 If FORCE, force a digest interpretation.  If not, try to guess
9123 what the document format is.
9124
9125 To control what happens when you exit the group, see the
9126 `gnus-auto-select-on-ephemeral-exit' variable."
9127   (interactive "P")
9128   (let ((conf gnus-current-window-configuration))
9129     (save-window-excursion
9130       (save-excursion
9131         (let (gnus-article-prepare-hook
9132               gnus-display-mime-function
9133               gnus-break-pages)
9134           (gnus-summary-select-article))))
9135     (setq gnus-current-window-configuration conf)
9136     (let* ((name (format "%s-%d"
9137                          (gnus-group-prefixed-name
9138                           gnus-newsgroup-name (list 'nndoc ""))
9139                          (with-current-buffer gnus-summary-buffer
9140                            gnus-current-article)))
9141            (ogroup gnus-newsgroup-name)
9142            (params (append (gnus-info-params (gnus-get-info ogroup))
9143                            (list (cons 'to-group ogroup))
9144                            (list (cons 'parent-group ogroup))
9145                            (list (cons 'save-article-group ogroup))))
9146            (case-fold-search t)
9147            (buf (current-buffer))
9148            dig to-address charset)
9149       (with-current-buffer gnus-original-article-buffer
9150         ;; Have the digest group inherit the main mail address of
9151         ;; the parent article.
9152         (when (setq to-address (or (gnus-fetch-field "reply-to")
9153                                    (gnus-fetch-field "from")))
9154           (setq params
9155                 (append
9156                  (list (cons 'to-address
9157                              (funcall gnus-decode-encoded-address-function
9158                                       to-address))))))
9159         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9160         (insert-buffer-substring gnus-original-article-buffer)
9161         (narrow-to-region
9162          (goto-char (point-min))
9163          (or (search-forward "\n\n" nil t) (point)))
9164         ;; Remove lines that may lead nndoc to misinterpret the
9165         ;; document type.
9166         (goto-char (point-min))
9167         (delete-matching-lines "^Path:\\|^From ")
9168         ;; Parse charset, and decode content transfer encoding.
9169         (setq charset (mail-content-type-get
9170                        (mail-header-parse-content-type
9171                         (or (gnus-fetch-field "content-type") ""))
9172                        'charset))
9173         (let ((encoding (gnus-fetch-field "content-transfer-encoding")))
9174           (when encoding
9175             (message-remove-header "content-transfer-encoding")
9176             (goto-char (point-max))
9177             (widen)
9178             (narrow-to-region (point) (point-max))
9179             (mm-decode-content-transfer-encoding
9180              (intern (downcase (mail-header-strip encoding))))))
9181         (widen))
9182       (unwind-protect
9183           (if (let ((gnus-newsgroup-ephemeral-charset
9184                      (if charset
9185                          (intern (downcase (gnus-strip-whitespace charset)))
9186                        gnus-newsgroup-charset))
9187                     (gnus-newsgroup-ephemeral-ignored-charsets
9188                      gnus-newsgroup-ignored-charsets))
9189                 (gnus-group-read-ephemeral-group
9190                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9191                               (nndoc-article-type
9192                                ,(if force 'mbox 'guess)))
9193                  t nil nil nil
9194                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9195                                                         "ADAPT")))))
9196               ;; Make all postings to this group go to the parent group.
9197               (nconc (gnus-info-params (gnus-get-info name))
9198                      params)
9199             ;; Couldn't select this doc group.
9200             (switch-to-buffer buf)
9201             (gnus-set-global-variables)
9202             (gnus-configure-windows 'summary)
9203             (gnus-message 3 "Article couldn't be entered?"))
9204         (kill-buffer dig)))))
9205
9206 (defun gnus-summary-read-document (n)
9207   "Open a new group based on the current article(s).
9208 This will allow you to read digests and other similar
9209 documents as newsgroups.
9210 Obeys the standard process/prefix convention."
9211   (interactive "P")
9212   (let* ((ogroup gnus-newsgroup-name)
9213          (params (append (gnus-info-params (gnus-get-info ogroup))
9214                          (list (cons 'to-group ogroup))))
9215          group egroup groups vgroup)
9216     (dolist (article (gnus-summary-work-articles n))
9217       (setq group (format "%s-%d" gnus-newsgroup-name article))
9218       (gnus-summary-remove-process-mark article)
9219       (when (gnus-summary-display-article article)
9220         (save-excursion ;;What for?
9221           (with-temp-buffer
9222             (insert-buffer-substring gnus-original-article-buffer)
9223             ;; Remove some headers that may lead nndoc to make
9224             ;; the wrong guess.
9225             (message-narrow-to-head)
9226             (goto-char (point-min))
9227             (delete-matching-lines "^Path:\\|^From ")
9228             (widen)
9229             (if (setq egroup
9230                       (gnus-group-read-ephemeral-group
9231                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9232                                      (nndoc-article-type guess))
9233                        t nil t))
9234                 (progn
9235                   ;; Make all postings to this group go to the parent group.
9236                   (nconc (gnus-info-params (gnus-get-info egroup))
9237                          params)
9238                   (push egroup groups))
9239               ;; Couldn't select this doc group.
9240               (gnus-error 3 "Article couldn't be entered"))))))
9241     ;; Now we have selected all the documents.
9242     (cond
9243      ((not groups)
9244       (error "None of the articles could be interpreted as documents"))
9245      ((gnus-group-read-ephemeral-group
9246        (setq vgroup (format
9247                      "nnvirtual:%s-%s" gnus-newsgroup-name
9248                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9249        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9250        t
9251        (cons (current-buffer) 'summary)))
9252      (t
9253       (error "Couldn't select virtual nndoc group")))))
9254
9255 (defun gnus-summary-widget-forward (arg)
9256   "Move point to the next field or button in the article.
9257 With optional ARG, move across that many fields."
9258   (interactive "p")
9259   (gnus-summary-select-article)
9260   (gnus-configure-windows 'article)
9261   (select-window (gnus-get-buffer-window gnus-article-buffer))
9262   (widget-forward arg))
9263
9264 (defun gnus-summary-widget-backward (arg)
9265   "Move point to the previous field or button in the article.
9266 With optional ARG, move across that many fields."
9267   (interactive "p")
9268   (gnus-summary-select-article)
9269   (gnus-configure-windows 'article)
9270   (select-window (gnus-get-buffer-window gnus-article-buffer))
9271   (unless (widget-at (point))
9272     (goto-char (point-max)))
9273   (widget-backward arg))
9274
9275 (defun gnus-summary-isearch-article (&optional regexp-p)
9276   "Do incremental search forward on the current article.
9277 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9278   (interactive "P")
9279   (gnus-summary-select-article)
9280   (gnus-configure-windows 'article)
9281   (gnus-eval-in-buffer-window gnus-article-buffer
9282     (save-restriction
9283       (widen)
9284       (isearch-forward regexp-p))))
9285
9286 (defun gnus-summary-repeat-search-article-forward ()
9287   "Repeat the previous search forwards."
9288   (interactive)
9289   (unless gnus-last-search-regexp
9290     (error "No previous search"))
9291   (gnus-summary-search-article-forward gnus-last-search-regexp))
9292
9293 (defun gnus-summary-repeat-search-article-backward ()
9294   "Repeat the previous search backwards."
9295   (interactive)
9296   (unless gnus-last-search-regexp
9297     (error "No previous search"))
9298   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9299
9300 (defun gnus-summary-search-article-forward (regexp &optional backward)
9301   "Search for an article containing REGEXP forward.
9302 If BACKWARD, search backward instead."
9303   (interactive
9304    (list (read-string
9305           (format "Search article %s (regexp%s): "
9306                   (if current-prefix-arg "backward" "forward")
9307                   (if gnus-last-search-regexp
9308                       (concat ", default " gnus-last-search-regexp)
9309                     "")))
9310          current-prefix-arg))
9311   (if (string-equal regexp "")
9312       (setq regexp (or gnus-last-search-regexp ""))
9313     (setq gnus-last-search-regexp regexp)
9314     (setq gnus-article-before-search gnus-current-article))
9315   ;; Intentionally set gnus-last-article.
9316   (setq gnus-last-article gnus-article-before-search)
9317   (let ((gnus-last-article gnus-last-article))
9318     (if (gnus-summary-search-article regexp backward)
9319         (gnus-summary-show-thread)
9320       (signal 'search-failed (list regexp)))))
9321
9322 (defun gnus-summary-search-article-backward (regexp)
9323   "Search for an article containing REGEXP backward."
9324   (interactive
9325    (list (read-string
9326           (format "Search article backward (regexp%s): "
9327                   (if gnus-last-search-regexp
9328                       (concat ", default " gnus-last-search-regexp)
9329                     "")))))
9330   (gnus-summary-search-article-forward regexp 'backward))
9331
9332 (defun gnus-summary-search-article (regexp &optional backward)
9333   "Search for an article containing REGEXP.
9334 Optional argument BACKWARD means do search for backward.
9335 `gnus-select-article-hook' is not called during the search."
9336   ;; We have to require this here to make sure that the following
9337   ;; dynamic binding isn't shadowed by autoloading.
9338   (require 'gnus-async)
9339   (require 'gnus-art)
9340   (let ((gnus-select-article-hook nil)  ;Disable hook.
9341         (gnus-article-prepare-hook nil)
9342         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9343         (gnus-use-article-prefetch nil)
9344         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9345         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9346         (gnus-visual nil)
9347         (gnus-keep-backlog nil)
9348         (gnus-break-pages nil)
9349         (gnus-summary-display-arrow nil)
9350         (gnus-updated-mode-lines nil)
9351         (gnus-auto-center-summary nil)
9352         (sum (current-buffer))
9353         (gnus-display-mime-function nil)
9354         (found nil)
9355         point)
9356     (gnus-save-hidden-threads
9357       (gnus-summary-select-article)
9358       (set-buffer gnus-article-buffer)
9359       (goto-char (window-point (get-buffer-window (current-buffer))))
9360       (when backward
9361         (forward-line -1))
9362       (while (not found)
9363         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9364         (if (if backward
9365                 (re-search-backward regexp nil t)
9366               (re-search-forward regexp nil t))
9367             ;; We found the regexp.
9368             (progn
9369               (setq found 'found)
9370               (beginning-of-line)
9371               (set-window-start
9372                (get-buffer-window (current-buffer))
9373                (point))
9374               (forward-line 1)
9375               (set-window-point
9376                (get-buffer-window (current-buffer))
9377                (point))
9378               (set-buffer sum)
9379               (setq point (point)))
9380           ;; We didn't find it, so we go to the next article.
9381           (set-buffer sum)
9382           (setq found 'not)
9383           (while (eq found 'not)
9384             (if (not (if backward (gnus-summary-find-prev)
9385                        (gnus-summary-find-next)))
9386                 ;; No more articles.
9387                 (setq found t)
9388               ;; Select the next article and adjust point.
9389               (unless (gnus-summary-article-sparse-p
9390                        (gnus-summary-article-number))
9391                 (setq found nil)
9392                 (gnus-summary-select-article)
9393                 (set-buffer gnus-article-buffer)
9394                 (widen)
9395                 (goto-char (if backward (point-max) (point-min))))))))
9396       (gnus-message 7 ""))
9397     ;; Return whether we found the regexp.
9398     (when (eq found 'found)
9399       (goto-char point)
9400       (gnus-summary-show-thread)
9401       (gnus-summary-goto-subject gnus-current-article)
9402       (gnus-summary-position-point)
9403       t)))
9404
9405 (defun gnus-find-matching-articles (header regexp)
9406   "Return a list of all articles that match REGEXP on HEADER.
9407 This search includes all articles in the current group that Gnus has
9408 fetched headers for, whether they are displayed or not."
9409   (let ((articles nil)
9410         ;; Can't eta-reduce because it's a macro.
9411         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9412         (case-fold-search t))
9413     (dolist (header gnus-newsgroup-headers)
9414       (when (string-match regexp (funcall func header))
9415         (push (mail-header-number header) articles)))
9416     (nreverse articles)))
9417
9418 (defun gnus-summary-find-matching (header regexp &optional backward unread
9419                                           not-case-fold not-matching)
9420   "Return a list of all articles that match REGEXP on HEADER.
9421 The search stars on the current article and goes forwards unless
9422 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9423 If UNREAD is non-nil, only unread articles will
9424 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9425 in the comparisons. If NOT-MATCHING, return a list of all articles that
9426 not match REGEXP on HEADER."
9427   (let ((case-fold-search (not not-case-fold))
9428         articles d func)
9429     (if (consp header)
9430         (if (eq (car header) 'extra)
9431             (setq func
9432                   `(lambda (h)
9433                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9434                          "")))
9435           (error "%s is an invalid header" header))
9436       (unless (fboundp (intern (concat "mail-header-" header)))
9437         (error "%s is not a valid header" header))
9438       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9439     (dolist (d (if (eq backward 'all)
9440                    gnus-newsgroup-data
9441                  (gnus-data-find-list
9442                   (gnus-summary-article-number)
9443                   (gnus-data-list backward))))
9444       (when (and (or (not unread)       ; We want all articles...
9445                      (gnus-data-unread-p d)) ; Or just unreads.
9446                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9447                  (if not-matching
9448                      (not (string-match
9449                            regexp
9450                            (funcall func (gnus-data-header d))))
9451                    (string-match regexp
9452                                  (funcall func (gnus-data-header d)))))
9453         (push (gnus-data-number d) articles))) ; Success!
9454     (nreverse articles)))
9455
9456 (defun gnus-summary-execute-command (header regexp command &optional backward)
9457   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9458 If HEADER is an empty string (or nil), the match is done on the entire
9459 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9460   (interactive
9461    (list (let ((completion-ignore-case t))
9462            (gnus-completing-read
9463             "Header name"
9464             (mapcar 'symbol-name
9465                     (append
9466                      '(Number Subject From Lines Date
9467                        Message-ID Xref References Body)
9468                      gnus-extra-headers))
9469             'require-match))
9470          (read-string "Regexp: ")
9471          (read-key-sequence "Command: ")
9472          current-prefix-arg))
9473   (when (equal header "Body")
9474     (setq header ""))
9475   ;; Hidden thread subtrees must be searched as well.
9476   (gnus-summary-show-all-threads)
9477   ;; We don't want to change current point nor window configuration.
9478   (save-excursion
9479     (save-window-excursion
9480       (let (gnus-visual
9481             gnus-treat-strip-trailing-blank-lines
9482             gnus-treat-strip-leading-blank-lines
9483             gnus-treat-strip-multiple-blank-lines
9484             gnus-treat-hide-boring-headers
9485             gnus-treat-fold-newsgroups
9486             gnus-article-prepare-hook)
9487         (gnus-message 6 "Executing %s..." (key-description command))
9488         ;; We'd like to execute COMMAND interactively so as to give arguments.
9489         (gnus-execute header regexp
9490                       `(call-interactively ',(key-binding command))
9491                       backward)
9492         (gnus-message 6 "Executing %s...done" (key-description command))))))
9493
9494 (defun gnus-summary-beginning-of-article ()
9495   "Scroll the article back to the beginning."
9496   (interactive)
9497   (gnus-summary-select-article)
9498   (gnus-configure-windows 'article)
9499   (gnus-eval-in-buffer-window gnus-article-buffer
9500     (widen)
9501     (goto-char (point-min))
9502     (when gnus-break-pages
9503       (gnus-narrow-to-page))))
9504
9505 (defun gnus-summary-end-of-article ()
9506   "Scroll to the end of the article."
9507   (interactive)
9508   (gnus-summary-select-article)
9509   (gnus-configure-windows 'article)
9510   (gnus-eval-in-buffer-window gnus-article-buffer
9511     (widen)
9512     (goto-char (point-max))
9513     (recenter -3)
9514     (when gnus-break-pages
9515       (gnus-narrow-to-page))))
9516
9517 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9518   "Truncate to LEN and quote all \"(\"'s in STRING."
9519   (gnus-replace-in-string (if (and len (> (length string) len))
9520                               (substring string 0 len)
9521                             string)
9522                           "[()]" "\\\\\\&"))
9523
9524 (defun gnus-summary-print-article (&optional filename n)
9525   "Generate and print a PostScript image of the process-marked (mail) articles.
9526
9527 If used interactively, print the current article if none are
9528 process-marked.  With prefix arg, prompt the user for the name of the
9529 file to save in.
9530
9531 When used from Lisp, accept two optional args FILENAME and N.  N means
9532 to print the next N articles.  If N is negative, print the N previous
9533 articles.  If N is nil and articles have been marked with the process
9534 mark, print these instead.
9535
9536 If the optional first argument FILENAME is nil, send the image to the
9537 printer.  If FILENAME is a string, save the PostScript image in a file with
9538 that name.  If FILENAME is a number, prompt the user for the name of the file
9539 to save in."
9540   (interactive (list (ps-print-preprint current-prefix-arg)))
9541   (dolist (article (gnus-summary-work-articles n))
9542     (gnus-summary-select-article nil nil 'pseudo article)
9543     (gnus-eval-in-buffer-window gnus-article-buffer
9544       (gnus-print-buffer))
9545     (gnus-summary-remove-process-mark article))
9546   (ps-despool filename))
9547
9548 (defun gnus-print-buffer ()
9549   (let ((ps-left-header
9550          (list
9551           (concat "("
9552                   (gnus-summary-print-truncate-and-quote
9553                    (mail-header-subject gnus-current-headers)
9554                    66) ")")
9555           (concat "("
9556                   (gnus-summary-print-truncate-and-quote
9557                    (mail-header-from gnus-current-headers)
9558                    45) ")")))
9559         (ps-right-header
9560          (list
9561           "/pagenumberstring load"
9562           (concat "("
9563                   (mail-header-date gnus-current-headers) ")"))))
9564     (gnus-run-hooks 'gnus-ps-print-hook)
9565     (save-excursion
9566       (if ps-print-color-p
9567           (ps-spool-buffer-with-faces)
9568         (ps-spool-buffer)))))
9569
9570 (defun gnus-summary-show-complete-article ()
9571   "Show a complete version of the current article.
9572 This is only useful if you're looking at a partial version of the
9573 article currently."
9574   (interactive)
9575   (let ((gnus-keep-backlog nil)
9576         (gnus-use-cache nil)
9577         (gnus-agent nil)
9578         (variable (intern
9579                    (format "%s-fetch-partial-articles"
9580                            (car (gnus-find-method-for-group
9581                                  gnus-newsgroup-name)))
9582                    obarray))
9583         old-val)
9584     (unwind-protect
9585         (progn
9586           (setq old-val (symbol-value variable))
9587           (set variable nil)
9588           (gnus-flush-original-article-buffer)
9589           (gnus-summary-show-article))
9590       (set variable old-val))))
9591
9592 (defun gnus-summary-show-article (&optional arg)
9593   "Force redisplaying of the current article.
9594 If ARG (the prefix) is a number, show the article with the charset
9595 defined in `gnus-summary-show-article-charset-alist', or the charset
9596 input.
9597 If ARG (the prefix) is non-nil and not a number, show the article,
9598 but without running any of the article treatment functions
9599 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9600 C-u g', show the raw article."
9601   (interactive "P")
9602   (cond
9603    ((numberp arg)
9604     (gnus-summary-show-article t)
9605     (let ((gnus-newsgroup-charset
9606            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9607                (mm-read-coding-system
9608                 "View as charset: " ;; actually it is coding system.
9609                 (with-current-buffer gnus-article-buffer
9610                   (mm-detect-coding-region (point) (point-max))))))
9611           (gnus-newsgroup-ignored-charsets 'gnus-all))
9612       (gnus-summary-select-article nil 'force)
9613       (let ((deps gnus-newsgroup-dependencies)
9614             head header lines)
9615         (with-current-buffer gnus-original-article-buffer
9616           (save-restriction
9617             (message-narrow-to-head)
9618             (setq head (buffer-string))
9619             (goto-char (point-min))
9620             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9621               (goto-char (point-max))
9622               (widen)
9623               (setq lines (1- (count-lines (point) (point-max))))))
9624           (with-temp-buffer
9625             (insert (format "211 %d Article retrieved.\n"
9626                             (cdr gnus-article-current)))
9627             (insert head)
9628             (if lines (insert (format "Lines: %d\n" lines)))
9629             (insert ".\n")
9630             (let ((nntp-server-buffer (current-buffer)))
9631               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9632         (gnus-data-set-header
9633          (gnus-data-find (cdr gnus-article-current))
9634          header)
9635         (gnus-summary-update-article-line
9636          (cdr gnus-article-current) header)
9637         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9638           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9639    ((not arg)
9640     ;; Select the article the normal way.
9641     (if (eq mm-text-html-renderer 'shr)
9642         (progn
9643           (require 'shr)
9644           (let ((shr-ignore-cache t))
9645             (gnus-summary-select-article nil 'force)))
9646       (gnus-summary-select-article nil 'force)))
9647    ((equal arg '(16))
9648     ;; C-u C-u g
9649     (let ((gnus-inhibit-article-treatments t))
9650       (gnus-summary-select-article nil 'force)))
9651    (t
9652     ;; We have to require this here to make sure that the following
9653     ;; dynamic binding isn't shadowed by autoloading.
9654     (require 'gnus-async)
9655     (require 'gnus-art)
9656     ;; Bind the article treatment functions to nil.
9657     (let ((gnus-have-all-headers t)
9658           gnus-article-prepare-hook
9659           gnus-article-decode-hook
9660           gnus-display-mime-function
9661           gnus-break-pages)
9662       ;; Destroy any MIME parts.
9663       (when (gnus-buffer-live-p gnus-article-buffer)
9664         (with-current-buffer gnus-article-buffer
9665           (gnus-article-stop-animations)
9666           (gnus-stop-downloads)
9667           (mm-destroy-parts gnus-article-mime-handles)
9668           ;; Set it to nil for safety reason.
9669           (setq gnus-article-mime-handle-alist nil)
9670           (setq gnus-article-mime-handles nil)))
9671       (gnus-summary-select-article nil 'force))))
9672   (gnus-summary-goto-subject gnus-current-article)
9673   (gnus-summary-position-point))
9674
9675 (defun gnus-summary-show-raw-article ()
9676   "Show the raw article without any article massaging functions being run."
9677   (interactive)
9678   (gnus-summary-show-article t))
9679
9680 (defun gnus-summary-verbose-headers (&optional arg)
9681   "Toggle permanent full header display.
9682 If ARG is a positive number, turn header display on.
9683 If ARG is a negative number, turn header display off."
9684   (interactive "P")
9685   (setq gnus-show-all-headers
9686         (cond ((or (not (numberp arg))
9687                    (zerop arg))
9688                (not gnus-show-all-headers))
9689               ((natnump arg)
9690                t)))
9691   (gnus-summary-show-article))
9692
9693 (defun gnus-summary-toggle-header (&optional arg)
9694   "Show the headers if they are hidden, or hide them if they are shown.
9695 If ARG is a positive number, show the entire header.
9696 If ARG is a negative number, hide the unwanted header lines."
9697   (interactive "P")
9698   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9699                      (get-buffer-window gnus-article-buffer t))))
9700     (with-current-buffer gnus-article-buffer
9701       (widen)
9702       (article-narrow-to-head)
9703       (let* ((inhibit-read-only t)
9704              (inhibit-point-motion-hooks t)
9705              (hidden (if (numberp arg)
9706                          (>= arg 0)
9707                        (or (not (looking-at "[^ \t\n]+:"))
9708                            (gnus-article-hidden-text-p 'headers))))
9709              s e)
9710         (delete-region (point-min) (point-max))
9711         (with-current-buffer gnus-original-article-buffer
9712           (goto-char (setq s (point-min)))
9713           (setq e (if (search-forward "\n\n" nil t)
9714                       (1- (point))
9715                     (point-max))))
9716         (insert-buffer-substring gnus-original-article-buffer s e)
9717         (run-hooks 'gnus-article-decode-hook)
9718         (if hidden
9719             (let ((gnus-treat-hide-headers nil)
9720                   (gnus-treat-hide-boring-headers nil))
9721               (gnus-delete-wash-type 'headers)
9722               (gnus-treat-article 'head))
9723           (gnus-treat-article 'head))
9724         (widen)
9725         (if window
9726             (set-window-start window (goto-char (point-min))))
9727         (if gnus-break-pages
9728             (gnus-narrow-to-page)
9729           (when (gnus-visual-p 'page-marker)
9730             (let ((inhibit-read-only t))
9731               (gnus-remove-text-with-property 'gnus-prev)
9732               (gnus-remove-text-with-property 'gnus-next))))
9733         (gnus-set-mode-line 'article)))))
9734
9735 (defun gnus-summary-show-all-headers ()
9736   "Make all header lines visible."
9737   (interactive)
9738   (gnus-summary-toggle-header 1))
9739
9740 (defun gnus-summary-caesar-message (&optional arg)
9741   "Caesar rotate the current article by 13.
9742 With a non-numerical prefix, also rotate headers.  A numerical
9743 prefix specifies how many places to rotate each letter forward."
9744   (interactive "P")
9745   (gnus-summary-select-article)
9746   (let ((mail-header-separator ""))
9747     (gnus-eval-in-buffer-window gnus-article-buffer
9748       (save-restriction
9749         (widen)
9750         (let ((start (window-start))
9751               (inhibit-read-only t))
9752           (if (equal arg '(4))
9753               (message-caesar-buffer-body nil t)
9754             (message-caesar-buffer-body arg))
9755           (set-window-start (get-buffer-window (current-buffer)) start)))))
9756   ;; Create buttons and stuff...
9757   (gnus-treat-article nil))
9758
9759 (declare-function idna-to-unicode "ext:idna" (str))
9760
9761 (defun gnus-summary-idna-message (&optional arg)
9762   "Decode IDNA encoded domain names in the current articles.
9763 IDNA encoded domain names looks like `xn--bar'.  If a string
9764 remain unencoded after running this function, it is likely an
9765 invalid IDNA string (`xn--bar' is invalid).
9766
9767 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9768 installed for this command to work."
9769   (interactive "P")
9770   (if (not (and (condition-case nil (require 'idna)
9771                   (file-error))
9772                 (mm-coding-system-p 'utf-8)
9773                 (executable-find (symbol-value 'idna-program))))
9774       (gnus-message
9775        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9776     (gnus-summary-select-article)
9777     (let ((mail-header-separator ""))
9778       (gnus-eval-in-buffer-window gnus-article-buffer
9779         (save-restriction
9780           (widen)
9781           (let ((start (window-start))
9782                 buffer-read-only)
9783             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9784               (replace-match (idna-to-unicode (match-string 1))))
9785             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9786
9787 (defun gnus-summary-morse-message (&optional arg)
9788   "Morse decode the current article."
9789   (interactive "P")
9790   (gnus-summary-select-article)
9791   (let ((mail-header-separator ""))
9792     (gnus-eval-in-buffer-window gnus-article-buffer
9793       (save-excursion
9794         (save-restriction
9795           (widen)
9796           (let ((pos (window-start))
9797                 (inhibit-read-only t))
9798             (goto-char (point-min))
9799             (when (message-goto-body)
9800               (gnus-narrow-to-body))
9801             (goto-char (point-min))
9802             (while (search-forward "·" (point-max) t)
9803               (replace-match "."))
9804             (unmorse-region (point-min) (point-max))
9805             (widen)
9806             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9807
9808 (defun gnus-summary-stop-page-breaking ()
9809   "Stop page breaking in the current article."
9810   (interactive)
9811   (gnus-summary-select-article)
9812   (gnus-eval-in-buffer-window gnus-article-buffer
9813     (widen)
9814     (when (gnus-visual-p 'page-marker)
9815       (let ((inhibit-read-only t))
9816         (gnus-remove-text-with-property 'gnus-prev)
9817         (gnus-remove-text-with-property 'gnus-next))
9818       (setq gnus-page-broken nil))))
9819
9820 (defun gnus-summary-move-article (&optional n to-newsgroup
9821                                             select-method action)
9822   "Move the current article to a different newsgroup.
9823 If N is a positive number, move the N next articles.
9824 If N is a negative number, move the N previous articles.
9825 If N is nil and any articles have been marked with the process mark,
9826 move those articles instead.
9827 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9828 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9829 re-spool using this method.
9830
9831 When called interactively with TO-NEWSGROUP being nil, the value of
9832 the variable `gnus-move-split-methods' is used for finding a default
9833 for the target newsgroup.
9834
9835 For this function to work, both the current newsgroup and the
9836 newsgroup that you want to move to have to support the `request-move'
9837 and `request-accept' functions.
9838
9839 ACTION can be either `move' (the default), `crosspost' or `copy'."
9840   (interactive "P")
9841   (unless action
9842     (setq action 'move))
9843   ;; Check whether the source group supports the required functions.
9844   (cond ((and (eq action 'move)
9845               (not (gnus-check-backend-function
9846                     'request-move-article gnus-newsgroup-name)))
9847          (error "The current group does not support article moving"))
9848         ((and (eq action 'crosspost)
9849               (not (gnus-check-backend-function
9850                     'request-replace-article gnus-newsgroup-name)))
9851          (error "The current group does not support article editing")))
9852   (let ((articles (gnus-summary-work-articles n))
9853         (prefix (if (gnus-check-backend-function
9854                      'request-move-article gnus-newsgroup-name)
9855                     (funcall gnus-move-group-prefix-function
9856                              gnus-newsgroup-name)
9857                   ""))
9858         (names '((move "Move" "Moving")
9859                  (copy "Copy" "Copying")
9860                  (crosspost "Crosspost" "Crossposting")))
9861         (copy-buf (save-excursion
9862                     (nnheader-set-temp-buffer " *copy article*")))
9863         art-group to-method new-xref article to-groups
9864         articles-to-update-marks encoded)
9865     (unless (assq action names)
9866       (error "Unknown action %s" action))
9867     ;; Read the newsgroup name.
9868     (when (and (not to-newsgroup)
9869                (not select-method))
9870       (if (and gnus-move-split-methods
9871                (not
9872                 (and (memq gnus-current-article articles)
9873                      (gnus-buffer-live-p gnus-original-article-buffer))))
9874           ;; When `gnus-move-split-methods' is non-nil, we have to
9875           ;; select an article to give `gnus-read-move-group-name' an
9876           ;; opportunity to suggest an appropriate default.  However,
9877           ;; we needn't render or mark the article.
9878           (let ((gnus-display-mime-function nil)
9879                 (gnus-article-prepare-hook nil)
9880                 (gnus-mark-article-hook nil))
9881             (gnus-summary-select-article nil nil nil (car articles))))
9882       (setq to-newsgroup (gnus-read-move-group-name
9883                           (cadr (assq action names))
9884                           (symbol-value
9885                            (intern (format "gnus-current-%s-group" action)))
9886                           articles prefix)
9887             encoded to-newsgroup
9888             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9889       (set (intern (format "gnus-current-%s-group" action))
9890            (mm-decode-coding-string
9891             to-newsgroup
9892             (gnus-group-name-charset to-method to-newsgroup))))
9893     (unless to-method
9894       (setq to-method (or select-method
9895                           (gnus-server-to-method
9896                            (gnus-group-method to-newsgroup)))))
9897     (setq to-newsgroup
9898           (or encoded
9899               (and to-newsgroup
9900                    (mm-encode-coding-string
9901                     to-newsgroup
9902                     (gnus-group-name-charset to-method to-newsgroup)))))
9903     ;; Check the method we are to move this article to...
9904     (unless (gnus-check-backend-function
9905              'request-accept-article (car to-method))
9906       (error "%s does not support article copying" (car to-method)))
9907     (unless (gnus-check-server to-method)
9908       (error "Can't open server %s" (car to-method)))
9909     (gnus-message 6 "%s to %s: %s..."
9910                   (caddr (assq action names))
9911                   (or (car select-method)
9912                       (gnus-group-decoded-name to-newsgroup))
9913                   articles)
9914     (while articles
9915       (setq article (pop articles))
9916       ;; Set any marks that may have changed in the summary buffer.
9917       (when gnus-preserve-marks
9918         (gnus-summary-push-marks-to-backend article))
9919       (setq
9920        art-group
9921        (cond
9922         ;; Move the article.
9923         ((eq action 'move)
9924          ;; Remove this article from future suppression.
9925          (gnus-dup-unsuppress-article article)
9926          (let* ((from-method (gnus-find-method-for-group
9927                               gnus-newsgroup-name))
9928                 (to-method (or select-method
9929                                (gnus-find-method-for-group to-newsgroup)))
9930                 (move-is-internal (gnus-server-equal from-method to-method)))
9931            (gnus-request-move-article
9932             article                     ; Article to move
9933             gnus-newsgroup-name         ; From newsgroup
9934             (nth 1 (gnus-find-method-for-group
9935                     gnus-newsgroup-name)) ; Server
9936             (list 'gnus-request-accept-article
9937                   to-newsgroup (list 'quote select-method)
9938                   (not articles) t)     ; Accept form
9939             (not articles)              ; Only save nov last time
9940             (and move-is-internal
9941                  to-newsgroup           ; Not respooling
9942                                         ; Is this move internal?
9943                  (gnus-group-real-name to-newsgroup)))))
9944         ;; Copy the article.
9945         ((eq action 'copy)
9946          (with-current-buffer copy-buf
9947            (when (gnus-request-article-this-buffer article
9948                                                    gnus-newsgroup-name)
9949              (save-restriction
9950                (nnheader-narrow-to-headers)
9951                (dolist (hdr gnus-copy-article-ignored-headers)
9952                  (message-remove-header hdr t)))
9953              (gnus-request-accept-article
9954               to-newsgroup select-method (not articles) t))))
9955         ;; Crosspost the article.
9956         ((eq action 'crosspost)
9957          (let ((xref (message-tokenize-header
9958                       (mail-header-xref (gnus-summary-article-header
9959                                          article))
9960                       " ")))
9961            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9962                                   ":" (number-to-string article)))
9963            (unless xref
9964              (setq xref (list (system-name))))
9965            (setq new-xref
9966                  (concat
9967                   (mapconcat 'identity
9968                              (delete "Xref:" (delete new-xref xref))
9969                              " ")
9970                   " " new-xref))
9971            (with-current-buffer copy-buf
9972              ;; First put the article in the destination group.
9973              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9974              (when (consp (setq art-group
9975                                 (gnus-request-accept-article
9976                                  to-newsgroup select-method (not articles)
9977                                  t)))
9978                (setq new-xref (concat new-xref " " (car art-group)
9979                                       ":"
9980                                       (number-to-string (cdr art-group))))
9981                ;; Now we have the new Xrefs header, so we insert
9982                ;; it and replace the new article.
9983                (nnheader-replace-header "Xref" new-xref)
9984                (gnus-request-replace-article
9985                 (cdr art-group) to-newsgroup (current-buffer) t)
9986                art-group))))))
9987       (cond
9988        ((not art-group)
9989         (gnus-message 1 "Couldn't %s article %s: %s"
9990                       (cadr (assq action names)) article
9991                       (nnheader-get-report (car to-method))))
9992        ((eq art-group 'junk)
9993         (when (eq action 'move)
9994           (gnus-summary-mark-article article gnus-canceled-mark)
9995           (gnus-message 4 "Deleted article %s" article)
9996           ;; run the delete hook
9997           (run-hook-with-args 'gnus-summary-article-delete-hook
9998                               action
9999                               (gnus-data-header
10000                                (assoc article (gnus-data-list nil)))
10001                               gnus-newsgroup-name nil
10002                               select-method)))
10003        (t
10004         (let* ((pto-group (gnus-group-prefixed-name
10005                            (car art-group) to-method))
10006                (info (gnus-get-info pto-group))
10007                (to-group (gnus-info-group info))
10008                to-marks)
10009           ;; Update the group that has been moved to.
10010           (when (and info
10011                      (memq action '(move copy)))
10012             (unless (member to-group to-groups)
10013               (push to-group to-groups))
10014
10015             (when (and (not (memq article gnus-newsgroup-unreads))
10016                        (cdr art-group))
10017               (push 'read to-marks)
10018               (gnus-info-set-read
10019                info (gnus-add-to-range (gnus-info-read info)
10020                                        (list (cdr art-group)))))
10021
10022             ;; See whether the article is to be put in the cache.
10023             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10024                    (marks (if expirable
10025                               gnus-article-mark-lists
10026                             (delete '(expirable . expire)
10027                                     (copy-sequence
10028                                      gnus-article-mark-lists))))
10029                    (to-article (cdr art-group)))
10030
10031               ;; Enter the article into the cache in the new group,
10032               ;; if that is required.
10033               (when (and to-article
10034                          gnus-use-cache)
10035                 (gnus-cache-possibly-enter-article
10036                  to-group to-article
10037                  (memq article gnus-newsgroup-marked)
10038                  (memq article gnus-newsgroup-dormant)
10039                  (memq article gnus-newsgroup-unreads)))
10040
10041               (when (and gnus-preserve-marks
10042                          to-article)
10043                 ;; Copy any marks over to the new group.
10044                 (when (and (equal to-group gnus-newsgroup-name)
10045                            (not (memq article gnus-newsgroup-unreads)))
10046                   ;; Mark this article as read in this group.
10047                   (push (cons to-article gnus-read-mark)
10048                         gnus-newsgroup-reads)
10049                   ;; Increase the active status of this group.
10050                   (setcdr (gnus-active to-group) to-article)
10051                   (setcdr gnus-newsgroup-active to-article))
10052
10053                 (while marks
10054                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10055                     (when (memq article (symbol-value
10056                                          (intern (format "gnus-newsgroup-%s"
10057                                                          (caar marks)))))
10058                       (push (cdar marks) to-marks)
10059                       ;; If the other group is the same as this group,
10060                       ;; then we have to add the mark to the list.
10061                       (when (equal to-group gnus-newsgroup-name)
10062                         (set (intern (format "gnus-newsgroup-%s"
10063                                              (caar marks)))
10064                              (cons to-article
10065                                    (symbol-value
10066                                     (intern (format "gnus-newsgroup-%s"
10067                                                     (caar marks)))))))
10068                       ;; Copy the marks to other group.
10069                       (gnus-add-marked-articles
10070                        to-group (cdar marks) (list to-article) info)))
10071                   (setq marks (cdr marks)))
10072
10073                 (when (and expirable
10074                            gnus-mark-copied-or-moved-articles-as-expirable
10075                            (not (memq 'expire to-marks)))
10076                   ;; Mark this article as expirable.
10077                   (push 'expire to-marks)
10078                   (when (equal to-group gnus-newsgroup-name)
10079                     (push to-article gnus-newsgroup-expirable))
10080                   ;; Copy the expirable mark to other group.
10081                   (gnus-add-marked-articles
10082                    to-group 'expire (list to-article) info))
10083
10084                 (when (and to-marks
10085                            (gnus-method-option-p
10086                             (gnus-find-method-for-group to-group)
10087                             'server-marks))
10088                   (gnus-request-set-mark
10089                    to-group (list (list (list to-article) 'add to-marks)))))
10090
10091               (gnus-dribble-enter
10092                (concat "(gnus-group-set-info '"
10093                        (gnus-prin1-to-string (gnus-get-info to-group))
10094                        ")")
10095                (concat "^(gnus-group-set-info '(\""
10096                        (regexp-quote to-group) "\""))))
10097
10098           ;; Update the Xref header in this article to point to
10099           ;; the new crossposted article we have just created.
10100           (when (eq action 'crosspost)
10101             (with-current-buffer copy-buf
10102               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10103               (nnheader-replace-header "Xref" new-xref)
10104               (gnus-request-replace-article
10105                article gnus-newsgroup-name (current-buffer) t)))
10106
10107           ;; run the move/copy/crosspost/respool hook
10108           (run-hook-with-args 'gnus-summary-article-move-hook
10109                               action
10110                               (gnus-data-header
10111                                (assoc article (gnus-data-list nil)))
10112                               gnus-newsgroup-name
10113                               to-newsgroup
10114                               select-method))
10115
10116         ;;;!!!Why is this necessary?
10117         (set-buffer gnus-summary-buffer)
10118
10119         (when (eq action 'move)
10120           (save-excursion
10121             (gnus-summary-goto-subject article)
10122             (gnus-summary-mark-article article gnus-canceled-mark)))))
10123       (push article articles-to-update-marks))
10124
10125     (save-excursion
10126       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10127     ;; Re-activate all groups that have been moved to.
10128     (with-current-buffer gnus-group-buffer
10129       (let ((gnus-group-marked to-groups))
10130         (gnus-group-get-new-news-this-group nil t)))
10131
10132     (gnus-kill-buffer copy-buf)
10133     (gnus-summary-position-point)
10134     (gnus-set-mode-line 'summary)))
10135
10136 (defun gnus-summary-push-marks-to-backend (article)
10137   (let ((set nil)
10138         (del nil)
10139         (marks gnus-article-mark-lists))
10140     (unless (memq article gnus-newsgroup-unreads)
10141       (push 'read set))
10142     (while marks
10143       (if (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10144                (memq article (symbol-value
10145                               (intern (format "gnus-newsgroup-%s"
10146                                               (caar marks))))))
10147           (push (cdar marks) set)
10148         (push (cdar marks) del))
10149       (pop marks))
10150     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)
10151                                                  ((,article) del ,del)))))
10152
10153 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10154   "Copy the current article to some other group.
10155 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10156 When called interactively, if TO-NEWSGROUP is nil, use the value of
10157 the variable `gnus-move-split-methods' for finding a default target
10158 newsgroup.
10159 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10160 re-spool using this method."
10161   (interactive "P")
10162   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10163
10164 (defun gnus-summary-crosspost-article (&optional n)
10165   "Crosspost the current article to some other group."
10166   (interactive "P")
10167   (gnus-summary-move-article n nil nil 'crosspost))
10168
10169 (defcustom gnus-summary-respool-default-method nil
10170   "Default method type for respooling an article.
10171 If nil, use to the current newsgroup method."
10172   :type 'symbol
10173   :group 'gnus-summary-mail)
10174
10175 (defun gnus-summary-respool-article (&optional n method)
10176   "Respool the current article.
10177 The article will be squeezed through the mail spooling process again,
10178 which means that it will be put in some mail newsgroup or other
10179 depending on `nnmail-split-methods'.
10180 If N is a positive number, respool the N next articles.
10181 If N is a negative number, respool the N previous articles.
10182 If N is nil and any articles have been marked with the process mark,
10183 respool those articles instead.
10184
10185 Respooling can be done both from mail groups and \"real\" newsgroups.
10186 In the former case, the articles in question will be moved from the
10187 current group into whatever groups they are destined to.  In the
10188 latter case, they will be copied into the relevant groups."
10189   (interactive
10190    (list current-prefix-arg
10191          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10192                 (methname
10193                  (symbol-name (or gnus-summary-respool-default-method
10194                                   (car (gnus-find-method-for-group
10195                                         gnus-newsgroup-name)))))
10196                 (method
10197                  (gnus-completing-read
10198                   "Backend to use when respooling"
10199                   methods t nil 'gnus-mail-method-history methname))
10200                 ms)
10201            (cond
10202             ((zerop (length (setq ms (gnus-servers-using-backend
10203                                       (intern method)))))
10204              (list (intern method) ""))
10205             ((= 1 (length ms))
10206              (car ms))
10207             (t
10208              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10209                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10210                            ms-alist))))))))
10211   (unless method
10212     (error "No method given for respooling"))
10213   (if (assoc (symbol-name
10214               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10215              (gnus-methods-using 'respool))
10216       (gnus-summary-move-article n nil method)
10217     (gnus-summary-copy-article n nil method)))
10218
10219 (defun gnus-summary-import-article (file &optional edit)
10220   "Import an arbitrary file into a mail newsgroup."
10221   (interactive "fImport file: \nP")
10222   (let ((group gnus-newsgroup-name)
10223         (now (current-time))
10224         atts lines group-art)
10225     (unless (gnus-check-backend-function 'request-accept-article group)
10226       (error "%s does not support article importing" group))
10227     (or (file-readable-p file)
10228         (not (file-regular-p file))
10229         (error "Can't read %s" file))
10230     (with-current-buffer (gnus-get-buffer-create " *import file*")
10231       (erase-buffer)
10232       (nnheader-insert-file-contents file)
10233       (goto-char (point-min))
10234       (if (nnheader-article-p)
10235           (save-restriction
10236             (goto-char (point-min))
10237             (search-forward "\n\n" nil t)
10238             (narrow-to-region (point-min) (1- (point)))
10239             (goto-char (point-min))
10240             (unless (re-search-forward "^date:" nil t)
10241               (goto-char (point-max))
10242               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10243        ;; This doesn't look like an article, so we fudge some headers.
10244         (setq atts (file-attributes file)
10245               lines (count-lines (point-min) (point-max)))
10246         (insert "From: " (read-string "From: ") "\n"
10247                 "Subject: " (read-string "Subject: ") "\n"
10248                 "Date: " (message-make-date (nth 5 atts)) "\n"
10249                 "Message-ID: " (message-make-message-id) "\n"
10250                 "Lines: " (int-to-string lines) "\n"
10251                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10252       (setq group-art (gnus-request-accept-article group nil t))
10253       (kill-buffer (current-buffer)))
10254     (setq gnus-newsgroup-active (gnus-activate-group group))
10255     (forward-line 1)
10256     (gnus-summary-goto-article (cdr group-art) nil t)
10257     (when edit
10258       (gnus-summary-edit-article))))
10259
10260 (defun gnus-summary-create-article ()
10261   "Create an article in a mail newsgroup."
10262   (interactive)
10263   (let ((group gnus-newsgroup-name)
10264         (now (current-time))
10265         group-art)
10266     (unless (gnus-check-backend-function 'request-accept-article group)
10267       (error "%s does not support article importing" group))
10268     (with-current-buffer (gnus-get-buffer-create " *import file*")
10269       (erase-buffer)
10270       (goto-char (point-min))
10271       ;; This doesn't look like an article, so we fudge some headers.
10272       (insert "From: " (read-string "From: ") "\n"
10273               "Subject: " (read-string "Subject: ") "\n"
10274               "Date: " (message-make-date now) "\n"
10275               "Message-ID: " (message-make-message-id) "\n")
10276       (setq group-art (gnus-request-accept-article group nil t))
10277       (kill-buffer (current-buffer)))
10278     (setq gnus-newsgroup-active (gnus-activate-group group))
10279     (forward-line 1)
10280     (gnus-summary-goto-article (cdr group-art) nil t)
10281     (gnus-summary-edit-article)))
10282
10283 (defun gnus-summary-article-posted-p ()
10284   "Say whether the current (mail) article is available from news as well.
10285 This will be the case if the article has both been mailed and posted."
10286   (interactive)
10287   (let ((id (mail-header-references (gnus-summary-article-header)))
10288         (gnus-override-method (car (gnus-refer-article-methods))))
10289     (if (gnus-request-head id "")
10290         (gnus-message 2 "The current message was found on %s"
10291                       gnus-override-method)
10292       (gnus-message 2 "The current message couldn't be found on %s"
10293                     gnus-override-method)
10294       nil)))
10295
10296 (defun gnus-summary-expire-articles (&optional now)
10297   "Expire all articles that are marked as expirable in the current group."
10298   (interactive)
10299   (when (and (not gnus-group-is-exiting-without-update-p)
10300              (gnus-check-backend-function
10301               'request-expire-articles gnus-newsgroup-name))
10302     ;; This backend supports expiry.
10303     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10304            (expirable
10305             (gnus-list-range-difference
10306              (if total
10307                  (progn
10308                    ;; We need to update the info for
10309                    ;; this group for `gnus-list-of-read-articles'
10310                    ;; to give us the right answer.
10311                    (gnus-run-hooks 'gnus-exit-group-hook)
10312                    (gnus-summary-update-info)
10313                    (gnus-list-of-read-articles gnus-newsgroup-name))
10314                (setq gnus-newsgroup-expirable
10315                      (sort gnus-newsgroup-expirable '<)))
10316              gnus-newsgroup-unexist))
10317            (expiry-wait (if now 'immediate
10318                           (gnus-group-find-parameter
10319                            gnus-newsgroup-name 'expiry-wait)))
10320            (nnmail-expiry-target
10321             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10322                 nnmail-expiry-target))
10323            es)
10324       (when expirable
10325         ;; There are expirable articles in this group, so we run them
10326         ;; through the expiry process.
10327         (gnus-message 6 "Expiring articles...")
10328         (when (gnus-check-group gnus-newsgroup-name)
10329           ;; The list of articles that weren't expired is returned.
10330           (save-excursion
10331             (if expiry-wait
10332                 (let ((nnmail-expiry-wait-function nil)
10333                       (nnmail-expiry-wait expiry-wait))
10334                   (setq es (gnus-request-expire-articles
10335                             expirable gnus-newsgroup-name)))
10336               (setq es (gnus-request-expire-articles
10337                         expirable gnus-newsgroup-name)))
10338             (unless total
10339               (setq gnus-newsgroup-expirable es))
10340             ;; We go through the old list of expirable, and mark all
10341             ;; really expired articles as nonexistent.
10342             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10343               (let ((gnus-use-cache nil))
10344                 (dolist (article expirable)
10345                   (when (and (not (memq article es))
10346                              (gnus-data-find article))
10347                     (gnus-summary-mark-article article gnus-canceled-mark)
10348                     (run-hook-with-args 'gnus-summary-article-expire-hook
10349                                         'delete
10350                                         (gnus-data-header
10351                                          (assoc article (gnus-data-list nil)))
10352                                         gnus-newsgroup-name
10353                                         nil
10354                                         nil)))))))
10355         (gnus-message 6 "Expiring articles...done")))))
10356
10357 (defun gnus-summary-expire-articles-now ()
10358   "Expunge all expirable articles in the current group.
10359 This means that *all* articles that are marked as expirable will be
10360 deleted forever, right now."
10361   (interactive)
10362   (or gnus-expert-user
10363       (gnus-yes-or-no-p
10364        "Are you really, really sure you want to delete all expirable messages? ")
10365       (error "Phew!"))
10366   (gnus-summary-expire-articles t))
10367
10368 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10369 (defun gnus-summary-delete-article (&optional n)
10370   "Delete the N next (mail) articles.
10371 This command actually deletes articles.  This is not a marking
10372 command.  The article will disappear forever from your life, never to
10373 return.
10374
10375 If N is negative, delete backwards.
10376 If N is nil and articles have been marked with the process mark,
10377 delete these instead.
10378
10379 If `gnus-novice-user' is non-nil you will be asked for
10380 confirmation before the articles are deleted."
10381   (interactive "P")
10382   (unless (gnus-check-backend-function 'request-expire-articles
10383                                        gnus-newsgroup-name)
10384     (error "The current newsgroup does not support article deletion"))
10385   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10386     (error "Couldn't open server"))
10387   ;; Compute the list of articles to delete.
10388   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10389         (nnmail-expiry-target 'delete)
10390         not-deleted)
10391     (if (and gnus-novice-user
10392              (not (gnus-yes-or-no-p
10393                    (format "Do you really want to delete %s forever? "
10394                            (if (> (length articles) 1)
10395                                (format "these %s articles" (length articles))
10396                              "this article")))))
10397         ()
10398       ;; Delete the articles.
10399       (setq not-deleted (gnus-request-expire-articles
10400                          articles gnus-newsgroup-name 'force))
10401       (save-excursion
10402         (while articles
10403           (gnus-summary-remove-process-mark (car articles))
10404           ;; The backend might not have been able to delete the article
10405           ;; after all.
10406           (unless (memq (car articles) not-deleted)
10407             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10408             (let* ((article (car articles))
10409                    (ghead  (gnus-data-header
10410                             (assoc article (gnus-data-list nil)))))
10411               (run-hook-with-args 'gnus-summary-article-delete-hook
10412                                   'delete ghead gnus-newsgroup-name nil
10413                                   nil)))
10414           (setq articles (cdr articles))))
10415       (when not-deleted
10416         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10417     (gnus-summary-position-point)
10418     (gnus-set-mode-line 'summary)
10419     not-deleted))
10420
10421 (defun gnus-summary-edit-article (&optional arg)
10422   "Edit the current article.
10423 This will have permanent effect only in mail groups.
10424 If ARG is nil, edit the decoded articles.
10425 If ARG is 1, edit the raw articles.
10426 If ARG is 2, edit the raw articles even in read-only groups.
10427 If ARG is 3, edit the articles with the current handles.
10428 Otherwise, allow editing of articles even in read-only
10429 groups."
10430   (interactive "P")
10431   (let (force raw current-handles)
10432     (cond
10433      ((null arg))
10434      ((eq arg 1)
10435       (setq raw t))
10436      ((eq arg 2)
10437       (setq raw t
10438             force t))
10439      ((eq arg 3)
10440       (setq current-handles
10441             (and (gnus-buffer-live-p gnus-article-buffer)
10442                  (with-current-buffer gnus-article-buffer
10443                    (prog1
10444                        gnus-article-mime-handles
10445                      (setq gnus-article-mime-handles nil))))))
10446      (t
10447       (setq force t)))
10448     (when (and raw (not force)
10449                (member gnus-newsgroup-name '("nndraft:delayed"
10450                                              "nndraft:drafts"
10451                                              "nndraft:queue")))
10452       (error "Can't edit the raw article in group %s"
10453              gnus-newsgroup-name))
10454     (with-current-buffer gnus-summary-buffer
10455       (let ((mail-parse-charset gnus-newsgroup-charset)
10456             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10457         (gnus-set-global-variables)
10458         (when (and (not force)
10459                    (gnus-group-read-only-p))
10460           (error "The current newsgroup does not support article editing"))
10461         (gnus-summary-show-article t)
10462         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10463           (with-current-buffer gnus-article-buffer
10464             (mm-enable-multibyte)))
10465         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10466             (setq raw t))
10467         (gnus-article-edit-article
10468          (if raw 'ignore
10469            `(lambda ()
10470               (let ((mbl mml-buffer-list))
10471                 (setq mml-buffer-list nil)
10472                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10473                   (mime-to-mml ,'current-handles))
10474                 (let ((mbl1 mml-buffer-list))
10475                   (setq mml-buffer-list mbl)
10476                   (set (make-local-variable 'mml-buffer-list) mbl1))
10477                 (gnus-make-local-hook 'kill-buffer-hook)
10478                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10479          `(lambda (no-highlight)
10480             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10481                   (message-options message-options)
10482                   (message-options-set-recipient)
10483                   (mail-parse-ignored-charsets
10484                    ',gnus-newsgroup-ignored-charsets)
10485                   (rfc2047-header-encoding-alist
10486                    ',(let ((charset (gnus-group-name-charset
10487                                      (gnus-find-method-for-group
10488                                       gnus-newsgroup-name)
10489                                      gnus-newsgroup-name)))
10490                        (append (list (cons "Newsgroups" charset)
10491                                      (cons "Followup-To" charset)
10492                                      (cons "Xref" charset))
10493                                rfc2047-header-encoding-alist))))
10494               ,(if (not raw) '(progn
10495                                 (mml-to-mime)
10496                                 (mml-destroy-buffers)
10497                                 (remove-hook 'kill-buffer-hook
10498                                              'mml-destroy-buffers t)
10499                                 (kill-local-variable 'mml-buffer-list)))
10500               (gnus-summary-edit-article-done
10501                ,(or (mail-header-references gnus-current-headers) "")
10502                ,(gnus-group-read-only-p)
10503                ,gnus-summary-buffer no-highlight))))))))
10504
10505 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10506
10507 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10508                                                  no-highlight)
10509   "Make edits to the current article permanent."
10510   (interactive)
10511   (save-excursion
10512     ;; The buffer restriction contains the entire article if it exists.
10513     (when (article-goto-body)
10514       (let ((lines (count-lines (point) (point-max)))
10515             (length (- (point-max) (point)))
10516             (case-fold-search t)
10517             (body (copy-marker (point))))
10518         (goto-char (point-min))
10519         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10520           (delete-region (match-beginning 1) (match-end 1))
10521           (insert (number-to-string length)))
10522         (goto-char (point-min))
10523         (when (re-search-forward
10524                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10525           (delete-region (match-beginning 1) (match-end 1))
10526           (insert (number-to-string length)))
10527         (goto-char (point-min))
10528         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10529           (delete-region (match-beginning 1) (match-end 1))
10530           (insert (number-to-string lines))))))
10531   ;; Replace the article.
10532   (let ((buf (current-buffer))
10533         (article (cdr gnus-article-current))
10534         replace-result)
10535     (with-temp-buffer
10536       (insert-buffer-substring buf)
10537       (if (and (not read-only)
10538                (not (setq replace-result
10539                           (gnus-request-replace-article
10540                            article (car gnus-article-current)
10541                            (current-buffer) t))))
10542           (error "Couldn't replace article")
10543         ;; If we got a number back, then that's the new article number
10544         ;; for this article.  Otherwise, the article number didn't change.
10545         (when (numberp replace-result)
10546           (with-current-buffer gnus-summary-buffer
10547             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10548             (gnus-summary-limit gnus-newsgroup-limit)
10549             (setq article replace-result)
10550             (gnus-summary-goto-subject article t)))
10551         ;; Update the summary buffer.
10552         (if (and references
10553                  (equal (message-tokenize-header references " ")
10554                         (message-tokenize-header
10555                          (or (message-fetch-field "references") "") " ")))
10556             ;; We only have to update this line.
10557             (save-excursion
10558               (save-restriction
10559                 (message-narrow-to-head)
10560                 (let ((head (buffer-substring-no-properties
10561                              (point-min) (point-max)))
10562                       header)
10563                   (with-temp-buffer
10564                     (insert (format "211 %d Article retrieved.\n" article))
10565                     (insert head)
10566                     (insert ".\n")
10567                     (let ((nntp-server-buffer (current-buffer)))
10568                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10569                     (with-current-buffer gnus-summary-buffer
10570                       (gnus-data-set-header (gnus-data-find article) header)
10571                       (gnus-summary-update-article-line article header)
10572                       (if (gnus-summary-goto-subject article nil t)
10573                           (gnus-summary-update-secondary-mark article)))))))
10574           ;; Update threads.
10575           (set-buffer (or buffer gnus-summary-buffer))
10576           (gnus-summary-update-article article)
10577           (if (gnus-summary-goto-subject article nil t)
10578               (gnus-summary-update-secondary-mark article)))
10579         ;; Prettify the article buffer again.
10580         (unless no-highlight
10581           (with-current-buffer gnus-article-buffer
10582             ;;!!! Fix this -- article should be rehighlighted.
10583             ;;(gnus-run-hooks 'gnus-article-display-hook)
10584             (set-buffer gnus-original-article-buffer)
10585             (gnus-request-article
10586              article (car gnus-article-current) (current-buffer))))
10587         ;; Prettify the summary buffer line.
10588         (when (gnus-visual-p 'summary-highlight 'highlight)
10589           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10590
10591 (defun gnus-summary-edit-wash (key)
10592   "Perform editing command KEY in the article buffer."
10593   (interactive
10594    (list
10595     (progn
10596       (message "%s" (concat (this-command-keys) "- "))
10597       (read-char))))
10598   (message "")
10599   (gnus-summary-edit-article)
10600   (execute-kbd-macro (concat (this-command-keys) key))
10601   (gnus-article-edit-done))
10602
10603 ;;; Respooling
10604
10605 (defun gnus-summary-respool-query (&optional silent trace)
10606   "Query where the respool algorithm would put this article."
10607   (interactive)
10608   (let (gnus-mark-article-hook)
10609     (gnus-summary-select-article)
10610     (with-current-buffer gnus-original-article-buffer
10611       (let ((groups (nnmail-article-group 'identity trace)))
10612         (unless silent
10613           (if groups
10614               (message "This message would go to %s"
10615                        (mapconcat 'car groups ", "))
10616             (message "This message would go to no groups"))
10617           groups)))))
10618
10619 (defun gnus-summary-respool-trace ()
10620   "Trace where the respool algorithm would put this article.
10621 Display a buffer showing all fancy splitting patterns which matched."
10622   (interactive)
10623   (gnus-summary-respool-query nil t))
10624
10625 ;; Summary marking commands.
10626
10627 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10628   "Mark articles which has the same subject as read, and then select the next.
10629 If UNMARK is positive, remove any kind of mark.
10630 If UNMARK is negative, tick articles."
10631   (interactive "P")
10632   (when unmark
10633     (setq unmark (prefix-numeric-value unmark)))
10634   (let ((count
10635          (gnus-summary-mark-same-subject
10636           (gnus-summary-article-subject) unmark)))
10637     ;; Select next unread article.  If auto-select-same mode, should
10638     ;; select the first unread article.
10639     (gnus-summary-next-article t (and gnus-auto-select-same
10640                                       (gnus-summary-article-subject)))
10641     (gnus-message 7 "%d article%s marked as %s"
10642                   count (if (= count 1) " is" "s are")
10643                   (if unmark "unread" "read"))))
10644
10645 (defun gnus-summary-kill-same-subject (&optional unmark)
10646   "Mark articles which has the same subject as read.
10647 If UNMARK is positive, remove any kind of mark.
10648 If UNMARK is negative, tick articles."
10649   (interactive "P")
10650   (when unmark
10651     (setq unmark (prefix-numeric-value unmark)))
10652   (let ((count
10653          (gnus-summary-mark-same-subject
10654           (gnus-summary-article-subject) unmark)))
10655     ;; If marked as read, go to next unread subject.
10656     (when (null unmark)
10657       ;; Go to next unread subject.
10658       (gnus-summary-next-subject 1 t))
10659     (gnus-message 7 "%d articles are marked as %s"
10660                   count (if unmark "unread" "read"))))
10661
10662 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10663   "Mark articles with same SUBJECT as read, and return marked number.
10664 If optional argument UNMARK is positive, remove any kinds of marks.
10665 If optional argument UNMARK is negative, mark articles as unread instead."
10666   (let ((count 1))
10667     (save-excursion
10668       (cond
10669        ((null unmark)                   ; Mark as read.
10670         (while (and
10671                 (progn
10672                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10673                   (gnus-summary-show-thread) t)
10674                 (gnus-summary-find-subject subject))
10675           (setq count (1+ count))))
10676        ((> unmark 0)                    ; Tick.
10677         (while (and
10678                 (progn
10679                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10680                   (gnus-summary-show-thread) t)
10681                 (gnus-summary-find-subject subject))
10682           (setq count (1+ count))))
10683        (t                               ; Mark as unread.
10684         (while (and
10685                 (progn
10686                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10687                   (gnus-summary-show-thread) t)
10688                 (gnus-summary-find-subject subject))
10689           (setq count (1+ count)))))
10690       (gnus-set-mode-line 'summary)
10691       ;; Return the number of marked articles.
10692       count)))
10693
10694 (defun gnus-summary-mark-as-processable (n &optional unmark)
10695   "Set the process mark on the next N articles.
10696 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10697 the process mark instead.  The difference between N and the actual
10698 number of articles marked is returned."
10699   (interactive "P")
10700   (if (and (null n) (gnus-region-active-p))
10701       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10702     (setq n (prefix-numeric-value n))
10703     (let ((backward (< n 0))
10704           (n (abs n)))
10705       (while (and
10706               (> n 0)
10707               (if unmark
10708                   (gnus-summary-remove-process-mark
10709                    (gnus-summary-article-number))
10710                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10711               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10712         (setq n (1- n)))
10713       (when (/= 0 n)
10714         (gnus-message 7 "No more articles"))
10715       (gnus-summary-recenter)
10716       (gnus-summary-position-point)
10717       n)))
10718
10719 (defun gnus-summary-unmark-as-processable (n)
10720   "Remove the process mark from the next N articles.
10721 If N is negative, unmark backward instead.  The difference between N and
10722 the actual number of articles unmarked is returned."
10723   (interactive "P")
10724   (gnus-summary-mark-as-processable n t))
10725
10726 (defun gnus-summary-unmark-all-processable ()
10727   "Remove the process mark from all articles."
10728   (interactive)
10729   (save-excursion
10730     (while gnus-newsgroup-processable
10731       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10732   (gnus-summary-position-point))
10733
10734 (defun gnus-summary-add-mark (article type)
10735   "Mark ARTICLE with a mark of TYPE."
10736   (let ((vtype (car (assq type gnus-article-mark-lists)))
10737         var)
10738     (if (not vtype)
10739         (error "No such mark type: %s" type)
10740       (setq var (intern (format "gnus-newsgroup-%s" type)))
10741       (set var (cons article (symbol-value var)))
10742       (if (memq type '(processable cached replied forwarded recent saved))
10743           (gnus-summary-update-secondary-mark article)
10744         ;;; !!! This is bogus.  We should find out what primary
10745         ;;; !!! mark we want to set.
10746         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10747
10748 (defun gnus-summary-mark-as-expirable (n)
10749   "Mark N articles forward as expirable.
10750 If N is negative, mark backward instead.  The difference between N and
10751 the actual number of articles marked is returned."
10752   (interactive "p")
10753   (gnus-summary-mark-forward n gnus-expirable-mark))
10754
10755 (defun gnus-summary-mark-as-spam (n)
10756   "Mark N articles forward as spam.
10757 If N is negative, mark backward instead.  The difference between N and
10758 the actual number of articles marked is returned."
10759   (interactive "p")
10760   (gnus-summary-mark-forward n gnus-spam-mark))
10761
10762 (defun gnus-summary-mark-article-as-replied (article)
10763   "Mark ARTICLE as replied to and update the summary line.
10764 ARTICLE can also be a list of articles."
10765   (interactive (list (gnus-summary-article-number)))
10766   (let ((articles (if (listp article) article (list article))))
10767     (dolist (article articles)
10768       (unless (numberp article)
10769         (error "%s is not a number" article))
10770       (push article gnus-newsgroup-replied)
10771       (let ((inhibit-read-only t))
10772         (when (gnus-summary-goto-subject article nil t)
10773           (gnus-summary-update-secondary-mark article))))))
10774
10775 (defun gnus-summary-mark-article-as-forwarded (article)
10776   "Mark ARTICLE as forwarded and update the summary line.
10777 ARTICLE can also be a list of articles."
10778   (let ((articles (if (listp article) article (list article))))
10779     (dolist (article articles)
10780       (push article gnus-newsgroup-forwarded)
10781       (let ((inhibit-read-only t))
10782         (when (gnus-summary-goto-subject article nil t)
10783           (gnus-summary-update-secondary-mark article))))))
10784
10785 (defun gnus-summary-set-bookmark (article)
10786   "Set a bookmark in current article."
10787   (interactive (list (gnus-summary-article-number)))
10788   (when (or (not (get-buffer gnus-article-buffer))
10789             (not gnus-current-article)
10790             (not gnus-article-current)
10791             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10792     (error "No current article selected"))
10793   ;; Remove old bookmark, if one exists.
10794   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10795   ;; Set the new bookmark, which is on the form
10796   ;; (article-number . line-number-in-body).
10797   (push
10798    (cons article
10799          (with-current-buffer gnus-article-buffer
10800            (count-lines
10801             (min (point)
10802                  (save-excursion
10803                    (article-goto-body)
10804                    (point)))
10805             (point))))
10806    gnus-newsgroup-bookmarks)
10807   (gnus-message 6 "A bookmark has been added to the current article."))
10808
10809 (defun gnus-summary-remove-bookmark (article)
10810   "Remove the bookmark from the current article."
10811   (interactive (list (gnus-summary-article-number)))
10812   ;; Remove old bookmark, if one exists.
10813   (if (not (assq article gnus-newsgroup-bookmarks))
10814       (gnus-message 6 "No bookmark in current article.")
10815     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10816     (gnus-message 6 "Removed bookmark.")))
10817
10818 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10819 (defun gnus-summary-mark-as-dormant (n)
10820   "Mark N articles forward as dormant.
10821 If N is negative, mark backward instead.  The difference between N and
10822 the actual number of articles marked is returned."
10823   (interactive "p")
10824   (gnus-summary-mark-forward n gnus-dormant-mark))
10825
10826 (defun gnus-summary-set-process-mark (article)
10827   "Set the process mark on ARTICLE and update the summary line."
10828   (setq gnus-newsgroup-processable
10829         (cons article
10830               (delq article gnus-newsgroup-processable)))
10831   (when (gnus-summary-goto-subject article)
10832     (gnus-summary-show-thread)
10833     (gnus-summary-goto-subject article)
10834     (gnus-summary-update-secondary-mark article)))
10835
10836 (defun gnus-summary-remove-process-mark (&rest articles)
10837   "Remove the process mark from ARTICLES and update the summary line."
10838   (dolist (article articles)
10839     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10840     (when (gnus-summary-goto-subject article)
10841       (gnus-summary-show-thread)
10842       (gnus-summary-goto-subject article)
10843       (gnus-summary-update-secondary-mark article)))
10844   t)
10845
10846 (defun gnus-summary-set-saved-mark (article)
10847   "Set the process mark on ARTICLE and update the summary line."
10848   (push article gnus-newsgroup-saved)
10849   (when (gnus-summary-goto-subject article)
10850     (gnus-summary-update-secondary-mark article)))
10851
10852 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10853   "Mark N articles as read forwards.
10854 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10855 The difference between N and the actual number of articles marked is
10856 returned.
10857 If NO-EXPIRE, auto-expiry will be inhibited."
10858   (interactive "p")
10859   (gnus-summary-show-thread)
10860   (let ((backward (< n 0))
10861         (gnus-summary-goto-unread
10862          (and gnus-summary-goto-unread
10863               (not (eq gnus-summary-goto-unread 'never))
10864               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10865                                     gnus-ticked-mark gnus-dormant-mark)))))
10866         (n (abs n))
10867         (mark (or mark gnus-del-mark)))
10868     (while (and (> n 0)
10869                 (gnus-summary-mark-article nil mark no-expire)
10870                 (zerop (gnus-summary-next-subject
10871                         (if backward -1 1)
10872                         (and gnus-summary-goto-unread
10873                              (not (eq gnus-summary-goto-unread 'never)))
10874                         t)))
10875       (setq n (1- n)))
10876     (when (/= 0 n)
10877       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10878     (gnus-summary-recenter)
10879     (gnus-summary-position-point)
10880     (gnus-set-mode-line 'summary)
10881     n))
10882
10883 (defun gnus-summary-mark-article-as-read (mark)
10884   "Mark the current article quickly as read with MARK."
10885   (let ((article (gnus-summary-article-number)))
10886     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10887     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10888     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10889     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10890     (push (cons article mark) gnus-newsgroup-reads)
10891     ;; Possibly remove from cache, if that is used.
10892     (when gnus-use-cache
10893       (gnus-cache-enter-remove-article article))
10894     ;; Allow the backend to change the mark.
10895     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10896     ;; Check for auto-expiry.
10897     (when (and gnus-newsgroup-auto-expire
10898                (memq mark gnus-auto-expirable-marks))
10899       (setq mark gnus-expirable-mark)
10900       ;; Let the backend know about the mark change.
10901       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10902       (push article gnus-newsgroup-expirable))
10903     ;; Set the mark in the buffer.
10904     (gnus-summary-update-mark mark 'unread)
10905     t))
10906
10907 (defun gnus-summary-mark-article-as-unread (mark)
10908   "Mark the current article quickly as unread with MARK."
10909   (let* ((article (gnus-summary-article-number))
10910          (old-mark (gnus-summary-article-mark article)))
10911     ;; Allow the backend to change the mark.
10912     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10913     (if (eq mark old-mark)
10914         t
10915       (if (<= article 0)
10916           (progn
10917             (gnus-error 1 "Can't mark negative article numbers")
10918             nil)
10919         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10920         (setq gnus-newsgroup-spam-marked
10921               (delq article gnus-newsgroup-spam-marked))
10922         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10923         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10924         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10925         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10926         (cond ((= mark gnus-ticked-mark)
10927                (setq gnus-newsgroup-marked
10928                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10929                                               article)))
10930               ((= mark gnus-spam-mark)
10931                (setq gnus-newsgroup-spam-marked
10932                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10933                                               article)))
10934               ((= mark gnus-dormant-mark)
10935                (setq gnus-newsgroup-dormant
10936                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10937                                               article)))
10938               (t
10939                (setq gnus-newsgroup-unreads
10940                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10941                                               article))))
10942         (gnus-alist-pull article gnus-newsgroup-reads)
10943
10944         ;; See whether the article is to be put in the cache.
10945         (and gnus-use-cache
10946              (vectorp (gnus-summary-article-header article))
10947              (save-excursion
10948                (gnus-cache-possibly-enter-article
10949                 gnus-newsgroup-name article
10950                 (= mark gnus-ticked-mark)
10951                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10952
10953         ;; Fix the mark.
10954         (gnus-summary-update-mark mark 'unread)
10955         t))))
10956
10957 (defun gnus-summary-mark-article (&optional article mark no-expire)
10958   "Mark ARTICLE with MARK.  MARK can be any character.
10959 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10960 `??' (dormant) and `?E' (expirable).
10961 If MARK is nil, then the default character `?r' is used.
10962 If ARTICLE is nil, then the article on the current line will be
10963 marked.
10964 If NO-EXPIRE, auto-expiry will be inhibited."
10965   ;; The mark might be a string.
10966   (when (stringp mark)
10967     (setq mark (aref mark 0)))
10968   ;; If no mark is given, then we check auto-expiring.
10969   (when (null mark)
10970     (setq mark gnus-del-mark))
10971   (when (and (not no-expire)
10972              gnus-newsgroup-auto-expire
10973              (memq mark gnus-auto-expirable-marks))
10974     (setq mark gnus-expirable-mark))
10975   (let ((article (or article (gnus-summary-article-number)))
10976         (old-mark (gnus-summary-article-mark article)))
10977     ;; Allow the backend to change the mark.
10978     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10979     (if (eq mark old-mark)
10980         t
10981       (unless article
10982         (error "No article on current line"))
10983       (if (not (if (or (= mark gnus-unread-mark)
10984                        (= mark gnus-ticked-mark)
10985                        (= mark gnus-spam-mark)
10986                        (= mark gnus-dormant-mark))
10987                    (gnus-mark-article-as-unread article mark)
10988                  (gnus-mark-article-as-read article mark)))
10989           t
10990         ;; See whether the article is to be put in the cache.
10991         (and gnus-use-cache
10992              (not (= mark gnus-canceled-mark))
10993              (vectorp (gnus-summary-article-header article))
10994              (save-excursion
10995                (gnus-cache-possibly-enter-article
10996                 gnus-newsgroup-name article
10997                 (= mark gnus-ticked-mark)
10998                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10999
11000         (when (gnus-summary-goto-subject article nil t)
11001           (let ((inhibit-read-only t))
11002             (gnus-summary-show-thread)
11003             ;; Fix the mark.
11004             (gnus-summary-update-mark mark 'unread)
11005             t))))))
11006
11007 (defun gnus-summary-update-secondary-mark (article)
11008   "Update the secondary (read, process, cache) mark."
11009   (gnus-summary-update-mark
11010    (cond ((memq article gnus-newsgroup-processable)
11011           gnus-process-mark)
11012          ((memq article gnus-newsgroup-cached)
11013           gnus-cached-mark)
11014          ((memq article gnus-newsgroup-replied)
11015           gnus-replied-mark)
11016          ((memq article gnus-newsgroup-forwarded)
11017           gnus-forwarded-mark)
11018          ((memq article gnus-newsgroup-saved)
11019           gnus-saved-mark)
11020          ((memq article gnus-newsgroup-unseen)
11021           gnus-unseen-mark)
11022          (t gnus-no-mark))
11023    'replied)
11024   (when (gnus-visual-p 'summary-highlight 'highlight)
11025     (gnus-summary-highlight-line)
11026     (gnus-run-hooks 'gnus-summary-update-hook))
11027   t)
11028
11029 (defun gnus-summary-update-download-mark (article)
11030   "Update the download mark."
11031   (gnus-summary-update-mark
11032    (cond ((memq article gnus-newsgroup-undownloaded)
11033           gnus-undownloaded-mark)
11034          (gnus-newsgroup-agentized
11035           gnus-downloaded-mark)
11036          (t
11037           gnus-no-mark))
11038    'download)
11039   (gnus-summary-update-line t)
11040   t)
11041
11042 (defun gnus-summary-update-mark (mark type)
11043   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11044         (inhibit-read-only t))
11045     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11046     (when forward
11047       (when (looking-at "\r")
11048         (incf forward))
11049       (when (<= (+ forward (point)) (point-max))
11050         ;; Go to the right position on the line.
11051         (goto-char (+ forward (point)))
11052         ;; Replace the old mark with the new mark.
11053         (let ((to-insert
11054                (mm-subst-char-in-string
11055                 (char-after) mark
11056                 (buffer-substring (point) (1+ (point))))))
11057           (delete-region (point) (1+ (point)))
11058           (insert to-insert))
11059         ;; Optionally update the marks by some user rule.
11060         (when (eq type 'unread)
11061           (gnus-data-set-mark
11062            (gnus-data-find (gnus-summary-article-number)) mark)
11063           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11064
11065 (defun gnus-mark-article-as-read (article &optional mark)
11066   "Enter ARTICLE in the pertinent lists and remove it from others."
11067   ;; Make the article expirable.
11068   (let ((mark (or mark gnus-del-mark)))
11069     (setq gnus-newsgroup-expirable
11070           (if (= mark gnus-expirable-mark)
11071               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11072             (delq article gnus-newsgroup-expirable)))
11073     ;; Remove from unread and marked lists.
11074     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11075     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11076     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11077     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11078     (push (cons article mark) gnus-newsgroup-reads)
11079     ;; Possibly remove from cache, if that is used.
11080     (when gnus-use-cache
11081       (gnus-cache-enter-remove-article article))
11082     t))
11083
11084 (defun gnus-mark-article-as-unread (article &optional mark)
11085   "Enter ARTICLE in the pertinent lists and remove it from others."
11086   (let ((mark (or mark gnus-ticked-mark)))
11087     (if (<= article 0)
11088         (progn
11089           (gnus-error 1 "Can't mark negative article numbers")
11090           nil)
11091       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11092             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11093             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11094             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11095             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11096
11097       ;; Unsuppress duplicates?
11098       (when gnus-suppress-duplicates
11099         (gnus-dup-unsuppress-article article))
11100
11101       (cond ((= mark gnus-ticked-mark)
11102              (setq gnus-newsgroup-marked
11103                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11104             ((= mark gnus-spam-mark)
11105              (setq gnus-newsgroup-spam-marked
11106                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11107                                             article)))
11108             ((= mark gnus-dormant-mark)
11109              (setq gnus-newsgroup-dormant
11110                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11111             (t
11112              (setq gnus-newsgroup-unreads
11113                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11114       (gnus-alist-pull article gnus-newsgroup-reads)
11115       t)))
11116
11117 (defun gnus-summary-tick-article-forward (n)
11118   "Tick N articles forwards.
11119 If N is negative, tick backwards instead.
11120 The difference between N and the number of articles ticked is returned."
11121   (interactive "p")
11122   (gnus-summary-mark-forward n gnus-ticked-mark))
11123
11124 (defun gnus-summary-tick-article-backward (n)
11125   "Tick N articles backwards.
11126 The difference between N and the number of articles ticked is returned."
11127   (interactive "p")
11128   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11129
11130 (defun gnus-summary-tick-article (&optional article clear-mark)
11131   "Mark current article as unread.
11132 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11133 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11134   (interactive)
11135   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11136                                        gnus-ticked-mark)))
11137
11138 (defun gnus-summary-mark-as-read-forward (n)
11139   "Mark N articles as read forwards.
11140 If N is negative, mark backwards instead.
11141 The difference between N and the actual number of articles marked is
11142 returned."
11143   (interactive "p")
11144   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11145
11146 (defun gnus-summary-mark-as-read-backward (n)
11147   "Mark the N articles as read backwards.
11148 The difference between N and the actual number of articles marked is
11149 returned."
11150   (interactive "p")
11151   (gnus-summary-mark-forward
11152    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11153
11154 (defun gnus-summary-mark-as-read (&optional article mark)
11155   "Mark current article as read.
11156 ARTICLE specifies the article to be marked as read.
11157 MARK specifies a string to be inserted at the beginning of the line."
11158   (gnus-summary-mark-article article mark))
11159
11160 (defun gnus-summary-clear-mark-forward (n)
11161   "Clear marks from N articles forward.
11162 If N is negative, clear backward instead.
11163 The difference between N and the number of marks cleared is returned."
11164   (interactive "p")
11165   (gnus-summary-mark-forward n gnus-unread-mark))
11166
11167 (defun gnus-summary-clear-mark-backward (n)
11168   "Clear marks from N articles backward.
11169 The difference between N and the number of marks cleared is returned."
11170   (interactive "p")
11171   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11172
11173 (defun gnus-summary-mark-unread-as-read ()
11174   "Intended to be used by `gnus-mark-article-hook'."
11175   (when (memq gnus-current-article gnus-newsgroup-unreads)
11176     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11177
11178 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11179   "Intended to be used by `gnus-mark-article-hook'."
11180   (let ((mark (gnus-summary-article-mark)))
11181     (when (or (gnus-unread-mark-p mark)
11182               (gnus-read-mark-p mark))
11183       (gnus-summary-mark-article gnus-current-article
11184                                  (or new-mark gnus-read-mark)))))
11185
11186 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11187   "Intended to be used by `gnus-mark-article-hook'."
11188   (let ((mark (gnus-summary-article-mark)))
11189     (when (or (gnus-unread-mark-p mark)
11190               (gnus-read-mark-p mark))
11191       (gnus-summary-mark-article (gnus-summary-article-number)
11192                                  (or new-mark gnus-read-mark)))))
11193
11194 (defun gnus-summary-mark-unread-as-ticked ()
11195   "Intended to be used by `gnus-mark-article-hook'."
11196   (when (memq gnus-current-article gnus-newsgroup-unreads)
11197     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11198
11199 (defun gnus-summary-mark-region-as-read (point mark all)
11200   "Mark all unread articles between point and mark as read.
11201 If given a prefix, mark all articles between point and mark as read,
11202 even ticked and dormant ones."
11203   (interactive "r\nP")
11204   (save-excursion
11205     (let (article)
11206       (goto-char point)
11207       (beginning-of-line)
11208       (while (and
11209               (< (point) mark)
11210               (progn
11211                 (when (or all
11212                           (memq (setq article (gnus-summary-article-number))
11213                                 gnus-newsgroup-unreads))
11214                   (gnus-summary-mark-article article gnus-del-mark))
11215                 t)
11216               (gnus-summary-find-next))))))
11217
11218 (defun gnus-summary-mark-below (score mark)
11219   "Mark articles with score less than SCORE with MARK."
11220   (interactive "P\ncMark: ")
11221   (setq score (if score
11222                   (prefix-numeric-value score)
11223                 (or gnus-summary-default-score 0)))
11224   (with-current-buffer gnus-summary-buffer
11225     (goto-char (point-min))
11226     (while
11227         (progn
11228           (and (< (gnus-summary-article-score) score)
11229                (gnus-summary-mark-article nil mark))
11230           (gnus-summary-find-next)))))
11231
11232 (defun gnus-summary-kill-below (&optional score)
11233   "Mark articles with score below SCORE as read."
11234   (interactive "P")
11235   (gnus-summary-mark-below score gnus-killed-mark))
11236
11237 (defun gnus-summary-clear-above (&optional score)
11238   "Clear all marks from articles with score above SCORE."
11239   (interactive "P")
11240   (gnus-summary-mark-above score gnus-unread-mark))
11241
11242 (defun gnus-summary-tick-above (&optional score)
11243   "Tick all articles with score above SCORE."
11244   (interactive "P")
11245   (gnus-summary-mark-above score gnus-ticked-mark))
11246
11247 (defun gnus-summary-mark-above (score mark)
11248   "Mark articles with score over SCORE with MARK."
11249   (interactive "P\ncMark: ")
11250   (setq score (if score
11251                   (prefix-numeric-value score)
11252                 (or gnus-summary-default-score 0)))
11253   (with-current-buffer gnus-summary-buffer
11254     (goto-char (point-min))
11255     (while (and (progn
11256                   (when (> (gnus-summary-article-score) score)
11257                     (gnus-summary-mark-article nil mark))
11258                   t)
11259                 (gnus-summary-find-next)))))
11260
11261 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11262 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11263 (defun gnus-summary-limit-include-expunged (&optional no-error)
11264   "Display all the hidden articles that were expunged for low scores."
11265   (interactive)
11266   (let ((inhibit-read-only t))
11267     (let ((scored gnus-newsgroup-scored)
11268           headers h)
11269       (while scored
11270         (unless (gnus-summary-article-header (caar scored))
11271           (and (setq h (gnus-number-to-header (caar scored)))
11272                (< (cdar scored) gnus-summary-expunge-below)
11273                (push h headers)))
11274         (setq scored (cdr scored)))
11275       (if (not headers)
11276           (when (not no-error)
11277             (error "No expunged articles hidden"))
11278         (goto-char (point-min))
11279         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11280         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11281         (dolist (x headers)
11282           (push (mail-header-number x) gnus-newsgroup-limit))
11283         (gnus-summary-prepare-unthreaded (nreverse headers))
11284         (goto-char (point-min))
11285         (gnus-summary-position-point)
11286         t))))
11287
11288 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11289   "Mark all unread articles in this newsgroup as read.
11290 If prefix argument ALL is non-nil, ticked and dormant articles will
11291 also be marked as read.
11292 If QUIETLY is non-nil, no questions will be asked.
11293
11294 If TO-HERE is non-nil, it should be a point in the buffer.  All
11295 articles before (after, if REVERSE is set) this point will be marked
11296 as read.
11297
11298 Note that this function will only catch up the unread article
11299 in the current summary buffer limitation.
11300
11301 The number of articles marked as read is returned."
11302   (interactive "P")
11303   (prog1
11304       (save-excursion
11305         (when (or quietly
11306                   (not gnus-interactive-catchup) ;Without confirmation?
11307                   gnus-expert-user
11308                   (gnus-y-or-n-p
11309                    (if all
11310                        "Mark absolutely all articles as read? "
11311                      "Mark all unread articles as read? ")))
11312           (if (and not-mark
11313                    (not gnus-newsgroup-adaptive)
11314                    (not gnus-newsgroup-auto-expire)
11315                    (not gnus-suppress-duplicates)
11316                    (or (not gnus-use-cache)
11317                        (eq gnus-use-cache 'passive)))
11318               (progn
11319                 (when all
11320                   (setq gnus-newsgroup-marked nil
11321                         gnus-newsgroup-spam-marked nil
11322                         gnus-newsgroup-dormant nil))
11323                 (setq gnus-newsgroup-unreads
11324                       (gnus-sorted-nunion
11325                        (gnus-sorted-intersection gnus-newsgroup-unreads
11326                                                  gnus-newsgroup-downloadable)
11327                        (gnus-sorted-difference gnus-newsgroup-unfetched
11328                                                gnus-newsgroup-cached))))
11329             ;; We actually mark all articles as canceled, which we
11330             ;; have to do when using auto-expiry or adaptive scoring.
11331             (gnus-summary-show-all-threads)
11332             (if (and to-here reverse)
11333                 (progn
11334                   (goto-char to-here)
11335                   (gnus-summary-mark-current-read-and-unread-as-read
11336                    gnus-catchup-mark)
11337                   (while (gnus-summary-find-next (not all))
11338                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11339               (when (gnus-summary-first-subject (not all))
11340                 (while (and
11341                         (if to-here (< (point) to-here) t)
11342                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11343                         (gnus-summary-find-next (not all))))))
11344             (gnus-set-mode-line 'summary))
11345           t))
11346     (gnus-summary-position-point)))
11347
11348 (defun gnus-summary-catchup-to-here (&optional all)
11349   "Mark all unticked articles before the current one as read.
11350 If ALL is non-nil, also mark ticked and dormant articles as read."
11351   (interactive "P")
11352   (save-excursion
11353     (gnus-save-hidden-threads
11354       (let ((beg (point)))
11355         ;; We check that there are unread articles.
11356         (when (or all (gnus-summary-find-prev))
11357           (gnus-summary-catchup all t beg)))))
11358   (gnus-summary-position-point))
11359
11360 (defun gnus-summary-catchup-from-here (&optional all)
11361   "Mark all unticked articles after (and including) 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-next))
11369           (gnus-summary-catchup all t beg nil t)))))
11370   (gnus-summary-position-point))
11371
11372 (defun gnus-summary-catchup-all (&optional quietly)
11373   "Mark all articles in this newsgroup as read.
11374 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11375 instead, which marks only unread articles as read."
11376   (interactive "P")
11377   (gnus-summary-catchup t quietly))
11378
11379 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11380   "Mark all unread articles in this group as read, then exit.
11381 If prefix argument ALL is non-nil, all articles are marked as read.
11382 If QUIETLY is non-nil, no questions will be asked."
11383   (interactive "P")
11384   (when (gnus-summary-catchup all quietly nil 'fast)
11385     ;; Select next newsgroup or exit.
11386     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11387              (eq gnus-auto-select-next 'quietly))
11388         (gnus-summary-next-group nil)
11389       (gnus-summary-exit))))
11390
11391 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11392   "Mark all articles in this newsgroup as read, and then exit.
11393 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11394 instead, which marks only unread articles as read."
11395   (interactive "P")
11396   (gnus-summary-catchup-and-exit t quietly))
11397
11398 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11399   "Mark all articles in this group as read and select the next group.
11400 If given a prefix, mark all articles, unread as well as ticked, as
11401 read."
11402   (interactive "P")
11403   (save-excursion
11404     (gnus-summary-catchup all))
11405   (gnus-summary-next-group))
11406
11407 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11408   "Mark all articles in this group as read and select the previous group.
11409 If given a prefix, mark all articles, unread as well as ticked, as
11410 read."
11411   (interactive "P")
11412   (save-excursion
11413     (gnus-summary-catchup all))
11414   (gnus-summary-next-group nil nil t))
11415
11416 ;;;
11417 ;;; with article
11418 ;;;
11419
11420 (defmacro gnus-with-article (article &rest forms)
11421   "Select ARTICLE and perform FORMS in the original article buffer.
11422 Then replace the article with the result."
11423   `(progn
11424      ;; We don't want the article to be marked as read.
11425      (let (gnus-mark-article-hook)
11426        (gnus-summary-select-article t t nil ,article))
11427      (set-buffer gnus-original-article-buffer)
11428      ,@forms
11429      (if (not (gnus-check-backend-function
11430                'request-replace-article (car gnus-article-current)))
11431          (gnus-message 5 "Read-only group; not replacing")
11432        (unless (gnus-request-replace-article
11433                 ,article (car gnus-article-current)
11434                 (current-buffer) t)
11435          (error "Couldn't replace article")))
11436      ;; The cache and backlog have to be flushed somewhat.
11437      (when gnus-keep-backlog
11438        (gnus-backlog-remove-article
11439         (car gnus-article-current) (cdr gnus-article-current)))
11440      (when gnus-use-cache
11441        (gnus-cache-update-article
11442         (car gnus-article-current) (cdr gnus-article-current)))))
11443
11444 (put 'gnus-with-article 'lisp-indent-function 1)
11445 (put 'gnus-with-article 'edebug-form-spec '(form body))
11446
11447 ;; Thread-based commands.
11448
11449 (defun gnus-summary-articles-in-thread (&optional article)
11450   "Return a list of all articles in the current thread.
11451 If ARTICLE is non-nil, return all articles in the thread that starts
11452 with that article."
11453   (let* ((article (or article (gnus-summary-article-number)))
11454          (data (gnus-data-find-list article))
11455          (top-level (gnus-data-level (car data)))
11456          (top-subject
11457           (cond ((null gnus-thread-operation-ignore-subject)
11458                  (gnus-simplify-subject-re
11459                   (mail-header-subject (gnus-data-header (car data)))))
11460                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11461                  (gnus-simplify-subject-fuzzy
11462                   (mail-header-subject (gnus-data-header (car data)))))
11463                 (t nil)))
11464          (end-point (save-excursion
11465                       (goto-char (gnus-data-pos (car data)))
11466                       (if (gnus-summary-go-to-next-thread)
11467                           (point) (point-max))))
11468          articles)
11469     (while (and data
11470                 (< (gnus-data-pos (car data)) end-point))
11471       (when (or (not top-subject)
11472                 (string= top-subject
11473                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11474                              (gnus-simplify-subject-fuzzy
11475                               (mail-header-subject
11476                                (gnus-data-header (car data))))
11477                            (gnus-simplify-subject-re
11478                             (mail-header-subject
11479                              (gnus-data-header (car data)))))))
11480         (push (gnus-data-number (car data)) articles))
11481       (unless (and (setq data (cdr data))
11482                    (> (gnus-data-level (car data)) top-level))
11483         (setq data nil)))
11484     ;; Return the list of articles.
11485     (nreverse articles)))
11486
11487 (defun gnus-summary-rethread-current ()
11488   "Rethread the thread the current article is part of."
11489   (interactive)
11490   (let* ((gnus-show-threads t)
11491          (article (gnus-summary-article-number))
11492          (id (mail-header-id (gnus-summary-article-header)))
11493          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11494     (unless id
11495       (error "No article on the current line"))
11496     (gnus-rebuild-thread id)
11497     (gnus-summary-goto-subject article)))
11498
11499 (defun gnus-summary-reparent-thread ()
11500   "Make the current article child of the marked (or previous) article.
11501
11502 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11503 is non-nil or the Subject: of both articles are the same."
11504   (interactive)
11505   (unless (not (gnus-group-read-only-p))
11506     (error "The current newsgroup does not support article editing"))
11507   (unless (<= (length gnus-newsgroup-processable) 1)
11508     (error "No more than one article may be marked"))
11509   (let ((child (gnus-summary-article-number))
11510         ;; First grab the marked article, otherwise one line up.
11511         (parent (if (not (null gnus-newsgroup-processable))
11512                     (car gnus-newsgroup-processable)
11513                   (save-excursion
11514                     (if (eq (forward-line -1) 0)
11515                         (gnus-summary-article-number)
11516                       (error "Beginning of summary buffer"))))))
11517     (gnus-summary-reparent-children parent (list child))))
11518
11519 (defun gnus-summary-reparent-children (parent children)
11520   "Make PARENT the parent of CHILDREN.
11521 When called interactively, PARENT is the current article and CHILDREN
11522 are the process-marked articles."
11523   (interactive
11524    (list (gnus-summary-article-number)
11525          (gnus-summary-work-articles nil)))
11526   (dolist (child children)
11527     (save-window-excursion
11528       (let ((gnus-article-buffer " *reparent*"))
11529         (unless (not (eq parent child))
11530           (error "An article may not be self-referential"))
11531         (let ((message-id (mail-header-id
11532                            (gnus-summary-article-header parent))))
11533           (unless (and message-id (not (equal message-id "")))
11534             (error "No message-id in desired parent"))
11535           (gnus-with-article child
11536             (save-restriction
11537               (goto-char (point-min))
11538               (message-narrow-to-head)
11539               (if (re-search-forward "^References: " nil t)
11540                   (progn
11541                     (re-search-forward "^[^ \t]" nil t)
11542                     (forward-line -1)
11543                     (end-of-line)
11544                     (insert " " message-id))
11545                 (insert "References: " message-id "\n"))))
11546           (set-buffer gnus-summary-buffer)
11547           (gnus-summary-unmark-all-processable)
11548           (gnus-summary-update-article child)
11549           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11550             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11551           (gnus-summary-rethread-current)
11552           (gnus-message 3 "Article %d is now the child of article %d"
11553                         child parent))))))
11554
11555 (defun gnus-summary-toggle-threads (&optional arg)
11556   "Toggle showing conversation threads.
11557 If ARG is positive number, turn showing conversation threads on."
11558   (interactive "P")
11559   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11560     (setq gnus-show-threads
11561           (if (null arg) (not gnus-show-threads)
11562             (> (prefix-numeric-value arg) 0)))
11563     (gnus-summary-prepare)
11564     (gnus-summary-goto-subject current)
11565     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11566     (gnus-summary-position-point)))
11567
11568 (eval-and-compile
11569   (if (fboundp 'remove-overlays)
11570       (defalias 'gnus-remove-overlays 'remove-overlays)
11571     (defun gnus-remove-overlays (beg end name val)
11572       "Clear BEG and END of overlays whose property NAME has value VAL.
11573 For compatibility with XEmacs."
11574       (dolist (ov (gnus-overlays-in beg end))
11575         (when (eq (gnus-overlay-get ov name) val)
11576           (gnus-delete-overlay ov))))))
11577
11578 (defun gnus-summary-show-all-threads ()
11579   "Show all threads."
11580   (interactive)
11581   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11582   (gnus-summary-position-point))
11583
11584 (defsubst gnus-summary--inv (p)
11585   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11586
11587 (defun gnus-summary-show-thread ()
11588   "Show thread subtrees.
11589 Returns nil if no thread was there to be shown."
11590   (interactive)
11591   (let* ((orig (point))
11592          (end (point-at-eol))
11593          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11594          ;; Leave point at bol
11595          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11596          (eoi (when end
11597                 (if (fboundp 'next-single-char-property-change)
11598                     ;; Note: XEmacs version of n-s-c-p-c may return nil
11599                     (or (next-single-char-property-change end 'invisible)
11600                         (point-max))
11601                   (while (progn
11602                            (end-of-line 2)
11603                            (and (not (eobp))
11604                                 (eq (get-char-property (point) 'invisible)
11605                                     'gnus-sum))))
11606                   (point)))))
11607     (when eoi
11608       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11609       (goto-char orig)
11610       (gnus-summary-position-point)
11611       eoi)))
11612
11613 (defun gnus-summary-maybe-hide-threads ()
11614   "If requested, hide the threads that should be hidden."
11615   (when (and gnus-show-threads
11616              gnus-thread-hide-subtree)
11617     (gnus-summary-hide-all-threads
11618      (if (or (consp gnus-thread-hide-subtree)
11619              (functionp gnus-thread-hide-subtree))
11620          (gnus-make-predicate gnus-thread-hide-subtree)
11621        nil))))
11622
11623 ;;; Hiding predicates.
11624
11625 (defun gnus-article-unread-p (header)
11626   (memq (mail-header-number header) gnus-newsgroup-unreads))
11627
11628 (defun gnus-article-unseen-p (header)
11629   (memq (mail-header-number header) gnus-newsgroup-unseen))
11630
11631 (defun gnus-map-articles (predicate articles)
11632   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11633   (apply 'gnus-or (mapcar predicate
11634                           (mapcar (lambda (number)
11635                                     (gnus-summary-article-header number))
11636                                   articles))))
11637
11638 (defun gnus-summary-hide-all-threads (&optional predicate)
11639   "Hide all thread subtrees.
11640 If PREDICATE is supplied, threads that satisfy this predicate
11641 will not be hidden."
11642   (interactive)
11643   (save-excursion
11644     (goto-char (point-min))
11645     (let ((end nil)
11646           (count 0))
11647       (while (not end)
11648         (incf count)
11649         (when (zerop (mod count 1000))
11650           (message "Hiding all threads... %d" count))
11651         (when (or (not predicate)
11652                   (gnus-map-articles
11653                    predicate (gnus-summary-article-children)))
11654             (gnus-summary-hide-thread))
11655         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11656   (gnus-summary-position-point))
11657
11658 (defun gnus-summary-hide-thread ()
11659   "Hide thread subtrees.
11660 If PREDICATE is supplied, threads that satisfy this predicate
11661 will not be hidden.
11662 Returns nil if no threads were there to be hidden."
11663   (interactive)
11664   (beginning-of-line)
11665   (let ((start (point))
11666         (starteol (line-end-position))
11667         (article (gnus-summary-article-number)))
11668     ;; Go forward until either the buffer ends or the subthread ends.
11669     (when (and (not (eobp))
11670                (or (zerop (gnus-summary-next-thread 1 t))
11671                    (goto-char (point-max))))
11672       (if (and (> (point) start)
11673                ;; FIXME: this should actually search for a non-invisible \n.
11674                (search-backward "\n" start t))
11675           (progn
11676             (when (> (point) starteol)
11677               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11678               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11679                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11680                 (gnus-overlay-put ol 'evaporate t)))
11681             (gnus-summary-goto-subject article)
11682             (when (> start (point))
11683               (message "Hiding the thread moved us backwards, aborting!")
11684               (goto-char (point-max))))
11685         (goto-char start)
11686         nil))))
11687
11688 (defun gnus-summary-go-to-next-thread (&optional previous)
11689   "Go to the same level (or less) next thread.
11690 If PREVIOUS is non-nil, go to previous thread instead.
11691 Return the article number moved to, or nil if moving was impossible."
11692   (let ((level (gnus-summary-thread-level))
11693         (way (if previous -1 1))
11694         (beg (point)))
11695     (forward-line way)
11696     (while (and (not (eobp))
11697                 (< level (gnus-summary-thread-level)))
11698       (forward-line way))
11699     (if (eobp)
11700         (progn
11701           (goto-char beg)
11702           nil)
11703       (setq beg (point))
11704       (prog1
11705           (gnus-summary-article-number)
11706         (goto-char beg)))))
11707
11708 (defun gnus-summary-next-thread (n &optional silent)
11709   "Go to the same level next N'th thread.
11710 If N is negative, search backward instead.
11711 Returns the difference between N and the number of skips actually
11712 done.
11713
11714 If SILENT, don't output messages."
11715   (interactive "p")
11716   (let ((backward (< n 0))
11717         (n (abs n)))
11718     (while (and (> n 0)
11719                 (gnus-summary-go-to-next-thread backward))
11720       (decf n))
11721     (unless silent
11722       (gnus-summary-position-point))
11723     (when (and (not silent) (/= 0 n))
11724       (gnus-message 7 "No more threads"))
11725     n))
11726
11727 (defun gnus-summary-prev-thread (n)
11728   "Go to the same level previous N'th thread.
11729 Returns the difference between N and the number of skips actually
11730 done."
11731   (interactive "p")
11732   (gnus-summary-next-thread (- n)))
11733
11734 (defun gnus-summary-go-down-thread ()
11735   "Go down one level in the current thread."
11736   (let ((children (gnus-summary-article-children)))
11737     (when children
11738       (gnus-summary-goto-subject (car children)))))
11739
11740 (defun gnus-summary-go-up-thread ()
11741   "Go up one level in the current thread."
11742   (let ((parent (gnus-summary-article-parent)))
11743     (when parent
11744       (gnus-summary-goto-subject parent))))
11745
11746 (defun gnus-summary-down-thread (n)
11747   "Go down thread N steps.
11748 If N is negative, go up instead.
11749 Returns the difference between N and how many steps down that were
11750 taken."
11751   (interactive "p")
11752   (let ((up (< n 0))
11753         (n (abs n)))
11754     (while (and (> n 0)
11755                 (if up (gnus-summary-go-up-thread)
11756                   (gnus-summary-go-down-thread)))
11757       (setq n (1- n)))
11758     (gnus-summary-position-point)
11759     (when (/= 0 n)
11760       (gnus-message 7 "Can't go further"))
11761     n))
11762
11763 (defun gnus-summary-up-thread (n)
11764   "Go up thread N steps.
11765 If N is negative, go down instead.
11766 Returns the difference between N and how many steps down that were
11767 taken."
11768   (interactive "p")
11769   (gnus-summary-down-thread (- n)))
11770
11771 (defun gnus-summary-top-thread ()
11772   "Go to the top of the thread."
11773   (interactive)
11774   (while (gnus-summary-go-up-thread))
11775   (gnus-summary-article-number))
11776
11777 (defun gnus-summary-expire-thread ()
11778   "Mark articles under current thread as expired."
11779   (interactive)
11780   (gnus-summary-kill-thread 0))
11781
11782 (defun gnus-summary-kill-thread (&optional unmark)
11783   "Mark articles under current thread as read.
11784 If the prefix argument is positive, remove any kinds of marks.
11785 If the prefix argument is zero, mark thread as expired.
11786 If the prefix argument is negative, tick articles instead."
11787   (interactive "P")
11788   (when unmark
11789     (setq unmark (prefix-numeric-value unmark)))
11790   (let ((articles (gnus-summary-articles-in-thread))
11791         (hide (or (null unmark) (= unmark 0))))
11792     (save-excursion
11793       ;; Expand the thread.
11794       (gnus-summary-show-thread)
11795       ;; Mark all the articles.
11796       (while articles
11797         (gnus-summary-goto-subject (car articles))
11798         (cond ((null unmark)
11799                (gnus-summary-mark-article-as-read gnus-killed-mark))
11800               ((> unmark 0)
11801                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11802               ((= unmark 0)
11803                (gnus-summary-mark-article nil gnus-expirable-mark))
11804               (t
11805                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11806         (setq articles (cdr articles))))
11807     ;; Hide killed subtrees when hide is true.
11808     (and hide
11809          gnus-thread-hide-killed
11810          (gnus-summary-hide-thread))
11811     ;; If hide is t, go to next unread subject.
11812     (when hide
11813       ;; Go to next unread subject.
11814       (gnus-summary-next-subject 1 t)))
11815   (gnus-set-mode-line 'summary))
11816
11817 ;; Summary sorting commands
11818
11819 (defun gnus-summary-sort-by-number (&optional reverse)
11820   "Sort the summary buffer by article number.
11821 Argument REVERSE means reverse order."
11822   (interactive "P")
11823   (gnus-summary-sort 'number reverse))
11824
11825 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11826   "Sort the summary buffer by most recent article number.
11827 Argument REVERSE means reverse order."
11828   (interactive "P")
11829   (gnus-summary-sort 'most-recent-number reverse))
11830
11831 (defun gnus-summary-sort-by-random (&optional reverse)
11832   "Randomize the order in the summary buffer.
11833 Argument REVERSE means to randomize in reverse order."
11834   (interactive "P")
11835   (gnus-summary-sort 'random reverse))
11836
11837 (defun gnus-summary-sort-by-author (&optional reverse)
11838   "Sort the summary buffer by author name alphabetically.
11839 If `case-fold-search' is non-nil, case of letters is ignored.
11840 Argument REVERSE means reverse order."
11841   (interactive "P")
11842   (gnus-summary-sort 'author reverse))
11843
11844 (defun gnus-summary-sort-by-recipient (&optional reverse)
11845   "Sort the summary buffer by recipient name alphabetically.
11846 If `case-fold-search' is non-nil, case of letters is ignored.
11847 Argument REVERSE means reverse order."
11848   (interactive "P")
11849   (gnus-summary-sort 'recipient reverse))
11850
11851 (defun gnus-summary-sort-by-subject (&optional reverse)
11852   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11853 If `case-fold-search' is non-nil, case of letters is ignored.
11854 Argument REVERSE means reverse order."
11855   (interactive "P")
11856   (gnus-summary-sort 'subject reverse))
11857
11858 (defun gnus-summary-sort-by-date (&optional reverse)
11859   "Sort the summary buffer by date.
11860 Argument REVERSE means reverse order."
11861   (interactive "P")
11862   (gnus-summary-sort 'date reverse))
11863
11864 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11865   "Sort the summary buffer by most recent date.
11866 Argument REVERSE means reverse order."
11867   (interactive "P")
11868   (gnus-summary-sort 'most-recent-date reverse))
11869
11870 (defun gnus-summary-sort-by-score (&optional reverse)
11871   "Sort the summary buffer by score.
11872 Argument REVERSE means reverse order."
11873   (interactive "P")
11874   (gnus-summary-sort 'score reverse))
11875
11876 (defun gnus-summary-sort-by-lines (&optional reverse)
11877   "Sort the summary buffer by the number of lines.
11878 Argument REVERSE means reverse order."
11879   (interactive "P")
11880   (gnus-summary-sort 'lines reverse))
11881
11882 (defun gnus-summary-sort-by-chars (&optional reverse)
11883   "Sort the summary buffer by article length.
11884 Argument REVERSE means reverse order."
11885   (interactive "P")
11886   (gnus-summary-sort 'chars reverse))
11887
11888 (defun gnus-summary-sort-by-original (&optional reverse)
11889   "Sort the summary buffer using the default sorting method.
11890 Argument REVERSE means reverse order."
11891   (interactive "P")
11892   (let* ((inhibit-read-only t)
11893          (gnus-summary-prepare-hook nil))
11894     ;; We do the sorting by regenerating the threads.
11895     (gnus-summary-prepare)
11896     ;; Hide subthreads if needed.
11897     (gnus-summary-maybe-hide-threads)))
11898
11899 (defun gnus-summary-sort (predicate reverse)
11900   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11901   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11902          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11903          (gnus-thread-sort-functions
11904           (if (not reverse)
11905               thread
11906             `(lambda (t1 t2)
11907                (,thread t2 t1))))
11908          (gnus-sort-gathered-threads-function
11909           gnus-thread-sort-functions)
11910          (gnus-article-sort-functions
11911           (if (not reverse)
11912               article
11913             `(lambda (t1 t2)
11914                (,article t2 t1))))
11915          (inhibit-read-only t)
11916          (gnus-summary-prepare-hook nil))
11917     ;; We do the sorting by regenerating the threads.
11918     (gnus-summary-prepare)
11919     ;; Hide subthreads if needed.
11920     (gnus-summary-maybe-hide-threads)))
11921
11922 ;; Summary saving commands.
11923
11924 (defun gnus-summary-save-article (&optional n not-saved)
11925   "Save the current article using the default saver function.
11926 If N is a positive number, save the N next articles.
11927 If N is a negative number, save the N previous articles.
11928 If N is nil and any articles have been marked with the process mark,
11929 save those articles instead.
11930 The variable `gnus-default-article-saver' specifies the saver function.
11931
11932 If the optional second argument NOT-SAVED is non-nil, articles saved
11933 will not be marked as saved."
11934   (interactive "P")
11935   (require 'gnus-art)
11936   (let* ((articles (gnus-summary-work-articles n))
11937          (save-buffer (save-excursion
11938                         (nnheader-set-temp-buffer " *Gnus Save*")))
11939          (num (length articles))
11940          ;; Whether to save decoded articles or raw articles.
11941          (decode (when gnus-article-save-coding-system
11942                    (get gnus-default-article-saver :decode)))
11943          ;; When saving many articles in a single file, use the other
11944          ;; function to save articles other than the first one.
11945          (saver2 (get gnus-default-article-saver :function))
11946          (gnus-prompt-before-saving (if saver2
11947                                         t
11948                                       gnus-prompt-before-saving))
11949          (gnus-default-article-saver gnus-default-article-saver)
11950          header file)
11951     (dolist (article articles)
11952       (setq header (gnus-summary-article-header article))
11953       (if (not (vectorp header))
11954           ;; This is a pseudo-article.
11955           (if (assq 'name header)
11956               (gnus-copy-file (cdr (assq 'name header)))
11957             (gnus-message 1 "Article %d is unsavable" article))
11958         ;; This is a real article.
11959         (save-window-excursion
11960           (gnus-summary-select-article decode decode nil article)
11961           (gnus-summary-goto-subject article))
11962         (with-current-buffer save-buffer
11963           (erase-buffer)
11964           (insert-buffer-substring (if decode
11965                                        gnus-article-buffer
11966                                      gnus-original-article-buffer)))
11967         (setq file (gnus-article-save save-buffer file num))
11968         (gnus-summary-remove-process-mark article)
11969         (unless not-saved
11970           (gnus-summary-set-saved-mark article)))
11971       (when saver2
11972         (setq gnus-default-article-saver saver2
11973               saver2 nil)))
11974     (gnus-kill-buffer save-buffer)
11975     (gnus-summary-position-point)
11976     (gnus-set-mode-line 'summary)
11977     n))
11978
11979 (defun gnus-summary-pipe-output (&optional n sym)
11980   "Pipe the current article to a subprocess.
11981 If N is a positive number, pipe the N next articles.
11982 If N is a negative number, pipe the N previous articles.
11983 If N is nil and any articles have been marked with the process mark,
11984 pipe those articles instead.
11985 The default command to which articles are piped is specified by the
11986 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11987 will be prompted for the command.
11988
11989 The properties `:decode' and `:headers' that are put to the function
11990 symbol `gnus-summary-save-in-pipe' control whether this function
11991 decodes articles and what headers to keep (see the doc string for the
11992 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11993 is neither omitted nor the symbol `r', force including all headers
11994 regardless of the `:headers' property.  If it is the symbol `r',
11995 articles that are not decoded and include all headers will be piped
11996 no matter what the properties `:decode' and `:headers' are."
11997   (interactive (gnus-interactive "P\ny"))
11998   (require 'gnus-art)
11999   (let* ((articles (gnus-summary-work-articles n))
12000          (result-buffer "*Shell Command Output*")
12001          (all-headers (not (memq sym '(nil r))))
12002          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
12003          (raw (eq sym 'r))
12004          (headers (get 'gnus-summary-save-in-pipe :headers))
12005          command result)
12006     (unless (numberp (car articles))
12007       (error "No article to pipe"))
12008     (setq command (gnus-read-shell-command
12009                    (concat "Shell command on "
12010                            (if (cdr articles)
12011                                (format "these %d articles" (length articles))
12012                              "this article")
12013                            ": ")
12014                    gnus-summary-pipe-output-default-command))
12015     (when (string-equal command "")
12016       (error "A command is required"))
12017     (when all-headers
12018       (put 'gnus-summary-save-in-pipe :headers nil))
12019     (unwind-protect
12020         (while articles
12021           (gnus-summary-goto-subject (pop articles))
12022           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12023           (when (and (get-buffer result-buffer)
12024                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12025             (setq result (concat result (with-current-buffer result-buffer
12026                                           (buffer-string))))))
12027       (put 'gnus-summary-save-in-pipe :headers headers))
12028     (unless (zerop (length result))
12029       (if (with-current-buffer (get-buffer-create result-buffer)
12030             (erase-buffer)
12031             (insert result)
12032             (prog1
12033                 (and (= (count-lines (point-min) (point)) 1)
12034                      (progn
12035                        (end-of-line 0)
12036                        (<= (current-column)
12037                            (window-width (minibuffer-window)))))
12038               (goto-char (point-min))))
12039           (message "%s" (substring result 0 -1))
12040         (message nil)
12041         (gnus-configure-windows 'pipe)))))
12042
12043 (defun gnus-summary-save-article-mail (&optional arg)
12044   "Append the current article to a Unix mail box file.
12045 If N is a positive number, save the N next articles.
12046 If N is a negative number, save the N previous articles.
12047 If N is nil and any articles have been marked with the process mark,
12048 save those articles instead."
12049   (interactive "P")
12050   (require 'gnus-art)
12051   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12052     (gnus-summary-save-article arg)))
12053
12054 (defun gnus-summary-save-article-rmail (&optional arg)
12055   "Append the current article to an rmail file.
12056 If N is a positive number, save the N next articles.
12057 If N is a negative number, save the N previous articles.
12058 If N is nil and any articles have been marked with the process mark,
12059 save those articles instead."
12060   (interactive "P")
12061   (require 'gnus-art)
12062   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12063     (gnus-summary-save-article arg)))
12064
12065 (defun gnus-summary-save-article-file (&optional arg)
12066   "Append the current article to a file.
12067 If N is a positive number, save the N next articles.
12068 If N is a negative number, save the N previous articles.
12069 If N is nil and any articles have been marked with the process mark,
12070 save those articles instead."
12071   (interactive "P")
12072   (require 'gnus-art)
12073   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12074     (gnus-summary-save-article arg)))
12075
12076 (defun gnus-summary-write-article-file (&optional arg)
12077   "Write the current article to a file, deleting the previous file.
12078 If N is a positive number, save the N next articles.
12079 If N is a negative number, save the N previous articles.
12080 If N is nil and any articles have been marked with the process mark,
12081 save those articles instead."
12082   (interactive "P")
12083   (require 'gnus-art)
12084   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12085     (gnus-summary-save-article arg)))
12086
12087 (defun gnus-summary-save-article-body-file (&optional arg)
12088   "Append the current article body to a file.
12089 If N is a positive number, save the N next articles.
12090 If N is a negative number, save the N previous articles.
12091 If N is nil and any articles have been marked with the process mark,
12092 save those articles instead."
12093   (interactive "P")
12094   (require 'gnus-art)
12095   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12096     (gnus-summary-save-article arg)))
12097
12098 (defun gnus-summary-write-article-body-file (&optional arg)
12099   "Write the current article body to a file, deleting the previous file.
12100 If N is a positive number, save the N next articles.
12101 If N is a negative number, save the N previous articles.
12102 If N is nil and any articles have been marked with the process mark,
12103 save those articles instead."
12104   (interactive "P")
12105   (require 'gnus-art)
12106   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12107     (gnus-summary-save-article arg)))
12108
12109 (defun gnus-summary-muttprint (&optional arg)
12110   "Print the current article using Muttprint.
12111 If N is a positive number, save the N next articles.
12112 If N is a negative number, save the N previous articles.
12113 If N is nil and any articles have been marked with the process mark,
12114 save those articles instead."
12115   (interactive "P")
12116   (require 'gnus-art)
12117   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12118     (gnus-summary-save-article arg t)))
12119
12120 (defun gnus-summary-pipe-message (program)
12121   "Pipe the current article through PROGRAM."
12122   (interactive "sProgram: ")
12123   (gnus-summary-select-article)
12124   (let ((mail-header-separator ""))
12125     (gnus-eval-in-buffer-window gnus-article-buffer
12126       (save-restriction
12127         (widen)
12128         (let ((start (window-start))
12129               (inhibit-read-only t))
12130           (message-pipe-buffer-body program)
12131           (set-window-start (get-buffer-window (current-buffer)) start))))))
12132
12133 (defun gnus-get-split-value (methods)
12134   "Return a value based on the split METHODS."
12135   (let (split-name method result match)
12136     (when methods
12137       (with-current-buffer gnus-original-article-buffer
12138         (save-restriction
12139           (nnheader-narrow-to-headers)
12140           (while (and methods (not split-name))
12141             (goto-char (point-min))
12142             (setq method (pop methods))
12143             (setq match (car method))
12144             (when (cond
12145                    ((stringp match)
12146                     ;; Regular expression.
12147                     (ignore-errors
12148                       (re-search-forward match nil t)))
12149                    ((functionp match)
12150                     ;; Function.
12151                     (save-restriction
12152                       (widen)
12153                       (setq result (funcall match gnus-newsgroup-name))))
12154                    ((consp match)
12155                     ;; Form.
12156                     (save-restriction
12157                       (widen)
12158                       (setq result (eval match)))))
12159               (setq split-name (cdr method))
12160               (cond ((stringp result)
12161                      (push (expand-file-name
12162                             result gnus-article-save-directory)
12163                            split-name))
12164                     ((consp result)
12165                      (setq split-name (append result split-name)))))))))
12166     (nreverse split-name)))
12167
12168 (defun gnus-valid-move-group-p (group)
12169   (and (symbolp group)
12170        (boundp group)
12171        (symbol-name group)
12172        (symbol-value group)
12173        (gnus-get-function (gnus-find-method-for-group
12174                            (symbol-name group)) 'request-accept-article t)))
12175
12176 (defun gnus-read-move-group-name (prompt default articles prefix)
12177   "Read a group name."
12178   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12179          (minibuffer-confirm-incomplete nil) ; XEmacs
12180          (prom
12181           (format "%s %s to"
12182                   prompt
12183                   (if (> (length articles) 1)
12184                       (format "these %d articles" (length articles))
12185                     "this article")))
12186          (to-newsgroup
12187           (cond
12188            ((null split-name)
12189             (gnus-group-completing-read
12190              prom
12191              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12192              nil prefix nil default))
12193            ((= 1 (length split-name))
12194             (gnus-group-completing-read
12195              prom
12196              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12197              nil prefix 'gnus-group-history (car split-name)))
12198            (t
12199             (gnus-completing-read
12200              prom (nreverse split-name) nil nil 'gnus-group-history))))
12201          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12202          encoded)
12203     (when to-newsgroup
12204       (if (or (string= to-newsgroup "")
12205               (string= to-newsgroup prefix))
12206           (setq to-newsgroup default))
12207       (unless to-newsgroup
12208         (error "No group name entered"))
12209       (setq encoded (mm-encode-coding-string
12210                      to-newsgroup
12211                      (gnus-group-name-charset to-method to-newsgroup)))
12212       (or (gnus-active encoded)
12213           (gnus-activate-group encoded nil nil to-method)
12214           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12215                                      to-newsgroup))
12216               (or (and (gnus-request-create-group encoded to-method)
12217                        (gnus-activate-group encoded nil nil to-method)
12218                        (gnus-subscribe-group encoded))
12219                   (error "Couldn't create group %s" to-newsgroup)))
12220           (error "No such group: %s" to-newsgroup))
12221       encoded)))
12222
12223 (defvar gnus-summary-save-parts-counter)
12224 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12225
12226 (defun gnus-summary-save-parts (type dir n &optional reverse)
12227   "Save parts matching TYPE to DIR.
12228 If REVERSE, save parts that do not match TYPE."
12229   (interactive
12230    (list (read-string "Save parts of type: "
12231                       (or (car gnus-summary-save-parts-type-history)
12232                           gnus-summary-save-parts-default-mime)
12233                       'gnus-summary-save-parts-type-history)
12234          (setq gnus-summary-save-parts-last-directory
12235                (read-directory-name "Save to directory: "
12236                                     gnus-summary-save-parts-last-directory
12237                                     nil t))
12238          current-prefix-arg))
12239   (gnus-summary-iterate n
12240     (let ((gnus-display-mime-function nil)
12241           gnus-article-prepare-hook
12242           gnus-article-decode-hook
12243           gnus-display-mime-function
12244           gnus-break-pages
12245           (gnus-inhibit-treatment t))
12246       (gnus-summary-select-article))
12247     (with-current-buffer gnus-article-buffer
12248       (let ((handles (or gnus-article-mime-handles
12249                          (mm-dissect-buffer nil gnus-article-loose-mime)
12250                          (and gnus-article-emulate-mime
12251                               (mm-uu-dissect))))
12252             (gnus-summary-save-parts-counter 1))
12253         (when handles
12254           (gnus-summary-save-parts-1 type dir handles reverse)
12255           (unless gnus-article-mime-handles ;; Don't destroy this case.
12256             (mm-destroy-parts handles)))))))
12257
12258 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12259   (if (stringp (car handle))
12260       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12261               (cdr handle))
12262     (when (if reverse
12263               (not (string-match type (mm-handle-media-type handle)))
12264             (string-match type (mm-handle-media-type handle)))
12265       (let ((file (expand-file-name
12266                    (gnus-map-function
12267                     mm-file-name-rewrite-functions
12268                     (file-name-nondirectory
12269                      (or
12270                       (mm-handle-filename handle)
12271                       (format "%s.%d.%d" gnus-newsgroup-name
12272                               (cdr gnus-article-current)
12273                               gnus-summary-save-parts-counter))))
12274                    dir)))
12275         (incf gnus-summary-save-parts-counter)
12276         (unless (file-exists-p file)
12277           (mm-save-part-to-file handle file))))))
12278
12279 ;; Summary extract commands
12280
12281 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12282   (let ((inhibit-read-only t)
12283         (article (gnus-summary-article-number))
12284         after-article b e)
12285     (unless (gnus-summary-goto-subject article)
12286       (error "No such article: %d" article))
12287     (gnus-summary-position-point)
12288     ;; If all commands are to be bunched up on one line, we collect
12289     ;; them here.
12290     (unless gnus-view-pseudos-separately
12291       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12292             files action)
12293         (while ps
12294           (setq action (cdr (assq 'action (car ps))))
12295           (setq files (list (cdr (assq 'name (car ps)))))
12296           (while (and ps (cdr ps)
12297                       (string= (or action "1")
12298                                (or (cdr (assq 'action (cadr ps))) "2")))
12299             (push (cdr (assq 'name (cadr ps))) files)
12300             (setcdr ps (cddr ps)))
12301           (when files
12302             (when (not (string-match "%s" action))
12303               (push " " files))
12304             (push " " files)
12305             (when (assq 'execute (car ps))
12306               (setcdr (assq 'execute (car ps))
12307                       (funcall (if (string-match "%s" action)
12308                                    'format 'concat)
12309                                action
12310                                (mapconcat
12311                                 (lambda (f)
12312                                   (if (equal f " ")
12313                                       f
12314                                     (shell-quote-argument f)))
12315                                 files " ")))))
12316           (setq ps (cdr ps)))))
12317     (if (and gnus-view-pseudos (not not-view))
12318         (while pslist
12319           (when (assq 'execute (car pslist))
12320             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12321                                   (eq gnus-view-pseudos 'not-confirm)))
12322           (setq pslist (cdr pslist)))
12323       (save-excursion
12324         (while pslist
12325           (setq after-article (or (cdr (assq 'article (car pslist)))
12326                                   (gnus-summary-article-number)))
12327           (gnus-summary-goto-subject after-article)
12328           (forward-line 1)
12329           (setq b (point))
12330           (insert "    " (file-name-nondirectory
12331                           (cdr (assq 'name (car pslist))))
12332                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12333           (setq e (point))
12334           (forward-line -1)             ; back to `b'
12335           (gnus-add-text-properties
12336            b (1- e) (list 'gnus-number gnus-reffed-article-number
12337                           gnus-mouse-face-prop gnus-mouse-face))
12338           (gnus-data-enter
12339            after-article gnus-reffed-article-number
12340            gnus-unread-mark b (car pslist) 0 (- e b))
12341           (setq gnus-newsgroup-unreads
12342                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12343                                          gnus-reffed-article-number))
12344           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12345           (setq pslist (cdr pslist)))))))
12346
12347 (defun gnus-pseudos< (p1 p2)
12348   (let ((c1 (cdr (assq 'action p1)))
12349         (c2 (cdr (assq 'action p2))))
12350     (and c1 c2 (string< c1 c2))))
12351
12352 (defun gnus-request-pseudo-article (props)
12353   (cond ((assq 'execute props)
12354          (gnus-execute-command (cdr (assq 'execute props)))))
12355   (let ((gnus-current-article (gnus-summary-article-number)))
12356     (gnus-run-hooks 'gnus-mark-article-hook)))
12357
12358 (defun gnus-execute-command (command &optional automatic)
12359   (save-excursion
12360     (gnus-article-setup-buffer)
12361     (set-buffer gnus-article-buffer)
12362     (setq buffer-read-only nil)
12363     (let ((command (if automatic command
12364                      (read-string "Command: " (cons command 0)))))
12365       (erase-buffer)
12366       (insert "$ " command "\n\n")
12367       (if gnus-view-pseudo-asynchronously
12368           (start-process "gnus-execute" (current-buffer) shell-file-name
12369                          shell-command-switch command)
12370         (call-process shell-file-name nil t nil
12371                       shell-command-switch command)))))
12372
12373 ;; Summary kill commands.
12374
12375 (defun gnus-summary-edit-global-kill (article)
12376   "Edit the \"global\" kill file."
12377   (interactive (list (gnus-summary-article-number)))
12378   (gnus-group-edit-global-kill article))
12379
12380 (defun gnus-summary-edit-local-kill ()
12381   "Edit a local kill file applied to the current newsgroup."
12382   (interactive)
12383   (setq gnus-current-headers (gnus-summary-article-header))
12384   (gnus-group-edit-local-kill
12385    (gnus-summary-article-number) gnus-newsgroup-name))
12386
12387 ;;; Header reading.
12388
12389 (defun gnus-read-header (id &optional header)
12390   "Read the headers of article ID and enter them into the Gnus system."
12391   (let ((group gnus-newsgroup-name)
12392         (gnus-override-method
12393          (or
12394           gnus-override-method
12395           (and (gnus-news-group-p gnus-newsgroup-name)
12396                (car (gnus-refer-article-methods)))))
12397         where)
12398     ;; First we check to see whether the header in question is already
12399     ;; fetched.
12400     (if (stringp id)
12401         ;; This is a Message-ID.
12402         (setq header (or header (gnus-id-to-header id)))
12403       ;; This is an article number.
12404       (setq header (or header (gnus-summary-article-header id))))
12405     (if (and header
12406              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12407         ;; We have found the header.
12408         header
12409       ;; We have to really fetch the header to this article.
12410       (with-current-buffer nntp-server-buffer
12411         (when (setq where (gnus-request-head id group))
12412           (nnheader-fold-continuation-lines)
12413           (goto-char (point-max))
12414           (insert ".\n")
12415           (goto-char (point-min))
12416           (insert "211 ")
12417           (princ (cond
12418                   ((numberp id) id)
12419                   ((cdr where) (cdr where))
12420                   (header (mail-header-number header))
12421                   (t gnus-reffed-article-number))
12422                  (current-buffer))
12423           (insert " Article retrieved.\n"))
12424         (if (or (not where)
12425                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12426             ()                          ; Malformed head.
12427           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12428             (when (and (bound-and-true-p gnus-registry-enabled)
12429                        (not (gnus-ephemeral-group-p (car where))))
12430               (gnus-registry-handle-action
12431                (mail-header-id header) nil
12432                (gnus-group-prefixed-name (car where) gnus-override-method)
12433                (mail-header-subject header)
12434                (mail-header-from header)))
12435             (when (and (stringp id)
12436                        (or
12437                         (not (string= (gnus-group-real-name group)
12438                                       (car where)))
12439                         (not (gnus-server-equal gnus-override-method
12440                                                 (gnus-group-method group)))))
12441               ;; If we fetched by Message-ID and the article came from
12442               ;; a different group (or server), we fudge some bogus
12443               ;; article numbers for this article.
12444               (mail-header-set-number header gnus-reffed-article-number))
12445             (with-current-buffer gnus-summary-buffer
12446               (decf gnus-reffed-article-number)
12447               (gnus-remove-header (mail-header-number header))
12448               (push header gnus-newsgroup-headers)
12449               (setq gnus-current-headers header)
12450               (push (mail-header-number header) gnus-newsgroup-limit)))
12451           header)))))
12452
12453 (defun gnus-remove-header (number)
12454   "Remove header NUMBER from `gnus-newsgroup-headers'."
12455   (if (and gnus-newsgroup-headers
12456            (= number (mail-header-number (car gnus-newsgroup-headers))))
12457       (pop gnus-newsgroup-headers)
12458     (let ((headers gnus-newsgroup-headers))
12459       (while (and (cdr headers)
12460                   (not (= number (mail-header-number (cadr headers)))))
12461         (pop headers))
12462       (when (cdr headers)
12463         (setcdr headers (cddr headers))))))
12464
12465 ;;;
12466 ;;; summary highlights
12467 ;;;
12468
12469 (defun gnus-highlight-selected-summary ()
12470   "Highlight selected article in summary buffer."
12471   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12472   (when gnus-summary-selected-face
12473     (save-excursion
12474       (let* ((beg (point-at-bol))
12475              (end (point-at-eol))
12476              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12477              (from (if (get-text-property beg gnus-mouse-face-prop)
12478                        beg
12479                      (or (next-single-property-change
12480                           beg gnus-mouse-face-prop nil end)
12481                          beg)))
12482              (to
12483               (if (= from end)
12484                   (- from 2)
12485                 (or (next-single-property-change
12486                      from gnus-mouse-face-prop nil end)
12487                     end))))
12488         ;; If no mouse-face prop on line we will have to = from = end,
12489         ;; so we highlight the entire line instead.
12490         (when (= (+ to 2) from)
12491           (setq from beg)
12492           (setq to end))
12493         (if gnus-newsgroup-selected-overlay
12494             ;; Move old overlay.
12495             (gnus-move-overlay
12496              gnus-newsgroup-selected-overlay from to (current-buffer))
12497           ;; Create new overlay.
12498           (gnus-overlay-put
12499            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12500            'face gnus-summary-selected-face))))))
12501
12502 (defvar gnus-summary-highlight-line-cached nil)
12503 (defvar gnus-summary-highlight-line-trigger nil)
12504
12505 (defun gnus-summary-highlight-line-0 ()
12506   (if (and (eq gnus-summary-highlight-line-trigger
12507                gnus-summary-highlight)
12508            gnus-summary-highlight-line-cached)
12509       gnus-summary-highlight-line-cached
12510     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12511           gnus-summary-highlight-line-cached
12512           (let* ((cond (list 'cond))
12513                  (c cond)
12514                  (list gnus-summary-highlight))
12515             (while list
12516               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12517                               nil))
12518               (setq c (cdr c)
12519                     list (cdr list)))
12520             (gnus-byte-compile (list 'lambda nil cond))))))
12521
12522 (defun gnus-summary-highlight-line ()
12523   "Highlight current line according to `gnus-summary-highlight'."
12524   (let* ((beg (point-at-bol))
12525          (article (or (gnus-summary-article-number) gnus-current-article))
12526          (score (or (cdr (assq article
12527                                gnus-newsgroup-scored))
12528                     gnus-summary-default-score 0))
12529          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12530          (inhibit-read-only t)
12531          (default gnus-summary-default-score)
12532          (default-high gnus-summary-default-high-score)
12533          (default-low gnus-summary-default-low-score)
12534          (uncached (and gnus-summary-use-undownloaded-faces
12535                         (memq article gnus-newsgroup-undownloaded)
12536                         (not (memq article gnus-newsgroup-cached)))))
12537     (let ((face (funcall (gnus-summary-highlight-line-0))))
12538       (unless (eq face (gnus-get-text-property-excluding-characters-with-faces beg 'face))
12539         (gnus-put-text-property-excluding-characters-with-faces
12540          beg (point-at-eol) 'face
12541          (setq face (if (boundp face) (symbol-value face) face)))
12542         (when gnus-summary-highlight-line-function
12543           (funcall gnus-summary-highlight-line-function article face))))))
12544
12545 (defun gnus-update-read-articles (group unread &optional compute)
12546   "Update the list of read articles in GROUP.
12547 UNREAD is a sorted list."
12548   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12549         (info (gnus-get-info group))
12550         (prev 1)
12551         read)
12552     (if (or (not info) (not active))
12553         ;; There is no info on this group if it was, in fact,
12554         ;; killed.  Gnus stores no information on killed groups, so
12555         ;; there's nothing to be done.
12556         ;; One could store the information somewhere temporarily,
12557         ;; perhaps...  Hmmm...
12558         ()
12559       ;; Remove any negative articles numbers.
12560       (while (and unread (< (car unread) 0))
12561         (setq unread (cdr unread)))
12562       ;; Remove any expired article numbers
12563       (while (and unread (< (car unread) (car active)))
12564         (setq unread (cdr unread)))
12565       ;; Compute the ranges of read articles by looking at the list of
12566       ;; unread articles.
12567       (while unread
12568         (when (/= (car unread) prev)
12569           (push (if (= prev (1- (car unread))) prev
12570                   (cons prev (1- (car unread))))
12571                 read))
12572         (setq prev (1+ (car unread)))
12573         (setq unread (cdr unread)))
12574       (when (<= prev (cdr active))
12575         (push (cons prev (cdr active)) read))
12576       (setq read (if (> (length read) 1) (nreverse read) read))
12577       (if compute
12578           read
12579         (save-excursion
12580           (let (setmarkundo)
12581             ;; Propagate the read marks to the backend.
12582             (when (and (gnus-method-option-p
12583                         (gnus-find-method-for-group group)
12584                         'server-marks)
12585                        (gnus-check-backend-function 'request-set-mark group))
12586               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12587                     (add (gnus-remove-from-range read (gnus-info-read info))))
12588                 (when (or add del)
12589                   (unless (gnus-check-group group)
12590                     (error "Can't open server for %s" group))
12591                   (gnus-request-set-mark
12592                    group (delq nil (list (if add (list add 'add '(read)))
12593                                          (if del (list del 'del '(read))))))
12594                   (setq setmarkundo
12595                         `(gnus-request-set-mark
12596                           ,group
12597                           ',(delq nil (list
12598                                        (if del (list del 'add '(read)))
12599                                        (if add (list add 'del '(read))))))))))
12600             (set-buffer gnus-group-buffer)
12601             (gnus-undo-register
12602               `(progn
12603                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12604                  (gnus-info-set-read ',info ',(gnus-info-read info))
12605                  (gnus-get-unread-articles-in-group ',info
12606                                                     (gnus-active ,group))
12607                  (gnus-group-update-group ,group t)
12608                  ,setmarkundo))))
12609         ;; Enter this list into the group info.
12610         (gnus-info-set-read info read)
12611         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12612         (gnus-get-unread-articles-in-group info (gnus-active group))
12613         t))))
12614
12615 (defun gnus-offer-save-summaries ()
12616   "Offer to save all active summary buffers."
12617   (let (buffers)
12618     ;; Go through all buffers and find all summaries.
12619     (dolist (buffer (buffer-list))
12620       (when (and (setq buffer (buffer-name buffer))
12621                  (string-match "Summary" buffer)
12622                  (with-current-buffer buffer
12623                    ;; We check that this is, indeed, a summary buffer.
12624                    (and (eq major-mode 'gnus-summary-mode)
12625                         ;; Also make sure this isn't bogus.
12626                         gnus-newsgroup-prepared
12627                         ;; Also make sure that this isn't a
12628                         ;; dead summary buffer.
12629                         (not gnus-dead-summary-mode))))
12630         (push buffer buffers)))
12631     ;; Go through all these summary buffers and offer to save them.
12632     (when buffers
12633       (save-excursion
12634         (if (eq gnus-interactive-exit 'quiet)
12635             (dolist (buffer buffers)
12636               (switch-to-buffer buffer)
12637               (gnus-summary-exit))
12638           (map-y-or-n-p
12639            "Update summary buffer %s? "
12640            (lambda (buf)
12641              (switch-to-buffer buf)
12642              (gnus-summary-exit))
12643            buffers))))))
12644
12645 (defun gnus-summary-setup-default-charset ()
12646   "Setup newsgroup default charset."
12647   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12648       (setq gnus-newsgroup-charset nil)
12649     (let* ((ignored-charsets
12650             (or gnus-newsgroup-ephemeral-ignored-charsets
12651                 (append
12652                  (and gnus-newsgroup-name
12653                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12654                  gnus-newsgroup-ignored-charsets))))
12655       (setq gnus-newsgroup-charset
12656             (or gnus-newsgroup-ephemeral-charset
12657                 (and gnus-newsgroup-name
12658                      (gnus-parameter-charset gnus-newsgroup-name))
12659                 gnus-default-charset))
12660       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12661            ignored-charsets))))
12662
12663 ;;;
12664 ;;; Mime Commands
12665 ;;;
12666
12667 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12668   "Display the current article buffer fully MIME-buttonized.
12669 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12670 treated as multipart/mixed."
12671   (interactive "P")
12672   (require 'gnus-art)
12673   (let ((gnus-unbuttonized-mime-types nil)
12674         (gnus-mime-display-multipart-as-mixed show-all-parts))
12675     (gnus-summary-show-article)))
12676
12677 (defun gnus-summary-repair-multipart (article)
12678   "Add a Content-Type header to a multipart article without one."
12679   (interactive (list (gnus-summary-article-number)))
12680   (gnus-with-article article
12681     (message-narrow-to-head)
12682     (message-remove-header "Mime-Version")
12683     (goto-char (point-max))
12684     (insert "Mime-Version: 1.0\n")
12685     (widen)
12686     (when (search-forward "\n--" nil t)
12687       (let ((separator (buffer-substring (point) (point-at-eol))))
12688         (message-narrow-to-head)
12689         (message-remove-header "Content-Type")
12690         (goto-char (point-max))
12691         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12692                         separator))
12693         (widen))))
12694   (let (gnus-mark-article-hook)
12695     (gnus-summary-select-article t t nil article)))
12696
12697 (defun gnus-summary-toggle-display-buttonized ()
12698   "Toggle the buttonizing of the article buffer."
12699   (interactive)
12700   (require 'gnus-art)
12701   (if (setq gnus-inhibit-mime-unbuttonizing
12702             (not gnus-inhibit-mime-unbuttonizing))
12703       (let ((gnus-unbuttonized-mime-types nil))
12704         (gnus-summary-show-article))
12705     (gnus-summary-show-article)))
12706
12707 ;;;
12708 ;;; Generic summary marking commands
12709 ;;;
12710
12711 (defvar gnus-summary-marking-alist
12712   '((read gnus-del-mark "d")
12713     (unread gnus-unread-mark "u")
12714     (ticked gnus-ticked-mark "!")
12715     (dormant gnus-dormant-mark "?")
12716     (expirable gnus-expirable-mark "e"))
12717   "An alist of names/marks/keystrokes.")
12718
12719 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12720 (defvar gnus-summary-mark-map)
12721
12722 (defun gnus-summary-make-all-marking-commands ()
12723   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12724   (dolist (elem gnus-summary-marking-alist)
12725     (apply 'gnus-summary-make-marking-command elem)))
12726
12727 (defun gnus-summary-make-marking-command (name mark keystroke)
12728   (let ((map (make-sparse-keymap)))
12729     (define-key gnus-summary-generic-mark-map keystroke map)
12730     (dolist (lway `((next "next" next nil "n")
12731                     (next-unread "next unread" next t "N")
12732                     (prev "previous" prev nil "p")
12733                     (prev-unread "previous unread" prev t "P")
12734                     (nomove "" nil nil ,keystroke)))
12735       (let ((func (gnus-summary-make-marking-command-1
12736                    mark (car lway) lway name)))
12737         (setq func (eval func))
12738         (define-key map (nth 4 lway) func)))))
12739
12740 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12741   `(defun ,(intern
12742             (format "gnus-summary-put-mark-as-%s%s"
12743                     name (if (eq way 'nomove)
12744                              ""
12745                            (concat "-" (symbol-name way)))))
12746      (n)
12747      ,(format
12748        "Mark the current article as %s%s.
12749 If N, the prefix, then repeat N times.
12750 If N is negative, move in reverse order.
12751 The difference between N and the actual number of articles marked is
12752 returned."
12753        name (cadr lway))
12754      (interactive "p")
12755      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12756
12757 (defun gnus-summary-generic-mark (n mark move unread)
12758   "Mark N articles with MARK."
12759   (unless (eq major-mode 'gnus-summary-mode)
12760     (error "This command can only be used in the summary buffer"))
12761   (gnus-summary-show-thread)
12762   (let ((nummove
12763          (cond
12764           ((eq move 'next) 1)
12765           ((eq move 'prev) -1)
12766           (t 0))))
12767     (if (zerop nummove)
12768         (setq n 1)
12769       (when (< n 0)
12770         (setq n (abs n)
12771               nummove (* -1 nummove))))
12772     (while (and (> n 0)
12773                 (gnus-summary-mark-article nil mark)
12774                 (zerop (gnus-summary-next-subject nummove unread t)))
12775       (setq n (1- n)))
12776     (when (/= 0 n)
12777       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12778     (gnus-summary-recenter)
12779     (gnus-summary-position-point)
12780     (gnus-set-mode-line 'summary)
12781     n))
12782
12783 (defun gnus-summary-insert-articles (articles)
12784   (when (setq articles
12785               (gnus-sorted-difference articles
12786                                       (mapcar (lambda (h)
12787                                                 (mail-header-number h))
12788                                               gnus-newsgroup-headers)))
12789     (setq gnus-newsgroup-headers
12790           (gnus-merge 'list
12791                       gnus-newsgroup-headers
12792                       (gnus-fetch-headers articles)
12793                       'gnus-article-sort-by-number))
12794     (setq gnus-newsgroup-articles
12795           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12796     ;; Suppress duplicates?
12797     (when gnus-suppress-duplicates
12798       (gnus-dup-suppress-articles))
12799
12800     (if (and gnus-fetch-old-headers
12801              (eq gnus-headers-retrieved-by 'nov))
12802         ;; We might want to build some more threads first.
12803         (if (eq gnus-fetch-old-headers 'invisible)
12804             (gnus-build-all-threads)
12805           (gnus-build-old-threads))
12806       ;; Mark the inserted articles that are unread as unread.
12807       (setq gnus-newsgroup-unreads
12808             (gnus-sorted-nunion
12809              gnus-newsgroup-unreads
12810              (gnus-sorted-nintersection
12811               (gnus-list-of-unread-articles gnus-newsgroup-name)
12812               articles)))
12813       ;; Mark the inserted articles as selected so that the information
12814       ;; of the marks having been changed by a user may be updated when
12815       ;; exiting this group.  See `gnus-summary-update-info'.
12816       (dolist (art articles)
12817         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12818     ;; Let the Gnus agent mark articles as read.
12819     (when gnus-agent
12820       (gnus-agent-get-undownloaded-list))
12821     ;; Remove list identifiers from subject
12822     (gnus-summary-remove-list-identifiers)
12823     ;; First and last article in this newsgroup.
12824     (when gnus-newsgroup-headers
12825       (setq gnus-newsgroup-begin
12826             (mail-header-number (car gnus-newsgroup-headers))
12827             gnus-newsgroup-end
12828             (mail-header-number
12829              (gnus-last-element gnus-newsgroup-headers))))
12830     (when gnus-use-scoring
12831       (gnus-possibly-score-headers))))
12832
12833 (defun gnus-summary-insert-old-articles (&optional all)
12834   "Insert all old articles in this group.
12835 If ALL is non-nil, already read articles become readable.
12836 If ALL is a number, fetch this number of articles."
12837   (interactive "P")
12838   (prog1
12839       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12840             older len)
12841         (setq older
12842               ;; Some nntp servers lie about their active range.  When
12843               ;; this happens, the active range can be in the millions.
12844               ;; Use a compressed range to avoid creating a huge list.
12845               (gnus-range-difference
12846                (gnus-range-difference (list gnus-newsgroup-active) old)
12847                gnus-newsgroup-unexist))
12848         (setq len (gnus-range-length older))
12849         (cond
12850          ((null older) nil)
12851          ((numberp all)
12852           (if (< all len)
12853               (let ((older-range (nreverse older)))
12854                 (setq older nil)
12855
12856                 (while (> all 0)
12857                   (let* ((r (pop older-range))
12858                          (min (if (numberp r) r (car r)))
12859                          (max (if (numberp r) r (cdr r))))
12860                     (while (and (<= min max)
12861                                 (> all 0))
12862                       (push max older)
12863                       (setq all (1- all)
12864                             max (1- max))))))
12865             (setq older (gnus-uncompress-range older))))
12866          (all
12867           (setq older (gnus-uncompress-range older)))
12868          (t
12869           (when (and (numberp gnus-large-newsgroup)
12870                    (> len gnus-large-newsgroup))
12871               (let* ((cursor-in-echo-area nil)
12872                      (initial (gnus-parameter-large-newsgroup-initial
12873                                gnus-newsgroup-name))
12874                      (input
12875                       (read-string
12876                        (format
12877                         "How many articles from %s (%s %d): "
12878                         (gnus-group-decoded-name gnus-newsgroup-name)
12879                         (if initial "max" "default")
12880                         len)
12881                        nil nil
12882                        (and initial
12883                             (number-to-string initial)))))
12884                 (unless (string-match "^[ \t]*$" input)
12885                   (setq all (string-to-number input))
12886                   (if (< all len)
12887                       (let ((older-range (nreverse older)))
12888                         (setq older nil)
12889
12890                         (while (> all 0)
12891                           (let* ((r (pop older-range))
12892                                  (min (if (numberp r) r (car r)))
12893                                  (max (if (numberp r) r (cdr r))))
12894                             (while (and (<= min max)
12895                                         (> all 0))
12896                               (push max older)
12897                               (setq all (1- all)
12898                                     max (1- max))))))))))
12899           (setq older (gnus-uncompress-range older))))
12900         (if (not older)
12901             (message "No old news.")
12902           (gnus-summary-insert-articles older)
12903           (gnus-summary-limit (gnus-sorted-nunion old older))))
12904     (gnus-summary-position-point)))
12905
12906 (defun gnus-summary-insert-new-articles ()
12907   "Insert all new articles in this group."
12908   (interactive)
12909   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12910         (old-high gnus-newsgroup-highest)
12911         (nnmail-fetched-sources (list t))
12912         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12913         i new)
12914     (unless new-active
12915       (error "Couldn't fetch new data"))
12916     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12917     (setq i (cdr gnus-newsgroup-active)
12918           gnus-newsgroup-highest i)
12919     (while (> i old-high)
12920       (push i new)
12921       (decf i))
12922     (if (not new)
12923         (message "No gnus is bad news")
12924       (gnus-summary-insert-articles new)
12925       (setq gnus-newsgroup-unreads
12926             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12927       (gnus-summary-limit (gnus-sorted-nunion old new))))
12928   (gnus-summary-position-point))
12929
12930 ;;; Bookmark support for Gnus.
12931 (declare-function bookmark-make-record-default
12932                   "bookmark" (&optional no-file no-context posn))
12933 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12934 (declare-function bookmark-default-handler "bookmark" (bmk))
12935 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12936 (defvar bookmark-yank-point)
12937 (defvar bookmark-current-buffer)
12938
12939 (defun gnus-summary-bookmark-make-record ()
12940   "Make a bookmark entry for a Gnus summary buffer."
12941   (let (pos buf)
12942     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12943       (save-restriction              ; FIXME is it necessary to widen?
12944         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12945       (setq buf "art") ; We are recording bookmark from article buffer.
12946       (setq bookmark-yank-point (point))
12947       (setq bookmark-current-buffer (current-buffer))
12948       (gnus-article-show-summary))      ; Go back in summary buffer.
12949     ;; We are now recording bookmark from summary buffer.
12950     (unless buf (setq buf "sum"))
12951     (let* ((subject (elt (gnus-summary-article-header) 1))
12952            (grp     (car gnus-article-current))
12953            (art     (cdr gnus-article-current))
12954            (head    (gnus-summary-article-header art))
12955            (id      (mail-header-id head)))
12956       `(,subject
12957         ,@(condition-case nil
12958               (bookmark-make-record-default 'no-file 'no-context pos)
12959             (wrong-number-of-arguments
12960              (bookmark-make-record-default 'point-only)))
12961         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12962         (group . ,grp) (article . ,art)
12963         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12964
12965 ;;;###autoload
12966 (defun gnus-summary-bookmark-jump (bookmark)
12967   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12968 BOOKMARK is a bookmark name or a bookmark record."
12969   (let ((group    (bookmark-prop-get bookmark 'group))
12970         (article  (bookmark-prop-get bookmark 'article))
12971         (id       (bookmark-prop-get bookmark 'message-id))
12972         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12973     (gnus-fetch-group group (list article))
12974     (gnus-summary-insert-cached-articles)
12975     (gnus-summary-goto-article id nil 'force)
12976     ;; FIXME we have to wait article buffer is ready (only large buffer)
12977     ;; Is there a better solution to know that?
12978     ;; If we don't wait `bookmark-default-handler' will have no chance
12979     ;; to set position. However there is no error, just wrong pos.
12980     (sit-for 1)
12981     (when (string= buf "Gnus-art")
12982       (other-window 1))
12983     (bookmark-default-handler
12984      `(""
12985        (buffer . ,(current-buffer))
12986        . ,(bookmark-get-bookmark-record bookmark)))))
12987
12988 (gnus-summary-make-all-marking-commands)
12989
12990 (gnus-ems-redefine)
12991
12992 (provide 'gnus-sum)
12993
12994 (run-hooks 'gnus-sum-load-hook)
12995
12996 ;; Local Variables:
12997 ;; coding: iso-8859-1
12998 ;; End:
12999
13000 ;;; gnus-sum.el ends here