Merge remote branch 'origin/no-gnus'
[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 2
455   "*If non-nil, always center the current summary buffer.
456 In particular, if `vertical' do only vertical recentering.  If non-nil
457 and non-`vertical', do both horizontal and vertical recentering."
458   :group 'gnus-summary-maneuvering
459   :type '(choice (const :tag "none" nil)
460                  (const vertical)
461                  (integer :tag "height")
462                  (sexp :menu-tag "both" t)))
463
464 (defcustom gnus-auto-center-group t
465   "If non-nil, always center the group buffer."
466   :group 'gnus-summary-maneuvering
467   :type 'boolean)
468
469 (defcustom gnus-show-all-headers nil
470   "*If non-nil, don't hide any headers."
471   :group 'gnus-article-hiding
472   :group 'gnus-article-headers
473   :type 'boolean)
474
475 (defcustom gnus-summary-ignore-duplicates nil
476   "*If non-nil, ignore articles with identical Message-ID headers."
477   :group 'gnus-summary
478   :type 'boolean)
479
480 (defcustom gnus-single-article-buffer nil
481   "*If non-nil, display all articles in the same buffer.
482 If nil, each group will get its own article buffer."
483   :version "24.1"
484   :group 'gnus-article-various
485   :type 'boolean)
486
487 (defcustom gnus-widen-article-window nil
488   "If non-nil, selecting the article buffer will display only the article buffer."
489   :version "24.1"
490   :group 'gnus-article-various
491   :type 'boolean)
492
493 (defcustom gnus-break-pages t
494   "*If non-nil, do page breaking on articles.
495 The page delimiter is specified by the `gnus-page-delimiter'
496 variable."
497   :group 'gnus-article-various
498   :type 'boolean)
499
500 (defcustom gnus-move-split-methods nil
501   "*Variable used to suggest where articles are to be moved to.
502 It uses the same syntax as the `gnus-split-methods' variable.
503 However, whereas `gnus-split-methods' specifies file names as targets,
504 this variable specifies group names."
505   :group 'gnus-summary-mail
506   :type '(repeat (choice (list :value (fun) function)
507                          (cons :value ("" "") regexp (repeat string))
508                          (sexp :value nil))))
509
510 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
511   "Function used to compute default prefix for article move/copy/etc prompts.
512 The function should take one argument, a group name, and return a
513 string with the suggested prefix."
514   :group 'gnus-summary-mail
515   :type 'function)
516
517 ;; FIXME: Although the custom type is `character' for the following variables,
518 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
519
520 (defcustom gnus-unread-mark ?           ;Whitespace
521   "*Mark used for unread articles."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-ticked-mark ?!
526   "*Mark used for ticked articles."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-dormant-mark ??
531   "*Mark used for dormant articles."
532   :group 'gnus-summary-marks
533   :type 'character)
534
535 (defcustom gnus-del-mark ?r
536   "*Mark used for del'd articles."
537   :group 'gnus-summary-marks
538   :type 'character)
539
540 (defcustom gnus-read-mark ?R
541   "*Mark used for read articles."
542   :group 'gnus-summary-marks
543   :type 'character)
544
545 (defcustom gnus-expirable-mark ?E
546   "*Mark used for expirable articles."
547   :group 'gnus-summary-marks
548   :type 'character)
549
550 (defcustom gnus-killed-mark ?K
551   "*Mark used for killed articles."
552   :group 'gnus-summary-marks
553   :type 'character)
554
555 (defcustom gnus-spam-mark ?$
556   "*Mark used for spam articles."
557   :version "22.1"
558   :group 'gnus-summary-marks
559   :type 'character)
560
561 (defcustom gnus-kill-file-mark ?X
562   "*Mark used for articles killed by kill files."
563   :group 'gnus-summary-marks
564   :type 'character)
565
566 (defcustom gnus-low-score-mark ?Y
567   "*Mark used for articles with a low score."
568   :group 'gnus-summary-marks
569   :type 'character)
570
571 (defcustom gnus-catchup-mark ?C
572   "*Mark used for articles that are caught up."
573   :group 'gnus-summary-marks
574   :type 'character)
575
576 (defcustom gnus-replied-mark ?A
577   "*Mark used for articles that have been replied to."
578   :group 'gnus-summary-marks
579   :type 'character)
580
581 (defcustom gnus-forwarded-mark ?F
582   "*Mark used for articles that have been forwarded."
583   :version "22.1"
584   :group 'gnus-summary-marks
585   :type 'character)
586
587 (defcustom gnus-recent-mark ?N
588   "*Mark used for articles that are recent."
589   :version "22.1"
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-cached-mark ?*
594   "*Mark used for articles that are in the cache."
595   :group 'gnus-summary-marks
596   :type 'character)
597
598 (defcustom gnus-saved-mark ?S
599   "*Mark used for articles that have been saved."
600   :group 'gnus-summary-marks
601   :type 'character)
602
603 (defcustom gnus-unseen-mark ?.
604   "*Mark used for articles that haven't been seen."
605   :version "22.1"
606   :group 'gnus-summary-marks
607   :type 'character)
608
609 (defcustom gnus-no-mark ?               ;Whitespace
610   "*Mark used for articles that have no other secondary mark."
611   :version "22.1"
612   :group 'gnus-summary-marks
613   :type 'character)
614
615 (defcustom gnus-ancient-mark ?O
616   "*Mark used for ancient articles."
617   :group 'gnus-summary-marks
618   :type 'character)
619
620 (defcustom gnus-sparse-mark ?Q
621   "*Mark used for sparsely reffed articles."
622   :group 'gnus-summary-marks
623   :type 'character)
624
625 (defcustom gnus-canceled-mark ?G
626   "*Mark used for canceled articles."
627   :group 'gnus-summary-marks
628   :type 'character)
629
630 (defcustom gnus-duplicate-mark ?M
631   "*Mark used for duplicate articles."
632   :group 'gnus-summary-marks
633   :type 'character)
634
635 (defcustom gnus-undownloaded-mark ?-
636   "*Mark used for articles that weren't downloaded."
637   :version "22.1"
638   :group 'gnus-summary-marks
639   :type 'character)
640
641 (defcustom gnus-downloaded-mark ?+
642   "*Mark used for articles that were downloaded."
643   :group 'gnus-summary-marks
644   :type 'character)
645
646 (defcustom gnus-downloadable-mark ?%
647   "*Mark used for articles that are to be downloaded."
648   :group 'gnus-summary-marks
649   :type 'character)
650
651 (defcustom gnus-unsendable-mark ?=
652   "*Mark used for articles that won't be sent."
653   :group 'gnus-summary-marks
654   :type 'character)
655
656 (defcustom gnus-score-over-mark ?+
657   "*Score mark used for articles with high scores."
658   :group 'gnus-summary-marks
659   :type 'character)
660
661 (defcustom gnus-score-below-mark ?-
662   "*Score mark used for articles with low scores."
663   :group 'gnus-summary-marks
664   :type 'character)
665
666 (defcustom gnus-empty-thread-mark ?     ;Whitespace
667   "*There is no thread under the article."
668   :group 'gnus-summary-marks
669   :type 'character)
670
671 (defcustom gnus-not-empty-thread-mark ?=
672   "*There is a thread under the article."
673   :group 'gnus-summary-marks
674   :type 'character)
675
676 (defcustom gnus-view-pseudo-asynchronously nil
677   "*If non-nil, Gnus will view pseudo-articles asynchronously."
678   :group 'gnus-extract-view
679   :type 'boolean)
680
681 (defcustom gnus-auto-expirable-marks
682   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
683         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
684         gnus-duplicate-mark)
685   "*The list of marks converted into expiration if a group is auto-expirable."
686   :version "24.1"
687   :group 'gnus-summary
688   :type '(repeat character))
689
690 (defcustom gnus-inhibit-user-auto-expire t
691   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
692   :version "21.1"
693   :group 'gnus-summary
694   :type 'boolean)
695
696 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
697   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
698 If nil, the expirable marks will be unchanged except that the marks
699 will be removed when copying or moving articles to a group that has
700 not turned auto-expire on.  If non-nil, articles that have been read
701 will be marked as expirable when being copied or moved to a group in
702 which auto-expire is turned on."
703   :version "23.2"
704   :type 'boolean
705   :group 'gnus-summary-marks)
706
707 (defcustom gnus-view-pseudos nil
708   "*If `automatic', pseudo-articles will be viewed automatically.
709 If `not-confirm', pseudos will be viewed automatically, and the user
710 will not be asked to confirm the command."
711   :group 'gnus-extract-view
712   :type '(choice (const :tag "off" nil)
713                  (const automatic)
714                  (const not-confirm)))
715
716 (defcustom gnus-view-pseudos-separately t
717   "*If non-nil, one pseudo-article will be created for each file to be viewed.
718 If nil, all files that use the same viewing command will be given as a
719 list of parameters to that command."
720   :group 'gnus-extract-view
721   :type 'boolean)
722
723 (defcustom gnus-insert-pseudo-articles t
724   "*If non-nil, insert pseudo-articles when decoding articles."
725   :group 'gnus-extract-view
726   :type 'boolean)
727
728 (defcustom gnus-summary-dummy-line-format
729   "   %(:                             :%) %S\n"
730   "*The format specification for the dummy roots in the summary buffer.
731 It works along the same lines as a normal formatting string,
732 with some simple extensions.
733
734 %S  The subject
735
736 General format specifiers can also be used.
737 See `(gnus)Formatting Variables'."
738   :link '(custom-manual "(gnus)Formatting Variables")
739   :group 'gnus-threading
740   :type 'string)
741
742 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
743   "*The format specification for the summary mode line.
744 It works along the same lines as a normal formatting string,
745 with some simple extensions:
746
747 %G  Group name
748 %p  Unprefixed group name
749 %A  Current article number
750 %z  Current article score
751 %V  Gnus version
752 %U  Number of unread articles in the group
753 %e  Number of unselected articles in the group
754 %Z  A string with unread/unselected article counts
755 %g  Shortish group name
756 %S  Subject of the current article
757 %u  User-defined spec
758 %s  Current score file name
759 %d  Number of dormant articles
760 %r  Number of articles that have been marked as read in this session
761 %E  Number of articles expunged by the score files"
762   :group 'gnus-summary-format
763   :type 'string)
764
765 (defcustom gnus-list-identifiers nil
766   "Regexp that matches list identifiers to be removed from subject.
767 This can also be a list of regexps."
768   :version "21.1"
769   :group 'gnus-summary-format
770   :group 'gnus-article-hiding
771   :type '(choice (const :tag "none" nil)
772                  (regexp :value ".*")
773                  (repeat :value (".*") regexp)))
774
775 (defcustom gnus-summary-mark-below 0
776   "*Mark all articles with a score below this variable as read.
777 This variable is local to each summary buffer and usually set by the
778 score file."
779   :group 'gnus-score-default
780   :type 'integer)
781
782 (defun gnus-widget-reversible-match (widget value)
783   "Ignoring WIDGET, convert VALUE to internal form.
784 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
785   ;; (debug value)
786   (or (symbolp value)
787       (and (listp value)
788            (eq (length value) 2)
789            (eq (nth 0 value) 'not)
790            (symbolp (nth 1 value)))))
791
792 (defun gnus-widget-reversible-to-internal (widget value)
793   "Ignoring WIDGET, convert VALUE to internal form.
794 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
795 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
796   ;; (debug value)
797   (if (atom value)
798       (list value nil)
799     (list (nth 1 value) t)))
800
801 (defun gnus-widget-reversible-to-external (widget value)
802   "Ignoring WIDGET, convert VALUE to external form.
803 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
804 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
805   ;; (debug value)
806   (if (nth 1 value)
807       (list 'not (nth 0 value))
808     (nth 0 value)))
809
810 (define-widget 'gnus-widget-reversible 'group
811   "A `group' that convert values."
812   :match 'gnus-widget-reversible-match
813   :value-to-internal 'gnus-widget-reversible-to-internal
814   :value-to-external 'gnus-widget-reversible-to-external)
815
816 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
817   "*List of functions used for sorting articles in the summary buffer.
818
819 Each function takes two articles and returns non-nil if the first
820 article should be sorted before the other.  If you use more than one
821 function, the primary sort function should be the last.  You should
822 probably always include `gnus-article-sort-by-number' in the list of
823 sorting functions -- preferably first.  Also note that sorting by date
824 is often much slower than sorting by number, and the sorting order is
825 very similar.  (Sorting by date means sorting by the time the message
826 was sent, sorting by number means sorting by arrival time.)
827
828 Each item can also be a list `(not F)' where F is a function;
829 this reverses the sort order.
830
831 Ready-made functions include `gnus-article-sort-by-number',
832 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
833 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
834 and `gnus-article-sort-by-score'.
835
836 When threading is turned on, the variable `gnus-thread-sort-functions'
837 controls how articles are sorted."
838   :group 'gnus-summary-sort
839   :type '(repeat (gnus-widget-reversible
840                   (choice (function-item gnus-article-sort-by-number)
841                           (function-item gnus-article-sort-by-author)
842                           (function-item gnus-article-sort-by-subject)
843                           (function-item gnus-article-sort-by-date)
844                           (function-item gnus-article-sort-by-score)
845                           (function-item gnus-article-sort-by-random)
846                           (function :tag "other"))
847                   (boolean :tag "Reverse order"))))
848
849
850 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
851   "*List of functions used for sorting threads in the summary buffer.
852 By default, threads are sorted by article number.
853
854 Each function takes two threads and returns non-nil if the first
855 thread should be sorted before the other.  If you use more than one
856 function, the primary sort function should be the last.  You should
857 probably always include `gnus-thread-sort-by-number' in the list of
858 sorting functions -- preferably first.  Also note that sorting by date
859 is often much slower than sorting by number, and the sorting order is
860 very similar.  (Sorting by date means sorting by the time the message
861 was sent, sorting by number means sorting by arrival time.)
862
863 Each list item can also be a list `(not F)' where F is a
864 function; this specifies reversed sort order.
865
866 Ready-made functions include `gnus-thread-sort-by-number',
867 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
868 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
869 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
870 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
871 and `gnus-thread-sort-by-total-score' (see
872 `gnus-thread-score-function').
873
874 When threading is turned off, the variable
875 `gnus-article-sort-functions' controls how articles are sorted."
876   :group 'gnus-summary-sort
877   :type '(repeat
878           (gnus-widget-reversible
879            (choice (function-item gnus-thread-sort-by-number)
880                    (function-item gnus-thread-sort-by-author)
881                    (function-item gnus-thread-sort-by-recipient)
882                    (function-item gnus-thread-sort-by-subject)
883                    (function-item gnus-thread-sort-by-date)
884                    (function-item gnus-thread-sort-by-score)
885                    (function-item gnus-thread-sort-by-most-recent-number)
886                    (function-item gnus-thread-sort-by-most-recent-date)
887                    (function-item gnus-thread-sort-by-random)
888                    (function-item gnus-thread-sort-by-total-score)
889                    (function :tag "other"))
890            (boolean :tag "Reverse order"))))
891
892 (defcustom gnus-thread-score-function '+
893   "*Function used for calculating the total score of a thread.
894
895 The function is called with the scores of the article and each
896 subthread and should then return the score of the thread.
897
898 Some functions you can use are `+', `max', or `min'."
899   :group 'gnus-summary-sort
900   :type 'function)
901
902 (defcustom gnus-summary-expunge-below nil
903   "All articles that have a score less than this variable will be expunged.
904 This variable is local to the summary buffers."
905   :group 'gnus-score-default
906   :type '(choice (const :tag "off" nil)
907                  integer))
908
909 (defcustom gnus-thread-expunge-below nil
910   "All threads that have a total score less than this variable will be expunged.
911 See `gnus-thread-score-function' for en explanation of what a
912 \"thread score\" is.
913
914 This variable is local to the summary buffers."
915   :group 'gnus-threading
916   :group 'gnus-score-default
917   :type '(choice (const :tag "off" nil)
918                  integer))
919
920 (defcustom gnus-summary-mode-hook nil
921   "*A hook for Gnus summary mode.
922 This hook is run before any variables are set in the summary buffer."
923   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
924   :group 'gnus-summary-various
925   :type 'hook)
926
927 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
928 (when (featurep 'xemacs)
929   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
930   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
931   (add-hook 'gnus-summary-mode-hook
932             'gnus-xmas-switch-horizontal-scrollbar-off))
933
934 (defcustom gnus-summary-menu-hook nil
935   "*Hook run after the creation of the summary mode menu."
936   :group 'gnus-summary-visual
937   :type 'hook)
938
939 (defcustom gnus-summary-exit-hook nil
940   "*A hook called on exit from the summary buffer.
941 It will be called with point in the group buffer."
942   :group 'gnus-summary-exit
943   :type 'hook)
944
945 (defcustom gnus-summary-prepare-hook nil
946   "*A hook called after the summary buffer has been generated.
947 If you want to modify the summary buffer, you can use this hook."
948   :group 'gnus-summary-various
949   :type 'hook)
950
951 (defcustom gnus-summary-prepared-hook nil
952   "*A hook called as the last thing after the summary buffer has been generated."
953   :group 'gnus-summary-various
954   :type 'hook)
955
956 (defcustom gnus-summary-generate-hook nil
957   "*A hook run just before generating the summary buffer.
958 This hook is commonly used to customize threading variables and the
959 like."
960   :group 'gnus-summary-various
961   :type 'hook)
962
963 (defcustom gnus-select-group-hook nil
964   "*A hook called when a newsgroup is selected.
965
966 If you'd like to simplify subjects like the
967 `gnus-summary-next-same-subject' command does, you can use the
968 following hook:
969
970  (add-hook gnus-select-group-hook
971            (lambda ()
972              (mapcar (lambda (header)
973                        (mail-header-set-subject
974                         header
975                         (gnus-simplify-subject
976                          (mail-header-subject header) 're-only)))
977                      gnus-newsgroup-headers)))"
978   :group 'gnus-group-select
979   :type 'hook)
980
981 (defcustom gnus-select-article-hook nil
982   "*A hook called when an article is selected."
983   :group 'gnus-summary-choose
984   :options '(gnus-agent-fetch-selected-article)
985   :type 'hook)
986
987 (defcustom gnus-visual-mark-article-hook
988   (list 'gnus-highlight-selected-summary)
989   "*Hook run after selecting an article in the summary buffer.
990 It is meant to be used for highlighting the article in some way.  It
991 is not run if `gnus-visual' is nil."
992   :group 'gnus-summary-visual
993   :type 'hook)
994
995 (defcustom gnus-parse-headers-hook nil
996   "*A hook called before parsing the headers."
997   :group 'gnus-various
998   :type 'hook)
999
1000 (defcustom gnus-exit-group-hook nil
1001   "*A hook called when exiting summary mode.
1002 This hook is not called from the non-updating exit commands like `Q'."
1003   :group 'gnus-various
1004   :type 'hook)
1005
1006 (defcustom gnus-summary-update-hook nil
1007   "*A hook called when a summary line is changed.
1008 The hook will not be called if `gnus-visual' is nil.
1009
1010 The default function `gnus-summary-highlight-line' will
1011 highlight the line according to the `gnus-summary-highlight'
1012 variable."
1013   :group 'gnus-summary-visual
1014   :type 'hook)
1015
1016 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1017   "*A hook called when an article is selected for the first time.
1018 The hook is intended to mark an article as read (or unread)
1019 automatically when it is selected."
1020   :group 'gnus-summary-choose
1021   :type 'hook)
1022
1023 (defcustom gnus-group-no-more-groups-hook nil
1024   "*A hook run when returning to group mode having no more (unread) groups."
1025   :group 'gnus-group-select
1026   :type 'hook)
1027
1028 (defcustom gnus-ps-print-hook nil
1029   "*A hook run before ps-printing something from Gnus."
1030   :group 'gnus-summary
1031   :type 'hook)
1032
1033 (defcustom gnus-summary-article-move-hook nil
1034   "*A hook called after an article is moved, copied, respooled, or crossposted."
1035   :version "22.1"
1036   :group 'gnus-summary
1037   :type 'hook)
1038
1039 (defcustom gnus-summary-article-delete-hook nil
1040   "*A hook called after an article is deleted."
1041   :version "22.1"
1042   :group 'gnus-summary
1043   :type 'hook)
1044
1045 (defcustom gnus-summary-article-expire-hook nil
1046   "*A hook called after an article is expired."
1047   :version "22.1"
1048   :group 'gnus-summary
1049   :type 'hook)
1050
1051 (defcustom gnus-summary-display-arrow
1052   (and (fboundp 'display-graphic-p)
1053        (display-graphic-p))
1054   "*If non-nil, display an arrow highlighting the current article."
1055   :version "22.1"
1056   :group 'gnus-summary
1057   :type 'boolean)
1058
1059 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1060   "Face used for highlighting the current article in the summary buffer."
1061   :group 'gnus-summary-visual
1062   :type 'face)
1063
1064 (defvar gnus-tmp-downloaded nil)
1065
1066 (defcustom gnus-summary-highlight
1067   '(((eq mark gnus-canceled-mark)
1068      . gnus-summary-cancelled)
1069     ((and uncached (> score default-high))
1070      . gnus-summary-high-undownloaded)
1071     ((and uncached (< score default-low))
1072      . gnus-summary-low-undownloaded)
1073     (uncached
1074      . gnus-summary-normal-undownloaded)
1075     ((and (> score default-high)
1076           (or (eq mark gnus-dormant-mark)
1077               (eq mark gnus-ticked-mark)))
1078      . gnus-summary-high-ticked)
1079     ((and (< score default-low)
1080           (or (eq mark gnus-dormant-mark)
1081               (eq mark gnus-ticked-mark)))
1082      . gnus-summary-low-ticked)
1083     ((or (eq mark gnus-dormant-mark)
1084          (eq mark gnus-ticked-mark))
1085      . gnus-summary-normal-ticked)
1086     ((and (> score default-high) (eq mark gnus-ancient-mark))
1087      . gnus-summary-high-ancient)
1088     ((and (< score default-low) (eq mark gnus-ancient-mark))
1089      . gnus-summary-low-ancient)
1090     ((eq mark gnus-ancient-mark)
1091      . gnus-summary-normal-ancient)
1092     ((and (> score default-high) (eq mark gnus-unread-mark))
1093      . gnus-summary-high-unread)
1094     ((and (< score default-low) (eq mark gnus-unread-mark))
1095      . gnus-summary-low-unread)
1096     ((eq mark gnus-unread-mark)
1097      . gnus-summary-normal-unread)
1098     ((> score default-high)
1099      . gnus-summary-high-read)
1100     ((< score default-low)
1101      . gnus-summary-low-read)
1102     (t
1103      . gnus-summary-normal-read))
1104   "*Controls the highlighting of summary buffer lines.
1105
1106 A list of (FORM . FACE) pairs.  When deciding how a particular
1107 summary line should be displayed, each form is evaluated.  The content
1108 of the face field after the first true form is used.  You can change
1109 how those summary lines are displayed, by editing the face field.
1110
1111 You can use the following variables in the FORM field.
1112
1113 score:        The article's score.
1114 default:      The default article score.
1115 default-high: The default score for high scored articles.
1116 default-low:  The default score for low scored articles.
1117 below:        The score below which articles are automatically marked as read.
1118 mark:         The article's mark.
1119 uncached:     Non-nil if the article is uncached."
1120   :group 'gnus-summary-visual
1121   :type '(repeat (cons (sexp :tag "Form" nil)
1122                        face)))
1123 (put 'gnus-summary-highlight 'risky-local-variable t)
1124
1125 (defcustom gnus-alter-header-function nil
1126   "Function called to allow alteration of article header structures.
1127 The function is called with one parameter, the article header vector,
1128 which it may alter in any way."
1129   :type '(choice (const :tag "None" nil)
1130                  function)
1131   :group 'gnus-summary)
1132
1133 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1134   "Function used to decode a string with encoded words.")
1135
1136 (defvar gnus-decode-encoded-address-function
1137   'mail-decode-encoded-address-string
1138   "Function used to decode addresses with encoded words.")
1139
1140 (defcustom gnus-extra-headers '(To Cc Keywords Gcc Newsgroups)
1141   "*Extra headers to parse."
1142   :version "24.1"                       ; added Cc Keywords Gcc
1143   :group 'gnus-summary
1144   :type '(repeat symbol))
1145
1146 (defcustom gnus-ignored-from-addresses
1147   (and user-mail-address
1148        (not (string= user-mail-address ""))
1149        (regexp-quote user-mail-address))
1150   "*From headers that may be suppressed in favor of To headers.
1151 This can be a regexp or a list of regexps."
1152   :version "21.1"
1153   :group 'gnus-summary
1154   :type '(choice regexp
1155                  (repeat :tag "Regexp List" regexp)))
1156
1157 (defsubst gnus-ignored-from-addresses ()
1158   (gmm-regexp-concat gnus-ignored-from-addresses))
1159
1160 (defcustom gnus-summary-to-prefix "-> "
1161   "*String prefixed to the To field in the summary line when
1162 using `gnus-ignored-from-addresses'."
1163   :version "22.1"
1164   :group 'gnus-summary
1165   :type 'string)
1166
1167 (defcustom gnus-summary-newsgroup-prefix "=> "
1168   "*String prefixed to the Newsgroup field in the summary
1169 line when using `gnus-ignored-from-addresses'."
1170   :version "22.1"
1171   :group 'gnus-summary
1172   :type 'string)
1173
1174 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1175   "List of charsets that should be ignored.
1176 When these charsets are used in the \"charset\" parameter, the
1177 default charset will be used instead."
1178   :version "21.1"
1179   :type '(repeat symbol)
1180   :group 'gnus-charset)
1181
1182 (defcustom gnus-newsgroup-maximum-articles nil
1183   "The maximum number of articles a newsgroup.
1184 If this is a number, old articles in a newsgroup exceeding this number
1185 are silently ignored.  If it is nil, no article is ignored.  Note that
1186 setting this variable to a number might prevent you from reading very
1187 old articles."
1188   :group 'gnus-group-select
1189   :version "22.2"
1190   :type '(choice (const :tag "No limit" nil)
1191                  integer))
1192
1193 (gnus-define-group-parameter
1194  ignored-charsets
1195  :type list
1196  :function-document
1197  "Return the ignored charsets of GROUP."
1198  :variable gnus-group-ignored-charsets-alist
1199  :variable-default
1200  '(("alt\\.chinese\\.text" iso-8859-1))
1201  :variable-document
1202  "Alist of regexps (to match group names) and charsets that should be ignored.
1203 When these charsets are used in the \"charset\" parameter, the
1204 default charset will be used instead."
1205  :variable-group gnus-charset
1206  :variable-type '(repeat (cons (regexp :tag "Group")
1207                                (repeat symbol)))
1208  :parameter-type '(choice :tag "Ignored charsets"
1209                           :value nil
1210                           (repeat (symbol)))
1211  :parameter-document       "\
1212 List of charsets that should be ignored.
1213
1214 When these charsets are used in the \"charset\" parameter, the
1215 default charset will be used instead.")
1216
1217 (defcustom gnus-group-highlight-words-alist nil
1218   "Alist of group regexps and highlight regexps.
1219 This variable uses the same syntax as `gnus-emphasis-alist'."
1220   :version "21.1"
1221   :type '(repeat (cons (regexp :tag "Group")
1222                        (repeat (list (regexp :tag "Highlight regexp")
1223                                      (number :tag "Group for entire word" 0)
1224                                      (number :tag "Group for displayed part" 0)
1225                                      (symbol :tag "Face"
1226                                              gnus-emphasis-highlight-words)))))
1227   :group 'gnus-summary-visual)
1228
1229 (defcustom gnus-summary-show-article-charset-alist
1230   nil
1231   "Alist of number and charset.
1232 The article will be shown with the charset corresponding to the
1233 numbered argument.
1234 For example: ((1 . cn-gb-2312) (2 . big5))."
1235   :version "21.1"
1236   :type '(repeat (cons (number :tag "Argument" 1)
1237                        (symbol :tag "Charset")))
1238   :group 'gnus-charset)
1239
1240 (defcustom gnus-preserve-marks t
1241   "Whether marks are preserved when moving, copying and respooling messages."
1242   :version "21.1"
1243   :type 'boolean
1244   :group 'gnus-summary-marks)
1245
1246 (defcustom gnus-alter-articles-to-read-function nil
1247   "Function to be called to alter the list of articles to be selected."
1248   :type '(choice (const nil) function)
1249   :group 'gnus-summary)
1250
1251 (defcustom gnus-orphan-score nil
1252   "*All orphans get this score added.  Set in the score file."
1253   :group 'gnus-score-default
1254   :type '(choice (const nil)
1255                  integer))
1256
1257 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1258   "*A regexp to match MIME parts when saving multiple parts of a
1259 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1260 This regexp will be used by default when prompting the user for which
1261 type of files to save."
1262   :group 'gnus-summary
1263   :type 'regexp)
1264
1265 (defcustom gnus-read-all-available-headers nil
1266   "Whether Gnus should parse all headers made available to it.
1267 This is mostly relevant for slow back ends where the user may
1268 wish to widen the summary buffer to include all headers
1269 that were fetched."
1270   :version "22.1"
1271   :group 'gnus-summary
1272   :type '(choice boolean regexp))
1273
1274 (defcustom gnus-summary-pipe-output-default-command nil
1275   "Command (and optional arguments) used to pipe article to subprocess.
1276 This will be used as the default command if it is non-nil.  The value
1277 will be updated if you modify it when executing the command
1278 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1279   :version "23.1" ;; No Gnus
1280   :group 'gnus-summary
1281   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1282
1283 (defcustom gnus-summary-muttprint-program "muttprint"
1284   "Command (and optional arguments) used to run Muttprint.
1285 The value will be updated if you modify it when executing the command
1286 `gnus-summary-muttprint'."
1287   :version "22.1"
1288   :group 'gnus-summary
1289   :type 'string)
1290
1291 (defcustom gnus-article-loose-mime t
1292   "If non-nil, don't require MIME-Version header.
1293 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1294 supply the MIME-Version header or deliberately strip it from the mail.
1295 If non-nil (the default), Gnus will treat some articles as MIME
1296 even if the MIME-Version header is missing."
1297   :version "22.1"
1298   :type 'boolean
1299   :group 'gnus-article-mime)
1300
1301 (defcustom gnus-article-emulate-mime t
1302   "If non-nil, use MIME emulation for uuencode and the like.
1303 This means that Gnus will search message bodies for text that look
1304 like uuencoded bits, yEncoded bits, and so on, and present that using
1305 the normal Gnus MIME machinery."
1306   :version "22.1"
1307   :type 'boolean
1308   :group 'gnus-article-mime)
1309
1310 ;;; Internal variables
1311
1312 (defvar gnus-summary-display-cache nil)
1313 (defvar gnus-article-mime-handles nil)
1314 (defvar gnus-article-decoded-p nil)
1315 (defvar gnus-article-charset nil)
1316 (defvar gnus-article-ignored-charsets nil)
1317 (defvar gnus-scores-exclude-files nil)
1318 (defvar gnus-page-broken nil)
1319
1320 (defvar gnus-original-article nil)
1321 (defvar gnus-article-internal-prepare-hook nil)
1322 (defvar gnus-newsgroup-process-stack nil)
1323
1324 (defvar gnus-thread-indent-array nil)
1325 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1326 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1327   "Function called to sort the articles within a thread after it has been gathered together.")
1328
1329 (defvar gnus-summary-save-parts-type-history nil)
1330 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1331
1332 ;; Avoid highlighting in kill files.
1333 (defvar gnus-summary-inhibit-highlight nil)
1334 (defvar gnus-newsgroup-selected-overlay nil)
1335 (defvar gnus-inhibit-limiting nil)
1336 (defvar gnus-newsgroup-adaptive-score-file nil)
1337 (defvar gnus-current-score-file nil)
1338 (defvar gnus-current-move-group nil)
1339 (defvar gnus-current-copy-group nil)
1340 (defvar gnus-current-crosspost-group nil)
1341 (defvar gnus-newsgroup-display nil)
1342
1343 (defvar gnus-newsgroup-dependencies nil)
1344 (defvar gnus-newsgroup-adaptive nil)
1345 (defvar gnus-summary-display-article-function nil)
1346 (defvar gnus-summary-highlight-line-function nil
1347   "Function called after highlighting a summary line.")
1348
1349 (defvar gnus-summary-line-format-alist
1350   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1351     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1352     (?s gnus-tmp-subject-or-nil ?s)
1353     (?n gnus-tmp-name ?s)
1354     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1355         ?s)
1356     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1357             gnus-tmp-from) ?s)
1358     (?F gnus-tmp-from ?s)
1359     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1360     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1361     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1362     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1363     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1364     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1365     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1366     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1367     (?L gnus-tmp-lines ?s)
1368     (?Z (or (nnir-article-rsv (mail-header-number gnus-tmp-header))
1369             0) ?d)
1370     (?G (or (nnir-article-group (mail-header-number gnus-tmp-header))
1371             "") ?s)
1372     (?g (or (gnus-group-short-name
1373              (nnir-article-group (mail-header-number gnus-tmp-header)))
1374             "") ?s)
1375     (?O gnus-tmp-downloaded ?c)
1376     (?I gnus-tmp-indentation ?s)
1377     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1378     (?R gnus-tmp-replied ?c)
1379     (?\[ gnus-tmp-opening-bracket ?c)
1380     (?\] gnus-tmp-closing-bracket ?c)
1381     (?\> (make-string gnus-tmp-level ? ) ?s)
1382     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1383     (?i gnus-tmp-score ?d)
1384     (?z gnus-tmp-score-char ?c)
1385     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1386     (?U gnus-tmp-unread ?c)
1387     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1388         ?s)
1389     (?t (gnus-summary-number-of-articles-in-thread
1390          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1391         ?d)
1392     (?e (gnus-summary-number-of-articles-in-thread
1393          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1394         ?c)
1395     (?u gnus-tmp-user-defined ?s)
1396     (?P (gnus-pick-line-number) ?d)
1397     (?B gnus-tmp-thread-tree-header-string ?s)
1398     (user-date (gnus-user-date
1399                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1400   "An alist of format specifications that can appear in summary lines.
1401 These are paired with what variables they correspond with, along with
1402 the type of the variable (string, integer, character, etc).")
1403
1404 (defvar gnus-summary-dummy-line-format-alist
1405   `((?S gnus-tmp-subject ?s)
1406     (?N gnus-tmp-number ?d)
1407     (?u gnus-tmp-user-defined ?s)))
1408
1409 (defvar gnus-summary-mode-line-format-alist
1410   `((?G gnus-tmp-group-name ?s)
1411     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1412     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1413     (?A gnus-tmp-article-number ?d)
1414     (?Z gnus-tmp-unread-and-unselected ?s)
1415     (?V gnus-version ?s)
1416     (?U gnus-tmp-unread-and-unticked ?d)
1417     (?S gnus-tmp-subject ?s)
1418     (?e gnus-tmp-unselected ?d)
1419     (?u gnus-tmp-user-defined ?s)
1420     (?d (length gnus-newsgroup-dormant) ?d)
1421     (?t (length gnus-newsgroup-marked) ?d)
1422     (?h (length gnus-newsgroup-spam-marked) ?d)
1423     (?r (length gnus-newsgroup-reads) ?d)
1424     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1425     (?E gnus-newsgroup-expunged-tally ?d)
1426     (?s (gnus-current-score-file-nondirectory) ?s)))
1427
1428 ;; This is here rather than in gnus-art for compilation reasons.
1429 (defvar gnus-article-mode-line-format-alist
1430   (nconc '((?w (gnus-article-wash-status) ?s)
1431            (?m (gnus-article-mime-part-status) ?s))
1432          gnus-summary-mode-line-format-alist))
1433
1434 (defvar gnus-last-search-regexp nil
1435   "Default regexp for article search command.")
1436
1437 (defvar gnus-last-shell-command nil
1438   "Default shell command on article.")
1439
1440 (defvar gnus-newsgroup-agentized nil
1441   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1442 (defvar gnus-newsgroup-begin nil)
1443 (defvar gnus-newsgroup-end nil)
1444 (defvar gnus-newsgroup-last-rmail nil)
1445 (defvar gnus-newsgroup-last-mail nil)
1446 (defvar gnus-newsgroup-last-folder nil)
1447 (defvar gnus-newsgroup-last-file nil)
1448 (defvar gnus-newsgroup-last-directory nil)
1449 (defvar gnus-newsgroup-auto-expire nil)
1450 (defvar gnus-newsgroup-active nil)
1451 (defvar gnus-newsgroup-highest nil)
1452
1453 (defvar gnus-newsgroup-data nil)
1454 (defvar gnus-newsgroup-data-reverse nil)
1455 (defvar gnus-newsgroup-limit nil)
1456 (defvar gnus-newsgroup-limits nil)
1457 (defvar gnus-summary-use-undownloaded-faces nil)
1458
1459 (defvar gnus-newsgroup-unreads nil
1460   "Sorted list of unread articles in the current newsgroup.")
1461
1462 (defvar gnus-newsgroup-unselected nil
1463   "Sorted list of unselected unread articles in the current newsgroup.")
1464
1465 (defvar gnus-newsgroup-reads nil
1466   "Alist of read articles and article marks in the current newsgroup.")
1467
1468 (defvar gnus-newsgroup-expunged-tally nil)
1469
1470 (defvar gnus-newsgroup-marked nil
1471   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1472
1473 (defvar gnus-newsgroup-spam-marked nil
1474   "List of ranges of articles that have been marked as spam.")
1475
1476 (defvar gnus-newsgroup-killed nil
1477   "List of ranges of articles that have been through the scoring process.")
1478
1479 (defvar gnus-newsgroup-cached nil
1480   "Sorted list of articles that come from the article cache.")
1481
1482 (defvar gnus-newsgroup-saved nil
1483   "List of articles that have been saved.")
1484
1485 (defvar gnus-newsgroup-kill-headers nil)
1486
1487 (defvar gnus-newsgroup-replied nil
1488   "List of articles that have been replied to in the current newsgroup.")
1489
1490 (defvar gnus-newsgroup-forwarded nil
1491   "List of articles that have been forwarded in the current newsgroup.")
1492
1493 (defvar gnus-newsgroup-expirable nil
1494   "Sorted list of articles in the current newsgroup that can be expired.")
1495
1496 (defvar gnus-newsgroup-processable nil
1497   "List of articles in the current newsgroup that can be processed.")
1498
1499 (defvar gnus-newsgroup-downloadable nil
1500   "Sorted list of articles in the current newsgroup that can be processed.")
1501
1502 (defvar gnus-newsgroup-unfetched nil
1503   "Sorted list of articles in the current newsgroup whose headers have
1504 not been fetched into the agent.
1505
1506 This list will always be a subset of gnus-newsgroup-undownloaded.")
1507
1508 (defvar gnus-newsgroup-undownloaded nil
1509   "List of articles in the current newsgroup that haven't been downloaded.")
1510
1511 (defvar gnus-newsgroup-unsendable nil
1512   "List of articles in the current newsgroup that won't be sent.")
1513
1514 (defvar gnus-newsgroup-bookmarks nil
1515   "List of articles in the current newsgroup that have bookmarks.")
1516
1517 (defvar gnus-newsgroup-dormant nil
1518   "Sorted list of dormant articles in the current newsgroup.")
1519
1520 (defvar gnus-newsgroup-unseen nil
1521   "List of unseen articles in the current newsgroup.")
1522
1523 (defvar gnus-newsgroup-seen nil
1524   "Range of seen articles in the current newsgroup.")
1525
1526 (defvar gnus-newsgroup-unexist nil
1527   "Range of unexistent articles in the current newsgroup.")
1528
1529 (defvar gnus-newsgroup-articles nil
1530   "List of articles in the current newsgroup.")
1531
1532 (defvar gnus-newsgroup-scored nil
1533   "List of scored articles in the current newsgroup.")
1534
1535 (defvar gnus-newsgroup-headers nil
1536   "List of article headers in the current newsgroup.")
1537
1538 (defvar gnus-newsgroup-threads nil)
1539
1540 (defvar gnus-newsgroup-prepared nil
1541   "Whether the current group has been prepared properly.")
1542
1543 (defvar gnus-newsgroup-ancient nil
1544   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1545
1546 (defvar gnus-newsgroup-sparse nil)
1547
1548 (defvar gnus-current-article nil)
1549 (defvar gnus-article-current nil)
1550 (defvar gnus-current-headers nil)
1551 (defvar gnus-have-all-headers nil)
1552 (defvar gnus-last-article nil)
1553 (defvar gnus-newsgroup-history nil)
1554 (defvar gnus-newsgroup-charset nil)
1555 (defvar gnus-newsgroup-ephemeral-charset nil)
1556 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1557
1558 (defvar gnus-article-before-search nil)
1559
1560 (defvar gnus-summary-local-variables
1561   '(gnus-newsgroup-name
1562
1563     ;; Marks lists
1564     gnus-newsgroup-unreads
1565     gnus-newsgroup-unselected
1566     gnus-newsgroup-marked
1567     gnus-newsgroup-spam-marked
1568     gnus-newsgroup-reads
1569     gnus-newsgroup-saved
1570     gnus-newsgroup-replied
1571     gnus-newsgroup-forwarded
1572     gnus-newsgroup-expirable
1573     gnus-newsgroup-killed
1574     gnus-newsgroup-unseen
1575     gnus-newsgroup-seen
1576     gnus-newsgroup-unexist
1577     gnus-newsgroup-cached
1578     gnus-newsgroup-downloadable
1579     gnus-newsgroup-undownloaded
1580     gnus-newsgroup-unsendable
1581
1582     gnus-newsgroup-begin gnus-newsgroup-end
1583     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1584     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1585     gnus-newsgroup-last-directory
1586     gnus-newsgroup-auto-expire
1587     gnus-newsgroup-processable
1588     gnus-newsgroup-unfetched
1589     gnus-newsgroup-articles
1590     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1591     gnus-newsgroup-headers gnus-newsgroup-threads
1592     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1593     gnus-current-article gnus-current-headers gnus-have-all-headers
1594     gnus-last-article gnus-article-internal-prepare-hook
1595     (gnus-summary-article-delete-hook . global)
1596     (gnus-summary-article-move-hook . global)
1597     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1598     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1599     gnus-thread-expunge-below
1600     gnus-score-alist gnus-current-score-file
1601     (gnus-summary-expunge-below . global)
1602     (gnus-summary-mark-below . global)
1603     (gnus-orphan-score . global)
1604     gnus-newsgroup-active gnus-scores-exclude-files
1605     gnus-newsgroup-highest
1606     gnus-newsgroup-history gnus-newsgroup-ancient
1607     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1608     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1609     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1610     (gnus-newsgroup-expunged-tally . 0)
1611     gnus-cache-removable-articles
1612     gnus-newsgroup-data gnus-newsgroup-data-reverse
1613     gnus-newsgroup-limit gnus-newsgroup-limits
1614     gnus-newsgroup-charset gnus-newsgroup-display
1615     gnus-summary-use-undownloaded-faces)
1616   "Variables that are buffer-local to the summary buffers.")
1617
1618 (defvar gnus-newsgroup-variables nil
1619   "A list of variables that have separate values in different newsgroups.
1620 A list of newsgroup (summary buffer) local variables, or cons of
1621 variables and their default expressions to be evalled (when the default
1622 values are not nil), that should be made global while the summary buffer
1623 is active.
1624
1625 Note: The default expressions will be evaluated (using function `eval')
1626 before assignment to the local variable rather than just assigned to it.
1627 If the default expression is the symbol `global', that symbol will not
1628 be evaluated but the global value of the local variable will be used
1629 instead.
1630
1631 These variables can be used to set variables in the group parameters
1632 while still allowing them to affect operations done in other buffers.
1633 For example:
1634
1635 \(setq gnus-newsgroup-variables
1636      '(message-use-followup-to
1637        (gnus-visible-headers .
1638          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1639 ")
1640
1641 (eval-when-compile
1642   ;; Bind features so that require will believe that gnus-sum has
1643   ;; already been loaded (avoids infinite recursion)
1644   (let ((features (cons 'gnus-sum features)))
1645     (require 'gnus-art)))
1646
1647 ;; MIME stuff.
1648
1649 (defvar gnus-decode-encoded-word-methods
1650   '(mail-decode-encoded-word-string)
1651   "List of methods used to decode encoded words.
1652
1653 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1654 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1655 \(REGEXP . FUNCTION), FUNCTION will be applied only to the newsgroups
1656 whose names match REGEXP.
1657
1658 For example:
1659 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1660  mail-decode-encoded-word-string
1661  (\"chinese\" . rfc1843-decode-string))")
1662
1663 (defvar gnus-decode-encoded-word-methods-cache nil)
1664
1665 (defun gnus-multi-decode-encoded-word-string (string)
1666   "Apply the functions from `gnus-encoded-word-methods' that match."
1667   (unless (and gnus-decode-encoded-word-methods-cache
1668                (eq gnus-newsgroup-name
1669                    (car gnus-decode-encoded-word-methods-cache)))
1670     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1671     (dolist (method gnus-decode-encoded-word-methods)
1672       (if (symbolp method)
1673           (nconc gnus-decode-encoded-word-methods-cache (list method))
1674         (if (and gnus-newsgroup-name
1675                  (string-match (car method) gnus-newsgroup-name))
1676             (nconc gnus-decode-encoded-word-methods-cache
1677                    (list (cdr method)))))))
1678   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1679     (setq string (funcall method string))))
1680
1681 ;; Subject simplification.
1682
1683 (defun gnus-simplify-whitespace (str)
1684   "Remove excessive whitespace from STR."
1685   ;; Multiple spaces.
1686   (while (string-match "[ \t][ \t]+" str)
1687     (setq str (concat (substring str 0 (match-beginning 0))
1688                         " "
1689                         (substring str (match-end 0)))))
1690   ;; Leading spaces.
1691   (when (string-match "^[ \t]+" str)
1692     (setq str (substring str (match-end 0))))
1693   ;; Trailing spaces.
1694   (when (string-match "[ \t]+$" str)
1695     (setq str (substring str 0 (match-beginning 0))))
1696   str)
1697
1698 (defun gnus-simplify-all-whitespace (str)
1699   "Remove all whitespace from STR."
1700   (while (string-match "[ \t\n]+" str)
1701     (setq str (replace-match "" nil nil str)))
1702   str)
1703
1704 (defsubst gnus-simplify-subject-re (subject)
1705   "Remove \"Re:\" from subject lines."
1706   (if (string-match message-subject-re-regexp subject)
1707       (substring subject (match-end 0))
1708     subject))
1709
1710 (defun gnus-simplify-subject (subject &optional re-only)
1711   "Remove `Re:' and words in parentheses.
1712 If RE-ONLY is non-nil, strip leading `Re:'s only."
1713   (let ((case-fold-search t))           ;Ignore case.
1714     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1715     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1716       (setq subject (substring subject (match-end 0))))
1717     ;; Remove uninteresting prefixes.
1718     (when (and (not re-only)
1719                gnus-simplify-ignored-prefixes
1720                (string-match gnus-simplify-ignored-prefixes subject))
1721       (setq subject (substring subject (match-end 0))))
1722     ;; Remove words in parentheses from end.
1723     (unless re-only
1724       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1725         (setq subject (substring subject 0 (match-beginning 0)))))
1726     ;; Return subject string.
1727     subject))
1728
1729 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1730 ;; all whitespace.
1731 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1732   (goto-char (point-min))
1733   (while (re-search-forward regexp nil t)
1734     (replace-match (or newtext ""))))
1735
1736 (defun gnus-simplify-buffer-fuzzy (regexp)
1737   "Simplify string in the buffer fuzzily.
1738 The string in the accessible portion of the current buffer is simplified.
1739 It is assumed to be a single-line subject.
1740 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1741 matter is removed.  Additional things can be deleted by setting
1742 `gnus-simplify-subject-fuzzy-regexp'."
1743   (let ((case-fold-search t)
1744         (modified-tick))
1745     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1746
1747     (while (not (eq modified-tick (buffer-modified-tick)))
1748       (setq modified-tick (buffer-modified-tick))
1749       (cond
1750        ((listp regexp)
1751         (mapc 'gnus-simplify-buffer-fuzzy-step regexp))
1752        (regexp
1753         (gnus-simplify-buffer-fuzzy-step regexp)))
1754       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1755       (gnus-simplify-buffer-fuzzy-step
1756        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1757       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1758
1759     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1760     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1761     (gnus-simplify-buffer-fuzzy-step " $")
1762     (gnus-simplify-buffer-fuzzy-step "^ +")))
1763
1764 (defun gnus-simplify-subject-fuzzy (subject)
1765   "Simplify a subject string fuzzily.
1766 See `gnus-simplify-buffer-fuzzy' for details."
1767   (save-excursion
1768     (let ((regexp gnus-simplify-subject-fuzzy-regexp))
1769       (gnus-set-work-buffer)
1770       (let ((case-fold-search t))
1771         ;; Remove uninteresting prefixes.
1772         (when (and gnus-simplify-ignored-prefixes
1773                    (string-match gnus-simplify-ignored-prefixes subject))
1774           (setq subject (substring subject (match-end 0))))
1775         (insert subject)
1776         (inline (gnus-simplify-buffer-fuzzy regexp))
1777         (buffer-string)))))
1778
1779 (defsubst gnus-simplify-subject-fully (subject)
1780   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1781   (cond
1782    (gnus-simplify-subject-functions
1783     (gnus-map-function gnus-simplify-subject-functions subject))
1784    ((null gnus-summary-gather-subject-limit)
1785     (gnus-simplify-subject-re subject))
1786    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1787     (gnus-simplify-subject-fuzzy subject))
1788    ((numberp gnus-summary-gather-subject-limit)
1789     (truncate-string-to-width (gnus-simplify-subject-re subject)
1790                               gnus-summary-gather-subject-limit))
1791    (t
1792     subject)))
1793
1794 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1795   "Check whether two subjects are equal.
1796 If optional argument SIMPLE-FIRST is t, first argument is already
1797 simplified."
1798   (cond
1799    ((null simple-first)
1800     (equal (gnus-simplify-subject-fully s1)
1801            (gnus-simplify-subject-fully s2)))
1802    (t
1803     (equal s1
1804            (gnus-simplify-subject-fully s2)))))
1805
1806 (defun gnus-summary-bubble-group ()
1807   "Increase the score of the current group.
1808 This is a handy function to add to `gnus-summary-exit-hook' to
1809 increase the score of each group you read."
1810   (gnus-group-add-score gnus-newsgroup-name))
1811
1812 \f
1813 ;;;
1814 ;;; Gnus summary mode
1815 ;;;
1816
1817 (put 'gnus-summary-mode 'mode-class 'special)
1818
1819 (defvar gnus-article-commands-menu)
1820
1821 ;; Non-orthogonal keys
1822
1823 (gnus-define-keys gnus-summary-mode-map
1824   " " gnus-summary-next-page
1825   "\177" gnus-summary-prev-page
1826   [delete] gnus-summary-prev-page
1827   [backspace] gnus-summary-prev-page
1828   "\r" gnus-summary-scroll-up
1829   "\M-\r" gnus-summary-scroll-down
1830   "n" gnus-summary-next-unread-article
1831   "p" gnus-summary-prev-unread-article
1832   "N" gnus-summary-next-article
1833   "P" gnus-summary-prev-article
1834   "\M-\C-n" gnus-summary-next-same-subject
1835   "\M-\C-p" gnus-summary-prev-same-subject
1836   "\M-n" gnus-summary-next-unread-subject
1837   "\M-p" gnus-summary-prev-unread-subject
1838   "." gnus-summary-first-unread-article
1839   "," gnus-summary-best-unread-article
1840   "\M-s" gnus-summary-search-article-forward
1841   "\M-r" gnus-summary-search-article-backward
1842   "\M-S" gnus-summary-repeat-search-article-forward
1843   "\M-R" gnus-summary-repeat-search-article-backward
1844   "<" gnus-summary-beginning-of-article
1845   ">" gnus-summary-end-of-article
1846   "j" gnus-summary-goto-article
1847   "^" gnus-summary-refer-parent-article
1848   "\M-^" gnus-summary-refer-article
1849   "u" gnus-summary-tick-article-forward
1850   "!" gnus-summary-tick-article-forward
1851   "U" gnus-summary-tick-article-backward
1852   "d" gnus-summary-mark-as-read-forward
1853   "D" gnus-summary-mark-as-read-backward
1854   "E" gnus-summary-mark-as-expirable
1855   "\M-u" gnus-summary-clear-mark-forward
1856   "\M-U" gnus-summary-clear-mark-backward
1857   "k" gnus-summary-kill-same-subject-and-select
1858   "\C-k" gnus-summary-kill-same-subject
1859   "\M-\C-k" gnus-summary-kill-thread
1860   "\M-\C-l" gnus-summary-lower-thread
1861   "e" gnus-summary-edit-article
1862   "#" gnus-summary-mark-as-processable
1863   "\M-#" gnus-summary-unmark-as-processable
1864   "\M-\C-t" gnus-summary-toggle-threads
1865   "\M-\C-s" gnus-summary-show-thread
1866   "\M-\C-h" gnus-summary-hide-thread
1867   "\M-\C-f" gnus-summary-next-thread
1868   "\M-\C-b" gnus-summary-prev-thread
1869   [(meta down)] gnus-summary-next-thread
1870   [(meta up)] gnus-summary-prev-thread
1871   "\M-\C-u" gnus-summary-up-thread
1872   "\M-\C-d" gnus-summary-down-thread
1873   "&" gnus-summary-execute-command
1874   "c" gnus-summary-catchup-and-exit
1875   "\C-w" gnus-summary-mark-region-as-read
1876   "\C-t" gnus-summary-toggle-truncation
1877   "?" gnus-summary-mark-as-dormant
1878   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1879   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1880   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1881   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1882   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1883   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1884   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1885   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1886   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1887   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1888   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1889   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1890   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1891   "=" gnus-summary-expand-window
1892   "\C-x\C-s" gnus-summary-reselect-current-group
1893   "\M-g" gnus-summary-rescan-group
1894   "\C-c\C-r" gnus-summary-caesar-message
1895   "f" gnus-summary-followup
1896   "F" gnus-summary-followup-with-original
1897   "C" gnus-summary-cancel-article
1898   "r" gnus-summary-reply
1899   "R" gnus-summary-reply-with-original
1900   "\C-c\C-f" gnus-summary-mail-forward
1901   "o" gnus-summary-save-article
1902   "\C-o" gnus-summary-save-article-mail
1903   "|" gnus-summary-pipe-output
1904   "\M-k" gnus-summary-edit-local-kill
1905   "\M-K" gnus-summary-edit-global-kill
1906   ;; "V" gnus-version
1907   "\C-c\C-d" gnus-summary-describe-group
1908   "q" gnus-summary-exit
1909   "Q" gnus-summary-exit-no-update
1910   "\C-c\C-i" gnus-info-find-node
1911   gnus-mouse-2 gnus-mouse-pick-article
1912   [follow-link] mouse-face
1913   "m" gnus-summary-mail-other-window
1914   "a" gnus-summary-post-news
1915   "x" gnus-summary-limit-to-unread
1916   "s" gnus-summary-isearch-article
1917   [tab] gnus-summary-widget-forward
1918   [backtab] gnus-summary-widget-backward
1919   "t" gnus-summary-toggle-header
1920   "g" gnus-summary-show-article
1921   "l" gnus-summary-goto-last-article
1922   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1923   "\C-d" gnus-summary-enter-digest-group
1924   "\M-\C-d" gnus-summary-read-document
1925   "\M-\C-e" gnus-summary-edit-parameters
1926   "\M-\C-a" gnus-summary-customize-parameters
1927   "\C-c\C-b" gnus-bug
1928   "*" gnus-cache-enter-article
1929   "\M-*" gnus-cache-remove-article
1930   "\M-&" gnus-summary-universal-argument
1931   "\C-l" gnus-recenter
1932   "I" gnus-summary-increase-score
1933   "L" gnus-summary-lower-score
1934   "\M-i" gnus-symbolic-argument
1935   "h" gnus-summary-select-article-buffer
1936
1937   "b" gnus-article-view-part
1938   "\M-t" gnus-summary-toggle-display-buttonized
1939
1940   "V" gnus-summary-score-map
1941   "X" gnus-uu-extract-map
1942   "S" gnus-summary-send-map)
1943
1944 ;; Sort of orthogonal keymap
1945 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1946   "t" gnus-summary-tick-article-forward
1947   "!" gnus-summary-tick-article-forward
1948   "d" gnus-summary-mark-as-read-forward
1949   "r" gnus-summary-mark-as-read-forward
1950   "c" gnus-summary-clear-mark-forward
1951   " " gnus-summary-clear-mark-forward
1952   "e" gnus-summary-mark-as-expirable
1953   "x" gnus-summary-mark-as-expirable
1954   "?" gnus-summary-mark-as-dormant
1955   "b" gnus-summary-set-bookmark
1956   "B" gnus-summary-remove-bookmark
1957   "#" gnus-summary-mark-as-processable
1958   "\M-#" gnus-summary-unmark-as-processable
1959   "S" gnus-summary-limit-include-expunged
1960   "C" gnus-summary-catchup
1961   "H" gnus-summary-catchup-to-here
1962   "h" gnus-summary-catchup-from-here
1963   "\C-c" gnus-summary-catchup-all
1964   "k" gnus-summary-kill-same-subject-and-select
1965   "K" gnus-summary-kill-same-subject
1966   "P" gnus-uu-mark-map)
1967
1968 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1969   "c" gnus-summary-clear-above
1970   "u" gnus-summary-tick-above
1971   "m" gnus-summary-mark-above
1972   "k" gnus-summary-kill-below)
1973
1974 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1975   "/" gnus-summary-limit-to-subject
1976   "n" gnus-summary-limit-to-articles
1977   "b" gnus-summary-limit-to-bodies
1978   "h" gnus-summary-limit-to-headers
1979   "w" gnus-summary-pop-limit
1980   "s" gnus-summary-limit-to-subject
1981   "a" gnus-summary-limit-to-author
1982   "u" gnus-summary-limit-to-unread
1983   "m" gnus-summary-limit-to-marks
1984   "M" gnus-summary-limit-exclude-marks
1985   "v" gnus-summary-limit-to-score
1986   "*" gnus-summary-limit-include-cached
1987   "D" gnus-summary-limit-include-dormant
1988   "T" gnus-summary-limit-include-thread
1989   "d" gnus-summary-limit-exclude-dormant
1990   "t" gnus-summary-limit-to-age
1991   "." gnus-summary-limit-to-unseen
1992   "x" gnus-summary-limit-to-extra
1993   "p" gnus-summary-limit-to-display-predicate
1994   "E" gnus-summary-limit-include-expunged
1995   "c" gnus-summary-limit-exclude-childless-dormant
1996   "C" gnus-summary-limit-mark-excluded-as-read
1997   "o" gnus-summary-insert-old-articles
1998   "N" gnus-summary-insert-new-articles
1999   "S" gnus-summary-limit-to-singletons
2000   "r" gnus-summary-limit-to-replied
2001   "R" gnus-summary-limit-to-recipient
2002   "A" gnus-summary-limit-to-address)
2003
2004 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2005   "n" gnus-summary-next-unread-article
2006   "p" gnus-summary-prev-unread-article
2007   "N" gnus-summary-next-article
2008   "P" gnus-summary-prev-article
2009   "\C-n" gnus-summary-next-same-subject
2010   "\C-p" gnus-summary-prev-same-subject
2011   "\M-n" gnus-summary-next-unread-subject
2012   "\M-p" gnus-summary-prev-unread-subject
2013   "f" gnus-summary-first-unread-article
2014   "b" gnus-summary-best-unread-article
2015   "j" gnus-summary-goto-article
2016   "g" gnus-summary-goto-subject
2017   "l" gnus-summary-goto-last-article
2018   "o" gnus-summary-pop-article)
2019
2020 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2021   "k" gnus-summary-kill-thread
2022   "E" gnus-summary-expire-thread
2023   "l" gnus-summary-lower-thread
2024   "i" gnus-summary-raise-thread
2025   "T" gnus-summary-toggle-threads
2026   "t" gnus-summary-rethread-current
2027   "^" gnus-summary-reparent-thread
2028   "\M-^" gnus-summary-reparent-children
2029   "s" gnus-summary-show-thread
2030   "S" gnus-summary-show-all-threads
2031   "h" gnus-summary-hide-thread
2032   "H" gnus-summary-hide-all-threads
2033   "n" gnus-summary-next-thread
2034   "p" gnus-summary-prev-thread
2035   "u" gnus-summary-up-thread
2036   "o" gnus-summary-top-thread
2037   "d" gnus-summary-down-thread
2038   "#" gnus-uu-mark-thread
2039   "\M-#" gnus-uu-unmark-thread)
2040
2041 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2042   "g" gnus-summary-prepare
2043   "c" gnus-summary-insert-cached-articles
2044   "d" gnus-summary-insert-dormant-articles
2045   "t" gnus-summary-insert-ticked-articles)
2046
2047 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2048   "c" gnus-summary-catchup-and-exit
2049   "C" gnus-summary-catchup-all-and-exit
2050   "E" gnus-summary-exit-no-update
2051   "Q" gnus-summary-exit
2052   "Z" gnus-summary-exit
2053   "n" gnus-summary-catchup-and-goto-next-group
2054   "p" gnus-summary-catchup-and-goto-prev-group
2055   "R" gnus-summary-reselect-current-group
2056   "G" gnus-summary-rescan-group
2057   "N" gnus-summary-next-group
2058   "s" gnus-summary-save-newsrc
2059   "P" gnus-summary-prev-group)
2060
2061 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2062   " " gnus-summary-next-page
2063   "n" gnus-summary-next-page
2064   "\177" gnus-summary-prev-page
2065   [delete] gnus-summary-prev-page
2066   "p" gnus-summary-prev-page
2067   "\r" gnus-summary-scroll-up
2068   "\M-\r" gnus-summary-scroll-down
2069   "<" gnus-summary-beginning-of-article
2070   ">" gnus-summary-end-of-article
2071   "b" gnus-summary-beginning-of-article
2072   "e" gnus-summary-end-of-article
2073   "^" gnus-summary-refer-parent-article
2074   "r" gnus-summary-refer-parent-article
2075   "C" gnus-summary-show-complete-article
2076   "D" gnus-summary-enter-digest-group
2077   "R" gnus-summary-refer-references
2078   "T" gnus-summary-refer-thread
2079   "W" gnus-warp-to-article
2080   "g" gnus-summary-show-article
2081   "s" gnus-summary-isearch-article
2082   [tab] gnus-summary-widget-forward
2083   [backtab] gnus-summary-widget-backward
2084   "P" gnus-summary-print-article
2085   "S" gnus-sticky-article
2086   "M" gnus-mailing-list-insinuate
2087   "t" gnus-article-babel)
2088
2089 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2090   "b" gnus-article-add-buttons
2091   "B" gnus-article-add-buttons-to-head
2092   "o" gnus-article-treat-overstrike
2093   "e" gnus-article-emphasize
2094   "w" gnus-article-fill-cited-article
2095   "Q" gnus-article-fill-long-lines
2096   "L" gnus-article-toggle-truncate-lines
2097   "C" gnus-article-capitalize-sentences
2098   "c" gnus-article-remove-cr
2099   "q" gnus-article-de-quoted-unreadable
2100   "6" gnus-article-de-base64-unreadable
2101   "Z" gnus-article-decode-HZ
2102   "A" gnus-article-treat-ansi-sequences
2103   "h" gnus-article-wash-html
2104   "u" gnus-article-unsplit-urls
2105   "s" gnus-summary-force-verify-and-decrypt
2106   "f" gnus-article-display-x-face
2107   "l" gnus-summary-stop-page-breaking
2108   "r" gnus-summary-caesar-message
2109   "m" gnus-summary-morse-message
2110   "t" gnus-summary-toggle-header
2111   "g" gnus-treat-smiley
2112   "v" gnus-summary-verbose-headers
2113   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2114   "p" gnus-article-verify-x-pgp-sig
2115   "d" gnus-article-treat-dumbquotes
2116   "U" gnus-article-treat-non-ascii
2117   "i" gnus-summary-idna-message)
2118
2119 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2120   ;; mnemonic: deuglif*Y*
2121   "u" gnus-article-outlook-unwrap-lines
2122   "a" gnus-article-outlook-repair-attribution
2123   "c" gnus-article-outlook-rearrange-citation
2124   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2125
2126 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2127   "a" gnus-article-hide
2128   "h" gnus-article-hide-headers
2129   "b" gnus-article-hide-boring-headers
2130   "s" gnus-article-hide-signature
2131   "c" gnus-article-hide-citation
2132   "C" gnus-article-hide-citation-in-followups
2133   "l" gnus-article-hide-list-identifiers
2134   "B" gnus-article-strip-banner
2135   "P" gnus-article-hide-pem
2136   "\C-c" gnus-article-hide-citation-maybe)
2137
2138 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2139   "a" gnus-article-highlight
2140   "h" gnus-article-highlight-headers
2141   "c" gnus-article-highlight-citation
2142   "s" gnus-article-highlight-signature)
2143
2144 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2145   "f" gnus-article-treat-fold-headers
2146   "u" gnus-article-treat-unfold-headers
2147   "n" gnus-article-treat-fold-newsgroups)
2148
2149 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2150   "x" gnus-article-display-x-face
2151   "d" gnus-article-display-face
2152   "s" gnus-treat-smiley
2153   "D" gnus-article-remove-images
2154   "W" gnus-article-show-images
2155   "f" gnus-treat-from-picon
2156   "m" gnus-treat-mail-picon
2157   "n" gnus-treat-newsgroups-picon
2158   "g" gnus-treat-from-gravatar
2159   "h" gnus-treat-mail-gravatar)
2160
2161 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2162   "w" gnus-article-decode-mime-words
2163   "c" gnus-article-decode-charset
2164   "v" gnus-mime-view-all-parts
2165   "b" gnus-article-view-part)
2166
2167 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2168   "z" gnus-article-date-ut
2169   "u" gnus-article-date-ut
2170   "l" gnus-article-date-local
2171   "p" gnus-article-date-english
2172   "e" gnus-article-date-lapsed
2173   "o" gnus-article-date-original
2174   "i" gnus-article-date-iso8601
2175   "s" gnus-article-date-user)
2176
2177 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2178   "t" gnus-article-remove-trailing-blank-lines
2179   "l" gnus-article-strip-leading-blank-lines
2180   "m" gnus-article-strip-multiple-blank-lines
2181   "a" gnus-article-strip-blank-lines
2182   "A" gnus-article-strip-all-blank-lines
2183   "s" gnus-article-strip-leading-space
2184   "e" gnus-article-strip-trailing-space
2185   "w" gnus-article-remove-leading-whitespace)
2186
2187 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2188   "v" gnus-version
2189   "d" gnus-summary-describe-group
2190   "h" gnus-summary-describe-briefly
2191   "i" gnus-info-find-node)
2192
2193 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2194   "e" gnus-summary-expire-articles
2195   "\M-\C-e" gnus-summary-expire-articles-now
2196   "\177" gnus-summary-delete-article
2197   [delete] gnus-summary-delete-article
2198   [backspace] gnus-summary-delete-article
2199   "m" gnus-summary-move-article
2200   "r" gnus-summary-respool-article
2201   "w" gnus-summary-edit-article
2202   "c" gnus-summary-copy-article
2203   "B" gnus-summary-crosspost-article
2204   "q" gnus-summary-respool-query
2205   "t" gnus-summary-respool-trace
2206   "i" gnus-summary-import-article
2207   "I" gnus-summary-create-article
2208   "p" gnus-summary-article-posted-p)
2209
2210 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2211   "o" gnus-summary-save-article
2212   "m" gnus-summary-save-article-mail
2213   "F" gnus-summary-write-article-file
2214   "r" gnus-summary-save-article-rmail
2215   "f" gnus-summary-save-article-file
2216   "b" gnus-summary-save-article-body-file
2217   "B" gnus-summary-write-article-body-file
2218   "h" gnus-summary-save-article-folder
2219   "v" gnus-summary-save-article-vm
2220   "p" gnus-summary-pipe-output
2221   "P" gnus-summary-muttprint)
2222
2223 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2224   "b" gnus-summary-display-buttonized
2225   "m" gnus-summary-repair-multipart
2226   "v" gnus-article-view-part
2227   "o" gnus-article-save-part
2228   "O" gnus-article-save-part-and-strip
2229   "r" gnus-article-replace-part
2230   "d" gnus-article-delete-part
2231   "t" gnus-article-view-part-as-type
2232   "j" gnus-article-jump-to-part
2233   "c" gnus-article-copy-part
2234   "C" gnus-article-view-part-as-charset
2235   "e" gnus-article-view-part-externally
2236   "H" gnus-article-browse-html-article
2237   "E" gnus-article-encrypt-body
2238   "i" gnus-article-inline-part
2239   "|" gnus-article-pipe-part)
2240
2241 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2242   "p" gnus-summary-mark-as-processable
2243   "u" gnus-summary-unmark-as-processable
2244   "U" gnus-summary-unmark-all-processable
2245   "v" gnus-uu-mark-over
2246   "s" gnus-uu-mark-series
2247   "r" gnus-uu-mark-region
2248   "g" gnus-uu-unmark-region
2249   "R" gnus-uu-mark-by-regexp
2250   "G" gnus-uu-unmark-by-regexp
2251   "t" gnus-uu-mark-thread
2252   "T" gnus-uu-unmark-thread
2253   "a" gnus-uu-mark-all
2254   "b" gnus-uu-mark-buffer
2255   "S" gnus-uu-mark-sparse
2256   "k" gnus-summary-kill-process-mark
2257   "y" gnus-summary-yank-process-mark
2258   "w" gnus-summary-save-process-mark
2259   "i" gnus-uu-invert-processable)
2260
2261 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2262   ;;"x" gnus-uu-extract-any
2263   "m" gnus-summary-save-parts
2264   "u" gnus-uu-decode-uu
2265   "U" gnus-uu-decode-uu-and-save
2266   "s" gnus-uu-decode-unshar
2267   "S" gnus-uu-decode-unshar-and-save
2268   "o" gnus-uu-decode-save
2269   "O" gnus-uu-decode-save
2270   "b" gnus-uu-decode-binhex
2271   "B" gnus-uu-decode-binhex
2272   "Y" gnus-uu-decode-yenc
2273   "p" gnus-uu-decode-postscript
2274   "P" gnus-uu-decode-postscript-and-save)
2275
2276 (gnus-define-keys
2277     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2278   "u" gnus-uu-decode-uu-view
2279   "U" gnus-uu-decode-uu-and-save-view
2280   "s" gnus-uu-decode-unshar-view
2281   "S" gnus-uu-decode-unshar-and-save-view
2282   "o" gnus-uu-decode-save-view
2283   "O" gnus-uu-decode-save-view
2284   "b" gnus-uu-decode-binhex-view
2285   "B" gnus-uu-decode-binhex-view
2286   "p" gnus-uu-decode-postscript-view
2287   "P" gnus-uu-decode-postscript-and-save-view)
2288
2289 (defvar gnus-article-post-menu nil)
2290
2291 (defconst gnus-summary-menu-maxlen 20)
2292
2293 (defun gnus-summary-menu-split (menu)
2294   ;; If we have lots of elements, divide them into groups of 20
2295   ;; and make a pane (or submenu) for each one.
2296   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2297       (let ((menu menu) sublists next
2298             (i 1))
2299         (while menu
2300           ;; Pull off the next gnus-summary-menu-maxlen elements
2301           ;; and make them the next element of sublist.
2302           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2303           (if next
2304               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2305                       nil))
2306           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2307                                              (aref (car (last menu)) 0)) menu)
2308                                sublists))
2309           (setq i (1+ i))
2310           (setq menu next))
2311         (nreverse sublists))
2312     ;; Few elements--put them all in one pane.
2313     menu))
2314
2315 (defun gnus-summary-make-menu-bar ()
2316   (gnus-turn-off-edit-menu 'summary)
2317
2318   (unless (boundp 'gnus-summary-misc-menu)
2319
2320     (easy-menu-define
2321       gnus-summary-kill-menu gnus-summary-mode-map ""
2322       (cons
2323        "Score"
2324        (nconc
2325         (list
2326          ["Customize" gnus-score-customize t])
2327         (gnus-make-score-map 'increase)
2328         (gnus-make-score-map 'lower)
2329         '(("Mark"
2330            ["Kill below" gnus-summary-kill-below t]
2331            ["Mark above" gnus-summary-mark-above t]
2332            ["Tick above" gnus-summary-tick-above t]
2333            ["Clear above" gnus-summary-clear-above t])
2334           ["Current score" gnus-summary-current-score t]
2335           ["Set score" gnus-summary-set-score t]
2336           ["Switch current score file..." gnus-score-change-score-file t]
2337           ["Set mark below..." gnus-score-set-mark-below t]
2338           ["Set expunge below..." gnus-score-set-expunge-below t]
2339           ["Edit current score file" gnus-score-edit-current-scores t]
2340           ["Edit score file..." gnus-score-edit-file t]
2341           ["Trace score" gnus-score-find-trace t]
2342           ["Find words" gnus-score-find-favourite-words t]
2343           ["Rescore buffer" gnus-summary-rescore t]
2344           ["Increase score..." gnus-summary-increase-score t]
2345           ["Lower score..." gnus-summary-lower-score t]))))
2346
2347     ;; Define both the Article menu in the summary buffer and the
2348     ;; equivalent Commands menu in the article buffer here for
2349     ;; consistency.
2350     (let ((innards
2351            `(("Hide"
2352               ["All" gnus-article-hide t]
2353               ["Headers" gnus-article-hide-headers t]
2354               ["Signature" gnus-article-hide-signature t]
2355               ["Citation" gnus-article-hide-citation t]
2356               ["List identifiers" gnus-article-hide-list-identifiers t]
2357               ["Banner" gnus-article-strip-banner t]
2358               ["Boring headers" gnus-article-hide-boring-headers t])
2359              ("Highlight"
2360               ["All" gnus-article-highlight t]
2361               ["Headers" gnus-article-highlight-headers t]
2362               ["Signature" gnus-article-highlight-signature t]
2363               ["Citation" gnus-article-highlight-citation t])
2364              ("MIME"
2365               ["Words" gnus-article-decode-mime-words t]
2366               ["Charset" gnus-article-decode-charset t]
2367               ["QP" gnus-article-de-quoted-unreadable t]
2368               ["Base64" gnus-article-de-base64-unreadable t]
2369               ["View MIME buttons" gnus-summary-display-buttonized t]
2370               ["View all" gnus-mime-view-all-parts t]
2371               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2372               ["Encrypt body" gnus-article-encrypt-body
2373                :active (not (gnus-group-read-only-p))
2374                ,@(if (featurep 'xemacs) nil
2375                    '(:help "Encrypt the message body on disk"))]
2376               ["Extract all parts..." gnus-summary-save-parts t]
2377               ("Multipart"
2378                ["Repair multipart" gnus-summary-repair-multipart t]
2379                ["Pipe part..." gnus-article-pipe-part t]
2380                ["Inline part" gnus-article-inline-part t]
2381                ["View part as type..." gnus-article-view-part-as-type t]
2382                ["Encrypt body" gnus-article-encrypt-body
2383                 :active (not (gnus-group-read-only-p))
2384                ,@(if (featurep 'xemacs) nil
2385                    '(:help "Encrypt the message body on disk"))]
2386                ["View part externally" gnus-article-view-part-externally t]
2387                ["View HTML parts in browser" gnus-article-browse-html-article t]
2388                ["View part with charset..." gnus-article-view-part-as-charset t]
2389                ["Copy part" gnus-article-copy-part t]
2390                ["Save part..." gnus-article-save-part t]
2391                ["View part" gnus-article-view-part t]))
2392              ("Date"
2393               ["Local" gnus-article-date-local t]
2394               ["ISO8601" gnus-article-date-iso8601 t]
2395               ["UT" gnus-article-date-ut t]
2396               ["Original" gnus-article-date-original t]
2397               ["Lapsed" gnus-article-date-lapsed t]
2398               ["User-defined" gnus-article-date-user t])
2399              ("Display"
2400               ["Remove images" gnus-article-remove-images t]
2401               ["Toggle smiley" gnus-treat-smiley t]
2402               ["Show X-Face" gnus-article-display-x-face t]
2403               ["Show picons in From" gnus-treat-from-picon t]
2404               ["Show picons in mail headers" gnus-treat-mail-picon t]
2405               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2406               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2407               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2408               ("View as different encoding"
2409                ,@(gnus-summary-menu-split
2410                   (mapcar
2411                    (lambda (cs)
2412                      ;; Since easymenu under Emacs doesn't allow
2413                      ;; lambda forms for menu commands, we should
2414                      ;; provide intern'ed function symbols.
2415                      (let ((command (intern (format "\
2416 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2417                        (fset command
2418                              `(lambda ()
2419                                 (interactive)
2420                                 (let ((gnus-summary-show-article-charset-alist
2421                                        '((1 . ,cs))))
2422                                   (gnus-summary-show-article 1))))
2423                        `[,(symbol-name cs) ,command t]))
2424                    (sort (if (fboundp 'coding-system-list)
2425                              (coding-system-list)
2426                            (mapcar 'car mm-mime-mule-charset-alist))
2427                          'string<)))))
2428              ("Washing"
2429               ("Remove Blanks"
2430                ["Leading" gnus-article-strip-leading-blank-lines t]
2431                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2432                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2433                ["All of the above" gnus-article-strip-blank-lines t]
2434                ["All" gnus-article-strip-all-blank-lines t]
2435                ["Leading space" gnus-article-strip-leading-space t]
2436                ["Trailing space" gnus-article-strip-trailing-space t]
2437                ["Leading space in headers"
2438                 gnus-article-remove-leading-whitespace t])
2439               ["Overstrike" gnus-article-treat-overstrike t]
2440               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2441               ["Non-ASCII" gnus-article-treat-non-ascii t]
2442               ["Emphasis" gnus-article-emphasize t]
2443               ["Word wrap" gnus-article-fill-cited-article t]
2444               ["Fill long lines" gnus-article-fill-long-lines t]
2445               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2446               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2447               ["Remove CR" gnus-article-remove-cr t]
2448               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2449               ["Base64" gnus-article-de-base64-unreadable t]
2450               ["Rot 13" gnus-summary-caesar-message
2451                ,@(if (featurep 'xemacs) '(t)
2452                    '(:help "\"Caesar rotate\" article by 13"))]
2453               ["De-IDNA" gnus-summary-idna-message t]
2454               ["Morse decode" gnus-summary-morse-message t]
2455               ["Unix pipe..." gnus-summary-pipe-message t]
2456               ["Add buttons" gnus-article-add-buttons t]
2457               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2458               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2459               ["Verbose header" gnus-summary-verbose-headers t]
2460               ["Toggle header" gnus-summary-toggle-header t]
2461               ["Unfold headers" gnus-article-treat-unfold-headers t]
2462               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2463               ["Html" gnus-article-wash-html t]
2464               ["Unsplit URLs" gnus-article-unsplit-urls t]
2465               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2466               ["Decode HZ" gnus-article-decode-HZ t]
2467               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2468               ("(Outlook) Deuglify"
2469                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2470                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2471                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2472                ["Full (Outlook) deuglify"
2473                 gnus-article-outlook-deuglify-article t])
2474               )
2475              ("Output"
2476               ["Save in default format..." gnus-summary-save-article
2477                ,@(if (featurep 'xemacs) '(t)
2478                    '(:help "Save article using default method"))]
2479               ["Save in file..." gnus-summary-save-article-file
2480                ,@(if (featurep 'xemacs) '(t)
2481                    '(:help "Save article in file"))]
2482               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2483               ["Save in MH folder..." gnus-summary-save-article-folder t]
2484               ["Save in VM folder..." gnus-summary-save-article-vm t]
2485               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2486               ["Save body in file..." gnus-summary-save-article-body-file t]
2487               ["Pipe through a filter..." gnus-summary-pipe-output t]
2488               ["Print with Muttprint..." gnus-summary-muttprint t]
2489               ["Print" gnus-summary-print-article
2490                ,@(if (featurep 'xemacs) '(t)
2491                    '(:help "Generate and print a PostScript image"))])
2492              ("Copy, move,... (Backend)"
2493               ,@(if (featurep 'xemacs) nil
2494                   '(:help "Copying, moving, expiring articles..."))
2495               ["Respool article..." gnus-summary-respool-article t]
2496               ["Move article..." gnus-summary-move-article
2497                (gnus-check-backend-function
2498                 'request-move-article gnus-newsgroup-name)]
2499               ["Copy article..." gnus-summary-copy-article t]
2500               ["Crosspost article..." gnus-summary-crosspost-article
2501                (gnus-check-backend-function
2502                 'request-replace-article gnus-newsgroup-name)]
2503               ["Import file..." gnus-summary-import-article
2504                (gnus-check-backend-function
2505                 'request-accept-article gnus-newsgroup-name)]
2506               ["Create article..." gnus-summary-create-article
2507                (gnus-check-backend-function
2508                 'request-accept-article gnus-newsgroup-name)]
2509               ["Check if posted" gnus-summary-article-posted-p t]
2510               ["Edit article" gnus-summary-edit-article
2511                (not (gnus-group-read-only-p))]
2512               ["Delete article" gnus-summary-delete-article
2513                (gnus-check-backend-function
2514                 'request-expire-articles gnus-newsgroup-name)]
2515               ["Query respool" gnus-summary-respool-query t]
2516               ["Trace respool" gnus-summary-respool-trace t]
2517               ["Delete expirable articles" gnus-summary-expire-articles-now
2518                (gnus-check-backend-function
2519                 'request-expire-articles gnus-newsgroup-name)])
2520              ("Extract"
2521               ["Uudecode" gnus-uu-decode-uu
2522                ,@(if (featurep 'xemacs) '(t)
2523                    '(:help "Decode uuencoded article(s)"))]
2524               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2525               ["Unshar" gnus-uu-decode-unshar t]
2526               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2527               ["Save" gnus-uu-decode-save t]
2528               ["Binhex" gnus-uu-decode-binhex t]
2529               ["PostScript" gnus-uu-decode-postscript t]
2530               ["All MIME parts" gnus-summary-save-parts t])
2531              ("Cache"
2532               ["Enter article" gnus-cache-enter-article t]
2533               ["Remove article" gnus-cache-remove-article t])
2534              ["Translate" gnus-article-babel t]
2535              ["Select article buffer" gnus-summary-select-article-buffer t]
2536              ["Make article buffer sticky" gnus-sticky-article t]
2537              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2538              ["Isearch article..." gnus-summary-isearch-article t]
2539              ["Beginning of the article" gnus-summary-beginning-of-article t]
2540              ["End of the article" gnus-summary-end-of-article t]
2541              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2542              ["Fetch referenced articles" gnus-summary-refer-references t]
2543              ["Fetch current thread" gnus-summary-refer-thread t]
2544              ["Fetch article with id..." gnus-summary-refer-article t]
2545              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2546              ["Redisplay" gnus-summary-show-article t]
2547              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2548       (easy-menu-define
2549         gnus-summary-article-menu gnus-summary-mode-map ""
2550         (cons "Article" innards))
2551
2552       (if (not (keymapp gnus-summary-article-menu))
2553           (easy-menu-define
2554             gnus-article-commands-menu gnus-article-mode-map ""
2555             (cons "Commands" innards))
2556         ;; in Emacs, don't share menu.
2557         (setq gnus-article-commands-menu
2558               (copy-keymap gnus-summary-article-menu))
2559         (define-key gnus-article-mode-map [menu-bar commands]
2560           (cons "Commands" gnus-article-commands-menu))))
2561
2562     (easy-menu-define
2563       gnus-summary-thread-menu gnus-summary-mode-map ""
2564       '("Threads"
2565         ["Find all messages in thread" gnus-summary-refer-thread t]
2566         ["Toggle threading" gnus-summary-toggle-threads t]
2567         ["Hide threads" gnus-summary-hide-all-threads t]
2568         ["Show threads" gnus-summary-show-all-threads t]
2569         ["Hide thread" gnus-summary-hide-thread t]
2570         ["Show thread" gnus-summary-show-thread t]
2571         ["Go to next thread" gnus-summary-next-thread t]
2572         ["Go to previous thread" gnus-summary-prev-thread t]
2573         ["Go down thread" gnus-summary-down-thread t]
2574         ["Go up thread" gnus-summary-up-thread t]
2575         ["Top of thread" gnus-summary-top-thread t]
2576         ["Mark thread as read" gnus-summary-kill-thread t]
2577         ["Mark thread as expired" gnus-summary-expire-thread t]
2578         ["Lower thread score" gnus-summary-lower-thread t]
2579         ["Raise thread score" gnus-summary-raise-thread t]
2580         ["Rethread current" gnus-summary-rethread-current t]))
2581
2582     (easy-menu-define
2583       gnus-summary-post-menu gnus-summary-mode-map ""
2584       `("Post"
2585         ["Send a message (mail or news)" gnus-summary-post-news
2586          ,@(if (featurep 'xemacs) '(t)
2587              '(:help "Compose a new message (mail or news)"))]
2588         ["Followup" gnus-summary-followup
2589          ,@(if (featurep 'xemacs) '(t)
2590              '(:help "Post followup to this article"))]
2591         ["Followup and yank" gnus-summary-followup-with-original
2592          ,@(if (featurep 'xemacs) '(t)
2593              '(:help "Post followup to this article, quoting its contents"))]
2594         ["Supersede article" gnus-summary-supersede-article t]
2595         ["Cancel article" gnus-summary-cancel-article
2596          ,@(if (featurep 'xemacs) '(t)
2597              '(:help "Cancel an article you posted"))]
2598         ["Reply" gnus-summary-reply t]
2599         ["Reply and yank" gnus-summary-reply-with-original t]
2600         ["Wide reply" gnus-summary-wide-reply t]
2601         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2602          ,@(if (featurep 'xemacs) '(t)
2603              '(:help "Mail a reply, quoting this article"))]
2604         ["Very wide reply" gnus-summary-very-wide-reply t]
2605         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2606          ,@(if (featurep 'xemacs) '(t)
2607              '(:help "Mail a very wide reply, quoting this article"))]
2608         ["Mail forward" gnus-summary-mail-forward t]
2609         ["Post forward" gnus-summary-post-forward t]
2610         ["Digest and mail" gnus-uu-digest-mail-forward t]
2611         ["Digest and post" gnus-uu-digest-post-forward t]
2612         ["Resend message" gnus-summary-resend-message t]
2613         ["Resend message edit" gnus-summary-resend-message-edit t]
2614         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2615         ["Send a mail" gnus-summary-mail-other-window t]
2616         ["Create a local message" gnus-summary-news-other-window t]
2617         ["Uuencode and post" gnus-uu-post-news
2618          ,@(if (featurep 'xemacs) '(t)
2619              '(:help "Post a uuencoded article"))]
2620         ["Followup via news" gnus-summary-followup-to-mail t]
2621         ["Followup via news and yank"
2622          gnus-summary-followup-to-mail-with-original t]
2623         ["Strip signature on reply"
2624          (lambda ()
2625            (interactive)
2626            (if (not (memq message-cite-function
2627                           '(message-cite-original-without-signature
2628                             message-cite-original)))
2629                ;; Stupid workaround for XEmacs not honoring :visible.
2630                (message "Can't toggle this value of `message-cite-function'")
2631              (setq message-cite-function
2632                    (if (eq message-cite-function
2633                            'message-cite-original-without-signature)
2634                        'message-cite-original
2635                      'message-cite-original-without-signature))))
2636          ;; XEmacs barfs on :visible.
2637          ,@(if (featurep 'xemacs) nil
2638              '(:visible (memq message-cite-function
2639                               '(message-cite-original-without-signature
2640                                 message-cite-original))))
2641          :style toggle
2642          :selected (eq message-cite-function
2643                        'message-cite-original-without-signature)
2644          ,@(if (featurep 'xemacs) nil
2645              '(:help "Strip signature from cited article when replying."))]
2646         ;;("Draft"
2647         ;;["Send" gnus-summary-send-draft t]
2648         ;;["Send bounced" gnus-resend-bounced-mail t])
2649         ))
2650
2651     (cond
2652      ((not (keymapp gnus-summary-post-menu))
2653       (setq gnus-article-post-menu gnus-summary-post-menu))
2654      ((not gnus-article-post-menu)
2655       ;; Don't share post menu.
2656       (setq gnus-article-post-menu
2657             (copy-keymap gnus-summary-post-menu))))
2658     (define-key gnus-article-mode-map [menu-bar post]
2659       (cons "Post" gnus-article-post-menu))
2660
2661     (easy-menu-define
2662       gnus-summary-misc-menu gnus-summary-mode-map ""
2663       `("Gnus"
2664         ("Mark Read"
2665          ["Mark as read" gnus-summary-mark-as-read-forward t]
2666          ["Mark same subject and select"
2667           gnus-summary-kill-same-subject-and-select t]
2668          ["Mark same subject" gnus-summary-kill-same-subject t]
2669          ["Catchup" gnus-summary-catchup
2670           ,@(if (featurep 'xemacs) '(t)
2671               '(:help "Mark unread articles in this group as read"))]
2672          ["Catchup all" gnus-summary-catchup-all t]
2673          ["Catchup to here" gnus-summary-catchup-to-here t]
2674          ["Catchup from here" gnus-summary-catchup-from-here t]
2675          ["Catchup region" gnus-summary-mark-region-as-read
2676           (gnus-mark-active-p)]
2677          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2678         ("Mark Various"
2679          ["Tick" gnus-summary-tick-article-forward t]
2680          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2681          ["Remove marks" gnus-summary-clear-mark-forward t]
2682          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2683          ["Set bookmark" gnus-summary-set-bookmark t]
2684          ["Remove bookmark" gnus-summary-remove-bookmark t])
2685         ("Limit to"
2686          ["Marks..." gnus-summary-limit-to-marks t]
2687          ["Subject..." gnus-summary-limit-to-subject t]
2688          ["Author..." gnus-summary-limit-to-author t]
2689          ["Recipient..." gnus-summary-limit-to-recipient t]
2690          ["Address..." gnus-summary-limit-to-address t]
2691          ["Age..." gnus-summary-limit-to-age t]
2692          ["Extra..." gnus-summary-limit-to-extra t]
2693          ["Score..." gnus-summary-limit-to-score t]
2694          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2695          ["Unread" gnus-summary-limit-to-unread t]
2696          ["Unseen" gnus-summary-limit-to-unseen t]
2697          ["Singletons" gnus-summary-limit-to-singletons t]
2698          ["Replied" gnus-summary-limit-to-replied t]
2699          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2700          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2701          ["Pop limit" gnus-summary-pop-limit t]
2702          ["Show dormant" gnus-summary-limit-include-dormant t]
2703          ["Hide childless dormant"
2704           gnus-summary-limit-exclude-childless-dormant t]
2705          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2706          ["Hide marked" gnus-summary-limit-exclude-marks t]
2707          ["Show expunged" gnus-summary-limit-include-expunged t])
2708         ("Process Mark"
2709          ["Set mark" gnus-summary-mark-as-processable t]
2710          ["Remove mark" gnus-summary-unmark-as-processable t]
2711          ["Remove all marks" gnus-summary-unmark-all-processable t]
2712          ["Invert marks" gnus-uu-invert-processable t]
2713          ["Mark above" gnus-uu-mark-over t]
2714          ["Mark series" gnus-uu-mark-series t]
2715          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2716          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2717          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2718          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2719          ["Mark all" gnus-uu-mark-all t]
2720          ["Mark buffer" gnus-uu-mark-buffer t]
2721          ["Mark sparse" gnus-uu-mark-sparse t]
2722          ["Mark thread" gnus-uu-mark-thread t]
2723          ["Unmark thread" gnus-uu-unmark-thread t]
2724          ("Process Mark Sets"
2725           ["Kill" gnus-summary-kill-process-mark t]
2726           ["Yank" gnus-summary-yank-process-mark
2727            gnus-newsgroup-process-stack]
2728           ["Save" gnus-summary-save-process-mark t]
2729           ["Run command on marked..." gnus-summary-universal-argument t]))
2730         ("Registry Marks")
2731         ("Scroll article"
2732          ["Page forward" gnus-summary-next-page
2733           ,@(if (featurep 'xemacs) '(t)
2734               '(:help "Show next page of article"))]
2735          ["Page backward" gnus-summary-prev-page
2736           ,@(if (featurep 'xemacs) '(t)
2737               '(:help "Show previous page of article"))]
2738          ["Line forward" gnus-summary-scroll-up t])
2739         ("Move"
2740          ["Next unread article" gnus-summary-next-unread-article t]
2741          ["Previous unread article" gnus-summary-prev-unread-article t]
2742          ["Next article" gnus-summary-next-article t]
2743          ["Previous article" gnus-summary-prev-article t]
2744          ["Next unread subject" gnus-summary-next-unread-subject t]
2745          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2746          ["Next article same subject" gnus-summary-next-same-subject t]
2747          ["Previous article same subject" gnus-summary-prev-same-subject t]
2748          ["First unread article" gnus-summary-first-unread-article t]
2749          ["Best unread article" gnus-summary-best-unread-article t]
2750          ["Go to subject number..." gnus-summary-goto-subject t]
2751          ["Go to article number..." gnus-summary-goto-article t]
2752          ["Go to the last article" gnus-summary-goto-last-article t]
2753          ["Pop article off history" gnus-summary-pop-article t])
2754         ("Sort"
2755          ["Sort by number" gnus-summary-sort-by-number t]
2756          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2757          ["Sort by author" gnus-summary-sort-by-author t]
2758          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2759          ["Sort by subject" gnus-summary-sort-by-subject t]
2760          ["Sort by date" gnus-summary-sort-by-date t]
2761          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2762          ["Sort by score" gnus-summary-sort-by-score t]
2763          ["Sort by lines" gnus-summary-sort-by-lines t]
2764          ["Sort by characters" gnus-summary-sort-by-chars t]
2765          ["Randomize" gnus-summary-sort-by-random t]
2766          ["Original sort" gnus-summary-sort-by-original t])
2767         ("Help"
2768          ["Describe group" gnus-summary-describe-group t]
2769          ["Read manual" gnus-info-find-node t])
2770         ("Modes"
2771          ["Pick and read" gnus-pick-mode t]
2772          ["Binary" gnus-binary-mode t])
2773         ("Regeneration"
2774          ["Regenerate" gnus-summary-prepare t]
2775          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2776          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2777          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2778          ["Toggle threading" gnus-summary-toggle-threads t])
2779         ["See old articles" gnus-summary-insert-old-articles t]
2780         ["See new articles" gnus-summary-insert-new-articles t]
2781         ["Filter articles..." gnus-summary-execute-command t]
2782         ["Run command on articles..." gnus-summary-universal-argument t]
2783         ["Search articles forward..." gnus-summary-search-article-forward t]
2784         ["Search articles backward..." gnus-summary-search-article-backward t]
2785         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2786         ["Expand window" gnus-summary-expand-window t]
2787         ["Expire expirable articles" gnus-summary-expire-articles
2788          (gnus-check-backend-function
2789           'request-expire-articles gnus-newsgroup-name)]
2790         ["Edit local kill file" gnus-summary-edit-local-kill t]
2791         ["Edit main kill file" gnus-summary-edit-global-kill t]
2792         ["Edit group parameters" gnus-summary-edit-parameters t]
2793         ["Customize group parameters" gnus-summary-customize-parameters t]
2794         ["Send a bug report" gnus-bug t]
2795         ("Exit"
2796          ["Catchup and exit" gnus-summary-catchup-and-exit
2797           ,@(if (featurep 'xemacs) '(t)
2798               '(:help "Mark unread articles in this group as read, then exit"))]
2799          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2800          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2801          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2802          ["Exit group" gnus-summary-exit
2803           ,@(if (featurep 'xemacs) '(t)
2804               '(:help "Exit current group, return to group selection mode"))]
2805          ["Exit group without updating" gnus-summary-exit-no-update t]
2806          ["Exit and goto next group" gnus-summary-next-group t]
2807          ["Exit and goto prev group" gnus-summary-prev-group t]
2808          ["Reselect group" gnus-summary-reselect-current-group t]
2809          ["Rescan group" gnus-summary-rescan-group t]
2810          ["Update dribble" gnus-summary-save-newsrc t])))
2811
2812     (gnus-run-hooks 'gnus-summary-menu-hook)))
2813
2814 (defvar gnus-summary-tool-bar-map nil)
2815
2816 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2817 ;; affect _new_ message buffers.  We might add a function that walks thru all
2818 ;; summary-mode buffers and force the update.
2819 (defun gnus-summary-tool-bar-update (&optional symbol value)
2820   "Update summary mode toolbar.
2821 Setter function for custom variables."
2822   (setq-default gnus-summary-tool-bar-map nil)
2823   (when symbol
2824     ;; When used as ":set" function:
2825     (set-default symbol value))
2826   (when (gnus-buffer-live-p gnus-summary-buffer)
2827     (with-current-buffer gnus-summary-buffer
2828       (gnus-summary-make-tool-bar))))
2829
2830 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2831                                      'gnus-summary-tool-bar-gnome
2832                                    'gnus-summary-tool-bar-retro)
2833   "Specifies the Gnus summary tool bar.
2834
2835 It can be either a list or a symbol referring to a list.  See
2836 `gmm-tool-bar-from-list' for the format of the list.  The
2837 default key map is `gnus-summary-mode-map'.
2838
2839 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2840 `gnus-summary-tool-bar-retro'."
2841   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2842                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2843                  (repeat :tag "User defined list" gmm-tool-bar-item)
2844                  (symbol))
2845   :version "23.1" ;; No Gnus
2846   :initialize 'custom-initialize-default
2847   :set 'gnus-summary-tool-bar-update
2848   :group 'gnus-summary)
2849
2850 (defcustom gnus-summary-tool-bar-gnome
2851   '((gnus-summary-post-news "mail/compose" nil)
2852     (gnus-summary-insert-new-articles "mail/inbox" nil
2853                                       :visible (or (not gnus-agent)
2854                                                    gnus-plugged))
2855     (gnus-summary-reply-with-original "mail/reply")
2856     (gnus-summary-reply "mail/reply" nil :visible nil)
2857     (gnus-summary-followup-with-original "mail/reply-all")
2858     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2859     (gnus-summary-mail-forward "mail/forward")
2860     (gnus-summary-save-article "mail/save")
2861     (gnus-summary-search-article-forward "search" nil :visible nil)
2862     (gnus-summary-print-article "print")
2863     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2864     ;; Some new commands that may need more suitable icons:
2865     (gnus-summary-save-newsrc "save" nil :visible nil)
2866     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2867     (gnus-summary-prev-article "left-arrow")
2868     (gnus-summary-next-article "right-arrow")
2869     (gnus-summary-next-page "next-page")
2870     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2871     ;;
2872     ;; Maybe some sort-by-... could be added:
2873     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2874     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2875     (gnus-summary-mark-as-expirable
2876      "delete" nil
2877      :visible (gnus-check-backend-function 'request-expire-articles
2878                                            gnus-newsgroup-name))
2879     (gnus-summary-mark-as-spam
2880      "mail/spam" t
2881      :visible (and (fboundp 'spam-group-ham-contents-p)
2882                    (spam-group-ham-contents-p gnus-newsgroup-name))
2883      :help "Mark as spam")
2884     (gnus-summary-mark-as-read-forward
2885      "mail/not-spam" nil
2886      :visible (and (fboundp 'spam-group-spam-contents-p)
2887                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2888     ;;
2889     (gnus-summary-exit "exit")
2890     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2891     (gnus-info-find-node "help"))
2892   "List of functions for the summary tool bar (GNOME style).
2893
2894 See `gmm-tool-bar-from-list' for the format of the list."
2895   :type '(repeat gmm-tool-bar-item)
2896   :version "23.1" ;; No Gnus
2897   :initialize 'custom-initialize-default
2898   :set 'gnus-summary-tool-bar-update
2899   :group 'gnus-summary)
2900
2901 (defcustom gnus-summary-tool-bar-retro
2902   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2903     (gnus-summary-next-unread-article "gnus/next-ur")
2904     (gnus-summary-post-news "gnus/post")
2905     (gnus-summary-followup-with-original "gnus/fuwo")
2906     (gnus-summary-followup "gnus/followup")
2907     (gnus-summary-reply-with-original "gnus/reply-wo")
2908     (gnus-summary-reply "gnus/reply")
2909     (gnus-summary-caesar-message "gnus/rot13")
2910     (gnus-uu-decode-uu "gnus/uu-decode")
2911     (gnus-summary-save-article-file "gnus/save-aif")
2912     (gnus-summary-save-article "gnus/save-art")
2913     (gnus-uu-post-news "gnus/uu-post")
2914     (gnus-summary-catchup "gnus/catchup")
2915     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2916     (gnus-summary-exit "gnus/exit-summ")
2917     ;; Some new command that may need more suitable icons:
2918     (gnus-summary-print-article "gnus/print" nil :visible nil)
2919     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2920     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2921     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2922     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2923     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2924     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2925     ;;
2926     (gnus-info-find-node "gnus/help" nil :visible nil))
2927   "List of functions for the summary tool bar (retro look).
2928
2929 See `gmm-tool-bar-from-list' for the format of the list."
2930   :type '(repeat gmm-tool-bar-item)
2931   :version "23.1" ;; No Gnus
2932   :initialize 'custom-initialize-default
2933   :set 'gnus-summary-tool-bar-update
2934   :group 'gnus-summary)
2935
2936 (defcustom gnus-summary-tool-bar-zap-list t
2937   "List of icon items from the global tool bar.
2938 These items are not displayed in the Gnus summary mode tool bar.
2939
2940 See `gmm-tool-bar-from-list' for the format of the list."
2941   :type 'gmm-tool-bar-zap-list
2942   :version "23.1" ;; No Gnus
2943   :initialize 'custom-initialize-default
2944   :set 'gnus-summary-tool-bar-update
2945   :group 'gnus-summary)
2946
2947 (defvar image-load-path)
2948 (defvar tool-bar-map)
2949
2950 (defun gnus-summary-make-tool-bar (&optional force)
2951   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2952 When FORCE, rebuild the tool bar."
2953   (when (and (not (featurep 'xemacs))
2954              (boundp 'tool-bar-mode)
2955              tool-bar-mode
2956              (or (not gnus-summary-tool-bar-map) force))
2957     (let* ((load-path
2958             (gmm-image-load-path-for-library "gnus"
2959                                              "mail/save.xpm"
2960                                              nil t))
2961            (image-load-path (cons (car load-path)
2962                                   (when (boundp 'image-load-path)
2963                                     image-load-path)))
2964            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2965                                         gnus-summary-tool-bar-zap-list
2966                                         'gnus-summary-mode-map)))
2967       (when map
2968         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2969         ;; uses its value.
2970         (setq gnus-summary-tool-bar-map map))))
2971   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2972
2973 (defun gnus-score-set-default (var value)
2974   "A version of set that updates the GNU Emacs menu-bar."
2975   (set var value)
2976   ;; It is the message that forces the active status to be updated.
2977   (message ""))
2978
2979 (defun gnus-make-score-map (type)
2980   "Make a summary score map of type TYPE."
2981   (if t
2982       nil
2983     (let ((headers '(("author" "from" string)
2984                      ("subject" "subject" string)
2985                      ("article body" "body" string)
2986                      ("article head" "head" string)
2987                      ("xref" "xref" string)
2988                      ("extra header" "extra" string)
2989                      ("lines" "lines" number)
2990                      ("followups to author" "followup" string)))
2991           (types '((number ("less than" <)
2992                            ("greater than" >)
2993                            ("equal" =))
2994                    (string ("substring" s)
2995                            ("exact string" e)
2996                            ("fuzzy string" f)
2997                            ("regexp" r))))
2998           (perms '(("temporary" (current-time-string))
2999                    ("permanent" nil)
3000                    ("immediate" now)))
3001           header)
3002       (list
3003        (apply
3004         'nconc
3005         (list
3006          (if (eq type 'lower)
3007              "Lower score"
3008            "Increase score"))
3009         (let (outh)
3010           (while headers
3011             (setq header (car headers))
3012             (setq outh
3013                   (cons
3014                    (apply
3015                     'nconc
3016                     (list (car header))
3017                     (let ((ts (cdr (assoc (nth 2 header) types)))
3018                           outt)
3019                       (while ts
3020                         (setq outt
3021                               (cons
3022                                (apply
3023                                 'nconc
3024                                 (list (caar ts))
3025                                 (let ((ps perms)
3026                                       outp)
3027                                   (while ps
3028                                     (setq outp
3029                                           (cons
3030                                            (vector
3031                                             (caar ps)
3032                                             (list
3033                                              'gnus-summary-score-entry
3034                                              (nth 1 header)
3035                                              (if (or (string= (nth 1 header)
3036                                                               "head")
3037                                                      (string= (nth 1 header)
3038                                                               "body"))
3039                                                  ""
3040                                                (list 'gnus-summary-header
3041                                                      (nth 1 header)))
3042                                              (list 'quote (nth 1 (car ts)))
3043                                              (list 'gnus-score-delta-default
3044                                                    nil)
3045                                              (nth 1 (car ps))
3046                                              t)
3047                                             t)
3048                                            outp))
3049                                     (setq ps (cdr ps)))
3050                                   (list (nreverse outp))))
3051                                outt))
3052                         (setq ts (cdr ts)))
3053                       (list (nreverse outt))))
3054                    outh))
3055             (setq headers (cdr headers)))
3056           (list (nreverse outh))))))))
3057
3058
3059 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3060 (defvar bookmark-make-record-function)
3061 \f
3062 (defvar bidi-paragraph-direction)
3063
3064 (defun gnus-summary-mode (&optional group)
3065   "Major mode for reading articles.
3066
3067 All normal editing commands are switched off.
3068 \\<gnus-summary-mode-map>
3069 Each line in this buffer represents one article.  To read an
3070 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3071 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3072 respectively.
3073
3074 You can also post articles and send mail from this buffer.  To
3075 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3076 of an article, type `\\[gnus-summary-reply]'.
3077
3078 There are approx. one gazillion commands you can execute in this
3079 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3080
3081 The following commands are available:
3082
3083 \\{gnus-summary-mode-map}"
3084   (interactive)
3085   (kill-all-local-variables)
3086   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3087     (gnus-summary-make-local-variables))
3088   (gnus-summary-make-local-variables)
3089   (setq gnus-newsgroup-name group)
3090   (when (gnus-visual-p 'summary-menu 'menu)
3091     (gnus-summary-make-menu-bar)
3092     (gnus-summary-make-tool-bar))
3093   (gnus-make-thread-indent-array)
3094   (gnus-simplify-mode-line)
3095   (setq major-mode 'gnus-summary-mode)
3096   (setq mode-name "Summary")
3097   (use-local-map gnus-summary-mode-map)
3098   (buffer-disable-undo)
3099   (setq buffer-read-only t              ;Disable modification
3100         show-trailing-whitespace nil)
3101   (setq truncate-lines t)
3102   ;; Force paragraph direction to be left-to-right.  Don't make it
3103   ;; bound globally in old Emacsen and XEmacsen.
3104   (set (make-local-variable 'bidi-paragraph-direction) 'left-to-right)
3105   (add-to-invisibility-spec '(gnus-sum . t))
3106   (gnus-summary-set-display-table)
3107   (gnus-set-default-directory)
3108   (make-local-variable 'gnus-summary-line-format)
3109   (make-local-variable 'gnus-summary-line-format-spec)
3110   (make-local-variable 'gnus-summary-dummy-line-format)
3111   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3112   (make-local-variable 'gnus-summary-mark-positions)
3113   (gnus-make-local-hook 'pre-command-hook)
3114   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3115   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3116   (turn-on-gnus-mailing-list-mode)
3117   (mm-enable-multibyte)
3118   (set (make-local-variable 'bookmark-make-record-function)
3119        'gnus-summary-bookmark-make-record)
3120   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3121   (gnus-update-summary-mark-positions))
3122
3123 (defun gnus-summary-make-local-variables ()
3124   "Make all the local summary buffer variables."
3125   (let (global)
3126     (dolist (local gnus-summary-local-variables)
3127       (if (consp local)
3128           (progn
3129             (if (eq (cdr local) 'global)
3130                 ;; Copy the global value of the variable.
3131                 (setq global (symbol-value (car local)))
3132               ;; Use the value from the list.
3133               (setq global (eval (cdr local))))
3134             (set (make-local-variable (car local)) global))
3135         ;; Simple nil-valued local variable.
3136         (set (make-local-variable local) nil)))))
3137
3138 ;; Summary data functions.
3139
3140 (defmacro gnus-data-number (data)
3141   `(car ,data))
3142
3143 (defmacro gnus-data-set-number (data number)
3144   `(setcar ,data ,number))
3145
3146 (defmacro gnus-data-mark (data)
3147   `(nth 1 ,data))
3148
3149 (defmacro gnus-data-set-mark (data mark)
3150   `(setcar (nthcdr 1 ,data) ,mark))
3151
3152 (defmacro gnus-data-pos (data)
3153   `(nth 2 ,data))
3154
3155 (defmacro gnus-data-set-pos (data pos)
3156   `(setcar (nthcdr 2 ,data) ,pos))
3157
3158 (defmacro gnus-data-header (data)
3159   `(nth 3 ,data))
3160
3161 (defmacro gnus-data-set-header (data header)
3162   `(setf (nth 3 ,data) ,header))
3163
3164 (defmacro gnus-data-level (data)
3165   `(nth 4 ,data))
3166
3167 (defmacro gnus-data-unread-p (data)
3168   `(= (nth 1 ,data) gnus-unread-mark))
3169
3170 (defmacro gnus-data-read-p (data)
3171   `(/= (nth 1 ,data) gnus-unread-mark))
3172
3173 (defmacro gnus-data-pseudo-p (data)
3174   `(consp (nth 3 ,data)))
3175
3176 (defmacro gnus-data-find (number)
3177   `(assq ,number gnus-newsgroup-data))
3178
3179 (defmacro gnus-data-find-list (number &optional data)
3180   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3181      (memq (assq ,number bdata)
3182            bdata)))
3183
3184 (defmacro gnus-data-make (number mark pos header level)
3185   `(list ,number ,mark ,pos ,header ,level))
3186
3187 (defun gnus-data-enter (after-article number mark pos header level offset)
3188   (let ((data (gnus-data-find-list after-article)))
3189     (unless data
3190       (error "No such article: %d" after-article))
3191     (setcdr data (cons (gnus-data-make number mark pos header level)
3192                        (cdr data)))
3193     (setq gnus-newsgroup-data-reverse nil)
3194     (gnus-data-update-list (cddr data) offset)))
3195
3196 (defun gnus-data-enter-list (after-article list &optional offset)
3197   (when list
3198     (let ((data (and after-article (gnus-data-find-list after-article)))
3199           (ilist list))
3200       (if (not (or data
3201                    after-article))
3202           (let ((odata gnus-newsgroup-data))
3203             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3204             (when offset
3205               (gnus-data-update-list odata offset)))
3206         ;; Find the last element in the list to be spliced into the main
3207         ;; list.
3208         (setq list (last list))
3209         (if (not data)
3210             (progn
3211               (setcdr list gnus-newsgroup-data)
3212               (setq gnus-newsgroup-data ilist)
3213               (when offset
3214                 (gnus-data-update-list (cdr list) offset)))
3215           (setcdr list (cdr data))
3216           (setcdr data ilist)
3217           (when offset
3218             (gnus-data-update-list (cdr list) offset))))
3219       (setq gnus-newsgroup-data-reverse nil))))
3220
3221 (defun gnus-data-remove (article &optional offset)
3222   (let ((data gnus-newsgroup-data))
3223     (if (= (gnus-data-number (car data)) article)
3224         (progn
3225           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3226                 gnus-newsgroup-data-reverse nil)
3227           (when offset
3228             (gnus-data-update-list gnus-newsgroup-data offset)))
3229       (while (cdr data)
3230         (when (= (gnus-data-number (cadr data)) article)
3231           (setcdr data (cddr data))
3232           (when offset
3233             (gnus-data-update-list (cdr data) offset))
3234           (setq data nil
3235                 gnus-newsgroup-data-reverse nil))
3236         (setq data (cdr data))))))
3237
3238 (defmacro gnus-data-list (backward)
3239   `(if ,backward
3240        (or gnus-newsgroup-data-reverse
3241            (setq gnus-newsgroup-data-reverse
3242                  (reverse gnus-newsgroup-data)))
3243      gnus-newsgroup-data))
3244
3245 (defun gnus-data-update-list (data offset)
3246   "Add OFFSET to the POS of all data entries in DATA."
3247   (setq gnus-newsgroup-data-reverse nil)
3248   (while data
3249     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3250     (setq data (cdr data))))
3251
3252 (defun gnus-summary-article-pseudo-p (article)
3253   "Say whether this article is a pseudo article or not."
3254   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3255
3256 (defmacro gnus-summary-article-sparse-p (article)
3257   "Say whether this article is a sparse article or not."
3258   `(memq ,article gnus-newsgroup-sparse))
3259
3260 (defmacro gnus-summary-article-ancient-p (article)
3261   "Say whether this article is a sparse article or not."
3262   `(memq ,article gnus-newsgroup-ancient))
3263
3264 (defun gnus-article-parent-p (number)
3265   "Say whether this article is a parent or not."
3266   (let ((data (gnus-data-find-list number)))
3267     (and (cdr data)              ; There has to be an article after...
3268          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3269             (gnus-data-level (nth 1 data))))))
3270
3271 (defun gnus-article-children (number)
3272   "Return a list of all children to NUMBER."
3273   (let* ((data (gnus-data-find-list number))
3274          (level (gnus-data-level (car data)))
3275          children)
3276     (setq data (cdr data))
3277     (while (and data
3278                 (= (gnus-data-level (car data)) (1+ level)))
3279       (push (gnus-data-number (car data)) children)
3280       (setq data (cdr data)))
3281     children))
3282
3283 (defmacro gnus-summary-skip-intangible ()
3284   "If the current article is intangible, then jump to a different article."
3285   '(let ((to (get-text-property (point) 'gnus-intangible)))
3286      (and to (gnus-summary-goto-subject to))))
3287
3288 (defmacro gnus-summary-article-intangible-p ()
3289   "Say whether this article is intangible or not."
3290   '(get-text-property (point) 'gnus-intangible))
3291
3292 (defun gnus-article-read-p (article)
3293   "Say whether ARTICLE is read or not."
3294   (not (or (memq article gnus-newsgroup-marked)
3295            (memq article gnus-newsgroup-spam-marked)
3296            (memq article gnus-newsgroup-unreads)
3297            (memq article gnus-newsgroup-unselected)
3298            (memq article gnus-newsgroup-dormant))))
3299
3300 ;; Some summary mode macros.
3301
3302 (defmacro gnus-summary-article-number ()
3303   "The article number of the article on the current line.
3304 If there isn't an article number here, then we return the current
3305 article number."
3306   '(progn
3307      (gnus-summary-skip-intangible)
3308      (or (get-text-property (point) 'gnus-number)
3309          (gnus-summary-last-subject))))
3310
3311 (defmacro gnus-summary-article-header (&optional number)
3312   "Return the header of article NUMBER."
3313   `(gnus-data-header (gnus-data-find
3314                       ,(or number '(gnus-summary-article-number)))))
3315
3316 (defmacro gnus-summary-thread-level (&optional number)
3317   "Return the level of thread that starts with article NUMBER."
3318   `(if (and (eq gnus-summary-make-false-root 'dummy)
3319             (get-text-property (point) 'gnus-intangible))
3320        0
3321      (gnus-data-level (gnus-data-find
3322                        ,(or number '(gnus-summary-article-number))))))
3323
3324 (defmacro gnus-summary-article-mark (&optional number)
3325   "Return the mark of article NUMBER."
3326   `(gnus-data-mark (gnus-data-find
3327                     ,(or number '(gnus-summary-article-number)))))
3328
3329 (defmacro gnus-summary-article-pos (&optional number)
3330   "Return the position of the line of article NUMBER."
3331   `(gnus-data-pos (gnus-data-find
3332                    ,(or number '(gnus-summary-article-number)))))
3333
3334 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3335 (defmacro gnus-summary-article-subject (&optional number)
3336   "Return current subject string or nil if nothing."
3337   `(let ((headers
3338           ,(if number
3339                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3340              '(gnus-data-header (assq (gnus-summary-article-number)
3341                                       gnus-newsgroup-data)))))
3342      (and headers
3343           (vectorp headers)
3344           (mail-header-subject headers))))
3345
3346 (defmacro gnus-summary-article-score (&optional number)
3347   "Return current article score."
3348   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3349                   gnus-newsgroup-scored))
3350        gnus-summary-default-score 0))
3351
3352 (defun gnus-summary-article-children (&optional number)
3353   "Return a list of article numbers that are children of article NUMBER."
3354   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3355          (level (gnus-data-level (car data)))
3356          l children)
3357     (while (and (setq data (cdr data))
3358                 (> (setq l (gnus-data-level (car data))) level))
3359       (and (= (1+ level) l)
3360            (push (gnus-data-number (car data))
3361                  children)))
3362     (nreverse children)))
3363
3364 (defun gnus-summary-article-parent (&optional number)
3365   "Return the article number of the parent of article NUMBER."
3366   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3367                                     (gnus-data-list t)))
3368          (level (gnus-data-level (car data))))
3369     (if (zerop level)
3370         ()                              ; This is a root.
3371       ;; We search until we find an article with a level less than
3372       ;; this one.  That function has to be the parent.
3373       (while (and (setq data (cdr data))
3374                   (not (< (gnus-data-level (car data)) level))))
3375       (and data (gnus-data-number (car data))))))
3376
3377 (defun gnus-unread-mark-p (mark)
3378   "Say whether MARK is the unread mark."
3379   (= mark gnus-unread-mark))
3380
3381 (defun gnus-read-mark-p (mark)
3382   "Say whether MARK is one of the marks that mark as read.
3383 This is all marks except unread, ticked, dormant, and expirable."
3384   (not (or (= mark gnus-unread-mark)
3385            (= mark gnus-ticked-mark)
3386            (= mark gnus-spam-mark)
3387            (= mark gnus-dormant-mark)
3388            (= mark gnus-expirable-mark))))
3389
3390 (defmacro gnus-article-mark (number)
3391   "Return the MARK of article NUMBER.
3392 This macro should only be used when computing the mark the \"first\"
3393 time; i.e., when generating the summary lines.  After that,
3394 `gnus-summary-article-mark' should be used to examine the
3395 marks of articles."
3396   `(cond
3397     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3398     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3399     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3400     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3401     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3402     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3403     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3404     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3405            gnus-ancient-mark))))
3406
3407 ;; Saving hidden threads.
3408
3409 (defmacro gnus-save-hidden-threads (&rest forms)
3410   "Save hidden threads, eval FORMS, and restore the hidden threads."
3411   (let ((config (make-symbol "config")))
3412     `(let ((,config (gnus-hidden-threads-configuration)))
3413        (unwind-protect
3414            (save-excursion
3415              ,@forms)
3416          (gnus-restore-hidden-threads-configuration ,config)))))
3417 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3418 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3419
3420 (defun gnus-data-compute-positions ()
3421   "Compute the positions of all articles."
3422   (setq gnus-newsgroup-data-reverse nil)
3423   (let ((data gnus-newsgroup-data))
3424     (save-excursion
3425       (gnus-save-hidden-threads
3426         (gnus-summary-show-all-threads)
3427         (goto-char (point-min))
3428         (while data
3429           (while (get-text-property (point) 'gnus-intangible)
3430             (forward-line 1))
3431           (gnus-data-set-pos (car data) (+ (point) 3))
3432           (setq data (cdr data))
3433           (forward-line 1))))))
3434
3435 (defun gnus-hidden-threads-configuration ()
3436   "Return the current hidden threads configuration."
3437   (save-excursion
3438     (let (config)
3439       (goto-char (point-min))
3440       (while (not (eobp))
3441         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3442           (push (save-excursion (forward-line 0) (point)) config))
3443         (forward-line 1))
3444       config)))
3445
3446 (defun gnus-restore-hidden-threads-configuration (config)
3447   "Restore hidden threads configuration from CONFIG."
3448   (save-excursion
3449     (let (point (inhibit-read-only t))
3450       (while (setq point (pop config))
3451         (goto-char point)
3452         (gnus-summary-hide-thread)))))
3453
3454 ;; Various summary mode internalish functions.
3455
3456 (defun gnus-mouse-pick-article (e)
3457   (interactive "e")
3458   (mouse-set-point e)
3459   (gnus-summary-next-page nil t))
3460
3461 (defun gnus-summary-set-display-table ()
3462   "Change the display table.
3463 Odd characters have a tendency to mess
3464 up nicely formatted displays - we make all possible glyphs
3465 display only a single character."
3466
3467   ;; We start from the standard display table, if any.
3468   (let ((table (or (copy-sequence standard-display-table)
3469                    (make-display-table)))
3470         (i 32))
3471     ;; Nix out all the control chars...
3472     (while (>= (setq i (1- i)) 0)
3473       (gnus-put-display-table i [??] table))
3474    ;; ... but not newline and cr, of course.  (cr is necessary for the
3475     ;; selective display).
3476     (gnus-put-display-table ?\n nil table)
3477     (gnus-put-display-table ?\r nil table)
3478     ;; We keep TAB as well.
3479     (gnus-put-display-table ?\t nil table)
3480     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3481     ;; Emacs 23 (unicode), that are not set already.
3482     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3483                  160
3484                256)))
3485       (while (>= (setq i (1- i)) 127)
3486         ;; Only modify if the entry is nil.
3487         (unless (gnus-get-display-table i table)
3488           (gnus-put-display-table i [??] table))))
3489     (setq buffer-display-table table)))
3490
3491 (defun gnus-summary-set-article-display-arrow (pos)
3492   "Update the overlay arrow to point to line at position POS."
3493   (when gnus-summary-display-arrow
3494     (make-local-variable 'overlay-arrow-position)
3495     (make-local-variable 'overlay-arrow-string)
3496     (save-excursion
3497       (goto-char pos)
3498       (beginning-of-line)
3499       (unless overlay-arrow-position
3500         (setq overlay-arrow-position (make-marker)))
3501       (setq overlay-arrow-string "=>"
3502             overlay-arrow-position (set-marker overlay-arrow-position
3503                                                (point)
3504                                                (current-buffer))))))
3505
3506 (defun gnus-summary-setup-buffer (group)
3507   "Initialize summary buffer.
3508 If the setup was successful, non-nil is returned."
3509   (let ((buffer (gnus-summary-buffer-name group))
3510         (dead-name (concat "*Dead Summary "
3511                            (gnus-group-decoded-name group) "*")))
3512     ;; If a dead summary buffer exists, we kill it.
3513     (when (gnus-buffer-live-p dead-name)
3514       (gnus-kill-buffer dead-name))
3515     (if (get-buffer buffer)
3516         (progn
3517           (set-buffer buffer)
3518           (setq gnus-summary-buffer (current-buffer))
3519           (not gnus-newsgroup-prepared))
3520       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3521       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3522       (gnus-summary-mode group)
3523       (when (gnus-group-quit-config group)
3524         (set (make-local-variable 'gnus-single-article-buffer) nil))
3525       (make-local-variable 'gnus-article-buffer)
3526       (make-local-variable 'gnus-article-current)
3527       (make-local-variable 'gnus-original-article-buffer)
3528       (setq gnus-newsgroup-name group)
3529       ;; Set any local variables in the group parameters.
3530       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3531       t)))
3532
3533 (defun gnus-set-global-variables ()
3534   "Set the global equivalents of the buffer-local variables.
3535 They are set to the latest values they had.  These reflect the summary
3536 buffer that was in action when the last article was fetched."
3537   (when (eq major-mode 'gnus-summary-mode)
3538     (setq gnus-summary-buffer (current-buffer))
3539     (let ((name gnus-newsgroup-name)
3540           (marked gnus-newsgroup-marked)
3541           (spam gnus-newsgroup-spam-marked)
3542           (unread gnus-newsgroup-unreads)
3543           (headers gnus-current-headers)
3544           (data gnus-newsgroup-data)
3545           (summary gnus-summary-buffer)
3546           (article-buffer gnus-article-buffer)
3547           (original gnus-original-article-buffer)
3548           (gac gnus-article-current)
3549           (reffed gnus-reffed-article-number)
3550           (score-file gnus-current-score-file)
3551           (default-charset gnus-newsgroup-charset)
3552           vlist)
3553       (let ((locals gnus-newsgroup-variables))
3554         (while locals
3555           (if (consp (car locals))
3556               (push (eval (caar locals)) vlist)
3557             (push (eval (car locals)) vlist))
3558           (setq locals (cdr locals)))
3559         (setq vlist (nreverse vlist)))
3560       (with-current-buffer gnus-group-buffer
3561         (setq gnus-newsgroup-name name
3562               gnus-newsgroup-marked marked
3563               gnus-newsgroup-spam-marked spam
3564               gnus-newsgroup-unreads unread
3565               gnus-current-headers headers
3566               gnus-newsgroup-data data
3567               gnus-article-current gac
3568               gnus-summary-buffer summary
3569               gnus-article-buffer article-buffer
3570               gnus-original-article-buffer original
3571               gnus-reffed-article-number reffed
3572               gnus-current-score-file score-file
3573               gnus-newsgroup-charset default-charset)
3574         (let ((locals gnus-newsgroup-variables))
3575           (while locals
3576             (if (consp (car locals))
3577                 (set (caar locals) (pop vlist))
3578               (set (car locals) (pop vlist)))
3579             (setq locals (cdr locals))))
3580         ;; The article buffer also has local variables.
3581         (when (gnus-buffer-live-p gnus-article-buffer)
3582           (set-buffer gnus-article-buffer)
3583           (setq gnus-summary-buffer summary))))))
3584
3585 (defun gnus-summary-article-unread-p (article)
3586   "Say whether ARTICLE is unread or not."
3587   (memq article gnus-newsgroup-unreads))
3588
3589 (defun gnus-summary-first-article-p (&optional article)
3590   "Return whether ARTICLE is the first article in the buffer."
3591   (if (not (setq article (or article (gnus-summary-article-number))))
3592       nil
3593     (eq article (caar gnus-newsgroup-data))))
3594
3595 (defun gnus-summary-last-article-p (&optional article)
3596   "Return whether ARTICLE is the last article in the buffer."
3597   (if (not (setq article (or article (gnus-summary-article-number))))
3598       ;; All non-existent numbers are the last article.  :-)
3599       t
3600     (not (cdr (gnus-data-find-list article)))))
3601
3602 (defun gnus-make-thread-indent-array (&optional n)
3603   (when (or n
3604             (progn (setq n 200) nil)
3605             (null gnus-thread-indent-array)
3606             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3607     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3608           gnus-thread-indent-array-level gnus-thread-indent-level)
3609     (while (>= n 0)
3610       (aset gnus-thread-indent-array n
3611             (make-string (* n gnus-thread-indent-level) ? ))
3612       (setq n (1- n)))))
3613
3614 (defun gnus-update-summary-mark-positions ()
3615   "Compute where the summary marks are to go."
3616   (save-excursion
3617     (when (gnus-buffer-exists-p gnus-summary-buffer)
3618       (set-buffer gnus-summary-buffer))
3619     (let ((spec gnus-summary-line-format-spec)
3620           pos)
3621       (save-excursion
3622         (gnus-set-work-buffer)
3623         (let ((gnus-tmp-unread ?Z)
3624               (gnus-replied-mark ?Z)
3625               (gnus-score-below-mark ?Z)
3626               (gnus-score-over-mark ?Z)
3627               (gnus-undownloaded-mark ?Z)
3628               (gnus-summary-line-format-spec spec)
3629               (gnus-newsgroup-downloadable '(0))
3630               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3631               case-fold-search ignores)
3632           ;; Here, all marks are bound to Z.
3633           (gnus-summary-insert-line header
3634                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3635           (goto-char (point-min))
3636           ;; Memorize the positions of the same characters as dummy marks.
3637           (while (re-search-forward "[A-D]" nil t)
3638             (push (point) ignores))
3639           (erase-buffer)
3640           ;; We use A-D as dummy marks in order to know column positions
3641           ;; where marks should be inserted.
3642           (setq gnus-tmp-unread ?A
3643                 gnus-replied-mark ?B
3644                 gnus-score-below-mark ?C
3645                 gnus-score-over-mark ?C
3646                 gnus-undownloaded-mark ?D)
3647           (gnus-summary-insert-line header
3648                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3649           ;; Ignore characters which aren't dummy marks.
3650           (dolist (p ignores)
3651             (delete-region (goto-char (1- p)) p)
3652             (insert ?Z))
3653           (goto-char (point-min))
3654           (setq pos (list (cons 'unread
3655                                 (and (search-forward "A" nil t)
3656                                      (- (point) (point-min) 1)))))
3657           (goto-char (point-min))
3658           (push (cons 'replied (and (search-forward "B" nil t)
3659                                     (- (point) (point-min) 1)))
3660                 pos)
3661           (goto-char (point-min))
3662           (push (cons 'score (and (search-forward "C" nil t)
3663                                   (- (point) (point-min) 1)))
3664                 pos)
3665           (goto-char (point-min))
3666           (push (cons 'download (and (search-forward "D" nil t)
3667                                      (- (point) (point-min) 1)))
3668                 pos)))
3669       (setq gnus-summary-mark-positions pos))))
3670
3671 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3672   "Insert a dummy root in the summary buffer."
3673   (beginning-of-line)
3674   (gnus-add-text-properties
3675    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3676    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3677
3678 (defun gnus-summary-extract-address-component (from)
3679   (or (car (funcall gnus-extract-address-components from))
3680       from))
3681
3682 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3683   (let ((mail-parse-charset gnus-newsgroup-charset)
3684         (ignored-from-addresses (gnus-ignored-from-addresses))
3685         ; Is it really necessary to do this next part for each summary line?
3686         ; Luckily, doesn't seem to slow things down much.
3687         (mail-parse-ignored-charsets
3688          (with-current-buffer gnus-summary-buffer
3689            gnus-newsgroup-ignored-charsets)))
3690     (or
3691      (and ignored-from-addresses
3692           (string-match ignored-from-addresses gnus-tmp-from)
3693           (let ((extra-headers (mail-header-extra header))
3694                 to
3695                 newsgroups)
3696             (cond
3697              ((setq to (cdr (assq 'To extra-headers)))
3698               (concat gnus-summary-to-prefix
3699                       (inline
3700                         (gnus-summary-extract-address-component
3701                          (funcall gnus-decode-encoded-address-function to)))))
3702              ((setq newsgroups
3703                     (or
3704                      (cdr (assq 'Newsgroups extra-headers))
3705                      (and
3706                       (memq 'Newsgroups gnus-extra-headers)
3707                       (eq (car (gnus-find-method-for-group
3708                                 gnus-newsgroup-name)) 'nntp)
3709                       (gnus-group-real-name gnus-newsgroup-name))))
3710               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3711      (gnus-string-mark-left-to-right
3712       (inline
3713        (gnus-summary-extract-address-component gnus-tmp-from))))))
3714
3715 (defun gnus-summary-insert-line (gnus-tmp-header
3716                                  gnus-tmp-level gnus-tmp-current
3717                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3718                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3719                                  &optional gnus-tmp-dummy gnus-tmp-score
3720                                  gnus-tmp-process)
3721   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3722       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3723                                           gnus-tmp-level)))
3724   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3725          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3726          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3727          (gnus-tmp-score-char
3728           (if (or (null gnus-summary-default-score)
3729                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3730                       gnus-summary-zcore-fuzz))
3731               ?                         ;Whitespace
3732             (if (< gnus-tmp-score gnus-summary-default-score)
3733                 gnus-score-below-mark gnus-score-over-mark)))
3734          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3735          (gnus-tmp-replied
3736           (cond (gnus-tmp-process gnus-process-mark)
3737                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3738                  gnus-cached-mark)
3739                 (gnus-tmp-replied gnus-replied-mark)
3740                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3741                  gnus-forwarded-mark)
3742                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3743                  gnus-saved-mark)
3744                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3745                  gnus-unseen-mark)
3746                 (t gnus-no-mark)))
3747          (gnus-tmp-downloaded
3748           (cond (undownloaded
3749                  gnus-undownloaded-mark)
3750                 (gnus-newsgroup-agentized
3751                  gnus-downloaded-mark)
3752                 (t
3753                  gnus-no-mark)))
3754          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3755          (gnus-tmp-name
3756           (cond
3757            ((string-match "<[^>]+> *$" gnus-tmp-from)
3758             (let ((beg (match-beginning 0)))
3759               (or (and (string-match "^\".+\"" gnus-tmp-from)
3760                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3761                   (substring gnus-tmp-from 0 beg))))
3762            ((string-match "(.+)" gnus-tmp-from)
3763             (substring gnus-tmp-from
3764                        (1+ (match-beginning 0)) (1- (match-end 0))))
3765            (t gnus-tmp-from)))
3766          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3767          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3768          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3769          (inhibit-read-only t))
3770     (when (string= gnus-tmp-name "")
3771       (setq gnus-tmp-name gnus-tmp-from))
3772     (unless (numberp gnus-tmp-lines)
3773       (setq gnus-tmp-lines -1))
3774     (if (= gnus-tmp-lines -1)
3775         (setq gnus-tmp-lines "?")
3776       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3777     (condition-case ()
3778         (gnus-put-text-property
3779          (point)
3780          (progn (eval gnus-summary-line-format-spec) (point))
3781          'gnus-number gnus-tmp-number)
3782       (error (gnus-message 5 "Error updating the summary line")))
3783     (when (gnus-visual-p 'summary-highlight 'highlight)
3784       (forward-line -1)
3785       (gnus-summary-highlight-line)
3786       (gnus-run-hooks 'gnus-summary-update-hook)
3787       (forward-line 1))))
3788
3789 (defun gnus-summary-update-line (&optional dont-update)
3790   "Update summary line after change."
3791   (when (and gnus-summary-default-score
3792              (not gnus-summary-inhibit-highlight))
3793     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3794            (article (gnus-summary-article-number))
3795            (score (gnus-summary-article-score article)))
3796       (unless dont-update
3797         (if (and gnus-summary-mark-below
3798                  (< (gnus-summary-article-score)
3799                     gnus-summary-mark-below))
3800             ;; This article has a low score, so we mark it as read.
3801             (when (memq article gnus-newsgroup-unreads)
3802               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3803           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3804             ;; This article was previously marked as read on account
3805             ;; of a low score, but now it has risen, so we mark it as
3806             ;; unread.
3807             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3808         (gnus-summary-update-mark
3809          (if (or (null gnus-summary-default-score)
3810                  (<= (abs (- score gnus-summary-default-score))
3811                      gnus-summary-zcore-fuzz))
3812              ?                          ;Whitespace
3813            (if (< score gnus-summary-default-score)
3814                gnus-score-below-mark gnus-score-over-mark))
3815          'score))
3816       ;; Do visual highlighting.
3817       (when (gnus-visual-p 'summary-highlight 'highlight)
3818         (gnus-summary-highlight-line)
3819         (gnus-run-hooks 'gnus-summary-update-hook)))))
3820
3821 (defvar gnus-tmp-new-adopts nil)
3822
3823 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3824   "Return the number of articles in THREAD.
3825 This may be 0 in some cases -- if none of the articles in
3826 the thread are to be displayed."
3827   (let* ((number
3828          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3829           (cond
3830            ((not (listp thread))
3831             1)
3832            ((and (consp thread) (cdr thread))
3833             (apply
3834              '+ 1 (mapcar
3835                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3836            ((null thread)
3837             1)
3838            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3839             1)
3840            (t 0))))
3841     (when (and level (zerop level) gnus-tmp-new-adopts)
3842       (incf number
3843             (apply '+ (mapcar
3844                        'gnus-summary-number-of-articles-in-thread
3845                        gnus-tmp-new-adopts))))
3846     (if char
3847         (if (> number 1) gnus-not-empty-thread-mark
3848           gnus-empty-thread-mark)
3849       number)))
3850
3851 (defsubst gnus-summary-line-message-size (head)
3852   "Return pretty-printed version of message size.
3853 This function is intended to be used in
3854 `gnus-summary-line-format-alist'."
3855   (let ((c (or (mail-header-chars head) -1)))
3856     (cond ((< c 0) "n/a")               ; chars not available
3857           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3858           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3859           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3860           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3861
3862 (defcustom gnus-user-date-format-alist
3863   '(((gnus-seconds-today) . "Today, %H:%M")
3864     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3865     (604800 . "%A %H:%M")               ; That's one week
3866     ((gnus-seconds-month) . "%A %d")
3867     ((gnus-seconds-year) . "%B %d")
3868     (t . "%b %d %Y"))                   ; This one is used when no other
3869                                         ; does match
3870   "Specifies date format depending on age of article.
3871 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3872 seconds) or a Lisp expression evaluating to a number.  When the age of
3873 the article is less than this number, then use `format-time-string'
3874 with the corresponding FORMAT for displaying the date of the article.
3875 If AGE is not a number or a Lisp expression evaluating to a
3876 non-number, then the corresponding FORMAT is used as a default value.
3877
3878 Note that the list is processed from the beginning, so it should be
3879 sorted by ascending AGE.  Also note that items following the first
3880 non-number AGE will be ignored.
3881
3882 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3883 and `gnus-seconds-year' in the AGE spec.  They return the number of
3884 seconds passed since the start of today, of this month, of this year,
3885 respectively."
3886   :version "24.1"
3887   :group 'gnus-summary-format
3888   :type '(alist :key-type sexp :value-type string))
3889
3890 (defun gnus-user-date (messy-date)
3891   "Format the messy-date according to `gnus-user-date-format-alist'.
3892 Returns \"  ?  \" if there's bad input or if another error occurs.
3893 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3894   (condition-case ()
3895       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3896              (now (gnus-float-time))
3897              ;;If we don't find something suitable we'll use this one
3898              (my-format "%b %d '%y"))
3899         (let* ((difference (- now messy-date))
3900                (templist gnus-user-date-format-alist)
3901                (top (eval (caar templist))))
3902           (while (if (numberp top) (< top difference) (not top))
3903             (progn
3904               (setq templist (cdr templist))
3905               (setq top (eval (caar templist)))))
3906           (if (stringp (cdr (car templist)))
3907               (setq my-format (cdr (car templist)))))
3908         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3909     (error "  ?   ")))
3910
3911 (defun gnus-summary-set-local-parameters (group)
3912   "Go through the local params of GROUP and set all variable specs in that list."
3913   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3914                                         ; really variables.
3915     (dolist (elem (gnus-group-find-parameter group))
3916       (and (consp elem)                 ; Has to be a cons.
3917            (consp (cdr elem))           ; The cdr has to be a list.
3918            (symbolp (car elem))         ; Has to be a symbol in there.
3919            (not (memq (car elem) vars))
3920            (ignore-errors
3921              (push (car elem) vars)
3922              ;; Variables like `gnus-show-threads' that are globally
3923              ;; bound, if used as group parameters, need to get to be
3924              ;; buffer-local, whereas just parameters like `gcc-self',
3925              ;; `timestamp', etc. should not be bound as variables.
3926              (if (boundp (car elem))
3927                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3928                (eval (nth 1 elem))))))))
3929
3930 (defun gnus-summary-read-group (group &optional show-all no-article
3931                                       kill-buffer no-display backward
3932                                       select-articles)
3933   "Start reading news in newsgroup GROUP.
3934 If SHOW-ALL is non-nil, already read articles are also listed.
3935 If NO-ARTICLE is non-nil, no article is selected initially.
3936 If NO-DISPLAY, don't generate the summary buffer contents.
3937 If KILL-BUFFER, it should be a buffer that's killed once the new
3938 summary buffer has been generated.
3939 If BACKWARD, move point to the previous group in the group buffer
3940 If SELECT-ARTICLES, only select those articles from GROUP."
3941   (let (result)
3942     (while (and group
3943                 (null (setq result
3944                             (let ((gnus-auto-select-next nil))
3945                               (or (gnus-summary-read-group-1
3946                                    group show-all no-article
3947                                    kill-buffer no-display
3948                                    select-articles)
3949                                   (setq show-all nil
3950                                         select-articles nil)))))
3951                 (eq gnus-auto-select-next 'quietly))
3952       (set-buffer gnus-group-buffer)
3953       ;; The entry function called above goes to the next
3954       ;; group automatically, so we go two groups back
3955       ;; if we are searching for the previous group.
3956       (when backward
3957         (gnus-group-prev-unread-group 2))
3958       (if (not (equal group (gnus-group-group-name)))
3959           (setq group (gnus-group-group-name))
3960         (setq group nil)))
3961     result))
3962
3963 (defun gnus-summary-read-group-1 (group show-all no-article
3964                                         kill-buffer no-display
3965                                         &optional select-articles)
3966   ;; Killed foreign groups can't be entered.
3967   ;;  (when (and (not (gnus-group-native-p group))
3968   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3969   ;;    (error "Dead non-native groups can't be entered"))
3970   (gnus-message 7 "Retrieving newsgroup: %s..."
3971                 (gnus-group-decoded-name group))
3972   (let* ((new-group (gnus-summary-setup-buffer group))
3973          (quit-config (gnus-group-quit-config group))
3974          (did-select (and new-group (gnus-select-newsgroup
3975                                      group show-all select-articles))))
3976     (cond
3977      ;; This summary buffer exists already, so we just select it.
3978      ((not new-group)
3979       (gnus-set-global-variables)
3980       (when kill-buffer
3981         (gnus-kill-or-deaden-summary kill-buffer))
3982       (gnus-configure-windows 'summary 'force)
3983       (gnus-set-mode-line 'summary)
3984       (gnus-summary-position-point)
3985       (message "")
3986       t)
3987      ;; We couldn't select this group.
3988      ((null did-select)
3989       (when (and (eq major-mode 'gnus-summary-mode)
3990                  (not (equal (current-buffer) kill-buffer)))
3991         (kill-buffer (current-buffer))
3992         (if (not quit-config)
3993             (progn
3994               ;; Update the info -- marks might need to be removed,
3995               ;; for instance.
3996               (gnus-summary-update-info)
3997               (set-buffer gnus-group-buffer)
3998               (gnus-group-jump-to-group group)
3999               (gnus-group-next-unread-group 1))
4000           (gnus-handle-ephemeral-exit quit-config)))
4001       (if (null (gnus-list-of-unread-articles group))
4002           (gnus-message 3 "Group %s contains no messages" group)
4003         (gnus-message 3 "Can't select group"))
4004       nil)
4005      ;; The user did a `C-g' while prompting for number of articles,
4006      ;; so we exit this group.
4007      ((eq did-select 'quit)
4008       (and (eq major-mode 'gnus-summary-mode)
4009            (not (equal (current-buffer) kill-buffer))
4010            (kill-buffer (current-buffer)))
4011       (when kill-buffer
4012         (gnus-kill-or-deaden-summary kill-buffer))
4013       (if (not quit-config)
4014           (progn
4015             (set-buffer gnus-group-buffer)
4016             (gnus-group-jump-to-group group)
4017             (gnus-configure-windows 'group 'force))
4018         (gnus-handle-ephemeral-exit quit-config))
4019       ;; Finally signal the quit.
4020       (signal 'quit nil))
4021      ;; The group was successfully selected.
4022      (t
4023       (gnus-set-global-variables)
4024       ;; Save the active value in effect when the group was entered.
4025       (setq gnus-newsgroup-active
4026             (gnus-copy-sequence
4027              (gnus-active gnus-newsgroup-name)))
4028       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4029       ;; You can change the summary buffer in some way with this hook.
4030       (gnus-run-hooks 'gnus-select-group-hook)
4031       (when (memq 'summary (gnus-update-format-specifications
4032                             nil 'summary 'summary-mode 'summary-dummy))
4033         ;; The format specification for the summary line was updated,
4034         ;; so we need to update the mark positions as well.
4035         (gnus-update-summary-mark-positions))
4036       ;; Do score processing.
4037       (when gnus-use-scoring
4038         (gnus-possibly-score-headers))
4039       ;; Check whether to fill in the gaps in the threads.
4040       (when gnus-build-sparse-threads
4041         (gnus-build-sparse-threads))
4042       ;; Find the initial limit.
4043       (if show-all
4044           (let ((gnus-newsgroup-dormant nil))
4045             (gnus-summary-initial-limit show-all))
4046         (gnus-summary-initial-limit show-all))
4047       ;; Generate the summary buffer.
4048       (unless no-display
4049         (gnus-summary-prepare))
4050       (when gnus-use-trees
4051         (gnus-tree-open group)
4052         (setq gnus-summary-highlight-line-function
4053               'gnus-tree-highlight-article))
4054       ;; If the summary buffer is empty, but there are some low-scored
4055       ;; articles or some excluded dormants, we include these in the
4056       ;; buffer.
4057       (when (and (zerop (buffer-size))
4058                  (not no-display))
4059         (cond (gnus-newsgroup-dormant
4060                (gnus-summary-limit-include-dormant))
4061               ((and gnus-newsgroup-scored show-all)
4062                (gnus-summary-limit-include-expunged t))))
4063       ;; Function `gnus-apply-kill-file' must be called in this hook.
4064       (gnus-run-hooks 'gnus-apply-kill-hook)
4065       (if (and (zerop (buffer-size))
4066                (not no-display))
4067           (progn
4068             ;; This newsgroup is empty.
4069             (gnus-summary-catchup-and-exit nil t)
4070             (gnus-message 6 "No unread news")
4071             (when kill-buffer
4072               (gnus-kill-or-deaden-summary kill-buffer))
4073             ;; Return nil from this function.
4074             nil)
4075         ;; Hide conversation thread subtrees.  We cannot do this in
4076         ;; gnus-summary-prepare-hook since kill processing may not
4077         ;; work with hidden articles.
4078         (gnus-summary-maybe-hide-threads)
4079         (gnus-configure-windows 'summary)
4080         (when kill-buffer
4081           (gnus-kill-or-deaden-summary kill-buffer))
4082         (gnus-summary-auto-select-subject)
4083         ;; Show first unread article if requested.
4084         (if (and (not no-article)
4085                  (not no-display)
4086                  gnus-newsgroup-unreads
4087                  gnus-auto-select-first)
4088             (progn
4089               (let ((art (gnus-summary-article-number)))
4090                 (unless (and (not gnus-plugged)
4091                              (or (memq art gnus-newsgroup-undownloaded)
4092                                  (memq art gnus-newsgroup-downloadable)))
4093                   (gnus-summary-goto-article art))))
4094           ;; Don't select any articles.
4095           (gnus-summary-position-point)
4096           (gnus-configure-windows 'summary 'force)
4097           (gnus-set-mode-line 'summary))
4098         (when (and gnus-auto-center-group
4099                    (get-buffer-window gnus-group-buffer t))
4100           ;; Gotta use windows, because recenter does weird stuff if
4101           ;; the current buffer ain't the displayed window.
4102           (let ((owin (selected-window)))
4103             (select-window (get-buffer-window gnus-group-buffer t))
4104             (when (gnus-group-goto-group group)
4105               (recenter))
4106             (select-window owin)))
4107         ;; Mark this buffer as "prepared".
4108         (setq gnus-newsgroup-prepared t)
4109         (gnus-run-hooks 'gnus-summary-prepared-hook)
4110         (unless (gnus-ephemeral-group-p group)
4111           (gnus-group-update-group group nil t))
4112         t)))))
4113
4114 (defun gnus-summary-auto-select-subject ()
4115   "Select the subject line on initial group entry."
4116   (goto-char (point-min))
4117   (cond
4118    ((eq gnus-auto-select-subject 'best)
4119     (gnus-summary-best-unread-subject))
4120    ((eq gnus-auto-select-subject 'unread)
4121     (gnus-summary-first-unread-subject))
4122    ((eq gnus-auto-select-subject 'unseen)
4123     (gnus-summary-first-unseen-subject))
4124    ((eq gnus-auto-select-subject 'unseen-or-unread)
4125     (gnus-summary-first-unseen-or-unread-subject))
4126    ((eq gnus-auto-select-subject 'first)
4127     ;; Do nothing.
4128     )
4129    ((functionp gnus-auto-select-subject)
4130     (funcall gnus-auto-select-subject))))
4131
4132 (defun gnus-summary-prepare ()
4133   "Generate the summary buffer."
4134   (interactive)
4135   (let ((inhibit-read-only t))
4136     (erase-buffer)
4137     (setq gnus-newsgroup-data nil
4138           gnus-newsgroup-data-reverse nil)
4139     (gnus-run-hooks 'gnus-summary-generate-hook)
4140     ;; Generate the buffer, either with threads or without.
4141     (when gnus-newsgroup-headers
4142       (gnus-summary-prepare-threads
4143        (if gnus-show-threads
4144            (gnus-sort-gathered-threads
4145             (funcall gnus-summary-thread-gathering-function
4146                      (gnus-sort-threads
4147                       (gnus-cut-threads (gnus-make-threads)))))
4148          ;; Unthreaded display.
4149          (gnus-sort-articles gnus-newsgroup-headers))))
4150     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4151     ;; Call hooks for modifying summary buffer.
4152     (goto-char (point-min))
4153     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4154
4155 (defsubst gnus-general-simplify-subject (subject)
4156   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4157   (setq subject
4158         (cond
4159          ;; Truncate the subject.
4160          (gnus-simplify-subject-functions
4161           (gnus-map-function gnus-simplify-subject-functions subject))
4162          ((numberp gnus-summary-gather-subject-limit)
4163           (setq subject (gnus-simplify-subject-re subject))
4164           (if (> (length subject) gnus-summary-gather-subject-limit)
4165               (substring subject 0 gnus-summary-gather-subject-limit)
4166             subject))
4167          ;; Fuzzily simplify it.
4168          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4169           (gnus-simplify-subject-fuzzy subject))
4170          ;; Just remove the leading "Re:".
4171          (t
4172           (gnus-simplify-subject-re subject))))
4173
4174   (if (and gnus-summary-gather-exclude-subject
4175            (string-match gnus-summary-gather-exclude-subject subject))
4176       nil                         ; This article shouldn't be gathered
4177     subject))
4178
4179 (defun gnus-summary-simplify-subject-query ()
4180   "Query where the respool algorithm would put this article."
4181   (interactive)
4182   (gnus-summary-select-article)
4183   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4184
4185 (defun gnus-gather-threads-by-subject (threads)
4186   "Gather threads by looking at Subject headers."
4187   (if (not gnus-summary-make-false-root)
4188       threads
4189     (let ((hashtb (gnus-make-hashtable 1024))
4190           (prev threads)
4191           (result threads)
4192           subject hthread whole-subject)
4193       (while threads
4194         (setq subject (gnus-general-simplify-subject
4195                        (setq whole-subject (mail-header-subject
4196                                             (caar threads)))))
4197         (when subject
4198           (if (setq hthread (gnus-gethash subject hashtb))
4199               (progn
4200                 ;; We enter a dummy root into the thread, if we
4201                 ;; haven't done that already.
4202                 (unless (stringp (caar hthread))
4203                   (setcar hthread (list whole-subject (car hthread))))
4204                 ;; We add this new gathered thread to this gathered
4205                 ;; thread.
4206                 (setcdr (car hthread)
4207                         (nconc (cdar hthread) (list (car threads))))
4208                 ;; Remove it from the list of threads.
4209                 (setcdr prev (cdr threads))
4210                 (setq threads prev))
4211             ;; Enter this thread into the hash table.
4212             (gnus-sethash subject
4213                           (if gnus-summary-make-false-root-always
4214                               (progn
4215                                 ;; If you want a dummy root above all
4216                                 ;; threads...
4217                                 (setcar threads (list whole-subject
4218                                                       (car threads)))
4219                                 threads)
4220                             threads)
4221                           hashtb)))
4222         (setq prev threads)
4223         (setq threads (cdr threads)))
4224       result)))
4225
4226 (defun gnus-gather-threads-by-references (threads)
4227   "Gather threads by looking at References headers."
4228   (let ((idhashtb (gnus-make-hashtable 1024))
4229         (thhashtb (gnus-make-hashtable 1024))
4230         (prev threads)
4231         (result threads)
4232         ids references id gthread gid entered ref)
4233     (while threads
4234       (when (setq references (mail-header-references (caar threads)))
4235         (setq id (mail-header-id (caar threads))
4236               ids (inline (gnus-split-references references))
4237               entered nil)
4238         (while (setq ref (pop ids))
4239           (setq ids (delete ref ids))
4240           (if (not (setq gid (gnus-gethash ref idhashtb)))
4241               (progn
4242                 (gnus-sethash ref id idhashtb)
4243                 (gnus-sethash id threads thhashtb))
4244             (setq gthread (gnus-gethash gid thhashtb))
4245             (unless entered
4246               ;; We enter a dummy root into the thread, if we
4247               ;; haven't done that already.
4248               (unless (stringp (caar gthread))
4249                 (setcar gthread (list (mail-header-subject (caar gthread))
4250                                       (car gthread))))
4251               ;; We add this new gathered thread to this gathered
4252               ;; thread.
4253               (setcdr (car gthread)
4254                       (nconc (cdar gthread) (list (car threads)))))
4255             ;; Add it into the thread hash table.
4256             (gnus-sethash id gthread thhashtb)
4257             (setq entered t)
4258             ;; Remove it from the list of threads.
4259             (setcdr prev (cdr threads))
4260             (setq threads prev))))
4261       (setq prev threads)
4262       (setq threads (cdr threads)))
4263     result))
4264
4265 (defun gnus-sort-gathered-threads (threads)
4266   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4267   (let ((result threads))
4268     (while threads
4269       (when (stringp (caar threads))
4270         (setcdr (car threads)
4271                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4272       (setq threads (cdr threads)))
4273     result))
4274
4275 (defun gnus-thread-loop-p (root thread)
4276   "Say whether ROOT is in THREAD."
4277   (let ((stack (list thread))
4278         (infloop 0)
4279         th)
4280     (while (setq thread (pop stack))
4281       (setq th (cdr thread))
4282       (while (and th
4283                   (not (eq (caar th) root)))
4284         (pop th))
4285       (if th
4286           ;; We have found a loop.
4287           (let (ref-dep)
4288             (setcdr thread (delq (car th) (cdr thread)))
4289             (if (boundp (setq ref-dep (intern "none"
4290                                               gnus-newsgroup-dependencies)))
4291                 (setcdr (symbol-value ref-dep)
4292                         (nconc (cdr (symbol-value ref-dep))
4293                                (list (car th))))
4294               (set ref-dep (list nil (car th))))
4295             (setq infloop 1
4296                   stack nil))
4297         ;; Push all the subthreads onto the stack.
4298         (push (cdr thread) stack)))
4299     infloop))
4300
4301 (defun gnus-make-threads ()
4302   "Go through the dependency hashtb and find the roots.  Return all threads."
4303   (let (threads)
4304     (while (catch 'infloop
4305              (mapatoms
4306               (lambda (refs)
4307                 ;; Deal with self-referencing References loops.
4308                 (when (and (car (symbol-value refs))
4309                            (not (zerop
4310                                  (apply
4311                                   '+
4312                                   (mapcar
4313                                    (lambda (thread)
4314                                      (gnus-thread-loop-p
4315                                       (car (symbol-value refs)) thread))
4316                                    (cdr (symbol-value refs)))))))
4317                   (setq threads nil)
4318                   (throw 'infloop t))
4319                 (unless (car (symbol-value refs))
4320                   ;; These threads do not refer back to any other
4321                   ;; articles, so they're roots.
4322                   (setq threads (append (cdr (symbol-value refs)) threads))))
4323               gnus-newsgroup-dependencies)))
4324     threads))
4325
4326 ;; Build the thread tree.
4327 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4328   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4329
4330 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4331 if it was already present.
4332
4333 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4334 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4335 Message-IDs will be renamed to a unique Message-ID before being
4336 entered.
4337
4338 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4339   (let* ((id (mail-header-id header))
4340          (id-dep (and id (intern id dependencies)))
4341          parent-id ref ref-dep ref-header replaced)
4342     ;; Enter this `header' in the `dependencies' table.
4343     (cond
4344      ((not id-dep)
4345       (setq header nil))
4346      ;; The first two cases do the normal part: enter a new `header'
4347      ;; in the `dependencies' table.
4348      ((not (boundp id-dep))
4349       (set id-dep (list header)))
4350      ((null (car (symbol-value id-dep)))
4351       (setcar (symbol-value id-dep) header))
4352
4353      ;; From here the `header' was already present in the
4354      ;; `dependencies' table.
4355      (force-new
4356       ;; Overrides an existing entry;
4357       ;; just set the header part of the entry.
4358       (setcar (symbol-value id-dep) header)
4359       (setq replaced t))
4360
4361      ;; Renames the existing `header' to a unique Message-ID.
4362      ((not gnus-summary-ignore-duplicates)
4363       ;; An article with this Message-ID has already been seen.
4364       ;; We rename the Message-ID.
4365       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4366            (list header))
4367       (mail-header-set-id header id))
4368
4369      ;; The last case ignores an existing entry, except it adds any
4370      ;; additional Xrefs (in case the two articles came from different
4371      ;; servers.
4372      ;; Also sets `header' to `nil' meaning that the `dependencies'
4373      ;; table was *not* modified.
4374      (t
4375       (mail-header-set-xref
4376        (car (symbol-value id-dep))
4377        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4378                    "")
4379                (or (mail-header-xref header) "")))
4380       (setq header nil)))
4381
4382     (when (and header (not replaced))
4383       ;; First check that we are not creating a References loop.
4384       (setq parent-id (gnus-parent-id (mail-header-references header)))
4385       (setq ref parent-id)
4386       (while (and ref
4387                   (setq ref-dep (intern-soft ref dependencies))
4388                   (boundp ref-dep)
4389                   (setq ref-header (car (symbol-value ref-dep))))
4390         (if (string= id ref)
4391             ;; Yuk!  This is a reference loop.  Make the article be a
4392             ;; root article.
4393             (progn
4394               (mail-header-set-references (car (symbol-value id-dep)) "none")
4395               (setq ref nil)
4396               (setq parent-id nil))
4397           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4398       (setq ref-dep (intern (or parent-id "none") dependencies))
4399       (if (boundp ref-dep)
4400           (setcdr (symbol-value ref-dep)
4401                   (nconc (cdr (symbol-value ref-dep))
4402                          (list (symbol-value id-dep))))
4403         (set ref-dep (list nil (symbol-value id-dep)))))
4404     header))
4405
4406 (defun gnus-extract-message-id-from-in-reply-to (string)
4407   (if (string-match "<[^>]+>" string)
4408       (substring string (match-beginning 0) (match-end 0))
4409     nil))
4410
4411 (defun gnus-build-sparse-threads ()
4412   (let ((headers gnus-newsgroup-headers)
4413         (mail-parse-charset gnus-newsgroup-charset)
4414         (gnus-summary-ignore-duplicates t)
4415         header references generation relations
4416         subject child end new-child date)
4417     ;; First we create an alist of generations/relations, where
4418     ;; generations is how much we trust the relation, and the relation
4419     ;; is parent/child.
4420     (gnus-message 7 "Making sparse threads...")
4421     (save-excursion
4422       (nnheader-set-temp-buffer " *gnus sparse threads*")
4423       (while (setq header (pop headers))
4424         (when (and (setq references (mail-header-references header))
4425                    (not (string= references "")))
4426           (insert references)
4427           (setq child (mail-header-id header)
4428                 subject (mail-header-subject header)
4429                 date (mail-header-date header)
4430                 generation 0)
4431           (while (search-backward ">" nil t)
4432             (setq end (1+ (point)))
4433             (when (search-backward "<" nil t)
4434               (setq new-child (buffer-substring (point) end))
4435               (push (list (incf generation)
4436                           child (setq child new-child)
4437                           subject date)
4438                     relations)))
4439           (when child
4440             (push (list (1+ generation) child nil subject) relations))
4441           (erase-buffer)))
4442       (kill-buffer (current-buffer)))
4443     ;; Sort over trustworthiness.
4444     (dolist (relation (sort relations 'car-less-than-car))
4445       (when (gnus-dependencies-add-header
4446              (make-full-mail-header
4447               gnus-reffed-article-number
4448               (nth 3 relation) "" (or (nth 4 relation) "")
4449               (nth 1 relation)
4450               (or (nth 2 relation) "") 0 0 "")
4451              gnus-newsgroup-dependencies nil)
4452         (push gnus-reffed-article-number gnus-newsgroup-limit)
4453         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4454         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4455               gnus-newsgroup-reads)
4456         (decf gnus-reffed-article-number)))
4457     (gnus-message 7 "Making sparse threads...done")))
4458
4459 (defun gnus-build-old-threads ()
4460   ;; Look at all the articles that refer back to old articles, and
4461   ;; fetch the headers for the articles that aren't there.  This will
4462   ;; build complete threads - if the roots haven't been expired by the
4463   ;; server, that is.
4464   (let ((mail-parse-charset gnus-newsgroup-charset)
4465         id heads)
4466     (mapatoms
4467      (lambda (refs)
4468        (when (not (car (symbol-value refs)))
4469          (setq heads (cdr (symbol-value refs)))
4470          (while heads
4471            (if (memq (mail-header-number (caar heads))
4472                      gnus-newsgroup-dormant)
4473                (setq heads (cdr heads))
4474              (setq id (symbol-name refs))
4475              (while (and (setq id (gnus-build-get-header id))
4476                          (not (car (gnus-id-to-thread id)))))
4477              (setq heads nil)))))
4478      gnus-newsgroup-dependencies)))
4479
4480 (defsubst gnus-remove-odd-characters (string)
4481   "Translate STRING into something that doesn't contain weird characters."
4482   (mm-subst-char-in-string
4483    ?\r ?\-
4484    (mm-subst-char-in-string ?\n ?\- string t) t))
4485
4486 ;; This function has to be called with point after the article number
4487 ;; on the beginning of the line.
4488 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4489   (let ((eol (point-at-eol))
4490         (buffer (current-buffer))
4491         header references in-reply-to)
4492
4493     ;; overview: [num subject from date id refs chars lines misc]
4494     (unwind-protect
4495         (let (x)
4496           (narrow-to-region (point) eol)
4497           (unless (eobp)
4498             (forward-char))
4499
4500           (setq header
4501                 (make-full-mail-header
4502                  number                 ; number
4503                  (condition-case ()     ; subject
4504                      (gnus-remove-odd-characters
4505                       (funcall gnus-decode-encoded-word-function
4506                                (setq x (nnheader-nov-field))))
4507                    (error x))
4508                  (condition-case ()     ; from
4509                      (gnus-remove-odd-characters
4510                       (funcall gnus-decode-encoded-address-function
4511                                (setq x (nnheader-nov-field))))
4512                    (error x))
4513                  (nnheader-nov-field)   ; date
4514                  (nnheader-nov-read-message-id number)  ; id
4515                  (setq references (nnheader-nov-field)) ; refs
4516                  (nnheader-nov-read-integer) ; chars
4517                  (nnheader-nov-read-integer) ; lines
4518                  (unless (eobp)
4519                    (if (looking-at "Xref: ")
4520                        (goto-char (match-end 0)))
4521                    (nnheader-nov-field)) ; Xref
4522                  (nnheader-nov-parse-extra)))) ; extra
4523
4524       (widen))
4525
4526     (when (and (string= references "")
4527                (setq in-reply-to (mail-header-extra header))
4528                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4529       (mail-header-set-references
4530        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4531
4532     (when gnus-alter-header-function
4533       (funcall gnus-alter-header-function header))
4534     (gnus-dependencies-add-header header dependencies force-new)))
4535
4536 (defun gnus-build-get-header (id)
4537   "Look through the buffer of NOV lines and find the header to ID.
4538 Enter this line into the dependencies hash table, and return
4539 the id of the parent article (if any)."
4540   (let ((deps gnus-newsgroup-dependencies)
4541         found header)
4542     (prog1
4543         (with-current-buffer nntp-server-buffer
4544           (let ((case-fold-search nil))
4545             (goto-char (point-min))
4546             (while (and (not found)
4547                         (search-forward id nil t))
4548               (beginning-of-line)
4549               (setq found (looking-at
4550                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4551                                    (regexp-quote id))))
4552               (or found (beginning-of-line 2)))
4553             (when found
4554               (beginning-of-line)
4555               (and
4556                (setq header (gnus-nov-parse-line
4557                              (read (current-buffer)) deps))
4558                (gnus-parent-id (mail-header-references header))))))
4559       (when header
4560         (let ((number (mail-header-number header)))
4561           (push number gnus-newsgroup-limit)
4562           (push header gnus-newsgroup-headers)
4563           (if (memq number gnus-newsgroup-unselected)
4564               (progn
4565                 (setq gnus-newsgroup-unreads
4566                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4567                                                number))
4568                 (setq gnus-newsgroup-unselected
4569                       (delq number gnus-newsgroup-unselected)))
4570             (push number gnus-newsgroup-ancient)))))))
4571
4572 (defun gnus-build-all-threads ()
4573   "Read all the headers."
4574   (let ((gnus-summary-ignore-duplicates t)
4575         (mail-parse-charset gnus-newsgroup-charset)
4576         (dependencies gnus-newsgroup-dependencies)
4577         header article)
4578     (with-current-buffer nntp-server-buffer
4579       (let ((case-fold-search nil))
4580         (goto-char (point-min))
4581         (while (not (eobp))
4582           (ignore-errors
4583             (setq article (read (current-buffer))
4584                   header (gnus-nov-parse-line article dependencies t)))
4585           (when header
4586             (with-current-buffer gnus-summary-buffer
4587               (push header gnus-newsgroup-headers)
4588               (if (memq (setq article (mail-header-number header))
4589                         gnus-newsgroup-unselected)
4590                   (progn
4591                     (setq gnus-newsgroup-unreads
4592                           (gnus-add-to-sorted-list
4593                            gnus-newsgroup-unreads article))
4594                     (setq gnus-newsgroup-unselected
4595                           (delq article gnus-newsgroup-unselected)))
4596                 (push article gnus-newsgroup-ancient)))
4597             (forward-line 1)))))))
4598
4599 (defun gnus-summary-update-article-line (article header)
4600   "Update the line for ARTICLE using HEADER."
4601   (let* ((id (mail-header-id header))
4602          (thread (gnus-id-to-thread id)))
4603     (unless thread
4604       (error "Article in no thread"))
4605     ;; Update the thread.
4606     (setcar thread header)
4607     (gnus-summary-goto-subject article)
4608     (let* ((datal (gnus-data-find-list article))
4609            (data (car datal))
4610            (inhibit-read-only t)
4611            (level (gnus-summary-thread-level)))
4612       (gnus-delete-line)
4613       (let ((inserted (- (point)
4614                          (progn
4615                            (gnus-summary-insert-line
4616                             header level nil
4617                             (memq article gnus-newsgroup-undownloaded)
4618                             (gnus-article-mark article)
4619                             (memq article gnus-newsgroup-replied)
4620                             (memq article gnus-newsgroup-expirable)
4621                             ;; Only insert the Subject string when it's different
4622                             ;; from the previous Subject string.
4623                             (if (and
4624                                  gnus-show-threads
4625                                  (gnus-subject-equal
4626                                   (condition-case ()
4627                                       (mail-header-subject
4628                                        (gnus-data-header
4629                                         (cadr
4630                                          (gnus-data-find-list
4631                                           article
4632                                           (gnus-data-list t)))))
4633                                     ;; Error on the side of excessive subjects.
4634                                     (error ""))
4635                                   (mail-header-subject header)))
4636                                 ""
4637                               (mail-header-subject header))
4638                             nil (cdr (assq article gnus-newsgroup-scored))
4639                             (memq article gnus-newsgroup-processable))
4640                            (point)))))
4641         (when (cdr datal)
4642           (gnus-data-update-list
4643            (cdr datal)
4644            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4645
4646 (defun gnus-summary-update-article (article &optional iheader)
4647   "Update ARTICLE in the summary buffer."
4648   (set-buffer gnus-summary-buffer)
4649   (let* ((header (gnus-summary-article-header article))
4650          (id (mail-header-id header))
4651          (data (gnus-data-find article))
4652          (thread (gnus-id-to-thread id))
4653          (references (mail-header-references header))
4654          (parent
4655           (gnus-id-to-thread
4656            (or (gnus-parent-id
4657                 (when (and references
4658                            (not (equal "" references)))
4659                   references))
4660                "none")))
4661          (inhibit-read-only t)
4662          (old (car thread)))
4663     (when thread
4664       (unless iheader
4665         (setcar thread nil)
4666         (when parent
4667           (delq thread parent)))
4668       (if (gnus-summary-insert-subject id header)
4669           ;; Set the (possibly) new article number in the data structure.
4670           (gnus-data-set-number data (gnus-id-to-article id))
4671         (setcar thread old)
4672         nil))))
4673
4674 (defun gnus-rebuild-thread (id &optional line)
4675   "Rebuild the thread containing ID.
4676 If LINE, insert the rebuilt thread starting on line LINE."
4677   (let ((inhibit-read-only t)
4678         old-pos current thread data)
4679     (if (not gnus-show-threads)
4680         (setq thread (list (car (gnus-id-to-thread id))))
4681       ;; Get the thread this article is part of.
4682       (setq thread (gnus-remove-thread id)))
4683     (setq old-pos (point-at-bol))
4684     (setq current (save-excursion
4685                     (and (re-search-backward "[\r\n]" nil t)
4686                          (gnus-summary-article-number))))
4687     ;; If this is a gathered thread, we have to go some re-gathering.
4688     (when (stringp (car thread))
4689       (let ((subject (car thread))
4690             roots thr)
4691         (setq thread (cdr thread))
4692         (while thread
4693           (unless (memq (setq thr (gnus-id-to-thread
4694                                    (gnus-root-id
4695                                     (mail-header-id (caar thread)))))
4696                         roots)
4697             (push thr roots))
4698           (setq thread (cdr thread)))
4699         ;; We now have all (unique) roots.
4700         (if (= (length roots) 1)
4701             ;; All the loose roots are now one solid root.
4702             (setq thread (car roots))
4703           (setq thread (cons subject (gnus-sort-threads roots))))))
4704     (let (threads)
4705       ;; We then insert this thread into the summary buffer.
4706       (when line
4707         (goto-char (point-min))
4708         (forward-line (1- line)))
4709       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4710         (if gnus-show-threads
4711             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4712           (gnus-summary-prepare-unthreaded thread))
4713         (setq data (nreverse gnus-newsgroup-data))
4714         (setq threads gnus-newsgroup-threads))
4715       ;; We splice the new data into the data structure.
4716       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4717       ;;!!! then we want to insert at the beginning of the buffer.
4718       ;;!!! That happens to be true with Gnus now, but that may
4719       ;;!!! change in the future.  Perhaps.
4720       (gnus-data-enter-list
4721        (if line nil current) data (- (point) old-pos))
4722       (setq gnus-newsgroup-threads
4723             (nconc threads gnus-newsgroup-threads))
4724       (gnus-data-compute-positions))))
4725
4726 (defun gnus-number-to-header (number)
4727   "Return the header for article NUMBER."
4728   (let ((headers gnus-newsgroup-headers))
4729     (while (and headers
4730                 (not (= number (mail-header-number (car headers)))))
4731       (pop headers))
4732     (when headers
4733       (car headers))))
4734
4735 (defun gnus-parent-headers (in-headers &optional generation)
4736   "Return the headers of the GENERATIONth parent of HEADERS."
4737   (unless generation
4738     (setq generation 1))
4739   (let ((parent t)
4740         (headers in-headers)
4741         references)
4742     (while (and parent
4743                 (not (zerop generation))
4744                 (setq references (mail-header-references headers)))
4745       (setq headers (if (and references
4746                              (setq parent (gnus-parent-id references)))
4747                         (car (gnus-id-to-thread parent))
4748                       nil))
4749       (decf generation))
4750     (and (not (eq headers in-headers))
4751          headers)))
4752
4753 (defun gnus-id-to-thread (id)
4754   "Return the (sub-)thread where ID appears."
4755   (gnus-gethash id gnus-newsgroup-dependencies))
4756
4757 (defun gnus-id-to-article (id)
4758   "Return the article number of ID."
4759   (let ((thread (gnus-id-to-thread id)))
4760     (when (and thread
4761                (car thread))
4762       (mail-header-number (car thread)))))
4763
4764 (defun gnus-id-to-header (id)
4765   "Return the article headers of ID."
4766   (car (gnus-id-to-thread id)))
4767
4768 (defun gnus-article-displayed-root-p (article)
4769   "Say whether ARTICLE is a root(ish) article."
4770   (let ((level (gnus-summary-thread-level article))
4771         (refs (mail-header-references  (gnus-summary-article-header article)))
4772         particle)
4773     (cond
4774      ((null level) nil)
4775      ((zerop level) t)
4776      ((null refs) t)
4777      ((null (gnus-parent-id refs)) t)
4778      ((and (= 1 level)
4779            (null (setq particle (gnus-id-to-article
4780                                  (gnus-parent-id refs))))
4781            (null (gnus-summary-thread-level particle)))))))
4782
4783 (defun gnus-root-id (id)
4784   "Return the id of the root of the thread where ID appears."
4785   (let (last-id prev)
4786     (while (and id (setq prev (car (gnus-id-to-thread id))))
4787       (setq last-id id
4788             id (gnus-parent-id (mail-header-references prev))))
4789     last-id))
4790
4791 (defun gnus-articles-in-thread (thread)
4792   "Return the list of articles in THREAD."
4793   (cons (mail-header-number (car thread))
4794         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4795
4796 (defun gnus-remove-thread (id &optional dont-remove)
4797   "Remove the thread that has ID in it."
4798   (let (headers thread last-id)
4799     ;; First go up in this thread until we find the root.
4800     (setq last-id (gnus-root-id id)
4801           headers (message-flatten-list (gnus-id-to-thread last-id)))
4802     ;; We have now found the real root of this thread.  It might have
4803     ;; been gathered into some loose thread, so we have to search
4804     ;; through the threads to find the thread we wanted.
4805     (let ((threads gnus-newsgroup-threads)
4806           sub)
4807       (while threads
4808         (setq sub (car threads))
4809         (if (stringp (car sub))
4810             ;; This is a gathered thread, so we look at the roots
4811             ;; below it to find whether this article is in this
4812             ;; gathered root.
4813             (progn
4814               (setq sub (cdr sub))
4815               (while sub
4816                 (when (member (caar sub) headers)
4817                   (setq thread (car threads)
4818                         threads nil
4819                         sub nil))
4820                 (setq sub (cdr sub))))
4821           ;; It's an ordinary thread, so we check it.
4822           (when (eq (car sub) (car headers))
4823             (setq thread sub
4824                   threads nil)))
4825         (setq threads (cdr threads)))
4826       ;; If this article is in no thread, then it's a root.
4827       (if thread
4828           (unless dont-remove
4829             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4830         (setq thread (gnus-id-to-thread last-id)))
4831       (when thread
4832         (prog1
4833             thread                      ; We return this thread.
4834           (unless dont-remove
4835             (if (stringp (car thread))
4836                 (progn
4837                   ;; If we use dummy roots, then we have to remove the
4838                   ;; dummy root as well.
4839                   (when (eq gnus-summary-make-false-root 'dummy)
4840                     ;; We go to the dummy root by going to
4841                     ;; the first sub-"thread", and then one line up.
4842                     (gnus-summary-goto-article
4843                      (mail-header-number (caadr thread)))
4844                     (forward-line -1)
4845                     (gnus-delete-line)
4846                     (gnus-data-compute-positions))
4847                   (setq thread (cdr thread))
4848                   (while thread
4849                     (gnus-remove-thread-1 (car thread))
4850                     (setq thread (cdr thread))))
4851               (gnus-remove-thread-1 thread))))))))
4852
4853 (defun gnus-remove-thread-1 (thread)
4854   "Remove the thread THREAD recursively."
4855   (let ((number (mail-header-number (pop thread)))
4856         d)
4857     (setq thread (reverse thread))
4858     (while thread
4859       (gnus-remove-thread-1 (pop thread)))
4860     (when (setq d (gnus-data-find number))
4861       (goto-char (gnus-data-pos d))
4862       (gnus-summary-show-thread)
4863       (gnus-data-remove
4864        number
4865        (- (point-at-bol)
4866           (prog1
4867               (1+ (point-at-eol))
4868             (gnus-delete-line)))))))
4869
4870 (defun gnus-sort-threads-recursive (threads func)
4871   (sort (mapcar (lambda (thread)
4872                   (cons (car thread)
4873                         (and (cdr thread)
4874                              (gnus-sort-threads-recursive (cdr thread) func))))
4875                 threads) func))
4876
4877 (defun gnus-sort-threads-loop (threads func)
4878   (let* ((superthread (cons nil threads))
4879          (stack (list (cons superthread threads)))
4880          remaining-threads thread)
4881     (while stack
4882       (setq remaining-threads (cdr (car stack)))
4883       (if remaining-threads
4884           (progn (setq thread (car remaining-threads))
4885                  (setcdr (car stack) (cdr remaining-threads))
4886                  (if (cdr thread)
4887                      (push (cons thread (cdr thread)) stack)))
4888         (setq thread (caar stack))
4889         (setcdr thread (sort (cdr thread) func))
4890         (pop stack)))
4891     (cdr superthread)))
4892
4893 (defun gnus-sort-threads (threads)
4894   "Sort THREADS."
4895   (if (not gnus-thread-sort-functions)
4896       threads
4897     (gnus-message 8 "Sorting threads...")
4898     (prog1
4899         (condition-case nil
4900             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4901               (gnus-sort-threads-recursive
4902                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4903           ;; Even after binding max-lisp-eval-depth, the recursive
4904           ;; sorter might fail for very long threads.  In that case,
4905           ;; try using a (less well-tested) non-recursive sorter.
4906           (error (gnus-message 9 "Sorting threads with loop...")
4907                  (gnus-sort-threads-loop
4908                   threads (gnus-make-sort-function
4909                            gnus-thread-sort-functions))))
4910       (gnus-message 8 "Sorting threads...done"))))
4911
4912 (defun gnus-sort-articles (articles)
4913   "Sort ARTICLES."
4914   (when gnus-article-sort-functions
4915     (gnus-message 7 "Sorting articles...")
4916     (prog1
4917         (setq gnus-newsgroup-headers
4918               (sort articles (gnus-make-sort-function
4919                               gnus-article-sort-functions)))
4920       (gnus-message 7 "Sorting articles...done"))))
4921
4922 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4923 (defmacro gnus-thread-header (thread)
4924   "Return header of first article in THREAD.
4925 Note that THREAD must never, ever be anything else than a variable -
4926 using some other form will lead to serious barfage."
4927   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4928   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4929   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4930         (vector thread) 2))
4931
4932 (defsubst gnus-article-sort-by-number (h1 h2)
4933   "Sort articles by article number."
4934   (< (mail-header-number h1)
4935      (mail-header-number h2)))
4936
4937 (defun gnus-thread-sort-by-number (h1 h2)
4938   "Sort threads by root article number."
4939   (gnus-article-sort-by-number
4940    (gnus-thread-header h1) (gnus-thread-header h2)))
4941
4942 (defsubst gnus-article-sort-by-random (h1 h2)
4943   "Sort articles randomly."
4944   (zerop (random 2)))
4945
4946 (defun gnus-thread-sort-by-random (h1 h2)
4947   "Sort threads randomly."
4948   (gnus-article-sort-by-random
4949    (gnus-thread-header h1) (gnus-thread-header h2)))
4950
4951 (defsubst gnus-article-sort-by-lines (h1 h2)
4952   "Sort articles by article Lines header."
4953   (< (mail-header-lines h1)
4954      (mail-header-lines h2)))
4955
4956 (defun gnus-thread-sort-by-lines (h1 h2)
4957   "Sort threads by root article Lines header."
4958   (gnus-article-sort-by-lines
4959    (gnus-thread-header h1) (gnus-thread-header h2)))
4960
4961 (defsubst gnus-article-sort-by-chars (h1 h2)
4962   "Sort articles by octet length."
4963   (< (mail-header-chars h1)
4964      (mail-header-chars h2)))
4965
4966 (defun gnus-thread-sort-by-chars (h1 h2)
4967   "Sort threads by root article octet length."
4968   (gnus-article-sort-by-chars
4969    (gnus-thread-header h1) (gnus-thread-header h2)))
4970
4971 (defsubst gnus-article-sort-by-author (h1 h2)
4972   "Sort articles by root author."
4973   (gnus-string<
4974    (let ((extract (funcall
4975                    gnus-extract-address-components
4976                    (mail-header-from h1))))
4977      (or (car extract) (cadr extract) ""))
4978    (let ((extract (funcall
4979                    gnus-extract-address-components
4980                    (mail-header-from h2))))
4981      (or (car extract) (cadr extract) ""))))
4982
4983 (defun gnus-thread-sort-by-author (h1 h2)
4984   "Sort threads by root author."
4985   (gnus-article-sort-by-author
4986    (gnus-thread-header h1)  (gnus-thread-header h2)))
4987
4988 (defsubst gnus-article-sort-by-recipient (h1 h2)
4989   "Sort articles by recipient."
4990   (gnus-string<
4991    (let ((extract (funcall
4992                    gnus-extract-address-components
4993                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4994      (or (car extract) (cadr extract)))
4995    (let ((extract (funcall
4996                    gnus-extract-address-components
4997                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4998      (or (car extract) (cadr extract)))))
4999
5000 (defun gnus-thread-sort-by-recipient (h1 h2)
5001   "Sort threads by root recipient."
5002   (gnus-article-sort-by-recipient
5003    (gnus-thread-header h1) (gnus-thread-header h2)))
5004
5005 (defsubst gnus-article-sort-by-subject (h1 h2)
5006   "Sort articles by root subject."
5007   (gnus-string<
5008    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5009    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5010
5011 (defun gnus-thread-sort-by-subject (h1 h2)
5012   "Sort threads by root subject."
5013   (gnus-article-sort-by-subject
5014    (gnus-thread-header h1) (gnus-thread-header h2)))
5015
5016 (defsubst gnus-article-sort-by-date (h1 h2)
5017   "Sort articles by root article date."
5018   (time-less-p
5019    (gnus-date-get-time (mail-header-date h1))
5020    (gnus-date-get-time (mail-header-date h2))))
5021
5022 (defun gnus-thread-sort-by-date (h1 h2)
5023   "Sort threads by root article date."
5024   (gnus-article-sort-by-date
5025    (gnus-thread-header h1) (gnus-thread-header h2)))
5026
5027 (defsubst gnus-article-sort-by-score (h1 h2)
5028   "Sort articles by root article score.
5029 Unscored articles will be counted as having a score of zero."
5030   (> (or (cdr (assq (mail-header-number h1)
5031                     gnus-newsgroup-scored))
5032          gnus-summary-default-score 0)
5033      (or (cdr (assq (mail-header-number h2)
5034                     gnus-newsgroup-scored))
5035          gnus-summary-default-score 0)))
5036
5037 (defun gnus-thread-sort-by-score (h1 h2)
5038   "Sort threads by root article score."
5039   (gnus-article-sort-by-score
5040    (gnus-thread-header h1) (gnus-thread-header h2)))
5041
5042 (defun gnus-thread-sort-by-total-score (h1 h2)
5043   "Sort threads by the sum of all scores in the thread.
5044 Unscored articles will be counted as having a score of zero."
5045   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5046
5047 (defun gnus-thread-total-score (thread)
5048   ;; This function find the total score of THREAD.
5049   (cond
5050    ((null thread)
5051     0)
5052    ((consp thread)
5053     (if (stringp (car thread))
5054         (apply gnus-thread-score-function 0
5055                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5056       (gnus-thread-total-score-1 thread)))
5057    (t
5058     (gnus-thread-total-score-1 (list thread)))))
5059
5060 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5061   "Sort articles by number."
5062   (gnus-article-sort-by-number h1 h2))
5063
5064 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5065   "Sort threads such that the thread with the most recently arrived article comes first."
5066   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5067
5068 (defun gnus-thread-highest-number (thread)
5069   "Return the highest article number in THREAD."
5070   (apply 'max (mapcar (lambda (header)
5071                         (mail-header-number header))
5072                       (message-flatten-list thread))))
5073
5074 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5075   "Sort articles by number."
5076   (gnus-article-sort-by-date h1 h2))
5077
5078 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5079   "Sort threads such that the thread with the most recently dated article comes first."
5080   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5081
5082 ; Since this is called not only to sort the top-level threads, but
5083 ; also in recursive sorts to order the articles within a thread, each
5084 ; article will be processed many times.  Thus it speeds things up
5085 ; quite a bit to use gnus-date-get-time, which caches the time value.
5086 (defun gnus-thread-latest-date (thread)
5087   "Return the highest article date in THREAD."
5088   (apply 'max
5089          (mapcar (lambda (header) (gnus-float-time
5090                                    (gnus-date-get-time
5091                                     (mail-header-date header))))
5092                  (message-flatten-list thread))))
5093
5094 (defun gnus-thread-total-score-1 (root)
5095   ;; This function find the total score of the thread below ROOT.
5096   (setq root (car root))
5097   (apply gnus-thread-score-function
5098          (or (append
5099               (mapcar 'gnus-thread-total-score
5100                       (cdr (gnus-id-to-thread (mail-header-id root))))
5101               (when (> (mail-header-number root) 0)
5102                 (list (or (cdr (assq (mail-header-number root)
5103                                      gnus-newsgroup-scored))
5104                           gnus-summary-default-score 0))))
5105              (list gnus-summary-default-score)
5106              '(0))))
5107
5108 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5109 (defvar gnus-tmp-prev-subject nil)
5110 (defvar gnus-tmp-false-parent nil)
5111 (defvar gnus-tmp-root-expunged nil)
5112 (defvar gnus-tmp-dummy-line nil)
5113
5114 (defun gnus-extra-header (type &optional header)
5115   "Return the extra header of TYPE."
5116   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5117       ""))
5118
5119 (defvar gnus-tmp-thread-tree-header-string "")
5120
5121 (defcustom gnus-sum-thread-tree-root "> "
5122   "With %B spec, used for the root of a thread.
5123 If nil, use subject instead."
5124   :version "22.1"
5125   :type '(radio (const :format "%v  " nil) string)
5126   :group 'gnus-thread)
5127
5128 (defcustom gnus-sum-thread-tree-false-root "> "
5129   "With %B spec, used for a false root of a thread.
5130 If nil, use subject instead."
5131   :version "22.1"
5132   :type '(radio (const :format "%v  " nil) string)
5133   :group 'gnus-thread)
5134
5135 (defcustom gnus-sum-thread-tree-single-indent ""
5136   "With %B spec, used for a thread with just one message.
5137 If nil, use subject instead."
5138   :version "22.1"
5139   :type '(radio (const :format "%v  " nil) string)
5140   :group 'gnus-thread)
5141
5142 (defcustom gnus-sum-thread-tree-vertical "| "
5143   "With %B spec, used for drawing a vertical line."
5144   :version "22.1"
5145   :type 'string
5146   :group 'gnus-thread)
5147
5148 (defcustom gnus-sum-thread-tree-indent "  "
5149   "With %B spec, used for indenting."
5150   :version "22.1"
5151   :type 'string
5152   :group 'gnus-thread)
5153
5154 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5155   "With %B spec, used for a leaf with brothers."
5156   :version "22.1"
5157   :type 'string
5158   :group 'gnus-thread)
5159
5160 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5161   "With %B spec, used for a leaf without brothers."
5162   :version "22.1"
5163   :type 'string
5164   :group 'gnus-thread)
5165
5166 (defcustom gnus-summary-display-while-building nil
5167   "If non-nil, show and update the summary buffer as it's being built.
5168 If the value is t, update the buffer after every line is inserted.  If
5169 the value is an integer (N), update the display every N lines."
5170   :version "22.1"
5171   :group 'gnus-thread
5172   :type '(choice (const :tag "off" nil)
5173                  number
5174                  (const :tag "frequently" t)))
5175
5176 (defun gnus-summary-prepare-threads (threads)
5177   "Prepare summary buffer from THREADS and indentation LEVEL.
5178 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5179 or a straight list of headers."
5180   (gnus-message 7 "Generating summary...")
5181
5182   (setq gnus-newsgroup-threads threads)
5183   (beginning-of-line)
5184
5185   (let ((gnus-tmp-level 0)
5186         (default-score (or gnus-summary-default-score 0))
5187         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5188         (building-line-count gnus-summary-display-while-building)
5189         (building-count (integerp gnus-summary-display-while-building))
5190         thread number subject stack state gnus-tmp-gathered beg-match
5191         new-roots gnus-tmp-new-adopts thread-end simp-subject
5192         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5193         gnus-tmp-replied gnus-tmp-subject-or-nil
5194         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5195         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5196         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5197         tree-stack)
5198
5199     (setq gnus-tmp-prev-subject nil
5200           gnus-tmp-thread-tree-header-string "")
5201
5202     (if (vectorp (car threads))
5203         ;; If this is a straight (sic) list of headers, then a
5204         ;; threaded summary display isn't required, so we just create
5205         ;; an unthreaded one.
5206         (gnus-summary-prepare-unthreaded threads)
5207
5208       ;; Do the threaded display.
5209
5210       (if gnus-summary-display-while-building
5211           (switch-to-buffer (buffer-name)))
5212       (while (or threads stack gnus-tmp-new-adopts new-roots)
5213
5214         (if (and (= gnus-tmp-level 0)
5215                  (or (not stack)
5216                      (= (caar stack) 0))
5217                  (not gnus-tmp-false-parent)
5218                  (or gnus-tmp-new-adopts new-roots))
5219             (if gnus-tmp-new-adopts
5220                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5221                       thread (list (car gnus-tmp-new-adopts))
5222                       gnus-tmp-header (caar thread)
5223                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5224               (when new-roots
5225                 (setq thread (list (car new-roots))
5226                       gnus-tmp-header (caar thread)
5227                       new-roots (cdr new-roots))))
5228
5229           (if threads
5230               ;; If there are some threads, we do them before the
5231               ;; threads on the stack.
5232               (setq thread threads
5233                     gnus-tmp-header (caar thread))
5234             ;; There were no current threads, so we pop something off
5235             ;; the stack.
5236             (setq state (car stack)
5237                   gnus-tmp-level (car state)
5238                   tree-stack (cadr state)
5239                   thread (caddr state)
5240                   stack (cdr stack)
5241                   gnus-tmp-header (caar thread))))
5242
5243         (setq gnus-tmp-false-parent nil)
5244         (setq gnus-tmp-root-expunged nil)
5245         (setq thread-end nil)
5246
5247         (if (stringp gnus-tmp-header)
5248             ;; The header is a dummy root.
5249             (cond
5250              ((eq gnus-summary-make-false-root 'adopt)
5251               ;; We let the first article adopt the rest.
5252               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5253                                                (cddar thread)))
5254               (setq gnus-tmp-gathered
5255                     (nconc (mapcar
5256                             (lambda (h) (mail-header-number (car h)))
5257                             (cddar thread))
5258                            gnus-tmp-gathered))
5259               (setq thread (cons (list (caar thread)
5260                                        (cadar thread))
5261                                  (cdr thread)))
5262               (setq gnus-tmp-level -1
5263                     gnus-tmp-false-parent t))
5264              ((eq gnus-summary-make-false-root 'empty)
5265               ;; We print adopted articles with empty subject fields.
5266               (setq gnus-tmp-gathered
5267                     (nconc (mapcar
5268                             (lambda (h) (mail-header-number (car h)))
5269                             (cddar thread))
5270                            gnus-tmp-gathered))
5271               (setq gnus-tmp-level -1))
5272              ((eq gnus-summary-make-false-root 'dummy)
5273               ;; We remember that we probably want to output a dummy
5274               ;; root.
5275               (setq gnus-tmp-dummy-line gnus-tmp-header)
5276               (setq gnus-tmp-prev-subject gnus-tmp-header))
5277              (t
5278               ;; We do not make a root for the gathered
5279               ;; sub-threads at all.
5280               (setq gnus-tmp-level -1)))
5281
5282           (setq number (mail-header-number gnus-tmp-header)
5283                 subject (mail-header-subject gnus-tmp-header)
5284                 simp-subject (gnus-simplify-subject-fully subject))
5285
5286           (cond
5287            ;; If the thread has changed subject, we might want to make
5288            ;; this subthread into a root.
5289            ((and (null gnus-thread-ignore-subject)
5290                  (not (zerop gnus-tmp-level))
5291                  gnus-tmp-prev-subject
5292                  (not (string= gnus-tmp-prev-subject simp-subject)))
5293             (setq new-roots (nconc new-roots (list (car thread)))
5294                   thread-end t
5295                   gnus-tmp-header nil))
5296            ;; If the article lies outside the current limit,
5297            ;; then we do not display it.
5298            ((not (memq number gnus-newsgroup-limit))
5299             (setq gnus-tmp-gathered
5300                   (nconc (mapcar
5301                           (lambda (h) (mail-header-number (car h)))
5302                           (cdar thread))
5303                          gnus-tmp-gathered))
5304             (setq gnus-tmp-new-adopts (if (cdar thread)
5305                                           (append gnus-tmp-new-adopts
5306                                                   (cdar thread))
5307                                         gnus-tmp-new-adopts)
5308                   thread-end t
5309                   gnus-tmp-header nil)
5310             (when (zerop gnus-tmp-level)
5311               (setq gnus-tmp-root-expunged t)))
5312            ;; Perhaps this article is to be marked as read?
5313            ((and gnus-summary-mark-below
5314                  (< (or (cdr (assq number gnus-newsgroup-scored))
5315                         default-score)
5316                     gnus-summary-mark-below)
5317                  ;; Don't touch sparse articles.
5318                  (not (gnus-summary-article-sparse-p number))
5319                  (not (gnus-summary-article-ancient-p number)))
5320             (setq gnus-newsgroup-unreads
5321                   (delq number gnus-newsgroup-unreads))
5322             (if gnus-newsgroup-auto-expire
5323                 (setq gnus-newsgroup-expirable
5324                       (gnus-add-to-sorted-list
5325                        gnus-newsgroup-expirable number))
5326               (push (cons number gnus-low-score-mark)
5327                     gnus-newsgroup-reads))))
5328
5329           (when gnus-tmp-header
5330             ;; We may have an old dummy line to output before this
5331             ;; article.
5332             (when (and gnus-tmp-dummy-line
5333                        (gnus-subject-equal
5334                         gnus-tmp-dummy-line
5335                         (mail-header-subject gnus-tmp-header)))
5336               (gnus-summary-insert-dummy-line
5337                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5338               (setq gnus-tmp-dummy-line nil))
5339
5340             ;; Compute the mark.
5341             (setq gnus-tmp-unread (gnus-article-mark number))
5342
5343             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5344                                   gnus-tmp-header gnus-tmp-level)
5345                   gnus-newsgroup-data)
5346
5347             ;; Actually insert the line.
5348             (setq
5349              gnus-tmp-subject-or-nil
5350              (cond
5351               ((and gnus-thread-ignore-subject
5352                     gnus-tmp-prev-subject
5353                     (not (string= gnus-tmp-prev-subject simp-subject)))
5354                subject)
5355               ((zerop gnus-tmp-level)
5356                (if (and (eq gnus-summary-make-false-root 'empty)
5357                         (memq number gnus-tmp-gathered)
5358                         gnus-tmp-prev-subject
5359                         (string= gnus-tmp-prev-subject simp-subject))
5360                    gnus-summary-same-subject
5361                  subject))
5362               (t gnus-summary-same-subject)))
5363             (if (and (eq gnus-summary-make-false-root 'adopt)
5364                      (= gnus-tmp-level 1)
5365                      (memq number gnus-tmp-gathered))
5366                 (setq gnus-tmp-opening-bracket ?\<
5367                       gnus-tmp-closing-bracket ?\>)
5368               (setq gnus-tmp-opening-bracket ?\[
5369                     gnus-tmp-closing-bracket ?\]))
5370             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5371                 (gnus-make-thread-indent-array
5372                  (max (* 2 (length gnus-thread-indent-array))
5373                       gnus-tmp-level)))
5374             (setq
5375              gnus-tmp-indentation
5376              (aref gnus-thread-indent-array gnus-tmp-level)
5377              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5378              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5379                                 gnus-summary-default-score 0)
5380              gnus-tmp-score-char
5381              (if (or (null gnus-summary-default-score)
5382                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5383                          gnus-summary-zcore-fuzz))
5384                  ?                      ;Whitespace
5385                (if (< gnus-tmp-score gnus-summary-default-score)
5386                    gnus-score-below-mark gnus-score-over-mark))
5387              gnus-tmp-replied
5388              (cond ((memq number gnus-newsgroup-processable)
5389                     gnus-process-mark)
5390                    ((memq number gnus-newsgroup-cached)
5391                     gnus-cached-mark)
5392                    ((memq number gnus-newsgroup-replied)
5393                     gnus-replied-mark)
5394                    ((memq number gnus-newsgroup-forwarded)
5395                     gnus-forwarded-mark)
5396                    ((memq number gnus-newsgroup-saved)
5397                     gnus-saved-mark)
5398                    ((memq number gnus-newsgroup-unseen)
5399                     gnus-unseen-mark)
5400                    (t gnus-no-mark))
5401              gnus-tmp-downloaded
5402              (cond ((memq number gnus-newsgroup-undownloaded)
5403                     gnus-undownloaded-mark)
5404                    (gnus-newsgroup-agentized
5405                     gnus-downloaded-mark)
5406                    (t
5407                     gnus-no-mark))
5408              gnus-tmp-from (mail-header-from gnus-tmp-header)
5409              gnus-tmp-name
5410              (cond
5411               ((string-match "<[^>]+> *$" gnus-tmp-from)
5412                (setq beg-match (match-beginning 0))
5413                (or (and (string-match "^\".+\"" gnus-tmp-from)
5414                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5415                    (substring gnus-tmp-from 0 beg-match)))
5416               ((string-match "(.+)" gnus-tmp-from)
5417                (substring gnus-tmp-from
5418                           (1+ (match-beginning 0)) (1- (match-end 0))))
5419               (t gnus-tmp-from))
5420
5421              ;; Do the %B string
5422              gnus-tmp-thread-tree-header-string
5423              (cond
5424               ((not gnus-show-threads) "")
5425               ((zerop gnus-tmp-level)
5426                (cond ((cdar thread)
5427                       (or gnus-sum-thread-tree-root subject))
5428                      (gnus-tmp-new-adopts
5429                       (or gnus-sum-thread-tree-false-root subject))
5430                      (t
5431                       (or gnus-sum-thread-tree-single-indent subject))))
5432               (t
5433                (concat (apply 'concat
5434                               (mapcar (lambda (item)
5435                                         (if (= item 1)
5436                                             gnus-sum-thread-tree-vertical
5437                                           gnus-sum-thread-tree-indent))
5438                                       (cdr (reverse tree-stack))))
5439                        (if (nth 1 thread)
5440                            gnus-sum-thread-tree-leaf-with-other
5441                          gnus-sum-thread-tree-single-leaf)))))
5442             (when (string= gnus-tmp-name "")
5443               (setq gnus-tmp-name gnus-tmp-from))
5444             (unless (numberp gnus-tmp-lines)
5445               (setq gnus-tmp-lines -1))
5446             (if (= gnus-tmp-lines -1)
5447                 (setq gnus-tmp-lines "?")
5448               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5449             (gnus-put-text-property
5450              (point)
5451              (progn (eval gnus-summary-line-format-spec) (point))
5452              'gnus-number number)
5453             (when gnus-visual-p
5454               (forward-line -1)
5455               (gnus-summary-highlight-line)
5456               (when gnus-summary-update-hook
5457                 (gnus-run-hooks 'gnus-summary-update-hook))
5458               (forward-line 1))
5459
5460             (setq gnus-tmp-prev-subject simp-subject)))
5461
5462         (when (nth 1 thread)
5463           (push (list (max 0 gnus-tmp-level)
5464                       (copy-sequence tree-stack)
5465                       (nthcdr 1 thread))
5466                 stack))
5467         (push (if (nth 1 thread) 1 0) tree-stack)
5468         (incf gnus-tmp-level)
5469         (setq threads (if thread-end nil (cdar thread)))
5470         (if gnus-summary-display-while-building
5471             (if building-count
5472                 (progn
5473                   ;; use a set frequency
5474                   (setq building-line-count (1- building-line-count))
5475                   (when (= building-line-count 0)
5476                     (sit-for 0)
5477                     (setq building-line-count
5478                           gnus-summary-display-while-building)))
5479               ;; always
5480               (sit-for 0)))
5481         (unless threads
5482           (setq gnus-tmp-level 0)))))
5483   (gnus-message 7 "Generating summary...done"))
5484
5485 (defun gnus-summary-prepare-unthreaded (headers)
5486   "Generate an unthreaded summary buffer based on HEADERS."
5487   (let (header number mark)
5488
5489     (beginning-of-line)
5490
5491     (while headers
5492       ;; We may have to root out some bad articles...
5493       (when (memq (setq number (mail-header-number
5494                                 (setq header (pop headers))))
5495                   gnus-newsgroup-limit)
5496         ;; Mark article as read when it has a low score.
5497         (when (and gnus-summary-mark-below
5498                    (< (or (cdr (assq number gnus-newsgroup-scored))
5499                           gnus-summary-default-score 0)
5500                       gnus-summary-mark-below)
5501                    (not (gnus-summary-article-ancient-p number)))
5502           (setq gnus-newsgroup-unreads
5503                 (delq number gnus-newsgroup-unreads))
5504           (if gnus-newsgroup-auto-expire
5505               (push number gnus-newsgroup-expirable)
5506             (push (cons number gnus-low-score-mark)
5507                   gnus-newsgroup-reads)))
5508
5509         (setq mark (gnus-article-mark number))
5510         (push (gnus-data-make number mark (1+ (point)) header 0)
5511               gnus-newsgroup-data)
5512         (gnus-summary-insert-line
5513          header 0 number
5514          (memq number gnus-newsgroup-undownloaded)
5515          mark (memq number gnus-newsgroup-replied)
5516          (memq number gnus-newsgroup-expirable)
5517          (mail-header-subject header) nil
5518          (cdr (assq number gnus-newsgroup-scored))
5519          (memq number gnus-newsgroup-processable))))))
5520
5521 (defun gnus-group-get-list-identifiers (group)
5522   "Get list identifier regexp for GROUP."
5523   (or (gnus-parameter-list-identifier group)
5524       (if (consp gnus-list-identifiers)
5525           (mapconcat 'identity gnus-list-identifiers " *\\|")
5526         gnus-list-identifiers)))
5527
5528 (defun gnus-summary-remove-list-identifiers ()
5529   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5530   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5531         changed subject)
5532     (when regexp
5533       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5534       (dolist (header gnus-newsgroup-headers)
5535         (setq subject (mail-header-subject header)
5536               changed nil)
5537         (while (string-match regexp subject)
5538           (setq subject
5539                 (concat (substring subject 0 (match-beginning 1))
5540                         (substring subject (match-end 0)))
5541                 changed t))
5542         (when changed
5543           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5544             (setq subject
5545                   (concat (substring subject 0 (match-beginning 1))
5546                           (substring subject (match-end 1)))))
5547           (mail-header-set-subject header subject))))))
5548
5549 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5550   "Fetch headers of ARTICLES."
5551   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5552     (gnus-message 7 "Fetching headers for %s..." name)
5553     (prog1
5554         (if (eq 'nov
5555                 (setq gnus-headers-retrieved-by
5556                       (gnus-retrieve-headers
5557                        articles gnus-newsgroup-name
5558                        (or limit
5559                            ;; We might want to fetch old headers, but
5560                            ;; not if there is only 1 article.
5561                            (and (or (and
5562                                      (not (eq gnus-fetch-old-headers 'some))
5563                                      (not (numberp gnus-fetch-old-headers)))
5564                                     (> (length articles) 1))
5565                                 gnus-fetch-old-headers)))))
5566             (gnus-get-newsgroup-headers-xover
5567              articles force-new dependencies gnus-newsgroup-name t)
5568           (gnus-get-newsgroup-headers dependencies force-new))
5569       (gnus-message 7 "Fetching headers for %s...done" name))))
5570
5571 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5572   "Select newsgroup GROUP.
5573 If READ-ALL is non-nil, all articles in the group are selected.
5574 If SELECT-ARTICLES, only select those articles from GROUP."
5575   (let* ((entry (gnus-group-entry group))
5576          ;;!!! Dirty hack; should be removed.
5577          (gnus-summary-ignore-duplicates
5578           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5579               t
5580             gnus-summary-ignore-duplicates))
5581          (info (nth 2 entry))
5582          charset articles fetched-articles cached)
5583
5584     (unless (gnus-check-server
5585              (set (make-local-variable 'gnus-current-select-method)
5586                   (gnus-find-method-for-group group)))
5587       (error "Couldn't open server"))
5588     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5589
5590     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5591         (gnus-activate-group group)     ; Or we can activate it...
5592         (progn                          ; Or we bug out.
5593           (when (equal major-mode 'gnus-summary-mode)
5594             (gnus-kill-buffer (current-buffer)))
5595           (error
5596            "Couldn't activate group %s: %s"
5597            (mm-decode-coding-string group charset)
5598            (mm-decode-coding-string (gnus-status-message group) charset))))
5599
5600     (unless (gnus-request-group group t)
5601       (when (equal major-mode 'gnus-summary-mode)
5602         (gnus-kill-buffer (current-buffer)))
5603       (error "Couldn't request group %s: %s"
5604              (mm-decode-coding-string group charset)
5605              (mm-decode-coding-string (gnus-status-message group) charset)))
5606
5607     (when (and gnus-agent
5608                (gnus-active group))
5609       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5610
5611       (setq gnus-summary-use-undownloaded-faces
5612             (gnus-agent-find-parameter
5613              group
5614              'agent-enable-undownloaded-faces)))
5615
5616     (setq gnus-newsgroup-name group
5617           gnus-newsgroup-unselected nil
5618           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5619
5620     (let ((display (gnus-group-find-parameter group 'display)))
5621       (setq gnus-newsgroup-display
5622             (cond
5623              ((not (zerop (or (car-safe read-all) 0)))
5624               ;; The user entered the group with C-u SPC/RET, let's show
5625               ;; all articles.
5626               'gnus-not-ignore)
5627              ((eq display 'all)
5628               'gnus-not-ignore)
5629              ((arrayp display)
5630               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5631              ((numberp display)
5632               ;; The following is probably the "correct" solution, but
5633               ;; it makes Gnus fetch all headers and then limit the
5634               ;; articles (which is slow), so instead we hack the
5635               ;; select-articles parameter instead. -- Simon Josefsson
5636               ;; <jas@kth.se>
5637               ;;
5638               ;; (gnus-byte-compile
5639               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5640               ;;                         display)))))
5641               (setq select-articles
5642                     (gnus-uncompress-range
5643                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5644                              (if (> tmp 0)
5645                                  tmp
5646                                1))
5647                            (cdr (gnus-active group)))))
5648               nil)
5649              (t
5650               nil))))
5651
5652     (gnus-summary-setup-default-charset)
5653
5654     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5655     (when (gnus-virtual-group-p group)
5656       (setq cached gnus-newsgroup-cached))
5657
5658     (setq gnus-newsgroup-unreads
5659           (gnus-sorted-ndifference
5660            (gnus-sorted-ndifference gnus-newsgroup-unreads
5661                                     gnus-newsgroup-marked)
5662            gnus-newsgroup-dormant))
5663
5664     (setq gnus-newsgroup-processable nil)
5665
5666     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5667
5668     ;; Adjust and set lists of article marks.
5669     (when info
5670       (gnus-adjust-marked-articles info))
5671     (if (setq articles select-articles)
5672         (setq gnus-newsgroup-unselected
5673               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5674       (setq articles (gnus-articles-to-read group read-all)))
5675     
5676     (cond
5677      ((null articles)
5678       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5679       'quit)
5680      ((eq articles 0) nil)
5681      (t
5682       ;; Init the dependencies hash table.
5683       (setq gnus-newsgroup-dependencies
5684             (gnus-make-hashtable (length articles)))
5685       (gnus-set-global-variables)
5686       ;; Retrieve the headers and read them in.
5687
5688       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5689
5690       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5691       (when cached
5692         (setq gnus-newsgroup-cached cached))
5693
5694       ;; Suppress duplicates?
5695       (when gnus-suppress-duplicates
5696         (gnus-dup-suppress-articles))
5697
5698       ;; Set the initial limit.
5699       (setq gnus-newsgroup-limit (copy-sequence articles))
5700       ;; Remove canceled articles from the list of unread articles.
5701       (setq fetched-articles
5702             (mapcar (lambda (headers) (mail-header-number headers))
5703                     gnus-newsgroup-headers))
5704       (setq gnus-newsgroup-articles fetched-articles)
5705       (setq gnus-newsgroup-unreads
5706             (gnus-sorted-nintersection
5707              gnus-newsgroup-unreads fetched-articles))
5708       (gnus-compute-unseen-list)
5709
5710       ;; Removed marked articles that do not exist.
5711       (gnus-update-missing-marks
5712        (gnus-sorted-difference articles fetched-articles))
5713       ;; We might want to build some more threads first.
5714       (when (and gnus-fetch-old-headers
5715                  (eq gnus-headers-retrieved-by 'nov))
5716         (if (eq gnus-fetch-old-headers 'invisible)
5717             (gnus-build-all-threads)
5718           (gnus-build-old-threads)))
5719       ;; Let the Gnus agent mark articles as read.
5720       (when gnus-agent
5721         (gnus-agent-get-undownloaded-list))
5722       ;; Remove list identifiers from subject
5723       (gnus-summary-remove-list-identifiers)
5724       ;; Check whether auto-expire is to be done in this group.
5725       (setq gnus-newsgroup-auto-expire
5726             (and (gnus-group-auto-expirable-p group)
5727                  (not (gnus-group-read-only-p group))))
5728       ;; Set up the article buffer now, if necessary.
5729       (unless (and gnus-single-article-buffer
5730                    (equal gnus-article-buffer "*Article*"))
5731         (gnus-article-setup-buffer))
5732       ;; First and last article in this newsgroup.
5733       (when gnus-newsgroup-headers
5734         (setq gnus-newsgroup-begin
5735               (mail-header-number (car gnus-newsgroup-headers))
5736               gnus-newsgroup-end
5737               (mail-header-number
5738                (gnus-last-element gnus-newsgroup-headers))))
5739       ;; GROUP is successfully selected.
5740       (or gnus-newsgroup-headers t)))))
5741
5742 (defun gnus-compute-unseen-list ()
5743   ;; The `seen' marks are treated specially.
5744   (if (not gnus-newsgroup-seen)
5745       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5746     (setq gnus-newsgroup-unseen
5747           (gnus-inverse-list-range-intersection
5748            gnus-newsgroup-articles gnus-newsgroup-seen))))
5749
5750 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5751
5752 (defun gnus-summary-display-make-predicate (display)
5753   (require 'gnus-agent)
5754   (when (= (length display) 1)
5755     (setq display (car display)))
5756   (unless gnus-summary-display-cache
5757     (dolist (elem (append '((unread . unread)
5758                             (read . read)
5759                             (unseen . unseen))
5760                           gnus-article-mark-lists))
5761       (push (cons (cdr elem)
5762                   (gnus-byte-compile    ;Why bother?
5763                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5764             gnus-summary-display-cache)))
5765   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5766         (gnus-category-predicate-cache gnus-summary-display-cache))
5767     (gnus-get-predicate display)))
5768
5769 ;; Uses the dynamically bound `gnus-number' variable.
5770 (defvar gnus-number)
5771 (defun gnus-article-marked-p (type &optional article)
5772   (let ((article (or article gnus-number)))
5773     (cond
5774      ((eq type 'tick)
5775       (memq article gnus-newsgroup-marked))
5776      ((eq type 'spam)
5777       (memq article gnus-newsgroup-spam-marked))
5778      ((eq type 'unsend)
5779       (memq article gnus-newsgroup-unsendable))
5780      ((eq type 'undownload)
5781       (memq article gnus-newsgroup-undownloaded))
5782      ((eq type 'download)
5783       (memq article gnus-newsgroup-downloadable))
5784      ((eq type 'unread)
5785       (memq article gnus-newsgroup-unreads))
5786      ((eq type 'read)
5787       (memq article gnus-newsgroup-reads))
5788      ((eq type 'dormant)
5789       (memq article gnus-newsgroup-dormant) )
5790      ((eq type 'expire)
5791       (memq article gnus-newsgroup-expirable))
5792      ((eq type 'reply)
5793       (memq article gnus-newsgroup-replied))
5794      ((eq type 'killed)
5795       (memq article gnus-newsgroup-killed))
5796      ((eq type 'bookmark)
5797       (assq article gnus-newsgroup-bookmarks))
5798      ((eq type 'score)
5799       (assq article gnus-newsgroup-scored))
5800      ((eq type 'save)
5801       (memq article gnus-newsgroup-saved))
5802      ((eq type 'cache)
5803       (memq article gnus-newsgroup-cached))
5804      ((eq type 'forward)
5805       (memq article gnus-newsgroup-forwarded))
5806      ((eq type 'seen)
5807       (not (memq article gnus-newsgroup-unseen)))
5808      (t t))))
5809
5810 (defun gnus-articles-to-read (group &optional read-all)
5811   "Find out what articles the user wants to read."
5812   (let* ((only-read-p t)
5813          (articles
5814           (gnus-list-range-difference
5815           ;; Select all articles if `read-all' is non-nil, or if there
5816           ;; are no unread articles.
5817           (if (or read-all
5818                   (and (zerop (length gnus-newsgroup-marked))
5819                        (zerop (length gnus-newsgroup-unreads)))
5820                   ;; Fetch all if the predicate is non-nil.
5821                   gnus-newsgroup-display)
5822               ;; We want to select the headers for all the articles in
5823               ;; the group, so we select either all the active
5824               ;; articles in the group, or (if that's nil), the
5825               ;; articles in the cache.
5826               (or
5827                (if gnus-newsgroup-maximum-articles
5828                    (let ((active (gnus-active group)))
5829                      (gnus-uncompress-range
5830                       (cons (max (car active)
5831                                  (- (cdr active)
5832                                     gnus-newsgroup-maximum-articles
5833                                     -1))
5834                             (cdr active))))
5835                  (gnus-uncompress-range (gnus-active group)))
5836                (gnus-cache-articles-in-group group))
5837             ;; Select only the "normal" subset of articles.
5838             (setq only-read-p nil)
5839             (gnus-sorted-nunion
5840              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5841              gnus-newsgroup-unreads))
5842           (cdr (assq 'unexist (gnus-info-marks (gnus-get-info group))))))
5843          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5844          (scored (length scored-list))
5845          (number (length articles))
5846          (marked (+ (length gnus-newsgroup-marked)
5847                     (length gnus-newsgroup-dormant)))
5848          (select
5849           (cond
5850            ((numberp read-all)
5851             read-all)
5852            ((numberp gnus-newsgroup-display)
5853             gnus-newsgroup-display)
5854            (t
5855             (condition-case ()
5856                 (cond
5857                  ((and (or (<= scored marked) (= scored number))
5858                        (numberp gnus-large-newsgroup)
5859                        (> number gnus-large-newsgroup))
5860                   (let* ((cursor-in-echo-area nil)
5861                          (initial (gnus-parameter-large-newsgroup-initial
5862                                    gnus-newsgroup-name))
5863                          (default (if only-read-p
5864                                       (or initial gnus-large-newsgroup)
5865                                     number))
5866                          (input
5867                           (read-string
5868                            (if only-read-p
5869                                (format
5870                                 "How many articles from %s (available %d, default %d): "
5871                                 (gnus-group-decoded-name
5872                                  (gnus-group-real-name gnus-newsgroup-name))
5873                                 number default)
5874                              (format
5875                               "How many articles from %s (%d default): "
5876                               (gnus-group-decoded-name
5877                                (gnus-group-real-name gnus-newsgroup-name))
5878                               default))
5879                            nil
5880                            nil
5881                            (number-to-string default))))
5882                     (if (string-match "^[ \t]*$" input) number input)))
5883                  ((and (> scored marked) (< scored number)
5884                        (> (- scored number) 20))
5885                   (let ((input
5886                          (read-string
5887                           (format "%s %s (%d scored, %d total): "
5888                                   "How many articles from"
5889                                   (gnus-group-decoded-name
5890                                    (gnus-group-real-name gnus-newsgroup-name))
5891                                   scored number))))
5892                     (if (string-match "^[ \t]*$" input)
5893                         number input)))
5894                  (t number))
5895               (quit
5896                (message "Quit getting the articles to read")
5897                nil))))))
5898     (setq select (if (stringp select) (string-to-number select) select))
5899     (if (or (null select) (zerop select))
5900         select
5901       (if (and (not (zerop scored)) (<= (abs select) scored))
5902           (progn
5903             (setq articles (sort scored-list '<))
5904             (setq number (length articles)))
5905         (setq articles (copy-sequence articles)))
5906
5907       (when (< (abs select) number)
5908         (if (< select 0)
5909             ;; Select the N oldest articles.
5910             (setcdr (nthcdr (1- (abs select)) articles) nil)
5911           ;; Select the N most recent articles.
5912           (setq articles (nthcdr (- number select) articles))))
5913       (setq gnus-newsgroup-unselected
5914             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5915       (when gnus-alter-articles-to-read-function
5916         (setq articles
5917               (sort
5918                (funcall gnus-alter-articles-to-read-function
5919                         gnus-newsgroup-name articles)
5920                '<)))
5921       articles)))
5922
5923 (defun gnus-killed-articles (killed articles)
5924   (let (out)
5925     (while articles
5926       (when (inline (gnus-member-of-range (car articles) killed))
5927         (push (car articles) out))
5928       (setq articles (cdr articles)))
5929     out))
5930
5931 (defun gnus-uncompress-marks (marks)
5932   "Uncompress the mark ranges in MARKS."
5933   (let ((uncompressed '(score bookmark))
5934         out)
5935     (while marks
5936       (if (memq (caar marks) uncompressed)
5937           (push (car marks) out)
5938         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5939       (setq marks (cdr marks)))
5940     out))
5941
5942 (defun gnus-article-mark-to-type (mark)
5943   "Return the type of MARK."
5944   (or (cadr (assq mark gnus-article-special-mark-lists))
5945       'list))
5946
5947 (defun gnus-article-unpropagatable-p (mark)
5948   "Return whether MARK should be propagated to back end."
5949   (memq mark gnus-article-unpropagated-mark-lists))
5950
5951 (defun gnus-adjust-marked-articles (info)
5952   "Set all article lists and remove all marks that are no longer valid."
5953   (let* ((marked-lists (gnus-info-marks info))
5954          (active (gnus-active (gnus-info-group info)))
5955          (min (car active))
5956          (max (cdr active))
5957          (types gnus-article-mark-lists)
5958          marks var articles article mark mark-type
5959          bgn end)
5960     ;; Hack to avoid adjusting marks for imap.
5961     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5962               'nnimap)
5963       (setq min 1))
5964
5965     (dolist (marks marked-lists)
5966       (setq mark (car marks)
5967             mark-type (gnus-article-mark-to-type mark)
5968             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5969       ;; We set the variable according to the type of the marks list,
5970       ;; and then adjust the marks to a subset of the active articles.
5971       (cond
5972        ;; Adjust "simple" lists - compressed yet unsorted
5973        ((eq mark-type 'list)
5974         ;; Simultaneously uncompress and clip to active range
5975         ;; See gnus-uncompress-range for a description of possible marks
5976         (let (l lh)
5977           (if (not (cadr marks))
5978               (set var nil)
5979             (setq articles (if (numberp (cddr marks))
5980                                (list (cdr marks))
5981                              (cdr marks))
5982                   lh (cons nil nil)
5983                   l lh)
5984
5985             (while (setq article (pop articles))
5986               (cond ((consp article)
5987                      (setq bgn (max (car article) min)
5988                            end (min (cdr article) max))
5989                      (while (<= bgn end)
5990                        (setq l (setcdr l (cons bgn nil))
5991                              bgn (1+ bgn))))
5992                     ((and (<= min article)
5993                           (>= max article))
5994                      (setq l (setcdr l (cons article nil))))))
5995             (set var (cdr lh)))))
5996        ;; Adjust assocs.
5997        ((eq mark-type 'tuple)
5998         (set var (setq articles (cdr marks)))
5999         (when (not (listp (cdr (symbol-value var))))
6000           (set var (list (symbol-value var))))
6001         (when (not (listp (cdr articles)))
6002           (setq articles (list articles)))
6003         (while articles
6004           (when (or (not (consp (setq article (pop articles))))
6005                     (< (car article) min)
6006                     (> (car article) max))
6007             (set var (delq article (symbol-value var))))))
6008        ;; Adjust ranges (sloppily).
6009        ((eq mark-type 'range)
6010         (cond
6011          ((eq mark 'seen)
6012           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
6013           ;; It should be (seen (NUM1 . NUM2)).
6014           (when (numberp (cddr marks))
6015             (setcdr marks (list (cdr marks))))
6016           (setq articles (cdr marks))
6017           (while (and articles
6018                       (or (and (consp (car articles))
6019                                (> min (cdar articles)))
6020                           (and (numberp (car articles))
6021                                (> min (car articles)))))
6022             (pop articles))
6023           (set var articles))
6024          ((eq mark 'unexist)
6025           (set var (cdr marks)))))))))
6026
6027 (defun gnus-update-missing-marks (missing)
6028   "Go through the list of MISSING articles and remove them from the mark lists."
6029   (when missing
6030     (let (var m)
6031       ;; Go through all types.
6032       (dolist (elem gnus-article-mark-lists)
6033         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6034           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6035           (when (symbol-value var)
6036             ;; This list has articles.  So we delete all missing
6037             ;; articles from it.
6038             (setq m missing)
6039             (while m
6040               (set var (delq (pop m) (symbol-value var))))))))))
6041
6042 (defun gnus-update-marks ()
6043   "Enter the various lists of marked articles into the newsgroup info list."
6044   (let ((types gnus-article-mark-lists)
6045         (info (gnus-get-info gnus-newsgroup-name))
6046         type list newmarked symbol delta-marks)
6047     (when info
6048       ;; Add all marks lists to the list of marks lists.
6049       (while (setq type (pop types))
6050         (setq list (symbol-value
6051                     (setq symbol
6052                           (intern (format "gnus-newsgroup-%s" (car type))))))
6053
6054         (when list
6055           ;; Get rid of the entries of the articles that have the
6056           ;; default score.
6057           (when (and (eq (cdr type) 'score)
6058                      gnus-save-score
6059                      list)
6060             (let* ((arts list)
6061                    (prev (cons nil list))
6062                    (all prev))
6063               (while arts
6064                 (if (or (not (consp (car arts)))
6065                         (= (cdar arts) gnus-summary-default-score))
6066                     (setcdr prev (cdr arts))
6067                   (setq prev arts))
6068                 (setq arts (cdr arts)))
6069               (setq list (cdr all)))))
6070
6071         (when (eq (cdr type) 'seen)
6072           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6073
6074         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6075           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6076
6077         (when (and (gnus-check-backend-function
6078                     'request-set-mark gnus-newsgroup-name)
6079                    (not (gnus-article-unpropagatable-p (cdr type))))
6080           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6081                  ;; Don't do anything about marks for articles we
6082                  ;; didn't actually get any headers for.
6083                  (del
6084                   (gnus-list-range-intersection
6085                    gnus-newsgroup-articles
6086                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6087                  (add
6088                   (gnus-list-range-intersection
6089                    gnus-newsgroup-articles
6090                    (gnus-remove-from-range
6091                     (gnus-copy-sequence list) old))))
6092             (when add
6093               (push (list add 'add (list (cdr type))) delta-marks))
6094             (when del
6095               ;; Don't delete marks from outside the active range.
6096               ;; This shouldn't happen, but is a sanity check.
6097               (setq del (gnus-sorted-range-intersection
6098                          (gnus-active gnus-newsgroup-name) del))
6099               (push (list del 'del (list (cdr type))) delta-marks))))
6100
6101         (when list
6102           (push (cons (cdr type) list) newmarked)))
6103
6104       (when delta-marks
6105         (unless (gnus-check-group gnus-newsgroup-name)
6106           (error "Can't open server for %s" gnus-newsgroup-name))
6107         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6108
6109       ;; Enter these new marks into the info of the group.
6110       (if (nthcdr 3 info)
6111           (setcar (nthcdr 3 info) newmarked)
6112         ;; Add the marks lists to the end of the info.
6113         (when newmarked
6114           (setcdr (nthcdr 2 info) (list newmarked))))
6115
6116       ;; Cut off the end of the info if there's nothing else there.
6117       (let ((i 5))
6118         (while (and (> i 2)
6119                     (not (nth i info)))
6120           (when (nthcdr (decf i) info)
6121             (setcdr (nthcdr i info) nil)))))))
6122
6123 (defun gnus-set-mode-line (where)
6124   "Set the mode line of the article or summary buffers.
6125 If WHERE is `summary', the summary mode line format will be used."
6126   ;; Is this mode line one we keep updated?
6127   (when (and (memq where gnus-updated-mode-lines)
6128              (symbol-value
6129               (intern (format "gnus-%s-mode-line-format-spec" where))))
6130     (let (mode-string)
6131       ;; We evaluate this in the summary buffer since these
6132       ;; variables are buffer-local to that buffer.
6133       (with-current-buffer gnus-summary-buffer
6134         ;; We bind all these variables that are used in the `eval' form
6135         ;; below.
6136         (let* ((mformat (symbol-value
6137                          (intern
6138                           (format "gnus-%s-mode-line-format-spec" where))))
6139                (gnus-tmp-group-name (gnus-mode-string-quote
6140                                      (gnus-group-decoded-name
6141                                       gnus-newsgroup-name)))
6142                (gnus-tmp-article-number (or gnus-current-article 0))
6143                (gnus-tmp-unread gnus-newsgroup-unreads)
6144                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6145                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6146                (gnus-tmp-unread-and-unselected
6147                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6148                             (zerop gnus-tmp-unselected))
6149                        "")
6150                       ((zerop gnus-tmp-unselected)
6151                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6152                       (t (format "{%d(+%d) more}"
6153                                  gnus-tmp-unread-and-unticked
6154                                  gnus-tmp-unselected))))
6155                (gnus-tmp-subject
6156                 (if (and gnus-current-headers
6157                          (vectorp gnus-current-headers))
6158                     (gnus-mode-string-quote
6159                      (mail-header-subject gnus-current-headers))
6160                   ""))
6161                bufname-length max-len
6162                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6163           (setq mode-string (eval mformat))
6164           (setq bufname-length (if (string-match "%b" mode-string)
6165                                    (- (length
6166                                        (buffer-name
6167                                         (if (eq where 'summary)
6168                                             nil
6169                                           (get-buffer gnus-article-buffer))))
6170                                       2)
6171                                  0))
6172           (setq max-len (max 4 (if gnus-mode-non-string-length
6173                                    (- (window-width)
6174                                       gnus-mode-non-string-length
6175                                       bufname-length)
6176                                  (length mode-string))))
6177           ;; We might have to chop a bit of the string off...
6178           (when (> (length mode-string) max-len)
6179             (setq mode-string
6180                   (concat (truncate-string-to-width mode-string (- max-len 3))
6181                           "...")))))
6182       ;; Update the mode line.
6183       (setq mode-line-buffer-identification
6184             (gnus-mode-line-buffer-identification (list mode-string)))
6185       (set-buffer-modified-p t))))
6186
6187 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6188   "Go through the HEADERS list and add all Xrefs to a hash table.
6189 The resulting hash table is returned, or nil if no Xrefs were found."
6190   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6191          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6192          (xref-hashtb (gnus-make-hashtable))
6193          start group entry number xrefs header)
6194     (while headers
6195       (setq header (pop headers))
6196       (when (and (setq xrefs (mail-header-xref header))
6197                  (not (memq (setq number (mail-header-number header))
6198                             unreads)))
6199         (setq start 0)
6200         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6201           (setq start (match-end 0))
6202           (setq group (if prefix
6203                           (concat prefix (substring xrefs (match-beginning 1)
6204                                                     (match-end 1)))
6205                         (substring xrefs (match-beginning 1) (match-end 1))))
6206           (setq number
6207                 (string-to-number (substring xrefs (match-beginning 2)
6208                                           (match-end 2))))
6209           (if (setq entry (gnus-gethash group xref-hashtb))
6210               (setcdr entry (cons number (cdr entry)))
6211             (gnus-sethash group (cons number nil) xref-hashtb)))))
6212     (and start xref-hashtb)))
6213
6214 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6215   "Look through all the headers and mark the Xrefs as read."
6216   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6217         name info xref-hashtb idlist method nth4)
6218     (with-current-buffer gnus-group-buffer
6219       (when (setq xref-hashtb
6220                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6221         (mapatoms
6222          (lambda (group)
6223            (unless (string= from-newsgroup (setq name (symbol-name group)))
6224              (setq idlist (symbol-value group))
6225              ;; Dead groups are not updated.
6226              (and (prog1
6227                       (setq info (gnus-get-info name))
6228                     (when (stringp (setq nth4 (gnus-info-method info)))
6229                       (setq nth4 (gnus-server-to-method nth4))))
6230                   ;; Only do the xrefs if the group has the same
6231                   ;; select method as the group we have just read.
6232                   (or (gnus-methods-equal-p
6233                        nth4 (gnus-find-method-for-group from-newsgroup))
6234                       virtual
6235                       (equal nth4 (setq method (gnus-find-method-for-group
6236                                                 from-newsgroup)))
6237                       (and (equal (car nth4) (car method))
6238                            (equal (nth 1 nth4) (nth 1 method))))
6239                   gnus-use-cross-reference
6240                   (or (not (eq gnus-use-cross-reference t))
6241                       virtual
6242                       ;; Only do cross-references on subscribed
6243                       ;; groups, if that is what is wanted.
6244                       (<= (gnus-info-level info) gnus-level-subscribed))
6245                   (gnus-group-make-articles-read name idlist))))
6246          xref-hashtb)))))
6247
6248 (defun gnus-compute-read-articles (group articles)
6249   (let* ((entry (gnus-group-entry group))
6250          (info (nth 2 entry))
6251          (active (gnus-active group))
6252          ninfo)
6253     (when entry
6254       ;; First peel off all invalid article numbers.
6255       (when active
6256         (let ((ids articles)
6257               id first)
6258           (while (setq id (pop ids))
6259             (when (and first (> id (cdr active)))
6260               ;; We'll end up in this situation in one particular
6261               ;; obscure situation.  If you re-scan a group and get
6262               ;; a new article that is cross-posted to a different
6263               ;; group that has not been re-scanned, you might get
6264               ;; crossposted article that has a higher number than
6265               ;; Gnus believes possible.  So we re-activate this
6266               ;; group as well.  This might mean doing the
6267               ;; crossposting thingy will *increase* the number
6268               ;; of articles in some groups.  Tsk, tsk.
6269               (setq active (or (gnus-activate-group group) active)))
6270             (when (or (> id (cdr active))
6271                       (< id (car active)))
6272               (setq articles (delq id articles))))))
6273       ;; If the read list is nil, we init it.
6274       (if (and active
6275                (null (gnus-info-read info))
6276                (> (car active) 1))
6277           (setq ninfo (cons 1 (1- (car active))))
6278         (setq ninfo (gnus-info-read info)))
6279       ;; Then we add the read articles to the range.
6280       (gnus-add-to-range
6281        ninfo (setq articles (sort articles '<))))))
6282
6283 (defun gnus-group-make-articles-read (group articles)
6284   "Update the info of GROUP to say that ARTICLES are read."
6285   (let* ((num 0)
6286          (entry (gnus-group-entry group))
6287          (info (nth 2 entry))
6288          (active (gnus-active group))
6289          (set-marks
6290           (gnus-method-option-p
6291            (gnus-find-method-for-group group)
6292            'server-marks))
6293          range)
6294     (if (not entry)
6295         ;; Group that Gnus doesn't know exists, but still allow the
6296         ;; backend to set marks.
6297         (when set-marks
6298           (gnus-request-set-mark
6299            group (list (list (gnus-compress-sequence (sort articles #'<))
6300                              'add '(read)))))
6301       ;; Normal, subscribed groups.
6302       (setq range (gnus-compute-read-articles group articles))
6303       (with-current-buffer gnus-group-buffer
6304         (gnus-undo-register
6305           `(progn
6306              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6307              (gnus-info-set-read ',info ',(gnus-info-read info))
6308              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6309              (when ,set-marks
6310                (gnus-request-set-mark
6311                 ,group (list (list ',range 'del '(read)))))
6312              (gnus-group-update-group ,group t))))
6313       ;; Add the read articles to the range.
6314       (gnus-info-set-read info range)
6315       (when set-marks
6316         (gnus-request-set-mark group (list (list range 'add '(read)))))
6317       ;; Then we have to re-compute how many unread
6318       ;; articles there are in this group.
6319       (when active
6320         (cond
6321          ((not range)
6322           (setq num (- (1+ (cdr active)) (car active))))
6323          ((not (listp (cdr range)))
6324           (setq num (- (cdr active) (- (1+ (cdr range))
6325                                        (car range)))))
6326          (t
6327           (while range
6328             (if (numberp (car range))
6329                 (setq num (1+ num))
6330               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6331             (setq range (cdr range)))
6332           (setq num (- (cdr active) num))))
6333         ;; Update the number of unread articles.
6334         (setcar entry num)
6335         ;; Update the group buffer.
6336         (unless (gnus-ephemeral-group-p group)
6337           (gnus-group-update-group group t))))))
6338
6339 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6340   (let ((cur nntp-server-buffer)
6341         (dependencies
6342          (or dependencies
6343              (with-current-buffer gnus-summary-buffer
6344                gnus-newsgroup-dependencies)))
6345         headers id end ref number
6346         (mail-parse-charset gnus-newsgroup-charset)
6347         (mail-parse-ignored-charsets
6348          (save-current-buffer (condition-case nil
6349                                   (set-buffer gnus-summary-buffer)
6350                                 (error))
6351                               gnus-newsgroup-ignored-charsets)))
6352     (with-current-buffer nntp-server-buffer
6353       ;; Translate all TAB characters into SPACE characters.
6354       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6355       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6356       (ietf-drums-unfold-fws)
6357       (gnus-run-hooks 'gnus-parse-headers-hook)
6358       (let ((case-fold-search t)
6359             in-reply-to header p lines chars)
6360         (goto-char (point-min))
6361         ;; Search to the beginning of the next header.  Error messages
6362         ;; do not begin with 2 or 3.
6363         (while (re-search-forward "^[23][0-9]+ " nil t)
6364           (setq id nil
6365                 ref nil)
6366           ;; This implementation of this function, with nine
6367           ;; search-forwards instead of the one re-search-forward and
6368           ;; a case (which basically was the old function) is actually
6369           ;; about twice as fast, even though it looks messier.  You
6370           ;; can't have everything, I guess.  Speed and elegance
6371           ;; doesn't always go hand in hand.
6372           (setq
6373            header
6374            (vector
6375             ;; Number.
6376             (prog1
6377                 (setq number (read cur))
6378               (end-of-line)
6379               (setq p (point))
6380               (narrow-to-region (point)
6381                                 (or (and (search-forward "\n.\n" nil t)
6382                                          (- (point) 2))
6383                                     (point))))
6384             ;; Subject.
6385             (progn
6386               (goto-char p)
6387               (if (search-forward "\nsubject:" nil t)
6388                   (funcall gnus-decode-encoded-word-function
6389                            (nnheader-header-value))
6390                 "(none)"))
6391             ;; From.
6392             (progn
6393               (goto-char p)
6394               (if (search-forward "\nfrom:" nil t)
6395                   (funcall gnus-decode-encoded-address-function
6396                            (nnheader-header-value))
6397                 "(nobody)"))
6398             ;; Date.
6399             (progn
6400               (goto-char p)
6401               (if (search-forward "\ndate:" nil t)
6402                   (nnheader-header-value) ""))
6403             ;; Message-ID.
6404             (progn
6405               (goto-char p)
6406               (setq id (if (re-search-forward
6407                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6408                            ;; We do it this way to make sure the Message-ID
6409                            ;; is (somewhat) syntactically valid.
6410                            (buffer-substring (match-beginning 1)
6411                                              (match-end 1))
6412                          ;; If there was no message-id, we just fake one
6413                          ;; to make subsequent routines simpler.
6414                          (nnheader-generate-fake-message-id number))))
6415             ;; References.
6416             (progn
6417               (goto-char p)
6418               (if (search-forward "\nreferences:" nil t)
6419                   (progn
6420                     (setq end (point))
6421                     (prog1
6422                         (nnheader-header-value)
6423                       (setq ref
6424                             (buffer-substring
6425                              (progn
6426                                (end-of-line)
6427                                (search-backward ">" end t)
6428                                (1+ (point)))
6429                              (progn
6430                                (search-backward "<" end t)
6431                                (point))))))
6432                 ;; Get the references from the in-reply-to header if there
6433                 ;; were no references and the in-reply-to header looks
6434                 ;; promising.
6435                 (if (and (search-forward "\nin-reply-to:" nil t)
6436                          (setq in-reply-to (nnheader-header-value))
6437                          (string-match "<[^>]+>" in-reply-to))
6438                     (let (ref2)
6439                       (setq ref (substring in-reply-to (match-beginning 0)
6440                                            (match-end 0)))
6441                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6442                         (setq ref2 (substring in-reply-to (match-beginning 0)
6443                                               (match-end 0)))
6444                         (when (> (length ref2) (length ref))
6445                           (setq ref ref2)))
6446                       ref)
6447                   (setq ref nil))))
6448             ;; Chars.
6449             (progn
6450               (goto-char p)
6451               (if (search-forward "\nchars: " nil t)
6452                   (if (numberp (setq chars (ignore-errors (read cur))))
6453                       chars -1)
6454                 -1))
6455             ;; Lines.
6456             (progn
6457               (goto-char p)
6458               (if (search-forward "\nlines: " nil t)
6459                   (if (numberp (setq lines (ignore-errors (read cur))))
6460                       lines -1)
6461                 -1))
6462             ;; Xref.
6463             (progn
6464               (goto-char p)
6465               (and (search-forward "\nxref:" nil t)
6466                    (nnheader-header-value)))
6467             ;; Extra.
6468             (when gnus-extra-headers
6469               (let ((extra gnus-extra-headers)
6470                     out)
6471                 (while extra
6472                   (goto-char p)
6473                   (when (search-forward
6474                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6475                     (push (cons (car extra) (nnheader-header-value))
6476                           out))
6477                   (pop extra))
6478                 out))))
6479           (when (equal id ref)
6480             (setq ref nil))
6481
6482           (when gnus-alter-header-function
6483             (funcall gnus-alter-header-function header)
6484             (setq id (mail-header-id header)
6485                   ref (gnus-parent-id (mail-header-references header))))
6486
6487           (when (setq header
6488                       (gnus-dependencies-add-header
6489                        header dependencies force-new))
6490             (push header headers))
6491           (goto-char (point-max))
6492           (widen))
6493         (nreverse headers)))))
6494
6495 ;; Goes through the xover lines and returns a list of vectors
6496 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6497                                                   force-new dependencies
6498                                                   group also-fetch-heads)
6499   "Parse the news overview data in the server buffer.
6500 Return a list of headers that match SEQUENCE (see
6501 `nntp-retrieve-headers')."
6502   ;; Get the Xref when the users reads the articles since most/some
6503   ;; NNTP servers do not include Xrefs when using XOVER.
6504   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6505   (let ((mail-parse-charset gnus-newsgroup-charset)
6506         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6507         (cur nntp-server-buffer)
6508         (dependencies (or dependencies gnus-newsgroup-dependencies))
6509         (allp (cond
6510                ((eq gnus-read-all-available-headers t)
6511                 t)
6512                ((and (stringp gnus-read-all-available-headers)
6513                      group)
6514                 (string-match gnus-read-all-available-headers group))
6515                (t
6516                 nil)))
6517         number headers header)
6518     (with-current-buffer nntp-server-buffer
6519       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6520       ;; Allow the user to mangle the headers before parsing them.
6521       (gnus-run-hooks 'gnus-parse-headers-hook)
6522       (goto-char (point-min))
6523       (gnus-parse-without-error
6524         (while (and (or sequence allp)
6525                     (not (eobp)))
6526           (setq number (read cur))
6527           (when (not allp)
6528             (while (and sequence
6529                         (< (car sequence) number))
6530               (setq sequence (cdr sequence))))
6531           (when (and (or allp
6532                          (and sequence
6533                               (eq number (car sequence))))
6534                      (progn
6535                        (setq sequence (cdr sequence))
6536                        (setq header (inline
6537                                       (gnus-nov-parse-line
6538                                        number dependencies force-new)))))
6539             (push header headers))
6540           (forward-line 1)))
6541       ;; A common bug in inn is that if you have posted an article and
6542       ;; then retrieves the active file, it will answer correctly --
6543       ;; the new article is included.  However, a NOV entry for the
6544       ;; article may not have been generated yet, so this may fail.
6545       ;; We work around this problem by retrieving the last few
6546       ;; headers using HEAD.
6547       (if (or (not also-fetch-heads)
6548               (not sequence))
6549           ;; We (probably) got all the headers.
6550           (nreverse headers)
6551         (let ((gnus-nov-is-evil t))
6552           (nconc
6553            (nreverse headers)
6554            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6555              (gnus-get-newsgroup-headers))))))))
6556
6557 (defun gnus-article-get-xrefs ()
6558   "Fill in the Xref value in `gnus-current-headers', if necessary.
6559 This is meant to be called in `gnus-article-internal-prepare-hook'."
6560   (let ((headers (with-current-buffer gnus-summary-buffer
6561                    gnus-current-headers)))
6562     (or (not gnus-use-cross-reference)
6563         (not headers)
6564         (and (mail-header-xref headers)
6565              (not (string= (mail-header-xref headers) "")))
6566         (let ((case-fold-search t)
6567               xref)
6568           (save-restriction
6569             (nnheader-narrow-to-headers)
6570             (goto-char (point-min))
6571             (when (or (and (not (eobp))
6572                            (eq (downcase (char-after)) ?x)
6573                            (looking-at "Xref:"))
6574                       (search-forward "\nXref:" nil t))
6575               (goto-char (1+ (match-end 0)))
6576               (setq xref (buffer-substring (point) (point-at-eol)))
6577               (mail-header-set-xref headers xref)))))))
6578
6579 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6580   "Find article ID and insert the summary line for that article.
6581 OLD-HEADER can either be a header or a line number to insert
6582 the subject line on.
6583 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6584 and OLD-HEADER will be used when the summary line is inserted,
6585 too, instead of trying to fetch new headers."
6586   (let* ((line (and (numberp old-header) old-header))
6587          (old-header (and (vectorp old-header) old-header))
6588          (header (cond ((and old-header use-old-header)
6589                         old-header)
6590                        ((and (numberp id)
6591                              (gnus-number-to-header id))
6592                         (gnus-number-to-header id))
6593                        (t
6594                         (gnus-read-header id))))
6595          (number (and (numberp id) id))
6596          d)
6597     (when header
6598       ;; Rebuild the thread that this article is part of and go to the
6599       ;; article we have fetched.
6600       (when (and (not gnus-show-threads)
6601                  old-header)
6602         (when (and number
6603                    (setq d (gnus-data-find (mail-header-number old-header))))
6604           (goto-char (gnus-data-pos d))
6605           (gnus-data-remove
6606            number
6607            (- (point-at-bol)
6608               (prog1
6609                   (1+ (point-at-eol))
6610                 (gnus-delete-line))))))
6611       ;; Remove list identifiers from subject.
6612       (let ((gnus-newsgroup-headers (list header)))
6613         (gnus-summary-remove-list-identifiers))
6614       (when old-header
6615         (mail-header-set-number header (mail-header-number old-header)))
6616       (setq gnus-newsgroup-sparse
6617             (delq (setq number (mail-header-number header))
6618                   gnus-newsgroup-sparse))
6619       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6620       (push number gnus-newsgroup-limit)
6621       (gnus-rebuild-thread (mail-header-id header) line)
6622       (gnus-summary-goto-subject number nil t))
6623     (when (and (numberp number)
6624                (> number 0))
6625       ;; We have to update the boundaries even if we can't fetch the
6626       ;; article if ID is a number -- so that the next `P' or `N'
6627       ;; command will fetch the previous (or next) article even
6628       ;; if the one we tried to fetch this time has been canceled.
6629       (when (> number gnus-newsgroup-end)
6630         (setq gnus-newsgroup-end number))
6631       (when (< number gnus-newsgroup-begin)
6632         (setq gnus-newsgroup-begin number))
6633       (setq gnus-newsgroup-unselected
6634             (delq number gnus-newsgroup-unselected)))
6635     ;; Report back a success?
6636     (and header (mail-header-number header))))
6637
6638 ;;; Process/prefix in the summary buffer
6639
6640 (defun gnus-summary-work-articles (n)
6641   "Return a list of articles to be worked upon.
6642 The prefix argument, the list of process marked articles, and the
6643 current article will be taken into consideration."
6644   (with-current-buffer gnus-summary-buffer
6645     (cond
6646      (n
6647       ;; A numerical prefix has been given.
6648       (setq n (prefix-numeric-value n))
6649       (let ((backward (< n 0))
6650             (n (abs (prefix-numeric-value n)))
6651             articles article)
6652         (save-excursion
6653           (while
6654               (and (> n 0)
6655                    (push (setq article (gnus-summary-article-number))
6656                          articles)
6657                    (if backward
6658                        (gnus-summary-find-prev nil article)
6659                      (gnus-summary-find-next nil article)))
6660             (decf n)))
6661         (nreverse articles)))
6662      ((and (gnus-region-active-p) (mark))
6663       (message "region active")
6664       ;; Work on the region between point and mark.
6665       (let ((max (max (point) (mark)))
6666             articles article)
6667         (save-excursion
6668           (goto-char (min (point) (mark)))
6669           (while
6670               (and
6671                (push (setq article (gnus-summary-article-number)) articles)
6672                (gnus-summary-find-next nil article)
6673                (< (point) max)))
6674           (nreverse articles))))
6675      (gnus-newsgroup-processable
6676       ;; There are process-marked articles present.
6677       ;; Save current state.
6678       (gnus-summary-save-process-mark)
6679       ;; Return the list.
6680       (reverse gnus-newsgroup-processable))
6681      (t
6682       ;; Just return the current article.
6683       (list (gnus-summary-article-number))))))
6684
6685 (defmacro gnus-summary-iterate (arg &rest forms)
6686   "Iterate over the process/prefixed articles and do FORMS.
6687 ARG is the interactive prefix given to the command.  FORMS will be
6688 executed with point over the summary line of the articles."
6689   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6690     `(let ((,articles (gnus-summary-work-articles ,arg)))
6691        (while ,articles
6692          (gnus-summary-goto-subject (car ,articles))
6693          ,@forms
6694          (pop ,articles)))))
6695
6696 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6697 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6698
6699 (defun gnus-summary-save-process-mark ()
6700   "Push the current set of process marked articles on the stack."
6701   (interactive)
6702   (push (copy-sequence gnus-newsgroup-processable)
6703         gnus-newsgroup-process-stack))
6704
6705 (defun gnus-summary-kill-process-mark ()
6706   "Push the current set of process marked articles on the stack and unmark."
6707   (interactive)
6708   (gnus-summary-save-process-mark)
6709   (gnus-summary-unmark-all-processable))
6710
6711 (defun gnus-summary-yank-process-mark ()
6712   "Pop the last process mark state off the stack and restore it."
6713   (interactive)
6714   (unless gnus-newsgroup-process-stack
6715     (error "Empty mark stack"))
6716   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6717
6718 (defun gnus-summary-process-mark-set (set)
6719   "Make SET into the current process marked articles."
6720   (gnus-summary-unmark-all-processable)
6721   (mapc 'gnus-summary-set-process-mark set))
6722
6723 ;;; Searching and stuff
6724
6725 (defun gnus-summary-search-group (&optional backward use-level)
6726   "Search for next unread newsgroup.
6727 If optional argument BACKWARD is non-nil, search backward instead."
6728   (with-current-buffer gnus-group-buffer
6729     (when (gnus-group-search-forward
6730            backward nil (if use-level (gnus-group-group-level) nil))
6731       (gnus-group-group-name))))
6732
6733 (defun gnus-summary-best-group (&optional exclude-group)
6734   "Find the name of the best unread group.
6735 If EXCLUDE-GROUP, do not go to this group."
6736   (with-current-buffer gnus-group-buffer
6737     (save-excursion
6738       (gnus-group-best-unread-group exclude-group))))
6739
6740 (defun gnus-summary-find-next (&optional unread article backward)
6741   (if backward
6742       (gnus-summary-find-prev unread article)
6743     (let* ((dummy (gnus-summary-article-intangible-p))
6744            (article (or article (gnus-summary-article-number)))
6745            (data (gnus-data-find-list article))
6746            result)
6747       (when (and (not dummy)
6748                  (or (not gnus-summary-check-current)
6749                      (not unread)
6750                      (not (gnus-data-unread-p (car data)))))
6751         (setq data (cdr data)))
6752       (when (setq result
6753                   (if unread
6754                       (progn
6755                         (while data
6756                           (unless (memq (gnus-data-number (car data))
6757                                         (cond
6758                                          ((eq gnus-auto-goto-ignores
6759                                               'always-undownloaded)
6760                                           gnus-newsgroup-undownloaded)
6761                                          (gnus-plugged
6762                                           nil)
6763                                          ((eq gnus-auto-goto-ignores
6764                                               'unfetched)
6765                                           gnus-newsgroup-unfetched)
6766                                          ((eq gnus-auto-goto-ignores
6767                                               'undownloaded)
6768                                           gnus-newsgroup-undownloaded)))
6769                             (when (gnus-data-unread-p (car data))
6770                               (setq result (car data)
6771                                     data nil)))
6772                           (setq data (cdr data)))
6773                         result)
6774                     (car data)))
6775         (goto-char (gnus-data-pos result))
6776         (gnus-data-number result)))))
6777
6778 (defun gnus-summary-find-prev (&optional unread article)
6779   (let* ((eobp (eobp))
6780          (article (or article (gnus-summary-article-number)))
6781          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6782          result)
6783     (when (and (not eobp)
6784                (or (not gnus-summary-check-current)
6785                    (not unread)
6786                    (not (gnus-data-unread-p (car data)))))
6787       (setq data (cdr data)))
6788     (when (setq result
6789                 (if unread
6790                     (progn
6791                       (while data
6792                         (unless (memq (gnus-data-number (car data))
6793                                       (cond
6794                                        ((eq gnus-auto-goto-ignores
6795                                             'always-undownloaded)
6796                                         gnus-newsgroup-undownloaded)
6797                                        (gnus-plugged
6798                                         nil)
6799                                        ((eq gnus-auto-goto-ignores
6800                                             'unfetched)
6801                                         gnus-newsgroup-unfetched)
6802                                        ((eq gnus-auto-goto-ignores
6803                                             'undownloaded)
6804                                         gnus-newsgroup-undownloaded)))
6805                           (when (gnus-data-unread-p (car data))
6806                             (setq result (car data)
6807                                   data nil)))
6808                         (setq data (cdr data)))
6809                       result)
6810                   (car data)))
6811       (goto-char (gnus-data-pos result))
6812       (gnus-data-number result))))
6813
6814 (defun gnus-summary-find-subject (subject &optional unread backward article)
6815   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6816          (article (or article (gnus-summary-article-number)))
6817          (articles (gnus-data-list backward))
6818          (arts (gnus-data-find-list article articles))
6819          result)
6820     (when (or (not gnus-summary-check-current)
6821               (not unread)
6822               (not (gnus-data-unread-p (car arts))))
6823       (setq arts (cdr arts)))
6824     (while arts
6825       (and (or (not unread)
6826                (gnus-data-unread-p (car arts)))
6827            (vectorp (gnus-data-header (car arts)))
6828            (gnus-subject-equal
6829             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6830            (setq result (car arts)
6831                  arts nil))
6832       (setq arts (cdr arts)))
6833     (and result
6834          (goto-char (gnus-data-pos result))
6835          (gnus-data-number result))))
6836
6837 (defun gnus-summary-search-forward (&optional unread subject backward)
6838   "Search forward for an article.
6839 If UNREAD, look for unread articles.  If SUBJECT, look for
6840 articles with that subject.  If BACKWARD, search backward instead."
6841   (cond (subject (gnus-summary-find-subject subject unread backward))
6842         (backward (gnus-summary-find-prev unread))
6843         (t (gnus-summary-find-next unread))))
6844
6845 (defun gnus-recenter (&optional n)
6846   "Center point in window and redisplay frame.
6847 Also do horizontal recentering."
6848   (interactive "P")
6849   (when (and gnus-auto-center-summary
6850              (not (eq gnus-auto-center-summary 'vertical)))
6851     (gnus-horizontal-recenter))
6852   (if (fboundp 'recenter-top-bottom)
6853       (recenter-top-bottom n)
6854     (recenter n)))
6855
6856 (put 'gnus-recenter 'isearch-scroll t)
6857
6858 (defun gnus-forward-line-ignore-invisible (n)
6859   "Move N lines forward (backward if N is negative).
6860 Like forward-line, but skip over (and don't count) invisible lines."
6861   (let (done)
6862     (while (and (> n 0) (not done))
6863       ;; If the following character is currently invisible,
6864       ;; skip all characters with that same `invisible' property value.
6865       (while (gnus-invisible-p (point))
6866         (goto-char (gnus-next-char-property-change (point))))
6867       (forward-line 1)
6868       (if (eobp)
6869           (setq done t)
6870         (setq n (1- n))))
6871     (while (and (< n 0) (not done))
6872       (forward-line -1)
6873       (if (bobp) (setq done t)
6874         (setq n (1+ n))
6875         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6876           (goto-char (gnus-previous-char-property-change (point))))))))
6877
6878 (defun gnus-summary-recenter ()
6879   "Center point in the summary window.
6880 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6881 displayed, no centering will be performed."
6882   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6883   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6884   (interactive)
6885   ;; The user has to want it.
6886   (when gnus-auto-center-summary
6887     (let* ((top (cond ((< (window-height) 4) 0)
6888                       ((< (window-height) 7) 1)
6889                       (t (if (numberp gnus-auto-center-summary)
6890                              gnus-auto-center-summary
6891                            (/ (1- (window-height)) 2)))))
6892            (height (1- (window-height)))
6893            (bottom (save-excursion
6894                      (goto-char (point-max))
6895                      (gnus-forward-line-ignore-invisible (- height))
6896                      (point)))
6897            (window (get-buffer-window (current-buffer))))
6898       (when (get-buffer-window gnus-article-buffer)
6899         ;; Only do recentering when the article buffer is displayed,
6900         ;; Set the window start to either `bottom', which is the biggest
6901         ;; possible valid number, or the second line from the top,
6902         ;; whichever is the least.
6903         (let ((top-pos (save-excursion
6904                          (gnus-forward-line-ignore-invisible (- top))
6905                          (point))))
6906           (if (> bottom top-pos)
6907               ;; Keep the second line from the top visible
6908               (set-window-start window top-pos)
6909             ;; Try to keep the bottom line visible; if it's partially
6910             ;; obscured, either scroll one more line to make it fully
6911             ;; visible, or revert to using TOP-POS.
6912             (save-excursion
6913               (goto-char (point-max))
6914               (gnus-forward-line-ignore-invisible -1)
6915               (let ((last-line-start (point)))
6916                 (goto-char bottom)
6917                 (set-window-start window (point) t)
6918                 (when (not (pos-visible-in-window-p last-line-start window))
6919                   (gnus-forward-line-ignore-invisible 1)
6920                   (set-window-start window (min (point) top-pos) t)))))))
6921       ;; Do horizontal recentering while we're at it.
6922       (when (and (get-buffer-window (current-buffer) t)
6923                  (not (eq gnus-auto-center-summary 'vertical)))
6924         (let ((selected (selected-window)))
6925           (select-window (get-buffer-window (current-buffer) t))
6926           (gnus-summary-position-point)
6927           (gnus-horizontal-recenter)
6928           (select-window selected))))))
6929
6930 (defun gnus-summary-jump-to-group (newsgroup)
6931   "Move point to NEWSGROUP in group mode buffer."
6932   ;; Keep update point of group mode buffer if visible.
6933   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6934       (save-window-excursion
6935         ;; Take care of tree window mode.
6936         (when (get-buffer-window gnus-group-buffer)
6937           (pop-to-buffer gnus-group-buffer))
6938         (gnus-group-jump-to-group newsgroup))
6939     (save-excursion
6940       ;; Take care of tree window mode.
6941       (if (get-buffer-window gnus-group-buffer 0)
6942           (pop-to-buffer gnus-group-buffer)
6943         (set-buffer gnus-group-buffer))
6944       (gnus-group-jump-to-group newsgroup))))
6945
6946 ;; This function returns a list of article numbers based on the
6947 ;; difference between the ranges of read articles in this group and
6948 ;; the range of active articles.
6949 (defun gnus-list-of-unread-articles (group)
6950   (let* ((read (gnus-info-read (gnus-get-info group)))
6951          (active (or (gnus-active group) (gnus-activate-group group)))
6952          (last (or (cdr active)
6953                    (error "Group %s couldn't be activated " group)))
6954          (bottom (if gnus-newsgroup-maximum-articles
6955                      (max (car active)
6956                           (- last gnus-newsgroup-maximum-articles -1))
6957                    (car active)))
6958          first nlast unread)
6959     ;; If none are read, then all are unread.
6960     (if (not read)
6961         (setq first bottom)
6962       ;; If the range of read articles is a single range, then the
6963       ;; first unread article is the article after the last read
6964       ;; article.  Sounds logical, doesn't it?
6965       (if (and (not (listp (cdr read)))
6966                (or (< (car read) bottom)
6967                    (progn (setq read (list read))
6968                           nil)))
6969           (setq first (max bottom (1+ (cdr read))))
6970         ;; `read' is a list of ranges.
6971         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6972                                   (caar read)))
6973                   1)
6974           (setq first bottom))
6975         (while read
6976           (when first
6977             (while (< first nlast)
6978               (setq unread (cons first unread)
6979                     first (1+ first))))
6980           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6981           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6982           (setq read (cdr read)))))
6983     ;; And add the last unread articles.
6984     (while (<= first last)
6985       (setq unread (cons first unread)
6986             first (1+ first)))
6987     ;; Return the list of unread articles.
6988     (delq 0 (nreverse unread))))
6989
6990 (defun gnus-list-of-read-articles (group)
6991   "Return a list of unread, unticked and non-dormant articles."
6992   (let* ((info (gnus-get-info group))
6993          (marked (gnus-info-marks info))
6994          (active (gnus-active group)))
6995     (and info active
6996          (gnus-list-range-difference
6997           (gnus-list-range-difference
6998            (gnus-sorted-complement
6999             (gnus-uncompress-range
7000              (if gnus-newsgroup-maximum-articles
7001                  (cons (max (car active)
7002                             (- (cdr active)
7003                                gnus-newsgroup-maximum-articles
7004                                -1))
7005                        (cdr active))
7006                active))
7007             (gnus-list-of-unread-articles group))
7008            (cdr (assq 'dormant marked)))
7009           (cdr (assq 'tick marked))))))
7010
7011 ;; This function returns a sequence of article numbers based on the
7012 ;; difference between the ranges of read articles in this group and
7013 ;; the range of active articles.
7014 (defun gnus-sequence-of-unread-articles (group)
7015   (let* ((read (gnus-info-read (gnus-get-info group)))
7016          (active (or (gnus-active group) (gnus-activate-group group)))
7017          (last (cdr active))
7018          (bottom (if gnus-newsgroup-maximum-articles
7019                      (max (car active)
7020                           (- last gnus-newsgroup-maximum-articles -1))
7021                    (car active)))
7022          first nlast unread)
7023     ;; If none are read, then all are unread.
7024     (if (not read)
7025         (setq first bottom)
7026       ;; If the range of read articles is a single range, then the
7027       ;; first unread article is the article after the last read
7028       ;; article.  Sounds logical, doesn't it?
7029       (if (and (not (listp (cdr read)))
7030                (or (< (car read) bottom)
7031                    (progn (setq read (list read))
7032                           nil)))
7033           (setq first (max bottom (1+ (cdr read))))
7034         ;; `read' is a list of ranges.
7035         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7036                                   (caar read)))
7037                   1)
7038           (setq first bottom))
7039         (while read
7040           (when first
7041             (push (cons first nlast) unread))
7042           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7043           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7044           (setq read (cdr read)))))
7045     ;; And add the last unread articles.
7046     (cond ((not (and first last))
7047            nil)
7048           ((< first last)
7049            (push (cons first last) unread))
7050           ((= first last)
7051            (push first unread)))
7052     ;; Return the sequence of unread articles.
7053     (delq 0 (nreverse unread))))
7054
7055 ;; Various summary commands
7056
7057 (defun gnus-summary-select-article-buffer ()
7058   "Reconfigure windows to show the article buffer.
7059 If `gnus-widen-article-window' is set, show only the article
7060 buffer."
7061   (interactive)
7062   (if (not (gnus-buffer-live-p gnus-article-buffer))
7063       (error "There is no article buffer for this summary buffer")
7064     (unless (get-buffer-window gnus-article-buffer)
7065       (gnus-summary-show-article))
7066     (gnus-configure-windows
7067      (if gnus-widen-article-window
7068          'only-article
7069        'article)
7070      t)
7071     (select-window (get-buffer-window gnus-article-buffer))))
7072
7073 (defun gnus-summary-universal-argument (arg)
7074   "Perform any operation on all articles that are process/prefixed."
7075   (interactive "P")
7076   (let ((articles (gnus-summary-work-articles arg))
7077         func article)
7078     (if (eq
7079          (setq
7080           func
7081           (key-binding
7082            (read-key-sequence
7083             (substitute-command-keys
7084              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7085          'undefined)
7086         (gnus-error 1 "Undefined key")
7087       (save-excursion
7088         (while articles
7089           (gnus-summary-goto-subject (setq article (pop articles)))
7090           (let (gnus-newsgroup-processable)
7091             (command-execute func))
7092           (gnus-summary-remove-process-mark article)))))
7093   (gnus-summary-position-point))
7094
7095 (defun gnus-summary-toggle-truncation (&optional arg)
7096   "Toggle truncation of summary lines.
7097 With ARG, turn line truncation on if ARG is positive."
7098   (interactive "P")
7099   (setq truncate-lines
7100         (if (null arg) (not truncate-lines)
7101           (> (prefix-numeric-value arg) 0)))
7102   (redraw-display))
7103
7104 (defun gnus-summary-find-for-reselect ()
7105   "Return the number of an article to stay on across a reselect.
7106 The current article is considered, then following articles, then previous
7107 articles.  An article is sought which is not canceled and isn't a temporary
7108 insertion from another group.  If there's no such then return a dummy 0."
7109   (let (found)
7110     (dolist (rev '(nil t))
7111       (unless found      ; don't demand the reverse list if we don't need it
7112         (let ((data (gnus-data-find-list
7113                      (gnus-summary-article-number) (gnus-data-list rev))))
7114           (while (and data (not found))
7115             (if (and (< 0 (gnus-data-number (car data)))
7116                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7117                 (setq found (gnus-data-number (car data))))
7118             (setq data (cdr data))))))
7119     (or found 0)))
7120
7121 (defun gnus-summary-reselect-current-group (&optional all rescan)
7122   "Exit and then reselect the current newsgroup.
7123 The prefix argument ALL means to select all articles."
7124   (interactive "P")
7125   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7126     (error "Ephemeral groups can't be reselected"))
7127   (let ((current-subject (gnus-summary-find-for-reselect))
7128         (group gnus-newsgroup-name))
7129     (setq gnus-newsgroup-begin nil)
7130     (gnus-summary-exit nil 'leave-hidden)
7131     ;; We have to adjust the point of group mode buffer because
7132     ;; point was moved to the next unread newsgroup by exiting.
7133     (gnus-summary-jump-to-group group)
7134     (when rescan
7135       (save-excursion
7136         (gnus-group-get-new-news-this-group 1)))
7137     (gnus-group-read-group all t)
7138     (gnus-summary-goto-subject current-subject nil t)))
7139
7140 (defun gnus-summary-rescan-group (&optional all)
7141   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7142   (interactive "P")
7143   (let ((config gnus-current-window-configuration))
7144     (gnus-summary-reselect-current-group all t)
7145     (gnus-configure-windows config)
7146     (when (eq config 'article)
7147       (gnus-summary-select-article))))
7148
7149 (defun gnus-summary-update-info (&optional non-destructive)
7150   (save-excursion
7151     (let ((group gnus-newsgroup-name))
7152       (when group
7153         (when gnus-newsgroup-kill-headers
7154           (setq gnus-newsgroup-killed
7155                 (gnus-compress-sequence
7156                  (gnus-sorted-union
7157                   (gnus-list-range-intersection
7158                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7159                   gnus-newsgroup-unreads)
7160                  t)))
7161         (unless (listp (cdr gnus-newsgroup-killed))
7162           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7163         (let ((headers gnus-newsgroup-headers)
7164               (ephemeral-p (gnus-ephemeral-group-p group))
7165               info)
7166           (unless ephemeral-p
7167             (setq info (copy-sequence (gnus-get-info group))
7168                   info (delq (gnus-info-params info) info)))
7169           ;; Set the new ranges of read articles.
7170           (with-current-buffer gnus-group-buffer
7171             (gnus-undo-force-boundary))
7172           (gnus-update-read-articles
7173            group (gnus-sorted-union
7174                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7175           ;; Set the current article marks.
7176           (let ((gnus-newsgroup-scored
7177                  (if (and (not gnus-save-score)
7178                           (not non-destructive))
7179                      nil
7180                    gnus-newsgroup-scored)))
7181             (save-excursion
7182               (gnus-update-marks)))
7183           ;; Do the cross-ref thing.
7184           (when gnus-use-cross-reference
7185             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7186           ;; Do not switch windows but change the buffer to work.
7187           (set-buffer gnus-group-buffer)
7188           (unless ephemeral-p
7189             (gnus-group-update-group
7190              group nil
7191              (equal info
7192                     (setq info (copy-sequence (gnus-get-info group))
7193                           info (delq (gnus-info-params info) info))))))))))
7194
7195 (defun gnus-summary-save-newsrc (&optional force)
7196   "Save the current number of read/marked articles in the dribble buffer.
7197 The dribble buffer will then be saved.
7198 If FORCE (the prefix), also save the .newsrc file(s)."
7199   (interactive "P")
7200   (gnus-summary-update-info t)
7201   (if force
7202       (gnus-save-newsrc-file)
7203     (gnus-dribble-save)))
7204
7205 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7206
7207 (defun gnus-summary-exit (&optional temporary leave-hidden)
7208   "Exit reading current newsgroup, and then return to group selection mode.
7209 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7210   (interactive)
7211   (gnus-set-global-variables)
7212   (when (gnus-buffer-live-p gnus-article-buffer)
7213     (with-current-buffer gnus-article-buffer
7214       (mm-destroy-parts gnus-article-mime-handles)
7215       ;; Set it to nil for safety reason.
7216       (setq gnus-article-mime-handle-alist nil)
7217       (setq gnus-article-mime-handles nil)))
7218   (gnus-kill-save-kill-buffer)
7219   (gnus-async-halt-prefetch)
7220   (let* ((group gnus-newsgroup-name)
7221          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7222          (gnus-group-is-exiting-p t)
7223          (article-buffer gnus-article-buffer)
7224          (original-article-buffer gnus-original-article-buffer)
7225          (mode major-mode)
7226          (group-point nil)
7227          (buf (current-buffer))
7228          ;; `gnus-single-article-buffer' is nil buffer-locally in
7229          ;; ephemeral group of which summary buffer will be killed,
7230          ;; but the global value may be non-nil.
7231          (single-article-buffer gnus-single-article-buffer))
7232     (unless quit-config
7233       ;; Do adaptive scoring, and possibly save score files.
7234       (when gnus-newsgroup-adaptive
7235         (gnus-score-adaptive))
7236       (when gnus-use-scoring
7237         (gnus-score-save)))
7238     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7239     (when gnus-use-cache
7240       (gnus-cache-possibly-remove-articles)
7241       (gnus-cache-save-buffers))
7242     (gnus-async-prefetch-remove-group group)
7243     (when gnus-suppress-duplicates
7244       (gnus-dup-enter-articles))
7245     (when gnus-use-trees
7246       (gnus-tree-close group))
7247     (when gnus-use-cache
7248       (gnus-cache-write-active))
7249     ;; Remove entries for this group.
7250     (nnmail-purge-split-history (gnus-group-real-name group))
7251     ;; Make all changes in this group permanent.
7252     (unless quit-config
7253       (gnus-run-hooks 'gnus-exit-group-hook)
7254       (gnus-summary-update-info))
7255     (gnus-close-group group)
7256     ;; Make sure where we were, and go to next newsgroup.
7257     (set-buffer gnus-group-buffer)
7258     (unless quit-config
7259       (gnus-group-jump-to-group group))
7260     (gnus-run-hooks 'gnus-summary-exit-hook)
7261     (unless (or quit-config
7262                 (not gnus-summary-next-group-on-exit)
7263                 ;; If this group has disappeared from the summary
7264                 ;; buffer, don't skip forwards.
7265                 (not (string= group (gnus-group-group-name))))
7266       (gnus-group-next-unread-group 1))
7267     (setq group-point (point))
7268     (if temporary
7269         nil                             ;Nothing to do.
7270       (set-buffer buf)
7271       (if (not gnus-kill-summary-on-exit)
7272           (progn
7273             (gnus-deaden-summary)
7274             (setq mode nil))
7275         (when (get-buffer gnus-article-buffer)
7276           (bury-buffer gnus-article-buffer))
7277         ;; Return to group mode buffer.
7278         (when (eq mode 'gnus-summary-mode)
7279           (gnus-kill-buffer buf)))
7280
7281       (setq gnus-current-select-method gnus-select-method)
7282       (set-buffer gnus-group-buffer)
7283       (if quit-config
7284           (gnus-handle-ephemeral-exit quit-config)
7285         (goto-char group-point)
7286         ;; If gnus-group-buffer is already displayed, make sure we also move
7287         ;; the cursor in the window that displays it.
7288         (let ((win (get-buffer-window (current-buffer) 0)))
7289           (if win (set-window-point win (point))))
7290         (unless leave-hidden
7291           (gnus-configure-windows 'group 'force)))
7292
7293       ;; If we have several article buffers, we kill them at exit.
7294       (unless single-article-buffer
7295         (when (gnus-buffer-live-p article-buffer)
7296           (with-current-buffer article-buffer
7297             ;; Don't kill sticky article buffers
7298             (unless (eq major-mode 'gnus-sticky-article-mode)
7299               (gnus-kill-buffer article-buffer)
7300               (setq gnus-article-current nil))))
7301         (gnus-kill-buffer original-article-buffer))
7302
7303       ;; Clear the current group name.
7304       (unless quit-config
7305         (setq gnus-newsgroup-name nil)))))
7306
7307 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7308 (defun gnus-summary-exit-no-update (&optional no-questions)
7309   "Quit reading current newsgroup without updating read article info."
7310   (interactive)
7311   (let* ((group gnus-newsgroup-name)
7312          (gnus-group-is-exiting-p t)
7313          (gnus-group-is-exiting-without-update-p t)
7314          (quit-config (gnus-group-quit-config group)))
7315     (when (or no-questions
7316               gnus-expert-user
7317               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7318       (gnus-async-halt-prefetch)
7319       (run-hooks 'gnus-summary-prepare-exit-hook)
7320       (when (gnus-buffer-live-p gnus-article-buffer)
7321         (with-current-buffer gnus-article-buffer
7322           (gnus-article-stop-animations)
7323           (gnus-stop-downloads)
7324           (mm-destroy-parts gnus-article-mime-handles)
7325           ;; Set it to nil for safety reason.
7326           (setq gnus-article-mime-handle-alist nil)
7327           (setq gnus-article-mime-handles nil)))
7328       ;; If we have several article buffers, we kill them at exit.
7329       (unless gnus-single-article-buffer
7330         (gnus-kill-buffer gnus-article-buffer)
7331         (gnus-kill-buffer gnus-original-article-buffer)
7332         (setq gnus-article-current nil))
7333       ;; Return to the group buffer.
7334       (if (not gnus-kill-summary-on-exit)
7335           (progn
7336             (gnus-deaden-summary)
7337             (gnus-configure-windows 'group 'force))
7338         (gnus-configure-windows 'group 'force)
7339         (gnus-close-group group)
7340         (gnus-kill-buffer gnus-summary-buffer))
7341       (unless gnus-single-article-buffer
7342         (setq gnus-article-current nil))
7343       (when gnus-use-trees
7344         (gnus-tree-close group))
7345       (gnus-async-prefetch-remove-group group)
7346       (when (get-buffer gnus-article-buffer)
7347         (bury-buffer gnus-article-buffer))
7348       ;; Clear the current group name.
7349       (setq gnus-newsgroup-name nil)
7350       (unless (gnus-ephemeral-group-p group)
7351         (gnus-group-update-group group nil t))
7352       (when (equal (gnus-group-group-name) group)
7353         (gnus-group-next-unread-group 1))
7354       (when quit-config
7355         (gnus-handle-ephemeral-exit quit-config)))))
7356
7357 (defun gnus-handle-ephemeral-exit (quit-config)
7358   "Handle movement when leaving an ephemeral group.
7359 The state which existed when entering the ephemeral is reset."
7360   (if (not (buffer-live-p (car quit-config)))
7361       (gnus-configure-windows 'group 'force)
7362     (set-buffer (car quit-config))
7363     (unless (eq (cdr quit-config) 'group)
7364       (setq gnus-current-select-method
7365             (gnus-find-method-for-group gnus-newsgroup-name)))
7366     (cond ((eq major-mode 'gnus-summary-mode)
7367            (gnus-set-global-variables))
7368           ((eq major-mode 'gnus-article-mode)
7369            (save-current-buffer
7370              ;; The `gnus-summary-buffer' variable may point
7371              ;; to the old summary buffer when using a single
7372              ;; article buffer.
7373              (unless (gnus-buffer-live-p gnus-summary-buffer)
7374                (set-buffer gnus-group-buffer))
7375              (set-buffer gnus-summary-buffer)
7376              (gnus-set-global-variables))))
7377     (if (or (eq (cdr quit-config) 'article)
7378             (eq (cdr quit-config) 'pick))
7379         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7380             (gnus-configure-windows 'pick 'force)
7381           (gnus-configure-windows (cdr quit-config) 'force))
7382       (gnus-configure-windows (cdr quit-config) 'force))
7383     (when (eq major-mode 'gnus-summary-mode)
7384       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7385                                                      next-unread-noselect))
7386           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7387                                   'next-noselect)
7388                               (gnus-summary-next-subject 1 nil t))
7389                              ((eq gnus-auto-select-on-ephemeral-exit
7390                                   'next-unread-noselect)
7391                               (gnus-summary-next-subject 1 t t))))
7392             ;; Hide the article buffer which displays the article different
7393             ;; from the one that the cursor points to in the summary buffer.
7394             (gnus-configure-windows 'summary 'force))
7395         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7396                (gnus-summary-next-subject 1))
7397               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7398                (gnus-summary-next-subject 1 t))))
7399       (gnus-summary-recenter)
7400       (gnus-summary-position-point))))
7401
7402 ;;; Dead summaries.
7403
7404 (defvar gnus-dead-summary-mode-map
7405   (let ((map (make-keymap)))
7406     (suppress-keymap map)
7407     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7408     (dolist (key '("\C-d" "\r" "\177" [delete]))
7409       (define-key map key 'gnus-summary-wake-up-the-dead))
7410     (dolist (key '("q" "Q"))
7411       (define-key map key 'bury-buffer))
7412     map))
7413
7414 (define-minor-mode gnus-dead-summary-mode
7415   "Minor mode for Gnus summary buffers."
7416   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7417   (unless (derived-mode-p 'gnus-summary-mode)
7418     (setq gnus-dead-summary-mode nil)))
7419
7420 (defun gnus-deaden-summary ()
7421   "Make the current summary buffer into a dead summary buffer."
7422   ;; Kill any previous dead summary buffer.
7423   (when (and gnus-dead-summary
7424              (buffer-name gnus-dead-summary))
7425     (with-current-buffer gnus-dead-summary
7426       (when gnus-dead-summary-mode
7427         (kill-buffer (current-buffer)))))
7428   ;; Make this the current dead summary.
7429   (setq gnus-dead-summary (current-buffer))
7430   (gnus-dead-summary-mode 1)
7431   (let ((name (buffer-name)))
7432     (when (string-match "Summary" name)
7433       (rename-buffer
7434        (concat (substring name 0 (match-beginning 0)) "Dead "
7435                (substring name (match-beginning 0)))
7436        t)
7437       (bury-buffer))))
7438
7439 (defun gnus-kill-or-deaden-summary (buffer)
7440   "Kill or deaden the summary BUFFER."
7441   (save-excursion
7442     (when (and (buffer-name buffer)
7443                (not gnus-single-article-buffer))
7444       (with-current-buffer buffer
7445         (gnus-kill-buffer gnus-article-buffer)
7446         (gnus-kill-buffer gnus-original-article-buffer)))
7447     (cond
7448      ;; Kill the buffer.
7449      (gnus-kill-summary-on-exit
7450       (when (and gnus-use-trees
7451                  (gnus-buffer-exists-p buffer))
7452         (with-current-buffer buffer
7453           (gnus-tree-close gnus-newsgroup-name)))
7454       (gnus-kill-buffer buffer))
7455      ;; Deaden the buffer.
7456      ((gnus-buffer-exists-p buffer)
7457       (with-current-buffer buffer
7458         (gnus-deaden-summary))))))
7459
7460 (defun gnus-summary-wake-up-the-dead (&rest args)
7461   "Wake up the dead summary buffer."
7462   (interactive)
7463   (gnus-dead-summary-mode -1)
7464   (let ((name (buffer-name)))
7465     (when (string-match "Dead " name)
7466       (rename-buffer
7467        (concat (substring name 0 (match-beginning 0))
7468                (substring name (match-end 0)))
7469        t)))
7470   (gnus-message 3 "This dead summary is now alive again"))
7471
7472 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7473 (defun gnus-summary-describe-group (&optional force)
7474   "Describe the current newsgroup."
7475   (interactive "P")
7476   (gnus-group-describe-group force gnus-newsgroup-name))
7477
7478 (defun gnus-summary-describe-briefly ()
7479   "Describe summary mode commands briefly."
7480   (interactive)
7481   (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")))
7482
7483 ;; Walking around group mode buffer from summary mode.
7484
7485 (defun gnus-summary-next-group (&optional no-article target-group backward)
7486   "Exit current newsgroup and then select next unread newsgroup.
7487 If prefix argument NO-ARTICLE is non-nil, no article is selected
7488 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7489 previous group instead."
7490   (interactive "P")
7491   ;; Stop pre-fetching.
7492   (gnus-async-halt-prefetch)
7493   (let ((current-group gnus-newsgroup-name)
7494         (current-buffer (current-buffer))
7495         entered)
7496     ;; First we semi-exit this group to update Xrefs and all variables.
7497     ;; We can't do a real exit, because the window conf must remain
7498     ;; the same in case the user is prompted for info, and we don't
7499     ;; want the window conf to change before that...
7500     (gnus-summary-exit t)
7501     (while (not entered)
7502       ;; Then we find what group we are supposed to enter.
7503       (set-buffer gnus-group-buffer)
7504       (gnus-group-jump-to-group current-group)
7505       (setq target-group
7506             (or target-group
7507                 (if (eq gnus-keep-same-level 'best)
7508                     (gnus-summary-best-group gnus-newsgroup-name)
7509                   (gnus-summary-search-group backward gnus-keep-same-level))))
7510       (if (not target-group)
7511           ;; There are no further groups, so we return to the group
7512           ;; buffer.
7513           (progn
7514             (gnus-message 5 "Returning to the group buffer")
7515             (setq entered t)
7516             (when (gnus-buffer-live-p current-buffer)
7517               (set-buffer current-buffer)
7518               (gnus-summary-exit))
7519             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7520         ;; We try to enter the target group.
7521         (gnus-group-jump-to-group target-group)
7522         (let ((unreads (gnus-group-group-unread)))
7523           (if (and (or (eq t unreads)
7524                        (and unreads (not (zerop unreads))))
7525                    (gnus-summary-read-group
7526                     target-group nil no-article
7527                     (and (buffer-name current-buffer) current-buffer)
7528                     nil backward))
7529               (setq entered t)
7530             (setq current-group target-group
7531                   target-group nil)))))))
7532
7533 (defun gnus-summary-prev-group (&optional no-article)
7534   "Exit current newsgroup and then select previous unread newsgroup.
7535 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7536   (interactive "P")
7537   (gnus-summary-next-group no-article nil t))
7538
7539 ;; Walking around summary lines.
7540
7541 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7542   "Go to the first subject satisfying any non-nil constraint.
7543 If UNREAD is non-nil, the article should be unread.
7544 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7545 If UNSEEN is non-nil, the article should be unseen as well as unread.
7546 Returns the article selected or nil if there are no matching articles."
7547   (interactive "P")
7548   (cond
7549    ;; Empty summary.
7550    ((null gnus-newsgroup-data)
7551     (gnus-message 3 "No articles in the group")
7552     nil)
7553    ;; Pick the first article.
7554    ((not (or unread undownloaded unseen))
7555     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7556     (gnus-data-number (car gnus-newsgroup-data)))
7557    ;; Find the first unread article.
7558    (t
7559     (let ((data gnus-newsgroup-data))
7560       (while (and data
7561                   (let ((num (gnus-data-number (car data))))
7562                     (or (memq num gnus-newsgroup-unfetched)
7563                         (not (or (and unread
7564                                       (memq num gnus-newsgroup-unreads))
7565                                  (and undownloaded
7566                                       (memq num gnus-newsgroup-undownloaded))
7567                                  (and unseen
7568                                       (memq num gnus-newsgroup-unseen)
7569                                       (memq num gnus-newsgroup-unreads)))))))
7570         (setq data (cdr data)))
7571       (prog1
7572           (if data
7573               (progn
7574                 (goto-char (gnus-data-pos (car data)))
7575                 (gnus-data-number (car data)))
7576             (gnus-message 3 "No more%s articles"
7577                           (let* ((r (when unread " unread"))
7578                                  (d (when undownloaded " undownloaded"))
7579                                  (s (when unseen " unseen"))
7580                                  (l (delq nil (list r d s))))
7581                             (cond ((= 3 (length l))
7582                                    (concat r "," d ", or" s))
7583                                   ((= 2 (length l))
7584                                    (concat (car l) ", or" (cadr l)))
7585                                   ((= 1 (length l))
7586                                    (car l))
7587                                   (t
7588                                    ""))))
7589             nil
7590             )
7591         (gnus-summary-position-point))))))
7592
7593 (defun gnus-summary-next-subject (n &optional unread dont-display)
7594   "Go to next N'th summary line.
7595 If N is negative, go to the previous N'th subject line.
7596 If UNREAD is non-nil, only unread articles are selected.
7597 The difference between N and the actual number of steps taken is
7598 returned."
7599   (interactive "p")
7600   (let ((backward (< n 0))
7601         (n (abs n)))
7602     (while (and (> n 0)
7603                 (if backward
7604                     (gnus-summary-find-prev unread)
7605                   (gnus-summary-find-next unread)))
7606       (unless (zerop (setq n (1- n)))
7607         (gnus-summary-show-thread)))
7608     (when (/= 0 n)
7609       (gnus-message 7 "No more%s articles"
7610                     (if unread " unread" "")))
7611     (unless dont-display
7612       (gnus-summary-recenter)
7613       (gnus-summary-position-point))
7614     n))
7615
7616 (defun gnus-summary-next-unread-subject (n)
7617   "Go to next N'th unread summary line."
7618   (interactive "p")
7619   (gnus-summary-next-subject n t))
7620
7621 (defun gnus-summary-prev-subject (n &optional unread)
7622   "Go to previous N'th summary line.
7623 If optional argument UNREAD is non-nil, only unread article is selected."
7624   (interactive "p")
7625   (gnus-summary-next-subject (- n) unread))
7626
7627 (defun gnus-summary-prev-unread-subject (n)
7628   "Go to previous N'th unread summary line."
7629   (interactive "p")
7630   (gnus-summary-next-subject (- n) t))
7631
7632 (defun gnus-summary-goto-subjects (articles)
7633   "Insert the subject header for ARTICLES in the current buffer."
7634   (save-excursion
7635     (dolist (article articles)
7636       (gnus-summary-goto-subject article t)))
7637   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7638   (gnus-summary-position-point))
7639
7640 (defun gnus-summary-goto-subject (article &optional force silent)
7641   "Go to the subject line of ARTICLE.
7642 If FORCE, also allow jumping to articles not currently shown."
7643   (interactive "nArticle number: ")
7644   (unless (numberp article)
7645     (error "Article %s is not a number" article))
7646   (let ((b (point))
7647         (data (gnus-data-find article)))
7648     ;; We read in the article if we have to.
7649     (and (not data)
7650          force
7651          (gnus-summary-insert-subject
7652           article
7653           (if (or (numberp force) (vectorp force)) force)
7654           t)
7655          (setq data (gnus-data-find article)))
7656     (goto-char b)
7657     (if (not data)
7658         (progn
7659           (unless silent
7660             (gnus-message 3 "Can't find article %d" article))
7661           nil)
7662       (let ((pt (gnus-data-pos data)))
7663         (goto-char pt)
7664         (gnus-summary-set-article-display-arrow pt))
7665       (gnus-summary-position-point)
7666       article)))
7667
7668 ;; Walking around summary lines with displaying articles.
7669
7670 (defun gnus-summary-expand-window (&optional arg)
7671   "Make the summary buffer take up the entire Emacs frame.
7672 Given a prefix, will force an `article' buffer configuration."
7673   (interactive "P")
7674   (if arg
7675       (gnus-configure-windows 'article 'force)
7676     (gnus-configure-windows 'summary 'force)))
7677
7678 (defun gnus-summary-display-article (article &optional all-header)
7679   "Display ARTICLE in article buffer."
7680   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7681                (with-current-buffer gnus-article-buffer
7682                  (eq major-mode 'gnus-article-mode)))
7683     (gnus-article-setup-buffer))
7684   (gnus-set-global-variables)
7685   (with-current-buffer gnus-article-buffer
7686     (setq gnus-article-charset gnus-newsgroup-charset)
7687     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7688     (mm-enable-multibyte))
7689   (if (null article)
7690       nil
7691     (prog1
7692         (if gnus-summary-display-article-function
7693             (funcall gnus-summary-display-article-function article all-header)
7694           (gnus-article-prepare article all-header))
7695       (gnus-run-hooks 'gnus-select-article-hook)
7696       (when (and gnus-current-article
7697                  (not (zerop gnus-current-article)))
7698         (gnus-summary-goto-subject gnus-current-article))
7699       (gnus-summary-recenter)
7700       (when (and gnus-use-trees gnus-show-threads)
7701         (gnus-possibly-generate-tree article)
7702         (gnus-highlight-selected-tree article))
7703       ;; Successfully display article.
7704       (gnus-article-set-window-start
7705        (cdr (assq article gnus-newsgroup-bookmarks))))))
7706
7707 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7708   "Select the current article.
7709 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7710 non-nil, the article will be re-fetched even if it already present in
7711 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7712 be displayed."
7713   ;; Make sure we are in the summary buffer to work around bbdb bug.
7714   (unless (eq major-mode 'gnus-summary-mode)
7715     (set-buffer gnus-summary-buffer))
7716   (let ((article (or article (gnus-summary-article-number)))
7717         (all-headers (not (not all-headers))) ;Must be t or nil.
7718         gnus-summary-display-article-function)
7719     (and (not pseudo)
7720          (gnus-summary-article-pseudo-p article)
7721          (error "This is a pseudo-article"))
7722     (with-current-buffer gnus-summary-buffer
7723       (if (or (and gnus-single-article-buffer
7724                    (or (null gnus-current-article)
7725                        (null gnus-article-current)
7726                        (null (get-buffer gnus-article-buffer))
7727                        (not (eq article (cdr gnus-article-current)))
7728                        (not (equal (car gnus-article-current)
7729                                    gnus-newsgroup-name))
7730                        (not (get-buffer gnus-original-article-buffer))))
7731               (and (not gnus-single-article-buffer)
7732                    (or (null gnus-current-article)
7733                        (not (get-buffer gnus-original-article-buffer))
7734                        (not (eq gnus-current-article article))))
7735               force)
7736           ;; The requested article is different from the current article.
7737           (progn
7738             (gnus-summary-display-article article all-headers)
7739             (when (gnus-buffer-live-p gnus-article-buffer)
7740               (with-current-buffer gnus-article-buffer
7741                 (if (not gnus-article-decoded-p) ;; a local variable
7742                     (mm-disable-multibyte))))
7743             (gnus-article-set-window-start
7744              (cdr (assq article gnus-newsgroup-bookmarks)))
7745             article)
7746         'old))))
7747
7748 (defun gnus-summary-force-verify-and-decrypt ()
7749   "Display buttons for signed/encrypted parts and verify/decrypt them."
7750   (interactive)
7751   (let ((mm-verify-option 'known)
7752         (mm-decrypt-option 'known)
7753         (gnus-article-emulate-mime t)
7754         (gnus-buttonized-mime-types (append (list "multipart/signed"
7755                                                   "multipart/encrypted")
7756                                             gnus-buttonized-mime-types)))
7757     (gnus-summary-select-article nil 'force)))
7758
7759 (defun gnus-summary-set-current-mark (&optional current-mark)
7760   "Obsolete function."
7761   nil)
7762
7763 (defun gnus-summary-next-article (&optional unread subject backward push)
7764   "Select the next article.
7765 If UNREAD, only unread articles are selected.
7766 If SUBJECT, only articles with SUBJECT are selected.
7767 If BACKWARD, the previous article is selected instead of the next."
7768   (interactive "P")
7769   ;; Make sure we are in the summary buffer.
7770   (unless (eq major-mode 'gnus-summary-mode)
7771     (set-buffer gnus-summary-buffer))
7772   (cond
7773    ;; Is there such an article?
7774    ((and (gnus-summary-search-forward unread subject backward)
7775          (or (gnus-summary-display-article (gnus-summary-article-number))
7776              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7777     (gnus-summary-position-point))
7778    ;; If not, we try the first unread, if that is wanted.
7779    ((and subject
7780          gnus-auto-select-same
7781          (gnus-summary-first-unread-article))
7782     (gnus-summary-position-point)
7783     (gnus-message 6 "Wrapped"))
7784    ;; Try to get next/previous article not displayed in this group.
7785    ((and gnus-auto-extend-newsgroup
7786          (not unread) (not subject))
7787     (gnus-summary-goto-article
7788      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7789      nil (count-lines (point-min) (point))))
7790    ;; Go to next/previous group.
7791    (t
7792     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7793       (gnus-summary-jump-to-group gnus-newsgroup-name))
7794     (let ((cmd (if (featurep 'xemacs)
7795                    last-command-char
7796                  last-command-event))
7797           (point
7798            (with-current-buffer gnus-group-buffer
7799              (point)))
7800           (current-summary (current-buffer))
7801           (group
7802            (if (eq gnus-keep-same-level 'best)
7803                (gnus-summary-best-group gnus-newsgroup-name)
7804              (gnus-summary-search-group backward gnus-keep-same-level))))
7805       ;; Select next unread newsgroup automagically.
7806       (cond
7807        ((or (not gnus-auto-select-next)
7808             (not cmd))
7809         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7810        ((or (eq gnus-auto-select-next 'quietly)
7811             (and (eq gnus-auto-select-next 'slightly-quietly)
7812                  push)
7813             (and (eq gnus-auto-select-next 'almost-quietly)
7814                  (gnus-summary-last-article-p)))
7815         ;; Select quietly.
7816         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7817             (gnus-summary-exit)
7818           (gnus-message 7 "No more%s articles (%s)..."
7819                         (if unread " unread" "")
7820                         (if group (concat "selecting " group)
7821                           "exiting"))
7822           (gnus-summary-next-group nil group backward)))
7823        (t
7824         (when (gnus-key-press-event-p last-input-event)
7825           ;; Somehow or other, we may now have selected a different
7826           ;; window.  Make point go back to the summary buffer.
7827           (when (eq current-summary (current-buffer))
7828             ;; FIXME: This burps when get-buffer-window returns nil.
7829             (select-window (get-buffer-window current-summary 0)))
7830           (gnus-summary-walk-group-buffer
7831            gnus-newsgroup-name cmd unread backward point))))))))
7832
7833 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7834   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7835                       (?\C-p (gnus-group-prev-unread-group 1))))
7836         (cursor-in-echo-area t)
7837         keve key group ended prompt)
7838     (with-current-buffer gnus-group-buffer
7839       (goto-char start)
7840       (setq group
7841             (if (eq gnus-keep-same-level 'best)
7842                 (gnus-summary-best-group gnus-newsgroup-name)
7843               (gnus-summary-search-group backward gnus-keep-same-level))))
7844     (while (not ended)
7845       (setq prompt
7846             (format
7847              "No more%s articles%s " (if unread " unread" "")
7848              (if (and group
7849                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7850                  (format " (Type %s for %s [%s])"
7851                          (single-key-description cmd)
7852                          (gnus-group-decoded-name group)
7853                          (gnus-group-unread group))
7854                (format " (Type %s to exit %s)"
7855                        (single-key-description cmd)
7856                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7857       ;; Confirm auto selection.
7858       (setq key (car (setq keve (gnus-read-event-char prompt)))
7859             ended t)
7860       (cond
7861        ((assq key keystrokes)
7862         (let ((obuf (current-buffer)))
7863           (switch-to-buffer gnus-group-buffer)
7864           (when group
7865             (gnus-group-jump-to-group group))
7866           (eval (cadr (assq key keystrokes)))
7867           (setq group (gnus-group-group-name))
7868           (switch-to-buffer obuf))
7869         (setq ended nil))
7870        ((equal key cmd)
7871         (if (or (not group)
7872                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7873             (gnus-summary-exit)
7874           (gnus-summary-next-group nil group backward)))
7875        (t
7876         (push (cdr keve) unread-command-events))))))
7877
7878 (defun gnus-summary-next-unread-article ()
7879   "Select unread article after current one."
7880   (interactive)
7881   (gnus-summary-next-article
7882    (or (not (eq gnus-summary-goto-unread 'never))
7883        (gnus-summary-last-article-p (gnus-summary-article-number)))
7884    (and gnus-auto-select-same
7885         (gnus-summary-article-subject))))
7886
7887 (defun gnus-summary-prev-article (&optional unread subject)
7888   "Select the article before the current one.
7889 If UNREAD is non-nil, only unread articles are selected."
7890   (interactive "P")
7891   (gnus-summary-next-article unread subject t))
7892
7893 (defun gnus-summary-prev-unread-article ()
7894   "Select unread article before current one."
7895   (interactive)
7896   (gnus-summary-prev-article
7897    (or (not (eq gnus-summary-goto-unread 'never))
7898        (gnus-summary-first-article-p (gnus-summary-article-number)))
7899    (and gnus-auto-select-same
7900         (gnus-summary-article-subject))))
7901
7902 (defun gnus-summary-next-page (&optional lines circular stop)
7903   "Show next page of the selected article.
7904 If at the end of the current article, select the next article.
7905 LINES says how many lines should be scrolled up.
7906
7907 If CIRCULAR is non-nil, go to the start of the article instead of
7908 selecting the next article when reaching the end of the current
7909 article.
7910
7911 If STOP is non-nil, just stop when reaching the end of the message.
7912
7913 Also see the variable `gnus-article-skip-boring'."
7914   (interactive "P")
7915   (setq gnus-summary-buffer (current-buffer))
7916   (gnus-set-global-variables)
7917   (let ((article (gnus-summary-article-number))
7918         (article-window (get-buffer-window gnus-article-buffer t))
7919         endp)
7920     ;; If the buffer is empty, we have no article.
7921     (unless article
7922       (error "No article to select"))
7923     (gnus-configure-windows 'article)
7924     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7925         (if (and (eq gnus-summary-goto-unread 'never)
7926                  (not (gnus-summary-last-article-p article)))
7927             (gnus-summary-next-article)
7928           (gnus-summary-next-unread-article))
7929       (if (or (null gnus-current-article)
7930               (null gnus-article-current)
7931               (/= article (cdr gnus-article-current))
7932               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7933           ;; Selected subject is different from current article's.
7934           (gnus-summary-display-article article)
7935         (when article-window
7936           (gnus-eval-in-buffer-window gnus-article-buffer
7937             (setq endp (or (gnus-article-next-page lines)
7938                            (gnus-article-only-boring-p))))
7939           (when endp
7940             (cond ((or stop gnus-summary-stop-at-end-of-message)
7941                    (gnus-message 3 "End of message"))
7942                   (circular
7943                    (gnus-summary-beginning-of-article))
7944                   (lines
7945                    (gnus-message 3 "End of message"))
7946                   ((null lines)
7947                    (if (and (eq gnus-summary-goto-unread 'never)
7948                             (not (gnus-summary-last-article-p article)))
7949                        (gnus-summary-next-article)
7950                      (gnus-summary-next-unread-article))))))))
7951     (gnus-summary-recenter)
7952     (gnus-summary-position-point)))
7953
7954 (defun gnus-summary-prev-page (&optional lines move)
7955   "Show previous page of selected article.
7956 Argument LINES specifies lines to be scrolled down.
7957 If MOVE, move to the previous unread article if point is at
7958 the beginning of the buffer."
7959   (interactive "P")
7960   (let ((article (gnus-summary-article-number))
7961         (article-window (get-buffer-window gnus-article-buffer t))
7962         endp)
7963     (gnus-configure-windows 'article)
7964     (if (or (null gnus-current-article)
7965             (null gnus-article-current)
7966             (/= article (cdr gnus-article-current))
7967             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7968         ;; Selected subject is different from current article's.
7969         (gnus-summary-display-article article)
7970       (gnus-summary-recenter)
7971       (when article-window
7972         (gnus-eval-in-buffer-window gnus-article-buffer
7973           (setq endp (gnus-article-prev-page lines)))
7974         (when (and move endp)
7975           (cond (lines
7976                  (gnus-message 3 "Beginning of message"))
7977                 ((null lines)
7978                  (if (and (eq gnus-summary-goto-unread 'never)
7979                           (not (gnus-summary-first-article-p article)))
7980                      (gnus-summary-prev-article)
7981                    (gnus-summary-prev-unread-article))))))))
7982   (gnus-summary-position-point))
7983
7984 (defun gnus-summary-prev-page-or-article (&optional lines)
7985   "Show previous page of selected article.
7986 Argument LINES specifies lines to be scrolled down.
7987 If at the beginning of the article, go to the next article."
7988   (interactive "P")
7989   (gnus-summary-prev-page lines t))
7990
7991 (defun gnus-summary-scroll-up (lines)
7992   "Scroll up (or down) one line current article.
7993 Argument LINES specifies lines to be scrolled up (or down if negative).
7994 If no article is selected, then the current article will be selected first."
7995   (interactive "p")
7996   (gnus-configure-windows 'article)
7997   (gnus-summary-show-thread)
7998   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7999     (gnus-eval-in-buffer-window gnus-article-buffer
8000       (cond ((> lines 0)
8001              (when (gnus-article-next-page lines)
8002                (gnus-message 3 "End of message")))
8003             ((< lines 0)
8004              (gnus-article-prev-page (- lines))))))
8005   (gnus-summary-recenter)
8006   (gnus-summary-position-point))
8007
8008 (defun gnus-summary-scroll-down (lines)
8009   "Scroll down (or up) one line current article.
8010 Argument LINES specifies lines to be scrolled down (or up if negative).
8011 If no article is selected, then the current article will be selected first."
8012   (interactive "p")
8013   (gnus-summary-scroll-up (- lines)))
8014
8015 (defun gnus-summary-next-same-subject ()
8016   "Select next article which has the same subject as current one."
8017   (interactive)
8018   (gnus-summary-next-article nil (gnus-summary-article-subject)))
8019
8020 (defun gnus-summary-prev-same-subject ()
8021   "Select previous article which has the same subject as current one."
8022   (interactive)
8023   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8024
8025 (defun gnus-summary-next-unread-same-subject ()
8026   "Select next unread article which has the same subject as current one."
8027   (interactive)
8028   (gnus-summary-next-article t (gnus-summary-article-subject)))
8029
8030 (defun gnus-summary-prev-unread-same-subject ()
8031   "Select previous unread article which has the same subject as current one."
8032   (interactive)
8033   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8034
8035 (defun gnus-summary-first-unread-article ()
8036   "Select the first unread article.
8037 Return nil if there are no unread articles."
8038   (interactive)
8039   (prog1
8040       (when (gnus-summary-first-subject t)
8041         (gnus-summary-show-thread)
8042         (gnus-summary-first-subject t)
8043         (gnus-summary-display-article (gnus-summary-article-number)))
8044     (gnus-summary-position-point)))
8045
8046 (defun gnus-summary-first-unread-subject ()
8047   "Place the point on the subject line of the first unread article.
8048 Return nil if there are no unread articles."
8049   (interactive)
8050   (prog1
8051       (when (gnus-summary-first-subject t)
8052         (gnus-summary-show-thread)
8053         (gnus-summary-first-subject t))
8054     (gnus-summary-position-point)))
8055
8056 (defun gnus-summary-first-unseen-subject ()
8057   "Place the point on the subject line of the first unseen article.
8058 Return nil if there are no unseen articles."
8059   (interactive)
8060   (prog1
8061       (when (gnus-summary-first-subject nil nil t)
8062         (gnus-summary-show-thread)
8063         (gnus-summary-first-subject nil nil t))
8064     (gnus-summary-position-point)))
8065
8066 (defun gnus-summary-first-unseen-or-unread-subject ()
8067   "Place the point on the subject line of the first unseen and unread article.
8068 If all article have been seen, on the subject line of the first unread
8069 article."
8070   (interactive)
8071   (prog1
8072       (unless (when (gnus-summary-first-subject nil nil t)
8073                 (gnus-summary-show-thread)
8074                 (gnus-summary-first-subject nil nil t))
8075         (when (gnus-summary-first-subject t)
8076           (gnus-summary-show-thread)
8077           (gnus-summary-first-subject t)))
8078     (gnus-summary-position-point)))
8079
8080 (defun gnus-summary-first-article ()
8081   "Select the first article.
8082 Return nil if there are no articles."
8083   (interactive)
8084   (prog1
8085       (when (gnus-summary-first-subject)
8086         (gnus-summary-show-thread)
8087         (gnus-summary-first-subject)
8088         (gnus-summary-display-article (gnus-summary-article-number)))
8089     (gnus-summary-position-point)))
8090
8091 (defun gnus-summary-best-unread-article (&optional arg)
8092   "Select the unread article with the highest score.
8093 If given a prefix argument, select the next unread article that has a
8094 score higher than the default score."
8095   (interactive "P")
8096   (let ((article (if arg
8097                      (gnus-summary-better-unread-subject)
8098                    (gnus-summary-best-unread-subject))))
8099     (if article
8100         (gnus-summary-goto-article article)
8101       (error "No unread articles"))))
8102
8103 (defun gnus-summary-best-unread-subject ()
8104   "Select the unread subject with the highest score."
8105   (interactive)
8106   (let ((best -1000000)
8107         (data gnus-newsgroup-data)
8108         article score)
8109     (while data
8110       (and (gnus-data-unread-p (car data))
8111            (> (setq score
8112                     (gnus-summary-article-score (gnus-data-number (car data))))
8113               best)
8114            (setq best score
8115                  article (gnus-data-number (car data))))
8116       (setq data (cdr data)))
8117     (when article
8118       (gnus-summary-goto-subject article))
8119     (gnus-summary-position-point)
8120     article))
8121
8122 (defun gnus-summary-better-unread-subject ()
8123   "Select the first unread subject that has a score over the default score."
8124   (interactive)
8125   (let ((data gnus-newsgroup-data)
8126         article score)
8127     (while (and (setq article (gnus-data-number (car data)))
8128                 (or (gnus-data-read-p (car data))
8129                     (not (> (gnus-summary-article-score article)
8130                             gnus-summary-default-score))))
8131       (setq data (cdr data)))
8132     (when article
8133       (gnus-summary-goto-subject article))
8134     (gnus-summary-position-point)
8135     article))
8136
8137 (defun gnus-summary-last-subject ()
8138   "Go to the last displayed subject line in the group."
8139   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8140     (when article
8141       (gnus-summary-goto-subject article))))
8142
8143 (defun gnus-summary-goto-article (article &optional all-headers force)
8144   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8145 If ALL-HEADERS is non-nil, no header lines are hidden.
8146 If FORCE, go to the article even if it isn't displayed.  If FORCE
8147 is a number, it is the line the article is to be displayed on."
8148   (interactive
8149    (list
8150     (gnus-completing-read
8151      "Article number or Message-ID"
8152      (mapcar 'int-to-string gnus-newsgroup-limit))
8153     current-prefix-arg
8154     t))
8155   (prog1
8156       (if (and (stringp article)
8157                (string-match "@\\|%40" article))
8158           (gnus-summary-refer-article article)
8159         (when (stringp article)
8160           (setq article (string-to-number article)))
8161         (if (gnus-summary-goto-subject article force)
8162             (gnus-summary-display-article article all-headers)
8163           (gnus-message 4 "Couldn't go to article %s" article) nil))
8164     (gnus-summary-position-point)))
8165
8166 (defun gnus-summary-goto-last-article ()
8167   "Go to the previously read article."
8168   (interactive)
8169   (prog1
8170       (when gnus-last-article
8171         (gnus-summary-goto-article gnus-last-article nil t))
8172     (gnus-summary-position-point)))
8173
8174 (defun gnus-summary-pop-article (number)
8175   "Pop one article off the history and go to the previous.
8176 NUMBER articles will be popped off."
8177   (interactive "p")
8178   (let (to)
8179     (setq gnus-newsgroup-history
8180           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8181     (if to
8182         (gnus-summary-goto-article (car to) nil t)
8183       (error "Article history empty")))
8184   (gnus-summary-position-point))
8185
8186 ;; Summary commands and functions for limiting the summary buffer.
8187
8188 (defun gnus-summary-limit-to-articles (n)
8189   "Limit the summary buffer to the next N articles.
8190 If not given a prefix, use the process marked articles instead."
8191   (interactive "P")
8192   (prog1
8193       (let ((articles (gnus-summary-work-articles n)))
8194         (setq gnus-newsgroup-processable nil)
8195         (gnus-summary-limit articles))
8196     (gnus-summary-position-point)))
8197
8198 (defun gnus-summary-pop-limit (&optional total)
8199   "Restore the previous limit.
8200 If given a prefix, remove all limits."
8201   (interactive "P")
8202   (when total
8203     (setq gnus-newsgroup-limits
8204           (list (mapcar (lambda (h) (mail-header-number h))
8205                         gnus-newsgroup-headers))))
8206   (unless gnus-newsgroup-limits
8207     (error "No limit to pop"))
8208   (prog1
8209       (gnus-summary-limit nil 'pop)
8210     (gnus-summary-position-point)))
8211
8212 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8213   "Limit the summary buffer to articles that have subjects that match a regexp.
8214 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8215   (interactive
8216    (list (read-string (if current-prefix-arg
8217                           "Exclude subject (regexp): "
8218                         "Limit to subject (regexp): "))
8219          nil current-prefix-arg))
8220   (unless header
8221     (setq header "subject"))
8222   (when (not (equal "" subject))
8223     (prog1
8224         (let ((articles (gnus-summary-find-matching
8225                          (or header "subject") subject 'all nil nil
8226                          not-matching)))
8227           (unless articles
8228             (error "Found no matches for \"%s\"" subject))
8229           (gnus-summary-limit articles))
8230       (gnus-summary-position-point))))
8231
8232 (defun gnus-summary-limit-to-author (from &optional not-matching)
8233   "Limit the summary buffer to articles that have authors that match a regexp.
8234 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8235   (interactive
8236    (list (read-string (if current-prefix-arg
8237                           "Exclude author (regexp): "
8238                         "Limit to author (regexp): "))
8239          current-prefix-arg))
8240   (gnus-summary-limit-to-subject from "from" not-matching))
8241
8242 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8243   "Limit the summary buffer to articles with the given RECIPIENT.
8244
8245 If NOT-MATCHING, exclude RECIPIENT.
8246
8247 To and Cc headers are checked.  You need to include them in
8248 `nnmail-extra-headers'."
8249   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8250   (interactive
8251    (list (read-string (format "%s recipient (regexp): "
8252                               (if current-prefix-arg "Exclude" "Limit to")))
8253          current-prefix-arg))
8254   (when (not (equal "" recipient))
8255     (prog1 (let* ((to
8256                    (if (memq 'To nnmail-extra-headers)
8257                        (gnus-summary-find-matching
8258                         (cons 'extra 'To) recipient 'all nil nil
8259                         not-matching)
8260                      (gnus-message
8261                       1 "`To' isn't present in `nnmail-extra-headers'")
8262                      (sit-for 1)
8263                      nil))
8264                   (cc
8265                    (if (memq 'Cc nnmail-extra-headers)
8266                        (gnus-summary-find-matching
8267                         (cons 'extra 'Cc) recipient 'all nil nil
8268                         not-matching)
8269                      (gnus-message
8270                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8271                      (sit-for 1)
8272                      nil))
8273                   (articles
8274                    (if not-matching
8275                        ;; We need the numbers that are in both lists:
8276                        (mapcar (lambda (a)
8277                                  (and (memq a to) a))
8278                                cc)
8279                      (nconc to cc))))
8280              (unless articles
8281                (error "Found no matches for \"%s\"" recipient))
8282              (gnus-summary-limit articles))
8283       (gnus-summary-position-point))))
8284
8285 (defun gnus-summary-limit-to-address (address &optional not-matching)
8286   "Limit the summary buffer to articles with the given ADDRESS.
8287
8288 If NOT-MATCHING, exclude ADDRESS.
8289
8290 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8291 in `nnmail-extra-headers'."
8292   (interactive
8293    (list (read-string (format "%s address (regexp): "
8294                               (if current-prefix-arg "Exclude" "Limit to")))
8295          current-prefix-arg))
8296   (when (not (equal "" address))
8297     (prog1 (let* ((to
8298                    (if (memq 'To nnmail-extra-headers)
8299                        (gnus-summary-find-matching
8300                         (cons 'extra 'To) address 'all nil nil
8301                         not-matching)
8302                      (gnus-message
8303                       1 "`To' isn't present in `nnmail-extra-headers'")
8304                      (sit-for 1)
8305                      t))
8306                   (cc
8307                    (if (memq 'Cc nnmail-extra-headers)
8308                        (gnus-summary-find-matching
8309                         (cons 'extra 'Cc) address 'all nil nil
8310                         not-matching)
8311                      (gnus-message
8312                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8313                      (sit-for 1)
8314                      t))
8315                   (from
8316                    (gnus-summary-find-matching "from" address
8317                                                'all nil nil not-matching))
8318                   (articles
8319                    (if not-matching
8320                        ;; We need the numbers that are in all lists:
8321                        (if (eq cc t)
8322                            (if (eq to t)
8323                                from
8324                              (mapcar (lambda (a) (car (memq a from))) to))
8325                          (if (eq to t)
8326                              (mapcar (lambda (a) (car (memq a from))) cc)
8327                            (mapcar (lambda (a) (car (memq a from)))
8328                                    (mapcar (lambda (a) (car (memq a to)))
8329                                            cc))))
8330                      (nconc (if (eq to t) nil to)
8331                             (if (eq cc t) nil cc)
8332                             from))))
8333              (unless articles
8334                (error "Found no matches for \"%s\"" address))
8335              (gnus-summary-limit articles))
8336       (gnus-summary-position-point))))
8337
8338 (defun gnus-summary-limit-strange-charsets-predicate (header)
8339   (when (fboundp 'char-charset)
8340     (let ((string (concat (mail-header-subject header)
8341                           (mail-header-from header)))
8342           charset found)
8343       (dotimes (i (1- (length string)))
8344         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8345         (when (string-match "unicode\\|big\\|japanese" charset)
8346           (setq found t)))
8347       found)))
8348
8349 (defun gnus-summary-limit-to-predicate (predicate)
8350   "Limit to articles where PREDICATE returns non-nil.
8351 PREDICATE will be called with the header structures of the
8352 articles."
8353   (let ((articles nil)
8354         (case-fold-search t))
8355     (dolist (header gnus-newsgroup-headers)
8356       (when (funcall predicate header)
8357         (push (mail-header-number header) articles)))
8358     (gnus-summary-limit (nreverse articles))))
8359
8360 (defun gnus-summary-limit-to-age (age &optional younger-p)
8361   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8362 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8363 articles that are younger than AGE days."
8364   (interactive
8365    (let ((younger current-prefix-arg)
8366          (days-got nil)
8367          days)
8368      (while (not days-got)
8369        (setq days (if younger
8370                       (read-string "Limit to articles younger than (in days, older when negative): ")
8371                     (read-string
8372                      "Limit to articles older than (in days, younger when negative): ")))
8373        (when (> (length days) 0)
8374          (setq days (read days)))
8375        (if (numberp days)
8376            (progn
8377              (setq days-got t)
8378              (when (< days 0)
8379                (setq younger (not younger))
8380                (setq days (* days -1))))
8381          (message "Please enter a number.")
8382          (sleep-for 1)))
8383      (list days younger)))
8384   (prog1
8385       (let ((data gnus-newsgroup-data)
8386             (cutoff (days-to-time age))
8387             articles d date is-younger)
8388         (while (setq d (pop data))
8389           (when (and (vectorp (gnus-data-header d))
8390                      (setq date (mail-header-date (gnus-data-header d))))
8391             (setq is-younger (time-less-p
8392                               (time-since (gnus-date-get-time date))
8393                               cutoff))
8394             (when (if younger-p
8395                       is-younger
8396                     (not is-younger))
8397               (push (gnus-data-number d) articles))))
8398         (gnus-summary-limit (nreverse articles)))
8399     (gnus-summary-position-point)))
8400
8401 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8402   "Limit the summary buffer to articles that match an 'extra' header."
8403   (interactive
8404    (let ((header
8405           (intern
8406            (gnus-completing-read
8407             (if current-prefix-arg
8408                 "Exclude extra header"
8409               "Limit extra header")
8410             (mapcar 'symbol-name gnus-extra-headers)
8411             t nil nil
8412             (symbol-name (car gnus-extra-headers))))))
8413      (list header
8414            (read-string (format "%s header %s (regexp): "
8415                                 (if current-prefix-arg "Exclude" "Limit to")
8416                                 header))
8417            current-prefix-arg)))
8418   (when (not (equal "" regexp))
8419     (prog1
8420         (let ((articles (gnus-summary-find-matching
8421                          (cons 'extra header) regexp 'all nil nil
8422                          not-matching)))
8423           (unless articles
8424             (error "Found no matches for \"%s\"" regexp))
8425           (gnus-summary-limit articles))
8426       (gnus-summary-position-point))))
8427
8428 (defun gnus-summary-limit-to-display-predicate ()
8429   "Limit the summary buffer to the predicated in the `display' group parameter."
8430   (interactive)
8431   (unless gnus-newsgroup-display
8432     (error "There is no `display' group parameter"))
8433   (let (articles)
8434     (dolist (gnus-number gnus-newsgroup-articles)
8435       (when (funcall gnus-newsgroup-display)
8436         (push gnus-number articles)))
8437     (gnus-summary-limit articles))
8438   (gnus-summary-position-point))
8439
8440 (defun gnus-summary-limit-to-unread (&optional all)
8441   "Limit the summary buffer to articles that are not marked as read.
8442 If ALL is non-nil, limit strictly to unread articles."
8443   (interactive "P")
8444   (if all
8445       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8446     (gnus-summary-limit-to-marks
8447      ;; Concat all the marks that say that an article is read and have
8448      ;; those removed.
8449      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8450            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8451            gnus-low-score-mark gnus-expirable-mark
8452            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8453            gnus-duplicate-mark)
8454      'reverse)))
8455
8456 (defun gnus-summary-limit-to-headers (match &optional reverse)
8457   "Limit the summary buffer to articles that have headers that match MATCH.
8458 If REVERSE (the prefix), limit to articles that don't match."
8459   (interactive "sMatch headers (regexp): \nP")
8460   (gnus-summary-limit-to-bodies match reverse t))
8461
8462 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8463   "Limit the summary buffer to articles that have bodies that match MATCH.
8464 If REVERSE (the prefix), limit to articles that don't match."
8465   (interactive "sMatch body (regexp): \nP")
8466   (let ((articles nil)
8467         (gnus-select-article-hook nil)  ;Disable hook.
8468         (gnus-article-prepare-hook nil)
8469         (gnus-use-article-prefetch nil)
8470         (gnus-keep-backlog nil)
8471         (gnus-break-pages nil)
8472         (gnus-summary-display-arrow nil)
8473         (gnus-updated-mode-lines nil)
8474         (gnus-auto-center-summary nil)
8475         (gnus-display-mime-function nil))
8476     (dolist (data gnus-newsgroup-data)
8477       (let (gnus-mark-article-hook)
8478         (gnus-summary-select-article t t nil (gnus-data-number data)))
8479       (with-current-buffer gnus-article-buffer
8480         (article-goto-body)
8481         (let* ((case-fold-search t)
8482                (found (if headersp
8483                           (re-search-backward match nil t)
8484                         (re-search-forward match nil t))))
8485           (when (or (and found
8486                          (not reverse))
8487                     (and (not found)
8488                          reverse))
8489             (push (gnus-data-number data) articles)))))
8490     (if (not articles)
8491         (message "No messages matched")
8492       (gnus-summary-limit articles)))
8493   (gnus-summary-position-point))
8494
8495 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8496   "Limit the summary buffer to articles that aren't part on any thread.
8497 If THREADSP (the prefix), limit to articles that are in threads."
8498   (interactive "P")
8499   (let ((articles nil)
8500         thread-articles
8501         threads)
8502     (dolist (thread gnus-newsgroup-threads)
8503       (if (stringp (car thread))
8504           (dolist (thread (cdr thread))
8505             (push thread threads))
8506         (push thread threads)))
8507     (dolist (thread threads)
8508       (setq thread-articles (gnus-articles-in-thread thread))
8509       (when (or (and threadsp
8510                      (> (length thread-articles) 1))
8511                 (and (not threadsp)
8512                      (= (length thread-articles) 1)))
8513         (setq articles (nconc thread-articles articles))))
8514     (if (not articles)
8515         (message "No messages matched")
8516       (gnus-summary-limit articles))
8517     (gnus-summary-position-point)))
8518
8519 (defun gnus-summary-limit-to-replied (&optional unreplied)
8520   "Limit the summary buffer to replied articles.
8521 If UNREPLIED (the prefix), limit to unreplied articles."
8522   (interactive "P")
8523   (if unreplied
8524       (gnus-summary-limit
8525        (gnus-set-difference gnus-newsgroup-articles
8526         gnus-newsgroup-replied))
8527     (gnus-summary-limit gnus-newsgroup-replied))
8528   (gnus-summary-position-point))
8529
8530 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8531   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8532 If REVERSE, limit the summary buffer to articles that are marked
8533 with MARKS.  MARKS can either be a string of marks or a list of marks.
8534 Returns how many articles were removed."
8535   (interactive "sMarks: ")
8536   (gnus-summary-limit-to-marks marks t))
8537
8538 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8539   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8540 If REVERSE (the prefix), limit the summary buffer to articles that are
8541 not marked with MARKS.  MARKS can either be a string of marks or a
8542 list of marks.
8543 Returns how many articles were removed."
8544   (interactive "sMarks: \nP")
8545   (prog1
8546       (let ((data gnus-newsgroup-data)
8547             (marks (if (listp marks) marks
8548                      (append marks nil))) ; Transform to list.
8549             articles)
8550         (while data
8551           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8552                   (memq (gnus-data-mark (car data)) marks))
8553             (push (gnus-data-number (car data)) articles))
8554           (setq data (cdr data)))
8555         (gnus-summary-limit articles))
8556     (gnus-summary-position-point)))
8557
8558 (defun gnus-summary-limit-to-score (score)
8559   "Limit to articles with score at or above SCORE."
8560   (interactive "NLimit to articles with score of at least: ")
8561   (let ((data gnus-newsgroup-data)
8562         articles)
8563     (while data
8564       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8565                 score)
8566         (push (gnus-data-number (car data)) articles))
8567       (setq data (cdr data)))
8568     (prog1
8569         (gnus-summary-limit articles)
8570       (gnus-summary-position-point))))
8571
8572 (defun gnus-summary-limit-to-unseen ()
8573   "Limit to unseen articles."
8574   (interactive)
8575   (prog1
8576       (gnus-summary-limit gnus-newsgroup-unseen)
8577     (gnus-summary-position-point)))
8578
8579 (defun gnus-summary-limit-include-thread (id)
8580   "Display all the hidden articles that is in the thread with ID in it.
8581 When called interactively, ID is the Message-ID of the current
8582 article."
8583   (interactive (list (mail-header-id (gnus-summary-article-header))))
8584   (let ((articles (gnus-articles-in-thread
8585                    (gnus-id-to-thread (gnus-root-id id))))
8586         ;;we REALLY want the whole thread---this prevents cut-threads
8587         ;;from removing the thread we want to include.
8588         (gnus-fetch-old-headers nil)
8589         (gnus-build-sparse-threads nil))
8590     (prog1
8591         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8592       (gnus-summary-limit-include-matching-articles
8593        "subject"
8594        (regexp-quote (gnus-simplify-subject-re
8595                       (mail-header-subject (gnus-id-to-header id)))))
8596       (gnus-summary-position-point))))
8597
8598 (defun gnus-summary-limit-include-matching-articles (header regexp)
8599   "Display all the hidden articles that have HEADERs that match REGEXP."
8600   (interactive (list (read-string "Match on header: ")
8601                      (read-string "Regexp: ")))
8602   (let ((articles (gnus-find-matching-articles header regexp)))
8603     (prog1
8604         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8605       (gnus-summary-position-point))))
8606
8607 (defun gnus-summary-insert-dormant-articles ()
8608   "Insert all the dormant articles for this group into the current buffer."
8609   (interactive)
8610   (let ((gnus-verbose (max 6 gnus-verbose)))
8611     (if (not gnus-newsgroup-dormant)
8612         (gnus-message 3 "No dormant articles for this group")
8613       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8614
8615 (defun gnus-summary-insert-ticked-articles ()
8616   "Insert ticked articles for this group into the current buffer."
8617   (interactive)
8618   (let ((gnus-verbose (max 6 gnus-verbose)))
8619     (if (not gnus-newsgroup-marked)
8620         (gnus-message 3 "No ticked articles for this group")
8621       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8622
8623 (defun gnus-summary-limit-include-dormant ()
8624   "Display all the hidden articles that are marked as dormant.
8625 Note that this command only works on a subset of the articles currently
8626 fetched for this group."
8627   (interactive)
8628   (unless gnus-newsgroup-dormant
8629     (error "There are no dormant articles in this group"))
8630   (prog1
8631       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8632     (gnus-summary-position-point)))
8633
8634 (defun gnus-summary-include-articles (articles)
8635   "Fetch the headers for ARTICLES and then display the summary lines."
8636   (let ((gnus-inhibit-demon t)
8637         (gnus-agent nil)
8638         (gnus-read-all-available-headers t))
8639     (setq gnus-newsgroup-headers
8640           (gnus-merge
8641            'list gnus-newsgroup-headers
8642            (gnus-fetch-headers articles nil t)
8643            'gnus-article-sort-by-number))
8644     (setq gnus-newsgroup-articles
8645           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8646     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8647
8648 (defun gnus-summary-limit-exclude-dormant ()
8649   "Hide all dormant articles."
8650   (interactive)
8651   (prog1
8652       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8653     (gnus-summary-position-point)))
8654
8655 (defun gnus-summary-limit-exclude-childless-dormant ()
8656   "Hide all dormant articles that have no children."
8657   (interactive)
8658   (let ((data (gnus-data-list t))
8659         articles d children)
8660     ;; Find all articles that are either not dormant or have
8661     ;; children.
8662     (while (setq d (pop data))
8663       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8664                 (and (setq children
8665                            (gnus-article-children (gnus-data-number d)))
8666                      (let (found)
8667                        (while children
8668                          (when (memq (car children) articles)
8669                            (setq children nil
8670                                  found t))
8671                          (pop children))
8672                        found)))
8673         (push (gnus-data-number d) articles)))
8674     ;; Do the limiting.
8675     (prog1
8676         (gnus-summary-limit articles)
8677       (gnus-summary-position-point))))
8678
8679 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8680   "Mark all unread excluded articles as read.
8681 If ALL, mark even excluded ticked and dormants as read."
8682   (interactive "P")
8683   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8684   (let ((articles (gnus-sorted-ndifference
8685                    (sort
8686                     (mapcar (lambda (h) (mail-header-number h))
8687                             gnus-newsgroup-headers)
8688                     '<)
8689                    gnus-newsgroup-limit))
8690         article)
8691     (setq gnus-newsgroup-unreads
8692           (gnus-sorted-intersection gnus-newsgroup-unreads
8693                                     gnus-newsgroup-limit))
8694     (if all
8695         (setq gnus-newsgroup-dormant nil
8696               gnus-newsgroup-marked nil
8697               gnus-newsgroup-reads
8698               (nconc
8699                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8700                gnus-newsgroup-reads))
8701       (while (setq article (pop articles))
8702         (unless (or (memq article gnus-newsgroup-dormant)
8703                     (memq article gnus-newsgroup-marked))
8704           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8705
8706 (defun gnus-summary-limit (articles &optional pop)
8707   (if pop
8708       ;; We pop the previous limit off the stack and use that.
8709       (setq articles (car gnus-newsgroup-limits)
8710             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8711     ;; We use the new limit, so we push the old limit on the stack.
8712     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8713   ;; Set the limit.
8714   (setq gnus-newsgroup-limit articles)
8715   (let ((total (length gnus-newsgroup-data))
8716         (data (gnus-data-find-list (gnus-summary-article-number)))
8717         (gnus-summary-mark-below nil)   ; Inhibit this.
8718         found)
8719     ;; This will do all the work of generating the new summary buffer
8720     ;; according to the new limit.
8721     (gnus-summary-prepare)
8722     ;; Hide any threads, possibly.
8723     (gnus-summary-maybe-hide-threads)
8724     ;; Try to return to the article you were at, or one in the
8725     ;; neighborhood.
8726     (when data
8727       ;; We try to find some article after the current one.
8728       (while data
8729         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8730           (setq data nil
8731                 found t))
8732         (setq data (cdr data))))
8733     (unless found
8734       ;; If there is no data, that means that we were after the last
8735       ;; article.  The same goes when we can't find any articles
8736       ;; after the current one.
8737       (goto-char (point-max))
8738       (gnus-summary-find-prev))
8739     (gnus-set-mode-line 'summary)
8740     ;; We return how many articles were removed from the summary
8741     ;; buffer as a result of the new limit.
8742     (- total (length gnus-newsgroup-data))))
8743
8744 (defsubst gnus-invisible-cut-children (threads)
8745   (let ((num 0))
8746     (while threads
8747       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8748         (incf num))
8749       (pop threads))
8750     (< num 2)))
8751
8752 (defsubst gnus-cut-thread (thread)
8753   "Go forwards in the thread until we find an article that we want to display."
8754   (when (or (eq gnus-fetch-old-headers 'some)
8755             (eq gnus-fetch-old-headers 'invisible)
8756             (numberp gnus-fetch-old-headers)
8757             (eq gnus-build-sparse-threads 'some)
8758             (eq gnus-build-sparse-threads 'more))
8759     ;; Deal with old-fetched headers and sparse threads.
8760     (while (and
8761             thread
8762             (or
8763              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8764              (gnus-summary-article-ancient-p
8765               (mail-header-number (car thread))))
8766             (if (or (<= (length (cdr thread)) 1)
8767                     (eq gnus-fetch-old-headers 'invisible))
8768                 (setq gnus-newsgroup-limit
8769                       (delq (mail-header-number (car thread))
8770                             gnus-newsgroup-limit)
8771                       thread (cadr thread))
8772               (when (gnus-invisible-cut-children (cdr thread))
8773                 (let ((th (cdr thread)))
8774                   (while th
8775                     (if (memq (mail-header-number (caar th))
8776                               gnus-newsgroup-limit)
8777                         (setq thread (car th)
8778                               th nil)
8779                       (setq th (cdr th))))))))))
8780   thread)
8781
8782 (defun gnus-cut-threads (threads)
8783   "Cut off all uninteresting articles from the beginning of THREADS."
8784   (when (or (eq gnus-fetch-old-headers 'some)
8785             (eq gnus-fetch-old-headers 'invisible)
8786             (numberp gnus-fetch-old-headers)
8787             (eq gnus-build-sparse-threads 'some)
8788             (eq gnus-build-sparse-threads 'more))
8789     (let ((th threads))
8790       (while th
8791         (setcar th (gnus-cut-thread (car th)))
8792         (setq th (cdr th)))))
8793   ;; Remove nixed out threads.
8794   (delq nil threads))
8795
8796 (defun gnus-summary-initial-limit (&optional show-if-empty)
8797   "Figure out what the initial limit is supposed to be on group entry.
8798 This entails weeding out unwanted dormants, low-scored articles,
8799 fetch-old-headers verbiage, and so on."
8800   ;; Most groups have nothing to remove.
8801   (unless (or gnus-inhibit-limiting
8802               (and (null gnus-newsgroup-dormant)
8803                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8804                    (not (eq gnus-fetch-old-headers 'some))
8805                    (not (numberp gnus-fetch-old-headers))
8806                    (not (eq gnus-fetch-old-headers 'invisible))
8807                    (null gnus-summary-expunge-below)
8808                    (not (eq gnus-build-sparse-threads 'some))
8809                    (not (eq gnus-build-sparse-threads 'more))
8810                    (null gnus-thread-expunge-below)))
8811     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8812     (setq gnus-newsgroup-limit nil)
8813     (mapatoms
8814      (lambda (node)
8815        (unless (car (symbol-value node))
8816          ;; These threads have no parents -- they are roots.
8817          (let ((nodes (cdr (symbol-value node)))
8818                thread)
8819            (while nodes
8820              (if (and gnus-thread-expunge-below
8821                       (< (gnus-thread-total-score (car nodes))
8822                          gnus-thread-expunge-below))
8823                  (gnus-expunge-thread (pop nodes))
8824                (setq thread (pop nodes))
8825                (gnus-summary-limit-children thread))))))
8826      gnus-newsgroup-dependencies)
8827     ;; If this limitation resulted in an empty group, we might
8828     ;; pop the previous limit and use it instead.
8829     (when (and (not gnus-newsgroup-limit)
8830                show-if-empty)
8831       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8832     gnus-newsgroup-limit))
8833
8834 (defun gnus-summary-limit-children (thread)
8835   "Return 1 if this subthread is visible and 0 if it is not."
8836   ;; First we get the number of visible children to this thread.  This
8837   ;; is done by recursing down the thread using this function, so this
8838   ;; will really go down to a leaf article first, before slowly
8839   ;; working its way up towards the root.
8840   (when thread
8841     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8842            (children
8843            (if (cdr thread)
8844                (apply '+ (mapcar 'gnus-summary-limit-children
8845                                  (cdr thread)))
8846              0))
8847            (number (mail-header-number (car thread)))
8848            score)
8849       (if (and
8850            (not (memq number gnus-newsgroup-marked))
8851            (or
8852             ;; If this article is dormant and has absolutely no visible
8853             ;; children, then this article isn't visible.
8854             (and (memq number gnus-newsgroup-dormant)
8855                  (zerop children))
8856             ;; If this is "fetch-old-headered" and there is no
8857             ;; visible children, then we don't want this article.
8858             (and (or (eq gnus-fetch-old-headers 'some)
8859                      (numberp gnus-fetch-old-headers))
8860                  (gnus-summary-article-ancient-p number)
8861                  (zerop children))
8862             ;; If this is "fetch-old-headered" and `invisible', then
8863             ;; we don't want this article.
8864             (and (eq gnus-fetch-old-headers 'invisible)
8865                  (gnus-summary-article-ancient-p number))
8866             ;; If this is a sparsely inserted article with no children,
8867             ;; we don't want it.
8868             (and (eq gnus-build-sparse-threads 'some)
8869                  (gnus-summary-article-sparse-p number)
8870                  (zerop children))
8871             ;; If we use expunging, and this article is really
8872             ;; low-scored, then we don't want this article.
8873             (when (and gnus-summary-expunge-below
8874                        (< (setq score
8875                                 (or (cdr (assq number gnus-newsgroup-scored))
8876                                     gnus-summary-default-score))
8877                           gnus-summary-expunge-below))
8878               ;; We increase the expunge-tally here, but that has
8879               ;; nothing to do with the limits, really.
8880               (incf gnus-newsgroup-expunged-tally)
8881               ;; We also mark as read here, if that's wanted.
8882               (when (and gnus-summary-mark-below
8883                          (< score gnus-summary-mark-below))
8884                 (setq gnus-newsgroup-unreads
8885                       (delq number gnus-newsgroup-unreads))
8886                 (if gnus-newsgroup-auto-expire
8887                     (push number gnus-newsgroup-expirable)
8888                   (push (cons number gnus-low-score-mark)
8889                         gnus-newsgroup-reads)))
8890               t)
8891             ;; Do the `display' group parameter.
8892             (and gnus-newsgroup-display
8893                  (let ((gnus-number number))
8894                    (not (funcall gnus-newsgroup-display))))))
8895           ;; Nope, invisible article.
8896           0
8897         ;; Ok, this article is to be visible, so we add it to the limit
8898         ;; and return 1.
8899         (push number gnus-newsgroup-limit)
8900         1))))
8901
8902 (defun gnus-expunge-thread (thread)
8903   "Mark all articles in THREAD as read."
8904   (let* ((number (mail-header-number (car thread))))
8905     (incf gnus-newsgroup-expunged-tally)
8906     ;; We also mark as read here, if that's wanted.
8907     (setq gnus-newsgroup-unreads
8908           (delq number gnus-newsgroup-unreads))
8909     (if gnus-newsgroup-auto-expire
8910         (push number gnus-newsgroup-expirable)
8911       (push (cons number gnus-low-score-mark)
8912             gnus-newsgroup-reads)))
8913   ;; Go recursively through all subthreads.
8914   (mapcar 'gnus-expunge-thread (cdr thread)))
8915
8916 ;; Summary article oriented commands
8917
8918 (defun gnus-summary-refer-parent-article (n)
8919   "Refer parent article N times.
8920 If N is negative, go to ancestor -N instead.
8921 The difference between N and the number of articles fetched is returned."
8922   (interactive "p")
8923   (let ((skip 1)
8924         error header ref)
8925     (when (not (natnump n))
8926       (setq skip (abs n)
8927             n 1))
8928     (while (and (> n 0)
8929                 (not error))
8930       (setq header (gnus-summary-article-header))
8931       (if (and (eq (mail-header-number header)
8932                    (cdr gnus-article-current))
8933                (equal gnus-newsgroup-name
8934                       (car gnus-article-current)))
8935           ;; If we try to find the parent of the currently
8936           ;; displayed article, then we take a look at the actual
8937           ;; References header, since this is slightly more
8938           ;; reliable than the References field we got from the
8939           ;; server.
8940           (with-current-buffer gnus-original-article-buffer
8941             (nnheader-narrow-to-headers)
8942             (unless (setq ref (message-fetch-field "references"))
8943               (when (setq ref (message-fetch-field "in-reply-to"))
8944                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8945             (widen))
8946         (setq ref
8947               ;; It's not the current article, so we take a bet on
8948               ;; the value we got from the server.
8949               (mail-header-references header)))
8950       (if (and ref
8951                (not (equal ref "")))
8952           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8953             (gnus-message 1 "Couldn't find parent"))
8954         (gnus-message 1 "No references in article %d"
8955                       (gnus-summary-article-number))
8956         (setq error t))
8957       (decf n))
8958     (gnus-summary-position-point)
8959     n))
8960
8961 (defun gnus-summary-refer-references ()
8962   "Fetch all articles mentioned in the References header.
8963 Return the number of articles fetched."
8964   (interactive)
8965   (let ((ref (mail-header-references (gnus-summary-article-header)))
8966         (current (gnus-summary-article-number))
8967         (n 0))
8968     (if (or (not ref)
8969             (equal ref ""))
8970         (error "No References in the current article")
8971       ;; For each Message-ID in the References header...
8972       (while (string-match "<[^>]*>" ref)
8973         (incf n)
8974         ;; ... fetch that article.
8975         (gnus-summary-refer-article
8976          (prog1 (match-string 0 ref)
8977            (setq ref (substring ref (match-end 0))))))
8978       (gnus-summary-goto-subject current)
8979       (gnus-summary-position-point)
8980       n)))
8981
8982 (defun gnus-delete-duplicate-headers (headers)
8983   ;; First remove leading duplicates.
8984   (while (and (> (length headers) 1)
8985               (= (mail-header-number (car headers))
8986                  (mail-header-number (cadr headers))))
8987     (pop headers))
8988   ;; Then the rest.
8989   (let ((result headers))
8990     (while (> (length headers) 1)
8991       (if (= (mail-header-number (car headers))
8992              (mail-header-number (cadr headers)))
8993           (setcdr headers (cddr headers))
8994         (pop headers)))
8995     result))
8996
8997 (defun gnus-summary-refer-thread (&optional limit)
8998   "Fetch all articles in the current thread. For backends that
8999 know how to search for threads (currently only 'nnimap) a
9000 non-numeric prefix arg will use nnir to search the entire
9001 server; without a prefix arg only the current group is
9002 searched. If the variable `gnus-refer-thread-use-nnir' is
9003 non-nil the prefix arg has the reverse meaning. If no
9004 backend-specific 'request-thread function is available fetch
9005 LIMIT (the numerical prefix) old headers. If LIMIT is
9006 non-numeric or nil fetch the number specified by the
9007 `gnus-refer-thread-limit' variable."
9008   (interactive "P")
9009   (gnus-warp-to-article)
9010   (let* ((header (gnus-summary-article-header))
9011          (id (mail-header-id header))
9012          (gnus-inhibit-demon t)
9013          (gnus-summary-ignore-duplicates t)
9014          (gnus-read-all-available-headers t)
9015          (gnus-refer-thread-use-nnir
9016           (if (and (not (null limit)) (listp limit))
9017               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9018          (new-headers
9019           (if (gnus-check-backend-function
9020                'request-thread gnus-newsgroup-name)
9021               (gnus-request-thread header gnus-newsgroup-name)
9022             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9023                             gnus-refer-thread-limit))
9024                    (last (if (numberp limit)
9025                              (min (+ (mail-header-number header)
9026                                      limit)
9027                                   gnus-newsgroup-highest)
9028                            gnus-newsgroup-highest))
9029                    (subject (gnus-simplify-subject
9030                              (mail-header-subject header)))
9031                    (refs (split-string (or (mail-header-references header)
9032                                            "")))
9033                    (gnus-parse-headers-hook
9034                     `(lambda () (goto-char (point-min))
9035                       (keep-lines
9036                        (regexp-opt ',(append refs (list id subject)))))))
9037               (gnus-fetch-headers (list last) (if (numberp limit)
9038                                                   (* 2 limit) limit) t))))
9039          article-ids)
9040     (when (listp new-headers)
9041       (dolist (header new-headers)
9042         (push (mail-header-number header) article-ids)
9043         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9044           (push (mail-header-number header) gnus-newsgroup-unreads)
9045           (setq gnus-newsgroup-unselected
9046                 (delete (mail-header-number header)
9047                         gnus-newsgroup-unselected))))
9048       (setq gnus-newsgroup-headers
9049             (gnus-delete-duplicate-headers
9050              (gnus-merge
9051               'list gnus-newsgroup-headers new-headers
9052               'gnus-article-sort-by-number)))
9053       (setq gnus-newsgroup-articles
9054             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9055       (gnus-summary-limit-include-thread id)))
9056   (gnus-summary-show-thread))
9057
9058 (defun gnus-summary-refer-article (message-id)
9059   "Fetch an article specified by MESSAGE-ID."
9060   (interactive "sMessage-ID: ")
9061   (gnus-warp-to-article)
9062   (when (and (stringp message-id)
9063              (not (zerop (length message-id))))
9064     (setq message-id (gnus-replace-in-string message-id " " ""))
9065     ;; Construct the correct Message-ID if necessary.
9066     ;; Suggested by tale@pawl.rpi.edu.
9067     (unless (string-match "^<" message-id)
9068       (setq message-id (concat "<" message-id)))
9069     (unless (string-match ">$" message-id)
9070       (setq message-id (concat message-id ">")))
9071     ;; People often post MIDs from URLs, so unhex it:
9072     (unless (string-match "@" message-id)
9073       (setq message-id (gnus-url-unhex-string message-id)))
9074     (let* ((header (gnus-id-to-header message-id))
9075            (sparse (and header
9076                         (gnus-summary-article-sparse-p
9077                          (mail-header-number header))
9078                         (memq (mail-header-number header)
9079                               gnus-newsgroup-limit)))
9080            number)
9081       (cond
9082        ;; If the article is present in the buffer we just go to it.
9083        ((and header
9084              (or (not (gnus-summary-article-sparse-p
9085                        (mail-header-number header)))
9086                  sparse))
9087         (prog1
9088             (gnus-summary-goto-article
9089              (mail-header-number header) nil t)
9090           (when sparse
9091             (gnus-summary-update-article (mail-header-number header)))))
9092        (t
9093         ;; We fetch the article.
9094         (catch 'found
9095           (dolist (gnus-override-method (gnus-refer-article-methods))
9096             (when (and (gnus-check-server gnus-override-method)
9097                        ;; Fetch the header,
9098                        (setq number (gnus-summary-insert-subject message-id)))
9099               ;; and display the article.
9100               (gnus-summary-select-article nil nil nil number)
9101               (throw 'found t)))
9102           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9103
9104 (defun gnus-refer-article-methods ()
9105   "Return a list of referable methods."
9106   (cond
9107    ;; No method, so we default to current and native.
9108    ((null gnus-refer-article-method)
9109     (list gnus-current-select-method gnus-select-method))
9110    ;; Current.
9111    ((eq 'current gnus-refer-article-method)
9112     (list gnus-current-select-method))
9113    ;; List of select methods.
9114    ((not (and (symbolp (car gnus-refer-article-method))
9115               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9116     (let (out)
9117       (dolist (method gnus-refer-article-method)
9118         (push (if (eq 'current method)
9119                   gnus-current-select-method
9120                 (if (eq 'nnir (car method))
9121                     (list
9122                      'nnir
9123                      (or (cadr method)
9124                          (gnus-method-to-server gnus-current-select-method)))
9125                   method))
9126               out))
9127       (nreverse out)))
9128    ;; One single select method.
9129    (t
9130     (list gnus-refer-article-method))))
9131
9132 (defun gnus-summary-edit-parameters ()
9133   "Edit the group parameters of the current group."
9134   (interactive)
9135   (gnus-group-edit-group gnus-newsgroup-name 'params))
9136
9137 (defun gnus-summary-customize-parameters ()
9138   "Customize the group parameters of the current group."
9139   (interactive)
9140   (gnus-group-customize gnus-newsgroup-name))
9141
9142 (defun gnus-summary-enter-digest-group (&optional force)
9143   "Enter an nndoc group based on the current article.
9144 If FORCE, force a digest interpretation.  If not, try to guess
9145 what the document format is.
9146
9147 To control what happens when you exit the group, see the
9148 `gnus-auto-select-on-ephemeral-exit' variable."
9149   (interactive "P")
9150   (let ((conf gnus-current-window-configuration))
9151     (save-window-excursion
9152       (save-excursion
9153         (let (gnus-article-prepare-hook
9154               gnus-display-mime-function
9155               gnus-break-pages)
9156           (gnus-summary-select-article))))
9157     (setq gnus-current-window-configuration conf)
9158     (let* ((name (format "%s-%d"
9159                          (gnus-group-prefixed-name
9160                           gnus-newsgroup-name (list 'nndoc ""))
9161                          (with-current-buffer gnus-summary-buffer
9162                            gnus-current-article)))
9163            (ogroup gnus-newsgroup-name)
9164            (params (append (gnus-info-params (gnus-get-info ogroup))
9165                            (list (cons 'to-group ogroup))
9166                            (list (cons 'parent-group ogroup))
9167                            (list (cons 'save-article-group ogroup))))
9168            (case-fold-search t)
9169            (buf (current-buffer))
9170            dig to-address)
9171       (with-current-buffer gnus-original-article-buffer
9172         ;; Have the digest group inherit the main mail address of
9173         ;; the parent article.
9174         (when (setq to-address (or (gnus-fetch-field "reply-to")
9175                                    (gnus-fetch-field "from")))
9176           (setq params
9177                 (append
9178                  (list (cons 'to-address
9179                              (funcall gnus-decode-encoded-address-function
9180                                       to-address))))))
9181         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9182         (insert-buffer-substring gnus-original-article-buffer)
9183         ;; Remove lines that may lead nndoc to misinterpret the
9184         ;; document type.
9185         (narrow-to-region
9186          (goto-char (point-min))
9187          (or (search-forward "\n\n" nil t) (point)))
9188         (goto-char (point-min))
9189         (delete-matching-lines "^Path:\\|^From ")
9190         (widen))
9191       (unwind-protect
9192           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
9193                     (gnus-newsgroup-ephemeral-ignored-charsets
9194                      gnus-newsgroup-ignored-charsets))
9195                 (gnus-group-read-ephemeral-group
9196                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9197                               (nndoc-article-type
9198                                ,(if force 'mbox 'guess)))
9199                  t nil nil nil
9200                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9201                                                         "ADAPT")))))
9202               ;; Make all postings to this group go to the parent group.
9203               (nconc (gnus-info-params (gnus-get-info name))
9204                      params)
9205             ;; Couldn't select this doc group.
9206             (switch-to-buffer buf)
9207             (gnus-set-global-variables)
9208             (gnus-configure-windows 'summary)
9209             (gnus-message 3 "Article couldn't be entered?"))
9210         (kill-buffer dig)))))
9211
9212 (defun gnus-summary-read-document (n)
9213   "Open a new group based on the current article(s).
9214 This will allow you to read digests and other similar
9215 documents as newsgroups.
9216 Obeys the standard process/prefix convention."
9217   (interactive "P")
9218   (let* ((ogroup gnus-newsgroup-name)
9219          (params (append (gnus-info-params (gnus-get-info ogroup))
9220                          (list (cons 'to-group ogroup))))
9221          group egroup groups vgroup)
9222     (dolist (article (gnus-summary-work-articles n))
9223       (setq group (format "%s-%d" gnus-newsgroup-name article))
9224       (gnus-summary-remove-process-mark article)
9225       (when (gnus-summary-display-article article)
9226         (save-excursion ;;What for?
9227           (with-temp-buffer
9228             (insert-buffer-substring gnus-original-article-buffer)
9229             ;; Remove some headers that may lead nndoc to make
9230             ;; the wrong guess.
9231             (message-narrow-to-head)
9232             (goto-char (point-min))
9233             (delete-matching-lines "^Path:\\|^From ")
9234             (widen)
9235             (if (setq egroup
9236                       (gnus-group-read-ephemeral-group
9237                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9238                                      (nndoc-article-type guess))
9239                        t nil t))
9240                 (progn
9241                   ;; Make all postings to this group go to the parent group.
9242                   (nconc (gnus-info-params (gnus-get-info egroup))
9243                          params)
9244                   (push egroup groups))
9245               ;; Couldn't select this doc group.
9246               (gnus-error 3 "Article couldn't be entered"))))))
9247     ;; Now we have selected all the documents.
9248     (cond
9249      ((not groups)
9250       (error "None of the articles could be interpreted as documents"))
9251      ((gnus-group-read-ephemeral-group
9252        (setq vgroup (format
9253                      "nnvirtual:%s-%s" gnus-newsgroup-name
9254                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9255        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9256        t
9257        (cons (current-buffer) 'summary)))
9258      (t
9259       (error "Couldn't select virtual nndoc group")))))
9260
9261 (defun gnus-summary-widget-forward (arg)
9262   "Move point to the next field or button in the article.
9263 With optional ARG, move across that many fields."
9264   (interactive "p")
9265   (gnus-summary-select-article)
9266   (gnus-configure-windows 'article)
9267   (select-window (gnus-get-buffer-window gnus-article-buffer))
9268   (widget-forward arg))
9269
9270 (defun gnus-summary-widget-backward (arg)
9271   "Move point to the previous field or button in the article.
9272 With optional ARG, move across that many fields."
9273   (interactive "p")
9274   (gnus-summary-select-article)
9275   (gnus-configure-windows 'article)
9276   (select-window (gnus-get-buffer-window gnus-article-buffer))
9277   (unless (widget-at (point))
9278     (goto-char (point-max)))
9279   (widget-backward arg))
9280
9281 (defun gnus-summary-isearch-article (&optional regexp-p)
9282   "Do incremental search forward on the current article.
9283 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9284   (interactive "P")
9285   (gnus-summary-select-article)
9286   (gnus-configure-windows 'article)
9287   (gnus-eval-in-buffer-window gnus-article-buffer
9288     (save-restriction
9289       (widen)
9290       (isearch-forward regexp-p))))
9291
9292 (defun gnus-summary-repeat-search-article-forward ()
9293   "Repeat the previous search forwards."
9294   (interactive)
9295   (unless gnus-last-search-regexp
9296     (error "No previous search"))
9297   (gnus-summary-search-article-forward gnus-last-search-regexp))
9298
9299 (defun gnus-summary-repeat-search-article-backward ()
9300   "Repeat the previous search backwards."
9301   (interactive)
9302   (unless gnus-last-search-regexp
9303     (error "No previous search"))
9304   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9305
9306 (defun gnus-summary-search-article-forward (regexp &optional backward)
9307   "Search for an article containing REGEXP forward.
9308 If BACKWARD, search backward instead."
9309   (interactive
9310    (list (read-string
9311           (format "Search article %s (regexp%s): "
9312                   (if current-prefix-arg "backward" "forward")
9313                   (if gnus-last-search-regexp
9314                       (concat ", default " gnus-last-search-regexp)
9315                     "")))
9316          current-prefix-arg))
9317   (if (string-equal regexp "")
9318       (setq regexp (or gnus-last-search-regexp ""))
9319     (setq gnus-last-search-regexp regexp)
9320     (setq gnus-article-before-search gnus-current-article))
9321   ;; Intentionally set gnus-last-article.
9322   (setq gnus-last-article gnus-article-before-search)
9323   (let ((gnus-last-article gnus-last-article))
9324     (if (gnus-summary-search-article regexp backward)
9325         (gnus-summary-show-thread)
9326       (signal 'search-failed (list regexp)))))
9327
9328 (defun gnus-summary-search-article-backward (regexp)
9329   "Search for an article containing REGEXP backward."
9330   (interactive
9331    (list (read-string
9332           (format "Search article backward (regexp%s): "
9333                   (if gnus-last-search-regexp
9334                       (concat ", default " gnus-last-search-regexp)
9335                     "")))))
9336   (gnus-summary-search-article-forward regexp 'backward))
9337
9338 (defun gnus-summary-search-article (regexp &optional backward)
9339   "Search for an article containing REGEXP.
9340 Optional argument BACKWARD means do search for backward.
9341 `gnus-select-article-hook' is not called during the search."
9342   ;; We have to require this here to make sure that the following
9343   ;; dynamic binding isn't shadowed by autoloading.
9344   (require 'gnus-async)
9345   (require 'gnus-art)
9346   (let ((gnus-select-article-hook nil)  ;Disable hook.
9347         (gnus-article-prepare-hook nil)
9348         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9349         (gnus-use-article-prefetch nil)
9350         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9351         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9352         (gnus-visual nil)
9353         (gnus-keep-backlog nil)
9354         (gnus-break-pages nil)
9355         (gnus-summary-display-arrow nil)
9356         (gnus-updated-mode-lines nil)
9357         (gnus-auto-center-summary nil)
9358         (sum (current-buffer))
9359         (gnus-display-mime-function nil)
9360         (found nil)
9361         point)
9362     (gnus-save-hidden-threads
9363       (gnus-summary-select-article)
9364       (set-buffer gnus-article-buffer)
9365       (goto-char (window-point (get-buffer-window (current-buffer))))
9366       (when backward
9367         (forward-line -1))
9368       (while (not found)
9369         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9370         (if (if backward
9371                 (re-search-backward regexp nil t)
9372               (re-search-forward regexp nil t))
9373             ;; We found the regexp.
9374             (progn
9375               (setq found 'found)
9376               (beginning-of-line)
9377               (set-window-start
9378                (get-buffer-window (current-buffer))
9379                (point))
9380               (forward-line 1)
9381               (set-window-point
9382                (get-buffer-window (current-buffer))
9383                (point))
9384               (set-buffer sum)
9385               (setq point (point)))
9386           ;; We didn't find it, so we go to the next article.
9387           (set-buffer sum)
9388           (setq found 'not)
9389           (while (eq found 'not)
9390             (if (not (if backward (gnus-summary-find-prev)
9391                        (gnus-summary-find-next)))
9392                 ;; No more articles.
9393                 (setq found t)
9394               ;; Select the next article and adjust point.
9395               (unless (gnus-summary-article-sparse-p
9396                        (gnus-summary-article-number))
9397                 (setq found nil)
9398                 (gnus-summary-select-article)
9399                 (set-buffer gnus-article-buffer)
9400                 (widen)
9401                 (goto-char (if backward (point-max) (point-min))))))))
9402       (gnus-message 7 ""))
9403     ;; Return whether we found the regexp.
9404     (when (eq found 'found)
9405       (goto-char point)
9406       (gnus-summary-show-thread)
9407       (gnus-summary-goto-subject gnus-current-article)
9408       (gnus-summary-position-point)
9409       t)))
9410
9411 (defun gnus-find-matching-articles (header regexp)
9412   "Return a list of all articles that match REGEXP on HEADER.
9413 This search includes all articles in the current group that Gnus has
9414 fetched headers for, whether they are displayed or not."
9415   (let ((articles nil)
9416         ;; Can't eta-reduce because it's a macro.
9417         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9418         (case-fold-search t))
9419     (dolist (header gnus-newsgroup-headers)
9420       (when (string-match regexp (funcall func header))
9421         (push (mail-header-number header) articles)))
9422     (nreverse articles)))
9423
9424 (defun gnus-summary-find-matching (header regexp &optional backward unread
9425                                           not-case-fold not-matching)
9426   "Return a list of all articles that match REGEXP on HEADER.
9427 The search stars on the current article and goes forwards unless
9428 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9429 If UNREAD is non-nil, only unread articles will
9430 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9431 in the comparisons. If NOT-MATCHING, return a list of all articles that
9432 not match REGEXP on HEADER."
9433   (let ((case-fold-search (not not-case-fold))
9434         articles d func)
9435     (if (consp header)
9436         (if (eq (car header) 'extra)
9437             (setq func
9438                   `(lambda (h)
9439                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9440                          "")))
9441           (error "%s is an invalid header" header))
9442       (unless (fboundp (intern (concat "mail-header-" header)))
9443         (error "%s is not a valid header" header))
9444       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9445     (dolist (d (if (eq backward 'all)
9446                    gnus-newsgroup-data
9447                  (gnus-data-find-list
9448                   (gnus-summary-article-number)
9449                   (gnus-data-list backward))))
9450       (when (and (or (not unread)       ; We want all articles...
9451                      (gnus-data-unread-p d)) ; Or just unreads.
9452                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9453                  (if not-matching
9454                      (not (string-match
9455                            regexp
9456                            (funcall func (gnus-data-header d))))
9457                    (string-match regexp
9458                                  (funcall func (gnus-data-header d)))))
9459         (push (gnus-data-number d) articles))) ; Success!
9460     (nreverse articles)))
9461
9462 (defun gnus-summary-execute-command (header regexp command &optional backward)
9463   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9464 If HEADER is an empty string (or nil), the match is done on the entire
9465 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9466   (interactive
9467    (list (let ((completion-ignore-case t))
9468            (gnus-completing-read
9469             "Header name"
9470             (mapcar 'symbol-name
9471                     (append
9472                      '(Number Subject From Lines Date
9473                        Message-ID Xref References Body)
9474                      gnus-extra-headers))
9475             'require-match))
9476          (read-string "Regexp: ")
9477          (read-key-sequence "Command: ")
9478          current-prefix-arg))
9479   (when (equal header "Body")
9480     (setq header ""))
9481   ;; Hidden thread subtrees must be searched as well.
9482   (gnus-summary-show-all-threads)
9483   ;; We don't want to change current point nor window configuration.
9484   (save-excursion
9485     (save-window-excursion
9486       (let (gnus-visual
9487             gnus-treat-strip-trailing-blank-lines
9488             gnus-treat-strip-leading-blank-lines
9489             gnus-treat-strip-multiple-blank-lines
9490             gnus-treat-hide-boring-headers
9491             gnus-treat-fold-newsgroups
9492             gnus-article-prepare-hook)
9493         (gnus-message 6 "Executing %s..." (key-description command))
9494         ;; We'd like to execute COMMAND interactively so as to give arguments.
9495         (gnus-execute header regexp
9496                       `(call-interactively ',(key-binding command))
9497                       backward)
9498         (gnus-message 6 "Executing %s...done" (key-description command))))))
9499
9500 (defun gnus-summary-beginning-of-article ()
9501   "Scroll the article back to the beginning."
9502   (interactive)
9503   (gnus-summary-select-article)
9504   (gnus-configure-windows 'article)
9505   (gnus-eval-in-buffer-window gnus-article-buffer
9506     (widen)
9507     (goto-char (point-min))
9508     (when gnus-break-pages
9509       (gnus-narrow-to-page))))
9510
9511 (defun gnus-summary-end-of-article ()
9512   "Scroll to the end of the article."
9513   (interactive)
9514   (gnus-summary-select-article)
9515   (gnus-configure-windows 'article)
9516   (gnus-eval-in-buffer-window gnus-article-buffer
9517     (widen)
9518     (goto-char (point-max))
9519     (recenter -3)
9520     (when gnus-break-pages
9521       (gnus-narrow-to-page))))
9522
9523 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9524   "Truncate to LEN and quote all \"(\"'s in STRING."
9525   (gnus-replace-in-string (if (and len (> (length string) len))
9526                               (substring string 0 len)
9527                             string)
9528                           "[()]" "\\\\\\&"))
9529
9530 (defun gnus-summary-print-article (&optional filename n)
9531   "Generate and print a PostScript image of the process-marked (mail) articles.
9532
9533 If used interactively, print the current article if none are
9534 process-marked.  With prefix arg, prompt the user for the name of the
9535 file to save in.
9536
9537 When used from Lisp, accept two optional args FILENAME and N.  N means
9538 to print the next N articles.  If N is negative, print the N previous
9539 articles.  If N is nil and articles have been marked with the process
9540 mark, print these instead.
9541
9542 If the optional first argument FILENAME is nil, send the image to the
9543 printer.  If FILENAME is a string, save the PostScript image in a file with
9544 that name.  If FILENAME is a number, prompt the user for the name of the file
9545 to save in."
9546   (interactive (list (ps-print-preprint current-prefix-arg)))
9547   (dolist (article (gnus-summary-work-articles n))
9548     (gnus-summary-select-article nil nil 'pseudo article)
9549     (gnus-eval-in-buffer-window gnus-article-buffer
9550       (gnus-print-buffer))
9551     (gnus-summary-remove-process-mark article))
9552   (ps-despool filename))
9553
9554 (defun gnus-print-buffer ()
9555   (let ((ps-left-header
9556          (list
9557           (concat "("
9558                   (gnus-summary-print-truncate-and-quote
9559                    (mail-header-subject gnus-current-headers)
9560                    66) ")")
9561           (concat "("
9562                   (gnus-summary-print-truncate-and-quote
9563                    (mail-header-from gnus-current-headers)
9564                    45) ")")))
9565         (ps-right-header
9566          (list
9567           "/pagenumberstring load"
9568           (concat "("
9569                   (mail-header-date gnus-current-headers) ")"))))
9570     (gnus-run-hooks 'gnus-ps-print-hook)
9571     (save-excursion
9572       (if ps-print-color-p
9573           (ps-spool-buffer-with-faces)
9574         (ps-spool-buffer)))))
9575
9576 (defun gnus-summary-show-complete-article ()
9577   "Show a complete version of the current article.
9578 This is only useful if you're looking at a partial version of the
9579 article currently."
9580   (interactive)
9581   (let ((gnus-keep-backlog nil)
9582         (gnus-use-cache nil)
9583         (gnus-agent nil)
9584         (variable (intern
9585                    (format "%s-fetch-partial-articles"
9586                            (car (gnus-find-method-for-group
9587                                  gnus-newsgroup-name)))
9588                    obarray))
9589         old-val)
9590     (unwind-protect
9591         (progn
9592           (setq old-val (symbol-value variable))
9593           (set variable nil)
9594           (gnus-flush-original-article-buffer)
9595           (gnus-summary-show-article))
9596       (set variable old-val))))
9597
9598 (defun gnus-summary-show-article (&optional arg)
9599   "Force redisplaying of the current article.
9600 If ARG (the prefix) is a number, show the article with the charset
9601 defined in `gnus-summary-show-article-charset-alist', or the charset
9602 input.
9603 If ARG (the prefix) is non-nil and not a number, show the article,
9604 but without running any of the article treatment functions
9605 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9606 C-u g', show the raw article."
9607   (interactive "P")
9608   (cond
9609    ((numberp arg)
9610     (gnus-summary-show-article t)
9611     (let ((gnus-newsgroup-charset
9612            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9613                (mm-read-coding-system
9614                 "View as charset: " ;; actually it is coding system.
9615                 (with-current-buffer gnus-article-buffer
9616                   (mm-detect-coding-region (point) (point-max))))))
9617           (gnus-newsgroup-ignored-charsets 'gnus-all))
9618       (gnus-summary-select-article nil 'force)
9619       (let ((deps gnus-newsgroup-dependencies)
9620             head header lines)
9621         (with-current-buffer gnus-original-article-buffer
9622           (save-restriction
9623             (message-narrow-to-head)
9624             (setq head (buffer-string))
9625             (goto-char (point-min))
9626             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9627               (goto-char (point-max))
9628               (widen)
9629               (setq lines (1- (count-lines (point) (point-max))))))
9630           (with-temp-buffer
9631             (insert (format "211 %d Article retrieved.\n"
9632                             (cdr gnus-article-current)))
9633             (insert head)
9634             (if lines (insert (format "Lines: %d\n" lines)))
9635             (insert ".\n")
9636             (let ((nntp-server-buffer (current-buffer)))
9637               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9638         (gnus-data-set-header
9639          (gnus-data-find (cdr gnus-article-current))
9640          header)
9641         (gnus-summary-update-article-line
9642          (cdr gnus-article-current) header)
9643         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9644           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9645    ((not arg)
9646     ;; Select the article the normal way.
9647     (if (eq mm-text-html-renderer 'shr)
9648         (progn
9649           (require 'shr)
9650           (let ((shr-ignore-cache t))
9651             (gnus-summary-select-article nil 'force)))
9652       (gnus-summary-select-article nil 'force)))
9653    ((equal arg '(16))
9654     ;; C-u C-u g
9655     (let ((gnus-inhibit-article-treatments t))
9656       (gnus-summary-select-article nil 'force)))
9657    (t
9658     ;; We have to require this here to make sure that the following
9659     ;; dynamic binding isn't shadowed by autoloading.
9660     (require 'gnus-async)
9661     (require 'gnus-art)
9662     ;; Bind the article treatment functions to nil.
9663     (let ((gnus-have-all-headers t)
9664           gnus-article-prepare-hook
9665           gnus-article-decode-hook
9666           gnus-display-mime-function
9667           gnus-break-pages)
9668       ;; Destroy any MIME parts.
9669       (when (gnus-buffer-live-p gnus-article-buffer)
9670         (with-current-buffer gnus-article-buffer
9671           (gnus-article-stop-animations)
9672           (gnus-stop-downloads)
9673           (mm-destroy-parts gnus-article-mime-handles)
9674           ;; Set it to nil for safety reason.
9675           (setq gnus-article-mime-handle-alist nil)
9676           (setq gnus-article-mime-handles nil)))
9677       (gnus-summary-select-article nil 'force))))
9678   (gnus-summary-goto-subject gnus-current-article)
9679   (gnus-summary-position-point))
9680
9681 (defun gnus-summary-show-raw-article ()
9682   "Show the raw article without any article massaging functions being run."
9683   (interactive)
9684   (gnus-summary-show-article t))
9685
9686 (defun gnus-summary-verbose-headers (&optional arg)
9687   "Toggle permanent full header display.
9688 If ARG is a positive number, turn header display on.
9689 If ARG is a negative number, turn header display off."
9690   (interactive "P")
9691   (setq gnus-show-all-headers
9692         (cond ((or (not (numberp arg))
9693                    (zerop arg))
9694                (not gnus-show-all-headers))
9695               ((natnump arg)
9696                t)))
9697   (gnus-summary-show-article))
9698
9699 (defun gnus-summary-toggle-header (&optional arg)
9700   "Show the headers if they are hidden, or hide them if they are shown.
9701 If ARG is a positive number, show the entire header.
9702 If ARG is a negative number, hide the unwanted header lines."
9703   (interactive "P")
9704   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9705                      (get-buffer-window gnus-article-buffer t))))
9706     (with-current-buffer gnus-article-buffer
9707       (widen)
9708       (article-narrow-to-head)
9709       (let* ((inhibit-read-only t)
9710              (inhibit-point-motion-hooks t)
9711              (hidden (if (numberp arg)
9712                          (>= arg 0)
9713                        (or (not (looking-at "[^ \t\n]+:"))
9714                            (gnus-article-hidden-text-p 'headers))))
9715              s e)
9716         (delete-region (point-min) (point-max))
9717         (with-current-buffer gnus-original-article-buffer
9718           (goto-char (setq s (point-min)))
9719           (setq e (if (search-forward "\n\n" nil t)
9720                       (1- (point))
9721                     (point-max))))
9722         (insert-buffer-substring gnus-original-article-buffer s e)
9723         (run-hooks 'gnus-article-decode-hook)
9724         (if hidden
9725             (let ((gnus-treat-hide-headers nil)
9726                   (gnus-treat-hide-boring-headers nil))
9727               (gnus-delete-wash-type 'headers)
9728               (gnus-treat-article 'head))
9729           (gnus-treat-article 'head))
9730         (widen)
9731         (if window
9732             (set-window-start window (goto-char (point-min))))
9733         (if gnus-break-pages
9734             (gnus-narrow-to-page)
9735           (when (gnus-visual-p 'page-marker)
9736             (let ((inhibit-read-only t))
9737               (gnus-remove-text-with-property 'gnus-prev)
9738               (gnus-remove-text-with-property 'gnus-next))))
9739         (gnus-set-mode-line 'article)))))
9740
9741 (defun gnus-summary-show-all-headers ()
9742   "Make all header lines visible."
9743   (interactive)
9744   (gnus-summary-toggle-header 1))
9745
9746 (defun gnus-summary-caesar-message (&optional arg)
9747   "Caesar rotate the current article by 13.
9748 With a non-numerical prefix, also rotate headers.  A numerical
9749 prefix specifies how many places to rotate each letter forward."
9750   (interactive "P")
9751   (gnus-summary-select-article)
9752   (let ((mail-header-separator ""))
9753     (gnus-eval-in-buffer-window gnus-article-buffer
9754       (save-restriction
9755         (widen)
9756         (let ((start (window-start))
9757               (inhibit-read-only t))
9758           (if (equal arg '(4))
9759               (message-caesar-buffer-body nil t)
9760             (message-caesar-buffer-body arg))
9761           (set-window-start (get-buffer-window (current-buffer)) start)))))
9762   ;; Create buttons and stuff...
9763   (gnus-treat-article nil))
9764
9765 (declare-function idna-to-unicode "ext:idna" (str))
9766
9767 (defun gnus-summary-idna-message (&optional arg)
9768   "Decode IDNA encoded domain names in the current articles.
9769 IDNA encoded domain names looks like `xn--bar'.  If a string
9770 remain unencoded after running this function, it is likely an
9771 invalid IDNA string (`xn--bar' is invalid).
9772
9773 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9774 installed for this command to work."
9775   (interactive "P")
9776   (if (not (and (condition-case nil (require 'idna)
9777                   (file-error))
9778                 (mm-coding-system-p 'utf-8)
9779                 (executable-find (symbol-value 'idna-program))))
9780       (gnus-message
9781        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9782     (gnus-summary-select-article)
9783     (let ((mail-header-separator ""))
9784       (gnus-eval-in-buffer-window gnus-article-buffer
9785         (save-restriction
9786           (widen)
9787           (let ((start (window-start))
9788                 buffer-read-only)
9789             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9790               (replace-match (idna-to-unicode (match-string 1))))
9791             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9792
9793 (defun gnus-summary-morse-message (&optional arg)
9794   "Morse decode the current article."
9795   (interactive "P")
9796   (gnus-summary-select-article)
9797   (let ((mail-header-separator ""))
9798     (gnus-eval-in-buffer-window gnus-article-buffer
9799       (save-excursion
9800         (save-restriction
9801           (widen)
9802           (let ((pos (window-start))
9803                 (inhibit-read-only t))
9804             (goto-char (point-min))
9805             (when (message-goto-body)
9806               (gnus-narrow-to-body))
9807             (goto-char (point-min))
9808             (while (search-forward "·" (point-max) t)
9809               (replace-match "."))
9810             (unmorse-region (point-min) (point-max))
9811             (widen)
9812             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9813
9814 (defun gnus-summary-stop-page-breaking ()
9815   "Stop page breaking in the current article."
9816   (interactive)
9817   (gnus-summary-select-article)
9818   (gnus-eval-in-buffer-window gnus-article-buffer
9819     (widen)
9820     (when (gnus-visual-p 'page-marker)
9821       (let ((inhibit-read-only t))
9822         (gnus-remove-text-with-property 'gnus-prev)
9823         (gnus-remove-text-with-property 'gnus-next))
9824       (setq gnus-page-broken nil))))
9825
9826 (defun gnus-summary-move-article (&optional n to-newsgroup
9827                                             select-method action)
9828   "Move the current article to a different newsgroup.
9829 If N is a positive number, move the N next articles.
9830 If N is a negative number, move the N previous articles.
9831 If N is nil and any articles have been marked with the process mark,
9832 move those articles instead.
9833 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9834 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9835 re-spool using this method.
9836
9837 When called interactively with TO-NEWSGROUP being nil, the value of
9838 the variable `gnus-move-split-methods' is used for finding a default
9839 for the target newsgroup.
9840
9841 For this function to work, both the current newsgroup and the
9842 newsgroup that you want to move to have to support the `request-move'
9843 and `request-accept' functions.
9844
9845 ACTION can be either `move' (the default), `crosspost' or `copy'."
9846   (interactive "P")
9847   (unless action
9848     (setq action 'move))
9849   ;; Check whether the source group supports the required functions.
9850   (cond ((and (eq action 'move)
9851               (not (gnus-check-backend-function
9852                     'request-move-article gnus-newsgroup-name)))
9853          (error "The current group does not support article moving"))
9854         ((and (eq action 'crosspost)
9855               (not (gnus-check-backend-function
9856                     'request-replace-article gnus-newsgroup-name)))
9857          (error "The current group does not support article editing")))
9858   (let ((articles (gnus-summary-work-articles n))
9859         (prefix (if (gnus-check-backend-function
9860                      'request-move-article gnus-newsgroup-name)
9861                     (funcall gnus-move-group-prefix-function
9862                              gnus-newsgroup-name)
9863                   ""))
9864         (names '((move "Move" "Moving")
9865                  (copy "Copy" "Copying")
9866                  (crosspost "Crosspost" "Crossposting")))
9867         (copy-buf (save-excursion
9868                     (nnheader-set-temp-buffer " *copy article*")))
9869         art-group to-method new-xref article to-groups
9870         articles-to-update-marks encoded)
9871     (unless (assq action names)
9872       (error "Unknown action %s" action))
9873     ;; Read the newsgroup name.
9874     (when (and (not to-newsgroup)
9875                (not select-method))
9876       (if (and gnus-move-split-methods
9877                (not
9878                 (and (memq gnus-current-article articles)
9879                      (gnus-buffer-live-p gnus-original-article-buffer))))
9880           ;; When `gnus-move-split-methods' is non-nil, we have to
9881           ;; select an article to give `gnus-read-move-group-name' an
9882           ;; opportunity to suggest an appropriate default.  However,
9883           ;; we needn't render or mark the article.
9884           (let ((gnus-display-mime-function nil)
9885                 (gnus-article-prepare-hook nil)
9886                 (gnus-mark-article-hook nil))
9887             (gnus-summary-select-article nil nil nil (car articles))))
9888       (setq to-newsgroup (gnus-read-move-group-name
9889                           (cadr (assq action names))
9890                           (symbol-value
9891                            (intern (format "gnus-current-%s-group" action)))
9892                           articles prefix)
9893             encoded to-newsgroup
9894             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9895       (set (intern (format "gnus-current-%s-group" action))
9896            (mm-decode-coding-string
9897             to-newsgroup
9898             (gnus-group-name-charset to-method to-newsgroup))))
9899     (unless to-method
9900       (setq to-method (or select-method
9901                           (gnus-server-to-method
9902                            (gnus-group-method to-newsgroup)))))
9903     (setq to-newsgroup
9904           (or encoded
9905               (and to-newsgroup
9906                    (mm-encode-coding-string
9907                     to-newsgroup
9908                     (gnus-group-name-charset to-method to-newsgroup)))))
9909     ;; Check the method we are to move this article to...
9910     (unless (gnus-check-backend-function
9911              'request-accept-article (car to-method))
9912       (error "%s does not support article copying" (car to-method)))
9913     (unless (gnus-check-server to-method)
9914       (error "Can't open server %s" (car to-method)))
9915     (gnus-message 6 "%s to %s: %s..."
9916                   (caddr (assq action names))
9917                   (or (car select-method)
9918                       (gnus-group-decoded-name to-newsgroup))
9919                   articles)
9920     (while articles
9921       (setq article (pop articles))
9922       ;; Set any marks that may have changed in the summary buffer.
9923       (when gnus-preserve-marks
9924         (gnus-summary-push-marks-to-backend article))
9925       (setq
9926        art-group
9927        (cond
9928         ;; Move the article.
9929         ((eq action 'move)
9930          ;; Remove this article from future suppression.
9931          (gnus-dup-unsuppress-article article)
9932          (let* ((from-method (gnus-find-method-for-group
9933                               gnus-newsgroup-name))
9934                 (to-method (or select-method
9935                                (gnus-find-method-for-group to-newsgroup)))
9936                 (move-is-internal (gnus-server-equal from-method to-method)))
9937            (gnus-request-move-article
9938             article                     ; Article to move
9939             gnus-newsgroup-name         ; From newsgroup
9940             (nth 1 (gnus-find-method-for-group
9941                     gnus-newsgroup-name)) ; Server
9942             (list 'gnus-request-accept-article
9943                   to-newsgroup (list 'quote select-method)
9944                   (not articles) t)     ; Accept form
9945             (not articles)              ; Only save nov last time
9946             (and move-is-internal
9947                  to-newsgroup           ; Not respooling
9948                                         ; Is this move internal?
9949                  (gnus-group-real-name to-newsgroup)))))
9950         ;; Copy the article.
9951         ((eq action 'copy)
9952          (with-current-buffer copy-buf
9953            (when (gnus-request-article-this-buffer article
9954                                                    gnus-newsgroup-name)
9955              (save-restriction
9956                (nnheader-narrow-to-headers)
9957                (dolist (hdr gnus-copy-article-ignored-headers)
9958                  (message-remove-header hdr t)))
9959              (gnus-request-accept-article
9960               to-newsgroup select-method (not articles) t))))
9961         ;; Crosspost the article.
9962         ((eq action 'crosspost)
9963          (let ((xref (message-tokenize-header
9964                       (mail-header-xref (gnus-summary-article-header
9965                                          article))
9966                       " ")))
9967            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9968                                   ":" (number-to-string article)))
9969            (unless xref
9970              (setq xref (list (system-name))))
9971            (setq new-xref
9972                  (concat
9973                   (mapconcat 'identity
9974                              (delete "Xref:" (delete new-xref xref))
9975                              " ")
9976                   " " new-xref))
9977            (with-current-buffer copy-buf
9978              ;; First put the article in the destination group.
9979              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9980              (when (consp (setq art-group
9981                                 (gnus-request-accept-article
9982                                  to-newsgroup select-method (not articles)
9983                                  t)))
9984                (setq new-xref (concat new-xref " " (car art-group)
9985                                       ":"
9986                                       (number-to-string (cdr art-group))))
9987                ;; Now we have the new Xrefs header, so we insert
9988                ;; it and replace the new article.
9989                (nnheader-replace-header "Xref" new-xref)
9990                (gnus-request-replace-article
9991                 (cdr art-group) to-newsgroup (current-buffer) t)
9992                art-group))))))
9993       (cond
9994        ((not art-group)
9995         (gnus-message 1 "Couldn't %s article %s: %s"
9996                       (cadr (assq action names)) article
9997                       (nnheader-get-report (car to-method))))
9998        ((eq art-group 'junk)
9999         (when (eq action 'move)
10000           (gnus-summary-mark-article article gnus-canceled-mark)
10001           (gnus-message 4 "Deleted article %s" article)
10002           ;; run the delete hook
10003           (run-hook-with-args 'gnus-summary-article-delete-hook
10004                               action
10005                               (gnus-data-header
10006                                (assoc article (gnus-data-list nil)))
10007                               gnus-newsgroup-name nil
10008                               select-method)))
10009        (t
10010         (let* ((pto-group (gnus-group-prefixed-name
10011                            (car art-group) to-method))
10012                (info (gnus-get-info pto-group))
10013                (to-group (gnus-info-group info))
10014                to-marks)
10015           ;; Update the group that has been moved to.
10016           (when (and info
10017                      (memq action '(move copy)))
10018             (unless (member to-group to-groups)
10019               (push to-group to-groups))
10020
10021             (when (and (not (memq article gnus-newsgroup-unreads))
10022                        (cdr art-group))
10023               (push 'read to-marks)
10024               (gnus-info-set-read
10025                info (gnus-add-to-range (gnus-info-read info)
10026                                        (list (cdr art-group)))))
10027
10028             ;; See whether the article is to be put in the cache.
10029             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10030                    (marks (if expirable
10031                               gnus-article-mark-lists
10032                             (delete '(expirable . expire)
10033                                     (copy-sequence
10034                                      gnus-article-mark-lists))))
10035                    (to-article (cdr art-group)))
10036
10037               ;; Enter the article into the cache in the new group,
10038               ;; if that is required.
10039               (when (and to-article
10040                          gnus-use-cache)
10041                 (gnus-cache-possibly-enter-article
10042                  to-group to-article
10043                  (memq article gnus-newsgroup-marked)
10044                  (memq article gnus-newsgroup-dormant)
10045                  (memq article gnus-newsgroup-unreads)))
10046
10047               (when (and gnus-preserve-marks
10048                          to-article)
10049                 ;; Copy any marks over to the new group.
10050                 (when (and (equal to-group gnus-newsgroup-name)
10051                            (not (memq article gnus-newsgroup-unreads)))
10052                   ;; Mark this article as read in this group.
10053                   (push (cons to-article gnus-read-mark)
10054                         gnus-newsgroup-reads)
10055                   ;; Increase the active status of this group.
10056                   (setcdr (gnus-active to-group) to-article)
10057                   (setcdr gnus-newsgroup-active to-article))
10058
10059                 (while marks
10060                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10061                     (when (memq article (symbol-value
10062                                          (intern (format "gnus-newsgroup-%s"
10063                                                          (caar marks)))))
10064                       (push (cdar marks) to-marks)
10065                       ;; If the other group is the same as this group,
10066                       ;; then we have to add the mark to the list.
10067                       (when (equal to-group gnus-newsgroup-name)
10068                         (set (intern (format "gnus-newsgroup-%s"
10069                                              (caar marks)))
10070                              (cons to-article
10071                                    (symbol-value
10072                                     (intern (format "gnus-newsgroup-%s"
10073                                                     (caar marks)))))))
10074                       ;; Copy the marks to other group.
10075                       (gnus-add-marked-articles
10076                        to-group (cdar marks) (list to-article) info)))
10077                   (setq marks (cdr marks)))
10078
10079                 (when (and expirable
10080                            gnus-mark-copied-or-moved-articles-as-expirable
10081                            (not (memq 'expire to-marks)))
10082                   ;; Mark this article as expirable.
10083                   (push 'expire to-marks)
10084                   (when (equal to-group gnus-newsgroup-name)
10085                     (push to-article gnus-newsgroup-expirable))
10086                   ;; Copy the expirable mark to other group.
10087                   (gnus-add-marked-articles
10088                    to-group 'expire (list to-article) info))
10089
10090                 (when (and to-marks
10091                            (gnus-method-option-p
10092                             (gnus-find-method-for-group to-group)
10093                             'server-marks))
10094                   (gnus-request-set-mark
10095                    to-group (list (list (list to-article) 'add to-marks)))))
10096
10097               (gnus-dribble-enter
10098                (concat "(gnus-group-set-info '"
10099                        (gnus-prin1-to-string (gnus-get-info to-group))
10100                        ")")
10101                (concat "^(gnus-group-set-info '(\""
10102                        (regexp-quote to-group) "\""))))
10103
10104           ;; Update the Xref header in this article to point to
10105           ;; the new crossposted article we have just created.
10106           (when (eq action 'crosspost)
10107             (with-current-buffer copy-buf
10108               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10109               (nnheader-replace-header "Xref" new-xref)
10110               (gnus-request-replace-article
10111                article gnus-newsgroup-name (current-buffer) t)))
10112
10113           ;; run the move/copy/crosspost/respool hook
10114           (run-hook-with-args 'gnus-summary-article-move-hook
10115                               action
10116                               (gnus-data-header
10117                                (assoc article (gnus-data-list nil)))
10118                               gnus-newsgroup-name
10119                               to-newsgroup
10120                               select-method))
10121
10122         ;;;!!!Why is this necessary?
10123         (set-buffer gnus-summary-buffer)
10124
10125         (when (eq action 'move)
10126           (save-excursion
10127             (gnus-summary-goto-subject article)
10128             (gnus-summary-mark-article article gnus-canceled-mark)))))
10129       (push article articles-to-update-marks))
10130
10131     (save-excursion
10132       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10133     ;; Re-activate all groups that have been moved to.
10134     (with-current-buffer gnus-group-buffer
10135       (let ((gnus-group-marked to-groups))
10136         (gnus-group-get-new-news-this-group nil t)))
10137
10138     (gnus-kill-buffer copy-buf)
10139     (gnus-summary-position-point)
10140     (gnus-set-mode-line 'summary)))
10141
10142 (defun gnus-summary-push-marks-to-backend (article)
10143   (let ((set nil)
10144         (marks gnus-article-mark-lists))
10145     (unless (memq article gnus-newsgroup-unreads)
10146       (push 'read set))
10147     (while marks
10148       (when (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10149                  (memq article (symbol-value
10150                                 (intern (format "gnus-newsgroup-%s"
10151                                                 (caar marks))))))
10152         (push (cdar marks) set))
10153       (pop marks))
10154     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)))))
10155
10156 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10157   "Copy the current article to some other group.
10158 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10159 When called interactively, if TO-NEWSGROUP is nil, use the value of
10160 the variable `gnus-move-split-methods' for finding a default target
10161 newsgroup.
10162 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10163 re-spool using this method."
10164   (interactive "P")
10165   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10166
10167 (defun gnus-summary-crosspost-article (&optional n)
10168   "Crosspost the current article to some other group."
10169   (interactive "P")
10170   (gnus-summary-move-article n nil nil 'crosspost))
10171
10172 (defcustom gnus-summary-respool-default-method nil
10173   "Default method type for respooling an article.
10174 If nil, use to the current newsgroup method."
10175   :type 'symbol
10176   :group 'gnus-summary-mail)
10177
10178 (defun gnus-summary-respool-article (&optional n method)
10179   "Respool the current article.
10180 The article will be squeezed through the mail spooling process again,
10181 which means that it will be put in some mail newsgroup or other
10182 depending on `nnmail-split-methods'.
10183 If N is a positive number, respool the N next articles.
10184 If N is a negative number, respool the N previous articles.
10185 If N is nil and any articles have been marked with the process mark,
10186 respool those articles instead.
10187
10188 Respooling can be done both from mail groups and \"real\" newsgroups.
10189 In the former case, the articles in question will be moved from the
10190 current group into whatever groups they are destined to.  In the
10191 latter case, they will be copied into the relevant groups."
10192   (interactive
10193    (list current-prefix-arg
10194          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10195                 (methname
10196                  (symbol-name (or gnus-summary-respool-default-method
10197                                   (car (gnus-find-method-for-group
10198                                         gnus-newsgroup-name)))))
10199                 (method
10200                  (gnus-completing-read
10201                   "Backend to use when respooling"
10202                   methods t nil 'gnus-mail-method-history methname))
10203                 ms)
10204            (cond
10205             ((zerop (length (setq ms (gnus-servers-using-backend
10206                                       (intern method)))))
10207              (list (intern method) ""))
10208             ((= 1 (length ms))
10209              (car ms))
10210             (t
10211              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10212                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10213                            ms-alist))))))))
10214   (unless method
10215     (error "No method given for respooling"))
10216   (if (assoc (symbol-name
10217               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10218              (gnus-methods-using 'respool))
10219       (gnus-summary-move-article n nil method)
10220     (gnus-summary-copy-article n nil method)))
10221
10222 (defun gnus-summary-import-article (file &optional edit)
10223   "Import an arbitrary file into a mail newsgroup."
10224   (interactive "fImport file: \nP")
10225   (let ((group gnus-newsgroup-name)
10226         (now (current-time))
10227         atts lines group-art)
10228     (unless (gnus-check-backend-function 'request-accept-article group)
10229       (error "%s does not support article importing" group))
10230     (or (file-readable-p file)
10231         (not (file-regular-p file))
10232         (error "Can't read %s" file))
10233     (with-current-buffer (gnus-get-buffer-create " *import file*")
10234       (erase-buffer)
10235       (nnheader-insert-file-contents file)
10236       (goto-char (point-min))
10237       (if (nnheader-article-p)
10238           (save-restriction
10239             (goto-char (point-min))
10240             (search-forward "\n\n" nil t)
10241             (narrow-to-region (point-min) (1- (point)))
10242             (goto-char (point-min))
10243             (unless (re-search-forward "^date:" nil t)
10244               (goto-char (point-max))
10245               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10246        ;; This doesn't look like an article, so we fudge some headers.
10247         (setq atts (file-attributes file)
10248               lines (count-lines (point-min) (point-max)))
10249         (insert "From: " (read-string "From: ") "\n"
10250                 "Subject: " (read-string "Subject: ") "\n"
10251                 "Date: " (message-make-date (nth 5 atts)) "\n"
10252                 "Message-ID: " (message-make-message-id) "\n"
10253                 "Lines: " (int-to-string lines) "\n"
10254                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10255       (setq group-art (gnus-request-accept-article group nil t))
10256       (kill-buffer (current-buffer)))
10257     (setq gnus-newsgroup-active (gnus-activate-group group))
10258     (forward-line 1)
10259     (gnus-summary-goto-article (cdr group-art) nil t)
10260     (when edit
10261       (gnus-summary-edit-article))))
10262
10263 (defun gnus-summary-create-article ()
10264   "Create an article in a mail newsgroup."
10265   (interactive)
10266   (let ((group gnus-newsgroup-name)
10267         (now (current-time))
10268         group-art)
10269     (unless (gnus-check-backend-function 'request-accept-article group)
10270       (error "%s does not support article importing" group))
10271     (with-current-buffer (gnus-get-buffer-create " *import file*")
10272       (erase-buffer)
10273       (goto-char (point-min))
10274       ;; This doesn't look like an article, so we fudge some headers.
10275       (insert "From: " (read-string "From: ") "\n"
10276               "Subject: " (read-string "Subject: ") "\n"
10277               "Date: " (message-make-date now) "\n"
10278               "Message-ID: " (message-make-message-id) "\n")
10279       (setq group-art (gnus-request-accept-article group nil t))
10280       (kill-buffer (current-buffer)))
10281     (setq gnus-newsgroup-active (gnus-activate-group group))
10282     (forward-line 1)
10283     (gnus-summary-goto-article (cdr group-art) nil t)
10284     (gnus-summary-edit-article)))
10285
10286 (defun gnus-summary-article-posted-p ()
10287   "Say whether the current (mail) article is available from news as well.
10288 This will be the case if the article has both been mailed and posted."
10289   (interactive)
10290   (let ((id (mail-header-references (gnus-summary-article-header)))
10291         (gnus-override-method (car (gnus-refer-article-methods))))
10292     (if (gnus-request-head id "")
10293         (gnus-message 2 "The current message was found on %s"
10294                       gnus-override-method)
10295       (gnus-message 2 "The current message couldn't be found on %s"
10296                     gnus-override-method)
10297       nil)))
10298
10299 (defun gnus-summary-expire-articles (&optional now)
10300   "Expire all articles that are marked as expirable in the current group."
10301   (interactive)
10302   (when (and (not gnus-group-is-exiting-without-update-p)
10303              (gnus-check-backend-function
10304               'request-expire-articles gnus-newsgroup-name))
10305     ;; This backend supports expiry.
10306     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10307            (expirable
10308             (gnus-list-range-difference
10309              (if total
10310                  (progn
10311                    ;; We need to update the info for
10312                    ;; this group for `gnus-list-of-read-articles'
10313                    ;; to give us the right answer.
10314                    (gnus-run-hooks 'gnus-exit-group-hook)
10315                    (gnus-summary-update-info)
10316                    (gnus-list-of-read-articles gnus-newsgroup-name))
10317                (setq gnus-newsgroup-expirable
10318                      (sort gnus-newsgroup-expirable '<)))
10319              gnus-newsgroup-unexist))
10320            (expiry-wait (if now 'immediate
10321                           (gnus-group-find-parameter
10322                            gnus-newsgroup-name 'expiry-wait)))
10323            (nnmail-expiry-target
10324             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10325                 nnmail-expiry-target))
10326            es)
10327       (when expirable
10328         ;; There are expirable articles in this group, so we run them
10329         ;; through the expiry process.
10330         (gnus-message 6 "Expiring articles...")
10331         (when (gnus-check-group gnus-newsgroup-name)
10332           ;; The list of articles that weren't expired is returned.
10333           (save-excursion
10334             (if expiry-wait
10335                 (let ((nnmail-expiry-wait-function nil)
10336                       (nnmail-expiry-wait expiry-wait))
10337                   (setq es (gnus-request-expire-articles
10338                             expirable gnus-newsgroup-name)))
10339               (setq es (gnus-request-expire-articles
10340                         expirable gnus-newsgroup-name)))
10341             (unless total
10342               (setq gnus-newsgroup-expirable es))
10343             ;; We go through the old list of expirable, and mark all
10344             ;; really expired articles as nonexistent.
10345             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10346               (let ((gnus-use-cache nil))
10347                 (dolist (article expirable)
10348                   (when (and (not (memq article es))
10349                              (gnus-data-find article))
10350                     (gnus-summary-mark-article article gnus-canceled-mark)
10351                     (run-hook-with-args 'gnus-summary-article-expire-hook
10352                                         'delete
10353                                         (gnus-data-header
10354                                          (assoc article (gnus-data-list nil)))
10355                                         gnus-newsgroup-name
10356                                         nil
10357                                         nil)))))))
10358         (gnus-message 6 "Expiring articles...done")))))
10359
10360 (defun gnus-summary-expire-articles-now ()
10361   "Expunge all expirable articles in the current group.
10362 This means that *all* articles that are marked as expirable will be
10363 deleted forever, right now."
10364   (interactive)
10365   (or gnus-expert-user
10366       (gnus-yes-or-no-p
10367        "Are you really, really sure you want to delete all expirable messages? ")
10368       (error "Phew!"))
10369   (gnus-summary-expire-articles t))
10370
10371 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10372 (defun gnus-summary-delete-article (&optional n)
10373   "Delete the N next (mail) articles.
10374 This command actually deletes articles.  This is not a marking
10375 command.  The article will disappear forever from your life, never to
10376 return.
10377
10378 If N is negative, delete backwards.
10379 If N is nil and articles have been marked with the process mark,
10380 delete these instead.
10381
10382 If `gnus-novice-user' is non-nil you will be asked for
10383 confirmation before the articles are deleted."
10384   (interactive "P")
10385   (unless (gnus-check-backend-function 'request-expire-articles
10386                                        gnus-newsgroup-name)
10387     (error "The current newsgroup does not support article deletion"))
10388   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10389     (error "Couldn't open server"))
10390   ;; Compute the list of articles to delete.
10391   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10392         (nnmail-expiry-target 'delete)
10393         not-deleted)
10394     (if (and gnus-novice-user
10395              (not (gnus-yes-or-no-p
10396                    (format "Do you really want to delete %s forever? "
10397                            (if (> (length articles) 1)
10398                                (format "these %s articles" (length articles))
10399                              "this article")))))
10400         ()
10401       ;; Delete the articles.
10402       (setq not-deleted (gnus-request-expire-articles
10403                          articles gnus-newsgroup-name 'force))
10404       (save-excursion
10405         (while articles
10406           (gnus-summary-remove-process-mark (car articles))
10407           ;; The backend might not have been able to delete the article
10408           ;; after all.
10409           (unless (memq (car articles) not-deleted)
10410             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10411             (let* ((article (car articles))
10412                    (ghead  (gnus-data-header
10413                             (assoc article (gnus-data-list nil)))))
10414               (run-hook-with-args 'gnus-summary-article-delete-hook
10415                                   'delete ghead gnus-newsgroup-name nil
10416                                   nil)))
10417           (setq articles (cdr articles))))
10418       (when not-deleted
10419         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10420     (gnus-summary-position-point)
10421     (gnus-set-mode-line 'summary)
10422     not-deleted))
10423
10424 (defun gnus-summary-edit-article (&optional arg)
10425   "Edit the current article.
10426 This will have permanent effect only in mail groups.
10427 If ARG is nil, edit the decoded articles.
10428 If ARG is 1, edit the raw articles.
10429 If ARG is 2, edit the raw articles even in read-only groups.
10430 If ARG is 3, edit the articles with the current handles.
10431 Otherwise, allow editing of articles even in read-only
10432 groups."
10433   (interactive "P")
10434   (let (force raw current-handles)
10435     (cond
10436      ((null arg))
10437      ((eq arg 1)
10438       (setq raw t))
10439      ((eq arg 2)
10440       (setq raw t
10441             force t))
10442      ((eq arg 3)
10443       (setq current-handles
10444             (and (gnus-buffer-live-p gnus-article-buffer)
10445                  (with-current-buffer gnus-article-buffer
10446                    (prog1
10447                        gnus-article-mime-handles
10448                      (setq gnus-article-mime-handles nil))))))
10449      (t
10450       (setq force t)))
10451     (when (and raw (not force)
10452                (member gnus-newsgroup-name '("nndraft:delayed"
10453                                              "nndraft:drafts"
10454                                              "nndraft:queue")))
10455       (error "Can't edit the raw article in group %s"
10456              gnus-newsgroup-name))
10457     (with-current-buffer gnus-summary-buffer
10458       (let ((mail-parse-charset gnus-newsgroup-charset)
10459             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10460         (gnus-set-global-variables)
10461         (when (and (not force)
10462                    (gnus-group-read-only-p))
10463           (error "The current newsgroup does not support article editing"))
10464         (gnus-summary-show-article t)
10465         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10466           (with-current-buffer gnus-article-buffer
10467             (mm-enable-multibyte)))
10468         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10469             (setq raw t))
10470         (gnus-article-edit-article
10471          (if raw 'ignore
10472            `(lambda ()
10473               (let ((mbl mml-buffer-list))
10474                 (setq mml-buffer-list nil)
10475                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10476                   (mime-to-mml ,'current-handles))
10477                 (let ((mbl1 mml-buffer-list))
10478                   (setq mml-buffer-list mbl)
10479                   (set (make-local-variable 'mml-buffer-list) mbl1))
10480                 (gnus-make-local-hook 'kill-buffer-hook)
10481                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10482          `(lambda (no-highlight)
10483             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10484                   (message-options message-options)
10485                   (message-options-set-recipient)
10486                   (mail-parse-ignored-charsets
10487                    ',gnus-newsgroup-ignored-charsets)
10488                   (rfc2047-header-encoding-alist
10489                    ',(let ((charset (gnus-group-name-charset
10490                                      (gnus-find-method-for-group
10491                                       gnus-newsgroup-name)
10492                                      gnus-newsgroup-name)))
10493                        (append (list (cons "Newsgroups" charset)
10494                                      (cons "Followup-To" charset)
10495                                      (cons "Xref" charset))
10496                                rfc2047-header-encoding-alist))))
10497               ,(if (not raw) '(progn
10498                                 (mml-to-mime)
10499                                 (mml-destroy-buffers)
10500                                 (remove-hook 'kill-buffer-hook
10501                                              'mml-destroy-buffers t)
10502                                 (kill-local-variable 'mml-buffer-list)))
10503               (gnus-summary-edit-article-done
10504                ,(or (mail-header-references gnus-current-headers) "")
10505                ,(gnus-group-read-only-p)
10506                ,gnus-summary-buffer no-highlight))))))))
10507
10508 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10509
10510 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10511                                                  no-highlight)
10512   "Make edits to the current article permanent."
10513   (interactive)
10514   (save-excursion
10515     ;; The buffer restriction contains the entire article if it exists.
10516     (when (article-goto-body)
10517       (let ((lines (count-lines (point) (point-max)))
10518             (length (- (point-max) (point)))
10519             (case-fold-search t)
10520             (body (copy-marker (point))))
10521         (goto-char (point-min))
10522         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10523           (delete-region (match-beginning 1) (match-end 1))
10524           (insert (number-to-string length)))
10525         (goto-char (point-min))
10526         (when (re-search-forward
10527                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10528           (delete-region (match-beginning 1) (match-end 1))
10529           (insert (number-to-string length)))
10530         (goto-char (point-min))
10531         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10532           (delete-region (match-beginning 1) (match-end 1))
10533           (insert (number-to-string lines))))))
10534   ;; Replace the article.
10535   (let ((buf (current-buffer))
10536         (article (cdr gnus-article-current))
10537         replace-result)
10538     (with-temp-buffer
10539       (insert-buffer-substring buf)
10540       (if (and (not read-only)
10541                (not (setq replace-result
10542                           (gnus-request-replace-article
10543                            article (car gnus-article-current)
10544                            (current-buffer) t))))
10545           (error "Couldn't replace article")
10546         ;; If we got a number back, then that's the new article number
10547         ;; for this article.  Otherwise, the article number didn't change.
10548         (when (numberp replace-result)
10549           (with-current-buffer gnus-summary-buffer
10550             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10551             (gnus-summary-limit gnus-newsgroup-limit)
10552             (setq article replace-result)
10553             (gnus-summary-goto-subject article t)))
10554         ;; Update the summary buffer.
10555         (if (and references
10556                  (equal (message-tokenize-header references " ")
10557                         (message-tokenize-header
10558                          (or (message-fetch-field "references") "") " ")))
10559             ;; We only have to update this line.
10560             (save-excursion
10561               (save-restriction
10562                 (message-narrow-to-head)
10563                 (let ((head (buffer-substring-no-properties
10564                              (point-min) (point-max)))
10565                       header)
10566                   (with-temp-buffer
10567                     (insert (format "211 %d Article retrieved.\n" article))
10568                     (insert head)
10569                     (insert ".\n")
10570                     (let ((nntp-server-buffer (current-buffer)))
10571                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10572                     (with-current-buffer gnus-summary-buffer
10573                       (gnus-data-set-header (gnus-data-find article) header)
10574                       (gnus-summary-update-article-line article header)
10575                       (if (gnus-summary-goto-subject article nil t)
10576                           (gnus-summary-update-secondary-mark article)))))))
10577           ;; Update threads.
10578           (set-buffer (or buffer gnus-summary-buffer))
10579           (gnus-summary-update-article article)
10580           (if (gnus-summary-goto-subject article nil t)
10581               (gnus-summary-update-secondary-mark article)))
10582         ;; Prettify the article buffer again.
10583         (unless no-highlight
10584           (with-current-buffer gnus-article-buffer
10585             ;;!!! Fix this -- article should be rehighlighted.
10586             ;;(gnus-run-hooks 'gnus-article-display-hook)
10587             (set-buffer gnus-original-article-buffer)
10588             (gnus-request-article
10589              article (car gnus-article-current) (current-buffer))))
10590         ;; Prettify the summary buffer line.
10591         (when (gnus-visual-p 'summary-highlight 'highlight)
10592           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10593
10594 (defun gnus-summary-edit-wash (key)
10595   "Perform editing command KEY in the article buffer."
10596   (interactive
10597    (list
10598     (progn
10599       (message "%s" (concat (this-command-keys) "- "))
10600       (read-char))))
10601   (message "")
10602   (gnus-summary-edit-article)
10603   (execute-kbd-macro (concat (this-command-keys) key))
10604   (gnus-article-edit-done))
10605
10606 ;;; Respooling
10607
10608 (defun gnus-summary-respool-query (&optional silent trace)
10609   "Query where the respool algorithm would put this article."
10610   (interactive)
10611   (let (gnus-mark-article-hook)
10612     (gnus-summary-select-article)
10613     (with-current-buffer gnus-original-article-buffer
10614       (let ((groups (nnmail-article-group 'identity trace)))
10615         (unless silent
10616           (if groups
10617               (message "This message would go to %s"
10618                        (mapconcat 'car groups ", "))
10619             (message "This message would go to no groups"))
10620           groups)))))
10621
10622 (defun gnus-summary-respool-trace ()
10623   "Trace where the respool algorithm would put this article.
10624 Display a buffer showing all fancy splitting patterns which matched."
10625   (interactive)
10626   (gnus-summary-respool-query nil t))
10627
10628 ;; Summary marking commands.
10629
10630 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10631   "Mark articles which has the same subject as read, and then select the next.
10632 If UNMARK is positive, remove any kind of mark.
10633 If UNMARK is negative, tick articles."
10634   (interactive "P")
10635   (when unmark
10636     (setq unmark (prefix-numeric-value unmark)))
10637   (let ((count
10638          (gnus-summary-mark-same-subject
10639           (gnus-summary-article-subject) unmark)))
10640     ;; Select next unread article.  If auto-select-same mode, should
10641     ;; select the first unread article.
10642     (gnus-summary-next-article t (and gnus-auto-select-same
10643                                       (gnus-summary-article-subject)))
10644     (gnus-message 7 "%d article%s marked as %s"
10645                   count (if (= count 1) " is" "s are")
10646                   (if unmark "unread" "read"))))
10647
10648 (defun gnus-summary-kill-same-subject (&optional unmark)
10649   "Mark articles which has the same subject as read.
10650 If UNMARK is positive, remove any kind of mark.
10651 If UNMARK is negative, tick articles."
10652   (interactive "P")
10653   (when unmark
10654     (setq unmark (prefix-numeric-value unmark)))
10655   (let ((count
10656          (gnus-summary-mark-same-subject
10657           (gnus-summary-article-subject) unmark)))
10658     ;; If marked as read, go to next unread subject.
10659     (when (null unmark)
10660       ;; Go to next unread subject.
10661       (gnus-summary-next-subject 1 t))
10662     (gnus-message 7 "%d articles are marked as %s"
10663                   count (if unmark "unread" "read"))))
10664
10665 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10666   "Mark articles with same SUBJECT as read, and return marked number.
10667 If optional argument UNMARK is positive, remove any kinds of marks.
10668 If optional argument UNMARK is negative, mark articles as unread instead."
10669   (let ((count 1))
10670     (save-excursion
10671       (cond
10672        ((null unmark)                   ; Mark as read.
10673         (while (and
10674                 (progn
10675                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10676                   (gnus-summary-show-thread) t)
10677                 (gnus-summary-find-subject subject))
10678           (setq count (1+ count))))
10679        ((> unmark 0)                    ; Tick.
10680         (while (and
10681                 (progn
10682                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10683                   (gnus-summary-show-thread) t)
10684                 (gnus-summary-find-subject subject))
10685           (setq count (1+ count))))
10686        (t                               ; Mark as unread.
10687         (while (and
10688                 (progn
10689                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10690                   (gnus-summary-show-thread) t)
10691                 (gnus-summary-find-subject subject))
10692           (setq count (1+ count)))))
10693       (gnus-set-mode-line 'summary)
10694       ;; Return the number of marked articles.
10695       count)))
10696
10697 (defun gnus-summary-mark-as-processable (n &optional unmark)
10698   "Set the process mark on the next N articles.
10699 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10700 the process mark instead.  The difference between N and the actual
10701 number of articles marked is returned."
10702   (interactive "P")
10703   (if (and (null n) (gnus-region-active-p))
10704       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10705     (setq n (prefix-numeric-value n))
10706     (let ((backward (< n 0))
10707           (n (abs n)))
10708       (while (and
10709               (> n 0)
10710               (if unmark
10711                   (gnus-summary-remove-process-mark
10712                    (gnus-summary-article-number))
10713                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10714               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10715         (setq n (1- n)))
10716       (when (/= 0 n)
10717         (gnus-message 7 "No more articles"))
10718       (gnus-summary-recenter)
10719       (gnus-summary-position-point)
10720       n)))
10721
10722 (defun gnus-summary-unmark-as-processable (n)
10723   "Remove the process mark from the next N articles.
10724 If N is negative, unmark backward instead.  The difference between N and
10725 the actual number of articles unmarked is returned."
10726   (interactive "P")
10727   (gnus-summary-mark-as-processable n t))
10728
10729 (defun gnus-summary-unmark-all-processable ()
10730   "Remove the process mark from all articles."
10731   (interactive)
10732   (save-excursion
10733     (while gnus-newsgroup-processable
10734       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10735   (gnus-summary-position-point))
10736
10737 (defun gnus-summary-add-mark (article type)
10738   "Mark ARTICLE with a mark of TYPE."
10739   (let ((vtype (car (assq type gnus-article-mark-lists)))
10740         var)
10741     (if (not vtype)
10742         (error "No such mark type: %s" type)
10743       (setq var (intern (format "gnus-newsgroup-%s" type)))
10744       (set var (cons article (symbol-value var)))
10745       (if (memq type '(processable cached replied forwarded recent saved))
10746           (gnus-summary-update-secondary-mark article)
10747         ;;; !!! This is bogus.  We should find out what primary
10748         ;;; !!! mark we want to set.
10749         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10750
10751 (defun gnus-summary-mark-as-expirable (n)
10752   "Mark N articles forward as expirable.
10753 If N is negative, mark backward instead.  The difference between N and
10754 the actual number of articles marked is returned."
10755   (interactive "p")
10756   (gnus-summary-mark-forward n gnus-expirable-mark))
10757
10758 (defun gnus-summary-mark-as-spam (n)
10759   "Mark N articles forward as spam.
10760 If N is negative, mark backward instead.  The difference between N and
10761 the actual number of articles marked is returned."
10762   (interactive "p")
10763   (gnus-summary-mark-forward n gnus-spam-mark))
10764
10765 (defun gnus-summary-mark-article-as-replied (article)
10766   "Mark ARTICLE as replied to and update the summary line.
10767 ARTICLE can also be a list of articles."
10768   (interactive (list (gnus-summary-article-number)))
10769   (let ((articles (if (listp article) article (list article))))
10770     (dolist (article articles)
10771       (unless (numberp article)
10772         (error "%s is not a number" article))
10773       (push article gnus-newsgroup-replied)
10774       (let ((inhibit-read-only t))
10775         (when (gnus-summary-goto-subject article nil t)
10776           (gnus-summary-update-secondary-mark article))))))
10777
10778 (defun gnus-summary-mark-article-as-forwarded (article)
10779   "Mark ARTICLE as forwarded and update the summary line.
10780 ARTICLE can also be a list of articles."
10781   (let ((articles (if (listp article) article (list article))))
10782     (dolist (article articles)
10783       (push article gnus-newsgroup-forwarded)
10784       (let ((inhibit-read-only t))
10785         (when (gnus-summary-goto-subject article nil t)
10786           (gnus-summary-update-secondary-mark article))))))
10787
10788 (defun gnus-summary-set-bookmark (article)
10789   "Set a bookmark in current article."
10790   (interactive (list (gnus-summary-article-number)))
10791   (when (or (not (get-buffer gnus-article-buffer))
10792             (not gnus-current-article)
10793             (not gnus-article-current)
10794             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10795     (error "No current article selected"))
10796   ;; Remove old bookmark, if one exists.
10797   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10798   ;; Set the new bookmark, which is on the form
10799   ;; (article-number . line-number-in-body).
10800   (push
10801    (cons article
10802          (with-current-buffer gnus-article-buffer
10803            (count-lines
10804             (min (point)
10805                  (save-excursion
10806                    (article-goto-body)
10807                    (point)))
10808             (point))))
10809    gnus-newsgroup-bookmarks)
10810   (gnus-message 6 "A bookmark has been added to the current article."))
10811
10812 (defun gnus-summary-remove-bookmark (article)
10813   "Remove the bookmark from the current article."
10814   (interactive (list (gnus-summary-article-number)))
10815   ;; Remove old bookmark, if one exists.
10816   (if (not (assq article gnus-newsgroup-bookmarks))
10817       (gnus-message 6 "No bookmark in current article.")
10818     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10819     (gnus-message 6 "Removed bookmark.")))
10820
10821 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10822 (defun gnus-summary-mark-as-dormant (n)
10823   "Mark N articles forward as dormant.
10824 If N is negative, mark backward instead.  The difference between N and
10825 the actual number of articles marked is returned."
10826   (interactive "p")
10827   (gnus-summary-mark-forward n gnus-dormant-mark))
10828
10829 (defun gnus-summary-set-process-mark (article)
10830   "Set the process mark on ARTICLE and update the summary line."
10831   (setq gnus-newsgroup-processable
10832         (cons article
10833               (delq article gnus-newsgroup-processable)))
10834   (when (gnus-summary-goto-subject article)
10835     (gnus-summary-show-thread)
10836     (gnus-summary-goto-subject article)
10837     (gnus-summary-update-secondary-mark article)))
10838
10839 (defun gnus-summary-remove-process-mark (&rest articles)
10840   "Remove the process mark from ARTICLES and update the summary line."
10841   (dolist (article articles)
10842     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10843     (when (gnus-summary-goto-subject article)
10844       (gnus-summary-show-thread)
10845       (gnus-summary-goto-subject article)
10846       (gnus-summary-update-secondary-mark article)))
10847   t)
10848
10849 (defun gnus-summary-set-saved-mark (article)
10850   "Set the process mark on ARTICLE and update the summary line."
10851   (push article gnus-newsgroup-saved)
10852   (when (gnus-summary-goto-subject article)
10853     (gnus-summary-update-secondary-mark article)))
10854
10855 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10856   "Mark N articles as read forwards.
10857 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10858 The difference between N and the actual number of articles marked is
10859 returned.
10860 If NO-EXPIRE, auto-expiry will be inhibited."
10861   (interactive "p")
10862   (gnus-summary-show-thread)
10863   (let ((backward (< n 0))
10864         (gnus-summary-goto-unread
10865          (and gnus-summary-goto-unread
10866               (not (eq gnus-summary-goto-unread 'never))
10867               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10868                                     gnus-ticked-mark gnus-dormant-mark)))))
10869         (n (abs n))
10870         (mark (or mark gnus-del-mark)))
10871     (while (and (> n 0)
10872                 (gnus-summary-mark-article nil mark no-expire)
10873                 (zerop (gnus-summary-next-subject
10874                         (if backward -1 1)
10875                         (and gnus-summary-goto-unread
10876                              (not (eq gnus-summary-goto-unread 'never)))
10877                         t)))
10878       (setq n (1- n)))
10879     (when (/= 0 n)
10880       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10881     (gnus-summary-recenter)
10882     (gnus-summary-position-point)
10883     (gnus-set-mode-line 'summary)
10884     n))
10885
10886 (defun gnus-summary-mark-article-as-read (mark)
10887   "Mark the current article quickly as read with MARK."
10888   (let ((article (gnus-summary-article-number)))
10889     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10890     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10891     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10892     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10893     (push (cons article mark) gnus-newsgroup-reads)
10894     ;; Possibly remove from cache, if that is used.
10895     (when gnus-use-cache
10896       (gnus-cache-enter-remove-article article))
10897     ;; Allow the backend to change the mark.
10898     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10899     ;; Check for auto-expiry.
10900     (when (and gnus-newsgroup-auto-expire
10901                (memq mark gnus-auto-expirable-marks))
10902       (setq mark gnus-expirable-mark)
10903       ;; Let the backend know about the mark change.
10904       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10905       (push article gnus-newsgroup-expirable))
10906     ;; Set the mark in the buffer.
10907     (gnus-summary-update-mark mark 'unread)
10908     t))
10909
10910 (defun gnus-summary-mark-article-as-unread (mark)
10911   "Mark the current article quickly as unread with MARK."
10912   (let* ((article (gnus-summary-article-number))
10913          (old-mark (gnus-summary-article-mark article)))
10914     ;; Allow the backend to change the mark.
10915     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10916     (if (eq mark old-mark)
10917         t
10918       (if (<= article 0)
10919           (progn
10920             (gnus-error 1 "Can't mark negative article numbers")
10921             nil)
10922         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10923         (setq gnus-newsgroup-spam-marked
10924               (delq article gnus-newsgroup-spam-marked))
10925         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10926         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10927         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10928         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10929         (cond ((= mark gnus-ticked-mark)
10930                (setq gnus-newsgroup-marked
10931                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10932                                               article)))
10933               ((= mark gnus-spam-mark)
10934                (setq gnus-newsgroup-spam-marked
10935                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10936                                               article)))
10937               ((= mark gnus-dormant-mark)
10938                (setq gnus-newsgroup-dormant
10939                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10940                                               article)))
10941               (t
10942                (setq gnus-newsgroup-unreads
10943                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10944                                               article))))
10945         (gnus-alist-pull article gnus-newsgroup-reads)
10946
10947         ;; See whether the article is to be put in the cache.
10948         (and gnus-use-cache
10949              (vectorp (gnus-summary-article-header article))
10950              (save-excursion
10951                (gnus-cache-possibly-enter-article
10952                 gnus-newsgroup-name article
10953                 (= mark gnus-ticked-mark)
10954                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10955
10956         ;; Fix the mark.
10957         (gnus-summary-update-mark mark 'unread)
10958         t))))
10959
10960 (defun gnus-summary-mark-article (&optional article mark no-expire)
10961   "Mark ARTICLE with MARK.  MARK can be any character.
10962 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10963 `??' (dormant) and `?E' (expirable).
10964 If MARK is nil, then the default character `?r' is used.
10965 If ARTICLE is nil, then the article on the current line will be
10966 marked.
10967 If NO-EXPIRE, auto-expiry will be inhibited."
10968   ;; The mark might be a string.
10969   (when (stringp mark)
10970     (setq mark (aref mark 0)))
10971   ;; If no mark is given, then we check auto-expiring.
10972   (when (null mark)
10973     (setq mark gnus-del-mark))
10974   (when (and (not no-expire)
10975              gnus-newsgroup-auto-expire
10976              (memq mark gnus-auto-expirable-marks))
10977     (setq mark gnus-expirable-mark))
10978   (let ((article (or article (gnus-summary-article-number)))
10979         (old-mark (gnus-summary-article-mark article)))
10980     ;; Allow the backend to change the mark.
10981     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10982     (if (eq mark old-mark)
10983         t
10984       (unless article
10985         (error "No article on current line"))
10986       (if (not (if (or (= mark gnus-unread-mark)
10987                        (= mark gnus-ticked-mark)
10988                        (= mark gnus-spam-mark)
10989                        (= mark gnus-dormant-mark))
10990                    (gnus-mark-article-as-unread article mark)
10991                  (gnus-mark-article-as-read article mark)))
10992           t
10993         ;; See whether the article is to be put in the cache.
10994         (and gnus-use-cache
10995              (not (= mark gnus-canceled-mark))
10996              (vectorp (gnus-summary-article-header article))
10997              (save-excursion
10998                (gnus-cache-possibly-enter-article
10999                 gnus-newsgroup-name article
11000                 (= mark gnus-ticked-mark)
11001                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11002
11003         (when (gnus-summary-goto-subject article nil t)
11004           (let ((inhibit-read-only t))
11005             (gnus-summary-show-thread)
11006             ;; Fix the mark.
11007             (gnus-summary-update-mark mark 'unread)
11008             t))))))
11009
11010 (defun gnus-summary-update-secondary-mark (article)
11011   "Update the secondary (read, process, cache) mark."
11012   (gnus-summary-update-mark
11013    (cond ((memq article gnus-newsgroup-processable)
11014           gnus-process-mark)
11015          ((memq article gnus-newsgroup-cached)
11016           gnus-cached-mark)
11017          ((memq article gnus-newsgroup-replied)
11018           gnus-replied-mark)
11019          ((memq article gnus-newsgroup-forwarded)
11020           gnus-forwarded-mark)
11021          ((memq article gnus-newsgroup-saved)
11022           gnus-saved-mark)
11023          ((memq article gnus-newsgroup-unseen)
11024           gnus-unseen-mark)
11025          (t gnus-no-mark))
11026    'replied)
11027   (when (gnus-visual-p 'summary-highlight 'highlight)
11028     (gnus-summary-highlight-line)
11029     (gnus-run-hooks 'gnus-summary-update-hook))
11030   t)
11031
11032 (defun gnus-summary-update-download-mark (article)
11033   "Update the download mark."
11034   (gnus-summary-update-mark
11035    (cond ((memq article gnus-newsgroup-undownloaded)
11036           gnus-undownloaded-mark)
11037          (gnus-newsgroup-agentized
11038           gnus-downloaded-mark)
11039          (t
11040           gnus-no-mark))
11041    'download)
11042   (gnus-summary-update-line t)
11043   t)
11044
11045 (defun gnus-summary-update-mark (mark type)
11046   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11047         (inhibit-read-only t))
11048     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11049     (when forward
11050       (when (looking-at "\r")
11051         (incf forward))
11052       (when (<= (+ forward (point)) (point-max))
11053         ;; Go to the right position on the line.
11054         (goto-char (+ forward (point)))
11055         ;; Replace the old mark with the new mark.
11056         (let ((to-insert
11057                (mm-subst-char-in-string
11058                 (char-after) mark
11059                 (buffer-substring (point) (1+ (point))))))
11060           (delete-region (point) (1+ (point)))
11061           (insert to-insert))
11062         ;; Optionally update the marks by some user rule.
11063         (when (eq type 'unread)
11064           (gnus-data-set-mark
11065            (gnus-data-find (gnus-summary-article-number)) mark)
11066           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11067
11068 (defun gnus-mark-article-as-read (article &optional mark)
11069   "Enter ARTICLE in the pertinent lists and remove it from others."
11070   ;; Make the article expirable.
11071   (let ((mark (or mark gnus-del-mark)))
11072     (setq gnus-newsgroup-expirable
11073           (if (= mark gnus-expirable-mark)
11074               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11075             (delq article gnus-newsgroup-expirable)))
11076     ;; Remove from unread and marked lists.
11077     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11078     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11079     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11080     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11081     (push (cons article mark) gnus-newsgroup-reads)
11082     ;; Possibly remove from cache, if that is used.
11083     (when gnus-use-cache
11084       (gnus-cache-enter-remove-article article))
11085     t))
11086
11087 (defun gnus-mark-article-as-unread (article &optional mark)
11088   "Enter ARTICLE in the pertinent lists and remove it from others."
11089   (let ((mark (or mark gnus-ticked-mark)))
11090     (if (<= article 0)
11091         (progn
11092           (gnus-error 1 "Can't mark negative article numbers")
11093           nil)
11094       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11095             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11096             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11097             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11098             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11099
11100       ;; Unsuppress duplicates?
11101       (when gnus-suppress-duplicates
11102         (gnus-dup-unsuppress-article article))
11103
11104       (cond ((= mark gnus-ticked-mark)
11105              (setq gnus-newsgroup-marked
11106                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11107             ((= mark gnus-spam-mark)
11108              (setq gnus-newsgroup-spam-marked
11109                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11110                                             article)))
11111             ((= mark gnus-dormant-mark)
11112              (setq gnus-newsgroup-dormant
11113                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11114             (t
11115              (setq gnus-newsgroup-unreads
11116                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11117       (gnus-alist-pull article gnus-newsgroup-reads)
11118       t)))
11119
11120 (defun gnus-summary-tick-article-forward (n)
11121   "Tick N articles forwards.
11122 If N is negative, tick backwards instead.
11123 The difference between N and the number of articles ticked is returned."
11124   (interactive "p")
11125   (gnus-summary-mark-forward n gnus-ticked-mark))
11126
11127 (defun gnus-summary-tick-article-backward (n)
11128   "Tick N articles backwards.
11129 The difference between N and the number of articles ticked is returned."
11130   (interactive "p")
11131   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11132
11133 (defun gnus-summary-tick-article (&optional article clear-mark)
11134   "Mark current article as unread.
11135 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11136 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11137   (interactive)
11138   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11139                                        gnus-ticked-mark)))
11140
11141 (defun gnus-summary-mark-as-read-forward (n)
11142   "Mark N articles as read forwards.
11143 If N is negative, mark backwards instead.
11144 The difference between N and the actual number of articles marked is
11145 returned."
11146   (interactive "p")
11147   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11148
11149 (defun gnus-summary-mark-as-read-backward (n)
11150   "Mark the N articles as read backwards.
11151 The difference between N and the actual number of articles marked is
11152 returned."
11153   (interactive "p")
11154   (gnus-summary-mark-forward
11155    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11156
11157 (defun gnus-summary-mark-as-read (&optional article mark)
11158   "Mark current article as read.
11159 ARTICLE specifies the article to be marked as read.
11160 MARK specifies a string to be inserted at the beginning of the line."
11161   (gnus-summary-mark-article article mark))
11162
11163 (defun gnus-summary-clear-mark-forward (n)
11164   "Clear marks from N articles forward.
11165 If N is negative, clear backward instead.
11166 The difference between N and the number of marks cleared is returned."
11167   (interactive "p")
11168   (gnus-summary-mark-forward n gnus-unread-mark))
11169
11170 (defun gnus-summary-clear-mark-backward (n)
11171   "Clear marks from N articles backward.
11172 The difference between N and the number of marks cleared is returned."
11173   (interactive "p")
11174   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11175
11176 (defun gnus-summary-mark-unread-as-read ()
11177   "Intended to be used by `gnus-mark-article-hook'."
11178   (when (memq gnus-current-article gnus-newsgroup-unreads)
11179     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11180
11181 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11182   "Intended to be used by `gnus-mark-article-hook'."
11183   (let ((mark (gnus-summary-article-mark)))
11184     (when (or (gnus-unread-mark-p mark)
11185               (gnus-read-mark-p mark))
11186       (gnus-summary-mark-article gnus-current-article
11187                                  (or new-mark gnus-read-mark)))))
11188
11189 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11190   "Intended to be used by `gnus-mark-article-hook'."
11191   (let ((mark (gnus-summary-article-mark)))
11192     (when (or (gnus-unread-mark-p mark)
11193               (gnus-read-mark-p mark))
11194       (gnus-summary-mark-article (gnus-summary-article-number)
11195                                  (or new-mark gnus-read-mark)))))
11196
11197 (defun gnus-summary-mark-unread-as-ticked ()
11198   "Intended to be used by `gnus-mark-article-hook'."
11199   (when (memq gnus-current-article gnus-newsgroup-unreads)
11200     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11201
11202 (defun gnus-summary-mark-region-as-read (point mark all)
11203   "Mark all unread articles between point and mark as read.
11204 If given a prefix, mark all articles between point and mark as read,
11205 even ticked and dormant ones."
11206   (interactive "r\nP")
11207   (save-excursion
11208     (let (article)
11209       (goto-char point)
11210       (beginning-of-line)
11211       (while (and
11212               (< (point) mark)
11213               (progn
11214                 (when (or all
11215                           (memq (setq article (gnus-summary-article-number))
11216                                 gnus-newsgroup-unreads))
11217                   (gnus-summary-mark-article article gnus-del-mark))
11218                 t)
11219               (gnus-summary-find-next))))))
11220
11221 (defun gnus-summary-mark-below (score mark)
11222   "Mark articles with score less than SCORE with MARK."
11223   (interactive "P\ncMark: ")
11224   (setq score (if score
11225                   (prefix-numeric-value score)
11226                 (or gnus-summary-default-score 0)))
11227   (with-current-buffer gnus-summary-buffer
11228     (goto-char (point-min))
11229     (while
11230         (progn
11231           (and (< (gnus-summary-article-score) score)
11232                (gnus-summary-mark-article nil mark))
11233           (gnus-summary-find-next)))))
11234
11235 (defun gnus-summary-kill-below (&optional score)
11236   "Mark articles with score below SCORE as read."
11237   (interactive "P")
11238   (gnus-summary-mark-below score gnus-killed-mark))
11239
11240 (defun gnus-summary-clear-above (&optional score)
11241   "Clear all marks from articles with score above SCORE."
11242   (interactive "P")
11243   (gnus-summary-mark-above score gnus-unread-mark))
11244
11245 (defun gnus-summary-tick-above (&optional score)
11246   "Tick all articles with score above SCORE."
11247   (interactive "P")
11248   (gnus-summary-mark-above score gnus-ticked-mark))
11249
11250 (defun gnus-summary-mark-above (score mark)
11251   "Mark articles with score over SCORE with MARK."
11252   (interactive "P\ncMark: ")
11253   (setq score (if score
11254                   (prefix-numeric-value score)
11255                 (or gnus-summary-default-score 0)))
11256   (with-current-buffer gnus-summary-buffer
11257     (goto-char (point-min))
11258     (while (and (progn
11259                   (when (> (gnus-summary-article-score) score)
11260                     (gnus-summary-mark-article nil mark))
11261                   t)
11262                 (gnus-summary-find-next)))))
11263
11264 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11265 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11266 (defun gnus-summary-limit-include-expunged (&optional no-error)
11267   "Display all the hidden articles that were expunged for low scores."
11268   (interactive)
11269   (let ((inhibit-read-only t))
11270     (let ((scored gnus-newsgroup-scored)
11271           headers h)
11272       (while scored
11273         (unless (gnus-summary-article-header (caar scored))
11274           (and (setq h (gnus-number-to-header (caar scored)))
11275                (< (cdar scored) gnus-summary-expunge-below)
11276                (push h headers)))
11277         (setq scored (cdr scored)))
11278       (if (not headers)
11279           (when (not no-error)
11280             (error "No expunged articles hidden"))
11281         (goto-char (point-min))
11282         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11283         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11284         (dolist (x headers)
11285           (push (mail-header-number x) gnus-newsgroup-limit))
11286         (gnus-summary-prepare-unthreaded (nreverse headers))
11287         (goto-char (point-min))
11288         (gnus-summary-position-point)
11289         t))))
11290
11291 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11292   "Mark all unread articles in this newsgroup as read.
11293 If prefix argument ALL is non-nil, ticked and dormant articles will
11294 also be marked as read.
11295 If QUIETLY is non-nil, no questions will be asked.
11296
11297 If TO-HERE is non-nil, it should be a point in the buffer.  All
11298 articles before (after, if REVERSE is set) this point will be marked
11299 as read.
11300
11301 Note that this function will only catch up the unread article
11302 in the current summary buffer limitation.
11303
11304 The number of articles marked as read is returned."
11305   (interactive "P")
11306   (prog1
11307       (save-excursion
11308         (when (or quietly
11309                   (not gnus-interactive-catchup) ;Without confirmation?
11310                   gnus-expert-user
11311                   (gnus-y-or-n-p
11312                    (if all
11313                        "Mark absolutely all articles as read? "
11314                      "Mark all unread articles as read? ")))
11315           (if (and not-mark
11316                    (not gnus-newsgroup-adaptive)
11317                    (not gnus-newsgroup-auto-expire)
11318                    (not gnus-suppress-duplicates)
11319                    (or (not gnus-use-cache)
11320                        (eq gnus-use-cache 'passive)))
11321               (progn
11322                 (when all
11323                   (setq gnus-newsgroup-marked nil
11324                         gnus-newsgroup-spam-marked nil
11325                         gnus-newsgroup-dormant nil))
11326                 (setq gnus-newsgroup-unreads
11327                       (gnus-sorted-nunion
11328                        (gnus-sorted-intersection gnus-newsgroup-unreads
11329                                                  gnus-newsgroup-downloadable)
11330                        (gnus-sorted-difference gnus-newsgroup-unfetched
11331                                                gnus-newsgroup-cached))))
11332             ;; We actually mark all articles as canceled, which we
11333             ;; have to do when using auto-expiry or adaptive scoring.
11334             (gnus-summary-show-all-threads)
11335             (if (and to-here reverse)
11336                 (progn
11337                   (goto-char to-here)
11338                   (gnus-summary-mark-current-read-and-unread-as-read
11339                    gnus-catchup-mark)
11340                   (while (gnus-summary-find-next (not all))
11341                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11342               (when (gnus-summary-first-subject (not all))
11343                 (while (and
11344                         (if to-here (< (point) to-here) t)
11345                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11346                         (gnus-summary-find-next (not all))))))
11347             (gnus-set-mode-line 'summary))
11348           t))
11349     (gnus-summary-position-point)))
11350
11351 (defun gnus-summary-catchup-to-here (&optional all)
11352   "Mark all unticked articles before the current one as read.
11353 If ALL is non-nil, also mark ticked and dormant articles as read."
11354   (interactive "P")
11355   (save-excursion
11356     (gnus-save-hidden-threads
11357       (let ((beg (point)))
11358         ;; We check that there are unread articles.
11359         (when (or all (gnus-summary-find-prev))
11360           (gnus-summary-catchup all t beg)))))
11361   (gnus-summary-position-point))
11362
11363 (defun gnus-summary-catchup-from-here (&optional all)
11364   "Mark all unticked articles after (and including) the current one as read.
11365 If ALL is non-nil, also mark ticked and dormant articles as read."
11366   (interactive "P")
11367   (save-excursion
11368     (gnus-save-hidden-threads
11369       (let ((beg (point)))
11370         ;; We check that there are unread articles.
11371         (when (or all (gnus-summary-find-next))
11372           (gnus-summary-catchup all t beg nil t)))))
11373   (gnus-summary-position-point))
11374
11375 (defun gnus-summary-catchup-all (&optional quietly)
11376   "Mark all articles in this newsgroup as read.
11377 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11378 instead, which marks only unread articles as read."
11379   (interactive "P")
11380   (gnus-summary-catchup t quietly))
11381
11382 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11383   "Mark all unread articles in this group as read, then exit.
11384 If prefix argument ALL is non-nil, all articles are marked as read.
11385 If QUIETLY is non-nil, no questions will be asked."
11386   (interactive "P")
11387   (when (gnus-summary-catchup all quietly nil 'fast)
11388     ;; Select next newsgroup or exit.
11389     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11390              (eq gnus-auto-select-next 'quietly))
11391         (gnus-summary-next-group nil)
11392       (gnus-summary-exit))))
11393
11394 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11395   "Mark all articles in this newsgroup as read, and then exit.
11396 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11397 instead, which marks only unread articles as read."
11398   (interactive "P")
11399   (gnus-summary-catchup-and-exit t quietly))
11400
11401 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11402   "Mark all articles in this group as read and select the next group.
11403 If given a prefix, mark all articles, unread as well as ticked, as
11404 read."
11405   (interactive "P")
11406   (save-excursion
11407     (gnus-summary-catchup all))
11408   (gnus-summary-next-group))
11409
11410 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11411   "Mark all articles in this group as read and select the previous group.
11412 If given a prefix, mark all articles, unread as well as ticked, as
11413 read."
11414   (interactive "P")
11415   (save-excursion
11416     (gnus-summary-catchup all))
11417   (gnus-summary-next-group nil nil t))
11418
11419 ;;;
11420 ;;; with article
11421 ;;;
11422
11423 (defmacro gnus-with-article (article &rest forms)
11424   "Select ARTICLE and perform FORMS in the original article buffer.
11425 Then replace the article with the result."
11426   `(progn
11427      ;; We don't want the article to be marked as read.
11428      (let (gnus-mark-article-hook)
11429        (gnus-summary-select-article t t nil ,article))
11430      (set-buffer gnus-original-article-buffer)
11431      ,@forms
11432      (if (not (gnus-check-backend-function
11433                'request-replace-article (car gnus-article-current)))
11434          (gnus-message 5 "Read-only group; not replacing")
11435        (unless (gnus-request-replace-article
11436                 ,article (car gnus-article-current)
11437                 (current-buffer) t)
11438          (error "Couldn't replace article")))
11439      ;; The cache and backlog have to be flushed somewhat.
11440      (when gnus-keep-backlog
11441        (gnus-backlog-remove-article
11442         (car gnus-article-current) (cdr gnus-article-current)))
11443      (when gnus-use-cache
11444        (gnus-cache-update-article
11445         (car gnus-article-current) (cdr gnus-article-current)))))
11446
11447 (put 'gnus-with-article 'lisp-indent-function 1)
11448 (put 'gnus-with-article 'edebug-form-spec '(form body))
11449
11450 ;; Thread-based commands.
11451
11452 (defun gnus-summary-articles-in-thread (&optional article)
11453   "Return a list of all articles in the current thread.
11454 If ARTICLE is non-nil, return all articles in the thread that starts
11455 with that article."
11456   (let* ((article (or article (gnus-summary-article-number)))
11457          (data (gnus-data-find-list article))
11458          (top-level (gnus-data-level (car data)))
11459          (top-subject
11460           (cond ((null gnus-thread-operation-ignore-subject)
11461                  (gnus-simplify-subject-re
11462                   (mail-header-subject (gnus-data-header (car data)))))
11463                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11464                  (gnus-simplify-subject-fuzzy
11465                   (mail-header-subject (gnus-data-header (car data)))))
11466                 (t nil)))
11467          (end-point (save-excursion
11468                       (goto-char (gnus-data-pos (car data)))
11469                       (if (gnus-summary-go-to-next-thread)
11470                           (point) (point-max))))
11471          articles)
11472     (while (and data
11473                 (< (gnus-data-pos (car data)) end-point))
11474       (when (or (not top-subject)
11475                 (string= top-subject
11476                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11477                              (gnus-simplify-subject-fuzzy
11478                               (mail-header-subject
11479                                (gnus-data-header (car data))))
11480                            (gnus-simplify-subject-re
11481                             (mail-header-subject
11482                              (gnus-data-header (car data)))))))
11483         (push (gnus-data-number (car data)) articles))
11484       (unless (and (setq data (cdr data))
11485                    (> (gnus-data-level (car data)) top-level))
11486         (setq data nil)))
11487     ;; Return the list of articles.
11488     (nreverse articles)))
11489
11490 (defun gnus-summary-rethread-current ()
11491   "Rethread the thread the current article is part of."
11492   (interactive)
11493   (let* ((gnus-show-threads t)
11494          (article (gnus-summary-article-number))
11495          (id (mail-header-id (gnus-summary-article-header)))
11496          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11497     (unless id
11498       (error "No article on the current line"))
11499     (gnus-rebuild-thread id)
11500     (gnus-summary-goto-subject article)))
11501
11502 (defun gnus-summary-reparent-thread ()
11503   "Make the current article child of the marked (or previous) article.
11504
11505 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11506 is non-nil or the Subject: of both articles are the same."
11507   (interactive)
11508   (unless (not (gnus-group-read-only-p))
11509     (error "The current newsgroup does not support article editing"))
11510   (unless (<= (length gnus-newsgroup-processable) 1)
11511     (error "No more than one article may be marked"))
11512   (let ((child (gnus-summary-article-number))
11513         ;; First grab the marked article, otherwise one line up.
11514         (parent (if (not (null gnus-newsgroup-processable))
11515                     (car gnus-newsgroup-processable)
11516                   (save-excursion
11517                     (if (eq (forward-line -1) 0)
11518                         (gnus-summary-article-number)
11519                       (error "Beginning of summary buffer"))))))
11520     (gnus-summary-reparent-children parent (list child))))
11521
11522 (defun gnus-summary-reparent-children (parent children)
11523   "Make PARENT the parent of CHILDREN.
11524 When called interactively, PARENT is the current article and CHILDREN
11525 are the process-marked articles."
11526   (interactive
11527    (list (gnus-summary-article-number)
11528          (gnus-summary-work-articles nil)))
11529   (dolist (child children)
11530     (save-window-excursion
11531       (let ((gnus-article-buffer " *reparent*"))
11532         (unless (not (eq parent child))
11533           (error "An article may not be self-referential"))
11534         (let ((message-id (mail-header-id
11535                            (gnus-summary-article-header parent))))
11536           (unless (and message-id (not (equal message-id "")))
11537             (error "No message-id in desired parent"))
11538           (gnus-with-article child
11539             (save-restriction
11540               (goto-char (point-min))
11541               (message-narrow-to-head)
11542               (if (re-search-forward "^References: " nil t)
11543                   (progn
11544                     (re-search-forward "^[^ \t]" nil t)
11545                     (forward-line -1)
11546                     (end-of-line)
11547                     (insert " " message-id))
11548                 (insert "References: " message-id "\n"))))
11549           (set-buffer gnus-summary-buffer)
11550           (gnus-summary-unmark-all-processable)
11551           (gnus-summary-update-article child)
11552           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11553             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11554           (gnus-summary-rethread-current)
11555           (gnus-message 3 "Article %d is now the child of article %d"
11556                         child parent))))))
11557
11558 (defun gnus-summary-toggle-threads (&optional arg)
11559   "Toggle showing conversation threads.
11560 If ARG is positive number, turn showing conversation threads on."
11561   (interactive "P")
11562   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11563     (setq gnus-show-threads
11564           (if (null arg) (not gnus-show-threads)
11565             (> (prefix-numeric-value arg) 0)))
11566     (gnus-summary-prepare)
11567     (gnus-summary-goto-subject current)
11568     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11569     (gnus-summary-position-point)))
11570
11571 (eval-and-compile
11572   (if (fboundp 'remove-overlays)
11573       (defalias 'gnus-remove-overlays 'remove-overlays)
11574     (defun gnus-remove-overlays (beg end name val)
11575       "Clear BEG and END of overlays whose property NAME has value VAL.
11576 For compatibility with XEmacs."
11577       (dolist (ov (gnus-overlays-in beg end))
11578         (when (eq (gnus-overlay-get ov name) val)
11579           (gnus-delete-overlay ov))))))
11580
11581 (defun gnus-summary-show-all-threads ()
11582   "Show all threads."
11583   (interactive)
11584   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11585   (gnus-summary-position-point))
11586
11587 (defsubst gnus-summary--inv (p)
11588   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11589
11590 (defun gnus-summary-show-thread ()
11591   "Show thread subtrees.
11592 Returns nil if no thread was there to be shown."
11593   (interactive)
11594   (let* ((orig (point))
11595          (end (point-at-eol))
11596          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11597          ;; Leave point at bol
11598          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11599          (eoi (when end
11600                 (if (fboundp 'next-single-char-property-change)
11601                     ;; Note: XEmacs version of n-s-c-p-c may return nil
11602                     (or (next-single-char-property-change end 'invisible)
11603                         (point-max))
11604                   (while (progn
11605                            (end-of-line 2)
11606                            (and (not (eobp))
11607                                 (eq (get-char-property (point) 'invisible)
11608                                     'gnus-sum))))
11609                   (point)))))
11610     (when eoi
11611       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11612       (goto-char orig)
11613       (gnus-summary-position-point)
11614       eoi)))
11615
11616 (defun gnus-summary-maybe-hide-threads ()
11617   "If requested, hide the threads that should be hidden."
11618   (when (and gnus-show-threads
11619              gnus-thread-hide-subtree)
11620     (gnus-summary-hide-all-threads
11621      (if (or (consp gnus-thread-hide-subtree)
11622              (functionp gnus-thread-hide-subtree))
11623          (gnus-make-predicate gnus-thread-hide-subtree)
11624        nil))))
11625
11626 ;;; Hiding predicates.
11627
11628 (defun gnus-article-unread-p (header)
11629   (memq (mail-header-number header) gnus-newsgroup-unreads))
11630
11631 (defun gnus-article-unseen-p (header)
11632   (memq (mail-header-number header) gnus-newsgroup-unseen))
11633
11634 (defun gnus-map-articles (predicate articles)
11635   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11636   (apply 'gnus-or (mapcar predicate
11637                           (mapcar (lambda (number)
11638                                     (gnus-summary-article-header number))
11639                                   articles))))
11640
11641 (defun gnus-summary-hide-all-threads (&optional predicate)
11642   "Hide all thread subtrees.
11643 If PREDICATE is supplied, threads that satisfy this predicate
11644 will not be hidden."
11645   (interactive)
11646   (save-excursion
11647     (goto-char (point-min))
11648     (let ((end nil)
11649           (count 0))
11650       (while (not end)
11651         (incf count)
11652         (when (zerop (mod count 1000))
11653           (message "Hiding all threads... %d" count))
11654         (when (or (not predicate)
11655                   (gnus-map-articles
11656                    predicate (gnus-summary-article-children)))
11657             (gnus-summary-hide-thread))
11658         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11659   (gnus-summary-position-point))
11660
11661 (defun gnus-summary-hide-thread ()
11662   "Hide thread subtrees.
11663 If PREDICATE is supplied, threads that satisfy this predicate
11664 will not be hidden.
11665 Returns nil if no threads were there to be hidden."
11666   (interactive)
11667   (let ((start (point))
11668         (starteol (line-end-position))
11669         (article (gnus-summary-article-number)))
11670     (goto-char start)
11671     ;; Go forward until either the buffer ends or the subthread ends.
11672     (when (and (not (eobp))
11673                (or (zerop (gnus-summary-next-thread 1 t))
11674                    (goto-char (point-max))))
11675       (if (and (> (point) start)
11676                ;; FIXME: this should actually search for a non-invisible \n.
11677                (search-backward "\n" start t))
11678           (progn
11679             (when (> (point) starteol)
11680               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11681               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11682                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11683                 (gnus-overlay-put ol 'evaporate t)))
11684             (gnus-summary-goto-subject article)
11685             (when (> start (point))
11686               (message "Hiding the thread moved us backwards, aborting!")
11687               (goto-char (point-max))))
11688         (goto-char start)
11689         nil))))
11690
11691 (defun gnus-summary-go-to-next-thread (&optional previous)
11692   "Go to the same level (or less) next thread.
11693 If PREVIOUS is non-nil, go to previous thread instead.
11694 Return the article number moved to, or nil if moving was impossible."
11695   (let ((level (gnus-summary-thread-level))
11696         (way (if previous -1 1))
11697         (beg (point)))
11698     (forward-line way)
11699     (while (and (not (eobp))
11700                 (< level (gnus-summary-thread-level)))
11701       (forward-line way))
11702     (if (eobp)
11703         (progn
11704           (goto-char beg)
11705           nil)
11706       (setq beg (point))
11707       (prog1
11708           (gnus-summary-article-number)
11709         (goto-char beg)))))
11710
11711 (defun gnus-summary-next-thread (n &optional silent)
11712   "Go to the same level next N'th thread.
11713 If N is negative, search backward instead.
11714 Returns the difference between N and the number of skips actually
11715 done.
11716
11717 If SILENT, don't output messages."
11718   (interactive "p")
11719   (let ((backward (< n 0))
11720         (n (abs n)))
11721     (while (and (> n 0)
11722                 (gnus-summary-go-to-next-thread backward))
11723       (decf n))
11724     (unless silent
11725       (gnus-summary-position-point))
11726     (when (and (not silent) (/= 0 n))
11727       (gnus-message 7 "No more threads"))
11728     n))
11729
11730 (defun gnus-summary-prev-thread (n)
11731   "Go to the same level previous N'th thread.
11732 Returns the difference between N and the number of skips actually
11733 done."
11734   (interactive "p")
11735   (gnus-summary-next-thread (- n)))
11736
11737 (defun gnus-summary-go-down-thread ()
11738   "Go down one level in the current thread."
11739   (let ((children (gnus-summary-article-children)))
11740     (when children
11741       (gnus-summary-goto-subject (car children)))))
11742
11743 (defun gnus-summary-go-up-thread ()
11744   "Go up one level in the current thread."
11745   (let ((parent (gnus-summary-article-parent)))
11746     (when parent
11747       (gnus-summary-goto-subject parent))))
11748
11749 (defun gnus-summary-down-thread (n)
11750   "Go down thread N steps.
11751 If N is negative, go up instead.
11752 Returns the difference between N and how many steps down that were
11753 taken."
11754   (interactive "p")
11755   (let ((up (< n 0))
11756         (n (abs n)))
11757     (while (and (> n 0)
11758                 (if up (gnus-summary-go-up-thread)
11759                   (gnus-summary-go-down-thread)))
11760       (setq n (1- n)))
11761     (gnus-summary-position-point)
11762     (when (/= 0 n)
11763       (gnus-message 7 "Can't go further"))
11764     n))
11765
11766 (defun gnus-summary-up-thread (n)
11767   "Go up thread N steps.
11768 If N is negative, go down instead.
11769 Returns the difference between N and how many steps down that were
11770 taken."
11771   (interactive "p")
11772   (gnus-summary-down-thread (- n)))
11773
11774 (defun gnus-summary-top-thread ()
11775   "Go to the top of the thread."
11776   (interactive)
11777   (while (gnus-summary-go-up-thread))
11778   (gnus-summary-article-number))
11779
11780 (defun gnus-summary-expire-thread ()
11781   "Mark articles under current thread as expired."
11782   (interactive)
11783   (gnus-summary-kill-thread 0))
11784
11785 (defun gnus-summary-kill-thread (&optional unmark)
11786   "Mark articles under current thread as read.
11787 If the prefix argument is positive, remove any kinds of marks.
11788 If the prefix argument is zero, mark thread as expired.
11789 If the prefix argument is negative, tick articles instead."
11790   (interactive "P")
11791   (when unmark
11792     (setq unmark (prefix-numeric-value unmark)))
11793   (let ((articles (gnus-summary-articles-in-thread))
11794         (hide (or (null unmark) (= unmark 0))))
11795     (save-excursion
11796       ;; Expand the thread.
11797       (gnus-summary-show-thread)
11798       ;; Mark all the articles.
11799       (while articles
11800         (gnus-summary-goto-subject (car articles))
11801         (cond ((null unmark)
11802                (gnus-summary-mark-article-as-read gnus-killed-mark))
11803               ((> unmark 0)
11804                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11805               ((= unmark 0)
11806                (gnus-summary-mark-article nil gnus-expirable-mark))
11807               (t
11808                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11809         (setq articles (cdr articles))))
11810     ;; Hide killed subtrees when hide is true.
11811     (and hide
11812          gnus-thread-hide-killed
11813          (gnus-summary-hide-thread))
11814     ;; If hide is t, go to next unread subject.
11815     (when hide
11816       ;; Go to next unread subject.
11817       (gnus-summary-next-subject 1 t)))
11818   (gnus-set-mode-line 'summary))
11819
11820 ;; Summary sorting commands
11821
11822 (defun gnus-summary-sort-by-number (&optional reverse)
11823   "Sort the summary buffer by article number.
11824 Argument REVERSE means reverse order."
11825   (interactive "P")
11826   (gnus-summary-sort 'number reverse))
11827
11828 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11829   "Sort the summary buffer by most recent article number.
11830 Argument REVERSE means reverse order."
11831   (interactive "P")
11832   (gnus-summary-sort 'most-recent-number reverse))
11833
11834 (defun gnus-summary-sort-by-random (&optional reverse)
11835   "Randomize the order in the summary buffer.
11836 Argument REVERSE means to randomize in reverse order."
11837   (interactive "P")
11838   (gnus-summary-sort 'random reverse))
11839
11840 (defun gnus-summary-sort-by-author (&optional reverse)
11841   "Sort the summary buffer by author name alphabetically.
11842 If `case-fold-search' is non-nil, case of letters is ignored.
11843 Argument REVERSE means reverse order."
11844   (interactive "P")
11845   (gnus-summary-sort 'author reverse))
11846
11847 (defun gnus-summary-sort-by-recipient (&optional reverse)
11848   "Sort the summary buffer by recipient name alphabetically.
11849 If `case-fold-search' is non-nil, case of letters is ignored.
11850 Argument REVERSE means reverse order."
11851   (interactive "P")
11852   (gnus-summary-sort 'recipient reverse))
11853
11854 (defun gnus-summary-sort-by-subject (&optional reverse)
11855   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11856 If `case-fold-search' is non-nil, case of letters is ignored.
11857 Argument REVERSE means reverse order."
11858   (interactive "P")
11859   (gnus-summary-sort 'subject reverse))
11860
11861 (defun gnus-summary-sort-by-date (&optional reverse)
11862   "Sort the summary buffer by date.
11863 Argument REVERSE means reverse order."
11864   (interactive "P")
11865   (gnus-summary-sort 'date reverse))
11866
11867 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11868   "Sort the summary buffer by most recent date.
11869 Argument REVERSE means reverse order."
11870   (interactive "P")
11871   (gnus-summary-sort 'most-recent-date reverse))
11872
11873 (defun gnus-summary-sort-by-score (&optional reverse)
11874   "Sort the summary buffer by score.
11875 Argument REVERSE means reverse order."
11876   (interactive "P")
11877   (gnus-summary-sort 'score reverse))
11878
11879 (defun gnus-summary-sort-by-lines (&optional reverse)
11880   "Sort the summary buffer by the number of lines.
11881 Argument REVERSE means reverse order."
11882   (interactive "P")
11883   (gnus-summary-sort 'lines reverse))
11884
11885 (defun gnus-summary-sort-by-chars (&optional reverse)
11886   "Sort the summary buffer by article length.
11887 Argument REVERSE means reverse order."
11888   (interactive "P")
11889   (gnus-summary-sort 'chars reverse))
11890
11891 (defun gnus-summary-sort-by-original (&optional reverse)
11892   "Sort the summary buffer using the default sorting method.
11893 Argument REVERSE means reverse order."
11894   (interactive "P")
11895   (let* ((inhibit-read-only t)
11896          (gnus-summary-prepare-hook nil))
11897     ;; We do the sorting by regenerating the threads.
11898     (gnus-summary-prepare)
11899     ;; Hide subthreads if needed.
11900     (gnus-summary-maybe-hide-threads)))
11901
11902 (defun gnus-summary-sort (predicate reverse)
11903   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11904   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11905          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11906          (gnus-thread-sort-functions
11907           (if (not reverse)
11908               thread
11909             `(lambda (t1 t2)
11910                (,thread t2 t1))))
11911          (gnus-sort-gathered-threads-function
11912           gnus-thread-sort-functions)
11913          (gnus-article-sort-functions
11914           (if (not reverse)
11915               article
11916             `(lambda (t1 t2)
11917                (,article t2 t1))))
11918          (inhibit-read-only t)
11919          (gnus-summary-prepare-hook nil))
11920     ;; We do the sorting by regenerating the threads.
11921     (gnus-summary-prepare)
11922     ;; Hide subthreads if needed.
11923     (gnus-summary-maybe-hide-threads)))
11924
11925 ;; Summary saving commands.
11926
11927 (defun gnus-summary-save-article (&optional n not-saved)
11928   "Save the current article using the default saver function.
11929 If N is a positive number, save the N next articles.
11930 If N is a negative number, save the N previous articles.
11931 If N is nil and any articles have been marked with the process mark,
11932 save those articles instead.
11933 The variable `gnus-default-article-saver' specifies the saver function.
11934
11935 If the optional second argument NOT-SAVED is non-nil, articles saved
11936 will not be marked as saved."
11937   (interactive "P")
11938   (require 'gnus-art)
11939   (let* ((articles (gnus-summary-work-articles n))
11940          (save-buffer (save-excursion
11941                         (nnheader-set-temp-buffer " *Gnus Save*")))
11942          (num (length articles))
11943          ;; Whether to save decoded articles or raw articles.
11944          (decode (when gnus-article-save-coding-system
11945                    (get gnus-default-article-saver :decode)))
11946          ;; When saving many articles in a single file, use the other
11947          ;; function to save articles other than the first one.
11948          (saver2 (get gnus-default-article-saver :function))
11949          (gnus-prompt-before-saving (if saver2
11950                                         t
11951                                       gnus-prompt-before-saving))
11952          (gnus-default-article-saver gnus-default-article-saver)
11953          header file)
11954     (dolist (article articles)
11955       (setq header (gnus-summary-article-header article))
11956       (if (not (vectorp header))
11957           ;; This is a pseudo-article.
11958           (if (assq 'name header)
11959               (gnus-copy-file (cdr (assq 'name header)))
11960             (gnus-message 1 "Article %d is unsavable" article))
11961         ;; This is a real article.
11962         (save-window-excursion
11963           (gnus-summary-select-article decode decode nil article)
11964           (gnus-summary-goto-subject article))
11965         (with-current-buffer save-buffer
11966           (erase-buffer)
11967           (insert-buffer-substring (if decode
11968                                        gnus-article-buffer
11969                                      gnus-original-article-buffer)))
11970         (setq file (gnus-article-save save-buffer file num))
11971         (gnus-summary-remove-process-mark article)
11972         (unless not-saved
11973           (gnus-summary-set-saved-mark article)))
11974       (when saver2
11975         (setq gnus-default-article-saver saver2
11976               saver2 nil)))
11977     (gnus-kill-buffer save-buffer)
11978     (gnus-summary-position-point)
11979     (gnus-set-mode-line 'summary)
11980     n))
11981
11982 (defun gnus-summary-pipe-output (&optional n sym)
11983   "Pipe the current article to a subprocess.
11984 If N is a positive number, pipe the N next articles.
11985 If N is a negative number, pipe the N previous articles.
11986 If N is nil and any articles have been marked with the process mark,
11987 pipe those articles instead.
11988 The default command to which articles are piped is specified by the
11989 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11990 will be prompted for the command.
11991
11992 The properties `:decode' and `:headers' that are put to the function
11993 symbol `gnus-summary-save-in-pipe' control whether this function
11994 decodes articles and what headers to keep (see the doc string for the
11995 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11996 is neither omitted nor the symbol `r', force including all headers
11997 regardless of the `:headers' property.  If it is the symbol `r',
11998 articles that are not decoded and include all headers will be piped
11999 no matter what the properties `:decode' and `:headers' are."
12000   (interactive (gnus-interactive "P\ny"))
12001   (require 'gnus-art)
12002   (let* ((articles (gnus-summary-work-articles n))
12003          (result-buffer "*Shell Command Output*")
12004          (all-headers (not (memq sym '(nil r))))
12005          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
12006          (raw (eq sym 'r))
12007          (headers (get 'gnus-summary-save-in-pipe :headers))
12008          command result)
12009     (unless (numberp (car articles))
12010       (error "No article to pipe"))
12011     (setq command (gnus-read-shell-command
12012                    (concat "Shell command on "
12013                            (if (cdr articles)
12014                                (format "these %d articles" (length articles))
12015                              "this article")
12016                            ": ")
12017                    gnus-summary-pipe-output-default-command))
12018     (when (string-equal command "")
12019       (error "A command is required"))
12020     (when all-headers
12021       (put 'gnus-summary-save-in-pipe :headers nil))
12022     (unwind-protect
12023         (while articles
12024           (gnus-summary-goto-subject (pop articles))
12025           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12026           (when (and (get-buffer result-buffer)
12027                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12028             (setq result (concat result (with-current-buffer result-buffer
12029                                           (buffer-string))))))
12030       (put 'gnus-summary-save-in-pipe :headers headers))
12031     (unless (zerop (length result))
12032       (if (with-current-buffer (get-buffer-create result-buffer)
12033             (erase-buffer)
12034             (insert result)
12035             (prog1
12036                 (and (= (count-lines (point-min) (point)) 1)
12037                      (progn
12038                        (end-of-line 0)
12039                        (<= (current-column)
12040                            (window-width (minibuffer-window)))))
12041               (goto-char (point-min))))
12042           (message "%s" (substring result 0 -1))
12043         (message nil)
12044         (gnus-configure-windows 'pipe)))))
12045
12046 (defun gnus-summary-save-article-mail (&optional arg)
12047   "Append the current article to a Unix mail box file.
12048 If N is a positive number, save the N next articles.
12049 If N is a negative number, save the N previous articles.
12050 If N is nil and any articles have been marked with the process mark,
12051 save those articles instead."
12052   (interactive "P")
12053   (require 'gnus-art)
12054   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12055     (gnus-summary-save-article arg)))
12056
12057 (defun gnus-summary-save-article-rmail (&optional arg)
12058   "Append the current article to an rmail file.
12059 If N is a positive number, save the N next articles.
12060 If N is a negative number, save the N previous articles.
12061 If N is nil and any articles have been marked with the process mark,
12062 save those articles instead."
12063   (interactive "P")
12064   (require 'gnus-art)
12065   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12066     (gnus-summary-save-article arg)))
12067
12068 (defun gnus-summary-save-article-file (&optional arg)
12069   "Append the current article to a file.
12070 If N is a positive number, save the N next articles.
12071 If N is a negative number, save the N previous articles.
12072 If N is nil and any articles have been marked with the process mark,
12073 save those articles instead."
12074   (interactive "P")
12075   (require 'gnus-art)
12076   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12077     (gnus-summary-save-article arg)))
12078
12079 (defun gnus-summary-write-article-file (&optional arg)
12080   "Write the current article to a file, deleting the previous file.
12081 If N is a positive number, save the N next articles.
12082 If N is a negative number, save the N previous articles.
12083 If N is nil and any articles have been marked with the process mark,
12084 save those articles instead."
12085   (interactive "P")
12086   (require 'gnus-art)
12087   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12088     (gnus-summary-save-article arg)))
12089
12090 (defun gnus-summary-save-article-body-file (&optional arg)
12091   "Append the current article body to a file.
12092 If N is a positive number, save the N next articles.
12093 If N is a negative number, save the N previous articles.
12094 If N is nil and any articles have been marked with the process mark,
12095 save those articles instead."
12096   (interactive "P")
12097   (require 'gnus-art)
12098   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12099     (gnus-summary-save-article arg)))
12100
12101 (defun gnus-summary-write-article-body-file (&optional arg)
12102   "Write the current article body to a file, deleting the previous file.
12103 If N is a positive number, save the N next articles.
12104 If N is a negative number, save the N previous articles.
12105 If N is nil and any articles have been marked with the process mark,
12106 save those articles instead."
12107   (interactive "P")
12108   (require 'gnus-art)
12109   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12110     (gnus-summary-save-article arg)))
12111
12112 (defun gnus-summary-muttprint (&optional arg)
12113   "Print the current article using Muttprint.
12114 If N is a positive number, save the N next articles.
12115 If N is a negative number, save the N previous articles.
12116 If N is nil and any articles have been marked with the process mark,
12117 save those articles instead."
12118   (interactive "P")
12119   (require 'gnus-art)
12120   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12121     (gnus-summary-save-article arg t)))
12122
12123 (defun gnus-summary-pipe-message (program)
12124   "Pipe the current article through PROGRAM."
12125   (interactive "sProgram: ")
12126   (gnus-summary-select-article)
12127   (let ((mail-header-separator ""))
12128     (gnus-eval-in-buffer-window gnus-article-buffer
12129       (save-restriction
12130         (widen)
12131         (let ((start (window-start))
12132               (inhibit-read-only t))
12133           (message-pipe-buffer-body program)
12134           (set-window-start (get-buffer-window (current-buffer)) start))))))
12135
12136 (defun gnus-get-split-value (methods)
12137   "Return a value based on the split METHODS."
12138   (let (split-name method result match)
12139     (when methods
12140       (with-current-buffer gnus-original-article-buffer
12141         (save-restriction
12142           (nnheader-narrow-to-headers)
12143           (while (and methods (not split-name))
12144             (goto-char (point-min))
12145             (setq method (pop methods))
12146             (setq match (car method))
12147             (when (cond
12148                    ((stringp match)
12149                     ;; Regular expression.
12150                     (ignore-errors
12151                       (re-search-forward match nil t)))
12152                    ((functionp match)
12153                     ;; Function.
12154                     (save-restriction
12155                       (widen)
12156                       (setq result (funcall match gnus-newsgroup-name))))
12157                    ((consp match)
12158                     ;; Form.
12159                     (save-restriction
12160                       (widen)
12161                       (setq result (eval match)))))
12162               (setq split-name (cdr method))
12163               (cond ((stringp result)
12164                      (push (expand-file-name
12165                             result gnus-article-save-directory)
12166                            split-name))
12167                     ((consp result)
12168                      (setq split-name (append result split-name)))))))))
12169     (nreverse split-name)))
12170
12171 (defun gnus-valid-move-group-p (group)
12172   (and (symbolp group)
12173        (boundp group)
12174        (symbol-name group)
12175        (symbol-value group)
12176        (gnus-get-function (gnus-find-method-for-group
12177                            (symbol-name group)) 'request-accept-article t)))
12178
12179 (defun gnus-read-move-group-name (prompt default articles prefix)
12180   "Read a group name."
12181   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12182          (minibuffer-confirm-incomplete nil) ; XEmacs
12183          (prom
12184           (format "%s %s to"
12185                   prompt
12186                   (if (> (length articles) 1)
12187                       (format "these %d articles" (length articles))
12188                     "this article")))
12189          (to-newsgroup
12190           (cond
12191            ((null split-name)
12192             (gnus-group-completing-read
12193              prom
12194              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12195              nil prefix nil default))
12196            ((= 1 (length split-name))
12197             (gnus-group-completing-read
12198              prom
12199              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12200              nil prefix 'gnus-group-history (car split-name)))
12201            (t
12202             (gnus-completing-read
12203              prom (nreverse split-name) nil nil 'gnus-group-history))))
12204          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12205          encoded)
12206     (when to-newsgroup
12207       (if (or (string= to-newsgroup "")
12208               (string= to-newsgroup prefix))
12209           (setq to-newsgroup default))
12210       (unless to-newsgroup
12211         (error "No group name entered"))
12212       (setq encoded (mm-encode-coding-string
12213                      to-newsgroup
12214                      (gnus-group-name-charset to-method to-newsgroup)))
12215       (or (gnus-active encoded)
12216           (gnus-activate-group encoded nil nil to-method)
12217           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12218                                      to-newsgroup))
12219               (or (and (gnus-request-create-group encoded to-method)
12220                        (gnus-activate-group encoded nil nil to-method)
12221                        (gnus-subscribe-group encoded))
12222                   (error "Couldn't create group %s" to-newsgroup)))
12223           (error "No such group: %s" to-newsgroup))
12224       encoded)))
12225
12226 (defvar gnus-summary-save-parts-counter)
12227 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12228
12229 (defun gnus-summary-save-parts (type dir n &optional reverse)
12230   "Save parts matching TYPE to DIR.
12231 If REVERSE, save parts that do not match TYPE."
12232   (interactive
12233    (list (read-string "Save parts of type: "
12234                       (or (car gnus-summary-save-parts-type-history)
12235                           gnus-summary-save-parts-default-mime)
12236                       'gnus-summary-save-parts-type-history)
12237          (setq gnus-summary-save-parts-last-directory
12238                (read-directory-name "Save to directory: "
12239                                     gnus-summary-save-parts-last-directory
12240                                     nil t))
12241          current-prefix-arg))
12242   (gnus-summary-iterate n
12243     (let ((gnus-display-mime-function nil)
12244           gnus-article-prepare-hook
12245           gnus-article-decode-hook
12246           gnus-display-mime-function
12247           gnus-break-pages
12248           (gnus-inhibit-treatment t))
12249       (gnus-summary-select-article))
12250     (with-current-buffer gnus-article-buffer
12251       (let ((handles (or gnus-article-mime-handles
12252                          (mm-dissect-buffer nil gnus-article-loose-mime)
12253                          (and gnus-article-emulate-mime
12254                               (mm-uu-dissect))))
12255             (gnus-summary-save-parts-counter 1))
12256         (when handles
12257           (gnus-summary-save-parts-1 type dir handles reverse)
12258           (unless gnus-article-mime-handles ;; Don't destroy this case.
12259             (mm-destroy-parts handles)))))))
12260
12261 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12262   (if (stringp (car handle))
12263       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12264               (cdr handle))
12265     (when (if reverse
12266               (not (string-match type (mm-handle-media-type handle)))
12267             (string-match type (mm-handle-media-type handle)))
12268       (let ((file (expand-file-name
12269                    (gnus-map-function
12270                     mm-file-name-rewrite-functions
12271                     (file-name-nondirectory
12272                      (or
12273                       (mm-handle-filename handle)
12274                       (format "%s.%d.%d" gnus-newsgroup-name
12275                               (cdr gnus-article-current)
12276                               gnus-summary-save-parts-counter))))
12277                    dir)))
12278         (incf gnus-summary-save-parts-counter)
12279         (unless (file-exists-p file)
12280           (mm-save-part-to-file handle file))))))
12281
12282 ;; Summary extract commands
12283
12284 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12285   (let ((inhibit-read-only t)
12286         (article (gnus-summary-article-number))
12287         after-article b e)
12288     (unless (gnus-summary-goto-subject article)
12289       (error "No such article: %d" article))
12290     (gnus-summary-position-point)
12291     ;; If all commands are to be bunched up on one line, we collect
12292     ;; them here.
12293     (unless gnus-view-pseudos-separately
12294       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12295             files action)
12296         (while ps
12297           (setq action (cdr (assq 'action (car ps))))
12298           (setq files (list (cdr (assq 'name (car ps)))))
12299           (while (and ps (cdr ps)
12300                       (string= (or action "1")
12301                                (or (cdr (assq 'action (cadr ps))) "2")))
12302             (push (cdr (assq 'name (cadr ps))) files)
12303             (setcdr ps (cddr ps)))
12304           (when files
12305             (when (not (string-match "%s" action))
12306               (push " " files))
12307             (push " " files)
12308             (when (assq 'execute (car ps))
12309               (setcdr (assq 'execute (car ps))
12310                       (funcall (if (string-match "%s" action)
12311                                    'format 'concat)
12312                                action
12313                                (mapconcat
12314                                 (lambda (f)
12315                                   (if (equal f " ")
12316                                       f
12317                                     (shell-quote-argument f)))
12318                                 files " ")))))
12319           (setq ps (cdr ps)))))
12320     (if (and gnus-view-pseudos (not not-view))
12321         (while pslist
12322           (when (assq 'execute (car pslist))
12323             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12324                                   (eq gnus-view-pseudos 'not-confirm)))
12325           (setq pslist (cdr pslist)))
12326       (save-excursion
12327         (while pslist
12328           (setq after-article (or (cdr (assq 'article (car pslist)))
12329                                   (gnus-summary-article-number)))
12330           (gnus-summary-goto-subject after-article)
12331           (forward-line 1)
12332           (setq b (point))
12333           (insert "    " (file-name-nondirectory
12334                           (cdr (assq 'name (car pslist))))
12335                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12336           (setq e (point))
12337           (forward-line -1)             ; back to `b'
12338           (gnus-add-text-properties
12339            b (1- e) (list 'gnus-number gnus-reffed-article-number
12340                           gnus-mouse-face-prop gnus-mouse-face))
12341           (gnus-data-enter
12342            after-article gnus-reffed-article-number
12343            gnus-unread-mark b (car pslist) 0 (- e b))
12344           (setq gnus-newsgroup-unreads
12345                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12346                                          gnus-reffed-article-number))
12347           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12348           (setq pslist (cdr pslist)))))))
12349
12350 (defun gnus-pseudos< (p1 p2)
12351   (let ((c1 (cdr (assq 'action p1)))
12352         (c2 (cdr (assq 'action p2))))
12353     (and c1 c2 (string< c1 c2))))
12354
12355 (defun gnus-request-pseudo-article (props)
12356   (cond ((assq 'execute props)
12357          (gnus-execute-command (cdr (assq 'execute props)))))
12358   (let ((gnus-current-article (gnus-summary-article-number)))
12359     (gnus-run-hooks 'gnus-mark-article-hook)))
12360
12361 (defun gnus-execute-command (command &optional automatic)
12362   (save-excursion
12363     (gnus-article-setup-buffer)
12364     (set-buffer gnus-article-buffer)
12365     (setq buffer-read-only nil)
12366     (let ((command (if automatic command
12367                      (read-string "Command: " (cons command 0)))))
12368       (erase-buffer)
12369       (insert "$ " command "\n\n")
12370       (if gnus-view-pseudo-asynchronously
12371           (start-process "gnus-execute" (current-buffer) shell-file-name
12372                          shell-command-switch command)
12373         (call-process shell-file-name nil t nil
12374                       shell-command-switch command)))))
12375
12376 ;; Summary kill commands.
12377
12378 (defun gnus-summary-edit-global-kill (article)
12379   "Edit the \"global\" kill file."
12380   (interactive (list (gnus-summary-article-number)))
12381   (gnus-group-edit-global-kill article))
12382
12383 (defun gnus-summary-edit-local-kill ()
12384   "Edit a local kill file applied to the current newsgroup."
12385   (interactive)
12386   (setq gnus-current-headers (gnus-summary-article-header))
12387   (gnus-group-edit-local-kill
12388    (gnus-summary-article-number) gnus-newsgroup-name))
12389
12390 ;;; Header reading.
12391
12392 (defun gnus-read-header (id &optional header)
12393   "Read the headers of article ID and enter them into the Gnus system."
12394   (let ((group gnus-newsgroup-name)
12395         (gnus-override-method
12396          (or
12397           gnus-override-method
12398           (and (gnus-news-group-p gnus-newsgroup-name)
12399                (car (gnus-refer-article-methods)))))
12400         where)
12401     ;; First we check to see whether the header in question is already
12402     ;; fetched.
12403     (if (stringp id)
12404         ;; This is a Message-ID.
12405         (setq header (or header (gnus-id-to-header id)))
12406       ;; This is an article number.
12407       (setq header (or header (gnus-summary-article-header id))))
12408     (if (and header
12409              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12410         ;; We have found the header.
12411         header
12412       ;; We have to really fetch the header to this article.
12413       (with-current-buffer nntp-server-buffer
12414         (when (setq where (gnus-request-head id group))
12415           (nnheader-fold-continuation-lines)
12416           (goto-char (point-max))
12417           (insert ".\n")
12418           (goto-char (point-min))
12419           (insert "211 ")
12420           (princ (cond
12421                   ((numberp id) id)
12422                   ((cdr where) (cdr where))
12423                   (header (mail-header-number header))
12424                   (t gnus-reffed-article-number))
12425                  (current-buffer))
12426           (insert " Article retrieved.\n"))
12427         (if (or (not where)
12428                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12429             ()                          ; Malformed head.
12430           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12431             (when (and (stringp id)
12432                        (or
12433                         (not (string= (gnus-group-real-name group)
12434                                       (car where)))
12435                         (not (gnus-server-equal gnus-override-method
12436                                                 (gnus-group-method group)))))
12437               ;; If we fetched by Message-ID and the article came from
12438               ;; a different group (or server), we fudge some bogus
12439               ;; article numbers for this article.
12440               (mail-header-set-number header gnus-reffed-article-number))
12441             (with-current-buffer gnus-summary-buffer
12442               (decf gnus-reffed-article-number)
12443               (gnus-remove-header (mail-header-number header))
12444               (push header gnus-newsgroup-headers)
12445               (setq gnus-current-headers header)
12446               (push (mail-header-number header) gnus-newsgroup-limit)))
12447           header)))))
12448
12449 (defun gnus-remove-header (number)
12450   "Remove header NUMBER from `gnus-newsgroup-headers'."
12451   (if (and gnus-newsgroup-headers
12452            (= number (mail-header-number (car gnus-newsgroup-headers))))
12453       (pop gnus-newsgroup-headers)
12454     (let ((headers gnus-newsgroup-headers))
12455       (while (and (cdr headers)
12456                   (not (= number (mail-header-number (cadr headers)))))
12457         (pop headers))
12458       (when (cdr headers)
12459         (setcdr headers (cddr headers))))))
12460
12461 ;;;
12462 ;;; summary highlights
12463 ;;;
12464
12465 (defun gnus-highlight-selected-summary ()
12466   "Highlight selected article in summary buffer."
12467   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12468   (when gnus-summary-selected-face
12469     (save-excursion
12470       (let* ((beg (point-at-bol))
12471              (end (point-at-eol))
12472              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12473              (from (if (get-text-property beg gnus-mouse-face-prop)
12474                        beg
12475                      (or (next-single-property-change
12476                           beg gnus-mouse-face-prop nil end)
12477                          beg)))
12478              (to
12479               (if (= from end)
12480                   (- from 2)
12481                 (or (next-single-property-change
12482                      from gnus-mouse-face-prop nil end)
12483                     end))))
12484         ;; If no mouse-face prop on line we will have to = from = end,
12485         ;; so we highlight the entire line instead.
12486         (when (= (+ to 2) from)
12487           (setq from beg)
12488           (setq to end))
12489         (if gnus-newsgroup-selected-overlay
12490             ;; Move old overlay.
12491             (gnus-move-overlay
12492              gnus-newsgroup-selected-overlay from to (current-buffer))
12493           ;; Create new overlay.
12494           (gnus-overlay-put
12495            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12496            'face gnus-summary-selected-face))))))
12497
12498 (defvar gnus-summary-highlight-line-cached nil)
12499 (defvar gnus-summary-highlight-line-trigger nil)
12500
12501 (defun gnus-summary-highlight-line-0 ()
12502   (if (and (eq gnus-summary-highlight-line-trigger
12503                gnus-summary-highlight)
12504            gnus-summary-highlight-line-cached)
12505       gnus-summary-highlight-line-cached
12506     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12507           gnus-summary-highlight-line-cached
12508           (let* ((cond (list 'cond))
12509                  (c cond)
12510                  (list gnus-summary-highlight))
12511             (while list
12512               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12513                               nil))
12514               (setq c (cdr c)
12515                     list (cdr list)))
12516             (gnus-byte-compile (list 'lambda nil cond))))))
12517
12518 (defun gnus-summary-highlight-line ()
12519   "Highlight current line according to `gnus-summary-highlight'."
12520   (let* ((beg (point-at-bol))
12521          (article (or (gnus-summary-article-number) gnus-current-article))
12522          (score (or (cdr (assq article
12523                                gnus-newsgroup-scored))
12524                     gnus-summary-default-score 0))
12525          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12526          (inhibit-read-only t)
12527          (default gnus-summary-default-score)
12528          (default-high gnus-summary-default-high-score)
12529          (default-low gnus-summary-default-low-score)
12530          (uncached (and gnus-summary-use-undownloaded-faces
12531                         (memq article gnus-newsgroup-undownloaded)
12532                         (not (memq article gnus-newsgroup-cached)))))
12533     (let ((face (funcall (gnus-summary-highlight-line-0))))
12534       (unless (eq face (get-text-property beg 'face))
12535         (gnus-put-text-property-excluding-characters-with-faces
12536          beg (point-at-eol) 'face
12537          (setq face (if (boundp face) (symbol-value face) face)))
12538         (when gnus-summary-highlight-line-function
12539           (funcall gnus-summary-highlight-line-function article face))))))
12540
12541 (defun gnus-update-read-articles (group unread &optional compute)
12542   "Update the list of read articles in GROUP.
12543 UNREAD is a sorted list."
12544   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12545         (info (gnus-get-info group))
12546         (prev 1)
12547         read)
12548     (if (or (not info) (not active))
12549         ;; There is no info on this group if it was, in fact,
12550         ;; killed.  Gnus stores no information on killed groups, so
12551         ;; there's nothing to be done.
12552         ;; One could store the information somewhere temporarily,
12553         ;; perhaps...  Hmmm...
12554         ()
12555       ;; Remove any negative articles numbers.
12556       (while (and unread (< (car unread) 0))
12557         (setq unread (cdr unread)))
12558       ;; Remove any expired article numbers
12559       (while (and unread (< (car unread) (car active)))
12560         (setq unread (cdr unread)))
12561       ;; Compute the ranges of read articles by looking at the list of
12562       ;; unread articles.
12563       (while unread
12564         (when (/= (car unread) prev)
12565           (push (if (= prev (1- (car unread))) prev
12566                   (cons prev (1- (car unread))))
12567                 read))
12568         (setq prev (1+ (car unread)))
12569         (setq unread (cdr unread)))
12570       (when (<= prev (cdr active))
12571         (push (cons prev (cdr active)) read))
12572       (setq read (if (> (length read) 1) (nreverse read) read))
12573       (if compute
12574           read
12575         (save-excursion
12576           (let (setmarkundo)
12577             ;; Propagate the read marks to the backend.
12578             (when (and (gnus-method-option-p
12579                         (gnus-find-method-for-group group)
12580                         'server-marks)
12581                        (gnus-check-backend-function 'request-set-mark group))
12582               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12583                     (add (gnus-remove-from-range read (gnus-info-read info))))
12584                 (when (or add del)
12585                   (unless (gnus-check-group group)
12586                     (error "Can't open server for %s" group))
12587                   (gnus-request-set-mark
12588                    group (delq nil (list (if add (list add 'add '(read)))
12589                                          (if del (list del 'del '(read))))))
12590                   (setq setmarkundo
12591                         `(gnus-request-set-mark
12592                           ,group
12593                           ',(delq nil (list
12594                                        (if del (list del 'add '(read)))
12595                                        (if add (list add 'del '(read))))))))))
12596             (set-buffer gnus-group-buffer)
12597             (gnus-undo-register
12598               `(progn
12599                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12600                  (gnus-info-set-read ',info ',(gnus-info-read info))
12601                  (gnus-get-unread-articles-in-group ',info
12602                                                     (gnus-active ,group))
12603                  (gnus-group-update-group ,group t)
12604                  ,setmarkundo))))
12605         ;; Enter this list into the group info.
12606         (gnus-info-set-read info read)
12607         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12608         (gnus-get-unread-articles-in-group info (gnus-active group))
12609         t))))
12610
12611 (defun gnus-offer-save-summaries ()
12612   "Offer to save all active summary buffers."
12613   (let (buffers)
12614     ;; Go through all buffers and find all summaries.
12615     (dolist (buffer (buffer-list))
12616       (when (and (setq buffer (buffer-name buffer))
12617                  (string-match "Summary" buffer)
12618                  (with-current-buffer buffer
12619                    ;; We check that this is, indeed, a summary buffer.
12620                    (and (eq major-mode 'gnus-summary-mode)
12621                         ;; Also make sure this isn't bogus.
12622                         gnus-newsgroup-prepared
12623                         ;; Also make sure that this isn't a
12624                         ;; dead summary buffer.
12625                         (not gnus-dead-summary-mode))))
12626         (push buffer buffers)))
12627     ;; Go through all these summary buffers and offer to save them.
12628     (when buffers
12629       (save-excursion
12630         (if (eq gnus-interactive-exit 'quiet)
12631             (dolist (buffer buffers)
12632               (switch-to-buffer buffer)
12633               (gnus-summary-exit))
12634           (map-y-or-n-p
12635            "Update summary buffer %s? "
12636            (lambda (buf)
12637              (switch-to-buffer buf)
12638              (gnus-summary-exit))
12639            buffers))))))
12640
12641 (defun gnus-summary-setup-default-charset ()
12642   "Setup newsgroup default charset."
12643   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12644       (setq gnus-newsgroup-charset nil)
12645     (let* ((ignored-charsets
12646             (or gnus-newsgroup-ephemeral-ignored-charsets
12647                 (append
12648                  (and gnus-newsgroup-name
12649                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12650                  gnus-newsgroup-ignored-charsets))))
12651       (setq gnus-newsgroup-charset
12652             (or gnus-newsgroup-ephemeral-charset
12653                 (and gnus-newsgroup-name
12654                      (gnus-parameter-charset gnus-newsgroup-name))
12655                 gnus-default-charset))
12656       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12657            ignored-charsets))))
12658
12659 ;;;
12660 ;;; Mime Commands
12661 ;;;
12662
12663 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12664   "Display the current article buffer fully MIME-buttonized.
12665 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12666 treated as multipart/mixed."
12667   (interactive "P")
12668   (require 'gnus-art)
12669   (let ((gnus-unbuttonized-mime-types nil)
12670         (gnus-mime-display-multipart-as-mixed show-all-parts))
12671     (gnus-summary-show-article)))
12672
12673 (defun gnus-summary-repair-multipart (article)
12674   "Add a Content-Type header to a multipart article without one."
12675   (interactive (list (gnus-summary-article-number)))
12676   (gnus-with-article article
12677     (message-narrow-to-head)
12678     (message-remove-header "Mime-Version")
12679     (goto-char (point-max))
12680     (insert "Mime-Version: 1.0\n")
12681     (widen)
12682     (when (search-forward "\n--" nil t)
12683       (let ((separator (buffer-substring (point) (point-at-eol))))
12684         (message-narrow-to-head)
12685         (message-remove-header "Content-Type")
12686         (goto-char (point-max))
12687         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12688                         separator))
12689         (widen))))
12690   (let (gnus-mark-article-hook)
12691     (gnus-summary-select-article t t nil article)))
12692
12693 (defun gnus-summary-toggle-display-buttonized ()
12694   "Toggle the buttonizing of the article buffer."
12695   (interactive)
12696   (require 'gnus-art)
12697   (if (setq gnus-inhibit-mime-unbuttonizing
12698             (not gnus-inhibit-mime-unbuttonizing))
12699       (let ((gnus-unbuttonized-mime-types nil))
12700         (gnus-summary-show-article))
12701     (gnus-summary-show-article)))
12702
12703 ;;;
12704 ;;; Generic summary marking commands
12705 ;;;
12706
12707 (defvar gnus-summary-marking-alist
12708   '((read gnus-del-mark "d")
12709     (unread gnus-unread-mark "u")
12710     (ticked gnus-ticked-mark "!")
12711     (dormant gnus-dormant-mark "?")
12712     (expirable gnus-expirable-mark "e"))
12713   "An alist of names/marks/keystrokes.")
12714
12715 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12716 (defvar gnus-summary-mark-map)
12717
12718 (defun gnus-summary-make-all-marking-commands ()
12719   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12720   (dolist (elem gnus-summary-marking-alist)
12721     (apply 'gnus-summary-make-marking-command elem)))
12722
12723 (defun gnus-summary-make-marking-command (name mark keystroke)
12724   (let ((map (make-sparse-keymap)))
12725     (define-key gnus-summary-generic-mark-map keystroke map)
12726     (dolist (lway `((next "next" next nil "n")
12727                     (next-unread "next unread" next t "N")
12728                     (prev "previous" prev nil "p")
12729                     (prev-unread "previous unread" prev t "P")
12730                     (nomove "" nil nil ,keystroke)))
12731       (let ((func (gnus-summary-make-marking-command-1
12732                    mark (car lway) lway name)))
12733         (setq func (eval func))
12734         (define-key map (nth 4 lway) func)))))
12735
12736 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12737   `(defun ,(intern
12738             (format "gnus-summary-put-mark-as-%s%s"
12739                     name (if (eq way 'nomove)
12740                              ""
12741                            (concat "-" (symbol-name way)))))
12742      (n)
12743      ,(format
12744        "Mark the current article as %s%s.
12745 If N, the prefix, then repeat N times.
12746 If N is negative, move in reverse order.
12747 The difference between N and the actual number of articles marked is
12748 returned."
12749        name (cadr lway))
12750      (interactive "p")
12751      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12752
12753 (defun gnus-summary-generic-mark (n mark move unread)
12754   "Mark N articles with MARK."
12755   (unless (eq major-mode 'gnus-summary-mode)
12756     (error "This command can only be used in the summary buffer"))
12757   (gnus-summary-show-thread)
12758   (let ((nummove
12759          (cond
12760           ((eq move 'next) 1)
12761           ((eq move 'prev) -1)
12762           (t 0))))
12763     (if (zerop nummove)
12764         (setq n 1)
12765       (when (< n 0)
12766         (setq n (abs n)
12767               nummove (* -1 nummove))))
12768     (while (and (> n 0)
12769                 (gnus-summary-mark-article nil mark)
12770                 (zerop (gnus-summary-next-subject nummove unread t)))
12771       (setq n (1- n)))
12772     (when (/= 0 n)
12773       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12774     (gnus-summary-recenter)
12775     (gnus-summary-position-point)
12776     (gnus-set-mode-line 'summary)
12777     n))
12778
12779 (defun gnus-summary-insert-articles (articles)
12780   (when (setq articles
12781               (gnus-sorted-difference articles
12782                                       (mapcar (lambda (h)
12783                                                 (mail-header-number h))
12784                                               gnus-newsgroup-headers)))
12785     (setq gnus-newsgroup-headers
12786           (gnus-merge 'list
12787                       gnus-newsgroup-headers
12788                       (gnus-fetch-headers articles)
12789                       'gnus-article-sort-by-number))
12790     (setq gnus-newsgroup-articles
12791           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12792     ;; Suppress duplicates?
12793     (when gnus-suppress-duplicates
12794       (gnus-dup-suppress-articles))
12795
12796     (if (and gnus-fetch-old-headers
12797              (eq gnus-headers-retrieved-by 'nov))
12798         ;; We might want to build some more threads first.
12799         (if (eq gnus-fetch-old-headers 'invisible)
12800             (gnus-build-all-threads)
12801           (gnus-build-old-threads))
12802       ;; Mark the inserted articles that are unread as unread.
12803       (setq gnus-newsgroup-unreads
12804             (gnus-sorted-nunion
12805              gnus-newsgroup-unreads
12806              (gnus-sorted-nintersection
12807               (gnus-list-of-unread-articles gnus-newsgroup-name)
12808               articles)))
12809       ;; Mark the inserted articles as selected so that the information
12810       ;; of the marks having been changed by a user may be updated when
12811       ;; exiting this group.  See `gnus-summary-update-info'.
12812       (dolist (art articles)
12813         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12814     ;; Let the Gnus agent mark articles as read.
12815     (when gnus-agent
12816       (gnus-agent-get-undownloaded-list))
12817     ;; Remove list identifiers from subject
12818     (gnus-summary-remove-list-identifiers)
12819     ;; First and last article in this newsgroup.
12820     (when gnus-newsgroup-headers
12821       (setq gnus-newsgroup-begin
12822             (mail-header-number (car gnus-newsgroup-headers))
12823             gnus-newsgroup-end
12824             (mail-header-number
12825              (gnus-last-element gnus-newsgroup-headers))))
12826     (when gnus-use-scoring
12827       (gnus-possibly-score-headers))))
12828
12829 (defun gnus-summary-insert-old-articles (&optional all)
12830   "Insert all old articles in this group.
12831 If ALL is non-nil, already read articles become readable.
12832 If ALL is a number, fetch this number of articles."
12833   (interactive "P")
12834   (prog1
12835       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12836             older len)
12837         (setq older
12838               ;; Some nntp servers lie about their active range.  When
12839               ;; this happens, the active range can be in the millions.
12840               ;; Use a compressed range to avoid creating a huge list.
12841               (gnus-range-difference (list gnus-newsgroup-active) old))
12842         (setq len (gnus-range-length older))
12843         (cond
12844          ((null older) nil)
12845          ((numberp all)
12846           (if (< all len)
12847               (let ((older-range (nreverse older)))
12848                 (setq older nil)
12849
12850                 (while (> all 0)
12851                   (let* ((r (pop older-range))
12852                          (min (if (numberp r) r (car r)))
12853                          (max (if (numberp r) r (cdr r))))
12854                     (while (and (<= min max)
12855                                 (> all 0))
12856                       (push max older)
12857                       (setq all (1- all)
12858                             max (1- max))))))
12859             (setq older (gnus-uncompress-range older))))
12860          (all
12861           (setq older (gnus-uncompress-range older)))
12862          (t
12863           (when (and (numberp gnus-large-newsgroup)
12864                    (> len gnus-large-newsgroup))
12865               (let* ((cursor-in-echo-area nil)
12866                      (initial (gnus-parameter-large-newsgroup-initial
12867                                gnus-newsgroup-name))
12868                      (input
12869                       (read-string
12870                        (format
12871                         "How many articles from %s (%s %d): "
12872                         (gnus-group-decoded-name gnus-newsgroup-name)
12873                         (if initial "max" "default")
12874                         len)
12875                        nil nil
12876                        (and initial
12877                             (number-to-string initial)))))
12878                 (unless (string-match "^[ \t]*$" input)
12879                   (setq all (string-to-number input))
12880                   (if (< all len)
12881                       (let ((older-range (nreverse older)))
12882                         (setq older nil)
12883
12884                         (while (> all 0)
12885                           (let* ((r (pop older-range))
12886                                  (min (if (numberp r) r (car r)))
12887                                  (max (if (numberp r) r (cdr r))))
12888                             (while (and (<= min max)
12889                                         (> all 0))
12890                               (push max older)
12891                               (setq all (1- all)
12892                                     max (1- max))))))))))
12893           (setq older (gnus-uncompress-range older))))
12894         (if (not older)
12895             (message "No old news.")
12896           (gnus-summary-insert-articles older)
12897           (gnus-summary-limit (gnus-sorted-nunion old older))))
12898     (gnus-summary-position-point)))
12899
12900 (defun gnus-summary-insert-new-articles ()
12901   "Insert all new articles in this group."
12902   (interactive)
12903   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12904         (old-high gnus-newsgroup-highest)
12905         (nnmail-fetched-sources (list t))
12906         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
12907         i new)
12908     (unless new-active
12909       (error "Couldn't fetch new data"))
12910     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
12911     (setq i (cdr gnus-newsgroup-active)
12912           gnus-newsgroup-highest i)
12913     (while (> i old-high)
12914       (push i new)
12915       (decf i))
12916     (if (not new)
12917         (message "No gnus is bad news")
12918       (gnus-summary-insert-articles new)
12919       (setq gnus-newsgroup-unreads
12920             (gnus-sorted-nunion gnus-newsgroup-unreads new))
12921       (gnus-summary-limit (gnus-sorted-nunion old new))))
12922   (gnus-summary-position-point))
12923
12924 ;;; Bookmark support for Gnus.
12925 (declare-function bookmark-make-record-default
12926                   "bookmark" (&optional no-file no-context posn))
12927 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
12928 (declare-function bookmark-default-handler "bookmark" (bmk))
12929 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
12930 (defvar bookmark-yank-point)
12931 (defvar bookmark-current-buffer)
12932
12933 (defun gnus-summary-bookmark-make-record ()
12934   "Make a bookmark entry for a Gnus summary buffer."
12935   (let (pos buf)
12936     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
12937       (save-restriction              ; FIXME is it necessary to widen?
12938         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
12939       (setq buf "art") ; We are recording bookmark from article buffer.
12940       (setq bookmark-yank-point (point))
12941       (setq bookmark-current-buffer (current-buffer))
12942       (gnus-article-show-summary))      ; Go back in summary buffer.
12943     ;; We are now recording bookmark from summary buffer.
12944     (unless buf (setq buf "sum"))
12945     (let* ((subject (elt (gnus-summary-article-header) 1))
12946            (grp     (car gnus-article-current))
12947            (art     (cdr gnus-article-current))
12948            (head    (gnus-summary-article-header art))
12949            (id      (mail-header-id head)))
12950       `(,subject
12951         ,@(condition-case nil
12952               (bookmark-make-record-default 'no-file 'no-context pos)
12953             (wrong-number-of-arguments
12954              (bookmark-make-record-default 'point-only)))
12955         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
12956         (group . ,grp) (article . ,art)
12957         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
12958
12959 ;;;###autoload
12960 (defun gnus-summary-bookmark-jump (bookmark)
12961   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
12962 BOOKMARK is a bookmark name or a bookmark record."
12963   (let ((group    (bookmark-prop-get bookmark 'group))
12964         (article  (bookmark-prop-get bookmark 'article))
12965         (id       (bookmark-prop-get bookmark 'message-id))
12966         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
12967     (gnus-fetch-group group (list article))
12968     (gnus-summary-insert-cached-articles)
12969     (gnus-summary-goto-article id nil 'force)
12970     ;; FIXME we have to wait article buffer is ready (only large buffer)
12971     ;; Is there a better solution to know that?
12972     ;; If we don't wait `bookmark-default-handler' will have no chance
12973     ;; to set position. However there is no error, just wrong pos.
12974     (sit-for 1)
12975     (when (string= buf "Gnus-art")
12976       (other-window 1))
12977     (bookmark-default-handler
12978      `(""
12979        (buffer . ,(current-buffer))
12980        . ,(bookmark-get-bookmark-record bookmark)))))
12981
12982 (gnus-summary-make-all-marking-commands)
12983
12984 (gnus-ems-redefine)
12985
12986 (provide 'gnus-sum)
12987
12988 (run-hooks 'gnus-sum-load-hook)
12989
12990 ;; Local Variables:
12991 ;; coding: iso-8859-1
12992 ;; End:
12993
12994 ;;; gnus-sum.el ends here