* gnus-art.el (gnus-narrow-to-page): Position point properly.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 ;; For Emacs < 22.2.
31 (eval-and-compile
32   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
33 (eval-when-compile
34   (require 'cl))
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
63 (defcustom gnus-kill-summary-on-exit t
64   "*If non-nil, kill the summary buffer when you exit from it.
65 If nil, the summary will become a \"*Dead Summary*\" buffer, and
66 it will be killed sometime later."
67   :group 'gnus-summary-exit
68   :type 'boolean)
69
70 (defcustom gnus-summary-next-group-on-exit t
71   "If non-nil, go to the next unread newsgroup on summary exit.
72 See `gnus-group-goto-unread'."
73   :link '(custom-manual "(gnus)Group Maneuvering")
74   :group 'gnus-summary-exit
75   :version "23.1" ;; No Gnus
76   :type 'boolean)
77
78 (defcustom gnus-fetch-old-headers nil
79   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
80 If an unread article in the group refers to an older, already
81 read (or just marked as read) article, the old article will not
82 normally be displayed in the Summary buffer.  If this variable is
83 t, Gnus will attempt to grab the headers to the old articles, and
84 thereby build complete threads.  If it has the value `some', all
85 old headers will be fetched but only enough headers to connect
86 otherwise loose threads will be displayed.  This variable can
87 also be a number.  In that case, no more than that number of old
88 headers will be fetched.  If it has the value `invisible', all
89 old headers will be fetched, but none will be displayed.
90
91 The server has to support NOV for any of this to work.
92
93 This feature can seriously impact performance it ignores all
94 locally cached header entries."
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const :tag "on" t)
98                  (const some)
99                  (const invisible)
100                  number
101                  (sexp :menu-tag "other" t)))
102
103 (defcustom gnus-refer-thread-limit 500
104   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
105 If t, fetch all the available old headers."
106   :group 'gnus-thread
107   :type '(choice number
108                  (sexp :menu-tag "other" t)))
109
110 (defcustom gnus-summary-make-false-root 'adopt
111   "*nil means that Gnus won't gather loose threads.
112 If the root of a thread has expired or been read in a previous
113 session, the information necessary to build a complete thread has been
114 lost.  Instead of having many small sub-threads from this original thread
115 scattered all over the summary buffer, Gnus can gather them.
116
117 If non-nil, Gnus will try to gather all loose sub-threads from an
118 original thread into one large thread.
119
120 If this variable is non-nil, it should be one of `none', `adopt',
121 `dummy' or `empty'.
122
123 If this variable is `none', Gnus will not make a false root, but just
124 present the sub-threads after another.
125 If this variable is `dummy', Gnus will create a dummy root that will
126 have all the sub-threads as children.
127 If this variable is `adopt', Gnus will make one of the \"children\"
128 the parent and mark all the step-children as such.
129 If this variable is `empty', the \"children\" are printed with empty
130 subject fields.  (Or rather, they will be printed with a string
131 given by the `gnus-summary-same-subject' variable.)"
132   :group 'gnus-thread
133   :type '(choice (const :tag "off" nil)
134                  (const none)
135                  (const dummy)
136                  (const adopt)
137                  (const empty)))
138
139 (defcustom gnus-summary-make-false-root-always nil
140   "Always make a false dummy root."
141   :version "22.1"
142   :group 'gnus-thread
143   :type 'boolean)
144
145 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
146   "*A regexp to match subjects to be excluded from loose thread gathering.
147 As loose thread gathering is done on subjects only, that means that
148 there can be many false gatherings performed.  By rooting out certain
149 common subjects, gathering might become saner."
150   :group 'gnus-thread
151   :type 'regexp)
152
153 (defcustom gnus-summary-gather-subject-limit nil
154   "*Maximum length of subject comparisons when gathering loose threads.
155 Use nil to compare full subjects.  Setting this variable to a low
156 number will help gather threads that have been corrupted by
157 newsreaders chopping off subject lines, but it might also mean that
158 unrelated articles that have subject that happen to begin with the
159 same few characters will be incorrectly gathered.
160
161 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
162 comparing subjects."
163   :group 'gnus-thread
164   :type '(choice (const :tag "off" nil)
165                  (const fuzzy)
166                  (sexp :menu-tag "on" t)))
167
168 (defcustom gnus-simplify-subject-functions nil
169   "List of functions taking a string argument that simplify subjects.
170 The functions are applied recursively.
171
172 Useful functions to put in this list include:
173 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
174 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
175   :group 'gnus-thread
176   :type '(repeat function))
177
178 (defcustom gnus-simplify-ignored-prefixes nil
179   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
180   :group 'gnus-thread
181   :type '(choice (const :tag "off" nil)
182                  regexp))
183
184 (defcustom gnus-build-sparse-threads nil
185   "*If non-nil, fill in the gaps in threads.
186 If `some', only fill in the gaps that are needed to tie loose threads
187 together.  If `more', fill in all leaf nodes that Gnus can find.  If
188 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
189   :group 'gnus-thread
190   :type '(choice (const :tag "off" nil)
191                  (const some)
192                  (const more)
193                  (sexp :menu-tag "all" t)))
194
195 (defcustom gnus-summary-thread-gathering-function
196   'gnus-gather-threads-by-subject
197   "*Function used for gathering loose threads.
198 There are two pre-defined functions: `gnus-gather-threads-by-subject',
199 which only takes Subjects into consideration; and
200 `gnus-gather-threads-by-references', which compared the References
201 headers of the articles to find matches."
202   :group 'gnus-thread
203   :type '(radio (function-item gnus-gather-threads-by-subject)
204                 (function-item gnus-gather-threads-by-references)
205                 (function :tag "other")))
206
207 (defcustom gnus-summary-same-subject ""
208   "*String indicating that the current article has the same subject as the previous.
209 This variable will only be used if the value of
210 `gnus-summary-make-false-root' is `empty'."
211   :group 'gnus-summary-format
212   :type 'string)
213
214 (defcustom gnus-summary-goto-unread t
215   "*If t, many commands will go to the next unread article.
216 This applies to marking commands as well as other commands that
217 \"naturally\" select the next article, like, for instance, `SPC' at
218 the end of an article.
219
220 If nil, the marking commands do NOT go to the next unread article
221 \(they go to the next article instead).  If `never', commands that
222 usually go to the next unread article, will go to the next article,
223 whether it is read or not."
224   :group 'gnus-summary-marks
225   :link '(custom-manual "(gnus)Setting Marks")
226   :type '(choice (const :tag "off" nil)
227                  (const never)
228                  (sexp :menu-tag "on" t)))
229
230 (defcustom gnus-summary-default-score 0
231   "*Default article score level.
232 All scores generated by the score files will be added to this score.
233 If this variable is nil, scoring will be disabled."
234   :group 'gnus-score-default
235   :type '(choice (const :tag "disable")
236                  integer))
237
238 (defcustom gnus-summary-default-high-score 0
239   "*Default threshold for a high scored article.
240 An article will be highlighted as high scored if its score is greater
241 than this score."
242   :version "22.1"
243   :group 'gnus-score-default
244   :type 'integer)
245
246 (defcustom gnus-summary-default-low-score 0
247   "*Default threshold for a low scored article.
248 An article will be highlighted as low scored if its score is smaller
249 than this score."
250   :version "22.1"
251   :group 'gnus-score-default
252   :type 'integer)
253
254 (defcustom gnus-summary-zcore-fuzz 0
255   "*Fuzziness factor for the zcore in the summary buffer.
256 Articles with scores closer than this to `gnus-summary-default-score'
257 will not be marked."
258   :group 'gnus-summary-format
259   :type 'integer)
260
261 (defcustom gnus-simplify-subject-fuzzy-regexp nil
262   "*Strings to be removed when doing fuzzy matches.
263 This can either be a regular expression or list of regular expressions
264 that will be removed from subject strings if fuzzy subject
265 simplification is selected."
266   :group 'gnus-thread
267   :type '(repeat regexp))
268
269 (defcustom gnus-show-threads t
270   "*If non-nil, display threads in summary mode."
271   :group 'gnus-thread
272   :type 'boolean)
273
274 (defcustom gnus-thread-hide-subtree nil
275   "*If non-nil, hide all threads initially.
276 This can be a predicate specifier which says which threads to hide.
277 If threads are hidden, you have to run the command
278 `gnus-summary-show-thread' by hand or select an article."
279   :group 'gnus-thread
280   :type '(radio (sexp :format "Non-nil\n"
281                       :match (lambda (widget value)
282                                (not (or (consp value) (functionp value))))
283                       :value t)
284                 (const nil)
285                 (sexp :tag "Predicate specifier")))
286
287 (defcustom gnus-thread-hide-killed t
288   "*If non-nil, hide killed threads automatically."
289   :group 'gnus-thread
290   :type 'boolean)
291
292 (defcustom gnus-thread-ignore-subject t
293   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
294 If nil, articles that have different subjects from their parents will
295 start separate threads."
296   :group 'gnus-thread
297   :type 'boolean)
298
299 (defcustom gnus-thread-operation-ignore-subject t
300   "*If non-nil, subjects will be ignored when doing thread commands.
301 This affects commands like `gnus-summary-kill-thread' and
302 `gnus-summary-lower-thread'.
303
304 If this variable is nil, articles in the same thread with different
305 subjects will not be included in the operation in question.  If this
306 variable is `fuzzy', only articles that have subjects that are fuzzily
307 equal will be included."
308   :group 'gnus-thread
309   :type '(choice (const :tag "off" nil)
310                  (const fuzzy)
311                  (sexp :tag "on" t)))
312
313 (defcustom gnus-thread-indent-level 4
314   "*Number that says how much each sub-thread should be indented."
315   :group 'gnus-thread
316   :type 'integer)
317
318 (defcustom gnus-auto-extend-newsgroup t
319   "*If non-nil, extend newsgroup forward and backward when requested."
320   :group 'gnus-summary-choose
321   :type 'boolean)
322
323 (defcustom gnus-auto-select-first t
324   "If non-nil, select an article on group entry.
325 An article is selected automatically when entering a group
326 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
327 `gnus-summary-catchup-and-goto-next-group'.
328
329 Which article is selected is controlled by the variable
330 `gnus-auto-select-subject'.
331
332 If you want to prevent automatic selection of articles in some
333 newsgroups, set the variable to nil in `gnus-select-group-hook'."
334   ;; Commands include...
335   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
336   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
337   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
338   :group 'gnus-group-select
339   :type '(choice (const :tag "none" nil)
340                  (sexp :menu-tag "first" t)))
341
342 (defcustom gnus-auto-select-subject 'unread
343   "*Says what subject to place under point when entering a group.
344
345 This variable can either be the symbols `first' (place point on the
346 first subject), `unread' (place point on the subject line of the first
347 unread article), `best' (place point on the subject line of the
348 higest-scored article), `unseen' (place point on the subject line of
349 the first unseen article), `unseen-or-unread' (place point on the subject
350 line of the first unseen article or, if all article have been seen, on the
351 subject line of the first unread article), or a function to be called to
352 place point on some subject line."
353   :version "22.1"
354   :group 'gnus-group-select
355   :type '(choice (const best)
356                  (const unread)
357                  (const first)
358                  (const unseen)
359                  (const unseen-or-unread)))
360
361 (defcustom gnus-auto-select-next t
362   "*If non-nil, offer to go to the next group from the end of the previous.
363 If the value is t and the next newsgroup is empty, Gnus will exit
364 summary mode and go back to group mode.  If the value is neither nil
365 nor t, Gnus will select the following unread newsgroup.  In
366 particular, if the value is the symbol `quietly', the next unread
367 newsgroup will be selected without any confirmation, and if it is
368 `almost-quietly', the next group will be selected without any
369 confirmation if you are located on the last article in the group.
370 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
371 will go to the next group without confirmation."
372   :group 'gnus-summary-maneuvering
373   :type '(choice (const :tag "off" nil)
374                  (const quietly)
375                  (const almost-quietly)
376                  (const slightly-quietly)
377                  (sexp :menu-tag "on" t)))
378
379 (defcustom gnus-auto-select-same nil
380   "*If non-nil, select the next article with the same subject.
381 If there are no more articles with the same subject, go to
382 the first unread article."
383   :group 'gnus-summary-maneuvering
384   :type 'boolean)
385
386 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
387   "What article should be selected after exiting an ephemeral group.
388 Valid values include:
389
390 `next'
391   Select the next article.
392 `next-unread'
393   Select the next unread article.
394 `next-noselect'
395   Move the cursor to the next article.  This is the default.
396 `next-unread-noselect'
397   Move the cursor to the next unread article.
398
399 If it has any other value or there is no next (unread) article, the
400 article selected before entering to the ephemeral group will appear."
401   :version "23.1" ;; No Gnus
402   :group 'gnus-summary-maneuvering
403   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
404                  (const next) (const next-unread)
405                  (const next-noselect) (const next-unread-noselect)
406                  (sexp :tag "other" :value nil)))
407
408 (defcustom gnus-auto-goto-ignores 'unfetched
409   "*Says how to handle unfetched articles when maneuvering.
410
411 This variable can either be the symbols nil (maneuver to any
412 article), `undownloaded' (maneuvering while unplugged ignores articles
413 that have not been fetched), `always-undownloaded' (maneuvering always
414 ignores articles that have not been fetched), `unfetched' (maneuvering
415 ignores articles whose headers have not been fetched).
416
417 NOTE: The list of unfetched articles will always be nil when plugged
418 and, when unplugged, a subset of the undownloaded article list."
419   :version "22.1"
420   :group 'gnus-summary-maneuvering
421   :type '(choice (const :tag "None" nil)
422                  (const :tag "Undownloaded when unplugged" undownloaded)
423                  (const :tag "Undownloaded" always-undownloaded)
424                  (const :tag "Unfetched" unfetched)))
425
426 (defcustom gnus-summary-check-current nil
427   "*If non-nil, consider the current article when moving.
428 The \"unread\" movement commands will stay on the same line if the
429 current article is unread."
430   :group 'gnus-summary-maneuvering
431   :type 'boolean)
432
433 (defcustom gnus-auto-center-summary 2
434   "*If non-nil, always center the current summary buffer.
435 In particular, if `vertical' do only vertical recentering.  If non-nil
436 and non-`vertical', do both horizontal and vertical recentering."
437   :group 'gnus-summary-maneuvering
438   :type '(choice (const :tag "none" nil)
439                  (const vertical)
440                  (integer :tag "height")
441                  (sexp :menu-tag "both" t)))
442
443 (defvar gnus-auto-center-group t
444   "*If non-nil, always center the group buffer.")
445
446 (defcustom gnus-show-all-headers nil
447   "*If non-nil, don't hide any headers."
448   :group 'gnus-article-hiding
449   :group 'gnus-article-headers
450   :type 'boolean)
451
452 (defcustom gnus-summary-ignore-duplicates nil
453   "*If non-nil, ignore articles with identical Message-ID headers."
454   :group 'gnus-summary
455   :type 'boolean)
456
457 (defcustom gnus-single-article-buffer t
458   "*If non-nil, display all articles in the same buffer.
459 If nil, each group will get its own article buffer."
460   :group 'gnus-article-various
461   :type 'boolean)
462
463 (defcustom gnus-break-pages t
464   "*If non-nil, do page breaking on articles.
465 The page delimiter is specified by the `gnus-page-delimiter'
466 variable."
467   :group 'gnus-article-various
468   :type 'boolean)
469
470 (defcustom gnus-move-split-methods nil
471   "*Variable used to suggest where articles are to be moved to.
472 It uses the same syntax as the `gnus-split-methods' variable.
473 However, whereas `gnus-split-methods' specifies file names as targets,
474 this variable specifies group names."
475   :group 'gnus-summary-mail
476   :type '(repeat (choice (list :value (fun) function)
477                          (cons :value ("" "") regexp (repeat string))
478                          (sexp :value nil))))
479
480 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
481   "Function used to compute default prefix for article move/copy/etc prompts.
482 The function should take one argument, a group name, and return a
483 string with the suggested prefix."
484   :group 'gnus-summary-mail
485   :type 'function)
486
487 ;; FIXME: Although the custom type is `character' for the following variables,
488 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
489
490 (defcustom gnus-unread-mark ?           ;Whitespace
491   "*Mark used for unread articles."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-ticked-mark ?!
496   "*Mark used for ticked articles."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-dormant-mark ??
501   "*Mark used for dormant articles."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-del-mark ?r
506   "*Mark used for del'd articles."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-read-mark ?R
511   "*Mark used for read articles."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-expirable-mark ?E
516   "*Mark used for expirable articles."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-killed-mark ?K
521   "*Mark used for killed articles."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-spam-mark ?$
526   "*Mark used for spam articles."
527   :version "22.1"
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-souped-mark ?F
532   "*Mark used for souped articles."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-kill-file-mark ?X
537   "*Mark used for articles killed by kill files."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-low-score-mark ?Y
542   "*Mark used for articles with a low score."
543   :group 'gnus-summary-marks
544   :type 'character)
545
546 (defcustom gnus-catchup-mark ?C
547   "*Mark used for articles that are caught up."
548   :group 'gnus-summary-marks
549   :type 'character)
550
551 (defcustom gnus-replied-mark ?A
552   "*Mark used for articles that have been replied to."
553   :group 'gnus-summary-marks
554   :type 'character)
555
556 (defcustom gnus-forwarded-mark ?F
557   "*Mark used for articles that have been forwarded."
558   :version "22.1"
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-recent-mark ?N
563   "*Mark used for articles that are recent."
564   :version "22.1"
565   :group 'gnus-summary-marks
566   :type 'character)
567
568 (defcustom gnus-cached-mark ?*
569   "*Mark used for articles that are in the cache."
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-saved-mark ?S
574   "*Mark used for articles that have been saved."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-unseen-mark ?.
579   "*Mark used for articles that haven't been seen."
580   :version "22.1"
581   :group 'gnus-summary-marks
582   :type 'character)
583
584 (defcustom gnus-no-mark ?               ;Whitespace
585   "*Mark used for articles that have no other secondary mark."
586   :version "22.1"
587   :group 'gnus-summary-marks
588   :type 'character)
589
590 (defcustom gnus-ancient-mark ?O
591   "*Mark used for ancient articles."
592   :group 'gnus-summary-marks
593   :type 'character)
594
595 (defcustom gnus-sparse-mark ?Q
596   "*Mark used for sparsely reffed articles."
597   :group 'gnus-summary-marks
598   :type 'character)
599
600 (defcustom gnus-canceled-mark ?G
601   "*Mark used for canceled articles."
602   :group 'gnus-summary-marks
603   :type 'character)
604
605 (defcustom gnus-duplicate-mark ?M
606   "*Mark used for duplicate articles."
607   :group 'gnus-summary-marks
608   :type 'character)
609
610 (defcustom gnus-undownloaded-mark ?-
611   "*Mark used for articles that weren't downloaded."
612   :version "22.1"
613   :group 'gnus-summary-marks
614   :type 'character)
615
616 (defcustom gnus-downloaded-mark ?+
617   "*Mark used for articles that were downloaded."
618   :group 'gnus-summary-marks
619   :type 'character)
620
621 (defcustom gnus-downloadable-mark ?%
622   "*Mark used for articles that are to be downloaded."
623   :group 'gnus-summary-marks
624   :type 'character)
625
626 (defcustom gnus-unsendable-mark ?=
627   "*Mark used for articles that won't be sent."
628   :group 'gnus-summary-marks
629   :type 'character)
630
631 (defcustom gnus-score-over-mark ?+
632   "*Score mark used for articles with high scores."
633   :group 'gnus-summary-marks
634   :type 'character)
635
636 (defcustom gnus-score-below-mark ?-
637   "*Score mark used for articles with low scores."
638   :group 'gnus-summary-marks
639   :type 'character)
640
641 (defcustom gnus-empty-thread-mark ?     ;Whitespace
642   "*There is no thread under the article."
643   :group 'gnus-summary-marks
644   :type 'character)
645
646 (defcustom gnus-not-empty-thread-mark ?=
647   "*There is a thread under the article."
648   :group 'gnus-summary-marks
649   :type 'character)
650
651 (defcustom gnus-view-pseudo-asynchronously nil
652   "*If non-nil, Gnus will view pseudo-articles asynchronously."
653   :group 'gnus-extract-view
654   :type 'boolean)
655
656 (defcustom gnus-auto-expirable-marks
657   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
658         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
659         gnus-souped-mark gnus-duplicate-mark)
660   "*The list of marks converted into expiration if a group is auto-expirable."
661   :version "21.1"
662   :group 'gnus-summary
663   :type '(repeat character))
664
665 (defcustom gnus-inhibit-user-auto-expire t
666   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
667   :version "21.1"
668   :group 'gnus-summary
669   :type 'boolean)
670
671 (defcustom gnus-view-pseudos nil
672   "*If `automatic', pseudo-articles will be viewed automatically.
673 If `not-confirm', pseudos will be viewed automatically, and the user
674 will not be asked to confirm the command."
675   :group 'gnus-extract-view
676   :type '(choice (const :tag "off" nil)
677                  (const automatic)
678                  (const not-confirm)))
679
680 (defcustom gnus-view-pseudos-separately t
681   "*If non-nil, one pseudo-article will be created for each file to be viewed.
682 If nil, all files that use the same viewing command will be given as a
683 list of parameters to that command."
684   :group 'gnus-extract-view
685   :type 'boolean)
686
687 (defcustom gnus-insert-pseudo-articles t
688   "*If non-nil, insert pseudo-articles when decoding articles."
689   :group 'gnus-extract-view
690   :type 'boolean)
691
692 (defcustom gnus-summary-dummy-line-format
693   "   %(:                             :%) %S\n"
694   "*The format specification for the dummy roots in the summary buffer.
695 It works along the same lines as a normal formatting string,
696 with some simple extensions.
697
698 %S  The subject
699
700 General format specifiers can also be used.
701 See `(gnus)Formatting Variables'."
702   :link '(custom-manual "(gnus)Formatting Variables")
703   :group 'gnus-threading
704   :type 'string)
705
706 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
707   "*The format specification for the summary mode line.
708 It works along the same lines as a normal formatting string,
709 with some simple extensions:
710
711 %G  Group name
712 %p  Unprefixed group name
713 %A  Current article number
714 %z  Current article score
715 %V  Gnus version
716 %U  Number of unread articles in the group
717 %e  Number of unselected articles in the group
718 %Z  A string with unread/unselected article counts
719 %g  Shortish group name
720 %S  Subject of the current article
721 %u  User-defined spec
722 %s  Current score file name
723 %d  Number of dormant articles
724 %r  Number of articles that have been marked as read in this session
725 %E  Number of articles expunged by the score files"
726   :group 'gnus-summary-format
727   :type 'string)
728
729 (defcustom gnus-list-identifiers nil
730   "Regexp that matches list identifiers to be removed from subject.
731 This can also be a list of regexps."
732   :version "21.1"
733   :group 'gnus-summary-format
734   :group 'gnus-article-hiding
735   :type '(choice (const :tag "none" nil)
736                  (regexp :value ".*")
737                  (repeat :value (".*") regexp)))
738
739 (defcustom gnus-summary-mark-below 0
740   "*Mark all articles with a score below this variable as read.
741 This variable is local to each summary buffer and usually set by the
742 score file."
743   :group 'gnus-score-default
744   :type 'integer)
745
746 (defun gnus-widget-reversible-match (widget value)
747   "Ignoring WIDGET, convert VALUE to internal form.
748 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
749   ;; (debug value)
750   (or (symbolp value)
751       (and (listp value)
752            (eq (length value) 2)
753            (eq (nth 0 value) 'not)
754            (symbolp (nth 1 value)))))
755
756 (defun gnus-widget-reversible-to-internal (widget value)
757   "Ignoring WIDGET, convert VALUE to internal form.
758 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
759 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
760   ;; (debug value)
761   (if (atom value)
762       (list value nil)
763     (list (nth 1 value) t)))
764
765 (defun gnus-widget-reversible-to-external (widget value)
766   "Ignoring WIDGET, convert VALUE to external form.
767 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
768 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
769   ;; (debug value)
770   (if (nth 1 value)
771       (list 'not (nth 0 value))
772     (nth 0 value)))
773
774 (define-widget 'gnus-widget-reversible 'group
775   "A `group' that convert values."
776   :match 'gnus-widget-reversible-match
777   :value-to-internal 'gnus-widget-reversible-to-internal
778   :value-to-external 'gnus-widget-reversible-to-external)
779
780 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
781   "*List of functions used for sorting articles in the summary buffer.
782
783 Each function takes two articles and returns non-nil if the first
784 article should be sorted before the other.  If you use more than one
785 function, the primary sort function should be the last.  You should
786 probably always include `gnus-article-sort-by-number' in the list of
787 sorting functions -- preferably first.  Also note that sorting by date
788 is often much slower than sorting by number, and the sorting order is
789 very similar.  (Sorting by date means sorting by the time the message
790 was sent, sorting by number means sorting by arrival time.)
791
792 Each item can also be a list `(not F)' where F is a function;
793 this reverses the sort order.
794
795 Ready-made functions include `gnus-article-sort-by-number',
796 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
797 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
798 and `gnus-article-sort-by-score'.
799
800 When threading is turned on, the variable `gnus-thread-sort-functions'
801 controls how articles are sorted."
802   :group 'gnus-summary-sort
803   :type '(repeat (gnus-widget-reversible
804                   (choice (function-item gnus-article-sort-by-number)
805                           (function-item gnus-article-sort-by-author)
806                           (function-item gnus-article-sort-by-subject)
807                           (function-item gnus-article-sort-by-date)
808                           (function-item gnus-article-sort-by-score)
809                           (function-item gnus-article-sort-by-random)
810                           (function :tag "other"))
811                   (boolean :tag "Reverse order"))))
812
813
814 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
815   "*List of functions used for sorting threads in the summary buffer.
816 By default, threads are sorted by article number.
817
818 Each function takes two threads and returns non-nil if the first
819 thread should be sorted before the other.  If you use more than one
820 function, the primary sort function should be the last.  You should
821 probably always include `gnus-thread-sort-by-number' in the list of
822 sorting functions -- preferably first.  Also note that sorting by date
823 is often much slower than sorting by number, and the sorting order is
824 very similar.  (Sorting by date means sorting by the time the message
825 was sent, sorting by number means sorting by arrival time.)
826
827 Each list item can also be a list `(not F)' where F is a
828 function; this specifies reversed sort order.
829
830 Ready-made functions include `gnus-thread-sort-by-number',
831 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
832 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
833 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
834 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
835 and `gnus-thread-sort-by-total-score' (see
836 `gnus-thread-score-function').
837
838 When threading is turned off, the variable
839 `gnus-article-sort-functions' controls how articles are sorted."
840   :group 'gnus-summary-sort
841   :type '(repeat
842           (gnus-widget-reversible
843            (choice (function-item gnus-thread-sort-by-number)
844                    (function-item gnus-thread-sort-by-author)
845                    (function-item gnus-thread-sort-by-recipient)
846                    (function-item gnus-thread-sort-by-subject)
847                    (function-item gnus-thread-sort-by-date)
848                    (function-item gnus-thread-sort-by-score)
849                    (function-item gnus-thread-sort-by-most-recent-number)
850                    (function-item gnus-thread-sort-by-most-recent-date)
851                    (function-item gnus-thread-sort-by-random)
852                    (function-item gnus-thread-sort-by-total-score)
853                    (function :tag "other"))
854            (boolean :tag "Reverse order"))))
855
856 (defcustom gnus-thread-score-function '+
857   "*Function used for calculating the total score of a thread.
858
859 The function is called with the scores of the article and each
860 subthread and should then return the score of the thread.
861
862 Some functions you can use are `+', `max', or `min'."
863   :group 'gnus-summary-sort
864   :type 'function)
865
866 (defcustom gnus-summary-expunge-below nil
867   "All articles that have a score less than this variable will be expunged.
868 This variable is local to the summary buffers."
869   :group 'gnus-score-default
870   :type '(choice (const :tag "off" nil)
871                  integer))
872
873 (defcustom gnus-thread-expunge-below nil
874   "All threads that have a total score less than this variable will be expunged.
875 See `gnus-thread-score-function' for en explanation of what a
876 \"thread score\" is.
877
878 This variable is local to the summary buffers."
879   :group 'gnus-threading
880   :group 'gnus-score-default
881   :type '(choice (const :tag "off" nil)
882                  integer))
883
884 (defcustom gnus-summary-mode-hook nil
885   "*A hook for Gnus summary mode.
886 This hook is run before any variables are set in the summary buffer."
887   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
888   :group 'gnus-summary-various
889   :type 'hook)
890
891 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
892 (when (featurep 'xemacs)
893   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
894   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
895   (add-hook 'gnus-summary-mode-hook
896             'gnus-xmas-switch-horizontal-scrollbar-off))
897
898 (defcustom gnus-summary-menu-hook nil
899   "*Hook run after the creation of the summary mode menu."
900   :group 'gnus-summary-visual
901   :type 'hook)
902
903 (defcustom gnus-summary-exit-hook nil
904   "*A hook called on exit from the summary buffer.
905 It will be called with point in the group buffer."
906   :group 'gnus-summary-exit
907   :type 'hook)
908
909 (defcustom gnus-summary-prepare-hook nil
910   "*A hook called after the summary buffer has been generated.
911 If you want to modify the summary buffer, you can use this hook."
912   :group 'gnus-summary-various
913   :type 'hook)
914
915 (defcustom gnus-summary-prepared-hook nil
916   "*A hook called as the last thing after the summary buffer has been generated."
917   :group 'gnus-summary-various
918   :type 'hook)
919
920 (defcustom gnus-summary-generate-hook nil
921   "*A hook run just before generating the summary buffer.
922 This hook is commonly used to customize threading variables and the
923 like."
924   :group 'gnus-summary-various
925   :type 'hook)
926
927 (defcustom gnus-select-group-hook nil
928   "*A hook called when a newsgroup is selected.
929
930 If you'd like to simplify subjects like the
931 `gnus-summary-next-same-subject' command does, you can use the
932 following hook:
933
934  (add-hook gnus-select-group-hook
935            (lambda ()
936              (mapcar (lambda (header)
937                        (mail-header-set-subject
938                         header
939                         (gnus-simplify-subject
940                          (mail-header-subject header) 're-only)))
941                      gnus-newsgroup-headers)))"
942   :group 'gnus-group-select
943   :type 'hook)
944
945 (defcustom gnus-select-article-hook nil
946   "*A hook called when an article is selected."
947   :group 'gnus-summary-choose
948   :options '(gnus-agent-fetch-selected-article)
949   :type 'hook)
950
951 (defcustom gnus-visual-mark-article-hook
952   (list 'gnus-highlight-selected-summary)
953   "*Hook run after selecting an article in the summary buffer.
954 It is meant to be used for highlighting the article in some way.  It
955 is not run if `gnus-visual' is nil."
956   :group 'gnus-summary-visual
957   :type 'hook)
958
959 (defcustom gnus-parse-headers-hook nil
960   "*A hook called before parsing the headers."
961   :group 'gnus-various
962   :type 'hook)
963
964 (defcustom gnus-exit-group-hook nil
965   "*A hook called when exiting summary mode.
966 This hook is not called from the non-updating exit commands like `Q'."
967   :group 'gnus-various
968   :type 'hook)
969
970 (defcustom gnus-summary-update-hook
971   (list 'gnus-summary-highlight-line)
972   "*A hook called when a summary line is changed.
973 The hook will not be called if `gnus-visual' is nil.
974
975 The default function `gnus-summary-highlight-line' will
976 highlight the line according to the `gnus-summary-highlight'
977 variable."
978   :group 'gnus-summary-visual
979   :type 'hook)
980
981 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
982   "*A hook called when an article is selected for the first time.
983 The hook is intended to mark an article as read (or unread)
984 automatically when it is selected."
985   :group 'gnus-summary-choose
986   :type 'hook)
987
988 (defcustom gnus-group-no-more-groups-hook nil
989   "*A hook run when returning to group mode having no more (unread) groups."
990   :group 'gnus-group-select
991   :type 'hook)
992
993 (defcustom gnus-ps-print-hook nil
994   "*A hook run before ps-printing something from Gnus."
995   :group 'gnus-summary
996   :type 'hook)
997
998 (defcustom gnus-summary-article-move-hook nil
999   "*A hook called after an article is moved, copied, respooled, or crossposted."
1000   :version "22.1"
1001   :group 'gnus-summary
1002   :type 'hook)
1003
1004 (defcustom gnus-summary-article-delete-hook nil
1005   "*A hook called after an article is deleted."
1006   :version "22.1"
1007   :group 'gnus-summary
1008   :type 'hook)
1009
1010 (defcustom gnus-summary-article-expire-hook nil
1011   "*A hook called after an article is expired."
1012   :version "22.1"
1013   :group 'gnus-summary
1014   :type 'hook)
1015
1016 (defcustom gnus-summary-display-arrow
1017   (and (fboundp 'display-graphic-p)
1018        (display-graphic-p))
1019   "*If non-nil, display an arrow highlighting the current article."
1020   :version "22.1"
1021   :group 'gnus-summary
1022   :type 'boolean)
1023
1024 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1025   "Face used for highlighting the current article in the summary buffer."
1026   :group 'gnus-summary-visual
1027   :type 'face)
1028
1029 (defvar gnus-tmp-downloaded nil)
1030
1031 (defcustom gnus-summary-highlight
1032   '(((eq mark gnus-canceled-mark)
1033      . gnus-summary-cancelled)
1034     ((and uncached (> score default-high))
1035      . gnus-summary-high-undownloaded)
1036     ((and uncached (< score default-low))
1037      . gnus-summary-low-undownloaded)
1038     (uncached
1039      . gnus-summary-normal-undownloaded)
1040     ((and (> score default-high)
1041           (or (eq mark gnus-dormant-mark)
1042               (eq mark gnus-ticked-mark)))
1043      . gnus-summary-high-ticked)
1044     ((and (< score default-low)
1045           (or (eq mark gnus-dormant-mark)
1046               (eq mark gnus-ticked-mark)))
1047      . gnus-summary-low-ticked)
1048     ((or (eq mark gnus-dormant-mark)
1049          (eq mark gnus-ticked-mark))
1050      . gnus-summary-normal-ticked)
1051     ((and (> score default-high) (eq mark gnus-ancient-mark))
1052      . gnus-summary-high-ancient)
1053     ((and (< score default-low) (eq mark gnus-ancient-mark))
1054      . gnus-summary-low-ancient)
1055     ((eq mark gnus-ancient-mark)
1056      . gnus-summary-normal-ancient)
1057     ((and (> score default-high) (eq mark gnus-unread-mark))
1058      . gnus-summary-high-unread)
1059     ((and (< score default-low) (eq mark gnus-unread-mark))
1060      . gnus-summary-low-unread)
1061     ((eq mark gnus-unread-mark)
1062      . gnus-summary-normal-unread)
1063     ((> score default-high)
1064      . gnus-summary-high-read)
1065     ((< score default-low)
1066      . gnus-summary-low-read)
1067     (t
1068      . gnus-summary-normal-read))
1069   "*Controls the highlighting of summary buffer lines.
1070
1071 A list of (FORM . FACE) pairs.  When deciding how a particular
1072 summary line should be displayed, each form is evaluated.  The content
1073 of the face field after the first true form is used.  You can change
1074 how those summary lines are displayed, by editing the face field.
1075
1076 You can use the following variables in the FORM field.
1077
1078 score:        The article's score.
1079 default:      The default article score.
1080 default-high: The default score for high scored articles.
1081 default-low:  The default score for low scored articles.
1082 below:        The score below which articles are automatically marked as read.
1083 mark:         The article's mark.
1084 uncached:     Non-nil if the article is uncached."
1085   :group 'gnus-summary-visual
1086   :type '(repeat (cons (sexp :tag "Form" nil)
1087                        face)))
1088 (put 'gnus-summary-highlight 'risky-local-variable t)
1089
1090 (defcustom gnus-alter-header-function nil
1091   "Function called to allow alteration of article header structures.
1092 The function is called with one parameter, the article header vector,
1093 which it may alter in any way."
1094   :type '(choice (const :tag "None" nil)
1095                  function)
1096   :group 'gnus-summary)
1097
1098 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1099   "Function used to decode a string with encoded words.")
1100
1101 (defvar gnus-decode-encoded-address-function
1102   'mail-decode-encoded-address-string
1103   "Function used to decode addresses with encoded words.")
1104
1105 (defcustom gnus-extra-headers '(To Newsgroups)
1106   "*Extra headers to parse."
1107   :version "21.1"
1108   :group 'gnus-summary
1109   :type '(repeat symbol))
1110
1111 (defcustom gnus-ignored-from-addresses
1112   (and user-mail-address
1113        (not (string= user-mail-address ""))
1114        (regexp-quote user-mail-address))
1115   "*From headers that may be suppressed in favor of To headers.
1116 This can be a regexp or a list of regexps."
1117   :version "21.1"
1118   :group 'gnus-summary
1119   :type '(choice regexp
1120                  (repeat :tag "Regexp List" regexp)))
1121
1122 (defsubst gnus-ignored-from-addresses ()
1123   (gmm-regexp-concat gnus-ignored-from-addresses))
1124
1125 (defcustom gnus-summary-to-prefix "-> "
1126   "*String prefixed to the To field in the summary line when
1127 using `gnus-ignored-from-addresses'."
1128   :version "22.1"
1129   :group 'gnus-summary
1130   :type 'string)
1131
1132 (defcustom gnus-summary-newsgroup-prefix "=> "
1133   "*String prefixed to the Newsgroup field in the summary
1134 line when using `gnus-ignored-from-addresses'."
1135   :version "22.1"
1136   :group 'gnus-summary
1137   :type 'string)
1138
1139 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1140   "List of charsets that should be ignored.
1141 When these charsets are used in the \"charset\" parameter, the
1142 default charset will be used instead."
1143   :version "21.1"
1144   :type '(repeat symbol)
1145   :group 'gnus-charset)
1146
1147 (defcustom gnus-newsgroup-maximum-articles nil
1148   "The maximum number of articles a newsgroup.
1149 If this is a number, old articles in a newsgroup exceeding this number
1150 are silently ignored.  If it is nil, no article is ignored.  Note that
1151 setting this variable to a number might prevent you from reading very
1152 old articles."
1153   :group 'gnus-group-select
1154   :version "22.2"
1155   :type '(choice (const :tag "No limit" nil)
1156                  integer))
1157
1158 (gnus-define-group-parameter
1159  ignored-charsets
1160  :type list
1161  :function-document
1162  "Return the ignored charsets of GROUP."
1163  :variable gnus-group-ignored-charsets-alist
1164  :variable-default
1165  '(("alt\\.chinese\\.text" iso-8859-1))
1166  :variable-document
1167  "Alist of regexps (to match group names) and charsets that should be ignored.
1168 When these charsets are used in the \"charset\" parameter, the
1169 default charset will be used instead."
1170  :variable-group gnus-charset
1171  :variable-type '(repeat (cons (regexp :tag "Group")
1172                                (repeat symbol)))
1173  :parameter-type '(choice :tag "Ignored charsets"
1174                           :value nil
1175                           (repeat (symbol)))
1176  :parameter-document       "\
1177 List of charsets that should be ignored.
1178
1179 When these charsets are used in the \"charset\" parameter, the
1180 default charset will be used instead.")
1181
1182 (defcustom gnus-group-highlight-words-alist nil
1183   "Alist of group regexps and highlight regexps.
1184 This variable uses the same syntax as `gnus-emphasis-alist'."
1185   :version "21.1"
1186   :type '(repeat (cons (regexp :tag "Group")
1187                        (repeat (list (regexp :tag "Highlight regexp")
1188                                      (number :tag "Group for entire word" 0)
1189                                      (number :tag "Group for displayed part" 0)
1190                                      (symbol :tag "Face"
1191                                              gnus-emphasis-highlight-words)))))
1192   :group 'gnus-summary-visual)
1193
1194 (defcustom gnus-summary-show-article-charset-alist
1195   nil
1196   "Alist of number and charset.
1197 The article will be shown with the charset corresponding to the
1198 numbered argument.
1199 For example: ((1 . cn-gb-2312) (2 . big5))."
1200   :version "21.1"
1201   :type '(repeat (cons (number :tag "Argument" 1)
1202                        (symbol :tag "Charset")))
1203   :group 'gnus-charset)
1204
1205 (defcustom gnus-preserve-marks t
1206   "Whether marks are preserved when moving, copying and respooling messages."
1207   :version "21.1"
1208   :type 'boolean
1209   :group 'gnus-summary-marks)
1210
1211 (defcustom gnus-alter-articles-to-read-function nil
1212   "Function to be called to alter the list of articles to be selected."
1213   :type '(choice (const nil) function)
1214   :group 'gnus-summary)
1215
1216 (defcustom gnus-orphan-score nil
1217   "*All orphans get this score added.  Set in the score file."
1218   :group 'gnus-score-default
1219   :type '(choice (const nil)
1220                  integer))
1221
1222 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1223   "*A regexp to match MIME parts when saving multiple parts of a
1224 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1225 This regexp will be used by default when prompting the user for which
1226 type of files to save."
1227   :group 'gnus-summary
1228   :type 'regexp)
1229
1230 (defcustom gnus-read-all-available-headers nil
1231   "Whether Gnus should parse all headers made available to it.
1232 This is mostly relevant for slow back ends where the user may
1233 wish to widen the summary buffer to include all headers
1234 that were fetched.  Say, for nnultimate groups."
1235   :version "22.1"
1236   :group 'gnus-summary
1237   :type '(choice boolean regexp))
1238
1239 (defcustom gnus-summary-muttprint-program "muttprint"
1240   "Command (and optional arguments) used to run Muttprint."
1241   :version "22.1"
1242   :group 'gnus-summary
1243   :type 'string)
1244
1245 (defcustom gnus-article-loose-mime t
1246   "If non-nil, don't require MIME-Version header.
1247 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1248 supply the MIME-Version header or deliberately strip it from the mail.
1249 If non-nil (the default), Gnus will treat some articles as MIME
1250 even if the MIME-Version header is missing."
1251   :version "22.1"
1252   :type 'boolean
1253   :group 'gnus-article-mime)
1254
1255 (defcustom gnus-article-emulate-mime t
1256   "If non-nil, use MIME emulation for uuencode and the like.
1257 This means that Gnus will search message bodies for text that look
1258 like uuencoded bits, yEncoded bits, and so on, and present that using
1259 the normal Gnus MIME machinery."
1260   :version "22.1"
1261   :type 'boolean
1262   :group 'gnus-article-mime)
1263
1264 ;;; Internal variables
1265
1266 (defvar gnus-summary-display-cache nil)
1267 (defvar gnus-article-mime-handles nil)
1268 (defvar gnus-article-decoded-p nil)
1269 (defvar gnus-article-charset nil)
1270 (defvar gnus-article-ignored-charsets nil)
1271 (defvar gnus-scores-exclude-files nil)
1272 (defvar gnus-page-broken nil)
1273
1274 (defvar gnus-original-article nil)
1275 (defvar gnus-article-internal-prepare-hook nil)
1276 (defvar gnus-newsgroup-process-stack nil)
1277
1278 (defvar gnus-thread-indent-array nil)
1279 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1280 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1281   "Function called to sort the articles within a thread after it has been gathered together.")
1282
1283 (defvar gnus-summary-save-parts-type-history nil)
1284 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1285
1286 ;; Avoid highlighting in kill files.
1287 (defvar gnus-summary-inhibit-highlight nil)
1288 (defvar gnus-newsgroup-selected-overlay nil)
1289 (defvar gnus-inhibit-limiting nil)
1290 (defvar gnus-newsgroup-adaptive-score-file nil)
1291 (defvar gnus-current-score-file nil)
1292 (defvar gnus-current-move-group nil)
1293 (defvar gnus-current-copy-group nil)
1294 (defvar gnus-current-crosspost-group nil)
1295 (defvar gnus-newsgroup-display nil)
1296
1297 (defvar gnus-newsgroup-dependencies nil)
1298 (defvar gnus-newsgroup-adaptive nil)
1299 (defvar gnus-summary-display-article-function nil)
1300 (defvar gnus-summary-highlight-line-function nil
1301   "Function called after highlighting a summary line.")
1302
1303 (defvar gnus-summary-line-format-alist
1304   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1305     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1306     (?s gnus-tmp-subject-or-nil ?s)
1307     (?n gnus-tmp-name ?s)
1308     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1309         ?s)
1310     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1311             gnus-tmp-from) ?s)
1312     (?F gnus-tmp-from ?s)
1313     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1314     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1315     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1316     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1317     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1318     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1319     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1320     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1321     (?L gnus-tmp-lines ?s)
1322     (?O gnus-tmp-downloaded ?c)
1323     (?I gnus-tmp-indentation ?s)
1324     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1325     (?R gnus-tmp-replied ?c)
1326     (?\[ gnus-tmp-opening-bracket ?c)
1327     (?\] gnus-tmp-closing-bracket ?c)
1328     (?\> (make-string gnus-tmp-level ? ) ?s)
1329     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1330     (?i gnus-tmp-score ?d)
1331     (?z gnus-tmp-score-char ?c)
1332     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1333     (?U gnus-tmp-unread ?c)
1334     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1335         ?s)
1336     (?t (gnus-summary-number-of-articles-in-thread
1337          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1338         ?d)
1339     (?e (gnus-summary-number-of-articles-in-thread
1340          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1341         ?c)
1342     (?u gnus-tmp-user-defined ?s)
1343     (?P (gnus-pick-line-number) ?d)
1344     (?B gnus-tmp-thread-tree-header-string ?s)
1345     (user-date (gnus-user-date
1346                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1347   "An alist of format specifications that can appear in summary lines.
1348 These are paired with what variables they correspond with, along with
1349 the type of the variable (string, integer, character, etc).")
1350
1351 (defvar gnus-summary-dummy-line-format-alist
1352   `((?S gnus-tmp-subject ?s)
1353     (?N gnus-tmp-number ?d)
1354     (?u gnus-tmp-user-defined ?s)))
1355
1356 (defvar gnus-summary-mode-line-format-alist
1357   `((?G gnus-tmp-group-name ?s)
1358     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1359     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1360     (?A gnus-tmp-article-number ?d)
1361     (?Z gnus-tmp-unread-and-unselected ?s)
1362     (?V gnus-version ?s)
1363     (?U gnus-tmp-unread-and-unticked ?d)
1364     (?S gnus-tmp-subject ?s)
1365     (?e gnus-tmp-unselected ?d)
1366     (?u gnus-tmp-user-defined ?s)
1367     (?d (length gnus-newsgroup-dormant) ?d)
1368     (?t (length gnus-newsgroup-marked) ?d)
1369     (?h (length gnus-newsgroup-spam-marked) ?d)
1370     (?r (length gnus-newsgroup-reads) ?d)
1371     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1372     (?E gnus-newsgroup-expunged-tally ?d)
1373     (?s (gnus-current-score-file-nondirectory) ?s)))
1374
1375 (defvar gnus-last-search-regexp nil
1376   "Default regexp for article search command.")
1377
1378 (defvar gnus-last-shell-command nil
1379   "Default shell command on article.")
1380
1381 (defvar gnus-newsgroup-agentized nil
1382   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1383 (defvar gnus-newsgroup-begin nil)
1384 (defvar gnus-newsgroup-end nil)
1385 (defvar gnus-newsgroup-last-rmail nil)
1386 (defvar gnus-newsgroup-last-mail nil)
1387 (defvar gnus-newsgroup-last-folder nil)
1388 (defvar gnus-newsgroup-last-file nil)
1389 (defvar gnus-newsgroup-last-directory nil)
1390 (defvar gnus-newsgroup-auto-expire nil)
1391 (defvar gnus-newsgroup-active nil)
1392
1393 (defvar gnus-newsgroup-data nil)
1394 (defvar gnus-newsgroup-data-reverse nil)
1395 (defvar gnus-newsgroup-limit nil)
1396 (defvar gnus-newsgroup-limits nil)
1397 (defvar gnus-summary-use-undownloaded-faces nil)
1398
1399 (defvar gnus-newsgroup-unreads nil
1400   "Sorted list of unread articles in the current newsgroup.")
1401
1402 (defvar gnus-newsgroup-unselected nil
1403   "Sorted list of unselected unread articles in the current newsgroup.")
1404
1405 (defvar gnus-newsgroup-reads nil
1406   "Alist of read articles and article marks in the current newsgroup.")
1407
1408 (defvar gnus-newsgroup-expunged-tally nil)
1409
1410 (defvar gnus-newsgroup-marked nil
1411   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1412
1413 (defvar gnus-newsgroup-spam-marked nil
1414   "List of ranges of articles that have been marked as spam.")
1415
1416 (defvar gnus-newsgroup-killed nil
1417   "List of ranges of articles that have been through the scoring process.")
1418
1419 (defvar gnus-newsgroup-cached nil
1420   "Sorted list of articles that come from the article cache.")
1421
1422 (defvar gnus-newsgroup-saved nil
1423   "List of articles that have been saved.")
1424
1425 (defvar gnus-newsgroup-kill-headers nil)
1426
1427 (defvar gnus-newsgroup-replied nil
1428   "List of articles that have been replied to in the current newsgroup.")
1429
1430 (defvar gnus-newsgroup-forwarded nil
1431   "List of articles that have been forwarded in the current newsgroup.")
1432
1433 (defvar gnus-newsgroup-recent nil
1434   "List of articles that have are recent in the current newsgroup.")
1435
1436 (defvar gnus-newsgroup-expirable nil
1437   "Sorted list of articles in the current newsgroup that can be expired.")
1438
1439 (defvar gnus-newsgroup-processable nil
1440   "List of articles in the current newsgroup that can be processed.")
1441
1442 (defvar gnus-newsgroup-downloadable nil
1443   "Sorted list of articles in the current newsgroup that can be processed.")
1444
1445 (defvar gnus-newsgroup-unfetched nil
1446   "Sorted list of articles in the current newsgroup whose headers have
1447 not been fetched into the agent.
1448
1449 This list will always be a subset of gnus-newsgroup-undownloaded.")
1450
1451 (defvar gnus-newsgroup-undownloaded nil
1452   "List of articles in the current newsgroup that haven't been downloaded.")
1453
1454 (defvar gnus-newsgroup-unsendable nil
1455   "List of articles in the current newsgroup that won't be sent.")
1456
1457 (defvar gnus-newsgroup-bookmarks nil
1458   "List of articles in the current newsgroup that have bookmarks.")
1459
1460 (defvar gnus-newsgroup-dormant nil
1461   "Sorted list of dormant articles in the current newsgroup.")
1462
1463 (defvar gnus-newsgroup-unseen nil
1464   "List of unseen articles in the current newsgroup.")
1465
1466 (defvar gnus-newsgroup-seen nil
1467   "Range of seen articles in the current newsgroup.")
1468
1469 (defvar gnus-newsgroup-articles nil
1470   "List of articles in the current newsgroup.")
1471
1472 (defvar gnus-newsgroup-scored nil
1473   "List of scored articles in the current newsgroup.")
1474
1475 (defvar gnus-newsgroup-headers nil
1476   "List of article headers in the current newsgroup.")
1477
1478 (defvar gnus-newsgroup-threads nil)
1479
1480 (defvar gnus-newsgroup-prepared nil
1481   "Whether the current group has been prepared properly.")
1482
1483 (defvar gnus-newsgroup-ancient nil
1484   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1485
1486 (defvar gnus-newsgroup-sparse nil)
1487
1488 (defvar gnus-current-article nil)
1489 (defvar gnus-article-current nil)
1490 (defvar gnus-current-headers nil)
1491 (defvar gnus-have-all-headers nil)
1492 (defvar gnus-last-article nil)
1493 (defvar gnus-newsgroup-history nil)
1494 (defvar gnus-newsgroup-charset nil)
1495 (defvar gnus-newsgroup-ephemeral-charset nil)
1496 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1497
1498 (defvar gnus-article-before-search nil)
1499
1500 (defvar gnus-summary-local-variables
1501   '(gnus-newsgroup-name
1502     gnus-newsgroup-begin gnus-newsgroup-end
1503     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1504     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1505     gnus-newsgroup-last-directory
1506     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1507     gnus-newsgroup-unselected gnus-newsgroup-marked
1508     gnus-newsgroup-spam-marked
1509     gnus-newsgroup-reads gnus-newsgroup-saved
1510     gnus-newsgroup-replied gnus-newsgroup-forwarded
1511     gnus-newsgroup-recent
1512     gnus-newsgroup-expirable
1513     gnus-newsgroup-processable gnus-newsgroup-killed
1514     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1515     gnus-newsgroup-unfetched
1516     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1517     gnus-newsgroup-seen gnus-newsgroup-articles
1518     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1519     gnus-newsgroup-headers gnus-newsgroup-threads
1520     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1521     gnus-current-article gnus-current-headers gnus-have-all-headers
1522     gnus-last-article gnus-article-internal-prepare-hook
1523     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1524     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1525     gnus-thread-expunge-below
1526     gnus-score-alist gnus-current-score-file
1527     (gnus-summary-expunge-below . global)
1528     (gnus-summary-mark-below . global)
1529     (gnus-orphan-score . global)
1530     gnus-newsgroup-active gnus-scores-exclude-files
1531     gnus-newsgroup-history gnus-newsgroup-ancient
1532     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1533     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1534     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1535     (gnus-newsgroup-expunged-tally . 0)
1536     gnus-cache-removable-articles gnus-newsgroup-cached
1537     gnus-newsgroup-data gnus-newsgroup-data-reverse
1538     gnus-newsgroup-limit gnus-newsgroup-limits
1539     gnus-newsgroup-charset gnus-newsgroup-display
1540     gnus-summary-use-undownloaded-faces)
1541   "Variables that are buffer-local to the summary buffers.")
1542
1543 (defvar gnus-newsgroup-variables nil
1544   "A list of variables that have separate values in different newsgroups.
1545 A list of newsgroup (summary buffer) local variables, or cons of
1546 variables and their default expressions to be evalled (when the default
1547 values are not nil), that should be made global while the summary buffer
1548 is active.
1549
1550 Note: The default expressions will be evaluated (using function `eval')
1551 before assignment to the local variable rather than just assigned to it.
1552 If the default expression is the symbol `global', that symbol will not
1553 be evaluated but the global value of the local variable will be used
1554 instead.
1555
1556 These variables can be used to set variables in the group parameters
1557 while still allowing them to affect operations done in other buffers.
1558 For example:
1559
1560 \(setq gnus-newsgroup-variables
1561      '(message-use-followup-to
1562        (gnus-visible-headers .
1563          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1564 ")
1565
1566 ;; Byte-compiler warning.  Specifically, this is responsible for:
1567 ;; "Warning: the following functions might not be defined at runtime:
1568 ;; gnus-build-sparse-threads, gnus-dead-summary-mode, gnus-summary-mark-below".
1569 (eval-when-compile
1570   ;; Bind features so that require will believe that gnus-sum has
1571   ;; already been loaded (avoids infinite recursion)
1572   (let ((features (cons 'gnus-sum features)))
1573     ;; Several of the declarations in gnus-sum are needed to load the
1574     ;; following files. Right now, these definitions have been
1575     ;; compiled but not defined (evaluated).  We could either do a
1576     ;; eval-and-compile about all of the declarations or evaluate the
1577     ;; source file.
1578     (if (boundp 'gnus-newsgroup-variables)
1579         nil
1580       (load "gnus-sum.el" t t t))
1581     (require 'gnus)
1582     (require 'gnus-art)))
1583
1584 ;; MIME stuff.
1585
1586 (defvar gnus-decode-encoded-word-methods
1587   '(mail-decode-encoded-word-string)
1588   "List of methods used to decode encoded words.
1589
1590 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1591 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1592 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1593 whose names match REGEXP.
1594
1595 For example:
1596 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1597  mail-decode-encoded-word-string
1598  (\"chinese\" . rfc1843-decode-string))")
1599
1600 (defvar gnus-decode-encoded-word-methods-cache nil)
1601
1602 (defun gnus-multi-decode-encoded-word-string (string)
1603   "Apply the functions from `gnus-encoded-word-methods' that match."
1604   (unless (and gnus-decode-encoded-word-methods-cache
1605                (eq gnus-newsgroup-name
1606                    (car gnus-decode-encoded-word-methods-cache)))
1607     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1608     (dolist (method gnus-decode-encoded-word-methods)
1609       (if (symbolp method)
1610           (nconc gnus-decode-encoded-word-methods-cache (list method))
1611         (if (and gnus-newsgroup-name
1612                  (string-match (car method) gnus-newsgroup-name))
1613             (nconc gnus-decode-encoded-word-methods-cache
1614                    (list (cdr method)))))))
1615   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1616     (setq string (funcall method string))))
1617
1618 ;; Subject simplification.
1619
1620 (defun gnus-simplify-whitespace (str)
1621   "Remove excessive whitespace from STR."
1622   ;; Multiple spaces.
1623   (while (string-match "[ \t][ \t]+" str)
1624     (setq str (concat (substring str 0 (match-beginning 0))
1625                         " "
1626                         (substring str (match-end 0)))))
1627   ;; Leading spaces.
1628   (when (string-match "^[ \t]+" str)
1629     (setq str (substring str (match-end 0))))
1630   ;; Trailing spaces.
1631   (when (string-match "[ \t]+$" str)
1632     (setq str (substring str 0 (match-beginning 0))))
1633   str)
1634
1635 (defun gnus-simplify-all-whitespace (str)
1636   "Remove all whitespace from STR."
1637   (while (string-match "[ \t\n]+" str)
1638     (setq str (replace-match "" nil nil str)))
1639   str)
1640
1641 (defsubst gnus-simplify-subject-re (subject)
1642   "Remove \"Re:\" from subject lines."
1643   (if (string-match message-subject-re-regexp subject)
1644       (substring subject (match-end 0))
1645     subject))
1646
1647 (defun gnus-simplify-subject (subject &optional re-only)
1648   "Remove `Re:' and words in parentheses.
1649 If RE-ONLY is non-nil, strip leading `Re:'s only."
1650   (let ((case-fold-search t))           ;Ignore case.
1651     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1652     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1653       (setq subject (substring subject (match-end 0))))
1654     ;; Remove uninteresting prefixes.
1655     (when (and (not re-only)
1656                gnus-simplify-ignored-prefixes
1657                (string-match gnus-simplify-ignored-prefixes subject))
1658       (setq subject (substring subject (match-end 0))))
1659     ;; Remove words in parentheses from end.
1660     (unless re-only
1661       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1662         (setq subject (substring subject 0 (match-beginning 0)))))
1663     ;; Return subject string.
1664     subject))
1665
1666 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1667 ;; all whitespace.
1668 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1669   (goto-char (point-min))
1670   (while (re-search-forward regexp nil t)
1671     (replace-match (or newtext ""))))
1672
1673 (defun gnus-simplify-buffer-fuzzy ()
1674   "Simplify string in the buffer fuzzily.
1675 The string in the accessible portion of the current buffer is simplified.
1676 It is assumed to be a single-line subject.
1677 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1678 matter is removed.  Additional things can be deleted by setting
1679 `gnus-simplify-subject-fuzzy-regexp'."
1680   (let ((case-fold-search t)
1681         (modified-tick))
1682     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1683
1684     (while (not (eq modified-tick (buffer-modified-tick)))
1685       (setq modified-tick (buffer-modified-tick))
1686       (cond
1687        ((listp gnus-simplify-subject-fuzzy-regexp)
1688         (mapc 'gnus-simplify-buffer-fuzzy-step
1689               gnus-simplify-subject-fuzzy-regexp))
1690        (gnus-simplify-subject-fuzzy-regexp
1691         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1692       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1693       (gnus-simplify-buffer-fuzzy-step
1694        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1695       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1696
1697     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1698     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1699     (gnus-simplify-buffer-fuzzy-step " $")
1700     (gnus-simplify-buffer-fuzzy-step "^ +")))
1701
1702 (defun gnus-simplify-subject-fuzzy (subject)
1703   "Simplify a subject string fuzzily.
1704 See `gnus-simplify-buffer-fuzzy' for details."
1705   (save-excursion
1706     (gnus-set-work-buffer)
1707     (let ((case-fold-search t))
1708       ;; Remove uninteresting prefixes.
1709       (when (and gnus-simplify-ignored-prefixes
1710                  (string-match gnus-simplify-ignored-prefixes subject))
1711         (setq subject (substring subject (match-end 0))))
1712       (insert subject)
1713       (inline (gnus-simplify-buffer-fuzzy))
1714       (buffer-string))))
1715
1716 (defsubst gnus-simplify-subject-fully (subject)
1717   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1718   (cond
1719    (gnus-simplify-subject-functions
1720     (gnus-map-function gnus-simplify-subject-functions subject))
1721    ((null gnus-summary-gather-subject-limit)
1722     (gnus-simplify-subject-re subject))
1723    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1724     (gnus-simplify-subject-fuzzy subject))
1725    ((numberp gnus-summary-gather-subject-limit)
1726     (truncate-string-to-width (gnus-simplify-subject-re subject)
1727                               gnus-summary-gather-subject-limit))
1728    (t
1729     subject)))
1730
1731 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1732   "Check whether two subjects are equal.
1733 If optional argument SIMPLE-FIRST is t, first argument is already
1734 simplified."
1735   (cond
1736    ((null simple-first)
1737     (equal (gnus-simplify-subject-fully s1)
1738            (gnus-simplify-subject-fully s2)))
1739    (t
1740     (equal s1
1741            (gnus-simplify-subject-fully s2)))))
1742
1743 (defun gnus-summary-bubble-group ()
1744   "Increase the score of the current group.
1745 This is a handy function to add to `gnus-summary-exit-hook' to
1746 increase the score of each group you read."
1747   (gnus-group-add-score gnus-newsgroup-name))
1748
1749 \f
1750 ;;;
1751 ;;; Gnus summary mode
1752 ;;;
1753
1754 (put 'gnus-summary-mode 'mode-class 'special)
1755
1756 (defvar gnus-article-commands-menu)
1757
1758 ;; Non-orthogonal keys
1759
1760 (gnus-define-keys gnus-summary-mode-map
1761   " " gnus-summary-next-page
1762   "\177" gnus-summary-prev-page
1763   [delete] gnus-summary-prev-page
1764   [backspace] gnus-summary-prev-page
1765   "\r" gnus-summary-scroll-up
1766   "\M-\r" gnus-summary-scroll-down
1767   "n" gnus-summary-next-unread-article
1768   "p" gnus-summary-prev-unread-article
1769   "N" gnus-summary-next-article
1770   "P" gnus-summary-prev-article
1771   "\M-\C-n" gnus-summary-next-same-subject
1772   "\M-\C-p" gnus-summary-prev-same-subject
1773   "\M-n" gnus-summary-next-unread-subject
1774   "\M-p" gnus-summary-prev-unread-subject
1775   "." gnus-summary-first-unread-article
1776   "," gnus-summary-best-unread-article
1777   "\M-s" gnus-summary-search-article-forward
1778   "\M-r" gnus-summary-search-article-backward
1779   "\M-S" gnus-summary-repeat-search-article-forward
1780   "\M-R" gnus-summary-repeat-search-article-backward
1781   "<" gnus-summary-beginning-of-article
1782   ">" gnus-summary-end-of-article
1783   "j" gnus-summary-goto-article
1784   "^" gnus-summary-refer-parent-article
1785   "\M-^" gnus-summary-refer-article
1786   "u" gnus-summary-tick-article-forward
1787   "!" gnus-summary-tick-article-forward
1788   "U" gnus-summary-tick-article-backward
1789   "d" gnus-summary-mark-as-read-forward
1790   "D" gnus-summary-mark-as-read-backward
1791   "E" gnus-summary-mark-as-expirable
1792   "\M-u" gnus-summary-clear-mark-forward
1793   "\M-U" gnus-summary-clear-mark-backward
1794   "k" gnus-summary-kill-same-subject-and-select
1795   "\C-k" gnus-summary-kill-same-subject
1796   "\M-\C-k" gnus-summary-kill-thread
1797   "\M-\C-l" gnus-summary-lower-thread
1798   "e" gnus-summary-edit-article
1799   "#" gnus-summary-mark-as-processable
1800   "\M-#" gnus-summary-unmark-as-processable
1801   "\M-\C-t" gnus-summary-toggle-threads
1802   "\M-\C-s" gnus-summary-show-thread
1803   "\M-\C-h" gnus-summary-hide-thread
1804   "\M-\C-f" gnus-summary-next-thread
1805   "\M-\C-b" gnus-summary-prev-thread
1806   [(meta down)] gnus-summary-next-thread
1807   [(meta up)] gnus-summary-prev-thread
1808   "\M-\C-u" gnus-summary-up-thread
1809   "\M-\C-d" gnus-summary-down-thread
1810   "&" gnus-summary-execute-command
1811   "c" gnus-summary-catchup-and-exit
1812   "\C-w" gnus-summary-mark-region-as-read
1813   "\C-t" gnus-summary-toggle-truncation
1814   "?" gnus-summary-mark-as-dormant
1815   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1816   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1817   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1818   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1819   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1820   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1821   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1822   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1823   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1824   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1825   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1826   "=" gnus-summary-expand-window
1827   "\C-x\C-s" gnus-summary-reselect-current-group
1828   "\M-g" gnus-summary-rescan-group
1829   "w" gnus-summary-stop-page-breaking
1830   "\C-c\C-r" gnus-summary-caesar-message
1831   "f" gnus-summary-followup
1832   "F" gnus-summary-followup-with-original
1833   "C" gnus-summary-cancel-article
1834   "r" gnus-summary-reply
1835   "R" gnus-summary-reply-with-original
1836   "\C-c\C-f" gnus-summary-mail-forward
1837   "o" gnus-summary-save-article
1838   "\C-o" gnus-summary-save-article-mail
1839   "|" gnus-summary-pipe-output
1840   "\M-k" gnus-summary-edit-local-kill
1841   "\M-K" gnus-summary-edit-global-kill
1842   ;; "V" gnus-version
1843   "\C-c\C-d" gnus-summary-describe-group
1844   "q" gnus-summary-exit
1845   "Q" gnus-summary-exit-no-update
1846   "\C-c\C-i" gnus-info-find-node
1847   gnus-mouse-2 gnus-mouse-pick-article
1848   [follow-link] mouse-face
1849   "m" gnus-summary-mail-other-window
1850   "a" gnus-summary-post-news
1851   "i" gnus-summary-news-other-window
1852   "x" gnus-summary-limit-to-unread
1853   "s" gnus-summary-isearch-article
1854   "t" gnus-summary-toggle-header
1855   "g" gnus-summary-show-article
1856   "l" gnus-summary-goto-last-article
1857   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1858   "\C-d" gnus-summary-enter-digest-group
1859   "\M-\C-d" gnus-summary-read-document
1860   "\M-\C-e" gnus-summary-edit-parameters
1861   "\M-\C-a" gnus-summary-customize-parameters
1862   "\C-c\C-b" gnus-bug
1863   "*" gnus-cache-enter-article
1864   "\M-*" gnus-cache-remove-article
1865   "\M-&" gnus-summary-universal-argument
1866   "\C-l" gnus-recenter
1867   "I" gnus-summary-increase-score
1868   "L" gnus-summary-lower-score
1869   "\M-i" gnus-symbolic-argument
1870   "h" gnus-summary-select-article-buffer
1871
1872   "b" gnus-article-view-part
1873   "\M-t" gnus-summary-toggle-display-buttonized
1874
1875   "V" gnus-summary-score-map
1876   "X" gnus-uu-extract-map
1877   "S" gnus-summary-send-map)
1878
1879 ;; Sort of orthogonal keymap
1880 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1881   "t" gnus-summary-tick-article-forward
1882   "!" gnus-summary-tick-article-forward
1883   "d" gnus-summary-mark-as-read-forward
1884   "r" gnus-summary-mark-as-read-forward
1885   "c" gnus-summary-clear-mark-forward
1886   " " gnus-summary-clear-mark-forward
1887   "e" gnus-summary-mark-as-expirable
1888   "x" gnus-summary-mark-as-expirable
1889   "?" gnus-summary-mark-as-dormant
1890   "b" gnus-summary-set-bookmark
1891   "B" gnus-summary-remove-bookmark
1892   "#" gnus-summary-mark-as-processable
1893   "\M-#" gnus-summary-unmark-as-processable
1894   "S" gnus-summary-limit-include-expunged
1895   "C" gnus-summary-catchup
1896   "H" gnus-summary-catchup-to-here
1897   "h" gnus-summary-catchup-from-here
1898   "\C-c" gnus-summary-catchup-all
1899   "k" gnus-summary-kill-same-subject-and-select
1900   "K" gnus-summary-kill-same-subject
1901   "P" gnus-uu-mark-map)
1902
1903 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1904   "c" gnus-summary-clear-above
1905   "u" gnus-summary-tick-above
1906   "m" gnus-summary-mark-above
1907   "k" gnus-summary-kill-below)
1908
1909 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1910   "/" gnus-summary-limit-to-subject
1911   "n" gnus-summary-limit-to-articles
1912   "b" gnus-summary-limit-to-bodies
1913   "h" gnus-summary-limit-to-headers
1914   "w" gnus-summary-pop-limit
1915   "s" gnus-summary-limit-to-subject
1916   "a" gnus-summary-limit-to-author
1917   "u" gnus-summary-limit-to-unread
1918   "m" gnus-summary-limit-to-marks
1919   "M" gnus-summary-limit-exclude-marks
1920   "v" gnus-summary-limit-to-score
1921   "*" gnus-summary-limit-include-cached
1922   "D" gnus-summary-limit-include-dormant
1923   "T" gnus-summary-limit-include-thread
1924   "d" gnus-summary-limit-exclude-dormant
1925   "t" gnus-summary-limit-to-age
1926   "." gnus-summary-limit-to-unseen
1927   "x" gnus-summary-limit-to-extra
1928   "p" gnus-summary-limit-to-display-predicate
1929   "E" gnus-summary-limit-include-expunged
1930   "c" gnus-summary-limit-exclude-childless-dormant
1931   "C" gnus-summary-limit-mark-excluded-as-read
1932   "o" gnus-summary-insert-old-articles
1933   "N" gnus-summary-insert-new-articles
1934   "S" gnus-summary-limit-to-singletons
1935   "r" gnus-summary-limit-to-replied
1936   "R" gnus-summary-limit-to-recipient
1937   "A" gnus-summary-limit-to-address)
1938
1939 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1940   "n" gnus-summary-next-unread-article
1941   "p" gnus-summary-prev-unread-article
1942   "N" gnus-summary-next-article
1943   "P" gnus-summary-prev-article
1944   "\C-n" gnus-summary-next-same-subject
1945   "\C-p" gnus-summary-prev-same-subject
1946   "\M-n" gnus-summary-next-unread-subject
1947   "\M-p" gnus-summary-prev-unread-subject
1948   "f" gnus-summary-first-unread-article
1949   "b" gnus-summary-best-unread-article
1950   "j" gnus-summary-goto-article
1951   "g" gnus-summary-goto-subject
1952   "l" gnus-summary-goto-last-article
1953   "o" gnus-summary-pop-article)
1954
1955 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1956   "k" gnus-summary-kill-thread
1957   "E" gnus-summary-expire-thread
1958   "l" gnus-summary-lower-thread
1959   "i" gnus-summary-raise-thread
1960   "T" gnus-summary-toggle-threads
1961   "t" gnus-summary-rethread-current
1962   "^" gnus-summary-reparent-thread
1963   "\M-^" gnus-summary-reparent-children
1964   "s" gnus-summary-show-thread
1965   "S" gnus-summary-show-all-threads
1966   "h" gnus-summary-hide-thread
1967   "H" gnus-summary-hide-all-threads
1968   "n" gnus-summary-next-thread
1969   "p" gnus-summary-prev-thread
1970   "u" gnus-summary-up-thread
1971   "o" gnus-summary-top-thread
1972   "d" gnus-summary-down-thread
1973   "#" gnus-uu-mark-thread
1974   "\M-#" gnus-uu-unmark-thread)
1975
1976 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1977   "g" gnus-summary-prepare
1978   "c" gnus-summary-insert-cached-articles
1979   "d" gnus-summary-insert-dormant-articles
1980   "t" gnus-summary-insert-ticked-articles)
1981
1982 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1983   "c" gnus-summary-catchup-and-exit
1984   "C" gnus-summary-catchup-all-and-exit
1985   "E" gnus-summary-exit-no-update
1986   "Q" gnus-summary-exit
1987   "Z" gnus-summary-exit
1988   "n" gnus-summary-catchup-and-goto-next-group
1989   "p" gnus-summary-catchup-and-goto-prev-group
1990   "R" gnus-summary-reselect-current-group
1991   "G" gnus-summary-rescan-group
1992   "N" gnus-summary-next-group
1993   "s" gnus-summary-save-newsrc
1994   "P" gnus-summary-prev-group)
1995
1996 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1997   " " gnus-summary-next-page
1998   "n" gnus-summary-next-page
1999   "\177" gnus-summary-prev-page
2000   [delete] gnus-summary-prev-page
2001   "p" gnus-summary-prev-page
2002   "\r" gnus-summary-scroll-up
2003   "\M-\r" gnus-summary-scroll-down
2004   "<" gnus-summary-beginning-of-article
2005   ">" gnus-summary-end-of-article
2006   "b" gnus-summary-beginning-of-article
2007   "e" gnus-summary-end-of-article
2008   "^" gnus-summary-refer-parent-article
2009   "r" gnus-summary-refer-parent-article
2010   "D" gnus-summary-enter-digest-group
2011   "R" gnus-summary-refer-references
2012   "T" gnus-summary-refer-thread
2013   "g" gnus-summary-show-article
2014   "s" gnus-summary-isearch-article
2015   "P" gnus-summary-print-article
2016   "S" gnus-sticky-article
2017   "M" gnus-mailing-list-insinuate
2018   "t" gnus-article-babel)
2019
2020 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2021   "b" gnus-article-add-buttons
2022   "B" gnus-article-add-buttons-to-head
2023   "o" gnus-article-treat-overstrike
2024   "e" gnus-article-emphasize
2025   "w" gnus-article-fill-cited-article
2026   "Q" gnus-article-fill-long-lines
2027   "L" gnus-article-toggle-truncate-lines
2028   "C" gnus-article-capitalize-sentences
2029   "c" gnus-article-remove-cr
2030   "q" gnus-article-de-quoted-unreadable
2031   "6" gnus-article-de-base64-unreadable
2032   "Z" gnus-article-decode-HZ
2033   "A" gnus-article-treat-ansi-sequences
2034   "h" gnus-article-wash-html
2035   "u" gnus-article-unsplit-urls
2036   "s" gnus-summary-force-verify-and-decrypt
2037   "f" gnus-article-display-x-face
2038   "l" gnus-summary-stop-page-breaking
2039   "r" gnus-summary-caesar-message
2040   "m" gnus-summary-morse-message
2041   "t" gnus-summary-toggle-header
2042   "g" gnus-treat-smiley
2043   "v" gnus-summary-verbose-headers
2044   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2045   "p" gnus-article-verify-x-pgp-sig
2046   "d" gnus-article-treat-dumbquotes
2047   "i" gnus-summary-idna-message)
2048
2049 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2050   ;; mnemonic: deuglif*Y*
2051   "u" gnus-article-outlook-unwrap-lines
2052   "a" gnus-article-outlook-repair-attribution
2053   "c" gnus-article-outlook-rearrange-citation
2054   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2055
2056 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2057   "a" gnus-article-hide
2058   "h" gnus-article-hide-headers
2059   "b" gnus-article-hide-boring-headers
2060   "s" gnus-article-hide-signature
2061   "c" gnus-article-hide-citation
2062   "C" gnus-article-hide-citation-in-followups
2063   "l" gnus-article-hide-list-identifiers
2064   "B" gnus-article-strip-banner
2065   "P" gnus-article-hide-pem
2066   "\C-c" gnus-article-hide-citation-maybe)
2067
2068 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2069   "a" gnus-article-highlight
2070   "h" gnus-article-highlight-headers
2071   "c" gnus-article-highlight-citation
2072   "s" gnus-article-highlight-signature)
2073
2074 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2075   "f" gnus-article-treat-fold-headers
2076   "u" gnus-article-treat-unfold-headers
2077   "n" gnus-article-treat-fold-newsgroups)
2078
2079 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2080   "x" gnus-article-display-x-face
2081   "d" gnus-article-display-face
2082   "s" gnus-treat-smiley
2083   "D" gnus-article-remove-images
2084   "f" gnus-treat-from-picon
2085   "m" gnus-treat-mail-picon
2086   "n" gnus-treat-newsgroups-picon)
2087
2088 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2089   "w" gnus-article-decode-mime-words
2090   "c" gnus-article-decode-charset
2091   "v" gnus-mime-view-all-parts
2092   "b" gnus-article-view-part)
2093
2094 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2095   "z" gnus-article-date-ut
2096   "u" gnus-article-date-ut
2097   "l" gnus-article-date-local
2098   "p" gnus-article-date-english
2099   "e" gnus-article-date-lapsed
2100   "o" gnus-article-date-original
2101   "i" gnus-article-date-iso8601
2102   "s" gnus-article-date-user)
2103
2104 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2105   "t" gnus-article-remove-trailing-blank-lines
2106   "l" gnus-article-strip-leading-blank-lines
2107   "m" gnus-article-strip-multiple-blank-lines
2108   "a" gnus-article-strip-blank-lines
2109   "A" gnus-article-strip-all-blank-lines
2110   "s" gnus-article-strip-leading-space
2111   "e" gnus-article-strip-trailing-space
2112   "w" gnus-article-remove-leading-whitespace)
2113
2114 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2115   "v" gnus-version
2116   "f" gnus-summary-fetch-faq
2117   "d" gnus-summary-describe-group
2118   "h" gnus-summary-describe-briefly
2119   "i" gnus-info-find-node
2120   "c" gnus-group-fetch-charter
2121   "C" gnus-group-fetch-control)
2122
2123 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2124   "e" gnus-summary-expire-articles
2125   "\M-\C-e" gnus-summary-expire-articles-now
2126   "\177" gnus-summary-delete-article
2127   [delete] gnus-summary-delete-article
2128   [backspace] gnus-summary-delete-article
2129   "m" gnus-summary-move-article
2130   "r" gnus-summary-respool-article
2131   "w" gnus-summary-edit-article
2132   "c" gnus-summary-copy-article
2133   "B" gnus-summary-crosspost-article
2134   "q" gnus-summary-respool-query
2135   "t" gnus-summary-respool-trace
2136   "i" gnus-summary-import-article
2137   "I" gnus-summary-create-article
2138   "p" gnus-summary-article-posted-p)
2139
2140 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2141   "o" gnus-summary-save-article
2142   "m" gnus-summary-save-article-mail
2143   "F" gnus-summary-write-article-file
2144   "r" gnus-summary-save-article-rmail
2145   "f" gnus-summary-save-article-file
2146   "b" gnus-summary-save-article-body-file
2147   "B" gnus-summary-write-article-body-file
2148   "h" gnus-summary-save-article-folder
2149   "v" gnus-summary-save-article-vm
2150   "p" gnus-summary-pipe-output
2151   "P" gnus-summary-muttprint
2152   "s" gnus-soup-add-article)
2153
2154 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2155   "b" gnus-summary-display-buttonized
2156   "m" gnus-summary-repair-multipart
2157   "v" gnus-article-view-part
2158   "o" gnus-article-save-part
2159   "O" gnus-article-save-part-and-strip
2160   "r" gnus-article-replace-part
2161   "d" gnus-article-delete-part
2162   "t" gnus-article-view-part-as-type
2163   "j" gnus-article-jump-to-part
2164   "c" gnus-article-copy-part
2165   "C" gnus-article-view-part-as-charset
2166   "e" gnus-article-view-part-externally
2167   "H" gnus-article-browse-html-article
2168   "E" gnus-article-encrypt-body
2169   "i" gnus-article-inline-part
2170   "|" gnus-article-pipe-part)
2171
2172 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2173   "p" gnus-summary-mark-as-processable
2174   "u" gnus-summary-unmark-as-processable
2175   "U" gnus-summary-unmark-all-processable
2176   "v" gnus-uu-mark-over
2177   "s" gnus-uu-mark-series
2178   "r" gnus-uu-mark-region
2179   "g" gnus-uu-unmark-region
2180   "R" gnus-uu-mark-by-regexp
2181   "G" gnus-uu-unmark-by-regexp
2182   "t" gnus-uu-mark-thread
2183   "T" gnus-uu-unmark-thread
2184   "a" gnus-uu-mark-all
2185   "b" gnus-uu-mark-buffer
2186   "S" gnus-uu-mark-sparse
2187   "k" gnus-summary-kill-process-mark
2188   "y" gnus-summary-yank-process-mark
2189   "w" gnus-summary-save-process-mark
2190   "i" gnus-uu-invert-processable)
2191
2192 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2193   ;;"x" gnus-uu-extract-any
2194   "m" gnus-summary-save-parts
2195   "u" gnus-uu-decode-uu
2196   "U" gnus-uu-decode-uu-and-save
2197   "s" gnus-uu-decode-unshar
2198   "S" gnus-uu-decode-unshar-and-save
2199   "o" gnus-uu-decode-save
2200   "O" gnus-uu-decode-save
2201   "b" gnus-uu-decode-binhex
2202   "B" gnus-uu-decode-binhex
2203   "Y" gnus-uu-decode-yenc
2204   "p" gnus-uu-decode-postscript
2205   "P" gnus-uu-decode-postscript-and-save)
2206
2207 (gnus-define-keys
2208     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2209   "u" gnus-uu-decode-uu-view
2210   "U" gnus-uu-decode-uu-and-save-view
2211   "s" gnus-uu-decode-unshar-view
2212   "S" gnus-uu-decode-unshar-and-save-view
2213   "o" gnus-uu-decode-save-view
2214   "O" gnus-uu-decode-save-view
2215   "b" gnus-uu-decode-binhex-view
2216   "B" gnus-uu-decode-binhex-view
2217   "p" gnus-uu-decode-postscript-view
2218   "P" gnus-uu-decode-postscript-and-save-view)
2219
2220 (defvar gnus-article-post-menu nil)
2221
2222 (defconst gnus-summary-menu-maxlen 20)
2223
2224 (defun gnus-summary-menu-split (menu)
2225   ;; If we have lots of elements, divide them into groups of 20
2226   ;; and make a pane (or submenu) for each one.
2227   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2228       (let ((menu menu) sublists next
2229             (i 1))
2230         (while menu
2231           ;; Pull off the next gnus-summary-menu-maxlen elements
2232           ;; and make them the next element of sublist.
2233           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2234           (if next
2235               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2236                       nil))
2237           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2238                                              (aref (car (last menu)) 0)) menu)
2239                                sublists))
2240           (setq i (1+ i))
2241           (setq menu next))
2242         (nreverse sublists))
2243     ;; Few elements--put them all in one pane.
2244     menu))
2245
2246 (defun gnus-summary-make-menu-bar ()
2247   (gnus-turn-off-edit-menu 'summary)
2248
2249   (unless (boundp 'gnus-summary-misc-menu)
2250
2251     (easy-menu-define
2252       gnus-summary-kill-menu gnus-summary-mode-map ""
2253       (cons
2254        "Score"
2255        (nconc
2256         (list
2257          ["Customize" gnus-score-customize t])
2258         (gnus-make-score-map 'increase)
2259         (gnus-make-score-map 'lower)
2260         '(("Mark"
2261            ["Kill below" gnus-summary-kill-below t]
2262            ["Mark above" gnus-summary-mark-above t]
2263            ["Tick above" gnus-summary-tick-above t]
2264            ["Clear above" gnus-summary-clear-above t])
2265           ["Current score" gnus-summary-current-score t]
2266           ["Set score" gnus-summary-set-score t]
2267           ["Switch current score file..." gnus-score-change-score-file t]
2268           ["Set mark below..." gnus-score-set-mark-below t]
2269           ["Set expunge below..." gnus-score-set-expunge-below t]
2270           ["Edit current score file" gnus-score-edit-current-scores t]
2271           ["Edit score file" gnus-score-edit-file t]
2272           ["Trace score" gnus-score-find-trace t]
2273           ["Find words" gnus-score-find-favourite-words t]
2274           ["Rescore buffer" gnus-summary-rescore t]
2275           ["Increase score..." gnus-summary-increase-score t]
2276           ["Lower score..." gnus-summary-lower-score t]))))
2277
2278     ;; Define both the Article menu in the summary buffer and the
2279     ;; equivalent Commands menu in the article buffer here for
2280     ;; consistency.
2281     (let ((innards
2282            `(("Hide"
2283               ["All" gnus-article-hide t]
2284               ["Headers" gnus-article-hide-headers t]
2285               ["Signature" gnus-article-hide-signature t]
2286               ["Citation" gnus-article-hide-citation t]
2287               ["List identifiers" gnus-article-hide-list-identifiers t]
2288               ["Banner" gnus-article-strip-banner t]
2289               ["Boring headers" gnus-article-hide-boring-headers t])
2290              ("Highlight"
2291               ["All" gnus-article-highlight t]
2292               ["Headers" gnus-article-highlight-headers t]
2293               ["Signature" gnus-article-highlight-signature t]
2294               ["Citation" gnus-article-highlight-citation t])
2295              ("MIME"
2296               ["Words" gnus-article-decode-mime-words t]
2297               ["Charset" gnus-article-decode-charset t]
2298               ["QP" gnus-article-de-quoted-unreadable t]
2299               ["Base64" gnus-article-de-base64-unreadable t]
2300               ["View MIME buttons" gnus-summary-display-buttonized t]
2301               ["View all" gnus-mime-view-all-parts t]
2302               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2303               ["Encrypt body" gnus-article-encrypt-body
2304                :active (not (gnus-group-read-only-p))
2305                ,@(if (featurep 'xemacs) nil
2306                    '(:help "Encrypt the message body on disk"))]
2307               ["Extract all parts..." gnus-summary-save-parts t]
2308               ("Multipart"
2309                ["Repair multipart" gnus-summary-repair-multipart t]
2310                ["Pipe part..." gnus-article-pipe-part t]
2311                ["Inline part" gnus-article-inline-part t]
2312                ["View part as type..." gnus-article-view-part-as-type t]
2313                ["Encrypt body" gnus-article-encrypt-body
2314                 :active (not (gnus-group-read-only-p))
2315                ,@(if (featurep 'xemacs) nil
2316                    '(:help "Encrypt the message body on disk"))]
2317                ["View part externally" gnus-article-view-part-externally t]
2318                ["View HTML parts in browser" gnus-article-browse-html-article t]
2319                ["View part with charset..." gnus-article-view-part-as-charset t]
2320                ["Copy part" gnus-article-copy-part t]
2321                ["Save part..." gnus-article-save-part t]
2322                ["View part" gnus-article-view-part t]))
2323              ("Date"
2324               ["Local" gnus-article-date-local t]
2325               ["ISO8601" gnus-article-date-iso8601 t]
2326               ["UT" gnus-article-date-ut t]
2327               ["Original" gnus-article-date-original t]
2328               ["Lapsed" gnus-article-date-lapsed t]
2329               ["User-defined" gnus-article-date-user t])
2330              ("Display"
2331               ["Remove images" gnus-article-remove-images t]
2332               ["Toggle smiley" gnus-treat-smiley t]
2333               ["Show X-Face" gnus-article-display-x-face t]
2334               ["Show picons in From" gnus-treat-from-picon t]
2335               ["Show picons in mail headers" gnus-treat-mail-picon t]
2336               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2337               ("View as different encoding"
2338                ,@(gnus-summary-menu-split
2339                   (mapcar
2340                    (lambda (cs)
2341                      ;; Since easymenu under Emacs doesn't allow
2342                      ;; lambda forms for menu commands, we should
2343                      ;; provide intern'ed function symbols.
2344                      (let ((command (intern (format "\
2345 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2346                        (fset command
2347                              `(lambda ()
2348                                 (interactive)
2349                                 (let ((gnus-summary-show-article-charset-alist
2350                                        '((1 . ,cs))))
2351                                   (gnus-summary-show-article 1))))
2352                        `[,(symbol-name cs) ,command t]))
2353                    (sort (if (fboundp 'coding-system-list)
2354                              (coding-system-list)
2355                            (mapcar 'car mm-mime-mule-charset-alist))
2356                          'string<)))))
2357              ("Washing"
2358               ("Remove Blanks"
2359                ["Leading" gnus-article-strip-leading-blank-lines t]
2360                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2361                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2362                ["All of the above" gnus-article-strip-blank-lines t]
2363                ["All" gnus-article-strip-all-blank-lines t]
2364                ["Leading space" gnus-article-strip-leading-space t]
2365                ["Trailing space" gnus-article-strip-trailing-space t]
2366                ["Leading space in headers"
2367                 gnus-article-remove-leading-whitespace t])
2368               ["Overstrike" gnus-article-treat-overstrike t]
2369               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2370               ["Emphasis" gnus-article-emphasize t]
2371               ["Word wrap" gnus-article-fill-cited-article t]
2372               ["Fill long lines" gnus-article-fill-long-lines t]
2373               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2374               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2375               ["Remove CR" gnus-article-remove-cr t]
2376               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2377               ["Base64" gnus-article-de-base64-unreadable t]
2378               ["Rot 13" gnus-summary-caesar-message
2379                ,@(if (featurep 'xemacs) '(t)
2380                    '(:help "\"Caesar rotate\" article by 13"))]
2381               ["De-IDNA" gnus-summary-idna-message t]
2382               ["Morse decode" gnus-summary-morse-message t]
2383               ["Unix pipe..." gnus-summary-pipe-message t]
2384               ["Add buttons" gnus-article-add-buttons t]
2385               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2386               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2387               ["Verbose header" gnus-summary-verbose-headers t]
2388               ["Toggle header" gnus-summary-toggle-header t]
2389               ["Unfold headers" gnus-article-treat-unfold-headers t]
2390               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2391               ["Html" gnus-article-wash-html t]
2392               ["Unsplit URLs" gnus-article-unsplit-urls t]
2393               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2394               ["Decode HZ" gnus-article-decode-HZ t]
2395               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2396               ("(Outlook) Deuglify"
2397                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2398                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2399                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2400                ["Full (Outlook) deuglify"
2401                 gnus-article-outlook-deuglify-article t])
2402               )
2403              ("Output"
2404               ["Save in default format..." gnus-summary-save-article
2405                ,@(if (featurep 'xemacs) '(t)
2406                    '(:help "Save article using default method"))]
2407               ["Save in file..." gnus-summary-save-article-file
2408                ,@(if (featurep 'xemacs) '(t)
2409                    '(:help "Save article in file"))]
2410               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2411               ["Save in MH folder..." gnus-summary-save-article-folder t]
2412               ["Save in VM folder..." gnus-summary-save-article-vm t]
2413               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2414               ["Save body in file..." gnus-summary-save-article-body-file t]
2415               ["Pipe through a filter..." gnus-summary-pipe-output t]
2416               ["Add to SOUP packet" gnus-soup-add-article t]
2417               ["Print with Muttprint..." gnus-summary-muttprint t]
2418               ["Print" gnus-summary-print-article
2419                ,@(if (featurep 'xemacs) '(t)
2420                    '(:help "Generate and print a PostScript image"))])
2421              ("Copy, move,... (Backend)"
2422               ,@(if (featurep 'xemacs) nil
2423                   '(:help "Copying, moving, expiring articles..."))
2424               ["Respool article..." gnus-summary-respool-article t]
2425               ["Move article..." gnus-summary-move-article
2426                (gnus-check-backend-function
2427                 'request-move-article gnus-newsgroup-name)]
2428               ["Copy article..." gnus-summary-copy-article t]
2429               ["Crosspost article..." gnus-summary-crosspost-article
2430                (gnus-check-backend-function
2431                 'request-replace-article gnus-newsgroup-name)]
2432               ["Import file..." gnus-summary-import-article
2433                (gnus-check-backend-function
2434                 'request-accept-article gnus-newsgroup-name)]
2435               ["Create article..." gnus-summary-create-article
2436                (gnus-check-backend-function
2437                 'request-accept-article gnus-newsgroup-name)]
2438               ["Check if posted" gnus-summary-article-posted-p t]
2439               ["Edit article" gnus-summary-edit-article
2440                (not (gnus-group-read-only-p))]
2441               ["Delete article" gnus-summary-delete-article
2442                (gnus-check-backend-function
2443                 'request-expire-articles gnus-newsgroup-name)]
2444               ["Query respool" gnus-summary-respool-query t]
2445               ["Trace respool" gnus-summary-respool-trace t]
2446               ["Delete expirable articles" gnus-summary-expire-articles-now
2447                (gnus-check-backend-function
2448                 'request-expire-articles gnus-newsgroup-name)])
2449              ("Extract"
2450               ["Uudecode" gnus-uu-decode-uu
2451                ,@(if (featurep 'xemacs) '(t)
2452                    '(:help "Decode uuencoded article(s)"))]
2453               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2454               ["Unshar" gnus-uu-decode-unshar t]
2455               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2456               ["Save" gnus-uu-decode-save t]
2457               ["Binhex" gnus-uu-decode-binhex t]
2458               ["Postscript" gnus-uu-decode-postscript t]
2459               ["All MIME parts" gnus-summary-save-parts t])
2460              ("Cache"
2461               ["Enter article" gnus-cache-enter-article t]
2462               ["Remove article" gnus-cache-remove-article t])
2463              ["Translate" gnus-article-babel t]
2464              ["Select article buffer" gnus-summary-select-article-buffer t]
2465              ["Make article buffer sticky" gnus-sticky-article t]
2466              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2467              ["Isearch article..." gnus-summary-isearch-article t]
2468              ["Beginning of the article" gnus-summary-beginning-of-article t]
2469              ["End of the article" gnus-summary-end-of-article t]
2470              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2471              ["Fetch referenced articles" gnus-summary-refer-references t]
2472              ["Fetch current thread" gnus-summary-refer-thread t]
2473              ["Fetch article with id..." gnus-summary-refer-article t]
2474              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2475              ["Redisplay" gnus-summary-show-article t]
2476              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2477       (easy-menu-define
2478         gnus-summary-article-menu gnus-summary-mode-map ""
2479         (cons "Article" innards))
2480
2481       (if (not (keymapp gnus-summary-article-menu))
2482           (easy-menu-define
2483             gnus-article-commands-menu gnus-article-mode-map ""
2484             (cons "Commands" innards))
2485         ;; in Emacs, don't share menu.
2486         (setq gnus-article-commands-menu
2487               (copy-keymap gnus-summary-article-menu))
2488         (define-key gnus-article-mode-map [menu-bar commands]
2489           (cons "Commands" gnus-article-commands-menu))))
2490
2491     (easy-menu-define
2492       gnus-summary-thread-menu gnus-summary-mode-map ""
2493       '("Threads"
2494         ["Find all messages in thread" gnus-summary-refer-thread t]
2495         ["Toggle threading" gnus-summary-toggle-threads t]
2496         ["Hide threads" gnus-summary-hide-all-threads t]
2497         ["Show threads" gnus-summary-show-all-threads t]
2498         ["Hide thread" gnus-summary-hide-thread t]
2499         ["Show thread" gnus-summary-show-thread t]
2500         ["Go to next thread" gnus-summary-next-thread t]
2501         ["Go to previous thread" gnus-summary-prev-thread t]
2502         ["Go down thread" gnus-summary-down-thread t]
2503         ["Go up thread" gnus-summary-up-thread t]
2504         ["Top of thread" gnus-summary-top-thread t]
2505         ["Mark thread as read" gnus-summary-kill-thread t]
2506         ["Mark thread as expired" gnus-summary-expire-thread t]
2507         ["Lower thread score" gnus-summary-lower-thread t]
2508         ["Raise thread score" gnus-summary-raise-thread t]
2509         ["Rethread current" gnus-summary-rethread-current t]))
2510
2511     (easy-menu-define
2512       gnus-summary-post-menu gnus-summary-mode-map ""
2513       `("Post"
2514         ["Send a message (mail or news)" gnus-summary-post-news
2515          ,@(if (featurep 'xemacs) '(t)
2516              '(:help "Compose a new message (mail or news)"))]
2517         ["Followup" gnus-summary-followup
2518          ,@(if (featurep 'xemacs) '(t)
2519              '(:help "Post followup to this article"))]
2520         ["Followup and yank" gnus-summary-followup-with-original
2521          ,@(if (featurep 'xemacs) '(t)
2522              '(:help "Post followup to this article, quoting its contents"))]
2523         ["Supersede article" gnus-summary-supersede-article t]
2524         ["Cancel article" gnus-summary-cancel-article
2525          ,@(if (featurep 'xemacs) '(t)
2526              '(:help "Cancel an article you posted"))]
2527         ["Reply" gnus-summary-reply t]
2528         ["Reply and yank" gnus-summary-reply-with-original t]
2529         ["Wide reply" gnus-summary-wide-reply t]
2530         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2531          ,@(if (featurep 'xemacs) '(t)
2532              '(:help "Mail a reply, quoting this article"))]
2533         ["Very wide reply" gnus-summary-very-wide-reply t]
2534         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2535          ,@(if (featurep 'xemacs) '(t)
2536              '(:help "Mail a very wide reply, quoting this article"))]
2537         ["Mail forward" gnus-summary-mail-forward t]
2538         ["Post forward" gnus-summary-post-forward t]
2539         ["Digest and mail" gnus-uu-digest-mail-forward t]
2540         ["Digest and post" gnus-uu-digest-post-forward t]
2541         ["Resend message" gnus-summary-resend-message t]
2542         ["Resend message edit" gnus-summary-resend-message-edit t]
2543         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2544         ["Send a mail" gnus-summary-mail-other-window t]
2545         ["Create a local message" gnus-summary-news-other-window t]
2546         ["Uuencode and post" gnus-uu-post-news
2547          ,@(if (featurep 'xemacs) '(t)
2548              '(:help "Post a uuencoded article"))]
2549         ["Followup via news" gnus-summary-followup-to-mail t]
2550         ["Followup via news and yank"
2551          gnus-summary-followup-to-mail-with-original t]
2552         ;;("Draft"
2553         ;;["Send" gnus-summary-send-draft t]
2554         ;;["Send bounced" gnus-resend-bounced-mail t])
2555         ))
2556
2557     (cond
2558      ((not (keymapp gnus-summary-post-menu))
2559       (setq gnus-article-post-menu gnus-summary-post-menu))
2560      ((not gnus-article-post-menu)
2561       ;; Don't share post menu.
2562       (setq gnus-article-post-menu
2563             (copy-keymap gnus-summary-post-menu))))
2564     (define-key gnus-article-mode-map [menu-bar post]
2565       (cons "Post" gnus-article-post-menu))
2566
2567     (easy-menu-define
2568       gnus-summary-misc-menu gnus-summary-mode-map ""
2569       `("Gnus"
2570         ("Mark Read"
2571          ["Mark as read" gnus-summary-mark-as-read-forward t]
2572          ["Mark same subject and select"
2573           gnus-summary-kill-same-subject-and-select t]
2574          ["Mark same subject" gnus-summary-kill-same-subject t]
2575          ["Catchup" gnus-summary-catchup
2576           ,@(if (featurep 'xemacs) '(t)
2577               '(:help "Mark unread articles in this group as read"))]
2578          ["Catchup all" gnus-summary-catchup-all t]
2579          ["Catchup to here" gnus-summary-catchup-to-here t]
2580          ["Catchup from here" gnus-summary-catchup-from-here t]
2581          ["Catchup region" gnus-summary-mark-region-as-read
2582           (gnus-mark-active-p)]
2583          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2584         ("Mark Various"
2585          ["Tick" gnus-summary-tick-article-forward t]
2586          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2587          ["Remove marks" gnus-summary-clear-mark-forward t]
2588          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2589          ["Set bookmark" gnus-summary-set-bookmark t]
2590          ["Remove bookmark" gnus-summary-remove-bookmark t])
2591         ("Registry Mark"
2592          ["Important"     gnus-registry-set-article-Important-mark    t]
2593          ["Not Important" gnus-registry-remove-article-Important-mark t]
2594          ["Work"          gnus-registry-set-article-Work-mark         t]
2595          ["Not Work"      gnus-registry-remove-article-Work-mark      t]
2596          ["Later"         gnus-registry-set-article-Later-mark        t]
2597          ["Not Later"     gnus-registry-remove-article-Later-mark     t]
2598          ["Personal"      gnus-registry-set-article-Personal-mark     t]
2599          ["Not Personal"  gnus-registry-remove-article-Personal-mark  t]
2600          ["To Do"         gnus-registry-set-article-To-Do-mark        t]
2601          ["Not To Do"     gnus-registry-remove-article-To-Do-mark     t])
2602         ("Limit to"
2603          ["Marks..." gnus-summary-limit-to-marks t]
2604          ["Subject..." gnus-summary-limit-to-subject t]
2605          ["Author..." gnus-summary-limit-to-author t]
2606          ["Recipient..." gnus-summary-limit-to-recipient t]
2607          ["Address..." gnus-summary-limit-to-address t]
2608          ["Age..." gnus-summary-limit-to-age t]
2609          ["Extra..." gnus-summary-limit-to-extra t]
2610          ["Score..." gnus-summary-limit-to-score t]
2611          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2612          ["Unread" gnus-summary-limit-to-unread t]
2613          ["Unseen" gnus-summary-limit-to-unseen t]
2614          ["Singletons" gnus-summary-limit-to-singletons t]
2615          ["Replied" gnus-summary-limit-to-replied t]
2616          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2617          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2618          ["Pop limit" gnus-summary-pop-limit t]
2619          ["Show dormant" gnus-summary-limit-include-dormant t]
2620          ["Hide childless dormant"
2621           gnus-summary-limit-exclude-childless-dormant t]
2622          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2623          ["Hide marked" gnus-summary-limit-exclude-marks t]
2624          ["Show expunged" gnus-summary-limit-include-expunged t])
2625         ("Process Mark"
2626          ["Set mark" gnus-summary-mark-as-processable t]
2627          ["Remove mark" gnus-summary-unmark-as-processable t]
2628          ["Remove all marks" gnus-summary-unmark-all-processable t]
2629          ["Invert marks" gnus-uu-invert-processable t]
2630          ["Mark above" gnus-uu-mark-over t]
2631          ["Mark series" gnus-uu-mark-series t]
2632          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2633          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2634          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2635          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2636          ["Mark all" gnus-uu-mark-all t]
2637          ["Mark buffer" gnus-uu-mark-buffer t]
2638          ["Mark sparse" gnus-uu-mark-sparse t]
2639          ["Mark thread" gnus-uu-mark-thread t]
2640          ["Unmark thread" gnus-uu-unmark-thread t]
2641          ("Process Mark Sets"
2642           ["Kill" gnus-summary-kill-process-mark t]
2643           ["Yank" gnus-summary-yank-process-mark
2644            gnus-newsgroup-process-stack]
2645           ["Save" gnus-summary-save-process-mark t]
2646           ["Run command on marked..." gnus-summary-universal-argument t]))
2647         ("Scroll article"
2648          ["Page forward" gnus-summary-next-page
2649           ,@(if (featurep 'xemacs) '(t)
2650               '(:help "Show next page of article"))]
2651          ["Page backward" gnus-summary-prev-page
2652           ,@(if (featurep 'xemacs) '(t)
2653               '(:help "Show previous page of article"))]
2654          ["Line forward" gnus-summary-scroll-up t])
2655         ("Move"
2656          ["Next unread article" gnus-summary-next-unread-article t]
2657          ["Previous unread article" gnus-summary-prev-unread-article t]
2658          ["Next article" gnus-summary-next-article t]
2659          ["Previous article" gnus-summary-prev-article t]
2660          ["Next unread subject" gnus-summary-next-unread-subject t]
2661          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2662          ["Next article same subject" gnus-summary-next-same-subject t]
2663          ["Previous article same subject" gnus-summary-prev-same-subject t]
2664          ["First unread article" gnus-summary-first-unread-article t]
2665          ["Best unread article" gnus-summary-best-unread-article t]
2666          ["Go to subject number..." gnus-summary-goto-subject t]
2667          ["Go to article number..." gnus-summary-goto-article t]
2668          ["Go to the last article" gnus-summary-goto-last-article t]
2669          ["Pop article off history" gnus-summary-pop-article t])
2670         ("Sort"
2671          ["Sort by number" gnus-summary-sort-by-number t]
2672          ["Sort by author" gnus-summary-sort-by-author t]
2673          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2674          ["Sort by subject" gnus-summary-sort-by-subject t]
2675          ["Sort by date" gnus-summary-sort-by-date t]
2676          ["Sort by score" gnus-summary-sort-by-score t]
2677          ["Sort by lines" gnus-summary-sort-by-lines t]
2678          ["Sort by characters" gnus-summary-sort-by-chars t]
2679          ["Randomize" gnus-summary-sort-by-random t]
2680          ["Original sort" gnus-summary-sort-by-original t])
2681         ("Help"
2682          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2683          ["Describe group" gnus-summary-describe-group t]
2684          ["Fetch charter" gnus-group-fetch-charter
2685           ,@(if (featurep 'xemacs) nil
2686               '(:help "Display the charter of the current group"))]
2687          ["Fetch control message" gnus-group-fetch-control
2688           ,@(if (featurep 'xemacs) nil
2689               '(:help "Display the archived control message for the current group"))]
2690          ["Read manual" gnus-info-find-node t])
2691         ("Modes"
2692          ["Pick and read" gnus-pick-mode t]
2693          ["Binary" gnus-binary-mode t])
2694         ("Regeneration"
2695          ["Regenerate" gnus-summary-prepare t]
2696          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2697          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2698          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2699          ["Toggle threading" gnus-summary-toggle-threads t])
2700         ["See old articles" gnus-summary-insert-old-articles t]
2701         ["See new articles" gnus-summary-insert-new-articles t]
2702         ["Filter articles..." gnus-summary-execute-command t]
2703         ["Run command on articles..." gnus-summary-universal-argument t]
2704         ["Search articles forward..." gnus-summary-search-article-forward t]
2705         ["Search articles backward..." gnus-summary-search-article-backward t]
2706         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2707         ["Expand window" gnus-summary-expand-window t]
2708         ["Expire expirable articles" gnus-summary-expire-articles
2709          (gnus-check-backend-function
2710           'request-expire-articles gnus-newsgroup-name)]
2711         ["Edit local kill file" gnus-summary-edit-local-kill t]
2712         ["Edit main kill file" gnus-summary-edit-global-kill t]
2713         ["Edit group parameters" gnus-summary-edit-parameters t]
2714         ["Customize group parameters" gnus-summary-customize-parameters t]
2715         ["Send a bug report" gnus-bug t]
2716         ("Exit"
2717          ["Catchup and exit" gnus-summary-catchup-and-exit
2718           ,@(if (featurep 'xemacs) '(t)
2719               '(:help "Mark unread articles in this group as read, then exit"))]
2720          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2721          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2722          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2723          ["Exit group" gnus-summary-exit
2724           ,@(if (featurep 'xemacs) '(t)
2725               '(:help "Exit current group, return to group selection mode"))]
2726          ["Exit group without updating" gnus-summary-exit-no-update t]
2727          ["Exit and goto next group" gnus-summary-next-group t]
2728          ["Exit and goto prev group" gnus-summary-prev-group t]
2729          ["Reselect group" gnus-summary-reselect-current-group t]
2730          ["Rescan group" gnus-summary-rescan-group t]
2731          ["Update dribble" gnus-summary-save-newsrc t])))
2732
2733     (gnus-run-hooks 'gnus-summary-menu-hook)))
2734
2735 (defvar gnus-summary-tool-bar-map nil)
2736
2737 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2738 ;; affect _new_ message buffers.  We might add a function that walks thru all
2739 ;; summary-mode buffers and force the update.
2740 (defun gnus-summary-tool-bar-update (&optional symbol value)
2741   "Update summary mode toolbar.
2742 Setter function for custom variables."
2743   (setq-default gnus-summary-tool-bar-map nil)
2744   (when symbol
2745     ;; When used as ":set" function:
2746     (set-default symbol value))
2747   (when (gnus-buffer-live-p gnus-summary-buffer)
2748     (with-current-buffer gnus-summary-buffer
2749       (gnus-summary-make-tool-bar))))
2750
2751 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2752                                      'gnus-summary-tool-bar-gnome
2753                                    'gnus-summary-tool-bar-retro)
2754   "Specifies the Gnus summary tool bar.
2755
2756 It can be either a list or a symbol refering to a list.  See
2757 `gmm-tool-bar-from-list' for the format of the list.  The
2758 default key map is `gnus-summary-mode-map'.
2759
2760 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2761 `gnus-summary-tool-bar-retro'."
2762   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2763                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2764                  (repeat :tag "User defined list" gmm-tool-bar-item)
2765                  (symbol))
2766   :version "23.1" ;; No Gnus
2767   :initialize 'custom-initialize-default
2768   :set 'gnus-summary-tool-bar-update
2769   :group 'gnus-summary)
2770
2771 (defcustom gnus-summary-tool-bar-gnome
2772   '((gnus-summary-post-news "mail/compose" nil)
2773     (gnus-summary-insert-new-articles "mail/inbox" nil
2774                                       :visible (or (not gnus-agent)
2775                                                    gnus-plugged))
2776     (gnus-summary-reply-with-original "mail/reply")
2777     (gnus-summary-reply "mail/reply" nil :visible nil)
2778     (gnus-summary-followup-with-original "mail/reply-all")
2779     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2780     (gnus-summary-mail-forward "mail/forward")
2781     (gnus-summary-save-article "mail/save")
2782     (gnus-summary-search-article-forward "search" nil :visible nil)
2783     (gnus-summary-print-article "print")
2784     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2785     ;; Some new commands that may need more suitable icons:
2786     (gnus-summary-save-newsrc "save" nil :visible nil)
2787     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2788     (gnus-summary-prev-article "left-arrow")
2789     (gnus-summary-next-article "right-arrow")
2790     (gnus-summary-next-page "next-page")
2791     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2792     ;;
2793     ;; Maybe some sort-by-... could be added:
2794     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2795     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2796     (gnus-summary-mark-as-expirable
2797      "delete" nil
2798      :visible (gnus-check-backend-function 'request-expire-articles
2799                                            gnus-newsgroup-name))
2800     (gnus-summary-mark-as-spam
2801      "mail/spam" t
2802      :visible (and (fboundp 'spam-group-ham-contents-p)
2803                    (spam-group-ham-contents-p gnus-newsgroup-name))
2804      :help "Mark as spam")
2805     (gnus-summary-mark-as-read-forward
2806      "mail/not-spam" nil
2807      :visible (and (fboundp 'spam-group-spam-contents-p)
2808                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2809     ;;
2810     (gnus-summary-exit "exit")
2811     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2812     (gnus-info-find-node "help"))
2813   "List of functions for the summary tool bar (GNOME style).
2814
2815 See `gmm-tool-bar-from-list' for the format of the list."
2816   :type '(repeat gmm-tool-bar-item)
2817   :version "23.1" ;; No Gnus
2818   :initialize 'custom-initialize-default
2819   :set 'gnus-summary-tool-bar-update
2820   :group 'gnus-summary)
2821
2822 (defcustom gnus-summary-tool-bar-retro
2823   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2824     (gnus-summary-next-unread-article "gnus/next-ur")
2825     (gnus-summary-post-news "gnus/post")
2826     (gnus-summary-followup-with-original "gnus/fuwo")
2827     (gnus-summary-followup "gnus/followup")
2828     (gnus-summary-reply-with-original "gnus/reply-wo")
2829     (gnus-summary-reply "gnus/reply")
2830     (gnus-summary-caesar-message "gnus/rot13")
2831     (gnus-uu-decode-uu "gnus/uu-decode")
2832     (gnus-summary-save-article-file "gnus/save-aif")
2833     (gnus-summary-save-article "gnus/save-art")
2834     (gnus-uu-post-news "gnus/uu-post")
2835     (gnus-summary-catchup "gnus/catchup")
2836     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2837     (gnus-summary-exit "gnus/exit-summ")
2838     ;; Some new command that may need more suitable icons:
2839     (gnus-summary-print-article "gnus/print" nil :visible nil)
2840     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2841     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2842     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2843     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2844     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2845     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2846     ;;
2847     (gnus-info-find-node "gnus/help" nil :visible nil))
2848   "List of functions for the summary tool bar (retro look).
2849
2850 See `gmm-tool-bar-from-list' for the format of the list."
2851   :type '(repeat gmm-tool-bar-item)
2852   :version "23.1" ;; No Gnus
2853   :initialize 'custom-initialize-default
2854   :set 'gnus-summary-tool-bar-update
2855   :group 'gnus-summary)
2856
2857 (defcustom gnus-summary-tool-bar-zap-list t
2858   "List of icon items from the global tool bar.
2859 These items are not displayed in the Gnus summary mode tool bar.
2860
2861 See `gmm-tool-bar-from-list' for the format of the list."
2862   :type 'gmm-tool-bar-zap-list
2863   :version "23.1" ;; No Gnus
2864   :initialize 'custom-initialize-default
2865   :set 'gnus-summary-tool-bar-update
2866   :group 'gnus-summary)
2867
2868 (defvar image-load-path)
2869
2870 (defun gnus-summary-make-tool-bar (&optional force)
2871   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2872 When FORCE, rebuild the tool bar."
2873   (when (and (not (featurep 'xemacs))
2874              (boundp 'tool-bar-mode)
2875              tool-bar-mode
2876              (or (not gnus-summary-tool-bar-map) force))
2877     (let* ((load-path
2878             (gmm-image-load-path-for-library "gnus"
2879                                              "mail/save.xpm"
2880                                              nil t))
2881            (image-load-path (cons (car load-path)
2882                                   (when (boundp 'image-load-path)
2883                                     image-load-path)))
2884            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2885                                         gnus-summary-tool-bar-zap-list
2886                                         'gnus-summary-mode-map)))
2887       (when map
2888         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2889         ;; uses it's value.
2890         (setq gnus-summary-tool-bar-map map))))
2891   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2892
2893 (defun gnus-score-set-default (var value)
2894   "A version of set that updates the GNU Emacs menu-bar."
2895   (set var value)
2896   ;; It is the message that forces the active status to be updated.
2897   (message ""))
2898
2899 (defun gnus-make-score-map (type)
2900   "Make a summary score map of type TYPE."
2901   (if t
2902       nil
2903     (let ((headers '(("author" "from" string)
2904                      ("subject" "subject" string)
2905                      ("article body" "body" string)
2906                      ("article head" "head" string)
2907                      ("xref" "xref" string)
2908                      ("extra header" "extra" string)
2909                      ("lines" "lines" number)
2910                      ("followups to author" "followup" string)))
2911           (types '((number ("less than" <)
2912                            ("greater than" >)
2913                            ("equal" =))
2914                    (string ("substring" s)
2915                            ("exact string" e)
2916                            ("fuzzy string" f)
2917                            ("regexp" r))))
2918           (perms '(("temporary" (current-time-string))
2919                    ("permanent" nil)
2920                    ("immediate" now)))
2921           header)
2922       (list
2923        (apply
2924         'nconc
2925         (list
2926          (if (eq type 'lower)
2927              "Lower score"
2928            "Increase score"))
2929         (let (outh)
2930           (while headers
2931             (setq header (car headers))
2932             (setq outh
2933                   (cons
2934                    (apply
2935                     'nconc
2936                     (list (car header))
2937                     (let ((ts (cdr (assoc (nth 2 header) types)))
2938                           outt)
2939                       (while ts
2940                         (setq outt
2941                               (cons
2942                                (apply
2943                                 'nconc
2944                                 (list (caar ts))
2945                                 (let ((ps perms)
2946                                       outp)
2947                                   (while ps
2948                                     (setq outp
2949                                           (cons
2950                                            (vector
2951                                             (caar ps)
2952                                             (list
2953                                              'gnus-summary-score-entry
2954                                              (nth 1 header)
2955                                              (if (or (string= (nth 1 header)
2956                                                               "head")
2957                                                      (string= (nth 1 header)
2958                                                               "body"))
2959                                                  ""
2960                                                (list 'gnus-summary-header
2961                                                      (nth 1 header)))
2962                                              (list 'quote (nth 1 (car ts)))
2963                                              (list 'gnus-score-delta-default
2964                                                    nil)
2965                                              (nth 1 (car ps))
2966                                              t)
2967                                             t)
2968                                            outp))
2969                                     (setq ps (cdr ps)))
2970                                   (list (nreverse outp))))
2971                                outt))
2972                         (setq ts (cdr ts)))
2973                       (list (nreverse outt))))
2974                    outh))
2975             (setq headers (cdr headers)))
2976           (list (nreverse outh))))))))
2977
2978
2979 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
2980
2981 \f
2982
2983 (defun gnus-summary-mode (&optional group)
2984   "Major mode for reading articles.
2985
2986 All normal editing commands are switched off.
2987 \\<gnus-summary-mode-map>
2988 Each line in this buffer represents one article.  To read an
2989 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2990 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2991 respectively.
2992
2993 You can also post articles and send mail from this buffer.  To
2994 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2995 of an article, type `\\[gnus-summary-reply]'.
2996
2997 There are approx. one gazillion commands you can execute in this
2998 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2999
3000 The following commands are available:
3001
3002 \\{gnus-summary-mode-map}"
3003   (interactive)
3004   (kill-all-local-variables)
3005   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3006     (gnus-summary-make-local-variables))
3007   (gnus-summary-make-local-variables)
3008   (setq gnus-newsgroup-name group)
3009   (when (gnus-visual-p 'summary-menu 'menu)
3010     (gnus-summary-make-menu-bar)
3011     (gnus-summary-make-tool-bar))
3012   (gnus-make-thread-indent-array)
3013   (gnus-simplify-mode-line)
3014   (setq major-mode 'gnus-summary-mode)
3015   (setq mode-name "Summary")
3016   (make-local-variable 'minor-mode-alist)
3017   (use-local-map gnus-summary-mode-map)
3018   (buffer-disable-undo)
3019   (setq buffer-read-only t              ;Disable modification
3020         show-trailing-whitespace nil)
3021   (setq truncate-lines t)
3022   (setq selective-display t)
3023   (setq selective-display-ellipses t)   ;Display `...'
3024   (gnus-summary-set-display-table)
3025   (gnus-set-default-directory)
3026   (make-local-variable 'gnus-summary-line-format)
3027   (make-local-variable 'gnus-summary-line-format-spec)
3028   (make-local-variable 'gnus-summary-dummy-line-format)
3029   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3030   (make-local-variable 'gnus-summary-mark-positions)
3031   (gnus-make-local-hook 'pre-command-hook)
3032   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3033   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3034   (turn-on-gnus-mailing-list-mode)
3035   (mm-enable-multibyte)
3036   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3037   (gnus-update-summary-mark-positions))
3038
3039 (defun gnus-summary-make-local-variables ()
3040   "Make all the local summary buffer variables."
3041   (let (global)
3042     (dolist (local gnus-summary-local-variables)
3043       (if (consp local)
3044           (progn
3045             (if (eq (cdr local) 'global)
3046                 ;; Copy the global value of the variable.
3047                 (setq global (symbol-value (car local)))
3048               ;; Use the value from the list.
3049               (setq global (eval (cdr local))))
3050             (set (make-local-variable (car local)) global))
3051         ;; Simple nil-valued local variable.
3052         (set (make-local-variable local) nil)))))
3053
3054 (defun gnus-summary-clear-local-variables ()
3055   (let ((locals gnus-summary-local-variables))
3056     (while locals
3057       (if (consp (car locals))
3058           (and (symbolp (caar locals))
3059                (set (caar locals) nil))
3060         (and (symbolp (car locals))
3061              (set (car locals) nil)))
3062       (setq locals (cdr locals)))))
3063
3064 ;; Summary data functions.
3065
3066 (defmacro gnus-data-number (data)
3067   `(car ,data))
3068
3069 (defmacro gnus-data-set-number (data number)
3070   `(setcar ,data ,number))
3071
3072 (defmacro gnus-data-mark (data)
3073   `(nth 1 ,data))
3074
3075 (defmacro gnus-data-set-mark (data mark)
3076   `(setcar (nthcdr 1 ,data) ,mark))
3077
3078 (defmacro gnus-data-pos (data)
3079   `(nth 2 ,data))
3080
3081 (defmacro gnus-data-set-pos (data pos)
3082   `(setcar (nthcdr 2 ,data) ,pos))
3083
3084 (defmacro gnus-data-header (data)
3085   `(nth 3 ,data))
3086
3087 (defmacro gnus-data-set-header (data header)
3088   `(setf (nth 3 ,data) ,header))
3089
3090 (defmacro gnus-data-level (data)
3091   `(nth 4 ,data))
3092
3093 (defmacro gnus-data-unread-p (data)
3094   `(= (nth 1 ,data) gnus-unread-mark))
3095
3096 (defmacro gnus-data-read-p (data)
3097   `(/= (nth 1 ,data) gnus-unread-mark))
3098
3099 (defmacro gnus-data-pseudo-p (data)
3100   `(consp (nth 3 ,data)))
3101
3102 (defmacro gnus-data-find (number)
3103   `(assq ,number gnus-newsgroup-data))
3104
3105 (defmacro gnus-data-find-list (number &optional data)
3106   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3107      (memq (assq ,number bdata)
3108            bdata)))
3109
3110 (defmacro gnus-data-make (number mark pos header level)
3111   `(list ,number ,mark ,pos ,header ,level))
3112
3113 (defun gnus-data-enter (after-article number mark pos header level offset)
3114   (let ((data (gnus-data-find-list after-article)))
3115     (unless data
3116       (error "No such article: %d" after-article))
3117     (setcdr data (cons (gnus-data-make number mark pos header level)
3118                        (cdr data)))
3119     (setq gnus-newsgroup-data-reverse nil)
3120     (gnus-data-update-list (cddr data) offset)))
3121
3122 (defun gnus-data-enter-list (after-article list &optional offset)
3123   (when list
3124     (let ((data (and after-article (gnus-data-find-list after-article)))
3125           (ilist list))
3126       (if (not (or data
3127                    after-article))
3128           (let ((odata gnus-newsgroup-data))
3129             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3130             (when offset
3131               (gnus-data-update-list odata offset)))
3132         ;; Find the last element in the list to be spliced into the main
3133         ;; list.
3134         (setq list (last list))
3135         (if (not data)
3136             (progn
3137               (setcdr list gnus-newsgroup-data)
3138               (setq gnus-newsgroup-data ilist)
3139               (when offset
3140                 (gnus-data-update-list (cdr list) offset)))
3141           (setcdr list (cdr data))
3142           (setcdr data ilist)
3143           (when offset
3144             (gnus-data-update-list (cdr list) offset))))
3145       (setq gnus-newsgroup-data-reverse nil))))
3146
3147 (defun gnus-data-remove (article &optional offset)
3148   (let ((data gnus-newsgroup-data))
3149     (if (= (gnus-data-number (car data)) article)
3150         (progn
3151           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3152                 gnus-newsgroup-data-reverse nil)
3153           (when offset
3154             (gnus-data-update-list gnus-newsgroup-data offset)))
3155       (while (cdr data)
3156         (when (= (gnus-data-number (cadr data)) article)
3157           (setcdr data (cddr data))
3158           (when offset
3159             (gnus-data-update-list (cdr data) offset))
3160           (setq data nil
3161                 gnus-newsgroup-data-reverse nil))
3162         (setq data (cdr data))))))
3163
3164 (defmacro gnus-data-list (backward)
3165   `(if ,backward
3166        (or gnus-newsgroup-data-reverse
3167            (setq gnus-newsgroup-data-reverse
3168                  (reverse gnus-newsgroup-data)))
3169      gnus-newsgroup-data))
3170
3171 (defun gnus-data-update-list (data offset)
3172   "Add OFFSET to the POS of all data entries in DATA."
3173   (setq gnus-newsgroup-data-reverse nil)
3174   (while data
3175     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3176     (setq data (cdr data))))
3177
3178 (defun gnus-summary-article-pseudo-p (article)
3179   "Say whether this article is a pseudo article or not."
3180   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3181
3182 (defmacro gnus-summary-article-sparse-p (article)
3183   "Say whether this article is a sparse article or not."
3184   `(memq ,article gnus-newsgroup-sparse))
3185
3186 (defmacro gnus-summary-article-ancient-p (article)
3187   "Say whether this article is a sparse article or not."
3188   `(memq ,article gnus-newsgroup-ancient))
3189
3190 (defun gnus-article-parent-p (number)
3191   "Say whether this article is a parent or not."
3192   (let ((data (gnus-data-find-list number)))
3193     (and (cdr data)              ; There has to be an article after...
3194          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3195             (gnus-data-level (nth 1 data))))))
3196
3197 (defun gnus-article-children (number)
3198   "Return a list of all children to NUMBER."
3199   (let* ((data (gnus-data-find-list number))
3200          (level (gnus-data-level (car data)))
3201          children)
3202     (setq data (cdr data))
3203     (while (and data
3204                 (= (gnus-data-level (car data)) (1+ level)))
3205       (push (gnus-data-number (car data)) children)
3206       (setq data (cdr data)))
3207     children))
3208
3209 (defmacro gnus-summary-skip-intangible ()
3210   "If the current article is intangible, then jump to a different article."
3211   '(let ((to (get-text-property (point) 'gnus-intangible)))
3212      (and to (gnus-summary-goto-subject to))))
3213
3214 (defmacro gnus-summary-article-intangible-p ()
3215   "Say whether this article is intangible or not."
3216   '(get-text-property (point) 'gnus-intangible))
3217
3218 (defun gnus-article-read-p (article)
3219   "Say whether ARTICLE is read or not."
3220   (not (or (memq article gnus-newsgroup-marked)
3221            (memq article gnus-newsgroup-spam-marked)
3222            (memq article gnus-newsgroup-unreads)
3223            (memq article gnus-newsgroup-unselected)
3224            (memq article gnus-newsgroup-dormant))))
3225
3226 ;; Some summary mode macros.
3227
3228 (defmacro gnus-summary-article-number ()
3229   "The article number of the article on the current line.
3230 If there isn't an article number here, then we return the current
3231 article number."
3232   '(progn
3233      (gnus-summary-skip-intangible)
3234      (or (get-text-property (point) 'gnus-number)
3235          (gnus-summary-last-subject))))
3236
3237 (defmacro gnus-summary-article-header (&optional number)
3238   "Return the header of article NUMBER."
3239   `(gnus-data-header (gnus-data-find
3240                       ,(or number '(gnus-summary-article-number)))))
3241
3242 (defmacro gnus-summary-thread-level (&optional number)
3243   "Return the level of thread that starts with article NUMBER."
3244   `(if (and (eq gnus-summary-make-false-root 'dummy)
3245             (get-text-property (point) 'gnus-intangible))
3246        0
3247      (gnus-data-level (gnus-data-find
3248                        ,(or number '(gnus-summary-article-number))))))
3249
3250 (defmacro gnus-summary-article-mark (&optional number)
3251   "Return the mark of article NUMBER."
3252   `(gnus-data-mark (gnus-data-find
3253                     ,(or number '(gnus-summary-article-number)))))
3254
3255 (defmacro gnus-summary-article-pos (&optional number)
3256   "Return the position of the line of article NUMBER."
3257   `(gnus-data-pos (gnus-data-find
3258                    ,(or number '(gnus-summary-article-number)))))
3259
3260 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3261 (defmacro gnus-summary-article-subject (&optional number)
3262   "Return current subject string or nil if nothing."
3263   `(let ((headers
3264           ,(if number
3265                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3266              '(gnus-data-header (assq (gnus-summary-article-number)
3267                                       gnus-newsgroup-data)))))
3268      (and headers
3269           (vectorp headers)
3270           (mail-header-subject headers))))
3271
3272 (defmacro gnus-summary-article-score (&optional number)
3273   "Return current article score."
3274   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3275                   gnus-newsgroup-scored))
3276        gnus-summary-default-score 0))
3277
3278 (defun gnus-summary-article-children (&optional number)
3279   "Return a list of article numbers that are children of article NUMBER."
3280   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3281          (level (gnus-data-level (car data)))
3282          l children)
3283     (while (and (setq data (cdr data))
3284                 (> (setq l (gnus-data-level (car data))) level))
3285       (and (= (1+ level) l)
3286            (push (gnus-data-number (car data))
3287                  children)))
3288     (nreverse children)))
3289
3290 (defun gnus-summary-article-parent (&optional number)
3291   "Return the article number of the parent of article NUMBER."
3292   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3293                                     (gnus-data-list t)))
3294          (level (gnus-data-level (car data))))
3295     (if (zerop level)
3296         ()                              ; This is a root.
3297       ;; We search until we find an article with a level less than
3298       ;; this one.  That function has to be the parent.
3299       (while (and (setq data (cdr data))
3300                   (not (< (gnus-data-level (car data)) level))))
3301       (and data (gnus-data-number (car data))))))
3302
3303 (defun gnus-unread-mark-p (mark)
3304   "Say whether MARK is the unread mark."
3305   (= mark gnus-unread-mark))
3306
3307 (defun gnus-read-mark-p (mark)
3308   "Say whether MARK is one of the marks that mark as read.
3309 This is all marks except unread, ticked, dormant, and expirable."
3310   (not (or (= mark gnus-unread-mark)
3311            (= mark gnus-ticked-mark)
3312            (= mark gnus-spam-mark)
3313            (= mark gnus-dormant-mark)
3314            (= mark gnus-expirable-mark))))
3315
3316 (defmacro gnus-article-mark (number)
3317   "Return the MARK of article NUMBER.
3318 This macro should only be used when computing the mark the \"first\"
3319 time; i.e., when generating the summary lines.  After that,
3320 `gnus-summary-article-mark' should be used to examine the
3321 marks of articles."
3322   `(cond
3323     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3324     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3325     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3326     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3327     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3328     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3329     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3330     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3331            gnus-ancient-mark))))
3332
3333 ;; Saving hidden threads.
3334
3335 (defmacro gnus-save-hidden-threads (&rest forms)
3336   "Save hidden threads, eval FORMS, and restore the hidden threads."
3337   (let ((config (make-symbol "config")))
3338     `(let ((,config (gnus-hidden-threads-configuration)))
3339        (unwind-protect
3340            (save-excursion
3341              ,@forms)
3342          (gnus-restore-hidden-threads-configuration ,config)))))
3343 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3344 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3345
3346 (defun gnus-data-compute-positions ()
3347   "Compute the positions of all articles."
3348   (setq gnus-newsgroup-data-reverse nil)
3349   (let ((data gnus-newsgroup-data))
3350     (save-excursion
3351       (gnus-save-hidden-threads
3352         (gnus-summary-show-all-threads)
3353         (goto-char (point-min))
3354         (while data
3355           (while (get-text-property (point) 'gnus-intangible)
3356             (forward-line 1))
3357           (gnus-data-set-pos (car data) (+ (point) 3))
3358           (setq data (cdr data))
3359           (forward-line 1))))))
3360
3361 (defun gnus-hidden-threads-configuration ()
3362   "Return the current hidden threads configuration."
3363   (save-excursion
3364     (let (config)
3365       (goto-char (point-min))
3366       (while (search-forward "\r" nil t)
3367         (push (1- (point)) config))
3368       config)))
3369
3370 (defun gnus-restore-hidden-threads-configuration (config)
3371   "Restore hidden threads configuration from CONFIG."
3372   (save-excursion
3373     (let (point buffer-read-only)
3374       (while (setq point (pop config))
3375         (when (and (< point (point-max))
3376                    (goto-char point)
3377                    (eq (char-after) ?\n))
3378           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3379
3380 ;; Various summary mode internalish functions.
3381
3382 (defun gnus-mouse-pick-article (e)
3383   (interactive "e")
3384   (mouse-set-point e)
3385   (gnus-summary-next-page nil t))
3386
3387 (defun gnus-summary-set-display-table ()
3388   "Change the display table.
3389 Odd characters have a tendency to mess
3390 up nicely formatted displays - we make all possible glyphs
3391 display only a single character."
3392
3393   ;; We start from the standard display table, if any.
3394   (let ((table (or (copy-sequence standard-display-table)
3395                    (make-display-table)))
3396         (i 32))
3397     ;; Nix out all the control chars...
3398     (while (>= (setq i (1- i)) 0)
3399       (aset table i [??]))
3400    ;; ... but not newline and cr, of course.  (cr is necessary for the
3401     ;; selective display).
3402     (aset table ?\n nil)
3403     (aset table ?\r nil)
3404     ;; We keep TAB as well.
3405     (aset table ?\t nil)
3406     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3407     ;; Emacs 23 (unicode), that are not set already.
3408     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3409                  160
3410                256)))
3411       (while (>= (setq i (1- i)) 127)
3412         ;; Only modify if the entry is nil.
3413         (unless (aref table i)
3414           (aset table i [??]))))
3415     (setq buffer-display-table table)))
3416
3417 (defun gnus-summary-set-article-display-arrow (pos)
3418   "Update the overlay arrow to point to line at position POS."
3419   (when (and gnus-summary-display-arrow
3420              (boundp 'overlay-arrow-position)
3421              (boundp 'overlay-arrow-string))
3422     (save-excursion
3423       (goto-char pos)
3424       (beginning-of-line)
3425       (unless overlay-arrow-position
3426         (setq overlay-arrow-position (make-marker)))
3427       (setq overlay-arrow-string "=>"
3428             overlay-arrow-position (set-marker overlay-arrow-position
3429                                                (point)
3430                                                (current-buffer))))))
3431
3432 (defun gnus-summary-setup-buffer (group)
3433   "Initialize summary buffer."
3434   (let ((buffer (gnus-summary-buffer-name group))
3435         (dead-name (concat "*Dead Summary "
3436                            (gnus-group-decoded-name group) "*")))
3437     ;; If a dead summary buffer exists, we kill it.
3438     (when (gnus-buffer-live-p dead-name)
3439       (gnus-kill-buffer dead-name))
3440     (if (get-buffer buffer)
3441         (progn
3442           (set-buffer buffer)
3443           (setq gnus-summary-buffer (current-buffer))
3444           (not gnus-newsgroup-prepared))
3445       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3446       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3447       (gnus-summary-mode group)
3448       (when gnus-carpal
3449         (gnus-carpal-setup-buffer 'summary))
3450       (when (gnus-group-quit-config group)
3451         (set (make-local-variable 'gnus-single-article-buffer) nil))
3452       (make-local-variable 'gnus-article-buffer)
3453       (make-local-variable 'gnus-article-current)
3454       (make-local-variable 'gnus-original-article-buffer)
3455       (setq gnus-newsgroup-name group)
3456       ;; Set any local variables in the group parameters.
3457       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3458       t)))
3459
3460 (defun gnus-set-global-variables ()
3461   "Set the global equivalents of the buffer-local variables.
3462 They are set to the latest values they had.  These reflect the summary
3463 buffer that was in action when the last article was fetched."
3464   (when (eq major-mode 'gnus-summary-mode)
3465     (setq gnus-summary-buffer (current-buffer))
3466     (let ((name gnus-newsgroup-name)
3467           (marked gnus-newsgroup-marked)
3468           (spam gnus-newsgroup-spam-marked)
3469           (unread gnus-newsgroup-unreads)
3470           (headers gnus-current-headers)
3471           (data gnus-newsgroup-data)
3472           (summary gnus-summary-buffer)
3473           (article-buffer gnus-article-buffer)
3474           (original gnus-original-article-buffer)
3475           (gac gnus-article-current)
3476           (reffed gnus-reffed-article-number)
3477           (score-file gnus-current-score-file)
3478           (default-charset gnus-newsgroup-charset)
3479           vlist)
3480       (let ((locals gnus-newsgroup-variables))
3481         (while locals
3482           (if (consp (car locals))
3483               (push (eval (caar locals)) vlist)
3484             (push (eval (car locals)) vlist))
3485           (setq locals (cdr locals)))
3486         (setq vlist (nreverse vlist)))
3487       (with-current-buffer gnus-group-buffer
3488         (setq gnus-newsgroup-name name
3489               gnus-newsgroup-marked marked
3490               gnus-newsgroup-spam-marked spam
3491               gnus-newsgroup-unreads unread
3492               gnus-current-headers headers
3493               gnus-newsgroup-data data
3494               gnus-article-current gac
3495               gnus-summary-buffer summary
3496               gnus-article-buffer article-buffer
3497               gnus-original-article-buffer original
3498               gnus-reffed-article-number reffed
3499               gnus-current-score-file score-file
3500               gnus-newsgroup-charset default-charset)
3501         (let ((locals gnus-newsgroup-variables))
3502           (while locals
3503             (if (consp (car locals))
3504                 (set (caar locals) (pop vlist))
3505               (set (car locals) (pop vlist)))
3506             (setq locals (cdr locals))))
3507         ;; The article buffer also has local variables.
3508         (when (gnus-buffer-live-p gnus-article-buffer)
3509           (set-buffer gnus-article-buffer)
3510           (setq gnus-summary-buffer summary))))))
3511
3512 (defun gnus-summary-article-unread-p (article)
3513   "Say whether ARTICLE is unread or not."
3514   (memq article gnus-newsgroup-unreads))
3515
3516 (defun gnus-summary-first-article-p (&optional article)
3517   "Return whether ARTICLE is the first article in the buffer."
3518   (if (not (setq article (or article (gnus-summary-article-number))))
3519       nil
3520     (eq article (caar gnus-newsgroup-data))))
3521
3522 (defun gnus-summary-last-article-p (&optional article)
3523   "Return whether ARTICLE is the last article in the buffer."
3524   (if (not (setq article (or article (gnus-summary-article-number))))
3525       ;; All non-existent numbers are the last article.  :-)
3526       t
3527     (not (cdr (gnus-data-find-list article)))))
3528
3529 (defun gnus-make-thread-indent-array (&optional n)
3530   (when (or n
3531             (progn (setq n 200) nil)
3532             (null gnus-thread-indent-array)
3533             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3534     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3535           gnus-thread-indent-array-level gnus-thread-indent-level)
3536     (while (>= n 0)
3537       (aset gnus-thread-indent-array n
3538             (make-string (* n gnus-thread-indent-level) ? ))
3539       (setq n (1- n)))))
3540
3541 (defun gnus-update-summary-mark-positions ()
3542   "Compute where the summary marks are to go."
3543   (save-excursion
3544     (when (gnus-buffer-exists-p gnus-summary-buffer)
3545       (set-buffer gnus-summary-buffer))
3546     (let ((spec gnus-summary-line-format-spec)
3547           pos)
3548       (save-excursion
3549         (gnus-set-work-buffer)
3550         (let ((gnus-tmp-unread ?Z)
3551               (gnus-replied-mark ?Z)
3552               (gnus-score-below-mark ?Z)
3553               (gnus-score-over-mark ?Z)
3554               (gnus-undownloaded-mark ?Z)
3555               (gnus-summary-line-format-spec spec)
3556               (gnus-newsgroup-downloadable '(0))
3557               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3558               case-fold-search ignores)
3559           ;; Here, all marks are bound to Z.
3560           (gnus-summary-insert-line header
3561                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3562           (goto-char (point-min))
3563           ;; Memorize the positions of the same characters as dummy marks.
3564           (while (re-search-forward "[A-D]" nil t)
3565             (push (point) ignores))
3566           (erase-buffer)
3567           ;; We use A-D as dummy marks in order to know column positions
3568           ;; where marks should be inserted.
3569           (setq gnus-tmp-unread ?A
3570                 gnus-replied-mark ?B
3571                 gnus-score-below-mark ?C
3572                 gnus-score-over-mark ?C
3573                 gnus-undownloaded-mark ?D)
3574           (gnus-summary-insert-line header
3575                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3576           ;; Ignore characters which aren't dummy marks.
3577           (dolist (p ignores)
3578             (delete-region (goto-char (1- p)) p)
3579             (insert ?Z))
3580           (goto-char (point-min))
3581           (setq pos (list (cons 'unread
3582                                 (and (search-forward "A" nil t)
3583                                      (- (point) (point-min) 1)))))
3584           (goto-char (point-min))
3585           (push (cons 'replied (and (search-forward "B" nil t)
3586                                     (- (point) (point-min) 1)))
3587                 pos)
3588           (goto-char (point-min))
3589           (push (cons 'score (and (search-forward "C" nil t)
3590                                   (- (point) (point-min) 1)))
3591                 pos)
3592           (goto-char (point-min))
3593           (push (cons 'download (and (search-forward "D" nil t)
3594                                      (- (point) (point-min) 1)))
3595                 pos)))
3596       (setq gnus-summary-mark-positions pos))))
3597
3598 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3599   "Insert a dummy root in the summary buffer."
3600   (beginning-of-line)
3601   (gnus-add-text-properties
3602    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3603    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3604
3605 (defun gnus-summary-extract-address-component (from)
3606   (or (car (funcall gnus-extract-address-components from))
3607       from))
3608
3609 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3610   (let ((mail-parse-charset gnus-newsgroup-charset)
3611         (ignored-from-addresses (gnus-ignored-from-addresses))
3612         ; Is it really necessary to do this next part for each summary line?
3613         ; Luckily, doesn't seem to slow things down much.
3614         (mail-parse-ignored-charsets
3615          (with-current-buffer gnus-summary-buffer
3616            gnus-newsgroup-ignored-charsets)))
3617     (or
3618      (and ignored-from-addresses
3619           (string-match ignored-from-addresses gnus-tmp-from)
3620           (let ((extra-headers (mail-header-extra header))
3621                 to
3622                 newsgroups)
3623             (cond
3624              ((setq to (cdr (assq 'To extra-headers)))
3625               (concat gnus-summary-to-prefix
3626                       (inline
3627                         (gnus-summary-extract-address-component
3628                          (funcall gnus-decode-encoded-address-function to)))))
3629              ((setq newsgroups
3630                     (or
3631                      (cdr (assq 'Newsgroups extra-headers))
3632                      (and
3633                       (memq 'Newsgroups gnus-extra-headers)
3634                       (eq (car (gnus-find-method-for-group
3635                                 gnus-newsgroup-name)) 'nntp)
3636                       (gnus-group-real-name gnus-newsgroup-name))))
3637               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3638      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3639
3640 (defun gnus-summary-insert-line (gnus-tmp-header
3641                                  gnus-tmp-level gnus-tmp-current
3642                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3643                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3644                                  &optional gnus-tmp-dummy gnus-tmp-score
3645                                  gnus-tmp-process)
3646   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3647       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3648                                           gnus-tmp-level)))
3649   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3650          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3651          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3652          (gnus-tmp-score-char
3653           (if (or (null gnus-summary-default-score)
3654                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3655                       gnus-summary-zcore-fuzz))
3656               ?                         ;Whitespace
3657             (if (< gnus-tmp-score gnus-summary-default-score)
3658                 gnus-score-below-mark gnus-score-over-mark)))
3659          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3660          (gnus-tmp-replied
3661           (cond (gnus-tmp-process gnus-process-mark)
3662                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3663                  gnus-cached-mark)
3664                 (gnus-tmp-replied gnus-replied-mark)
3665                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3666                  gnus-forwarded-mark)
3667                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3668                  gnus-saved-mark)
3669                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3670                  gnus-recent-mark)
3671                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3672                  gnus-unseen-mark)
3673                 (t gnus-no-mark)))
3674          (gnus-tmp-downloaded
3675           (cond (undownloaded
3676                  gnus-undownloaded-mark)
3677                 (gnus-newsgroup-agentized
3678                  gnus-downloaded-mark)
3679                 (t
3680                  gnus-no-mark)))
3681          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3682          (gnus-tmp-name
3683           (cond
3684            ((string-match "<[^>]+> *$" gnus-tmp-from)
3685             (let ((beg (match-beginning 0)))
3686               (or (and (string-match "^\".+\"" gnus-tmp-from)
3687                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3688                   (substring gnus-tmp-from 0 beg))))
3689            ((string-match "(.+)" gnus-tmp-from)
3690             (substring gnus-tmp-from
3691                        (1+ (match-beginning 0)) (1- (match-end 0))))
3692            (t gnus-tmp-from)))
3693          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3694          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3695          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3696          (buffer-read-only nil))
3697     (when (string= gnus-tmp-name "")
3698       (setq gnus-tmp-name gnus-tmp-from))
3699     (unless (numberp gnus-tmp-lines)
3700       (setq gnus-tmp-lines -1))
3701     (if (= gnus-tmp-lines -1)
3702         (setq gnus-tmp-lines "?")
3703       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3704       (gnus-put-text-property
3705      (point)
3706      (progn (eval gnus-summary-line-format-spec) (point))
3707        'gnus-number gnus-tmp-number)
3708     (when (gnus-visual-p 'summary-highlight 'highlight)
3709       (forward-line -1)
3710       (gnus-run-hooks 'gnus-summary-update-hook)
3711       (forward-line 1))))
3712
3713 (defun gnus-summary-update-line (&optional dont-update)
3714   "Update summary line after change."
3715   (when (and gnus-summary-default-score
3716              (not gnus-summary-inhibit-highlight))
3717     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3718            (article (gnus-summary-article-number))
3719            (score (gnus-summary-article-score article)))
3720       (unless dont-update
3721         (if (and gnus-summary-mark-below
3722                  (< (gnus-summary-article-score)
3723                     gnus-summary-mark-below))
3724             ;; This article has a low score, so we mark it as read.
3725             (when (memq article gnus-newsgroup-unreads)
3726               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3727           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3728             ;; This article was previously marked as read on account
3729             ;; of a low score, but now it has risen, so we mark it as
3730             ;; unread.
3731             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3732         (gnus-summary-update-mark
3733          (if (or (null gnus-summary-default-score)
3734                  (<= (abs (- score gnus-summary-default-score))
3735                      gnus-summary-zcore-fuzz))
3736              ?                          ;Whitespace
3737            (if (< score gnus-summary-default-score)
3738                gnus-score-below-mark gnus-score-over-mark))
3739          'score))
3740       ;; Do visual highlighting.
3741       (when (gnus-visual-p 'summary-highlight 'highlight)
3742         (gnus-run-hooks 'gnus-summary-update-hook)))))
3743
3744 (defvar gnus-tmp-new-adopts nil)
3745
3746 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3747   "Return the number of articles in THREAD.
3748 This may be 0 in some cases -- if none of the articles in
3749 the thread are to be displayed."
3750   (let* ((number
3751          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3752           (cond
3753            ((not (listp thread))
3754             1)
3755            ((and (consp thread) (cdr thread))
3756             (apply
3757              '+ 1 (mapcar
3758                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3759            ((null thread)
3760             1)
3761            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3762             1)
3763            (t 0))))
3764     (when (and level (zerop level) gnus-tmp-new-adopts)
3765       (incf number
3766             (apply '+ (mapcar
3767                        'gnus-summary-number-of-articles-in-thread
3768                        gnus-tmp-new-adopts))))
3769     (if char
3770         (if (> number 1) gnus-not-empty-thread-mark
3771           gnus-empty-thread-mark)
3772       number)))
3773
3774 (defsubst gnus-summary-line-message-size (head)
3775   "Return pretty-printed version of message size.
3776 This function is intended to be used in
3777 `gnus-summary-line-format-alist'."
3778   (let ((c (or (mail-header-chars head) -1)))
3779     (cond ((< c 0) "n/a")               ; chars not available
3780           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3781           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3782           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3783           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3784
3785
3786 (defun gnus-summary-set-local-parameters (group)
3787   "Go through the local params of GROUP and set all variable specs in that list."
3788   (let ((vars '(quit-config)))          ; Ignore quit-config.
3789     (dolist (elem (gnus-group-find-parameter group))
3790       (and (consp elem)                 ; Has to be a cons.
3791            (consp (cdr elem))           ; The cdr has to be a list.
3792            (symbolp (car elem))         ; Has to be a symbol in there.
3793            (not (memq (car elem) vars))
3794            (ignore-errors               ; So we set it.
3795              (push (car elem) vars)
3796              (make-local-variable (car elem))
3797              (set (car elem) (eval (nth 1 elem))))))))
3798
3799 (defun gnus-summary-read-group (group &optional show-all no-article
3800                                       kill-buffer no-display backward
3801                                       select-articles)
3802   "Start reading news in newsgroup GROUP.
3803 If SHOW-ALL is non-nil, already read articles are also listed.
3804 If NO-ARTICLE is non-nil, no article is selected initially.
3805 If NO-DISPLAY, don't generate a summary buffer."
3806   (let (result)
3807     (while (and group
3808                 (null (setq result
3809                             (let ((gnus-auto-select-next nil))
3810                               (or (gnus-summary-read-group-1
3811                                    group show-all no-article
3812                                    kill-buffer no-display
3813                                    select-articles)
3814                                   (setq show-all nil
3815                                         select-articles nil)))))
3816                 (eq gnus-auto-select-next 'quietly))
3817       (set-buffer gnus-group-buffer)
3818       ;; The entry function called above goes to the next
3819       ;; group automatically, so we go two groups back
3820       ;; if we are searching for the previous group.
3821       (when backward
3822         (gnus-group-prev-unread-group 2))
3823       (if (not (equal group (gnus-group-group-name)))
3824           (setq group (gnus-group-group-name))
3825         (setq group nil)))
3826     result))
3827
3828 (defun gnus-summary-read-group-1 (group show-all no-article
3829                                         kill-buffer no-display
3830                                         &optional select-articles)
3831   ;; Killed foreign groups can't be entered.
3832   ;;  (when (and (not (gnus-group-native-p group))
3833   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3834   ;;    (error "Dead non-native groups can't be entered"))
3835   (gnus-message 5 "Retrieving newsgroup: %s..."
3836                 (gnus-group-decoded-name group))
3837   (let* ((new-group (gnus-summary-setup-buffer group))
3838          (quit-config (gnus-group-quit-config group))
3839          (did-select (and new-group (gnus-select-newsgroup
3840                                      group show-all select-articles))))
3841     (cond
3842      ;; This summary buffer exists already, so we just select it.
3843      ((not new-group)
3844       (gnus-set-global-variables)
3845       (when kill-buffer
3846         (gnus-kill-or-deaden-summary kill-buffer))
3847       (gnus-configure-windows 'summary 'force)
3848       (gnus-set-mode-line 'summary)
3849       (gnus-summary-position-point)
3850       (message "")
3851       t)
3852      ;; We couldn't select this group.
3853      ((null did-select)
3854       (when (and (eq major-mode 'gnus-summary-mode)
3855                  (not (equal (current-buffer) kill-buffer)))
3856         (kill-buffer (current-buffer))
3857         (if (not quit-config)
3858             (progn
3859               ;; Update the info -- marks might need to be removed,
3860               ;; for instance.
3861               (gnus-summary-update-info)
3862               (set-buffer gnus-group-buffer)
3863               (gnus-group-jump-to-group group)
3864               (gnus-group-next-unread-group 1))
3865           (gnus-handle-ephemeral-exit quit-config)))
3866       (let ((grpinfo (gnus-get-info group)))
3867         (if (null (gnus-info-read grpinfo))
3868             (gnus-message 3 "Group %s contains no messages"
3869                           (gnus-group-decoded-name group))
3870           (gnus-message 3 "Can't select group")))
3871       nil)
3872      ;; The user did a `C-g' while prompting for number of articles,
3873      ;; so we exit this group.
3874      ((eq did-select 'quit)
3875       (and (eq major-mode 'gnus-summary-mode)
3876            (not (equal (current-buffer) kill-buffer))
3877            (kill-buffer (current-buffer)))
3878       (when kill-buffer
3879         (gnus-kill-or-deaden-summary kill-buffer))
3880       (if (not quit-config)
3881           (progn
3882             (set-buffer gnus-group-buffer)
3883             (gnus-group-jump-to-group group)
3884             (gnus-group-next-unread-group 1)
3885             (gnus-configure-windows 'group 'force))
3886         (gnus-handle-ephemeral-exit quit-config))
3887       ;; Finally signal the quit.
3888       (signal 'quit nil))
3889      ;; The group was successfully selected.
3890      (t
3891       (gnus-set-global-variables)
3892       ;; Save the active value in effect when the group was entered.
3893       (setq gnus-newsgroup-active
3894             (gnus-copy-sequence
3895              (gnus-active gnus-newsgroup-name)))
3896       ;; You can change the summary buffer in some way with this hook.
3897       (gnus-run-hooks 'gnus-select-group-hook)
3898       (when (memq 'summary (gnus-update-format-specifications
3899                             nil 'summary 'summary-mode 'summary-dummy))
3900         ;; The format specification for the summary line was updated,
3901         ;; so we need to update the mark positions as well.
3902         (gnus-update-summary-mark-positions))
3903       ;; Do score processing.
3904       (when gnus-use-scoring
3905         (gnus-possibly-score-headers))
3906       ;; Check whether to fill in the gaps in the threads.
3907       (when gnus-build-sparse-threads
3908         (gnus-build-sparse-threads))
3909       ;; Find the initial limit.
3910       (if show-all
3911           (let ((gnus-newsgroup-dormant nil))
3912             (gnus-summary-initial-limit show-all))
3913         (gnus-summary-initial-limit show-all))
3914       ;; Generate the summary buffer.
3915       (unless no-display
3916         (gnus-summary-prepare))
3917       (when gnus-use-trees
3918         (gnus-tree-open group)
3919         (setq gnus-summary-highlight-line-function
3920               'gnus-tree-highlight-article))
3921       ;; If the summary buffer is empty, but there are some low-scored
3922       ;; articles or some excluded dormants, we include these in the
3923       ;; buffer.
3924       (when (and (zerop (buffer-size))
3925                  (not no-display))
3926         (cond (gnus-newsgroup-dormant
3927                (gnus-summary-limit-include-dormant))
3928               ((and gnus-newsgroup-scored show-all)
3929                (gnus-summary-limit-include-expunged t))))
3930       ;; Function `gnus-apply-kill-file' must be called in this hook.
3931       (gnus-run-hooks 'gnus-apply-kill-hook)
3932       (if (and (zerop (buffer-size))
3933                (not no-display))
3934           (progn
3935             ;; This newsgroup is empty.
3936             (gnus-summary-catchup-and-exit nil t)
3937             (gnus-message 6 "No unread news")
3938             (when kill-buffer
3939               (gnus-kill-or-deaden-summary kill-buffer))
3940             ;; Return nil from this function.
3941             nil)
3942         ;; Hide conversation thread subtrees.  We cannot do this in
3943         ;; gnus-summary-prepare-hook since kill processing may not
3944         ;; work with hidden articles.
3945         (gnus-summary-maybe-hide-threads)
3946         (when kill-buffer
3947           (gnus-kill-or-deaden-summary kill-buffer))
3948         (gnus-summary-auto-select-subject)
3949         ;; Show first unread article if requested.
3950         (if (and (not no-article)
3951                  (not no-display)
3952                  gnus-newsgroup-unreads
3953                  gnus-auto-select-first)
3954             (progn
3955               (gnus-configure-windows 'summary)
3956               (let ((art (gnus-summary-article-number)))
3957                 (unless (and (not gnus-plugged)
3958                              (or (memq art gnus-newsgroup-undownloaded)
3959                                  (memq art gnus-newsgroup-downloadable)))
3960                   (gnus-summary-goto-article art))))
3961           ;; Don't select any articles.
3962           (gnus-summary-position-point)
3963           (gnus-configure-windows 'summary 'force)
3964           (gnus-set-mode-line 'summary))
3965         (when (and gnus-auto-center-group
3966                    (get-buffer-window gnus-group-buffer t))
3967           ;; Gotta use windows, because recenter does weird stuff if
3968           ;; the current buffer ain't the displayed window.
3969           (let ((owin (selected-window)))
3970             (select-window (get-buffer-window gnus-group-buffer t))
3971             (when (gnus-group-goto-group group)
3972               (recenter))
3973             (select-window owin)))
3974         ;; Mark this buffer as "prepared".
3975         (setq gnus-newsgroup-prepared t)
3976         (gnus-run-hooks 'gnus-summary-prepared-hook)
3977         (unless (gnus-ephemeral-group-p group)
3978           (gnus-group-update-group group))
3979         t)))))
3980
3981 (defun gnus-summary-auto-select-subject ()
3982   "Select the subject line on initial group entry."
3983   (goto-char (point-min))
3984   (cond
3985    ((eq gnus-auto-select-subject 'best)
3986     (gnus-summary-best-unread-subject))
3987    ((eq gnus-auto-select-subject 'unread)
3988     (gnus-summary-first-unread-subject))
3989    ((eq gnus-auto-select-subject 'unseen)
3990     (gnus-summary-first-unseen-subject))
3991    ((eq gnus-auto-select-subject 'unseen-or-unread)
3992     (gnus-summary-first-unseen-or-unread-subject))
3993    ((eq gnus-auto-select-subject 'first)
3994     ;; Do nothing.
3995     )
3996    ((functionp gnus-auto-select-subject)
3997     (funcall gnus-auto-select-subject))))
3998
3999 (defun gnus-summary-prepare ()
4000   "Generate the summary buffer."
4001   (interactive)
4002   (let ((buffer-read-only nil))
4003     (erase-buffer)
4004     (setq gnus-newsgroup-data nil
4005           gnus-newsgroup-data-reverse nil)
4006     (gnus-run-hooks 'gnus-summary-generate-hook)
4007     ;; Generate the buffer, either with threads or without.
4008     (when gnus-newsgroup-headers
4009       (gnus-summary-prepare-threads
4010        (if gnus-show-threads
4011            (gnus-sort-gathered-threads
4012             (funcall gnus-summary-thread-gathering-function
4013                      (gnus-sort-threads
4014                       (gnus-cut-threads (gnus-make-threads)))))
4015          ;; Unthreaded display.
4016          (gnus-sort-articles gnus-newsgroup-headers))))
4017     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4018     ;; Call hooks for modifying summary buffer.
4019     (goto-char (point-min))
4020     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4021
4022 (defsubst gnus-general-simplify-subject (subject)
4023   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4024   (setq subject
4025         (cond
4026          ;; Truncate the subject.
4027          (gnus-simplify-subject-functions
4028           (gnus-map-function gnus-simplify-subject-functions subject))
4029          ((numberp gnus-summary-gather-subject-limit)
4030           (setq subject (gnus-simplify-subject-re subject))
4031           (if (> (length subject) gnus-summary-gather-subject-limit)
4032               (substring subject 0 gnus-summary-gather-subject-limit)
4033             subject))
4034          ;; Fuzzily simplify it.
4035          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4036           (gnus-simplify-subject-fuzzy subject))
4037          ;; Just remove the leading "Re:".
4038          (t
4039           (gnus-simplify-subject-re subject))))
4040
4041   (if (and gnus-summary-gather-exclude-subject
4042            (string-match gnus-summary-gather-exclude-subject subject))
4043       nil                         ; This article shouldn't be gathered
4044     subject))
4045
4046 (defun gnus-summary-simplify-subject-query ()
4047   "Query where the respool algorithm would put this article."
4048   (interactive)
4049   (gnus-summary-select-article)
4050   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4051
4052 (defun gnus-gather-threads-by-subject (threads)
4053   "Gather threads by looking at Subject headers."
4054   (if (not gnus-summary-make-false-root)
4055       threads
4056     (let ((hashtb (gnus-make-hashtable 1024))
4057           (prev threads)
4058           (result threads)
4059           subject hthread whole-subject)
4060       (while threads
4061         (setq subject (gnus-general-simplify-subject
4062                        (setq whole-subject (mail-header-subject
4063                                             (caar threads)))))
4064         (when subject
4065           (if (setq hthread (gnus-gethash subject hashtb))
4066               (progn
4067                 ;; We enter a dummy root into the thread, if we
4068                 ;; haven't done that already.
4069                 (unless (stringp (caar hthread))
4070                   (setcar hthread (list whole-subject (car hthread))))
4071                 ;; We add this new gathered thread to this gathered
4072                 ;; thread.
4073                 (setcdr (car hthread)
4074                         (nconc (cdar hthread) (list (car threads))))
4075                 ;; Remove it from the list of threads.
4076                 (setcdr prev (cdr threads))
4077                 (setq threads prev))
4078             ;; Enter this thread into the hash table.
4079             (gnus-sethash subject
4080                           (if gnus-summary-make-false-root-always
4081                               (progn
4082                                 ;; If you want a dummy root above all
4083                                 ;; threads...
4084                                 (setcar threads (list whole-subject
4085                                                       (car threads)))
4086                                 threads)
4087                             threads)
4088                           hashtb)))
4089         (setq prev threads)
4090         (setq threads (cdr threads)))
4091       result)))
4092
4093 (defun gnus-gather-threads-by-references (threads)
4094   "Gather threads by looking at References headers."
4095   (let ((idhashtb (gnus-make-hashtable 1024))
4096         (thhashtb (gnus-make-hashtable 1024))
4097         (prev threads)
4098         (result threads)
4099         ids references id gthread gid entered ref)
4100     (while threads
4101       (when (setq references (mail-header-references (caar threads)))
4102         (setq id (mail-header-id (caar threads))
4103               ids (inline (gnus-split-references references))
4104               entered nil)
4105         (while (setq ref (pop ids))
4106           (setq ids (delete ref ids))
4107           (if (not (setq gid (gnus-gethash ref idhashtb)))
4108               (progn
4109                 (gnus-sethash ref id idhashtb)
4110                 (gnus-sethash id threads thhashtb))
4111             (setq gthread (gnus-gethash gid thhashtb))
4112             (unless entered
4113               ;; We enter a dummy root into the thread, if we
4114               ;; haven't done that already.
4115               (unless (stringp (caar gthread))
4116                 (setcar gthread (list (mail-header-subject (caar gthread))
4117                                       (car gthread))))
4118               ;; We add this new gathered thread to this gathered
4119               ;; thread.
4120               (setcdr (car gthread)
4121                       (nconc (cdar gthread) (list (car threads)))))
4122             ;; Add it into the thread hash table.
4123             (gnus-sethash id gthread thhashtb)
4124             (setq entered t)
4125             ;; Remove it from the list of threads.
4126             (setcdr prev (cdr threads))
4127             (setq threads prev))))
4128       (setq prev threads)
4129       (setq threads (cdr threads)))
4130     result))
4131
4132 (defun gnus-sort-gathered-threads (threads)
4133   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4134   (let ((result threads))
4135     (while threads
4136       (when (stringp (caar threads))
4137         (setcdr (car threads)
4138                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4139       (setq threads (cdr threads)))
4140     result))
4141
4142 (defun gnus-thread-loop-p (root thread)
4143   "Say whether ROOT is in THREAD."
4144   (let ((stack (list thread))
4145         (infloop 0)
4146         th)
4147     (while (setq thread (pop stack))
4148       (setq th (cdr thread))
4149       (while (and th
4150                   (not (eq (caar th) root)))
4151         (pop th))
4152       (if th
4153           ;; We have found a loop.
4154           (let (ref-dep)
4155             (setcdr thread (delq (car th) (cdr thread)))
4156             (if (boundp (setq ref-dep (intern "none"
4157                                               gnus-newsgroup-dependencies)))
4158                 (setcdr (symbol-value ref-dep)
4159                         (nconc (cdr (symbol-value ref-dep))
4160                                (list (car th))))
4161               (set ref-dep (list nil (car th))))
4162             (setq infloop 1
4163                   stack nil))
4164         ;; Push all the subthreads onto the stack.
4165         (push (cdr thread) stack)))
4166     infloop))
4167
4168 (defun gnus-make-threads ()
4169   "Go through the dependency hashtb and find the roots.  Return all threads."
4170   (let (threads)
4171     (while (catch 'infloop
4172              (mapatoms
4173               (lambda (refs)
4174                 ;; Deal with self-referencing References loops.
4175                 (when (and (car (symbol-value refs))
4176                            (not (zerop
4177                                  (apply
4178                                   '+
4179                                   (mapcar
4180                                    (lambda (thread)
4181                                      (gnus-thread-loop-p
4182                                       (car (symbol-value refs)) thread))
4183                                    (cdr (symbol-value refs)))))))
4184                   (setq threads nil)
4185                   (throw 'infloop t))
4186                 (unless (car (symbol-value refs))
4187                   ;; These threads do not refer back to any other
4188                   ;; articles, so they're roots.
4189                   (setq threads (append (cdr (symbol-value refs)) threads))))
4190               gnus-newsgroup-dependencies)))
4191     threads))
4192
4193 ;; Build the thread tree.
4194 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4195   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4196
4197 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4198 if it was already present.
4199
4200 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4201 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4202 Message-IDs will be renamed to a unique Message-ID before being
4203 entered.
4204
4205 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4206   (let* ((id (mail-header-id header))
4207          (id-dep (and id (intern id dependencies)))
4208          parent-id ref ref-dep ref-header replaced)
4209     ;; Enter this `header' in the `dependencies' table.
4210     (cond
4211      ((not id-dep)
4212       (setq header nil))
4213      ;; The first two cases do the normal part: enter a new `header'
4214      ;; in the `dependencies' table.
4215      ((not (boundp id-dep))
4216       (set id-dep (list header)))
4217      ((null (car (symbol-value id-dep)))
4218       (setcar (symbol-value id-dep) header))
4219
4220      ;; From here the `header' was already present in the
4221      ;; `dependencies' table.
4222      (force-new
4223       ;; Overrides an existing entry;
4224       ;; just set the header part of the entry.
4225       (setcar (symbol-value id-dep) header)
4226       (setq replaced t))
4227
4228      ;; Renames the existing `header' to a unique Message-ID.
4229      ((not gnus-summary-ignore-duplicates)
4230       ;; An article with this Message-ID has already been seen.
4231       ;; We rename the Message-ID.
4232       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4233            (list header))
4234       (mail-header-set-id header id))
4235
4236      ;; The last case ignores an existing entry, except it adds any
4237      ;; additional Xrefs (in case the two articles came from different
4238      ;; servers.
4239      ;; Also sets `header' to `nil' meaning that the `dependencies'
4240      ;; table was *not* modified.
4241      (t
4242       (mail-header-set-xref
4243        (car (symbol-value id-dep))
4244        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4245                    "")
4246                (or (mail-header-xref header) "")))
4247       (setq header nil)))
4248
4249     (when (and header (not replaced))
4250       ;; First check that we are not creating a References loop.
4251       (setq parent-id (gnus-parent-id (mail-header-references header)))
4252       (setq ref parent-id)
4253       (while (and ref
4254                   (setq ref-dep (intern-soft ref dependencies))
4255                   (boundp ref-dep)
4256                   (setq ref-header (car (symbol-value ref-dep))))
4257         (if (string= id ref)
4258             ;; Yuk!  This is a reference loop.  Make the article be a
4259             ;; root article.
4260             (progn
4261               (mail-header-set-references (car (symbol-value id-dep)) "none")
4262               (setq ref nil)
4263               (setq parent-id nil))
4264           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4265       (setq ref-dep (intern (or parent-id "none") dependencies))
4266       (if (boundp ref-dep)
4267           (setcdr (symbol-value ref-dep)
4268                   (nconc (cdr (symbol-value ref-dep))
4269                          (list (symbol-value id-dep))))
4270         (set ref-dep (list nil (symbol-value id-dep)))))
4271     header))
4272
4273 (defun gnus-extract-message-id-from-in-reply-to (string)
4274   (if (string-match "<[^>]+>" string)
4275       (substring string (match-beginning 0) (match-end 0))
4276     nil))
4277
4278 (defun gnus-build-sparse-threads ()
4279   (let ((headers gnus-newsgroup-headers)
4280         (mail-parse-charset gnus-newsgroup-charset)
4281         (gnus-summary-ignore-duplicates t)
4282         header references generation relations
4283         subject child end new-child date)
4284     ;; First we create an alist of generations/relations, where
4285     ;; generations is how much we trust the relation, and the relation
4286     ;; is parent/child.
4287     (gnus-message 7 "Making sparse threads...")
4288     (save-excursion
4289       (nnheader-set-temp-buffer " *gnus sparse threads*")
4290       (while (setq header (pop headers))
4291         (when (and (setq references (mail-header-references header))
4292                    (not (string= references "")))
4293           (insert references)
4294           (setq child (mail-header-id header)
4295                 subject (mail-header-subject header)
4296                 date (mail-header-date header)
4297                 generation 0)
4298           (while (search-backward ">" nil t)
4299             (setq end (1+ (point)))
4300             (when (search-backward "<" nil t)
4301               (setq new-child (buffer-substring (point) end))
4302               (push (list (incf generation)
4303                           child (setq child new-child)
4304                           subject date)
4305                     relations)))
4306           (when child
4307             (push (list (1+ generation) child nil subject) relations))
4308           (erase-buffer)))
4309       (kill-buffer (current-buffer)))
4310     ;; Sort over trustworthiness.
4311     (dolist (relation (sort relations 'car-less-than-car))
4312       (when (gnus-dependencies-add-header
4313              (make-full-mail-header
4314               gnus-reffed-article-number
4315               (nth 3 relation) "" (or (nth 4 relation) "")
4316               (nth 1 relation)
4317               (or (nth 2 relation) "") 0 0 "")
4318              gnus-newsgroup-dependencies nil)
4319         (push gnus-reffed-article-number gnus-newsgroup-limit)
4320         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4321         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4322               gnus-newsgroup-reads)
4323         (decf gnus-reffed-article-number)))
4324     (gnus-message 7 "Making sparse threads...done")))
4325
4326 (defun gnus-build-old-threads ()
4327   ;; Look at all the articles that refer back to old articles, and
4328   ;; fetch the headers for the articles that aren't there.  This will
4329   ;; build complete threads - if the roots haven't been expired by the
4330   ;; server, that is.
4331   (let ((mail-parse-charset gnus-newsgroup-charset)
4332         id heads)
4333     (mapatoms
4334      (lambda (refs)
4335        (when (not (car (symbol-value refs)))
4336          (setq heads (cdr (symbol-value refs)))
4337          (while heads
4338            (if (memq (mail-header-number (caar heads))
4339                      gnus-newsgroup-dormant)
4340                (setq heads (cdr heads))
4341              (setq id (symbol-name refs))
4342              (while (and (setq id (gnus-build-get-header id))
4343                          (not (car (gnus-id-to-thread id)))))
4344              (setq heads nil)))))
4345      gnus-newsgroup-dependencies)))
4346
4347 (defsubst gnus-remove-odd-characters (string)
4348   "Translate STRING into something that doesn't contain weird characters."
4349   (mm-subst-char-in-string
4350    ?\r ?\-
4351    (mm-subst-char-in-string ?\n ?\- string t) t))
4352
4353 ;; This function has to be called with point after the article number
4354 ;; on the beginning of the line.
4355 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4356   (let ((eol (point-at-eol))
4357         (buffer (current-buffer))
4358         header references in-reply-to)
4359
4360     ;; overview: [num subject from date id refs chars lines misc]
4361     (unwind-protect
4362         (let (x)
4363           (narrow-to-region (point) eol)
4364           (unless (eobp)
4365             (forward-char))
4366
4367           (setq header
4368                 (make-full-mail-header
4369                  number                 ; number
4370                  (condition-case ()     ; subject
4371                      (gnus-remove-odd-characters
4372                       (funcall gnus-decode-encoded-word-function
4373                                (setq x (nnheader-nov-field))))
4374                    (error x))
4375                  (condition-case ()     ; from
4376                      (gnus-remove-odd-characters
4377                       (funcall gnus-decode-encoded-address-function
4378                                (setq x (nnheader-nov-field))))
4379                    (error x))
4380                  (nnheader-nov-field)   ; date
4381                  (nnheader-nov-read-message-id number)  ; id
4382                  (setq references (nnheader-nov-field)) ; refs
4383                  (nnheader-nov-read-integer) ; chars
4384                  (nnheader-nov-read-integer) ; lines
4385                  (unless (eobp)
4386                    (if (looking-at "Xref: ")
4387                        (goto-char (match-end 0)))
4388                    (nnheader-nov-field)) ; Xref
4389                  (nnheader-nov-parse-extra)))) ; extra
4390
4391       (widen))
4392
4393     (when (and (string= references "")
4394                (setq in-reply-to (mail-header-extra header))
4395                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4396       (mail-header-set-references
4397        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4398
4399     (when gnus-alter-header-function
4400       (funcall gnus-alter-header-function header))
4401     (gnus-dependencies-add-header header dependencies force-new)))
4402
4403 (defun gnus-build-get-header (id)
4404   "Look through the buffer of NOV lines and find the header to ID.
4405 Enter this line into the dependencies hash table, and return
4406 the id of the parent article (if any)."
4407   (let ((deps gnus-newsgroup-dependencies)
4408         found header)
4409     (prog1
4410         (save-excursion
4411           (set-buffer nntp-server-buffer)
4412           (let ((case-fold-search nil))
4413             (goto-char (point-min))
4414             (while (and (not found)
4415                         (search-forward id nil t))
4416               (beginning-of-line)
4417               (setq found (looking-at
4418                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4419                                    (regexp-quote id))))
4420               (or found (beginning-of-line 2)))
4421             (when found
4422               (beginning-of-line)
4423               (and
4424                (setq header (gnus-nov-parse-line
4425                              (read (current-buffer)) deps))
4426                (gnus-parent-id (mail-header-references header))))))
4427       (when header
4428         (let ((number (mail-header-number header)))
4429           (push number gnus-newsgroup-limit)
4430           (push header gnus-newsgroup-headers)
4431           (if (memq number gnus-newsgroup-unselected)
4432               (progn
4433                 (setq gnus-newsgroup-unreads
4434                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4435                                                number))
4436                 (setq gnus-newsgroup-unselected
4437                       (delq number gnus-newsgroup-unselected)))
4438             (push number gnus-newsgroup-ancient)))))))
4439
4440 (defun gnus-build-all-threads ()
4441   "Read all the headers."
4442   (let ((gnus-summary-ignore-duplicates t)
4443         (mail-parse-charset gnus-newsgroup-charset)
4444         (dependencies gnus-newsgroup-dependencies)
4445         header article)
4446     (save-excursion
4447       (set-buffer nntp-server-buffer)
4448       (let ((case-fold-search nil))
4449         (goto-char (point-min))
4450         (while (not (eobp))
4451           (ignore-errors
4452             (setq article (read (current-buffer))
4453                   header (gnus-nov-parse-line article dependencies)))
4454           (when header
4455             (with-current-buffer gnus-summary-buffer
4456               (push header gnus-newsgroup-headers)
4457               (if (memq (setq article (mail-header-number header))
4458                         gnus-newsgroup-unselected)
4459                   (progn
4460                     (setq gnus-newsgroup-unreads
4461                           (gnus-add-to-sorted-list
4462                            gnus-newsgroup-unreads article))
4463                     (setq gnus-newsgroup-unselected
4464                           (delq article gnus-newsgroup-unselected)))
4465                 (push article gnus-newsgroup-ancient)))
4466             (forward-line 1)))))))
4467
4468 (defun gnus-summary-update-article-line (article header)
4469   "Update the line for ARTICLE using HEADER."
4470   (let* ((id (mail-header-id header))
4471          (thread (gnus-id-to-thread id)))
4472     (unless thread
4473       (error "Article in no thread"))
4474     ;; Update the thread.
4475     (setcar thread header)
4476     (gnus-summary-goto-subject article)
4477     (let* ((datal (gnus-data-find-list article))
4478            (data (car datal))
4479            (buffer-read-only nil)
4480            (level (gnus-summary-thread-level)))
4481       (gnus-delete-line)
4482       (let ((inserted (- (point)
4483                          (progn
4484                            (gnus-summary-insert-line
4485                             header level nil
4486                             (memq article gnus-newsgroup-undownloaded)
4487                             (gnus-article-mark article)
4488                             (memq article gnus-newsgroup-replied)
4489                             (memq article gnus-newsgroup-expirable)
4490                             ;; Only insert the Subject string when it's different
4491                             ;; from the previous Subject string.
4492                             (if (and
4493                                  gnus-show-threads
4494                                  (gnus-subject-equal
4495                                   (condition-case ()
4496                                       (mail-header-subject
4497                                        (gnus-data-header
4498                                         (cadr
4499                                          (gnus-data-find-list
4500                                           article
4501                                           (gnus-data-list t)))))
4502                                     ;; Error on the side of excessive subjects.
4503                                     (error ""))
4504                                   (mail-header-subject header)))
4505                                 ""
4506                               (mail-header-subject header))
4507                             nil (cdr (assq article gnus-newsgroup-scored))
4508                             (memq article gnus-newsgroup-processable))
4509                            (point)))))
4510         (when (cdr datal)
4511           (gnus-data-update-list
4512            (cdr datal)
4513            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4514
4515 (defun gnus-summary-update-article (article &optional iheader)
4516   "Update ARTICLE in the summary buffer."
4517   (set-buffer gnus-summary-buffer)
4518   (let* ((header (gnus-summary-article-header article))
4519          (id (mail-header-id header))
4520          (data (gnus-data-find article))
4521          (thread (gnus-id-to-thread id))
4522          (references (mail-header-references header))
4523          (parent
4524           (gnus-id-to-thread
4525            (or (gnus-parent-id
4526                 (when (and references
4527                            (not (equal "" references)))
4528                   references))
4529                "none")))
4530          (buffer-read-only nil)
4531          (old (car thread)))
4532     (when thread
4533       (unless iheader
4534         (setcar thread nil)
4535         (when parent
4536           (delq thread parent)))
4537       (if (gnus-summary-insert-subject id header)
4538           ;; Set the (possibly) new article number in the data structure.
4539           (gnus-data-set-number data (gnus-id-to-article id))
4540         (setcar thread old)
4541         nil))))
4542
4543 (defun gnus-rebuild-thread (id &optional line)
4544   "Rebuild the thread containing ID.
4545 If LINE, insert the rebuilt thread starting on line LINE."
4546   (let ((buffer-read-only nil)
4547         old-pos current thread data)
4548     (if (not gnus-show-threads)
4549         (setq thread (list (car (gnus-id-to-thread id))))
4550       ;; Get the thread this article is part of.
4551       (setq thread (gnus-remove-thread id)))
4552     (setq old-pos (point-at-bol))
4553     (setq current (save-excursion
4554                     (and (re-search-backward "[\r\n]" nil t)
4555                          (gnus-summary-article-number))))
4556     ;; If this is a gathered thread, we have to go some re-gathering.
4557     (when (stringp (car thread))
4558       (let ((subject (car thread))
4559             roots thr)
4560         (setq thread (cdr thread))
4561         (while thread
4562           (unless (memq (setq thr (gnus-id-to-thread
4563                                    (gnus-root-id
4564                                     (mail-header-id (caar thread)))))
4565                         roots)
4566             (push thr roots))
4567           (setq thread (cdr thread)))
4568         ;; We now have all (unique) roots.
4569         (if (= (length roots) 1)
4570             ;; All the loose roots are now one solid root.
4571             (setq thread (car roots))
4572           (setq thread (cons subject (gnus-sort-threads roots))))))
4573     (let (threads)
4574       ;; We then insert this thread into the summary buffer.
4575       (when line
4576         (goto-char (point-min))
4577         (forward-line (1- line)))
4578       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4579         (if gnus-show-threads
4580             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4581           (gnus-summary-prepare-unthreaded thread))
4582         (setq data (nreverse gnus-newsgroup-data))
4583         (setq threads gnus-newsgroup-threads))
4584       ;; We splice the new data into the data structure.
4585       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4586       ;;!!! then we want to insert at the beginning of the buffer.
4587       ;;!!! That happens to be true with Gnus now, but that may
4588       ;;!!! change in the future.  Perhaps.
4589       (gnus-data-enter-list
4590        (if line nil current) data (- (point) old-pos))
4591       (setq gnus-newsgroup-threads
4592             (nconc threads gnus-newsgroup-threads))
4593       (gnus-data-compute-positions))))
4594
4595 (defun gnus-number-to-header (number)
4596   "Return the header for article NUMBER."
4597   (let ((headers gnus-newsgroup-headers))
4598     (while (and headers
4599                 (not (= number (mail-header-number (car headers)))))
4600       (pop headers))
4601     (when headers
4602       (car headers))))
4603
4604 (defun gnus-parent-headers (in-headers &optional generation)
4605   "Return the headers of the GENERATIONeth parent of HEADERS."
4606   (unless generation
4607     (setq generation 1))
4608   (let ((parent t)
4609         (headers in-headers)
4610         references)
4611     (while (and parent
4612                 (not (zerop generation))
4613                 (setq references (mail-header-references headers)))
4614       (setq headers (if (and references
4615                              (setq parent (gnus-parent-id references)))
4616                         (car (gnus-id-to-thread parent))
4617                       nil))
4618       (decf generation))
4619     (and (not (eq headers in-headers))
4620          headers)))
4621
4622 (defun gnus-id-to-thread (id)
4623   "Return the (sub-)thread where ID appears."
4624   (gnus-gethash id gnus-newsgroup-dependencies))
4625
4626 (defun gnus-id-to-article (id)
4627   "Return the article number of ID."
4628   (let ((thread (gnus-id-to-thread id)))
4629     (when (and thread
4630                (car thread))
4631       (mail-header-number (car thread)))))
4632
4633 (defun gnus-id-to-header (id)
4634   "Return the article headers of ID."
4635   (car (gnus-id-to-thread id)))
4636
4637 (defun gnus-article-displayed-root-p (article)
4638   "Say whether ARTICLE is a root(ish) article."
4639   (let ((level (gnus-summary-thread-level article))
4640         (refs (mail-header-references  (gnus-summary-article-header article)))
4641         particle)
4642     (cond
4643      ((null level) nil)
4644      ((zerop level) t)
4645      ((null refs) t)
4646      ((null (gnus-parent-id refs)) t)
4647      ((and (= 1 level)
4648            (null (setq particle (gnus-id-to-article
4649                                  (gnus-parent-id refs))))
4650            (null (gnus-summary-thread-level particle)))))))
4651
4652 (defun gnus-root-id (id)
4653   "Return the id of the root of the thread where ID appears."
4654   (let (last-id prev)
4655     (while (and id (setq prev (car (gnus-id-to-thread id))))
4656       (setq last-id id
4657             id (gnus-parent-id (mail-header-references prev))))
4658     last-id))
4659
4660 (defun gnus-articles-in-thread (thread)
4661   "Return the list of articles in THREAD."
4662   (cons (mail-header-number (car thread))
4663         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4664
4665 (defun gnus-remove-thread (id &optional dont-remove)
4666   "Remove the thread that has ID in it."
4667   (let (headers thread last-id)
4668     ;; First go up in this thread until we find the root.
4669     (setq last-id (gnus-root-id id)
4670           headers (message-flatten-list (gnus-id-to-thread last-id)))
4671     ;; We have now found the real root of this thread.  It might have
4672     ;; been gathered into some loose thread, so we have to search
4673     ;; through the threads to find the thread we wanted.
4674     (let ((threads gnus-newsgroup-threads)
4675           sub)
4676       (while threads
4677         (setq sub (car threads))
4678         (if (stringp (car sub))
4679             ;; This is a gathered thread, so we look at the roots
4680             ;; below it to find whether this article is in this
4681             ;; gathered root.
4682             (progn
4683               (setq sub (cdr sub))
4684               (while sub
4685                 (when (member (caar sub) headers)
4686                   (setq thread (car threads)
4687                         threads nil
4688                         sub nil))
4689                 (setq sub (cdr sub))))
4690           ;; It's an ordinary thread, so we check it.
4691           (when (eq (car sub) (car headers))
4692             (setq thread sub
4693                   threads nil)))
4694         (setq threads (cdr threads)))
4695       ;; If this article is in no thread, then it's a root.
4696       (if thread
4697           (unless dont-remove
4698             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4699         (setq thread (gnus-id-to-thread last-id)))
4700       (when thread
4701         (prog1
4702             thread                      ; We return this thread.
4703           (unless dont-remove
4704             (if (stringp (car thread))
4705                 (progn
4706                   ;; If we use dummy roots, then we have to remove the
4707                   ;; dummy root as well.
4708                   (when (eq gnus-summary-make-false-root 'dummy)
4709                     ;; We go to the dummy root by going to
4710                     ;; the first sub-"thread", and then one line up.
4711                     (gnus-summary-goto-article
4712                      (mail-header-number (caadr thread)))
4713                     (forward-line -1)
4714                     (gnus-delete-line)
4715                     (gnus-data-compute-positions))
4716                   (setq thread (cdr thread))
4717                   (while thread
4718                     (gnus-remove-thread-1 (car thread))
4719                     (setq thread (cdr thread))))
4720               (gnus-remove-thread-1 thread))))))))
4721
4722 (defun gnus-remove-thread-1 (thread)
4723   "Remove the thread THREAD recursively."
4724   (let ((number (mail-header-number (pop thread)))
4725         d)
4726     (setq thread (reverse thread))
4727     (while thread
4728       (gnus-remove-thread-1 (pop thread)))
4729     (when (setq d (gnus-data-find number))
4730       (goto-char (gnus-data-pos d))
4731       (gnus-summary-show-thread)
4732       (gnus-data-remove
4733        number
4734        (- (point-at-bol)
4735           (prog1
4736               (1+ (point-at-eol))
4737             (gnus-delete-line)))))))
4738
4739 (defun gnus-sort-threads-recursive (threads func)
4740   (sort (mapcar (lambda (thread)
4741                   (cons (car thread)
4742                         (and (cdr thread)
4743                              (gnus-sort-threads-recursive (cdr thread) func))))
4744                 threads) func))
4745
4746 (defun gnus-sort-threads-loop (threads func)
4747   (let* ((superthread (cons nil threads))
4748          (stack (list (cons superthread threads)))
4749          remaining-threads thread)
4750     (while stack
4751       (setq remaining-threads (cdr (car stack)))
4752       (if remaining-threads
4753           (progn (setq thread (car remaining-threads))
4754                  (setcdr (car stack) (cdr remaining-threads))
4755                  (if (cdr thread)
4756                      (push (cons thread (cdr thread)) stack)))
4757         (setq thread (caar stack))
4758         (setcdr thread (sort (cdr thread) func))
4759         (pop stack)))
4760     (cdr superthread)))
4761
4762 (defun gnus-sort-threads (threads)
4763   "Sort THREADS."
4764   (if (not gnus-thread-sort-functions)
4765       threads
4766     (gnus-message 8 "Sorting threads...")
4767     (prog1
4768         (condition-case nil
4769             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4770               (gnus-sort-threads-recursive
4771                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4772           ;; Even after binding max-lisp-eval-depth, the recursive
4773           ;; sorter might fail for very long threads.  In that case,
4774           ;; try using a (less well-tested) non-recursive sorter.
4775           (error (gnus-sort-threads-loop
4776                   threads (gnus-make-sort-function
4777                            gnus-thread-sort-functions))))
4778       (gnus-message 8 "Sorting threads...done"))))
4779
4780 (defun gnus-sort-articles (articles)
4781   "Sort ARTICLES."
4782   (when gnus-article-sort-functions
4783     (gnus-message 7 "Sorting articles...")
4784     (prog1
4785         (setq gnus-newsgroup-headers
4786               (sort articles (gnus-make-sort-function
4787                               gnus-article-sort-functions)))
4788       (gnus-message 7 "Sorting articles...done"))))
4789
4790 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4791 (defmacro gnus-thread-header (thread)
4792   "Return header of first article in THREAD.
4793 Note that THREAD must never, ever be anything else than a variable -
4794 using some other form will lead to serious barfage."
4795   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4796   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4797   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4798         (vector thread) 2))
4799
4800 (defsubst gnus-article-sort-by-number (h1 h2)
4801   "Sort articles by article number."
4802   (< (mail-header-number h1)
4803      (mail-header-number h2)))
4804
4805 (defun gnus-thread-sort-by-number (h1 h2)
4806   "Sort threads by root article number."
4807   (gnus-article-sort-by-number
4808    (gnus-thread-header h1) (gnus-thread-header h2)))
4809
4810 (defsubst gnus-article-sort-by-random (h1 h2)
4811   "Sort articles randomly."
4812   (zerop (random 2)))
4813
4814 (defun gnus-thread-sort-by-random (h1 h2)
4815   "Sort threads randomly."
4816   (gnus-article-sort-by-random
4817    (gnus-thread-header h1) (gnus-thread-header h2)))
4818
4819 (defsubst gnus-article-sort-by-lines (h1 h2)
4820   "Sort articles by article Lines header."
4821   (< (mail-header-lines h1)
4822      (mail-header-lines h2)))
4823
4824 (defun gnus-thread-sort-by-lines (h1 h2)
4825   "Sort threads by root article Lines header."
4826   (gnus-article-sort-by-lines
4827    (gnus-thread-header h1) (gnus-thread-header h2)))
4828
4829 (defsubst gnus-article-sort-by-chars (h1 h2)
4830   "Sort articles by octet length."
4831   (< (mail-header-chars h1)
4832      (mail-header-chars h2)))
4833
4834 (defun gnus-thread-sort-by-chars (h1 h2)
4835   "Sort threads by root article octet length."
4836   (gnus-article-sort-by-chars
4837    (gnus-thread-header h1) (gnus-thread-header h2)))
4838
4839 (defsubst gnus-article-sort-by-author (h1 h2)
4840   "Sort articles by root author."
4841   (gnus-string<
4842    (let ((extract (funcall
4843                    gnus-extract-address-components
4844                    (mail-header-from h1))))
4845      (or (car extract) (cadr extract) ""))
4846    (let ((extract (funcall
4847                    gnus-extract-address-components
4848                    (mail-header-from h2))))
4849      (or (car extract) (cadr extract) ""))))
4850
4851 (defun gnus-thread-sort-by-author (h1 h2)
4852   "Sort threads by root author."
4853   (gnus-article-sort-by-author
4854    (gnus-thread-header h1)  (gnus-thread-header h2)))
4855
4856 (defsubst gnus-article-sort-by-recipient (h1 h2)
4857   "Sort articles by recipient."
4858   (gnus-string<
4859    (let ((extract (funcall
4860                    gnus-extract-address-components
4861                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4862      (or (car extract) (cadr extract)))
4863    (let ((extract (funcall
4864                    gnus-extract-address-components
4865                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4866      (or (car extract) (cadr extract)))))
4867
4868 (defun gnus-thread-sort-by-recipient (h1 h2)
4869   "Sort threads by root recipient."
4870   (gnus-article-sort-by-recipient
4871    (gnus-thread-header h1) (gnus-thread-header h2)))
4872
4873 (defsubst gnus-article-sort-by-subject (h1 h2)
4874   "Sort articles by root subject."
4875   (gnus-string<
4876    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4877    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4878
4879 (defun gnus-thread-sort-by-subject (h1 h2)
4880   "Sort threads by root subject."
4881   (gnus-article-sort-by-subject
4882    (gnus-thread-header h1) (gnus-thread-header h2)))
4883
4884 (defsubst gnus-article-sort-by-date (h1 h2)
4885   "Sort articles by root article date."
4886   (time-less-p
4887    (gnus-date-get-time (mail-header-date h1))
4888    (gnus-date-get-time (mail-header-date h2))))
4889
4890 (defun gnus-thread-sort-by-date (h1 h2)
4891   "Sort threads by root article date."
4892   (gnus-article-sort-by-date
4893    (gnus-thread-header h1) (gnus-thread-header h2)))
4894
4895 (defsubst gnus-article-sort-by-score (h1 h2)
4896   "Sort articles by root article score.
4897 Unscored articles will be counted as having a score of zero."
4898   (> (or (cdr (assq (mail-header-number h1)
4899                     gnus-newsgroup-scored))
4900          gnus-summary-default-score 0)
4901      (or (cdr (assq (mail-header-number h2)
4902                     gnus-newsgroup-scored))
4903          gnus-summary-default-score 0)))
4904
4905 (defun gnus-thread-sort-by-score (h1 h2)
4906   "Sort threads by root article score."
4907   (gnus-article-sort-by-score
4908    (gnus-thread-header h1) (gnus-thread-header h2)))
4909
4910 (defun gnus-thread-sort-by-total-score (h1 h2)
4911   "Sort threads by the sum of all scores in the thread.
4912 Unscored articles will be counted as having a score of zero."
4913   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4914
4915 (defun gnus-thread-total-score (thread)
4916   ;; This function find the total score of THREAD.
4917   (cond
4918    ((null thread)
4919     0)
4920    ((consp thread)
4921     (if (stringp (car thread))
4922         (apply gnus-thread-score-function 0
4923                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4924       (gnus-thread-total-score-1 thread)))
4925    (t
4926     (gnus-thread-total-score-1 (list thread)))))
4927
4928 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4929   "Sort threads such that the thread with the most recently arrived article comes first."
4930   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4931
4932 (defun gnus-thread-highest-number (thread)
4933   "Return the highest article number in THREAD."
4934   (apply 'max (mapcar (lambda (header)
4935                         (mail-header-number header))
4936                       (message-flatten-list thread))))
4937
4938 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4939   "Sort threads such that the thread with the most recently dated article comes first."
4940   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4941
4942 (defun gnus-thread-latest-date (thread)
4943   "Return the highest article date in THREAD."
4944   (let ((previous-time 0))
4945     (apply 'max
4946            (mapcar
4947             (lambda (header)
4948               (setq previous-time
4949                     (condition-case ()
4950                         (time-to-seconds (mail-header-parse-date
4951                                           (mail-header-date header)))
4952                       (error previous-time))))
4953             (sort
4954              (message-flatten-list thread)
4955              (lambda (h1 h2)
4956                (< (mail-header-number h1)
4957                   (mail-header-number h2))))))))
4958
4959 (defun gnus-thread-total-score-1 (root)
4960   ;; This function find the total score of the thread below ROOT.
4961   (setq root (car root))
4962   (apply gnus-thread-score-function
4963          (or (append
4964               (mapcar 'gnus-thread-total-score
4965                       (cdr (gnus-id-to-thread (mail-header-id root))))
4966               (when (> (mail-header-number root) 0)
4967                 (list (or (cdr (assq (mail-header-number root)
4968                                      gnus-newsgroup-scored))
4969                           gnus-summary-default-score 0))))
4970              (list gnus-summary-default-score)
4971              '(0))))
4972
4973 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4974 (defvar gnus-tmp-prev-subject nil)
4975 (defvar gnus-tmp-false-parent nil)
4976 (defvar gnus-tmp-root-expunged nil)
4977 (defvar gnus-tmp-dummy-line nil)
4978
4979 (defun gnus-extra-header (type &optional header)
4980   "Return the extra header of TYPE."
4981   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4982       ""))
4983
4984 (defvar gnus-tmp-thread-tree-header-string "")
4985
4986 (defcustom gnus-sum-thread-tree-root "> "
4987   "With %B spec, used for the root of a thread.
4988 If nil, use subject instead."
4989   :version "22.1"
4990   :type '(radio (const :format "%v  " nil) string)
4991   :group 'gnus-thread)
4992
4993 (defcustom gnus-sum-thread-tree-false-root "> "
4994   "With %B spec, used for a false root of a thread.
4995 If nil, use subject instead."
4996   :version "22.1"
4997   :type '(radio (const :format "%v  " nil) string)
4998   :group 'gnus-thread)
4999
5000 (defcustom gnus-sum-thread-tree-single-indent ""
5001   "With %B spec, used for a thread with just one message.
5002 If nil, use subject instead."
5003   :version "22.1"
5004   :type '(radio (const :format "%v  " nil) string)
5005   :group 'gnus-thread)
5006
5007 (defcustom gnus-sum-thread-tree-vertical "| "
5008   "With %B spec, used for drawing a vertical line."
5009   :version "22.1"
5010   :type 'string
5011   :group 'gnus-thread)
5012
5013 (defcustom gnus-sum-thread-tree-indent "  "
5014   "With %B spec, used for indenting."
5015   :version "22.1"
5016   :type 'string
5017   :group 'gnus-thread)
5018
5019 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5020   "With %B spec, used for a leaf with brothers."
5021   :version "22.1"
5022   :type 'string
5023   :group 'gnus-thread)
5024
5025 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5026   "With %B spec, used for a leaf without brothers."
5027   :version "22.1"
5028   :type 'string
5029   :group 'gnus-thread)
5030
5031 (defun gnus-summary-prepare-threads (threads)
5032   "Prepare summary buffer from THREADS and indentation LEVEL.
5033 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5034 or a straight list of headers."
5035   (gnus-message 7 "Generating summary...")
5036
5037   (setq gnus-newsgroup-threads threads)
5038   (beginning-of-line)
5039
5040   (let ((gnus-tmp-level 0)
5041         (default-score (or gnus-summary-default-score 0))
5042         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5043         (building-line-count gnus-summary-display-while-building)
5044         (building-count (integerp gnus-summary-display-while-building))
5045         thread number subject stack state gnus-tmp-gathered beg-match
5046         new-roots gnus-tmp-new-adopts thread-end simp-subject
5047         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5048         gnus-tmp-replied gnus-tmp-subject-or-nil
5049         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5050         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5051         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5052         tree-stack)
5053
5054     (setq gnus-tmp-prev-subject nil
5055           gnus-tmp-thread-tree-header-string "")
5056
5057     (if (vectorp (car threads))
5058         ;; If this is a straight (sic) list of headers, then a
5059         ;; threaded summary display isn't required, so we just create
5060         ;; an unthreaded one.
5061         (gnus-summary-prepare-unthreaded threads)
5062
5063       ;; Do the threaded display.
5064
5065       (if gnus-summary-display-while-building
5066           (switch-to-buffer (buffer-name)))
5067       (while (or threads stack gnus-tmp-new-adopts new-roots)
5068
5069         (if (and (= gnus-tmp-level 0)
5070                  (or (not stack)
5071                      (= (caar stack) 0))
5072                  (not gnus-tmp-false-parent)
5073                  (or gnus-tmp-new-adopts new-roots))
5074             (if gnus-tmp-new-adopts
5075                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5076                       thread (list (car gnus-tmp-new-adopts))
5077                       gnus-tmp-header (caar thread)
5078                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5079               (when new-roots
5080                 (setq thread (list (car new-roots))
5081                       gnus-tmp-header (caar thread)
5082                       new-roots (cdr new-roots))))
5083
5084           (if threads
5085               ;; If there are some threads, we do them before the
5086               ;; threads on the stack.
5087               (setq thread threads
5088                     gnus-tmp-header (caar thread))
5089             ;; There were no current threads, so we pop something off
5090             ;; the stack.
5091             (setq state (car stack)
5092                   gnus-tmp-level (car state)
5093                   tree-stack (cadr state)
5094                   thread (caddr state)
5095                   stack (cdr stack)
5096                   gnus-tmp-header (caar thread))))
5097
5098         (setq gnus-tmp-false-parent nil)
5099         (setq gnus-tmp-root-expunged nil)
5100         (setq thread-end nil)
5101
5102         (if (stringp gnus-tmp-header)
5103             ;; The header is a dummy root.
5104             (cond
5105              ((eq gnus-summary-make-false-root 'adopt)
5106               ;; We let the first article adopt the rest.
5107               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5108                                                (cddar thread)))
5109               (setq gnus-tmp-gathered
5110                     (nconc (mapcar
5111                             (lambda (h) (mail-header-number (car h)))
5112                             (cddar thread))
5113                            gnus-tmp-gathered))
5114               (setq thread (cons (list (caar thread)
5115                                        (cadar thread))
5116                                  (cdr thread)))
5117               (setq gnus-tmp-level -1
5118                     gnus-tmp-false-parent t))
5119              ((eq gnus-summary-make-false-root 'empty)
5120               ;; We print adopted articles with empty subject fields.
5121               (setq gnus-tmp-gathered
5122                     (nconc (mapcar
5123                             (lambda (h) (mail-header-number (car h)))
5124                             (cddar thread))
5125                            gnus-tmp-gathered))
5126               (setq gnus-tmp-level -1))
5127              ((eq gnus-summary-make-false-root 'dummy)
5128               ;; We remember that we probably want to output a dummy
5129               ;; root.
5130               (setq gnus-tmp-dummy-line gnus-tmp-header)
5131               (setq gnus-tmp-prev-subject gnus-tmp-header))
5132              (t
5133               ;; We do not make a root for the gathered
5134               ;; sub-threads at all.
5135               (setq gnus-tmp-level -1)))
5136
5137           (setq number (mail-header-number gnus-tmp-header)
5138                 subject (mail-header-subject gnus-tmp-header)
5139                 simp-subject (gnus-simplify-subject-fully subject))
5140
5141           (cond
5142            ;; If the thread has changed subject, we might want to make
5143            ;; this subthread into a root.
5144            ((and (null gnus-thread-ignore-subject)
5145                  (not (zerop gnus-tmp-level))
5146                  gnus-tmp-prev-subject
5147                  (not (string= gnus-tmp-prev-subject simp-subject)))
5148             (setq new-roots (nconc new-roots (list (car thread)))
5149                   thread-end t
5150                   gnus-tmp-header nil))
5151            ;; If the article lies outside the current limit,
5152            ;; then we do not display it.
5153            ((not (memq number gnus-newsgroup-limit))
5154             (setq gnus-tmp-gathered
5155                   (nconc (mapcar
5156                           (lambda (h) (mail-header-number (car h)))
5157                           (cdar thread))
5158                          gnus-tmp-gathered))
5159             (setq gnus-tmp-new-adopts (if (cdar thread)
5160                                           (append gnus-tmp-new-adopts
5161                                                   (cdar thread))
5162                                         gnus-tmp-new-adopts)
5163                   thread-end t
5164                   gnus-tmp-header nil)
5165             (when (zerop gnus-tmp-level)
5166               (setq gnus-tmp-root-expunged t)))
5167            ;; Perhaps this article is to be marked as read?
5168            ((and gnus-summary-mark-below
5169                  (< (or (cdr (assq number gnus-newsgroup-scored))
5170                         default-score)
5171                     gnus-summary-mark-below)
5172                  ;; Don't touch sparse articles.
5173                  (not (gnus-summary-article-sparse-p number))
5174                  (not (gnus-summary-article-ancient-p number)))
5175             (setq gnus-newsgroup-unreads
5176                   (delq number gnus-newsgroup-unreads))
5177             (if gnus-newsgroup-auto-expire
5178                 (setq gnus-newsgroup-expirable
5179                       (gnus-add-to-sorted-list
5180                        gnus-newsgroup-expirable number))
5181               (push (cons number gnus-low-score-mark)
5182                     gnus-newsgroup-reads))))
5183
5184           (when gnus-tmp-header
5185             ;; We may have an old dummy line to output before this
5186             ;; article.
5187             (when (and gnus-tmp-dummy-line
5188                        (gnus-subject-equal
5189                         gnus-tmp-dummy-line
5190                         (mail-header-subject gnus-tmp-header)))
5191               (gnus-summary-insert-dummy-line
5192                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5193               (setq gnus-tmp-dummy-line nil))
5194
5195             ;; Compute the mark.
5196             (setq gnus-tmp-unread (gnus-article-mark number))
5197
5198             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5199                                   gnus-tmp-header gnus-tmp-level)
5200                   gnus-newsgroup-data)
5201
5202             ;; Actually insert the line.
5203             (setq
5204              gnus-tmp-subject-or-nil
5205              (cond
5206               ((and gnus-thread-ignore-subject
5207                     gnus-tmp-prev-subject
5208                     (not (string= gnus-tmp-prev-subject simp-subject)))
5209                subject)
5210               ((zerop gnus-tmp-level)
5211                (if (and (eq gnus-summary-make-false-root 'empty)
5212                         (memq number gnus-tmp-gathered)
5213                         gnus-tmp-prev-subject
5214                         (string= gnus-tmp-prev-subject simp-subject))
5215                    gnus-summary-same-subject
5216                  subject))
5217               (t gnus-summary-same-subject)))
5218             (if (and (eq gnus-summary-make-false-root 'adopt)
5219                      (= gnus-tmp-level 1)
5220                      (memq number gnus-tmp-gathered))
5221                 (setq gnus-tmp-opening-bracket ?\<
5222                       gnus-tmp-closing-bracket ?\>)
5223               (setq gnus-tmp-opening-bracket ?\[
5224                     gnus-tmp-closing-bracket ?\]))
5225             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5226                 (gnus-make-thread-indent-array
5227                  (max (* 2 (length gnus-thread-indent-array))
5228                       gnus-tmp-level)))
5229             (setq
5230              gnus-tmp-indentation
5231              (aref gnus-thread-indent-array gnus-tmp-level)
5232              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5233              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5234                                 gnus-summary-default-score 0)
5235              gnus-tmp-score-char
5236              (if (or (null gnus-summary-default-score)
5237                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5238                          gnus-summary-zcore-fuzz))
5239                  ?                      ;Whitespace
5240                (if (< gnus-tmp-score gnus-summary-default-score)
5241                    gnus-score-below-mark gnus-score-over-mark))
5242              gnus-tmp-replied
5243              (cond ((memq number gnus-newsgroup-processable)
5244                     gnus-process-mark)
5245                    ((memq number gnus-newsgroup-cached)
5246                     gnus-cached-mark)
5247                    ((memq number gnus-newsgroup-replied)
5248                     gnus-replied-mark)
5249                    ((memq number gnus-newsgroup-forwarded)
5250                     gnus-forwarded-mark)
5251                    ((memq number gnus-newsgroup-saved)
5252                     gnus-saved-mark)
5253                    ((memq number gnus-newsgroup-recent)
5254                     gnus-recent-mark)
5255                    ((memq number gnus-newsgroup-unseen)
5256                     gnus-unseen-mark)
5257                    (t gnus-no-mark))
5258              gnus-tmp-downloaded
5259              (cond ((memq number gnus-newsgroup-undownloaded)
5260                     gnus-undownloaded-mark)
5261                    (gnus-newsgroup-agentized
5262                     gnus-downloaded-mark)
5263                    (t
5264                     gnus-no-mark))
5265              gnus-tmp-from (mail-header-from gnus-tmp-header)
5266              gnus-tmp-name
5267              (cond
5268               ((string-match "<[^>]+> *$" gnus-tmp-from)
5269                (setq beg-match (match-beginning 0))
5270                (or (and (string-match "^\".+\"" gnus-tmp-from)
5271                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5272                    (substring gnus-tmp-from 0 beg-match)))
5273               ((string-match "(.+)" gnus-tmp-from)
5274                (substring gnus-tmp-from
5275                           (1+ (match-beginning 0)) (1- (match-end 0))))
5276               (t gnus-tmp-from))
5277
5278              ;; Do the %B string
5279              gnus-tmp-thread-tree-header-string
5280              (cond
5281               ((not gnus-show-threads) "")
5282               ((zerop gnus-tmp-level)
5283                (cond ((cdar thread)
5284                       (or gnus-sum-thread-tree-root subject))
5285                      (gnus-tmp-new-adopts
5286                       (or gnus-sum-thread-tree-false-root subject))
5287                      (t
5288                       (or gnus-sum-thread-tree-single-indent subject))))
5289               (t
5290                (concat (apply 'concat
5291                               (mapcar (lambda (item)
5292                                         (if (= item 1)
5293                                             gnus-sum-thread-tree-vertical
5294                                           gnus-sum-thread-tree-indent))
5295                                       (cdr (reverse tree-stack))))
5296                        (if (nth 1 thread)
5297                            gnus-sum-thread-tree-leaf-with-other
5298                          gnus-sum-thread-tree-single-leaf)))))
5299             (when (string= gnus-tmp-name "")
5300               (setq gnus-tmp-name gnus-tmp-from))
5301             (unless (numberp gnus-tmp-lines)
5302               (setq gnus-tmp-lines -1))
5303             (if (= gnus-tmp-lines -1)
5304                 (setq gnus-tmp-lines "?")
5305               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5306               (gnus-put-text-property
5307              (point)
5308              (progn (eval gnus-summary-line-format-spec) (point))
5309                'gnus-number number)
5310             (when gnus-visual-p
5311               (forward-line -1)
5312               (gnus-run-hooks 'gnus-summary-update-hook)
5313               (forward-line 1))
5314
5315             (setq gnus-tmp-prev-subject simp-subject)))
5316
5317         (when (nth 1 thread)
5318           (push (list (max 0 gnus-tmp-level)
5319                       (copy-sequence tree-stack)
5320                       (nthcdr 1 thread))
5321                 stack))
5322         (push (if (nth 1 thread) 1 0) tree-stack)
5323         (incf gnus-tmp-level)
5324         (setq threads (if thread-end nil (cdar thread)))
5325         (if gnus-summary-display-while-building
5326             (if building-count
5327                 (progn
5328                   ;; use a set frequency
5329                   (setq building-line-count (1- building-line-count))
5330                   (when (= building-line-count 0)
5331                     (sit-for 0)
5332                     (setq building-line-count
5333                           gnus-summary-display-while-building)))
5334               ;; always
5335               (sit-for 0)))
5336         (unless threads
5337           (setq gnus-tmp-level 0)))))
5338   (gnus-message 7 "Generating summary...done"))
5339
5340 (defun gnus-summary-prepare-unthreaded (headers)
5341   "Generate an unthreaded summary buffer based on HEADERS."
5342   (let (header number mark)
5343
5344     (beginning-of-line)
5345
5346     (while headers
5347       ;; We may have to root out some bad articles...
5348       (when (memq (setq number (mail-header-number
5349                                 (setq header (pop headers))))
5350                   gnus-newsgroup-limit)
5351         ;; Mark article as read when it has a low score.
5352         (when (and gnus-summary-mark-below
5353                    (< (or (cdr (assq number gnus-newsgroup-scored))
5354                           gnus-summary-default-score 0)
5355                       gnus-summary-mark-below)
5356                    (not (gnus-summary-article-ancient-p number)))
5357           (setq gnus-newsgroup-unreads
5358                 (delq number gnus-newsgroup-unreads))
5359           (if gnus-newsgroup-auto-expire
5360               (push number gnus-newsgroup-expirable)
5361             (push (cons number gnus-low-score-mark)
5362                   gnus-newsgroup-reads)))
5363
5364         (setq mark (gnus-article-mark number))
5365         (push (gnus-data-make number mark (1+ (point)) header 0)
5366               gnus-newsgroup-data)
5367         (gnus-summary-insert-line
5368          header 0 number
5369          (memq number gnus-newsgroup-undownloaded)
5370          mark (memq number gnus-newsgroup-replied)
5371          (memq number gnus-newsgroup-expirable)
5372          (mail-header-subject header) nil
5373          (cdr (assq number gnus-newsgroup-scored))
5374          (memq number gnus-newsgroup-processable))))))
5375
5376 (defun gnus-summary-remove-list-identifiers ()
5377   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5378   (let ((regexp (if (consp gnus-list-identifiers)
5379                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5380                   gnus-list-identifiers))
5381         changed subject)
5382     (when regexp
5383       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5384       (dolist (header gnus-newsgroup-headers)
5385         (setq subject (mail-header-subject header)
5386               changed nil)
5387         (while (string-match regexp subject)
5388           (setq subject
5389                 (concat (substring subject 0 (match-beginning 1))
5390                         (substring subject (match-end 0)))
5391                 changed t))
5392         (when changed
5393           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5394             (setq subject
5395                   (concat (substring subject 0 (match-beginning 1))
5396                           (substring subject (match-end 1)))))
5397           (mail-header-set-subject header subject))))))
5398
5399 (defun gnus-fetch-headers (articles)
5400   "Fetch headers of ARTICLES."
5401   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5402     (gnus-message 5 "Fetching headers for %s..." name)
5403     (prog1
5404         (if (eq 'nov
5405                 (setq gnus-headers-retrieved-by
5406                       (gnus-retrieve-headers
5407                        articles gnus-newsgroup-name
5408                        ;; We might want to fetch old headers, but
5409                        ;; not if there is only 1 article.
5410                        (and (or (and
5411                                  (not (eq gnus-fetch-old-headers 'some))
5412                                  (not (numberp gnus-fetch-old-headers)))
5413                                 (> (length articles) 1))
5414                             gnus-fetch-old-headers))))
5415             (gnus-get-newsgroup-headers-xover
5416              articles nil nil gnus-newsgroup-name t)
5417           (gnus-get-newsgroup-headers))
5418       (gnus-message 5 "Fetching headers for %s...done" name))))
5419
5420 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5421   "Select newsgroup GROUP.
5422 If READ-ALL is non-nil, all articles in the group are selected.
5423 If SELECT-ARTICLES, only select those articles from GROUP."
5424   (let* ((entry (gnus-group-entry group))
5425          ;;!!! Dirty hack; should be removed.
5426          (gnus-summary-ignore-duplicates
5427           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5428               t
5429             gnus-summary-ignore-duplicates))
5430          (info (nth 2 entry))
5431          charset articles fetched-articles cached)
5432
5433     (unless (gnus-check-server
5434              (set (make-local-variable 'gnus-current-select-method)
5435                   (gnus-find-method-for-group group)))
5436       (error "Couldn't open server"))
5437     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5438
5439     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5440         (gnus-activate-group group)     ; Or we can activate it...
5441         (progn                          ; Or we bug out.
5442           (when (equal major-mode 'gnus-summary-mode)
5443             (gnus-kill-buffer (current-buffer)))
5444           (error
5445            "Couldn't activate group %s: %s"
5446            (mm-decode-coding-string group charset)
5447            (mm-decode-coding-string (gnus-status-message group) charset))))
5448
5449     (unless (gnus-request-group group t)
5450         (when (equal major-mode 'gnus-summary-mode)
5451           (gnus-kill-buffer (current-buffer)))
5452         (error "Couldn't request group %s: %s"
5453                (mm-decode-coding-string group charset)
5454                (mm-decode-coding-string (gnus-status-message group) charset)))
5455
5456     (when gnus-agent
5457       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5458
5459       (setq gnus-summary-use-undownloaded-faces
5460             (gnus-agent-find-parameter
5461              group
5462              'agent-enable-undownloaded-faces)))
5463
5464     (setq gnus-newsgroup-name group
5465           gnus-newsgroup-unselected nil
5466           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5467
5468     (let ((display (gnus-group-find-parameter group 'display)))
5469       (setq gnus-newsgroup-display
5470             (cond
5471              ((not (zerop (or (car-safe read-all) 0)))
5472               ;; The user entered the group with C-u SPC/RET, let's show
5473               ;; all articles.
5474               'gnus-not-ignore)
5475              ((eq display 'all)
5476               'gnus-not-ignore)
5477              ((arrayp display)
5478               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5479              ((numberp display)
5480               ;; The following is probably the "correct" solution, but
5481               ;; it makes Gnus fetch all headers and then limit the
5482               ;; articles (which is slow), so instead we hack the
5483               ;; select-articles parameter instead. -- Simon Josefsson
5484               ;; <jas@kth.se>
5485               ;;
5486               ;; (gnus-byte-compile
5487               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5488               ;;                         display)))))
5489               (setq select-articles
5490                     (gnus-uncompress-range
5491                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5492                              (if (> tmp 0)
5493                                  tmp
5494                                1))
5495                            (cdr (gnus-active group)))))
5496               nil)
5497              (t
5498               nil))))
5499
5500     (gnus-summary-setup-default-charset)
5501
5502     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5503     (when (gnus-virtual-group-p group)
5504       (setq cached gnus-newsgroup-cached))
5505
5506     (setq gnus-newsgroup-unreads
5507           (gnus-sorted-ndifference
5508            (gnus-sorted-ndifference gnus-newsgroup-unreads
5509                                     gnus-newsgroup-marked)
5510            gnus-newsgroup-dormant))
5511
5512     (setq gnus-newsgroup-processable nil)
5513
5514     (gnus-update-read-articles group gnus-newsgroup-unreads)
5515
5516     ;; Adjust and set lists of article marks.
5517     (when info
5518       (gnus-adjust-marked-articles info))
5519     (if (setq articles select-articles)
5520         (setq gnus-newsgroup-unselected
5521               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5522       (setq articles (gnus-articles-to-read group read-all)))
5523
5524     (cond
5525      ((null articles)
5526       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5527       'quit)
5528      ((eq articles 0) nil)
5529      (t
5530       ;; Init the dependencies hash table.
5531       (setq gnus-newsgroup-dependencies
5532             (gnus-make-hashtable (length articles)))
5533       (gnus-set-global-variables)
5534       ;; Retrieve the headers and read them in.
5535
5536       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5537
5538       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5539       (when cached
5540         (setq gnus-newsgroup-cached cached))
5541
5542       ;; Suppress duplicates?
5543       (when gnus-suppress-duplicates
5544         (gnus-dup-suppress-articles))
5545
5546       ;; Set the initial limit.
5547       (setq gnus-newsgroup-limit (copy-sequence articles))
5548       ;; Remove canceled articles from the list of unread articles.
5549       (setq fetched-articles
5550             (mapcar (lambda (headers) (mail-header-number headers))
5551                     gnus-newsgroup-headers))
5552       (setq gnus-newsgroup-articles fetched-articles)
5553       (setq gnus-newsgroup-unreads
5554             (gnus-sorted-nintersection
5555              gnus-newsgroup-unreads fetched-articles))
5556       (gnus-compute-unseen-list)
5557
5558       ;; Removed marked articles that do not exist.
5559       (gnus-update-missing-marks
5560        (gnus-sorted-difference articles fetched-articles))
5561       ;; We might want to build some more threads first.
5562       (when (and gnus-fetch-old-headers
5563                  (eq gnus-headers-retrieved-by 'nov))
5564         (if (eq gnus-fetch-old-headers 'invisible)
5565             (gnus-build-all-threads)
5566           (gnus-build-old-threads)))
5567       ;; Let the Gnus agent mark articles as read.
5568       (when gnus-agent
5569         (gnus-agent-get-undownloaded-list))
5570       ;; Remove list identifiers from subject
5571       (when gnus-list-identifiers
5572         (gnus-summary-remove-list-identifiers))
5573       ;; Check whether auto-expire is to be done in this group.
5574       (setq gnus-newsgroup-auto-expire
5575             (gnus-group-auto-expirable-p group))
5576       ;; Set up the article buffer now, if necessary.
5577       (unless (and gnus-single-article-buffer
5578                    (equal gnus-article-buffer "*Article*"))
5579         (gnus-article-setup-buffer))
5580       ;; First and last article in this newsgroup.
5581       (when gnus-newsgroup-headers
5582         (setq gnus-newsgroup-begin
5583               (mail-header-number (car gnus-newsgroup-headers))
5584               gnus-newsgroup-end
5585               (mail-header-number
5586                (gnus-last-element gnus-newsgroup-headers))))
5587       ;; GROUP is successfully selected.
5588       (or gnus-newsgroup-headers t)))))
5589
5590 (defun gnus-compute-unseen-list ()
5591   ;; The `seen' marks are treated specially.
5592   (if (not gnus-newsgroup-seen)
5593       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5594     (setq gnus-newsgroup-unseen
5595           (gnus-inverse-list-range-intersection
5596            gnus-newsgroup-articles gnus-newsgroup-seen))))
5597
5598 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5599
5600 (defun gnus-summary-display-make-predicate (display)
5601   (require 'gnus-agent)
5602   (when (= (length display) 1)
5603     (setq display (car display)))
5604   (unless gnus-summary-display-cache
5605     (dolist (elem (append '((unread . unread)
5606                             (read . read)
5607                             (unseen . unseen))
5608                           gnus-article-mark-lists))
5609       (push (cons (cdr elem)
5610                   (gnus-byte-compile
5611                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5612             gnus-summary-display-cache)))
5613   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5614         (gnus-category-predicate-cache gnus-summary-display-cache))
5615     (gnus-get-predicate display)))
5616
5617 ;; Uses the dynamically bound `number' variable.
5618 (defvar number)
5619 (defun gnus-article-marked-p (type &optional article)
5620   (let ((article (or article number)))
5621     (cond
5622      ((eq type 'tick)
5623       (memq article gnus-newsgroup-marked))
5624      ((eq type 'spam)
5625       (memq article gnus-newsgroup-spam-marked))
5626      ((eq type 'unsend)
5627       (memq article gnus-newsgroup-unsendable))
5628      ((eq type 'undownload)
5629       (memq article gnus-newsgroup-undownloaded))
5630      ((eq type 'download)
5631       (memq article gnus-newsgroup-downloadable))
5632      ((eq type 'unread)
5633       (memq article gnus-newsgroup-unreads))
5634      ((eq type 'read)
5635       (memq article gnus-newsgroup-reads))
5636      ((eq type 'dormant)
5637       (memq article gnus-newsgroup-dormant) )
5638      ((eq type 'expire)
5639       (memq article gnus-newsgroup-expirable))
5640      ((eq type 'reply)
5641       (memq article gnus-newsgroup-replied))
5642      ((eq type 'killed)
5643       (memq article gnus-newsgroup-killed))
5644      ((eq type 'bookmark)
5645       (assq article gnus-newsgroup-bookmarks))
5646      ((eq type 'score)
5647       (assq article gnus-newsgroup-scored))
5648      ((eq type 'save)
5649       (memq article gnus-newsgroup-saved))
5650      ((eq type 'cache)
5651       (memq article gnus-newsgroup-cached))
5652      ((eq type 'forward)
5653       (memq article gnus-newsgroup-forwarded))
5654      ((eq type 'seen)
5655       (not (memq article gnus-newsgroup-unseen)))
5656      ((eq type 'recent)
5657       (memq article gnus-newsgroup-recent))
5658      (t t))))
5659
5660 (defun gnus-articles-to-read (group &optional read-all)
5661   "Find out what articles the user wants to read."
5662   (let* ((articles
5663           ;; Select all articles if `read-all' is non-nil, or if there
5664           ;; are no unread articles.
5665           (if (or read-all
5666                   (and (zerop (length gnus-newsgroup-marked))
5667                        (zerop (length gnus-newsgroup-unreads)))
5668                   ;; Fetch all if the predicate is non-nil.
5669                   gnus-newsgroup-display)
5670               ;; We want to select the headers for all the articles in
5671               ;; the group, so we select either all the active
5672               ;; articles in the group, or (if that's nil), the
5673               ;; articles in the cache.
5674               (or
5675                (if gnus-newsgroup-maximum-articles
5676                    (let ((active (gnus-active group)))
5677                      (gnus-uncompress-range
5678                       (cons (max (car active)
5679                                  (- (cdr active)
5680                                     gnus-newsgroup-maximum-articles
5681                                     -1))
5682                             (cdr active))))
5683                  (gnus-uncompress-range (gnus-active group)))
5684                (gnus-cache-articles-in-group group))
5685             ;; Select only the "normal" subset of articles.
5686             (gnus-sorted-nunion
5687              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5688              gnus-newsgroup-unreads)))
5689          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5690          (scored (length scored-list))
5691          (number (length articles))
5692          (marked (+ (length gnus-newsgroup-marked)
5693                     (length gnus-newsgroup-dormant)))
5694          (select
5695           (cond
5696            ((numberp read-all)
5697             read-all)
5698            ((numberp gnus-newsgroup-display)
5699             gnus-newsgroup-display)
5700            (t
5701             (condition-case ()
5702                 (cond
5703                  ((and (or (<= scored marked) (= scored number))
5704                        (numberp gnus-large-newsgroup)
5705                        (> number gnus-large-newsgroup))
5706                   (let* ((cursor-in-echo-area nil)
5707                          (initial (gnus-parameter-large-newsgroup-initial
5708                                    gnus-newsgroup-name))
5709                          (input
5710                           (read-string
5711                            (format
5712                             "How many articles from %s (%s %d): "
5713                             (gnus-group-decoded-name gnus-newsgroup-name)
5714                             (if initial "max" "default")
5715                             number)
5716                            (if initial
5717                                (cons (number-to-string initial)
5718                                      0)))))
5719                     (if (string-match "^[ \t]*$" input) number input)))
5720                  ((and (> scored marked) (< scored number)
5721                        (> (- scored number) 20))
5722                   (let ((input
5723                          (read-string
5724                           (format "%s %s (%d scored, %d total): "
5725                                   "How many articles from"
5726                                   (gnus-group-decoded-name group)
5727                                   scored number))))
5728                     (if (string-match "^[ \t]*$" input)
5729                         number input)))
5730                  (t number))
5731               (quit
5732                (message "Quit getting the articles to read")
5733                nil))))))
5734     (setq select (if (stringp select) (string-to-number select) select))
5735     (if (or (null select) (zerop select))
5736         select
5737       (if (and (not (zerop scored)) (<= (abs select) scored))
5738           (progn
5739             (setq articles (sort scored-list '<))
5740             (setq number (length articles)))
5741         (setq articles (copy-sequence articles)))
5742
5743       (when (< (abs select) number)
5744         (if (< select 0)
5745             ;; Select the N oldest articles.
5746             (setcdr (nthcdr (1- (abs select)) articles) nil)
5747           ;; Select the N most recent articles.
5748           (setq articles (nthcdr (- number select) articles))))
5749       (setq gnus-newsgroup-unselected
5750             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5751       (when gnus-alter-articles-to-read-function
5752         (setq articles
5753               (sort
5754                (funcall gnus-alter-articles-to-read-function
5755                         gnus-newsgroup-name articles)
5756                '<)))
5757       articles)))
5758
5759 (defun gnus-killed-articles (killed articles)
5760   (let (out)
5761     (while articles
5762       (when (inline (gnus-member-of-range (car articles) killed))
5763         (push (car articles) out))
5764       (setq articles (cdr articles)))
5765     out))
5766
5767 (defun gnus-uncompress-marks (marks)
5768   "Uncompress the mark ranges in MARKS."
5769   (let ((uncompressed '(score bookmark))
5770         out)
5771     (while marks
5772       (if (memq (caar marks) uncompressed)
5773           (push (car marks) out)
5774         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5775       (setq marks (cdr marks)))
5776     out))
5777
5778 (defun gnus-article-mark-to-type (mark)
5779   "Return the type of MARK."
5780   (or (cadr (assq mark gnus-article-special-mark-lists))
5781       'list))
5782
5783 (defun gnus-article-unpropagatable-p (mark)
5784   "Return whether MARK should be propagated to back end."
5785   (memq mark gnus-article-unpropagated-mark-lists))
5786
5787 (defun gnus-adjust-marked-articles (info)
5788   "Set all article lists and remove all marks that are no longer valid."
5789   (let* ((marked-lists (gnus-info-marks info))
5790          (active (gnus-active (gnus-info-group info)))
5791          (min (car active))
5792          (max (cdr active))
5793          (types gnus-article-mark-lists)
5794          marks var articles article mark mark-type
5795          bgn end)
5796
5797     (dolist (marks marked-lists)
5798       (setq mark (car marks)
5799             mark-type (gnus-article-mark-to-type mark)
5800             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5801
5802       ;; We set the variable according to the type of the marks list,
5803       ;; and then adjust the marks to a subset of the active articles.
5804       (cond
5805        ;; Adjust "simple" lists - compressed yet unsorted
5806        ((eq mark-type 'list)
5807         ;; Simultaneously uncompress and clip to active range
5808         ;; See gnus-uncompress-range for a description of possible marks
5809         (let (l lh)
5810           (if (not (cadr marks))
5811               (set var nil)
5812             (setq articles (if (numberp (cddr marks))
5813                                (list (cdr marks))
5814                              (cdr marks))
5815                   lh (cons nil nil)
5816                   l lh)
5817
5818             (while (setq article (pop articles))
5819               (cond ((consp article)
5820                      (setq bgn (max (car article) min)
5821                            end (min (cdr article) max))
5822                      (while (<= bgn end)
5823                        (setq l (setcdr l (cons bgn nil))
5824                              bgn (1+ bgn))))
5825                     ((and (<= min article)
5826                           (>= max article))
5827                      (setq l (setcdr l (cons article nil))))))
5828             (set var (cdr lh)))))
5829        ;; Adjust assocs.
5830        ((eq mark-type 'tuple)
5831         (set var (setq articles (cdr marks)))
5832         (when (not (listp (cdr (symbol-value var))))
5833           (set var (list (symbol-value var))))
5834         (when (not (listp (cdr articles)))
5835           (setq articles (list articles)))
5836         (while articles
5837           (when (or (not (consp (setq article (pop articles))))
5838                     (< (car article) min)
5839                     (> (car article) max))
5840             (set var (delq article (symbol-value var))))))
5841        ;; Adjust ranges (sloppily).
5842        ((eq mark-type 'range)
5843         (cond
5844          ((eq mark 'seen)
5845           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5846           ;; It should be (seen (NUM1 . NUM2)).
5847           (when (numberp (cddr marks))
5848             (setcdr marks (list (cdr marks))))
5849           (setq articles (cdr marks))
5850           (while (and articles
5851                       (or (and (consp (car articles))
5852                                (> min (cdar articles)))
5853                           (and (numberp (car articles))
5854                                (> min (car articles)))))
5855             (pop articles))
5856           (set var articles))))))))
5857
5858 (defun gnus-update-missing-marks (missing)
5859   "Go through the list of MISSING articles and remove them from the mark lists."
5860   (when missing
5861     (let (var m)
5862       ;; Go through all types.
5863       (dolist (elem gnus-article-mark-lists)
5864         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5865           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5866           (when (symbol-value var)
5867             ;; This list has articles.  So we delete all missing
5868             ;; articles from it.
5869             (setq m missing)
5870             (while m
5871               (set var (delq (pop m) (symbol-value var))))))))))
5872
5873 (defun gnus-update-marks ()
5874   "Enter the various lists of marked articles into the newsgroup info list."
5875   (let ((types gnus-article-mark-lists)
5876         (info (gnus-get-info gnus-newsgroup-name))
5877         type list newmarked symbol delta-marks)
5878     (when info
5879       ;; Add all marks lists to the list of marks lists.
5880       (while (setq type (pop types))
5881         (setq list (symbol-value
5882                     (setq symbol
5883                           (intern (format "gnus-newsgroup-%s" (car type))))))
5884
5885         (when list
5886           ;; Get rid of the entries of the articles that have the
5887           ;; default score.
5888           (when (and (eq (cdr type) 'score)
5889                      gnus-save-score
5890                      list)
5891             (let* ((arts list)
5892                    (prev (cons nil list))
5893                    (all prev))
5894               (while arts
5895                 (if (or (not (consp (car arts)))
5896                         (= (cdar arts) gnus-summary-default-score))
5897                     (setcdr prev (cdr arts))
5898                   (setq prev arts))
5899                 (setq arts (cdr arts)))
5900               (setq list (cdr all)))))
5901
5902         (when (eq (cdr type) 'seen)
5903           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5904
5905         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5906           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5907
5908         (when (and (gnus-check-backend-function
5909                     'request-set-mark gnus-newsgroup-name)
5910                    (not (gnus-article-unpropagatable-p (cdr type))))
5911           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5912                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5913                  (add (gnus-remove-from-range
5914                        (gnus-copy-sequence list) old)))
5915             (when add
5916               (push (list add 'add (list (cdr type))) delta-marks))
5917             (when del
5918               (push (list del 'del (list (cdr type))) delta-marks))))
5919
5920         (when list
5921           (push (cons (cdr type) list) newmarked)))
5922
5923       (when delta-marks
5924         (unless (gnus-check-group gnus-newsgroup-name)
5925           (error "Can't open server for %s" gnus-newsgroup-name))
5926         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5927
5928       ;; Enter these new marks into the info of the group.
5929       (if (nthcdr 3 info)
5930           (setcar (nthcdr 3 info) newmarked)
5931         ;; Add the marks lists to the end of the info.
5932         (when newmarked
5933           (setcdr (nthcdr 2 info) (list newmarked))))
5934
5935       ;; Cut off the end of the info if there's nothing else there.
5936       (let ((i 5))
5937         (while (and (> i 2)
5938                     (not (nth i info)))
5939           (when (nthcdr (decf i) info)
5940             (setcdr (nthcdr i info) nil)))))))
5941
5942 (defun gnus-set-mode-line (where)
5943   "Set the mode line of the article or summary buffers.
5944 If WHERE is `summary', the summary mode line format will be used."
5945   ;; Is this mode line one we keep updated?
5946   (when (and (memq where gnus-updated-mode-lines)
5947              (symbol-value
5948               (intern (format "gnus-%s-mode-line-format-spec" where))))
5949     (let (mode-string)
5950       (save-excursion
5951         ;; We evaluate this in the summary buffer since these
5952         ;; variables are buffer-local to that buffer.
5953         (set-buffer gnus-summary-buffer)
5954        ;; We bind all these variables that are used in the `eval' form
5955         ;; below.
5956         (let* ((mformat (symbol-value
5957                          (intern
5958                           (format "gnus-%s-mode-line-format-spec" where))))
5959                (gnus-tmp-group-name (gnus-mode-string-quote
5960                                      (gnus-group-decoded-name
5961                                       gnus-newsgroup-name)))
5962                (gnus-tmp-article-number (or gnus-current-article 0))
5963                (gnus-tmp-unread gnus-newsgroup-unreads)
5964                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5965                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5966                (gnus-tmp-unread-and-unselected
5967                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5968                             (zerop gnus-tmp-unselected))
5969                        "")
5970                       ((zerop gnus-tmp-unselected)
5971                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5972                       (t (format "{%d(+%d) more}"
5973                                  gnus-tmp-unread-and-unticked
5974                                  gnus-tmp-unselected))))
5975                (gnus-tmp-subject
5976                 (if (and gnus-current-headers
5977                          (vectorp gnus-current-headers))
5978                     (gnus-mode-string-quote
5979                      (mail-header-subject gnus-current-headers))
5980                   ""))
5981                bufname-length max-len
5982                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5983           (setq mode-string (eval mformat))
5984           (setq bufname-length (if (string-match "%b" mode-string)
5985                                    (- (length
5986                                        (buffer-name
5987                                         (if (eq where 'summary)
5988                                             nil
5989                                           (get-buffer gnus-article-buffer))))
5990                                       2)
5991                                  0))
5992           (setq max-len (max 4 (if gnus-mode-non-string-length
5993                                    (- (window-width)
5994                                       gnus-mode-non-string-length
5995                                       bufname-length)
5996                                  (length mode-string))))
5997           ;; We might have to chop a bit of the string off...
5998           (when (> (length mode-string) max-len)
5999             (setq mode-string
6000                   (concat (truncate-string-to-width mode-string (- max-len 3))
6001                           "...")))
6002           ;; Pad the mode string a bit.
6003           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
6004       ;; Update the mode line.
6005       (setq mode-line-buffer-identification
6006             (gnus-mode-line-buffer-identification (list mode-string)))
6007       (set-buffer-modified-p t))))
6008
6009 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6010   "Go through the HEADERS list and add all Xrefs to a hash table.
6011 The resulting hash table is returned, or nil if no Xrefs were found."
6012   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6013          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6014          (xref-hashtb (gnus-make-hashtable))
6015          start group entry number xrefs header)
6016     (while headers
6017       (setq header (pop headers))
6018       (when (and (setq xrefs (mail-header-xref header))
6019                  (not (memq (setq number (mail-header-number header))
6020                             unreads)))
6021         (setq start 0)
6022         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6023           (setq start (match-end 0))
6024           (setq group (if prefix
6025                           (concat prefix (substring xrefs (match-beginning 1)
6026                                                     (match-end 1)))
6027                         (substring xrefs (match-beginning 1) (match-end 1))))
6028           (setq number
6029                 (string-to-number (substring xrefs (match-beginning 2)
6030                                           (match-end 2))))
6031           (if (setq entry (gnus-gethash group xref-hashtb))
6032               (setcdr entry (cons number (cdr entry)))
6033             (gnus-sethash group (cons number nil) xref-hashtb)))))
6034     (and start xref-hashtb)))
6035
6036 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6037   "Look through all the headers and mark the Xrefs as read."
6038   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6039         name info xref-hashtb idlist method nth4)
6040     (save-excursion
6041       (set-buffer gnus-group-buffer)
6042       (when (setq xref-hashtb
6043                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6044         (mapatoms
6045          (lambda (group)
6046            (unless (string= from-newsgroup (setq name (symbol-name group)))
6047              (setq idlist (symbol-value group))
6048              ;; Dead groups are not updated.
6049              (and (prog1
6050                       (setq info (gnus-get-info name))
6051                     (when (stringp (setq nth4 (gnus-info-method info)))
6052                       (setq nth4 (gnus-server-to-method nth4))))
6053                   ;; Only do the xrefs if the group has the same
6054                   ;; select method as the group we have just read.
6055                   (or (gnus-methods-equal-p
6056                        nth4 (gnus-find-method-for-group from-newsgroup))
6057                       virtual
6058                       (equal nth4 (setq method (gnus-find-method-for-group
6059                                                 from-newsgroup)))
6060                       (and (equal (car nth4) (car method))
6061                            (equal (nth 1 nth4) (nth 1 method))))
6062                   gnus-use-cross-reference
6063                   (or (not (eq gnus-use-cross-reference t))
6064                       virtual
6065                       ;; Only do cross-references on subscribed
6066                       ;; groups, if that is what is wanted.
6067                       (<= (gnus-info-level info) gnus-level-subscribed))
6068                   (gnus-group-make-articles-read name idlist))))
6069          xref-hashtb)))))
6070
6071 (defun gnus-compute-read-articles (group articles)
6072   (let* ((entry (gnus-group-entry group))
6073          (info (nth 2 entry))
6074          (active (gnus-active group))
6075          ninfo)
6076     (when entry
6077       ;; First peel off all invalid article numbers.
6078       (when active
6079         (let ((ids articles)
6080               id first)
6081           (while (setq id (pop ids))
6082             (when (and first (> id (cdr active)))
6083               ;; We'll end up in this situation in one particular
6084               ;; obscure situation.  If you re-scan a group and get
6085               ;; a new article that is cross-posted to a different
6086               ;; group that has not been re-scanned, you might get
6087               ;; crossposted article that has a higher number than
6088               ;; Gnus believes possible.  So we re-activate this
6089               ;; group as well.  This might mean doing the
6090               ;; crossposting thingy will *increase* the number
6091               ;; of articles in some groups.  Tsk, tsk.
6092               (setq active (or (gnus-activate-group group) active)))
6093             (when (or (> id (cdr active))
6094                       (< id (car active)))
6095               (setq articles (delq id articles))))))
6096       ;; If the read list is nil, we init it.
6097       (if (and active
6098                (null (gnus-info-read info))
6099                (> (car active) 1))
6100           (setq ninfo (cons 1 (1- (car active))))
6101         (setq ninfo (gnus-info-read info)))
6102       ;; Then we add the read articles to the range.
6103       (gnus-add-to-range
6104        ninfo (setq articles (sort articles '<))))))
6105
6106 (defun gnus-group-make-articles-read (group articles)
6107   "Update the info of GROUP to say that ARTICLES are read."
6108   (let* ((num 0)
6109          (entry (gnus-group-entry group))
6110          (info (nth 2 entry))
6111          (active (gnus-active group))
6112          range)
6113     (when entry
6114       (setq range (gnus-compute-read-articles group articles))
6115       (with-current-buffer gnus-group-buffer
6116         (gnus-undo-register
6117           `(progn
6118              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6119              (gnus-info-set-read ',info ',(gnus-info-read info))
6120              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6121              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6122              (gnus-group-update-group ,group t))))
6123       ;; Add the read articles to the range.
6124       (gnus-info-set-read info range)
6125       (gnus-request-set-mark group (list (list range 'add '(read))))
6126       ;; Then we have to re-compute how many unread
6127       ;; articles there are in this group.
6128       (when active
6129         (cond
6130          ((not range)
6131           (setq num (- (1+ (cdr active)) (car active))))
6132          ((not (listp (cdr range)))
6133           (setq num (- (cdr active) (- (1+ (cdr range))
6134                                        (car range)))))
6135          (t
6136           (while range
6137             (if (numberp (car range))
6138                 (setq num (1+ num))
6139               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6140             (setq range (cdr range)))
6141           (setq num (- (cdr active) num))))
6142         ;; Update the number of unread articles.
6143         (setcar entry num)
6144         ;; Update the group buffer.
6145         (unless (gnus-ephemeral-group-p group)
6146           (gnus-group-update-group group t))))))
6147
6148 (defvar gnus-newsgroup-none-id 0)
6149
6150 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6151   (let ((cur nntp-server-buffer)
6152         (dependencies
6153          (or dependencies
6154              (with-current-buffer gnus-summary-buffer
6155                gnus-newsgroup-dependencies)))
6156         headers id end ref number
6157         (mail-parse-charset gnus-newsgroup-charset)
6158         (mail-parse-ignored-charsets
6159          (save-excursion (condition-case nil
6160                              (set-buffer gnus-summary-buffer)
6161                            (error))
6162                          gnus-newsgroup-ignored-charsets)))
6163     (save-excursion
6164       (set-buffer nntp-server-buffer)
6165       ;; Translate all TAB characters into SPACE characters.
6166       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6167       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6168       (ietf-drums-unfold-fws)
6169       (gnus-run-hooks 'gnus-parse-headers-hook)
6170       (let ((case-fold-search t)
6171             in-reply-to header p lines chars)
6172         (goto-char (point-min))
6173         ;; Search to the beginning of the next header.  Error messages
6174         ;; do not begin with 2 or 3.
6175         (while (re-search-forward "^[23][0-9]+ " nil t)
6176           (setq id nil
6177                 ref nil)
6178           ;; This implementation of this function, with nine
6179           ;; search-forwards instead of the one re-search-forward and
6180           ;; a case (which basically was the old function) is actually
6181           ;; about twice as fast, even though it looks messier.  You
6182           ;; can't have everything, I guess.  Speed and elegance
6183           ;; doesn't always go hand in hand.
6184           (setq
6185            header
6186            (vector
6187             ;; Number.
6188             (prog1
6189                 (setq number (read cur))
6190               (end-of-line)
6191               (setq p (point))
6192               (narrow-to-region (point)
6193                                 (or (and (search-forward "\n.\n" nil t)
6194                                          (- (point) 2))
6195                                     (point))))
6196             ;; Subject.
6197             (progn
6198               (goto-char p)
6199               (if (search-forward "\nsubject:" nil t)
6200                   (funcall gnus-decode-encoded-word-function
6201                            (nnheader-header-value))
6202                 "(none)"))
6203             ;; From.
6204             (progn
6205               (goto-char p)
6206               (if (search-forward "\nfrom:" nil t)
6207                   (funcall gnus-decode-encoded-address-function
6208                            (nnheader-header-value))
6209                 "(nobody)"))
6210             ;; Date.
6211             (progn
6212               (goto-char p)
6213               (if (search-forward "\ndate:" nil t)
6214                   (nnheader-header-value) ""))
6215             ;; Message-ID.
6216             (progn
6217               (goto-char p)
6218               (setq id (if (re-search-forward
6219                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6220                            ;; We do it this way to make sure the Message-ID
6221                            ;; is (somewhat) syntactically valid.
6222                            (buffer-substring (match-beginning 1)
6223                                              (match-end 1))
6224                          ;; If there was no message-id, we just fake one
6225                          ;; to make subsequent routines simpler.
6226                          (nnheader-generate-fake-message-id number))))
6227             ;; References.
6228             (progn
6229               (goto-char p)
6230               (if (search-forward "\nreferences:" nil t)
6231                   (progn
6232                     (setq end (point))
6233                     (prog1
6234                         (nnheader-header-value)
6235                       (setq ref
6236                             (buffer-substring
6237                              (progn
6238                                (end-of-line)
6239                                (search-backward ">" end t)
6240                                (1+ (point)))
6241                              (progn
6242                                (search-backward "<" end t)
6243                                (point))))))
6244                 ;; Get the references from the in-reply-to header if there
6245                 ;; were no references and the in-reply-to header looks
6246                 ;; promising.
6247                 (if (and (search-forward "\nin-reply-to:" nil t)
6248                          (setq in-reply-to (nnheader-header-value))
6249                          (string-match "<[^>]+>" in-reply-to))
6250                     (let (ref2)
6251                       (setq ref (substring in-reply-to (match-beginning 0)
6252                                            (match-end 0)))
6253                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6254                         (setq ref2 (substring in-reply-to (match-beginning 0)
6255                                               (match-end 0)))
6256                         (when (> (length ref2) (length ref))
6257                           (setq ref ref2)))
6258                       ref)
6259                   (setq ref nil))))
6260             ;; Chars.
6261             (progn
6262               (goto-char p)
6263               (if (search-forward "\nchars: " nil t)
6264                   (if (numberp (setq chars (ignore-errors (read cur))))
6265                       chars -1)
6266                 -1))
6267             ;; Lines.
6268             (progn
6269               (goto-char p)
6270               (if (search-forward "\nlines: " nil t)
6271                   (if (numberp (setq lines (ignore-errors (read cur))))
6272                       lines -1)
6273                 -1))
6274             ;; Xref.
6275             (progn
6276               (goto-char p)
6277               (and (search-forward "\nxref:" nil t)
6278                    (nnheader-header-value)))
6279             ;; Extra.
6280             (when gnus-extra-headers
6281               (let ((extra gnus-extra-headers)
6282                     out)
6283                 (while extra
6284                   (goto-char p)
6285                   (when (search-forward
6286                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6287                     (push (cons (car extra) (nnheader-header-value))
6288                           out))
6289                   (pop extra))
6290                 out))))
6291           (when (equal id ref)
6292             (setq ref nil))
6293
6294           (when gnus-alter-header-function
6295             (funcall gnus-alter-header-function header)
6296             (setq id (mail-header-id header)
6297                   ref (gnus-parent-id (mail-header-references header))))
6298
6299           (when (setq header
6300                       (gnus-dependencies-add-header
6301                        header dependencies force-new))
6302             (push header headers))
6303           (goto-char (point-max))
6304           (widen))
6305         (nreverse headers)))))
6306
6307 ;; Goes through the xover lines and returns a list of vectors
6308 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6309                                                   force-new dependencies
6310                                                   group also-fetch-heads)
6311   "Parse the news overview data in the server buffer.
6312 Return a list of headers that match SEQUENCE (see
6313 `nntp-retrieve-headers')."
6314   ;; Get the Xref when the users reads the articles since most/some
6315   ;; NNTP servers do not include Xrefs when using XOVER.
6316   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6317   (let ((mail-parse-charset gnus-newsgroup-charset)
6318         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6319         (cur nntp-server-buffer)
6320         (dependencies (or dependencies gnus-newsgroup-dependencies))
6321         (allp (cond
6322                ((eq gnus-read-all-available-headers t)
6323                 t)
6324                ((and (stringp gnus-read-all-available-headers)
6325                      group)
6326                 (string-match gnus-read-all-available-headers group))
6327                (t
6328                 nil)))
6329         number headers header)
6330     (save-excursion
6331       (set-buffer nntp-server-buffer)
6332       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6333       ;; Allow the user to mangle the headers before parsing them.
6334       (gnus-run-hooks 'gnus-parse-headers-hook)
6335       (goto-char (point-min))
6336       (gnus-parse-without-error
6337         (while (and (or sequence allp)
6338                     (not (eobp)))
6339           (setq number (read cur))
6340           (when (not allp)
6341             (while (and sequence
6342                         (< (car sequence) number))
6343               (setq sequence (cdr sequence))))
6344           (when (and (or allp
6345                          (and sequence
6346                               (eq number (car sequence))))
6347                      (progn
6348                        (setq sequence (cdr sequence))
6349                        (setq header (inline
6350                                       (gnus-nov-parse-line
6351                                        number dependencies force-new)))))
6352             (push header headers))
6353           (forward-line 1)))
6354       ;; A common bug in inn is that if you have posted an article and
6355       ;; then retrieves the active file, it will answer correctly --
6356       ;; the new article is included.  However, a NOV entry for the
6357       ;; article may not have been generated yet, so this may fail.
6358       ;; We work around this problem by retrieving the last few
6359       ;; headers using HEAD.
6360       (if (or (not also-fetch-heads)
6361               (not sequence))
6362           ;; We (probably) got all the headers.
6363           (nreverse headers)
6364         (let ((gnus-nov-is-evil t))
6365           (nconc
6366            (nreverse headers)
6367            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6368              (gnus-get-newsgroup-headers))))))))
6369
6370 (defun gnus-article-get-xrefs ()
6371   "Fill in the Xref value in `gnus-current-headers', if necessary.
6372 This is meant to be called in `gnus-article-internal-prepare-hook'."
6373   (let ((headers (with-current-buffer gnus-summary-buffer
6374                    gnus-current-headers)))
6375     (or (not gnus-use-cross-reference)
6376         (not headers)
6377         (and (mail-header-xref headers)
6378              (not (string= (mail-header-xref headers) "")))
6379         (let ((case-fold-search t)
6380               xref)
6381           (save-restriction
6382             (nnheader-narrow-to-headers)
6383             (goto-char (point-min))
6384             (when (or (and (not (eobp))
6385                            (eq (downcase (char-after)) ?x)
6386                            (looking-at "Xref:"))
6387                       (search-forward "\nXref:" nil t))
6388               (goto-char (1+ (match-end 0)))
6389               (setq xref (buffer-substring (point) (point-at-eol)))
6390               (mail-header-set-xref headers xref)))))))
6391
6392 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6393   "Find article ID and insert the summary line for that article.
6394 OLD-HEADER can either be a header or a line number to insert
6395 the subject line on."
6396   (let* ((line (and (numberp old-header) old-header))
6397          (old-header (and (vectorp old-header) old-header))
6398          (header (cond ((and old-header use-old-header)
6399                         old-header)
6400                        ((and (numberp id)
6401                              (gnus-number-to-header id))
6402                         (gnus-number-to-header id))
6403                        (t
6404                         (gnus-read-header id))))
6405          (number (and (numberp id) id))
6406          d)
6407     (when header
6408       ;; Rebuild the thread that this article is part of and go to the
6409       ;; article we have fetched.
6410       (when (and (not gnus-show-threads)
6411                  old-header)
6412         (when (and number
6413                    (setq d (gnus-data-find (mail-header-number old-header))))
6414           (goto-char (gnus-data-pos d))
6415           (gnus-data-remove
6416            number
6417            (- (point-at-bol)
6418               (prog1
6419                   (1+ (point-at-eol))
6420                 (gnus-delete-line))))))
6421       ;; Remove list identifiers from subject.
6422       (when gnus-list-identifiers
6423         (let ((gnus-newsgroup-headers (list header)))
6424           (gnus-summary-remove-list-identifiers)))
6425       (when old-header
6426         (mail-header-set-number header (mail-header-number old-header)))
6427       (setq gnus-newsgroup-sparse
6428             (delq (setq number (mail-header-number header))
6429                   gnus-newsgroup-sparse))
6430       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6431       (push number gnus-newsgroup-limit)
6432       (gnus-rebuild-thread (mail-header-id header) line)
6433       (gnus-summary-goto-subject number nil t))
6434     (when (and (numberp number)
6435                (> number 0))
6436       ;; We have to update the boundaries even if we can't fetch the
6437       ;; article if ID is a number -- so that the next `P' or `N'
6438       ;; command will fetch the previous (or next) article even
6439       ;; if the one we tried to fetch this time has been canceled.
6440       (when (> number gnus-newsgroup-end)
6441         (setq gnus-newsgroup-end number))
6442       (when (< number gnus-newsgroup-begin)
6443         (setq gnus-newsgroup-begin number))
6444       (setq gnus-newsgroup-unselected
6445             (delq number gnus-newsgroup-unselected)))
6446     ;; Report back a success?
6447     (and header (mail-header-number header))))
6448
6449 ;;; Process/prefix in the summary buffer
6450
6451 (defun gnus-summary-work-articles (n)
6452   "Return a list of articles to be worked upon.
6453 The prefix argument, the list of process marked articles, and the
6454 current article will be taken into consideration."
6455   (save-excursion
6456     (set-buffer gnus-summary-buffer)
6457     (cond
6458      (n
6459       ;; A numerical prefix has been given.
6460       (setq n (prefix-numeric-value n))
6461       (let ((backward (< n 0))
6462             (n (abs (prefix-numeric-value n)))
6463             articles article)
6464         (save-excursion
6465           (while
6466               (and (> n 0)
6467                    (push (setq article (gnus-summary-article-number))
6468                          articles)
6469                    (if backward
6470                        (gnus-summary-find-prev nil article)
6471                      (gnus-summary-find-next nil article)))
6472             (decf n)))
6473         (nreverse articles)))
6474      ((and (gnus-region-active-p) (mark))
6475       (message "region active")
6476       ;; Work on the region between point and mark.
6477       (let ((max (max (point) (mark)))
6478             articles article)
6479         (save-excursion
6480           (goto-char (min (point) (mark)))
6481           (while
6482               (and
6483                (push (setq article (gnus-summary-article-number)) articles)
6484                (gnus-summary-find-next nil article)
6485                (< (point) max)))
6486           (nreverse articles))))
6487      (gnus-newsgroup-processable
6488       ;; There are process-marked articles present.
6489       ;; Save current state.
6490       (gnus-summary-save-process-mark)
6491       ;; Return the list.
6492       (reverse gnus-newsgroup-processable))
6493      (t
6494       ;; Just return the current article.
6495       (list (gnus-summary-article-number))))))
6496
6497 (defmacro gnus-summary-iterate (arg &rest forms)
6498   "Iterate over the process/prefixed articles and do FORMS.
6499 ARG is the interactive prefix given to the command.  FORMS will be
6500 executed with point over the summary line of the articles."
6501   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6502     `(let ((,articles (gnus-summary-work-articles ,arg)))
6503        (while ,articles
6504          (gnus-summary-goto-subject (car ,articles))
6505          ,@forms
6506          (pop ,articles)))))
6507
6508 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6509 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6510
6511 (defun gnus-summary-save-process-mark ()
6512   "Push the current set of process marked articles on the stack."
6513   (interactive)
6514   (push (copy-sequence gnus-newsgroup-processable)
6515         gnus-newsgroup-process-stack))
6516
6517 (defun gnus-summary-kill-process-mark ()
6518   "Push the current set of process marked articles on the stack and unmark."
6519   (interactive)
6520   (gnus-summary-save-process-mark)
6521   (gnus-summary-unmark-all-processable))
6522
6523 (defun gnus-summary-yank-process-mark ()
6524   "Pop the last process mark state off the stack and restore it."
6525   (interactive)
6526   (unless gnus-newsgroup-process-stack
6527     (error "Empty mark stack"))
6528   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6529
6530 (defun gnus-summary-process-mark-set (set)
6531   "Make SET into the current process marked articles."
6532   (gnus-summary-unmark-all-processable)
6533   (mapc 'gnus-summary-set-process-mark set))
6534
6535 ;;; Searching and stuff
6536
6537 (defun gnus-summary-search-group (&optional backward use-level)
6538   "Search for next unread newsgroup.
6539 If optional argument BACKWARD is non-nil, search backward instead."
6540   (save-excursion
6541     (set-buffer gnus-group-buffer)
6542     (when (gnus-group-search-forward
6543            backward nil (if use-level (gnus-group-group-level) nil))
6544       (gnus-group-group-name))))
6545
6546 (defun gnus-summary-best-group (&optional exclude-group)
6547   "Find the name of the best unread group.
6548 If EXCLUDE-GROUP, do not go to this group."
6549   (with-current-buffer gnus-group-buffer
6550     (save-excursion
6551       (gnus-group-best-unread-group exclude-group))))
6552
6553 (defun gnus-summary-find-next (&optional unread article backward)
6554   (if backward
6555       (gnus-summary-find-prev unread article)
6556     (let* ((dummy (gnus-summary-article-intangible-p))
6557            (article (or article (gnus-summary-article-number)))
6558            (data (gnus-data-find-list article))
6559            result)
6560       (when (and (not dummy)
6561                  (or (not gnus-summary-check-current)
6562                      (not unread)
6563                      (not (gnus-data-unread-p (car data)))))
6564         (setq data (cdr data)))
6565       (when (setq result
6566                   (if unread
6567                       (progn
6568                         (while data
6569                           (unless (memq (gnus-data-number (car data))
6570                                         (cond
6571                                          ((eq gnus-auto-goto-ignores
6572                                               'always-undownloaded)
6573                                           gnus-newsgroup-undownloaded)
6574                                          (gnus-plugged
6575                                           nil)
6576                                          ((eq gnus-auto-goto-ignores
6577                                               'unfetched)
6578                                           gnus-newsgroup-unfetched)
6579                                          ((eq gnus-auto-goto-ignores
6580                                               'undownloaded)
6581                                           gnus-newsgroup-undownloaded)))
6582                             (when (gnus-data-unread-p (car data))
6583                               (setq result (car data)
6584                                     data nil)))
6585                           (setq data (cdr data)))
6586                         result)
6587                     (car data)))
6588         (goto-char (gnus-data-pos result))
6589         (gnus-data-number result)))))
6590
6591 (defun gnus-summary-find-prev (&optional unread article)
6592   (let* ((eobp (eobp))
6593          (article (or article (gnus-summary-article-number)))
6594          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6595          result)
6596     (when (and (not eobp)
6597                (or (not gnus-summary-check-current)
6598                    (not unread)
6599                    (not (gnus-data-unread-p (car data)))))
6600       (setq data (cdr data)))
6601     (when (setq result
6602                 (if unread
6603                     (progn
6604                       (while data
6605                         (unless (memq (gnus-data-number (car data))
6606                                       (cond
6607                                        ((eq gnus-auto-goto-ignores
6608                                             'always-undownloaded)
6609                                         gnus-newsgroup-undownloaded)
6610                                        (gnus-plugged
6611                                         nil)
6612                                        ((eq gnus-auto-goto-ignores
6613                                             'unfetched)
6614                                         gnus-newsgroup-unfetched)
6615                                        ((eq gnus-auto-goto-ignores
6616                                             'undownloaded)
6617                                         gnus-newsgroup-undownloaded)))
6618                           (when (gnus-data-unread-p (car data))
6619                             (setq result (car data)
6620                                   data nil)))
6621                         (setq data (cdr data)))
6622                       result)
6623                   (car data)))
6624       (goto-char (gnus-data-pos result))
6625       (gnus-data-number result))))
6626
6627 (defun gnus-summary-find-subject (subject &optional unread backward article)
6628   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6629          (article (or article (gnus-summary-article-number)))
6630          (articles (gnus-data-list backward))
6631          (arts (gnus-data-find-list article articles))
6632          result)
6633     (when (or (not gnus-summary-check-current)
6634               (not unread)
6635               (not (gnus-data-unread-p (car arts))))
6636       (setq arts (cdr arts)))
6637     (while arts
6638       (and (or (not unread)
6639                (gnus-data-unread-p (car arts)))
6640            (vectorp (gnus-data-header (car arts)))
6641            (gnus-subject-equal
6642             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6643            (setq result (car arts)
6644                  arts nil))
6645       (setq arts (cdr arts)))
6646     (and result
6647          (goto-char (gnus-data-pos result))
6648          (gnus-data-number result))))
6649
6650 (defun gnus-summary-search-forward (&optional unread subject backward)
6651   "Search forward for an article.
6652 If UNREAD, look for unread articles.  If SUBJECT, look for
6653 articles with that subject.  If BACKWARD, search backward instead."
6654   (cond (subject (gnus-summary-find-subject subject unread backward))
6655         (backward (gnus-summary-find-prev unread))
6656         (t (gnus-summary-find-next unread))))
6657
6658 (defun gnus-recenter (&optional n)
6659   "Center point in window and redisplay frame.
6660 Also do horizontal recentering."
6661   (interactive "P")
6662   (when (and gnus-auto-center-summary
6663              (not (eq gnus-auto-center-summary 'vertical)))
6664     (gnus-horizontal-recenter))
6665   (recenter n))
6666
6667 (defun gnus-summary-recenter ()
6668   "Center point in the summary window.
6669 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6670 displayed, no centering will be performed."
6671   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6672   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6673   (interactive)
6674   ;; The user has to want it.
6675   (when gnus-auto-center-summary
6676     (let* ((top (cond ((< (window-height) 4) 0)
6677                       ((< (window-height) 7) 1)
6678                       (t (if (numberp gnus-auto-center-summary)
6679                              gnus-auto-center-summary
6680                            (/ (1- (window-height)) 2)))))
6681            (height (1- (window-height)))
6682            (bottom (save-excursion (goto-char (point-max))
6683                                    (forward-line (- height))
6684                                    (point)))
6685            (window (get-buffer-window (current-buffer))))
6686       (when (get-buffer-window gnus-article-buffer)
6687         ;; Only do recentering when the article buffer is displayed,
6688         ;; Set the window start to either `bottom', which is the biggest
6689         ;; possible valid number, or the second line from the top,
6690         ;; whichever is the least.
6691         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6692           (if (> bottom top-pos)
6693               ;; Keep the second line from the top visible
6694               (set-window-start window top-pos)
6695             ;; Try to keep the bottom line visible; if it's partially
6696             ;; obscured, either scroll one more line to make it fully
6697             ;; visible, or revert to using TOP-POS.
6698             (save-excursion
6699               (goto-char (point-max))
6700               (forward-line -1)
6701               (let ((last-line-start (point)))
6702                 (goto-char bottom)
6703                 (set-window-start window (point) t)
6704                 (when (not (pos-visible-in-window-p last-line-start window))
6705                   (forward-line 1)
6706                   (set-window-start window (min (point) top-pos) t)))))))
6707       ;; Do horizontal recentering while we're at it.
6708       (when (and (get-buffer-window (current-buffer) t)
6709                  (not (eq gnus-auto-center-summary 'vertical)))
6710         (let ((selected (selected-window)))
6711           (select-window (get-buffer-window (current-buffer) t))
6712           (gnus-summary-position-point)
6713           (gnus-horizontal-recenter)
6714           (select-window selected))))))
6715
6716 (defun gnus-summary-jump-to-group (newsgroup)
6717   "Move point to NEWSGROUP in group mode buffer."
6718   ;; Keep update point of group mode buffer if visible.
6719   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6720       (save-window-excursion
6721         ;; Take care of tree window mode.
6722         (when (get-buffer-window gnus-group-buffer)
6723           (pop-to-buffer gnus-group-buffer))
6724         (gnus-group-jump-to-group newsgroup))
6725     (save-excursion
6726       ;; Take care of tree window mode.
6727       (if (get-buffer-window gnus-group-buffer)
6728           (pop-to-buffer gnus-group-buffer)
6729         (set-buffer gnus-group-buffer))
6730       (gnus-group-jump-to-group newsgroup))))
6731
6732 ;; This function returns a list of article numbers based on the
6733 ;; difference between the ranges of read articles in this group and
6734 ;; the range of active articles.
6735 (defun gnus-list-of-unread-articles (group)
6736   (let* ((read (gnus-info-read (gnus-get-info group)))
6737          (active (or (gnus-active group) (gnus-activate-group group)))
6738          (last (or (cdr active)
6739                    (error "Group %s couldn't be activated " group)))
6740          (bottom (if gnus-newsgroup-maximum-articles
6741                      (max (car active)
6742                           (- last gnus-newsgroup-maximum-articles -1))
6743                    (car active)))
6744          first nlast unread)
6745     ;; If none are read, then all are unread.
6746     (if (not read)
6747         (setq first bottom)
6748       ;; If the range of read articles is a single range, then the
6749       ;; first unread article is the article after the last read
6750       ;; article.  Sounds logical, doesn't it?
6751       (if (and (not (listp (cdr read)))
6752                (or (< (car read) bottom)
6753                    (progn (setq read (list read))
6754                           nil)))
6755           (setq first (max bottom (1+ (cdr read))))
6756         ;; `read' is a list of ranges.
6757         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6758                                   (caar read)))
6759                   1)
6760           (setq first bottom))
6761         (while read
6762           (when first
6763             (while (< first nlast)
6764               (setq unread (cons first unread)
6765                     first (1+ first))))
6766           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6767           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6768           (setq read (cdr read)))))
6769     ;; And add the last unread articles.
6770     (while (<= first last)
6771       (setq unread (cons first unread)
6772             first (1+ first)))
6773     ;; Return the list of unread articles.
6774     (delq 0 (nreverse unread))))
6775
6776 (defun gnus-list-of-read-articles (group)
6777   "Return a list of unread, unticked and non-dormant articles."
6778   (let* ((info (gnus-get-info group))
6779          (marked (gnus-info-marks info))
6780          (active (gnus-active group)))
6781     (and info active
6782          (gnus-list-range-difference
6783           (gnus-list-range-difference
6784            (gnus-sorted-complement
6785             (gnus-uncompress-range
6786              (if gnus-newsgroup-maximum-articles
6787                  (cons (max (car active)
6788                             (- (cdr active)
6789                                gnus-newsgroup-maximum-articles
6790                                -1))
6791                        (cdr active))
6792                active))
6793             (gnus-list-of-unread-articles group))
6794            (cdr (assq 'dormant marked)))
6795           (cdr (assq 'tick marked))))))
6796
6797 ;; This function returns a sequence of article numbers based on the
6798 ;; difference between the ranges of read articles in this group and
6799 ;; the range of active articles.
6800 (defun gnus-sequence-of-unread-articles (group)
6801   (let* ((read (gnus-info-read (gnus-get-info group)))
6802          (active (or (gnus-active group) (gnus-activate-group group)))
6803          (last (cdr active))
6804          (bottom (if gnus-newsgroup-maximum-articles
6805                      (max (car active)
6806                           (- last gnus-newsgroup-maximum-articles -1))
6807                    (car active)))
6808          first nlast unread)
6809     ;; If none are read, then all are unread.
6810     (if (not read)
6811         (setq first bottom)
6812       ;; If the range of read articles is a single range, then the
6813       ;; first unread article is the article after the last read
6814       ;; article.  Sounds logical, doesn't it?
6815       (if (and (not (listp (cdr read)))
6816                (or (< (car read) bottom)
6817                    (progn (setq read (list read))
6818                           nil)))
6819           (setq first (max bottom (1+ (cdr read))))
6820         ;; `read' is a list of ranges.
6821         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6822                                   (caar read)))
6823                   1)
6824           (setq first bottom))
6825         (while read
6826           (when first
6827             (push (cons first nlast) unread))
6828           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6829           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6830           (setq read (cdr read)))))
6831     ;; And add the last unread articles.
6832     (cond ((not (and first last))
6833            nil)
6834           ((< first last)
6835            (push (cons first last) unread))
6836           ((= first last)
6837            (push first unread)))
6838     ;; Return the sequence of unread articles.
6839     (delq 0 (nreverse unread))))
6840
6841 ;; Various summary commands
6842
6843 (defun gnus-summary-select-article-buffer ()
6844   "Reconfigure windows to show article buffer."
6845   (interactive)
6846   (if (not (gnus-buffer-live-p gnus-article-buffer))
6847       (error "There is no article buffer for this summary buffer")
6848     (gnus-configure-windows 'article)
6849     (select-window (get-buffer-window gnus-article-buffer))))
6850
6851 (defun gnus-summary-universal-argument (arg)
6852   "Perform any operation on all articles that are process/prefixed."
6853   (interactive "P")
6854   (let ((articles (gnus-summary-work-articles arg))
6855         func article)
6856     (if (eq
6857          (setq
6858           func
6859           (key-binding
6860            (read-key-sequence
6861             (substitute-command-keys
6862              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6863          'undefined)
6864         (gnus-error 1 "Undefined key")
6865       (save-excursion
6866         (while articles
6867           (gnus-summary-goto-subject (setq article (pop articles)))
6868           (let (gnus-newsgroup-processable)
6869             (command-execute func))
6870           (gnus-summary-remove-process-mark article)))))
6871   (gnus-summary-position-point))
6872
6873 (defun gnus-summary-toggle-truncation (&optional arg)
6874   "Toggle truncation of summary lines.
6875 With ARG, turn line truncation on if ARG is positive."
6876   (interactive "P")
6877   (setq truncate-lines
6878         (if (null arg) (not truncate-lines)
6879           (> (prefix-numeric-value arg) 0)))
6880   (redraw-display))
6881
6882 (defun gnus-summary-find-for-reselect ()
6883   "Return the number of an article to stay on across a reselect.
6884 The current article is considered, then following articles, then previous
6885 articles.  An article is sought which is not cancelled and isn't a temporary
6886 insertion from another group.  If there's no such then return a dummy 0."
6887   (let (found)
6888     (dolist (rev '(nil t))
6889       (unless found      ; don't demand the reverse list if we don't need it
6890         (let ((data (gnus-data-find-list
6891                      (gnus-summary-article-number) (gnus-data-list rev))))
6892           (while (and data (not found))
6893             (if (and (< 0 (gnus-data-number (car data)))
6894                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6895                 (setq found (gnus-data-number (car data))))
6896             (setq data (cdr data))))))
6897     (or found 0)))
6898
6899 (defun gnus-summary-reselect-current-group (&optional all rescan)
6900   "Exit and then reselect the current newsgroup.
6901 The prefix argument ALL means to select all articles."
6902   (interactive "P")
6903   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6904     (error "Ephemeral groups can't be reselected"))
6905   (let ((current-subject (gnus-summary-find-for-reselect))
6906         (group gnus-newsgroup-name))
6907     (setq gnus-newsgroup-begin nil)
6908     (gnus-summary-exit nil 'leave-hidden)
6909     ;; We have to adjust the point of group mode buffer because
6910     ;; point was moved to the next unread newsgroup by exiting.
6911     (gnus-summary-jump-to-group group)
6912     (when rescan
6913       (save-excursion
6914         (gnus-group-get-new-news-this-group 1)))
6915     (gnus-group-read-group all t)
6916     (gnus-summary-goto-subject current-subject nil t)))
6917
6918 (defun gnus-summary-rescan-group (&optional all)
6919   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6920   (interactive "P")
6921   (gnus-summary-reselect-current-group all t))
6922
6923 (defun gnus-summary-update-info (&optional non-destructive)
6924   (save-excursion
6925     (let ((group gnus-newsgroup-name))
6926       (when group
6927         (when gnus-newsgroup-kill-headers
6928           (setq gnus-newsgroup-killed
6929                 (gnus-compress-sequence
6930                  (gnus-sorted-union
6931                   (gnus-list-range-intersection
6932                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6933                   gnus-newsgroup-unreads)
6934                  t)))
6935         (unless (listp (cdr gnus-newsgroup-killed))
6936           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6937         (let ((headers gnus-newsgroup-headers))
6938           ;; Set the new ranges of read articles.
6939           (with-current-buffer gnus-group-buffer
6940             (gnus-undo-force-boundary))
6941           (gnus-update-read-articles
6942            group (gnus-sorted-union
6943                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6944           ;; Set the current article marks.
6945           (let ((gnus-newsgroup-scored
6946                  (if (and (not gnus-save-score)
6947                           (not non-destructive))
6948                      nil
6949                    gnus-newsgroup-scored)))
6950             (save-excursion
6951               (gnus-update-marks)))
6952           ;; Do the cross-ref thing.
6953           (when gnus-use-cross-reference
6954             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6955           ;; Do not switch windows but change the buffer to work.
6956           (set-buffer gnus-group-buffer)
6957           (unless (gnus-ephemeral-group-p group)
6958             (gnus-group-update-group group)))))))
6959
6960 (defun gnus-summary-save-newsrc (&optional force)
6961   "Save the current number of read/marked articles in the dribble buffer.
6962 The dribble buffer will then be saved.
6963 If FORCE (the prefix), also save the .newsrc file(s)."
6964   (interactive "P")
6965   (gnus-summary-update-info t)
6966   (if force
6967       (gnus-save-newsrc-file)
6968     (gnus-dribble-save)))
6969
6970 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
6971
6972 (defun gnus-summary-exit (&optional temporary leave-hidden)
6973   "Exit reading current newsgroup, and then return to group selection mode.
6974 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6975   (interactive)
6976   (gnus-set-global-variables)
6977   (when (gnus-buffer-live-p gnus-article-buffer)
6978     (save-excursion
6979       (set-buffer gnus-article-buffer)
6980       (mm-destroy-parts gnus-article-mime-handles)
6981       ;; Set it to nil for safety reason.
6982       (setq gnus-article-mime-handle-alist nil)
6983       (setq gnus-article-mime-handles nil)))
6984   (gnus-kill-save-kill-buffer)
6985   (gnus-async-halt-prefetch)
6986   (let* ((group gnus-newsgroup-name)
6987          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6988          (gnus-group-is-exiting-p t)
6989          (mode major-mode)
6990          (group-point nil)
6991          (buf (current-buffer)))
6992     (unless quit-config
6993       ;; Do adaptive scoring, and possibly save score files.
6994       (when gnus-newsgroup-adaptive
6995         (gnus-score-adaptive))
6996       (when gnus-use-scoring
6997         (gnus-score-save)))
6998     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6999     ;; If we have several article buffers, we kill them at exit.
7000     (unless gnus-single-article-buffer
7001       (when (gnus-buffer-live-p gnus-article-buffer)
7002         (with-current-buffer gnus-article-buffer
7003           ;; Don't kill sticky article buffers
7004           (unless (eq major-mode 'gnus-sticky-article-mode)
7005             (gnus-kill-buffer gnus-article-buffer)
7006             (setq gnus-article-current nil))))
7007       (gnus-kill-buffer gnus-original-article-buffer))
7008     (when gnus-use-cache
7009       (gnus-cache-possibly-remove-articles)
7010       (gnus-cache-save-buffers))
7011     (gnus-async-prefetch-remove-group group)
7012     (when gnus-suppress-duplicates
7013       (gnus-dup-enter-articles))
7014     (when gnus-use-trees
7015       (gnus-tree-close group))
7016     (when gnus-use-cache
7017       (gnus-cache-write-active))
7018     ;; Remove entries for this group.
7019     (nnmail-purge-split-history (gnus-group-real-name group))
7020     ;; Make all changes in this group permanent.
7021     (unless quit-config
7022       (gnus-run-hooks 'gnus-exit-group-hook)
7023       (gnus-summary-update-info))
7024     (gnus-close-group group)
7025     ;; Make sure where we were, and go to next newsgroup.
7026     (set-buffer gnus-group-buffer)
7027     (unless quit-config
7028       (gnus-group-jump-to-group group))
7029     (gnus-run-hooks 'gnus-summary-exit-hook)
7030     (unless (or quit-config
7031                 (not gnus-summary-next-group-on-exit)
7032                 ;; If this group has disappeared from the summary
7033                 ;; buffer, don't skip forwards.
7034                 (not (string= group (gnus-group-group-name))))
7035       (gnus-group-next-unread-group 1))
7036     (setq group-point (point))
7037     (if temporary
7038         nil                             ;Nothing to do.
7039       (set-buffer buf)
7040       (if (not gnus-kill-summary-on-exit)
7041           (progn
7042             (gnus-deaden-summary)
7043             (setq mode nil))
7044         ;; We set all buffer-local variables to nil.  It is unclear why
7045         ;; this is needed, but if we don't, buffer-local variables are
7046         ;; not garbage-collected, it seems.  This would the lead to en
7047         ;; ever-growing Emacs.
7048         (gnus-summary-clear-local-variables)
7049         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7050           (gnus-summary-clear-local-variables))
7051         (when (get-buffer gnus-article-buffer)
7052           (bury-buffer gnus-article-buffer))
7053         ;; Return to group mode buffer.
7054         (when (eq mode 'gnus-summary-mode)
7055           (gnus-kill-buffer buf)))
7056       (setq gnus-current-select-method gnus-select-method)
7057       (set-buffer gnus-group-buffer)
7058       (if quit-config
7059           (gnus-handle-ephemeral-exit quit-config)
7060         (goto-char group-point)
7061         ;; If gnus-group-buffer is already displayed, make sure we also move
7062         ;; the cursor in the window that displays it.
7063         (let ((win (get-buffer-window (current-buffer) 0)))
7064           (if win (set-window-point win (point))))
7065         (unless leave-hidden
7066           (gnus-configure-windows 'group 'force)))
7067       ;; Clear the current group name.
7068       (unless quit-config
7069         (setq gnus-newsgroup-name nil)))))
7070
7071 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7072 (defun gnus-summary-exit-no-update (&optional no-questions)
7073   "Quit reading current newsgroup without updating read article info."
7074   (interactive)
7075   (let* ((group gnus-newsgroup-name)
7076          (gnus-group-is-exiting-p t)
7077          (gnus-group-is-exiting-without-update-p t)
7078          (quit-config (gnus-group-quit-config group)))
7079     (when (or no-questions
7080               gnus-expert-user
7081               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7082       (gnus-async-halt-prefetch)
7083       (run-hooks 'gnus-summary-prepare-exit-hook)
7084       (when (gnus-buffer-live-p gnus-article-buffer)
7085         (save-excursion
7086           (set-buffer gnus-article-buffer)
7087           (mm-destroy-parts gnus-article-mime-handles)
7088           ;; Set it to nil for safety reason.
7089           (setq gnus-article-mime-handle-alist nil)
7090           (setq gnus-article-mime-handles nil)))
7091       ;; If we have several article buffers, we kill them at exit.
7092       (unless gnus-single-article-buffer
7093         (gnus-kill-buffer gnus-article-buffer)
7094         (gnus-kill-buffer gnus-original-article-buffer)
7095         (setq gnus-article-current nil))
7096       (if (not gnus-kill-summary-on-exit)
7097           (gnus-deaden-summary)
7098         (gnus-close-group group)
7099         (gnus-summary-clear-local-variables)
7100         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7101           (gnus-summary-clear-local-variables))
7102         (gnus-kill-buffer gnus-summary-buffer))
7103       (unless gnus-single-article-buffer
7104         (setq gnus-article-current nil))
7105       (when gnus-use-trees
7106         (gnus-tree-close group))
7107       (gnus-async-prefetch-remove-group group)
7108       (when (get-buffer gnus-article-buffer)
7109         (bury-buffer gnus-article-buffer))
7110       ;; Return to the group buffer.
7111       (gnus-configure-windows 'group 'force)
7112       ;; Clear the current group name.
7113       (setq gnus-newsgroup-name nil)
7114       (unless (gnus-ephemeral-group-p group)
7115         (gnus-group-update-group group))
7116       (when (equal (gnus-group-group-name) group)
7117         (gnus-group-next-unread-group 1))
7118       (when quit-config
7119         (gnus-handle-ephemeral-exit quit-config)))))
7120
7121 (defun gnus-handle-ephemeral-exit (quit-config)
7122   "Handle movement when leaving an ephemeral group.
7123 The state which existed when entering the ephemeral is reset."
7124   (if (not (buffer-name (car quit-config)))
7125       (gnus-configure-windows 'group 'force)
7126     (set-buffer (car quit-config))
7127     (cond ((eq major-mode 'gnus-summary-mode)
7128            (gnus-set-global-variables))
7129           ((eq major-mode 'gnus-article-mode)
7130            (save-excursion
7131              ;; The `gnus-summary-buffer' variable may point
7132              ;; to the old summary buffer when using a single
7133              ;; article buffer.
7134              (unless (gnus-buffer-live-p gnus-summary-buffer)
7135                (set-buffer gnus-group-buffer))
7136              (set-buffer gnus-summary-buffer)
7137              (gnus-set-global-variables))))
7138     (if (or (eq (cdr quit-config) 'article)
7139             (eq (cdr quit-config) 'pick))
7140         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7141             (gnus-configure-windows 'pick 'force)
7142           (gnus-configure-windows (cdr quit-config) 'force))
7143       (gnus-configure-windows (cdr quit-config) 'force))
7144     (when (eq major-mode 'gnus-summary-mode)
7145       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7146                                                      next-unread-noselect))
7147           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7148                                   'next-noselect)
7149                               (gnus-summary-next-subject 1 nil t))
7150                              ((eq gnus-auto-select-on-ephemeral-exit
7151                                   'next-unread-noselect)
7152                               (gnus-summary-next-subject 1 t t))))
7153             ;; Hide the article buffer which displays the article different
7154             ;; from the one that the cursor points to in the summary buffer.
7155             (gnus-configure-windows 'summary 'force))
7156         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7157                (gnus-summary-next-subject 1))
7158               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7159                (gnus-summary-next-subject 1 t))))
7160       (gnus-summary-recenter)
7161       (gnus-summary-position-point))))
7162
7163 ;;; Dead summaries.
7164
7165 (defvar gnus-dead-summary-mode-map nil)
7166
7167 (unless gnus-dead-summary-mode-map
7168   (setq gnus-dead-summary-mode-map (make-keymap))
7169   (suppress-keymap gnus-dead-summary-mode-map)
7170   (substitute-key-definition
7171    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7172   (dolist (key '("\C-d" "\r" "\177" [delete]))
7173     (define-key gnus-dead-summary-mode-map
7174       key 'gnus-summary-wake-up-the-dead))
7175   (dolist (key '("q" "Q"))
7176     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7177
7178 (defvar gnus-dead-summary-mode nil
7179   "Minor mode for Gnus summary buffers.")
7180
7181 (defun gnus-dead-summary-mode (&optional arg)
7182   "Minor mode for Gnus summary buffers."
7183   (interactive "P")
7184   (when (eq major-mode 'gnus-summary-mode)
7185     (make-local-variable 'gnus-dead-summary-mode)
7186     (setq gnus-dead-summary-mode
7187           (if (null arg) (not gnus-dead-summary-mode)
7188             (> (prefix-numeric-value arg) 0)))
7189     (when gnus-dead-summary-mode
7190       (add-minor-mode
7191        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7192
7193 (defun gnus-deaden-summary ()
7194   "Make the current summary buffer into a dead summary buffer."
7195   ;; Kill any previous dead summary buffer.
7196   (when (and gnus-dead-summary
7197              (buffer-name gnus-dead-summary))
7198     (with-current-buffer gnus-dead-summary
7199       (when gnus-dead-summary-mode
7200         (kill-buffer (current-buffer)))))
7201   ;; Make this the current dead summary.
7202   (setq gnus-dead-summary (current-buffer))
7203   (gnus-dead-summary-mode 1)
7204   (let ((name (buffer-name)))
7205     (when (string-match "Summary" name)
7206       (rename-buffer
7207        (concat (substring name 0 (match-beginning 0)) "Dead "
7208                (substring name (match-beginning 0)))
7209        t)
7210       (bury-buffer))))
7211
7212 (defun gnus-kill-or-deaden-summary (buffer)
7213   "Kill or deaden the summary BUFFER."
7214   (save-excursion
7215     (when (and (buffer-name buffer)
7216                (not gnus-single-article-buffer))
7217       (with-current-buffer buffer
7218         (gnus-kill-buffer gnus-article-buffer)
7219         (gnus-kill-buffer gnus-original-article-buffer)))
7220     (cond
7221      ;; Kill the buffer.
7222      (gnus-kill-summary-on-exit
7223       (when (and gnus-use-trees
7224                  (gnus-buffer-exists-p buffer))
7225         (save-excursion
7226           (set-buffer buffer)
7227           (gnus-tree-close gnus-newsgroup-name)))
7228       (gnus-kill-buffer buffer))
7229      ;; Deaden the buffer.
7230      ((gnus-buffer-exists-p buffer)
7231       (save-excursion
7232         (set-buffer buffer)
7233         (gnus-deaden-summary))))))
7234
7235 (defun gnus-summary-wake-up-the-dead (&rest args)
7236   "Wake up the dead summary buffer."
7237   (interactive)
7238   (gnus-dead-summary-mode -1)
7239   (let ((name (buffer-name)))
7240     (when (string-match "Dead " name)
7241       (rename-buffer
7242        (concat (substring name 0 (match-beginning 0))
7243                (substring name (match-end 0)))
7244        t)))
7245   (gnus-message 3 "This dead summary is now alive again"))
7246
7247 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7248 (defun gnus-summary-fetch-faq (&optional faq-dir)
7249   "Fetch the FAQ for the current group.
7250 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7251 in."
7252   (interactive
7253    (list
7254     (when current-prefix-arg
7255       (completing-read
7256        "FAQ dir: " (and (listp gnus-group-faq-directory)
7257                         (mapcar 'list
7258                                 gnus-group-faq-directory))))))
7259   (let (gnus-faq-buffer)
7260     (when (setq gnus-faq-buffer
7261                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7262       (gnus-configure-windows 'summary-faq))))
7263
7264 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7265 (defun gnus-summary-describe-group (&optional force)
7266   "Describe the current newsgroup."
7267   (interactive "P")
7268   (gnus-group-describe-group force gnus-newsgroup-name))
7269
7270 (defun gnus-summary-describe-briefly ()
7271   "Describe summary mode commands briefly."
7272   (interactive)
7273   (gnus-message 6 (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")))
7274
7275 ;; Walking around group mode buffer from summary mode.
7276
7277 (defun gnus-summary-next-group (&optional no-article target-group backward)
7278   "Exit current newsgroup and then select next unread newsgroup.
7279 If prefix argument NO-ARTICLE is non-nil, no article is selected
7280 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7281 previous group instead."
7282   (interactive "P")
7283   ;; Stop pre-fetching.
7284   (gnus-async-halt-prefetch)
7285   (let ((current-group gnus-newsgroup-name)
7286         (current-buffer (current-buffer))
7287         entered)
7288     ;; First we semi-exit this group to update Xrefs and all variables.
7289     ;; We can't do a real exit, because the window conf must remain
7290     ;; the same in case the user is prompted for info, and we don't
7291     ;; want the window conf to change before that...
7292     (gnus-summary-exit t)
7293     (while (not entered)
7294       ;; Then we find what group we are supposed to enter.
7295       (set-buffer gnus-group-buffer)
7296       (gnus-group-jump-to-group current-group)
7297       (setq target-group
7298             (or target-group
7299                 (if (eq gnus-keep-same-level 'best)
7300                     (gnus-summary-best-group gnus-newsgroup-name)
7301                   (gnus-summary-search-group backward gnus-keep-same-level))))
7302       (if (not target-group)
7303           ;; There are no further groups, so we return to the group
7304           ;; buffer.
7305           (progn
7306             (gnus-message 5 "Returning to the group buffer")
7307             (setq entered t)
7308             (when (gnus-buffer-live-p current-buffer)
7309               (set-buffer current-buffer)
7310               (gnus-summary-exit))
7311             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7312         ;; We try to enter the target group.
7313         (gnus-group-jump-to-group target-group)
7314         (let ((unreads (gnus-group-group-unread)))
7315           (if (and (or (eq t unreads)
7316                        (and unreads (not (zerop unreads))))
7317                    (gnus-summary-read-group
7318                     target-group nil no-article
7319                     (and (buffer-name current-buffer) current-buffer)
7320                     nil backward))
7321               (setq entered t)
7322             (setq current-group target-group
7323                   target-group nil)))))))
7324
7325 (defun gnus-summary-prev-group (&optional no-article)
7326   "Exit current newsgroup and then select previous unread newsgroup.
7327 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7328   (interactive "P")
7329   (gnus-summary-next-group no-article nil t))
7330
7331 ;; Walking around summary lines.
7332
7333 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7334   "Go to the first subject satisfying any non-nil constraint.
7335 If UNREAD is non-nil, the article should be unread.
7336 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7337 If UNSEEN is non-nil, the article should be unseen.
7338 Returns the article selected or nil if there are no matching articles."
7339   (interactive "P")
7340   (cond
7341    ;; Empty summary.
7342    ((null gnus-newsgroup-data)
7343     (gnus-message 3 "No articles in the group")
7344     nil)
7345    ;; Pick the first article.
7346    ((not (or unread undownloaded unseen))
7347     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7348     (gnus-data-number (car gnus-newsgroup-data)))
7349    ;; Find the first unread article.
7350    (t
7351     (let ((data gnus-newsgroup-data))
7352       (while (and data
7353                   (let ((num (gnus-data-number (car data))))
7354                     (or (memq num gnus-newsgroup-unfetched)
7355                         (not (or (and unread
7356                                       (memq num gnus-newsgroup-unreads))
7357                                  (and undownloaded
7358                                       (memq num gnus-newsgroup-undownloaded))
7359                                  (and unseen
7360                                       (memq num gnus-newsgroup-unseen)))))))
7361         (setq data (cdr data)))
7362       (prog1
7363           (if data
7364               (progn
7365                 (goto-char (gnus-data-pos (car data)))
7366                 (gnus-data-number (car data)))
7367             (gnus-message 3 "No more%s articles"
7368                           (let* ((r (when unread " unread"))
7369                                  (d (when undownloaded " undownloaded"))
7370                                  (s (when unseen " unseen"))
7371                                  (l (delq nil (list r d s))))
7372                             (cond ((= 3 (length l))
7373                                    (concat r "," d ", or" s))
7374                                   ((= 2 (length l))
7375                                    (concat (car l) ", or" (cadr l)))
7376                                   ((= 1 (length l))
7377                                    (car l))
7378                                   (t
7379                                    ""))))
7380             nil
7381             )
7382         (gnus-summary-position-point))))))
7383
7384 (defun gnus-summary-next-subject (n &optional unread dont-display)
7385   "Go to next N'th summary line.
7386 If N is negative, go to the previous N'th subject line.
7387 If UNREAD is non-nil, only unread articles are selected.
7388 The difference between N and the actual number of steps taken is
7389 returned."
7390   (interactive "p")
7391   (let ((backward (< n 0))
7392         (n (abs n)))
7393     (while (and (> n 0)
7394                 (if backward
7395                     (gnus-summary-find-prev unread)
7396                   (gnus-summary-find-next unread)))
7397       (unless (zerop (setq n (1- n)))
7398         (gnus-summary-show-thread)))
7399     (when (/= 0 n)
7400       (gnus-message 7 "No more%s articles"
7401                     (if unread " unread" "")))
7402     (unless dont-display
7403       (gnus-summary-recenter)
7404       (gnus-summary-position-point))
7405     n))
7406
7407 (defun gnus-summary-next-unread-subject (n)
7408   "Go to next N'th unread summary line."
7409   (interactive "p")
7410   (gnus-summary-next-subject n t))
7411
7412 (defun gnus-summary-prev-subject (n &optional unread)
7413   "Go to previous N'th summary line.
7414 If optional argument UNREAD is non-nil, only unread article is selected."
7415   (interactive "p")
7416   (gnus-summary-next-subject (- n) unread))
7417
7418 (defun gnus-summary-prev-unread-subject (n)
7419   "Go to previous N'th unread summary line."
7420   (interactive "p")
7421   (gnus-summary-next-subject (- n) t))
7422
7423 (defun gnus-summary-goto-subjects (articles)
7424   "Insert the subject header for ARTICLES in the current buffer."
7425   (save-excursion
7426     (dolist (article articles)
7427       (gnus-summary-goto-subject article t)))
7428   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7429   (gnus-summary-position-point))
7430
7431 (defun gnus-summary-goto-subject (article &optional force silent)
7432   "Go the subject line of ARTICLE.
7433 If FORCE, also allow jumping to articles not currently shown."
7434   (interactive "nArticle number: ")
7435   (unless (numberp article)
7436     (error "Article %s is not a number" article))
7437   (let ((b (point))
7438         (data (gnus-data-find article)))
7439     ;; We read in the article if we have to.
7440     (and (not data)
7441          force
7442          (gnus-summary-insert-subject
7443           article
7444           (if (or (numberp force) (vectorp force)) force)
7445           t)
7446          (setq data (gnus-data-find article)))
7447     (goto-char b)
7448     (if (not data)
7449         (progn
7450           (unless silent
7451             (gnus-message 3 "Can't find article %d" article))
7452           nil)
7453       (let ((pt (gnus-data-pos data)))
7454         (goto-char pt)
7455         (gnus-summary-set-article-display-arrow pt))
7456       (gnus-summary-position-point)
7457       article)))
7458
7459 ;; Walking around summary lines with displaying articles.
7460
7461 (defun gnus-summary-expand-window (&optional arg)
7462   "Make the summary buffer take up the entire Emacs frame.
7463 Given a prefix, will force an `article' buffer configuration."
7464   (interactive "P")
7465   (if arg
7466       (gnus-configure-windows 'article 'force)
7467     (gnus-configure-windows 'summary 'force)))
7468
7469 (defun gnus-summary-display-article (article &optional all-header)
7470   "Display ARTICLE in article buffer."
7471   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7472                (with-current-buffer gnus-article-buffer
7473                  (eq major-mode 'gnus-article-mode)))
7474     (gnus-article-setup-buffer))
7475   (gnus-set-global-variables)
7476   (with-current-buffer gnus-article-buffer
7477     (setq gnus-article-charset gnus-newsgroup-charset)
7478     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7479     (mm-enable-multibyte))
7480   (if (null article)
7481       nil
7482     (prog1
7483         (if gnus-summary-display-article-function
7484             (funcall gnus-summary-display-article-function article all-header)
7485           (gnus-article-prepare article all-header))
7486       (gnus-run-hooks 'gnus-select-article-hook)
7487       (when (and gnus-current-article
7488                  (not (zerop gnus-current-article)))
7489         (gnus-summary-goto-subject gnus-current-article))
7490       (gnus-summary-recenter)
7491       (when (and gnus-use-trees gnus-show-threads)
7492         (gnus-possibly-generate-tree article)
7493         (gnus-highlight-selected-tree article))
7494       ;; Successfully display article.
7495       (gnus-article-set-window-start
7496        (cdr (assq article gnus-newsgroup-bookmarks))))))
7497
7498 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7499   "Select the current article.
7500 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7501 non-nil, the article will be re-fetched even if it already present in
7502 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7503 be displayed."
7504   ;; Make sure we are in the summary buffer to work around bbdb bug.
7505   (unless (eq major-mode 'gnus-summary-mode)
7506     (set-buffer gnus-summary-buffer))
7507   (let ((article (or article (gnus-summary-article-number)))
7508         (all-headers (not (not all-headers))) ;Must be t or nil.
7509         gnus-summary-display-article-function)
7510     (and (not pseudo)
7511          (gnus-summary-article-pseudo-p article)
7512          (error "This is a pseudo-article"))
7513     (save-excursion
7514       (set-buffer gnus-summary-buffer)
7515       (if (or (and gnus-single-article-buffer
7516                    (or (null gnus-current-article)
7517                        (null gnus-article-current)
7518                        (null (get-buffer gnus-article-buffer))
7519                        (not (eq article (cdr gnus-article-current)))
7520                        (not (equal (car gnus-article-current)
7521                                    gnus-newsgroup-name))))
7522               (and (not gnus-single-article-buffer)
7523                    (or (null gnus-current-article)
7524                        (not (eq gnus-current-article article))))
7525               force)
7526           ;; The requested article is different from the current article.
7527           (progn
7528             (gnus-summary-display-article article all-headers)
7529             (when (gnus-buffer-live-p gnus-article-buffer)
7530               (with-current-buffer gnus-article-buffer
7531                 (if (not gnus-article-decoded-p) ;; a local variable
7532                     (mm-disable-multibyte))))
7533             (gnus-article-set-window-start
7534              (cdr (assq article gnus-newsgroup-bookmarks)))
7535             article)
7536         'old))))
7537
7538 (defun gnus-summary-force-verify-and-decrypt ()
7539   "Display buttons for signed/encrypted parts and verify/decrypt them."
7540   (interactive)
7541   (let ((mm-verify-option 'known)
7542         (mm-decrypt-option 'known)
7543         (gnus-article-emulate-mime t)
7544         (gnus-buttonized-mime-types (append (list "multipart/signed"
7545                                                   "multipart/encrypted")
7546                                             gnus-buttonized-mime-types)))
7547     (gnus-summary-select-article nil 'force)))
7548
7549 (defun gnus-summary-set-current-mark (&optional current-mark)
7550   "Obsolete function."
7551   nil)
7552
7553 (defun gnus-summary-next-article (&optional unread subject backward push)
7554   "Select the next article.
7555 If UNREAD, only unread articles are selected.
7556 If SUBJECT, only articles with SUBJECT are selected.
7557 If BACKWARD, the previous article is selected instead of the next."
7558   (interactive "P")
7559   ;; Make sure we are in the summary buffer.
7560   (unless (eq major-mode 'gnus-summary-mode)
7561     (set-buffer gnus-summary-buffer))
7562   (cond
7563    ;; Is there such an article?
7564    ((and (gnus-summary-search-forward unread subject backward)
7565          (or (gnus-summary-display-article (gnus-summary-article-number))
7566              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7567     (gnus-summary-position-point))
7568    ;; If not, we try the first unread, if that is wanted.
7569    ((and subject
7570          gnus-auto-select-same
7571          (gnus-summary-first-unread-article))
7572     (gnus-summary-position-point)
7573     (gnus-message 6 "Wrapped"))
7574    ;; Try to get next/previous article not displayed in this group.
7575    ((and gnus-auto-extend-newsgroup
7576          (not unread) (not subject))
7577     (gnus-summary-goto-article
7578      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7579      nil (count-lines (point-min) (point))))
7580    ;; Go to next/previous group.
7581    (t
7582     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7583       (gnus-summary-jump-to-group gnus-newsgroup-name))
7584     (let ((cmd last-command-char)
7585           (point
7586            (with-current-buffer gnus-group-buffer
7587              (point)))
7588           (group
7589            (if (eq gnus-keep-same-level 'best)
7590                (gnus-summary-best-group gnus-newsgroup-name)
7591              (gnus-summary-search-group backward gnus-keep-same-level))))
7592       ;; For some reason, the group window gets selected.  We change
7593       ;; it back.
7594       (select-window (get-buffer-window (current-buffer)))
7595       ;; Select next unread newsgroup automagically.
7596       (cond
7597        ((or (not gnus-auto-select-next)
7598             (not cmd))
7599         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7600        ((or (eq gnus-auto-select-next 'quietly)
7601             (and (eq gnus-auto-select-next 'slightly-quietly)
7602                  push)
7603             (and (eq gnus-auto-select-next 'almost-quietly)
7604                  (gnus-summary-last-article-p)))
7605         ;; Select quietly.
7606         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7607             (gnus-summary-exit)
7608           (gnus-message 7 "No more%s articles (%s)..."
7609                         (if unread " unread" "")
7610                         (if group (concat "selecting " group)
7611                           "exiting"))
7612           (gnus-summary-next-group nil group backward)))
7613        (t
7614         (when (gnus-key-press-event-p last-input-event)
7615           (gnus-summary-walk-group-buffer
7616            gnus-newsgroup-name cmd unread backward point))))))))
7617
7618 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7619   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7620                       (?\C-p (gnus-group-prev-unread-group 1))))
7621         (cursor-in-echo-area t)
7622         keve key group ended prompt)
7623     (save-excursion
7624       (set-buffer gnus-group-buffer)
7625       (goto-char start)
7626       (setq group
7627             (if (eq gnus-keep-same-level 'best)
7628                 (gnus-summary-best-group gnus-newsgroup-name)
7629               (gnus-summary-search-group backward gnus-keep-same-level))))
7630     (while (not ended)
7631       (setq prompt
7632             (format
7633              "No more%s articles%s " (if unread " unread" "")
7634              (if (and group
7635                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7636                  (format " (Type %s for %s [%s])"
7637                          (single-key-description cmd)
7638                          (gnus-group-decoded-name group)
7639                          (gnus-group-unread group))
7640                (format " (Type %s to exit %s)"
7641                        (single-key-description cmd)
7642                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7643       ;; Confirm auto selection.
7644       (setq key (car (setq keve (gnus-read-event-char prompt)))
7645             ended t)
7646       (cond
7647        ((assq key keystrokes)
7648         (let ((obuf (current-buffer)))
7649           (switch-to-buffer gnus-group-buffer)
7650           (when group
7651             (gnus-group-jump-to-group group))
7652           (eval (cadr (assq key keystrokes)))
7653           (setq group (gnus-group-group-name))
7654           (switch-to-buffer obuf))
7655         (setq ended nil))
7656        ((equal key cmd)
7657         (if (or (not group)
7658                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7659             (gnus-summary-exit)
7660           (gnus-summary-next-group nil group backward)))
7661        (t
7662         (push (cdr keve) unread-command-events))))))
7663
7664 (defun gnus-summary-next-unread-article ()
7665   "Select unread article after current one."
7666   (interactive)
7667   (gnus-summary-next-article
7668    (or (not (eq gnus-summary-goto-unread 'never))
7669        (gnus-summary-last-article-p (gnus-summary-article-number)))
7670    (and gnus-auto-select-same
7671         (gnus-summary-article-subject))))
7672
7673 (defun gnus-summary-prev-article (&optional unread subject)
7674   "Select the article before the current one.
7675 If UNREAD is non-nil, only unread articles are selected."
7676   (interactive "P")
7677   (gnus-summary-next-article unread subject t))
7678
7679 (defun gnus-summary-prev-unread-article ()
7680   "Select unread article before current one."
7681   (interactive)
7682   (gnus-summary-prev-article
7683    (or (not (eq gnus-summary-goto-unread 'never))
7684        (gnus-summary-first-article-p (gnus-summary-article-number)))
7685    (and gnus-auto-select-same
7686         (gnus-summary-article-subject))))
7687
7688 (defun gnus-summary-next-page (&optional lines circular stop)
7689   "Show next page of the selected article.
7690 If at the end of the current article, select the next article.
7691 LINES says how many lines should be scrolled up.
7692
7693 If CIRCULAR is non-nil, go to the start of the article instead of
7694 selecting the next article when reaching the end of the current
7695 article.
7696
7697 If STOP is non-nil, just stop when reaching the end of the message.
7698
7699 Also see the variable `gnus-article-skip-boring'."
7700   (interactive "P")
7701   (setq gnus-summary-buffer (current-buffer))
7702   (gnus-set-global-variables)
7703   (let ((article (gnus-summary-article-number))
7704         (article-window (get-buffer-window gnus-article-buffer t))
7705         endp)
7706     ;; If the buffer is empty, we have no article.
7707     (unless article
7708       (error "No article to select"))
7709     (gnus-configure-windows 'article)
7710     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7711         (if (and (eq gnus-summary-goto-unread 'never)
7712                  (not (gnus-summary-last-article-p article)))
7713             (gnus-summary-next-article)
7714           (gnus-summary-next-unread-article))
7715       (if (or (null gnus-current-article)
7716               (null gnus-article-current)
7717               (/= article (cdr gnus-article-current))
7718               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7719           ;; Selected subject is different from current article's.
7720           (gnus-summary-display-article article)
7721         (when article-window
7722           (gnus-eval-in-buffer-window gnus-article-buffer
7723             (setq endp (or (gnus-article-next-page lines)
7724                            (gnus-article-only-boring-p))))
7725           (when endp
7726             (cond (stop
7727                    (gnus-message 3 "End of message"))
7728                   (circular
7729                    (gnus-summary-beginning-of-article))
7730                   (lines
7731                    (gnus-message 3 "End of message"))
7732                   ((null lines)
7733                    (if (and (eq gnus-summary-goto-unread 'never)
7734                             (not (gnus-summary-last-article-p article)))
7735                        (gnus-summary-next-article)
7736                      (gnus-summary-next-unread-article))))))))
7737     (gnus-summary-recenter)
7738     (gnus-summary-position-point)))
7739
7740 (defun gnus-summary-prev-page (&optional lines move)
7741   "Show previous page of selected article.
7742 Argument LINES specifies lines to be scrolled down.
7743 If MOVE, move to the previous unread article if point is at
7744 the beginning of the buffer."
7745   (interactive "P")
7746   (let ((article (gnus-summary-article-number))
7747         (article-window (get-buffer-window gnus-article-buffer t))
7748         endp)
7749     (gnus-configure-windows 'article)
7750     (if (or (null gnus-current-article)
7751             (null gnus-article-current)
7752             (/= article (cdr gnus-article-current))
7753             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7754         ;; Selected subject is different from current article's.
7755         (gnus-summary-display-article article)
7756       (gnus-summary-recenter)
7757       (when article-window
7758         (gnus-eval-in-buffer-window gnus-article-buffer
7759           (setq endp (gnus-article-prev-page lines)))
7760         (when (and move endp)
7761           (cond (lines
7762                  (gnus-message 3 "Beginning of message"))
7763                 ((null lines)
7764                  (if (and (eq gnus-summary-goto-unread 'never)
7765                           (not (gnus-summary-first-article-p article)))
7766                      (gnus-summary-prev-article)
7767                    (gnus-summary-prev-unread-article))))))))
7768   (gnus-summary-position-point))
7769
7770 (defun gnus-summary-prev-page-or-article (&optional lines)
7771   "Show previous page of selected article.
7772 Argument LINES specifies lines to be scrolled down.
7773 If at the beginning of the article, go to the next article."
7774   (interactive "P")
7775   (gnus-summary-prev-page lines t))
7776
7777 (defun gnus-summary-scroll-up (lines)
7778   "Scroll up (or down) one line current article.
7779 Argument LINES specifies lines to be scrolled up (or down if negative)."
7780   (interactive "p")
7781   (gnus-configure-windows 'article)
7782   (gnus-summary-show-thread)
7783   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7784     (gnus-eval-in-buffer-window gnus-article-buffer
7785       (cond ((> lines 0)
7786              (when (gnus-article-next-page lines)
7787                (gnus-message 3 "End of message")))
7788             ((< lines 0)
7789              (gnus-article-prev-page (- lines))))))
7790   (gnus-summary-recenter)
7791   (gnus-summary-position-point))
7792
7793 (defun gnus-summary-scroll-down (lines)
7794   "Scroll down (or up) one line current article.
7795 Argument LINES specifies lines to be scrolled down (or up if negative)."
7796   (interactive "p")
7797   (gnus-summary-scroll-up (- lines)))
7798
7799 (defun gnus-summary-next-same-subject ()
7800   "Select next article which has the same subject as current one."
7801   (interactive)
7802   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7803
7804 (defun gnus-summary-prev-same-subject ()
7805   "Select previous article which has the same subject as current one."
7806   (interactive)
7807   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7808
7809 (defun gnus-summary-next-unread-same-subject ()
7810   "Select next unread article which has the same subject as current one."
7811   (interactive)
7812   (gnus-summary-next-article t (gnus-summary-article-subject)))
7813
7814 (defun gnus-summary-prev-unread-same-subject ()
7815   "Select previous unread article which has the same subject as current one."
7816   (interactive)
7817   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7818
7819 (defun gnus-summary-first-unread-article ()
7820   "Select the first unread article.
7821 Return nil if there are no unread articles."
7822   (interactive)
7823   (prog1
7824       (when (gnus-summary-first-subject t)
7825         (gnus-summary-show-thread)
7826         (gnus-summary-first-subject t)
7827         (gnus-summary-display-article (gnus-summary-article-number)))
7828     (gnus-summary-position-point)))
7829
7830 (defun gnus-summary-first-unread-subject ()
7831   "Place the point on the subject line of the first unread article.
7832 Return nil if there are no unread articles."
7833   (interactive)
7834   (prog1
7835       (when (gnus-summary-first-subject t)
7836         (gnus-summary-show-thread)
7837         (gnus-summary-first-subject t))
7838     (gnus-summary-position-point)))
7839
7840 (defun gnus-summary-first-unseen-subject ()
7841   "Place the point on the subject line of the first unseen article.
7842 Return nil if there are no unseen articles."
7843   (interactive)
7844   (prog1
7845       (when (gnus-summary-first-subject nil nil t)
7846         (gnus-summary-show-thread)
7847         (gnus-summary-first-subject nil nil t))
7848     (gnus-summary-position-point)))
7849
7850 (defun gnus-summary-first-unseen-or-unread-subject ()
7851   "Place the point on the subject line of the first unseen article or,
7852 if all article have been seen, on the subject line of the first unread
7853 article."
7854   (interactive)
7855   (prog1
7856       (unless (when (gnus-summary-first-subject nil nil t)
7857                 (gnus-summary-show-thread)
7858                 (gnus-summary-first-subject nil nil t))
7859         (when (gnus-summary-first-subject t)
7860           (gnus-summary-show-thread)
7861           (gnus-summary-first-subject t)))
7862     (gnus-summary-position-point)))
7863
7864 (defun gnus-summary-first-article ()
7865   "Select the first article.
7866 Return nil if there are no articles."
7867   (interactive)
7868   (prog1
7869       (when (gnus-summary-first-subject)
7870         (gnus-summary-show-thread)
7871         (gnus-summary-first-subject)
7872         (gnus-summary-display-article (gnus-summary-article-number)))
7873     (gnus-summary-position-point)))
7874
7875 (defun gnus-summary-best-unread-article (&optional arg)
7876   "Select the unread article with the highest score.
7877 If given a prefix argument, select the next unread article that has a
7878 score higher than the default score."
7879   (interactive "P")
7880   (let ((article (if arg
7881                      (gnus-summary-better-unread-subject)
7882                    (gnus-summary-best-unread-subject))))
7883     (if article
7884         (gnus-summary-goto-article article)
7885       (error "No unread articles"))))
7886
7887 (defun gnus-summary-best-unread-subject ()
7888   "Select the unread subject with the highest score."
7889   (interactive)
7890   (let ((best -1000000)
7891         (data gnus-newsgroup-data)
7892         article score)
7893     (while data
7894       (and (gnus-data-unread-p (car data))
7895            (> (setq score
7896                     (gnus-summary-article-score (gnus-data-number (car data))))
7897               best)
7898            (setq best score
7899                  article (gnus-data-number (car data))))
7900       (setq data (cdr data)))
7901     (when article
7902       (gnus-summary-goto-subject article))
7903     (gnus-summary-position-point)
7904     article))
7905
7906 (defun gnus-summary-better-unread-subject ()
7907   "Select the first unread subject that has a score over the default score."
7908   (interactive)
7909   (let ((data gnus-newsgroup-data)
7910         article score)
7911     (while (and (setq article (gnus-data-number (car data)))
7912                 (or (gnus-data-read-p (car data))
7913                     (not (> (gnus-summary-article-score article)
7914                             gnus-summary-default-score))))
7915       (setq data (cdr data)))
7916     (when article
7917       (gnus-summary-goto-subject article))
7918     (gnus-summary-position-point)
7919     article))
7920
7921 (defun gnus-summary-last-subject ()
7922   "Go to the last displayed subject line in the group."
7923   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7924     (when article
7925       (gnus-summary-goto-subject article))))
7926
7927 (defun gnus-summary-goto-article (article &optional all-headers force)
7928   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7929 If ALL-HEADERS is non-nil, no header lines are hidden.
7930 If FORCE, go to the article even if it isn't displayed.  If FORCE
7931 is a number, it is the line the article is to be displayed on."
7932   (interactive
7933    (list
7934     (completing-read
7935      "Article number or Message-ID: "
7936      (mapcar (lambda (number) (list (int-to-string number)))
7937              gnus-newsgroup-limit))
7938     current-prefix-arg
7939     t))
7940   (prog1
7941       (if (and (stringp article)
7942                (string-match "@\\|%40" article))
7943           (gnus-summary-refer-article article)
7944         (when (stringp article)
7945           (setq article (string-to-number article)))
7946         (if (gnus-summary-goto-subject article force)
7947             (gnus-summary-display-article article all-headers)
7948           (gnus-message 4 "Couldn't go to article %s" article) nil))
7949     (gnus-summary-position-point)))
7950
7951 (defun gnus-summary-goto-last-article ()
7952   "Go to the previously read article."
7953   (interactive)
7954   (prog1
7955       (when gnus-last-article
7956         (gnus-summary-goto-article gnus-last-article nil t))
7957     (gnus-summary-position-point)))
7958
7959 (defun gnus-summary-pop-article (number)
7960   "Pop one article off the history and go to the previous.
7961 NUMBER articles will be popped off."
7962   (interactive "p")
7963   (let (to)
7964     (setq gnus-newsgroup-history
7965           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7966     (if to
7967         (gnus-summary-goto-article (car to) nil t)
7968       (error "Article history empty")))
7969   (gnus-summary-position-point))
7970
7971 ;; Summary commands and functions for limiting the summary buffer.
7972
7973 (defun gnus-summary-limit-to-articles (n)
7974   "Limit the summary buffer to the next N articles.
7975 If not given a prefix, use the process marked articles instead."
7976   (interactive "P")
7977   (prog1
7978       (let ((articles (gnus-summary-work-articles n)))
7979         (setq gnus-newsgroup-processable nil)
7980         (gnus-summary-limit articles))
7981     (gnus-summary-position-point)))
7982
7983 (defun gnus-summary-pop-limit (&optional total)
7984   "Restore the previous limit.
7985 If given a prefix, remove all limits."
7986   (interactive "P")
7987   (when total
7988     (setq gnus-newsgroup-limits
7989           (list (mapcar (lambda (h) (mail-header-number h))
7990                         gnus-newsgroup-headers))))
7991   (unless gnus-newsgroup-limits
7992     (error "No limit to pop"))
7993   (prog1
7994       (gnus-summary-limit nil 'pop)
7995     (gnus-summary-position-point)))
7996
7997 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7998   "Limit the summary buffer to articles that have subjects that match a regexp.
7999 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8000   (interactive
8001    (list (read-string (if current-prefix-arg
8002                           "Exclude subject (regexp): "
8003                         "Limit to subject (regexp): "))
8004          nil current-prefix-arg))
8005   (unless header
8006     (setq header "subject"))
8007   (when (not (equal "" subject))
8008     (prog1
8009         (let ((articles (gnus-summary-find-matching
8010                          (or header "subject") subject 'all nil nil
8011                          not-matching)))
8012           (unless articles
8013             (error "Found no matches for \"%s\"" subject))
8014           (gnus-summary-limit articles))
8015       (gnus-summary-position-point))))
8016
8017 (defun gnus-summary-limit-to-author (from &optional not-matching)
8018   "Limit the summary buffer to articles that have authors that match a regexp.
8019 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8020   (interactive
8021    (list (read-string (if current-prefix-arg
8022                           "Exclude author (regexp): "
8023                         "Limit to author (regexp): "))
8024          current-prefix-arg))
8025   (gnus-summary-limit-to-subject from "from" not-matching))
8026
8027 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8028   "Limit the summary buffer to articles with the given RECIPIENT.
8029
8030 If NOT-MATCHING, exclude RECIPIENT.
8031
8032 To and Cc headers are checked.  You need to include them in
8033 `nnmail-extra-headers'."
8034   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8035   (interactive
8036    (list (read-string (format "%s recipient (regexp): "
8037                               (if current-prefix-arg "Exclude" "Limit to")))
8038          current-prefix-arg))
8039   (when (not (equal "" recipient))
8040     (prog1 (let* ((to
8041                    (if (memq 'To nnmail-extra-headers)
8042                        (gnus-summary-find-matching
8043                         (cons 'extra 'To) recipient 'all nil nil
8044                         not-matching)
8045                      (gnus-message
8046                       1 "`To' isn't present in `nnmail-extra-headers'")
8047                      (sit-for 1)
8048                      nil))
8049                   (cc
8050                    (if (memq 'Cc nnmail-extra-headers)
8051                        (gnus-summary-find-matching
8052                         (cons 'extra 'Cc) recipient 'all nil nil
8053                         not-matching)
8054                      (gnus-message
8055                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8056                      (sit-for 1)
8057                      nil))
8058                   (articles
8059                    (if not-matching
8060                        ;; We need the numbers that are in both lists:
8061                        (mapcar (lambda (a)
8062                                  (and (memq a to) a))
8063                                cc)
8064                      (nconc to cc))))
8065              (unless articles
8066                (error "Found no matches for \"%s\"" recipient))
8067              (gnus-summary-limit articles))
8068       (gnus-summary-position-point))))
8069
8070 (defun gnus-summary-limit-to-address (address &optional not-matching)
8071   "Limit the summary buffer to articles with the given ADDRESS.
8072
8073 If NOT-MATCHING, exclude ADDRESS.
8074
8075 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8076 in `nnmail-extra-headers'."
8077   (interactive
8078    (list (read-string (format "%s address (regexp): "
8079                               (if current-prefix-arg "Exclude" "Limit to")))
8080          current-prefix-arg))
8081   (when (not (equal "" address))
8082     (prog1 (let* ((to
8083                    (if (memq 'To nnmail-extra-headers)
8084                        (gnus-summary-find-matching
8085                         (cons 'extra 'To) address 'all nil nil
8086                         not-matching)
8087                      (gnus-message
8088                       1 "`To' isn't present in `nnmail-extra-headers'")
8089                      (sit-for 1)
8090                      t))
8091                   (cc
8092                    (if (memq 'Cc nnmail-extra-headers)
8093                        (gnus-summary-find-matching
8094                         (cons 'extra 'Cc) address 'all nil nil
8095                         not-matching)
8096                      (gnus-message
8097                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8098                      (sit-for 1)
8099                      t))
8100                   (from
8101                    (gnus-summary-find-matching "from" address
8102                                                'all nil nil not-matching))
8103                   (articles
8104                    (if not-matching
8105                        ;; We need the numbers that are in all lists:
8106                        (if (eq cc t)
8107                            (if (eq to t)
8108                                from
8109                              (mapcar (lambda (a) (car (memq a from))) to))
8110                          (if (eq to t)
8111                              (mapcar (lambda (a) (car (memq a from))) cc)
8112                            (mapcar (lambda (a) (car (memq a from)))
8113                                    (mapcar (lambda (a) (car (memq a to)))
8114                                            cc))))
8115                      (nconc (if (eq to t) nil to)
8116                             (if (eq cc t) nil cc)
8117                             from))))
8118              (unless articles
8119                (error "Found no matches for \"%s\"" address))
8120              (gnus-summary-limit articles))
8121       (gnus-summary-position-point))))
8122
8123 (defun gnus-summary-limit-strange-charsets-predicate (header)
8124   (let ((string (concat (mail-header-subject header)
8125                         (mail-header-from header)))
8126         charset found)
8127     (dotimes (i (1- (length string)))
8128       (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8129       (when (string-match "unicode\\|big\\|japanese" charset)
8130         (setq found t)))
8131     found))
8132
8133 (defun gnus-summary-limit-to-predicate (predicate)
8134   "Limit to articles where PREDICATE returns non-nil.
8135 PREDICATE will be called with the header structures of the
8136 articles."
8137   (let ((articles nil)
8138         (case-fold-search t))
8139     (dolist (header gnus-newsgroup-headers)
8140       (when (funcall predicate header)
8141         (push (mail-header-number header) articles)))
8142     (gnus-summary-limit (nreverse articles))))
8143
8144 (defun gnus-summary-limit-to-age (age &optional younger-p)
8145   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8146 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8147 articles that are younger than AGE days."
8148   (interactive
8149    (let ((younger current-prefix-arg)
8150          (days-got nil)
8151          days)
8152      (while (not days-got)
8153        (setq days (if younger
8154                       (read-string "Limit to articles younger than (in days, older when negative): ")
8155                     (read-string
8156                      "Limit to articles older than (in days, younger when negative): ")))
8157        (when (> (length days) 0)
8158          (setq days (read days)))
8159        (if (numberp days)
8160            (progn
8161              (setq days-got t)
8162              (when (< days 0)
8163                (setq younger (not younger))
8164                (setq days (* days -1))))
8165          (message "Please enter a number.")
8166          (sleep-for 1)))
8167      (list days younger)))
8168   (prog1
8169       (let ((data gnus-newsgroup-data)
8170             (cutoff (days-to-time age))
8171             articles d date is-younger)
8172         (while (setq d (pop data))
8173           (when (and (vectorp (gnus-data-header d))
8174                      (setq date (mail-header-date (gnus-data-header d))))
8175             (setq is-younger (time-less-p
8176                               (time-since (condition-case ()
8177                                               (date-to-time date)
8178                                             (error '(0 0))))
8179                               cutoff))
8180             (when (if younger-p
8181                       is-younger
8182                     (not is-younger))
8183               (push (gnus-data-number d) articles))))
8184         (gnus-summary-limit (nreverse articles)))
8185     (gnus-summary-position-point)))
8186
8187 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8188   "Limit the summary buffer to articles that match an 'extra' header."
8189   (interactive
8190    (let ((header
8191           (intern
8192            (gnus-completing-read-with-default
8193             (symbol-name (car gnus-extra-headers))
8194             (if current-prefix-arg
8195                 "Exclude extra header"
8196               "Limit extra header")
8197             (mapcar (lambda (x)
8198                       (cons (symbol-name x) x))
8199                     gnus-extra-headers)
8200             nil
8201             t))))
8202      (list header
8203            (read-string (format "%s header %s (regexp): "
8204                                 (if current-prefix-arg "Exclude" "Limit to")
8205                                 header))
8206            current-prefix-arg)))
8207   (when (not (equal "" regexp))
8208     (prog1
8209         (let ((articles (gnus-summary-find-matching
8210                          (cons 'extra header) regexp 'all nil nil
8211                          not-matching)))
8212           (unless articles
8213             (error "Found no matches for \"%s\"" regexp))
8214           (gnus-summary-limit articles))
8215       (gnus-summary-position-point))))
8216
8217 (defun gnus-summary-limit-to-display-predicate ()
8218   "Limit the summary buffer to the predicated in the `display' group parameter."
8219   (interactive)
8220   (unless gnus-newsgroup-display
8221     (error "There is no `display' group parameter"))
8222   (let (articles)
8223     (dolist (number gnus-newsgroup-articles)
8224       (when (funcall gnus-newsgroup-display)
8225         (push number articles)))
8226     (gnus-summary-limit articles))
8227   (gnus-summary-position-point))
8228
8229 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8230 (make-obsolete
8231  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8232
8233 (defun gnus-summary-limit-to-unread (&optional all)
8234   "Limit the summary buffer to articles that are not marked as read.
8235 If ALL is non-nil, limit strictly to unread articles."
8236   (interactive "P")
8237   (if all
8238       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8239     (gnus-summary-limit-to-marks
8240      ;; Concat all the marks that say that an article is read and have
8241      ;; those removed.
8242      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8243            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8244            gnus-low-score-mark gnus-expirable-mark
8245            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8246            gnus-duplicate-mark gnus-souped-mark)
8247      'reverse)))
8248
8249 (defun gnus-summary-limit-to-headers (match &optional reverse)
8250   "Limit the summary buffer to articles that have headers that match MATCH.
8251 If REVERSE (the prefix), limit to articles that don't match."
8252   (interactive "sMatch headers (regexp): \nP")
8253   (gnus-summary-limit-to-bodies match reverse t))
8254
8255 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8256   "Limit the summary buffer to articles that have bodies that match MATCH.
8257 If REVERSE (the prefix), limit to articles that don't match."
8258   (interactive "sMatch body (regexp): \nP")
8259   (let ((articles nil)
8260         (gnus-select-article-hook nil)  ;Disable hook.
8261         (gnus-article-prepare-hook nil)
8262         (gnus-use-article-prefetch nil)
8263         (gnus-keep-backlog nil)
8264         (gnus-break-pages nil)
8265         (gnus-summary-display-arrow nil)
8266         (gnus-updated-mode-lines nil)
8267         (gnus-auto-center-summary nil)
8268         (gnus-display-mime-function nil))
8269     (dolist (data gnus-newsgroup-data)
8270       (let (gnus-mark-article-hook)
8271         (gnus-summary-select-article t t nil (gnus-data-number data)))
8272       (save-excursion
8273         (set-buffer gnus-article-buffer)
8274         (article-goto-body)
8275         (let* ((case-fold-search t)
8276                (found (if headersp
8277                           (re-search-backward match nil t)
8278                         (re-search-forward match nil t))))
8279           (when (or (and found
8280                          (not reverse))
8281                     (and (not found)
8282                          reverse))
8283             (push (gnus-data-number data) articles)))))
8284     (if (not articles)
8285         (message "No messages matched")
8286       (gnus-summary-limit articles)))
8287   (gnus-summary-position-point))
8288
8289 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8290   "Limit the summary buffer to articles that aren't part on any thread.
8291 If THREADSP (the prefix), limit to articles that are in threads."
8292   (interactive "P")
8293   (let ((articles nil)
8294         thread-articles
8295         threads)
8296     (dolist (thread gnus-newsgroup-threads)
8297       (if (stringp (car thread))
8298           (dolist (thread (cdr thread))
8299             (push thread threads))
8300         (push thread threads)))
8301     (dolist (thread threads)
8302       (setq thread-articles (gnus-articles-in-thread thread))
8303       (when (or (and threadsp
8304                      (> (length thread-articles) 1))
8305                 (and (not threadsp)
8306                      (= (length thread-articles) 1)))
8307         (setq articles (nconc thread-articles articles))))
8308     (if (not articles)
8309         (message "No messages matched")
8310       (gnus-summary-limit articles))
8311     (gnus-summary-position-point)))
8312
8313 (defun gnus-summary-limit-to-replied (&optional unreplied)
8314   "Limit the summary buffer to replied articles.
8315 If UNREPLIED (the prefix), limit to unreplied articles."
8316   (interactive "P")
8317   (if unreplied
8318       (gnus-summary-limit
8319        (gnus-set-difference gnus-newsgroup-articles
8320         gnus-newsgroup-replied))
8321     (gnus-summary-limit gnus-newsgroup-replied))
8322   (gnus-summary-position-point))
8323
8324 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8325 (make-obsolete 'gnus-summary-delete-marked-with
8326                'gnus-summary-limit-exclude-marks)
8327
8328 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8329   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8330 If REVERSE, limit the summary buffer to articles that are marked
8331 with MARKS.  MARKS can either be a string of marks or a list of marks.
8332 Returns how many articles were removed."
8333   (interactive "sMarks: ")
8334   (gnus-summary-limit-to-marks marks t))
8335
8336 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8337   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8338 If REVERSE (the prefix), limit the summary buffer to articles that are
8339 not marked with MARKS.  MARKS can either be a string of marks or a
8340 list of marks.
8341 Returns how many articles were removed."
8342   (interactive "sMarks: \nP")
8343   (prog1
8344       (let ((data gnus-newsgroup-data)
8345             (marks (if (listp marks) marks
8346                      (append marks nil))) ; Transform to list.
8347             articles)
8348         (while data
8349           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8350                   (memq (gnus-data-mark (car data)) marks))
8351             (push (gnus-data-number (car data)) articles))
8352           (setq data (cdr data)))
8353         (gnus-summary-limit articles))
8354     (gnus-summary-position-point)))
8355
8356 (defun gnus-summary-limit-to-score (score)
8357   "Limit to articles with score at or above SCORE."
8358   (interactive "NLimit to articles with score of at least: ")
8359   (let ((data gnus-newsgroup-data)
8360         articles)
8361     (while data
8362       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8363                 score)
8364         (push (gnus-data-number (car data)) articles))
8365       (setq data (cdr data)))
8366     (prog1
8367         (gnus-summary-limit articles)
8368       (gnus-summary-position-point))))
8369
8370 (defun gnus-summary-limit-to-unseen ()
8371   "Limit to unseen articles."
8372   (interactive)
8373   (prog1
8374       (gnus-summary-limit gnus-newsgroup-unseen)
8375     (gnus-summary-position-point)))
8376
8377 (defun gnus-summary-limit-include-thread (id)
8378   "Display all the hidden articles that is in the thread with ID in it.
8379 When called interactively, ID is the Message-ID of the current
8380 article."
8381   (interactive (list (mail-header-id (gnus-summary-article-header))))
8382   (let ((articles (gnus-articles-in-thread
8383                    (gnus-id-to-thread (gnus-root-id id)))))
8384     (prog1
8385         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8386       (gnus-summary-limit-include-matching-articles
8387        "subject"
8388        (regexp-quote (gnus-simplify-subject-re
8389                       (mail-header-subject (gnus-id-to-header id)))))
8390       (gnus-summary-position-point))))
8391
8392 (defun gnus-summary-limit-include-matching-articles (header regexp)
8393   "Display all the hidden articles that have HEADERs that match REGEXP."
8394   (interactive (list (read-string "Match on header: ")
8395                      (read-string "Regexp: ")))
8396   (let ((articles (gnus-find-matching-articles header regexp)))
8397     (prog1
8398         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8399       (gnus-summary-position-point))))
8400
8401 (defun gnus-summary-insert-dormant-articles ()
8402   "Insert all the dormant articles for this group into the current buffer."
8403   (interactive)
8404   (let ((gnus-verbose (max 6 gnus-verbose)))
8405     (if (not gnus-newsgroup-dormant)
8406         (gnus-message 3 "No dormant articles for this group")
8407       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8408
8409 (defun gnus-summary-insert-ticked-articles ()
8410   "Insert ticked articles for this group into the current buffer."
8411   (interactive)
8412   (let ((gnus-verbose (max 6 gnus-verbose)))
8413     (if (not gnus-newsgroup-marked)
8414         (gnus-message 3 "No ticked articles for this group")
8415       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8416
8417 (defun gnus-summary-limit-include-dormant ()
8418   "Display all the hidden articles that are marked as dormant.
8419 Note that this command only works on a subset of the articles currently
8420 fetched for this group."
8421   (interactive)
8422   (unless gnus-newsgroup-dormant
8423     (error "There are no dormant articles in this group"))
8424   (prog1
8425       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8426     (gnus-summary-position-point)))
8427
8428 (defun gnus-summary-limit-exclude-dormant ()
8429   "Hide all dormant articles."
8430   (interactive)
8431   (prog1
8432       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8433     (gnus-summary-position-point)))
8434
8435 (defun gnus-summary-limit-exclude-childless-dormant ()
8436   "Hide all dormant articles that have no children."
8437   (interactive)
8438   (let ((data (gnus-data-list t))
8439         articles d children)
8440     ;; Find all articles that are either not dormant or have
8441     ;; children.
8442     (while (setq d (pop data))
8443       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8444                 (and (setq children
8445                            (gnus-article-children (gnus-data-number d)))
8446                      (let (found)
8447                        (while children
8448                          (when (memq (car children) articles)
8449                            (setq children nil
8450                                  found t))
8451                          (pop children))
8452                        found)))
8453         (push (gnus-data-number d) articles)))
8454     ;; Do the limiting.
8455     (prog1
8456         (gnus-summary-limit articles)
8457       (gnus-summary-position-point))))
8458
8459 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8460   "Mark all unread excluded articles as read.
8461 If ALL, mark even excluded ticked and dormants as read."
8462   (interactive "P")
8463   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8464   (let ((articles (gnus-sorted-ndifference
8465                    (sort
8466                     (mapcar (lambda (h) (mail-header-number h))
8467                             gnus-newsgroup-headers)
8468                     '<)
8469                    gnus-newsgroup-limit))
8470         article)
8471     (setq gnus-newsgroup-unreads
8472           (gnus-sorted-intersection gnus-newsgroup-unreads
8473                                     gnus-newsgroup-limit))
8474     (if all
8475         (setq gnus-newsgroup-dormant nil
8476               gnus-newsgroup-marked nil
8477               gnus-newsgroup-reads
8478               (nconc
8479                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8480                gnus-newsgroup-reads))
8481       (while (setq article (pop articles))
8482         (unless (or (memq article gnus-newsgroup-dormant)
8483                     (memq article gnus-newsgroup-marked))
8484           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8485
8486 (defun gnus-summary-limit (articles &optional pop)
8487   (if pop
8488       ;; We pop the previous limit off the stack and use that.
8489       (setq articles (car gnus-newsgroup-limits)
8490             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8491     ;; We use the new limit, so we push the old limit on the stack.
8492     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8493   ;; Set the limit.
8494   (setq gnus-newsgroup-limit articles)
8495   (let ((total (length gnus-newsgroup-data))
8496         (data (gnus-data-find-list (gnus-summary-article-number)))
8497         (gnus-summary-mark-below nil)   ; Inhibit this.
8498         found)
8499     ;; This will do all the work of generating the new summary buffer
8500     ;; according to the new limit.
8501     (gnus-summary-prepare)
8502     ;; Hide any threads, possibly.
8503     (gnus-summary-maybe-hide-threads)
8504     ;; Try to return to the article you were at, or one in the
8505     ;; neighborhood.
8506     (when data
8507       ;; We try to find some article after the current one.
8508       (while data
8509         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8510           (setq data nil
8511                 found t))
8512         (setq data (cdr data))))
8513     (unless found
8514       ;; If there is no data, that means that we were after the last
8515       ;; article.  The same goes when we can't find any articles
8516       ;; after the current one.
8517       (goto-char (point-max))
8518       (gnus-summary-find-prev))
8519     (gnus-set-mode-line 'summary)
8520     ;; We return how many articles were removed from the summary
8521     ;; buffer as a result of the new limit.
8522     (- total (length gnus-newsgroup-data))))
8523
8524 (defsubst gnus-invisible-cut-children (threads)
8525   (let ((num 0))
8526     (while threads
8527       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8528         (incf num))
8529       (pop threads))
8530     (< num 2)))
8531
8532 (defsubst gnus-cut-thread (thread)
8533   "Go forwards in the thread until we find an article that we want to display."
8534   (when (or (eq gnus-fetch-old-headers 'some)
8535             (eq gnus-fetch-old-headers 'invisible)
8536             (numberp gnus-fetch-old-headers)
8537             (eq gnus-build-sparse-threads 'some)
8538             (eq gnus-build-sparse-threads 'more))
8539     ;; Deal with old-fetched headers and sparse threads.
8540     (while (and
8541             thread
8542             (or
8543              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8544              (gnus-summary-article-ancient-p
8545               (mail-header-number (car thread))))
8546             (if (or (<= (length (cdr thread)) 1)
8547                     (eq gnus-fetch-old-headers 'invisible))
8548                 (setq gnus-newsgroup-limit
8549                       (delq (mail-header-number (car thread))
8550                             gnus-newsgroup-limit)
8551                       thread (cadr thread))
8552               (when (gnus-invisible-cut-children (cdr thread))
8553                 (let ((th (cdr thread)))
8554                   (while th
8555                     (if (memq (mail-header-number (caar th))
8556                               gnus-newsgroup-limit)
8557                         (setq thread (car th)
8558                               th nil)
8559                       (setq th (cdr th))))))))))
8560   thread)
8561
8562 (defun gnus-cut-threads (threads)
8563   "Cut off all uninteresting articles from the beginning of THREADS."
8564   (when (or (eq gnus-fetch-old-headers 'some)
8565             (eq gnus-fetch-old-headers 'invisible)
8566             (numberp gnus-fetch-old-headers)
8567             (eq gnus-build-sparse-threads 'some)
8568             (eq gnus-build-sparse-threads 'more))
8569     (let ((th threads))
8570       (while th
8571         (setcar th (gnus-cut-thread (car th)))
8572         (setq th (cdr th)))))
8573   ;; Remove nixed out threads.
8574   (delq nil threads))
8575
8576 (defun gnus-summary-initial-limit (&optional show-if-empty)
8577   "Figure out what the initial limit is supposed to be on group entry.
8578 This entails weeding out unwanted dormants, low-scored articles,
8579 fetch-old-headers verbiage, and so on."
8580   ;; Most groups have nothing to remove.
8581   (if (or gnus-inhibit-limiting
8582           (and (null gnus-newsgroup-dormant)
8583                (eq gnus-newsgroup-display 'gnus-not-ignore)
8584                (not (eq gnus-fetch-old-headers 'some))
8585                (not (numberp gnus-fetch-old-headers))
8586                (not (eq gnus-fetch-old-headers 'invisible))
8587                (null gnus-summary-expunge-below)
8588                (not (eq gnus-build-sparse-threads 'some))
8589                (not (eq gnus-build-sparse-threads 'more))
8590                (null gnus-thread-expunge-below)
8591                (not gnus-use-nocem)))
8592       ()                                ; Do nothing.
8593     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8594     (setq gnus-newsgroup-limit nil)
8595     (mapatoms
8596      (lambda (node)
8597        (unless (car (symbol-value node))
8598          ;; These threads have no parents -- they are roots.
8599          (let ((nodes (cdr (symbol-value node)))
8600                thread)
8601            (while nodes
8602              (if (and gnus-thread-expunge-below
8603                       (< (gnus-thread-total-score (car nodes))
8604                          gnus-thread-expunge-below))
8605                  (gnus-expunge-thread (pop nodes))
8606                (setq thread (pop nodes))
8607                (gnus-summary-limit-children thread))))))
8608      gnus-newsgroup-dependencies)
8609     ;; If this limitation resulted in an empty group, we might
8610     ;; pop the previous limit and use it instead.
8611     (when (and (not gnus-newsgroup-limit)
8612                show-if-empty)
8613       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8614     gnus-newsgroup-limit))
8615
8616 (defun gnus-summary-limit-children (thread)
8617   "Return 1 if this subthread is visible and 0 if it is not."
8618   ;; First we get the number of visible children to this thread.  This
8619   ;; is done by recursing down the thread using this function, so this
8620   ;; will really go down to a leaf article first, before slowly
8621   ;; working its way up towards the root.
8622   (when thread
8623     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8624            (children
8625            (if (cdr thread)
8626                (apply '+ (mapcar 'gnus-summary-limit-children
8627                                  (cdr thread)))
8628              0))
8629           (number (mail-header-number (car thread)))
8630           score)
8631       (if (and
8632            (not (memq number gnus-newsgroup-marked))
8633            (or
8634             ;; If this article is dormant and has absolutely no visible
8635             ;; children, then this article isn't visible.
8636             (and (memq number gnus-newsgroup-dormant)
8637                  (zerop children))
8638             ;; If this is "fetch-old-headered" and there is no
8639             ;; visible children, then we don't want this article.
8640             (and (or (eq gnus-fetch-old-headers 'some)
8641                      (numberp gnus-fetch-old-headers))
8642                  (gnus-summary-article-ancient-p number)
8643                  (zerop children))
8644             ;; If this is "fetch-old-headered" and `invisible', then
8645             ;; we don't want this article.
8646             (and (eq gnus-fetch-old-headers 'invisible)
8647                  (gnus-summary-article-ancient-p number))
8648             ;; If this is a sparsely inserted article with no children,
8649             ;; we don't want it.
8650             (and (eq gnus-build-sparse-threads 'some)
8651                  (gnus-summary-article-sparse-p number)
8652                  (zerop children))
8653             ;; If we use expunging, and this article is really
8654             ;; low-scored, then we don't want this article.
8655             (when (and gnus-summary-expunge-below
8656                        (< (setq score
8657                                 (or (cdr (assq number gnus-newsgroup-scored))
8658                                     gnus-summary-default-score))
8659                           gnus-summary-expunge-below))
8660               ;; We increase the expunge-tally here, but that has
8661               ;; nothing to do with the limits, really.
8662               (incf gnus-newsgroup-expunged-tally)
8663               ;; We also mark as read here, if that's wanted.
8664               (when (and gnus-summary-mark-below
8665                          (< score gnus-summary-mark-below))
8666                 (setq gnus-newsgroup-unreads
8667                       (delq number gnus-newsgroup-unreads))
8668                 (if gnus-newsgroup-auto-expire
8669                     (push number gnus-newsgroup-expirable)
8670                   (push (cons number gnus-low-score-mark)
8671                         gnus-newsgroup-reads)))
8672               t)
8673             ;; Do the `display' group parameter.
8674             (and gnus-newsgroup-display
8675                  (not (funcall gnus-newsgroup-display)))
8676             ;; Check NoCeM things.
8677             (when (and gnus-use-nocem
8678                        (gnus-nocem-unwanted-article-p
8679                         (mail-header-id (car thread))))
8680               (setq gnus-newsgroup-unreads
8681                     (delq number gnus-newsgroup-unreads))
8682               t)))
8683           ;; Nope, invisible article.
8684           0
8685         ;; Ok, this article is to be visible, so we add it to the limit
8686         ;; and return 1.
8687         (push number gnus-newsgroup-limit)
8688         1))))
8689
8690 (defun gnus-expunge-thread (thread)
8691   "Mark all articles in THREAD as read."
8692   (let* ((number (mail-header-number (car thread))))
8693     (incf gnus-newsgroup-expunged-tally)
8694     ;; We also mark as read here, if that's wanted.
8695     (setq gnus-newsgroup-unreads
8696           (delq number gnus-newsgroup-unreads))
8697     (if gnus-newsgroup-auto-expire
8698         (push number gnus-newsgroup-expirable)
8699       (push (cons number gnus-low-score-mark)
8700             gnus-newsgroup-reads)))
8701   ;; Go recursively through all subthreads.
8702   (mapcar 'gnus-expunge-thread (cdr thread)))
8703
8704 ;; Summary article oriented commands
8705
8706 (defun gnus-summary-refer-parent-article (n)
8707   "Refer parent article N times.
8708 If N is negative, go to ancestor -N instead.
8709 The difference between N and the number of articles fetched is returned."
8710   (interactive "p")
8711   (let ((skip 1)
8712         error header ref)
8713     (when (not (natnump n))
8714       (setq skip (abs n)
8715             n 1))
8716     (while (and (> n 0)
8717                 (not error))
8718       (setq header (gnus-summary-article-header))
8719       (if (and (eq (mail-header-number header)
8720                    (cdr gnus-article-current))
8721                (equal gnus-newsgroup-name
8722                       (car gnus-article-current)))
8723           ;; If we try to find the parent of the currently
8724           ;; displayed article, then we take a look at the actual
8725           ;; References header, since this is slightly more
8726           ;; reliable than the References field we got from the
8727           ;; server.
8728           (save-excursion
8729             (set-buffer gnus-original-article-buffer)
8730             (nnheader-narrow-to-headers)
8731             (unless (setq ref (message-fetch-field "references"))
8732               (when (setq ref (message-fetch-field "in-reply-to"))
8733                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8734             (widen))
8735         (setq ref
8736               ;; It's not the current article, so we take a bet on
8737               ;; the value we got from the server.
8738               (mail-header-references header)))
8739       (if (and ref
8740                (not (equal ref "")))
8741           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8742             (gnus-message 1 "Couldn't find parent"))
8743         (gnus-message 1 "No references in article %d"
8744                       (gnus-summary-article-number))
8745         (setq error t))
8746       (decf n))
8747     (gnus-summary-position-point)
8748     n))
8749
8750 (defun gnus-summary-refer-references ()
8751   "Fetch all articles mentioned in the References header.
8752 Return the number of articles fetched."
8753   (interactive)
8754   (let ((ref (mail-header-references (gnus-summary-article-header)))
8755         (current (gnus-summary-article-number))
8756         (n 0))
8757     (if (or (not ref)
8758             (equal ref ""))
8759         (error "No References in the current article")
8760       ;; For each Message-ID in the References header...
8761       (while (string-match "<[^>]*>" ref)
8762         (incf n)
8763         ;; ... fetch that article.
8764         (gnus-summary-refer-article
8765          (prog1 (match-string 0 ref)
8766            (setq ref (substring ref (match-end 0))))))
8767       (gnus-summary-goto-subject current)
8768       (gnus-summary-position-point)
8769       n)))
8770
8771 (defun gnus-summary-refer-thread (&optional limit)
8772   "Fetch all articles in the current thread.
8773 If LIMIT (the numerical prefix), fetch that many old headers instead
8774 of what's specified by the `gnus-refer-thread-limit' variable."
8775   (interactive "P")
8776   (let ((id (mail-header-id (gnus-summary-article-header)))
8777         (limit (if limit (prefix-numeric-value limit)
8778                  gnus-refer-thread-limit)))
8779     (unless (eq gnus-fetch-old-headers 'invisible)
8780       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8781       ;; Retrieve the headers and read them in.
8782       (if (eq (if (numberp limit)
8783                   (gnus-retrieve-headers
8784                    (list (min
8785                           (+ (mail-header-number
8786                               (gnus-summary-article-header))
8787                              limit)
8788                           gnus-newsgroup-end))
8789                    gnus-newsgroup-name (* limit 2))
8790                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8791                 ;; headers.
8792                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8793                                        gnus-newsgroup-name limit))
8794               'nov)
8795           (gnus-build-all-threads)
8796         (error "Can't fetch thread from back ends that don't support NOV"))
8797       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8798     (gnus-summary-limit-include-thread id)))
8799
8800 (defun gnus-summary-refer-article (message-id)
8801   "Fetch an article specified by MESSAGE-ID."
8802   (interactive "sMessage-ID: ")
8803   (when (and (stringp message-id)
8804              (not (zerop (length message-id))))
8805     (setq message-id (gnus-replace-in-string message-id " " ""))
8806     ;; Construct the correct Message-ID if necessary.
8807     ;; Suggested by tale@pawl.rpi.edu.
8808     (unless (string-match "^<" message-id)
8809       (setq message-id (concat "<" message-id)))
8810     (unless (string-match ">$" message-id)
8811       (setq message-id (concat message-id ">")))
8812     ;; People often post MIDs from URLs, so unhex it:
8813     (unless (string-match "@" message-id)
8814       (setq message-id (gnus-url-unhex-string message-id)))
8815     (let* ((header (gnus-id-to-header message-id))
8816            (sparse (and header
8817                         (gnus-summary-article-sparse-p
8818                          (mail-header-number header))
8819                         (memq (mail-header-number header)
8820                               gnus-newsgroup-limit)))
8821            number)
8822       (cond
8823        ;; If the article is present in the buffer we just go to it.
8824        ((and header
8825              (or (not (gnus-summary-article-sparse-p
8826                        (mail-header-number header)))
8827                  sparse))
8828         (prog1
8829             (gnus-summary-goto-article
8830              (mail-header-number header) nil t)
8831           (when sparse
8832             (gnus-summary-update-article (mail-header-number header)))))
8833        (t
8834         ;; We fetch the article.
8835         (catch 'found
8836           (dolist (gnus-override-method (gnus-refer-article-methods))
8837             (when (and (gnus-check-server gnus-override-method)
8838                        ;; Fetch the header,
8839                        (setq number (gnus-summary-insert-subject message-id)))
8840               ;; and display the article.
8841               (gnus-summary-select-article nil nil nil number)
8842               (throw 'found t)))
8843           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8844
8845 (defun gnus-refer-article-methods ()
8846   "Return a list of referable methods."
8847   (cond
8848    ;; No method, so we default to current and native.
8849    ((null gnus-refer-article-method)
8850     (list gnus-current-select-method gnus-select-method))
8851    ;; Current.
8852    ((eq 'current gnus-refer-article-method)
8853     (list gnus-current-select-method))
8854    ;; List of select methods.
8855    ((not (and (symbolp (car gnus-refer-article-method))
8856               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8857     (let (out)
8858       (dolist (method gnus-refer-article-method)
8859         (push (if (eq 'current method)
8860                   gnus-current-select-method
8861                 method)
8862               out))
8863       (nreverse out)))
8864    ;; One single select method.
8865    (t
8866     (list gnus-refer-article-method))))
8867
8868 (defun gnus-summary-edit-parameters ()
8869   "Edit the group parameters of the current group."
8870   (interactive)
8871   (gnus-group-edit-group gnus-newsgroup-name 'params))
8872
8873 (defun gnus-summary-customize-parameters ()
8874   "Customize the group parameters of the current group."
8875   (interactive)
8876   (gnus-group-customize gnus-newsgroup-name))
8877
8878 (defun gnus-summary-enter-digest-group (&optional force)
8879   "Enter an nndoc group based on the current article.
8880 If FORCE, force a digest interpretation.  If not, try
8881 to guess what the document format is."
8882   (interactive "P")
8883   (let ((conf gnus-current-window-configuration))
8884     (save-window-excursion
8885       (save-excursion
8886         (let (gnus-article-prepare-hook
8887               gnus-display-mime-function
8888               gnus-break-pages)
8889           (gnus-summary-select-article))))
8890     (setq gnus-current-window-configuration conf)
8891     (let* ((name (format "%s-%d"
8892                          (gnus-group-prefixed-name
8893                           gnus-newsgroup-name (list 'nndoc ""))
8894                          (with-current-buffer gnus-summary-buffer
8895                            gnus-current-article)))
8896            (ogroup gnus-newsgroup-name)
8897            (params (append (gnus-info-params (gnus-get-info ogroup))
8898                            (list (cons 'to-group ogroup))
8899                            (list (cons 'parent-group ogroup))
8900                            (list (cons 'save-article-group ogroup))))
8901            (case-fold-search t)
8902            (buf (current-buffer))
8903            dig to-address)
8904       (save-excursion
8905         (set-buffer gnus-original-article-buffer)
8906         ;; Have the digest group inherit the main mail address of
8907         ;; the parent article.
8908         (when (setq to-address (or (gnus-fetch-field "reply-to")
8909                                    (gnus-fetch-field "from")))
8910           (setq params
8911                 (append
8912                  (list (cons 'to-address
8913                              (funcall gnus-decode-encoded-address-function
8914                                       to-address))))))
8915         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8916         (insert-buffer-substring gnus-original-article-buffer)
8917         ;; Remove lines that may lead nndoc to misinterpret the
8918         ;; document type.
8919         (narrow-to-region
8920          (goto-char (point-min))
8921          (or (search-forward "\n\n" nil t) (point)))
8922         (goto-char (point-min))
8923         (delete-matching-lines "^Path:\\|^From ")
8924         (widen))
8925       (unwind-protect
8926           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8927                     (gnus-newsgroup-ephemeral-ignored-charsets
8928                      gnus-newsgroup-ignored-charsets))
8929                 (gnus-group-read-ephemeral-group
8930                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8931                               (nndoc-article-type
8932                                ,(if force 'mbox 'guess)))
8933                  t nil nil nil
8934                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8935                                                         "ADAPT")))))
8936               ;; Make all postings to this group go to the parent group.
8937               (nconc (gnus-info-params (gnus-get-info name))
8938                      params)
8939             ;; Couldn't select this doc group.
8940             (switch-to-buffer buf)
8941             (gnus-set-global-variables)
8942             (gnus-configure-windows 'summary)
8943             (gnus-message 3 "Article couldn't be entered?"))
8944         (kill-buffer dig)))))
8945
8946 (defun gnus-summary-read-document (n)
8947   "Open a new group based on the current article(s).
8948 This will allow you to read digests and other similar
8949 documents as newsgroups.
8950 Obeys the standard process/prefix convention."
8951   (interactive "P")
8952   (let* ((ogroup gnus-newsgroup-name)
8953          (params (append (gnus-info-params (gnus-get-info ogroup))
8954                          (list (cons 'to-group ogroup))))
8955          group egroup groups vgroup)
8956     (dolist (article (gnus-summary-work-articles n))
8957       (setq group (format "%s-%d" gnus-newsgroup-name article))
8958       (gnus-summary-remove-process-mark article)
8959       (when (gnus-summary-display-article article)
8960         (save-excursion
8961           (with-temp-buffer
8962             (insert-buffer-substring gnus-original-article-buffer)
8963             ;; Remove some headers that may lead nndoc to make
8964             ;; the wrong guess.
8965             (message-narrow-to-head)
8966             (goto-char (point-min))
8967             (delete-matching-lines "^Path:\\|^From ")
8968             (widen)
8969             (if (setq egroup
8970                       (gnus-group-read-ephemeral-group
8971                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8972                                      (nndoc-article-type guess))
8973                        t nil t))
8974                 (progn
8975             ;; Make all postings to this group go to the parent group.
8976                   (nconc (gnus-info-params (gnus-get-info egroup))
8977                          params)
8978                   (push egroup groups))
8979               ;; Couldn't select this doc group.
8980               (gnus-error 3 "Article couldn't be entered"))))))
8981     ;; Now we have selected all the documents.
8982     (cond
8983      ((not groups)
8984       (error "None of the articles could be interpreted as documents"))
8985      ((gnus-group-read-ephemeral-group
8986        (setq vgroup (format
8987                      "nnvirtual:%s-%s" gnus-newsgroup-name
8988                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8989        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8990        t
8991        (cons (current-buffer) 'summary)))
8992      (t
8993       (error "Couldn't select virtual nndoc group")))))
8994
8995 (defun gnus-summary-isearch-article (&optional regexp-p)
8996   "Do incremental search forward on the current article.
8997 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8998   (interactive "P")
8999   (gnus-summary-select-article)
9000   (gnus-configure-windows 'article)
9001   (gnus-eval-in-buffer-window gnus-article-buffer
9002     (save-restriction
9003       (widen)
9004       (isearch-forward regexp-p))))
9005
9006 (defun gnus-summary-repeat-search-article-forward ()
9007   "Repeat the previous search forwards."
9008   (interactive)
9009   (unless gnus-last-search-regexp
9010     (error "No previous search"))
9011   (gnus-summary-search-article-forward gnus-last-search-regexp))
9012
9013 (defun gnus-summary-repeat-search-article-backward ()
9014   "Repeat the previous search backwards."
9015   (interactive)
9016   (unless gnus-last-search-regexp
9017     (error "No previous search"))
9018   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9019
9020 (defun gnus-summary-search-article-forward (regexp &optional backward)
9021   "Search for an article containing REGEXP forward.
9022 If BACKWARD, search backward instead."
9023   (interactive
9024    (list (read-string
9025           (format "Search article %s (regexp%s): "
9026                   (if current-prefix-arg "backward" "forward")
9027                   (if gnus-last-search-regexp
9028                       (concat ", default " gnus-last-search-regexp)
9029                     "")))
9030          current-prefix-arg))
9031   (if (string-equal regexp "")
9032       (setq regexp (or gnus-last-search-regexp ""))
9033     (setq gnus-last-search-regexp regexp)
9034     (setq gnus-article-before-search gnus-current-article))
9035   ;; Intentionally set gnus-last-article.
9036   (setq gnus-last-article gnus-article-before-search)
9037   (let ((gnus-last-article gnus-last-article))
9038     (if (gnus-summary-search-article regexp backward)
9039         (gnus-summary-show-thread)
9040       (signal 'search-failed (list regexp)))))
9041
9042 (defun gnus-summary-search-article-backward (regexp)
9043   "Search for an article containing REGEXP backward."
9044   (interactive
9045    (list (read-string
9046           (format "Search article backward (regexp%s): "
9047                   (if gnus-last-search-regexp
9048                       (concat ", default " gnus-last-search-regexp)
9049                     "")))))
9050   (gnus-summary-search-article-forward regexp 'backward))
9051
9052 (defun gnus-summary-search-article (regexp &optional backward)
9053   "Search for an article containing REGEXP.
9054 Optional argument BACKWARD means do search for backward.
9055 `gnus-select-article-hook' is not called during the search."
9056   ;; We have to require this here to make sure that the following
9057   ;; dynamic binding isn't shadowed by autoloading.
9058   (require 'gnus-async)
9059   (require 'gnus-art)
9060   (let ((gnus-select-article-hook nil)  ;Disable hook.
9061         (gnus-article-prepare-hook nil)
9062         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9063         (gnus-use-article-prefetch nil)
9064         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9065         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9066         (gnus-visual nil)
9067         (gnus-keep-backlog nil)
9068         (gnus-break-pages nil)
9069         (gnus-summary-display-arrow nil)
9070         (gnus-updated-mode-lines nil)
9071         (gnus-auto-center-summary nil)
9072         (sum (current-buffer))
9073         (gnus-display-mime-function nil)
9074         (found nil)
9075         point)
9076     (gnus-save-hidden-threads
9077       (gnus-summary-select-article)
9078       (set-buffer gnus-article-buffer)
9079       (goto-char (window-point (get-buffer-window (current-buffer))))
9080       (when backward
9081         (forward-line -1))
9082       (while (not found)
9083         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9084         (if (if backward
9085                 (re-search-backward regexp nil t)
9086               (re-search-forward regexp nil t))
9087             ;; We found the regexp.
9088             (progn
9089               (setq found 'found)
9090               (beginning-of-line)
9091               (set-window-start
9092                (get-buffer-window (current-buffer))
9093                (point))
9094               (forward-line 1)
9095               (set-window-point
9096                (get-buffer-window (current-buffer))
9097                (point))
9098               (set-buffer sum)
9099               (setq point (point)))
9100           ;; We didn't find it, so we go to the next article.
9101           (set-buffer sum)
9102           (setq found 'not)
9103           (while (eq found 'not)
9104             (if (not (if backward (gnus-summary-find-prev)
9105                        (gnus-summary-find-next)))
9106                 ;; No more articles.
9107                 (setq found t)
9108               ;; Select the next article and adjust point.
9109               (unless (gnus-summary-article-sparse-p
9110                        (gnus-summary-article-number))
9111                 (setq found nil)
9112                 (gnus-summary-select-article)
9113                 (set-buffer gnus-article-buffer)
9114                 (widen)
9115                 (goto-char (if backward (point-max) (point-min))))))))
9116       (gnus-message 7 ""))
9117     ;; Return whether we found the regexp.
9118     (when (eq found 'found)
9119       (goto-char point)
9120       (gnus-summary-show-thread)
9121       (gnus-summary-goto-subject gnus-current-article)
9122       (gnus-summary-position-point)
9123       t)))
9124
9125 (defun gnus-find-matching-articles (header regexp)
9126   "Return a list of all articles that match REGEXP on HEADER.
9127 This search includes all articles in the current group that Gnus has
9128 fetched headers for, whether they are displayed or not."
9129   (let ((articles nil)
9130         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9131         (case-fold-search t))
9132     (dolist (header gnus-newsgroup-headers)
9133       (when (string-match regexp (funcall func header))
9134         (push (mail-header-number header) articles)))
9135     (nreverse articles)))
9136
9137 (defun gnus-summary-find-matching (header regexp &optional backward unread
9138                                           not-case-fold not-matching)
9139   "Return a list of all articles that match REGEXP on HEADER.
9140 The search stars on the current article and goes forwards unless
9141 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9142 If UNREAD is non-nil, only unread articles will
9143 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9144 in the comparisons. If NOT-MATCHING, return a list of all articles that
9145 not match REGEXP on HEADER."
9146   (let ((case-fold-search (not not-case-fold))
9147         articles d func)
9148     (if (consp header)
9149         (if (eq (car header) 'extra)
9150             (setq func
9151                   `(lambda (h)
9152                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9153                          "")))
9154           (error "%s is an invalid header" header))
9155       (unless (fboundp (intern (concat "mail-header-" header)))
9156         (error "%s is not a valid header" header))
9157       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9158     (dolist (d (if (eq backward 'all)
9159                    gnus-newsgroup-data
9160                  (gnus-data-find-list
9161                   (gnus-summary-article-number)
9162                   (gnus-data-list backward))))
9163       (when (and (or (not unread)       ; We want all articles...
9164                      (gnus-data-unread-p d)) ; Or just unreads.
9165                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9166                  (if not-matching
9167                      (not (string-match
9168                            regexp
9169                            (funcall func (gnus-data-header d))))
9170                    (string-match regexp
9171                                  (funcall func (gnus-data-header d)))))
9172         (push (gnus-data-number d) articles))) ; Success!
9173     (nreverse articles)))
9174
9175 (defun gnus-summary-execute-command (header regexp command &optional backward)
9176   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9177 If HEADER is an empty string (or nil), the match is done on the entire
9178 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9179   (interactive
9180    (list (let ((completion-ignore-case t))
9181            (completing-read
9182             "Header name: "
9183             (mapcar (lambda (header) (list (format "%s" header)))
9184                     (append
9185                      '("Number" "Subject" "From" "Lines" "Date"
9186                        "Message-ID" "Xref" "References" "Body")
9187                      gnus-extra-headers))
9188             nil 'require-match))
9189          (read-string "Regexp: ")
9190          (read-key-sequence "Command: ")
9191          current-prefix-arg))
9192   (when (equal header "Body")
9193     (setq header ""))
9194   ;; Hidden thread subtrees must be searched as well.
9195   (gnus-summary-show-all-threads)
9196   ;; We don't want to change current point nor window configuration.
9197   (save-excursion
9198     (save-window-excursion
9199       (let (gnus-visual
9200             gnus-treat-strip-trailing-blank-lines
9201             gnus-treat-strip-leading-blank-lines
9202             gnus-treat-strip-multiple-blank-lines
9203             gnus-treat-hide-boring-headers
9204             gnus-treat-fold-newsgroups
9205             gnus-article-prepare-hook)
9206         (gnus-message 6 "Executing %s..." (key-description command))
9207         ;; We'd like to execute COMMAND interactively so as to give arguments.
9208         (gnus-execute header regexp
9209                       `(call-interactively ',(key-binding command))
9210                       backward)
9211         (gnus-message 6 "Executing %s...done" (key-description command))))))
9212
9213 (defun gnus-summary-beginning-of-article ()
9214   "Scroll the article back to the beginning."
9215   (interactive)
9216   (gnus-summary-select-article)
9217   (gnus-configure-windows 'article)
9218   (gnus-eval-in-buffer-window gnus-article-buffer
9219     (widen)
9220     (goto-char (point-min))
9221     (when gnus-break-pages
9222       (gnus-narrow-to-page))))
9223
9224 (defun gnus-summary-end-of-article ()
9225   "Scroll to the end of the article."
9226   (interactive)
9227   (gnus-summary-select-article)
9228   (gnus-configure-windows 'article)
9229   (gnus-eval-in-buffer-window gnus-article-buffer
9230     (widen)
9231     (goto-char (point-max))
9232     (recenter -3)
9233     (when gnus-break-pages
9234       (gnus-narrow-to-page))))
9235
9236 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9237   "Truncate to LEN and quote all \"(\"'s in STRING."
9238   (gnus-replace-in-string (if (and len (> (length string) len))
9239                               (substring string 0 len)
9240                             string)
9241                           "[()]" "\\\\\\&"))
9242
9243 (defun gnus-summary-print-article (&optional filename n)
9244   "Generate and print a PostScript image of the process-marked (mail) articles.
9245
9246 If used interactively, print the current article if none are
9247 process-marked.  With prefix arg, prompt the user for the name of the
9248 file to save in.
9249
9250 When used from Lisp, accept two optional args FILENAME and N.  N means
9251 to print the next N articles.  If N is negative, print the N previous
9252 articles.  If N is nil and articles have been marked with the process
9253 mark, print these instead.
9254
9255 If the optional first argument FILENAME is nil, send the image to the
9256 printer.  If FILENAME is a string, save the PostScript image in a file with
9257 that name.  If FILENAME is a number, prompt the user for the name of the file
9258 to save in."
9259   (interactive (list (ps-print-preprint current-prefix-arg)))
9260   (dolist (article (gnus-summary-work-articles n))
9261     (gnus-summary-select-article nil nil 'pseudo article)
9262     (gnus-eval-in-buffer-window gnus-article-buffer
9263       (gnus-print-buffer))
9264     (gnus-summary-remove-process-mark article))
9265   (ps-despool filename))
9266
9267 (defun gnus-print-buffer ()
9268   (let ((buffer (generate-new-buffer " *print*")))
9269     (unwind-protect
9270         (progn
9271           (copy-to-buffer buffer (point-min) (point-max))
9272           (set-buffer buffer)
9273           (gnus-remove-text-with-property 'gnus-decoration)
9274           (when (gnus-visual-p 'article-highlight 'highlight)
9275             ;; Copy-to-buffer doesn't copy overlay.  So redo
9276             ;; highlight.
9277             (let ((gnus-article-buffer buffer))
9278               (gnus-article-highlight-citation t)
9279               (gnus-article-highlight-signature)
9280               (gnus-article-emphasize)
9281               (gnus-article-delete-invisible-text)))
9282           (let ((ps-left-header
9283                  (list
9284                   (concat "("
9285                           (gnus-summary-print-truncate-and-quote
9286                            (mail-header-subject gnus-current-headers)
9287                            66) ")")
9288                   (concat "("
9289                           (gnus-summary-print-truncate-and-quote
9290                            (mail-header-from gnus-current-headers)
9291                            45) ")")))
9292                 (ps-right-header
9293                  (list
9294                   "/pagenumberstring load"
9295                   (concat "("
9296                           (mail-header-date gnus-current-headers) ")"))))
9297             (gnus-run-hooks 'gnus-ps-print-hook)
9298             (save-excursion
9299               (if ps-print-color-p
9300                   (ps-spool-buffer-with-faces)
9301                 (ps-spool-buffer)))))
9302       (kill-buffer buffer))))
9303
9304 (defun gnus-summary-show-article (&optional arg)
9305   "Force redisplaying of the current article.
9306 If ARG (the prefix) is a number, show the article with the charset
9307 defined in `gnus-summary-show-article-charset-alist', or the charset
9308 input.
9309 If ARG (the prefix) is non-nil and not a number, show the raw article
9310 without any article massaging functions being run.  Normally, the key
9311 strokes are `C-u g'."
9312   (interactive "P")
9313   (cond
9314    ((numberp arg)
9315     (gnus-summary-show-article t)
9316     (let ((gnus-newsgroup-charset
9317            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9318                (mm-read-coding-system
9319                 "View as charset: " ;; actually it is coding system.
9320                 (with-current-buffer gnus-article-buffer
9321                   (mm-detect-coding-region (point) (point-max))))))
9322           (gnus-newsgroup-ignored-charsets 'gnus-all))
9323       (gnus-summary-select-article nil 'force)
9324       (let ((deps gnus-newsgroup-dependencies)
9325             head header lines)
9326         (save-excursion
9327           (set-buffer gnus-original-article-buffer)
9328           (save-restriction
9329             (message-narrow-to-head)
9330             (setq head (buffer-string))
9331             (goto-char (point-min))
9332             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9333               (goto-char (point-max))
9334               (widen)
9335               (setq lines (1- (count-lines (point) (point-max))))))
9336           (with-temp-buffer
9337             (insert (format "211 %d Article retrieved.\n"
9338                             (cdr gnus-article-current)))
9339             (insert head)
9340             (if lines (insert (format "Lines: %d\n" lines)))
9341             (insert ".\n")
9342             (let ((nntp-server-buffer (current-buffer)))
9343               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9344         (gnus-data-set-header
9345          (gnus-data-find (cdr gnus-article-current))
9346          header)
9347         (gnus-summary-update-article-line
9348          (cdr gnus-article-current) header)
9349         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9350           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9351    ((not arg)
9352     ;; Select the article the normal way.
9353     (gnus-summary-select-article nil 'force))
9354    (t
9355     ;; We have to require this here to make sure that the following
9356     ;; dynamic binding isn't shadowed by autoloading.
9357     (require 'gnus-async)
9358     (require 'gnus-art)
9359     ;; Bind the article treatment functions to nil.
9360     (let ((gnus-have-all-headers t)
9361           gnus-article-prepare-hook
9362           gnus-article-decode-hook
9363           gnus-display-mime-function
9364           gnus-break-pages)
9365       ;; Destroy any MIME parts.
9366       (when (gnus-buffer-live-p gnus-article-buffer)
9367         (save-excursion
9368           (set-buffer gnus-article-buffer)
9369           (mm-destroy-parts gnus-article-mime-handles)
9370           ;; Set it to nil for safety reason.
9371           (setq gnus-article-mime-handle-alist nil)
9372           (setq gnus-article-mime-handles nil)))
9373       (gnus-summary-select-article nil 'force))))
9374   (gnus-summary-goto-subject gnus-current-article)
9375   (gnus-summary-position-point))
9376
9377 (defun gnus-summary-show-raw-article ()
9378   "Show the raw article without any article massaging functions being run."
9379   (interactive)
9380   (gnus-summary-show-article t))
9381
9382 (defun gnus-summary-verbose-headers (&optional arg)
9383   "Toggle permanent full header display.
9384 If ARG is a positive number, turn header display on.
9385 If ARG is a negative number, turn header display off."
9386   (interactive "P")
9387   (setq gnus-show-all-headers
9388         (cond ((or (not (numberp arg))
9389                    (zerop arg))
9390                (not gnus-show-all-headers))
9391               ((natnump arg)
9392                t)))
9393   (gnus-summary-show-article))
9394
9395 (defun gnus-summary-toggle-header (&optional arg)
9396   "Show the headers if they are hidden, or hide them if they are shown.
9397 If ARG is a positive number, show the entire header.
9398 If ARG is a negative number, hide the unwanted header lines."
9399   (interactive "P")
9400   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9401                      (get-buffer-window gnus-article-buffer t))))
9402     (with-current-buffer gnus-article-buffer
9403       (widen)
9404       (article-narrow-to-head)
9405       (let* ((buffer-read-only nil)
9406              (inhibit-point-motion-hooks t)
9407              (hidden (if (numberp arg)
9408                          (>= arg 0)
9409                        (or (not (looking-at "[^ \t\n]+:"))
9410                            (gnus-article-hidden-text-p 'headers))))
9411              s e)
9412         (delete-region (point-min) (point-max))
9413         (with-current-buffer gnus-original-article-buffer
9414           (goto-char (setq s (point-min)))
9415           (setq e (if (search-forward "\n\n" nil t)
9416                       (1- (point))
9417                     (point-max))))
9418         (insert-buffer-substring gnus-original-article-buffer s e)
9419         (run-hooks 'gnus-article-decode-hook)
9420         (if hidden
9421             (let ((gnus-treat-hide-headers nil)
9422                   (gnus-treat-hide-boring-headers nil))
9423               (gnus-delete-wash-type 'headers)
9424               (gnus-treat-article 'head))
9425           (gnus-treat-article 'head))
9426         (widen)
9427         (if window
9428             (set-window-start window (goto-char (point-min))))
9429         (if gnus-break-pages
9430             (gnus-narrow-to-page)
9431           (when (gnus-visual-p 'page-marker)
9432             (let ((buffer-read-only nil))
9433               (gnus-remove-text-with-property 'gnus-prev)
9434               (gnus-remove-text-with-property 'gnus-next))))
9435         (gnus-set-mode-line 'article)))))
9436
9437 (defun gnus-summary-show-all-headers ()
9438   "Make all header lines visible."
9439   (interactive)
9440   (gnus-summary-toggle-header 1))
9441
9442 (defun gnus-summary-caesar-message (&optional arg)
9443   "Caesar rotate the current article by 13.
9444 With a non-numerical prefix, also rotate headers.  A numerical
9445 prefix specifies how many places to rotate each letter forward."
9446   (interactive "P")
9447   (gnus-summary-select-article)
9448   (let ((mail-header-separator ""))
9449     (gnus-eval-in-buffer-window gnus-article-buffer
9450       (save-restriction
9451         (widen)
9452         (let ((start (window-start))
9453               buffer-read-only)
9454           (if (equal arg '(4))
9455               (message-caesar-buffer-body nil t)
9456             (message-caesar-buffer-body arg))
9457           (set-window-start (get-buffer-window (current-buffer)) start)))))
9458   ;; Create buttons and stuff...
9459   (gnus-treat-article nil))
9460
9461 (declare-function idna-to-unicode "ext:idna" (str))
9462
9463 (defun gnus-summary-idna-message (&optional arg)
9464   "Decode IDNA encoded domain names in the current articles.
9465 IDNA encoded domain names looks like `xn--bar'.  If a string
9466 remain unencoded after running this function, it is likely an
9467 invalid IDNA string (`xn--bar' is invalid).
9468
9469 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9470 installed for this command to work."
9471   (interactive "P")
9472   (if (not (and (condition-case nil (require 'idna)
9473                   (file-error))
9474                 (mm-coding-system-p 'utf-8)
9475                 (executable-find (symbol-value 'idna-program))))
9476       (gnus-message
9477        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9478     (gnus-summary-select-article)
9479     (let ((mail-header-separator ""))
9480       (gnus-eval-in-buffer-window gnus-article-buffer
9481         (save-restriction
9482           (widen)
9483           (let ((start (window-start))
9484                 buffer-read-only)
9485             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9486               (replace-match (idna-to-unicode (match-string 1))))
9487             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9488
9489 (defun gnus-summary-morse-message (&optional arg)
9490   "Morse decode the current article."
9491   (interactive "P")
9492   (gnus-summary-select-article)
9493   (let ((mail-header-separator ""))
9494     (gnus-eval-in-buffer-window gnus-article-buffer
9495       (save-excursion
9496         (save-restriction
9497           (widen)
9498           (let ((pos (window-start))
9499                 buffer-read-only)
9500             (goto-char (point-min))
9501             (when (message-goto-body)
9502               (gnus-narrow-to-body))
9503             (goto-char (point-min))
9504             (while (search-forward "·" (point-max) t)
9505               (replace-match "."))
9506             (unmorse-region (point-min) (point-max))
9507             (widen)
9508             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9509
9510 (defun gnus-summary-stop-page-breaking ()
9511   "Stop page breaking in the current article."
9512   (interactive)
9513   (gnus-summary-select-article)
9514   (gnus-eval-in-buffer-window gnus-article-buffer
9515     (widen)
9516     (when (gnus-visual-p 'page-marker)
9517       (let ((buffer-read-only nil))
9518         (gnus-remove-text-with-property 'gnus-prev)
9519         (gnus-remove-text-with-property 'gnus-next))
9520       (setq gnus-page-broken nil))))
9521
9522 (defun gnus-summary-move-article (&optional n to-newsgroup
9523                                             select-method action)
9524   "Move the current article to a different newsgroup.
9525 If N is a positive number, move the N next articles.
9526 If N is a negative number, move the N previous articles.
9527 If N is nil and any articles have been marked with the process mark,
9528 move those articles instead.
9529 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9530 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9531 re-spool using this method.
9532
9533 When called interactively with TO-NEWSGROUP being nil, the value of
9534 the variable `gnus-move-split-methods' is used for finding a default
9535 for the target newsgroup.
9536
9537 For this function to work, both the current newsgroup and the
9538 newsgroup that you want to move to have to support the `request-move'
9539 and `request-accept' functions.
9540
9541 ACTION can be either `move' (the default), `crosspost' or `copy'."
9542   (interactive "P")
9543   (unless action
9544     (setq action 'move))
9545   ;; Check whether the source group supports the required functions.
9546   (cond ((and (eq action 'move)
9547               (not (gnus-check-backend-function
9548                     'request-move-article gnus-newsgroup-name)))
9549          (error "The current group does not support article moving"))
9550         ((and (eq action 'crosspost)
9551               (not (gnus-check-backend-function
9552                     'request-replace-article gnus-newsgroup-name)))
9553          (error "The current group does not support article editing")))
9554   (let ((articles (gnus-summary-work-articles n))
9555         (prefix (if (gnus-check-backend-function
9556                      'request-move-article gnus-newsgroup-name)
9557                     (funcall gnus-move-group-prefix-function
9558                              gnus-newsgroup-name)
9559                   ""))
9560         (names '((move "Move" "Moving")
9561                  (copy "Copy" "Copying")
9562                  (crosspost "Crosspost" "Crossposting")))
9563         (copy-buf (save-excursion
9564                     (nnheader-set-temp-buffer " *copy article*")))
9565         art-group to-method new-xref article to-groups
9566         articles-to-update-marks encoded)
9567     (unless (assq action names)
9568       (error "Unknown action %s" action))
9569     ;; Read the newsgroup name.
9570     (when (and (not to-newsgroup)
9571                (not select-method))
9572       (if (and gnus-move-split-methods
9573                (not
9574                 (and (memq gnus-current-article articles)
9575                      (gnus-buffer-live-p gnus-original-article-buffer))))
9576           ;; When `gnus-move-split-methods' is non-nil, we have to
9577           ;; select an article to give `gnus-read-move-group-name' an
9578           ;; opportunity to suggest an appropriate default.  However,
9579           ;; we needn't render or mark the article.
9580           (let ((gnus-display-mime-function nil)
9581                 (gnus-article-prepare-hook nil)
9582                 (gnus-mark-article-hook nil))
9583             (gnus-summary-select-article nil nil nil (car articles))))
9584       (setq to-newsgroup (gnus-read-move-group-name
9585                           (cadr (assq action names))
9586                           (symbol-value
9587                            (intern (format "gnus-current-%s-group" action)))
9588                           articles prefix)
9589             encoded to-newsgroup
9590             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9591       (set (intern (format "gnus-current-%s-group" action))
9592            (mm-decode-coding-string
9593             to-newsgroup
9594             (gnus-group-name-charset to-method to-newsgroup))))
9595     (unless to-method
9596       (setq to-method (or select-method
9597                           (gnus-server-to-method
9598                            (gnus-group-method to-newsgroup)))))
9599     (setq to-newsgroup
9600           (or encoded
9601               (and to-newsgroup
9602                    (mm-encode-coding-string
9603                     to-newsgroup
9604                     (gnus-group-name-charset to-method to-newsgroup)))))
9605     ;; Check the method we are to move this article to...
9606     (unless (gnus-check-backend-function
9607              'request-accept-article (car to-method))
9608       (error "%s does not support article copying" (car to-method)))
9609     (unless (gnus-check-server to-method)
9610       (error "Can't open server %s" (car to-method)))
9611     (gnus-message 6 "%s to %s: %s..."
9612                   (caddr (assq action names))
9613                   (or (car select-method)
9614                       (gnus-group-decoded-name to-newsgroup))
9615                   articles)
9616     (while articles
9617       (setq article (pop articles))
9618       (setq
9619        art-group
9620        (cond
9621         ;; Move the article.
9622         ((eq action 'move)
9623          ;; Remove this article from future suppression.
9624          (gnus-dup-unsuppress-article article)
9625          (let* ((from-method (gnus-find-method-for-group
9626                               gnus-newsgroup-name))
9627                 (to-method (or select-method
9628                                (gnus-find-method-for-group to-newsgroup)))
9629                 (move-is-internal (gnus-method-equal from-method to-method)))
9630            (gnus-request-move-article
9631             article                     ; Article to move
9632             gnus-newsgroup-name         ; From newsgroup
9633             (nth 1 (gnus-find-method-for-group
9634                     gnus-newsgroup-name)) ; Server
9635             (list 'gnus-request-accept-article
9636                   to-newsgroup (list 'quote select-method)
9637                   (not articles) t)     ; Accept form
9638             (not articles)              ; Only save nov last time
9639             move-is-internal)))         ; is this move internal?
9640         ;; Copy the article.
9641         ((eq action 'copy)
9642          (save-excursion
9643            (set-buffer copy-buf)
9644            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9645              (save-restriction
9646                (nnheader-narrow-to-headers)
9647                (dolist (hdr gnus-copy-article-ignored-headers)
9648                  (message-remove-header hdr t)))
9649              (gnus-request-accept-article
9650               to-newsgroup select-method (not articles) t))))
9651         ;; Crosspost the article.
9652         ((eq action 'crosspost)
9653          (let ((xref (message-tokenize-header
9654                       (mail-header-xref (gnus-summary-article-header article))
9655                       " ")))
9656            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9657                                   ":" (number-to-string article)))
9658            (unless xref
9659              (setq xref (list (system-name))))
9660            (setq new-xref
9661                  (concat
9662                   (mapconcat 'identity
9663                              (delete "Xref:" (delete new-xref xref))
9664                              " ")
9665                   " " new-xref))
9666            (save-excursion
9667              (set-buffer copy-buf)
9668              ;; First put the article in the destination group.
9669              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9670              (when (consp (setq art-group
9671                                 (gnus-request-accept-article
9672                                  to-newsgroup select-method (not articles) t)))
9673                (setq new-xref (concat new-xref " " (car art-group)
9674                                       ":"
9675                                       (number-to-string (cdr art-group))))
9676                ;; Now we have the new Xrefs header, so we insert
9677                ;; it and replace the new article.
9678                (nnheader-replace-header "Xref" new-xref)
9679                (gnus-request-replace-article
9680                 (cdr art-group) to-newsgroup (current-buffer) t)
9681                art-group))))))
9682       (cond
9683        ((not art-group)
9684         (gnus-message 1 "Couldn't %s article %s: %s"
9685                       (cadr (assq action names)) article
9686                       (nnheader-get-report (car to-method))))
9687        ((eq art-group 'junk)
9688         (when (eq action 'move)
9689           (gnus-summary-mark-article article gnus-canceled-mark)
9690           (gnus-message 4 "Deleted article %s" article)
9691           ;; run the delete hook
9692           (run-hook-with-args 'gnus-summary-article-delete-hook
9693                               action
9694                               (gnus-data-header
9695                                (assoc article (gnus-data-list nil)))
9696                               gnus-newsgroup-name nil
9697                               select-method)))
9698        (t
9699         (let* ((pto-group (gnus-group-prefixed-name
9700                            (car art-group) to-method))
9701                (info (gnus-get-info pto-group))
9702                (to-group (gnus-info-group info))
9703                to-marks)
9704           ;; Update the group that has been moved to.
9705           (when (and info
9706                      (memq action '(move copy)))
9707             (unless (member to-group to-groups)
9708               (push to-group to-groups))
9709
9710             (unless (memq article gnus-newsgroup-unreads)
9711               (push 'read to-marks)
9712               (gnus-info-set-read
9713                info (gnus-add-to-range (gnus-info-read info)
9714                                        (list (cdr art-group)))))
9715
9716             ;; See whether the article is to be put in the cache.
9717             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9718                              gnus-article-mark-lists
9719                            (delete '(expirable . expire)
9720                                    (copy-sequence gnus-article-mark-lists))))
9721                   (to-article (cdr art-group)))
9722
9723               ;; Enter the article into the cache in the new group,
9724               ;; if that is required.
9725               (when gnus-use-cache
9726                 (gnus-cache-possibly-enter-article
9727                  to-group to-article
9728                  (memq article gnus-newsgroup-marked)
9729                  (memq article gnus-newsgroup-dormant)
9730                  (memq article gnus-newsgroup-unreads)))
9731
9732               (when gnus-preserve-marks
9733                 ;; Copy any marks over to the new group.
9734                 (when (and (equal to-group gnus-newsgroup-name)
9735                            (not (memq article gnus-newsgroup-unreads)))
9736                   ;; Mark this article as read in this group.
9737                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9738                   (setcdr (gnus-active to-group) to-article)
9739                   (setcdr gnus-newsgroup-active to-article))
9740
9741                 (while marks
9742                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9743                     (when (memq article (symbol-value
9744                                          (intern (format "gnus-newsgroup-%s"
9745                                                          (caar marks)))))
9746                       (push (cdar marks) to-marks)
9747                       ;; If the other group is the same as this group,
9748                       ;; then we have to add the mark to the list.
9749                       (when (equal to-group gnus-newsgroup-name)
9750                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9751                              (cons to-article
9752                                    (symbol-value
9753                                     (intern (format "gnus-newsgroup-%s"
9754                                                     (caar marks)))))))
9755                       ;; Copy the marks to other group.
9756                       (gnus-add-marked-articles
9757                        to-group (cdar marks) (list to-article) info)))
9758                   (setq marks (cdr marks)))
9759
9760                 (gnus-request-set-mark
9761                  to-group (list (list (list to-article) 'add to-marks))))
9762
9763               (gnus-dribble-enter
9764                (concat "(gnus-group-set-info '"
9765                        (gnus-prin1-to-string (gnus-get-info to-group))
9766                        ")"))))
9767
9768           ;; Update the Xref header in this article to point to
9769           ;; the new crossposted article we have just created.
9770           (when (eq action 'crosspost)
9771             (save-excursion
9772               (set-buffer copy-buf)
9773               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9774               (nnheader-replace-header "Xref" new-xref)
9775               (gnus-request-replace-article
9776                article gnus-newsgroup-name (current-buffer) t)))
9777
9778           ;; run the move/copy/crosspost/respool hook
9779           (run-hook-with-args 'gnus-summary-article-move-hook
9780                               action
9781                               (gnus-data-header
9782                                (assoc article (gnus-data-list nil)))
9783                               gnus-newsgroup-name
9784                               to-newsgroup
9785                               select-method))
9786
9787         ;;;!!!Why is this necessary?
9788         (set-buffer gnus-summary-buffer)
9789
9790         (gnus-summary-goto-subject article)
9791         (when (eq action 'move)
9792           (gnus-summary-mark-article article gnus-canceled-mark))))
9793       (push article articles-to-update-marks))
9794
9795     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9796     ;; Re-activate all groups that have been moved to.
9797     (save-excursion
9798       (set-buffer gnus-group-buffer)
9799       (let ((gnus-group-marked to-groups))
9800         (gnus-group-get-new-news-this-group nil t)))
9801
9802     (gnus-kill-buffer copy-buf)
9803     (gnus-summary-position-point)
9804     (gnus-set-mode-line 'summary)))
9805
9806 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9807   "Copy the current article to some other group.
9808 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9809 When called interactively, if TO-NEWSGROUP is nil, use the value of
9810 the variable `gnus-move-split-methods' for finding a default target
9811 newsgroup.
9812 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9813 re-spool using this method."
9814   (interactive "P")
9815   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9816
9817 (defun gnus-summary-crosspost-article (&optional n)
9818   "Crosspost the current article to some other group."
9819   (interactive "P")
9820   (gnus-summary-move-article n nil nil 'crosspost))
9821
9822 (defcustom gnus-summary-respool-default-method nil
9823   "Default method type for respooling an article.
9824 If nil, use to the current newsgroup method."
9825   :type 'symbol
9826   :group 'gnus-summary-mail)
9827
9828 (defcustom gnus-summary-display-while-building nil
9829   "If non-nil, show and update the summary buffer as it's being built.
9830 If the value is t, update the buffer after every line is inserted.  If
9831 the value is an integer (N), update the display every N lines."
9832   :version "22.1"
9833   :group 'gnus-thread
9834   :type '(choice (const :tag "off" nil)
9835                  number
9836                  (const :tag "frequently" t)))
9837
9838 (defun gnus-summary-respool-article (&optional n method)
9839   "Respool the current article.
9840 The article will be squeezed through the mail spooling process again,
9841 which means that it will be put in some mail newsgroup or other
9842 depending on `nnmail-split-methods'.
9843 If N is a positive number, respool the N next articles.
9844 If N is a negative number, respool the N previous articles.
9845 If N is nil and any articles have been marked with the process mark,
9846 respool those articles instead.
9847
9848 Respooling can be done both from mail groups and \"real\" newsgroups.
9849 In the former case, the articles in question will be moved from the
9850 current group into whatever groups they are destined to.  In the
9851 latter case, they will be copied into the relevant groups."
9852   (interactive
9853    (list current-prefix-arg
9854          (let* ((methods (gnus-methods-using 'respool))
9855                 (methname
9856                  (symbol-name (or gnus-summary-respool-default-method
9857                                   (car (gnus-find-method-for-group
9858                                         gnus-newsgroup-name)))))
9859                 (method
9860                  (gnus-completing-read-with-default
9861                   methname "Backend to use when respooling"
9862                   methods nil t nil 'gnus-mail-method-history))
9863                 ms)
9864            (cond
9865             ((zerop (length (setq ms (gnus-servers-using-backend
9866                                       (intern method)))))
9867              (list (intern method) ""))
9868             ((= 1 (length ms))
9869              (car ms))
9870             (t
9871              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9872                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9873                            ms-alist))))))))
9874   (unless method
9875     (error "No method given for respooling"))
9876   (if (assoc (symbol-name
9877               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9878              (gnus-methods-using 'respool))
9879       (gnus-summary-move-article n nil method)
9880     (gnus-summary-copy-article n nil method)))
9881
9882 (defun gnus-summary-import-article (file &optional edit)
9883   "Import an arbitrary file into a mail newsgroup."
9884   (interactive "fImport file: \nP")
9885   (let ((group gnus-newsgroup-name)
9886         (now (current-time))
9887         atts lines group-art)
9888     (unless (gnus-check-backend-function 'request-accept-article group)
9889       (error "%s does not support article importing" group))
9890     (or (file-readable-p file)
9891         (not (file-regular-p file))
9892         (error "Can't read %s" file))
9893     (save-excursion
9894       (set-buffer (gnus-get-buffer-create " *import file*"))
9895       (erase-buffer)
9896       (nnheader-insert-file-contents file)
9897       (goto-char (point-min))
9898       (if (nnheader-article-p)
9899           (save-restriction
9900             (goto-char (point-min))
9901             (search-forward "\n\n" nil t)
9902             (narrow-to-region (point-min) (1- (point)))
9903             (goto-char (point-min))
9904             (unless (re-search-forward "^date:" nil t)
9905               (goto-char (point-max))
9906               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9907        ;; This doesn't look like an article, so we fudge some headers.
9908         (setq atts (file-attributes file)
9909               lines (count-lines (point-min) (point-max)))
9910         (insert "From: " (read-string "From: ") "\n"
9911                 "Subject: " (read-string "Subject: ") "\n"
9912                 "Date: " (message-make-date (nth 5 atts)) "\n"
9913                 "Message-ID: " (message-make-message-id) "\n"
9914                 "Lines: " (int-to-string lines) "\n"
9915                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9916       (setq group-art (gnus-request-accept-article group nil t))
9917       (kill-buffer (current-buffer)))
9918     (setq gnus-newsgroup-active (gnus-activate-group group))
9919     (forward-line 1)
9920     (gnus-summary-goto-article (cdr group-art) nil t)
9921     (when edit
9922       (gnus-summary-edit-article))))
9923
9924 (defun gnus-summary-create-article ()
9925   "Create an article in a mail newsgroup."
9926   (interactive)
9927   (let ((group gnus-newsgroup-name)
9928         (now (current-time))
9929         group-art)
9930     (unless (gnus-check-backend-function 'request-accept-article group)
9931       (error "%s does not support article importing" group))
9932     (save-excursion
9933       (set-buffer (gnus-get-buffer-create " *import file*"))
9934       (erase-buffer)
9935       (goto-char (point-min))
9936       ;; This doesn't look like an article, so we fudge some headers.
9937       (insert "From: " (read-string "From: ") "\n"
9938               "Subject: " (read-string "Subject: ") "\n"
9939               "Date: " (message-make-date now) "\n"
9940               "Message-ID: " (message-make-message-id) "\n")
9941       (setq group-art (gnus-request-accept-article group nil t))
9942       (kill-buffer (current-buffer)))
9943     (setq gnus-newsgroup-active (gnus-activate-group group))
9944     (forward-line 1)
9945     (gnus-summary-goto-article (cdr group-art) nil t)
9946     (gnus-summary-edit-article)))
9947
9948 (defun gnus-summary-article-posted-p ()
9949   "Say whether the current (mail) article is available from news as well.
9950 This will be the case if the article has both been mailed and posted."
9951   (interactive)
9952   (let ((id (mail-header-references (gnus-summary-article-header)))
9953         (gnus-override-method (car (gnus-refer-article-methods))))
9954     (if (gnus-request-head id "")
9955         (gnus-message 2 "The current message was found on %s"
9956                       gnus-override-method)
9957       (gnus-message 2 "The current message couldn't be found on %s"
9958                     gnus-override-method)
9959       nil)))
9960
9961 (defun gnus-summary-expire-articles (&optional now)
9962   "Expire all articles that are marked as expirable in the current group."
9963   (interactive)
9964   (when (and (not gnus-group-is-exiting-without-update-p)
9965              (gnus-check-backend-function
9966               'request-expire-articles gnus-newsgroup-name))
9967     ;; This backend supports expiry.
9968     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9969            (expirable (if total
9970                           (progn
9971                             ;; We need to update the info for
9972                             ;; this group for `gnus-list-of-read-articles'
9973                             ;; to give us the right answer.
9974                             (gnus-run-hooks 'gnus-exit-group-hook)
9975                             (gnus-summary-update-info)
9976                             (gnus-list-of-read-articles gnus-newsgroup-name))
9977                         (setq gnus-newsgroup-expirable
9978                               (sort gnus-newsgroup-expirable '<))))
9979            (expiry-wait (if now 'immediate
9980                           (gnus-group-find-parameter
9981                            gnus-newsgroup-name 'expiry-wait)))
9982            (nnmail-expiry-target
9983             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9984                 nnmail-expiry-target))
9985            es)
9986       (when expirable
9987         ;; There are expirable articles in this group, so we run them
9988         ;; through the expiry process.
9989         (gnus-message 6 "Expiring articles...")
9990         (unless (gnus-check-group gnus-newsgroup-name)
9991           (error "Can't open server for %s" gnus-newsgroup-name))
9992         ;; The list of articles that weren't expired is returned.
9993         (save-excursion
9994           (if expiry-wait
9995               (let ((nnmail-expiry-wait-function nil)
9996                     (nnmail-expiry-wait expiry-wait))
9997                 (setq es (gnus-request-expire-articles
9998                           expirable gnus-newsgroup-name)))
9999             (setq es (gnus-request-expire-articles
10000                       expirable gnus-newsgroup-name)))
10001           (unless total
10002             (setq gnus-newsgroup-expirable es))
10003           ;; We go through the old list of expirable, and mark all
10004           ;; really expired articles as nonexistent.
10005           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10006             (let ((gnus-use-cache nil))
10007               (dolist (article expirable)
10008                 (when (and (not (memq article es))
10009                            (gnus-data-find article))
10010                   (gnus-summary-mark-article article gnus-canceled-mark)
10011                   (run-hook-with-args 'gnus-summary-article-expire-hook
10012                                       'delete
10013                                       (gnus-data-header
10014                                        (assoc article (gnus-data-list nil)))
10015                                       gnus-newsgroup-name
10016                                       nil
10017                                       nil))))))
10018         (gnus-message 6 "Expiring articles...done")))))
10019
10020 (defun gnus-summary-expire-articles-now ()
10021   "Expunge all expirable articles in the current group.
10022 This means that *all* articles that are marked as expirable will be
10023 deleted forever, right now."
10024   (interactive)
10025   (or gnus-expert-user
10026       (gnus-yes-or-no-p
10027        "Are you really, really sure you want to delete all expirable messages? ")
10028       (error "Phew!"))
10029   (gnus-summary-expire-articles t))
10030
10031 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10032 (defun gnus-summary-delete-article (&optional n)
10033   "Delete the N next (mail) articles.
10034 This command actually deletes articles.  This is not a marking
10035 command.  The article will disappear forever from your life, never to
10036 return.
10037
10038 If N is negative, delete backwards.
10039 If N is nil and articles have been marked with the process mark,
10040 delete these instead.
10041
10042 If `gnus-novice-user' is non-nil you will be asked for
10043 confirmation before the articles are deleted."
10044   (interactive "P")
10045   (unless (gnus-check-backend-function 'request-expire-articles
10046                                        gnus-newsgroup-name)
10047     (error "The current newsgroup does not support article deletion"))
10048   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10049     (error "Couldn't open server"))
10050   ;; Compute the list of articles to delete.
10051   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10052         (nnmail-expiry-target 'delete)
10053         not-deleted)
10054     (if (and gnus-novice-user
10055              (not (gnus-yes-or-no-p
10056                    (format "Do you really want to delete %s forever? "
10057                            (if (> (length articles) 1)
10058                                (format "these %s articles" (length articles))
10059                              "this article")))))
10060         ()
10061       ;; Delete the articles.
10062       (setq not-deleted (gnus-request-expire-articles
10063                          articles gnus-newsgroup-name 'force))
10064       (while articles
10065         (gnus-summary-remove-process-mark (car articles))
10066         ;; The backend might not have been able to delete the article
10067         ;; after all.
10068         (unless (memq (car articles) not-deleted)
10069           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10070         (let* ((article (car articles))
10071                (ghead  (gnus-data-header
10072                                     (assoc article (gnus-data-list nil)))))
10073           (run-hook-with-args 'gnus-summary-article-delete-hook
10074                               'delete ghead gnus-newsgroup-name nil
10075                               nil))
10076         (setq articles (cdr articles)))
10077       (when not-deleted
10078         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10079     (gnus-summary-position-point)
10080     (gnus-set-mode-line 'summary)
10081     not-deleted))
10082
10083 (defun gnus-summary-edit-article (&optional arg)
10084   "Edit the current article.
10085 This will have permanent effect only in mail groups.
10086 If ARG is nil, edit the decoded articles.
10087 If ARG is 1, edit the raw articles.
10088 If ARG is 2, edit the raw articles even in read-only groups.
10089 If ARG is 3, edit the articles with the current handles.
10090 Otherwise, allow editing of articles even in read-only
10091 groups."
10092   (interactive "P")
10093   (let (force raw current-handles)
10094     (cond
10095      ((null arg))
10096      ((eq arg 1)
10097       (setq raw t))
10098      ((eq arg 2)
10099       (setq raw t
10100             force t))
10101      ((eq arg 3)
10102       (setq current-handles
10103             (and (gnus-buffer-live-p gnus-article-buffer)
10104                  (with-current-buffer gnus-article-buffer
10105                    (prog1
10106                        gnus-article-mime-handles
10107                      (setq gnus-article-mime-handles nil))))))
10108      (t
10109       (setq force t)))
10110     (when (and raw (not force)
10111                (member gnus-newsgroup-name '("nndraft:delayed"
10112                                              "nndraft:drafts"
10113                                              "nndraft:queue")))
10114       (error "Can't edit the raw article in group %s"
10115              gnus-newsgroup-name))
10116     (save-excursion
10117       (set-buffer gnus-summary-buffer)
10118       (let ((mail-parse-charset gnus-newsgroup-charset)
10119             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10120         (gnus-set-global-variables)
10121         (when (and (not force)
10122                    (gnus-group-read-only-p))
10123           (error "The current newsgroup does not support article editing"))
10124         (gnus-summary-show-article t)
10125         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10126           (with-current-buffer gnus-article-buffer
10127             (mm-enable-multibyte)))
10128         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10129             (setq raw t))
10130         (gnus-article-edit-article
10131          (if raw 'ignore
10132            `(lambda ()
10133               (let ((mbl mml-buffer-list))
10134                 (setq mml-buffer-list nil)
10135                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10136                   (mime-to-mml ,'current-handles))
10137                 (let ((mbl1 mml-buffer-list))
10138                   (setq mml-buffer-list mbl)
10139                   (set (make-local-variable 'mml-buffer-list) mbl1))
10140                 (gnus-make-local-hook 'kill-buffer-hook)
10141                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10142          `(lambda (no-highlight)
10143             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10144                   (message-options message-options)
10145                   (message-options-set-recipient)
10146                   (mail-parse-ignored-charsets
10147                    ',gnus-newsgroup-ignored-charsets)
10148                   (rfc2047-header-encoding-alist
10149                    ',(let ((charset (gnus-group-name-charset
10150                                      (gnus-find-method-for-group
10151                                       gnus-newsgroup-name)
10152                                      gnus-newsgroup-name)))
10153                        (append (list (cons "Newsgroups" charset)
10154                                      (cons "Followup-To" charset)
10155                                      (cons "Xref" charset))
10156                                rfc2047-header-encoding-alist))))
10157               ,(if (not raw) '(progn
10158                                 (mml-to-mime)
10159                                 (mml-destroy-buffers)
10160                                 (remove-hook 'kill-buffer-hook
10161                                              'mml-destroy-buffers t)
10162                                 (kill-local-variable 'mml-buffer-list)))
10163               (gnus-summary-edit-article-done
10164                ,(or (mail-header-references gnus-current-headers) "")
10165                ,(gnus-group-read-only-p)
10166                ,gnus-summary-buffer no-highlight))))))))
10167
10168 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10169
10170 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10171                                                  no-highlight)
10172   "Make edits to the current article permanent."
10173   (interactive)
10174   (save-excursion
10175    ;; The buffer restriction contains the entire article if it exists.
10176     (when (article-goto-body)
10177       (let ((lines (count-lines (point) (point-max)))
10178             (length (- (point-max) (point)))
10179             (case-fold-search t)
10180             (body (copy-marker (point))))
10181         (goto-char (point-min))
10182         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10183           (delete-region (match-beginning 1) (match-end 1))
10184           (insert (number-to-string length)))
10185         (goto-char (point-min))
10186         (when (re-search-forward
10187                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10188           (delete-region (match-beginning 1) (match-end 1))
10189           (insert (number-to-string length)))
10190         (goto-char (point-min))
10191         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10192           (delete-region (match-beginning 1) (match-end 1))
10193           (insert (number-to-string lines))))))
10194   ;; Replace the article.
10195   (let ((buf (current-buffer)))
10196     (with-temp-buffer
10197       (insert-buffer-substring buf)
10198
10199       (if (and (not read-only)
10200                (not (gnus-request-replace-article
10201                      (cdr gnus-article-current) (car gnus-article-current)
10202                      (current-buffer) t)))
10203           (error "Couldn't replace article")
10204         ;; Update the summary buffer.
10205         (if (and references
10206                  (equal (message-tokenize-header references " ")
10207                         (message-tokenize-header
10208                          (or (message-fetch-field "references") "") " ")))
10209             ;; We only have to update this line.
10210             (save-excursion
10211               (save-restriction
10212                 (message-narrow-to-head)
10213                 (let ((head (buffer-substring-no-properties
10214                              (point-min) (point-max)))
10215                       header)
10216                   (with-temp-buffer
10217                     (insert (format "211 %d Article retrieved.\n"
10218                                     (cdr gnus-article-current)))
10219                     (insert head)
10220                     (insert ".\n")
10221                     (let ((nntp-server-buffer (current-buffer)))
10222                       (setq header (car (gnus-get-newsgroup-headers
10223                                          nil t))))
10224                     (save-excursion
10225                       (set-buffer gnus-summary-buffer)
10226                       (gnus-data-set-header
10227                        (gnus-data-find (cdr gnus-article-current))
10228                        header)
10229                       (gnus-summary-update-article-line
10230                        (cdr gnus-article-current) header)
10231                       (if (gnus-summary-goto-subject
10232                            (cdr gnus-article-current) nil t)
10233                           (gnus-summary-update-secondary-mark
10234                            (cdr gnus-article-current))))))))
10235           ;; Update threads.
10236           (set-buffer (or buffer gnus-summary-buffer))
10237           (gnus-summary-update-article (cdr gnus-article-current))
10238           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10239               (gnus-summary-update-secondary-mark
10240                (cdr gnus-article-current))))
10241         ;; Prettify the article buffer again.
10242         (unless no-highlight
10243           (save-excursion
10244             (set-buffer gnus-article-buffer)
10245             ;;;!!! Fix this -- article should be rehighlighted.
10246             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10247             (set-buffer gnus-original-article-buffer)
10248             (gnus-request-article
10249              (cdr gnus-article-current)
10250              (car gnus-article-current) (current-buffer))))
10251         ;; Prettify the summary buffer line.
10252         (when (gnus-visual-p 'summary-highlight 'highlight)
10253           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10254
10255 (defun gnus-summary-edit-wash (key)
10256   "Perform editing command KEY in the article buffer."
10257   (interactive
10258    (list
10259     (progn
10260       (message "%s" (concat (this-command-keys) "- "))
10261       (read-char))))
10262   (message "")
10263   (gnus-summary-edit-article)
10264   (execute-kbd-macro (concat (this-command-keys) key))
10265   (gnus-article-edit-done))
10266
10267 ;;; Respooling
10268
10269 (defun gnus-summary-respool-query (&optional silent trace)
10270   "Query where the respool algorithm would put this article."
10271   (interactive)
10272   (let (gnus-mark-article-hook)
10273     (gnus-summary-select-article)
10274     (save-excursion
10275       (set-buffer gnus-original-article-buffer)
10276       (let ((groups (nnmail-article-group 'identity trace)))
10277         (unless silent
10278           (if groups
10279               (message "This message would go to %s"
10280                        (mapconcat 'car groups ", "))
10281             (message "This message would go to no groups"))
10282           groups)))))
10283
10284 (defun gnus-summary-respool-trace ()
10285   "Trace where the respool algorithm would put this article.
10286 Display a buffer showing all fancy splitting patterns which matched."
10287   (interactive)
10288   (gnus-summary-respool-query nil t))
10289
10290 ;; Summary marking commands.
10291
10292 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10293   "Mark articles which has the same subject as read, and then select the next.
10294 If UNMARK is positive, remove any kind of mark.
10295 If UNMARK is negative, tick articles."
10296   (interactive "P")
10297   (when unmark
10298     (setq unmark (prefix-numeric-value unmark)))
10299   (let ((count
10300          (gnus-summary-mark-same-subject
10301           (gnus-summary-article-subject) unmark)))
10302     ;; Select next unread article.  If auto-select-same mode, should
10303     ;; select the first unread article.
10304     (gnus-summary-next-article t (and gnus-auto-select-same
10305                                       (gnus-summary-article-subject)))
10306     (gnus-message 7 "%d article%s marked as %s"
10307                   count (if (= count 1) " is" "s are")
10308                   (if unmark "unread" "read"))))
10309
10310 (defun gnus-summary-kill-same-subject (&optional unmark)
10311   "Mark articles which has the same subject as read.
10312 If UNMARK is positive, remove any kind of mark.
10313 If UNMARK is negative, tick articles."
10314   (interactive "P")
10315   (when unmark
10316     (setq unmark (prefix-numeric-value unmark)))
10317   (let ((count
10318          (gnus-summary-mark-same-subject
10319           (gnus-summary-article-subject) unmark)))
10320     ;; If marked as read, go to next unread subject.
10321     (when (null unmark)
10322       ;; Go to next unread subject.
10323       (gnus-summary-next-subject 1 t))
10324     (gnus-message 7 "%d articles are marked as %s"
10325                   count (if unmark "unread" "read"))))
10326
10327 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10328   "Mark articles with same SUBJECT as read, and return marked number.
10329 If optional argument UNMARK is positive, remove any kinds of marks.
10330 If optional argument UNMARK is negative, mark articles as unread instead."
10331   (let ((count 1))
10332     (save-excursion
10333       (cond
10334        ((null unmark)                   ; Mark as read.
10335         (while (and
10336                 (progn
10337                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10338                   (gnus-summary-show-thread) t)
10339                 (gnus-summary-find-subject subject))
10340           (setq count (1+ count))))
10341        ((> unmark 0)                    ; Tick.
10342         (while (and
10343                 (progn
10344                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10345                   (gnus-summary-show-thread) t)
10346                 (gnus-summary-find-subject subject))
10347           (setq count (1+ count))))
10348        (t                               ; Mark as unread.
10349         (while (and
10350                 (progn
10351                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10352                   (gnus-summary-show-thread) t)
10353                 (gnus-summary-find-subject subject))
10354           (setq count (1+ count)))))
10355       (gnus-set-mode-line 'summary)
10356       ;; Return the number of marked articles.
10357       count)))
10358
10359 (defun gnus-summary-mark-as-processable (n &optional unmark)
10360   "Set the process mark on the next N articles.
10361 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10362 the process mark instead.  The difference between N and the actual
10363 number of articles marked is returned."
10364   (interactive "P")
10365   (if (and (null n) (gnus-region-active-p))
10366       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10367     (setq n (prefix-numeric-value n))
10368     (let ((backward (< n 0))
10369           (n (abs n)))
10370       (while (and
10371               (> n 0)
10372               (if unmark
10373                   (gnus-summary-remove-process-mark
10374                    (gnus-summary-article-number))
10375                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10376               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10377         (setq n (1- n)))
10378       (when (/= 0 n)
10379         (gnus-message 7 "No more articles"))
10380       (gnus-summary-recenter)
10381       (gnus-summary-position-point)
10382       n)))
10383
10384 (defun gnus-summary-unmark-as-processable (n)
10385   "Remove the process mark from the next N articles.
10386 If N is negative, unmark backward instead.  The difference between N and
10387 the actual number of articles unmarked is returned."
10388   (interactive "P")
10389   (gnus-summary-mark-as-processable n t))
10390
10391 (defun gnus-summary-unmark-all-processable ()
10392   "Remove the process mark from all articles."
10393   (interactive)
10394   (save-excursion
10395     (while gnus-newsgroup-processable
10396       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10397   (gnus-summary-position-point))
10398
10399 (defun gnus-summary-add-mark (article type)
10400   "Mark ARTICLE with a mark of TYPE."
10401   (let ((vtype (car (assq type gnus-article-mark-lists)))
10402         var)
10403     (if (not vtype)
10404         (error "No such mark type: %s" type)
10405       (setq var (intern (format "gnus-newsgroup-%s" type)))
10406       (set var (cons article (symbol-value var)))
10407       (if (memq type '(processable cached replied forwarded recent saved))
10408           (gnus-summary-update-secondary-mark article)
10409         ;;; !!! This is bogus.  We should find out what primary
10410         ;;; !!! mark we want to set.
10411         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10412
10413 (defun gnus-summary-mark-as-expirable (n)
10414   "Mark N articles forward as expirable.
10415 If N is negative, mark backward instead.  The difference between N and
10416 the actual number of articles marked is returned."
10417   (interactive "p")
10418   (gnus-summary-mark-forward n gnus-expirable-mark))
10419
10420 (defun gnus-summary-mark-as-spam (n)
10421   "Mark N articles forward as spam.
10422 If N is negative, mark backward instead.  The difference between N and
10423 the actual number of articles marked is returned."
10424   (interactive "p")
10425   (gnus-summary-mark-forward n gnus-spam-mark))
10426
10427 (defun gnus-summary-mark-article-as-replied (article)
10428   "Mark ARTICLE as replied to and update the summary line.
10429 ARTICLE can also be a list of articles."
10430   (interactive (list (gnus-summary-article-number)))
10431   (let ((articles (if (listp article) article (list article))))
10432     (dolist (article articles)
10433       (unless (numberp article)
10434         (error "%s is not a number" article))
10435       (push article gnus-newsgroup-replied)
10436       (let ((buffer-read-only nil))
10437         (when (gnus-summary-goto-subject article nil t)
10438           (gnus-summary-update-secondary-mark article))))))
10439
10440 (defun gnus-summary-mark-article-as-forwarded (article)
10441   "Mark ARTICLE as forwarded and update the summary line.
10442 ARTICLE can also be a list of articles."
10443   (let ((articles (if (listp article) article (list article))))
10444     (dolist (article articles)
10445       (push article gnus-newsgroup-forwarded)
10446       (let ((buffer-read-only nil))
10447         (when (gnus-summary-goto-subject article nil t)
10448           (gnus-summary-update-secondary-mark article))))))
10449
10450 (defun gnus-summary-set-bookmark (article)
10451   "Set a bookmark in current article."
10452   (interactive (list (gnus-summary-article-number)))
10453   (when (or (not (get-buffer gnus-article-buffer))
10454             (not gnus-current-article)
10455             (not gnus-article-current)
10456             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10457     (error "No current article selected"))
10458   ;; Remove old bookmark, if one exists.
10459   (gnus-pull article gnus-newsgroup-bookmarks)
10460   ;; Set the new bookmark, which is on the form
10461   ;; (article-number . line-number-in-body).
10462   (push
10463    (cons article
10464          (with-current-buffer gnus-article-buffer
10465            (count-lines
10466             (min (point)
10467                  (save-excursion
10468                    (article-goto-body)
10469                    (point)))
10470             (point))))
10471    gnus-newsgroup-bookmarks)
10472   (gnus-message 6 "A bookmark has been added to the current article."))
10473
10474 (defun gnus-summary-remove-bookmark (article)
10475   "Remove the bookmark from the current article."
10476   (interactive (list (gnus-summary-article-number)))
10477   ;; Remove old bookmark, if one exists.
10478   (if (not (assq article gnus-newsgroup-bookmarks))
10479       (gnus-message 6 "No bookmark in current article.")
10480     (gnus-pull article gnus-newsgroup-bookmarks)
10481     (gnus-message 6 "Removed bookmark.")))
10482
10483 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10484 (defun gnus-summary-mark-as-dormant (n)
10485   "Mark N articles forward as dormant.
10486 If N is negative, mark backward instead.  The difference between N and
10487 the actual number of articles marked is returned."
10488   (interactive "p")
10489   (gnus-summary-mark-forward n gnus-dormant-mark))
10490
10491 (defun gnus-summary-set-process-mark (article)
10492   "Set the process mark on ARTICLE and update the summary line."
10493   (setq gnus-newsgroup-processable
10494         (cons article
10495               (delq article gnus-newsgroup-processable)))
10496   (when (gnus-summary-goto-subject article)
10497     (gnus-summary-show-thread)
10498     (gnus-summary-goto-subject article)
10499     (gnus-summary-update-secondary-mark article)))
10500
10501 (defun gnus-summary-remove-process-mark (&rest articles)
10502   "Remove the process mark from ARTICLES and update the summary line."
10503   (dolist (article articles)
10504     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10505     (when (gnus-summary-goto-subject article)
10506       (gnus-summary-show-thread)
10507       (gnus-summary-goto-subject article)
10508       (gnus-summary-update-secondary-mark article)))
10509   t)
10510
10511 (defun gnus-summary-set-saved-mark (article)
10512   "Set the process mark on ARTICLE and update the summary line."
10513   (push article gnus-newsgroup-saved)
10514   (when (gnus-summary-goto-subject article)
10515     (gnus-summary-update-secondary-mark article)))
10516
10517 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10518   "Mark N articles as read forwards.
10519 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10520 The difference between N and the actual number of articles marked is
10521 returned.
10522 If NO-EXPIRE, auto-expiry will be inhibited."
10523   (interactive "p")
10524   (gnus-summary-show-thread)
10525   (let ((backward (< n 0))
10526         (gnus-summary-goto-unread
10527          (and gnus-summary-goto-unread
10528               (not (eq gnus-summary-goto-unread 'never))
10529               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10530                                     gnus-ticked-mark gnus-dormant-mark)))))
10531         (n (abs n))
10532         (mark (or mark gnus-del-mark)))
10533     (while (and (> n 0)
10534                 (gnus-summary-mark-article nil mark no-expire)
10535                 (zerop (gnus-summary-next-subject
10536                         (if backward -1 1)
10537                         (and gnus-summary-goto-unread
10538                              (not (eq gnus-summary-goto-unread 'never)))
10539                         t)))
10540       (setq n (1- n)))
10541     (when (/= 0 n)
10542       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10543     (gnus-summary-recenter)
10544     (gnus-summary-position-point)
10545     (gnus-set-mode-line 'summary)
10546     n))
10547
10548 (defun gnus-summary-mark-article-as-read (mark)
10549   "Mark the current article quickly as read with MARK."
10550   (let ((article (gnus-summary-article-number)))
10551     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10552     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10553     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10554     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10555     (push (cons article mark) gnus-newsgroup-reads)
10556     ;; Possibly remove from cache, if that is used.
10557     (when gnus-use-cache
10558       (gnus-cache-enter-remove-article article))
10559     ;; Allow the backend to change the mark.
10560     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10561     ;; Check for auto-expiry.
10562     (when (and gnus-newsgroup-auto-expire
10563                (memq mark gnus-auto-expirable-marks))
10564       (setq mark gnus-expirable-mark)
10565       ;; Let the backend know about the mark change.
10566       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10567       (push article gnus-newsgroup-expirable))
10568     ;; Set the mark in the buffer.
10569     (gnus-summary-update-mark mark 'unread)
10570     t))
10571
10572 (defun gnus-summary-mark-article-as-unread (mark)
10573   "Mark the current article quickly as unread with MARK."
10574   (let* ((article (gnus-summary-article-number))
10575          (old-mark (gnus-summary-article-mark article)))
10576     ;; Allow the backend to change the mark.
10577     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10578     (if (eq mark old-mark)
10579         t
10580       (if (<= article 0)
10581           (progn
10582             (gnus-error 1 "Can't mark negative article numbers")
10583             nil)
10584         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10585         (setq gnus-newsgroup-spam-marked
10586               (delq article gnus-newsgroup-spam-marked))
10587         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10588         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10589         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10590         (cond ((= mark gnus-ticked-mark)
10591                (setq gnus-newsgroup-marked
10592                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10593                                               article)))
10594               ((= mark gnus-spam-mark)
10595                (setq gnus-newsgroup-spam-marked
10596                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10597                                               article)))
10598               ((= mark gnus-dormant-mark)
10599                (setq gnus-newsgroup-dormant
10600                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10601                                               article)))
10602               (t
10603                (setq gnus-newsgroup-unreads
10604                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10605                                               article))))
10606         (gnus-pull article gnus-newsgroup-reads)
10607
10608         ;; See whether the article is to be put in the cache.
10609         (and gnus-use-cache
10610              (vectorp (gnus-summary-article-header article))
10611              (save-excursion
10612                (gnus-cache-possibly-enter-article
10613                 gnus-newsgroup-name article
10614                 (= mark gnus-ticked-mark)
10615                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10616
10617         ;; Fix the mark.
10618         (gnus-summary-update-mark mark 'unread)
10619         t))))
10620
10621 (defun gnus-summary-mark-article (&optional article mark no-expire)
10622   "Mark ARTICLE with MARK.  MARK can be any character.
10623 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10624 `??' (dormant) and `?E' (expirable).
10625 If MARK is nil, then the default character `?r' is used.
10626 If ARTICLE is nil, then the article on the current line will be
10627 marked.
10628 If NO-EXPIRE, auto-expiry will be inhibited."
10629   ;; The mark might be a string.
10630   (when (stringp mark)
10631     (setq mark (aref mark 0)))
10632   ;; If no mark is given, then we check auto-expiring.
10633   (when (null mark)
10634     (setq mark gnus-del-mark))
10635   (when (and (not no-expire)
10636              gnus-newsgroup-auto-expire
10637              (memq mark gnus-auto-expirable-marks))
10638     (setq mark gnus-expirable-mark))
10639   (let ((article (or article (gnus-summary-article-number)))
10640         (old-mark (gnus-summary-article-mark article)))
10641     ;; Allow the backend to change the mark.
10642     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10643     (if (eq mark old-mark)
10644         t
10645       (unless article
10646         (error "No article on current line"))
10647       (if (not (if (or (= mark gnus-unread-mark)
10648                        (= mark gnus-ticked-mark)
10649                        (= mark gnus-spam-mark)
10650                        (= mark gnus-dormant-mark))
10651                    (gnus-mark-article-as-unread article mark)
10652                  (gnus-mark-article-as-read article mark)))
10653           t
10654         ;; See whether the article is to be put in the cache.
10655         (and gnus-use-cache
10656              (not (= mark gnus-canceled-mark))
10657              (vectorp (gnus-summary-article-header article))
10658              (save-excursion
10659                (gnus-cache-possibly-enter-article
10660                 gnus-newsgroup-name article
10661                 (= mark gnus-ticked-mark)
10662                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10663
10664         (when (gnus-summary-goto-subject article nil t)
10665           (let ((buffer-read-only nil))
10666             (gnus-summary-show-thread)
10667             ;; Fix the mark.
10668             (gnus-summary-update-mark mark 'unread)
10669             t))))))
10670
10671 (defun gnus-summary-update-secondary-mark (article)
10672   "Update the secondary (read, process, cache) mark."
10673   (gnus-summary-update-mark
10674    (cond ((memq article gnus-newsgroup-processable)
10675           gnus-process-mark)
10676          ((memq article gnus-newsgroup-cached)
10677           gnus-cached-mark)
10678          ((memq article gnus-newsgroup-replied)
10679           gnus-replied-mark)
10680          ((memq article gnus-newsgroup-forwarded)
10681           gnus-forwarded-mark)
10682          ((memq article gnus-newsgroup-saved)
10683           gnus-saved-mark)
10684          ((memq article gnus-newsgroup-recent)
10685           gnus-recent-mark)
10686          ((memq article gnus-newsgroup-unseen)
10687           gnus-unseen-mark)
10688          (t gnus-no-mark))
10689    'replied)
10690   (when (gnus-visual-p 'summary-highlight 'highlight)
10691     (gnus-run-hooks 'gnus-summary-update-hook))
10692   t)
10693
10694 (defun gnus-summary-update-download-mark (article)
10695   "Update the download mark."
10696   (gnus-summary-update-mark
10697    (cond ((memq article gnus-newsgroup-undownloaded)
10698           gnus-undownloaded-mark)
10699          (gnus-newsgroup-agentized
10700           gnus-downloaded-mark)
10701          (t
10702           gnus-no-mark))
10703    'download)
10704   (gnus-summary-update-line t)
10705   t)
10706
10707 (defun gnus-summary-update-mark (mark type)
10708   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10709         (buffer-read-only nil))
10710     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10711     (when forward
10712       (when (looking-at "\r")
10713         (incf forward))
10714       (when (<= (+ forward (point)) (point-max))
10715         ;; Go to the right position on the line.
10716         (goto-char (+ forward (point)))
10717         ;; Replace the old mark with the new mark.
10718         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10719         ;; Optionally update the marks by some user rule.
10720         (when (eq type 'unread)
10721           (gnus-data-set-mark
10722            (gnus-data-find (gnus-summary-article-number)) mark)
10723           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10724
10725 (defun gnus-mark-article-as-read (article &optional mark)
10726   "Enter ARTICLE in the pertinent lists and remove it from others."
10727   ;; Make the article expirable.
10728   (let ((mark (or mark gnus-del-mark)))
10729     (setq gnus-newsgroup-expirable
10730           (if (= mark gnus-expirable-mark)
10731               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10732             (delq article gnus-newsgroup-expirable)))
10733     ;; Remove from unread and marked lists.
10734     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10735     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10736     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10737     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10738     (push (cons article mark) gnus-newsgroup-reads)
10739     ;; Possibly remove from cache, if that is used.
10740     (when gnus-use-cache
10741       (gnus-cache-enter-remove-article article))
10742     t))
10743
10744 (defun gnus-mark-article-as-unread (article &optional mark)
10745   "Enter ARTICLE in the pertinent lists and remove it from others."
10746   (let ((mark (or mark gnus-ticked-mark)))
10747     (if (<= article 0)
10748         (progn
10749           (gnus-error 1 "Can't mark negative article numbers")
10750           nil)
10751       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10752             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10753             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10754             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10755             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10756
10757       ;; Unsuppress duplicates?
10758       (when gnus-suppress-duplicates
10759         (gnus-dup-unsuppress-article article))
10760
10761       (cond ((= mark gnus-ticked-mark)
10762              (setq gnus-newsgroup-marked
10763                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10764             ((= mark gnus-spam-mark)
10765              (setq gnus-newsgroup-spam-marked
10766                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10767                                             article)))
10768             ((= mark gnus-dormant-mark)
10769              (setq gnus-newsgroup-dormant
10770                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10771             (t
10772              (setq gnus-newsgroup-unreads
10773                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10774       (gnus-pull article gnus-newsgroup-reads)
10775       t)))
10776
10777 (defalias 'gnus-summary-mark-as-unread-forward
10778   'gnus-summary-tick-article-forward)
10779 (make-obsolete 'gnus-summary-mark-as-unread-forward
10780                'gnus-summary-tick-article-forward)
10781 (defun gnus-summary-tick-article-forward (n)
10782   "Tick N articles forwards.
10783 If N is negative, tick backwards instead.
10784 The difference between N and the number of articles ticked is returned."
10785   (interactive "p")
10786   (gnus-summary-mark-forward n gnus-ticked-mark))
10787
10788 (defalias 'gnus-summary-mark-as-unread-backward
10789   'gnus-summary-tick-article-backward)
10790 (make-obsolete 'gnus-summary-mark-as-unread-backward
10791                'gnus-summary-tick-article-backward)
10792 (defun gnus-summary-tick-article-backward (n)
10793   "Tick N articles backwards.
10794 The difference between N and the number of articles ticked is returned."
10795   (interactive "p")
10796   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10797
10798 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10799 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10800 (defun gnus-summary-tick-article (&optional article clear-mark)
10801   "Mark current article as unread.
10802 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10803 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10804   (interactive)
10805   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10806                                        gnus-ticked-mark)))
10807
10808 (defun gnus-summary-mark-as-read-forward (n)
10809   "Mark N articles as read forwards.
10810 If N is negative, mark backwards instead.
10811 The difference between N and the actual number of articles marked is
10812 returned."
10813   (interactive "p")
10814   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10815
10816 (defun gnus-summary-mark-as-read-backward (n)
10817   "Mark the N articles as read backwards.
10818 The difference between N and the actual number of articles marked is
10819 returned."
10820   (interactive "p")
10821   (gnus-summary-mark-forward
10822    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10823
10824 (defun gnus-summary-mark-as-read (&optional article mark)
10825   "Mark current article as read.
10826 ARTICLE specifies the article to be marked as read.
10827 MARK specifies a string to be inserted at the beginning of the line."
10828   (gnus-summary-mark-article article mark))
10829
10830 (defun gnus-summary-clear-mark-forward (n)
10831   "Clear marks from N articles forward.
10832 If N is negative, clear backward instead.
10833 The difference between N and the number of marks cleared is returned."
10834   (interactive "p")
10835   (gnus-summary-mark-forward n gnus-unread-mark))
10836
10837 (defun gnus-summary-clear-mark-backward (n)
10838   "Clear marks from N articles backward.
10839 The difference between N and the number of marks cleared is returned."
10840   (interactive "p")
10841   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10842
10843 (defun gnus-summary-mark-unread-as-read ()
10844   "Intended to be used by `gnus-mark-article-hook'."
10845   (when (memq gnus-current-article gnus-newsgroup-unreads)
10846     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10847
10848 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10849   "Intended to be used by `gnus-mark-article-hook'."
10850   (let ((mark (gnus-summary-article-mark)))
10851     (when (or (gnus-unread-mark-p mark)
10852               (gnus-read-mark-p mark))
10853       (gnus-summary-mark-article gnus-current-article
10854                                  (or new-mark gnus-read-mark)))))
10855
10856 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10857   "Intended to be used by `gnus-mark-article-hook'."
10858   (let ((mark (gnus-summary-article-mark)))
10859     (when (or (gnus-unread-mark-p mark)
10860               (gnus-read-mark-p mark))
10861       (gnus-summary-mark-article (gnus-summary-article-number)
10862                                  (or new-mark gnus-read-mark)))))
10863
10864 (defun gnus-summary-mark-unread-as-ticked ()
10865   "Intended to be used by `gnus-mark-article-hook'."
10866   (when (memq gnus-current-article gnus-newsgroup-unreads)
10867     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10868
10869 (defun gnus-summary-mark-region-as-read (point mark all)
10870   "Mark all unread articles between point and mark as read.
10871 If given a prefix, mark all articles between point and mark as read,
10872 even ticked and dormant ones."
10873   (interactive "r\nP")
10874   (save-excursion
10875     (let (article)
10876       (goto-char point)
10877       (beginning-of-line)
10878       (while (and
10879               (< (point) mark)
10880               (progn
10881                 (when (or all
10882                           (memq (setq article (gnus-summary-article-number))
10883                                 gnus-newsgroup-unreads))
10884                   (gnus-summary-mark-article article gnus-del-mark))
10885                 t)
10886               (gnus-summary-find-next))))))
10887
10888 (defun gnus-summary-mark-below (score mark)
10889   "Mark articles with score less than SCORE with MARK."
10890   (interactive "P\ncMark: ")
10891   (setq score (if score
10892                   (prefix-numeric-value score)
10893                 (or gnus-summary-default-score 0)))
10894   (save-excursion
10895     (set-buffer gnus-summary-buffer)
10896     (goto-char (point-min))
10897     (while
10898         (progn
10899           (and (< (gnus-summary-article-score) score)
10900                (gnus-summary-mark-article nil mark))
10901           (gnus-summary-find-next)))))
10902
10903 (defun gnus-summary-kill-below (&optional score)
10904   "Mark articles with score below SCORE as read."
10905   (interactive "P")
10906   (gnus-summary-mark-below score gnus-killed-mark))
10907
10908 (defun gnus-summary-clear-above (&optional score)
10909   "Clear all marks from articles with score above SCORE."
10910   (interactive "P")
10911   (gnus-summary-mark-above score gnus-unread-mark))
10912
10913 (defun gnus-summary-tick-above (&optional score)
10914   "Tick all articles with score above SCORE."
10915   (interactive "P")
10916   (gnus-summary-mark-above score gnus-ticked-mark))
10917
10918 (defun gnus-summary-mark-above (score mark)
10919   "Mark articles with score over SCORE with MARK."
10920   (interactive "P\ncMark: ")
10921   (setq score (if score
10922                   (prefix-numeric-value score)
10923                 (or gnus-summary-default-score 0)))
10924   (save-excursion
10925     (set-buffer gnus-summary-buffer)
10926     (goto-char (point-min))
10927     (while (and (progn
10928                   (when (> (gnus-summary-article-score) score)
10929                     (gnus-summary-mark-article nil mark))
10930                   t)
10931                 (gnus-summary-find-next)))))
10932
10933 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10934 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10935 (defun gnus-summary-limit-include-expunged (&optional no-error)
10936   "Display all the hidden articles that were expunged for low scores."
10937   (interactive)
10938   (let ((buffer-read-only nil))
10939     (let ((scored gnus-newsgroup-scored)
10940           headers h)
10941       (while scored
10942         (unless (gnus-summary-article-header (caar scored))
10943           (and (setq h (gnus-number-to-header (caar scored)))
10944                (< (cdar scored) gnus-summary-expunge-below)
10945                (push h headers)))
10946         (setq scored (cdr scored)))
10947       (if (not headers)
10948           (when (not no-error)
10949             (error "No expunged articles hidden"))
10950         (goto-char (point-min))
10951         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10952         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10953         (dolist (x headers)
10954           (push (mail-header-number x) gnus-newsgroup-limit))
10955         (gnus-summary-prepare-unthreaded (nreverse headers))
10956         (goto-char (point-min))
10957         (gnus-summary-position-point)
10958         t))))
10959
10960 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10961   "Mark all unread articles in this newsgroup as read.
10962 If prefix argument ALL is non-nil, ticked and dormant articles will
10963 also be marked as read.
10964 If QUIETLY is non-nil, no questions will be asked.
10965
10966 If TO-HERE is non-nil, it should be a point in the buffer.  All
10967 articles before (after, if REVERSE is set) this point will be marked
10968 as read.
10969
10970 Note that this function will only catch up the unread article
10971 in the current summary buffer limitation.
10972
10973 The number of articles marked as read is returned."
10974   (interactive "P")
10975   (prog1
10976       (save-excursion
10977         (when (or quietly
10978                   (not gnus-interactive-catchup) ;Without confirmation?
10979                   gnus-expert-user
10980                   (gnus-y-or-n-p
10981                    (if all
10982                        "Mark absolutely all articles as read? "
10983                      "Mark all unread articles as read? ")))
10984           (if (and not-mark
10985                    (not gnus-newsgroup-adaptive)
10986                    (not gnus-newsgroup-auto-expire)
10987                    (not gnus-suppress-duplicates)
10988                    (or (not gnus-use-cache)
10989                        (eq gnus-use-cache 'passive)))
10990               (progn
10991                 (when all
10992                   (setq gnus-newsgroup-marked nil
10993                         gnus-newsgroup-spam-marked nil
10994                         gnus-newsgroup-dormant nil))
10995                 (setq gnus-newsgroup-unreads
10996                       (gnus-sorted-nunion
10997                        (gnus-sorted-intersection gnus-newsgroup-unreads
10998                                                  gnus-newsgroup-downloadable)
10999                        (gnus-sorted-difference gnus-newsgroup-unfetched
11000                                                gnus-newsgroup-cached))))
11001             ;; We actually mark all articles as canceled, which we
11002             ;; have to do when using auto-expiry or adaptive scoring.
11003             (gnus-summary-show-all-threads)
11004             (if (and to-here reverse)
11005                 (progn
11006                   (goto-char to-here)
11007                   (gnus-summary-mark-current-read-and-unread-as-read
11008                    gnus-catchup-mark)
11009                   (while (gnus-summary-find-next (not all))
11010                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11011               (when (gnus-summary-first-subject (not all))
11012                 (while (and
11013                         (if to-here (< (point) to-here) t)
11014                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11015                         (gnus-summary-find-next (not all))))))
11016             (gnus-set-mode-line 'summary))
11017           t))
11018     (gnus-summary-position-point)))
11019
11020 (defun gnus-summary-catchup-to-here (&optional all)
11021   "Mark all unticked articles before the current one as read.
11022 If ALL is non-nil, also mark ticked and dormant articles as read."
11023   (interactive "P")
11024   (save-excursion
11025     (gnus-save-hidden-threads
11026       (let ((beg (point)))
11027         ;; We check that there are unread articles.
11028         (when (or all (gnus-summary-find-prev))
11029           (gnus-summary-catchup all t beg)))))
11030   (gnus-summary-position-point))
11031
11032 (defun gnus-summary-catchup-from-here (&optional all)
11033   "Mark all unticked articles after (and including) the current one as read.
11034 If ALL is non-nil, also mark ticked and dormant articles as read."
11035   (interactive "P")
11036   (save-excursion
11037     (gnus-save-hidden-threads
11038       (let ((beg (point)))
11039         ;; We check that there are unread articles.
11040         (when (or all (gnus-summary-find-next))
11041           (gnus-summary-catchup all t beg nil t)))))
11042   (gnus-summary-position-point))
11043
11044 (defun gnus-summary-catchup-all (&optional quietly)
11045   "Mark all articles in this newsgroup as read.
11046 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11047 instead, which marks only unread articles as read."
11048   (interactive "P")
11049   (gnus-summary-catchup t quietly))
11050
11051 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11052   "Mark all unread articles in this group as read, then exit.
11053 If prefix argument ALL is non-nil, all articles are marked as read.
11054 If QUIETLY is non-nil, no questions will be asked."
11055   (interactive "P")
11056   (when (gnus-summary-catchup all quietly nil 'fast)
11057     ;; Select next newsgroup or exit.
11058     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11059              (eq gnus-auto-select-next 'quietly))
11060         (gnus-summary-next-group nil)
11061       (gnus-summary-exit))))
11062
11063 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11064   "Mark all articles in this newsgroup as read, and then exit.
11065 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11066 instead, which marks only unread articles as read."
11067   (interactive "P")
11068   (gnus-summary-catchup-and-exit t quietly))
11069
11070 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11071   "Mark all articles in this group as read and select the next group.
11072 If given a prefix, mark all articles, unread as well as ticked, as
11073 read."
11074   (interactive "P")
11075   (save-excursion
11076     (gnus-summary-catchup all))
11077   (gnus-summary-next-group))
11078
11079 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11080   "Mark all articles in this group as read and select the previous group.
11081 If given a prefix, mark all articles, unread as well as ticked, as
11082 read."
11083   (interactive "P")
11084   (save-excursion
11085     (gnus-summary-catchup all))
11086   (gnus-summary-next-group nil nil t))
11087
11088 ;;;
11089 ;;; with article
11090 ;;;
11091
11092 (defmacro gnus-with-article (article &rest forms)
11093   "Select ARTICLE and perform FORMS in the original article buffer.
11094 Then replace the article with the result."
11095   `(progn
11096      ;; We don't want the article to be marked as read.
11097      (let (gnus-mark-article-hook)
11098        (gnus-summary-select-article t t nil ,article))
11099      (set-buffer gnus-original-article-buffer)
11100      ,@forms
11101      (if (not (gnus-check-backend-function
11102                'request-replace-article (car gnus-article-current)))
11103          (gnus-message 5 "Read-only group; not replacing")
11104        (unless (gnus-request-replace-article
11105                 ,article (car gnus-article-current)
11106                 (current-buffer) t)
11107          (error "Couldn't replace article")))
11108      ;; The cache and backlog have to be flushed somewhat.
11109      (when gnus-keep-backlog
11110        (gnus-backlog-remove-article
11111         (car gnus-article-current) (cdr gnus-article-current)))
11112      (when gnus-use-cache
11113        (gnus-cache-update-article
11114         (car gnus-article-current) (cdr gnus-article-current)))))
11115
11116 (put 'gnus-with-article 'lisp-indent-function 1)
11117 (put 'gnus-with-article 'edebug-form-spec '(form body))
11118
11119 ;; Thread-based commands.
11120
11121 (defun gnus-summary-articles-in-thread (&optional article)
11122   "Return a list of all articles in the current thread.
11123 If ARTICLE is non-nil, return all articles in the thread that starts
11124 with that article."
11125   (let* ((article (or article (gnus-summary-article-number)))
11126          (data (gnus-data-find-list article))
11127          (top-level (gnus-data-level (car data)))
11128          (top-subject
11129           (cond ((null gnus-thread-operation-ignore-subject)
11130                  (gnus-simplify-subject-re
11131                   (mail-header-subject (gnus-data-header (car data)))))
11132                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11133                  (gnus-simplify-subject-fuzzy
11134                   (mail-header-subject (gnus-data-header (car data)))))
11135                 (t nil)))
11136          (end-point (save-excursion
11137                       (if (gnus-summary-go-to-next-thread)
11138                           (point) (point-max))))
11139          articles)
11140     (while (and data
11141                 (< (gnus-data-pos (car data)) end-point))
11142       (when (or (not top-subject)
11143                 (string= top-subject
11144                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11145                              (gnus-simplify-subject-fuzzy
11146                               (mail-header-subject
11147                                (gnus-data-header (car data))))
11148                            (gnus-simplify-subject-re
11149                             (mail-header-subject
11150                              (gnus-data-header (car data)))))))
11151         (push (gnus-data-number (car data)) articles))
11152       (unless (and (setq data (cdr data))
11153                    (> (gnus-data-level (car data)) top-level))
11154         (setq data nil)))
11155     ;; Return the list of articles.
11156     (nreverse articles)))
11157
11158 (defun gnus-summary-rethread-current ()
11159   "Rethread the thread the current article is part of."
11160   (interactive)
11161   (let* ((gnus-show-threads t)
11162          (article (gnus-summary-article-number))
11163          (id (mail-header-id (gnus-summary-article-header)))
11164          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11165     (unless id
11166       (error "No article on the current line"))
11167     (gnus-rebuild-thread id)
11168     (gnus-summary-goto-subject article)))
11169
11170 (defun gnus-summary-reparent-thread ()
11171   "Make the current article child of the marked (or previous) article.
11172
11173 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11174 is non-nil or the Subject: of both articles are the same."
11175   (interactive)
11176   (unless (not (gnus-group-read-only-p))
11177     (error "The current newsgroup does not support article editing"))
11178   (unless (<= (length gnus-newsgroup-processable) 1)
11179     (error "No more than one article may be marked"))
11180   (let ((child (gnus-summary-article-number))
11181         ;; First grab the marked article, otherwise one line up.
11182         (parent (if (not (null gnus-newsgroup-processable))
11183                     (car gnus-newsgroup-processable)
11184                   (save-excursion
11185                     (if (eq (forward-line -1) 0)
11186                         (gnus-summary-article-number)
11187                       (error "Beginning of summary buffer"))))))
11188     (gnus-summary-reparent-children parent (list child))))
11189
11190 (defun gnus-summary-reparent-children (parent children)
11191   "Make PARENT the parent of CHILDREN.
11192 When called interactively, PARENT is the current article and CHILDREN
11193 are the process-marked articles."
11194   (interactive
11195    (list (gnus-summary-article-number)
11196          (gnus-summary-work-articles nil)))
11197   (dolist (child children)
11198     (save-window-excursion
11199       (let ((gnus-article-buffer " *reparent*"))
11200         (unless (not (eq parent child))
11201           (error "An article may not be self-referential"))
11202         (let ((message-id (mail-header-id
11203                            (gnus-summary-article-header parent))))
11204           (unless (and message-id (not (equal message-id "")))
11205             (error "No message-id in desired parent"))
11206           (gnus-with-article child
11207             (save-restriction
11208               (goto-char (point-min))
11209               (message-narrow-to-head)
11210               (if (re-search-forward "^References: " nil t)
11211                   (progn
11212                     (re-search-forward "^[^ \t]" nil t)
11213                     (forward-line -1)
11214                     (end-of-line)
11215                     (insert " " message-id))
11216                 (insert "References: " message-id "\n"))))
11217           (set-buffer gnus-summary-buffer)
11218           (gnus-summary-unmark-all-processable)
11219           (gnus-summary-update-article child)
11220           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11221             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11222           (gnus-summary-rethread-current)
11223           (gnus-message 3 "Article %d is now the child of article %d"
11224                         child parent))))))
11225
11226 (defun gnus-summary-toggle-threads (&optional arg)
11227   "Toggle showing conversation threads.
11228 If ARG is positive number, turn showing conversation threads on."
11229   (interactive "P")
11230   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11231     (setq gnus-show-threads
11232           (if (null arg) (not gnus-show-threads)
11233             (> (prefix-numeric-value arg) 0)))
11234     (gnus-summary-prepare)
11235     (gnus-summary-goto-subject current)
11236     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11237     (gnus-summary-position-point)))
11238
11239 (defun gnus-summary-show-all-threads ()
11240   "Show all threads."
11241   (interactive)
11242   (save-excursion
11243     (let ((buffer-read-only nil))
11244       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
11245   (gnus-summary-position-point))
11246
11247 (defun gnus-summary-show-thread ()
11248   "Show thread subtrees.
11249 Returns nil if no thread was there to be shown."
11250   (interactive)
11251   (let ((buffer-read-only nil)
11252         (orig (point))
11253         (end (point-at-eol))
11254         ;; Leave point at bol
11255         (beg (progn (beginning-of-line) (point))))
11256     (prog1
11257         ;; Any hidden lines here?
11258         (search-forward "\r" end t)
11259       (subst-char-in-region beg end ?\^M ?\n t)
11260       (goto-char orig)
11261       (gnus-summary-position-point))))
11262
11263 (defun gnus-summary-maybe-hide-threads ()
11264   "If requested, hide the threads that should be hidden."
11265   (when (and gnus-show-threads
11266              gnus-thread-hide-subtree)
11267     (gnus-summary-hide-all-threads
11268      (if (or (consp gnus-thread-hide-subtree)
11269              (functionp gnus-thread-hide-subtree))
11270          (gnus-make-predicate gnus-thread-hide-subtree)
11271        nil))))
11272
11273 ;;; Hiding predicates.
11274
11275 (defun gnus-article-unread-p (header)
11276   (memq (mail-header-number header) gnus-newsgroup-unreads))
11277
11278 (defun gnus-article-unseen-p (header)
11279   (memq (mail-header-number header) gnus-newsgroup-unseen))
11280
11281 (defun gnus-map-articles (predicate articles)
11282   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11283   (apply 'gnus-or (mapcar predicate
11284                           (mapcar (lambda (number)
11285                                     (gnus-summary-article-header number))
11286                                   articles))))
11287
11288 (defun gnus-summary-hide-all-threads (&optional predicate)
11289   "Hide all thread subtrees.
11290 If PREDICATE is supplied, threads that satisfy this predicate
11291 will not be hidden."
11292   (interactive)
11293   (save-excursion
11294     (goto-char (point-min))
11295     (let ((end nil))
11296       (while (not end)
11297         (when (or (not predicate)
11298                   (gnus-map-articles
11299                    predicate (gnus-summary-article-children)))
11300             (gnus-summary-hide-thread))
11301         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11302   (gnus-summary-position-point))
11303
11304 (defun gnus-summary-hide-thread ()
11305   "Hide thread subtrees.
11306 If PREDICATE is supplied, threads that satisfy this predicate
11307 will not be hidden.
11308 Returns nil if no threads were there to be hidden."
11309   (interactive)
11310   (let ((buffer-read-only nil)
11311         (start (point))
11312         (article (gnus-summary-article-number)))
11313     (goto-char start)
11314     ;; Go forward until either the buffer ends or the subthread
11315     ;; ends.
11316     (when (and (not (eobp))
11317                (or (zerop (gnus-summary-next-thread 1 t))
11318                    (goto-char (point-max))))
11319       (prog1
11320           (if (and (> (point) start)
11321                    (search-backward "\n" start t))
11322               (progn
11323                 (subst-char-in-region start (point) ?\n ?\^M)
11324                 (gnus-summary-goto-subject article))
11325             (goto-char start)
11326             nil)))))
11327
11328 (defun gnus-summary-go-to-next-thread (&optional previous)
11329   "Go to the same level (or less) next thread.
11330 If PREVIOUS is non-nil, go to previous thread instead.
11331 Return the article number moved to, or nil if moving was impossible."
11332   (let ((level (gnus-summary-thread-level))
11333         (way (if previous -1 1))
11334         (beg (point)))
11335     (forward-line way)
11336     (while (and (not (eobp))
11337                 (< level (gnus-summary-thread-level)))
11338       (forward-line way))
11339     (if (eobp)
11340         (progn
11341           (goto-char beg)
11342           nil)
11343       (setq beg (point))
11344       (prog1
11345           (gnus-summary-article-number)
11346         (goto-char beg)))))
11347
11348 (defun gnus-summary-next-thread (n &optional silent)
11349   "Go to the same level next N'th thread.
11350 If N is negative, search backward instead.
11351 Returns the difference between N and the number of skips actually
11352 done.
11353
11354 If SILENT, don't output messages."
11355   (interactive "p")
11356   (let ((backward (< n 0))
11357         (n (abs n)))
11358     (while (and (> n 0)
11359                 (gnus-summary-go-to-next-thread backward))
11360       (decf n))
11361     (unless silent
11362       (gnus-summary-position-point))
11363     (when (and (not silent) (/= 0 n))
11364       (gnus-message 7 "No more threads"))
11365     n))
11366
11367 (defun gnus-summary-prev-thread (n)
11368   "Go to the same level previous N'th thread.
11369 Returns the difference between N and the number of skips actually
11370 done."
11371   (interactive "p")
11372   (gnus-summary-next-thread (- n)))
11373
11374 (defun gnus-summary-go-down-thread ()
11375   "Go down one level in the current thread."
11376   (let ((children (gnus-summary-article-children)))
11377     (when children
11378       (gnus-summary-goto-subject (car children)))))
11379
11380 (defun gnus-summary-go-up-thread ()
11381   "Go up one level in the current thread."
11382   (let ((parent (gnus-summary-article-parent)))
11383     (when parent
11384       (gnus-summary-goto-subject parent))))
11385
11386 (defun gnus-summary-down-thread (n)
11387   "Go down thread N steps.
11388 If N is negative, go up instead.
11389 Returns the difference between N and how many steps down that were
11390 taken."
11391   (interactive "p")
11392   (let ((up (< n 0))
11393         (n (abs n)))
11394     (while (and (> n 0)
11395                 (if up (gnus-summary-go-up-thread)
11396                   (gnus-summary-go-down-thread)))
11397       (setq n (1- n)))
11398     (gnus-summary-position-point)
11399     (when (/= 0 n)
11400       (gnus-message 7 "Can't go further"))
11401     n))
11402
11403 (defun gnus-summary-up-thread (n)
11404   "Go up thread N steps.
11405 If N is negative, go down instead.
11406 Returns the difference between N and how many steps down that were
11407 taken."
11408   (interactive "p")
11409   (gnus-summary-down-thread (- n)))
11410
11411 (defun gnus-summary-top-thread ()
11412   "Go to the top of the thread."
11413   (interactive)
11414   (while (gnus-summary-go-up-thread))
11415   (gnus-summary-article-number))
11416
11417 (defun gnus-summary-expire-thread ()
11418   "Mark articles under current thread as expired."
11419   (interactive)
11420   (gnus-summary-kill-thread 0))
11421
11422 (defun gnus-summary-kill-thread (&optional unmark)
11423   "Mark articles under current thread as read.
11424 If the prefix argument is positive, remove any kinds of marks.
11425 If the prefix argument is zero, mark thread as expired.
11426 If the prefix argument is negative, tick articles instead."
11427   (interactive "P")
11428   (when unmark
11429     (setq unmark (prefix-numeric-value unmark)))
11430   (let ((articles (gnus-summary-articles-in-thread))
11431         (hide (or (null unmark) (= unmark 0))))
11432     (save-excursion
11433       ;; Expand the thread.
11434       (gnus-summary-show-thread)
11435       ;; Mark all the articles.
11436       (while articles
11437         (gnus-summary-goto-subject (car articles))
11438         (cond ((null unmark)
11439                (gnus-summary-mark-article-as-read gnus-killed-mark))
11440               ((> unmark 0)
11441                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11442               ((= unmark 0)
11443                (gnus-summary-mark-article-as-unread gnus-expirable-mark))
11444               (t
11445                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11446         (setq articles (cdr articles))))
11447     ;; Hide killed subtrees when hide is true.
11448     (and hide
11449          gnus-thread-hide-killed
11450          (gnus-summary-hide-thread))
11451     ;; If hide is t, go to next unread subject.
11452     (when hide
11453       ;; Go to next unread subject.
11454       (gnus-summary-next-subject 1 t)))
11455   (gnus-set-mode-line 'summary))
11456
11457 ;; Summary sorting commands
11458
11459 (defun gnus-summary-sort-by-number (&optional reverse)
11460   "Sort the summary buffer by article number.
11461 Argument REVERSE means reverse order."
11462   (interactive "P")
11463   (gnus-summary-sort 'number reverse))
11464
11465 (defun gnus-summary-sort-by-random (&optional reverse)
11466   "Randomize the order in the summary buffer.
11467 Argument REVERSE means to randomize in reverse order."
11468   (interactive "P")
11469   (gnus-summary-sort 'random reverse))
11470
11471 (defun gnus-summary-sort-by-author (&optional reverse)
11472   "Sort the summary buffer by author name alphabetically.
11473 If `case-fold-search' is non-nil, case of letters is ignored.
11474 Argument REVERSE means reverse order."
11475   (interactive "P")
11476   (gnus-summary-sort 'author reverse))
11477
11478 (defun gnus-summary-sort-by-recipient (&optional reverse)
11479   "Sort the summary buffer by recipient name alphabetically.
11480 If `case-fold-search' is non-nil, case of letters is ignored.
11481 Argument REVERSE means reverse order."
11482   (interactive "P")
11483   (gnus-summary-sort 'recipient reverse))
11484
11485 (defun gnus-summary-sort-by-subject (&optional reverse)
11486   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11487 If `case-fold-search' is non-nil, case of letters is ignored.
11488 Argument REVERSE means reverse order."
11489   (interactive "P")
11490   (gnus-summary-sort 'subject reverse))
11491
11492 (defun gnus-summary-sort-by-date (&optional reverse)
11493   "Sort the summary buffer by date.
11494 Argument REVERSE means reverse order."
11495   (interactive "P")
11496   (gnus-summary-sort 'date reverse))
11497
11498 (defun gnus-summary-sort-by-score (&optional reverse)
11499   "Sort the summary buffer by score.
11500 Argument REVERSE means reverse order."
11501   (interactive "P")
11502   (gnus-summary-sort 'score reverse))
11503
11504 (defun gnus-summary-sort-by-lines (&optional reverse)
11505   "Sort the summary buffer by the number of lines.
11506 Argument REVERSE means reverse order."
11507   (interactive "P")
11508   (gnus-summary-sort 'lines reverse))
11509
11510 (defun gnus-summary-sort-by-chars (&optional reverse)
11511   "Sort the summary buffer by article length.
11512 Argument REVERSE means reverse order."
11513   (interactive "P")
11514   (gnus-summary-sort 'chars reverse))
11515
11516 (defun gnus-summary-sort-by-original (&optional reverse)
11517   "Sort the summary buffer using the default sorting method.
11518 Argument REVERSE means reverse order."
11519   (interactive "P")
11520   (let* ((buffer-read-only)
11521          (gnus-summary-prepare-hook nil))
11522     ;; We do the sorting by regenerating the threads.
11523     (gnus-summary-prepare)
11524     ;; Hide subthreads if needed.
11525     (gnus-summary-maybe-hide-threads)))
11526
11527 (defun gnus-summary-sort (predicate reverse)
11528   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11529   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11530          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11531          (gnus-thread-sort-functions
11532           (if (not reverse)
11533               thread
11534             `(lambda (t1 t2)
11535                (,thread t2 t1))))
11536          (gnus-sort-gathered-threads-function
11537           gnus-thread-sort-functions)
11538          (gnus-article-sort-functions
11539           (if (not reverse)
11540               article
11541             `(lambda (t1 t2)
11542                (,article t2 t1))))
11543          (buffer-read-only)
11544          (gnus-summary-prepare-hook nil))
11545     ;; We do the sorting by regenerating the threads.
11546     (gnus-summary-prepare)
11547     ;; Hide subthreads if needed.
11548     (gnus-summary-maybe-hide-threads)))
11549
11550 ;; Summary saving commands.
11551
11552 (defun gnus-summary-save-article (&optional n not-saved)
11553   "Save the current article using the default saver function.
11554 If N is a positive number, save the N next articles.
11555 If N is a negative number, save the N previous articles.
11556 If N is nil and any articles have been marked with the process mark,
11557 save those articles instead.
11558 The variable `gnus-default-article-saver' specifies the saver function.
11559
11560 If the optional second argument NOT-SAVED is non-nil, articles saved
11561 will not be marked as saved."
11562   (interactive "P")
11563   (require 'gnus-art)
11564   (let* ((articles (gnus-summary-work-articles n))
11565          (save-buffer (save-excursion
11566                         (nnheader-set-temp-buffer " *Gnus Save*")))
11567          (num (length articles))
11568          ;; Whether to save decoded articles or raw articles.
11569          (decode (when gnus-article-save-coding-system
11570                    (get gnus-default-article-saver :decode)))
11571          ;; When saving many articles in a single file, use the other
11572          ;; function to save articles other than the first one.
11573          (saver2 (get gnus-default-article-saver :function))
11574          (gnus-prompt-before-saving (if saver2
11575                                         t
11576                                       gnus-prompt-before-saving))
11577          (gnus-default-article-saver gnus-default-article-saver)
11578          header file)
11579     (dolist (article articles)
11580       (setq header (gnus-summary-article-header article))
11581       (if (not (vectorp header))
11582           ;; This is a pseudo-article.
11583           (if (assq 'name header)
11584               (gnus-copy-file (cdr (assq 'name header)))
11585             (gnus-message 1 "Article %d is unsaveable" article))
11586         ;; This is a real article.
11587         (save-window-excursion
11588           (let ((gnus-display-mime-function (when decode
11589                                               gnus-display-mime-function))
11590                 (gnus-article-prepare-hook (when decode
11591                                              gnus-article-prepare-hook)))
11592             (gnus-summary-select-article t nil nil article)
11593             (gnus-summary-goto-subject article)))
11594         (save-excursion
11595           (set-buffer save-buffer)
11596           (erase-buffer)
11597           (insert-buffer-substring (if decode
11598                                        gnus-article-buffer
11599                                      gnus-original-article-buffer)))
11600         (setq file (gnus-article-save save-buffer file num))
11601         (gnus-summary-remove-process-mark article)
11602         (unless not-saved
11603           (gnus-summary-set-saved-mark article)))
11604       (when saver2
11605         (setq gnus-default-article-saver saver2
11606               saver2 nil)))
11607     (gnus-kill-buffer save-buffer)
11608     (gnus-summary-position-point)
11609     (gnus-set-mode-line 'summary)
11610     n))
11611
11612 (defun gnus-summary-pipe-output (&optional arg headers)
11613   "Pipe the current article to a subprocess.
11614 If N is a positive number, pipe the N next articles.
11615 If N is a negative number, pipe the N previous articles.
11616 If N is nil and any articles have been marked with the process mark,
11617 pipe those articles instead.
11618 If HEADERS (the symbolic prefix), include the headers, too."
11619   (interactive (gnus-interactive "P\ny"))
11620   (require 'gnus-art)
11621   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11622         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11623     (gnus-summary-save-article arg t))
11624   (let ((buffer (get-buffer "*Shell Command Output*")))
11625     (when (and buffer
11626                (not (zerop (buffer-size buffer))))
11627       (gnus-configure-windows 'pipe))))
11628
11629 (defun gnus-summary-save-article-mail (&optional arg)
11630   "Append the current article to a Unix mail box file.
11631 If N is a positive number, save the N next articles.
11632 If N is a negative number, save the N previous articles.
11633 If N is nil and any articles have been marked with the process mark,
11634 save those articles instead."
11635   (interactive "P")
11636   (require 'gnus-art)
11637   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11638     (gnus-summary-save-article arg)))
11639
11640 (defun gnus-summary-save-article-rmail (&optional arg)
11641   "Append the current article to an rmail file.
11642 If N is a positive number, save the N next articles.
11643 If N is a negative number, save the N previous articles.
11644 If N is nil and any articles have been marked with the process mark,
11645 save those articles instead."
11646   (interactive "P")
11647   (require 'gnus-art)
11648   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11649     (gnus-summary-save-article arg)))
11650
11651 (defun gnus-summary-save-article-file (&optional arg)
11652   "Append the current article to a file.
11653 If N is a positive number, save the N next articles.
11654 If N is a negative number, save the N previous articles.
11655 If N is nil and any articles have been marked with the process mark,
11656 save those articles instead."
11657   (interactive "P")
11658   (require 'gnus-art)
11659   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11660     (gnus-summary-save-article arg)))
11661
11662 (defun gnus-summary-write-article-file (&optional arg)
11663   "Write the current article to a file, deleting the previous file.
11664 If N is a positive number, save the N next articles.
11665 If N is a negative number, save the N previous articles.
11666 If N is nil and any articles have been marked with the process mark,
11667 save those articles instead."
11668   (interactive "P")
11669   (require 'gnus-art)
11670   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11671     (gnus-summary-save-article arg)))
11672
11673 (defun gnus-summary-save-article-body-file (&optional arg)
11674   "Append the current article body to a file.
11675 If N is a positive number, save the N next articles.
11676 If N is a negative number, save the N previous articles.
11677 If N is nil and any articles have been marked with the process mark,
11678 save those articles instead."
11679   (interactive "P")
11680   (require 'gnus-art)
11681   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11682     (gnus-summary-save-article arg)))
11683
11684 (defun gnus-summary-write-article-body-file (&optional arg)
11685   "Write the current article body to a file, deleting the previous file.
11686 If N is a positive number, save the N next articles.
11687 If N is a negative number, save the N previous articles.
11688 If N is nil and any articles have been marked with the process mark,
11689 save those articles instead."
11690   (interactive "P")
11691   (require 'gnus-art)
11692   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11693     (gnus-summary-save-article arg)))
11694
11695 (defun gnus-summary-muttprint (&optional arg)
11696   "Print the current article using Muttprint.
11697 If N is a positive number, save the N next articles.
11698 If N is a negative number, save the N previous articles.
11699 If N is nil and any articles have been marked with the process mark,
11700 save those articles instead."
11701   (interactive "P")
11702   (require 'gnus-art)
11703   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11704     (gnus-summary-save-article arg t)))
11705
11706 (defun gnus-summary-pipe-message (program)
11707   "Pipe the current article through PROGRAM."
11708   (interactive "sProgram: ")
11709   (gnus-summary-select-article)
11710   (let ((mail-header-separator ""))
11711     (gnus-eval-in-buffer-window gnus-article-buffer
11712       (save-restriction
11713         (widen)
11714         (let ((start (window-start))
11715               buffer-read-only)
11716           (message-pipe-buffer-body program)
11717           (set-window-start (get-buffer-window (current-buffer)) start))))))
11718
11719 (defun gnus-get-split-value (methods)
11720   "Return a value based on the split METHODS."
11721   (let (split-name method result match)
11722     (when methods
11723       (save-excursion
11724         (set-buffer gnus-original-article-buffer)
11725         (save-restriction
11726           (nnheader-narrow-to-headers)
11727           (while (and methods (not split-name))
11728             (goto-char (point-min))
11729             (setq method (pop methods))
11730             (setq match (car method))
11731             (when (cond
11732                    ((stringp match)
11733                     ;; Regular expression.
11734                     (ignore-errors
11735                       (re-search-forward match nil t)))
11736                    ((functionp match)
11737                     ;; Function.
11738                     (save-restriction
11739                       (widen)
11740                       (setq result (funcall match gnus-newsgroup-name))))
11741                    ((consp match)
11742                     ;; Form.
11743                     (save-restriction
11744                       (widen)
11745                       (setq result (eval match)))))
11746               (setq split-name (cdr method))
11747               (cond ((stringp result)
11748                      (push (expand-file-name
11749                             result gnus-article-save-directory)
11750                            split-name))
11751                     ((consp result)
11752                      (setq split-name (append result split-name)))))))))
11753     (nreverse split-name)))
11754
11755 (defun gnus-valid-move-group-p (group)
11756   (and (boundp group)
11757        (symbol-name group)
11758        (symbol-value group)
11759        (gnus-get-function (gnus-find-method-for-group
11760                            (symbol-name group)) 'request-accept-article t)))
11761
11762 (defun gnus-read-move-group-name (prompt default articles prefix)
11763   "Read a group name."
11764   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11765          (minibuffer-confirm-incomplete nil) ; XEmacs
11766          (prom
11767           (format "%s %s to"
11768                   prompt
11769                   (if (> (length articles) 1)
11770                       (format "these %d articles" (length articles))
11771                     "this article")))
11772          (to-newsgroup
11773           (let (active group)
11774             (when (or (null split-name) (= 1 (length split-name)))
11775               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11776               (mapatoms (lambda (symbol)
11777                           (setq group (symbol-name symbol))
11778                           (when (string-match "[^\000-\177]" group)
11779                             (setq group (gnus-group-decoded-name group)))
11780                           (set (intern group active) group))
11781                         gnus-active-hashtb))
11782             (cond
11783              ((null split-name)
11784               (gnus-completing-read-with-default
11785                default prom active 'gnus-valid-move-group-p nil prefix
11786                'gnus-group-history))
11787              ((= 1 (length split-name))
11788               (gnus-completing-read-with-default
11789                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11790                'gnus-group-history))
11791              (t
11792               (gnus-completing-read-with-default
11793                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11794                'gnus-group-history)))))
11795          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11796          encoded)
11797     (when to-newsgroup
11798       (if (or (string= to-newsgroup "")
11799               (string= to-newsgroup prefix))
11800           (setq to-newsgroup default))
11801       (unless to-newsgroup
11802         (error "No group name entered"))
11803       (setq encoded (mm-encode-coding-string
11804                      to-newsgroup
11805                      (gnus-group-name-charset to-method to-newsgroup)))
11806       (or (gnus-active encoded)
11807           (gnus-activate-group encoded nil nil to-method)
11808           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11809                                      to-newsgroup))
11810               (or (and (gnus-request-create-group encoded to-method)
11811                        (gnus-activate-group encoded nil nil to-method)
11812                        (gnus-subscribe-group encoded))
11813                   (error "Couldn't create group %s" to-newsgroup)))
11814           (error "No such group: %s" to-newsgroup))
11815       encoded)))
11816
11817 (defvar gnus-summary-save-parts-counter)
11818 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11819
11820 (defun gnus-summary-save-parts (type dir n &optional reverse)
11821   "Save parts matching TYPE to DIR.
11822 If REVERSE, save parts that do not match TYPE."
11823   (interactive
11824    (list (read-string "Save parts of type: "
11825                       (or (car gnus-summary-save-parts-type-history)
11826                           gnus-summary-save-parts-default-mime)
11827                       'gnus-summary-save-parts-type-history)
11828          (setq gnus-summary-save-parts-last-directory
11829                (read-file-name "Save to directory: "
11830                                gnus-summary-save-parts-last-directory
11831                                nil t))
11832          current-prefix-arg))
11833   (gnus-summary-iterate n
11834     (let ((gnus-display-mime-function nil)
11835           (gnus-inhibit-treatment t))
11836       (gnus-summary-select-article))
11837     (save-excursion
11838       (set-buffer gnus-article-buffer)
11839       (let ((handles (or gnus-article-mime-handles
11840                          (mm-dissect-buffer nil gnus-article-loose-mime)
11841                          (and gnus-article-emulate-mime
11842                               (mm-uu-dissect))))
11843             (gnus-summary-save-parts-counter 1))
11844         (when handles
11845           (gnus-summary-save-parts-1 type dir handles reverse)
11846           (unless gnus-article-mime-handles ;; Don't destroy this case.
11847             (mm-destroy-parts handles)))))))
11848
11849 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11850   (if (stringp (car handle))
11851       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11852               (cdr handle))
11853     (when (if reverse
11854               (not (string-match type (mm-handle-media-type handle)))
11855             (string-match type (mm-handle-media-type handle)))
11856       (let ((file (expand-file-name
11857                    (gnus-map-function
11858                     mm-file-name-rewrite-functions
11859                     (file-name-nondirectory
11860                      (or
11861                       (mail-content-type-get
11862                        (mm-handle-disposition handle) 'filename)
11863                       (mail-content-type-get
11864                        (mm-handle-type handle) 'name)
11865                       (format "%s.%d.%d" gnus-newsgroup-name
11866                               (cdr gnus-article-current)
11867                               gnus-summary-save-parts-counter))))
11868                    dir)))
11869         (incf gnus-summary-save-parts-counter)
11870         (unless (file-exists-p file)
11871           (mm-save-part-to-file handle file))))))
11872
11873 ;; Summary extract commands
11874
11875 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11876   (let ((buffer-read-only nil)
11877         (article (gnus-summary-article-number))
11878         after-article b e)
11879     (unless (gnus-summary-goto-subject article)
11880       (error "No such article: %d" article))
11881     (gnus-summary-position-point)
11882     ;; If all commands are to be bunched up on one line, we collect
11883     ;; them here.
11884     (unless gnus-view-pseudos-separately
11885       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11886             files action)
11887         (while ps
11888           (setq action (cdr (assq 'action (car ps))))
11889           (setq files (list (cdr (assq 'name (car ps)))))
11890           (while (and ps (cdr ps)
11891                       (string= (or action "1")
11892                                (or (cdr (assq 'action (cadr ps))) "2")))
11893             (push (cdr (assq 'name (cadr ps))) files)
11894             (setcdr ps (cddr ps)))
11895           (when files
11896             (when (not (string-match "%s" action))
11897               (push " " files))
11898             (push " " files)
11899             (when (assq 'execute (car ps))
11900               (setcdr (assq 'execute (car ps))
11901                       (funcall (if (string-match "%s" action)
11902                                    'format 'concat)
11903                                action
11904                                (mapconcat
11905                                 (lambda (f)
11906                                   (if (equal f " ")
11907                                       f
11908                                     (shell-quote-argument f)))
11909                                 files " ")))))
11910           (setq ps (cdr ps)))))
11911     (if (and gnus-view-pseudos (not not-view))
11912         (while pslist
11913           (when (assq 'execute (car pslist))
11914             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11915                                   (eq gnus-view-pseudos 'not-confirm)))
11916           (setq pslist (cdr pslist)))
11917       (save-excursion
11918         (while pslist
11919           (setq after-article (or (cdr (assq 'article (car pslist)))
11920                                   (gnus-summary-article-number)))
11921           (gnus-summary-goto-subject after-article)
11922           (forward-line 1)
11923           (setq b (point))
11924           (insert "    " (file-name-nondirectory
11925                           (cdr (assq 'name (car pslist))))
11926                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11927           (setq e (point))
11928           (forward-line -1)             ; back to `b'
11929           (gnus-add-text-properties
11930            b (1- e) (list 'gnus-number gnus-reffed-article-number
11931                           gnus-mouse-face-prop gnus-mouse-face))
11932           (gnus-data-enter
11933            after-article gnus-reffed-article-number
11934            gnus-unread-mark b (car pslist) 0 (- e b))
11935           (setq gnus-newsgroup-unreads
11936                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11937                                          gnus-reffed-article-number))
11938           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11939           (setq pslist (cdr pslist)))))))
11940
11941 (defun gnus-pseudos< (p1 p2)
11942   (let ((c1 (cdr (assq 'action p1)))
11943         (c2 (cdr (assq 'action p2))))
11944     (and c1 c2 (string< c1 c2))))
11945
11946 (defun gnus-request-pseudo-article (props)
11947   (cond ((assq 'execute props)
11948          (gnus-execute-command (cdr (assq 'execute props)))))
11949   (let ((gnus-current-article (gnus-summary-article-number)))
11950     (gnus-run-hooks 'gnus-mark-article-hook)))
11951
11952 (defun gnus-execute-command (command &optional automatic)
11953   (save-excursion
11954     (gnus-article-setup-buffer)
11955     (set-buffer gnus-article-buffer)
11956     (setq buffer-read-only nil)
11957     (let ((command (if automatic command
11958                      (read-string "Command: " (cons command 0)))))
11959       (erase-buffer)
11960       (insert "$ " command "\n\n")
11961       (if gnus-view-pseudo-asynchronously
11962           (start-process "gnus-execute" (current-buffer) shell-file-name
11963                          shell-command-switch command)
11964         (call-process shell-file-name nil t nil
11965                       shell-command-switch command)))))
11966
11967 ;; Summary kill commands.
11968
11969 (defun gnus-summary-edit-global-kill (article)
11970   "Edit the \"global\" kill file."
11971   (interactive (list (gnus-summary-article-number)))
11972   (gnus-group-edit-global-kill article))
11973
11974 (defun gnus-summary-edit-local-kill ()
11975   "Edit a local kill file applied to the current newsgroup."
11976   (interactive)
11977   (setq gnus-current-headers (gnus-summary-article-header))
11978   (gnus-group-edit-local-kill
11979    (gnus-summary-article-number) gnus-newsgroup-name))
11980
11981 ;;; Header reading.
11982
11983 (defun gnus-read-header (id &optional header)
11984   "Read the headers of article ID and enter them into the Gnus system."
11985   (let ((group gnus-newsgroup-name)
11986         (gnus-override-method
11987          (or
11988           gnus-override-method
11989           (and (gnus-news-group-p gnus-newsgroup-name)
11990                (car (gnus-refer-article-methods)))))
11991         where)
11992     ;; First we check to see whether the header in question is already
11993     ;; fetched.
11994     (if (stringp id)
11995         ;; This is a Message-ID.
11996         (setq header (or header (gnus-id-to-header id)))
11997       ;; This is an article number.
11998       (setq header (or header (gnus-summary-article-header id))))
11999     (if (and header
12000              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12001         ;; We have found the header.
12002         header
12003       ;; We have to really fetch the header to this article.
12004       (save-excursion
12005         (set-buffer nntp-server-buffer)
12006         (when (setq where (gnus-request-head id group))
12007           (nnheader-fold-continuation-lines)
12008           (goto-char (point-max))
12009           (insert ".\n")
12010           (goto-char (point-min))
12011           (insert "211 ")
12012           (princ (cond
12013                   ((numberp id) id)
12014                   ((cdr where) (cdr where))
12015                   (header (mail-header-number header))
12016                   (t gnus-reffed-article-number))
12017                  (current-buffer))
12018           (insert " Article retrieved.\n"))
12019         (if (or (not where)
12020                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12021             ()                          ; Malformed head.
12022           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12023             (when (and (stringp id)
12024                        (or
12025                         (not (string= (gnus-group-real-name group)
12026                                       (car where)))
12027                         (not (gnus-server-equal gnus-override-method
12028                                                 (gnus-group-method group)))))
12029               ;; If we fetched by Message-ID and the article came from
12030               ;; a different group (or server), we fudge some bogus
12031               ;; article numbers for this article.
12032               (mail-header-set-number header gnus-reffed-article-number))
12033             (save-excursion
12034               (set-buffer gnus-summary-buffer)
12035               (decf gnus-reffed-article-number)
12036               (gnus-remove-header (mail-header-number header))
12037               (push header gnus-newsgroup-headers)
12038               (setq gnus-current-headers header)
12039               (push (mail-header-number header) gnus-newsgroup-limit)))
12040           header)))))
12041
12042 (defun gnus-remove-header (number)
12043   "Remove header NUMBER from `gnus-newsgroup-headers'."
12044   (if (and gnus-newsgroup-headers
12045            (= number (mail-header-number (car gnus-newsgroup-headers))))
12046       (pop gnus-newsgroup-headers)
12047     (let ((headers gnus-newsgroup-headers))
12048       (while (and (cdr headers)
12049                   (not (= number (mail-header-number (cadr headers)))))
12050         (pop headers))
12051       (when (cdr headers)
12052         (setcdr headers (cddr headers))))))
12053
12054 ;;;
12055 ;;; summary highlights
12056 ;;;
12057
12058 (defun gnus-highlight-selected-summary ()
12059   "Highlight selected article in summary buffer."
12060   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12061   (when gnus-summary-selected-face
12062     (save-excursion
12063       (let* ((beg (point-at-bol))
12064              (end (point-at-eol))
12065              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12066              (from (if (get-text-property beg gnus-mouse-face-prop)
12067                        beg
12068                      (or (next-single-property-change
12069                           beg gnus-mouse-face-prop nil end)
12070                          beg)))
12071              (to
12072               (if (= from end)
12073                   (- from 2)
12074                 (or (next-single-property-change
12075                      from gnus-mouse-face-prop nil end)
12076                     end))))
12077         ;; If no mouse-face prop on line we will have to = from = end,
12078         ;; so we highlight the entire line instead.
12079         (when (= (+ to 2) from)
12080           (setq from beg)
12081           (setq to end))
12082         (if gnus-newsgroup-selected-overlay
12083             ;; Move old overlay.
12084             (gnus-move-overlay
12085              gnus-newsgroup-selected-overlay from to (current-buffer))
12086           ;; Create new overlay.
12087           (gnus-overlay-put
12088            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12089            'face gnus-summary-selected-face))))))
12090
12091 (defvar gnus-summary-highlight-line-cached nil)
12092 (defvar gnus-summary-highlight-line-trigger nil)
12093
12094 (defun gnus-summary-highlight-line-0 ()
12095   (if (and (eq gnus-summary-highlight-line-trigger
12096                gnus-summary-highlight)
12097            gnus-summary-highlight-line-cached)
12098       gnus-summary-highlight-line-cached
12099     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12100           gnus-summary-highlight-line-cached
12101           (let* ((cond (list 'cond))
12102                  (c cond)
12103                  (list gnus-summary-highlight))
12104             (while list
12105               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12106                               nil))
12107               (setq c (cdr c)
12108                     list (cdr list)))
12109             (gnus-byte-compile (list 'lambda nil cond))))))
12110
12111 (defun gnus-summary-highlight-line ()
12112   "Highlight current line according to `gnus-summary-highlight'."
12113   (let* ((beg (point-at-bol))
12114          (article (or (gnus-summary-article-number) gnus-current-article))
12115          (score (or (cdr (assq article
12116                                gnus-newsgroup-scored))
12117                     gnus-summary-default-score 0))
12118          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12119          (inhibit-read-only t)
12120          (default gnus-summary-default-score)
12121          (default-high gnus-summary-default-high-score)
12122          (default-low gnus-summary-default-low-score)
12123          (uncached (and gnus-summary-use-undownloaded-faces
12124                         (memq article gnus-newsgroup-undownloaded)
12125                         (not (memq article gnus-newsgroup-cached)))))
12126     (let ((face (funcall (gnus-summary-highlight-line-0))))
12127       (unless (eq face (get-text-property beg 'face))
12128         (gnus-put-text-property-excluding-characters-with-faces
12129          beg (point-at-eol) 'face
12130          (setq face (if (boundp face) (symbol-value face) face)))
12131         (when gnus-summary-highlight-line-function
12132           (funcall gnus-summary-highlight-line-function article face))))))
12133
12134 (defun gnus-update-read-articles (group unread &optional compute)
12135   "Update the list of read articles in GROUP.
12136 UNREAD is a sorted list."
12137   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12138         (info (gnus-get-info group))
12139         (prev 1)
12140         read)
12141     (if (or (not info) (not active))
12142         ;; There is no info on this group if it was, in fact,
12143         ;; killed.  Gnus stores no information on killed groups, so
12144         ;; there's nothing to be done.
12145         ;; One could store the information somewhere temporarily,
12146         ;; perhaps...  Hmmm...
12147         ()
12148       ;; Remove any negative articles numbers.
12149       (while (and unread (< (car unread) 0))
12150         (setq unread (cdr unread)))
12151       ;; Remove any expired article numbers
12152       (while (and unread (< (car unread) (car active)))
12153         (setq unread (cdr unread)))
12154       ;; Compute the ranges of read articles by looking at the list of
12155       ;; unread articles.
12156       (while unread
12157         (when (/= (car unread) prev)
12158           (push (if (= prev (1- (car unread))) prev
12159                   (cons prev (1- (car unread))))
12160                 read))
12161         (setq prev (1+ (car unread)))
12162         (setq unread (cdr unread)))
12163       (when (<= prev (cdr active))
12164         (push (cons prev (cdr active)) read))
12165       (setq read (if (> (length read) 1) (nreverse read) read))
12166       (if compute
12167           read
12168         (save-excursion
12169           (let (setmarkundo)
12170             ;; Propagate the read marks to the backend.
12171             (when (gnus-check-backend-function 'request-set-mark group)
12172               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12173                     (add (gnus-remove-from-range read (gnus-info-read info))))
12174                 (when (or add del)
12175                   (unless (gnus-check-group group)
12176                     (error "Can't open server for %s" group))
12177                   (gnus-request-set-mark
12178                    group (delq nil (list (if add (list add 'add '(read)))
12179                                          (if del (list del 'del '(read))))))
12180                   (setq setmarkundo
12181                         `(gnus-request-set-mark
12182                           ,group
12183                           ',(delq nil (list
12184                                        (if del (list del 'add '(read)))
12185                                        (if add (list add 'del '(read))))))))))
12186             (set-buffer gnus-group-buffer)
12187             (gnus-undo-register
12188               `(progn
12189                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12190                  (gnus-info-set-read ',info ',(gnus-info-read info))
12191                  (gnus-get-unread-articles-in-group ',info
12192                                                     (gnus-active ,group))
12193                  (gnus-group-update-group ,group t)
12194                  ,setmarkundo))))
12195         ;; Enter this list into the group info.
12196         (gnus-info-set-read info read)
12197         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12198         (gnus-get-unread-articles-in-group info (gnus-active group))
12199         t))))
12200
12201 (defun gnus-offer-save-summaries ()
12202   "Offer to save all active summary buffers."
12203   (let (buffers)
12204     ;; Go through all buffers and find all summaries.
12205     (dolist (buffer (buffer-list))
12206       (when (and (setq buffer (buffer-name buffer))
12207                  (string-match "Summary" buffer)
12208                  (with-current-buffer buffer
12209                    ;; We check that this is, indeed, a summary buffer.
12210                    (and (eq major-mode 'gnus-summary-mode)
12211                         ;; Also make sure this isn't bogus.
12212                         gnus-newsgroup-prepared
12213                         ;; Also make sure that this isn't a
12214                         ;; dead summary buffer.
12215                         (not gnus-dead-summary-mode))))
12216         (push buffer buffers)))
12217     ;; Go through all these summary buffers and offer to save them.
12218     (when buffers
12219       (save-excursion
12220         (map-y-or-n-p
12221          "Update summary buffer %s? "
12222          (lambda (buf)
12223            (switch-to-buffer buf)
12224            (gnus-summary-exit))
12225          buffers)))))
12226
12227 (defun gnus-summary-setup-default-charset ()
12228   "Setup newsgroup default charset."
12229   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12230       (setq gnus-newsgroup-charset nil)
12231     (let* ((ignored-charsets
12232             (or gnus-newsgroup-ephemeral-ignored-charsets
12233                 (append
12234                  (and gnus-newsgroup-name
12235                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12236                  gnus-newsgroup-ignored-charsets))))
12237       (setq gnus-newsgroup-charset
12238             (or gnus-newsgroup-ephemeral-charset
12239                 (and gnus-newsgroup-name
12240                      (gnus-parameter-charset gnus-newsgroup-name))
12241                 gnus-default-charset))
12242       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12243            ignored-charsets))))
12244
12245 ;;;
12246 ;;; Mime Commands
12247 ;;;
12248
12249 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12250   "Display the current article buffer fully MIME-buttonized.
12251 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12252 treated as multipart/mixed."
12253   (interactive "P")
12254   (require 'gnus-art)
12255   (let ((gnus-unbuttonized-mime-types nil)
12256         (gnus-mime-display-multipart-as-mixed show-all-parts))
12257     (gnus-summary-show-article)))
12258
12259 (defun gnus-summary-repair-multipart (article)
12260   "Add a Content-Type header to a multipart article without one."
12261   (interactive (list (gnus-summary-article-number)))
12262   (gnus-with-article article
12263     (message-narrow-to-head)
12264     (message-remove-header "Mime-Version")
12265     (goto-char (point-max))
12266     (insert "Mime-Version: 1.0\n")
12267     (widen)
12268     (when (search-forward "\n--" nil t)
12269       (let ((separator (buffer-substring (point) (point-at-eol))))
12270         (message-narrow-to-head)
12271         (message-remove-header "Content-Type")
12272         (goto-char (point-max))
12273         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12274                         separator))
12275         (widen))))
12276   (let (gnus-mark-article-hook)
12277     (gnus-summary-select-article t t nil article)))
12278
12279 (defun gnus-summary-toggle-display-buttonized ()
12280   "Toggle the buttonizing of the article buffer."
12281   (interactive)
12282   (require 'gnus-art)
12283   (if (setq gnus-inhibit-mime-unbuttonizing
12284             (not gnus-inhibit-mime-unbuttonizing))
12285       (let ((gnus-unbuttonized-mime-types nil))
12286         (gnus-summary-show-article))
12287     (gnus-summary-show-article)))
12288
12289 ;;;
12290 ;;; Generic summary marking commands
12291 ;;;
12292
12293 (defvar gnus-summary-marking-alist
12294   '((read gnus-del-mark "d")
12295     (unread gnus-unread-mark "u")
12296     (ticked gnus-ticked-mark "!")
12297     (dormant gnus-dormant-mark "?")
12298     (expirable gnus-expirable-mark "e"))
12299   "An alist of names/marks/keystrokes.")
12300
12301 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12302 (defvar gnus-summary-mark-map)
12303
12304 (defun gnus-summary-make-all-marking-commands ()
12305   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12306   (dolist (elem gnus-summary-marking-alist)
12307     (apply 'gnus-summary-make-marking-command elem)))
12308
12309 (defun gnus-summary-make-marking-command (name mark keystroke)
12310   (let ((map (make-sparse-keymap)))
12311     (define-key gnus-summary-generic-mark-map keystroke map)
12312     (dolist (lway `((next "next" next nil "n")
12313                     (next-unread "next unread" next t "N")
12314                     (prev "previous" prev nil "p")
12315                     (prev-unread "previous unread" prev t "P")
12316                     (nomove "" nil nil ,keystroke)))
12317       (let ((func (gnus-summary-make-marking-command-1
12318                    mark (car lway) lway name)))
12319         (setq func (eval func))
12320         (define-key map (nth 4 lway) func)))))
12321
12322 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12323   `(defun ,(intern
12324             (format "gnus-summary-put-mark-as-%s%s"
12325                     name (if (eq way 'nomove)
12326                              ""
12327                            (concat "-" (symbol-name way)))))
12328      (n)
12329      ,(format
12330        "Mark the current article as %s%s.
12331 If N, the prefix, then repeat N times.
12332 If N is negative, move in reverse order.
12333 The difference between N and the actual number of articles marked is
12334 returned."
12335        name (cadr lway))
12336      (interactive "p")
12337      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12338
12339 (defun gnus-summary-generic-mark (n mark move unread)
12340   "Mark N articles with MARK."
12341   (unless (eq major-mode 'gnus-summary-mode)
12342     (error "This command can only be used in the summary buffer"))
12343   (gnus-summary-show-thread)
12344   (let ((nummove
12345          (cond
12346           ((eq move 'next) 1)
12347           ((eq move 'prev) -1)
12348           (t 0))))
12349     (if (zerop nummove)
12350         (setq n 1)
12351       (when (< n 0)
12352         (setq n (abs n)
12353               nummove (* -1 nummove))))
12354     (while (and (> n 0)
12355                 (gnus-summary-mark-article nil mark)
12356                 (zerop (gnus-summary-next-subject nummove unread t)))
12357       (setq n (1- n)))
12358     (when (/= 0 n)
12359       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12360     (gnus-summary-recenter)
12361     (gnus-summary-position-point)
12362     (gnus-set-mode-line 'summary)
12363     n))
12364
12365 (defun gnus-summary-insert-articles (articles)
12366   (when (setq articles
12367               (gnus-sorted-difference articles
12368                                       (mapcar (lambda (h)
12369                                                 (mail-header-number h))
12370                                               gnus-newsgroup-headers)))
12371     (setq gnus-newsgroup-headers
12372           (gnus-merge 'list
12373                       gnus-newsgroup-headers
12374                       (gnus-fetch-headers articles)
12375                       'gnus-article-sort-by-number))
12376     ;; Suppress duplicates?
12377     (when gnus-suppress-duplicates
12378       (gnus-dup-suppress-articles))
12379
12380     (if (and gnus-fetch-old-headers
12381              (eq gnus-headers-retrieved-by 'nov))
12382         ;; We might want to build some more threads first.
12383         (if (eq gnus-fetch-old-headers 'invisible)
12384             (gnus-build-all-threads)
12385           (gnus-build-old-threads))
12386       ;; Mark the inserted articles that are unread as unread.
12387       (setq gnus-newsgroup-unreads
12388             (gnus-sorted-nunion
12389              gnus-newsgroup-unreads
12390              (gnus-sorted-nintersection
12391               (gnus-list-of-unread-articles gnus-newsgroup-name)
12392               articles)))
12393       ;; Mark the inserted articles as selected so that the information
12394       ;; of the marks having been changed by a user may be updated when
12395       ;; exiting this group.  See `gnus-summary-update-info'.
12396       (dolist (art articles)
12397         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12398     ;; Let the Gnus agent mark articles as read.
12399     (when gnus-agent
12400       (gnus-agent-get-undownloaded-list))
12401     ;; Remove list identifiers from subject
12402     (when gnus-list-identifiers
12403       (gnus-summary-remove-list-identifiers))
12404     ;; First and last article in this newsgroup.
12405     (when gnus-newsgroup-headers
12406       (setq gnus-newsgroup-begin
12407             (mail-header-number (car gnus-newsgroup-headers))
12408             gnus-newsgroup-end
12409             (mail-header-number
12410              (gnus-last-element gnus-newsgroup-headers))))
12411     (when gnus-use-scoring
12412       (gnus-possibly-score-headers))))
12413
12414 (defun gnus-summary-insert-old-articles (&optional all)
12415   "Insert all old articles in this group.
12416 If ALL is non-nil, already read articles become readable.
12417 If ALL is a number, fetch this number of articles."
12418   (interactive "P")
12419   (prog1
12420       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12421             older len)
12422         (setq older
12423               ;; Some nntp servers lie about their active range.  When
12424               ;; this happens, the active range can be in the millions.
12425               ;; Use a compressed range to avoid creating a huge list.
12426               (gnus-range-difference (list gnus-newsgroup-active) old))
12427         (setq len (gnus-range-length older))
12428         (cond
12429          ((null older) nil)
12430          ((numberp all)
12431           (if (< all len)
12432               (let ((older-range (nreverse older)))
12433                 (setq older nil)
12434
12435                 (while (> all 0)
12436                   (let* ((r (pop older-range))
12437                          (min (if (numberp r) r (car r)))
12438                          (max (if (numberp r) r (cdr r))))
12439                     (while (and (<= min max)
12440                                 (> all 0))
12441                       (push max older)
12442                       (setq all (1- all)
12443                             max (1- max))))))
12444             (setq older (gnus-uncompress-range older))))
12445          (all
12446           (setq older (gnus-uncompress-range older)))
12447          (t
12448           (when (and (numberp gnus-large-newsgroup)
12449                    (> len gnus-large-newsgroup))
12450               (let* ((cursor-in-echo-area nil)
12451                      (initial (gnus-parameter-large-newsgroup-initial
12452                                gnus-newsgroup-name))
12453                      (input
12454                       (read-string
12455                        (format
12456                         "How many articles from %s (%s %d): "
12457                         (gnus-group-decoded-name gnus-newsgroup-name)
12458                         (if initial "max" "default")
12459                         len)
12460                        (if initial
12461                            (cons (number-to-string initial)
12462                                  0)))))
12463                 (unless (string-match "^[ \t]*$" input)
12464                   (setq all (string-to-number input))
12465                   (if (< all len)
12466                       (let ((older-range (nreverse older)))
12467                         (setq older nil)
12468
12469                         (while (> all 0)
12470                           (let* ((r (pop older-range))
12471                                  (min (if (numberp r) r (car r)))
12472                                  (max (if (numberp r) r (cdr r))))
12473                             (while (and (<= min max)
12474                                         (> all 0))
12475                               (push max older)
12476                               (setq all (1- all)
12477                                     max (1- max))))))))))
12478           (setq older (gnus-uncompress-range older))))
12479         (if (not older)
12480             (message "No old news.")
12481           (gnus-summary-insert-articles older)
12482           (gnus-summary-limit (gnus-sorted-nunion old older))))
12483     (gnus-summary-position-point)))
12484
12485 (defun gnus-summary-insert-new-articles ()
12486   "Insert all new articles in this group."
12487   (interactive)
12488   (prog1
12489       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12490             (old-active gnus-newsgroup-active)
12491             (nnmail-fetched-sources (list t))
12492             i new)
12493         (setq gnus-newsgroup-active
12494               (gnus-activate-group gnus-newsgroup-name 'scan))
12495         (setq i (cdr gnus-newsgroup-active))
12496         (while (> i (cdr old-active))
12497           (push i new)
12498           (decf i))
12499         (if (not new)
12500             (message "No gnus is bad news")
12501           (gnus-summary-insert-articles new)
12502           (setq gnus-newsgroup-unreads
12503                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12504           (gnus-summary-limit (gnus-sorted-nunion old new))))
12505     (gnus-summary-position-point)))
12506
12507 (gnus-summary-make-all-marking-commands)
12508
12509 (gnus-ems-redefine)
12510
12511 (provide 'gnus-sum)
12512
12513 (run-hooks 'gnus-sum-load-hook)
12514
12515 ;; Local Variables:
12516 ;; coding: iso-8859-1
12517 ;; End:
12518
12519 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12520 ;;; gnus-sum.el ends here