edec081f2ce595dd6338492867fc474ff12a8fb6
[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-m\C-n" gnus-summary-sort-by-most-recent-number
1829   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1830   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1831   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1832   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1833   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1834   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1835   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1836   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1837   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1838   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1839   "=" gnus-summary-expand-window
1840   "\C-x\C-s" gnus-summary-reselect-current-group
1841   "\M-g" gnus-summary-rescan-group
1842   "w" gnus-summary-stop-page-breaking
1843   "\C-c\C-r" gnus-summary-caesar-message
1844   "f" gnus-summary-followup
1845   "F" gnus-summary-followup-with-original
1846   "C" gnus-summary-cancel-article
1847   "r" gnus-summary-reply
1848   "R" gnus-summary-reply-with-original
1849   "\C-c\C-f" gnus-summary-mail-forward
1850   "o" gnus-summary-save-article
1851   "\C-o" gnus-summary-save-article-mail
1852   "|" gnus-summary-pipe-output
1853   "\M-k" gnus-summary-edit-local-kill
1854   "\M-K" gnus-summary-edit-global-kill
1855   ;; "V" gnus-version
1856   "\C-c\C-d" gnus-summary-describe-group
1857   "q" gnus-summary-exit
1858   "Q" gnus-summary-exit-no-update
1859   "\C-c\C-i" gnus-info-find-node
1860   gnus-mouse-2 gnus-mouse-pick-article
1861   [follow-link] mouse-face
1862   "m" gnus-summary-mail-other-window
1863   "a" gnus-summary-post-news
1864   "i" gnus-summary-news-other-window
1865   "x" gnus-summary-limit-to-unread
1866   "s" gnus-summary-isearch-article
1867   "t" gnus-summary-toggle-header
1868   "g" gnus-summary-show-article
1869   "l" gnus-summary-goto-last-article
1870   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1871   "\C-d" gnus-summary-enter-digest-group
1872   "\M-\C-d" gnus-summary-read-document
1873   "\M-\C-e" gnus-summary-edit-parameters
1874   "\M-\C-a" gnus-summary-customize-parameters
1875   "\C-c\C-b" gnus-bug
1876   "*" gnus-cache-enter-article
1877   "\M-*" gnus-cache-remove-article
1878   "\M-&" gnus-summary-universal-argument
1879   "\C-l" gnus-recenter
1880   "I" gnus-summary-increase-score
1881   "L" gnus-summary-lower-score
1882   "\M-i" gnus-symbolic-argument
1883   "h" gnus-summary-select-article-buffer
1884
1885   "b" gnus-article-view-part
1886   "\M-t" gnus-summary-toggle-display-buttonized
1887
1888   "V" gnus-summary-score-map
1889   "X" gnus-uu-extract-map
1890   "S" gnus-summary-send-map)
1891
1892 ;; Sort of orthogonal keymap
1893 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1894   "t" gnus-summary-tick-article-forward
1895   "!" gnus-summary-tick-article-forward
1896   "d" gnus-summary-mark-as-read-forward
1897   "r" gnus-summary-mark-as-read-forward
1898   "c" gnus-summary-clear-mark-forward
1899   " " gnus-summary-clear-mark-forward
1900   "e" gnus-summary-mark-as-expirable
1901   "x" gnus-summary-mark-as-expirable
1902   "?" gnus-summary-mark-as-dormant
1903   "b" gnus-summary-set-bookmark
1904   "B" gnus-summary-remove-bookmark
1905   "#" gnus-summary-mark-as-processable
1906   "\M-#" gnus-summary-unmark-as-processable
1907   "S" gnus-summary-limit-include-expunged
1908   "C" gnus-summary-catchup
1909   "H" gnus-summary-catchup-to-here
1910   "h" gnus-summary-catchup-from-here
1911   "\C-c" gnus-summary-catchup-all
1912   "k" gnus-summary-kill-same-subject-and-select
1913   "K" gnus-summary-kill-same-subject
1914   "P" gnus-uu-mark-map)
1915
1916 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1917   "c" gnus-summary-clear-above
1918   "u" gnus-summary-tick-above
1919   "m" gnus-summary-mark-above
1920   "k" gnus-summary-kill-below)
1921
1922 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1923   "/" gnus-summary-limit-to-subject
1924   "n" gnus-summary-limit-to-articles
1925   "b" gnus-summary-limit-to-bodies
1926   "h" gnus-summary-limit-to-headers
1927   "w" gnus-summary-pop-limit
1928   "s" gnus-summary-limit-to-subject
1929   "a" gnus-summary-limit-to-author
1930   "u" gnus-summary-limit-to-unread
1931   "m" gnus-summary-limit-to-marks
1932   "M" gnus-summary-limit-exclude-marks
1933   "v" gnus-summary-limit-to-score
1934   "*" gnus-summary-limit-include-cached
1935   "D" gnus-summary-limit-include-dormant
1936   "T" gnus-summary-limit-include-thread
1937   "d" gnus-summary-limit-exclude-dormant
1938   "t" gnus-summary-limit-to-age
1939   "." gnus-summary-limit-to-unseen
1940   "x" gnus-summary-limit-to-extra
1941   "p" gnus-summary-limit-to-display-predicate
1942   "E" gnus-summary-limit-include-expunged
1943   "c" gnus-summary-limit-exclude-childless-dormant
1944   "C" gnus-summary-limit-mark-excluded-as-read
1945   "o" gnus-summary-insert-old-articles
1946   "N" gnus-summary-insert-new-articles
1947   "S" gnus-summary-limit-to-singletons
1948   "r" gnus-summary-limit-to-replied
1949   "R" gnus-summary-limit-to-recipient
1950   "A" gnus-summary-limit-to-address)
1951
1952 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1953   "n" gnus-summary-next-unread-article
1954   "p" gnus-summary-prev-unread-article
1955   "N" gnus-summary-next-article
1956   "P" gnus-summary-prev-article
1957   "\C-n" gnus-summary-next-same-subject
1958   "\C-p" gnus-summary-prev-same-subject
1959   "\M-n" gnus-summary-next-unread-subject
1960   "\M-p" gnus-summary-prev-unread-subject
1961   "f" gnus-summary-first-unread-article
1962   "b" gnus-summary-best-unread-article
1963   "j" gnus-summary-goto-article
1964   "g" gnus-summary-goto-subject
1965   "l" gnus-summary-goto-last-article
1966   "o" gnus-summary-pop-article)
1967
1968 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1969   "k" gnus-summary-kill-thread
1970   "E" gnus-summary-expire-thread
1971   "l" gnus-summary-lower-thread
1972   "i" gnus-summary-raise-thread
1973   "T" gnus-summary-toggle-threads
1974   "t" gnus-summary-rethread-current
1975   "^" gnus-summary-reparent-thread
1976   "\M-^" gnus-summary-reparent-children
1977   "s" gnus-summary-show-thread
1978   "S" gnus-summary-show-all-threads
1979   "h" gnus-summary-hide-thread
1980   "H" gnus-summary-hide-all-threads
1981   "n" gnus-summary-next-thread
1982   "p" gnus-summary-prev-thread
1983   "u" gnus-summary-up-thread
1984   "o" gnus-summary-top-thread
1985   "d" gnus-summary-down-thread
1986   "#" gnus-uu-mark-thread
1987   "\M-#" gnus-uu-unmark-thread)
1988
1989 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1990   "g" gnus-summary-prepare
1991   "c" gnus-summary-insert-cached-articles
1992   "d" gnus-summary-insert-dormant-articles
1993   "t" gnus-summary-insert-ticked-articles)
1994
1995 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1996   "c" gnus-summary-catchup-and-exit
1997   "C" gnus-summary-catchup-all-and-exit
1998   "E" gnus-summary-exit-no-update
1999   "Q" gnus-summary-exit
2000   "Z" gnus-summary-exit
2001   "n" gnus-summary-catchup-and-goto-next-group
2002   "p" gnus-summary-catchup-and-goto-prev-group
2003   "R" gnus-summary-reselect-current-group
2004   "G" gnus-summary-rescan-group
2005   "N" gnus-summary-next-group
2006   "s" gnus-summary-save-newsrc
2007   "P" gnus-summary-prev-group)
2008
2009 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2010   " " gnus-summary-next-page
2011   "n" gnus-summary-next-page
2012   "\177" gnus-summary-prev-page
2013   [delete] gnus-summary-prev-page
2014   "p" gnus-summary-prev-page
2015   "\r" gnus-summary-scroll-up
2016   "\M-\r" gnus-summary-scroll-down
2017   "<" gnus-summary-beginning-of-article
2018   ">" gnus-summary-end-of-article
2019   "b" gnus-summary-beginning-of-article
2020   "e" gnus-summary-end-of-article
2021   "^" gnus-summary-refer-parent-article
2022   "r" gnus-summary-refer-parent-article
2023   "D" gnus-summary-enter-digest-group
2024   "R" gnus-summary-refer-references
2025   "T" gnus-summary-refer-thread
2026   "g" gnus-summary-show-article
2027   "s" gnus-summary-isearch-article
2028   "P" gnus-summary-print-article
2029   "S" gnus-sticky-article
2030   "M" gnus-mailing-list-insinuate
2031   "t" gnus-article-babel)
2032
2033 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2034   "b" gnus-article-add-buttons
2035   "B" gnus-article-add-buttons-to-head
2036   "o" gnus-article-treat-overstrike
2037   "e" gnus-article-emphasize
2038   "w" gnus-article-fill-cited-article
2039   "Q" gnus-article-fill-long-lines
2040   "L" gnus-article-toggle-truncate-lines
2041   "C" gnus-article-capitalize-sentences
2042   "c" gnus-article-remove-cr
2043   "q" gnus-article-de-quoted-unreadable
2044   "6" gnus-article-de-base64-unreadable
2045   "Z" gnus-article-decode-HZ
2046   "A" gnus-article-treat-ansi-sequences
2047   "h" gnus-article-wash-html
2048   "u" gnus-article-unsplit-urls
2049   "s" gnus-summary-force-verify-and-decrypt
2050   "f" gnus-article-display-x-face
2051   "l" gnus-summary-stop-page-breaking
2052   "r" gnus-summary-caesar-message
2053   "m" gnus-summary-morse-message
2054   "t" gnus-summary-toggle-header
2055   "g" gnus-treat-smiley
2056   "v" gnus-summary-verbose-headers
2057   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2058   "p" gnus-article-verify-x-pgp-sig
2059   "d" gnus-article-treat-dumbquotes
2060   "i" gnus-summary-idna-message)
2061
2062 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2063   ;; mnemonic: deuglif*Y*
2064   "u" gnus-article-outlook-unwrap-lines
2065   "a" gnus-article-outlook-repair-attribution
2066   "c" gnus-article-outlook-rearrange-citation
2067   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2068
2069 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2070   "a" gnus-article-hide
2071   "h" gnus-article-hide-headers
2072   "b" gnus-article-hide-boring-headers
2073   "s" gnus-article-hide-signature
2074   "c" gnus-article-hide-citation
2075   "C" gnus-article-hide-citation-in-followups
2076   "l" gnus-article-hide-list-identifiers
2077   "B" gnus-article-strip-banner
2078   "P" gnus-article-hide-pem
2079   "\C-c" gnus-article-hide-citation-maybe)
2080
2081 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2082   "a" gnus-article-highlight
2083   "h" gnus-article-highlight-headers
2084   "c" gnus-article-highlight-citation
2085   "s" gnus-article-highlight-signature)
2086
2087 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2088   "f" gnus-article-treat-fold-headers
2089   "u" gnus-article-treat-unfold-headers
2090   "n" gnus-article-treat-fold-newsgroups)
2091
2092 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2093   "x" gnus-article-display-x-face
2094   "d" gnus-article-display-face
2095   "s" gnus-treat-smiley
2096   "D" gnus-article-remove-images
2097   "f" gnus-treat-from-picon
2098   "m" gnus-treat-mail-picon
2099   "n" gnus-treat-newsgroups-picon)
2100
2101 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2102   "w" gnus-article-decode-mime-words
2103   "c" gnus-article-decode-charset
2104   "v" gnus-mime-view-all-parts
2105   "b" gnus-article-view-part)
2106
2107 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2108   "z" gnus-article-date-ut
2109   "u" gnus-article-date-ut
2110   "l" gnus-article-date-local
2111   "p" gnus-article-date-english
2112   "e" gnus-article-date-lapsed
2113   "o" gnus-article-date-original
2114   "i" gnus-article-date-iso8601
2115   "s" gnus-article-date-user)
2116
2117 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2118   "t" gnus-article-remove-trailing-blank-lines
2119   "l" gnus-article-strip-leading-blank-lines
2120   "m" gnus-article-strip-multiple-blank-lines
2121   "a" gnus-article-strip-blank-lines
2122   "A" gnus-article-strip-all-blank-lines
2123   "s" gnus-article-strip-leading-space
2124   "e" gnus-article-strip-trailing-space
2125   "w" gnus-article-remove-leading-whitespace)
2126
2127 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2128   "v" gnus-version
2129   "f" gnus-summary-fetch-faq
2130   "d" gnus-summary-describe-group
2131   "h" gnus-summary-describe-briefly
2132   "i" gnus-info-find-node
2133   "c" gnus-group-fetch-charter
2134   "C" gnus-group-fetch-control)
2135
2136 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2137   "e" gnus-summary-expire-articles
2138   "\M-\C-e" gnus-summary-expire-articles-now
2139   "\177" gnus-summary-delete-article
2140   [delete] gnus-summary-delete-article
2141   [backspace] gnus-summary-delete-article
2142   "m" gnus-summary-move-article
2143   "r" gnus-summary-respool-article
2144   "w" gnus-summary-edit-article
2145   "c" gnus-summary-copy-article
2146   "B" gnus-summary-crosspost-article
2147   "q" gnus-summary-respool-query
2148   "t" gnus-summary-respool-trace
2149   "i" gnus-summary-import-article
2150   "I" gnus-summary-create-article
2151   "p" gnus-summary-article-posted-p)
2152
2153 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2154   "o" gnus-summary-save-article
2155   "m" gnus-summary-save-article-mail
2156   "F" gnus-summary-write-article-file
2157   "r" gnus-summary-save-article-rmail
2158   "f" gnus-summary-save-article-file
2159   "b" gnus-summary-save-article-body-file
2160   "B" gnus-summary-write-article-body-file
2161   "h" gnus-summary-save-article-folder
2162   "v" gnus-summary-save-article-vm
2163   "p" gnus-summary-pipe-output
2164   "P" gnus-summary-muttprint
2165   "s" gnus-soup-add-article)
2166
2167 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2168   "b" gnus-summary-display-buttonized
2169   "m" gnus-summary-repair-multipart
2170   "v" gnus-article-view-part
2171   "o" gnus-article-save-part
2172   "O" gnus-article-save-part-and-strip
2173   "r" gnus-article-replace-part
2174   "d" gnus-article-delete-part
2175   "t" gnus-article-view-part-as-type
2176   "j" gnus-article-jump-to-part
2177   "c" gnus-article-copy-part
2178   "C" gnus-article-view-part-as-charset
2179   "e" gnus-article-view-part-externally
2180   "H" gnus-article-browse-html-article
2181   "E" gnus-article-encrypt-body
2182   "i" gnus-article-inline-part
2183   "|" gnus-article-pipe-part)
2184
2185 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2186   "p" gnus-summary-mark-as-processable
2187   "u" gnus-summary-unmark-as-processable
2188   "U" gnus-summary-unmark-all-processable
2189   "v" gnus-uu-mark-over
2190   "s" gnus-uu-mark-series
2191   "r" gnus-uu-mark-region
2192   "g" gnus-uu-unmark-region
2193   "R" gnus-uu-mark-by-regexp
2194   "G" gnus-uu-unmark-by-regexp
2195   "t" gnus-uu-mark-thread
2196   "T" gnus-uu-unmark-thread
2197   "a" gnus-uu-mark-all
2198   "b" gnus-uu-mark-buffer
2199   "S" gnus-uu-mark-sparse
2200   "k" gnus-summary-kill-process-mark
2201   "y" gnus-summary-yank-process-mark
2202   "w" gnus-summary-save-process-mark
2203   "i" gnus-uu-invert-processable)
2204
2205 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2206   ;;"x" gnus-uu-extract-any
2207   "m" gnus-summary-save-parts
2208   "u" gnus-uu-decode-uu
2209   "U" gnus-uu-decode-uu-and-save
2210   "s" gnus-uu-decode-unshar
2211   "S" gnus-uu-decode-unshar-and-save
2212   "o" gnus-uu-decode-save
2213   "O" gnus-uu-decode-save
2214   "b" gnus-uu-decode-binhex
2215   "B" gnus-uu-decode-binhex
2216   "Y" gnus-uu-decode-yenc
2217   "p" gnus-uu-decode-postscript
2218   "P" gnus-uu-decode-postscript-and-save)
2219
2220 (gnus-define-keys
2221     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2222   "u" gnus-uu-decode-uu-view
2223   "U" gnus-uu-decode-uu-and-save-view
2224   "s" gnus-uu-decode-unshar-view
2225   "S" gnus-uu-decode-unshar-and-save-view
2226   "o" gnus-uu-decode-save-view
2227   "O" gnus-uu-decode-save-view
2228   "b" gnus-uu-decode-binhex-view
2229   "B" gnus-uu-decode-binhex-view
2230   "p" gnus-uu-decode-postscript-view
2231   "P" gnus-uu-decode-postscript-and-save-view)
2232
2233 (defvar gnus-article-post-menu nil)
2234
2235 (defconst gnus-summary-menu-maxlen 20)
2236
2237 (defun gnus-summary-menu-split (menu)
2238   ;; If we have lots of elements, divide them into groups of 20
2239   ;; and make a pane (or submenu) for each one.
2240   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2241       (let ((menu menu) sublists next
2242             (i 1))
2243         (while menu
2244           ;; Pull off the next gnus-summary-menu-maxlen elements
2245           ;; and make them the next element of sublist.
2246           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2247           (if next
2248               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2249                       nil))
2250           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2251                                              (aref (car (last menu)) 0)) menu)
2252                                sublists))
2253           (setq i (1+ i))
2254           (setq menu next))
2255         (nreverse sublists))
2256     ;; Few elements--put them all in one pane.
2257     menu))
2258
2259 (defun gnus-summary-make-menu-bar ()
2260   (gnus-turn-off-edit-menu 'summary)
2261
2262   (unless (boundp 'gnus-summary-misc-menu)
2263
2264     (easy-menu-define
2265       gnus-summary-kill-menu gnus-summary-mode-map ""
2266       (cons
2267        "Score"
2268        (nconc
2269         (list
2270          ["Customize" gnus-score-customize t])
2271         (gnus-make-score-map 'increase)
2272         (gnus-make-score-map 'lower)
2273         '(("Mark"
2274            ["Kill below" gnus-summary-kill-below t]
2275            ["Mark above" gnus-summary-mark-above t]
2276            ["Tick above" gnus-summary-tick-above t]
2277            ["Clear above" gnus-summary-clear-above t])
2278           ["Current score" gnus-summary-current-score t]
2279           ["Set score" gnus-summary-set-score t]
2280           ["Switch current score file..." gnus-score-change-score-file t]
2281           ["Set mark below..." gnus-score-set-mark-below t]
2282           ["Set expunge below..." gnus-score-set-expunge-below t]
2283           ["Edit current score file" gnus-score-edit-current-scores t]
2284           ["Edit score file..." gnus-score-edit-file t]
2285           ["Trace score" gnus-score-find-trace t]
2286           ["Find words" gnus-score-find-favourite-words t]
2287           ["Rescore buffer" gnus-summary-rescore t]
2288           ["Increase score..." gnus-summary-increase-score t]
2289           ["Lower score..." gnus-summary-lower-score t]))))
2290
2291     ;; Define both the Article menu in the summary buffer and the
2292     ;; equivalent Commands menu in the article buffer here for
2293     ;; consistency.
2294     (let ((innards
2295            `(("Hide"
2296               ["All" gnus-article-hide t]
2297               ["Headers" gnus-article-hide-headers t]
2298               ["Signature" gnus-article-hide-signature t]
2299               ["Citation" gnus-article-hide-citation t]
2300               ["List identifiers" gnus-article-hide-list-identifiers t]
2301               ["Banner" gnus-article-strip-banner t]
2302               ["Boring headers" gnus-article-hide-boring-headers t])
2303              ("Highlight"
2304               ["All" gnus-article-highlight t]
2305               ["Headers" gnus-article-highlight-headers t]
2306               ["Signature" gnus-article-highlight-signature t]
2307               ["Citation" gnus-article-highlight-citation t])
2308              ("MIME"
2309               ["Words" gnus-article-decode-mime-words t]
2310               ["Charset" gnus-article-decode-charset t]
2311               ["QP" gnus-article-de-quoted-unreadable t]
2312               ["Base64" gnus-article-de-base64-unreadable t]
2313               ["View MIME buttons" gnus-summary-display-buttonized t]
2314               ["View all" gnus-mime-view-all-parts t]
2315               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2316               ["Encrypt body" gnus-article-encrypt-body
2317                :active (not (gnus-group-read-only-p))
2318                ,@(if (featurep 'xemacs) nil
2319                    '(:help "Encrypt the message body on disk"))]
2320               ["Extract all parts..." gnus-summary-save-parts t]
2321               ("Multipart"
2322                ["Repair multipart" gnus-summary-repair-multipart t]
2323                ["Pipe part..." gnus-article-pipe-part t]
2324                ["Inline part" gnus-article-inline-part t]
2325                ["View part as type..." gnus-article-view-part-as-type t]
2326                ["Encrypt body" gnus-article-encrypt-body
2327                 :active (not (gnus-group-read-only-p))
2328                ,@(if (featurep 'xemacs) nil
2329                    '(:help "Encrypt the message body on disk"))]
2330                ["View part externally" gnus-article-view-part-externally t]
2331                ["View HTML parts in browser" gnus-article-browse-html-article t]
2332                ["View part with charset..." gnus-article-view-part-as-charset t]
2333                ["Copy part" gnus-article-copy-part t]
2334                ["Save part..." gnus-article-save-part t]
2335                ["View part" gnus-article-view-part t]))
2336              ("Date"
2337               ["Local" gnus-article-date-local t]
2338               ["ISO8601" gnus-article-date-iso8601 t]
2339               ["UT" gnus-article-date-ut t]
2340               ["Original" gnus-article-date-original t]
2341               ["Lapsed" gnus-article-date-lapsed t]
2342               ["User-defined" gnus-article-date-user t])
2343              ("Display"
2344               ["Remove images" gnus-article-remove-images t]
2345               ["Toggle smiley" gnus-treat-smiley t]
2346               ["Show X-Face" gnus-article-display-x-face t]
2347               ["Show picons in From" gnus-treat-from-picon t]
2348               ["Show picons in mail headers" gnus-treat-mail-picon t]
2349               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2350               ("View as different encoding"
2351                ,@(gnus-summary-menu-split
2352                   (mapcar
2353                    (lambda (cs)
2354                      ;; Since easymenu under Emacs doesn't allow
2355                      ;; lambda forms for menu commands, we should
2356                      ;; provide intern'ed function symbols.
2357                      (let ((command (intern (format "\
2358 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2359                        (fset command
2360                              `(lambda ()
2361                                 (interactive)
2362                                 (let ((gnus-summary-show-article-charset-alist
2363                                        '((1 . ,cs))))
2364                                   (gnus-summary-show-article 1))))
2365                        `[,(symbol-name cs) ,command t]))
2366                    (sort (if (fboundp 'coding-system-list)
2367                              (coding-system-list)
2368                            (mapcar 'car mm-mime-mule-charset-alist))
2369                          'string<)))))
2370              ("Washing"
2371               ("Remove Blanks"
2372                ["Leading" gnus-article-strip-leading-blank-lines t]
2373                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2374                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2375                ["All of the above" gnus-article-strip-blank-lines t]
2376                ["All" gnus-article-strip-all-blank-lines t]
2377                ["Leading space" gnus-article-strip-leading-space t]
2378                ["Trailing space" gnus-article-strip-trailing-space t]
2379                ["Leading space in headers"
2380                 gnus-article-remove-leading-whitespace t])
2381               ["Overstrike" gnus-article-treat-overstrike t]
2382               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2383               ["Emphasis" gnus-article-emphasize t]
2384               ["Word wrap" gnus-article-fill-cited-article t]
2385               ["Fill long lines" gnus-article-fill-long-lines t]
2386               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2387               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2388               ["Remove CR" gnus-article-remove-cr t]
2389               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2390               ["Base64" gnus-article-de-base64-unreadable t]
2391               ["Rot 13" gnus-summary-caesar-message
2392                ,@(if (featurep 'xemacs) '(t)
2393                    '(:help "\"Caesar rotate\" article by 13"))]
2394               ["De-IDNA" gnus-summary-idna-message t]
2395               ["Morse decode" gnus-summary-morse-message t]
2396               ["Unix pipe..." gnus-summary-pipe-message t]
2397               ["Add buttons" gnus-article-add-buttons t]
2398               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2399               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2400               ["Verbose header" gnus-summary-verbose-headers t]
2401               ["Toggle header" gnus-summary-toggle-header t]
2402               ["Unfold headers" gnus-article-treat-unfold-headers t]
2403               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2404               ["Html" gnus-article-wash-html t]
2405               ["Unsplit URLs" gnus-article-unsplit-urls t]
2406               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2407               ["Decode HZ" gnus-article-decode-HZ t]
2408               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2409               ("(Outlook) Deuglify"
2410                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2411                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2412                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2413                ["Full (Outlook) deuglify"
2414                 gnus-article-outlook-deuglify-article t])
2415               )
2416              ("Output"
2417               ["Save in default format..." gnus-summary-save-article
2418                ,@(if (featurep 'xemacs) '(t)
2419                    '(:help "Save article using default method"))]
2420               ["Save in file..." gnus-summary-save-article-file
2421                ,@(if (featurep 'xemacs) '(t)
2422                    '(:help "Save article in file"))]
2423               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2424               ["Save in MH folder..." gnus-summary-save-article-folder t]
2425               ["Save in VM folder..." gnus-summary-save-article-vm t]
2426               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2427               ["Save body in file..." gnus-summary-save-article-body-file t]
2428               ["Pipe through a filter..." gnus-summary-pipe-output t]
2429               ["Add to SOUP packet" gnus-soup-add-article t]
2430               ["Print with Muttprint..." gnus-summary-muttprint t]
2431               ["Print" gnus-summary-print-article
2432                ,@(if (featurep 'xemacs) '(t)
2433                    '(:help "Generate and print a PostScript image"))])
2434              ("Copy, move,... (Backend)"
2435               ,@(if (featurep 'xemacs) nil
2436                   '(:help "Copying, moving, expiring articles..."))
2437               ["Respool article..." gnus-summary-respool-article t]
2438               ["Move article..." gnus-summary-move-article
2439                (gnus-check-backend-function
2440                 'request-move-article gnus-newsgroup-name)]
2441               ["Copy article..." gnus-summary-copy-article t]
2442               ["Crosspost article..." gnus-summary-crosspost-article
2443                (gnus-check-backend-function
2444                 'request-replace-article gnus-newsgroup-name)]
2445               ["Import file..." gnus-summary-import-article
2446                (gnus-check-backend-function
2447                 'request-accept-article gnus-newsgroup-name)]
2448               ["Create article..." gnus-summary-create-article
2449                (gnus-check-backend-function
2450                 'request-accept-article gnus-newsgroup-name)]
2451               ["Check if posted" gnus-summary-article-posted-p t]
2452               ["Edit article" gnus-summary-edit-article
2453                (not (gnus-group-read-only-p))]
2454               ["Delete article" gnus-summary-delete-article
2455                (gnus-check-backend-function
2456                 'request-expire-articles gnus-newsgroup-name)]
2457               ["Query respool" gnus-summary-respool-query t]
2458               ["Trace respool" gnus-summary-respool-trace t]
2459               ["Delete expirable articles" gnus-summary-expire-articles-now
2460                (gnus-check-backend-function
2461                 'request-expire-articles gnus-newsgroup-name)])
2462              ("Extract"
2463               ["Uudecode" gnus-uu-decode-uu
2464                ,@(if (featurep 'xemacs) '(t)
2465                    '(:help "Decode uuencoded article(s)"))]
2466               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2467               ["Unshar" gnus-uu-decode-unshar t]
2468               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2469               ["Save" gnus-uu-decode-save t]
2470               ["Binhex" gnus-uu-decode-binhex t]
2471               ["Postscript" gnus-uu-decode-postscript t]
2472               ["All MIME parts" gnus-summary-save-parts t])
2473              ("Cache"
2474               ["Enter article" gnus-cache-enter-article t]
2475               ["Remove article" gnus-cache-remove-article t])
2476              ["Translate" gnus-article-babel t]
2477              ["Select article buffer" gnus-summary-select-article-buffer t]
2478              ["Make article buffer sticky" gnus-sticky-article t]
2479              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2480              ["Isearch article..." gnus-summary-isearch-article t]
2481              ["Beginning of the article" gnus-summary-beginning-of-article t]
2482              ["End of the article" gnus-summary-end-of-article t]
2483              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2484              ["Fetch referenced articles" gnus-summary-refer-references t]
2485              ["Fetch current thread" gnus-summary-refer-thread t]
2486              ["Fetch article with id..." gnus-summary-refer-article t]
2487              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2488              ["Redisplay" gnus-summary-show-article t]
2489              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2490       (easy-menu-define
2491         gnus-summary-article-menu gnus-summary-mode-map ""
2492         (cons "Article" innards))
2493
2494       (if (not (keymapp gnus-summary-article-menu))
2495           (easy-menu-define
2496             gnus-article-commands-menu gnus-article-mode-map ""
2497             (cons "Commands" innards))
2498         ;; in Emacs, don't share menu.
2499         (setq gnus-article-commands-menu
2500               (copy-keymap gnus-summary-article-menu))
2501         (define-key gnus-article-mode-map [menu-bar commands]
2502           (cons "Commands" gnus-article-commands-menu))))
2503
2504     (easy-menu-define
2505       gnus-summary-thread-menu gnus-summary-mode-map ""
2506       '("Threads"
2507         ["Find all messages in thread" gnus-summary-refer-thread t]
2508         ["Toggle threading" gnus-summary-toggle-threads t]
2509         ["Hide threads" gnus-summary-hide-all-threads t]
2510         ["Show threads" gnus-summary-show-all-threads t]
2511         ["Hide thread" gnus-summary-hide-thread t]
2512         ["Show thread" gnus-summary-show-thread t]
2513         ["Go to next thread" gnus-summary-next-thread t]
2514         ["Go to previous thread" gnus-summary-prev-thread t]
2515         ["Go down thread" gnus-summary-down-thread t]
2516         ["Go up thread" gnus-summary-up-thread t]
2517         ["Top of thread" gnus-summary-top-thread t]
2518         ["Mark thread as read" gnus-summary-kill-thread t]
2519         ["Mark thread as expired" gnus-summary-expire-thread t]
2520         ["Lower thread score" gnus-summary-lower-thread t]
2521         ["Raise thread score" gnus-summary-raise-thread t]
2522         ["Rethread current" gnus-summary-rethread-current t]))
2523
2524     (easy-menu-define
2525       gnus-summary-post-menu gnus-summary-mode-map ""
2526       `("Post"
2527         ["Send a message (mail or news)" gnus-summary-post-news
2528          ,@(if (featurep 'xemacs) '(t)
2529              '(:help "Compose a new message (mail or news)"))]
2530         ["Followup" gnus-summary-followup
2531          ,@(if (featurep 'xemacs) '(t)
2532              '(:help "Post followup to this article"))]
2533         ["Followup and yank" gnus-summary-followup-with-original
2534          ,@(if (featurep 'xemacs) '(t)
2535              '(:help "Post followup to this article, quoting its contents"))]
2536         ["Supersede article" gnus-summary-supersede-article t]
2537         ["Cancel article" gnus-summary-cancel-article
2538          ,@(if (featurep 'xemacs) '(t)
2539              '(:help "Cancel an article you posted"))]
2540         ["Reply" gnus-summary-reply t]
2541         ["Reply and yank" gnus-summary-reply-with-original t]
2542         ["Wide reply" gnus-summary-wide-reply t]
2543         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2544          ,@(if (featurep 'xemacs) '(t)
2545              '(:help "Mail a reply, quoting this article"))]
2546         ["Very wide reply" gnus-summary-very-wide-reply t]
2547         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2548          ,@(if (featurep 'xemacs) '(t)
2549              '(:help "Mail a very wide reply, quoting this article"))]
2550         ["Mail forward" gnus-summary-mail-forward t]
2551         ["Post forward" gnus-summary-post-forward t]
2552         ["Digest and mail" gnus-uu-digest-mail-forward t]
2553         ["Digest and post" gnus-uu-digest-post-forward t]
2554         ["Resend message" gnus-summary-resend-message t]
2555         ["Resend message edit" gnus-summary-resend-message-edit t]
2556         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2557         ["Send a mail" gnus-summary-mail-other-window t]
2558         ["Create a local message" gnus-summary-news-other-window t]
2559         ["Uuencode and post" gnus-uu-post-news
2560          ,@(if (featurep 'xemacs) '(t)
2561              '(:help "Post a uuencoded article"))]
2562         ["Followup via news" gnus-summary-followup-to-mail t]
2563         ["Followup via news and yank"
2564          gnus-summary-followup-to-mail-with-original t]
2565         ["Strip signature on reply"
2566          (lambda ()
2567            (interactive)
2568            (if (not (memq message-cite-function
2569                           '(message-cite-original-without-signature
2570                             message-cite-original)))
2571                ;; Stupid workaround for XEmacs not honoring :visible.
2572                (message "Can't toggle this value of `message-cite-function'")
2573              (setq message-cite-function
2574                    (if (eq message-cite-function
2575                            'message-cite-original-without-signature)
2576                        'message-cite-original
2577                      'message-cite-original-without-signature))))
2578          ;; XEmacs barfs on :visible.
2579          ,@(if (featurep 'xemacs) nil
2580              '(:visible (memq message-cite-function
2581                               '(message-cite-original-without-signature
2582                                 message-cite-original))))
2583          :style toggle
2584          :selected (eq message-cite-function
2585                        'message-cite-original-without-signature)
2586          ,@(if (featurep 'xemacs) nil
2587              '(:help "Strip signature from cited article when replying."))]
2588         ;;("Draft"
2589         ;;["Send" gnus-summary-send-draft t]
2590         ;;["Send bounced" gnus-resend-bounced-mail t])
2591         ))
2592
2593     (cond
2594      ((not (keymapp gnus-summary-post-menu))
2595       (setq gnus-article-post-menu gnus-summary-post-menu))
2596      ((not gnus-article-post-menu)
2597       ;; Don't share post menu.
2598       (setq gnus-article-post-menu
2599             (copy-keymap gnus-summary-post-menu))))
2600     (define-key gnus-article-mode-map [menu-bar post]
2601       (cons "Post" gnus-article-post-menu))
2602
2603     (easy-menu-define
2604       gnus-summary-misc-menu gnus-summary-mode-map ""
2605       `("Gnus"
2606         ("Mark Read"
2607          ["Mark as read" gnus-summary-mark-as-read-forward t]
2608          ["Mark same subject and select"
2609           gnus-summary-kill-same-subject-and-select t]
2610          ["Mark same subject" gnus-summary-kill-same-subject t]
2611          ["Catchup" gnus-summary-catchup
2612           ,@(if (featurep 'xemacs) '(t)
2613               '(:help "Mark unread articles in this group as read"))]
2614          ["Catchup all" gnus-summary-catchup-all t]
2615          ["Catchup to here" gnus-summary-catchup-to-here t]
2616          ["Catchup from here" gnus-summary-catchup-from-here t]
2617          ["Catchup region" gnus-summary-mark-region-as-read
2618           (gnus-mark-active-p)]
2619          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2620         ("Mark Various"
2621          ["Tick" gnus-summary-tick-article-forward t]
2622          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2623          ["Remove marks" gnus-summary-clear-mark-forward t]
2624          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2625          ["Set bookmark" gnus-summary-set-bookmark t]
2626          ["Remove bookmark" gnus-summary-remove-bookmark t])
2627         ("Registry Mark"
2628          ["Important"     gnus-registry-set-article-Important-mark    t]
2629          ["Not Important" gnus-registry-remove-article-Important-mark t]
2630          ["Work"          gnus-registry-set-article-Work-mark         t]
2631          ["Not Work"      gnus-registry-remove-article-Work-mark      t]
2632          ["Later"         gnus-registry-set-article-Later-mark        t]
2633          ["Not Later"     gnus-registry-remove-article-Later-mark     t]
2634          ["Personal"      gnus-registry-set-article-Personal-mark     t]
2635          ["Not Personal"  gnus-registry-remove-article-Personal-mark  t]
2636          ["To Do"         gnus-registry-set-article-To-Do-mark        t]
2637          ["Not To Do"     gnus-registry-remove-article-To-Do-mark     t])
2638         ("Limit to"
2639          ["Marks..." gnus-summary-limit-to-marks t]
2640          ["Subject..." gnus-summary-limit-to-subject t]
2641          ["Author..." gnus-summary-limit-to-author t]
2642          ["Recipient..." gnus-summary-limit-to-recipient t]
2643          ["Address..." gnus-summary-limit-to-address t]
2644          ["Age..." gnus-summary-limit-to-age t]
2645          ["Extra..." gnus-summary-limit-to-extra t]
2646          ["Score..." gnus-summary-limit-to-score t]
2647          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2648          ["Unread" gnus-summary-limit-to-unread t]
2649          ["Unseen" gnus-summary-limit-to-unseen t]
2650          ["Singletons" gnus-summary-limit-to-singletons t]
2651          ["Replied" gnus-summary-limit-to-replied t]
2652          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2653          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2654          ["Pop limit" gnus-summary-pop-limit t]
2655          ["Show dormant" gnus-summary-limit-include-dormant t]
2656          ["Hide childless dormant"
2657           gnus-summary-limit-exclude-childless-dormant t]
2658          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2659          ["Hide marked" gnus-summary-limit-exclude-marks t]
2660          ["Show expunged" gnus-summary-limit-include-expunged t])
2661         ("Process Mark"
2662          ["Set mark" gnus-summary-mark-as-processable t]
2663          ["Remove mark" gnus-summary-unmark-as-processable t]
2664          ["Remove all marks" gnus-summary-unmark-all-processable t]
2665          ["Invert marks" gnus-uu-invert-processable t]
2666          ["Mark above" gnus-uu-mark-over t]
2667          ["Mark series" gnus-uu-mark-series t]
2668          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2669          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2670          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2671          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2672          ["Mark all" gnus-uu-mark-all t]
2673          ["Mark buffer" gnus-uu-mark-buffer t]
2674          ["Mark sparse" gnus-uu-mark-sparse t]
2675          ["Mark thread" gnus-uu-mark-thread t]
2676          ["Unmark thread" gnus-uu-unmark-thread t]
2677          ("Process Mark Sets"
2678           ["Kill" gnus-summary-kill-process-mark t]
2679           ["Yank" gnus-summary-yank-process-mark
2680            gnus-newsgroup-process-stack]
2681           ["Save" gnus-summary-save-process-mark t]
2682           ["Run command on marked..." gnus-summary-universal-argument t]))
2683         ("Scroll article"
2684          ["Page forward" gnus-summary-next-page
2685           ,@(if (featurep 'xemacs) '(t)
2686               '(:help "Show next page of article"))]
2687          ["Page backward" gnus-summary-prev-page
2688           ,@(if (featurep 'xemacs) '(t)
2689               '(:help "Show previous page of article"))]
2690          ["Line forward" gnus-summary-scroll-up t])
2691         ("Move"
2692          ["Next unread article" gnus-summary-next-unread-article t]
2693          ["Previous unread article" gnus-summary-prev-unread-article t]
2694          ["Next article" gnus-summary-next-article t]
2695          ["Previous article" gnus-summary-prev-article t]
2696          ["Next unread subject" gnus-summary-next-unread-subject t]
2697          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2698          ["Next article same subject" gnus-summary-next-same-subject t]
2699          ["Previous article same subject" gnus-summary-prev-same-subject t]
2700          ["First unread article" gnus-summary-first-unread-article t]
2701          ["Best unread article" gnus-summary-best-unread-article t]
2702          ["Go to subject number..." gnus-summary-goto-subject t]
2703          ["Go to article number..." gnus-summary-goto-article t]
2704          ["Go to the last article" gnus-summary-goto-last-article t]
2705          ["Pop article off history" gnus-summary-pop-article t])
2706         ("Sort"
2707          ["Sort by number" gnus-summary-sort-by-number t]
2708          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2709          ["Sort by author" gnus-summary-sort-by-author t]
2710          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2711          ["Sort by subject" gnus-summary-sort-by-subject t]
2712          ["Sort by date" gnus-summary-sort-by-date t]
2713          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2714          ["Sort by score" gnus-summary-sort-by-score t]
2715          ["Sort by lines" gnus-summary-sort-by-lines t]
2716          ["Sort by characters" gnus-summary-sort-by-chars t]
2717          ["Randomize" gnus-summary-sort-by-random t]
2718          ["Original sort" gnus-summary-sort-by-original t])
2719         ("Help"
2720          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2721          ["Describe group" gnus-summary-describe-group t]
2722          ["Fetch charter" gnus-group-fetch-charter
2723           ,@(if (featurep 'xemacs) nil
2724               '(:help "Display the charter of the current group"))]
2725          ["Fetch control message" gnus-group-fetch-control
2726           ,@(if (featurep 'xemacs) nil
2727               '(:help "Display the archived control message for the current group"))]
2728          ["Read manual" gnus-info-find-node t])
2729         ("Modes"
2730          ["Pick and read" gnus-pick-mode t]
2731          ["Binary" gnus-binary-mode t])
2732         ("Regeneration"
2733          ["Regenerate" gnus-summary-prepare t]
2734          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2735          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2736          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2737          ["Toggle threading" gnus-summary-toggle-threads t])
2738         ["See old articles" gnus-summary-insert-old-articles t]
2739         ["See new articles" gnus-summary-insert-new-articles t]
2740         ["Filter articles..." gnus-summary-execute-command t]
2741         ["Run command on articles..." gnus-summary-universal-argument t]
2742         ["Search articles forward..." gnus-summary-search-article-forward t]
2743         ["Search articles backward..." gnus-summary-search-article-backward t]
2744         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2745         ["Expand window" gnus-summary-expand-window t]
2746         ["Expire expirable articles" gnus-summary-expire-articles
2747          (gnus-check-backend-function
2748           'request-expire-articles gnus-newsgroup-name)]
2749         ["Edit local kill file" gnus-summary-edit-local-kill t]
2750         ["Edit main kill file" gnus-summary-edit-global-kill t]
2751         ["Edit group parameters" gnus-summary-edit-parameters t]
2752         ["Customize group parameters" gnus-summary-customize-parameters t]
2753         ["Send a bug report" gnus-bug t]
2754         ("Exit"
2755          ["Catchup and exit" gnus-summary-catchup-and-exit
2756           ,@(if (featurep 'xemacs) '(t)
2757               '(:help "Mark unread articles in this group as read, then exit"))]
2758          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2759          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2760          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2761          ["Exit group" gnus-summary-exit
2762           ,@(if (featurep 'xemacs) '(t)
2763               '(:help "Exit current group, return to group selection mode"))]
2764          ["Exit group without updating" gnus-summary-exit-no-update t]
2765          ["Exit and goto next group" gnus-summary-next-group t]
2766          ["Exit and goto prev group" gnus-summary-prev-group t]
2767          ["Reselect group" gnus-summary-reselect-current-group t]
2768          ["Rescan group" gnus-summary-rescan-group t]
2769          ["Update dribble" gnus-summary-save-newsrc t])))
2770
2771     (gnus-run-hooks 'gnus-summary-menu-hook)))
2772
2773 (defvar gnus-summary-tool-bar-map nil)
2774
2775 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2776 ;; affect _new_ message buffers.  We might add a function that walks thru all
2777 ;; summary-mode buffers and force the update.
2778 (defun gnus-summary-tool-bar-update (&optional symbol value)
2779   "Update summary mode toolbar.
2780 Setter function for custom variables."
2781   (setq-default gnus-summary-tool-bar-map nil)
2782   (when symbol
2783     ;; When used as ":set" function:
2784     (set-default symbol value))
2785   (when (gnus-buffer-live-p gnus-summary-buffer)
2786     (with-current-buffer gnus-summary-buffer
2787       (gnus-summary-make-tool-bar))))
2788
2789 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2790                                      'gnus-summary-tool-bar-gnome
2791                                    'gnus-summary-tool-bar-retro)
2792   "Specifies the Gnus summary tool bar.
2793
2794 It can be either a list or a symbol refering to a list.  See
2795 `gmm-tool-bar-from-list' for the format of the list.  The
2796 default key map is `gnus-summary-mode-map'.
2797
2798 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2799 `gnus-summary-tool-bar-retro'."
2800   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2801                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2802                  (repeat :tag "User defined list" gmm-tool-bar-item)
2803                  (symbol))
2804   :version "23.1" ;; No Gnus
2805   :initialize 'custom-initialize-default
2806   :set 'gnus-summary-tool-bar-update
2807   :group 'gnus-summary)
2808
2809 (defcustom gnus-summary-tool-bar-gnome
2810   '((gnus-summary-post-news "mail/compose" nil)
2811     (gnus-summary-insert-new-articles "mail/inbox" nil
2812                                       :visible (or (not gnus-agent)
2813                                                    gnus-plugged))
2814     (gnus-summary-reply-with-original "mail/reply")
2815     (gnus-summary-reply "mail/reply" nil :visible nil)
2816     (gnus-summary-followup-with-original "mail/reply-all")
2817     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2818     (gnus-summary-mail-forward "mail/forward")
2819     (gnus-summary-save-article "mail/save")
2820     (gnus-summary-search-article-forward "search" nil :visible nil)
2821     (gnus-summary-print-article "print")
2822     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2823     ;; Some new commands that may need more suitable icons:
2824     (gnus-summary-save-newsrc "save" nil :visible nil)
2825     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2826     (gnus-summary-prev-article "left-arrow")
2827     (gnus-summary-next-article "right-arrow")
2828     (gnus-summary-next-page "next-page")
2829     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2830     ;;
2831     ;; Maybe some sort-by-... could be added:
2832     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2833     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2834     (gnus-summary-mark-as-expirable
2835      "delete" nil
2836      :visible (gnus-check-backend-function 'request-expire-articles
2837                                            gnus-newsgroup-name))
2838     (gnus-summary-mark-as-spam
2839      "mail/spam" t
2840      :visible (and (fboundp 'spam-group-ham-contents-p)
2841                    (spam-group-ham-contents-p gnus-newsgroup-name))
2842      :help "Mark as spam")
2843     (gnus-summary-mark-as-read-forward
2844      "mail/not-spam" nil
2845      :visible (and (fboundp 'spam-group-spam-contents-p)
2846                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2847     ;;
2848     (gnus-summary-exit "exit")
2849     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2850     (gnus-info-find-node "help"))
2851   "List of functions for the summary tool bar (GNOME style).
2852
2853 See `gmm-tool-bar-from-list' for the format of the list."
2854   :type '(repeat gmm-tool-bar-item)
2855   :version "23.1" ;; No Gnus
2856   :initialize 'custom-initialize-default
2857   :set 'gnus-summary-tool-bar-update
2858   :group 'gnus-summary)
2859
2860 (defcustom gnus-summary-tool-bar-retro
2861   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2862     (gnus-summary-next-unread-article "gnus/next-ur")
2863     (gnus-summary-post-news "gnus/post")
2864     (gnus-summary-followup-with-original "gnus/fuwo")
2865     (gnus-summary-followup "gnus/followup")
2866     (gnus-summary-reply-with-original "gnus/reply-wo")
2867     (gnus-summary-reply "gnus/reply")
2868     (gnus-summary-caesar-message "gnus/rot13")
2869     (gnus-uu-decode-uu "gnus/uu-decode")
2870     (gnus-summary-save-article-file "gnus/save-aif")
2871     (gnus-summary-save-article "gnus/save-art")
2872     (gnus-uu-post-news "gnus/uu-post")
2873     (gnus-summary-catchup "gnus/catchup")
2874     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2875     (gnus-summary-exit "gnus/exit-summ")
2876     ;; Some new command that may need more suitable icons:
2877     (gnus-summary-print-article "gnus/print" nil :visible nil)
2878     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2879     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2880     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2881     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2882     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2883     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2884     ;;
2885     (gnus-info-find-node "gnus/help" nil :visible nil))
2886   "List of functions for the summary tool bar (retro look).
2887
2888 See `gmm-tool-bar-from-list' for the format of the list."
2889   :type '(repeat gmm-tool-bar-item)
2890   :version "23.1" ;; No Gnus
2891   :initialize 'custom-initialize-default
2892   :set 'gnus-summary-tool-bar-update
2893   :group 'gnus-summary)
2894
2895 (defcustom gnus-summary-tool-bar-zap-list t
2896   "List of icon items from the global tool bar.
2897 These items are not displayed in the Gnus summary mode tool bar.
2898
2899 See `gmm-tool-bar-from-list' for the format of the list."
2900   :type 'gmm-tool-bar-zap-list
2901   :version "23.1" ;; No Gnus
2902   :initialize 'custom-initialize-default
2903   :set 'gnus-summary-tool-bar-update
2904   :group 'gnus-summary)
2905
2906 (defvar image-load-path)
2907 (defvar tool-bar-map)
2908
2909 (defun gnus-summary-make-tool-bar (&optional force)
2910   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2911 When FORCE, rebuild the tool bar."
2912   (when (and (not (featurep 'xemacs))
2913              (boundp 'tool-bar-mode)
2914              tool-bar-mode
2915              (or (not gnus-summary-tool-bar-map) force))
2916     (let* ((load-path
2917             (gmm-image-load-path-for-library "gnus"
2918                                              "mail/save.xpm"
2919                                              nil t))
2920            (image-load-path (cons (car load-path)
2921                                   (when (boundp 'image-load-path)
2922                                     image-load-path)))
2923            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2924                                         gnus-summary-tool-bar-zap-list
2925                                         'gnus-summary-mode-map)))
2926       (when map
2927         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2928         ;; uses it's value.
2929         (setq gnus-summary-tool-bar-map map))))
2930   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2931
2932 (defun gnus-score-set-default (var value)
2933   "A version of set that updates the GNU Emacs menu-bar."
2934   (set var value)
2935   ;; It is the message that forces the active status to be updated.
2936   (message ""))
2937
2938 (defun gnus-make-score-map (type)
2939   "Make a summary score map of type TYPE."
2940   (if t
2941       nil
2942     (let ((headers '(("author" "from" string)
2943                      ("subject" "subject" string)
2944                      ("article body" "body" string)
2945                      ("article head" "head" string)
2946                      ("xref" "xref" string)
2947                      ("extra header" "extra" string)
2948                      ("lines" "lines" number)
2949                      ("followups to author" "followup" string)))
2950           (types '((number ("less than" <)
2951                            ("greater than" >)
2952                            ("equal" =))
2953                    (string ("substring" s)
2954                            ("exact string" e)
2955                            ("fuzzy string" f)
2956                            ("regexp" r))))
2957           (perms '(("temporary" (current-time-string))
2958                    ("permanent" nil)
2959                    ("immediate" now)))
2960           header)
2961       (list
2962        (apply
2963         'nconc
2964         (list
2965          (if (eq type 'lower)
2966              "Lower score"
2967            "Increase score"))
2968         (let (outh)
2969           (while headers
2970             (setq header (car headers))
2971             (setq outh
2972                   (cons
2973                    (apply
2974                     'nconc
2975                     (list (car header))
2976                     (let ((ts (cdr (assoc (nth 2 header) types)))
2977                           outt)
2978                       (while ts
2979                         (setq outt
2980                               (cons
2981                                (apply
2982                                 'nconc
2983                                 (list (caar ts))
2984                                 (let ((ps perms)
2985                                       outp)
2986                                   (while ps
2987                                     (setq outp
2988                                           (cons
2989                                            (vector
2990                                             (caar ps)
2991                                             (list
2992                                              'gnus-summary-score-entry
2993                                              (nth 1 header)
2994                                              (if (or (string= (nth 1 header)
2995                                                               "head")
2996                                                      (string= (nth 1 header)
2997                                                               "body"))
2998                                                  ""
2999                                                (list 'gnus-summary-header
3000                                                      (nth 1 header)))
3001                                              (list 'quote (nth 1 (car ts)))
3002                                              (list 'gnus-score-delta-default
3003                                                    nil)
3004                                              (nth 1 (car ps))
3005                                              t)
3006                                             t)
3007                                            outp))
3008                                     (setq ps (cdr ps)))
3009                                   (list (nreverse outp))))
3010                                outt))
3011                         (setq ts (cdr ts)))
3012                       (list (nreverse outt))))
3013                    outh))
3014             (setq headers (cdr headers)))
3015           (list (nreverse outh))))))))
3016
3017
3018 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3019
3020 \f
3021
3022 (defun gnus-summary-mode (&optional group)
3023   "Major mode for reading articles.
3024
3025 All normal editing commands are switched off.
3026 \\<gnus-summary-mode-map>
3027 Each line in this buffer represents one article.  To read an
3028 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3029 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3030 respectively.
3031
3032 You can also post articles and send mail from this buffer.  To
3033 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3034 of an article, type `\\[gnus-summary-reply]'.
3035
3036 There are approx. one gazillion commands you can execute in this
3037 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3038
3039 The following commands are available:
3040
3041 \\{gnus-summary-mode-map}"
3042   (interactive)
3043   (kill-all-local-variables)
3044   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3045     (gnus-summary-make-local-variables))
3046   (gnus-summary-make-local-variables)
3047   (setq gnus-newsgroup-name group)
3048   (when (gnus-visual-p 'summary-menu 'menu)
3049     (gnus-summary-make-menu-bar)
3050     (gnus-summary-make-tool-bar))
3051   (gnus-make-thread-indent-array)
3052   (gnus-simplify-mode-line)
3053   (setq major-mode 'gnus-summary-mode)
3054   (setq mode-name "Summary")
3055   (make-local-variable 'minor-mode-alist)
3056   (use-local-map gnus-summary-mode-map)
3057   (buffer-disable-undo)
3058   (setq buffer-read-only t              ;Disable modification
3059         show-trailing-whitespace nil)
3060   (setq truncate-lines t)
3061   (setq selective-display t)
3062   (setq selective-display-ellipses t)   ;Display `...'
3063   (gnus-summary-set-display-table)
3064   (gnus-set-default-directory)
3065   (make-local-variable 'gnus-summary-line-format)
3066   (make-local-variable 'gnus-summary-line-format-spec)
3067   (make-local-variable 'gnus-summary-dummy-line-format)
3068   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3069   (make-local-variable 'gnus-summary-mark-positions)
3070   (gnus-make-local-hook 'pre-command-hook)
3071   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3072   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3073   (turn-on-gnus-mailing-list-mode)
3074   (mm-enable-multibyte)
3075   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3076   (gnus-update-summary-mark-positions))
3077
3078 (defun gnus-summary-make-local-variables ()
3079   "Make all the local summary buffer variables."
3080   (let (global)
3081     (dolist (local gnus-summary-local-variables)
3082       (if (consp local)
3083           (progn
3084             (if (eq (cdr local) 'global)
3085                 ;; Copy the global value of the variable.
3086                 (setq global (symbol-value (car local)))
3087               ;; Use the value from the list.
3088               (setq global (eval (cdr local))))
3089             (set (make-local-variable (car local)) global))
3090         ;; Simple nil-valued local variable.
3091         (set (make-local-variable local) nil)))))
3092
3093 (defun gnus-summary-clear-local-variables ()
3094   (let ((locals gnus-summary-local-variables))
3095     (while locals
3096       (if (consp (car locals))
3097           (and (symbolp (caar locals))
3098                (set (caar locals) nil))
3099         (and (symbolp (car locals))
3100              (set (car locals) nil)))
3101       (setq locals (cdr locals)))))
3102
3103 ;; Summary data functions.
3104
3105 (defmacro gnus-data-number (data)
3106   `(car ,data))
3107
3108 (defmacro gnus-data-set-number (data number)
3109   `(setcar ,data ,number))
3110
3111 (defmacro gnus-data-mark (data)
3112   `(nth 1 ,data))
3113
3114 (defmacro gnus-data-set-mark (data mark)
3115   `(setcar (nthcdr 1 ,data) ,mark))
3116
3117 (defmacro gnus-data-pos (data)
3118   `(nth 2 ,data))
3119
3120 (defmacro gnus-data-set-pos (data pos)
3121   `(setcar (nthcdr 2 ,data) ,pos))
3122
3123 (defmacro gnus-data-header (data)
3124   `(nth 3 ,data))
3125
3126 (defmacro gnus-data-set-header (data header)
3127   `(setf (nth 3 ,data) ,header))
3128
3129 (defmacro gnus-data-level (data)
3130   `(nth 4 ,data))
3131
3132 (defmacro gnus-data-unread-p (data)
3133   `(= (nth 1 ,data) gnus-unread-mark))
3134
3135 (defmacro gnus-data-read-p (data)
3136   `(/= (nth 1 ,data) gnus-unread-mark))
3137
3138 (defmacro gnus-data-pseudo-p (data)
3139   `(consp (nth 3 ,data)))
3140
3141 (defmacro gnus-data-find (number)
3142   `(assq ,number gnus-newsgroup-data))
3143
3144 (defmacro gnus-data-find-list (number &optional data)
3145   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3146      (memq (assq ,number bdata)
3147            bdata)))
3148
3149 (defmacro gnus-data-make (number mark pos header level)
3150   `(list ,number ,mark ,pos ,header ,level))
3151
3152 (defun gnus-data-enter (after-article number mark pos header level offset)
3153   (let ((data (gnus-data-find-list after-article)))
3154     (unless data
3155       (error "No such article: %d" after-article))
3156     (setcdr data (cons (gnus-data-make number mark pos header level)
3157                        (cdr data)))
3158     (setq gnus-newsgroup-data-reverse nil)
3159     (gnus-data-update-list (cddr data) offset)))
3160
3161 (defun gnus-data-enter-list (after-article list &optional offset)
3162   (when list
3163     (let ((data (and after-article (gnus-data-find-list after-article)))
3164           (ilist list))
3165       (if (not (or data
3166                    after-article))
3167           (let ((odata gnus-newsgroup-data))
3168             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3169             (when offset
3170               (gnus-data-update-list odata offset)))
3171         ;; Find the last element in the list to be spliced into the main
3172         ;; list.
3173         (setq list (last list))
3174         (if (not data)
3175             (progn
3176               (setcdr list gnus-newsgroup-data)
3177               (setq gnus-newsgroup-data ilist)
3178               (when offset
3179                 (gnus-data-update-list (cdr list) offset)))
3180           (setcdr list (cdr data))
3181           (setcdr data ilist)
3182           (when offset
3183             (gnus-data-update-list (cdr list) offset))))
3184       (setq gnus-newsgroup-data-reverse nil))))
3185
3186 (defun gnus-data-remove (article &optional offset)
3187   (let ((data gnus-newsgroup-data))
3188     (if (= (gnus-data-number (car data)) article)
3189         (progn
3190           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3191                 gnus-newsgroup-data-reverse nil)
3192           (when offset
3193             (gnus-data-update-list gnus-newsgroup-data offset)))
3194       (while (cdr data)
3195         (when (= (gnus-data-number (cadr data)) article)
3196           (setcdr data (cddr data))
3197           (when offset
3198             (gnus-data-update-list (cdr data) offset))
3199           (setq data nil
3200                 gnus-newsgroup-data-reverse nil))
3201         (setq data (cdr data))))))
3202
3203 (defmacro gnus-data-list (backward)
3204   `(if ,backward
3205        (or gnus-newsgroup-data-reverse
3206            (setq gnus-newsgroup-data-reverse
3207                  (reverse gnus-newsgroup-data)))
3208      gnus-newsgroup-data))
3209
3210 (defun gnus-data-update-list (data offset)
3211   "Add OFFSET to the POS of all data entries in DATA."
3212   (setq gnus-newsgroup-data-reverse nil)
3213   (while data
3214     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3215     (setq data (cdr data))))
3216
3217 (defun gnus-summary-article-pseudo-p (article)
3218   "Say whether this article is a pseudo article or not."
3219   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3220
3221 (defmacro gnus-summary-article-sparse-p (article)
3222   "Say whether this article is a sparse article or not."
3223   `(memq ,article gnus-newsgroup-sparse))
3224
3225 (defmacro gnus-summary-article-ancient-p (article)
3226   "Say whether this article is a sparse article or not."
3227   `(memq ,article gnus-newsgroup-ancient))
3228
3229 (defun gnus-article-parent-p (number)
3230   "Say whether this article is a parent or not."
3231   (let ((data (gnus-data-find-list number)))
3232     (and (cdr data)              ; There has to be an article after...
3233          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3234             (gnus-data-level (nth 1 data))))))
3235
3236 (defun gnus-article-children (number)
3237   "Return a list of all children to NUMBER."
3238   (let* ((data (gnus-data-find-list number))
3239          (level (gnus-data-level (car data)))
3240          children)
3241     (setq data (cdr data))
3242     (while (and data
3243                 (= (gnus-data-level (car data)) (1+ level)))
3244       (push (gnus-data-number (car data)) children)
3245       (setq data (cdr data)))
3246     children))
3247
3248 (defmacro gnus-summary-skip-intangible ()
3249   "If the current article is intangible, then jump to a different article."
3250   '(let ((to (get-text-property (point) 'gnus-intangible)))
3251      (and to (gnus-summary-goto-subject to))))
3252
3253 (defmacro gnus-summary-article-intangible-p ()
3254   "Say whether this article is intangible or not."
3255   '(get-text-property (point) 'gnus-intangible))
3256
3257 (defun gnus-article-read-p (article)
3258   "Say whether ARTICLE is read or not."
3259   (not (or (memq article gnus-newsgroup-marked)
3260            (memq article gnus-newsgroup-spam-marked)
3261            (memq article gnus-newsgroup-unreads)
3262            (memq article gnus-newsgroup-unselected)
3263            (memq article gnus-newsgroup-dormant))))
3264
3265 ;; Some summary mode macros.
3266
3267 (defmacro gnus-summary-article-number ()
3268   "The article number of the article on the current line.
3269 If there isn't an article number here, then we return the current
3270 article number."
3271   '(progn
3272      (gnus-summary-skip-intangible)
3273      (or (get-text-property (point) 'gnus-number)
3274          (gnus-summary-last-subject))))
3275
3276 (defmacro gnus-summary-article-header (&optional number)
3277   "Return the header of article NUMBER."
3278   `(gnus-data-header (gnus-data-find
3279                       ,(or number '(gnus-summary-article-number)))))
3280
3281 (defmacro gnus-summary-thread-level (&optional number)
3282   "Return the level of thread that starts with article NUMBER."
3283   `(if (and (eq gnus-summary-make-false-root 'dummy)
3284             (get-text-property (point) 'gnus-intangible))
3285        0
3286      (gnus-data-level (gnus-data-find
3287                        ,(or number '(gnus-summary-article-number))))))
3288
3289 (defmacro gnus-summary-article-mark (&optional number)
3290   "Return the mark of article NUMBER."
3291   `(gnus-data-mark (gnus-data-find
3292                     ,(or number '(gnus-summary-article-number)))))
3293
3294 (defmacro gnus-summary-article-pos (&optional number)
3295   "Return the position of the line of article NUMBER."
3296   `(gnus-data-pos (gnus-data-find
3297                    ,(or number '(gnus-summary-article-number)))))
3298
3299 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3300 (defmacro gnus-summary-article-subject (&optional number)
3301   "Return current subject string or nil if nothing."
3302   `(let ((headers
3303           ,(if number
3304                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3305              '(gnus-data-header (assq (gnus-summary-article-number)
3306                                       gnus-newsgroup-data)))))
3307      (and headers
3308           (vectorp headers)
3309           (mail-header-subject headers))))
3310
3311 (defmacro gnus-summary-article-score (&optional number)
3312   "Return current article score."
3313   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3314                   gnus-newsgroup-scored))
3315        gnus-summary-default-score 0))
3316
3317 (defun gnus-summary-article-children (&optional number)
3318   "Return a list of article numbers that are children of article NUMBER."
3319   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3320          (level (gnus-data-level (car data)))
3321          l children)
3322     (while (and (setq data (cdr data))
3323                 (> (setq l (gnus-data-level (car data))) level))
3324       (and (= (1+ level) l)
3325            (push (gnus-data-number (car data))
3326                  children)))
3327     (nreverse children)))
3328
3329 (defun gnus-summary-article-parent (&optional number)
3330   "Return the article number of the parent of article NUMBER."
3331   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3332                                     (gnus-data-list t)))
3333          (level (gnus-data-level (car data))))
3334     (if (zerop level)
3335         ()                              ; This is a root.
3336       ;; We search until we find an article with a level less than
3337       ;; this one.  That function has to be the parent.
3338       (while (and (setq data (cdr data))
3339                   (not (< (gnus-data-level (car data)) level))))
3340       (and data (gnus-data-number (car data))))))
3341
3342 (defun gnus-unread-mark-p (mark)
3343   "Say whether MARK is the unread mark."
3344   (= mark gnus-unread-mark))
3345
3346 (defun gnus-read-mark-p (mark)
3347   "Say whether MARK is one of the marks that mark as read.
3348 This is all marks except unread, ticked, dormant, and expirable."
3349   (not (or (= mark gnus-unread-mark)
3350            (= mark gnus-ticked-mark)
3351            (= mark gnus-spam-mark)
3352            (= mark gnus-dormant-mark)
3353            (= mark gnus-expirable-mark))))
3354
3355 (defmacro gnus-article-mark (number)
3356   "Return the MARK of article NUMBER.
3357 This macro should only be used when computing the mark the \"first\"
3358 time; i.e., when generating the summary lines.  After that,
3359 `gnus-summary-article-mark' should be used to examine the
3360 marks of articles."
3361   `(cond
3362     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3363     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3364     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3365     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3366     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3367     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3368     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3369     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3370            gnus-ancient-mark))))
3371
3372 ;; Saving hidden threads.
3373
3374 (defmacro gnus-save-hidden-threads (&rest forms)
3375   "Save hidden threads, eval FORMS, and restore the hidden threads."
3376   (let ((config (make-symbol "config")))
3377     `(let ((,config (gnus-hidden-threads-configuration)))
3378        (unwind-protect
3379            (save-excursion
3380              ,@forms)
3381          (gnus-restore-hidden-threads-configuration ,config)))))
3382 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3383 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3384
3385 (defun gnus-data-compute-positions ()
3386   "Compute the positions of all articles."
3387   (setq gnus-newsgroup-data-reverse nil)
3388   (let ((data gnus-newsgroup-data))
3389     (save-excursion
3390       (gnus-save-hidden-threads
3391         (gnus-summary-show-all-threads)
3392         (goto-char (point-min))
3393         (while data
3394           (while (get-text-property (point) 'gnus-intangible)
3395             (forward-line 1))
3396           (gnus-data-set-pos (car data) (+ (point) 3))
3397           (setq data (cdr data))
3398           (forward-line 1))))))
3399
3400 (defun gnus-hidden-threads-configuration ()
3401   "Return the current hidden threads configuration."
3402   (save-excursion
3403     (let (config)
3404       (goto-char (point-min))
3405       (while (search-forward "\r" nil t)
3406         (push (1- (point)) config))
3407       config)))
3408
3409 (defun gnus-restore-hidden-threads-configuration (config)
3410   "Restore hidden threads configuration from CONFIG."
3411   (save-excursion
3412     (let (point (inhibit-read-only t))
3413       (while (setq point (pop config))
3414         (when (and (< point (point-max))
3415                    (goto-char point)
3416                    (eq (char-after) ?\n))
3417           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3418
3419 ;; Various summary mode internalish functions.
3420
3421 (defun gnus-mouse-pick-article (e)
3422   (interactive "e")
3423   (mouse-set-point e)
3424   (gnus-summary-next-page nil t))
3425
3426 (defun gnus-summary-set-display-table ()
3427   "Change the display table.
3428 Odd characters have a tendency to mess
3429 up nicely formatted displays - we make all possible glyphs
3430 display only a single character."
3431
3432   ;; We start from the standard display table, if any.
3433   (let ((table (or (copy-sequence standard-display-table)
3434                    (make-display-table)))
3435         (i 32))
3436     ;; Nix out all the control chars...
3437     (while (>= (setq i (1- i)) 0)
3438       (gnus-put-display-table i [??] table))
3439    ;; ... but not newline and cr, of course.  (cr is necessary for the
3440     ;; selective display).
3441     (gnus-put-display-table ?\n nil table)
3442     (gnus-put-display-table ?\r nil table)
3443     ;; We keep TAB as well.
3444     (gnus-put-display-table ?\t nil table)
3445     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3446     ;; Emacs 23 (unicode), that are not set already.
3447     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3448                  160
3449                256)))
3450       (while (>= (setq i (1- i)) 127)
3451         ;; Only modify if the entry is nil.
3452         (unless (gnus-get-display-table i table)
3453           (gnus-put-display-table i [??] table))))
3454     (setq buffer-display-table table)))
3455
3456 (defun gnus-summary-set-article-display-arrow (pos)
3457   "Update the overlay arrow to point to line at position POS."
3458   (when (and gnus-summary-display-arrow
3459              (boundp 'overlay-arrow-position)
3460              (boundp 'overlay-arrow-string))
3461     (save-excursion
3462       (goto-char pos)
3463       (beginning-of-line)
3464       (unless overlay-arrow-position
3465         (setq overlay-arrow-position (make-marker)))
3466       (setq overlay-arrow-string "=>"
3467             overlay-arrow-position (set-marker overlay-arrow-position
3468                                                (point)
3469                                                (current-buffer))))))
3470
3471 (defun gnus-summary-setup-buffer (group)
3472   "Initialize summary buffer."
3473   (let ((buffer (gnus-summary-buffer-name group))
3474         (dead-name (concat "*Dead Summary "
3475                            (gnus-group-decoded-name group) "*")))
3476     ;; If a dead summary buffer exists, we kill it.
3477     (when (gnus-buffer-live-p dead-name)
3478       (gnus-kill-buffer dead-name))
3479     (if (get-buffer buffer)
3480         (progn
3481           (set-buffer buffer)
3482           (setq gnus-summary-buffer (current-buffer))
3483           (not gnus-newsgroup-prepared))
3484       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3485       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3486       (gnus-summary-mode group)
3487       (when gnus-carpal
3488         (gnus-carpal-setup-buffer 'summary))
3489       (when (gnus-group-quit-config group)
3490         (set (make-local-variable 'gnus-single-article-buffer) nil))
3491       (make-local-variable 'gnus-article-buffer)
3492       (make-local-variable 'gnus-article-current)
3493       (make-local-variable 'gnus-original-article-buffer)
3494       (setq gnus-newsgroup-name group)
3495       ;; Set any local variables in the group parameters.
3496       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3497       t)))
3498
3499 (defun gnus-set-global-variables ()
3500   "Set the global equivalents of the buffer-local variables.
3501 They are set to the latest values they had.  These reflect the summary
3502 buffer that was in action when the last article was fetched."
3503   (when (eq major-mode 'gnus-summary-mode)
3504     (setq gnus-summary-buffer (current-buffer))
3505     (let ((name gnus-newsgroup-name)
3506           (marked gnus-newsgroup-marked)
3507           (spam gnus-newsgroup-spam-marked)
3508           (unread gnus-newsgroup-unreads)
3509           (headers gnus-current-headers)
3510           (data gnus-newsgroup-data)
3511           (summary gnus-summary-buffer)
3512           (article-buffer gnus-article-buffer)
3513           (original gnus-original-article-buffer)
3514           (gac gnus-article-current)
3515           (reffed gnus-reffed-article-number)
3516           (score-file gnus-current-score-file)
3517           (default-charset gnus-newsgroup-charset)
3518           vlist)
3519       (let ((locals gnus-newsgroup-variables))
3520         (while locals
3521           (if (consp (car locals))
3522               (push (eval (caar locals)) vlist)
3523             (push (eval (car locals)) vlist))
3524           (setq locals (cdr locals)))
3525         (setq vlist (nreverse vlist)))
3526       (with-current-buffer gnus-group-buffer
3527         (setq gnus-newsgroup-name name
3528               gnus-newsgroup-marked marked
3529               gnus-newsgroup-spam-marked spam
3530               gnus-newsgroup-unreads unread
3531               gnus-current-headers headers
3532               gnus-newsgroup-data data
3533               gnus-article-current gac
3534               gnus-summary-buffer summary
3535               gnus-article-buffer article-buffer
3536               gnus-original-article-buffer original
3537               gnus-reffed-article-number reffed
3538               gnus-current-score-file score-file
3539               gnus-newsgroup-charset default-charset)
3540         (let ((locals gnus-newsgroup-variables))
3541           (while locals
3542             (if (consp (car locals))
3543                 (set (caar locals) (pop vlist))
3544               (set (car locals) (pop vlist)))
3545             (setq locals (cdr locals))))
3546         ;; The article buffer also has local variables.
3547         (when (gnus-buffer-live-p gnus-article-buffer)
3548           (set-buffer gnus-article-buffer)
3549           (setq gnus-summary-buffer summary))))))
3550
3551 (defun gnus-summary-article-unread-p (article)
3552   "Say whether ARTICLE is unread or not."
3553   (memq article gnus-newsgroup-unreads))
3554
3555 (defun gnus-summary-first-article-p (&optional article)
3556   "Return whether ARTICLE is the first article in the buffer."
3557   (if (not (setq article (or article (gnus-summary-article-number))))
3558       nil
3559     (eq article (caar gnus-newsgroup-data))))
3560
3561 (defun gnus-summary-last-article-p (&optional article)
3562   "Return whether ARTICLE is the last article in the buffer."
3563   (if (not (setq article (or article (gnus-summary-article-number))))
3564       ;; All non-existent numbers are the last article.  :-)
3565       t
3566     (not (cdr (gnus-data-find-list article)))))
3567
3568 (defun gnus-make-thread-indent-array (&optional n)
3569   (when (or n
3570             (progn (setq n 200) nil)
3571             (null gnus-thread-indent-array)
3572             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3573     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3574           gnus-thread-indent-array-level gnus-thread-indent-level)
3575     (while (>= n 0)
3576       (aset gnus-thread-indent-array n
3577             (make-string (* n gnus-thread-indent-level) ? ))
3578       (setq n (1- n)))))
3579
3580 (defun gnus-update-summary-mark-positions ()
3581   "Compute where the summary marks are to go."
3582   (save-excursion
3583     (when (gnus-buffer-exists-p gnus-summary-buffer)
3584       (set-buffer gnus-summary-buffer))
3585     (let ((spec gnus-summary-line-format-spec)
3586           pos)
3587       (save-excursion
3588         (gnus-set-work-buffer)
3589         (let ((gnus-tmp-unread ?Z)
3590               (gnus-replied-mark ?Z)
3591               (gnus-score-below-mark ?Z)
3592               (gnus-score-over-mark ?Z)
3593               (gnus-undownloaded-mark ?Z)
3594               (gnus-summary-line-format-spec spec)
3595               (gnus-newsgroup-downloadable '(0))
3596               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3597               case-fold-search ignores)
3598           ;; Here, all marks are bound to Z.
3599           (gnus-summary-insert-line header
3600                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3601           (goto-char (point-min))
3602           ;; Memorize the positions of the same characters as dummy marks.
3603           (while (re-search-forward "[A-D]" nil t)
3604             (push (point) ignores))
3605           (erase-buffer)
3606           ;; We use A-D as dummy marks in order to know column positions
3607           ;; where marks should be inserted.
3608           (setq gnus-tmp-unread ?A
3609                 gnus-replied-mark ?B
3610                 gnus-score-below-mark ?C
3611                 gnus-score-over-mark ?C
3612                 gnus-undownloaded-mark ?D)
3613           (gnus-summary-insert-line header
3614                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3615           ;; Ignore characters which aren't dummy marks.
3616           (dolist (p ignores)
3617             (delete-region (goto-char (1- p)) p)
3618             (insert ?Z))
3619           (goto-char (point-min))
3620           (setq pos (list (cons 'unread
3621                                 (and (search-forward "A" nil t)
3622                                      (- (point) (point-min) 1)))))
3623           (goto-char (point-min))
3624           (push (cons 'replied (and (search-forward "B" nil t)
3625                                     (- (point) (point-min) 1)))
3626                 pos)
3627           (goto-char (point-min))
3628           (push (cons 'score (and (search-forward "C" nil t)
3629                                   (- (point) (point-min) 1)))
3630                 pos)
3631           (goto-char (point-min))
3632           (push (cons 'download (and (search-forward "D" nil t)
3633                                      (- (point) (point-min) 1)))
3634                 pos)))
3635       (setq gnus-summary-mark-positions pos))))
3636
3637 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3638   "Insert a dummy root in the summary buffer."
3639   (beginning-of-line)
3640   (gnus-add-text-properties
3641    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3642    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3643
3644 (defun gnus-summary-extract-address-component (from)
3645   (or (car (funcall gnus-extract-address-components from))
3646       from))
3647
3648 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3649   (let ((mail-parse-charset gnus-newsgroup-charset)
3650         (ignored-from-addresses (gnus-ignored-from-addresses))
3651         ; Is it really necessary to do this next part for each summary line?
3652         ; Luckily, doesn't seem to slow things down much.
3653         (mail-parse-ignored-charsets
3654          (with-current-buffer gnus-summary-buffer
3655            gnus-newsgroup-ignored-charsets)))
3656     (or
3657      (and ignored-from-addresses
3658           (string-match ignored-from-addresses gnus-tmp-from)
3659           (let ((extra-headers (mail-header-extra header))
3660                 to
3661                 newsgroups)
3662             (cond
3663              ((setq to (cdr (assq 'To extra-headers)))
3664               (concat gnus-summary-to-prefix
3665                       (inline
3666                         (gnus-summary-extract-address-component
3667                          (funcall gnus-decode-encoded-address-function to)))))
3668              ((setq newsgroups
3669                     (or
3670                      (cdr (assq 'Newsgroups extra-headers))
3671                      (and
3672                       (memq 'Newsgroups gnus-extra-headers)
3673                       (eq (car (gnus-find-method-for-group
3674                                 gnus-newsgroup-name)) 'nntp)
3675                       (gnus-group-real-name gnus-newsgroup-name))))
3676               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3677      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3678
3679 (defun gnus-summary-insert-line (gnus-tmp-header
3680                                  gnus-tmp-level gnus-tmp-current
3681                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3682                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3683                                  &optional gnus-tmp-dummy gnus-tmp-score
3684                                  gnus-tmp-process)
3685   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3686       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3687                                           gnus-tmp-level)))
3688   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3689          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3690          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3691          (gnus-tmp-score-char
3692           (if (or (null gnus-summary-default-score)
3693                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3694                       gnus-summary-zcore-fuzz))
3695               ?                         ;Whitespace
3696             (if (< gnus-tmp-score gnus-summary-default-score)
3697                 gnus-score-below-mark gnus-score-over-mark)))
3698          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3699          (gnus-tmp-replied
3700           (cond (gnus-tmp-process gnus-process-mark)
3701                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3702                  gnus-cached-mark)
3703                 (gnus-tmp-replied gnus-replied-mark)
3704                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3705                  gnus-forwarded-mark)
3706                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3707                  gnus-saved-mark)
3708                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3709                  gnus-recent-mark)
3710                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3711                  gnus-unseen-mark)
3712                 (t gnus-no-mark)))
3713          (gnus-tmp-downloaded
3714           (cond (undownloaded
3715                  gnus-undownloaded-mark)
3716                 (gnus-newsgroup-agentized
3717                  gnus-downloaded-mark)
3718                 (t
3719                  gnus-no-mark)))
3720          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3721          (gnus-tmp-name
3722           (cond
3723            ((string-match "<[^>]+> *$" gnus-tmp-from)
3724             (let ((beg (match-beginning 0)))
3725               (or (and (string-match "^\".+\"" gnus-tmp-from)
3726                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3727                   (substring gnus-tmp-from 0 beg))))
3728            ((string-match "(.+)" gnus-tmp-from)
3729             (substring gnus-tmp-from
3730                        (1+ (match-beginning 0)) (1- (match-end 0))))
3731            (t gnus-tmp-from)))
3732          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3733          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3734          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3735          (inhibit-read-only t))
3736     (when (string= gnus-tmp-name "")
3737       (setq gnus-tmp-name gnus-tmp-from))
3738     (unless (numberp gnus-tmp-lines)
3739       (setq gnus-tmp-lines -1))
3740     (if (= gnus-tmp-lines -1)
3741         (setq gnus-tmp-lines "?")
3742       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3743       (gnus-put-text-property
3744      (point)
3745      (progn (eval gnus-summary-line-format-spec) (point))
3746        'gnus-number gnus-tmp-number)
3747     (when (gnus-visual-p 'summary-highlight 'highlight)
3748       (forward-line -1)
3749       (gnus-run-hooks 'gnus-summary-update-hook)
3750       (forward-line 1))))
3751
3752 (defun gnus-summary-update-line (&optional dont-update)
3753   "Update summary line after change."
3754   (when (and gnus-summary-default-score
3755              (not gnus-summary-inhibit-highlight))
3756     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3757            (article (gnus-summary-article-number))
3758            (score (gnus-summary-article-score article)))
3759       (unless dont-update
3760         (if (and gnus-summary-mark-below
3761                  (< (gnus-summary-article-score)
3762                     gnus-summary-mark-below))
3763             ;; This article has a low score, so we mark it as read.
3764             (when (memq article gnus-newsgroup-unreads)
3765               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3766           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3767             ;; This article was previously marked as read on account
3768             ;; of a low score, but now it has risen, so we mark it as
3769             ;; unread.
3770             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3771         (gnus-summary-update-mark
3772          (if (or (null gnus-summary-default-score)
3773                  (<= (abs (- score gnus-summary-default-score))
3774                      gnus-summary-zcore-fuzz))
3775              ?                          ;Whitespace
3776            (if (< score gnus-summary-default-score)
3777                gnus-score-below-mark gnus-score-over-mark))
3778          'score))
3779       ;; Do visual highlighting.
3780       (when (gnus-visual-p 'summary-highlight 'highlight)
3781         (gnus-run-hooks 'gnus-summary-update-hook)))))
3782
3783 (defvar gnus-tmp-new-adopts nil)
3784
3785 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3786   "Return the number of articles in THREAD.
3787 This may be 0 in some cases -- if none of the articles in
3788 the thread are to be displayed."
3789   (let* ((number
3790          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3791           (cond
3792            ((not (listp thread))
3793             1)
3794            ((and (consp thread) (cdr thread))
3795             (apply
3796              '+ 1 (mapcar
3797                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3798            ((null thread)
3799             1)
3800            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3801             1)
3802            (t 0))))
3803     (when (and level (zerop level) gnus-tmp-new-adopts)
3804       (incf number
3805             (apply '+ (mapcar
3806                        'gnus-summary-number-of-articles-in-thread
3807                        gnus-tmp-new-adopts))))
3808     (if char
3809         (if (> number 1) gnus-not-empty-thread-mark
3810           gnus-empty-thread-mark)
3811       number)))
3812
3813 (defsubst gnus-summary-line-message-size (head)
3814   "Return pretty-printed version of message size.
3815 This function is intended to be used in
3816 `gnus-summary-line-format-alist'."
3817   (let ((c (or (mail-header-chars head) -1)))
3818     (cond ((< c 0) "n/a")               ; chars not available
3819           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3820           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3821           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3822           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3823
3824
3825 (defun gnus-summary-set-local-parameters (group)
3826   "Go through the local params of GROUP and set all variable specs in that list."
3827   (let ((vars '(quit-config)))          ; Ignore quit-config.
3828     (dolist (elem (gnus-group-find-parameter group))
3829       (and (consp elem)                 ; Has to be a cons.
3830            (consp (cdr elem))           ; The cdr has to be a list.
3831            (symbolp (car elem))         ; Has to be a symbol in there.
3832            (not (memq (car elem) vars))
3833            (ignore-errors               ; So we set it.
3834              (push (car elem) vars)
3835              (make-local-variable (car elem))
3836              (set (car elem) (eval (nth 1 elem))))))))
3837
3838 (defun gnus-summary-read-group (group &optional show-all no-article
3839                                       kill-buffer no-display backward
3840                                       select-articles)
3841   "Start reading news in newsgroup GROUP.
3842 If SHOW-ALL is non-nil, already read articles are also listed.
3843 If NO-ARTICLE is non-nil, no article is selected initially.
3844 If NO-DISPLAY, don't generate a summary buffer."
3845   (let (result)
3846     (while (and group
3847                 (null (setq result
3848                             (let ((gnus-auto-select-next nil))
3849                               (or (gnus-summary-read-group-1
3850                                    group show-all no-article
3851                                    kill-buffer no-display
3852                                    select-articles)
3853                                   (setq show-all nil
3854                                         select-articles nil)))))
3855                 (eq gnus-auto-select-next 'quietly))
3856       (set-buffer gnus-group-buffer)
3857       ;; The entry function called above goes to the next
3858       ;; group automatically, so we go two groups back
3859       ;; if we are searching for the previous group.
3860       (when backward
3861         (gnus-group-prev-unread-group 2))
3862       (if (not (equal group (gnus-group-group-name)))
3863           (setq group (gnus-group-group-name))
3864         (setq group nil)))
3865     result))
3866
3867 (defun gnus-summary-read-group-1 (group show-all no-article
3868                                         kill-buffer no-display
3869                                         &optional select-articles)
3870   ;; Killed foreign groups can't be entered.
3871   ;;  (when (and (not (gnus-group-native-p group))
3872   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3873   ;;    (error "Dead non-native groups can't be entered"))
3874   (gnus-message 5 "Retrieving newsgroup: %s..."
3875                 (gnus-group-decoded-name group))
3876   (let* ((new-group (gnus-summary-setup-buffer group))
3877          (quit-config (gnus-group-quit-config group))
3878          (did-select (and new-group (gnus-select-newsgroup
3879                                      group show-all select-articles))))
3880     (cond
3881      ;; This summary buffer exists already, so we just select it.
3882      ((not new-group)
3883       (gnus-set-global-variables)
3884       (when kill-buffer
3885         (gnus-kill-or-deaden-summary kill-buffer))
3886       (gnus-configure-windows 'summary 'force)
3887       (gnus-set-mode-line 'summary)
3888       (gnus-summary-position-point)
3889       (message "")
3890       t)
3891      ;; We couldn't select this group.
3892      ((null did-select)
3893       (when (and (eq major-mode 'gnus-summary-mode)
3894                  (not (equal (current-buffer) kill-buffer)))
3895         (kill-buffer (current-buffer))
3896         (if (not quit-config)
3897             (progn
3898               ;; Update the info -- marks might need to be removed,
3899               ;; for instance.
3900               (gnus-summary-update-info)
3901               (set-buffer gnus-group-buffer)
3902               (gnus-group-jump-to-group group)
3903               (gnus-group-next-unread-group 1))
3904           (gnus-handle-ephemeral-exit quit-config)))
3905       (let ((grpinfo (gnus-get-info group)))
3906         (if (null (gnus-info-read grpinfo))
3907             (gnus-message 3 "Group %s contains no messages"
3908                           (gnus-group-decoded-name group))
3909           (gnus-message 3 "Can't select group")))
3910       nil)
3911      ;; The user did a `C-g' while prompting for number of articles,
3912      ;; so we exit this group.
3913      ((eq did-select 'quit)
3914       (and (eq major-mode 'gnus-summary-mode)
3915            (not (equal (current-buffer) kill-buffer))
3916            (kill-buffer (current-buffer)))
3917       (when kill-buffer
3918         (gnus-kill-or-deaden-summary kill-buffer))
3919       (if (not quit-config)
3920           (progn
3921             (set-buffer gnus-group-buffer)
3922             (gnus-group-jump-to-group group)
3923             (gnus-group-next-unread-group 1)
3924             (gnus-configure-windows 'group 'force))
3925         (gnus-handle-ephemeral-exit quit-config))
3926       ;; Finally signal the quit.
3927       (signal 'quit nil))
3928      ;; The group was successfully selected.
3929      (t
3930       (gnus-set-global-variables)
3931       ;; Save the active value in effect when the group was entered.
3932       (setq gnus-newsgroup-active
3933             (gnus-copy-sequence
3934              (gnus-active gnus-newsgroup-name)))
3935       ;; You can change the summary buffer in some way with this hook.
3936       (gnus-run-hooks 'gnus-select-group-hook)
3937       (when (memq 'summary (gnus-update-format-specifications
3938                             nil 'summary 'summary-mode 'summary-dummy))
3939         ;; The format specification for the summary line was updated,
3940         ;; so we need to update the mark positions as well.
3941         (gnus-update-summary-mark-positions))
3942       ;; Do score processing.
3943       (when gnus-use-scoring
3944         (gnus-possibly-score-headers))
3945       ;; Check whether to fill in the gaps in the threads.
3946       (when gnus-build-sparse-threads
3947         (gnus-build-sparse-threads))
3948       ;; Find the initial limit.
3949       (if show-all
3950           (let ((gnus-newsgroup-dormant nil))
3951             (gnus-summary-initial-limit show-all))
3952         (gnus-summary-initial-limit show-all))
3953       ;; Generate the summary buffer.
3954       (unless no-display
3955         (gnus-summary-prepare))
3956       (when gnus-use-trees
3957         (gnus-tree-open group)
3958         (setq gnus-summary-highlight-line-function
3959               'gnus-tree-highlight-article))
3960       ;; If the summary buffer is empty, but there are some low-scored
3961       ;; articles or some excluded dormants, we include these in the
3962       ;; buffer.
3963       (when (and (zerop (buffer-size))
3964                  (not no-display))
3965         (cond (gnus-newsgroup-dormant
3966                (gnus-summary-limit-include-dormant))
3967               ((and gnus-newsgroup-scored show-all)
3968                (gnus-summary-limit-include-expunged t))))
3969       ;; Function `gnus-apply-kill-file' must be called in this hook.
3970       (gnus-run-hooks 'gnus-apply-kill-hook)
3971       (if (and (zerop (buffer-size))
3972                (not no-display))
3973           (progn
3974             ;; This newsgroup is empty.
3975             (gnus-summary-catchup-and-exit nil t)
3976             (gnus-message 6 "No unread news")
3977             (when kill-buffer
3978               (gnus-kill-or-deaden-summary kill-buffer))
3979             ;; Return nil from this function.
3980             nil)
3981         ;; Hide conversation thread subtrees.  We cannot do this in
3982         ;; gnus-summary-prepare-hook since kill processing may not
3983         ;; work with hidden articles.
3984         (gnus-summary-maybe-hide-threads)
3985         (when kill-buffer
3986           (gnus-kill-or-deaden-summary kill-buffer))
3987         (gnus-summary-auto-select-subject)
3988         ;; Show first unread article if requested.
3989         (if (and (not no-article)
3990                  (not no-display)
3991                  gnus-newsgroup-unreads
3992                  gnus-auto-select-first)
3993             (progn
3994               (gnus-configure-windows 'summary)
3995               (let ((art (gnus-summary-article-number)))
3996                 (unless (and (not gnus-plugged)
3997                              (or (memq art gnus-newsgroup-undownloaded)
3998                                  (memq art gnus-newsgroup-downloadable)))
3999                   (gnus-summary-goto-article art))))
4000           ;; Don't select any articles.
4001           (gnus-summary-position-point)
4002           (gnus-configure-windows 'summary 'force)
4003           (gnus-set-mode-line 'summary))
4004         (when (and gnus-auto-center-group
4005                    (get-buffer-window gnus-group-buffer t))
4006           ;; Gotta use windows, because recenter does weird stuff if
4007           ;; the current buffer ain't the displayed window.
4008           (let ((owin (selected-window)))
4009             (select-window (get-buffer-window gnus-group-buffer t))
4010             (when (gnus-group-goto-group group)
4011               (recenter))
4012             (select-window owin)))
4013         ;; Mark this buffer as "prepared".
4014         (setq gnus-newsgroup-prepared t)
4015         (gnus-run-hooks 'gnus-summary-prepared-hook)
4016         (unless (gnus-ephemeral-group-p group)
4017           (gnus-group-update-group group))
4018         t)))))
4019
4020 (defun gnus-summary-auto-select-subject ()
4021   "Select the subject line on initial group entry."
4022   (goto-char (point-min))
4023   (cond
4024    ((eq gnus-auto-select-subject 'best)
4025     (gnus-summary-best-unread-subject))
4026    ((eq gnus-auto-select-subject 'unread)
4027     (gnus-summary-first-unread-subject))
4028    ((eq gnus-auto-select-subject 'unseen)
4029     (gnus-summary-first-unseen-subject))
4030    ((eq gnus-auto-select-subject 'unseen-or-unread)
4031     (gnus-summary-first-unseen-or-unread-subject))
4032    ((eq gnus-auto-select-subject 'first)
4033     ;; Do nothing.
4034     )
4035    ((functionp gnus-auto-select-subject)
4036     (funcall gnus-auto-select-subject))))
4037
4038 (defun gnus-summary-prepare ()
4039   "Generate the summary buffer."
4040   (interactive)
4041   (let ((inhibit-read-only t))
4042     (erase-buffer)
4043     (setq gnus-newsgroup-data nil
4044           gnus-newsgroup-data-reverse nil)
4045     (gnus-run-hooks 'gnus-summary-generate-hook)
4046     ;; Generate the buffer, either with threads or without.
4047     (when gnus-newsgroup-headers
4048       (gnus-summary-prepare-threads
4049        (if gnus-show-threads
4050            (gnus-sort-gathered-threads
4051             (funcall gnus-summary-thread-gathering-function
4052                      (gnus-sort-threads
4053                       (gnus-cut-threads (gnus-make-threads)))))
4054          ;; Unthreaded display.
4055          (gnus-sort-articles gnus-newsgroup-headers))))
4056     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4057     ;; Call hooks for modifying summary buffer.
4058     (goto-char (point-min))
4059     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4060
4061 (defsubst gnus-general-simplify-subject (subject)
4062   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4063   (setq subject
4064         (cond
4065          ;; Truncate the subject.
4066          (gnus-simplify-subject-functions
4067           (gnus-map-function gnus-simplify-subject-functions subject))
4068          ((numberp gnus-summary-gather-subject-limit)
4069           (setq subject (gnus-simplify-subject-re subject))
4070           (if (> (length subject) gnus-summary-gather-subject-limit)
4071               (substring subject 0 gnus-summary-gather-subject-limit)
4072             subject))
4073          ;; Fuzzily simplify it.
4074          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4075           (gnus-simplify-subject-fuzzy subject))
4076          ;; Just remove the leading "Re:".
4077          (t
4078           (gnus-simplify-subject-re subject))))
4079
4080   (if (and gnus-summary-gather-exclude-subject
4081            (string-match gnus-summary-gather-exclude-subject subject))
4082       nil                         ; This article shouldn't be gathered
4083     subject))
4084
4085 (defun gnus-summary-simplify-subject-query ()
4086   "Query where the respool algorithm would put this article."
4087   (interactive)
4088   (gnus-summary-select-article)
4089   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4090
4091 (defun gnus-gather-threads-by-subject (threads)
4092   "Gather threads by looking at Subject headers."
4093   (if (not gnus-summary-make-false-root)
4094       threads
4095     (let ((hashtb (gnus-make-hashtable 1024))
4096           (prev threads)
4097           (result threads)
4098           subject hthread whole-subject)
4099       (while threads
4100         (setq subject (gnus-general-simplify-subject
4101                        (setq whole-subject (mail-header-subject
4102                                             (caar threads)))))
4103         (when subject
4104           (if (setq hthread (gnus-gethash subject hashtb))
4105               (progn
4106                 ;; We enter a dummy root into the thread, if we
4107                 ;; haven't done that already.
4108                 (unless (stringp (caar hthread))
4109                   (setcar hthread (list whole-subject (car hthread))))
4110                 ;; We add this new gathered thread to this gathered
4111                 ;; thread.
4112                 (setcdr (car hthread)
4113                         (nconc (cdar hthread) (list (car threads))))
4114                 ;; Remove it from the list of threads.
4115                 (setcdr prev (cdr threads))
4116                 (setq threads prev))
4117             ;; Enter this thread into the hash table.
4118             (gnus-sethash subject
4119                           (if gnus-summary-make-false-root-always
4120                               (progn
4121                                 ;; If you want a dummy root above all
4122                                 ;; threads...
4123                                 (setcar threads (list whole-subject
4124                                                       (car threads)))
4125                                 threads)
4126                             threads)
4127                           hashtb)))
4128         (setq prev threads)
4129         (setq threads (cdr threads)))
4130       result)))
4131
4132 (defun gnus-gather-threads-by-references (threads)
4133   "Gather threads by looking at References headers."
4134   (let ((idhashtb (gnus-make-hashtable 1024))
4135         (thhashtb (gnus-make-hashtable 1024))
4136         (prev threads)
4137         (result threads)
4138         ids references id gthread gid entered ref)
4139     (while threads
4140       (when (setq references (mail-header-references (caar threads)))
4141         (setq id (mail-header-id (caar threads))
4142               ids (inline (gnus-split-references references))
4143               entered nil)
4144         (while (setq ref (pop ids))
4145           (setq ids (delete ref ids))
4146           (if (not (setq gid (gnus-gethash ref idhashtb)))
4147               (progn
4148                 (gnus-sethash ref id idhashtb)
4149                 (gnus-sethash id threads thhashtb))
4150             (setq gthread (gnus-gethash gid thhashtb))
4151             (unless entered
4152               ;; We enter a dummy root into the thread, if we
4153               ;; haven't done that already.
4154               (unless (stringp (caar gthread))
4155                 (setcar gthread (list (mail-header-subject (caar gthread))
4156                                       (car gthread))))
4157               ;; We add this new gathered thread to this gathered
4158               ;; thread.
4159               (setcdr (car gthread)
4160                       (nconc (cdar gthread) (list (car threads)))))
4161             ;; Add it into the thread hash table.
4162             (gnus-sethash id gthread thhashtb)
4163             (setq entered t)
4164             ;; Remove it from the list of threads.
4165             (setcdr prev (cdr threads))
4166             (setq threads prev))))
4167       (setq prev threads)
4168       (setq threads (cdr threads)))
4169     result))
4170
4171 (defun gnus-sort-gathered-threads (threads)
4172   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4173   (let ((result threads))
4174     (while threads
4175       (when (stringp (caar threads))
4176         (setcdr (car threads)
4177                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4178       (setq threads (cdr threads)))
4179     result))
4180
4181 (defun gnus-thread-loop-p (root thread)
4182   "Say whether ROOT is in THREAD."
4183   (let ((stack (list thread))
4184         (infloop 0)
4185         th)
4186     (while (setq thread (pop stack))
4187       (setq th (cdr thread))
4188       (while (and th
4189                   (not (eq (caar th) root)))
4190         (pop th))
4191       (if th
4192           ;; We have found a loop.
4193           (let (ref-dep)
4194             (setcdr thread (delq (car th) (cdr thread)))
4195             (if (boundp (setq ref-dep (intern "none"
4196                                               gnus-newsgroup-dependencies)))
4197                 (setcdr (symbol-value ref-dep)
4198                         (nconc (cdr (symbol-value ref-dep))
4199                                (list (car th))))
4200               (set ref-dep (list nil (car th))))
4201             (setq infloop 1
4202                   stack nil))
4203         ;; Push all the subthreads onto the stack.
4204         (push (cdr thread) stack)))
4205     infloop))
4206
4207 (defun gnus-make-threads ()
4208   "Go through the dependency hashtb and find the roots.  Return all threads."
4209   (let (threads)
4210     (while (catch 'infloop
4211              (mapatoms
4212               (lambda (refs)
4213                 ;; Deal with self-referencing References loops.
4214                 (when (and (car (symbol-value refs))
4215                            (not (zerop
4216                                  (apply
4217                                   '+
4218                                   (mapcar
4219                                    (lambda (thread)
4220                                      (gnus-thread-loop-p
4221                                       (car (symbol-value refs)) thread))
4222                                    (cdr (symbol-value refs)))))))
4223                   (setq threads nil)
4224                   (throw 'infloop t))
4225                 (unless (car (symbol-value refs))
4226                   ;; These threads do not refer back to any other
4227                   ;; articles, so they're roots.
4228                   (setq threads (append (cdr (symbol-value refs)) threads))))
4229               gnus-newsgroup-dependencies)))
4230     threads))
4231
4232 ;; Build the thread tree.
4233 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4234   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4235
4236 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4237 if it was already present.
4238
4239 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4240 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4241 Message-IDs will be renamed to a unique Message-ID before being
4242 entered.
4243
4244 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4245   (let* ((id (mail-header-id header))
4246          (id-dep (and id (intern id dependencies)))
4247          parent-id ref ref-dep ref-header replaced)
4248     ;; Enter this `header' in the `dependencies' table.
4249     (cond
4250      ((not id-dep)
4251       (setq header nil))
4252      ;; The first two cases do the normal part: enter a new `header'
4253      ;; in the `dependencies' table.
4254      ((not (boundp id-dep))
4255       (set id-dep (list header)))
4256      ((null (car (symbol-value id-dep)))
4257       (setcar (symbol-value id-dep) header))
4258
4259      ;; From here the `header' was already present in the
4260      ;; `dependencies' table.
4261      (force-new
4262       ;; Overrides an existing entry;
4263       ;; just set the header part of the entry.
4264       (setcar (symbol-value id-dep) header)
4265       (setq replaced t))
4266
4267      ;; Renames the existing `header' to a unique Message-ID.
4268      ((not gnus-summary-ignore-duplicates)
4269       ;; An article with this Message-ID has already been seen.
4270       ;; We rename the Message-ID.
4271       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4272            (list header))
4273       (mail-header-set-id header id))
4274
4275      ;; The last case ignores an existing entry, except it adds any
4276      ;; additional Xrefs (in case the two articles came from different
4277      ;; servers.
4278      ;; Also sets `header' to `nil' meaning that the `dependencies'
4279      ;; table was *not* modified.
4280      (t
4281       (mail-header-set-xref
4282        (car (symbol-value id-dep))
4283        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4284                    "")
4285                (or (mail-header-xref header) "")))
4286       (setq header nil)))
4287
4288     (when (and header (not replaced))
4289       ;; First check that we are not creating a References loop.
4290       (setq parent-id (gnus-parent-id (mail-header-references header)))
4291       (setq ref parent-id)
4292       (while (and ref
4293                   (setq ref-dep (intern-soft ref dependencies))
4294                   (boundp ref-dep)
4295                   (setq ref-header (car (symbol-value ref-dep))))
4296         (if (string= id ref)
4297             ;; Yuk!  This is a reference loop.  Make the article be a
4298             ;; root article.
4299             (progn
4300               (mail-header-set-references (car (symbol-value id-dep)) "none")
4301               (setq ref nil)
4302               (setq parent-id nil))
4303           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4304       (setq ref-dep (intern (or parent-id "none") dependencies))
4305       (if (boundp ref-dep)
4306           (setcdr (symbol-value ref-dep)
4307                   (nconc (cdr (symbol-value ref-dep))
4308                          (list (symbol-value id-dep))))
4309         (set ref-dep (list nil (symbol-value id-dep)))))
4310     header))
4311
4312 (defun gnus-extract-message-id-from-in-reply-to (string)
4313   (if (string-match "<[^>]+>" string)
4314       (substring string (match-beginning 0) (match-end 0))
4315     nil))
4316
4317 (defun gnus-build-sparse-threads ()
4318   (let ((headers gnus-newsgroup-headers)
4319         (mail-parse-charset gnus-newsgroup-charset)
4320         (gnus-summary-ignore-duplicates t)
4321         header references generation relations
4322         subject child end new-child date)
4323     ;; First we create an alist of generations/relations, where
4324     ;; generations is how much we trust the relation, and the relation
4325     ;; is parent/child.
4326     (gnus-message 7 "Making sparse threads...")
4327     (save-excursion
4328       (nnheader-set-temp-buffer " *gnus sparse threads*")
4329       (while (setq header (pop headers))
4330         (when (and (setq references (mail-header-references header))
4331                    (not (string= references "")))
4332           (insert references)
4333           (setq child (mail-header-id header)
4334                 subject (mail-header-subject header)
4335                 date (mail-header-date header)
4336                 generation 0)
4337           (while (search-backward ">" nil t)
4338             (setq end (1+ (point)))
4339             (when (search-backward "<" nil t)
4340               (setq new-child (buffer-substring (point) end))
4341               (push (list (incf generation)
4342                           child (setq child new-child)
4343                           subject date)
4344                     relations)))
4345           (when child
4346             (push (list (1+ generation) child nil subject) relations))
4347           (erase-buffer)))
4348       (kill-buffer (current-buffer)))
4349     ;; Sort over trustworthiness.
4350     (dolist (relation (sort relations 'car-less-than-car))
4351       (when (gnus-dependencies-add-header
4352              (make-full-mail-header
4353               gnus-reffed-article-number
4354               (nth 3 relation) "" (or (nth 4 relation) "")
4355               (nth 1 relation)
4356               (or (nth 2 relation) "") 0 0 "")
4357              gnus-newsgroup-dependencies nil)
4358         (push gnus-reffed-article-number gnus-newsgroup-limit)
4359         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4360         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4361               gnus-newsgroup-reads)
4362         (decf gnus-reffed-article-number)))
4363     (gnus-message 7 "Making sparse threads...done")))
4364
4365 (defun gnus-build-old-threads ()
4366   ;; Look at all the articles that refer back to old articles, and
4367   ;; fetch the headers for the articles that aren't there.  This will
4368   ;; build complete threads - if the roots haven't been expired by the
4369   ;; server, that is.
4370   (let ((mail-parse-charset gnus-newsgroup-charset)
4371         id heads)
4372     (mapatoms
4373      (lambda (refs)
4374        (when (not (car (symbol-value refs)))
4375          (setq heads (cdr (symbol-value refs)))
4376          (while heads
4377            (if (memq (mail-header-number (caar heads))
4378                      gnus-newsgroup-dormant)
4379                (setq heads (cdr heads))
4380              (setq id (symbol-name refs))
4381              (while (and (setq id (gnus-build-get-header id))
4382                          (not (car (gnus-id-to-thread id)))))
4383              (setq heads nil)))))
4384      gnus-newsgroup-dependencies)))
4385
4386 (defsubst gnus-remove-odd-characters (string)
4387   "Translate STRING into something that doesn't contain weird characters."
4388   (mm-subst-char-in-string
4389    ?\r ?\-
4390    (mm-subst-char-in-string ?\n ?\- string t) t))
4391
4392 ;; This function has to be called with point after the article number
4393 ;; on the beginning of the line.
4394 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4395   (let ((eol (point-at-eol))
4396         (buffer (current-buffer))
4397         header references in-reply-to)
4398
4399     ;; overview: [num subject from date id refs chars lines misc]
4400     (unwind-protect
4401         (let (x)
4402           (narrow-to-region (point) eol)
4403           (unless (eobp)
4404             (forward-char))
4405
4406           (setq header
4407                 (make-full-mail-header
4408                  number                 ; number
4409                  (condition-case ()     ; subject
4410                      (gnus-remove-odd-characters
4411                       (funcall gnus-decode-encoded-word-function
4412                                (setq x (nnheader-nov-field))))
4413                    (error x))
4414                  (condition-case ()     ; from
4415                      (gnus-remove-odd-characters
4416                       (funcall gnus-decode-encoded-address-function
4417                                (setq x (nnheader-nov-field))))
4418                    (error x))
4419                  (nnheader-nov-field)   ; date
4420                  (nnheader-nov-read-message-id number)  ; id
4421                  (setq references (nnheader-nov-field)) ; refs
4422                  (nnheader-nov-read-integer) ; chars
4423                  (nnheader-nov-read-integer) ; lines
4424                  (unless (eobp)
4425                    (if (looking-at "Xref: ")
4426                        (goto-char (match-end 0)))
4427                    (nnheader-nov-field)) ; Xref
4428                  (nnheader-nov-parse-extra)))) ; extra
4429
4430       (widen))
4431
4432     (when (and (string= references "")
4433                (setq in-reply-to (mail-header-extra header))
4434                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4435       (mail-header-set-references
4436        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4437
4438     (when gnus-alter-header-function
4439       (funcall gnus-alter-header-function header))
4440     (gnus-dependencies-add-header header dependencies force-new)))
4441
4442 (defun gnus-build-get-header (id)
4443   "Look through the buffer of NOV lines and find the header to ID.
4444 Enter this line into the dependencies hash table, and return
4445 the id of the parent article (if any)."
4446   (let ((deps gnus-newsgroup-dependencies)
4447         found header)
4448     (prog1
4449         (with-current-buffer nntp-server-buffer
4450           (let ((case-fold-search nil))
4451             (goto-char (point-min))
4452             (while (and (not found)
4453                         (search-forward id nil t))
4454               (beginning-of-line)
4455               (setq found (looking-at
4456                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4457                                    (regexp-quote id))))
4458               (or found (beginning-of-line 2)))
4459             (when found
4460               (beginning-of-line)
4461               (and
4462                (setq header (gnus-nov-parse-line
4463                              (read (current-buffer)) deps))
4464                (gnus-parent-id (mail-header-references header))))))
4465       (when header
4466         (let ((number (mail-header-number header)))
4467           (push number gnus-newsgroup-limit)
4468           (push header gnus-newsgroup-headers)
4469           (if (memq number gnus-newsgroup-unselected)
4470               (progn
4471                 (setq gnus-newsgroup-unreads
4472                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4473                                                number))
4474                 (setq gnus-newsgroup-unselected
4475                       (delq number gnus-newsgroup-unselected)))
4476             (push number gnus-newsgroup-ancient)))))))
4477
4478 (defun gnus-build-all-threads ()
4479   "Read all the headers."
4480   (let ((gnus-summary-ignore-duplicates t)
4481         (mail-parse-charset gnus-newsgroup-charset)
4482         (dependencies gnus-newsgroup-dependencies)
4483         header article)
4484     (with-current-buffer nntp-server-buffer
4485       (let ((case-fold-search nil))
4486         (goto-char (point-min))
4487         (while (not (eobp))
4488           (ignore-errors
4489             (setq article (read (current-buffer))
4490                   header (gnus-nov-parse-line article dependencies)))
4491           (when header
4492             (with-current-buffer gnus-summary-buffer
4493               (push header gnus-newsgroup-headers)
4494               (if (memq (setq article (mail-header-number header))
4495                         gnus-newsgroup-unselected)
4496                   (progn
4497                     (setq gnus-newsgroup-unreads
4498                           (gnus-add-to-sorted-list
4499                            gnus-newsgroup-unreads article))
4500                     (setq gnus-newsgroup-unselected
4501                           (delq article gnus-newsgroup-unselected)))
4502                 (push article gnus-newsgroup-ancient)))
4503             (forward-line 1)))))))
4504
4505 (defun gnus-summary-update-article-line (article header)
4506   "Update the line for ARTICLE using HEADER."
4507   (let* ((id (mail-header-id header))
4508          (thread (gnus-id-to-thread id)))
4509     (unless thread
4510       (error "Article in no thread"))
4511     ;; Update the thread.
4512     (setcar thread header)
4513     (gnus-summary-goto-subject article)
4514     (let* ((datal (gnus-data-find-list article))
4515            (data (car datal))
4516            (inhibit-read-only t)
4517            (level (gnus-summary-thread-level)))
4518       (gnus-delete-line)
4519       (let ((inserted (- (point)
4520                          (progn
4521                            (gnus-summary-insert-line
4522                             header level nil
4523                             (memq article gnus-newsgroup-undownloaded)
4524                             (gnus-article-mark article)
4525                             (memq article gnus-newsgroup-replied)
4526                             (memq article gnus-newsgroup-expirable)
4527                             ;; Only insert the Subject string when it's different
4528                             ;; from the previous Subject string.
4529                             (if (and
4530                                  gnus-show-threads
4531                                  (gnus-subject-equal
4532                                   (condition-case ()
4533                                       (mail-header-subject
4534                                        (gnus-data-header
4535                                         (cadr
4536                                          (gnus-data-find-list
4537                                           article
4538                                           (gnus-data-list t)))))
4539                                     ;; Error on the side of excessive subjects.
4540                                     (error ""))
4541                                   (mail-header-subject header)))
4542                                 ""
4543                               (mail-header-subject header))
4544                             nil (cdr (assq article gnus-newsgroup-scored))
4545                             (memq article gnus-newsgroup-processable))
4546                            (point)))))
4547         (when (cdr datal)
4548           (gnus-data-update-list
4549            (cdr datal)
4550            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4551
4552 (defun gnus-summary-update-article (article &optional iheader)
4553   "Update ARTICLE in the summary buffer."
4554   (set-buffer gnus-summary-buffer)
4555   (let* ((header (gnus-summary-article-header article))
4556          (id (mail-header-id header))
4557          (data (gnus-data-find article))
4558          (thread (gnus-id-to-thread id))
4559          (references (mail-header-references header))
4560          (parent
4561           (gnus-id-to-thread
4562            (or (gnus-parent-id
4563                 (when (and references
4564                            (not (equal "" references)))
4565                   references))
4566                "none")))
4567          (inhibit-read-only t)
4568          (old (car thread)))
4569     (when thread
4570       (unless iheader
4571         (setcar thread nil)
4572         (when parent
4573           (delq thread parent)))
4574       (if (gnus-summary-insert-subject id header)
4575           ;; Set the (possibly) new article number in the data structure.
4576           (gnus-data-set-number data (gnus-id-to-article id))
4577         (setcar thread old)
4578         nil))))
4579
4580 (defun gnus-rebuild-thread (id &optional line)
4581   "Rebuild the thread containing ID.
4582 If LINE, insert the rebuilt thread starting on line LINE."
4583   (let ((inhibit-read-only t)
4584         old-pos current thread data)
4585     (if (not gnus-show-threads)
4586         (setq thread (list (car (gnus-id-to-thread id))))
4587       ;; Get the thread this article is part of.
4588       (setq thread (gnus-remove-thread id)))
4589     (setq old-pos (point-at-bol))
4590     (setq current (save-excursion
4591                     (and (re-search-backward "[\r\n]" nil t)
4592                          (gnus-summary-article-number))))
4593     ;; If this is a gathered thread, we have to go some re-gathering.
4594     (when (stringp (car thread))
4595       (let ((subject (car thread))
4596             roots thr)
4597         (setq thread (cdr thread))
4598         (while thread
4599           (unless (memq (setq thr (gnus-id-to-thread
4600                                    (gnus-root-id
4601                                     (mail-header-id (caar thread)))))
4602                         roots)
4603             (push thr roots))
4604           (setq thread (cdr thread)))
4605         ;; We now have all (unique) roots.
4606         (if (= (length roots) 1)
4607             ;; All the loose roots are now one solid root.
4608             (setq thread (car roots))
4609           (setq thread (cons subject (gnus-sort-threads roots))))))
4610     (let (threads)
4611       ;; We then insert this thread into the summary buffer.
4612       (when line
4613         (goto-char (point-min))
4614         (forward-line (1- line)))
4615       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4616         (if gnus-show-threads
4617             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4618           (gnus-summary-prepare-unthreaded thread))
4619         (setq data (nreverse gnus-newsgroup-data))
4620         (setq threads gnus-newsgroup-threads))
4621       ;; We splice the new data into the data structure.
4622       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4623       ;;!!! then we want to insert at the beginning of the buffer.
4624       ;;!!! That happens to be true with Gnus now, but that may
4625       ;;!!! change in the future.  Perhaps.
4626       (gnus-data-enter-list
4627        (if line nil current) data (- (point) old-pos))
4628       (setq gnus-newsgroup-threads
4629             (nconc threads gnus-newsgroup-threads))
4630       (gnus-data-compute-positions))))
4631
4632 (defun gnus-number-to-header (number)
4633   "Return the header for article NUMBER."
4634   (let ((headers gnus-newsgroup-headers))
4635     (while (and headers
4636                 (not (= number (mail-header-number (car headers)))))
4637       (pop headers))
4638     (when headers
4639       (car headers))))
4640
4641 (defun gnus-parent-headers (in-headers &optional generation)
4642   "Return the headers of the GENERATIONeth parent of HEADERS."
4643   (unless generation
4644     (setq generation 1))
4645   (let ((parent t)
4646         (headers in-headers)
4647         references)
4648     (while (and parent
4649                 (not (zerop generation))
4650                 (setq references (mail-header-references headers)))
4651       (setq headers (if (and references
4652                              (setq parent (gnus-parent-id references)))
4653                         (car (gnus-id-to-thread parent))
4654                       nil))
4655       (decf generation))
4656     (and (not (eq headers in-headers))
4657          headers)))
4658
4659 (defun gnus-id-to-thread (id)
4660   "Return the (sub-)thread where ID appears."
4661   (gnus-gethash id gnus-newsgroup-dependencies))
4662
4663 (defun gnus-id-to-article (id)
4664   "Return the article number of ID."
4665   (let ((thread (gnus-id-to-thread id)))
4666     (when (and thread
4667                (car thread))
4668       (mail-header-number (car thread)))))
4669
4670 (defun gnus-id-to-header (id)
4671   "Return the article headers of ID."
4672   (car (gnus-id-to-thread id)))
4673
4674 (defun gnus-article-displayed-root-p (article)
4675   "Say whether ARTICLE is a root(ish) article."
4676   (let ((level (gnus-summary-thread-level article))
4677         (refs (mail-header-references  (gnus-summary-article-header article)))
4678         particle)
4679     (cond
4680      ((null level) nil)
4681      ((zerop level) t)
4682      ((null refs) t)
4683      ((null (gnus-parent-id refs)) t)
4684      ((and (= 1 level)
4685            (null (setq particle (gnus-id-to-article
4686                                  (gnus-parent-id refs))))
4687            (null (gnus-summary-thread-level particle)))))))
4688
4689 (defun gnus-root-id (id)
4690   "Return the id of the root of the thread where ID appears."
4691   (let (last-id prev)
4692     (while (and id (setq prev (car (gnus-id-to-thread id))))
4693       (setq last-id id
4694             id (gnus-parent-id (mail-header-references prev))))
4695     last-id))
4696
4697 (defun gnus-articles-in-thread (thread)
4698   "Return the list of articles in THREAD."
4699   (cons (mail-header-number (car thread))
4700         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4701
4702 (defun gnus-remove-thread (id &optional dont-remove)
4703   "Remove the thread that has ID in it."
4704   (let (headers thread last-id)
4705     ;; First go up in this thread until we find the root.
4706     (setq last-id (gnus-root-id id)
4707           headers (message-flatten-list (gnus-id-to-thread last-id)))
4708     ;; We have now found the real root of this thread.  It might have
4709     ;; been gathered into some loose thread, so we have to search
4710     ;; through the threads to find the thread we wanted.
4711     (let ((threads gnus-newsgroup-threads)
4712           sub)
4713       (while threads
4714         (setq sub (car threads))
4715         (if (stringp (car sub))
4716             ;; This is a gathered thread, so we look at the roots
4717             ;; below it to find whether this article is in this
4718             ;; gathered root.
4719             (progn
4720               (setq sub (cdr sub))
4721               (while sub
4722                 (when (member (caar sub) headers)
4723                   (setq thread (car threads)
4724                         threads nil
4725                         sub nil))
4726                 (setq sub (cdr sub))))
4727           ;; It's an ordinary thread, so we check it.
4728           (when (eq (car sub) (car headers))
4729             (setq thread sub
4730                   threads nil)))
4731         (setq threads (cdr threads)))
4732       ;; If this article is in no thread, then it's a root.
4733       (if thread
4734           (unless dont-remove
4735             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4736         (setq thread (gnus-id-to-thread last-id)))
4737       (when thread
4738         (prog1
4739             thread                      ; We return this thread.
4740           (unless dont-remove
4741             (if (stringp (car thread))
4742                 (progn
4743                   ;; If we use dummy roots, then we have to remove the
4744                   ;; dummy root as well.
4745                   (when (eq gnus-summary-make-false-root 'dummy)
4746                     ;; We go to the dummy root by going to
4747                     ;; the first sub-"thread", and then one line up.
4748                     (gnus-summary-goto-article
4749                      (mail-header-number (caadr thread)))
4750                     (forward-line -1)
4751                     (gnus-delete-line)
4752                     (gnus-data-compute-positions))
4753                   (setq thread (cdr thread))
4754                   (while thread
4755                     (gnus-remove-thread-1 (car thread))
4756                     (setq thread (cdr thread))))
4757               (gnus-remove-thread-1 thread))))))))
4758
4759 (defun gnus-remove-thread-1 (thread)
4760   "Remove the thread THREAD recursively."
4761   (let ((number (mail-header-number (pop thread)))
4762         d)
4763     (setq thread (reverse thread))
4764     (while thread
4765       (gnus-remove-thread-1 (pop thread)))
4766     (when (setq d (gnus-data-find number))
4767       (goto-char (gnus-data-pos d))
4768       (gnus-summary-show-thread)
4769       (gnus-data-remove
4770        number
4771        (- (point-at-bol)
4772           (prog1
4773               (1+ (point-at-eol))
4774             (gnus-delete-line)))))))
4775
4776 (defun gnus-sort-threads-recursive (threads func)
4777   (sort (mapcar (lambda (thread)
4778                   (cons (car thread)
4779                         (and (cdr thread)
4780                              (gnus-sort-threads-recursive (cdr thread) func))))
4781                 threads) func))
4782
4783 (defun gnus-sort-threads-loop (threads func)
4784   (let* ((superthread (cons nil threads))
4785          (stack (list (cons superthread threads)))
4786          remaining-threads thread)
4787     (while stack
4788       (setq remaining-threads (cdr (car stack)))
4789       (if remaining-threads
4790           (progn (setq thread (car remaining-threads))
4791                  (setcdr (car stack) (cdr remaining-threads))
4792                  (if (cdr thread)
4793                      (push (cons thread (cdr thread)) stack)))
4794         (setq thread (caar stack))
4795         (setcdr thread (sort (cdr thread) func))
4796         (pop stack)))
4797     (cdr superthread)))
4798
4799 (defun gnus-sort-threads (threads)
4800   "Sort THREADS."
4801   (if (not gnus-thread-sort-functions)
4802       threads
4803     (gnus-message 8 "Sorting threads...")
4804     (prog1
4805         (condition-case nil
4806             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4807               (gnus-sort-threads-recursive
4808                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4809           ;; Even after binding max-lisp-eval-depth, the recursive
4810           ;; sorter might fail for very long threads.  In that case,
4811           ;; try using a (less well-tested) non-recursive sorter.
4812           (error (gnus-sort-threads-loop
4813                   threads (gnus-make-sort-function
4814                            gnus-thread-sort-functions))))
4815       (gnus-message 8 "Sorting threads...done"))))
4816
4817 (defun gnus-sort-articles (articles)
4818   "Sort ARTICLES."
4819   (when gnus-article-sort-functions
4820     (gnus-message 7 "Sorting articles...")
4821     (prog1
4822         (setq gnus-newsgroup-headers
4823               (sort articles (gnus-make-sort-function
4824                               gnus-article-sort-functions)))
4825       (gnus-message 7 "Sorting articles...done"))))
4826
4827 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4828 (defmacro gnus-thread-header (thread)
4829   "Return header of first article in THREAD.
4830 Note that THREAD must never, ever be anything else than a variable -
4831 using some other form will lead to serious barfage."
4832   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4833   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4834   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4835         (vector thread) 2))
4836
4837 (defsubst gnus-article-sort-by-number (h1 h2)
4838   "Sort articles by article number."
4839   (< (mail-header-number h1)
4840      (mail-header-number h2)))
4841
4842 (defun gnus-thread-sort-by-number (h1 h2)
4843   "Sort threads by root article number."
4844   (gnus-article-sort-by-number
4845    (gnus-thread-header h1) (gnus-thread-header h2)))
4846
4847 (defsubst gnus-article-sort-by-random (h1 h2)
4848   "Sort articles randomly."
4849   (zerop (random 2)))
4850
4851 (defun gnus-thread-sort-by-random (h1 h2)
4852   "Sort threads randomly."
4853   (gnus-article-sort-by-random
4854    (gnus-thread-header h1) (gnus-thread-header h2)))
4855
4856 (defsubst gnus-article-sort-by-lines (h1 h2)
4857   "Sort articles by article Lines header."
4858   (< (mail-header-lines h1)
4859      (mail-header-lines h2)))
4860
4861 (defun gnus-thread-sort-by-lines (h1 h2)
4862   "Sort threads by root article Lines header."
4863   (gnus-article-sort-by-lines
4864    (gnus-thread-header h1) (gnus-thread-header h2)))
4865
4866 (defsubst gnus-article-sort-by-chars (h1 h2)
4867   "Sort articles by octet length."
4868   (< (mail-header-chars h1)
4869      (mail-header-chars h2)))
4870
4871 (defun gnus-thread-sort-by-chars (h1 h2)
4872   "Sort threads by root article octet length."
4873   (gnus-article-sort-by-chars
4874    (gnus-thread-header h1) (gnus-thread-header h2)))
4875
4876 (defsubst gnus-article-sort-by-author (h1 h2)
4877   "Sort articles by root author."
4878   (gnus-string<
4879    (let ((extract (funcall
4880                    gnus-extract-address-components
4881                    (mail-header-from h1))))
4882      (or (car extract) (cadr extract) ""))
4883    (let ((extract (funcall
4884                    gnus-extract-address-components
4885                    (mail-header-from h2))))
4886      (or (car extract) (cadr extract) ""))))
4887
4888 (defun gnus-thread-sort-by-author (h1 h2)
4889   "Sort threads by root author."
4890   (gnus-article-sort-by-author
4891    (gnus-thread-header h1)  (gnus-thread-header h2)))
4892
4893 (defsubst gnus-article-sort-by-recipient (h1 h2)
4894   "Sort articles by recipient."
4895   (gnus-string<
4896    (let ((extract (funcall
4897                    gnus-extract-address-components
4898                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4899      (or (car extract) (cadr extract)))
4900    (let ((extract (funcall
4901                    gnus-extract-address-components
4902                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4903      (or (car extract) (cadr extract)))))
4904
4905 (defun gnus-thread-sort-by-recipient (h1 h2)
4906   "Sort threads by root recipient."
4907   (gnus-article-sort-by-recipient
4908    (gnus-thread-header h1) (gnus-thread-header h2)))
4909
4910 (defsubst gnus-article-sort-by-subject (h1 h2)
4911   "Sort articles by root subject."
4912   (gnus-string<
4913    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4914    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4915
4916 (defun gnus-thread-sort-by-subject (h1 h2)
4917   "Sort threads by root subject."
4918   (gnus-article-sort-by-subject
4919    (gnus-thread-header h1) (gnus-thread-header h2)))
4920
4921 (defsubst gnus-article-sort-by-date (h1 h2)
4922   "Sort articles by root article date."
4923   (time-less-p
4924    (gnus-date-get-time (mail-header-date h1))
4925    (gnus-date-get-time (mail-header-date h2))))
4926
4927 (defun gnus-thread-sort-by-date (h1 h2)
4928   "Sort threads by root article date."
4929   (gnus-article-sort-by-date
4930    (gnus-thread-header h1) (gnus-thread-header h2)))
4931
4932 (defsubst gnus-article-sort-by-score (h1 h2)
4933   "Sort articles by root article score.
4934 Unscored articles will be counted as having a score of zero."
4935   (> (or (cdr (assq (mail-header-number h1)
4936                     gnus-newsgroup-scored))
4937          gnus-summary-default-score 0)
4938      (or (cdr (assq (mail-header-number h2)
4939                     gnus-newsgroup-scored))
4940          gnus-summary-default-score 0)))
4941
4942 (defun gnus-thread-sort-by-score (h1 h2)
4943   "Sort threads by root article score."
4944   (gnus-article-sort-by-score
4945    (gnus-thread-header h1) (gnus-thread-header h2)))
4946
4947 (defun gnus-thread-sort-by-total-score (h1 h2)
4948   "Sort threads by the sum of all scores in the thread.
4949 Unscored articles will be counted as having a score of zero."
4950   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4951
4952 (defun gnus-thread-total-score (thread)
4953   ;; This function find the total score of THREAD.
4954   (cond
4955    ((null thread)
4956     0)
4957    ((consp thread)
4958     (if (stringp (car thread))
4959         (apply gnus-thread-score-function 0
4960                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4961       (gnus-thread-total-score-1 thread)))
4962    (t
4963     (gnus-thread-total-score-1 (list thread)))))
4964
4965 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4966   "Sort threads such that the thread with the most recently arrived article comes first."
4967   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4968
4969 (defun gnus-thread-highest-number (thread)
4970   "Return the highest article number in THREAD."
4971   (apply 'max (mapcar (lambda (header)
4972                         (mail-header-number header))
4973                       (message-flatten-list thread))))
4974
4975 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4976   "Sort threads such that the thread with the most recently dated article comes first."
4977   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4978
4979 (defun gnus-thread-latest-date (thread)
4980   "Return the highest article date in THREAD."
4981   (let ((previous-time 0))
4982     (apply 'max
4983            (mapcar
4984             (lambda (header)
4985               (setq previous-time
4986                     (condition-case ()
4987                         (time-to-seconds (mail-header-parse-date
4988                                           (mail-header-date header)))
4989                       (error previous-time))))
4990             (sort
4991              (message-flatten-list thread)
4992              (lambda (h1 h2)
4993                (< (mail-header-number h1)
4994                   (mail-header-number h2))))))))
4995
4996 (defun gnus-thread-total-score-1 (root)
4997   ;; This function find the total score of the thread below ROOT.
4998   (setq root (car root))
4999   (apply gnus-thread-score-function
5000          (or (append
5001               (mapcar 'gnus-thread-total-score
5002                       (cdr (gnus-id-to-thread (mail-header-id root))))
5003               (when (> (mail-header-number root) 0)
5004                 (list (or (cdr (assq (mail-header-number root)
5005                                      gnus-newsgroup-scored))
5006                           gnus-summary-default-score 0))))
5007              (list gnus-summary-default-score)
5008              '(0))))
5009
5010 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5011 (defvar gnus-tmp-prev-subject nil)
5012 (defvar gnus-tmp-false-parent nil)
5013 (defvar gnus-tmp-root-expunged nil)
5014 (defvar gnus-tmp-dummy-line nil)
5015
5016 (defun gnus-extra-header (type &optional header)
5017   "Return the extra header of TYPE."
5018   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5019       ""))
5020
5021 (defvar gnus-tmp-thread-tree-header-string "")
5022
5023 (defcustom gnus-sum-thread-tree-root "> "
5024   "With %B spec, used for the root of a thread.
5025 If nil, use subject instead."
5026   :version "22.1"
5027   :type '(radio (const :format "%v  " nil) string)
5028   :group 'gnus-thread)
5029
5030 (defcustom gnus-sum-thread-tree-false-root "> "
5031   "With %B spec, used for a false root of a thread.
5032 If nil, use subject instead."
5033   :version "22.1"
5034   :type '(radio (const :format "%v  " nil) string)
5035   :group 'gnus-thread)
5036
5037 (defcustom gnus-sum-thread-tree-single-indent ""
5038   "With %B spec, used for a thread with just one message.
5039 If nil, use subject instead."
5040   :version "22.1"
5041   :type '(radio (const :format "%v  " nil) string)
5042   :group 'gnus-thread)
5043
5044 (defcustom gnus-sum-thread-tree-vertical "| "
5045   "With %B spec, used for drawing a vertical line."
5046   :version "22.1"
5047   :type 'string
5048   :group 'gnus-thread)
5049
5050 (defcustom gnus-sum-thread-tree-indent "  "
5051   "With %B spec, used for indenting."
5052   :version "22.1"
5053   :type 'string
5054   :group 'gnus-thread)
5055
5056 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5057   "With %B spec, used for a leaf with brothers."
5058   :version "22.1"
5059   :type 'string
5060   :group 'gnus-thread)
5061
5062 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5063   "With %B spec, used for a leaf without brothers."
5064   :version "22.1"
5065   :type 'string
5066   :group 'gnus-thread)
5067
5068 (defcustom gnus-summary-display-while-building nil
5069   "If non-nil, show and update the summary buffer as it's being built.
5070 If the value is t, update the buffer after every line is inserted.  If
5071 the value is an integer (N), update the display every N lines."
5072   :version "22.1"
5073   :group 'gnus-thread
5074   :type '(choice (const :tag "off" nil)
5075                  number
5076                  (const :tag "frequently" t)))
5077
5078 (defun gnus-summary-prepare-threads (threads)
5079   "Prepare summary buffer from THREADS and indentation LEVEL.
5080 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5081 or a straight list of headers."
5082   (gnus-message 7 "Generating summary...")
5083
5084   (setq gnus-newsgroup-threads threads)
5085   (beginning-of-line)
5086
5087   (let ((gnus-tmp-level 0)
5088         (default-score (or gnus-summary-default-score 0))
5089         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5090         (building-line-count gnus-summary-display-while-building)
5091         (building-count (integerp gnus-summary-display-while-building))
5092         thread number subject stack state gnus-tmp-gathered beg-match
5093         new-roots gnus-tmp-new-adopts thread-end simp-subject
5094         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5095         gnus-tmp-replied gnus-tmp-subject-or-nil
5096         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5097         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5098         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5099         tree-stack)
5100
5101     (setq gnus-tmp-prev-subject nil
5102           gnus-tmp-thread-tree-header-string "")
5103
5104     (if (vectorp (car threads))
5105         ;; If this is a straight (sic) list of headers, then a
5106         ;; threaded summary display isn't required, so we just create
5107         ;; an unthreaded one.
5108         (gnus-summary-prepare-unthreaded threads)
5109
5110       ;; Do the threaded display.
5111
5112       (if gnus-summary-display-while-building
5113           (switch-to-buffer (buffer-name)))
5114       (while (or threads stack gnus-tmp-new-adopts new-roots)
5115
5116         (if (and (= gnus-tmp-level 0)
5117                  (or (not stack)
5118                      (= (caar stack) 0))
5119                  (not gnus-tmp-false-parent)
5120                  (or gnus-tmp-new-adopts new-roots))
5121             (if gnus-tmp-new-adopts
5122                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5123                       thread (list (car gnus-tmp-new-adopts))
5124                       gnus-tmp-header (caar thread)
5125                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5126               (when new-roots
5127                 (setq thread (list (car new-roots))
5128                       gnus-tmp-header (caar thread)
5129                       new-roots (cdr new-roots))))
5130
5131           (if threads
5132               ;; If there are some threads, we do them before the
5133               ;; threads on the stack.
5134               (setq thread threads
5135                     gnus-tmp-header (caar thread))
5136             ;; There were no current threads, so we pop something off
5137             ;; the stack.
5138             (setq state (car stack)
5139                   gnus-tmp-level (car state)
5140                   tree-stack (cadr state)
5141                   thread (caddr state)
5142                   stack (cdr stack)
5143                   gnus-tmp-header (caar thread))))
5144
5145         (setq gnus-tmp-false-parent nil)
5146         (setq gnus-tmp-root-expunged nil)
5147         (setq thread-end nil)
5148
5149         (if (stringp gnus-tmp-header)
5150             ;; The header is a dummy root.
5151             (cond
5152              ((eq gnus-summary-make-false-root 'adopt)
5153               ;; We let the first article adopt the rest.
5154               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5155                                                (cddar thread)))
5156               (setq gnus-tmp-gathered
5157                     (nconc (mapcar
5158                             (lambda (h) (mail-header-number (car h)))
5159                             (cddar thread))
5160                            gnus-tmp-gathered))
5161               (setq thread (cons (list (caar thread)
5162                                        (cadar thread))
5163                                  (cdr thread)))
5164               (setq gnus-tmp-level -1
5165                     gnus-tmp-false-parent t))
5166              ((eq gnus-summary-make-false-root 'empty)
5167               ;; We print adopted articles with empty subject fields.
5168               (setq gnus-tmp-gathered
5169                     (nconc (mapcar
5170                             (lambda (h) (mail-header-number (car h)))
5171                             (cddar thread))
5172                            gnus-tmp-gathered))
5173               (setq gnus-tmp-level -1))
5174              ((eq gnus-summary-make-false-root 'dummy)
5175               ;; We remember that we probably want to output a dummy
5176               ;; root.
5177               (setq gnus-tmp-dummy-line gnus-tmp-header)
5178               (setq gnus-tmp-prev-subject gnus-tmp-header))
5179              (t
5180               ;; We do not make a root for the gathered
5181               ;; sub-threads at all.
5182               (setq gnus-tmp-level -1)))
5183
5184           (setq number (mail-header-number gnus-tmp-header)
5185                 subject (mail-header-subject gnus-tmp-header)
5186                 simp-subject (gnus-simplify-subject-fully subject))
5187
5188           (cond
5189            ;; If the thread has changed subject, we might want to make
5190            ;; this subthread into a root.
5191            ((and (null gnus-thread-ignore-subject)
5192                  (not (zerop gnus-tmp-level))
5193                  gnus-tmp-prev-subject
5194                  (not (string= gnus-tmp-prev-subject simp-subject)))
5195             (setq new-roots (nconc new-roots (list (car thread)))
5196                   thread-end t
5197                   gnus-tmp-header nil))
5198            ;; If the article lies outside the current limit,
5199            ;; then we do not display it.
5200            ((not (memq number gnus-newsgroup-limit))
5201             (setq gnus-tmp-gathered
5202                   (nconc (mapcar
5203                           (lambda (h) (mail-header-number (car h)))
5204                           (cdar thread))
5205                          gnus-tmp-gathered))
5206             (setq gnus-tmp-new-adopts (if (cdar thread)
5207                                           (append gnus-tmp-new-adopts
5208                                                   (cdar thread))
5209                                         gnus-tmp-new-adopts)
5210                   thread-end t
5211                   gnus-tmp-header nil)
5212             (when (zerop gnus-tmp-level)
5213               (setq gnus-tmp-root-expunged t)))
5214            ;; Perhaps this article is to be marked as read?
5215            ((and gnus-summary-mark-below
5216                  (< (or (cdr (assq number gnus-newsgroup-scored))
5217                         default-score)
5218                     gnus-summary-mark-below)
5219                  ;; Don't touch sparse articles.
5220                  (not (gnus-summary-article-sparse-p number))
5221                  (not (gnus-summary-article-ancient-p number)))
5222             (setq gnus-newsgroup-unreads
5223                   (delq number gnus-newsgroup-unreads))
5224             (if gnus-newsgroup-auto-expire
5225                 (setq gnus-newsgroup-expirable
5226                       (gnus-add-to-sorted-list
5227                        gnus-newsgroup-expirable number))
5228               (push (cons number gnus-low-score-mark)
5229                     gnus-newsgroup-reads))))
5230
5231           (when gnus-tmp-header
5232             ;; We may have an old dummy line to output before this
5233             ;; article.
5234             (when (and gnus-tmp-dummy-line
5235                        (gnus-subject-equal
5236                         gnus-tmp-dummy-line
5237                         (mail-header-subject gnus-tmp-header)))
5238               (gnus-summary-insert-dummy-line
5239                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5240               (setq gnus-tmp-dummy-line nil))
5241
5242             ;; Compute the mark.
5243             (setq gnus-tmp-unread (gnus-article-mark number))
5244
5245             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5246                                   gnus-tmp-header gnus-tmp-level)
5247                   gnus-newsgroup-data)
5248
5249             ;; Actually insert the line.
5250             (setq
5251              gnus-tmp-subject-or-nil
5252              (cond
5253               ((and gnus-thread-ignore-subject
5254                     gnus-tmp-prev-subject
5255                     (not (string= gnus-tmp-prev-subject simp-subject)))
5256                subject)
5257               ((zerop gnus-tmp-level)
5258                (if (and (eq gnus-summary-make-false-root 'empty)
5259                         (memq number gnus-tmp-gathered)
5260                         gnus-tmp-prev-subject
5261                         (string= gnus-tmp-prev-subject simp-subject))
5262                    gnus-summary-same-subject
5263                  subject))
5264               (t gnus-summary-same-subject)))
5265             (if (and (eq gnus-summary-make-false-root 'adopt)
5266                      (= gnus-tmp-level 1)
5267                      (memq number gnus-tmp-gathered))
5268                 (setq gnus-tmp-opening-bracket ?\<
5269                       gnus-tmp-closing-bracket ?\>)
5270               (setq gnus-tmp-opening-bracket ?\[
5271                     gnus-tmp-closing-bracket ?\]))
5272             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5273                 (gnus-make-thread-indent-array
5274                  (max (* 2 (length gnus-thread-indent-array))
5275                       gnus-tmp-level)))
5276             (setq
5277              gnus-tmp-indentation
5278              (aref gnus-thread-indent-array gnus-tmp-level)
5279              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5280              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5281                                 gnus-summary-default-score 0)
5282              gnus-tmp-score-char
5283              (if (or (null gnus-summary-default-score)
5284                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5285                          gnus-summary-zcore-fuzz))
5286                  ?                      ;Whitespace
5287                (if (< gnus-tmp-score gnus-summary-default-score)
5288                    gnus-score-below-mark gnus-score-over-mark))
5289              gnus-tmp-replied
5290              (cond ((memq number gnus-newsgroup-processable)
5291                     gnus-process-mark)
5292                    ((memq number gnus-newsgroup-cached)
5293                     gnus-cached-mark)
5294                    ((memq number gnus-newsgroup-replied)
5295                     gnus-replied-mark)
5296                    ((memq number gnus-newsgroup-forwarded)
5297                     gnus-forwarded-mark)
5298                    ((memq number gnus-newsgroup-saved)
5299                     gnus-saved-mark)
5300                    ((memq number gnus-newsgroup-recent)
5301                     gnus-recent-mark)
5302                    ((memq number gnus-newsgroup-unseen)
5303                     gnus-unseen-mark)
5304                    (t gnus-no-mark))
5305              gnus-tmp-downloaded
5306              (cond ((memq number gnus-newsgroup-undownloaded)
5307                     gnus-undownloaded-mark)
5308                    (gnus-newsgroup-agentized
5309                     gnus-downloaded-mark)
5310                    (t
5311                     gnus-no-mark))
5312              gnus-tmp-from (mail-header-from gnus-tmp-header)
5313              gnus-tmp-name
5314              (cond
5315               ((string-match "<[^>]+> *$" gnus-tmp-from)
5316                (setq beg-match (match-beginning 0))
5317                (or (and (string-match "^\".+\"" gnus-tmp-from)
5318                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5319                    (substring gnus-tmp-from 0 beg-match)))
5320               ((string-match "(.+)" gnus-tmp-from)
5321                (substring gnus-tmp-from
5322                           (1+ (match-beginning 0)) (1- (match-end 0))))
5323               (t gnus-tmp-from))
5324
5325              ;; Do the %B string
5326              gnus-tmp-thread-tree-header-string
5327              (cond
5328               ((not gnus-show-threads) "")
5329               ((zerop gnus-tmp-level)
5330                (cond ((cdar thread)
5331                       (or gnus-sum-thread-tree-root subject))
5332                      (gnus-tmp-new-adopts
5333                       (or gnus-sum-thread-tree-false-root subject))
5334                      (t
5335                       (or gnus-sum-thread-tree-single-indent subject))))
5336               (t
5337                (concat (apply 'concat
5338                               (mapcar (lambda (item)
5339                                         (if (= item 1)
5340                                             gnus-sum-thread-tree-vertical
5341                                           gnus-sum-thread-tree-indent))
5342                                       (cdr (reverse tree-stack))))
5343                        (if (nth 1 thread)
5344                            gnus-sum-thread-tree-leaf-with-other
5345                          gnus-sum-thread-tree-single-leaf)))))
5346             (when (string= gnus-tmp-name "")
5347               (setq gnus-tmp-name gnus-tmp-from))
5348             (unless (numberp gnus-tmp-lines)
5349               (setq gnus-tmp-lines -1))
5350             (if (= gnus-tmp-lines -1)
5351                 (setq gnus-tmp-lines "?")
5352               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5353               (gnus-put-text-property
5354              (point)
5355              (progn (eval gnus-summary-line-format-spec) (point))
5356                'gnus-number number)
5357             (when gnus-visual-p
5358               (forward-line -1)
5359               (gnus-run-hooks 'gnus-summary-update-hook)
5360               (forward-line 1))
5361
5362             (setq gnus-tmp-prev-subject simp-subject)))
5363
5364         (when (nth 1 thread)
5365           (push (list (max 0 gnus-tmp-level)
5366                       (copy-sequence tree-stack)
5367                       (nthcdr 1 thread))
5368                 stack))
5369         (push (if (nth 1 thread) 1 0) tree-stack)
5370         (incf gnus-tmp-level)
5371         (setq threads (if thread-end nil (cdar thread)))
5372         (if gnus-summary-display-while-building
5373             (if building-count
5374                 (progn
5375                   ;; use a set frequency
5376                   (setq building-line-count (1- building-line-count))
5377                   (when (= building-line-count 0)
5378                     (sit-for 0)
5379                     (setq building-line-count
5380                           gnus-summary-display-while-building)))
5381               ;; always
5382               (sit-for 0)))
5383         (unless threads
5384           (setq gnus-tmp-level 0)))))
5385   (gnus-message 7 "Generating summary...done"))
5386
5387 (defun gnus-summary-prepare-unthreaded (headers)
5388   "Generate an unthreaded summary buffer based on HEADERS."
5389   (let (header number mark)
5390
5391     (beginning-of-line)
5392
5393     (while headers
5394       ;; We may have to root out some bad articles...
5395       (when (memq (setq number (mail-header-number
5396                                 (setq header (pop headers))))
5397                   gnus-newsgroup-limit)
5398         ;; Mark article as read when it has a low score.
5399         (when (and gnus-summary-mark-below
5400                    (< (or (cdr (assq number gnus-newsgroup-scored))
5401                           gnus-summary-default-score 0)
5402                       gnus-summary-mark-below)
5403                    (not (gnus-summary-article-ancient-p number)))
5404           (setq gnus-newsgroup-unreads
5405                 (delq number gnus-newsgroup-unreads))
5406           (if gnus-newsgroup-auto-expire
5407               (push number gnus-newsgroup-expirable)
5408             (push (cons number gnus-low-score-mark)
5409                   gnus-newsgroup-reads)))
5410
5411         (setq mark (gnus-article-mark number))
5412         (push (gnus-data-make number mark (1+ (point)) header 0)
5413               gnus-newsgroup-data)
5414         (gnus-summary-insert-line
5415          header 0 number
5416          (memq number gnus-newsgroup-undownloaded)
5417          mark (memq number gnus-newsgroup-replied)
5418          (memq number gnus-newsgroup-expirable)
5419          (mail-header-subject header) nil
5420          (cdr (assq number gnus-newsgroup-scored))
5421          (memq number gnus-newsgroup-processable))))))
5422
5423 (defun gnus-summary-remove-list-identifiers ()
5424   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5425   (let ((regexp (if (consp gnus-list-identifiers)
5426                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5427                   gnus-list-identifiers))
5428         changed subject)
5429     (when regexp
5430       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5431       (dolist (header gnus-newsgroup-headers)
5432         (setq subject (mail-header-subject header)
5433               changed nil)
5434         (while (string-match regexp subject)
5435           (setq subject
5436                 (concat (substring subject 0 (match-beginning 1))
5437                         (substring subject (match-end 0)))
5438                 changed t))
5439         (when changed
5440           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5441             (setq subject
5442                   (concat (substring subject 0 (match-beginning 1))
5443                           (substring subject (match-end 1)))))
5444           (mail-header-set-subject header subject))))))
5445
5446 (defun gnus-fetch-headers (articles)
5447   "Fetch headers of ARTICLES."
5448   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5449     (gnus-message 5 "Fetching headers for %s..." name)
5450     (prog1
5451         (if (eq 'nov
5452                 (setq gnus-headers-retrieved-by
5453                       (gnus-retrieve-headers
5454                        articles gnus-newsgroup-name
5455                        ;; We might want to fetch old headers, but
5456                        ;; not if there is only 1 article.
5457                        (and (or (and
5458                                  (not (eq gnus-fetch-old-headers 'some))
5459                                  (not (numberp gnus-fetch-old-headers)))
5460                                 (> (length articles) 1))
5461                             gnus-fetch-old-headers))))
5462             (gnus-get-newsgroup-headers-xover
5463              articles nil nil gnus-newsgroup-name t)
5464           (gnus-get-newsgroup-headers))
5465       (gnus-message 5 "Fetching headers for %s...done" name))))
5466
5467 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5468   "Select newsgroup GROUP.
5469 If READ-ALL is non-nil, all articles in the group are selected.
5470 If SELECT-ARTICLES, only select those articles from GROUP."
5471   (let* ((entry (gnus-group-entry group))
5472          ;;!!! Dirty hack; should be removed.
5473          (gnus-summary-ignore-duplicates
5474           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5475               t
5476             gnus-summary-ignore-duplicates))
5477          (info (nth 2 entry))
5478          charset articles fetched-articles cached)
5479
5480     (unless (gnus-check-server
5481              (set (make-local-variable 'gnus-current-select-method)
5482                   (gnus-find-method-for-group group)))
5483       (error "Couldn't open server"))
5484     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5485
5486     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5487         (gnus-activate-group group)     ; Or we can activate it...
5488         (progn                          ; Or we bug out.
5489           (when (equal major-mode 'gnus-summary-mode)
5490             (gnus-kill-buffer (current-buffer)))
5491           (error
5492            "Couldn't activate group %s: %s"
5493            (mm-decode-coding-string group charset)
5494            (mm-decode-coding-string (gnus-status-message group) charset))))
5495
5496     (unless (gnus-request-group group t)
5497         (when (equal major-mode 'gnus-summary-mode)
5498           (gnus-kill-buffer (current-buffer)))
5499         (error "Couldn't request group %s: %s"
5500                (mm-decode-coding-string group charset)
5501                (mm-decode-coding-string (gnus-status-message group) charset)))
5502
5503     (when gnus-agent
5504       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5505
5506       (setq gnus-summary-use-undownloaded-faces
5507             (gnus-agent-find-parameter
5508              group
5509              'agent-enable-undownloaded-faces)))
5510
5511     (setq gnus-newsgroup-name group
5512           gnus-newsgroup-unselected nil
5513           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5514
5515     (let ((display (gnus-group-find-parameter group 'display)))
5516       (setq gnus-newsgroup-display
5517             (cond
5518              ((not (zerop (or (car-safe read-all) 0)))
5519               ;; The user entered the group with C-u SPC/RET, let's show
5520               ;; all articles.
5521               'gnus-not-ignore)
5522              ((eq display 'all)
5523               'gnus-not-ignore)
5524              ((arrayp display)
5525               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5526              ((numberp display)
5527               ;; The following is probably the "correct" solution, but
5528               ;; it makes Gnus fetch all headers and then limit the
5529               ;; articles (which is slow), so instead we hack the
5530               ;; select-articles parameter instead. -- Simon Josefsson
5531               ;; <jas@kth.se>
5532               ;;
5533               ;; (gnus-byte-compile
5534               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5535               ;;                         display)))))
5536               (setq select-articles
5537                     (gnus-uncompress-range
5538                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5539                              (if (> tmp 0)
5540                                  tmp
5541                                1))
5542                            (cdr (gnus-active group)))))
5543               nil)
5544              (t
5545               nil))))
5546
5547     (gnus-summary-setup-default-charset)
5548
5549     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5550     (when (gnus-virtual-group-p group)
5551       (setq cached gnus-newsgroup-cached))
5552
5553     (setq gnus-newsgroup-unreads
5554           (gnus-sorted-ndifference
5555            (gnus-sorted-ndifference gnus-newsgroup-unreads
5556                                     gnus-newsgroup-marked)
5557            gnus-newsgroup-dormant))
5558
5559     (setq gnus-newsgroup-processable nil)
5560
5561     (gnus-update-read-articles group gnus-newsgroup-unreads)
5562
5563     ;; Adjust and set lists of article marks.
5564     (when info
5565       (gnus-adjust-marked-articles info))
5566     (if (setq articles select-articles)
5567         (setq gnus-newsgroup-unselected
5568               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5569       (setq articles (gnus-articles-to-read group read-all)))
5570
5571     (cond
5572      ((null articles)
5573       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5574       'quit)
5575      ((eq articles 0) nil)
5576      (t
5577       ;; Init the dependencies hash table.
5578       (setq gnus-newsgroup-dependencies
5579             (gnus-make-hashtable (length articles)))
5580       (gnus-set-global-variables)
5581       ;; Retrieve the headers and read them in.
5582
5583       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5584
5585       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5586       (when cached
5587         (setq gnus-newsgroup-cached cached))
5588
5589       ;; Suppress duplicates?
5590       (when gnus-suppress-duplicates
5591         (gnus-dup-suppress-articles))
5592
5593       ;; Set the initial limit.
5594       (setq gnus-newsgroup-limit (copy-sequence articles))
5595       ;; Remove canceled articles from the list of unread articles.
5596       (setq fetched-articles
5597             (mapcar (lambda (headers) (mail-header-number headers))
5598                     gnus-newsgroup-headers))
5599       (setq gnus-newsgroup-articles fetched-articles)
5600       (setq gnus-newsgroup-unreads
5601             (gnus-sorted-nintersection
5602              gnus-newsgroup-unreads fetched-articles))
5603       (gnus-compute-unseen-list)
5604
5605       ;; Removed marked articles that do not exist.
5606       (gnus-update-missing-marks
5607        (gnus-sorted-difference articles fetched-articles))
5608       ;; We might want to build some more threads first.
5609       (when (and gnus-fetch-old-headers
5610                  (eq gnus-headers-retrieved-by 'nov))
5611         (if (eq gnus-fetch-old-headers 'invisible)
5612             (gnus-build-all-threads)
5613           (gnus-build-old-threads)))
5614       ;; Let the Gnus agent mark articles as read.
5615       (when gnus-agent
5616         (gnus-agent-get-undownloaded-list))
5617       ;; Remove list identifiers from subject
5618       (when gnus-list-identifiers
5619         (gnus-summary-remove-list-identifiers))
5620       ;; Check whether auto-expire is to be done in this group.
5621       (setq gnus-newsgroup-auto-expire
5622             (gnus-group-auto-expirable-p group))
5623       ;; Set up the article buffer now, if necessary.
5624       (unless (and gnus-single-article-buffer
5625                    (equal gnus-article-buffer "*Article*"))
5626         (gnus-article-setup-buffer))
5627       ;; First and last article in this newsgroup.
5628       (when gnus-newsgroup-headers
5629         (setq gnus-newsgroup-begin
5630               (mail-header-number (car gnus-newsgroup-headers))
5631               gnus-newsgroup-end
5632               (mail-header-number
5633                (gnus-last-element gnus-newsgroup-headers))))
5634       ;; GROUP is successfully selected.
5635       (or gnus-newsgroup-headers t)))))
5636
5637 (defun gnus-compute-unseen-list ()
5638   ;; The `seen' marks are treated specially.
5639   (if (not gnus-newsgroup-seen)
5640       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5641     (setq gnus-newsgroup-unseen
5642           (gnus-inverse-list-range-intersection
5643            gnus-newsgroup-articles gnus-newsgroup-seen))))
5644
5645 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5646
5647 (defun gnus-summary-display-make-predicate (display)
5648   (require 'gnus-agent)
5649   (when (= (length display) 1)
5650     (setq display (car display)))
5651   (unless gnus-summary-display-cache
5652     (dolist (elem (append '((unread . unread)
5653                             (read . read)
5654                             (unseen . unseen))
5655                           gnus-article-mark-lists))
5656       (push (cons (cdr elem)
5657                   (gnus-byte-compile
5658                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5659             gnus-summary-display-cache)))
5660   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5661         (gnus-category-predicate-cache gnus-summary-display-cache))
5662     (gnus-get-predicate display)))
5663
5664 ;; Uses the dynamically bound `number' variable.
5665 (defvar number)
5666 (defun gnus-article-marked-p (type &optional article)
5667   (let ((article (or article number)))
5668     (cond
5669      ((eq type 'tick)
5670       (memq article gnus-newsgroup-marked))
5671      ((eq type 'spam)
5672       (memq article gnus-newsgroup-spam-marked))
5673      ((eq type 'unsend)
5674       (memq article gnus-newsgroup-unsendable))
5675      ((eq type 'undownload)
5676       (memq article gnus-newsgroup-undownloaded))
5677      ((eq type 'download)
5678       (memq article gnus-newsgroup-downloadable))
5679      ((eq type 'unread)
5680       (memq article gnus-newsgroup-unreads))
5681      ((eq type 'read)
5682       (memq article gnus-newsgroup-reads))
5683      ((eq type 'dormant)
5684       (memq article gnus-newsgroup-dormant) )
5685      ((eq type 'expire)
5686       (memq article gnus-newsgroup-expirable))
5687      ((eq type 'reply)
5688       (memq article gnus-newsgroup-replied))
5689      ((eq type 'killed)
5690       (memq article gnus-newsgroup-killed))
5691      ((eq type 'bookmark)
5692       (assq article gnus-newsgroup-bookmarks))
5693      ((eq type 'score)
5694       (assq article gnus-newsgroup-scored))
5695      ((eq type 'save)
5696       (memq article gnus-newsgroup-saved))
5697      ((eq type 'cache)
5698       (memq article gnus-newsgroup-cached))
5699      ((eq type 'forward)
5700       (memq article gnus-newsgroup-forwarded))
5701      ((eq type 'seen)
5702       (not (memq article gnus-newsgroup-unseen)))
5703      ((eq type 'recent)
5704       (memq article gnus-newsgroup-recent))
5705      (t t))))
5706
5707 (defun gnus-articles-to-read (group &optional read-all)
5708   "Find out what articles the user wants to read."
5709   (let* ((articles
5710           ;; Select all articles if `read-all' is non-nil, or if there
5711           ;; are no unread articles.
5712           (if (or read-all
5713                   (and (zerop (length gnus-newsgroup-marked))
5714                        (zerop (length gnus-newsgroup-unreads)))
5715                   ;; Fetch all if the predicate is non-nil.
5716                   gnus-newsgroup-display)
5717               ;; We want to select the headers for all the articles in
5718               ;; the group, so we select either all the active
5719               ;; articles in the group, or (if that's nil), the
5720               ;; articles in the cache.
5721               (or
5722                (if gnus-newsgroup-maximum-articles
5723                    (let ((active (gnus-active group)))
5724                      (gnus-uncompress-range
5725                       (cons (max (car active)
5726                                  (- (cdr active)
5727                                     gnus-newsgroup-maximum-articles
5728                                     -1))
5729                             (cdr active))))
5730                  (gnus-uncompress-range (gnus-active group)))
5731                (gnus-cache-articles-in-group group))
5732             ;; Select only the "normal" subset of articles.
5733             (gnus-sorted-nunion
5734              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5735              gnus-newsgroup-unreads)))
5736          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5737          (scored (length scored-list))
5738          (number (length articles))
5739          (marked (+ (length gnus-newsgroup-marked)
5740                     (length gnus-newsgroup-dormant)))
5741          (select
5742           (cond
5743            ((numberp read-all)
5744             read-all)
5745            ((numberp gnus-newsgroup-display)
5746             gnus-newsgroup-display)
5747            (t
5748             (condition-case ()
5749                 (cond
5750                  ((and (or (<= scored marked) (= scored number))
5751                        (numberp gnus-large-newsgroup)
5752                        (> number gnus-large-newsgroup))
5753                   (let* ((cursor-in-echo-area nil)
5754                          (initial (gnus-parameter-large-newsgroup-initial
5755                                    gnus-newsgroup-name))
5756                          (input
5757                           (read-string
5758                            (format
5759                             "How many articles from %s (%s %d): "
5760                             (gnus-group-decoded-name gnus-newsgroup-name)
5761                             (if initial "max" "default")
5762                             number)
5763                            (if initial
5764                                (cons (number-to-string initial)
5765                                      0)))))
5766                     (if (string-match "^[ \t]*$" input) number input)))
5767                  ((and (> scored marked) (< scored number)
5768                        (> (- scored number) 20))
5769                   (let ((input
5770                          (read-string
5771                           (format "%s %s (%d scored, %d total): "
5772                                   "How many articles from"
5773                                   (gnus-group-decoded-name group)
5774                                   scored number))))
5775                     (if (string-match "^[ \t]*$" input)
5776                         number input)))
5777                  (t number))
5778               (quit
5779                (message "Quit getting the articles to read")
5780                nil))))))
5781     (setq select (if (stringp select) (string-to-number select) select))
5782     (if (or (null select) (zerop select))
5783         select
5784       (if (and (not (zerop scored)) (<= (abs select) scored))
5785           (progn
5786             (setq articles (sort scored-list '<))
5787             (setq number (length articles)))
5788         (setq articles (copy-sequence articles)))
5789
5790       (when (< (abs select) number)
5791         (if (< select 0)
5792             ;; Select the N oldest articles.
5793             (setcdr (nthcdr (1- (abs select)) articles) nil)
5794           ;; Select the N most recent articles.
5795           (setq articles (nthcdr (- number select) articles))))
5796       (setq gnus-newsgroup-unselected
5797             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5798       (when gnus-alter-articles-to-read-function
5799         (setq articles
5800               (sort
5801                (funcall gnus-alter-articles-to-read-function
5802                         gnus-newsgroup-name articles)
5803                '<)))
5804       articles)))
5805
5806 (defun gnus-killed-articles (killed articles)
5807   (let (out)
5808     (while articles
5809       (when (inline (gnus-member-of-range (car articles) killed))
5810         (push (car articles) out))
5811       (setq articles (cdr articles)))
5812     out))
5813
5814 (defun gnus-uncompress-marks (marks)
5815   "Uncompress the mark ranges in MARKS."
5816   (let ((uncompressed '(score bookmark))
5817         out)
5818     (while marks
5819       (if (memq (caar marks) uncompressed)
5820           (push (car marks) out)
5821         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5822       (setq marks (cdr marks)))
5823     out))
5824
5825 (defun gnus-article-mark-to-type (mark)
5826   "Return the type of MARK."
5827   (or (cadr (assq mark gnus-article-special-mark-lists))
5828       'list))
5829
5830 (defun gnus-article-unpropagatable-p (mark)
5831   "Return whether MARK should be propagated to back end."
5832   (memq mark gnus-article-unpropagated-mark-lists))
5833
5834 (defun gnus-adjust-marked-articles (info)
5835   "Set all article lists and remove all marks that are no longer valid."
5836   (let* ((marked-lists (gnus-info-marks info))
5837          (active (gnus-active (gnus-info-group info)))
5838          (min (car active))
5839          (max (cdr active))
5840          (types gnus-article-mark-lists)
5841          marks var articles article mark mark-type
5842          bgn end)
5843
5844     (dolist (marks marked-lists)
5845       (setq mark (car marks)
5846             mark-type (gnus-article-mark-to-type mark)
5847             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5848
5849       ;; We set the variable according to the type of the marks list,
5850       ;; and then adjust the marks to a subset of the active articles.
5851       (cond
5852        ;; Adjust "simple" lists - compressed yet unsorted
5853        ((eq mark-type 'list)
5854         ;; Simultaneously uncompress and clip to active range
5855         ;; See gnus-uncompress-range for a description of possible marks
5856         (let (l lh)
5857           (if (not (cadr marks))
5858               (set var nil)
5859             (setq articles (if (numberp (cddr marks))
5860                                (list (cdr marks))
5861                              (cdr marks))
5862                   lh (cons nil nil)
5863                   l lh)
5864
5865             (while (setq article (pop articles))
5866               (cond ((consp article)
5867                      (setq bgn (max (car article) min)
5868                            end (min (cdr article) max))
5869                      (while (<= bgn end)
5870                        (setq l (setcdr l (cons bgn nil))
5871                              bgn (1+ bgn))))
5872                     ((and (<= min article)
5873                           (>= max article))
5874                      (setq l (setcdr l (cons article nil))))))
5875             (set var (cdr lh)))))
5876        ;; Adjust assocs.
5877        ((eq mark-type 'tuple)
5878         (set var (setq articles (cdr marks)))
5879         (when (not (listp (cdr (symbol-value var))))
5880           (set var (list (symbol-value var))))
5881         (when (not (listp (cdr articles)))
5882           (setq articles (list articles)))
5883         (while articles
5884           (when (or (not (consp (setq article (pop articles))))
5885                     (< (car article) min)
5886                     (> (car article) max))
5887             (set var (delq article (symbol-value var))))))
5888        ;; Adjust ranges (sloppily).
5889        ((eq mark-type 'range)
5890         (cond
5891          ((eq mark 'seen)
5892           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5893           ;; It should be (seen (NUM1 . NUM2)).
5894           (when (numberp (cddr marks))
5895             (setcdr marks (list (cdr marks))))
5896           (setq articles (cdr marks))
5897           (while (and articles
5898                       (or (and (consp (car articles))
5899                                (> min (cdar articles)))
5900                           (and (numberp (car articles))
5901                                (> min (car articles)))))
5902             (pop articles))
5903           (set var articles))))))))
5904
5905 (defun gnus-update-missing-marks (missing)
5906   "Go through the list of MISSING articles and remove them from the mark lists."
5907   (when missing
5908     (let (var m)
5909       ;; Go through all types.
5910       (dolist (elem gnus-article-mark-lists)
5911         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5912           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5913           (when (symbol-value var)
5914             ;; This list has articles.  So we delete all missing
5915             ;; articles from it.
5916             (setq m missing)
5917             (while m
5918               (set var (delq (pop m) (symbol-value var))))))))))
5919
5920 (defun gnus-update-marks ()
5921   "Enter the various lists of marked articles into the newsgroup info list."
5922   (let ((types gnus-article-mark-lists)
5923         (info (gnus-get-info gnus-newsgroup-name))
5924         type list newmarked symbol delta-marks)
5925     (when info
5926       ;; Add all marks lists to the list of marks lists.
5927       (while (setq type (pop types))
5928         (setq list (symbol-value
5929                     (setq symbol
5930                           (intern (format "gnus-newsgroup-%s" (car type))))))
5931
5932         (when list
5933           ;; Get rid of the entries of the articles that have the
5934           ;; default score.
5935           (when (and (eq (cdr type) 'score)
5936                      gnus-save-score
5937                      list)
5938             (let* ((arts list)
5939                    (prev (cons nil list))
5940                    (all prev))
5941               (while arts
5942                 (if (or (not (consp (car arts)))
5943                         (= (cdar arts) gnus-summary-default-score))
5944                     (setcdr prev (cdr arts))
5945                   (setq prev arts))
5946                 (setq arts (cdr arts)))
5947               (setq list (cdr all)))))
5948
5949         (when (eq (cdr type) 'seen)
5950           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5951
5952         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5953           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5954
5955         (when (and (gnus-check-backend-function
5956                     'request-set-mark gnus-newsgroup-name)
5957                    (not (gnus-article-unpropagatable-p (cdr type))))
5958           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5959                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5960                  (add (gnus-remove-from-range
5961                        (gnus-copy-sequence list) old)))
5962             (when add
5963               (push (list add 'add (list (cdr type))) delta-marks))
5964             (when del
5965               (push (list del 'del (list (cdr type))) delta-marks))))
5966
5967         (when list
5968           (push (cons (cdr type) list) newmarked)))
5969
5970       (when delta-marks
5971         (unless (gnus-check-group gnus-newsgroup-name)
5972           (error "Can't open server for %s" gnus-newsgroup-name))
5973         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5974
5975       ;; Enter these new marks into the info of the group.
5976       (if (nthcdr 3 info)
5977           (setcar (nthcdr 3 info) newmarked)
5978         ;; Add the marks lists to the end of the info.
5979         (when newmarked
5980           (setcdr (nthcdr 2 info) (list newmarked))))
5981
5982       ;; Cut off the end of the info if there's nothing else there.
5983       (let ((i 5))
5984         (while (and (> i 2)
5985                     (not (nth i info)))
5986           (when (nthcdr (decf i) info)
5987             (setcdr (nthcdr i info) nil)))))))
5988
5989 (defun gnus-set-mode-line (where)
5990   "Set the mode line of the article or summary buffers.
5991 If WHERE is `summary', the summary mode line format will be used."
5992   ;; Is this mode line one we keep updated?
5993   (when (and (memq where gnus-updated-mode-lines)
5994              (symbol-value
5995               (intern (format "gnus-%s-mode-line-format-spec" where))))
5996     (let (mode-string)
5997       ;; We evaluate this in the summary buffer since these
5998       ;; variables are buffer-local to that buffer.
5999       (with-current-buffer gnus-summary-buffer
6000         ;; We bind all these variables that are used in the `eval' form
6001         ;; below.
6002         (let* ((mformat (symbol-value
6003                          (intern
6004                           (format "gnus-%s-mode-line-format-spec" where))))
6005                (gnus-tmp-group-name (gnus-mode-string-quote
6006                                      (gnus-group-decoded-name
6007                                       gnus-newsgroup-name)))
6008                (gnus-tmp-article-number (or gnus-current-article 0))
6009                (gnus-tmp-unread gnus-newsgroup-unreads)
6010                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6011                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6012                (gnus-tmp-unread-and-unselected
6013                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6014                             (zerop gnus-tmp-unselected))
6015                        "")
6016                       ((zerop gnus-tmp-unselected)
6017                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6018                       (t (format "{%d(+%d) more}"
6019                                  gnus-tmp-unread-and-unticked
6020                                  gnus-tmp-unselected))))
6021                (gnus-tmp-subject
6022                 (if (and gnus-current-headers
6023                          (vectorp gnus-current-headers))
6024                     (gnus-mode-string-quote
6025                      (mail-header-subject gnus-current-headers))
6026                   ""))
6027                bufname-length max-len
6028                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6029           (setq mode-string (eval mformat))
6030           (setq bufname-length (if (string-match "%b" mode-string)
6031                                    (- (length
6032                                        (buffer-name
6033                                         (if (eq where 'summary)
6034                                             nil
6035                                           (get-buffer gnus-article-buffer))))
6036                                       2)
6037                                  0))
6038           (setq max-len (max 4 (if gnus-mode-non-string-length
6039                                    (- (window-width)
6040                                       gnus-mode-non-string-length
6041                                       bufname-length)
6042                                  (length mode-string))))
6043           ;; We might have to chop a bit of the string off...
6044           (when (> (length mode-string) max-len)
6045             (setq mode-string
6046                   (concat (truncate-string-to-width mode-string (- max-len 3))
6047                           "...")))
6048           ;; Pad the mode string a bit.
6049           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
6050       ;; Update the mode line.
6051       (setq mode-line-buffer-identification
6052             (gnus-mode-line-buffer-identification (list mode-string)))
6053       (set-buffer-modified-p t))))
6054
6055 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6056   "Go through the HEADERS list and add all Xrefs to a hash table.
6057 The resulting hash table is returned, or nil if no Xrefs were found."
6058   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6059          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6060          (xref-hashtb (gnus-make-hashtable))
6061          start group entry number xrefs header)
6062     (while headers
6063       (setq header (pop headers))
6064       (when (and (setq xrefs (mail-header-xref header))
6065                  (not (memq (setq number (mail-header-number header))
6066                             unreads)))
6067         (setq start 0)
6068         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6069           (setq start (match-end 0))
6070           (setq group (if prefix
6071                           (concat prefix (substring xrefs (match-beginning 1)
6072                                                     (match-end 1)))
6073                         (substring xrefs (match-beginning 1) (match-end 1))))
6074           (setq number
6075                 (string-to-number (substring xrefs (match-beginning 2)
6076                                           (match-end 2))))
6077           (if (setq entry (gnus-gethash group xref-hashtb))
6078               (setcdr entry (cons number (cdr entry)))
6079             (gnus-sethash group (cons number nil) xref-hashtb)))))
6080     (and start xref-hashtb)))
6081
6082 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6083   "Look through all the headers and mark the Xrefs as read."
6084   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6085         name info xref-hashtb idlist method nth4)
6086     (save-excursion
6087       (set-buffer gnus-group-buffer)
6088       (when (setq xref-hashtb
6089                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6090         (mapatoms
6091          (lambda (group)
6092            (unless (string= from-newsgroup (setq name (symbol-name group)))
6093              (setq idlist (symbol-value group))
6094              ;; Dead groups are not updated.
6095              (and (prog1
6096                       (setq info (gnus-get-info name))
6097                     (when (stringp (setq nth4 (gnus-info-method info)))
6098                       (setq nth4 (gnus-server-to-method nth4))))
6099                   ;; Only do the xrefs if the group has the same
6100                   ;; select method as the group we have just read.
6101                   (or (gnus-methods-equal-p
6102                        nth4 (gnus-find-method-for-group from-newsgroup))
6103                       virtual
6104                       (equal nth4 (setq method (gnus-find-method-for-group
6105                                                 from-newsgroup)))
6106                       (and (equal (car nth4) (car method))
6107                            (equal (nth 1 nth4) (nth 1 method))))
6108                   gnus-use-cross-reference
6109                   (or (not (eq gnus-use-cross-reference t))
6110                       virtual
6111                       ;; Only do cross-references on subscribed
6112                       ;; groups, if that is what is wanted.
6113                       (<= (gnus-info-level info) gnus-level-subscribed))
6114                   (gnus-group-make-articles-read name idlist))))
6115          xref-hashtb)))))
6116
6117 (defun gnus-compute-read-articles (group articles)
6118   (let* ((entry (gnus-group-entry group))
6119          (info (nth 2 entry))
6120          (active (gnus-active group))
6121          ninfo)
6122     (when entry
6123       ;; First peel off all invalid article numbers.
6124       (when active
6125         (let ((ids articles)
6126               id first)
6127           (while (setq id (pop ids))
6128             (when (and first (> id (cdr active)))
6129               ;; We'll end up in this situation in one particular
6130               ;; obscure situation.  If you re-scan a group and get
6131               ;; a new article that is cross-posted to a different
6132               ;; group that has not been re-scanned, you might get
6133               ;; crossposted article that has a higher number than
6134               ;; Gnus believes possible.  So we re-activate this
6135               ;; group as well.  This might mean doing the
6136               ;; crossposting thingy will *increase* the number
6137               ;; of articles in some groups.  Tsk, tsk.
6138               (setq active (or (gnus-activate-group group) active)))
6139             (when (or (> id (cdr active))
6140                       (< id (car active)))
6141               (setq articles (delq id articles))))))
6142       ;; If the read list is nil, we init it.
6143       (if (and active
6144                (null (gnus-info-read info))
6145                (> (car active) 1))
6146           (setq ninfo (cons 1 (1- (car active))))
6147         (setq ninfo (gnus-info-read info)))
6148       ;; Then we add the read articles to the range.
6149       (gnus-add-to-range
6150        ninfo (setq articles (sort articles '<))))))
6151
6152 (defun gnus-group-make-articles-read (group articles)
6153   "Update the info of GROUP to say that ARTICLES are read."
6154   (let* ((num 0)
6155          (entry (gnus-group-entry group))
6156          (info (nth 2 entry))
6157          (active (gnus-active group))
6158          range)
6159     (when entry
6160       (setq range (gnus-compute-read-articles group articles))
6161       (with-current-buffer gnus-group-buffer
6162         (gnus-undo-register
6163           `(progn
6164              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6165              (gnus-info-set-read ',info ',(gnus-info-read info))
6166              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6167              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6168              (gnus-group-update-group ,group t))))
6169       ;; Add the read articles to the range.
6170       (gnus-info-set-read info range)
6171       (gnus-request-set-mark group (list (list range 'add '(read))))
6172       ;; Then we have to re-compute how many unread
6173       ;; articles there are in this group.
6174       (when active
6175         (cond
6176          ((not range)
6177           (setq num (- (1+ (cdr active)) (car active))))
6178          ((not (listp (cdr range)))
6179           (setq num (- (cdr active) (- (1+ (cdr range))
6180                                        (car range)))))
6181          (t
6182           (while range
6183             (if (numberp (car range))
6184                 (setq num (1+ num))
6185               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6186             (setq range (cdr range)))
6187           (setq num (- (cdr active) num))))
6188         ;; Update the number of unread articles.
6189         (setcar entry num)
6190         ;; Update the group buffer.
6191         (unless (gnus-ephemeral-group-p group)
6192           (gnus-group-update-group group t))))))
6193
6194 (defvar gnus-newsgroup-none-id 0)
6195
6196 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6197   (let ((cur nntp-server-buffer)
6198         (dependencies
6199          (or dependencies
6200              (with-current-buffer gnus-summary-buffer
6201                gnus-newsgroup-dependencies)))
6202         headers id end ref number
6203         (mail-parse-charset gnus-newsgroup-charset)
6204         (mail-parse-ignored-charsets
6205          (save-current-buffer (condition-case nil
6206                                   (set-buffer gnus-summary-buffer)
6207                                 (error))
6208                               gnus-newsgroup-ignored-charsets)))
6209     (with-current-buffer nntp-server-buffer
6210       ;; Translate all TAB characters into SPACE characters.
6211       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6212       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6213       (ietf-drums-unfold-fws)
6214       (gnus-run-hooks 'gnus-parse-headers-hook)
6215       (let ((case-fold-search t)
6216             in-reply-to header p lines chars)
6217         (goto-char (point-min))
6218         ;; Search to the beginning of the next header.  Error messages
6219         ;; do not begin with 2 or 3.
6220         (while (re-search-forward "^[23][0-9]+ " nil t)
6221           (setq id nil
6222                 ref nil)
6223           ;; This implementation of this function, with nine
6224           ;; search-forwards instead of the one re-search-forward and
6225           ;; a case (which basically was the old function) is actually
6226           ;; about twice as fast, even though it looks messier.  You
6227           ;; can't have everything, I guess.  Speed and elegance
6228           ;; doesn't always go hand in hand.
6229           (setq
6230            header
6231            (vector
6232             ;; Number.
6233             (prog1
6234                 (setq number (read cur))
6235               (end-of-line)
6236               (setq p (point))
6237               (narrow-to-region (point)
6238                                 (or (and (search-forward "\n.\n" nil t)
6239                                          (- (point) 2))
6240                                     (point))))
6241             ;; Subject.
6242             (progn
6243               (goto-char p)
6244               (if (search-forward "\nsubject:" nil t)
6245                   (funcall gnus-decode-encoded-word-function
6246                            (nnheader-header-value))
6247                 "(none)"))
6248             ;; From.
6249             (progn
6250               (goto-char p)
6251               (if (search-forward "\nfrom:" nil t)
6252                   (funcall gnus-decode-encoded-address-function
6253                            (nnheader-header-value))
6254                 "(nobody)"))
6255             ;; Date.
6256             (progn
6257               (goto-char p)
6258               (if (search-forward "\ndate:" nil t)
6259                   (nnheader-header-value) ""))
6260             ;; Message-ID.
6261             (progn
6262               (goto-char p)
6263               (setq id (if (re-search-forward
6264                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6265                            ;; We do it this way to make sure the Message-ID
6266                            ;; is (somewhat) syntactically valid.
6267                            (buffer-substring (match-beginning 1)
6268                                              (match-end 1))
6269                          ;; If there was no message-id, we just fake one
6270                          ;; to make subsequent routines simpler.
6271                          (nnheader-generate-fake-message-id number))))
6272             ;; References.
6273             (progn
6274               (goto-char p)
6275               (if (search-forward "\nreferences:" nil t)
6276                   (progn
6277                     (setq end (point))
6278                     (prog1
6279                         (nnheader-header-value)
6280                       (setq ref
6281                             (buffer-substring
6282                              (progn
6283                                (end-of-line)
6284                                (search-backward ">" end t)
6285                                (1+ (point)))
6286                              (progn
6287                                (search-backward "<" end t)
6288                                (point))))))
6289                 ;; Get the references from the in-reply-to header if there
6290                 ;; were no references and the in-reply-to header looks
6291                 ;; promising.
6292                 (if (and (search-forward "\nin-reply-to:" nil t)
6293                          (setq in-reply-to (nnheader-header-value))
6294                          (string-match "<[^>]+>" in-reply-to))
6295                     (let (ref2)
6296                       (setq ref (substring in-reply-to (match-beginning 0)
6297                                            (match-end 0)))
6298                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6299                         (setq ref2 (substring in-reply-to (match-beginning 0)
6300                                               (match-end 0)))
6301                         (when (> (length ref2) (length ref))
6302                           (setq ref ref2)))
6303                       ref)
6304                   (setq ref nil))))
6305             ;; Chars.
6306             (progn
6307               (goto-char p)
6308               (if (search-forward "\nchars: " nil t)
6309                   (if (numberp (setq chars (ignore-errors (read cur))))
6310                       chars -1)
6311                 -1))
6312             ;; Lines.
6313             (progn
6314               (goto-char p)
6315               (if (search-forward "\nlines: " nil t)
6316                   (if (numberp (setq lines (ignore-errors (read cur))))
6317                       lines -1)
6318                 -1))
6319             ;; Xref.
6320             (progn
6321               (goto-char p)
6322               (and (search-forward "\nxref:" nil t)
6323                    (nnheader-header-value)))
6324             ;; Extra.
6325             (when gnus-extra-headers
6326               (let ((extra gnus-extra-headers)
6327                     out)
6328                 (while extra
6329                   (goto-char p)
6330                   (when (search-forward
6331                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6332                     (push (cons (car extra) (nnheader-header-value))
6333                           out))
6334                   (pop extra))
6335                 out))))
6336           (when (equal id ref)
6337             (setq ref nil))
6338
6339           (when gnus-alter-header-function
6340             (funcall gnus-alter-header-function header)
6341             (setq id (mail-header-id header)
6342                   ref (gnus-parent-id (mail-header-references header))))
6343
6344           (when (setq header
6345                       (gnus-dependencies-add-header
6346                        header dependencies force-new))
6347             (push header headers))
6348           (goto-char (point-max))
6349           (widen))
6350         (nreverse headers)))))
6351
6352 ;; Goes through the xover lines and returns a list of vectors
6353 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6354                                                   force-new dependencies
6355                                                   group also-fetch-heads)
6356   "Parse the news overview data in the server buffer.
6357 Return a list of headers that match SEQUENCE (see
6358 `nntp-retrieve-headers')."
6359   ;; Get the Xref when the users reads the articles since most/some
6360   ;; NNTP servers do not include Xrefs when using XOVER.
6361   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6362   (let ((mail-parse-charset gnus-newsgroup-charset)
6363         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6364         (cur nntp-server-buffer)
6365         (dependencies (or dependencies gnus-newsgroup-dependencies))
6366         (allp (cond
6367                ((eq gnus-read-all-available-headers t)
6368                 t)
6369                ((and (stringp gnus-read-all-available-headers)
6370                      group)
6371                 (string-match gnus-read-all-available-headers group))
6372                (t
6373                 nil)))
6374         number headers header)
6375     (with-current-buffer nntp-server-buffer
6376       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6377       ;; Allow the user to mangle the headers before parsing them.
6378       (gnus-run-hooks 'gnus-parse-headers-hook)
6379       (goto-char (point-min))
6380       (gnus-parse-without-error
6381         (while (and (or sequence allp)
6382                     (not (eobp)))
6383           (setq number (read cur))
6384           (when (not allp)
6385             (while (and sequence
6386                         (< (car sequence) number))
6387               (setq sequence (cdr sequence))))
6388           (when (and (or allp
6389                          (and sequence
6390                               (eq number (car sequence))))
6391                      (progn
6392                        (setq sequence (cdr sequence))
6393                        (setq header (inline
6394                                       (gnus-nov-parse-line
6395                                        number dependencies force-new)))))
6396             (push header headers))
6397           (forward-line 1)))
6398       ;; A common bug in inn is that if you have posted an article and
6399       ;; then retrieves the active file, it will answer correctly --
6400       ;; the new article is included.  However, a NOV entry for the
6401       ;; article may not have been generated yet, so this may fail.
6402       ;; We work around this problem by retrieving the last few
6403       ;; headers using HEAD.
6404       (if (or (not also-fetch-heads)
6405               (not sequence))
6406           ;; We (probably) got all the headers.
6407           (nreverse headers)
6408         (let ((gnus-nov-is-evil t))
6409           (nconc
6410            (nreverse headers)
6411            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6412              (gnus-get-newsgroup-headers))))))))
6413
6414 (defun gnus-article-get-xrefs ()
6415   "Fill in the Xref value in `gnus-current-headers', if necessary.
6416 This is meant to be called in `gnus-article-internal-prepare-hook'."
6417   (let ((headers (with-current-buffer gnus-summary-buffer
6418                    gnus-current-headers)))
6419     (or (not gnus-use-cross-reference)
6420         (not headers)
6421         (and (mail-header-xref headers)
6422              (not (string= (mail-header-xref headers) "")))
6423         (let ((case-fold-search t)
6424               xref)
6425           (save-restriction
6426             (nnheader-narrow-to-headers)
6427             (goto-char (point-min))
6428             (when (or (and (not (eobp))
6429                            (eq (downcase (char-after)) ?x)
6430                            (looking-at "Xref:"))
6431                       (search-forward "\nXref:" nil t))
6432               (goto-char (1+ (match-end 0)))
6433               (setq xref (buffer-substring (point) (point-at-eol)))
6434               (mail-header-set-xref headers xref)))))))
6435
6436 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6437   "Find article ID and insert the summary line for that article.
6438 OLD-HEADER can either be a header or a line number to insert
6439 the subject line on."
6440   (let* ((line (and (numberp old-header) old-header))
6441          (old-header (and (vectorp old-header) old-header))
6442          (header (cond ((and old-header use-old-header)
6443                         old-header)
6444                        ((and (numberp id)
6445                              (gnus-number-to-header id))
6446                         (gnus-number-to-header id))
6447                        (t
6448                         (gnus-read-header id))))
6449          (number (and (numberp id) id))
6450          d)
6451     (when header
6452       ;; Rebuild the thread that this article is part of and go to the
6453       ;; article we have fetched.
6454       (when (and (not gnus-show-threads)
6455                  old-header)
6456         (when (and number
6457                    (setq d (gnus-data-find (mail-header-number old-header))))
6458           (goto-char (gnus-data-pos d))
6459           (gnus-data-remove
6460            number
6461            (- (point-at-bol)
6462               (prog1
6463                   (1+ (point-at-eol))
6464                 (gnus-delete-line))))))
6465       ;; Remove list identifiers from subject.
6466       (when gnus-list-identifiers
6467         (let ((gnus-newsgroup-headers (list header)))
6468           (gnus-summary-remove-list-identifiers)))
6469       (when old-header
6470         (mail-header-set-number header (mail-header-number old-header)))
6471       (setq gnus-newsgroup-sparse
6472             (delq (setq number (mail-header-number header))
6473                   gnus-newsgroup-sparse))
6474       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6475       (push number gnus-newsgroup-limit)
6476       (gnus-rebuild-thread (mail-header-id header) line)
6477       (gnus-summary-goto-subject number nil t))
6478     (when (and (numberp number)
6479                (> number 0))
6480       ;; We have to update the boundaries even if we can't fetch the
6481       ;; article if ID is a number -- so that the next `P' or `N'
6482       ;; command will fetch the previous (or next) article even
6483       ;; if the one we tried to fetch this time has been canceled.
6484       (when (> number gnus-newsgroup-end)
6485         (setq gnus-newsgroup-end number))
6486       (when (< number gnus-newsgroup-begin)
6487         (setq gnus-newsgroup-begin number))
6488       (setq gnus-newsgroup-unselected
6489             (delq number gnus-newsgroup-unselected)))
6490     ;; Report back a success?
6491     (and header (mail-header-number header))))
6492
6493 ;;; Process/prefix in the summary buffer
6494
6495 (defun gnus-summary-work-articles (n)
6496   "Return a list of articles to be worked upon.
6497 The prefix argument, the list of process marked articles, and the
6498 current article will be taken into consideration."
6499   (with-current-buffer gnus-summary-buffer
6500     (cond
6501      (n
6502       ;; A numerical prefix has been given.
6503       (setq n (prefix-numeric-value n))
6504       (let ((backward (< n 0))
6505             (n (abs (prefix-numeric-value n)))
6506             articles article)
6507         (save-excursion
6508           (while
6509               (and (> n 0)
6510                    (push (setq article (gnus-summary-article-number))
6511                          articles)
6512                    (if backward
6513                        (gnus-summary-find-prev nil article)
6514                      (gnus-summary-find-next nil article)))
6515             (decf n)))
6516         (nreverse articles)))
6517      ((and (gnus-region-active-p) (mark))
6518       (message "region active")
6519       ;; Work on the region between point and mark.
6520       (let ((max (max (point) (mark)))
6521             articles article)
6522         (save-excursion
6523           (goto-char (min (point) (mark)))
6524           (while
6525               (and
6526                (push (setq article (gnus-summary-article-number)) articles)
6527                (gnus-summary-find-next nil article)
6528                (< (point) max)))
6529           (nreverse articles))))
6530      (gnus-newsgroup-processable
6531       ;; There are process-marked articles present.
6532       ;; Save current state.
6533       (gnus-summary-save-process-mark)
6534       ;; Return the list.
6535       (reverse gnus-newsgroup-processable))
6536      (t
6537       ;; Just return the current article.
6538       (list (gnus-summary-article-number))))))
6539
6540 (defmacro gnus-summary-iterate (arg &rest forms)
6541   "Iterate over the process/prefixed articles and do FORMS.
6542 ARG is the interactive prefix given to the command.  FORMS will be
6543 executed with point over the summary line of the articles."
6544   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6545     `(let ((,articles (gnus-summary-work-articles ,arg)))
6546        (while ,articles
6547          (gnus-summary-goto-subject (car ,articles))
6548          ,@forms
6549          (pop ,articles)))))
6550
6551 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6552 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6553
6554 (defun gnus-summary-save-process-mark ()
6555   "Push the current set of process marked articles on the stack."
6556   (interactive)
6557   (push (copy-sequence gnus-newsgroup-processable)
6558         gnus-newsgroup-process-stack))
6559
6560 (defun gnus-summary-kill-process-mark ()
6561   "Push the current set of process marked articles on the stack and unmark."
6562   (interactive)
6563   (gnus-summary-save-process-mark)
6564   (gnus-summary-unmark-all-processable))
6565
6566 (defun gnus-summary-yank-process-mark ()
6567   "Pop the last process mark state off the stack and restore it."
6568   (interactive)
6569   (unless gnus-newsgroup-process-stack
6570     (error "Empty mark stack"))
6571   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6572
6573 (defun gnus-summary-process-mark-set (set)
6574   "Make SET into the current process marked articles."
6575   (gnus-summary-unmark-all-processable)
6576   (mapc 'gnus-summary-set-process-mark set))
6577
6578 ;;; Searching and stuff
6579
6580 (defun gnus-summary-search-group (&optional backward use-level)
6581   "Search for next unread newsgroup.
6582 If optional argument BACKWARD is non-nil, search backward instead."
6583   (with-current-buffer gnus-group-buffer
6584     (when (gnus-group-search-forward
6585            backward nil (if use-level (gnus-group-group-level) nil))
6586       (gnus-group-group-name))))
6587
6588 (defun gnus-summary-best-group (&optional exclude-group)
6589   "Find the name of the best unread group.
6590 If EXCLUDE-GROUP, do not go to this group."
6591   (with-current-buffer gnus-group-buffer
6592     (save-excursion
6593       (gnus-group-best-unread-group exclude-group))))
6594
6595 (defun gnus-summary-find-next (&optional unread article backward)
6596   (if backward
6597       (gnus-summary-find-prev unread article)
6598     (let* ((dummy (gnus-summary-article-intangible-p))
6599            (article (or article (gnus-summary-article-number)))
6600            (data (gnus-data-find-list article))
6601            result)
6602       (when (and (not dummy)
6603                  (or (not gnus-summary-check-current)
6604                      (not unread)
6605                      (not (gnus-data-unread-p (car data)))))
6606         (setq data (cdr data)))
6607       (when (setq result
6608                   (if unread
6609                       (progn
6610                         (while data
6611                           (unless (memq (gnus-data-number (car data))
6612                                         (cond
6613                                          ((eq gnus-auto-goto-ignores
6614                                               'always-undownloaded)
6615                                           gnus-newsgroup-undownloaded)
6616                                          (gnus-plugged
6617                                           nil)
6618                                          ((eq gnus-auto-goto-ignores
6619                                               'unfetched)
6620                                           gnus-newsgroup-unfetched)
6621                                          ((eq gnus-auto-goto-ignores
6622                                               'undownloaded)
6623                                           gnus-newsgroup-undownloaded)))
6624                             (when (gnus-data-unread-p (car data))
6625                               (setq result (car data)
6626                                     data nil)))
6627                           (setq data (cdr data)))
6628                         result)
6629                     (car data)))
6630         (goto-char (gnus-data-pos result))
6631         (gnus-data-number result)))))
6632
6633 (defun gnus-summary-find-prev (&optional unread article)
6634   (let* ((eobp (eobp))
6635          (article (or article (gnus-summary-article-number)))
6636          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6637          result)
6638     (when (and (not eobp)
6639                (or (not gnus-summary-check-current)
6640                    (not unread)
6641                    (not (gnus-data-unread-p (car data)))))
6642       (setq data (cdr data)))
6643     (when (setq result
6644                 (if unread
6645                     (progn
6646                       (while data
6647                         (unless (memq (gnus-data-number (car data))
6648                                       (cond
6649                                        ((eq gnus-auto-goto-ignores
6650                                             'always-undownloaded)
6651                                         gnus-newsgroup-undownloaded)
6652                                        (gnus-plugged
6653                                         nil)
6654                                        ((eq gnus-auto-goto-ignores
6655                                             'unfetched)
6656                                         gnus-newsgroup-unfetched)
6657                                        ((eq gnus-auto-goto-ignores
6658                                             'undownloaded)
6659                                         gnus-newsgroup-undownloaded)))
6660                           (when (gnus-data-unread-p (car data))
6661                             (setq result (car data)
6662                                   data nil)))
6663                         (setq data (cdr data)))
6664                       result)
6665                   (car data)))
6666       (goto-char (gnus-data-pos result))
6667       (gnus-data-number result))))
6668
6669 (defun gnus-summary-find-subject (subject &optional unread backward article)
6670   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6671          (article (or article (gnus-summary-article-number)))
6672          (articles (gnus-data-list backward))
6673          (arts (gnus-data-find-list article articles))
6674          result)
6675     (when (or (not gnus-summary-check-current)
6676               (not unread)
6677               (not (gnus-data-unread-p (car arts))))
6678       (setq arts (cdr arts)))
6679     (while arts
6680       (and (or (not unread)
6681                (gnus-data-unread-p (car arts)))
6682            (vectorp (gnus-data-header (car arts)))
6683            (gnus-subject-equal
6684             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6685            (setq result (car arts)
6686                  arts nil))
6687       (setq arts (cdr arts)))
6688     (and result
6689          (goto-char (gnus-data-pos result))
6690          (gnus-data-number result))))
6691
6692 (defun gnus-summary-search-forward (&optional unread subject backward)
6693   "Search forward for an article.
6694 If UNREAD, look for unread articles.  If SUBJECT, look for
6695 articles with that subject.  If BACKWARD, search backward instead."
6696   (cond (subject (gnus-summary-find-subject subject unread backward))
6697         (backward (gnus-summary-find-prev unread))
6698         (t (gnus-summary-find-next unread))))
6699
6700 (defun gnus-recenter (&optional n)
6701   "Center point in window and redisplay frame.
6702 Also do horizontal recentering."
6703   (interactive "P")
6704   (when (and gnus-auto-center-summary
6705              (not (eq gnus-auto-center-summary 'vertical)))
6706     (gnus-horizontal-recenter))
6707   (recenter n))
6708
6709 (defun gnus-summary-recenter ()
6710   "Center point in the summary window.
6711 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6712 displayed, no centering will be performed."
6713   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6714   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6715   (interactive)
6716   ;; The user has to want it.
6717   (when gnus-auto-center-summary
6718     (let* ((top (cond ((< (window-height) 4) 0)
6719                       ((< (window-height) 7) 1)
6720                       (t (if (numberp gnus-auto-center-summary)
6721                              gnus-auto-center-summary
6722                            (/ (1- (window-height)) 2)))))
6723            (height (1- (window-height)))
6724            (bottom (save-excursion (goto-char (point-max))
6725                                    (forward-line (- height))
6726                                    (point)))
6727            (window (get-buffer-window (current-buffer))))
6728       (when (get-buffer-window gnus-article-buffer)
6729         ;; Only do recentering when the article buffer is displayed,
6730         ;; Set the window start to either `bottom', which is the biggest
6731         ;; possible valid number, or the second line from the top,
6732         ;; whichever is the least.
6733         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6734           (if (> bottom top-pos)
6735               ;; Keep the second line from the top visible
6736               (set-window-start window top-pos)
6737             ;; Try to keep the bottom line visible; if it's partially
6738             ;; obscured, either scroll one more line to make it fully
6739             ;; visible, or revert to using TOP-POS.
6740             (save-excursion
6741               (goto-char (point-max))
6742               (forward-line -1)
6743               (let ((last-line-start (point)))
6744                 (goto-char bottom)
6745                 (set-window-start window (point) t)
6746                 (when (not (pos-visible-in-window-p last-line-start window))
6747                   (forward-line 1)
6748                   (set-window-start window (min (point) top-pos) t)))))))
6749       ;; Do horizontal recentering while we're at it.
6750       (when (and (get-buffer-window (current-buffer) t)
6751                  (not (eq gnus-auto-center-summary 'vertical)))
6752         (let ((selected (selected-window)))
6753           (select-window (get-buffer-window (current-buffer) t))
6754           (gnus-summary-position-point)
6755           (gnus-horizontal-recenter)
6756           (select-window selected))))))
6757
6758 (defun gnus-summary-jump-to-group (newsgroup)
6759   "Move point to NEWSGROUP in group mode buffer."
6760   ;; Keep update point of group mode buffer if visible.
6761   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6762       (save-window-excursion
6763         ;; Take care of tree window mode.
6764         (when (get-buffer-window gnus-group-buffer)
6765           (pop-to-buffer gnus-group-buffer))
6766         (gnus-group-jump-to-group newsgroup))
6767     (save-excursion
6768       ;; Take care of tree window mode.
6769       (if (get-buffer-window gnus-group-buffer 0)
6770           (pop-to-buffer gnus-group-buffer)
6771         (set-buffer gnus-group-buffer))
6772       (gnus-group-jump-to-group newsgroup))))
6773
6774 ;; This function returns a list of article numbers based on the
6775 ;; difference between the ranges of read articles in this group and
6776 ;; the range of active articles.
6777 (defun gnus-list-of-unread-articles (group)
6778   (let* ((read (gnus-info-read (gnus-get-info group)))
6779          (active (or (gnus-active group) (gnus-activate-group group)))
6780          (last (or (cdr active)
6781                    (error "Group %s couldn't be activated " group)))
6782          (bottom (if gnus-newsgroup-maximum-articles
6783                      (max (car active)
6784                           (- last gnus-newsgroup-maximum-articles -1))
6785                    (car active)))
6786          first nlast unread)
6787     ;; If none are read, then all are unread.
6788     (if (not read)
6789         (setq first bottom)
6790       ;; If the range of read articles is a single range, then the
6791       ;; first unread article is the article after the last read
6792       ;; article.  Sounds logical, doesn't it?
6793       (if (and (not (listp (cdr read)))
6794                (or (< (car read) bottom)
6795                    (progn (setq read (list read))
6796                           nil)))
6797           (setq first (max bottom (1+ (cdr read))))
6798         ;; `read' is a list of ranges.
6799         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6800                                   (caar read)))
6801                   1)
6802           (setq first bottom))
6803         (while read
6804           (when first
6805             (while (< first nlast)
6806               (setq unread (cons first unread)
6807                     first (1+ first))))
6808           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6809           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6810           (setq read (cdr read)))))
6811     ;; And add the last unread articles.
6812     (while (<= first last)
6813       (setq unread (cons first unread)
6814             first (1+ first)))
6815     ;; Return the list of unread articles.
6816     (delq 0 (nreverse unread))))
6817
6818 (defun gnus-list-of-read-articles (group)
6819   "Return a list of unread, unticked and non-dormant articles."
6820   (let* ((info (gnus-get-info group))
6821          (marked (gnus-info-marks info))
6822          (active (gnus-active group)))
6823     (and info active
6824          (gnus-list-range-difference
6825           (gnus-list-range-difference
6826            (gnus-sorted-complement
6827             (gnus-uncompress-range
6828              (if gnus-newsgroup-maximum-articles
6829                  (cons (max (car active)
6830                             (- (cdr active)
6831                                gnus-newsgroup-maximum-articles
6832                                -1))
6833                        (cdr active))
6834                active))
6835             (gnus-list-of-unread-articles group))
6836            (cdr (assq 'dormant marked)))
6837           (cdr (assq 'tick marked))))))
6838
6839 ;; This function returns a sequence of article numbers based on the
6840 ;; difference between the ranges of read articles in this group and
6841 ;; the range of active articles.
6842 (defun gnus-sequence-of-unread-articles (group)
6843   (let* ((read (gnus-info-read (gnus-get-info group)))
6844          (active (or (gnus-active group) (gnus-activate-group group)))
6845          (last (cdr active))
6846          (bottom (if gnus-newsgroup-maximum-articles
6847                      (max (car active)
6848                           (- last gnus-newsgroup-maximum-articles -1))
6849                    (car active)))
6850          first nlast unread)
6851     ;; If none are read, then all are unread.
6852     (if (not read)
6853         (setq first bottom)
6854       ;; If the range of read articles is a single range, then the
6855       ;; first unread article is the article after the last read
6856       ;; article.  Sounds logical, doesn't it?
6857       (if (and (not (listp (cdr read)))
6858                (or (< (car read) bottom)
6859                    (progn (setq read (list read))
6860                           nil)))
6861           (setq first (max bottom (1+ (cdr read))))
6862         ;; `read' is a list of ranges.
6863         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6864                                   (caar read)))
6865                   1)
6866           (setq first bottom))
6867         (while read
6868           (when first
6869             (push (cons first nlast) unread))
6870           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6871           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6872           (setq read (cdr read)))))
6873     ;; And add the last unread articles.
6874     (cond ((not (and first last))
6875            nil)
6876           ((< first last)
6877            (push (cons first last) unread))
6878           ((= first last)
6879            (push first unread)))
6880     ;; Return the sequence of unread articles.
6881     (delq 0 (nreverse unread))))
6882
6883 ;; Various summary commands
6884
6885 (defun gnus-summary-select-article-buffer ()
6886   "Reconfigure windows to show article buffer."
6887   (interactive)
6888   (if (not (gnus-buffer-live-p gnus-article-buffer))
6889       (error "There is no article buffer for this summary buffer")
6890     (gnus-configure-windows 'article)
6891     (select-window (get-buffer-window gnus-article-buffer))))
6892
6893 (defun gnus-summary-universal-argument (arg)
6894   "Perform any operation on all articles that are process/prefixed."
6895   (interactive "P")
6896   (let ((articles (gnus-summary-work-articles arg))
6897         func article)
6898     (if (eq
6899          (setq
6900           func
6901           (key-binding
6902            (read-key-sequence
6903             (substitute-command-keys
6904              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6905          'undefined)
6906         (gnus-error 1 "Undefined key")
6907       (save-excursion
6908         (while articles
6909           (gnus-summary-goto-subject (setq article (pop articles)))
6910           (let (gnus-newsgroup-processable)
6911             (command-execute func))
6912           (gnus-summary-remove-process-mark article)))))
6913   (gnus-summary-position-point))
6914
6915 (defun gnus-summary-toggle-truncation (&optional arg)
6916   "Toggle truncation of summary lines.
6917 With ARG, turn line truncation on if ARG is positive."
6918   (interactive "P")
6919   (setq truncate-lines
6920         (if (null arg) (not truncate-lines)
6921           (> (prefix-numeric-value arg) 0)))
6922   (redraw-display))
6923
6924 (defun gnus-summary-find-for-reselect ()
6925   "Return the number of an article to stay on across a reselect.
6926 The current article is considered, then following articles, then previous
6927 articles.  An article is sought which is not cancelled and isn't a temporary
6928 insertion from another group.  If there's no such then return a dummy 0."
6929   (let (found)
6930     (dolist (rev '(nil t))
6931       (unless found      ; don't demand the reverse list if we don't need it
6932         (let ((data (gnus-data-find-list
6933                      (gnus-summary-article-number) (gnus-data-list rev))))
6934           (while (and data (not found))
6935             (if (and (< 0 (gnus-data-number (car data)))
6936                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6937                 (setq found (gnus-data-number (car data))))
6938             (setq data (cdr data))))))
6939     (or found 0)))
6940
6941 (defun gnus-summary-reselect-current-group (&optional all rescan)
6942   "Exit and then reselect the current newsgroup.
6943 The prefix argument ALL means to select all articles."
6944   (interactive "P")
6945   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6946     (error "Ephemeral groups can't be reselected"))
6947   (let ((current-subject (gnus-summary-find-for-reselect))
6948         (group gnus-newsgroup-name))
6949     (setq gnus-newsgroup-begin nil)
6950     (gnus-summary-exit nil 'leave-hidden)
6951     ;; We have to adjust the point of group mode buffer because
6952     ;; point was moved to the next unread newsgroup by exiting.
6953     (gnus-summary-jump-to-group group)
6954     (when rescan
6955       (save-excursion
6956         (gnus-group-get-new-news-this-group 1)))
6957     (gnus-group-read-group all t)
6958     (gnus-summary-goto-subject current-subject nil t)))
6959
6960 (defun gnus-summary-rescan-group (&optional all)
6961   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6962   (interactive "P")
6963   (gnus-summary-reselect-current-group all t))
6964
6965 (defun gnus-summary-update-info (&optional non-destructive)
6966   (save-excursion
6967     (let ((group gnus-newsgroup-name))
6968       (when group
6969         (when gnus-newsgroup-kill-headers
6970           (setq gnus-newsgroup-killed
6971                 (gnus-compress-sequence
6972                  (gnus-sorted-union
6973                   (gnus-list-range-intersection
6974                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6975                   gnus-newsgroup-unreads)
6976                  t)))
6977         (unless (listp (cdr gnus-newsgroup-killed))
6978           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6979         (let ((headers gnus-newsgroup-headers))
6980           ;; Set the new ranges of read articles.
6981           (with-current-buffer gnus-group-buffer
6982             (gnus-undo-force-boundary))
6983           (gnus-update-read-articles
6984            group (gnus-sorted-union
6985                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6986           ;; Set the current article marks.
6987           (let ((gnus-newsgroup-scored
6988                  (if (and (not gnus-save-score)
6989                           (not non-destructive))
6990                      nil
6991                    gnus-newsgroup-scored)))
6992             (save-excursion
6993               (gnus-update-marks)))
6994           ;; Do the cross-ref thing.
6995           (when gnus-use-cross-reference
6996             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6997           ;; Do not switch windows but change the buffer to work.
6998           (set-buffer gnus-group-buffer)
6999           (unless (gnus-ephemeral-group-p group)
7000             (gnus-group-update-group group)))))))
7001
7002 (defun gnus-summary-save-newsrc (&optional force)
7003   "Save the current number of read/marked articles in the dribble buffer.
7004 The dribble buffer will then be saved.
7005 If FORCE (the prefix), also save the .newsrc file(s)."
7006   (interactive "P")
7007   (gnus-summary-update-info t)
7008   (if force
7009       (gnus-save-newsrc-file)
7010     (gnus-dribble-save)))
7011
7012 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7013
7014 (defun gnus-summary-exit (&optional temporary leave-hidden)
7015   "Exit reading current newsgroup, and then return to group selection mode.
7016 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7017   (interactive)
7018   (gnus-set-global-variables)
7019   (when (gnus-buffer-live-p gnus-article-buffer)
7020     (with-current-buffer gnus-article-buffer
7021       (mm-destroy-parts gnus-article-mime-handles)
7022       ;; Set it to nil for safety reason.
7023       (setq gnus-article-mime-handle-alist nil)
7024       (setq gnus-article-mime-handles nil)))
7025   (gnus-kill-save-kill-buffer)
7026   (gnus-async-halt-prefetch)
7027   (let* ((group gnus-newsgroup-name)
7028          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7029          (gnus-group-is-exiting-p t)
7030          (mode major-mode)
7031          (group-point nil)
7032          (buf (current-buffer)))
7033     (unless quit-config
7034       ;; Do adaptive scoring, and possibly save score files.
7035       (when gnus-newsgroup-adaptive
7036         (gnus-score-adaptive))
7037       (when gnus-use-scoring
7038         (gnus-score-save)))
7039     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7040     ;; If we have several article buffers, we kill them at exit.
7041     (unless gnus-single-article-buffer
7042       (when (gnus-buffer-live-p gnus-article-buffer)
7043         (with-current-buffer gnus-article-buffer
7044           ;; Don't kill sticky article buffers
7045           (unless (eq major-mode 'gnus-sticky-article-mode)
7046             (gnus-kill-buffer gnus-article-buffer)
7047             (setq gnus-article-current nil))))
7048       (gnus-kill-buffer gnus-original-article-buffer))
7049     (when gnus-use-cache
7050       (gnus-cache-possibly-remove-articles)
7051       (gnus-cache-save-buffers))
7052     (gnus-async-prefetch-remove-group group)
7053     (when gnus-suppress-duplicates
7054       (gnus-dup-enter-articles))
7055     (when gnus-use-trees
7056       (gnus-tree-close group))
7057     (when gnus-use-cache
7058       (gnus-cache-write-active))
7059     ;; Remove entries for this group.
7060     (nnmail-purge-split-history (gnus-group-real-name group))
7061     ;; Make all changes in this group permanent.
7062     (unless quit-config
7063       (gnus-run-hooks 'gnus-exit-group-hook)
7064       (gnus-summary-update-info))
7065     (gnus-close-group group)
7066     ;; Make sure where we were, and go to next newsgroup.
7067     (set-buffer gnus-group-buffer)
7068     (unless quit-config
7069       (gnus-group-jump-to-group group))
7070     (gnus-run-hooks 'gnus-summary-exit-hook)
7071     (unless (or quit-config
7072                 (not gnus-summary-next-group-on-exit)
7073                 ;; If this group has disappeared from the summary
7074                 ;; buffer, don't skip forwards.
7075                 (not (string= group (gnus-group-group-name))))
7076       (gnus-group-next-unread-group 1))
7077     (setq group-point (point))
7078     (if temporary
7079         nil                             ;Nothing to do.
7080       (set-buffer buf)
7081       (if (not gnus-kill-summary-on-exit)
7082           (progn
7083             (gnus-deaden-summary)
7084             (setq mode nil))
7085         ;; We set all buffer-local variables to nil.  It is unclear why
7086         ;; this is needed, but if we don't, buffer-local variables are
7087         ;; not garbage-collected, it seems.  This would the lead to en
7088         ;; ever-growing Emacs.
7089         (gnus-summary-clear-local-variables)
7090         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7091           (gnus-summary-clear-local-variables))
7092         (when (get-buffer gnus-article-buffer)
7093           (bury-buffer gnus-article-buffer))
7094         ;; Return to group mode buffer.
7095         (when (eq mode 'gnus-summary-mode)
7096           (gnus-kill-buffer buf)))
7097       (setq gnus-current-select-method gnus-select-method)
7098       (set-buffer gnus-group-buffer)
7099       (if quit-config
7100           (gnus-handle-ephemeral-exit quit-config)
7101         (goto-char group-point)
7102         ;; If gnus-group-buffer is already displayed, make sure we also move
7103         ;; the cursor in the window that displays it.
7104         (let ((win (get-buffer-window (current-buffer) 0)))
7105           (if win (set-window-point win (point))))
7106         (unless leave-hidden
7107           (gnus-configure-windows 'group 'force)))
7108       ;; Clear the current group name.
7109       (unless quit-config
7110         (setq gnus-newsgroup-name nil)))))
7111
7112 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7113 (defun gnus-summary-exit-no-update (&optional no-questions)
7114   "Quit reading current newsgroup without updating read article info."
7115   (interactive)
7116   (let* ((group gnus-newsgroup-name)
7117          (gnus-group-is-exiting-p t)
7118          (gnus-group-is-exiting-without-update-p t)
7119          (quit-config (gnus-group-quit-config group)))
7120     (when (or no-questions
7121               gnus-expert-user
7122               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7123       (gnus-async-halt-prefetch)
7124       (run-hooks 'gnus-summary-prepare-exit-hook)
7125       (when (gnus-buffer-live-p gnus-article-buffer)
7126         (with-current-buffer gnus-article-buffer
7127           (mm-destroy-parts gnus-article-mime-handles)
7128           ;; Set it to nil for safety reason.
7129           (setq gnus-article-mime-handle-alist nil)
7130           (setq gnus-article-mime-handles nil)))
7131       ;; If we have several article buffers, we kill them at exit.
7132       (unless gnus-single-article-buffer
7133         (gnus-kill-buffer gnus-article-buffer)
7134         (gnus-kill-buffer gnus-original-article-buffer)
7135         (setq gnus-article-current nil))
7136       (if (not gnus-kill-summary-on-exit)
7137           (gnus-deaden-summary)
7138         (gnus-close-group group)
7139         (gnus-summary-clear-local-variables)
7140         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7141           (gnus-summary-clear-local-variables))
7142         (gnus-kill-buffer gnus-summary-buffer))
7143       (unless gnus-single-article-buffer
7144         (setq gnus-article-current nil))
7145       (when gnus-use-trees
7146         (gnus-tree-close group))
7147       (gnus-async-prefetch-remove-group group)
7148       (when (get-buffer gnus-article-buffer)
7149         (bury-buffer gnus-article-buffer))
7150       ;; Return to the group buffer.
7151       (gnus-configure-windows 'group 'force)
7152       ;; Clear the current group name.
7153       (setq gnus-newsgroup-name nil)
7154       (unless (gnus-ephemeral-group-p group)
7155         (gnus-group-update-group group))
7156       (when (equal (gnus-group-group-name) group)
7157         (gnus-group-next-unread-group 1))
7158       (when quit-config
7159         (gnus-handle-ephemeral-exit quit-config)))))
7160
7161 (defun gnus-handle-ephemeral-exit (quit-config)
7162   "Handle movement when leaving an ephemeral group.
7163 The state which existed when entering the ephemeral is reset."
7164   (if (not (buffer-name (car quit-config)))
7165       (gnus-configure-windows 'group 'force)
7166     (set-buffer (car quit-config))
7167     (cond ((eq major-mode 'gnus-summary-mode)
7168            (gnus-set-global-variables))
7169           ((eq major-mode 'gnus-article-mode)
7170            (save-current-buffer
7171              ;; The `gnus-summary-buffer' variable may point
7172              ;; to the old summary buffer when using a single
7173              ;; article buffer.
7174              (unless (gnus-buffer-live-p gnus-summary-buffer)
7175                (set-buffer gnus-group-buffer))
7176              (set-buffer gnus-summary-buffer)
7177              (gnus-set-global-variables))))
7178     (if (or (eq (cdr quit-config) 'article)
7179             (eq (cdr quit-config) 'pick))
7180         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7181             (gnus-configure-windows 'pick 'force)
7182           (gnus-configure-windows (cdr quit-config) 'force))
7183       (gnus-configure-windows (cdr quit-config) 'force))
7184     (when (eq major-mode 'gnus-summary-mode)
7185       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7186                                                      next-unread-noselect))
7187           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7188                                   'next-noselect)
7189                               (gnus-summary-next-subject 1 nil t))
7190                              ((eq gnus-auto-select-on-ephemeral-exit
7191                                   'next-unread-noselect)
7192                               (gnus-summary-next-subject 1 t t))))
7193             ;; Hide the article buffer which displays the article different
7194             ;; from the one that the cursor points to in the summary buffer.
7195             (gnus-configure-windows 'summary 'force))
7196         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7197                (gnus-summary-next-subject 1))
7198               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7199                (gnus-summary-next-subject 1 t))))
7200       (gnus-summary-recenter)
7201       (gnus-summary-position-point))))
7202
7203 ;;; Dead summaries.
7204
7205 (defvar gnus-dead-summary-mode-map nil)
7206
7207 (unless gnus-dead-summary-mode-map
7208   (setq gnus-dead-summary-mode-map (make-keymap))
7209   (suppress-keymap gnus-dead-summary-mode-map)
7210   (substitute-key-definition
7211    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7212   (dolist (key '("\C-d" "\r" "\177" [delete]))
7213     (define-key gnus-dead-summary-mode-map
7214       key 'gnus-summary-wake-up-the-dead))
7215   (dolist (key '("q" "Q"))
7216     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7217
7218 (defvar gnus-dead-summary-mode nil
7219   "Minor mode for Gnus summary buffers.")
7220
7221 (defun gnus-dead-summary-mode (&optional arg)
7222   "Minor mode for Gnus summary buffers."
7223   (interactive "P")
7224   (when (eq major-mode 'gnus-summary-mode)
7225     (make-local-variable 'gnus-dead-summary-mode)
7226     (setq gnus-dead-summary-mode
7227           (if (null arg) (not gnus-dead-summary-mode)
7228             (> (prefix-numeric-value arg) 0)))
7229     (when gnus-dead-summary-mode
7230       (add-minor-mode
7231        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7232
7233 (defun gnus-deaden-summary ()
7234   "Make the current summary buffer into a dead summary buffer."
7235   ;; Kill any previous dead summary buffer.
7236   (when (and gnus-dead-summary
7237              (buffer-name gnus-dead-summary))
7238     (with-current-buffer gnus-dead-summary
7239       (when gnus-dead-summary-mode
7240         (kill-buffer (current-buffer)))))
7241   ;; Make this the current dead summary.
7242   (setq gnus-dead-summary (current-buffer))
7243   (gnus-dead-summary-mode 1)
7244   (let ((name (buffer-name)))
7245     (when (string-match "Summary" name)
7246       (rename-buffer
7247        (concat (substring name 0 (match-beginning 0)) "Dead "
7248                (substring name (match-beginning 0)))
7249        t)
7250       (bury-buffer))))
7251
7252 (defun gnus-kill-or-deaden-summary (buffer)
7253   "Kill or deaden the summary BUFFER."
7254   (save-excursion
7255     (when (and (buffer-name buffer)
7256                (not gnus-single-article-buffer))
7257       (with-current-buffer buffer
7258         (gnus-kill-buffer gnus-article-buffer)
7259         (gnus-kill-buffer gnus-original-article-buffer)))
7260     (cond
7261      ;; Kill the buffer.
7262      (gnus-kill-summary-on-exit
7263       (when (and gnus-use-trees
7264                  (gnus-buffer-exists-p buffer))
7265         (with-current-buffer buffer
7266           (gnus-tree-close gnus-newsgroup-name)))
7267       (gnus-kill-buffer buffer))
7268      ;; Deaden the buffer.
7269      ((gnus-buffer-exists-p buffer)
7270       (with-current-buffer buffer
7271         (gnus-deaden-summary))))))
7272
7273 (defun gnus-summary-wake-up-the-dead (&rest args)
7274   "Wake up the dead summary buffer."
7275   (interactive)
7276   (gnus-dead-summary-mode -1)
7277   (let ((name (buffer-name)))
7278     (when (string-match "Dead " name)
7279       (rename-buffer
7280        (concat (substring name 0 (match-beginning 0))
7281                (substring name (match-end 0)))
7282        t)))
7283   (gnus-message 3 "This dead summary is now alive again"))
7284
7285 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7286 (defun gnus-summary-fetch-faq (&optional faq-dir)
7287   "Fetch the FAQ for the current group.
7288 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7289 in."
7290   (interactive
7291    (list
7292     (when current-prefix-arg
7293       (completing-read
7294        "FAQ dir: " (and (listp gnus-group-faq-directory)
7295                         (mapcar 'list
7296                                 gnus-group-faq-directory))))))
7297   (let (gnus-faq-buffer)
7298     (when (setq gnus-faq-buffer
7299                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7300       (gnus-configure-windows 'summary-faq))))
7301
7302 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7303 (defun gnus-summary-describe-group (&optional force)
7304   "Describe the current newsgroup."
7305   (interactive "P")
7306   (gnus-group-describe-group force gnus-newsgroup-name))
7307
7308 (defun gnus-summary-describe-briefly ()
7309   "Describe summary mode commands briefly."
7310   (interactive)
7311   (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")))
7312
7313 ;; Walking around group mode buffer from summary mode.
7314
7315 (defun gnus-summary-next-group (&optional no-article target-group backward)
7316   "Exit current newsgroup and then select next unread newsgroup.
7317 If prefix argument NO-ARTICLE is non-nil, no article is selected
7318 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7319 previous group instead."
7320   (interactive "P")
7321   ;; Stop pre-fetching.
7322   (gnus-async-halt-prefetch)
7323   (let ((current-group gnus-newsgroup-name)
7324         (current-buffer (current-buffer))
7325         entered)
7326     ;; First we semi-exit this group to update Xrefs and all variables.
7327     ;; We can't do a real exit, because the window conf must remain
7328     ;; the same in case the user is prompted for info, and we don't
7329     ;; want the window conf to change before that...
7330     (gnus-summary-exit t)
7331     (while (not entered)
7332       ;; Then we find what group we are supposed to enter.
7333       (set-buffer gnus-group-buffer)
7334       (gnus-group-jump-to-group current-group)
7335       (setq target-group
7336             (or target-group
7337                 (if (eq gnus-keep-same-level 'best)
7338                     (gnus-summary-best-group gnus-newsgroup-name)
7339                   (gnus-summary-search-group backward gnus-keep-same-level))))
7340       (if (not target-group)
7341           ;; There are no further groups, so we return to the group
7342           ;; buffer.
7343           (progn
7344             (gnus-message 5 "Returning to the group buffer")
7345             (setq entered t)
7346             (when (gnus-buffer-live-p current-buffer)
7347               (set-buffer current-buffer)
7348               (gnus-summary-exit))
7349             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7350         ;; We try to enter the target group.
7351         (gnus-group-jump-to-group target-group)
7352         (let ((unreads (gnus-group-group-unread)))
7353           (if (and (or (eq t unreads)
7354                        (and unreads (not (zerop unreads))))
7355                    (gnus-summary-read-group
7356                     target-group nil no-article
7357                     (and (buffer-name current-buffer) current-buffer)
7358                     nil backward))
7359               (setq entered t)
7360             (setq current-group target-group
7361                   target-group nil)))))))
7362
7363 (defun gnus-summary-prev-group (&optional no-article)
7364   "Exit current newsgroup and then select previous unread newsgroup.
7365 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7366   (interactive "P")
7367   (gnus-summary-next-group no-article nil t))
7368
7369 ;; Walking around summary lines.
7370
7371 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7372   "Go to the first subject satisfying any non-nil constraint.
7373 If UNREAD is non-nil, the article should be unread.
7374 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7375 If UNSEEN is non-nil, the article should be unseen.
7376 Returns the article selected or nil if there are no matching articles."
7377   (interactive "P")
7378   (cond
7379    ;; Empty summary.
7380    ((null gnus-newsgroup-data)
7381     (gnus-message 3 "No articles in the group")
7382     nil)
7383    ;; Pick the first article.
7384    ((not (or unread undownloaded unseen))
7385     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7386     (gnus-data-number (car gnus-newsgroup-data)))
7387    ;; Find the first unread article.
7388    (t
7389     (let ((data gnus-newsgroup-data))
7390       (while (and data
7391                   (let ((num (gnus-data-number (car data))))
7392                     (or (memq num gnus-newsgroup-unfetched)
7393                         (not (or (and unread
7394                                       (memq num gnus-newsgroup-unreads))
7395                                  (and undownloaded
7396                                       (memq num gnus-newsgroup-undownloaded))
7397                                  (and unseen
7398                                       (memq num gnus-newsgroup-unseen)))))))
7399         (setq data (cdr data)))
7400       (prog1
7401           (if data
7402               (progn
7403                 (goto-char (gnus-data-pos (car data)))
7404                 (gnus-data-number (car data)))
7405             (gnus-message 3 "No more%s articles"
7406                           (let* ((r (when unread " unread"))
7407                                  (d (when undownloaded " undownloaded"))
7408                                  (s (when unseen " unseen"))
7409                                  (l (delq nil (list r d s))))
7410                             (cond ((= 3 (length l))
7411                                    (concat r "," d ", or" s))
7412                                   ((= 2 (length l))
7413                                    (concat (car l) ", or" (cadr l)))
7414                                   ((= 1 (length l))
7415                                    (car l))
7416                                   (t
7417                                    ""))))
7418             nil
7419             )
7420         (gnus-summary-position-point))))))
7421
7422 (defun gnus-summary-next-subject (n &optional unread dont-display)
7423   "Go to next N'th summary line.
7424 If N is negative, go to the previous N'th subject line.
7425 If UNREAD is non-nil, only unread articles are selected.
7426 The difference between N and the actual number of steps taken is
7427 returned."
7428   (interactive "p")
7429   (let ((backward (< n 0))
7430         (n (abs n)))
7431     (while (and (> n 0)
7432                 (if backward
7433                     (gnus-summary-find-prev unread)
7434                   (gnus-summary-find-next unread)))
7435       (unless (zerop (setq n (1- n)))
7436         (gnus-summary-show-thread)))
7437     (when (/= 0 n)
7438       (gnus-message 7 "No more%s articles"
7439                     (if unread " unread" "")))
7440     (unless dont-display
7441       (gnus-summary-recenter)
7442       (gnus-summary-position-point))
7443     n))
7444
7445 (defun gnus-summary-next-unread-subject (n)
7446   "Go to next N'th unread summary line."
7447   (interactive "p")
7448   (gnus-summary-next-subject n t))
7449
7450 (defun gnus-summary-prev-subject (n &optional unread)
7451   "Go to previous N'th summary line.
7452 If optional argument UNREAD is non-nil, only unread article is selected."
7453   (interactive "p")
7454   (gnus-summary-next-subject (- n) unread))
7455
7456 (defun gnus-summary-prev-unread-subject (n)
7457   "Go to previous N'th unread summary line."
7458   (interactive "p")
7459   (gnus-summary-next-subject (- n) t))
7460
7461 (defun gnus-summary-goto-subjects (articles)
7462   "Insert the subject header for ARTICLES in the current buffer."
7463   (save-excursion
7464     (dolist (article articles)
7465       (gnus-summary-goto-subject article t)))
7466   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7467   (gnus-summary-position-point))
7468
7469 (defun gnus-summary-goto-subject (article &optional force silent)
7470   "Go to the subject line of ARTICLE.
7471 If FORCE, also allow jumping to articles not currently shown."
7472   (interactive "nArticle number: ")
7473   (unless (numberp article)
7474     (error "Article %s is not a number" article))
7475   (let ((b (point))
7476         (data (gnus-data-find article)))
7477     ;; We read in the article if we have to.
7478     (and (not data)
7479          force
7480          (gnus-summary-insert-subject
7481           article
7482           (if (or (numberp force) (vectorp force)) force)
7483           t)
7484          (setq data (gnus-data-find article)))
7485     (goto-char b)
7486     (if (not data)
7487         (progn
7488           (unless silent
7489             (gnus-message 3 "Can't find article %d" article))
7490           nil)
7491       (let ((pt (gnus-data-pos data)))
7492         (goto-char pt)
7493         (gnus-summary-set-article-display-arrow pt))
7494       (gnus-summary-position-point)
7495       article)))
7496
7497 ;; Walking around summary lines with displaying articles.
7498
7499 (defun gnus-summary-expand-window (&optional arg)
7500   "Make the summary buffer take up the entire Emacs frame.
7501 Given a prefix, will force an `article' buffer configuration."
7502   (interactive "P")
7503   (if arg
7504       (gnus-configure-windows 'article 'force)
7505     (gnus-configure-windows 'summary 'force)))
7506
7507 (defun gnus-summary-display-article (article &optional all-header)
7508   "Display ARTICLE in article buffer."
7509   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7510                (with-current-buffer gnus-article-buffer
7511                  (eq major-mode 'gnus-article-mode)))
7512     (gnus-article-setup-buffer))
7513   (gnus-set-global-variables)
7514   (with-current-buffer gnus-article-buffer
7515     (setq gnus-article-charset gnus-newsgroup-charset)
7516     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7517     (mm-enable-multibyte))
7518   (if (null article)
7519       nil
7520     (prog1
7521         (if gnus-summary-display-article-function
7522             (funcall gnus-summary-display-article-function article all-header)
7523           (gnus-article-prepare article all-header))
7524       (gnus-run-hooks 'gnus-select-article-hook)
7525       (when (and gnus-current-article
7526                  (not (zerop gnus-current-article)))
7527         (gnus-summary-goto-subject gnus-current-article))
7528       (gnus-summary-recenter)
7529       (when (and gnus-use-trees gnus-show-threads)
7530         (gnus-possibly-generate-tree article)
7531         (gnus-highlight-selected-tree article))
7532       ;; Successfully display article.
7533       (gnus-article-set-window-start
7534        (cdr (assq article gnus-newsgroup-bookmarks))))))
7535
7536 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7537   "Select the current article.
7538 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7539 non-nil, the article will be re-fetched even if it already present in
7540 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7541 be displayed."
7542   ;; Make sure we are in the summary buffer to work around bbdb bug.
7543   (unless (eq major-mode 'gnus-summary-mode)
7544     (set-buffer gnus-summary-buffer))
7545   (let ((article (or article (gnus-summary-article-number)))
7546         (all-headers (not (not all-headers))) ;Must be t or nil.
7547         gnus-summary-display-article-function)
7548     (and (not pseudo)
7549          (gnus-summary-article-pseudo-p article)
7550          (error "This is a pseudo-article"))
7551     (with-current-buffer gnus-summary-buffer
7552       (if (or (and gnus-single-article-buffer
7553                    (or (null gnus-current-article)
7554                        (null gnus-article-current)
7555                        (null (get-buffer gnus-article-buffer))
7556                        (not (eq article (cdr gnus-article-current)))
7557                        (not (equal (car gnus-article-current)
7558                                    gnus-newsgroup-name))))
7559               (and (not gnus-single-article-buffer)
7560                    (or (null gnus-current-article)
7561                        (not (eq gnus-current-article article))))
7562               force)
7563           ;; The requested article is different from the current article.
7564           (progn
7565             (gnus-summary-display-article article all-headers)
7566             (when (gnus-buffer-live-p gnus-article-buffer)
7567               (with-current-buffer gnus-article-buffer
7568                 (if (not gnus-article-decoded-p) ;; a local variable
7569                     (mm-disable-multibyte))))
7570             (gnus-article-set-window-start
7571              (cdr (assq article gnus-newsgroup-bookmarks)))
7572             article)
7573         'old))))
7574
7575 (defun gnus-summary-force-verify-and-decrypt ()
7576   "Display buttons for signed/encrypted parts and verify/decrypt them."
7577   (interactive)
7578   (let ((mm-verify-option 'known)
7579         (mm-decrypt-option 'known)
7580         (gnus-article-emulate-mime t)
7581         (gnus-buttonized-mime-types (append (list "multipart/signed"
7582                                                   "multipart/encrypted")
7583                                             gnus-buttonized-mime-types)))
7584     (gnus-summary-select-article nil 'force)))
7585
7586 (defun gnus-summary-set-current-mark (&optional current-mark)
7587   "Obsolete function."
7588   nil)
7589
7590 (defun gnus-summary-next-article (&optional unread subject backward push)
7591   "Select the next article.
7592 If UNREAD, only unread articles are selected.
7593 If SUBJECT, only articles with SUBJECT are selected.
7594 If BACKWARD, the previous article is selected instead of the next."
7595   (interactive "P")
7596   ;; Make sure we are in the summary buffer.
7597   (unless (eq major-mode 'gnus-summary-mode)
7598     (set-buffer gnus-summary-buffer))
7599   (cond
7600    ;; Is there such an article?
7601    ((and (gnus-summary-search-forward unread subject backward)
7602          (or (gnus-summary-display-article (gnus-summary-article-number))
7603              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7604     (gnus-summary-position-point))
7605    ;; If not, we try the first unread, if that is wanted.
7606    ((and subject
7607          gnus-auto-select-same
7608          (gnus-summary-first-unread-article))
7609     (gnus-summary-position-point)
7610     (gnus-message 6 "Wrapped"))
7611    ;; Try to get next/previous article not displayed in this group.
7612    ((and gnus-auto-extend-newsgroup
7613          (not unread) (not subject))
7614     (gnus-summary-goto-article
7615      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7616      nil (count-lines (point-min) (point))))
7617    ;; Go to next/previous group.
7618    (t
7619     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7620       (gnus-summary-jump-to-group gnus-newsgroup-name))
7621     (let ((cmd last-command-char)
7622           (point
7623            (with-current-buffer gnus-group-buffer
7624              (point)))
7625           (group
7626            (if (eq gnus-keep-same-level 'best)
7627                (gnus-summary-best-group gnus-newsgroup-name)
7628              (gnus-summary-search-group backward gnus-keep-same-level))))
7629       ;; For some reason, the group window gets selected.  We change
7630       ;; it back.
7631       (select-window (get-buffer-window (current-buffer)))
7632       ;; Select next unread newsgroup automagically.
7633       (cond
7634        ((or (not gnus-auto-select-next)
7635             (not cmd))
7636         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7637        ((or (eq gnus-auto-select-next 'quietly)
7638             (and (eq gnus-auto-select-next 'slightly-quietly)
7639                  push)
7640             (and (eq gnus-auto-select-next 'almost-quietly)
7641                  (gnus-summary-last-article-p)))
7642         ;; Select quietly.
7643         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7644             (gnus-summary-exit)
7645           (gnus-message 7 "No more%s articles (%s)..."
7646                         (if unread " unread" "")
7647                         (if group (concat "selecting " group)
7648                           "exiting"))
7649           (gnus-summary-next-group nil group backward)))
7650        (t
7651         (when (gnus-key-press-event-p last-input-event)
7652           (gnus-summary-walk-group-buffer
7653            gnus-newsgroup-name cmd unread backward point))))))))
7654
7655 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7656   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7657                       (?\C-p (gnus-group-prev-unread-group 1))))
7658         (cursor-in-echo-area t)
7659         keve key group ended prompt)
7660     (with-current-buffer gnus-group-buffer
7661       (goto-char start)
7662       (setq group
7663             (if (eq gnus-keep-same-level 'best)
7664                 (gnus-summary-best-group gnus-newsgroup-name)
7665               (gnus-summary-search-group backward gnus-keep-same-level))))
7666     (while (not ended)
7667       (setq prompt
7668             (format
7669              "No more%s articles%s " (if unread " unread" "")
7670              (if (and group
7671                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7672                  (format " (Type %s for %s [%s])"
7673                          (single-key-description cmd)
7674                          (gnus-group-decoded-name group)
7675                          (gnus-group-unread group))
7676                (format " (Type %s to exit %s)"
7677                        (single-key-description cmd)
7678                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7679       ;; Confirm auto selection.
7680       (setq key (car (setq keve (gnus-read-event-char prompt)))
7681             ended t)
7682       (cond
7683        ((assq key keystrokes)
7684         (let ((obuf (current-buffer)))
7685           (switch-to-buffer gnus-group-buffer)
7686           (when group
7687             (gnus-group-jump-to-group group))
7688           (eval (cadr (assq key keystrokes)))
7689           (setq group (gnus-group-group-name))
7690           (switch-to-buffer obuf))
7691         (setq ended nil))
7692        ((equal key cmd)
7693         (if (or (not group)
7694                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7695             (gnus-summary-exit)
7696           (gnus-summary-next-group nil group backward)))
7697        (t
7698         (push (cdr keve) unread-command-events))))))
7699
7700 (defun gnus-summary-next-unread-article ()
7701   "Select unread article after current one."
7702   (interactive)
7703   (gnus-summary-next-article
7704    (or (not (eq gnus-summary-goto-unread 'never))
7705        (gnus-summary-last-article-p (gnus-summary-article-number)))
7706    (and gnus-auto-select-same
7707         (gnus-summary-article-subject))))
7708
7709 (defun gnus-summary-prev-article (&optional unread subject)
7710   "Select the article before the current one.
7711 If UNREAD is non-nil, only unread articles are selected."
7712   (interactive "P")
7713   (gnus-summary-next-article unread subject t))
7714
7715 (defun gnus-summary-prev-unread-article ()
7716   "Select unread article before current one."
7717   (interactive)
7718   (gnus-summary-prev-article
7719    (or (not (eq gnus-summary-goto-unread 'never))
7720        (gnus-summary-first-article-p (gnus-summary-article-number)))
7721    (and gnus-auto-select-same
7722         (gnus-summary-article-subject))))
7723
7724 (defun gnus-summary-next-page (&optional lines circular stop)
7725   "Show next page of the selected article.
7726 If at the end of the current article, select the next article.
7727 LINES says how many lines should be scrolled up.
7728
7729 If CIRCULAR is non-nil, go to the start of the article instead of
7730 selecting the next article when reaching the end of the current
7731 article.
7732
7733 If STOP is non-nil, just stop when reaching the end of the message.
7734
7735 Also see the variable `gnus-article-skip-boring'."
7736   (interactive "P")
7737   (setq gnus-summary-buffer (current-buffer))
7738   (gnus-set-global-variables)
7739   (let ((article (gnus-summary-article-number))
7740         (article-window (get-buffer-window gnus-article-buffer t))
7741         endp)
7742     ;; If the buffer is empty, we have no article.
7743     (unless article
7744       (error "No article to select"))
7745     (gnus-configure-windows 'article)
7746     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7747         (if (and (eq gnus-summary-goto-unread 'never)
7748                  (not (gnus-summary-last-article-p article)))
7749             (gnus-summary-next-article)
7750           (gnus-summary-next-unread-article))
7751       (if (or (null gnus-current-article)
7752               (null gnus-article-current)
7753               (/= article (cdr gnus-article-current))
7754               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7755           ;; Selected subject is different from current article's.
7756           (gnus-summary-display-article article)
7757         (when article-window
7758           (gnus-eval-in-buffer-window gnus-article-buffer
7759             (setq endp (or (gnus-article-next-page lines)
7760                            (gnus-article-only-boring-p))))
7761           (when endp
7762             (cond (stop
7763                    (gnus-message 3 "End of message"))
7764                   (circular
7765                    (gnus-summary-beginning-of-article))
7766                   (lines
7767                    (gnus-message 3 "End of message"))
7768                   ((null lines)
7769                    (if (and (eq gnus-summary-goto-unread 'never)
7770                             (not (gnus-summary-last-article-p article)))
7771                        (gnus-summary-next-article)
7772                      (gnus-summary-next-unread-article))))))))
7773     (gnus-summary-recenter)
7774     (gnus-summary-position-point)))
7775
7776 (defun gnus-summary-prev-page (&optional lines move)
7777   "Show previous page of selected article.
7778 Argument LINES specifies lines to be scrolled down.
7779 If MOVE, move to the previous unread article if point is at
7780 the beginning of the buffer."
7781   (interactive "P")
7782   (let ((article (gnus-summary-article-number))
7783         (article-window (get-buffer-window gnus-article-buffer t))
7784         endp)
7785     (gnus-configure-windows 'article)
7786     (if (or (null gnus-current-article)
7787             (null gnus-article-current)
7788             (/= article (cdr gnus-article-current))
7789             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7790         ;; Selected subject is different from current article's.
7791         (gnus-summary-display-article article)
7792       (gnus-summary-recenter)
7793       (when article-window
7794         (gnus-eval-in-buffer-window gnus-article-buffer
7795           (setq endp (gnus-article-prev-page lines)))
7796         (when (and move endp)
7797           (cond (lines
7798                  (gnus-message 3 "Beginning of message"))
7799                 ((null lines)
7800                  (if (and (eq gnus-summary-goto-unread 'never)
7801                           (not (gnus-summary-first-article-p article)))
7802                      (gnus-summary-prev-article)
7803                    (gnus-summary-prev-unread-article))))))))
7804   (gnus-summary-position-point))
7805
7806 (defun gnus-summary-prev-page-or-article (&optional lines)
7807   "Show previous page of selected article.
7808 Argument LINES specifies lines to be scrolled down.
7809 If at the beginning of the article, go to the next article."
7810   (interactive "P")
7811   (gnus-summary-prev-page lines t))
7812
7813 (defun gnus-summary-scroll-up (lines)
7814   "Scroll up (or down) one line current article.
7815 Argument LINES specifies lines to be scrolled up (or down if negative)."
7816   (interactive "p")
7817   (gnus-configure-windows 'article)
7818   (gnus-summary-show-thread)
7819   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7820     (gnus-eval-in-buffer-window gnus-article-buffer
7821       (cond ((> lines 0)
7822              (when (gnus-article-next-page lines)
7823                (gnus-message 3 "End of message")))
7824             ((< lines 0)
7825              (gnus-article-prev-page (- lines))))))
7826   (gnus-summary-recenter)
7827   (gnus-summary-position-point))
7828
7829 (defun gnus-summary-scroll-down (lines)
7830   "Scroll down (or up) one line current article.
7831 Argument LINES specifies lines to be scrolled down (or up if negative)."
7832   (interactive "p")
7833   (gnus-summary-scroll-up (- lines)))
7834
7835 (defun gnus-summary-next-same-subject ()
7836   "Select next article which has the same subject as current one."
7837   (interactive)
7838   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7839
7840 (defun gnus-summary-prev-same-subject ()
7841   "Select previous article which has the same subject as current one."
7842   (interactive)
7843   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7844
7845 (defun gnus-summary-next-unread-same-subject ()
7846   "Select next unread article which has the same subject as current one."
7847   (interactive)
7848   (gnus-summary-next-article t (gnus-summary-article-subject)))
7849
7850 (defun gnus-summary-prev-unread-same-subject ()
7851   "Select previous unread article which has the same subject as current one."
7852   (interactive)
7853   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7854
7855 (defun gnus-summary-first-unread-article ()
7856   "Select the first unread article.
7857 Return nil if there are no unread articles."
7858   (interactive)
7859   (prog1
7860       (when (gnus-summary-first-subject t)
7861         (gnus-summary-show-thread)
7862         (gnus-summary-first-subject t)
7863         (gnus-summary-display-article (gnus-summary-article-number)))
7864     (gnus-summary-position-point)))
7865
7866 (defun gnus-summary-first-unread-subject ()
7867   "Place the point on the subject line of the first unread article.
7868 Return nil if there are no unread articles."
7869   (interactive)
7870   (prog1
7871       (when (gnus-summary-first-subject t)
7872         (gnus-summary-show-thread)
7873         (gnus-summary-first-subject t))
7874     (gnus-summary-position-point)))
7875
7876 (defun gnus-summary-first-unseen-subject ()
7877   "Place the point on the subject line of the first unseen article.
7878 Return nil if there are no unseen articles."
7879   (interactive)
7880   (prog1
7881       (when (gnus-summary-first-subject nil nil t)
7882         (gnus-summary-show-thread)
7883         (gnus-summary-first-subject nil nil t))
7884     (gnus-summary-position-point)))
7885
7886 (defun gnus-summary-first-unseen-or-unread-subject ()
7887   "Place the point on the subject line of the first unseen article or,
7888 if all article have been seen, on the subject line of the first unread
7889 article."
7890   (interactive)
7891   (prog1
7892       (unless (when (gnus-summary-first-subject nil nil t)
7893                 (gnus-summary-show-thread)
7894                 (gnus-summary-first-subject nil nil t))
7895         (when (gnus-summary-first-subject t)
7896           (gnus-summary-show-thread)
7897           (gnus-summary-first-subject t)))
7898     (gnus-summary-position-point)))
7899
7900 (defun gnus-summary-first-article ()
7901   "Select the first article.
7902 Return nil if there are no articles."
7903   (interactive)
7904   (prog1
7905       (when (gnus-summary-first-subject)
7906         (gnus-summary-show-thread)
7907         (gnus-summary-first-subject)
7908         (gnus-summary-display-article (gnus-summary-article-number)))
7909     (gnus-summary-position-point)))
7910
7911 (defun gnus-summary-best-unread-article (&optional arg)
7912   "Select the unread article with the highest score.
7913 If given a prefix argument, select the next unread article that has a
7914 score higher than the default score."
7915   (interactive "P")
7916   (let ((article (if arg
7917                      (gnus-summary-better-unread-subject)
7918                    (gnus-summary-best-unread-subject))))
7919     (if article
7920         (gnus-summary-goto-article article)
7921       (error "No unread articles"))))
7922
7923 (defun gnus-summary-best-unread-subject ()
7924   "Select the unread subject with the highest score."
7925   (interactive)
7926   (let ((best -1000000)
7927         (data gnus-newsgroup-data)
7928         article score)
7929     (while data
7930       (and (gnus-data-unread-p (car data))
7931            (> (setq score
7932                     (gnus-summary-article-score (gnus-data-number (car data))))
7933               best)
7934            (setq best score
7935                  article (gnus-data-number (car data))))
7936       (setq data (cdr data)))
7937     (when article
7938       (gnus-summary-goto-subject article))
7939     (gnus-summary-position-point)
7940     article))
7941
7942 (defun gnus-summary-better-unread-subject ()
7943   "Select the first unread subject that has a score over the default score."
7944   (interactive)
7945   (let ((data gnus-newsgroup-data)
7946         article score)
7947     (while (and (setq article (gnus-data-number (car data)))
7948                 (or (gnus-data-read-p (car data))
7949                     (not (> (gnus-summary-article-score article)
7950                             gnus-summary-default-score))))
7951       (setq data (cdr data)))
7952     (when article
7953       (gnus-summary-goto-subject article))
7954     (gnus-summary-position-point)
7955     article))
7956
7957 (defun gnus-summary-last-subject ()
7958   "Go to the last displayed subject line in the group."
7959   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7960     (when article
7961       (gnus-summary-goto-subject article))))
7962
7963 (defun gnus-summary-goto-article (article &optional all-headers force)
7964   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7965 If ALL-HEADERS is non-nil, no header lines are hidden.
7966 If FORCE, go to the article even if it isn't displayed.  If FORCE
7967 is a number, it is the line the article is to be displayed on."
7968   (interactive
7969    (list
7970     (completing-read
7971      "Article number or Message-ID: "
7972      (mapcar (lambda (number) (list (int-to-string number)))
7973              gnus-newsgroup-limit))
7974     current-prefix-arg
7975     t))
7976   (prog1
7977       (if (and (stringp article)
7978                (string-match "@\\|%40" article))
7979           (gnus-summary-refer-article article)
7980         (when (stringp article)
7981           (setq article (string-to-number article)))
7982         (if (gnus-summary-goto-subject article force)
7983             (gnus-summary-display-article article all-headers)
7984           (gnus-message 4 "Couldn't go to article %s" article) nil))
7985     (gnus-summary-position-point)))
7986
7987 (defun gnus-summary-goto-last-article ()
7988   "Go to the previously read article."
7989   (interactive)
7990   (prog1
7991       (when gnus-last-article
7992         (gnus-summary-goto-article gnus-last-article nil t))
7993     (gnus-summary-position-point)))
7994
7995 (defun gnus-summary-pop-article (number)
7996   "Pop one article off the history and go to the previous.
7997 NUMBER articles will be popped off."
7998   (interactive "p")
7999   (let (to)
8000     (setq gnus-newsgroup-history
8001           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8002     (if to
8003         (gnus-summary-goto-article (car to) nil t)
8004       (error "Article history empty")))
8005   (gnus-summary-position-point))
8006
8007 ;; Summary commands and functions for limiting the summary buffer.
8008
8009 (defun gnus-summary-limit-to-articles (n)
8010   "Limit the summary buffer to the next N articles.
8011 If not given a prefix, use the process marked articles instead."
8012   (interactive "P")
8013   (prog1
8014       (let ((articles (gnus-summary-work-articles n)))
8015         (setq gnus-newsgroup-processable nil)
8016         (gnus-summary-limit articles))
8017     (gnus-summary-position-point)))
8018
8019 (defun gnus-summary-pop-limit (&optional total)
8020   "Restore the previous limit.
8021 If given a prefix, remove all limits."
8022   (interactive "P")
8023   (when total
8024     (setq gnus-newsgroup-limits
8025           (list (mapcar (lambda (h) (mail-header-number h))
8026                         gnus-newsgroup-headers))))
8027   (unless gnus-newsgroup-limits
8028     (error "No limit to pop"))
8029   (prog1
8030       (gnus-summary-limit nil 'pop)
8031     (gnus-summary-position-point)))
8032
8033 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8034   "Limit the summary buffer to articles that have subjects that match a regexp.
8035 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8036   (interactive
8037    (list (read-string (if current-prefix-arg
8038                           "Exclude subject (regexp): "
8039                         "Limit to subject (regexp): "))
8040          nil current-prefix-arg))
8041   (unless header
8042     (setq header "subject"))
8043   (when (not (equal "" subject))
8044     (prog1
8045         (let ((articles (gnus-summary-find-matching
8046                          (or header "subject") subject 'all nil nil
8047                          not-matching)))
8048           (unless articles
8049             (error "Found no matches for \"%s\"" subject))
8050           (gnus-summary-limit articles))
8051       (gnus-summary-position-point))))
8052
8053 (defun gnus-summary-limit-to-author (from &optional not-matching)
8054   "Limit the summary buffer to articles that have authors that match a regexp.
8055 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8056   (interactive
8057    (list (read-string (if current-prefix-arg
8058                           "Exclude author (regexp): "
8059                         "Limit to author (regexp): "))
8060          current-prefix-arg))
8061   (gnus-summary-limit-to-subject from "from" not-matching))
8062
8063 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8064   "Limit the summary buffer to articles with the given RECIPIENT.
8065
8066 If NOT-MATCHING, exclude RECIPIENT.
8067
8068 To and Cc headers are checked.  You need to include them in
8069 `nnmail-extra-headers'."
8070   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8071   (interactive
8072    (list (read-string (format "%s recipient (regexp): "
8073                               (if current-prefix-arg "Exclude" "Limit to")))
8074          current-prefix-arg))
8075   (when (not (equal "" recipient))
8076     (prog1 (let* ((to
8077                    (if (memq 'To nnmail-extra-headers)
8078                        (gnus-summary-find-matching
8079                         (cons 'extra 'To) recipient 'all nil nil
8080                         not-matching)
8081                      (gnus-message
8082                       1 "`To' isn't present in `nnmail-extra-headers'")
8083                      (sit-for 1)
8084                      nil))
8085                   (cc
8086                    (if (memq 'Cc nnmail-extra-headers)
8087                        (gnus-summary-find-matching
8088                         (cons 'extra 'Cc) recipient 'all nil nil
8089                         not-matching)
8090                      (gnus-message
8091                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8092                      (sit-for 1)
8093                      nil))
8094                   (articles
8095                    (if not-matching
8096                        ;; We need the numbers that are in both lists:
8097                        (mapcar (lambda (a)
8098                                  (and (memq a to) a))
8099                                cc)
8100                      (nconc to cc))))
8101              (unless articles
8102                (error "Found no matches for \"%s\"" recipient))
8103              (gnus-summary-limit articles))
8104       (gnus-summary-position-point))))
8105
8106 (defun gnus-summary-limit-to-address (address &optional not-matching)
8107   "Limit the summary buffer to articles with the given ADDRESS.
8108
8109 If NOT-MATCHING, exclude ADDRESS.
8110
8111 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8112 in `nnmail-extra-headers'."
8113   (interactive
8114    (list (read-string (format "%s address (regexp): "
8115                               (if current-prefix-arg "Exclude" "Limit to")))
8116          current-prefix-arg))
8117   (when (not (equal "" address))
8118     (prog1 (let* ((to
8119                    (if (memq 'To nnmail-extra-headers)
8120                        (gnus-summary-find-matching
8121                         (cons 'extra 'To) address 'all nil nil
8122                         not-matching)
8123                      (gnus-message
8124                       1 "`To' isn't present in `nnmail-extra-headers'")
8125                      (sit-for 1)
8126                      t))
8127                   (cc
8128                    (if (memq 'Cc nnmail-extra-headers)
8129                        (gnus-summary-find-matching
8130                         (cons 'extra 'Cc) address 'all nil nil
8131                         not-matching)
8132                      (gnus-message
8133                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8134                      (sit-for 1)
8135                      t))
8136                   (from
8137                    (gnus-summary-find-matching "from" address
8138                                                'all nil nil not-matching))
8139                   (articles
8140                    (if not-matching
8141                        ;; We need the numbers that are in all lists:
8142                        (if (eq cc t)
8143                            (if (eq to t)
8144                                from
8145                              (mapcar (lambda (a) (car (memq a from))) to))
8146                          (if (eq to t)
8147                              (mapcar (lambda (a) (car (memq a from))) cc)
8148                            (mapcar (lambda (a) (car (memq a from)))
8149                                    (mapcar (lambda (a) (car (memq a to)))
8150                                            cc))))
8151                      (nconc (if (eq to t) nil to)
8152                             (if (eq cc t) nil cc)
8153                             from))))
8154              (unless articles
8155                (error "Found no matches for \"%s\"" address))
8156              (gnus-summary-limit articles))
8157       (gnus-summary-position-point))))
8158
8159 (defun gnus-summary-limit-strange-charsets-predicate (header)
8160   (let ((string (concat (mail-header-subject header)
8161                         (mail-header-from header)))
8162         charset found)
8163     (dotimes (i (1- (length string)))
8164       (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8165       (when (string-match "unicode\\|big\\|japanese" charset)
8166         (setq found t)))
8167     found))
8168
8169 (defun gnus-summary-limit-to-predicate (predicate)
8170   "Limit to articles where PREDICATE returns non-nil.
8171 PREDICATE will be called with the header structures of the
8172 articles."
8173   (let ((articles nil)
8174         (case-fold-search t))
8175     (dolist (header gnus-newsgroup-headers)
8176       (when (funcall predicate header)
8177         (push (mail-header-number header) articles)))
8178     (gnus-summary-limit (nreverse articles))))
8179
8180 (defun gnus-summary-limit-to-age (age &optional younger-p)
8181   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8182 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8183 articles that are younger than AGE days."
8184   (interactive
8185    (let ((younger current-prefix-arg)
8186          (days-got nil)
8187          days)
8188      (while (not days-got)
8189        (setq days (if younger
8190                       (read-string "Limit to articles younger than (in days, older when negative): ")
8191                     (read-string
8192                      "Limit to articles older than (in days, younger when negative): ")))
8193        (when (> (length days) 0)
8194          (setq days (read days)))
8195        (if (numberp days)
8196            (progn
8197              (setq days-got t)
8198              (when (< days 0)
8199                (setq younger (not younger))
8200                (setq days (* days -1))))
8201          (message "Please enter a number.")
8202          (sleep-for 1)))
8203      (list days younger)))
8204   (prog1
8205       (let ((data gnus-newsgroup-data)
8206             (cutoff (days-to-time age))
8207             articles d date is-younger)
8208         (while (setq d (pop data))
8209           (when (and (vectorp (gnus-data-header d))
8210                      (setq date (mail-header-date (gnus-data-header d))))
8211             (setq is-younger (time-less-p
8212                               (time-since (condition-case ()
8213                                               (date-to-time date)
8214                                             (error '(0 0))))
8215                               cutoff))
8216             (when (if younger-p
8217                       is-younger
8218                     (not is-younger))
8219               (push (gnus-data-number d) articles))))
8220         (gnus-summary-limit (nreverse articles)))
8221     (gnus-summary-position-point)))
8222
8223 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8224   "Limit the summary buffer to articles that match an 'extra' header."
8225   (interactive
8226    (let ((header
8227           (intern
8228            (gnus-completing-read-with-default
8229             (symbol-name (car gnus-extra-headers))
8230             (if current-prefix-arg
8231                 "Exclude extra header"
8232               "Limit extra header")
8233             (mapcar (lambda (x)
8234                       (cons (symbol-name x) x))
8235                     gnus-extra-headers)
8236             nil
8237             t))))
8238      (list header
8239            (read-string (format "%s header %s (regexp): "
8240                                 (if current-prefix-arg "Exclude" "Limit to")
8241                                 header))
8242            current-prefix-arg)))
8243   (when (not (equal "" regexp))
8244     (prog1
8245         (let ((articles (gnus-summary-find-matching
8246                          (cons 'extra header) regexp 'all nil nil
8247                          not-matching)))
8248           (unless articles
8249             (error "Found no matches for \"%s\"" regexp))
8250           (gnus-summary-limit articles))
8251       (gnus-summary-position-point))))
8252
8253 (defun gnus-summary-limit-to-display-predicate ()
8254   "Limit the summary buffer to the predicated in the `display' group parameter."
8255   (interactive)
8256   (unless gnus-newsgroup-display
8257     (error "There is no `display' group parameter"))
8258   (let (articles)
8259     (dolist (number gnus-newsgroup-articles)
8260       (when (funcall gnus-newsgroup-display)
8261         (push number articles)))
8262     (gnus-summary-limit articles))
8263   (gnus-summary-position-point))
8264
8265 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8266 (make-obsolete
8267  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8268
8269 (defun gnus-summary-limit-to-unread (&optional all)
8270   "Limit the summary buffer to articles that are not marked as read.
8271 If ALL is non-nil, limit strictly to unread articles."
8272   (interactive "P")
8273   (if all
8274       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8275     (gnus-summary-limit-to-marks
8276      ;; Concat all the marks that say that an article is read and have
8277      ;; those removed.
8278      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8279            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8280            gnus-low-score-mark gnus-expirable-mark
8281            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8282            gnus-duplicate-mark gnus-souped-mark)
8283      'reverse)))
8284
8285 (defun gnus-summary-limit-to-headers (match &optional reverse)
8286   "Limit the summary buffer to articles that have headers that match MATCH.
8287 If REVERSE (the prefix), limit to articles that don't match."
8288   (interactive "sMatch headers (regexp): \nP")
8289   (gnus-summary-limit-to-bodies match reverse t))
8290
8291 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8292   "Limit the summary buffer to articles that have bodies that match MATCH.
8293 If REVERSE (the prefix), limit to articles that don't match."
8294   (interactive "sMatch body (regexp): \nP")
8295   (let ((articles nil)
8296         (gnus-select-article-hook nil)  ;Disable hook.
8297         (gnus-article-prepare-hook nil)
8298         (gnus-use-article-prefetch nil)
8299         (gnus-keep-backlog nil)
8300         (gnus-break-pages nil)
8301         (gnus-summary-display-arrow nil)
8302         (gnus-updated-mode-lines nil)
8303         (gnus-auto-center-summary nil)
8304         (gnus-display-mime-function nil))
8305     (dolist (data gnus-newsgroup-data)
8306       (let (gnus-mark-article-hook)
8307         (gnus-summary-select-article t t nil (gnus-data-number data)))
8308       (save-excursion
8309         (set-buffer gnus-article-buffer)
8310         (article-goto-body)
8311         (let* ((case-fold-search t)
8312                (found (if headersp
8313                           (re-search-backward match nil t)
8314                         (re-search-forward match nil t))))
8315           (when (or (and found
8316                          (not reverse))
8317                     (and (not found)
8318                          reverse))
8319             (push (gnus-data-number data) articles)))))
8320     (if (not articles)
8321         (message "No messages matched")
8322       (gnus-summary-limit articles)))
8323   (gnus-summary-position-point))
8324
8325 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8326   "Limit the summary buffer to articles that aren't part on any thread.
8327 If THREADSP (the prefix), limit to articles that are in threads."
8328   (interactive "P")
8329   (let ((articles nil)
8330         thread-articles
8331         threads)
8332     (dolist (thread gnus-newsgroup-threads)
8333       (if (stringp (car thread))
8334           (dolist (thread (cdr thread))
8335             (push thread threads))
8336         (push thread threads)))
8337     (dolist (thread threads)
8338       (setq thread-articles (gnus-articles-in-thread thread))
8339       (when (or (and threadsp
8340                      (> (length thread-articles) 1))
8341                 (and (not threadsp)
8342                      (= (length thread-articles) 1)))
8343         (setq articles (nconc thread-articles articles))))
8344     (if (not articles)
8345         (message "No messages matched")
8346       (gnus-summary-limit articles))
8347     (gnus-summary-position-point)))
8348
8349 (defun gnus-summary-limit-to-replied (&optional unreplied)
8350   "Limit the summary buffer to replied articles.
8351 If UNREPLIED (the prefix), limit to unreplied articles."
8352   (interactive "P")
8353   (if unreplied
8354       (gnus-summary-limit
8355        (gnus-set-difference gnus-newsgroup-articles
8356         gnus-newsgroup-replied))
8357     (gnus-summary-limit gnus-newsgroup-replied))
8358   (gnus-summary-position-point))
8359
8360 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8361 (make-obsolete 'gnus-summary-delete-marked-with
8362                'gnus-summary-limit-exclude-marks)
8363
8364 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8365   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8366 If REVERSE, limit the summary buffer to articles that are marked
8367 with MARKS.  MARKS can either be a string of marks or a list of marks.
8368 Returns how many articles were removed."
8369   (interactive "sMarks: ")
8370   (gnus-summary-limit-to-marks marks t))
8371
8372 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8373   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8374 If REVERSE (the prefix), limit the summary buffer to articles that are
8375 not marked with MARKS.  MARKS can either be a string of marks or a
8376 list of marks.
8377 Returns how many articles were removed."
8378   (interactive "sMarks: \nP")
8379   (prog1
8380       (let ((data gnus-newsgroup-data)
8381             (marks (if (listp marks) marks
8382                      (append marks nil))) ; Transform to list.
8383             articles)
8384         (while data
8385           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8386                   (memq (gnus-data-mark (car data)) marks))
8387             (push (gnus-data-number (car data)) articles))
8388           (setq data (cdr data)))
8389         (gnus-summary-limit articles))
8390     (gnus-summary-position-point)))
8391
8392 (defun gnus-summary-limit-to-score (score)
8393   "Limit to articles with score at or above SCORE."
8394   (interactive "NLimit to articles with score of at least: ")
8395   (let ((data gnus-newsgroup-data)
8396         articles)
8397     (while data
8398       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8399                 score)
8400         (push (gnus-data-number (car data)) articles))
8401       (setq data (cdr data)))
8402     (prog1
8403         (gnus-summary-limit articles)
8404       (gnus-summary-position-point))))
8405
8406 (defun gnus-summary-limit-to-unseen ()
8407   "Limit to unseen articles."
8408   (interactive)
8409   (prog1
8410       (gnus-summary-limit gnus-newsgroup-unseen)
8411     (gnus-summary-position-point)))
8412
8413 (defun gnus-summary-limit-include-thread (id)
8414   "Display all the hidden articles that is in the thread with ID in it.
8415 When called interactively, ID is the Message-ID of the current
8416 article."
8417   (interactive (list (mail-header-id (gnus-summary-article-header))))
8418   (let ((articles (gnus-articles-in-thread
8419                    (gnus-id-to-thread (gnus-root-id id)))))
8420     (prog1
8421         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8422       (gnus-summary-limit-include-matching-articles
8423        "subject"
8424        (regexp-quote (gnus-simplify-subject-re
8425                       (mail-header-subject (gnus-id-to-header id)))))
8426       (gnus-summary-position-point))))
8427
8428 (defun gnus-summary-limit-include-matching-articles (header regexp)
8429   "Display all the hidden articles that have HEADERs that match REGEXP."
8430   (interactive (list (read-string "Match on header: ")
8431                      (read-string "Regexp: ")))
8432   (let ((articles (gnus-find-matching-articles header regexp)))
8433     (prog1
8434         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8435       (gnus-summary-position-point))))
8436
8437 (defun gnus-summary-insert-dormant-articles ()
8438   "Insert all the dormant articles for this group into the current buffer."
8439   (interactive)
8440   (let ((gnus-verbose (max 6 gnus-verbose)))
8441     (if (not gnus-newsgroup-dormant)
8442         (gnus-message 3 "No dormant articles for this group")
8443       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8444
8445 (defun gnus-summary-insert-ticked-articles ()
8446   "Insert ticked articles for this group into the current buffer."
8447   (interactive)
8448   (let ((gnus-verbose (max 6 gnus-verbose)))
8449     (if (not gnus-newsgroup-marked)
8450         (gnus-message 3 "No ticked articles for this group")
8451       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8452
8453 (defun gnus-summary-limit-include-dormant ()
8454   "Display all the hidden articles that are marked as dormant.
8455 Note that this command only works on a subset of the articles currently
8456 fetched for this group."
8457   (interactive)
8458   (unless gnus-newsgroup-dormant
8459     (error "There are no dormant articles in this group"))
8460   (prog1
8461       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8462     (gnus-summary-position-point)))
8463
8464 (defun gnus-summary-limit-exclude-dormant ()
8465   "Hide all dormant articles."
8466   (interactive)
8467   (prog1
8468       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8469     (gnus-summary-position-point)))
8470
8471 (defun gnus-summary-limit-exclude-childless-dormant ()
8472   "Hide all dormant articles that have no children."
8473   (interactive)
8474   (let ((data (gnus-data-list t))
8475         articles d children)
8476     ;; Find all articles that are either not dormant or have
8477     ;; children.
8478     (while (setq d (pop data))
8479       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8480                 (and (setq children
8481                            (gnus-article-children (gnus-data-number d)))
8482                      (let (found)
8483                        (while children
8484                          (when (memq (car children) articles)
8485                            (setq children nil
8486                                  found t))
8487                          (pop children))
8488                        found)))
8489         (push (gnus-data-number d) articles)))
8490     ;; Do the limiting.
8491     (prog1
8492         (gnus-summary-limit articles)
8493       (gnus-summary-position-point))))
8494
8495 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8496   "Mark all unread excluded articles as read.
8497 If ALL, mark even excluded ticked and dormants as read."
8498   (interactive "P")
8499   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8500   (let ((articles (gnus-sorted-ndifference
8501                    (sort
8502                     (mapcar (lambda (h) (mail-header-number h))
8503                             gnus-newsgroup-headers)
8504                     '<)
8505                    gnus-newsgroup-limit))
8506         article)
8507     (setq gnus-newsgroup-unreads
8508           (gnus-sorted-intersection gnus-newsgroup-unreads
8509                                     gnus-newsgroup-limit))
8510     (if all
8511         (setq gnus-newsgroup-dormant nil
8512               gnus-newsgroup-marked nil
8513               gnus-newsgroup-reads
8514               (nconc
8515                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8516                gnus-newsgroup-reads))
8517       (while (setq article (pop articles))
8518         (unless (or (memq article gnus-newsgroup-dormant)
8519                     (memq article gnus-newsgroup-marked))
8520           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8521
8522 (defun gnus-summary-limit (articles &optional pop)
8523   (if pop
8524       ;; We pop the previous limit off the stack and use that.
8525       (setq articles (car gnus-newsgroup-limits)
8526             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8527     ;; We use the new limit, so we push the old limit on the stack.
8528     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8529   ;; Set the limit.
8530   (setq gnus-newsgroup-limit articles)
8531   (let ((total (length gnus-newsgroup-data))
8532         (data (gnus-data-find-list (gnus-summary-article-number)))
8533         (gnus-summary-mark-below nil)   ; Inhibit this.
8534         found)
8535     ;; This will do all the work of generating the new summary buffer
8536     ;; according to the new limit.
8537     (gnus-summary-prepare)
8538     ;; Hide any threads, possibly.
8539     (gnus-summary-maybe-hide-threads)
8540     ;; Try to return to the article you were at, or one in the
8541     ;; neighborhood.
8542     (when data
8543       ;; We try to find some article after the current one.
8544       (while data
8545         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8546           (setq data nil
8547                 found t))
8548         (setq data (cdr data))))
8549     (unless found
8550       ;; If there is no data, that means that we were after the last
8551       ;; article.  The same goes when we can't find any articles
8552       ;; after the current one.
8553       (goto-char (point-max))
8554       (gnus-summary-find-prev))
8555     (gnus-set-mode-line 'summary)
8556     ;; We return how many articles were removed from the summary
8557     ;; buffer as a result of the new limit.
8558     (- total (length gnus-newsgroup-data))))
8559
8560 (defsubst gnus-invisible-cut-children (threads)
8561   (let ((num 0))
8562     (while threads
8563       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8564         (incf num))
8565       (pop threads))
8566     (< num 2)))
8567
8568 (defsubst gnus-cut-thread (thread)
8569   "Go forwards in the thread until we find an article that we want to display."
8570   (when (or (eq gnus-fetch-old-headers 'some)
8571             (eq gnus-fetch-old-headers 'invisible)
8572             (numberp gnus-fetch-old-headers)
8573             (eq gnus-build-sparse-threads 'some)
8574             (eq gnus-build-sparse-threads 'more))
8575     ;; Deal with old-fetched headers and sparse threads.
8576     (while (and
8577             thread
8578             (or
8579              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8580              (gnus-summary-article-ancient-p
8581               (mail-header-number (car thread))))
8582             (if (or (<= (length (cdr thread)) 1)
8583                     (eq gnus-fetch-old-headers 'invisible))
8584                 (setq gnus-newsgroup-limit
8585                       (delq (mail-header-number (car thread))
8586                             gnus-newsgroup-limit)
8587                       thread (cadr thread))
8588               (when (gnus-invisible-cut-children (cdr thread))
8589                 (let ((th (cdr thread)))
8590                   (while th
8591                     (if (memq (mail-header-number (caar th))
8592                               gnus-newsgroup-limit)
8593                         (setq thread (car th)
8594                               th nil)
8595                       (setq th (cdr th))))))))))
8596   thread)
8597
8598 (defun gnus-cut-threads (threads)
8599   "Cut off all uninteresting articles from the beginning of THREADS."
8600   (when (or (eq gnus-fetch-old-headers 'some)
8601             (eq gnus-fetch-old-headers 'invisible)
8602             (numberp gnus-fetch-old-headers)
8603             (eq gnus-build-sparse-threads 'some)
8604             (eq gnus-build-sparse-threads 'more))
8605     (let ((th threads))
8606       (while th
8607         (setcar th (gnus-cut-thread (car th)))
8608         (setq th (cdr th)))))
8609   ;; Remove nixed out threads.
8610   (delq nil threads))
8611
8612 (defun gnus-summary-initial-limit (&optional show-if-empty)
8613   "Figure out what the initial limit is supposed to be on group entry.
8614 This entails weeding out unwanted dormants, low-scored articles,
8615 fetch-old-headers verbiage, and so on."
8616   ;; Most groups have nothing to remove.
8617   (unless (or gnus-inhibit-limiting
8618               (and (null gnus-newsgroup-dormant)
8619                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8620                    (not (eq gnus-fetch-old-headers 'some))
8621                    (not (numberp gnus-fetch-old-headers))
8622                    (not (eq gnus-fetch-old-headers 'invisible))
8623                    (null gnus-summary-expunge-below)
8624                    (not (eq gnus-build-sparse-threads 'some))
8625                    (not (eq gnus-build-sparse-threads 'more))
8626                    (null gnus-thread-expunge-below)
8627                    (not gnus-use-nocem)))
8628     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8629     (setq gnus-newsgroup-limit nil)
8630     (mapatoms
8631      (lambda (node)
8632        (unless (car (symbol-value node))
8633          ;; These threads have no parents -- they are roots.
8634          (let ((nodes (cdr (symbol-value node)))
8635                thread)
8636            (while nodes
8637              (if (and gnus-thread-expunge-below
8638                       (< (gnus-thread-total-score (car nodes))
8639                          gnus-thread-expunge-below))
8640                  (gnus-expunge-thread (pop nodes))
8641                (setq thread (pop nodes))
8642                (gnus-summary-limit-children thread))))))
8643      gnus-newsgroup-dependencies)
8644     ;; If this limitation resulted in an empty group, we might
8645     ;; pop the previous limit and use it instead.
8646     (when (and (not gnus-newsgroup-limit)
8647                show-if-empty)
8648       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8649     gnus-newsgroup-limit))
8650
8651 (defun gnus-summary-limit-children (thread)
8652   "Return 1 if this subthread is visible and 0 if it is not."
8653   ;; First we get the number of visible children to this thread.  This
8654   ;; is done by recursing down the thread using this function, so this
8655   ;; will really go down to a leaf article first, before slowly
8656   ;; working its way up towards the root.
8657   (when thread
8658     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8659            (children
8660            (if (cdr thread)
8661                (apply '+ (mapcar 'gnus-summary-limit-children
8662                                  (cdr thread)))
8663              0))
8664           (number (mail-header-number (car thread)))
8665           score)
8666       (if (and
8667            (not (memq number gnus-newsgroup-marked))
8668            (or
8669             ;; If this article is dormant and has absolutely no visible
8670             ;; children, then this article isn't visible.
8671             (and (memq number gnus-newsgroup-dormant)
8672                  (zerop children))
8673             ;; If this is "fetch-old-headered" and there is no
8674             ;; visible children, then we don't want this article.
8675             (and (or (eq gnus-fetch-old-headers 'some)
8676                      (numberp gnus-fetch-old-headers))
8677                  (gnus-summary-article-ancient-p number)
8678                  (zerop children))
8679             ;; If this is "fetch-old-headered" and `invisible', then
8680             ;; we don't want this article.
8681             (and (eq gnus-fetch-old-headers 'invisible)
8682                  (gnus-summary-article-ancient-p number))
8683             ;; If this is a sparsely inserted article with no children,
8684             ;; we don't want it.
8685             (and (eq gnus-build-sparse-threads 'some)
8686                  (gnus-summary-article-sparse-p number)
8687                  (zerop children))
8688             ;; If we use expunging, and this article is really
8689             ;; low-scored, then we don't want this article.
8690             (when (and gnus-summary-expunge-below
8691                        (< (setq score
8692                                 (or (cdr (assq number gnus-newsgroup-scored))
8693                                     gnus-summary-default-score))
8694                           gnus-summary-expunge-below))
8695               ;; We increase the expunge-tally here, but that has
8696               ;; nothing to do with the limits, really.
8697               (incf gnus-newsgroup-expunged-tally)
8698               ;; We also mark as read here, if that's wanted.
8699               (when (and gnus-summary-mark-below
8700                          (< score gnus-summary-mark-below))
8701                 (setq gnus-newsgroup-unreads
8702                       (delq number gnus-newsgroup-unreads))
8703                 (if gnus-newsgroup-auto-expire
8704                     (push number gnus-newsgroup-expirable)
8705                   (push (cons number gnus-low-score-mark)
8706                         gnus-newsgroup-reads)))
8707               t)
8708             ;; Do the `display' group parameter.
8709             (and gnus-newsgroup-display
8710                  (not (funcall gnus-newsgroup-display)))
8711             ;; Check NoCeM things.
8712             (when (and gnus-use-nocem
8713                        (gnus-nocem-unwanted-article-p
8714                         (mail-header-id (car thread))))
8715               (setq gnus-newsgroup-unreads
8716                     (delq number gnus-newsgroup-unreads))
8717               t)))
8718           ;; Nope, invisible article.
8719           0
8720         ;; Ok, this article is to be visible, so we add it to the limit
8721         ;; and return 1.
8722         (push number gnus-newsgroup-limit)
8723         1))))
8724
8725 (defun gnus-expunge-thread (thread)
8726   "Mark all articles in THREAD as read."
8727   (let* ((number (mail-header-number (car thread))))
8728     (incf gnus-newsgroup-expunged-tally)
8729     ;; We also mark as read here, if that's wanted.
8730     (setq gnus-newsgroup-unreads
8731           (delq number gnus-newsgroup-unreads))
8732     (if gnus-newsgroup-auto-expire
8733         (push number gnus-newsgroup-expirable)
8734       (push (cons number gnus-low-score-mark)
8735             gnus-newsgroup-reads)))
8736   ;; Go recursively through all subthreads.
8737   (mapcar 'gnus-expunge-thread (cdr thread)))
8738
8739 ;; Summary article oriented commands
8740
8741 (defun gnus-summary-refer-parent-article (n)
8742   "Refer parent article N times.
8743 If N is negative, go to ancestor -N instead.
8744 The difference between N and the number of articles fetched is returned."
8745   (interactive "p")
8746   (let ((skip 1)
8747         error header ref)
8748     (when (not (natnump n))
8749       (setq skip (abs n)
8750             n 1))
8751     (while (and (> n 0)
8752                 (not error))
8753       (setq header (gnus-summary-article-header))
8754       (if (and (eq (mail-header-number header)
8755                    (cdr gnus-article-current))
8756                (equal gnus-newsgroup-name
8757                       (car gnus-article-current)))
8758           ;; If we try to find the parent of the currently
8759           ;; displayed article, then we take a look at the actual
8760           ;; References header, since this is slightly more
8761           ;; reliable than the References field we got from the
8762           ;; server.
8763           (with-current-buffer gnus-original-article-buffer
8764             (nnheader-narrow-to-headers)
8765             (unless (setq ref (message-fetch-field "references"))
8766               (when (setq ref (message-fetch-field "in-reply-to"))
8767                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8768             (widen))
8769         (setq ref
8770               ;; It's not the current article, so we take a bet on
8771               ;; the value we got from the server.
8772               (mail-header-references header)))
8773       (if (and ref
8774                (not (equal ref "")))
8775           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8776             (gnus-message 1 "Couldn't find parent"))
8777         (gnus-message 1 "No references in article %d"
8778                       (gnus-summary-article-number))
8779         (setq error t))
8780       (decf n))
8781     (gnus-summary-position-point)
8782     n))
8783
8784 (defun gnus-summary-refer-references ()
8785   "Fetch all articles mentioned in the References header.
8786 Return the number of articles fetched."
8787   (interactive)
8788   (let ((ref (mail-header-references (gnus-summary-article-header)))
8789         (current (gnus-summary-article-number))
8790         (n 0))
8791     (if (or (not ref)
8792             (equal ref ""))
8793         (error "No References in the current article")
8794       ;; For each Message-ID in the References header...
8795       (while (string-match "<[^>]*>" ref)
8796         (incf n)
8797         ;; ... fetch that article.
8798         (gnus-summary-refer-article
8799          (prog1 (match-string 0 ref)
8800            (setq ref (substring ref (match-end 0))))))
8801       (gnus-summary-goto-subject current)
8802       (gnus-summary-position-point)
8803       n)))
8804
8805 (defun gnus-summary-refer-thread (&optional limit)
8806   "Fetch all articles in the current thread.
8807 If LIMIT (the numerical prefix), fetch that many old headers instead
8808 of what's specified by the `gnus-refer-thread-limit' variable."
8809   (interactive "P")
8810   (let ((id (mail-header-id (gnus-summary-article-header)))
8811         (limit (if limit (prefix-numeric-value limit)
8812                  gnus-refer-thread-limit)))
8813     (unless (eq gnus-fetch-old-headers 'invisible)
8814       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8815       ;; Retrieve the headers and read them in.
8816       (if (eq (if (numberp limit)
8817                   (gnus-retrieve-headers
8818                    (list (min
8819                           (+ (mail-header-number
8820                               (gnus-summary-article-header))
8821                              limit)
8822                           gnus-newsgroup-end))
8823                    gnus-newsgroup-name (* limit 2))
8824                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8825                 ;; headers.
8826                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8827                                        gnus-newsgroup-name limit))
8828               'nov)
8829           (gnus-build-all-threads)
8830         (error "Can't fetch thread from back ends that don't support NOV"))
8831       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8832     (gnus-summary-limit-include-thread id)))
8833
8834 (defun gnus-summary-refer-article (message-id)
8835   "Fetch an article specified by MESSAGE-ID."
8836   (interactive "sMessage-ID: ")
8837   (when (and (stringp message-id)
8838              (not (zerop (length message-id))))
8839     (setq message-id (gnus-replace-in-string message-id " " ""))
8840     ;; Construct the correct Message-ID if necessary.
8841     ;; Suggested by tale@pawl.rpi.edu.
8842     (unless (string-match "^<" message-id)
8843       (setq message-id (concat "<" message-id)))
8844     (unless (string-match ">$" message-id)
8845       (setq message-id (concat message-id ">")))
8846     ;; People often post MIDs from URLs, so unhex it:
8847     (unless (string-match "@" message-id)
8848       (setq message-id (gnus-url-unhex-string message-id)))
8849     (let* ((header (gnus-id-to-header message-id))
8850            (sparse (and header
8851                         (gnus-summary-article-sparse-p
8852                          (mail-header-number header))
8853                         (memq (mail-header-number header)
8854                               gnus-newsgroup-limit)))
8855            number)
8856       (cond
8857        ;; If the article is present in the buffer we just go to it.
8858        ((and header
8859              (or (not (gnus-summary-article-sparse-p
8860                        (mail-header-number header)))
8861                  sparse))
8862         (prog1
8863             (gnus-summary-goto-article
8864              (mail-header-number header) nil t)
8865           (when sparse
8866             (gnus-summary-update-article (mail-header-number header)))))
8867        (t
8868         ;; We fetch the article.
8869         (catch 'found
8870           (dolist (gnus-override-method (gnus-refer-article-methods))
8871             (when (and (gnus-check-server gnus-override-method)
8872                        ;; Fetch the header,
8873                        (setq number (gnus-summary-insert-subject message-id)))
8874               ;; and display the article.
8875               (gnus-summary-select-article nil nil nil number)
8876               (throw 'found t)))
8877           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8878
8879 (defun gnus-refer-article-methods ()
8880   "Return a list of referable methods."
8881   (cond
8882    ;; No method, so we default to current and native.
8883    ((null gnus-refer-article-method)
8884     (list gnus-current-select-method gnus-select-method))
8885    ;; Current.
8886    ((eq 'current gnus-refer-article-method)
8887     (list gnus-current-select-method))
8888    ;; List of select methods.
8889    ((not (and (symbolp (car gnus-refer-article-method))
8890               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8891     (let (out)
8892       (dolist (method gnus-refer-article-method)
8893         (push (if (eq 'current method)
8894                   gnus-current-select-method
8895                 method)
8896               out))
8897       (nreverse out)))
8898    ;; One single select method.
8899    (t
8900     (list gnus-refer-article-method))))
8901
8902 (defun gnus-summary-edit-parameters ()
8903   "Edit the group parameters of the current group."
8904   (interactive)
8905   (gnus-group-edit-group gnus-newsgroup-name 'params))
8906
8907 (defun gnus-summary-customize-parameters ()
8908   "Customize the group parameters of the current group."
8909   (interactive)
8910   (gnus-group-customize gnus-newsgroup-name))
8911
8912 (defun gnus-summary-enter-digest-group (&optional force)
8913   "Enter an nndoc group based on the current article.
8914 If FORCE, force a digest interpretation.  If not, try
8915 to guess what the document format is."
8916   (interactive "P")
8917   (let ((conf gnus-current-window-configuration))
8918     (save-window-excursion
8919       (save-excursion
8920         (let (gnus-article-prepare-hook
8921               gnus-display-mime-function
8922               gnus-break-pages)
8923           (gnus-summary-select-article))))
8924     (setq gnus-current-window-configuration conf)
8925     (let* ((name (format "%s-%d"
8926                          (gnus-group-prefixed-name
8927                           gnus-newsgroup-name (list 'nndoc ""))
8928                          (with-current-buffer gnus-summary-buffer
8929                            gnus-current-article)))
8930            (ogroup gnus-newsgroup-name)
8931            (params (append (gnus-info-params (gnus-get-info ogroup))
8932                            (list (cons 'to-group ogroup))
8933                            (list (cons 'parent-group ogroup))
8934                            (list (cons 'save-article-group ogroup))))
8935            (case-fold-search t)
8936            (buf (current-buffer))
8937            dig to-address)
8938       (with-current-buffer gnus-original-article-buffer
8939         ;; Have the digest group inherit the main mail address of
8940         ;; the parent article.
8941         (when (setq to-address (or (gnus-fetch-field "reply-to")
8942                                    (gnus-fetch-field "from")))
8943           (setq params
8944                 (append
8945                  (list (cons 'to-address
8946                              (funcall gnus-decode-encoded-address-function
8947                                       to-address))))))
8948         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8949         (insert-buffer-substring gnus-original-article-buffer)
8950         ;; Remove lines that may lead nndoc to misinterpret the
8951         ;; document type.
8952         (narrow-to-region
8953          (goto-char (point-min))
8954          (or (search-forward "\n\n" nil t) (point)))
8955         (goto-char (point-min))
8956         (delete-matching-lines "^Path:\\|^From ")
8957         (widen))
8958       (unwind-protect
8959           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8960                     (gnus-newsgroup-ephemeral-ignored-charsets
8961                      gnus-newsgroup-ignored-charsets))
8962                 (gnus-group-read-ephemeral-group
8963                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8964                               (nndoc-article-type
8965                                ,(if force 'mbox 'guess)))
8966                  t nil nil nil
8967                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8968                                                         "ADAPT")))))
8969               ;; Make all postings to this group go to the parent group.
8970               (nconc (gnus-info-params (gnus-get-info name))
8971                      params)
8972             ;; Couldn't select this doc group.
8973             (switch-to-buffer buf)
8974             (gnus-set-global-variables)
8975             (gnus-configure-windows 'summary)
8976             (gnus-message 3 "Article couldn't be entered?"))
8977         (kill-buffer dig)))))
8978
8979 (defun gnus-summary-read-document (n)
8980   "Open a new group based on the current article(s).
8981 This will allow you to read digests and other similar
8982 documents as newsgroups.
8983 Obeys the standard process/prefix convention."
8984   (interactive "P")
8985   (let* ((ogroup gnus-newsgroup-name)
8986          (params (append (gnus-info-params (gnus-get-info ogroup))
8987                          (list (cons 'to-group ogroup))))
8988          group egroup groups vgroup)
8989     (dolist (article (gnus-summary-work-articles n))
8990       (setq group (format "%s-%d" gnus-newsgroup-name article))
8991       (gnus-summary-remove-process-mark article)
8992       (when (gnus-summary-display-article article)
8993         (save-excursion
8994           (with-temp-buffer
8995             (insert-buffer-substring gnus-original-article-buffer)
8996             ;; Remove some headers that may lead nndoc to make
8997             ;; the wrong guess.
8998             (message-narrow-to-head)
8999             (goto-char (point-min))
9000             (delete-matching-lines "^Path:\\|^From ")
9001             (widen)
9002             (if (setq egroup
9003                       (gnus-group-read-ephemeral-group
9004                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9005                                      (nndoc-article-type guess))
9006                        t nil t))
9007                 (progn
9008                   ;; Make all postings to this group go to the parent group.
9009                   (nconc (gnus-info-params (gnus-get-info egroup))
9010                          params)
9011                   (push egroup groups))
9012               ;; Couldn't select this doc group.
9013               (gnus-error 3 "Article couldn't be entered"))))))
9014     ;; Now we have selected all the documents.
9015     (cond
9016      ((not groups)
9017       (error "None of the articles could be interpreted as documents"))
9018      ((gnus-group-read-ephemeral-group
9019        (setq vgroup (format
9020                      "nnvirtual:%s-%s" gnus-newsgroup-name
9021                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9022        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9023        t
9024        (cons (current-buffer) 'summary)))
9025      (t
9026       (error "Couldn't select virtual nndoc group")))))
9027
9028 (defun gnus-summary-isearch-article (&optional regexp-p)
9029   "Do incremental search forward on the current article.
9030 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9031   (interactive "P")
9032   (gnus-summary-select-article)
9033   (gnus-configure-windows 'article)
9034   (gnus-eval-in-buffer-window gnus-article-buffer
9035     (save-restriction
9036       (widen)
9037       (isearch-forward regexp-p))))
9038
9039 (defun gnus-summary-repeat-search-article-forward ()
9040   "Repeat the previous search forwards."
9041   (interactive)
9042   (unless gnus-last-search-regexp
9043     (error "No previous search"))
9044   (gnus-summary-search-article-forward gnus-last-search-regexp))
9045
9046 (defun gnus-summary-repeat-search-article-backward ()
9047   "Repeat the previous search backwards."
9048   (interactive)
9049   (unless gnus-last-search-regexp
9050     (error "No previous search"))
9051   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9052
9053 (defun gnus-summary-search-article-forward (regexp &optional backward)
9054   "Search for an article containing REGEXP forward.
9055 If BACKWARD, search backward instead."
9056   (interactive
9057    (list (read-string
9058           (format "Search article %s (regexp%s): "
9059                   (if current-prefix-arg "backward" "forward")
9060                   (if gnus-last-search-regexp
9061                       (concat ", default " gnus-last-search-regexp)
9062                     "")))
9063          current-prefix-arg))
9064   (if (string-equal regexp "")
9065       (setq regexp (or gnus-last-search-regexp ""))
9066     (setq gnus-last-search-regexp regexp)
9067     (setq gnus-article-before-search gnus-current-article))
9068   ;; Intentionally set gnus-last-article.
9069   (setq gnus-last-article gnus-article-before-search)
9070   (let ((gnus-last-article gnus-last-article))
9071     (if (gnus-summary-search-article regexp backward)
9072         (gnus-summary-show-thread)
9073       (signal 'search-failed (list regexp)))))
9074
9075 (defun gnus-summary-search-article-backward (regexp)
9076   "Search for an article containing REGEXP backward."
9077   (interactive
9078    (list (read-string
9079           (format "Search article backward (regexp%s): "
9080                   (if gnus-last-search-regexp
9081                       (concat ", default " gnus-last-search-regexp)
9082                     "")))))
9083   (gnus-summary-search-article-forward regexp 'backward))
9084
9085 (defun gnus-summary-search-article (regexp &optional backward)
9086   "Search for an article containing REGEXP.
9087 Optional argument BACKWARD means do search for backward.
9088 `gnus-select-article-hook' is not called during the search."
9089   ;; We have to require this here to make sure that the following
9090   ;; dynamic binding isn't shadowed by autoloading.
9091   (require 'gnus-async)
9092   (require 'gnus-art)
9093   (let ((gnus-select-article-hook nil)  ;Disable hook.
9094         (gnus-article-prepare-hook nil)
9095         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9096         (gnus-use-article-prefetch nil)
9097         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9098         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9099         (gnus-visual nil)
9100         (gnus-keep-backlog nil)
9101         (gnus-break-pages nil)
9102         (gnus-summary-display-arrow nil)
9103         (gnus-updated-mode-lines nil)
9104         (gnus-auto-center-summary nil)
9105         (sum (current-buffer))
9106         (gnus-display-mime-function nil)
9107         (found nil)
9108         point)
9109     (gnus-save-hidden-threads
9110       (gnus-summary-select-article)
9111       (set-buffer gnus-article-buffer)
9112       (goto-char (window-point (get-buffer-window (current-buffer))))
9113       (when backward
9114         (forward-line -1))
9115       (while (not found)
9116         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9117         (if (if backward
9118                 (re-search-backward regexp nil t)
9119               (re-search-forward regexp nil t))
9120             ;; We found the regexp.
9121             (progn
9122               (setq found 'found)
9123               (beginning-of-line)
9124               (set-window-start
9125                (get-buffer-window (current-buffer))
9126                (point))
9127               (forward-line 1)
9128               (set-window-point
9129                (get-buffer-window (current-buffer))
9130                (point))
9131               (set-buffer sum)
9132               (setq point (point)))
9133           ;; We didn't find it, so we go to the next article.
9134           (set-buffer sum)
9135           (setq found 'not)
9136           (while (eq found 'not)
9137             (if (not (if backward (gnus-summary-find-prev)
9138                        (gnus-summary-find-next)))
9139                 ;; No more articles.
9140                 (setq found t)
9141               ;; Select the next article and adjust point.
9142               (unless (gnus-summary-article-sparse-p
9143                        (gnus-summary-article-number))
9144                 (setq found nil)
9145                 (gnus-summary-select-article)
9146                 (set-buffer gnus-article-buffer)
9147                 (widen)
9148                 (goto-char (if backward (point-max) (point-min))))))))
9149       (gnus-message 7 ""))
9150     ;; Return whether we found the regexp.
9151     (when (eq found 'found)
9152       (goto-char point)
9153       (gnus-summary-show-thread)
9154       (gnus-summary-goto-subject gnus-current-article)
9155       (gnus-summary-position-point)
9156       t)))
9157
9158 (defun gnus-find-matching-articles (header regexp)
9159   "Return a list of all articles that match REGEXP on HEADER.
9160 This search includes all articles in the current group that Gnus has
9161 fetched headers for, whether they are displayed or not."
9162   (let ((articles nil)
9163         ;; Can't eta-reduce because it's a macro.
9164         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9165         (case-fold-search t))
9166     (dolist (header gnus-newsgroup-headers)
9167       (when (string-match regexp (funcall func header))
9168         (push (mail-header-number header) articles)))
9169     (nreverse articles)))
9170
9171 (defun gnus-summary-find-matching (header regexp &optional backward unread
9172                                           not-case-fold not-matching)
9173   "Return a list of all articles that match REGEXP on HEADER.
9174 The search stars on the current article and goes forwards unless
9175 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9176 If UNREAD is non-nil, only unread articles will
9177 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9178 in the comparisons. If NOT-MATCHING, return a list of all articles that
9179 not match REGEXP on HEADER."
9180   (let ((case-fold-search (not not-case-fold))
9181         articles d func)
9182     (if (consp header)
9183         (if (eq (car header) 'extra)
9184             (setq func
9185                   `(lambda (h)
9186                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9187                          "")))
9188           (error "%s is an invalid header" header))
9189       (unless (fboundp (intern (concat "mail-header-" header)))
9190         (error "%s is not a valid header" header))
9191       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9192     (dolist (d (if (eq backward 'all)
9193                    gnus-newsgroup-data
9194                  (gnus-data-find-list
9195                   (gnus-summary-article-number)
9196                   (gnus-data-list backward))))
9197       (when (and (or (not unread)       ; We want all articles...
9198                      (gnus-data-unread-p d)) ; Or just unreads.
9199                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9200                  (if not-matching
9201                      (not (string-match
9202                            regexp
9203                            (funcall func (gnus-data-header d))))
9204                    (string-match regexp
9205                                  (funcall func (gnus-data-header d)))))
9206         (push (gnus-data-number d) articles))) ; Success!
9207     (nreverse articles)))
9208
9209 (defun gnus-summary-execute-command (header regexp command &optional backward)
9210   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9211 If HEADER is an empty string (or nil), the match is done on the entire
9212 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9213   (interactive
9214    (list (let ((completion-ignore-case t))
9215            (completing-read
9216             "Header name: "
9217             (mapcar (lambda (header) (list (format "%s" header)))
9218                     (append
9219                      '("Number" "Subject" "From" "Lines" "Date"
9220                        "Message-ID" "Xref" "References" "Body")
9221                      gnus-extra-headers))
9222             nil 'require-match))
9223          (read-string "Regexp: ")
9224          (read-key-sequence "Command: ")
9225          current-prefix-arg))
9226   (when (equal header "Body")
9227     (setq header ""))
9228   ;; Hidden thread subtrees must be searched as well.
9229   (gnus-summary-show-all-threads)
9230   ;; We don't want to change current point nor window configuration.
9231   (save-excursion
9232     (save-window-excursion
9233       (let (gnus-visual
9234             gnus-treat-strip-trailing-blank-lines
9235             gnus-treat-strip-leading-blank-lines
9236             gnus-treat-strip-multiple-blank-lines
9237             gnus-treat-hide-boring-headers
9238             gnus-treat-fold-newsgroups
9239             gnus-article-prepare-hook)
9240         (gnus-message 6 "Executing %s..." (key-description command))
9241         ;; We'd like to execute COMMAND interactively so as to give arguments.
9242         (gnus-execute header regexp
9243                       `(call-interactively ',(key-binding command))
9244                       backward)
9245         (gnus-message 6 "Executing %s...done" (key-description command))))))
9246
9247 (defun gnus-summary-beginning-of-article ()
9248   "Scroll the article back to the beginning."
9249   (interactive)
9250   (gnus-summary-select-article)
9251   (gnus-configure-windows 'article)
9252   (gnus-eval-in-buffer-window gnus-article-buffer
9253     (widen)
9254     (goto-char (point-min))
9255     (when gnus-break-pages
9256       (gnus-narrow-to-page))))
9257
9258 (defun gnus-summary-end-of-article ()
9259   "Scroll to the end of the article."
9260   (interactive)
9261   (gnus-summary-select-article)
9262   (gnus-configure-windows 'article)
9263   (gnus-eval-in-buffer-window gnus-article-buffer
9264     (widen)
9265     (goto-char (point-max))
9266     (recenter -3)
9267     (when gnus-break-pages
9268       (gnus-narrow-to-page))))
9269
9270 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9271   "Truncate to LEN and quote all \"(\"'s in STRING."
9272   (gnus-replace-in-string (if (and len (> (length string) len))
9273                               (substring string 0 len)
9274                             string)
9275                           "[()]" "\\\\\\&"))
9276
9277 (defun gnus-summary-print-article (&optional filename n)
9278   "Generate and print a PostScript image of the process-marked (mail) articles.
9279
9280 If used interactively, print the current article if none are
9281 process-marked.  With prefix arg, prompt the user for the name of the
9282 file to save in.
9283
9284 When used from Lisp, accept two optional args FILENAME and N.  N means
9285 to print the next N articles.  If N is negative, print the N previous
9286 articles.  If N is nil and articles have been marked with the process
9287 mark, print these instead.
9288
9289 If the optional first argument FILENAME is nil, send the image to the
9290 printer.  If FILENAME is a string, save the PostScript image in a file with
9291 that name.  If FILENAME is a number, prompt the user for the name of the file
9292 to save in."
9293   (interactive (list (ps-print-preprint current-prefix-arg)))
9294   (dolist (article (gnus-summary-work-articles n))
9295     (gnus-summary-select-article nil nil 'pseudo article)
9296     (gnus-eval-in-buffer-window gnus-article-buffer
9297       (gnus-print-buffer))
9298     (gnus-summary-remove-process-mark article))
9299   (ps-despool filename))
9300
9301 (defun gnus-print-buffer ()
9302   (let ((buffer (generate-new-buffer " *print*")))
9303     (unwind-protect
9304         (progn
9305           (copy-to-buffer buffer (point-min) (point-max))
9306           (set-buffer buffer)
9307           (gnus-remove-text-with-property 'gnus-decoration)
9308           (when (gnus-visual-p 'article-highlight 'highlight)
9309             ;; Copy-to-buffer doesn't copy overlay.  So redo
9310             ;; highlight.
9311             (let ((gnus-article-buffer buffer))
9312               (gnus-article-highlight-citation t)
9313               (gnus-article-highlight-signature)
9314               (gnus-article-emphasize)
9315               (gnus-article-delete-invisible-text)))
9316           (let ((ps-left-header
9317                  (list
9318                   (concat "("
9319                           (gnus-summary-print-truncate-and-quote
9320                            (mail-header-subject gnus-current-headers)
9321                            66) ")")
9322                   (concat "("
9323                           (gnus-summary-print-truncate-and-quote
9324                            (mail-header-from gnus-current-headers)
9325                            45) ")")))
9326                 (ps-right-header
9327                  (list
9328                   "/pagenumberstring load"
9329                   (concat "("
9330                           (mail-header-date gnus-current-headers) ")"))))
9331             (gnus-run-hooks 'gnus-ps-print-hook)
9332             (save-excursion
9333               (if ps-print-color-p
9334                   (ps-spool-buffer-with-faces)
9335                 (ps-spool-buffer)))))
9336       (kill-buffer buffer))))
9337
9338 (defun gnus-summary-show-article (&optional arg)
9339   "Force redisplaying of the current article.
9340 If ARG (the prefix) is a number, show the article with the charset
9341 defined in `gnus-summary-show-article-charset-alist', or the charset
9342 input.
9343 If ARG (the prefix) is non-nil and not a number, show the raw article
9344 without any article massaging functions being run.  Normally, the key
9345 strokes are `C-u g'."
9346   (interactive "P")
9347   (cond
9348    ((numberp arg)
9349     (gnus-summary-show-article t)
9350     (let ((gnus-newsgroup-charset
9351            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9352                (mm-read-coding-system
9353                 "View as charset: " ;; actually it is coding system.
9354                 (with-current-buffer gnus-article-buffer
9355                   (mm-detect-coding-region (point) (point-max))))))
9356           (gnus-newsgroup-ignored-charsets 'gnus-all))
9357       (gnus-summary-select-article nil 'force)
9358       (let ((deps gnus-newsgroup-dependencies)
9359             head header lines)
9360         (with-current-buffer gnus-original-article-buffer
9361           (save-restriction
9362             (message-narrow-to-head)
9363             (setq head (buffer-string))
9364             (goto-char (point-min))
9365             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9366               (goto-char (point-max))
9367               (widen)
9368               (setq lines (1- (count-lines (point) (point-max))))))
9369           (with-temp-buffer
9370             (insert (format "211 %d Article retrieved.\n"
9371                             (cdr gnus-article-current)))
9372             (insert head)
9373             (if lines (insert (format "Lines: %d\n" lines)))
9374             (insert ".\n")
9375             (let ((nntp-server-buffer (current-buffer)))
9376               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9377         (gnus-data-set-header
9378          (gnus-data-find (cdr gnus-article-current))
9379          header)
9380         (gnus-summary-update-article-line
9381          (cdr gnus-article-current) header)
9382         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9383           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9384    ((not arg)
9385     ;; Select the article the normal way.
9386     (gnus-summary-select-article nil 'force))
9387    (t
9388     ;; We have to require this here to make sure that the following
9389     ;; dynamic binding isn't shadowed by autoloading.
9390     (require 'gnus-async)
9391     (require 'gnus-art)
9392     ;; Bind the article treatment functions to nil.
9393     (let ((gnus-have-all-headers t)
9394           gnus-article-prepare-hook
9395           gnus-article-decode-hook
9396           gnus-display-mime-function
9397           gnus-break-pages)
9398       ;; Destroy any MIME parts.
9399       (when (gnus-buffer-live-p gnus-article-buffer)
9400         (with-current-buffer gnus-article-buffer
9401           (mm-destroy-parts gnus-article-mime-handles)
9402           ;; Set it to nil for safety reason.
9403           (setq gnus-article-mime-handle-alist nil)
9404           (setq gnus-article-mime-handles nil)))
9405       (gnus-summary-select-article nil 'force))))
9406   (gnus-summary-goto-subject gnus-current-article)
9407   (gnus-summary-position-point))
9408
9409 (defun gnus-summary-show-raw-article ()
9410   "Show the raw article without any article massaging functions being run."
9411   (interactive)
9412   (gnus-summary-show-article t))
9413
9414 (defun gnus-summary-verbose-headers (&optional arg)
9415   "Toggle permanent full header display.
9416 If ARG is a positive number, turn header display on.
9417 If ARG is a negative number, turn header display off."
9418   (interactive "P")
9419   (setq gnus-show-all-headers
9420         (cond ((or (not (numberp arg))
9421                    (zerop arg))
9422                (not gnus-show-all-headers))
9423               ((natnump arg)
9424                t)))
9425   (gnus-summary-show-article))
9426
9427 (defun gnus-summary-toggle-header (&optional arg)
9428   "Show the headers if they are hidden, or hide them if they are shown.
9429 If ARG is a positive number, show the entire header.
9430 If ARG is a negative number, hide the unwanted header lines."
9431   (interactive "P")
9432   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9433                      (get-buffer-window gnus-article-buffer t))))
9434     (with-current-buffer gnus-article-buffer
9435       (widen)
9436       (article-narrow-to-head)
9437       (let* ((inhibit-read-only t)
9438              (inhibit-point-motion-hooks t)
9439              (hidden (if (numberp arg)
9440                          (>= arg 0)
9441                        (or (not (looking-at "[^ \t\n]+:"))
9442                            (gnus-article-hidden-text-p 'headers))))
9443              s e)
9444         (delete-region (point-min) (point-max))
9445         (with-current-buffer gnus-original-article-buffer
9446           (goto-char (setq s (point-min)))
9447           (setq e (if (search-forward "\n\n" nil t)
9448                       (1- (point))
9449                     (point-max))))
9450         (insert-buffer-substring gnus-original-article-buffer s e)
9451         (run-hooks 'gnus-article-decode-hook)
9452         (if hidden
9453             (let ((gnus-treat-hide-headers nil)
9454                   (gnus-treat-hide-boring-headers nil))
9455               (gnus-delete-wash-type 'headers)
9456               (gnus-treat-article 'head))
9457           (gnus-treat-article 'head))
9458         (widen)
9459         (if window
9460             (set-window-start window (goto-char (point-min))))
9461         (if gnus-break-pages
9462             (gnus-narrow-to-page)
9463           (when (gnus-visual-p 'page-marker)
9464             (let ((inhibit-read-only t))
9465               (gnus-remove-text-with-property 'gnus-prev)
9466               (gnus-remove-text-with-property 'gnus-next))))
9467         (gnus-set-mode-line 'article)))))
9468
9469 (defun gnus-summary-show-all-headers ()
9470   "Make all header lines visible."
9471   (interactive)
9472   (gnus-summary-toggle-header 1))
9473
9474 (defun gnus-summary-caesar-message (&optional arg)
9475   "Caesar rotate the current article by 13.
9476 With a non-numerical prefix, also rotate headers.  A numerical
9477 prefix specifies how many places to rotate each letter forward."
9478   (interactive "P")
9479   (gnus-summary-select-article)
9480   (let ((mail-header-separator ""))
9481     (gnus-eval-in-buffer-window gnus-article-buffer
9482       (save-restriction
9483         (widen)
9484         (let ((start (window-start))
9485               (inhibit-read-only t))
9486           (if (equal arg '(4))
9487               (message-caesar-buffer-body nil t)
9488             (message-caesar-buffer-body arg))
9489           (set-window-start (get-buffer-window (current-buffer)) start)))))
9490   ;; Create buttons and stuff...
9491   (gnus-treat-article nil))
9492
9493 (declare-function idna-to-unicode "ext:idna" (str))
9494
9495 (defun gnus-summary-idna-message (&optional arg)
9496   "Decode IDNA encoded domain names in the current articles.
9497 IDNA encoded domain names looks like `xn--bar'.  If a string
9498 remain unencoded after running this function, it is likely an
9499 invalid IDNA string (`xn--bar' is invalid).
9500
9501 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9502 installed for this command to work."
9503   (interactive "P")
9504   (if (not (and (condition-case nil (require 'idna)
9505                   (file-error))
9506                 (mm-coding-system-p 'utf-8)
9507                 (executable-find (symbol-value 'idna-program))))
9508       (gnus-message
9509        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9510     (gnus-summary-select-article)
9511     (let ((mail-header-separator ""))
9512       (gnus-eval-in-buffer-window gnus-article-buffer
9513         (save-restriction
9514           (widen)
9515           (let ((start (window-start))
9516                 buffer-read-only)
9517             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9518               (replace-match (idna-to-unicode (match-string 1))))
9519             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9520
9521 (defun gnus-summary-morse-message (&optional arg)
9522   "Morse decode the current article."
9523   (interactive "P")
9524   (gnus-summary-select-article)
9525   (let ((mail-header-separator ""))
9526     (gnus-eval-in-buffer-window gnus-article-buffer
9527       (save-excursion
9528         (save-restriction
9529           (widen)
9530           (let ((pos (window-start))
9531                 (inhibit-read-only t))
9532             (goto-char (point-min))
9533             (when (message-goto-body)
9534               (gnus-narrow-to-body))
9535             (goto-char (point-min))
9536             (while (search-forward "·" (point-max) t)
9537               (replace-match "."))
9538             (unmorse-region (point-min) (point-max))
9539             (widen)
9540             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9541
9542 (defun gnus-summary-stop-page-breaking ()
9543   "Stop page breaking in the current article."
9544   (interactive)
9545   (gnus-summary-select-article)
9546   (gnus-eval-in-buffer-window gnus-article-buffer
9547     (widen)
9548     (when (gnus-visual-p 'page-marker)
9549       (let ((inhibit-read-only t))
9550         (gnus-remove-text-with-property 'gnus-prev)
9551         (gnus-remove-text-with-property 'gnus-next))
9552       (setq gnus-page-broken nil))))
9553
9554 (defun gnus-summary-move-article (&optional n to-newsgroup
9555                                             select-method action)
9556   "Move the current article to a different newsgroup.
9557 If N is a positive number, move the N next articles.
9558 If N is a negative number, move the N previous articles.
9559 If N is nil and any articles have been marked with the process mark,
9560 move those articles instead.
9561 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9562 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9563 re-spool using this method.
9564
9565 When called interactively with TO-NEWSGROUP being nil, the value of
9566 the variable `gnus-move-split-methods' is used for finding a default
9567 for the target newsgroup.
9568
9569 For this function to work, both the current newsgroup and the
9570 newsgroup that you want to move to have to support the `request-move'
9571 and `request-accept' functions.
9572
9573 ACTION can be either `move' (the default), `crosspost' or `copy'."
9574   (interactive "P")
9575   (unless action
9576     (setq action 'move))
9577   ;; Check whether the source group supports the required functions.
9578   (cond ((and (eq action 'move)
9579               (not (gnus-check-backend-function
9580                     'request-move-article gnus-newsgroup-name)))
9581          (error "The current group does not support article moving"))
9582         ((and (eq action 'crosspost)
9583               (not (gnus-check-backend-function
9584                     'request-replace-article gnus-newsgroup-name)))
9585          (error "The current group does not support article editing")))
9586   (let ((articles (gnus-summary-work-articles n))
9587         (prefix (if (gnus-check-backend-function
9588                      'request-move-article gnus-newsgroup-name)
9589                     (funcall gnus-move-group-prefix-function
9590                              gnus-newsgroup-name)
9591                   ""))
9592         (names '((move "Move" "Moving")
9593                  (copy "Copy" "Copying")
9594                  (crosspost "Crosspost" "Crossposting")))
9595         (copy-buf (save-excursion
9596                     (nnheader-set-temp-buffer " *copy article*")))
9597         art-group to-method new-xref article to-groups
9598         articles-to-update-marks encoded)
9599     (unless (assq action names)
9600       (error "Unknown action %s" action))
9601     ;; Read the newsgroup name.
9602     (when (and (not to-newsgroup)
9603                (not select-method))
9604       (if (and gnus-move-split-methods
9605                (not
9606                 (and (memq gnus-current-article articles)
9607                      (gnus-buffer-live-p gnus-original-article-buffer))))
9608           ;; When `gnus-move-split-methods' is non-nil, we have to
9609           ;; select an article to give `gnus-read-move-group-name' an
9610           ;; opportunity to suggest an appropriate default.  However,
9611           ;; we needn't render or mark the article.
9612           (let ((gnus-display-mime-function nil)
9613                 (gnus-article-prepare-hook nil)
9614                 (gnus-mark-article-hook nil))
9615             (gnus-summary-select-article nil nil nil (car articles))))
9616       (setq to-newsgroup (gnus-read-move-group-name
9617                           (cadr (assq action names))
9618                           (symbol-value
9619                            (intern (format "gnus-current-%s-group" action)))
9620                           articles prefix)
9621             encoded to-newsgroup
9622             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9623       (set (intern (format "gnus-current-%s-group" action))
9624            (mm-decode-coding-string
9625             to-newsgroup
9626             (gnus-group-name-charset to-method to-newsgroup))))
9627     (unless to-method
9628       (setq to-method (or select-method
9629                           (gnus-server-to-method
9630                            (gnus-group-method to-newsgroup)))))
9631     (setq to-newsgroup
9632           (or encoded
9633               (and to-newsgroup
9634                    (mm-encode-coding-string
9635                     to-newsgroup
9636                     (gnus-group-name-charset to-method to-newsgroup)))))
9637     ;; Check the method we are to move this article to...
9638     (unless (gnus-check-backend-function
9639              'request-accept-article (car to-method))
9640       (error "%s does not support article copying" (car to-method)))
9641     (unless (gnus-check-server to-method)
9642       (error "Can't open server %s" (car to-method)))
9643     (gnus-message 6 "%s to %s: %s..."
9644                   (caddr (assq action names))
9645                   (or (car select-method)
9646                       (gnus-group-decoded-name to-newsgroup))
9647                   articles)
9648     (while articles
9649       (setq article (pop articles))
9650       (setq
9651        art-group
9652        (cond
9653         ;; Move the article.
9654         ((eq action 'move)
9655          ;; Remove this article from future suppression.
9656          (gnus-dup-unsuppress-article article)
9657          (let* ((from-method (gnus-find-method-for-group
9658                               gnus-newsgroup-name))
9659                 (to-method (or select-method
9660                                (gnus-find-method-for-group to-newsgroup)))
9661                 (move-is-internal (gnus-method-equal from-method to-method)))
9662            (gnus-request-move-article
9663             article                     ; Article to move
9664             gnus-newsgroup-name         ; From newsgroup
9665             (nth 1 (gnus-find-method-for-group
9666                     gnus-newsgroup-name)) ; Server
9667             (list 'gnus-request-accept-article
9668                   to-newsgroup (list 'quote select-method)
9669                   (not articles) t)     ; Accept form
9670             (not articles)              ; Only save nov last time
9671             move-is-internal)))         ; is this move internal?
9672         ;; Copy the article.
9673         ((eq action 'copy)
9674          (with-current-buffer copy-buf
9675            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9676              (save-restriction
9677                (nnheader-narrow-to-headers)
9678                (dolist (hdr gnus-copy-article-ignored-headers)
9679                  (message-remove-header hdr t)))
9680              (gnus-request-accept-article
9681               to-newsgroup select-method (not articles) t))))
9682         ;; Crosspost the article.
9683         ((eq action 'crosspost)
9684          (let ((xref (message-tokenize-header
9685                       (mail-header-xref (gnus-summary-article-header article))
9686                       " ")))
9687            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9688                                   ":" (number-to-string article)))
9689            (unless xref
9690              (setq xref (list (system-name))))
9691            (setq new-xref
9692                  (concat
9693                   (mapconcat 'identity
9694                              (delete "Xref:" (delete new-xref xref))
9695                              " ")
9696                   " " new-xref))
9697            (with-current-buffer copy-buf
9698              ;; First put the article in the destination group.
9699              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9700              (when (consp (setq art-group
9701                                 (gnus-request-accept-article
9702                                  to-newsgroup select-method (not articles) t)))
9703                (setq new-xref (concat new-xref " " (car art-group)
9704                                       ":"
9705                                       (number-to-string (cdr art-group))))
9706                ;; Now we have the new Xrefs header, so we insert
9707                ;; it and replace the new article.
9708                (nnheader-replace-header "Xref" new-xref)
9709                (gnus-request-replace-article
9710                 (cdr art-group) to-newsgroup (current-buffer) t)
9711                art-group))))))
9712       (cond
9713        ((not art-group)
9714         (gnus-message 1 "Couldn't %s article %s: %s"
9715                       (cadr (assq action names)) article
9716                       (nnheader-get-report (car to-method))))
9717        ((eq art-group 'junk)
9718         (when (eq action 'move)
9719           (gnus-summary-mark-article article gnus-canceled-mark)
9720           (gnus-message 4 "Deleted article %s" article)
9721           ;; run the delete hook
9722           (run-hook-with-args 'gnus-summary-article-delete-hook
9723                               action
9724                               (gnus-data-header
9725                                (assoc article (gnus-data-list nil)))
9726                               gnus-newsgroup-name nil
9727                               select-method)))
9728        (t
9729         (let* ((pto-group (gnus-group-prefixed-name
9730                            (car art-group) to-method))
9731                (info (gnus-get-info pto-group))
9732                (to-group (gnus-info-group info))
9733                to-marks)
9734           ;; Update the group that has been moved to.
9735           (when (and info
9736                      (memq action '(move copy)))
9737             (unless (member to-group to-groups)
9738               (push to-group to-groups))
9739
9740             (unless (memq article gnus-newsgroup-unreads)
9741               (push 'read to-marks)
9742               (gnus-info-set-read
9743                info (gnus-add-to-range (gnus-info-read info)
9744                                        (list (cdr art-group)))))
9745
9746             ;; See whether the article is to be put in the cache.
9747             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9748                              gnus-article-mark-lists
9749                            (delete '(expirable . expire)
9750                                    (copy-sequence gnus-article-mark-lists))))
9751                   (to-article (cdr art-group)))
9752
9753               ;; Enter the article into the cache in the new group,
9754               ;; if that is required.
9755               (when gnus-use-cache
9756                 (gnus-cache-possibly-enter-article
9757                  to-group to-article
9758                  (memq article gnus-newsgroup-marked)
9759                  (memq article gnus-newsgroup-dormant)
9760                  (memq article gnus-newsgroup-unreads)))
9761
9762               (when gnus-preserve-marks
9763                 ;; Copy any marks over to the new group.
9764                 (when (and (equal to-group gnus-newsgroup-name)
9765                            (not (memq article gnus-newsgroup-unreads)))
9766                   ;; Mark this article as read in this group.
9767                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9768                   (setcdr (gnus-active to-group) to-article)
9769                   (setcdr gnus-newsgroup-active to-article))
9770
9771                 (while marks
9772                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9773                     (when (memq article (symbol-value
9774                                          (intern (format "gnus-newsgroup-%s"
9775                                                          (caar marks)))))
9776                       (push (cdar marks) to-marks)
9777                       ;; If the other group is the same as this group,
9778                       ;; then we have to add the mark to the list.
9779                       (when (equal to-group gnus-newsgroup-name)
9780                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9781                              (cons to-article
9782                                    (symbol-value
9783                                     (intern (format "gnus-newsgroup-%s"
9784                                                     (caar marks)))))))
9785                       ;; Copy the marks to other group.
9786                       (gnus-add-marked-articles
9787                        to-group (cdar marks) (list to-article) info)))
9788                   (setq marks (cdr marks)))
9789
9790                 (gnus-request-set-mark
9791                  to-group (list (list (list to-article) 'add to-marks))))
9792
9793               (gnus-dribble-enter
9794                (concat "(gnus-group-set-info '"
9795                        (gnus-prin1-to-string (gnus-get-info to-group))
9796                        ")"))))
9797
9798           ;; Update the Xref header in this article to point to
9799           ;; the new crossposted article we have just created.
9800           (when (eq action 'crosspost)
9801             (with-current-buffer copy-buf
9802               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9803               (nnheader-replace-header "Xref" new-xref)
9804               (gnus-request-replace-article
9805                article gnus-newsgroup-name (current-buffer) t)))
9806
9807           ;; run the move/copy/crosspost/respool hook
9808           (run-hook-with-args 'gnus-summary-article-move-hook
9809                               action
9810                               (gnus-data-header
9811                                (assoc article (gnus-data-list nil)))
9812                               gnus-newsgroup-name
9813                               to-newsgroup
9814                               select-method))
9815
9816         ;;;!!!Why is this necessary?
9817         (set-buffer gnus-summary-buffer)
9818
9819         (gnus-summary-goto-subject article)
9820         (when (eq action 'move)
9821           (gnus-summary-mark-article article gnus-canceled-mark))))
9822       (push article articles-to-update-marks))
9823
9824     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9825     ;; Re-activate all groups that have been moved to.
9826     (with-current-buffer gnus-group-buffer
9827       (let ((gnus-group-marked to-groups))
9828         (gnus-group-get-new-news-this-group nil t)))
9829
9830     (gnus-kill-buffer copy-buf)
9831     (gnus-summary-position-point)
9832     (gnus-set-mode-line 'summary)))
9833
9834 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9835   "Copy the current article to some other group.
9836 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9837 When called interactively, if TO-NEWSGROUP is nil, use the value of
9838 the variable `gnus-move-split-methods' for finding a default target
9839 newsgroup.
9840 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9841 re-spool using this method."
9842   (interactive "P")
9843   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9844
9845 (defun gnus-summary-crosspost-article (&optional n)
9846   "Crosspost the current article to some other group."
9847   (interactive "P")
9848   (gnus-summary-move-article n nil nil 'crosspost))
9849
9850 (defcustom gnus-summary-respool-default-method nil
9851   "Default method type for respooling an article.
9852 If nil, use to the current newsgroup method."
9853   :type 'symbol
9854   :group 'gnus-summary-mail)
9855
9856 (defun gnus-summary-respool-article (&optional n method)
9857   "Respool the current article.
9858 The article will be squeezed through the mail spooling process again,
9859 which means that it will be put in some mail newsgroup or other
9860 depending on `nnmail-split-methods'.
9861 If N is a positive number, respool the N next articles.
9862 If N is a negative number, respool the N previous articles.
9863 If N is nil and any articles have been marked with the process mark,
9864 respool those articles instead.
9865
9866 Respooling can be done both from mail groups and \"real\" newsgroups.
9867 In the former case, the articles in question will be moved from the
9868 current group into whatever groups they are destined to.  In the
9869 latter case, they will be copied into the relevant groups."
9870   (interactive
9871    (list current-prefix-arg
9872          (let* ((methods (gnus-methods-using 'respool))
9873                 (methname
9874                  (symbol-name (or gnus-summary-respool-default-method
9875                                   (car (gnus-find-method-for-group
9876                                         gnus-newsgroup-name)))))
9877                 (method
9878                  (gnus-completing-read-with-default
9879                   methname "Backend to use when respooling"
9880                   methods nil t nil 'gnus-mail-method-history))
9881                 ms)
9882            (cond
9883             ((zerop (length (setq ms (gnus-servers-using-backend
9884                                       (intern method)))))
9885              (list (intern method) ""))
9886             ((= 1 (length ms))
9887              (car ms))
9888             (t
9889              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9890                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9891                            ms-alist))))))))
9892   (unless method
9893     (error "No method given for respooling"))
9894   (if (assoc (symbol-name
9895               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9896              (gnus-methods-using 'respool))
9897       (gnus-summary-move-article n nil method)
9898     (gnus-summary-copy-article n nil method)))
9899
9900 (defun gnus-summary-import-article (file &optional edit)
9901   "Import an arbitrary file into a mail newsgroup."
9902   (interactive "fImport file: \nP")
9903   (let ((group gnus-newsgroup-name)
9904         (now (current-time))
9905         atts lines group-art)
9906     (unless (gnus-check-backend-function 'request-accept-article group)
9907       (error "%s does not support article importing" group))
9908     (or (file-readable-p file)
9909         (not (file-regular-p file))
9910         (error "Can't read %s" file))
9911     (with-current-buffer (gnus-get-buffer-create " *import file*")
9912       (erase-buffer)
9913       (nnheader-insert-file-contents file)
9914       (goto-char (point-min))
9915       (if (nnheader-article-p)
9916           (save-restriction
9917             (goto-char (point-min))
9918             (search-forward "\n\n" nil t)
9919             (narrow-to-region (point-min) (1- (point)))
9920             (goto-char (point-min))
9921             (unless (re-search-forward "^date:" nil t)
9922               (goto-char (point-max))
9923               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9924        ;; This doesn't look like an article, so we fudge some headers.
9925         (setq atts (file-attributes file)
9926               lines (count-lines (point-min) (point-max)))
9927         (insert "From: " (read-string "From: ") "\n"
9928                 "Subject: " (read-string "Subject: ") "\n"
9929                 "Date: " (message-make-date (nth 5 atts)) "\n"
9930                 "Message-ID: " (message-make-message-id) "\n"
9931                 "Lines: " (int-to-string lines) "\n"
9932                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9933       (setq group-art (gnus-request-accept-article group nil t))
9934       (kill-buffer (current-buffer)))
9935     (setq gnus-newsgroup-active (gnus-activate-group group))
9936     (forward-line 1)
9937     (gnus-summary-goto-article (cdr group-art) nil t)
9938     (when edit
9939       (gnus-summary-edit-article))))
9940
9941 (defun gnus-summary-create-article ()
9942   "Create an article in a mail newsgroup."
9943   (interactive)
9944   (let ((group gnus-newsgroup-name)
9945         (now (current-time))
9946         group-art)
9947     (unless (gnus-check-backend-function 'request-accept-article group)
9948       (error "%s does not support article importing" group))
9949     (with-current-buffer (gnus-get-buffer-create " *import file*")
9950       (erase-buffer)
9951       (goto-char (point-min))
9952       ;; This doesn't look like an article, so we fudge some headers.
9953       (insert "From: " (read-string "From: ") "\n"
9954               "Subject: " (read-string "Subject: ") "\n"
9955               "Date: " (message-make-date now) "\n"
9956               "Message-ID: " (message-make-message-id) "\n")
9957       (setq group-art (gnus-request-accept-article group nil t))
9958       (kill-buffer (current-buffer)))
9959     (setq gnus-newsgroup-active (gnus-activate-group group))
9960     (forward-line 1)
9961     (gnus-summary-goto-article (cdr group-art) nil t)
9962     (gnus-summary-edit-article)))
9963
9964 (defun gnus-summary-article-posted-p ()
9965   "Say whether the current (mail) article is available from news as well.
9966 This will be the case if the article has both been mailed and posted."
9967   (interactive)
9968   (let ((id (mail-header-references (gnus-summary-article-header)))
9969         (gnus-override-method (car (gnus-refer-article-methods))))
9970     (if (gnus-request-head id "")
9971         (gnus-message 2 "The current message was found on %s"
9972                       gnus-override-method)
9973       (gnus-message 2 "The current message couldn't be found on %s"
9974                     gnus-override-method)
9975       nil)))
9976
9977 (defun gnus-summary-expire-articles (&optional now)
9978   "Expire all articles that are marked as expirable in the current group."
9979   (interactive)
9980   (when (and (not gnus-group-is-exiting-without-update-p)
9981              (gnus-check-backend-function
9982               'request-expire-articles gnus-newsgroup-name))
9983     ;; This backend supports expiry.
9984     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9985            (expirable (if total
9986                           (progn
9987                             ;; We need to update the info for
9988                             ;; this group for `gnus-list-of-read-articles'
9989                             ;; to give us the right answer.
9990                             (gnus-run-hooks 'gnus-exit-group-hook)
9991                             (gnus-summary-update-info)
9992                             (gnus-list-of-read-articles gnus-newsgroup-name))
9993                         (setq gnus-newsgroup-expirable
9994                               (sort gnus-newsgroup-expirable '<))))
9995            (expiry-wait (if now 'immediate
9996                           (gnus-group-find-parameter
9997                            gnus-newsgroup-name 'expiry-wait)))
9998            (nnmail-expiry-target
9999             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10000                 nnmail-expiry-target))
10001            es)
10002       (when expirable
10003         ;; There are expirable articles in this group, so we run them
10004         ;; through the expiry process.
10005         (gnus-message 6 "Expiring articles...")
10006         (unless (gnus-check-group gnus-newsgroup-name)
10007           (error "Can't open server for %s" gnus-newsgroup-name))
10008         ;; The list of articles that weren't expired is returned.
10009         (save-excursion
10010           (if expiry-wait
10011               (let ((nnmail-expiry-wait-function nil)
10012                     (nnmail-expiry-wait expiry-wait))
10013                 (setq es (gnus-request-expire-articles
10014                           expirable gnus-newsgroup-name)))
10015             (setq es (gnus-request-expire-articles
10016                       expirable gnus-newsgroup-name)))
10017           (unless total
10018             (setq gnus-newsgroup-expirable es))
10019           ;; We go through the old list of expirable, and mark all
10020           ;; really expired articles as nonexistent.
10021           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10022             (let ((gnus-use-cache nil))
10023               (dolist (article expirable)
10024                 (when (and (not (memq article es))
10025                            (gnus-data-find article))
10026                   (gnus-summary-mark-article article gnus-canceled-mark)
10027                   (run-hook-with-args 'gnus-summary-article-expire-hook
10028                                       'delete
10029                                       (gnus-data-header
10030                                        (assoc article (gnus-data-list nil)))
10031                                       gnus-newsgroup-name
10032                                       nil
10033                                       nil))))))
10034         (gnus-message 6 "Expiring articles...done")))))
10035
10036 (defun gnus-summary-expire-articles-now ()
10037   "Expunge all expirable articles in the current group.
10038 This means that *all* articles that are marked as expirable will be
10039 deleted forever, right now."
10040   (interactive)
10041   (or gnus-expert-user
10042       (gnus-yes-or-no-p
10043        "Are you really, really sure you want to delete all expirable messages? ")
10044       (error "Phew!"))
10045   (gnus-summary-expire-articles t))
10046
10047 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10048 (defun gnus-summary-delete-article (&optional n)
10049   "Delete the N next (mail) articles.
10050 This command actually deletes articles.  This is not a marking
10051 command.  The article will disappear forever from your life, never to
10052 return.
10053
10054 If N is negative, delete backwards.
10055 If N is nil and articles have been marked with the process mark,
10056 delete these instead.
10057
10058 If `gnus-novice-user' is non-nil you will be asked for
10059 confirmation before the articles are deleted."
10060   (interactive "P")
10061   (unless (gnus-check-backend-function 'request-expire-articles
10062                                        gnus-newsgroup-name)
10063     (error "The current newsgroup does not support article deletion"))
10064   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10065     (error "Couldn't open server"))
10066   ;; Compute the list of articles to delete.
10067   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10068         (nnmail-expiry-target 'delete)
10069         not-deleted)
10070     (if (and gnus-novice-user
10071              (not (gnus-yes-or-no-p
10072                    (format "Do you really want to delete %s forever? "
10073                            (if (> (length articles) 1)
10074                                (format "these %s articles" (length articles))
10075                              "this article")))))
10076         ()
10077       ;; Delete the articles.
10078       (setq not-deleted (gnus-request-expire-articles
10079                          articles gnus-newsgroup-name 'force))
10080       (while articles
10081         (gnus-summary-remove-process-mark (car articles))
10082         ;; The backend might not have been able to delete the article
10083         ;; after all.
10084         (unless (memq (car articles) not-deleted)
10085           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10086         (let* ((article (car articles))
10087                (ghead  (gnus-data-header
10088                                     (assoc article (gnus-data-list nil)))))
10089           (run-hook-with-args 'gnus-summary-article-delete-hook
10090                               'delete ghead gnus-newsgroup-name nil
10091                               nil))
10092         (setq articles (cdr articles)))
10093       (when not-deleted
10094         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10095     (gnus-summary-position-point)
10096     (gnus-set-mode-line 'summary)
10097     not-deleted))
10098
10099 (defun gnus-summary-edit-article (&optional arg)
10100   "Edit the current article.
10101 This will have permanent effect only in mail groups.
10102 If ARG is nil, edit the decoded articles.
10103 If ARG is 1, edit the raw articles.
10104 If ARG is 2, edit the raw articles even in read-only groups.
10105 If ARG is 3, edit the articles with the current handles.
10106 Otherwise, allow editing of articles even in read-only
10107 groups."
10108   (interactive "P")
10109   (let (force raw current-handles)
10110     (cond
10111      ((null arg))
10112      ((eq arg 1)
10113       (setq raw t))
10114      ((eq arg 2)
10115       (setq raw t
10116             force t))
10117      ((eq arg 3)
10118       (setq current-handles
10119             (and (gnus-buffer-live-p gnus-article-buffer)
10120                  (with-current-buffer gnus-article-buffer
10121                    (prog1
10122                        gnus-article-mime-handles
10123                      (setq gnus-article-mime-handles nil))))))
10124      (t
10125       (setq force t)))
10126     (when (and raw (not force)
10127                (member gnus-newsgroup-name '("nndraft:delayed"
10128                                              "nndraft:drafts"
10129                                              "nndraft:queue")))
10130       (error "Can't edit the raw article in group %s"
10131              gnus-newsgroup-name))
10132     (with-current-buffer gnus-summary-buffer
10133       (let ((mail-parse-charset gnus-newsgroup-charset)
10134             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10135         (gnus-set-global-variables)
10136         (when (and (not force)
10137                    (gnus-group-read-only-p))
10138           (error "The current newsgroup does not support article editing"))
10139         (gnus-summary-show-article t)
10140         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10141           (with-current-buffer gnus-article-buffer
10142             (mm-enable-multibyte)))
10143         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10144             (setq raw t))
10145         (gnus-article-edit-article
10146          (if raw 'ignore
10147            `(lambda ()
10148               (let ((mbl mml-buffer-list))
10149                 (setq mml-buffer-list nil)
10150                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10151                   (mime-to-mml ,'current-handles))
10152                 (let ((mbl1 mml-buffer-list))
10153                   (setq mml-buffer-list mbl)
10154                   (set (make-local-variable 'mml-buffer-list) mbl1))
10155                 (gnus-make-local-hook 'kill-buffer-hook)
10156                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10157          `(lambda (no-highlight)
10158             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10159                   (message-options message-options)
10160                   (message-options-set-recipient)
10161                   (mail-parse-ignored-charsets
10162                    ',gnus-newsgroup-ignored-charsets)
10163                   (rfc2047-header-encoding-alist
10164                    ',(let ((charset (gnus-group-name-charset
10165                                      (gnus-find-method-for-group
10166                                       gnus-newsgroup-name)
10167                                      gnus-newsgroup-name)))
10168                        (append (list (cons "Newsgroups" charset)
10169                                      (cons "Followup-To" charset)
10170                                      (cons "Xref" charset))
10171                                rfc2047-header-encoding-alist))))
10172               ,(if (not raw) '(progn
10173                                 (mml-to-mime)
10174                                 (mml-destroy-buffers)
10175                                 (remove-hook 'kill-buffer-hook
10176                                              'mml-destroy-buffers t)
10177                                 (kill-local-variable 'mml-buffer-list)))
10178               (gnus-summary-edit-article-done
10179                ,(or (mail-header-references gnus-current-headers) "")
10180                ,(gnus-group-read-only-p)
10181                ,gnus-summary-buffer no-highlight))))))))
10182
10183 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10184
10185 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10186                                                  no-highlight)
10187   "Make edits to the current article permanent."
10188   (interactive)
10189   (save-excursion
10190    ;; The buffer restriction contains the entire article if it exists.
10191     (when (article-goto-body)
10192       (let ((lines (count-lines (point) (point-max)))
10193             (length (- (point-max) (point)))
10194             (case-fold-search t)
10195             (body (copy-marker (point))))
10196         (goto-char (point-min))
10197         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10198           (delete-region (match-beginning 1) (match-end 1))
10199           (insert (number-to-string length)))
10200         (goto-char (point-min))
10201         (when (re-search-forward
10202                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10203           (delete-region (match-beginning 1) (match-end 1))
10204           (insert (number-to-string length)))
10205         (goto-char (point-min))
10206         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10207           (delete-region (match-beginning 1) (match-end 1))
10208           (insert (number-to-string lines))))))
10209   ;; Replace the article.
10210   (let ((buf (current-buffer)))
10211     (with-temp-buffer
10212       (insert-buffer-substring buf)
10213
10214       (if (and (not read-only)
10215                (not (gnus-request-replace-article
10216                      (cdr gnus-article-current) (car gnus-article-current)
10217                      (current-buffer) t)))
10218           (error "Couldn't replace article")
10219         ;; Update the summary buffer.
10220         (if (and references
10221                  (equal (message-tokenize-header references " ")
10222                         (message-tokenize-header
10223                          (or (message-fetch-field "references") "") " ")))
10224             ;; We only have to update this line.
10225             (save-excursion
10226               (save-restriction
10227                 (message-narrow-to-head)
10228                 (let ((head (buffer-substring-no-properties
10229                              (point-min) (point-max)))
10230                       header)
10231                   (with-temp-buffer
10232                     (insert (format "211 %d Article retrieved.\n"
10233                                     (cdr gnus-article-current)))
10234                     (insert head)
10235                     (insert ".\n")
10236                     (let ((nntp-server-buffer (current-buffer)))
10237                       (setq header (car (gnus-get-newsgroup-headers
10238                                          nil t))))
10239                     (with-current-buffer gnus-summary-buffer
10240                       (gnus-data-set-header
10241                        (gnus-data-find (cdr gnus-article-current))
10242                        header)
10243                       (gnus-summary-update-article-line
10244                        (cdr gnus-article-current) header)
10245                       (if (gnus-summary-goto-subject
10246                            (cdr gnus-article-current) nil t)
10247                           (gnus-summary-update-secondary-mark
10248                            (cdr gnus-article-current))))))))
10249           ;; Update threads.
10250           (set-buffer (or buffer gnus-summary-buffer))
10251           (gnus-summary-update-article (cdr gnus-article-current))
10252           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10253               (gnus-summary-update-secondary-mark
10254                (cdr gnus-article-current))))
10255         ;; Prettify the article buffer again.
10256         (unless no-highlight
10257           (with-current-buffer gnus-article-buffer
10258             ;;;!!! Fix this -- article should be rehighlighted.
10259             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10260             (set-buffer gnus-original-article-buffer)
10261             (gnus-request-article
10262              (cdr gnus-article-current)
10263              (car gnus-article-current) (current-buffer))))
10264         ;; Prettify the summary buffer line.
10265         (when (gnus-visual-p 'summary-highlight 'highlight)
10266           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10267
10268 (defun gnus-summary-edit-wash (key)
10269   "Perform editing command KEY in the article buffer."
10270   (interactive
10271    (list
10272     (progn
10273       (message "%s" (concat (this-command-keys) "- "))
10274       (read-char))))
10275   (message "")
10276   (gnus-summary-edit-article)
10277   (execute-kbd-macro (concat (this-command-keys) key))
10278   (gnus-article-edit-done))
10279
10280 ;;; Respooling
10281
10282 (defun gnus-summary-respool-query (&optional silent trace)
10283   "Query where the respool algorithm would put this article."
10284   (interactive)
10285   (let (gnus-mark-article-hook)
10286     (gnus-summary-select-article)
10287     (with-current-buffer gnus-original-article-buffer
10288       (let ((groups (nnmail-article-group 'identity trace)))
10289         (unless silent
10290           (if groups
10291               (message "This message would go to %s"
10292                        (mapconcat 'car groups ", "))
10293             (message "This message would go to no groups"))
10294           groups)))))
10295
10296 (defun gnus-summary-respool-trace ()
10297   "Trace where the respool algorithm would put this article.
10298 Display a buffer showing all fancy splitting patterns which matched."
10299   (interactive)
10300   (gnus-summary-respool-query nil t))
10301
10302 ;; Summary marking commands.
10303
10304 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10305   "Mark articles which has the same subject as read, and then select the next.
10306 If UNMARK is positive, remove any kind of mark.
10307 If UNMARK is negative, tick articles."
10308   (interactive "P")
10309   (when unmark
10310     (setq unmark (prefix-numeric-value unmark)))
10311   (let ((count
10312          (gnus-summary-mark-same-subject
10313           (gnus-summary-article-subject) unmark)))
10314     ;; Select next unread article.  If auto-select-same mode, should
10315     ;; select the first unread article.
10316     (gnus-summary-next-article t (and gnus-auto-select-same
10317                                       (gnus-summary-article-subject)))
10318     (gnus-message 7 "%d article%s marked as %s"
10319                   count (if (= count 1) " is" "s are")
10320                   (if unmark "unread" "read"))))
10321
10322 (defun gnus-summary-kill-same-subject (&optional unmark)
10323   "Mark articles which has the same subject as read.
10324 If UNMARK is positive, remove any kind of mark.
10325 If UNMARK is negative, tick articles."
10326   (interactive "P")
10327   (when unmark
10328     (setq unmark (prefix-numeric-value unmark)))
10329   (let ((count
10330          (gnus-summary-mark-same-subject
10331           (gnus-summary-article-subject) unmark)))
10332     ;; If marked as read, go to next unread subject.
10333     (when (null unmark)
10334       ;; Go to next unread subject.
10335       (gnus-summary-next-subject 1 t))
10336     (gnus-message 7 "%d articles are marked as %s"
10337                   count (if unmark "unread" "read"))))
10338
10339 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10340   "Mark articles with same SUBJECT as read, and return marked number.
10341 If optional argument UNMARK is positive, remove any kinds of marks.
10342 If optional argument UNMARK is negative, mark articles as unread instead."
10343   (let ((count 1))
10344     (save-excursion
10345       (cond
10346        ((null unmark)                   ; Mark as read.
10347         (while (and
10348                 (progn
10349                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10350                   (gnus-summary-show-thread) t)
10351                 (gnus-summary-find-subject subject))
10352           (setq count (1+ count))))
10353        ((> unmark 0)                    ; Tick.
10354         (while (and
10355                 (progn
10356                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10357                   (gnus-summary-show-thread) t)
10358                 (gnus-summary-find-subject subject))
10359           (setq count (1+ count))))
10360        (t                               ; Mark as unread.
10361         (while (and
10362                 (progn
10363                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10364                   (gnus-summary-show-thread) t)
10365                 (gnus-summary-find-subject subject))
10366           (setq count (1+ count)))))
10367       (gnus-set-mode-line 'summary)
10368       ;; Return the number of marked articles.
10369       count)))
10370
10371 (defun gnus-summary-mark-as-processable (n &optional unmark)
10372   "Set the process mark on the next N articles.
10373 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10374 the process mark instead.  The difference between N and the actual
10375 number of articles marked is returned."
10376   (interactive "P")
10377   (if (and (null n) (gnus-region-active-p))
10378       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10379     (setq n (prefix-numeric-value n))
10380     (let ((backward (< n 0))
10381           (n (abs n)))
10382       (while (and
10383               (> n 0)
10384               (if unmark
10385                   (gnus-summary-remove-process-mark
10386                    (gnus-summary-article-number))
10387                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10388               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10389         (setq n (1- n)))
10390       (when (/= 0 n)
10391         (gnus-message 7 "No more articles"))
10392       (gnus-summary-recenter)
10393       (gnus-summary-position-point)
10394       n)))
10395
10396 (defun gnus-summary-unmark-as-processable (n)
10397   "Remove the process mark from the next N articles.
10398 If N is negative, unmark backward instead.  The difference between N and
10399 the actual number of articles unmarked is returned."
10400   (interactive "P")
10401   (gnus-summary-mark-as-processable n t))
10402
10403 (defun gnus-summary-unmark-all-processable ()
10404   "Remove the process mark from all articles."
10405   (interactive)
10406   (save-excursion
10407     (while gnus-newsgroup-processable
10408       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10409   (gnus-summary-position-point))
10410
10411 (defun gnus-summary-add-mark (article type)
10412   "Mark ARTICLE with a mark of TYPE."
10413   (let ((vtype (car (assq type gnus-article-mark-lists)))
10414         var)
10415     (if (not vtype)
10416         (error "No such mark type: %s" type)
10417       (setq var (intern (format "gnus-newsgroup-%s" type)))
10418       (set var (cons article (symbol-value var)))
10419       (if (memq type '(processable cached replied forwarded recent saved))
10420           (gnus-summary-update-secondary-mark article)
10421         ;;; !!! This is bogus.  We should find out what primary
10422         ;;; !!! mark we want to set.
10423         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10424
10425 (defun gnus-summary-mark-as-expirable (n)
10426   "Mark N articles forward as expirable.
10427 If N is negative, mark backward instead.  The difference between N and
10428 the actual number of articles marked is returned."
10429   (interactive "p")
10430   (gnus-summary-mark-forward n gnus-expirable-mark))
10431
10432 (defun gnus-summary-mark-as-spam (n)
10433   "Mark N articles forward as spam.
10434 If N is negative, mark backward instead.  The difference between N and
10435 the actual number of articles marked is returned."
10436   (interactive "p")
10437   (gnus-summary-mark-forward n gnus-spam-mark))
10438
10439 (defun gnus-summary-mark-article-as-replied (article)
10440   "Mark ARTICLE as replied to and update the summary line.
10441 ARTICLE can also be a list of articles."
10442   (interactive (list (gnus-summary-article-number)))
10443   (let ((articles (if (listp article) article (list article))))
10444     (dolist (article articles)
10445       (unless (numberp article)
10446         (error "%s is not a number" article))
10447       (push article gnus-newsgroup-replied)
10448       (let ((inhibit-read-only t))
10449         (when (gnus-summary-goto-subject article nil t)
10450           (gnus-summary-update-secondary-mark article))))))
10451
10452 (defun gnus-summary-mark-article-as-forwarded (article)
10453   "Mark ARTICLE as forwarded and update the summary line.
10454 ARTICLE can also be a list of articles."
10455   (let ((articles (if (listp article) article (list article))))
10456     (dolist (article articles)
10457       (push article gnus-newsgroup-forwarded)
10458       (let ((inhibit-read-only t))
10459         (when (gnus-summary-goto-subject article nil t)
10460           (gnus-summary-update-secondary-mark article))))))
10461
10462 (defun gnus-summary-set-bookmark (article)
10463   "Set a bookmark in current article."
10464   (interactive (list (gnus-summary-article-number)))
10465   (when (or (not (get-buffer gnus-article-buffer))
10466             (not gnus-current-article)
10467             (not gnus-article-current)
10468             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10469     (error "No current article selected"))
10470   ;; Remove old bookmark, if one exists.
10471   (gnus-pull article gnus-newsgroup-bookmarks)
10472   ;; Set the new bookmark, which is on the form
10473   ;; (article-number . line-number-in-body).
10474   (push
10475    (cons article
10476          (with-current-buffer gnus-article-buffer
10477            (count-lines
10478             (min (point)
10479                  (save-excursion
10480                    (article-goto-body)
10481                    (point)))
10482             (point))))
10483    gnus-newsgroup-bookmarks)
10484   (gnus-message 6 "A bookmark has been added to the current article."))
10485
10486 (defun gnus-summary-remove-bookmark (article)
10487   "Remove the bookmark from the current article."
10488   (interactive (list (gnus-summary-article-number)))
10489   ;; Remove old bookmark, if one exists.
10490   (if (not (assq article gnus-newsgroup-bookmarks))
10491       (gnus-message 6 "No bookmark in current article.")
10492     (gnus-pull article gnus-newsgroup-bookmarks)
10493     (gnus-message 6 "Removed bookmark.")))
10494
10495 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10496 (defun gnus-summary-mark-as-dormant (n)
10497   "Mark N articles forward as dormant.
10498 If N is negative, mark backward instead.  The difference between N and
10499 the actual number of articles marked is returned."
10500   (interactive "p")
10501   (gnus-summary-mark-forward n gnus-dormant-mark))
10502
10503 (defun gnus-summary-set-process-mark (article)
10504   "Set the process mark on ARTICLE and update the summary line."
10505   (setq gnus-newsgroup-processable
10506         (cons article
10507               (delq article gnus-newsgroup-processable)))
10508   (when (gnus-summary-goto-subject article)
10509     (gnus-summary-show-thread)
10510     (gnus-summary-goto-subject article)
10511     (gnus-summary-update-secondary-mark article)))
10512
10513 (defun gnus-summary-remove-process-mark (&rest articles)
10514   "Remove the process mark from ARTICLES and update the summary line."
10515   (dolist (article articles)
10516     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10517     (when (gnus-summary-goto-subject article)
10518       (gnus-summary-show-thread)
10519       (gnus-summary-goto-subject article)
10520       (gnus-summary-update-secondary-mark article)))
10521   t)
10522
10523 (defun gnus-summary-set-saved-mark (article)
10524   "Set the process mark on ARTICLE and update the summary line."
10525   (push article gnus-newsgroup-saved)
10526   (when (gnus-summary-goto-subject article)
10527     (gnus-summary-update-secondary-mark article)))
10528
10529 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10530   "Mark N articles as read forwards.
10531 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10532 The difference between N and the actual number of articles marked is
10533 returned.
10534 If NO-EXPIRE, auto-expiry will be inhibited."
10535   (interactive "p")
10536   (gnus-summary-show-thread)
10537   (let ((backward (< n 0))
10538         (gnus-summary-goto-unread
10539          (and gnus-summary-goto-unread
10540               (not (eq gnus-summary-goto-unread 'never))
10541               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10542                                     gnus-ticked-mark gnus-dormant-mark)))))
10543         (n (abs n))
10544         (mark (or mark gnus-del-mark)))
10545     (while (and (> n 0)
10546                 (gnus-summary-mark-article nil mark no-expire)
10547                 (zerop (gnus-summary-next-subject
10548                         (if backward -1 1)
10549                         (and gnus-summary-goto-unread
10550                              (not (eq gnus-summary-goto-unread 'never)))
10551                         t)))
10552       (setq n (1- n)))
10553     (when (/= 0 n)
10554       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10555     (gnus-summary-recenter)
10556     (gnus-summary-position-point)
10557     (gnus-set-mode-line 'summary)
10558     n))
10559
10560 (defun gnus-summary-mark-article-as-read (mark)
10561   "Mark the current article quickly as read with MARK."
10562   (let ((article (gnus-summary-article-number)))
10563     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10564     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10565     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10566     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10567     (push (cons article mark) gnus-newsgroup-reads)
10568     ;; Possibly remove from cache, if that is used.
10569     (when gnus-use-cache
10570       (gnus-cache-enter-remove-article article))
10571     ;; Allow the backend to change the mark.
10572     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10573     ;; Check for auto-expiry.
10574     (when (and gnus-newsgroup-auto-expire
10575                (memq mark gnus-auto-expirable-marks))
10576       (setq mark gnus-expirable-mark)
10577       ;; Let the backend know about the mark change.
10578       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10579       (push article gnus-newsgroup-expirable))
10580     ;; Set the mark in the buffer.
10581     (gnus-summary-update-mark mark 'unread)
10582     t))
10583
10584 (defun gnus-summary-mark-article-as-unread (mark)
10585   "Mark the current article quickly as unread with MARK."
10586   (let* ((article (gnus-summary-article-number))
10587          (old-mark (gnus-summary-article-mark article)))
10588     ;; Allow the backend to change the mark.
10589     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10590     (if (eq mark old-mark)
10591         t
10592       (if (<= article 0)
10593           (progn
10594             (gnus-error 1 "Can't mark negative article numbers")
10595             nil)
10596         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10597         (setq gnus-newsgroup-spam-marked
10598               (delq article gnus-newsgroup-spam-marked))
10599         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10600         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10601         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10602         (cond ((= mark gnus-ticked-mark)
10603                (setq gnus-newsgroup-marked
10604                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10605                                               article)))
10606               ((= mark gnus-spam-mark)
10607                (setq gnus-newsgroup-spam-marked
10608                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10609                                               article)))
10610               ((= mark gnus-dormant-mark)
10611                (setq gnus-newsgroup-dormant
10612                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10613                                               article)))
10614               (t
10615                (setq gnus-newsgroup-unreads
10616                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10617                                               article))))
10618         (gnus-pull article gnus-newsgroup-reads)
10619
10620         ;; See whether the article is to be put in the cache.
10621         (and gnus-use-cache
10622              (vectorp (gnus-summary-article-header article))
10623              (save-excursion
10624                (gnus-cache-possibly-enter-article
10625                 gnus-newsgroup-name article
10626                 (= mark gnus-ticked-mark)
10627                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10628
10629         ;; Fix the mark.
10630         (gnus-summary-update-mark mark 'unread)
10631         t))))
10632
10633 (defun gnus-summary-mark-article (&optional article mark no-expire)
10634   "Mark ARTICLE with MARK.  MARK can be any character.
10635 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10636 `??' (dormant) and `?E' (expirable).
10637 If MARK is nil, then the default character `?r' is used.
10638 If ARTICLE is nil, then the article on the current line will be
10639 marked.
10640 If NO-EXPIRE, auto-expiry will be inhibited."
10641   ;; The mark might be a string.
10642   (when (stringp mark)
10643     (setq mark (aref mark 0)))
10644   ;; If no mark is given, then we check auto-expiring.
10645   (when (null mark)
10646     (setq mark gnus-del-mark))
10647   (when (and (not no-expire)
10648              gnus-newsgroup-auto-expire
10649              (memq mark gnus-auto-expirable-marks))
10650     (setq mark gnus-expirable-mark))
10651   (let ((article (or article (gnus-summary-article-number)))
10652         (old-mark (gnus-summary-article-mark article)))
10653     ;; Allow the backend to change the mark.
10654     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10655     (if (eq mark old-mark)
10656         t
10657       (unless article
10658         (error "No article on current line"))
10659       (if (not (if (or (= mark gnus-unread-mark)
10660                        (= mark gnus-ticked-mark)
10661                        (= mark gnus-spam-mark)
10662                        (= mark gnus-dormant-mark))
10663                    (gnus-mark-article-as-unread article mark)
10664                  (gnus-mark-article-as-read article mark)))
10665           t
10666         ;; See whether the article is to be put in the cache.
10667         (and gnus-use-cache
10668              (not (= mark gnus-canceled-mark))
10669              (vectorp (gnus-summary-article-header article))
10670              (save-excursion
10671                (gnus-cache-possibly-enter-article
10672                 gnus-newsgroup-name article
10673                 (= mark gnus-ticked-mark)
10674                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10675
10676         (when (gnus-summary-goto-subject article nil t)
10677           (let ((inhibit-read-only t))
10678             (gnus-summary-show-thread)
10679             ;; Fix the mark.
10680             (gnus-summary-update-mark mark 'unread)
10681             t))))))
10682
10683 (defun gnus-summary-update-secondary-mark (article)
10684   "Update the secondary (read, process, cache) mark."
10685   (gnus-summary-update-mark
10686    (cond ((memq article gnus-newsgroup-processable)
10687           gnus-process-mark)
10688          ((memq article gnus-newsgroup-cached)
10689           gnus-cached-mark)
10690          ((memq article gnus-newsgroup-replied)
10691           gnus-replied-mark)
10692          ((memq article gnus-newsgroup-forwarded)
10693           gnus-forwarded-mark)
10694          ((memq article gnus-newsgroup-saved)
10695           gnus-saved-mark)
10696          ((memq article gnus-newsgroup-recent)
10697           gnus-recent-mark)
10698          ((memq article gnus-newsgroup-unseen)
10699           gnus-unseen-mark)
10700          (t gnus-no-mark))
10701    'replied)
10702   (when (gnus-visual-p 'summary-highlight 'highlight)
10703     (gnus-run-hooks 'gnus-summary-update-hook))
10704   t)
10705
10706 (defun gnus-summary-update-download-mark (article)
10707   "Update the download mark."
10708   (gnus-summary-update-mark
10709    (cond ((memq article gnus-newsgroup-undownloaded)
10710           gnus-undownloaded-mark)
10711          (gnus-newsgroup-agentized
10712           gnus-downloaded-mark)
10713          (t
10714           gnus-no-mark))
10715    'download)
10716   (gnus-summary-update-line t)
10717   t)
10718
10719 (defun gnus-summary-update-mark (mark type)
10720   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10721         (inhibit-read-only t))
10722     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10723     (when forward
10724       (when (looking-at "\r")
10725         (incf forward))
10726       (when (<= (+ forward (point)) (point-max))
10727         ;; Go to the right position on the line.
10728         (goto-char (+ forward (point)))
10729         ;; Replace the old mark with the new mark.
10730         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10731         ;; Optionally update the marks by some user rule.
10732         (when (eq type 'unread)
10733           (gnus-data-set-mark
10734            (gnus-data-find (gnus-summary-article-number)) mark)
10735           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10736
10737 (defun gnus-mark-article-as-read (article &optional mark)
10738   "Enter ARTICLE in the pertinent lists and remove it from others."
10739   ;; Make the article expirable.
10740   (let ((mark (or mark gnus-del-mark)))
10741     (setq gnus-newsgroup-expirable
10742           (if (= mark gnus-expirable-mark)
10743               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10744             (delq article gnus-newsgroup-expirable)))
10745     ;; Remove from unread and marked lists.
10746     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10747     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10748     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10749     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10750     (push (cons article mark) gnus-newsgroup-reads)
10751     ;; Possibly remove from cache, if that is used.
10752     (when gnus-use-cache
10753       (gnus-cache-enter-remove-article article))
10754     t))
10755
10756 (defun gnus-mark-article-as-unread (article &optional mark)
10757   "Enter ARTICLE in the pertinent lists and remove it from others."
10758   (let ((mark (or mark gnus-ticked-mark)))
10759     (if (<= article 0)
10760         (progn
10761           (gnus-error 1 "Can't mark negative article numbers")
10762           nil)
10763       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10764             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10765             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10766             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10767             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10768
10769       ;; Unsuppress duplicates?
10770       (when gnus-suppress-duplicates
10771         (gnus-dup-unsuppress-article article))
10772
10773       (cond ((= mark gnus-ticked-mark)
10774              (setq gnus-newsgroup-marked
10775                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10776             ((= mark gnus-spam-mark)
10777              (setq gnus-newsgroup-spam-marked
10778                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10779                                             article)))
10780             ((= mark gnus-dormant-mark)
10781              (setq gnus-newsgroup-dormant
10782                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10783             (t
10784              (setq gnus-newsgroup-unreads
10785                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10786       (gnus-pull article gnus-newsgroup-reads)
10787       t)))
10788
10789 (defalias 'gnus-summary-mark-as-unread-forward
10790   'gnus-summary-tick-article-forward)
10791 (make-obsolete 'gnus-summary-mark-as-unread-forward
10792                'gnus-summary-tick-article-forward)
10793 (defun gnus-summary-tick-article-forward (n)
10794   "Tick N articles forwards.
10795 If N is negative, tick backwards instead.
10796 The difference between N and the number of articles ticked is returned."
10797   (interactive "p")
10798   (gnus-summary-mark-forward n gnus-ticked-mark))
10799
10800 (defalias 'gnus-summary-mark-as-unread-backward
10801   'gnus-summary-tick-article-backward)
10802 (make-obsolete 'gnus-summary-mark-as-unread-backward
10803                'gnus-summary-tick-article-backward)
10804 (defun gnus-summary-tick-article-backward (n)
10805   "Tick N articles backwards.
10806 The difference between N and the number of articles ticked is returned."
10807   (interactive "p")
10808   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10809
10810 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10811 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10812 (defun gnus-summary-tick-article (&optional article clear-mark)
10813   "Mark current article as unread.
10814 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10815 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10816   (interactive)
10817   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10818                                        gnus-ticked-mark)))
10819
10820 (defun gnus-summary-mark-as-read-forward (n)
10821   "Mark N articles as read forwards.
10822 If N is negative, mark backwards instead.
10823 The difference between N and the actual number of articles marked is
10824 returned."
10825   (interactive "p")
10826   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10827
10828 (defun gnus-summary-mark-as-read-backward (n)
10829   "Mark the N articles as read backwards.
10830 The difference between N and the actual number of articles marked is
10831 returned."
10832   (interactive "p")
10833   (gnus-summary-mark-forward
10834    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10835
10836 (defun gnus-summary-mark-as-read (&optional article mark)
10837   "Mark current article as read.
10838 ARTICLE specifies the article to be marked as read.
10839 MARK specifies a string to be inserted at the beginning of the line."
10840   (gnus-summary-mark-article article mark))
10841
10842 (defun gnus-summary-clear-mark-forward (n)
10843   "Clear marks from N articles forward.
10844 If N is negative, clear backward instead.
10845 The difference between N and the number of marks cleared is returned."
10846   (interactive "p")
10847   (gnus-summary-mark-forward n gnus-unread-mark))
10848
10849 (defun gnus-summary-clear-mark-backward (n)
10850   "Clear marks from N articles backward.
10851 The difference between N and the number of marks cleared is returned."
10852   (interactive "p")
10853   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10854
10855 (defun gnus-summary-mark-unread-as-read ()
10856   "Intended to be used by `gnus-mark-article-hook'."
10857   (when (memq gnus-current-article gnus-newsgroup-unreads)
10858     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10859
10860 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10861   "Intended to be used by `gnus-mark-article-hook'."
10862   (let ((mark (gnus-summary-article-mark)))
10863     (when (or (gnus-unread-mark-p mark)
10864               (gnus-read-mark-p mark))
10865       (gnus-summary-mark-article gnus-current-article
10866                                  (or new-mark gnus-read-mark)))))
10867
10868 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10869   "Intended to be used by `gnus-mark-article-hook'."
10870   (let ((mark (gnus-summary-article-mark)))
10871     (when (or (gnus-unread-mark-p mark)
10872               (gnus-read-mark-p mark))
10873       (gnus-summary-mark-article (gnus-summary-article-number)
10874                                  (or new-mark gnus-read-mark)))))
10875
10876 (defun gnus-summary-mark-unread-as-ticked ()
10877   "Intended to be used by `gnus-mark-article-hook'."
10878   (when (memq gnus-current-article gnus-newsgroup-unreads)
10879     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10880
10881 (defun gnus-summary-mark-region-as-read (point mark all)
10882   "Mark all unread articles between point and mark as read.
10883 If given a prefix, mark all articles between point and mark as read,
10884 even ticked and dormant ones."
10885   (interactive "r\nP")
10886   (save-excursion
10887     (let (article)
10888       (goto-char point)
10889       (beginning-of-line)
10890       (while (and
10891               (< (point) mark)
10892               (progn
10893                 (when (or all
10894                           (memq (setq article (gnus-summary-article-number))
10895                                 gnus-newsgroup-unreads))
10896                   (gnus-summary-mark-article article gnus-del-mark))
10897                 t)
10898               (gnus-summary-find-next))))))
10899
10900 (defun gnus-summary-mark-below (score mark)
10901   "Mark articles with score less than SCORE with MARK."
10902   (interactive "P\ncMark: ")
10903   (setq score (if score
10904                   (prefix-numeric-value score)
10905                 (or gnus-summary-default-score 0)))
10906   (with-current-buffer gnus-summary-buffer
10907     (goto-char (point-min))
10908     (while
10909         (progn
10910           (and (< (gnus-summary-article-score) score)
10911                (gnus-summary-mark-article nil mark))
10912           (gnus-summary-find-next)))))
10913
10914 (defun gnus-summary-kill-below (&optional score)
10915   "Mark articles with score below SCORE as read."
10916   (interactive "P")
10917   (gnus-summary-mark-below score gnus-killed-mark))
10918
10919 (defun gnus-summary-clear-above (&optional score)
10920   "Clear all marks from articles with score above SCORE."
10921   (interactive "P")
10922   (gnus-summary-mark-above score gnus-unread-mark))
10923
10924 (defun gnus-summary-tick-above (&optional score)
10925   "Tick all articles with score above SCORE."
10926   (interactive "P")
10927   (gnus-summary-mark-above score gnus-ticked-mark))
10928
10929 (defun gnus-summary-mark-above (score mark)
10930   "Mark articles with score over SCORE with MARK."
10931   (interactive "P\ncMark: ")
10932   (setq score (if score
10933                   (prefix-numeric-value score)
10934                 (or gnus-summary-default-score 0)))
10935   (with-current-buffer gnus-summary-buffer
10936     (goto-char (point-min))
10937     (while (and (progn
10938                   (when (> (gnus-summary-article-score) score)
10939                     (gnus-summary-mark-article nil mark))
10940                   t)
10941                 (gnus-summary-find-next)))))
10942
10943 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10944 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10945 (defun gnus-summary-limit-include-expunged (&optional no-error)
10946   "Display all the hidden articles that were expunged for low scores."
10947   (interactive)
10948   (let ((inhibit-read-only t))
10949     (let ((scored gnus-newsgroup-scored)
10950           headers h)
10951       (while scored
10952         (unless (gnus-summary-article-header (caar scored))
10953           (and (setq h (gnus-number-to-header (caar scored)))
10954                (< (cdar scored) gnus-summary-expunge-below)
10955                (push h headers)))
10956         (setq scored (cdr scored)))
10957       (if (not headers)
10958           (when (not no-error)
10959             (error "No expunged articles hidden"))
10960         (goto-char (point-min))
10961         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10962         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10963         (dolist (x headers)
10964           (push (mail-header-number x) gnus-newsgroup-limit))
10965         (gnus-summary-prepare-unthreaded (nreverse headers))
10966         (goto-char (point-min))
10967         (gnus-summary-position-point)
10968         t))))
10969
10970 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10971   "Mark all unread articles in this newsgroup as read.
10972 If prefix argument ALL is non-nil, ticked and dormant articles will
10973 also be marked as read.
10974 If QUIETLY is non-nil, no questions will be asked.
10975
10976 If TO-HERE is non-nil, it should be a point in the buffer.  All
10977 articles before (after, if REVERSE is set) this point will be marked
10978 as read.
10979
10980 Note that this function will only catch up the unread article
10981 in the current summary buffer limitation.
10982
10983 The number of articles marked as read is returned."
10984   (interactive "P")
10985   (prog1
10986       (save-excursion
10987         (when (or quietly
10988                   (not gnus-interactive-catchup) ;Without confirmation?
10989                   gnus-expert-user
10990                   (gnus-y-or-n-p
10991                    (if all
10992                        "Mark absolutely all articles as read? "
10993                      "Mark all unread articles as read? ")))
10994           (if (and not-mark
10995                    (not gnus-newsgroup-adaptive)
10996                    (not gnus-newsgroup-auto-expire)
10997                    (not gnus-suppress-duplicates)
10998                    (or (not gnus-use-cache)
10999                        (eq gnus-use-cache 'passive)))
11000               (progn
11001                 (when all
11002                   (setq gnus-newsgroup-marked nil
11003                         gnus-newsgroup-spam-marked nil
11004                         gnus-newsgroup-dormant nil))
11005                 (setq gnus-newsgroup-unreads
11006                       (gnus-sorted-nunion
11007                        (gnus-sorted-intersection gnus-newsgroup-unreads
11008                                                  gnus-newsgroup-downloadable)
11009                        (gnus-sorted-difference gnus-newsgroup-unfetched
11010                                                gnus-newsgroup-cached))))
11011             ;; We actually mark all articles as canceled, which we
11012             ;; have to do when using auto-expiry or adaptive scoring.
11013             (gnus-summary-show-all-threads)
11014             (if (and to-here reverse)
11015                 (progn
11016                   (goto-char to-here)
11017                   (gnus-summary-mark-current-read-and-unread-as-read
11018                    gnus-catchup-mark)
11019                   (while (gnus-summary-find-next (not all))
11020                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11021               (when (gnus-summary-first-subject (not all))
11022                 (while (and
11023                         (if to-here (< (point) to-here) t)
11024                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11025                         (gnus-summary-find-next (not all))))))
11026             (gnus-set-mode-line 'summary))
11027           t))
11028     (gnus-summary-position-point)))
11029
11030 (defun gnus-summary-catchup-to-here (&optional all)
11031   "Mark all unticked articles before the current one as read.
11032 If ALL is non-nil, also mark ticked and dormant articles as read."
11033   (interactive "P")
11034   (save-excursion
11035     (gnus-save-hidden-threads
11036       (let ((beg (point)))
11037         ;; We check that there are unread articles.
11038         (when (or all (gnus-summary-find-prev))
11039           (gnus-summary-catchup all t beg)))))
11040   (gnus-summary-position-point))
11041
11042 (defun gnus-summary-catchup-from-here (&optional all)
11043   "Mark all unticked articles after (and including) the current one as read.
11044 If ALL is non-nil, also mark ticked and dormant articles as read."
11045   (interactive "P")
11046   (save-excursion
11047     (gnus-save-hidden-threads
11048       (let ((beg (point)))
11049         ;; We check that there are unread articles.
11050         (when (or all (gnus-summary-find-next))
11051           (gnus-summary-catchup all t beg nil t)))))
11052   (gnus-summary-position-point))
11053
11054 (defun gnus-summary-catchup-all (&optional quietly)
11055   "Mark all articles in this newsgroup as read.
11056 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11057 instead, which marks only unread articles as read."
11058   (interactive "P")
11059   (gnus-summary-catchup t quietly))
11060
11061 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11062   "Mark all unread articles in this group as read, then exit.
11063 If prefix argument ALL is non-nil, all articles are marked as read.
11064 If QUIETLY is non-nil, no questions will be asked."
11065   (interactive "P")
11066   (when (gnus-summary-catchup all quietly nil 'fast)
11067     ;; Select next newsgroup or exit.
11068     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11069              (eq gnus-auto-select-next 'quietly))
11070         (gnus-summary-next-group nil)
11071       (gnus-summary-exit))))
11072
11073 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11074   "Mark all articles in this newsgroup as read, and then exit.
11075 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11076 instead, which marks only unread articles as read."
11077   (interactive "P")
11078   (gnus-summary-catchup-and-exit t quietly))
11079
11080 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11081   "Mark all articles in this group as read and select the next group.
11082 If given a prefix, mark all articles, unread as well as ticked, as
11083 read."
11084   (interactive "P")
11085   (save-excursion
11086     (gnus-summary-catchup all))
11087   (gnus-summary-next-group))
11088
11089 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11090   "Mark all articles in this group as read and select the previous group.
11091 If given a prefix, mark all articles, unread as well as ticked, as
11092 read."
11093   (interactive "P")
11094   (save-excursion
11095     (gnus-summary-catchup all))
11096   (gnus-summary-next-group nil nil t))
11097
11098 ;;;
11099 ;;; with article
11100 ;;;
11101
11102 (defmacro gnus-with-article (article &rest forms)
11103   "Select ARTICLE and perform FORMS in the original article buffer.
11104 Then replace the article with the result."
11105   `(progn
11106      ;; We don't want the article to be marked as read.
11107      (let (gnus-mark-article-hook)
11108        (gnus-summary-select-article t t nil ,article))
11109      (set-buffer gnus-original-article-buffer)
11110      ,@forms
11111      (if (not (gnus-check-backend-function
11112                'request-replace-article (car gnus-article-current)))
11113          (gnus-message 5 "Read-only group; not replacing")
11114        (unless (gnus-request-replace-article
11115                 ,article (car gnus-article-current)
11116                 (current-buffer) t)
11117          (error "Couldn't replace article")))
11118      ;; The cache and backlog have to be flushed somewhat.
11119      (when gnus-keep-backlog
11120        (gnus-backlog-remove-article
11121         (car gnus-article-current) (cdr gnus-article-current)))
11122      (when gnus-use-cache
11123        (gnus-cache-update-article
11124         (car gnus-article-current) (cdr gnus-article-current)))))
11125
11126 (put 'gnus-with-article 'lisp-indent-function 1)
11127 (put 'gnus-with-article 'edebug-form-spec '(form body))
11128
11129 ;; Thread-based commands.
11130
11131 (defun gnus-summary-articles-in-thread (&optional article)
11132   "Return a list of all articles in the current thread.
11133 If ARTICLE is non-nil, return all articles in the thread that starts
11134 with that article."
11135   (let* ((article (or article (gnus-summary-article-number)))
11136          (data (gnus-data-find-list article))
11137          (top-level (gnus-data-level (car data)))
11138          (top-subject
11139           (cond ((null gnus-thread-operation-ignore-subject)
11140                  (gnus-simplify-subject-re
11141                   (mail-header-subject (gnus-data-header (car data)))))
11142                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11143                  (gnus-simplify-subject-fuzzy
11144                   (mail-header-subject (gnus-data-header (car data)))))
11145                 (t nil)))
11146          (end-point (save-excursion
11147                       (if (gnus-summary-go-to-next-thread)
11148                           (point) (point-max))))
11149          articles)
11150     (while (and data
11151                 (< (gnus-data-pos (car data)) end-point))
11152       (when (or (not top-subject)
11153                 (string= top-subject
11154                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11155                              (gnus-simplify-subject-fuzzy
11156                               (mail-header-subject
11157                                (gnus-data-header (car data))))
11158                            (gnus-simplify-subject-re
11159                             (mail-header-subject
11160                              (gnus-data-header (car data)))))))
11161         (push (gnus-data-number (car data)) articles))
11162       (unless (and (setq data (cdr data))
11163                    (> (gnus-data-level (car data)) top-level))
11164         (setq data nil)))
11165     ;; Return the list of articles.
11166     (nreverse articles)))
11167
11168 (defun gnus-summary-rethread-current ()
11169   "Rethread the thread the current article is part of."
11170   (interactive)
11171   (let* ((gnus-show-threads t)
11172          (article (gnus-summary-article-number))
11173          (id (mail-header-id (gnus-summary-article-header)))
11174          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11175     (unless id
11176       (error "No article on the current line"))
11177     (gnus-rebuild-thread id)
11178     (gnus-summary-goto-subject article)))
11179
11180 (defun gnus-summary-reparent-thread ()
11181   "Make the current article child of the marked (or previous) article.
11182
11183 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11184 is non-nil or the Subject: of both articles are the same."
11185   (interactive)
11186   (unless (not (gnus-group-read-only-p))
11187     (error "The current newsgroup does not support article editing"))
11188   (unless (<= (length gnus-newsgroup-processable) 1)
11189     (error "No more than one article may be marked"))
11190   (let ((child (gnus-summary-article-number))
11191         ;; First grab the marked article, otherwise one line up.
11192         (parent (if (not (null gnus-newsgroup-processable))
11193                     (car gnus-newsgroup-processable)
11194                   (save-excursion
11195                     (if (eq (forward-line -1) 0)
11196                         (gnus-summary-article-number)
11197                       (error "Beginning of summary buffer"))))))
11198     (gnus-summary-reparent-children parent (list child))))
11199
11200 (defun gnus-summary-reparent-children (parent children)
11201   "Make PARENT the parent of CHILDREN.
11202 When called interactively, PARENT is the current article and CHILDREN
11203 are the process-marked articles."
11204   (interactive
11205    (list (gnus-summary-article-number)
11206          (gnus-summary-work-articles nil)))
11207   (dolist (child children)
11208     (save-window-excursion
11209       (let ((gnus-article-buffer " *reparent*"))
11210         (unless (not (eq parent child))
11211           (error "An article may not be self-referential"))
11212         (let ((message-id (mail-header-id
11213                            (gnus-summary-article-header parent))))
11214           (unless (and message-id (not (equal message-id "")))
11215             (error "No message-id in desired parent"))
11216           (gnus-with-article child
11217             (save-restriction
11218               (goto-char (point-min))
11219               (message-narrow-to-head)
11220               (if (re-search-forward "^References: " nil t)
11221                   (progn
11222                     (re-search-forward "^[^ \t]" nil t)
11223                     (forward-line -1)
11224                     (end-of-line)
11225                     (insert " " message-id))
11226                 (insert "References: " message-id "\n"))))
11227           (set-buffer gnus-summary-buffer)
11228           (gnus-summary-unmark-all-processable)
11229           (gnus-summary-update-article child)
11230           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11231             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11232           (gnus-summary-rethread-current)
11233           (gnus-message 3 "Article %d is now the child of article %d"
11234                         child parent))))))
11235
11236 (defun gnus-summary-toggle-threads (&optional arg)
11237   "Toggle showing conversation threads.
11238 If ARG is positive number, turn showing conversation threads on."
11239   (interactive "P")
11240   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11241     (setq gnus-show-threads
11242           (if (null arg) (not gnus-show-threads)
11243             (> (prefix-numeric-value arg) 0)))
11244     (gnus-summary-prepare)
11245     (gnus-summary-goto-subject current)
11246     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11247     (gnus-summary-position-point)))
11248
11249 (defun gnus-summary-show-all-threads ()
11250   "Show all threads."
11251   (interactive)
11252   (save-excursion
11253     (let ((buffer-read-only nil))
11254       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
11255   (gnus-summary-position-point))
11256
11257 (defun gnus-summary-show-thread ()
11258   "Show thread subtrees.
11259 Returns nil if no thread was there to be shown."
11260   (interactive)
11261   (let ((buffer-read-only nil)
11262         (orig (point))
11263         (end (point-at-eol))
11264         ;; Leave point at bol
11265         (beg (progn (beginning-of-line) (point))))
11266     (prog1
11267         ;; Any hidden lines here?
11268         (search-forward "\r" end t)
11269       (subst-char-in-region beg end ?\^M ?\n t)
11270       (goto-char orig)
11271       (gnus-summary-position-point))))
11272
11273 (defun gnus-summary-maybe-hide-threads ()
11274   "If requested, hide the threads that should be hidden."
11275   (when (and gnus-show-threads
11276              gnus-thread-hide-subtree)
11277     (gnus-summary-hide-all-threads
11278      (if (or (consp gnus-thread-hide-subtree)
11279              (functionp gnus-thread-hide-subtree))
11280          (gnus-make-predicate gnus-thread-hide-subtree)
11281        nil))))
11282
11283 ;;; Hiding predicates.
11284
11285 (defun gnus-article-unread-p (header)
11286   (memq (mail-header-number header) gnus-newsgroup-unreads))
11287
11288 (defun gnus-article-unseen-p (header)
11289   (memq (mail-header-number header) gnus-newsgroup-unseen))
11290
11291 (defun gnus-map-articles (predicate articles)
11292   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11293   (apply 'gnus-or (mapcar predicate
11294                           (mapcar (lambda (number)
11295                                     (gnus-summary-article-header number))
11296                                   articles))))
11297
11298 (defun gnus-summary-hide-all-threads (&optional predicate)
11299   "Hide all thread subtrees.
11300 If PREDICATE is supplied, threads that satisfy this predicate
11301 will not be hidden."
11302   (interactive)
11303   (save-excursion
11304     (goto-char (point-min))
11305     (let ((end nil))
11306       (while (not end)
11307         (when (or (not predicate)
11308                   (gnus-map-articles
11309                    predicate (gnus-summary-article-children)))
11310             (gnus-summary-hide-thread))
11311         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11312   (gnus-summary-position-point))
11313
11314 (defun gnus-summary-hide-thread ()
11315   "Hide thread subtrees.
11316 If PREDICATE is supplied, threads that satisfy this predicate
11317 will not be hidden.
11318 Returns nil if no threads were there to be hidden."
11319   (interactive)
11320   (let ((buffer-read-only nil)
11321         (start (point))
11322         (article (gnus-summary-article-number)))
11323     (goto-char start)
11324     ;; Go forward until either the buffer ends or the subthread ends.
11325     (when (and (not (eobp))
11326                (or (zerop (gnus-summary-next-thread 1 t))
11327                    (goto-char (point-max))))
11328       (prog1
11329           (if (and (> (point) start)
11330                    (search-backward "\n" start t))
11331               (progn
11332                 (subst-char-in-region start (point) ?\n ?\^M)
11333                 (gnus-summary-goto-subject article))
11334             (goto-char start)
11335             nil)))))
11336
11337 (defun gnus-summary-go-to-next-thread (&optional previous)
11338   "Go to the same level (or less) next thread.
11339 If PREVIOUS is non-nil, go to previous thread instead.
11340 Return the article number moved to, or nil if moving was impossible."
11341   (let ((level (gnus-summary-thread-level))
11342         (way (if previous -1 1))
11343         (beg (point)))
11344     (forward-line way)
11345     (while (and (not (eobp))
11346                 (< level (gnus-summary-thread-level)))
11347       (forward-line way))
11348     (if (eobp)
11349         (progn
11350           (goto-char beg)
11351           nil)
11352       (setq beg (point))
11353       (prog1
11354           (gnus-summary-article-number)
11355         (goto-char beg)))))
11356
11357 (defun gnus-summary-next-thread (n &optional silent)
11358   "Go to the same level next N'th thread.
11359 If N is negative, search backward instead.
11360 Returns the difference between N and the number of skips actually
11361 done.
11362
11363 If SILENT, don't output messages."
11364   (interactive "p")
11365   (let ((backward (< n 0))
11366         (n (abs n)))
11367     (while (and (> n 0)
11368                 (gnus-summary-go-to-next-thread backward))
11369       (decf n))
11370     (unless silent
11371       (gnus-summary-position-point))
11372     (when (and (not silent) (/= 0 n))
11373       (gnus-message 7 "No more threads"))
11374     n))
11375
11376 (defun gnus-summary-prev-thread (n)
11377   "Go to the same level previous N'th thread.
11378 Returns the difference between N and the number of skips actually
11379 done."
11380   (interactive "p")
11381   (gnus-summary-next-thread (- n)))
11382
11383 (defun gnus-summary-go-down-thread ()
11384   "Go down one level in the current thread."
11385   (let ((children (gnus-summary-article-children)))
11386     (when children
11387       (gnus-summary-goto-subject (car children)))))
11388
11389 (defun gnus-summary-go-up-thread ()
11390   "Go up one level in the current thread."
11391   (let ((parent (gnus-summary-article-parent)))
11392     (when parent
11393       (gnus-summary-goto-subject parent))))
11394
11395 (defun gnus-summary-down-thread (n)
11396   "Go down thread N steps.
11397 If N is negative, go up instead.
11398 Returns the difference between N and how many steps down that were
11399 taken."
11400   (interactive "p")
11401   (let ((up (< n 0))
11402         (n (abs n)))
11403     (while (and (> n 0)
11404                 (if up (gnus-summary-go-up-thread)
11405                   (gnus-summary-go-down-thread)))
11406       (setq n (1- n)))
11407     (gnus-summary-position-point)
11408     (when (/= 0 n)
11409       (gnus-message 7 "Can't go further"))
11410     n))
11411
11412 (defun gnus-summary-up-thread (n)
11413   "Go up thread N steps.
11414 If N is negative, go down instead.
11415 Returns the difference between N and how many steps down that were
11416 taken."
11417   (interactive "p")
11418   (gnus-summary-down-thread (- n)))
11419
11420 (defun gnus-summary-top-thread ()
11421   "Go to the top of the thread."
11422   (interactive)
11423   (while (gnus-summary-go-up-thread))
11424   (gnus-summary-article-number))
11425
11426 (defun gnus-summary-expire-thread ()
11427   "Mark articles under current thread as expired."
11428   (interactive)
11429   (gnus-summary-kill-thread 0))
11430
11431 (defun gnus-summary-kill-thread (&optional unmark)
11432   "Mark articles under current thread as read.
11433 If the prefix argument is positive, remove any kinds of marks.
11434 If the prefix argument is zero, mark thread as expired.
11435 If the prefix argument is negative, tick articles instead."
11436   (interactive "P")
11437   (when unmark
11438     (setq unmark (prefix-numeric-value unmark)))
11439   (let ((articles (gnus-summary-articles-in-thread))
11440         (hide (or (null unmark) (= unmark 0))))
11441     (save-excursion
11442       ;; Expand the thread.
11443       (gnus-summary-show-thread)
11444       ;; Mark all the articles.
11445       (while articles
11446         (gnus-summary-goto-subject (car articles))
11447         (cond ((null unmark)
11448                (gnus-summary-mark-article-as-read gnus-killed-mark))
11449               ((> unmark 0)
11450                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11451               ((= unmark 0)
11452                (gnus-summary-mark-article-as-unread gnus-expirable-mark))
11453               (t
11454                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11455         (setq articles (cdr articles))))
11456     ;; Hide killed subtrees when hide is true.
11457     (and hide
11458          gnus-thread-hide-killed
11459          (gnus-summary-hide-thread))
11460     ;; If hide is t, go to next unread subject.
11461     (when hide
11462       ;; Go to next unread subject.
11463       (gnus-summary-next-subject 1 t)))
11464   (gnus-set-mode-line 'summary))
11465
11466 ;; Summary sorting commands
11467
11468 (defun gnus-summary-sort-by-number (&optional reverse)
11469   "Sort the summary buffer by article number.
11470 Argument REVERSE means reverse order."
11471   (interactive "P")
11472   (gnus-summary-sort 'number reverse))
11473
11474 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11475   "Sort the summary buffer by most recent article number.
11476 Argument REVERSE means reverse order."
11477   (interactive "P")
11478   (gnus-summary-sort 'most-recent-number reverse))
11479
11480 (defun gnus-summary-sort-by-random (&optional reverse)
11481   "Randomize the order in the summary buffer.
11482 Argument REVERSE means to randomize in reverse order."
11483   (interactive "P")
11484   (gnus-summary-sort 'random reverse))
11485
11486 (defun gnus-summary-sort-by-author (&optional reverse)
11487   "Sort the summary buffer by author name alphabetically.
11488 If `case-fold-search' is non-nil, case of letters is ignored.
11489 Argument REVERSE means reverse order."
11490   (interactive "P")
11491   (gnus-summary-sort 'author reverse))
11492
11493 (defun gnus-summary-sort-by-recipient (&optional reverse)
11494   "Sort the summary buffer by recipient name alphabetically.
11495 If `case-fold-search' is non-nil, case of letters is ignored.
11496 Argument REVERSE means reverse order."
11497   (interactive "P")
11498   (gnus-summary-sort 'recipient reverse))
11499
11500 (defun gnus-summary-sort-by-subject (&optional reverse)
11501   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11502 If `case-fold-search' is non-nil, case of letters is ignored.
11503 Argument REVERSE means reverse order."
11504   (interactive "P")
11505   (gnus-summary-sort 'subject reverse))
11506
11507 (defun gnus-summary-sort-by-date (&optional reverse)
11508   "Sort the summary buffer by date.
11509 Argument REVERSE means reverse order."
11510   (interactive "P")
11511   (gnus-summary-sort 'date reverse))
11512
11513 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11514   "Sort the summary buffer by most recent date.
11515 Argument REVERSE means reverse order."
11516   (interactive "P")
11517   (gnus-summary-sort 'most-recent-date reverse))
11518
11519 (defun gnus-summary-sort-by-score (&optional reverse)
11520   "Sort the summary buffer by score.
11521 Argument REVERSE means reverse order."
11522   (interactive "P")
11523   (gnus-summary-sort 'score reverse))
11524
11525 (defun gnus-summary-sort-by-lines (&optional reverse)
11526   "Sort the summary buffer by the number of lines.
11527 Argument REVERSE means reverse order."
11528   (interactive "P")
11529   (gnus-summary-sort 'lines reverse))
11530
11531 (defun gnus-summary-sort-by-chars (&optional reverse)
11532   "Sort the summary buffer by article length.
11533 Argument REVERSE means reverse order."
11534   (interactive "P")
11535   (gnus-summary-sort 'chars reverse))
11536
11537 (defun gnus-summary-sort-by-original (&optional reverse)
11538   "Sort the summary buffer using the default sorting method.
11539 Argument REVERSE means reverse order."
11540   (interactive "P")
11541   (let* ((inhibit-read-only t)
11542          (gnus-summary-prepare-hook nil))
11543     ;; We do the sorting by regenerating the threads.
11544     (gnus-summary-prepare)
11545     ;; Hide subthreads if needed.
11546     (gnus-summary-maybe-hide-threads)))
11547
11548 (defun gnus-summary-sort (predicate reverse)
11549   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11550   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11551          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11552          (gnus-thread-sort-functions
11553           (if (not reverse)
11554               thread
11555             `(lambda (t1 t2)
11556                (,thread t2 t1))))
11557          (gnus-sort-gathered-threads-function
11558           gnus-thread-sort-functions)
11559          (gnus-article-sort-functions
11560           (if (not reverse)
11561               article
11562             `(lambda (t1 t2)
11563                (,article t2 t1))))
11564          (inhibit-read-only t)
11565          (gnus-summary-prepare-hook nil))
11566     ;; We do the sorting by regenerating the threads.
11567     (gnus-summary-prepare)
11568     ;; Hide subthreads if needed.
11569     (gnus-summary-maybe-hide-threads)))
11570
11571 ;; Summary saving commands.
11572
11573 (defun gnus-summary-save-article (&optional n not-saved)
11574   "Save the current article using the default saver function.
11575 If N is a positive number, save the N next articles.
11576 If N is a negative number, save the N previous articles.
11577 If N is nil and any articles have been marked with the process mark,
11578 save those articles instead.
11579 The variable `gnus-default-article-saver' specifies the saver function.
11580
11581 If the optional second argument NOT-SAVED is non-nil, articles saved
11582 will not be marked as saved."
11583   (interactive "P")
11584   (require 'gnus-art)
11585   (let* ((articles (gnus-summary-work-articles n))
11586          (save-buffer (save-excursion
11587                         (nnheader-set-temp-buffer " *Gnus Save*")))
11588          (num (length articles))
11589          ;; Whether to save decoded articles or raw articles.
11590          (decode (when gnus-article-save-coding-system
11591                    (get gnus-default-article-saver :decode)))
11592          ;; When saving many articles in a single file, use the other
11593          ;; function to save articles other than the first one.
11594          (saver2 (get gnus-default-article-saver :function))
11595          (gnus-prompt-before-saving (if saver2
11596                                         t
11597                                       gnus-prompt-before-saving))
11598          (gnus-default-article-saver gnus-default-article-saver)
11599          header file)
11600     (dolist (article articles)
11601       (setq header (gnus-summary-article-header article))
11602       (if (not (vectorp header))
11603           ;; This is a pseudo-article.
11604           (if (assq 'name header)
11605               (gnus-copy-file (cdr (assq 'name header)))
11606             (gnus-message 1 "Article %d is unsaveable" article))
11607         ;; This is a real article.
11608         (save-window-excursion
11609           (let ((gnus-display-mime-function (when decode
11610                                               gnus-display-mime-function))
11611                 (gnus-article-prepare-hook (when decode
11612                                              gnus-article-prepare-hook)))
11613             (gnus-summary-select-article t t nil article)
11614             (gnus-summary-goto-subject article)))
11615         (with-current-buffer save-buffer
11616           (erase-buffer)
11617           (insert-buffer-substring (if decode
11618                                        gnus-article-buffer
11619                                      gnus-original-article-buffer)))
11620         (setq file (gnus-article-save save-buffer file num))
11621         (gnus-summary-remove-process-mark article)
11622         (unless not-saved
11623           (gnus-summary-set-saved-mark article)))
11624       (when saver2
11625         (setq gnus-default-article-saver saver2
11626               saver2 nil)))
11627     (gnus-kill-buffer save-buffer)
11628     (gnus-summary-position-point)
11629     (gnus-set-mode-line 'summary)
11630     n))
11631
11632 (defun gnus-summary-pipe-output (&optional n sym)
11633   "Pipe the current article to a subprocess.
11634 If N is a positive number, pipe the N next articles.
11635 If N is a negative number, pipe the N previous articles.
11636 If N is nil and any articles have been marked with the process mark,
11637 pipe those articles instead.
11638 The default command to which articles are piped is specified by the
11639 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11640 will be prompted for the command.
11641
11642 The properties `:decode' and `:headers' that are put to the function
11643 symbol `gnus-summary-save-in-pipe' control whether this function
11644 decodes articles and what headers to keep (see the doc string for the
11645 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11646 is neither omitted nor the symbol `r', force including all headers
11647 regardless of the `:headers' property.  If it is the symbol `r',
11648 articles that are not decoded and include all headers will be piped
11649 no matter what the properties `:decode' and `:headers' are."
11650   (interactive (gnus-interactive "P\ny"))
11651   (require 'gnus-art)
11652   (let* ((articles (gnus-summary-work-articles n))
11653          (result-buffer "*Shell Command Output*")
11654          (all-headers (not (memq sym '(nil r))))
11655          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11656          (raw (eq sym 'r))
11657          (headers (get 'gnus-summary-save-in-pipe :headers))
11658          command result)
11659     (unless (numberp (car articles))
11660       (error "No article to pipe"))
11661     (setq command (gnus-read-shell-command
11662                    (concat "Shell command on "
11663                            (if (cdr articles)
11664                                (format "these %d articles" (length articles))
11665                              "this article")
11666                            ": ")
11667                    gnus-summary-pipe-output-default-command))
11668     (when (string-equal command "")
11669       (error "A command is required"))
11670     (when all-headers
11671       (put 'gnus-summary-save-in-pipe :headers nil))
11672     (unwind-protect
11673         (while articles
11674           (gnus-summary-goto-subject (pop articles))
11675           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11676           (when (and (get-buffer result-buffer)
11677                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11678             (setq result (concat result (with-current-buffer result-buffer
11679                                           (buffer-string))))))
11680       (put 'gnus-summary-save-in-pipe :headers headers))
11681     (unless (zerop (length result))
11682       (if (with-current-buffer (get-buffer-create result-buffer)
11683             (erase-buffer)
11684             (insert result)
11685             (prog1
11686                 (and (= (count-lines (point-min) (point)) 1)
11687                      (progn
11688                        (end-of-line 0)
11689                        (<= (current-column)
11690                            (window-width (minibuffer-window)))))
11691               (goto-char (point-min))))
11692           (message "%s" (substring result 0 -1))
11693         (message nil)
11694         (gnus-configure-windows 'pipe)))))
11695
11696 (defun gnus-summary-save-article-mail (&optional arg)
11697   "Append the current article to a Unix mail box file.
11698 If N is a positive number, save the N next articles.
11699 If N is a negative number, save the N previous articles.
11700 If N is nil and any articles have been marked with the process mark,
11701 save those articles instead."
11702   (interactive "P")
11703   (require 'gnus-art)
11704   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11705     (gnus-summary-save-article arg)))
11706
11707 (defun gnus-summary-save-article-rmail (&optional arg)
11708   "Append the current article to an rmail file.
11709 If N is a positive number, save the N next articles.
11710 If N is a negative number, save the N previous articles.
11711 If N is nil and any articles have been marked with the process mark,
11712 save those articles instead."
11713   (interactive "P")
11714   (require 'gnus-art)
11715   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11716     (gnus-summary-save-article arg)))
11717
11718 (defun gnus-summary-save-article-file (&optional arg)
11719   "Append the current article to a file.
11720 If N is a positive number, save the N next articles.
11721 If N is a negative number, save the N previous articles.
11722 If N is nil and any articles have been marked with the process mark,
11723 save those articles instead."
11724   (interactive "P")
11725   (require 'gnus-art)
11726   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11727     (gnus-summary-save-article arg)))
11728
11729 (defun gnus-summary-write-article-file (&optional arg)
11730   "Write the current article to a file, deleting the previous file.
11731 If N is a positive number, save the N next articles.
11732 If N is a negative number, save the N previous articles.
11733 If N is nil and any articles have been marked with the process mark,
11734 save those articles instead."
11735   (interactive "P")
11736   (require 'gnus-art)
11737   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11738     (gnus-summary-save-article arg)))
11739
11740 (defun gnus-summary-save-article-body-file (&optional arg)
11741   "Append the current article body to a file.
11742 If N is a positive number, save the N next articles.
11743 If N is a negative number, save the N previous articles.
11744 If N is nil and any articles have been marked with the process mark,
11745 save those articles instead."
11746   (interactive "P")
11747   (require 'gnus-art)
11748   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11749     (gnus-summary-save-article arg)))
11750
11751 (defun gnus-summary-write-article-body-file (&optional arg)
11752   "Write the current article body to a file, deleting the previous file.
11753 If N is a positive number, save the N next articles.
11754 If N is a negative number, save the N previous articles.
11755 If N is nil and any articles have been marked with the process mark,
11756 save those articles instead."
11757   (interactive "P")
11758   (require 'gnus-art)
11759   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11760     (gnus-summary-save-article arg)))
11761
11762 (defun gnus-summary-muttprint (&optional arg)
11763   "Print the current article using Muttprint.
11764 If N is a positive number, save the N next articles.
11765 If N is a negative number, save the N previous articles.
11766 If N is nil and any articles have been marked with the process mark,
11767 save those articles instead."
11768   (interactive "P")
11769   (require 'gnus-art)
11770   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11771     (gnus-summary-save-article arg t)))
11772
11773 (defun gnus-summary-pipe-message (program)
11774   "Pipe the current article through PROGRAM."
11775   (interactive "sProgram: ")
11776   (gnus-summary-select-article)
11777   (let ((mail-header-separator ""))
11778     (gnus-eval-in-buffer-window gnus-article-buffer
11779       (save-restriction
11780         (widen)
11781         (let ((start (window-start))
11782               (inhibit-read-only t))
11783           (message-pipe-buffer-body program)
11784           (set-window-start (get-buffer-window (current-buffer)) start))))))
11785
11786 (defun gnus-get-split-value (methods)
11787   "Return a value based on the split METHODS."
11788   (let (split-name method result match)
11789     (when methods
11790       (with-current-buffer gnus-original-article-buffer
11791         (save-restriction
11792           (nnheader-narrow-to-headers)
11793           (while (and methods (not split-name))
11794             (goto-char (point-min))
11795             (setq method (pop methods))
11796             (setq match (car method))
11797             (when (cond
11798                    ((stringp match)
11799                     ;; Regular expression.
11800                     (ignore-errors
11801                       (re-search-forward match nil t)))
11802                    ((functionp match)
11803                     ;; Function.
11804                     (save-restriction
11805                       (widen)
11806                       (setq result (funcall match gnus-newsgroup-name))))
11807                    ((consp match)
11808                     ;; Form.
11809                     (save-restriction
11810                       (widen)
11811                       (setq result (eval match)))))
11812               (setq split-name (cdr method))
11813               (cond ((stringp result)
11814                      (push (expand-file-name
11815                             result gnus-article-save-directory)
11816                            split-name))
11817                     ((consp result)
11818                      (setq split-name (append result split-name)))))))))
11819     (nreverse split-name)))
11820
11821 (defun gnus-valid-move-group-p (group)
11822   (and (boundp group)
11823        (symbol-name group)
11824        (symbol-value group)
11825        (gnus-get-function (gnus-find-method-for-group
11826                            (symbol-name group)) 'request-accept-article t)))
11827
11828 (defun gnus-read-move-group-name (prompt default articles prefix)
11829   "Read a group name."
11830   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11831          (minibuffer-confirm-incomplete nil) ; XEmacs
11832          (prom
11833           (format "%s %s to"
11834                   prompt
11835                   (if (> (length articles) 1)
11836                       (format "these %d articles" (length articles))
11837                     "this article")))
11838          (to-newsgroup
11839           (let (active group)
11840             (when (or (null split-name) (= 1 (length split-name)))
11841               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11842               (mapatoms (lambda (symbol)
11843                           (setq group (symbol-name symbol))
11844                           (when (string-match "[^\000-\177]" group)
11845                             (setq group (gnus-group-decoded-name group)))
11846                           (set (intern group active) group))
11847                         gnus-active-hashtb))
11848             (cond
11849              ((null split-name)
11850               (gnus-completing-read-with-default
11851                default prom active 'gnus-valid-move-group-p nil prefix
11852                'gnus-group-history))
11853              ((= 1 (length split-name))
11854               (gnus-completing-read-with-default
11855                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11856                'gnus-group-history))
11857              (t
11858               (gnus-completing-read-with-default
11859                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11860                'gnus-group-history)))))
11861          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11862          encoded)
11863     (when to-newsgroup
11864       (if (or (string= to-newsgroup "")
11865               (string= to-newsgroup prefix))
11866           (setq to-newsgroup default))
11867       (unless to-newsgroup
11868         (error "No group name entered"))
11869       (setq encoded (mm-encode-coding-string
11870                      to-newsgroup
11871                      (gnus-group-name-charset to-method to-newsgroup)))
11872       (or (gnus-active encoded)
11873           (gnus-activate-group encoded nil nil to-method)
11874           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11875                                      to-newsgroup))
11876               (or (and (gnus-request-create-group encoded to-method)
11877                        (gnus-activate-group encoded nil nil to-method)
11878                        (gnus-subscribe-group encoded))
11879                   (error "Couldn't create group %s" to-newsgroup)))
11880           (error "No such group: %s" to-newsgroup))
11881       encoded)))
11882
11883 (defvar gnus-summary-save-parts-counter)
11884 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11885
11886 (defun gnus-summary-save-parts (type dir n &optional reverse)
11887   "Save parts matching TYPE to DIR.
11888 If REVERSE, save parts that do not match TYPE."
11889   (interactive
11890    (list (read-string "Save parts of type: "
11891                       (or (car gnus-summary-save-parts-type-history)
11892                           gnus-summary-save-parts-default-mime)
11893                       'gnus-summary-save-parts-type-history)
11894          (setq gnus-summary-save-parts-last-directory
11895                (read-file-name "Save to directory: "
11896                                gnus-summary-save-parts-last-directory
11897                                nil t))
11898          current-prefix-arg))
11899   (gnus-summary-iterate n
11900     (let ((gnus-display-mime-function nil)
11901           gnus-article-prepare-hook
11902           gnus-article-decode-hook
11903           gnus-display-mime-function
11904           gnus-break-pages
11905           (gnus-inhibit-treatment t))
11906       (gnus-summary-select-article))
11907     (with-current-buffer gnus-article-buffer
11908       (let ((handles (or gnus-article-mime-handles
11909                          (mm-dissect-buffer nil gnus-article-loose-mime)
11910                          (and gnus-article-emulate-mime
11911                               (mm-uu-dissect))))
11912             (gnus-summary-save-parts-counter 1))
11913         (when handles
11914           (gnus-summary-save-parts-1 type dir handles reverse)
11915           (unless gnus-article-mime-handles ;; Don't destroy this case.
11916             (mm-destroy-parts handles)))))))
11917
11918 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11919   (if (stringp (car handle))
11920       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11921               (cdr handle))
11922     (when (if reverse
11923               (not (string-match type (mm-handle-media-type handle)))
11924             (string-match type (mm-handle-media-type handle)))
11925       (let ((file (expand-file-name
11926                    (gnus-map-function
11927                     mm-file-name-rewrite-functions
11928                     (file-name-nondirectory
11929                      (or
11930                       (mail-content-type-get
11931                        (mm-handle-disposition handle) 'filename)
11932                       (mail-content-type-get
11933                        (mm-handle-type handle) 'name)
11934                       (format "%s.%d.%d" gnus-newsgroup-name
11935                               (cdr gnus-article-current)
11936                               gnus-summary-save-parts-counter))))
11937                    dir)))
11938         (incf gnus-summary-save-parts-counter)
11939         (unless (file-exists-p file)
11940           (mm-save-part-to-file handle file))))))
11941
11942 ;; Summary extract commands
11943
11944 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11945   (let ((inhibit-read-only t)
11946         (article (gnus-summary-article-number))
11947         after-article b e)
11948     (unless (gnus-summary-goto-subject article)
11949       (error "No such article: %d" article))
11950     (gnus-summary-position-point)
11951     ;; If all commands are to be bunched up on one line, we collect
11952     ;; them here.
11953     (unless gnus-view-pseudos-separately
11954       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11955             files action)
11956         (while ps
11957           (setq action (cdr (assq 'action (car ps))))
11958           (setq files (list (cdr (assq 'name (car ps)))))
11959           (while (and ps (cdr ps)
11960                       (string= (or action "1")
11961                                (or (cdr (assq 'action (cadr ps))) "2")))
11962             (push (cdr (assq 'name (cadr ps))) files)
11963             (setcdr ps (cddr ps)))
11964           (when files
11965             (when (not (string-match "%s" action))
11966               (push " " files))
11967             (push " " files)
11968             (when (assq 'execute (car ps))
11969               (setcdr (assq 'execute (car ps))
11970                       (funcall (if (string-match "%s" action)
11971                                    'format 'concat)
11972                                action
11973                                (mapconcat
11974                                 (lambda (f)
11975                                   (if (equal f " ")
11976                                       f
11977                                     (shell-quote-argument f)))
11978                                 files " ")))))
11979           (setq ps (cdr ps)))))
11980     (if (and gnus-view-pseudos (not not-view))
11981         (while pslist
11982           (when (assq 'execute (car pslist))
11983             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11984                                   (eq gnus-view-pseudos 'not-confirm)))
11985           (setq pslist (cdr pslist)))
11986       (save-excursion
11987         (while pslist
11988           (setq after-article (or (cdr (assq 'article (car pslist)))
11989                                   (gnus-summary-article-number)))
11990           (gnus-summary-goto-subject after-article)
11991           (forward-line 1)
11992           (setq b (point))
11993           (insert "    " (file-name-nondirectory
11994                           (cdr (assq 'name (car pslist))))
11995                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11996           (setq e (point))
11997           (forward-line -1)             ; back to `b'
11998           (gnus-add-text-properties
11999            b (1- e) (list 'gnus-number gnus-reffed-article-number
12000                           gnus-mouse-face-prop gnus-mouse-face))
12001           (gnus-data-enter
12002            after-article gnus-reffed-article-number
12003            gnus-unread-mark b (car pslist) 0 (- e b))
12004           (setq gnus-newsgroup-unreads
12005                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12006                                          gnus-reffed-article-number))
12007           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12008           (setq pslist (cdr pslist)))))))
12009
12010 (defun gnus-pseudos< (p1 p2)
12011   (let ((c1 (cdr (assq 'action p1)))
12012         (c2 (cdr (assq 'action p2))))
12013     (and c1 c2 (string< c1 c2))))
12014
12015 (defun gnus-request-pseudo-article (props)
12016   (cond ((assq 'execute props)
12017          (gnus-execute-command (cdr (assq 'execute props)))))
12018   (let ((gnus-current-article (gnus-summary-article-number)))
12019     (gnus-run-hooks 'gnus-mark-article-hook)))
12020
12021 (defun gnus-execute-command (command &optional automatic)
12022   (save-excursion
12023     (gnus-article-setup-buffer)
12024     (set-buffer gnus-article-buffer)
12025     (setq buffer-read-only nil)
12026     (let ((command (if automatic command
12027                      (read-string "Command: " (cons command 0)))))
12028       (erase-buffer)
12029       (insert "$ " command "\n\n")
12030       (if gnus-view-pseudo-asynchronously
12031           (start-process "gnus-execute" (current-buffer) shell-file-name
12032                          shell-command-switch command)
12033         (call-process shell-file-name nil t nil
12034                       shell-command-switch command)))))
12035
12036 ;; Summary kill commands.
12037
12038 (defun gnus-summary-edit-global-kill (article)
12039   "Edit the \"global\" kill file."
12040   (interactive (list (gnus-summary-article-number)))
12041   (gnus-group-edit-global-kill article))
12042
12043 (defun gnus-summary-edit-local-kill ()
12044   "Edit a local kill file applied to the current newsgroup."
12045   (interactive)
12046   (setq gnus-current-headers (gnus-summary-article-header))
12047   (gnus-group-edit-local-kill
12048    (gnus-summary-article-number) gnus-newsgroup-name))
12049
12050 ;;; Header reading.
12051
12052 (defun gnus-read-header (id &optional header)
12053   "Read the headers of article ID and enter them into the Gnus system."
12054   (let ((group gnus-newsgroup-name)
12055         (gnus-override-method
12056          (or
12057           gnus-override-method
12058           (and (gnus-news-group-p gnus-newsgroup-name)
12059                (car (gnus-refer-article-methods)))))
12060         where)
12061     ;; First we check to see whether the header in question is already
12062     ;; fetched.
12063     (if (stringp id)
12064         ;; This is a Message-ID.
12065         (setq header (or header (gnus-id-to-header id)))
12066       ;; This is an article number.
12067       (setq header (or header (gnus-summary-article-header id))))
12068     (if (and header
12069              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12070         ;; We have found the header.
12071         header
12072       ;; We have to really fetch the header to this article.
12073       (with-current-buffer nntp-server-buffer
12074         (when (setq where (gnus-request-head id group))
12075           (nnheader-fold-continuation-lines)
12076           (goto-char (point-max))
12077           (insert ".\n")
12078           (goto-char (point-min))
12079           (insert "211 ")
12080           (princ (cond
12081                   ((numberp id) id)
12082                   ((cdr where) (cdr where))
12083                   (header (mail-header-number header))
12084                   (t gnus-reffed-article-number))
12085                  (current-buffer))
12086           (insert " Article retrieved.\n"))
12087         (if (or (not where)
12088                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12089             ()                          ; Malformed head.
12090           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12091             (when (and (stringp id)
12092                        (or
12093                         (not (string= (gnus-group-real-name group)
12094                                       (car where)))
12095                         (not (gnus-server-equal gnus-override-method
12096                                                 (gnus-group-method group)))))
12097               ;; If we fetched by Message-ID and the article came from
12098               ;; a different group (or server), we fudge some bogus
12099               ;; article numbers for this article.
12100               (mail-header-set-number header gnus-reffed-article-number))
12101             (with-current-buffer gnus-summary-buffer
12102               (decf gnus-reffed-article-number)
12103               (gnus-remove-header (mail-header-number header))
12104               (push header gnus-newsgroup-headers)
12105               (setq gnus-current-headers header)
12106               (push (mail-header-number header) gnus-newsgroup-limit)))
12107           header)))))
12108
12109 (defun gnus-remove-header (number)
12110   "Remove header NUMBER from `gnus-newsgroup-headers'."
12111   (if (and gnus-newsgroup-headers
12112            (= number (mail-header-number (car gnus-newsgroup-headers))))
12113       (pop gnus-newsgroup-headers)
12114     (let ((headers gnus-newsgroup-headers))
12115       (while (and (cdr headers)
12116                   (not (= number (mail-header-number (cadr headers)))))
12117         (pop headers))
12118       (when (cdr headers)
12119         (setcdr headers (cddr headers))))))
12120
12121 ;;;
12122 ;;; summary highlights
12123 ;;;
12124
12125 (defun gnus-highlight-selected-summary ()
12126   "Highlight selected article in summary buffer."
12127   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12128   (when gnus-summary-selected-face
12129     (save-excursion
12130       (let* ((beg (point-at-bol))
12131              (end (point-at-eol))
12132              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12133              (from (if (get-text-property beg gnus-mouse-face-prop)
12134                        beg
12135                      (or (next-single-property-change
12136                           beg gnus-mouse-face-prop nil end)
12137                          beg)))
12138              (to
12139               (if (= from end)
12140                   (- from 2)
12141                 (or (next-single-property-change
12142                      from gnus-mouse-face-prop nil end)
12143                     end))))
12144         ;; If no mouse-face prop on line we will have to = from = end,
12145         ;; so we highlight the entire line instead.
12146         (when (= (+ to 2) from)
12147           (setq from beg)
12148           (setq to end))
12149         (if gnus-newsgroup-selected-overlay
12150             ;; Move old overlay.
12151             (gnus-move-overlay
12152              gnus-newsgroup-selected-overlay from to (current-buffer))
12153           ;; Create new overlay.
12154           (gnus-overlay-put
12155            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12156            'face gnus-summary-selected-face))))))
12157
12158 (defvar gnus-summary-highlight-line-cached nil)
12159 (defvar gnus-summary-highlight-line-trigger nil)
12160
12161 (defun gnus-summary-highlight-line-0 ()
12162   (if (and (eq gnus-summary-highlight-line-trigger
12163                gnus-summary-highlight)
12164            gnus-summary-highlight-line-cached)
12165       gnus-summary-highlight-line-cached
12166     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12167           gnus-summary-highlight-line-cached
12168           (let* ((cond (list 'cond))
12169                  (c cond)
12170                  (list gnus-summary-highlight))
12171             (while list
12172               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12173                               nil))
12174               (setq c (cdr c)
12175                     list (cdr list)))
12176             (gnus-byte-compile (list 'lambda nil cond))))))
12177
12178 (defun gnus-summary-highlight-line ()
12179   "Highlight current line according to `gnus-summary-highlight'."
12180   (let* ((beg (point-at-bol))
12181          (article (or (gnus-summary-article-number) gnus-current-article))
12182          (score (or (cdr (assq article
12183                                gnus-newsgroup-scored))
12184                     gnus-summary-default-score 0))
12185          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12186          (inhibit-read-only t)
12187          (default gnus-summary-default-score)
12188          (default-high gnus-summary-default-high-score)
12189          (default-low gnus-summary-default-low-score)
12190          (uncached (and gnus-summary-use-undownloaded-faces
12191                         (memq article gnus-newsgroup-undownloaded)
12192                         (not (memq article gnus-newsgroup-cached)))))
12193     (let ((face (funcall (gnus-summary-highlight-line-0))))
12194       (unless (eq face (get-text-property beg 'face))
12195         (gnus-put-text-property-excluding-characters-with-faces
12196          beg (point-at-eol) 'face
12197          (setq face (if (boundp face) (symbol-value face) face)))
12198         (when gnus-summary-highlight-line-function
12199           (funcall gnus-summary-highlight-line-function article face))))))
12200
12201 (defun gnus-update-read-articles (group unread &optional compute)
12202   "Update the list of read articles in GROUP.
12203 UNREAD is a sorted list."
12204   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12205         (info (gnus-get-info group))
12206         (prev 1)
12207         read)
12208     (if (or (not info) (not active))
12209         ;; There is no info on this group if it was, in fact,
12210         ;; killed.  Gnus stores no information on killed groups, so
12211         ;; there's nothing to be done.
12212         ;; One could store the information somewhere temporarily,
12213         ;; perhaps...  Hmmm...
12214         ()
12215       ;; Remove any negative articles numbers.
12216       (while (and unread (< (car unread) 0))
12217         (setq unread (cdr unread)))
12218       ;; Remove any expired article numbers
12219       (while (and unread (< (car unread) (car active)))
12220         (setq unread (cdr unread)))
12221       ;; Compute the ranges of read articles by looking at the list of
12222       ;; unread articles.
12223       (while unread
12224         (when (/= (car unread) prev)
12225           (push (if (= prev (1- (car unread))) prev
12226                   (cons prev (1- (car unread))))
12227                 read))
12228         (setq prev (1+ (car unread)))
12229         (setq unread (cdr unread)))
12230       (when (<= prev (cdr active))
12231         (push (cons prev (cdr active)) read))
12232       (setq read (if (> (length read) 1) (nreverse read) read))
12233       (if compute
12234           read
12235         (save-excursion
12236           (let (setmarkundo)
12237             ;; Propagate the read marks to the backend.
12238             (when (and gnus-propagate-marks
12239                        (gnus-check-backend-function 'request-set-mark group))
12240               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12241                     (add (gnus-remove-from-range read (gnus-info-read info))))
12242                 (when (or add del)
12243                   (unless (gnus-check-group group)
12244                     (error "Can't open server for %s" group))
12245                   (gnus-request-set-mark
12246                    group (delq nil (list (if add (list add 'add '(read)))
12247                                          (if del (list del 'del '(read))))))
12248                   (setq setmarkundo
12249                         `(gnus-request-set-mark
12250                           ,group
12251                           ',(delq nil (list
12252                                        (if del (list del 'add '(read)))
12253                                        (if add (list add 'del '(read))))))))))
12254             (set-buffer gnus-group-buffer)
12255             (gnus-undo-register
12256               `(progn
12257                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12258                  (gnus-info-set-read ',info ',(gnus-info-read info))
12259                  (gnus-get-unread-articles-in-group ',info
12260                                                     (gnus-active ,group))
12261                  (gnus-group-update-group ,group t)
12262                  ,setmarkundo))))
12263         ;; Enter this list into the group info.
12264         (gnus-info-set-read info read)
12265         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12266         (gnus-get-unread-articles-in-group info (gnus-active group))
12267         t))))
12268
12269 (defun gnus-offer-save-summaries ()
12270   "Offer to save all active summary buffers."
12271   (let (buffers)
12272     ;; Go through all buffers and find all summaries.
12273     (dolist (buffer (buffer-list))
12274       (when (and (setq buffer (buffer-name buffer))
12275                  (string-match "Summary" buffer)
12276                  (with-current-buffer buffer
12277                    ;; We check that this is, indeed, a summary buffer.
12278                    (and (eq major-mode 'gnus-summary-mode)
12279                         ;; Also make sure this isn't bogus.
12280                         gnus-newsgroup-prepared
12281                         ;; Also make sure that this isn't a
12282                         ;; dead summary buffer.
12283                         (not gnus-dead-summary-mode))))
12284         (push buffer buffers)))
12285     ;; Go through all these summary buffers and offer to save them.
12286     (when buffers
12287       (save-excursion
12288         (map-y-or-n-p
12289          "Update summary buffer %s? "
12290          (lambda (buf)
12291            (switch-to-buffer buf)
12292            (gnus-summary-exit))
12293          buffers)))))
12294
12295 (defun gnus-summary-setup-default-charset ()
12296   "Setup newsgroup default charset."
12297   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12298       (setq gnus-newsgroup-charset nil)
12299     (let* ((ignored-charsets
12300             (or gnus-newsgroup-ephemeral-ignored-charsets
12301                 (append
12302                  (and gnus-newsgroup-name
12303                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12304                  gnus-newsgroup-ignored-charsets))))
12305       (setq gnus-newsgroup-charset
12306             (or gnus-newsgroup-ephemeral-charset
12307                 (and gnus-newsgroup-name
12308                      (gnus-parameter-charset gnus-newsgroup-name))
12309                 gnus-default-charset))
12310       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12311            ignored-charsets))))
12312
12313 ;;;
12314 ;;; Mime Commands
12315 ;;;
12316
12317 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12318   "Display the current article buffer fully MIME-buttonized.
12319 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12320 treated as multipart/mixed."
12321   (interactive "P")
12322   (require 'gnus-art)
12323   (let ((gnus-unbuttonized-mime-types nil)
12324         (gnus-mime-display-multipart-as-mixed show-all-parts))
12325     (gnus-summary-show-article)))
12326
12327 (defun gnus-summary-repair-multipart (article)
12328   "Add a Content-Type header to a multipart article without one."
12329   (interactive (list (gnus-summary-article-number)))
12330   (gnus-with-article article
12331     (message-narrow-to-head)
12332     (message-remove-header "Mime-Version")
12333     (goto-char (point-max))
12334     (insert "Mime-Version: 1.0\n")
12335     (widen)
12336     (when (search-forward "\n--" nil t)
12337       (let ((separator (buffer-substring (point) (point-at-eol))))
12338         (message-narrow-to-head)
12339         (message-remove-header "Content-Type")
12340         (goto-char (point-max))
12341         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12342                         separator))
12343         (widen))))
12344   (let (gnus-mark-article-hook)
12345     (gnus-summary-select-article t t nil article)))
12346
12347 (defun gnus-summary-toggle-display-buttonized ()
12348   "Toggle the buttonizing of the article buffer."
12349   (interactive)
12350   (require 'gnus-art)
12351   (if (setq gnus-inhibit-mime-unbuttonizing
12352             (not gnus-inhibit-mime-unbuttonizing))
12353       (let ((gnus-unbuttonized-mime-types nil))
12354         (gnus-summary-show-article))
12355     (gnus-summary-show-article)))
12356
12357 ;;;
12358 ;;; Generic summary marking commands
12359 ;;;
12360
12361 (defvar gnus-summary-marking-alist
12362   '((read gnus-del-mark "d")
12363     (unread gnus-unread-mark "u")
12364     (ticked gnus-ticked-mark "!")
12365     (dormant gnus-dormant-mark "?")
12366     (expirable gnus-expirable-mark "e"))
12367   "An alist of names/marks/keystrokes.")
12368
12369 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12370 (defvar gnus-summary-mark-map)
12371
12372 (defun gnus-summary-make-all-marking-commands ()
12373   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12374   (dolist (elem gnus-summary-marking-alist)
12375     (apply 'gnus-summary-make-marking-command elem)))
12376
12377 (defun gnus-summary-make-marking-command (name mark keystroke)
12378   (let ((map (make-sparse-keymap)))
12379     (define-key gnus-summary-generic-mark-map keystroke map)
12380     (dolist (lway `((next "next" next nil "n")
12381                     (next-unread "next unread" next t "N")
12382                     (prev "previous" prev nil "p")
12383                     (prev-unread "previous unread" prev t "P")
12384                     (nomove "" nil nil ,keystroke)))
12385       (let ((func (gnus-summary-make-marking-command-1
12386                    mark (car lway) lway name)))
12387         (setq func (eval func))
12388         (define-key map (nth 4 lway) func)))))
12389
12390 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12391   `(defun ,(intern
12392             (format "gnus-summary-put-mark-as-%s%s"
12393                     name (if (eq way 'nomove)
12394                              ""
12395                            (concat "-" (symbol-name way)))))
12396      (n)
12397      ,(format
12398        "Mark the current article as %s%s.
12399 If N, the prefix, then repeat N times.
12400 If N is negative, move in reverse order.
12401 The difference between N and the actual number of articles marked is
12402 returned."
12403        name (cadr lway))
12404      (interactive "p")
12405      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12406
12407 (defun gnus-summary-generic-mark (n mark move unread)
12408   "Mark N articles with MARK."
12409   (unless (eq major-mode 'gnus-summary-mode)
12410     (error "This command can only be used in the summary buffer"))
12411   (gnus-summary-show-thread)
12412   (let ((nummove
12413          (cond
12414           ((eq move 'next) 1)
12415           ((eq move 'prev) -1)
12416           (t 0))))
12417     (if (zerop nummove)
12418         (setq n 1)
12419       (when (< n 0)
12420         (setq n (abs n)
12421               nummove (* -1 nummove))))
12422     (while (and (> n 0)
12423                 (gnus-summary-mark-article nil mark)
12424                 (zerop (gnus-summary-next-subject nummove unread t)))
12425       (setq n (1- n)))
12426     (when (/= 0 n)
12427       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12428     (gnus-summary-recenter)
12429     (gnus-summary-position-point)
12430     (gnus-set-mode-line 'summary)
12431     n))
12432
12433 (defun gnus-summary-insert-articles (articles)
12434   (when (setq articles
12435               (gnus-sorted-difference articles
12436                                       (mapcar (lambda (h)
12437                                                 (mail-header-number h))
12438                                               gnus-newsgroup-headers)))
12439     (setq gnus-newsgroup-headers
12440           (gnus-merge 'list
12441                       gnus-newsgroup-headers
12442                       (gnus-fetch-headers articles)
12443                       'gnus-article-sort-by-number))
12444     ;; Suppress duplicates?
12445     (when gnus-suppress-duplicates
12446       (gnus-dup-suppress-articles))
12447
12448     (if (and gnus-fetch-old-headers
12449              (eq gnus-headers-retrieved-by 'nov))
12450         ;; We might want to build some more threads first.
12451         (if (eq gnus-fetch-old-headers 'invisible)
12452             (gnus-build-all-threads)
12453           (gnus-build-old-threads))
12454       ;; Mark the inserted articles that are unread as unread.
12455       (setq gnus-newsgroup-unreads
12456             (gnus-sorted-nunion
12457              gnus-newsgroup-unreads
12458              (gnus-sorted-nintersection
12459               (gnus-list-of-unread-articles gnus-newsgroup-name)
12460               articles)))
12461       ;; Mark the inserted articles as selected so that the information
12462       ;; of the marks having been changed by a user may be updated when
12463       ;; exiting this group.  See `gnus-summary-update-info'.
12464       (dolist (art articles)
12465         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12466     ;; Let the Gnus agent mark articles as read.
12467     (when gnus-agent
12468       (gnus-agent-get-undownloaded-list))
12469     ;; Remove list identifiers from subject
12470     (when gnus-list-identifiers
12471       (gnus-summary-remove-list-identifiers))
12472     ;; First and last article in this newsgroup.
12473     (when gnus-newsgroup-headers
12474       (setq gnus-newsgroup-begin
12475             (mail-header-number (car gnus-newsgroup-headers))
12476             gnus-newsgroup-end
12477             (mail-header-number
12478              (gnus-last-element gnus-newsgroup-headers))))
12479     (when gnus-use-scoring
12480       (gnus-possibly-score-headers))))
12481
12482 (defun gnus-summary-insert-old-articles (&optional all)
12483   "Insert all old articles in this group.
12484 If ALL is non-nil, already read articles become readable.
12485 If ALL is a number, fetch this number of articles."
12486   (interactive "P")
12487   (prog1
12488       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12489             older len)
12490         (setq older
12491               ;; Some nntp servers lie about their active range.  When
12492               ;; this happens, the active range can be in the millions.
12493               ;; Use a compressed range to avoid creating a huge list.
12494               (gnus-range-difference (list gnus-newsgroup-active) old))
12495         (setq len (gnus-range-length older))
12496         (cond
12497          ((null older) nil)
12498          ((numberp all)
12499           (if (< all len)
12500               (let ((older-range (nreverse older)))
12501                 (setq older nil)
12502
12503                 (while (> all 0)
12504                   (let* ((r (pop older-range))
12505                          (min (if (numberp r) r (car r)))
12506                          (max (if (numberp r) r (cdr r))))
12507                     (while (and (<= min max)
12508                                 (> all 0))
12509                       (push max older)
12510                       (setq all (1- all)
12511                             max (1- max))))))
12512             (setq older (gnus-uncompress-range older))))
12513          (all
12514           (setq older (gnus-uncompress-range older)))
12515          (t
12516           (when (and (numberp gnus-large-newsgroup)
12517                    (> len gnus-large-newsgroup))
12518               (let* ((cursor-in-echo-area nil)
12519                      (initial (gnus-parameter-large-newsgroup-initial
12520                                gnus-newsgroup-name))
12521                      (input
12522                       (read-string
12523                        (format
12524                         "How many articles from %s (%s %d): "
12525                         (gnus-group-decoded-name gnus-newsgroup-name)
12526                         (if initial "max" "default")
12527                         len)
12528                        (if initial
12529                            (cons (number-to-string initial)
12530                                  0)))))
12531                 (unless (string-match "^[ \t]*$" input)
12532                   (setq all (string-to-number input))
12533                   (if (< all len)
12534                       (let ((older-range (nreverse older)))
12535                         (setq older nil)
12536
12537                         (while (> all 0)
12538                           (let* ((r (pop older-range))
12539                                  (min (if (numberp r) r (car r)))
12540                                  (max (if (numberp r) r (cdr r))))
12541                             (while (and (<= min max)
12542                                         (> all 0))
12543                               (push max older)
12544                               (setq all (1- all)
12545                                     max (1- max))))))))))
12546           (setq older (gnus-uncompress-range older))))
12547         (if (not older)
12548             (message "No old news.")
12549           (gnus-summary-insert-articles older)
12550           (gnus-summary-limit (gnus-sorted-nunion old older))))
12551     (gnus-summary-position-point)))
12552
12553 (defun gnus-summary-insert-new-articles ()
12554   "Insert all new articles in this group."
12555   (interactive)
12556   (prog1
12557       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12558             (old-active gnus-newsgroup-active)
12559             (nnmail-fetched-sources (list t))
12560             i new)
12561         (setq gnus-newsgroup-active
12562               (gnus-activate-group gnus-newsgroup-name 'scan))
12563         (setq i (cdr gnus-newsgroup-active))
12564         (while (> i (cdr old-active))
12565           (push i new)
12566           (decf i))
12567         (if (not new)
12568             (message "No gnus is bad news")
12569           (gnus-summary-insert-articles new)
12570           (setq gnus-newsgroup-unreads
12571                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12572           (gnus-summary-limit (gnus-sorted-nunion old new))))
12573     (gnus-summary-position-point)))
12574
12575 (gnus-summary-make-all-marking-commands)
12576
12577 (gnus-ems-redefine)
12578
12579 (provide 'gnus-sum)
12580
12581 (run-hooks 'gnus-sum-load-hook)
12582
12583 ;; Local Variables:
12584 ;; coding: iso-8859-1
12585 ;; End:
12586
12587 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12588 ;;; gnus-sum.el ends here