* gnus-sum.el (gnus-summary-make-menu-bar): Let
[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, 2009, 2010 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-mark-copied-or-moved-articles-as-expirable nil
672   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
673 If nil, the expirable marks will be unchanged except that the marks
674 will be removed when copying or moving articles to a group that has
675 not turned auto-expire on.  If non-nil, articles that have been read
676 will be marked as expirable when being copied or moved to a group in
677 which auto-expire is turned on."
678   :version "23.2"
679   :type 'boolean
680   :group 'gnus-summary-marks)
681
682 (defcustom gnus-view-pseudos nil
683   "*If `automatic', pseudo-articles will be viewed automatically.
684 If `not-confirm', pseudos will be viewed automatically, and the user
685 will not be asked to confirm the command."
686   :group 'gnus-extract-view
687   :type '(choice (const :tag "off" nil)
688                  (const automatic)
689                  (const not-confirm)))
690
691 (defcustom gnus-view-pseudos-separately t
692   "*If non-nil, one pseudo-article will be created for each file to be viewed.
693 If nil, all files that use the same viewing command will be given as a
694 list of parameters to that command."
695   :group 'gnus-extract-view
696   :type 'boolean)
697
698 (defcustom gnus-insert-pseudo-articles t
699   "*If non-nil, insert pseudo-articles when decoding articles."
700   :group 'gnus-extract-view
701   :type 'boolean)
702
703 (defcustom gnus-summary-dummy-line-format
704   "   %(:                             :%) %S\n"
705   "*The format specification for the dummy roots in the summary buffer.
706 It works along the same lines as a normal formatting string,
707 with some simple extensions.
708
709 %S  The subject
710
711 General format specifiers can also be used.
712 See `(gnus)Formatting Variables'."
713   :link '(custom-manual "(gnus)Formatting Variables")
714   :group 'gnus-threading
715   :type 'string)
716
717 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
718   "*The format specification for the summary mode line.
719 It works along the same lines as a normal formatting string,
720 with some simple extensions:
721
722 %G  Group name
723 %p  Unprefixed group name
724 %A  Current article number
725 %z  Current article score
726 %V  Gnus version
727 %U  Number of unread articles in the group
728 %e  Number of unselected articles in the group
729 %Z  A string with unread/unselected article counts
730 %g  Shortish group name
731 %S  Subject of the current article
732 %u  User-defined spec
733 %s  Current score file name
734 %d  Number of dormant articles
735 %r  Number of articles that have been marked as read in this session
736 %E  Number of articles expunged by the score files"
737   :group 'gnus-summary-format
738   :type 'string)
739
740 (defcustom gnus-list-identifiers nil
741   "Regexp that matches list identifiers to be removed from subject.
742 This can also be a list of regexps."
743   :version "21.1"
744   :group 'gnus-summary-format
745   :group 'gnus-article-hiding
746   :type '(choice (const :tag "none" nil)
747                  (regexp :value ".*")
748                  (repeat :value (".*") regexp)))
749
750 (defcustom gnus-summary-mark-below 0
751   "*Mark all articles with a score below this variable as read.
752 This variable is local to each summary buffer and usually set by the
753 score file."
754   :group 'gnus-score-default
755   :type 'integer)
756
757 (defun gnus-widget-reversible-match (widget value)
758   "Ignoring WIDGET, convert VALUE to internal form.
759 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
760   ;; (debug value)
761   (or (symbolp value)
762       (and (listp value)
763            (eq (length value) 2)
764            (eq (nth 0 value) 'not)
765            (symbolp (nth 1 value)))))
766
767 (defun gnus-widget-reversible-to-internal (widget value)
768   "Ignoring WIDGET, convert VALUE to internal form.
769 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
770 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
771   ;; (debug value)
772   (if (atom value)
773       (list value nil)
774     (list (nth 1 value) t)))
775
776 (defun gnus-widget-reversible-to-external (widget value)
777   "Ignoring WIDGET, convert VALUE to external form.
778 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
779 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
780   ;; (debug value)
781   (if (nth 1 value)
782       (list 'not (nth 0 value))
783     (nth 0 value)))
784
785 (define-widget 'gnus-widget-reversible 'group
786   "A `group' that convert values."
787   :match 'gnus-widget-reversible-match
788   :value-to-internal 'gnus-widget-reversible-to-internal
789   :value-to-external 'gnus-widget-reversible-to-external)
790
791 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
792   "*List of functions used for sorting articles in the summary buffer.
793
794 Each function takes two articles and returns non-nil if the first
795 article should be sorted before the other.  If you use more than one
796 function, the primary sort function should be the last.  You should
797 probably always include `gnus-article-sort-by-number' in the list of
798 sorting functions -- preferably first.  Also note that sorting by date
799 is often much slower than sorting by number, and the sorting order is
800 very similar.  (Sorting by date means sorting by the time the message
801 was sent, sorting by number means sorting by arrival time.)
802
803 Each item can also be a list `(not F)' where F is a function;
804 this reverses the sort order.
805
806 Ready-made functions include `gnus-article-sort-by-number',
807 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
808 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
809 and `gnus-article-sort-by-score'.
810
811 When threading is turned on, the variable `gnus-thread-sort-functions'
812 controls how articles are sorted."
813   :group 'gnus-summary-sort
814   :type '(repeat (gnus-widget-reversible
815                   (choice (function-item gnus-article-sort-by-number)
816                           (function-item gnus-article-sort-by-author)
817                           (function-item gnus-article-sort-by-subject)
818                           (function-item gnus-article-sort-by-date)
819                           (function-item gnus-article-sort-by-score)
820                           (function-item gnus-article-sort-by-random)
821                           (function :tag "other"))
822                   (boolean :tag "Reverse order"))))
823
824
825 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
826   "*List of functions used for sorting threads in the summary buffer.
827 By default, threads are sorted by article number.
828
829 Each function takes two threads and returns non-nil if the first
830 thread should be sorted before the other.  If you use more than one
831 function, the primary sort function should be the last.  You should
832 probably always include `gnus-thread-sort-by-number' in the list of
833 sorting functions -- preferably first.  Also note that sorting by date
834 is often much slower than sorting by number, and the sorting order is
835 very similar.  (Sorting by date means sorting by the time the message
836 was sent, sorting by number means sorting by arrival time.)
837
838 Each list item can also be a list `(not F)' where F is a
839 function; this specifies reversed sort order.
840
841 Ready-made functions include `gnus-thread-sort-by-number',
842 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
843 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
844 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
845 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
846 and `gnus-thread-sort-by-total-score' (see
847 `gnus-thread-score-function').
848
849 When threading is turned off, the variable
850 `gnus-article-sort-functions' controls how articles are sorted."
851   :group 'gnus-summary-sort
852   :type '(repeat
853           (gnus-widget-reversible
854            (choice (function-item gnus-thread-sort-by-number)
855                    (function-item gnus-thread-sort-by-author)
856                    (function-item gnus-thread-sort-by-recipient)
857                    (function-item gnus-thread-sort-by-subject)
858                    (function-item gnus-thread-sort-by-date)
859                    (function-item gnus-thread-sort-by-score)
860                    (function-item gnus-thread-sort-by-most-recent-number)
861                    (function-item gnus-thread-sort-by-most-recent-date)
862                    (function-item gnus-thread-sort-by-random)
863                    (function-item gnus-thread-sort-by-total-score)
864                    (function :tag "other"))
865            (boolean :tag "Reverse order"))))
866
867 (defcustom gnus-thread-score-function '+
868   "*Function used for calculating the total score of a thread.
869
870 The function is called with the scores of the article and each
871 subthread and should then return the score of the thread.
872
873 Some functions you can use are `+', `max', or `min'."
874   :group 'gnus-summary-sort
875   :type 'function)
876
877 (defcustom gnus-summary-expunge-below nil
878   "All articles that have a score less than this variable will be expunged.
879 This variable is local to the summary buffers."
880   :group 'gnus-score-default
881   :type '(choice (const :tag "off" nil)
882                  integer))
883
884 (defcustom gnus-thread-expunge-below nil
885   "All threads that have a total score less than this variable will be expunged.
886 See `gnus-thread-score-function' for en explanation of what a
887 \"thread score\" is.
888
889 This variable is local to the summary buffers."
890   :group 'gnus-threading
891   :group 'gnus-score-default
892   :type '(choice (const :tag "off" nil)
893                  integer))
894
895 (defcustom gnus-summary-mode-hook nil
896   "*A hook for Gnus summary mode.
897 This hook is run before any variables are set in the summary buffer."
898   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
899   :group 'gnus-summary-various
900   :type 'hook)
901
902 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
903 (when (featurep 'xemacs)
904   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
905   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
906   (add-hook 'gnus-summary-mode-hook
907             'gnus-xmas-switch-horizontal-scrollbar-off))
908
909 (defcustom gnus-summary-menu-hook nil
910   "*Hook run after the creation of the summary mode menu."
911   :group 'gnus-summary-visual
912   :type 'hook)
913
914 (defcustom gnus-summary-exit-hook nil
915   "*A hook called on exit from the summary buffer.
916 It will be called with point in the group buffer."
917   :group 'gnus-summary-exit
918   :type 'hook)
919
920 (defcustom gnus-summary-prepare-hook nil
921   "*A hook called after the summary buffer has been generated.
922 If you want to modify the summary buffer, you can use this hook."
923   :group 'gnus-summary-various
924   :type 'hook)
925
926 (defcustom gnus-summary-prepared-hook nil
927   "*A hook called as the last thing after the summary buffer has been generated."
928   :group 'gnus-summary-various
929   :type 'hook)
930
931 (defcustom gnus-summary-generate-hook nil
932   "*A hook run just before generating the summary buffer.
933 This hook is commonly used to customize threading variables and the
934 like."
935   :group 'gnus-summary-various
936   :type 'hook)
937
938 (defcustom gnus-select-group-hook nil
939   "*A hook called when a newsgroup is selected.
940
941 If you'd like to simplify subjects like the
942 `gnus-summary-next-same-subject' command does, you can use the
943 following hook:
944
945  (add-hook gnus-select-group-hook
946            (lambda ()
947              (mapcar (lambda (header)
948                        (mail-header-set-subject
949                         header
950                         (gnus-simplify-subject
951                          (mail-header-subject header) 're-only)))
952                      gnus-newsgroup-headers)))"
953   :group 'gnus-group-select
954   :type 'hook)
955
956 (defcustom gnus-select-article-hook nil
957   "*A hook called when an article is selected."
958   :group 'gnus-summary-choose
959   :options '(gnus-agent-fetch-selected-article)
960   :type 'hook)
961
962 (defcustom gnus-visual-mark-article-hook
963   (list 'gnus-highlight-selected-summary)
964   "*Hook run after selecting an article in the summary buffer.
965 It is meant to be used for highlighting the article in some way.  It
966 is not run if `gnus-visual' is nil."
967   :group 'gnus-summary-visual
968   :type 'hook)
969
970 (defcustom gnus-parse-headers-hook nil
971   "*A hook called before parsing the headers."
972   :group 'gnus-various
973   :type 'hook)
974
975 (defcustom gnus-exit-group-hook nil
976   "*A hook called when exiting summary mode.
977 This hook is not called from the non-updating exit commands like `Q'."
978   :group 'gnus-various
979   :type 'hook)
980
981 (defcustom gnus-summary-update-hook
982   (list 'gnus-summary-highlight-line)
983   "*A hook called when a summary line is changed.
984 The hook will not be called if `gnus-visual' is nil.
985
986 The default function `gnus-summary-highlight-line' will
987 highlight the line according to the `gnus-summary-highlight'
988 variable."
989   :group 'gnus-summary-visual
990   :type 'hook)
991
992 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
993   "*A hook called when an article is selected for the first time.
994 The hook is intended to mark an article as read (or unread)
995 automatically when it is selected."
996   :group 'gnus-summary-choose
997   :type 'hook)
998
999 (defcustom gnus-group-no-more-groups-hook nil
1000   "*A hook run when returning to group mode having no more (unread) groups."
1001   :group 'gnus-group-select
1002   :type 'hook)
1003
1004 (defcustom gnus-ps-print-hook nil
1005   "*A hook run before ps-printing something from Gnus."
1006   :group 'gnus-summary
1007   :type 'hook)
1008
1009 (defcustom gnus-summary-article-move-hook nil
1010   "*A hook called after an article is moved, copied, respooled, or crossposted."
1011   :version "22.1"
1012   :group 'gnus-summary
1013   :type 'hook)
1014
1015 (defcustom gnus-summary-article-delete-hook nil
1016   "*A hook called after an article is deleted."
1017   :version "22.1"
1018   :group 'gnus-summary
1019   :type 'hook)
1020
1021 (defcustom gnus-summary-article-expire-hook nil
1022   "*A hook called after an article is expired."
1023   :version "22.1"
1024   :group 'gnus-summary
1025   :type 'hook)
1026
1027 (defcustom gnus-summary-display-arrow
1028   (and (fboundp 'display-graphic-p)
1029        (display-graphic-p))
1030   "*If non-nil, display an arrow highlighting the current article."
1031   :version "22.1"
1032   :group 'gnus-summary
1033   :type 'boolean)
1034
1035 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1036   "Face used for highlighting the current article in the summary buffer."
1037   :group 'gnus-summary-visual
1038   :type 'face)
1039
1040 (defvar gnus-tmp-downloaded nil)
1041
1042 (defcustom gnus-summary-highlight
1043   '(((eq mark gnus-canceled-mark)
1044      . gnus-summary-cancelled)
1045     ((and uncached (> score default-high))
1046      . gnus-summary-high-undownloaded)
1047     ((and uncached (< score default-low))
1048      . gnus-summary-low-undownloaded)
1049     (uncached
1050      . gnus-summary-normal-undownloaded)
1051     ((and (> score default-high)
1052           (or (eq mark gnus-dormant-mark)
1053               (eq mark gnus-ticked-mark)))
1054      . gnus-summary-high-ticked)
1055     ((and (< score default-low)
1056           (or (eq mark gnus-dormant-mark)
1057               (eq mark gnus-ticked-mark)))
1058      . gnus-summary-low-ticked)
1059     ((or (eq mark gnus-dormant-mark)
1060          (eq mark gnus-ticked-mark))
1061      . gnus-summary-normal-ticked)
1062     ((and (> score default-high) (eq mark gnus-ancient-mark))
1063      . gnus-summary-high-ancient)
1064     ((and (< score default-low) (eq mark gnus-ancient-mark))
1065      . gnus-summary-low-ancient)
1066     ((eq mark gnus-ancient-mark)
1067      . gnus-summary-normal-ancient)
1068     ((and (> score default-high) (eq mark gnus-unread-mark))
1069      . gnus-summary-high-unread)
1070     ((and (< score default-low) (eq mark gnus-unread-mark))
1071      . gnus-summary-low-unread)
1072     ((eq mark gnus-unread-mark)
1073      . gnus-summary-normal-unread)
1074     ((> score default-high)
1075      . gnus-summary-high-read)
1076     ((< score default-low)
1077      . gnus-summary-low-read)
1078     (t
1079      . gnus-summary-normal-read))
1080   "*Controls the highlighting of summary buffer lines.
1081
1082 A list of (FORM . FACE) pairs.  When deciding how a particular
1083 summary line should be displayed, each form is evaluated.  The content
1084 of the face field after the first true form is used.  You can change
1085 how those summary lines are displayed, by editing the face field.
1086
1087 You can use the following variables in the FORM field.
1088
1089 score:        The article's score.
1090 default:      The default article score.
1091 default-high: The default score for high scored articles.
1092 default-low:  The default score for low scored articles.
1093 below:        The score below which articles are automatically marked as read.
1094 mark:         The article's mark.
1095 uncached:     Non-nil if the article is uncached."
1096   :group 'gnus-summary-visual
1097   :type '(repeat (cons (sexp :tag "Form" nil)
1098                        face)))
1099 (put 'gnus-summary-highlight 'risky-local-variable t)
1100
1101 (defcustom gnus-alter-header-function nil
1102   "Function called to allow alteration of article header structures.
1103 The function is called with one parameter, the article header vector,
1104 which it may alter in any way."
1105   :type '(choice (const :tag "None" nil)
1106                  function)
1107   :group 'gnus-summary)
1108
1109 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1110   "Function used to decode a string with encoded words.")
1111
1112 (defvar gnus-decode-encoded-address-function
1113   'mail-decode-encoded-address-string
1114   "Function used to decode addresses with encoded words.")
1115
1116 (defcustom gnus-extra-headers '(To Newsgroups)
1117   "*Extra headers to parse."
1118   :version "21.1"
1119   :group 'gnus-summary
1120   :type '(repeat symbol))
1121
1122 (defcustom gnus-ignored-from-addresses
1123   (and user-mail-address
1124        (not (string= user-mail-address ""))
1125        (regexp-quote user-mail-address))
1126   "*From headers that may be suppressed in favor of To headers.
1127 This can be a regexp or a list of regexps."
1128   :version "21.1"
1129   :group 'gnus-summary
1130   :type '(choice regexp
1131                  (repeat :tag "Regexp List" regexp)))
1132
1133 (defsubst gnus-ignored-from-addresses ()
1134   (gmm-regexp-concat gnus-ignored-from-addresses))
1135
1136 (defcustom gnus-summary-to-prefix "-> "
1137   "*String prefixed to the To field in the summary line when
1138 using `gnus-ignored-from-addresses'."
1139   :version "22.1"
1140   :group 'gnus-summary
1141   :type 'string)
1142
1143 (defcustom gnus-summary-newsgroup-prefix "=> "
1144   "*String prefixed to the Newsgroup field in the summary
1145 line when using `gnus-ignored-from-addresses'."
1146   :version "22.1"
1147   :group 'gnus-summary
1148   :type 'string)
1149
1150 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1151   "List of charsets that should be ignored.
1152 When these charsets are used in the \"charset\" parameter, the
1153 default charset will be used instead."
1154   :version "21.1"
1155   :type '(repeat symbol)
1156   :group 'gnus-charset)
1157
1158 (defcustom gnus-newsgroup-maximum-articles nil
1159   "The maximum number of articles a newsgroup.
1160 If this is a number, old articles in a newsgroup exceeding this number
1161 are silently ignored.  If it is nil, no article is ignored.  Note that
1162 setting this variable to a number might prevent you from reading very
1163 old articles."
1164   :group 'gnus-group-select
1165   :version "22.2"
1166   :type '(choice (const :tag "No limit" nil)
1167                  integer))
1168
1169 (gnus-define-group-parameter
1170  ignored-charsets
1171  :type list
1172  :function-document
1173  "Return the ignored charsets of GROUP."
1174  :variable gnus-group-ignored-charsets-alist
1175  :variable-default
1176  '(("alt\\.chinese\\.text" iso-8859-1))
1177  :variable-document
1178  "Alist of regexps (to match group names) and charsets that should be ignored.
1179 When these charsets are used in the \"charset\" parameter, the
1180 default charset will be used instead."
1181  :variable-group gnus-charset
1182  :variable-type '(repeat (cons (regexp :tag "Group")
1183                                (repeat symbol)))
1184  :parameter-type '(choice :tag "Ignored charsets"
1185                           :value nil
1186                           (repeat (symbol)))
1187  :parameter-document       "\
1188 List of charsets that should be ignored.
1189
1190 When these charsets are used in the \"charset\" parameter, the
1191 default charset will be used instead.")
1192
1193 (defcustom gnus-group-highlight-words-alist nil
1194   "Alist of group regexps and highlight regexps.
1195 This variable uses the same syntax as `gnus-emphasis-alist'."
1196   :version "21.1"
1197   :type '(repeat (cons (regexp :tag "Group")
1198                        (repeat (list (regexp :tag "Highlight regexp")
1199                                      (number :tag "Group for entire word" 0)
1200                                      (number :tag "Group for displayed part" 0)
1201                                      (symbol :tag "Face"
1202                                              gnus-emphasis-highlight-words)))))
1203   :group 'gnus-summary-visual)
1204
1205 (defcustom gnus-summary-show-article-charset-alist
1206   nil
1207   "Alist of number and charset.
1208 The article will be shown with the charset corresponding to the
1209 numbered argument.
1210 For example: ((1 . cn-gb-2312) (2 . big5))."
1211   :version "21.1"
1212   :type '(repeat (cons (number :tag "Argument" 1)
1213                        (symbol :tag "Charset")))
1214   :group 'gnus-charset)
1215
1216 (defcustom gnus-preserve-marks t
1217   "Whether marks are preserved when moving, copying and respooling messages."
1218   :version "21.1"
1219   :type 'boolean
1220   :group 'gnus-summary-marks)
1221
1222 (defcustom gnus-propagate-marks t
1223   "If non-nil, do not propagate marks to the backends."
1224   :version "23.1" ;; No Gnus
1225   :type 'boolean
1226   :group 'gnus-summary-marks)
1227
1228 (defcustom gnus-alter-articles-to-read-function nil
1229   "Function to be called to alter the list of articles to be selected."
1230   :type '(choice (const nil) function)
1231   :group 'gnus-summary)
1232
1233 (defcustom gnus-orphan-score nil
1234   "*All orphans get this score added.  Set in the score file."
1235   :group 'gnus-score-default
1236   :type '(choice (const nil)
1237                  integer))
1238
1239 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1240   "*A regexp to match MIME parts when saving multiple parts of a
1241 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1242 This regexp will be used by default when prompting the user for which
1243 type of files to save."
1244   :group 'gnus-summary
1245   :type 'regexp)
1246
1247 (defcustom gnus-read-all-available-headers nil
1248   "Whether Gnus should parse all headers made available to it.
1249 This is mostly relevant for slow back ends where the user may
1250 wish to widen the summary buffer to include all headers
1251 that were fetched.  Say, for nnultimate groups."
1252   :version "22.1"
1253   :group 'gnus-summary
1254   :type '(choice boolean regexp))
1255
1256 (defcustom gnus-summary-pipe-output-default-command nil
1257   "Command (and optional arguments) used to pipe article to subprocess.
1258 This will be used as the default command if it is non-nil.  The value
1259 will be updated if you modify it when executing the command
1260 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1261   :version "23.1" ;; No Gnus
1262   :group 'gnus-summary
1263   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1264
1265 (defcustom gnus-summary-muttprint-program "muttprint"
1266   "Command (and optional arguments) used to run Muttprint.
1267 The value will be updated if you modify it when executing the command
1268 `gnus-summary-muttprint'."
1269   :version "22.1"
1270   :group 'gnus-summary
1271   :type 'string)
1272
1273 (defcustom gnus-article-loose-mime t
1274   "If non-nil, don't require MIME-Version header.
1275 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1276 supply the MIME-Version header or deliberately strip it from the mail.
1277 If non-nil (the default), Gnus will treat some articles as MIME
1278 even if the MIME-Version header is missing."
1279   :version "22.1"
1280   :type 'boolean
1281   :group 'gnus-article-mime)
1282
1283 (defcustom gnus-article-emulate-mime t
1284   "If non-nil, use MIME emulation for uuencode and the like.
1285 This means that Gnus will search message bodies for text that look
1286 like uuencoded bits, yEncoded bits, and so on, and present that using
1287 the normal Gnus MIME machinery."
1288   :version "22.1"
1289   :type 'boolean
1290   :group 'gnus-article-mime)
1291
1292 ;;; Internal variables
1293
1294 (defvar gnus-summary-display-cache nil)
1295 (defvar gnus-article-mime-handles nil)
1296 (defvar gnus-article-decoded-p nil)
1297 (defvar gnus-article-charset nil)
1298 (defvar gnus-article-ignored-charsets nil)
1299 (defvar gnus-scores-exclude-files nil)
1300 (defvar gnus-page-broken nil)
1301
1302 (defvar gnus-original-article nil)
1303 (defvar gnus-article-internal-prepare-hook nil)
1304 (defvar gnus-newsgroup-process-stack nil)
1305
1306 (defvar gnus-thread-indent-array nil)
1307 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1308 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1309   "Function called to sort the articles within a thread after it has been gathered together.")
1310
1311 (defvar gnus-summary-save-parts-type-history nil)
1312 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1313
1314 ;; Avoid highlighting in kill files.
1315 (defvar gnus-summary-inhibit-highlight nil)
1316 (defvar gnus-newsgroup-selected-overlay nil)
1317 (defvar gnus-inhibit-limiting nil)
1318 (defvar gnus-newsgroup-adaptive-score-file nil)
1319 (defvar gnus-current-score-file nil)
1320 (defvar gnus-current-move-group nil)
1321 (defvar gnus-current-copy-group nil)
1322 (defvar gnus-current-crosspost-group nil)
1323 (defvar gnus-newsgroup-display nil)
1324
1325 (defvar gnus-newsgroup-dependencies nil)
1326 (defvar gnus-newsgroup-adaptive nil)
1327 (defvar gnus-summary-display-article-function nil)
1328 (defvar gnus-summary-highlight-line-function nil
1329   "Function called after highlighting a summary line.")
1330
1331 (defvar gnus-summary-line-format-alist
1332   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1333     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1334     (?s gnus-tmp-subject-or-nil ?s)
1335     (?n gnus-tmp-name ?s)
1336     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1337         ?s)
1338     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1339             gnus-tmp-from) ?s)
1340     (?F gnus-tmp-from ?s)
1341     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1342     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1343     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1344     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1345     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1346     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1347     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1348     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1349     (?L gnus-tmp-lines ?s)
1350     (?O gnus-tmp-downloaded ?c)
1351     (?I gnus-tmp-indentation ?s)
1352     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1353     (?R gnus-tmp-replied ?c)
1354     (?\[ gnus-tmp-opening-bracket ?c)
1355     (?\] gnus-tmp-closing-bracket ?c)
1356     (?\> (make-string gnus-tmp-level ? ) ?s)
1357     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1358     (?i gnus-tmp-score ?d)
1359     (?z gnus-tmp-score-char ?c)
1360     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1361     (?U gnus-tmp-unread ?c)
1362     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1363         ?s)
1364     (?t (gnus-summary-number-of-articles-in-thread
1365          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1366         ?d)
1367     (?e (gnus-summary-number-of-articles-in-thread
1368          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1369         ?c)
1370     (?u gnus-tmp-user-defined ?s)
1371     (?P (gnus-pick-line-number) ?d)
1372     (?B gnus-tmp-thread-tree-header-string ?s)
1373     (user-date (gnus-user-date
1374                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1375   "An alist of format specifications that can appear in summary lines.
1376 These are paired with what variables they correspond with, along with
1377 the type of the variable (string, integer, character, etc).")
1378
1379 (defvar gnus-summary-dummy-line-format-alist
1380   `((?S gnus-tmp-subject ?s)
1381     (?N gnus-tmp-number ?d)
1382     (?u gnus-tmp-user-defined ?s)))
1383
1384 (defvar gnus-summary-mode-line-format-alist
1385   `((?G gnus-tmp-group-name ?s)
1386     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1387     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1388     (?A gnus-tmp-article-number ?d)
1389     (?Z gnus-tmp-unread-and-unselected ?s)
1390     (?V gnus-version ?s)
1391     (?U gnus-tmp-unread-and-unticked ?d)
1392     (?S gnus-tmp-subject ?s)
1393     (?e gnus-tmp-unselected ?d)
1394     (?u gnus-tmp-user-defined ?s)
1395     (?d (length gnus-newsgroup-dormant) ?d)
1396     (?t (length gnus-newsgroup-marked) ?d)
1397     (?h (length gnus-newsgroup-spam-marked) ?d)
1398     (?r (length gnus-newsgroup-reads) ?d)
1399     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1400     (?E gnus-newsgroup-expunged-tally ?d)
1401     (?s (gnus-current-score-file-nondirectory) ?s)))
1402
1403 ;; This is here rather than in gnus-art for compilation reasons.
1404 (defvar gnus-article-mode-line-format-alist
1405   (nconc '((?w (gnus-article-wash-status) ?s)
1406            (?m (gnus-article-mime-part-status) ?s))
1407          gnus-summary-mode-line-format-alist))
1408
1409 (defvar gnus-last-search-regexp nil
1410   "Default regexp for article search command.")
1411
1412 (defvar gnus-last-shell-command nil
1413   "Default shell command on article.")
1414
1415 (defvar gnus-newsgroup-agentized nil
1416   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1417 (defvar gnus-newsgroup-begin nil)
1418 (defvar gnus-newsgroup-end nil)
1419 (defvar gnus-newsgroup-last-rmail nil)
1420 (defvar gnus-newsgroup-last-mail nil)
1421 (defvar gnus-newsgroup-last-folder nil)
1422 (defvar gnus-newsgroup-last-file nil)
1423 (defvar gnus-newsgroup-last-directory nil)
1424 (defvar gnus-newsgroup-auto-expire nil)
1425 (defvar gnus-newsgroup-active nil)
1426
1427 (defvar gnus-newsgroup-data nil)
1428 (defvar gnus-newsgroup-data-reverse nil)
1429 (defvar gnus-newsgroup-limit nil)
1430 (defvar gnus-newsgroup-limits nil)
1431 (defvar gnus-summary-use-undownloaded-faces nil)
1432
1433 (defvar gnus-newsgroup-unreads nil
1434   "Sorted list of unread articles in the current newsgroup.")
1435
1436 (defvar gnus-newsgroup-unselected nil
1437   "Sorted list of unselected unread articles in the current newsgroup.")
1438
1439 (defvar gnus-newsgroup-reads nil
1440   "Alist of read articles and article marks in the current newsgroup.")
1441
1442 (defvar gnus-newsgroup-expunged-tally nil)
1443
1444 (defvar gnus-newsgroup-marked nil
1445   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1446
1447 (defvar gnus-newsgroup-spam-marked nil
1448   "List of ranges of articles that have been marked as spam.")
1449
1450 (defvar gnus-newsgroup-killed nil
1451   "List of ranges of articles that have been through the scoring process.")
1452
1453 (defvar gnus-newsgroup-cached nil
1454   "Sorted list of articles that come from the article cache.")
1455
1456 (defvar gnus-newsgroup-saved nil
1457   "List of articles that have been saved.")
1458
1459 (defvar gnus-newsgroup-kill-headers nil)
1460
1461 (defvar gnus-newsgroup-replied nil
1462   "List of articles that have been replied to in the current newsgroup.")
1463
1464 (defvar gnus-newsgroup-forwarded nil
1465   "List of articles that have been forwarded in the current newsgroup.")
1466
1467 (defvar gnus-newsgroup-recent nil
1468   "List of articles that have are recent in the current newsgroup.")
1469
1470 (defvar gnus-newsgroup-expirable nil
1471   "Sorted list of articles in the current newsgroup that can be expired.")
1472
1473 (defvar gnus-newsgroup-processable nil
1474   "List of articles in the current newsgroup that can be processed.")
1475
1476 (defvar gnus-newsgroup-downloadable nil
1477   "Sorted list of articles in the current newsgroup that can be processed.")
1478
1479 (defvar gnus-newsgroup-unfetched nil
1480   "Sorted list of articles in the current newsgroup whose headers have
1481 not been fetched into the agent.
1482
1483 This list will always be a subset of gnus-newsgroup-undownloaded.")
1484
1485 (defvar gnus-newsgroup-undownloaded nil
1486   "List of articles in the current newsgroup that haven't been downloaded.")
1487
1488 (defvar gnus-newsgroup-unsendable nil
1489   "List of articles in the current newsgroup that won't be sent.")
1490
1491 (defvar gnus-newsgroup-bookmarks nil
1492   "List of articles in the current newsgroup that have bookmarks.")
1493
1494 (defvar gnus-newsgroup-dormant nil
1495   "Sorted list of dormant articles in the current newsgroup.")
1496
1497 (defvar gnus-newsgroup-unseen nil
1498   "List of unseen articles in the current newsgroup.")
1499
1500 (defvar gnus-newsgroup-seen nil
1501   "Range of seen articles in the current newsgroup.")
1502
1503 (defvar gnus-newsgroup-articles nil
1504   "List of articles in the current newsgroup.")
1505
1506 (defvar gnus-newsgroup-scored nil
1507   "List of scored articles in the current newsgroup.")
1508
1509 (defvar gnus-newsgroup-headers nil
1510   "List of article headers in the current newsgroup.")
1511
1512 (defvar gnus-newsgroup-threads nil)
1513
1514 (defvar gnus-newsgroup-prepared nil
1515   "Whether the current group has been prepared properly.")
1516
1517 (defvar gnus-newsgroup-ancient nil
1518   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1519
1520 (defvar gnus-newsgroup-sparse nil)
1521
1522 (defvar gnus-current-article nil)
1523 (defvar gnus-article-current nil)
1524 (defvar gnus-current-headers nil)
1525 (defvar gnus-have-all-headers nil)
1526 (defvar gnus-last-article nil)
1527 (defvar gnus-newsgroup-history nil)
1528 (defvar gnus-newsgroup-charset nil)
1529 (defvar gnus-newsgroup-ephemeral-charset nil)
1530 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1531
1532 (defvar gnus-article-before-search nil)
1533
1534 (defvar gnus-summary-local-variables
1535   '(gnus-newsgroup-name
1536     gnus-newsgroup-begin gnus-newsgroup-end
1537     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1538     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1539     gnus-newsgroup-last-directory
1540     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1541     gnus-newsgroup-unselected gnus-newsgroup-marked
1542     gnus-newsgroup-spam-marked
1543     gnus-newsgroup-reads gnus-newsgroup-saved
1544     gnus-newsgroup-replied gnus-newsgroup-forwarded
1545     gnus-newsgroup-recent
1546     gnus-newsgroup-expirable
1547     gnus-newsgroup-processable gnus-newsgroup-killed
1548     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1549     gnus-newsgroup-unfetched
1550     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1551     gnus-newsgroup-seen gnus-newsgroup-articles
1552     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1553     gnus-newsgroup-headers gnus-newsgroup-threads
1554     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1555     gnus-current-article gnus-current-headers gnus-have-all-headers
1556     gnus-last-article gnus-article-internal-prepare-hook
1557     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1558     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1559     gnus-thread-expunge-below
1560     gnus-score-alist gnus-current-score-file
1561     (gnus-summary-expunge-below . global)
1562     (gnus-summary-mark-below . global)
1563     (gnus-orphan-score . global)
1564     gnus-newsgroup-active gnus-scores-exclude-files
1565     gnus-newsgroup-history gnus-newsgroup-ancient
1566     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1567     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1568     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1569     (gnus-newsgroup-expunged-tally . 0)
1570     gnus-cache-removable-articles gnus-newsgroup-cached
1571     gnus-newsgroup-data gnus-newsgroup-data-reverse
1572     gnus-newsgroup-limit gnus-newsgroup-limits
1573     gnus-newsgroup-charset gnus-newsgroup-display
1574     gnus-summary-use-undownloaded-faces)
1575   "Variables that are buffer-local to the summary buffers.")
1576
1577 (defvar gnus-newsgroup-variables nil
1578   "A list of variables that have separate values in different newsgroups.
1579 A list of newsgroup (summary buffer) local variables, or cons of
1580 variables and their default expressions to be evalled (when the default
1581 values are not nil), that should be made global while the summary buffer
1582 is active.
1583
1584 Note: The default expressions will be evaluated (using function `eval')
1585 before assignment to the local variable rather than just assigned to it.
1586 If the default expression is the symbol `global', that symbol will not
1587 be evaluated but the global value of the local variable will be used
1588 instead.
1589
1590 These variables can be used to set variables in the group parameters
1591 while still allowing them to affect operations done in other buffers.
1592 For example:
1593
1594 \(setq gnus-newsgroup-variables
1595      '(message-use-followup-to
1596        (gnus-visible-headers .
1597          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1598 ")
1599
1600 (eval-when-compile
1601   ;; Bind features so that require will believe that gnus-sum has
1602   ;; already been loaded (avoids infinite recursion)
1603   (let ((features (cons 'gnus-sum features)))
1604     (require 'gnus-art)))
1605
1606 ;; MIME stuff.
1607
1608 (defvar gnus-decode-encoded-word-methods
1609   '(mail-decode-encoded-word-string)
1610   "List of methods used to decode encoded words.
1611
1612 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1613 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1614 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1615 whose names match REGEXP.
1616
1617 For example:
1618 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1619  mail-decode-encoded-word-string
1620  (\"chinese\" . rfc1843-decode-string))")
1621
1622 (defvar gnus-decode-encoded-word-methods-cache nil)
1623
1624 (defun gnus-multi-decode-encoded-word-string (string)
1625   "Apply the functions from `gnus-encoded-word-methods' that match."
1626   (unless (and gnus-decode-encoded-word-methods-cache
1627                (eq gnus-newsgroup-name
1628                    (car gnus-decode-encoded-word-methods-cache)))
1629     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1630     (dolist (method gnus-decode-encoded-word-methods)
1631       (if (symbolp method)
1632           (nconc gnus-decode-encoded-word-methods-cache (list method))
1633         (if (and gnus-newsgroup-name
1634                  (string-match (car method) gnus-newsgroup-name))
1635             (nconc gnus-decode-encoded-word-methods-cache
1636                    (list (cdr method)))))))
1637   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1638     (setq string (funcall method string))))
1639
1640 ;; Subject simplification.
1641
1642 (defun gnus-simplify-whitespace (str)
1643   "Remove excessive whitespace from STR."
1644   ;; Multiple spaces.
1645   (while (string-match "[ \t][ \t]+" str)
1646     (setq str (concat (substring str 0 (match-beginning 0))
1647                         " "
1648                         (substring str (match-end 0)))))
1649   ;; Leading spaces.
1650   (when (string-match "^[ \t]+" str)
1651     (setq str (substring str (match-end 0))))
1652   ;; Trailing spaces.
1653   (when (string-match "[ \t]+$" str)
1654     (setq str (substring str 0 (match-beginning 0))))
1655   str)
1656
1657 (defun gnus-simplify-all-whitespace (str)
1658   "Remove all whitespace from STR."
1659   (while (string-match "[ \t\n]+" str)
1660     (setq str (replace-match "" nil nil str)))
1661   str)
1662
1663 (defsubst gnus-simplify-subject-re (subject)
1664   "Remove \"Re:\" from subject lines."
1665   (if (string-match message-subject-re-regexp subject)
1666       (substring subject (match-end 0))
1667     subject))
1668
1669 (defun gnus-simplify-subject (subject &optional re-only)
1670   "Remove `Re:' and words in parentheses.
1671 If RE-ONLY is non-nil, strip leading `Re:'s only."
1672   (let ((case-fold-search t))           ;Ignore case.
1673     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1674     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1675       (setq subject (substring subject (match-end 0))))
1676     ;; Remove uninteresting prefixes.
1677     (when (and (not re-only)
1678                gnus-simplify-ignored-prefixes
1679                (string-match gnus-simplify-ignored-prefixes subject))
1680       (setq subject (substring subject (match-end 0))))
1681     ;; Remove words in parentheses from end.
1682     (unless re-only
1683       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1684         (setq subject (substring subject 0 (match-beginning 0)))))
1685     ;; Return subject string.
1686     subject))
1687
1688 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1689 ;; all whitespace.
1690 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1691   (goto-char (point-min))
1692   (while (re-search-forward regexp nil t)
1693     (replace-match (or newtext ""))))
1694
1695 (defun gnus-simplify-buffer-fuzzy ()
1696   "Simplify string in the buffer fuzzily.
1697 The string in the accessible portion of the current buffer is simplified.
1698 It is assumed to be a single-line subject.
1699 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1700 matter is removed.  Additional things can be deleted by setting
1701 `gnus-simplify-subject-fuzzy-regexp'."
1702   (let ((case-fold-search t)
1703         (modified-tick))
1704     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1705
1706     (while (not (eq modified-tick (buffer-modified-tick)))
1707       (setq modified-tick (buffer-modified-tick))
1708       (cond
1709        ((listp gnus-simplify-subject-fuzzy-regexp)
1710         (mapc 'gnus-simplify-buffer-fuzzy-step
1711               gnus-simplify-subject-fuzzy-regexp))
1712        (gnus-simplify-subject-fuzzy-regexp
1713         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1714       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1715       (gnus-simplify-buffer-fuzzy-step
1716        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1717       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1718
1719     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1720     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1721     (gnus-simplify-buffer-fuzzy-step " $")
1722     (gnus-simplify-buffer-fuzzy-step "^ +")))
1723
1724 (defun gnus-simplify-subject-fuzzy (subject)
1725   "Simplify a subject string fuzzily.
1726 See `gnus-simplify-buffer-fuzzy' for details."
1727   (save-excursion
1728     (gnus-set-work-buffer)
1729     (let ((case-fold-search t))
1730       ;; Remove uninteresting prefixes.
1731       (when (and gnus-simplify-ignored-prefixes
1732                  (string-match gnus-simplify-ignored-prefixes subject))
1733         (setq subject (substring subject (match-end 0))))
1734       (insert subject)
1735       (inline (gnus-simplify-buffer-fuzzy))
1736       (buffer-string))))
1737
1738 (defsubst gnus-simplify-subject-fully (subject)
1739   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1740   (cond
1741    (gnus-simplify-subject-functions
1742     (gnus-map-function gnus-simplify-subject-functions subject))
1743    ((null gnus-summary-gather-subject-limit)
1744     (gnus-simplify-subject-re subject))
1745    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1746     (gnus-simplify-subject-fuzzy subject))
1747    ((numberp gnus-summary-gather-subject-limit)
1748     (truncate-string-to-width (gnus-simplify-subject-re subject)
1749                               gnus-summary-gather-subject-limit))
1750    (t
1751     subject)))
1752
1753 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1754   "Check whether two subjects are equal.
1755 If optional argument SIMPLE-FIRST is t, first argument is already
1756 simplified."
1757   (cond
1758    ((null simple-first)
1759     (equal (gnus-simplify-subject-fully s1)
1760            (gnus-simplify-subject-fully s2)))
1761    (t
1762     (equal s1
1763            (gnus-simplify-subject-fully s2)))))
1764
1765 (defun gnus-summary-bubble-group ()
1766   "Increase the score of the current group.
1767 This is a handy function to add to `gnus-summary-exit-hook' to
1768 increase the score of each group you read."
1769   (gnus-group-add-score gnus-newsgroup-name))
1770
1771 \f
1772 ;;;
1773 ;;; Gnus summary mode
1774 ;;;
1775
1776 (put 'gnus-summary-mode 'mode-class 'special)
1777
1778 (defvar gnus-article-commands-menu)
1779
1780 ;; Non-orthogonal keys
1781
1782 (gnus-define-keys gnus-summary-mode-map
1783   " " gnus-summary-next-page
1784   "\177" gnus-summary-prev-page
1785   [delete] gnus-summary-prev-page
1786   [backspace] gnus-summary-prev-page
1787   "\r" gnus-summary-scroll-up
1788   "\M-\r" gnus-summary-scroll-down
1789   "n" gnus-summary-next-unread-article
1790   "p" gnus-summary-prev-unread-article
1791   "N" gnus-summary-next-article
1792   "P" gnus-summary-prev-article
1793   "\M-\C-n" gnus-summary-next-same-subject
1794   "\M-\C-p" gnus-summary-prev-same-subject
1795   "\M-n" gnus-summary-next-unread-subject
1796   "\M-p" gnus-summary-prev-unread-subject
1797   "." gnus-summary-first-unread-article
1798   "," gnus-summary-best-unread-article
1799   "\M-s" gnus-summary-search-article-forward
1800   "\M-r" gnus-summary-search-article-backward
1801   "\M-S" gnus-summary-repeat-search-article-forward
1802   "\M-R" gnus-summary-repeat-search-article-backward
1803   "<" gnus-summary-beginning-of-article
1804   ">" gnus-summary-end-of-article
1805   "j" gnus-summary-goto-article
1806   "^" gnus-summary-refer-parent-article
1807   "\M-^" gnus-summary-refer-article
1808   "u" gnus-summary-tick-article-forward
1809   "!" gnus-summary-tick-article-forward
1810   "U" gnus-summary-tick-article-backward
1811   "d" gnus-summary-mark-as-read-forward
1812   "D" gnus-summary-mark-as-read-backward
1813   "E" gnus-summary-mark-as-expirable
1814   "\M-u" gnus-summary-clear-mark-forward
1815   "\M-U" gnus-summary-clear-mark-backward
1816   "k" gnus-summary-kill-same-subject-and-select
1817   "\C-k" gnus-summary-kill-same-subject
1818   "\M-\C-k" gnus-summary-kill-thread
1819   "\M-\C-l" gnus-summary-lower-thread
1820   "e" gnus-summary-edit-article
1821   "#" gnus-summary-mark-as-processable
1822   "\M-#" gnus-summary-unmark-as-processable
1823   "\M-\C-t" gnus-summary-toggle-threads
1824   "\M-\C-s" gnus-summary-show-thread
1825   "\M-\C-h" gnus-summary-hide-thread
1826   "\M-\C-f" gnus-summary-next-thread
1827   "\M-\C-b" gnus-summary-prev-thread
1828   [(meta down)] gnus-summary-next-thread
1829   [(meta up)] gnus-summary-prev-thread
1830   "\M-\C-u" gnus-summary-up-thread
1831   "\M-\C-d" gnus-summary-down-thread
1832   "&" gnus-summary-execute-command
1833   "c" gnus-summary-catchup-and-exit
1834   "\C-w" gnus-summary-mark-region-as-read
1835   "\C-t" gnus-summary-toggle-truncation
1836   "?" gnus-summary-mark-as-dormant
1837   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1838   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1839   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1840   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1841   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1842   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1843   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1844   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1845   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1846   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1847   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1848   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1849   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1850   "=" gnus-summary-expand-window
1851   "\C-x\C-s" gnus-summary-reselect-current-group
1852   "\M-g" gnus-summary-rescan-group
1853   "w" gnus-summary-stop-page-breaking
1854   "\C-c\C-r" gnus-summary-caesar-message
1855   "f" gnus-summary-followup
1856   "F" gnus-summary-followup-with-original
1857   "C" gnus-summary-cancel-article
1858   "r" gnus-summary-reply
1859   "R" gnus-summary-reply-with-original
1860   "\C-c\C-f" gnus-summary-mail-forward
1861   "o" gnus-summary-save-article
1862   "\C-o" gnus-summary-save-article-mail
1863   "|" gnus-summary-pipe-output
1864   "\M-k" gnus-summary-edit-local-kill
1865   "\M-K" gnus-summary-edit-global-kill
1866   ;; "V" gnus-version
1867   "\C-c\C-d" gnus-summary-describe-group
1868   "q" gnus-summary-exit
1869   "Q" gnus-summary-exit-no-update
1870   "\C-c\C-i" gnus-info-find-node
1871   gnus-mouse-2 gnus-mouse-pick-article
1872   [follow-link] mouse-face
1873   "m" gnus-summary-mail-other-window
1874   "a" gnus-summary-post-news
1875   "i" gnus-summary-news-other-window
1876   "x" gnus-summary-limit-to-unread
1877   "s" gnus-summary-isearch-article
1878   "t" gnus-summary-toggle-header
1879   "g" gnus-summary-show-article
1880   "l" gnus-summary-goto-last-article
1881   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1882   "\C-d" gnus-summary-enter-digest-group
1883   "\M-\C-d" gnus-summary-read-document
1884   "\M-\C-e" gnus-summary-edit-parameters
1885   "\M-\C-a" gnus-summary-customize-parameters
1886   "\C-c\C-b" gnus-bug
1887   "*" gnus-cache-enter-article
1888   "\M-*" gnus-cache-remove-article
1889   "\M-&" gnus-summary-universal-argument
1890   "\C-l" gnus-recenter
1891   "I" gnus-summary-increase-score
1892   "L" gnus-summary-lower-score
1893   "\M-i" gnus-symbolic-argument
1894   "h" gnus-summary-select-article-buffer
1895
1896   "b" gnus-article-view-part
1897   "\M-t" gnus-summary-toggle-display-buttonized
1898
1899   "V" gnus-summary-score-map
1900   "X" gnus-uu-extract-map
1901   "S" gnus-summary-send-map)
1902
1903 ;; Sort of orthogonal keymap
1904 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1905   "t" gnus-summary-tick-article-forward
1906   "!" gnus-summary-tick-article-forward
1907   "d" gnus-summary-mark-as-read-forward
1908   "r" gnus-summary-mark-as-read-forward
1909   "c" gnus-summary-clear-mark-forward
1910   " " gnus-summary-clear-mark-forward
1911   "e" gnus-summary-mark-as-expirable
1912   "x" gnus-summary-mark-as-expirable
1913   "?" gnus-summary-mark-as-dormant
1914   "b" gnus-summary-set-bookmark
1915   "B" gnus-summary-remove-bookmark
1916   "#" gnus-summary-mark-as-processable
1917   "\M-#" gnus-summary-unmark-as-processable
1918   "S" gnus-summary-limit-include-expunged
1919   "C" gnus-summary-catchup
1920   "H" gnus-summary-catchup-to-here
1921   "h" gnus-summary-catchup-from-here
1922   "\C-c" gnus-summary-catchup-all
1923   "k" gnus-summary-kill-same-subject-and-select
1924   "K" gnus-summary-kill-same-subject
1925   "P" gnus-uu-mark-map)
1926
1927 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1928   "c" gnus-summary-clear-above
1929   "u" gnus-summary-tick-above
1930   "m" gnus-summary-mark-above
1931   "k" gnus-summary-kill-below)
1932
1933 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1934   "/" gnus-summary-limit-to-subject
1935   "n" gnus-summary-limit-to-articles
1936   "b" gnus-summary-limit-to-bodies
1937   "h" gnus-summary-limit-to-headers
1938   "w" gnus-summary-pop-limit
1939   "s" gnus-summary-limit-to-subject
1940   "a" gnus-summary-limit-to-author
1941   "u" gnus-summary-limit-to-unread
1942   "m" gnus-summary-limit-to-marks
1943   "M" gnus-summary-limit-exclude-marks
1944   "v" gnus-summary-limit-to-score
1945   "*" gnus-summary-limit-include-cached
1946   "D" gnus-summary-limit-include-dormant
1947   "T" gnus-summary-limit-include-thread
1948   "d" gnus-summary-limit-exclude-dormant
1949   "t" gnus-summary-limit-to-age
1950   "." gnus-summary-limit-to-unseen
1951   "x" gnus-summary-limit-to-extra
1952   "p" gnus-summary-limit-to-display-predicate
1953   "E" gnus-summary-limit-include-expunged
1954   "c" gnus-summary-limit-exclude-childless-dormant
1955   "C" gnus-summary-limit-mark-excluded-as-read
1956   "o" gnus-summary-insert-old-articles
1957   "N" gnus-summary-insert-new-articles
1958   "S" gnus-summary-limit-to-singletons
1959   "r" gnus-summary-limit-to-replied
1960   "R" gnus-summary-limit-to-recipient
1961   "A" gnus-summary-limit-to-address)
1962
1963 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1964   "n" gnus-summary-next-unread-article
1965   "p" gnus-summary-prev-unread-article
1966   "N" gnus-summary-next-article
1967   "P" gnus-summary-prev-article
1968   "\C-n" gnus-summary-next-same-subject
1969   "\C-p" gnus-summary-prev-same-subject
1970   "\M-n" gnus-summary-next-unread-subject
1971   "\M-p" gnus-summary-prev-unread-subject
1972   "f" gnus-summary-first-unread-article
1973   "b" gnus-summary-best-unread-article
1974   "j" gnus-summary-goto-article
1975   "g" gnus-summary-goto-subject
1976   "l" gnus-summary-goto-last-article
1977   "o" gnus-summary-pop-article)
1978
1979 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1980   "k" gnus-summary-kill-thread
1981   "E" gnus-summary-expire-thread
1982   "l" gnus-summary-lower-thread
1983   "i" gnus-summary-raise-thread
1984   "T" gnus-summary-toggle-threads
1985   "t" gnus-summary-rethread-current
1986   "^" gnus-summary-reparent-thread
1987   "\M-^" gnus-summary-reparent-children
1988   "s" gnus-summary-show-thread
1989   "S" gnus-summary-show-all-threads
1990   "h" gnus-summary-hide-thread
1991   "H" gnus-summary-hide-all-threads
1992   "n" gnus-summary-next-thread
1993   "p" gnus-summary-prev-thread
1994   "u" gnus-summary-up-thread
1995   "o" gnus-summary-top-thread
1996   "d" gnus-summary-down-thread
1997   "#" gnus-uu-mark-thread
1998   "\M-#" gnus-uu-unmark-thread)
1999
2000 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2001   "g" gnus-summary-prepare
2002   "c" gnus-summary-insert-cached-articles
2003   "d" gnus-summary-insert-dormant-articles
2004   "t" gnus-summary-insert-ticked-articles)
2005
2006 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2007   "c" gnus-summary-catchup-and-exit
2008   "C" gnus-summary-catchup-all-and-exit
2009   "E" gnus-summary-exit-no-update
2010   "Q" gnus-summary-exit
2011   "Z" gnus-summary-exit
2012   "n" gnus-summary-catchup-and-goto-next-group
2013   "p" gnus-summary-catchup-and-goto-prev-group
2014   "R" gnus-summary-reselect-current-group
2015   "G" gnus-summary-rescan-group
2016   "N" gnus-summary-next-group
2017   "s" gnus-summary-save-newsrc
2018   "P" gnus-summary-prev-group)
2019
2020 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2021   " " gnus-summary-next-page
2022   "n" gnus-summary-next-page
2023   "\177" gnus-summary-prev-page
2024   [delete] gnus-summary-prev-page
2025   "p" gnus-summary-prev-page
2026   "\r" gnus-summary-scroll-up
2027   "\M-\r" gnus-summary-scroll-down
2028   "<" gnus-summary-beginning-of-article
2029   ">" gnus-summary-end-of-article
2030   "b" gnus-summary-beginning-of-article
2031   "e" gnus-summary-end-of-article
2032   "^" gnus-summary-refer-parent-article
2033   "r" gnus-summary-refer-parent-article
2034   "D" gnus-summary-enter-digest-group
2035   "R" gnus-summary-refer-references
2036   "T" gnus-summary-refer-thread
2037   "g" gnus-summary-show-article
2038   "s" gnus-summary-isearch-article
2039   "P" gnus-summary-print-article
2040   "S" gnus-sticky-article
2041   "M" gnus-mailing-list-insinuate
2042   "t" gnus-article-babel)
2043
2044 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2045   "b" gnus-article-add-buttons
2046   "B" gnus-article-add-buttons-to-head
2047   "o" gnus-article-treat-overstrike
2048   "e" gnus-article-emphasize
2049   "w" gnus-article-fill-cited-article
2050   "Q" gnus-article-fill-long-lines
2051   "L" gnus-article-toggle-truncate-lines
2052   "C" gnus-article-capitalize-sentences
2053   "c" gnus-article-remove-cr
2054   "q" gnus-article-de-quoted-unreadable
2055   "6" gnus-article-de-base64-unreadable
2056   "Z" gnus-article-decode-HZ
2057   "A" gnus-article-treat-ansi-sequences
2058   "h" gnus-article-wash-html
2059   "u" gnus-article-unsplit-urls
2060   "s" gnus-summary-force-verify-and-decrypt
2061   "f" gnus-article-display-x-face
2062   "l" gnus-summary-stop-page-breaking
2063   "r" gnus-summary-caesar-message
2064   "m" gnus-summary-morse-message
2065   "t" gnus-summary-toggle-header
2066   "g" gnus-treat-smiley
2067   "v" gnus-summary-verbose-headers
2068   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2069   "p" gnus-article-verify-x-pgp-sig
2070   "d" gnus-article-treat-dumbquotes
2071   "i" gnus-summary-idna-message)
2072
2073 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2074   ;; mnemonic: deuglif*Y*
2075   "u" gnus-article-outlook-unwrap-lines
2076   "a" gnus-article-outlook-repair-attribution
2077   "c" gnus-article-outlook-rearrange-citation
2078   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2079
2080 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2081   "a" gnus-article-hide
2082   "h" gnus-article-hide-headers
2083   "b" gnus-article-hide-boring-headers
2084   "s" gnus-article-hide-signature
2085   "c" gnus-article-hide-citation
2086   "C" gnus-article-hide-citation-in-followups
2087   "l" gnus-article-hide-list-identifiers
2088   "B" gnus-article-strip-banner
2089   "P" gnus-article-hide-pem
2090   "\C-c" gnus-article-hide-citation-maybe)
2091
2092 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2093   "a" gnus-article-highlight
2094   "h" gnus-article-highlight-headers
2095   "c" gnus-article-highlight-citation
2096   "s" gnus-article-highlight-signature)
2097
2098 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2099   "f" gnus-article-treat-fold-headers
2100   "u" gnus-article-treat-unfold-headers
2101   "n" gnus-article-treat-fold-newsgroups)
2102
2103 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2104   "x" gnus-article-display-x-face
2105   "d" gnus-article-display-face
2106   "s" gnus-treat-smiley
2107   "D" gnus-article-remove-images
2108   "f" gnus-treat-from-picon
2109   "m" gnus-treat-mail-picon
2110   "n" gnus-treat-newsgroups-picon)
2111
2112 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2113   "w" gnus-article-decode-mime-words
2114   "c" gnus-article-decode-charset
2115   "v" gnus-mime-view-all-parts
2116   "b" gnus-article-view-part)
2117
2118 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2119   "z" gnus-article-date-ut
2120   "u" gnus-article-date-ut
2121   "l" gnus-article-date-local
2122   "p" gnus-article-date-english
2123   "e" gnus-article-date-lapsed
2124   "o" gnus-article-date-original
2125   "i" gnus-article-date-iso8601
2126   "s" gnus-article-date-user)
2127
2128 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2129   "t" gnus-article-remove-trailing-blank-lines
2130   "l" gnus-article-strip-leading-blank-lines
2131   "m" gnus-article-strip-multiple-blank-lines
2132   "a" gnus-article-strip-blank-lines
2133   "A" gnus-article-strip-all-blank-lines
2134   "s" gnus-article-strip-leading-space
2135   "e" gnus-article-strip-trailing-space
2136   "w" gnus-article-remove-leading-whitespace)
2137
2138 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2139   "v" gnus-version
2140   "f" gnus-summary-fetch-faq
2141   "d" gnus-summary-describe-group
2142   "h" gnus-summary-describe-briefly
2143   "i" gnus-info-find-node
2144   "c" gnus-group-fetch-charter
2145   "C" gnus-group-fetch-control)
2146
2147 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2148   "e" gnus-summary-expire-articles
2149   "\M-\C-e" gnus-summary-expire-articles-now
2150   "\177" gnus-summary-delete-article
2151   [delete] gnus-summary-delete-article
2152   [backspace] gnus-summary-delete-article
2153   "m" gnus-summary-move-article
2154   "r" gnus-summary-respool-article
2155   "w" gnus-summary-edit-article
2156   "c" gnus-summary-copy-article
2157   "B" gnus-summary-crosspost-article
2158   "q" gnus-summary-respool-query
2159   "t" gnus-summary-respool-trace
2160   "i" gnus-summary-import-article
2161   "I" gnus-summary-create-article
2162   "p" gnus-summary-article-posted-p)
2163
2164 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2165   "o" gnus-summary-save-article
2166   "m" gnus-summary-save-article-mail
2167   "F" gnus-summary-write-article-file
2168   "r" gnus-summary-save-article-rmail
2169   "f" gnus-summary-save-article-file
2170   "b" gnus-summary-save-article-body-file
2171   "B" gnus-summary-write-article-body-file
2172   "h" gnus-summary-save-article-folder
2173   "v" gnus-summary-save-article-vm
2174   "p" gnus-summary-pipe-output
2175   "P" gnus-summary-muttprint
2176   "s" gnus-soup-add-article)
2177
2178 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2179   "b" gnus-summary-display-buttonized
2180   "m" gnus-summary-repair-multipart
2181   "v" gnus-article-view-part
2182   "o" gnus-article-save-part
2183   "O" gnus-article-save-part-and-strip
2184   "r" gnus-article-replace-part
2185   "d" gnus-article-delete-part
2186   "t" gnus-article-view-part-as-type
2187   "j" gnus-article-jump-to-part
2188   "c" gnus-article-copy-part
2189   "C" gnus-article-view-part-as-charset
2190   "e" gnus-article-view-part-externally
2191   "H" gnus-article-browse-html-article
2192   "E" gnus-article-encrypt-body
2193   "i" gnus-article-inline-part
2194   "|" gnus-article-pipe-part)
2195
2196 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2197   "p" gnus-summary-mark-as-processable
2198   "u" gnus-summary-unmark-as-processable
2199   "U" gnus-summary-unmark-all-processable
2200   "v" gnus-uu-mark-over
2201   "s" gnus-uu-mark-series
2202   "r" gnus-uu-mark-region
2203   "g" gnus-uu-unmark-region
2204   "R" gnus-uu-mark-by-regexp
2205   "G" gnus-uu-unmark-by-regexp
2206   "t" gnus-uu-mark-thread
2207   "T" gnus-uu-unmark-thread
2208   "a" gnus-uu-mark-all
2209   "b" gnus-uu-mark-buffer
2210   "S" gnus-uu-mark-sparse
2211   "k" gnus-summary-kill-process-mark
2212   "y" gnus-summary-yank-process-mark
2213   "w" gnus-summary-save-process-mark
2214   "i" gnus-uu-invert-processable)
2215
2216 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2217   ;;"x" gnus-uu-extract-any
2218   "m" gnus-summary-save-parts
2219   "u" gnus-uu-decode-uu
2220   "U" gnus-uu-decode-uu-and-save
2221   "s" gnus-uu-decode-unshar
2222   "S" gnus-uu-decode-unshar-and-save
2223   "o" gnus-uu-decode-save
2224   "O" gnus-uu-decode-save
2225   "b" gnus-uu-decode-binhex
2226   "B" gnus-uu-decode-binhex
2227   "Y" gnus-uu-decode-yenc
2228   "p" gnus-uu-decode-postscript
2229   "P" gnus-uu-decode-postscript-and-save)
2230
2231 (gnus-define-keys
2232     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2233   "u" gnus-uu-decode-uu-view
2234   "U" gnus-uu-decode-uu-and-save-view
2235   "s" gnus-uu-decode-unshar-view
2236   "S" gnus-uu-decode-unshar-and-save-view
2237   "o" gnus-uu-decode-save-view
2238   "O" gnus-uu-decode-save-view
2239   "b" gnus-uu-decode-binhex-view
2240   "B" gnus-uu-decode-binhex-view
2241   "p" gnus-uu-decode-postscript-view
2242   "P" gnus-uu-decode-postscript-and-save-view)
2243
2244 (defvar gnus-article-post-menu nil)
2245
2246 (defconst gnus-summary-menu-maxlen 20)
2247
2248 (defun gnus-summary-menu-split (menu)
2249   ;; If we have lots of elements, divide them into groups of 20
2250   ;; and make a pane (or submenu) for each one.
2251   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2252       (let ((menu menu) sublists next
2253             (i 1))
2254         (while menu
2255           ;; Pull off the next gnus-summary-menu-maxlen elements
2256           ;; and make them the next element of sublist.
2257           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2258           (if next
2259               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2260                       nil))
2261           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2262                                              (aref (car (last menu)) 0)) menu)
2263                                sublists))
2264           (setq i (1+ i))
2265           (setq menu next))
2266         (nreverse sublists))
2267     ;; Few elements--put them all in one pane.
2268     menu))
2269
2270 (defun gnus-summary-make-menu-bar ()
2271   (gnus-turn-off-edit-menu 'summary)
2272
2273   (unless (boundp 'gnus-summary-misc-menu)
2274
2275     (easy-menu-define
2276       gnus-summary-kill-menu gnus-summary-mode-map ""
2277       (cons
2278        "Score"
2279        (nconc
2280         (list
2281          ["Customize" gnus-score-customize t])
2282         (gnus-make-score-map 'increase)
2283         (gnus-make-score-map 'lower)
2284         '(("Mark"
2285            ["Kill below" gnus-summary-kill-below t]
2286            ["Mark above" gnus-summary-mark-above t]
2287            ["Tick above" gnus-summary-tick-above t]
2288            ["Clear above" gnus-summary-clear-above t])
2289           ["Current score" gnus-summary-current-score t]
2290           ["Set score" gnus-summary-set-score t]
2291           ["Switch current score file..." gnus-score-change-score-file t]
2292           ["Set mark below..." gnus-score-set-mark-below t]
2293           ["Set expunge below..." gnus-score-set-expunge-below t]
2294           ["Edit current score file" gnus-score-edit-current-scores t]
2295           ["Edit score file..." gnus-score-edit-file t]
2296           ["Trace score" gnus-score-find-trace t]
2297           ["Find words" gnus-score-find-favourite-words t]
2298           ["Rescore buffer" gnus-summary-rescore t]
2299           ["Increase score..." gnus-summary-increase-score t]
2300           ["Lower score..." gnus-summary-lower-score t]))))
2301
2302     ;; Define both the Article menu in the summary buffer and the
2303     ;; equivalent Commands menu in the article buffer here for
2304     ;; consistency.
2305     (let ((innards
2306            `(("Hide"
2307               ["All" gnus-article-hide t]
2308               ["Headers" gnus-article-hide-headers t]
2309               ["Signature" gnus-article-hide-signature t]
2310               ["Citation" gnus-article-hide-citation t]
2311               ["List identifiers" gnus-article-hide-list-identifiers t]
2312               ["Banner" gnus-article-strip-banner t]
2313               ["Boring headers" gnus-article-hide-boring-headers t])
2314              ("Highlight"
2315               ["All" gnus-article-highlight t]
2316               ["Headers" gnus-article-highlight-headers t]
2317               ["Signature" gnus-article-highlight-signature t]
2318               ["Citation" gnus-article-highlight-citation t])
2319              ("MIME"
2320               ["Words" gnus-article-decode-mime-words t]
2321               ["Charset" gnus-article-decode-charset t]
2322               ["QP" gnus-article-de-quoted-unreadable t]
2323               ["Base64" gnus-article-de-base64-unreadable t]
2324               ["View MIME buttons" gnus-summary-display-buttonized t]
2325               ["View all" gnus-mime-view-all-parts t]
2326               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2327               ["Encrypt body" gnus-article-encrypt-body
2328                :active (not (gnus-group-read-only-p))
2329                ,@(if (featurep 'xemacs) nil
2330                    '(:help "Encrypt the message body on disk"))]
2331               ["Extract all parts..." gnus-summary-save-parts t]
2332               ("Multipart"
2333                ["Repair multipart" gnus-summary-repair-multipart t]
2334                ["Pipe part..." gnus-article-pipe-part t]
2335                ["Inline part" gnus-article-inline-part t]
2336                ["View part as type..." gnus-article-view-part-as-type t]
2337                ["Encrypt body" gnus-article-encrypt-body
2338                 :active (not (gnus-group-read-only-p))
2339                ,@(if (featurep 'xemacs) nil
2340                    '(:help "Encrypt the message body on disk"))]
2341                ["View part externally" gnus-article-view-part-externally t]
2342                ["View HTML parts in browser" gnus-article-browse-html-article t]
2343                ["View part with charset..." gnus-article-view-part-as-charset t]
2344                ["Copy part" gnus-article-copy-part t]
2345                ["Save part..." gnus-article-save-part t]
2346                ["View part" gnus-article-view-part t]))
2347              ("Date"
2348               ["Local" gnus-article-date-local t]
2349               ["ISO8601" gnus-article-date-iso8601 t]
2350               ["UT" gnus-article-date-ut t]
2351               ["Original" gnus-article-date-original t]
2352               ["Lapsed" gnus-article-date-lapsed t]
2353               ["User-defined" gnus-article-date-user t])
2354              ("Display"
2355               ["Remove images" gnus-article-remove-images t]
2356               ["Toggle smiley" gnus-treat-smiley t]
2357               ["Show X-Face" gnus-article-display-x-face t]
2358               ["Show picons in From" gnus-treat-from-picon t]
2359               ["Show picons in mail headers" gnus-treat-mail-picon t]
2360               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2361               ("View as different encoding"
2362                ,@(gnus-summary-menu-split
2363                   (mapcar
2364                    (lambda (cs)
2365                      ;; Since easymenu under Emacs doesn't allow
2366                      ;; lambda forms for menu commands, we should
2367                      ;; provide intern'ed function symbols.
2368                      (let ((command (intern (format "\
2369 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2370                        (fset command
2371                              `(lambda ()
2372                                 (interactive)
2373                                 (let ((gnus-summary-show-article-charset-alist
2374                                        '((1 . ,cs))))
2375                                   (gnus-summary-show-article 1))))
2376                        `[,(symbol-name cs) ,command t]))
2377                    (sort (if (fboundp 'coding-system-list)
2378                              (coding-system-list)
2379                            (mapcar 'car mm-mime-mule-charset-alist))
2380                          'string<)))))
2381              ("Washing"
2382               ("Remove Blanks"
2383                ["Leading" gnus-article-strip-leading-blank-lines t]
2384                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2385                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2386                ["All of the above" gnus-article-strip-blank-lines t]
2387                ["All" gnus-article-strip-all-blank-lines t]
2388                ["Leading space" gnus-article-strip-leading-space t]
2389                ["Trailing space" gnus-article-strip-trailing-space t]
2390                ["Leading space in headers"
2391                 gnus-article-remove-leading-whitespace t])
2392               ["Overstrike" gnus-article-treat-overstrike t]
2393               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2394               ["Emphasis" gnus-article-emphasize t]
2395               ["Word wrap" gnus-article-fill-cited-article t]
2396               ["Fill long lines" gnus-article-fill-long-lines t]
2397               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2398               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2399               ["Remove CR" gnus-article-remove-cr t]
2400               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2401               ["Base64" gnus-article-de-base64-unreadable t]
2402               ["Rot 13" gnus-summary-caesar-message
2403                ,@(if (featurep 'xemacs) '(t)
2404                    '(:help "\"Caesar rotate\" article by 13"))]
2405               ["De-IDNA" gnus-summary-idna-message t]
2406               ["Morse decode" gnus-summary-morse-message t]
2407               ["Unix pipe..." gnus-summary-pipe-message t]
2408               ["Add buttons" gnus-article-add-buttons t]
2409               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2410               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2411               ["Verbose header" gnus-summary-verbose-headers t]
2412               ["Toggle header" gnus-summary-toggle-header t]
2413               ["Unfold headers" gnus-article-treat-unfold-headers t]
2414               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2415               ["Html" gnus-article-wash-html t]
2416               ["Unsplit URLs" gnus-article-unsplit-urls t]
2417               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2418               ["Decode HZ" gnus-article-decode-HZ t]
2419               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2420               ("(Outlook) Deuglify"
2421                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2422                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2423                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2424                ["Full (Outlook) deuglify"
2425                 gnus-article-outlook-deuglify-article t])
2426               )
2427              ("Output"
2428               ["Save in default format..." gnus-summary-save-article
2429                ,@(if (featurep 'xemacs) '(t)
2430                    '(:help "Save article using default method"))]
2431               ["Save in file..." gnus-summary-save-article-file
2432                ,@(if (featurep 'xemacs) '(t)
2433                    '(:help "Save article in file"))]
2434               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2435               ["Save in MH folder..." gnus-summary-save-article-folder t]
2436               ["Save in VM folder..." gnus-summary-save-article-vm t]
2437               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2438               ["Save body in file..." gnus-summary-save-article-body-file t]
2439               ["Pipe through a filter..." gnus-summary-pipe-output t]
2440               ["Add to SOUP packet" gnus-soup-add-article t]
2441               ["Print with Muttprint..." gnus-summary-muttprint t]
2442               ["Print" gnus-summary-print-article
2443                ,@(if (featurep 'xemacs) '(t)
2444                    '(:help "Generate and print a PostScript image"))])
2445              ("Copy, move,... (Backend)"
2446               ,@(if (featurep 'xemacs) nil
2447                   '(:help "Copying, moving, expiring articles..."))
2448               ["Respool article..." gnus-summary-respool-article t]
2449               ["Move article..." gnus-summary-move-article
2450                (gnus-check-backend-function
2451                 'request-move-article gnus-newsgroup-name)]
2452               ["Copy article..." gnus-summary-copy-article t]
2453               ["Crosspost article..." gnus-summary-crosspost-article
2454                (gnus-check-backend-function
2455                 'request-replace-article gnus-newsgroup-name)]
2456               ["Import file..." gnus-summary-import-article
2457                (gnus-check-backend-function
2458                 'request-accept-article gnus-newsgroup-name)]
2459               ["Create article..." gnus-summary-create-article
2460                (gnus-check-backend-function
2461                 'request-accept-article gnus-newsgroup-name)]
2462               ["Check if posted" gnus-summary-article-posted-p t]
2463               ["Edit article" gnus-summary-edit-article
2464                (not (gnus-group-read-only-p))]
2465               ["Delete article" gnus-summary-delete-article
2466                (gnus-check-backend-function
2467                 'request-expire-articles gnus-newsgroup-name)]
2468               ["Query respool" gnus-summary-respool-query t]
2469               ["Trace respool" gnus-summary-respool-trace t]
2470               ["Delete expirable articles" gnus-summary-expire-articles-now
2471                (gnus-check-backend-function
2472                 'request-expire-articles gnus-newsgroup-name)])
2473              ("Extract"
2474               ["Uudecode" gnus-uu-decode-uu
2475                ,@(if (featurep 'xemacs) '(t)
2476                    '(:help "Decode uuencoded article(s)"))]
2477               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2478               ["Unshar" gnus-uu-decode-unshar t]
2479               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2480               ["Save" gnus-uu-decode-save t]
2481               ["Binhex" gnus-uu-decode-binhex t]
2482               ["Postscript" gnus-uu-decode-postscript t]
2483               ["All MIME parts" gnus-summary-save-parts t])
2484              ("Cache"
2485               ["Enter article" gnus-cache-enter-article t]
2486               ["Remove article" gnus-cache-remove-article t])
2487              ["Translate" gnus-article-babel t]
2488              ["Select article buffer" gnus-summary-select-article-buffer t]
2489              ["Make article buffer sticky" gnus-sticky-article t]
2490              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2491              ["Isearch article..." gnus-summary-isearch-article t]
2492              ["Beginning of the article" gnus-summary-beginning-of-article t]
2493              ["End of the article" gnus-summary-end-of-article t]
2494              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2495              ["Fetch referenced articles" gnus-summary-refer-references t]
2496              ["Fetch current thread" gnus-summary-refer-thread t]
2497              ["Fetch article with id..." gnus-summary-refer-article t]
2498              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2499              ["Redisplay" gnus-summary-show-article t]
2500              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2501       (easy-menu-define
2502         gnus-summary-article-menu gnus-summary-mode-map ""
2503         (cons "Article" innards))
2504
2505       (if (not (keymapp gnus-summary-article-menu))
2506           (easy-menu-define
2507             gnus-article-commands-menu gnus-article-mode-map ""
2508             (cons "Commands" innards))
2509         ;; in Emacs, don't share menu.
2510         (setq gnus-article-commands-menu
2511               (copy-keymap gnus-summary-article-menu))
2512         (define-key gnus-article-mode-map [menu-bar commands]
2513           (cons "Commands" gnus-article-commands-menu))))
2514
2515     (easy-menu-define
2516       gnus-summary-thread-menu gnus-summary-mode-map ""
2517       '("Threads"
2518         ["Find all messages in thread" gnus-summary-refer-thread t]
2519         ["Toggle threading" gnus-summary-toggle-threads t]
2520         ["Hide threads" gnus-summary-hide-all-threads t]
2521         ["Show threads" gnus-summary-show-all-threads t]
2522         ["Hide thread" gnus-summary-hide-thread t]
2523         ["Show thread" gnus-summary-show-thread t]
2524         ["Go to next thread" gnus-summary-next-thread t]
2525         ["Go to previous thread" gnus-summary-prev-thread t]
2526         ["Go down thread" gnus-summary-down-thread t]
2527         ["Go up thread" gnus-summary-up-thread t]
2528         ["Top of thread" gnus-summary-top-thread t]
2529         ["Mark thread as read" gnus-summary-kill-thread t]
2530         ["Mark thread as expired" gnus-summary-expire-thread t]
2531         ["Lower thread score" gnus-summary-lower-thread t]
2532         ["Raise thread score" gnus-summary-raise-thread t]
2533         ["Rethread current" gnus-summary-rethread-current t]))
2534
2535     (easy-menu-define
2536       gnus-summary-post-menu gnus-summary-mode-map ""
2537       `("Post"
2538         ["Send a message (mail or news)" gnus-summary-post-news
2539          ,@(if (featurep 'xemacs) '(t)
2540              '(:help "Compose a new message (mail or news)"))]
2541         ["Followup" gnus-summary-followup
2542          ,@(if (featurep 'xemacs) '(t)
2543              '(:help "Post followup to this article"))]
2544         ["Followup and yank" gnus-summary-followup-with-original
2545          ,@(if (featurep 'xemacs) '(t)
2546              '(:help "Post followup to this article, quoting its contents"))]
2547         ["Supersede article" gnus-summary-supersede-article t]
2548         ["Cancel article" gnus-summary-cancel-article
2549          ,@(if (featurep 'xemacs) '(t)
2550              '(:help "Cancel an article you posted"))]
2551         ["Reply" gnus-summary-reply t]
2552         ["Reply and yank" gnus-summary-reply-with-original t]
2553         ["Wide reply" gnus-summary-wide-reply t]
2554         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2555          ,@(if (featurep 'xemacs) '(t)
2556              '(:help "Mail a reply, quoting this article"))]
2557         ["Very wide reply" gnus-summary-very-wide-reply t]
2558         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2559          ,@(if (featurep 'xemacs) '(t)
2560              '(:help "Mail a very wide reply, quoting this article"))]
2561         ["Mail forward" gnus-summary-mail-forward t]
2562         ["Post forward" gnus-summary-post-forward t]
2563         ["Digest and mail" gnus-uu-digest-mail-forward t]
2564         ["Digest and post" gnus-uu-digest-post-forward t]
2565         ["Resend message" gnus-summary-resend-message t]
2566         ["Resend message edit" gnus-summary-resend-message-edit t]
2567         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2568         ["Send a mail" gnus-summary-mail-other-window t]
2569         ["Create a local message" gnus-summary-news-other-window t]
2570         ["Uuencode and post" gnus-uu-post-news
2571          ,@(if (featurep 'xemacs) '(t)
2572              '(:help "Post a uuencoded article"))]
2573         ["Followup via news" gnus-summary-followup-to-mail t]
2574         ["Followup via news and yank"
2575          gnus-summary-followup-to-mail-with-original t]
2576         ["Strip signature on reply"
2577          (lambda ()
2578            (interactive)
2579            (if (not (memq message-cite-function
2580                           '(message-cite-original-without-signature
2581                             message-cite-original)))
2582                ;; Stupid workaround for XEmacs not honoring :visible.
2583                (message "Can't toggle this value of `message-cite-function'")
2584              (setq message-cite-function
2585                    (if (eq message-cite-function
2586                            'message-cite-original-without-signature)
2587                        'message-cite-original
2588                      'message-cite-original-without-signature))))
2589          ;; XEmacs barfs on :visible.
2590          ,@(if (featurep 'xemacs) nil
2591              '(:visible (memq message-cite-function
2592                               '(message-cite-original-without-signature
2593                                 message-cite-original))))
2594          :style toggle
2595          :selected (eq message-cite-function
2596                        'message-cite-original-without-signature)
2597          ,@(if (featurep 'xemacs) nil
2598              '(:help "Strip signature from cited article when replying."))]
2599         ;;("Draft"
2600         ;;["Send" gnus-summary-send-draft t]
2601         ;;["Send bounced" gnus-resend-bounced-mail t])
2602         ))
2603
2604     (cond
2605      ((not (keymapp gnus-summary-post-menu))
2606       (setq gnus-article-post-menu gnus-summary-post-menu))
2607      ((not gnus-article-post-menu)
2608       ;; Don't share post menu.
2609       (setq gnus-article-post-menu
2610             (copy-keymap gnus-summary-post-menu))))
2611     (define-key gnus-article-mode-map [menu-bar post]
2612       (cons "Post" gnus-article-post-menu))
2613
2614     (easy-menu-define
2615       gnus-summary-misc-menu gnus-summary-mode-map ""
2616       `("Gnus"
2617         ("Mark Read"
2618          ["Mark as read" gnus-summary-mark-as-read-forward t]
2619          ["Mark same subject and select"
2620           gnus-summary-kill-same-subject-and-select t]
2621          ["Mark same subject" gnus-summary-kill-same-subject t]
2622          ["Catchup" gnus-summary-catchup
2623           ,@(if (featurep 'xemacs) '(t)
2624               '(:help "Mark unread articles in this group as read"))]
2625          ["Catchup all" gnus-summary-catchup-all t]
2626          ["Catchup to here" gnus-summary-catchup-to-here t]
2627          ["Catchup from here" gnus-summary-catchup-from-here t]
2628          ["Catchup region" gnus-summary-mark-region-as-read
2629           (gnus-mark-active-p)]
2630          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2631         ("Mark Various"
2632          ["Tick" gnus-summary-tick-article-forward t]
2633          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2634          ["Remove marks" gnus-summary-clear-mark-forward t]
2635          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2636          ["Set bookmark" gnus-summary-set-bookmark t]
2637          ["Remove bookmark" gnus-summary-remove-bookmark 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         ("Registry Marks")
2684         ("Scroll article"
2685          ["Page forward" gnus-summary-next-page
2686           ,@(if (featurep 'xemacs) '(t)
2687               '(:help "Show next page of article"))]
2688          ["Page backward" gnus-summary-prev-page
2689           ,@(if (featurep 'xemacs) '(t)
2690               '(:help "Show previous page of article"))]
2691          ["Line forward" gnus-summary-scroll-up t])
2692         ("Move"
2693          ["Next unread article" gnus-summary-next-unread-article t]
2694          ["Previous unread article" gnus-summary-prev-unread-article t]
2695          ["Next article" gnus-summary-next-article t]
2696          ["Previous article" gnus-summary-prev-article t]
2697          ["Next unread subject" gnus-summary-next-unread-subject t]
2698          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2699          ["Next article same subject" gnus-summary-next-same-subject t]
2700          ["Previous article same subject" gnus-summary-prev-same-subject t]
2701          ["First unread article" gnus-summary-first-unread-article t]
2702          ["Best unread article" gnus-summary-best-unread-article t]
2703          ["Go to subject number..." gnus-summary-goto-subject t]
2704          ["Go to article number..." gnus-summary-goto-article t]
2705          ["Go to the last article" gnus-summary-goto-last-article t]
2706          ["Pop article off history" gnus-summary-pop-article t])
2707         ("Sort"
2708          ["Sort by number" gnus-summary-sort-by-number t]
2709          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2710          ["Sort by author" gnus-summary-sort-by-author t]
2711          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2712          ["Sort by subject" gnus-summary-sort-by-subject t]
2713          ["Sort by date" gnus-summary-sort-by-date t]
2714          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2715          ["Sort by score" gnus-summary-sort-by-score t]
2716          ["Sort by lines" gnus-summary-sort-by-lines t]
2717          ["Sort by characters" gnus-summary-sort-by-chars t]
2718          ["Randomize" gnus-summary-sort-by-random t]
2719          ["Original sort" gnus-summary-sort-by-original t])
2720         ("Help"
2721          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2722          ["Describe group" gnus-summary-describe-group t]
2723          ["Fetch charter" gnus-group-fetch-charter
2724           ,@(if (featurep 'xemacs) nil
2725               '(:help "Display the charter of the current group"))]
2726          ["Fetch control message" gnus-group-fetch-control
2727           ,@(if (featurep 'xemacs) nil
2728               '(:help "Display the archived control message for the current group"))]
2729          ["Read manual" gnus-info-find-node t])
2730         ("Modes"
2731          ["Pick and read" gnus-pick-mode t]
2732          ["Binary" gnus-binary-mode t])
2733         ("Regeneration"
2734          ["Regenerate" gnus-summary-prepare t]
2735          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2736          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2737          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2738          ["Toggle threading" gnus-summary-toggle-threads t])
2739         ["See old articles" gnus-summary-insert-old-articles t]
2740         ["See new articles" gnus-summary-insert-new-articles t]
2741         ["Filter articles..." gnus-summary-execute-command t]
2742         ["Run command on articles..." gnus-summary-universal-argument t]
2743         ["Search articles forward..." gnus-summary-search-article-forward t]
2744         ["Search articles backward..." gnus-summary-search-article-backward t]
2745         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2746         ["Expand window" gnus-summary-expand-window t]
2747         ["Expire expirable articles" gnus-summary-expire-articles
2748          (gnus-check-backend-function
2749           'request-expire-articles gnus-newsgroup-name)]
2750         ["Edit local kill file" gnus-summary-edit-local-kill t]
2751         ["Edit main kill file" gnus-summary-edit-global-kill t]
2752         ["Edit group parameters" gnus-summary-edit-parameters t]
2753         ["Customize group parameters" gnus-summary-customize-parameters t]
2754         ["Send a bug report" gnus-bug t]
2755         ("Exit"
2756          ["Catchup and exit" gnus-summary-catchup-and-exit
2757           ,@(if (featurep 'xemacs) '(t)
2758               '(:help "Mark unread articles in this group as read, then exit"))]
2759          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2760          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2761          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2762          ["Exit group" gnus-summary-exit
2763           ,@(if (featurep 'xemacs) '(t)
2764               '(:help "Exit current group, return to group selection mode"))]
2765          ["Exit group without updating" gnus-summary-exit-no-update t]
2766          ["Exit and goto next group" gnus-summary-next-group t]
2767          ["Exit and goto prev group" gnus-summary-prev-group t]
2768          ["Reselect group" gnus-summary-reselect-current-group t]
2769          ["Rescan group" gnus-summary-rescan-group t]
2770          ["Update dribble" gnus-summary-save-newsrc t])))
2771
2772     (gnus-run-hooks 'gnus-summary-menu-hook)))
2773
2774 (defvar gnus-summary-tool-bar-map nil)
2775
2776 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2777 ;; affect _new_ message buffers.  We might add a function that walks thru all
2778 ;; summary-mode buffers and force the update.
2779 (defun gnus-summary-tool-bar-update (&optional symbol value)
2780   "Update summary mode toolbar.
2781 Setter function for custom variables."
2782   (setq-default gnus-summary-tool-bar-map nil)
2783   (when symbol
2784     ;; When used as ":set" function:
2785     (set-default symbol value))
2786   (when (gnus-buffer-live-p gnus-summary-buffer)
2787     (with-current-buffer gnus-summary-buffer
2788       (gnus-summary-make-tool-bar))))
2789
2790 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2791                                      'gnus-summary-tool-bar-gnome
2792                                    'gnus-summary-tool-bar-retro)
2793   "Specifies the Gnus summary tool bar.
2794
2795 It can be either a list or a symbol refering to a list.  See
2796 `gmm-tool-bar-from-list' for the format of the list.  The
2797 default key map is `gnus-summary-mode-map'.
2798
2799 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2800 `gnus-summary-tool-bar-retro'."
2801   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2802                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2803                  (repeat :tag "User defined list" gmm-tool-bar-item)
2804                  (symbol))
2805   :version "23.1" ;; No Gnus
2806   :initialize 'custom-initialize-default
2807   :set 'gnus-summary-tool-bar-update
2808   :group 'gnus-summary)
2809
2810 (defcustom gnus-summary-tool-bar-gnome
2811   '((gnus-summary-post-news "mail/compose" nil)
2812     (gnus-summary-insert-new-articles "mail/inbox" nil
2813                                       :visible (or (not gnus-agent)
2814                                                    gnus-plugged))
2815     (gnus-summary-reply-with-original "mail/reply")
2816     (gnus-summary-reply "mail/reply" nil :visible nil)
2817     (gnus-summary-followup-with-original "mail/reply-all")
2818     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2819     (gnus-summary-mail-forward "mail/forward")
2820     (gnus-summary-save-article "mail/save")
2821     (gnus-summary-search-article-forward "search" nil :visible nil)
2822     (gnus-summary-print-article "print")
2823     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2824     ;; Some new commands that may need more suitable icons:
2825     (gnus-summary-save-newsrc "save" nil :visible nil)
2826     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2827     (gnus-summary-prev-article "left-arrow")
2828     (gnus-summary-next-article "right-arrow")
2829     (gnus-summary-next-page "next-page")
2830     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2831     ;;
2832     ;; Maybe some sort-by-... could be added:
2833     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2834     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2835     (gnus-summary-mark-as-expirable
2836      "delete" nil
2837      :visible (gnus-check-backend-function 'request-expire-articles
2838                                            gnus-newsgroup-name))
2839     (gnus-summary-mark-as-spam
2840      "mail/spam" t
2841      :visible (and (fboundp 'spam-group-ham-contents-p)
2842                    (spam-group-ham-contents-p gnus-newsgroup-name))
2843      :help "Mark as spam")
2844     (gnus-summary-mark-as-read-forward
2845      "mail/not-spam" nil
2846      :visible (and (fboundp 'spam-group-spam-contents-p)
2847                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2848     ;;
2849     (gnus-summary-exit "exit")
2850     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2851     (gnus-info-find-node "help"))
2852   "List of functions for the summary tool bar (GNOME style).
2853
2854 See `gmm-tool-bar-from-list' for the format of the list."
2855   :type '(repeat gmm-tool-bar-item)
2856   :version "23.1" ;; No Gnus
2857   :initialize 'custom-initialize-default
2858   :set 'gnus-summary-tool-bar-update
2859   :group 'gnus-summary)
2860
2861 (defcustom gnus-summary-tool-bar-retro
2862   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2863     (gnus-summary-next-unread-article "gnus/next-ur")
2864     (gnus-summary-post-news "gnus/post")
2865     (gnus-summary-followup-with-original "gnus/fuwo")
2866     (gnus-summary-followup "gnus/followup")
2867     (gnus-summary-reply-with-original "gnus/reply-wo")
2868     (gnus-summary-reply "gnus/reply")
2869     (gnus-summary-caesar-message "gnus/rot13")
2870     (gnus-uu-decode-uu "gnus/uu-decode")
2871     (gnus-summary-save-article-file "gnus/save-aif")
2872     (gnus-summary-save-article "gnus/save-art")
2873     (gnus-uu-post-news "gnus/uu-post")
2874     (gnus-summary-catchup "gnus/catchup")
2875     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2876     (gnus-summary-exit "gnus/exit-summ")
2877     ;; Some new command that may need more suitable icons:
2878     (gnus-summary-print-article "gnus/print" nil :visible nil)
2879     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2880     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2881     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2882     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2883     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2884     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2885     ;;
2886     (gnus-info-find-node "gnus/help" nil :visible nil))
2887   "List of functions for the summary tool bar (retro look).
2888
2889 See `gmm-tool-bar-from-list' for the format of the list."
2890   :type '(repeat gmm-tool-bar-item)
2891   :version "23.1" ;; No Gnus
2892   :initialize 'custom-initialize-default
2893   :set 'gnus-summary-tool-bar-update
2894   :group 'gnus-summary)
2895
2896 (defcustom gnus-summary-tool-bar-zap-list t
2897   "List of icon items from the global tool bar.
2898 These items are not displayed in the Gnus summary mode tool bar.
2899
2900 See `gmm-tool-bar-from-list' for the format of the list."
2901   :type 'gmm-tool-bar-zap-list
2902   :version "23.1" ;; No Gnus
2903   :initialize 'custom-initialize-default
2904   :set 'gnus-summary-tool-bar-update
2905   :group 'gnus-summary)
2906
2907 (defvar image-load-path)
2908 (defvar tool-bar-map)
2909
2910 (defun gnus-summary-make-tool-bar (&optional force)
2911   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2912 When FORCE, rebuild the tool bar."
2913   (when (and (not (featurep 'xemacs))
2914              (boundp 'tool-bar-mode)
2915              tool-bar-mode
2916              (or (not gnus-summary-tool-bar-map) force))
2917     (let* ((load-path
2918             (gmm-image-load-path-for-library "gnus"
2919                                              "mail/save.xpm"
2920                                              nil t))
2921            (image-load-path (cons (car load-path)
2922                                   (when (boundp 'image-load-path)
2923                                     image-load-path)))
2924            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2925                                         gnus-summary-tool-bar-zap-list
2926                                         'gnus-summary-mode-map)))
2927       (when map
2928         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2929         ;; uses it's value.
2930         (setq gnus-summary-tool-bar-map map))))
2931   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2932
2933 (defun gnus-score-set-default (var value)
2934   "A version of set that updates the GNU Emacs menu-bar."
2935   (set var value)
2936   ;; It is the message that forces the active status to be updated.
2937   (message ""))
2938
2939 (defun gnus-make-score-map (type)
2940   "Make a summary score map of type TYPE."
2941   (if t
2942       nil
2943     (let ((headers '(("author" "from" string)
2944                      ("subject" "subject" string)
2945                      ("article body" "body" string)
2946                      ("article head" "head" string)
2947                      ("xref" "xref" string)
2948                      ("extra header" "extra" string)
2949                      ("lines" "lines" number)
2950                      ("followups to author" "followup" string)))
2951           (types '((number ("less than" <)
2952                            ("greater than" >)
2953                            ("equal" =))
2954                    (string ("substring" s)
2955                            ("exact string" e)
2956                            ("fuzzy string" f)
2957                            ("regexp" r))))
2958           (perms '(("temporary" (current-time-string))
2959                    ("permanent" nil)
2960                    ("immediate" now)))
2961           header)
2962       (list
2963        (apply
2964         'nconc
2965         (list
2966          (if (eq type 'lower)
2967              "Lower score"
2968            "Increase score"))
2969         (let (outh)
2970           (while headers
2971             (setq header (car headers))
2972             (setq outh
2973                   (cons
2974                    (apply
2975                     'nconc
2976                     (list (car header))
2977                     (let ((ts (cdr (assoc (nth 2 header) types)))
2978                           outt)
2979                       (while ts
2980                         (setq outt
2981                               (cons
2982                                (apply
2983                                 'nconc
2984                                 (list (caar ts))
2985                                 (let ((ps perms)
2986                                       outp)
2987                                   (while ps
2988                                     (setq outp
2989                                           (cons
2990                                            (vector
2991                                             (caar ps)
2992                                             (list
2993                                              'gnus-summary-score-entry
2994                                              (nth 1 header)
2995                                              (if (or (string= (nth 1 header)
2996                                                               "head")
2997                                                      (string= (nth 1 header)
2998                                                               "body"))
2999                                                  ""
3000                                                (list 'gnus-summary-header
3001                                                      (nth 1 header)))
3002                                              (list 'quote (nth 1 (car ts)))
3003                                              (list 'gnus-score-delta-default
3004                                                    nil)
3005                                              (nth 1 (car ps))
3006                                              t)
3007                                             t)
3008                                            outp))
3009                                     (setq ps (cdr ps)))
3010                                   (list (nreverse outp))))
3011                                outt))
3012                         (setq ts (cdr ts)))
3013                       (list (nreverse outt))))
3014                    outh))
3015             (setq headers (cdr headers)))
3016           (list (nreverse outh))))))))
3017
3018
3019 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3020
3021 \f
3022
3023 (defun gnus-summary-mode (&optional group)
3024   "Major mode for reading articles.
3025
3026 All normal editing commands are switched off.
3027 \\<gnus-summary-mode-map>
3028 Each line in this buffer represents one article.  To read an
3029 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3030 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3031 respectively.
3032
3033 You can also post articles and send mail from this buffer.  To
3034 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3035 of an article, type `\\[gnus-summary-reply]'.
3036
3037 There are approx. one gazillion commands you can execute in this
3038 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3039
3040 The following commands are available:
3041
3042 \\{gnus-summary-mode-map}"
3043   (interactive)
3044   (kill-all-local-variables)
3045   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3046     (gnus-summary-make-local-variables))
3047   (gnus-summary-make-local-variables)
3048   (setq gnus-newsgroup-name group)
3049   (when (gnus-visual-p 'summary-menu 'menu)
3050     (gnus-summary-make-menu-bar)
3051     (gnus-summary-make-tool-bar))
3052   (gnus-make-thread-indent-array)
3053   (gnus-simplify-mode-line)
3054   (setq major-mode 'gnus-summary-mode)
3055   (setq mode-name "Summary")
3056   (make-local-variable 'minor-mode-alist)
3057   (use-local-map gnus-summary-mode-map)
3058   (buffer-disable-undo)
3059   (setq buffer-read-only t              ;Disable modification
3060         show-trailing-whitespace nil)
3061   (setq truncate-lines t)
3062   (add-to-invisibility-spec '(gnus-sum . t))
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 gnus-summary-display-arrow
3459     (make-local-variable 'overlay-arrow-position)
3460     (make-local-variable '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     (condition-case ()
3744         (gnus-put-text-property
3745          (point)
3746          (progn (eval gnus-summary-line-format-spec) (point))
3747          'gnus-number gnus-tmp-number)
3748       (error (gnus-message 5 "Error updating the summary line")))
3749     (when (gnus-visual-p 'summary-highlight 'highlight)
3750       (forward-line -1)
3751       (gnus-run-hooks 'gnus-summary-update-hook)
3752       (forward-line 1))))
3753
3754 (defun gnus-summary-update-line (&optional dont-update)
3755   "Update summary line after change."
3756   (when (and gnus-summary-default-score
3757              (not gnus-summary-inhibit-highlight))
3758     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3759            (article (gnus-summary-article-number))
3760            (score (gnus-summary-article-score article)))
3761       (unless dont-update
3762         (if (and gnus-summary-mark-below
3763                  (< (gnus-summary-article-score)
3764                     gnus-summary-mark-below))
3765             ;; This article has a low score, so we mark it as read.
3766             (when (memq article gnus-newsgroup-unreads)
3767               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3768           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3769             ;; This article was previously marked as read on account
3770             ;; of a low score, but now it has risen, so we mark it as
3771             ;; unread.
3772             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3773         (gnus-summary-update-mark
3774          (if (or (null gnus-summary-default-score)
3775                  (<= (abs (- score gnus-summary-default-score))
3776                      gnus-summary-zcore-fuzz))
3777              ?                          ;Whitespace
3778            (if (< score gnus-summary-default-score)
3779                gnus-score-below-mark gnus-score-over-mark))
3780          'score))
3781       ;; Do visual highlighting.
3782       (when (gnus-visual-p 'summary-highlight 'highlight)
3783         (gnus-run-hooks 'gnus-summary-update-hook)))))
3784
3785 (defvar gnus-tmp-new-adopts nil)
3786
3787 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3788   "Return the number of articles in THREAD.
3789 This may be 0 in some cases -- if none of the articles in
3790 the thread are to be displayed."
3791   (let* ((number
3792          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3793           (cond
3794            ((not (listp thread))
3795             1)
3796            ((and (consp thread) (cdr thread))
3797             (apply
3798              '+ 1 (mapcar
3799                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3800            ((null thread)
3801             1)
3802            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3803             1)
3804            (t 0))))
3805     (when (and level (zerop level) gnus-tmp-new-adopts)
3806       (incf number
3807             (apply '+ (mapcar
3808                        'gnus-summary-number-of-articles-in-thread
3809                        gnus-tmp-new-adopts))))
3810     (if char
3811         (if (> number 1) gnus-not-empty-thread-mark
3812           gnus-empty-thread-mark)
3813       number)))
3814
3815 (defsubst gnus-summary-line-message-size (head)
3816   "Return pretty-printed version of message size.
3817 This function is intended to be used in
3818 `gnus-summary-line-format-alist'."
3819   (let ((c (or (mail-header-chars head) -1)))
3820     (cond ((< c 0) "n/a")               ; chars not available
3821           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3822           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3823           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3824           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3825
3826
3827 (defun gnus-summary-set-local-parameters (group)
3828   "Go through the local params of GROUP and set all variable specs in that list."
3829   (let ((vars '(quit-config)))          ; Ignore quit-config.
3830     (dolist (elem (gnus-group-find-parameter group))
3831       (and (consp elem)                 ; Has to be a cons.
3832            (consp (cdr elem))           ; The cdr has to be a list.
3833            (symbolp (car elem))         ; Has to be a symbol in there.
3834            (not (memq (car elem) vars))
3835            (ignore-errors
3836              (push (car elem) vars)
3837              ;; Variables like `gnus-show-threads' that are globally
3838              ;; bound, if used as group parameters, need to get to be
3839              ;; buffer-local, whereas just parameters like `gcc-self',
3840              ;; `timestamp', etc. should not be bound as variables.
3841              (if (boundp (car elem))
3842                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3843                (eval (nth 1 elem))))))))
3844
3845 (defun gnus-summary-read-group (group &optional show-all no-article
3846                                       kill-buffer no-display backward
3847                                       select-articles)
3848   "Start reading news in newsgroup GROUP.
3849 If SHOW-ALL is non-nil, already read articles are also listed.
3850 If NO-ARTICLE is non-nil, no article is selected initially.
3851 If NO-DISPLAY, don't generate a summary buffer."
3852   (let (result)
3853     (while (and group
3854                 (null (setq result
3855                             (let ((gnus-auto-select-next nil))
3856                               (or (gnus-summary-read-group-1
3857                                    group show-all no-article
3858                                    kill-buffer no-display
3859                                    select-articles)
3860                                   (setq show-all nil
3861                                         select-articles nil)))))
3862                 (eq gnus-auto-select-next 'quietly))
3863       (set-buffer gnus-group-buffer)
3864       ;; The entry function called above goes to the next
3865       ;; group automatically, so we go two groups back
3866       ;; if we are searching for the previous group.
3867       (when backward
3868         (gnus-group-prev-unread-group 2))
3869       (if (not (equal group (gnus-group-group-name)))
3870           (setq group (gnus-group-group-name))
3871         (setq group nil)))
3872     result))
3873
3874 (defun gnus-summary-read-group-1 (group show-all no-article
3875                                         kill-buffer no-display
3876                                         &optional select-articles)
3877   ;; Killed foreign groups can't be entered.
3878   ;;  (when (and (not (gnus-group-native-p group))
3879   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3880   ;;    (error "Dead non-native groups can't be entered"))
3881   (gnus-message 5 "Retrieving newsgroup: %s..."
3882                 (gnus-group-decoded-name group))
3883   (let* ((new-group (gnus-summary-setup-buffer group))
3884          (quit-config (gnus-group-quit-config group))
3885          (did-select (and new-group (gnus-select-newsgroup
3886                                      group show-all select-articles))))
3887     (cond
3888      ;; This summary buffer exists already, so we just select it.
3889      ((not new-group)
3890       (gnus-set-global-variables)
3891       (when kill-buffer
3892         (gnus-kill-or-deaden-summary kill-buffer))
3893       (gnus-configure-windows 'summary 'force)
3894       (gnus-set-mode-line 'summary)
3895       (gnus-summary-position-point)
3896       (message "")
3897       t)
3898      ;; We couldn't select this group.
3899      ((null did-select)
3900       (when (and (eq major-mode 'gnus-summary-mode)
3901                  (not (equal (current-buffer) kill-buffer)))
3902         (kill-buffer (current-buffer))
3903         (if (not quit-config)
3904             (progn
3905               ;; Update the info -- marks might need to be removed,
3906               ;; for instance.
3907               (gnus-summary-update-info)
3908               (set-buffer gnus-group-buffer)
3909               (gnus-group-jump-to-group group)
3910               (gnus-group-next-unread-group 1))
3911           (gnus-handle-ephemeral-exit quit-config)))
3912       (let ((grpinfo (gnus-get-info group)))
3913         (if (null (gnus-info-read grpinfo))
3914             (gnus-message 3 "Group %s contains no messages"
3915                           (gnus-group-decoded-name group))
3916           (gnus-message 3 "Can't select group")))
3917       nil)
3918      ;; The user did a `C-g' while prompting for number of articles,
3919      ;; so we exit this group.
3920      ((eq did-select 'quit)
3921       (and (eq major-mode 'gnus-summary-mode)
3922            (not (equal (current-buffer) kill-buffer))
3923            (kill-buffer (current-buffer)))
3924       (when kill-buffer
3925         (gnus-kill-or-deaden-summary kill-buffer))
3926       (if (not quit-config)
3927           (progn
3928             (set-buffer gnus-group-buffer)
3929             (gnus-group-jump-to-group group)
3930             (gnus-group-next-unread-group 1)
3931             (gnus-configure-windows 'group 'force))
3932         (gnus-handle-ephemeral-exit quit-config))
3933       ;; Finally signal the quit.
3934       (signal 'quit nil))
3935      ;; The group was successfully selected.
3936      (t
3937       (gnus-set-global-variables)
3938       ;; Save the active value in effect when the group was entered.
3939       (setq gnus-newsgroup-active
3940             (gnus-copy-sequence
3941              (gnus-active gnus-newsgroup-name)))
3942       ;; You can change the summary buffer in some way with this hook.
3943       (gnus-run-hooks 'gnus-select-group-hook)
3944       (when (memq 'summary (gnus-update-format-specifications
3945                             nil 'summary 'summary-mode 'summary-dummy))
3946         ;; The format specification for the summary line was updated,
3947         ;; so we need to update the mark positions as well.
3948         (gnus-update-summary-mark-positions))
3949       ;; Do score processing.
3950       (when gnus-use-scoring
3951         (gnus-possibly-score-headers))
3952       ;; Check whether to fill in the gaps in the threads.
3953       (when gnus-build-sparse-threads
3954         (gnus-build-sparse-threads))
3955       ;; Find the initial limit.
3956       (if show-all
3957           (let ((gnus-newsgroup-dormant nil))
3958             (gnus-summary-initial-limit show-all))
3959         (gnus-summary-initial-limit show-all))
3960       ;; Generate the summary buffer.
3961       (unless no-display
3962         (gnus-summary-prepare))
3963       (when gnus-use-trees
3964         (gnus-tree-open group)
3965         (setq gnus-summary-highlight-line-function
3966               'gnus-tree-highlight-article))
3967       ;; If the summary buffer is empty, but there are some low-scored
3968       ;; articles or some excluded dormants, we include these in the
3969       ;; buffer.
3970       (when (and (zerop (buffer-size))
3971                  (not no-display))
3972         (cond (gnus-newsgroup-dormant
3973                (gnus-summary-limit-include-dormant))
3974               ((and gnus-newsgroup-scored show-all)
3975                (gnus-summary-limit-include-expunged t))))
3976       ;; Function `gnus-apply-kill-file' must be called in this hook.
3977       (gnus-run-hooks 'gnus-apply-kill-hook)
3978       (if (and (zerop (buffer-size))
3979                (not no-display))
3980           (progn
3981             ;; This newsgroup is empty.
3982             (gnus-summary-catchup-and-exit nil t)
3983             (gnus-message 6 "No unread news")
3984             (when kill-buffer
3985               (gnus-kill-or-deaden-summary kill-buffer))
3986             ;; Return nil from this function.
3987             nil)
3988         ;; Hide conversation thread subtrees.  We cannot do this in
3989         ;; gnus-summary-prepare-hook since kill processing may not
3990         ;; work with hidden articles.
3991         (gnus-summary-maybe-hide-threads)
3992         (when kill-buffer
3993           (gnus-kill-or-deaden-summary kill-buffer))
3994         (gnus-summary-auto-select-subject)
3995         ;; Show first unread article if requested.
3996         (if (and (not no-article)
3997                  (not no-display)
3998                  gnus-newsgroup-unreads
3999                  gnus-auto-select-first)
4000             (progn
4001               (gnus-configure-windows 'summary)
4002               (let ((art (gnus-summary-article-number)))
4003                 (unless (and (not gnus-plugged)
4004                              (or (memq art gnus-newsgroup-undownloaded)
4005                                  (memq art gnus-newsgroup-downloadable)))
4006                   (gnus-summary-goto-article art))))
4007           ;; Don't select any articles.
4008           (gnus-summary-position-point)
4009           (gnus-configure-windows 'summary 'force)
4010           (gnus-set-mode-line 'summary))
4011         (when (and gnus-auto-center-group
4012                    (get-buffer-window gnus-group-buffer t))
4013           ;; Gotta use windows, because recenter does weird stuff if
4014           ;; the current buffer ain't the displayed window.
4015           (let ((owin (selected-window)))
4016             (select-window (get-buffer-window gnus-group-buffer t))
4017             (when (gnus-group-goto-group group)
4018               (recenter))
4019             (select-window owin)))
4020         ;; Mark this buffer as "prepared".
4021         (setq gnus-newsgroup-prepared t)
4022         (gnus-run-hooks 'gnus-summary-prepared-hook)
4023         (unless (gnus-ephemeral-group-p group)
4024           (gnus-group-update-group group))
4025         t)))))
4026
4027 (defun gnus-summary-auto-select-subject ()
4028   "Select the subject line on initial group entry."
4029   (goto-char (point-min))
4030   (cond
4031    ((eq gnus-auto-select-subject 'best)
4032     (gnus-summary-best-unread-subject))
4033    ((eq gnus-auto-select-subject 'unread)
4034     (gnus-summary-first-unread-subject))
4035    ((eq gnus-auto-select-subject 'unseen)
4036     (gnus-summary-first-unseen-subject))
4037    ((eq gnus-auto-select-subject 'unseen-or-unread)
4038     (gnus-summary-first-unseen-or-unread-subject))
4039    ((eq gnus-auto-select-subject 'first)
4040     ;; Do nothing.
4041     )
4042    ((functionp gnus-auto-select-subject)
4043     (funcall gnus-auto-select-subject))))
4044
4045 (defun gnus-summary-prepare ()
4046   "Generate the summary buffer."
4047   (interactive)
4048   (let ((inhibit-read-only t))
4049     (erase-buffer)
4050     (setq gnus-newsgroup-data nil
4051           gnus-newsgroup-data-reverse nil)
4052     (gnus-run-hooks 'gnus-summary-generate-hook)
4053     ;; Generate the buffer, either with threads or without.
4054     (when gnus-newsgroup-headers
4055       (gnus-summary-prepare-threads
4056        (if gnus-show-threads
4057            (gnus-sort-gathered-threads
4058             (funcall gnus-summary-thread-gathering-function
4059                      (gnus-sort-threads
4060                       (gnus-cut-threads (gnus-make-threads)))))
4061          ;; Unthreaded display.
4062          (gnus-sort-articles gnus-newsgroup-headers))))
4063     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4064     ;; Call hooks for modifying summary buffer.
4065     (goto-char (point-min))
4066     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4067
4068 (defsubst gnus-general-simplify-subject (subject)
4069   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4070   (setq subject
4071         (cond
4072          ;; Truncate the subject.
4073          (gnus-simplify-subject-functions
4074           (gnus-map-function gnus-simplify-subject-functions subject))
4075          ((numberp gnus-summary-gather-subject-limit)
4076           (setq subject (gnus-simplify-subject-re subject))
4077           (if (> (length subject) gnus-summary-gather-subject-limit)
4078               (substring subject 0 gnus-summary-gather-subject-limit)
4079             subject))
4080          ;; Fuzzily simplify it.
4081          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4082           (gnus-simplify-subject-fuzzy subject))
4083          ;; Just remove the leading "Re:".
4084          (t
4085           (gnus-simplify-subject-re subject))))
4086
4087   (if (and gnus-summary-gather-exclude-subject
4088            (string-match gnus-summary-gather-exclude-subject subject))
4089       nil                         ; This article shouldn't be gathered
4090     subject))
4091
4092 (defun gnus-summary-simplify-subject-query ()
4093   "Query where the respool algorithm would put this article."
4094   (interactive)
4095   (gnus-summary-select-article)
4096   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4097
4098 (defun gnus-gather-threads-by-subject (threads)
4099   "Gather threads by looking at Subject headers."
4100   (if (not gnus-summary-make-false-root)
4101       threads
4102     (let ((hashtb (gnus-make-hashtable 1024))
4103           (prev threads)
4104           (result threads)
4105           subject hthread whole-subject)
4106       (while threads
4107         (setq subject (gnus-general-simplify-subject
4108                        (setq whole-subject (mail-header-subject
4109                                             (caar threads)))))
4110         (when subject
4111           (if (setq hthread (gnus-gethash subject hashtb))
4112               (progn
4113                 ;; We enter a dummy root into the thread, if we
4114                 ;; haven't done that already.
4115                 (unless (stringp (caar hthread))
4116                   (setcar hthread (list whole-subject (car hthread))))
4117                 ;; We add this new gathered thread to this gathered
4118                 ;; thread.
4119                 (setcdr (car hthread)
4120                         (nconc (cdar hthread) (list (car threads))))
4121                 ;; Remove it from the list of threads.
4122                 (setcdr prev (cdr threads))
4123                 (setq threads prev))
4124             ;; Enter this thread into the hash table.
4125             (gnus-sethash subject
4126                           (if gnus-summary-make-false-root-always
4127                               (progn
4128                                 ;; If you want a dummy root above all
4129                                 ;; threads...
4130                                 (setcar threads (list whole-subject
4131                                                       (car threads)))
4132                                 threads)
4133                             threads)
4134                           hashtb)))
4135         (setq prev threads)
4136         (setq threads (cdr threads)))
4137       result)))
4138
4139 (defun gnus-gather-threads-by-references (threads)
4140   "Gather threads by looking at References headers."
4141   (let ((idhashtb (gnus-make-hashtable 1024))
4142         (thhashtb (gnus-make-hashtable 1024))
4143         (prev threads)
4144         (result threads)
4145         ids references id gthread gid entered ref)
4146     (while threads
4147       (when (setq references (mail-header-references (caar threads)))
4148         (setq id (mail-header-id (caar threads))
4149               ids (inline (gnus-split-references references))
4150               entered nil)
4151         (while (setq ref (pop ids))
4152           (setq ids (delete ref ids))
4153           (if (not (setq gid (gnus-gethash ref idhashtb)))
4154               (progn
4155                 (gnus-sethash ref id idhashtb)
4156                 (gnus-sethash id threads thhashtb))
4157             (setq gthread (gnus-gethash gid thhashtb))
4158             (unless entered
4159               ;; We enter a dummy root into the thread, if we
4160               ;; haven't done that already.
4161               (unless (stringp (caar gthread))
4162                 (setcar gthread (list (mail-header-subject (caar gthread))
4163                                       (car gthread))))
4164               ;; We add this new gathered thread to this gathered
4165               ;; thread.
4166               (setcdr (car gthread)
4167                       (nconc (cdar gthread) (list (car threads)))))
4168             ;; Add it into the thread hash table.
4169             (gnus-sethash id gthread thhashtb)
4170             (setq entered t)
4171             ;; Remove it from the list of threads.
4172             (setcdr prev (cdr threads))
4173             (setq threads prev))))
4174       (setq prev threads)
4175       (setq threads (cdr threads)))
4176     result))
4177
4178 (defun gnus-sort-gathered-threads (threads)
4179   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4180   (let ((result threads))
4181     (while threads
4182       (when (stringp (caar threads))
4183         (setcdr (car threads)
4184                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4185       (setq threads (cdr threads)))
4186     result))
4187
4188 (defun gnus-thread-loop-p (root thread)
4189   "Say whether ROOT is in THREAD."
4190   (let ((stack (list thread))
4191         (infloop 0)
4192         th)
4193     (while (setq thread (pop stack))
4194       (setq th (cdr thread))
4195       (while (and th
4196                   (not (eq (caar th) root)))
4197         (pop th))
4198       (if th
4199           ;; We have found a loop.
4200           (let (ref-dep)
4201             (setcdr thread (delq (car th) (cdr thread)))
4202             (if (boundp (setq ref-dep (intern "none"
4203                                               gnus-newsgroup-dependencies)))
4204                 (setcdr (symbol-value ref-dep)
4205                         (nconc (cdr (symbol-value ref-dep))
4206                                (list (car th))))
4207               (set ref-dep (list nil (car th))))
4208             (setq infloop 1
4209                   stack nil))
4210         ;; Push all the subthreads onto the stack.
4211         (push (cdr thread) stack)))
4212     infloop))
4213
4214 (defun gnus-make-threads ()
4215   "Go through the dependency hashtb and find the roots.  Return all threads."
4216   (let (threads)
4217     (while (catch 'infloop
4218              (mapatoms
4219               (lambda (refs)
4220                 ;; Deal with self-referencing References loops.
4221                 (when (and (car (symbol-value refs))
4222                            (not (zerop
4223                                  (apply
4224                                   '+
4225                                   (mapcar
4226                                    (lambda (thread)
4227                                      (gnus-thread-loop-p
4228                                       (car (symbol-value refs)) thread))
4229                                    (cdr (symbol-value refs)))))))
4230                   (setq threads nil)
4231                   (throw 'infloop t))
4232                 (unless (car (symbol-value refs))
4233                   ;; These threads do not refer back to any other
4234                   ;; articles, so they're roots.
4235                   (setq threads (append (cdr (symbol-value refs)) threads))))
4236               gnus-newsgroup-dependencies)))
4237     threads))
4238
4239 ;; Build the thread tree.
4240 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4241   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4242
4243 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4244 if it was already present.
4245
4246 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4247 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4248 Message-IDs will be renamed to a unique Message-ID before being
4249 entered.
4250
4251 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4252   (let* ((id (mail-header-id header))
4253          (id-dep (and id (intern id dependencies)))
4254          parent-id ref ref-dep ref-header replaced)
4255     ;; Enter this `header' in the `dependencies' table.
4256     (cond
4257      ((not id-dep)
4258       (setq header nil))
4259      ;; The first two cases do the normal part: enter a new `header'
4260      ;; in the `dependencies' table.
4261      ((not (boundp id-dep))
4262       (set id-dep (list header)))
4263      ((null (car (symbol-value id-dep)))
4264       (setcar (symbol-value id-dep) header))
4265
4266      ;; From here the `header' was already present in the
4267      ;; `dependencies' table.
4268      (force-new
4269       ;; Overrides an existing entry;
4270       ;; just set the header part of the entry.
4271       (setcar (symbol-value id-dep) header)
4272       (setq replaced t))
4273
4274      ;; Renames the existing `header' to a unique Message-ID.
4275      ((not gnus-summary-ignore-duplicates)
4276       ;; An article with this Message-ID has already been seen.
4277       ;; We rename the Message-ID.
4278       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4279            (list header))
4280       (mail-header-set-id header id))
4281
4282      ;; The last case ignores an existing entry, except it adds any
4283      ;; additional Xrefs (in case the two articles came from different
4284      ;; servers.
4285      ;; Also sets `header' to `nil' meaning that the `dependencies'
4286      ;; table was *not* modified.
4287      (t
4288       (mail-header-set-xref
4289        (car (symbol-value id-dep))
4290        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4291                    "")
4292                (or (mail-header-xref header) "")))
4293       (setq header nil)))
4294
4295     (when (and header (not replaced))
4296       ;; First check that we are not creating a References loop.
4297       (setq parent-id (gnus-parent-id (mail-header-references header)))
4298       (setq ref parent-id)
4299       (while (and ref
4300                   (setq ref-dep (intern-soft ref dependencies))
4301                   (boundp ref-dep)
4302                   (setq ref-header (car (symbol-value ref-dep))))
4303         (if (string= id ref)
4304             ;; Yuk!  This is a reference loop.  Make the article be a
4305             ;; root article.
4306             (progn
4307               (mail-header-set-references (car (symbol-value id-dep)) "none")
4308               (setq ref nil)
4309               (setq parent-id nil))
4310           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4311       (setq ref-dep (intern (or parent-id "none") dependencies))
4312       (if (boundp ref-dep)
4313           (setcdr (symbol-value ref-dep)
4314                   (nconc (cdr (symbol-value ref-dep))
4315                          (list (symbol-value id-dep))))
4316         (set ref-dep (list nil (symbol-value id-dep)))))
4317     header))
4318
4319 (defun gnus-extract-message-id-from-in-reply-to (string)
4320   (if (string-match "<[^>]+>" string)
4321       (substring string (match-beginning 0) (match-end 0))
4322     nil))
4323
4324 (defun gnus-build-sparse-threads ()
4325   (let ((headers gnus-newsgroup-headers)
4326         (mail-parse-charset gnus-newsgroup-charset)
4327         (gnus-summary-ignore-duplicates t)
4328         header references generation relations
4329         subject child end new-child date)
4330     ;; First we create an alist of generations/relations, where
4331     ;; generations is how much we trust the relation, and the relation
4332     ;; is parent/child.
4333     (gnus-message 7 "Making sparse threads...")
4334     (save-excursion
4335       (nnheader-set-temp-buffer " *gnus sparse threads*")
4336       (while (setq header (pop headers))
4337         (when (and (setq references (mail-header-references header))
4338                    (not (string= references "")))
4339           (insert references)
4340           (setq child (mail-header-id header)
4341                 subject (mail-header-subject header)
4342                 date (mail-header-date header)
4343                 generation 0)
4344           (while (search-backward ">" nil t)
4345             (setq end (1+ (point)))
4346             (when (search-backward "<" nil t)
4347               (setq new-child (buffer-substring (point) end))
4348               (push (list (incf generation)
4349                           child (setq child new-child)
4350                           subject date)
4351                     relations)))
4352           (when child
4353             (push (list (1+ generation) child nil subject) relations))
4354           (erase-buffer)))
4355       (kill-buffer (current-buffer)))
4356     ;; Sort over trustworthiness.
4357     (dolist (relation (sort relations 'car-less-than-car))
4358       (when (gnus-dependencies-add-header
4359              (make-full-mail-header
4360               gnus-reffed-article-number
4361               (nth 3 relation) "" (or (nth 4 relation) "")
4362               (nth 1 relation)
4363               (or (nth 2 relation) "") 0 0 "")
4364              gnus-newsgroup-dependencies nil)
4365         (push gnus-reffed-article-number gnus-newsgroup-limit)
4366         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4367         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4368               gnus-newsgroup-reads)
4369         (decf gnus-reffed-article-number)))
4370     (gnus-message 7 "Making sparse threads...done")))
4371
4372 (defun gnus-build-old-threads ()
4373   ;; Look at all the articles that refer back to old articles, and
4374   ;; fetch the headers for the articles that aren't there.  This will
4375   ;; build complete threads - if the roots haven't been expired by the
4376   ;; server, that is.
4377   (let ((mail-parse-charset gnus-newsgroup-charset)
4378         id heads)
4379     (mapatoms
4380      (lambda (refs)
4381        (when (not (car (symbol-value refs)))
4382          (setq heads (cdr (symbol-value refs)))
4383          (while heads
4384            (if (memq (mail-header-number (caar heads))
4385                      gnus-newsgroup-dormant)
4386                (setq heads (cdr heads))
4387              (setq id (symbol-name refs))
4388              (while (and (setq id (gnus-build-get-header id))
4389                          (not (car (gnus-id-to-thread id)))))
4390              (setq heads nil)))))
4391      gnus-newsgroup-dependencies)))
4392
4393 (defsubst gnus-remove-odd-characters (string)
4394   "Translate STRING into something that doesn't contain weird characters."
4395   (mm-subst-char-in-string
4396    ?\r ?\-
4397    (mm-subst-char-in-string ?\n ?\- string t) t))
4398
4399 ;; This function has to be called with point after the article number
4400 ;; on the beginning of the line.
4401 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4402   (let ((eol (point-at-eol))
4403         (buffer (current-buffer))
4404         header references in-reply-to)
4405
4406     ;; overview: [num subject from date id refs chars lines misc]
4407     (unwind-protect
4408         (let (x)
4409           (narrow-to-region (point) eol)
4410           (unless (eobp)
4411             (forward-char))
4412
4413           (setq header
4414                 (make-full-mail-header
4415                  number                 ; number
4416                  (condition-case ()     ; subject
4417                      (gnus-remove-odd-characters
4418                       (funcall gnus-decode-encoded-word-function
4419                                (setq x (nnheader-nov-field))))
4420                    (error x))
4421                  (condition-case ()     ; from
4422                      (gnus-remove-odd-characters
4423                       (funcall gnus-decode-encoded-address-function
4424                                (setq x (nnheader-nov-field))))
4425                    (error x))
4426                  (nnheader-nov-field)   ; date
4427                  (nnheader-nov-read-message-id number)  ; id
4428                  (setq references (nnheader-nov-field)) ; refs
4429                  (nnheader-nov-read-integer) ; chars
4430                  (nnheader-nov-read-integer) ; lines
4431                  (unless (eobp)
4432                    (if (looking-at "Xref: ")
4433                        (goto-char (match-end 0)))
4434                    (nnheader-nov-field)) ; Xref
4435                  (nnheader-nov-parse-extra)))) ; extra
4436
4437       (widen))
4438
4439     (when (and (string= references "")
4440                (setq in-reply-to (mail-header-extra header))
4441                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4442       (mail-header-set-references
4443        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4444
4445     (when gnus-alter-header-function
4446       (funcall gnus-alter-header-function header))
4447     (gnus-dependencies-add-header header dependencies force-new)))
4448
4449 (defun gnus-build-get-header (id)
4450   "Look through the buffer of NOV lines and find the header to ID.
4451 Enter this line into the dependencies hash table, and return
4452 the id of the parent article (if any)."
4453   (let ((deps gnus-newsgroup-dependencies)
4454         found header)
4455     (prog1
4456         (with-current-buffer nntp-server-buffer
4457           (let ((case-fold-search nil))
4458             (goto-char (point-min))
4459             (while (and (not found)
4460                         (search-forward id nil t))
4461               (beginning-of-line)
4462               (setq found (looking-at
4463                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4464                                    (regexp-quote id))))
4465               (or found (beginning-of-line 2)))
4466             (when found
4467               (beginning-of-line)
4468               (and
4469                (setq header (gnus-nov-parse-line
4470                              (read (current-buffer)) deps))
4471                (gnus-parent-id (mail-header-references header))))))
4472       (when header
4473         (let ((number (mail-header-number header)))
4474           (push number gnus-newsgroup-limit)
4475           (push header gnus-newsgroup-headers)
4476           (if (memq number gnus-newsgroup-unselected)
4477               (progn
4478                 (setq gnus-newsgroup-unreads
4479                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4480                                                number))
4481                 (setq gnus-newsgroup-unselected
4482                       (delq number gnus-newsgroup-unselected)))
4483             (push number gnus-newsgroup-ancient)))))))
4484
4485 (defun gnus-build-all-threads ()
4486   "Read all the headers."
4487   (let ((gnus-summary-ignore-duplicates t)
4488         (mail-parse-charset gnus-newsgroup-charset)
4489         (dependencies gnus-newsgroup-dependencies)
4490         header article)
4491     (with-current-buffer nntp-server-buffer
4492       (let ((case-fold-search nil))
4493         (goto-char (point-min))
4494         (while (not (eobp))
4495           (ignore-errors
4496             (setq article (read (current-buffer))
4497                   header (gnus-nov-parse-line article dependencies)))
4498           (when header
4499             (with-current-buffer gnus-summary-buffer
4500               (push header gnus-newsgroup-headers)
4501               (if (memq (setq article (mail-header-number header))
4502                         gnus-newsgroup-unselected)
4503                   (progn
4504                     (setq gnus-newsgroup-unreads
4505                           (gnus-add-to-sorted-list
4506                            gnus-newsgroup-unreads article))
4507                     (setq gnus-newsgroup-unselected
4508                           (delq article gnus-newsgroup-unselected)))
4509                 (push article gnus-newsgroup-ancient)))
4510             (forward-line 1)))))))
4511
4512 (defun gnus-summary-update-article-line (article header)
4513   "Update the line for ARTICLE using HEADER."
4514   (let* ((id (mail-header-id header))
4515          (thread (gnus-id-to-thread id)))
4516     (unless thread
4517       (error "Article in no thread"))
4518     ;; Update the thread.
4519     (setcar thread header)
4520     (gnus-summary-goto-subject article)
4521     (let* ((datal (gnus-data-find-list article))
4522            (data (car datal))
4523            (inhibit-read-only t)
4524            (level (gnus-summary-thread-level)))
4525       (gnus-delete-line)
4526       (let ((inserted (- (point)
4527                          (progn
4528                            (gnus-summary-insert-line
4529                             header level nil
4530                             (memq article gnus-newsgroup-undownloaded)
4531                             (gnus-article-mark article)
4532                             (memq article gnus-newsgroup-replied)
4533                             (memq article gnus-newsgroup-expirable)
4534                             ;; Only insert the Subject string when it's different
4535                             ;; from the previous Subject string.
4536                             (if (and
4537                                  gnus-show-threads
4538                                  (gnus-subject-equal
4539                                   (condition-case ()
4540                                       (mail-header-subject
4541                                        (gnus-data-header
4542                                         (cadr
4543                                          (gnus-data-find-list
4544                                           article
4545                                           (gnus-data-list t)))))
4546                                     ;; Error on the side of excessive subjects.
4547                                     (error ""))
4548                                   (mail-header-subject header)))
4549                                 ""
4550                               (mail-header-subject header))
4551                             nil (cdr (assq article gnus-newsgroup-scored))
4552                             (memq article gnus-newsgroup-processable))
4553                            (point)))))
4554         (when (cdr datal)
4555           (gnus-data-update-list
4556            (cdr datal)
4557            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4558
4559 (defun gnus-summary-update-article (article &optional iheader)
4560   "Update ARTICLE in the summary buffer."
4561   (set-buffer gnus-summary-buffer)
4562   (let* ((header (gnus-summary-article-header article))
4563          (id (mail-header-id header))
4564          (data (gnus-data-find article))
4565          (thread (gnus-id-to-thread id))
4566          (references (mail-header-references header))
4567          (parent
4568           (gnus-id-to-thread
4569            (or (gnus-parent-id
4570                 (when (and references
4571                            (not (equal "" references)))
4572                   references))
4573                "none")))
4574          (inhibit-read-only t)
4575          (old (car thread)))
4576     (when thread
4577       (unless iheader
4578         (setcar thread nil)
4579         (when parent
4580           (delq thread parent)))
4581       (if (gnus-summary-insert-subject id header)
4582           ;; Set the (possibly) new article number in the data structure.
4583           (gnus-data-set-number data (gnus-id-to-article id))
4584         (setcar thread old)
4585         nil))))
4586
4587 (defun gnus-rebuild-thread (id &optional line)
4588   "Rebuild the thread containing ID.
4589 If LINE, insert the rebuilt thread starting on line LINE."
4590   (let ((inhibit-read-only t)
4591         old-pos current thread data)
4592     (if (not gnus-show-threads)
4593         (setq thread (list (car (gnus-id-to-thread id))))
4594       ;; Get the thread this article is part of.
4595       (setq thread (gnus-remove-thread id)))
4596     (setq old-pos (point-at-bol))
4597     (setq current (save-excursion
4598                     (and (re-search-backward "[\r\n]" nil t)
4599                          (gnus-summary-article-number))))
4600     ;; If this is a gathered thread, we have to go some re-gathering.
4601     (when (stringp (car thread))
4602       (let ((subject (car thread))
4603             roots thr)
4604         (setq thread (cdr thread))
4605         (while thread
4606           (unless (memq (setq thr (gnus-id-to-thread
4607                                    (gnus-root-id
4608                                     (mail-header-id (caar thread)))))
4609                         roots)
4610             (push thr roots))
4611           (setq thread (cdr thread)))
4612         ;; We now have all (unique) roots.
4613         (if (= (length roots) 1)
4614             ;; All the loose roots are now one solid root.
4615             (setq thread (car roots))
4616           (setq thread (cons subject (gnus-sort-threads roots))))))
4617     (let (threads)
4618       ;; We then insert this thread into the summary buffer.
4619       (when line
4620         (goto-char (point-min))
4621         (forward-line (1- line)))
4622       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4623         (if gnus-show-threads
4624             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4625           (gnus-summary-prepare-unthreaded thread))
4626         (setq data (nreverse gnus-newsgroup-data))
4627         (setq threads gnus-newsgroup-threads))
4628       ;; We splice the new data into the data structure.
4629       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4630       ;;!!! then we want to insert at the beginning of the buffer.
4631       ;;!!! That happens to be true with Gnus now, but that may
4632       ;;!!! change in the future.  Perhaps.
4633       (gnus-data-enter-list
4634        (if line nil current) data (- (point) old-pos))
4635       (setq gnus-newsgroup-threads
4636             (nconc threads gnus-newsgroup-threads))
4637       (gnus-data-compute-positions))))
4638
4639 (defun gnus-number-to-header (number)
4640   "Return the header for article NUMBER."
4641   (let ((headers gnus-newsgroup-headers))
4642     (while (and headers
4643                 (not (= number (mail-header-number (car headers)))))
4644       (pop headers))
4645     (when headers
4646       (car headers))))
4647
4648 (defun gnus-parent-headers (in-headers &optional generation)
4649   "Return the headers of the GENERATIONeth parent of HEADERS."
4650   (unless generation
4651     (setq generation 1))
4652   (let ((parent t)
4653         (headers in-headers)
4654         references)
4655     (while (and parent
4656                 (not (zerop generation))
4657                 (setq references (mail-header-references headers)))
4658       (setq headers (if (and references
4659                              (setq parent (gnus-parent-id references)))
4660                         (car (gnus-id-to-thread parent))
4661                       nil))
4662       (decf generation))
4663     (and (not (eq headers in-headers))
4664          headers)))
4665
4666 (defun gnus-id-to-thread (id)
4667   "Return the (sub-)thread where ID appears."
4668   (gnus-gethash id gnus-newsgroup-dependencies))
4669
4670 (defun gnus-id-to-article (id)
4671   "Return the article number of ID."
4672   (let ((thread (gnus-id-to-thread id)))
4673     (when (and thread
4674                (car thread))
4675       (mail-header-number (car thread)))))
4676
4677 (defun gnus-id-to-header (id)
4678   "Return the article headers of ID."
4679   (car (gnus-id-to-thread id)))
4680
4681 (defun gnus-article-displayed-root-p (article)
4682   "Say whether ARTICLE is a root(ish) article."
4683   (let ((level (gnus-summary-thread-level article))
4684         (refs (mail-header-references  (gnus-summary-article-header article)))
4685         particle)
4686     (cond
4687      ((null level) nil)
4688      ((zerop level) t)
4689      ((null refs) t)
4690      ((null (gnus-parent-id refs)) t)
4691      ((and (= 1 level)
4692            (null (setq particle (gnus-id-to-article
4693                                  (gnus-parent-id refs))))
4694            (null (gnus-summary-thread-level particle)))))))
4695
4696 (defun gnus-root-id (id)
4697   "Return the id of the root of the thread where ID appears."
4698   (let (last-id prev)
4699     (while (and id (setq prev (car (gnus-id-to-thread id))))
4700       (setq last-id id
4701             id (gnus-parent-id (mail-header-references prev))))
4702     last-id))
4703
4704 (defun gnus-articles-in-thread (thread)
4705   "Return the list of articles in THREAD."
4706   (cons (mail-header-number (car thread))
4707         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4708
4709 (defun gnus-remove-thread (id &optional dont-remove)
4710   "Remove the thread that has ID in it."
4711   (let (headers thread last-id)
4712     ;; First go up in this thread until we find the root.
4713     (setq last-id (gnus-root-id id)
4714           headers (message-flatten-list (gnus-id-to-thread last-id)))
4715     ;; We have now found the real root of this thread.  It might have
4716     ;; been gathered into some loose thread, so we have to search
4717     ;; through the threads to find the thread we wanted.
4718     (let ((threads gnus-newsgroup-threads)
4719           sub)
4720       (while threads
4721         (setq sub (car threads))
4722         (if (stringp (car sub))
4723             ;; This is a gathered thread, so we look at the roots
4724             ;; below it to find whether this article is in this
4725             ;; gathered root.
4726             (progn
4727               (setq sub (cdr sub))
4728               (while sub
4729                 (when (member (caar sub) headers)
4730                   (setq thread (car threads)
4731                         threads nil
4732                         sub nil))
4733                 (setq sub (cdr sub))))
4734           ;; It's an ordinary thread, so we check it.
4735           (when (eq (car sub) (car headers))
4736             (setq thread sub
4737                   threads nil)))
4738         (setq threads (cdr threads)))
4739       ;; If this article is in no thread, then it's a root.
4740       (if thread
4741           (unless dont-remove
4742             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4743         (setq thread (gnus-id-to-thread last-id)))
4744       (when thread
4745         (prog1
4746             thread                      ; We return this thread.
4747           (unless dont-remove
4748             (if (stringp (car thread))
4749                 (progn
4750                   ;; If we use dummy roots, then we have to remove the
4751                   ;; dummy root as well.
4752                   (when (eq gnus-summary-make-false-root 'dummy)
4753                     ;; We go to the dummy root by going to
4754                     ;; the first sub-"thread", and then one line up.
4755                     (gnus-summary-goto-article
4756                      (mail-header-number (caadr thread)))
4757                     (forward-line -1)
4758                     (gnus-delete-line)
4759                     (gnus-data-compute-positions))
4760                   (setq thread (cdr thread))
4761                   (while thread
4762                     (gnus-remove-thread-1 (car thread))
4763                     (setq thread (cdr thread))))
4764               (gnus-remove-thread-1 thread))))))))
4765
4766 (defun gnus-remove-thread-1 (thread)
4767   "Remove the thread THREAD recursively."
4768   (let ((number (mail-header-number (pop thread)))
4769         d)
4770     (setq thread (reverse thread))
4771     (while thread
4772       (gnus-remove-thread-1 (pop thread)))
4773     (when (setq d (gnus-data-find number))
4774       (goto-char (gnus-data-pos d))
4775       (gnus-summary-show-thread)
4776       (gnus-data-remove
4777        number
4778        (- (point-at-bol)
4779           (prog1
4780               (1+ (point-at-eol))
4781             (gnus-delete-line)))))))
4782
4783 (defun gnus-sort-threads-recursive (threads func)
4784   (sort (mapcar (lambda (thread)
4785                   (cons (car thread)
4786                         (and (cdr thread)
4787                              (gnus-sort-threads-recursive (cdr thread) func))))
4788                 threads) func))
4789
4790 (defun gnus-sort-threads-loop (threads func)
4791   (let* ((superthread (cons nil threads))
4792          (stack (list (cons superthread threads)))
4793          remaining-threads thread)
4794     (while stack
4795       (setq remaining-threads (cdr (car stack)))
4796       (if remaining-threads
4797           (progn (setq thread (car remaining-threads))
4798                  (setcdr (car stack) (cdr remaining-threads))
4799                  (if (cdr thread)
4800                      (push (cons thread (cdr thread)) stack)))
4801         (setq thread (caar stack))
4802         (setcdr thread (sort (cdr thread) func))
4803         (pop stack)))
4804     (cdr superthread)))
4805
4806 (defun gnus-sort-threads (threads)
4807   "Sort THREADS."
4808   (if (not gnus-thread-sort-functions)
4809       threads
4810     (gnus-message 8 "Sorting threads...")
4811     (prog1
4812         (condition-case nil
4813             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4814               (gnus-sort-threads-recursive
4815                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4816           ;; Even after binding max-lisp-eval-depth, the recursive
4817           ;; sorter might fail for very long threads.  In that case,
4818           ;; try using a (less well-tested) non-recursive sorter.
4819           (error (gnus-sort-threads-loop
4820                   threads (gnus-make-sort-function
4821                            gnus-thread-sort-functions))))
4822       (gnus-message 8 "Sorting threads...done"))))
4823
4824 (defun gnus-sort-articles (articles)
4825   "Sort ARTICLES."
4826   (when gnus-article-sort-functions
4827     (gnus-message 7 "Sorting articles...")
4828     (prog1
4829         (setq gnus-newsgroup-headers
4830               (sort articles (gnus-make-sort-function
4831                               gnus-article-sort-functions)))
4832       (gnus-message 7 "Sorting articles...done"))))
4833
4834 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4835 (defmacro gnus-thread-header (thread)
4836   "Return header of first article in THREAD.
4837 Note that THREAD must never, ever be anything else than a variable -
4838 using some other form will lead to serious barfage."
4839   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4840   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4841   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4842         (vector thread) 2))
4843
4844 (defsubst gnus-article-sort-by-number (h1 h2)
4845   "Sort articles by article number."
4846   (< (mail-header-number h1)
4847      (mail-header-number h2)))
4848
4849 (defun gnus-thread-sort-by-number (h1 h2)
4850   "Sort threads by root article number."
4851   (gnus-article-sort-by-number
4852    (gnus-thread-header h1) (gnus-thread-header h2)))
4853
4854 (defsubst gnus-article-sort-by-random (h1 h2)
4855   "Sort articles randomly."
4856   (zerop (random 2)))
4857
4858 (defun gnus-thread-sort-by-random (h1 h2)
4859   "Sort threads randomly."
4860   (gnus-article-sort-by-random
4861    (gnus-thread-header h1) (gnus-thread-header h2)))
4862
4863 (defsubst gnus-article-sort-by-lines (h1 h2)
4864   "Sort articles by article Lines header."
4865   (< (mail-header-lines h1)
4866      (mail-header-lines h2)))
4867
4868 (defun gnus-thread-sort-by-lines (h1 h2)
4869   "Sort threads by root article Lines header."
4870   (gnus-article-sort-by-lines
4871    (gnus-thread-header h1) (gnus-thread-header h2)))
4872
4873 (defsubst gnus-article-sort-by-chars (h1 h2)
4874   "Sort articles by octet length."
4875   (< (mail-header-chars h1)
4876      (mail-header-chars h2)))
4877
4878 (defun gnus-thread-sort-by-chars (h1 h2)
4879   "Sort threads by root article octet length."
4880   (gnus-article-sort-by-chars
4881    (gnus-thread-header h1) (gnus-thread-header h2)))
4882
4883 (defsubst gnus-article-sort-by-author (h1 h2)
4884   "Sort articles by root author."
4885   (gnus-string<
4886    (let ((extract (funcall
4887                    gnus-extract-address-components
4888                    (mail-header-from h1))))
4889      (or (car extract) (cadr extract) ""))
4890    (let ((extract (funcall
4891                    gnus-extract-address-components
4892                    (mail-header-from h2))))
4893      (or (car extract) (cadr extract) ""))))
4894
4895 (defun gnus-thread-sort-by-author (h1 h2)
4896   "Sort threads by root author."
4897   (gnus-article-sort-by-author
4898    (gnus-thread-header h1)  (gnus-thread-header h2)))
4899
4900 (defsubst gnus-article-sort-by-recipient (h1 h2)
4901   "Sort articles by recipient."
4902   (gnus-string<
4903    (let ((extract (funcall
4904                    gnus-extract-address-components
4905                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4906      (or (car extract) (cadr extract)))
4907    (let ((extract (funcall
4908                    gnus-extract-address-components
4909                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4910      (or (car extract) (cadr extract)))))
4911
4912 (defun gnus-thread-sort-by-recipient (h1 h2)
4913   "Sort threads by root recipient."
4914   (gnus-article-sort-by-recipient
4915    (gnus-thread-header h1) (gnus-thread-header h2)))
4916
4917 (defsubst gnus-article-sort-by-subject (h1 h2)
4918   "Sort articles by root subject."
4919   (gnus-string<
4920    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4921    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4922
4923 (defun gnus-thread-sort-by-subject (h1 h2)
4924   "Sort threads by root subject."
4925   (gnus-article-sort-by-subject
4926    (gnus-thread-header h1) (gnus-thread-header h2)))
4927
4928 (defsubst gnus-article-sort-by-date (h1 h2)
4929   "Sort articles by root article date."
4930   (time-less-p
4931    (gnus-date-get-time (mail-header-date h1))
4932    (gnus-date-get-time (mail-header-date h2))))
4933
4934 (defun gnus-thread-sort-by-date (h1 h2)
4935   "Sort threads by root article date."
4936   (gnus-article-sort-by-date
4937    (gnus-thread-header h1) (gnus-thread-header h2)))
4938
4939 (defsubst gnus-article-sort-by-score (h1 h2)
4940   "Sort articles by root article score.
4941 Unscored articles will be counted as having a score of zero."
4942   (> (or (cdr (assq (mail-header-number h1)
4943                     gnus-newsgroup-scored))
4944          gnus-summary-default-score 0)
4945      (or (cdr (assq (mail-header-number h2)
4946                     gnus-newsgroup-scored))
4947          gnus-summary-default-score 0)))
4948
4949 (defun gnus-thread-sort-by-score (h1 h2)
4950   "Sort threads by root article score."
4951   (gnus-article-sort-by-score
4952    (gnus-thread-header h1) (gnus-thread-header h2)))
4953
4954 (defun gnus-thread-sort-by-total-score (h1 h2)
4955   "Sort threads by the sum of all scores in the thread.
4956 Unscored articles will be counted as having a score of zero."
4957   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4958
4959 (defun gnus-thread-total-score (thread)
4960   ;; This function find the total score of THREAD.
4961   (cond
4962    ((null thread)
4963     0)
4964    ((consp thread)
4965     (if (stringp (car thread))
4966         (apply gnus-thread-score-function 0
4967                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4968       (gnus-thread-total-score-1 thread)))
4969    (t
4970     (gnus-thread-total-score-1 (list thread)))))
4971
4972 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4973   "Sort threads such that the thread with the most recently arrived article comes first."
4974   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4975
4976 (defun gnus-thread-highest-number (thread)
4977   "Return the highest article number in THREAD."
4978   (apply 'max (mapcar (lambda (header)
4979                         (mail-header-number header))
4980                       (message-flatten-list thread))))
4981
4982 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4983   "Sort threads such that the thread with the most recently dated article comes first."
4984   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4985
4986 (defun gnus-thread-latest-date (thread)
4987   "Return the highest article date in THREAD."
4988   (let ((previous-time 0))
4989     (apply 'max
4990            (mapcar
4991             (lambda (header)
4992               (setq previous-time
4993                     (condition-case ()
4994                         (gnus-float-time (mail-header-parse-date
4995                                           (mail-header-date header)))
4996                       (error previous-time))))
4997             (sort
4998              (message-flatten-list thread)
4999              (lambda (h1 h2)
5000                (< (mail-header-number h1)
5001                   (mail-header-number h2))))))))
5002
5003 (defun gnus-thread-total-score-1 (root)
5004   ;; This function find the total score of the thread below ROOT.
5005   (setq root (car root))
5006   (apply gnus-thread-score-function
5007          (or (append
5008               (mapcar 'gnus-thread-total-score
5009                       (cdr (gnus-id-to-thread (mail-header-id root))))
5010               (when (> (mail-header-number root) 0)
5011                 (list (or (cdr (assq (mail-header-number root)
5012                                      gnus-newsgroup-scored))
5013                           gnus-summary-default-score 0))))
5014              (list gnus-summary-default-score)
5015              '(0))))
5016
5017 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5018 (defvar gnus-tmp-prev-subject nil)
5019 (defvar gnus-tmp-false-parent nil)
5020 (defvar gnus-tmp-root-expunged nil)
5021 (defvar gnus-tmp-dummy-line nil)
5022
5023 (defun gnus-extra-header (type &optional header)
5024   "Return the extra header of TYPE."
5025   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5026       ""))
5027
5028 (defvar gnus-tmp-thread-tree-header-string "")
5029
5030 (defcustom gnus-sum-thread-tree-root "> "
5031   "With %B spec, used for the 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-false-root "> "
5038   "With %B spec, used for a false root of a thread.
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-single-indent ""
5045   "With %B spec, used for a thread with just one message.
5046 If nil, use subject instead."
5047   :version "22.1"
5048   :type '(radio (const :format "%v  " nil) string)
5049   :group 'gnus-thread)
5050
5051 (defcustom gnus-sum-thread-tree-vertical "| "
5052   "With %B spec, used for drawing a vertical line."
5053   :version "22.1"
5054   :type 'string
5055   :group 'gnus-thread)
5056
5057 (defcustom gnus-sum-thread-tree-indent "  "
5058   "With %B spec, used for indenting."
5059   :version "22.1"
5060   :type 'string
5061   :group 'gnus-thread)
5062
5063 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5064   "With %B spec, used for a leaf with brothers."
5065   :version "22.1"
5066   :type 'string
5067   :group 'gnus-thread)
5068
5069 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5070   "With %B spec, used for a leaf without brothers."
5071   :version "22.1"
5072   :type 'string
5073   :group 'gnus-thread)
5074
5075 (defcustom gnus-summary-display-while-building nil
5076   "If non-nil, show and update the summary buffer as it's being built.
5077 If the value is t, update the buffer after every line is inserted.  If
5078 the value is an integer (N), update the display every N lines."
5079   :version "22.1"
5080   :group 'gnus-thread
5081   :type '(choice (const :tag "off" nil)
5082                  number
5083                  (const :tag "frequently" t)))
5084
5085 (defun gnus-summary-prepare-threads (threads)
5086   "Prepare summary buffer from THREADS and indentation LEVEL.
5087 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5088 or a straight list of headers."
5089   (gnus-message 7 "Generating summary...")
5090
5091   (setq gnus-newsgroup-threads threads)
5092   (beginning-of-line)
5093
5094   (let ((gnus-tmp-level 0)
5095         (default-score (or gnus-summary-default-score 0))
5096         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5097         (building-line-count gnus-summary-display-while-building)
5098         (building-count (integerp gnus-summary-display-while-building))
5099         thread number subject stack state gnus-tmp-gathered beg-match
5100         new-roots gnus-tmp-new-adopts thread-end simp-subject
5101         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5102         gnus-tmp-replied gnus-tmp-subject-or-nil
5103         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5104         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5105         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5106         tree-stack)
5107
5108     (setq gnus-tmp-prev-subject nil
5109           gnus-tmp-thread-tree-header-string "")
5110
5111     (if (vectorp (car threads))
5112         ;; If this is a straight (sic) list of headers, then a
5113         ;; threaded summary display isn't required, so we just create
5114         ;; an unthreaded one.
5115         (gnus-summary-prepare-unthreaded threads)
5116
5117       ;; Do the threaded display.
5118
5119       (if gnus-summary-display-while-building
5120           (switch-to-buffer (buffer-name)))
5121       (while (or threads stack gnus-tmp-new-adopts new-roots)
5122
5123         (if (and (= gnus-tmp-level 0)
5124                  (or (not stack)
5125                      (= (caar stack) 0))
5126                  (not gnus-tmp-false-parent)
5127                  (or gnus-tmp-new-adopts new-roots))
5128             (if gnus-tmp-new-adopts
5129                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5130                       thread (list (car gnus-tmp-new-adopts))
5131                       gnus-tmp-header (caar thread)
5132                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5133               (when new-roots
5134                 (setq thread (list (car new-roots))
5135                       gnus-tmp-header (caar thread)
5136                       new-roots (cdr new-roots))))
5137
5138           (if threads
5139               ;; If there are some threads, we do them before the
5140               ;; threads on the stack.
5141               (setq thread threads
5142                     gnus-tmp-header (caar thread))
5143             ;; There were no current threads, so we pop something off
5144             ;; the stack.
5145             (setq state (car stack)
5146                   gnus-tmp-level (car state)
5147                   tree-stack (cadr state)
5148                   thread (caddr state)
5149                   stack (cdr stack)
5150                   gnus-tmp-header (caar thread))))
5151
5152         (setq gnus-tmp-false-parent nil)
5153         (setq gnus-tmp-root-expunged nil)
5154         (setq thread-end nil)
5155
5156         (if (stringp gnus-tmp-header)
5157             ;; The header is a dummy root.
5158             (cond
5159              ((eq gnus-summary-make-false-root 'adopt)
5160               ;; We let the first article adopt the rest.
5161               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5162                                                (cddar thread)))
5163               (setq gnus-tmp-gathered
5164                     (nconc (mapcar
5165                             (lambda (h) (mail-header-number (car h)))
5166                             (cddar thread))
5167                            gnus-tmp-gathered))
5168               (setq thread (cons (list (caar thread)
5169                                        (cadar thread))
5170                                  (cdr thread)))
5171               (setq gnus-tmp-level -1
5172                     gnus-tmp-false-parent t))
5173              ((eq gnus-summary-make-false-root 'empty)
5174               ;; We print adopted articles with empty subject fields.
5175               (setq gnus-tmp-gathered
5176                     (nconc (mapcar
5177                             (lambda (h) (mail-header-number (car h)))
5178                             (cddar thread))
5179                            gnus-tmp-gathered))
5180               (setq gnus-tmp-level -1))
5181              ((eq gnus-summary-make-false-root 'dummy)
5182               ;; We remember that we probably want to output a dummy
5183               ;; root.
5184               (setq gnus-tmp-dummy-line gnus-tmp-header)
5185               (setq gnus-tmp-prev-subject gnus-tmp-header))
5186              (t
5187               ;; We do not make a root for the gathered
5188               ;; sub-threads at all.
5189               (setq gnus-tmp-level -1)))
5190
5191           (setq number (mail-header-number gnus-tmp-header)
5192                 subject (mail-header-subject gnus-tmp-header)
5193                 simp-subject (gnus-simplify-subject-fully subject))
5194
5195           (cond
5196            ;; If the thread has changed subject, we might want to make
5197            ;; this subthread into a root.
5198            ((and (null gnus-thread-ignore-subject)
5199                  (not (zerop gnus-tmp-level))
5200                  gnus-tmp-prev-subject
5201                  (not (string= gnus-tmp-prev-subject simp-subject)))
5202             (setq new-roots (nconc new-roots (list (car thread)))
5203                   thread-end t
5204                   gnus-tmp-header nil))
5205            ;; If the article lies outside the current limit,
5206            ;; then we do not display it.
5207            ((not (memq number gnus-newsgroup-limit))
5208             (setq gnus-tmp-gathered
5209                   (nconc (mapcar
5210                           (lambda (h) (mail-header-number (car h)))
5211                           (cdar thread))
5212                          gnus-tmp-gathered))
5213             (setq gnus-tmp-new-adopts (if (cdar thread)
5214                                           (append gnus-tmp-new-adopts
5215                                                   (cdar thread))
5216                                         gnus-tmp-new-adopts)
5217                   thread-end t
5218                   gnus-tmp-header nil)
5219             (when (zerop gnus-tmp-level)
5220               (setq gnus-tmp-root-expunged t)))
5221            ;; Perhaps this article is to be marked as read?
5222            ((and gnus-summary-mark-below
5223                  (< (or (cdr (assq number gnus-newsgroup-scored))
5224                         default-score)
5225                     gnus-summary-mark-below)
5226                  ;; Don't touch sparse articles.
5227                  (not (gnus-summary-article-sparse-p number))
5228                  (not (gnus-summary-article-ancient-p number)))
5229             (setq gnus-newsgroup-unreads
5230                   (delq number gnus-newsgroup-unreads))
5231             (if gnus-newsgroup-auto-expire
5232                 (setq gnus-newsgroup-expirable
5233                       (gnus-add-to-sorted-list
5234                        gnus-newsgroup-expirable number))
5235               (push (cons number gnus-low-score-mark)
5236                     gnus-newsgroup-reads))))
5237
5238           (when gnus-tmp-header
5239             ;; We may have an old dummy line to output before this
5240             ;; article.
5241             (when (and gnus-tmp-dummy-line
5242                        (gnus-subject-equal
5243                         gnus-tmp-dummy-line
5244                         (mail-header-subject gnus-tmp-header)))
5245               (gnus-summary-insert-dummy-line
5246                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5247               (setq gnus-tmp-dummy-line nil))
5248
5249             ;; Compute the mark.
5250             (setq gnus-tmp-unread (gnus-article-mark number))
5251
5252             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5253                                   gnus-tmp-header gnus-tmp-level)
5254                   gnus-newsgroup-data)
5255
5256             ;; Actually insert the line.
5257             (setq
5258              gnus-tmp-subject-or-nil
5259              (cond
5260               ((and gnus-thread-ignore-subject
5261                     gnus-tmp-prev-subject
5262                     (not (string= gnus-tmp-prev-subject simp-subject)))
5263                subject)
5264               ((zerop gnus-tmp-level)
5265                (if (and (eq gnus-summary-make-false-root 'empty)
5266                         (memq number gnus-tmp-gathered)
5267                         gnus-tmp-prev-subject
5268                         (string= gnus-tmp-prev-subject simp-subject))
5269                    gnus-summary-same-subject
5270                  subject))
5271               (t gnus-summary-same-subject)))
5272             (if (and (eq gnus-summary-make-false-root 'adopt)
5273                      (= gnus-tmp-level 1)
5274                      (memq number gnus-tmp-gathered))
5275                 (setq gnus-tmp-opening-bracket ?\<
5276                       gnus-tmp-closing-bracket ?\>)
5277               (setq gnus-tmp-opening-bracket ?\[
5278                     gnus-tmp-closing-bracket ?\]))
5279             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5280                 (gnus-make-thread-indent-array
5281                  (max (* 2 (length gnus-thread-indent-array))
5282                       gnus-tmp-level)))
5283             (setq
5284              gnus-tmp-indentation
5285              (aref gnus-thread-indent-array gnus-tmp-level)
5286              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5287              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5288                                 gnus-summary-default-score 0)
5289              gnus-tmp-score-char
5290              (if (or (null gnus-summary-default-score)
5291                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5292                          gnus-summary-zcore-fuzz))
5293                  ?                      ;Whitespace
5294                (if (< gnus-tmp-score gnus-summary-default-score)
5295                    gnus-score-below-mark gnus-score-over-mark))
5296              gnus-tmp-replied
5297              (cond ((memq number gnus-newsgroup-processable)
5298                     gnus-process-mark)
5299                    ((memq number gnus-newsgroup-cached)
5300                     gnus-cached-mark)
5301                    ((memq number gnus-newsgroup-replied)
5302                     gnus-replied-mark)
5303                    ((memq number gnus-newsgroup-forwarded)
5304                     gnus-forwarded-mark)
5305                    ((memq number gnus-newsgroup-saved)
5306                     gnus-saved-mark)
5307                    ((memq number gnus-newsgroup-recent)
5308                     gnus-recent-mark)
5309                    ((memq number gnus-newsgroup-unseen)
5310                     gnus-unseen-mark)
5311                    (t gnus-no-mark))
5312              gnus-tmp-downloaded
5313              (cond ((memq number gnus-newsgroup-undownloaded)
5314                     gnus-undownloaded-mark)
5315                    (gnus-newsgroup-agentized
5316                     gnus-downloaded-mark)
5317                    (t
5318                     gnus-no-mark))
5319              gnus-tmp-from (mail-header-from gnus-tmp-header)
5320              gnus-tmp-name
5321              (cond
5322               ((string-match "<[^>]+> *$" gnus-tmp-from)
5323                (setq beg-match (match-beginning 0))
5324                (or (and (string-match "^\".+\"" gnus-tmp-from)
5325                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5326                    (substring gnus-tmp-from 0 beg-match)))
5327               ((string-match "(.+)" gnus-tmp-from)
5328                (substring gnus-tmp-from
5329                           (1+ (match-beginning 0)) (1- (match-end 0))))
5330               (t gnus-tmp-from))
5331
5332              ;; Do the %B string
5333              gnus-tmp-thread-tree-header-string
5334              (cond
5335               ((not gnus-show-threads) "")
5336               ((zerop gnus-tmp-level)
5337                (cond ((cdar thread)
5338                       (or gnus-sum-thread-tree-root subject))
5339                      (gnus-tmp-new-adopts
5340                       (or gnus-sum-thread-tree-false-root subject))
5341                      (t
5342                       (or gnus-sum-thread-tree-single-indent subject))))
5343               (t
5344                (concat (apply 'concat
5345                               (mapcar (lambda (item)
5346                                         (if (= item 1)
5347                                             gnus-sum-thread-tree-vertical
5348                                           gnus-sum-thread-tree-indent))
5349                                       (cdr (reverse tree-stack))))
5350                        (if (nth 1 thread)
5351                            gnus-sum-thread-tree-leaf-with-other
5352                          gnus-sum-thread-tree-single-leaf)))))
5353             (when (string= gnus-tmp-name "")
5354               (setq gnus-tmp-name gnus-tmp-from))
5355             (unless (numberp gnus-tmp-lines)
5356               (setq gnus-tmp-lines -1))
5357             (if (= gnus-tmp-lines -1)
5358                 (setq gnus-tmp-lines "?")
5359               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5360               (gnus-put-text-property
5361                (point)
5362                (progn (eval gnus-summary-line-format-spec) (point))
5363                'gnus-number number)
5364               (when gnus-visual-p
5365                 (forward-line -1)
5366                 (gnus-run-hooks 'gnus-summary-update-hook)
5367                 (forward-line 1))
5368
5369               (setq gnus-tmp-prev-subject simp-subject)))
5370
5371         (when (nth 1 thread)
5372           (push (list (max 0 gnus-tmp-level)
5373                       (copy-sequence tree-stack)
5374                       (nthcdr 1 thread))
5375                 stack))
5376         (push (if (nth 1 thread) 1 0) tree-stack)
5377         (incf gnus-tmp-level)
5378         (setq threads (if thread-end nil (cdar thread)))
5379         (if gnus-summary-display-while-building
5380             (if building-count
5381                 (progn
5382                   ;; use a set frequency
5383                   (setq building-line-count (1- building-line-count))
5384                   (when (= building-line-count 0)
5385                     (sit-for 0)
5386                     (setq building-line-count
5387                           gnus-summary-display-while-building)))
5388               ;; always
5389               (sit-for 0)))
5390         (unless threads
5391           (setq gnus-tmp-level 0)))))
5392   (gnus-message 7 "Generating summary...done"))
5393
5394 (defun gnus-summary-prepare-unthreaded (headers)
5395   "Generate an unthreaded summary buffer based on HEADERS."
5396   (let (header number mark)
5397
5398     (beginning-of-line)
5399
5400     (while headers
5401       ;; We may have to root out some bad articles...
5402       (when (memq (setq number (mail-header-number
5403                                 (setq header (pop headers))))
5404                   gnus-newsgroup-limit)
5405         ;; Mark article as read when it has a low score.
5406         (when (and gnus-summary-mark-below
5407                    (< (or (cdr (assq number gnus-newsgroup-scored))
5408                           gnus-summary-default-score 0)
5409                       gnus-summary-mark-below)
5410                    (not (gnus-summary-article-ancient-p number)))
5411           (setq gnus-newsgroup-unreads
5412                 (delq number gnus-newsgroup-unreads))
5413           (if gnus-newsgroup-auto-expire
5414               (push number gnus-newsgroup-expirable)
5415             (push (cons number gnus-low-score-mark)
5416                   gnus-newsgroup-reads)))
5417
5418         (setq mark (gnus-article-mark number))
5419         (push (gnus-data-make number mark (1+ (point)) header 0)
5420               gnus-newsgroup-data)
5421         (gnus-summary-insert-line
5422          header 0 number
5423          (memq number gnus-newsgroup-undownloaded)
5424          mark (memq number gnus-newsgroup-replied)
5425          (memq number gnus-newsgroup-expirable)
5426          (mail-header-subject header) nil
5427          (cdr (assq number gnus-newsgroup-scored))
5428          (memq number gnus-newsgroup-processable))))))
5429
5430 (defun gnus-summary-remove-list-identifiers ()
5431   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5432   (let ((regexp (if (consp gnus-list-identifiers)
5433                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5434                   gnus-list-identifiers))
5435         changed subject)
5436     (when regexp
5437       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5438       (dolist (header gnus-newsgroup-headers)
5439         (setq subject (mail-header-subject header)
5440               changed nil)
5441         (while (string-match regexp subject)
5442           (setq subject
5443                 (concat (substring subject 0 (match-beginning 1))
5444                         (substring subject (match-end 0)))
5445                 changed t))
5446         (when changed
5447           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5448             (setq subject
5449                   (concat (substring subject 0 (match-beginning 1))
5450                           (substring subject (match-end 1)))))
5451           (mail-header-set-subject header subject))))))
5452
5453 (defun gnus-fetch-headers (articles)
5454   "Fetch headers of ARTICLES."
5455   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5456     (gnus-message 5 "Fetching headers for %s..." name)
5457     (prog1
5458         (if (eq 'nov
5459                 (setq gnus-headers-retrieved-by
5460                       (gnus-retrieve-headers
5461                        articles gnus-newsgroup-name
5462                        ;; We might want to fetch old headers, but
5463                        ;; not if there is only 1 article.
5464                        (and (or (and
5465                                  (not (eq gnus-fetch-old-headers 'some))
5466                                  (not (numberp gnus-fetch-old-headers)))
5467                                 (> (length articles) 1))
5468                             gnus-fetch-old-headers))))
5469             (gnus-get-newsgroup-headers-xover
5470              articles nil nil gnus-newsgroup-name t)
5471           (gnus-get-newsgroup-headers))
5472       (gnus-message 5 "Fetching headers for %s...done" name))))
5473
5474 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5475   "Select newsgroup GROUP.
5476 If READ-ALL is non-nil, all articles in the group are selected.
5477 If SELECT-ARTICLES, only select those articles from GROUP."
5478   (let* ((entry (gnus-group-entry group))
5479          ;;!!! Dirty hack; should be removed.
5480          (gnus-summary-ignore-duplicates
5481           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5482               t
5483             gnus-summary-ignore-duplicates))
5484          (info (nth 2 entry))
5485          charset articles fetched-articles cached)
5486
5487     (unless (gnus-check-server
5488              (set (make-local-variable 'gnus-current-select-method)
5489                   (gnus-find-method-for-group group)))
5490       (error "Couldn't open server"))
5491     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5492
5493     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5494         (gnus-activate-group group)     ; Or we can activate it...
5495         (progn                          ; Or we bug out.
5496           (when (equal major-mode 'gnus-summary-mode)
5497             (gnus-kill-buffer (current-buffer)))
5498           (error
5499            "Couldn't activate group %s: %s"
5500            (mm-decode-coding-string group charset)
5501            (mm-decode-coding-string (gnus-status-message group) charset))))
5502
5503     (unless (gnus-request-group group t)
5504         (when (equal major-mode 'gnus-summary-mode)
5505           (gnus-kill-buffer (current-buffer)))
5506         (error "Couldn't request group %s: %s"
5507                (mm-decode-coding-string group charset)
5508                (mm-decode-coding-string (gnus-status-message group) charset)))
5509
5510     (when gnus-agent
5511       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5512
5513       (setq gnus-summary-use-undownloaded-faces
5514             (gnus-agent-find-parameter
5515              group
5516              'agent-enable-undownloaded-faces)))
5517
5518     (setq gnus-newsgroup-name group
5519           gnus-newsgroup-unselected nil
5520           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5521
5522     (let ((display (gnus-group-find-parameter group 'display)))
5523       (setq gnus-newsgroup-display
5524             (cond
5525              ((not (zerop (or (car-safe read-all) 0)))
5526               ;; The user entered the group with C-u SPC/RET, let's show
5527               ;; all articles.
5528               'gnus-not-ignore)
5529              ((eq display 'all)
5530               'gnus-not-ignore)
5531              ((arrayp display)
5532               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5533              ((numberp display)
5534               ;; The following is probably the "correct" solution, but
5535               ;; it makes Gnus fetch all headers and then limit the
5536               ;; articles (which is slow), so instead we hack the
5537               ;; select-articles parameter instead. -- Simon Josefsson
5538               ;; <jas@kth.se>
5539               ;;
5540               ;; (gnus-byte-compile
5541               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5542               ;;                         display)))))
5543               (setq select-articles
5544                     (gnus-uncompress-range
5545                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5546                              (if (> tmp 0)
5547                                  tmp
5548                                1))
5549                            (cdr (gnus-active group)))))
5550               nil)
5551              (t
5552               nil))))
5553
5554     (gnus-summary-setup-default-charset)
5555
5556     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5557     (when (gnus-virtual-group-p group)
5558       (setq cached gnus-newsgroup-cached))
5559
5560     (setq gnus-newsgroup-unreads
5561           (gnus-sorted-ndifference
5562            (gnus-sorted-ndifference gnus-newsgroup-unreads
5563                                     gnus-newsgroup-marked)
5564            gnus-newsgroup-dormant))
5565
5566     (setq gnus-newsgroup-processable nil)
5567
5568     (gnus-update-read-articles group gnus-newsgroup-unreads)
5569
5570     ;; Adjust and set lists of article marks.
5571     (when info
5572       (gnus-adjust-marked-articles info))
5573     (if (setq articles select-articles)
5574         (setq gnus-newsgroup-unselected
5575               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5576       (setq articles (gnus-articles-to-read group read-all)))
5577
5578     (cond
5579      ((null articles)
5580       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5581       'quit)
5582      ((eq articles 0) nil)
5583      (t
5584       ;; Init the dependencies hash table.
5585       (setq gnus-newsgroup-dependencies
5586             (gnus-make-hashtable (length articles)))
5587       (gnus-set-global-variables)
5588       ;; Retrieve the headers and read them in.
5589
5590       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5591
5592       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5593       (when cached
5594         (setq gnus-newsgroup-cached cached))
5595
5596       ;; Suppress duplicates?
5597       (when gnus-suppress-duplicates
5598         (gnus-dup-suppress-articles))
5599
5600       ;; Set the initial limit.
5601       (setq gnus-newsgroup-limit (copy-sequence articles))
5602       ;; Remove canceled articles from the list of unread articles.
5603       (setq fetched-articles
5604             (mapcar (lambda (headers) (mail-header-number headers))
5605                     gnus-newsgroup-headers))
5606       (setq gnus-newsgroup-articles fetched-articles)
5607       (setq gnus-newsgroup-unreads
5608             (gnus-sorted-nintersection
5609              gnus-newsgroup-unreads fetched-articles))
5610       (gnus-compute-unseen-list)
5611
5612       ;; Removed marked articles that do not exist.
5613       (gnus-update-missing-marks
5614        (gnus-sorted-difference articles fetched-articles))
5615       ;; We might want to build some more threads first.
5616       (when (and gnus-fetch-old-headers
5617                  (eq gnus-headers-retrieved-by 'nov))
5618         (if (eq gnus-fetch-old-headers 'invisible)
5619             (gnus-build-all-threads)
5620           (gnus-build-old-threads)))
5621       ;; Let the Gnus agent mark articles as read.
5622       (when gnus-agent
5623         (gnus-agent-get-undownloaded-list))
5624       ;; Remove list identifiers from subject
5625       (when gnus-list-identifiers
5626         (gnus-summary-remove-list-identifiers))
5627       ;; Check whether auto-expire is to be done in this group.
5628       (setq gnus-newsgroup-auto-expire
5629             (gnus-group-auto-expirable-p group))
5630       ;; Set up the article buffer now, if necessary.
5631       (unless (and gnus-single-article-buffer
5632                    (equal gnus-article-buffer "*Article*"))
5633         (gnus-article-setup-buffer))
5634       ;; First and last article in this newsgroup.
5635       (when gnus-newsgroup-headers
5636         (setq gnus-newsgroup-begin
5637               (mail-header-number (car gnus-newsgroup-headers))
5638               gnus-newsgroup-end
5639               (mail-header-number
5640                (gnus-last-element gnus-newsgroup-headers))))
5641       ;; GROUP is successfully selected.
5642       (or gnus-newsgroup-headers t)))))
5643
5644 (defun gnus-compute-unseen-list ()
5645   ;; The `seen' marks are treated specially.
5646   (if (not gnus-newsgroup-seen)
5647       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5648     (setq gnus-newsgroup-unseen
5649           (gnus-inverse-list-range-intersection
5650            gnus-newsgroup-articles gnus-newsgroup-seen))))
5651
5652 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5653
5654 (defun gnus-summary-display-make-predicate (display)
5655   (require 'gnus-agent)
5656   (when (= (length display) 1)
5657     (setq display (car display)))
5658   (unless gnus-summary-display-cache
5659     (dolist (elem (append '((unread . unread)
5660                             (read . read)
5661                             (unseen . unseen))
5662                           gnus-article-mark-lists))
5663       (push (cons (cdr elem)
5664                   (gnus-byte-compile
5665                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5666             gnus-summary-display-cache)))
5667   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5668         (gnus-category-predicate-cache gnus-summary-display-cache))
5669     (gnus-get-predicate display)))
5670
5671 ;; Uses the dynamically bound `number' variable.
5672 (defvar number)
5673 (defun gnus-article-marked-p (type &optional article)
5674   (let ((article (or article number)))
5675     (cond
5676      ((eq type 'tick)
5677       (memq article gnus-newsgroup-marked))
5678      ((eq type 'spam)
5679       (memq article gnus-newsgroup-spam-marked))
5680      ((eq type 'unsend)
5681       (memq article gnus-newsgroup-unsendable))
5682      ((eq type 'undownload)
5683       (memq article gnus-newsgroup-undownloaded))
5684      ((eq type 'download)
5685       (memq article gnus-newsgroup-downloadable))
5686      ((eq type 'unread)
5687       (memq article gnus-newsgroup-unreads))
5688      ((eq type 'read)
5689       (memq article gnus-newsgroup-reads))
5690      ((eq type 'dormant)
5691       (memq article gnus-newsgroup-dormant) )
5692      ((eq type 'expire)
5693       (memq article gnus-newsgroup-expirable))
5694      ((eq type 'reply)
5695       (memq article gnus-newsgroup-replied))
5696      ((eq type 'killed)
5697       (memq article gnus-newsgroup-killed))
5698      ((eq type 'bookmark)
5699       (assq article gnus-newsgroup-bookmarks))
5700      ((eq type 'score)
5701       (assq article gnus-newsgroup-scored))
5702      ((eq type 'save)
5703       (memq article gnus-newsgroup-saved))
5704      ((eq type 'cache)
5705       (memq article gnus-newsgroup-cached))
5706      ((eq type 'forward)
5707       (memq article gnus-newsgroup-forwarded))
5708      ((eq type 'seen)
5709       (not (memq article gnus-newsgroup-unseen)))
5710      ((eq type 'recent)
5711       (memq article gnus-newsgroup-recent))
5712      (t t))))
5713
5714 (defun gnus-articles-to-read (group &optional read-all)
5715   "Find out what articles the user wants to read."
5716   (let* ((articles
5717           ;; Select all articles if `read-all' is non-nil, or if there
5718           ;; are no unread articles.
5719           (if (or read-all
5720                   (and (zerop (length gnus-newsgroup-marked))
5721                        (zerop (length gnus-newsgroup-unreads)))
5722                   ;; Fetch all if the predicate is non-nil.
5723                   gnus-newsgroup-display)
5724               ;; We want to select the headers for all the articles in
5725               ;; the group, so we select either all the active
5726               ;; articles in the group, or (if that's nil), the
5727               ;; articles in the cache.
5728               (or
5729                (if gnus-newsgroup-maximum-articles
5730                    (let ((active (gnus-active group)))
5731                      (gnus-uncompress-range
5732                       (cons (max (car active)
5733                                  (- (cdr active)
5734                                     gnus-newsgroup-maximum-articles
5735                                     -1))
5736                             (cdr active))))
5737                  (gnus-uncompress-range (gnus-active group)))
5738                (gnus-cache-articles-in-group group))
5739             ;; Select only the "normal" subset of articles.
5740             (gnus-sorted-nunion
5741              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5742              gnus-newsgroup-unreads)))
5743          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5744          (scored (length scored-list))
5745          (number (length articles))
5746          (marked (+ (length gnus-newsgroup-marked)
5747                     (length gnus-newsgroup-dormant)))
5748          (select
5749           (cond
5750            ((numberp read-all)
5751             read-all)
5752            ((numberp gnus-newsgroup-display)
5753             gnus-newsgroup-display)
5754            (t
5755             (condition-case ()
5756                 (cond
5757                  ((and (or (<= scored marked) (= scored number))
5758                        (numberp gnus-large-newsgroup)
5759                        (> number gnus-large-newsgroup))
5760                   (let* ((cursor-in-echo-area nil)
5761                          (initial (gnus-parameter-large-newsgroup-initial
5762                                    gnus-newsgroup-name))
5763                          (input
5764                           (read-string
5765                            (format
5766                             "How many articles from %s (%s %d): "
5767                             (gnus-group-decoded-name gnus-newsgroup-name)
5768                             (if initial "max" "default")
5769                             number)
5770                            (if initial
5771                                (cons (number-to-string initial)
5772                                      0)))))
5773                     (if (string-match "^[ \t]*$" input) number input)))
5774                  ((and (> scored marked) (< scored number)
5775                        (> (- scored number) 20))
5776                   (let ((input
5777                          (read-string
5778                           (format "%s %s (%d scored, %d total): "
5779                                   "How many articles from"
5780                                   (gnus-group-decoded-name group)
5781                                   scored number))))
5782                     (if (string-match "^[ \t]*$" input)
5783                         number input)))
5784                  (t number))
5785               (quit
5786                (message "Quit getting the articles to read")
5787                nil))))))
5788     (setq select (if (stringp select) (string-to-number select) select))
5789     (if (or (null select) (zerop select))
5790         select
5791       (if (and (not (zerop scored)) (<= (abs select) scored))
5792           (progn
5793             (setq articles (sort scored-list '<))
5794             (setq number (length articles)))
5795         (setq articles (copy-sequence articles)))
5796
5797       (when (< (abs select) number)
5798         (if (< select 0)
5799             ;; Select the N oldest articles.
5800             (setcdr (nthcdr (1- (abs select)) articles) nil)
5801           ;; Select the N most recent articles.
5802           (setq articles (nthcdr (- number select) articles))))
5803       (setq gnus-newsgroup-unselected
5804             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5805       (when gnus-alter-articles-to-read-function
5806         (setq articles
5807               (sort
5808                (funcall gnus-alter-articles-to-read-function
5809                         gnus-newsgroup-name articles)
5810                '<)))
5811       articles)))
5812
5813 (defun gnus-killed-articles (killed articles)
5814   (let (out)
5815     (while articles
5816       (when (inline (gnus-member-of-range (car articles) killed))
5817         (push (car articles) out))
5818       (setq articles (cdr articles)))
5819     out))
5820
5821 (defun gnus-uncompress-marks (marks)
5822   "Uncompress the mark ranges in MARKS."
5823   (let ((uncompressed '(score bookmark))
5824         out)
5825     (while marks
5826       (if (memq (caar marks) uncompressed)
5827           (push (car marks) out)
5828         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5829       (setq marks (cdr marks)))
5830     out))
5831
5832 (defun gnus-article-mark-to-type (mark)
5833   "Return the type of MARK."
5834   (or (cadr (assq mark gnus-article-special-mark-lists))
5835       'list))
5836
5837 (defun gnus-article-unpropagatable-p (mark)
5838   "Return whether MARK should be propagated to back end."
5839   (memq mark gnus-article-unpropagated-mark-lists))
5840
5841 (defun gnus-adjust-marked-articles (info)
5842   "Set all article lists and remove all marks that are no longer valid."
5843   (let* ((marked-lists (gnus-info-marks info))
5844          (active (gnus-active (gnus-info-group info)))
5845          (min (car active))
5846          (max (cdr active))
5847          (types gnus-article-mark-lists)
5848          marks var articles article mark mark-type
5849          bgn end)
5850
5851     (dolist (marks marked-lists)
5852       (setq mark (car marks)
5853             mark-type (gnus-article-mark-to-type mark)
5854             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5855
5856       ;; We set the variable according to the type of the marks list,
5857       ;; and then adjust the marks to a subset of the active articles.
5858       (cond
5859        ;; Adjust "simple" lists - compressed yet unsorted
5860        ((eq mark-type 'list)
5861         ;; Simultaneously uncompress and clip to active range
5862         ;; See gnus-uncompress-range for a description of possible marks
5863         (let (l lh)
5864           (if (not (cadr marks))
5865               (set var nil)
5866             (setq articles (if (numberp (cddr marks))
5867                                (list (cdr marks))
5868                              (cdr marks))
5869                   lh (cons nil nil)
5870                   l lh)
5871
5872             (while (setq article (pop articles))
5873               (cond ((consp article)
5874                      (setq bgn (max (car article) min)
5875                            end (min (cdr article) max))
5876                      (while (<= bgn end)
5877                        (setq l (setcdr l (cons bgn nil))
5878                              bgn (1+ bgn))))
5879                     ((and (<= min article)
5880                           (>= max article))
5881                      (setq l (setcdr l (cons article nil))))))
5882             (set var (cdr lh)))))
5883        ;; Adjust assocs.
5884        ((eq mark-type 'tuple)
5885         (set var (setq articles (cdr marks)))
5886         (when (not (listp (cdr (symbol-value var))))
5887           (set var (list (symbol-value var))))
5888         (when (not (listp (cdr articles)))
5889           (setq articles (list articles)))
5890         (while articles
5891           (when (or (not (consp (setq article (pop articles))))
5892                     (< (car article) min)
5893                     (> (car article) max))
5894             (set var (delq article (symbol-value var))))))
5895        ;; Adjust ranges (sloppily).
5896        ((eq mark-type 'range)
5897         (cond
5898          ((eq mark 'seen)
5899           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5900           ;; It should be (seen (NUM1 . NUM2)).
5901           (when (numberp (cddr marks))
5902             (setcdr marks (list (cdr marks))))
5903           (setq articles (cdr marks))
5904           (while (and articles
5905                       (or (and (consp (car articles))
5906                                (> min (cdar articles)))
5907                           (and (numberp (car articles))
5908                                (> min (car articles)))))
5909             (pop articles))
5910           (set var articles))))))))
5911
5912 (defun gnus-update-missing-marks (missing)
5913   "Go through the list of MISSING articles and remove them from the mark lists."
5914   (when missing
5915     (let (var m)
5916       ;; Go through all types.
5917       (dolist (elem gnus-article-mark-lists)
5918         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5919           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5920           (when (symbol-value var)
5921             ;; This list has articles.  So we delete all missing
5922             ;; articles from it.
5923             (setq m missing)
5924             (while m
5925               (set var (delq (pop m) (symbol-value var))))))))))
5926
5927 (defun gnus-update-marks ()
5928   "Enter the various lists of marked articles into the newsgroup info list."
5929   (let ((types gnus-article-mark-lists)
5930         (info (gnus-get-info gnus-newsgroup-name))
5931         type list newmarked symbol delta-marks)
5932     (when info
5933       ;; Add all marks lists to the list of marks lists.
5934       (while (setq type (pop types))
5935         (setq list (symbol-value
5936                     (setq symbol
5937                           (intern (format "gnus-newsgroup-%s" (car type))))))
5938
5939         (when list
5940           ;; Get rid of the entries of the articles that have the
5941           ;; default score.
5942           (when (and (eq (cdr type) 'score)
5943                      gnus-save-score
5944                      list)
5945             (let* ((arts list)
5946                    (prev (cons nil list))
5947                    (all prev))
5948               (while arts
5949                 (if (or (not (consp (car arts)))
5950                         (= (cdar arts) gnus-summary-default-score))
5951                     (setcdr prev (cdr arts))
5952                   (setq prev arts))
5953                 (setq arts (cdr arts)))
5954               (setq list (cdr all)))))
5955
5956         (when (eq (cdr type) 'seen)
5957           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5958
5959         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5960           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5961
5962         (when (and (gnus-check-backend-function
5963                     'request-set-mark gnus-newsgroup-name)
5964                    (not (gnus-article-unpropagatable-p (cdr type))))
5965           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5966                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5967                  (add (gnus-remove-from-range
5968                        (gnus-copy-sequence list) old)))
5969             (when add
5970               (push (list add 'add (list (cdr type))) delta-marks))
5971             (when del
5972               (push (list del 'del (list (cdr type))) delta-marks))))
5973
5974         (when list
5975           (push (cons (cdr type) list) newmarked)))
5976
5977       (when delta-marks
5978         (unless (gnus-check-group gnus-newsgroup-name)
5979           (error "Can't open server for %s" gnus-newsgroup-name))
5980         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5981
5982       ;; Enter these new marks into the info of the group.
5983       (if (nthcdr 3 info)
5984           (setcar (nthcdr 3 info) newmarked)
5985         ;; Add the marks lists to the end of the info.
5986         (when newmarked
5987           (setcdr (nthcdr 2 info) (list newmarked))))
5988
5989       ;; Cut off the end of the info if there's nothing else there.
5990       (let ((i 5))
5991         (while (and (> i 2)
5992                     (not (nth i info)))
5993           (when (nthcdr (decf i) info)
5994             (setcdr (nthcdr i info) nil)))))))
5995
5996 (defun gnus-set-mode-line (where)
5997   "Set the mode line of the article or summary buffers.
5998 If WHERE is `summary', the summary mode line format will be used."
5999   ;; Is this mode line one we keep updated?
6000   (when (and (memq where gnus-updated-mode-lines)
6001              (symbol-value
6002               (intern (format "gnus-%s-mode-line-format-spec" where))))
6003     (let (mode-string)
6004       ;; We evaluate this in the summary buffer since these
6005       ;; variables are buffer-local to that buffer.
6006       (with-current-buffer gnus-summary-buffer
6007         ;; We bind all these variables that are used in the `eval' form
6008         ;; below.
6009         (let* ((mformat (symbol-value
6010                          (intern
6011                           (format "gnus-%s-mode-line-format-spec" where))))
6012                (gnus-tmp-group-name (gnus-mode-string-quote
6013                                      (gnus-group-decoded-name
6014                                       gnus-newsgroup-name)))
6015                (gnus-tmp-article-number (or gnus-current-article 0))
6016                (gnus-tmp-unread gnus-newsgroup-unreads)
6017                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6018                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6019                (gnus-tmp-unread-and-unselected
6020                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6021                             (zerop gnus-tmp-unselected))
6022                        "")
6023                       ((zerop gnus-tmp-unselected)
6024                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6025                       (t (format "{%d(+%d) more}"
6026                                  gnus-tmp-unread-and-unticked
6027                                  gnus-tmp-unselected))))
6028                (gnus-tmp-subject
6029                 (if (and gnus-current-headers
6030                          (vectorp gnus-current-headers))
6031                     (gnus-mode-string-quote
6032                      (mail-header-subject gnus-current-headers))
6033                   ""))
6034                bufname-length max-len
6035                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6036           (setq mode-string (eval mformat))
6037           (setq bufname-length (if (string-match "%b" mode-string)
6038                                    (- (length
6039                                        (buffer-name
6040                                         (if (eq where 'summary)
6041                                             nil
6042                                           (get-buffer gnus-article-buffer))))
6043                                       2)
6044                                  0))
6045           (setq max-len (max 4 (if gnus-mode-non-string-length
6046                                    (- (window-width)
6047                                       gnus-mode-non-string-length
6048                                       bufname-length)
6049                                  (length mode-string))))
6050           ;; We might have to chop a bit of the string off...
6051           (when (> (length mode-string) max-len)
6052             (setq mode-string
6053                   (concat (truncate-string-to-width mode-string (- max-len 3))
6054                           "...")))
6055           ;; Pad the mode string a bit.
6056           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
6057       ;; Update the mode line.
6058       (setq mode-line-buffer-identification
6059             (gnus-mode-line-buffer-identification (list mode-string)))
6060       (set-buffer-modified-p t))))
6061
6062 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6063   "Go through the HEADERS list and add all Xrefs to a hash table.
6064 The resulting hash table is returned, or nil if no Xrefs were found."
6065   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6066          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6067          (xref-hashtb (gnus-make-hashtable))
6068          start group entry number xrefs header)
6069     (while headers
6070       (setq header (pop headers))
6071       (when (and (setq xrefs (mail-header-xref header))
6072                  (not (memq (setq number (mail-header-number header))
6073                             unreads)))
6074         (setq start 0)
6075         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6076           (setq start (match-end 0))
6077           (setq group (if prefix
6078                           (concat prefix (substring xrefs (match-beginning 1)
6079                                                     (match-end 1)))
6080                         (substring xrefs (match-beginning 1) (match-end 1))))
6081           (setq number
6082                 (string-to-number (substring xrefs (match-beginning 2)
6083                                           (match-end 2))))
6084           (if (setq entry (gnus-gethash group xref-hashtb))
6085               (setcdr entry (cons number (cdr entry)))
6086             (gnus-sethash group (cons number nil) xref-hashtb)))))
6087     (and start xref-hashtb)))
6088
6089 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6090   "Look through all the headers and mark the Xrefs as read."
6091   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6092         name info xref-hashtb idlist method nth4)
6093     (save-excursion
6094       (set-buffer gnus-group-buffer)
6095       (when (setq xref-hashtb
6096                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6097         (mapatoms
6098          (lambda (group)
6099            (unless (string= from-newsgroup (setq name (symbol-name group)))
6100              (setq idlist (symbol-value group))
6101              ;; Dead groups are not updated.
6102              (and (prog1
6103                       (setq info (gnus-get-info name))
6104                     (when (stringp (setq nth4 (gnus-info-method info)))
6105                       (setq nth4 (gnus-server-to-method nth4))))
6106                   ;; Only do the xrefs if the group has the same
6107                   ;; select method as the group we have just read.
6108                   (or (gnus-methods-equal-p
6109                        nth4 (gnus-find-method-for-group from-newsgroup))
6110                       virtual
6111                       (equal nth4 (setq method (gnus-find-method-for-group
6112                                                 from-newsgroup)))
6113                       (and (equal (car nth4) (car method))
6114                            (equal (nth 1 nth4) (nth 1 method))))
6115                   gnus-use-cross-reference
6116                   (or (not (eq gnus-use-cross-reference t))
6117                       virtual
6118                       ;; Only do cross-references on subscribed
6119                       ;; groups, if that is what is wanted.
6120                       (<= (gnus-info-level info) gnus-level-subscribed))
6121                   (gnus-group-make-articles-read name idlist))))
6122          xref-hashtb)))))
6123
6124 (defun gnus-compute-read-articles (group articles)
6125   (let* ((entry (gnus-group-entry group))
6126          (info (nth 2 entry))
6127          (active (gnus-active group))
6128          ninfo)
6129     (when entry
6130       ;; First peel off all invalid article numbers.
6131       (when active
6132         (let ((ids articles)
6133               id first)
6134           (while (setq id (pop ids))
6135             (when (and first (> id (cdr active)))
6136               ;; We'll end up in this situation in one particular
6137               ;; obscure situation.  If you re-scan a group and get
6138               ;; a new article that is cross-posted to a different
6139               ;; group that has not been re-scanned, you might get
6140               ;; crossposted article that has a higher number than
6141               ;; Gnus believes possible.  So we re-activate this
6142               ;; group as well.  This might mean doing the
6143               ;; crossposting thingy will *increase* the number
6144               ;; of articles in some groups.  Tsk, tsk.
6145               (setq active (or (gnus-activate-group group) active)))
6146             (when (or (> id (cdr active))
6147                       (< id (car active)))
6148               (setq articles (delq id articles))))))
6149       ;; If the read list is nil, we init it.
6150       (if (and active
6151                (null (gnus-info-read info))
6152                (> (car active) 1))
6153           (setq ninfo (cons 1 (1- (car active))))
6154         (setq ninfo (gnus-info-read info)))
6155       ;; Then we add the read articles to the range.
6156       (gnus-add-to-range
6157        ninfo (setq articles (sort articles '<))))))
6158
6159 (defun gnus-group-make-articles-read (group articles)
6160   "Update the info of GROUP to say that ARTICLES are read."
6161   (let* ((num 0)
6162          (entry (gnus-group-entry group))
6163          (info (nth 2 entry))
6164          (active (gnus-active group))
6165          range)
6166     (when entry
6167       (setq range (gnus-compute-read-articles group articles))
6168       (with-current-buffer gnus-group-buffer
6169         (gnus-undo-register
6170           `(progn
6171              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6172              (gnus-info-set-read ',info ',(gnus-info-read info))
6173              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6174              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6175              (gnus-group-update-group ,group t))))
6176       ;; Add the read articles to the range.
6177       (gnus-info-set-read info range)
6178       (gnus-request-set-mark group (list (list range 'add '(read))))
6179       ;; Then we have to re-compute how many unread
6180       ;; articles there are in this group.
6181       (when active
6182         (cond
6183          ((not range)
6184           (setq num (- (1+ (cdr active)) (car active))))
6185          ((not (listp (cdr range)))
6186           (setq num (- (cdr active) (- (1+ (cdr range))
6187                                        (car range)))))
6188          (t
6189           (while range
6190             (if (numberp (car range))
6191                 (setq num (1+ num))
6192               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6193             (setq range (cdr range)))
6194           (setq num (- (cdr active) num))))
6195         ;; Update the number of unread articles.
6196         (setcar entry num)
6197         ;; Update the group buffer.
6198         (unless (gnus-ephemeral-group-p group)
6199           (gnus-group-update-group group t))))))
6200
6201 (defvar gnus-newsgroup-none-id 0)
6202
6203 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6204   (let ((cur nntp-server-buffer)
6205         (dependencies
6206          (or dependencies
6207              (with-current-buffer gnus-summary-buffer
6208                gnus-newsgroup-dependencies)))
6209         headers id end ref number
6210         (mail-parse-charset gnus-newsgroup-charset)
6211         (mail-parse-ignored-charsets
6212          (save-current-buffer (condition-case nil
6213                                   (set-buffer gnus-summary-buffer)
6214                                 (error))
6215                               gnus-newsgroup-ignored-charsets)))
6216     (with-current-buffer nntp-server-buffer
6217       ;; Translate all TAB characters into SPACE characters.
6218       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6219       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6220       (ietf-drums-unfold-fws)
6221       (gnus-run-hooks 'gnus-parse-headers-hook)
6222       (let ((case-fold-search t)
6223             in-reply-to header p lines chars)
6224         (goto-char (point-min))
6225         ;; Search to the beginning of the next header.  Error messages
6226         ;; do not begin with 2 or 3.
6227         (while (re-search-forward "^[23][0-9]+ " nil t)
6228           (setq id nil
6229                 ref nil)
6230           ;; This implementation of this function, with nine
6231           ;; search-forwards instead of the one re-search-forward and
6232           ;; a case (which basically was the old function) is actually
6233           ;; about twice as fast, even though it looks messier.  You
6234           ;; can't have everything, I guess.  Speed and elegance
6235           ;; doesn't always go hand in hand.
6236           (setq
6237            header
6238            (vector
6239             ;; Number.
6240             (prog1
6241                 (setq number (read cur))
6242               (end-of-line)
6243               (setq p (point))
6244               (narrow-to-region (point)
6245                                 (or (and (search-forward "\n.\n" nil t)
6246                                          (- (point) 2))
6247                                     (point))))
6248             ;; Subject.
6249             (progn
6250               (goto-char p)
6251               (if (search-forward "\nsubject:" nil t)
6252                   (funcall gnus-decode-encoded-word-function
6253                            (nnheader-header-value))
6254                 "(none)"))
6255             ;; From.
6256             (progn
6257               (goto-char p)
6258               (if (search-forward "\nfrom:" nil t)
6259                   (funcall gnus-decode-encoded-address-function
6260                            (nnheader-header-value))
6261                 "(nobody)"))
6262             ;; Date.
6263             (progn
6264               (goto-char p)
6265               (if (search-forward "\ndate:" nil t)
6266                   (nnheader-header-value) ""))
6267             ;; Message-ID.
6268             (progn
6269               (goto-char p)
6270               (setq id (if (re-search-forward
6271                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6272                            ;; We do it this way to make sure the Message-ID
6273                            ;; is (somewhat) syntactically valid.
6274                            (buffer-substring (match-beginning 1)
6275                                              (match-end 1))
6276                          ;; If there was no message-id, we just fake one
6277                          ;; to make subsequent routines simpler.
6278                          (nnheader-generate-fake-message-id number))))
6279             ;; References.
6280             (progn
6281               (goto-char p)
6282               (if (search-forward "\nreferences:" nil t)
6283                   (progn
6284                     (setq end (point))
6285                     (prog1
6286                         (nnheader-header-value)
6287                       (setq ref
6288                             (buffer-substring
6289                              (progn
6290                                (end-of-line)
6291                                (search-backward ">" end t)
6292                                (1+ (point)))
6293                              (progn
6294                                (search-backward "<" end t)
6295                                (point))))))
6296                 ;; Get the references from the in-reply-to header if there
6297                 ;; were no references and the in-reply-to header looks
6298                 ;; promising.
6299                 (if (and (search-forward "\nin-reply-to:" nil t)
6300                          (setq in-reply-to (nnheader-header-value))
6301                          (string-match "<[^>]+>" in-reply-to))
6302                     (let (ref2)
6303                       (setq ref (substring in-reply-to (match-beginning 0)
6304                                            (match-end 0)))
6305                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6306                         (setq ref2 (substring in-reply-to (match-beginning 0)
6307                                               (match-end 0)))
6308                         (when (> (length ref2) (length ref))
6309                           (setq ref ref2)))
6310                       ref)
6311                   (setq ref nil))))
6312             ;; Chars.
6313             (progn
6314               (goto-char p)
6315               (if (search-forward "\nchars: " nil t)
6316                   (if (numberp (setq chars (ignore-errors (read cur))))
6317                       chars -1)
6318                 -1))
6319             ;; Lines.
6320             (progn
6321               (goto-char p)
6322               (if (search-forward "\nlines: " nil t)
6323                   (if (numberp (setq lines (ignore-errors (read cur))))
6324                       lines -1)
6325                 -1))
6326             ;; Xref.
6327             (progn
6328               (goto-char p)
6329               (and (search-forward "\nxref:" nil t)
6330                    (nnheader-header-value)))
6331             ;; Extra.
6332             (when gnus-extra-headers
6333               (let ((extra gnus-extra-headers)
6334                     out)
6335                 (while extra
6336                   (goto-char p)
6337                   (when (search-forward
6338                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6339                     (push (cons (car extra) (nnheader-header-value))
6340                           out))
6341                   (pop extra))
6342                 out))))
6343           (when (equal id ref)
6344             (setq ref nil))
6345
6346           (when gnus-alter-header-function
6347             (funcall gnus-alter-header-function header)
6348             (setq id (mail-header-id header)
6349                   ref (gnus-parent-id (mail-header-references header))))
6350
6351           (when (setq header
6352                       (gnus-dependencies-add-header
6353                        header dependencies force-new))
6354             (push header headers))
6355           (goto-char (point-max))
6356           (widen))
6357         (nreverse headers)))))
6358
6359 ;; Goes through the xover lines and returns a list of vectors
6360 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6361                                                   force-new dependencies
6362                                                   group also-fetch-heads)
6363   "Parse the news overview data in the server buffer.
6364 Return a list of headers that match SEQUENCE (see
6365 `nntp-retrieve-headers')."
6366   ;; Get the Xref when the users reads the articles since most/some
6367   ;; NNTP servers do not include Xrefs when using XOVER.
6368   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6369   (let ((mail-parse-charset gnus-newsgroup-charset)
6370         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6371         (cur nntp-server-buffer)
6372         (dependencies (or dependencies gnus-newsgroup-dependencies))
6373         (allp (cond
6374                ((eq gnus-read-all-available-headers t)
6375                 t)
6376                ((and (stringp gnus-read-all-available-headers)
6377                      group)
6378                 (string-match gnus-read-all-available-headers group))
6379                (t
6380                 nil)))
6381         number headers header)
6382     (with-current-buffer nntp-server-buffer
6383       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6384       ;; Allow the user to mangle the headers before parsing them.
6385       (gnus-run-hooks 'gnus-parse-headers-hook)
6386       (goto-char (point-min))
6387       (gnus-parse-without-error
6388         (while (and (or sequence allp)
6389                     (not (eobp)))
6390           (setq number (read cur))
6391           (when (not allp)
6392             (while (and sequence
6393                         (< (car sequence) number))
6394               (setq sequence (cdr sequence))))
6395           (when (and (or allp
6396                          (and sequence
6397                               (eq number (car sequence))))
6398                      (progn
6399                        (setq sequence (cdr sequence))
6400                        (setq header (inline
6401                                       (gnus-nov-parse-line
6402                                        number dependencies force-new)))))
6403             (push header headers))
6404           (forward-line 1)))
6405       ;; A common bug in inn is that if you have posted an article and
6406       ;; then retrieves the active file, it will answer correctly --
6407       ;; the new article is included.  However, a NOV entry for the
6408       ;; article may not have been generated yet, so this may fail.
6409       ;; We work around this problem by retrieving the last few
6410       ;; headers using HEAD.
6411       (if (or (not also-fetch-heads)
6412               (not sequence))
6413           ;; We (probably) got all the headers.
6414           (nreverse headers)
6415         (let ((gnus-nov-is-evil t))
6416           (nconc
6417            (nreverse headers)
6418            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6419              (gnus-get-newsgroup-headers))))))))
6420
6421 (defun gnus-article-get-xrefs ()
6422   "Fill in the Xref value in `gnus-current-headers', if necessary.
6423 This is meant to be called in `gnus-article-internal-prepare-hook'."
6424   (let ((headers (with-current-buffer gnus-summary-buffer
6425                    gnus-current-headers)))
6426     (or (not gnus-use-cross-reference)
6427         (not headers)
6428         (and (mail-header-xref headers)
6429              (not (string= (mail-header-xref headers) "")))
6430         (let ((case-fold-search t)
6431               xref)
6432           (save-restriction
6433             (nnheader-narrow-to-headers)
6434             (goto-char (point-min))
6435             (when (or (and (not (eobp))
6436                            (eq (downcase (char-after)) ?x)
6437                            (looking-at "Xref:"))
6438                       (search-forward "\nXref:" nil t))
6439               (goto-char (1+ (match-end 0)))
6440               (setq xref (buffer-substring (point) (point-at-eol)))
6441               (mail-header-set-xref headers xref)))))))
6442
6443 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6444   "Find article ID and insert the summary line for that article.
6445 OLD-HEADER can either be a header or a line number to insert
6446 the subject line on."
6447   (let* ((line (and (numberp old-header) old-header))
6448          (old-header (and (vectorp old-header) old-header))
6449          (header (cond ((and old-header use-old-header)
6450                         old-header)
6451                        ((and (numberp id)
6452                              (gnus-number-to-header id))
6453                         (gnus-number-to-header id))
6454                        (t
6455                         (gnus-read-header id))))
6456          (number (and (numberp id) id))
6457          d)
6458     (when header
6459       ;; Rebuild the thread that this article is part of and go to the
6460       ;; article we have fetched.
6461       (when (and (not gnus-show-threads)
6462                  old-header)
6463         (when (and number
6464                    (setq d (gnus-data-find (mail-header-number old-header))))
6465           (goto-char (gnus-data-pos d))
6466           (gnus-data-remove
6467            number
6468            (- (point-at-bol)
6469               (prog1
6470                   (1+ (point-at-eol))
6471                 (gnus-delete-line))))))
6472       ;; Remove list identifiers from subject.
6473       (when gnus-list-identifiers
6474         (let ((gnus-newsgroup-headers (list header)))
6475           (gnus-summary-remove-list-identifiers)))
6476       (when old-header
6477         (mail-header-set-number header (mail-header-number old-header)))
6478       (setq gnus-newsgroup-sparse
6479             (delq (setq number (mail-header-number header))
6480                   gnus-newsgroup-sparse))
6481       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6482       (push number gnus-newsgroup-limit)
6483       (gnus-rebuild-thread (mail-header-id header) line)
6484       (gnus-summary-goto-subject number nil t))
6485     (when (and (numberp number)
6486                (> number 0))
6487       ;; We have to update the boundaries even if we can't fetch the
6488       ;; article if ID is a number -- so that the next `P' or `N'
6489       ;; command will fetch the previous (or next) article even
6490       ;; if the one we tried to fetch this time has been canceled.
6491       (when (> number gnus-newsgroup-end)
6492         (setq gnus-newsgroup-end number))
6493       (when (< number gnus-newsgroup-begin)
6494         (setq gnus-newsgroup-begin number))
6495       (setq gnus-newsgroup-unselected
6496             (delq number gnus-newsgroup-unselected)))
6497     ;; Report back a success?
6498     (and header (mail-header-number header))))
6499
6500 ;;; Process/prefix in the summary buffer
6501
6502 (defun gnus-summary-work-articles (n)
6503   "Return a list of articles to be worked upon.
6504 The prefix argument, the list of process marked articles, and the
6505 current article will be taken into consideration."
6506   (with-current-buffer gnus-summary-buffer
6507     (cond
6508      (n
6509       ;; A numerical prefix has been given.
6510       (setq n (prefix-numeric-value n))
6511       (let ((backward (< n 0))
6512             (n (abs (prefix-numeric-value n)))
6513             articles article)
6514         (save-excursion
6515           (while
6516               (and (> n 0)
6517                    (push (setq article (gnus-summary-article-number))
6518                          articles)
6519                    (if backward
6520                        (gnus-summary-find-prev nil article)
6521                      (gnus-summary-find-next nil article)))
6522             (decf n)))
6523         (nreverse articles)))
6524      ((and (gnus-region-active-p) (mark))
6525       (message "region active")
6526       ;; Work on the region between point and mark.
6527       (let ((max (max (point) (mark)))
6528             articles article)
6529         (save-excursion
6530           (goto-char (min (point) (mark)))
6531           (while
6532               (and
6533                (push (setq article (gnus-summary-article-number)) articles)
6534                (gnus-summary-find-next nil article)
6535                (< (point) max)))
6536           (nreverse articles))))
6537      (gnus-newsgroup-processable
6538       ;; There are process-marked articles present.
6539       ;; Save current state.
6540       (gnus-summary-save-process-mark)
6541       ;; Return the list.
6542       (reverse gnus-newsgroup-processable))
6543      (t
6544       ;; Just return the current article.
6545       (list (gnus-summary-article-number))))))
6546
6547 (defmacro gnus-summary-iterate (arg &rest forms)
6548   "Iterate over the process/prefixed articles and do FORMS.
6549 ARG is the interactive prefix given to the command.  FORMS will be
6550 executed with point over the summary line of the articles."
6551   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6552     `(let ((,articles (gnus-summary-work-articles ,arg)))
6553        (while ,articles
6554          (gnus-summary-goto-subject (car ,articles))
6555          ,@forms
6556          (pop ,articles)))))
6557
6558 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6559 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6560
6561 (defun gnus-summary-save-process-mark ()
6562   "Push the current set of process marked articles on the stack."
6563   (interactive)
6564   (push (copy-sequence gnus-newsgroup-processable)
6565         gnus-newsgroup-process-stack))
6566
6567 (defun gnus-summary-kill-process-mark ()
6568   "Push the current set of process marked articles on the stack and unmark."
6569   (interactive)
6570   (gnus-summary-save-process-mark)
6571   (gnus-summary-unmark-all-processable))
6572
6573 (defun gnus-summary-yank-process-mark ()
6574   "Pop the last process mark state off the stack and restore it."
6575   (interactive)
6576   (unless gnus-newsgroup-process-stack
6577     (error "Empty mark stack"))
6578   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6579
6580 (defun gnus-summary-process-mark-set (set)
6581   "Make SET into the current process marked articles."
6582   (gnus-summary-unmark-all-processable)
6583   (mapc 'gnus-summary-set-process-mark set))
6584
6585 ;;; Searching and stuff
6586
6587 (defun gnus-summary-search-group (&optional backward use-level)
6588   "Search for next unread newsgroup.
6589 If optional argument BACKWARD is non-nil, search backward instead."
6590   (with-current-buffer gnus-group-buffer
6591     (when (gnus-group-search-forward
6592            backward nil (if use-level (gnus-group-group-level) nil))
6593       (gnus-group-group-name))))
6594
6595 (defun gnus-summary-best-group (&optional exclude-group)
6596   "Find the name of the best unread group.
6597 If EXCLUDE-GROUP, do not go to this group."
6598   (with-current-buffer gnus-group-buffer
6599     (save-excursion
6600       (gnus-group-best-unread-group exclude-group))))
6601
6602 (defun gnus-summary-find-next (&optional unread article backward)
6603   (if backward
6604       (gnus-summary-find-prev unread article)
6605     (let* ((dummy (gnus-summary-article-intangible-p))
6606            (article (or article (gnus-summary-article-number)))
6607            (data (gnus-data-find-list article))
6608            result)
6609       (when (and (not dummy)
6610                  (or (not gnus-summary-check-current)
6611                      (not unread)
6612                      (not (gnus-data-unread-p (car data)))))
6613         (setq data (cdr data)))
6614       (when (setq result
6615                   (if unread
6616                       (progn
6617                         (while data
6618                           (unless (memq (gnus-data-number (car data))
6619                                         (cond
6620                                          ((eq gnus-auto-goto-ignores
6621                                               'always-undownloaded)
6622                                           gnus-newsgroup-undownloaded)
6623                                          (gnus-plugged
6624                                           nil)
6625                                          ((eq gnus-auto-goto-ignores
6626                                               'unfetched)
6627                                           gnus-newsgroup-unfetched)
6628                                          ((eq gnus-auto-goto-ignores
6629                                               'undownloaded)
6630                                           gnus-newsgroup-undownloaded)))
6631                             (when (gnus-data-unread-p (car data))
6632                               (setq result (car data)
6633                                     data nil)))
6634                           (setq data (cdr data)))
6635                         result)
6636                     (car data)))
6637         (goto-char (gnus-data-pos result))
6638         (gnus-data-number result)))))
6639
6640 (defun gnus-summary-find-prev (&optional unread article)
6641   (let* ((eobp (eobp))
6642          (article (or article (gnus-summary-article-number)))
6643          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6644          result)
6645     (when (and (not eobp)
6646                (or (not gnus-summary-check-current)
6647                    (not unread)
6648                    (not (gnus-data-unread-p (car data)))))
6649       (setq data (cdr data)))
6650     (when (setq result
6651                 (if unread
6652                     (progn
6653                       (while data
6654                         (unless (memq (gnus-data-number (car data))
6655                                       (cond
6656                                        ((eq gnus-auto-goto-ignores
6657                                             'always-undownloaded)
6658                                         gnus-newsgroup-undownloaded)
6659                                        (gnus-plugged
6660                                         nil)
6661                                        ((eq gnus-auto-goto-ignores
6662                                             'unfetched)
6663                                         gnus-newsgroup-unfetched)
6664                                        ((eq gnus-auto-goto-ignores
6665                                             'undownloaded)
6666                                         gnus-newsgroup-undownloaded)))
6667                           (when (gnus-data-unread-p (car data))
6668                             (setq result (car data)
6669                                   data nil)))
6670                         (setq data (cdr data)))
6671                       result)
6672                   (car data)))
6673       (goto-char (gnus-data-pos result))
6674       (gnus-data-number result))))
6675
6676 (defun gnus-summary-find-subject (subject &optional unread backward article)
6677   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6678          (article (or article (gnus-summary-article-number)))
6679          (articles (gnus-data-list backward))
6680          (arts (gnus-data-find-list article articles))
6681          result)
6682     (when (or (not gnus-summary-check-current)
6683               (not unread)
6684               (not (gnus-data-unread-p (car arts))))
6685       (setq arts (cdr arts)))
6686     (while arts
6687       (and (or (not unread)
6688                (gnus-data-unread-p (car arts)))
6689            (vectorp (gnus-data-header (car arts)))
6690            (gnus-subject-equal
6691             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6692            (setq result (car arts)
6693                  arts nil))
6694       (setq arts (cdr arts)))
6695     (and result
6696          (goto-char (gnus-data-pos result))
6697          (gnus-data-number result))))
6698
6699 (defun gnus-summary-search-forward (&optional unread subject backward)
6700   "Search forward for an article.
6701 If UNREAD, look for unread articles.  If SUBJECT, look for
6702 articles with that subject.  If BACKWARD, search backward instead."
6703   (cond (subject (gnus-summary-find-subject subject unread backward))
6704         (backward (gnus-summary-find-prev unread))
6705         (t (gnus-summary-find-next unread))))
6706
6707 (defun gnus-recenter (&optional n)
6708   "Center point in window and redisplay frame.
6709 Also do horizontal recentering."
6710   (interactive "P")
6711   (when (and gnus-auto-center-summary
6712              (not (eq gnus-auto-center-summary 'vertical)))
6713     (gnus-horizontal-recenter))
6714   (if (fboundp 'recenter-top-bottom)
6715       (recenter-top-bottom n)
6716     (recenter n)))
6717
6718 (put 'gnus-recenter 'isearch-scroll t)
6719
6720 (defun gnus-forward-line-ignore-invisible (n)
6721   "Move N lines forward (backward if N is negative).
6722 Like forward-line, but skip over (and don't count) invisible lines."
6723   (let (done)
6724     (while (and (> n 0) (not done))
6725       ;; If the following character is currently invisible,
6726       ;; skip all characters with that same `invisible' property value.
6727       (while (gnus-invisible-p (point))
6728         (goto-char (gnus-next-char-property-change (point))))
6729       (forward-line 1)
6730       (if (eobp)
6731           (setq done t)
6732         (setq n (1- n))))
6733     (while (and (< n 0) (not done))
6734       (forward-line -1)
6735       (if (bobp) (setq done t)
6736         (setq n (1+ n))
6737         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6738           (goto-char (gnus-previous-char-property-change (point))))))))
6739
6740 (defun gnus-summary-recenter ()
6741   "Center point in the summary window.
6742 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6743 displayed, no centering will be performed."
6744   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6745   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6746   (interactive)
6747   ;; The user has to want it.
6748   (when gnus-auto-center-summary
6749     (let* ((top (cond ((< (window-height) 4) 0)
6750                       ((< (window-height) 7) 1)
6751                       (t (if (numberp gnus-auto-center-summary)
6752                              gnus-auto-center-summary
6753                            (/ (1- (window-height)) 2)))))
6754            (height (1- (window-height)))
6755            (bottom (save-excursion
6756                      (goto-char (point-max))
6757                      (gnus-forward-line-ignore-invisible (- height))
6758                      (point)))
6759            (window (get-buffer-window (current-buffer))))
6760       (when (get-buffer-window gnus-article-buffer)
6761         ;; Only do recentering when the article buffer is displayed,
6762         ;; Set the window start to either `bottom', which is the biggest
6763         ;; possible valid number, or the second line from the top,
6764         ;; whichever is the least.
6765         (let ((top-pos (save-excursion
6766                          (gnus-forward-line-ignore-invisible (- top))
6767                          (point))))
6768           (if (> bottom top-pos)
6769               ;; Keep the second line from the top visible
6770               (set-window-start window top-pos)
6771             ;; Try to keep the bottom line visible; if it's partially
6772             ;; obscured, either scroll one more line to make it fully
6773             ;; visible, or revert to using TOP-POS.
6774             (save-excursion
6775               (goto-char (point-max))
6776               (gnus-forward-line-ignore-invisible -1)
6777               (let ((last-line-start (point)))
6778                 (goto-char bottom)
6779                 (set-window-start window (point) t)
6780                 (when (not (pos-visible-in-window-p last-line-start window))
6781                   (gnus-forward-line-ignore-invisible 1)
6782                   (set-window-start window (min (point) top-pos) t)))))))
6783       ;; Do horizontal recentering while we're at it.
6784       (when (and (get-buffer-window (current-buffer) t)
6785                  (not (eq gnus-auto-center-summary 'vertical)))
6786         (let ((selected (selected-window)))
6787           (select-window (get-buffer-window (current-buffer) t))
6788           (gnus-summary-position-point)
6789           (gnus-horizontal-recenter)
6790           (select-window selected))))))
6791
6792 (defun gnus-summary-jump-to-group (newsgroup)
6793   "Move point to NEWSGROUP in group mode buffer."
6794   ;; Keep update point of group mode buffer if visible.
6795   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6796       (save-window-excursion
6797         ;; Take care of tree window mode.
6798         (when (get-buffer-window gnus-group-buffer)
6799           (pop-to-buffer gnus-group-buffer))
6800         (gnus-group-jump-to-group newsgroup))
6801     (save-excursion
6802       ;; Take care of tree window mode.
6803       (if (get-buffer-window gnus-group-buffer 0)
6804           (pop-to-buffer gnus-group-buffer)
6805         (set-buffer gnus-group-buffer))
6806       (gnus-group-jump-to-group newsgroup))))
6807
6808 ;; This function returns a list of article numbers based on the
6809 ;; difference between the ranges of read articles in this group and
6810 ;; the range of active articles.
6811 (defun gnus-list-of-unread-articles (group)
6812   (let* ((read (gnus-info-read (gnus-get-info group)))
6813          (active (or (gnus-active group) (gnus-activate-group group)))
6814          (last (or (cdr active)
6815                    (error "Group %s couldn't be activated " group)))
6816          (bottom (if gnus-newsgroup-maximum-articles
6817                      (max (car active)
6818                           (- last gnus-newsgroup-maximum-articles -1))
6819                    (car active)))
6820          first nlast unread)
6821     ;; If none are read, then all are unread.
6822     (if (not read)
6823         (setq first bottom)
6824       ;; If the range of read articles is a single range, then the
6825       ;; first unread article is the article after the last read
6826       ;; article.  Sounds logical, doesn't it?
6827       (if (and (not (listp (cdr read)))
6828                (or (< (car read) bottom)
6829                    (progn (setq read (list read))
6830                           nil)))
6831           (setq first (max bottom (1+ (cdr read))))
6832         ;; `read' is a list of ranges.
6833         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6834                                   (caar read)))
6835                   1)
6836           (setq first bottom))
6837         (while read
6838           (when first
6839             (while (< first nlast)
6840               (setq unread (cons first unread)
6841                     first (1+ first))))
6842           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6843           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6844           (setq read (cdr read)))))
6845     ;; And add the last unread articles.
6846     (while (<= first last)
6847       (setq unread (cons first unread)
6848             first (1+ first)))
6849     ;; Return the list of unread articles.
6850     (delq 0 (nreverse unread))))
6851
6852 (defun gnus-list-of-read-articles (group)
6853   "Return a list of unread, unticked and non-dormant articles."
6854   (let* ((info (gnus-get-info group))
6855          (marked (gnus-info-marks info))
6856          (active (gnus-active group)))
6857     (and info active
6858          (gnus-list-range-difference
6859           (gnus-list-range-difference
6860            (gnus-sorted-complement
6861             (gnus-uncompress-range
6862              (if gnus-newsgroup-maximum-articles
6863                  (cons (max (car active)
6864                             (- (cdr active)
6865                                gnus-newsgroup-maximum-articles
6866                                -1))
6867                        (cdr active))
6868                active))
6869             (gnus-list-of-unread-articles group))
6870            (cdr (assq 'dormant marked)))
6871           (cdr (assq 'tick marked))))))
6872
6873 ;; This function returns a sequence of article numbers based on the
6874 ;; difference between the ranges of read articles in this group and
6875 ;; the range of active articles.
6876 (defun gnus-sequence-of-unread-articles (group)
6877   (let* ((read (gnus-info-read (gnus-get-info group)))
6878          (active (or (gnus-active group) (gnus-activate-group group)))
6879          (last (cdr active))
6880          (bottom (if gnus-newsgroup-maximum-articles
6881                      (max (car active)
6882                           (- last gnus-newsgroup-maximum-articles -1))
6883                    (car active)))
6884          first nlast unread)
6885     ;; If none are read, then all are unread.
6886     (if (not read)
6887         (setq first bottom)
6888       ;; If the range of read articles is a single range, then the
6889       ;; first unread article is the article after the last read
6890       ;; article.  Sounds logical, doesn't it?
6891       (if (and (not (listp (cdr read)))
6892                (or (< (car read) bottom)
6893                    (progn (setq read (list read))
6894                           nil)))
6895           (setq first (max bottom (1+ (cdr read))))
6896         ;; `read' is a list of ranges.
6897         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6898                                   (caar read)))
6899                   1)
6900           (setq first bottom))
6901         (while read
6902           (when first
6903             (push (cons first nlast) unread))
6904           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6905           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6906           (setq read (cdr read)))))
6907     ;; And add the last unread articles.
6908     (cond ((not (and first last))
6909            nil)
6910           ((< first last)
6911            (push (cons first last) unread))
6912           ((= first last)
6913            (push first unread)))
6914     ;; Return the sequence of unread articles.
6915     (delq 0 (nreverse unread))))
6916
6917 ;; Various summary commands
6918
6919 (defun gnus-summary-select-article-buffer ()
6920   "Reconfigure windows to show article buffer."
6921   (interactive)
6922   (if (not (gnus-buffer-live-p gnus-article-buffer))
6923       (error "There is no article buffer for this summary buffer")
6924     (gnus-configure-windows 'article)
6925     (select-window (get-buffer-window gnus-article-buffer))))
6926
6927 (defun gnus-summary-universal-argument (arg)
6928   "Perform any operation on all articles that are process/prefixed."
6929   (interactive "P")
6930   (let ((articles (gnus-summary-work-articles arg))
6931         func article)
6932     (if (eq
6933          (setq
6934           func
6935           (key-binding
6936            (read-key-sequence
6937             (substitute-command-keys
6938              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6939          'undefined)
6940         (gnus-error 1 "Undefined key")
6941       (save-excursion
6942         (while articles
6943           (gnus-summary-goto-subject (setq article (pop articles)))
6944           (let (gnus-newsgroup-processable)
6945             (command-execute func))
6946           (gnus-summary-remove-process-mark article)))))
6947   (gnus-summary-position-point))
6948
6949 (defun gnus-summary-toggle-truncation (&optional arg)
6950   "Toggle truncation of summary lines.
6951 With ARG, turn line truncation on if ARG is positive."
6952   (interactive "P")
6953   (setq truncate-lines
6954         (if (null arg) (not truncate-lines)
6955           (> (prefix-numeric-value arg) 0)))
6956   (redraw-display))
6957
6958 (defun gnus-summary-find-for-reselect ()
6959   "Return the number of an article to stay on across a reselect.
6960 The current article is considered, then following articles, then previous
6961 articles.  An article is sought which is not cancelled and isn't a temporary
6962 insertion from another group.  If there's no such then return a dummy 0."
6963   (let (found)
6964     (dolist (rev '(nil t))
6965       (unless found      ; don't demand the reverse list if we don't need it
6966         (let ((data (gnus-data-find-list
6967                      (gnus-summary-article-number) (gnus-data-list rev))))
6968           (while (and data (not found))
6969             (if (and (< 0 (gnus-data-number (car data)))
6970                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6971                 (setq found (gnus-data-number (car data))))
6972             (setq data (cdr data))))))
6973     (or found 0)))
6974
6975 (defun gnus-summary-reselect-current-group (&optional all rescan)
6976   "Exit and then reselect the current newsgroup.
6977 The prefix argument ALL means to select all articles."
6978   (interactive "P")
6979   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6980     (error "Ephemeral groups can't be reselected"))
6981   (let ((current-subject (gnus-summary-find-for-reselect))
6982         (group gnus-newsgroup-name))
6983     (setq gnus-newsgroup-begin nil)
6984     (gnus-summary-exit nil 'leave-hidden)
6985     ;; We have to adjust the point of group mode buffer because
6986     ;; point was moved to the next unread newsgroup by exiting.
6987     (gnus-summary-jump-to-group group)
6988     (when rescan
6989       (save-excursion
6990         (gnus-group-get-new-news-this-group 1)))
6991     (gnus-group-read-group all t)
6992     (gnus-summary-goto-subject current-subject nil t)))
6993
6994 (defun gnus-summary-rescan-group (&optional all)
6995   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6996   (interactive "P")
6997   (gnus-summary-reselect-current-group all t))
6998
6999 (defun gnus-summary-update-info (&optional non-destructive)
7000   (save-excursion
7001     (let ((group gnus-newsgroup-name))
7002       (when group
7003         (when gnus-newsgroup-kill-headers
7004           (setq gnus-newsgroup-killed
7005                 (gnus-compress-sequence
7006                  (gnus-sorted-union
7007                   (gnus-list-range-intersection
7008                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7009                   gnus-newsgroup-unreads)
7010                  t)))
7011         (unless (listp (cdr gnus-newsgroup-killed))
7012           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7013         (let ((headers gnus-newsgroup-headers))
7014           ;; Set the new ranges of read articles.
7015           (with-current-buffer gnus-group-buffer
7016             (gnus-undo-force-boundary))
7017           (gnus-update-read-articles
7018            group (gnus-sorted-union
7019                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7020           ;; Set the current article marks.
7021           (let ((gnus-newsgroup-scored
7022                  (if (and (not gnus-save-score)
7023                           (not non-destructive))
7024                      nil
7025                    gnus-newsgroup-scored)))
7026             (save-excursion
7027               (gnus-update-marks)))
7028           ;; Do the cross-ref thing.
7029           (when gnus-use-cross-reference
7030             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7031           ;; Do not switch windows but change the buffer to work.
7032           (set-buffer gnus-group-buffer)
7033           (unless (gnus-ephemeral-group-p group)
7034             (gnus-group-update-group group)))))))
7035
7036 (defun gnus-summary-save-newsrc (&optional force)
7037   "Save the current number of read/marked articles in the dribble buffer.
7038 The dribble buffer will then be saved.
7039 If FORCE (the prefix), also save the .newsrc file(s)."
7040   (interactive "P")
7041   (gnus-summary-update-info t)
7042   (if force
7043       (gnus-save-newsrc-file)
7044     (gnus-dribble-save)))
7045
7046 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7047
7048 (defun gnus-summary-exit (&optional temporary leave-hidden)
7049   "Exit reading current newsgroup, and then return to group selection mode.
7050 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7051   (interactive)
7052   (gnus-set-global-variables)
7053   (when (gnus-buffer-live-p gnus-article-buffer)
7054     (with-current-buffer gnus-article-buffer
7055       (mm-destroy-parts gnus-article-mime-handles)
7056       ;; Set it to nil for safety reason.
7057       (setq gnus-article-mime-handle-alist nil)
7058       (setq gnus-article-mime-handles nil)))
7059   (gnus-kill-save-kill-buffer)
7060   (gnus-async-halt-prefetch)
7061   (let* ((group gnus-newsgroup-name)
7062          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7063          (gnus-group-is-exiting-p t)
7064          (mode major-mode)
7065          (group-point nil)
7066          (buf (current-buffer)))
7067     (unless quit-config
7068       ;; Do adaptive scoring, and possibly save score files.
7069       (when gnus-newsgroup-adaptive
7070         (gnus-score-adaptive))
7071       (when gnus-use-scoring
7072         (gnus-score-save)))
7073     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7074     ;; If we have several article buffers, we kill them at exit.
7075     (unless gnus-single-article-buffer
7076       (when (gnus-buffer-live-p gnus-article-buffer)
7077         (with-current-buffer gnus-article-buffer
7078           ;; Don't kill sticky article buffers
7079           (unless (eq major-mode 'gnus-sticky-article-mode)
7080             (gnus-kill-buffer gnus-article-buffer)
7081             (setq gnus-article-current nil))))
7082       (gnus-kill-buffer gnus-original-article-buffer))
7083     (when gnus-use-cache
7084       (gnus-cache-possibly-remove-articles)
7085       (gnus-cache-save-buffers))
7086     (gnus-async-prefetch-remove-group group)
7087     (when gnus-suppress-duplicates
7088       (gnus-dup-enter-articles))
7089     (when gnus-use-trees
7090       (gnus-tree-close group))
7091     (when gnus-use-cache
7092       (gnus-cache-write-active))
7093     ;; Remove entries for this group.
7094     (nnmail-purge-split-history (gnus-group-real-name group))
7095     ;; Make all changes in this group permanent.
7096     (unless quit-config
7097       (gnus-run-hooks 'gnus-exit-group-hook)
7098       (gnus-summary-update-info))
7099     (gnus-close-group group)
7100     ;; Make sure where we were, and go to next newsgroup.
7101     (set-buffer gnus-group-buffer)
7102     (unless quit-config
7103       (gnus-group-jump-to-group group))
7104     (gnus-run-hooks 'gnus-summary-exit-hook)
7105     (unless (or quit-config
7106                 (not gnus-summary-next-group-on-exit)
7107                 ;; If this group has disappeared from the summary
7108                 ;; buffer, don't skip forwards.
7109                 (not (string= group (gnus-group-group-name))))
7110       (gnus-group-next-unread-group 1))
7111     (setq group-point (point))
7112     (if temporary
7113         nil                             ;Nothing to do.
7114       (set-buffer buf)
7115       (if (not gnus-kill-summary-on-exit)
7116           (progn
7117             (gnus-deaden-summary)
7118             (setq mode nil))
7119         ;; We set all buffer-local variables to nil.  It is unclear why
7120         ;; this is needed, but if we don't, buffer-local variables are
7121         ;; not garbage-collected, it seems.  This would the lead to en
7122         ;; ever-growing Emacs.
7123         (gnus-summary-clear-local-variables)
7124         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7125           (gnus-summary-clear-local-variables))
7126         (when (get-buffer gnus-article-buffer)
7127           (bury-buffer gnus-article-buffer))
7128         ;; Return to group mode buffer.
7129         (when (eq mode 'gnus-summary-mode)
7130           (gnus-kill-buffer buf)))
7131       (setq gnus-current-select-method gnus-select-method)
7132       (set-buffer gnus-group-buffer)
7133       (if quit-config
7134           (gnus-handle-ephemeral-exit quit-config)
7135         (goto-char group-point)
7136         ;; If gnus-group-buffer is already displayed, make sure we also move
7137         ;; the cursor in the window that displays it.
7138         (let ((win (get-buffer-window (current-buffer) 0)))
7139           (if win (set-window-point win (point))))
7140         (unless leave-hidden
7141           (gnus-configure-windows 'group 'force)))
7142       ;; Clear the current group name.
7143       (unless quit-config
7144         (setq gnus-newsgroup-name nil)))))
7145
7146 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7147 (defun gnus-summary-exit-no-update (&optional no-questions)
7148   "Quit reading current newsgroup without updating read article info."
7149   (interactive)
7150   (let* ((group gnus-newsgroup-name)
7151          (gnus-group-is-exiting-p t)
7152          (gnus-group-is-exiting-without-update-p t)
7153          (quit-config (gnus-group-quit-config group)))
7154     (when (or no-questions
7155               gnus-expert-user
7156               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7157       (gnus-async-halt-prefetch)
7158       (run-hooks 'gnus-summary-prepare-exit-hook)
7159       (when (gnus-buffer-live-p gnus-article-buffer)
7160         (with-current-buffer gnus-article-buffer
7161           (mm-destroy-parts gnus-article-mime-handles)
7162           ;; Set it to nil for safety reason.
7163           (setq gnus-article-mime-handle-alist nil)
7164           (setq gnus-article-mime-handles nil)))
7165       ;; If we have several article buffers, we kill them at exit.
7166       (unless gnus-single-article-buffer
7167         (gnus-kill-buffer gnus-article-buffer)
7168         (gnus-kill-buffer gnus-original-article-buffer)
7169         (setq gnus-article-current nil))
7170       (if (not gnus-kill-summary-on-exit)
7171           (gnus-deaden-summary)
7172         (gnus-close-group group)
7173         (gnus-summary-clear-local-variables)
7174         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7175           (gnus-summary-clear-local-variables))
7176         (gnus-kill-buffer gnus-summary-buffer))
7177       (unless gnus-single-article-buffer
7178         (setq gnus-article-current nil))
7179       (when gnus-use-trees
7180         (gnus-tree-close group))
7181       (gnus-async-prefetch-remove-group group)
7182       (when (get-buffer gnus-article-buffer)
7183         (bury-buffer gnus-article-buffer))
7184       ;; Return to the group buffer.
7185       (gnus-configure-windows 'group 'force)
7186       ;; Clear the current group name.
7187       (setq gnus-newsgroup-name nil)
7188       (unless (gnus-ephemeral-group-p group)
7189         (gnus-group-update-group group))
7190       (when (equal (gnus-group-group-name) group)
7191         (gnus-group-next-unread-group 1))
7192       (when quit-config
7193         (gnus-handle-ephemeral-exit quit-config)))))
7194
7195 (defun gnus-handle-ephemeral-exit (quit-config)
7196   "Handle movement when leaving an ephemeral group.
7197 The state which existed when entering the ephemeral is reset."
7198   (if (not (buffer-name (car quit-config)))
7199       (gnus-configure-windows 'group 'force)
7200     (set-buffer (car quit-config))
7201     (cond ((eq major-mode 'gnus-summary-mode)
7202            (gnus-set-global-variables))
7203           ((eq major-mode 'gnus-article-mode)
7204            (save-current-buffer
7205              ;; The `gnus-summary-buffer' variable may point
7206              ;; to the old summary buffer when using a single
7207              ;; article buffer.
7208              (unless (gnus-buffer-live-p gnus-summary-buffer)
7209                (set-buffer gnus-group-buffer))
7210              (set-buffer gnus-summary-buffer)
7211              (gnus-set-global-variables))))
7212     (if (or (eq (cdr quit-config) 'article)
7213             (eq (cdr quit-config) 'pick))
7214         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7215             (gnus-configure-windows 'pick 'force)
7216           (gnus-configure-windows (cdr quit-config) 'force))
7217       (gnus-configure-windows (cdr quit-config) 'force))
7218     (when (eq major-mode 'gnus-summary-mode)
7219       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7220                                                      next-unread-noselect))
7221           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7222                                   'next-noselect)
7223                               (gnus-summary-next-subject 1 nil t))
7224                              ((eq gnus-auto-select-on-ephemeral-exit
7225                                   'next-unread-noselect)
7226                               (gnus-summary-next-subject 1 t t))))
7227             ;; Hide the article buffer which displays the article different
7228             ;; from the one that the cursor points to in the summary buffer.
7229             (gnus-configure-windows 'summary 'force))
7230         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7231                (gnus-summary-next-subject 1))
7232               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7233                (gnus-summary-next-subject 1 t))))
7234       (gnus-summary-recenter)
7235       (gnus-summary-position-point))))
7236
7237 ;;; Dead summaries.
7238
7239 (defvar gnus-dead-summary-mode-map nil)
7240
7241 (unless gnus-dead-summary-mode-map
7242   (setq gnus-dead-summary-mode-map (make-keymap))
7243   (suppress-keymap gnus-dead-summary-mode-map)
7244   (substitute-key-definition
7245    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7246   (dolist (key '("\C-d" "\r" "\177" [delete]))
7247     (define-key gnus-dead-summary-mode-map
7248       key 'gnus-summary-wake-up-the-dead))
7249   (dolist (key '("q" "Q"))
7250     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7251
7252 (defvar gnus-dead-summary-mode nil
7253   "Minor mode for Gnus summary buffers.")
7254
7255 (defun gnus-dead-summary-mode (&optional arg)
7256   "Minor mode for Gnus summary buffers."
7257   (interactive "P")
7258   (when (eq major-mode 'gnus-summary-mode)
7259     (make-local-variable 'gnus-dead-summary-mode)
7260     (setq gnus-dead-summary-mode
7261           (if (null arg) (not gnus-dead-summary-mode)
7262             (> (prefix-numeric-value arg) 0)))
7263     (when gnus-dead-summary-mode
7264       (add-minor-mode
7265        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7266
7267 (defun gnus-deaden-summary ()
7268   "Make the current summary buffer into a dead summary buffer."
7269   ;; Kill any previous dead summary buffer.
7270   (when (and gnus-dead-summary
7271              (buffer-name gnus-dead-summary))
7272     (with-current-buffer gnus-dead-summary
7273       (when gnus-dead-summary-mode
7274         (kill-buffer (current-buffer)))))
7275   ;; Make this the current dead summary.
7276   (setq gnus-dead-summary (current-buffer))
7277   (gnus-dead-summary-mode 1)
7278   (let ((name (buffer-name)))
7279     (when (string-match "Summary" name)
7280       (rename-buffer
7281        (concat (substring name 0 (match-beginning 0)) "Dead "
7282                (substring name (match-beginning 0)))
7283        t)
7284       (bury-buffer))))
7285
7286 (defun gnus-kill-or-deaden-summary (buffer)
7287   "Kill or deaden the summary BUFFER."
7288   (save-excursion
7289     (when (and (buffer-name buffer)
7290                (not gnus-single-article-buffer))
7291       (with-current-buffer buffer
7292         (gnus-kill-buffer gnus-article-buffer)
7293         (gnus-kill-buffer gnus-original-article-buffer)))
7294     (cond
7295      ;; Kill the buffer.
7296      (gnus-kill-summary-on-exit
7297       (when (and gnus-use-trees
7298                  (gnus-buffer-exists-p buffer))
7299         (with-current-buffer buffer
7300           (gnus-tree-close gnus-newsgroup-name)))
7301       (gnus-kill-buffer buffer))
7302      ;; Deaden the buffer.
7303      ((gnus-buffer-exists-p buffer)
7304       (with-current-buffer buffer
7305         (gnus-deaden-summary))))))
7306
7307 (defun gnus-summary-wake-up-the-dead (&rest args)
7308   "Wake up the dead summary buffer."
7309   (interactive)
7310   (gnus-dead-summary-mode -1)
7311   (let ((name (buffer-name)))
7312     (when (string-match "Dead " name)
7313       (rename-buffer
7314        (concat (substring name 0 (match-beginning 0))
7315                (substring name (match-end 0)))
7316        t)))
7317   (gnus-message 3 "This dead summary is now alive again"))
7318
7319 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7320 (defun gnus-summary-fetch-faq (&optional faq-dir)
7321   "Fetch the FAQ for the current group.
7322 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7323 in."
7324   (interactive
7325    (list
7326     (when current-prefix-arg
7327       (completing-read
7328        "FAQ dir: " (and (listp gnus-group-faq-directory)
7329                         (mapcar 'list
7330                                 gnus-group-faq-directory))))))
7331   (let (gnus-faq-buffer)
7332     (when (setq gnus-faq-buffer
7333                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7334       (gnus-configure-windows 'summary-faq))))
7335
7336 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7337 (defun gnus-summary-describe-group (&optional force)
7338   "Describe the current newsgroup."
7339   (interactive "P")
7340   (gnus-group-describe-group force gnus-newsgroup-name))
7341
7342 (defun gnus-summary-describe-briefly ()
7343   "Describe summary mode commands briefly."
7344   (interactive)
7345   (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")))
7346
7347 ;; Walking around group mode buffer from summary mode.
7348
7349 (defun gnus-summary-next-group (&optional no-article target-group backward)
7350   "Exit current newsgroup and then select next unread newsgroup.
7351 If prefix argument NO-ARTICLE is non-nil, no article is selected
7352 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7353 previous group instead."
7354   (interactive "P")
7355   ;; Stop pre-fetching.
7356   (gnus-async-halt-prefetch)
7357   (let ((current-group gnus-newsgroup-name)
7358         (current-buffer (current-buffer))
7359         entered)
7360     ;; First we semi-exit this group to update Xrefs and all variables.
7361     ;; We can't do a real exit, because the window conf must remain
7362     ;; the same in case the user is prompted for info, and we don't
7363     ;; want the window conf to change before that...
7364     (gnus-summary-exit t)
7365     (while (not entered)
7366       ;; Then we find what group we are supposed to enter.
7367       (set-buffer gnus-group-buffer)
7368       (gnus-group-jump-to-group current-group)
7369       (setq target-group
7370             (or target-group
7371                 (if (eq gnus-keep-same-level 'best)
7372                     (gnus-summary-best-group gnus-newsgroup-name)
7373                   (gnus-summary-search-group backward gnus-keep-same-level))))
7374       (if (not target-group)
7375           ;; There are no further groups, so we return to the group
7376           ;; buffer.
7377           (progn
7378             (gnus-message 5 "Returning to the group buffer")
7379             (setq entered t)
7380             (when (gnus-buffer-live-p current-buffer)
7381               (set-buffer current-buffer)
7382               (gnus-summary-exit))
7383             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7384         ;; We try to enter the target group.
7385         (gnus-group-jump-to-group target-group)
7386         (let ((unreads (gnus-group-group-unread)))
7387           (if (and (or (eq t unreads)
7388                        (and unreads (not (zerop unreads))))
7389                    (gnus-summary-read-group
7390                     target-group nil no-article
7391                     (and (buffer-name current-buffer) current-buffer)
7392                     nil backward))
7393               (setq entered t)
7394             (setq current-group target-group
7395                   target-group nil)))))))
7396
7397 (defun gnus-summary-prev-group (&optional no-article)
7398   "Exit current newsgroup and then select previous unread newsgroup.
7399 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7400   (interactive "P")
7401   (gnus-summary-next-group no-article nil t))
7402
7403 ;; Walking around summary lines.
7404
7405 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7406   "Go to the first subject satisfying any non-nil constraint.
7407 If UNREAD is non-nil, the article should be unread.
7408 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7409 If UNSEEN is non-nil, the article should be unseen.
7410 Returns the article selected or nil if there are no matching articles."
7411   (interactive "P")
7412   (cond
7413    ;; Empty summary.
7414    ((null gnus-newsgroup-data)
7415     (gnus-message 3 "No articles in the group")
7416     nil)
7417    ;; Pick the first article.
7418    ((not (or unread undownloaded unseen))
7419     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7420     (gnus-data-number (car gnus-newsgroup-data)))
7421    ;; Find the first unread article.
7422    (t
7423     (let ((data gnus-newsgroup-data))
7424       (while (and data
7425                   (let ((num (gnus-data-number (car data))))
7426                     (or (memq num gnus-newsgroup-unfetched)
7427                         (not (or (and unread
7428                                       (memq num gnus-newsgroup-unreads))
7429                                  (and undownloaded
7430                                       (memq num gnus-newsgroup-undownloaded))
7431                                  (and unseen
7432                                       (memq num gnus-newsgroup-unseen)))))))
7433         (setq data (cdr data)))
7434       (prog1
7435           (if data
7436               (progn
7437                 (goto-char (gnus-data-pos (car data)))
7438                 (gnus-data-number (car data)))
7439             (gnus-message 3 "No more%s articles"
7440                           (let* ((r (when unread " unread"))
7441                                  (d (when undownloaded " undownloaded"))
7442                                  (s (when unseen " unseen"))
7443                                  (l (delq nil (list r d s))))
7444                             (cond ((= 3 (length l))
7445                                    (concat r "," d ", or" s))
7446                                   ((= 2 (length l))
7447                                    (concat (car l) ", or" (cadr l)))
7448                                   ((= 1 (length l))
7449                                    (car l))
7450                                   (t
7451                                    ""))))
7452             nil
7453             )
7454         (gnus-summary-position-point))))))
7455
7456 (defun gnus-summary-next-subject (n &optional unread dont-display)
7457   "Go to next N'th summary line.
7458 If N is negative, go to the previous N'th subject line.
7459 If UNREAD is non-nil, only unread articles are selected.
7460 The difference between N and the actual number of steps taken is
7461 returned."
7462   (interactive "p")
7463   (let ((backward (< n 0))
7464         (n (abs n)))
7465     (while (and (> n 0)
7466                 (if backward
7467                     (gnus-summary-find-prev unread)
7468                   (gnus-summary-find-next unread)))
7469       (unless (zerop (setq n (1- n)))
7470         (gnus-summary-show-thread)))
7471     (when (/= 0 n)
7472       (gnus-message 7 "No more%s articles"
7473                     (if unread " unread" "")))
7474     (unless dont-display
7475       (gnus-summary-recenter)
7476       (gnus-summary-position-point))
7477     n))
7478
7479 (defun gnus-summary-next-unread-subject (n)
7480   "Go to next N'th unread summary line."
7481   (interactive "p")
7482   (gnus-summary-next-subject n t))
7483
7484 (defun gnus-summary-prev-subject (n &optional unread)
7485   "Go to previous N'th summary line.
7486 If optional argument UNREAD is non-nil, only unread article is selected."
7487   (interactive "p")
7488   (gnus-summary-next-subject (- n) unread))
7489
7490 (defun gnus-summary-prev-unread-subject (n)
7491   "Go to previous N'th unread summary line."
7492   (interactive "p")
7493   (gnus-summary-next-subject (- n) t))
7494
7495 (defun gnus-summary-goto-subjects (articles)
7496   "Insert the subject header for ARTICLES in the current buffer."
7497   (save-excursion
7498     (dolist (article articles)
7499       (gnus-summary-goto-subject article t)))
7500   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7501   (gnus-summary-position-point))
7502
7503 (defun gnus-summary-goto-subject (article &optional force silent)
7504   "Go to the subject line of ARTICLE.
7505 If FORCE, also allow jumping to articles not currently shown."
7506   (interactive "nArticle number: ")
7507   (unless (numberp article)
7508     (error "Article %s is not a number" article))
7509   (let ((b (point))
7510         (data (gnus-data-find article)))
7511     ;; We read in the article if we have to.
7512     (and (not data)
7513          force
7514          (gnus-summary-insert-subject
7515           article
7516           (if (or (numberp force) (vectorp force)) force)
7517           t)
7518          (setq data (gnus-data-find article)))
7519     (goto-char b)
7520     (if (not data)
7521         (progn
7522           (unless silent
7523             (gnus-message 3 "Can't find article %d" article))
7524           nil)
7525       (let ((pt (gnus-data-pos data)))
7526         (goto-char pt)
7527         (gnus-summary-set-article-display-arrow pt))
7528       (gnus-summary-position-point)
7529       article)))
7530
7531 ;; Walking around summary lines with displaying articles.
7532
7533 (defun gnus-summary-expand-window (&optional arg)
7534   "Make the summary buffer take up the entire Emacs frame.
7535 Given a prefix, will force an `article' buffer configuration."
7536   (interactive "P")
7537   (if arg
7538       (gnus-configure-windows 'article 'force)
7539     (gnus-configure-windows 'summary 'force)))
7540
7541 (defun gnus-summary-display-article (article &optional all-header)
7542   "Display ARTICLE in article buffer."
7543   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7544                (with-current-buffer gnus-article-buffer
7545                  (eq major-mode 'gnus-article-mode)))
7546     (gnus-article-setup-buffer))
7547   (gnus-set-global-variables)
7548   (with-current-buffer gnus-article-buffer
7549     (setq gnus-article-charset gnus-newsgroup-charset)
7550     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7551     (mm-enable-multibyte))
7552   (if (null article)
7553       nil
7554     (prog1
7555         (if gnus-summary-display-article-function
7556             (funcall gnus-summary-display-article-function article all-header)
7557           (gnus-article-prepare article all-header))
7558       (gnus-run-hooks 'gnus-select-article-hook)
7559       (when (and gnus-current-article
7560                  (not (zerop gnus-current-article)))
7561         (gnus-summary-goto-subject gnus-current-article))
7562       (gnus-summary-recenter)
7563       (when (and gnus-use-trees gnus-show-threads)
7564         (gnus-possibly-generate-tree article)
7565         (gnus-highlight-selected-tree article))
7566       ;; Successfully display article.
7567       (gnus-article-set-window-start
7568        (cdr (assq article gnus-newsgroup-bookmarks))))))
7569
7570 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7571   "Select the current article.
7572 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7573 non-nil, the article will be re-fetched even if it already present in
7574 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7575 be displayed."
7576   ;; Make sure we are in the summary buffer to work around bbdb bug.
7577   (unless (eq major-mode 'gnus-summary-mode)
7578     (set-buffer gnus-summary-buffer))
7579   (let ((article (or article (gnus-summary-article-number)))
7580         (all-headers (not (not all-headers))) ;Must be t or nil.
7581         gnus-summary-display-article-function)
7582     (and (not pseudo)
7583          (gnus-summary-article-pseudo-p article)
7584          (error "This is a pseudo-article"))
7585     (with-current-buffer gnus-summary-buffer
7586       (if (or (and gnus-single-article-buffer
7587                    (or (null gnus-current-article)
7588                        (null gnus-article-current)
7589                        (null (get-buffer gnus-article-buffer))
7590                        (not (eq article (cdr gnus-article-current)))
7591                        (not (equal (car gnus-article-current)
7592                                    gnus-newsgroup-name))))
7593               (and (not gnus-single-article-buffer)
7594                    (or (null gnus-current-article)
7595                        (not (eq gnus-current-article article))))
7596               force)
7597           ;; The requested article is different from the current article.
7598           (progn
7599             (gnus-summary-display-article article all-headers)
7600             (when (gnus-buffer-live-p gnus-article-buffer)
7601               (with-current-buffer gnus-article-buffer
7602                 (if (not gnus-article-decoded-p) ;; a local variable
7603                     (mm-disable-multibyte))))
7604             (gnus-article-set-window-start
7605              (cdr (assq article gnus-newsgroup-bookmarks)))
7606             article)
7607         'old))))
7608
7609 (defun gnus-summary-force-verify-and-decrypt ()
7610   "Display buttons for signed/encrypted parts and verify/decrypt them."
7611   (interactive)
7612   (let ((mm-verify-option 'known)
7613         (mm-decrypt-option 'known)
7614         (gnus-article-emulate-mime t)
7615         (gnus-buttonized-mime-types (append (list "multipart/signed"
7616                                                   "multipart/encrypted")
7617                                             gnus-buttonized-mime-types)))
7618     (gnus-summary-select-article nil 'force)))
7619
7620 (defun gnus-summary-set-current-mark (&optional current-mark)
7621   "Obsolete function."
7622   nil)
7623
7624 (defun gnus-summary-next-article (&optional unread subject backward push)
7625   "Select the next article.
7626 If UNREAD, only unread articles are selected.
7627 If SUBJECT, only articles with SUBJECT are selected.
7628 If BACKWARD, the previous article is selected instead of the next."
7629   (interactive "P")
7630   ;; Make sure we are in the summary buffer.
7631   (unless (eq major-mode 'gnus-summary-mode)
7632     (set-buffer gnus-summary-buffer))
7633   (cond
7634    ;; Is there such an article?
7635    ((and (gnus-summary-search-forward unread subject backward)
7636          (or (gnus-summary-display-article (gnus-summary-article-number))
7637              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7638     (gnus-summary-position-point))
7639    ;; If not, we try the first unread, if that is wanted.
7640    ((and subject
7641          gnus-auto-select-same
7642          (gnus-summary-first-unread-article))
7643     (gnus-summary-position-point)
7644     (gnus-message 6 "Wrapped"))
7645    ;; Try to get next/previous article not displayed in this group.
7646    ((and gnus-auto-extend-newsgroup
7647          (not unread) (not subject))
7648     (gnus-summary-goto-article
7649      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7650      nil (count-lines (point-min) (point))))
7651    ;; Go to next/previous group.
7652    (t
7653     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7654       (gnus-summary-jump-to-group gnus-newsgroup-name))
7655     (let ((cmd (if (featurep 'xemacs)
7656                    last-command-char
7657                  last-command-event))
7658           (point
7659            (with-current-buffer gnus-group-buffer
7660              (point)))
7661           (group
7662            (if (eq gnus-keep-same-level 'best)
7663                (gnus-summary-best-group gnus-newsgroup-name)
7664              (gnus-summary-search-group backward gnus-keep-same-level))))
7665       ;; For some reason, the group window gets selected.  We change
7666       ;; it back.
7667       (select-window (get-buffer-window (current-buffer)))
7668       ;; Select next unread newsgroup automagically.
7669       (cond
7670        ((or (not gnus-auto-select-next)
7671             (not cmd))
7672         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7673        ((or (eq gnus-auto-select-next 'quietly)
7674             (and (eq gnus-auto-select-next 'slightly-quietly)
7675                  push)
7676             (and (eq gnus-auto-select-next 'almost-quietly)
7677                  (gnus-summary-last-article-p)))
7678         ;; Select quietly.
7679         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7680             (gnus-summary-exit)
7681           (gnus-message 7 "No more%s articles (%s)..."
7682                         (if unread " unread" "")
7683                         (if group (concat "selecting " group)
7684                           "exiting"))
7685           (gnus-summary-next-group nil group backward)))
7686        (t
7687         (when (gnus-key-press-event-p last-input-event)
7688           (gnus-summary-walk-group-buffer
7689            gnus-newsgroup-name cmd unread backward point))))))))
7690
7691 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7692   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7693                       (?\C-p (gnus-group-prev-unread-group 1))))
7694         (cursor-in-echo-area t)
7695         keve key group ended prompt)
7696     (with-current-buffer gnus-group-buffer
7697       (goto-char start)
7698       (setq group
7699             (if (eq gnus-keep-same-level 'best)
7700                 (gnus-summary-best-group gnus-newsgroup-name)
7701               (gnus-summary-search-group backward gnus-keep-same-level))))
7702     (while (not ended)
7703       (setq prompt
7704             (format
7705              "No more%s articles%s " (if unread " unread" "")
7706              (if (and group
7707                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7708                  (format " (Type %s for %s [%s])"
7709                          (single-key-description cmd)
7710                          (gnus-group-decoded-name group)
7711                          (gnus-group-unread group))
7712                (format " (Type %s to exit %s)"
7713                        (single-key-description cmd)
7714                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7715       ;; Confirm auto selection.
7716       (setq key (car (setq keve (gnus-read-event-char prompt)))
7717             ended t)
7718       (cond
7719        ((assq key keystrokes)
7720         (let ((obuf (current-buffer)))
7721           (switch-to-buffer gnus-group-buffer)
7722           (when group
7723             (gnus-group-jump-to-group group))
7724           (eval (cadr (assq key keystrokes)))
7725           (setq group (gnus-group-group-name))
7726           (switch-to-buffer obuf))
7727         (setq ended nil))
7728        ((equal key cmd)
7729         (if (or (not group)
7730                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7731             (gnus-summary-exit)
7732           (gnus-summary-next-group nil group backward)))
7733        (t
7734         (push (cdr keve) unread-command-events))))))
7735
7736 (defun gnus-summary-next-unread-article ()
7737   "Select unread article after current one."
7738   (interactive)
7739   (gnus-summary-next-article
7740    (or (not (eq gnus-summary-goto-unread 'never))
7741        (gnus-summary-last-article-p (gnus-summary-article-number)))
7742    (and gnus-auto-select-same
7743         (gnus-summary-article-subject))))
7744
7745 (defun gnus-summary-prev-article (&optional unread subject)
7746   "Select the article before the current one.
7747 If UNREAD is non-nil, only unread articles are selected."
7748   (interactive "P")
7749   (gnus-summary-next-article unread subject t))
7750
7751 (defun gnus-summary-prev-unread-article ()
7752   "Select unread article before current one."
7753   (interactive)
7754   (gnus-summary-prev-article
7755    (or (not (eq gnus-summary-goto-unread 'never))
7756        (gnus-summary-first-article-p (gnus-summary-article-number)))
7757    (and gnus-auto-select-same
7758         (gnus-summary-article-subject))))
7759
7760 (defun gnus-summary-next-page (&optional lines circular stop)
7761   "Show next page of the selected article.
7762 If at the end of the current article, select the next article.
7763 LINES says how many lines should be scrolled up.
7764
7765 If CIRCULAR is non-nil, go to the start of the article instead of
7766 selecting the next article when reaching the end of the current
7767 article.
7768
7769 If STOP is non-nil, just stop when reaching the end of the message.
7770
7771 Also see the variable `gnus-article-skip-boring'."
7772   (interactive "P")
7773   (setq gnus-summary-buffer (current-buffer))
7774   (gnus-set-global-variables)
7775   (let ((article (gnus-summary-article-number))
7776         (article-window (get-buffer-window gnus-article-buffer t))
7777         endp)
7778     ;; If the buffer is empty, we have no article.
7779     (unless article
7780       (error "No article to select"))
7781     (gnus-configure-windows 'article)
7782     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7783         (if (and (eq gnus-summary-goto-unread 'never)
7784                  (not (gnus-summary-last-article-p article)))
7785             (gnus-summary-next-article)
7786           (gnus-summary-next-unread-article))
7787       (if (or (null gnus-current-article)
7788               (null gnus-article-current)
7789               (/= article (cdr gnus-article-current))
7790               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7791           ;; Selected subject is different from current article's.
7792           (gnus-summary-display-article article)
7793         (when article-window
7794           (gnus-eval-in-buffer-window gnus-article-buffer
7795             (setq endp (or (gnus-article-next-page lines)
7796                            (gnus-article-only-boring-p))))
7797           (when endp
7798             (cond (stop
7799                    (gnus-message 3 "End of message"))
7800                   (circular
7801                    (gnus-summary-beginning-of-article))
7802                   (lines
7803                    (gnus-message 3 "End of message"))
7804                   ((null lines)
7805                    (if (and (eq gnus-summary-goto-unread 'never)
7806                             (not (gnus-summary-last-article-p article)))
7807                        (gnus-summary-next-article)
7808                      (gnus-summary-next-unread-article))))))))
7809     (gnus-summary-recenter)
7810     (gnus-summary-position-point)))
7811
7812 (defun gnus-summary-prev-page (&optional lines move)
7813   "Show previous page of selected article.
7814 Argument LINES specifies lines to be scrolled down.
7815 If MOVE, move to the previous unread article if point is at
7816 the beginning of the buffer."
7817   (interactive "P")
7818   (let ((article (gnus-summary-article-number))
7819         (article-window (get-buffer-window gnus-article-buffer t))
7820         endp)
7821     (gnus-configure-windows 'article)
7822     (if (or (null gnus-current-article)
7823             (null gnus-article-current)
7824             (/= article (cdr gnus-article-current))
7825             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7826         ;; Selected subject is different from current article's.
7827         (gnus-summary-display-article article)
7828       (gnus-summary-recenter)
7829       (when article-window
7830         (gnus-eval-in-buffer-window gnus-article-buffer
7831           (setq endp (gnus-article-prev-page lines)))
7832         (when (and move endp)
7833           (cond (lines
7834                  (gnus-message 3 "Beginning of message"))
7835                 ((null lines)
7836                  (if (and (eq gnus-summary-goto-unread 'never)
7837                           (not (gnus-summary-first-article-p article)))
7838                      (gnus-summary-prev-article)
7839                    (gnus-summary-prev-unread-article))))))))
7840   (gnus-summary-position-point))
7841
7842 (defun gnus-summary-prev-page-or-article (&optional lines)
7843   "Show previous page of selected article.
7844 Argument LINES specifies lines to be scrolled down.
7845 If at the beginning of the article, go to the next article."
7846   (interactive "P")
7847   (gnus-summary-prev-page lines t))
7848
7849 (defun gnus-summary-scroll-up (lines)
7850   "Scroll up (or down) one line current article.
7851 Argument LINES specifies lines to be scrolled up (or down if negative)."
7852   (interactive "p")
7853   (gnus-configure-windows 'article)
7854   (gnus-summary-show-thread)
7855   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7856     (gnus-eval-in-buffer-window gnus-article-buffer
7857       (cond ((> lines 0)
7858              (when (gnus-article-next-page lines)
7859                (gnus-message 3 "End of message")))
7860             ((< lines 0)
7861              (gnus-article-prev-page (- lines))))))
7862   (gnus-summary-recenter)
7863   (gnus-summary-position-point))
7864
7865 (defun gnus-summary-scroll-down (lines)
7866   "Scroll down (or up) one line current article.
7867 Argument LINES specifies lines to be scrolled down (or up if negative)."
7868   (interactive "p")
7869   (gnus-summary-scroll-up (- lines)))
7870
7871 (defun gnus-summary-next-same-subject ()
7872   "Select next article which has the same subject as current one."
7873   (interactive)
7874   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7875
7876 (defun gnus-summary-prev-same-subject ()
7877   "Select previous article which has the same subject as current one."
7878   (interactive)
7879   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7880
7881 (defun gnus-summary-next-unread-same-subject ()
7882   "Select next unread article which has the same subject as current one."
7883   (interactive)
7884   (gnus-summary-next-article t (gnus-summary-article-subject)))
7885
7886 (defun gnus-summary-prev-unread-same-subject ()
7887   "Select previous unread article which has the same subject as current one."
7888   (interactive)
7889   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7890
7891 (defun gnus-summary-first-unread-article ()
7892   "Select the first unread article.
7893 Return nil if there are no unread articles."
7894   (interactive)
7895   (prog1
7896       (when (gnus-summary-first-subject t)
7897         (gnus-summary-show-thread)
7898         (gnus-summary-first-subject t)
7899         (gnus-summary-display-article (gnus-summary-article-number)))
7900     (gnus-summary-position-point)))
7901
7902 (defun gnus-summary-first-unread-subject ()
7903   "Place the point on the subject line of the first unread article.
7904 Return nil if there are no unread articles."
7905   (interactive)
7906   (prog1
7907       (when (gnus-summary-first-subject t)
7908         (gnus-summary-show-thread)
7909         (gnus-summary-first-subject t))
7910     (gnus-summary-position-point)))
7911
7912 (defun gnus-summary-first-unseen-subject ()
7913   "Place the point on the subject line of the first unseen article.
7914 Return nil if there are no unseen articles."
7915   (interactive)
7916   (prog1
7917       (when (gnus-summary-first-subject nil nil t)
7918         (gnus-summary-show-thread)
7919         (gnus-summary-first-subject nil nil t))
7920     (gnus-summary-position-point)))
7921
7922 (defun gnus-summary-first-unseen-or-unread-subject ()
7923   "Place the point on the subject line of the first unseen article or,
7924 if all article have been seen, on the subject line of the first unread
7925 article."
7926   (interactive)
7927   (prog1
7928       (unless (when (gnus-summary-first-subject nil nil t)
7929                 (gnus-summary-show-thread)
7930                 (gnus-summary-first-subject nil nil t))
7931         (when (gnus-summary-first-subject t)
7932           (gnus-summary-show-thread)
7933           (gnus-summary-first-subject t)))
7934     (gnus-summary-position-point)))
7935
7936 (defun gnus-summary-first-article ()
7937   "Select the first article.
7938 Return nil if there are no articles."
7939   (interactive)
7940   (prog1
7941       (when (gnus-summary-first-subject)
7942         (gnus-summary-show-thread)
7943         (gnus-summary-first-subject)
7944         (gnus-summary-display-article (gnus-summary-article-number)))
7945     (gnus-summary-position-point)))
7946
7947 (defun gnus-summary-best-unread-article (&optional arg)
7948   "Select the unread article with the highest score.
7949 If given a prefix argument, select the next unread article that has a
7950 score higher than the default score."
7951   (interactive "P")
7952   (let ((article (if arg
7953                      (gnus-summary-better-unread-subject)
7954                    (gnus-summary-best-unread-subject))))
7955     (if article
7956         (gnus-summary-goto-article article)
7957       (error "No unread articles"))))
7958
7959 (defun gnus-summary-best-unread-subject ()
7960   "Select the unread subject with the highest score."
7961   (interactive)
7962   (let ((best -1000000)
7963         (data gnus-newsgroup-data)
7964         article score)
7965     (while data
7966       (and (gnus-data-unread-p (car data))
7967            (> (setq score
7968                     (gnus-summary-article-score (gnus-data-number (car data))))
7969               best)
7970            (setq best score
7971                  article (gnus-data-number (car data))))
7972       (setq data (cdr data)))
7973     (when article
7974       (gnus-summary-goto-subject article))
7975     (gnus-summary-position-point)
7976     article))
7977
7978 (defun gnus-summary-better-unread-subject ()
7979   "Select the first unread subject that has a score over the default score."
7980   (interactive)
7981   (let ((data gnus-newsgroup-data)
7982         article score)
7983     (while (and (setq article (gnus-data-number (car data)))
7984                 (or (gnus-data-read-p (car data))
7985                     (not (> (gnus-summary-article-score article)
7986                             gnus-summary-default-score))))
7987       (setq data (cdr data)))
7988     (when article
7989       (gnus-summary-goto-subject article))
7990     (gnus-summary-position-point)
7991     article))
7992
7993 (defun gnus-summary-last-subject ()
7994   "Go to the last displayed subject line in the group."
7995   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7996     (when article
7997       (gnus-summary-goto-subject article))))
7998
7999 (defun gnus-summary-goto-article (article &optional all-headers force)
8000   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8001 If ALL-HEADERS is non-nil, no header lines are hidden.
8002 If FORCE, go to the article even if it isn't displayed.  If FORCE
8003 is a number, it is the line the article is to be displayed on."
8004   (interactive
8005    (list
8006     (completing-read
8007      "Article number or Message-ID: "
8008      (mapcar (lambda (number) (list (int-to-string number)))
8009              gnus-newsgroup-limit))
8010     current-prefix-arg
8011     t))
8012   (prog1
8013       (if (and (stringp article)
8014                (string-match "@\\|%40" article))
8015           (gnus-summary-refer-article article)
8016         (when (stringp article)
8017           (setq article (string-to-number article)))
8018         (if (gnus-summary-goto-subject article force)
8019             (gnus-summary-display-article article all-headers)
8020           (gnus-message 4 "Couldn't go to article %s" article) nil))
8021     (gnus-summary-position-point)))
8022
8023 (defun gnus-summary-goto-last-article ()
8024   "Go to the previously read article."
8025   (interactive)
8026   (prog1
8027       (when gnus-last-article
8028         (gnus-summary-goto-article gnus-last-article nil t))
8029     (gnus-summary-position-point)))
8030
8031 (defun gnus-summary-pop-article (number)
8032   "Pop one article off the history and go to the previous.
8033 NUMBER articles will be popped off."
8034   (interactive "p")
8035   (let (to)
8036     (setq gnus-newsgroup-history
8037           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8038     (if to
8039         (gnus-summary-goto-article (car to) nil t)
8040       (error "Article history empty")))
8041   (gnus-summary-position-point))
8042
8043 ;; Summary commands and functions for limiting the summary buffer.
8044
8045 (defun gnus-summary-limit-to-articles (n)
8046   "Limit the summary buffer to the next N articles.
8047 If not given a prefix, use the process marked articles instead."
8048   (interactive "P")
8049   (prog1
8050       (let ((articles (gnus-summary-work-articles n)))
8051         (setq gnus-newsgroup-processable nil)
8052         (gnus-summary-limit articles))
8053     (gnus-summary-position-point)))
8054
8055 (defun gnus-summary-pop-limit (&optional total)
8056   "Restore the previous limit.
8057 If given a prefix, remove all limits."
8058   (interactive "P")
8059   (when total
8060     (setq gnus-newsgroup-limits
8061           (list (mapcar (lambda (h) (mail-header-number h))
8062                         gnus-newsgroup-headers))))
8063   (unless gnus-newsgroup-limits
8064     (error "No limit to pop"))
8065   (prog1
8066       (gnus-summary-limit nil 'pop)
8067     (gnus-summary-position-point)))
8068
8069 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8070   "Limit the summary buffer to articles that have subjects that match a regexp.
8071 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8072   (interactive
8073    (list (read-string (if current-prefix-arg
8074                           "Exclude subject (regexp): "
8075                         "Limit to subject (regexp): "))
8076          nil current-prefix-arg))
8077   (unless header
8078     (setq header "subject"))
8079   (when (not (equal "" subject))
8080     (prog1
8081         (let ((articles (gnus-summary-find-matching
8082                          (or header "subject") subject 'all nil nil
8083                          not-matching)))
8084           (unless articles
8085             (error "Found no matches for \"%s\"" subject))
8086           (gnus-summary-limit articles))
8087       (gnus-summary-position-point))))
8088
8089 (defun gnus-summary-limit-to-author (from &optional not-matching)
8090   "Limit the summary buffer to articles that have authors that match a regexp.
8091 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8092   (interactive
8093    (list (read-string (if current-prefix-arg
8094                           "Exclude author (regexp): "
8095                         "Limit to author (regexp): "))
8096          current-prefix-arg))
8097   (gnus-summary-limit-to-subject from "from" not-matching))
8098
8099 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8100   "Limit the summary buffer to articles with the given RECIPIENT.
8101
8102 If NOT-MATCHING, exclude RECIPIENT.
8103
8104 To and Cc headers are checked.  You need to include them in
8105 `nnmail-extra-headers'."
8106   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8107   (interactive
8108    (list (read-string (format "%s recipient (regexp): "
8109                               (if current-prefix-arg "Exclude" "Limit to")))
8110          current-prefix-arg))
8111   (when (not (equal "" recipient))
8112     (prog1 (let* ((to
8113                    (if (memq 'To nnmail-extra-headers)
8114                        (gnus-summary-find-matching
8115                         (cons 'extra 'To) recipient 'all nil nil
8116                         not-matching)
8117                      (gnus-message
8118                       1 "`To' isn't present in `nnmail-extra-headers'")
8119                      (sit-for 1)
8120                      nil))
8121                   (cc
8122                    (if (memq 'Cc nnmail-extra-headers)
8123                        (gnus-summary-find-matching
8124                         (cons 'extra 'Cc) recipient 'all nil nil
8125                         not-matching)
8126                      (gnus-message
8127                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8128                      (sit-for 1)
8129                      nil))
8130                   (articles
8131                    (if not-matching
8132                        ;; We need the numbers that are in both lists:
8133                        (mapcar (lambda (a)
8134                                  (and (memq a to) a))
8135                                cc)
8136                      (nconc to cc))))
8137              (unless articles
8138                (error "Found no matches for \"%s\"" recipient))
8139              (gnus-summary-limit articles))
8140       (gnus-summary-position-point))))
8141
8142 (defun gnus-summary-limit-to-address (address &optional not-matching)
8143   "Limit the summary buffer to articles with the given ADDRESS.
8144
8145 If NOT-MATCHING, exclude ADDRESS.
8146
8147 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8148 in `nnmail-extra-headers'."
8149   (interactive
8150    (list (read-string (format "%s address (regexp): "
8151                               (if current-prefix-arg "Exclude" "Limit to")))
8152          current-prefix-arg))
8153   (when (not (equal "" address))
8154     (prog1 (let* ((to
8155                    (if (memq 'To nnmail-extra-headers)
8156                        (gnus-summary-find-matching
8157                         (cons 'extra 'To) address 'all nil nil
8158                         not-matching)
8159                      (gnus-message
8160                       1 "`To' isn't present in `nnmail-extra-headers'")
8161                      (sit-for 1)
8162                      t))
8163                   (cc
8164                    (if (memq 'Cc nnmail-extra-headers)
8165                        (gnus-summary-find-matching
8166                         (cons 'extra 'Cc) address 'all nil nil
8167                         not-matching)
8168                      (gnus-message
8169                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8170                      (sit-for 1)
8171                      t))
8172                   (from
8173                    (gnus-summary-find-matching "from" address
8174                                                'all nil nil not-matching))
8175                   (articles
8176                    (if not-matching
8177                        ;; We need the numbers that are in all lists:
8178                        (if (eq cc t)
8179                            (if (eq to t)
8180                                from
8181                              (mapcar (lambda (a) (car (memq a from))) to))
8182                          (if (eq to t)
8183                              (mapcar (lambda (a) (car (memq a from))) cc)
8184                            (mapcar (lambda (a) (car (memq a from)))
8185                                    (mapcar (lambda (a) (car (memq a to)))
8186                                            cc))))
8187                      (nconc (if (eq to t) nil to)
8188                             (if (eq cc t) nil cc)
8189                             from))))
8190              (unless articles
8191                (error "Found no matches for \"%s\"" address))
8192              (gnus-summary-limit articles))
8193       (gnus-summary-position-point))))
8194
8195 (defun gnus-summary-limit-strange-charsets-predicate (header)
8196   (let ((string (concat (mail-header-subject header)
8197                         (mail-header-from header)))
8198         charset found)
8199     (dotimes (i (1- (length string)))
8200       (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8201       (when (string-match "unicode\\|big\\|japanese" charset)
8202         (setq found t)))
8203     found))
8204
8205 (defun gnus-summary-limit-to-predicate (predicate)
8206   "Limit to articles where PREDICATE returns non-nil.
8207 PREDICATE will be called with the header structures of the
8208 articles."
8209   (let ((articles nil)
8210         (case-fold-search t))
8211     (dolist (header gnus-newsgroup-headers)
8212       (when (funcall predicate header)
8213         (push (mail-header-number header) articles)))
8214     (gnus-summary-limit (nreverse articles))))
8215
8216 (defun gnus-summary-limit-to-age (age &optional younger-p)
8217   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8218 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8219 articles that are younger than AGE days."
8220   (interactive
8221    (let ((younger current-prefix-arg)
8222          (days-got nil)
8223          days)
8224      (while (not days-got)
8225        (setq days (if younger
8226                       (read-string "Limit to articles younger than (in days, older when negative): ")
8227                     (read-string
8228                      "Limit to articles older than (in days, younger when negative): ")))
8229        (when (> (length days) 0)
8230          (setq days (read days)))
8231        (if (numberp days)
8232            (progn
8233              (setq days-got t)
8234              (when (< days 0)
8235                (setq younger (not younger))
8236                (setq days (* days -1))))
8237          (message "Please enter a number.")
8238          (sleep-for 1)))
8239      (list days younger)))
8240   (prog1
8241       (let ((data gnus-newsgroup-data)
8242             (cutoff (days-to-time age))
8243             articles d date is-younger)
8244         (while (setq d (pop data))
8245           (when (and (vectorp (gnus-data-header d))
8246                      (setq date (mail-header-date (gnus-data-header d))))
8247             (setq is-younger (time-less-p
8248                               (time-since (condition-case ()
8249                                               (date-to-time date)
8250                                             (error '(0 0))))
8251                               cutoff))
8252             (when (if younger-p
8253                       is-younger
8254                     (not is-younger))
8255               (push (gnus-data-number d) articles))))
8256         (gnus-summary-limit (nreverse articles)))
8257     (gnus-summary-position-point)))
8258
8259 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8260   "Limit the summary buffer to articles that match an 'extra' header."
8261   (interactive
8262    (let ((header
8263           (intern
8264            (gnus-completing-read-with-default
8265             (symbol-name (car gnus-extra-headers))
8266             (if current-prefix-arg
8267                 "Exclude extra header"
8268               "Limit extra header")
8269             (mapcar (lambda (x)
8270                       (cons (symbol-name x) x))
8271                     gnus-extra-headers)
8272             nil
8273             t))))
8274      (list header
8275            (read-string (format "%s header %s (regexp): "
8276                                 (if current-prefix-arg "Exclude" "Limit to")
8277                                 header))
8278            current-prefix-arg)))
8279   (when (not (equal "" regexp))
8280     (prog1
8281         (let ((articles (gnus-summary-find-matching
8282                          (cons 'extra header) regexp 'all nil nil
8283                          not-matching)))
8284           (unless articles
8285             (error "Found no matches for \"%s\"" regexp))
8286           (gnus-summary-limit articles))
8287       (gnus-summary-position-point))))
8288
8289 (defun gnus-summary-limit-to-display-predicate ()
8290   "Limit the summary buffer to the predicated in the `display' group parameter."
8291   (interactive)
8292   (unless gnus-newsgroup-display
8293     (error "There is no `display' group parameter"))
8294   (let (articles)
8295     (dolist (number gnus-newsgroup-articles)
8296       (when (funcall gnus-newsgroup-display)
8297         (push number articles)))
8298     (gnus-summary-limit articles))
8299   (gnus-summary-position-point))
8300
8301 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8302 (make-obsolete
8303  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread "Emacs 20.4")
8304
8305 (defun gnus-summary-limit-to-unread (&optional all)
8306   "Limit the summary buffer to articles that are not marked as read.
8307 If ALL is non-nil, limit strictly to unread articles."
8308   (interactive "P")
8309   (if all
8310       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8311     (gnus-summary-limit-to-marks
8312      ;; Concat all the marks that say that an article is read and have
8313      ;; those removed.
8314      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8315            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8316            gnus-low-score-mark gnus-expirable-mark
8317            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8318            gnus-duplicate-mark gnus-souped-mark)
8319      'reverse)))
8320
8321 (defun gnus-summary-limit-to-headers (match &optional reverse)
8322   "Limit the summary buffer to articles that have headers that match MATCH.
8323 If REVERSE (the prefix), limit to articles that don't match."
8324   (interactive "sMatch headers (regexp): \nP")
8325   (gnus-summary-limit-to-bodies match reverse t))
8326
8327 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8328   "Limit the summary buffer to articles that have bodies that match MATCH.
8329 If REVERSE (the prefix), limit to articles that don't match."
8330   (interactive "sMatch body (regexp): \nP")
8331   (let ((articles nil)
8332         (gnus-select-article-hook nil)  ;Disable hook.
8333         (gnus-article-prepare-hook nil)
8334         (gnus-use-article-prefetch nil)
8335         (gnus-keep-backlog nil)
8336         (gnus-break-pages nil)
8337         (gnus-summary-display-arrow nil)
8338         (gnus-updated-mode-lines nil)
8339         (gnus-auto-center-summary nil)
8340         (gnus-display-mime-function nil))
8341     (dolist (data gnus-newsgroup-data)
8342       (let (gnus-mark-article-hook)
8343         (gnus-summary-select-article t t nil (gnus-data-number data)))
8344       (save-excursion
8345         (set-buffer gnus-article-buffer)
8346         (article-goto-body)
8347         (let* ((case-fold-search t)
8348                (found (if headersp
8349                           (re-search-backward match nil t)
8350                         (re-search-forward match nil t))))
8351           (when (or (and found
8352                          (not reverse))
8353                     (and (not found)
8354                          reverse))
8355             (push (gnus-data-number data) articles)))))
8356     (if (not articles)
8357         (message "No messages matched")
8358       (gnus-summary-limit articles)))
8359   (gnus-summary-position-point))
8360
8361 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8362   "Limit the summary buffer to articles that aren't part on any thread.
8363 If THREADSP (the prefix), limit to articles that are in threads."
8364   (interactive "P")
8365   (let ((articles nil)
8366         thread-articles
8367         threads)
8368     (dolist (thread gnus-newsgroup-threads)
8369       (if (stringp (car thread))
8370           (dolist (thread (cdr thread))
8371             (push thread threads))
8372         (push thread threads)))
8373     (dolist (thread threads)
8374       (setq thread-articles (gnus-articles-in-thread thread))
8375       (when (or (and threadsp
8376                      (> (length thread-articles) 1))
8377                 (and (not threadsp)
8378                      (= (length thread-articles) 1)))
8379         (setq articles (nconc thread-articles articles))))
8380     (if (not articles)
8381         (message "No messages matched")
8382       (gnus-summary-limit articles))
8383     (gnus-summary-position-point)))
8384
8385 (defun gnus-summary-limit-to-replied (&optional unreplied)
8386   "Limit the summary buffer to replied articles.
8387 If UNREPLIED (the prefix), limit to unreplied articles."
8388   (interactive "P")
8389   (if unreplied
8390       (gnus-summary-limit
8391        (gnus-set-difference gnus-newsgroup-articles
8392         gnus-newsgroup-replied))
8393     (gnus-summary-limit gnus-newsgroup-replied))
8394   (gnus-summary-position-point))
8395
8396 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8397 (make-obsolete 'gnus-summary-delete-marked-with
8398                'gnus-summary-limit-exclude-marks "Emacs 20.4")
8399
8400 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8401   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8402 If REVERSE, limit the summary buffer to articles that are marked
8403 with MARKS.  MARKS can either be a string of marks or a list of marks.
8404 Returns how many articles were removed."
8405   (interactive "sMarks: ")
8406   (gnus-summary-limit-to-marks marks t))
8407
8408 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8409   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8410 If REVERSE (the prefix), limit the summary buffer to articles that are
8411 not marked with MARKS.  MARKS can either be a string of marks or a
8412 list of marks.
8413 Returns how many articles were removed."
8414   (interactive "sMarks: \nP")
8415   (prog1
8416       (let ((data gnus-newsgroup-data)
8417             (marks (if (listp marks) marks
8418                      (append marks nil))) ; Transform to list.
8419             articles)
8420         (while data
8421           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8422                   (memq (gnus-data-mark (car data)) marks))
8423             (push (gnus-data-number (car data)) articles))
8424           (setq data (cdr data)))
8425         (gnus-summary-limit articles))
8426     (gnus-summary-position-point)))
8427
8428 (defun gnus-summary-limit-to-score (score)
8429   "Limit to articles with score at or above SCORE."
8430   (interactive "NLimit to articles with score of at least: ")
8431   (let ((data gnus-newsgroup-data)
8432         articles)
8433     (while data
8434       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8435                 score)
8436         (push (gnus-data-number (car data)) articles))
8437       (setq data (cdr data)))
8438     (prog1
8439         (gnus-summary-limit articles)
8440       (gnus-summary-position-point))))
8441
8442 (defun gnus-summary-limit-to-unseen ()
8443   "Limit to unseen articles."
8444   (interactive)
8445   (prog1
8446       (gnus-summary-limit gnus-newsgroup-unseen)
8447     (gnus-summary-position-point)))
8448
8449 (defun gnus-summary-limit-include-thread (id)
8450   "Display all the hidden articles that is in the thread with ID in it.
8451 When called interactively, ID is the Message-ID of the current
8452 article."
8453   (interactive (list (mail-header-id (gnus-summary-article-header))))
8454   (let ((articles (gnus-articles-in-thread
8455                    (gnus-id-to-thread (gnus-root-id id)))))
8456     (prog1
8457         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8458       (gnus-summary-limit-include-matching-articles
8459        "subject"
8460        (regexp-quote (gnus-simplify-subject-re
8461                       (mail-header-subject (gnus-id-to-header id)))))
8462       (gnus-summary-position-point))))
8463
8464 (defun gnus-summary-limit-include-matching-articles (header regexp)
8465   "Display all the hidden articles that have HEADERs that match REGEXP."
8466   (interactive (list (read-string "Match on header: ")
8467                      (read-string "Regexp: ")))
8468   (let ((articles (gnus-find-matching-articles header regexp)))
8469     (prog1
8470         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8471       (gnus-summary-position-point))))
8472
8473 (defun gnus-summary-insert-dormant-articles ()
8474   "Insert all the dormant articles for this group into the current buffer."
8475   (interactive)
8476   (let ((gnus-verbose (max 6 gnus-verbose)))
8477     (if (not gnus-newsgroup-dormant)
8478         (gnus-message 3 "No dormant articles for this group")
8479       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8480
8481 (defun gnus-summary-insert-ticked-articles ()
8482   "Insert ticked articles for this group into the current buffer."
8483   (interactive)
8484   (let ((gnus-verbose (max 6 gnus-verbose)))
8485     (if (not gnus-newsgroup-marked)
8486         (gnus-message 3 "No ticked articles for this group")
8487       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8488
8489 (defun gnus-summary-limit-include-dormant ()
8490   "Display all the hidden articles that are marked as dormant.
8491 Note that this command only works on a subset of the articles currently
8492 fetched for this group."
8493   (interactive)
8494   (unless gnus-newsgroup-dormant
8495     (error "There are no dormant articles in this group"))
8496   (prog1
8497       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8498     (gnus-summary-position-point)))
8499
8500 (defun gnus-summary-limit-exclude-dormant ()
8501   "Hide all dormant articles."
8502   (interactive)
8503   (prog1
8504       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8505     (gnus-summary-position-point)))
8506
8507 (defun gnus-summary-limit-exclude-childless-dormant ()
8508   "Hide all dormant articles that have no children."
8509   (interactive)
8510   (let ((data (gnus-data-list t))
8511         articles d children)
8512     ;; Find all articles that are either not dormant or have
8513     ;; children.
8514     (while (setq d (pop data))
8515       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8516                 (and (setq children
8517                            (gnus-article-children (gnus-data-number d)))
8518                      (let (found)
8519                        (while children
8520                          (when (memq (car children) articles)
8521                            (setq children nil
8522                                  found t))
8523                          (pop children))
8524                        found)))
8525         (push (gnus-data-number d) articles)))
8526     ;; Do the limiting.
8527     (prog1
8528         (gnus-summary-limit articles)
8529       (gnus-summary-position-point))))
8530
8531 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8532   "Mark all unread excluded articles as read.
8533 If ALL, mark even excluded ticked and dormants as read."
8534   (interactive "P")
8535   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8536   (let ((articles (gnus-sorted-ndifference
8537                    (sort
8538                     (mapcar (lambda (h) (mail-header-number h))
8539                             gnus-newsgroup-headers)
8540                     '<)
8541                    gnus-newsgroup-limit))
8542         article)
8543     (setq gnus-newsgroup-unreads
8544           (gnus-sorted-intersection gnus-newsgroup-unreads
8545                                     gnus-newsgroup-limit))
8546     (if all
8547         (setq gnus-newsgroup-dormant nil
8548               gnus-newsgroup-marked nil
8549               gnus-newsgroup-reads
8550               (nconc
8551                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8552                gnus-newsgroup-reads))
8553       (while (setq article (pop articles))
8554         (unless (or (memq article gnus-newsgroup-dormant)
8555                     (memq article gnus-newsgroup-marked))
8556           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8557
8558 (defun gnus-summary-limit (articles &optional pop)
8559   (if pop
8560       ;; We pop the previous limit off the stack and use that.
8561       (setq articles (car gnus-newsgroup-limits)
8562             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8563     ;; We use the new limit, so we push the old limit on the stack.
8564     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8565   ;; Set the limit.
8566   (setq gnus-newsgroup-limit articles)
8567   (let ((total (length gnus-newsgroup-data))
8568         (data (gnus-data-find-list (gnus-summary-article-number)))
8569         (gnus-summary-mark-below nil)   ; Inhibit this.
8570         found)
8571     ;; This will do all the work of generating the new summary buffer
8572     ;; according to the new limit.
8573     (gnus-summary-prepare)
8574     ;; Hide any threads, possibly.
8575     (gnus-summary-maybe-hide-threads)
8576     ;; Try to return to the article you were at, or one in the
8577     ;; neighborhood.
8578     (when data
8579       ;; We try to find some article after the current one.
8580       (while data
8581         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8582           (setq data nil
8583                 found t))
8584         (setq data (cdr data))))
8585     (unless found
8586       ;; If there is no data, that means that we were after the last
8587       ;; article.  The same goes when we can't find any articles
8588       ;; after the current one.
8589       (goto-char (point-max))
8590       (gnus-summary-find-prev))
8591     (gnus-set-mode-line 'summary)
8592     ;; We return how many articles were removed from the summary
8593     ;; buffer as a result of the new limit.
8594     (- total (length gnus-newsgroup-data))))
8595
8596 (defsubst gnus-invisible-cut-children (threads)
8597   (let ((num 0))
8598     (while threads
8599       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8600         (incf num))
8601       (pop threads))
8602     (< num 2)))
8603
8604 (defsubst gnus-cut-thread (thread)
8605   "Go forwards in the thread until we find an article that we want to display."
8606   (when (or (eq gnus-fetch-old-headers 'some)
8607             (eq gnus-fetch-old-headers 'invisible)
8608             (numberp gnus-fetch-old-headers)
8609             (eq gnus-build-sparse-threads 'some)
8610             (eq gnus-build-sparse-threads 'more))
8611     ;; Deal with old-fetched headers and sparse threads.
8612     (while (and
8613             thread
8614             (or
8615              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8616              (gnus-summary-article-ancient-p
8617               (mail-header-number (car thread))))
8618             (if (or (<= (length (cdr thread)) 1)
8619                     (eq gnus-fetch-old-headers 'invisible))
8620                 (setq gnus-newsgroup-limit
8621                       (delq (mail-header-number (car thread))
8622                             gnus-newsgroup-limit)
8623                       thread (cadr thread))
8624               (when (gnus-invisible-cut-children (cdr thread))
8625                 (let ((th (cdr thread)))
8626                   (while th
8627                     (if (memq (mail-header-number (caar th))
8628                               gnus-newsgroup-limit)
8629                         (setq thread (car th)
8630                               th nil)
8631                       (setq th (cdr th))))))))))
8632   thread)
8633
8634 (defun gnus-cut-threads (threads)
8635   "Cut off all uninteresting articles from the beginning of THREADS."
8636   (when (or (eq gnus-fetch-old-headers 'some)
8637             (eq gnus-fetch-old-headers 'invisible)
8638             (numberp gnus-fetch-old-headers)
8639             (eq gnus-build-sparse-threads 'some)
8640             (eq gnus-build-sparse-threads 'more))
8641     (let ((th threads))
8642       (while th
8643         (setcar th (gnus-cut-thread (car th)))
8644         (setq th (cdr th)))))
8645   ;; Remove nixed out threads.
8646   (delq nil threads))
8647
8648 (defun gnus-summary-initial-limit (&optional show-if-empty)
8649   "Figure out what the initial limit is supposed to be on group entry.
8650 This entails weeding out unwanted dormants, low-scored articles,
8651 fetch-old-headers verbiage, and so on."
8652   ;; Most groups have nothing to remove.
8653   (unless (or gnus-inhibit-limiting
8654               (and (null gnus-newsgroup-dormant)
8655                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8656                    (not (eq gnus-fetch-old-headers 'some))
8657                    (not (numberp gnus-fetch-old-headers))
8658                    (not (eq gnus-fetch-old-headers 'invisible))
8659                    (null gnus-summary-expunge-below)
8660                    (not (eq gnus-build-sparse-threads 'some))
8661                    (not (eq gnus-build-sparse-threads 'more))
8662                    (null gnus-thread-expunge-below)
8663                    (not gnus-use-nocem)))
8664     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8665     (setq gnus-newsgroup-limit nil)
8666     (mapatoms
8667      (lambda (node)
8668        (unless (car (symbol-value node))
8669          ;; These threads have no parents -- they are roots.
8670          (let ((nodes (cdr (symbol-value node)))
8671                thread)
8672            (while nodes
8673              (if (and gnus-thread-expunge-below
8674                       (< (gnus-thread-total-score (car nodes))
8675                          gnus-thread-expunge-below))
8676                  (gnus-expunge-thread (pop nodes))
8677                (setq thread (pop nodes))
8678                (gnus-summary-limit-children thread))))))
8679      gnus-newsgroup-dependencies)
8680     ;; If this limitation resulted in an empty group, we might
8681     ;; pop the previous limit and use it instead.
8682     (when (and (not gnus-newsgroup-limit)
8683                show-if-empty)
8684       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8685     gnus-newsgroup-limit))
8686
8687 (defun gnus-summary-limit-children (thread)
8688   "Return 1 if this subthread is visible and 0 if it is not."
8689   ;; First we get the number of visible children to this thread.  This
8690   ;; is done by recursing down the thread using this function, so this
8691   ;; will really go down to a leaf article first, before slowly
8692   ;; working its way up towards the root.
8693   (when thread
8694     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8695            (children
8696            (if (cdr thread)
8697                (apply '+ (mapcar 'gnus-summary-limit-children
8698                                  (cdr thread)))
8699              0))
8700           (number (mail-header-number (car thread)))
8701           score)
8702       (if (and
8703            (not (memq number gnus-newsgroup-marked))
8704            (or
8705             ;; If this article is dormant and has absolutely no visible
8706             ;; children, then this article isn't visible.
8707             (and (memq number gnus-newsgroup-dormant)
8708                  (zerop children))
8709             ;; If this is "fetch-old-headered" and there is no
8710             ;; visible children, then we don't want this article.
8711             (and (or (eq gnus-fetch-old-headers 'some)
8712                      (numberp gnus-fetch-old-headers))
8713                  (gnus-summary-article-ancient-p number)
8714                  (zerop children))
8715             ;; If this is "fetch-old-headered" and `invisible', then
8716             ;; we don't want this article.
8717             (and (eq gnus-fetch-old-headers 'invisible)
8718                  (gnus-summary-article-ancient-p number))
8719             ;; If this is a sparsely inserted article with no children,
8720             ;; we don't want it.
8721             (and (eq gnus-build-sparse-threads 'some)
8722                  (gnus-summary-article-sparse-p number)
8723                  (zerop children))
8724             ;; If we use expunging, and this article is really
8725             ;; low-scored, then we don't want this article.
8726             (when (and gnus-summary-expunge-below
8727                        (< (setq score
8728                                 (or (cdr (assq number gnus-newsgroup-scored))
8729                                     gnus-summary-default-score))
8730                           gnus-summary-expunge-below))
8731               ;; We increase the expunge-tally here, but that has
8732               ;; nothing to do with the limits, really.
8733               (incf gnus-newsgroup-expunged-tally)
8734               ;; We also mark as read here, if that's wanted.
8735               (when (and gnus-summary-mark-below
8736                          (< score gnus-summary-mark-below))
8737                 (setq gnus-newsgroup-unreads
8738                       (delq number gnus-newsgroup-unreads))
8739                 (if gnus-newsgroup-auto-expire
8740                     (push number gnus-newsgroup-expirable)
8741                   (push (cons number gnus-low-score-mark)
8742                         gnus-newsgroup-reads)))
8743               t)
8744             ;; Do the `display' group parameter.
8745             (and gnus-newsgroup-display
8746                  (not (funcall gnus-newsgroup-display)))
8747             ;; Check NoCeM things.
8748             (when (and gnus-use-nocem
8749                        (gnus-nocem-unwanted-article-p
8750                         (mail-header-id (car thread))))
8751               (setq gnus-newsgroup-unreads
8752                     (delq number gnus-newsgroup-unreads))
8753               t)))
8754           ;; Nope, invisible article.
8755           0
8756         ;; Ok, this article is to be visible, so we add it to the limit
8757         ;; and return 1.
8758         (push number gnus-newsgroup-limit)
8759         1))))
8760
8761 (defun gnus-expunge-thread (thread)
8762   "Mark all articles in THREAD as read."
8763   (let* ((number (mail-header-number (car thread))))
8764     (incf gnus-newsgroup-expunged-tally)
8765     ;; We also mark as read here, if that's wanted.
8766     (setq gnus-newsgroup-unreads
8767           (delq number gnus-newsgroup-unreads))
8768     (if gnus-newsgroup-auto-expire
8769         (push number gnus-newsgroup-expirable)
8770       (push (cons number gnus-low-score-mark)
8771             gnus-newsgroup-reads)))
8772   ;; Go recursively through all subthreads.
8773   (mapcar 'gnus-expunge-thread (cdr thread)))
8774
8775 ;; Summary article oriented commands
8776
8777 (defun gnus-summary-refer-parent-article (n)
8778   "Refer parent article N times.
8779 If N is negative, go to ancestor -N instead.
8780 The difference between N and the number of articles fetched is returned."
8781   (interactive "p")
8782   (let ((skip 1)
8783         error header ref)
8784     (when (not (natnump n))
8785       (setq skip (abs n)
8786             n 1))
8787     (while (and (> n 0)
8788                 (not error))
8789       (setq header (gnus-summary-article-header))
8790       (if (and (eq (mail-header-number header)
8791                    (cdr gnus-article-current))
8792                (equal gnus-newsgroup-name
8793                       (car gnus-article-current)))
8794           ;; If we try to find the parent of the currently
8795           ;; displayed article, then we take a look at the actual
8796           ;; References header, since this is slightly more
8797           ;; reliable than the References field we got from the
8798           ;; server.
8799           (with-current-buffer gnus-original-article-buffer
8800             (nnheader-narrow-to-headers)
8801             (unless (setq ref (message-fetch-field "references"))
8802               (when (setq ref (message-fetch-field "in-reply-to"))
8803                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8804             (widen))
8805         (setq ref
8806               ;; It's not the current article, so we take a bet on
8807               ;; the value we got from the server.
8808               (mail-header-references header)))
8809       (if (and ref
8810                (not (equal ref "")))
8811           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8812             (gnus-message 1 "Couldn't find parent"))
8813         (gnus-message 1 "No references in article %d"
8814                       (gnus-summary-article-number))
8815         (setq error t))
8816       (decf n))
8817     (gnus-summary-position-point)
8818     n))
8819
8820 (defun gnus-summary-refer-references ()
8821   "Fetch all articles mentioned in the References header.
8822 Return the number of articles fetched."
8823   (interactive)
8824   (let ((ref (mail-header-references (gnus-summary-article-header)))
8825         (current (gnus-summary-article-number))
8826         (n 0))
8827     (if (or (not ref)
8828             (equal ref ""))
8829         (error "No References in the current article")
8830       ;; For each Message-ID in the References header...
8831       (while (string-match "<[^>]*>" ref)
8832         (incf n)
8833         ;; ... fetch that article.
8834         (gnus-summary-refer-article
8835          (prog1 (match-string 0 ref)
8836            (setq ref (substring ref (match-end 0))))))
8837       (gnus-summary-goto-subject current)
8838       (gnus-summary-position-point)
8839       n)))
8840
8841 (defun gnus-summary-refer-thread (&optional limit)
8842   "Fetch all articles in the current thread.
8843 If LIMIT (the numerical prefix), fetch that many old headers instead
8844 of what's specified by the `gnus-refer-thread-limit' variable."
8845   (interactive "P")
8846   (let ((id (mail-header-id (gnus-summary-article-header)))
8847         (limit (if limit (prefix-numeric-value limit)
8848                  gnus-refer-thread-limit)))
8849     (unless (eq gnus-fetch-old-headers 'invisible)
8850       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8851       ;; Retrieve the headers and read them in.
8852       (if (eq (if (numberp limit)
8853                   (gnus-retrieve-headers
8854                    (list (min
8855                           (+ (mail-header-number
8856                               (gnus-summary-article-header))
8857                              limit)
8858                           gnus-newsgroup-end))
8859                    gnus-newsgroup-name (* limit 2))
8860                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8861                 ;; headers.
8862                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8863                                        gnus-newsgroup-name limit))
8864               'nov)
8865           (gnus-build-all-threads)
8866         (error "Can't fetch thread from back ends that don't support NOV"))
8867       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8868     (gnus-summary-limit-include-thread id)))
8869
8870 (defun gnus-summary-refer-article (message-id)
8871   "Fetch an article specified by MESSAGE-ID."
8872   (interactive "sMessage-ID: ")
8873   (when (and (stringp message-id)
8874              (not (zerop (length message-id))))
8875     (setq message-id (gnus-replace-in-string message-id " " ""))
8876     ;; Construct the correct Message-ID if necessary.
8877     ;; Suggested by tale@pawl.rpi.edu.
8878     (unless (string-match "^<" message-id)
8879       (setq message-id (concat "<" message-id)))
8880     (unless (string-match ">$" message-id)
8881       (setq message-id (concat message-id ">")))
8882     ;; People often post MIDs from URLs, so unhex it:
8883     (unless (string-match "@" message-id)
8884       (setq message-id (gnus-url-unhex-string message-id)))
8885     (let* ((header (gnus-id-to-header message-id))
8886            (sparse (and header
8887                         (gnus-summary-article-sparse-p
8888                          (mail-header-number header))
8889                         (memq (mail-header-number header)
8890                               gnus-newsgroup-limit)))
8891            number)
8892       (cond
8893        ;; If the article is present in the buffer we just go to it.
8894        ((and header
8895              (or (not (gnus-summary-article-sparse-p
8896                        (mail-header-number header)))
8897                  sparse))
8898         (prog1
8899             (gnus-summary-goto-article
8900              (mail-header-number header) nil t)
8901           (when sparse
8902             (gnus-summary-update-article (mail-header-number header)))))
8903        (t
8904         ;; We fetch the article.
8905         (catch 'found
8906           (dolist (gnus-override-method (gnus-refer-article-methods))
8907             (when (and (gnus-check-server gnus-override-method)
8908                        ;; Fetch the header,
8909                        (setq number (gnus-summary-insert-subject message-id)))
8910               ;; and display the article.
8911               (gnus-summary-select-article nil nil nil number)
8912               (throw 'found t)))
8913           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8914
8915 (defun gnus-refer-article-methods ()
8916   "Return a list of referable methods."
8917   (cond
8918    ;; No method, so we default to current and native.
8919    ((null gnus-refer-article-method)
8920     (list gnus-current-select-method gnus-select-method))
8921    ;; Current.
8922    ((eq 'current gnus-refer-article-method)
8923     (list gnus-current-select-method))
8924    ;; List of select methods.
8925    ((not (and (symbolp (car gnus-refer-article-method))
8926               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8927     (let (out)
8928       (dolist (method gnus-refer-article-method)
8929         (push (if (eq 'current method)
8930                   gnus-current-select-method
8931                 method)
8932               out))
8933       (nreverse out)))
8934    ;; One single select method.
8935    (t
8936     (list gnus-refer-article-method))))
8937
8938 (defun gnus-summary-edit-parameters ()
8939   "Edit the group parameters of the current group."
8940   (interactive)
8941   (gnus-group-edit-group gnus-newsgroup-name 'params))
8942
8943 (defun gnus-summary-customize-parameters ()
8944   "Customize the group parameters of the current group."
8945   (interactive)
8946   (gnus-group-customize gnus-newsgroup-name))
8947
8948 (defun gnus-summary-enter-digest-group (&optional force)
8949   "Enter an nndoc group based on the current article.
8950 If FORCE, force a digest interpretation.  If not, try
8951 to guess what the document format is."
8952   (interactive "P")
8953   (let ((conf gnus-current-window-configuration))
8954     (save-window-excursion
8955       (save-excursion
8956         (let (gnus-article-prepare-hook
8957               gnus-display-mime-function
8958               gnus-break-pages)
8959           (gnus-summary-select-article))))
8960     (setq gnus-current-window-configuration conf)
8961     (let* ((name (format "%s-%d"
8962                          (gnus-group-prefixed-name
8963                           gnus-newsgroup-name (list 'nndoc ""))
8964                          (with-current-buffer gnus-summary-buffer
8965                            gnus-current-article)))
8966            (ogroup gnus-newsgroup-name)
8967            (params (append (gnus-info-params (gnus-get-info ogroup))
8968                            (list (cons 'to-group ogroup))
8969                            (list (cons 'parent-group ogroup))
8970                            (list (cons 'save-article-group ogroup))))
8971            (case-fold-search t)
8972            (buf (current-buffer))
8973            dig to-address)
8974       (with-current-buffer gnus-original-article-buffer
8975         ;; Have the digest group inherit the main mail address of
8976         ;; the parent article.
8977         (when (setq to-address (or (gnus-fetch-field "reply-to")
8978                                    (gnus-fetch-field "from")))
8979           (setq params
8980                 (append
8981                  (list (cons 'to-address
8982                              (funcall gnus-decode-encoded-address-function
8983                                       to-address))))))
8984         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8985         (insert-buffer-substring gnus-original-article-buffer)
8986         ;; Remove lines that may lead nndoc to misinterpret the
8987         ;; document type.
8988         (narrow-to-region
8989          (goto-char (point-min))
8990          (or (search-forward "\n\n" nil t) (point)))
8991         (goto-char (point-min))
8992         (delete-matching-lines "^Path:\\|^From ")
8993         (widen))
8994       (unwind-protect
8995           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8996                     (gnus-newsgroup-ephemeral-ignored-charsets
8997                      gnus-newsgroup-ignored-charsets))
8998                 (gnus-group-read-ephemeral-group
8999                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9000                               (nndoc-article-type
9001                                ,(if force 'mbox 'guess)))
9002                  t nil nil nil
9003                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9004                                                         "ADAPT")))))
9005               ;; Make all postings to this group go to the parent group.
9006               (nconc (gnus-info-params (gnus-get-info name))
9007                      params)
9008             ;; Couldn't select this doc group.
9009             (switch-to-buffer buf)
9010             (gnus-set-global-variables)
9011             (gnus-configure-windows 'summary)
9012             (gnus-message 3 "Article couldn't be entered?"))
9013         (kill-buffer dig)))))
9014
9015 (defun gnus-summary-read-document (n)
9016   "Open a new group based on the current article(s).
9017 This will allow you to read digests and other similar
9018 documents as newsgroups.
9019 Obeys the standard process/prefix convention."
9020   (interactive "P")
9021   (let* ((ogroup gnus-newsgroup-name)
9022          (params (append (gnus-info-params (gnus-get-info ogroup))
9023                          (list (cons 'to-group ogroup))))
9024          group egroup groups vgroup)
9025     (dolist (article (gnus-summary-work-articles n))
9026       (setq group (format "%s-%d" gnus-newsgroup-name article))
9027       (gnus-summary-remove-process-mark article)
9028       (when (gnus-summary-display-article article)
9029         (save-excursion
9030           (with-temp-buffer
9031             (insert-buffer-substring gnus-original-article-buffer)
9032             ;; Remove some headers that may lead nndoc to make
9033             ;; the wrong guess.
9034             (message-narrow-to-head)
9035             (goto-char (point-min))
9036             (delete-matching-lines "^Path:\\|^From ")
9037             (widen)
9038             (if (setq egroup
9039                       (gnus-group-read-ephemeral-group
9040                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9041                                      (nndoc-article-type guess))
9042                        t nil t))
9043                 (progn
9044                   ;; Make all postings to this group go to the parent group.
9045                   (nconc (gnus-info-params (gnus-get-info egroup))
9046                          params)
9047                   (push egroup groups))
9048               ;; Couldn't select this doc group.
9049               (gnus-error 3 "Article couldn't be entered"))))))
9050     ;; Now we have selected all the documents.
9051     (cond
9052      ((not groups)
9053       (error "None of the articles could be interpreted as documents"))
9054      ((gnus-group-read-ephemeral-group
9055        (setq vgroup (format
9056                      "nnvirtual:%s-%s" gnus-newsgroup-name
9057                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9058        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9059        t
9060        (cons (current-buffer) 'summary)))
9061      (t
9062       (error "Couldn't select virtual nndoc group")))))
9063
9064 (defun gnus-summary-isearch-article (&optional regexp-p)
9065   "Do incremental search forward on the current article.
9066 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9067   (interactive "P")
9068   (gnus-summary-select-article)
9069   (gnus-configure-windows 'article)
9070   (gnus-eval-in-buffer-window gnus-article-buffer
9071     (save-restriction
9072       (widen)
9073       (isearch-forward regexp-p))))
9074
9075 (defun gnus-summary-repeat-search-article-forward ()
9076   "Repeat the previous search forwards."
9077   (interactive)
9078   (unless gnus-last-search-regexp
9079     (error "No previous search"))
9080   (gnus-summary-search-article-forward gnus-last-search-regexp))
9081
9082 (defun gnus-summary-repeat-search-article-backward ()
9083   "Repeat the previous search backwards."
9084   (interactive)
9085   (unless gnus-last-search-regexp
9086     (error "No previous search"))
9087   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9088
9089 (defun gnus-summary-search-article-forward (regexp &optional backward)
9090   "Search for an article containing REGEXP forward.
9091 If BACKWARD, search backward instead."
9092   (interactive
9093    (list (read-string
9094           (format "Search article %s (regexp%s): "
9095                   (if current-prefix-arg "backward" "forward")
9096                   (if gnus-last-search-regexp
9097                       (concat ", default " gnus-last-search-regexp)
9098                     "")))
9099          current-prefix-arg))
9100   (if (string-equal regexp "")
9101       (setq regexp (or gnus-last-search-regexp ""))
9102     (setq gnus-last-search-regexp regexp)
9103     (setq gnus-article-before-search gnus-current-article))
9104   ;; Intentionally set gnus-last-article.
9105   (setq gnus-last-article gnus-article-before-search)
9106   (let ((gnus-last-article gnus-last-article))
9107     (if (gnus-summary-search-article regexp backward)
9108         (gnus-summary-show-thread)
9109       (signal 'search-failed (list regexp)))))
9110
9111 (defun gnus-summary-search-article-backward (regexp)
9112   "Search for an article containing REGEXP backward."
9113   (interactive
9114    (list (read-string
9115           (format "Search article backward (regexp%s): "
9116                   (if gnus-last-search-regexp
9117                       (concat ", default " gnus-last-search-regexp)
9118                     "")))))
9119   (gnus-summary-search-article-forward regexp 'backward))
9120
9121 (defun gnus-summary-search-article (regexp &optional backward)
9122   "Search for an article containing REGEXP.
9123 Optional argument BACKWARD means do search for backward.
9124 `gnus-select-article-hook' is not called during the search."
9125   ;; We have to require this here to make sure that the following
9126   ;; dynamic binding isn't shadowed by autoloading.
9127   (require 'gnus-async)
9128   (require 'gnus-art)
9129   (let ((gnus-select-article-hook nil)  ;Disable hook.
9130         (gnus-article-prepare-hook nil)
9131         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9132         (gnus-use-article-prefetch nil)
9133         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9134         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9135         (gnus-visual nil)
9136         (gnus-keep-backlog nil)
9137         (gnus-break-pages nil)
9138         (gnus-summary-display-arrow nil)
9139         (gnus-updated-mode-lines nil)
9140         (gnus-auto-center-summary nil)
9141         (sum (current-buffer))
9142         (gnus-display-mime-function nil)
9143         (found nil)
9144         point)
9145     (gnus-save-hidden-threads
9146       (gnus-summary-select-article)
9147       (set-buffer gnus-article-buffer)
9148       (goto-char (window-point (get-buffer-window (current-buffer))))
9149       (when backward
9150         (forward-line -1))
9151       (while (not found)
9152         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9153         (if (if backward
9154                 (re-search-backward regexp nil t)
9155               (re-search-forward regexp nil t))
9156             ;; We found the regexp.
9157             (progn
9158               (setq found 'found)
9159               (beginning-of-line)
9160               (set-window-start
9161                (get-buffer-window (current-buffer))
9162                (point))
9163               (forward-line 1)
9164               (set-window-point
9165                (get-buffer-window (current-buffer))
9166                (point))
9167               (set-buffer sum)
9168               (setq point (point)))
9169           ;; We didn't find it, so we go to the next article.
9170           (set-buffer sum)
9171           (setq found 'not)
9172           (while (eq found 'not)
9173             (if (not (if backward (gnus-summary-find-prev)
9174                        (gnus-summary-find-next)))
9175                 ;; No more articles.
9176                 (setq found t)
9177               ;; Select the next article and adjust point.
9178               (unless (gnus-summary-article-sparse-p
9179                        (gnus-summary-article-number))
9180                 (setq found nil)
9181                 (gnus-summary-select-article)
9182                 (set-buffer gnus-article-buffer)
9183                 (widen)
9184                 (goto-char (if backward (point-max) (point-min))))))))
9185       (gnus-message 7 ""))
9186     ;; Return whether we found the regexp.
9187     (when (eq found 'found)
9188       (goto-char point)
9189       (gnus-summary-show-thread)
9190       (gnus-summary-goto-subject gnus-current-article)
9191       (gnus-summary-position-point)
9192       t)))
9193
9194 (defun gnus-find-matching-articles (header regexp)
9195   "Return a list of all articles that match REGEXP on HEADER.
9196 This search includes all articles in the current group that Gnus has
9197 fetched headers for, whether they are displayed or not."
9198   (let ((articles nil)
9199         ;; Can't eta-reduce because it's a macro.
9200         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9201         (case-fold-search t))
9202     (dolist (header gnus-newsgroup-headers)
9203       (when (string-match regexp (funcall func header))
9204         (push (mail-header-number header) articles)))
9205     (nreverse articles)))
9206
9207 (defun gnus-summary-find-matching (header regexp &optional backward unread
9208                                           not-case-fold not-matching)
9209   "Return a list of all articles that match REGEXP on HEADER.
9210 The search stars on the current article and goes forwards unless
9211 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9212 If UNREAD is non-nil, only unread articles will
9213 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9214 in the comparisons. If NOT-MATCHING, return a list of all articles that
9215 not match REGEXP on HEADER."
9216   (let ((case-fold-search (not not-case-fold))
9217         articles d func)
9218     (if (consp header)
9219         (if (eq (car header) 'extra)
9220             (setq func
9221                   `(lambda (h)
9222                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9223                          "")))
9224           (error "%s is an invalid header" header))
9225       (unless (fboundp (intern (concat "mail-header-" header)))
9226         (error "%s is not a valid header" header))
9227       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9228     (dolist (d (if (eq backward 'all)
9229                    gnus-newsgroup-data
9230                  (gnus-data-find-list
9231                   (gnus-summary-article-number)
9232                   (gnus-data-list backward))))
9233       (when (and (or (not unread)       ; We want all articles...
9234                      (gnus-data-unread-p d)) ; Or just unreads.
9235                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9236                  (if not-matching
9237                      (not (string-match
9238                            regexp
9239                            (funcall func (gnus-data-header d))))
9240                    (string-match regexp
9241                                  (funcall func (gnus-data-header d)))))
9242         (push (gnus-data-number d) articles))) ; Success!
9243     (nreverse articles)))
9244
9245 (defun gnus-summary-execute-command (header regexp command &optional backward)
9246   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9247 If HEADER is an empty string (or nil), the match is done on the entire
9248 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9249   (interactive
9250    (list (let ((completion-ignore-case t))
9251            (completing-read
9252             "Header name: "
9253             (mapcar (lambda (header) (list (format "%s" header)))
9254                     (append
9255                      '("Number" "Subject" "From" "Lines" "Date"
9256                        "Message-ID" "Xref" "References" "Body")
9257                      gnus-extra-headers))
9258             nil 'require-match))
9259          (read-string "Regexp: ")
9260          (read-key-sequence "Command: ")
9261          current-prefix-arg))
9262   (when (equal header "Body")
9263     (setq header ""))
9264   ;; Hidden thread subtrees must be searched as well.
9265   (gnus-summary-show-all-threads)
9266   ;; We don't want to change current point nor window configuration.
9267   (save-excursion
9268     (save-window-excursion
9269       (let (gnus-visual
9270             gnus-treat-strip-trailing-blank-lines
9271             gnus-treat-strip-leading-blank-lines
9272             gnus-treat-strip-multiple-blank-lines
9273             gnus-treat-hide-boring-headers
9274             gnus-treat-fold-newsgroups
9275             gnus-article-prepare-hook)
9276         (gnus-message 6 "Executing %s..." (key-description command))
9277         ;; We'd like to execute COMMAND interactively so as to give arguments.
9278         (gnus-execute header regexp
9279                       `(call-interactively ',(key-binding command))
9280                       backward)
9281         (gnus-message 6 "Executing %s...done" (key-description command))))))
9282
9283 (defun gnus-summary-beginning-of-article ()
9284   "Scroll the article back to the beginning."
9285   (interactive)
9286   (gnus-summary-select-article)
9287   (gnus-configure-windows 'article)
9288   (gnus-eval-in-buffer-window gnus-article-buffer
9289     (widen)
9290     (goto-char (point-min))
9291     (when gnus-break-pages
9292       (gnus-narrow-to-page))))
9293
9294 (defun gnus-summary-end-of-article ()
9295   "Scroll to the end of the article."
9296   (interactive)
9297   (gnus-summary-select-article)
9298   (gnus-configure-windows 'article)
9299   (gnus-eval-in-buffer-window gnus-article-buffer
9300     (widen)
9301     (goto-char (point-max))
9302     (recenter -3)
9303     (when gnus-break-pages
9304       (gnus-narrow-to-page))))
9305
9306 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9307   "Truncate to LEN and quote all \"(\"'s in STRING."
9308   (gnus-replace-in-string (if (and len (> (length string) len))
9309                               (substring string 0 len)
9310                             string)
9311                           "[()]" "\\\\\\&"))
9312
9313 (defun gnus-summary-print-article (&optional filename n)
9314   "Generate and print a PostScript image of the process-marked (mail) articles.
9315
9316 If used interactively, print the current article if none are
9317 process-marked.  With prefix arg, prompt the user for the name of the
9318 file to save in.
9319
9320 When used from Lisp, accept two optional args FILENAME and N.  N means
9321 to print the next N articles.  If N is negative, print the N previous
9322 articles.  If N is nil and articles have been marked with the process
9323 mark, print these instead.
9324
9325 If the optional first argument FILENAME is nil, send the image to the
9326 printer.  If FILENAME is a string, save the PostScript image in a file with
9327 that name.  If FILENAME is a number, prompt the user for the name of the file
9328 to save in."
9329   (interactive (list (ps-print-preprint current-prefix-arg)))
9330   (dolist (article (gnus-summary-work-articles n))
9331     (gnus-summary-select-article nil nil 'pseudo article)
9332     (gnus-eval-in-buffer-window gnus-article-buffer
9333       (gnus-print-buffer))
9334     (gnus-summary-remove-process-mark article))
9335   (ps-despool filename))
9336
9337 (defun gnus-print-buffer ()
9338   (let ((buffer (generate-new-buffer " *print*")))
9339     (unwind-protect
9340         (progn
9341           (copy-to-buffer buffer (point-min) (point-max))
9342           (set-buffer buffer)
9343           (gnus-remove-text-with-property 'gnus-decoration)
9344           (when (gnus-visual-p 'article-highlight 'highlight)
9345             ;; Copy-to-buffer doesn't copy overlay.  So redo
9346             ;; highlight.
9347             (let ((gnus-article-buffer buffer))
9348               (gnus-article-highlight-citation t)
9349               (gnus-article-highlight-signature)
9350               (gnus-article-emphasize)
9351               (gnus-article-delete-invisible-text)))
9352           (let ((ps-left-header
9353                  (list
9354                   (concat "("
9355                           (gnus-summary-print-truncate-and-quote
9356                            (mail-header-subject gnus-current-headers)
9357                            66) ")")
9358                   (concat "("
9359                           (gnus-summary-print-truncate-and-quote
9360                            (mail-header-from gnus-current-headers)
9361                            45) ")")))
9362                 (ps-right-header
9363                  (list
9364                   "/pagenumberstring load"
9365                   (concat "("
9366                           (mail-header-date gnus-current-headers) ")"))))
9367             (gnus-run-hooks 'gnus-ps-print-hook)
9368             (save-excursion
9369               (if ps-print-color-p
9370                   (ps-spool-buffer-with-faces)
9371                 (ps-spool-buffer)))))
9372       (kill-buffer buffer))))
9373
9374 (defun gnus-summary-show-article (&optional arg)
9375   "Force redisplaying of the current article.
9376 If ARG (the prefix) is a number, show the article with the charset
9377 defined in `gnus-summary-show-article-charset-alist', or the charset
9378 input.
9379 If ARG (the prefix) is non-nil and not a number, show the raw article
9380 without any article massaging functions being run.  Normally, the key
9381 strokes are `C-u g'."
9382   (interactive "P")
9383   (cond
9384    ((numberp arg)
9385     (gnus-summary-show-article t)
9386     (let ((gnus-newsgroup-charset
9387            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9388                (mm-read-coding-system
9389                 "View as charset: " ;; actually it is coding system.
9390                 (with-current-buffer gnus-article-buffer
9391                   (mm-detect-coding-region (point) (point-max))))))
9392           (gnus-newsgroup-ignored-charsets 'gnus-all))
9393       (gnus-summary-select-article nil 'force)
9394       (let ((deps gnus-newsgroup-dependencies)
9395             head header lines)
9396         (with-current-buffer gnus-original-article-buffer
9397           (save-restriction
9398             (message-narrow-to-head)
9399             (setq head (buffer-string))
9400             (goto-char (point-min))
9401             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9402               (goto-char (point-max))
9403               (widen)
9404               (setq lines (1- (count-lines (point) (point-max))))))
9405           (with-temp-buffer
9406             (insert (format "211 %d Article retrieved.\n"
9407                             (cdr gnus-article-current)))
9408             (insert head)
9409             (if lines (insert (format "Lines: %d\n" lines)))
9410             (insert ".\n")
9411             (let ((nntp-server-buffer (current-buffer)))
9412               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9413         (gnus-data-set-header
9414          (gnus-data-find (cdr gnus-article-current))
9415          header)
9416         (gnus-summary-update-article-line
9417          (cdr gnus-article-current) header)
9418         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9419           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9420    ((not arg)
9421     ;; Select the article the normal way.
9422     (gnus-summary-select-article nil 'force))
9423    (t
9424     ;; We have to require this here to make sure that the following
9425     ;; dynamic binding isn't shadowed by autoloading.
9426     (require 'gnus-async)
9427     (require 'gnus-art)
9428     ;; Bind the article treatment functions to nil.
9429     (let ((gnus-have-all-headers t)
9430           gnus-article-prepare-hook
9431           gnus-article-decode-hook
9432           gnus-display-mime-function
9433           gnus-break-pages)
9434       ;; Destroy any MIME parts.
9435       (when (gnus-buffer-live-p gnus-article-buffer)
9436         (with-current-buffer gnus-article-buffer
9437           (mm-destroy-parts gnus-article-mime-handles)
9438           ;; Set it to nil for safety reason.
9439           (setq gnus-article-mime-handle-alist nil)
9440           (setq gnus-article-mime-handles nil)))
9441       (gnus-summary-select-article nil 'force))))
9442   (gnus-summary-goto-subject gnus-current-article)
9443   (gnus-summary-position-point))
9444
9445 (defun gnus-summary-show-raw-article ()
9446   "Show the raw article without any article massaging functions being run."
9447   (interactive)
9448   (gnus-summary-show-article t))
9449
9450 (defun gnus-summary-verbose-headers (&optional arg)
9451   "Toggle permanent full header display.
9452 If ARG is a positive number, turn header display on.
9453 If ARG is a negative number, turn header display off."
9454   (interactive "P")
9455   (setq gnus-show-all-headers
9456         (cond ((or (not (numberp arg))
9457                    (zerop arg))
9458                (not gnus-show-all-headers))
9459               ((natnump arg)
9460                t)))
9461   (gnus-summary-show-article))
9462
9463 (defun gnus-summary-toggle-header (&optional arg)
9464   "Show the headers if they are hidden, or hide them if they are shown.
9465 If ARG is a positive number, show the entire header.
9466 If ARG is a negative number, hide the unwanted header lines."
9467   (interactive "P")
9468   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9469                      (get-buffer-window gnus-article-buffer t))))
9470     (with-current-buffer gnus-article-buffer
9471       (widen)
9472       (article-narrow-to-head)
9473       (let* ((inhibit-read-only t)
9474              (inhibit-point-motion-hooks t)
9475              (hidden (if (numberp arg)
9476                          (>= arg 0)
9477                        (or (not (looking-at "[^ \t\n]+:"))
9478                            (gnus-article-hidden-text-p 'headers))))
9479              s e)
9480         (delete-region (point-min) (point-max))
9481         (with-current-buffer gnus-original-article-buffer
9482           (goto-char (setq s (point-min)))
9483           (setq e (if (search-forward "\n\n" nil t)
9484                       (1- (point))
9485                     (point-max))))
9486         (insert-buffer-substring gnus-original-article-buffer s e)
9487         (run-hooks 'gnus-article-decode-hook)
9488         (if hidden
9489             (let ((gnus-treat-hide-headers nil)
9490                   (gnus-treat-hide-boring-headers nil))
9491               (gnus-delete-wash-type 'headers)
9492               (gnus-treat-article 'head))
9493           (gnus-treat-article 'head))
9494         (widen)
9495         (if window
9496             (set-window-start window (goto-char (point-min))))
9497         (if gnus-break-pages
9498             (gnus-narrow-to-page)
9499           (when (gnus-visual-p 'page-marker)
9500             (let ((inhibit-read-only t))
9501               (gnus-remove-text-with-property 'gnus-prev)
9502               (gnus-remove-text-with-property 'gnus-next))))
9503         (gnus-set-mode-line 'article)))))
9504
9505 (defun gnus-summary-show-all-headers ()
9506   "Make all header lines visible."
9507   (interactive)
9508   (gnus-summary-toggle-header 1))
9509
9510 (defun gnus-summary-caesar-message (&optional arg)
9511   "Caesar rotate the current article by 13.
9512 With a non-numerical prefix, also rotate headers.  A numerical
9513 prefix specifies how many places to rotate each letter forward."
9514   (interactive "P")
9515   (gnus-summary-select-article)
9516   (let ((mail-header-separator ""))
9517     (gnus-eval-in-buffer-window gnus-article-buffer
9518       (save-restriction
9519         (widen)
9520         (let ((start (window-start))
9521               (inhibit-read-only t))
9522           (if (equal arg '(4))
9523               (message-caesar-buffer-body nil t)
9524             (message-caesar-buffer-body arg))
9525           (set-window-start (get-buffer-window (current-buffer)) start)))))
9526   ;; Create buttons and stuff...
9527   (gnus-treat-article nil))
9528
9529 (declare-function idna-to-unicode "ext:idna" (str))
9530
9531 (defun gnus-summary-idna-message (&optional arg)
9532   "Decode IDNA encoded domain names in the current articles.
9533 IDNA encoded domain names looks like `xn--bar'.  If a string
9534 remain unencoded after running this function, it is likely an
9535 invalid IDNA string (`xn--bar' is invalid).
9536
9537 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9538 installed for this command to work."
9539   (interactive "P")
9540   (if (not (and (condition-case nil (require 'idna)
9541                   (file-error))
9542                 (mm-coding-system-p 'utf-8)
9543                 (executable-find (symbol-value 'idna-program))))
9544       (gnus-message
9545        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9546     (gnus-summary-select-article)
9547     (let ((mail-header-separator ""))
9548       (gnus-eval-in-buffer-window gnus-article-buffer
9549         (save-restriction
9550           (widen)
9551           (let ((start (window-start))
9552                 buffer-read-only)
9553             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9554               (replace-match (idna-to-unicode (match-string 1))))
9555             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9556
9557 (defun gnus-summary-morse-message (&optional arg)
9558   "Morse decode the current article."
9559   (interactive "P")
9560   (gnus-summary-select-article)
9561   (let ((mail-header-separator ""))
9562     (gnus-eval-in-buffer-window gnus-article-buffer
9563       (save-excursion
9564         (save-restriction
9565           (widen)
9566           (let ((pos (window-start))
9567                 (inhibit-read-only t))
9568             (goto-char (point-min))
9569             (when (message-goto-body)
9570               (gnus-narrow-to-body))
9571             (goto-char (point-min))
9572             (while (search-forward "·" (point-max) t)
9573               (replace-match "."))
9574             (unmorse-region (point-min) (point-max))
9575             (widen)
9576             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9577
9578 (defun gnus-summary-stop-page-breaking ()
9579   "Stop page breaking in the current article."
9580   (interactive)
9581   (gnus-summary-select-article)
9582   (gnus-eval-in-buffer-window gnus-article-buffer
9583     (widen)
9584     (when (gnus-visual-p 'page-marker)
9585       (let ((inhibit-read-only t))
9586         (gnus-remove-text-with-property 'gnus-prev)
9587         (gnus-remove-text-with-property 'gnus-next))
9588       (setq gnus-page-broken nil))))
9589
9590 (defun gnus-summary-move-article (&optional n to-newsgroup
9591                                             select-method action)
9592   "Move the current article to a different newsgroup.
9593 If N is a positive number, move the N next articles.
9594 If N is a negative number, move the N previous articles.
9595 If N is nil and any articles have been marked with the process mark,
9596 move those articles instead.
9597 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9598 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9599 re-spool using this method.
9600
9601 When called interactively with TO-NEWSGROUP being nil, the value of
9602 the variable `gnus-move-split-methods' is used for finding a default
9603 for the target newsgroup.
9604
9605 For this function to work, both the current newsgroup and the
9606 newsgroup that you want to move to have to support the `request-move'
9607 and `request-accept' functions.
9608
9609 ACTION can be either `move' (the default), `crosspost' or `copy'."
9610   (interactive "P")
9611   (unless action
9612     (setq action 'move))
9613   ;; Check whether the source group supports the required functions.
9614   (cond ((and (eq action 'move)
9615               (not (gnus-check-backend-function
9616                     'request-move-article gnus-newsgroup-name)))
9617          (error "The current group does not support article moving"))
9618         ((and (eq action 'crosspost)
9619               (not (gnus-check-backend-function
9620                     'request-replace-article gnus-newsgroup-name)))
9621          (error "The current group does not support article editing")))
9622   (let ((articles (gnus-summary-work-articles n))
9623         (prefix (if (gnus-check-backend-function
9624                      'request-move-article gnus-newsgroup-name)
9625                     (funcall gnus-move-group-prefix-function
9626                              gnus-newsgroup-name)
9627                   ""))
9628         (names '((move "Move" "Moving")
9629                  (copy "Copy" "Copying")
9630                  (crosspost "Crosspost" "Crossposting")))
9631         (copy-buf (save-excursion
9632                     (nnheader-set-temp-buffer " *copy article*")))
9633         art-group to-method new-xref article to-groups
9634         articles-to-update-marks encoded)
9635     (unless (assq action names)
9636       (error "Unknown action %s" action))
9637     ;; Read the newsgroup name.
9638     (when (and (not to-newsgroup)
9639                (not select-method))
9640       (if (and gnus-move-split-methods
9641                (not
9642                 (and (memq gnus-current-article articles)
9643                      (gnus-buffer-live-p gnus-original-article-buffer))))
9644           ;; When `gnus-move-split-methods' is non-nil, we have to
9645           ;; select an article to give `gnus-read-move-group-name' an
9646           ;; opportunity to suggest an appropriate default.  However,
9647           ;; we needn't render or mark the article.
9648           (let ((gnus-display-mime-function nil)
9649                 (gnus-article-prepare-hook nil)
9650                 (gnus-mark-article-hook nil))
9651             (gnus-summary-select-article nil nil nil (car articles))))
9652       (setq to-newsgroup (gnus-read-move-group-name
9653                           (cadr (assq action names))
9654                           (symbol-value
9655                            (intern (format "gnus-current-%s-group" action)))
9656                           articles prefix)
9657             encoded to-newsgroup
9658             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9659       (set (intern (format "gnus-current-%s-group" action))
9660            (mm-decode-coding-string
9661             to-newsgroup
9662             (gnus-group-name-charset to-method to-newsgroup))))
9663     (unless to-method
9664       (setq to-method (or select-method
9665                           (gnus-server-to-method
9666                            (gnus-group-method to-newsgroup)))))
9667     (setq to-newsgroup
9668           (or encoded
9669               (and to-newsgroup
9670                    (mm-encode-coding-string
9671                     to-newsgroup
9672                     (gnus-group-name-charset to-method to-newsgroup)))))
9673     ;; Check the method we are to move this article to...
9674     (unless (gnus-check-backend-function
9675              'request-accept-article (car to-method))
9676       (error "%s does not support article copying" (car to-method)))
9677     (unless (gnus-check-server to-method)
9678       (error "Can't open server %s" (car to-method)))
9679     (gnus-message 6 "%s to %s: %s..."
9680                   (caddr (assq action names))
9681                   (or (car select-method)
9682                       (gnus-group-decoded-name to-newsgroup))
9683                   articles)
9684     (while articles
9685       (setq article (pop articles))
9686       (setq
9687        art-group
9688        (cond
9689         ;; Move the article.
9690         ((eq action 'move)
9691          ;; Remove this article from future suppression.
9692          (gnus-dup-unsuppress-article article)
9693          (let* ((from-method (gnus-find-method-for-group
9694                               gnus-newsgroup-name))
9695                 (to-method (or select-method
9696                                (gnus-find-method-for-group to-newsgroup)))
9697                 (move-is-internal (gnus-method-equal from-method to-method)))
9698            (gnus-request-move-article
9699             article                     ; Article to move
9700             gnus-newsgroup-name         ; From newsgroup
9701             (nth 1 (gnus-find-method-for-group
9702                     gnus-newsgroup-name)) ; Server
9703             (list 'gnus-request-accept-article
9704                   to-newsgroup (list 'quote select-method)
9705                   (not articles) t)     ; Accept form
9706             (not articles)              ; Only save nov last time
9707             move-is-internal)))         ; is this move internal?
9708         ;; Copy the article.
9709         ((eq action 'copy)
9710          (with-current-buffer copy-buf
9711            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9712              (save-restriction
9713                (nnheader-narrow-to-headers)
9714                (dolist (hdr gnus-copy-article-ignored-headers)
9715                  (message-remove-header hdr t)))
9716              (gnus-request-accept-article
9717               to-newsgroup select-method (not articles) t))))
9718         ;; Crosspost the article.
9719         ((eq action 'crosspost)
9720          (let ((xref (message-tokenize-header
9721                       (mail-header-xref (gnus-summary-article-header article))
9722                       " ")))
9723            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9724                                   ":" (number-to-string article)))
9725            (unless xref
9726              (setq xref (list (system-name))))
9727            (setq new-xref
9728                  (concat
9729                   (mapconcat 'identity
9730                              (delete "Xref:" (delete new-xref xref))
9731                              " ")
9732                   " " new-xref))
9733            (with-current-buffer copy-buf
9734              ;; First put the article in the destination group.
9735              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9736              (when (consp (setq art-group
9737                                 (gnus-request-accept-article
9738                                  to-newsgroup select-method (not articles) t)))
9739                (setq new-xref (concat new-xref " " (car art-group)
9740                                       ":"
9741                                       (number-to-string (cdr art-group))))
9742                ;; Now we have the new Xrefs header, so we insert
9743                ;; it and replace the new article.
9744                (nnheader-replace-header "Xref" new-xref)
9745                (gnus-request-replace-article
9746                 (cdr art-group) to-newsgroup (current-buffer) t)
9747                art-group))))))
9748       (cond
9749        ((not art-group)
9750         (gnus-message 1 "Couldn't %s article %s: %s"
9751                       (cadr (assq action names)) article
9752                       (nnheader-get-report (car to-method))))
9753        ((eq art-group 'junk)
9754         (when (eq action 'move)
9755           (gnus-summary-mark-article article gnus-canceled-mark)
9756           (gnus-message 4 "Deleted article %s" article)
9757           ;; run the delete hook
9758           (run-hook-with-args 'gnus-summary-article-delete-hook
9759                               action
9760                               (gnus-data-header
9761                                (assoc article (gnus-data-list nil)))
9762                               gnus-newsgroup-name nil
9763                               select-method)))
9764        (t
9765         (let* ((pto-group (gnus-group-prefixed-name
9766                            (car art-group) to-method))
9767                (info (gnus-get-info pto-group))
9768                (to-group (gnus-info-group info))
9769                to-marks)
9770           ;; Update the group that has been moved to.
9771           (when (and info
9772                      (memq action '(move copy)))
9773             (unless (member to-group to-groups)
9774               (push to-group to-groups))
9775
9776             (unless (memq article gnus-newsgroup-unreads)
9777               (push 'read to-marks)
9778               (gnus-info-set-read
9779                info (gnus-add-to-range (gnus-info-read info)
9780                                        (list (cdr art-group)))))
9781
9782             ;; See whether the article is to be put in the cache.
9783             (let* ((expirable (gnus-group-auto-expirable-p to-group))
9784                    (marks (if expirable
9785                               gnus-article-mark-lists
9786                             (delete '(expirable . expire)
9787                                     (copy-sequence gnus-article-mark-lists))))
9788                    (to-article (cdr art-group)))
9789
9790               ;; Enter the article into the cache in the new group,
9791               ;; if that is required.
9792               (when gnus-use-cache
9793                 (gnus-cache-possibly-enter-article
9794                  to-group to-article
9795                  (memq article gnus-newsgroup-marked)
9796                  (memq article gnus-newsgroup-dormant)
9797                  (memq article gnus-newsgroup-unreads)))
9798
9799               (when gnus-preserve-marks
9800                 ;; Copy any marks over to the new group.
9801                 (when (and (equal to-group gnus-newsgroup-name)
9802                            (not (memq article gnus-newsgroup-unreads)))
9803                   ;; Mark this article as read in this group.
9804                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9805                   (setcdr (gnus-active to-group) to-article)
9806                   (setcdr gnus-newsgroup-active to-article))
9807
9808                 (while marks
9809                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9810                     (when (memq article (symbol-value
9811                                          (intern (format "gnus-newsgroup-%s"
9812                                                          (caar marks)))))
9813                       (push (cdar marks) to-marks)
9814                       ;; If the other group is the same as this group,
9815                       ;; then we have to add the mark to the list.
9816                       (when (equal to-group gnus-newsgroup-name)
9817                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9818                              (cons to-article
9819                                    (symbol-value
9820                                     (intern (format "gnus-newsgroup-%s"
9821                                                     (caar marks)))))))
9822                       ;; Copy the marks to other group.
9823                       (gnus-add-marked-articles
9824                        to-group (cdar marks) (list to-article) info)))
9825                   (setq marks (cdr marks)))
9826
9827                 (when (and expirable
9828                            gnus-mark-copied-or-moved-articles-as-expirable
9829                            (not (memq 'expire to-marks)))
9830                   ;; Mark this article as expirable.
9831                   (push 'expire to-marks)
9832                   (when (equal to-group gnus-newsgroup-name)
9833                     (push to-article gnus-newsgroup-expirable))
9834                   ;; Copy the expirable mark to other group.
9835                   (gnus-add-marked-articles
9836                    to-group 'expire (list to-article) info))
9837
9838                 (gnus-request-set-mark
9839                  to-group (list (list (list to-article) 'add to-marks))))
9840
9841               (gnus-dribble-enter
9842                (concat "(gnus-group-set-info '"
9843                        (gnus-prin1-to-string (gnus-get-info to-group))
9844                        ")"))))
9845
9846           ;; Update the Xref header in this article to point to
9847           ;; the new crossposted article we have just created.
9848           (when (eq action 'crosspost)
9849             (with-current-buffer copy-buf
9850               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9851               (nnheader-replace-header "Xref" new-xref)
9852               (gnus-request-replace-article
9853                article gnus-newsgroup-name (current-buffer) t)))
9854
9855           ;; run the move/copy/crosspost/respool hook
9856           (run-hook-with-args 'gnus-summary-article-move-hook
9857                               action
9858                               (gnus-data-header
9859                                (assoc article (gnus-data-list nil)))
9860                               gnus-newsgroup-name
9861                               to-newsgroup
9862                               select-method))
9863
9864         ;;;!!!Why is this necessary?
9865         (set-buffer gnus-summary-buffer)
9866
9867         (gnus-summary-goto-subject article)
9868         (when (eq action 'move)
9869           (gnus-summary-mark-article article gnus-canceled-mark))))
9870       (push article articles-to-update-marks))
9871
9872     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9873     ;; Re-activate all groups that have been moved to.
9874     (with-current-buffer gnus-group-buffer
9875       (let ((gnus-group-marked to-groups))
9876         (gnus-group-get-new-news-this-group nil t)))
9877
9878     (gnus-kill-buffer copy-buf)
9879     (gnus-summary-position-point)
9880     (gnus-set-mode-line 'summary)))
9881
9882 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9883   "Copy the current article to some other group.
9884 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9885 When called interactively, if TO-NEWSGROUP is nil, use the value of
9886 the variable `gnus-move-split-methods' for finding a default target
9887 newsgroup.
9888 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9889 re-spool using this method."
9890   (interactive "P")
9891   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9892
9893 (defun gnus-summary-crosspost-article (&optional n)
9894   "Crosspost the current article to some other group."
9895   (interactive "P")
9896   (gnus-summary-move-article n nil nil 'crosspost))
9897
9898 (defcustom gnus-summary-respool-default-method nil
9899   "Default method type for respooling an article.
9900 If nil, use to the current newsgroup method."
9901   :type 'symbol
9902   :group 'gnus-summary-mail)
9903
9904 (defun gnus-summary-respool-article (&optional n method)
9905   "Respool the current article.
9906 The article will be squeezed through the mail spooling process again,
9907 which means that it will be put in some mail newsgroup or other
9908 depending on `nnmail-split-methods'.
9909 If N is a positive number, respool the N next articles.
9910 If N is a negative number, respool the N previous articles.
9911 If N is nil and any articles have been marked with the process mark,
9912 respool those articles instead.
9913
9914 Respooling can be done both from mail groups and \"real\" newsgroups.
9915 In the former case, the articles in question will be moved from the
9916 current group into whatever groups they are destined to.  In the
9917 latter case, they will be copied into the relevant groups."
9918   (interactive
9919    (list current-prefix-arg
9920          (let* ((methods (gnus-methods-using 'respool))
9921                 (methname
9922                  (symbol-name (or gnus-summary-respool-default-method
9923                                   (car (gnus-find-method-for-group
9924                                         gnus-newsgroup-name)))))
9925                 (method
9926                  (gnus-completing-read-with-default
9927                   methname "Backend to use when respooling"
9928                   methods nil t nil 'gnus-mail-method-history))
9929                 ms)
9930            (cond
9931             ((zerop (length (setq ms (gnus-servers-using-backend
9932                                       (intern method)))))
9933              (list (intern method) ""))
9934             ((= 1 (length ms))
9935              (car ms))
9936             (t
9937              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9938                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9939                            ms-alist))))))))
9940   (unless method
9941     (error "No method given for respooling"))
9942   (if (assoc (symbol-name
9943               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9944              (gnus-methods-using 'respool))
9945       (gnus-summary-move-article n nil method)
9946     (gnus-summary-copy-article n nil method)))
9947
9948 (defun gnus-summary-import-article (file &optional edit)
9949   "Import an arbitrary file into a mail newsgroup."
9950   (interactive "fImport file: \nP")
9951   (let ((group gnus-newsgroup-name)
9952         (now (current-time))
9953         atts lines group-art)
9954     (unless (gnus-check-backend-function 'request-accept-article group)
9955       (error "%s does not support article importing" group))
9956     (or (file-readable-p file)
9957         (not (file-regular-p file))
9958         (error "Can't read %s" file))
9959     (with-current-buffer (gnus-get-buffer-create " *import file*")
9960       (erase-buffer)
9961       (nnheader-insert-file-contents file)
9962       (goto-char (point-min))
9963       (if (nnheader-article-p)
9964           (save-restriction
9965             (goto-char (point-min))
9966             (search-forward "\n\n" nil t)
9967             (narrow-to-region (point-min) (1- (point)))
9968             (goto-char (point-min))
9969             (unless (re-search-forward "^date:" nil t)
9970               (goto-char (point-max))
9971               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9972        ;; This doesn't look like an article, so we fudge some headers.
9973         (setq atts (file-attributes file)
9974               lines (count-lines (point-min) (point-max)))
9975         (insert "From: " (read-string "From: ") "\n"
9976                 "Subject: " (read-string "Subject: ") "\n"
9977                 "Date: " (message-make-date (nth 5 atts)) "\n"
9978                 "Message-ID: " (message-make-message-id) "\n"
9979                 "Lines: " (int-to-string lines) "\n"
9980                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9981       (setq group-art (gnus-request-accept-article group nil t))
9982       (kill-buffer (current-buffer)))
9983     (setq gnus-newsgroup-active (gnus-activate-group group))
9984     (forward-line 1)
9985     (gnus-summary-goto-article (cdr group-art) nil t)
9986     (when edit
9987       (gnus-summary-edit-article))))
9988
9989 (defun gnus-summary-create-article ()
9990   "Create an article in a mail newsgroup."
9991   (interactive)
9992   (let ((group gnus-newsgroup-name)
9993         (now (current-time))
9994         group-art)
9995     (unless (gnus-check-backend-function 'request-accept-article group)
9996       (error "%s does not support article importing" group))
9997     (with-current-buffer (gnus-get-buffer-create " *import file*")
9998       (erase-buffer)
9999       (goto-char (point-min))
10000       ;; This doesn't look like an article, so we fudge some headers.
10001       (insert "From: " (read-string "From: ") "\n"
10002               "Subject: " (read-string "Subject: ") "\n"
10003               "Date: " (message-make-date now) "\n"
10004               "Message-ID: " (message-make-message-id) "\n")
10005       (setq group-art (gnus-request-accept-article group nil t))
10006       (kill-buffer (current-buffer)))
10007     (setq gnus-newsgroup-active (gnus-activate-group group))
10008     (forward-line 1)
10009     (gnus-summary-goto-article (cdr group-art) nil t)
10010     (gnus-summary-edit-article)))
10011
10012 (defun gnus-summary-article-posted-p ()
10013   "Say whether the current (mail) article is available from news as well.
10014 This will be the case if the article has both been mailed and posted."
10015   (interactive)
10016   (let ((id (mail-header-references (gnus-summary-article-header)))
10017         (gnus-override-method (car (gnus-refer-article-methods))))
10018     (if (gnus-request-head id "")
10019         (gnus-message 2 "The current message was found on %s"
10020                       gnus-override-method)
10021       (gnus-message 2 "The current message couldn't be found on %s"
10022                     gnus-override-method)
10023       nil)))
10024
10025 (defun gnus-summary-expire-articles (&optional now)
10026   "Expire all articles that are marked as expirable in the current group."
10027   (interactive)
10028   (when (and (not gnus-group-is-exiting-without-update-p)
10029              (gnus-check-backend-function
10030               'request-expire-articles gnus-newsgroup-name))
10031     ;; This backend supports expiry.
10032     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10033            (expirable (if total
10034                           (progn
10035                             ;; We need to update the info for
10036                             ;; this group for `gnus-list-of-read-articles'
10037                             ;; to give us the right answer.
10038                             (gnus-run-hooks 'gnus-exit-group-hook)
10039                             (gnus-summary-update-info)
10040                             (gnus-list-of-read-articles gnus-newsgroup-name))
10041                         (setq gnus-newsgroup-expirable
10042                               (sort gnus-newsgroup-expirable '<))))
10043            (expiry-wait (if now 'immediate
10044                           (gnus-group-find-parameter
10045                            gnus-newsgroup-name 'expiry-wait)))
10046            (nnmail-expiry-target
10047             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10048                 nnmail-expiry-target))
10049            es)
10050       (when expirable
10051         ;; There are expirable articles in this group, so we run them
10052         ;; through the expiry process.
10053         (gnus-message 6 "Expiring articles...")
10054         (unless (gnus-check-group gnus-newsgroup-name)
10055           (error "Can't open server for %s" gnus-newsgroup-name))
10056         ;; The list of articles that weren't expired is returned.
10057         (save-excursion
10058           (if expiry-wait
10059               (let ((nnmail-expiry-wait-function nil)
10060                     (nnmail-expiry-wait expiry-wait))
10061                 (setq es (gnus-request-expire-articles
10062                           expirable gnus-newsgroup-name)))
10063             (setq es (gnus-request-expire-articles
10064                       expirable gnus-newsgroup-name)))
10065           (unless total
10066             (setq gnus-newsgroup-expirable es))
10067           ;; We go through the old list of expirable, and mark all
10068           ;; really expired articles as nonexistent.
10069           (unless (eq es expirable) ;If nothing was expired, we don't mark.
10070             (let ((gnus-use-cache nil))
10071               (dolist (article expirable)
10072                 (when (and (not (memq article es))
10073                            (gnus-data-find article))
10074                   (gnus-summary-mark-article article gnus-canceled-mark)
10075                   (run-hook-with-args 'gnus-summary-article-expire-hook
10076                                       'delete
10077                                       (gnus-data-header
10078                                        (assoc article (gnus-data-list nil)))
10079                                       gnus-newsgroup-name
10080                                       nil
10081                                       nil))))))
10082         (gnus-message 6 "Expiring articles...done")))))
10083
10084 (defun gnus-summary-expire-articles-now ()
10085   "Expunge all expirable articles in the current group.
10086 This means that *all* articles that are marked as expirable will be
10087 deleted forever, right now."
10088   (interactive)
10089   (or gnus-expert-user
10090       (gnus-yes-or-no-p
10091        "Are you really, really sure you want to delete all expirable messages? ")
10092       (error "Phew!"))
10093   (gnus-summary-expire-articles t))
10094
10095 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10096 (defun gnus-summary-delete-article (&optional n)
10097   "Delete the N next (mail) articles.
10098 This command actually deletes articles.  This is not a marking
10099 command.  The article will disappear forever from your life, never to
10100 return.
10101
10102 If N is negative, delete backwards.
10103 If N is nil and articles have been marked with the process mark,
10104 delete these instead.
10105
10106 If `gnus-novice-user' is non-nil you will be asked for
10107 confirmation before the articles are deleted."
10108   (interactive "P")
10109   (unless (gnus-check-backend-function 'request-expire-articles
10110                                        gnus-newsgroup-name)
10111     (error "The current newsgroup does not support article deletion"))
10112   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10113     (error "Couldn't open server"))
10114   ;; Compute the list of articles to delete.
10115   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10116         (nnmail-expiry-target 'delete)
10117         not-deleted)
10118     (if (and gnus-novice-user
10119              (not (gnus-yes-or-no-p
10120                    (format "Do you really want to delete %s forever? "
10121                            (if (> (length articles) 1)
10122                                (format "these %s articles" (length articles))
10123                              "this article")))))
10124         ()
10125       ;; Delete the articles.
10126       (setq not-deleted (gnus-request-expire-articles
10127                          articles gnus-newsgroup-name 'force))
10128       (while articles
10129         (gnus-summary-remove-process-mark (car articles))
10130         ;; The backend might not have been able to delete the article
10131         ;; after all.
10132         (unless (memq (car articles) not-deleted)
10133           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10134         (let* ((article (car articles))
10135                (ghead  (gnus-data-header
10136                                     (assoc article (gnus-data-list nil)))))
10137           (run-hook-with-args 'gnus-summary-article-delete-hook
10138                               'delete ghead gnus-newsgroup-name nil
10139                               nil))
10140         (setq articles (cdr articles)))
10141       (when not-deleted
10142         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10143     (gnus-summary-position-point)
10144     (gnus-set-mode-line 'summary)
10145     not-deleted))
10146
10147 (defun gnus-summary-edit-article (&optional arg)
10148   "Edit the current article.
10149 This will have permanent effect only in mail groups.
10150 If ARG is nil, edit the decoded articles.
10151 If ARG is 1, edit the raw articles.
10152 If ARG is 2, edit the raw articles even in read-only groups.
10153 If ARG is 3, edit the articles with the current handles.
10154 Otherwise, allow editing of articles even in read-only
10155 groups."
10156   (interactive "P")
10157   (let (force raw current-handles)
10158     (cond
10159      ((null arg))
10160      ((eq arg 1)
10161       (setq raw t))
10162      ((eq arg 2)
10163       (setq raw t
10164             force t))
10165      ((eq arg 3)
10166       (setq current-handles
10167             (and (gnus-buffer-live-p gnus-article-buffer)
10168                  (with-current-buffer gnus-article-buffer
10169                    (prog1
10170                        gnus-article-mime-handles
10171                      (setq gnus-article-mime-handles nil))))))
10172      (t
10173       (setq force t)))
10174     (when (and raw (not force)
10175                (member gnus-newsgroup-name '("nndraft:delayed"
10176                                              "nndraft:drafts"
10177                                              "nndraft:queue")))
10178       (error "Can't edit the raw article in group %s"
10179              gnus-newsgroup-name))
10180     (with-current-buffer gnus-summary-buffer
10181       (let ((mail-parse-charset gnus-newsgroup-charset)
10182             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10183         (gnus-set-global-variables)
10184         (when (and (not force)
10185                    (gnus-group-read-only-p))
10186           (error "The current newsgroup does not support article editing"))
10187         (gnus-summary-show-article t)
10188         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10189           (with-current-buffer gnus-article-buffer
10190             (mm-enable-multibyte)))
10191         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10192             (setq raw t))
10193         (gnus-article-edit-article
10194          (if raw 'ignore
10195            `(lambda ()
10196               (let ((mbl mml-buffer-list))
10197                 (setq mml-buffer-list nil)
10198                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10199                   (mime-to-mml ,'current-handles))
10200                 (let ((mbl1 mml-buffer-list))
10201                   (setq mml-buffer-list mbl)
10202                   (set (make-local-variable 'mml-buffer-list) mbl1))
10203                 (gnus-make-local-hook 'kill-buffer-hook)
10204                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10205          `(lambda (no-highlight)
10206             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10207                   (message-options message-options)
10208                   (message-options-set-recipient)
10209                   (mail-parse-ignored-charsets
10210                    ',gnus-newsgroup-ignored-charsets)
10211                   (rfc2047-header-encoding-alist
10212                    ',(let ((charset (gnus-group-name-charset
10213                                      (gnus-find-method-for-group
10214                                       gnus-newsgroup-name)
10215                                      gnus-newsgroup-name)))
10216                        (append (list (cons "Newsgroups" charset)
10217                                      (cons "Followup-To" charset)
10218                                      (cons "Xref" charset))
10219                                rfc2047-header-encoding-alist))))
10220               ,(if (not raw) '(progn
10221                                 (mml-to-mime)
10222                                 (mml-destroy-buffers)
10223                                 (remove-hook 'kill-buffer-hook
10224                                              'mml-destroy-buffers t)
10225                                 (kill-local-variable 'mml-buffer-list)))
10226               (gnus-summary-edit-article-done
10227                ,(or (mail-header-references gnus-current-headers) "")
10228                ,(gnus-group-read-only-p)
10229                ,gnus-summary-buffer no-highlight))))))))
10230
10231 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10232
10233 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10234                                                  no-highlight)
10235   "Make edits to the current article permanent."
10236   (interactive)
10237   (save-excursion
10238    ;; The buffer restriction contains the entire article if it exists.
10239     (when (article-goto-body)
10240       (let ((lines (count-lines (point) (point-max)))
10241             (length (- (point-max) (point)))
10242             (case-fold-search t)
10243             (body (copy-marker (point))))
10244         (goto-char (point-min))
10245         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10246           (delete-region (match-beginning 1) (match-end 1))
10247           (insert (number-to-string length)))
10248         (goto-char (point-min))
10249         (when (re-search-forward
10250                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10251           (delete-region (match-beginning 1) (match-end 1))
10252           (insert (number-to-string length)))
10253         (goto-char (point-min))
10254         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10255           (delete-region (match-beginning 1) (match-end 1))
10256           (insert (number-to-string lines))))))
10257   ;; Replace the article.
10258   (let ((buf (current-buffer)))
10259     (with-temp-buffer
10260       (insert-buffer-substring buf)
10261
10262       (if (and (not read-only)
10263                (not (gnus-request-replace-article
10264                      (cdr gnus-article-current) (car gnus-article-current)
10265                      (current-buffer) t)))
10266           (error "Couldn't replace article")
10267         ;; Update the summary buffer.
10268         (if (and references
10269                  (equal (message-tokenize-header references " ")
10270                         (message-tokenize-header
10271                          (or (message-fetch-field "references") "") " ")))
10272             ;; We only have to update this line.
10273             (save-excursion
10274               (save-restriction
10275                 (message-narrow-to-head)
10276                 (let ((head (buffer-substring-no-properties
10277                              (point-min) (point-max)))
10278                       header)
10279                   (with-temp-buffer
10280                     (insert (format "211 %d Article retrieved.\n"
10281                                     (cdr gnus-article-current)))
10282                     (insert head)
10283                     (insert ".\n")
10284                     (let ((nntp-server-buffer (current-buffer)))
10285                       (setq header (car (gnus-get-newsgroup-headers
10286                                          nil t))))
10287                     (with-current-buffer gnus-summary-buffer
10288                       (gnus-data-set-header
10289                        (gnus-data-find (cdr gnus-article-current))
10290                        header)
10291                       (gnus-summary-update-article-line
10292                        (cdr gnus-article-current) header)
10293                       (if (gnus-summary-goto-subject
10294                            (cdr gnus-article-current) nil t)
10295                           (gnus-summary-update-secondary-mark
10296                            (cdr gnus-article-current))))))))
10297           ;; Update threads.
10298           (set-buffer (or buffer gnus-summary-buffer))
10299           (gnus-summary-update-article (cdr gnus-article-current))
10300           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10301               (gnus-summary-update-secondary-mark
10302                (cdr gnus-article-current))))
10303         ;; Prettify the article buffer again.
10304         (unless no-highlight
10305           (with-current-buffer gnus-article-buffer
10306             ;;;!!! Fix this -- article should be rehighlighted.
10307             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10308             (set-buffer gnus-original-article-buffer)
10309             (gnus-request-article
10310              (cdr gnus-article-current)
10311              (car gnus-article-current) (current-buffer))))
10312         ;; Prettify the summary buffer line.
10313         (when (gnus-visual-p 'summary-highlight 'highlight)
10314           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10315
10316 (defun gnus-summary-edit-wash (key)
10317   "Perform editing command KEY in the article buffer."
10318   (interactive
10319    (list
10320     (progn
10321       (message "%s" (concat (this-command-keys) "- "))
10322       (read-char))))
10323   (message "")
10324   (gnus-summary-edit-article)
10325   (execute-kbd-macro (concat (this-command-keys) key))
10326   (gnus-article-edit-done))
10327
10328 ;;; Respooling
10329
10330 (defun gnus-summary-respool-query (&optional silent trace)
10331   "Query where the respool algorithm would put this article."
10332   (interactive)
10333   (let (gnus-mark-article-hook)
10334     (gnus-summary-select-article)
10335     (with-current-buffer gnus-original-article-buffer
10336       (let ((groups (nnmail-article-group 'identity trace)))
10337         (unless silent
10338           (if groups
10339               (message "This message would go to %s"
10340                        (mapconcat 'car groups ", "))
10341             (message "This message would go to no groups"))
10342           groups)))))
10343
10344 (defun gnus-summary-respool-trace ()
10345   "Trace where the respool algorithm would put this article.
10346 Display a buffer showing all fancy splitting patterns which matched."
10347   (interactive)
10348   (gnus-summary-respool-query nil t))
10349
10350 ;; Summary marking commands.
10351
10352 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10353   "Mark articles which has the same subject as read, and then select the next.
10354 If UNMARK is positive, remove any kind of mark.
10355 If UNMARK is negative, tick articles."
10356   (interactive "P")
10357   (when unmark
10358     (setq unmark (prefix-numeric-value unmark)))
10359   (let ((count
10360          (gnus-summary-mark-same-subject
10361           (gnus-summary-article-subject) unmark)))
10362     ;; Select next unread article.  If auto-select-same mode, should
10363     ;; select the first unread article.
10364     (gnus-summary-next-article t (and gnus-auto-select-same
10365                                       (gnus-summary-article-subject)))
10366     (gnus-message 7 "%d article%s marked as %s"
10367                   count (if (= count 1) " is" "s are")
10368                   (if unmark "unread" "read"))))
10369
10370 (defun gnus-summary-kill-same-subject (&optional unmark)
10371   "Mark articles which has the same subject as read.
10372 If UNMARK is positive, remove any kind of mark.
10373 If UNMARK is negative, tick articles."
10374   (interactive "P")
10375   (when unmark
10376     (setq unmark (prefix-numeric-value unmark)))
10377   (let ((count
10378          (gnus-summary-mark-same-subject
10379           (gnus-summary-article-subject) unmark)))
10380     ;; If marked as read, go to next unread subject.
10381     (when (null unmark)
10382       ;; Go to next unread subject.
10383       (gnus-summary-next-subject 1 t))
10384     (gnus-message 7 "%d articles are marked as %s"
10385                   count (if unmark "unread" "read"))))
10386
10387 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10388   "Mark articles with same SUBJECT as read, and return marked number.
10389 If optional argument UNMARK is positive, remove any kinds of marks.
10390 If optional argument UNMARK is negative, mark articles as unread instead."
10391   (let ((count 1))
10392     (save-excursion
10393       (cond
10394        ((null unmark)                   ; Mark as read.
10395         (while (and
10396                 (progn
10397                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10398                   (gnus-summary-show-thread) t)
10399                 (gnus-summary-find-subject subject))
10400           (setq count (1+ count))))
10401        ((> unmark 0)                    ; Tick.
10402         (while (and
10403                 (progn
10404                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10405                   (gnus-summary-show-thread) t)
10406                 (gnus-summary-find-subject subject))
10407           (setq count (1+ count))))
10408        (t                               ; Mark as unread.
10409         (while (and
10410                 (progn
10411                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10412                   (gnus-summary-show-thread) t)
10413                 (gnus-summary-find-subject subject))
10414           (setq count (1+ count)))))
10415       (gnus-set-mode-line 'summary)
10416       ;; Return the number of marked articles.
10417       count)))
10418
10419 (defun gnus-summary-mark-as-processable (n &optional unmark)
10420   "Set the process mark on the next N articles.
10421 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10422 the process mark instead.  The difference between N and the actual
10423 number of articles marked is returned."
10424   (interactive "P")
10425   (if (and (null n) (gnus-region-active-p))
10426       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10427     (setq n (prefix-numeric-value n))
10428     (let ((backward (< n 0))
10429           (n (abs n)))
10430       (while (and
10431               (> n 0)
10432               (if unmark
10433                   (gnus-summary-remove-process-mark
10434                    (gnus-summary-article-number))
10435                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10436               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10437         (setq n (1- n)))
10438       (when (/= 0 n)
10439         (gnus-message 7 "No more articles"))
10440       (gnus-summary-recenter)
10441       (gnus-summary-position-point)
10442       n)))
10443
10444 (defun gnus-summary-unmark-as-processable (n)
10445   "Remove the process mark from the next N articles.
10446 If N is negative, unmark backward instead.  The difference between N and
10447 the actual number of articles unmarked is returned."
10448   (interactive "P")
10449   (gnus-summary-mark-as-processable n t))
10450
10451 (defun gnus-summary-unmark-all-processable ()
10452   "Remove the process mark from all articles."
10453   (interactive)
10454   (save-excursion
10455     (while gnus-newsgroup-processable
10456       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10457   (gnus-summary-position-point))
10458
10459 (defun gnus-summary-add-mark (article type)
10460   "Mark ARTICLE with a mark of TYPE."
10461   (let ((vtype (car (assq type gnus-article-mark-lists)))
10462         var)
10463     (if (not vtype)
10464         (error "No such mark type: %s" type)
10465       (setq var (intern (format "gnus-newsgroup-%s" type)))
10466       (set var (cons article (symbol-value var)))
10467       (if (memq type '(processable cached replied forwarded recent saved))
10468           (gnus-summary-update-secondary-mark article)
10469         ;;; !!! This is bogus.  We should find out what primary
10470         ;;; !!! mark we want to set.
10471         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10472
10473 (defun gnus-summary-mark-as-expirable (n)
10474   "Mark N articles forward as expirable.
10475 If N is negative, mark backward instead.  The difference between N and
10476 the actual number of articles marked is returned."
10477   (interactive "p")
10478   (gnus-summary-mark-forward n gnus-expirable-mark))
10479
10480 (defun gnus-summary-mark-as-spam (n)
10481   "Mark N articles forward as spam.
10482 If N is negative, mark backward instead.  The difference between N and
10483 the actual number of articles marked is returned."
10484   (interactive "p")
10485   (gnus-summary-mark-forward n gnus-spam-mark))
10486
10487 (defun gnus-summary-mark-article-as-replied (article)
10488   "Mark ARTICLE as replied to and update the summary line.
10489 ARTICLE can also be a list of articles."
10490   (interactive (list (gnus-summary-article-number)))
10491   (let ((articles (if (listp article) article (list article))))
10492     (dolist (article articles)
10493       (unless (numberp article)
10494         (error "%s is not a number" article))
10495       (push article gnus-newsgroup-replied)
10496       (let ((inhibit-read-only t))
10497         (when (gnus-summary-goto-subject article nil t)
10498           (gnus-summary-update-secondary-mark article))))))
10499
10500 (defun gnus-summary-mark-article-as-forwarded (article)
10501   "Mark ARTICLE as forwarded and update the summary line.
10502 ARTICLE can also be a list of articles."
10503   (let ((articles (if (listp article) article (list article))))
10504     (dolist (article articles)
10505       (push article gnus-newsgroup-forwarded)
10506       (let ((inhibit-read-only t))
10507         (when (gnus-summary-goto-subject article nil t)
10508           (gnus-summary-update-secondary-mark article))))))
10509
10510 (defun gnus-summary-set-bookmark (article)
10511   "Set a bookmark in current article."
10512   (interactive (list (gnus-summary-article-number)))
10513   (when (or (not (get-buffer gnus-article-buffer))
10514             (not gnus-current-article)
10515             (not gnus-article-current)
10516             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10517     (error "No current article selected"))
10518   ;; Remove old bookmark, if one exists.
10519   (gnus-pull article gnus-newsgroup-bookmarks)
10520   ;; Set the new bookmark, which is on the form
10521   ;; (article-number . line-number-in-body).
10522   (push
10523    (cons article
10524          (with-current-buffer gnus-article-buffer
10525            (count-lines
10526             (min (point)
10527                  (save-excursion
10528                    (article-goto-body)
10529                    (point)))
10530             (point))))
10531    gnus-newsgroup-bookmarks)
10532   (gnus-message 6 "A bookmark has been added to the current article."))
10533
10534 (defun gnus-summary-remove-bookmark (article)
10535   "Remove the bookmark from the current article."
10536   (interactive (list (gnus-summary-article-number)))
10537   ;; Remove old bookmark, if one exists.
10538   (if (not (assq article gnus-newsgroup-bookmarks))
10539       (gnus-message 6 "No bookmark in current article.")
10540     (gnus-pull article gnus-newsgroup-bookmarks)
10541     (gnus-message 6 "Removed bookmark.")))
10542
10543 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10544 (defun gnus-summary-mark-as-dormant (n)
10545   "Mark N articles forward as dormant.
10546 If N is negative, mark backward instead.  The difference between N and
10547 the actual number of articles marked is returned."
10548   (interactive "p")
10549   (gnus-summary-mark-forward n gnus-dormant-mark))
10550
10551 (defun gnus-summary-set-process-mark (article)
10552   "Set the process mark on ARTICLE and update the summary line."
10553   (setq gnus-newsgroup-processable
10554         (cons article
10555               (delq article gnus-newsgroup-processable)))
10556   (when (gnus-summary-goto-subject article)
10557     (gnus-summary-show-thread)
10558     (gnus-summary-goto-subject article)
10559     (gnus-summary-update-secondary-mark article)))
10560
10561 (defun gnus-summary-remove-process-mark (&rest articles)
10562   "Remove the process mark from ARTICLES and update the summary line."
10563   (dolist (article articles)
10564     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10565     (when (gnus-summary-goto-subject article)
10566       (gnus-summary-show-thread)
10567       (gnus-summary-goto-subject article)
10568       (gnus-summary-update-secondary-mark article)))
10569   t)
10570
10571 (defun gnus-summary-set-saved-mark (article)
10572   "Set the process mark on ARTICLE and update the summary line."
10573   (push article gnus-newsgroup-saved)
10574   (when (gnus-summary-goto-subject article)
10575     (gnus-summary-update-secondary-mark article)))
10576
10577 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10578   "Mark N articles as read forwards.
10579 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10580 The difference between N and the actual number of articles marked is
10581 returned.
10582 If NO-EXPIRE, auto-expiry will be inhibited."
10583   (interactive "p")
10584   (gnus-summary-show-thread)
10585   (let ((backward (< n 0))
10586         (gnus-summary-goto-unread
10587          (and gnus-summary-goto-unread
10588               (not (eq gnus-summary-goto-unread 'never))
10589               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10590                                     gnus-ticked-mark gnus-dormant-mark)))))
10591         (n (abs n))
10592         (mark (or mark gnus-del-mark)))
10593     (while (and (> n 0)
10594                 (gnus-summary-mark-article nil mark no-expire)
10595                 (zerop (gnus-summary-next-subject
10596                         (if backward -1 1)
10597                         (and gnus-summary-goto-unread
10598                              (not (eq gnus-summary-goto-unread 'never)))
10599                         t)))
10600       (setq n (1- n)))
10601     (when (/= 0 n)
10602       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10603     (gnus-summary-recenter)
10604     (gnus-summary-position-point)
10605     (gnus-set-mode-line 'summary)
10606     n))
10607
10608 (defun gnus-summary-mark-article-as-read (mark)
10609   "Mark the current article quickly as read with MARK."
10610   (let ((article (gnus-summary-article-number)))
10611     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10612     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10613     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10614     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10615     (push (cons article mark) gnus-newsgroup-reads)
10616     ;; Possibly remove from cache, if that is used.
10617     (when gnus-use-cache
10618       (gnus-cache-enter-remove-article article))
10619     ;; Allow the backend to change the mark.
10620     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10621     ;; Check for auto-expiry.
10622     (when (and gnus-newsgroup-auto-expire
10623                (memq mark gnus-auto-expirable-marks))
10624       (setq mark gnus-expirable-mark)
10625       ;; Let the backend know about the mark change.
10626       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10627       (push article gnus-newsgroup-expirable))
10628     ;; Set the mark in the buffer.
10629     (gnus-summary-update-mark mark 'unread)
10630     t))
10631
10632 (defun gnus-summary-mark-article-as-unread (mark)
10633   "Mark the current article quickly as unread with MARK."
10634   (let* ((article (gnus-summary-article-number))
10635          (old-mark (gnus-summary-article-mark article)))
10636     ;; Allow the backend to change the mark.
10637     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10638     (if (eq mark old-mark)
10639         t
10640       (if (<= article 0)
10641           (progn
10642             (gnus-error 1 "Can't mark negative article numbers")
10643             nil)
10644         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10645         (setq gnus-newsgroup-spam-marked
10646               (delq article gnus-newsgroup-spam-marked))
10647         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10648         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10649         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10650         (cond ((= mark gnus-ticked-mark)
10651                (setq gnus-newsgroup-marked
10652                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10653                                               article)))
10654               ((= mark gnus-spam-mark)
10655                (setq gnus-newsgroup-spam-marked
10656                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10657                                               article)))
10658               ((= mark gnus-dormant-mark)
10659                (setq gnus-newsgroup-dormant
10660                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10661                                               article)))
10662               (t
10663                (setq gnus-newsgroup-unreads
10664                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10665                                               article))))
10666         (gnus-pull article gnus-newsgroup-reads)
10667
10668         ;; See whether the article is to be put in the cache.
10669         (and gnus-use-cache
10670              (vectorp (gnus-summary-article-header article))
10671              (save-excursion
10672                (gnus-cache-possibly-enter-article
10673                 gnus-newsgroup-name article
10674                 (= mark gnus-ticked-mark)
10675                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10676
10677         ;; Fix the mark.
10678         (gnus-summary-update-mark mark 'unread)
10679         t))))
10680
10681 (defun gnus-summary-mark-article (&optional article mark no-expire)
10682   "Mark ARTICLE with MARK.  MARK can be any character.
10683 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10684 `??' (dormant) and `?E' (expirable).
10685 If MARK is nil, then the default character `?r' is used.
10686 If ARTICLE is nil, then the article on the current line will be
10687 marked.
10688 If NO-EXPIRE, auto-expiry will be inhibited."
10689   ;; The mark might be a string.
10690   (when (stringp mark)
10691     (setq mark (aref mark 0)))
10692   ;; If no mark is given, then we check auto-expiring.
10693   (when (null mark)
10694     (setq mark gnus-del-mark))
10695   (when (and (not no-expire)
10696              gnus-newsgroup-auto-expire
10697              (memq mark gnus-auto-expirable-marks))
10698     (setq mark gnus-expirable-mark))
10699   (let ((article (or article (gnus-summary-article-number)))
10700         (old-mark (gnus-summary-article-mark article)))
10701     ;; Allow the backend to change the mark.
10702     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10703     (if (eq mark old-mark)
10704         t
10705       (unless article
10706         (error "No article on current line"))
10707       (if (not (if (or (= mark gnus-unread-mark)
10708                        (= mark gnus-ticked-mark)
10709                        (= mark gnus-spam-mark)
10710                        (= mark gnus-dormant-mark))
10711                    (gnus-mark-article-as-unread article mark)
10712                  (gnus-mark-article-as-read article mark)))
10713           t
10714         ;; See whether the article is to be put in the cache.
10715         (and gnus-use-cache
10716              (not (= mark gnus-canceled-mark))
10717              (vectorp (gnus-summary-article-header article))
10718              (save-excursion
10719                (gnus-cache-possibly-enter-article
10720                 gnus-newsgroup-name article
10721                 (= mark gnus-ticked-mark)
10722                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10723
10724         (when (gnus-summary-goto-subject article nil t)
10725           (let ((inhibit-read-only t))
10726             (gnus-summary-show-thread)
10727             ;; Fix the mark.
10728             (gnus-summary-update-mark mark 'unread)
10729             t))))))
10730
10731 (defun gnus-summary-update-secondary-mark (article)
10732   "Update the secondary (read, process, cache) mark."
10733   (gnus-summary-update-mark
10734    (cond ((memq article gnus-newsgroup-processable)
10735           gnus-process-mark)
10736          ((memq article gnus-newsgroup-cached)
10737           gnus-cached-mark)
10738          ((memq article gnus-newsgroup-replied)
10739           gnus-replied-mark)
10740          ((memq article gnus-newsgroup-forwarded)
10741           gnus-forwarded-mark)
10742          ((memq article gnus-newsgroup-saved)
10743           gnus-saved-mark)
10744          ((memq article gnus-newsgroup-recent)
10745           gnus-recent-mark)
10746          ((memq article gnus-newsgroup-unseen)
10747           gnus-unseen-mark)
10748          (t gnus-no-mark))
10749    'replied)
10750   (when (gnus-visual-p 'summary-highlight 'highlight)
10751     (gnus-run-hooks 'gnus-summary-update-hook))
10752   t)
10753
10754 (defun gnus-summary-update-download-mark (article)
10755   "Update the download mark."
10756   (gnus-summary-update-mark
10757    (cond ((memq article gnus-newsgroup-undownloaded)
10758           gnus-undownloaded-mark)
10759          (gnus-newsgroup-agentized
10760           gnus-downloaded-mark)
10761          (t
10762           gnus-no-mark))
10763    'download)
10764   (gnus-summary-update-line t)
10765   t)
10766
10767 (defun gnus-summary-update-mark (mark type)
10768   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10769         (inhibit-read-only t))
10770     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10771     (when forward
10772       (when (looking-at "\r")
10773         (incf forward))
10774       (when (<= (+ forward (point)) (point-max))
10775         ;; Go to the right position on the line.
10776         (goto-char (+ forward (point)))
10777         ;; Replace the old mark with the new mark.
10778         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10779         ;; Optionally update the marks by some user rule.
10780         (when (eq type 'unread)
10781           (gnus-data-set-mark
10782            (gnus-data-find (gnus-summary-article-number)) mark)
10783           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10784
10785 (defun gnus-mark-article-as-read (article &optional mark)
10786   "Enter ARTICLE in the pertinent lists and remove it from others."
10787   ;; Make the article expirable.
10788   (let ((mark (or mark gnus-del-mark)))
10789     (setq gnus-newsgroup-expirable
10790           (if (= mark gnus-expirable-mark)
10791               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10792             (delq article gnus-newsgroup-expirable)))
10793     ;; Remove from unread and marked lists.
10794     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10795     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10796     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10797     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10798     (push (cons article mark) gnus-newsgroup-reads)
10799     ;; Possibly remove from cache, if that is used.
10800     (when gnus-use-cache
10801       (gnus-cache-enter-remove-article article))
10802     t))
10803
10804 (defun gnus-mark-article-as-unread (article &optional mark)
10805   "Enter ARTICLE in the pertinent lists and remove it from others."
10806   (let ((mark (or mark gnus-ticked-mark)))
10807     (if (<= article 0)
10808         (progn
10809           (gnus-error 1 "Can't mark negative article numbers")
10810           nil)
10811       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10812             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10813             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10814             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10815             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10816
10817       ;; Unsuppress duplicates?
10818       (when gnus-suppress-duplicates
10819         (gnus-dup-unsuppress-article article))
10820
10821       (cond ((= mark gnus-ticked-mark)
10822              (setq gnus-newsgroup-marked
10823                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10824             ((= mark gnus-spam-mark)
10825              (setq gnus-newsgroup-spam-marked
10826                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10827                                             article)))
10828             ((= mark gnus-dormant-mark)
10829              (setq gnus-newsgroup-dormant
10830                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10831             (t
10832              (setq gnus-newsgroup-unreads
10833                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10834       (gnus-pull article gnus-newsgroup-reads)
10835       t)))
10836
10837 (defalias 'gnus-summary-mark-as-unread-forward
10838   'gnus-summary-tick-article-forward)
10839 (make-obsolete 'gnus-summary-mark-as-unread-forward
10840                'gnus-summary-tick-article-forward "Emacs 20.4")
10841 (defun gnus-summary-tick-article-forward (n)
10842   "Tick N articles forwards.
10843 If N is negative, tick backwards instead.
10844 The difference between N and the number of articles ticked is returned."
10845   (interactive "p")
10846   (gnus-summary-mark-forward n gnus-ticked-mark))
10847
10848 (defalias 'gnus-summary-mark-as-unread-backward
10849   'gnus-summary-tick-article-backward)
10850 (make-obsolete 'gnus-summary-mark-as-unread-backward
10851                'gnus-summary-tick-article-backward "Emacs 20.4")
10852 (defun gnus-summary-tick-article-backward (n)
10853   "Tick N articles backwards.
10854 The difference between N and the number of articles ticked is returned."
10855   (interactive "p")
10856   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10857
10858 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10859 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article "Emacs 20.4")
10860 (defun gnus-summary-tick-article (&optional article clear-mark)
10861   "Mark current article as unread.
10862 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10863 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10864   (interactive)
10865   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10866                                        gnus-ticked-mark)))
10867
10868 (defun gnus-summary-mark-as-read-forward (n)
10869   "Mark N articles as read forwards.
10870 If N is negative, mark backwards instead.
10871 The difference between N and the actual number of articles marked is
10872 returned."
10873   (interactive "p")
10874   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10875
10876 (defun gnus-summary-mark-as-read-backward (n)
10877   "Mark the N articles as read backwards.
10878 The difference between N and the actual number of articles marked is
10879 returned."
10880   (interactive "p")
10881   (gnus-summary-mark-forward
10882    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10883
10884 (defun gnus-summary-mark-as-read (&optional article mark)
10885   "Mark current article as read.
10886 ARTICLE specifies the article to be marked as read.
10887 MARK specifies a string to be inserted at the beginning of the line."
10888   (gnus-summary-mark-article article mark))
10889
10890 (defun gnus-summary-clear-mark-forward (n)
10891   "Clear marks from N articles forward.
10892 If N is negative, clear backward instead.
10893 The difference between N and the number of marks cleared is returned."
10894   (interactive "p")
10895   (gnus-summary-mark-forward n gnus-unread-mark))
10896
10897 (defun gnus-summary-clear-mark-backward (n)
10898   "Clear marks from N articles backward.
10899 The difference between N and the number of marks cleared is returned."
10900   (interactive "p")
10901   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10902
10903 (defun gnus-summary-mark-unread-as-read ()
10904   "Intended to be used by `gnus-mark-article-hook'."
10905   (when (memq gnus-current-article gnus-newsgroup-unreads)
10906     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10907
10908 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10909   "Intended to be used by `gnus-mark-article-hook'."
10910   (let ((mark (gnus-summary-article-mark)))
10911     (when (or (gnus-unread-mark-p mark)
10912               (gnus-read-mark-p mark))
10913       (gnus-summary-mark-article gnus-current-article
10914                                  (or new-mark gnus-read-mark)))))
10915
10916 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10917   "Intended to be used by `gnus-mark-article-hook'."
10918   (let ((mark (gnus-summary-article-mark)))
10919     (when (or (gnus-unread-mark-p mark)
10920               (gnus-read-mark-p mark))
10921       (gnus-summary-mark-article (gnus-summary-article-number)
10922                                  (or new-mark gnus-read-mark)))))
10923
10924 (defun gnus-summary-mark-unread-as-ticked ()
10925   "Intended to be used by `gnus-mark-article-hook'."
10926   (when (memq gnus-current-article gnus-newsgroup-unreads)
10927     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10928
10929 (defun gnus-summary-mark-region-as-read (point mark all)
10930   "Mark all unread articles between point and mark as read.
10931 If given a prefix, mark all articles between point and mark as read,
10932 even ticked and dormant ones."
10933   (interactive "r\nP")
10934   (save-excursion
10935     (let (article)
10936       (goto-char point)
10937       (beginning-of-line)
10938       (while (and
10939               (< (point) mark)
10940               (progn
10941                 (when (or all
10942                           (memq (setq article (gnus-summary-article-number))
10943                                 gnus-newsgroup-unreads))
10944                   (gnus-summary-mark-article article gnus-del-mark))
10945                 t)
10946               (gnus-summary-find-next))))))
10947
10948 (defun gnus-summary-mark-below (score mark)
10949   "Mark articles with score less than SCORE with MARK."
10950   (interactive "P\ncMark: ")
10951   (setq score (if score
10952                   (prefix-numeric-value score)
10953                 (or gnus-summary-default-score 0)))
10954   (with-current-buffer gnus-summary-buffer
10955     (goto-char (point-min))
10956     (while
10957         (progn
10958           (and (< (gnus-summary-article-score) score)
10959                (gnus-summary-mark-article nil mark))
10960           (gnus-summary-find-next)))))
10961
10962 (defun gnus-summary-kill-below (&optional score)
10963   "Mark articles with score below SCORE as read."
10964   (interactive "P")
10965   (gnus-summary-mark-below score gnus-killed-mark))
10966
10967 (defun gnus-summary-clear-above (&optional score)
10968   "Clear all marks from articles with score above SCORE."
10969   (interactive "P")
10970   (gnus-summary-mark-above score gnus-unread-mark))
10971
10972 (defun gnus-summary-tick-above (&optional score)
10973   "Tick all articles with score above SCORE."
10974   (interactive "P")
10975   (gnus-summary-mark-above score gnus-ticked-mark))
10976
10977 (defun gnus-summary-mark-above (score mark)
10978   "Mark articles with score over SCORE with MARK."
10979   (interactive "P\ncMark: ")
10980   (setq score (if score
10981                   (prefix-numeric-value score)
10982                 (or gnus-summary-default-score 0)))
10983   (with-current-buffer gnus-summary-buffer
10984     (goto-char (point-min))
10985     (while (and (progn
10986                   (when (> (gnus-summary-article-score) score)
10987                     (gnus-summary-mark-article nil mark))
10988                   t)
10989                 (gnus-summary-find-next)))))
10990
10991 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10992 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10993 (defun gnus-summary-limit-include-expunged (&optional no-error)
10994   "Display all the hidden articles that were expunged for low scores."
10995   (interactive)
10996   (let ((inhibit-read-only t))
10997     (let ((scored gnus-newsgroup-scored)
10998           headers h)
10999       (while scored
11000         (unless (gnus-summary-article-header (caar scored))
11001           (and (setq h (gnus-number-to-header (caar scored)))
11002                (< (cdar scored) gnus-summary-expunge-below)
11003                (push h headers)))
11004         (setq scored (cdr scored)))
11005       (if (not headers)
11006           (when (not no-error)
11007             (error "No expunged articles hidden"))
11008         (goto-char (point-min))
11009         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11010         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11011         (dolist (x headers)
11012           (push (mail-header-number x) gnus-newsgroup-limit))
11013         (gnus-summary-prepare-unthreaded (nreverse headers))
11014         (goto-char (point-min))
11015         (gnus-summary-position-point)
11016         t))))
11017
11018 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11019   "Mark all unread articles in this newsgroup as read.
11020 If prefix argument ALL is non-nil, ticked and dormant articles will
11021 also be marked as read.
11022 If QUIETLY is non-nil, no questions will be asked.
11023
11024 If TO-HERE is non-nil, it should be a point in the buffer.  All
11025 articles before (after, if REVERSE is set) this point will be marked
11026 as read.
11027
11028 Note that this function will only catch up the unread article
11029 in the current summary buffer limitation.
11030
11031 The number of articles marked as read is returned."
11032   (interactive "P")
11033   (prog1
11034       (save-excursion
11035         (when (or quietly
11036                   (not gnus-interactive-catchup) ;Without confirmation?
11037                   gnus-expert-user
11038                   (gnus-y-or-n-p
11039                    (if all
11040                        "Mark absolutely all articles as read? "
11041                      "Mark all unread articles as read? ")))
11042           (if (and not-mark
11043                    (not gnus-newsgroup-adaptive)
11044                    (not gnus-newsgroup-auto-expire)
11045                    (not gnus-suppress-duplicates)
11046                    (or (not gnus-use-cache)
11047                        (eq gnus-use-cache 'passive)))
11048               (progn
11049                 (when all
11050                   (setq gnus-newsgroup-marked nil
11051                         gnus-newsgroup-spam-marked nil
11052                         gnus-newsgroup-dormant nil))
11053                 (setq gnus-newsgroup-unreads
11054                       (gnus-sorted-nunion
11055                        (gnus-sorted-intersection gnus-newsgroup-unreads
11056                                                  gnus-newsgroup-downloadable)
11057                        (gnus-sorted-difference gnus-newsgroup-unfetched
11058                                                gnus-newsgroup-cached))))
11059             ;; We actually mark all articles as canceled, which we
11060             ;; have to do when using auto-expiry or adaptive scoring.
11061             (gnus-summary-show-all-threads)
11062             (if (and to-here reverse)
11063                 (progn
11064                   (goto-char to-here)
11065                   (gnus-summary-mark-current-read-and-unread-as-read
11066                    gnus-catchup-mark)
11067                   (while (gnus-summary-find-next (not all))
11068                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11069               (when (gnus-summary-first-subject (not all))
11070                 (while (and
11071                         (if to-here (< (point) to-here) t)
11072                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11073                         (gnus-summary-find-next (not all))))))
11074             (gnus-set-mode-line 'summary))
11075           t))
11076     (gnus-summary-position-point)))
11077
11078 (defun gnus-summary-catchup-to-here (&optional all)
11079   "Mark all unticked articles before the current one as read.
11080 If ALL is non-nil, also mark ticked and dormant articles as read."
11081   (interactive "P")
11082   (save-excursion
11083     (gnus-save-hidden-threads
11084       (let ((beg (point)))
11085         ;; We check that there are unread articles.
11086         (when (or all (gnus-summary-find-prev))
11087           (gnus-summary-catchup all t beg)))))
11088   (gnus-summary-position-point))
11089
11090 (defun gnus-summary-catchup-from-here (&optional all)
11091   "Mark all unticked articles after (and including) the current one as read.
11092 If ALL is non-nil, also mark ticked and dormant articles as read."
11093   (interactive "P")
11094   (save-excursion
11095     (gnus-save-hidden-threads
11096       (let ((beg (point)))
11097         ;; We check that there are unread articles.
11098         (when (or all (gnus-summary-find-next))
11099           (gnus-summary-catchup all t beg nil t)))))
11100   (gnus-summary-position-point))
11101
11102 (defun gnus-summary-catchup-all (&optional quietly)
11103   "Mark all articles in this newsgroup as read.
11104 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11105 instead, which marks only unread articles as read."
11106   (interactive "P")
11107   (gnus-summary-catchup t quietly))
11108
11109 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11110   "Mark all unread articles in this group as read, then exit.
11111 If prefix argument ALL is non-nil, all articles are marked as read.
11112 If QUIETLY is non-nil, no questions will be asked."
11113   (interactive "P")
11114   (when (gnus-summary-catchup all quietly nil 'fast)
11115     ;; Select next newsgroup or exit.
11116     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11117              (eq gnus-auto-select-next 'quietly))
11118         (gnus-summary-next-group nil)
11119       (gnus-summary-exit))))
11120
11121 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11122   "Mark all articles in this newsgroup as read, and then exit.
11123 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11124 instead, which marks only unread articles as read."
11125   (interactive "P")
11126   (gnus-summary-catchup-and-exit t quietly))
11127
11128 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11129   "Mark all articles in this group as read and select the next group.
11130 If given a prefix, mark all articles, unread as well as ticked, as
11131 read."
11132   (interactive "P")
11133   (save-excursion
11134     (gnus-summary-catchup all))
11135   (gnus-summary-next-group))
11136
11137 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11138   "Mark all articles in this group as read and select the previous group.
11139 If given a prefix, mark all articles, unread as well as ticked, as
11140 read."
11141   (interactive "P")
11142   (save-excursion
11143     (gnus-summary-catchup all))
11144   (gnus-summary-next-group nil nil t))
11145
11146 ;;;
11147 ;;; with article
11148 ;;;
11149
11150 (defmacro gnus-with-article (article &rest forms)
11151   "Select ARTICLE and perform FORMS in the original article buffer.
11152 Then replace the article with the result."
11153   `(progn
11154      ;; We don't want the article to be marked as read.
11155      (let (gnus-mark-article-hook)
11156        (gnus-summary-select-article t t nil ,article))
11157      (set-buffer gnus-original-article-buffer)
11158      ,@forms
11159      (if (not (gnus-check-backend-function
11160                'request-replace-article (car gnus-article-current)))
11161          (gnus-message 5 "Read-only group; not replacing")
11162        (unless (gnus-request-replace-article
11163                 ,article (car gnus-article-current)
11164                 (current-buffer) t)
11165          (error "Couldn't replace article")))
11166      ;; The cache and backlog have to be flushed somewhat.
11167      (when gnus-keep-backlog
11168        (gnus-backlog-remove-article
11169         (car gnus-article-current) (cdr gnus-article-current)))
11170      (when gnus-use-cache
11171        (gnus-cache-update-article
11172         (car gnus-article-current) (cdr gnus-article-current)))))
11173
11174 (put 'gnus-with-article 'lisp-indent-function 1)
11175 (put 'gnus-with-article 'edebug-form-spec '(form body))
11176
11177 ;; Thread-based commands.
11178
11179 (defun gnus-summary-articles-in-thread (&optional article)
11180   "Return a list of all articles in the current thread.
11181 If ARTICLE is non-nil, return all articles in the thread that starts
11182 with that article."
11183   (let* ((article (or article (gnus-summary-article-number)))
11184          (data (gnus-data-find-list article))
11185          (top-level (gnus-data-level (car data)))
11186          (top-subject
11187           (cond ((null gnus-thread-operation-ignore-subject)
11188                  (gnus-simplify-subject-re
11189                   (mail-header-subject (gnus-data-header (car data)))))
11190                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11191                  (gnus-simplify-subject-fuzzy
11192                   (mail-header-subject (gnus-data-header (car data)))))
11193                 (t nil)))
11194          (end-point (save-excursion
11195                       (if (gnus-summary-go-to-next-thread)
11196                           (point) (point-max))))
11197          articles)
11198     (while (and data
11199                 (< (gnus-data-pos (car data)) end-point))
11200       (when (or (not top-subject)
11201                 (string= top-subject
11202                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11203                              (gnus-simplify-subject-fuzzy
11204                               (mail-header-subject
11205                                (gnus-data-header (car data))))
11206                            (gnus-simplify-subject-re
11207                             (mail-header-subject
11208                              (gnus-data-header (car data)))))))
11209         (push (gnus-data-number (car data)) articles))
11210       (unless (and (setq data (cdr data))
11211                    (> (gnus-data-level (car data)) top-level))
11212         (setq data nil)))
11213     ;; Return the list of articles.
11214     (nreverse articles)))
11215
11216 (defun gnus-summary-rethread-current ()
11217   "Rethread the thread the current article is part of."
11218   (interactive)
11219   (let* ((gnus-show-threads t)
11220          (article (gnus-summary-article-number))
11221          (id (mail-header-id (gnus-summary-article-header)))
11222          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11223     (unless id
11224       (error "No article on the current line"))
11225     (gnus-rebuild-thread id)
11226     (gnus-summary-goto-subject article)))
11227
11228 (defun gnus-summary-reparent-thread ()
11229   "Make the current article child of the marked (or previous) article.
11230
11231 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11232 is non-nil or the Subject: of both articles are the same."
11233   (interactive)
11234   (unless (not (gnus-group-read-only-p))
11235     (error "The current newsgroup does not support article editing"))
11236   (unless (<= (length gnus-newsgroup-processable) 1)
11237     (error "No more than one article may be marked"))
11238   (let ((child (gnus-summary-article-number))
11239         ;; First grab the marked article, otherwise one line up.
11240         (parent (if (not (null gnus-newsgroup-processable))
11241                     (car gnus-newsgroup-processable)
11242                   (save-excursion
11243                     (if (eq (forward-line -1) 0)
11244                         (gnus-summary-article-number)
11245                       (error "Beginning of summary buffer"))))))
11246     (gnus-summary-reparent-children parent (list child))))
11247
11248 (defun gnus-summary-reparent-children (parent children)
11249   "Make PARENT the parent of CHILDREN.
11250 When called interactively, PARENT is the current article and CHILDREN
11251 are the process-marked articles."
11252   (interactive
11253    (list (gnus-summary-article-number)
11254          (gnus-summary-work-articles nil)))
11255   (dolist (child children)
11256     (save-window-excursion
11257       (let ((gnus-article-buffer " *reparent*"))
11258         (unless (not (eq parent child))
11259           (error "An article may not be self-referential"))
11260         (let ((message-id (mail-header-id
11261                            (gnus-summary-article-header parent))))
11262           (unless (and message-id (not (equal message-id "")))
11263             (error "No message-id in desired parent"))
11264           (gnus-with-article child
11265             (save-restriction
11266               (goto-char (point-min))
11267               (message-narrow-to-head)
11268               (if (re-search-forward "^References: " nil t)
11269                   (progn
11270                     (re-search-forward "^[^ \t]" nil t)
11271                     (forward-line -1)
11272                     (end-of-line)
11273                     (insert " " message-id))
11274                 (insert "References: " message-id "\n"))))
11275           (set-buffer gnus-summary-buffer)
11276           (gnus-summary-unmark-all-processable)
11277           (gnus-summary-update-article child)
11278           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11279             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11280           (gnus-summary-rethread-current)
11281           (gnus-message 3 "Article %d is now the child of article %d"
11282                         child parent))))))
11283
11284 (defun gnus-summary-toggle-threads (&optional arg)
11285   "Toggle showing conversation threads.
11286 If ARG is positive number, turn showing conversation threads on."
11287   (interactive "P")
11288   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11289     (setq gnus-show-threads
11290           (if (null arg) (not gnus-show-threads)
11291             (> (prefix-numeric-value arg) 0)))
11292     (gnus-summary-prepare)
11293     (gnus-summary-goto-subject current)
11294     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11295     (gnus-summary-position-point)))
11296
11297 (eval-and-compile
11298   (if (fboundp 'remove-overlays)
11299       (defalias 'gnus-remove-overlays 'remove-overlays)
11300     (defun gnus-remove-overlays (beg end name val)
11301       "Clear BEG and END of overlays whose property NAME has value VAL.
11302 For compatibility with Emacs 21 and XEmacs."
11303       (dolist (ov (gnus-overlays-in beg end))
11304         (when (eq (gnus-overlay-get ov name) val)
11305           (gnus-delete-overlay ov))))))
11306
11307 (defun gnus-summary-show-all-threads ()
11308   "Show all threads."
11309   (interactive)
11310   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11311   (gnus-summary-position-point))
11312
11313 (defun gnus-summary-show-thread ()
11314   "Show thread subtrees.
11315 Returns nil if no thread was there to be shown."
11316   (interactive)
11317   (let* ((orig (point))
11318          (end (point-at-eol))
11319          ;; Leave point at bol
11320          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11321          (eoi (when (eq (get-char-property end 'invisible) 'gnus-sum)
11322                 (if (fboundp 'next-single-char-property-change)
11323                     (or (next-single-char-property-change end 'invisible)
11324                         (point-max))
11325                   (while (progn
11326                            (end-of-line 2)
11327                            (and (not (eobp))
11328                                 (eq (get-char-property (point) 'invisible)
11329                                     'gnus-sum))))
11330                   (point)))))
11331     (when eoi
11332       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11333       (goto-char orig)
11334       (gnus-summary-position-point)
11335       eoi)))
11336
11337 (defun gnus-summary-maybe-hide-threads ()
11338   "If requested, hide the threads that should be hidden."
11339   (when (and gnus-show-threads
11340              gnus-thread-hide-subtree)
11341     (gnus-summary-hide-all-threads
11342      (if (or (consp gnus-thread-hide-subtree)
11343              (functionp gnus-thread-hide-subtree))
11344          (gnus-make-predicate gnus-thread-hide-subtree)
11345        nil))))
11346
11347 ;;; Hiding predicates.
11348
11349 (defun gnus-article-unread-p (header)
11350   (memq (mail-header-number header) gnus-newsgroup-unreads))
11351
11352 (defun gnus-article-unseen-p (header)
11353   (memq (mail-header-number header) gnus-newsgroup-unseen))
11354
11355 (defun gnus-map-articles (predicate articles)
11356   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11357   (apply 'gnus-or (mapcar predicate
11358                           (mapcar (lambda (number)
11359                                     (gnus-summary-article-header number))
11360                                   articles))))
11361
11362 (defun gnus-summary-hide-all-threads (&optional predicate)
11363   "Hide all thread subtrees.
11364 If PREDICATE is supplied, threads that satisfy this predicate
11365 will not be hidden."
11366   (interactive)
11367   (save-excursion
11368     (goto-char (point-min))
11369     (let ((end nil))
11370       (while (not end)
11371         (when (or (not predicate)
11372                   (gnus-map-articles
11373                    predicate (gnus-summary-article-children)))
11374             (gnus-summary-hide-thread))
11375         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11376   (gnus-summary-position-point))
11377
11378 (defun gnus-summary-hide-thread ()
11379   "Hide thread subtrees.
11380 If PREDICATE is supplied, threads that satisfy this predicate
11381 will not be hidden.
11382 Returns nil if no threads were there to be hidden."
11383   (interactive)
11384   (let ((start (point))
11385         (starteol (line-end-position))
11386         (article (gnus-summary-article-number)))
11387     (goto-char start)
11388     ;; Go forward until either the buffer ends or the subthread ends.
11389     (when (and (not (eobp))
11390                (or (zerop (gnus-summary-next-thread 1 t))
11391                    (goto-char (point-max))))
11392       (if (and (> (point) start)
11393                ;; FIXME: this should actually search for a non-invisible \n.
11394                (search-backward "\n" start t))
11395           (progn
11396             (when (> (point) starteol)
11397               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11398               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11399                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11400                 (gnus-overlay-put ol 'evaporate t)))
11401             (gnus-summary-goto-subject article))
11402         (goto-char start)
11403         nil))))
11404
11405 (defun gnus-summary-go-to-next-thread (&optional previous)
11406   "Go to the same level (or less) next thread.
11407 If PREVIOUS is non-nil, go to previous thread instead.
11408 Return the article number moved to, or nil if moving was impossible."
11409   (let ((level (gnus-summary-thread-level))
11410         (way (if previous -1 1))
11411         (beg (point)))
11412     (forward-line way)
11413     (while (and (not (eobp))
11414                 (< level (gnus-summary-thread-level)))
11415       (forward-line way))
11416     (if (eobp)
11417         (progn
11418           (goto-char beg)
11419           nil)
11420       (setq beg (point))
11421       (prog1
11422           (gnus-summary-article-number)
11423         (goto-char beg)))))
11424
11425 (defun gnus-summary-next-thread (n &optional silent)
11426   "Go to the same level next N'th thread.
11427 If N is negative, search backward instead.
11428 Returns the difference between N and the number of skips actually
11429 done.
11430
11431 If SILENT, don't output messages."
11432   (interactive "p")
11433   (let ((backward (< n 0))
11434         (n (abs n)))
11435     (while (and (> n 0)
11436                 (gnus-summary-go-to-next-thread backward))
11437       (decf n))
11438     (unless silent
11439       (gnus-summary-position-point))
11440     (when (and (not silent) (/= 0 n))
11441       (gnus-message 7 "No more threads"))
11442     n))
11443
11444 (defun gnus-summary-prev-thread (n)
11445   "Go to the same level previous N'th thread.
11446 Returns the difference between N and the number of skips actually
11447 done."
11448   (interactive "p")
11449   (gnus-summary-next-thread (- n)))
11450
11451 (defun gnus-summary-go-down-thread ()
11452   "Go down one level in the current thread."
11453   (let ((children (gnus-summary-article-children)))
11454     (when children
11455       (gnus-summary-goto-subject (car children)))))
11456
11457 (defun gnus-summary-go-up-thread ()
11458   "Go up one level in the current thread."
11459   (let ((parent (gnus-summary-article-parent)))
11460     (when parent
11461       (gnus-summary-goto-subject parent))))
11462
11463 (defun gnus-summary-down-thread (n)
11464   "Go down thread N steps.
11465 If N is negative, go up instead.
11466 Returns the difference between N and how many steps down that were
11467 taken."
11468   (interactive "p")
11469   (let ((up (< n 0))
11470         (n (abs n)))
11471     (while (and (> n 0)
11472                 (if up (gnus-summary-go-up-thread)
11473                   (gnus-summary-go-down-thread)))
11474       (setq n (1- n)))
11475     (gnus-summary-position-point)
11476     (when (/= 0 n)
11477       (gnus-message 7 "Can't go further"))
11478     n))
11479
11480 (defun gnus-summary-up-thread (n)
11481   "Go up thread N steps.
11482 If N is negative, go down instead.
11483 Returns the difference between N and how many steps down that were
11484 taken."
11485   (interactive "p")
11486   (gnus-summary-down-thread (- n)))
11487
11488 (defun gnus-summary-top-thread ()
11489   "Go to the top of the thread."
11490   (interactive)
11491   (while (gnus-summary-go-up-thread))
11492   (gnus-summary-article-number))
11493
11494 (defun gnus-summary-expire-thread ()
11495   "Mark articles under current thread as expired."
11496   (interactive)
11497   (gnus-summary-kill-thread 0))
11498
11499 (defun gnus-summary-kill-thread (&optional unmark)
11500   "Mark articles under current thread as read.
11501 If the prefix argument is positive, remove any kinds of marks.
11502 If the prefix argument is zero, mark thread as expired.
11503 If the prefix argument is negative, tick articles instead."
11504   (interactive "P")
11505   (when unmark
11506     (setq unmark (prefix-numeric-value unmark)))
11507   (let ((articles (gnus-summary-articles-in-thread))
11508         (hide (or (null unmark) (= unmark 0))))
11509     (save-excursion
11510       ;; Expand the thread.
11511       (gnus-summary-show-thread)
11512       ;; Mark all the articles.
11513       (while articles
11514         (gnus-summary-goto-subject (car articles))
11515         (cond ((null unmark)
11516                (gnus-summary-mark-article-as-read gnus-killed-mark))
11517               ((> unmark 0)
11518                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11519               ((= unmark 0)
11520                (gnus-summary-mark-article-as-unread gnus-expirable-mark))
11521               (t
11522                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11523         (setq articles (cdr articles))))
11524     ;; Hide killed subtrees when hide is true.
11525     (and hide
11526          gnus-thread-hide-killed
11527          (gnus-summary-hide-thread))
11528     ;; If hide is t, go to next unread subject.
11529     (when hide
11530       ;; Go to next unread subject.
11531       (gnus-summary-next-subject 1 t)))
11532   (gnus-set-mode-line 'summary))
11533
11534 ;; Summary sorting commands
11535
11536 (defun gnus-summary-sort-by-number (&optional reverse)
11537   "Sort the summary buffer by article number.
11538 Argument REVERSE means reverse order."
11539   (interactive "P")
11540   (gnus-summary-sort 'number reverse))
11541
11542 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11543   "Sort the summary buffer by most recent article number.
11544 Argument REVERSE means reverse order."
11545   (interactive "P")
11546   (gnus-summary-sort 'most-recent-number reverse))
11547
11548 (defun gnus-summary-sort-by-random (&optional reverse)
11549   "Randomize the order in the summary buffer.
11550 Argument REVERSE means to randomize in reverse order."
11551   (interactive "P")
11552   (gnus-summary-sort 'random reverse))
11553
11554 (defun gnus-summary-sort-by-author (&optional reverse)
11555   "Sort the summary buffer by author name alphabetically.
11556 If `case-fold-search' is non-nil, case of letters is ignored.
11557 Argument REVERSE means reverse order."
11558   (interactive "P")
11559   (gnus-summary-sort 'author reverse))
11560
11561 (defun gnus-summary-sort-by-recipient (&optional reverse)
11562   "Sort the summary buffer by recipient name alphabetically.
11563 If `case-fold-search' is non-nil, case of letters is ignored.
11564 Argument REVERSE means reverse order."
11565   (interactive "P")
11566   (gnus-summary-sort 'recipient reverse))
11567
11568 (defun gnus-summary-sort-by-subject (&optional reverse)
11569   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11570 If `case-fold-search' is non-nil, case of letters is ignored.
11571 Argument REVERSE means reverse order."
11572   (interactive "P")
11573   (gnus-summary-sort 'subject reverse))
11574
11575 (defun gnus-summary-sort-by-date (&optional reverse)
11576   "Sort the summary buffer by date.
11577 Argument REVERSE means reverse order."
11578   (interactive "P")
11579   (gnus-summary-sort 'date reverse))
11580
11581 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11582   "Sort the summary buffer by most recent date.
11583 Argument REVERSE means reverse order."
11584   (interactive "P")
11585   (gnus-summary-sort 'most-recent-date reverse))
11586
11587 (defun gnus-summary-sort-by-score (&optional reverse)
11588   "Sort the summary buffer by score.
11589 Argument REVERSE means reverse order."
11590   (interactive "P")
11591   (gnus-summary-sort 'score reverse))
11592
11593 (defun gnus-summary-sort-by-lines (&optional reverse)
11594   "Sort the summary buffer by the number of lines.
11595 Argument REVERSE means reverse order."
11596   (interactive "P")
11597   (gnus-summary-sort 'lines reverse))
11598
11599 (defun gnus-summary-sort-by-chars (&optional reverse)
11600   "Sort the summary buffer by article length.
11601 Argument REVERSE means reverse order."
11602   (interactive "P")
11603   (gnus-summary-sort 'chars reverse))
11604
11605 (defun gnus-summary-sort-by-original (&optional reverse)
11606   "Sort the summary buffer using the default sorting method.
11607 Argument REVERSE means reverse order."
11608   (interactive "P")
11609   (let* ((inhibit-read-only t)
11610          (gnus-summary-prepare-hook nil))
11611     ;; We do the sorting by regenerating the threads.
11612     (gnus-summary-prepare)
11613     ;; Hide subthreads if needed.
11614     (gnus-summary-maybe-hide-threads)))
11615
11616 (defun gnus-summary-sort (predicate reverse)
11617   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11618   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11619          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11620          (gnus-thread-sort-functions
11621           (if (not reverse)
11622               thread
11623             `(lambda (t1 t2)
11624                (,thread t2 t1))))
11625          (gnus-sort-gathered-threads-function
11626           gnus-thread-sort-functions)
11627          (gnus-article-sort-functions
11628           (if (not reverse)
11629               article
11630             `(lambda (t1 t2)
11631                (,article t2 t1))))
11632          (inhibit-read-only t)
11633          (gnus-summary-prepare-hook nil))
11634     ;; We do the sorting by regenerating the threads.
11635     (gnus-summary-prepare)
11636     ;; Hide subthreads if needed.
11637     (gnus-summary-maybe-hide-threads)))
11638
11639 ;; Summary saving commands.
11640
11641 (defun gnus-summary-save-article (&optional n not-saved)
11642   "Save the current article using the default saver function.
11643 If N is a positive number, save the N next articles.
11644 If N is a negative number, save the N previous articles.
11645 If N is nil and any articles have been marked with the process mark,
11646 save those articles instead.
11647 The variable `gnus-default-article-saver' specifies the saver function.
11648
11649 If the optional second argument NOT-SAVED is non-nil, articles saved
11650 will not be marked as saved."
11651   (interactive "P")
11652   (require 'gnus-art)
11653   (let* ((articles (gnus-summary-work-articles n))
11654          (save-buffer (save-excursion
11655                         (nnheader-set-temp-buffer " *Gnus Save*")))
11656          (num (length articles))
11657          ;; Whether to save decoded articles or raw articles.
11658          (decode (when gnus-article-save-coding-system
11659                    (get gnus-default-article-saver :decode)))
11660          ;; When saving many articles in a single file, use the other
11661          ;; function to save articles other than the first one.
11662          (saver2 (get gnus-default-article-saver :function))
11663          (gnus-prompt-before-saving (if saver2
11664                                         t
11665                                       gnus-prompt-before-saving))
11666          (gnus-default-article-saver gnus-default-article-saver)
11667          header file)
11668     (dolist (article articles)
11669       (setq header (gnus-summary-article-header article))
11670       (if (not (vectorp header))
11671           ;; This is a pseudo-article.
11672           (if (assq 'name header)
11673               (gnus-copy-file (cdr (assq 'name header)))
11674             (gnus-message 1 "Article %d is unsaveable" article))
11675         ;; This is a real article.
11676         (save-window-excursion
11677           (let ((gnus-display-mime-function (when decode
11678                                               gnus-display-mime-function))
11679                 (gnus-article-prepare-hook (when decode
11680                                              gnus-article-prepare-hook)))
11681             (gnus-summary-select-article t t nil article)
11682             (gnus-summary-goto-subject article)))
11683         (with-current-buffer save-buffer
11684           (erase-buffer)
11685           (insert-buffer-substring (if decode
11686                                        gnus-article-buffer
11687                                      gnus-original-article-buffer)))
11688         (setq file (gnus-article-save save-buffer file num))
11689         (gnus-summary-remove-process-mark article)
11690         (unless not-saved
11691           (gnus-summary-set-saved-mark article)))
11692       (when saver2
11693         (setq gnus-default-article-saver saver2
11694               saver2 nil)))
11695     (gnus-kill-buffer save-buffer)
11696     (gnus-summary-position-point)
11697     (gnus-set-mode-line 'summary)
11698     n))
11699
11700 (defun gnus-summary-pipe-output (&optional n sym)
11701   "Pipe the current article to a subprocess.
11702 If N is a positive number, pipe the N next articles.
11703 If N is a negative number, pipe the N previous articles.
11704 If N is nil and any articles have been marked with the process mark,
11705 pipe those articles instead.
11706 The default command to which articles are piped is specified by the
11707 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
11708 will be prompted for the command.
11709
11710 The properties `:decode' and `:headers' that are put to the function
11711 symbol `gnus-summary-save-in-pipe' control whether this function
11712 decodes articles and what headers to keep (see the doc string for the
11713 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
11714 is neither omitted nor the symbol `r', force including all headers
11715 regardless of the `:headers' property.  If it is the symbol `r',
11716 articles that are not decoded and include all headers will be piped
11717 no matter what the properties `:decode' and `:headers' are."
11718   (interactive (gnus-interactive "P\ny"))
11719   (require 'gnus-art)
11720   (let* ((articles (gnus-summary-work-articles n))
11721          (result-buffer "*Shell Command Output*")
11722          (all-headers (not (memq sym '(nil r))))
11723          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
11724          (raw (eq sym 'r))
11725          (headers (get 'gnus-summary-save-in-pipe :headers))
11726          command result)
11727     (unless (numberp (car articles))
11728       (error "No article to pipe"))
11729     (setq command (gnus-read-shell-command
11730                    (concat "Shell command on "
11731                            (if (cdr articles)
11732                                (format "these %d articles" (length articles))
11733                              "this article")
11734                            ": ")
11735                    gnus-summary-pipe-output-default-command))
11736     (when (string-equal command "")
11737       (error "A command is required"))
11738     (when all-headers
11739       (put 'gnus-summary-save-in-pipe :headers nil))
11740     (unwind-protect
11741         (while articles
11742           (gnus-summary-goto-subject (pop articles))
11743           (save-window-excursion (gnus-summary-save-in-pipe command raw))
11744           (when (and (get-buffer result-buffer)
11745                      (not (zerop (buffer-size (get-buffer result-buffer)))))
11746             (setq result (concat result (with-current-buffer result-buffer
11747                                           (buffer-string))))))
11748       (put 'gnus-summary-save-in-pipe :headers headers))
11749     (unless (zerop (length result))
11750       (if (with-current-buffer (get-buffer-create result-buffer)
11751             (erase-buffer)
11752             (insert result)
11753             (prog1
11754                 (and (= (count-lines (point-min) (point)) 1)
11755                      (progn
11756                        (end-of-line 0)
11757                        (<= (current-column)
11758                            (window-width (minibuffer-window)))))
11759               (goto-char (point-min))))
11760           (message "%s" (substring result 0 -1))
11761         (message nil)
11762         (gnus-configure-windows 'pipe)))))
11763
11764 (defun gnus-summary-save-article-mail (&optional arg)
11765   "Append the current article to a Unix mail box file.
11766 If N is a positive number, save the N next articles.
11767 If N is a negative number, save the N previous articles.
11768 If N is nil and any articles have been marked with the process mark,
11769 save those articles instead."
11770   (interactive "P")
11771   (require 'gnus-art)
11772   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11773     (gnus-summary-save-article arg)))
11774
11775 (defun gnus-summary-save-article-rmail (&optional arg)
11776   "Append the current article to an rmail file.
11777 If N is a positive number, save the N next articles.
11778 If N is a negative number, save the N previous articles.
11779 If N is nil and any articles have been marked with the process mark,
11780 save those articles instead."
11781   (interactive "P")
11782   (require 'gnus-art)
11783   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11784     (gnus-summary-save-article arg)))
11785
11786 (defun gnus-summary-save-article-file (&optional arg)
11787   "Append the current article to a file.
11788 If N is a positive number, save the N next articles.
11789 If N is a negative number, save the N previous articles.
11790 If N is nil and any articles have been marked with the process mark,
11791 save those articles instead."
11792   (interactive "P")
11793   (require 'gnus-art)
11794   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11795     (gnus-summary-save-article arg)))
11796
11797 (defun gnus-summary-write-article-file (&optional arg)
11798   "Write the current article to a file, deleting the previous file.
11799 If N is a positive number, save the N next articles.
11800 If N is a negative number, save the N previous articles.
11801 If N is nil and any articles have been marked with the process mark,
11802 save those articles instead."
11803   (interactive "P")
11804   (require 'gnus-art)
11805   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11806     (gnus-summary-save-article arg)))
11807
11808 (defun gnus-summary-save-article-body-file (&optional arg)
11809   "Append the current article body to a file.
11810 If N is a positive number, save the N next articles.
11811 If N is a negative number, save the N previous articles.
11812 If N is nil and any articles have been marked with the process mark,
11813 save those articles instead."
11814   (interactive "P")
11815   (require 'gnus-art)
11816   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11817     (gnus-summary-save-article arg)))
11818
11819 (defun gnus-summary-write-article-body-file (&optional arg)
11820   "Write the current article body to a file, deleting the previous file.
11821 If N is a positive number, save the N next articles.
11822 If N is a negative number, save the N previous articles.
11823 If N is nil and any articles have been marked with the process mark,
11824 save those articles instead."
11825   (interactive "P")
11826   (require 'gnus-art)
11827   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11828     (gnus-summary-save-article arg)))
11829
11830 (defun gnus-summary-muttprint (&optional arg)
11831   "Print the current article using Muttprint.
11832 If N is a positive number, save the N next articles.
11833 If N is a negative number, save the N previous articles.
11834 If N is nil and any articles have been marked with the process mark,
11835 save those articles instead."
11836   (interactive "P")
11837   (require 'gnus-art)
11838   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11839     (gnus-summary-save-article arg t)))
11840
11841 (defun gnus-summary-pipe-message (program)
11842   "Pipe the current article through PROGRAM."
11843   (interactive "sProgram: ")
11844   (gnus-summary-select-article)
11845   (let ((mail-header-separator ""))
11846     (gnus-eval-in-buffer-window gnus-article-buffer
11847       (save-restriction
11848         (widen)
11849         (let ((start (window-start))
11850               (inhibit-read-only t))
11851           (message-pipe-buffer-body program)
11852           (set-window-start (get-buffer-window (current-buffer)) start))))))
11853
11854 (defun gnus-get-split-value (methods)
11855   "Return a value based on the split METHODS."
11856   (let (split-name method result match)
11857     (when methods
11858       (with-current-buffer gnus-original-article-buffer
11859         (save-restriction
11860           (nnheader-narrow-to-headers)
11861           (while (and methods (not split-name))
11862             (goto-char (point-min))
11863             (setq method (pop methods))
11864             (setq match (car method))
11865             (when (cond
11866                    ((stringp match)
11867                     ;; Regular expression.
11868                     (ignore-errors
11869                       (re-search-forward match nil t)))
11870                    ((functionp match)
11871                     ;; Function.
11872                     (save-restriction
11873                       (widen)
11874                       (setq result (funcall match gnus-newsgroup-name))))
11875                    ((consp match)
11876                     ;; Form.
11877                     (save-restriction
11878                       (widen)
11879                       (setq result (eval match)))))
11880               (setq split-name (cdr method))
11881               (cond ((stringp result)
11882                      (push (expand-file-name
11883                             result gnus-article-save-directory)
11884                            split-name))
11885                     ((consp result)
11886                      (setq split-name (append result split-name)))))))))
11887     (nreverse split-name)))
11888
11889 (defun gnus-valid-move-group-p (group)
11890   (and (boundp group)
11891        (symbol-name group)
11892        (symbol-value group)
11893        (gnus-get-function (gnus-find-method-for-group
11894                            (symbol-name group)) 'request-accept-article t)))
11895
11896 (defun gnus-read-move-group-name (prompt default articles prefix)
11897   "Read a group name."
11898   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11899          (minibuffer-confirm-incomplete nil) ; XEmacs
11900          (prom
11901           (format "%s %s to"
11902                   prompt
11903                   (if (> (length articles) 1)
11904                       (format "these %d articles" (length articles))
11905                     "this article")))
11906          (to-newsgroup
11907           (let (active group)
11908             (when (or (null split-name) (= 1 (length split-name)))
11909               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11910               (mapatoms (lambda (symbol)
11911                           (setq group (symbol-name symbol))
11912                           (when (string-match "[^\000-\177]" group)
11913                             (setq group (gnus-group-decoded-name group)))
11914                           (set (intern group active) group))
11915                         gnus-active-hashtb))
11916             (cond
11917              ((null split-name)
11918               (gnus-completing-read-with-default
11919                default prom active 'gnus-valid-move-group-p nil prefix
11920                'gnus-group-history))
11921              ((= 1 (length split-name))
11922               (gnus-completing-read-with-default
11923                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11924                'gnus-group-history))
11925              (t
11926               (gnus-completing-read-with-default
11927                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11928                'gnus-group-history)))))
11929          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11930          encoded)
11931     (when to-newsgroup
11932       (if (or (string= to-newsgroup "")
11933               (string= to-newsgroup prefix))
11934           (setq to-newsgroup default))
11935       (unless to-newsgroup
11936         (error "No group name entered"))
11937       (setq encoded (mm-encode-coding-string
11938                      to-newsgroup
11939                      (gnus-group-name-charset to-method to-newsgroup)))
11940       (or (gnus-active encoded)
11941           (gnus-activate-group encoded nil nil to-method)
11942           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11943                                      to-newsgroup))
11944               (or (and (gnus-request-create-group encoded to-method)
11945                        (gnus-activate-group encoded nil nil to-method)
11946                        (gnus-subscribe-group encoded))
11947                   (error "Couldn't create group %s" to-newsgroup)))
11948           (error "No such group: %s" to-newsgroup))
11949       encoded)))
11950
11951 (defvar gnus-summary-save-parts-counter)
11952 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11953
11954 (defun gnus-summary-save-parts (type dir n &optional reverse)
11955   "Save parts matching TYPE to DIR.
11956 If REVERSE, save parts that do not match TYPE."
11957   (interactive
11958    (list (read-string "Save parts of type: "
11959                       (or (car gnus-summary-save-parts-type-history)
11960                           gnus-summary-save-parts-default-mime)
11961                       'gnus-summary-save-parts-type-history)
11962          (setq gnus-summary-save-parts-last-directory
11963                (read-file-name "Save to directory: "
11964                                gnus-summary-save-parts-last-directory
11965                                nil t))
11966          current-prefix-arg))
11967   (gnus-summary-iterate n
11968     (let ((gnus-display-mime-function nil)
11969           gnus-article-prepare-hook
11970           gnus-article-decode-hook
11971           gnus-display-mime-function
11972           gnus-break-pages
11973           (gnus-inhibit-treatment t))
11974       (gnus-summary-select-article))
11975     (with-current-buffer gnus-article-buffer
11976       (let ((handles (or gnus-article-mime-handles
11977                          (mm-dissect-buffer nil gnus-article-loose-mime)
11978                          (and gnus-article-emulate-mime
11979                               (mm-uu-dissect))))
11980             (gnus-summary-save-parts-counter 1))
11981         (when handles
11982           (gnus-summary-save-parts-1 type dir handles reverse)
11983           (unless gnus-article-mime-handles ;; Don't destroy this case.
11984             (mm-destroy-parts handles)))))))
11985
11986 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11987   (if (stringp (car handle))
11988       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11989               (cdr handle))
11990     (when (if reverse
11991               (not (string-match type (mm-handle-media-type handle)))
11992             (string-match type (mm-handle-media-type handle)))
11993       (let ((file (expand-file-name
11994                    (gnus-map-function
11995                     mm-file-name-rewrite-functions
11996                     (file-name-nondirectory
11997                      (or
11998                       (mail-content-type-get
11999                        (mm-handle-disposition handle) 'filename)
12000                       (mail-content-type-get
12001                        (mm-handle-type handle) 'name)
12002                       (format "%s.%d.%d" gnus-newsgroup-name
12003                               (cdr gnus-article-current)
12004                               gnus-summary-save-parts-counter))))
12005                    dir)))
12006         (incf gnus-summary-save-parts-counter)
12007         (unless (file-exists-p file)
12008           (mm-save-part-to-file handle file))))))
12009
12010 ;; Summary extract commands
12011
12012 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12013   (let ((inhibit-read-only t)
12014         (article (gnus-summary-article-number))
12015         after-article b e)
12016     (unless (gnus-summary-goto-subject article)
12017       (error "No such article: %d" article))
12018     (gnus-summary-position-point)
12019     ;; If all commands are to be bunched up on one line, we collect
12020     ;; them here.
12021     (unless gnus-view-pseudos-separately
12022       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12023             files action)
12024         (while ps
12025           (setq action (cdr (assq 'action (car ps))))
12026           (setq files (list (cdr (assq 'name (car ps)))))
12027           (while (and ps (cdr ps)
12028                       (string= (or action "1")
12029                                (or (cdr (assq 'action (cadr ps))) "2")))
12030             (push (cdr (assq 'name (cadr ps))) files)
12031             (setcdr ps (cddr ps)))
12032           (when files
12033             (when (not (string-match "%s" action))
12034               (push " " files))
12035             (push " " files)
12036             (when (assq 'execute (car ps))
12037               (setcdr (assq 'execute (car ps))
12038                       (funcall (if (string-match "%s" action)
12039                                    'format 'concat)
12040                                action
12041                                (mapconcat
12042                                 (lambda (f)
12043                                   (if (equal f " ")
12044                                       f
12045                                     (shell-quote-argument f)))
12046                                 files " ")))))
12047           (setq ps (cdr ps)))))
12048     (if (and gnus-view-pseudos (not not-view))
12049         (while pslist
12050           (when (assq 'execute (car pslist))
12051             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12052                                   (eq gnus-view-pseudos 'not-confirm)))
12053           (setq pslist (cdr pslist)))
12054       (save-excursion
12055         (while pslist
12056           (setq after-article (or (cdr (assq 'article (car pslist)))
12057                                   (gnus-summary-article-number)))
12058           (gnus-summary-goto-subject after-article)
12059           (forward-line 1)
12060           (setq b (point))
12061           (insert "    " (file-name-nondirectory
12062                           (cdr (assq 'name (car pslist))))
12063                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12064           (setq e (point))
12065           (forward-line -1)             ; back to `b'
12066           (gnus-add-text-properties
12067            b (1- e) (list 'gnus-number gnus-reffed-article-number
12068                           gnus-mouse-face-prop gnus-mouse-face))
12069           (gnus-data-enter
12070            after-article gnus-reffed-article-number
12071            gnus-unread-mark b (car pslist) 0 (- e b))
12072           (setq gnus-newsgroup-unreads
12073                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12074                                          gnus-reffed-article-number))
12075           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12076           (setq pslist (cdr pslist)))))))
12077
12078 (defun gnus-pseudos< (p1 p2)
12079   (let ((c1 (cdr (assq 'action p1)))
12080         (c2 (cdr (assq 'action p2))))
12081     (and c1 c2 (string< c1 c2))))
12082
12083 (defun gnus-request-pseudo-article (props)
12084   (cond ((assq 'execute props)
12085          (gnus-execute-command (cdr (assq 'execute props)))))
12086   (let ((gnus-current-article (gnus-summary-article-number)))
12087     (gnus-run-hooks 'gnus-mark-article-hook)))
12088
12089 (defun gnus-execute-command (command &optional automatic)
12090   (save-excursion
12091     (gnus-article-setup-buffer)
12092     (set-buffer gnus-article-buffer)
12093     (setq buffer-read-only nil)
12094     (let ((command (if automatic command
12095                      (read-string "Command: " (cons command 0)))))
12096       (erase-buffer)
12097       (insert "$ " command "\n\n")
12098       (if gnus-view-pseudo-asynchronously
12099           (start-process "gnus-execute" (current-buffer) shell-file-name
12100                          shell-command-switch command)
12101         (call-process shell-file-name nil t nil
12102                       shell-command-switch command)))))
12103
12104 ;; Summary kill commands.
12105
12106 (defun gnus-summary-edit-global-kill (article)
12107   "Edit the \"global\" kill file."
12108   (interactive (list (gnus-summary-article-number)))
12109   (gnus-group-edit-global-kill article))
12110
12111 (defun gnus-summary-edit-local-kill ()
12112   "Edit a local kill file applied to the current newsgroup."
12113   (interactive)
12114   (setq gnus-current-headers (gnus-summary-article-header))
12115   (gnus-group-edit-local-kill
12116    (gnus-summary-article-number) gnus-newsgroup-name))
12117
12118 ;;; Header reading.
12119
12120 (defun gnus-read-header (id &optional header)
12121   "Read the headers of article ID and enter them into the Gnus system."
12122   (let ((group gnus-newsgroup-name)
12123         (gnus-override-method
12124          (or
12125           gnus-override-method
12126           (and (gnus-news-group-p gnus-newsgroup-name)
12127                (car (gnus-refer-article-methods)))))
12128         where)
12129     ;; First we check to see whether the header in question is already
12130     ;; fetched.
12131     (if (stringp id)
12132         ;; This is a Message-ID.
12133         (setq header (or header (gnus-id-to-header id)))
12134       ;; This is an article number.
12135       (setq header (or header (gnus-summary-article-header id))))
12136     (if (and header
12137              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12138         ;; We have found the header.
12139         header
12140       ;; We have to really fetch the header to this article.
12141       (with-current-buffer nntp-server-buffer
12142         (when (setq where (gnus-request-head id group))
12143           (nnheader-fold-continuation-lines)
12144           (goto-char (point-max))
12145           (insert ".\n")
12146           (goto-char (point-min))
12147           (insert "211 ")
12148           (princ (cond
12149                   ((numberp id) id)
12150                   ((cdr where) (cdr where))
12151                   (header (mail-header-number header))
12152                   (t gnus-reffed-article-number))
12153                  (current-buffer))
12154           (insert " Article retrieved.\n"))
12155         (if (or (not where)
12156                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12157             ()                          ; Malformed head.
12158           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12159             (when (and (stringp id)
12160                        (or
12161                         (not (string= (gnus-group-real-name group)
12162                                       (car where)))
12163                         (not (gnus-server-equal gnus-override-method
12164                                                 (gnus-group-method group)))))
12165               ;; If we fetched by Message-ID and the article came from
12166               ;; a different group (or server), we fudge some bogus
12167               ;; article numbers for this article.
12168               (mail-header-set-number header gnus-reffed-article-number))
12169             (with-current-buffer gnus-summary-buffer
12170               (decf gnus-reffed-article-number)
12171               (gnus-remove-header (mail-header-number header))
12172               (push header gnus-newsgroup-headers)
12173               (setq gnus-current-headers header)
12174               (push (mail-header-number header) gnus-newsgroup-limit)))
12175           header)))))
12176
12177 (defun gnus-remove-header (number)
12178   "Remove header NUMBER from `gnus-newsgroup-headers'."
12179   (if (and gnus-newsgroup-headers
12180            (= number (mail-header-number (car gnus-newsgroup-headers))))
12181       (pop gnus-newsgroup-headers)
12182     (let ((headers gnus-newsgroup-headers))
12183       (while (and (cdr headers)
12184                   (not (= number (mail-header-number (cadr headers)))))
12185         (pop headers))
12186       (when (cdr headers)
12187         (setcdr headers (cddr headers))))))
12188
12189 ;;;
12190 ;;; summary highlights
12191 ;;;
12192
12193 (defun gnus-highlight-selected-summary ()
12194   "Highlight selected article in summary buffer."
12195   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12196   (when gnus-summary-selected-face
12197     (save-excursion
12198       (let* ((beg (point-at-bol))
12199              (end (point-at-eol))
12200              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12201              (from (if (get-text-property beg gnus-mouse-face-prop)
12202                        beg
12203                      (or (next-single-property-change
12204                           beg gnus-mouse-face-prop nil end)
12205                          beg)))
12206              (to
12207               (if (= from end)
12208                   (- from 2)
12209                 (or (next-single-property-change
12210                      from gnus-mouse-face-prop nil end)
12211                     end))))
12212         ;; If no mouse-face prop on line we will have to = from = end,
12213         ;; so we highlight the entire line instead.
12214         (when (= (+ to 2) from)
12215           (setq from beg)
12216           (setq to end))
12217         (if gnus-newsgroup-selected-overlay
12218             ;; Move old overlay.
12219             (gnus-move-overlay
12220              gnus-newsgroup-selected-overlay from to (current-buffer))
12221           ;; Create new overlay.
12222           (gnus-overlay-put
12223            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12224            'face gnus-summary-selected-face))))))
12225
12226 (defvar gnus-summary-highlight-line-cached nil)
12227 (defvar gnus-summary-highlight-line-trigger nil)
12228
12229 (defun gnus-summary-highlight-line-0 ()
12230   (if (and (eq gnus-summary-highlight-line-trigger
12231                gnus-summary-highlight)
12232            gnus-summary-highlight-line-cached)
12233       gnus-summary-highlight-line-cached
12234     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12235           gnus-summary-highlight-line-cached
12236           (let* ((cond (list 'cond))
12237                  (c cond)
12238                  (list gnus-summary-highlight))
12239             (while list
12240               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12241                               nil))
12242               (setq c (cdr c)
12243                     list (cdr list)))
12244             (gnus-byte-compile (list 'lambda nil cond))))))
12245
12246 (defun gnus-summary-highlight-line ()
12247   "Highlight current line according to `gnus-summary-highlight'."
12248   (let* ((beg (point-at-bol))
12249          (article (or (gnus-summary-article-number) gnus-current-article))
12250          (score (or (cdr (assq article
12251                                gnus-newsgroup-scored))
12252                     gnus-summary-default-score 0))
12253          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12254          (inhibit-read-only t)
12255          (default gnus-summary-default-score)
12256          (default-high gnus-summary-default-high-score)
12257          (default-low gnus-summary-default-low-score)
12258          (uncached (and gnus-summary-use-undownloaded-faces
12259                         (memq article gnus-newsgroup-undownloaded)
12260                         (not (memq article gnus-newsgroup-cached)))))
12261     (let ((face (funcall (gnus-summary-highlight-line-0))))
12262       (unless (eq face (get-text-property beg 'face))
12263         (gnus-put-text-property-excluding-characters-with-faces
12264          beg (point-at-eol) 'face
12265          (setq face (if (boundp face) (symbol-value face) face)))
12266         (when gnus-summary-highlight-line-function
12267           (funcall gnus-summary-highlight-line-function article face))))))
12268
12269 (defun gnus-update-read-articles (group unread &optional compute)
12270   "Update the list of read articles in GROUP.
12271 UNREAD is a sorted list."
12272   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12273         (info (gnus-get-info group))
12274         (prev 1)
12275         read)
12276     (if (or (not info) (not active))
12277         ;; There is no info on this group if it was, in fact,
12278         ;; killed.  Gnus stores no information on killed groups, so
12279         ;; there's nothing to be done.
12280         ;; One could store the information somewhere temporarily,
12281         ;; perhaps...  Hmmm...
12282         ()
12283       ;; Remove any negative articles numbers.
12284       (while (and unread (< (car unread) 0))
12285         (setq unread (cdr unread)))
12286       ;; Remove any expired article numbers
12287       (while (and unread (< (car unread) (car active)))
12288         (setq unread (cdr unread)))
12289       ;; Compute the ranges of read articles by looking at the list of
12290       ;; unread articles.
12291       (while unread
12292         (when (/= (car unread) prev)
12293           (push (if (= prev (1- (car unread))) prev
12294                   (cons prev (1- (car unread))))
12295                 read))
12296         (setq prev (1+ (car unread)))
12297         (setq unread (cdr unread)))
12298       (when (<= prev (cdr active))
12299         (push (cons prev (cdr active)) read))
12300       (setq read (if (> (length read) 1) (nreverse read) read))
12301       (if compute
12302           read
12303         (save-excursion
12304           (let (setmarkundo)
12305             ;; Propagate the read marks to the backend.
12306             (when (and gnus-propagate-marks
12307                        (gnus-check-backend-function 'request-set-mark group))
12308               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12309                     (add (gnus-remove-from-range read (gnus-info-read info))))
12310                 (when (or add del)
12311                   (unless (gnus-check-group group)
12312                     (error "Can't open server for %s" group))
12313                   (gnus-request-set-mark
12314                    group (delq nil (list (if add (list add 'add '(read)))
12315                                          (if del (list del 'del '(read))))))
12316                   (setq setmarkundo
12317                         `(gnus-request-set-mark
12318                           ,group
12319                           ',(delq nil (list
12320                                        (if del (list del 'add '(read)))
12321                                        (if add (list add 'del '(read))))))))))
12322             (set-buffer gnus-group-buffer)
12323             (gnus-undo-register
12324               `(progn
12325                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12326                  (gnus-info-set-read ',info ',(gnus-info-read info))
12327                  (gnus-get-unread-articles-in-group ',info
12328                                                     (gnus-active ,group))
12329                  (gnus-group-update-group ,group t)
12330                  ,setmarkundo))))
12331         ;; Enter this list into the group info.
12332         (gnus-info-set-read info read)
12333         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12334         (gnus-get-unread-articles-in-group info (gnus-active group))
12335         t))))
12336
12337 (defun gnus-offer-save-summaries ()
12338   "Offer to save all active summary buffers."
12339   (let (buffers)
12340     ;; Go through all buffers and find all summaries.
12341     (dolist (buffer (buffer-list))
12342       (when (and (setq buffer (buffer-name buffer))
12343                  (string-match "Summary" buffer)
12344                  (with-current-buffer buffer
12345                    ;; We check that this is, indeed, a summary buffer.
12346                    (and (eq major-mode 'gnus-summary-mode)
12347                         ;; Also make sure this isn't bogus.
12348                         gnus-newsgroup-prepared
12349                         ;; Also make sure that this isn't a
12350                         ;; dead summary buffer.
12351                         (not gnus-dead-summary-mode))))
12352         (push buffer buffers)))
12353     ;; Go through all these summary buffers and offer to save them.
12354     (when buffers
12355       (save-excursion
12356         (map-y-or-n-p
12357          "Update summary buffer %s? "
12358          (lambda (buf)
12359            (switch-to-buffer buf)
12360            (gnus-summary-exit))
12361          buffers)))))
12362
12363 (defun gnus-summary-setup-default-charset ()
12364   "Setup newsgroup default charset."
12365   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12366       (setq gnus-newsgroup-charset nil)
12367     (let* ((ignored-charsets
12368             (or gnus-newsgroup-ephemeral-ignored-charsets
12369                 (append
12370                  (and gnus-newsgroup-name
12371                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12372                  gnus-newsgroup-ignored-charsets))))
12373       (setq gnus-newsgroup-charset
12374             (or gnus-newsgroup-ephemeral-charset
12375                 (and gnus-newsgroup-name
12376                      (gnus-parameter-charset gnus-newsgroup-name))
12377                 gnus-default-charset))
12378       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12379            ignored-charsets))))
12380
12381 ;;;
12382 ;;; Mime Commands
12383 ;;;
12384
12385 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12386   "Display the current article buffer fully MIME-buttonized.
12387 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12388 treated as multipart/mixed."
12389   (interactive "P")
12390   (require 'gnus-art)
12391   (let ((gnus-unbuttonized-mime-types nil)
12392         (gnus-mime-display-multipart-as-mixed show-all-parts))
12393     (gnus-summary-show-article)))
12394
12395 (defun gnus-summary-repair-multipart (article)
12396   "Add a Content-Type header to a multipart article without one."
12397   (interactive (list (gnus-summary-article-number)))
12398   (gnus-with-article article
12399     (message-narrow-to-head)
12400     (message-remove-header "Mime-Version")
12401     (goto-char (point-max))
12402     (insert "Mime-Version: 1.0\n")
12403     (widen)
12404     (when (search-forward "\n--" nil t)
12405       (let ((separator (buffer-substring (point) (point-at-eol))))
12406         (message-narrow-to-head)
12407         (message-remove-header "Content-Type")
12408         (goto-char (point-max))
12409         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12410                         separator))
12411         (widen))))
12412   (let (gnus-mark-article-hook)
12413     (gnus-summary-select-article t t nil article)))
12414
12415 (defun gnus-summary-toggle-display-buttonized ()
12416   "Toggle the buttonizing of the article buffer."
12417   (interactive)
12418   (require 'gnus-art)
12419   (if (setq gnus-inhibit-mime-unbuttonizing
12420             (not gnus-inhibit-mime-unbuttonizing))
12421       (let ((gnus-unbuttonized-mime-types nil))
12422         (gnus-summary-show-article))
12423     (gnus-summary-show-article)))
12424
12425 ;;;
12426 ;;; Generic summary marking commands
12427 ;;;
12428
12429 (defvar gnus-summary-marking-alist
12430   '((read gnus-del-mark "d")
12431     (unread gnus-unread-mark "u")
12432     (ticked gnus-ticked-mark "!")
12433     (dormant gnus-dormant-mark "?")
12434     (expirable gnus-expirable-mark "e"))
12435   "An alist of names/marks/keystrokes.")
12436
12437 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12438 (defvar gnus-summary-mark-map)
12439
12440 (defun gnus-summary-make-all-marking-commands ()
12441   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12442   (dolist (elem gnus-summary-marking-alist)
12443     (apply 'gnus-summary-make-marking-command elem)))
12444
12445 (defun gnus-summary-make-marking-command (name mark keystroke)
12446   (let ((map (make-sparse-keymap)))
12447     (define-key gnus-summary-generic-mark-map keystroke map)
12448     (dolist (lway `((next "next" next nil "n")
12449                     (next-unread "next unread" next t "N")
12450                     (prev "previous" prev nil "p")
12451                     (prev-unread "previous unread" prev t "P")
12452                     (nomove "" nil nil ,keystroke)))
12453       (let ((func (gnus-summary-make-marking-command-1
12454                    mark (car lway) lway name)))
12455         (setq func (eval func))
12456         (define-key map (nth 4 lway) func)))))
12457
12458 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12459   `(defun ,(intern
12460             (format "gnus-summary-put-mark-as-%s%s"
12461                     name (if (eq way 'nomove)
12462                              ""
12463                            (concat "-" (symbol-name way)))))
12464      (n)
12465      ,(format
12466        "Mark the current article as %s%s.
12467 If N, the prefix, then repeat N times.
12468 If N is negative, move in reverse order.
12469 The difference between N and the actual number of articles marked is
12470 returned."
12471        name (cadr lway))
12472      (interactive "p")
12473      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12474
12475 (defun gnus-summary-generic-mark (n mark move unread)
12476   "Mark N articles with MARK."
12477   (unless (eq major-mode 'gnus-summary-mode)
12478     (error "This command can only be used in the summary buffer"))
12479   (gnus-summary-show-thread)
12480   (let ((nummove
12481          (cond
12482           ((eq move 'next) 1)
12483           ((eq move 'prev) -1)
12484           (t 0))))
12485     (if (zerop nummove)
12486         (setq n 1)
12487       (when (< n 0)
12488         (setq n (abs n)
12489               nummove (* -1 nummove))))
12490     (while (and (> n 0)
12491                 (gnus-summary-mark-article nil mark)
12492                 (zerop (gnus-summary-next-subject nummove unread t)))
12493       (setq n (1- n)))
12494     (when (/= 0 n)
12495       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12496     (gnus-summary-recenter)
12497     (gnus-summary-position-point)
12498     (gnus-set-mode-line 'summary)
12499     n))
12500
12501 (defun gnus-summary-insert-articles (articles)
12502   (when (setq articles
12503               (gnus-sorted-difference articles
12504                                       (mapcar (lambda (h)
12505                                                 (mail-header-number h))
12506                                               gnus-newsgroup-headers)))
12507     (setq gnus-newsgroup-headers
12508           (gnus-merge 'list
12509                       gnus-newsgroup-headers
12510                       (gnus-fetch-headers articles)
12511                       'gnus-article-sort-by-number))
12512     ;; Suppress duplicates?
12513     (when gnus-suppress-duplicates
12514       (gnus-dup-suppress-articles))
12515
12516     (if (and gnus-fetch-old-headers
12517              (eq gnus-headers-retrieved-by 'nov))
12518         ;; We might want to build some more threads first.
12519         (if (eq gnus-fetch-old-headers 'invisible)
12520             (gnus-build-all-threads)
12521           (gnus-build-old-threads))
12522       ;; Mark the inserted articles that are unread as unread.
12523       (setq gnus-newsgroup-unreads
12524             (gnus-sorted-nunion
12525              gnus-newsgroup-unreads
12526              (gnus-sorted-nintersection
12527               (gnus-list-of-unread-articles gnus-newsgroup-name)
12528               articles)))
12529       ;; Mark the inserted articles as selected so that the information
12530       ;; of the marks having been changed by a user may be updated when
12531       ;; exiting this group.  See `gnus-summary-update-info'.
12532       (dolist (art articles)
12533         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12534     ;; Let the Gnus agent mark articles as read.
12535     (when gnus-agent
12536       (gnus-agent-get-undownloaded-list))
12537     ;; Remove list identifiers from subject
12538     (when gnus-list-identifiers
12539       (gnus-summary-remove-list-identifiers))
12540     ;; First and last article in this newsgroup.
12541     (when gnus-newsgroup-headers
12542       (setq gnus-newsgroup-begin
12543             (mail-header-number (car gnus-newsgroup-headers))
12544             gnus-newsgroup-end
12545             (mail-header-number
12546              (gnus-last-element gnus-newsgroup-headers))))
12547     (when gnus-use-scoring
12548       (gnus-possibly-score-headers))))
12549
12550 (defun gnus-summary-insert-old-articles (&optional all)
12551   "Insert all old articles in this group.
12552 If ALL is non-nil, already read articles become readable.
12553 If ALL is a number, fetch this number of articles."
12554   (interactive "P")
12555   (prog1
12556       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12557             older len)
12558         (setq older
12559               ;; Some nntp servers lie about their active range.  When
12560               ;; this happens, the active range can be in the millions.
12561               ;; Use a compressed range to avoid creating a huge list.
12562               (gnus-range-difference (list gnus-newsgroup-active) old))
12563         (setq len (gnus-range-length older))
12564         (cond
12565          ((null older) nil)
12566          ((numberp all)
12567           (if (< all len)
12568               (let ((older-range (nreverse older)))
12569                 (setq older nil)
12570
12571                 (while (> all 0)
12572                   (let* ((r (pop older-range))
12573                          (min (if (numberp r) r (car r)))
12574                          (max (if (numberp r) r (cdr r))))
12575                     (while (and (<= min max)
12576                                 (> all 0))
12577                       (push max older)
12578                       (setq all (1- all)
12579                             max (1- max))))))
12580             (setq older (gnus-uncompress-range older))))
12581          (all
12582           (setq older (gnus-uncompress-range older)))
12583          (t
12584           (when (and (numberp gnus-large-newsgroup)
12585                    (> len gnus-large-newsgroup))
12586               (let* ((cursor-in-echo-area nil)
12587                      (initial (gnus-parameter-large-newsgroup-initial
12588                                gnus-newsgroup-name))
12589                      (input
12590                       (read-string
12591                        (format
12592                         "How many articles from %s (%s %d): "
12593                         (gnus-group-decoded-name gnus-newsgroup-name)
12594                         (if initial "max" "default")
12595                         len)
12596                        (if initial
12597                            (cons (number-to-string initial)
12598                                  0)))))
12599                 (unless (string-match "^[ \t]*$" input)
12600                   (setq all (string-to-number input))
12601                   (if (< all len)
12602                       (let ((older-range (nreverse older)))
12603                         (setq older nil)
12604
12605                         (while (> all 0)
12606                           (let* ((r (pop older-range))
12607                                  (min (if (numberp r) r (car r)))
12608                                  (max (if (numberp r) r (cdr r))))
12609                             (while (and (<= min max)
12610                                         (> all 0))
12611                               (push max older)
12612                               (setq all (1- all)
12613                                     max (1- max))))))))))
12614           (setq older (gnus-uncompress-range older))))
12615         (if (not older)
12616             (message "No old news.")
12617           (gnus-summary-insert-articles older)
12618           (gnus-summary-limit (gnus-sorted-nunion old older))))
12619     (gnus-summary-position-point)))
12620
12621 (defun gnus-summary-insert-new-articles ()
12622   "Insert all new articles in this group."
12623   (interactive)
12624   (prog1
12625       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12626             (old-active gnus-newsgroup-active)
12627             (nnmail-fetched-sources (list t))
12628             i new)
12629         (setq gnus-newsgroup-active
12630               (gnus-activate-group gnus-newsgroup-name 'scan))
12631         (setq i (cdr gnus-newsgroup-active))
12632         (while (> i (cdr old-active))
12633           (push i new)
12634           (decf i))
12635         (if (not new)
12636             (message "No gnus is bad news")
12637           (gnus-summary-insert-articles new)
12638           (setq gnus-newsgroup-unreads
12639                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12640           (gnus-summary-limit (gnus-sorted-nunion old new))))
12641     (gnus-summary-position-point)))
12642
12643 (gnus-summary-make-all-marking-commands)
12644
12645 (gnus-ems-redefine)
12646
12647 (provide 'gnus-sum)
12648
12649 (run-hooks 'gnus-sum-load-hook)
12650
12651 ;; Local Variables:
12652 ;; coding: iso-8859-1
12653 ;; End:
12654
12655 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12656 ;;; gnus-sum.el ends here