(gnus-summary-sort-by-most-recent-number)
[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 of the License, or
14 ;; (at your option) 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.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32   (require 'cl))
33
34 (defvar tool-bar-mode)
35 (defvar gnus-tmp-header)
36
37 (require 'gnus)
38 (require 'gnus-group)
39 (require 'gnus-spec)
40 (require 'gnus-range)
41 (require 'gnus-int)
42 (require 'gnus-undo)
43 (require 'gnus-util)
44 (require 'gmm-utils)
45 (require 'mm-decode)
46 (require 'nnoo)
47
48 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
49 (autoload 'gnus-cache-write-active "gnus-cache")
50 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
51 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
52 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
53 (autoload 'mm-uu-dissect "mm-uu")
54 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
55   "Deuglify broken Outlook (Express) articles and redisplay."
56   t)
57 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
58 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
59 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
60
61 (defcustom gnus-kill-summary-on-exit t
62   "*If non-nil, kill the summary buffer when you exit from it.
63 If nil, the summary will become a \"*Dead Summary*\" buffer, and
64 it will be killed sometime later."
65   :group 'gnus-summary-exit
66   :type 'boolean)
67
68 (defcustom gnus-summary-next-group-on-exit t
69   "If non-nil, go to the next unread newsgroup on summary exit.
70 See `gnus-group-goto-unread'."
71   :link '(custom-manual "(gnus)Group Maneuvering")
72   :group 'gnus-summary-exit
73   :version "23.1" ;; No Gnus
74   :type 'boolean)
75
76 (defcustom gnus-fetch-old-headers nil
77   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
78 If an unread article in the group refers to an older, already
79 read (or just marked as read) article, the old article will not
80 normally be displayed in the Summary buffer.  If this variable is
81 t, Gnus will attempt to grab the headers to the old articles, and
82 thereby build complete threads.  If it has the value `some', all
83 old headers will be fetched but only enough headers to connect
84 otherwise loose threads will be displayed.  This variable can
85 also be a number.  In that case, no more than that number of old
86 headers will be fetched.  If it has the value `invisible', all
87 old headers will be fetched, but none will be displayed.
88
89 The server has to support NOV for any of this to work.
90
91 This feature can seriously impact performance it ignores all
92 locally cached header entries.  Setting it to t for groups for a
93 server that doesn't expire articles (such as news.gmane.org),
94 leads to very slow summary generation."
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-propagate-marks t
1212   "If non-nil, do not propagate marks to the backends."
1213   :version "23.1" ;; No Gnus
1214   :type 'boolean
1215   :group 'gnus-summary-marks)
1216
1217 (defcustom gnus-alter-articles-to-read-function nil
1218   "Function to be called to alter the list of articles to be selected."
1219   :type '(choice (const nil) function)
1220   :group 'gnus-summary)
1221
1222 (defcustom gnus-orphan-score nil
1223   "*All orphans get this score added.  Set in the score file."
1224   :group 'gnus-score-default
1225   :type '(choice (const nil)
1226                  integer))
1227
1228 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1229   "*A regexp to match MIME parts when saving multiple parts of a
1230 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1231 This regexp will be used by default when prompting the user for which
1232 type of files to save."
1233   :group 'gnus-summary
1234   :type 'regexp)
1235
1236 (defcustom gnus-read-all-available-headers nil
1237   "Whether Gnus should parse all headers made available to it.
1238 This is mostly relevant for slow back ends where the user may
1239 wish to widen the summary buffer to include all headers
1240 that were fetched.  Say, for nnultimate groups."
1241   :version "22.1"
1242   :group 'gnus-summary
1243   :type '(choice boolean regexp))
1244
1245 (defcustom gnus-summary-pipe-output-default-command nil
1246   "Command (and optional arguments) used to pipe article to subprocess.
1247 This will be used as the default command if it is non-nil.  The value
1248 will be updated if you modify it when executing the command
1249 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1250   :version "23.1" ;; No Gnus
1251   :group 'gnus-summary
1252   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1253
1254 (defcustom gnus-summary-muttprint-program "muttprint"
1255   "Command (and optional arguments) used to run Muttprint.
1256 The value will be updated if you modify it when executing the command
1257 `gnus-summary-muttprint'."
1258   :version "22.1"
1259   :group 'gnus-summary
1260   :type 'string)
1261
1262 (defcustom gnus-article-loose-mime t
1263   "If non-nil, don't require MIME-Version header.
1264 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1265 supply the MIME-Version header or deliberately strip it from the mail.
1266 If non-nil (the default), Gnus will treat some articles as MIME
1267 even if the MIME-Version header is missing."
1268   :version "22.1"
1269   :type 'boolean
1270   :group 'gnus-article-mime)
1271
1272 (defcustom gnus-article-emulate-mime t
1273   "If non-nil, use MIME emulation for uuencode and the like.
1274 This means that Gnus will search message bodies for text that look
1275 like uuencoded bits, yEncoded bits, and so on, and present that using
1276 the normal Gnus MIME machinery."
1277   :version "22.1"
1278   :type 'boolean
1279   :group 'gnus-article-mime)
1280
1281 ;;; Internal variables
1282
1283 (defvar gnus-summary-display-cache nil)
1284 (defvar gnus-article-mime-handles nil)
1285 (defvar gnus-article-decoded-p nil)
1286 (defvar gnus-article-charset nil)
1287 (defvar gnus-article-ignored-charsets nil)
1288 (defvar gnus-scores-exclude-files nil)
1289 (defvar gnus-page-broken nil)
1290
1291 (defvar gnus-original-article nil)
1292 (defvar gnus-article-internal-prepare-hook nil)
1293 (defvar gnus-newsgroup-process-stack nil)
1294
1295 (defvar gnus-thread-indent-array nil)
1296 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1297 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1298   "Function called to sort the articles within a thread after it has been gathered together.")
1299
1300 (defvar gnus-summary-save-parts-type-history nil)
1301 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1302
1303 ;; Avoid highlighting in kill files.
1304 (defvar gnus-summary-inhibit-highlight nil)
1305 (defvar gnus-newsgroup-selected-overlay nil)
1306 (defvar gnus-inhibit-limiting nil)
1307 (defvar gnus-newsgroup-adaptive-score-file nil)
1308 (defvar gnus-current-score-file nil)
1309 (defvar gnus-current-move-group nil)
1310 (defvar gnus-current-copy-group nil)
1311 (defvar gnus-current-crosspost-group nil)
1312 (defvar gnus-newsgroup-display nil)
1313
1314 (defvar gnus-newsgroup-dependencies nil)
1315 (defvar gnus-newsgroup-adaptive nil)
1316 (defvar gnus-summary-display-article-function nil)
1317 (defvar gnus-summary-highlight-line-function nil
1318   "Function called after highlighting a summary line.")
1319
1320 (defvar gnus-summary-line-format-alist
1321   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1322     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1323     (?s gnus-tmp-subject-or-nil ?s)
1324     (?n gnus-tmp-name ?s)
1325     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1326         ?s)
1327     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1328             gnus-tmp-from) ?s)
1329     (?F gnus-tmp-from ?s)
1330     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1331     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1332     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1333     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1334     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1335     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1336     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1337     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1338     (?L gnus-tmp-lines ?s)
1339     (?O gnus-tmp-downloaded ?c)
1340     (?I gnus-tmp-indentation ?s)
1341     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1342     (?R gnus-tmp-replied ?c)
1343     (?\[ gnus-tmp-opening-bracket ?c)
1344     (?\] gnus-tmp-closing-bracket ?c)
1345     (?\> (make-string gnus-tmp-level ? ) ?s)
1346     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1347     (?i gnus-tmp-score ?d)
1348     (?z gnus-tmp-score-char ?c)
1349     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1350     (?U gnus-tmp-unread ?c)
1351     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1352         ?s)
1353     (?t (gnus-summary-number-of-articles-in-thread
1354          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1355         ?d)
1356     (?e (gnus-summary-number-of-articles-in-thread
1357          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1358         ?c)
1359     (?u gnus-tmp-user-defined ?s)
1360     (?P (gnus-pick-line-number) ?d)
1361     (?B gnus-tmp-thread-tree-header-string ?s)
1362     (user-date (gnus-user-date
1363                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1364   "An alist of format specifications that can appear in summary lines.
1365 These are paired with what variables they correspond with, along with
1366 the type of the variable (string, integer, character, etc).")
1367
1368 (defvar gnus-summary-dummy-line-format-alist
1369   `((?S gnus-tmp-subject ?s)
1370     (?N gnus-tmp-number ?d)
1371     (?u gnus-tmp-user-defined ?s)))
1372
1373 (defvar gnus-summary-mode-line-format-alist
1374   `((?G gnus-tmp-group-name ?s)
1375     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1376     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1377     (?A gnus-tmp-article-number ?d)
1378     (?Z gnus-tmp-unread-and-unselected ?s)
1379     (?V gnus-version ?s)
1380     (?U gnus-tmp-unread-and-unticked ?d)
1381     (?S gnus-tmp-subject ?s)
1382     (?e gnus-tmp-unselected ?d)
1383     (?u gnus-tmp-user-defined ?s)
1384     (?d (length gnus-newsgroup-dormant) ?d)
1385     (?t (length gnus-newsgroup-marked) ?d)
1386     (?h (length gnus-newsgroup-spam-marked) ?d)
1387     (?r (length gnus-newsgroup-reads) ?d)
1388     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1389     (?E gnus-newsgroup-expunged-tally ?d)
1390     (?s (gnus-current-score-file-nondirectory) ?s)))
1391
1392 ;; This is here rather than in gnus-art for compilation reasons.
1393 (defvar gnus-article-mode-line-format-alist
1394   (nconc '((?w (gnus-article-wash-status) ?s)
1395            (?m (gnus-article-mime-part-status) ?s))
1396          gnus-summary-mode-line-format-alist))
1397
1398 (defvar gnus-last-search-regexp nil
1399   "Default regexp for article search command.")
1400
1401 (defvar gnus-last-shell-command nil
1402   "Default shell command on article.")
1403
1404 (defvar gnus-newsgroup-agentized nil
1405   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1406 (defvar gnus-newsgroup-begin nil)
1407 (defvar gnus-newsgroup-end nil)
1408 (defvar gnus-newsgroup-last-rmail nil)
1409 (defvar gnus-newsgroup-last-mail nil)
1410 (defvar gnus-newsgroup-last-folder nil)
1411 (defvar gnus-newsgroup-last-file nil)
1412 (defvar gnus-newsgroup-last-directory nil)
1413 (defvar gnus-newsgroup-auto-expire nil)
1414 (defvar gnus-newsgroup-active nil)
1415
1416 (defvar gnus-newsgroup-data nil)
1417 (defvar gnus-newsgroup-data-reverse nil)
1418 (defvar gnus-newsgroup-limit nil)
1419 (defvar gnus-newsgroup-limits nil)
1420 (defvar gnus-summary-use-undownloaded-faces nil)
1421
1422 (defvar gnus-newsgroup-unreads nil
1423   "Sorted list of unread articles in the current newsgroup.")
1424
1425 (defvar gnus-newsgroup-unselected nil
1426   "Sorted list of unselected unread articles in the current newsgroup.")
1427
1428 (defvar gnus-newsgroup-reads nil
1429   "Alist of read articles and article marks in the current newsgroup.")
1430
1431 (defvar gnus-newsgroup-expunged-tally nil)
1432
1433 (defvar gnus-newsgroup-marked nil
1434   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1435
1436 (defvar gnus-newsgroup-spam-marked nil
1437   "List of ranges of articles that have been marked as spam.")
1438
1439 (defvar gnus-newsgroup-killed nil
1440   "List of ranges of articles that have been through the scoring process.")
1441
1442 (defvar gnus-newsgroup-cached nil
1443   "Sorted list of articles that come from the article cache.")
1444
1445 (defvar gnus-newsgroup-saved nil
1446   "List of articles that have been saved.")
1447
1448 (defvar gnus-newsgroup-kill-headers nil)
1449
1450 (defvar gnus-newsgroup-replied nil
1451   "List of articles that have been replied to in the current newsgroup.")
1452
1453 (defvar gnus-newsgroup-forwarded nil
1454   "List of articles that have been forwarded in the current newsgroup.")
1455
1456 (defvar gnus-newsgroup-recent nil
1457   "List of articles that have are recent in the current newsgroup.")
1458
1459 (defvar gnus-newsgroup-expirable nil
1460   "Sorted list of articles in the current newsgroup that can be expired.")
1461
1462 (defvar gnus-newsgroup-processable nil
1463   "List of articles in the current newsgroup that can be processed.")
1464
1465 (defvar gnus-newsgroup-downloadable nil
1466   "Sorted list of articles in the current newsgroup that can be processed.")
1467
1468 (defvar gnus-newsgroup-unfetched nil
1469   "Sorted list of articles in the current newsgroup whose headers have
1470 not been fetched into the agent.
1471
1472 This list will always be a subset of gnus-newsgroup-undownloaded.")
1473
1474 (defvar gnus-newsgroup-undownloaded nil
1475   "List of articles in the current newsgroup that haven't been downloaded.")
1476
1477 (defvar gnus-newsgroup-unsendable nil
1478   "List of articles in the current newsgroup that won't be sent.")
1479
1480 (defvar gnus-newsgroup-bookmarks nil
1481   "List of articles in the current newsgroup that have bookmarks.")
1482
1483 (defvar gnus-newsgroup-dormant nil
1484   "Sorted list of dormant articles in the current newsgroup.")
1485
1486 (defvar gnus-newsgroup-unseen nil
1487   "List of unseen articles in the current newsgroup.")
1488
1489 (defvar gnus-newsgroup-seen nil
1490   "Range of seen articles in the current newsgroup.")
1491
1492 (defvar gnus-newsgroup-articles nil
1493   "List of articles in the current newsgroup.")
1494
1495 (defvar gnus-newsgroup-scored nil
1496   "List of scored articles in the current newsgroup.")
1497
1498 (defvar gnus-newsgroup-headers nil
1499   "List of article headers in the current newsgroup.")
1500
1501 (defvar gnus-newsgroup-threads nil)
1502
1503 (defvar gnus-newsgroup-prepared nil
1504   "Whether the current group has been prepared properly.")
1505
1506 (defvar gnus-newsgroup-ancient nil
1507   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1508
1509 (defvar gnus-newsgroup-sparse nil)
1510
1511 (defvar gnus-current-article nil)
1512 (defvar gnus-article-current nil)
1513 (defvar gnus-current-headers nil)
1514 (defvar gnus-have-all-headers nil)
1515 (defvar gnus-last-article nil)
1516 (defvar gnus-newsgroup-history nil)
1517 (defvar gnus-newsgroup-charset nil)
1518 (defvar gnus-newsgroup-ephemeral-charset nil)
1519 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1520
1521 (defvar gnus-article-before-search nil)
1522
1523 (defvar gnus-summary-local-variables
1524   '(gnus-newsgroup-name
1525     gnus-newsgroup-begin gnus-newsgroup-end
1526     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1527     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1528     gnus-newsgroup-last-directory
1529     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1530     gnus-newsgroup-unselected gnus-newsgroup-marked
1531     gnus-newsgroup-spam-marked
1532     gnus-newsgroup-reads gnus-newsgroup-saved
1533     gnus-newsgroup-replied gnus-newsgroup-forwarded
1534     gnus-newsgroup-recent
1535     gnus-newsgroup-expirable
1536     gnus-newsgroup-processable gnus-newsgroup-killed
1537     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1538     gnus-newsgroup-unfetched
1539     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1540     gnus-newsgroup-seen gnus-newsgroup-articles
1541     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1542     gnus-newsgroup-headers gnus-newsgroup-threads
1543     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1544     gnus-current-article gnus-current-headers gnus-have-all-headers
1545     gnus-last-article gnus-article-internal-prepare-hook
1546     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1547     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1548     gnus-thread-expunge-below
1549     gnus-score-alist gnus-current-score-file
1550     (gnus-summary-expunge-below . global)
1551     (gnus-summary-mark-below . global)
1552     (gnus-orphan-score . global)
1553     gnus-newsgroup-active gnus-scores-exclude-files
1554     gnus-newsgroup-history gnus-newsgroup-ancient
1555     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1556     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1557     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1558     (gnus-newsgroup-expunged-tally . 0)
1559     gnus-cache-removable-articles gnus-newsgroup-cached
1560     gnus-newsgroup-data gnus-newsgroup-data-reverse
1561     gnus-newsgroup-limit gnus-newsgroup-limits
1562     gnus-newsgroup-charset gnus-newsgroup-display
1563     gnus-summary-use-undownloaded-faces)
1564   "Variables that are buffer-local to the summary buffers.")
1565
1566 (defvar gnus-newsgroup-variables nil
1567   "A list of variables that have separate values in different newsgroups.
1568 A list of newsgroup (summary buffer) local variables, or cons of
1569 variables and their default expressions to be evalled (when the default
1570 values are not nil), that should be made global while the summary buffer
1571 is active.
1572
1573 Note: The default expressions will be evaluated (using function `eval')
1574 before assignment to the local variable rather than just assigned to it.
1575 If the default expression is the symbol `global', that symbol will not
1576 be evaluated but the global value of the local variable will be used
1577 instead.
1578
1579 These variables can be used to set variables in the group parameters
1580 while still allowing them to affect operations done in other buffers.
1581 For example:
1582
1583 \(setq gnus-newsgroup-variables
1584      '(message-use-followup-to
1585        (gnus-visible-headers .
1586          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1587 ")
1588
1589 (eval-when-compile
1590   ;; Bind features so that require will believe that gnus-sum has
1591   ;; already been loaded (avoids infinite recursion)
1592   (let ((features (cons 'gnus-sum features)))
1593     (require 'gnus-art)))
1594
1595 ;; MIME stuff.
1596
1597 (defvar gnus-decode-encoded-word-methods
1598   '(mail-decode-encoded-word-string)
1599   "List of methods used to decode encoded words.
1600
1601 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1602 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1603 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1604 whose names match REGEXP.
1605
1606 For example:
1607 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1608  mail-decode-encoded-word-string
1609  (\"chinese\" . rfc1843-decode-string))")
1610
1611 (defvar gnus-decode-encoded-word-methods-cache nil)
1612
1613 (defun gnus-multi-decode-encoded-word-string (string)
1614   "Apply the functions from `gnus-encoded-word-methods' that match."
1615   (unless (and gnus-decode-encoded-word-methods-cache
1616                (eq gnus-newsgroup-name
1617                    (car gnus-decode-encoded-word-methods-cache)))
1618     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1619     (dolist (method gnus-decode-encoded-word-methods)
1620       (if (symbolp method)
1621           (nconc gnus-decode-encoded-word-methods-cache (list method))
1622         (if (and gnus-newsgroup-name
1623                  (string-match (car method) gnus-newsgroup-name))
1624             (nconc gnus-decode-encoded-word-methods-cache
1625                    (list (cdr method)))))))
1626   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1627     (setq string (funcall method string))))
1628
1629 ;; Subject simplification.
1630
1631 (defun gnus-simplify-whitespace (str)
1632   "Remove excessive whitespace from STR."
1633   ;; Multiple spaces.
1634   (while (string-match "[ \t][ \t]+" str)
1635     (setq str (concat (substring str 0 (match-beginning 0))
1636                         " "
1637                         (substring str (match-end 0)))))
1638   ;; Leading spaces.
1639   (when (string-match "^[ \t]+" str)
1640     (setq str (substring str (match-end 0))))
1641   ;; Trailing spaces.
1642   (when (string-match "[ \t]+$" str)
1643     (setq str (substring str 0 (match-beginning 0))))
1644   str)
1645
1646 (defun gnus-simplify-all-whitespace (str)
1647   "Remove all whitespace from STR."
1648   (while (string-match "[ \t\n]+" str)
1649     (setq str (replace-match "" nil nil str)))
1650   str)
1651
1652 (defsubst gnus-simplify-subject-re (subject)
1653   "Remove \"Re:\" from subject lines."
1654   (if (string-match message-subject-re-regexp subject)
1655       (substring subject (match-end 0))
1656     subject))
1657
1658 (defun gnus-simplify-subject (subject &optional re-only)
1659   "Remove `Re:' and words in parentheses.
1660 If RE-ONLY is non-nil, strip leading `Re:'s only."
1661   (let ((case-fold-search t))           ;Ignore case.
1662     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1663     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1664       (setq subject (substring subject (match-end 0))))
1665     ;; Remove uninteresting prefixes.
1666     (when (and (not re-only)
1667                gnus-simplify-ignored-prefixes
1668                (string-match gnus-simplify-ignored-prefixes subject))
1669       (setq subject (substring subject (match-end 0))))
1670     ;; Remove words in parentheses from end.
1671     (unless re-only
1672       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1673         (setq subject (substring subject 0 (match-beginning 0)))))
1674     ;; Return subject string.
1675     subject))
1676
1677 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1678 ;; all whitespace.
1679 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1680   (goto-char (point-min))
1681   (while (re-search-forward regexp nil t)
1682     (replace-match (or newtext ""))))
1683
1684 (defun gnus-simplify-buffer-fuzzy ()
1685   "Simplify string in the buffer fuzzily.
1686 The string in the accessible portion of the current buffer is simplified.
1687 It is assumed to be a single-line subject.
1688 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1689 matter is removed.  Additional things can be deleted by setting
1690 `gnus-simplify-subject-fuzzy-regexp'."
1691   (let ((case-fold-search t)
1692         (modified-tick))
1693     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1694
1695     (while (not (eq modified-tick (buffer-modified-tick)))
1696       (setq modified-tick (buffer-modified-tick))
1697       (cond
1698        ((listp gnus-simplify-subject-fuzzy-regexp)
1699         (mapc 'gnus-simplify-buffer-fuzzy-step
1700               gnus-simplify-subject-fuzzy-regexp))
1701        (gnus-simplify-subject-fuzzy-regexp
1702         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1703       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1704       (gnus-simplify-buffer-fuzzy-step
1705        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1706       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1707
1708     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1709     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1710     (gnus-simplify-buffer-fuzzy-step " $")
1711     (gnus-simplify-buffer-fuzzy-step "^ +")))
1712
1713 (defun gnus-simplify-subject-fuzzy (subject)
1714   "Simplify a subject string fuzzily.
1715 See `gnus-simplify-buffer-fuzzy' for details."
1716   (save-excursion
1717     (gnus-set-work-buffer)
1718     (let ((case-fold-search t))
1719       ;; Remove uninteresting prefixes.
1720       (when (and gnus-simplify-ignored-prefixes
1721                  (string-match gnus-simplify-ignored-prefixes subject))
1722         (setq subject (substring subject (match-end 0))))
1723       (insert subject)
1724       (inline (gnus-simplify-buffer-fuzzy))
1725       (buffer-string))))
1726
1727 (defsubst gnus-simplify-subject-fully (subject)
1728   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1729   (cond
1730    (gnus-simplify-subject-functions
1731     (gnus-map-function gnus-simplify-subject-functions subject))
1732    ((null gnus-summary-gather-subject-limit)
1733     (gnus-simplify-subject-re subject))
1734    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1735     (gnus-simplify-subject-fuzzy subject))
1736    ((numberp gnus-summary-gather-subject-limit)
1737     (truncate-string-to-width (gnus-simplify-subject-re subject)
1738                               gnus-summary-gather-subject-limit))
1739    (t
1740     subject)))
1741
1742 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1743   "Check whether two subjects are equal.
1744 If optional argument SIMPLE-FIRST is t, first argument is already
1745 simplified."
1746   (cond
1747    ((null simple-first)
1748     (equal (gnus-simplify-subject-fully s1)
1749            (gnus-simplify-subject-fully s2)))
1750    (t
1751     (equal s1
1752            (gnus-simplify-subject-fully s2)))))
1753
1754 (defun gnus-summary-bubble-group ()
1755   "Increase the score of the current group.
1756 This is a handy function to add to `gnus-summary-exit-hook' to
1757 increase the score of each group you read."
1758   (gnus-group-add-score gnus-newsgroup-name))
1759
1760 \f
1761 ;;;
1762 ;;; Gnus summary mode
1763 ;;;
1764
1765 (put 'gnus-summary-mode 'mode-class 'special)
1766
1767 (defvar gnus-article-commands-menu)
1768
1769 ;; Non-orthogonal keys
1770
1771 (gnus-define-keys gnus-summary-mode-map
1772   " " gnus-summary-next-page
1773   "\177" gnus-summary-prev-page
1774   [delete] gnus-summary-prev-page
1775   [backspace] gnus-summary-prev-page
1776   "\r" gnus-summary-scroll-up
1777   "\M-\r" gnus-summary-scroll-down
1778   "n" gnus-summary-next-unread-article
1779   "p" gnus-summary-prev-unread-article
1780   "N" gnus-summary-next-article
1781   "P" gnus-summary-prev-article
1782   "\M-\C-n" gnus-summary-next-same-subject
1783   "\M-\C-p" gnus-summary-prev-same-subject
1784   "\M-n" gnus-summary-next-unread-subject
1785   "\M-p" gnus-summary-prev-unread-subject
1786   "." gnus-summary-first-unread-article
1787   "," gnus-summary-best-unread-article
1788   "\M-s" gnus-summary-search-article-forward
1789   "\M-r" gnus-summary-search-article-backward
1790   "\M-S" gnus-summary-repeat-search-article-forward
1791   "\M-R" gnus-summary-repeat-search-article-backward
1792   "<" gnus-summary-beginning-of-article
1793   ">" gnus-summary-end-of-article
1794   "j" gnus-summary-goto-article
1795   "^" gnus-summary-refer-parent-article
1796   "\M-^" gnus-summary-refer-article
1797   "u" gnus-summary-tick-article-forward
1798   "!" gnus-summary-tick-article-forward
1799   "U" gnus-summary-tick-article-backward
1800   "d" gnus-summary-mark-as-read-forward
1801   "D" gnus-summary-mark-as-read-backward
1802   "E" gnus-summary-mark-as-expirable
1803   "\M-u" gnus-summary-clear-mark-forward
1804   "\M-U" gnus-summary-clear-mark-backward
1805   "k" gnus-summary-kill-same-subject-and-select
1806   "\C-k" gnus-summary-kill-same-subject
1807   "\M-\C-k" gnus-summary-kill-thread
1808   "\M-\C-l" gnus-summary-lower-thread
1809   "e" gnus-summary-edit-article
1810   "#" gnus-summary-mark-as-processable
1811   "\M-#" gnus-summary-unmark-as-processable
1812   "\M-\C-t" gnus-summary-toggle-threads
1813   "\M-\C-s" gnus-summary-show-thread
1814   "\M-\C-h" gnus-summary-hide-thread
1815   "\M-\C-f" gnus-summary-next-thread
1816   "\M-\C-b" gnus-summary-prev-thread
1817   [(meta down)] gnus-summary-next-thread
1818   [(meta up)] gnus-summary-prev-thread
1819   "\M-\C-u" gnus-summary-up-thread
1820   "\M-\C-d" gnus-summary-down-thread
1821   "&" gnus-summary-execute-command
1822   "c" gnus-summary-catchup-and-exit
1823   "\C-w" gnus-summary-mark-region-as-read
1824   "\C-t" gnus-summary-toggle-truncation
1825   "?" gnus-summary-mark-as-dormant
1826   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1827   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1828   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1829   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1830   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1831   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1832   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1833   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1834   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1835   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1836   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1837   "=" gnus-summary-expand-window
1838   "\C-x\C-s" gnus-summary-reselect-current-group
1839   "\M-g" gnus-summary-rescan-group
1840   "w" gnus-summary-stop-page-breaking
1841   "\C-c\C-r" gnus-summary-caesar-message
1842   "f" gnus-summary-followup
1843   "F" gnus-summary-followup-with-original
1844   "C" gnus-summary-cancel-article
1845   "r" gnus-summary-reply
1846   "R" gnus-summary-reply-with-original
1847   "\C-c\C-f" gnus-summary-mail-forward
1848   "o" gnus-summary-save-article
1849   "\C-o" gnus-summary-save-article-mail
1850   "|" gnus-summary-pipe-output
1851   "\M-k" gnus-summary-edit-local-kill
1852   "\M-K" gnus-summary-edit-global-kill
1853   ;; "V" gnus-version
1854   "\C-c\C-d" gnus-summary-describe-group
1855   "q" gnus-summary-exit
1856   "Q" gnus-summary-exit-no-update
1857   "\C-c\C-i" gnus-info-find-node
1858   gnus-mouse-2 gnus-mouse-pick-article
1859   [follow-link] mouse-face
1860   "m" gnus-summary-mail-other-window
1861   "a" gnus-summary-post-news
1862   "i" gnus-summary-news-other-window
1863   "x" gnus-summary-limit-to-unread
1864   "s" gnus-summary-isearch-article
1865   "t" gnus-summary-toggle-header
1866   "g" gnus-summary-show-article
1867   "l" gnus-summary-goto-last-article
1868   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1869   "\C-d" gnus-summary-enter-digest-group
1870   "\M-\C-d" gnus-summary-read-document
1871   "\M-\C-e" gnus-summary-edit-parameters
1872   "\M-\C-a" gnus-summary-customize-parameters
1873   "\C-c\C-b" gnus-bug
1874   "*" gnus-cache-enter-article
1875   "\M-*" gnus-cache-remove-article
1876   "\M-&" gnus-summary-universal-argument
1877   "\C-l" gnus-recenter
1878   "I" gnus-summary-increase-score
1879   "L" gnus-summary-lower-score
1880   "\M-i" gnus-symbolic-argument
1881   "h" gnus-summary-select-article-buffer
1882
1883   "b" gnus-article-view-part
1884   "\M-t" gnus-summary-toggle-display-buttonized
1885
1886   "V" gnus-summary-score-map
1887   "X" gnus-uu-extract-map
1888   "S" gnus-summary-send-map)
1889
1890 ;; Sort of orthogonal keymap
1891 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1892   "t" gnus-summary-tick-article-forward
1893   "!" gnus-summary-tick-article-forward
1894   "d" gnus-summary-mark-as-read-forward
1895   "r" gnus-summary-mark-as-read-forward
1896   "c" gnus-summary-clear-mark-forward
1897   " " gnus-summary-clear-mark-forward
1898   "e" gnus-summary-mark-as-expirable
1899   "x" gnus-summary-mark-as-expirable
1900   "?" gnus-summary-mark-as-dormant
1901   "b" gnus-summary-set-bookmark
1902   "B" gnus-summary-remove-bookmark
1903   "#" gnus-summary-mark-as-processable
1904   "\M-#" gnus-summary-unmark-as-processable
1905   "S" gnus-summary-limit-include-expunged
1906   "C" gnus-summary-catchup
1907   "H" gnus-summary-catchup-to-here
1908   "h" gnus-summary-catchup-from-here
1909   "\C-c" gnus-summary-catchup-all
1910   "k" gnus-summary-kill-same-subject-and-select
1911   "K" gnus-summary-kill-same-subject
1912   "P" gnus-uu-mark-map)
1913
1914 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1915   "c" gnus-summary-clear-above
1916   "u" gnus-summary-tick-above
1917   "m" gnus-summary-mark-above
1918   "k" gnus-summary-kill-below)
1919
1920 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1921   "/" gnus-summary-limit-to-subject
1922   "n" gnus-summary-limit-to-articles
1923   "b" gnus-summary-limit-to-bodies
1924   "h" gnus-summary-limit-to-headers
1925   "w" gnus-summary-pop-limit
1926   "s" gnus-summary-limit-to-subject
1927   "a" gnus-summary-limit-to-author
1928   "u" gnus-summary-limit-to-unread
1929   "m" gnus-summary-limit-to-marks
1930   "M" gnus-summary-limit-exclude-marks
1931   "v" gnus-summary-limit-to-score
1932   "*" gnus-summary-limit-include-cached
1933   "D" gnus-summary-limit-include-dormant
1934   "T" gnus-summary-limit-include-thread
1935   "d" gnus-summary-limit-exclude-dormant
1936   "t" gnus-summary-limit-to-age
1937   "." gnus-summary-limit-to-unseen
1938   "x" gnus-summary-limit-to-extra
1939   "p" gnus-summary-limit-to-display-predicate
1940   "E" gnus-summary-limit-include-expunged
1941   "c" gnus-summary-limit-exclude-childless-dormant
1942   "C" gnus-summary-limit-mark-excluded-as-read
1943   "o" gnus-summary-insert-old-articles
1944   "N" gnus-summary-insert-new-articles
1945   "S" gnus-summary-limit-to-singletons
1946   "r" gnus-summary-limit-to-replied
1947   "R" gnus-summary-limit-to-recipient
1948   "A" gnus-summary-limit-to-address)
1949
1950 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1951   "n" gnus-summary-next-unread-article
1952   "p" gnus-summary-prev-unread-article
1953   "N" gnus-summary-next-article
1954   "P" gnus-summary-prev-article
1955   "\C-n" gnus-summary-next-same-subject
1956   "\C-p" gnus-summary-prev-same-subject
1957   "\M-n" gnus-summary-next-unread-subject
1958   "\M-p" gnus-summary-prev-unread-subject
1959   "f" gnus-summary-first-unread-article
1960   "b" gnus-summary-best-unread-article
1961   "j" gnus-summary-goto-article
1962   "g" gnus-summary-goto-subject
1963   "l" gnus-summary-goto-last-article
1964   "o" gnus-summary-pop-article)
1965
1966 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1967   "k" gnus-summary-kill-thread
1968   "E" gnus-summary-expire-thread
1969   "l" gnus-summary-lower-thread
1970   "i" gnus-summary-raise-thread
1971   "T" gnus-summary-toggle-threads
1972   "t" gnus-summary-rethread-current
1973   "^" gnus-summary-reparent-thread
1974   "\M-^" gnus-summary-reparent-children
1975   "s" gnus-summary-show-thread
1976   "S" gnus-summary-show-all-threads
1977   "h" gnus-summary-hide-thread
1978   "H" gnus-summary-hide-all-threads
1979   "n" gnus-summary-next-thread
1980   "p" gnus-summary-prev-thread
1981   "u" gnus-summary-up-thread
1982   "o" gnus-summary-top-thread
1983   "d" gnus-summary-down-thread
1984   "#" gnus-uu-mark-thread
1985   "\M-#" gnus-uu-unmark-thread)
1986
1987 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1988   "g" gnus-summary-prepare
1989   "c" gnus-summary-insert-cached-articles
1990   "d" gnus-summary-insert-dormant-articles
1991   "t" gnus-summary-insert-ticked-articles)
1992
1993 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1994   "c" gnus-summary-catchup-and-exit
1995   "C" gnus-summary-catchup-all-and-exit
1996   "E" gnus-summary-exit-no-update
1997   "Q" gnus-summary-exit
1998   "Z" gnus-summary-exit
1999   "n" gnus-summary-catchup-and-goto-next-group
2000   "p" gnus-summary-catchup-and-goto-prev-group
2001   "R" gnus-summary-reselect-current-group
2002   "G" gnus-summary-rescan-group
2003   "N" gnus-summary-next-group
2004   "s" gnus-summary-save-newsrc
2005   "P" gnus-summary-prev-group)
2006
2007 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2008   " " gnus-summary-next-page
2009   "n" gnus-summary-next-page
2010   "\177" gnus-summary-prev-page
2011   [delete] gnus-summary-prev-page
2012   "p" gnus-summary-prev-page
2013   "\r" gnus-summary-scroll-up
2014   "\M-\r" gnus-summary-scroll-down
2015   "<" gnus-summary-beginning-of-article
2016   ">" gnus-summary-end-of-article
2017   "b" gnus-summary-beginning-of-article
2018   "e" gnus-summary-end-of-article
2019   "^" gnus-summary-refer-parent-article
2020   "r" gnus-summary-refer-parent-article
2021   "D" gnus-summary-enter-digest-group
2022   "R" gnus-summary-refer-references
2023   "T" gnus-summary-refer-thread
2024   "g" gnus-summary-show-article
2025   "s" gnus-summary-isearch-article
2026   "P" gnus-summary-print-article
2027   "S" gnus-sticky-article
2028   "M" gnus-mailing-list-insinuate
2029   "t" gnus-article-babel)
2030
2031 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2032   "b" gnus-article-add-buttons
2033   "B" gnus-article-add-buttons-to-head
2034   "o" gnus-article-treat-overstrike
2035   "e" gnus-article-emphasize
2036   "w" gnus-article-fill-cited-article
2037   "Q" gnus-article-fill-long-lines
2038   "L" gnus-article-toggle-truncate-lines
2039   "C" gnus-article-capitalize-sentences
2040   "c" gnus-article-remove-cr
2041   "q" gnus-article-de-quoted-unreadable
2042   "6" gnus-article-de-base64-unreadable
2043   "Z" gnus-article-decode-HZ
2044   "A" gnus-article-treat-ansi-sequences
2045   "h" gnus-article-wash-html
2046   "u" gnus-article-unsplit-urls
2047   "s" gnus-summary-force-verify-and-decrypt
2048   "f" gnus-article-display-x-face
2049   "l" gnus-summary-stop-page-breaking
2050   "r" gnus-summary-caesar-message
2051   "m" gnus-summary-morse-message
2052   "t" gnus-summary-toggle-header
2053   "g" gnus-treat-smiley
2054   "v" gnus-summary-verbose-headers
2055   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2056   "p" gnus-article-verify-x-pgp-sig
2057   "d" gnus-article-treat-dumbquotes
2058   "i" gnus-summary-idna-message)
2059
2060 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2061   ;; mnemonic: deuglif*Y*
2062   "u" gnus-article-outlook-unwrap-lines
2063   "a" gnus-article-outlook-repair-attribution
2064   "c" gnus-article-outlook-rearrange-citation
2065   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2066
2067 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2068   "a" gnus-article-hide
2069   "h" gnus-article-hide-headers
2070   "b" gnus-article-hide-boring-headers
2071   "s" gnus-article-hide-signature
2072   "c" gnus-article-hide-citation
2073   "C" gnus-article-hide-citation-in-followups
2074   "l" gnus-article-hide-list-identifiers
2075   "B" gnus-article-strip-banner
2076   "P" gnus-article-hide-pem
2077   "\C-c" gnus-article-hide-citation-maybe)
2078
2079 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2080   "a" gnus-article-highlight
2081   "h" gnus-article-highlight-headers
2082   "c" gnus-article-highlight-citation
2083   "s" gnus-article-highlight-signature)
2084
2085 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2086   "f" gnus-article-treat-fold-headers
2087   "u" gnus-article-treat-unfold-headers
2088   "n" gnus-article-treat-fold-newsgroups)
2089
2090 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2091   "x" gnus-article-display-x-face
2092   "d" gnus-article-display-face
2093   "s" gnus-treat-smiley
2094   "D" gnus-article-remove-images
2095   "f" gnus-treat-from-picon
2096   "m" gnus-treat-mail-picon
2097   "n" gnus-treat-newsgroups-picon)
2098
2099 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2100   "w" gnus-article-decode-mime-words
2101   "c" gnus-article-decode-charset
2102   "v" gnus-mime-view-all-parts
2103   "b" gnus-article-view-part)
2104
2105 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2106   "z" gnus-article-date-ut
2107   "u" gnus-article-date-ut
2108   "l" gnus-article-date-local
2109   "p" gnus-article-date-english
2110   "e" gnus-article-date-lapsed
2111   "o" gnus-article-date-original
2112   "i" gnus-article-date-iso8601
2113   "s" gnus-article-date-user)
2114
2115 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2116   "t" gnus-article-remove-trailing-blank-lines
2117   "l" gnus-article-strip-leading-blank-lines
2118   "m" gnus-article-strip-multiple-blank-lines
2119   "a" gnus-article-strip-blank-lines
2120   "A" gnus-article-strip-all-blank-lines
2121   "s" gnus-article-strip-leading-space
2122   "e" gnus-article-strip-trailing-space
2123   "w" gnus-article-remove-leading-whitespace)
2124
2125 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2126   "v" gnus-version
2127   "f" gnus-summary-fetch-faq
2128   "d" gnus-summary-describe-group
2129   "h" gnus-summary-describe-briefly
2130   "i" gnus-info-find-node
2131   "c" gnus-group-fetch-charter
2132   "C" gnus-group-fetch-control)
2133
2134 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2135   "e" gnus-summary-expire-articles
2136   "\M-\C-e" gnus-summary-expire-articles-now
2137   "\177" gnus-summary-delete-article
2138   [delete] gnus-summary-delete-article
2139   [backspace] gnus-summary-delete-article
2140   "m" gnus-summary-move-article
2141   "r" gnus-summary-respool-article
2142   "w" gnus-summary-edit-article
2143   "c" gnus-summary-copy-article
2144   "B" gnus-summary-crosspost-article
2145   "q" gnus-summary-respool-query
2146   "t" gnus-summary-respool-trace
2147   "i" gnus-summary-import-article
2148   "I" gnus-summary-create-article
2149   "p" gnus-summary-article-posted-p)
2150
2151 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2152   "o" gnus-summary-save-article
2153   "m" gnus-summary-save-article-mail
2154   "F" gnus-summary-write-article-file
2155   "r" gnus-summary-save-article-rmail
2156   "f" gnus-summary-save-article-file
2157   "b" gnus-summary-save-article-body-file
2158   "B" gnus-summary-write-article-body-file
2159   "h" gnus-summary-save-article-folder
2160   "v" gnus-summary-save-article-vm
2161   "p" gnus-summary-pipe-output
2162   "P" gnus-summary-muttprint
2163   "s" gnus-soup-add-article)
2164
2165 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2166   "b" gnus-summary-display-buttonized
2167   "m" gnus-summary-repair-multipart
2168   "v" gnus-article-view-part
2169   "o" gnus-article-save-part
2170   "O" gnus-article-save-part-and-strip
2171   "r" gnus-article-replace-part
2172   "d" gnus-article-delete-part
2173   "t" gnus-article-view-part-as-type
2174   "j" gnus-article-jump-to-part
2175   "c" gnus-article-copy-part
2176   "C" gnus-article-view-part-as-charset
2177   "e" gnus-article-view-part-externally
2178   "H" gnus-article-browse-html-article
2179   "E" gnus-article-encrypt-body
2180   "i" gnus-article-inline-part
2181   "|" gnus-article-pipe-part)
2182
2183 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2184   "p" gnus-summary-mark-as-processable
2185   "u" gnus-summary-unmark-as-processable
2186   "U" gnus-summary-unmark-all-processable
2187   "v" gnus-uu-mark-over
2188   "s" gnus-uu-mark-series
2189   "r" gnus-uu-mark-region
2190   "g" gnus-uu-unmark-region
2191   "R" gnus-uu-mark-by-regexp
2192   "G" gnus-uu-unmark-by-regexp
2193   "t" gnus-uu-mark-thread
2194   "T" gnus-uu-unmark-thread
2195   "a" gnus-uu-mark-all
2196   "b" gnus-uu-mark-buffer
2197   "S" gnus-uu-mark-sparse
2198   "k" gnus-summary-kill-process-mark
2199   "y" gnus-summary-yank-process-mark
2200   "w" gnus-summary-save-process-mark
2201   "i" gnus-uu-invert-processable)
2202
2203 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2204   ;;"x" gnus-uu-extract-any
2205   "m" gnus-summary-save-parts
2206   "u" gnus-uu-decode-uu
2207   "U" gnus-uu-decode-uu-and-save
2208   "s" gnus-uu-decode-unshar
2209   "S" gnus-uu-decode-unshar-and-save
2210   "o" gnus-uu-decode-save
2211   "O" gnus-uu-decode-save
2212   "b" gnus-uu-decode-binhex
2213   "B" gnus-uu-decode-binhex
2214   "Y" gnus-uu-decode-yenc
2215   "p" gnus-uu-decode-postscript
2216   "P" gnus-uu-decode-postscript-and-save)
2217
2218 (gnus-define-keys
2219     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2220   "u" gnus-uu-decode-uu-view
2221   "U" gnus-uu-decode-uu-and-save-view
2222   "s" gnus-uu-decode-unshar-view
2223   "S" gnus-uu-decode-unshar-and-save-view
2224   "o" gnus-uu-decode-save-view
2225   "O" gnus-uu-decode-save-view
2226   "b" gnus-uu-decode-binhex-view
2227   "B" gnus-uu-decode-binhex-view
2228   "p" gnus-uu-decode-postscript-view
2229   "P" gnus-uu-decode-postscript-and-save-view)
2230
2231 (defvar gnus-article-post-menu nil)
2232
2233 (defconst gnus-summary-menu-maxlen 20)
2234
2235 (defun gnus-summary-menu-split (menu)
2236   ;; If we have lots of elements, divide them into groups of 20
2237   ;; and make a pane (or submenu) for each one.
2238   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2239       (let ((menu menu) sublists next
2240             (i 1))
2241         (while menu
2242           ;; Pull off the next gnus-summary-menu-maxlen elements
2243           ;; and make them the next element of sublist.
2244           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2245           (if next
2246               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2247                       nil))
2248           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2249                                              (aref (car (last menu)) 0)) menu)
2250                                sublists))
2251           (setq i (1+ i))
2252           (setq menu next))
2253         (nreverse sublists))
2254     ;; Few elements--put them all in one pane.
2255     menu))
2256
2257 (defun gnus-summary-make-menu-bar ()
2258   (gnus-turn-off-edit-menu 'summary)
2259
2260   (unless (boundp 'gnus-summary-misc-menu)
2261
2262     (easy-menu-define
2263       gnus-summary-kill-menu gnus-summary-mode-map ""
2264       (cons
2265        "Score"
2266        (nconc
2267         (list
2268          ["Customize" gnus-score-customize t])
2269         (gnus-make-score-map 'increase)
2270         (gnus-make-score-map 'lower)
2271         '(("Mark"
2272            ["Kill below" gnus-summary-kill-below t]
2273            ["Mark above" gnus-summary-mark-above t]
2274            ["Tick above" gnus-summary-tick-above t]
2275            ["Clear above" gnus-summary-clear-above t])
2276           ["Current score" gnus-summary-current-score t]
2277           ["Set score" gnus-summary-set-score t]
2278           ["Switch current score file..." gnus-score-change-score-file t]
2279           ["Set mark below..." gnus-score-set-mark-below t]
2280           ["Set expunge below..." gnus-score-set-expunge-below t]
2281           ["Edit current score file" gnus-score-edit-current-scores t]
2282           ["Edit score file..." gnus-score-edit-file t]
2283           ["Trace score" gnus-score-find-trace t]
2284           ["Find words" gnus-score-find-favourite-words t]
2285           ["Rescore buffer" gnus-summary-rescore t]
2286           ["Increase score..." gnus-summary-increase-score t]
2287           ["Lower score..." gnus-summary-lower-score t]))))
2288
2289     ;; Define both the Article menu in the summary buffer and the
2290     ;; equivalent Commands menu in the article buffer here for
2291     ;; consistency.
2292     (let ((innards
2293            `(("Hide"
2294               ["All" gnus-article-hide t]
2295               ["Headers" gnus-article-hide-headers t]
2296               ["Signature" gnus-article-hide-signature t]
2297               ["Citation" gnus-article-hide-citation t]
2298               ["List identifiers" gnus-article-hide-list-identifiers t]
2299               ["Banner" gnus-article-strip-banner t]
2300               ["Boring headers" gnus-article-hide-boring-headers t])
2301              ("Highlight"
2302               ["All" gnus-article-highlight t]
2303               ["Headers" gnus-article-highlight-headers t]
2304               ["Signature" gnus-article-highlight-signature t]
2305               ["Citation" gnus-article-highlight-citation t])
2306              ("MIME"
2307               ["Words" gnus-article-decode-mime-words t]
2308               ["Charset" gnus-article-decode-charset t]
2309               ["QP" gnus-article-de-quoted-unreadable t]
2310               ["Base64" gnus-article-de-base64-unreadable t]
2311               ["View MIME buttons" gnus-summary-display-buttonized t]
2312               ["View all" gnus-mime-view-all-parts t]
2313               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2314               ["Encrypt body" gnus-article-encrypt-body
2315                :active (not (gnus-group-read-only-p))
2316                ,@(if (featurep 'xemacs) nil
2317                    '(:help "Encrypt the message body on disk"))]
2318               ["Extract all parts..." gnus-summary-save-parts t]
2319               ("Multipart"
2320                ["Repair multipart" gnus-summary-repair-multipart t]
2321                ["Pipe part..." gnus-article-pipe-part t]
2322                ["Inline part" gnus-article-inline-part t]
2323                ["View part as type..." gnus-article-view-part-as-type t]
2324                ["Encrypt body" gnus-article-encrypt-body
2325                 :active (not (gnus-group-read-only-p))
2326                ,@(if (featurep 'xemacs) nil
2327                    '(:help "Encrypt the message body on disk"))]
2328                ["View part externally" gnus-article-view-part-externally t]
2329                ["View HTML parts in browser" gnus-article-browse-html-article t]
2330                ["View part with charset..." gnus-article-view-part-as-charset t]
2331                ["Copy part" gnus-article-copy-part t]
2332                ["Save part..." gnus-article-save-part t]
2333                ["View part" gnus-article-view-part t]))
2334              ("Date"
2335               ["Local" gnus-article-date-local t]
2336               ["ISO8601" gnus-article-date-iso8601 t]
2337               ["UT" gnus-article-date-ut t]
2338               ["Original" gnus-article-date-original t]
2339               ["Lapsed" gnus-article-date-lapsed t]
2340               ["User-defined" gnus-article-date-user t])
2341              ("Display"
2342               ["Remove images" gnus-article-remove-images t]
2343               ["Toggle smiley" gnus-treat-smiley t]
2344               ["Show X-Face" gnus-article-display-x-face t]
2345               ["Show picons in From" gnus-treat-from-picon t]
2346               ["Show picons in mail headers" gnus-treat-mail-picon t]
2347               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2348               ("View as different encoding"
2349                ,@(gnus-summary-menu-split
2350                   (mapcar
2351                    (lambda (cs)
2352                      ;; Since easymenu under Emacs doesn't allow
2353                      ;; lambda forms for menu commands, we should
2354                      ;; provide intern'ed function symbols.
2355                      (let ((command (intern (format "\
2356 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2357                        (fset command
2358                              `(lambda ()
2359                                 (interactive)
2360                                 (let ((gnus-summary-show-article-charset-alist
2361                                        '((1 . ,cs))))
2362                                   (gnus-summary-show-article 1))))
2363                        `[,(symbol-name cs) ,command t]))
2364                    (sort (if (fboundp 'coding-system-list)
2365                              (coding-system-list)
2366                            (mapcar 'car mm-mime-mule-charset-alist))
2367                          'string<)))))
2368              ("Washing"
2369               ("Remove Blanks"
2370                ["Leading" gnus-article-strip-leading-blank-lines t]
2371                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2372                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2373                ["All of the above" gnus-article-strip-blank-lines t]
2374                ["All" gnus-article-strip-all-blank-lines t]
2375                ["Leading space" gnus-article-strip-leading-space t]
2376                ["Trailing space" gnus-article-strip-trailing-space t]
2377                ["Leading space in headers"
2378                 gnus-article-remove-leading-whitespace t])
2379               ["Overstrike" gnus-article-treat-overstrike t]
2380               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2381               ["Emphasis" gnus-article-emphasize t]
2382               ["Word wrap" gnus-article-fill-cited-article t]
2383               ["Fill long lines" gnus-article-fill-long-lines t]
2384               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2385               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2386               ["Remove CR" gnus-article-remove-cr t]
2387               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2388               ["Base64" gnus-article-de-base64-unreadable t]
2389               ["Rot 13" gnus-summary-caesar-message
2390                ,@(if (featurep 'xemacs) '(t)
2391                    '(:help "\"Caesar rotate\" article by 13"))]
2392               ["De-IDNA" gnus-summary-idna-message t]
2393               ["Morse decode" gnus-summary-morse-message t]
2394               ["Unix pipe..." gnus-summary-pipe-message t]
2395               ["Add buttons" gnus-article-add-buttons t]
2396               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2397               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2398               ["Verbose header" gnus-summary-verbose-headers t]
2399               ["Toggle header" gnus-summary-toggle-header t]
2400               ["Unfold headers" gnus-article-treat-unfold-headers t]
2401               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2402               ["Html" gnus-article-wash-html t]
2403               ["Unsplit URLs" gnus-article-unsplit-urls t]
2404               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2405               ["Decode HZ" gnus-article-decode-HZ t]
2406               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2407               ("(Outlook) Deuglify"
2408                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2409                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2410                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2411                ["Full (Outlook) deuglify"
2412                 gnus-article-outlook-deuglify-article t])
2413               )
2414              ("Output"
2415               ["Save in default format..." gnus-summary-save-article
2416                ,@(if (featurep 'xemacs) '(t)
2417                    '(:help "Save article using default method"))]
2418               ["Save in file..." gnus-summary-save-article-file
2419                ,@(if (featurep 'xemacs) '(t)
2420                    '(:help "Save article in file"))]
2421               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2422               ["Save in MH folder..." gnus-summary-save-article-folder t]
2423               ["Save in VM folder..." gnus-summary-save-article-vm t]
2424               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2425               ["Save body in file..." gnus-summary-save-article-body-file t]
2426               ["Pipe through a filter..." gnus-summary-pipe-output t]
2427               ["Add to SOUP packet" gnus-soup-add-article t]
2428               ["Print with Muttprint..." gnus-summary-muttprint t]
2429               ["Print" gnus-summary-print-article
2430                ,@(if (featurep 'xemacs) '(t)
2431                    '(:help "Generate and print a PostScript image"))])
2432              ("Copy, move,... (Backend)"
2433               ,@(if (featurep 'xemacs) nil
2434                   '(:help "Copying, moving, expiring articles..."))
2435               ["Respool article..." gnus-summary-respool-article t]
2436               ["Move article..." gnus-summary-move-article
2437                (gnus-check-backend-function
2438                 'request-move-article gnus-newsgroup-name)]
2439               ["Copy article..." gnus-summary-copy-article t]
2440               ["Crosspost article..." gnus-summary-crosspost-article
2441                (gnus-check-backend-function
2442                 'request-replace-article gnus-newsgroup-name)]
2443               ["Import file..." gnus-summary-import-article
2444                (gnus-check-backend-function
2445                 'request-accept-article gnus-newsgroup-name)]
2446               ["Create article..." gnus-summary-create-article
2447                (gnus-check-backend-function
2448                 'request-accept-article gnus-newsgroup-name)]
2449               ["Check if posted" gnus-summary-article-posted-p t]
2450               ["Edit article" gnus-summary-edit-article
2451                (not (gnus-group-read-only-p))]
2452               ["Delete article" gnus-summary-delete-article
2453                (gnus-check-backend-function
2454                 'request-expire-articles gnus-newsgroup-name)]
2455               ["Query respool" gnus-summary-respool-query t]
2456               ["Trace respool" gnus-summary-respool-trace t]
2457               ["Delete expirable articles" gnus-summary-expire-articles-now
2458                (gnus-check-backend-function
2459                 'request-expire-articles gnus-newsgroup-name)])
2460              ("Extract"
2461               ["Uudecode" gnus-uu-decode-uu
2462                ,@(if (featurep 'xemacs) '(t)
2463                    '(:help "Decode uuencoded article(s)"))]
2464               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2465               ["Unshar" gnus-uu-decode-unshar t]
2466               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2467               ["Save" gnus-uu-decode-save t]
2468               ["Binhex" gnus-uu-decode-binhex t]
2469               ["Postscript" gnus-uu-decode-postscript t]
2470               ["All MIME parts" gnus-summary-save-parts t])
2471              ("Cache"
2472               ["Enter article" gnus-cache-enter-article t]
2473               ["Remove article" gnus-cache-remove-article t])
2474              ["Translate" gnus-article-babel t]
2475              ["Select article buffer" gnus-summary-select-article-buffer t]
2476              ["Make article buffer sticky" gnus-sticky-article t]
2477              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2478              ["Isearch article..." gnus-summary-isearch-article t]
2479              ["Beginning of the article" gnus-summary-beginning-of-article t]
2480              ["End of the article" gnus-summary-end-of-article t]
2481              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2482              ["Fetch referenced articles" gnus-summary-refer-references t]
2483              ["Fetch current thread" gnus-summary-refer-thread t]
2484              ["Fetch article with id..." gnus-summary-refer-article t]
2485              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2486              ["Redisplay" gnus-summary-show-article t]
2487              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2488       (easy-menu-define
2489         gnus-summary-article-menu gnus-summary-mode-map ""
2490         (cons "Article" innards))
2491
2492       (if (not (keymapp gnus-summary-article-menu))
2493           (easy-menu-define
2494             gnus-article-commands-menu gnus-article-mode-map ""
2495             (cons "Commands" innards))
2496         ;; in Emacs, don't share menu.
2497         (setq gnus-article-commands-menu
2498               (copy-keymap gnus-summary-article-menu))
2499         (define-key gnus-article-mode-map [menu-bar commands]
2500           (cons "Commands" gnus-article-commands-menu))))
2501
2502     (easy-menu-define
2503       gnus-summary-thread-menu gnus-summary-mode-map ""
2504       '("Threads"
2505         ["Find all messages in thread" gnus-summary-refer-thread t]
2506         ["Toggle threading" gnus-summary-toggle-threads t]
2507         ["Hide threads" gnus-summary-hide-all-threads t]
2508         ["Show threads" gnus-summary-show-all-threads t]
2509         ["Hide thread" gnus-summary-hide-thread t]
2510         ["Show thread" gnus-summary-show-thread t]
2511         ["Go to next thread" gnus-summary-next-thread t]
2512         ["Go to previous thread" gnus-summary-prev-thread t]
2513         ["Go down thread" gnus-summary-down-thread t]
2514         ["Go up thread" gnus-summary-up-thread t]
2515         ["Top of thread" gnus-summary-top-thread t]
2516         ["Mark thread as read" gnus-summary-kill-thread t]
2517         ["Mark thread as expired" gnus-summary-expire-thread t]
2518         ["Lower thread score" gnus-summary-lower-thread t]
2519         ["Raise thread score" gnus-summary-raise-thread t]
2520         ["Rethread current" gnus-summary-rethread-current t]))
2521
2522     (easy-menu-define
2523       gnus-summary-post-menu gnus-summary-mode-map ""
2524       `("Post"
2525         ["Send a message (mail or news)" gnus-summary-post-news
2526          ,@(if (featurep 'xemacs) '(t)
2527              '(:help "Compose a new message (mail or news)"))]
2528         ["Followup" gnus-summary-followup
2529          ,@(if (featurep 'xemacs) '(t)
2530              '(:help "Post followup to this article"))]
2531         ["Followup and yank" gnus-summary-followup-with-original
2532          ,@(if (featurep 'xemacs) '(t)
2533              '(:help "Post followup to this article, quoting its contents"))]
2534         ["Supersede article" gnus-summary-supersede-article t]
2535         ["Cancel article" gnus-summary-cancel-article
2536          ,@(if (featurep 'xemacs) '(t)
2537              '(:help "Cancel an article you posted"))]
2538         ["Reply" gnus-summary-reply t]
2539         ["Reply and yank" gnus-summary-reply-with-original t]
2540         ["Wide reply" gnus-summary-wide-reply t]
2541         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2542          ,@(if (featurep 'xemacs) '(t)
2543              '(:help "Mail a reply, quoting this article"))]
2544         ["Very wide reply" gnus-summary-very-wide-reply t]
2545         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2546          ,@(if (featurep 'xemacs) '(t)
2547              '(:help "Mail a very wide reply, quoting this article"))]
2548         ["Mail forward" gnus-summary-mail-forward t]
2549         ["Post forward" gnus-summary-post-forward t]
2550         ["Digest and mail" gnus-uu-digest-mail-forward t]
2551         ["Digest and post" gnus-uu-digest-post-forward t]
2552         ["Resend message" gnus-summary-resend-message t]
2553         ["Resend message edit" gnus-summary-resend-message-edit t]
2554         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2555         ["Send a mail" gnus-summary-mail-other-window t]
2556         ["Create a local message" gnus-summary-news-other-window t]
2557         ["Uuencode and post" gnus-uu-post-news
2558          ,@(if (featurep 'xemacs) '(t)
2559              '(:help "Post a uuencoded article"))]
2560         ["Followup via news" gnus-summary-followup-to-mail t]
2561         ["Followup via news and yank"
2562          gnus-summary-followup-to-mail-with-original t]
2563         ["Strip signature on reply"
2564          (lambda ()
2565            (interactive)
2566            (if (not (memq message-cite-function
2567                           '(message-cite-original-without-signature
2568                             message-cite-original)))
2569                ;; Stupid workaround for XEmacs not honoring :visible.
2570                (message "Can't toggle this value of `message-cite-function'")
2571              (setq message-cite-function
2572                    (if (eq message-cite-function
2573                            'message-cite-original-without-signature)
2574                        'message-cite-original
2575                      'message-cite-original-without-signature))))
2576          ;; XEmacs barfs on :visible.
2577          ,@(if (featurep 'xemacs) nil
2578              '(:visible (memq message-cite-function
2579                               '(message-cite-original-without-signature
2580                                 message-cite-original))))
2581          :style toggle
2582          :selected (eq message-cite-function
2583                        'message-cite-original-without-signature)
2584          ,@(if (featurep 'xemacs) nil
2585              '(:help "Strip signature from cited article when replying."))]
2586         ;;("Draft"
2587         ;;["Send" gnus-summary-send-draft t]
2588         ;;["Send bounced" gnus-resend-bounced-mail t])
2589         ))
2590
2591     (cond
2592      ((not (keymapp gnus-summary-post-menu))
2593       (setq gnus-article-post-menu gnus-summary-post-menu))
2594      ((not gnus-article-post-menu)
2595       ;; Don't share post menu.
2596       (setq gnus-article-post-menu
2597             (copy-keymap gnus-summary-post-menu))))
2598     (define-key gnus-article-mode-map [menu-bar post]
2599       (cons "Post" gnus-article-post-menu))
2600
2601     (easy-menu-define
2602       gnus-summary-misc-menu gnus-summary-mode-map ""
2603       `("Gnus"
2604         ("Mark Read"
2605          ["Mark as read" gnus-summary-mark-as-read-forward t]
2606          ["Mark same subject and select"
2607           gnus-summary-kill-same-subject-and-select t]
2608          ["Mark same subject" gnus-summary-kill-same-subject t]
2609          ["Catchup" gnus-summary-catchup
2610           ,@(if (featurep 'xemacs) '(t)
2611               '(:help "Mark unread articles in this group as read"))]
2612          ["Catchup all" gnus-summary-catchup-all t]
2613          ["Catchup to here" gnus-summary-catchup-to-here t]
2614          ["Catchup from here" gnus-summary-catchup-from-here t]
2615          ["Catchup region" gnus-summary-mark-region-as-read
2616           (gnus-mark-active-p)]
2617          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2618         ("Mark Various"
2619          ["Tick" gnus-summary-tick-article-forward t]
2620          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2621          ["Remove marks" gnus-summary-clear-mark-forward t]
2622          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2623          ["Set bookmark" gnus-summary-set-bookmark t]
2624          ["Remove bookmark" gnus-summary-remove-bookmark t])
2625         ("Registry Mark"
2626          ["Important"     gnus-registry-set-article-Important-mark    t]
2627          ["Not Important" gnus-registry-remove-article-Important-mark t]
2628          ["Work"          gnus-registry-set-article-Work-mark         t]
2629          ["Not Work"      gnus-registry-remove-article-Work-mark      t]
2630          ["Later"         gnus-registry-set-article-Later-mark        t]
2631          ["Not Later"     gnus-registry-remove-article-Later-mark     t]
2632          ["Personal"      gnus-registry-set-article-Personal-mark     t]
2633          ["Not Personal"  gnus-registry-remove-article-Personal-mark  t]
2634          ["To Do"         gnus-registry-set-article-To-Do-mark        t]
2635          ["Not To Do"     gnus-registry-remove-article-To-Do-mark     t])
2636         ("Limit to"
2637          ["Marks..." gnus-summary-limit-to-marks t]
2638          ["Subject..." gnus-summary-limit-to-subject t]
2639          ["Author..." gnus-summary-limit-to-author t]
2640          ["Recipient..." gnus-summary-limit-to-recipient t]
2641          ["Address..." gnus-summary-limit-to-address t]
2642          ["Age..." gnus-summary-limit-to-age t]
2643          ["Extra..." gnus-summary-limit-to-extra t]
2644          ["Score..." gnus-summary-limit-to-score t]
2645          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2646          ["Unread" gnus-summary-limit-to-unread t]
2647          ["Unseen" gnus-summary-limit-to-unseen t]
2648          ["Singletons" gnus-summary-limit-to-singletons t]
2649          ["Replied" gnus-summary-limit-to-replied t]
2650          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2651          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2652          ["Pop limit" gnus-summary-pop-limit t]
2653          ["Show dormant" gnus-summary-limit-include-dormant t]
2654          ["Hide childless dormant"
2655           gnus-summary-limit-exclude-childless-dormant t]
2656          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2657          ["Hide marked" gnus-summary-limit-exclude-marks t]
2658          ["Show expunged" gnus-summary-limit-include-expunged t])
2659         ("Process Mark"
2660          ["Set mark" gnus-summary-mark-as-processable t]
2661          ["Remove mark" gnus-summary-unmark-as-processable t]
2662          ["Remove all marks" gnus-summary-unmark-all-processable t]
2663          ["Invert marks" gnus-uu-invert-processable t]
2664          ["Mark above" gnus-uu-mark-over t]
2665          ["Mark series" gnus-uu-mark-series t]
2666          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2667          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2668          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2669          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2670          ["Mark all" gnus-uu-mark-all t]
2671          ["Mark buffer" gnus-uu-mark-buffer t]
2672          ["Mark sparse" gnus-uu-mark-sparse t]
2673          ["Mark thread" gnus-uu-mark-thread t]
2674          ["Unmark thread" gnus-uu-unmark-thread t]
2675          ("Process Mark Sets"
2676           ["Kill" gnus-summary-kill-process-mark t]
2677           ["Yank" gnus-summary-yank-process-mark
2678            gnus-newsgroup-process-stack]
2679           ["Save" gnus-summary-save-process-mark t]
2680           ["Run command on marked..." gnus-summary-universal-argument t]))
2681         ("Scroll article"
2682          ["Page forward" gnus-summary-next-page
2683           ,@(if (featurep 'xemacs) '(t)
2684               '(:help "Show next page of article"))]
2685          ["Page backward" gnus-summary-prev-page
2686           ,@(if (featurep 'xemacs) '(t)
2687               '(:help "Show previous page of article"))]
2688          ["Line forward" gnus-summary-scroll-up t])
2689         ("Move"
2690          ["Next unread article" gnus-summary-next-unread-article t]
2691          ["Previous unread article" gnus-summary-prev-unread-article t]
2692          ["Next article" gnus-summary-next-article t]
2693          ["Previous article" gnus-summary-prev-article t]
2694          ["Next unread subject" gnus-summary-next-unread-subject t]
2695          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2696          ["Next article same subject" gnus-summary-next-same-subject t]
2697          ["Previous article same subject" gnus-summary-prev-same-subject t]
2698          ["First unread article" gnus-summary-first-unread-article t]
2699          ["Best unread article" gnus-summary-best-unread-article t]
2700          ["Go to subject number..." gnus-summary-goto-subject t]
2701          ["Go to article number..." gnus-summary-goto-article t]
2702          ["Go to the last article" gnus-summary-goto-last-article t]
2703          ["Pop article off history" gnus-summary-pop-article t])
2704         ("Sort"
2705          ["Sort by number" gnus-summary-sort-by-number t]
2706          ["Sort by author" gnus-summary-sort-by-author t]
2707          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2708          ["Sort by subject" gnus-summary-sort-by-subject t]
2709          ["Sort by date" gnus-summary-sort-by-date t]
2710          ["Sort by score" gnus-summary-sort-by-score t]
2711          ["Sort by lines" gnus-summary-sort-by-lines t]
2712          ["Sort by characters" gnus-summary-sort-by-chars t]
2713          ["Randomize" gnus-summary-sort-by-random t]
2714          ["Original sort" gnus-summary-sort-by-original t])
2715         ("Help"
2716          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2717          ["Describe group" gnus-summary-describe-group t]
2718          ["Fetch charter" gnus-group-fetch-charter
2719           ,@(if (featurep 'xemacs) nil
2720               '(:help "Display the charter of the current group"))]
2721          ["Fetch control message" gnus-group-fetch-control
2722           ,@(if (featurep 'xemacs) nil
2723               '(:help "Display the archived control message for the current group"))]
2724          ["Read manual" gnus-info-find-node t])
2725         ("Modes"
2726          ["Pick and read" gnus-pick-mode t]
2727          ["Binary" gnus-binary-mode t])
2728         ("Regeneration"
2729          ["Regenerate" gnus-summary-prepare t]
2730          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2731          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2732          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2733          ["Toggle threading" gnus-summary-toggle-threads t])
2734         ["See old articles" gnus-summary-insert-old-articles t]
2735         ["See new articles" gnus-summary-insert-new-articles t]
2736         ["Filter articles..." gnus-summary-execute-command t]
2737         ["Run command on articles..." gnus-summary-universal-argument t]
2738         ["Search articles forward..." gnus-summary-search-article-forward t]
2739         ["Search articles backward..." gnus-summary-search-article-backward t]
2740         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2741         ["Expand window" gnus-summary-expand-window t]
2742         ["Expire expirable articles" gnus-summary-expire-articles
2743          (gnus-check-backend-function
2744           'request-expire-articles gnus-newsgroup-name)]
2745         ["Edit local kill file" gnus-summary-edit-local-kill t]
2746         ["Edit main kill file" gnus-summary-edit-global-kill t]
2747         ["Edit group parameters" gnus-summary-edit-parameters t]
2748         ["Customize group parameters" gnus-summary-customize-parameters t]
2749         ["Send a bug report" gnus-bug t]
2750         ("Exit"
2751          ["Catchup and exit" gnus-summary-catchup-and-exit
2752           ,@(if (featurep 'xemacs) '(t)
2753               '(:help "Mark unread articles in this group as read, then exit"))]
2754          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2755          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2756          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2757          ["Exit group" gnus-summary-exit
2758           ,@(if (featurep 'xemacs) '(t)
2759               '(:help "Exit current group, return to group selection mode"))]
2760          ["Exit group without updating" gnus-summary-exit-no-update t]
2761          ["Exit and goto next group" gnus-summary-next-group t]
2762          ["Exit and goto prev group" gnus-summary-prev-group t]
2763          ["Reselect group" gnus-summary-reselect-current-group t]
2764          ["Rescan group" gnus-summary-rescan-group t]
2765          ["Update dribble" gnus-summary-save-newsrc t])))
2766
2767     (gnus-run-hooks 'gnus-summary-menu-hook)))
2768
2769 (defvar gnus-summary-tool-bar-map nil)
2770
2771 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2772 ;; affect _new_ message buffers.  We might add a function that walks thru all
2773 ;; summary-mode buffers and force the update.
2774 (defun gnus-summary-tool-bar-update (&optional symbol value)
2775   "Update summary mode toolbar.
2776 Setter function for custom variables."
2777   (setq-default gnus-summary-tool-bar-map nil)
2778   (when symbol
2779     ;; When used as ":set" function:
2780     (set-default symbol value))
2781   (when (gnus-buffer-live-p gnus-summary-buffer)
2782     (with-current-buffer gnus-summary-buffer
2783       (gnus-summary-make-tool-bar))))
2784
2785 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2786                                      'gnus-summary-tool-bar-gnome
2787                                    'gnus-summary-tool-bar-retro)
2788   "Specifies the Gnus summary tool bar.
2789
2790 It can be either a list or a symbol refering to a list.  See
2791 `gmm-tool-bar-from-list' for the format of the list.  The
2792 default key map is `gnus-summary-mode-map'.
2793
2794 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2795 `gnus-summary-tool-bar-retro'."
2796   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2797                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2798                  (repeat :tag "User defined list" gmm-tool-bar-item)
2799                  (symbol))
2800   :version "23.1" ;; No Gnus
2801   :initialize 'custom-initialize-default
2802   :set 'gnus-summary-tool-bar-update
2803   :group 'gnus-summary)
2804
2805 (defcustom gnus-summary-tool-bar-gnome
2806   '((gnus-summary-post-news "mail/compose" nil)
2807     (gnus-summary-insert-new-articles "mail/inbox" nil
2808                                       :visible (or (not gnus-agent)
2809                                                    gnus-plugged))
2810     (gnus-summary-reply-with-original "mail/reply")
2811     (gnus-summary-reply "mail/reply" nil :visible nil)
2812     (gnus-summary-followup-with-original "mail/reply-all")
2813     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2814     (gnus-summary-mail-forward "mail/forward")
2815     (gnus-summary-save-article "mail/save")
2816     (gnus-summary-search-article-forward "search" nil :visible nil)
2817     (gnus-summary-print-article "print")
2818     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2819     ;; Some new commands that may need more suitable icons:
2820     (gnus-summary-save-newsrc "save" nil :visible nil)
2821     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2822     (gnus-summary-prev-article "left-arrow")
2823     (gnus-summary-next-article "right-arrow")
2824     (gnus-summary-next-page "next-page")
2825     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2826     ;;
2827     ;; Maybe some sort-by-... could be added:
2828     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2829     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2830     (gnus-summary-mark-as-expirable
2831      "delete" nil
2832      :visible (gnus-check-backend-function 'request-expire-articles
2833                                            gnus-newsgroup-name))
2834     (gnus-summary-mark-as-spam
2835      "mail/spam" t
2836      :visible (and (fboundp 'spam-group-ham-contents-p)
2837                    (spam-group-ham-contents-p gnus-newsgroup-name))
2838      :help "Mark as spam")
2839     (gnus-summary-mark-as-read-forward
2840      "mail/not-spam" nil
2841      :visible (and (fboundp 'spam-group-spam-contents-p)
2842                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2843     ;;
2844     (gnus-summary-exit "exit")
2845     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2846     (gnus-info-find-node "help"))
2847   "List of functions for the summary tool bar (GNOME style).
2848
2849 See `gmm-tool-bar-from-list' for the format of the list."
2850   :type '(repeat gmm-tool-bar-item)
2851   :version "23.1" ;; No Gnus
2852   :initialize 'custom-initialize-default
2853   :set 'gnus-summary-tool-bar-update
2854   :group 'gnus-summary)
2855
2856 (defcustom gnus-summary-tool-bar-retro
2857   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2858     (gnus-summary-next-unread-article "gnus/next-ur")
2859     (gnus-summary-post-news "gnus/post")
2860     (gnus-summary-followup-with-original "gnus/fuwo")
2861     (gnus-summary-followup "gnus/followup")
2862     (gnus-summary-reply-with-original "gnus/reply-wo")
2863     (gnus-summary-reply "gnus/reply")
2864     (gnus-summary-caesar-message "gnus/rot13")
2865     (gnus-uu-decode-uu "gnus/uu-decode")
2866     (gnus-summary-save-article-file "gnus/save-aif")
2867     (gnus-summary-save-article "gnus/save-art")
2868     (gnus-uu-post-news "gnus/uu-post")
2869     (gnus-summary-catchup "gnus/catchup")
2870     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2871     (gnus-summary-exit "gnus/exit-summ")
2872     ;; Some new command that may need more suitable icons:
2873     (gnus-summary-print-article "gnus/print" nil :visible nil)
2874     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2875     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2876     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2877     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2878     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2879     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2880     ;;
2881     (gnus-info-find-node "gnus/help" nil :visible nil))
2882   "List of functions for the summary tool bar (retro look).
2883
2884 See `gmm-tool-bar-from-list' for the format of the list."
2885   :type '(repeat gmm-tool-bar-item)
2886   :version "23.1" ;; No Gnus
2887   :initialize 'custom-initialize-default
2888   :set 'gnus-summary-tool-bar-update
2889   :group 'gnus-summary)
2890
2891 (defcustom gnus-summary-tool-bar-zap-list t
2892   "List of icon items from the global tool bar.
2893 These items are not displayed in the Gnus summary mode tool bar.
2894
2895 See `gmm-tool-bar-from-list' for the format of the list."
2896   :type 'gmm-tool-bar-zap-list
2897   :version "23.1" ;; No Gnus
2898   :initialize 'custom-initialize-default
2899   :set 'gnus-summary-tool-bar-update
2900   :group 'gnus-summary)
2901
2902 (defvar image-load-path)
2903 (defvar tool-bar-map)
2904
2905 (defun gnus-summary-make-tool-bar (&optional force)
2906   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2907 When FORCE, rebuild the tool bar."
2908   (when (and (not (featurep 'xemacs))
2909              (boundp 'tool-bar-mode)
2910              tool-bar-mode
2911              (or (not gnus-summary-tool-bar-map) force))
2912     (let* ((load-path
2913             (gmm-image-load-path-for-library "gnus"
2914                                              "mail/save.xpm"
2915                                              nil t))
2916            (image-load-path (cons (car load-path)
2917                                   (when (boundp 'image-load-path)
2918                                     image-load-path)))
2919            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2920                                         gnus-summary-tool-bar-zap-list
2921                                         'gnus-summary-mode-map)))
2922       (when map
2923         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2924         ;; uses it's value.
2925         (setq gnus-summary-tool-bar-map map))))
2926   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2927
2928 (defun gnus-score-set-default (var value)
2929   "A version of set that updates the GNU Emacs menu-bar."
2930   (set var value)
2931   ;; It is the message that forces the active status to be updated.
2932   (message ""))
2933
2934 (defun gnus-make-score-map (type)
2935   "Make a summary score map of type TYPE."
2936   (if t
2937       nil
2938     (let ((headers '(("author" "from" string)
2939                      ("subject" "subject" string)
2940                      ("article body" "body" string)
2941                      ("article head" "head" string)
2942                      ("xref" "xref" string)
2943                      ("extra header" "extra" string)
2944                      ("lines" "lines" number)
2945                      ("followups to author" "followup" string)))
2946           (types '((number ("less than" <)
2947                            ("greater than" >)
2948                            ("equal" =))
2949                    (string ("substring" s)
2950                            ("exact string" e)
2951                            ("fuzzy string" f)
2952                            ("regexp" r))))
2953           (perms '(("temporary" (current-time-string))
2954                    ("permanent" nil)
2955                    ("immediate" now)))
2956           header)
2957       (list
2958        (apply
2959         'nconc
2960         (list
2961          (if (eq type 'lower)
2962              "Lower score"
2963            "Increase score"))
2964         (let (outh)
2965           (while headers
2966             (setq header (car headers))
2967             (setq outh
2968                   (cons
2969                    (apply
2970                     'nconc
2971                     (list (car header))
2972                     (let ((ts (cdr (assoc (nth 2 header) types)))
2973                           outt)
2974                       (while ts
2975                         (setq outt
2976                               (cons
2977                                (apply
2978                                 'nconc
2979                                 (list (caar ts))
2980                                 (let ((ps perms)
2981                                       outp)
2982                                   (while ps
2983                                     (setq outp
2984                                           (cons
2985                                            (vector
2986                                             (caar ps)
2987                                             (list
2988                                              'gnus-summary-score-entry
2989                                              (nth 1 header)
2990                                              (if (or (string= (nth 1 header)
2991                                                               "head")
2992                                                      (string= (nth 1 header)
2993                                                               "body"))
2994                                                  ""
2995                                                (list 'gnus-summary-header
2996                                                      (nth 1 header)))
2997                                              (list 'quote (nth 1 (car ts)))
2998                                              (list 'gnus-score-delta-default
2999                                                    nil)
3000                                              (nth 1 (car ps))
3001                                              t)
3002                                             t)
3003                                            outp))
3004                                     (setq ps (cdr ps)))
3005                                   (list (nreverse outp))))
3006                                outt))
3007                         (setq ts (cdr ts)))
3008                       (list (nreverse outt))))
3009                    outh))
3010             (setq headers (cdr headers)))
3011           (list (nreverse outh))))))))
3012
3013
3014 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3015
3016 \f
3017
3018 (defun gnus-summary-mode (&optional group)
3019   "Major mode for reading articles.
3020
3021 All normal editing commands are switched off.
3022 \\<gnus-summary-mode-map>
3023 Each line in this buffer represents one article.  To read an
3024 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3025 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3026 respectively.
3027
3028 You can also post articles and send mail from this buffer.  To
3029 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3030 of an article, type `\\[gnus-summary-reply]'.
3031
3032 There are approx. one gazillion commands you can execute in this
3033 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3034
3035 The following commands are available:
3036
3037 \\{gnus-summary-mode-map}"
3038   (interactive)
3039   (kill-all-local-variables)
3040   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3041     (gnus-summary-make-local-variables))
3042   (gnus-summary-make-local-variables)
3043   (setq gnus-newsgroup-name group)
3044   (when (gnus-visual-p 'summary-menu 'menu)
3045     (gnus-summary-make-menu-bar)
3046     (gnus-summary-make-tool-bar))
3047   (gnus-make-thread-indent-array)
3048   (gnus-simplify-mode-line)
3049   (setq major-mode 'gnus-summary-mode)
3050   (setq mode-name "Summary")
3051   (make-local-variable 'minor-mode-alist)
3052   (use-local-map gnus-summary-mode-map)
3053   (buffer-disable-undo)
3054   (setq buffer-read-only t              ;Disable modification
3055         show-trailing-whitespace nil)
3056   (setq truncate-lines t)
3057   (setq selective-display t)
3058   (setq selective-display-ellipses t)   ;Display `...'
3059   (gnus-summary-set-display-table)
3060   (gnus-set-default-directory)
3061   (make-local-variable 'gnus-summary-line-format)
3062   (make-local-variable 'gnus-summary-line-format-spec)
3063   (make-local-variable 'gnus-summary-dummy-line-format)
3064   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3065   (make-local-variable 'gnus-summary-mark-positions)
3066   (gnus-make-local-hook 'pre-command-hook)
3067   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3068   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3069   (turn-on-gnus-mailing-list-mode)
3070   (mm-enable-multibyte)
3071   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3072   (gnus-update-summary-mark-positions))
3073
3074 (defun gnus-summary-make-local-variables ()
3075   "Make all the local summary buffer variables."
3076   (let (global)
3077     (dolist (local gnus-summary-local-variables)
3078       (if (consp local)
3079           (progn
3080             (if (eq (cdr local) 'global)
3081                 ;; Copy the global value of the variable.
3082                 (setq global (symbol-value (car local)))
3083               ;; Use the value from the list.
3084               (setq global (eval (cdr local))))
3085             (set (make-local-variable (car local)) global))
3086         ;; Simple nil-valued local variable.
3087         (set (make-local-variable local) nil)))))
3088
3089 (defun gnus-summary-clear-local-variables ()
3090   (let ((locals gnus-summary-local-variables))
3091     (while locals
3092       (if (consp (car locals))
3093           (and (symbolp (caar locals))
3094                (set (caar locals) nil))
3095         (and (symbolp (car locals))
3096              (set (car locals) nil)))
3097       (setq locals (cdr locals)))))
3098
3099 ;; Summary data functions.
3100
3101 (defmacro gnus-data-number (data)
3102   `(car ,data))
3103
3104 (defmacro gnus-data-set-number (data number)
3105   `(setcar ,data ,number))
3106
3107 (defmacro gnus-data-mark (data)
3108   `(nth 1 ,data))
3109
3110 (defmacro gnus-data-set-mark (data mark)
3111   `(setcar (nthcdr 1 ,data) ,mark))
3112
3113 (defmacro gnus-data-pos (data)
3114   `(nth 2 ,data))
3115
3116 (defmacro gnus-data-set-pos (data pos)
3117   `(setcar (nthcdr 2 ,data) ,pos))
3118
3119 (defmacro gnus-data-header (data)
3120   `(nth 3 ,data))
3121
3122 (defmacro gnus-data-set-header (data header)
3123   `(setf (nth 3 ,data) ,header))
3124
3125 (defmacro gnus-data-level (data)
3126   `(nth 4 ,data))
3127
3128 (defmacro gnus-data-unread-p (data)
3129   `(= (nth 1 ,data) gnus-unread-mark))
3130
3131 (defmacro gnus-data-read-p (data)
3132   `(/= (nth 1 ,data) gnus-unread-mark))
3133
3134 (defmacro gnus-data-pseudo-p (data)
3135   `(consp (nth 3 ,data)))
3136
3137 (defmacro gnus-data-find (number)
3138   `(assq ,number gnus-newsgroup-data))
3139
3140 (defmacro gnus-data-find-list (number &optional data)
3141   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3142      (memq (assq ,number bdata)
3143            bdata)))
3144
3145 (defmacro gnus-data-make (number mark pos header level)
3146   `(list ,number ,mark ,pos ,header ,level))
3147
3148 (defun gnus-data-enter (after-article number mark pos header level offset)
3149   (let ((data (gnus-data-find-list after-article)))
3150     (unless data
3151       (error "No such article: %d" after-article))
3152     (setcdr data (cons (gnus-data-make number mark pos header level)
3153                        (cdr data)))
3154     (setq gnus-newsgroup-data-reverse nil)
3155     (gnus-data-update-list (cddr data) offset)))
3156
3157 (defun gnus-data-enter-list (after-article list &optional offset)
3158   (when list
3159     (let ((data (and after-article (gnus-data-find-list after-article)))
3160           (ilist list))
3161       (if (not (or data
3162                    after-article))
3163           (let ((odata gnus-newsgroup-data))
3164             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3165             (when offset
3166               (gnus-data-update-list odata offset)))
3167         ;; Find the last element in the list to be spliced into the main
3168         ;; list.
3169         (setq list (last list))
3170         (if (not data)
3171             (progn
3172               (setcdr list gnus-newsgroup-data)
3173               (setq gnus-newsgroup-data ilist)
3174               (when offset
3175                 (gnus-data-update-list (cdr list) offset)))
3176           (setcdr list (cdr data))
3177           (setcdr data ilist)
3178           (when offset
3179             (gnus-data-update-list (cdr list) offset))))
3180       (setq gnus-newsgroup-data-reverse nil))))
3181
3182 (defun gnus-data-remove (article &optional offset)
3183   (let ((data gnus-newsgroup-data))
3184     (if (= (gnus-data-number (car data)) article)
3185         (progn
3186           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3187                 gnus-newsgroup-data-reverse nil)
3188           (when offset
3189             (gnus-data-update-list gnus-newsgroup-data offset)))
3190       (while (cdr data)
3191         (when (= (gnus-data-number (cadr data)) article)
3192           (setcdr data (cddr data))
3193           (when offset
3194             (gnus-data-update-list (cdr data) offset))
3195           (setq data nil
3196                 gnus-newsgroup-data-reverse nil))
3197         (setq data (cdr data))))))
3198
3199 (defmacro gnus-data-list (backward)
3200   `(if ,backward
3201        (or gnus-newsgroup-data-reverse
3202            (setq gnus-newsgroup-data-reverse
3203                  (reverse gnus-newsgroup-data)))
3204      gnus-newsgroup-data))
3205
3206 (defun gnus-data-update-list (data offset)
3207   "Add OFFSET to the POS of all data entries in DATA."
3208   (setq gnus-newsgroup-data-reverse nil)
3209   (while data
3210     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3211     (setq data (cdr data))))
3212
3213 (defun gnus-summary-article-pseudo-p (article)
3214   "Say whether this article is a pseudo article or not."
3215   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3216
3217 (defmacro gnus-summary-article-sparse-p (article)
3218   "Say whether this article is a sparse article or not."
3219   `(memq ,article gnus-newsgroup-sparse))
3220
3221 (defmacro gnus-summary-article-ancient-p (article)
3222   "Say whether this article is a sparse article or not."
3223   `(memq ,article gnus-newsgroup-ancient))
3224
3225 (defun gnus-article-parent-p (number)
3226   "Say whether this article is a parent or not."
3227   (let ((data (gnus-data-find-list number)))
3228     (and (cdr data)              ; There has to be an article after...
3229          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3230             (gnus-data-level (nth 1 data))))))
3231
3232 (defun gnus-article-children (number)
3233   "Return a list of all children to NUMBER."
3234   (let* ((data (gnus-data-find-list number))
3235          (level (gnus-data-level (car data)))
3236          children)
3237     (setq data (cdr data))
3238     (while (and data
3239                 (= (gnus-data-level (car data)) (1+ level)))
3240       (push (gnus-data-number (car data)) children)
3241       (setq data (cdr data)))
3242     children))
3243
3244 (defmacro gnus-summary-skip-intangible ()
3245   "If the current article is intangible, then jump to a different article."
3246   '(let ((to (get-text-property (point) 'gnus-intangible)))
3247      (and to (gnus-summary-goto-subject to))))
3248
3249 (defmacro gnus-summary-article-intangible-p ()
3250   "Say whether this article is intangible or not."
3251   '(get-text-property (point) 'gnus-intangible))
3252
3253 (defun gnus-article-read-p (article)
3254   "Say whether ARTICLE is read or not."
3255   (not (or (memq article gnus-newsgroup-marked)
3256            (memq article gnus-newsgroup-spam-marked)
3257            (memq article gnus-newsgroup-unreads)
3258            (memq article gnus-newsgroup-unselected)
3259            (memq article gnus-newsgroup-dormant))))
3260
3261 ;; Some summary mode macros.
3262
3263 (defmacro gnus-summary-article-number ()
3264   "The article number of the article on the current line.
3265 If there isn't an article number here, then we return the current
3266 article number."
3267   '(progn
3268      (gnus-summary-skip-intangible)
3269      (or (get-text-property (point) 'gnus-number)
3270          (gnus-summary-last-subject))))
3271
3272 (defmacro gnus-summary-article-header (&optional number)
3273   "Return the header of article NUMBER."
3274   `(gnus-data-header (gnus-data-find
3275                       ,(or number '(gnus-summary-article-number)))))
3276
3277 (defmacro gnus-summary-thread-level (&optional number)
3278   "Return the level of thread that starts with article NUMBER."
3279   `(if (and (eq gnus-summary-make-false-root 'dummy)
3280             (get-text-property (point) 'gnus-intangible))
3281        0
3282      (gnus-data-level (gnus-data-find
3283                        ,(or number '(gnus-summary-article-number))))))
3284
3285 (defmacro gnus-summary-article-mark (&optional number)
3286   "Return the mark of article NUMBER."
3287   `(gnus-data-mark (gnus-data-find
3288                     ,(or number '(gnus-summary-article-number)))))
3289
3290 (defmacro gnus-summary-article-pos (&optional number)
3291   "Return the position of the line of article NUMBER."
3292   `(gnus-data-pos (gnus-data-find
3293                    ,(or number '(gnus-summary-article-number)))))
3294
3295 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3296 (defmacro gnus-summary-article-subject (&optional number)
3297   "Return current subject string or nil if nothing."
3298   `(let ((headers
3299           ,(if number
3300                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3301              '(gnus-data-header (assq (gnus-summary-article-number)
3302                                       gnus-newsgroup-data)))))
3303      (and headers
3304           (vectorp headers)
3305           (mail-header-subject headers))))
3306
3307 (defmacro gnus-summary-article-score (&optional number)
3308   "Return current article score."
3309   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3310                   gnus-newsgroup-scored))
3311        gnus-summary-default-score 0))
3312
3313 (defun gnus-summary-article-children (&optional number)
3314   "Return a list of article numbers that are children of article NUMBER."
3315   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3316          (level (gnus-data-level (car data)))
3317          l children)
3318     (while (and (setq data (cdr data))
3319                 (> (setq l (gnus-data-level (car data))) level))
3320       (and (= (1+ level) l)
3321            (push (gnus-data-number (car data))
3322                  children)))
3323     (nreverse children)))
3324
3325 (defun gnus-summary-article-parent (&optional number)
3326   "Return the article number of the parent of article NUMBER."
3327   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3328                                     (gnus-data-list t)))
3329          (level (gnus-data-level (car data))))
3330     (if (zerop level)
3331         ()                              ; This is a root.
3332       ;; We search until we find an article with a level less than
3333       ;; this one.  That function has to be the parent.
3334       (while (and (setq data (cdr data))
3335                   (not (< (gnus-data-level (car data)) level))))
3336       (and data (gnus-data-number (car data))))))
3337
3338 (defun gnus-unread-mark-p (mark)
3339   "Say whether MARK is the unread mark."
3340   (= mark gnus-unread-mark))
3341
3342 (defun gnus-read-mark-p (mark)
3343   "Say whether MARK is one of the marks that mark as read.
3344 This is all marks except unread, ticked, dormant, and expirable."
3345   (not (or (= mark gnus-unread-mark)
3346            (= mark gnus-ticked-mark)
3347            (= mark gnus-spam-mark)
3348            (= mark gnus-dormant-mark)
3349            (= mark gnus-expirable-mark))))
3350
3351 (defmacro gnus-article-mark (number)
3352   "Return the MARK of article NUMBER.
3353 This macro should only be used when computing the mark the \"first\"
3354 time; i.e., when generating the summary lines.  After that,
3355 `gnus-summary-article-mark' should be used to examine the
3356 marks of articles."
3357   `(cond
3358     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3359     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3360     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3361     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3362     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3363     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3364     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3365     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3366            gnus-ancient-mark))))
3367
3368 ;; Saving hidden threads.
3369
3370 (defmacro gnus-save-hidden-threads (&rest forms)
3371   "Save hidden threads, eval FORMS, and restore the hidden threads."
3372   (let ((config (make-symbol "config")))
3373     `(let ((,config (gnus-hidden-threads-configuration)))
3374        (unwind-protect
3375            (save-excursion
3376              ,@forms)
3377          (gnus-restore-hidden-threads-configuration ,config)))))
3378 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3379 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3380
3381 (defun gnus-data-compute-positions ()
3382   "Compute the positions of all articles."
3383   (setq gnus-newsgroup-data-reverse nil)
3384   (let ((data gnus-newsgroup-data))
3385     (save-excursion
3386       (gnus-save-hidden-threads
3387         (gnus-summary-show-all-threads)
3388         (goto-char (point-min))
3389         (while data
3390           (while (get-text-property (point) 'gnus-intangible)
3391             (forward-line 1))
3392           (gnus-data-set-pos (car data) (+ (point) 3))
3393           (setq data (cdr data))
3394           (forward-line 1))))))
3395
3396 (defun gnus-hidden-threads-configuration ()
3397   "Return the current hidden threads configuration."
3398   (save-excursion
3399     (let (config)
3400       (goto-char (point-min))
3401       (while (search-forward "\r" nil t)
3402         (push (1- (point)) config))
3403       config)))
3404
3405 (defun gnus-restore-hidden-threads-configuration (config)
3406   "Restore hidden threads configuration from CONFIG."
3407   (save-excursion
3408     (let (point (inhibit-read-only t))
3409       (while (setq point (pop config))
3410         (when (and (< point (point-max))
3411                    (goto-char point)
3412                    (eq (char-after) ?\n))
3413           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3414
3415 ;; Various summary mode internalish functions.
3416
3417 (defun gnus-mouse-pick-article (e)
3418   (interactive "e")
3419   (mouse-set-point e)
3420   (gnus-summary-next-page nil t))
3421
3422 (defun gnus-summary-set-display-table ()
3423   "Change the display table.
3424 Odd characters have a tendency to mess
3425 up nicely formatted displays - we make all possible glyphs
3426 display only a single character."
3427
3428   ;; We start from the standard display table, if any.
3429   (let ((table (or (copy-sequence standard-display-table)
3430                    (make-display-table)))
3431         (i 32))
3432     ;; Nix out all the control chars...
3433     (while (>= (setq i (1- i)) 0)
3434       (gnus-put-display-table i [??] table))
3435    ;; ... but not newline and cr, of course.  (cr is necessary for the
3436     ;; selective display).
3437     (gnus-put-display-table ?\n nil table)
3438     (gnus-put-display-table ?\r nil table)
3439     ;; We keep TAB as well.
3440     (gnus-put-display-table ?\t nil table)
3441     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3442     ;; Emacs 23 (unicode), that are not set already.
3443     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3444                  160
3445                256)))
3446       (while (>= (setq i (1- i)) 127)
3447         ;; Only modify if the entry is nil.
3448         (unless (gnus-get-display-table i table)
3449           (gnus-put-display-table i [??] table))))
3450     (setq buffer-display-table table)))
3451
3452 (defun gnus-summary-set-article-display-arrow (pos)
3453   "Update the overlay arrow to point to line at position POS."
3454   (when (and gnus-summary-display-arrow
3455              (boundp 'overlay-arrow-position)
3456              (boundp 'overlay-arrow-string))
3457     (save-excursion
3458       (goto-char pos)
3459       (beginning-of-line)
3460       (unless overlay-arrow-position
3461         (setq overlay-arrow-position (make-marker)))
3462       (setq overlay-arrow-string "=>"
3463             overlay-arrow-position (set-marker overlay-arrow-position
3464                                                (point)
3465                                                (current-buffer))))))
3466
3467 (defun gnus-summary-setup-buffer (group)
3468   "Initialize summary buffer."
3469   (let ((buffer (gnus-summary-buffer-name group))
3470         (dead-name (concat "*Dead Summary "
3471                            (gnus-group-decoded-name group) "*")))
3472     ;; If a dead summary buffer exists, we kill it.
3473     (when (gnus-buffer-live-p dead-name)
3474       (gnus-kill-buffer dead-name))
3475     (if (get-buffer buffer)
3476         (progn
3477           (set-buffer buffer)
3478           (setq gnus-summary-buffer (current-buffer))
3479           (not gnus-newsgroup-prepared))
3480       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3481       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3482       (gnus-summary-mode group)
3483       (when gnus-carpal
3484         (gnus-carpal-setup-buffer 'summary))
3485       (when (gnus-group-quit-config group)
3486         (set (make-local-variable 'gnus-single-article-buffer) nil))
3487       (make-local-variable 'gnus-article-buffer)
3488       (make-local-variable 'gnus-article-current)
3489       (make-local-variable 'gnus-original-article-buffer)
3490       (setq gnus-newsgroup-name group)
3491       ;; Set any local variables in the group parameters.
3492       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3493       t)))
3494
3495 (defun gnus-set-global-variables ()
3496   "Set the global equivalents of the buffer-local variables.
3497 They are set to the latest values they had.  These reflect the summary
3498 buffer that was in action when the last article was fetched."
3499   (when (eq major-mode 'gnus-summary-mode)
3500     (setq gnus-summary-buffer (current-buffer))
3501     (let ((name gnus-newsgroup-name)
3502           (marked gnus-newsgroup-marked)
3503           (spam gnus-newsgroup-spam-marked)
3504           (unread gnus-newsgroup-unreads)
3505           (headers gnus-current-headers)
3506           (data gnus-newsgroup-data)
3507           (summary gnus-summary-buffer)
3508           (article-buffer gnus-article-buffer)
3509           (original gnus-original-article-buffer)
3510           (gac gnus-article-current)
3511           (reffed gnus-reffed-article-number)
3512           (score-file gnus-current-score-file)
3513           (default-charset gnus-newsgroup-charset)
3514           vlist)
3515       (let ((locals gnus-newsgroup-variables))
3516         (while locals
3517           (if (consp (car locals))
3518               (push (eval (caar locals)) vlist)
3519             (push (eval (car locals)) vlist))
3520           (setq locals (cdr locals)))
3521         (setq vlist (nreverse vlist)))
3522       (with-current-buffer gnus-group-buffer
3523         (setq gnus-newsgroup-name name
3524               gnus-newsgroup-marked marked
3525               gnus-newsgroup-spam-marked spam
3526               gnus-newsgroup-unreads unread
3527               gnus-current-headers headers
3528               gnus-newsgroup-data data
3529               gnus-article-current gac
3530               gnus-summary-buffer summary
3531               gnus-article-buffer article-buffer
3532               gnus-original-article-buffer original
3533               gnus-reffed-article-number reffed
3534               gnus-current-score-file score-file
3535               gnus-newsgroup-charset default-charset)
3536         (let ((locals gnus-newsgroup-variables))
3537           (while locals
3538             (if (consp (car locals))
3539                 (set (caar locals) (pop vlist))
3540               (set (car locals) (pop vlist)))
3541             (setq locals (cdr locals))))
3542         ;; The article buffer also has local variables.
3543         (when (gnus-buffer-live-p gnus-article-buffer)
3544           (set-buffer gnus-article-buffer)
3545           (setq gnus-summary-buffer summary))))))
3546
3547 (defun gnus-summary-article-unread-p (article)
3548   "Say whether ARTICLE is unread or not."
3549   (memq article gnus-newsgroup-unreads))
3550
3551 (defun gnus-summary-first-article-p (&optional article)
3552   "Return whether ARTICLE is the first article in the buffer."
3553   (if (not (setq article (or article (gnus-summary-article-number))))
3554       nil
3555     (eq article (caar gnus-newsgroup-data))))
3556
3557 (defun gnus-summary-last-article-p (&optional article)
3558   "Return whether ARTICLE is the last article in the buffer."
3559   (if (not (setq article (or article (gnus-summary-article-number))))
3560       ;; All non-existent numbers are the last article.  :-)
3561       t
3562     (not (cdr (gnus-data-find-list article)))))
3563
3564 (defun gnus-make-thread-indent-array (&optional n)
3565   (when (or n
3566             (progn (setq n 200) nil)
3567             (null gnus-thread-indent-array)
3568             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3569     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3570           gnus-thread-indent-array-level gnus-thread-indent-level)
3571     (while (>= n 0)
3572       (aset gnus-thread-indent-array n
3573             (make-string (* n gnus-thread-indent-level) ? ))
3574       (setq n (1- n)))))
3575
3576 (defun gnus-update-summary-mark-positions ()
3577   "Compute where the summary marks are to go."
3578   (save-excursion
3579     (when (gnus-buffer-exists-p gnus-summary-buffer)
3580       (set-buffer gnus-summary-buffer))
3581     (let ((spec gnus-summary-line-format-spec)
3582           pos)
3583       (save-excursion
3584         (gnus-set-work-buffer)
3585         (let ((gnus-tmp-unread ?Z)
3586               (gnus-replied-mark ?Z)
3587               (gnus-score-below-mark ?Z)
3588               (gnus-score-over-mark ?Z)
3589               (gnus-undownloaded-mark ?Z)
3590               (gnus-summary-line-format-spec spec)
3591               (gnus-newsgroup-downloadable '(0))
3592               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3593               case-fold-search ignores)
3594           ;; Here, all marks are bound to Z.
3595           (gnus-summary-insert-line header
3596                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3597           (goto-char (point-min))
3598           ;; Memorize the positions of the same characters as dummy marks.
3599           (while (re-search-forward "[A-D]" nil t)
3600             (push (point) ignores))
3601           (erase-buffer)
3602           ;; We use A-D as dummy marks in order to know column positions
3603           ;; where marks should be inserted.
3604           (setq gnus-tmp-unread ?A
3605                 gnus-replied-mark ?B
3606                 gnus-score-below-mark ?C
3607                 gnus-score-over-mark ?C
3608                 gnus-undownloaded-mark ?D)
3609           (gnus-summary-insert-line header
3610                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3611           ;; Ignore characters which aren't dummy marks.
3612           (dolist (p ignores)
3613             (delete-region (goto-char (1- p)) p)
3614             (insert ?Z))
3615           (goto-char (point-min))
3616           (setq pos (list (cons 'unread
3617                                 (and (search-forward "A" nil t)
3618                                      (- (point) (point-min) 1)))))
3619           (goto-char (point-min))
3620           (push (cons 'replied (and (search-forward "B" nil t)
3621                                     (- (point) (point-min) 1)))
3622                 pos)
3623           (goto-char (point-min))
3624           (push (cons 'score (and (search-forward "C" nil t)
3625                                   (- (point) (point-min) 1)))
3626                 pos)
3627           (goto-char (point-min))
3628           (push (cons 'download (and (search-forward "D" nil t)
3629                                      (- (point) (point-min) 1)))
3630                 pos)))
3631       (setq gnus-summary-mark-positions pos))))
3632
3633 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3634   "Insert a dummy root in the summary buffer."
3635   (beginning-of-line)
3636   (gnus-add-text-properties
3637    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3638    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3639
3640 (defun gnus-summary-extract-address-component (from)
3641   (or (car (funcall gnus-extract-address-components from))
3642       from))
3643
3644 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3645   (let ((mail-parse-charset gnus-newsgroup-charset)
3646         (ignored-from-addresses (gnus-ignored-from-addresses))
3647         ; Is it really necessary to do this next part for each summary line?
3648         ; Luckily, doesn't seem to slow things down much.
3649         (mail-parse-ignored-charsets
3650          (with-current-buffer gnus-summary-buffer
3651            gnus-newsgroup-ignored-charsets)))
3652     (or
3653      (and ignored-from-addresses
3654           (string-match ignored-from-addresses gnus-tmp-from)
3655           (let ((extra-headers (mail-header-extra header))
3656                 to
3657                 newsgroups)
3658             (cond
3659              ((setq to (cdr (assq 'To extra-headers)))
3660               (concat gnus-summary-to-prefix
3661                       (inline
3662                         (gnus-summary-extract-address-component
3663                          (funcall gnus-decode-encoded-address-function to)))))
3664              ((setq newsgroups
3665                     (or
3666                      (cdr (assq 'Newsgroups extra-headers))
3667                      (and
3668                       (memq 'Newsgroups gnus-extra-headers)
3669                       (eq (car (gnus-find-method-for-group
3670                                 gnus-newsgroup-name)) 'nntp)
3671                       (gnus-group-real-name gnus-newsgroup-name))))
3672               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3673      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3674
3675 (defun gnus-summary-insert-line (gnus-tmp-header
3676                                  gnus-tmp-level gnus-tmp-current
3677                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3678                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3679                                  &optional gnus-tmp-dummy gnus-tmp-score
3680                                  gnus-tmp-process)
3681   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3682       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3683                                           gnus-tmp-level)))
3684   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3685          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3686          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3687          (gnus-tmp-score-char
3688           (if (or (null gnus-summary-default-score)
3689                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3690                       gnus-summary-zcore-fuzz))
3691               ?                         ;Whitespace
3692             (if (< gnus-tmp-score gnus-summary-default-score)
3693                 gnus-score-below-mark gnus-score-over-mark)))
3694          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3695          (gnus-tmp-replied
3696           (cond (gnus-tmp-process gnus-process-mark)
3697                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3698                  gnus-cached-mark)
3699                 (gnus-tmp-replied gnus-replied-mark)
3700                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3701                  gnus-forwarded-mark)
3702                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3703                  gnus-saved-mark)
3704                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3705                  gnus-recent-mark)
3706                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3707                  gnus-unseen-mark)
3708                 (t gnus-no-mark)))
3709          (gnus-tmp-downloaded
3710           (cond (undownloaded
3711                  gnus-undownloaded-mark)
3712                 (gnus-newsgroup-agentized
3713                  gnus-downloaded-mark)
3714                 (t
3715                  gnus-no-mark)))
3716          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3717          (gnus-tmp-name
3718           (cond
3719            ((string-match "<[^>]+> *$" gnus-tmp-from)
3720             (let ((beg (match-beginning 0)))
3721               (or (and (string-match "^\".+\"" gnus-tmp-from)
3722                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3723                   (substring gnus-tmp-from 0 beg))))
3724            ((string-match "(.+)" gnus-tmp-from)
3725             (substring gnus-tmp-from
3726                        (1+ (match-beginning 0)) (1- (match-end 0))))
3727            (t gnus-tmp-from)))
3728          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3729          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3730          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3731          (inhibit-read-only t))
3732     (when (string= gnus-tmp-name "")
3733       (setq gnus-tmp-name gnus-tmp-from))
3734     (unless (numberp gnus-tmp-lines)
3735       (setq gnus-tmp-lines -1))
3736     (if (= gnus-tmp-lines -1)
3737         (setq gnus-tmp-lines "?")
3738       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3739       (gnus-put-text-property
3740      (point)
3741      (progn (eval gnus-summary-line-format-spec) (point))
3742        'gnus-number gnus-tmp-number)
3743     (when (gnus-visual-p 'summary-highlight 'highlight)
3744       (forward-line -1)
3745       (gnus-run-hooks 'gnus-summary-update-hook)
3746       (forward-line 1))))
3747
3748 (defun gnus-summary-update-line (&optional dont-update)
3749   "Update summary line after change."
3750   (when (and gnus-summary-default-score
3751              (not gnus-summary-inhibit-highlight))
3752     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3753            (article (gnus-summary-article-number))
3754            (score (gnus-summary-article-score article)))
3755       (unless dont-update
3756         (if (and gnus-summary-mark-below
3757                  (< (gnus-summary-article-score)
3758                     gnus-summary-mark-below))
3759             ;; This article has a low score, so we mark it as read.
3760             (when (memq article gnus-newsgroup-unreads)
3761               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3762           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3763             ;; This article was previously marked as read on account
3764             ;; of a low score, but now it has risen, so we mark it as
3765             ;; unread.
3766             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3767         (gnus-summary-update-mark
3768          (if (or (null gnus-summary-default-score)
3769                  (<= (abs (- score gnus-summary-default-score))
3770                      gnus-summary-zcore-fuzz))
3771              ?                          ;Whitespace
3772            (if (< score gnus-summary-default-score)
3773                gnus-score-below-mark gnus-score-over-mark))
3774          'score))
3775       ;; Do visual highlighting.
3776       (when (gnus-visual-p 'summary-highlight 'highlight)
3777         (gnus-run-hooks 'gnus-summary-update-hook)))))
3778
3779 (defvar gnus-tmp-new-adopts nil)
3780
3781 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3782   "Return the number of articles in THREAD.
3783 This may be 0 in some cases -- if none of the articles in
3784 the thread are to be displayed."
3785   (let* ((number
3786          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3787           (cond
3788            ((not (listp thread))
3789             1)
3790            ((and (consp thread) (cdr thread))
3791             (apply
3792              '+ 1 (mapcar
3793                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3794            ((null thread)
3795             1)
3796            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3797             1)
3798            (t 0))))
3799     (when (and level (zerop level) gnus-tmp-new-adopts)
3800       (incf number
3801             (apply '+ (mapcar
3802                        'gnus-summary-number-of-articles-in-thread
3803                        gnus-tmp-new-adopts))))
3804     (if char
3805         (if (> number 1) gnus-not-empty-thread-mark
3806           gnus-empty-thread-mark)
3807       number)))
3808
3809 (defsubst gnus-summary-line-message-size (head)
3810   "Return pretty-printed version of message size.
3811 This function is intended to be used in
3812 `gnus-summary-line-format-alist'."
3813   (let ((c (or (mail-header-chars head) -1)))
3814     (cond ((< c 0) "n/a")               ; chars not available
3815           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3816           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3817           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3818           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3819
3820
3821 (defun gnus-summary-set-local-parameters (group)
3822   "Go through the local params of GROUP and set all variable specs in that list."
3823   (let ((vars '(quit-config)))          ; Ignore quit-config.
3824     (dolist (elem (gnus-group-find-parameter group))
3825       (and (consp elem)                 ; Has to be a cons.
3826            (consp (cdr elem))           ; The cdr has to be a list.
3827            (symbolp (car elem))         ; Has to be a symbol in there.
3828            (not (memq (car elem) vars))
3829            (ignore-errors               ; So we set it.
3830              (push (car elem) vars)
3831              (make-local-variable (car elem))
3832              (set (car elem) (eval (nth 1 elem))))))))
3833
3834 (defun gnus-summary-read-group (group &optional show-all no-article
3835                                       kill-buffer no-display backward
3836                                       select-articles)
3837   "Start reading news in newsgroup GROUP.
3838 If SHOW-ALL is non-nil, already read articles are also listed.
3839 If NO-ARTICLE is non-nil, no article is selected initially.
3840 If NO-DISPLAY, don't generate a summary buffer."
3841   (let (result)
3842     (while (and group
3843                 (null (setq result
3844                             (let ((gnus-auto-select-next nil))
3845                               (or (gnus-summary-read-group-1
3846                                    group show-all no-article
3847                                    kill-buffer no-display
3848                                    select-articles)
3849                                   (setq show-all nil
3850                                         select-articles nil)))))
3851                 (eq gnus-auto-select-next 'quietly))
3852       (set-buffer gnus-group-buffer)
3853       ;; The entry function called above goes to the next
3854       ;; group automatically, so we go two groups back
3855       ;; if we are searching for the previous group.
3856       (when backward
3857         (gnus-group-prev-unread-group 2))
3858       (if (not (equal group (gnus-group-group-name)))
3859           (setq group (gnus-group-group-name))
3860         (setq group nil)))
3861     result))
3862
3863 (defun gnus-summary-read-group-1 (group show-all no-article
3864                                         kill-buffer no-display
3865                                         &optional select-articles)
3866   ;; Killed foreign groups can't be entered.
3867   ;;  (when (and (not (gnus-group-native-p group))
3868   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3869   ;;    (error "Dead non-native groups can't be entered"))
3870   (gnus-message 5 "Retrieving newsgroup: %s..."
3871                 (gnus-group-decoded-name group))
3872   (let* ((new-group (gnus-summary-setup-buffer group))
3873          (quit-config (gnus-group-quit-config group))
3874          (did-select (and new-group (gnus-select-newsgroup
3875                                      group show-all select-articles))))
3876     (cond
3877      ;; This summary buffer exists already, so we just select it.
3878      ((not new-group)
3879       (gnus-set-global-variables)
3880       (when kill-buffer
3881         (gnus-kill-or-deaden-summary kill-buffer))
3882       (gnus-configure-windows 'summary 'force)
3883       (gnus-set-mode-line 'summary)
3884       (gnus-summary-position-point)
3885       (message "")
3886       t)
3887      ;; We couldn't select this group.
3888      ((null did-select)
3889       (when (and (eq major-mode 'gnus-summary-mode)
3890                  (not (equal (current-buffer) kill-buffer)))
3891         (kill-buffer (current-buffer))
3892         (if (not quit-config)
3893             (progn
3894               ;; Update the info -- marks might need to be removed,
3895               ;; for instance.
3896               (gnus-summary-update-info)
3897               (set-buffer gnus-group-buffer)
3898               (gnus-group-jump-to-group group)
3899               (gnus-group-next-unread-group 1))
3900           (gnus-handle-ephemeral-exit quit-config)))
3901       (let ((grpinfo (gnus-get-info group)))
3902         (if (null (gnus-info-read grpinfo))
3903             (gnus-message 3 "Group %s contains no messages"
3904                           (gnus-group-decoded-name group))
3905           (gnus-message 3 "Can't select group")))
3906       nil)
3907      ;; The user did a `C-g' while prompting for number of articles,
3908      ;; so we exit this group.
3909      ((eq did-select 'quit)
3910       (and (eq major-mode 'gnus-summary-mode)
3911            (not (equal (current-buffer) kill-buffer))
3912            (kill-buffer (current-buffer)))
3913       (when kill-buffer
3914         (gnus-kill-or-deaden-summary kill-buffer))
3915       (if (not quit-config)
3916           (progn
3917             (set-buffer gnus-group-buffer)
3918             (gnus-group-jump-to-group group)
3919             (gnus-group-next-unread-group 1)
3920             (gnus-configure-windows 'group 'force))
3921         (gnus-handle-ephemeral-exit quit-config))
3922       ;; Finally signal the quit.
3923       (signal 'quit nil))
3924      ;; The group was successfully selected.
3925      (t
3926       (gnus-set-global-variables)
3927       ;; Save the active value in effect when the group was entered.
3928       (setq gnus-newsgroup-active
3929             (gnus-copy-sequence
3930              (gnus-active gnus-newsgroup-name)))
3931       ;; You can change the summary buffer in some way with this hook.
3932       (gnus-run-hooks 'gnus-select-group-hook)
3933       (when (memq 'summary (gnus-update-format-specifications
3934                             nil 'summary 'summary-mode 'summary-dummy))
3935         ;; The format specification for the summary line was updated,
3936         ;; so we need to update the mark positions as well.
3937         (gnus-update-summary-mark-positions))
3938       ;; Do score processing.
3939       (when gnus-use-scoring
3940         (gnus-possibly-score-headers))
3941       ;; Check whether to fill in the gaps in the threads.
3942       (when gnus-build-sparse-threads
3943         (gnus-build-sparse-threads))
3944       ;; Find the initial limit.
3945       (if show-all
3946           (let ((gnus-newsgroup-dormant nil))
3947             (gnus-summary-initial-limit show-all))
3948         (gnus-summary-initial-limit show-all))
3949       ;; Generate the summary buffer.
3950       (unless no-display
3951         (gnus-summary-prepare))
3952       (when gnus-use-trees
3953         (gnus-tree-open group)
3954         (setq gnus-summary-highlight-line-function
3955               'gnus-tree-highlight-article))
3956       ;; If the summary buffer is empty, but there are some low-scored
3957       ;; articles or some excluded dormants, we include these in the
3958       ;; buffer.
3959       (when (and (zerop (buffer-size))
3960                  (not no-display))
3961         (cond (gnus-newsgroup-dormant
3962                (gnus-summary-limit-include-dormant))
3963               ((and gnus-newsgroup-scored show-all)
3964                (gnus-summary-limit-include-expunged t))))
3965       ;; Function `gnus-apply-kill-file' must be called in this hook.
3966       (gnus-run-hooks 'gnus-apply-kill-hook)
3967       (if (and (zerop (buffer-size))
3968                (not no-display))
3969           (progn
3970             ;; This newsgroup is empty.
3971             (gnus-summary-catchup-and-exit nil t)
3972             (gnus-message 6 "No unread news")
3973             (when kill-buffer
3974               (gnus-kill-or-deaden-summary kill-buffer))
3975             ;; Return nil from this function.
3976             nil)
3977         ;; Hide conversation thread subtrees.  We cannot do this in
3978         ;; gnus-summary-prepare-hook since kill processing may not
3979         ;; work with hidden articles.
3980         (gnus-summary-maybe-hide-threads)
3981         (when kill-buffer
3982           (gnus-kill-or-deaden-summary kill-buffer))
3983         (gnus-summary-auto-select-subject)
3984         ;; Show first unread article if requested.
3985         (if (and (not no-article)
3986                  (not no-display)
3987                  gnus-newsgroup-unreads
3988                  gnus-auto-select-first)
3989             (progn
3990               (gnus-configure-windows 'summary)
3991               (let ((art (gnus-summary-article-number)))
3992                 (unless (and (not gnus-plugged)
3993                              (or (memq art gnus-newsgroup-undownloaded)
3994                                  (memq art gnus-newsgroup-downloadable)))
3995                   (gnus-summary-goto-article art))))
3996           ;; Don't select any articles.
3997           (gnus-summary-position-point)
3998           (gnus-configure-windows 'summary 'force)
3999           (gnus-set-mode-line 'summary))
4000         (when (and gnus-auto-center-group
4001                    (get-buffer-window gnus-group-buffer t))
4002           ;; Gotta use windows, because recenter does weird stuff if
4003           ;; the current buffer ain't the displayed window.
4004           (let ((owin (selected-window)))
4005             (select-window (get-buffer-window gnus-group-buffer t))
4006             (when (gnus-group-goto-group group)
4007               (recenter))
4008             (select-window owin)))
4009         ;; Mark this buffer as "prepared".
4010         (setq gnus-newsgroup-prepared t)
4011         (gnus-run-hooks 'gnus-summary-prepared-hook)
4012         (unless (gnus-ephemeral-group-p group)
4013           (gnus-group-update-group group))
4014         t)))))
4015
4016 (defun gnus-summary-auto-select-subject ()
4017   "Select the subject line on initial group entry."
4018   (goto-char (point-min))
4019   (cond
4020    ((eq gnus-auto-select-subject 'best)
4021     (gnus-summary-best-unread-subject))
4022    ((eq gnus-auto-select-subject 'unread)
4023     (gnus-summary-first-unread-subject))
4024    ((eq gnus-auto-select-subject 'unseen)
4025     (gnus-summary-first-unseen-subject))
4026    ((eq gnus-auto-select-subject 'unseen-or-unread)
4027     (gnus-summary-first-unseen-or-unread-subject))
4028    ((eq gnus-auto-select-subject 'first)
4029     ;; Do nothing.
4030     )
4031    ((functionp gnus-auto-select-subject)
4032     (funcall gnus-auto-select-subject))))
4033
4034 (defun gnus-summary-prepare ()
4035   "Generate the summary buffer."
4036   (interactive)
4037   (let ((inhibit-read-only t))
4038     (erase-buffer)
4039     (setq gnus-newsgroup-data nil
4040           gnus-newsgroup-data-reverse nil)
4041     (gnus-run-hooks 'gnus-summary-generate-hook)
4042     ;; Generate the buffer, either with threads or without.
4043     (when gnus-newsgroup-headers
4044       (gnus-summary-prepare-threads
4045        (if gnus-show-threads
4046            (gnus-sort-gathered-threads
4047             (funcall gnus-summary-thread-gathering-function
4048                      (gnus-sort-threads
4049                       (gnus-cut-threads (gnus-make-threads)))))
4050          ;; Unthreaded display.
4051          (gnus-sort-articles gnus-newsgroup-headers))))
4052     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4053     ;; Call hooks for modifying summary buffer.
4054     (goto-char (point-min))
4055     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4056
4057 (defsubst gnus-general-simplify-subject (subject)
4058   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4059   (setq subject
4060         (cond
4061          ;; Truncate the subject.
4062          (gnus-simplify-subject-functions
4063           (gnus-map-function gnus-simplify-subject-functions subject))
4064          ((numberp gnus-summary-gather-subject-limit)
4065           (setq subject (gnus-simplify-subject-re subject))
4066           (if (> (length subject) gnus-summary-gather-subject-limit)
4067               (substring subject 0 gnus-summary-gather-subject-limit)
4068             subject))
4069          ;; Fuzzily simplify it.
4070          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4071           (gnus-simplify-subject-fuzzy subject))
4072          ;; Just remove the leading "Re:".
4073          (t
4074           (gnus-simplify-subject-re subject))))
4075
4076   (if (and gnus-summary-gather-exclude-subject
4077            (string-match gnus-summary-gather-exclude-subject subject))
4078       nil                         ; This article shouldn't be gathered
4079     subject))
4080
4081 (defun gnus-summary-simplify-subject-query ()
4082   "Query where the respool algorithm would put this article."
4083   (interactive)
4084   (gnus-summary-select-article)
4085   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4086
4087 (defun gnus-gather-threads-by-subject (threads)
4088   "Gather threads by looking at Subject headers."
4089   (if (not gnus-summary-make-false-root)
4090       threads
4091     (let ((hashtb (gnus-make-hashtable 1024))
4092           (prev threads)
4093           (result threads)
4094           subject hthread whole-subject)
4095       (while threads
4096         (setq subject (gnus-general-simplify-subject
4097                        (setq whole-subject (mail-header-subject
4098                                             (caar threads)))))
4099         (when subject
4100           (if (setq hthread (gnus-gethash subject hashtb))
4101               (progn
4102                 ;; We enter a dummy root into the thread, if we
4103                 ;; haven't done that already.
4104                 (unless (stringp (caar hthread))
4105                   (setcar hthread (list whole-subject (car hthread))))
4106                 ;; We add this new gathered thread to this gathered
4107                 ;; thread.
4108                 (setcdr (car hthread)
4109                         (nconc (cdar hthread) (list (car threads))))
4110                 ;; Remove it from the list of threads.
4111                 (setcdr prev (cdr threads))
4112                 (setq threads prev))
4113             ;; Enter this thread into the hash table.
4114             (gnus-sethash subject
4115                           (if gnus-summary-make-false-root-always
4116                               (progn
4117                                 ;; If you want a dummy root above all
4118                                 ;; threads...
4119                                 (setcar threads (list whole-subject
4120                                                       (car threads)))
4121                                 threads)
4122                             threads)
4123                           hashtb)))
4124         (setq prev threads)
4125         (setq threads (cdr threads)))
4126       result)))
4127
4128 (defun gnus-gather-threads-by-references (threads)
4129   "Gather threads by looking at References headers."
4130   (let ((idhashtb (gnus-make-hashtable 1024))
4131         (thhashtb (gnus-make-hashtable 1024))
4132         (prev threads)
4133         (result threads)
4134         ids references id gthread gid entered ref)
4135     (while threads
4136       (when (setq references (mail-header-references (caar threads)))
4137         (setq id (mail-header-id (caar threads))
4138               ids (inline (gnus-split-references references))
4139               entered nil)
4140         (while (setq ref (pop ids))
4141           (setq ids (delete ref ids))
4142           (if (not (setq gid (gnus-gethash ref idhashtb)))
4143               (progn
4144                 (gnus-sethash ref id idhashtb)
4145                 (gnus-sethash id threads thhashtb))
4146             (setq gthread (gnus-gethash gid thhashtb))
4147             (unless entered
4148               ;; We enter a dummy root into the thread, if we
4149               ;; haven't done that already.
4150               (unless (stringp (caar gthread))
4151                 (setcar gthread (list (mail-header-subject (caar gthread))
4152                                       (car gthread))))
4153               ;; We add this new gathered thread to this gathered
4154               ;; thread.
4155               (setcdr (car gthread)
4156                       (nconc (cdar gthread) (list (car threads)))))
4157             ;; Add it into the thread hash table.
4158             (gnus-sethash id gthread thhashtb)
4159             (setq entered t)
4160             ;; Remove it from the list of threads.
4161             (setcdr prev (cdr threads))
4162             (setq threads prev))))
4163       (setq prev threads)
4164       (setq threads (cdr threads)))
4165     result))
4166
4167 (defun gnus-sort-gathered-threads (threads)
4168   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4169   (let ((result threads))
4170     (while threads
4171       (when (stringp (caar threads))
4172         (setcdr (car threads)
4173                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4174       (setq threads (cdr threads)))
4175     result))
4176
4177 (defun gnus-thread-loop-p (root thread)
4178   "Say whether ROOT is in THREAD."
4179   (let ((stack (list thread))
4180         (infloop 0)
4181         th)
4182     (while (setq thread (pop stack))
4183       (setq th (cdr thread))
4184       (while (and th
4185                   (not (eq (caar th) root)))
4186         (pop th))
4187       (if th
4188           ;; We have found a loop.
4189           (let (ref-dep)
4190             (setcdr thread (delq (car th) (cdr thread)))
4191             (if (boundp (setq ref-dep (intern "none"
4192                                               gnus-newsgroup-dependencies)))
4193                 (setcdr (symbol-value ref-dep)
4194                         (nconc (cdr (symbol-value ref-dep))
4195                                (list (car th))))
4196               (set ref-dep (list nil (car th))))
4197             (setq infloop 1
4198                   stack nil))
4199         ;; Push all the subthreads onto the stack.
4200         (push (cdr thread) stack)))
4201     infloop))
4202
4203 (defun gnus-make-threads ()
4204   "Go through the dependency hashtb and find the roots.  Return all threads."
4205   (let (threads)
4206     (while (catch 'infloop
4207              (mapatoms
4208               (lambda (refs)
4209                 ;; Deal with self-referencing References loops.
4210                 (when (and (car (symbol-value refs))
4211                            (not (zerop
4212                                  (apply
4213                                   '+
4214                                   (mapcar
4215                                    (lambda (thread)
4216                                      (gnus-thread-loop-p
4217                                       (car (symbol-value refs)) thread))
4218                                    (cdr (symbol-value refs)))))))
4219                   (setq threads nil)
4220                   (throw 'infloop t))
4221                 (unless (car (symbol-value refs))
4222                   ;; These threads do not refer back to any other
4223                   ;; articles, so they're roots.
4224                   (setq threads (append (cdr (symbol-value refs)) threads))))
4225               gnus-newsgroup-dependencies)))
4226     threads))
4227
4228 ;; Build the thread tree.
4229 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4230   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4231
4232 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4233 if it was already present.
4234
4235 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4236 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4237 Message-IDs will be renamed to a unique Message-ID before being
4238 entered.
4239
4240 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4241   (let* ((id (mail-header-id header))
4242          (id-dep (and id (intern id dependencies)))
4243          parent-id ref ref-dep ref-header replaced)
4244     ;; Enter this `header' in the `dependencies' table.
4245     (cond
4246      ((not id-dep)
4247       (setq header nil))
4248      ;; The first two cases do the normal part: enter a new `header'
4249      ;; in the `dependencies' table.
4250      ((not (boundp id-dep))
4251       (set id-dep (list header)))
4252      ((null (car (symbol-value id-dep)))
4253       (setcar (symbol-value id-dep) header))
4254
4255      ;; From here the `header' was already present in the
4256      ;; `dependencies' table.
4257      (force-new
4258       ;; Overrides an existing entry;
4259       ;; just set the header part of the entry.
4260       (setcar (symbol-value id-dep) header)
4261       (setq replaced t))
4262
4263      ;; Renames the existing `header' to a unique Message-ID.
4264      ((not gnus-summary-ignore-duplicates)
4265       ;; An article with this Message-ID has already been seen.
4266       ;; We rename the Message-ID.
4267       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4268            (list header))
4269       (mail-header-set-id header id))
4270
4271      ;; The last case ignores an existing entry, except it adds any
4272      ;; additional Xrefs (in case the two articles came from different
4273      ;; servers.
4274      ;; Also sets `header' to `nil' meaning that the `dependencies'
4275      ;; table was *not* modified.
4276      (t
4277       (mail-header-set-xref
4278        (car (symbol-value id-dep))
4279        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4280                    "")
4281                (or (mail-header-xref header) "")))
4282       (setq header nil)))
4283
4284     (when (and header (not replaced))
4285       ;; First check that we are not creating a References loop.
4286       (setq parent-id (gnus-parent-id (mail-header-references header)))
4287       (setq ref parent-id)
4288       (while (and ref
4289                   (setq ref-dep (intern-soft ref dependencies))
4290                   (boundp ref-dep)
4291                   (setq ref-header (car (symbol-value ref-dep))))
4292         (if (string= id ref)
4293             ;; Yuk!  This is a reference loop.  Make the article be a
4294             ;; root article.
4295             (progn
4296               (mail-header-set-references (car (symbol-value id-dep)) "none")
4297               (setq ref nil)
4298               (setq parent-id nil))
4299           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4300       (setq ref-dep (intern (or parent-id "none") dependencies))
4301       (if (boundp ref-dep)
4302           (setcdr (symbol-value ref-dep)
4303                   (nconc (cdr (symbol-value ref-dep))
4304                          (list (symbol-value id-dep))))
4305         (set ref-dep (list nil (symbol-value id-dep)))))
4306     header))
4307
4308 (defun gnus-extract-message-id-from-in-reply-to (string)
4309   (if (string-match "<[^>]+>" string)
4310       (substring string (match-beginning 0) (match-end 0))
4311     nil))
4312
4313 (defun gnus-build-sparse-threads ()
4314   (let ((headers gnus-newsgroup-headers)
4315         (mail-parse-charset gnus-newsgroup-charset)
4316         (gnus-summary-ignore-duplicates t)
4317         header references generation relations
4318         subject child end new-child date)
4319     ;; First we create an alist of generations/relations, where
4320     ;; generations is how much we trust the relation, and the relation
4321     ;; is parent/child.
4322     (gnus-message 7 "Making sparse threads...")
4323     (save-excursion
4324       (nnheader-set-temp-buffer " *gnus sparse threads*")
4325       (while (setq header (pop headers))
4326         (when (and (setq references (mail-header-references header))
4327                    (not (string= references "")))
4328           (insert references)
4329           (setq child (mail-header-id header)
4330                 subject (mail-header-subject header)
4331                 date (mail-header-date header)
4332                 generation 0)
4333           (while (search-backward ">" nil t)
4334             (setq end (1+ (point)))
4335             (when (search-backward "<" nil t)
4336               (setq new-child (buffer-substring (point) end))
4337               (push (list (incf generation)
4338                           child (setq child new-child)
4339                           subject date)
4340                     relations)))
4341           (when child
4342             (push (list (1+ generation) child nil subject) relations))
4343           (erase-buffer)))
4344       (kill-buffer (current-buffer)))
4345     ;; Sort over trustworthiness.
4346     (dolist (relation (sort relations 'car-less-than-car))
4347       (when (gnus-dependencies-add-header
4348              (make-full-mail-header
4349               gnus-reffed-article-number
4350               (nth 3 relation) "" (or (nth 4 relation) "")
4351               (nth 1 relation)
4352               (or (nth 2 relation) "") 0 0 "")
4353              gnus-newsgroup-dependencies nil)
4354         (push gnus-reffed-article-number gnus-newsgroup-limit)
4355         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4356         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4357               gnus-newsgroup-reads)
4358         (decf gnus-reffed-article-number)))
4359     (gnus-message 7 "Making sparse threads...done")))
4360
4361 (defun gnus-build-old-threads ()
4362   ;; Look at all the articles that refer back to old articles, and
4363   ;; fetch the headers for the articles that aren't there.  This will
4364   ;; build complete threads - if the roots haven't been expired by the
4365   ;; server, that is.
4366   (let ((mail-parse-charset gnus-newsgroup-charset)
4367         id heads)
4368     (mapatoms
4369      (lambda (refs)
4370        (when (not (car (symbol-value refs)))
4371          (setq heads (cdr (symbol-value refs)))
4372          (while heads
4373            (if (memq (mail-header-number (caar heads))
4374                      gnus-newsgroup-dormant)
4375                (setq heads (cdr heads))
4376              (setq id (symbol-name refs))
4377              (while (and (setq id (gnus-build-get-header id))
4378                          (not (car (gnus-id-to-thread id)))))
4379              (setq heads nil)))))
4380      gnus-newsgroup-dependencies)))
4381
4382 (defsubst gnus-remove-odd-characters (string)
4383   "Translate STRING into something that doesn't contain weird characters."
4384   (mm-subst-char-in-string
4385    ?\r ?\-
4386    (mm-subst-char-in-string ?\n ?\- string t) t))
4387
4388 ;; This function has to be called with point after the article number
4389 ;; on the beginning of the line.
4390 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4391   (let ((eol (point-at-eol))
4392         (buffer (current-buffer))
4393         header references in-reply-to)
4394
4395     ;; overview: [num subject from date id refs chars lines misc]
4396     (unwind-protect
4397         (let (x)
4398           (narrow-to-region (point) eol)
4399           (unless (eobp)
4400             (forward-char))
4401
4402           (setq header
4403                 (make-full-mail-header
4404                  number                 ; number
4405                  (condition-case ()     ; subject
4406                      (gnus-remove-odd-characters
4407                       (funcall gnus-decode-encoded-word-function
4408                                (setq x (nnheader-nov-field))))
4409                    (error x))
4410                  (condition-case ()     ; from
4411                      (gnus-remove-odd-characters
4412                       (funcall gnus-decode-encoded-address-function
4413                                (setq x (nnheader-nov-field))))
4414                    (error x))
4415                  (nnheader-nov-field)   ; date
4416                  (nnheader-nov-read-message-id number)  ; id
4417                  (setq references (nnheader-nov-field)) ; refs
4418                  (nnheader-nov-read-integer) ; chars
4419                  (nnheader-nov-read-integer) ; lines
4420                  (unless (eobp)
4421                    (if (looking-at "Xref: ")
4422                        (goto-char (match-end 0)))
4423                    (nnheader-nov-field)) ; Xref
4424                  (nnheader-nov-parse-extra)))) ; extra
4425
4426       (widen))
4427
4428     (when (and (string= references "")
4429                (setq in-reply-to (mail-header-extra header))
4430                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4431       (mail-header-set-references
4432        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4433
4434     (when gnus-alter-header-function
4435       (funcall gnus-alter-header-function header))
4436     (gnus-dependencies-add-header header dependencies force-new)))
4437
4438 (defun gnus-build-get-header (id)
4439   "Look through the buffer of NOV lines and find the header to ID.
4440 Enter this line into the dependencies hash table, and return
4441 the id of the parent article (if any)."
4442   (let ((deps gnus-newsgroup-dependencies)
4443         found header)
4444     (prog1
4445         (with-current-buffer nntp-server-buffer
4446           (let ((case-fold-search nil))
4447             (goto-char (point-min))
4448             (while (and (not found)
4449                         (search-forward id nil t))
4450               (beginning-of-line)
4451               (setq found (looking-at
4452                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4453                                    (regexp-quote id))))
4454               (or found (beginning-of-line 2)))
4455             (when found
4456               (beginning-of-line)
4457               (and
4458                (setq header (gnus-nov-parse-line
4459                              (read (current-buffer)) deps))
4460                (gnus-parent-id (mail-header-references header))))))
4461       (when header
4462         (let ((number (mail-header-number header)))
4463           (push number gnus-newsgroup-limit)
4464           (push header gnus-newsgroup-headers)
4465           (if (memq number gnus-newsgroup-unselected)
4466               (progn
4467                 (setq gnus-newsgroup-unreads
4468                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4469                                                number))
4470                 (setq gnus-newsgroup-unselected
4471                       (delq number gnus-newsgroup-unselected)))
4472             (push number gnus-newsgroup-ancient)))))))
4473
4474 (defun gnus-build-all-threads ()
4475   "Read all the headers."
4476   (let ((gnus-summary-ignore-duplicates t)
4477         (mail-parse-charset gnus-newsgroup-charset)
4478         (dependencies gnus-newsgroup-dependencies)
4479         header article)
4480     (with-current-buffer nntp-server-buffer
4481       (let ((case-fold-search nil))
4482         (goto-char (point-min))
4483         (while (not (eobp))
4484           (ignore-errors
4485             (setq article (read (current-buffer))
4486                   header (gnus-nov-parse-line article dependencies)))
4487           (when header
4488             (with-current-buffer gnus-summary-buffer
4489               (push header gnus-newsgroup-headers)
4490               (if (memq (setq article (mail-header-number header))
4491                         gnus-newsgroup-unselected)
4492                   (progn
4493                     (setq gnus-newsgroup-unreads
4494                           (gnus-add-to-sorted-list
4495                            gnus-newsgroup-unreads article))
4496                     (setq gnus-newsgroup-unselected
4497                           (delq article gnus-newsgroup-unselected)))
4498                 (push article gnus-newsgroup-ancient)))
4499             (forward-line 1)))))))
4500
4501 (defun gnus-summary-update-article-line (article header)
4502   "Update the line for ARTICLE using HEADER."
4503   (let* ((id (mail-header-id header))
4504          (thread (gnus-id-to-thread id)))
4505     (unless thread
4506       (error "Article in no thread"))
4507     ;; Update the thread.
4508     (setcar thread header)
4509     (gnus-summary-goto-subject article)
4510     (let* ((datal (gnus-data-find-list article))
4511            (data (car datal))
4512            (inhibit-read-only t)
4513            (level (gnus-summary-thread-level)))
4514       (gnus-delete-line)
4515       (let ((inserted (- (point)
4516                          (progn
4517                            (gnus-summary-insert-line
4518                             header level nil
4519                             (memq article gnus-newsgroup-undownloaded)
4520                             (gnus-article-mark article)
4521                             (memq article gnus-newsgroup-replied)
4522                             (memq article gnus-newsgroup-expirable)
4523                             ;; Only insert the Subject string when it's different
4524                             ;; from the previous Subject string.
4525                             (if (and
4526                                  gnus-show-threads
4527                                  (gnus-subject-equal
4528                                   (condition-case ()
4529                                       (mail-header-subject
4530                                        (gnus-data-header
4531                                         (cadr
4532                                          (gnus-data-find-list
4533                                           article
4534                                           (gnus-data-list t)))))
4535                                     ;; Error on the side of excessive subjects.
4536                                     (error ""))
4537                                   (mail-header-subject header)))
4538                                 ""
4539                               (mail-header-subject header))
4540                             nil (cdr (assq article gnus-newsgroup-scored))
4541                             (memq article gnus-newsgroup-processable))
4542                            (point)))))
4543         (when (cdr datal)
4544           (gnus-data-update-list
4545            (cdr datal)
4546            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4547
4548 (defun gnus-summary-update-article (article &optional iheader)
4549   "Update ARTICLE in the summary buffer."
4550   (set-buffer gnus-summary-buffer)
4551   (let* ((header (gnus-summary-article-header article))
4552          (id (mail-header-id header))
4553          (data (gnus-data-find article))
4554          (thread (gnus-id-to-thread id))
4555          (references (mail-header-references header))
4556          (parent
4557           (gnus-id-to-thread
4558            (or (gnus-parent-id
4559                 (when (and references
4560                            (not (equal "" references)))
4561                   references))
4562                "none")))
4563          (inhibit-read-only t)
4564          (old (car thread)))
4565     (when thread
4566       (unless iheader
4567         (setcar thread nil)
4568         (when parent
4569           (delq thread parent)))
4570       (if (gnus-summary-insert-subject id header)
4571           ;; Set the (possibly) new article number in the data structure.
4572           (gnus-data-set-number data (gnus-id-to-article id))
4573         (setcar thread old)
4574         nil))))
4575
4576 (defun gnus-rebuild-thread (id &optional line)
4577   "Rebuild the thread containing ID.
4578 If LINE, insert the rebuilt thread starting on line LINE."
4579   (let ((inhibit-read-only t)
4580         old-pos current thread data)
4581     (if (not gnus-show-threads)
4582         (setq thread (list (car (gnus-id-to-thread id))))
4583       ;; Get the thread this article is part of.
4584       (setq thread (gnus-remove-thread id)))
4585     (setq old-pos (point-at-bol))
4586     (setq current (save-excursion
4587                     (and (re-search-backward "[\r\n]" nil t)
4588                          (gnus-summary-article-number))))
4589     ;; If this is a gathered thread, we have to go some re-gathering.
4590     (when (stringp (car thread))
4591       (let ((subject (car thread))
4592             roots thr)
4593         (setq thread (cdr thread))
4594         (while thread
4595           (unless (memq (setq thr (gnus-id-to-thread
4596                                    (gnus-root-id
4597                                     (mail-header-id (caar thread)))))
4598                         roots)
4599             (push thr roots))
4600           (setq thread (cdr thread)))
4601         ;; We now have all (unique) roots.
4602         (if (= (length roots) 1)
4603             ;; All the loose roots are now one solid root.
4604             (setq thread (car roots))
4605           (setq thread (cons subject (gnus-sort-threads roots))))))
4606     (let (threads)
4607       ;; We then insert this thread into the summary buffer.
4608       (when line
4609         (goto-char (point-min))
4610         (forward-line (1- line)))
4611       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4612         (if gnus-show-threads
4613             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4614           (gnus-summary-prepare-unthreaded thread))
4615         (setq data (nreverse gnus-newsgroup-data))
4616         (setq threads gnus-newsgroup-threads))
4617       ;; We splice the new data into the data structure.
4618       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4619       ;;!!! then we want to insert at the beginning of the buffer.
4620       ;;!!! That happens to be true with Gnus now, but that may
4621       ;;!!! change in the future.  Perhaps.
4622       (gnus-data-enter-list
4623        (if line nil current) data (- (point) old-pos))
4624       (setq gnus-newsgroup-threads
4625             (nconc threads gnus-newsgroup-threads))
4626       (gnus-data-compute-positions))))
4627
4628 (defun gnus-number-to-header (number)
4629   "Return the header for article NUMBER."
4630   (let ((headers gnus-newsgroup-headers))
4631     (while (and headers
4632                 (not (= number (mail-header-number (car headers)))))
4633       (pop headers))
4634     (when headers
4635       (car headers))))
4636
4637 (defun gnus-parent-headers (in-headers &optional generation)
4638   "Return the headers of the GENERATIONeth parent of HEADERS."
4639   (unless generation
4640     (setq generation 1))
4641   (let ((parent t)
4642         (headers in-headers)
4643         references)
4644     (while (and parent
4645                 (not (zerop generation))
4646                 (setq references (mail-header-references headers)))
4647       (setq headers (if (and references
4648                              (setq parent (gnus-parent-id references)))
4649                         (car (gnus-id-to-thread parent))
4650                       nil))
4651       (decf generation))
4652     (and (not (eq headers in-headers))
4653          headers)))
4654
4655 (defun gnus-id-to-thread (id)
4656   "Return the (sub-)thread where ID appears."
4657   (gnus-gethash id gnus-newsgroup-dependencies))
4658
4659 (defun gnus-id-to-article (id)
4660   "Return the article number of ID."
4661   (let ((thread (gnus-id-to-thread id)))
4662     (when (and thread
4663                (car thread))
4664       (mail-header-number (car thread)))))
4665
4666 (defun gnus-id-to-header (id)
4667   "Return the article headers of ID."
4668   (car (gnus-id-to-thread id)))
4669
4670 (defun gnus-article-displayed-root-p (article)
4671   "Say whether ARTICLE is a root(ish) article."
4672   (let ((level (gnus-summary-thread-level article))
4673         (refs (mail-header-references  (gnus-summary-article-header article)))
4674         particle)
4675     (cond
4676      ((null level) nil)
4677      ((zerop level) t)
4678      ((null refs) t)
4679      ((null (gnus-parent-id refs)) t)
4680      ((and (= 1 level)
4681            (null (setq particle (gnus-id-to-article
4682                                  (gnus-parent-id refs))))
4683            (null (gnus-summary-thread-level particle)))))))
4684
4685 (defun gnus-root-id (id)
4686   "Return the id of the root of the thread where ID appears."
4687   (let (last-id prev)
4688     (while (and id (setq prev (car (gnus-id-to-thread id))))
4689       (setq last-id id
4690             id (gnus-parent-id (mail-header-references prev))))
4691     last-id))
4692
4693 (defun gnus-articles-in-thread (thread)
4694   "Return the list of articles in THREAD."
4695   (cons (mail-header-number (car thread))
4696         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4697
4698 (defun gnus-remove-thread (id &optional dont-remove)
4699   "Remove the thread that has ID in it."
4700   (let (headers thread last-id)
4701     ;; First go up in this thread until we find the root.
4702     (setq last-id (gnus-root-id id)
4703           headers (message-flatten-list (gnus-id-to-thread last-id)))
4704     ;; We have now found the real root of this thread.  It might have
4705     ;; been gathered into some loose thread, so we have to search
4706     ;; through the threads to find the thread we wanted.
4707     (let ((threads gnus-newsgroup-threads)
4708           sub)
4709       (while threads
4710         (setq sub (car threads))
4711         (if (stringp (car sub))
4712             ;; This is a gathered thread, so we look at the roots
4713             ;; below it to find whether this article is in this
4714             ;; gathered root.
4715             (progn
4716               (setq sub (cdr sub))
4717               (while sub
4718                 (when (member (caar sub) headers)
4719                   (setq thread (car threads)
4720                         threads nil
4721                         sub nil))
4722                 (setq sub (cdr sub))))
4723           ;; It's an ordinary thread, so we check it.
4724           (when (eq (car sub) (car headers))
4725             (setq thread sub
4726                   threads nil)))
4727         (setq threads (cdr threads)))
4728       ;; If this article is in no thread, then it's a root.
4729       (if thread
4730           (unless dont-remove
4731             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4732         (setq thread (gnus-id-to-thread last-id)))
4733       (when thread
4734         (prog1
4735             thread                      ; We return this thread.
4736           (unless dont-remove
4737             (if (stringp (car thread))
4738                 (progn
4739                   ;; If we use dummy roots, then we have to remove the
4740                   ;; dummy root as well.
4741                   (when (eq gnus-summary-make-false-root 'dummy)
4742                     ;; We go to the dummy root by going to
4743                     ;; the first sub-"thread", and then one line up.
4744                     (gnus-summary-goto-article
4745                      (mail-header-number (caadr thread)))
4746                     (forward-line -1)
4747                     (gnus-delete-line)
4748                     (gnus-data-compute-positions))
4749                   (setq thread (cdr thread))
4750                   (while thread
4751                     (gnus-remove-thread-1 (car thread))
4752                     (setq thread (cdr thread))))
4753               (gnus-remove-thread-1 thread))))))))
4754
4755 (defun gnus-remove-thread-1 (thread)
4756   "Remove the thread THREAD recursively."
4757   (let ((number (mail-header-number (pop thread)))
4758         d)
4759     (setq thread (reverse thread))
4760     (while thread
4761       (gnus-remove-thread-1 (pop thread)))
4762     (when (setq d (gnus-data-find number))
4763       (goto-char (gnus-data-pos d))
4764       (gnus-summary-show-thread)
4765       (gnus-data-remove
4766        number
4767        (- (point-at-bol)
4768           (prog1
4769               (1+ (point-at-eol))
4770             (gnus-delete-line)))))))
4771
4772 (defun gnus-sort-threads-recursive (threads func)
4773   (sort (mapcar (lambda (thread)
4774                   (cons (car thread)
4775                         (and (cdr thread)
4776                              (gnus-sort-threads-recursive (cdr thread) func))))
4777                 threads) func))
4778
4779 (defun gnus-sort-threads-loop (threads func)
4780   (let* ((superthread (cons nil threads))
4781          (stack (list (cons superthread threads)))
4782          remaining-threads thread)
4783     (while stack
4784       (setq remaining-threads (cdr (car stack)))
4785       (if remaining-threads
4786           (progn (setq thread (car remaining-threads))
4787                  (setcdr (car stack) (cdr remaining-threads))
4788                  (if (cdr thread)
4789                      (push (cons thread (cdr thread)) stack)))
4790         (setq thread (caar stack))
4791         (setcdr thread (sort (cdr thread) func))
4792         (pop stack)))
4793     (cdr superthread)))
4794
4795 (defun gnus-sort-threads (threads)
4796   "Sort THREADS."
4797   (if (not gnus-thread-sort-functions)
4798       threads
4799     (gnus-message 8 "Sorting threads...")
4800     (prog1
4801         (condition-case nil
4802             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4803               (gnus-sort-threads-recursive
4804                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4805           ;; Even after binding max-lisp-eval-depth, the recursive
4806           ;; sorter might fail for very long threads.  In that case,
4807           ;; try using a (less well-tested) non-recursive sorter.
4808           (error (gnus-sort-threads-loop
4809                   threads (gnus-make-sort-function
4810                            gnus-thread-sort-functions))))
4811       (gnus-message 8 "Sorting threads...done"))))
4812
4813 (defun gnus-sort-articles (articles)
4814   "Sort ARTICLES."
4815   (when gnus-article-sort-functions
4816     (gnus-message 7 "Sorting articles...")
4817     (prog1
4818         (setq gnus-newsgroup-headers
4819               (sort articles (gnus-make-sort-function
4820                               gnus-article-sort-functions)))
4821       (gnus-message 7 "Sorting articles...done"))))
4822
4823 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4824 (defmacro gnus-thread-header (thread)
4825   "Return header of first article in THREAD.
4826 Note that THREAD must never, ever be anything else than a variable -
4827 using some other form will lead to serious barfage."
4828   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4829   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4830   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4831         (vector thread) 2))
4832
4833 (defsubst gnus-article-sort-by-number (h1 h2)
4834   "Sort articles by article number."
4835   (< (mail-header-number h1)
4836      (mail-header-number h2)))
4837
4838 (defun gnus-thread-sort-by-number (h1 h2)
4839   "Sort threads by root article number."
4840   (gnus-article-sort-by-number
4841    (gnus-thread-header h1) (gnus-thread-header h2)))
4842
4843 (defsubst gnus-article-sort-by-random (h1 h2)
4844   "Sort articles randomly."
4845   (zerop (random 2)))
4846
4847 (defun gnus-thread-sort-by-random (h1 h2)
4848   "Sort threads randomly."
4849   (gnus-article-sort-by-random
4850    (gnus-thread-header h1) (gnus-thread-header h2)))
4851
4852 (defsubst gnus-article-sort-by-lines (h1 h2)
4853   "Sort articles by article Lines header."
4854   (< (mail-header-lines h1)
4855      (mail-header-lines h2)))
4856
4857 (defun gnus-thread-sort-by-lines (h1 h2)
4858   "Sort threads by root article Lines header."
4859   (gnus-article-sort-by-lines
4860    (gnus-thread-header h1) (gnus-thread-header h2)))
4861
4862 (defsubst gnus-article-sort-by-chars (h1 h2)
4863   "Sort articles by octet length."
4864   (< (mail-header-chars h1)
4865      (mail-header-chars h2)))
4866
4867 (defun gnus-thread-sort-by-chars (h1 h2)
4868   "Sort threads by root article octet length."
4869   (gnus-article-sort-by-chars
4870    (gnus-thread-header h1) (gnus-thread-header h2)))
4871
4872 (defsubst gnus-article-sort-by-author (h1 h2)
4873   "Sort articles by root author."
4874   (gnus-string<
4875    (let ((extract (funcall
4876                    gnus-extract-address-components
4877                    (mail-header-from h1))))
4878      (or (car extract) (cadr extract) ""))
4879    (let ((extract (funcall
4880                    gnus-extract-address-components
4881                    (mail-header-from h2))))
4882      (or (car extract) (cadr extract) ""))))
4883
4884 (defun gnus-thread-sort-by-author (h1 h2)
4885   "Sort threads by root author."
4886   (gnus-article-sort-by-author
4887    (gnus-thread-header h1)  (gnus-thread-header h2)))
4888
4889 (defsubst gnus-article-sort-by-recipient (h1 h2)
4890   "Sort articles by recipient."
4891   (gnus-string<
4892    (let ((extract (funcall
4893                    gnus-extract-address-components
4894                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4895      (or (car extract) (cadr extract)))
4896    (let ((extract (funcall
4897                    gnus-extract-address-components
4898                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4899      (or (car extract) (cadr extract)))))
4900
4901 (defun gnus-thread-sort-by-recipient (h1 h2)
4902   "Sort threads by root recipient."
4903   (gnus-article-sort-by-recipient
4904    (gnus-thread-header h1) (gnus-thread-header h2)))
4905
4906 (defsubst gnus-article-sort-by-subject (h1 h2)
4907   "Sort articles by root subject."
4908   (gnus-string<
4909    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4910    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4911
4912 (defun gnus-thread-sort-by-subject (h1 h2)
4913   "Sort threads by root subject."
4914   (gnus-article-sort-by-subject
4915    (gnus-thread-header h1) (gnus-thread-header h2)))
4916
4917 (defsubst gnus-article-sort-by-date (h1 h2)
4918   "Sort articles by root article date."
4919   (time-less-p
4920    (gnus-date-get-time (mail-header-date h1))
4921    (gnus-date-get-time (mail-header-date h2))))
4922
4923 (defun gnus-thread-sort-by-date (h1 h2)
4924   "Sort threads by root article date."
4925   (gnus-article-sort-by-date
4926    (gnus-thread-header h1) (gnus-thread-header h2)))
4927
4928 (defsubst gnus-article-sort-by-score (h1 h2)
4929   "Sort articles by root article score.
4930 Unscored articles will be counted as having a score of zero."
4931   (> (or (cdr (assq (mail-header-number h1)
4932                     gnus-newsgroup-scored))
4933          gnus-summary-default-score 0)
4934      (or (cdr (assq (mail-header-number h2)
4935                     gnus-newsgroup-scored))
4936          gnus-summary-default-score 0)))
4937
4938 (defun gnus-thread-sort-by-score (h1 h2)
4939   "Sort threads by root article score."
4940   (gnus-article-sort-by-score
4941    (gnus-thread-header h1) (gnus-thread-header h2)))
4942
4943 (defun gnus-thread-sort-by-total-score (h1 h2)
4944   "Sort threads by the sum of all scores in the thread.
4945 Unscored articles will be counted as having a score of zero."
4946   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4947
4948 (defun gnus-thread-total-score (thread)
4949   ;; This function find the total score of THREAD.
4950   (cond
4951    ((null thread)
4952     0)
4953    ((consp thread)
4954     (if (stringp (car thread))
4955         (apply gnus-thread-score-function 0
4956                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4957       (gnus-thread-total-score-1 thread)))
4958    (t
4959     (gnus-thread-total-score-1 (list thread)))))
4960
4961 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4962   "Sort threads such that the thread with the most recently arrived article comes first."
4963   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4964
4965 (defun gnus-thread-highest-number (thread)
4966   "Return the highest article number in THREAD."
4967   (apply 'max (mapcar (lambda (header)
4968                         (mail-header-number header))
4969                       (message-flatten-list thread))))
4970
4971 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4972   "Sort threads such that the thread with the most recently dated article comes first."
4973   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4974
4975 (defun gnus-thread-latest-date (thread)
4976   "Return the highest article date in THREAD."
4977   (let ((previous-time 0))
4978     (apply 'max
4979            (mapcar
4980             (lambda (header)
4981               (setq previous-time
4982                     (condition-case ()
4983                         (time-to-seconds (mail-header-parse-date
4984                                           (mail-header-date header)))
4985                       (error previous-time))))
4986             (sort
4987              (message-flatten-list thread)
4988              (lambda (h1 h2)
4989                (< (mail-header-number h1)
4990                   (mail-header-number h2))))))))
4991
4992 (defun gnus-thread-total-score-1 (root)
4993   ;; This function find the total score of the thread below ROOT.
4994   (setq root (car root))
4995   (apply gnus-thread-score-function
4996          (or (append
4997               (mapcar 'gnus-thread-total-score
4998                       (cdr (gnus-id-to-thread (mail-header-id root))))
4999               (when (> (mail-header-number root) 0)
5000                 (list (or (cdr (assq (mail-header-number root)
5001                                      gnus-newsgroup-scored))
5002                           gnus-summary-default-score 0))))
5003              (list gnus-summary-default-score)
5004              '(0))))
5005
5006 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5007 (defvar gnus-tmp-prev-subject nil)
5008 (defvar gnus-tmp-false-parent nil)
5009 (defvar gnus-tmp-root-expunged nil)
5010 (defvar gnus-tmp-dummy-line nil)
5011
5012 (defun gnus-extra-header (type &optional header)
5013   "Return the extra header of TYPE."
5014   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5015       ""))
5016
5017 (defvar gnus-tmp-thread-tree-header-string "")
5018
5019 (defcustom gnus-sum-thread-tree-root "> "
5020   "With %B spec, used for the root of a thread.
5021 If nil, use subject instead."
5022   :version "22.1"
5023   :type '(radio (const :format "%v  " nil) string)
5024   :group 'gnus-thread)
5025
5026 (defcustom gnus-sum-thread-tree-false-root "> "
5027   "With %B spec, used for a false root of a thread.
5028 If nil, use subject instead."
5029   :version "22.1"
5030   :type '(radio (const :format "%v  " nil) string)
5031   :group 'gnus-thread)
5032
5033 (defcustom gnus-sum-thread-tree-single-indent ""
5034   "With %B spec, used for a thread with just one message.
5035 If nil, use subject instead."
5036   :version "22.1"
5037   :type '(radio (const :format "%v  " nil) string)
5038   :group 'gnus-thread)
5039
5040 (defcustom gnus-sum-thread-tree-vertical "| "
5041   "With %B spec, used for drawing a vertical line."
5042   :version "22.1"
5043   :type 'string
5044   :group 'gnus-thread)
5045
5046 (defcustom gnus-sum-thread-tree-indent "  "
5047   "With %B spec, used for indenting."
5048   :version "22.1"
5049   :type 'string
5050   :group 'gnus-thread)
5051
5052 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5053   "With %B spec, used for a leaf with brothers."
5054   :version "22.1"
5055   :type 'string
5056   :group 'gnus-thread)
5057
5058 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5059   "With %B spec, used for a leaf without brothers."
5060   :version "22.1"
5061   :type 'string
5062   :group 'gnus-thread)
5063
5064 (defcustom gnus-summary-display-while-building nil
5065   "If non-nil, show and update the summary buffer as it's being built.
5066 If the value is t, update the buffer after every line is inserted.  If
5067 the value is an integer (N), update the display every N lines."
5068   :version "22.1"
5069   :group 'gnus-thread
5070   :type '(choice (const :tag "off" nil)
5071                  number
5072                  (const :tag "frequently" t)))
5073
5074 (defun gnus-summary-prepare-threads (threads)
5075   "Prepare summary buffer from THREADS and indentation LEVEL.
5076 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5077 or a straight list of headers."
5078   (gnus-message 7 "Generating summary...")
5079
5080   (setq gnus-newsgroup-threads threads)
5081   (beginning-of-line)
5082
5083   (let ((gnus-tmp-level 0)
5084         (default-score (or gnus-summary-default-score 0))
5085         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5086         (building-line-count gnus-summary-display-while-building)
5087         (building-count (integerp gnus-summary-display-while-building))
5088         thread number subject stack state gnus-tmp-gathered beg-match
5089         new-roots gnus-tmp-new-adopts thread-end simp-subject
5090         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5091         gnus-tmp-replied gnus-tmp-subject-or-nil
5092         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5093         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5094         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5095         tree-stack)
5096
5097     (setq gnus-tmp-prev-subject nil
5098           gnus-tmp-thread-tree-header-string "")
5099
5100     (if (vectorp (car threads))
5101         ;; If this is a straight (sic) list of headers, then a
5102         ;; threaded summary display isn't required, so we just create
5103         ;; an unthreaded one.
5104         (gnus-summary-prepare-unthreaded threads)
5105
5106       ;; Do the threaded display.
5107
5108       (if gnus-summary-display-while-building
5109           (switch-to-buffer (buffer-name)))
5110       (while (or threads stack gnus-tmp-new-adopts new-roots)
5111
5112         (if (and (= gnus-tmp-level 0)
5113                  (or (not stack)
5114                      (= (caar stack) 0))
5115                  (not gnus-tmp-false-parent)
5116                  (or gnus-tmp-new-adopts new-roots))
5117             (if gnus-tmp-new-adopts
5118                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5119                       thread (list (car gnus-tmp-new-adopts))
5120                       gnus-tmp-header (caar thread)
5121                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5122               (when new-roots
5123                 (setq thread (list (car new-roots))
5124                       gnus-tmp-header (caar thread)
5125                       new-roots (cdr new-roots))))
5126
5127           (if threads
5128               ;; If there are some threads, we do them before the
5129               ;; threads on the stack.
5130               (setq thread threads
5131                     gnus-tmp-header (caar thread))
5132             ;; There were no current threads, so we pop something off
5133             ;; the stack.
5134             (setq state (car stack)
5135                   gnus-tmp-level (car state)
5136                   tree-stack (cadr state)
5137                   thread (caddr state)
5138                   stack (cdr stack)
5139                   gnus-tmp-header (caar thread))))
5140
5141         (setq gnus-tmp-false-parent nil)
5142         (setq gnus-tmp-root-expunged nil)
5143         (setq thread-end nil)
5144
5145         (if (stringp gnus-tmp-header)
5146             ;; The header is a dummy root.
5147             (cond
5148              ((eq gnus-summary-make-false-root 'adopt)
5149               ;; We let the first article adopt the rest.
5150               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5151                                                (cddar thread)))
5152               (setq gnus-tmp-gathered
5153                     (nconc (mapcar
5154                             (lambda (h) (mail-header-number (car h)))
5155                             (cddar thread))
5156                            gnus-tmp-gathered))
5157               (setq thread (cons (list (caar thread)
5158                                        (cadar thread))
5159                                  (cdr thread)))
5160               (setq gnus-tmp-level -1
5161                     gnus-tmp-false-parent t))
5162              ((eq gnus-summary-make-false-root 'empty)
5163               ;; We print adopted articles with empty subject fields.
5164               (setq gnus-tmp-gathered
5165                     (nconc (mapcar
5166                             (lambda (h) (mail-header-number (car h)))
5167                             (cddar thread))
5168                            gnus-tmp-gathered))
5169               (setq gnus-tmp-level -1))
5170              ((eq gnus-summary-make-false-root 'dummy)
5171               ;; We remember that we probably want to output a dummy
5172               ;; root.
5173               (setq gnus-tmp-dummy-line gnus-tmp-header)
5174               (setq gnus-tmp-prev-subject gnus-tmp-header))
5175              (t
5176               ;; We do not make a root for the gathered
5177               ;; sub-threads at all.
5178               (setq gnus-tmp-level -1)))
5179
5180           (setq number (mail-header-number gnus-tmp-header)
5181                 subject (mail-header-subject gnus-tmp-header)
5182                 simp-subject (gnus-simplify-subject-fully subject))
5183
5184           (cond
5185            ;; If the thread has changed subject, we might want to make
5186            ;; this subthread into a root.
5187            ((and (null gnus-thread-ignore-subject)
5188                  (not (zerop gnus-tmp-level))
5189                  gnus-tmp-prev-subject
5190                  (not (string= gnus-tmp-prev-subject simp-subject)))
5191             (setq new-roots (nconc new-roots (list (car thread)))
5192                   thread-end t
5193                   gnus-tmp-header nil))
5194            ;; If the article lies outside the current limit,
5195            ;; then we do not display it.
5196            ((not (memq number gnus-newsgroup-limit))
5197             (setq gnus-tmp-gathered
5198                   (nconc (mapcar
5199                           (lambda (h) (mail-header-number (car h)))
5200                           (cdar thread))
5201                          gnus-tmp-gathered))
5202             (setq gnus-tmp-new-adopts (if (cdar thread)
5203                                           (append gnus-tmp-new-adopts
5204                                                   (cdar thread))
5205                                         gnus-tmp-new-adopts)
5206                   thread-end t
5207                   gnus-tmp-header nil)
5208             (when (zerop gnus-tmp-level)
5209               (setq gnus-tmp-root-expunged t)))
5210            ;; Perhaps this article is to be marked as read?
5211            ((and gnus-summary-mark-below
5212                  (< (or (cdr (assq number gnus-newsgroup-scored))
5213                         default-score)
5214                     gnus-summary-mark-below)
5215                  ;; Don't touch sparse articles.
5216                  (not (gnus-summary-article-sparse-p number))
5217                  (not (gnus-summary-article-ancient-p number)))
5218             (setq gnus-newsgroup-unreads
5219                   (delq number gnus-newsgroup-unreads))
5220             (if gnus-newsgroup-auto-expire
5221                 (setq gnus-newsgroup-expirable
5222                       (gnus-add-to-sorted-list
5223                        gnus-newsgroup-expirable number))
5224               (push (cons number gnus-low-score-mark)
5225                     gnus-newsgroup-reads))))
5226
5227           (when gnus-tmp-header
5228             ;; We may have an old dummy line to output before this
5229             ;; article.
5230             (when (and gnus-tmp-dummy-line
5231                        (gnus-subject-equal
5232                         gnus-tmp-dummy-line
5233                         (mail-header-subject gnus-tmp-header)))
5234               (gnus-summary-insert-dummy-line
5235                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5236               (setq gnus-tmp-dummy-line nil))
5237
5238             ;; Compute the mark.
5239             (setq gnus-tmp-unread (gnus-article-mark number))
5240
5241             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5242                                   gnus-tmp-header gnus-tmp-level)
5243                   gnus-newsgroup-data)
5244
5245             ;; Actually insert the line.
5246             (setq
5247              gnus-tmp-subject-or-nil
5248              (cond
5249               ((and gnus-thread-ignore-subject
5250                     gnus-tmp-prev-subject
5251                     (not (string= gnus-tmp-prev-subject simp-subject)))
5252                subject)
5253               ((zerop gnus-tmp-level)
5254                (if (and (eq gnus-summary-make-false-root 'empty)
5255                         (memq number gnus-tmp-gathered)
5256                         gnus-tmp-prev-subject
5257                         (string= gnus-tmp-prev-subject simp-subject))
5258                    gnus-summary-same-subject
5259                  subject))
5260               (t gnus-summary-same-subject)))
5261             (if (and (eq gnus-summary-make-false-root 'adopt)
5262                      (= gnus-tmp-level 1)
5263                      (memq number gnus-tmp-gathered))
5264                 (setq gnus-tmp-opening-bracket ?\<
5265                       gnus-tmp-closing-bracket ?\>)
5266               (setq gnus-tmp-opening-bracket ?\[
5267                     gnus-tmp-closing-bracket ?\]))
5268             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5269                 (gnus-make-thread-indent-array
5270                  (max (* 2 (length gnus-thread-indent-array))
5271                       gnus-tmp-level)))
5272             (setq
5273              gnus-tmp-indentation
5274              (aref gnus-thread-indent-array gnus-tmp-level)
5275              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5276              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5277                                 gnus-summary-default-score 0)
5278              gnus-tmp-score-char
5279              (if (or (null gnus-summary-default-score)
5280                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5281                          gnus-summary-zcore-fuzz))
5282                  ?                      ;Whitespace
5283                (if (< gnus-tmp-score gnus-summary-default-score)
5284                    gnus-score-below-mark gnus-score-over-mark))
5285              gnus-tmp-replied
5286              (cond ((memq number gnus-newsgroup-processable)
5287                     gnus-process-mark)
5288                    ((memq number gnus-newsgroup-cached)
5289                     gnus-cached-mark)
5290                    ((memq number gnus-newsgroup-replied)
5291                     gnus-replied-mark)
5292                    ((memq number gnus-newsgroup-forwarded)
5293                     gnus-forwarded-mark)
5294                    ((memq number gnus-newsgroup-saved)
5295                     gnus-saved-mark)
5296                    ((memq number gnus-newsgroup-recent)
5297                     gnus-recent-mark)
5298                    ((memq number gnus-newsgroup-unseen)
5299                     gnus-unseen-mark)
5300                    (t gnus-no-mark))
5301              gnus-tmp-downloaded
5302              (cond ((memq number gnus-newsgroup-undownloaded)
5303                     gnus-undownloaded-mark)
5304                    (gnus-newsgroup-agentized
5305                     gnus-downloaded-mark)
5306                    (t
5307                     gnus-no-mark))
5308              gnus-tmp-from (mail-header-from gnus-tmp-header)
5309              gnus-tmp-name
5310              (cond
5311               ((string-match "<[^>]+> *$" gnus-tmp-from)
5312                (setq beg-match (match-beginning 0))
5313                (or (and (string-match "^\".+\"" gnus-tmp-from)
5314                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5315                    (substring gnus-tmp-from 0 beg-match)))
5316               ((string-match "(.+)" gnus-tmp-from)
5317                (substring gnus-tmp-from
5318                           (1+ (match-beginning 0)) (1- (match-end 0))))
5319               (t gnus-tmp-from))
5320
5321              ;; Do the %B string
5322              gnus-tmp-thread-tree-header-string
5323              (cond
5324               ((not gnus-show-threads) "")
5325               ((zerop gnus-tmp-level)
5326                (cond ((cdar thread)
5327                       (or gnus-sum-thread-tree-root subject))
5328                      (gnus-tmp-new-adopts
5329                       (or gnus-sum-thread-tree-false-root subject))
5330                      (t
5331                       (or gnus-sum-thread-tree-single-indent subject))))
5332               (t
5333                (concat (apply 'concat
5334                               (mapcar (lambda (item)
5335                                         (if (= item 1)
5336                                             gnus-sum-thread-tree-vertical
5337                                           gnus-sum-thread-tree-indent))
5338                                       (cdr (reverse tree-stack))))
5339                        (if (nth 1 thread)
5340                            gnus-sum-thread-tree-leaf-with-other
5341                          gnus-sum-thread-tree-single-leaf)))))
5342             (when (string= gnus-tmp-name "")
5343               (setq gnus-tmp-name gnus-tmp-from))
5344             (unless (numberp gnus-tmp-lines)
5345               (setq gnus-tmp-lines -1))
5346             (if (= gnus-tmp-lines -1)
5347                 (setq gnus-tmp-lines "?")
5348               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5349               (gnus-put-text-property
5350              (point)
5351              (progn (eval gnus-summary-line-format-spec) (point))
5352                'gnus-number number)
5353             (when gnus-visual-p
5354               (forward-line -1)
5355               (gnus-run-hooks 'gnus-summary-update-hook)
5356               (forward-line 1))
5357
5358             (setq gnus-tmp-prev-subject simp-subject)))
5359
5360         (when (nth 1 thread)
5361           (push (list (max 0 gnus-tmp-level)
5362                       (copy-sequence tree-stack)
5363                       (nthcdr 1 thread))
5364                 stack))
5365         (push (if (nth 1 thread) 1 0) tree-stack)
5366         (incf gnus-tmp-level)
5367         (setq threads (if thread-end nil (cdar thread)))
5368         (if gnus-summary-display-while-building
5369             (if building-count
5370                 (progn
5371                   ;; use a set frequency
5372                   (setq building-line-count (1- building-line-count))
5373                   (when (= building-line-count 0)
5374                     (sit-for 0)
5375                     (setq building-line-count
5376                           gnus-summary-display-while-building)))
5377               ;; always
5378               (sit-for 0)))
5379         (unless threads
5380           (setq gnus-tmp-level 0)))))
5381   (gnus-message 7 "Generating summary...done"))
5382
5383 (defun gnus-summary-prepare-unthreaded (headers)
5384   "Generate an unthreaded summary buffer based on HEADERS."
5385   (let (header number mark)
5386
5387     (beginning-of-line)
5388
5389     (while headers
5390       ;; We may have to root out some bad articles...
5391       (when (memq (setq number (mail-header-number
5392                                 (setq header (pop headers))))
5393                   gnus-newsgroup-limit)
5394         ;; Mark article as read when it has a low score.
5395         (when (and gnus-summary-mark-below
5396                    (< (or (cdr (assq number gnus-newsgroup-scored))
5397                           gnus-summary-default-score 0)
5398                       gnus-summary-mark-below)
5399                    (not (gnus-summary-article-ancient-p number)))
5400           (setq gnus-newsgroup-unreads
5401                 (delq number gnus-newsgroup-unreads))
5402           (if gnus-newsgroup-auto-expire
5403               (push number gnus-newsgroup-expirable)
5404             (push (cons number gnus-low-score-mark)
5405                   gnus-newsgroup-reads)))
5406
5407         (setq mark (gnus-article-mark number))
5408         (push (gnus-data-make number mark (1+ (point)) header 0)
5409               gnus-newsgroup-data)
5410         (gnus-summary-insert-line
5411          header 0 number
5412          (memq number gnus-newsgroup-undownloaded)
5413          mark (memq number gnus-newsgroup-replied)
5414          (memq number gnus-newsgroup-expirable)
5415          (mail-header-subject header) nil
5416          (cdr (assq number gnus-newsgroup-scored))
5417          (memq number gnus-newsgroup-processable))))))
5418
5419 (defun gnus-summary-remove-list-identifiers ()
5420   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5421   (let ((regexp (if (consp gnus-list-identifiers)
5422                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5423                   gnus-list-identifiers))
5424         changed subject)
5425     (when regexp
5426       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5427       (dolist (header gnus-newsgroup-headers)
5428         (setq subject (mail-header-subject header)
5429               changed nil)
5430         (while (string-match regexp subject)
5431           (setq subject
5432                 (concat (substring subject 0 (match-beginning 1))
5433                         (substring subject (match-end 0)))
5434                 changed t))
5435         (when changed
5436           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5437             (setq subject
5438                   (concat (substring subject 0 (match-beginning 1))
5439                           (substring subject (match-end 1)))))
5440           (mail-header-set-subject header subject))))))
5441
5442 (defun gnus-fetch-headers (articles)
5443   "Fetch headers of ARTICLES."
5444   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5445     (gnus-message 5 "Fetching headers for %s..." name)
5446     (prog1
5447         (if (eq 'nov
5448                 (setq gnus-headers-retrieved-by
5449                       (gnus-retrieve-headers
5450                        articles gnus-newsgroup-name
5451                        ;; We might want to fetch old headers, but
5452                        ;; not if there is only 1 article.
5453                        (and (or (and
5454                                  (not (eq gnus-fetch-old-headers 'some))
5455                                  (not (numberp gnus-fetch-old-headers)))
5456                                 (> (length articles) 1))
5457                             gnus-fetch-old-headers))))
5458             (gnus-get-newsgroup-headers-xover
5459              articles nil nil gnus-newsgroup-name t)
5460           (gnus-get-newsgroup-headers))
5461       (gnus-message 5 "Fetching headers for %s...done" name))))
5462
5463 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5464   "Select newsgroup GROUP.
5465 If READ-ALL is non-nil, all articles in the group are selected.
5466 If SELECT-ARTICLES, only select those articles from GROUP."
5467   (let* ((entry (gnus-group-entry group))
5468          ;;!!! Dirty hack; should be removed.
5469          (gnus-summary-ignore-duplicates
5470           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5471               t
5472             gnus-summary-ignore-duplicates))
5473          (info (nth 2 entry))
5474          charset articles fetched-articles cached)
5475
5476     (unless (gnus-check-server
5477              (set (make-local-variable 'gnus-current-select-method)
5478                   (gnus-find-method-for-group group)))
5479       (error "Couldn't open server"))
5480     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5481
5482     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5483         (gnus-activate-group group)     ; Or we can activate it...
5484         (progn                          ; Or we bug out.
5485           (when (equal major-mode 'gnus-summary-mode)
5486             (gnus-kill-buffer (current-buffer)))
5487           (error
5488            "Couldn't activate group %s: %s"
5489            (mm-decode-coding-string group charset)
5490            (mm-decode-coding-string (gnus-status-message group) charset))))
5491
5492     (unless (gnus-request-group group t)
5493         (when (equal major-mode 'gnus-summary-mode)
5494           (gnus-kill-buffer (current-buffer)))
5495         (error "Couldn't request group %s: %s"
5496                (mm-decode-coding-string group charset)
5497                (mm-decode-coding-string (gnus-status-message group) charset)))
5498
5499     (when gnus-agent
5500       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5501
5502       (setq gnus-summary-use-undownloaded-faces
5503             (gnus-agent-find-parameter
5504              group
5505              'agent-enable-undownloaded-faces)))
5506
5507     (setq gnus-newsgroup-name group
5508           gnus-newsgroup-unselected nil
5509           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5510
5511     (let ((display (gnus-group-find-parameter group 'display)))
5512       (setq gnus-newsgroup-display
5513             (cond
5514              ((not (zerop (or (car-safe read-all) 0)))
5515               ;; The user entered the group with C-u SPC/RET, let's show
5516               ;; all articles.
5517               'gnus-not-ignore)
5518              ((eq display 'all)
5519               'gnus-not-ignore)
5520              ((arrayp display)
5521               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5522              ((numberp display)
5523               ;; The following is probably the "correct" solution, but
5524               ;; it makes Gnus fetch all headers and then limit the
5525               ;; articles (which is slow), so instead we hack the
5526               ;; select-articles parameter instead. -- Simon Josefsson
5527               ;; <jas@kth.se>
5528               ;;
5529               ;; (gnus-byte-compile
5530               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5531               ;;                         display)))))
5532               (setq select-articles
5533                     (gnus-uncompress-range
5534                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5535                              (if (> tmp 0)
5536                                  tmp
5537                                1))
5538                            (cdr (gnus-active group)))))
5539               nil)
5540              (t
5541               nil))))
5542
5543     (gnus-summary-setup-default-charset)
5544
5545     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5546     (when (gnus-virtual-group-p group)
5547       (setq cached gnus-newsgroup-cached))
5548
5549     (setq gnus-newsgroup-unreads
5550           (gnus-sorted-ndifference
5551            (gnus-sorted-ndifference gnus-newsgroup-unreads
5552                                     gnus-newsgroup-marked)
5553            gnus-newsgroup-dormant))
5554
5555     (setq gnus-newsgroup-processable nil)
5556
5557     (gnus-update-read-articles group gnus-newsgroup-unreads)
5558
5559     ;; Adjust and set lists of article marks.
5560     (when info
5561       (gnus-adjust-marked-articles info))
5562     (if (setq articles select-articles)
5563         (setq gnus-newsgroup-unselected
5564               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5565       (setq articles (gnus-articles-to-read group read-all)))
5566
5567     (cond
5568      ((null articles)
5569       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5570       'quit)
5571      ((eq articles 0) nil)
5572      (t
5573       ;; Init the dependencies hash table.
5574       (setq gnus-newsgroup-dependencies
5575             (gnus-make-hashtable (length articles)))
5576       (gnus-set-global-variables)
5577       ;; Retrieve the headers and read them in.
5578
5579       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5580
5581       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5582       (when cached
5583         (setq gnus-newsgroup-cached cached))
5584
5585       ;; Suppress duplicates?
5586       (when gnus-suppress-duplicates
5587         (gnus-dup-suppress-articles))
5588
5589       ;; Set the initial limit.
5590       (setq gnus-newsgroup-limit (copy-sequence articles))
5591       ;; Remove canceled articles from the list of unread articles.
5592       (setq fetched-articles
5593             (mapcar (lambda (headers) (mail-header-number headers))
5594                     gnus-newsgroup-headers))
5595       (setq gnus-newsgroup-articles fetched-articles)
5596       (setq gnus-newsgroup-unreads
5597             (gnus-sorted-nintersection
5598              gnus-newsgroup-unreads fetched-articles))
5599       (gnus-compute-unseen-list)
5600
5601       ;; Removed marked articles that do not exist.
5602       (gnus-update-missing-marks
5603        (gnus-sorted-difference articles fetched-articles))
5604       ;; We might want to build some more threads first.
5605       (when (and gnus-fetch-old-headers
5606                  (eq gnus-headers-retrieved-by 'nov))
5607         (if (eq gnus-fetch-old-headers 'invisible)
5608             (gnus-build-all-threads)
5609           (gnus-build-old-threads)))
5610       ;; Let the Gnus agent mark articles as read.
5611       (when gnus-agent
5612         (gnus-agent-get-undownloaded-list))
5613       ;; Remove list identifiers from subject
5614       (when gnus-list-identifiers
5615         (gnus-summary-remove-list-identifiers))
5616       ;; Check whether auto-expire is to be done in this group.
5617       (setq gnus-newsgroup-auto-expire
5618             (gnus-group-auto-expirable-p group))
5619       ;; Set up the article buffer now, if necessary.
5620       (unless (and gnus-single-article-buffer
5621                    (equal gnus-article-buffer "*Article*"))
5622         (gnus-article-setup-buffer))
5623       ;; First and last article in this newsgroup.
5624       (when gnus-newsgroup-headers
5625         (setq gnus-newsgroup-begin
5626               (mail-header-number (car gnus-newsgroup-headers))
5627               gnus-newsgroup-end
5628               (mail-header-number
5629                (gnus-last-element gnus-newsgroup-headers))))
5630       ;; GROUP is successfully selected.
5631       (or gnus-newsgroup-headers t)))))
5632
5633 (defun gnus-compute-unseen-list ()
5634   ;; The `seen' marks are treated specially.
5635   (if (not gnus-newsgroup-seen)
5636       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5637     (setq gnus-newsgroup-unseen
5638           (gnus-inverse-list-range-intersection
5639            gnus-newsgroup-articles gnus-newsgroup-seen))))
5640
5641 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5642
5643 (defun gnus-summary-display-make-predicate (display)
5644   (require 'gnus-agent)
5645   (when (= (length display) 1)
5646     (setq display (car display)))
5647   (unless gnus-summary-display-cache
5648     (dolist (elem (append '((unread . unread)
5649                             (read . read)
5650                             (unseen . unseen))
5651                           gnus-article-mark-lists))
5652       (push (cons (cdr elem)
5653                   (gnus-byte-compile
5654                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5655             gnus-summary-display-cache)))
5656   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5657         (gnus-category-predicate-cache gnus-summary-display-cache))
5658     (gnus-get-predicate display)))
5659
5660 ;; Uses the dynamically bound `number' variable.
5661 (defvar number)
5662 (defun gnus-article-marked-p (type &optional article)
5663   (let ((article (or article number)))
5664     (cond
5665      ((eq type 'tick)
5666       (memq article gnus-newsgroup-marked))
5667      ((eq type 'spam)
5668       (memq article gnus-newsgroup-spam-marked))
5669      ((eq type 'unsend)
5670       (memq article gnus-newsgroup-unsendable))
5671      ((eq type 'undownload)
5672       (memq article gnus-newsgroup-undownloaded))
5673      ((eq type 'download)
5674       (memq article gnus-newsgroup-downloadable))
5675      ((eq type 'unread)
5676       (memq article gnus-newsgroup-unreads))
5677      ((eq type 'read)
5678       (memq article gnus-newsgroup-reads))
5679      ((eq type 'dormant)
5680       (memq article gnus-newsgroup-dormant) )
5681      ((eq type 'expire)
5682       (memq article gnus-newsgroup-expirable))
5683      ((eq type 'reply)
5684       (memq article gnus-newsgroup-replied))
5685      ((eq type 'killed)
5686       (memq article gnus-newsgroup-killed))
5687      ((eq type 'bookmark)
5688       (assq article gnus-newsgroup-bookmarks))
5689      ((eq type 'score)
5690       (assq article gnus-newsgroup-scored))
5691      ((eq type 'save)
5692       (memq article gnus-newsgroup-saved))
5693      ((eq type 'cache)
5694       (memq article gnus-newsgroup-cached))
5695      ((eq type 'forward)
5696       (memq article gnus-newsgroup-forwarded))
5697      ((eq type 'seen)
5698       (not (memq article gnus-newsgroup-unseen)))
5699      ((eq type 'recent)
5700       (memq article gnus-newsgroup-recent))
5701      (t t))))
5702
5703 (defun gnus-articles-to-read (group &optional read-all)
5704   "Find out what articles the user wants to read."
5705   (let* ((articles
5706           ;; Select all articles if `read-all' is non-nil, or if there
5707           ;; are no unread articles.
5708           (if (or read-all
5709                   (and (zerop (length gnus-newsgroup-marked))
5710                        (zerop (length gnus-newsgroup-unreads)))
5711                   ;; Fetch all if the predicate is non-nil.
5712                   gnus-newsgroup-display)
5713               ;; We want to select the headers for all the articles in
5714               ;; the group, so we select either all the active
5715               ;; articles in the group, or (if that's nil), the
5716               ;; articles in the cache.
5717               (or
5718                (if gnus-newsgroup-maximum-articles
5719                    (let ((active (gnus-active group)))
5720                      (gnus-uncompress-range
5721                       (cons (max (car active)
5722                                  (- (cdr active)
5723                                     gnus-newsgroup-maximum-articles
5724                                     -1))
5725                             (cdr active))))
5726                  (gnus-uncompress-range (gnus-active group)))
5727                (gnus-cache-articles-in-group group))
5728             ;; Select only the "normal" subset of articles.
5729             (gnus-sorted-nunion
5730              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5731              gnus-newsgroup-unreads)))
5732          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5733          (scored (length scored-list))
5734          (number (length articles))
5735          (marked (+ (length gnus-newsgroup-marked)
5736                     (length gnus-newsgroup-dormant)))
5737          (select
5738           (cond
5739            ((numberp read-all)
5740             read-all)
5741            ((numberp gnus-newsgroup-display)
5742             gnus-newsgroup-display)
5743            (t
5744             (condition-case ()
5745                 (cond
5746                  ((and (or (<= scored marked) (= scored number))
5747                        (numberp gnus-large-newsgroup)
5748                        (> number gnus-large-newsgroup))
5749                   (let* ((cursor-in-echo-area nil)
5750                          (initial (gnus-parameter-large-newsgroup-initial
5751                                    gnus-newsgroup-name))
5752                          (input
5753                           (read-string
5754                            (format
5755                             "How many articles from %s (%s %d): "
5756                             (gnus-group-decoded-name gnus-newsgroup-name)
5757                             (if initial "max" "default")
5758                             number)
5759                            (if initial
5760                                (cons (number-to-string initial)
5761                                      0)))))
5762                     (if (string-match "^[ \t]*$" input) number input)))
5763                  ((and (> scored marked) (< scored number)
5764                        (> (- scored number) 20))
5765                   (let ((input
5766                          (read-string
5767                           (format "%s %s (%d scored, %d total): "
5768                                   "How many articles from"
5769                                   (gnus-group-decoded-name group)
5770                                   scored number))))
5771                     (if (string-match "^[ \t]*$" input)
5772                         number input)))
5773                  (t number))
5774               (quit
5775                (message "Quit getting the articles to read")
5776                nil))))))
5777     (setq select (if (stringp select) (string-to-number select) select))
5778     (if (or (null select) (zerop select))
5779         select
5780       (if (and (not (zerop scored)) (<= (abs select) scored))
5781           (progn
5782             (setq articles (sort scored-list '<))
5783             (setq number (length articles)))
5784         (setq articles (copy-sequence articles)))
5785
5786       (when (< (abs select) number)
5787         (if (< select 0)
5788             ;; Select the N oldest articles.
5789             (setcdr (nthcdr (1- (abs select)) articles) nil)
5790           ;; Select the N most recent articles.
5791           (setq articles (nthcdr (- number select) articles))))
5792       (setq gnus-newsgroup-unselected
5793             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5794       (when gnus-alter-articles-to-read-function
5795         (setq articles
5796               (sort
5797                (funcall gnus-alter-articles-to-read-function
5798                         gnus-newsgroup-name articles)
5799                '<)))
5800       articles)))
5801
5802 (defun gnus-killed-articles (killed articles)
5803   (let (out)
5804     (while articles
5805       (when (inline (gnus-member-of-range (car articles) killed))
5806         (push (car articles) out))
5807       (setq articles (cdr articles)))
5808     out))
5809
5810 (defun gnus-uncompress-marks (marks)
5811   "Uncompress the mark ranges in MARKS."
5812   (let ((uncompressed '(score bookmark))
5813         out)
5814     (while marks
5815       (if (memq (caar marks) uncompressed)
5816           (push (car marks) out)
5817         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5818       (setq marks (cdr marks)))
5819     out))
5820
5821 (defun gnus-article-mark-to-type (mark)
5822   "Return the type of MARK."
5823   (or (cadr (assq mark gnus-article-special-mark-lists))
5824       'list))
5825
5826 (defun gnus-article-unpropagatable-p (mark)
5827   "Return whether MARK should be propagated to back end."
5828   (memq mark gnus-article-unpropagated-mark-lists))
5829
5830 (defun gnus-adjust-marked-articles (info)
5831   "Set all article lists and remove all marks that are no longer valid."
5832   (let* ((marked-lists (gnus-info-marks info))
5833          (active (gnus-active (gnus-info-group info)))
5834          (min (car active))
5835          (max (cdr active))
5836          (types gnus-article-mark-lists)
5837          marks var articles article mark mark-type
5838          bgn end)
5839
5840     (dolist (marks marked-lists)
5841       (setq mark (car marks)
5842             mark-type (gnus-article-mark-to-type mark)
5843             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5844
5845       ;; We set the variable according to the type of the marks list,
5846       ;; and then adjust the marks to a subset of the active articles.
5847       (cond
5848        ;; Adjust "simple" lists - compressed yet unsorted
5849        ((eq mark-type 'list)
5850         ;; Simultaneously uncompress and clip to active range
5851         ;; See gnus-uncompress-range for a description of possible marks
5852         (let (l lh)
5853           (if (not (cadr marks))
5854               (set var nil)
5855             (setq articles (if (numberp (cddr marks))
5856                                (list (cdr marks))
5857                              (cdr marks))
5858                   lh (cons nil nil)
5859                   l lh)
5860
5861             (while (setq article (pop articles))
5862               (cond ((consp article)
5863                      (setq bgn (max (car article) min)
5864                            end (min (cdr article) max))
5865                      (while (<= bgn end)
5866                        (setq l (setcdr l (cons bgn nil))
5867                              bgn (1+ bgn))))
5868                     ((and (<= min article)
5869                           (>= max article))
5870                      (setq l (setcdr l (cons article nil))))))
5871             (set var (cdr lh)))))
5872        ;; Adjust assocs.
5873        ((eq mark-type 'tuple)
5874         (set var (setq articles (cdr marks)))
5875         (when (not (listp (cdr (symbol-value var))))
5876           (set var (list (symbol-value var))))
5877         (when (not (listp (cdr articles)))
5878           (setq articles (list articles)))
5879         (while articles
5880           (when (or (not (consp (setq article (pop articles))))
5881                     (< (car article) min)
5882                     (> (car article) max))
5883             (set var (delq article (symbol-value var))))))
5884        ;; Adjust ranges (sloppily).
5885        ((eq mark-type 'range)
5886         (cond
5887          ((eq mark 'seen)
5888           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5889           ;; It should be (seen (NUM1 . NUM2)).
5890           (when (numberp (cddr marks))
5891             (setcdr marks (list (cdr marks))))
5892           (setq articles (cdr marks))
5893           (while (and articles
5894                       (or (and (consp (car articles))
5895                                (> min (cdar articles)))
5896                           (and (numberp (car articles))
5897                                (> min (car articles)))))
5898             (pop articles))
5899           (set var articles))))))))
5900
5901 (defun gnus-update-missing-marks (missing)
5902   "Go through the list of MISSING articles and remove them from the mark lists."
5903   (when missing
5904     (let (var m)
5905       ;; Go through all types.
5906       (dolist (elem gnus-article-mark-lists)
5907         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5908           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5909           (when (symbol-value var)
5910             ;; This list has articles.  So we delete all missing
5911             ;; articles from it.
5912             (setq m missing)
5913             (while m
5914               (set var (delq (pop m) (symbol-value var))))))))))
5915
5916 (defun gnus-update-marks ()
5917   "Enter the various lists of marked articles into the newsgroup info list."
5918   (let ((types gnus-article-mark-lists)
5919         (info (gnus-get-info gnus-newsgroup-name))
5920         type list newmarked symbol delta-marks)
5921     (when info
5922       ;; Add all marks lists to the list of marks lists.
5923       (while (setq type (pop types))
5924         (setq list (symbol-value
5925                     (setq symbol
5926                           (intern (format "gnus-newsgroup-%s" (car type))))))
5927
5928         (when list
5929           ;; Get rid of the entries of the articles that have the
5930           ;; default score.
5931           (when (and (eq (cdr type) 'score)
5932                      gnus-save-score
5933                      list)
5934             (let* ((arts list)
5935                    (prev (cons nil list))
5936                    (all prev))
5937               (while arts
5938                 (if (or (not (consp (car arts)))
5939                         (= (cdar arts) gnus-summary-default-score))
5940                     (setcdr prev (cdr arts))
5941                   (setq prev arts))
5942                 (setq arts (cdr arts)))
5943               (setq list (cdr all)))))
5944
5945         (when (eq (cdr type) 'seen)
5946           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5947
5948         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5949           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5950
5951         (when (and (gnus-check-backend-function
5952                     'request-set-mark gnus-newsgroup-name)
5953                    (not (gnus-article-unpropagatable-p (cdr type))))
5954           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5955                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5956                  (add (gnus-remove-from-range
5957                        (gnus-copy-sequence list) old)))
5958             (when add
5959               (push (list add 'add (list (cdr type))) delta-marks))
5960             (when del
5961               (push (list del 'del (list (cdr type))) delta-marks))))
5962
5963         (when list
5964           (push (cons (cdr type) list) newmarked)))
5965
5966       (when delta-marks
5967         (unless (gnus-check-group gnus-newsgroup-name)
5968           (error "Can't open server for %s" gnus-newsgroup-name))
5969         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5970
5971       ;; Enter these new marks into the info of the group.
5972       (if (nthcdr 3 info)
5973           (setcar (nthcdr 3 info) newmarked)
5974         ;; Add the marks lists to the end of the info.
5975         (when newmarked
5976           (setcdr (nthcdr 2 info) (list newmarked))))
5977
5978       ;; Cut off the end of the info if there's nothing else there.
5979       (let ((i 5))
5980         (while (and (> i 2)
5981                     (not (nth i info)))
5982           (when (nthcdr (decf i) info)
5983             (setcdr (nthcdr i info) nil)))))))
5984
5985 (defun gnus-set-mode-line (where)
5986   "Set the mode line of the article or summary buffers.
5987 If WHERE is `summary', the summary mode line format will be used."
5988   ;; Is this mode line one we keep updated?
5989   (when (and (memq where gnus-updated-mode-lines)
5990              (symbol-value
5991               (intern (format "gnus-%s-mode-line-format-spec" where))))
5992     (let (mode-string)
5993       ;; We evaluate this in the summary buffer since these
5994       ;; variables are buffer-local to that buffer.
5995       (with-current-buffer gnus-summary-buffer
5996         ;; We bind all these variables that are used in the `eval' form
5997         ;; below.
5998         (let* ((mformat (symbol-value
5999                          (intern
6000                           (format "gnus-%s-mode-line-format-spec" where))))
6001                (gnus-tmp-group-name (gnus-mode-string-quote
6002                                      (gnus-group-decoded-name
6003                                       gnus-newsgroup-name)))
6004                (gnus-tmp-article-number (or gnus-current-article 0))
6005                (gnus-tmp-unread gnus-newsgroup-unreads)
6006                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6007                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6008                (gnus-tmp-unread-and-unselected
6009                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6010                             (zerop gnus-tmp-unselected))
6011                        "")
6012                       ((zerop gnus-tmp-unselected)
6013                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6014                       (t (format "{%d(+%d) more}"
6015                                  gnus-tmp-unread-and-unticked
6016                                  gnus-tmp-unselected))))
6017                (gnus-tmp-subject
6018                 (if (and gnus-current-headers
6019                          (vectorp gnus-current-headers))
6020                     (gnus-mode-string-quote
6021                      (mail-header-subject gnus-current-headers))
6022                   ""))
6023                bufname-length max-len
6024                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6025           (setq mode-string (eval mformat))
6026           (setq bufname-length (if (string-match "%b" mode-string)
6027                                    (- (length
6028                                        (buffer-name
6029                                         (if (eq where 'summary)
6030                                             nil
6031                                           (get-buffer gnus-article-buffer))))
6032                                       2)
6033                                  0))
6034           (setq max-len (max 4 (if gnus-mode-non-string-length
6035                                    (- (window-width)
6036                                       gnus-mode-non-string-length
6037                                       bufname-length)
6038                                  (length mode-string))))
6039           ;; We might have to chop a bit of the string off...
6040           (when (> (length mode-string) max-len)
6041             (setq mode-string
6042                   (concat (truncate-string-to-width mode-string (- max-len 3))
6043                           "...")))
6044           ;; Pad the mode string a bit.
6045           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
6046       ;; Update the mode line.
6047       (setq mode-line-buffer-identification
6048             (gnus-mode-line-buffer-identification (list mode-string)))
6049       (set-buffer-modified-p t))))
6050
6051 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6052   "Go through the HEADERS list and add all Xrefs to a hash table.
6053 The resulting hash table is returned, or nil if no Xrefs were found."
6054   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6055          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6056          (xref-hashtb (gnus-make-hashtable))
6057          start group entry number xrefs header)
6058     (while headers
6059       (setq header (pop headers))
6060       (when (and (setq xrefs (mail-header-xref header))
6061                  (not (memq (setq number (mail-header-number header))
6062                             unreads)))
6063         (setq start 0)
6064         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6065           (setq start (match-end 0))
6066           (setq group (if prefix
6067                           (concat prefix (substring xrefs (match-beginning 1)
6068                                                     (match-end 1)))
6069                         (substring xrefs (match-beginning 1) (match-end 1))))
6070           (setq number
6071                 (string-to-number (substring xrefs (match-beginning 2)
6072                                           (match-end 2))))
6073           (if (setq entry (gnus-gethash group xref-hashtb))
6074               (setcdr entry (cons number (cdr entry)))
6075             (gnus-sethash group (cons number nil) xref-hashtb)))))
6076     (and start xref-hashtb)))
6077
6078 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6079   "Look through all the headers and mark the Xrefs as read."
6080   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6081         name info xref-hashtb idlist method nth4)
6082     (save-excursion
6083       (set-buffer gnus-group-buffer)
6084       (when (setq xref-hashtb
6085                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6086         (mapatoms
6087          (lambda (group)
6088            (unless (string= from-newsgroup (setq name (symbol-name group)))
6089              (setq idlist (symbol-value group))
6090              ;; Dead groups are not updated.
6091              (and (prog1
6092                       (setq info (gnus-get-info name))
6093                     (when (stringp (setq nth4 (gnus-info-method info)))
6094                       (setq nth4 (gnus-server-to-method nth4))))
6095                   ;; Only do the xrefs if the group has the same
6096                   ;; select method as the group we have just read.
6097                   (or (gnus-methods-equal-p
6098                        nth4 (gnus-find-method-for-group from-newsgroup))
6099                       virtual
6100                       (equal nth4 (setq method (gnus-find-method-for-group
6101                                                 from-newsgroup)))
6102                       (and (equal (car nth4) (car method))
6103                            (equal (nth 1 nth4) (nth 1 method))))
6104                   gnus-use-cross-reference
6105                   (or (not (eq gnus-use-cross-reference t))
6106                       virtual
6107                       ;; Only do cross-references on subscribed
6108                       ;; groups, if that is what is wanted.
6109                       (<= (gnus-info-level info) gnus-level-subscribed))
6110                   (gnus-group-make-articles-read name idlist))))
6111          xref-hashtb)))))
6112
6113 (defun gnus-compute-read-articles (group articles)
6114   (let* ((entry (gnus-group-entry group))
6115          (info (nth 2 entry))
6116          (active (gnus-active group))
6117          ninfo)
6118     (when entry
6119       ;; First peel off all invalid article numbers.
6120       (when active
6121         (let ((ids articles)
6122               id first)
6123           (while (setq id (pop ids))
6124             (when (and first (> id (cdr active)))
6125               ;; We'll end up in this situation in one particular
6126               ;; obscure situation.  If you re-scan a group and get
6127               ;; a new article that is cross-posted to a different
6128               ;; group that has not been re-scanned, you might get
6129               ;; crossposted article that has a higher number than
6130               ;; Gnus believes possible.  So we re-activate this
6131               ;; group as well.  This might mean doing the
6132               ;; crossposting thingy will *increase* the number
6133               ;; of articles in some groups.  Tsk, tsk.
6134               (setq active (or (gnus-activate-group group) active)))
6135             (when (or (> id (cdr active))
6136                       (< id (car active)))
6137               (setq articles (delq id articles))))))
6138       ;; If the read list is nil, we init it.
6139       (if (and active
6140                (null (gnus-info-read info))
6141                (> (car active) 1))
6142           (setq ninfo (cons 1 (1- (car active))))
6143         (setq ninfo (gnus-info-read info)))
6144       ;; Then we add the read articles to the range.
6145       (gnus-add-to-range
6146        ninfo (setq articles (sort articles '<))))))
6147
6148 (defun gnus-group-make-articles-read (group articles)
6149   "Update the info of GROUP to say that ARTICLES are read."
6150   (let* ((num 0)
6151          (entry (gnus-group-entry group))
6152          (info (nth 2 entry))
6153          (active (gnus-active group))
6154          range)
6155     (when entry
6156       (setq range (gnus-compute-read-articles group articles))
6157       (with-current-buffer gnus-group-buffer
6158         (gnus-undo-register
6159           `(progn
6160              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6161              (gnus-info-set-read ',info ',(gnus-info-read info))
6162              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6163              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6164              (gnus-group-update-group ,group t))))
6165       ;; Add the read articles to the range.
6166       (gnus-info-set-read info range)
6167       (gnus-request-set-mark group (list (list range 'add '(read))))
6168       ;; Then we have to re-compute how many unread
6169       ;; articles there are in this group.
6170       (when active
6171         (cond
6172          ((not range)
6173           (setq num (- (1+ (cdr active)) (car active))))
6174          ((not (listp (cdr range)))
6175           (setq num (- (cdr active) (- (1+ (cdr range))
6176                                        (car range)))))
6177          (t
6178           (while range
6179             (if (numberp (car range))
6180                 (setq num (1+ num))
6181               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6182             (setq range (cdr range)))
6183           (setq num (- (cdr active) num))))
6184         ;; Update the number of unread articles.
6185         (setcar entry num)
6186         ;; Update the group buffer.
6187         (unless (gnus-ephemeral-group-p group)
6188           (gnus-group-update-group group t))))))
6189
6190 (defvar gnus-newsgroup-none-id 0)
6191
6192 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6193   (let ((cur nntp-server-buffer)
6194         (dependencies
6195          (or dependencies
6196              (with-current-buffer gnus-summary-buffer
6197                gnus-newsgroup-dependencies)))
6198         headers id end ref number
6199         (mail-parse-charset gnus-newsgroup-charset)
6200         (mail-parse-ignored-charsets
6201          (save-current-buffer (condition-case nil
6202                                   (set-buffer gnus-summary-buffer)
6203                                 (error))
6204                               gnus-newsgroup-ignored-charsets)))
6205     (with-current-buffer nntp-server-buffer
6206       ;; Translate all TAB characters into SPACE characters.
6207       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6208       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6209       (ietf-drums-unfold-fws)
6210       (gnus-run-hooks 'gnus-parse-headers-hook)
6211       (let ((case-fold-search t)
6212             in-reply-to header p lines chars)
6213         (goto-char (point-min))
6214         ;; Search to the beginning of the next header.  Error messages
6215         ;; do not begin with 2 or 3.
6216         (while (re-search-forward "^[23][0-9]+ " nil t)
6217           (setq id nil
6218                 ref nil)
6219           ;; This implementation of this function, with nine
6220           ;; search-forwards instead of the one re-search-forward and
6221           ;; a case (which basically was the old function) is actually
6222           ;; about twice as fast, even though it looks messier.  You
6223           ;; can't have everything, I guess.  Speed and elegance
6224           ;; doesn't always go hand in hand.
6225           (setq
6226            header
6227            (vector
6228             ;; Number.
6229             (prog1
6230                 (setq number (read cur))
6231               (end-of-line)
6232               (setq p (point))
6233               (narrow-to-region (point)
6234                                 (or (and (search-forward "\n.\n" nil t)
6235                                          (- (point) 2))
6236                                     (point))))
6237             ;; Subject.
6238             (progn
6239               (goto-char p)
6240               (if (search-forward "\nsubject:" nil t)
6241                   (funcall gnus-decode-encoded-word-function
6242                            (nnheader-header-value))
6243                 "(none)"))
6244             ;; From.
6245             (progn
6246               (goto-char p)
6247               (if (search-forward "\nfrom:" nil t)
6248                   (funcall gnus-decode-encoded-address-function
6249                            (nnheader-header-value))
6250                 "(nobody)"))
6251             ;; Date.
6252             (progn
6253               (goto-char p)
6254               (if (search-forward "\ndate:" nil t)
6255                   (nnheader-header-value) ""))
6256             ;; Message-ID.
6257             (progn
6258               (goto-char p)
6259               (setq id (if (re-search-forward
6260                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6261                            ;; We do it this way to make sure the Message-ID
6262                            ;; is (somewhat) syntactically valid.
6263                            (buffer-substring (match-beginning 1)
6264                                              (match-end 1))
6265                          ;; If there was no message-id, we just fake one
6266                          ;; to make subsequent routines simpler.
6267                          (nnheader-generate-fake-message-id number))))
6268             ;; References.
6269             (progn
6270               (goto-char p)
6271               (if (search-forward "\nreferences:" nil t)
6272                   (progn
6273                     (setq end (point))
6274                     (prog1
6275                         (nnheader-header-value)
6276                       (setq ref
6277                             (buffer-substring
6278                              (progn
6279                                (end-of-line)
6280                                (search-backward ">" end t)
6281                                (1+ (point)))
6282                              (progn
6283                                (search-backward "<" end t)
6284                                (point))))))
6285                 ;; Get the references from the in-reply-to header if there
6286                 ;; were no references and the in-reply-to header looks
6287                 ;; promising.
6288                 (if (and (search-forward "\nin-reply-to:" nil t)
6289                          (setq in-reply-to (nnheader-header-value))
6290                          (string-match "<[^>]+>" in-reply-to))
6291                     (let (ref2)
6292                       (setq ref (substring in-reply-to (match-beginning 0)
6293                                            (match-end 0)))
6294                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6295                         (setq ref2 (substring in-reply-to (match-beginning 0)
6296                                               (match-end 0)))
6297                         (when (> (length ref2) (length ref))
6298                           (setq ref ref2)))
6299                       ref)
6300                   (setq ref nil))))
6301             ;; Chars.
6302             (progn
6303               (goto-char p)
6304               (if (search-forward "\nchars: " nil t)
6305                   (if (numberp (setq chars (ignore-errors (read cur))))
6306                       chars -1)
6307                 -1))
6308             ;; Lines.
6309             (progn
6310               (goto-char p)
6311               (if (search-forward "\nlines: " nil t)
6312                   (if (numberp (setq lines (ignore-errors (read cur))))
6313                       lines -1)
6314                 -1))
6315             ;; Xref.
6316             (progn
6317               (goto-char p)
6318               (and (search-forward "\nxref:" nil t)
6319                    (nnheader-header-value)))
6320             ;; Extra.
6321             (when gnus-extra-headers
6322               (let ((extra gnus-extra-headers)
6323                     out)
6324                 (while extra
6325                   (goto-char p)
6326                   (when (search-forward
6327                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6328                     (push (cons (car extra) (nnheader-header-value))
6329                           out))
6330                   (pop extra))
6331                 out))))
6332           (when (equal id ref)
6333             (setq ref nil))
6334
6335           (when gnus-alter-header-function
6336             (funcall gnus-alter-header-function header)
6337             (setq id (mail-header-id header)
6338                   ref (gnus-parent-id (mail-header-references header))))
6339
6340           (when (setq header
6341                       (gnus-dependencies-add-header
6342                        header dependencies force-new))
6343             (push header headers))
6344           (goto-char (point-max))
6345           (widen))
6346         (nreverse headers)))))
6347
6348 ;; Goes through the xover lines and returns a list of vectors
6349 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6350                                                   force-new dependencies
6351                                                   group also-fetch-heads)
6352   "Parse the news overview data in the server buffer.
6353 Return a list of headers that match SEQUENCE (see
6354 `nntp-retrieve-headers')."
6355   ;; Get the Xref when the users reads the articles since most/some
6356   ;; NNTP servers do not include Xrefs when using XOVER.
6357   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6358   (let ((mail-parse-charset gnus-newsgroup-charset)
6359         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6360         (cur nntp-server-buffer)
6361         (dependencies (or dependencies gnus-newsgroup-dependencies))
6362         (allp (cond
6363                ((eq gnus-read-all-available-headers t)
6364                 t)
6365                ((and (stringp gnus-read-all-available-headers)
6366                      group)
6367                 (string-match gnus-read-all-available-headers group))
6368                (t
6369                 nil)))
6370         number headers header)
6371     (with-current-buffer nntp-server-buffer
6372       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6373       ;; Allow the user to mangle the headers before parsing them.
6374       (gnus-run-hooks 'gnus-parse-headers-hook)
6375       (goto-char (point-min))
6376       (gnus-parse-without-error
6377         (while (and (or sequence allp)
6378                     (not (eobp)))
6379           (setq number (read cur))
6380           (when (not allp)
6381             (while (and sequence
6382                         (< (car sequence) number))
6383               (setq sequence (cdr sequence))))
6384           (when (and (or allp
6385                          (and sequence
6386                               (eq number (car sequence))))
6387                      (progn
6388                        (setq sequence (cdr sequence))
6389                        (setq header (inline
6390                                       (gnus-nov-parse-line
6391                                        number dependencies force-new)))))
6392             (push header headers))
6393           (forward-line 1)))
6394       ;; A common bug in inn is that if you have posted an article and
6395       ;; then retrieves the active file, it will answer correctly --
6396       ;; the new article is included.  However, a NOV entry for the
6397       ;; article may not have been generated yet, so this may fail.
6398       ;; We work around this problem by retrieving the last few
6399       ;; headers using HEAD.
6400       (if (or (not also-fetch-heads)
6401               (not sequence))
6402           ;; We (probably) got all the headers.
6403           (nreverse headers)
6404         (let ((gnus-nov-is-evil t))
6405           (nconc
6406            (nreverse headers)
6407            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6408              (gnus-get-newsgroup-headers))))))))
6409
6410 (defun gnus-article-get-xrefs ()
6411   "Fill in the Xref value in `gnus-current-headers', if necessary.
6412 This is meant to be called in `gnus-article-internal-prepare-hook'."
6413   (let ((headers (with-current-buffer gnus-summary-buffer
6414                    gnus-current-headers)))
6415     (or (not gnus-use-cross-reference)
6416         (not headers)
6417         (and (mail-header-xref headers)
6418              (not (string= (mail-header-xref headers) "")))
6419         (let ((case-fold-search t)
6420               xref)
6421           (save-restriction
6422             (nnheader-narrow-to-headers)
6423             (goto-char (point-min))
6424             (when (or (and (not (eobp))
6425                            (eq (downcase (char-after)) ?x)
6426                            (looking-at "Xref:"))
6427                       (search-forward "\nXref:" nil t))
6428               (goto-char (1+ (match-end 0)))
6429               (setq xref (buffer-substring (point) (point-at-eol)))
6430               (mail-header-set-xref headers xref)))))))
6431
6432 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6433   "Find article ID and insert the summary line for that article.
6434 OLD-HEADER can either be a header or a line number to insert
6435 the subject line on."
6436   (let* ((line (and (numberp old-header) old-header))
6437          (old-header (and (vectorp old-header) old-header))
6438          (header (cond ((and old-header use-old-header)
6439                         old-header)
6440                        ((and (numberp id)
6441                              (gnus-number-to-header id))
6442                         (gnus-number-to-header id))
6443                        (t
6444                         (gnus-read-header id))))
6445          (number (and (numberp id) id))
6446          d)
6447     (when header
6448       ;; Rebuild the thread that this article is part of and go to the
6449       ;; article we have fetched.
6450       (when (and (not gnus-show-threads)
6451                  old-header)
6452         (when (and number
6453                    (setq d (gnus-data-find (mail-header-number old-header))))
6454           (goto-char (gnus-data-pos d))
6455           (gnus-data-remove
6456            number
6457            (- (point-at-bol)
6458               (prog1
6459                   (1+ (point-at-eol))
6460                 (gnus-delete-line))))))
6461       ;; Remove list identifiers from subject.
6462       (when gnus-list-identifiers
6463         (let ((gnus-newsgroup-headers (list header)))
6464           (gnus-summary-remove-list-identifiers)))
6465       (when old-header
6466         (mail-header-set-number header (mail-header-number old-header)))
6467       (setq gnus-newsgroup-sparse
6468             (delq (setq number (mail-header-number header))
6469                   gnus-newsgroup-sparse))
6470       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6471       (push number gnus-newsgroup-limit)
6472       (gnus-rebuild-thread (mail-header-id header) line)
6473       (gnus-summary-goto-subject number nil t))
6474     (when (and (numberp number)
6475                (> number 0))
6476       ;; We have to update the boundaries even if we can't fetch the
6477       ;; article if ID is a number -- so that the next `P' or `N'
6478       ;; command will fetch the previous (or next) article even
6479       ;; if the one we tried to fetch this time has been canceled.
6480       (when (> number gnus-newsgroup-end)
6481         (setq gnus-newsgroup-end number))
6482       (when (< number gnus-newsgroup-begin)
6483         (setq gnus-newsgroup-begin number))
6484       (setq gnus-newsgroup-unselected
6485             (delq number gnus-newsgroup-unselected)))
6486     ;; Report back a success?
6487     (and header (mail-header-number header))))
6488
6489 ;;; Process/prefix in the summary buffer
6490
6491 (defun gnus-summary-work-articles (n)
6492   "Return a list of articles to be worked upon.
6493 The prefix argument, the list of process marked articles, and the
6494 current article will be taken into consideration."
6495   (with-current-buffer gnus-summary-buffer
6496     (cond
6497      (n
6498       ;; A numerical prefix has been given.
6499       (setq n (prefix-numeric-value n))
6500       (let ((backward (< n 0))
6501             (n (abs (prefix-numeric-value n)))
6502             articles article)
6503         (save-excursion
6504           (while
6505               (and (> n 0)
6506                    (push (setq article (gnus-summary-article-number))
6507                          articles)
6508                    (if backward
6509                        (gnus-summary-find-prev nil article)
6510                      (gnus-summary-find-next nil article)))
6511             (decf n)))
6512         (nreverse articles)))
6513      ((and (gnus-region-active-p) (mark))
6514       (message "region active")
6515       ;; Work on the region between point and mark.
6516       (let ((max (max (point) (mark)))
6517             articles article)
6518         (save-excursion
6519           (goto-char (min (point) (mark)))
6520           (while
6521               (and
6522                (push (setq article (gnus-summary-article-number)) articles)
6523                (gnus-summary-find-next nil article)
6524                (< (point) max)))
6525           (nreverse articles))))
6526      (gnus-newsgroup-processable
6527       ;; There are process-marked articles present.
6528       ;; Save current state.
6529       (gnus-summary-save-process-mark)
6530       ;; Return the list.
6531       (reverse gnus-newsgroup-processable))
6532      (t
6533       ;; Just return the current article.
6534       (list (gnus-summary-article-number))))))
6535
6536 (defmacro gnus-summary-iterate (arg &rest forms)
6537   "Iterate over the process/prefixed articles and do FORMS.
6538 ARG is the interactive prefix given to the command.  FORMS will be
6539 executed with point over the summary line of the articles."
6540   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6541     `(let ((,articles (gnus-summary-work-articles ,arg)))
6542        (while ,articles
6543          (gnus-summary-goto-subject (car ,articles))
6544          ,@forms
6545          (pop ,articles)))))
6546
6547 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6548 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6549
6550 (defun gnus-summary-save-process-mark ()
6551   "Push the current set of process marked articles on the stack."
6552   (interactive)
6553   (push (copy-sequence gnus-newsgroup-processable)
6554         gnus-newsgroup-process-stack))
6555
6556 (defun gnus-summary-kill-process-mark ()
6557   "Push the current set of process marked articles on the stack and unmark."
6558   (interactive)
6559   (gnus-summary-save-process-mark)
6560   (gnus-summary-unmark-all-processable))
6561
6562 (defun gnus-summary-yank-process-mark ()
6563   "Pop the last process mark state off the stack and restore it."
6564   (interactive)
6565   (unless gnus-newsgroup-process-stack
6566     (error "Empty mark stack"))
6567   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6568
6569 (defun gnus-summary-process-mark-set (set)
6570   "Make SET into the current process marked articles."
6571   (gnus-summary-unmark-all-processable)
6572   (mapc 'gnus-summary-set-process-mark set))
6573
6574 ;;; Searching and stuff
6575
6576 (defun gnus-summary-search-group (&optional backward use-level)
6577   "Search for next unread newsgroup.
6578 If optional argument BACKWARD is non-nil, search backward instead."
6579   (with-current-buffer gnus-group-buffer
6580     (when (gnus-group-search-forward
6581            backward nil (if use-level (gnus-group-group-level) nil))
6582       (gnus-group-group-name))))
6583
6584 (defun gnus-summary-best-group (&optional exclude-group)
6585   "Find the name of the best unread group.
6586 If EXCLUDE-GROUP, do not go to this group."
6587   (with-current-buffer gnus-group-buffer
6588     (save-excursion
6589       (gnus-group-best-unread-group exclude-group))))
6590
6591 (defun gnus-summary-find-next (&optional unread article backward)
6592   (if backward
6593       (gnus-summary-find-prev unread article)
6594     (let* ((dummy (gnus-summary-article-intangible-p))
6595            (article (or article (gnus-summary-article-number)))
6596            (data (gnus-data-find-list article))
6597            result)
6598       (when (and (not dummy)
6599                  (or (not gnus-summary-check-current)
6600                      (not unread)
6601                      (not (gnus-data-unread-p (car data)))))
6602         (setq data (cdr data)))
6603       (when (setq result
6604                   (if unread
6605                       (progn
6606                         (while data
6607                           (unless (memq (gnus-data-number (car data))
6608                                         (cond
6609                                          ((eq gnus-auto-goto-ignores
6610                                               'always-undownloaded)
6611                                           gnus-newsgroup-undownloaded)
6612                                          (gnus-plugged
6613                                           nil)
6614                                          ((eq gnus-auto-goto-ignores
6615                                               'unfetched)
6616                                           gnus-newsgroup-unfetched)
6617                                          ((eq gnus-auto-goto-ignores
6618                                               'undownloaded)
6619                                           gnus-newsgroup-undownloaded)))
6620                             (when (gnus-data-unread-p (car data))
6621                               (setq result (car data)
6622                                     data nil)))
6623                           (setq data (cdr data)))
6624                         result)
6625                     (car data)))
6626         (goto-char (gnus-data-pos result))
6627         (gnus-data-number result)))))
6628
6629 (defun gnus-summary-find-prev (&optional unread article)
6630   (let* ((eobp (eobp))
6631          (article (or article (gnus-summary-article-number)))
6632          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6633          result)
6634     (when (and (not eobp)
6635                (or (not gnus-summary-check-current)
6636                    (not unread)
6637                    (not (gnus-data-unread-p (car data)))))
6638       (setq data (cdr data)))
6639     (when (setq result
6640                 (if unread
6641                     (progn
6642                       (while data
6643                         (unless (memq (gnus-data-number (car data))
6644                                       (cond
6645                                        ((eq gnus-auto-goto-ignores
6646                                             'always-undownloaded)
6647                                         gnus-newsgroup-undownloaded)
6648                                        (gnus-plugged
6649                                         nil)
6650                                        ((eq gnus-auto-goto-ignores
6651                                             'unfetched)
6652                                         gnus-newsgroup-unfetched)
6653                                        ((eq gnus-auto-goto-ignores
6654                                             'undownloaded)
6655                                         gnus-newsgroup-undownloaded)))
6656                           (when (gnus-data-unread-p (car data))
6657                             (setq result (car data)
6658                                   data nil)))
6659                         (setq data (cdr data)))
6660                       result)
6661                   (car data)))
6662       (goto-char (gnus-data-pos result))
6663       (gnus-data-number result))))
6664
6665 (defun gnus-summary-find-subject (subject &optional unread backward article)
6666   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6667          (article (or article (gnus-summary-article-number)))
6668          (articles (gnus-data-list backward))
6669          (arts (gnus-data-find-list article articles))
6670          result)
6671     (when (or (not gnus-summary-check-current)
6672               (not unread)
6673               (not (gnus-data-unread-p (car arts))))
6674       (setq arts (cdr arts)))
6675     (while arts
6676       (and (or (not unread)
6677                (gnus-data-unread-p (car arts)))
6678            (vectorp (gnus-data-header (car arts)))
6679            (gnus-subject-equal
6680             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6681            (setq result (car arts)
6682                  arts nil))
6683       (setq arts (cdr arts)))
6684     (and result
6685          (goto-char (gnus-data-pos result))
6686          (gnus-data-number result))))
6687
6688 (defun gnus-summary-search-forward (&optional unread subject backward)
6689   "Search forward for an article.
6690 If UNREAD, look for unread articles.  If SUBJECT, look for
6691 articles with that subject.  If BACKWARD, search backward instead."
6692   (cond (subject (gnus-summary-find-subject subject unread backward))
6693         (backward (gnus-summary-find-prev unread))
6694         (t (gnus-summary-find-next unread))))
6695
6696 (defun gnus-recenter (&optional n)
6697   "Center point in window and redisplay frame.
6698 Also do horizontal recentering."
6699   (interactive "P")
6700   (when (and gnus-auto-center-summary
6701              (not (eq gnus-auto-center-summary 'vertical)))
6702     (gnus-horizontal-recenter))
6703   (recenter n))
6704
6705 (defun gnus-summary-recenter ()
6706   "Center point in the summary window.
6707 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6708 displayed, no centering will be performed."
6709   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6710   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6711   (interactive)
6712   ;; The user has to want it.
6713   (when gnus-auto-center-summary
6714     (let* ((top (cond ((< (window-height) 4) 0)
6715                       ((< (window-height) 7) 1)
6716                       (t (if (numberp gnus-auto-center-summary)
6717                              gnus-auto-center-summary
6718                            (/ (1- (window-height)) 2)))))
6719            (height (1- (window-height)))
6720            (bottom (save-excursion (goto-char (point-max))
6721                                    (forward-line (- height))
6722                                    (point)))
6723            (window (get-buffer-window (current-buffer))))
6724       (when (get-buffer-window gnus-article-buffer)
6725         ;; Only do recentering when the article buffer is displayed,
6726         ;; Set the window start to either `bottom', which is the biggest
6727         ;; possible valid number, or the second line from the top,
6728         ;; whichever is the least.
6729         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6730           (if (> bottom top-pos)
6731               ;; Keep the second line from the top visible
6732               (set-window-start window top-pos)
6733             ;; Try to keep the bottom line visible; if it's partially
6734             ;; obscured, either scroll one more line to make it fully
6735             ;; visible, or revert to using TOP-POS.
6736             (save-excursion
6737               (goto-char (point-max))
6738               (forward-line -1)
6739               (let ((last-line-start (point)))
6740                 (goto-char bottom)
6741                 (set-window-start window (point) t)
6742                 (when (not (pos-visible-in-window-p last-line-start window))
6743                   (forward-line 1)
6744                   (set-window-start window (min (point) top-pos) t)))))))
6745       ;; Do horizontal recentering while we're at it.
6746       (when (and (get-buffer-window (current-buffer) t)
6747                  (not (eq gnus-auto-center-summary 'vertical)))
6748         (let ((selected (selected-window)))
6749           (select-window (get-buffer-window (current-buffer) t))
6750           (gnus-summary-position-point)
6751           (gnus-horizontal-recenter)
6752           (select-window selected))))))
6753
6754 (defun gnus-summary-jump-to-group (newsgroup)
6755   "Move point to NEWSGROUP in group mode buffer."
6756   ;; Keep update point of group mode buffer if visible.
6757   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6758       (save-window-excursion
6759         ;; Take care of tree window mode.
6760         (when (get-buffer-window gnus-group-buffer)
6761           (pop-to-buffer gnus-group-buffer))
6762         (gnus-group-jump-to-group newsgroup))
6763     (save-excursion
6764       ;; Take care of tree window mode.
6765       (if (get-buffer-window gnus-group-buffer 0)
6766           (pop-to-buffer gnus-group-buffer)
6767         (set-buffer gnus-group-buffer))
6768       (gnus-group-jump-to-group newsgroup))))
6769
6770 ;; This function returns a list of article numbers based on the
6771 ;; difference between the ranges of read articles in this group and
6772 ;; the range of active articles.
6773 (defun gnus-list-of-unread-articles (group)
6774   (let* ((read (gnus-info-read (gnus-get-info group)))
6775          (active (or (gnus-active group) (gnus-activate-group group)))
6776          (last (or (cdr active)
6777                    (error "Group %s couldn't be activated " group)))
6778          (bottom (if gnus-newsgroup-maximum-articles
6779                      (max (car active)
6780                           (- last gnus-newsgroup-maximum-articles -1))
6781                    (car active)))
6782          first nlast unread)
6783     ;; If none are read, then all are unread.
6784     (if (not read)
6785         (setq first bottom)
6786       ;; If the range of read articles is a single range, then the
6787       ;; first unread article is the article after the last read
6788       ;; article.  Sounds logical, doesn't it?
6789       (if (and (not (listp (cdr read)))
6790                (or (< (car read) bottom)
6791                    (progn (setq read (list read))
6792                           nil)))
6793           (setq first (max bottom (1+ (cdr read))))
6794         ;; `read' is a list of ranges.
6795         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6796                                   (caar read)))
6797                   1)
6798           (setq first bottom))
6799         (while read
6800           (when first
6801             (while (< first nlast)
6802               (setq unread (cons first unread)
6803                     first (1+ first))))
6804           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6805           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6806           (setq read (cdr read)))))
6807     ;; And add the last unread articles.
6808     (while (<= first last)
6809       (setq unread (cons first unread)
6810             first (1+ first)))
6811     ;; Return the list of unread articles.
6812     (delq 0 (nreverse unread))))
6813
6814 (defun gnus-list-of-read-articles (group)
6815   "Return a list of unread, unticked and non-dormant articles."
6816   (let* ((info (gnus-get-info group))
6817          (marked (gnus-info-marks info))
6818          (active (gnus-active group)))
6819     (and info active
6820          (gnus-list-range-difference
6821           (gnus-list-range-difference
6822            (gnus-sorted-complement
6823             (gnus-uncompress-range
6824              (if gnus-newsgroup-maximum-articles
6825                  (cons (max (car active)
6826                             (- (cdr active)
6827                                gnus-newsgroup-maximum-articles
6828                                -1))
6829                        (cdr active))
6830                active))
6831             (gnus-list-of-unread-articles group))
6832            (cdr (assq 'dormant marked)))
6833           (cdr (assq 'tick marked))))))
6834
6835 ;; This function returns a sequence of article numbers based on the
6836 ;; difference between the ranges of read articles in this group and
6837 ;; the range of active articles.
6838 (defun gnus-sequence-of-unread-articles (group)
6839   (let* ((read (gnus-info-read (gnus-get-info group)))
6840          (active (or (gnus-active group) (gnus-activate-group group)))
6841          (last (cdr active))
6842          (bottom (if gnus-newsgroup-maximum-articles
6843                      (max (car active)
6844                           (- last gnus-newsgroup-maximum-articles -1))
6845                    (car active)))
6846          first nlast unread)
6847     ;; If none are read, then all are unread.
6848     (if (not read)
6849         (setq first bottom)
6850       ;; If the range of read articles is a single range, then the
6851       ;; first unread article is the article after the last read
6852       ;; article.  Sounds logical, doesn't it?
6853       (if (and (not (listp (cdr read)))
6854                (or (< (car read) bottom)
6855                    (progn (setq read (list read))
6856                           nil)))
6857           (setq first (max bottom (1+ (cdr read))))
6858         ;; `read' is a list of ranges.
6859         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6860                                   (caar read)))
6861                   1)
6862           (setq first bottom))
6863         (while read
6864           (when first
6865             (push (cons first nlast) unread))
6866           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6867           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6868           (setq read (cdr read)))))
6869     ;; And add the last unread articles.
6870     (cond ((not (and first last))
6871            nil)
6872           ((< first last)
6873            (push (cons first last) unread))
6874           ((= first last)
6875            (push first unread)))
6876     ;; Return the sequence of unread articles.
6877     (delq 0 (nreverse unread))))
6878
6879 ;; Various summary commands
6880
6881 (defun gnus-summary-select-article-buffer ()
6882   "Reconfigure windows to show article buffer."
6883   (interactive)
6884   (if (not (gnus-buffer-live-p gnus-article-buffer))
6885       (error "There is no article buffer for this summary buffer")
6886     (gnus-configure-windows 'article)
6887     (select-window (get-buffer-window gnus-article-buffer))))
6888
6889 (defun gnus-summary-universal-argument (arg)
6890   "Perform any operation on all articles that are process/prefixed."
6891   (interactive "P")
6892   (let ((articles (gnus-summary-work-articles arg))
6893         func article)
6894     (if (eq
6895          (setq
6896           func
6897           (key-binding
6898            (read-key-sequence
6899             (substitute-command-keys
6900              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6901          'undefined)
6902         (gnus-error 1 "Undefined key")
6903       (save-excursion
6904         (while articles
6905           (gnus-summary-goto-subject (setq article (pop articles)))
6906           (let (gnus-newsgroup-processable)
6907             (command-execute func))
6908           (gnus-summary-remove-process-mark article)))))
6909   (gnus-summary-position-point))
6910
6911 (defun gnus-summary-toggle-truncation (&optional arg)
6912   "Toggle truncation of summary lines.
6913 With ARG, turn line truncation on if ARG is positive."
6914   (interactive "P")
6915   (setq truncate-lines
6916         (if (null arg) (not truncate-lines)
6917           (> (prefix-numeric-value arg) 0)))
6918   (redraw-display))
6919
6920 (defun gnus-summary-find-for-reselect ()
6921   "Return the number of an article to stay on across a reselect.
6922 The current article is considered, then following articles, then previous
6923 articles.  An article is sought which is not cancelled and isn't a temporary
6924 insertion from another group.  If there's no such then return a dummy 0."
6925   (let (found)
6926     (dolist (rev '(nil t))
6927       (unless found      ; don't demand the reverse list if we don't need it
6928         (let ((data (gnus-data-find-list
6929                      (gnus-summary-article-number) (gnus-data-list rev))))
6930           (while (and data (not found))
6931             (if (and (< 0 (gnus-data-number (car data)))
6932                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6933                 (setq found (gnus-data-number (car data))))
6934             (setq data (cdr data))))))
6935     (or found 0)))
6936
6937 (defun gnus-summary-reselect-current-group (&optional all rescan)
6938   "Exit and then reselect the current newsgroup.
6939 The prefix argument ALL means to select all articles."
6940   (interactive "P")
6941   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6942     (error "Ephemeral groups can't be reselected"))
6943   (let ((current-subject (gnus-summary-find-for-reselect))
6944         (group gnus-newsgroup-name))
6945     (setq gnus-newsgroup-begin nil)
6946     (gnus-summary-exit nil 'leave-hidden)
6947     ;; We have to adjust the point of group mode buffer because
6948     ;; point was moved to the next unread newsgroup by exiting.
6949     (gnus-summary-jump-to-group group)
6950     (when rescan
6951       (save-excursion
6952         (gnus-group-get-new-news-this-group 1)))
6953     (gnus-group-read-group all t)
6954     (gnus-summary-goto-subject current-subject nil t)))
6955
6956 (defun gnus-summary-rescan-group (&optional all)
6957   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6958   (interactive "P")
6959   (gnus-summary-reselect-current-group all t))
6960
6961 (defun gnus-summary-update-info (&optional non-destructive)
6962   (save-excursion
6963     (let ((group gnus-newsgroup-name))
6964       (when group
6965         (when gnus-newsgroup-kill-headers
6966           (setq gnus-newsgroup-killed
6967                 (gnus-compress-sequence
6968                  (gnus-sorted-union
6969                   (gnus-list-range-intersection
6970                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6971                   gnus-newsgroup-unreads)
6972                  t)))
6973         (unless (listp (cdr gnus-newsgroup-killed))
6974           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6975         (let ((headers gnus-newsgroup-headers))
6976           ;; Set the new ranges of read articles.
6977           (with-current-buffer gnus-group-buffer
6978             (gnus-undo-force-boundary))
6979           (gnus-update-read-articles
6980            group (gnus-sorted-union
6981                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6982           ;; Set the current article marks.
6983           (let ((gnus-newsgroup-scored
6984                  (if (and (not gnus-save-score)
6985                           (not non-destructive))
6986                      nil
6987                    gnus-newsgroup-scored)))
6988             (save-excursion
6989               (gnus-update-marks)))
6990           ;; Do the cross-ref thing.
6991           (when gnus-use-cross-reference
6992             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6993           ;; Do not switch windows but change the buffer to work.
6994           (set-buffer gnus-group-buffer)
6995           (unless (gnus-ephemeral-group-p group)
6996             (gnus-group-update-group group)))))))
6997
6998 (defun gnus-summary-save-newsrc (&optional force)
6999   "Save the current number of read/marked articles in the dribble buffer.
7000 The dribble buffer will then be saved.
7001 If FORCE (the prefix), also save the .newsrc file(s)."
7002   (interactive "P")
7003   (gnus-summary-update-info t)
7004   (if force
7005       (gnus-save-newsrc-file)
7006     (gnus-dribble-save)))
7007
7008 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7009
7010 (defun gnus-summary-exit (&optional temporary leave-hidden)
7011   "Exit reading current newsgroup, and then return to group selection mode.
7012 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7013   (interactive)
7014   (gnus-set-global-variables)
7015   (when (gnus-buffer-live-p gnus-article-buffer)
7016     (with-current-buffer gnus-article-buffer
7017       (mm-destroy-parts gnus-article-mime-handles)
7018       ;; Set it to nil for safety reason.
7019       (setq gnus-article-mime-handle-alist nil)
7020       (setq gnus-article-mime-handles nil)))
7021   (gnus-kill-save-kill-buffer)
7022   (gnus-async-halt-prefetch)
7023   (let* ((group gnus-newsgroup-name)
7024          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7025          (gnus-group-is-exiting-p t)
7026          (mode major-mode)
7027          (group-point nil)
7028          (buf (current-buffer)))
7029     (unless quit-config
7030       ;; Do adaptive scoring, and possibly save score files.
7031       (when gnus-newsgroup-adaptive
7032         (gnus-score-adaptive))
7033       (when gnus-use-scoring
7034         (gnus-score-save)))
7035     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7036     ;; If we have several article buffers, we kill them at exit.
7037     (unless gnus-single-article-buffer
7038       (when (gnus-buffer-live-p gnus-article-buffer)
7039         (with-current-buffer gnus-article-buffer
7040           ;; Don't kill sticky article buffers
7041           (unless (eq major-mode 'gnus-sticky-article-mode)
7042             (gnus-kill-buffer gnus-article-buffer)
7043             (setq gnus-article-current nil))))
7044       (gnus-kill-buffer gnus-original-article-buffer))
7045     (when gnus-use-cache
7046       (gnus-cache-possibly-remove-articles)
7047       (gnus-cache-save-buffers))
7048     (gnus-async-prefetch-remove-group group)
7049     (when gnus-suppress-duplicates
7050       (gnus-dup-enter-articles))
7051     (when gnus-use-trees
7052       (gnus-tree-close group))
7053     (when gnus-use-cache
7054       (gnus-cache-write-active))
7055     ;; Remove entries for this group.
7056     (nnmail-purge-split-history (gnus-group-real-name group))
7057     ;; Make all changes in this group permanent.
7058     (unless quit-config
7059       (gnus-run-hooks 'gnus-exit-group-hook)
7060       (gnus-summary-update-info))
7061     (gnus-close-group group)
7062     ;; Make sure where we were, and go to next newsgroup.
7063     (set-buffer gnus-group-buffer)
7064     (unless quit-config
7065       (gnus-group-jump-to-group group))
7066     (gnus-run-hooks 'gnus-summary-exit-hook)
7067     (unless (or quit-config
7068                 (not gnus-summary-next-group-on-exit)
7069                 ;; If this group has disappeared from the summary
7070                 ;; buffer, don't skip forwards.
7071                 (not (string= group (gnus-group-group-name))))
7072       (gnus-group-next-unread-group 1))
7073     (setq group-point (point))
7074     (if temporary
7075         nil                             ;Nothing to do.
7076       (set-buffer buf)
7077       (if (not gnus-kill-summary-on-exit)
7078           (progn
7079             (gnus-deaden-summary)
7080             (setq mode nil))
7081         ;; We set all buffer-local variables to nil.  It is unclear why
7082         ;; this is needed, but if we don't, buffer-local variables are
7083         ;; not garbage-collected, it seems.  This would the lead to en
7084         ;; ever-growing Emacs.
7085         (gnus-summary-clear-local-variables)
7086         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7087           (gnus-summary-clear-local-variables))
7088         (when (get-buffer gnus-article-buffer)
7089           (bury-buffer gnus-article-buffer))
7090         ;; Return to group mode buffer.
7091         (when (eq mode 'gnus-summary-mode)
7092           (gnus-kill-buffer buf)))
7093       (setq gnus-current-select-method gnus-select-method)
7094       (set-buffer gnus-group-buffer)
7095       (if quit-config
7096           (gnus-handle-ephemeral-exit quit-config)
7097         (goto-char group-point)
7098         ;; If gnus-group-buffer is already displayed, make sure we also move
7099         ;; the cursor in the window that displays it.
7100         (let ((win (get-buffer-window (current-buffer) 0)))
7101           (if win (set-window-point win (point))))
7102         (unless leave-hidden
7103           (gnus-configure-windows 'group 'force)))
7104       ;; Clear the current group name.
7105       (unless quit-config
7106         (setq gnus-newsgroup-name nil)))))
7107
7108 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7109 (defun gnus-summary-exit-no-update (&optional no-questions)
7110   "Quit reading current newsgroup without updating read article info."
7111   (interactive)
7112   (let* ((group gnus-newsgroup-name)
7113          (gnus-group-is-exiting-p t)
7114          (gnus-group-is-exiting-without-update-p t)
7115          (quit-config (gnus-group-quit-config group)))
7116     (when (or no-questions
7117               gnus-expert-user
7118               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7119       (gnus-async-halt-prefetch)
7120       (run-hooks 'gnus-summary-prepare-exit-hook)
7121       (when (gnus-buffer-live-p gnus-article-buffer)
7122         (with-current-buffer gnus-article-buffer
7123           (mm-destroy-parts gnus-article-mime-handles)
7124           ;; Set it to nil for safety reason.
7125           (setq gnus-article-mime-handle-alist nil)
7126           (setq gnus-article-mime-handles nil)))
7127       ;; If we have several article buffers, we kill them at exit.
7128       (unless gnus-single-article-buffer
7129         (gnus-kill-buffer gnus-article-buffer)
7130         (gnus-kill-buffer gnus-original-article-buffer)
7131         (setq gnus-article-current nil))
7132       (if (not gnus-kill-summary-on-exit)
7133           (gnus-deaden-summary)
7134         (gnus-close-group group)
7135         (gnus-summary-clear-local-variables)
7136         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7137           (gnus-summary-clear-local-variables))
7138         (gnus-kill-buffer gnus-summary-buffer))
7139       (unless gnus-single-article-buffer
7140         (setq gnus-article-current nil))
7141       (when gnus-use-trees
7142         (gnus-tree-close group))
7143       (gnus-async-prefetch-remove-group group)
7144       (when (get-buffer gnus-article-buffer)
7145         (bury-buffer gnus-article-buffer))
7146       ;; Return to the group buffer.
7147       (gnus-configure-windows 'group 'force)
7148       ;; Clear the current group name.
7149       (setq gnus-newsgroup-name nil)
7150       (unless (gnus-ephemeral-group-p group)
7151         (gnus-group-update-group group))
7152       (when (equal (gnus-group-group-name) group)
7153         (gnus-group-next-unread-group 1))
7154       (when quit-config
7155         (gnus-handle-ephemeral-exit quit-config)))))
7156
7157 (defun gnus-handle-ephemeral-exit (quit-config)
7158   "Handle movement when leaving an ephemeral group.
7159 The state which existed when entering the ephemeral is reset."
7160   (if (not (buffer-name (car quit-config)))
7161       (gnus-configure-windows 'group 'force)
7162     (set-buffer (car quit-config))
7163     (cond ((eq major-mode 'gnus-summary-mode)
7164            (gnus-set-global-variables))
7165           ((eq major-mode 'gnus-article-mode)
7166            (save-current-buffer
7167              ;; The `gnus-summary-buffer' variable may point
7168              ;; to the old summary buffer when using a single
7169              ;; article buffer.
7170              (unless (gnus-buffer-live-p gnus-summary-buffer)
7171                (set-buffer gnus-group-buffer))
7172              (set-buffer gnus-summary-buffer)
7173              (gnus-set-global-variables))))
7174     (if (or (eq (cdr quit-config) 'article)
7175             (eq (cdr quit-config) 'pick))
7176         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7177             (gnus-configure-windows 'pick 'force)
7178           (gnus-configure-windows (cdr quit-config) 'force))
7179       (gnus-configure-windows (cdr quit-config) 'force))
7180     (when (eq major-mode 'gnus-summary-mode)
7181       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7182                                                      next-unread-noselect))
7183           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7184                                   'next-noselect)
7185                               (gnus-summary-next-subject 1 nil t))
7186                              ((eq gnus-auto-select-on-ephemeral-exit
7187                                   'next-unread-noselect)
7188                               (gnus-summary-next-subject 1 t t))))
7189             ;; Hide the article buffer which displays the article different
7190             ;; from the one that the cursor points to in the summary buffer.
7191             (gnus-configure-windows 'summary 'force))
7192         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7193                (gnus-summary-next-subject 1))
7194               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7195                (gnus-summary-next-subject 1 t))))
7196       (gnus-summary-recenter)
7197       (gnus-summary-position-point))))
7198
7199 ;;; Dead summaries.
7200
7201 (defvar gnus-dead-summary-mode-map nil)
7202
7203 (unless gnus-dead-summary-mode-map
7204   (setq gnus-dead-summary-mode-map (make-keymap))
7205   (suppress-keymap gnus-dead-summary-mode-map)
7206   (substitute-key-definition
7207    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7208   (dolist (key '("\C-d" "\r" "\177" [delete]))
7209     (define-key gnus-dead-summary-mode-map
7210       key 'gnus-summary-wake-up-the-dead))
7211   (dolist (key '("q" "Q"))
7212     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7213
7214 (defvar gnus-dead-summary-mode nil
7215   "Minor mode for Gnus summary buffers.")
7216
7217 (defun gnus-dead-summary-mode (&optional arg)
7218   "Minor mode for Gnus summary buffers."
7219   (interactive "P")
7220   (when (eq major-mode 'gnus-summary-mode)
7221     (make-local-variable 'gnus-dead-summary-mode)
7222     (setq gnus-dead-summary-mode
7223           (if (null arg) (not gnus-dead-summary-mode)
7224             (> (prefix-numeric-value arg) 0)))
7225     (when gnus-dead-summary-mode
7226       (add-minor-mode
7227        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7228
7229 (defun gnus-deaden-summary ()
7230   "Make the current summary buffer into a dead summary buffer."
7231   ;; Kill any previous dead summary buffer.
7232   (when (and gnus-dead-summary
7233              (buffer-name gnus-dead-summary))
7234     (with-current-buffer gnus-dead-summary
7235       (when gnus-dead-summary-mode
7236         (kill-buffer (current-buffer)))))
7237   ;; Make this the current dead summary.
7238   (setq gnus-dead-summary (current-buffer))
7239   (gnus-dead-summary-mode 1)
7240   (let ((name (buffer-name)))
7241     (when (string-match "Summary" name)
7242       (rename-buffer
7243        (concat (substring name 0 (match-beginning 0)) "Dead "
7244                (substring name (match-beginning 0)))
7245        t)
7246       (bury-buffer))))
7247
7248 (defun gnus-kill-or-deaden-summary (buffer)
7249   "Kill or deaden the summary BUFFER."
7250   (save-excursion
7251     (when (and (buffer-name buffer)
7252                (not gnus-single-article-buffer))
7253       (with-current-buffer buffer
7254         (gnus-kill-buffer gnus-article-buffer)
7255         (gnus-kill-buffer gnus-original-article-buffer)))
7256     (cond
7257      ;; Kill the buffer.
7258      (gnus-kill-summary-on-exit
7259       (when (and gnus-use-trees
7260                  (gnus-buffer-exists-p buffer))
7261         (with-current-buffer buffer
7262           (gnus-tree-close gnus-newsgroup-name)))
7263       (gnus-kill-buffer buffer))
7264      ;; Deaden the buffer.
7265      ((gnus-buffer-exists-p buffer)
7266       (with-current-buffer buffer
7267         (gnus-deaden-summary))))))
7268
7269 (defun gnus-summary-wake-up-the-dead (&rest args)
7270   "Wake up the dead summary buffer."
7271   (interactive)
7272   (gnus-dead-summary-mode -1)
7273   (let ((name (buffer-name)))
7274     (when (string-match "Dead " name)
7275       (rename-buffer
7276        (concat (substring name 0 (match-beginning 0))
7277                (substring name (match-end 0)))
7278        t)))
7279   (gnus-message 3 "This dead summary is now alive again"))
7280
7281 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7282 (defun gnus-summary-fetch-faq (&optional faq-dir)
7283   "Fetch the FAQ for the current group.
7284 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7285 in."
7286   (interactive
7287    (list
7288     (when current-prefix-arg
7289       (completing-read
7290        "FAQ dir: " (and (listp gnus-group-faq-directory)
7291                         (mapcar 'list
7292                                 gnus-group-faq-directory))))))
7293   (let (gnus-faq-buffer)
7294     (when (setq gnus-faq-buffer
7295                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7296       (gnus-configure-windows 'summary-faq))))
7297
7298 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7299 (defun gnus-summary-describe-group (&optional force)
7300   "Describe the current newsgroup."
7301   (interactive "P")
7302   (gnus-group-describe-group force gnus-newsgroup-name))
7303
7304 (defun gnus-summary-describe-briefly ()
7305   "Describe summary mode commands briefly."
7306   (interactive)
7307   (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")))
7308
7309 ;; Walking around group mode buffer from summary mode.
7310
7311 (defun gnus-summary-next-group (&optional no-article target-group backward)
7312   "Exit current newsgroup and then select next unread newsgroup.
7313 If prefix argument NO-ARTICLE is non-nil, no article is selected
7314 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7315 previous group instead."
7316   (interactive "P")
7317   ;; Stop pre-fetching.
7318   (gnus-async-halt-prefetch)
7319   (let ((current-group gnus-newsgroup-name)
7320         (current-buffer (current-buffer))
7321         entered)
7322     ;; First we semi-exit this group to update Xrefs and all variables.
7323     ;; We can't do a real exit, because the window conf must remain
7324     ;; the same in case the user is prompted for info, and we don't
7325     ;; want the window conf to change before that...
7326     (gnus-summary-exit t)
7327     (while (not entered)
7328       ;; Then we find what group we are supposed to enter.
7329       (set-buffer gnus-group-buffer)
7330       (gnus-group-jump-to-group current-group)
7331       (setq target-group
7332             (or target-group
7333                 (if (eq gnus-keep-same-level 'best)
7334                     (gnus-summary-best-group gnus-newsgroup-name)
7335                   (gnus-summary-search-group backward gnus-keep-same-level))))
7336       (if (not target-group)
7337           ;; There are no further groups, so we return to the group
7338           ;; buffer.
7339           (progn
7340             (gnus-message 5 "Returning to the group buffer")
7341             (setq entered t)
7342             (when (gnus-buffer-live-p current-buffer)
7343               (set-buffer current-buffer)
7344               (gnus-summary-exit))
7345             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7346         ;; We try to enter the target group.
7347         (gnus-group-jump-to-group target-group)
7348         (let ((unreads (gnus-group-group-unread)))
7349           (if (and (or (eq t unreads)
7350                        (and unreads (not (zerop unreads))))
7351                    (gnus-summary-read-group
7352                     target-group nil no-article
7353                     (and (buffer-name current-buffer) current-buffer)
7354                     nil backward))
7355               (setq entered t)
7356             (setq current-group target-group
7357                   target-group nil)))))))
7358
7359 (defun gnus-summary-prev-group (&optional no-article)
7360   "Exit current newsgroup and then select previous unread newsgroup.
7361 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7362   (interactive "P")
7363   (gnus-summary-next-group no-article nil t))
7364
7365 ;; Walking around summary lines.
7366
7367 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7368   "Go to the first subject satisfying any non-nil constraint.
7369 If UNREAD is non-nil, the article should be unread.
7370 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7371 If UNSEEN is non-nil, the article should be unseen.
7372 Returns the article selected or nil if there are no matching articles."
7373   (interactive "P")
7374   (cond
7375    ;; Empty summary.
7376    ((null gnus-newsgroup-data)
7377     (gnus-message 3 "No articles in the group")
7378     nil)
7379    ;; Pick the first article.
7380    ((not (or unread undownloaded unseen))
7381     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7382     (gnus-data-number (car gnus-newsgroup-data)))
7383    ;; Find the first unread article.
7384    (t
7385     (let ((data gnus-newsgroup-data))
7386       (while (and data
7387                   (let ((num (gnus-data-number (car data))))
7388                     (or (memq num gnus-newsgroup-unfetched)
7389                         (not (or (and unread
7390                                       (memq num gnus-newsgroup-unreads))
7391                                  (and undownloaded
7392                                       (memq num gnus-newsgroup-undownloaded))
7393                                  (and unseen
7394                                       (memq num gnus-newsgroup-unseen)))))))
7395         (setq data (cdr data)))
7396       (prog1
7397           (if data
7398               (progn
7399                 (goto-char (gnus-data-pos (car data)))
7400                 (gnus-data-number (car data)))
7401             (gnus-message 3 "No more%s articles"
7402                           (let* ((r (when unread " unread"))
7403                                  (d (when undownloaded " undownloaded"))
7404                                  (s (when unseen " unseen"))
7405                                  (l (delq nil (list r d s))))
7406                             (cond ((= 3 (length l))
7407                                    (concat r "," d ", or" s))
7408                                   ((= 2 (length l))
7409                                    (concat (car l) ", or" (cadr l)))
7410                                   ((= 1 (length l))
7411                                    (car l))
7412                                   (t
7413                                    ""))))
7414             nil
7415             )
7416         (gnus-summary-position-point))))))
7417
7418 (defun gnus-summary-next-subject (n &optional unread dont-display)
7419   "Go to next N'th summary line.
7420 If N is negative, go to the previous N'th subject line.
7421 If UNREAD is non-nil, only unread articles are selected.
7422 The difference between N and the actual number of steps taken is
7423 returned."
7424   (interactive "p")
7425   (let ((backward (< n 0))
7426         (n (abs n)))
7427     (while (and (> n 0)
7428                 (if backward
7429                     (gnus-summary-find-prev unread)
7430                   (gnus-summary-find-next unread)))
7431       (unless (zerop (setq n (1- n)))
7432         (gnus-summary-show-thread)))
7433     (when (/= 0 n)
7434       (gnus-message 7 "No more%s articles"
7435                     (if unread " unread" "")))
7436     (unless dont-display
7437       (gnus-summary-recenter)
7438       (gnus-summary-position-point))
7439     n))
7440
7441 (defun gnus-summary-next-unread-subject (n)
7442   "Go to next N'th unread summary line."
7443   (interactive "p")
7444   (gnus-summary-next-subject n t))
7445
7446 (defun gnus-summary-prev-subject (n &optional unread)
7447   "Go to previous N'th summary line.
7448 If optional argument UNREAD is non-nil, only unread article is selected."
7449   (interactive "p")
7450   (gnus-summary-next-subject (- n) unread))
7451
7452 (defun gnus-summary-prev-unread-subject (n)
7453   "Go to previous N'th unread summary line."
7454   (interactive "p")
7455   (gnus-summary-next-subject (- n) t))
7456
7457 (defun gnus-summary-goto-subjects (articles)
7458   "Insert the subject header for ARTICLES in the current buffer."
7459   (save-excursion
7460     (dolist (article articles)
7461       (gnus-summary-goto-subject article t)))
7462   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7463   (gnus-summary-position-point))
7464
7465 (defun gnus-summary-goto-subject (article &optional force silent)
7466   "Go to the subject line of ARTICLE.
7467 If FORCE, also allow jumping to articles not currently shown."
7468   (interactive "nArticle number: ")
7469   (unless (numberp article)
7470     (error "Article %s is not a number" article))
7471   (let ((b (point))
7472         (data (gnus-data-find article)))
7473     ;; We read in the article if we have to.
7474     (and (not data)
7475          force
7476          (gnus-summary-insert-subject
7477           article
7478           (if (or (numberp force) (vectorp force)) force)
7479           t)
7480          (setq data (gnus-data-find article)))
7481     (goto-char b)
7482     (if (not data)
7483         (progn
7484           (unless silent
7485             (gnus-message 3 "Can't find article %d" article))
7486           nil)
7487       (let ((pt (gnus-data-pos data)))
7488         (goto-char pt)
7489         (gnus-summary-set-article-display-arrow pt))
7490       (gnus-summary-position-point)
7491       article)))
7492
7493 ;; Walking around summary lines with displaying articles.
7494
7495 (defun gnus-summary-expand-window (&optional arg)
7496   "Make the summary buffer take up the entire Emacs frame.
7497 Given a prefix, will force an `article' buffer configuration."
7498   (interactive "P")
7499   (if arg
7500       (gnus-configure-windows 'article 'force)
7501     (gnus-configure-windows 'summary 'force)))
7502
7503 (defun gnus-summary-display-article (article &optional all-header)
7504   "Display ARTICLE in article buffer."
7505   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7506                (with-current-buffer gnus-article-buffer
7507                  (eq major-mode 'gnus-article-mode)))
7508     (gnus-article-setup-buffer))
7509   (gnus-set-global-variables)
7510   (with-current-buffer gnus-article-buffer
7511     (setq gnus-article-charset gnus-newsgroup-charset)
7512     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7513     (mm-enable-multibyte))
7514   (if (null article)
7515       nil
7516     (prog1
7517         (if gnus-summary-display-article-function
7518             (funcall gnus-summary-display-article-function article all-header)
7519           (gnus-article-prepare article all-header))
7520       (gnus-run-hooks 'gnus-select-article-hook)
7521       (when (and gnus-current-article
7522                  (not (zerop gnus-current-article)))
7523         (gnus-summary-goto-subject gnus-current-article))
7524       (gnus-summary-recenter)
7525       (when (and gnus-use-trees gnus-show-threads)
7526         (gnus-possibly-generate-tree article)
7527         (gnus-highlight-selected-tree article))
7528       ;; Successfully display article.
7529       (gnus-article-set-window-start
7530        (cdr (assq article gnus-newsgroup-bookmarks))))))
7531
7532 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7533   "Select the current article.
7534 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7535 non-nil, the article will be re-fetched even if it already present in
7536 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7537 be displayed."
7538   ;; Make sure we are in the summary buffer to work around bbdb bug.
7539   (unless (eq major-mode 'gnus-summary-mode)
7540     (set-buffer gnus-summary-buffer))
7541   (let ((article (or article (gnus-summary-article-number)))
7542         (all-headers (not (not all-headers))) ;Must be t or nil.
7543         gnus-summary-display-article-function)
7544     (and (not pseudo)
7545          (gnus-summary-article-pseudo-p article)
7546          (error "This is a pseudo-article"))
7547     (with-current-buffer gnus-summary-buffer
7548       (if (or (and gnus-single-article-buffer
7549                    (or (null gnus-current-article)
7550                        (null gnus-article-current)
7551                        (null (get-buffer gnus-article-buffer))
7552                        (not (eq article (cdr gnus-article-current)))
7553                        (not (equal (car gnus-article-current)
7554                                    gnus-newsgroup-name))))
7555               (and (not gnus-single-article-buffer)
7556                    (or (null gnus-current-article)
7557                        (not (eq gnus-current-article article))))
7558               force)
7559           ;; The requested article is different from the current article.
7560           (progn
7561             (gnus-summary-display-article article all-headers)
7562             (when (gnus-buffer-live-p gnus-article-buffer)
7563               (with-current-buffer gnus-article-buffer
7564                 (if (not gnus-article-decoded-p) ;; a local variable
7565                     (mm-disable-multibyte))))
7566             (gnus-article-set-window-start
7567              (cdr (assq article gnus-newsgroup-bookmarks)))
7568             article)
7569         'old))))
7570
7571 (defun gnus-summary-force-verify-and-decrypt ()
7572   "Display buttons for signed/encrypted parts and verify/decrypt them."
7573   (interactive)
7574   (let ((mm-verify-option 'known)
7575         (mm-decrypt-option 'known)
7576         (gnus-article-emulate-mime t)
7577         (gnus-buttonized-mime-types (append (list "multipart/signed"
7578                                                   "multipart/encrypted")
7579                                             gnus-buttonized-mime-types)))
7580     (gnus-summary-select-article nil 'force)))
7581
7582 (defun gnus-summary-set-current-mark (&optional current-mark)
7583   "Obsolete function."
7584   nil)
7585
7586 (defun gnus-summary-next-article (&optional unread subject backward push)
7587   "Select the next article.
7588 If UNREAD, only unread articles are selected.
7589 If SUBJECT, only articles with SUBJECT are selected.
7590 If BACKWARD, the previous article is selected instead of the next."
7591   (interactive "P")
7592   ;; Make sure we are in the summary buffer.
7593   (unless (eq major-mode 'gnus-summary-mode)
7594     (set-buffer gnus-summary-buffer))
7595   (cond
7596    ;; Is there such an article?
7597    ((and (gnus-summary-search-forward unread subject backward)
7598          (or (gnus-summary-display-article (gnus-summary-article-number))
7599              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7600     (gnus-summary-position-point))
7601    ;; If not, we try the first unread, if that is wanted.
7602    ((and subject
7603          gnus-auto-select-same
7604          (gnus-summary-first-unread-article))
7605     (gnus-summary-position-point)
7606     (gnus-message 6 "Wrapped"))
7607    ;; Try to get next/previous article not displayed in this group.
7608    ((and gnus-auto-extend-newsgroup
7609          (not unread) (not subject))
7610     (gnus-summary-goto-article
7611      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7612      nil (count-lines (point-min) (point))))
7613    ;; Go to next/previous group.
7614    (t
7615     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7616       (gnus-summary-jump-to-group gnus-newsgroup-name))
7617     (let ((cmd last-command-char)
7618           (point
7619            (with-current-buffer gnus-group-buffer
7620              (point)))
7621           (group
7622            (if (eq gnus-keep-same-level 'best)
7623                (gnus-summary-best-group gnus-newsgroup-name)
7624              (gnus-summary-search-group backward gnus-keep-same-level))))
7625       ;; For some reason, the group window gets selected.  We change
7626       ;; it back.
7627       (select-window (get-buffer-window (current-buffer)))
7628       ;; Select next unread newsgroup automagically.
7629       (cond
7630        ((or (not gnus-auto-select-next)
7631             (not cmd))
7632         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7633        ((or (eq gnus-auto-select-next 'quietly)
7634             (and (eq gnus-auto-select-next 'slightly-quietly)
7635                  push)
7636             (and (eq gnus-auto-select-next 'almost-quietly)
7637                  (gnus-summary-last-article-p)))
7638         ;; Select quietly.
7639         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7640             (gnus-summary-exit)
7641           (gnus-message 7 "No more%s articles (%s)..."
7642                         (if unread " unread" "")
7643                         (if group (concat "selecting " group)
7644                           "exiting"))
7645           (gnus-summary-next-group nil group backward)))
7646        (t
7647         (when (gnus-key-press-event-p last-input-event)
7648           (gnus-summary-walk-group-buffer
7649            gnus-newsgroup-name cmd unread backward point))))))))
7650
7651 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7652   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7653                       (?\C-p (gnus-group-prev-unread-group 1))))
7654         (cursor-in-echo-area t)
7655         keve key group ended prompt)
7656     (with-current-buffer gnus-group-buffer
7657       (goto-char start)
7658       (setq group
7659             (if (eq gnus-keep-same-level 'best)
7660                 (gnus-summary-best-group gnus-newsgroup-name)
7661               (gnus-summary-search-group backward gnus-keep-same-level))))
7662     (while (not ended)
7663       (setq prompt
7664             (format
7665              "No more%s articles%s " (if unread " unread" "")
7666              (if (and group
7667                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7668                  (format " (Type %s for %s [%s])"
7669                          (single-key-description cmd)
7670                          (gnus-group-decoded-name group)
7671                          (gnus-group-unread group))
7672                (format " (Type %s to exit %s)"
7673                        (single-key-description cmd)
7674                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7675       ;; Confirm auto selection.
7676       (setq key (car (setq keve (gnus-read-event-char prompt)))
7677             ended t)
7678       (cond
7679        ((assq key keystrokes)
7680         (let ((obuf (current-buffer)))
7681           (switch-to-buffer gnus-group-buffer)
7682           (when group
7683             (gnus-group-jump-to-group group))
7684           (eval (cadr (assq key keystrokes)))
7685           (setq group (gnus-group-group-name))
7686           (switch-to-buffer obuf))
7687         (setq ended nil))
7688        ((equal key cmd)
7689         (if (or (not group)
7690                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7691             (gnus-summary-exit)
7692           (gnus-summary-next-group nil group backward)))
7693        (t
7694         (push (cdr keve) unread-command-events))))))
7695
7696 (defun gnus-summary-next-unread-article ()
7697   "Select unread article after current one."
7698   (interactive)
7699   (gnus-summary-next-article
7700    (or (not (eq gnus-summary-goto-unread 'never))
7701        (gnus-summary-last-article-p (gnus-summary-article-number)))
7702    (and gnus-auto-select-same
7703         (gnus-summary-article-subject))))
7704
7705 (defun gnus-summary-prev-article (&optional unread subject)
7706   "Select the article before the current one.
7707 If UNREAD is non-nil, only unread articles are selected."
7708   (interactive "P")
7709   (gnus-summary-next-article unread subject t))
7710
7711 (defun gnus-summary-prev-unread-article ()
7712   "Select unread article before current one."
7713   (interactive)
7714   (gnus-summary-prev-article
7715    (or (not (eq gnus-summary-goto-unread 'never))
7716        (gnus-summary-first-article-p (gnus-summary-article-number)))
7717    (and gnus-auto-select-same
7718         (gnus-summary-article-subject))))
7719
7720 (defun gnus-summary-next-page (&optional lines circular stop)
7721   "Show next page of the selected article.
7722 If at the end of the current article, select the next article.
7723 LINES says how many lines should be scrolled up.
7724
7725 If CIRCULAR is non-nil, go to the start of the article instead of
7726 selecting the next article when reaching the end of the current
7727 article.
7728
7729 If STOP is non-nil, just stop when reaching the end of the message.
7730
7731 Also see the variable `gnus-article-skip-boring'."
7732   (interactive "P")
7733   (setq gnus-summary-buffer (current-buffer))
7734   (gnus-set-global-variables)
7735   (let ((article (gnus-summary-article-number))
7736         (article-window (get-buffer-window gnus-article-buffer t))
7737         endp)
7738     ;; If the buffer is empty, we have no article.
7739     (unless article
7740       (error "No article to select"))
7741     (gnus-configure-windows 'article)
7742     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7743         (if (and (eq gnus-summary-goto-unread 'never)
7744                  (not (gnus-summary-last-article-p article)))
7745             (gnus-summary-next-article)
7746           (gnus-summary-next-unread-article))
7747       (if (or (null gnus-current-article)
7748               (null gnus-article-current)
7749               (/= article (cdr gnus-article-current))
7750               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7751           ;; Selected subject is different from current article's.
7752           (gnus-summary-display-article article)
7753         (when article-window
7754           (gnus-eval-in-buffer-window gnus-article-buffer
7755             (setq endp (or (gnus-article-next-page lines)
7756                            (gnus-article-only-boring-p))))
7757           (when endp
7758             (cond (stop
7759                    (gnus-message 3 "End of message"))
7760                   (circular
7761                    (gnus-summary-beginning-of-article))
7762                   (lines
7763                    (gnus-message 3 "End of message"))
7764                   ((null lines)
7765                    (if (and (eq gnus-summary-goto-unread 'never)
7766                             (not (gnus-summary-last-article-p article)))
7767                        (gnus-summary-next-article)
7768                      (gnus-summary-next-unread-article))))))))
7769     (gnus-summary-recenter)
7770     (gnus-summary-position-point)))
7771
7772 (defun gnus-summary-prev-page (&optional lines move)
7773   "Show previous page of selected article.
7774 Argument LINES specifies lines to be scrolled down.
7775 If MOVE, move to the previous unread article if point is at
7776 the beginning of the buffer."
7777   (interactive "P")
7778   (let ((article (gnus-summary-article-number))
7779         (article-window (get-buffer-window gnus-article-buffer t))
7780         endp)
7781     (gnus-configure-windows 'article)
7782     (if (or (null gnus-current-article)
7783             (null gnus-article-current)
7784             (/= article (cdr gnus-article-current))
7785             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7786         ;; Selected subject is different from current article's.
7787         (gnus-summary-display-article article)
7788       (gnus-summary-recenter)
7789       (when article-window
7790         (gnus-eval-in-buffer-window gnus-article-buffer
7791           (setq endp (gnus-article-prev-page lines)))
7792         (when (and move endp)
7793           (cond (lines
7794                  (gnus-message 3 "Beginning of message"))
7795                 ((null lines)
7796                  (if (and (eq gnus-summary-goto-unread 'never)
7797                           (not (gnus-summary-first-article-p article)))
7798                      (gnus-summary-prev-article)
7799                    (gnus-summary-prev-unread-article))))))))
7800   (gnus-summary-position-point))
7801
7802 (defun gnus-summary-prev-page-or-article (&optional lines)
7803   "Show previous page of selected article.
7804 Argument LINES specifies lines to be scrolled down.
7805 If at the beginning of the article, go to the next article."
7806   (interactive "P")
7807   (gnus-summary-prev-page lines t))
7808
7809 (defun gnus-summary-scroll-up (lines)
7810   "Scroll up (or down) one line current article.
7811 Argument LINES specifies lines to be scrolled up (or down if negative)."
7812   (interactive "p")
7813   (gnus-configure-windows 'article)
7814   (gnus-summary-show-thread)
7815   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7816     (gnus-eval-in-buffer-window gnus-article-buffer
7817       (cond ((> lines 0)
7818              (when (gnus-article-next-page lines)
7819                (gnus-message 3 "End of message")))
7820             ((< lines 0)
7821              (gnus-article-prev-page (- lines))))))
7822   (gnus-summary-recenter)
7823   (gnus-summary-position-point))
7824
7825 (defun gnus-summary-scroll-down (lines)
7826   "Scroll down (or up) one line current article.
7827 Argument LINES specifies lines to be scrolled down (or up if negative)."
7828   (interactive "p")
7829   (gnus-summary-scroll-up (- lines)))
7830
7831 (defun gnus-summary-next-same-subject ()
7832   "Select next article which has the same subject as current one."
7833   (interactive)
7834   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7835
7836 (defun gnus-summary-prev-same-subject ()
7837   "Select previous article which has the same subject as current one."
7838   (interactive)
7839   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7840
7841 (defun gnus-summary-next-unread-same-subject ()
7842   "Select next unread article which has the same subject as current one."
7843   (interactive)
7844   (gnus-summary-next-article t (gnus-summary-article-subject)))
7845
7846 (defun gnus-summary-prev-unread-same-subject ()
7847   "Select previous unread article which has the same subject as current one."
7848   (interactive)
7849   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7850
7851 (defun gnus-summary-first-unread-article ()
7852   "Select the first unread article.
7853 Return nil if there are no unread articles."
7854   (interactive)
7855   (prog1
7856       (when (gnus-summary-first-subject t)
7857         (gnus-summary-show-thread)
7858         (gnus-summary-first-subject t)
7859         (gnus-summary-display-article (gnus-summary-article-number)))
7860     (gnus-summary-position-point)))
7861
7862 (defun gnus-summary-first-unread-subject ()
7863   "Place the point on the subject line of the first unread article.
7864 Return nil if there are no unread articles."
7865   (interactive)
7866   (prog1
7867       (when (gnus-summary-first-subject t)
7868         (gnus-summary-show-thread)
7869         (gnus-summary-first-subject t))
7870     (gnus-summary-position-point)))
7871
7872 (defun gnus-summary-first-unseen-subject ()
7873   "Place the point on the subject line of the first unseen article.
7874 Return nil if there are no unseen articles."
7875   (interactive)
7876   (prog1
7877       (when (gnus-summary-first-subject nil nil t)
7878         (gnus-summary-show-thread)
7879         (gnus-summary-first-subject nil nil t))
7880     (gnus-summary-position-point)))
7881
7882 (defun gnus-summary-first-unseen-or-unread-subject ()
7883   "Place the point on the subject line of the first unseen article or,
7884 if all article have been seen, on the subject line of the first unread
7885 article."
7886   (interactive)
7887   (prog1
7888       (unless (when (gnus-summary-first-subject nil nil t)
7889                 (gnus-summary-show-thread)
7890                 (gnus-summary-first-subject nil nil t))
7891         (when (gnus-summary-first-subject t)
7892           (gnus-summary-show-thread)
7893           (gnus-summary-first-subject t)))
7894     (gnus-summary-position-point)))
7895
7896 (defun gnus-summary-first-article ()
7897   "Select the first article.
7898 Return nil if there are no articles."
7899   (interactive)
7900   (prog1
7901       (when (gnus-summary-first-subject)
7902         (gnus-summary-show-thread)
7903         (gnus-summary-first-subject)
7904         (gnus-summary-display-article (gnus-summary-article-number)))
7905     (gnus-summary-position-point)))
7906
7907 (defun gnus-summary-best-unread-article (&optional arg)
7908   "Select the unread article with the highest score.
7909 If given a prefix argument, select the next unread article that has a
7910 score higher than the default score."
7911   (interactive "P")
7912   (let ((article (if arg
7913                      (gnus-summary-better-unread-subject)
7914                    (gnus-summary-best-unread-subject))))
7915     (if article
7916         (gnus-summary-goto-article article)
7917       (error "No unread articles"))))
7918
7919 (defun gnus-summary-best-unread-subject ()
7920   "Select the unread subject with the highest score."
7921   (interactive)
7922   (let ((best -1000000)
7923         (data gnus-newsgroup-data)
7924         article score)
7925     (while data
7926       (and (gnus-data-unread-p (car data))
7927            (> (setq score
7928                     (gnus-summary-article-score (gnus-data-number (car data))))
7929               best)
7930            (setq best score
7931                  article (gnus-data-number (car data))))
7932       (setq data (cdr data)))
7933     (when article
7934       (gnus-summary-goto-subject article))
7935     (gnus-summary-position-point)
7936     article))
7937
7938 (defun gnus-summary-better-unread-subject ()
7939   "Select the first unread subject that has a score over the default score."
7940   (interactive)
7941   (let ((data gnus-newsgroup-data)
7942         article score)
7943     (while (and (setq article (gnus-data-number (car data)))
7944                 (or (gnus-data-read-p (car data))
7945                     (not (> (gnus-summary-article-score article)
7946                             gnus-summary-default-score))))
7947       (setq data (cdr data)))
7948     (when article
7949       (gnus-summary-goto-subject article))
7950     (gnus-summary-position-point)
7951     article))
7952
7953 (defun gnus-summary-last-subject ()
7954   "Go to the last displayed subject line in the group."
7955   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7956     (when article
7957       (gnus-summary-goto-subject article))))
7958
7959 (defun gnus-summary-goto-article (article &optional all-headers force)
7960   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7961 If ALL-HEADERS is non-nil, no header lines are hidden.
7962 If FORCE, go to the article even if it isn't displayed.  If FORCE
7963 is a number, it is the line the article is to be displayed on."
7964   (interactive
7965    (list
7966     (completing-read
7967      "Article number or Message-ID: "
7968      (mapcar (lambda (number) (list (int-to-string number)))
7969              gnus-newsgroup-limit))
7970     current-prefix-arg
7971     t))
7972   (prog1
7973       (if (and (stringp article)
7974                (string-match "@\\|%40" article))
7975           (gnus-summary-refer-article article)
7976         (when (stringp article)
7977           (setq article (string-to-number article)))
7978         (if (gnus-summary-goto-subject article force)
7979             (gnus-summary-display-article article all-headers)
7980           (gnus-message 4 "Couldn't go to article %s" article) nil))
7981     (gnus-summary-position-point)))
7982
7983 (defun gnus-summary-goto-last-article ()
7984   "Go to the previously read article."
7985   (interactive)
7986   (prog1
7987       (when gnus-last-article
7988         (gnus-summary-goto-article gnus-last-article nil t))
7989     (gnus-summary-position-point)))
7990
7991 (defun gnus-summary-pop-article (number)
7992   "Pop one article off the history and go to the previous.
7993 NUMBER articles will be popped off."
7994   (interactive "p")
7995   (let (to)
7996     (setq gnus-newsgroup-history
7997           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7998     (if to
7999         (gnus-summary-goto-article (car to) nil t)
8000       (error "Article history empty")))
8001   (gnus-summary-position-point))
8002
8003 ;; Summary commands and functions for limiting the summary buffer.
8004
8005 (defun gnus-summary-limit-to-articles (n)
8006   "Limit the summary buffer to the next N articles.
8007 If not given a prefix, use the process marked articles instead."
8008   (interactive "P")
8009   (prog1
8010       (let ((articles (gnus-summary-work-articles n)))
8011         (setq gnus-newsgroup-processable nil)
8012         (gnus-summary-limit articles))
8013     (gnus-summary-position-point)))
8014
8015 (defun gnus-summary-pop-limit (&optional total)
8016   "Restore the previous limit.
8017 If given a prefix, remove all limits."
8018   (interactive "P")
8019   (when total
8020     (setq gnus-newsgroup-limits
8021           (list (mapcar (lambda (h) (mail-header-number h))
8022                         gnus-newsgroup-headers))))
8023   (unless gnus-newsgroup-limits
8024     (error "No limit to pop"))
8025   (prog1
8026       (gnus-summary-limit nil 'pop)
8027     (gnus-summary-position-point)))
8028
8029 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8030   "Limit the summary buffer to articles that have subjects that match a regexp.
8031 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8032   (interactive
8033    (list (read-string (if current-prefix-arg
8034                           "Exclude subject (regexp): "
8035                         "Limit to subject (regexp): "))
8036          nil current-prefix-arg))
8037   (unless header
8038     (setq header "subject"))
8039   (when (not (equal "" subject))
8040     (prog1
8041         (let ((articles (gnus-summary-find-matching
8042                          (or header "subject") subject 'all nil nil
8043                          not-matching)))
8044           (unless articles
8045             (error "Found no matches for \"%s\"" subject))
8046           (gnus-summary-limit articles))
8047       (gnus-summary-position-point))))
8048
8049 (defun gnus-summary-limit-to-author (from &optional not-matching)
8050   "Limit the summary buffer to articles that have authors that match a regexp.
8051 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8052   (interactive
8053    (list (read-string (if current-prefix-arg
8054                           "Exclude author (regexp): "
8055                         "Limit to author (regexp): "))
8056          current-prefix-arg))
8057   (gnus-summary-limit-to-subject from "from" not-matching))
8058
8059 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8060   "Limit the summary buffer to articles with the given RECIPIENT.
8061
8062 If NOT-MATCHING, exclude RECIPIENT.
8063
8064 To and Cc headers are checked.  You need to include them in
8065 `nnmail-extra-headers'."
8066   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8067   (interactive
8068    (list (read-string (format "%s recipient (regexp): "
8069                               (if current-prefix-arg "Exclude" "Limit to")))
8070          current-prefix-arg))
8071   (when (not (equal "" recipient))
8072     (prog1 (let* ((to
8073                    (if (memq 'To nnmail-extra-headers)
8074                        (gnus-summary-find-matching
8075                         (cons 'extra 'To) recipient 'all nil nil
8076                         not-matching)
8077                      (gnus-message
8078                       1 "`To' isn't present in `nnmail-extra-headers'")
8079                      (sit-for 1)
8080                      nil))
8081                   (cc
8082                    (if (memq 'Cc nnmail-extra-headers)
8083                        (gnus-summary-find-matching
8084                         (cons 'extra 'Cc) recipient 'all nil nil
8085                         not-matching)
8086                      (gnus-message
8087                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8088                      (sit-for 1)
8089                      nil))
8090                   (articles
8091                    (if not-matching
8092                        ;; We need the numbers that are in both lists:
8093                        (mapcar (lambda (a)
8094                                  (and (memq a to) a))
8095                                cc)
8096                      (nconc to cc))))
8097              (unless articles
8098                (error "Found no matches for \"%s\"" recipient))
8099              (gnus-summary-limit articles))
8100       (gnus-summary-position-point))))
8101
8102 (defun gnus-summary-limit-to-address (address &optional not-matching)
8103   "Limit the summary buffer to articles with the given ADDRESS.
8104
8105 If NOT-MATCHING, exclude ADDRESS.
8106
8107 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8108 in `nnmail-extra-headers'."
8109   (interactive
8110    (list (read-string (format "%s address (regexp): "
8111                               (if current-prefix-arg "Exclude" "Limit to")))
8112          current-prefix-arg))
8113   (when (not (equal "" address))
8114     (prog1 (let* ((to
8115                    (if (memq 'To nnmail-extra-headers)
8116                        (gnus-summary-find-matching
8117                         (cons 'extra 'To) address 'all nil nil
8118                         not-matching)
8119                      (gnus-message
8120                       1 "`To' isn't present in `nnmail-extra-headers'")
8121                      (sit-for 1)
8122                      t))
8123                   (cc
8124                    (if (memq 'Cc nnmail-extra-headers)
8125                        (gnus-summary-find-matching
8126                         (cons 'extra 'Cc) address 'all nil nil
8127                         not-matching)
8128                      (gnus-message
8129                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8130                      (sit-for 1)
8131                      t))
8132                   (from
8133                    (gnus-summary-find-matching "from" address
8134                                                'all nil nil not-matching))
8135                   (articles
8136                    (if not-matching
8137                        ;; We need the numbers that are in all lists:
8138                        (if (eq cc t)
8139                            (if (eq to t)
8140                                from
8141                              (mapcar (lambda (a) (car (memq a from))) to))
8142                          (if (eq to t)
8143                              (mapcar (lambda (a) (car (memq a from))) cc)
8144                            (mapcar (lambda (a) (car (memq a from)))
8145                                    (mapcar (lambda (a) (car (memq a to)))
8146                                            cc))))
8147                      (nconc (if (eq to t) nil to)
8148                             (if (eq cc t) nil cc)
8149                             from))))
8150              (unless articles
8151                (error "Found no matches for \"%s\"" address))
8152              (gnus-summary-limit articles))
8153       (gnus-summary-position-point))))
8154
8155 (defun gnus-summary-limit-strange-charsets-predicate (header)
8156   (let ((string (concat (mail-header-subject header)
8157                         (mail-header-from header)))
8158         charset found)
8159     (dotimes (i (1- (length string)))
8160       (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8161       (when (string-match "unicode\\|big\\|japanese" charset)
8162         (setq found t)))
8163     found))
8164
8165 (defun gnus-summary-limit-to-predicate (predicate)
8166   "Limit to articles where PREDICATE returns non-nil.
8167 PREDICATE will be called with the header structures of the
8168 articles."
8169   (let ((articles nil)
8170         (case-fold-search t))
8171     (dolist (header gnus-newsgroup-headers)
8172       (when (funcall predicate header)
8173         (push (mail-header-number header) articles)))
8174     (gnus-summary-limit (nreverse articles))))
8175
8176 (defun gnus-summary-limit-to-age (age &optional younger-p)
8177   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8178 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8179 articles that are younger than AGE days."
8180   (interactive
8181    (let ((younger current-prefix-arg)
8182          (days-got nil)
8183          days)
8184      (while (not days-got)
8185        (setq days (if younger
8186                       (read-string "Limit to articles younger than (in days, older when negative): ")
8187                     (read-string
8188                      "Limit to articles older than (in days, younger when negative): ")))
8189        (when (> (length days) 0)
8190          (setq days (read days)))
8191        (if (numberp days)
8192            (progn
8193              (setq days-got t)
8194              (when (< days 0)
8195                (setq younger (not younger))
8196                (setq days (* days -1))))
8197          (message "Please enter a number.")
8198          (sleep-for 1)))
8199      (list days younger)))
8200   (prog1
8201       (let ((data gnus-newsgroup-data)
8202             (cutoff (days-to-time age))
8203             articles d date is-younger)
8204         (while (setq d (pop data))
8205           (when (and (vectorp (gnus-data-header d))
8206                      (setq date (mail-header-date (gnus-data-header d))))
8207             (setq is-younger (time-less-p
8208                               (time-since (condition-case ()
8209                                               (date-to-time date)
8210                                             (error '(0 0))))
8211                               cutoff))
8212             (when (if younger-p
8213                       is-younger
8214                     (not is-younger))
8215               (push (gnus-data-number d) articles))))
8216         (gnus-summary-limit (nreverse articles)))
8217     (gnus-summary-position-point)))
8218
8219 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8220   "Limit the summary buffer to articles that match an 'extra' header."
8221   (interactive
8222    (let ((header
8223           (intern
8224            (gnus-completing-read-with-default
8225             (symbol-name (car gnus-extra-headers))
8226             (if current-prefix-arg
8227                 "Exclude extra header"
8228               "Limit extra header")
8229             (mapcar (lambda (x)
8230                       (cons (symbol-name x) x))
8231                     gnus-extra-headers)
8232             nil
8233             t))))
8234      (list header
8235            (read-string (format "%s header %s (regexp): "
8236                                 (if current-prefix-arg "Exclude" "Limit to")
8237                                 header))
8238            current-prefix-arg)))
8239   (when (not (equal "" regexp))
8240     (prog1
8241         (let ((articles (gnus-summary-find-matching
8242                          (cons 'extra header) regexp 'all nil nil
8243                          not-matching)))
8244           (unless articles
8245             (error "Found no matches for \"%s\"" regexp))
8246           (gnus-summary-limit articles))
8247       (gnus-summary-position-point))))
8248
8249 (defun gnus-summary-limit-to-display-predicate ()
8250   "Limit the summary buffer to the predicated in the `display' group parameter."
8251   (interactive)
8252   (unless gnus-newsgroup-display
8253     (error "There is no `display' group parameter"))
8254   (let (articles)
8255     (dolist (number gnus-newsgroup-articles)
8256       (when (funcall gnus-newsgroup-display)
8257         (push number articles)))
8258     (gnus-summary-limit articles))
8259   (gnus-summary-position-point))
8260
8261 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8262 (make-obsolete
8263  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8264
8265 (defun gnus-summary-limit-to-unread (&optional all)
8266   "Limit the summary buffer to articles that are not marked as read.
8267 If ALL is non-nil, limit strictly to unread articles."
8268   (interactive "P")
8269   (if all
8270       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8271     (gnus-summary-limit-to-marks
8272      ;; Concat all the marks that say that an article is read and have
8273      ;; those removed.
8274      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8275            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8276            gnus-low-score-mark gnus-expirable-mark
8277            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8278            gnus-duplicate-mark gnus-souped-mark)
8279      'reverse)))
8280
8281 (defun gnus-summary-limit-to-headers (match &optional reverse)
8282   "Limit the summary buffer to articles that have headers that match MATCH.
8283 If REVERSE (the prefix), limit to articles that don't match."
8284   (interactive "sMatch headers (regexp): \nP")
8285   (gnus-summary-limit-to-bodies match reverse t))
8286
8287 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8288   "Limit the summary buffer to articles that have bodies that match MATCH.
8289 If REVERSE (the prefix), limit to articles that don't match."
8290   (interactive "sMatch body (regexp): \nP")
8291   (let ((articles nil)
8292         (gnus-select-article-hook nil)  ;Disable hook.
8293         (gnus-article-prepare-hook nil)
8294         (gnus-use-article-prefetch nil)
8295         (gnus-keep-backlog nil)
8296         (gnus-break-pages nil)
8297         (gnus-summary-display-arrow nil)
8298         (gnus-updated-mode-lines nil)
8299         (gnus-auto-center-summary nil)
8300         (gnus-display-mime-function nil))
8301     (dolist (data gnus-newsgroup-data)
8302       (let (gnus-mark-article-hook)
8303         (gnus-summary-select-article t t nil (gnus-data-number data)))
8304       (save-excursion
8305         (set-buffer gnus-article-buffer)
8306         (article-goto-body)
8307         (let* ((case-fold-search t)
8308                (found (if headersp
8309                           (re-search-backward match nil t)
8310                         (re-search-forward match nil t))))
8311           (when (or (and found
8312                          (not reverse))
8313                     (and (not found)
8314                          reverse))
8315             (push (gnus-data-number data) articles)))))
8316     (if (not articles)
8317         (message "No messages matched")
8318       (gnus-summary-limit articles)))
8319   (gnus-summary-position-point))
8320
8321 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8322   "Limit the summary buffer to articles that aren't part on any thread.
8323 If THREADSP (the prefix), limit to articles that are in threads."
8324   (interactive "P")
8325   (let ((articles nil)
8326         thread-articles
8327         threads)
8328     (dolist (thread gnus-newsgroup-threads)
8329       (if (stringp (car thread))
8330           (dolist (thread (cdr thread))
8331             (push thread threads))
8332         (push thread threads)))
8333     (dolist (thread threads)
8334       (setq thread-articles (gnus-articles-in-thread thread))
8335       (when (or (and threadsp
8336                      (> (length thread-articles) 1))
8337                 (and (not threadsp)
8338                      (= (length thread-articles) 1)))
8339         (setq articles (nconc thread-articles articles))))
8340     (if (not articles)
8341         (message "No messages matched")
8342       (gnus-summary-limit articles))
8343     (gnus-summary-position-point)))
8344
8345 (defun gnus-summary-limit-to-replied (&optional unreplied)
8346   "Limit the summary buffer to replied articles.
8347 If UNREPLIED (the prefix), limit to unreplied articles."
8348   (interactive "P")
8349   (if unreplied
8350       (gnus-summary-limit
8351        (gnus-set-difference gnus-newsgroup-articles
8352         gnus-newsgroup-replied))
8353     (gnus-summary-limit gnus-newsgroup-replied))
8354   (gnus-summary-position-point))
8355
8356 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8357 (make-obsolete 'gnus-summary-delete-marked-with
8358                'gnus-summary-limit-exclude-marks)
8359
8360 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8361   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8362 If REVERSE, limit the summary buffer to articles that are marked
8363 with MARKS.  MARKS can either be a string of marks or a list of marks.
8364 Returns how many articles were removed."
8365   (interactive "sMarks: ")
8366   (gnus-summary-limit-to-marks marks t))
8367
8368 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8369   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8370 If REVERSE (the prefix), limit the summary buffer to articles that are
8371 not marked with MARKS.  MARKS can either be a string of marks or a
8372 list of marks.
8373 Returns how many articles were removed."
8374   (interactive "sMarks: \nP")
8375   (prog1
8376       (let ((data gnus-newsgroup-data)
8377             (marks (if (listp marks) marks
8378                      (append marks nil))) ; Transform to list.
8379             articles)
8380         (while data
8381           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8382                   (memq (gnus-data-mark (car data)) marks))
8383             (push (gnus-data-number (car data)) articles))
8384           (setq data (cdr data)))
8385         (gnus-summary-limit articles))
8386     (gnus-summary-position-point)))
8387
8388 (defun gnus-summary-limit-to-score (score)
8389   "Limit to articles with score at or above SCORE."
8390   (interactive "NLimit to articles with score of at least: ")
8391   (let ((data gnus-newsgroup-data)
8392         articles)
8393     (while data
8394       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8395                 score)
8396         (push (gnus-data-number (car data)) articles))
8397       (setq data (cdr data)))
8398     (prog1
8399         (gnus-summary-limit articles)
8400       (gnus-summary-position-point))))
8401
8402 (defun gnus-summary-limit-to-unseen ()
8403   "Limit to unseen articles."
8404   (interactive)
8405   (prog1
8406       (gnus-summary-limit gnus-newsgroup-unseen)
8407     (gnus-summary-position-point)))
8408
8409 (defun gnus-summary-limit-include-thread (id)
8410   "Display all the hidden articles that is in the thread with ID in it.
8411 When called interactively, ID is the Message-ID of the current
8412 article."
8413   (interactive (list (mail-header-id (gnus-summary-article-header))))
8414   (let ((articles (gnus-articles-in-thread
8415                    (gnus-id-to-thread (gnus-root-id id)))))
8416     (prog1
8417         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8418       (gnus-summary-limit-include-matching-articles
8419        "subject"
8420        (regexp-quote (gnus-simplify-subject-re
8421                       (mail-header-subject (gnus-id-to-header id)))))
8422       (gnus-summary-position-point))))
8423
8424 (defun gnus-summary-limit-include-matching-articles (header regexp)
8425   "Display all the hidden articles that have HEADERs that match REGEXP."
8426   (interactive (list (read-string "Match on header: ")
8427                      (read-string "Regexp: ")))
8428   (let ((articles (gnus-find-matching-articles header regexp)))
8429     (prog1
8430         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8431       (gnus-summary-position-point))))
8432
8433 (defun gnus-summary-insert-dormant-articles ()
8434   "Insert all the dormant articles for this group into the current buffer."
8435   (interactive)
8436   (let ((gnus-verbose (max 6 gnus-verbose)))
8437     (if (not gnus-newsgroup-dormant)
8438         (gnus-message 3 "No dormant articles for this group")
8439       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8440
8441 (defun gnus-summary-insert-ticked-articles ()
8442   "Insert ticked articles for this group into the current buffer."
8443   (interactive)
8444   (let ((gnus-verbose (max 6 gnus-verbose)))
8445     (if (not gnus-newsgroup-marked)
8446         (gnus-message 3 "No ticked articles for this group")
8447       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8448
8449 (defun gnus-summary-limit-include-dormant ()
8450   "Display all the hidden articles that are marked as dormant.
8451 Note that this command only works on a subset of the articles currently
8452 fetched for this group."
8453   (interactive)
8454   (unless gnus-newsgroup-dormant
8455     (error "There are no dormant articles in this group"))
8456   (prog1
8457       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8458     (gnus-summary-position-point)))
8459
8460 (defun gnus-summary-limit-exclude-dormant ()
8461   "Hide all dormant articles."
8462   (interactive)
8463   (prog1
8464       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8465     (gnus-summary-position-point)))
8466
8467 (defun gnus-summary-limit-exclude-childless-dormant ()
8468   "Hide all dormant articles that have no children."
8469   (interactive)
8470   (let ((data (gnus-data-list t))
8471         articles d children)
8472     ;; Find all articles that are either not dormant or have
8473     ;; children.
8474     (while (setq d (pop data))
8475       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8476                 (and (setq children
8477                            (gnus-article-children (gnus-data-number d)))
8478                      (let (found)
8479                        (while children
8480                          (when (memq (car children) articles)
8481                            (setq children nil
8482                                  found t))
8483                          (pop children))
8484                        found)))
8485         (push (gnus-data-number d) articles)))
8486     ;; Do the limiting.
8487     (prog1
8488         (gnus-summary-limit articles)
8489       (gnus-summary-position-point))))
8490
8491 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8492   "Mark all unread excluded articles as read.
8493 If ALL, mark even excluded ticked and dormants as read."
8494   (interactive "P")
8495   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8496   (let ((articles (gnus-sorted-ndifference
8497                    (sort
8498                     (mapcar (lambda (h) (mail-header-number h))
8499                             gnus-newsgroup-headers)
8500                     '<)
8501                    gnus-newsgroup-limit))
8502         article)
8503     (setq gnus-newsgroup-unreads
8504           (gnus-sorted-intersection gnus-newsgroup-unreads
8505                                     gnus-newsgroup-limit))
8506     (if all
8507         (setq gnus-newsgroup-dormant nil
8508               gnus-newsgroup-marked nil
8509               gnus-newsgroup-reads
8510               (nconc
8511                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8512                gnus-newsgroup-reads))
8513       (while (setq article (pop articles))
8514         (unless (or (memq article gnus-newsgroup-dormant)
8515                     (memq article gnus-newsgroup-marked))
8516           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8517
8518 (defun gnus-summary-limit (articles &optional pop)
8519   (if pop
8520       ;; We pop the previous limit off the stack and use that.
8521       (setq articles (car gnus-newsgroup-limits)
8522             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8523     ;; We use the new limit, so we push the old limit on the stack.
8524     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8525   ;; Set the limit.
8526   (setq gnus-newsgroup-limit articles)
8527   (let ((total (length gnus-newsgroup-data))
8528         (data (gnus-data-find-list (gnus-summary-article-number)))
8529         (gnus-summary-mark-below nil)   ; Inhibit this.
8530         found)
8531     ;; This will do all the work of generating the new summary buffer
8532     ;; according to the new limit.
8533     (gnus-summary-prepare)
8534     ;; Hide any threads, possibly.
8535     (gnus-summary-maybe-hide-threads)
8536     ;; Try to return to the article you were at, or one in the
8537     ;; neighborhood.
8538     (when data
8539       ;; We try to find some article after the current one.
8540       (while data
8541         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8542           (setq data nil
8543                 found t))
8544         (setq data (cdr data))))
8545     (unless found
8546       ;; If there is no data, that means that we were after the last
8547       ;; article.  The same goes when we can't find any articles
8548       ;; after the current one.
8549       (goto-char (point-max))
8550       (gnus-summary-find-prev))
8551     (gnus-set-mode-line 'summary)
8552     ;; We return how many articles were removed from the summary
8553     ;; buffer as a result of the new limit.
8554     (- total (length gnus-newsgroup-data))))
8555
8556 (defsubst gnus-invisible-cut-children (threads)
8557   (let ((num 0))
8558     (while threads
8559       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8560         (incf num))
8561       (pop threads))
8562     (< num 2)))
8563
8564 (defsubst gnus-cut-thread (thread)
8565   "Go forwards in the thread until we find an article that we want to display."
8566   (when (or (eq gnus-fetch-old-headers 'some)
8567             (eq gnus-fetch-old-headers 'invisible)
8568             (numberp gnus-fetch-old-headers)
8569             (eq gnus-build-sparse-threads 'some)
8570             (eq gnus-build-sparse-threads 'more))
8571     ;; Deal with old-fetched headers and sparse threads.
8572     (while (and
8573             thread
8574             (or
8575              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8576              (gnus-summary-article-ancient-p
8577               (mail-header-number (car thread))))
8578             (if (or (<= (length (cdr thread)) 1)
8579                     (eq gnus-fetch-old-headers 'invisible))
8580                 (setq gnus-newsgroup-limit
8581                       (delq (mail-header-number (car thread))
8582                             gnus-newsgroup-limit)
8583                       thread (cadr thread))
8584               (when (gnus-invisible-cut-children (cdr thread))
8585                 (let ((th (cdr thread)))
8586                   (while th
8587                     (if (memq (mail-header-number (caar th))
8588                               gnus-newsgroup-limit)
8589                         (setq thread (car th)
8590                               th nil)
8591                       (setq th (cdr th))))))))))
8592   thread)
8593
8594 (defun gnus-cut-threads (threads)
8595   "Cut off all uninteresting articles from the beginning of THREADS."
8596   (when (or (eq gnus-fetch-old-headers 'some)
8597             (eq gnus-fetch-old-headers 'invisible)
8598             (numberp gnus-fetch-old-headers)
8599             (eq gnus-build-sparse-threads 'some)
8600             (eq gnus-build-sparse-threads 'more))
8601     (let ((th threads))
8602       (while th
8603         (setcar th (gnus-cut-thread (car th)))
8604         (setq th (cdr th)))))
8605   ;; Remove nixed out threads.
8606   (delq nil threads))
8607
8608 (defun gnus-summary-initial-limit (&optional show-if-empty)
8609   "Figure out what the initial limit is supposed to be on group entry.
8610 This entails weeding out unwanted dormants, low-scored articles,
8611 fetch-old-headers verbiage, and so on."
8612   ;; Most groups have nothing to remove.
8613   (unless (or gnus-inhibit-limiting
8614               (and (null gnus-newsgroup-dormant)
8615                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8616                    (not (eq gnus-fetch-old-headers 'some))
8617                    (not (numberp gnus-fetch-old-headers))
8618                    (not (eq gnus-fetch-old-headers 'invisible))
8619                    (null gnus-summary-expunge-below)
8620                    (not (eq gnus-build-sparse-threads 'some))
8621                    (not (eq gnus-build-sparse-threads 'more))
8622                    (null gnus-thread-expunge-below)
8623                    (not gnus-use-nocem)))
8624     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8625     (setq gnus-newsgroup-limit nil)
8626     (mapatoms
8627      (lambda (node)
8628        (unless (car (symbol-value node))
8629          ;; These threads have no parents -- they are roots.
8630          (let ((nodes (cdr (symbol-value node)))
8631                thread)
8632            (while nodes
8633              (if (and gnus-thread-expunge-below
8634                       (< (gnus-thread-total-score (car nodes))
8635                          gnus-thread-expunge-below))
8636                  (gnus-expunge-thread (pop nodes))
8637                (setq thread (pop nodes))
8638                (gnus-summary-limit-children thread))))))
8639      gnus-newsgroup-dependencies)
8640     ;; If this limitation resulted in an empty group, we might
8641     ;; pop the previous limit and use it instead.
8642     (when (and (not gnus-newsgroup-limit)
8643                show-if-empty)
8644       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8645     gnus-newsgroup-limit))
8646
8647 (defun gnus-summary-limit-children (thread)
8648   "Return 1 if this subthread is visible and 0 if it is not."
8649   ;; First we get the number of visible children to this thread.  This
8650   ;; is done by recursing down the thread using this function, so this
8651   ;; will really go down to a leaf article first, before slowly
8652   ;; working its way up towards the root.
8653   (when thread
8654     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8655            (children
8656            (if (cdr thread)
8657                (apply '+ (mapcar 'gnus-summary-limit-children
8658                                  (cdr thread)))
8659              0))
8660           (number (mail-header-number (car thread)))
8661           score)
8662       (if (and
8663            (not (memq number gnus-newsgroup-marked))
8664            (or
8665             ;; If this article is dormant and has absolutely no visible
8666             ;; children, then this article isn't visible.
8667             (and (memq number gnus-newsgroup-dormant)
8668                  (zerop children))
8669             ;; If this is "fetch-old-headered" and there is no
8670             ;; visible children, then we don't want this article.
8671             (and (or (eq gnus-fetch-old-headers 'some)
8672                      (numberp gnus-fetch-old-headers))
8673                  (gnus-summary-article-ancient-p number)
8674                  (zerop children))
8675             ;; If this is "fetch-old-headered" and `invisible', then
8676             ;; we don't want this article.
8677             (and (eq gnus-fetch-old-headers 'invisible)
8678                  (gnus-summary-article-ancient-p number))
8679             ;; If this is a sparsely inserted article with no children,
8680             ;; we don't want it.
8681             (and (eq gnus-build-sparse-threads 'some)
8682                  (gnus-summary-article-sparse-p number)
8683                  (zerop children))
8684             ;; If we use expunging, and this article is really
8685             ;; low-scored, then we don't want this article.
8686             (when (and gnus-summary-expunge-below
8687                        (< (setq score
8688                                 (or (cdr (assq number gnus-newsgroup-scored))
8689                                     gnus-summary-default-score))
8690                           gnus-summary-expunge-below))
8691               ;; We increase the expunge-tally here, but that has
8692               ;; nothing to do with the limits, really.
8693               (incf gnus-newsgroup-expunged-tally)
8694               ;; We also mark as read here, if that's wanted.
8695               (when (and gnus-summary-mark-below
8696                          (< score gnus-summary-mark-below))
8697                 (setq gnus-newsgroup-unreads
8698                       (delq number gnus-newsgroup-unreads))
8699                 (if gnus-newsgroup-auto-expire
8700                     (push number gnus-newsgroup-expirable)
8701                   (push (cons number gnus-low-score-mark)
8702                         gnus-newsgroup-reads)))
8703               t)
8704             ;; Do the `display' group parameter.
8705             (and gnus-newsgroup-display
8706                  (not (funcall gnus-newsgroup-display)))
8707             ;; Check NoCeM things.
8708             (when (and gnus-use-nocem
8709                        (gnus-nocem-unwanted-article-p
8710                         (mail-header-id (car thread))))
8711               (setq gnus-newsgroup-unreads
8712                     (delq number gnus-newsgroup-unreads))
8713               t)))
8714           ;; Nope, invisible article.
8715           0
8716         ;; Ok, this article is to be visible, so we add it to the limit
8717         ;; and return 1.
8718         (push number gnus-newsgroup-limit)
8719         1))))
8720
8721 (defun gnus-expunge-thread (thread)
8722   "Mark all articles in THREAD as read."
8723   (let* ((number (mail-header-number (car thread))))
8724     (incf gnus-newsgroup-expunged-tally)
8725     ;; We also mark as read here, if that's wanted.
8726     (setq gnus-newsgroup-unreads
8727           (delq number gnus-newsgroup-unreads))
8728     (if gnus-newsgroup-auto-expire
8729         (push number gnus-newsgroup-expirable)
8730       (push (cons number gnus-low-score-mark)
8731             gnus-newsgroup-reads)))
8732   ;; Go recursively through all subthreads.
8733   (mapcar 'gnus-expunge-thread (cdr thread)))
8734
8735 ;; Summary article oriented commands
8736
8737 (defun gnus-summary-refer-parent-article (n)
8738   "Refer parent article N times.
8739 If N is negative, go to ancestor -N instead.
8740 The difference between N and the number of articles fetched is returned."
8741   (interactive "p")
8742   (let ((skip 1)
8743         error header ref)
8744     (when (not (natnump n))
8745       (setq skip (abs n)
8746             n 1))
8747     (while (and (> n 0)
8748                 (not error))
8749       (setq header (gnus-summary-article-header))
8750       (if (and (eq (mail-header-number header)
8751                    (cdr gnus-article-current))
8752                (equal gnus-newsgroup-name
8753                       (car gnus-article-current)))
8754           ;; If we try to find the parent of the currently
8755           ;; displayed article, then we take a look at the actual
8756           ;; References header, since this is slightly more
8757           ;; reliable than the References field we got from the
8758           ;; server.
8759           (with-current-buffer gnus-original-article-buffer
8760             (nnheader-narrow-to-headers)
8761             (unless (setq ref (message-fetch-field "references"))
8762               (when (setq ref (message-fetch-field "in-reply-to"))
8763                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8764             (widen))
8765         (setq ref
8766               ;; It's not the current article, so we take a bet on
8767               ;; the value we got from the server.
8768               (mail-header-references header)))
8769       (if (and ref
8770                (not (equal ref "")))
8771           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8772             (gnus-message 1 "Couldn't find parent"))
8773         (gnus-message 1 "No references in article %d"
8774                       (gnus-summary-article-number))
8775         (setq error t))
8776       (decf n))
8777     (gnus-summary-position-point)
8778     n))
8779
8780 (defun gnus-summary-refer-references ()
8781   "Fetch all articles mentioned in the References header.
8782 Return the number of articles fetched."
8783   (interactive)
8784   (let ((ref (mail-header-references (gnus-summary-article-header)))
8785         (current (gnus-summary-article-number))
8786         (n 0))
8787     (if (or (not ref)
8788             (equal ref ""))
8789         (error "No References in the current article")
8790       ;; For each Message-ID in the References header...
8791       (while (string-match "<[^>]*>" ref)
8792         (incf n)
8793         ;; ... fetch that article.
8794         (gnus-summary-refer-article
8795          (prog1 (match-string 0 ref)
8796            (setq ref (substring ref (match-end 0))))))
8797       (gnus-summary-goto-subject current)
8798       (gnus-summary-position-point)
8799       n)))
8800
8801 (defun gnus-summary-refer-thread (&optional limit)
8802   "Fetch all articles in the current thread.
8803 If LIMIT (the numerical prefix), fetch that many old headers instead
8804 of what's specified by the `gnus-refer-thread-limit' variable."
8805   (interactive "P")
8806   (let ((id (mail-header-id (gnus-summary-article-header)))
8807         (limit (if limit (prefix-numeric-value limit)
8808                  gnus-refer-thread-limit)))
8809     (unless (eq gnus-fetch-old-headers 'invisible)
8810       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8811       ;; Retrieve the headers and read them in.
8812       (if (eq (if (numberp limit)
8813                   (gnus-retrieve-headers
8814                    (list (min
8815                           (+ (mail-header-number
8816                               (gnus-summary-article-header))
8817                              limit)
8818                           gnus-newsgroup-end))
8819                    gnus-newsgroup-name (* limit 2))
8820                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8821                 ;; headers.
8822                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8823                                        gnus-newsgroup-name limit))
8824               'nov)
8825           (gnus-build-all-threads)
8826         (error "Can't fetch thread from back ends that don't support NOV"))
8827       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8828     (gnus-summary-limit-include-thread id)))
8829
8830 (defun gnus-summary-refer-article (message-id)
8831   "Fetch an article specified by MESSAGE-ID."
8832   (interactive "sMessage-ID: ")
8833   (when (and (stringp message-id)
8834              (not (zerop (length message-id))))
8835     (setq message-id (gnus-replace-in-string message-id " " ""))
8836     ;; Construct the correct Message-ID if necessary.
8837     ;; Suggested by tale@pawl.rpi.edu.
8838     (unless (string-match "^<" message-id)
8839       (setq message-id (concat "<" message-id)))
8840     (unless (string-match ">$" message-id)
8841       (setq message-id (concat message-id ">")))
8842     ;; People often post MIDs from URLs, so unhex it:
8843     (unless (string-match "@" message-id)
8844       (setq message-id (gnus-url-unhex-string message-id)))
8845     (let* ((header (gnus-id-to-header message-id))
8846            (sparse (and header
8847                         (gnus-summary-article-sparse-p
8848                          (mail-header-number header))
8849                         (memq (mail-header-number header)
8850                               gnus-newsgroup-limit)))
8851            number)
8852       (cond
8853        ;; If the article is present in the buffer we just go to it.
8854        ((and header
8855              (or (not (gnus-summary-article-sparse-p
8856                        (mail-header-number header)))
8857                  sparse))
8858         (prog1
8859             (gnus-summary-goto-article
8860              (mail-header-number header) nil t)
8861           (when sparse
8862             (gnus-summary-update-article (mail-header-number header)))))
8863        (t
8864         ;; We fetch the article.
8865         (catch 'found
8866           (dolist (gnus-override-method (gnus-refer-article-methods))
8867             (when (and (gnus-check-server gnus-override-method)
8868                        ;; Fetch the header,
8869                        (setq number (gnus-summary-insert-subject message-id)))
8870               ;; and display the article.
8871               (gnus-summary-select-article nil nil nil number)
8872               (throw 'found t)))
8873           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8874
8875 (defun gnus-refer-article-methods ()
8876   "Return a list of referable methods."
8877   (cond
8878    ;; No method, so we default to current and native.
8879    ((null gnus-refer-article-method)
8880     (list gnus-current-select-method gnus-select-method))
8881    ;; Current.
8882    ((eq 'current gnus-refer-article-method)
8883     (list gnus-current-select-method))
8884    ;; List of select methods.
8885    ((not (and (symbolp (car gnus-refer-article-method))
8886               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8887     (let (out)
8888       (dolist (method gnus-refer-article-method)
8889         (push (if (eq 'current method)
8890                   gnus-current-select-method
8891                 method)
8892               out))
8893       (nreverse out)))
8894    ;; One single select method.
8895    (t
8896     (list gnus-refer-article-method))))
8897
8898 (defun gnus-summary-edit-parameters ()
8899   "Edit the group parameters of the current group."
8900   (interactive)
8901   (gnus-group-edit-group gnus-newsgroup-name 'params))
8902
8903 (defun gnus-summary-customize-parameters ()
8904   "Customize the group parameters of the current group."
8905   (interactive)
8906   (gnus-group-customize gnus-newsgroup-name))
8907
8908 (defun gnus-summary-enter-digest-group (&optional force)
8909   "Enter an nndoc group based on the current article.
8910 If FORCE, force a digest interpretation.  If not, try
8911 to guess what the document format is."
8912   (interactive "P")
8913   (let ((conf gnus-current-window-configuration))
8914     (save-window-excursion
8915       (save-excursion
8916         (let (gnus-article-prepare-hook
8917               gnus-display-mime-function
8918               gnus-break-pages)
8919           (gnus-summary-select-article))))
8920     (setq gnus-current-window-configuration conf)
8921     (let* ((name (format "%s-%d"
8922                          (gnus-group-prefixed-name
8923                           gnus-newsgroup-name (list 'nndoc ""))
8924                          (with-current-buffer gnus-summary-buffer
8925                            gnus-current-article)))
8926            (ogroup gnus-newsgroup-name)
8927            (params (append (gnus-info-params (gnus-get-info ogroup))
8928                            (list (cons 'to-group ogroup))
8929                            (list (cons 'parent-group ogroup))
8930                            (list (cons 'save-article-group ogroup))))
8931            (case-fold-search t)
8932            (buf (current-buffer))
8933            dig to-address)
8934       (with-current-buffer gnus-original-article-buffer
8935         ;; Have the digest group inherit the main mail address of
8936         ;; the parent article.
8937         (when (setq to-address (or (gnus-fetch-field "reply-to")
8938                                    (gnus-fetch-field "from")))
8939           (setq params
8940                 (append
8941                  (list (cons 'to-address
8942                              (funcall gnus-decode-encoded-address-function
8943                                       to-address))))))
8944         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8945         (insert-buffer-substring gnus-original-article-buffer)
8946         ;; Remove lines that may lead nndoc to misinterpret the
8947         ;; document type.
8948         (narrow-to-region
8949          (goto-char (point-min))
8950          (or (search-forward "\n\n" nil t) (point)))
8951         (goto-char (point-min))
8952         (delete-matching-lines "^Path:\\|^From ")
8953         (widen))
8954       (unwind-protect
8955           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8956                     (gnus-newsgroup-ephemeral-ignored-charsets
8957                      gnus-newsgroup-ignored-charsets))
8958                 (gnus-group-read-ephemeral-group
8959                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8960                               (nndoc-article-type
8961                                ,(if force 'mbox 'guess)))
8962                  t nil nil nil
8963                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8964                                                         "ADAPT")))))
8965               ;; Make all postings to this group go to the parent group.
8966               (nconc (gnus-info-params (gnus-get-info name))
8967                      params)
8968             ;; Couldn't select this doc group.
8969             (switch-to-buffer buf)
8970             (gnus-set-global-variables)
8971             (gnus-configure-windows 'summary)
8972             (gnus-message 3 "Article couldn't be entered?"))
8973         (kill-buffer dig)))))
8974
8975 (defun gnus-summary-read-document (n)
8976   "Open a new group based on the current article(s).
8977 This will allow you to read digests and other similar
8978 documents as newsgroups.
8979 Obeys the standard process/prefix convention."
8980   (interactive "P")
8981   (let* ((ogroup gnus-newsgroup-name)
8982          (params (append (gnus-info-params (gnus-get-info ogroup))
8983                          (list (cons 'to-group ogroup))))
8984          group egroup groups vgroup)
8985     (dolist (article (gnus-summary-work-articles n))
8986       (setq group (format "%s-%d" gnus-newsgroup-name article))
8987       (gnus-summary-remove-process-mark article)
8988       (when (gnus-summary-display-article article)
8989         (save-excursion
8990           (with-temp-buffer
8991             (insert-buffer-substring gnus-original-article-buffer)
8992             ;; Remove some headers that may lead nndoc to make
8993             ;; the wrong guess.
8994             (message-narrow-to-head)
8995             (goto-char (point-min))
8996             (delete-matching-lines "^Path:\\|^From ")
8997             (widen)
8998             (if (setq egroup
8999                       (gnus-group-read-ephemeral-group
9000                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9001                                      (nndoc-article-type guess))
9002                        t nil t))
9003                 (progn
9004                   ;; Make all postings to this group go to the parent group.
9005                   (nconc (gnus-info-params (gnus-get-info egroup))
9006                          params)
9007                   (push egroup groups))
9008               ;; Couldn't select this doc group.
9009               (gnus-error 3 "Article couldn't be entered"))))))
9010     ;; Now we have selected all the documents.
9011     (cond
9012      ((not groups)
9013       (error "None of the articles could be interpreted as documents"))
9014      ((gnus-group-read-ephemeral-group
9015        (setq vgroup (format
9016                      "nnvirtual:%s-%s" gnus-newsgroup-name
9017                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9018        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9019        t
9020        (cons (current-buffer) 'summary)))
9021      (t
9022       (error "Couldn't select virtual nndoc group")))))
9023
9024 (defun gnus-summary-isearch-article (&optional regexp-p)
9025   "Do incremental search forward on the current article.
9026 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9027   (interactive "P")
9028   (gnus-summary-select-article)
9029   (gnus-configure-windows 'article)
9030   (gnus-eval-in-buffer-window gnus-article-buffer
9031     (save-restriction
9032       (widen)
9033       (isearch-forward regexp-p))))
9034
9035 (defun gnus-summary-repeat-search-article-forward ()
9036   "Repeat the previous search forwards."
9037   (interactive)
9038   (unless gnus-last-search-regexp
9039     (error "No previous search"))
9040   (gnus-summary-search-article-forward gnus-last-search-regexp))
9041
9042 (defun gnus-summary-repeat-search-article-backward ()
9043   "Repeat the previous search backwards."
9044   (interactive)
9045   (unless gnus-last-search-regexp
9046     (error "No previous search"))
9047   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9048
9049 (defun gnus-summary-search-article-forward (regexp &optional backward)
9050   "Search for an article containing REGEXP forward.
9051 If BACKWARD, search backward instead."
9052   (interactive
9053    (list (read-string
9054           (format "Search article %s (regexp%s): "
9055                   (if current-prefix-arg "backward" "forward")
9056                   (if gnus-last-search-regexp
9057                       (concat ", default " gnus-last-search-regexp)
9058                     "")))
9059          current-prefix-arg))
9060   (if (string-equal regexp "")
9061       (setq regexp (or gnus-last-search-regexp ""))
9062     (setq gnus-last-search-regexp regexp)
9063     (setq gnus-article-before-search gnus-current-article))
9064   ;; Intentionally set gnus-last-article.
9065   (setq gnus-last-article gnus-article-before-search)
9066   (let ((gnus-last-article gnus-last-article))
9067     (if (gnus-summary-search-article regexp backward)
9068         (gnus-summary-show-thread)
9069       (signal 'search-failed (list regexp)))))
9070
9071 (defun gnus-summary-search-article-backward (regexp)
9072   "Search for an article containing REGEXP backward."
9073   (interactive
9074    (list (read-string
9075           (format "Search article backward (regexp%s): "
9076                   (if gnus-last-search-regexp
9077                       (concat ", default " gnus-last-search-regexp)
9078                     "")))))
9079   (gnus-summary-search-article-forward regexp 'backward))
9080
9081 (defun gnus-summary-search-article (regexp &optional backward)
9082   "Search for an article containing REGEXP.
9083 Optional argument BACKWARD means do search for backward.
9084 `gnus-select-article-hook' is not called during the search."
9085   ;; We have to require this here to make sure that the following
9086   ;; dynamic binding isn't shadowed by autoloading.
9087   (require 'gnus-async)
9088   (require 'gnus-art)
9089   (let ((gnus-select-article-hook nil)  ;Disable hook.
9090         (gnus-article-prepare-hook nil)
9091         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9092         (gnus-use-article-prefetch nil)
9093         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9094         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9095         (gnus-visual nil)
9096         (gnus-keep-backlog nil)
9097         (gnus-break-pages nil)
9098         (gnus-summary-display-arrow nil)
9099         (gnus-updated-mode-lines nil)
9100         (gnus-auto-center-summary nil)
9101         (sum (current-buffer))
9102         (gnus-display-mime-function nil)
9103         (found nil)
9104         point)
9105     (gnus-save-hidden-threads
9106       (gnus-summary-select-article)
9107       (set-buffer gnus-article-buffer)
9108       (goto-char (window-point (get-buffer-window (current-buffer))))
9109       (when backward
9110         (forward-line -1))
9111       (while (not found)
9112         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9113         (if (if backward
9114                 (re-search-backward regexp nil t)
9115               (re-search-forward regexp nil t))
9116             ;; We found the regexp.
9117             (progn
9118               (setq found 'found)
9119               (beginning-of-line)
9120               (set-window-start
9121                (get-buffer-window (current-buffer))
9122                (point))
9123               (forward-line 1)
9124               (set-window-point
9125                (get-buffer-window (current-buffer))
9126                (point))
9127               (set-buffer sum)
9128               (setq point (point)))
9129           ;; We didn't find it, so we go to the next article.
9130           (set-buffer sum)
9131           (setq found 'not)
9132           (while (eq found 'not)
9133             (if (not (if backward (gnus-summary-find-prev)
9134                        (gnus-summary-find-next)))
9135                 ;; No more articles.
9136                 (setq found t)
9137               ;; Select the next article and adjust point.
9138               (unless (gnus-summary-article-sparse-p
9139                        (gnus-summary-article-number))
9140                 (setq found nil)
9141                 (gnus-summary-select-article)
9142                 (set-buffer gnus-article-buffer)
9143                 (widen)
9144                 (goto-char (if backward (point-max) (point-min))))))))
9145       (gnus-message 7 ""))
9146     ;; Return whether we found the regexp.
9147     (when (eq found 'found)
9148       (goto-char point)
9149       (gnus-summary-show-thread)
9150       (gnus-summary-goto-subject gnus-current-article)
9151       (gnus-summary-position-point)
9152       t)))
9153
9154 (defun gnus-find-matching-articles (header regexp)
9155   "Return a list of all articles that match REGEXP on HEADER.
9156 This search includes all articles in the current group that Gnus has
9157 fetched headers for, whether they are displayed or not."
9158   (let ((articles nil)
9159         ;; Can't eta-reduce because it's a macro.
9160         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9161         (case-fold-search t))
9162     (dolist (header gnus-newsgroup-headers)
9163       (when (string-match regexp (funcall func header))
9164         (push (mail-header-number header) articles)))
9165     (nreverse articles)))
9166
9167 (defun gnus-summary-find-matching (header regexp &optional backward unread
9168                                           not-case-fold not-matching)
9169   "Return a list of all articles that match REGEXP on HEADER.
9170 The search stars on the current article and goes forwards unless
9171 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9172 If UNREAD is non-nil, only unread articles will
9173 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9174 in the comparisons. If NOT-MATCHING, return a list of all articles that
9175 not match REGEXP on HEADER."
9176   (let ((case-fold-search (not not-case-fold))
9177         articles d func)
9178     (if (consp header)
9179         (if (eq (car header) 'extra)
9180             (setq func
9181                   `(lambda (h)
9182                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9183                          "")))
9184           (error "%s is an invalid header" header))
9185       (unless (fboundp (intern (concat "mail-header-" header)))
9186         (error "%s is not a valid header" header))
9187       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9188     (dolist (d (if (eq backward 'all)
9189                    gnus-newsgroup-data
9190                  (gnus-data-find-list
9191                   (gnus-summary-article-number)
9192                   (gnus-data-list backward))))
9193       (when (and (or (not unread)       ; We want all articles...
9194                      (gnus-data-unread-p d)) ; Or just unreads.
9195                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9196                  (if not-matching
9197                      (not (string-match
9198                            regexp
9199                            (funcall func (gnus-data-header d))))
9200                    (string-match regexp
9201                                  (funcall func (gnus-data-header d)))))
9202         (push (gnus-data-number d) articles))) ; Success!
9203     (nreverse articles)))
9204
9205 (defun gnus-summary-execute-command (header regexp command &optional backward)
9206   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9207 If HEADER is an empty string (or nil), the match is done on the entire
9208 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9209   (interactive
9210    (list (let ((completion-ignore-case t))
9211            (completing-read
9212             "Header name: "
9213             (mapcar (lambda (header) (list (format "%s" header)))
9214                     (append
9215                      '("Number" "Subject" "From" "Lines" "Date"
9216                        "Message-ID" "Xref" "References" "Body")
9217                      gnus-extra-headers))
9218             nil 'require-match))
9219          (read-string "Regexp: ")
9220          (read-key-sequence "Command: ")
9221          current-prefix-arg))
9222   (when (equal header "Body")
9223     (setq header ""))
9224   ;; Hidden thread subtrees must be searched as well.
9225   (gnus-summary-show-all-threads)
9226   ;; We don't want to change current point nor window configuration.
9227   (save-excursion
9228     (save-window-excursion
9229       (let (gnus-visual
9230             gnus-treat-strip-trailing-blank-lines
9231             gnus-treat-strip-leading-blank-lines
9232             gnus-treat-strip-multiple-blank-lines
9233             gnus-treat-hide-boring-headers
9234             gnus-treat-fold-newsgroups
9235             gnus-article-prepare-hook)
9236         (gnus-message 6 "Executing %s..." (key-description command))
9237         ;; We'd like to execute COMMAND interactively so as to give arguments.
9238         (gnus-execute header regexp
9239                       `(call-interactively ',(key-binding command))
9240                       backward)
9241         (gnus-message 6 "Executing %s...done" (key-description command))))))
9242
9243 (defun gnus-summary-beginning-of-article ()
9244   "Scroll the article back to the beginning."
9245   (interactive)
9246   (gnus-summary-select-article)
9247   (gnus-configure-windows 'article)
9248   (gnus-eval-in-buffer-window gnus-article-buffer
9249     (widen)
9250     (goto-char (point-min))
9251     (when gnus-break-pages
9252       (gnus-narrow-to-page))))
9253
9254 (defun gnus-summary-end-of-article ()
9255   "Scroll to the end of the article."
9256   (interactive)
9257   (gnus-summary-select-article)
9258   (gnus-configure-windows 'article)
9259   (gnus-eval-in-buffer-window gnus-article-buffer
9260     (widen)
9261     (goto-char (point-max))
9262     (recenter -3)
9263     (when gnus-break-pages
9264       (gnus-narrow-to-page))))
9265
9266 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9267   "Truncate to LEN and quote all \"(\"'s in STRING."
9268   (gnus-replace-in-string (if (and len (> (length string) len))
9269                               (substring string 0 len)
9270                             string)
9271                           "[()]" "\\\\\\&"))
9272
9273 (defun gnus-summary-print-article (&optional filename n)
9274   "Generate and print a PostScript image of the process-marked (mail) articles.
9275
9276 If used interactively, print the current article if none are
9277 process-marked.  With prefix arg, prompt the user for the name of the
9278 file to save in.
9279
9280 When used from Lisp, accept two optional args FILENAME and N.  N means
9281 to print the next N articles.  If N is negative, print the N previous
9282 articles.  If N is nil and articles have been marked with the process
9283 mark, print these instead.
9284
9285 If the optional first argument FILENAME is nil, send the image to the
9286 printer.  If FILENAME is a string, save the PostScript image in a file with
9287 that name.  If FILENAME is a number, prompt the user for the name of the file
9288 to save in."
9289   (interactive (list (ps-print-preprint current-prefix-arg)))
9290   (dolist (article (gnus-summary-work-articles n))
9291     (gnus-summary-select-article nil nil 'pseudo article)
9292     (gnus-eval-in-buffer-window gnus-article-buffer
9293       (gnus-print-buffer))
9294     (gnus-summary-remove-process-mark article))
9295   (ps-despool filename))
9296
9297 (defun gnus-print-buffer ()
9298   (let ((buffer (generate-new-buffer " *print*")))
9299     (unwind-protect
9300         (progn
9301           (copy-to-buffer buffer (point-min) (point-max))
9302           (set-buffer buffer)
9303           (gnus-remove-text-with-property 'gnus-decoration)
9304           (when (gnus-visual-p 'article-highlight 'highlight)
9305             ;; Copy-to-buffer doesn't copy overlay.  So redo
9306             ;; highlight.
9307             (let ((gnus-article-buffer buffer))
9308               (gnus-article-highlight-citation t)
9309               (gnus-article-highlight-signature)
9310               (gnus-article-emphasize)
9311               (gnus-article-delete-invisible-text)))
9312           (let ((ps-left-header
9313                  (list
9314                   (concat "("
9315                           (gnus-summary-print-truncate-and-quote
9316                            (mail-header-subject gnus-current-headers)
9317                            66) ")")
9318                   (concat "("
9319                           (gnus-summary-print-truncate-and-quote
9320                            (mail-header-from gnus-current-headers)
9321                            45) ")")))
9322                 (ps-right-header
9323                  (list
9324                   "/pagenumberstring load"
9325                   (concat "("
9326                           (mail-header-date gnus-current-headers) ")"))))
9327             (gnus-run-hooks 'gnus-ps-print-hook)
9328             (save-excursion
9329               (if ps-print-color-p
9330                   (ps-spool-buffer-with-faces)
9331                 (ps-spool-buffer)))))
9332       (kill-buffer buffer))))
9333
9334 (defun gnus-summary-show-article (&optional arg)
9335   "Force redisplaying of the current article.
9336 If ARG (the prefix) is a number, show the article with the charset
9337 defined in `gnus-summary-show-article-charset-alist', or the charset
9338 input.
9339 If ARG (the prefix) is non-nil and not a number, show the raw article
9340 without any article massaging functions being run.  Normally, the key
9341 strokes are `C-u g'."
9342   (interactive "P")
9343   (cond
9344    ((numberp arg)
9345     (gnus-summary-show-article t)
9346     (let ((gnus-newsgroup-charset
9347            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9348                (mm-read-coding-system
9349                 "View as charset: " ;; actually it is coding system.
9350                 (with-current-buffer gnus-article-buffer
9351                   (mm-detect-coding-region (point) (point-max))))))
9352           (gnus-newsgroup-ignored-charsets 'gnus-all))
9353       (gnus-summary-select-article nil 'force)
9354       (let ((deps gnus-newsgroup-dependencies)
9355             head header lines)
9356         (with-current-buffer gnus-original-article-buffer
9357           (save-restriction
9358             (message-narrow-to-head)
9359             (setq head (buffer-string))
9360             (goto-char (point-min))
9361             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9362               (goto-char (point-max))
9363               (widen)
9364               (setq lines (1- (count-lines (point) (point-max))))))
9365           (with-temp-buffer
9366             (insert (format "211 %d Article retrieved.\n"
9367                             (cdr gnus-article-current)))
9368             (insert head)
9369             (if lines (insert (format "Lines: %d\n" lines)))
9370             (insert ".\n")
9371             (let ((nntp-server-buffer (current-buffer)))
9372               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9373         (gnus-data-set-header
9374          (gnus-data-find (cdr gnus-article-current))
9375          header)
9376         (gnus-summary-update-article-line
9377          (cdr gnus-article-current) header)
9378         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9379           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9380    ((not arg)
9381     ;; Select the article the normal way.
9382     (gnus-summary-select-article nil 'force))
9383    (t
9384     ;; We have to require this here to make sure that the following
9385     ;; dynamic binding isn't shadowed by autoloading.
9386     (require 'gnus-async)
9387     (require 'gnus-art)
9388     ;; Bind the article treatment functions to nil.
9389     (let ((gnus-have-all-headers t)
9390           gnus-article-prepare-hook
9391           gnus-article-decode-hook
9392           gnus-display-mime-function
9393           gnus-break-pages)
9394       ;; Destroy any MIME parts.
9395       (when (gnus-buffer-live-p gnus-article-buffer)
9396         (with-current-buffer gnus-article-buffer
9397           (mm-destroy-parts gnus-article-mime-handles)
9398           ;; Set it to nil for safety reason.
9399           (setq gnus-article-mime-handle-alist nil)
9400           (setq gnus-article-mime-handles nil)))
9401       (gnus-summary-select-article nil 'force))))
9402   (gnus-summary-goto-subject gnus-current-article)
9403   (gnus-summary-position-point))
9404
9405 (defun gnus-summary-show-raw-article ()
9406   "Show the raw article without any article massaging functions being run."
9407   (interactive)
9408   (gnus-summary-show-article t))
9409
9410 (defun gnus-summary-verbose-headers (&optional arg)
9411   "Toggle permanent full header display.
9412 If ARG is a positive number, turn header display on.
9413 If ARG is a negative number, turn header display off."
9414   (interactive "P")
9415   (setq gnus-show-all-headers
9416         (cond ((or (not (numberp arg))
9417                    (zerop arg))
9418                (not gnus-show-all-headers))
9419               ((natnump arg)
9420                t)))
9421   (gnus-summary-show-article))
9422
9423 (defun gnus-summary-toggle-header (&optional arg)
9424   "Show the headers if they are hidden, or hide them if they are shown.
9425 If ARG is a positive number, show the entire header.
9426 If ARG is a negative number, hide the unwanted header lines."
9427   (interactive "P")
9428   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9429                      (get-buffer-window gnus-article-buffer t))))
9430     (with-current-buffer gnus-article-buffer
9431       (widen)
9432       (article-narrow-to-head)
9433       (let* ((inhibit-read-only t)
9434              (inhibit-point-motion-hooks t)
9435              (hidden (if (numberp arg)
9436                          (>= arg 0)
9437                        (or (not (looking-at "[^ \t\n]+:"))
9438                            (gnus-article-hidden-text-p 'headers))))
9439              s e)
9440         (delete-region (point-min) (point-max))
9441         (with-current-buffer gnus-original-article-buffer
9442           (goto-char (setq s (point-min)))
9443           (setq e (if (search-forward "\n\n" nil t)
9444                       (1- (point))
9445                     (point-max))))
9446         (insert-buffer-substring gnus-original-article-buffer s e)
9447         (run-hooks 'gnus-article-decode-hook)
9448         (if hidden
9449             (let ((gnus-treat-hide-headers nil)
9450                   (gnus-treat-hide-boring-headers nil))
9451               (gnus-delete-wash-type 'headers)
9452               (gnus-treat-article 'head))
9453           (gnus-treat-article 'head))
9454         (widen)
9455         (if window
9456             (set-window-start window (goto-char (point-min))))
9457         (if gnus-break-pages
9458             (gnus-narrow-to-page)
9459           (when (gnus-visual-p 'page-marker)
9460             (let ((inhibit-read-only t))
9461               (gnus-remove-text-with-property 'gnus-prev)
9462               (gnus-remove-text-with-property 'gnus-next))))
9463         (gnus-set-mode-line 'article)))))
9464
9465 (defun gnus-summary-show-all-headers ()
9466   "Make all header lines visible."
9467   (interactive)
9468   (gnus-summary-toggle-header 1))
9469
9470 (defun gnus-summary-caesar-message (&optional arg)
9471   "Caesar rotate the current article by 13.
9472 With a non-numerical prefix, also rotate headers.  A numerical
9473 prefix specifies how many places to rotate each letter forward."
9474   (interactive "P")
9475   (gnus-summary-select-article)
9476   (let ((mail-header-separator ""))
9477     (gnus-eval-in-buffer-window gnus-article-buffer
9478       (save-restriction
9479         (widen)
9480         (let ((start (window-start))
9481               (inhibit-read-only t))
9482           (if (equal arg '(4))
9483               (message-caesar-buffer-body nil t)
9484             (message-caesar-buffer-body arg))
9485           (set-window-start (get-buffer-window (current-buffer)) start)))))
9486   ;; Create buttons and stuff...
9487   (gnus-treat-article nil))
9488
9489 (declare-function idna-to-unicode "ext:idna" (str))
9490
9491 (defun gnus-summary-idna-message (&optional arg)
9492   "Decode IDNA encoded domain names in the current articles.
9493 IDNA encoded domain names looks like `xn--bar'.  If a string
9494 remain unencoded after running this function, it is likely an
9495 invalid IDNA string (`xn--bar' is invalid).
9496
9497 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9498 installed for this command to work."
9499   (interactive "P")
9500   (if (not (and (condition-case nil (require 'idna)
9501                   (file-error))
9502                 (mm-coding-system-p 'utf-8)
9503                 (executable-find (symbol-value 'idna-program))))
9504       (gnus-message
9505        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9506     (gnus-summary-select-article)
9507     (let ((mail-header-separator ""))
9508       (gnus-eval-in-buffer-window gnus-article-buffer
9509         (save-restriction
9510           (widen)
9511           (let ((start (window-start))
9512                 buffer-read-only)
9513             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9514               (replace-match (idna-to-unicode (match-string 1))))
9515             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9516
9517 (defun gnus-summary-morse-message (&optional arg)
9518   "Morse decode the current article."
9519   (interactive "P")
9520   (gnus-summary-select-article)
9521   (let ((mail-header-separator ""))
9522     (gnus-eval-in-buffer-window gnus-article-buffer
9523       (save-excursion
9524         (save-restriction
9525           (widen)
9526           (let ((pos (window-start))
9527                 (inhibit-read-only t))
9528             (goto-char (point-min))
9529             (when (message-goto-body)
9530               (gnus-narrow-to-body))
9531             (goto-char (point-min))
9532             (while (search-forward "·" (point-max) t)
9533               (replace-match "."))
9534             (unmorse-region (point-min) (point-max))
9535             (widen)
9536             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9537
9538 (defun gnus-summary-stop-page-breaking ()
9539   "Stop page breaking in the current article."
9540   (interactive)
9541   (gnus-summary-select-article)
9542   (gnus-eval-in-buffer-window gnus-article-buffer
9543     (widen)
9544     (when (gnus-visual-p 'page-marker)
9545       (let ((inhibit-read-only t))
9546         (gnus-remove-text-with-property 'gnus-prev)
9547         (gnus-remove-text-with-property 'gnus-next))
9548       (setq gnus-page-broken nil))))
9549
9550 (defun gnus-summary-move-article (&optional n to-newsgroup
9551                                             select-method action)
9552   "Move the current article to a different newsgroup.
9553 If N is a positive number, move the N next articles.
9554 If N is a negative number, move the N previous articles.
9555 If N is nil and any articles have been marked with the process mark,
9556 move those articles instead.
9557 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9558 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9559 re-spool using this method.
9560
9561 When called interactively with TO-NEWSGROUP being nil, the value of
9562 the variable `gnus-move-split-methods' is used for finding a default
9563 for the target newsgroup.
9564
9565 For this function to work, both the current newsgroup and the
9566 newsgroup that you want to move to have to support the `request-move'
9567 and `request-accept' functions.
9568
9569 ACTION can be either `move' (the default), `crosspost' or `copy'."
9570   (interactive "P")
9571   (unless action
9572     (setq action 'move))
9573   ;; Check whether the source group supports the required functions.
9574   (cond ((and (eq action 'move)
9575               (not (gnus-check-backend-function
9576                     'request-move-article gnus-newsgroup-name)))
9577          (error "The current group does not support article moving"))
9578         ((and (eq action 'crosspost)
9579               (not (gnus-check-backend-function
9580                     'request-replace-article gnus-newsgroup-name)))
9581          (error "The current group does not support article editing")))
9582   (let ((articles (gnus-summary-work-articles n))
9583         (prefix (if (gnus-check-backend-function
9584                      'request-move-article gnus-newsgroup-name)
9585                     (funcall gnus-move-group-prefix-function
9586                              gnus-newsgroup-name)
9587                   ""))
9588         (names '((move "Move" "Moving")
9589                  (copy "Copy" "Copying")
9590                  (crosspost "Crosspost" "Crossposting")))
9591         (copy-buf (save-excursion
9592                     (nnheader-set-temp-buffer " *copy article*")))
9593         art-group to-method new-xref article to-groups
9594         articles-to-update-marks encoded)
9595     (unless (assq action names)
9596       (error "Unknown action %s" action))
9597     ;; Read the newsgroup name.
9598     (when (and (not to-newsgroup)
9599                (not select-method))
9600       (if (and gnus-move-split-methods
9601                (not
9602                 (and (memq gnus-current-article articles)
9603                      (gnus-buffer-live-p gnus-original-article-buffer))))
9604           ;; When `gnus-move-split-methods' is non-nil, we have to
9605           ;; select an article to give `gnus-read-move-group-name' an
9606           ;; opportunity to suggest an appropriate default.  However,
9607           ;; we needn't render or mark the article.
9608           (let ((gnus-display-mime-function nil)
9609                 (gnus-article-prepare-hook nil)
9610                 (gnus-mark-article-hook nil))
9611             (gnus-summary-select-article nil nil nil (car articles))))
9612       (setq to-newsgroup (gnus-read-move-group-name
9613                           (cadr (assq action names))
9614                           (symbol-value
9615                            (intern (format "gnus-current-%s-group" action)))
9616                           articles prefix)
9617             encoded to-newsgroup
9618             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9619       (set (intern (format "gnus-current-%s-group" action))
9620            (mm-decode-coding-string
9621             to-newsgroup
9622             (gnus-group-name-charset to-method to-newsgroup))))
9623     (unless to-method
9624       (setq to-method (or select-method
9625                           (gnus-server-to-method
9626                            (gnus-group-method to-newsgroup)))))
9627     (setq to-newsgroup
9628           (or encoded
9629               (and to-newsgroup
9630                    (mm-encode-coding-string
9631                     to-newsgroup
9632                     (gnus-group-name-charset to-method to-newsgroup)))))
9633     ;; Check the method we are to move this article to...
9634     (unless (gnus-check-backend-function
9635              'request-accept-article (car to-method))
9636       (error "%s does not support article copying" (car to-method)))
9637     (unless (gnus-check-server to-method)
9638       (error "Can't open server %s" (car to-method)))
9639     (gnus-message 6 "%s to %s: %s..."
9640                   (caddr (assq action names))
9641                   (or (car select-method)
9642                       (gnus-group-decoded-name to-newsgroup))
9643                   articles)
9644     (while articles
9645       (setq article (pop articles))
9646       (setq
9647        art-group
9648        (cond
9649         ;; Move the article.
9650         ((eq action 'move)
9651          ;; Remove this article from future suppression.
9652          (gnus-dup-unsuppress-article article)
9653          (let* ((from-method (gnus-find-method-for-group
9654                               gnus-newsgroup-name))
9655                 (to-method (or select-method
9656                                (gnus-find-method-for-group to-newsgroup)))
9657                 (move-is-internal (gnus-method-equal from-method to-method)))
9658            (gnus-request-move-article
9659             article                     ; Article to move
9660             gnus-newsgroup-name         ; From newsgroup
9661             (nth 1 (gnus-find-method-for-group
9662                     gnus-newsgroup-name)) ; Server
9663             (list 'gnus-request-accept-article
9664                   to-newsgroup (list 'quote select-method)
9665                   (not articles) t)     ; Accept form
9666             (not articles)              ; Only save nov last time
9667             move-is-internal)))         ; is this move internal?
9668         ;; Copy the article.
9669         ((eq action 'copy)
9670          (with-current-buffer copy-buf
9671            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9672              (save-restriction
9673                (nnheader-narrow-to-headers)
9674                (dolist (hdr gnus-copy-article-ignored-headers)
9675                  (message-remove-header hdr t)))
9676              (gnus-request-accept-article
9677               to-newsgroup select-method (not articles) t))))
9678         ;; Crosspost the article.
9679         ((eq action 'crosspost)
9680          (let ((xref (message-tokenize-header
9681                       (mail-header-xref (gnus-summary-article-header article))
9682                       " ")))
9683            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9684                                   ":" (number-to-string article)))
9685            (unless xref
9686              (setq xref (list (system-name))))
9687            (setq new-xref
9688                  (concat
9689                   (mapconcat 'identity
9690                              (delete "Xref:" (delete new-xref xref))
9691                              " ")
9692                   " " new-xref))
9693            (with-current-buffer copy-buf
9694              ;; First put the article in the destination group.
9695              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9696              (when (consp (setq art-group
9697                                 (gnus-request-accept-article
9698                                  to-newsgroup select-method (not articles) t)))
9699                (setq new-xref (concat new-xref " " (car art-group)
9700                                       ":"
9701                                       (number-to-string (cdr art-group))))
9702                ;; Now we have the new Xrefs header, so we insert
9703                ;; it and replace the new article.
9704                (nnheader-replace-header "Xref" new-xref)
9705                (gnus-request-replace-article
9706                 (cdr art-group) to-newsgroup (current-buffer) t)
9707                art-group))))))
9708       (cond
9709        ((not art-group)
9710         (gnus-message 1 "Couldn't %s article %s: %s"
9711                       (cadr (assq action names)) article
9712                       (nnheader-get-report (car to-method))))
9713        ((eq art-group 'junk)
9714         (when (eq action 'move)
9715           (gnus-summary-mark-article article gnus-canceled-mark)
9716           (gnus-message 4 "Deleted article %s" article)
9717           ;; run the delete hook
9718           (run-hook-with-args 'gnus-summary-article-delete-hook
9719                               action
9720                               (gnus-data-header
9721                                (assoc article (gnus-data-list nil)))
9722                               gnus-newsgroup-name nil
9723                               select-method)))
9724        (t
9725         (let* ((pto-group (gnus-group-prefixed-name
9726                            (car art-group) to-method))
9727                (info (gnus-get-info pto-group))
9728                (to-group (gnus-info-group info))
9729                to-marks)
9730           ;; Update the group that has been moved to.
9731           (when (and info
9732                      (memq action '(move copy)))
9733             (unless (member to-group to-groups)
9734               (push to-group to-groups))
9735
9736             (unless (memq article gnus-newsgroup-unreads)
9737               (push 'read to-marks)
9738               (gnus-info-set-read
9739                info (gnus-add-to-range (gnus-info-read info)
9740                                        (list (cdr art-group)))))
9741
9742             ;; See whether the article is to be put in the cache.
9743             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9744                              gnus-article-mark-lists
9745                            (delete '(expirable . expire)
9746                                    (copy-sequence gnus-article-mark-lists))))
9747                   (to-article (cdr art-group)))
9748
9749               ;; Enter the article into the cache in the new group,
9750               ;; if that is required.
9751               (when gnus-use-cache
9752                 (gnus-cache-possibly-enter-article
9753                  to-group to-article
9754                  (memq article gnus-newsgroup-marked)
9755                  (memq article gnus-newsgroup-dormant)
9756                  (memq article gnus-newsgroup-unreads)))
9757
9758               (when gnus-preserve-marks
9759                 ;; Copy any marks over to the new group.
9760                 (when (and (equal to-group gnus-newsgroup-name)
9761                            (not (memq article gnus-newsgroup-unreads)))
9762                   ;; Mark this article as read in this group.
9763                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9764                   (setcdr (gnus-active to-group) to-article)
9765                   (setcdr gnus-newsgroup-active to-article))
9766
9767                 (while marks
9768                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9769                     (when (memq article (symbol-value
9770                                          (intern (format "gnus-newsgroup-%s"
9771                                                          (caar marks)))))
9772                       (push (cdar marks) to-marks)
9773                       ;; If the other group is the same as this group,
9774                       ;; then we have to add the mark to the list.
9775                       (when (equal to-group gnus-newsgroup-name)
9776                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9777                              (cons to-article
9778                                    (symbol-value
9779                                     (intern (format "gnus-newsgroup-%s"
9780                                                     (caar marks)))))))
9781                       ;; Copy the marks to other group.
9782                       (gnus-add-marked-articles
9783                        to-group (cdar marks) (list to-article) info)))
9784                   (setq marks (cdr marks)))
9785
9786                 (gnus-request-set-mark
9787                  to-group (list (list (list to-article) 'add to-marks))))
9788
9789               (gnus-dribble-enter
9790                (concat "(gnus-group-set-info '"
9791                        (gnus-prin1-to-string (gnus-get-info to-group))
9792                        ")"))))
9793
9794           ;; Update the Xref header in this article to point to
9795           ;; the new crossposted article we have just created.
9796           (when (eq action 'crosspost)
9797             (with-current-buffer copy-buf
9798               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9799               (nnheader-replace-header "Xref" new-xref)
9800               (gnus-request-replace-article
9801                article gnus-newsgroup-name (current-buffer) t)))
9802
9803           ;; run the move/copy/crosspost/respool hook
9804           (run-hook-with-args 'gnus-summary-article-move-hook
9805                               action
9806                               (gnus-data-header
9807                                (assoc article (gnus-data-list nil)))
9808                               gnus-newsgroup-name
9809                               to-newsgroup
9810                               select-method))
9811
9812         ;;;!!!Why is this necessary?
9813         (set-buffer gnus-summary-buffer)
9814
9815         (gnus-summary-goto-subject article)
9816         (when (eq action 'move)
9817           (gnus-summary-mark-article article gnus-canceled-mark))))
9818       (push article articles-to-update-marks))
9819
9820     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9821     ;; Re-activate all groups that have been moved to.
9822     (with-current-buffer gnus-group-buffer
9823       (let ((gnus-group-marked to-groups))
9824         (gnus-group-get-new-news-this-group nil t)))
9825
9826     (gnus-kill-buffer copy-buf)
9827     (gnus-summary-position-point)
9828     (gnus-set-mode-line 'summary)))
9829
9830 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9831   "Copy the current article to some other group.
9832 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9833 When called interactively, if TO-NEWSGROUP is nil, use the value of
9834 the variable `gnus-move-split-methods' for finding a default target
9835 newsgroup.
9836 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9837 re-spool using this method."
9838   (interactive "P")
9839   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9840
9841 (defun gnus-summary-crosspost-article (&optional n)
9842   "Crosspost the current article to some other group."
9843   (interactive "P")
9844   (gnus-summary-move-article n nil nil 'crosspost))
9845
9846 (defcustom gnus-summary-respool-default-method nil
9847   "Default method type for respooling an article.
9848 If nil, use to the current newsgroup method."
9849   :type 'symbol
9850   :group 'gnus-summary-mail)
9851
9852 (defun gnus-summary-respool-article (&optional n method)
9853   "Respool the current article.
9854 The article will be squeezed through the mail spooling process again,
9855 which means that it will be put in some mail newsgroup or other
9856 depending on `nnmail-split-methods'.
9857 If N is a positive number, respool the N next articles.
9858 If N is a negative number, respool the N previous articles.
9859 If N is nil and any articles have been marked with the process mark,
9860 respool those articles instead.
9861
9862 Respooling can be done both from mail groups and \"real\" newsgroups.
9863 In the former case, the articles in question will be moved from the
9864 current group into whatever groups they are destined to.  In the
9865 latter case, they will be copied into the relevant groups."
9866   (interactive
9867    (list current-prefix-arg
9868          (let* ((methods (gnus-methods-using 'respool))
9869                 (methname
9870                  (symbol-name (or gnus-summary-respool-default-method
9871                                   (car (gnus-find-method-for-group
9872                                         gnus-newsgroup-name)))))
9873                 (method
9874                  (gnus-completing-read-with-default
9875                   methname "Backend to use when respooling"
9876                   methods nil t nil 'gnus-mail-method-history))
9877                 ms)
9878            (cond
9879             ((zerop (length (setq ms (gnus-servers-using-backend
9880                                       (intern method)))))
9881              (list (intern method) ""))
9882             ((= 1 (length ms))
9883              (car ms))
9884             (t
9885              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9886                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9887                            ms-alist))))))))
9888   (unless method
9889     (error "No method given for respooling"))
9890   (if (assoc (symbol-name
9891               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9892              (gnus-methods-using 'respool))
9893       (gnus-summary-move-article n nil method)
9894     (gnus-summary-copy-article n nil method)))
9895
9896 (defun gnus-summary-import-article (file &optional edit)
9897   "Import an arbitrary file into a mail newsgroup."
9898   (interactive "fImport file: \nP")
9899   (let ((group gnus-newsgroup-name)
9900         (now (current-time))
9901         atts lines group-art)
9902     (unless (gnus-check-backend-function 'request-accept-article group)
9903       (error "%s does not support article importing" group))
9904     (or (file-readable-p file)
9905         (not (file-regular-p file))
9906         (error "Can't read %s" file))
9907     (with-current-buffer (gnus-get-buffer-create " *import file*")
9908       (erase-buffer)
9909       (nnheader-insert-file-contents file)
9910       (goto-char (point-min))
9911       (if (nnheader-article-p)
9912           (save-restriction
9913             (goto-char (point-min))
9914             (search-forward "\n\n" nil t)
9915             (narrow-to-region (point-min) (1- (point)))
9916             (goto-char (point-min))
9917             (unless (re-search-forward "^date:" nil t)
9918               (goto-char (point-max))
9919               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9920        ;; This doesn't look like an article, so we fudge some headers.
9921         (setq atts (file-attributes file)
9922               lines (count-lines (point-min) (point-max)))
9923         (insert "From: " (read-string "From: ") "\n"
9924                 "Subject: " (read-string "Subject: ") "\n"
9925                 "Date: " (message-make-date (nth 5 atts)) "\n"
9926                 "Message-ID: " (message-make-message-id) "\n"
9927                 "Lines: " (int-to-string lines) "\n"
9928                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9929       (setq group-art (gnus-request-accept-article group nil t))
9930       (kill-buffer (current-buffer)))
9931     (setq gnus-newsgroup-active (gnus-activate-group group))
9932     (forward-line 1)
9933     (gnus-summary-goto-article (cdr group-art) nil t)
9934     (when edit
9935       (gnus-summary-edit-article))))
9936
9937 (defun gnus-summary-create-article ()
9938   "Create an article in a mail newsgroup."
9939   (interactive)
9940   (let ((group gnus-newsgroup-name)
9941         (now (current-time))
9942         group-art)
9943     (unless (gnus-check-backend-function 'request-accept-article group)
9944       (error "%s does not support article importing" group))
9945     (with-current-buffer (gnus-get-buffer-create " *import file*")
9946       (erase-buffer)
9947       (goto-char (point-min))
9948       ;; This doesn't look like an article, so we fudge some headers.
9949       (insert "From: " (read-string "From: ") "\n"
9950               "Subject: " (read-string "Subject: ") "\n"
9951               "Date: " (message-make-date now) "\n"
9952               "Message-ID: " (message-make-message-id) "\n")
9953       (setq group-art (gnus-request-accept-article group nil t))
9954       (kill-buffer (current-buffer)))
9955     (setq gnus-newsgroup-active (gnus-activate-group group))
9956     (forward-line 1)
9957     (gnus-summary-goto-article (cdr group-art) nil t)
9958     (gnus-summary-edit-article)))
9959
9960 (defun gnus-summary-article-posted-p ()
9961   "Say whether the current (mail) article is available from news as well.
9962 This will be the case if the article has both been mailed and posted."
9963   (interactive)
9964   (let ((id (mail-header-references (gnus-summary-article-header)))
9965         (gnus-override-method (car (gnus-refer-article-methods))))
9966     (if (gnus-request-head id "")
9967         (gnus-message 2 "The current message was found on %s"
9968                       gnus-override-method)
9969       (gnus-message 2 "The current message couldn't be found on %s"
9970                     gnus-override-method)
9971       nil)))
9972
9973 (defun gnus-summary-expire-articles (&optional now)
9974   "Expire all articles that are marked as expirable in the current group."
9975   (interactive)
9976   (when (and (not gnus-group-is-exiting-without-update-p)
9977              (gnus-check-backend-function
9978               'request-expire-articles gnus-newsgroup-name))
9979     ;; This backend supports expiry.
9980     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9981            (expirable (if total
9982                           (progn
9983                             ;; We need to update the info for
9984                             ;; this group for `gnus-list-of-read-articles'
9985                             ;; to give us the right answer.
9986                             (gnus-run-hooks 'gnus-exit-group-hook)
9987                             (gnus-summary-update-info)
9988                             (gnus-list-of-read-articles gnus-newsgroup-name))
9989                         (setq gnus-newsgroup-expirable
9990                               (sort gnus-newsgroup-expirable '<))))
9991            (expiry-wait (if now 'immediate
9992                           (gnus-group-find-parameter
9993                            gnus-newsgroup-name 'expiry-wait)))
9994            (nnmail-expiry-target
9995             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9996                 nnmail-expiry-target))
9997            es)
9998       (when expirable
9999         ;; There are expirable articles in this group, so we run them
10000         ;; through the expiry process.
10001         (gnus-message 6 "Expiring articles...")
10002         (unless (gnus-check-group gnus-newsgroup-name)
10003           (error "Can't open server for %s" gnus-newsgroup-name))
10004         ;; The list of articles that weren't expired is returned.
10005         (save-excursion
10006           (if expiry-wait
10007               (let ((nnmail-expiry-wait-function nil)
10008                     (nnmail-expiry-wait expiry-wait))
10009                 (setq es (gnus-request-expire-articles
10010                           expirable gnus-newsgroup-name)))
10011             (setq es (gnus-request-expire-articles
10012                       expirable gnus-newsgroup-name)))
10013           (unless total
10014             (setq gnus-newsgroup-expirable es))
10015           ;; We go through the old list of expirable, and mark all
10016           ;; really expired articles as nonexistent.
10017           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10018             (let ((gnus-use-cache nil))
10019               (dolist (article expirable)
10020                 (when (and (not (memq article es))
10021                            (gnus-data-find article))
10022                   (gnus-summary-mark-article article gnus-canceled-mark)
10023                   (run-hook-with-args 'gnus-summary-article-expire-hook
10024                                       'delete
10025                                       (gnus-data-header
10026                                        (assoc article (gnus-data-list nil)))
10027                                       gnus-newsgroup-name
10028                                       nil
10029                                       nil))))))
10030         (gnus-message 6 "Expiring articles...done")))))
10031
10032 (defun gnus-summary-expire-articles-now ()
10033   "Expunge all expirable articles in the current group.
10034 This means that *all* articles that are marked as expirable will be
10035 deleted forever, right now."
10036   (interactive)
10037   (or gnus-expert-user
10038       (gnus-yes-or-no-p
10039        "Are you really, really sure you want to delete all expirable messages? ")
10040       (error "Phew!"))
10041   (gnus-summary-expire-articles t))
10042
10043 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10044 (defun gnus-summary-delete-article (&optional n)
10045   "Delete the N next (mail) articles.
10046 This command actually deletes articles.  This is not a marking
10047 command.  The article will disappear forever from your life, never to
10048 return.
10049
10050 If N is negative, delete backwards.
10051 If N is nil and articles have been marked with the process mark,
10052 delete these instead.
10053
10054 If `gnus-novice-user' is non-nil you will be asked for
10055 confirmation before the articles are deleted."
10056   (interactive "P")
10057   (unless (gnus-check-backend-function 'request-expire-articles
10058                                        gnus-newsgroup-name)
10059     (error "The current newsgroup does not support article deletion"))
10060   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10061     (error "Couldn't open server"))
10062   ;; Compute the list of articles to delete.
10063   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10064         (nnmail-expiry-target 'delete)
10065         not-deleted)
10066     (if (and gnus-novice-user
10067              (not (gnus-yes-or-no-p
10068                    (format "Do you really want to delete %s forever? "
10069                            (if (> (length articles) 1)
10070                                (format "these %s articles" (length articles))
10071                              "this article")))))
10072         ()
10073       ;; Delete the articles.
10074       (setq not-deleted (gnus-request-expire-articles
10075                          articles gnus-newsgroup-name 'force))
10076       (while articles
10077         (gnus-summary-remove-process-mark (car articles))
10078         ;; The backend might not have been able to delete the article
10079         ;; after all.
10080         (unless (memq (car articles) not-deleted)
10081           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10082         (let* ((article (car articles))
10083                (ghead  (gnus-data-header
10084                                     (assoc article (gnus-data-list nil)))))
10085           (run-hook-with-args 'gnus-summary-article-delete-hook
10086                               'delete ghead gnus-newsgroup-name nil
10087                               nil))
10088         (setq articles (cdr articles)))
10089       (when not-deleted
10090         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10091     (gnus-summary-position-point)
10092     (gnus-set-mode-line 'summary)
10093     not-deleted))
10094
10095 (defun gnus-summary-edit-article (&optional arg)
10096   "Edit the current article.
10097 This will have permanent effect only in mail groups.
10098 If ARG is nil, edit the decoded articles.
10099 If ARG is 1, edit the raw articles.
10100 If ARG is 2, edit the raw articles even in read-only groups.
10101 If ARG is 3, edit the articles with the current handles.
10102 Otherwise, allow editing of articles even in read-only
10103 groups."
10104   (interactive "P")
10105   (let (force raw current-handles)
10106     (cond
10107      ((null arg))
10108      ((eq arg 1)
10109       (setq raw t))
10110      ((eq arg 2)
10111       (setq raw t
10112             force t))
10113      ((eq arg 3)
10114       (setq current-handles
10115             (and (gnus-buffer-live-p gnus-article-buffer)
10116                  (with-current-buffer gnus-article-buffer
10117                    (prog1
10118                        gnus-article-mime-handles
10119                      (setq gnus-article-mime-handles nil))))))
10120      (t
10121       (setq force t)))
10122     (when (and raw (not force)
10123                (member gnus-newsgroup-name '("nndraft:delayed"
10124                                              "nndraft:drafts"
10125                                              "nndraft:queue")))
10126       (error "Can't edit the raw article in group %s"
10127              gnus-newsgroup-name))
10128     (with-current-buffer gnus-summary-buffer
10129       (let ((mail-parse-charset gnus-newsgroup-charset)
10130             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10131         (gnus-set-global-variables)
10132         (when (and (not force)
10133                    (gnus-group-read-only-p))
10134           (error "The current newsgroup does not support article editing"))
10135         (gnus-summary-show-article t)
10136         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10137           (with-current-buffer gnus-article-buffer
10138             (mm-enable-multibyte)))
10139         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10140             (setq raw t))
10141         (gnus-article-edit-article
10142          (if raw 'ignore
10143            `(lambda ()
10144               (let ((mbl mml-buffer-list))
10145                 (setq mml-buffer-list nil)
10146                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10147                   (mime-to-mml ,'current-handles))
10148                 (let ((mbl1 mml-buffer-list))
10149                   (setq mml-buffer-list mbl)
10150                   (set (make-local-variable 'mml-buffer-list) mbl1))
10151                 (gnus-make-local-hook 'kill-buffer-hook)
10152                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10153          `(lambda (no-highlight)
10154             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10155                   (message-options message-options)
10156                   (message-options-set-recipient)
10157                   (mail-parse-ignored-charsets
10158                    ',gnus-newsgroup-ignored-charsets)
10159                   (rfc2047-header-encoding-alist
10160                    ',(let ((charset (gnus-group-name-charset
10161                                      (gnus-find-method-for-group
10162                                       gnus-newsgroup-name)
10163                                      gnus-newsgroup-name)))
10164                        (append (list (cons "Newsgroups" charset)
10165                                      (cons "Followup-To" charset)
10166                                      (cons "Xref" charset))
10167                                rfc2047-header-encoding-alist))))
10168               ,(if (not raw) '(progn
10169                                 (mml-to-mime)
10170                                 (mml-destroy-buffers)
10171                                 (remove-hook 'kill-buffer-hook
10172                                              'mml-destroy-buffers t)
10173                                 (kill-local-variable 'mml-buffer-list)))
10174               (gnus-summary-edit-article-done
10175                ,(or (mail-header-references gnus-current-headers) "")
10176                ,(gnus-group-read-only-p)
10177                ,gnus-summary-buffer no-highlight))))))))
10178
10179 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10180
10181 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10182                                                  no-highlight)
10183   "Make edits to the current article permanent."
10184   (interactive)
10185   (save-excursion
10186    ;; The buffer restriction contains the entire article if it exists.
10187     (when (article-goto-body)
10188       (let ((lines (count-lines (point) (point-max)))
10189             (length (- (point-max) (point)))
10190             (case-fold-search t)
10191             (body (copy-marker (point))))
10192         (goto-char (point-min))
10193         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10194           (delete-region (match-beginning 1) (match-end 1))
10195           (insert (number-to-string length)))
10196         (goto-char (point-min))
10197         (when (re-search-forward
10198                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10199           (delete-region (match-beginning 1) (match-end 1))
10200           (insert (number-to-string length)))
10201         (goto-char (point-min))
10202         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10203           (delete-region (match-beginning 1) (match-end 1))
10204           (insert (number-to-string lines))))))
10205   ;; Replace the article.
10206   (let ((buf (current-buffer)))
10207     (with-temp-buffer
10208       (insert-buffer-substring buf)
10209
10210       (if (and (not read-only)
10211                (not (gnus-request-replace-article
10212                      (cdr gnus-article-current) (car gnus-article-current)
10213                      (current-buffer) t)))
10214           (error "Couldn't replace article")
10215         ;; Update the summary buffer.
10216         (if (and references
10217                  (equal (message-tokenize-header references " ")
10218                         (message-tokenize-header
10219                          (or (message-fetch-field "references") "") " ")))
10220             ;; We only have to update this line.
10221             (save-excursion
10222               (save-restriction
10223                 (message-narrow-to-head)
10224                 (let ((head (buffer-substring-no-properties
10225                              (point-min) (point-max)))
10226                       header)
10227                   (with-temp-buffer
10228                     (insert (format "211 %d Article retrieved.\n"
10229                                     (cdr gnus-article-current)))
10230                     (insert head)
10231                     (insert ".\n")
10232                     (let ((nntp-server-buffer (current-buffer)))
10233                       (setq header (car (gnus-get-newsgroup-headers
10234                                          nil t))))
10235                     (with-current-buffer gnus-summary-buffer
10236                       (gnus-data-set-header
10237                        (gnus-data-find (cdr gnus-article-current))
10238                        header)
10239                       (gnus-summary-update-article-line
10240                        (cdr gnus-article-current) header)
10241                       (if (gnus-summary-goto-subject
10242                            (cdr gnus-article-current) nil t)
10243                           (gnus-summary-update-secondary-mark
10244                            (cdr gnus-article-current))))))))
10245           ;; Update threads.
10246           (set-buffer (or buffer gnus-summary-buffer))
10247           (gnus-summary-update-article (cdr gnus-article-current))
10248           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10249               (gnus-summary-update-secondary-mark
10250                (cdr gnus-article-current))))
10251         ;; Prettify the article buffer again.
10252         (unless no-highlight
10253           (with-current-buffer gnus-article-buffer
10254             ;;;!!! Fix this -- article should be rehighlighted.
10255             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10256             (set-buffer gnus-original-article-buffer)
10257             (gnus-request-article
10258              (cdr gnus-article-current)
10259              (car gnus-article-current) (current-buffer))))
10260         ;; Prettify the summary buffer line.
10261         (when (gnus-visual-p 'summary-highlight 'highlight)
10262           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10263
10264 (defun gnus-summary-edit-wash (key)
10265   "Perform editing command KEY in the article buffer."
10266   (interactive
10267    (list
10268     (progn
10269       (message "%s" (concat (this-command-keys) "- "))
10270       (read-char))))
10271   (message "")
10272   (gnus-summary-edit-article)
10273   (execute-kbd-macro (concat (this-command-keys) key))
10274   (gnus-article-edit-done))
10275
10276 ;;; Respooling
10277
10278 (defun gnus-summary-respool-query (&optional silent trace)
10279   "Query where the respool algorithm would put this article."
10280   (interactive)
10281   (let (gnus-mark-article-hook)
10282     (gnus-summary-select-article)
10283     (with-current-buffer gnus-original-article-buffer
10284       (let ((groups (nnmail-article-group 'identity trace)))
10285         (unless silent
10286           (if groups
10287               (message "This message would go to %s"
10288                        (mapconcat 'car groups ", "))
10289             (message "This message would go to no groups"))
10290           groups)))))
10291
10292 (defun gnus-summary-respool-trace ()
10293   "Trace where the respool algorithm would put this article.
10294 Display a buffer showing all fancy splitting patterns which matched."
10295   (interactive)
10296   (gnus-summary-respool-query nil t))
10297
10298 ;; Summary marking commands.
10299
10300 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10301   "Mark articles which has the same subject as read, and then select the next.
10302 If UNMARK is positive, remove any kind of mark.
10303 If UNMARK is negative, tick articles."
10304   (interactive "P")
10305   (when unmark
10306     (setq unmark (prefix-numeric-value unmark)))
10307   (let ((count
10308          (gnus-summary-mark-same-subject
10309           (gnus-summary-article-subject) unmark)))
10310     ;; Select next unread article.  If auto-select-same mode, should
10311     ;; select the first unread article.
10312     (gnus-summary-next-article t (and gnus-auto-select-same
10313                                       (gnus-summary-article-subject)))
10314     (gnus-message 7 "%d article%s marked as %s"
10315                   count (if (= count 1) " is" "s are")
10316                   (if unmark "unread" "read"))))
10317
10318 (defun gnus-summary-kill-same-subject (&optional unmark)
10319   "Mark articles which has the same subject as read.
10320 If UNMARK is positive, remove any kind of mark.
10321 If UNMARK is negative, tick articles."
10322   (interactive "P")
10323   (when unmark
10324     (setq unmark (prefix-numeric-value unmark)))
10325   (let ((count
10326          (gnus-summary-mark-same-subject
10327           (gnus-summary-article-subject) unmark)))
10328     ;; If marked as read, go to next unread subject.
10329     (when (null unmark)
10330       ;; Go to next unread subject.
10331       (gnus-summary-next-subject 1 t))
10332     (gnus-message 7 "%d articles are marked as %s"
10333                   count (if unmark "unread" "read"))))
10334
10335 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10336   "Mark articles with same SUBJECT as read, and return marked number.
10337 If optional argument UNMARK is positive, remove any kinds of marks.
10338 If optional argument UNMARK is negative, mark articles as unread instead."
10339   (let ((count 1))
10340     (save-excursion
10341       (cond
10342        ((null unmark)                   ; Mark as read.
10343         (while (and
10344                 (progn
10345                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10346                   (gnus-summary-show-thread) t)
10347                 (gnus-summary-find-subject subject))
10348           (setq count (1+ count))))
10349        ((> unmark 0)                    ; Tick.
10350         (while (and
10351                 (progn
10352                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10353                   (gnus-summary-show-thread) t)
10354                 (gnus-summary-find-subject subject))
10355           (setq count (1+ count))))
10356        (t                               ; Mark as unread.
10357         (while (and
10358                 (progn
10359                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10360                   (gnus-summary-show-thread) t)
10361                 (gnus-summary-find-subject subject))
10362           (setq count (1+ count)))))
10363       (gnus-set-mode-line 'summary)
10364       ;; Return the number of marked articles.
10365       count)))
10366
10367 (defun gnus-summary-mark-as-processable (n &optional unmark)
10368   "Set the process mark on the next N articles.
10369 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10370 the process mark instead.  The difference between N and the actual
10371 number of articles marked is returned."
10372   (interactive "P")
10373   (if (and (null n) (gnus-region-active-p))
10374       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10375     (setq n (prefix-numeric-value n))
10376     (let ((backward (< n 0))
10377           (n (abs n)))
10378       (while (and
10379               (> n 0)
10380               (if unmark
10381                   (gnus-summary-remove-process-mark
10382                    (gnus-summary-article-number))
10383                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10384               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10385         (setq n (1- n)))
10386       (when (/= 0 n)
10387         (gnus-message 7 "No more articles"))
10388       (gnus-summary-recenter)
10389       (gnus-summary-position-point)
10390       n)))
10391
10392 (defun gnus-summary-unmark-as-processable (n)
10393   "Remove the process mark from the next N articles.
10394 If N is negative, unmark backward instead.  The difference between N and
10395 the actual number of articles unmarked is returned."
10396   (interactive "P")
10397   (gnus-summary-mark-as-processable n t))
10398
10399 (defun gnus-summary-unmark-all-processable ()
10400   "Remove the process mark from all articles."
10401   (interactive)
10402   (save-excursion
10403     (while gnus-newsgroup-processable
10404       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10405   (gnus-summary-position-point))
10406
10407 (defun gnus-summary-add-mark (article type)
10408   "Mark ARTICLE with a mark of TYPE."
10409   (let ((vtype (car (assq type gnus-article-mark-lists)))
10410         var)
10411     (if (not vtype)
10412         (error "No such mark type: %s" type)
10413       (setq var (intern (format "gnus-newsgroup-%s" type)))
10414       (set var (cons article (symbol-value var)))
10415       (if (memq type '(processable cached replied forwarded recent saved))
10416           (gnus-summary-update-secondary-mark article)
10417         ;;; !!! This is bogus.  We should find out what primary
10418         ;;; !!! mark we want to set.
10419         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10420
10421 (defun gnus-summary-mark-as-expirable (n)
10422   "Mark N articles forward as expirable.
10423 If N is negative, mark backward instead.  The difference between N and
10424 the actual number of articles marked is returned."
10425   (interactive "p")
10426   (gnus-summary-mark-forward n gnus-expirable-mark))
10427
10428 (defun gnus-summary-mark-as-spam (n)
10429   "Mark N articles forward as spam.
10430 If N is negative, mark backward instead.  The difference between N and
10431 the actual number of articles marked is returned."
10432   (interactive "p")
10433   (gnus-summary-mark-forward n gnus-spam-mark))
10434
10435 (defun gnus-summary-mark-article-as-replied (article)
10436   "Mark ARTICLE as replied to and update the summary line.
10437 ARTICLE can also be a list of articles."
10438   (interactive (list (gnus-summary-article-number)))
10439   (let ((articles (if (listp article) article (list article))))
10440     (dolist (article articles)
10441       (unless (numberp article)
10442         (error "%s is not a number" article))
10443       (push article gnus-newsgroup-replied)
10444       (let ((inhibit-read-only t))
10445         (when (gnus-summary-goto-subject article nil t)
10446           (gnus-summary-update-secondary-mark article))))))
10447
10448 (defun gnus-summary-mark-article-as-forwarded (article)
10449   "Mark ARTICLE as forwarded and update the summary line.
10450 ARTICLE can also be a list of articles."
10451   (let ((articles (if (listp article) article (list article))))
10452     (dolist (article articles)
10453       (push article gnus-newsgroup-forwarded)
10454       (let ((inhibit-read-only t))
10455         (when (gnus-summary-goto-subject article nil t)
10456           (gnus-summary-update-secondary-mark article))))))
10457
10458 (defun gnus-summary-set-bookmark (article)
10459   "Set a bookmark in current article."
10460   (interactive (list (gnus-summary-article-number)))
10461   (when (or (not (get-buffer gnus-article-buffer))
10462             (not gnus-current-article)
10463             (not gnus-article-current)
10464             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10465     (error "No current article selected"))
10466   ;; Remove old bookmark, if one exists.
10467   (gnus-pull article gnus-newsgroup-bookmarks)
10468   ;; Set the new bookmark, which is on the form
10469   ;; (article-number . line-number-in-body).
10470   (push
10471    (cons article
10472          (with-current-buffer gnus-article-buffer
10473            (count-lines
10474             (min (point)
10475                  (save-excursion
10476                    (article-goto-body)
10477                    (point)))
10478             (point))))
10479    gnus-newsgroup-bookmarks)
10480   (gnus-message 6 "A bookmark has been added to the current article."))
10481
10482 (defun gnus-summary-remove-bookmark (article)
10483   "Remove the bookmark from the current article."
10484   (interactive (list (gnus-summary-article-number)))
10485   ;; Remove old bookmark, if one exists.
10486   (if (not (assq article gnus-newsgroup-bookmarks))
10487       (gnus-message 6 "No bookmark in current article.")
10488     (gnus-pull article gnus-newsgroup-bookmarks)
10489     (gnus-message 6 "Removed bookmark.")))
10490
10491 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10492 (defun gnus-summary-mark-as-dormant (n)
10493   "Mark N articles forward as dormant.
10494 If N is negative, mark backward instead.  The difference between N and
10495 the actual number of articles marked is returned."
10496   (interactive "p")
10497   (gnus-summary-mark-forward n gnus-dormant-mark))
10498
10499 (defun gnus-summary-set-process-mark (article)
10500   "Set the process mark on ARTICLE and update the summary line."
10501   (setq gnus-newsgroup-processable
10502         (cons article
10503               (delq article gnus-newsgroup-processable)))
10504   (when (gnus-summary-goto-subject article)
10505     (gnus-summary-show-thread)
10506     (gnus-summary-goto-subject article)
10507     (gnus-summary-update-secondary-mark article)))
10508
10509 (defun gnus-summary-remove-process-mark (&rest articles)
10510   "Remove the process mark from ARTICLES and update the summary line."
10511   (dolist (article articles)
10512     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10513     (when (gnus-summary-goto-subject article)
10514       (gnus-summary-show-thread)
10515       (gnus-summary-goto-subject article)
10516       (gnus-summary-update-secondary-mark article)))
10517   t)
10518
10519 (defun gnus-summary-set-saved-mark (article)
10520   "Set the process mark on ARTICLE and update the summary line."
10521   (push article gnus-newsgroup-saved)
10522   (when (gnus-summary-goto-subject article)
10523     (gnus-summary-update-secondary-mark article)))
10524
10525 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10526   "Mark N articles as read forwards.
10527 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10528 The difference between N and the actual number of articles marked is
10529 returned.
10530 If NO-EXPIRE, auto-expiry will be inhibited."
10531   (interactive "p")
10532   (gnus-summary-show-thread)
10533   (let ((backward (< n 0))
10534         (gnus-summary-goto-unread
10535          (and gnus-summary-goto-unread
10536               (not (eq gnus-summary-goto-unread 'never))
10537               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10538                                     gnus-ticked-mark gnus-dormant-mark)))))
10539         (n (abs n))
10540         (mark (or mark gnus-del-mark)))
10541     (while (and (> n 0)
10542                 (gnus-summary-mark-article nil mark no-expire)
10543                 (zerop (gnus-summary-next-subject
10544                         (if backward -1 1)
10545                         (and gnus-summary-goto-unread
10546                              (not (eq gnus-summary-goto-unread 'never)))
10547                         t)))
10548       (setq n (1- n)))
10549     (when (/= 0 n)
10550       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10551     (gnus-summary-recenter)
10552     (gnus-summary-position-point)
10553     (gnus-set-mode-line 'summary)
10554     n))
10555
10556 (defun gnus-summary-mark-article-as-read (mark)
10557   "Mark the current article quickly as read with MARK."
10558   (let ((article (gnus-summary-article-number)))
10559     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10560     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10561     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10562     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10563     (push (cons article mark) gnus-newsgroup-reads)
10564     ;; Possibly remove from cache, if that is used.
10565     (when gnus-use-cache
10566       (gnus-cache-enter-remove-article article))
10567     ;; Allow the backend to change the mark.
10568     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10569     ;; Check for auto-expiry.
10570     (when (and gnus-newsgroup-auto-expire
10571                (memq mark gnus-auto-expirable-marks))
10572       (setq mark gnus-expirable-mark)
10573       ;; Let the backend know about the mark change.
10574       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10575       (push article gnus-newsgroup-expirable))
10576     ;; Set the mark in the buffer.
10577     (gnus-summary-update-mark mark 'unread)
10578     t))
10579
10580 (defun gnus-summary-mark-article-as-unread (mark)
10581   "Mark the current article quickly as unread with MARK."
10582   (let* ((article (gnus-summary-article-number))
10583          (old-mark (gnus-summary-article-mark article)))
10584     ;; Allow the backend to change the mark.
10585     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10586     (if (eq mark old-mark)
10587         t
10588       (if (<= article 0)
10589           (progn
10590             (gnus-error 1 "Can't mark negative article numbers")
10591             nil)
10592         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10593         (setq gnus-newsgroup-spam-marked
10594               (delq article gnus-newsgroup-spam-marked))
10595         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10596         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10597         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10598         (cond ((= mark gnus-ticked-mark)
10599                (setq gnus-newsgroup-marked
10600                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10601                                               article)))
10602               ((= mark gnus-spam-mark)
10603                (setq gnus-newsgroup-spam-marked
10604                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10605                                               article)))
10606               ((= mark gnus-dormant-mark)
10607                (setq gnus-newsgroup-dormant
10608                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10609                                               article)))
10610               (t
10611                (setq gnus-newsgroup-unreads
10612                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10613                                               article))))
10614         (gnus-pull article gnus-newsgroup-reads)
10615
10616         ;; See whether the article is to be put in the cache.
10617         (and gnus-use-cache
10618              (vectorp (gnus-summary-article-header article))
10619              (save-excursion
10620                (gnus-cache-possibly-enter-article
10621                 gnus-newsgroup-name article
10622                 (= mark gnus-ticked-mark)
10623                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10624
10625         ;; Fix the mark.
10626         (gnus-summary-update-mark mark 'unread)
10627         t))))
10628
10629 (defun gnus-summary-mark-article (&optional article mark no-expire)
10630   "Mark ARTICLE with MARK.  MARK can be any character.
10631 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10632 `??' (dormant) and `?E' (expirable).
10633 If MARK is nil, then the default character `?r' is used.
10634 If ARTICLE is nil, then the article on the current line will be
10635 marked.
10636 If NO-EXPIRE, auto-expiry will be inhibited."
10637   ;; The mark might be a string.
10638   (when (stringp mark)
10639     (setq mark (aref mark 0)))
10640   ;; If no mark is given, then we check auto-expiring.
10641   (when (null mark)
10642     (setq mark gnus-del-mark))
10643   (when (and (not no-expire)
10644              gnus-newsgroup-auto-expire
10645              (memq mark gnus-auto-expirable-marks))
10646     (setq mark gnus-expirable-mark))
10647   (let ((article (or article (gnus-summary-article-number)))
10648         (old-mark (gnus-summary-article-mark article)))
10649     ;; Allow the backend to change the mark.
10650     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10651     (if (eq mark old-mark)
10652         t
10653       (unless article
10654         (error "No article on current line"))
10655       (if (not (if (or (= mark gnus-unread-mark)
10656                        (= mark gnus-ticked-mark)
10657                        (= mark gnus-spam-mark)
10658                        (= mark gnus-dormant-mark))
10659                    (gnus-mark-article-as-unread article mark)
10660                  (gnus-mark-article-as-read article mark)))
10661           t
10662         ;; See whether the article is to be put in the cache.
10663         (and gnus-use-cache
10664              (not (= mark gnus-canceled-mark))
10665              (vectorp (gnus-summary-article-header article))
10666              (save-excursion
10667                (gnus-cache-possibly-enter-article
10668                 gnus-newsgroup-name article
10669                 (= mark gnus-ticked-mark)
10670                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10671
10672         (when (gnus-summary-goto-subject article nil t)
10673           (let ((inhibit-read-only t))
10674             (gnus-summary-show-thread)
10675             ;; Fix the mark.
10676             (gnus-summary-update-mark mark 'unread)
10677             t))))))
10678
10679 (defun gnus-summary-update-secondary-mark (article)
10680   "Update the secondary (read, process, cache) mark."
10681   (gnus-summary-update-mark
10682    (cond ((memq article gnus-newsgroup-processable)
10683           gnus-process-mark)
10684          ((memq article gnus-newsgroup-cached)
10685           gnus-cached-mark)
10686          ((memq article gnus-newsgroup-replied)
10687           gnus-replied-mark)
10688          ((memq article gnus-newsgroup-forwarded)
10689           gnus-forwarded-mark)
10690          ((memq article gnus-newsgroup-saved)
10691           gnus-saved-mark)
10692          ((memq article gnus-newsgroup-recent)
10693           gnus-recent-mark)
10694          ((memq article gnus-newsgroup-unseen)
10695           gnus-unseen-mark)
10696          (t gnus-no-mark))
10697    'replied)
10698   (when (gnus-visual-p 'summary-highlight 'highlight)
10699     (gnus-run-hooks 'gnus-summary-update-hook))
10700   t)
10701
10702 (defun gnus-summary-update-download-mark (article)
10703   "Update the download mark."
10704   (gnus-summary-update-mark
10705    (cond ((memq article gnus-newsgroup-undownloaded)
10706           gnus-undownloaded-mark)
10707          (gnus-newsgroup-agentized
10708           gnus-downloaded-mark)
10709          (t
10710           gnus-no-mark))
10711    'download)
10712   (gnus-summary-update-line t)
10713   t)
10714
10715 (defun gnus-summary-update-mark (mark type)
10716   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10717         (inhibit-read-only t))
10718     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10719     (when forward
10720       (when (looking-at "\r")
10721         (incf forward))
10722       (when (<= (+ forward (point)) (point-max))
10723         ;; Go to the right position on the line.
10724         (goto-char (+ forward (point)))
10725         ;; Replace the old mark with the new mark.
10726         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10727         ;; Optionally update the marks by some user rule.
10728         (when (eq type 'unread)
10729           (gnus-data-set-mark
10730            (gnus-data-find (gnus-summary-article-number)) mark)
10731           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10732
10733 (defun gnus-mark-article-as-read (article &optional mark)
10734   "Enter ARTICLE in the pertinent lists and remove it from others."
10735   ;; Make the article expirable.
10736   (let ((mark (or mark gnus-del-mark)))
10737     (setq gnus-newsgroup-expirable
10738           (if (= mark gnus-expirable-mark)
10739               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10740             (delq article gnus-newsgroup-expirable)))
10741     ;; Remove from unread and marked lists.
10742     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10743     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10744     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10745     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10746     (push (cons article mark) gnus-newsgroup-reads)
10747     ;; Possibly remove from cache, if that is used.
10748     (when gnus-use-cache
10749       (gnus-cache-enter-remove-article article))
10750     t))
10751
10752 (defun gnus-mark-article-as-unread (article &optional mark)
10753   "Enter ARTICLE in the pertinent lists and remove it from others."
10754   (let ((mark (or mark gnus-ticked-mark)))
10755     (if (<= article 0)
10756         (progn
10757           (gnus-error 1 "Can't mark negative article numbers")
10758           nil)
10759       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10760             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10761             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10762             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10763             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10764
10765       ;; Unsuppress duplicates?
10766       (when gnus-suppress-duplicates
10767         (gnus-dup-unsuppress-article article))
10768
10769       (cond ((= mark gnus-ticked-mark)
10770              (setq gnus-newsgroup-marked
10771                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10772             ((= mark gnus-spam-mark)
10773              (setq gnus-newsgroup-spam-marked
10774                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10775                                             article)))
10776             ((= mark gnus-dormant-mark)
10777              (setq gnus-newsgroup-dormant
10778                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10779             (t
10780              (setq gnus-newsgroup-unreads
10781                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10782       (gnus-pull article gnus-newsgroup-reads)
10783       t)))
10784
10785 (defalias 'gnus-summary-mark-as-unread-forward
10786   'gnus-summary-tick-article-forward)
10787 (make-obsolete 'gnus-summary-mark-as-unread-forward
10788                'gnus-summary-tick-article-forward)
10789 (defun gnus-summary-tick-article-forward (n)
10790   "Tick N articles forwards.
10791 If N is negative, tick backwards instead.
10792 The difference between N and the number of articles ticked is returned."
10793   (interactive "p")
10794   (gnus-summary-mark-forward n gnus-ticked-mark))
10795
10796 (defalias 'gnus-summary-mark-as-unread-backward
10797   'gnus-summary-tick-article-backward)
10798 (make-obsolete 'gnus-summary-mark-as-unread-backward
10799                'gnus-summary-tick-article-backward)
10800 (defun gnus-summary-tick-article-backward (n)
10801   "Tick N articles backwards.
10802 The difference between N and the number of articles ticked is returned."
10803   (interactive "p")
10804   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10805
10806 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10807 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10808 (defun gnus-summary-tick-article (&optional article clear-mark)
10809   "Mark current article as unread.
10810 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10811 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10812   (interactive)
10813   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10814                                        gnus-ticked-mark)))
10815
10816 (defun gnus-summary-mark-as-read-forward (n)
10817   "Mark N articles as read forwards.
10818 If N is negative, mark backwards instead.
10819 The difference between N and the actual number of articles marked is
10820 returned."
10821   (interactive "p")
10822   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10823
10824 (defun gnus-summary-mark-as-read-backward (n)
10825   "Mark the N articles as read backwards.
10826 The difference between N and the actual number of articles marked is
10827 returned."
10828   (interactive "p")
10829   (gnus-summary-mark-forward
10830    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10831
10832 (defun gnus-summary-mark-as-read (&optional article mark)
10833   "Mark current article as read.
10834 ARTICLE specifies the article to be marked as read.
10835 MARK specifies a string to be inserted at the beginning of the line."
10836   (gnus-summary-mark-article article mark))
10837
10838 (defun gnus-summary-clear-mark-forward (n)
10839   "Clear marks from N articles forward.
10840 If N is negative, clear backward instead.
10841 The difference between N and the number of marks cleared is returned."
10842   (interactive "p")
10843   (gnus-summary-mark-forward n gnus-unread-mark))
10844
10845 (defun gnus-summary-clear-mark-backward (n)
10846   "Clear marks from N articles backward.
10847 The difference between N and the number of marks cleared is returned."
10848   (interactive "p")
10849   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10850
10851 (defun gnus-summary-mark-unread-as-read ()
10852   "Intended to be used by `gnus-mark-article-hook'."
10853   (when (memq gnus-current-article gnus-newsgroup-unreads)
10854     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10855
10856 (defun gnus-summary-mark-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-current-article
10862                                  (or new-mark gnus-read-mark)))))
10863
10864 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10865   "Intended to be used by `gnus-mark-article-hook'."
10866   (let ((mark (gnus-summary-article-mark)))
10867     (when (or (gnus-unread-mark-p mark)
10868               (gnus-read-mark-p mark))
10869       (gnus-summary-mark-article (gnus-summary-article-number)
10870                                  (or new-mark gnus-read-mark)))))
10871
10872 (defun gnus-summary-mark-unread-as-ticked ()
10873   "Intended to be used by `gnus-mark-article-hook'."
10874   (when (memq gnus-current-article gnus-newsgroup-unreads)
10875     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10876
10877 (defun gnus-summary-mark-region-as-read (point mark all)
10878   "Mark all unread articles between point and mark as read.
10879 If given a prefix, mark all articles between point and mark as read,
10880 even ticked and dormant ones."
10881   (interactive "r\nP")
10882   (save-excursion
10883     (let (article)
10884       (goto-char point)
10885       (beginning-of-line)
10886       (while (and
10887               (< (point) mark)
10888               (progn
10889                 (when (or all
10890                           (memq (setq article (gnus-summary-article-number))
10891                                 gnus-newsgroup-unreads))
10892                   (gnus-summary-mark-article article gnus-del-mark))
10893                 t)
10894               (gnus-summary-find-next))))))
10895
10896 (defun gnus-summary-mark-below (score mark)
10897   "Mark articles with score less than SCORE with MARK."
10898   (interactive "P\ncMark: ")
10899   (setq score (if score
10900                   (prefix-numeric-value score)
10901                 (or gnus-summary-default-score 0)))
10902   (with-current-buffer gnus-summary-buffer
10903     (goto-char (point-min))
10904     (while
10905         (progn
10906           (and (< (gnus-summary-article-score) score)
10907                (gnus-summary-mark-article nil mark))
10908           (gnus-summary-find-next)))))
10909
10910 (defun gnus-summary-kill-below (&optional score)
10911   "Mark articles with score below SCORE as read."
10912   (interactive "P")
10913   (gnus-summary-mark-below score gnus-killed-mark))
10914
10915 (defun gnus-summary-clear-above (&optional score)
10916   "Clear all marks from articles with score above SCORE."
10917   (interactive "P")
10918   (gnus-summary-mark-above score gnus-unread-mark))
10919
10920 (defun gnus-summary-tick-above (&optional score)
10921   "Tick all articles with score above SCORE."
10922   (interactive "P")
10923   (gnus-summary-mark-above score gnus-ticked-mark))
10924
10925 (defun gnus-summary-mark-above (score mark)
10926   "Mark articles with score over SCORE with MARK."
10927   (interactive "P\ncMark: ")
10928   (setq score (if score
10929                   (prefix-numeric-value score)
10930                 (or gnus-summary-default-score 0)))
10931   (with-current-buffer gnus-summary-buffer
10932     (goto-char (point-min))
10933     (while (and (progn
10934                   (when (> (gnus-summary-article-score) score)
10935                     (gnus-summary-mark-article nil mark))
10936                   t)
10937                 (gnus-summary-find-next)))))
10938
10939 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10940 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10941 (defun gnus-summary-limit-include-expunged (&optional no-error)
10942   "Display all the hidden articles that were expunged for low scores."
10943   (interactive)
10944   (let ((inhibit-read-only t))
10945     (let ((scored gnus-newsgroup-scored)
10946           headers h)
10947       (while scored
10948         (unless (gnus-summary-article-header (caar scored))
10949           (and (setq h (gnus-number-to-header (caar scored)))
10950                (< (cdar scored) gnus-summary-expunge-below)
10951                (push h headers)))
10952         (setq scored (cdr scored)))
10953       (if (not headers)
10954           (when (not no-error)
10955             (error "No expunged articles hidden"))
10956         (goto-char (point-min))
10957         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10958         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10959         (dolist (x headers)
10960           (push (mail-header-number x) gnus-newsgroup-limit))
10961         (gnus-summary-prepare-unthreaded (nreverse headers))
10962         (goto-char (point-min))
10963         (gnus-summary-position-point)
10964         t))))
10965
10966 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10967   "Mark all unread articles in this newsgroup as read.
10968 If prefix argument ALL is non-nil, ticked and dormant articles will
10969 also be marked as read.
10970 If QUIETLY is non-nil, no questions will be asked.
10971
10972 If TO-HERE is non-nil, it should be a point in the buffer.  All
10973 articles before (after, if REVERSE is set) this point will be marked
10974 as read.
10975
10976 Note that this function will only catch up the unread article
10977 in the current summary buffer limitation.
10978
10979 The number of articles marked as read is returned."
10980   (interactive "P")
10981   (prog1
10982       (save-excursion
10983         (when (or quietly
10984                   (not gnus-interactive-catchup) ;Without confirmation?
10985                   gnus-expert-user
10986                   (gnus-y-or-n-p
10987                    (if all
10988                        "Mark absolutely all articles as read? "
10989                      "Mark all unread articles as read? ")))
10990           (if (and not-mark
10991                    (not gnus-newsgroup-adaptive)
10992                    (not gnus-newsgroup-auto-expire)
10993                    (not gnus-suppress-duplicates)
10994                    (or (not gnus-use-cache)
10995                        (eq gnus-use-cache 'passive)))
10996               (progn
10997                 (when all
10998                   (setq gnus-newsgroup-marked nil
10999                         gnus-newsgroup-spam-marked nil
11000                         gnus-newsgroup-dormant nil))
11001                 (setq gnus-newsgroup-unreads
11002                       (gnus-sorted-nunion
11003                        (gnus-sorted-intersection gnus-newsgroup-unreads
11004                                                  gnus-newsgroup-downloadable)
11005                        (gnus-sorted-difference gnus-newsgroup-unfetched
11006                                                gnus-newsgroup-cached))))
11007             ;; We actually mark all articles as canceled, which we
11008             ;; have to do when using auto-expiry or adaptive scoring.
11009             (gnus-summary-show-all-threads)
11010             (if (and to-here reverse)
11011                 (progn
11012                   (goto-char to-here)
11013                   (gnus-summary-mark-current-read-and-unread-as-read
11014                    gnus-catchup-mark)
11015                   (while (gnus-summary-find-next (not all))
11016                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11017               (when (gnus-summary-first-subject (not all))
11018                 (while (and
11019                         (if to-here (< (point) to-here) t)
11020                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11021                         (gnus-summary-find-next (not all))))))
11022             (gnus-set-mode-line 'summary))
11023           t))
11024     (gnus-summary-position-point)))
11025
11026 (defun gnus-summary-catchup-to-here (&optional all)
11027   "Mark all unticked articles before the current one as read.
11028 If ALL is non-nil, also mark ticked and dormant articles as read."
11029   (interactive "P")
11030   (save-excursion
11031     (gnus-save-hidden-threads
11032       (let ((beg (point)))
11033         ;; We check that there are unread articles.
11034         (when (or all (gnus-summary-find-prev))
11035           (gnus-summary-catchup all t beg)))))
11036   (gnus-summary-position-point))
11037
11038 (defun gnus-summary-catchup-from-here (&optional all)
11039   "Mark all unticked articles after (and including) the current one as read.
11040 If ALL is non-nil, also mark ticked and dormant articles as read."
11041   (interactive "P")
11042   (save-excursion
11043     (gnus-save-hidden-threads
11044       (let ((beg (point)))
11045         ;; We check that there are unread articles.
11046         (when (or all (gnus-summary-find-next))
11047           (gnus-summary-catchup all t beg nil t)))))
11048   (gnus-summary-position-point))
11049
11050 (defun gnus-summary-catchup-all (&optional quietly)
11051   "Mark all articles in this newsgroup as read.
11052 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11053 instead, which marks only unread articles as read."
11054   (interactive "P")
11055   (gnus-summary-catchup t quietly))
11056
11057 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11058   "Mark all unread articles in this group as read, then exit.
11059 If prefix argument ALL is non-nil, all articles are marked as read.
11060 If QUIETLY is non-nil, no questions will be asked."
11061   (interactive "P")
11062   (when (gnus-summary-catchup all quietly nil 'fast)
11063     ;; Select next newsgroup or exit.
11064     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11065              (eq gnus-auto-select-next 'quietly))
11066         (gnus-summary-next-group nil)
11067       (gnus-summary-exit))))
11068
11069 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11070   "Mark all articles in this newsgroup as read, and then exit.
11071 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11072 instead, which marks only unread articles as read."
11073   (interactive "P")
11074   (gnus-summary-catchup-and-exit t quietly))
11075
11076 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11077   "Mark all articles in this group as read and select the next group.
11078 If given a prefix, mark all articles, unread as well as ticked, as
11079 read."
11080   (interactive "P")
11081   (save-excursion
11082     (gnus-summary-catchup all))
11083   (gnus-summary-next-group))
11084
11085 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11086   "Mark all articles in this group as read and select the previous group.
11087 If given a prefix, mark all articles, unread as well as ticked, as
11088 read."
11089   (interactive "P")
11090   (save-excursion
11091     (gnus-summary-catchup all))
11092   (gnus-summary-next-group nil nil t))
11093
11094 ;;;
11095 ;;; with article
11096 ;;;
11097
11098 (defmacro gnus-with-article (article &rest forms)
11099   "Select ARTICLE and perform FORMS in the original article buffer.
11100 Then replace the article with the result."
11101   `(progn
11102      ;; We don't want the article to be marked as read.
11103      (let (gnus-mark-article-hook)
11104        (gnus-summary-select-article t t nil ,article))
11105      (set-buffer gnus-original-article-buffer)
11106      ,@forms
11107      (if (not (gnus-check-backend-function
11108                'request-replace-article (car gnus-article-current)))
11109          (gnus-message 5 "Read-only group; not replacing")
11110        (unless (gnus-request-replace-article
11111                 ,article (car gnus-article-current)
11112                 (current-buffer) t)
11113          (error "Couldn't replace article")))
11114      ;; The cache and backlog have to be flushed somewhat.
11115      (when gnus-keep-backlog
11116        (gnus-backlog-remove-article
11117         (car gnus-article-current) (cdr gnus-article-current)))
11118      (when gnus-use-cache
11119        (gnus-cache-update-article
11120         (car gnus-article-current) (cdr gnus-article-current)))))
11121
11122 (put 'gnus-with-article 'lisp-indent-function 1)
11123 (put 'gnus-with-article 'edebug-form-spec '(form body))
11124
11125 ;; Thread-based commands.
11126
11127 (defun gnus-summary-articles-in-thread (&optional article)
11128   "Return a list of all articles in the current thread.
11129 If ARTICLE is non-nil, return all articles in the thread that starts
11130 with that article."
11131   (let* ((article (or article (gnus-summary-article-number)))
11132          (data (gnus-data-find-list article))
11133          (top-level (gnus-data-level (car data)))
11134          (top-subject
11135           (cond ((null gnus-thread-operation-ignore-subject)
11136                  (gnus-simplify-subject-re
11137                   (mail-header-subject (gnus-data-header (car data)))))
11138                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11139                  (gnus-simplify-subject-fuzzy
11140                   (mail-header-subject (gnus-data-header (car data)))))
11141                 (t nil)))
11142          (end-point (save-excursion
11143                       (if (gnus-summary-go-to-next-thread)
11144                           (point) (point-max))))
11145          articles)
11146     (while (and data
11147                 (< (gnus-data-pos (car data)) end-point))
11148       (when (or (not top-subject)
11149                 (string= top-subject
11150                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11151                              (gnus-simplify-subject-fuzzy
11152                               (mail-header-subject
11153                                (gnus-data-header (car data))))
11154                            (gnus-simplify-subject-re
11155                             (mail-header-subject
11156                              (gnus-data-header (car data)))))))
11157         (push (gnus-data-number (car data)) articles))
11158       (unless (and (setq data (cdr data))
11159                    (> (gnus-data-level (car data)) top-level))
11160         (setq data nil)))
11161     ;; Return the list of articles.
11162     (nreverse articles)))
11163
11164 (defun gnus-summary-rethread-current ()
11165   "Rethread the thread the current article is part of."
11166   (interactive)
11167   (let* ((gnus-show-threads t)
11168          (article (gnus-summary-article-number))
11169          (id (mail-header-id (gnus-summary-article-header)))
11170          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11171     (unless id
11172       (error "No article on the current line"))
11173     (gnus-rebuild-thread id)
11174     (gnus-summary-goto-subject article)))
11175
11176 (defun gnus-summary-reparent-thread ()
11177   "Make the current article child of the marked (or previous) article.
11178
11179 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11180 is non-nil or the Subject: of both articles are the same."
11181   (interactive)
11182   (unless (not (gnus-group-read-only-p))
11183     (error "The current newsgroup does not support article editing"))
11184   (unless (<= (length gnus-newsgroup-processable) 1)
11185     (error "No more than one article may be marked"))
11186   (let ((child (gnus-summary-article-number))
11187         ;; First grab the marked article, otherwise one line up.
11188         (parent (if (not (null gnus-newsgroup-processable))
11189                     (car gnus-newsgroup-processable)
11190                   (save-excursion
11191                     (if (eq (forward-line -1) 0)
11192                         (gnus-summary-article-number)
11193                       (error "Beginning of summary buffer"))))))
11194     (gnus-summary-reparent-children parent (list child))))
11195
11196 (defun gnus-summary-reparent-children (parent children)
11197   "Make PARENT the parent of CHILDREN.
11198 When called interactively, PARENT is the current article and CHILDREN
11199 are the process-marked articles."
11200   (interactive
11201    (list (gnus-summary-article-number)
11202          (gnus-summary-work-articles nil)))
11203   (dolist (child children)
11204     (save-window-excursion
11205       (let ((gnus-article-buffer " *reparent*"))
11206         (unless (not (eq parent child))
11207           (error "An article may not be self-referential"))
11208         (let ((message-id (mail-header-id
11209                            (gnus-summary-article-header parent))))
11210           (unless (and message-id (not (equal message-id "")))
11211             (error "No message-id in desired parent"))
11212           (gnus-with-article child
11213             (save-restriction
11214               (goto-char (point-min))
11215               (message-narrow-to-head)
11216               (if (re-search-forward "^References: " nil t)
11217                   (progn
11218                     (re-search-forward "^[^ \t]" nil t)
11219                     (forward-line -1)
11220                     (end-of-line)
11221                     (insert " " message-id))
11222                 (insert "References: " message-id "\n"))))
11223           (set-buffer gnus-summary-buffer)
11224           (gnus-summary-unmark-all-processable)
11225           (gnus-summary-update-article child)
11226           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11227             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11228           (gnus-summary-rethread-current)
11229           (gnus-message 3 "Article %d is now the child of article %d"
11230                         child parent))))))
11231
11232 (defun gnus-summary-toggle-threads (&optional arg)
11233   "Toggle showing conversation threads.
11234 If ARG is positive number, turn showing conversation threads on."
11235   (interactive "P")
11236   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11237     (setq gnus-show-threads
11238           (if (null arg) (not gnus-show-threads)
11239             (> (prefix-numeric-value arg) 0)))
11240     (gnus-summary-prepare)
11241     (gnus-summary-goto-subject current)
11242     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11243     (gnus-summary-position-point)))
11244
11245 (defun gnus-summary-show-all-threads ()
11246   "Show all threads."
11247   (interactive)
11248   (save-excursion
11249     (let ((buffer-read-only nil))
11250       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
11251   (gnus-summary-position-point))
11252
11253 (defun gnus-summary-show-thread ()
11254   "Show thread subtrees.
11255 Returns nil if no thread was there to be shown."
11256   (interactive)
11257   (let ((buffer-read-only nil)
11258         (orig (point))
11259         (end (point-at-eol))
11260         ;; Leave point at bol
11261         (beg (progn (beginning-of-line) (point))))
11262     (prog1
11263         ;; Any hidden lines here?
11264         (search-forward "\r" end t)
11265       (subst-char-in-region beg end ?\^M ?\n t)
11266       (goto-char orig)
11267       (gnus-summary-position-point))))
11268
11269 (defun gnus-summary-maybe-hide-threads ()
11270   "If requested, hide the threads that should be hidden."
11271   (when (and gnus-show-threads
11272              gnus-thread-hide-subtree)
11273     (gnus-summary-hide-all-threads
11274      (if (or (consp gnus-thread-hide-subtree)
11275              (functionp gnus-thread-hide-subtree))
11276          (gnus-make-predicate gnus-thread-hide-subtree)
11277        nil))))
11278
11279 ;;; Hiding predicates.
11280
11281 (defun gnus-article-unread-p (header)
11282   (memq (mail-header-number header) gnus-newsgroup-unreads))
11283
11284 (defun gnus-article-unseen-p (header)
11285   (memq (mail-header-number header) gnus-newsgroup-unseen))
11286
11287 (defun gnus-map-articles (predicate articles)
11288   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11289   (apply 'gnus-or (mapcar predicate
11290                           (mapcar (lambda (number)
11291                                     (gnus-summary-article-header number))
11292                                   articles))))
11293
11294 (defun gnus-summary-hide-all-threads (&optional predicate)
11295   "Hide all thread subtrees.
11296 If PREDICATE is supplied, threads that satisfy this predicate
11297 will not be hidden."
11298   (interactive)
11299   (save-excursion
11300     (goto-char (point-min))
11301     (let ((end nil))
11302       (while (not end)
11303         (when (or (not predicate)
11304                   (gnus-map-articles
11305                    predicate (gnus-summary-article-children)))
11306             (gnus-summary-hide-thread))
11307         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11308   (gnus-summary-position-point))
11309
11310 (defun gnus-summary-hide-thread ()
11311   "Hide thread subtrees.
11312 If PREDICATE is supplied, threads that satisfy this predicate
11313 will not be hidden.
11314 Returns nil if no threads were there to be hidden."
11315   (interactive)
11316   (let ((buffer-read-only nil)
11317         (start (point))
11318         (article (gnus-summary-article-number)))
11319     (goto-char start)
11320     ;; Go forward until either the buffer ends or the subthread ends.
11321     (when (and (not (eobp))
11322                (or (zerop (gnus-summary-next-thread 1 t))
11323                    (goto-char (point-max))))
11324       (prog1
11325           (if (and (> (point) start)
11326                    (search-backward "\n" start t))
11327               (progn
11328                 (subst-char-in-region start (point) ?\n ?\^M)
11329                 (gnus-summary-goto-subject article))
11330             (goto-char start)
11331             nil)))))
11332
11333 (defun gnus-summary-go-to-next-thread (&optional previous)
11334   "Go to the same level (or less) next thread.
11335 If PREVIOUS is non-nil, go to previous thread instead.
11336 Return the article number moved to, or nil if moving was impossible."
11337   (let ((level (gnus-summary-thread-level))
11338         (way (if previous -1 1))
11339         (beg (point)))
11340     (forward-line way)
11341     (while (and (not (eobp))
11342                 (< level (gnus-summary-thread-level)))
11343       (forward-line way))
11344     (if (eobp)
11345         (progn
11346           (goto-char beg)
11347           nil)
11348       (setq beg (point))
11349       (prog1
11350           (gnus-summary-article-number)
11351         (goto-char beg)))))
11352
11353 (defun gnus-summary-next-thread (n &optional silent)
11354   "Go to the same level next N'th thread.
11355 If N is negative, search backward instead.
11356 Returns the difference between N and the number of skips actually
11357 done.
11358
11359 If SILENT, don't output messages."
11360   (interactive "p")
11361   (let ((backward (< n 0))
11362         (n (abs n)))
11363     (while (and (> n 0)
11364                 (gnus-summary-go-to-next-thread backward))
11365       (decf n))
11366     (unless silent
11367       (gnus-summary-position-point))
11368     (when (and (not silent) (/= 0 n))
11369       (gnus-message 7 "No more threads"))
11370     n))
11371
11372 (defun gnus-summary-prev-thread (n)
11373   "Go to the same level previous N'th thread.
11374 Returns the difference between N and the number of skips actually
11375 done."
11376   (interactive "p")
11377   (gnus-summary-next-thread (- n)))
11378
11379 (defun gnus-summary-go-down-thread ()
11380   "Go down one level in the current thread."
11381   (let ((children (gnus-summary-article-children)))
11382     (when children
11383       (gnus-summary-goto-subject (car children)))))
11384
11385 (defun gnus-summary-go-up-thread ()
11386   "Go up one level in the current thread."
11387   (let ((parent (gnus-summary-article-parent)))
11388     (when parent
11389       (gnus-summary-goto-subject parent))))
11390
11391 (defun gnus-summary-down-thread (n)
11392   "Go down thread N steps.
11393 If N is negative, go up instead.
11394 Returns the difference between N and how many steps down that were
11395 taken."
11396   (interactive "p")
11397   (let ((up (< n 0))
11398         (n (abs n)))
11399     (while (and (> n 0)
11400                 (if up (gnus-summary-go-up-thread)
11401                   (gnus-summary-go-down-thread)))
11402       (setq n (1- n)))
11403     (gnus-summary-position-point)
11404     (when (/= 0 n)
11405       (gnus-message 7 "Can't go further"))
11406     n))
11407
11408 (defun gnus-summary-up-thread (n)
11409   "Go up thread N steps.
11410 If N is negative, go down instead.
11411 Returns the difference between N and how many steps down that were
11412 taken."
11413   (interactive "p")
11414   (gnus-summary-down-thread (- n)))
11415
11416 (defun gnus-summary-top-thread ()
11417   "Go to the top of the thread."
11418   (interactive)
11419   (while (gnus-summary-go-up-thread))
11420   (gnus-summary-article-number))
11421
11422 (defun gnus-summary-expire-thread ()
11423   "Mark articles under current thread as expired."
11424   (interactive)
11425   (gnus-summary-kill-thread 0))
11426
11427 (defun gnus-summary-kill-thread (&optional unmark)
11428   "Mark articles under current thread as read.
11429 If the prefix argument is positive, remove any kinds of marks.
11430 If the prefix argument is zero, mark thread as expired.
11431 If the prefix argument is negative, tick articles instead."
11432   (interactive "P")
11433   (when unmark
11434     (setq unmark (prefix-numeric-value unmark)))
11435   (let ((articles (gnus-summary-articles-in-thread))
11436         (hide (or (null unmark) (= unmark 0))))
11437     (save-excursion
11438       ;; Expand the thread.
11439       (gnus-summary-show-thread)
11440       ;; Mark all the articles.
11441       (while articles
11442         (gnus-summary-goto-subject (car articles))
11443         (cond ((null unmark)
11444                (gnus-summary-mark-article-as-read gnus-killed-mark))
11445               ((> unmark 0)
11446                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11447               ((= unmark 0)
11448                (gnus-summary-mark-article-as-unread gnus-expirable-mark))
11449               (t
11450                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11451         (setq articles (cdr articles))))
11452     ;; Hide killed subtrees when hide is true.
11453     (and hide
11454          gnus-thread-hide-killed
11455          (gnus-summary-hide-thread))
11456     ;; If hide is t, go to next unread subject.
11457     (when hide
11458       ;; Go to next unread subject.
11459       (gnus-summary-next-subject 1 t)))
11460   (gnus-set-mode-line 'summary))
11461
11462 ;; Summary sorting commands
11463
11464 (defun gnus-summary-sort-by-number (&optional reverse)
11465   "Sort the summary buffer by article number.
11466 Argument REVERSE means reverse order."
11467   (interactive "P")
11468   (gnus-summary-sort 'number reverse))
11469
11470 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11471   "Sort the summary buffer by most recent article number.
11472 Argument REVERSE means reverse order."
11473   (interactive "P")
11474   (gnus-summary-sort 'most-recent-number reverse))
11475
11476 (defun gnus-summary-sort-by-random (&optional reverse)
11477   "Randomize the order in the summary buffer.
11478 Argument REVERSE means to randomize in reverse order."
11479   (interactive "P")
11480   (gnus-summary-sort 'random reverse))
11481
11482 (defun gnus-summary-sort-by-author (&optional reverse)
11483   "Sort the summary buffer by author name alphabetically.
11484 If `case-fold-search' is non-nil, case of letters is ignored.
11485 Argument REVERSE means reverse order."
11486   (interactive "P")
11487   (gnus-summary-sort 'author reverse))
11488
11489 (defun gnus-summary-sort-by-recipient (&optional reverse)
11490   "Sort the summary buffer by recipient name alphabetically.
11491 If `case-fold-search' is non-nil, case of letters is ignored.
11492 Argument REVERSE means reverse order."
11493   (interactive "P")
11494   (gnus-summary-sort 'recipient reverse))
11495
11496 (defun gnus-summary-sort-by-subject (&optional reverse)
11497   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11498 If `case-fold-search' is non-nil, case of letters is ignored.
11499 Argument REVERSE means reverse order."
11500   (interactive "P")
11501   (gnus-summary-sort 'subject reverse))
11502
11503 (defun gnus-summary-sort-by-date (&optional reverse)
11504   "Sort the summary buffer by date.
11505 Argument REVERSE means reverse order."
11506   (interactive "P")
11507   (gnus-summary-sort 'date reverse))
11508
11509 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11510   "Sort the summary buffer by most recent date.
11511 Argument REVERSE means reverse order."
11512   (interactive "P")
11513   (gnus-summary-sort 'most-recent-date reverse))
11514
11515 (defun gnus-summary-sort-by-score (&optional reverse)
11516   "Sort the summary buffer by score.
11517 Argument REVERSE means reverse order."
11518   (interactive "P")
11519   (gnus-summary-sort 'score reverse))
11520
11521 (defun gnus-summary-sort-by-lines (&optional reverse)
11522   "Sort the summary buffer by the number of lines.
11523 Argument REVERSE means reverse order."
11524   (interactive "P")
11525   (gnus-summary-sort 'lines reverse))
11526
11527 (defun gnus-summary-sort-by-chars (&optional reverse)
11528   "Sort the summary buffer by article length.
11529 Argument REVERSE means reverse order."
11530   (interactive "P")
11531   (gnus-summary-sort 'chars reverse))
11532
11533 (defun gnus-summary-sort-by-original (&optional reverse)
11534   "Sort the summary buffer using the default sorting method.
11535 Argument REVERSE means reverse order."
11536   (interactive "P")
11537   (let* ((inhibit-read-only t)
11538          (gnus-summary-prepare-hook nil))
11539     ;; We do the sorting by regenerating the threads.
11540     (gnus-summary-prepare)
11541     ;; Hide subthreads if needed.
11542     (gnus-summary-maybe-hide-threads)))
11543
11544 (defun gnus-summary-sort (predicate reverse)
11545   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11546   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11547          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11548          (gnus-thread-sort-functions
11549           (if (not reverse)
11550               thread
11551             `(lambda (t1 t2)
11552                (,thread t2 t1))))
11553          (gnus-sort-gathered-threads-function
11554           gnus-thread-sort-functions)
11555          (gnus-article-sort-functions
11556           (if (not reverse)
11557               article
11558             `(lambda (t1 t2)
11559                (,article t2 t1))))
11560          (inhibit-read-only t)
11561          (gnus-summary-prepare-hook nil))
11562     ;; We do the sorting by regenerating the threads.
11563     (gnus-summary-prepare)
11564     ;; Hide subthreads if needed.
11565     (gnus-summary-maybe-hide-threads)))
11566
11567 ;; Summary saving commands.
11568
11569 (defun gnus-summary-save-article (&optional n not-saved)
11570   "Save the current article using the default saver function.
11571 If N is a positive number, save the N next articles.
11572 If N is a negative number, save the N previous articles.
11573 If N is nil and any articles have been marked with the process mark,
11574 save those articles instead.
11575 The variable `gnus-default-article-saver' specifies the saver function.
11576
11577 If the optional second argument NOT-SAVED is non-nil, articles saved
11578 will not be marked as saved."
11579   (interactive "P")
11580   (require 'gnus-art)
11581   (let* ((articles (gnus-summary-work-articles n))
11582          (save-buffer (save-excursion
11583                         (nnheader-set-temp-buffer " *Gnus Save*")))
11584          (num (length articles))
11585          ;; Whether to save decoded articles or raw articles.
11586          (decode (when gnus-article-save-coding-system
11587                    (get gnus-default-article-saver :decode)))
11588          ;; When saving many articles in a single file, use the other
11589          ;; function to save articles other than the first one.
11590          (saver2 (get gnus-default-article-saver :function))
11591          (gnus-prompt-before-saving (if saver2
11592                                         t
11593                                       gnus-prompt-before-saving))
11594          (gnus-default-article-saver gnus-default-article-saver)
11595          header file)
11596     (dolist (article articles)
11597       (setq header (gnus-summary-article-header article))
11598       (if (not (vectorp header))
11599           ;; This is a pseudo-article.
11600           (if (assq 'name header)
11601               (gnus-copy-file (cdr (assq 'name header)))
11602             (gnus-message 1 "Article %d is unsaveable" article))
11603         ;; This is a real article.
11604         (save-window-excursion
11605           (let ((gnus-display-mime-function (when decode
11606                                               gnus-display-mime-function))
11607                 (gnus-article-prepare-hook (when decode
11608                                              gnus-article-prepare-hook)))
11609             (gnus-summary-select-article t t nil article)
11610             (gnus-summary-goto-subject article)))
11611         (with-current-buffer save-buffer
11612           (erase-buffer)
11613           (insert-buffer-substring (if decode
11614                                        gnus-article-buffer
11615                                      gnus-original-article-buffer)))
11616         (setq file (gnus-article-save save-buffer file num))
11617         (gnus-summary-remove-process-mark article)
11618         (unless not-saved
11619           (gnus-summary-set-saved-mark article)))
11620       (when saver2
11621         (setq gnus-default-article-saver saver2
11622               saver2 nil)))
11623     (gnus-kill-buffer save-buffer)
11624     (gnus-summary-position-point)
11625     (gnus-set-mode-line 'summary)
11626     n))
11627
11628 (defun gnus-summary-pipe-output (&optional n sym)
11629   "Pipe the current article to a subprocess.
11630 If N is a positive number, pipe the N next articles.
11631 If N is a negative number, pipe the N previous articles.
11632 If N is nil and any articles have been marked with the process mark,
11633 pipe those articles instead.
11634 The default command to which articles are piped is specified by the
11635 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11636 will be prompted for the command.
11637
11638 The properties `:decode' and `:headers' that are put to the function
11639 symbol `gnus-summary-save-in-pipe' control whether this function
11640 decodes articles and what headers to keep (see the doc string for the
11641 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11642 is neither omitted nor the symbol `r', force including all headers
11643 regardless of the `:headers' property.  If it is the symbol `r',
11644 articles that are not decoded and include all headers will be piped
11645 no matter what the properties `:decode' and `:headers' are."
11646   (interactive (gnus-interactive "P\ny"))
11647   (require 'gnus-art)
11648   (let* ((articles (gnus-summary-work-articles n))
11649          (result-buffer "*Shell Command Output*")
11650          (all-headers (not (memq sym '(nil r))))
11651          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11652          (raw (eq sym 'r))
11653          (headers (get 'gnus-summary-save-in-pipe :headers))
11654          command result)
11655     (unless (numberp (car articles))
11656       (error "No article to pipe"))
11657     (setq command (gnus-read-shell-command
11658                    (concat "Shell command on "
11659                            (if (cdr articles)
11660                                (format "these %d articles" (length articles))
11661                              "this article")
11662                            ": ")
11663                    gnus-summary-pipe-output-default-command))
11664     (when (string-equal command "")
11665       (error "A command is required"))
11666     (when all-headers
11667       (put 'gnus-summary-save-in-pipe :headers nil))
11668     (unwind-protect
11669         (while articles
11670           (gnus-summary-goto-subject (pop articles))
11671           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11672           (when (and (get-buffer result-buffer)
11673                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11674             (setq result (concat result (with-current-buffer result-buffer
11675                                           (buffer-string))))))
11676       (put 'gnus-summary-save-in-pipe :headers headers))
11677     (unless (zerop (length result))
11678       (if (with-current-buffer (get-buffer-create result-buffer)
11679             (erase-buffer)
11680             (insert result)
11681             (prog1
11682                 (and (= (count-lines (point-min) (point)) 1)
11683                      (progn
11684                        (end-of-line 0)
11685                        (<= (current-column)
11686                            (window-width (minibuffer-window)))))
11687               (goto-char (point-min))))
11688           (message "%s" (substring result 0 -1))
11689         (message nil)
11690         (gnus-configure-windows 'pipe)))))
11691
11692 (defun gnus-summary-save-article-mail (&optional arg)
11693   "Append the current article to a Unix mail box file.
11694 If N is a positive number, save the N next articles.
11695 If N is a negative number, save the N previous articles.
11696 If N is nil and any articles have been marked with the process mark,
11697 save those articles instead."
11698   (interactive "P")
11699   (require 'gnus-art)
11700   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11701     (gnus-summary-save-article arg)))
11702
11703 (defun gnus-summary-save-article-rmail (&optional arg)
11704   "Append the current article to an rmail file.
11705 If N is a positive number, save the N next articles.
11706 If N is a negative number, save the N previous articles.
11707 If N is nil and any articles have been marked with the process mark,
11708 save those articles instead."
11709   (interactive "P")
11710   (require 'gnus-art)
11711   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11712     (gnus-summary-save-article arg)))
11713
11714 (defun gnus-summary-save-article-file (&optional arg)
11715   "Append the current article to a file.
11716 If N is a positive number, save the N next articles.
11717 If N is a negative number, save the N previous articles.
11718 If N is nil and any articles have been marked with the process mark,
11719 save those articles instead."
11720   (interactive "P")
11721   (require 'gnus-art)
11722   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11723     (gnus-summary-save-article arg)))
11724
11725 (defun gnus-summary-write-article-file (&optional arg)
11726   "Write the current article to a file, deleting the previous file.
11727 If N is a positive number, save the N next articles.
11728 If N is a negative number, save the N previous articles.
11729 If N is nil and any articles have been marked with the process mark,
11730 save those articles instead."
11731   (interactive "P")
11732   (require 'gnus-art)
11733   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11734     (gnus-summary-save-article arg)))
11735
11736 (defun gnus-summary-save-article-body-file (&optional arg)
11737   "Append the current article body to a file.
11738 If N is a positive number, save the N next articles.
11739 If N is a negative number, save the N previous articles.
11740 If N is nil and any articles have been marked with the process mark,
11741 save those articles instead."
11742   (interactive "P")
11743   (require 'gnus-art)
11744   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11745     (gnus-summary-save-article arg)))
11746
11747 (defun gnus-summary-write-article-body-file (&optional arg)
11748   "Write the current article body to a file, deleting the previous file.
11749 If N is a positive number, save the N next articles.
11750 If N is a negative number, save the N previous articles.
11751 If N is nil and any articles have been marked with the process mark,
11752 save those articles instead."
11753   (interactive "P")
11754   (require 'gnus-art)
11755   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11756     (gnus-summary-save-article arg)))
11757
11758 (defun gnus-summary-muttprint (&optional arg)
11759   "Print the current article using Muttprint.
11760 If N is a positive number, save the N next articles.
11761 If N is a negative number, save the N previous articles.
11762 If N is nil and any articles have been marked with the process mark,
11763 save those articles instead."
11764   (interactive "P")
11765   (require 'gnus-art)
11766   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11767     (gnus-summary-save-article arg t)))
11768
11769 (defun gnus-summary-pipe-message (program)
11770   "Pipe the current article through PROGRAM."
11771   (interactive "sProgram: ")
11772   (gnus-summary-select-article)
11773   (let ((mail-header-separator ""))
11774     (gnus-eval-in-buffer-window gnus-article-buffer
11775       (save-restriction
11776         (widen)
11777         (let ((start (window-start))
11778               (inhibit-read-only t))
11779           (message-pipe-buffer-body program)
11780           (set-window-start (get-buffer-window (current-buffer)) start))))))
11781
11782 (defun gnus-get-split-value (methods)
11783   "Return a value based on the split METHODS."
11784   (let (split-name method result match)
11785     (when methods
11786       (with-current-buffer gnus-original-article-buffer
11787         (save-restriction
11788           (nnheader-narrow-to-headers)
11789           (while (and methods (not split-name))
11790             (goto-char (point-min))
11791             (setq method (pop methods))
11792             (setq match (car method))
11793             (when (cond
11794                    ((stringp match)
11795                     ;; Regular expression.
11796                     (ignore-errors
11797                       (re-search-forward match nil t)))
11798                    ((functionp match)
11799                     ;; Function.
11800                     (save-restriction
11801                       (widen)
11802                       (setq result (funcall match gnus-newsgroup-name))))
11803                    ((consp match)
11804                     ;; Form.
11805                     (save-restriction
11806                       (widen)
11807                       (setq result (eval match)))))
11808               (setq split-name (cdr method))
11809               (cond ((stringp result)
11810                      (push (expand-file-name
11811                             result gnus-article-save-directory)
11812                            split-name))
11813                     ((consp result)
11814                      (setq split-name (append result split-name)))))))))
11815     (nreverse split-name)))
11816
11817 (defun gnus-valid-move-group-p (group)
11818   (and (boundp group)
11819        (symbol-name group)
11820        (symbol-value group)
11821        (gnus-get-function (gnus-find-method-for-group
11822                            (symbol-name group)) 'request-accept-article t)))
11823
11824 (defun gnus-read-move-group-name (prompt default articles prefix)
11825   "Read a group name."
11826   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11827          (minibuffer-confirm-incomplete nil) ; XEmacs
11828          (prom
11829           (format "%s %s to"
11830                   prompt
11831                   (if (> (length articles) 1)
11832                       (format "these %d articles" (length articles))
11833                     "this article")))
11834          (to-newsgroup
11835           (let (active group)
11836             (when (or (null split-name) (= 1 (length split-name)))
11837               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11838               (mapatoms (lambda (symbol)
11839                           (setq group (symbol-name symbol))
11840                           (when (string-match "[^\000-\177]" group)
11841                             (setq group (gnus-group-decoded-name group)))
11842                           (set (intern group active) group))
11843                         gnus-active-hashtb))
11844             (cond
11845              ((null split-name)
11846               (gnus-completing-read-with-default
11847                default prom active 'gnus-valid-move-group-p nil prefix
11848                'gnus-group-history))
11849              ((= 1 (length split-name))
11850               (gnus-completing-read-with-default
11851                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11852                'gnus-group-history))
11853              (t
11854               (gnus-completing-read-with-default
11855                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11856                'gnus-group-history)))))
11857          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11858          encoded)
11859     (when to-newsgroup
11860       (if (or (string= to-newsgroup "")
11861               (string= to-newsgroup prefix))
11862           (setq to-newsgroup default))
11863       (unless to-newsgroup
11864         (error "No group name entered"))
11865       (setq encoded (mm-encode-coding-string
11866                      to-newsgroup
11867                      (gnus-group-name-charset to-method to-newsgroup)))
11868       (or (gnus-active encoded)
11869           (gnus-activate-group encoded nil nil to-method)
11870           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11871                                      to-newsgroup))
11872               (or (and (gnus-request-create-group encoded to-method)
11873                        (gnus-activate-group encoded nil nil to-method)
11874                        (gnus-subscribe-group encoded))
11875                   (error "Couldn't create group %s" to-newsgroup)))
11876           (error "No such group: %s" to-newsgroup))
11877       encoded)))
11878
11879 (defvar gnus-summary-save-parts-counter)
11880 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11881
11882 (defun gnus-summary-save-parts (type dir n &optional reverse)
11883   "Save parts matching TYPE to DIR.
11884 If REVERSE, save parts that do not match TYPE."
11885   (interactive
11886    (list (read-string "Save parts of type: "
11887                       (or (car gnus-summary-save-parts-type-history)
11888                           gnus-summary-save-parts-default-mime)
11889                       'gnus-summary-save-parts-type-history)
11890          (setq gnus-summary-save-parts-last-directory
11891                (read-file-name "Save to directory: "
11892                                gnus-summary-save-parts-last-directory
11893                                nil t))
11894          current-prefix-arg))
11895   (gnus-summary-iterate n
11896     (let ((gnus-display-mime-function nil)
11897           gnus-article-prepare-hook
11898           gnus-article-decode-hook
11899           gnus-display-mime-function
11900           gnus-break-pages
11901           (gnus-inhibit-treatment t))
11902       (gnus-summary-select-article))
11903     (with-current-buffer gnus-article-buffer
11904       (let ((handles (or gnus-article-mime-handles
11905                          (mm-dissect-buffer nil gnus-article-loose-mime)
11906                          (and gnus-article-emulate-mime
11907                               (mm-uu-dissect))))
11908             (gnus-summary-save-parts-counter 1))
11909         (when handles
11910           (gnus-summary-save-parts-1 type dir handles reverse)
11911           (unless gnus-article-mime-handles ;; Don't destroy this case.
11912             (mm-destroy-parts handles)))))))
11913
11914 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11915   (if (stringp (car handle))
11916       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11917               (cdr handle))
11918     (when (if reverse
11919               (not (string-match type (mm-handle-media-type handle)))
11920             (string-match type (mm-handle-media-type handle)))
11921       (let ((file (expand-file-name
11922                    (gnus-map-function
11923                     mm-file-name-rewrite-functions
11924                     (file-name-nondirectory
11925                      (or
11926                       (mail-content-type-get
11927                        (mm-handle-disposition handle) 'filename)
11928                       (mail-content-type-get
11929                        (mm-handle-type handle) 'name)
11930                       (format "%s.%d.%d" gnus-newsgroup-name
11931                               (cdr gnus-article-current)
11932                               gnus-summary-save-parts-counter))))
11933                    dir)))
11934         (incf gnus-summary-save-parts-counter)
11935         (unless (file-exists-p file)
11936           (mm-save-part-to-file handle file))))))
11937
11938 ;; Summary extract commands
11939
11940 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11941   (let ((inhibit-read-only t)
11942         (article (gnus-summary-article-number))
11943         after-article b e)
11944     (unless (gnus-summary-goto-subject article)
11945       (error "No such article: %d" article))
11946     (gnus-summary-position-point)
11947     ;; If all commands are to be bunched up on one line, we collect
11948     ;; them here.
11949     (unless gnus-view-pseudos-separately
11950       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11951             files action)
11952         (while ps
11953           (setq action (cdr (assq 'action (car ps))))
11954           (setq files (list (cdr (assq 'name (car ps)))))
11955           (while (and ps (cdr ps)
11956                       (string= (or action "1")
11957                                (or (cdr (assq 'action (cadr ps))) "2")))
11958             (push (cdr (assq 'name (cadr ps))) files)
11959             (setcdr ps (cddr ps)))
11960           (when files
11961             (when (not (string-match "%s" action))
11962               (push " " files))
11963             (push " " files)
11964             (when (assq 'execute (car ps))
11965               (setcdr (assq 'execute (car ps))
11966                       (funcall (if (string-match "%s" action)
11967                                    'format 'concat)
11968                                action
11969                                (mapconcat
11970                                 (lambda (f)
11971                                   (if (equal f " ")
11972                                       f
11973                                     (shell-quote-argument f)))
11974                                 files " ")))))
11975           (setq ps (cdr ps)))))
11976     (if (and gnus-view-pseudos (not not-view))
11977         (while pslist
11978           (when (assq 'execute (car pslist))
11979             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11980                                   (eq gnus-view-pseudos 'not-confirm)))
11981           (setq pslist (cdr pslist)))
11982       (save-excursion
11983         (while pslist
11984           (setq after-article (or (cdr (assq 'article (car pslist)))
11985                                   (gnus-summary-article-number)))
11986           (gnus-summary-goto-subject after-article)
11987           (forward-line 1)
11988           (setq b (point))
11989           (insert "    " (file-name-nondirectory
11990                           (cdr (assq 'name (car pslist))))
11991                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11992           (setq e (point))
11993           (forward-line -1)             ; back to `b'
11994           (gnus-add-text-properties
11995            b (1- e) (list 'gnus-number gnus-reffed-article-number
11996                           gnus-mouse-face-prop gnus-mouse-face))
11997           (gnus-data-enter
11998            after-article gnus-reffed-article-number
11999            gnus-unread-mark b (car pslist) 0 (- e b))
12000           (setq gnus-newsgroup-unreads
12001                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12002                                          gnus-reffed-article-number))
12003           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12004           (setq pslist (cdr pslist)))))))
12005
12006 (defun gnus-pseudos< (p1 p2)
12007   (let ((c1 (cdr (assq 'action p1)))
12008         (c2 (cdr (assq 'action p2))))
12009     (and c1 c2 (string< c1 c2))))
12010
12011 (defun gnus-request-pseudo-article (props)
12012   (cond ((assq 'execute props)
12013          (gnus-execute-command (cdr (assq 'execute props)))))
12014   (let ((gnus-current-article (gnus-summary-article-number)))
12015     (gnus-run-hooks 'gnus-mark-article-hook)))
12016
12017 (defun gnus-execute-command (command &optional automatic)
12018   (save-excursion
12019     (gnus-article-setup-buffer)
12020     (set-buffer gnus-article-buffer)
12021     (setq buffer-read-only nil)
12022     (let ((command (if automatic command
12023                      (read-string "Command: " (cons command 0)))))
12024       (erase-buffer)
12025       (insert "$ " command "\n\n")
12026       (if gnus-view-pseudo-asynchronously
12027           (start-process "gnus-execute" (current-buffer) shell-file-name
12028                          shell-command-switch command)
12029         (call-process shell-file-name nil t nil
12030                       shell-command-switch command)))))
12031
12032 ;; Summary kill commands.
12033
12034 (defun gnus-summary-edit-global-kill (article)
12035   "Edit the \"global\" kill file."
12036   (interactive (list (gnus-summary-article-number)))
12037   (gnus-group-edit-global-kill article))
12038
12039 (defun gnus-summary-edit-local-kill ()
12040   "Edit a local kill file applied to the current newsgroup."
12041   (interactive)
12042   (setq gnus-current-headers (gnus-summary-article-header))
12043   (gnus-group-edit-local-kill
12044    (gnus-summary-article-number) gnus-newsgroup-name))
12045
12046 ;;; Header reading.
12047
12048 (defun gnus-read-header (id &optional header)
12049   "Read the headers of article ID and enter them into the Gnus system."
12050   (let ((group gnus-newsgroup-name)
12051         (gnus-override-method
12052          (or
12053           gnus-override-method
12054           (and (gnus-news-group-p gnus-newsgroup-name)
12055                (car (gnus-refer-article-methods)))))
12056         where)
12057     ;; First we check to see whether the header in question is already
12058     ;; fetched.
12059     (if (stringp id)
12060         ;; This is a Message-ID.
12061         (setq header (or header (gnus-id-to-header id)))
12062       ;; This is an article number.
12063       (setq header (or header (gnus-summary-article-header id))))
12064     (if (and header
12065              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12066         ;; We have found the header.
12067         header
12068       ;; We have to really fetch the header to this article.
12069       (with-current-buffer nntp-server-buffer
12070         (when (setq where (gnus-request-head id group))
12071           (nnheader-fold-continuation-lines)
12072           (goto-char (point-max))
12073           (insert ".\n")
12074           (goto-char (point-min))
12075           (insert "211 ")
12076           (princ (cond
12077                   ((numberp id) id)
12078                   ((cdr where) (cdr where))
12079                   (header (mail-header-number header))
12080                   (t gnus-reffed-article-number))
12081                  (current-buffer))
12082           (insert " Article retrieved.\n"))
12083         (if (or (not where)
12084                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12085             ()                          ; Malformed head.
12086           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12087             (when (and (stringp id)
12088                        (or
12089                         (not (string= (gnus-group-real-name group)
12090                                       (car where)))
12091                         (not (gnus-server-equal gnus-override-method
12092                                                 (gnus-group-method group)))))
12093               ;; If we fetched by Message-ID and the article came from
12094               ;; a different group (or server), we fudge some bogus
12095               ;; article numbers for this article.
12096               (mail-header-set-number header gnus-reffed-article-number))
12097             (with-current-buffer gnus-summary-buffer
12098               (decf gnus-reffed-article-number)
12099               (gnus-remove-header (mail-header-number header))
12100               (push header gnus-newsgroup-headers)
12101               (setq gnus-current-headers header)
12102               (push (mail-header-number header) gnus-newsgroup-limit)))
12103           header)))))
12104
12105 (defun gnus-remove-header (number)
12106   "Remove header NUMBER from `gnus-newsgroup-headers'."
12107   (if (and gnus-newsgroup-headers
12108            (= number (mail-header-number (car gnus-newsgroup-headers))))
12109       (pop gnus-newsgroup-headers)
12110     (let ((headers gnus-newsgroup-headers))
12111       (while (and (cdr headers)
12112                   (not (= number (mail-header-number (cadr headers)))))
12113         (pop headers))
12114       (when (cdr headers)
12115         (setcdr headers (cddr headers))))))
12116
12117 ;;;
12118 ;;; summary highlights
12119 ;;;
12120
12121 (defun gnus-highlight-selected-summary ()
12122   "Highlight selected article in summary buffer."
12123   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12124   (when gnus-summary-selected-face
12125     (save-excursion
12126       (let* ((beg (point-at-bol))
12127              (end (point-at-eol))
12128              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12129              (from (if (get-text-property beg gnus-mouse-face-prop)
12130                        beg
12131                      (or (next-single-property-change
12132                           beg gnus-mouse-face-prop nil end)
12133                          beg)))
12134              (to
12135               (if (= from end)
12136                   (- from 2)
12137                 (or (next-single-property-change
12138                      from gnus-mouse-face-prop nil end)
12139                     end))))
12140         ;; If no mouse-face prop on line we will have to = from = end,
12141         ;; so we highlight the entire line instead.
12142         (when (= (+ to 2) from)
12143           (setq from beg)
12144           (setq to end))
12145         (if gnus-newsgroup-selected-overlay
12146             ;; Move old overlay.
12147             (gnus-move-overlay
12148              gnus-newsgroup-selected-overlay from to (current-buffer))
12149           ;; Create new overlay.
12150           (gnus-overlay-put
12151            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12152            'face gnus-summary-selected-face))))))
12153
12154 (defvar gnus-summary-highlight-line-cached nil)
12155 (defvar gnus-summary-highlight-line-trigger nil)
12156
12157 (defun gnus-summary-highlight-line-0 ()
12158   (if (and (eq gnus-summary-highlight-line-trigger
12159                gnus-summary-highlight)
12160            gnus-summary-highlight-line-cached)
12161       gnus-summary-highlight-line-cached
12162     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12163           gnus-summary-highlight-line-cached
12164           (let* ((cond (list 'cond))
12165                  (c cond)
12166                  (list gnus-summary-highlight))
12167             (while list
12168               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12169                               nil))
12170               (setq c (cdr c)
12171                     list (cdr list)))
12172             (gnus-byte-compile (list 'lambda nil cond))))))
12173
12174 (defun gnus-summary-highlight-line ()
12175   "Highlight current line according to `gnus-summary-highlight'."
12176   (let* ((beg (point-at-bol))
12177          (article (or (gnus-summary-article-number) gnus-current-article))
12178          (score (or (cdr (assq article
12179                                gnus-newsgroup-scored))
12180                     gnus-summary-default-score 0))
12181          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12182          (inhibit-read-only t)
12183          (default gnus-summary-default-score)
12184          (default-high gnus-summary-default-high-score)
12185          (default-low gnus-summary-default-low-score)
12186          (uncached (and gnus-summary-use-undownloaded-faces
12187                         (memq article gnus-newsgroup-undownloaded)
12188                         (not (memq article gnus-newsgroup-cached)))))
12189     (let ((face (funcall (gnus-summary-highlight-line-0))))
12190       (unless (eq face (get-text-property beg 'face))
12191         (gnus-put-text-property-excluding-characters-with-faces
12192          beg (point-at-eol) 'face
12193          (setq face (if (boundp face) (symbol-value face) face)))
12194         (when gnus-summary-highlight-line-function
12195           (funcall gnus-summary-highlight-line-function article face))))))
12196
12197 (defun gnus-update-read-articles (group unread &optional compute)
12198   "Update the list of read articles in GROUP.
12199 UNREAD is a sorted list."
12200   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12201         (info (gnus-get-info group))
12202         (prev 1)
12203         read)
12204     (if (or (not info) (not active))
12205         ;; There is no info on this group if it was, in fact,
12206         ;; killed.  Gnus stores no information on killed groups, so
12207         ;; there's nothing to be done.
12208         ;; One could store the information somewhere temporarily,
12209         ;; perhaps...  Hmmm...
12210         ()
12211       ;; Remove any negative articles numbers.
12212       (while (and unread (< (car unread) 0))
12213         (setq unread (cdr unread)))
12214       ;; Remove any expired article numbers
12215       (while (and unread (< (car unread) (car active)))
12216         (setq unread (cdr unread)))
12217       ;; Compute the ranges of read articles by looking at the list of
12218       ;; unread articles.
12219       (while unread
12220         (when (/= (car unread) prev)
12221           (push (if (= prev (1- (car unread))) prev
12222                   (cons prev (1- (car unread))))
12223                 read))
12224         (setq prev (1+ (car unread)))
12225         (setq unread (cdr unread)))
12226       (when (<= prev (cdr active))
12227         (push (cons prev (cdr active)) read))
12228       (setq read (if (> (length read) 1) (nreverse read) read))
12229       (if compute
12230           read
12231         (save-excursion
12232           (let (setmarkundo)
12233             ;; Propagate the read marks to the backend.
12234             (when (and gnus-propagate-marks
12235                        (gnus-check-backend-function 'request-set-mark group))
12236               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12237                     (add (gnus-remove-from-range read (gnus-info-read info))))
12238                 (when (or add del)
12239                   (unless (gnus-check-group group)
12240                     (error "Can't open server for %s" group))
12241                   (gnus-request-set-mark
12242                    group (delq nil (list (if add (list add 'add '(read)))
12243                                          (if del (list del 'del '(read))))))
12244                   (setq setmarkundo
12245                         `(gnus-request-set-mark
12246                           ,group
12247                           ',(delq nil (list
12248                                        (if del (list del 'add '(read)))
12249                                        (if add (list add 'del '(read))))))))))
12250             (set-buffer gnus-group-buffer)
12251             (gnus-undo-register
12252               `(progn
12253                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12254                  (gnus-info-set-read ',info ',(gnus-info-read info))
12255                  (gnus-get-unread-articles-in-group ',info
12256                                                     (gnus-active ,group))
12257                  (gnus-group-update-group ,group t)
12258                  ,setmarkundo))))
12259         ;; Enter this list into the group info.
12260         (gnus-info-set-read info read)
12261         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12262         (gnus-get-unread-articles-in-group info (gnus-active group))
12263         t))))
12264
12265 (defun gnus-offer-save-summaries ()
12266   "Offer to save all active summary buffers."
12267   (let (buffers)
12268     ;; Go through all buffers and find all summaries.
12269     (dolist (buffer (buffer-list))
12270       (when (and (setq buffer (buffer-name buffer))
12271                  (string-match "Summary" buffer)
12272                  (with-current-buffer buffer
12273                    ;; We check that this is, indeed, a summary buffer.
12274                    (and (eq major-mode 'gnus-summary-mode)
12275                         ;; Also make sure this isn't bogus.
12276                         gnus-newsgroup-prepared
12277                         ;; Also make sure that this isn't a
12278                         ;; dead summary buffer.
12279                         (not gnus-dead-summary-mode))))
12280         (push buffer buffers)))
12281     ;; Go through all these summary buffers and offer to save them.
12282     (when buffers
12283       (save-excursion
12284         (map-y-or-n-p
12285          "Update summary buffer %s? "
12286          (lambda (buf)
12287            (switch-to-buffer buf)
12288            (gnus-summary-exit))
12289          buffers)))))
12290
12291 (defun gnus-summary-setup-default-charset ()
12292   "Setup newsgroup default charset."
12293   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12294       (setq gnus-newsgroup-charset nil)
12295     (let* ((ignored-charsets
12296             (or gnus-newsgroup-ephemeral-ignored-charsets
12297                 (append
12298                  (and gnus-newsgroup-name
12299                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12300                  gnus-newsgroup-ignored-charsets))))
12301       (setq gnus-newsgroup-charset
12302             (or gnus-newsgroup-ephemeral-charset
12303                 (and gnus-newsgroup-name
12304                      (gnus-parameter-charset gnus-newsgroup-name))
12305                 gnus-default-charset))
12306       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12307            ignored-charsets))))
12308
12309 ;;;
12310 ;;; Mime Commands
12311 ;;;
12312
12313 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12314   "Display the current article buffer fully MIME-buttonized.
12315 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12316 treated as multipart/mixed."
12317   (interactive "P")
12318   (require 'gnus-art)
12319   (let ((gnus-unbuttonized-mime-types nil)
12320         (gnus-mime-display-multipart-as-mixed show-all-parts))
12321     (gnus-summary-show-article)))
12322
12323 (defun gnus-summary-repair-multipart (article)
12324   "Add a Content-Type header to a multipart article without one."
12325   (interactive (list (gnus-summary-article-number)))
12326   (gnus-with-article article
12327     (message-narrow-to-head)
12328     (message-remove-header "Mime-Version")
12329     (goto-char (point-max))
12330     (insert "Mime-Version: 1.0\n")
12331     (widen)
12332     (when (search-forward "\n--" nil t)
12333       (let ((separator (buffer-substring (point) (point-at-eol))))
12334         (message-narrow-to-head)
12335         (message-remove-header "Content-Type")
12336         (goto-char (point-max))
12337         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12338                         separator))
12339         (widen))))
12340   (let (gnus-mark-article-hook)
12341     (gnus-summary-select-article t t nil article)))
12342
12343 (defun gnus-summary-toggle-display-buttonized ()
12344   "Toggle the buttonizing of the article buffer."
12345   (interactive)
12346   (require 'gnus-art)
12347   (if (setq gnus-inhibit-mime-unbuttonizing
12348             (not gnus-inhibit-mime-unbuttonizing))
12349       (let ((gnus-unbuttonized-mime-types nil))
12350         (gnus-summary-show-article))
12351     (gnus-summary-show-article)))
12352
12353 ;;;
12354 ;;; Generic summary marking commands
12355 ;;;
12356
12357 (defvar gnus-summary-marking-alist
12358   '((read gnus-del-mark "d")
12359     (unread gnus-unread-mark "u")
12360     (ticked gnus-ticked-mark "!")
12361     (dormant gnus-dormant-mark "?")
12362     (expirable gnus-expirable-mark "e"))
12363   "An alist of names/marks/keystrokes.")
12364
12365 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12366 (defvar gnus-summary-mark-map)
12367
12368 (defun gnus-summary-make-all-marking-commands ()
12369   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12370   (dolist (elem gnus-summary-marking-alist)
12371     (apply 'gnus-summary-make-marking-command elem)))
12372
12373 (defun gnus-summary-make-marking-command (name mark keystroke)
12374   (let ((map (make-sparse-keymap)))
12375     (define-key gnus-summary-generic-mark-map keystroke map)
12376     (dolist (lway `((next "next" next nil "n")
12377                     (next-unread "next unread" next t "N")
12378                     (prev "previous" prev nil "p")
12379                     (prev-unread "previous unread" prev t "P")
12380                     (nomove "" nil nil ,keystroke)))
12381       (let ((func (gnus-summary-make-marking-command-1
12382                    mark (car lway) lway name)))
12383         (setq func (eval func))
12384         (define-key map (nth 4 lway) func)))))
12385
12386 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12387   `(defun ,(intern
12388             (format "gnus-summary-put-mark-as-%s%s"
12389                     name (if (eq way 'nomove)
12390                              ""
12391                            (concat "-" (symbol-name way)))))
12392      (n)
12393      ,(format
12394        "Mark the current article as %s%s.
12395 If N, the prefix, then repeat N times.
12396 If N is negative, move in reverse order.
12397 The difference between N and the actual number of articles marked is
12398 returned."
12399        name (cadr lway))
12400      (interactive "p")
12401      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12402
12403 (defun gnus-summary-generic-mark (n mark move unread)
12404   "Mark N articles with MARK."
12405   (unless (eq major-mode 'gnus-summary-mode)
12406     (error "This command can only be used in the summary buffer"))
12407   (gnus-summary-show-thread)
12408   (let ((nummove
12409          (cond
12410           ((eq move 'next) 1)
12411           ((eq move 'prev) -1)
12412           (t 0))))
12413     (if (zerop nummove)
12414         (setq n 1)
12415       (when (< n 0)
12416         (setq n (abs n)
12417               nummove (* -1 nummove))))
12418     (while (and (> n 0)
12419                 (gnus-summary-mark-article nil mark)
12420                 (zerop (gnus-summary-next-subject nummove unread t)))
12421       (setq n (1- n)))
12422     (when (/= 0 n)
12423       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12424     (gnus-summary-recenter)
12425     (gnus-summary-position-point)
12426     (gnus-set-mode-line 'summary)
12427     n))
12428
12429 (defun gnus-summary-insert-articles (articles)
12430   (when (setq articles
12431               (gnus-sorted-difference articles
12432                                       (mapcar (lambda (h)
12433                                                 (mail-header-number h))
12434                                               gnus-newsgroup-headers)))
12435     (setq gnus-newsgroup-headers
12436           (gnus-merge 'list
12437                       gnus-newsgroup-headers
12438                       (gnus-fetch-headers articles)
12439                       'gnus-article-sort-by-number))
12440     ;; Suppress duplicates?
12441     (when gnus-suppress-duplicates
12442       (gnus-dup-suppress-articles))
12443
12444     (if (and gnus-fetch-old-headers
12445              (eq gnus-headers-retrieved-by 'nov))
12446         ;; We might want to build some more threads first.
12447         (if (eq gnus-fetch-old-headers 'invisible)
12448             (gnus-build-all-threads)
12449           (gnus-build-old-threads))
12450       ;; Mark the inserted articles that are unread as unread.
12451       (setq gnus-newsgroup-unreads
12452             (gnus-sorted-nunion
12453              gnus-newsgroup-unreads
12454              (gnus-sorted-nintersection
12455               (gnus-list-of-unread-articles gnus-newsgroup-name)
12456               articles)))
12457       ;; Mark the inserted articles as selected so that the information
12458       ;; of the marks having been changed by a user may be updated when
12459       ;; exiting this group.  See `gnus-summary-update-info'.
12460       (dolist (art articles)
12461         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12462     ;; Let the Gnus agent mark articles as read.
12463     (when gnus-agent
12464       (gnus-agent-get-undownloaded-list))
12465     ;; Remove list identifiers from subject
12466     (when gnus-list-identifiers
12467       (gnus-summary-remove-list-identifiers))
12468     ;; First and last article in this newsgroup.
12469     (when gnus-newsgroup-headers
12470       (setq gnus-newsgroup-begin
12471             (mail-header-number (car gnus-newsgroup-headers))
12472             gnus-newsgroup-end
12473             (mail-header-number
12474              (gnus-last-element gnus-newsgroup-headers))))
12475     (when gnus-use-scoring
12476       (gnus-possibly-score-headers))))
12477
12478 (defun gnus-summary-insert-old-articles (&optional all)
12479   "Insert all old articles in this group.
12480 If ALL is non-nil, already read articles become readable.
12481 If ALL is a number, fetch this number of articles."
12482   (interactive "P")
12483   (prog1
12484       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12485             older len)
12486         (setq older
12487               ;; Some nntp servers lie about their active range.  When
12488               ;; this happens, the active range can be in the millions.
12489               ;; Use a compressed range to avoid creating a huge list.
12490               (gnus-range-difference (list gnus-newsgroup-active) old))
12491         (setq len (gnus-range-length older))
12492         (cond
12493          ((null older) nil)
12494          ((numberp all)
12495           (if (< all len)
12496               (let ((older-range (nreverse older)))
12497                 (setq older nil)
12498
12499                 (while (> all 0)
12500                   (let* ((r (pop older-range))
12501                          (min (if (numberp r) r (car r)))
12502                          (max (if (numberp r) r (cdr r))))
12503                     (while (and (<= min max)
12504                                 (> all 0))
12505                       (push max older)
12506                       (setq all (1- all)
12507                             max (1- max))))))
12508             (setq older (gnus-uncompress-range older))))
12509          (all
12510           (setq older (gnus-uncompress-range older)))
12511          (t
12512           (when (and (numberp gnus-large-newsgroup)
12513                    (> len gnus-large-newsgroup))
12514               (let* ((cursor-in-echo-area nil)
12515                      (initial (gnus-parameter-large-newsgroup-initial
12516                                gnus-newsgroup-name))
12517                      (input
12518                       (read-string
12519                        (format
12520                         "How many articles from %s (%s %d): "
12521                         (gnus-group-decoded-name gnus-newsgroup-name)
12522                         (if initial "max" "default")
12523                         len)
12524                        (if initial
12525                            (cons (number-to-string initial)
12526                                  0)))))
12527                 (unless (string-match "^[ \t]*$" input)
12528                   (setq all (string-to-number input))
12529                   (if (< all len)
12530                       (let ((older-range (nreverse older)))
12531                         (setq older nil)
12532
12533                         (while (> all 0)
12534                           (let* ((r (pop older-range))
12535                                  (min (if (numberp r) r (car r)))
12536                                  (max (if (numberp r) r (cdr r))))
12537                             (while (and (<= min max)
12538                                         (> all 0))
12539                               (push max older)
12540                               (setq all (1- all)
12541                                     max (1- max))))))))))
12542           (setq older (gnus-uncompress-range older))))
12543         (if (not older)
12544             (message "No old news.")
12545           (gnus-summary-insert-articles older)
12546           (gnus-summary-limit (gnus-sorted-nunion old older))))
12547     (gnus-summary-position-point)))
12548
12549 (defun gnus-summary-insert-new-articles ()
12550   "Insert all new articles in this group."
12551   (interactive)
12552   (prog1
12553       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12554             (old-active gnus-newsgroup-active)
12555             (nnmail-fetched-sources (list t))
12556             i new)
12557         (setq gnus-newsgroup-active
12558               (gnus-activate-group gnus-newsgroup-name 'scan))
12559         (setq i (cdr gnus-newsgroup-active))
12560         (while (> i (cdr old-active))
12561           (push i new)
12562           (decf i))
12563         (if (not new)
12564             (message "No gnus is bad news")
12565           (gnus-summary-insert-articles new)
12566           (setq gnus-newsgroup-unreads
12567                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12568           (gnus-summary-limit (gnus-sorted-nunion old new))))
12569     (gnus-summary-position-point)))
12570
12571 (gnus-summary-make-all-marking-commands)
12572
12573 (gnus-ems-redefine)
12574
12575 (provide 'gnus-sum)
12576
12577 (run-hooks 'gnus-sum-load-hook)
12578
12579 ;; Local Variables:
12580 ;; coding: iso-8859-1
12581 ;; End:
12582
12583 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12584 ;;; gnus-sum.el ends here