Merge from emacs--devo--0
[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 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 ;; For Emacs < 22.2.
31 (eval-and-compile
32   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
33 (eval-when-compile
34   (require 'cl))
35
36 (defvar tool-bar-mode)
37 (defvar gnus-tmp-header)
38
39 (require 'gnus)
40 (require 'gnus-group)
41 (require 'gnus-spec)
42 (require 'gnus-range)
43 (require 'gnus-int)
44 (require 'gnus-undo)
45 (require 'gnus-util)
46 (require 'gmm-utils)
47 (require 'mm-decode)
48 (require 'nnoo)
49
50 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
51 (autoload 'gnus-cache-write-active "gnus-cache")
52 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
53 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
54 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
55 (autoload 'mm-uu-dissect "mm-uu")
56 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
57   "Deuglify broken Outlook (Express) articles and redisplay."
58   t)
59 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
60 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
61 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
62
63 (defcustom gnus-kill-summary-on-exit t
64   "*If non-nil, kill the summary buffer when you exit from it.
65 If nil, the summary will become a \"*Dead Summary*\" buffer, and
66 it will be killed sometime later."
67   :group 'gnus-summary-exit
68   :type 'boolean)
69
70 (defcustom gnus-summary-next-group-on-exit t
71   "If non-nil, go to the next unread newsgroup on summary exit.
72 See `gnus-group-goto-unread'."
73   :link '(custom-manual "(gnus)Group Maneuvering")
74   :group 'gnus-summary-exit
75   :version "23.0" ;; No Gnus
76   :type 'boolean)
77
78 (defcustom gnus-fetch-old-headers nil
79   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
80 If an unread article in the group refers to an older, already
81 read (or just marked as read) article, the old article will not
82 normally be displayed in the Summary buffer.  If this variable is
83 t, Gnus will attempt to grab the headers to the old articles, and
84 thereby build complete threads.  If it has the value `some', all
85 old headers will be fetched but only enough headers to connect
86 otherwise loose threads will be displayed.  This variable can
87 also be a number.  In that case, no more than that number of old
88 headers will be fetched.  If it has the value `invisible', all
89 old headers will be fetched, but none will be displayed.
90
91 The server has to support NOV for any of this to work.
92
93 This feature can seriously impact performance it ignores all
94 locally cached header entries."
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const :tag "on" t)
98                  (const some)
99                  (const invisible)
100                  number
101                  (sexp :menu-tag "other" t)))
102
103 (defcustom gnus-refer-thread-limit 500
104   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
105 If t, fetch all the available old headers."
106   :group 'gnus-thread
107   :type '(choice number
108                  (sexp :menu-tag "other" t)))
109
110 (defcustom gnus-summary-make-false-root 'adopt
111   "*nil means that Gnus won't gather loose threads.
112 If the root of a thread has expired or been read in a previous
113 session, the information necessary to build a complete thread has been
114 lost.  Instead of having many small sub-threads from this original thread
115 scattered all over the summary buffer, Gnus can gather them.
116
117 If non-nil, Gnus will try to gather all loose sub-threads from an
118 original thread into one large thread.
119
120 If this variable is non-nil, it should be one of `none', `adopt',
121 `dummy' or `empty'.
122
123 If this variable is `none', Gnus will not make a false root, but just
124 present the sub-threads after another.
125 If this variable is `dummy', Gnus will create a dummy root that will
126 have all the sub-threads as children.
127 If this variable is `adopt', Gnus will make one of the \"children\"
128 the parent and mark all the step-children as such.
129 If this variable is `empty', the \"children\" are printed with empty
130 subject fields.  (Or rather, they will be printed with a string
131 given by the `gnus-summary-same-subject' variable.)"
132   :group 'gnus-thread
133   :type '(choice (const :tag "off" nil)
134                  (const none)
135                  (const dummy)
136                  (const adopt)
137                  (const empty)))
138
139 (defcustom gnus-summary-make-false-root-always nil
140   "Always make a false dummy root."
141   :version "22.1"
142   :group 'gnus-thread
143   :type 'boolean)
144
145 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
146   "*A regexp to match subjects to be excluded from loose thread gathering.
147 As loose thread gathering is done on subjects only, that means that
148 there can be many false gatherings performed.  By rooting out certain
149 common subjects, gathering might become saner."
150   :group 'gnus-thread
151   :type 'regexp)
152
153 (defcustom gnus-summary-gather-subject-limit nil
154   "*Maximum length of subject comparisons when gathering loose threads.
155 Use nil to compare full subjects.  Setting this variable to a low
156 number will help gather threads that have been corrupted by
157 newsreaders chopping off subject lines, but it might also mean that
158 unrelated articles that have subject that happen to begin with the
159 same few characters will be incorrectly gathered.
160
161 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
162 comparing subjects."
163   :group 'gnus-thread
164   :type '(choice (const :tag "off" nil)
165                  (const fuzzy)
166                  (sexp :menu-tag "on" t)))
167
168 (defcustom gnus-simplify-subject-functions nil
169   "List of functions taking a string argument that simplify subjects.
170 The functions are applied recursively.
171
172 Useful functions to put in this list include:
173 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
174 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
175   :group 'gnus-thread
176   :type '(repeat function))
177
178 (defcustom gnus-simplify-ignored-prefixes nil
179   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
180   :group 'gnus-thread
181   :type '(choice (const :tag "off" nil)
182                  regexp))
183
184 (defcustom gnus-build-sparse-threads nil
185   "*If non-nil, fill in the gaps in threads.
186 If `some', only fill in the gaps that are needed to tie loose threads
187 together.  If `more', fill in all leaf nodes that Gnus can find.  If
188 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
189   :group 'gnus-thread
190   :type '(choice (const :tag "off" nil)
191                  (const some)
192                  (const more)
193                  (sexp :menu-tag "all" t)))
194
195 (defcustom gnus-summary-thread-gathering-function
196   'gnus-gather-threads-by-subject
197   "*Function used for gathering loose threads.
198 There are two pre-defined functions: `gnus-gather-threads-by-subject',
199 which only takes Subjects into consideration; and
200 `gnus-gather-threads-by-references', which compared the References
201 headers of the articles to find matches."
202   :group 'gnus-thread
203   :type '(radio (function-item gnus-gather-threads-by-subject)
204                 (function-item gnus-gather-threads-by-references)
205                 (function :tag "other")))
206
207 (defcustom gnus-summary-same-subject ""
208   "*String indicating that the current article has the same subject as the previous.
209 This variable will only be used if the value of
210 `gnus-summary-make-false-root' is `empty'."
211   :group 'gnus-summary-format
212   :type 'string)
213
214 (defcustom gnus-summary-goto-unread t
215   "*If t, many commands will go to the next unread article.
216 This applies to marking commands as well as other commands that
217 \"naturally\" select the next article, like, for instance, `SPC' at
218 the end of an article.
219
220 If nil, the marking commands do NOT go to the next unread article
221 \(they go to the next article instead).  If `never', commands that
222 usually go to the next unread article, will go to the next article,
223 whether it is read or not."
224   :group 'gnus-summary-marks
225   :link '(custom-manual "(gnus)Setting Marks")
226   :type '(choice (const :tag "off" nil)
227                  (const never)
228                  (sexp :menu-tag "on" t)))
229
230 (defcustom gnus-summary-default-score 0
231   "*Default article score level.
232 All scores generated by the score files will be added to this score.
233 If this variable is nil, scoring will be disabled."
234   :group 'gnus-score-default
235   :type '(choice (const :tag "disable")
236                  integer))
237
238 (defcustom gnus-summary-default-high-score 0
239   "*Default threshold for a high scored article.
240 An article will be highlighted as high scored if its score is greater
241 than this score."
242   :version "22.1"
243   :group 'gnus-score-default
244   :type 'integer)
245
246 (defcustom gnus-summary-default-low-score 0
247   "*Default threshold for a low scored article.
248 An article will be highlighted as low scored if its score is smaller
249 than this score."
250   :version "22.1"
251   :group 'gnus-score-default
252   :type 'integer)
253
254 (defcustom gnus-summary-zcore-fuzz 0
255   "*Fuzziness factor for the zcore in the summary buffer.
256 Articles with scores closer than this to `gnus-summary-default-score'
257 will not be marked."
258   :group 'gnus-summary-format
259   :type 'integer)
260
261 (defcustom gnus-simplify-subject-fuzzy-regexp nil
262   "*Strings to be removed when doing fuzzy matches.
263 This can either be a regular expression or list of regular expressions
264 that will be removed from subject strings if fuzzy subject
265 simplification is selected."
266   :group 'gnus-thread
267   :type '(repeat regexp))
268
269 (defcustom gnus-show-threads t
270   "*If non-nil, display threads in summary mode."
271   :group 'gnus-thread
272   :type 'boolean)
273
274 (defcustom gnus-thread-hide-subtree nil
275   "*If non-nil, hide all threads initially.
276 This can be a predicate specifier which says which threads to hide.
277 If threads are hidden, you have to run the command
278 `gnus-summary-show-thread' by hand or select an article."
279   :group 'gnus-thread
280   :type '(radio (sexp :format "Non-nil\n"
281                       :match (lambda (widget value)
282                                (not (or (consp value) (functionp value))))
283                       :value t)
284                 (const nil)
285                 (sexp :tag "Predicate specifier")))
286
287 (defcustom gnus-thread-hide-killed t
288   "*If non-nil, hide killed threads automatically."
289   :group 'gnus-thread
290   :type 'boolean)
291
292 (defcustom gnus-thread-ignore-subject t
293   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
294 If nil, articles that have different subjects from their parents will
295 start separate threads."
296   :group 'gnus-thread
297   :type 'boolean)
298
299 (defcustom gnus-thread-operation-ignore-subject t
300   "*If non-nil, subjects will be ignored when doing thread commands.
301 This affects commands like `gnus-summary-kill-thread' and
302 `gnus-summary-lower-thread'.
303
304 If this variable is nil, articles in the same thread with different
305 subjects will not be included in the operation in question.  If this
306 variable is `fuzzy', only articles that have subjects that are fuzzily
307 equal will be included."
308   :group 'gnus-thread
309   :type '(choice (const :tag "off" nil)
310                  (const fuzzy)
311                  (sexp :tag "on" t)))
312
313 (defcustom gnus-thread-indent-level 4
314   "*Number that says how much each sub-thread should be indented."
315   :group 'gnus-thread
316   :type 'integer)
317
318 (defcustom gnus-auto-extend-newsgroup t
319   "*If non-nil, extend newsgroup forward and backward when requested."
320   :group 'gnus-summary-choose
321   :type 'boolean)
322
323 (defcustom gnus-auto-select-first t
324   "If non-nil, select an article on group entry.
325 An article is selected automatically when entering a group
326 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
327 `gnus-summary-catchup-and-goto-next-group'.
328
329 Which article is selected is controlled by the variable
330 `gnus-auto-select-subject'.
331
332 If you want to prevent automatic selection of articles in some
333 newsgroups, set the variable to nil in `gnus-select-group-hook'."
334   ;; Commands include...
335   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
336   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
337   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
338   :group 'gnus-group-select
339   :type '(choice (const :tag "none" nil)
340                  (sexp :menu-tag "first" t)))
341
342 (defcustom gnus-auto-select-subject 'unread
343   "*Says what subject to place under point when entering a group.
344
345 This variable can either be the symbols `first' (place point on the
346 first subject), `unread' (place point on the subject line of the first
347 unread article), `best' (place point on the subject line of the
348 higest-scored article), `unseen' (place point on the subject line of
349 the first unseen article), `unseen-or-unread' (place point on the subject
350 line of the first unseen article or, if all article have been seen, on the
351 subject line of the first unread article), or a function to be called to
352 place point on some subject line."
353   :version "22.1"
354   :group 'gnus-group-select
355   :type '(choice (const best)
356                  (const unread)
357                  (const first)
358                  (const unseen)
359                  (const unseen-or-unread)))
360
361 (defcustom gnus-auto-select-next t
362   "*If non-nil, offer to go to the next group from the end of the previous.
363 If the value is t and the next newsgroup is empty, Gnus will exit
364 summary mode and go back to group mode.  If the value is neither nil
365 nor t, Gnus will select the following unread newsgroup.  In
366 particular, if the value is the symbol `quietly', the next unread
367 newsgroup will be selected without any confirmation, and if it is
368 `almost-quietly', the next group will be selected without any
369 confirmation if you are located on the last article in the group.
370 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
371 will go to the next group without confirmation."
372   :group 'gnus-summary-maneuvering
373   :type '(choice (const :tag "off" nil)
374                  (const quietly)
375                  (const almost-quietly)
376                  (const slightly-quietly)
377                  (sexp :menu-tag "on" t)))
378
379 (defcustom gnus-auto-select-same nil
380   "*If non-nil, select the next article with the same subject.
381 If there are no more articles with the same subject, go to
382 the first unread article."
383   :group 'gnus-summary-maneuvering
384   :type 'boolean)
385
386 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
387   "What article should be selected after exiting an ephemeral group.
388 Valid values include:
389
390 `next'
391   Select the next article.
392 `next-unread'
393   Select the next unread article.
394 `next-noselect'
395   Move the cursor to the next article.  This is the default.
396 `next-unread-noselect'
397   Move the cursor to the next unread article.
398
399 If it has any other value or there is no next (unread) article, the
400 article selected before entering to the ephemeral group will appear."
401   :version "23.0" ;; No Gnus
402   :group 'gnus-summary-maneuvering
403   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
404                  (const next) (const next-unread)
405                  (const next-noselect) (const next-unread-noselect)
406                  (sexp :tag "other" :value nil)))
407
408 (defcustom gnus-auto-goto-ignores 'unfetched
409   "*Says how to handle unfetched articles when maneuvering.
410
411 This variable can either be the symbols nil (maneuver to any
412 article), `undownloaded' (maneuvering while unplugged ignores articles
413 that have not been fetched), `always-undownloaded' (maneuvering always
414 ignores articles that have not been fetched), `unfetched' (maneuvering
415 ignores articles whose headers have not been fetched).
416
417 NOTE: The list of unfetched articles will always be nil when plugged
418 and, when unplugged, a subset of the undownloaded article list."
419   :version "22.1"
420   :group 'gnus-summary-maneuvering
421   :type '(choice (const :tag "None" nil)
422                  (const :tag "Undownloaded when unplugged" undownloaded)
423                  (const :tag "Undownloaded" always-undownloaded)
424                  (const :tag "Unfetched" unfetched)))
425
426 (defcustom gnus-summary-check-current nil
427   "*If non-nil, consider the current article when moving.
428 The \"unread\" movement commands will stay on the same line if the
429 current article is unread."
430   :group 'gnus-summary-maneuvering
431   :type 'boolean)
432
433 (defcustom gnus-auto-center-summary 2
434   "*If non-nil, always center the current summary buffer.
435 In particular, if `vertical' do only vertical recentering.  If non-nil
436 and non-`vertical', do both horizontal and vertical recentering."
437   :group 'gnus-summary-maneuvering
438   :type '(choice (const :tag "none" nil)
439                  (const vertical)
440                  (integer :tag "height")
441                  (sexp :menu-tag "both" t)))
442
443 (defvar gnus-auto-center-group t
444   "*If non-nil, always center the group buffer.")
445
446 (defcustom gnus-show-all-headers nil
447   "*If non-nil, don't hide any headers."
448   :group 'gnus-article-hiding
449   :group 'gnus-article-headers
450   :type 'boolean)
451
452 (defcustom gnus-summary-ignore-duplicates nil
453   "*If non-nil, ignore articles with identical Message-ID headers."
454   :group 'gnus-summary
455   :type 'boolean)
456
457 (defcustom gnus-single-article-buffer t
458   "*If non-nil, display all articles in the same buffer.
459 If nil, each group will get its own article buffer."
460   :group 'gnus-article-various
461   :type 'boolean)
462
463 (defcustom gnus-break-pages t
464   "*If non-nil, do page breaking on articles.
465 The page delimiter is specified by the `gnus-page-delimiter'
466 variable."
467   :group 'gnus-article-various
468   :type 'boolean)
469
470 (defcustom gnus-move-split-methods nil
471   "*Variable used to suggest where articles are to be moved to.
472 It uses the same syntax as the `gnus-split-methods' variable.
473 However, whereas `gnus-split-methods' specifies file names as targets,
474 this variable specifies group names."
475   :group 'gnus-summary-mail
476   :type '(repeat (choice (list :value (fun) function)
477                          (cons :value ("" "") regexp (repeat string))
478                          (sexp :value nil))))
479
480 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
481   "Function used to compute default prefix for article move/copy/etc prompts.
482 The function should take one argument, a group name, and return a
483 string with the suggested prefix."
484   :group 'gnus-summary-mail
485   :type 'function)
486
487 ;; FIXME: Although the custom type is `character' for the following variables,
488 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
489
490 (defcustom gnus-unread-mark ?           ;Whitespace
491   "*Mark used for unread articles."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-ticked-mark ?!
496   "*Mark used for ticked articles."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-dormant-mark ??
501   "*Mark used for dormant articles."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-del-mark ?r
506   "*Mark used for del'd articles."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-read-mark ?R
511   "*Mark used for read articles."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-expirable-mark ?E
516   "*Mark used for expirable articles."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-killed-mark ?K
521   "*Mark used for killed articles."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-spam-mark ?$
526   "*Mark used for spam articles."
527   :version "22.1"
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-souped-mark ?F
532   "*Mark used for souped articles."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-kill-file-mark ?X
537   "*Mark used for articles killed by kill files."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-low-score-mark ?Y
542   "*Mark used for articles with a low score."
543   :group 'gnus-summary-marks
544   :type 'character)
545
546 (defcustom gnus-catchup-mark ?C
547   "*Mark used for articles that are caught up."
548   :group 'gnus-summary-marks
549   :type 'character)
550
551 (defcustom gnus-replied-mark ?A
552   "*Mark used for articles that have been replied to."
553   :group 'gnus-summary-marks
554   :type 'character)
555
556 (defcustom gnus-forwarded-mark ?F
557   "*Mark used for articles that have been forwarded."
558   :version "22.1"
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-recent-mark ?N
563   "*Mark used for articles that are recent."
564   :version "22.1"
565   :group 'gnus-summary-marks
566   :type 'character)
567
568 (defcustom gnus-cached-mark ?*
569   "*Mark used for articles that are in the cache."
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-saved-mark ?S
574   "*Mark used for articles that have been saved."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-unseen-mark ?.
579   "*Mark used for articles that haven't been seen."
580   :version "22.1"
581   :group 'gnus-summary-marks
582   :type 'character)
583
584 (defcustom gnus-no-mark ?               ;Whitespace
585   "*Mark used for articles that have no other secondary mark."
586   :version "22.1"
587   :group 'gnus-summary-marks
588   :type 'character)
589
590 (defcustom gnus-ancient-mark ?O
591   "*Mark used for ancient articles."
592   :group 'gnus-summary-marks
593   :type 'character)
594
595 (defcustom gnus-sparse-mark ?Q
596   "*Mark used for sparsely reffed articles."
597   :group 'gnus-summary-marks
598   :type 'character)
599
600 (defcustom gnus-canceled-mark ?G
601   "*Mark used for canceled articles."
602   :group 'gnus-summary-marks
603   :type 'character)
604
605 (defcustom gnus-duplicate-mark ?M
606   "*Mark used for duplicate articles."
607   :group 'gnus-summary-marks
608   :type 'character)
609
610 (defcustom gnus-undownloaded-mark ?-
611   "*Mark used for articles that weren't downloaded."
612   :version "22.1"
613   :group 'gnus-summary-marks
614   :type 'character)
615
616 (defcustom gnus-downloaded-mark ?+
617   "*Mark used for articles that were downloaded."
618   :group 'gnus-summary-marks
619   :type 'character)
620
621 (defcustom gnus-downloadable-mark ?%
622   "*Mark used for articles that are to be downloaded."
623   :group 'gnus-summary-marks
624   :type 'character)
625
626 (defcustom gnus-unsendable-mark ?=
627   "*Mark used for articles that won't be sent."
628   :group 'gnus-summary-marks
629   :type 'character)
630
631 (defcustom gnus-score-over-mark ?+
632   "*Score mark used for articles with high scores."
633   :group 'gnus-summary-marks
634   :type 'character)
635
636 (defcustom gnus-score-below-mark ?-
637   "*Score mark used for articles with low scores."
638   :group 'gnus-summary-marks
639   :type 'character)
640
641 (defcustom gnus-empty-thread-mark ?     ;Whitespace
642   "*There is no thread under the article."
643   :group 'gnus-summary-marks
644   :type 'character)
645
646 (defcustom gnus-not-empty-thread-mark ?=
647   "*There is a thread under the article."
648   :group 'gnus-summary-marks
649   :type 'character)
650
651 (defcustom gnus-view-pseudo-asynchronously nil
652   "*If non-nil, Gnus will view pseudo-articles asynchronously."
653   :group 'gnus-extract-view
654   :type 'boolean)
655
656 (defcustom gnus-auto-expirable-marks
657   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
658         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
659         gnus-souped-mark gnus-duplicate-mark)
660   "*The list of marks converted into expiration if a group is auto-expirable."
661   :version "21.1"
662   :group 'gnus-summary
663   :type '(repeat character))
664
665 (defcustom gnus-inhibit-user-auto-expire t
666   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
667   :version "21.1"
668   :group 'gnus-summary
669   :type 'boolean)
670
671 (defcustom gnus-view-pseudos nil
672   "*If `automatic', pseudo-articles will be viewed automatically.
673 If `not-confirm', pseudos will be viewed automatically, and the user
674 will not be asked to confirm the command."
675   :group 'gnus-extract-view
676   :type '(choice (const :tag "off" nil)
677                  (const automatic)
678                  (const not-confirm)))
679
680 (defcustom gnus-view-pseudos-separately t
681   "*If non-nil, one pseudo-article will be created for each file to be viewed.
682 If nil, all files that use the same viewing command will be given as a
683 list of parameters to that command."
684   :group 'gnus-extract-view
685   :type 'boolean)
686
687 (defcustom gnus-insert-pseudo-articles t
688   "*If non-nil, insert pseudo-articles when decoding articles."
689   :group 'gnus-extract-view
690   :type 'boolean)
691
692 (defcustom gnus-summary-dummy-line-format
693   "   %(:                             :%) %S\n"
694   "*The format specification for the dummy roots in the summary buffer.
695 It works along the same lines as a normal formatting string,
696 with some simple extensions.
697
698 %S  The subject
699
700 General format specifiers can also be used.
701 See `(gnus)Formatting Variables'."
702   :link '(custom-manual "(gnus)Formatting Variables")
703   :group 'gnus-threading
704   :type 'string)
705
706 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
707   "*The format specification for the summary mode line.
708 It works along the same lines as a normal formatting string,
709 with some simple extensions:
710
711 %G  Group name
712 %p  Unprefixed group name
713 %A  Current article number
714 %z  Current article score
715 %V  Gnus version
716 %U  Number of unread articles in the group
717 %e  Number of unselected articles in the group
718 %Z  A string with unread/unselected article counts
719 %g  Shortish group name
720 %S  Subject of the current article
721 %u  User-defined spec
722 %s  Current score file name
723 %d  Number of dormant articles
724 %r  Number of articles that have been marked as read in this session
725 %E  Number of articles expunged by the score files"
726   :group 'gnus-summary-format
727   :type 'string)
728
729 (defcustom gnus-list-identifiers nil
730   "Regexp that matches list identifiers to be removed from subject.
731 This can also be a list of regexps."
732   :version "21.1"
733   :group 'gnus-summary-format
734   :group 'gnus-article-hiding
735   :type '(choice (const :tag "none" nil)
736                  (regexp :value ".*")
737                  (repeat :value (".*") regexp)))
738
739 (defcustom gnus-summary-mark-below 0
740   "*Mark all articles with a score below this variable as read.
741 This variable is local to each summary buffer and usually set by the
742 score file."
743   :group 'gnus-score-default
744   :type 'integer)
745
746 (defun gnus-widget-reversible-match (widget value)
747   "Ignoring WIDGET, convert VALUE to internal form.
748 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
749   ;; (debug value)
750   (or (symbolp value)
751       (and (listp value)
752            (eq (length value) 2)
753            (eq (nth 0 value) 'not)
754            (symbolp (nth 1 value)))))
755
756 (defun gnus-widget-reversible-to-internal (widget value)
757   "Ignoring WIDGET, convert VALUE to internal form.
758 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
759 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
760   ;; (debug value)
761   (if (atom value)
762       (list value nil)
763     (list (nth 1 value) t)))
764
765 (defun gnus-widget-reversible-to-external (widget value)
766   "Ignoring WIDGET, convert VALUE to external form.
767 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
768 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
769   ;; (debug value)
770   (if (nth 1 value)
771       (list 'not (nth 0 value))
772     (nth 0 value)))
773
774 (define-widget 'gnus-widget-reversible 'group
775   "A `group' that convert values."
776   :match 'gnus-widget-reversible-match
777   :value-to-internal 'gnus-widget-reversible-to-internal
778   :value-to-external 'gnus-widget-reversible-to-external)
779
780 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
781   "*List of functions used for sorting articles in the summary buffer.
782
783 Each function takes two articles and returns non-nil if the first
784 article should be sorted before the other.  If you use more than one
785 function, the primary sort function should be the last.  You should
786 probably always include `gnus-article-sort-by-number' in the list of
787 sorting functions -- preferably first.  Also note that sorting by date
788 is often much slower than sorting by number, and the sorting order is
789 very similar.  (Sorting by date means sorting by the time the message
790 was sent, sorting by number means sorting by arrival time.)
791
792 Each item can also be a list `(not F)' where F is a function;
793 this reverses the sort order.
794
795 Ready-made functions include `gnus-article-sort-by-number',
796 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
797 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
798 and `gnus-article-sort-by-score'.
799
800 When threading is turned on, the variable `gnus-thread-sort-functions'
801 controls how articles are sorted."
802   :group 'gnus-summary-sort
803   :type '(repeat (gnus-widget-reversible
804                   (choice (function-item gnus-article-sort-by-number)
805                           (function-item gnus-article-sort-by-author)
806                           (function-item gnus-article-sort-by-subject)
807                           (function-item gnus-article-sort-by-date)
808                           (function-item gnus-article-sort-by-score)
809                           (function-item gnus-article-sort-by-random)
810                           (function :tag "other"))
811                   (boolean :tag "Reverse order"))))
812
813
814 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
815   "*List of functions used for sorting threads in the summary buffer.
816 By default, threads are sorted by article number.
817
818 Each function takes two threads and returns non-nil if the first
819 thread should be sorted before the other.  If you use more than one
820 function, the primary sort function should be the last.  You should
821 probably always include `gnus-thread-sort-by-number' in the list of
822 sorting functions -- preferably first.  Also note that sorting by date
823 is often much slower than sorting by number, and the sorting order is
824 very similar.  (Sorting by date means sorting by the time the message
825 was sent, sorting by number means sorting by arrival time.)
826
827 Each list item can also be a list `(not F)' where F is a
828 function; this specifies reversed sort order.
829
830 Ready-made functions include `gnus-thread-sort-by-number',
831 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
832 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
833 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
834 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
835 and `gnus-thread-sort-by-total-score' (see
836 `gnus-thread-score-function').
837
838 When threading is turned off, the variable
839 `gnus-article-sort-functions' controls how articles are sorted."
840   :group 'gnus-summary-sort
841   :type '(repeat
842           (gnus-widget-reversible
843            (choice (function-item gnus-thread-sort-by-number)
844                    (function-item gnus-thread-sort-by-author)
845                    (function-item gnus-thread-sort-by-recipient)
846                    (function-item gnus-thread-sort-by-subject)
847                    (function-item gnus-thread-sort-by-date)
848                    (function-item gnus-thread-sort-by-score)
849                    (function-item gnus-thread-sort-by-most-recent-number)
850                    (function-item gnus-thread-sort-by-most-recent-date)
851                    (function-item gnus-thread-sort-by-random)
852                    (function-item gnus-thread-sort-by-total-score)
853                    (function :tag "other"))
854            (boolean :tag "Reverse order"))))
855
856 (defcustom gnus-thread-score-function '+
857   "*Function used for calculating the total score of a thread.
858
859 The function is called with the scores of the article and each
860 subthread and should then return the score of the thread.
861
862 Some functions you can use are `+', `max', or `min'."
863   :group 'gnus-summary-sort
864   :type 'function)
865
866 (defcustom gnus-summary-expunge-below nil
867   "All articles that have a score less than this variable will be expunged.
868 This variable is local to the summary buffers."
869   :group 'gnus-score-default
870   :type '(choice (const :tag "off" nil)
871                  integer))
872
873 (defcustom gnus-thread-expunge-below nil
874   "All threads that have a total score less than this variable will be expunged.
875 See `gnus-thread-score-function' for en explanation of what a
876 \"thread score\" is.
877
878 This variable is local to the summary buffers."
879   :group 'gnus-threading
880   :group 'gnus-score-default
881   :type '(choice (const :tag "off" nil)
882                  integer))
883
884 (defcustom gnus-summary-mode-hook nil
885   "*A hook for Gnus summary mode.
886 This hook is run before any variables are set in the summary buffer."
887   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
888   :group 'gnus-summary-various
889   :type 'hook)
890
891 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
892 (when (featurep 'xemacs)
893   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
894   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
895   (add-hook 'gnus-summary-mode-hook
896             'gnus-xmas-switch-horizontal-scrollbar-off))
897
898 (defcustom gnus-summary-menu-hook nil
899   "*Hook run after the creation of the summary mode menu."
900   :group 'gnus-summary-visual
901   :type 'hook)
902
903 (defcustom gnus-summary-exit-hook nil
904   "*A hook called on exit from the summary buffer.
905 It will be called with point in the group buffer."
906   :group 'gnus-summary-exit
907   :type 'hook)
908
909 (defcustom gnus-summary-prepare-hook nil
910   "*A hook called after the summary buffer has been generated.
911 If you want to modify the summary buffer, you can use this hook."
912   :group 'gnus-summary-various
913   :type 'hook)
914
915 (defcustom gnus-summary-prepared-hook nil
916   "*A hook called as the last thing after the summary buffer has been generated."
917   :group 'gnus-summary-various
918   :type 'hook)
919
920 (defcustom gnus-summary-generate-hook nil
921   "*A hook run just before generating the summary buffer.
922 This hook is commonly used to customize threading variables and the
923 like."
924   :group 'gnus-summary-various
925   :type 'hook)
926
927 (defcustom gnus-select-group-hook nil
928   "*A hook called when a newsgroup is selected.
929
930 If you'd like to simplify subjects like the
931 `gnus-summary-next-same-subject' command does, you can use the
932 following hook:
933
934  (add-hook gnus-select-group-hook
935            (lambda ()
936              (mapcar (lambda (header)
937                        (mail-header-set-subject
938                         header
939                         (gnus-simplify-subject
940                          (mail-header-subject header) 're-only)))
941                      gnus-newsgroup-headers)))"
942   :group 'gnus-group-select
943   :type 'hook)
944
945 (defcustom gnus-select-article-hook nil
946   "*A hook called when an article is selected."
947   :group 'gnus-summary-choose
948   :options '(gnus-agent-fetch-selected-article)
949   :type 'hook)
950
951 (defcustom gnus-visual-mark-article-hook
952   (list 'gnus-highlight-selected-summary)
953   "*Hook run after selecting an article in the summary buffer.
954 It is meant to be used for highlighting the article in some way.  It
955 is not run if `gnus-visual' is nil."
956   :group 'gnus-summary-visual
957   :type 'hook)
958
959 (defcustom gnus-parse-headers-hook nil
960   "*A hook called before parsing the headers."
961   :group 'gnus-various
962   :type 'hook)
963
964 (defcustom gnus-exit-group-hook nil
965   "*A hook called when exiting summary mode.
966 This hook is not called from the non-updating exit commands like `Q'."
967   :group 'gnus-various
968   :type 'hook)
969
970 (defcustom gnus-summary-update-hook
971   (list 'gnus-summary-highlight-line)
972   "*A hook called when a summary line is changed.
973 The hook will not be called if `gnus-visual' is nil.
974
975 The default function `gnus-summary-highlight-line' will
976 highlight the line according to the `gnus-summary-highlight'
977 variable."
978   :group 'gnus-summary-visual
979   :type 'hook)
980
981 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
982   "*A hook called when an article is selected for the first time.
983 The hook is intended to mark an article as read (or unread)
984 automatically when it is selected."
985   :group 'gnus-summary-choose
986   :type 'hook)
987
988 (defcustom gnus-group-no-more-groups-hook nil
989   "*A hook run when returning to group mode having no more (unread) groups."
990   :group 'gnus-group-select
991   :type 'hook)
992
993 (defcustom gnus-ps-print-hook nil
994   "*A hook run before ps-printing something from Gnus."
995   :group 'gnus-summary
996   :type 'hook)
997
998 (defcustom gnus-summary-article-move-hook nil
999   "*A hook called after an article is moved, copied, respooled, or crossposted."
1000   :version "22.1"
1001   :group 'gnus-summary
1002   :type 'hook)
1003
1004 (defcustom gnus-summary-article-delete-hook nil
1005   "*A hook called after an article is deleted."
1006   :version "22.1"
1007   :group 'gnus-summary
1008   :type 'hook)
1009
1010 (defcustom gnus-summary-article-expire-hook nil
1011   "*A hook called after an article is expired."
1012   :version "22.1"
1013   :group 'gnus-summary
1014   :type 'hook)
1015
1016 (defcustom gnus-summary-display-arrow
1017   (and (fboundp 'display-graphic-p)
1018        (display-graphic-p))
1019   "*If non-nil, display an arrow highlighting the current article."
1020   :version "22.1"
1021   :group 'gnus-summary
1022   :type 'boolean)
1023
1024 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1025   "Face used for highlighting the current article in the summary buffer."
1026   :group 'gnus-summary-visual
1027   :type 'face)
1028
1029 (defvar gnus-tmp-downloaded nil)
1030
1031 (defcustom gnus-summary-highlight
1032   '(((eq mark gnus-canceled-mark)
1033      . gnus-summary-cancelled)
1034     ((and uncached (> score default-high))
1035      . gnus-summary-high-undownloaded)
1036     ((and uncached (< score default-low))
1037      . gnus-summary-low-undownloaded)
1038     (uncached
1039      . gnus-summary-normal-undownloaded)
1040     ((and (> score default-high)
1041           (or (eq mark gnus-dormant-mark)
1042               (eq mark gnus-ticked-mark)))
1043      . gnus-summary-high-ticked)
1044     ((and (< score default-low)
1045           (or (eq mark gnus-dormant-mark)
1046               (eq mark gnus-ticked-mark)))
1047      . gnus-summary-low-ticked)
1048     ((or (eq mark gnus-dormant-mark)
1049          (eq mark gnus-ticked-mark))
1050      . gnus-summary-normal-ticked)
1051     ((and (> score default-high) (eq mark gnus-ancient-mark))
1052      . gnus-summary-high-ancient)
1053     ((and (< score default-low) (eq mark gnus-ancient-mark))
1054      . gnus-summary-low-ancient)
1055     ((eq mark gnus-ancient-mark)
1056      . gnus-summary-normal-ancient)
1057     ((and (> score default-high) (eq mark gnus-unread-mark))
1058      . gnus-summary-high-unread)
1059     ((and (< score default-low) (eq mark gnus-unread-mark))
1060      . gnus-summary-low-unread)
1061     ((eq mark gnus-unread-mark)
1062      . gnus-summary-normal-unread)
1063     ((> score default-high)
1064      . gnus-summary-high-read)
1065     ((< score default-low)
1066      . gnus-summary-low-read)
1067     (t
1068      . gnus-summary-normal-read))
1069   "*Controls the highlighting of summary buffer lines.
1070
1071 A list of (FORM . FACE) pairs.  When deciding how a particular
1072 summary line should be displayed, each form is evaluated.  The content
1073 of the face field after the first true form is used.  You can change
1074 how those summary lines are displayed, by editing the face field.
1075
1076 You can use the following variables in the FORM field.
1077
1078 score:        The article's score.
1079 default:      The default article score.
1080 default-high: The default score for high scored articles.
1081 default-low:  The default score for low scored articles.
1082 below:        The score below which articles are automatically marked as read.
1083 mark:         The article's mark.
1084 uncached:     Non-nil if the article is uncached."
1085   :group 'gnus-summary-visual
1086   :type '(repeat (cons (sexp :tag "Form" nil)
1087                        face)))
1088 (put 'gnus-summary-highlight 'risky-local-variable t)
1089
1090 (defcustom gnus-alter-header-function nil
1091   "Function called to allow alteration of article header structures.
1092 The function is called with one parameter, the article header vector,
1093 which it may alter in any way."
1094   :type '(choice (const :tag "None" nil)
1095                  function)
1096   :group 'gnus-summary)
1097
1098 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1099   "Function used to decode a string with encoded words.")
1100
1101 (defvar gnus-decode-encoded-address-function
1102   'mail-decode-encoded-address-string
1103   "Function used to decode addresses with encoded words.")
1104
1105 (defcustom gnus-extra-headers '(To Newsgroups)
1106   "*Extra headers to parse."
1107   :version "21.1"
1108   :group 'gnus-summary
1109   :type '(repeat symbol))
1110
1111 (defcustom gnus-ignored-from-addresses
1112   (and user-mail-address
1113        (not (string= user-mail-address ""))
1114        (regexp-quote user-mail-address))
1115   "*From headers that may be suppressed in favor of To headers.
1116 This can be a regexp or a list of regexps."
1117   :version "21.1"
1118   :group 'gnus-summary
1119   :type '(choice regexp
1120                  (repeat :tag "Regexp List" regexp)))
1121
1122 (defsubst gnus-ignored-from-addresses ()
1123   (gmm-regexp-concat gnus-ignored-from-addresses))
1124
1125 (defcustom gnus-summary-to-prefix "-> "
1126   "*String prefixed to the To field in the summary line when
1127 using `gnus-ignored-from-addresses'."
1128   :version "22.1"
1129   :group 'gnus-summary
1130   :type 'string)
1131
1132 (defcustom gnus-summary-newsgroup-prefix "=> "
1133   "*String prefixed to the Newsgroup field in the summary
1134 line when using `gnus-ignored-from-addresses'."
1135   :version "22.1"
1136   :group 'gnus-summary
1137   :type 'string)
1138
1139 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1140   "List of charsets that should be ignored.
1141 When these charsets are used in the \"charset\" parameter, the
1142 default charset will be used instead."
1143   :version "21.1"
1144   :type '(repeat symbol)
1145   :group 'gnus-charset)
1146
1147 (defcustom gnus-newsgroup-maximum-articles nil
1148   "The maximum number of articles a newsgroup.
1149 If this is a number, old articles in a newsgroup exceeding this number
1150 are silently ignored.  If it is nil, no article is ignored.  Note that
1151 setting this variable to a number might prevent you from reading very
1152 old articles."
1153   :group 'gnus-group-select
1154   :version "22.2"
1155   :type '(choice (const :tag "No limit" nil)
1156                  integer))
1157
1158 (gnus-define-group-parameter
1159  ignored-charsets
1160  :type list
1161  :function-document
1162  "Return the ignored charsets of GROUP."
1163  :variable gnus-group-ignored-charsets-alist
1164  :variable-default
1165  '(("alt\\.chinese\\.text" iso-8859-1))
1166  :variable-document
1167  "Alist of regexps (to match group names) and charsets that should be ignored.
1168 When these charsets are used in the \"charset\" parameter, the
1169 default charset will be used instead."
1170  :variable-group gnus-charset
1171  :variable-type '(repeat (cons (regexp :tag "Group")
1172                                (repeat symbol)))
1173  :parameter-type '(choice :tag "Ignored charsets"
1174                           :value nil
1175                           (repeat (symbol)))
1176  :parameter-document       "\
1177 List of charsets that should be ignored.
1178
1179 When these charsets are used in the \"charset\" parameter, the
1180 default charset will be used instead.")
1181
1182 (defcustom gnus-group-highlight-words-alist nil
1183   "Alist of group regexps and highlight regexps.
1184 This variable uses the same syntax as `gnus-emphasis-alist'."
1185   :version "21.1"
1186   :type '(repeat (cons (regexp :tag "Group")
1187                        (repeat (list (regexp :tag "Highlight regexp")
1188                                      (number :tag "Group for entire word" 0)
1189                                      (number :tag "Group for displayed part" 0)
1190                                      (symbol :tag "Face"
1191                                              gnus-emphasis-highlight-words)))))
1192   :group 'gnus-summary-visual)
1193
1194 (defcustom gnus-summary-show-article-charset-alist
1195   nil
1196   "Alist of number and charset.
1197 The article will be shown with the charset corresponding to the
1198 numbered argument.
1199 For example: ((1 . cn-gb-2312) (2 . big5))."
1200   :version "21.1"
1201   :type '(repeat (cons (number :tag "Argument" 1)
1202                        (symbol :tag "Charset")))
1203   :group 'gnus-charset)
1204
1205 (defcustom gnus-preserve-marks t
1206   "Whether marks are preserved when moving, copying and respooling messages."
1207   :version "21.1"
1208   :type 'boolean
1209   :group 'gnus-summary-marks)
1210
1211 (defcustom gnus-alter-articles-to-read-function nil
1212   "Function to be called to alter the list of articles to be selected."
1213   :type '(choice (const nil) function)
1214   :group 'gnus-summary)
1215
1216 (defcustom gnus-orphan-score nil
1217   "*All orphans get this score added.  Set in the score file."
1218   :group 'gnus-score-default
1219   :type '(choice (const nil)
1220                  integer))
1221
1222 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1223   "*A regexp to match MIME parts when saving multiple parts of a
1224 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1225 This regexp will be used by default when prompting the user for which
1226 type of files to save."
1227   :group 'gnus-summary
1228   :type 'regexp)
1229
1230 (defcustom gnus-read-all-available-headers nil
1231   "Whether Gnus should parse all headers made available to it.
1232 This is mostly relevant for slow back ends where the user may
1233 wish to widen the summary buffer to include all headers
1234 that were fetched.  Say, for nnultimate groups."
1235   :version "22.1"
1236   :group 'gnus-summary
1237   :type '(choice boolean regexp))
1238
1239 (defcustom gnus-summary-muttprint-program "muttprint"
1240   "Command (and optional arguments) used to run Muttprint."
1241   :version "22.1"
1242   :group 'gnus-summary
1243   :type 'string)
1244
1245 (defcustom gnus-article-loose-mime t
1246   "If non-nil, don't require MIME-Version header.
1247 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1248 supply the MIME-Version header or deliberately strip it from the mail.
1249 If non-nil (the default), Gnus will treat some articles as MIME
1250 even if the MIME-Version header is missing."
1251   :version "22.1"
1252   :type 'boolean
1253   :group 'gnus-article-mime)
1254
1255 (defcustom gnus-article-emulate-mime t
1256   "If non-nil, use MIME emulation for uuencode and the like.
1257 This means that Gnus will search message bodies for text that look
1258 like uuencoded bits, yEncoded bits, and so on, and present that using
1259 the normal Gnus MIME machinery."
1260   :version "22.1"
1261   :type 'boolean
1262   :group 'gnus-article-mime)
1263
1264 ;;; Internal variables
1265
1266 (defvar gnus-summary-display-cache nil)
1267 (defvar gnus-article-mime-handles nil)
1268 (defvar gnus-article-decoded-p nil)
1269 (defvar gnus-article-charset nil)
1270 (defvar gnus-article-ignored-charsets nil)
1271 (defvar gnus-scores-exclude-files nil)
1272 (defvar gnus-page-broken nil)
1273
1274 (defvar gnus-original-article nil)
1275 (defvar gnus-article-internal-prepare-hook nil)
1276 (defvar gnus-newsgroup-process-stack nil)
1277
1278 (defvar gnus-thread-indent-array nil)
1279 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1280 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1281   "Function called to sort the articles within a thread after it has been gathered together.")
1282
1283 (defvar gnus-summary-save-parts-type-history nil)
1284 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1285
1286 ;; Avoid highlighting in kill files.
1287 (defvar gnus-summary-inhibit-highlight nil)
1288 (defvar gnus-newsgroup-selected-overlay nil)
1289 (defvar gnus-inhibit-limiting nil)
1290 (defvar gnus-newsgroup-adaptive-score-file nil)
1291 (defvar gnus-current-score-file nil)
1292 (defvar gnus-current-move-group nil)
1293 (defvar gnus-current-copy-group nil)
1294 (defvar gnus-current-crosspost-group nil)
1295 (defvar gnus-newsgroup-display nil)
1296
1297 (defvar gnus-newsgroup-dependencies nil)
1298 (defvar gnus-newsgroup-adaptive nil)
1299 (defvar gnus-summary-display-article-function nil)
1300 (defvar gnus-summary-highlight-line-function nil
1301   "Function called after highlighting a summary line.")
1302
1303 (defvar gnus-summary-line-format-alist
1304   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1305     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1306     (?s gnus-tmp-subject-or-nil ?s)
1307     (?n gnus-tmp-name ?s)
1308     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1309         ?s)
1310     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1311             gnus-tmp-from) ?s)
1312     (?F gnus-tmp-from ?s)
1313     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1314     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1315     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1316     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1317     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1318     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1319     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1320     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1321     (?L gnus-tmp-lines ?s)
1322     (?O gnus-tmp-downloaded ?c)
1323     (?I gnus-tmp-indentation ?s)
1324     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1325     (?R gnus-tmp-replied ?c)
1326     (?\[ gnus-tmp-opening-bracket ?c)
1327     (?\] gnus-tmp-closing-bracket ?c)
1328     (?\> (make-string gnus-tmp-level ? ) ?s)
1329     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1330     (?i gnus-tmp-score ?d)
1331     (?z gnus-tmp-score-char ?c)
1332     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1333     (?U gnus-tmp-unread ?c)
1334     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1335         ?s)
1336     (?t (gnus-summary-number-of-articles-in-thread
1337          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1338         ?d)
1339     (?e (gnus-summary-number-of-articles-in-thread
1340          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1341         ?c)
1342     (?u gnus-tmp-user-defined ?s)
1343     (?P (gnus-pick-line-number) ?d)
1344     (?B gnus-tmp-thread-tree-header-string ?s)
1345     (user-date (gnus-user-date
1346                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1347   "An alist of format specifications that can appear in summary lines.
1348 These are paired with what variables they correspond with, along with
1349 the type of the variable (string, integer, character, etc).")
1350
1351 (defvar gnus-summary-dummy-line-format-alist
1352   `((?S gnus-tmp-subject ?s)
1353     (?N gnus-tmp-number ?d)
1354     (?u gnus-tmp-user-defined ?s)))
1355
1356 (defvar gnus-summary-mode-line-format-alist
1357   `((?G gnus-tmp-group-name ?s)
1358     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1359     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1360     (?A gnus-tmp-article-number ?d)
1361     (?Z gnus-tmp-unread-and-unselected ?s)
1362     (?V gnus-version ?s)
1363     (?U gnus-tmp-unread-and-unticked ?d)
1364     (?S gnus-tmp-subject ?s)
1365     (?e gnus-tmp-unselected ?d)
1366     (?u gnus-tmp-user-defined ?s)
1367     (?d (length gnus-newsgroup-dormant) ?d)
1368     (?t (length gnus-newsgroup-marked) ?d)
1369     (?h (length gnus-newsgroup-spam-marked) ?d)
1370     (?r (length gnus-newsgroup-reads) ?d)
1371     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1372     (?E gnus-newsgroup-expunged-tally ?d)
1373     (?s (gnus-current-score-file-nondirectory) ?s)))
1374
1375 (defvar gnus-last-search-regexp nil
1376   "Default regexp for article search command.")
1377
1378 (defvar gnus-last-shell-command nil
1379   "Default shell command on article.")
1380
1381 (defvar gnus-newsgroup-agentized nil
1382   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1383 (defvar gnus-newsgroup-begin nil)
1384 (defvar gnus-newsgroup-end nil)
1385 (defvar gnus-newsgroup-last-rmail nil)
1386 (defvar gnus-newsgroup-last-mail nil)
1387 (defvar gnus-newsgroup-last-folder nil)
1388 (defvar gnus-newsgroup-last-file nil)
1389 (defvar gnus-newsgroup-last-directory nil)
1390 (defvar gnus-newsgroup-auto-expire nil)
1391 (defvar gnus-newsgroup-active nil)
1392
1393 (defvar gnus-newsgroup-data nil)
1394 (defvar gnus-newsgroup-data-reverse nil)
1395 (defvar gnus-newsgroup-limit nil)
1396 (defvar gnus-newsgroup-limits nil)
1397 (defvar gnus-summary-use-undownloaded-faces nil)
1398
1399 (defvar gnus-newsgroup-unreads nil
1400   "Sorted list of unread articles in the current newsgroup.")
1401
1402 (defvar gnus-newsgroup-unselected nil
1403   "Sorted list of unselected unread articles in the current newsgroup.")
1404
1405 (defvar gnus-newsgroup-reads nil
1406   "Alist of read articles and article marks in the current newsgroup.")
1407
1408 (defvar gnus-newsgroup-expunged-tally nil)
1409
1410 (defvar gnus-newsgroup-marked nil
1411   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1412
1413 (defvar gnus-newsgroup-spam-marked nil
1414   "List of ranges of articles that have been marked as spam.")
1415
1416 (defvar gnus-newsgroup-killed nil
1417   "List of ranges of articles that have been through the scoring process.")
1418
1419 (defvar gnus-newsgroup-cached nil
1420   "Sorted list of articles that come from the article cache.")
1421
1422 (defvar gnus-newsgroup-saved nil
1423   "List of articles that have been saved.")
1424
1425 (defvar gnus-newsgroup-kill-headers nil)
1426
1427 (defvar gnus-newsgroup-replied nil
1428   "List of articles that have been replied to in the current newsgroup.")
1429
1430 (defvar gnus-newsgroup-forwarded nil
1431   "List of articles that have been forwarded in the current newsgroup.")
1432
1433 (defvar gnus-newsgroup-recent nil
1434   "List of articles that have are recent in the current newsgroup.")
1435
1436 (defvar gnus-newsgroup-expirable nil
1437   "Sorted list of articles in the current newsgroup that can be expired.")
1438
1439 (defvar gnus-newsgroup-processable nil
1440   "List of articles in the current newsgroup that can be processed.")
1441
1442 (defvar gnus-newsgroup-downloadable nil
1443   "Sorted list of articles in the current newsgroup that can be processed.")
1444
1445 (defvar gnus-newsgroup-unfetched nil
1446   "Sorted list of articles in the current newsgroup whose headers have
1447 not been fetched into the agent.
1448
1449 This list will always be a subset of gnus-newsgroup-undownloaded.")
1450
1451 (defvar gnus-newsgroup-undownloaded nil
1452   "List of articles in the current newsgroup that haven't been downloaded.")
1453
1454 (defvar gnus-newsgroup-unsendable nil
1455   "List of articles in the current newsgroup that won't be sent.")
1456
1457 (defvar gnus-newsgroup-bookmarks nil
1458   "List of articles in the current newsgroup that have bookmarks.")
1459
1460 (defvar gnus-newsgroup-dormant nil
1461   "Sorted list of dormant articles in the current newsgroup.")
1462
1463 (defvar gnus-newsgroup-unseen nil
1464   "List of unseen articles in the current newsgroup.")
1465
1466 (defvar gnus-newsgroup-seen nil
1467   "Range of seen articles in the current newsgroup.")
1468
1469 (defvar gnus-newsgroup-articles nil
1470   "List of articles in the current newsgroup.")
1471
1472 (defvar gnus-newsgroup-scored nil
1473   "List of scored articles in the current newsgroup.")
1474
1475 (defvar gnus-newsgroup-headers nil
1476   "List of article headers in the current newsgroup.")
1477
1478 (defvar gnus-newsgroup-threads nil)
1479
1480 (defvar gnus-newsgroup-prepared nil
1481   "Whether the current group has been prepared properly.")
1482
1483 (defvar gnus-newsgroup-ancient nil
1484   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1485
1486 (defvar gnus-newsgroup-sparse nil)
1487
1488 (defvar gnus-current-article nil)
1489 (defvar gnus-article-current nil)
1490 (defvar gnus-current-headers nil)
1491 (defvar gnus-have-all-headers nil)
1492 (defvar gnus-last-article nil)
1493 (defvar gnus-newsgroup-history nil)
1494 (defvar gnus-newsgroup-charset nil)
1495 (defvar gnus-newsgroup-ephemeral-charset nil)
1496 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1497
1498 (defvar gnus-article-before-search nil)
1499
1500 (defvar gnus-summary-local-variables
1501   '(gnus-newsgroup-name
1502     gnus-newsgroup-begin gnus-newsgroup-end
1503     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1504     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1505     gnus-newsgroup-last-directory
1506     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1507     gnus-newsgroup-unselected gnus-newsgroup-marked
1508     gnus-newsgroup-spam-marked
1509     gnus-newsgroup-reads gnus-newsgroup-saved
1510     gnus-newsgroup-replied gnus-newsgroup-forwarded
1511     gnus-newsgroup-recent
1512     gnus-newsgroup-expirable
1513     gnus-newsgroup-processable gnus-newsgroup-killed
1514     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1515     gnus-newsgroup-unfetched
1516     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1517     gnus-newsgroup-seen gnus-newsgroup-articles
1518     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1519     gnus-newsgroup-headers gnus-newsgroup-threads
1520     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1521     gnus-current-article gnus-current-headers gnus-have-all-headers
1522     gnus-last-article gnus-article-internal-prepare-hook
1523     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1524     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1525     gnus-thread-expunge-below
1526     gnus-score-alist gnus-current-score-file
1527     (gnus-summary-expunge-below . global)
1528     (gnus-summary-mark-below . global)
1529     (gnus-orphan-score . global)
1530     gnus-newsgroup-active gnus-scores-exclude-files
1531     gnus-newsgroup-history gnus-newsgroup-ancient
1532     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1533     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1534     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1535     (gnus-newsgroup-expunged-tally . 0)
1536     gnus-cache-removable-articles gnus-newsgroup-cached
1537     gnus-newsgroup-data gnus-newsgroup-data-reverse
1538     gnus-newsgroup-limit gnus-newsgroup-limits
1539     gnus-newsgroup-charset gnus-newsgroup-display
1540     gnus-summary-use-undownloaded-faces)
1541   "Variables that are buffer-local to the summary buffers.")
1542
1543 (defvar gnus-newsgroup-variables nil
1544   "A list of variables that have separate values in different newsgroups.
1545 A list of newsgroup (summary buffer) local variables, or cons of
1546 variables and their default expressions to be evalled (when the default
1547 values are not nil), that should be made global while the summary buffer
1548 is active.
1549
1550 Note: The default expressions will be evaluated (using function `eval')
1551 before assignment to the local variable rather than just assigned to it.
1552 If the default expression is the symbol `global', that symbol will not
1553 be evaluated but the global value of the local variable will be used
1554 instead.
1555
1556 These variables can be used to set variables in the group parameters
1557 while still allowing them to affect operations done in other buffers.
1558 For example:
1559
1560 \(setq gnus-newsgroup-variables
1561      '(message-use-followup-to
1562        (gnus-visible-headers .
1563          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1564 ")
1565
1566 ;; Byte-compiler warning.
1567 (eval-when-compile
1568   ;; Bind features so that require will believe that gnus-sum has
1569   ;; already been loaded (avoids infinite recursion)
1570   (let ((features (cons 'gnus-sum features)))
1571     ;; Several of the declarations in gnus-sum are needed to load the
1572     ;; following files. Right now, these definitions have been
1573     ;; compiled but not defined (evaluated).  We could either do a
1574     ;; eval-and-compile about all of the declarations or evaluate the
1575     ;; source file.
1576     (if (boundp 'gnus-newsgroup-variables)
1577         nil
1578       (load "gnus-sum.el" t t t))
1579     (require 'gnus)
1580     (require 'gnus-art)))
1581
1582 ;; MIME stuff.
1583
1584 (defvar gnus-decode-encoded-word-methods
1585   '(mail-decode-encoded-word-string)
1586   "List of methods used to decode encoded words.
1587
1588 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1589 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1590 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1591 whose names match REGEXP.
1592
1593 For example:
1594 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1595  mail-decode-encoded-word-string
1596  (\"chinese\" . rfc1843-decode-string))")
1597
1598 (defvar gnus-decode-encoded-word-methods-cache nil)
1599
1600 (defun gnus-multi-decode-encoded-word-string (string)
1601   "Apply the functions from `gnus-encoded-word-methods' that match."
1602   (unless (and gnus-decode-encoded-word-methods-cache
1603                (eq gnus-newsgroup-name
1604                    (car gnus-decode-encoded-word-methods-cache)))
1605     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1606     (dolist (method gnus-decode-encoded-word-methods)
1607       (if (symbolp method)
1608           (nconc gnus-decode-encoded-word-methods-cache (list method))
1609         (if (and gnus-newsgroup-name
1610                  (string-match (car method) gnus-newsgroup-name))
1611             (nconc gnus-decode-encoded-word-methods-cache
1612                    (list (cdr method)))))))
1613   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1614     (setq string (funcall method string))))
1615
1616 ;; Subject simplification.
1617
1618 (defun gnus-simplify-whitespace (str)
1619   "Remove excessive whitespace from STR."
1620   ;; Multiple spaces.
1621   (while (string-match "[ \t][ \t]+" str)
1622     (setq str (concat (substring str 0 (match-beginning 0))
1623                         " "
1624                         (substring str (match-end 0)))))
1625   ;; Leading spaces.
1626   (when (string-match "^[ \t]+" str)
1627     (setq str (substring str (match-end 0))))
1628   ;; Trailing spaces.
1629   (when (string-match "[ \t]+$" str)
1630     (setq str (substring str 0 (match-beginning 0))))
1631   str)
1632
1633 (defun gnus-simplify-all-whitespace (str)
1634   "Remove all whitespace from STR."
1635   (while (string-match "[ \t\n]+" str)
1636     (setq str (replace-match "" nil nil str)))
1637   str)
1638
1639 (defsubst gnus-simplify-subject-re (subject)
1640   "Remove \"Re:\" from subject lines."
1641   (if (string-match message-subject-re-regexp subject)
1642       (substring subject (match-end 0))
1643     subject))
1644
1645 (defun gnus-simplify-subject (subject &optional re-only)
1646   "Remove `Re:' and words in parentheses.
1647 If RE-ONLY is non-nil, strip leading `Re:'s only."
1648   (let ((case-fold-search t))           ;Ignore case.
1649     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1650     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1651       (setq subject (substring subject (match-end 0))))
1652     ;; Remove uninteresting prefixes.
1653     (when (and (not re-only)
1654                gnus-simplify-ignored-prefixes
1655                (string-match gnus-simplify-ignored-prefixes subject))
1656       (setq subject (substring subject (match-end 0))))
1657     ;; Remove words in parentheses from end.
1658     (unless re-only
1659       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1660         (setq subject (substring subject 0 (match-beginning 0)))))
1661     ;; Return subject string.
1662     subject))
1663
1664 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1665 ;; all whitespace.
1666 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1667   (goto-char (point-min))
1668   (while (re-search-forward regexp nil t)
1669     (replace-match (or newtext ""))))
1670
1671 (defun gnus-simplify-buffer-fuzzy ()
1672   "Simplify string in the buffer fuzzily.
1673 The string in the accessible portion of the current buffer is simplified.
1674 It is assumed to be a single-line subject.
1675 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1676 matter is removed.  Additional things can be deleted by setting
1677 `gnus-simplify-subject-fuzzy-regexp'."
1678   (let ((case-fold-search t)
1679         (modified-tick))
1680     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1681
1682     (while (not (eq modified-tick (buffer-modified-tick)))
1683       (setq modified-tick (buffer-modified-tick))
1684       (cond
1685        ((listp gnus-simplify-subject-fuzzy-regexp)
1686         (mapc 'gnus-simplify-buffer-fuzzy-step
1687               gnus-simplify-subject-fuzzy-regexp))
1688        (gnus-simplify-subject-fuzzy-regexp
1689         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1690       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1691       (gnus-simplify-buffer-fuzzy-step
1692        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1693       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1694
1695     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1696     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1697     (gnus-simplify-buffer-fuzzy-step " $")
1698     (gnus-simplify-buffer-fuzzy-step "^ +")))
1699
1700 (defun gnus-simplify-subject-fuzzy (subject)
1701   "Simplify a subject string fuzzily.
1702 See `gnus-simplify-buffer-fuzzy' for details."
1703   (save-excursion
1704     (gnus-set-work-buffer)
1705     (let ((case-fold-search t))
1706       ;; Remove uninteresting prefixes.
1707       (when (and gnus-simplify-ignored-prefixes
1708                  (string-match gnus-simplify-ignored-prefixes subject))
1709         (setq subject (substring subject (match-end 0))))
1710       (insert subject)
1711       (inline (gnus-simplify-buffer-fuzzy))
1712       (buffer-string))))
1713
1714 (defsubst gnus-simplify-subject-fully (subject)
1715   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1716   (cond
1717    (gnus-simplify-subject-functions
1718     (gnus-map-function gnus-simplify-subject-functions subject))
1719    ((null gnus-summary-gather-subject-limit)
1720     (gnus-simplify-subject-re subject))
1721    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1722     (gnus-simplify-subject-fuzzy subject))
1723    ((numberp gnus-summary-gather-subject-limit)
1724     (truncate-string-to-width (gnus-simplify-subject-re subject)
1725                               gnus-summary-gather-subject-limit))
1726    (t
1727     subject)))
1728
1729 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1730   "Check whether two subjects are equal.
1731 If optional argument SIMPLE-FIRST is t, first argument is already
1732 simplified."
1733   (cond
1734    ((null simple-first)
1735     (equal (gnus-simplify-subject-fully s1)
1736            (gnus-simplify-subject-fully s2)))
1737    (t
1738     (equal s1
1739            (gnus-simplify-subject-fully s2)))))
1740
1741 (defun gnus-summary-bubble-group ()
1742   "Increase the score of the current group.
1743 This is a handy function to add to `gnus-summary-exit-hook' to
1744 increase the score of each group you read."
1745   (gnus-group-add-score gnus-newsgroup-name))
1746
1747 \f
1748 ;;;
1749 ;;; Gnus summary mode
1750 ;;;
1751
1752 (put 'gnus-summary-mode 'mode-class 'special)
1753
1754 (defvar gnus-article-commands-menu)
1755
1756 ;; Non-orthogonal keys
1757
1758 (gnus-define-keys gnus-summary-mode-map
1759   " " gnus-summary-next-page
1760   "\177" gnus-summary-prev-page
1761   [delete] gnus-summary-prev-page
1762   [backspace] gnus-summary-prev-page
1763   "\r" gnus-summary-scroll-up
1764   "\M-\r" gnus-summary-scroll-down
1765   "n" gnus-summary-next-unread-article
1766   "p" gnus-summary-prev-unread-article
1767   "N" gnus-summary-next-article
1768   "P" gnus-summary-prev-article
1769   "\M-\C-n" gnus-summary-next-same-subject
1770   "\M-\C-p" gnus-summary-prev-same-subject
1771   "\M-n" gnus-summary-next-unread-subject
1772   "\M-p" gnus-summary-prev-unread-subject
1773   "." gnus-summary-first-unread-article
1774   "," gnus-summary-best-unread-article
1775   "\M-s" gnus-summary-search-article-forward
1776   "\M-r" gnus-summary-search-article-backward
1777   "\M-S" gnus-summary-repeat-search-article-forward
1778   "\M-R" gnus-summary-repeat-search-article-backward
1779   "<" gnus-summary-beginning-of-article
1780   ">" gnus-summary-end-of-article
1781   "j" gnus-summary-goto-article
1782   "^" gnus-summary-refer-parent-article
1783   "\M-^" gnus-summary-refer-article
1784   "u" gnus-summary-tick-article-forward
1785   "!" gnus-summary-tick-article-forward
1786   "U" gnus-summary-tick-article-backward
1787   "d" gnus-summary-mark-as-read-forward
1788   "D" gnus-summary-mark-as-read-backward
1789   "E" gnus-summary-mark-as-expirable
1790   "\M-u" gnus-summary-clear-mark-forward
1791   "\M-U" gnus-summary-clear-mark-backward
1792   "k" gnus-summary-kill-same-subject-and-select
1793   "\C-k" gnus-summary-kill-same-subject
1794   "\M-\C-k" gnus-summary-kill-thread
1795   "\M-\C-l" gnus-summary-lower-thread
1796   "e" gnus-summary-edit-article
1797   "#" gnus-summary-mark-as-processable
1798   "\M-#" gnus-summary-unmark-as-processable
1799   "\M-\C-t" gnus-summary-toggle-threads
1800   "\M-\C-s" gnus-summary-show-thread
1801   "\M-\C-h" gnus-summary-hide-thread
1802   "\M-\C-f" gnus-summary-next-thread
1803   "\M-\C-b" gnus-summary-prev-thread
1804   [(meta down)] gnus-summary-next-thread
1805   [(meta up)] gnus-summary-prev-thread
1806   "\M-\C-u" gnus-summary-up-thread
1807   "\M-\C-d" gnus-summary-down-thread
1808   "&" gnus-summary-execute-command
1809   "c" gnus-summary-catchup-and-exit
1810   "\C-w" gnus-summary-mark-region-as-read
1811   "\C-t" gnus-summary-toggle-truncation
1812   "?" gnus-summary-mark-as-dormant
1813   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1814   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1815   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1816   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1817   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1818   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1819   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1820   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1821   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1822   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1823   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1824   "=" gnus-summary-expand-window
1825   "\C-x\C-s" gnus-summary-reselect-current-group
1826   "\M-g" gnus-summary-rescan-group
1827   "w" gnus-summary-stop-page-breaking
1828   "\C-c\C-r" gnus-summary-caesar-message
1829   "f" gnus-summary-followup
1830   "F" gnus-summary-followup-with-original
1831   "C" gnus-summary-cancel-article
1832   "r" gnus-summary-reply
1833   "R" gnus-summary-reply-with-original
1834   "\C-c\C-f" gnus-summary-mail-forward
1835   "o" gnus-summary-save-article
1836   "\C-o" gnus-summary-save-article-mail
1837   "|" gnus-summary-pipe-output
1838   "\M-k" gnus-summary-edit-local-kill
1839   "\M-K" gnus-summary-edit-global-kill
1840   ;; "V" gnus-version
1841   "\C-c\C-d" gnus-summary-describe-group
1842   "q" gnus-summary-exit
1843   "Q" gnus-summary-exit-no-update
1844   "\C-c\C-i" gnus-info-find-node
1845   gnus-mouse-2 gnus-mouse-pick-article
1846   [follow-link] mouse-face
1847   "m" gnus-summary-mail-other-window
1848   "a" gnus-summary-post-news
1849   "i" gnus-summary-news-other-window
1850   "x" gnus-summary-limit-to-unread
1851   "s" gnus-summary-isearch-article
1852   "t" gnus-summary-toggle-header
1853   "g" gnus-summary-show-article
1854   "l" gnus-summary-goto-last-article
1855   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1856   "\C-d" gnus-summary-enter-digest-group
1857   "\M-\C-d" gnus-summary-read-document
1858   "\M-\C-e" gnus-summary-edit-parameters
1859   "\M-\C-a" gnus-summary-customize-parameters
1860   "\C-c\C-b" gnus-bug
1861   "*" gnus-cache-enter-article
1862   "\M-*" gnus-cache-remove-article
1863   "\M-&" gnus-summary-universal-argument
1864   "\C-l" gnus-recenter
1865   "I" gnus-summary-increase-score
1866   "L" gnus-summary-lower-score
1867   "\M-i" gnus-symbolic-argument
1868   "h" gnus-summary-select-article-buffer
1869
1870   "b" gnus-article-view-part
1871   "\M-t" gnus-summary-toggle-display-buttonized
1872
1873   "V" gnus-summary-score-map
1874   "X" gnus-uu-extract-map
1875   "S" gnus-summary-send-map)
1876
1877 ;; Sort of orthogonal keymap
1878 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1879   "t" gnus-summary-tick-article-forward
1880   "!" gnus-summary-tick-article-forward
1881   "d" gnus-summary-mark-as-read-forward
1882   "r" gnus-summary-mark-as-read-forward
1883   "c" gnus-summary-clear-mark-forward
1884   " " gnus-summary-clear-mark-forward
1885   "e" gnus-summary-mark-as-expirable
1886   "x" gnus-summary-mark-as-expirable
1887   "?" gnus-summary-mark-as-dormant
1888   "b" gnus-summary-set-bookmark
1889   "B" gnus-summary-remove-bookmark
1890   "#" gnus-summary-mark-as-processable
1891   "\M-#" gnus-summary-unmark-as-processable
1892   "S" gnus-summary-limit-include-expunged
1893   "C" gnus-summary-catchup
1894   "H" gnus-summary-catchup-to-here
1895   "h" gnus-summary-catchup-from-here
1896   "\C-c" gnus-summary-catchup-all
1897   "k" gnus-summary-kill-same-subject-and-select
1898   "K" gnus-summary-kill-same-subject
1899   "P" gnus-uu-mark-map)
1900
1901 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1902   "c" gnus-summary-clear-above
1903   "u" gnus-summary-tick-above
1904   "m" gnus-summary-mark-above
1905   "k" gnus-summary-kill-below)
1906
1907 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1908   "/" gnus-summary-limit-to-subject
1909   "n" gnus-summary-limit-to-articles
1910   "b" gnus-summary-limit-to-bodies
1911   "h" gnus-summary-limit-to-headers
1912   "w" gnus-summary-pop-limit
1913   "s" gnus-summary-limit-to-subject
1914   "a" gnus-summary-limit-to-author
1915   "u" gnus-summary-limit-to-unread
1916   "m" gnus-summary-limit-to-marks
1917   "M" gnus-summary-limit-exclude-marks
1918   "v" gnus-summary-limit-to-score
1919   "*" gnus-summary-limit-include-cached
1920   "D" gnus-summary-limit-include-dormant
1921   "T" gnus-summary-limit-include-thread
1922   "d" gnus-summary-limit-exclude-dormant
1923   "t" gnus-summary-limit-to-age
1924   "." gnus-summary-limit-to-unseen
1925   "x" gnus-summary-limit-to-extra
1926   "p" gnus-summary-limit-to-display-predicate
1927   "E" gnus-summary-limit-include-expunged
1928   "c" gnus-summary-limit-exclude-childless-dormant
1929   "C" gnus-summary-limit-mark-excluded-as-read
1930   "o" gnus-summary-insert-old-articles
1931   "N" gnus-summary-insert-new-articles
1932   "S" gnus-summary-limit-to-singletons
1933   "r" gnus-summary-limit-to-replied
1934   "R" gnus-summary-limit-to-recipient
1935   "A" gnus-summary-limit-to-address)
1936
1937 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1938   "n" gnus-summary-next-unread-article
1939   "p" gnus-summary-prev-unread-article
1940   "N" gnus-summary-next-article
1941   "P" gnus-summary-prev-article
1942   "\C-n" gnus-summary-next-same-subject
1943   "\C-p" gnus-summary-prev-same-subject
1944   "\M-n" gnus-summary-next-unread-subject
1945   "\M-p" gnus-summary-prev-unread-subject
1946   "f" gnus-summary-first-unread-article
1947   "b" gnus-summary-best-unread-article
1948   "j" gnus-summary-goto-article
1949   "g" gnus-summary-goto-subject
1950   "l" gnus-summary-goto-last-article
1951   "o" gnus-summary-pop-article)
1952
1953 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1954   "k" gnus-summary-kill-thread
1955   "E" gnus-summary-expire-thread
1956   "l" gnus-summary-lower-thread
1957   "i" gnus-summary-raise-thread
1958   "T" gnus-summary-toggle-threads
1959   "t" gnus-summary-rethread-current
1960   "^" gnus-summary-reparent-thread
1961   "\M-^" gnus-summary-reparent-children
1962   "s" gnus-summary-show-thread
1963   "S" gnus-summary-show-all-threads
1964   "h" gnus-summary-hide-thread
1965   "H" gnus-summary-hide-all-threads
1966   "n" gnus-summary-next-thread
1967   "p" gnus-summary-prev-thread
1968   "u" gnus-summary-up-thread
1969   "o" gnus-summary-top-thread
1970   "d" gnus-summary-down-thread
1971   "#" gnus-uu-mark-thread
1972   "\M-#" gnus-uu-unmark-thread)
1973
1974 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1975   "g" gnus-summary-prepare
1976   "c" gnus-summary-insert-cached-articles
1977   "d" gnus-summary-insert-dormant-articles
1978   "t" gnus-summary-insert-ticked-articles)
1979
1980 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1981   "c" gnus-summary-catchup-and-exit
1982   "C" gnus-summary-catchup-all-and-exit
1983   "E" gnus-summary-exit-no-update
1984   "Q" gnus-summary-exit
1985   "Z" gnus-summary-exit
1986   "n" gnus-summary-catchup-and-goto-next-group
1987   "p" gnus-summary-catchup-and-goto-prev-group
1988   "R" gnus-summary-reselect-current-group
1989   "G" gnus-summary-rescan-group
1990   "N" gnus-summary-next-group
1991   "s" gnus-summary-save-newsrc
1992   "P" gnus-summary-prev-group)
1993
1994 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1995   " " gnus-summary-next-page
1996   "n" gnus-summary-next-page
1997   "\177" gnus-summary-prev-page
1998   [delete] gnus-summary-prev-page
1999   "p" gnus-summary-prev-page
2000   "\r" gnus-summary-scroll-up
2001   "\M-\r" gnus-summary-scroll-down
2002   "<" gnus-summary-beginning-of-article
2003   ">" gnus-summary-end-of-article
2004   "b" gnus-summary-beginning-of-article
2005   "e" gnus-summary-end-of-article
2006   "^" gnus-summary-refer-parent-article
2007   "r" gnus-summary-refer-parent-article
2008   "D" gnus-summary-enter-digest-group
2009   "R" gnus-summary-refer-references
2010   "T" gnus-summary-refer-thread
2011   "g" gnus-summary-show-article
2012   "s" gnus-summary-isearch-article
2013   "P" gnus-summary-print-article
2014   "S" gnus-sticky-article
2015   "M" gnus-mailing-list-insinuate
2016   "t" gnus-article-babel)
2017
2018 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2019   "b" gnus-article-add-buttons
2020   "B" gnus-article-add-buttons-to-head
2021   "o" gnus-article-treat-overstrike
2022   "e" gnus-article-emphasize
2023   "w" gnus-article-fill-cited-article
2024   "Q" gnus-article-fill-long-lines
2025   "L" gnus-article-toggle-truncate-lines
2026   "C" gnus-article-capitalize-sentences
2027   "c" gnus-article-remove-cr
2028   "q" gnus-article-de-quoted-unreadable
2029   "6" gnus-article-de-base64-unreadable
2030   "Z" gnus-article-decode-HZ
2031   "A" gnus-article-treat-ansi-sequences
2032   "h" gnus-article-wash-html
2033   "u" gnus-article-unsplit-urls
2034   "s" gnus-summary-force-verify-and-decrypt
2035   "f" gnus-article-display-x-face
2036   "l" gnus-summary-stop-page-breaking
2037   "r" gnus-summary-caesar-message
2038   "m" gnus-summary-morse-message
2039   "t" gnus-summary-toggle-header
2040   "g" gnus-treat-smiley
2041   "v" gnus-summary-verbose-headers
2042   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2043   "p" gnus-article-verify-x-pgp-sig
2044   "d" gnus-article-treat-dumbquotes
2045   "i" gnus-summary-idna-message)
2046
2047 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2048   ;; mnemonic: deuglif*Y*
2049   "u" gnus-article-outlook-unwrap-lines
2050   "a" gnus-article-outlook-repair-attribution
2051   "c" gnus-article-outlook-rearrange-citation
2052   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2053
2054 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2055   "a" gnus-article-hide
2056   "h" gnus-article-hide-headers
2057   "b" gnus-article-hide-boring-headers
2058   "s" gnus-article-hide-signature
2059   "c" gnus-article-hide-citation
2060   "C" gnus-article-hide-citation-in-followups
2061   "l" gnus-article-hide-list-identifiers
2062   "B" gnus-article-strip-banner
2063   "P" gnus-article-hide-pem
2064   "\C-c" gnus-article-hide-citation-maybe)
2065
2066 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2067   "a" gnus-article-highlight
2068   "h" gnus-article-highlight-headers
2069   "c" gnus-article-highlight-citation
2070   "s" gnus-article-highlight-signature)
2071
2072 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2073   "f" gnus-article-treat-fold-headers
2074   "u" gnus-article-treat-unfold-headers
2075   "n" gnus-article-treat-fold-newsgroups)
2076
2077 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2078   "x" gnus-article-display-x-face
2079   "d" gnus-article-display-face
2080   "s" gnus-treat-smiley
2081   "D" gnus-article-remove-images
2082   "f" gnus-treat-from-picon
2083   "m" gnus-treat-mail-picon
2084   "n" gnus-treat-newsgroups-picon)
2085
2086 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2087   "w" gnus-article-decode-mime-words
2088   "c" gnus-article-decode-charset
2089   "v" gnus-mime-view-all-parts
2090   "b" gnus-article-view-part)
2091
2092 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2093   "z" gnus-article-date-ut
2094   "u" gnus-article-date-ut
2095   "l" gnus-article-date-local
2096   "p" gnus-article-date-english
2097   "e" gnus-article-date-lapsed
2098   "o" gnus-article-date-original
2099   "i" gnus-article-date-iso8601
2100   "s" gnus-article-date-user)
2101
2102 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2103   "t" gnus-article-remove-trailing-blank-lines
2104   "l" gnus-article-strip-leading-blank-lines
2105   "m" gnus-article-strip-multiple-blank-lines
2106   "a" gnus-article-strip-blank-lines
2107   "A" gnus-article-strip-all-blank-lines
2108   "s" gnus-article-strip-leading-space
2109   "e" gnus-article-strip-trailing-space
2110   "w" gnus-article-remove-leading-whitespace)
2111
2112 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2113   "v" gnus-version
2114   "f" gnus-summary-fetch-faq
2115   "d" gnus-summary-describe-group
2116   "h" gnus-summary-describe-briefly
2117   "i" gnus-info-find-node
2118   "c" gnus-group-fetch-charter
2119   "C" gnus-group-fetch-control)
2120
2121 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2122   "e" gnus-summary-expire-articles
2123   "\M-\C-e" gnus-summary-expire-articles-now
2124   "\177" gnus-summary-delete-article
2125   [delete] gnus-summary-delete-article
2126   [backspace] gnus-summary-delete-article
2127   "m" gnus-summary-move-article
2128   "r" gnus-summary-respool-article
2129   "w" gnus-summary-edit-article
2130   "c" gnus-summary-copy-article
2131   "B" gnus-summary-crosspost-article
2132   "q" gnus-summary-respool-query
2133   "t" gnus-summary-respool-trace
2134   "i" gnus-summary-import-article
2135   "I" gnus-summary-create-article
2136   "p" gnus-summary-article-posted-p)
2137
2138 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2139   "o" gnus-summary-save-article
2140   "m" gnus-summary-save-article-mail
2141   "F" gnus-summary-write-article-file
2142   "r" gnus-summary-save-article-rmail
2143   "f" gnus-summary-save-article-file
2144   "b" gnus-summary-save-article-body-file
2145   "B" gnus-summary-write-article-body-file
2146   "h" gnus-summary-save-article-folder
2147   "v" gnus-summary-save-article-vm
2148   "p" gnus-summary-pipe-output
2149   "P" gnus-summary-muttprint
2150   "s" gnus-soup-add-article)
2151
2152 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2153   "b" gnus-summary-display-buttonized
2154   "m" gnus-summary-repair-multipart
2155   "v" gnus-article-view-part
2156   "o" gnus-article-save-part
2157   "O" gnus-article-save-part-and-strip
2158   "r" gnus-article-replace-part
2159   "d" gnus-article-delete-part
2160   "t" gnus-article-view-part-as-type
2161   "j" gnus-article-jump-to-part
2162   "c" gnus-article-copy-part
2163   "C" gnus-article-view-part-as-charset
2164   "e" gnus-article-view-part-externally
2165   "H" gnus-article-browse-html-article
2166   "E" gnus-article-encrypt-body
2167   "i" gnus-article-inline-part
2168   "|" gnus-article-pipe-part)
2169
2170 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2171   "p" gnus-summary-mark-as-processable
2172   "u" gnus-summary-unmark-as-processable
2173   "U" gnus-summary-unmark-all-processable
2174   "v" gnus-uu-mark-over
2175   "s" gnus-uu-mark-series
2176   "r" gnus-uu-mark-region
2177   "g" gnus-uu-unmark-region
2178   "R" gnus-uu-mark-by-regexp
2179   "G" gnus-uu-unmark-by-regexp
2180   "t" gnus-uu-mark-thread
2181   "T" gnus-uu-unmark-thread
2182   "a" gnus-uu-mark-all
2183   "b" gnus-uu-mark-buffer
2184   "S" gnus-uu-mark-sparse
2185   "k" gnus-summary-kill-process-mark
2186   "y" gnus-summary-yank-process-mark
2187   "w" gnus-summary-save-process-mark
2188   "i" gnus-uu-invert-processable)
2189
2190 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2191   ;;"x" gnus-uu-extract-any
2192   "m" gnus-summary-save-parts
2193   "u" gnus-uu-decode-uu
2194   "U" gnus-uu-decode-uu-and-save
2195   "s" gnus-uu-decode-unshar
2196   "S" gnus-uu-decode-unshar-and-save
2197   "o" gnus-uu-decode-save
2198   "O" gnus-uu-decode-save
2199   "b" gnus-uu-decode-binhex
2200   "B" gnus-uu-decode-binhex
2201   "Y" gnus-uu-decode-yenc
2202   "p" gnus-uu-decode-postscript
2203   "P" gnus-uu-decode-postscript-and-save)
2204
2205 (gnus-define-keys
2206     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2207   "u" gnus-uu-decode-uu-view
2208   "U" gnus-uu-decode-uu-and-save-view
2209   "s" gnus-uu-decode-unshar-view
2210   "S" gnus-uu-decode-unshar-and-save-view
2211   "o" gnus-uu-decode-save-view
2212   "O" gnus-uu-decode-save-view
2213   "b" gnus-uu-decode-binhex-view
2214   "B" gnus-uu-decode-binhex-view
2215   "p" gnus-uu-decode-postscript-view
2216   "P" gnus-uu-decode-postscript-and-save-view)
2217
2218 (defvar gnus-article-post-menu nil)
2219
2220 (defconst gnus-summary-menu-maxlen 20)
2221
2222 (defun gnus-summary-menu-split (menu)
2223   ;; If we have lots of elements, divide them into groups of 20
2224   ;; and make a pane (or submenu) for each one.
2225   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2226       (let ((menu menu) sublists next
2227             (i 1))
2228         (while menu
2229           ;; Pull off the next gnus-summary-menu-maxlen elements
2230           ;; and make them the next element of sublist.
2231           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2232           (if next
2233               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2234                       nil))
2235           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2236                                              (aref (car (last menu)) 0)) menu)
2237                                sublists))
2238           (setq i (1+ i))
2239           (setq menu next))
2240         (nreverse sublists))
2241     ;; Few elements--put them all in one pane.
2242     menu))
2243
2244 (defun gnus-summary-make-menu-bar ()
2245   (gnus-turn-off-edit-menu 'summary)
2246
2247   (unless (boundp 'gnus-summary-misc-menu)
2248
2249     (easy-menu-define
2250       gnus-summary-kill-menu gnus-summary-mode-map ""
2251       (cons
2252        "Score"
2253        (nconc
2254         (list
2255          ["Customize" gnus-score-customize t])
2256         (gnus-make-score-map 'increase)
2257         (gnus-make-score-map 'lower)
2258         '(("Mark"
2259            ["Kill below" gnus-summary-kill-below t]
2260            ["Mark above" gnus-summary-mark-above t]
2261            ["Tick above" gnus-summary-tick-above t]
2262            ["Clear above" gnus-summary-clear-above t])
2263           ["Current score" gnus-summary-current-score t]
2264           ["Set score" gnus-summary-set-score t]
2265           ["Switch current score file..." gnus-score-change-score-file t]
2266           ["Set mark below..." gnus-score-set-mark-below t]
2267           ["Set expunge below..." gnus-score-set-expunge-below t]
2268           ["Edit current score file" gnus-score-edit-current-scores t]
2269           ["Edit score file" gnus-score-edit-file t]
2270           ["Trace score" gnus-score-find-trace t]
2271           ["Find words" gnus-score-find-favourite-words t]
2272           ["Rescore buffer" gnus-summary-rescore t]
2273           ["Increase score..." gnus-summary-increase-score t]
2274           ["Lower score..." gnus-summary-lower-score t]))))
2275
2276     ;; Define both the Article menu in the summary buffer and the
2277     ;; equivalent Commands menu in the article buffer here for
2278     ;; consistency.
2279     (let ((innards
2280            `(("Hide"
2281               ["All" gnus-article-hide t]
2282               ["Headers" gnus-article-hide-headers t]
2283               ["Signature" gnus-article-hide-signature t]
2284               ["Citation" gnus-article-hide-citation t]
2285               ["List identifiers" gnus-article-hide-list-identifiers t]
2286               ["Banner" gnus-article-strip-banner t]
2287               ["Boring headers" gnus-article-hide-boring-headers t])
2288              ("Highlight"
2289               ["All" gnus-article-highlight t]
2290               ["Headers" gnus-article-highlight-headers t]
2291               ["Signature" gnus-article-highlight-signature t]
2292               ["Citation" gnus-article-highlight-citation t])
2293              ("MIME"
2294               ["Words" gnus-article-decode-mime-words t]
2295               ["Charset" gnus-article-decode-charset t]
2296               ["QP" gnus-article-de-quoted-unreadable t]
2297               ["Base64" gnus-article-de-base64-unreadable t]
2298               ["View MIME buttons" gnus-summary-display-buttonized t]
2299               ["View all" gnus-mime-view-all-parts t]
2300               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2301               ["Encrypt body" gnus-article-encrypt-body
2302                :active (not (gnus-group-read-only-p))
2303                ,@(if (featurep 'xemacs) nil
2304                    '(:help "Encrypt the message body on disk"))]
2305               ["Extract all parts..." gnus-summary-save-parts t]
2306               ("Multipart"
2307                ["Repair multipart" gnus-summary-repair-multipart t]
2308                ["Pipe part..." gnus-article-pipe-part t]
2309                ["Inline part" gnus-article-inline-part t]
2310                ["View part as type..." gnus-article-view-part-as-type t]
2311                ["Encrypt body" gnus-article-encrypt-body
2312                 :active (not (gnus-group-read-only-p))
2313                ,@(if (featurep 'xemacs) nil
2314                    '(:help "Encrypt the message body on disk"))]
2315                ["View part externally" gnus-article-view-part-externally t]
2316                ["View HTML parts in browser" gnus-article-browse-html-article t]
2317                ["View part with charset..." gnus-article-view-part-as-charset t]
2318                ["Copy part" gnus-article-copy-part t]
2319                ["Save part..." gnus-article-save-part t]
2320                ["View part" gnus-article-view-part t]))
2321              ("Date"
2322               ["Local" gnus-article-date-local t]
2323               ["ISO8601" gnus-article-date-iso8601 t]
2324               ["UT" gnus-article-date-ut t]
2325               ["Original" gnus-article-date-original t]
2326               ["Lapsed" gnus-article-date-lapsed t]
2327               ["User-defined" gnus-article-date-user t])
2328              ("Display"
2329               ["Remove images" gnus-article-remove-images t]
2330               ["Toggle smiley" gnus-treat-smiley t]
2331               ["Show X-Face" gnus-article-display-x-face t]
2332               ["Show picons in From" gnus-treat-from-picon t]
2333               ["Show picons in mail headers" gnus-treat-mail-picon t]
2334               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2335               ("View as different encoding"
2336                ,@(gnus-summary-menu-split
2337                   (mapcar
2338                    (lambda (cs)
2339                      ;; Since easymenu under Emacs doesn't allow
2340                      ;; lambda forms for menu commands, we should
2341                      ;; provide intern'ed function symbols.
2342                      (let ((command (intern (format "\
2343 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2344                        (fset command
2345                              `(lambda ()
2346                                 (interactive)
2347                                 (let ((gnus-summary-show-article-charset-alist
2348                                        '((1 . ,cs))))
2349                                   (gnus-summary-show-article 1))))
2350                        `[,(symbol-name cs) ,command t]))
2351                    (sort (if (fboundp 'coding-system-list)
2352                              (coding-system-list)
2353                            (mapcar 'car mm-mime-mule-charset-alist))
2354                          'string<)))))
2355              ("Washing"
2356               ("Remove Blanks"
2357                ["Leading" gnus-article-strip-leading-blank-lines t]
2358                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2359                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2360                ["All of the above" gnus-article-strip-blank-lines t]
2361                ["All" gnus-article-strip-all-blank-lines t]
2362                ["Leading space" gnus-article-strip-leading-space t]
2363                ["Trailing space" gnus-article-strip-trailing-space t]
2364                ["Leading space in headers"
2365                 gnus-article-remove-leading-whitespace t])
2366               ["Overstrike" gnus-article-treat-overstrike t]
2367               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2368               ["Emphasis" gnus-article-emphasize t]
2369               ["Word wrap" gnus-article-fill-cited-article t]
2370               ["Fill long lines" gnus-article-fill-long-lines t]
2371               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2372               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2373               ["Remove CR" gnus-article-remove-cr t]
2374               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2375               ["Base64" gnus-article-de-base64-unreadable t]
2376               ["Rot 13" gnus-summary-caesar-message
2377                ,@(if (featurep 'xemacs) '(t)
2378                    '(:help "\"Caesar rotate\" article by 13"))]
2379               ["De-IDNA" gnus-summary-idna-message t]
2380               ["Morse decode" gnus-summary-morse-message t]
2381               ["Unix pipe..." gnus-summary-pipe-message t]
2382               ["Add buttons" gnus-article-add-buttons t]
2383               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2384               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2385               ["Verbose header" gnus-summary-verbose-headers t]
2386               ["Toggle header" gnus-summary-toggle-header t]
2387               ["Unfold headers" gnus-article-treat-unfold-headers t]
2388               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2389               ["Html" gnus-article-wash-html t]
2390               ["Unsplit URLs" gnus-article-unsplit-urls t]
2391               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2392               ["Decode HZ" gnus-article-decode-HZ t]
2393               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2394               ("(Outlook) Deuglify"
2395                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2396                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2397                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2398                ["Full (Outlook) deuglify"
2399                 gnus-article-outlook-deuglify-article t])
2400               )
2401              ("Output"
2402               ["Save in default format..." gnus-summary-save-article
2403                ,@(if (featurep 'xemacs) '(t)
2404                    '(:help "Save article using default method"))]
2405               ["Save in file..." gnus-summary-save-article-file
2406                ,@(if (featurep 'xemacs) '(t)
2407                    '(:help "Save article in file"))]
2408               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2409               ["Save in MH folder..." gnus-summary-save-article-folder t]
2410               ["Save in VM folder..." gnus-summary-save-article-vm t]
2411               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2412               ["Save body in file..." gnus-summary-save-article-body-file t]
2413               ["Pipe through a filter..." gnus-summary-pipe-output t]
2414               ["Add to SOUP packet" gnus-soup-add-article t]
2415               ["Print with Muttprint..." gnus-summary-muttprint t]
2416               ["Print" gnus-summary-print-article
2417                ,@(if (featurep 'xemacs) '(t)
2418                    '(:help "Generate and print a PostScript image"))])
2419              ("Copy, move,... (Backend)"
2420               ,@(if (featurep 'xemacs) nil
2421                   '(:help "Copying, moving, expiring articles..."))
2422               ["Respool article..." gnus-summary-respool-article t]
2423               ["Move article..." gnus-summary-move-article
2424                (gnus-check-backend-function
2425                 'request-move-article gnus-newsgroup-name)]
2426               ["Copy article..." gnus-summary-copy-article t]
2427               ["Crosspost article..." gnus-summary-crosspost-article
2428                (gnus-check-backend-function
2429                 'request-replace-article gnus-newsgroup-name)]
2430               ["Import file..." gnus-summary-import-article
2431                (gnus-check-backend-function
2432                 'request-accept-article gnus-newsgroup-name)]
2433               ["Create article..." gnus-summary-create-article
2434                (gnus-check-backend-function
2435                 'request-accept-article gnus-newsgroup-name)]
2436               ["Check if posted" gnus-summary-article-posted-p t]
2437               ["Edit article" gnus-summary-edit-article
2438                (not (gnus-group-read-only-p))]
2439               ["Delete article" gnus-summary-delete-article
2440                (gnus-check-backend-function
2441                 'request-expire-articles gnus-newsgroup-name)]
2442               ["Query respool" gnus-summary-respool-query t]
2443               ["Trace respool" gnus-summary-respool-trace t]
2444               ["Delete expirable articles" gnus-summary-expire-articles-now
2445                (gnus-check-backend-function
2446                 'request-expire-articles gnus-newsgroup-name)])
2447              ("Extract"
2448               ["Uudecode" gnus-uu-decode-uu
2449                ,@(if (featurep 'xemacs) '(t)
2450                    '(:help "Decode uuencoded article(s)"))]
2451               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2452               ["Unshar" gnus-uu-decode-unshar t]
2453               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2454               ["Save" gnus-uu-decode-save t]
2455               ["Binhex" gnus-uu-decode-binhex t]
2456               ["Postscript" gnus-uu-decode-postscript t]
2457               ["All MIME parts" gnus-summary-save-parts t])
2458              ("Cache"
2459               ["Enter article" gnus-cache-enter-article t]
2460               ["Remove article" gnus-cache-remove-article t])
2461              ["Translate" gnus-article-babel t]
2462              ["Select article buffer" gnus-summary-select-article-buffer t]
2463              ["Make article buffer sticky" gnus-sticky-article t]
2464              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2465              ["Isearch article..." gnus-summary-isearch-article t]
2466              ["Beginning of the article" gnus-summary-beginning-of-article t]
2467              ["End of the article" gnus-summary-end-of-article t]
2468              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2469              ["Fetch referenced articles" gnus-summary-refer-references t]
2470              ["Fetch current thread" gnus-summary-refer-thread t]
2471              ["Fetch article with id..." gnus-summary-refer-article t]
2472              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2473              ["Redisplay" gnus-summary-show-article t]
2474              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2475       (easy-menu-define
2476         gnus-summary-article-menu gnus-summary-mode-map ""
2477         (cons "Article" innards))
2478
2479       (if (not (keymapp gnus-summary-article-menu))
2480           (easy-menu-define
2481             gnus-article-commands-menu gnus-article-mode-map ""
2482             (cons "Commands" innards))
2483         ;; in Emacs, don't share menu.
2484         (setq gnus-article-commands-menu
2485               (copy-keymap gnus-summary-article-menu))
2486         (define-key gnus-article-mode-map [menu-bar commands]
2487           (cons "Commands" gnus-article-commands-menu))))
2488
2489     (easy-menu-define
2490       gnus-summary-thread-menu gnus-summary-mode-map ""
2491       '("Threads"
2492         ["Find all messages in thread" gnus-summary-refer-thread t]
2493         ["Toggle threading" gnus-summary-toggle-threads t]
2494         ["Hide threads" gnus-summary-hide-all-threads t]
2495         ["Show threads" gnus-summary-show-all-threads t]
2496         ["Hide thread" gnus-summary-hide-thread t]
2497         ["Show thread" gnus-summary-show-thread t]
2498         ["Go to next thread" gnus-summary-next-thread t]
2499         ["Go to previous thread" gnus-summary-prev-thread t]
2500         ["Go down thread" gnus-summary-down-thread t]
2501         ["Go up thread" gnus-summary-up-thread t]
2502         ["Top of thread" gnus-summary-top-thread t]
2503         ["Mark thread as read" gnus-summary-kill-thread t]
2504         ["Mark thread as expired" gnus-summary-expire-thread t]
2505         ["Lower thread score" gnus-summary-lower-thread t]
2506         ["Raise thread score" gnus-summary-raise-thread t]
2507         ["Rethread current" gnus-summary-rethread-current t]))
2508
2509     (easy-menu-define
2510       gnus-summary-post-menu gnus-summary-mode-map ""
2511       `("Post"
2512         ["Send a message (mail or news)" gnus-summary-post-news
2513          ,@(if (featurep 'xemacs) '(t)
2514              '(:help "Compose a new message (mail or news)"))]
2515         ["Followup" gnus-summary-followup
2516          ,@(if (featurep 'xemacs) '(t)
2517              '(:help "Post followup to this article"))]
2518         ["Followup and yank" gnus-summary-followup-with-original
2519          ,@(if (featurep 'xemacs) '(t)
2520              '(:help "Post followup to this article, quoting its contents"))]
2521         ["Supersede article" gnus-summary-supersede-article t]
2522         ["Cancel article" gnus-summary-cancel-article
2523          ,@(if (featurep 'xemacs) '(t)
2524              '(:help "Cancel an article you posted"))]
2525         ["Reply" gnus-summary-reply t]
2526         ["Reply and yank" gnus-summary-reply-with-original t]
2527         ["Wide reply" gnus-summary-wide-reply t]
2528         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2529          ,@(if (featurep 'xemacs) '(t)
2530              '(:help "Mail a reply, quoting this article"))]
2531         ["Very wide reply" gnus-summary-very-wide-reply t]
2532         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2533          ,@(if (featurep 'xemacs) '(t)
2534              '(:help "Mail a very wide reply, quoting this article"))]
2535         ["Mail forward" gnus-summary-mail-forward t]
2536         ["Post forward" gnus-summary-post-forward t]
2537         ["Digest and mail" gnus-uu-digest-mail-forward t]
2538         ["Digest and post" gnus-uu-digest-post-forward t]
2539         ["Resend message" gnus-summary-resend-message t]
2540         ["Resend message edit" gnus-summary-resend-message-edit t]
2541         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2542         ["Send a mail" gnus-summary-mail-other-window t]
2543         ["Create a local message" gnus-summary-news-other-window t]
2544         ["Uuencode and post" gnus-uu-post-news
2545          ,@(if (featurep 'xemacs) '(t)
2546              '(:help "Post a uuencoded article"))]
2547         ["Followup via news" gnus-summary-followup-to-mail t]
2548         ["Followup via news and yank"
2549          gnus-summary-followup-to-mail-with-original t]
2550         ;;("Draft"
2551         ;;["Send" gnus-summary-send-draft t]
2552         ;;["Send bounced" gnus-resend-bounced-mail t])
2553         ))
2554
2555     (cond
2556      ((not (keymapp gnus-summary-post-menu))
2557       (setq gnus-article-post-menu gnus-summary-post-menu))
2558      ((not gnus-article-post-menu)
2559       ;; Don't share post menu.
2560       (setq gnus-article-post-menu
2561             (copy-keymap gnus-summary-post-menu))))
2562     (define-key gnus-article-mode-map [menu-bar post]
2563       (cons "Post" gnus-article-post-menu))
2564
2565     (easy-menu-define
2566       gnus-summary-misc-menu gnus-summary-mode-map ""
2567       `("Gnus"
2568         ("Mark Read"
2569          ["Mark as read" gnus-summary-mark-as-read-forward t]
2570          ["Mark same subject and select"
2571           gnus-summary-kill-same-subject-and-select t]
2572          ["Mark same subject" gnus-summary-kill-same-subject t]
2573          ["Catchup" gnus-summary-catchup
2574           ,@(if (featurep 'xemacs) '(t)
2575               '(:help "Mark unread articles in this group as read"))]
2576          ["Catchup all" gnus-summary-catchup-all t]
2577          ["Catchup to here" gnus-summary-catchup-to-here t]
2578          ["Catchup from here" gnus-summary-catchup-from-here t]
2579          ["Catchup region" gnus-summary-mark-region-as-read
2580           (gnus-mark-active-p)]
2581          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2582         ("Mark Various"
2583          ["Tick" gnus-summary-tick-article-forward t]
2584          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2585          ["Remove marks" gnus-summary-clear-mark-forward t]
2586          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2587          ["Set bookmark" gnus-summary-set-bookmark t]
2588          ["Remove bookmark" gnus-summary-remove-bookmark t])
2589         ("Limit to"
2590          ["Marks..." gnus-summary-limit-to-marks t]
2591          ["Subject..." gnus-summary-limit-to-subject t]
2592          ["Author..." gnus-summary-limit-to-author t]
2593          ["Recipient..." gnus-summary-limit-to-recipient t]
2594          ["Address..." gnus-summary-limit-to-address t]
2595          ["Age..." gnus-summary-limit-to-age t]
2596          ["Extra..." gnus-summary-limit-to-extra t]
2597          ["Score..." gnus-summary-limit-to-score t]
2598          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2599          ["Unread" gnus-summary-limit-to-unread t]
2600          ["Unseen" gnus-summary-limit-to-unseen t]
2601          ["Singletons" gnus-summary-limit-to-singletons t]
2602          ["Replied" gnus-summary-limit-to-replied t]
2603          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2604          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2605          ["Pop limit" gnus-summary-pop-limit t]
2606          ["Show dormant" gnus-summary-limit-include-dormant t]
2607          ["Hide childless dormant"
2608           gnus-summary-limit-exclude-childless-dormant t]
2609          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2610          ["Hide marked" gnus-summary-limit-exclude-marks t]
2611          ["Show expunged" gnus-summary-limit-include-expunged t])
2612         ("Process Mark"
2613          ["Set mark" gnus-summary-mark-as-processable t]
2614          ["Remove mark" gnus-summary-unmark-as-processable t]
2615          ["Remove all marks" gnus-summary-unmark-all-processable t]
2616          ["Invert marks" gnus-uu-invert-processable t]
2617          ["Mark above" gnus-uu-mark-over t]
2618          ["Mark series" gnus-uu-mark-series t]
2619          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2620          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2621          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2622          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2623          ["Mark all" gnus-uu-mark-all t]
2624          ["Mark buffer" gnus-uu-mark-buffer t]
2625          ["Mark sparse" gnus-uu-mark-sparse t]
2626          ["Mark thread" gnus-uu-mark-thread t]
2627          ["Unmark thread" gnus-uu-unmark-thread t]
2628          ("Process Mark Sets"
2629           ["Kill" gnus-summary-kill-process-mark t]
2630           ["Yank" gnus-summary-yank-process-mark
2631            gnus-newsgroup-process-stack]
2632           ["Save" gnus-summary-save-process-mark t]
2633           ["Run command on marked..." gnus-summary-universal-argument t]))
2634         ("Scroll article"
2635          ["Page forward" gnus-summary-next-page
2636           ,@(if (featurep 'xemacs) '(t)
2637               '(:help "Show next page of article"))]
2638          ["Page backward" gnus-summary-prev-page
2639           ,@(if (featurep 'xemacs) '(t)
2640               '(:help "Show previous page of article"))]
2641          ["Line forward" gnus-summary-scroll-up t])
2642         ("Move"
2643          ["Next unread article" gnus-summary-next-unread-article t]
2644          ["Previous unread article" gnus-summary-prev-unread-article t]
2645          ["Next article" gnus-summary-next-article t]
2646          ["Previous article" gnus-summary-prev-article t]
2647          ["Next unread subject" gnus-summary-next-unread-subject t]
2648          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2649          ["Next article same subject" gnus-summary-next-same-subject t]
2650          ["Previous article same subject" gnus-summary-prev-same-subject t]
2651          ["First unread article" gnus-summary-first-unread-article t]
2652          ["Best unread article" gnus-summary-best-unread-article t]
2653          ["Go to subject number..." gnus-summary-goto-subject t]
2654          ["Go to article number..." gnus-summary-goto-article t]
2655          ["Go to the last article" gnus-summary-goto-last-article t]
2656          ["Pop article off history" gnus-summary-pop-article t])
2657         ("Sort"
2658          ["Sort by number" gnus-summary-sort-by-number t]
2659          ["Sort by author" gnus-summary-sort-by-author t]
2660          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2661          ["Sort by subject" gnus-summary-sort-by-subject t]
2662          ["Sort by date" gnus-summary-sort-by-date t]
2663          ["Sort by score" gnus-summary-sort-by-score t]
2664          ["Sort by lines" gnus-summary-sort-by-lines t]
2665          ["Sort by characters" gnus-summary-sort-by-chars t]
2666          ["Randomize" gnus-summary-sort-by-random t]
2667          ["Original sort" gnus-summary-sort-by-original t])
2668         ("Help"
2669          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2670          ["Describe group" gnus-summary-describe-group t]
2671          ["Fetch charter" gnus-group-fetch-charter
2672           ,@(if (featurep 'xemacs) nil
2673               '(:help "Display the charter of the current group"))]
2674          ["Fetch control message" gnus-group-fetch-control
2675           ,@(if (featurep 'xemacs) nil
2676               '(:help "Display the archived control message for the current group"))]
2677          ["Read manual" gnus-info-find-node t])
2678         ("Modes"
2679          ["Pick and read" gnus-pick-mode t]
2680          ["Binary" gnus-binary-mode t])
2681         ("Regeneration"
2682          ["Regenerate" gnus-summary-prepare t]
2683          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2684          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2685          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2686          ["Toggle threading" gnus-summary-toggle-threads t])
2687         ["See old articles" gnus-summary-insert-old-articles t]
2688         ["See new articles" gnus-summary-insert-new-articles t]
2689         ["Filter articles..." gnus-summary-execute-command t]
2690         ["Run command on articles..." gnus-summary-universal-argument t]
2691         ["Search articles forward..." gnus-summary-search-article-forward t]
2692         ["Search articles backward..." gnus-summary-search-article-backward t]
2693         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2694         ["Expand window" gnus-summary-expand-window t]
2695         ["Expire expirable articles" gnus-summary-expire-articles
2696          (gnus-check-backend-function
2697           'request-expire-articles gnus-newsgroup-name)]
2698         ["Edit local kill file" gnus-summary-edit-local-kill t]
2699         ["Edit main kill file" gnus-summary-edit-global-kill t]
2700         ["Edit group parameters" gnus-summary-edit-parameters t]
2701         ["Customize group parameters" gnus-summary-customize-parameters t]
2702         ["Send a bug report" gnus-bug t]
2703         ("Exit"
2704          ["Catchup and exit" gnus-summary-catchup-and-exit
2705           ,@(if (featurep 'xemacs) '(t)
2706               '(:help "Mark unread articles in this group as read, then exit"))]
2707          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2708          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2709          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2710          ["Exit group" gnus-summary-exit
2711           ,@(if (featurep 'xemacs) '(t)
2712               '(:help "Exit current group, return to group selection mode"))]
2713          ["Exit group without updating" gnus-summary-exit-no-update t]
2714          ["Exit and goto next group" gnus-summary-next-group t]
2715          ["Exit and goto prev group" gnus-summary-prev-group t]
2716          ["Reselect group" gnus-summary-reselect-current-group t]
2717          ["Rescan group" gnus-summary-rescan-group t]
2718          ["Update dribble" gnus-summary-save-newsrc t])))
2719
2720     (gnus-run-hooks 'gnus-summary-menu-hook)))
2721
2722 (defvar gnus-summary-tool-bar-map nil)
2723
2724 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2725 ;; affect _new_ message buffers.  We might add a function that walks thru all
2726 ;; summary-mode buffers and force the update.
2727 (defun gnus-summary-tool-bar-update (&optional symbol value)
2728   "Update summary mode toolbar.
2729 Setter function for custom variables."
2730   (setq-default gnus-summary-tool-bar-map nil)
2731   (when symbol
2732     ;; When used as ":set" function:
2733     (set-default symbol value))
2734   (when (gnus-buffer-live-p gnus-summary-buffer)
2735     (with-current-buffer gnus-summary-buffer
2736       (gnus-summary-make-tool-bar))))
2737
2738 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2739                                      'gnus-summary-tool-bar-gnome
2740                                    'gnus-summary-tool-bar-retro)
2741   "Specifies the Gnus summary tool bar.
2742
2743 It can be either a list or a symbol refering to a list.  See
2744 `gmm-tool-bar-from-list' for the format of the list.  The
2745 default key map is `gnus-summary-mode-map'.
2746
2747 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2748 `gnus-summary-tool-bar-retro'."
2749   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2750                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2751                  (repeat :tag "User defined list" gmm-tool-bar-item)
2752                  (symbol))
2753   :version "23.0" ;; No Gnus
2754   :initialize 'custom-initialize-default
2755   :set 'gnus-summary-tool-bar-update
2756   :group 'gnus-summary)
2757
2758 (defcustom gnus-summary-tool-bar-gnome
2759   '((gnus-summary-post-news "mail/compose" nil)
2760     (gnus-summary-insert-new-articles "mail/inbox" nil
2761                                       :visible (or (not gnus-agent)
2762                                                    gnus-plugged))
2763     (gnus-summary-reply-with-original "mail/reply")
2764     (gnus-summary-reply "mail/reply" nil :visible nil)
2765     (gnus-summary-followup-with-original "mail/reply-all")
2766     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2767     (gnus-summary-mail-forward "mail/forward")
2768     (gnus-summary-save-article "mail/save")
2769     (gnus-summary-search-article-forward "search" nil :visible nil)
2770     (gnus-summary-print-article "print")
2771     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2772     ;; Some new commands that may need more suitable icons:
2773     (gnus-summary-save-newsrc "save" nil :visible nil)
2774     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2775     (gnus-summary-prev-article "left-arrow")
2776     (gnus-summary-next-article "right-arrow")
2777     (gnus-summary-next-page "next-page")
2778     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2779     ;;
2780     ;; Maybe some sort-by-... could be added:
2781     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2782     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2783     (gnus-summary-mark-as-expirable
2784      "delete" nil
2785      :visible (gnus-check-backend-function 'request-expire-articles
2786                                            gnus-newsgroup-name))
2787     (gnus-summary-mark-as-spam
2788      "mail/spam" t
2789      :visible (and (fboundp 'spam-group-ham-contents-p)
2790                    (spam-group-ham-contents-p gnus-newsgroup-name))
2791      :help "Mark as spam")
2792     (gnus-summary-mark-as-read-forward
2793      "mail/not-spam" nil
2794      :visible (and (fboundp 'spam-group-spam-contents-p)
2795                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2796     ;;
2797     (gnus-summary-exit "exit")
2798     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2799     (gnus-info-find-node "help"))
2800   "List of functions for the summary tool bar (GNOME style).
2801
2802 See `gmm-tool-bar-from-list' for the format of the list."
2803   :type '(repeat gmm-tool-bar-item)
2804   :version "23.0" ;; No Gnus
2805   :initialize 'custom-initialize-default
2806   :set 'gnus-summary-tool-bar-update
2807   :group 'gnus-summary)
2808
2809 (defcustom gnus-summary-tool-bar-retro
2810   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2811     (gnus-summary-next-unread-article "gnus/next-ur")
2812     (gnus-summary-post-news "gnus/post")
2813     (gnus-summary-followup-with-original "gnus/fuwo")
2814     (gnus-summary-followup "gnus/followup")
2815     (gnus-summary-reply-with-original "gnus/reply-wo")
2816     (gnus-summary-reply "gnus/reply")
2817     (gnus-summary-caesar-message "gnus/rot13")
2818     (gnus-uu-decode-uu "gnus/uu-decode")
2819     (gnus-summary-save-article-file "gnus/save-aif")
2820     (gnus-summary-save-article "gnus/save-art")
2821     (gnus-uu-post-news "gnus/uu-post")
2822     (gnus-summary-catchup "gnus/catchup")
2823     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2824     (gnus-summary-exit "gnus/exit-summ")
2825     ;; Some new command that may need more suitable icons:
2826     (gnus-summary-print-article "gnus/print" nil :visible nil)
2827     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2828     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2829     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2830     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2831     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2832     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2833     ;;
2834     (gnus-info-find-node "gnus/help" nil :visible nil))
2835   "List of functions for the summary tool bar (retro look).
2836
2837 See `gmm-tool-bar-from-list' for the format of the list."
2838   :type '(repeat gmm-tool-bar-item)
2839   :version "23.0" ;; No Gnus
2840   :initialize 'custom-initialize-default
2841   :set 'gnus-summary-tool-bar-update
2842   :group 'gnus-summary)
2843
2844 (defcustom gnus-summary-tool-bar-zap-list t
2845   "List of icon items from the global tool bar.
2846 These items are not displayed in the Gnus summary mode tool bar.
2847
2848 See `gmm-tool-bar-from-list' for the format of the list."
2849   :type 'gmm-tool-bar-zap-list
2850   :version "23.0" ;; No Gnus
2851   :initialize 'custom-initialize-default
2852   :set 'gnus-summary-tool-bar-update
2853   :group 'gnus-summary)
2854
2855 (defvar image-load-path)
2856
2857 (defun gnus-summary-make-tool-bar (&optional force)
2858   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2859 When FORCE, rebuild the tool bar."
2860   (when (and (not (featurep 'xemacs))
2861              (boundp 'tool-bar-mode)
2862              tool-bar-mode
2863              (or (not gnus-summary-tool-bar-map) force))
2864     (let* ((load-path
2865             (gmm-image-load-path-for-library "gnus"
2866                                              "mail/save.xpm"
2867                                              nil t))
2868            (image-load-path (cons (car load-path)
2869                                   (when (boundp 'image-load-path)
2870                                     image-load-path)))
2871            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2872                                         gnus-summary-tool-bar-zap-list
2873                                         'gnus-summary-mode-map)))
2874       (when map
2875         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2876         ;; uses it's value.
2877         (setq gnus-summary-tool-bar-map map))))
2878   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2879
2880 (defun gnus-score-set-default (var value)
2881   "A version of set that updates the GNU Emacs menu-bar."
2882   (set var value)
2883   ;; It is the message that forces the active status to be updated.
2884   (message ""))
2885
2886 (defun gnus-make-score-map (type)
2887   "Make a summary score map of type TYPE."
2888   (if t
2889       nil
2890     (let ((headers '(("author" "from" string)
2891                      ("subject" "subject" string)
2892                      ("article body" "body" string)
2893                      ("article head" "head" string)
2894                      ("xref" "xref" string)
2895                      ("extra header" "extra" string)
2896                      ("lines" "lines" number)
2897                      ("followups to author" "followup" string)))
2898           (types '((number ("less than" <)
2899                            ("greater than" >)
2900                            ("equal" =))
2901                    (string ("substring" s)
2902                            ("exact string" e)
2903                            ("fuzzy string" f)
2904                            ("regexp" r))))
2905           (perms '(("temporary" (current-time-string))
2906                    ("permanent" nil)
2907                    ("immediate" now)))
2908           header)
2909       (list
2910        (apply
2911         'nconc
2912         (list
2913          (if (eq type 'lower)
2914              "Lower score"
2915            "Increase score"))
2916         (let (outh)
2917           (while headers
2918             (setq header (car headers))
2919             (setq outh
2920                   (cons
2921                    (apply
2922                     'nconc
2923                     (list (car header))
2924                     (let ((ts (cdr (assoc (nth 2 header) types)))
2925                           outt)
2926                       (while ts
2927                         (setq outt
2928                               (cons
2929                                (apply
2930                                 'nconc
2931                                 (list (caar ts))
2932                                 (let ((ps perms)
2933                                       outp)
2934                                   (while ps
2935                                     (setq outp
2936                                           (cons
2937                                            (vector
2938                                             (caar ps)
2939                                             (list
2940                                              'gnus-summary-score-entry
2941                                              (nth 1 header)
2942                                              (if (or (string= (nth 1 header)
2943                                                               "head")
2944                                                      (string= (nth 1 header)
2945                                                               "body"))
2946                                                  ""
2947                                                (list 'gnus-summary-header
2948                                                      (nth 1 header)))
2949                                              (list 'quote (nth 1 (car ts)))
2950                                              (list 'gnus-score-delta-default
2951                                                    nil)
2952                                              (nth 1 (car ps))
2953                                              t)
2954                                             t)
2955                                            outp))
2956                                     (setq ps (cdr ps)))
2957                                   (list (nreverse outp))))
2958                                outt))
2959                         (setq ts (cdr ts)))
2960                       (list (nreverse outt))))
2961                    outh))
2962             (setq headers (cdr headers)))
2963           (list (nreverse outh))))))))
2964
2965
2966 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
2967
2968 \f
2969
2970 (defun gnus-summary-mode (&optional group)
2971   "Major mode for reading articles.
2972
2973 All normal editing commands are switched off.
2974 \\<gnus-summary-mode-map>
2975 Each line in this buffer represents one article.  To read an
2976 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2977 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2978 respectively.
2979
2980 You can also post articles and send mail from this buffer.  To
2981 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2982 of an article, type `\\[gnus-summary-reply]'.
2983
2984 There are approx. one gazillion commands you can execute in this
2985 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2986
2987 The following commands are available:
2988
2989 \\{gnus-summary-mode-map}"
2990   (interactive)
2991   (kill-all-local-variables)
2992   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2993     (gnus-summary-make-local-variables))
2994   (gnus-summary-make-local-variables)
2995   (setq gnus-newsgroup-name group)
2996   (when (gnus-visual-p 'summary-menu 'menu)
2997     (gnus-summary-make-menu-bar)
2998     (gnus-summary-make-tool-bar))
2999   (gnus-make-thread-indent-array)
3000   (gnus-simplify-mode-line)
3001   (setq major-mode 'gnus-summary-mode)
3002   (setq mode-name "Summary")
3003   (make-local-variable 'minor-mode-alist)
3004   (use-local-map gnus-summary-mode-map)
3005   (buffer-disable-undo)
3006   (setq buffer-read-only t              ;Disable modification
3007         show-trailing-whitespace nil)
3008   (setq truncate-lines t)
3009   (setq selective-display t)
3010   (setq selective-display-ellipses t)   ;Display `...'
3011   (gnus-summary-set-display-table)
3012   (gnus-set-default-directory)
3013   (make-local-variable 'gnus-summary-line-format)
3014   (make-local-variable 'gnus-summary-line-format-spec)
3015   (make-local-variable 'gnus-summary-dummy-line-format)
3016   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3017   (make-local-variable 'gnus-summary-mark-positions)
3018   (gnus-make-local-hook 'pre-command-hook)
3019   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3020   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3021   (turn-on-gnus-mailing-list-mode)
3022   (mm-enable-multibyte)
3023   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3024   (gnus-update-summary-mark-positions))
3025
3026 (defun gnus-summary-make-local-variables ()
3027   "Make all the local summary buffer variables."
3028   (let (global)
3029     (dolist (local gnus-summary-local-variables)
3030       (if (consp local)
3031           (progn
3032             (if (eq (cdr local) 'global)
3033                 ;; Copy the global value of the variable.
3034                 (setq global (symbol-value (car local)))
3035               ;; Use the value from the list.
3036               (setq global (eval (cdr local))))
3037             (set (make-local-variable (car local)) global))
3038         ;; Simple nil-valued local variable.
3039         (set (make-local-variable local) nil)))))
3040
3041 (defun gnus-summary-clear-local-variables ()
3042   (let ((locals gnus-summary-local-variables))
3043     (while locals
3044       (if (consp (car locals))
3045           (and (symbolp (caar locals))
3046                (set (caar locals) nil))
3047         (and (symbolp (car locals))
3048              (set (car locals) nil)))
3049       (setq locals (cdr locals)))))
3050
3051 ;; Summary data functions.
3052
3053 (defmacro gnus-data-number (data)
3054   `(car ,data))
3055
3056 (defmacro gnus-data-set-number (data number)
3057   `(setcar ,data ,number))
3058
3059 (defmacro gnus-data-mark (data)
3060   `(nth 1 ,data))
3061
3062 (defmacro gnus-data-set-mark (data mark)
3063   `(setcar (nthcdr 1 ,data) ,mark))
3064
3065 (defmacro gnus-data-pos (data)
3066   `(nth 2 ,data))
3067
3068 (defmacro gnus-data-set-pos (data pos)
3069   `(setcar (nthcdr 2 ,data) ,pos))
3070
3071 (defmacro gnus-data-header (data)
3072   `(nth 3 ,data))
3073
3074 (defmacro gnus-data-set-header (data header)
3075   `(setf (nth 3 ,data) ,header))
3076
3077 (defmacro gnus-data-level (data)
3078   `(nth 4 ,data))
3079
3080 (defmacro gnus-data-unread-p (data)
3081   `(= (nth 1 ,data) gnus-unread-mark))
3082
3083 (defmacro gnus-data-read-p (data)
3084   `(/= (nth 1 ,data) gnus-unread-mark))
3085
3086 (defmacro gnus-data-pseudo-p (data)
3087   `(consp (nth 3 ,data)))
3088
3089 (defmacro gnus-data-find (number)
3090   `(assq ,number gnus-newsgroup-data))
3091
3092 (defmacro gnus-data-find-list (number &optional data)
3093   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3094      (memq (assq ,number bdata)
3095            bdata)))
3096
3097 (defmacro gnus-data-make (number mark pos header level)
3098   `(list ,number ,mark ,pos ,header ,level))
3099
3100 (defun gnus-data-enter (after-article number mark pos header level offset)
3101   (let ((data (gnus-data-find-list after-article)))
3102     (unless data
3103       (error "No such article: %d" after-article))
3104     (setcdr data (cons (gnus-data-make number mark pos header level)
3105                        (cdr data)))
3106     (setq gnus-newsgroup-data-reverse nil)
3107     (gnus-data-update-list (cddr data) offset)))
3108
3109 (defun gnus-data-enter-list (after-article list &optional offset)
3110   (when list
3111     (let ((data (and after-article (gnus-data-find-list after-article)))
3112           (ilist list))
3113       (if (not (or data
3114                    after-article))
3115           (let ((odata gnus-newsgroup-data))
3116             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3117             (when offset
3118               (gnus-data-update-list odata offset)))
3119         ;; Find the last element in the list to be spliced into the main
3120         ;; list.
3121         (setq list (last list))
3122         (if (not data)
3123             (progn
3124               (setcdr list gnus-newsgroup-data)
3125               (setq gnus-newsgroup-data ilist)
3126               (when offset
3127                 (gnus-data-update-list (cdr list) offset)))
3128           (setcdr list (cdr data))
3129           (setcdr data ilist)
3130           (when offset
3131             (gnus-data-update-list (cdr list) offset))))
3132       (setq gnus-newsgroup-data-reverse nil))))
3133
3134 (defun gnus-data-remove (article &optional offset)
3135   (let ((data gnus-newsgroup-data))
3136     (if (= (gnus-data-number (car data)) article)
3137         (progn
3138           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3139                 gnus-newsgroup-data-reverse nil)
3140           (when offset
3141             (gnus-data-update-list gnus-newsgroup-data offset)))
3142       (while (cdr data)
3143         (when (= (gnus-data-number (cadr data)) article)
3144           (setcdr data (cddr data))
3145           (when offset
3146             (gnus-data-update-list (cdr data) offset))
3147           (setq data nil
3148                 gnus-newsgroup-data-reverse nil))
3149         (setq data (cdr data))))))
3150
3151 (defmacro gnus-data-list (backward)
3152   `(if ,backward
3153        (or gnus-newsgroup-data-reverse
3154            (setq gnus-newsgroup-data-reverse
3155                  (reverse gnus-newsgroup-data)))
3156      gnus-newsgroup-data))
3157
3158 (defun gnus-data-update-list (data offset)
3159   "Add OFFSET to the POS of all data entries in DATA."
3160   (setq gnus-newsgroup-data-reverse nil)
3161   (while data
3162     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3163     (setq data (cdr data))))
3164
3165 (defun gnus-summary-article-pseudo-p (article)
3166   "Say whether this article is a pseudo article or not."
3167   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3168
3169 (defmacro gnus-summary-article-sparse-p (article)
3170   "Say whether this article is a sparse article or not."
3171   `(memq ,article gnus-newsgroup-sparse))
3172
3173 (defmacro gnus-summary-article-ancient-p (article)
3174   "Say whether this article is a sparse article or not."
3175   `(memq ,article gnus-newsgroup-ancient))
3176
3177 (defun gnus-article-parent-p (number)
3178   "Say whether this article is a parent or not."
3179   (let ((data (gnus-data-find-list number)))
3180     (and (cdr data)              ; There has to be an article after...
3181          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3182             (gnus-data-level (nth 1 data))))))
3183
3184 (defun gnus-article-children (number)
3185   "Return a list of all children to NUMBER."
3186   (let* ((data (gnus-data-find-list number))
3187          (level (gnus-data-level (car data)))
3188          children)
3189     (setq data (cdr data))
3190     (while (and data
3191                 (= (gnus-data-level (car data)) (1+ level)))
3192       (push (gnus-data-number (car data)) children)
3193       (setq data (cdr data)))
3194     children))
3195
3196 (defmacro gnus-summary-skip-intangible ()
3197   "If the current article is intangible, then jump to a different article."
3198   '(let ((to (get-text-property (point) 'gnus-intangible)))
3199      (and to (gnus-summary-goto-subject to))))
3200
3201 (defmacro gnus-summary-article-intangible-p ()
3202   "Say whether this article is intangible or not."
3203   '(get-text-property (point) 'gnus-intangible))
3204
3205 (defun gnus-article-read-p (article)
3206   "Say whether ARTICLE is read or not."
3207   (not (or (memq article gnus-newsgroup-marked)
3208            (memq article gnus-newsgroup-spam-marked)
3209            (memq article gnus-newsgroup-unreads)
3210            (memq article gnus-newsgroup-unselected)
3211            (memq article gnus-newsgroup-dormant))))
3212
3213 ;; Some summary mode macros.
3214
3215 (defmacro gnus-summary-article-number ()
3216   "The article number of the article on the current line.
3217 If there isn't an article number here, then we return the current
3218 article number."
3219   '(progn
3220      (gnus-summary-skip-intangible)
3221      (or (get-text-property (point) 'gnus-number)
3222          (gnus-summary-last-subject))))
3223
3224 (defmacro gnus-summary-article-header (&optional number)
3225   "Return the header of article NUMBER."
3226   `(gnus-data-header (gnus-data-find
3227                       ,(or number '(gnus-summary-article-number)))))
3228
3229 (defmacro gnus-summary-thread-level (&optional number)
3230   "Return the level of thread that starts with article NUMBER."
3231   `(if (and (eq gnus-summary-make-false-root 'dummy)
3232             (get-text-property (point) 'gnus-intangible))
3233        0
3234      (gnus-data-level (gnus-data-find
3235                        ,(or number '(gnus-summary-article-number))))))
3236
3237 (defmacro gnus-summary-article-mark (&optional number)
3238   "Return the mark of article NUMBER."
3239   `(gnus-data-mark (gnus-data-find
3240                     ,(or number '(gnus-summary-article-number)))))
3241
3242 (defmacro gnus-summary-article-pos (&optional number)
3243   "Return the position of the line of article NUMBER."
3244   `(gnus-data-pos (gnus-data-find
3245                    ,(or number '(gnus-summary-article-number)))))
3246
3247 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3248 (defmacro gnus-summary-article-subject (&optional number)
3249   "Return current subject string or nil if nothing."
3250   `(let ((headers
3251           ,(if number
3252                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3253              '(gnus-data-header (assq (gnus-summary-article-number)
3254                                       gnus-newsgroup-data)))))
3255      (and headers
3256           (vectorp headers)
3257           (mail-header-subject headers))))
3258
3259 (defmacro gnus-summary-article-score (&optional number)
3260   "Return current article score."
3261   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3262                   gnus-newsgroup-scored))
3263        gnus-summary-default-score 0))
3264
3265 (defun gnus-summary-article-children (&optional number)
3266   "Return a list of article numbers that are children of article NUMBER."
3267   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3268          (level (gnus-data-level (car data)))
3269          l children)
3270     (while (and (setq data (cdr data))
3271                 (> (setq l (gnus-data-level (car data))) level))
3272       (and (= (1+ level) l)
3273            (push (gnus-data-number (car data))
3274                  children)))
3275     (nreverse children)))
3276
3277 (defun gnus-summary-article-parent (&optional number)
3278   "Return the article number of the parent of article NUMBER."
3279   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3280                                     (gnus-data-list t)))
3281          (level (gnus-data-level (car data))))
3282     (if (zerop level)
3283         ()                              ; This is a root.
3284       ;; We search until we find an article with a level less than
3285       ;; this one.  That function has to be the parent.
3286       (while (and (setq data (cdr data))
3287                   (not (< (gnus-data-level (car data)) level))))
3288       (and data (gnus-data-number (car data))))))
3289
3290 (defun gnus-unread-mark-p (mark)
3291   "Say whether MARK is the unread mark."
3292   (= mark gnus-unread-mark))
3293
3294 (defun gnus-read-mark-p (mark)
3295   "Say whether MARK is one of the marks that mark as read.
3296 This is all marks except unread, ticked, dormant, and expirable."
3297   (not (or (= mark gnus-unread-mark)
3298            (= mark gnus-ticked-mark)
3299            (= mark gnus-spam-mark)
3300            (= mark gnus-dormant-mark)
3301            (= mark gnus-expirable-mark))))
3302
3303 (defmacro gnus-article-mark (number)
3304   "Return the MARK of article NUMBER.
3305 This macro should only be used when computing the mark the \"first\"
3306 time; i.e., when generating the summary lines.  After that,
3307 `gnus-summary-article-mark' should be used to examine the
3308 marks of articles."
3309   `(cond
3310     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3311     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3312     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3313     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3314     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3315     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3316     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3317     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3318            gnus-ancient-mark))))
3319
3320 ;; Saving hidden threads.
3321
3322 (defmacro gnus-save-hidden-threads (&rest forms)
3323   "Save hidden threads, eval FORMS, and restore the hidden threads."
3324   (let ((config (make-symbol "config")))
3325     `(let ((,config (gnus-hidden-threads-configuration)))
3326        (unwind-protect
3327            (save-excursion
3328              ,@forms)
3329          (gnus-restore-hidden-threads-configuration ,config)))))
3330 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3331 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3332
3333 (defun gnus-data-compute-positions ()
3334   "Compute the positions of all articles."
3335   (setq gnus-newsgroup-data-reverse nil)
3336   (let ((data gnus-newsgroup-data))
3337     (save-excursion
3338       (gnus-save-hidden-threads
3339         (gnus-summary-show-all-threads)
3340         (goto-char (point-min))
3341         (while data
3342           (while (get-text-property (point) 'gnus-intangible)
3343             (forward-line 1))
3344           (gnus-data-set-pos (car data) (+ (point) 3))
3345           (setq data (cdr data))
3346           (forward-line 1))))))
3347
3348 (defun gnus-hidden-threads-configuration ()
3349   "Return the current hidden threads configuration."
3350   (save-excursion
3351     (let (config)
3352       (goto-char (point-min))
3353       (while (search-forward "\r" nil t)
3354         (push (1- (point)) config))
3355       config)))
3356
3357 (defun gnus-restore-hidden-threads-configuration (config)
3358   "Restore hidden threads configuration from CONFIG."
3359   (save-excursion
3360     (let (point buffer-read-only)
3361       (while (setq point (pop config))
3362         (when (and (< point (point-max))
3363                    (goto-char point)
3364                    (eq (char-after) ?\n))
3365           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3366
3367 ;; Various summary mode internalish functions.
3368
3369 (defun gnus-mouse-pick-article (e)
3370   (interactive "e")
3371   (mouse-set-point e)
3372   (gnus-summary-next-page nil t))
3373
3374 (defun gnus-summary-set-display-table ()
3375   "Change the display table.
3376 Odd characters have a tendency to mess
3377 up nicely formatted displays - we make all possible glyphs
3378 display only a single character."
3379
3380   ;; We start from the standard display table, if any.
3381   (let ((table (or (copy-sequence standard-display-table)
3382                    (make-display-table)))
3383         (i 32))
3384     ;; Nix out all the control chars...
3385     (while (>= (setq i (1- i)) 0)
3386       (aset table i [??]))
3387    ;; ... but not newline and cr, of course.  (cr is necessary for the
3388     ;; selective display).
3389     (aset table ?\n nil)
3390     (aset table ?\r nil)
3391     ;; We keep TAB as well.
3392     (aset table ?\t nil)
3393     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3394     ;; Emacs 23 (unicode), that are not set already.
3395     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3396                  160
3397                256)))
3398       (while (>= (setq i (1- i)) 127)
3399         ;; Only modify if the entry is nil.
3400         (unless (aref table i)
3401           (aset table i [??]))))
3402     (setq buffer-display-table table)))
3403
3404 (defun gnus-summary-set-article-display-arrow (pos)
3405   "Update the overlay arrow to point to line at position POS."
3406   (when (and gnus-summary-display-arrow
3407              (boundp 'overlay-arrow-position)
3408              (boundp 'overlay-arrow-string))
3409     (save-excursion
3410       (goto-char pos)
3411       (beginning-of-line)
3412       (unless overlay-arrow-position
3413         (setq overlay-arrow-position (make-marker)))
3414       (setq overlay-arrow-string "=>"
3415             overlay-arrow-position (set-marker overlay-arrow-position
3416                                                (point)
3417                                                (current-buffer))))))
3418
3419 (defun gnus-summary-setup-buffer (group)
3420   "Initialize summary buffer."
3421   (let ((buffer (gnus-summary-buffer-name group))
3422         (dead-name (concat "*Dead Summary "
3423                            (gnus-group-decoded-name group) "*")))
3424     ;; If a dead summary buffer exists, we kill it.
3425     (when (gnus-buffer-live-p dead-name)
3426       (gnus-kill-buffer dead-name))
3427     (if (get-buffer buffer)
3428         (progn
3429           (set-buffer buffer)
3430           (setq gnus-summary-buffer (current-buffer))
3431           (not gnus-newsgroup-prepared))
3432       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3433       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3434       (gnus-summary-mode group)
3435       (when gnus-carpal
3436         (gnus-carpal-setup-buffer 'summary))
3437       (when (gnus-group-quit-config group)
3438         (set (make-local-variable 'gnus-single-article-buffer) nil))
3439       (make-local-variable 'gnus-article-buffer)
3440       (make-local-variable 'gnus-article-current)
3441       (make-local-variable 'gnus-original-article-buffer)
3442       (setq gnus-newsgroup-name group)
3443       ;; Set any local variables in the group parameters.
3444       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3445       t)))
3446
3447 (defun gnus-set-global-variables ()
3448   "Set the global equivalents of the buffer-local variables.
3449 They are set to the latest values they had.  These reflect the summary
3450 buffer that was in action when the last article was fetched."
3451   (when (eq major-mode 'gnus-summary-mode)
3452     (setq gnus-summary-buffer (current-buffer))
3453     (let ((name gnus-newsgroup-name)
3454           (marked gnus-newsgroup-marked)
3455           (spam gnus-newsgroup-spam-marked)
3456           (unread gnus-newsgroup-unreads)
3457           (headers gnus-current-headers)
3458           (data gnus-newsgroup-data)
3459           (summary gnus-summary-buffer)
3460           (article-buffer gnus-article-buffer)
3461           (original gnus-original-article-buffer)
3462           (gac gnus-article-current)
3463           (reffed gnus-reffed-article-number)
3464           (score-file gnus-current-score-file)
3465           (default-charset gnus-newsgroup-charset)
3466           vlist)
3467       (let ((locals gnus-newsgroup-variables))
3468         (while locals
3469           (if (consp (car locals))
3470               (push (eval (caar locals)) vlist)
3471             (push (eval (car locals)) vlist))
3472           (setq locals (cdr locals)))
3473         (setq vlist (nreverse vlist)))
3474       (with-current-buffer gnus-group-buffer
3475         (setq gnus-newsgroup-name name
3476               gnus-newsgroup-marked marked
3477               gnus-newsgroup-spam-marked spam
3478               gnus-newsgroup-unreads unread
3479               gnus-current-headers headers
3480               gnus-newsgroup-data data
3481               gnus-article-current gac
3482               gnus-summary-buffer summary
3483               gnus-article-buffer article-buffer
3484               gnus-original-article-buffer original
3485               gnus-reffed-article-number reffed
3486               gnus-current-score-file score-file
3487               gnus-newsgroup-charset default-charset)
3488         (let ((locals gnus-newsgroup-variables))
3489           (while locals
3490             (if (consp (car locals))
3491                 (set (caar locals) (pop vlist))
3492               (set (car locals) (pop vlist)))
3493             (setq locals (cdr locals))))
3494         ;; The article buffer also has local variables.
3495         (when (gnus-buffer-live-p gnus-article-buffer)
3496           (set-buffer gnus-article-buffer)
3497           (setq gnus-summary-buffer summary))))))
3498
3499 (defun gnus-summary-article-unread-p (article)
3500   "Say whether ARTICLE is unread or not."
3501   (memq article gnus-newsgroup-unreads))
3502
3503 (defun gnus-summary-first-article-p (&optional article)
3504   "Return whether ARTICLE is the first article in the buffer."
3505   (if (not (setq article (or article (gnus-summary-article-number))))
3506       nil
3507     (eq article (caar gnus-newsgroup-data))))
3508
3509 (defun gnus-summary-last-article-p (&optional article)
3510   "Return whether ARTICLE is the last article in the buffer."
3511   (if (not (setq article (or article (gnus-summary-article-number))))
3512       ;; All non-existent numbers are the last article.  :-)
3513       t
3514     (not (cdr (gnus-data-find-list article)))))
3515
3516 (defun gnus-make-thread-indent-array (&optional n)
3517   (when (or n
3518             (progn (setq n 200) nil)
3519             (null gnus-thread-indent-array)
3520             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3521     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3522           gnus-thread-indent-array-level gnus-thread-indent-level)
3523     (while (>= n 0)
3524       (aset gnus-thread-indent-array n
3525             (make-string (* n gnus-thread-indent-level) ? ))
3526       (setq n (1- n)))))
3527
3528 (defun gnus-update-summary-mark-positions ()
3529   "Compute where the summary marks are to go."
3530   (save-excursion
3531     (when (gnus-buffer-exists-p gnus-summary-buffer)
3532       (set-buffer gnus-summary-buffer))
3533     (let ((spec gnus-summary-line-format-spec)
3534           pos)
3535       (save-excursion
3536         (gnus-set-work-buffer)
3537         (let ((gnus-tmp-unread ?Z)
3538               (gnus-replied-mark ?Z)
3539               (gnus-score-below-mark ?Z)
3540               (gnus-score-over-mark ?Z)
3541               (gnus-undownloaded-mark ?Z)
3542               (gnus-summary-line-format-spec spec)
3543               (gnus-newsgroup-downloadable '(0))
3544               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3545               case-fold-search ignores)
3546           ;; Here, all marks are bound to Z.
3547           (gnus-summary-insert-line header
3548                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3549           (goto-char (point-min))
3550           ;; Memorize the positions of the same characters as dummy marks.
3551           (while (re-search-forward "[A-D]" nil t)
3552             (push (point) ignores))
3553           (erase-buffer)
3554           ;; We use A-D as dummy marks in order to know column positions
3555           ;; where marks should be inserted.
3556           (setq gnus-tmp-unread ?A
3557                 gnus-replied-mark ?B
3558                 gnus-score-below-mark ?C
3559                 gnus-score-over-mark ?C
3560                 gnus-undownloaded-mark ?D)
3561           (gnus-summary-insert-line header
3562                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3563           ;; Ignore characters which aren't dummy marks.
3564           (dolist (p ignores)
3565             (delete-region (goto-char (1- p)) p)
3566             (insert ?Z))
3567           (goto-char (point-min))
3568           (setq pos (list (cons 'unread
3569                                 (and (search-forward "A" nil t)
3570                                      (- (point) (point-min) 1)))))
3571           (goto-char (point-min))
3572           (push (cons 'replied (and (search-forward "B" nil t)
3573                                     (- (point) (point-min) 1)))
3574                 pos)
3575           (goto-char (point-min))
3576           (push (cons 'score (and (search-forward "C" nil t)
3577                                   (- (point) (point-min) 1)))
3578                 pos)
3579           (goto-char (point-min))
3580           (push (cons 'download (and (search-forward "D" nil t)
3581                                      (- (point) (point-min) 1)))
3582                 pos)))
3583       (setq gnus-summary-mark-positions pos))))
3584
3585 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3586   "Insert a dummy root in the summary buffer."
3587   (beginning-of-line)
3588   (gnus-add-text-properties
3589    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3590    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3591
3592 (defun gnus-summary-extract-address-component (from)
3593   (or (car (funcall gnus-extract-address-components from))
3594       from))
3595
3596 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3597   (let ((mail-parse-charset gnus-newsgroup-charset)
3598         (ignored-from-addresses (gnus-ignored-from-addresses))
3599         ; Is it really necessary to do this next part for each summary line?
3600         ; Luckily, doesn't seem to slow things down much.
3601         (mail-parse-ignored-charsets
3602          (with-current-buffer gnus-summary-buffer
3603            gnus-newsgroup-ignored-charsets)))
3604     (or
3605      (and ignored-from-addresses
3606           (string-match ignored-from-addresses gnus-tmp-from)
3607           (let ((extra-headers (mail-header-extra header))
3608                 to
3609                 newsgroups)
3610             (cond
3611              ((setq to (cdr (assq 'To extra-headers)))
3612               (concat gnus-summary-to-prefix
3613                       (inline
3614                         (gnus-summary-extract-address-component
3615                          (funcall gnus-decode-encoded-address-function to)))))
3616              ((setq newsgroups
3617                     (or
3618                      (cdr (assq 'Newsgroups extra-headers))
3619                      (and
3620                       (memq 'Newsgroups gnus-extra-headers)
3621                       (eq (car (gnus-find-method-for-group
3622                                 gnus-newsgroup-name)) 'nntp)
3623                       (gnus-group-real-name gnus-newsgroup-name))))
3624               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3625      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3626
3627 (defun gnus-summary-insert-line (gnus-tmp-header
3628                                  gnus-tmp-level gnus-tmp-current
3629                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3630                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3631                                  &optional gnus-tmp-dummy gnus-tmp-score
3632                                  gnus-tmp-process)
3633   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3634       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3635                                           gnus-tmp-level)))
3636   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3637          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3638          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3639          (gnus-tmp-score-char
3640           (if (or (null gnus-summary-default-score)
3641                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3642                       gnus-summary-zcore-fuzz))
3643               ?                         ;Whitespace
3644             (if (< gnus-tmp-score gnus-summary-default-score)
3645                 gnus-score-below-mark gnus-score-over-mark)))
3646          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3647          (gnus-tmp-replied
3648           (cond (gnus-tmp-process gnus-process-mark)
3649                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3650                  gnus-cached-mark)
3651                 (gnus-tmp-replied gnus-replied-mark)
3652                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3653                  gnus-forwarded-mark)
3654                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3655                  gnus-saved-mark)
3656                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3657                  gnus-recent-mark)
3658                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3659                  gnus-unseen-mark)
3660                 (t gnus-no-mark)))
3661          (gnus-tmp-downloaded
3662           (cond (undownloaded
3663                  gnus-undownloaded-mark)
3664                 (gnus-newsgroup-agentized
3665                  gnus-downloaded-mark)
3666                 (t
3667                  gnus-no-mark)))
3668          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3669          (gnus-tmp-name
3670           (cond
3671            ((string-match "<[^>]+> *$" gnus-tmp-from)
3672             (let ((beg (match-beginning 0)))
3673               (or (and (string-match "^\".+\"" gnus-tmp-from)
3674                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3675                   (substring gnus-tmp-from 0 beg))))
3676            ((string-match "(.+)" gnus-tmp-from)
3677             (substring gnus-tmp-from
3678                        (1+ (match-beginning 0)) (1- (match-end 0))))
3679            (t gnus-tmp-from)))
3680          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3681          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3682          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3683          (buffer-read-only nil))
3684     (when (string= gnus-tmp-name "")
3685       (setq gnus-tmp-name gnus-tmp-from))
3686     (unless (numberp gnus-tmp-lines)
3687       (setq gnus-tmp-lines -1))
3688     (if (= gnus-tmp-lines -1)
3689         (setq gnus-tmp-lines "?")
3690       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3691       (gnus-put-text-property
3692      (point)
3693      (progn (eval gnus-summary-line-format-spec) (point))
3694        'gnus-number gnus-tmp-number)
3695     (when (gnus-visual-p 'summary-highlight 'highlight)
3696       (forward-line -1)
3697       (gnus-run-hooks 'gnus-summary-update-hook)
3698       (forward-line 1))))
3699
3700 (defun gnus-summary-update-line (&optional dont-update)
3701   "Update summary line after change."
3702   (when (and gnus-summary-default-score
3703              (not gnus-summary-inhibit-highlight))
3704     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3705            (article (gnus-summary-article-number))
3706            (score (gnus-summary-article-score article)))
3707       (unless dont-update
3708         (if (and gnus-summary-mark-below
3709                  (< (gnus-summary-article-score)
3710                     gnus-summary-mark-below))
3711             ;; This article has a low score, so we mark it as read.
3712             (when (memq article gnus-newsgroup-unreads)
3713               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3714           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3715             ;; This article was previously marked as read on account
3716             ;; of a low score, but now it has risen, so we mark it as
3717             ;; unread.
3718             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3719         (gnus-summary-update-mark
3720          (if (or (null gnus-summary-default-score)
3721                  (<= (abs (- score gnus-summary-default-score))
3722                      gnus-summary-zcore-fuzz))
3723              ?                          ;Whitespace
3724            (if (< score gnus-summary-default-score)
3725                gnus-score-below-mark gnus-score-over-mark))
3726          'score))
3727       ;; Do visual highlighting.
3728       (when (gnus-visual-p 'summary-highlight 'highlight)
3729         (gnus-run-hooks 'gnus-summary-update-hook)))))
3730
3731 (defvar gnus-tmp-new-adopts nil)
3732
3733 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3734   "Return the number of articles in THREAD.
3735 This may be 0 in some cases -- if none of the articles in
3736 the thread are to be displayed."
3737   (let* ((number
3738          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3739           (cond
3740            ((not (listp thread))
3741             1)
3742            ((and (consp thread) (cdr thread))
3743             (apply
3744              '+ 1 (mapcar
3745                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3746            ((null thread)
3747             1)
3748            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3749             1)
3750            (t 0))))
3751     (when (and level (zerop level) gnus-tmp-new-adopts)
3752       (incf number
3753             (apply '+ (mapcar
3754                        'gnus-summary-number-of-articles-in-thread
3755                        gnus-tmp-new-adopts))))
3756     (if char
3757         (if (> number 1) gnus-not-empty-thread-mark
3758           gnus-empty-thread-mark)
3759       number)))
3760
3761 (defsubst gnus-summary-line-message-size (head)
3762   "Return pretty-printed version of message size.
3763 This function is intended to be used in
3764 `gnus-summary-line-format-alist'."
3765   (let ((c (or (mail-header-chars head) -1)))
3766     (cond ((< c 0) "n/a")               ; chars not available
3767           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3768           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3769           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3770           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3771
3772
3773 (defun gnus-summary-set-local-parameters (group)
3774   "Go through the local params of GROUP and set all variable specs in that list."
3775   (let ((vars '(quit-config)))          ; Ignore quit-config.
3776     (dolist (elem (gnus-group-find-parameter group))
3777       (and (consp elem)                 ; Has to be a cons.
3778            (consp (cdr elem))           ; The cdr has to be a list.
3779            (symbolp (car elem))         ; Has to be a symbol in there.
3780            (not (memq (car elem) vars))
3781            (ignore-errors               ; So we set it.
3782              (push (car elem) vars)
3783              (make-local-variable (car elem))
3784              (set (car elem) (eval (nth 1 elem))))))))
3785
3786 (defun gnus-summary-read-group (group &optional show-all no-article
3787                                       kill-buffer no-display backward
3788                                       select-articles)
3789   "Start reading news in newsgroup GROUP.
3790 If SHOW-ALL is non-nil, already read articles are also listed.
3791 If NO-ARTICLE is non-nil, no article is selected initially.
3792 If NO-DISPLAY, don't generate a summary buffer."
3793   (let (result)
3794     (while (and group
3795                 (null (setq result
3796                             (let ((gnus-auto-select-next nil))
3797                               (or (gnus-summary-read-group-1
3798                                    group show-all no-article
3799                                    kill-buffer no-display
3800                                    select-articles)
3801                                   (setq show-all nil
3802                                         select-articles nil)))))
3803                 (eq gnus-auto-select-next 'quietly))
3804       (set-buffer gnus-group-buffer)
3805       ;; The entry function called above goes to the next
3806       ;; group automatically, so we go two groups back
3807       ;; if we are searching for the previous group.
3808       (when backward
3809         (gnus-group-prev-unread-group 2))
3810       (if (not (equal group (gnus-group-group-name)))
3811           (setq group (gnus-group-group-name))
3812         (setq group nil)))
3813     result))
3814
3815 (defun gnus-summary-read-group-1 (group show-all no-article
3816                                         kill-buffer no-display
3817                                         &optional select-articles)
3818   ;; Killed foreign groups can't be entered.
3819   ;;  (when (and (not (gnus-group-native-p group))
3820   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3821   ;;    (error "Dead non-native groups can't be entered"))
3822   (gnus-message 5 "Retrieving newsgroup: %s..."
3823                 (gnus-group-decoded-name group))
3824   (let* ((new-group (gnus-summary-setup-buffer group))
3825          (quit-config (gnus-group-quit-config group))
3826          (did-select (and new-group (gnus-select-newsgroup
3827                                      group show-all select-articles))))
3828     (cond
3829      ;; This summary buffer exists already, so we just select it.
3830      ((not new-group)
3831       (gnus-set-global-variables)
3832       (when kill-buffer
3833         (gnus-kill-or-deaden-summary kill-buffer))
3834       (gnus-configure-windows 'summary 'force)
3835       (gnus-set-mode-line 'summary)
3836       (gnus-summary-position-point)
3837       (message "")
3838       t)
3839      ;; We couldn't select this group.
3840      ((null did-select)
3841       (when (and (eq major-mode 'gnus-summary-mode)
3842                  (not (equal (current-buffer) kill-buffer)))
3843         (kill-buffer (current-buffer))
3844         (if (not quit-config)
3845             (progn
3846               ;; Update the info -- marks might need to be removed,
3847               ;; for instance.
3848               (gnus-summary-update-info)
3849               (set-buffer gnus-group-buffer)
3850               (gnus-group-jump-to-group group)
3851               (gnus-group-next-unread-group 1))
3852           (gnus-handle-ephemeral-exit quit-config)))
3853       (let ((grpinfo (gnus-get-info group)))
3854         (if (null (gnus-info-read grpinfo))
3855             (gnus-message 3 "Group %s contains no messages"
3856                           (gnus-group-decoded-name group))
3857           (gnus-message 3 "Can't select group")))
3858       nil)
3859      ;; The user did a `C-g' while prompting for number of articles,
3860      ;; so we exit this group.
3861      ((eq did-select 'quit)
3862       (and (eq major-mode 'gnus-summary-mode)
3863            (not (equal (current-buffer) kill-buffer))
3864            (kill-buffer (current-buffer)))
3865       (when kill-buffer
3866         (gnus-kill-or-deaden-summary kill-buffer))
3867       (if (not quit-config)
3868           (progn
3869             (set-buffer gnus-group-buffer)
3870             (gnus-group-jump-to-group group)
3871             (gnus-group-next-unread-group 1)
3872             (gnus-configure-windows 'group 'force))
3873         (gnus-handle-ephemeral-exit quit-config))
3874       ;; Finally signal the quit.
3875       (signal 'quit nil))
3876      ;; The group was successfully selected.
3877      (t
3878       (gnus-set-global-variables)
3879       ;; Save the active value in effect when the group was entered.
3880       (setq gnus-newsgroup-active
3881             (gnus-copy-sequence
3882              (gnus-active gnus-newsgroup-name)))
3883       ;; You can change the summary buffer in some way with this hook.
3884       (gnus-run-hooks 'gnus-select-group-hook)
3885       (when (memq 'summary (gnus-update-format-specifications
3886                             nil 'summary 'summary-mode 'summary-dummy))
3887         ;; The format specification for the summary line was updated,
3888         ;; so we need to update the mark positions as well.
3889         (gnus-update-summary-mark-positions))
3890       ;; Do score processing.
3891       (when gnus-use-scoring
3892         (gnus-possibly-score-headers))
3893       ;; Check whether to fill in the gaps in the threads.
3894       (when gnus-build-sparse-threads
3895         (gnus-build-sparse-threads))
3896       ;; Find the initial limit.
3897       (if show-all
3898           (let ((gnus-newsgroup-dormant nil))
3899             (gnus-summary-initial-limit show-all))
3900         (gnus-summary-initial-limit show-all))
3901       ;; Generate the summary buffer.
3902       (unless no-display
3903         (gnus-summary-prepare))
3904       (when gnus-use-trees
3905         (gnus-tree-open group)
3906         (setq gnus-summary-highlight-line-function
3907               'gnus-tree-highlight-article))
3908       ;; If the summary buffer is empty, but there are some low-scored
3909       ;; articles or some excluded dormants, we include these in the
3910       ;; buffer.
3911       (when (and (zerop (buffer-size))
3912                  (not no-display))
3913         (cond (gnus-newsgroup-dormant
3914                (gnus-summary-limit-include-dormant))
3915               ((and gnus-newsgroup-scored show-all)
3916                (gnus-summary-limit-include-expunged t))))
3917       ;; Function `gnus-apply-kill-file' must be called in this hook.
3918       (gnus-run-hooks 'gnus-apply-kill-hook)
3919       (if (and (zerop (buffer-size))
3920                (not no-display))
3921           (progn
3922             ;; This newsgroup is empty.
3923             (gnus-summary-catchup-and-exit nil t)
3924             (gnus-message 6 "No unread news")
3925             (when kill-buffer
3926               (gnus-kill-or-deaden-summary kill-buffer))
3927             ;; Return nil from this function.
3928             nil)
3929         ;; Hide conversation thread subtrees.  We cannot do this in
3930         ;; gnus-summary-prepare-hook since kill processing may not
3931         ;; work with hidden articles.
3932         (gnus-summary-maybe-hide-threads)
3933         (when kill-buffer
3934           (gnus-kill-or-deaden-summary kill-buffer))
3935         (gnus-summary-auto-select-subject)
3936         ;; Show first unread article if requested.
3937         (if (and (not no-article)
3938                  (not no-display)
3939                  gnus-newsgroup-unreads
3940                  gnus-auto-select-first)
3941             (progn
3942               (gnus-configure-windows 'summary)
3943               (let ((art (gnus-summary-article-number)))
3944                 (unless (and (not gnus-plugged)
3945                              (or (memq art gnus-newsgroup-undownloaded)
3946                                  (memq art gnus-newsgroup-downloadable)))
3947                   (gnus-summary-goto-article art))))
3948           ;; Don't select any articles.
3949           (gnus-summary-position-point)
3950           (gnus-configure-windows 'summary 'force)
3951           (gnus-set-mode-line 'summary))
3952         (when (and gnus-auto-center-group
3953                    (get-buffer-window gnus-group-buffer t))
3954           ;; Gotta use windows, because recenter does weird stuff if
3955           ;; the current buffer ain't the displayed window.
3956           (let ((owin (selected-window)))
3957             (select-window (get-buffer-window gnus-group-buffer t))
3958             (when (gnus-group-goto-group group)
3959               (recenter))
3960             (select-window owin)))
3961         ;; Mark this buffer as "prepared".
3962         (setq gnus-newsgroup-prepared t)
3963         (gnus-run-hooks 'gnus-summary-prepared-hook)
3964         (unless (gnus-ephemeral-group-p group)
3965           (gnus-group-update-group group))
3966         t)))))
3967
3968 (defun gnus-summary-auto-select-subject ()
3969   "Select the subject line on initial group entry."
3970   (goto-char (point-min))
3971   (cond
3972    ((eq gnus-auto-select-subject 'best)
3973     (gnus-summary-best-unread-subject))
3974    ((eq gnus-auto-select-subject 'unread)
3975     (gnus-summary-first-unread-subject))
3976    ((eq gnus-auto-select-subject 'unseen)
3977     (gnus-summary-first-unseen-subject))
3978    ((eq gnus-auto-select-subject 'unseen-or-unread)
3979     (gnus-summary-first-unseen-or-unread-subject))
3980    ((eq gnus-auto-select-subject 'first)
3981     ;; Do nothing.
3982     )
3983    ((functionp gnus-auto-select-subject)
3984     (funcall gnus-auto-select-subject))))
3985
3986 (defun gnus-summary-prepare ()
3987   "Generate the summary buffer."
3988   (interactive)
3989   (let ((buffer-read-only nil))
3990     (erase-buffer)
3991     (setq gnus-newsgroup-data nil
3992           gnus-newsgroup-data-reverse nil)
3993     (gnus-run-hooks 'gnus-summary-generate-hook)
3994     ;; Generate the buffer, either with threads or without.
3995     (when gnus-newsgroup-headers
3996       (gnus-summary-prepare-threads
3997        (if gnus-show-threads
3998            (gnus-sort-gathered-threads
3999             (funcall gnus-summary-thread-gathering-function
4000                      (gnus-sort-threads
4001                       (gnus-cut-threads (gnus-make-threads)))))
4002          ;; Unthreaded display.
4003          (gnus-sort-articles gnus-newsgroup-headers))))
4004     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4005     ;; Call hooks for modifying summary buffer.
4006     (goto-char (point-min))
4007     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4008
4009 (defsubst gnus-general-simplify-subject (subject)
4010   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4011   (setq subject
4012         (cond
4013          ;; Truncate the subject.
4014          (gnus-simplify-subject-functions
4015           (gnus-map-function gnus-simplify-subject-functions subject))
4016          ((numberp gnus-summary-gather-subject-limit)
4017           (setq subject (gnus-simplify-subject-re subject))
4018           (if (> (length subject) gnus-summary-gather-subject-limit)
4019               (substring subject 0 gnus-summary-gather-subject-limit)
4020             subject))
4021          ;; Fuzzily simplify it.
4022          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4023           (gnus-simplify-subject-fuzzy subject))
4024          ;; Just remove the leading "Re:".
4025          (t
4026           (gnus-simplify-subject-re subject))))
4027
4028   (if (and gnus-summary-gather-exclude-subject
4029            (string-match gnus-summary-gather-exclude-subject subject))
4030       nil                         ; This article shouldn't be gathered
4031     subject))
4032
4033 (defun gnus-summary-simplify-subject-query ()
4034   "Query where the respool algorithm would put this article."
4035   (interactive)
4036   (gnus-summary-select-article)
4037   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4038
4039 (defun gnus-gather-threads-by-subject (threads)
4040   "Gather threads by looking at Subject headers."
4041   (if (not gnus-summary-make-false-root)
4042       threads
4043     (let ((hashtb (gnus-make-hashtable 1024))
4044           (prev threads)
4045           (result threads)
4046           subject hthread whole-subject)
4047       (while threads
4048         (setq subject (gnus-general-simplify-subject
4049                        (setq whole-subject (mail-header-subject
4050                                             (caar threads)))))
4051         (when subject
4052           (if (setq hthread (gnus-gethash subject hashtb))
4053               (progn
4054                 ;; We enter a dummy root into the thread, if we
4055                 ;; haven't done that already.
4056                 (unless (stringp (caar hthread))
4057                   (setcar hthread (list whole-subject (car hthread))))
4058                 ;; We add this new gathered thread to this gathered
4059                 ;; thread.
4060                 (setcdr (car hthread)
4061                         (nconc (cdar hthread) (list (car threads))))
4062                 ;; Remove it from the list of threads.
4063                 (setcdr prev (cdr threads))
4064                 (setq threads prev))
4065             ;; Enter this thread into the hash table.
4066             (gnus-sethash subject
4067                           (if gnus-summary-make-false-root-always
4068                               (progn
4069                                 ;; If you want a dummy root above all
4070                                 ;; threads...
4071                                 (setcar threads (list whole-subject
4072                                                       (car threads)))
4073                                 threads)
4074                             threads)
4075                           hashtb)))
4076         (setq prev threads)
4077         (setq threads (cdr threads)))
4078       result)))
4079
4080 (defun gnus-gather-threads-by-references (threads)
4081   "Gather threads by looking at References headers."
4082   (let ((idhashtb (gnus-make-hashtable 1024))
4083         (thhashtb (gnus-make-hashtable 1024))
4084         (prev threads)
4085         (result threads)
4086         ids references id gthread gid entered ref)
4087     (while threads
4088       (when (setq references (mail-header-references (caar threads)))
4089         (setq id (mail-header-id (caar threads))
4090               ids (inline (gnus-split-references references))
4091               entered nil)
4092         (while (setq ref (pop ids))
4093           (setq ids (delete ref ids))
4094           (if (not (setq gid (gnus-gethash ref idhashtb)))
4095               (progn
4096                 (gnus-sethash ref id idhashtb)
4097                 (gnus-sethash id threads thhashtb))
4098             (setq gthread (gnus-gethash gid thhashtb))
4099             (unless entered
4100               ;; We enter a dummy root into the thread, if we
4101               ;; haven't done that already.
4102               (unless (stringp (caar gthread))
4103                 (setcar gthread (list (mail-header-subject (caar gthread))
4104                                       (car gthread))))
4105               ;; We add this new gathered thread to this gathered
4106               ;; thread.
4107               (setcdr (car gthread)
4108                       (nconc (cdar gthread) (list (car threads)))))
4109             ;; Add it into the thread hash table.
4110             (gnus-sethash id gthread thhashtb)
4111             (setq entered t)
4112             ;; Remove it from the list of threads.
4113             (setcdr prev (cdr threads))
4114             (setq threads prev))))
4115       (setq prev threads)
4116       (setq threads (cdr threads)))
4117     result))
4118
4119 (defun gnus-sort-gathered-threads (threads)
4120   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4121   (let ((result threads))
4122     (while threads
4123       (when (stringp (caar threads))
4124         (setcdr (car threads)
4125                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4126       (setq threads (cdr threads)))
4127     result))
4128
4129 (defun gnus-thread-loop-p (root thread)
4130   "Say whether ROOT is in THREAD."
4131   (let ((stack (list thread))
4132         (infloop 0)
4133         th)
4134     (while (setq thread (pop stack))
4135       (setq th (cdr thread))
4136       (while (and th
4137                   (not (eq (caar th) root)))
4138         (pop th))
4139       (if th
4140           ;; We have found a loop.
4141           (let (ref-dep)
4142             (setcdr thread (delq (car th) (cdr thread)))
4143             (if (boundp (setq ref-dep (intern "none"
4144                                               gnus-newsgroup-dependencies)))
4145                 (setcdr (symbol-value ref-dep)
4146                         (nconc (cdr (symbol-value ref-dep))
4147                                (list (car th))))
4148               (set ref-dep (list nil (car th))))
4149             (setq infloop 1
4150                   stack nil))
4151         ;; Push all the subthreads onto the stack.
4152         (push (cdr thread) stack)))
4153     infloop))
4154
4155 (defun gnus-make-threads ()
4156   "Go through the dependency hashtb and find the roots.  Return all threads."
4157   (let (threads)
4158     (while (catch 'infloop
4159              (mapatoms
4160               (lambda (refs)
4161                 ;; Deal with self-referencing References loops.
4162                 (when (and (car (symbol-value refs))
4163                            (not (zerop
4164                                  (apply
4165                                   '+
4166                                   (mapcar
4167                                    (lambda (thread)
4168                                      (gnus-thread-loop-p
4169                                       (car (symbol-value refs)) thread))
4170                                    (cdr (symbol-value refs)))))))
4171                   (setq threads nil)
4172                   (throw 'infloop t))
4173                 (unless (car (symbol-value refs))
4174                   ;; These threads do not refer back to any other
4175                   ;; articles, so they're roots.
4176                   (setq threads (append (cdr (symbol-value refs)) threads))))
4177               gnus-newsgroup-dependencies)))
4178     threads))
4179
4180 ;; Build the thread tree.
4181 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4182   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4183
4184 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4185 if it was already present.
4186
4187 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4188 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4189 Message-IDs will be renamed to a unique Message-ID before being
4190 entered.
4191
4192 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4193   (let* ((id (mail-header-id header))
4194          (id-dep (and id (intern id dependencies)))
4195          parent-id ref ref-dep ref-header replaced)
4196     ;; Enter this `header' in the `dependencies' table.
4197     (cond
4198      ((not id-dep)
4199       (setq header nil))
4200      ;; The first two cases do the normal part: enter a new `header'
4201      ;; in the `dependencies' table.
4202      ((not (boundp id-dep))
4203       (set id-dep (list header)))
4204      ((null (car (symbol-value id-dep)))
4205       (setcar (symbol-value id-dep) header))
4206
4207      ;; From here the `header' was already present in the
4208      ;; `dependencies' table.
4209      (force-new
4210       ;; Overrides an existing entry;
4211       ;; just set the header part of the entry.
4212       (setcar (symbol-value id-dep) header)
4213       (setq replaced t))
4214
4215      ;; Renames the existing `header' to a unique Message-ID.
4216      ((not gnus-summary-ignore-duplicates)
4217       ;; An article with this Message-ID has already been seen.
4218       ;; We rename the Message-ID.
4219       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4220            (list header))
4221       (mail-header-set-id header id))
4222
4223      ;; The last case ignores an existing entry, except it adds any
4224      ;; additional Xrefs (in case the two articles came from different
4225      ;; servers.
4226      ;; Also sets `header' to `nil' meaning that the `dependencies'
4227      ;; table was *not* modified.
4228      (t
4229       (mail-header-set-xref
4230        (car (symbol-value id-dep))
4231        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4232                    "")
4233                (or (mail-header-xref header) "")))
4234       (setq header nil)))
4235
4236     (when (and header (not replaced))
4237       ;; First check that we are not creating a References loop.
4238       (setq parent-id (gnus-parent-id (mail-header-references header)))
4239       (setq ref parent-id)
4240       (while (and ref
4241                   (setq ref-dep (intern-soft ref dependencies))
4242                   (boundp ref-dep)
4243                   (setq ref-header (car (symbol-value ref-dep))))
4244         (if (string= id ref)
4245             ;; Yuk!  This is a reference loop.  Make the article be a
4246             ;; root article.
4247             (progn
4248               (mail-header-set-references (car (symbol-value id-dep)) "none")
4249               (setq ref nil)
4250               (setq parent-id nil))
4251           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4252       (setq ref-dep (intern (or parent-id "none") dependencies))
4253       (if (boundp ref-dep)
4254           (setcdr (symbol-value ref-dep)
4255                   (nconc (cdr (symbol-value ref-dep))
4256                          (list (symbol-value id-dep))))
4257         (set ref-dep (list nil (symbol-value id-dep)))))
4258     header))
4259
4260 (defun gnus-extract-message-id-from-in-reply-to (string)
4261   (if (string-match "<[^>]+>" string)
4262       (substring string (match-beginning 0) (match-end 0))
4263     nil))
4264
4265 (defun gnus-build-sparse-threads ()
4266   (let ((headers gnus-newsgroup-headers)
4267         (mail-parse-charset gnus-newsgroup-charset)
4268         (gnus-summary-ignore-duplicates t)
4269         header references generation relations
4270         subject child end new-child date)
4271     ;; First we create an alist of generations/relations, where
4272     ;; generations is how much we trust the relation, and the relation
4273     ;; is parent/child.
4274     (gnus-message 7 "Making sparse threads...")
4275     (save-excursion
4276       (nnheader-set-temp-buffer " *gnus sparse threads*")
4277       (while (setq header (pop headers))
4278         (when (and (setq references (mail-header-references header))
4279                    (not (string= references "")))
4280           (insert references)
4281           (setq child (mail-header-id header)
4282                 subject (mail-header-subject header)
4283                 date (mail-header-date header)
4284                 generation 0)
4285           (while (search-backward ">" nil t)
4286             (setq end (1+ (point)))
4287             (when (search-backward "<" nil t)
4288               (setq new-child (buffer-substring (point) end))
4289               (push (list (incf generation)
4290                           child (setq child new-child)
4291                           subject date)
4292                     relations)))
4293           (when child
4294             (push (list (1+ generation) child nil subject) relations))
4295           (erase-buffer)))
4296       (kill-buffer (current-buffer)))
4297     ;; Sort over trustworthiness.
4298     (dolist (relation (sort relations 'car-less-than-car))
4299       (when (gnus-dependencies-add-header
4300              (make-full-mail-header
4301               gnus-reffed-article-number
4302               (nth 3 relation) "" (or (nth 4 relation) "")
4303               (nth 1 relation)
4304               (or (nth 2 relation) "") 0 0 "")
4305              gnus-newsgroup-dependencies nil)
4306         (push gnus-reffed-article-number gnus-newsgroup-limit)
4307         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4308         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4309               gnus-newsgroup-reads)
4310         (decf gnus-reffed-article-number)))
4311     (gnus-message 7 "Making sparse threads...done")))
4312
4313 (defun gnus-build-old-threads ()
4314   ;; Look at all the articles that refer back to old articles, and
4315   ;; fetch the headers for the articles that aren't there.  This will
4316   ;; build complete threads - if the roots haven't been expired by the
4317   ;; server, that is.
4318   (let ((mail-parse-charset gnus-newsgroup-charset)
4319         id heads)
4320     (mapatoms
4321      (lambda (refs)
4322        (when (not (car (symbol-value refs)))
4323          (setq heads (cdr (symbol-value refs)))
4324          (while heads
4325            (if (memq (mail-header-number (caar heads))
4326                      gnus-newsgroup-dormant)
4327                (setq heads (cdr heads))
4328              (setq id (symbol-name refs))
4329              (while (and (setq id (gnus-build-get-header id))
4330                          (not (car (gnus-id-to-thread id)))))
4331              (setq heads nil)))))
4332      gnus-newsgroup-dependencies)))
4333
4334 (defsubst gnus-remove-odd-characters (string)
4335   "Translate STRING into something that doesn't contain weird characters."
4336   (mm-subst-char-in-string
4337    ?\r ?\-
4338    (mm-subst-char-in-string ?\n ?\- string t) t))
4339
4340 ;; This function has to be called with point after the article number
4341 ;; on the beginning of the line.
4342 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4343   (let ((eol (point-at-eol))
4344         (buffer (current-buffer))
4345         header references in-reply-to)
4346
4347     ;; overview: [num subject from date id refs chars lines misc]
4348     (unwind-protect
4349         (let (x)
4350           (narrow-to-region (point) eol)
4351           (unless (eobp)
4352             (forward-char))
4353
4354           (setq header
4355                 (make-full-mail-header
4356                  number                 ; number
4357                  (condition-case ()     ; subject
4358                      (gnus-remove-odd-characters
4359                       (funcall gnus-decode-encoded-word-function
4360                                (setq x (nnheader-nov-field))))
4361                    (error x))
4362                  (condition-case ()     ; from
4363                      (gnus-remove-odd-characters
4364                       (funcall gnus-decode-encoded-address-function
4365                                (setq x (nnheader-nov-field))))
4366                    (error x))
4367                  (nnheader-nov-field)   ; date
4368                  (nnheader-nov-read-message-id number)  ; id
4369                  (setq references (nnheader-nov-field)) ; refs
4370                  (nnheader-nov-read-integer) ; chars
4371                  (nnheader-nov-read-integer) ; lines
4372                  (unless (eobp)
4373                    (if (looking-at "Xref: ")
4374                        (goto-char (match-end 0)))
4375                    (nnheader-nov-field)) ; Xref
4376                  (nnheader-nov-parse-extra)))) ; extra
4377
4378       (widen))
4379
4380     (when (and (string= references "")
4381                (setq in-reply-to (mail-header-extra header))
4382                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4383       (mail-header-set-references
4384        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4385
4386     (when gnus-alter-header-function
4387       (funcall gnus-alter-header-function header))
4388     (gnus-dependencies-add-header header dependencies force-new)))
4389
4390 (defun gnus-build-get-header (id)
4391   "Look through the buffer of NOV lines and find the header to ID.
4392 Enter this line into the dependencies hash table, and return
4393 the id of the parent article (if any)."
4394   (let ((deps gnus-newsgroup-dependencies)
4395         found header)
4396     (prog1
4397         (save-excursion
4398           (set-buffer nntp-server-buffer)
4399           (let ((case-fold-search nil))
4400             (goto-char (point-min))
4401             (while (and (not found)
4402                         (search-forward id nil t))
4403               (beginning-of-line)
4404               (setq found (looking-at
4405                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4406                                    (regexp-quote id))))
4407               (or found (beginning-of-line 2)))
4408             (when found
4409               (beginning-of-line)
4410               (and
4411                (setq header (gnus-nov-parse-line
4412                              (read (current-buffer)) deps))
4413                (gnus-parent-id (mail-header-references header))))))
4414       (when header
4415         (let ((number (mail-header-number header)))
4416           (push number gnus-newsgroup-limit)
4417           (push header gnus-newsgroup-headers)
4418           (if (memq number gnus-newsgroup-unselected)
4419               (progn
4420                 (setq gnus-newsgroup-unreads
4421                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4422                                                number))
4423                 (setq gnus-newsgroup-unselected
4424                       (delq number gnus-newsgroup-unselected)))
4425             (push number gnus-newsgroup-ancient)))))))
4426
4427 (defun gnus-build-all-threads ()
4428   "Read all the headers."
4429   (let ((gnus-summary-ignore-duplicates t)
4430         (mail-parse-charset gnus-newsgroup-charset)
4431         (dependencies gnus-newsgroup-dependencies)
4432         header article)
4433     (save-excursion
4434       (set-buffer nntp-server-buffer)
4435       (let ((case-fold-search nil))
4436         (goto-char (point-min))
4437         (while (not (eobp))
4438           (ignore-errors
4439             (setq article (read (current-buffer))
4440                   header (gnus-nov-parse-line article dependencies)))
4441           (when header
4442             (with-current-buffer gnus-summary-buffer
4443               (push header gnus-newsgroup-headers)
4444               (if (memq (setq article (mail-header-number header))
4445                         gnus-newsgroup-unselected)
4446                   (progn
4447                     (setq gnus-newsgroup-unreads
4448                           (gnus-add-to-sorted-list
4449                            gnus-newsgroup-unreads article))
4450                     (setq gnus-newsgroup-unselected
4451                           (delq article gnus-newsgroup-unselected)))
4452                 (push article gnus-newsgroup-ancient)))
4453             (forward-line 1)))))))
4454
4455 (defun gnus-summary-update-article-line (article header)
4456   "Update the line for ARTICLE using HEADER."
4457   (let* ((id (mail-header-id header))
4458          (thread (gnus-id-to-thread id)))
4459     (unless thread
4460       (error "Article in no thread"))
4461     ;; Update the thread.
4462     (setcar thread header)
4463     (gnus-summary-goto-subject article)
4464     (let* ((datal (gnus-data-find-list article))
4465            (data (car datal))
4466            (buffer-read-only nil)
4467            (level (gnus-summary-thread-level)))
4468       (gnus-delete-line)
4469       (let ((inserted (- (point)
4470                          (progn
4471                            (gnus-summary-insert-line
4472                             header level nil
4473                             (memq article gnus-newsgroup-undownloaded)
4474                             (gnus-article-mark article)
4475                             (memq article gnus-newsgroup-replied)
4476                             (memq article gnus-newsgroup-expirable)
4477                             ;; Only insert the Subject string when it's different
4478                             ;; from the previous Subject string.
4479                             (if (and
4480                                  gnus-show-threads
4481                                  (gnus-subject-equal
4482                                   (condition-case ()
4483                                       (mail-header-subject
4484                                        (gnus-data-header
4485                                         (cadr
4486                                          (gnus-data-find-list
4487                                           article
4488                                           (gnus-data-list t)))))
4489                                     ;; Error on the side of excessive subjects.
4490                                     (error ""))
4491                                   (mail-header-subject header)))
4492                                 ""
4493                               (mail-header-subject header))
4494                             nil (cdr (assq article gnus-newsgroup-scored))
4495                             (memq article gnus-newsgroup-processable))
4496                            (point)))))
4497         (when (cdr datal)
4498           (gnus-data-update-list
4499            (cdr datal)
4500            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4501
4502 (defun gnus-summary-update-article (article &optional iheader)
4503   "Update ARTICLE in the summary buffer."
4504   (set-buffer gnus-summary-buffer)
4505   (let* ((header (gnus-summary-article-header article))
4506          (id (mail-header-id header))
4507          (data (gnus-data-find article))
4508          (thread (gnus-id-to-thread id))
4509          (references (mail-header-references header))
4510          (parent
4511           (gnus-id-to-thread
4512            (or (gnus-parent-id
4513                 (when (and references
4514                            (not (equal "" references)))
4515                   references))
4516                "none")))
4517          (buffer-read-only nil)
4518          (old (car thread)))
4519     (when thread
4520       (unless iheader
4521         (setcar thread nil)
4522         (when parent
4523           (delq thread parent)))
4524       (if (gnus-summary-insert-subject id header)
4525           ;; Set the (possibly) new article number in the data structure.
4526           (gnus-data-set-number data (gnus-id-to-article id))
4527         (setcar thread old)
4528         nil))))
4529
4530 (defun gnus-rebuild-thread (id &optional line)
4531   "Rebuild the thread containing ID.
4532 If LINE, insert the rebuilt thread starting on line LINE."
4533   (let ((buffer-read-only nil)
4534         old-pos current thread data)
4535     (if (not gnus-show-threads)
4536         (setq thread (list (car (gnus-id-to-thread id))))
4537       ;; Get the thread this article is part of.
4538       (setq thread (gnus-remove-thread id)))
4539     (setq old-pos (point-at-bol))
4540     (setq current (save-excursion
4541                     (and (re-search-backward "[\r\n]" nil t)
4542                          (gnus-summary-article-number))))
4543     ;; If this is a gathered thread, we have to go some re-gathering.
4544     (when (stringp (car thread))
4545       (let ((subject (car thread))
4546             roots thr)
4547         (setq thread (cdr thread))
4548         (while thread
4549           (unless (memq (setq thr (gnus-id-to-thread
4550                                    (gnus-root-id
4551                                     (mail-header-id (caar thread)))))
4552                         roots)
4553             (push thr roots))
4554           (setq thread (cdr thread)))
4555         ;; We now have all (unique) roots.
4556         (if (= (length roots) 1)
4557             ;; All the loose roots are now one solid root.
4558             (setq thread (car roots))
4559           (setq thread (cons subject (gnus-sort-threads roots))))))
4560     (let (threads)
4561       ;; We then insert this thread into the summary buffer.
4562       (when line
4563         (goto-char (point-min))
4564         (forward-line (1- line)))
4565       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4566         (if gnus-show-threads
4567             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4568           (gnus-summary-prepare-unthreaded thread))
4569         (setq data (nreverse gnus-newsgroup-data))
4570         (setq threads gnus-newsgroup-threads))
4571       ;; We splice the new data into the data structure.
4572       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4573       ;;!!! then we want to insert at the beginning of the buffer.
4574       ;;!!! That happens to be true with Gnus now, but that may
4575       ;;!!! change in the future.  Perhaps.
4576       (gnus-data-enter-list
4577        (if line nil current) data (- (point) old-pos))
4578       (setq gnus-newsgroup-threads
4579             (nconc threads gnus-newsgroup-threads))
4580       (gnus-data-compute-positions))))
4581
4582 (defun gnus-number-to-header (number)
4583   "Return the header for article NUMBER."
4584   (let ((headers gnus-newsgroup-headers))
4585     (while (and headers
4586                 (not (= number (mail-header-number (car headers)))))
4587       (pop headers))
4588     (when headers
4589       (car headers))))
4590
4591 (defun gnus-parent-headers (in-headers &optional generation)
4592   "Return the headers of the GENERATIONeth parent of HEADERS."
4593   (unless generation
4594     (setq generation 1))
4595   (let ((parent t)
4596         (headers in-headers)
4597         references)
4598     (while (and parent
4599                 (not (zerop generation))
4600                 (setq references (mail-header-references headers)))
4601       (setq headers (if (and references
4602                              (setq parent (gnus-parent-id references)))
4603                         (car (gnus-id-to-thread parent))
4604                       nil))
4605       (decf generation))
4606     (and (not (eq headers in-headers))
4607          headers)))
4608
4609 (defun gnus-id-to-thread (id)
4610   "Return the (sub-)thread where ID appears."
4611   (gnus-gethash id gnus-newsgroup-dependencies))
4612
4613 (defun gnus-id-to-article (id)
4614   "Return the article number of ID."
4615   (let ((thread (gnus-id-to-thread id)))
4616     (when (and thread
4617                (car thread))
4618       (mail-header-number (car thread)))))
4619
4620 (defun gnus-id-to-header (id)
4621   "Return the article headers of ID."
4622   (car (gnus-id-to-thread id)))
4623
4624 (defun gnus-article-displayed-root-p (article)
4625   "Say whether ARTICLE is a root(ish) article."
4626   (let ((level (gnus-summary-thread-level article))
4627         (refs (mail-header-references  (gnus-summary-article-header article)))
4628         particle)
4629     (cond
4630      ((null level) nil)
4631      ((zerop level) t)
4632      ((null refs) t)
4633      ((null (gnus-parent-id refs)) t)
4634      ((and (= 1 level)
4635            (null (setq particle (gnus-id-to-article
4636                                  (gnus-parent-id refs))))
4637            (null (gnus-summary-thread-level particle)))))))
4638
4639 (defun gnus-root-id (id)
4640   "Return the id of the root of the thread where ID appears."
4641   (let (last-id prev)
4642     (while (and id (setq prev (car (gnus-id-to-thread id))))
4643       (setq last-id id
4644             id (gnus-parent-id (mail-header-references prev))))
4645     last-id))
4646
4647 (defun gnus-articles-in-thread (thread)
4648   "Return the list of articles in THREAD."
4649   (cons (mail-header-number (car thread))
4650         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4651
4652 (defun gnus-remove-thread (id &optional dont-remove)
4653   "Remove the thread that has ID in it."
4654   (let (headers thread last-id)
4655     ;; First go up in this thread until we find the root.
4656     (setq last-id (gnus-root-id id)
4657           headers (message-flatten-list (gnus-id-to-thread last-id)))
4658     ;; We have now found the real root of this thread.  It might have
4659     ;; been gathered into some loose thread, so we have to search
4660     ;; through the threads to find the thread we wanted.
4661     (let ((threads gnus-newsgroup-threads)
4662           sub)
4663       (while threads
4664         (setq sub (car threads))
4665         (if (stringp (car sub))
4666             ;; This is a gathered thread, so we look at the roots
4667             ;; below it to find whether this article is in this
4668             ;; gathered root.
4669             (progn
4670               (setq sub (cdr sub))
4671               (while sub
4672                 (when (member (caar sub) headers)
4673                   (setq thread (car threads)
4674                         threads nil
4675                         sub nil))
4676                 (setq sub (cdr sub))))
4677           ;; It's an ordinary thread, so we check it.
4678           (when (eq (car sub) (car headers))
4679             (setq thread sub
4680                   threads nil)))
4681         (setq threads (cdr threads)))
4682       ;; If this article is in no thread, then it's a root.
4683       (if thread
4684           (unless dont-remove
4685             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4686         (setq thread (gnus-id-to-thread last-id)))
4687       (when thread
4688         (prog1
4689             thread                      ; We return this thread.
4690           (unless dont-remove
4691             (if (stringp (car thread))
4692                 (progn
4693                   ;; If we use dummy roots, then we have to remove the
4694                   ;; dummy root as well.
4695                   (when (eq gnus-summary-make-false-root 'dummy)
4696                     ;; We go to the dummy root by going to
4697                     ;; the first sub-"thread", and then one line up.
4698                     (gnus-summary-goto-article
4699                      (mail-header-number (caadr thread)))
4700                     (forward-line -1)
4701                     (gnus-delete-line)
4702                     (gnus-data-compute-positions))
4703                   (setq thread (cdr thread))
4704                   (while thread
4705                     (gnus-remove-thread-1 (car thread))
4706                     (setq thread (cdr thread))))
4707               (gnus-remove-thread-1 thread))))))))
4708
4709 (defun gnus-remove-thread-1 (thread)
4710   "Remove the thread THREAD recursively."
4711   (let ((number (mail-header-number (pop thread)))
4712         d)
4713     (setq thread (reverse thread))
4714     (while thread
4715       (gnus-remove-thread-1 (pop thread)))
4716     (when (setq d (gnus-data-find number))
4717       (goto-char (gnus-data-pos d))
4718       (gnus-summary-show-thread)
4719       (gnus-data-remove
4720        number
4721        (- (point-at-bol)
4722           (prog1
4723               (1+ (point-at-eol))
4724             (gnus-delete-line)))))))
4725
4726 (defun gnus-sort-threads-recursive (threads func)
4727   (sort (mapcar (lambda (thread)
4728                   (cons (car thread)
4729                         (and (cdr thread)
4730                              (gnus-sort-threads-recursive (cdr thread) func))))
4731                 threads) func))
4732
4733 (defun gnus-sort-threads-loop (threads func)
4734   (let* ((superthread (cons nil threads))
4735          (stack (list (cons superthread threads)))
4736          remaining-threads thread)
4737     (while stack
4738       (setq remaining-threads (cdr (car stack)))
4739       (if remaining-threads
4740           (progn (setq thread (car remaining-threads))
4741                  (setcdr (car stack) (cdr remaining-threads))
4742                  (if (cdr thread)
4743                      (push (cons thread (cdr thread)) stack)))
4744         (setq thread (caar stack))
4745         (setcdr thread (sort (cdr thread) func))
4746         (pop stack)))
4747     (cdr superthread)))
4748
4749 (defun gnus-sort-threads (threads)
4750   "Sort THREADS."
4751   (if (not gnus-thread-sort-functions)
4752       threads
4753     (gnus-message 8 "Sorting threads...")
4754     (prog1
4755         (condition-case nil
4756             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4757               (gnus-sort-threads-recursive
4758                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4759           ;; Even after binding max-lisp-eval-depth, the recursive
4760           ;; sorter might fail for very long threads.  In that case,
4761           ;; try using a (less well-tested) non-recursive sorter.
4762           (error (gnus-sort-threads-loop
4763                   threads (gnus-make-sort-function
4764                            gnus-thread-sort-functions))))
4765       (gnus-message 8 "Sorting threads...done"))))
4766
4767 (defun gnus-sort-articles (articles)
4768   "Sort ARTICLES."
4769   (when gnus-article-sort-functions
4770     (gnus-message 7 "Sorting articles...")
4771     (prog1
4772         (setq gnus-newsgroup-headers
4773               (sort articles (gnus-make-sort-function
4774                               gnus-article-sort-functions)))
4775       (gnus-message 7 "Sorting articles...done"))))
4776
4777 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4778 (defmacro gnus-thread-header (thread)
4779   "Return header of first article in THREAD.
4780 Note that THREAD must never, ever be anything else than a variable -
4781 using some other form will lead to serious barfage."
4782   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4783   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4784   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4785         (vector thread) 2))
4786
4787 (defsubst gnus-article-sort-by-number (h1 h2)
4788   "Sort articles by article number."
4789   (< (mail-header-number h1)
4790      (mail-header-number h2)))
4791
4792 (defun gnus-thread-sort-by-number (h1 h2)
4793   "Sort threads by root article number."
4794   (gnus-article-sort-by-number
4795    (gnus-thread-header h1) (gnus-thread-header h2)))
4796
4797 (defsubst gnus-article-sort-by-random (h1 h2)
4798   "Sort articles by article number."
4799   (zerop (random 2)))
4800
4801 (defun gnus-thread-sort-by-random (h1 h2)
4802   "Sort threads by root article number."
4803   (gnus-article-sort-by-random
4804    (gnus-thread-header h1) (gnus-thread-header h2)))
4805
4806 (defsubst gnus-article-sort-by-lines (h1 h2)
4807   "Sort articles by article Lines header."
4808   (< (mail-header-lines h1)
4809      (mail-header-lines h2)))
4810
4811 (defun gnus-thread-sort-by-lines (h1 h2)
4812   "Sort threads by root article Lines header."
4813   (gnus-article-sort-by-lines
4814    (gnus-thread-header h1) (gnus-thread-header h2)))
4815
4816 (defsubst gnus-article-sort-by-chars (h1 h2)
4817   "Sort articles by octet length."
4818   (< (mail-header-chars h1)
4819      (mail-header-chars h2)))
4820
4821 (defun gnus-thread-sort-by-chars (h1 h2)
4822   "Sort threads by root article octet length."
4823   (gnus-article-sort-by-chars
4824    (gnus-thread-header h1) (gnus-thread-header h2)))
4825
4826 (defsubst gnus-article-sort-by-author (h1 h2)
4827   "Sort articles by root author."
4828   (gnus-string<
4829    (let ((extract (funcall
4830                    gnus-extract-address-components
4831                    (mail-header-from h1))))
4832      (or (car extract) (cadr extract) ""))
4833    (let ((extract (funcall
4834                    gnus-extract-address-components
4835                    (mail-header-from h2))))
4836      (or (car extract) (cadr extract) ""))))
4837
4838 (defun gnus-thread-sort-by-author (h1 h2)
4839   "Sort threads by root author."
4840   (gnus-article-sort-by-author
4841    (gnus-thread-header h1)  (gnus-thread-header h2)))
4842
4843 (defsubst gnus-article-sort-by-recipient (h1 h2)
4844   "Sort articles by recipient."
4845   (gnus-string<
4846    (let ((extract (funcall
4847                    gnus-extract-address-components
4848                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4849      (or (car extract) (cadr extract)))
4850    (let ((extract (funcall
4851                    gnus-extract-address-components
4852                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4853      (or (car extract) (cadr extract)))))
4854
4855 (defun gnus-thread-sort-by-recipient (h1 h2)
4856   "Sort threads by root recipient."
4857   (gnus-article-sort-by-recipient
4858    (gnus-thread-header h1) (gnus-thread-header h2)))
4859
4860 (defsubst gnus-article-sort-by-subject (h1 h2)
4861   "Sort articles by root subject."
4862   (gnus-string<
4863    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4864    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4865
4866 (defun gnus-thread-sort-by-subject (h1 h2)
4867   "Sort threads by root subject."
4868   (gnus-article-sort-by-subject
4869    (gnus-thread-header h1) (gnus-thread-header h2)))
4870
4871 (defsubst gnus-article-sort-by-date (h1 h2)
4872   "Sort articles by root article date."
4873   (time-less-p
4874    (gnus-date-get-time (mail-header-date h1))
4875    (gnus-date-get-time (mail-header-date h2))))
4876
4877 (defun gnus-thread-sort-by-date (h1 h2)
4878   "Sort threads by root article date."
4879   (gnus-article-sort-by-date
4880    (gnus-thread-header h1) (gnus-thread-header h2)))
4881
4882 (defsubst gnus-article-sort-by-score (h1 h2)
4883   "Sort articles by root article score.
4884 Unscored articles will be counted as having a score of zero."
4885   (> (or (cdr (assq (mail-header-number h1)
4886                     gnus-newsgroup-scored))
4887          gnus-summary-default-score 0)
4888      (or (cdr (assq (mail-header-number h2)
4889                     gnus-newsgroup-scored))
4890          gnus-summary-default-score 0)))
4891
4892 (defun gnus-thread-sort-by-score (h1 h2)
4893   "Sort threads by root article score."
4894   (gnus-article-sort-by-score
4895    (gnus-thread-header h1) (gnus-thread-header h2)))
4896
4897 (defun gnus-thread-sort-by-total-score (h1 h2)
4898   "Sort threads by the sum of all scores in the thread.
4899 Unscored articles will be counted as having a score of zero."
4900   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4901
4902 (defun gnus-thread-total-score (thread)
4903   ;; This function find the total score of THREAD.
4904   (cond
4905    ((null thread)
4906     0)
4907    ((consp thread)
4908     (if (stringp (car thread))
4909         (apply gnus-thread-score-function 0
4910                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4911       (gnus-thread-total-score-1 thread)))
4912    (t
4913     (gnus-thread-total-score-1 (list thread)))))
4914
4915 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4916   "Sort threads such that the thread with the most recently arrived article comes first."
4917   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4918
4919 (defun gnus-thread-highest-number (thread)
4920   "Return the highest article number in THREAD."
4921   (apply 'max (mapcar (lambda (header)
4922                         (mail-header-number header))
4923                       (message-flatten-list thread))))
4924
4925 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4926   "Sort threads such that the thread with the most recently dated article comes first."
4927   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4928
4929 (defun gnus-thread-latest-date (thread)
4930   "Return the highest article date in THREAD."
4931   (let ((previous-time 0))
4932     (apply 'max
4933            (mapcar
4934             (lambda (header)
4935               (setq previous-time
4936                     (condition-case ()
4937                         (time-to-seconds (mail-header-parse-date
4938                                           (mail-header-date header)))
4939                       (error previous-time))))
4940             (sort
4941              (message-flatten-list thread)
4942              (lambda (h1 h2)
4943                (< (mail-header-number h1)
4944                   (mail-header-number h2))))))))
4945
4946 (defun gnus-thread-total-score-1 (root)
4947   ;; This function find the total score of the thread below ROOT.
4948   (setq root (car root))
4949   (apply gnus-thread-score-function
4950          (or (append
4951               (mapcar 'gnus-thread-total-score
4952                       (cdr (gnus-id-to-thread (mail-header-id root))))
4953               (when (> (mail-header-number root) 0)
4954                 (list (or (cdr (assq (mail-header-number root)
4955                                      gnus-newsgroup-scored))
4956                           gnus-summary-default-score 0))))
4957              (list gnus-summary-default-score)
4958              '(0))))
4959
4960 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4961 (defvar gnus-tmp-prev-subject nil)
4962 (defvar gnus-tmp-false-parent nil)
4963 (defvar gnus-tmp-root-expunged nil)
4964 (defvar gnus-tmp-dummy-line nil)
4965
4966 (defun gnus-extra-header (type &optional header)
4967   "Return the extra header of TYPE."
4968   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4969       ""))
4970
4971 (defvar gnus-tmp-thread-tree-header-string "")
4972
4973 (defcustom gnus-sum-thread-tree-root "> "
4974   "With %B spec, used for the root of a thread.
4975 If nil, use subject instead."
4976   :version "22.1"
4977   :type '(radio (const :format "%v  " nil) string)
4978   :group 'gnus-thread)
4979
4980 (defcustom gnus-sum-thread-tree-false-root "> "
4981   "With %B spec, used for a false root of a thread.
4982 If nil, use subject instead."
4983   :version "22.1"
4984   :type '(radio (const :format "%v  " nil) string)
4985   :group 'gnus-thread)
4986
4987 (defcustom gnus-sum-thread-tree-single-indent ""
4988   "With %B spec, used for a thread with just one message.
4989 If nil, use subject instead."
4990   :version "22.1"
4991   :type '(radio (const :format "%v  " nil) string)
4992   :group 'gnus-thread)
4993
4994 (defcustom gnus-sum-thread-tree-vertical "| "
4995   "With %B spec, used for drawing a vertical line."
4996   :version "22.1"
4997   :type 'string
4998   :group 'gnus-thread)
4999
5000 (defcustom gnus-sum-thread-tree-indent "  "
5001   "With %B spec, used for indenting."
5002   :version "22.1"
5003   :type 'string
5004   :group 'gnus-thread)
5005
5006 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5007   "With %B spec, used for a leaf with brothers."
5008   :version "22.1"
5009   :type 'string
5010   :group 'gnus-thread)
5011
5012 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5013   "With %B spec, used for a leaf without brothers."
5014   :version "22.1"
5015   :type 'string
5016   :group 'gnus-thread)
5017
5018 (defun gnus-summary-prepare-threads (threads)
5019   "Prepare summary buffer from THREADS and indentation LEVEL.
5020 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5021 or a straight list of headers."
5022   (gnus-message 7 "Generating summary...")
5023
5024   (setq gnus-newsgroup-threads threads)
5025   (beginning-of-line)
5026
5027   (let ((gnus-tmp-level 0)
5028         (default-score (or gnus-summary-default-score 0))
5029         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5030         (building-line-count gnus-summary-display-while-building)
5031         (building-count (integerp gnus-summary-display-while-building))
5032         thread number subject stack state gnus-tmp-gathered beg-match
5033         new-roots gnus-tmp-new-adopts thread-end simp-subject
5034         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5035         gnus-tmp-replied gnus-tmp-subject-or-nil
5036         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5037         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5038         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5039         tree-stack)
5040
5041     (setq gnus-tmp-prev-subject nil
5042           gnus-tmp-thread-tree-header-string "")
5043
5044     (if (vectorp (car threads))
5045         ;; If this is a straight (sic) list of headers, then a
5046         ;; threaded summary display isn't required, so we just create
5047         ;; an unthreaded one.
5048         (gnus-summary-prepare-unthreaded threads)
5049
5050       ;; Do the threaded display.
5051
5052       (if gnus-summary-display-while-building
5053           (switch-to-buffer (buffer-name)))
5054       (while (or threads stack gnus-tmp-new-adopts new-roots)
5055
5056         (if (and (= gnus-tmp-level 0)
5057                  (or (not stack)
5058                      (= (caar stack) 0))
5059                  (not gnus-tmp-false-parent)
5060                  (or gnus-tmp-new-adopts new-roots))
5061             (if gnus-tmp-new-adopts
5062                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5063                       thread (list (car gnus-tmp-new-adopts))
5064                       gnus-tmp-header (caar thread)
5065                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5066               (when new-roots
5067                 (setq thread (list (car new-roots))
5068                       gnus-tmp-header (caar thread)
5069                       new-roots (cdr new-roots))))
5070
5071           (if threads
5072               ;; If there are some threads, we do them before the
5073               ;; threads on the stack.
5074               (setq thread threads
5075                     gnus-tmp-header (caar thread))
5076             ;; There were no current threads, so we pop something off
5077             ;; the stack.
5078             (setq state (car stack)
5079                   gnus-tmp-level (car state)
5080                   tree-stack (cadr state)
5081                   thread (caddr state)
5082                   stack (cdr stack)
5083                   gnus-tmp-header (caar thread))))
5084
5085         (setq gnus-tmp-false-parent nil)
5086         (setq gnus-tmp-root-expunged nil)
5087         (setq thread-end nil)
5088
5089         (if (stringp gnus-tmp-header)
5090             ;; The header is a dummy root.
5091             (cond
5092              ((eq gnus-summary-make-false-root 'adopt)
5093               ;; We let the first article adopt the rest.
5094               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5095                                                (cddar thread)))
5096               (setq gnus-tmp-gathered
5097                     (nconc (mapcar
5098                             (lambda (h) (mail-header-number (car h)))
5099                             (cddar thread))
5100                            gnus-tmp-gathered))
5101               (setq thread (cons (list (caar thread)
5102                                        (cadar thread))
5103                                  (cdr thread)))
5104               (setq gnus-tmp-level -1
5105                     gnus-tmp-false-parent t))
5106              ((eq gnus-summary-make-false-root 'empty)
5107               ;; We print adopted articles with empty subject fields.
5108               (setq gnus-tmp-gathered
5109                     (nconc (mapcar
5110                             (lambda (h) (mail-header-number (car h)))
5111                             (cddar thread))
5112                            gnus-tmp-gathered))
5113               (setq gnus-tmp-level -1))
5114              ((eq gnus-summary-make-false-root 'dummy)
5115               ;; We remember that we probably want to output a dummy
5116               ;; root.
5117               (setq gnus-tmp-dummy-line gnus-tmp-header)
5118               (setq gnus-tmp-prev-subject gnus-tmp-header))
5119              (t
5120               ;; We do not make a root for the gathered
5121               ;; sub-threads at all.
5122               (setq gnus-tmp-level -1)))
5123
5124           (setq number (mail-header-number gnus-tmp-header)
5125                 subject (mail-header-subject gnus-tmp-header)
5126                 simp-subject (gnus-simplify-subject-fully subject))
5127
5128           (cond
5129            ;; If the thread has changed subject, we might want to make
5130            ;; this subthread into a root.
5131            ((and (null gnus-thread-ignore-subject)
5132                  (not (zerop gnus-tmp-level))
5133                  gnus-tmp-prev-subject
5134                  (not (string= gnus-tmp-prev-subject simp-subject)))
5135             (setq new-roots (nconc new-roots (list (car thread)))
5136                   thread-end t
5137                   gnus-tmp-header nil))
5138            ;; If the article lies outside the current limit,
5139            ;; then we do not display it.
5140            ((not (memq number gnus-newsgroup-limit))
5141             (setq gnus-tmp-gathered
5142                   (nconc (mapcar
5143                           (lambda (h) (mail-header-number (car h)))
5144                           (cdar thread))
5145                          gnus-tmp-gathered))
5146             (setq gnus-tmp-new-adopts (if (cdar thread)
5147                                           (append gnus-tmp-new-adopts
5148                                                   (cdar thread))
5149                                         gnus-tmp-new-adopts)
5150                   thread-end t
5151                   gnus-tmp-header nil)
5152             (when (zerop gnus-tmp-level)
5153               (setq gnus-tmp-root-expunged t)))
5154            ;; Perhaps this article is to be marked as read?
5155            ((and gnus-summary-mark-below
5156                  (< (or (cdr (assq number gnus-newsgroup-scored))
5157                         default-score)
5158                     gnus-summary-mark-below)
5159                  ;; Don't touch sparse articles.
5160                  (not (gnus-summary-article-sparse-p number))
5161                  (not (gnus-summary-article-ancient-p number)))
5162             (setq gnus-newsgroup-unreads
5163                   (delq number gnus-newsgroup-unreads))
5164             (if gnus-newsgroup-auto-expire
5165                 (setq gnus-newsgroup-expirable
5166                       (gnus-add-to-sorted-list
5167                        gnus-newsgroup-expirable number))
5168               (push (cons number gnus-low-score-mark)
5169                     gnus-newsgroup-reads))))
5170
5171           (when gnus-tmp-header
5172             ;; We may have an old dummy line to output before this
5173             ;; article.
5174             (when (and gnus-tmp-dummy-line
5175                        (gnus-subject-equal
5176                         gnus-tmp-dummy-line
5177                         (mail-header-subject gnus-tmp-header)))
5178               (gnus-summary-insert-dummy-line
5179                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5180               (setq gnus-tmp-dummy-line nil))
5181
5182             ;; Compute the mark.
5183             (setq gnus-tmp-unread (gnus-article-mark number))
5184
5185             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5186                                   gnus-tmp-header gnus-tmp-level)
5187                   gnus-newsgroup-data)
5188
5189             ;; Actually insert the line.
5190             (setq
5191              gnus-tmp-subject-or-nil
5192              (cond
5193               ((and gnus-thread-ignore-subject
5194                     gnus-tmp-prev-subject
5195                     (not (string= gnus-tmp-prev-subject simp-subject)))
5196                subject)
5197               ((zerop gnus-tmp-level)
5198                (if (and (eq gnus-summary-make-false-root 'empty)
5199                         (memq number gnus-tmp-gathered)
5200                         gnus-tmp-prev-subject
5201                         (string= gnus-tmp-prev-subject simp-subject))
5202                    gnus-summary-same-subject
5203                  subject))
5204               (t gnus-summary-same-subject)))
5205             (if (and (eq gnus-summary-make-false-root 'adopt)
5206                      (= gnus-tmp-level 1)
5207                      (memq number gnus-tmp-gathered))
5208                 (setq gnus-tmp-opening-bracket ?\<
5209                       gnus-tmp-closing-bracket ?\>)
5210               (setq gnus-tmp-opening-bracket ?\[
5211                     gnus-tmp-closing-bracket ?\]))
5212             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5213                 (gnus-make-thread-indent-array
5214                  (max (* 2 (length gnus-thread-indent-array))
5215                       gnus-tmp-level)))
5216             (setq
5217              gnus-tmp-indentation
5218              (aref gnus-thread-indent-array gnus-tmp-level)
5219              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5220              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5221                                 gnus-summary-default-score 0)
5222              gnus-tmp-score-char
5223              (if (or (null gnus-summary-default-score)
5224                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5225                          gnus-summary-zcore-fuzz))
5226                  ?                      ;Whitespace
5227                (if (< gnus-tmp-score gnus-summary-default-score)
5228                    gnus-score-below-mark gnus-score-over-mark))
5229              gnus-tmp-replied
5230              (cond ((memq number gnus-newsgroup-processable)
5231                     gnus-process-mark)
5232                    ((memq number gnus-newsgroup-cached)
5233                     gnus-cached-mark)
5234                    ((memq number gnus-newsgroup-replied)
5235                     gnus-replied-mark)
5236                    ((memq number gnus-newsgroup-forwarded)
5237                     gnus-forwarded-mark)
5238                    ((memq number gnus-newsgroup-saved)
5239                     gnus-saved-mark)
5240                    ((memq number gnus-newsgroup-recent)
5241                     gnus-recent-mark)
5242                    ((memq number gnus-newsgroup-unseen)
5243                     gnus-unseen-mark)
5244                    (t gnus-no-mark))
5245              gnus-tmp-downloaded
5246              (cond ((memq number gnus-newsgroup-undownloaded)
5247                     gnus-undownloaded-mark)
5248                    (gnus-newsgroup-agentized
5249                     gnus-downloaded-mark)
5250                    (t
5251                     gnus-no-mark))
5252              gnus-tmp-from (mail-header-from gnus-tmp-header)
5253              gnus-tmp-name
5254              (cond
5255               ((string-match "<[^>]+> *$" gnus-tmp-from)
5256                (setq beg-match (match-beginning 0))
5257                (or (and (string-match "^\".+\"" gnus-tmp-from)
5258                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5259                    (substring gnus-tmp-from 0 beg-match)))
5260               ((string-match "(.+)" gnus-tmp-from)
5261                (substring gnus-tmp-from
5262                           (1+ (match-beginning 0)) (1- (match-end 0))))
5263               (t gnus-tmp-from))
5264
5265              ;; Do the %B string
5266              gnus-tmp-thread-tree-header-string
5267              (cond
5268               ((not gnus-show-threads) "")
5269               ((zerop gnus-tmp-level)
5270                (cond ((cdar thread)
5271                       (or gnus-sum-thread-tree-root subject))
5272                      (gnus-tmp-new-adopts
5273                       (or gnus-sum-thread-tree-false-root subject))
5274                      (t
5275                       (or gnus-sum-thread-tree-single-indent subject))))
5276               (t
5277                (concat (apply 'concat
5278                               (mapcar (lambda (item)
5279                                         (if (= item 1)
5280                                             gnus-sum-thread-tree-vertical
5281                                           gnus-sum-thread-tree-indent))
5282                                       (cdr (reverse tree-stack))))
5283                        (if (nth 1 thread)
5284                            gnus-sum-thread-tree-leaf-with-other
5285                          gnus-sum-thread-tree-single-leaf)))))
5286             (when (string= gnus-tmp-name "")
5287               (setq gnus-tmp-name gnus-tmp-from))
5288             (unless (numberp gnus-tmp-lines)
5289               (setq gnus-tmp-lines -1))
5290             (if (= gnus-tmp-lines -1)
5291                 (setq gnus-tmp-lines "?")
5292               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5293               (gnus-put-text-property
5294              (point)
5295              (progn (eval gnus-summary-line-format-spec) (point))
5296                'gnus-number number)
5297             (when gnus-visual-p
5298               (forward-line -1)
5299               (gnus-run-hooks 'gnus-summary-update-hook)
5300               (forward-line 1))
5301
5302             (setq gnus-tmp-prev-subject simp-subject)))
5303
5304         (when (nth 1 thread)
5305           (push (list (max 0 gnus-tmp-level)
5306                       (copy-sequence tree-stack)
5307                       (nthcdr 1 thread))
5308                 stack))
5309         (push (if (nth 1 thread) 1 0) tree-stack)
5310         (incf gnus-tmp-level)
5311         (setq threads (if thread-end nil (cdar thread)))
5312         (if gnus-summary-display-while-building
5313             (if building-count
5314                 (progn
5315                   ;; use a set frequency
5316                   (setq building-line-count (1- building-line-count))
5317                   (when (= building-line-count 0)
5318                     (sit-for 0)
5319                     (setq building-line-count
5320                           gnus-summary-display-while-building)))
5321               ;; always
5322               (sit-for 0)))
5323         (unless threads
5324           (setq gnus-tmp-level 0)))))
5325   (gnus-message 7 "Generating summary...done"))
5326
5327 (defun gnus-summary-prepare-unthreaded (headers)
5328   "Generate an unthreaded summary buffer based on HEADERS."
5329   (let (header number mark)
5330
5331     (beginning-of-line)
5332
5333     (while headers
5334       ;; We may have to root out some bad articles...
5335       (when (memq (setq number (mail-header-number
5336                                 (setq header (pop headers))))
5337                   gnus-newsgroup-limit)
5338         ;; Mark article as read when it has a low score.
5339         (when (and gnus-summary-mark-below
5340                    (< (or (cdr (assq number gnus-newsgroup-scored))
5341                           gnus-summary-default-score 0)
5342                       gnus-summary-mark-below)
5343                    (not (gnus-summary-article-ancient-p number)))
5344           (setq gnus-newsgroup-unreads
5345                 (delq number gnus-newsgroup-unreads))
5346           (if gnus-newsgroup-auto-expire
5347               (push number gnus-newsgroup-expirable)
5348             (push (cons number gnus-low-score-mark)
5349                   gnus-newsgroup-reads)))
5350
5351         (setq mark (gnus-article-mark number))
5352         (push (gnus-data-make number mark (1+ (point)) header 0)
5353               gnus-newsgroup-data)
5354         (gnus-summary-insert-line
5355          header 0 number
5356          (memq number gnus-newsgroup-undownloaded)
5357          mark (memq number gnus-newsgroup-replied)
5358          (memq number gnus-newsgroup-expirable)
5359          (mail-header-subject header) nil
5360          (cdr (assq number gnus-newsgroup-scored))
5361          (memq number gnus-newsgroup-processable))))))
5362
5363 (defun gnus-summary-remove-list-identifiers ()
5364   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5365   (let ((regexp (if (consp gnus-list-identifiers)
5366                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5367                   gnus-list-identifiers))
5368         changed subject)
5369     (when regexp
5370       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5371       (dolist (header gnus-newsgroup-headers)
5372         (setq subject (mail-header-subject header)
5373               changed nil)
5374         (while (string-match regexp subject)
5375           (setq subject
5376                 (concat (substring subject 0 (match-beginning 1))
5377                         (substring subject (match-end 0)))
5378                 changed t))
5379         (when changed
5380           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5381             (setq subject
5382                   (concat (substring subject 0 (match-beginning 1))
5383                           (substring subject (match-end 1)))))
5384           (mail-header-set-subject header subject))))))
5385
5386 (defun gnus-fetch-headers (articles)
5387   "Fetch headers of ARTICLES."
5388   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5389     (gnus-message 5 "Fetching headers for %s..." name)
5390     (prog1
5391         (if (eq 'nov
5392                 (setq gnus-headers-retrieved-by
5393                       (gnus-retrieve-headers
5394                        articles gnus-newsgroup-name
5395                        ;; We might want to fetch old headers, but
5396                        ;; not if there is only 1 article.
5397                        (and (or (and
5398                                  (not (eq gnus-fetch-old-headers 'some))
5399                                  (not (numberp gnus-fetch-old-headers)))
5400                                 (> (length articles) 1))
5401                             gnus-fetch-old-headers))))
5402             (gnus-get-newsgroup-headers-xover
5403              articles nil nil gnus-newsgroup-name t)
5404           (gnus-get-newsgroup-headers))
5405       (gnus-message 5 "Fetching headers for %s...done" name))))
5406
5407 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5408   "Select newsgroup GROUP.
5409 If READ-ALL is non-nil, all articles in the group are selected.
5410 If SELECT-ARTICLES, only select those articles from GROUP."
5411   (let* ((entry (gnus-group-entry group))
5412          ;;!!! Dirty hack; should be removed.
5413          (gnus-summary-ignore-duplicates
5414           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5415               t
5416             gnus-summary-ignore-duplicates))
5417          (info (nth 2 entry))
5418          charset articles fetched-articles cached)
5419
5420     (unless (gnus-check-server
5421              (set (make-local-variable 'gnus-current-select-method)
5422                   (gnus-find-method-for-group group)))
5423       (error "Couldn't open server"))
5424     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5425
5426     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5427         (gnus-activate-group group)     ; Or we can activate it...
5428         (progn                          ; Or we bug out.
5429           (when (equal major-mode 'gnus-summary-mode)
5430             (gnus-kill-buffer (current-buffer)))
5431           (error
5432            "Couldn't activate group %s: %s"
5433            (mm-decode-coding-string group charset)
5434            (mm-decode-coding-string (gnus-status-message group) charset))))
5435
5436     (unless (gnus-request-group group t)
5437         (when (equal major-mode 'gnus-summary-mode)
5438           (gnus-kill-buffer (current-buffer)))
5439         (error "Couldn't request group %s: %s"
5440                (mm-decode-coding-string group charset)
5441                (mm-decode-coding-string (gnus-status-message group) charset)))
5442
5443     (when gnus-agent
5444       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5445
5446       (setq gnus-summary-use-undownloaded-faces
5447             (gnus-agent-find-parameter
5448              group
5449              'agent-enable-undownloaded-faces)))
5450
5451     (setq gnus-newsgroup-name group
5452           gnus-newsgroup-unselected nil
5453           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5454
5455     (let ((display (gnus-group-find-parameter group 'display)))
5456       (setq gnus-newsgroup-display
5457             (cond
5458              ((not (zerop (or (car-safe read-all) 0)))
5459               ;; The user entered the group with C-u SPC/RET, let's show
5460               ;; all articles.
5461               'gnus-not-ignore)
5462              ((eq display 'all)
5463               'gnus-not-ignore)
5464              ((arrayp display)
5465               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5466              ((numberp display)
5467               ;; The following is probably the "correct" solution, but
5468               ;; it makes Gnus fetch all headers and then limit the
5469               ;; articles (which is slow), so instead we hack the
5470               ;; select-articles parameter instead. -- Simon Josefsson
5471               ;; <jas@kth.se>
5472               ;;
5473               ;; (gnus-byte-compile
5474               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5475               ;;                         display)))))
5476               (setq select-articles
5477                     (gnus-uncompress-range
5478                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5479                              (if (> tmp 0)
5480                                  tmp
5481                                1))
5482                            (cdr (gnus-active group)))))
5483               nil)
5484              (t
5485               nil))))
5486
5487     (gnus-summary-setup-default-charset)
5488
5489     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5490     (when (gnus-virtual-group-p group)
5491       (setq cached gnus-newsgroup-cached))
5492
5493     (setq gnus-newsgroup-unreads
5494           (gnus-sorted-ndifference
5495            (gnus-sorted-ndifference gnus-newsgroup-unreads
5496                                     gnus-newsgroup-marked)
5497            gnus-newsgroup-dormant))
5498
5499     (setq gnus-newsgroup-processable nil)
5500
5501     (gnus-update-read-articles group gnus-newsgroup-unreads)
5502
5503     ;; Adjust and set lists of article marks.
5504     (when info
5505       (gnus-adjust-marked-articles info))
5506     (if (setq articles select-articles)
5507         (setq gnus-newsgroup-unselected
5508               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5509       (setq articles (gnus-articles-to-read group read-all)))
5510
5511     (cond
5512      ((null articles)
5513       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5514       'quit)
5515      ((eq articles 0) nil)
5516      (t
5517       ;; Init the dependencies hash table.
5518       (setq gnus-newsgroup-dependencies
5519             (gnus-make-hashtable (length articles)))
5520       (gnus-set-global-variables)
5521       ;; Retrieve the headers and read them in.
5522
5523       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5524
5525       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5526       (when cached
5527         (setq gnus-newsgroup-cached cached))
5528
5529       ;; Suppress duplicates?
5530       (when gnus-suppress-duplicates
5531         (gnus-dup-suppress-articles))
5532
5533       ;; Set the initial limit.
5534       (setq gnus-newsgroup-limit (copy-sequence articles))
5535       ;; Remove canceled articles from the list of unread articles.
5536       (setq fetched-articles
5537             (mapcar (lambda (headers) (mail-header-number headers))
5538                     gnus-newsgroup-headers))
5539       (setq gnus-newsgroup-articles fetched-articles)
5540       (setq gnus-newsgroup-unreads
5541             (gnus-sorted-nintersection
5542              gnus-newsgroup-unreads fetched-articles))
5543       (gnus-compute-unseen-list)
5544
5545       ;; Removed marked articles that do not exist.
5546       (gnus-update-missing-marks
5547        (gnus-sorted-difference articles fetched-articles))
5548       ;; We might want to build some more threads first.
5549       (when (and gnus-fetch-old-headers
5550                  (eq gnus-headers-retrieved-by 'nov))
5551         (if (eq gnus-fetch-old-headers 'invisible)
5552             (gnus-build-all-threads)
5553           (gnus-build-old-threads)))
5554       ;; Let the Gnus agent mark articles as read.
5555       (when gnus-agent
5556         (gnus-agent-get-undownloaded-list))
5557       ;; Remove list identifiers from subject
5558       (when gnus-list-identifiers
5559         (gnus-summary-remove-list-identifiers))
5560       ;; Check whether auto-expire is to be done in this group.
5561       (setq gnus-newsgroup-auto-expire
5562             (gnus-group-auto-expirable-p group))
5563       ;; Set up the article buffer now, if necessary.
5564       (unless (and gnus-single-article-buffer
5565                    (equal gnus-article-buffer "*Article*"))
5566         (gnus-article-setup-buffer))
5567       ;; First and last article in this newsgroup.
5568       (when gnus-newsgroup-headers
5569         (setq gnus-newsgroup-begin
5570               (mail-header-number (car gnus-newsgroup-headers))
5571               gnus-newsgroup-end
5572               (mail-header-number
5573                (gnus-last-element gnus-newsgroup-headers))))
5574       ;; GROUP is successfully selected.
5575       (or gnus-newsgroup-headers t)))))
5576
5577 (defun gnus-compute-unseen-list ()
5578   ;; The `seen' marks are treated specially.
5579   (if (not gnus-newsgroup-seen)
5580       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5581     (setq gnus-newsgroup-unseen
5582           (gnus-inverse-list-range-intersection
5583            gnus-newsgroup-articles gnus-newsgroup-seen))))
5584
5585 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5586
5587 (defun gnus-summary-display-make-predicate (display)
5588   (require 'gnus-agent)
5589   (when (= (length display) 1)
5590     (setq display (car display)))
5591   (unless gnus-summary-display-cache
5592     (dolist (elem (append '((unread . unread)
5593                             (read . read)
5594                             (unseen . unseen))
5595                           gnus-article-mark-lists))
5596       (push (cons (cdr elem)
5597                   (gnus-byte-compile
5598                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5599             gnus-summary-display-cache)))
5600   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5601         (gnus-category-predicate-cache gnus-summary-display-cache))
5602     (gnus-get-predicate display)))
5603
5604 ;; Uses the dynamically bound `number' variable.
5605 (defvar number)
5606 (defun gnus-article-marked-p (type &optional article)
5607   (let ((article (or article number)))
5608     (cond
5609      ((eq type 'tick)
5610       (memq article gnus-newsgroup-marked))
5611      ((eq type 'spam)
5612       (memq article gnus-newsgroup-spam-marked))
5613      ((eq type 'unsend)
5614       (memq article gnus-newsgroup-unsendable))
5615      ((eq type 'undownload)
5616       (memq article gnus-newsgroup-undownloaded))
5617      ((eq type 'download)
5618       (memq article gnus-newsgroup-downloadable))
5619      ((eq type 'unread)
5620       (memq article gnus-newsgroup-unreads))
5621      ((eq type 'read)
5622       (memq article gnus-newsgroup-reads))
5623      ((eq type 'dormant)
5624       (memq article gnus-newsgroup-dormant) )
5625      ((eq type 'expire)
5626       (memq article gnus-newsgroup-expirable))
5627      ((eq type 'reply)
5628       (memq article gnus-newsgroup-replied))
5629      ((eq type 'killed)
5630       (memq article gnus-newsgroup-killed))
5631      ((eq type 'bookmark)
5632       (assq article gnus-newsgroup-bookmarks))
5633      ((eq type 'score)
5634       (assq article gnus-newsgroup-scored))
5635      ((eq type 'save)
5636       (memq article gnus-newsgroup-saved))
5637      ((eq type 'cache)
5638       (memq article gnus-newsgroup-cached))
5639      ((eq type 'forward)
5640       (memq article gnus-newsgroup-forwarded))
5641      ((eq type 'seen)
5642       (not (memq article gnus-newsgroup-unseen)))
5643      ((eq type 'recent)
5644       (memq article gnus-newsgroup-recent))
5645      (t t))))
5646
5647 (defun gnus-articles-to-read (group &optional read-all)
5648   "Find out what articles the user wants to read."
5649   (let* ((articles
5650           ;; Select all articles if `read-all' is non-nil, or if there
5651           ;; are no unread articles.
5652           (if (or read-all
5653                   (and (zerop (length gnus-newsgroup-marked))
5654                        (zerop (length gnus-newsgroup-unreads)))
5655                   ;; Fetch all if the predicate is non-nil.
5656                   gnus-newsgroup-display)
5657               ;; We want to select the headers for all the articles in
5658               ;; the group, so we select either all the active
5659               ;; articles in the group, or (if that's nil), the
5660               ;; articles in the cache.
5661               (or
5662                (if gnus-newsgroup-maximum-articles
5663                    (let ((active (gnus-active group)))
5664                      (gnus-uncompress-range
5665                       (cons (max (car active)
5666                                  (- (cdr active)
5667                                     gnus-newsgroup-maximum-articles
5668                                     -1))
5669                             (cdr active))))
5670                  (gnus-uncompress-range (gnus-active group)))
5671                (gnus-cache-articles-in-group group))
5672             ;; Select only the "normal" subset of articles.
5673             (gnus-sorted-nunion
5674              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5675              gnus-newsgroup-unreads)))
5676          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5677          (scored (length scored-list))
5678          (number (length articles))
5679          (marked (+ (length gnus-newsgroup-marked)
5680                     (length gnus-newsgroup-dormant)))
5681          (select
5682           (cond
5683            ((numberp read-all)
5684             read-all)
5685            ((numberp gnus-newsgroup-display)
5686             gnus-newsgroup-display)
5687            (t
5688             (condition-case ()
5689                 (cond
5690                  ((and (or (<= scored marked) (= scored number))
5691                        (numberp gnus-large-newsgroup)
5692                        (> number gnus-large-newsgroup))
5693                   (let* ((cursor-in-echo-area nil)
5694                          (initial (gnus-parameter-large-newsgroup-initial
5695                                    gnus-newsgroup-name))
5696                          (input
5697                           (read-string
5698                            (format
5699                             "How many articles from %s (%s %d): "
5700                             (gnus-group-decoded-name gnus-newsgroup-name)
5701                             (if initial "max" "default")
5702                             number)
5703                            (if initial
5704                                (cons (number-to-string initial)
5705                                      0)))))
5706                     (if (string-match "^[ \t]*$" input) number input)))
5707                  ((and (> scored marked) (< scored number)
5708                        (> (- scored number) 20))
5709                   (let ((input
5710                          (read-string
5711                           (format "%s %s (%d scored, %d total): "
5712                                   "How many articles from"
5713                                   (gnus-group-decoded-name group)
5714                                   scored number))))
5715                     (if (string-match "^[ \t]*$" input)
5716                         number input)))
5717                  (t number))
5718               (quit
5719                (message "Quit getting the articles to read")
5720                nil))))))
5721     (setq select (if (stringp select) (string-to-number select) select))
5722     (if (or (null select) (zerop select))
5723         select
5724       (if (and (not (zerop scored)) (<= (abs select) scored))
5725           (progn
5726             (setq articles (sort scored-list '<))
5727             (setq number (length articles)))
5728         (setq articles (copy-sequence articles)))
5729
5730       (when (< (abs select) number)
5731         (if (< select 0)
5732             ;; Select the N oldest articles.
5733             (setcdr (nthcdr (1- (abs select)) articles) nil)
5734           ;; Select the N most recent articles.
5735           (setq articles (nthcdr (- number select) articles))))
5736       (setq gnus-newsgroup-unselected
5737             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5738       (when gnus-alter-articles-to-read-function
5739         (setq articles
5740               (sort
5741                (funcall gnus-alter-articles-to-read-function
5742                         gnus-newsgroup-name articles)
5743                '<)))
5744       articles)))
5745
5746 (defun gnus-killed-articles (killed articles)
5747   (let (out)
5748     (while articles
5749       (when (inline (gnus-member-of-range (car articles) killed))
5750         (push (car articles) out))
5751       (setq articles (cdr articles)))
5752     out))
5753
5754 (defun gnus-uncompress-marks (marks)
5755   "Uncompress the mark ranges in MARKS."
5756   (let ((uncompressed '(score bookmark))
5757         out)
5758     (while marks
5759       (if (memq (caar marks) uncompressed)
5760           (push (car marks) out)
5761         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5762       (setq marks (cdr marks)))
5763     out))
5764
5765 (defun gnus-article-mark-to-type (mark)
5766   "Return the type of MARK."
5767   (or (cadr (assq mark gnus-article-special-mark-lists))
5768       'list))
5769
5770 (defun gnus-article-unpropagatable-p (mark)
5771   "Return whether MARK should be propagated to back end."
5772   (memq mark gnus-article-unpropagated-mark-lists))
5773
5774 (defun gnus-adjust-marked-articles (info)
5775   "Set all article lists and remove all marks that are no longer valid."
5776   (let* ((marked-lists (gnus-info-marks info))
5777          (active (gnus-active (gnus-info-group info)))
5778          (min (car active))
5779          (max (cdr active))
5780          (types gnus-article-mark-lists)
5781          marks var articles article mark mark-type
5782          bgn end)
5783
5784     (dolist (marks marked-lists)
5785       (setq mark (car marks)
5786             mark-type (gnus-article-mark-to-type mark)
5787             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5788
5789       ;; We set the variable according to the type of the marks list,
5790       ;; and then adjust the marks to a subset of the active articles.
5791       (cond
5792        ;; Adjust "simple" lists - compressed yet unsorted
5793        ((eq mark-type 'list)
5794         ;; Simultaneously uncompress and clip to active range
5795         ;; See gnus-uncompress-range for a description of possible marks
5796         (let (l lh)
5797           (if (not (cadr marks))
5798               (set var nil)
5799             (setq articles (if (numberp (cddr marks))
5800                                (list (cdr marks))
5801                              (cdr marks))
5802                   lh (cons nil nil)
5803                   l lh)
5804
5805             (while (setq article (pop articles))
5806               (cond ((consp article)
5807                      (setq bgn (max (car article) min)
5808                            end (min (cdr article) max))
5809                      (while (<= bgn end)
5810                        (setq l (setcdr l (cons bgn nil))
5811                              bgn (1+ bgn))))
5812                     ((and (<= min article)
5813                           (>= max article))
5814                      (setq l (setcdr l (cons article nil))))))
5815             (set var (cdr lh)))))
5816        ;; Adjust assocs.
5817        ((eq mark-type 'tuple)
5818         (set var (setq articles (cdr marks)))
5819         (when (not (listp (cdr (symbol-value var))))
5820           (set var (list (symbol-value var))))
5821         (when (not (listp (cdr articles)))
5822           (setq articles (list articles)))
5823         (while articles
5824           (when (or (not (consp (setq article (pop articles))))
5825                     (< (car article) min)
5826                     (> (car article) max))
5827             (set var (delq article (symbol-value var))))))
5828        ;; Adjust ranges (sloppily).
5829        ((eq mark-type 'range)
5830         (cond
5831          ((eq mark 'seen)
5832           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5833           ;; It should be (seen (NUM1 . NUM2)).
5834           (when (numberp (cddr marks))
5835             (setcdr marks (list (cdr marks))))
5836           (setq articles (cdr marks))
5837           (while (and articles
5838                       (or (and (consp (car articles))
5839                                (> min (cdar articles)))
5840                           (and (numberp (car articles))
5841                                (> min (car articles)))))
5842             (pop articles))
5843           (set var articles))))))))
5844
5845 (defun gnus-update-missing-marks (missing)
5846   "Go through the list of MISSING articles and remove them from the mark lists."
5847   (when missing
5848     (let (var m)
5849       ;; Go through all types.
5850       (dolist (elem gnus-article-mark-lists)
5851         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5852           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5853           (when (symbol-value var)
5854             ;; This list has articles.  So we delete all missing
5855             ;; articles from it.
5856             (setq m missing)
5857             (while m
5858               (set var (delq (pop m) (symbol-value var))))))))))
5859
5860 (defun gnus-update-marks ()
5861   "Enter the various lists of marked articles into the newsgroup info list."
5862   (let ((types gnus-article-mark-lists)
5863         (info (gnus-get-info gnus-newsgroup-name))
5864         type list newmarked symbol delta-marks)
5865     (when info
5866       ;; Add all marks lists to the list of marks lists.
5867       (while (setq type (pop types))
5868         (setq list (symbol-value
5869                     (setq symbol
5870                           (intern (format "gnus-newsgroup-%s" (car type))))))
5871
5872         (when list
5873           ;; Get rid of the entries of the articles that have the
5874           ;; default score.
5875           (when (and (eq (cdr type) 'score)
5876                      gnus-save-score
5877                      list)
5878             (let* ((arts list)
5879                    (prev (cons nil list))
5880                    (all prev))
5881               (while arts
5882                 (if (or (not (consp (car arts)))
5883                         (= (cdar arts) gnus-summary-default-score))
5884                     (setcdr prev (cdr arts))
5885                   (setq prev arts))
5886                 (setq arts (cdr arts)))
5887               (setq list (cdr all)))))
5888
5889         (when (eq (cdr type) 'seen)
5890           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5891
5892         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5893           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5894
5895         (when (and (gnus-check-backend-function
5896                     'request-set-mark gnus-newsgroup-name)
5897                    (not (gnus-article-unpropagatable-p (cdr type))))
5898           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5899                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5900                  (add (gnus-remove-from-range
5901                        (gnus-copy-sequence list) old)))
5902             (when add
5903               (push (list add 'add (list (cdr type))) delta-marks))
5904             (when del
5905               (push (list del 'del (list (cdr type))) delta-marks))))
5906
5907         (when list
5908           (push (cons (cdr type) list) newmarked)))
5909
5910       (when delta-marks
5911         (unless (gnus-check-group gnus-newsgroup-name)
5912           (error "Can't open server for %s" gnus-newsgroup-name))
5913         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5914
5915       ;; Enter these new marks into the info of the group.
5916       (if (nthcdr 3 info)
5917           (setcar (nthcdr 3 info) newmarked)
5918         ;; Add the marks lists to the end of the info.
5919         (when newmarked
5920           (setcdr (nthcdr 2 info) (list newmarked))))
5921
5922       ;; Cut off the end of the info if there's nothing else there.
5923       (let ((i 5))
5924         (while (and (> i 2)
5925                     (not (nth i info)))
5926           (when (nthcdr (decf i) info)
5927             (setcdr (nthcdr i info) nil)))))))
5928
5929 (defun gnus-set-mode-line (where)
5930   "Set the mode line of the article or summary buffers.
5931 If WHERE is `summary', the summary mode line format will be used."
5932   ;; Is this mode line one we keep updated?
5933   (when (and (memq where gnus-updated-mode-lines)
5934              (symbol-value
5935               (intern (format "gnus-%s-mode-line-format-spec" where))))
5936     (let (mode-string)
5937       (save-excursion
5938         ;; We evaluate this in the summary buffer since these
5939         ;; variables are buffer-local to that buffer.
5940         (set-buffer gnus-summary-buffer)
5941        ;; We bind all these variables that are used in the `eval' form
5942         ;; below.
5943         (let* ((mformat (symbol-value
5944                          (intern
5945                           (format "gnus-%s-mode-line-format-spec" where))))
5946                (gnus-tmp-group-name (gnus-mode-string-quote
5947                                      (gnus-group-decoded-name
5948                                       gnus-newsgroup-name)))
5949                (gnus-tmp-article-number (or gnus-current-article 0))
5950                (gnus-tmp-unread gnus-newsgroup-unreads)
5951                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5952                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5953                (gnus-tmp-unread-and-unselected
5954                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5955                             (zerop gnus-tmp-unselected))
5956                        "")
5957                       ((zerop gnus-tmp-unselected)
5958                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5959                       (t (format "{%d(+%d) more}"
5960                                  gnus-tmp-unread-and-unticked
5961                                  gnus-tmp-unselected))))
5962                (gnus-tmp-subject
5963                 (if (and gnus-current-headers
5964                          (vectorp gnus-current-headers))
5965                     (gnus-mode-string-quote
5966                      (mail-header-subject gnus-current-headers))
5967                   ""))
5968                bufname-length max-len
5969                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5970           (setq mode-string (eval mformat))
5971           (setq bufname-length (if (string-match "%b" mode-string)
5972                                    (- (length
5973                                        (buffer-name
5974                                         (if (eq where 'summary)
5975                                             nil
5976                                           (get-buffer gnus-article-buffer))))
5977                                       2)
5978                                  0))
5979           (setq max-len (max 4 (if gnus-mode-non-string-length
5980                                    (- (window-width)
5981                                       gnus-mode-non-string-length
5982                                       bufname-length)
5983                                  (length mode-string))))
5984           ;; We might have to chop a bit of the string off...
5985           (when (> (length mode-string) max-len)
5986             (setq mode-string
5987                   (concat (truncate-string-to-width mode-string (- max-len 3))
5988                           "...")))
5989           ;; Pad the mode string a bit.
5990           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5991       ;; Update the mode line.
5992       (setq mode-line-buffer-identification
5993             (gnus-mode-line-buffer-identification (list mode-string)))
5994       (set-buffer-modified-p t))))
5995
5996 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5997   "Go through the HEADERS list and add all Xrefs to a hash table.
5998 The resulting hash table is returned, or nil if no Xrefs were found."
5999   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6000          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6001          (xref-hashtb (gnus-make-hashtable))
6002          start group entry number xrefs header)
6003     (while headers
6004       (setq header (pop headers))
6005       (when (and (setq xrefs (mail-header-xref header))
6006                  (not (memq (setq number (mail-header-number header))
6007                             unreads)))
6008         (setq start 0)
6009         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6010           (setq start (match-end 0))
6011           (setq group (if prefix
6012                           (concat prefix (substring xrefs (match-beginning 1)
6013                                                     (match-end 1)))
6014                         (substring xrefs (match-beginning 1) (match-end 1))))
6015           (setq number
6016                 (string-to-number (substring xrefs (match-beginning 2)
6017                                           (match-end 2))))
6018           (if (setq entry (gnus-gethash group xref-hashtb))
6019               (setcdr entry (cons number (cdr entry)))
6020             (gnus-sethash group (cons number nil) xref-hashtb)))))
6021     (and start xref-hashtb)))
6022
6023 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6024   "Look through all the headers and mark the Xrefs as read."
6025   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6026         name info xref-hashtb idlist method nth4)
6027     (save-excursion
6028       (set-buffer gnus-group-buffer)
6029       (when (setq xref-hashtb
6030                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6031         (mapatoms
6032          (lambda (group)
6033            (unless (string= from-newsgroup (setq name (symbol-name group)))
6034              (setq idlist (symbol-value group))
6035              ;; Dead groups are not updated.
6036              (and (prog1
6037                       (setq info (gnus-get-info name))
6038                     (when (stringp (setq nth4 (gnus-info-method info)))
6039                       (setq nth4 (gnus-server-to-method nth4))))
6040                   ;; Only do the xrefs if the group has the same
6041                   ;; select method as the group we have just read.
6042                   (or (gnus-methods-equal-p
6043                        nth4 (gnus-find-method-for-group from-newsgroup))
6044                       virtual
6045                       (equal nth4 (setq method (gnus-find-method-for-group
6046                                                 from-newsgroup)))
6047                       (and (equal (car nth4) (car method))
6048                            (equal (nth 1 nth4) (nth 1 method))))
6049                   gnus-use-cross-reference
6050                   (or (not (eq gnus-use-cross-reference t))
6051                       virtual
6052                       ;; Only do cross-references on subscribed
6053                       ;; groups, if that is what is wanted.
6054                       (<= (gnus-info-level info) gnus-level-subscribed))
6055                   (gnus-group-make-articles-read name idlist))))
6056          xref-hashtb)))))
6057
6058 (defun gnus-compute-read-articles (group articles)
6059   (let* ((entry (gnus-group-entry group))
6060          (info (nth 2 entry))
6061          (active (gnus-active group))
6062          ninfo)
6063     (when entry
6064       ;; First peel off all invalid article numbers.
6065       (when active
6066         (let ((ids articles)
6067               id first)
6068           (while (setq id (pop ids))
6069             (when (and first (> id (cdr active)))
6070               ;; We'll end up in this situation in one particular
6071               ;; obscure situation.  If you re-scan a group and get
6072               ;; a new article that is cross-posted to a different
6073               ;; group that has not been re-scanned, you might get
6074               ;; crossposted article that has a higher number than
6075               ;; Gnus believes possible.  So we re-activate this
6076               ;; group as well.  This might mean doing the
6077               ;; crossposting thingy will *increase* the number
6078               ;; of articles in some groups.  Tsk, tsk.
6079               (setq active (or (gnus-activate-group group) active)))
6080             (when (or (> id (cdr active))
6081                       (< id (car active)))
6082               (setq articles (delq id articles))))))
6083       ;; If the read list is nil, we init it.
6084       (if (and active
6085                (null (gnus-info-read info))
6086                (> (car active) 1))
6087           (setq ninfo (cons 1 (1- (car active))))
6088         (setq ninfo (gnus-info-read info)))
6089       ;; Then we add the read articles to the range.
6090       (gnus-add-to-range
6091        ninfo (setq articles (sort articles '<))))))
6092
6093 (defun gnus-group-make-articles-read (group articles)
6094   "Update the info of GROUP to say that ARTICLES are read."
6095   (let* ((num 0)
6096          (entry (gnus-group-entry group))
6097          (info (nth 2 entry))
6098          (active (gnus-active group))
6099          range)
6100     (when entry
6101       (setq range (gnus-compute-read-articles group articles))
6102       (with-current-buffer gnus-group-buffer
6103         (gnus-undo-register
6104           `(progn
6105              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6106              (gnus-info-set-read ',info ',(gnus-info-read info))
6107              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6108              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6109              (gnus-group-update-group ,group t))))
6110       ;; Add the read articles to the range.
6111       (gnus-info-set-read info range)
6112       (gnus-request-set-mark group (list (list range 'add '(read))))
6113       ;; Then we have to re-compute how many unread
6114       ;; articles there are in this group.
6115       (when active
6116         (cond
6117          ((not range)
6118           (setq num (- (1+ (cdr active)) (car active))))
6119          ((not (listp (cdr range)))
6120           (setq num (- (cdr active) (- (1+ (cdr range))
6121                                        (car range)))))
6122          (t
6123           (while range
6124             (if (numberp (car range))
6125                 (setq num (1+ num))
6126               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6127             (setq range (cdr range)))
6128           (setq num (- (cdr active) num))))
6129         ;; Update the number of unread articles.
6130         (setcar entry num)
6131         ;; Update the group buffer.
6132         (unless (gnus-ephemeral-group-p group)
6133           (gnus-group-update-group group t))))))
6134
6135 (defvar gnus-newsgroup-none-id 0)
6136
6137 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6138   (let ((cur nntp-server-buffer)
6139         (dependencies
6140          (or dependencies
6141              (with-current-buffer gnus-summary-buffer
6142                gnus-newsgroup-dependencies)))
6143         headers id end ref number
6144         (mail-parse-charset gnus-newsgroup-charset)
6145         (mail-parse-ignored-charsets
6146          (save-excursion (condition-case nil
6147                              (set-buffer gnus-summary-buffer)
6148                            (error))
6149                          gnus-newsgroup-ignored-charsets)))
6150     (save-excursion
6151       (set-buffer nntp-server-buffer)
6152       ;; Translate all TAB characters into SPACE characters.
6153       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6154       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6155       (ietf-drums-unfold-fws)
6156       (gnus-run-hooks 'gnus-parse-headers-hook)
6157       (let ((case-fold-search t)
6158             in-reply-to header p lines chars)
6159         (goto-char (point-min))
6160         ;; Search to the beginning of the next header.  Error messages
6161         ;; do not begin with 2 or 3.
6162         (while (re-search-forward "^[23][0-9]+ " nil t)
6163           (setq id nil
6164                 ref nil)
6165           ;; This implementation of this function, with nine
6166           ;; search-forwards instead of the one re-search-forward and
6167           ;; a case (which basically was the old function) is actually
6168           ;; about twice as fast, even though it looks messier.  You
6169           ;; can't have everything, I guess.  Speed and elegance
6170           ;; doesn't always go hand in hand.
6171           (setq
6172            header
6173            (vector
6174             ;; Number.
6175             (prog1
6176                 (setq number (read cur))
6177               (end-of-line)
6178               (setq p (point))
6179               (narrow-to-region (point)
6180                                 (or (and (search-forward "\n.\n" nil t)
6181                                          (- (point) 2))
6182                                     (point))))
6183             ;; Subject.
6184             (progn
6185               (goto-char p)
6186               (if (search-forward "\nsubject:" nil t)
6187                   (funcall gnus-decode-encoded-word-function
6188                            (nnheader-header-value))
6189                 "(none)"))
6190             ;; From.
6191             (progn
6192               (goto-char p)
6193               (if (search-forward "\nfrom:" nil t)
6194                   (funcall gnus-decode-encoded-address-function
6195                            (nnheader-header-value))
6196                 "(nobody)"))
6197             ;; Date.
6198             (progn
6199               (goto-char p)
6200               (if (search-forward "\ndate:" nil t)
6201                   (nnheader-header-value) ""))
6202             ;; Message-ID.
6203             (progn
6204               (goto-char p)
6205               (setq id (if (re-search-forward
6206                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6207                            ;; We do it this way to make sure the Message-ID
6208                            ;; is (somewhat) syntactically valid.
6209                            (buffer-substring (match-beginning 1)
6210                                              (match-end 1))
6211                          ;; If there was no message-id, we just fake one
6212                          ;; to make subsequent routines simpler.
6213                          (nnheader-generate-fake-message-id number))))
6214             ;; References.
6215             (progn
6216               (goto-char p)
6217               (if (search-forward "\nreferences:" nil t)
6218                   (progn
6219                     (setq end (point))
6220                     (prog1
6221                         (nnheader-header-value)
6222                       (setq ref
6223                             (buffer-substring
6224                              (progn
6225                                (end-of-line)
6226                                (search-backward ">" end t)
6227                                (1+ (point)))
6228                              (progn
6229                                (search-backward "<" end t)
6230                                (point))))))
6231                 ;; Get the references from the in-reply-to header if there
6232                 ;; were no references and the in-reply-to header looks
6233                 ;; promising.
6234                 (if (and (search-forward "\nin-reply-to:" nil t)
6235                          (setq in-reply-to (nnheader-header-value))
6236                          (string-match "<[^>]+>" in-reply-to))
6237                     (let (ref2)
6238                       (setq ref (substring in-reply-to (match-beginning 0)
6239                                            (match-end 0)))
6240                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6241                         (setq ref2 (substring in-reply-to (match-beginning 0)
6242                                               (match-end 0)))
6243                         (when (> (length ref2) (length ref))
6244                           (setq ref ref2)))
6245                       ref)
6246                   (setq ref nil))))
6247             ;; Chars.
6248             (progn
6249               (goto-char p)
6250               (if (search-forward "\nchars: " nil t)
6251                   (if (numberp (setq chars (ignore-errors (read cur))))
6252                       chars -1)
6253                 -1))
6254             ;; Lines.
6255             (progn
6256               (goto-char p)
6257               (if (search-forward "\nlines: " nil t)
6258                   (if (numberp (setq lines (ignore-errors (read cur))))
6259                       lines -1)
6260                 -1))
6261             ;; Xref.
6262             (progn
6263               (goto-char p)
6264               (and (search-forward "\nxref:" nil t)
6265                    (nnheader-header-value)))
6266             ;; Extra.
6267             (when gnus-extra-headers
6268               (let ((extra gnus-extra-headers)
6269                     out)
6270                 (while extra
6271                   (goto-char p)
6272                   (when (search-forward
6273                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6274                     (push (cons (car extra) (nnheader-header-value))
6275                           out))
6276                   (pop extra))
6277                 out))))
6278           (when (equal id ref)
6279             (setq ref nil))
6280
6281           (when gnus-alter-header-function
6282             (funcall gnus-alter-header-function header)
6283             (setq id (mail-header-id header)
6284                   ref (gnus-parent-id (mail-header-references header))))
6285
6286           (when (setq header
6287                       (gnus-dependencies-add-header
6288                        header dependencies force-new))
6289             (push header headers))
6290           (goto-char (point-max))
6291           (widen))
6292         (nreverse headers)))))
6293
6294 ;; Goes through the xover lines and returns a list of vectors
6295 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6296                                                   force-new dependencies
6297                                                   group also-fetch-heads)
6298   "Parse the news overview data in the server buffer.
6299 Return a list of headers that match SEQUENCE (see
6300 `nntp-retrieve-headers')."
6301   ;; Get the Xref when the users reads the articles since most/some
6302   ;; NNTP servers do not include Xrefs when using XOVER.
6303   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6304   (let ((mail-parse-charset gnus-newsgroup-charset)
6305         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6306         (cur nntp-server-buffer)
6307         (dependencies (or dependencies gnus-newsgroup-dependencies))
6308         (allp (cond
6309                ((eq gnus-read-all-available-headers t)
6310                 t)
6311                ((and (stringp gnus-read-all-available-headers)
6312                      group)
6313                 (string-match gnus-read-all-available-headers group))
6314                (t
6315                 nil)))
6316         number headers header)
6317     (save-excursion
6318       (set-buffer nntp-server-buffer)
6319       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6320       ;; Allow the user to mangle the headers before parsing them.
6321       (gnus-run-hooks 'gnus-parse-headers-hook)
6322       (goto-char (point-min))
6323       (gnus-parse-without-error
6324         (while (and (or sequence allp)
6325                     (not (eobp)))
6326           (setq number (read cur))
6327           (when (not allp)
6328             (while (and sequence
6329                         (< (car sequence) number))
6330               (setq sequence (cdr sequence))))
6331           (when (and (or allp
6332                          (and sequence
6333                               (eq number (car sequence))))
6334                      (progn
6335                        (setq sequence (cdr sequence))
6336                        (setq header (inline
6337                                       (gnus-nov-parse-line
6338                                        number dependencies force-new)))))
6339             (push header headers))
6340           (forward-line 1)))
6341       ;; A common bug in inn is that if you have posted an article and
6342       ;; then retrieves the active file, it will answer correctly --
6343       ;; the new article is included.  However, a NOV entry for the
6344       ;; article may not have been generated yet, so this may fail.
6345       ;; We work around this problem by retrieving the last few
6346       ;; headers using HEAD.
6347       (if (or (not also-fetch-heads)
6348               (not sequence))
6349           ;; We (probably) got all the headers.
6350           (nreverse headers)
6351         (let ((gnus-nov-is-evil t))
6352           (nconc
6353            (nreverse headers)
6354            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6355              (gnus-get-newsgroup-headers))))))))
6356
6357 (defun gnus-article-get-xrefs ()
6358   "Fill in the Xref value in `gnus-current-headers', if necessary.
6359 This is meant to be called in `gnus-article-internal-prepare-hook'."
6360   (let ((headers (with-current-buffer gnus-summary-buffer
6361                    gnus-current-headers)))
6362     (or (not gnus-use-cross-reference)
6363         (not headers)
6364         (and (mail-header-xref headers)
6365              (not (string= (mail-header-xref headers) "")))
6366         (let ((case-fold-search t)
6367               xref)
6368           (save-restriction
6369             (nnheader-narrow-to-headers)
6370             (goto-char (point-min))
6371             (when (or (and (not (eobp))
6372                            (eq (downcase (char-after)) ?x)
6373                            (looking-at "Xref:"))
6374                       (search-forward "\nXref:" nil t))
6375               (goto-char (1+ (match-end 0)))
6376               (setq xref (buffer-substring (point) (point-at-eol)))
6377               (mail-header-set-xref headers xref)))))))
6378
6379 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6380   "Find article ID and insert the summary line for that article.
6381 OLD-HEADER can either be a header or a line number to insert
6382 the subject line on."
6383   (let* ((line (and (numberp old-header) old-header))
6384          (old-header (and (vectorp old-header) old-header))
6385          (header (cond ((and old-header use-old-header)
6386                         old-header)
6387                        ((and (numberp id)
6388                              (gnus-number-to-header id))
6389                         (gnus-number-to-header id))
6390                        (t
6391                         (gnus-read-header id))))
6392          (number (and (numberp id) id))
6393          d)
6394     (when header
6395       ;; Rebuild the thread that this article is part of and go to the
6396       ;; article we have fetched.
6397       (when (and (not gnus-show-threads)
6398                  old-header)
6399         (when (and number
6400                    (setq d (gnus-data-find (mail-header-number old-header))))
6401           (goto-char (gnus-data-pos d))
6402           (gnus-data-remove
6403            number
6404            (- (point-at-bol)
6405               (prog1
6406                   (1+ (point-at-eol))
6407                 (gnus-delete-line))))))
6408       ;; Remove list identifiers from subject.
6409       (when gnus-list-identifiers
6410         (let ((gnus-newsgroup-headers (list header)))
6411           (gnus-summary-remove-list-identifiers)))
6412       (when old-header
6413         (mail-header-set-number header (mail-header-number old-header)))
6414       (setq gnus-newsgroup-sparse
6415             (delq (setq number (mail-header-number header))
6416                   gnus-newsgroup-sparse))
6417       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6418       (push number gnus-newsgroup-limit)
6419       (gnus-rebuild-thread (mail-header-id header) line)
6420       (gnus-summary-goto-subject number nil t))
6421     (when (and (numberp number)
6422                (> number 0))
6423       ;; We have to update the boundaries even if we can't fetch the
6424       ;; article if ID is a number -- so that the next `P' or `N'
6425       ;; command will fetch the previous (or next) article even
6426       ;; if the one we tried to fetch this time has been canceled.
6427       (when (> number gnus-newsgroup-end)
6428         (setq gnus-newsgroup-end number))
6429       (when (< number gnus-newsgroup-begin)
6430         (setq gnus-newsgroup-begin number))
6431       (setq gnus-newsgroup-unselected
6432             (delq number gnus-newsgroup-unselected)))
6433     ;; Report back a success?
6434     (and header (mail-header-number header))))
6435
6436 ;;; Process/prefix in the summary buffer
6437
6438 (defun gnus-summary-work-articles (n)
6439   "Return a list of articles to be worked upon.
6440 The prefix argument, the list of process marked articles, and the
6441 current article will be taken into consideration."
6442   (save-excursion
6443     (set-buffer gnus-summary-buffer)
6444     (cond
6445      (n
6446       ;; A numerical prefix has been given.
6447       (setq n (prefix-numeric-value n))
6448       (let ((backward (< n 0))
6449             (n (abs (prefix-numeric-value n)))
6450             articles article)
6451         (save-excursion
6452           (while
6453               (and (> n 0)
6454                    (push (setq article (gnus-summary-article-number))
6455                          articles)
6456                    (if backward
6457                        (gnus-summary-find-prev nil article)
6458                      (gnus-summary-find-next nil article)))
6459             (decf n)))
6460         (nreverse articles)))
6461      ((and (gnus-region-active-p) (mark))
6462       (message "region active")
6463       ;; Work on the region between point and mark.
6464       (let ((max (max (point) (mark)))
6465             articles article)
6466         (save-excursion
6467           (goto-char (min (point) (mark)))
6468           (while
6469               (and
6470                (push (setq article (gnus-summary-article-number)) articles)
6471                (gnus-summary-find-next nil article)
6472                (< (point) max)))
6473           (nreverse articles))))
6474      (gnus-newsgroup-processable
6475       ;; There are process-marked articles present.
6476       ;; Save current state.
6477       (gnus-summary-save-process-mark)
6478       ;; Return the list.
6479       (reverse gnus-newsgroup-processable))
6480      (t
6481       ;; Just return the current article.
6482       (list (gnus-summary-article-number))))))
6483
6484 (defmacro gnus-summary-iterate (arg &rest forms)
6485   "Iterate over the process/prefixed articles and do FORMS.
6486 ARG is the interactive prefix given to the command.  FORMS will be
6487 executed with point over the summary line of the articles."
6488   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6489     `(let ((,articles (gnus-summary-work-articles ,arg)))
6490        (while ,articles
6491          (gnus-summary-goto-subject (car ,articles))
6492          ,@forms
6493          (pop ,articles)))))
6494
6495 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6496 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6497
6498 (defun gnus-summary-save-process-mark ()
6499   "Push the current set of process marked articles on the stack."
6500   (interactive)
6501   (push (copy-sequence gnus-newsgroup-processable)
6502         gnus-newsgroup-process-stack))
6503
6504 (defun gnus-summary-kill-process-mark ()
6505   "Push the current set of process marked articles on the stack and unmark."
6506   (interactive)
6507   (gnus-summary-save-process-mark)
6508   (gnus-summary-unmark-all-processable))
6509
6510 (defun gnus-summary-yank-process-mark ()
6511   "Pop the last process mark state off the stack and restore it."
6512   (interactive)
6513   (unless gnus-newsgroup-process-stack
6514     (error "Empty mark stack"))
6515   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6516
6517 (defun gnus-summary-process-mark-set (set)
6518   "Make SET into the current process marked articles."
6519   (gnus-summary-unmark-all-processable)
6520   (mapc 'gnus-summary-set-process-mark set))
6521
6522 ;;; Searching and stuff
6523
6524 (defun gnus-summary-search-group (&optional backward use-level)
6525   "Search for next unread newsgroup.
6526 If optional argument BACKWARD is non-nil, search backward instead."
6527   (save-excursion
6528     (set-buffer gnus-group-buffer)
6529     (when (gnus-group-search-forward
6530            backward nil (if use-level (gnus-group-group-level) nil))
6531       (gnus-group-group-name))))
6532
6533 (defun gnus-summary-best-group (&optional exclude-group)
6534   "Find the name of the best unread group.
6535 If EXCLUDE-GROUP, do not go to this group."
6536   (with-current-buffer gnus-group-buffer
6537     (save-excursion
6538       (gnus-group-best-unread-group exclude-group))))
6539
6540 (defun gnus-summary-find-next (&optional unread article backward)
6541   (if backward
6542       (gnus-summary-find-prev unread article)
6543     (let* ((dummy (gnus-summary-article-intangible-p))
6544            (article (or article (gnus-summary-article-number)))
6545            (data (gnus-data-find-list article))
6546            result)
6547       (when (and (not dummy)
6548                  (or (not gnus-summary-check-current)
6549                      (not unread)
6550                      (not (gnus-data-unread-p (car data)))))
6551         (setq data (cdr data)))
6552       (when (setq result
6553                   (if unread
6554                       (progn
6555                         (while data
6556                           (unless (memq (gnus-data-number (car data))
6557                                         (cond
6558                                          ((eq gnus-auto-goto-ignores
6559                                               'always-undownloaded)
6560                                           gnus-newsgroup-undownloaded)
6561                                          (gnus-plugged
6562                                           nil)
6563                                          ((eq gnus-auto-goto-ignores
6564                                               'unfetched)
6565                                           gnus-newsgroup-unfetched)
6566                                          ((eq gnus-auto-goto-ignores
6567                                               'undownloaded)
6568                                           gnus-newsgroup-undownloaded)))
6569                             (when (gnus-data-unread-p (car data))
6570                               (setq result (car data)
6571                                     data nil)))
6572                           (setq data (cdr data)))
6573                         result)
6574                     (car data)))
6575         (goto-char (gnus-data-pos result))
6576         (gnus-data-number result)))))
6577
6578 (defun gnus-summary-find-prev (&optional unread article)
6579   (let* ((eobp (eobp))
6580          (article (or article (gnus-summary-article-number)))
6581          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6582          result)
6583     (when (and (not eobp)
6584                (or (not gnus-summary-check-current)
6585                    (not unread)
6586                    (not (gnus-data-unread-p (car data)))))
6587       (setq data (cdr data)))
6588     (when (setq result
6589                 (if unread
6590                     (progn
6591                       (while data
6592                         (unless (memq (gnus-data-number (car data))
6593                                       (cond
6594                                        ((eq gnus-auto-goto-ignores
6595                                             'always-undownloaded)
6596                                         gnus-newsgroup-undownloaded)
6597                                        (gnus-plugged
6598                                         nil)
6599                                        ((eq gnus-auto-goto-ignores
6600                                             'unfetched)
6601                                         gnus-newsgroup-unfetched)
6602                                        ((eq gnus-auto-goto-ignores
6603                                             'undownloaded)
6604                                         gnus-newsgroup-undownloaded)))
6605                           (when (gnus-data-unread-p (car data))
6606                             (setq result (car data)
6607                                   data nil)))
6608                         (setq data (cdr data)))
6609                       result)
6610                   (car data)))
6611       (goto-char (gnus-data-pos result))
6612       (gnus-data-number result))))
6613
6614 (defun gnus-summary-find-subject (subject &optional unread backward article)
6615   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6616          (article (or article (gnus-summary-article-number)))
6617          (articles (gnus-data-list backward))
6618          (arts (gnus-data-find-list article articles))
6619          result)
6620     (when (or (not gnus-summary-check-current)
6621               (not unread)
6622               (not (gnus-data-unread-p (car arts))))
6623       (setq arts (cdr arts)))
6624     (while arts
6625       (and (or (not unread)
6626                (gnus-data-unread-p (car arts)))
6627            (vectorp (gnus-data-header (car arts)))
6628            (gnus-subject-equal
6629             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6630            (setq result (car arts)
6631                  arts nil))
6632       (setq arts (cdr arts)))
6633     (and result
6634          (goto-char (gnus-data-pos result))
6635          (gnus-data-number result))))
6636
6637 (defun gnus-summary-search-forward (&optional unread subject backward)
6638   "Search forward for an article.
6639 If UNREAD, look for unread articles.  If SUBJECT, look for
6640 articles with that subject.  If BACKWARD, search backward instead."
6641   (cond (subject (gnus-summary-find-subject subject unread backward))
6642         (backward (gnus-summary-find-prev unread))
6643         (t (gnus-summary-find-next unread))))
6644
6645 (defun gnus-recenter (&optional n)
6646   "Center point in window and redisplay frame.
6647 Also do horizontal recentering."
6648   (interactive "P")
6649   (when (and gnus-auto-center-summary
6650              (not (eq gnus-auto-center-summary 'vertical)))
6651     (gnus-horizontal-recenter))
6652   (recenter n))
6653
6654 (defun gnus-summary-recenter ()
6655   "Center point in the summary window.
6656 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6657 displayed, no centering will be performed."
6658   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6659   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6660   (interactive)
6661   ;; The user has to want it.
6662   (when gnus-auto-center-summary
6663     (let* ((top (cond ((< (window-height) 4) 0)
6664                       ((< (window-height) 7) 1)
6665                       (t (if (numberp gnus-auto-center-summary)
6666                              gnus-auto-center-summary
6667                            (/ (1- (window-height)) 2)))))
6668            (height (1- (window-height)))
6669            (bottom (save-excursion (goto-char (point-max))
6670                                    (forward-line (- height))
6671                                    (point)))
6672            (window (get-buffer-window (current-buffer))))
6673       (when (get-buffer-window gnus-article-buffer)
6674         ;; Only do recentering when the article buffer is displayed,
6675         ;; Set the window start to either `bottom', which is the biggest
6676         ;; possible valid number, or the second line from the top,
6677         ;; whichever is the least.
6678         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6679           (if (> bottom top-pos)
6680               ;; Keep the second line from the top visible
6681               (set-window-start window top-pos)
6682             ;; Try to keep the bottom line visible; if it's partially
6683             ;; obscured, either scroll one more line to make it fully
6684             ;; visible, or revert to using TOP-POS.
6685             (save-excursion
6686               (goto-char (point-max))
6687               (forward-line -1)
6688               (let ((last-line-start (point)))
6689                 (goto-char bottom)
6690                 (set-window-start window (point) t)
6691                 (when (not (pos-visible-in-window-p last-line-start window))
6692                   (forward-line 1)
6693                   (set-window-start window (min (point) top-pos) t)))))))
6694       ;; Do horizontal recentering while we're at it.
6695       (when (and (get-buffer-window (current-buffer) t)
6696                  (not (eq gnus-auto-center-summary 'vertical)))
6697         (let ((selected (selected-window)))
6698           (select-window (get-buffer-window (current-buffer) t))
6699           (gnus-summary-position-point)
6700           (gnus-horizontal-recenter)
6701           (select-window selected))))))
6702
6703 (defun gnus-summary-jump-to-group (newsgroup)
6704   "Move point to NEWSGROUP in group mode buffer."
6705   ;; Keep update point of group mode buffer if visible.
6706   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6707       (save-window-excursion
6708         ;; Take care of tree window mode.
6709         (when (get-buffer-window gnus-group-buffer)
6710           (pop-to-buffer gnus-group-buffer))
6711         (gnus-group-jump-to-group newsgroup))
6712     (save-excursion
6713       ;; Take care of tree window mode.
6714       (if (get-buffer-window gnus-group-buffer)
6715           (pop-to-buffer gnus-group-buffer)
6716         (set-buffer gnus-group-buffer))
6717       (gnus-group-jump-to-group newsgroup))))
6718
6719 ;; This function returns a list of article numbers based on the
6720 ;; difference between the ranges of read articles in this group and
6721 ;; the range of active articles.
6722 (defun gnus-list-of-unread-articles (group)
6723   (let* ((read (gnus-info-read (gnus-get-info group)))
6724          (active (or (gnus-active group) (gnus-activate-group group)))
6725          (last (or (cdr active)
6726                    (error "Group %s couldn't be activated " group)))
6727          (bottom (if gnus-newsgroup-maximum-articles
6728                      (max (car active)
6729                           (- last gnus-newsgroup-maximum-articles -1))
6730                    (car active)))
6731          first nlast unread)
6732     ;; If none are read, then all are unread.
6733     (if (not read)
6734         (setq first bottom)
6735       ;; If the range of read articles is a single range, then the
6736       ;; first unread article is the article after the last read
6737       ;; article.  Sounds logical, doesn't it?
6738       (if (and (not (listp (cdr read)))
6739                (or (< (car read) bottom)
6740                    (progn (setq read (list read))
6741                           nil)))
6742           (setq first (max bottom (1+ (cdr read))))
6743         ;; `read' is a list of ranges.
6744         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6745                                   (caar read)))
6746                   1)
6747           (setq first bottom))
6748         (while read
6749           (when first
6750             (while (< first nlast)
6751               (setq unread (cons first unread)
6752                     first (1+ first))))
6753           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6754           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6755           (setq read (cdr read)))))
6756     ;; And add the last unread articles.
6757     (while (<= first last)
6758       (setq unread (cons first unread)
6759             first (1+ first)))
6760     ;; Return the list of unread articles.
6761     (delq 0 (nreverse unread))))
6762
6763 (defun gnus-list-of-read-articles (group)
6764   "Return a list of unread, unticked and non-dormant articles."
6765   (let* ((info (gnus-get-info group))
6766          (marked (gnus-info-marks info))
6767          (active (gnus-active group)))
6768     (and info active
6769          (gnus-list-range-difference
6770           (gnus-list-range-difference
6771            (gnus-sorted-complement
6772             (gnus-uncompress-range
6773              (if gnus-newsgroup-maximum-articles
6774                  (cons (max (car active)
6775                             (- (cdr active)
6776                                gnus-newsgroup-maximum-articles
6777                                -1))
6778                        (cdr active))
6779                active))
6780             (gnus-list-of-unread-articles group))
6781            (cdr (assq 'dormant marked)))
6782           (cdr (assq 'tick marked))))))
6783
6784 ;; This function returns a sequence of article numbers based on the
6785 ;; difference between the ranges of read articles in this group and
6786 ;; the range of active articles.
6787 (defun gnus-sequence-of-unread-articles (group)
6788   (let* ((read (gnus-info-read (gnus-get-info group)))
6789          (active (or (gnus-active group) (gnus-activate-group group)))
6790          (last (cdr active))
6791          (bottom (if gnus-newsgroup-maximum-articles
6792                      (max (car active)
6793                           (- last gnus-newsgroup-maximum-articles -1))
6794                    (car active)))
6795          first nlast unread)
6796     ;; If none are read, then all are unread.
6797     (if (not read)
6798         (setq first bottom)
6799       ;; If the range of read articles is a single range, then the
6800       ;; first unread article is the article after the last read
6801       ;; article.  Sounds logical, doesn't it?
6802       (if (and (not (listp (cdr read)))
6803                (or (< (car read) bottom)
6804                    (progn (setq read (list read))
6805                           nil)))
6806           (setq first (max bottom (1+ (cdr read))))
6807         ;; `read' is a list of ranges.
6808         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6809                                   (caar read)))
6810                   1)
6811           (setq first bottom))
6812         (while read
6813           (when first
6814             (push (cons first nlast) unread))
6815           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6816           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6817           (setq read (cdr read)))))
6818     ;; And add the last unread articles.
6819     (cond ((not (and first last))
6820            nil)
6821           ((< first last)
6822            (push (cons first last) unread))
6823           ((= first last)
6824            (push first unread)))
6825     ;; Return the sequence of unread articles.
6826     (delq 0 (nreverse unread))))
6827
6828 ;; Various summary commands
6829
6830 (defun gnus-summary-select-article-buffer ()
6831   "Reconfigure windows to show article buffer."
6832   (interactive)
6833   (if (not (gnus-buffer-live-p gnus-article-buffer))
6834       (error "There is no article buffer for this summary buffer")
6835     (gnus-configure-windows 'article)
6836     (select-window (get-buffer-window gnus-article-buffer))))
6837
6838 (defun gnus-summary-universal-argument (arg)
6839   "Perform any operation on all articles that are process/prefixed."
6840   (interactive "P")
6841   (let ((articles (gnus-summary-work-articles arg))
6842         func article)
6843     (if (eq
6844          (setq
6845           func
6846           (key-binding
6847            (read-key-sequence
6848             (substitute-command-keys
6849              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6850          'undefined)
6851         (gnus-error 1 "Undefined key")
6852       (save-excursion
6853         (while articles
6854           (gnus-summary-goto-subject (setq article (pop articles)))
6855           (let (gnus-newsgroup-processable)
6856             (command-execute func))
6857           (gnus-summary-remove-process-mark article)))))
6858   (gnus-summary-position-point))
6859
6860 (defun gnus-summary-toggle-truncation (&optional arg)
6861   "Toggle truncation of summary lines.
6862 With ARG, turn line truncation on if ARG is positive."
6863   (interactive "P")
6864   (setq truncate-lines
6865         (if (null arg) (not truncate-lines)
6866           (> (prefix-numeric-value arg) 0)))
6867   (redraw-display))
6868
6869 (defun gnus-summary-find-for-reselect ()
6870   "Return the number of an article to stay on across a reselect.
6871 The current article is considered, then following articles, then previous
6872 articles.  An article is sought which is not cancelled and isn't a temporary
6873 insertion from another group.  If there's no such then return a dummy 0."
6874   (let (found)
6875     (dolist (rev '(nil t))
6876       (unless found      ; don't demand the reverse list if we don't need it
6877         (let ((data (gnus-data-find-list
6878                      (gnus-summary-article-number) (gnus-data-list rev))))
6879           (while (and data (not found))
6880             (if (and (< 0 (gnus-data-number (car data)))
6881                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6882                 (setq found (gnus-data-number (car data))))
6883             (setq data (cdr data))))))
6884     (or found 0)))
6885
6886 (defun gnus-summary-reselect-current-group (&optional all rescan)
6887   "Exit and then reselect the current newsgroup.
6888 The prefix argument ALL means to select all articles."
6889   (interactive "P")
6890   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6891     (error "Ephemeral groups can't be reselected"))
6892   (let ((current-subject (gnus-summary-find-for-reselect))
6893         (group gnus-newsgroup-name))
6894     (setq gnus-newsgroup-begin nil)
6895     (gnus-summary-exit nil 'leave-hidden)
6896     ;; We have to adjust the point of group mode buffer because
6897     ;; point was moved to the next unread newsgroup by exiting.
6898     (gnus-summary-jump-to-group group)
6899     (when rescan
6900       (save-excursion
6901         (gnus-group-get-new-news-this-group 1)))
6902     (gnus-group-read-group all t)
6903     (gnus-summary-goto-subject current-subject nil t)))
6904
6905 (defun gnus-summary-rescan-group (&optional all)
6906   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6907   (interactive "P")
6908   (gnus-summary-reselect-current-group all t))
6909
6910 (defun gnus-summary-update-info (&optional non-destructive)
6911   (save-excursion
6912     (let ((group gnus-newsgroup-name))
6913       (when group
6914         (when gnus-newsgroup-kill-headers
6915           (setq gnus-newsgroup-killed
6916                 (gnus-compress-sequence
6917                  (gnus-sorted-union
6918                   (gnus-list-range-intersection
6919                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6920                   gnus-newsgroup-unreads)
6921                  t)))
6922         (unless (listp (cdr gnus-newsgroup-killed))
6923           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6924         (let ((headers gnus-newsgroup-headers))
6925           ;; Set the new ranges of read articles.
6926           (with-current-buffer gnus-group-buffer
6927             (gnus-undo-force-boundary))
6928           (gnus-update-read-articles
6929            group (gnus-sorted-union
6930                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6931           ;; Set the current article marks.
6932           (let ((gnus-newsgroup-scored
6933                  (if (and (not gnus-save-score)
6934                           (not non-destructive))
6935                      nil
6936                    gnus-newsgroup-scored)))
6937             (save-excursion
6938               (gnus-update-marks)))
6939           ;; Do the cross-ref thing.
6940           (when gnus-use-cross-reference
6941             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6942           ;; Do not switch windows but change the buffer to work.
6943           (set-buffer gnus-group-buffer)
6944           (unless (gnus-ephemeral-group-p group)
6945             (gnus-group-update-group group)))))))
6946
6947 (defun gnus-summary-save-newsrc (&optional force)
6948   "Save the current number of read/marked articles in the dribble buffer.
6949 The dribble buffer will then be saved.
6950 If FORCE (the prefix), also save the .newsrc file(s)."
6951   (interactive "P")
6952   (gnus-summary-update-info t)
6953   (if force
6954       (gnus-save-newsrc-file)
6955     (gnus-dribble-save)))
6956
6957 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
6958
6959 (defun gnus-summary-exit (&optional temporary leave-hidden)
6960   "Exit reading current newsgroup, and then return to group selection mode.
6961 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6962   (interactive)
6963   (gnus-set-global-variables)
6964   (when (gnus-buffer-live-p gnus-article-buffer)
6965     (save-excursion
6966       (set-buffer gnus-article-buffer)
6967       (mm-destroy-parts gnus-article-mime-handles)
6968       ;; Set it to nil for safety reason.
6969       (setq gnus-article-mime-handle-alist nil)
6970       (setq gnus-article-mime-handles nil)))
6971   (gnus-kill-save-kill-buffer)
6972   (gnus-async-halt-prefetch)
6973   (let* ((group gnus-newsgroup-name)
6974          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6975          (gnus-group-is-exiting-p t)
6976          (mode major-mode)
6977          (group-point nil)
6978          (buf (current-buffer)))
6979     (unless quit-config
6980       ;; Do adaptive scoring, and possibly save score files.
6981       (when gnus-newsgroup-adaptive
6982         (gnus-score-adaptive))
6983       (when gnus-use-scoring
6984         (gnus-score-save)))
6985     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6986     ;; If we have several article buffers, we kill them at exit.
6987     (unless gnus-single-article-buffer
6988       (when (gnus-buffer-live-p gnus-article-buffer)
6989         (with-current-buffer gnus-article-buffer
6990           ;; Don't kill sticky article buffers
6991           (unless (eq major-mode 'gnus-sticky-article-mode)
6992             (gnus-kill-buffer gnus-article-buffer)
6993             (setq gnus-article-current nil))))
6994       (gnus-kill-buffer gnus-original-article-buffer))
6995     (when gnus-use-cache
6996       (gnus-cache-possibly-remove-articles)
6997       (gnus-cache-save-buffers))
6998     (gnus-async-prefetch-remove-group group)
6999     (when gnus-suppress-duplicates
7000       (gnus-dup-enter-articles))
7001     (when gnus-use-trees
7002       (gnus-tree-close group))
7003     (when gnus-use-cache
7004       (gnus-cache-write-active))
7005     ;; Remove entries for this group.
7006     (nnmail-purge-split-history (gnus-group-real-name group))
7007     ;; Make all changes in this group permanent.
7008     (unless quit-config
7009       (gnus-run-hooks 'gnus-exit-group-hook)
7010       (gnus-summary-update-info))
7011     (gnus-close-group group)
7012     ;; Make sure where we were, and go to next newsgroup.
7013     (set-buffer gnus-group-buffer)
7014     (unless quit-config
7015       (gnus-group-jump-to-group group))
7016     (gnus-run-hooks 'gnus-summary-exit-hook)
7017     (unless (or quit-config
7018                 (not gnus-summary-next-group-on-exit)
7019                 ;; If this group has disappeared from the summary
7020                 ;; buffer, don't skip forwards.
7021                 (not (string= group (gnus-group-group-name))))
7022       (gnus-group-next-unread-group 1))
7023     (setq group-point (point))
7024     (if temporary
7025         nil                             ;Nothing to do.
7026       (set-buffer buf)
7027       (if (not gnus-kill-summary-on-exit)
7028           (progn
7029             (gnus-deaden-summary)
7030             (setq mode nil))
7031         ;; We set all buffer-local variables to nil.  It is unclear why
7032         ;; this is needed, but if we don't, buffer-local variables are
7033         ;; not garbage-collected, it seems.  This would the lead to en
7034         ;; ever-growing Emacs.
7035         (gnus-summary-clear-local-variables)
7036         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7037           (gnus-summary-clear-local-variables))
7038         (when (get-buffer gnus-article-buffer)
7039           (bury-buffer gnus-article-buffer))
7040         ;; Return to group mode buffer.
7041         (when (eq mode 'gnus-summary-mode)
7042           (gnus-kill-buffer buf)))
7043       (setq gnus-current-select-method gnus-select-method)
7044       (set-buffer gnus-group-buffer)
7045       (if quit-config
7046           (gnus-handle-ephemeral-exit quit-config)
7047         (goto-char group-point)
7048         ;; If gnus-group-buffer is already displayed, make sure we also move
7049         ;; the cursor in the window that displays it.
7050         (let ((win (get-buffer-window (current-buffer) 0)))
7051           (if win (set-window-point win (point))))
7052         (unless leave-hidden
7053           (gnus-configure-windows 'group 'force)))
7054       ;; Clear the current group name.
7055       (unless quit-config
7056         (setq gnus-newsgroup-name nil)))))
7057
7058 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7059 (defun gnus-summary-exit-no-update (&optional no-questions)
7060   "Quit reading current newsgroup without updating read article info."
7061   (interactive)
7062   (let* ((group gnus-newsgroup-name)
7063          (gnus-group-is-exiting-p t)
7064          (gnus-group-is-exiting-without-update-p t)
7065          (quit-config (gnus-group-quit-config group)))
7066     (when (or no-questions
7067               gnus-expert-user
7068               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7069       (gnus-async-halt-prefetch)
7070       (run-hooks 'gnus-summary-prepare-exit-hook)
7071       (when (gnus-buffer-live-p gnus-article-buffer)
7072         (save-excursion
7073           (set-buffer gnus-article-buffer)
7074           (mm-destroy-parts gnus-article-mime-handles)
7075           ;; Set it to nil for safety reason.
7076           (setq gnus-article-mime-handle-alist nil)
7077           (setq gnus-article-mime-handles nil)))
7078       ;; If we have several article buffers, we kill them at exit.
7079       (unless gnus-single-article-buffer
7080         (gnus-kill-buffer gnus-article-buffer)
7081         (gnus-kill-buffer gnus-original-article-buffer)
7082         (setq gnus-article-current nil))
7083       (if (not gnus-kill-summary-on-exit)
7084           (gnus-deaden-summary)
7085         (gnus-close-group group)
7086         (gnus-summary-clear-local-variables)
7087         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7088           (gnus-summary-clear-local-variables))
7089         (gnus-kill-buffer gnus-summary-buffer))
7090       (unless gnus-single-article-buffer
7091         (setq gnus-article-current nil))
7092       (when gnus-use-trees
7093         (gnus-tree-close group))
7094       (gnus-async-prefetch-remove-group group)
7095       (when (get-buffer gnus-article-buffer)
7096         (bury-buffer gnus-article-buffer))
7097       ;; Return to the group buffer.
7098       (gnus-configure-windows 'group 'force)
7099       ;; Clear the current group name.
7100       (setq gnus-newsgroup-name nil)
7101       (unless (gnus-ephemeral-group-p group)
7102         (gnus-group-update-group group))
7103       (when (equal (gnus-group-group-name) group)
7104         (gnus-group-next-unread-group 1))
7105       (when quit-config
7106         (gnus-handle-ephemeral-exit quit-config)))))
7107
7108 (defun gnus-handle-ephemeral-exit (quit-config)
7109   "Handle movement when leaving an ephemeral group.
7110 The state which existed when entering the ephemeral is reset."
7111   (if (not (buffer-name (car quit-config)))
7112       (gnus-configure-windows 'group 'force)
7113     (set-buffer (car quit-config))
7114     (cond ((eq major-mode 'gnus-summary-mode)
7115            (gnus-set-global-variables))
7116           ((eq major-mode 'gnus-article-mode)
7117            (save-excursion
7118              ;; The `gnus-summary-buffer' variable may point
7119              ;; to the old summary buffer when using a single
7120              ;; article buffer.
7121              (unless (gnus-buffer-live-p gnus-summary-buffer)
7122                (set-buffer gnus-group-buffer))
7123              (set-buffer gnus-summary-buffer)
7124              (gnus-set-global-variables))))
7125     (if (or (eq (cdr quit-config) 'article)
7126             (eq (cdr quit-config) 'pick))
7127         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7128             (gnus-configure-windows 'pick 'force)
7129           (gnus-configure-windows (cdr quit-config) 'force))
7130       (gnus-configure-windows (cdr quit-config) 'force))
7131     (when (eq major-mode 'gnus-summary-mode)
7132       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7133                                                      next-unread-noselect))
7134           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7135                                   'next-noselect)
7136                               (gnus-summary-next-subject 1 nil t))
7137                              ((eq gnus-auto-select-on-ephemeral-exit
7138                                   'next-unread-noselect)
7139                               (gnus-summary-next-subject 1 t t))))
7140             ;; Hide the article buffer which displays the article different
7141             ;; from the one that the cursor points to in the summary buffer.
7142             (gnus-configure-windows 'summary 'force))
7143         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7144                (gnus-summary-next-subject 1))
7145               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7146                (gnus-summary-next-subject 1 t))))
7147       (gnus-summary-recenter)
7148       (gnus-summary-position-point))))
7149
7150 ;;; Dead summaries.
7151
7152 (defvar gnus-dead-summary-mode-map nil)
7153
7154 (unless gnus-dead-summary-mode-map
7155   (setq gnus-dead-summary-mode-map (make-keymap))
7156   (suppress-keymap gnus-dead-summary-mode-map)
7157   (substitute-key-definition
7158    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7159   (dolist (key '("\C-d" "\r" "\177" [delete]))
7160     (define-key gnus-dead-summary-mode-map
7161       key 'gnus-summary-wake-up-the-dead))
7162   (dolist (key '("q" "Q"))
7163     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7164
7165 (defvar gnus-dead-summary-mode nil
7166   "Minor mode for Gnus summary buffers.")
7167
7168 (defun gnus-dead-summary-mode (&optional arg)
7169   "Minor mode for Gnus summary buffers."
7170   (interactive "P")
7171   (when (eq major-mode 'gnus-summary-mode)
7172     (make-local-variable 'gnus-dead-summary-mode)
7173     (setq gnus-dead-summary-mode
7174           (if (null arg) (not gnus-dead-summary-mode)
7175             (> (prefix-numeric-value arg) 0)))
7176     (when gnus-dead-summary-mode
7177       (add-minor-mode
7178        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7179
7180 (defun gnus-deaden-summary ()
7181   "Make the current summary buffer into a dead summary buffer."
7182   ;; Kill any previous dead summary buffer.
7183   (when (and gnus-dead-summary
7184              (buffer-name gnus-dead-summary))
7185     (with-current-buffer gnus-dead-summary
7186       (when gnus-dead-summary-mode
7187         (kill-buffer (current-buffer)))))
7188   ;; Make this the current dead summary.
7189   (setq gnus-dead-summary (current-buffer))
7190   (gnus-dead-summary-mode 1)
7191   (let ((name (buffer-name)))
7192     (when (string-match "Summary" name)
7193       (rename-buffer
7194        (concat (substring name 0 (match-beginning 0)) "Dead "
7195                (substring name (match-beginning 0)))
7196        t)
7197       (bury-buffer))))
7198
7199 (defun gnus-kill-or-deaden-summary (buffer)
7200   "Kill or deaden the summary BUFFER."
7201   (save-excursion
7202     (when (and (buffer-name buffer)
7203                (not gnus-single-article-buffer))
7204       (with-current-buffer buffer
7205         (gnus-kill-buffer gnus-article-buffer)
7206         (gnus-kill-buffer gnus-original-article-buffer)))
7207     (cond
7208      ;; Kill the buffer.
7209      (gnus-kill-summary-on-exit
7210       (when (and gnus-use-trees
7211                  (gnus-buffer-exists-p buffer))
7212         (save-excursion
7213           (set-buffer buffer)
7214           (gnus-tree-close gnus-newsgroup-name)))
7215       (gnus-kill-buffer buffer))
7216      ;; Deaden the buffer.
7217      ((gnus-buffer-exists-p buffer)
7218       (save-excursion
7219         (set-buffer buffer)
7220         (gnus-deaden-summary))))))
7221
7222 (defun gnus-summary-wake-up-the-dead (&rest args)
7223   "Wake up the dead summary buffer."
7224   (interactive)
7225   (gnus-dead-summary-mode -1)
7226   (let ((name (buffer-name)))
7227     (when (string-match "Dead " name)
7228       (rename-buffer
7229        (concat (substring name 0 (match-beginning 0))
7230                (substring name (match-end 0)))
7231        t)))
7232   (gnus-message 3 "This dead summary is now alive again"))
7233
7234 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7235 (defun gnus-summary-fetch-faq (&optional faq-dir)
7236   "Fetch the FAQ for the current group.
7237 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7238 in."
7239   (interactive
7240    (list
7241     (when current-prefix-arg
7242       (completing-read
7243        "FAQ dir: " (and (listp gnus-group-faq-directory)
7244                         (mapcar 'list
7245                                 gnus-group-faq-directory))))))
7246   (let (gnus-faq-buffer)
7247     (when (setq gnus-faq-buffer
7248                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7249       (gnus-configure-windows 'summary-faq))))
7250
7251 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7252 (defun gnus-summary-describe-group (&optional force)
7253   "Describe the current newsgroup."
7254   (interactive "P")
7255   (gnus-group-describe-group force gnus-newsgroup-name))
7256
7257 (defun gnus-summary-describe-briefly ()
7258   "Describe summary mode commands briefly."
7259   (interactive)
7260   (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")))
7261
7262 ;; Walking around group mode buffer from summary mode.
7263
7264 (defun gnus-summary-next-group (&optional no-article target-group backward)
7265   "Exit current newsgroup and then select next unread newsgroup.
7266 If prefix argument NO-ARTICLE is non-nil, no article is selected
7267 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7268 previous group instead."
7269   (interactive "P")
7270   ;; Stop pre-fetching.
7271   (gnus-async-halt-prefetch)
7272   (let ((current-group gnus-newsgroup-name)
7273         (current-buffer (current-buffer))
7274         entered)
7275     ;; First we semi-exit this group to update Xrefs and all variables.
7276     ;; We can't do a real exit, because the window conf must remain
7277     ;; the same in case the user is prompted for info, and we don't
7278     ;; want the window conf to change before that...
7279     (gnus-summary-exit t)
7280     (while (not entered)
7281       ;; Then we find what group we are supposed to enter.
7282       (set-buffer gnus-group-buffer)
7283       (gnus-group-jump-to-group current-group)
7284       (setq target-group
7285             (or target-group
7286                 (if (eq gnus-keep-same-level 'best)
7287                     (gnus-summary-best-group gnus-newsgroup-name)
7288                   (gnus-summary-search-group backward gnus-keep-same-level))))
7289       (if (not target-group)
7290           ;; There are no further groups, so we return to the group
7291           ;; buffer.
7292           (progn
7293             (gnus-message 5 "Returning to the group buffer")
7294             (setq entered t)
7295             (when (gnus-buffer-live-p current-buffer)
7296               (set-buffer current-buffer)
7297               (gnus-summary-exit))
7298             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7299         ;; We try to enter the target group.
7300         (gnus-group-jump-to-group target-group)
7301         (let ((unreads (gnus-group-group-unread)))
7302           (if (and (or (eq t unreads)
7303                        (and unreads (not (zerop unreads))))
7304                    (gnus-summary-read-group
7305                     target-group nil no-article
7306                     (and (buffer-name current-buffer) current-buffer)
7307                     nil backward))
7308               (setq entered t)
7309             (setq current-group target-group
7310                   target-group nil)))))))
7311
7312 (defun gnus-summary-prev-group (&optional no-article)
7313   "Exit current newsgroup and then select previous unread newsgroup.
7314 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7315   (interactive "P")
7316   (gnus-summary-next-group no-article nil t))
7317
7318 ;; Walking around summary lines.
7319
7320 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7321   "Go to the first subject satisfying any non-nil constraint.
7322 If UNREAD is non-nil, the article should be unread.
7323 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7324 If UNSEEN is non-nil, the article should be unseen.
7325 Returns the article selected or nil if there are no matching articles."
7326   (interactive "P")
7327   (cond
7328    ;; Empty summary.
7329    ((null gnus-newsgroup-data)
7330     (gnus-message 3 "No articles in the group")
7331     nil)
7332    ;; Pick the first article.
7333    ((not (or unread undownloaded unseen))
7334     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7335     (gnus-data-number (car gnus-newsgroup-data)))
7336    ;; Find the first unread article.
7337    (t
7338     (let ((data gnus-newsgroup-data))
7339       (while (and data
7340                   (let ((num (gnus-data-number (car data))))
7341                     (or (memq num gnus-newsgroup-unfetched)
7342                         (not (or (and unread
7343                                       (memq num gnus-newsgroup-unreads))
7344                                  (and undownloaded
7345                                       (memq num gnus-newsgroup-undownloaded))
7346                                  (and unseen
7347                                       (memq num gnus-newsgroup-unseen)))))))
7348         (setq data (cdr data)))
7349       (prog1
7350           (if data
7351               (progn
7352                 (goto-char (gnus-data-pos (car data)))
7353                 (gnus-data-number (car data)))
7354             (gnus-message 3 "No more%s articles"
7355                           (let* ((r (when unread " unread"))
7356                                  (d (when undownloaded " undownloaded"))
7357                                  (s (when unseen " unseen"))
7358                                  (l (delq nil (list r d s))))
7359                             (cond ((= 3 (length l))
7360                                    (concat r "," d ", or" s))
7361                                   ((= 2 (length l))
7362                                    (concat (car l) ", or" (cadr l)))
7363                                   ((= 1 (length l))
7364                                    (car l))
7365                                   (t
7366                                    ""))))
7367             nil
7368             )
7369         (gnus-summary-position-point))))))
7370
7371 (defun gnus-summary-next-subject (n &optional unread dont-display)
7372   "Go to next N'th summary line.
7373 If N is negative, go to the previous N'th subject line.
7374 If UNREAD is non-nil, only unread articles are selected.
7375 The difference between N and the actual number of steps taken is
7376 returned."
7377   (interactive "p")
7378   (let ((backward (< n 0))
7379         (n (abs n)))
7380     (while (and (> n 0)
7381                 (if backward
7382                     (gnus-summary-find-prev unread)
7383                   (gnus-summary-find-next unread)))
7384       (unless (zerop (setq n (1- n)))
7385         (gnus-summary-show-thread)))
7386     (when (/= 0 n)
7387       (gnus-message 7 "No more%s articles"
7388                     (if unread " unread" "")))
7389     (unless dont-display
7390       (gnus-summary-recenter)
7391       (gnus-summary-position-point))
7392     n))
7393
7394 (defun gnus-summary-next-unread-subject (n)
7395   "Go to next N'th unread summary line."
7396   (interactive "p")
7397   (gnus-summary-next-subject n t))
7398
7399 (defun gnus-summary-prev-subject (n &optional unread)
7400   "Go to previous N'th summary line.
7401 If optional argument UNREAD is non-nil, only unread article is selected."
7402   (interactive "p")
7403   (gnus-summary-next-subject (- n) unread))
7404
7405 (defun gnus-summary-prev-unread-subject (n)
7406   "Go to previous N'th unread summary line."
7407   (interactive "p")
7408   (gnus-summary-next-subject (- n) t))
7409
7410 (defun gnus-summary-goto-subjects (articles)
7411   "Insert the subject header for ARTICLES in the current buffer."
7412   (save-excursion
7413     (dolist (article articles)
7414       (gnus-summary-goto-subject article t)))
7415   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7416   (gnus-summary-position-point))
7417
7418 (defun gnus-summary-goto-subject (article &optional force silent)
7419   "Go the subject line of ARTICLE.
7420 If FORCE, also allow jumping to articles not currently shown."
7421   (interactive "nArticle number: ")
7422   (unless (numberp article)
7423     (error "Article %s is not a number" article))
7424   (let ((b (point))
7425         (data (gnus-data-find article)))
7426     ;; We read in the article if we have to.
7427     (and (not data)
7428          force
7429          (gnus-summary-insert-subject
7430           article
7431           (if (or (numberp force) (vectorp force)) force)
7432           t)
7433          (setq data (gnus-data-find article)))
7434     (goto-char b)
7435     (if (not data)
7436         (progn
7437           (unless silent
7438             (gnus-message 3 "Can't find article %d" article))
7439           nil)
7440       (let ((pt (gnus-data-pos data)))
7441         (goto-char pt)
7442         (gnus-summary-set-article-display-arrow pt))
7443       (gnus-summary-position-point)
7444       article)))
7445
7446 ;; Walking around summary lines with displaying articles.
7447
7448 (defun gnus-summary-expand-window (&optional arg)
7449   "Make the summary buffer take up the entire Emacs frame.
7450 Given a prefix, will force an `article' buffer configuration."
7451   (interactive "P")
7452   (if arg
7453       (gnus-configure-windows 'article 'force)
7454     (gnus-configure-windows 'summary 'force)))
7455
7456 (defun gnus-summary-display-article (article &optional all-header)
7457   "Display ARTICLE in article buffer."
7458   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7459                (with-current-buffer gnus-article-buffer
7460                  (eq major-mode 'gnus-article-mode)))
7461     (gnus-article-setup-buffer))
7462   (gnus-set-global-variables)
7463   (with-current-buffer gnus-article-buffer
7464     (setq gnus-article-charset gnus-newsgroup-charset)
7465     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7466     (mm-enable-multibyte))
7467   (if (null article)
7468       nil
7469     (prog1
7470         (if gnus-summary-display-article-function
7471             (funcall gnus-summary-display-article-function article all-header)
7472           (gnus-article-prepare article all-header))
7473       (gnus-run-hooks 'gnus-select-article-hook)
7474       (when (and gnus-current-article
7475                  (not (zerop gnus-current-article)))
7476         (gnus-summary-goto-subject gnus-current-article))
7477       (gnus-summary-recenter)
7478       (when (and gnus-use-trees gnus-show-threads)
7479         (gnus-possibly-generate-tree article)
7480         (gnus-highlight-selected-tree article))
7481       ;; Successfully display article.
7482       (gnus-article-set-window-start
7483        (cdr (assq article gnus-newsgroup-bookmarks))))))
7484
7485 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7486   "Select the current article.
7487 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7488 non-nil, the article will be re-fetched even if it already present in
7489 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7490 be displayed."
7491   ;; Make sure we are in the summary buffer to work around bbdb bug.
7492   (unless (eq major-mode 'gnus-summary-mode)
7493     (set-buffer gnus-summary-buffer))
7494   (let ((article (or article (gnus-summary-article-number)))
7495         (all-headers (not (not all-headers))) ;Must be t or nil.
7496         gnus-summary-display-article-function)
7497     (and (not pseudo)
7498          (gnus-summary-article-pseudo-p article)
7499          (error "This is a pseudo-article"))
7500     (save-excursion
7501       (set-buffer gnus-summary-buffer)
7502       (if (or (and gnus-single-article-buffer
7503                    (or (null gnus-current-article)
7504                        (null gnus-article-current)
7505                        (null (get-buffer gnus-article-buffer))
7506                        (not (eq article (cdr gnus-article-current)))
7507                        (not (equal (car gnus-article-current)
7508                                    gnus-newsgroup-name))))
7509               (and (not gnus-single-article-buffer)
7510                    (or (null gnus-current-article)
7511                        (not (eq gnus-current-article article))))
7512               force)
7513           ;; The requested article is different from the current article.
7514           (progn
7515             (gnus-summary-display-article article all-headers)
7516             (when (gnus-buffer-live-p gnus-article-buffer)
7517               (with-current-buffer gnus-article-buffer
7518                 (if (not gnus-article-decoded-p) ;; a local variable
7519                     (mm-disable-multibyte))))
7520             (gnus-article-set-window-start
7521              (cdr (assq article gnus-newsgroup-bookmarks)))
7522             article)
7523         'old))))
7524
7525 (defun gnus-summary-force-verify-and-decrypt ()
7526   "Display buttons for signed/encrypted parts and verify/decrypt them."
7527   (interactive)
7528   (let ((mm-verify-option 'known)
7529         (mm-decrypt-option 'known)
7530         (gnus-article-emulate-mime t)
7531         (gnus-buttonized-mime-types (append (list "multipart/signed"
7532                                                   "multipart/encrypted")
7533                                             gnus-buttonized-mime-types)))
7534     (gnus-summary-select-article nil 'force)))
7535
7536 (defun gnus-summary-set-current-mark (&optional current-mark)
7537   "Obsolete function."
7538   nil)
7539
7540 (defun gnus-summary-next-article (&optional unread subject backward push)
7541   "Select the next article.
7542 If UNREAD, only unread articles are selected.
7543 If SUBJECT, only articles with SUBJECT are selected.
7544 If BACKWARD, the previous article is selected instead of the next."
7545   (interactive "P")
7546   ;; Make sure we are in the summary buffer.
7547   (unless (eq major-mode 'gnus-summary-mode)
7548     (set-buffer gnus-summary-buffer))
7549   (cond
7550    ;; Is there such an article?
7551    ((and (gnus-summary-search-forward unread subject backward)
7552          (or (gnus-summary-display-article (gnus-summary-article-number))
7553              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7554     (gnus-summary-position-point))
7555    ;; If not, we try the first unread, if that is wanted.
7556    ((and subject
7557          gnus-auto-select-same
7558          (gnus-summary-first-unread-article))
7559     (gnus-summary-position-point)
7560     (gnus-message 6 "Wrapped"))
7561    ;; Try to get next/previous article not displayed in this group.
7562    ((and gnus-auto-extend-newsgroup
7563          (not unread) (not subject))
7564     (gnus-summary-goto-article
7565      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7566      nil (count-lines (point-min) (point))))
7567    ;; Go to next/previous group.
7568    (t
7569     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7570       (gnus-summary-jump-to-group gnus-newsgroup-name))
7571     (let ((cmd last-command-char)
7572           (point
7573            (with-current-buffer gnus-group-buffer
7574              (point)))
7575           (group
7576            (if (eq gnus-keep-same-level 'best)
7577                (gnus-summary-best-group gnus-newsgroup-name)
7578              (gnus-summary-search-group backward gnus-keep-same-level))))
7579       ;; For some reason, the group window gets selected.  We change
7580       ;; it back.
7581       (select-window (get-buffer-window (current-buffer)))
7582       ;; Select next unread newsgroup automagically.
7583       (cond
7584        ((or (not gnus-auto-select-next)
7585             (not cmd))
7586         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7587        ((or (eq gnus-auto-select-next 'quietly)
7588             (and (eq gnus-auto-select-next 'slightly-quietly)
7589                  push)
7590             (and (eq gnus-auto-select-next 'almost-quietly)
7591                  (gnus-summary-last-article-p)))
7592         ;; Select quietly.
7593         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7594             (gnus-summary-exit)
7595           (gnus-message 7 "No more%s articles (%s)..."
7596                         (if unread " unread" "")
7597                         (if group (concat "selecting " group)
7598                           "exiting"))
7599           (gnus-summary-next-group nil group backward)))
7600        (t
7601         (when (gnus-key-press-event-p last-input-event)
7602           (gnus-summary-walk-group-buffer
7603            gnus-newsgroup-name cmd unread backward point))))))))
7604
7605 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7606   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7607                       (?\C-p (gnus-group-prev-unread-group 1))))
7608         (cursor-in-echo-area t)
7609         keve key group ended prompt)
7610     (save-excursion
7611       (set-buffer gnus-group-buffer)
7612       (goto-char start)
7613       (setq group
7614             (if (eq gnus-keep-same-level 'best)
7615                 (gnus-summary-best-group gnus-newsgroup-name)
7616               (gnus-summary-search-group backward gnus-keep-same-level))))
7617     (while (not ended)
7618       (setq prompt
7619             (format
7620              "No more%s articles%s " (if unread " unread" "")
7621              (if (and group
7622                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7623                  (format " (Type %s for %s [%s])"
7624                          (single-key-description cmd)
7625                          (gnus-group-decoded-name group)
7626                          (gnus-group-unread group))
7627                (format " (Type %s to exit %s)"
7628                        (single-key-description cmd)
7629                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7630       ;; Confirm auto selection.
7631       (setq key (car (setq keve (gnus-read-event-char prompt)))
7632             ended t)
7633       (cond
7634        ((assq key keystrokes)
7635         (let ((obuf (current-buffer)))
7636           (switch-to-buffer gnus-group-buffer)
7637           (when group
7638             (gnus-group-jump-to-group group))
7639           (eval (cadr (assq key keystrokes)))
7640           (setq group (gnus-group-group-name))
7641           (switch-to-buffer obuf))
7642         (setq ended nil))
7643        ((equal key cmd)
7644         (if (or (not group)
7645                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7646             (gnus-summary-exit)
7647           (gnus-summary-next-group nil group backward)))
7648        (t
7649         (push (cdr keve) unread-command-events))))))
7650
7651 (defun gnus-summary-next-unread-article ()
7652   "Select unread article after current one."
7653   (interactive)
7654   (gnus-summary-next-article
7655    (or (not (eq gnus-summary-goto-unread 'never))
7656        (gnus-summary-last-article-p (gnus-summary-article-number)))
7657    (and gnus-auto-select-same
7658         (gnus-summary-article-subject))))
7659
7660 (defun gnus-summary-prev-article (&optional unread subject)
7661   "Select the article before the current one.
7662 If UNREAD is non-nil, only unread articles are selected."
7663   (interactive "P")
7664   (gnus-summary-next-article unread subject t))
7665
7666 (defun gnus-summary-prev-unread-article ()
7667   "Select unread article before current one."
7668   (interactive)
7669   (gnus-summary-prev-article
7670    (or (not (eq gnus-summary-goto-unread 'never))
7671        (gnus-summary-first-article-p (gnus-summary-article-number)))
7672    (and gnus-auto-select-same
7673         (gnus-summary-article-subject))))
7674
7675 (defun gnus-summary-next-page (&optional lines circular stop)
7676   "Show next page of the selected article.
7677 If at the end of the current article, select the next article.
7678 LINES says how many lines should be scrolled up.
7679
7680 If CIRCULAR is non-nil, go to the start of the article instead of
7681 selecting the next article when reaching the end of the current
7682 article.
7683
7684 If STOP is non-nil, just stop when reaching the end of the message.
7685
7686 Also see the variable `gnus-article-skip-boring'."
7687   (interactive "P")
7688   (setq gnus-summary-buffer (current-buffer))
7689   (gnus-set-global-variables)
7690   (let ((article (gnus-summary-article-number))
7691         (article-window (get-buffer-window gnus-article-buffer t))
7692         endp)
7693     ;; If the buffer is empty, we have no article.
7694     (unless article
7695       (error "No article to select"))
7696     (gnus-configure-windows 'article)
7697     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7698         (if (and (eq gnus-summary-goto-unread 'never)
7699                  (not (gnus-summary-last-article-p article)))
7700             (gnus-summary-next-article)
7701           (gnus-summary-next-unread-article))
7702       (if (or (null gnus-current-article)
7703               (null gnus-article-current)
7704               (/= article (cdr gnus-article-current))
7705               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7706           ;; Selected subject is different from current article's.
7707           (gnus-summary-display-article article)
7708         (when article-window
7709           (gnus-eval-in-buffer-window gnus-article-buffer
7710             (setq endp (or (gnus-article-next-page lines)
7711                            (gnus-article-only-boring-p))))
7712           (when endp
7713             (cond (stop
7714                    (gnus-message 3 "End of message"))
7715                   (circular
7716                    (gnus-summary-beginning-of-article))
7717                   (lines
7718                    (gnus-message 3 "End of message"))
7719                   ((null lines)
7720                    (if (and (eq gnus-summary-goto-unread 'never)
7721                             (not (gnus-summary-last-article-p article)))
7722                        (gnus-summary-next-article)
7723                      (gnus-summary-next-unread-article))))))))
7724     (gnus-summary-recenter)
7725     (gnus-summary-position-point)))
7726
7727 (defun gnus-summary-prev-page (&optional lines move)
7728   "Show previous page of selected article.
7729 Argument LINES specifies lines to be scrolled down.
7730 If MOVE, move to the previous unread article if point is at
7731 the beginning of the buffer."
7732   (interactive "P")
7733   (let ((article (gnus-summary-article-number))
7734         (article-window (get-buffer-window gnus-article-buffer t))
7735         endp)
7736     (gnus-configure-windows 'article)
7737     (if (or (null gnus-current-article)
7738             (null gnus-article-current)
7739             (/= article (cdr gnus-article-current))
7740             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7741         ;; Selected subject is different from current article's.
7742         (gnus-summary-display-article article)
7743       (gnus-summary-recenter)
7744       (when article-window
7745         (gnus-eval-in-buffer-window gnus-article-buffer
7746           (setq endp (gnus-article-prev-page lines)))
7747         (when (and move endp)
7748           (cond (lines
7749                  (gnus-message 3 "Beginning of message"))
7750                 ((null lines)
7751                  (if (and (eq gnus-summary-goto-unread 'never)
7752                           (not (gnus-summary-first-article-p article)))
7753                      (gnus-summary-prev-article)
7754                    (gnus-summary-prev-unread-article))))))))
7755   (gnus-summary-position-point))
7756
7757 (defun gnus-summary-prev-page-or-article (&optional lines)
7758   "Show previous page of selected article.
7759 Argument LINES specifies lines to be scrolled down.
7760 If at the beginning of the article, go to the next article."
7761   (interactive "P")
7762   (gnus-summary-prev-page lines t))
7763
7764 (defun gnus-summary-scroll-up (lines)
7765   "Scroll up (or down) one line current article.
7766 Argument LINES specifies lines to be scrolled up (or down if negative)."
7767   (interactive "p")
7768   (gnus-configure-windows 'article)
7769   (gnus-summary-show-thread)
7770   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7771     (gnus-eval-in-buffer-window gnus-article-buffer
7772       (cond ((> lines 0)
7773              (when (gnus-article-next-page lines)
7774                (gnus-message 3 "End of message")))
7775             ((< lines 0)
7776              (gnus-article-prev-page (- lines))))))
7777   (gnus-summary-recenter)
7778   (gnus-summary-position-point))
7779
7780 (defun gnus-summary-scroll-down (lines)
7781   "Scroll down (or up) one line current article.
7782 Argument LINES specifies lines to be scrolled down (or up if negative)."
7783   (interactive "p")
7784   (gnus-summary-scroll-up (- lines)))
7785
7786 (defun gnus-summary-next-same-subject ()
7787   "Select next article which has the same subject as current one."
7788   (interactive)
7789   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7790
7791 (defun gnus-summary-prev-same-subject ()
7792   "Select previous article which has the same subject as current one."
7793   (interactive)
7794   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7795
7796 (defun gnus-summary-next-unread-same-subject ()
7797   "Select next unread article which has the same subject as current one."
7798   (interactive)
7799   (gnus-summary-next-article t (gnus-summary-article-subject)))
7800
7801 (defun gnus-summary-prev-unread-same-subject ()
7802   "Select previous unread article which has the same subject as current one."
7803   (interactive)
7804   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7805
7806 (defun gnus-summary-first-unread-article ()
7807   "Select the first unread article.
7808 Return nil if there are no unread articles."
7809   (interactive)
7810   (prog1
7811       (when (gnus-summary-first-subject t)
7812         (gnus-summary-show-thread)
7813         (gnus-summary-first-subject t)
7814         (gnus-summary-display-article (gnus-summary-article-number)))
7815     (gnus-summary-position-point)))
7816
7817 (defun gnus-summary-first-unread-subject ()
7818   "Place the point on the subject line of the first unread article.
7819 Return nil if there are no unread articles."
7820   (interactive)
7821   (prog1
7822       (when (gnus-summary-first-subject t)
7823         (gnus-summary-show-thread)
7824         (gnus-summary-first-subject t))
7825     (gnus-summary-position-point)))
7826
7827 (defun gnus-summary-first-unseen-subject ()
7828   "Place the point on the subject line of the first unseen article.
7829 Return nil if there are no unseen articles."
7830   (interactive)
7831   (prog1
7832       (when (gnus-summary-first-subject nil nil t)
7833         (gnus-summary-show-thread)
7834         (gnus-summary-first-subject nil nil t))
7835     (gnus-summary-position-point)))
7836
7837 (defun gnus-summary-first-unseen-or-unread-subject ()
7838   "Place the point on the subject line of the first unseen article or,
7839 if all article have been seen, on the subject line of the first unread
7840 article."
7841   (interactive)
7842   (prog1
7843       (unless (when (gnus-summary-first-subject nil nil t)
7844                 (gnus-summary-show-thread)
7845                 (gnus-summary-first-subject nil nil t))
7846         (when (gnus-summary-first-subject t)
7847           (gnus-summary-show-thread)
7848           (gnus-summary-first-subject t)))
7849     (gnus-summary-position-point)))
7850
7851 (defun gnus-summary-first-article ()
7852   "Select the first article.
7853 Return nil if there are no articles."
7854   (interactive)
7855   (prog1
7856       (when (gnus-summary-first-subject)
7857         (gnus-summary-show-thread)
7858         (gnus-summary-first-subject)
7859         (gnus-summary-display-article (gnus-summary-article-number)))
7860     (gnus-summary-position-point)))
7861
7862 (defun gnus-summary-best-unread-article (&optional arg)
7863   "Select the unread article with the highest score.
7864 If given a prefix argument, select the next unread article that has a
7865 score higher than the default score."
7866   (interactive "P")
7867   (let ((article (if arg
7868                      (gnus-summary-better-unread-subject)
7869                    (gnus-summary-best-unread-subject))))
7870     (if article
7871         (gnus-summary-goto-article article)
7872       (error "No unread articles"))))
7873
7874 (defun gnus-summary-best-unread-subject ()
7875   "Select the unread subject with the highest score."
7876   (interactive)
7877   (let ((best -1000000)
7878         (data gnus-newsgroup-data)
7879         article score)
7880     (while data
7881       (and (gnus-data-unread-p (car data))
7882            (> (setq score
7883                     (gnus-summary-article-score (gnus-data-number (car data))))
7884               best)
7885            (setq best score
7886                  article (gnus-data-number (car data))))
7887       (setq data (cdr data)))
7888     (when article
7889       (gnus-summary-goto-subject article))
7890     (gnus-summary-position-point)
7891     article))
7892
7893 (defun gnus-summary-better-unread-subject ()
7894   "Select the first unread subject that has a score over the default score."
7895   (interactive)
7896   (let ((data gnus-newsgroup-data)
7897         article score)
7898     (while (and (setq article (gnus-data-number (car data)))
7899                 (or (gnus-data-read-p (car data))
7900                     (not (> (gnus-summary-article-score article)
7901                             gnus-summary-default-score))))
7902       (setq data (cdr data)))
7903     (when article
7904       (gnus-summary-goto-subject article))
7905     (gnus-summary-position-point)
7906     article))
7907
7908 (defun gnus-summary-last-subject ()
7909   "Go to the last displayed subject line in the group."
7910   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7911     (when article
7912       (gnus-summary-goto-subject article))))
7913
7914 (defun gnus-summary-goto-article (article &optional all-headers force)
7915   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7916 If ALL-HEADERS is non-nil, no header lines are hidden.
7917 If FORCE, go to the article even if it isn't displayed.  If FORCE
7918 is a number, it is the line the article is to be displayed on."
7919   (interactive
7920    (list
7921     (completing-read
7922      "Article number or Message-ID: "
7923      (mapcar (lambda (number) (list (int-to-string number)))
7924              gnus-newsgroup-limit))
7925     current-prefix-arg
7926     t))
7927   (prog1
7928       (if (and (stringp article)
7929                (string-match "@\\|%40" article))
7930           (gnus-summary-refer-article article)
7931         (when (stringp article)
7932           (setq article (string-to-number article)))
7933         (if (gnus-summary-goto-subject article force)
7934             (gnus-summary-display-article article all-headers)
7935           (gnus-message 4 "Couldn't go to article %s" article) nil))
7936     (gnus-summary-position-point)))
7937
7938 (defun gnus-summary-goto-last-article ()
7939   "Go to the previously read article."
7940   (interactive)
7941   (prog1
7942       (when gnus-last-article
7943         (gnus-summary-goto-article gnus-last-article nil t))
7944     (gnus-summary-position-point)))
7945
7946 (defun gnus-summary-pop-article (number)
7947   "Pop one article off the history and go to the previous.
7948 NUMBER articles will be popped off."
7949   (interactive "p")
7950   (let (to)
7951     (setq gnus-newsgroup-history
7952           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7953     (if to
7954         (gnus-summary-goto-article (car to) nil t)
7955       (error "Article history empty")))
7956   (gnus-summary-position-point))
7957
7958 ;; Summary commands and functions for limiting the summary buffer.
7959
7960 (defun gnus-summary-limit-to-articles (n)
7961   "Limit the summary buffer to the next N articles.
7962 If not given a prefix, use the process marked articles instead."
7963   (interactive "P")
7964   (prog1
7965       (let ((articles (gnus-summary-work-articles n)))
7966         (setq gnus-newsgroup-processable nil)
7967         (gnus-summary-limit articles))
7968     (gnus-summary-position-point)))
7969
7970 (defun gnus-summary-pop-limit (&optional total)
7971   "Restore the previous limit.
7972 If given a prefix, remove all limits."
7973   (interactive "P")
7974   (when total
7975     (setq gnus-newsgroup-limits
7976           (list (mapcar (lambda (h) (mail-header-number h))
7977                         gnus-newsgroup-headers))))
7978   (unless gnus-newsgroup-limits
7979     (error "No limit to pop"))
7980   (prog1
7981       (gnus-summary-limit nil 'pop)
7982     (gnus-summary-position-point)))
7983
7984 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7985   "Limit the summary buffer to articles that have subjects that match a regexp.
7986 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7987   (interactive
7988    (list (read-string (if current-prefix-arg
7989                           "Exclude subject (regexp): "
7990                         "Limit to subject (regexp): "))
7991          nil current-prefix-arg))
7992   (unless header
7993     (setq header "subject"))
7994   (when (not (equal "" subject))
7995     (prog1
7996         (let ((articles (gnus-summary-find-matching
7997                          (or header "subject") subject 'all nil nil
7998                          not-matching)))
7999           (unless articles
8000             (error "Found no matches for \"%s\"" subject))
8001           (gnus-summary-limit articles))
8002       (gnus-summary-position-point))))
8003
8004 (defun gnus-summary-limit-to-author (from &optional not-matching)
8005   "Limit the summary buffer to articles that have authors that match a regexp.
8006 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8007   (interactive
8008    (list (read-string (if current-prefix-arg
8009                           "Exclude author (regexp): "
8010                         "Limit to author (regexp): "))
8011          current-prefix-arg))
8012   (gnus-summary-limit-to-subject from "from" not-matching))
8013
8014 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8015   "Limit the summary buffer to articles with the given RECIPIENT.
8016
8017 If NOT-MATCHING, exclude RECIPIENT.
8018
8019 To and Cc headers are checked.  You need to include them in
8020 `nnmail-extra-headers'."
8021   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8022   (interactive
8023    (list (read-string (format "%s recipient (regexp): "
8024                               (if current-prefix-arg "Exclude" "Limit to")))
8025          current-prefix-arg))
8026   (when (not (equal "" recipient))
8027     (prog1 (let* ((to
8028                    (if (memq 'To nnmail-extra-headers)
8029                        (gnus-summary-find-matching
8030                         (cons 'extra 'To) recipient 'all nil nil
8031                         not-matching)
8032                      (gnus-message
8033                       1 "`To' isn't present in `nnmail-extra-headers'")
8034                      (sit-for 1)
8035                      nil))
8036                   (cc
8037                    (if (memq 'Cc nnmail-extra-headers)
8038                        (gnus-summary-find-matching
8039                         (cons 'extra 'Cc) recipient 'all nil nil
8040                         not-matching)
8041                      (gnus-message
8042                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8043                      (sit-for 1)
8044                      nil))
8045                   (articles
8046                    (if not-matching
8047                        ;; We need the numbers that are in both lists:
8048                        (mapcar (lambda (a)
8049                                  (and (memq a to) a))
8050                                cc)
8051                      (nconc to cc))))
8052              (unless articles
8053                (error "Found no matches for \"%s\"" recipient))
8054              (gnus-summary-limit articles))
8055       (gnus-summary-position-point))))
8056
8057 (defun gnus-summary-limit-to-address (address &optional not-matching)
8058   "Limit the summary buffer to articles with the given ADDRESS.
8059
8060 If NOT-MATCHING, exclude ADDRESS.
8061
8062 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8063 in `nnmail-extra-headers'."
8064   (interactive
8065    (list (read-string (format "%s address (regexp): "
8066                               (if current-prefix-arg "Exclude" "Limit to")))
8067          current-prefix-arg))
8068   (when (not (equal "" address))
8069     (prog1 (let* ((to
8070                    (if (memq 'To nnmail-extra-headers)
8071                        (gnus-summary-find-matching
8072                         (cons 'extra 'To) address 'all nil nil
8073                         not-matching)
8074                      (gnus-message
8075                       1 "`To' isn't present in `nnmail-extra-headers'")
8076                      (sit-for 1)
8077                      t))
8078                   (cc
8079                    (if (memq 'Cc nnmail-extra-headers)
8080                        (gnus-summary-find-matching
8081                         (cons 'extra 'Cc) address 'all nil nil
8082                         not-matching)
8083                      (gnus-message
8084                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8085                      (sit-for 1)
8086                      t))
8087                   (from
8088                    (gnus-summary-find-matching "from" address
8089                                                'all nil nil not-matching))
8090                   (articles
8091                    (if not-matching
8092                        ;; We need the numbers that are in all lists:
8093                        (if (eq cc t)
8094                            (if (eq to t)
8095                                from
8096                              (mapcar (lambda (a) (car (memq a from))) to))
8097                          (if (eq to t)
8098                              (mapcar (lambda (a) (car (memq a from))) cc)
8099                            (mapcar (lambda (a) (car (memq a from)))
8100                                    (mapcar (lambda (a) (car (memq a to)))
8101                                            cc))))
8102                      (nconc (if (eq to t) nil to)
8103                             (if (eq cc t) nil cc)
8104                             from))))
8105              (unless articles
8106                (error "Found no matches for \"%s\"" address))
8107              (gnus-summary-limit articles))
8108       (gnus-summary-position-point))))
8109
8110 (defun gnus-summary-limit-strange-charsets-predicate (header)
8111   (let ((string (concat (mail-header-subject header)
8112                         (mail-header-from header)))
8113         charset found)
8114     (dotimes (i (1- (length string)))
8115       (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8116       (when (string-match "unicode\\|big\\|japanese" charset)
8117         (setq found t)))
8118     found))
8119
8120 (defun gnus-summary-limit-to-predicate (predicate)
8121   "Limit to articles where PREDICATE returns non-nil.
8122 PREDICATE will be called with the header structures of the
8123 articles."
8124   (let ((articles nil)
8125         (case-fold-search t))
8126     (dolist (header gnus-newsgroup-headers)
8127       (when (funcall predicate header)
8128         (push (mail-header-number header) articles)))
8129     (gnus-summary-limit (nreverse articles))))
8130
8131 (defun gnus-summary-limit-to-age (age &optional younger-p)
8132   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8133 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8134 articles that are younger than AGE days."
8135   (interactive
8136    (let ((younger current-prefix-arg)
8137          (days-got nil)
8138          days)
8139      (while (not days-got)
8140        (setq days (if younger
8141                       (read-string "Limit to articles younger than (in days, older when negative): ")
8142                     (read-string
8143                      "Limit to articles older than (in days, younger when negative): ")))
8144        (when (> (length days) 0)
8145          (setq days (read days)))
8146        (if (numberp days)
8147            (progn
8148              (setq days-got t)
8149              (when (< days 0)
8150                (setq younger (not younger))
8151                (setq days (* days -1))))
8152          (message "Please enter a number.")
8153          (sleep-for 1)))
8154      (list days younger)))
8155   (prog1
8156       (let ((data gnus-newsgroup-data)
8157             (cutoff (days-to-time age))
8158             articles d date is-younger)
8159         (while (setq d (pop data))
8160           (when (and (vectorp (gnus-data-header d))
8161                      (setq date (mail-header-date (gnus-data-header d))))
8162             (setq is-younger (time-less-p
8163                               (time-since (condition-case ()
8164                                               (date-to-time date)
8165                                             (error '(0 0))))
8166                               cutoff))
8167             (when (if younger-p
8168                       is-younger
8169                     (not is-younger))
8170               (push (gnus-data-number d) articles))))
8171         (gnus-summary-limit (nreverse articles)))
8172     (gnus-summary-position-point)))
8173
8174 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8175   "Limit the summary buffer to articles that match an 'extra' header."
8176   (interactive
8177    (let ((header
8178           (intern
8179            (gnus-completing-read-with-default
8180             (symbol-name (car gnus-extra-headers))
8181             (if current-prefix-arg
8182                 "Exclude extra header"
8183               "Limit extra header")
8184             (mapcar (lambda (x)
8185                       (cons (symbol-name x) x))
8186                     gnus-extra-headers)
8187             nil
8188             t))))
8189      (list header
8190            (read-string (format "%s header %s (regexp): "
8191                                 (if current-prefix-arg "Exclude" "Limit to")
8192                                 header))
8193            current-prefix-arg)))
8194   (when (not (equal "" regexp))
8195     (prog1
8196         (let ((articles (gnus-summary-find-matching
8197                          (cons 'extra header) regexp 'all nil nil
8198                          not-matching)))
8199           (unless articles
8200             (error "Found no matches for \"%s\"" regexp))
8201           (gnus-summary-limit articles))
8202       (gnus-summary-position-point))))
8203
8204 (defun gnus-summary-limit-to-display-predicate ()
8205   "Limit the summary buffer to the predicated in the `display' group parameter."
8206   (interactive)
8207   (unless gnus-newsgroup-display
8208     (error "There is no `display' group parameter"))
8209   (let (articles)
8210     (dolist (number gnus-newsgroup-articles)
8211       (when (funcall gnus-newsgroup-display)
8212         (push number articles)))
8213     (gnus-summary-limit articles))
8214   (gnus-summary-position-point))
8215
8216 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8217 (make-obsolete
8218  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8219
8220 (defun gnus-summary-limit-to-unread (&optional all)
8221   "Limit the summary buffer to articles that are not marked as read.
8222 If ALL is non-nil, limit strictly to unread articles."
8223   (interactive "P")
8224   (if all
8225       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8226     (gnus-summary-limit-to-marks
8227      ;; Concat all the marks that say that an article is read and have
8228      ;; those removed.
8229      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8230            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8231            gnus-low-score-mark gnus-expirable-mark
8232            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8233            gnus-duplicate-mark gnus-souped-mark)
8234      'reverse)))
8235
8236 (defun gnus-summary-limit-to-headers (match &optional reverse)
8237   "Limit the summary buffer to articles that have headers that match MATCH.
8238 If REVERSE (the prefix), limit to articles that don't match."
8239   (interactive "sMatch headers (regexp): \nP")
8240   (gnus-summary-limit-to-bodies match reverse t))
8241
8242 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8243   "Limit the summary buffer to articles that have bodies that match MATCH.
8244 If REVERSE (the prefix), limit to articles that don't match."
8245   (interactive "sMatch body (regexp): \nP")
8246   (let ((articles nil)
8247         (gnus-select-article-hook nil)  ;Disable hook.
8248         (gnus-article-prepare-hook nil)
8249         (gnus-use-article-prefetch nil)
8250         (gnus-keep-backlog nil)
8251         (gnus-break-pages nil)
8252         (gnus-summary-display-arrow nil)
8253         (gnus-updated-mode-lines nil)
8254         (gnus-auto-center-summary nil)
8255         (gnus-display-mime-function nil))
8256     (dolist (data gnus-newsgroup-data)
8257       (let (gnus-mark-article-hook)
8258         (gnus-summary-select-article t t nil (gnus-data-number data)))
8259       (save-excursion
8260         (set-buffer gnus-article-buffer)
8261         (article-goto-body)
8262         (let* ((case-fold-search t)
8263                (found (if headersp
8264                           (re-search-backward match nil t)
8265                         (re-search-forward match nil t))))
8266           (when (or (and found
8267                          (not reverse))
8268                     (and (not found)
8269                          reverse))
8270             (push (gnus-data-number data) articles)))))
8271     (if (not articles)
8272         (message "No messages matched")
8273       (gnus-summary-limit articles)))
8274   (gnus-summary-position-point))
8275
8276 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8277   "Limit the summary buffer to articles that aren't part on any thread.
8278 If THREADSP (the prefix), limit to articles that are in threads."
8279   (interactive "P")
8280   (let ((articles nil)
8281         thread-articles
8282         threads)
8283     (dolist (thread gnus-newsgroup-threads)
8284       (if (stringp (car thread))
8285           (dolist (thread (cdr thread))
8286             (push thread threads))
8287         (push thread threads)))
8288     (dolist (thread threads)
8289       (setq thread-articles (gnus-articles-in-thread thread))
8290       (when (or (and threadsp
8291                      (> (length thread-articles) 1))
8292                 (and (not threadsp)
8293                      (= (length thread-articles) 1)))
8294         (setq articles (nconc thread-articles articles))))
8295     (if (not articles)
8296         (message "No messages matched")
8297       (gnus-summary-limit articles))
8298     (gnus-summary-position-point)))
8299
8300 (defun gnus-summary-limit-to-replied (&optional unreplied)
8301   "Limit the summary buffer to replied articles.
8302 If UNREPLIED (the prefix), limit to unreplied articles."
8303   (interactive "P")
8304   (if unreplied
8305       (gnus-summary-limit
8306        (gnus-set-difference gnus-newsgroup-articles
8307         gnus-newsgroup-replied))
8308     (gnus-summary-limit gnus-newsgroup-replied))
8309   (gnus-summary-position-point))
8310
8311 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8312 (make-obsolete 'gnus-summary-delete-marked-with
8313                'gnus-summary-limit-exclude-marks)
8314
8315 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8316   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8317 If REVERSE, limit the summary buffer to articles that are marked
8318 with MARKS.  MARKS can either be a string of marks or a list of marks.
8319 Returns how many articles were removed."
8320   (interactive "sMarks: ")
8321   (gnus-summary-limit-to-marks marks t))
8322
8323 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8324   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8325 If REVERSE (the prefix), limit the summary buffer to articles that are
8326 not marked with MARKS.  MARKS can either be a string of marks or a
8327 list of marks.
8328 Returns how many articles were removed."
8329   (interactive "sMarks: \nP")
8330   (prog1
8331       (let ((data gnus-newsgroup-data)
8332             (marks (if (listp marks) marks
8333                      (append marks nil))) ; Transform to list.
8334             articles)
8335         (while data
8336           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8337                   (memq (gnus-data-mark (car data)) marks))
8338             (push (gnus-data-number (car data)) articles))
8339           (setq data (cdr data)))
8340         (gnus-summary-limit articles))
8341     (gnus-summary-position-point)))
8342
8343 (defun gnus-summary-limit-to-score (score)
8344   "Limit to articles with score at or above SCORE."
8345   (interactive "NLimit to articles with score of at least: ")
8346   (let ((data gnus-newsgroup-data)
8347         articles)
8348     (while data
8349       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8350                 score)
8351         (push (gnus-data-number (car data)) articles))
8352       (setq data (cdr data)))
8353     (prog1
8354         (gnus-summary-limit articles)
8355       (gnus-summary-position-point))))
8356
8357 (defun gnus-summary-limit-to-unseen ()
8358   "Limit to unseen articles."
8359   (interactive)
8360   (prog1
8361       (gnus-summary-limit gnus-newsgroup-unseen)
8362     (gnus-summary-position-point)))
8363
8364 (defun gnus-summary-limit-include-thread (id)
8365   "Display all the hidden articles that is in the thread with ID in it.
8366 When called interactively, ID is the Message-ID of the current
8367 article."
8368   (interactive (list (mail-header-id (gnus-summary-article-header))))
8369   (let ((articles (gnus-articles-in-thread
8370                    (gnus-id-to-thread (gnus-root-id id)))))
8371     (prog1
8372         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8373       (gnus-summary-limit-include-matching-articles
8374        "subject"
8375        (regexp-quote (gnus-simplify-subject-re
8376                       (mail-header-subject (gnus-id-to-header id)))))
8377       (gnus-summary-position-point))))
8378
8379 (defun gnus-summary-limit-include-matching-articles (header regexp)
8380   "Display all the hidden articles that have HEADERs that match REGEXP."
8381   (interactive (list (read-string "Match on header: ")
8382                      (read-string "Regexp: ")))
8383   (let ((articles (gnus-find-matching-articles header regexp)))
8384     (prog1
8385         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8386       (gnus-summary-position-point))))
8387
8388 (defun gnus-summary-insert-dormant-articles ()
8389   "Insert all the dormant articles for this group into the current buffer."
8390   (interactive)
8391   (let ((gnus-verbose (max 6 gnus-verbose)))
8392     (if (not gnus-newsgroup-dormant)
8393         (gnus-message 3 "No dormant articles for this group")
8394       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8395
8396 (defun gnus-summary-insert-ticked-articles ()
8397   "Insert ticked articles for this group into the current buffer."
8398   (interactive)
8399   (let ((gnus-verbose (max 6 gnus-verbose)))
8400     (if (not gnus-newsgroup-marked)
8401         (gnus-message 3 "No ticked articles for this group")
8402       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8403
8404 (defun gnus-summary-limit-include-dormant ()
8405   "Display all the hidden articles that are marked as dormant.
8406 Note that this command only works on a subset of the articles currently
8407 fetched for this group."
8408   (interactive)
8409   (unless gnus-newsgroup-dormant
8410     (error "There are no dormant articles in this group"))
8411   (prog1
8412       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8413     (gnus-summary-position-point)))
8414
8415 (defun gnus-summary-limit-exclude-dormant ()
8416   "Hide all dormant articles."
8417   (interactive)
8418   (prog1
8419       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8420     (gnus-summary-position-point)))
8421
8422 (defun gnus-summary-limit-exclude-childless-dormant ()
8423   "Hide all dormant articles that have no children."
8424   (interactive)
8425   (let ((data (gnus-data-list t))
8426         articles d children)
8427     ;; Find all articles that are either not dormant or have
8428     ;; children.
8429     (while (setq d (pop data))
8430       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8431                 (and (setq children
8432                            (gnus-article-children (gnus-data-number d)))
8433                      (let (found)
8434                        (while children
8435                          (when (memq (car children) articles)
8436                            (setq children nil
8437                                  found t))
8438                          (pop children))
8439                        found)))
8440         (push (gnus-data-number d) articles)))
8441     ;; Do the limiting.
8442     (prog1
8443         (gnus-summary-limit articles)
8444       (gnus-summary-position-point))))
8445
8446 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8447   "Mark all unread excluded articles as read.
8448 If ALL, mark even excluded ticked and dormants as read."
8449   (interactive "P")
8450   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8451   (let ((articles (gnus-sorted-ndifference
8452                    (sort
8453                     (mapcar (lambda (h) (mail-header-number h))
8454                             gnus-newsgroup-headers)
8455                     '<)
8456                    gnus-newsgroup-limit))
8457         article)
8458     (setq gnus-newsgroup-unreads
8459           (gnus-sorted-intersection gnus-newsgroup-unreads
8460                                     gnus-newsgroup-limit))
8461     (if all
8462         (setq gnus-newsgroup-dormant nil
8463               gnus-newsgroup-marked nil
8464               gnus-newsgroup-reads
8465               (nconc
8466                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8467                gnus-newsgroup-reads))
8468       (while (setq article (pop articles))
8469         (unless (or (memq article gnus-newsgroup-dormant)
8470                     (memq article gnus-newsgroup-marked))
8471           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8472
8473 (defun gnus-summary-limit (articles &optional pop)
8474   (if pop
8475       ;; We pop the previous limit off the stack and use that.
8476       (setq articles (car gnus-newsgroup-limits)
8477             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8478     ;; We use the new limit, so we push the old limit on the stack.
8479     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8480   ;; Set the limit.
8481   (setq gnus-newsgroup-limit articles)
8482   (let ((total (length gnus-newsgroup-data))
8483         (data (gnus-data-find-list (gnus-summary-article-number)))
8484         (gnus-summary-mark-below nil)   ; Inhibit this.
8485         found)
8486     ;; This will do all the work of generating the new summary buffer
8487     ;; according to the new limit.
8488     (gnus-summary-prepare)
8489     ;; Hide any threads, possibly.
8490     (gnus-summary-maybe-hide-threads)
8491     ;; Try to return to the article you were at, or one in the
8492     ;; neighborhood.
8493     (when data
8494       ;; We try to find some article after the current one.
8495       (while data
8496         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8497           (setq data nil
8498                 found t))
8499         (setq data (cdr data))))
8500     (unless found
8501       ;; If there is no data, that means that we were after the last
8502       ;; article.  The same goes when we can't find any articles
8503       ;; after the current one.
8504       (goto-char (point-max))
8505       (gnus-summary-find-prev))
8506     (gnus-set-mode-line 'summary)
8507     ;; We return how many articles were removed from the summary
8508     ;; buffer as a result of the new limit.
8509     (- total (length gnus-newsgroup-data))))
8510
8511 (defsubst gnus-invisible-cut-children (threads)
8512   (let ((num 0))
8513     (while threads
8514       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8515         (incf num))
8516       (pop threads))
8517     (< num 2)))
8518
8519 (defsubst gnus-cut-thread (thread)
8520   "Go forwards in the thread until we find an article that we want to display."
8521   (when (or (eq gnus-fetch-old-headers 'some)
8522             (eq gnus-fetch-old-headers 'invisible)
8523             (numberp gnus-fetch-old-headers)
8524             (eq gnus-build-sparse-threads 'some)
8525             (eq gnus-build-sparse-threads 'more))
8526     ;; Deal with old-fetched headers and sparse threads.
8527     (while (and
8528             thread
8529             (or
8530              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8531              (gnus-summary-article-ancient-p
8532               (mail-header-number (car thread))))
8533             (if (or (<= (length (cdr thread)) 1)
8534                     (eq gnus-fetch-old-headers 'invisible))
8535                 (setq gnus-newsgroup-limit
8536                       (delq (mail-header-number (car thread))
8537                             gnus-newsgroup-limit)
8538                       thread (cadr thread))
8539               (when (gnus-invisible-cut-children (cdr thread))
8540                 (let ((th (cdr thread)))
8541                   (while th
8542                     (if (memq (mail-header-number (caar th))
8543                               gnus-newsgroup-limit)
8544                         (setq thread (car th)
8545                               th nil)
8546                       (setq th (cdr th))))))))))
8547   thread)
8548
8549 (defun gnus-cut-threads (threads)
8550   "Cut off all uninteresting articles from the beginning of THREADS."
8551   (when (or (eq gnus-fetch-old-headers 'some)
8552             (eq gnus-fetch-old-headers 'invisible)
8553             (numberp gnus-fetch-old-headers)
8554             (eq gnus-build-sparse-threads 'some)
8555             (eq gnus-build-sparse-threads 'more))
8556     (let ((th threads))
8557       (while th
8558         (setcar th (gnus-cut-thread (car th)))
8559         (setq th (cdr th)))))
8560   ;; Remove nixed out threads.
8561   (delq nil threads))
8562
8563 (defun gnus-summary-initial-limit (&optional show-if-empty)
8564   "Figure out what the initial limit is supposed to be on group entry.
8565 This entails weeding out unwanted dormants, low-scored articles,
8566 fetch-old-headers verbiage, and so on."
8567   ;; Most groups have nothing to remove.
8568   (if (or gnus-inhibit-limiting
8569           (and (null gnus-newsgroup-dormant)
8570                (eq gnus-newsgroup-display 'gnus-not-ignore)
8571                (not (eq gnus-fetch-old-headers 'some))
8572                (not (numberp gnus-fetch-old-headers))
8573                (not (eq gnus-fetch-old-headers 'invisible))
8574                (null gnus-summary-expunge-below)
8575                (not (eq gnus-build-sparse-threads 'some))
8576                (not (eq gnus-build-sparse-threads 'more))
8577                (null gnus-thread-expunge-below)
8578                (not gnus-use-nocem)))
8579       ()                                ; Do nothing.
8580     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8581     (setq gnus-newsgroup-limit nil)
8582     (mapatoms
8583      (lambda (node)
8584        (unless (car (symbol-value node))
8585          ;; These threads have no parents -- they are roots.
8586          (let ((nodes (cdr (symbol-value node)))
8587                thread)
8588            (while nodes
8589              (if (and gnus-thread-expunge-below
8590                       (< (gnus-thread-total-score (car nodes))
8591                          gnus-thread-expunge-below))
8592                  (gnus-expunge-thread (pop nodes))
8593                (setq thread (pop nodes))
8594                (gnus-summary-limit-children thread))))))
8595      gnus-newsgroup-dependencies)
8596     ;; If this limitation resulted in an empty group, we might
8597     ;; pop the previous limit and use it instead.
8598     (when (and (not gnus-newsgroup-limit)
8599                show-if-empty)
8600       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8601     gnus-newsgroup-limit))
8602
8603 (defun gnus-summary-limit-children (thread)
8604   "Return 1 if this subthread is visible and 0 if it is not."
8605   ;; First we get the number of visible children to this thread.  This
8606   ;; is done by recursing down the thread using this function, so this
8607   ;; will really go down to a leaf article first, before slowly
8608   ;; working its way up towards the root.
8609   (when thread
8610     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8611            (children
8612            (if (cdr thread)
8613                (apply '+ (mapcar 'gnus-summary-limit-children
8614                                  (cdr thread)))
8615              0))
8616           (number (mail-header-number (car thread)))
8617           score)
8618       (if (and
8619            (not (memq number gnus-newsgroup-marked))
8620            (or
8621             ;; If this article is dormant and has absolutely no visible
8622             ;; children, then this article isn't visible.
8623             (and (memq number gnus-newsgroup-dormant)
8624                  (zerop children))
8625             ;; If this is "fetch-old-headered" and there is no
8626             ;; visible children, then we don't want this article.
8627             (and (or (eq gnus-fetch-old-headers 'some)
8628                      (numberp gnus-fetch-old-headers))
8629                  (gnus-summary-article-ancient-p number)
8630                  (zerop children))
8631             ;; If this is "fetch-old-headered" and `invisible', then
8632             ;; we don't want this article.
8633             (and (eq gnus-fetch-old-headers 'invisible)
8634                  (gnus-summary-article-ancient-p number))
8635             ;; If this is a sparsely inserted article with no children,
8636             ;; we don't want it.
8637             (and (eq gnus-build-sparse-threads 'some)
8638                  (gnus-summary-article-sparse-p number)
8639                  (zerop children))
8640             ;; If we use expunging, and this article is really
8641             ;; low-scored, then we don't want this article.
8642             (when (and gnus-summary-expunge-below
8643                        (< (setq score
8644                                 (or (cdr (assq number gnus-newsgroup-scored))
8645                                     gnus-summary-default-score))
8646                           gnus-summary-expunge-below))
8647               ;; We increase the expunge-tally here, but that has
8648               ;; nothing to do with the limits, really.
8649               (incf gnus-newsgroup-expunged-tally)
8650               ;; We also mark as read here, if that's wanted.
8651               (when (and gnus-summary-mark-below
8652                          (< score gnus-summary-mark-below))
8653                 (setq gnus-newsgroup-unreads
8654                       (delq number gnus-newsgroup-unreads))
8655                 (if gnus-newsgroup-auto-expire
8656                     (push number gnus-newsgroup-expirable)
8657                   (push (cons number gnus-low-score-mark)
8658                         gnus-newsgroup-reads)))
8659               t)
8660             ;; Do the `display' group parameter.
8661             (and gnus-newsgroup-display
8662                  (not (funcall gnus-newsgroup-display)))
8663             ;; Check NoCeM things.
8664             (when (and gnus-use-nocem
8665                        (gnus-nocem-unwanted-article-p
8666                         (mail-header-id (car thread))))
8667               (setq gnus-newsgroup-unreads
8668                     (delq number gnus-newsgroup-unreads))
8669               t)))
8670           ;; Nope, invisible article.
8671           0
8672         ;; Ok, this article is to be visible, so we add it to the limit
8673         ;; and return 1.
8674         (push number gnus-newsgroup-limit)
8675         1))))
8676
8677 (defun gnus-expunge-thread (thread)
8678   "Mark all articles in THREAD as read."
8679   (let* ((number (mail-header-number (car thread))))
8680     (incf gnus-newsgroup-expunged-tally)
8681     ;; We also mark as read here, if that's wanted.
8682     (setq gnus-newsgroup-unreads
8683           (delq number gnus-newsgroup-unreads))
8684     (if gnus-newsgroup-auto-expire
8685         (push number gnus-newsgroup-expirable)
8686       (push (cons number gnus-low-score-mark)
8687             gnus-newsgroup-reads)))
8688   ;; Go recursively through all subthreads.
8689   (mapcar 'gnus-expunge-thread (cdr thread)))
8690
8691 ;; Summary article oriented commands
8692
8693 (defun gnus-summary-refer-parent-article (n)
8694   "Refer parent article N times.
8695 If N is negative, go to ancestor -N instead.
8696 The difference between N and the number of articles fetched is returned."
8697   (interactive "p")
8698   (let ((skip 1)
8699         error header ref)
8700     (when (not (natnump n))
8701       (setq skip (abs n)
8702             n 1))
8703     (while (and (> n 0)
8704                 (not error))
8705       (setq header (gnus-summary-article-header))
8706       (if (and (eq (mail-header-number header)
8707                    (cdr gnus-article-current))
8708                (equal gnus-newsgroup-name
8709                       (car gnus-article-current)))
8710           ;; If we try to find the parent of the currently
8711           ;; displayed article, then we take a look at the actual
8712           ;; References header, since this is slightly more
8713           ;; reliable than the References field we got from the
8714           ;; server.
8715           (save-excursion
8716             (set-buffer gnus-original-article-buffer)
8717             (nnheader-narrow-to-headers)
8718             (unless (setq ref (message-fetch-field "references"))
8719               (when (setq ref (message-fetch-field "in-reply-to"))
8720                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8721             (widen))
8722         (setq ref
8723               ;; It's not the current article, so we take a bet on
8724               ;; the value we got from the server.
8725               (mail-header-references header)))
8726       (if (and ref
8727                (not (equal ref "")))
8728           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8729             (gnus-message 1 "Couldn't find parent"))
8730         (gnus-message 1 "No references in article %d"
8731                       (gnus-summary-article-number))
8732         (setq error t))
8733       (decf n))
8734     (gnus-summary-position-point)
8735     n))
8736
8737 (defun gnus-summary-refer-references ()
8738   "Fetch all articles mentioned in the References header.
8739 Return the number of articles fetched."
8740   (interactive)
8741   (let ((ref (mail-header-references (gnus-summary-article-header)))
8742         (current (gnus-summary-article-number))
8743         (n 0))
8744     (if (or (not ref)
8745             (equal ref ""))
8746         (error "No References in the current article")
8747       ;; For each Message-ID in the References header...
8748       (while (string-match "<[^>]*>" ref)
8749         (incf n)
8750         ;; ... fetch that article.
8751         (gnus-summary-refer-article
8752          (prog1 (match-string 0 ref)
8753            (setq ref (substring ref (match-end 0))))))
8754       (gnus-summary-goto-subject current)
8755       (gnus-summary-position-point)
8756       n)))
8757
8758 (defun gnus-summary-refer-thread (&optional limit)
8759   "Fetch all articles in the current thread.
8760 If LIMIT (the numerical prefix), fetch that many old headers instead
8761 of what's specified by the `gnus-refer-thread-limit' variable."
8762   (interactive "P")
8763   (let ((id (mail-header-id (gnus-summary-article-header)))
8764         (limit (if limit (prefix-numeric-value limit)
8765                  gnus-refer-thread-limit)))
8766     (unless (eq gnus-fetch-old-headers 'invisible)
8767       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8768       ;; Retrieve the headers and read them in.
8769       (if (eq (if (numberp limit)
8770                   (gnus-retrieve-headers
8771                    (list (min
8772                           (+ (mail-header-number
8773                               (gnus-summary-article-header))
8774                              limit)
8775                           gnus-newsgroup-end))
8776                    gnus-newsgroup-name (* limit 2))
8777                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8778                 ;; headers.
8779                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8780                                        gnus-newsgroup-name limit))
8781               'nov)
8782           (gnus-build-all-threads)
8783         (error "Can't fetch thread from back ends that don't support NOV"))
8784       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8785     (gnus-summary-limit-include-thread id)))
8786
8787 (defun gnus-summary-refer-article (message-id)
8788   "Fetch an article specified by MESSAGE-ID."
8789   (interactive "sMessage-ID: ")
8790   (when (and (stringp message-id)
8791              (not (zerop (length message-id))))
8792     (setq message-id (gnus-replace-in-string message-id " " ""))
8793     ;; Construct the correct Message-ID if necessary.
8794     ;; Suggested by tale@pawl.rpi.edu.
8795     (unless (string-match "^<" message-id)
8796       (setq message-id (concat "<" message-id)))
8797     (unless (string-match ">$" message-id)
8798       (setq message-id (concat message-id ">")))
8799     ;; People often post MIDs from URLs, so unhex it:
8800     (unless (string-match "@" message-id)
8801       (setq message-id (gnus-url-unhex-string message-id)))
8802     (let* ((header (gnus-id-to-header message-id))
8803            (sparse (and header
8804                         (gnus-summary-article-sparse-p
8805                          (mail-header-number header))
8806                         (memq (mail-header-number header)
8807                               gnus-newsgroup-limit)))
8808            number)
8809       (cond
8810        ;; If the article is present in the buffer we just go to it.
8811        ((and header
8812              (or (not (gnus-summary-article-sparse-p
8813                        (mail-header-number header)))
8814                  sparse))
8815         (prog1
8816             (gnus-summary-goto-article
8817              (mail-header-number header) nil t)
8818           (when sparse
8819             (gnus-summary-update-article (mail-header-number header)))))
8820        (t
8821         ;; We fetch the article.
8822         (catch 'found
8823           (dolist (gnus-override-method (gnus-refer-article-methods))
8824             (when (and (gnus-check-server gnus-override-method)
8825                        ;; Fetch the header,
8826                        (setq number (gnus-summary-insert-subject message-id)))
8827               ;; and display the article.
8828               (gnus-summary-select-article nil nil nil number)
8829               (throw 'found t)))
8830           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8831
8832 (defun gnus-refer-article-methods ()
8833   "Return a list of referable methods."
8834   (cond
8835    ;; No method, so we default to current and native.
8836    ((null gnus-refer-article-method)
8837     (list gnus-current-select-method gnus-select-method))
8838    ;; Current.
8839    ((eq 'current gnus-refer-article-method)
8840     (list gnus-current-select-method))
8841    ;; List of select methods.
8842    ((not (and (symbolp (car gnus-refer-article-method))
8843               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8844     (let (out)
8845       (dolist (method gnus-refer-article-method)
8846         (push (if (eq 'current method)
8847                   gnus-current-select-method
8848                 method)
8849               out))
8850       (nreverse out)))
8851    ;; One single select method.
8852    (t
8853     (list gnus-refer-article-method))))
8854
8855 (defun gnus-summary-edit-parameters ()
8856   "Edit the group parameters of the current group."
8857   (interactive)
8858   (gnus-group-edit-group gnus-newsgroup-name 'params))
8859
8860 (defun gnus-summary-customize-parameters ()
8861   "Customize the group parameters of the current group."
8862   (interactive)
8863   (gnus-group-customize gnus-newsgroup-name))
8864
8865 (defun gnus-summary-enter-digest-group (&optional force)
8866   "Enter an nndoc group based on the current article.
8867 If FORCE, force a digest interpretation.  If not, try
8868 to guess what the document format is."
8869   (interactive "P")
8870   (let ((conf gnus-current-window-configuration))
8871     (save-window-excursion
8872       (save-excursion
8873         (let (gnus-article-prepare-hook
8874               gnus-display-mime-function
8875               gnus-break-pages)
8876           (gnus-summary-select-article))))
8877     (setq gnus-current-window-configuration conf)
8878     (let* ((name (format "%s-%d"
8879                          (gnus-group-prefixed-name
8880                           gnus-newsgroup-name (list 'nndoc ""))
8881                          (with-current-buffer gnus-summary-buffer
8882                            gnus-current-article)))
8883            (ogroup gnus-newsgroup-name)
8884            (params (append (gnus-info-params (gnus-get-info ogroup))
8885                            (list (cons 'to-group ogroup))
8886                            (list (cons 'parent-group ogroup))
8887                            (list (cons 'save-article-group ogroup))))
8888            (case-fold-search t)
8889            (buf (current-buffer))
8890            dig to-address)
8891       (save-excursion
8892         (set-buffer gnus-original-article-buffer)
8893         ;; Have the digest group inherit the main mail address of
8894         ;; the parent article.
8895         (when (setq to-address (or (gnus-fetch-field "reply-to")
8896                                    (gnus-fetch-field "from")))
8897           (setq params
8898                 (append
8899                  (list (cons 'to-address
8900                              (funcall gnus-decode-encoded-address-function
8901                                       to-address))))))
8902         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8903         (insert-buffer-substring gnus-original-article-buffer)
8904         ;; Remove lines that may lead nndoc to misinterpret the
8905         ;; document type.
8906         (narrow-to-region
8907          (goto-char (point-min))
8908          (or (search-forward "\n\n" nil t) (point)))
8909         (goto-char (point-min))
8910         (delete-matching-lines "^Path:\\|^From ")
8911         (widen))
8912       (unwind-protect
8913           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8914                     (gnus-newsgroup-ephemeral-ignored-charsets
8915                      gnus-newsgroup-ignored-charsets))
8916                 (gnus-group-read-ephemeral-group
8917                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8918                               (nndoc-article-type
8919                                ,(if force 'mbox 'guess)))
8920                  t nil nil nil
8921                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8922                                                         "ADAPT")))))
8923               ;; Make all postings to this group go to the parent group.
8924               (nconc (gnus-info-params (gnus-get-info name))
8925                      params)
8926             ;; Couldn't select this doc group.
8927             (switch-to-buffer buf)
8928             (gnus-set-global-variables)
8929             (gnus-configure-windows 'summary)
8930             (gnus-message 3 "Article couldn't be entered?"))
8931         (kill-buffer dig)))))
8932
8933 (defun gnus-summary-read-document (n)
8934   "Open a new group based on the current article(s).
8935 This will allow you to read digests and other similar
8936 documents as newsgroups.
8937 Obeys the standard process/prefix convention."
8938   (interactive "P")
8939   (let* ((ogroup gnus-newsgroup-name)
8940          (params (append (gnus-info-params (gnus-get-info ogroup))
8941                          (list (cons 'to-group ogroup))))
8942          group egroup groups vgroup)
8943     (dolist (article (gnus-summary-work-articles n))
8944       (setq group (format "%s-%d" gnus-newsgroup-name article))
8945       (gnus-summary-remove-process-mark article)
8946       (when (gnus-summary-display-article article)
8947         (save-excursion
8948           (with-temp-buffer
8949             (insert-buffer-substring gnus-original-article-buffer)
8950             ;; Remove some headers that may lead nndoc to make
8951             ;; the wrong guess.
8952             (message-narrow-to-head)
8953             (goto-char (point-min))
8954             (delete-matching-lines "^Path:\\|^From ")
8955             (widen)
8956             (if (setq egroup
8957                       (gnus-group-read-ephemeral-group
8958                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8959                                      (nndoc-article-type guess))
8960                        t nil t))
8961                 (progn
8962             ;; Make all postings to this group go to the parent group.
8963                   (nconc (gnus-info-params (gnus-get-info egroup))
8964                          params)
8965                   (push egroup groups))
8966               ;; Couldn't select this doc group.
8967               (gnus-error 3 "Article couldn't be entered"))))))
8968     ;; Now we have selected all the documents.
8969     (cond
8970      ((not groups)
8971       (error "None of the articles could be interpreted as documents"))
8972      ((gnus-group-read-ephemeral-group
8973        (setq vgroup (format
8974                      "nnvirtual:%s-%s" gnus-newsgroup-name
8975                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8976        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8977        t
8978        (cons (current-buffer) 'summary)))
8979      (t
8980       (error "Couldn't select virtual nndoc group")))))
8981
8982 (defun gnus-summary-isearch-article (&optional regexp-p)
8983   "Do incremental search forward on the current article.
8984 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8985   (interactive "P")
8986   (gnus-summary-select-article)
8987   (gnus-configure-windows 'article)
8988   (gnus-eval-in-buffer-window gnus-article-buffer
8989     (save-restriction
8990       (widen)
8991       (isearch-forward regexp-p))))
8992
8993 (defun gnus-summary-repeat-search-article-forward ()
8994   "Repeat the previous search forwards."
8995   (interactive)
8996   (unless gnus-last-search-regexp
8997     (error "No previous search"))
8998   (gnus-summary-search-article-forward gnus-last-search-regexp))
8999
9000 (defun gnus-summary-repeat-search-article-backward ()
9001   "Repeat the previous search backwards."
9002   (interactive)
9003   (unless gnus-last-search-regexp
9004     (error "No previous search"))
9005   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9006
9007 (defun gnus-summary-search-article-forward (regexp &optional backward)
9008   "Search for an article containing REGEXP forward.
9009 If BACKWARD, search backward instead."
9010   (interactive
9011    (list (read-string
9012           (format "Search article %s (regexp%s): "
9013                   (if current-prefix-arg "backward" "forward")
9014                   (if gnus-last-search-regexp
9015                       (concat ", default " gnus-last-search-regexp)
9016                     "")))
9017          current-prefix-arg))
9018   (if (string-equal regexp "")
9019       (setq regexp (or gnus-last-search-regexp ""))
9020     (setq gnus-last-search-regexp regexp)
9021     (setq gnus-article-before-search gnus-current-article))
9022   ;; Intentionally set gnus-last-article.
9023   (setq gnus-last-article gnus-article-before-search)
9024   (let ((gnus-last-article gnus-last-article))
9025     (if (gnus-summary-search-article regexp backward)
9026         (gnus-summary-show-thread)
9027       (signal 'search-failed (list regexp)))))
9028
9029 (defun gnus-summary-search-article-backward (regexp)
9030   "Search for an article containing REGEXP backward."
9031   (interactive
9032    (list (read-string
9033           (format "Search article backward (regexp%s): "
9034                   (if gnus-last-search-regexp
9035                       (concat ", default " gnus-last-search-regexp)
9036                     "")))))
9037   (gnus-summary-search-article-forward regexp 'backward))
9038
9039 (defun gnus-summary-search-article (regexp &optional backward)
9040   "Search for an article containing REGEXP.
9041 Optional argument BACKWARD means do search for backward.
9042 `gnus-select-article-hook' is not called during the search."
9043   ;; We have to require this here to make sure that the following
9044   ;; dynamic binding isn't shadowed by autoloading.
9045   (require 'gnus-async)
9046   (require 'gnus-art)
9047   (let ((gnus-select-article-hook nil)  ;Disable hook.
9048         (gnus-article-prepare-hook nil)
9049         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9050         (gnus-use-article-prefetch nil)
9051         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9052         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9053         (gnus-visual nil)
9054         (gnus-keep-backlog nil)
9055         (gnus-break-pages nil)
9056         (gnus-summary-display-arrow nil)
9057         (gnus-updated-mode-lines nil)
9058         (gnus-auto-center-summary nil)
9059         (sum (current-buffer))
9060         (gnus-display-mime-function nil)
9061         (found nil)
9062         point)
9063     (gnus-save-hidden-threads
9064       (gnus-summary-select-article)
9065       (set-buffer gnus-article-buffer)
9066       (goto-char (window-point (get-buffer-window (current-buffer))))
9067       (when backward
9068         (forward-line -1))
9069       (while (not found)
9070         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9071         (if (if backward
9072                 (re-search-backward regexp nil t)
9073               (re-search-forward regexp nil t))
9074             ;; We found the regexp.
9075             (progn
9076               (setq found 'found)
9077               (beginning-of-line)
9078               (set-window-start
9079                (get-buffer-window (current-buffer))
9080                (point))
9081               (forward-line 1)
9082               (set-window-point
9083                (get-buffer-window (current-buffer))
9084                (point))
9085               (set-buffer sum)
9086               (setq point (point)))
9087           ;; We didn't find it, so we go to the next article.
9088           (set-buffer sum)
9089           (setq found 'not)
9090           (while (eq found 'not)
9091             (if (not (if backward (gnus-summary-find-prev)
9092                        (gnus-summary-find-next)))
9093                 ;; No more articles.
9094                 (setq found t)
9095               ;; Select the next article and adjust point.
9096               (unless (gnus-summary-article-sparse-p
9097                        (gnus-summary-article-number))
9098                 (setq found nil)
9099                 (gnus-summary-select-article)
9100                 (set-buffer gnus-article-buffer)
9101                 (widen)
9102                 (goto-char (if backward (point-max) (point-min))))))))
9103       (gnus-message 7 ""))
9104     ;; Return whether we found the regexp.
9105     (when (eq found 'found)
9106       (goto-char point)
9107       (gnus-summary-show-thread)
9108       (gnus-summary-goto-subject gnus-current-article)
9109       (gnus-summary-position-point)
9110       t)))
9111
9112 (defun gnus-find-matching-articles (header regexp)
9113   "Return a list of all articles that match REGEXP on HEADER.
9114 This search includes all articles in the current group that Gnus has
9115 fetched headers for, whether they are displayed or not."
9116   (let ((articles nil)
9117         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9118         (case-fold-search t))
9119     (dolist (header gnus-newsgroup-headers)
9120       (when (string-match regexp (funcall func header))
9121         (push (mail-header-number header) articles)))
9122     (nreverse articles)))
9123
9124 (defun gnus-summary-find-matching (header regexp &optional backward unread
9125                                           not-case-fold not-matching)
9126   "Return a list of all articles that match REGEXP on HEADER.
9127 The search stars on the current article and goes forwards unless
9128 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9129 If UNREAD is non-nil, only unread articles will
9130 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9131 in the comparisons. If NOT-MATCHING, return a list of all articles that
9132 not match REGEXP on HEADER."
9133   (let ((case-fold-search (not not-case-fold))
9134         articles d func)
9135     (if (consp header)
9136         (if (eq (car header) 'extra)
9137             (setq func
9138                   `(lambda (h)
9139                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9140                          "")))
9141           (error "%s is an invalid header" header))
9142       (unless (fboundp (intern (concat "mail-header-" header)))
9143         (error "%s is not a valid header" header))
9144       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9145     (dolist (d (if (eq backward 'all)
9146                    gnus-newsgroup-data
9147                  (gnus-data-find-list
9148                   (gnus-summary-article-number)
9149                   (gnus-data-list backward))))
9150       (when (and (or (not unread)       ; We want all articles...
9151                      (gnus-data-unread-p d)) ; Or just unreads.
9152                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9153                  (if not-matching
9154                      (not (string-match
9155                            regexp
9156                            (funcall func (gnus-data-header d))))
9157                    (string-match regexp
9158                                  (funcall func (gnus-data-header d)))))
9159         (push (gnus-data-number d) articles))) ; Success!
9160     (nreverse articles)))
9161
9162 (defun gnus-summary-execute-command (header regexp command &optional backward)
9163   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9164 If HEADER is an empty string (or nil), the match is done on the entire
9165 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9166   (interactive
9167    (list (let ((completion-ignore-case t))
9168            (completing-read
9169             "Header name: "
9170             (mapcar (lambda (header) (list (format "%s" header)))
9171                     (append
9172                      '("Number" "Subject" "From" "Lines" "Date"
9173                        "Message-ID" "Xref" "References" "Body")
9174                      gnus-extra-headers))
9175             nil 'require-match))
9176          (read-string "Regexp: ")
9177          (read-key-sequence "Command: ")
9178          current-prefix-arg))
9179   (when (equal header "Body")
9180     (setq header ""))
9181   ;; Hidden thread subtrees must be searched as well.
9182   (gnus-summary-show-all-threads)
9183   ;; We don't want to change current point nor window configuration.
9184   (save-excursion
9185     (save-window-excursion
9186       (let (gnus-visual
9187             gnus-treat-strip-trailing-blank-lines
9188             gnus-treat-strip-leading-blank-lines
9189             gnus-treat-strip-multiple-blank-lines
9190             gnus-treat-hide-boring-headers
9191             gnus-treat-fold-newsgroups
9192             gnus-article-prepare-hook)
9193         (gnus-message 6 "Executing %s..." (key-description command))
9194         ;; We'd like to execute COMMAND interactively so as to give arguments.
9195         (gnus-execute header regexp
9196                       `(call-interactively ',(key-binding command))
9197                       backward)
9198         (gnus-message 6 "Executing %s...done" (key-description command))))))
9199
9200 (defun gnus-summary-beginning-of-article ()
9201   "Scroll the article back to the beginning."
9202   (interactive)
9203   (gnus-summary-select-article)
9204   (gnus-configure-windows 'article)
9205   (gnus-eval-in-buffer-window gnus-article-buffer
9206     (widen)
9207     (goto-char (point-min))
9208     (when gnus-break-pages
9209       (gnus-narrow-to-page))))
9210
9211 (defun gnus-summary-end-of-article ()
9212   "Scroll to the end of the article."
9213   (interactive)
9214   (gnus-summary-select-article)
9215   (gnus-configure-windows 'article)
9216   (gnus-eval-in-buffer-window gnus-article-buffer
9217     (widen)
9218     (goto-char (point-max))
9219     (recenter -3)
9220     (when gnus-break-pages
9221       (when (re-search-backward page-delimiter nil t)
9222         (narrow-to-region (match-end 0) (point-max)))
9223       (gnus-narrow-to-page))))
9224
9225 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9226   "Truncate to LEN and quote all \"(\"'s in STRING."
9227   (gnus-replace-in-string (if (and len (> (length string) len))
9228                               (substring string 0 len)
9229                             string)
9230                           "[()]" "\\\\\\&"))
9231
9232 (defun gnus-summary-print-article (&optional filename n)
9233   "Generate and print a PostScript image of the process-marked (mail) articles.
9234
9235 If used interactively, print the current article if none are
9236 process-marked.  With prefix arg, prompt the user for the name of the
9237 file to save in.
9238
9239 When used from Lisp, accept two optional args FILENAME and N.  N means
9240 to print the next N articles.  If N is negative, print the N previous
9241 articles.  If N is nil and articles have been marked with the process
9242 mark, print these instead.
9243
9244 If the optional first argument FILENAME is nil, send the image to the
9245 printer.  If FILENAME is a string, save the PostScript image in a file with
9246 that name.  If FILENAME is a number, prompt the user for the name of the file
9247 to save in."
9248   (interactive (list (ps-print-preprint current-prefix-arg)))
9249   (dolist (article (gnus-summary-work-articles n))
9250     (gnus-summary-select-article nil nil 'pseudo article)
9251     (gnus-eval-in-buffer-window gnus-article-buffer
9252       (gnus-print-buffer))
9253     (gnus-summary-remove-process-mark article))
9254   (ps-despool filename))
9255
9256 (defun gnus-print-buffer ()
9257   (let ((buffer (generate-new-buffer " *print*")))
9258     (unwind-protect
9259         (progn
9260           (copy-to-buffer buffer (point-min) (point-max))
9261           (set-buffer buffer)
9262           (gnus-remove-text-with-property 'gnus-decoration)
9263           (when (gnus-visual-p 'article-highlight 'highlight)
9264             ;; Copy-to-buffer doesn't copy overlay.  So redo
9265             ;; highlight.
9266             (let ((gnus-article-buffer buffer))
9267               (gnus-article-highlight-citation t)
9268               (gnus-article-highlight-signature)
9269               (gnus-article-emphasize)
9270               (gnus-article-delete-invisible-text)))
9271           (let ((ps-left-header
9272                  (list
9273                   (concat "("
9274                           (gnus-summary-print-truncate-and-quote
9275                            (mail-header-subject gnus-current-headers)
9276                            66) ")")
9277                   (concat "("
9278                           (gnus-summary-print-truncate-and-quote
9279                            (mail-header-from gnus-current-headers)
9280                            45) ")")))
9281                 (ps-right-header
9282                  (list
9283                   "/pagenumberstring load"
9284                   (concat "("
9285                           (mail-header-date gnus-current-headers) ")"))))
9286             (gnus-run-hooks 'gnus-ps-print-hook)
9287             (save-excursion
9288               (if window-system
9289                   (ps-spool-buffer-with-faces)
9290                 (ps-spool-buffer)))))
9291       (kill-buffer buffer))))
9292
9293 (defun gnus-summary-show-article (&optional arg)
9294   "Force redisplaying of the current article.
9295 If ARG (the prefix) is a number, show the article with the charset
9296 defined in `gnus-summary-show-article-charset-alist', or the charset
9297 input.
9298 If ARG (the prefix) is non-nil and not a number, show the raw article
9299 without any article massaging functions being run.  Normally, the key
9300 strokes are `C-u g'."
9301   (interactive "P")
9302   (cond
9303    ((numberp arg)
9304     (gnus-summary-show-article t)
9305     (let ((gnus-newsgroup-charset
9306            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9307                (mm-read-coding-system
9308                 "View as charset: " ;; actually it is coding system.
9309                 (with-current-buffer gnus-article-buffer
9310                   (mm-detect-coding-region (point) (point-max))))))
9311           (gnus-newsgroup-ignored-charsets 'gnus-all))
9312       (gnus-summary-select-article nil 'force)
9313       (let ((deps gnus-newsgroup-dependencies)
9314             head header lines)
9315         (save-excursion
9316           (set-buffer gnus-original-article-buffer)
9317           (save-restriction
9318             (message-narrow-to-head)
9319             (setq head (buffer-string))
9320             (goto-char (point-min))
9321             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9322               (goto-char (point-max))
9323               (widen)
9324               (setq lines (1- (count-lines (point) (point-max))))))
9325           (with-temp-buffer
9326             (insert (format "211 %d Article retrieved.\n"
9327                             (cdr gnus-article-current)))
9328             (insert head)
9329             (if lines (insert (format "Lines: %d\n" lines)))
9330             (insert ".\n")
9331             (let ((nntp-server-buffer (current-buffer)))
9332               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9333         (gnus-data-set-header
9334          (gnus-data-find (cdr gnus-article-current))
9335          header)
9336         (gnus-summary-update-article-line
9337          (cdr gnus-article-current) header)
9338         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9339           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9340    ((not arg)
9341     ;; Select the article the normal way.
9342     (gnus-summary-select-article nil 'force))
9343    (t
9344     ;; We have to require this here to make sure that the following
9345     ;; dynamic binding isn't shadowed by autoloading.
9346     (require 'gnus-async)
9347     (require 'gnus-art)
9348     ;; Bind the article treatment functions to nil.
9349     (let ((gnus-have-all-headers t)
9350           gnus-article-prepare-hook
9351           gnus-article-decode-hook
9352           gnus-display-mime-function
9353           gnus-break-pages)
9354       ;; Destroy any MIME parts.
9355       (when (gnus-buffer-live-p gnus-article-buffer)
9356         (save-excursion
9357           (set-buffer gnus-article-buffer)
9358           (mm-destroy-parts gnus-article-mime-handles)
9359           ;; Set it to nil for safety reason.
9360           (setq gnus-article-mime-handle-alist nil)
9361           (setq gnus-article-mime-handles nil)))
9362       (gnus-summary-select-article nil 'force))))
9363   (gnus-summary-goto-subject gnus-current-article)
9364   (gnus-summary-position-point))
9365
9366 (defun gnus-summary-show-raw-article ()
9367   "Show the raw article without any article massaging functions being run."
9368   (interactive)
9369   (gnus-summary-show-article t))
9370
9371 (defun gnus-summary-verbose-headers (&optional arg)
9372   "Toggle permanent full header display.
9373 If ARG is a positive number, turn header display on.
9374 If ARG is a negative number, turn header display off."
9375   (interactive "P")
9376   (setq gnus-show-all-headers
9377         (cond ((or (not (numberp arg))
9378                    (zerop arg))
9379                (not gnus-show-all-headers))
9380               ((natnump arg)
9381                t)))
9382   (gnus-summary-show-article))
9383
9384 (defun gnus-summary-toggle-header (&optional arg)
9385   "Show the headers if they are hidden, or hide them if they are shown.
9386 If ARG is a positive number, show the entire header.
9387 If ARG is a negative number, hide the unwanted header lines."
9388   (interactive "P")
9389   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9390                      (get-buffer-window gnus-article-buffer t))))
9391     (with-current-buffer gnus-article-buffer
9392       (widen)
9393       (article-narrow-to-head)
9394       (let* ((buffer-read-only nil)
9395              (inhibit-point-motion-hooks t)
9396              (hidden (if (numberp arg)
9397                          (>= arg 0)
9398                        (or (not (looking-at "[^ \t\n]+:"))
9399                            (gnus-article-hidden-text-p 'headers))))
9400              s e)
9401         (delete-region (point-min) (point-max))
9402         (with-current-buffer gnus-original-article-buffer
9403           (goto-char (setq s (point-min)))
9404           (setq e (if (search-forward "\n\n" nil t)
9405                       (1- (point))
9406                     (point-max))))
9407         (insert-buffer-substring gnus-original-article-buffer s e)
9408         (run-hooks 'gnus-article-decode-hook)
9409         (if hidden
9410             (let ((gnus-treat-hide-headers nil)
9411                   (gnus-treat-hide-boring-headers nil))
9412               (gnus-delete-wash-type 'headers)
9413               (gnus-treat-article 'head))
9414           (gnus-treat-article 'head))
9415         (widen)
9416         (if window
9417             (set-window-start window (goto-char (point-min))))
9418         (if gnus-break-pages
9419             (gnus-narrow-to-page)
9420           (when (gnus-visual-p 'page-marker)
9421             (let ((buffer-read-only nil))
9422               (gnus-remove-text-with-property 'gnus-prev)
9423               (gnus-remove-text-with-property 'gnus-next))))
9424         (gnus-set-mode-line 'article)))))
9425
9426 (defun gnus-summary-show-all-headers ()
9427   "Make all header lines visible."
9428   (interactive)
9429   (gnus-summary-toggle-header 1))
9430
9431 (defun gnus-summary-caesar-message (&optional arg)
9432   "Caesar rotate the current article by 13.
9433 With a non-numerical prefix, also rotate headers.  A numerical
9434 prefix specifies how many places to rotate each letter forward."
9435   (interactive "P")
9436   (gnus-summary-select-article)
9437   (let ((mail-header-separator ""))
9438     (gnus-eval-in-buffer-window gnus-article-buffer
9439       (save-restriction
9440         (widen)
9441         (let ((start (window-start))
9442               buffer-read-only)
9443           (if (equal arg '(4))
9444               (message-caesar-buffer-body nil t)
9445             (message-caesar-buffer-body arg))
9446           (set-window-start (get-buffer-window (current-buffer)) start)))))
9447   ;; Create buttons and stuff...
9448   (gnus-treat-article nil))
9449
9450 (declare-function idna-to-unicode "ext:idna" (str))
9451
9452 (defun gnus-summary-idna-message (&optional arg)
9453   "Decode IDNA encoded domain names in the current articles.
9454 IDNA encoded domain names looks like `xn--bar'.  If a string
9455 remain unencoded after running this function, it is likely an
9456 invalid IDNA string (`xn--bar' is invalid).
9457
9458 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9459 installed for this command to work."
9460   (interactive "P")
9461   (if (not (and (condition-case nil (require 'idna)
9462                   (file-error))
9463                 (mm-coding-system-p 'utf-8)
9464                 (executable-find (symbol-value 'idna-program))))
9465       (gnus-message
9466        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9467     (gnus-summary-select-article)
9468     (let ((mail-header-separator ""))
9469       (gnus-eval-in-buffer-window gnus-article-buffer
9470         (save-restriction
9471           (widen)
9472           (let ((start (window-start))
9473                 buffer-read-only)
9474             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9475               (replace-match (idna-to-unicode (match-string 1))))
9476             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9477
9478 (defun gnus-summary-morse-message (&optional arg)
9479   "Morse decode the current article."
9480   (interactive "P")
9481   (gnus-summary-select-article)
9482   (let ((mail-header-separator ""))
9483     (gnus-eval-in-buffer-window gnus-article-buffer
9484       (save-excursion
9485         (save-restriction
9486           (widen)
9487           (let ((pos (window-start))
9488                 buffer-read-only)
9489             (goto-char (point-min))
9490             (when (message-goto-body)
9491               (gnus-narrow-to-body))
9492             (goto-char (point-min))
9493             (while (search-forward "·" (point-max) t)
9494               (replace-match "."))
9495             (unmorse-region (point-min) (point-max))
9496             (widen)
9497             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9498
9499 (defun gnus-summary-stop-page-breaking ()
9500   "Stop page breaking in the current article."
9501   (interactive)
9502   (gnus-summary-select-article)
9503   (gnus-eval-in-buffer-window gnus-article-buffer
9504     (widen)
9505     (when (gnus-visual-p 'page-marker)
9506       (let ((buffer-read-only nil))
9507         (gnus-remove-text-with-property 'gnus-prev)
9508         (gnus-remove-text-with-property 'gnus-next))
9509       (setq gnus-page-broken nil))))
9510
9511 (defun gnus-summary-move-article (&optional n to-newsgroup
9512                                             select-method action)
9513   "Move the current article to a different newsgroup.
9514 If N is a positive number, move the N next articles.
9515 If N is a negative number, move the N previous articles.
9516 If N is nil and any articles have been marked with the process mark,
9517 move those articles instead.
9518 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9519 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9520 re-spool using this method.
9521
9522 When called interactively with TO-NEWSGROUP being nil, the value of
9523 the variable `gnus-move-split-methods' is used for finding a default
9524 for the target newsgroup.
9525
9526 For this function to work, both the current newsgroup and the
9527 newsgroup that you want to move to have to support the `request-move'
9528 and `request-accept' functions.
9529
9530 ACTION can be either `move' (the default), `crosspost' or `copy'."
9531   (interactive "P")
9532   (unless action
9533     (setq action 'move))
9534   ;; Check whether the source group supports the required functions.
9535   (cond ((and (eq action 'move)
9536               (not (gnus-check-backend-function
9537                     'request-move-article gnus-newsgroup-name)))
9538          (error "The current group does not support article moving"))
9539         ((and (eq action 'crosspost)
9540               (not (gnus-check-backend-function
9541                     'request-replace-article gnus-newsgroup-name)))
9542          (error "The current group does not support article editing")))
9543   (let ((articles (gnus-summary-work-articles n))
9544         (prefix (if (gnus-check-backend-function
9545                      'request-move-article gnus-newsgroup-name)
9546                     (funcall gnus-move-group-prefix-function
9547                              gnus-newsgroup-name)
9548                   ""))
9549         (names '((move "Move" "Moving")
9550                  (copy "Copy" "Copying")
9551                  (crosspost "Crosspost" "Crossposting")))
9552         (copy-buf (save-excursion
9553                     (nnheader-set-temp-buffer " *copy article*")))
9554         art-group to-method new-xref article to-groups
9555         articles-to-update-marks encoded)
9556     (unless (assq action names)
9557       (error "Unknown action %s" action))
9558     ;; Read the newsgroup name.
9559     (when (and (not to-newsgroup)
9560                (not select-method))
9561       (if (and gnus-move-split-methods
9562                (not
9563                 (and (memq gnus-current-article articles)
9564                      (gnus-buffer-live-p gnus-original-article-buffer))))
9565           ;; When `gnus-move-split-methods' is non-nil, we have to
9566           ;; select an article to give `gnus-read-move-group-name' an
9567           ;; opportunity to suggest an appropriate default.  However,
9568           ;; we needn't render or mark the article.
9569           (let ((gnus-display-mime-function nil)
9570                 (gnus-article-prepare-hook nil)
9571                 (gnus-mark-article-hook nil))
9572             (gnus-summary-select-article nil nil nil (car articles))))
9573       (setq to-newsgroup (gnus-read-move-group-name
9574                           (cadr (assq action names))
9575                           (symbol-value
9576                            (intern (format "gnus-current-%s-group" action)))
9577                           articles prefix)
9578             encoded to-newsgroup
9579             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9580       (set (intern (format "gnus-current-%s-group" action))
9581            (mm-decode-coding-string
9582             to-newsgroup
9583             (gnus-group-name-charset to-method to-newsgroup))))
9584     (unless to-method
9585       (setq to-method (or select-method
9586                           (gnus-server-to-method
9587                            (gnus-group-method to-newsgroup)))))
9588     (setq to-newsgroup
9589           (or encoded
9590               (and to-newsgroup
9591                    (mm-encode-coding-string
9592                     to-newsgroup
9593                     (gnus-group-name-charset to-method to-newsgroup)))))
9594     ;; Check the method we are to move this article to...
9595     (unless (gnus-check-backend-function
9596              'request-accept-article (car to-method))
9597       (error "%s does not support article copying" (car to-method)))
9598     (unless (gnus-check-server to-method)
9599       (error "Can't open server %s" (car to-method)))
9600     (gnus-message 6 "%s to %s: %s..."
9601                   (caddr (assq action names))
9602                   (or (car select-method)
9603                       (gnus-group-decoded-name to-newsgroup))
9604                   articles)
9605     (while articles
9606       (setq article (pop articles))
9607       (setq
9608        art-group
9609        (cond
9610         ;; Move the article.
9611         ((eq action 'move)
9612          ;; Remove this article from future suppression.
9613          (gnus-dup-unsuppress-article article)
9614          (let* ((from-method (gnus-find-method-for-group
9615                               gnus-newsgroup-name))
9616                 (to-method (or select-method
9617                                (gnus-find-method-for-group to-newsgroup)))
9618                 (move-is-internal (gnus-method-equal from-method to-method)))
9619            (gnus-request-move-article
9620             article                     ; Article to move
9621             gnus-newsgroup-name         ; From newsgroup
9622             (nth 1 (gnus-find-method-for-group
9623                     gnus-newsgroup-name)) ; Server
9624             (list 'gnus-request-accept-article
9625                   to-newsgroup (list 'quote select-method)
9626                   (not articles) t)     ; Accept form
9627             (not articles)              ; Only save nov last time
9628             move-is-internal)))         ; is this move internal?
9629         ;; Copy the article.
9630         ((eq action 'copy)
9631          (save-excursion
9632            (set-buffer copy-buf)
9633            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9634              (save-restriction
9635                (nnheader-narrow-to-headers)
9636                (dolist (hdr gnus-copy-article-ignored-headers)
9637                  (message-remove-header hdr t)))
9638              (gnus-request-accept-article
9639               to-newsgroup select-method (not articles) t))))
9640         ;; Crosspost the article.
9641         ((eq action 'crosspost)
9642          (let ((xref (message-tokenize-header
9643                       (mail-header-xref (gnus-summary-article-header article))
9644                       " ")))
9645            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9646                                   ":" (number-to-string article)))
9647            (unless xref
9648              (setq xref (list (system-name))))
9649            (setq new-xref
9650                  (concat
9651                   (mapconcat 'identity
9652                              (delete "Xref:" (delete new-xref xref))
9653                              " ")
9654                   " " new-xref))
9655            (save-excursion
9656              (set-buffer copy-buf)
9657              ;; First put the article in the destination group.
9658              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9659              (when (consp (setq art-group
9660                                 (gnus-request-accept-article
9661                                  to-newsgroup select-method (not articles) t)))
9662                (setq new-xref (concat new-xref " " (car art-group)
9663                                       ":"
9664                                       (number-to-string (cdr art-group))))
9665                ;; Now we have the new Xrefs header, so we insert
9666                ;; it and replace the new article.
9667                (nnheader-replace-header "Xref" new-xref)
9668                (gnus-request-replace-article
9669                 (cdr art-group) to-newsgroup (current-buffer) t)
9670                art-group))))))
9671       (cond
9672        ((not art-group)
9673         (gnus-message 1 "Couldn't %s article %s: %s"
9674                       (cadr (assq action names)) article
9675                       (nnheader-get-report (car to-method))))
9676        ((eq art-group 'junk)
9677         (when (eq action 'move)
9678           (gnus-summary-mark-article article gnus-canceled-mark)
9679           (gnus-message 4 "Deleted article %s" article)
9680           ;; run the delete hook
9681           (run-hook-with-args 'gnus-summary-article-delete-hook
9682                               action
9683                               (gnus-data-header
9684                                (assoc article (gnus-data-list nil)))
9685                               gnus-newsgroup-name nil
9686                               select-method)))
9687        (t
9688         (let* ((pto-group (gnus-group-prefixed-name
9689                            (car art-group) to-method))
9690                (info (gnus-get-info pto-group))
9691                (to-group (gnus-info-group info))
9692                to-marks)
9693           ;; Update the group that has been moved to.
9694           (when (and info
9695                      (memq action '(move copy)))
9696             (unless (member to-group to-groups)
9697               (push to-group to-groups))
9698
9699             (unless (memq article gnus-newsgroup-unreads)
9700               (push 'read to-marks)
9701               (gnus-info-set-read
9702                info (gnus-add-to-range (gnus-info-read info)
9703                                        (list (cdr art-group)))))
9704
9705             ;; See whether the article is to be put in the cache.
9706             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9707                              gnus-article-mark-lists
9708                            (delete '(expirable . expire)
9709                                    (copy-sequence gnus-article-mark-lists))))
9710                   (to-article (cdr art-group)))
9711
9712               ;; Enter the article into the cache in the new group,
9713               ;; if that is required.
9714               (when gnus-use-cache
9715                 (gnus-cache-possibly-enter-article
9716                  to-group to-article
9717                  (memq article gnus-newsgroup-marked)
9718                  (memq article gnus-newsgroup-dormant)
9719                  (memq article gnus-newsgroup-unreads)))
9720
9721               (when gnus-preserve-marks
9722                 ;; Copy any marks over to the new group.
9723                 (when (and (equal to-group gnus-newsgroup-name)
9724                            (not (memq article gnus-newsgroup-unreads)))
9725                   ;; Mark this article as read in this group.
9726                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9727                   (setcdr (gnus-active to-group) to-article)
9728                   (setcdr gnus-newsgroup-active to-article))
9729
9730                 (while marks
9731                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9732                     (when (memq article (symbol-value
9733                                          (intern (format "gnus-newsgroup-%s"
9734                                                          (caar marks)))))
9735                       (push (cdar marks) to-marks)
9736                       ;; If the other group is the same as this group,
9737                       ;; then we have to add the mark to the list.
9738                       (when (equal to-group gnus-newsgroup-name)
9739                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9740                              (cons to-article
9741                                    (symbol-value
9742                                     (intern (format "gnus-newsgroup-%s"
9743                                                     (caar marks)))))))
9744                       ;; Copy the marks to other group.
9745                       (gnus-add-marked-articles
9746                        to-group (cdar marks) (list to-article) info)))
9747                   (setq marks (cdr marks)))
9748
9749                 (gnus-request-set-mark
9750                  to-group (list (list (list to-article) 'add to-marks))))
9751
9752               (gnus-dribble-enter
9753                (concat "(gnus-group-set-info '"
9754                        (gnus-prin1-to-string (gnus-get-info to-group))
9755                        ")"))))
9756
9757           ;; Update the Xref header in this article to point to
9758           ;; the new crossposted article we have just created.
9759           (when (eq action 'crosspost)
9760             (save-excursion
9761               (set-buffer copy-buf)
9762               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9763               (nnheader-replace-header "Xref" new-xref)
9764               (gnus-request-replace-article
9765                article gnus-newsgroup-name (current-buffer) t)))
9766
9767           ;; run the move/copy/crosspost/respool hook
9768           (run-hook-with-args 'gnus-summary-article-move-hook
9769                               action
9770                               (gnus-data-header
9771                                (assoc article (gnus-data-list nil)))
9772                               gnus-newsgroup-name
9773                               to-newsgroup
9774                               select-method))
9775
9776         ;;;!!!Why is this necessary?
9777         (set-buffer gnus-summary-buffer)
9778
9779         (gnus-summary-goto-subject article)
9780         (when (eq action 'move)
9781           (gnus-summary-mark-article article gnus-canceled-mark))))
9782       (push article articles-to-update-marks))
9783
9784     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9785     ;; Re-activate all groups that have been moved to.
9786     (save-excursion
9787       (set-buffer gnus-group-buffer)
9788       (let ((gnus-group-marked to-groups))
9789         (gnus-group-get-new-news-this-group nil t)))
9790
9791     (gnus-kill-buffer copy-buf)
9792     (gnus-summary-position-point)
9793     (gnus-set-mode-line 'summary)))
9794
9795 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9796   "Copy the current article to some other group.
9797 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9798 When called interactively, if TO-NEWSGROUP is nil, use the value of
9799 the variable `gnus-move-split-methods' for finding a default target
9800 newsgroup.
9801 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9802 re-spool using this method."
9803   (interactive "P")
9804   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9805
9806 (defun gnus-summary-crosspost-article (&optional n)
9807   "Crosspost the current article to some other group."
9808   (interactive "P")
9809   (gnus-summary-move-article n nil nil 'crosspost))
9810
9811 (defcustom gnus-summary-respool-default-method nil
9812   "Default method type for respooling an article.
9813 If nil, use to the current newsgroup method."
9814   :type 'symbol
9815   :group 'gnus-summary-mail)
9816
9817 (defcustom gnus-summary-display-while-building nil
9818   "If non-nil, show and update the summary buffer as it's being built.
9819 If the value is t, update the buffer after every line is inserted.  If
9820 the value is an integer (N), update the display every N lines."
9821   :version "22.1"
9822   :group 'gnus-thread
9823   :type '(choice (const :tag "off" nil)
9824                  number
9825                  (const :tag "frequently" t)))
9826
9827 (defun gnus-summary-respool-article (&optional n method)
9828   "Respool the current article.
9829 The article will be squeezed through the mail spooling process again,
9830 which means that it will be put in some mail newsgroup or other
9831 depending on `nnmail-split-methods'.
9832 If N is a positive number, respool the N next articles.
9833 If N is a negative number, respool the N previous articles.
9834 If N is nil and any articles have been marked with the process mark,
9835 respool those articles instead.
9836
9837 Respooling can be done both from mail groups and \"real\" newsgroups.
9838 In the former case, the articles in question will be moved from the
9839 current group into whatever groups they are destined to.  In the
9840 latter case, they will be copied into the relevant groups."
9841   (interactive
9842    (list current-prefix-arg
9843          (let* ((methods (gnus-methods-using 'respool))
9844                 (methname
9845                  (symbol-name (or gnus-summary-respool-default-method
9846                                   (car (gnus-find-method-for-group
9847                                         gnus-newsgroup-name)))))
9848                 (method
9849                  (gnus-completing-read-with-default
9850                   methname "Backend to use when respooling"
9851                   methods nil t nil 'gnus-mail-method-history))
9852                 ms)
9853            (cond
9854             ((zerop (length (setq ms (gnus-servers-using-backend
9855                                       (intern method)))))
9856              (list (intern method) ""))
9857             ((= 1 (length ms))
9858              (car ms))
9859             (t
9860              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9861                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9862                            ms-alist))))))))
9863   (unless method
9864     (error "No method given for respooling"))
9865   (if (assoc (symbol-name
9866               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9867              (gnus-methods-using 'respool))
9868       (gnus-summary-move-article n nil method)
9869     (gnus-summary-copy-article n nil method)))
9870
9871 (defun gnus-summary-import-article (file &optional edit)
9872   "Import an arbitrary file into a mail newsgroup."
9873   (interactive "fImport file: \nP")
9874   (let ((group gnus-newsgroup-name)
9875         (now (current-time))
9876         atts lines group-art)
9877     (unless (gnus-check-backend-function 'request-accept-article group)
9878       (error "%s does not support article importing" group))
9879     (or (file-readable-p file)
9880         (not (file-regular-p file))
9881         (error "Can't read %s" file))
9882     (save-excursion
9883       (set-buffer (gnus-get-buffer-create " *import file*"))
9884       (erase-buffer)
9885       (nnheader-insert-file-contents file)
9886       (goto-char (point-min))
9887       (if (nnheader-article-p)
9888           (save-restriction
9889             (goto-char (point-min))
9890             (search-forward "\n\n" nil t)
9891             (narrow-to-region (point-min) (1- (point)))
9892             (goto-char (point-min))
9893             (unless (re-search-forward "^date:" nil t)
9894               (goto-char (point-max))
9895               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9896        ;; This doesn't look like an article, so we fudge some headers.
9897         (setq atts (file-attributes file)
9898               lines (count-lines (point-min) (point-max)))
9899         (insert "From: " (read-string "From: ") "\n"
9900                 "Subject: " (read-string "Subject: ") "\n"
9901                 "Date: " (message-make-date (nth 5 atts)) "\n"
9902                 "Message-ID: " (message-make-message-id) "\n"
9903                 "Lines: " (int-to-string lines) "\n"
9904                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9905       (setq group-art (gnus-request-accept-article group nil t))
9906       (kill-buffer (current-buffer)))
9907     (setq gnus-newsgroup-active (gnus-activate-group group))
9908     (forward-line 1)
9909     (gnus-summary-goto-article (cdr group-art) nil t)
9910     (when edit
9911       (gnus-summary-edit-article))))
9912
9913 (defun gnus-summary-create-article ()
9914   "Create an article in a mail newsgroup."
9915   (interactive)
9916   (let ((group gnus-newsgroup-name)
9917         (now (current-time))
9918         group-art)
9919     (unless (gnus-check-backend-function 'request-accept-article group)
9920       (error "%s does not support article importing" group))
9921     (save-excursion
9922       (set-buffer (gnus-get-buffer-create " *import file*"))
9923       (erase-buffer)
9924       (goto-char (point-min))
9925       ;; This doesn't look like an article, so we fudge some headers.
9926       (insert "From: " (read-string "From: ") "\n"
9927               "Subject: " (read-string "Subject: ") "\n"
9928               "Date: " (message-make-date now) "\n"
9929               "Message-ID: " (message-make-message-id) "\n")
9930       (setq group-art (gnus-request-accept-article group nil t))
9931       (kill-buffer (current-buffer)))
9932     (setq gnus-newsgroup-active (gnus-activate-group group))
9933     (forward-line 1)
9934     (gnus-summary-goto-article (cdr group-art) nil t)
9935     (gnus-summary-edit-article)))
9936
9937 (defun gnus-summary-article-posted-p ()
9938   "Say whether the current (mail) article is available from news as well.
9939 This will be the case if the article has both been mailed and posted."
9940   (interactive)
9941   (let ((id (mail-header-references (gnus-summary-article-header)))
9942         (gnus-override-method (car (gnus-refer-article-methods))))
9943     (if (gnus-request-head id "")
9944         (gnus-message 2 "The current message was found on %s"
9945                       gnus-override-method)
9946       (gnus-message 2 "The current message couldn't be found on %s"
9947                     gnus-override-method)
9948       nil)))
9949
9950 (defun gnus-summary-expire-articles (&optional now)
9951   "Expire all articles that are marked as expirable in the current group."
9952   (interactive)
9953   (when (and (not gnus-group-is-exiting-without-update-p)
9954              (gnus-check-backend-function
9955               'request-expire-articles gnus-newsgroup-name))
9956     ;; This backend supports expiry.
9957     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9958            (expirable (if total
9959                           (progn
9960                             ;; We need to update the info for
9961                             ;; this group for `gnus-list-of-read-articles'
9962                             ;; to give us the right answer.
9963                             (gnus-run-hooks 'gnus-exit-group-hook)
9964                             (gnus-summary-update-info)
9965                             (gnus-list-of-read-articles gnus-newsgroup-name))
9966                         (setq gnus-newsgroup-expirable
9967                               (sort gnus-newsgroup-expirable '<))))
9968            (expiry-wait (if now 'immediate
9969                           (gnus-group-find-parameter
9970                            gnus-newsgroup-name 'expiry-wait)))
9971            (nnmail-expiry-target
9972             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9973                 nnmail-expiry-target))
9974            es)
9975       (when expirable
9976         ;; There are expirable articles in this group, so we run them
9977         ;; through the expiry process.
9978         (gnus-message 6 "Expiring articles...")
9979         (unless (gnus-check-group gnus-newsgroup-name)
9980           (error "Can't open server for %s" gnus-newsgroup-name))
9981         ;; The list of articles that weren't expired is returned.
9982         (save-excursion
9983           (if expiry-wait
9984               (let ((nnmail-expiry-wait-function nil)
9985                     (nnmail-expiry-wait expiry-wait))
9986                 (setq es (gnus-request-expire-articles
9987                           expirable gnus-newsgroup-name)))
9988             (setq es (gnus-request-expire-articles
9989                       expirable gnus-newsgroup-name)))
9990           (unless total
9991             (setq gnus-newsgroup-expirable es))
9992           ;; We go through the old list of expirable, and mark all
9993           ;; really expired articles as nonexistent.
9994           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9995             (let ((gnus-use-cache nil))
9996               (dolist (article expirable)
9997                 (when (and (not (memq article es))
9998                            (gnus-data-find article))
9999                   (gnus-summary-mark-article article gnus-canceled-mark)
10000                   (run-hook-with-args 'gnus-summary-article-expire-hook
10001                                       'delete
10002                                       (gnus-data-header
10003                                        (assoc article (gnus-data-list nil)))
10004                                       gnus-newsgroup-name
10005                                       nil
10006                                       nil))))))
10007         (gnus-message 6 "Expiring articles...done")))))
10008
10009 (defun gnus-summary-expire-articles-now ()
10010   "Expunge all expirable articles in the current group.
10011 This means that *all* articles that are marked as expirable will be
10012 deleted forever, right now."
10013   (interactive)
10014   (or gnus-expert-user
10015       (gnus-yes-or-no-p
10016        "Are you really, really sure you want to delete all expirable messages? ")
10017       (error "Phew!"))
10018   (gnus-summary-expire-articles t))
10019
10020 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10021 (defun gnus-summary-delete-article (&optional n)
10022   "Delete the N next (mail) articles.
10023 This command actually deletes articles.  This is not a marking
10024 command.  The article will disappear forever from your life, never to
10025 return.
10026
10027 If N is negative, delete backwards.
10028 If N is nil and articles have been marked with the process mark,
10029 delete these instead.
10030
10031 If `gnus-novice-user' is non-nil you will be asked for
10032 confirmation before the articles are deleted."
10033   (interactive "P")
10034   (unless (gnus-check-backend-function 'request-expire-articles
10035                                        gnus-newsgroup-name)
10036     (error "The current newsgroup does not support article deletion"))
10037   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10038     (error "Couldn't open server"))
10039   ;; Compute the list of articles to delete.
10040   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10041         (nnmail-expiry-target 'delete)
10042         not-deleted)
10043     (if (and gnus-novice-user
10044              (not (gnus-yes-or-no-p
10045                    (format "Do you really want to delete %s forever? "
10046                            (if (> (length articles) 1)
10047                                (format "these %s articles" (length articles))
10048                              "this article")))))
10049         ()
10050       ;; Delete the articles.
10051       (setq not-deleted (gnus-request-expire-articles
10052                          articles gnus-newsgroup-name 'force))
10053       (while articles
10054         (gnus-summary-remove-process-mark (car articles))
10055         ;; The backend might not have been able to delete the article
10056         ;; after all.
10057         (unless (memq (car articles) not-deleted)
10058           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10059         (let* ((article (car articles))
10060                (ghead  (gnus-data-header
10061                                     (assoc article (gnus-data-list nil)))))
10062           (run-hook-with-args 'gnus-summary-article-delete-hook
10063                               'delete ghead gnus-newsgroup-name nil
10064                               nil))
10065         (setq articles (cdr articles)))
10066       (when not-deleted
10067         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10068     (gnus-summary-position-point)
10069     (gnus-set-mode-line 'summary)
10070     not-deleted))
10071
10072 (defun gnus-summary-edit-article (&optional arg)
10073   "Edit the current article.
10074 This will have permanent effect only in mail groups.
10075 If ARG is nil, edit the decoded articles.
10076 If ARG is 1, edit the raw articles.
10077 If ARG is 2, edit the raw articles even in read-only groups.
10078 If ARG is 3, edit the articles with the current handles.
10079 Otherwise, allow editing of articles even in read-only
10080 groups."
10081   (interactive "P")
10082   (let (force raw current-handles)
10083     (cond
10084      ((null arg))
10085      ((eq arg 1)
10086       (setq raw t))
10087      ((eq arg 2)
10088       (setq raw t
10089             force t))
10090      ((eq arg 3)
10091       (setq current-handles
10092             (and (gnus-buffer-live-p gnus-article-buffer)
10093                  (with-current-buffer gnus-article-buffer
10094                    (prog1
10095                        gnus-article-mime-handles
10096                      (setq gnus-article-mime-handles nil))))))
10097      (t
10098       (setq force t)))
10099     (when (and raw (not force)
10100                (member gnus-newsgroup-name '("nndraft:delayed"
10101                                              "nndraft:drafts"
10102                                              "nndraft:queue")))
10103       (error "Can't edit the raw article in group %s"
10104              gnus-newsgroup-name))
10105     (save-excursion
10106       (set-buffer gnus-summary-buffer)
10107       (let ((mail-parse-charset gnus-newsgroup-charset)
10108             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10109         (gnus-set-global-variables)
10110         (when (and (not force)
10111                    (gnus-group-read-only-p))
10112           (error "The current newsgroup does not support article editing"))
10113         (gnus-summary-show-article t)
10114         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10115           (with-current-buffer gnus-article-buffer
10116             (mm-enable-multibyte)))
10117         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10118             (setq raw t))
10119         (gnus-article-edit-article
10120          (if raw 'ignore
10121            `(lambda ()
10122               (let ((mbl mml-buffer-list))
10123                 (setq mml-buffer-list nil)
10124                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10125                   (mime-to-mml ,'current-handles))
10126                 (let ((mbl1 mml-buffer-list))
10127                   (setq mml-buffer-list mbl)
10128                   (set (make-local-variable 'mml-buffer-list) mbl1))
10129                 (gnus-make-local-hook 'kill-buffer-hook)
10130                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10131          `(lambda (no-highlight)
10132             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10133                   (message-options message-options)
10134                   (message-options-set-recipient)
10135                   (mail-parse-ignored-charsets
10136                    ',gnus-newsgroup-ignored-charsets)
10137                   (rfc2047-header-encoding-alist
10138                    ',(let ((charset (gnus-group-name-charset
10139                                      (gnus-find-method-for-group
10140                                       gnus-newsgroup-name)
10141                                      gnus-newsgroup-name)))
10142                        (append (list (cons "Newsgroups" charset)
10143                                      (cons "Followup-To" charset)
10144                                      (cons "Xref" charset))
10145                                rfc2047-header-encoding-alist))))
10146               ,(if (not raw) '(progn
10147                                 (mml-to-mime)
10148                                 (mml-destroy-buffers)
10149                                 (remove-hook 'kill-buffer-hook
10150                                              'mml-destroy-buffers t)
10151                                 (kill-local-variable 'mml-buffer-list)))
10152               (gnus-summary-edit-article-done
10153                ,(or (mail-header-references gnus-current-headers) "")
10154                ,(gnus-group-read-only-p)
10155                ,gnus-summary-buffer no-highlight))))))))
10156
10157 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10158
10159 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10160                                                  no-highlight)
10161   "Make edits to the current article permanent."
10162   (interactive)
10163   (save-excursion
10164    ;; The buffer restriction contains the entire article if it exists.
10165     (when (article-goto-body)
10166       (let ((lines (count-lines (point) (point-max)))
10167             (length (- (point-max) (point)))
10168             (case-fold-search t)
10169             (body (copy-marker (point))))
10170         (goto-char (point-min))
10171         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10172           (delete-region (match-beginning 1) (match-end 1))
10173           (insert (number-to-string length)))
10174         (goto-char (point-min))
10175         (when (re-search-forward
10176                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10177           (delete-region (match-beginning 1) (match-end 1))
10178           (insert (number-to-string length)))
10179         (goto-char (point-min))
10180         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10181           (delete-region (match-beginning 1) (match-end 1))
10182           (insert (number-to-string lines))))))
10183   ;; Replace the article.
10184   (let ((buf (current-buffer)))
10185     (with-temp-buffer
10186       (insert-buffer-substring buf)
10187
10188       (if (and (not read-only)
10189                (not (gnus-request-replace-article
10190                      (cdr gnus-article-current) (car gnus-article-current)
10191                      (current-buffer) t)))
10192           (error "Couldn't replace article")
10193         ;; Update the summary buffer.
10194         (if (and references
10195                  (equal (message-tokenize-header references " ")
10196                         (message-tokenize-header
10197                          (or (message-fetch-field "references") "") " ")))
10198             ;; We only have to update this line.
10199             (save-excursion
10200               (save-restriction
10201                 (message-narrow-to-head)
10202                 (let ((head (buffer-substring-no-properties
10203                              (point-min) (point-max)))
10204                       header)
10205                   (with-temp-buffer
10206                     (insert (format "211 %d Article retrieved.\n"
10207                                     (cdr gnus-article-current)))
10208                     (insert head)
10209                     (insert ".\n")
10210                     (let ((nntp-server-buffer (current-buffer)))
10211                       (setq header (car (gnus-get-newsgroup-headers
10212                                          nil t))))
10213                     (save-excursion
10214                       (set-buffer gnus-summary-buffer)
10215                       (gnus-data-set-header
10216                        (gnus-data-find (cdr gnus-article-current))
10217                        header)
10218                       (gnus-summary-update-article-line
10219                        (cdr gnus-article-current) header)
10220                       (if (gnus-summary-goto-subject
10221                            (cdr gnus-article-current) nil t)
10222                           (gnus-summary-update-secondary-mark
10223                            (cdr gnus-article-current))))))))
10224           ;; Update threads.
10225           (set-buffer (or buffer gnus-summary-buffer))
10226           (gnus-summary-update-article (cdr gnus-article-current))
10227           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10228               (gnus-summary-update-secondary-mark
10229                (cdr gnus-article-current))))
10230         ;; Prettify the article buffer again.
10231         (unless no-highlight
10232           (save-excursion
10233             (set-buffer gnus-article-buffer)
10234             ;;;!!! Fix this -- article should be rehighlighted.
10235             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10236             (set-buffer gnus-original-article-buffer)
10237             (gnus-request-article
10238              (cdr gnus-article-current)
10239              (car gnus-article-current) (current-buffer))))
10240         ;; Prettify the summary buffer line.
10241         (when (gnus-visual-p 'summary-highlight 'highlight)
10242           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10243
10244 (defun gnus-summary-edit-wash (key)
10245   "Perform editing command KEY in the article buffer."
10246   (interactive
10247    (list
10248     (progn
10249       (message "%s" (concat (this-command-keys) "- "))
10250       (read-char))))
10251   (message "")
10252   (gnus-summary-edit-article)
10253   (execute-kbd-macro (concat (this-command-keys) key))
10254   (gnus-article-edit-done))
10255
10256 ;;; Respooling
10257
10258 (defun gnus-summary-respool-query (&optional silent trace)
10259   "Query where the respool algorithm would put this article."
10260   (interactive)
10261   (let (gnus-mark-article-hook)
10262     (gnus-summary-select-article)
10263     (save-excursion
10264       (set-buffer gnus-original-article-buffer)
10265       (let ((groups (nnmail-article-group 'identity trace)))
10266         (unless silent
10267           (if groups
10268               (message "This message would go to %s"
10269                        (mapconcat 'car groups ", "))
10270             (message "This message would go to no groups"))
10271           groups)))))
10272
10273 (defun gnus-summary-respool-trace ()
10274   "Trace where the respool algorithm would put this article.
10275 Display a buffer showing all fancy splitting patterns which matched."
10276   (interactive)
10277   (gnus-summary-respool-query nil t))
10278
10279 ;; Summary marking commands.
10280
10281 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10282   "Mark articles which has the same subject as read, and then select the next.
10283 If UNMARK is positive, remove any kind of mark.
10284 If UNMARK is negative, tick articles."
10285   (interactive "P")
10286   (when unmark
10287     (setq unmark (prefix-numeric-value unmark)))
10288   (let ((count
10289          (gnus-summary-mark-same-subject
10290           (gnus-summary-article-subject) unmark)))
10291     ;; Select next unread article.  If auto-select-same mode, should
10292     ;; select the first unread article.
10293     (gnus-summary-next-article t (and gnus-auto-select-same
10294                                       (gnus-summary-article-subject)))
10295     (gnus-message 7 "%d article%s marked as %s"
10296                   count (if (= count 1) " is" "s are")
10297                   (if unmark "unread" "read"))))
10298
10299 (defun gnus-summary-kill-same-subject (&optional unmark)
10300   "Mark articles which has the same subject as read.
10301 If UNMARK is positive, remove any kind of mark.
10302 If UNMARK is negative, tick articles."
10303   (interactive "P")
10304   (when unmark
10305     (setq unmark (prefix-numeric-value unmark)))
10306   (let ((count
10307          (gnus-summary-mark-same-subject
10308           (gnus-summary-article-subject) unmark)))
10309     ;; If marked as read, go to next unread subject.
10310     (when (null unmark)
10311       ;; Go to next unread subject.
10312       (gnus-summary-next-subject 1 t))
10313     (gnus-message 7 "%d articles are marked as %s"
10314                   count (if unmark "unread" "read"))))
10315
10316 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10317   "Mark articles with same SUBJECT as read, and return marked number.
10318 If optional argument UNMARK is positive, remove any kinds of marks.
10319 If optional argument UNMARK is negative, mark articles as unread instead."
10320   (let ((count 1))
10321     (save-excursion
10322       (cond
10323        ((null unmark)                   ; Mark as read.
10324         (while (and
10325                 (progn
10326                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10327                   (gnus-summary-show-thread) t)
10328                 (gnus-summary-find-subject subject))
10329           (setq count (1+ count))))
10330        ((> unmark 0)                    ; Tick.
10331         (while (and
10332                 (progn
10333                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10334                   (gnus-summary-show-thread) t)
10335                 (gnus-summary-find-subject subject))
10336           (setq count (1+ count))))
10337        (t                               ; Mark as unread.
10338         (while (and
10339                 (progn
10340                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10341                   (gnus-summary-show-thread) t)
10342                 (gnus-summary-find-subject subject))
10343           (setq count (1+ count)))))
10344       (gnus-set-mode-line 'summary)
10345       ;; Return the number of marked articles.
10346       count)))
10347
10348 (defun gnus-summary-mark-as-processable (n &optional unmark)
10349   "Set the process mark on the next N articles.
10350 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10351 the process mark instead.  The difference between N and the actual
10352 number of articles marked is returned."
10353   (interactive "P")
10354   (if (and (null n) (gnus-region-active-p))
10355       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10356     (setq n (prefix-numeric-value n))
10357     (let ((backward (< n 0))
10358           (n (abs n)))
10359       (while (and
10360               (> n 0)
10361               (if unmark
10362                   (gnus-summary-remove-process-mark
10363                    (gnus-summary-article-number))
10364                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10365               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10366         (setq n (1- n)))
10367       (when (/= 0 n)
10368         (gnus-message 7 "No more articles"))
10369       (gnus-summary-recenter)
10370       (gnus-summary-position-point)
10371       n)))
10372
10373 (defun gnus-summary-unmark-as-processable (n)
10374   "Remove the process mark from the next N articles.
10375 If N is negative, unmark backward instead.  The difference between N and
10376 the actual number of articles unmarked is returned."
10377   (interactive "P")
10378   (gnus-summary-mark-as-processable n t))
10379
10380 (defun gnus-summary-unmark-all-processable ()
10381   "Remove the process mark from all articles."
10382   (interactive)
10383   (save-excursion
10384     (while gnus-newsgroup-processable
10385       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10386   (gnus-summary-position-point))
10387
10388 (defun gnus-summary-add-mark (article type)
10389   "Mark ARTICLE with a mark of TYPE."
10390   (let ((vtype (car (assq type gnus-article-mark-lists)))
10391         var)
10392     (if (not vtype)
10393         (error "No such mark type: %s" type)
10394       (setq var (intern (format "gnus-newsgroup-%s" type)))
10395       (set var (cons article (symbol-value var)))
10396       (if (memq type '(processable cached replied forwarded recent saved))
10397           (gnus-summary-update-secondary-mark article)
10398         ;;; !!! This is bogus.  We should find out what primary
10399         ;;; !!! mark we want to set.
10400         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10401
10402 (defun gnus-summary-mark-as-expirable (n)
10403   "Mark N articles forward as expirable.
10404 If N is negative, mark backward instead.  The difference between N and
10405 the actual number of articles marked is returned."
10406   (interactive "p")
10407   (gnus-summary-mark-forward n gnus-expirable-mark))
10408
10409 (defun gnus-summary-mark-as-spam (n)
10410   "Mark N articles forward as spam.
10411 If N is negative, mark backward instead.  The difference between N and
10412 the actual number of articles marked is returned."
10413   (interactive "p")
10414   (gnus-summary-mark-forward n gnus-spam-mark))
10415
10416 (defun gnus-summary-mark-article-as-replied (article)
10417   "Mark ARTICLE as replied to and update the summary line.
10418 ARTICLE can also be a list of articles."
10419   (interactive (list (gnus-summary-article-number)))
10420   (let ((articles (if (listp article) article (list article))))
10421     (dolist (article articles)
10422       (unless (numberp article)
10423         (error "%s is not a number" article))
10424       (push article gnus-newsgroup-replied)
10425       (let ((buffer-read-only nil))
10426         (when (gnus-summary-goto-subject article nil t)
10427           (gnus-summary-update-secondary-mark article))))))
10428
10429 (defun gnus-summary-mark-article-as-forwarded (article)
10430   "Mark ARTICLE as forwarded and update the summary line.
10431 ARTICLE can also be a list of articles."
10432   (let ((articles (if (listp article) article (list article))))
10433     (dolist (article articles)
10434       (push article gnus-newsgroup-forwarded)
10435       (let ((buffer-read-only nil))
10436         (when (gnus-summary-goto-subject article nil t)
10437           (gnus-summary-update-secondary-mark article))))))
10438
10439 (defun gnus-summary-set-bookmark (article)
10440   "Set a bookmark in current article."
10441   (interactive (list (gnus-summary-article-number)))
10442   (when (or (not (get-buffer gnus-article-buffer))
10443             (not gnus-current-article)
10444             (not gnus-article-current)
10445             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10446     (error "No current article selected"))
10447   ;; Remove old bookmark, if one exists.
10448   (gnus-pull article gnus-newsgroup-bookmarks)
10449   ;; Set the new bookmark, which is on the form
10450   ;; (article-number . line-number-in-body).
10451   (push
10452    (cons article
10453          (with-current-buffer gnus-article-buffer
10454            (count-lines
10455             (min (point)
10456                  (save-excursion
10457                    (article-goto-body)
10458                    (point)))
10459             (point))))
10460    gnus-newsgroup-bookmarks)
10461   (gnus-message 6 "A bookmark has been added to the current article."))
10462
10463 (defun gnus-summary-remove-bookmark (article)
10464   "Remove the bookmark from the current article."
10465   (interactive (list (gnus-summary-article-number)))
10466   ;; Remove old bookmark, if one exists.
10467   (if (not (assq article gnus-newsgroup-bookmarks))
10468       (gnus-message 6 "No bookmark in current article.")
10469     (gnus-pull article gnus-newsgroup-bookmarks)
10470     (gnus-message 6 "Removed bookmark.")))
10471
10472 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10473 (defun gnus-summary-mark-as-dormant (n)
10474   "Mark N articles forward as dormant.
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-dormant-mark))
10479
10480 (defun gnus-summary-set-process-mark (article)
10481   "Set the process mark on ARTICLE and update the summary line."
10482   (setq gnus-newsgroup-processable
10483         (cons article
10484               (delq article gnus-newsgroup-processable)))
10485   (when (gnus-summary-goto-subject article)
10486     (gnus-summary-show-thread)
10487     (gnus-summary-goto-subject article)
10488     (gnus-summary-update-secondary-mark article)))
10489
10490 (defun gnus-summary-remove-process-mark (&rest articles)
10491   "Remove the process mark from ARTICLES and update the summary line."
10492   (dolist (article articles)
10493     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10494     (when (gnus-summary-goto-subject article)
10495       (gnus-summary-show-thread)
10496       (gnus-summary-goto-subject article)
10497       (gnus-summary-update-secondary-mark article)))
10498   t)
10499
10500 (defun gnus-summary-set-saved-mark (article)
10501   "Set the process mark on ARTICLE and update the summary line."
10502   (push article gnus-newsgroup-saved)
10503   (when (gnus-summary-goto-subject article)
10504     (gnus-summary-update-secondary-mark article)))
10505
10506 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10507   "Mark N articles as read forwards.
10508 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10509 The difference between N and the actual number of articles marked is
10510 returned.
10511 If NO-EXPIRE, auto-expiry will be inhibited."
10512   (interactive "p")
10513   (gnus-summary-show-thread)
10514   (let ((backward (< n 0))
10515         (gnus-summary-goto-unread
10516          (and gnus-summary-goto-unread
10517               (not (eq gnus-summary-goto-unread 'never))
10518               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10519                                     gnus-ticked-mark gnus-dormant-mark)))))
10520         (n (abs n))
10521         (mark (or mark gnus-del-mark)))
10522     (while (and (> n 0)
10523                 (gnus-summary-mark-article nil mark no-expire)
10524                 (zerop (gnus-summary-next-subject
10525                         (if backward -1 1)
10526                         (and gnus-summary-goto-unread
10527                              (not (eq gnus-summary-goto-unread 'never)))
10528                         t)))
10529       (setq n (1- n)))
10530     (when (/= 0 n)
10531       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10532     (gnus-summary-recenter)
10533     (gnus-summary-position-point)
10534     (gnus-set-mode-line 'summary)
10535     n))
10536
10537 (defun gnus-summary-mark-article-as-read (mark)
10538   "Mark the current article quickly as read with MARK."
10539   (let ((article (gnus-summary-article-number)))
10540     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10541     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10542     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10543     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10544     (push (cons article mark) gnus-newsgroup-reads)
10545     ;; Possibly remove from cache, if that is used.
10546     (when gnus-use-cache
10547       (gnus-cache-enter-remove-article article))
10548     ;; Allow the backend to change the mark.
10549     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10550     ;; Check for auto-expiry.
10551     (when (and gnus-newsgroup-auto-expire
10552                (memq mark gnus-auto-expirable-marks))
10553       (setq mark gnus-expirable-mark)
10554       ;; Let the backend know about the mark change.
10555       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10556       (push article gnus-newsgroup-expirable))
10557     ;; Set the mark in the buffer.
10558     (gnus-summary-update-mark mark 'unread)
10559     t))
10560
10561 (defun gnus-summary-mark-article-as-unread (mark)
10562   "Mark the current article quickly as unread with MARK."
10563   (let* ((article (gnus-summary-article-number))
10564          (old-mark (gnus-summary-article-mark article)))
10565     ;; Allow the backend to change the mark.
10566     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10567     (if (eq mark old-mark)
10568         t
10569       (if (<= article 0)
10570           (progn
10571             (gnus-error 1 "Can't mark negative article numbers")
10572             nil)
10573         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10574         (setq gnus-newsgroup-spam-marked
10575               (delq article gnus-newsgroup-spam-marked))
10576         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10577         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10578         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10579         (cond ((= mark gnus-ticked-mark)
10580                (setq gnus-newsgroup-marked
10581                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10582                                               article)))
10583               ((= mark gnus-spam-mark)
10584                (setq gnus-newsgroup-spam-marked
10585                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10586                                               article)))
10587               ((= mark gnus-dormant-mark)
10588                (setq gnus-newsgroup-dormant
10589                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10590                                               article)))
10591               (t
10592                (setq gnus-newsgroup-unreads
10593                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10594                                               article))))
10595         (gnus-pull article gnus-newsgroup-reads)
10596
10597         ;; See whether the article is to be put in the cache.
10598         (and gnus-use-cache
10599              (vectorp (gnus-summary-article-header article))
10600              (save-excursion
10601                (gnus-cache-possibly-enter-article
10602                 gnus-newsgroup-name article
10603                 (= mark gnus-ticked-mark)
10604                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10605
10606         ;; Fix the mark.
10607         (gnus-summary-update-mark mark 'unread)
10608         t))))
10609
10610 (defun gnus-summary-mark-article (&optional article mark no-expire)
10611   "Mark ARTICLE with MARK.  MARK can be any character.
10612 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10613 `??' (dormant) and `?E' (expirable).
10614 If MARK is nil, then the default character `?r' is used.
10615 If ARTICLE is nil, then the article on the current line will be
10616 marked.
10617 If NO-EXPIRE, auto-expiry will be inhibited."
10618   ;; The mark might be a string.
10619   (when (stringp mark)
10620     (setq mark (aref mark 0)))
10621   ;; If no mark is given, then we check auto-expiring.
10622   (when (null mark)
10623     (setq mark gnus-del-mark))
10624   (when (and (not no-expire)
10625              gnus-newsgroup-auto-expire
10626              (memq mark gnus-auto-expirable-marks))
10627     (setq mark gnus-expirable-mark))
10628   (let ((article (or article (gnus-summary-article-number)))
10629         (old-mark (gnus-summary-article-mark article)))
10630     ;; Allow the backend to change the mark.
10631     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10632     (if (eq mark old-mark)
10633         t
10634       (unless article
10635         (error "No article on current line"))
10636       (if (not (if (or (= mark gnus-unread-mark)
10637                        (= mark gnus-ticked-mark)
10638                        (= mark gnus-spam-mark)
10639                        (= mark gnus-dormant-mark))
10640                    (gnus-mark-article-as-unread article mark)
10641                  (gnus-mark-article-as-read article mark)))
10642           t
10643         ;; See whether the article is to be put in the cache.
10644         (and gnus-use-cache
10645              (not (= mark gnus-canceled-mark))
10646              (vectorp (gnus-summary-article-header article))
10647              (save-excursion
10648                (gnus-cache-possibly-enter-article
10649                 gnus-newsgroup-name article
10650                 (= mark gnus-ticked-mark)
10651                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10652
10653         (when (gnus-summary-goto-subject article nil t)
10654           (let ((buffer-read-only nil))
10655             (gnus-summary-show-thread)
10656             ;; Fix the mark.
10657             (gnus-summary-update-mark mark 'unread)
10658             t))))))
10659
10660 (defun gnus-summary-update-secondary-mark (article)
10661   "Update the secondary (read, process, cache) mark."
10662   (gnus-summary-update-mark
10663    (cond ((memq article gnus-newsgroup-processable)
10664           gnus-process-mark)
10665          ((memq article gnus-newsgroup-cached)
10666           gnus-cached-mark)
10667          ((memq article gnus-newsgroup-replied)
10668           gnus-replied-mark)
10669          ((memq article gnus-newsgroup-forwarded)
10670           gnus-forwarded-mark)
10671          ((memq article gnus-newsgroup-saved)
10672           gnus-saved-mark)
10673          ((memq article gnus-newsgroup-recent)
10674           gnus-recent-mark)
10675          ((memq article gnus-newsgroup-unseen)
10676           gnus-unseen-mark)
10677          (t gnus-no-mark))
10678    'replied)
10679   (when (gnus-visual-p 'summary-highlight 'highlight)
10680     (gnus-run-hooks 'gnus-summary-update-hook))
10681   t)
10682
10683 (defun gnus-summary-update-download-mark (article)
10684   "Update the download mark."
10685   (gnus-summary-update-mark
10686    (cond ((memq article gnus-newsgroup-undownloaded)
10687           gnus-undownloaded-mark)
10688          (gnus-newsgroup-agentized
10689           gnus-downloaded-mark)
10690          (t
10691           gnus-no-mark))
10692    'download)
10693   (gnus-summary-update-line t)
10694   t)
10695
10696 (defun gnus-summary-update-mark (mark type)
10697   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10698         (buffer-read-only nil))
10699     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10700     (when forward
10701       (when (looking-at "\r")
10702         (incf forward))
10703       (when (<= (+ forward (point)) (point-max))
10704         ;; Go to the right position on the line.
10705         (goto-char (+ forward (point)))
10706         ;; Replace the old mark with the new mark.
10707         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10708         ;; Optionally update the marks by some user rule.
10709         (when (eq type 'unread)
10710           (gnus-data-set-mark
10711            (gnus-data-find (gnus-summary-article-number)) mark)
10712           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10713
10714 (defun gnus-mark-article-as-read (article &optional mark)
10715   "Enter ARTICLE in the pertinent lists and remove it from others."
10716   ;; Make the article expirable.
10717   (let ((mark (or mark gnus-del-mark)))
10718     (setq gnus-newsgroup-expirable
10719           (if (= mark gnus-expirable-mark)
10720               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10721             (delq article gnus-newsgroup-expirable)))
10722     ;; Remove from unread and marked lists.
10723     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10724     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10725     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10726     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10727     (push (cons article mark) gnus-newsgroup-reads)
10728     ;; Possibly remove from cache, if that is used.
10729     (when gnus-use-cache
10730       (gnus-cache-enter-remove-article article))
10731     t))
10732
10733 (defun gnus-mark-article-as-unread (article &optional mark)
10734   "Enter ARTICLE in the pertinent lists and remove it from others."
10735   (let ((mark (or mark gnus-ticked-mark)))
10736     (if (<= article 0)
10737         (progn
10738           (gnus-error 1 "Can't mark negative article numbers")
10739           nil)
10740       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10741             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10742             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10743             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10744             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10745
10746       ;; Unsuppress duplicates?
10747       (when gnus-suppress-duplicates
10748         (gnus-dup-unsuppress-article article))
10749
10750       (cond ((= mark gnus-ticked-mark)
10751              (setq gnus-newsgroup-marked
10752                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10753             ((= mark gnus-spam-mark)
10754              (setq gnus-newsgroup-spam-marked
10755                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10756                                             article)))
10757             ((= mark gnus-dormant-mark)
10758              (setq gnus-newsgroup-dormant
10759                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10760             (t
10761              (setq gnus-newsgroup-unreads
10762                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10763       (gnus-pull article gnus-newsgroup-reads)
10764       t)))
10765
10766 (defalias 'gnus-summary-mark-as-unread-forward
10767   'gnus-summary-tick-article-forward)
10768 (make-obsolete 'gnus-summary-mark-as-unread-forward
10769                'gnus-summary-tick-article-forward)
10770 (defun gnus-summary-tick-article-forward (n)
10771   "Tick N articles forwards.
10772 If N is negative, tick backwards instead.
10773 The difference between N and the number of articles ticked is returned."
10774   (interactive "p")
10775   (gnus-summary-mark-forward n gnus-ticked-mark))
10776
10777 (defalias 'gnus-summary-mark-as-unread-backward
10778   'gnus-summary-tick-article-backward)
10779 (make-obsolete 'gnus-summary-mark-as-unread-backward
10780                'gnus-summary-tick-article-backward)
10781 (defun gnus-summary-tick-article-backward (n)
10782   "Tick N articles backwards.
10783 The difference between N and the number of articles ticked is returned."
10784   (interactive "p")
10785   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10786
10787 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10788 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10789 (defun gnus-summary-tick-article (&optional article clear-mark)
10790   "Mark current article as unread.
10791 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10792 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10793   (interactive)
10794   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10795                                        gnus-ticked-mark)))
10796
10797 (defun gnus-summary-mark-as-read-forward (n)
10798   "Mark N articles as read forwards.
10799 If N is negative, mark backwards instead.
10800 The difference between N and the actual number of articles marked is
10801 returned."
10802   (interactive "p")
10803   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10804
10805 (defun gnus-summary-mark-as-read-backward (n)
10806   "Mark the N articles as read backwards.
10807 The difference between N and the actual number of articles marked is
10808 returned."
10809   (interactive "p")
10810   (gnus-summary-mark-forward
10811    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10812
10813 (defun gnus-summary-mark-as-read (&optional article mark)
10814   "Mark current article as read.
10815 ARTICLE specifies the article to be marked as read.
10816 MARK specifies a string to be inserted at the beginning of the line."
10817   (gnus-summary-mark-article article mark))
10818
10819 (defun gnus-summary-clear-mark-forward (n)
10820   "Clear marks from N articles forward.
10821 If N is negative, clear backward instead.
10822 The difference between N and the number of marks cleared is returned."
10823   (interactive "p")
10824   (gnus-summary-mark-forward n gnus-unread-mark))
10825
10826 (defun gnus-summary-clear-mark-backward (n)
10827   "Clear marks from N articles backward.
10828 The difference between N and the number of marks cleared is returned."
10829   (interactive "p")
10830   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10831
10832 (defun gnus-summary-mark-unread-as-read ()
10833   "Intended to be used by `gnus-mark-article-hook'."
10834   (when (memq gnus-current-article gnus-newsgroup-unreads)
10835     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10836
10837 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10838   "Intended to be used by `gnus-mark-article-hook'."
10839   (let ((mark (gnus-summary-article-mark)))
10840     (when (or (gnus-unread-mark-p mark)
10841               (gnus-read-mark-p mark))
10842       (gnus-summary-mark-article gnus-current-article
10843                                  (or new-mark gnus-read-mark)))))
10844
10845 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10846   "Intended to be used by `gnus-mark-article-hook'."
10847   (let ((mark (gnus-summary-article-mark)))
10848     (when (or (gnus-unread-mark-p mark)
10849               (gnus-read-mark-p mark))
10850       (gnus-summary-mark-article (gnus-summary-article-number)
10851                                  (or new-mark gnus-read-mark)))))
10852
10853 (defun gnus-summary-mark-unread-as-ticked ()
10854   "Intended to be used by `gnus-mark-article-hook'."
10855   (when (memq gnus-current-article gnus-newsgroup-unreads)
10856     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10857
10858 (defun gnus-summary-mark-region-as-read (point mark all)
10859   "Mark all unread articles between point and mark as read.
10860 If given a prefix, mark all articles between point and mark as read,
10861 even ticked and dormant ones."
10862   (interactive "r\nP")
10863   (save-excursion
10864     (let (article)
10865       (goto-char point)
10866       (beginning-of-line)
10867       (while (and
10868               (< (point) mark)
10869               (progn
10870                 (when (or all
10871                           (memq (setq article (gnus-summary-article-number))
10872                                 gnus-newsgroup-unreads))
10873                   (gnus-summary-mark-article article gnus-del-mark))
10874                 t)
10875               (gnus-summary-find-next))))))
10876
10877 (defun gnus-summary-mark-below (score mark)
10878   "Mark articles with score less than SCORE with MARK."
10879   (interactive "P\ncMark: ")
10880   (setq score (if score
10881                   (prefix-numeric-value score)
10882                 (or gnus-summary-default-score 0)))
10883   (save-excursion
10884     (set-buffer gnus-summary-buffer)
10885     (goto-char (point-min))
10886     (while
10887         (progn
10888           (and (< (gnus-summary-article-score) score)
10889                (gnus-summary-mark-article nil mark))
10890           (gnus-summary-find-next)))))
10891
10892 (defun gnus-summary-kill-below (&optional score)
10893   "Mark articles with score below SCORE as read."
10894   (interactive "P")
10895   (gnus-summary-mark-below score gnus-killed-mark))
10896
10897 (defun gnus-summary-clear-above (&optional score)
10898   "Clear all marks from articles with score above SCORE."
10899   (interactive "P")
10900   (gnus-summary-mark-above score gnus-unread-mark))
10901
10902 (defun gnus-summary-tick-above (&optional score)
10903   "Tick all articles with score above SCORE."
10904   (interactive "P")
10905   (gnus-summary-mark-above score gnus-ticked-mark))
10906
10907 (defun gnus-summary-mark-above (score mark)
10908   "Mark articles with score over SCORE with MARK."
10909   (interactive "P\ncMark: ")
10910   (setq score (if score
10911                   (prefix-numeric-value score)
10912                 (or gnus-summary-default-score 0)))
10913   (save-excursion
10914     (set-buffer gnus-summary-buffer)
10915     (goto-char (point-min))
10916     (while (and (progn
10917                   (when (> (gnus-summary-article-score) score)
10918                     (gnus-summary-mark-article nil mark))
10919                   t)
10920                 (gnus-summary-find-next)))))
10921
10922 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10923 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10924 (defun gnus-summary-limit-include-expunged (&optional no-error)
10925   "Display all the hidden articles that were expunged for low scores."
10926   (interactive)
10927   (let ((buffer-read-only nil))
10928     (let ((scored gnus-newsgroup-scored)
10929           headers h)
10930       (while scored
10931         (unless (gnus-summary-article-header (caar scored))
10932           (and (setq h (gnus-number-to-header (caar scored)))
10933                (< (cdar scored) gnus-summary-expunge-below)
10934                (push h headers)))
10935         (setq scored (cdr scored)))
10936       (if (not headers)
10937           (when (not no-error)
10938             (error "No expunged articles hidden"))
10939         (goto-char (point-min))
10940         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10941         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10942         (dolist (x headers)
10943           (push (mail-header-number x) gnus-newsgroup-limit))
10944         (gnus-summary-prepare-unthreaded (nreverse headers))
10945         (goto-char (point-min))
10946         (gnus-summary-position-point)
10947         t))))
10948
10949 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10950   "Mark all unread articles in this newsgroup as read.
10951 If prefix argument ALL is non-nil, ticked and dormant articles will
10952 also be marked as read.
10953 If QUIETLY is non-nil, no questions will be asked.
10954
10955 If TO-HERE is non-nil, it should be a point in the buffer.  All
10956 articles before (after, if REVERSE is set) this point will be marked
10957 as read.
10958
10959 Note that this function will only catch up the unread article
10960 in the current summary buffer limitation.
10961
10962 The number of articles marked as read is returned."
10963   (interactive "P")
10964   (prog1
10965       (save-excursion
10966         (when (or quietly
10967                   (not gnus-interactive-catchup) ;Without confirmation?
10968                   gnus-expert-user
10969                   (gnus-y-or-n-p
10970                    (if all
10971                        "Mark absolutely all articles as read? "
10972                      "Mark all unread articles as read? ")))
10973           (if (and not-mark
10974                    (not gnus-newsgroup-adaptive)
10975                    (not gnus-newsgroup-auto-expire)
10976                    (not gnus-suppress-duplicates)
10977                    (or (not gnus-use-cache)
10978                        (eq gnus-use-cache 'passive)))
10979               (progn
10980                 (when all
10981                   (setq gnus-newsgroup-marked nil
10982                         gnus-newsgroup-spam-marked nil
10983                         gnus-newsgroup-dormant nil))
10984                 (setq gnus-newsgroup-unreads
10985                       (gnus-sorted-nunion
10986                        (gnus-sorted-intersection gnus-newsgroup-unreads
10987                                                  gnus-newsgroup-downloadable)
10988                        (gnus-sorted-difference gnus-newsgroup-unfetched
10989                                                gnus-newsgroup-cached))))
10990             ;; We actually mark all articles as canceled, which we
10991             ;; have to do when using auto-expiry or adaptive scoring.
10992             (gnus-summary-show-all-threads)
10993             (if (and to-here reverse)
10994                 (progn
10995                   (goto-char to-here)
10996                   (gnus-summary-mark-current-read-and-unread-as-read
10997                    gnus-catchup-mark)
10998                   (while (gnus-summary-find-next (not all))
10999                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11000               (when (gnus-summary-first-subject (not all))
11001                 (while (and
11002                         (if to-here (< (point) to-here) t)
11003                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11004                         (gnus-summary-find-next (not all))))))
11005             (gnus-set-mode-line 'summary))
11006           t))
11007     (gnus-summary-position-point)))
11008
11009 (defun gnus-summary-catchup-to-here (&optional all)
11010   "Mark all unticked articles before the current one as read.
11011 If ALL is non-nil, also mark ticked and dormant articles as read."
11012   (interactive "P")
11013   (save-excursion
11014     (gnus-save-hidden-threads
11015       (let ((beg (point)))
11016         ;; We check that there are unread articles.
11017         (when (or all (gnus-summary-find-prev))
11018           (gnus-summary-catchup all t beg)))))
11019   (gnus-summary-position-point))
11020
11021 (defun gnus-summary-catchup-from-here (&optional all)
11022   "Mark all unticked articles after (and including) the current one as read.
11023 If ALL is non-nil, also mark ticked and dormant articles as read."
11024   (interactive "P")
11025   (save-excursion
11026     (gnus-save-hidden-threads
11027       (let ((beg (point)))
11028         ;; We check that there are unread articles.
11029         (when (or all (gnus-summary-find-next))
11030           (gnus-summary-catchup all t beg nil t)))))
11031   (gnus-summary-position-point))
11032
11033 (defun gnus-summary-catchup-all (&optional quietly)
11034   "Mark all articles in this newsgroup as read.
11035 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11036 instead, which marks only unread articles as read."
11037   (interactive "P")
11038   (gnus-summary-catchup t quietly))
11039
11040 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11041   "Mark all unread articles in this group as read, then exit.
11042 If prefix argument ALL is non-nil, all articles are marked as read.
11043 If QUIETLY is non-nil, no questions will be asked."
11044   (interactive "P")
11045   (when (gnus-summary-catchup all quietly nil 'fast)
11046     ;; Select next newsgroup or exit.
11047     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11048              (eq gnus-auto-select-next 'quietly))
11049         (gnus-summary-next-group nil)
11050       (gnus-summary-exit))))
11051
11052 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11053   "Mark all articles in this newsgroup as read, and then exit.
11054 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11055 instead, which marks only unread articles as read."
11056   (interactive "P")
11057   (gnus-summary-catchup-and-exit t quietly))
11058
11059 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11060   "Mark all articles in this group as read and select the next group.
11061 If given a prefix, mark all articles, unread as well as ticked, as
11062 read."
11063   (interactive "P")
11064   (save-excursion
11065     (gnus-summary-catchup all))
11066   (gnus-summary-next-group))
11067
11068 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11069   "Mark all articles in this group as read and select the previous group.
11070 If given a prefix, mark all articles, unread as well as ticked, as
11071 read."
11072   (interactive "P")
11073   (save-excursion
11074     (gnus-summary-catchup all))
11075   (gnus-summary-next-group nil nil t))
11076
11077 ;;;
11078 ;;; with article
11079 ;;;
11080
11081 (defmacro gnus-with-article (article &rest forms)
11082   "Select ARTICLE and perform FORMS in the original article buffer.
11083 Then replace the article with the result."
11084   `(progn
11085      ;; We don't want the article to be marked as read.
11086      (let (gnus-mark-article-hook)
11087        (gnus-summary-select-article t t nil ,article))
11088      (set-buffer gnus-original-article-buffer)
11089      ,@forms
11090      (if (not (gnus-check-backend-function
11091                'request-replace-article (car gnus-article-current)))
11092          (gnus-message 5 "Read-only group; not replacing")
11093        (unless (gnus-request-replace-article
11094                 ,article (car gnus-article-current)
11095                 (current-buffer) t)
11096          (error "Couldn't replace article")))
11097      ;; The cache and backlog have to be flushed somewhat.
11098      (when gnus-keep-backlog
11099        (gnus-backlog-remove-article
11100         (car gnus-article-current) (cdr gnus-article-current)))
11101      (when gnus-use-cache
11102        (gnus-cache-update-article
11103         (car gnus-article-current) (cdr gnus-article-current)))))
11104
11105 (put 'gnus-with-article 'lisp-indent-function 1)
11106 (put 'gnus-with-article 'edebug-form-spec '(form body))
11107
11108 ;; Thread-based commands.
11109
11110 (defun gnus-summary-articles-in-thread (&optional article)
11111   "Return a list of all articles in the current thread.
11112 If ARTICLE is non-nil, return all articles in the thread that starts
11113 with that article."
11114   (let* ((article (or article (gnus-summary-article-number)))
11115          (data (gnus-data-find-list article))
11116          (top-level (gnus-data-level (car data)))
11117          (top-subject
11118           (cond ((null gnus-thread-operation-ignore-subject)
11119                  (gnus-simplify-subject-re
11120                   (mail-header-subject (gnus-data-header (car data)))))
11121                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11122                  (gnus-simplify-subject-fuzzy
11123                   (mail-header-subject (gnus-data-header (car data)))))
11124                 (t nil)))
11125          (end-point (save-excursion
11126                       (if (gnus-summary-go-to-next-thread)
11127                           (point) (point-max))))
11128          articles)
11129     (while (and data
11130                 (< (gnus-data-pos (car data)) end-point))
11131       (when (or (not top-subject)
11132                 (string= top-subject
11133                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11134                              (gnus-simplify-subject-fuzzy
11135                               (mail-header-subject
11136                                (gnus-data-header (car data))))
11137                            (gnus-simplify-subject-re
11138                             (mail-header-subject
11139                              (gnus-data-header (car data)))))))
11140         (push (gnus-data-number (car data)) articles))
11141       (unless (and (setq data (cdr data))
11142                    (> (gnus-data-level (car data)) top-level))
11143         (setq data nil)))
11144     ;; Return the list of articles.
11145     (nreverse articles)))
11146
11147 (defun gnus-summary-rethread-current ()
11148   "Rethread the thread the current article is part of."
11149   (interactive)
11150   (let* ((gnus-show-threads t)
11151          (article (gnus-summary-article-number))
11152          (id (mail-header-id (gnus-summary-article-header)))
11153          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11154     (unless id
11155       (error "No article on the current line"))
11156     (gnus-rebuild-thread id)
11157     (gnus-summary-goto-subject article)))
11158
11159 (defun gnus-summary-reparent-thread ()
11160   "Make the current article child of the marked (or previous) article.
11161
11162 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11163 is non-nil or the Subject: of both articles are the same."
11164   (interactive)
11165   (unless (not (gnus-group-read-only-p))
11166     (error "The current newsgroup does not support article editing"))
11167   (unless (<= (length gnus-newsgroup-processable) 1)
11168     (error "No more than one article may be marked"))
11169   (let ((child (gnus-summary-article-number))
11170         ;; First grab the marked article, otherwise one line up.
11171         (parent (if (not (null gnus-newsgroup-processable))
11172                     (car gnus-newsgroup-processable)
11173                   (save-excursion
11174                     (if (eq (forward-line -1) 0)
11175                         (gnus-summary-article-number)
11176                       (error "Beginning of summary buffer"))))))
11177     (gnus-summary-reparent-children parent (list child))))
11178
11179 (defun gnus-summary-reparent-children (parent children)
11180   "Make PARENT the parent of CHILDREN.
11181 When called interactively, PARENT is the current article and CHILDREN
11182 are the process-marked articles."
11183   (interactive
11184    (list (gnus-summary-article-number)
11185          (gnus-summary-work-articles nil)))
11186   (dolist (child children)
11187     (save-window-excursion
11188       (let ((gnus-article-buffer " *reparent*"))
11189         (unless (not (eq parent child))
11190           (error "An article may not be self-referential"))
11191         (let ((message-id (mail-header-id
11192                            (gnus-summary-article-header parent))))
11193           (unless (and message-id (not (equal message-id "")))
11194             (error "No message-id in desired parent"))
11195           (gnus-with-article child
11196             (save-restriction
11197               (goto-char (point-min))
11198               (message-narrow-to-head)
11199               (if (re-search-forward "^References: " nil t)
11200                   (progn
11201                     (re-search-forward "^[^ \t]" nil t)
11202                     (forward-line -1)
11203                     (end-of-line)
11204                     (insert " " message-id))
11205                 (insert "References: " message-id "\n"))))
11206           (set-buffer gnus-summary-buffer)
11207           (gnus-summary-unmark-all-processable)
11208           (gnus-summary-update-article child)
11209           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11210             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11211           (gnus-summary-rethread-current)
11212           (gnus-message 3 "Article %d is now the child of article %d"
11213                         child parent))))))
11214
11215 (defun gnus-summary-toggle-threads (&optional arg)
11216   "Toggle showing conversation threads.
11217 If ARG is positive number, turn showing conversation threads on."
11218   (interactive "P")
11219   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11220     (setq gnus-show-threads
11221           (if (null arg) (not gnus-show-threads)
11222             (> (prefix-numeric-value arg) 0)))
11223     (gnus-summary-prepare)
11224     (gnus-summary-goto-subject current)
11225     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11226     (gnus-summary-position-point)))
11227
11228 (defun gnus-summary-show-all-threads ()
11229   "Show all threads."
11230   (interactive)
11231   (save-excursion
11232     (let ((buffer-read-only nil))
11233       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
11234   (gnus-summary-position-point))
11235
11236 (defun gnus-summary-show-thread ()
11237   "Show thread subtrees.
11238 Returns nil if no thread was there to be shown."
11239   (interactive)
11240   (let ((buffer-read-only nil)
11241         (orig (point))
11242         (end (point-at-eol))
11243         ;; Leave point at bol
11244         (beg (progn (beginning-of-line) (point))))
11245     (prog1
11246         ;; Any hidden lines here?
11247         (search-forward "\r" end t)
11248       (subst-char-in-region beg end ?\^M ?\n t)
11249       (goto-char orig)
11250       (gnus-summary-position-point))))
11251
11252 (defun gnus-summary-maybe-hide-threads ()
11253   "If requested, hide the threads that should be hidden."
11254   (when (and gnus-show-threads
11255              gnus-thread-hide-subtree)
11256     (gnus-summary-hide-all-threads
11257      (if (or (consp gnus-thread-hide-subtree)
11258              (functionp gnus-thread-hide-subtree))
11259          (gnus-make-predicate gnus-thread-hide-subtree)
11260        nil))))
11261
11262 ;;; Hiding predicates.
11263
11264 (defun gnus-article-unread-p (header)
11265   (memq (mail-header-number header) gnus-newsgroup-unreads))
11266
11267 (defun gnus-article-unseen-p (header)
11268   (memq (mail-header-number header) gnus-newsgroup-unseen))
11269
11270 (defun gnus-map-articles (predicate articles)
11271   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11272   (apply 'gnus-or (mapcar predicate
11273                           (mapcar (lambda (number)
11274                                     (gnus-summary-article-header number))
11275                                   articles))))
11276
11277 (defun gnus-summary-hide-all-threads (&optional predicate)
11278   "Hide all thread subtrees.
11279 If PREDICATE is supplied, threads that satisfy this predicate
11280 will not be hidden."
11281   (interactive)
11282   (save-excursion
11283     (goto-char (point-min))
11284     (let ((end nil))
11285       (while (not end)
11286         (when (or (not predicate)
11287                   (gnus-map-articles
11288                    predicate (gnus-summary-article-children)))
11289             (gnus-summary-hide-thread))
11290         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11291   (gnus-summary-position-point))
11292
11293 (defun gnus-summary-hide-thread ()
11294   "Hide thread subtrees.
11295 If PREDICATE is supplied, threads that satisfy this predicate
11296 will not be hidden.
11297 Returns nil if no threads were there to be hidden."
11298   (interactive)
11299   (let ((buffer-read-only nil)
11300         (start (point))
11301         (article (gnus-summary-article-number)))
11302     (goto-char start)
11303     ;; Go forward until either the buffer ends or the subthread
11304     ;; ends.
11305     (when (and (not (eobp))
11306                (or (zerop (gnus-summary-next-thread 1 t))
11307                    (goto-char (point-max))))
11308       (prog1
11309           (if (and (> (point) start)
11310                    (search-backward "\n" start t))
11311               (progn
11312                 (subst-char-in-region start (point) ?\n ?\^M)
11313                 (gnus-summary-goto-subject article))
11314             (goto-char start)
11315             nil)))))
11316
11317 (defun gnus-summary-go-to-next-thread (&optional previous)
11318   "Go to the same level (or less) next thread.
11319 If PREVIOUS is non-nil, go to previous thread instead.
11320 Return the article number moved to, or nil if moving was impossible."
11321   (let ((level (gnus-summary-thread-level))
11322         (way (if previous -1 1))
11323         (beg (point)))
11324     (forward-line way)
11325     (while (and (not (eobp))
11326                 (< level (gnus-summary-thread-level)))
11327       (forward-line way))
11328     (if (eobp)
11329         (progn
11330           (goto-char beg)
11331           nil)
11332       (setq beg (point))
11333       (prog1
11334           (gnus-summary-article-number)
11335         (goto-char beg)))))
11336
11337 (defun gnus-summary-next-thread (n &optional silent)
11338   "Go to the same level next N'th thread.
11339 If N is negative, search backward instead.
11340 Returns the difference between N and the number of skips actually
11341 done.
11342
11343 If SILENT, don't output messages."
11344   (interactive "p")
11345   (let ((backward (< n 0))
11346         (n (abs n)))
11347     (while (and (> n 0)
11348                 (gnus-summary-go-to-next-thread backward))
11349       (decf n))
11350     (unless silent
11351       (gnus-summary-position-point))
11352     (when (and (not silent) (/= 0 n))
11353       (gnus-message 7 "No more threads"))
11354     n))
11355
11356 (defun gnus-summary-prev-thread (n)
11357   "Go to the same level previous N'th thread.
11358 Returns the difference between N and the number of skips actually
11359 done."
11360   (interactive "p")
11361   (gnus-summary-next-thread (- n)))
11362
11363 (defun gnus-summary-go-down-thread ()
11364   "Go down one level in the current thread."
11365   (let ((children (gnus-summary-article-children)))
11366     (when children
11367       (gnus-summary-goto-subject (car children)))))
11368
11369 (defun gnus-summary-go-up-thread ()
11370   "Go up one level in the current thread."
11371   (let ((parent (gnus-summary-article-parent)))
11372     (when parent
11373       (gnus-summary-goto-subject parent))))
11374
11375 (defun gnus-summary-down-thread (n)
11376   "Go down thread N steps.
11377 If N is negative, go up instead.
11378 Returns the difference between N and how many steps down that were
11379 taken."
11380   (interactive "p")
11381   (let ((up (< n 0))
11382         (n (abs n)))
11383     (while (and (> n 0)
11384                 (if up (gnus-summary-go-up-thread)
11385                   (gnus-summary-go-down-thread)))
11386       (setq n (1- n)))
11387     (gnus-summary-position-point)
11388     (when (/= 0 n)
11389       (gnus-message 7 "Can't go further"))
11390     n))
11391
11392 (defun gnus-summary-up-thread (n)
11393   "Go up thread N steps.
11394 If N is negative, go down instead.
11395 Returns the difference between N and how many steps down that were
11396 taken."
11397   (interactive "p")
11398   (gnus-summary-down-thread (- n)))
11399
11400 (defun gnus-summary-top-thread ()
11401   "Go to the top of the thread."
11402   (interactive)
11403   (while (gnus-summary-go-up-thread))
11404   (gnus-summary-article-number))
11405
11406 (defun gnus-summary-expire-thread ()
11407   "Mark articles under current thread as expired."
11408   (interactive)
11409   (gnus-summary-kill-thread 0))
11410
11411 (defun gnus-summary-kill-thread (&optional unmark)
11412   "Mark articles under current thread as read.
11413 If the prefix argument is positive, remove any kinds of marks.
11414 If the prefix argument is zero, mark thread as expired.
11415 If the prefix argument is negative, tick articles instead."
11416   (interactive "P")
11417   (when unmark
11418     (setq unmark (prefix-numeric-value unmark)))
11419   (let ((articles (gnus-summary-articles-in-thread))
11420         (hide (or (null unmark) (= unmark 0))))
11421     (save-excursion
11422       ;; Expand the thread.
11423       (gnus-summary-show-thread)
11424       ;; Mark all the articles.
11425       (while articles
11426         (gnus-summary-goto-subject (car articles))
11427         (cond ((null unmark)
11428                (gnus-summary-mark-article-as-read gnus-killed-mark))
11429               ((> unmark 0)
11430                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11431               ((= unmark 0)
11432                (gnus-summary-mark-article-as-unread gnus-expirable-mark))
11433               (t
11434                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11435         (setq articles (cdr articles))))
11436     ;; Hide killed subtrees when hide is true.
11437     (and hide
11438          gnus-thread-hide-killed
11439          (gnus-summary-hide-thread))
11440     ;; If hide is t, go to next unread subject.
11441     (when hide
11442       ;; Go to next unread subject.
11443       (gnus-summary-next-subject 1 t)))
11444   (gnus-set-mode-line 'summary))
11445
11446 ;; Summary sorting commands
11447
11448 (defun gnus-summary-sort-by-number (&optional reverse)
11449   "Sort the summary buffer by article number.
11450 Argument REVERSE means reverse order."
11451   (interactive "P")
11452   (gnus-summary-sort 'number reverse))
11453
11454 (defun gnus-summary-sort-by-random (&optional reverse)
11455   "Randomize the order in the summary buffer.
11456 Argument REVERSE means to randomize in reverse order."
11457   (interactive "P")
11458   (gnus-summary-sort 'random reverse))
11459
11460 (defun gnus-summary-sort-by-author (&optional reverse)
11461   "Sort the summary buffer by author name alphabetically.
11462 If `case-fold-search' is non-nil, case of letters is ignored.
11463 Argument REVERSE means reverse order."
11464   (interactive "P")
11465   (gnus-summary-sort 'author reverse))
11466
11467 (defun gnus-summary-sort-by-recipient (&optional reverse)
11468   "Sort the summary buffer by recipient name alphabetically.
11469 If `case-fold-search' is non-nil, case of letters is ignored.
11470 Argument REVERSE means reverse order."
11471   (interactive "P")
11472   (gnus-summary-sort 'recipient reverse))
11473
11474 (defun gnus-summary-sort-by-subject (&optional reverse)
11475   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11476 If `case-fold-search' is non-nil, case of letters is ignored.
11477 Argument REVERSE means reverse order."
11478   (interactive "P")
11479   (gnus-summary-sort 'subject reverse))
11480
11481 (defun gnus-summary-sort-by-date (&optional reverse)
11482   "Sort the summary buffer by date.
11483 Argument REVERSE means reverse order."
11484   (interactive "P")
11485   (gnus-summary-sort 'date reverse))
11486
11487 (defun gnus-summary-sort-by-score (&optional reverse)
11488   "Sort the summary buffer by score.
11489 Argument REVERSE means reverse order."
11490   (interactive "P")
11491   (gnus-summary-sort 'score reverse))
11492
11493 (defun gnus-summary-sort-by-lines (&optional reverse)
11494   "Sort the summary buffer by the number of lines.
11495 Argument REVERSE means reverse order."
11496   (interactive "P")
11497   (gnus-summary-sort 'lines reverse))
11498
11499 (defun gnus-summary-sort-by-chars (&optional reverse)
11500   "Sort the summary buffer by article length.
11501 Argument REVERSE means reverse order."
11502   (interactive "P")
11503   (gnus-summary-sort 'chars reverse))
11504
11505 (defun gnus-summary-sort-by-original (&optional reverse)
11506   "Sort the summary buffer using the default sorting method.
11507 Argument REVERSE means reverse order."
11508   (interactive "P")
11509   (let* ((buffer-read-only)
11510          (gnus-summary-prepare-hook nil))
11511     ;; We do the sorting by regenerating the threads.
11512     (gnus-summary-prepare)
11513     ;; Hide subthreads if needed.
11514     (gnus-summary-maybe-hide-threads)))
11515
11516 (defun gnus-summary-sort (predicate reverse)
11517   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11518   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11519          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11520          (gnus-thread-sort-functions
11521           (if (not reverse)
11522               thread
11523             `(lambda (t1 t2)
11524                (,thread t2 t1))))
11525          (gnus-sort-gathered-threads-function
11526           gnus-thread-sort-functions)
11527          (gnus-article-sort-functions
11528           (if (not reverse)
11529               article
11530             `(lambda (t1 t2)
11531                (,article t2 t1))))
11532          (buffer-read-only)
11533          (gnus-summary-prepare-hook nil))
11534     ;; We do the sorting by regenerating the threads.
11535     (gnus-summary-prepare)
11536     ;; Hide subthreads if needed.
11537     (gnus-summary-maybe-hide-threads)))
11538
11539 ;; Summary saving commands.
11540
11541 (defun gnus-summary-save-article (&optional n not-saved)
11542   "Save the current article using the default saver function.
11543 If N is a positive number, save the N next articles.
11544 If N is a negative number, save the N previous articles.
11545 If N is nil and any articles have been marked with the process mark,
11546 save those articles instead.
11547 The variable `gnus-default-article-saver' specifies the saver function.
11548
11549 If the optional second argument NOT-SAVED is non-nil, articles saved
11550 will not be marked as saved."
11551   (interactive "P")
11552   (require 'gnus-art)
11553   (let* ((articles (gnus-summary-work-articles n))
11554          (save-buffer (save-excursion
11555                         (nnheader-set-temp-buffer " *Gnus Save*")))
11556          (num (length articles))
11557          ;; Whether to save decoded articles or raw articles.
11558          (decode (when gnus-article-save-coding-system
11559                    (get gnus-default-article-saver :decode)))
11560          ;; When saving many articles in a single file, use the other
11561          ;; function to save articles other than the first one.
11562          (saver2 (get gnus-default-article-saver :function))
11563          (gnus-prompt-before-saving (if saver2
11564                                         t
11565                                       gnus-prompt-before-saving))
11566          (gnus-default-article-saver gnus-default-article-saver)
11567          header file)
11568     (dolist (article articles)
11569       (setq header (gnus-summary-article-header article))
11570       (if (not (vectorp header))
11571           ;; This is a pseudo-article.
11572           (if (assq 'name header)
11573               (gnus-copy-file (cdr (assq 'name header)))
11574             (gnus-message 1 "Article %d is unsaveable" article))
11575         ;; This is a real article.
11576         (save-window-excursion
11577           (let ((gnus-display-mime-function (when decode
11578                                               gnus-display-mime-function))
11579                 (gnus-article-prepare-hook (when decode
11580                                              gnus-article-prepare-hook)))
11581             (gnus-summary-select-article t nil nil article)
11582             (gnus-summary-goto-subject article)))
11583         (save-excursion
11584           (set-buffer save-buffer)
11585           (erase-buffer)
11586           (insert-buffer-substring (if decode
11587                                        gnus-article-buffer
11588                                      gnus-original-article-buffer)))
11589         (setq file (gnus-article-save save-buffer file num))
11590         (gnus-summary-remove-process-mark article)
11591         (unless not-saved
11592           (gnus-summary-set-saved-mark article)))
11593       (when saver2
11594         (setq gnus-default-article-saver saver2
11595               saver2 nil)))
11596     (gnus-kill-buffer save-buffer)
11597     (gnus-summary-position-point)
11598     (gnus-set-mode-line 'summary)
11599     n))
11600
11601 (defun gnus-summary-pipe-output (&optional arg headers)
11602   "Pipe the current article to a subprocess.
11603 If N is a positive number, pipe the N next articles.
11604 If N is a negative number, pipe the N previous articles.
11605 If N is nil and any articles have been marked with the process mark,
11606 pipe those articles instead.
11607 If HEADERS (the symbolic prefix), include the headers, too."
11608   (interactive (gnus-interactive "P\ny"))
11609   (require 'gnus-art)
11610   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11611         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11612     (gnus-summary-save-article arg t))
11613   (let ((buffer (get-buffer "*Shell Command Output*")))
11614     (when (and buffer
11615                (not (zerop (buffer-size buffer))))
11616       (gnus-configure-windows 'pipe))))
11617
11618 (defun gnus-summary-save-article-mail (&optional arg)
11619   "Append the current article to a Unix mail box file.
11620 If N is a positive number, save the N next articles.
11621 If N is a negative number, save the N previous articles.
11622 If N is nil and any articles have been marked with the process mark,
11623 save those articles instead."
11624   (interactive "P")
11625   (require 'gnus-art)
11626   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11627     (gnus-summary-save-article arg)))
11628
11629 (defun gnus-summary-save-article-rmail (&optional arg)
11630   "Append the current article to an rmail file.
11631 If N is a positive number, save the N next articles.
11632 If N is a negative number, save the N previous articles.
11633 If N is nil and any articles have been marked with the process mark,
11634 save those articles instead."
11635   (interactive "P")
11636   (require 'gnus-art)
11637   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11638     (gnus-summary-save-article arg)))
11639
11640 (defun gnus-summary-save-article-file (&optional arg)
11641   "Append the current article to a file.
11642 If N is a positive number, save the N next articles.
11643 If N is a negative number, save the N previous articles.
11644 If N is nil and any articles have been marked with the process mark,
11645 save those articles instead."
11646   (interactive "P")
11647   (require 'gnus-art)
11648   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11649     (gnus-summary-save-article arg)))
11650
11651 (defun gnus-summary-write-article-file (&optional arg)
11652   "Write the current article to a file, deleting the previous file.
11653 If N is a positive number, save the N next articles.
11654 If N is a negative number, save the N previous articles.
11655 If N is nil and any articles have been marked with the process mark,
11656 save those articles instead."
11657   (interactive "P")
11658   (require 'gnus-art)
11659   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11660     (gnus-summary-save-article arg)))
11661
11662 (defun gnus-summary-save-article-body-file (&optional arg)
11663   "Append the current article body to a file.
11664 If N is a positive number, save the N next articles.
11665 If N is a negative number, save the N previous articles.
11666 If N is nil and any articles have been marked with the process mark,
11667 save those articles instead."
11668   (interactive "P")
11669   (require 'gnus-art)
11670   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11671     (gnus-summary-save-article arg)))
11672
11673 (defun gnus-summary-write-article-body-file (&optional arg)
11674   "Write the current article body to a file, deleting the previous file.
11675 If N is a positive number, save the N next articles.
11676 If N is a negative number, save the N previous articles.
11677 If N is nil and any articles have been marked with the process mark,
11678 save those articles instead."
11679   (interactive "P")
11680   (require 'gnus-art)
11681   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11682     (gnus-summary-save-article arg)))
11683
11684 (defun gnus-summary-muttprint (&optional arg)
11685   "Print the current article using Muttprint.
11686 If N is a positive number, save the N next articles.
11687 If N is a negative number, save the N previous articles.
11688 If N is nil and any articles have been marked with the process mark,
11689 save those articles instead."
11690   (interactive "P")
11691   (require 'gnus-art)
11692   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11693     (gnus-summary-save-article arg t)))
11694
11695 (defun gnus-summary-pipe-message (program)
11696   "Pipe the current article through PROGRAM."
11697   (interactive "sProgram: ")
11698   (gnus-summary-select-article)
11699   (let ((mail-header-separator ""))
11700     (gnus-eval-in-buffer-window gnus-article-buffer
11701       (save-restriction
11702         (widen)
11703         (let ((start (window-start))
11704               buffer-read-only)
11705           (message-pipe-buffer-body program)
11706           (set-window-start (get-buffer-window (current-buffer)) start))))))
11707
11708 (defun gnus-get-split-value (methods)
11709   "Return a value based on the split METHODS."
11710   (let (split-name method result match)
11711     (when methods
11712       (save-excursion
11713         (set-buffer gnus-original-article-buffer)
11714         (save-restriction
11715           (nnheader-narrow-to-headers)
11716           (while (and methods (not split-name))
11717             (goto-char (point-min))
11718             (setq method (pop methods))
11719             (setq match (car method))
11720             (when (cond
11721                    ((stringp match)
11722                     ;; Regular expression.
11723                     (ignore-errors
11724                       (re-search-forward match nil t)))
11725                    ((functionp match)
11726                     ;; Function.
11727                     (save-restriction
11728                       (widen)
11729                       (setq result (funcall match gnus-newsgroup-name))))
11730                    ((consp match)
11731                     ;; Form.
11732                     (save-restriction
11733                       (widen)
11734                       (setq result (eval match)))))
11735               (setq split-name (cdr method))
11736               (cond ((stringp result)
11737                      (push (expand-file-name
11738                             result gnus-article-save-directory)
11739                            split-name))
11740                     ((consp result)
11741                      (setq split-name (append result split-name)))))))))
11742     (nreverse split-name)))
11743
11744 (defun gnus-valid-move-group-p (group)
11745   (and (boundp group)
11746        (symbol-name group)
11747        (symbol-value group)
11748        (gnus-get-function (gnus-find-method-for-group
11749                            (symbol-name group)) 'request-accept-article t)))
11750
11751 (defun gnus-read-move-group-name (prompt default articles prefix)
11752   "Read a group name."
11753   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11754          (minibuffer-confirm-incomplete nil) ; XEmacs
11755          (prom
11756           (format "%s %s to"
11757                   prompt
11758                   (if (> (length articles) 1)
11759                       (format "these %d articles" (length articles))
11760                     "this article")))
11761          (to-newsgroup
11762           (let (active group)
11763             (when (or (null split-name) (= 1 (length split-name)))
11764               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11765               (mapatoms (lambda (symbol)
11766                           (setq group (symbol-name symbol))
11767                           (when (string-match "[^\000-\177]" group)
11768                             (setq group (gnus-group-decoded-name group)))
11769                           (set (intern group active) group))
11770                         gnus-active-hashtb))
11771             (cond
11772              ((null split-name)
11773               (gnus-completing-read-with-default
11774                default prom active 'gnus-valid-move-group-p nil prefix
11775                'gnus-group-history))
11776              ((= 1 (length split-name))
11777               (gnus-completing-read-with-default
11778                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11779                'gnus-group-history))
11780              (t
11781               (gnus-completing-read-with-default
11782                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11783                'gnus-group-history)))))
11784          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11785          encoded)
11786     (when to-newsgroup
11787       (if (or (string= to-newsgroup "")
11788               (string= to-newsgroup prefix))
11789           (setq to-newsgroup default))
11790       (unless to-newsgroup
11791         (error "No group name entered"))
11792       (setq encoded (mm-encode-coding-string
11793                      to-newsgroup
11794                      (gnus-group-name-charset to-method to-newsgroup)))
11795       (or (gnus-active encoded)
11796           (gnus-activate-group encoded nil nil to-method)
11797           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11798                                      to-newsgroup))
11799               (or (and (gnus-request-create-group encoded to-method)
11800                        (gnus-activate-group encoded nil nil to-method)
11801                        (gnus-subscribe-group encoded))
11802                   (error "Couldn't create group %s" to-newsgroup)))
11803           (error "No such group: %s" to-newsgroup))
11804       encoded)))
11805
11806 (defvar gnus-summary-save-parts-counter)
11807 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
11808
11809 (defun gnus-summary-save-parts (type dir n &optional reverse)
11810   "Save parts matching TYPE to DIR.
11811 If REVERSE, save parts that do not match TYPE."
11812   (interactive
11813    (list (read-string "Save parts of type: "
11814                       (or (car gnus-summary-save-parts-type-history)
11815                           gnus-summary-save-parts-default-mime)
11816                       'gnus-summary-save-parts-type-history)
11817          (setq gnus-summary-save-parts-last-directory
11818                (read-file-name "Save to directory: "
11819                                gnus-summary-save-parts-last-directory
11820                                nil t))
11821          current-prefix-arg))
11822   (gnus-summary-iterate n
11823     (let ((gnus-display-mime-function nil)
11824           (gnus-inhibit-treatment t))
11825       (gnus-summary-select-article))
11826     (save-excursion
11827       (set-buffer gnus-article-buffer)
11828       (let ((handles (or gnus-article-mime-handles
11829                          (mm-dissect-buffer nil gnus-article-loose-mime)
11830                          (and gnus-article-emulate-mime
11831                               (mm-uu-dissect))))
11832             (gnus-summary-save-parts-counter 1))
11833         (when handles
11834           (gnus-summary-save-parts-1 type dir handles reverse)
11835           (unless gnus-article-mime-handles ;; Don't destroy this case.
11836             (mm-destroy-parts handles)))))))
11837
11838 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11839   (if (stringp (car handle))
11840       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11841               (cdr handle))
11842     (when (if reverse
11843               (not (string-match type (mm-handle-media-type handle)))
11844             (string-match type (mm-handle-media-type handle)))
11845       (let ((file (expand-file-name
11846                    (gnus-map-function
11847                     mm-file-name-rewrite-functions
11848                     (file-name-nondirectory
11849                      (or
11850                       (mail-content-type-get
11851                        (mm-handle-disposition handle) 'filename)
11852                       (mail-content-type-get
11853                        (mm-handle-type handle) 'name)
11854                       (format "%s.%d.%d" gnus-newsgroup-name
11855                               (cdr gnus-article-current)
11856                               gnus-summary-save-parts-counter))))
11857                    dir)))
11858         (incf gnus-summary-save-parts-counter)
11859         (unless (file-exists-p file)
11860           (mm-save-part-to-file handle file))))))
11861
11862 ;; Summary extract commands
11863
11864 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11865   (let ((buffer-read-only nil)
11866         (article (gnus-summary-article-number))
11867         after-article b e)
11868     (unless (gnus-summary-goto-subject article)
11869       (error "No such article: %d" article))
11870     (gnus-summary-position-point)
11871     ;; If all commands are to be bunched up on one line, we collect
11872     ;; them here.
11873     (unless gnus-view-pseudos-separately
11874       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11875             files action)
11876         (while ps
11877           (setq action (cdr (assq 'action (car ps))))
11878           (setq files (list (cdr (assq 'name (car ps)))))
11879           (while (and ps (cdr ps)
11880                       (string= (or action "1")
11881                                (or (cdr (assq 'action (cadr ps))) "2")))
11882             (push (cdr (assq 'name (cadr ps))) files)
11883             (setcdr ps (cddr ps)))
11884           (when files
11885             (when (not (string-match "%s" action))
11886               (push " " files))
11887             (push " " files)
11888             (when (assq 'execute (car ps))
11889               (setcdr (assq 'execute (car ps))
11890                       (funcall (if (string-match "%s" action)
11891                                    'format 'concat)
11892                                action
11893                                (mapconcat
11894                                 (lambda (f)
11895                                   (if (equal f " ")
11896                                       f
11897                                     (shell-quote-argument f)))
11898                                 files " ")))))
11899           (setq ps (cdr ps)))))
11900     (if (and gnus-view-pseudos (not not-view))
11901         (while pslist
11902           (when (assq 'execute (car pslist))
11903             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11904                                   (eq gnus-view-pseudos 'not-confirm)))
11905           (setq pslist (cdr pslist)))
11906       (save-excursion
11907         (while pslist
11908           (setq after-article (or (cdr (assq 'article (car pslist)))
11909                                   (gnus-summary-article-number)))
11910           (gnus-summary-goto-subject after-article)
11911           (forward-line 1)
11912           (setq b (point))
11913           (insert "    " (file-name-nondirectory
11914                           (cdr (assq 'name (car pslist))))
11915                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11916           (setq e (point))
11917           (forward-line -1)             ; back to `b'
11918           (gnus-add-text-properties
11919            b (1- e) (list 'gnus-number gnus-reffed-article-number
11920                           gnus-mouse-face-prop gnus-mouse-face))
11921           (gnus-data-enter
11922            after-article gnus-reffed-article-number
11923            gnus-unread-mark b (car pslist) 0 (- e b))
11924           (setq gnus-newsgroup-unreads
11925                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11926                                          gnus-reffed-article-number))
11927           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11928           (setq pslist (cdr pslist)))))))
11929
11930 (defun gnus-pseudos< (p1 p2)
11931   (let ((c1 (cdr (assq 'action p1)))
11932         (c2 (cdr (assq 'action p2))))
11933     (and c1 c2 (string< c1 c2))))
11934
11935 (defun gnus-request-pseudo-article (props)
11936   (cond ((assq 'execute props)
11937          (gnus-execute-command (cdr (assq 'execute props)))))
11938   (let ((gnus-current-article (gnus-summary-article-number)))
11939     (gnus-run-hooks 'gnus-mark-article-hook)))
11940
11941 (defun gnus-execute-command (command &optional automatic)
11942   (save-excursion
11943     (gnus-article-setup-buffer)
11944     (set-buffer gnus-article-buffer)
11945     (setq buffer-read-only nil)
11946     (let ((command (if automatic command
11947                      (read-string "Command: " (cons command 0)))))
11948       (erase-buffer)
11949       (insert "$ " command "\n\n")
11950       (if gnus-view-pseudo-asynchronously
11951           (start-process "gnus-execute" (current-buffer) shell-file-name
11952                          shell-command-switch command)
11953         (call-process shell-file-name nil t nil
11954                       shell-command-switch command)))))
11955
11956 ;; Summary kill commands.
11957
11958 (defun gnus-summary-edit-global-kill (article)
11959   "Edit the \"global\" kill file."
11960   (interactive (list (gnus-summary-article-number)))
11961   (gnus-group-edit-global-kill article))
11962
11963 (defun gnus-summary-edit-local-kill ()
11964   "Edit a local kill file applied to the current newsgroup."
11965   (interactive)
11966   (setq gnus-current-headers (gnus-summary-article-header))
11967   (gnus-group-edit-local-kill
11968    (gnus-summary-article-number) gnus-newsgroup-name))
11969
11970 ;;; Header reading.
11971
11972 (defun gnus-read-header (id &optional header)
11973   "Read the headers of article ID and enter them into the Gnus system."
11974   (let ((group gnus-newsgroup-name)
11975         (gnus-override-method
11976          (or
11977           gnus-override-method
11978           (and (gnus-news-group-p gnus-newsgroup-name)
11979                (car (gnus-refer-article-methods)))))
11980         where)
11981     ;; First we check to see whether the header in question is already
11982     ;; fetched.
11983     (if (stringp id)
11984         ;; This is a Message-ID.
11985         (setq header (or header (gnus-id-to-header id)))
11986       ;; This is an article number.
11987       (setq header (or header (gnus-summary-article-header id))))
11988     (if (and header
11989              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11990         ;; We have found the header.
11991         header
11992       ;; We have to really fetch the header to this article.
11993       (save-excursion
11994         (set-buffer nntp-server-buffer)
11995         (when (setq where (gnus-request-head id group))
11996           (nnheader-fold-continuation-lines)
11997           (goto-char (point-max))
11998           (insert ".\n")
11999           (goto-char (point-min))
12000           (insert "211 ")
12001           (princ (cond
12002                   ((numberp id) id)
12003                   ((cdr where) (cdr where))
12004                   (header (mail-header-number header))
12005                   (t gnus-reffed-article-number))
12006                  (current-buffer))
12007           (insert " Article retrieved.\n"))
12008         (if (or (not where)
12009                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12010             ()                          ; Malformed head.
12011           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12012             (when (and (stringp id)
12013                        (or
12014                         (not (string= (gnus-group-real-name group)
12015                                       (car where)))
12016                         (not (gnus-server-equal gnus-override-method
12017                                                 (gnus-group-method group)))))
12018               ;; If we fetched by Message-ID and the article came from
12019               ;; a different group (or server), we fudge some bogus
12020               ;; article numbers for this article.
12021               (mail-header-set-number header gnus-reffed-article-number))
12022             (save-excursion
12023               (set-buffer gnus-summary-buffer)
12024               (decf gnus-reffed-article-number)
12025               (gnus-remove-header (mail-header-number header))
12026               (push header gnus-newsgroup-headers)
12027               (setq gnus-current-headers header)
12028               (push (mail-header-number header) gnus-newsgroup-limit)))
12029           header)))))
12030
12031 (defun gnus-remove-header (number)
12032   "Remove header NUMBER from `gnus-newsgroup-headers'."
12033   (if (and gnus-newsgroup-headers
12034            (= number (mail-header-number (car gnus-newsgroup-headers))))
12035       (pop gnus-newsgroup-headers)
12036     (let ((headers gnus-newsgroup-headers))
12037       (while (and (cdr headers)
12038                   (not (= number (mail-header-number (cadr headers)))))
12039         (pop headers))
12040       (when (cdr headers)
12041         (setcdr headers (cddr headers))))))
12042
12043 ;;;
12044 ;;; summary highlights
12045 ;;;
12046
12047 (defun gnus-highlight-selected-summary ()
12048   "Highlight selected article in summary buffer."
12049   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12050   (when gnus-summary-selected-face
12051     (save-excursion
12052       (let* ((beg (point-at-bol))
12053              (end (point-at-eol))
12054              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12055              (from (if (get-text-property beg gnus-mouse-face-prop)
12056                        beg
12057                      (or (next-single-property-change
12058                           beg gnus-mouse-face-prop nil end)
12059                          beg)))
12060              (to
12061               (if (= from end)
12062                   (- from 2)
12063                 (or (next-single-property-change
12064                      from gnus-mouse-face-prop nil end)
12065                     end))))
12066         ;; If no mouse-face prop on line we will have to = from = end,
12067         ;; so we highlight the entire line instead.
12068         (when (= (+ to 2) from)
12069           (setq from beg)
12070           (setq to end))
12071         (if gnus-newsgroup-selected-overlay
12072             ;; Move old overlay.
12073             (gnus-move-overlay
12074              gnus-newsgroup-selected-overlay from to (current-buffer))
12075           ;; Create new overlay.
12076           (gnus-overlay-put
12077            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12078            'face gnus-summary-selected-face))))))
12079
12080 (defvar gnus-summary-highlight-line-cached nil)
12081 (defvar gnus-summary-highlight-line-trigger nil)
12082
12083 (defun gnus-summary-highlight-line-0 ()
12084   (if (and (eq gnus-summary-highlight-line-trigger
12085                gnus-summary-highlight)
12086            gnus-summary-highlight-line-cached)
12087       gnus-summary-highlight-line-cached
12088     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12089           gnus-summary-highlight-line-cached
12090           (let* ((cond (list 'cond))
12091                  (c cond)
12092                  (list gnus-summary-highlight))
12093             (while list
12094               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12095                               nil))
12096               (setq c (cdr c)
12097                     list (cdr list)))
12098             (gnus-byte-compile (list 'lambda nil cond))))))
12099
12100 (defun gnus-summary-highlight-line ()
12101   "Highlight current line according to `gnus-summary-highlight'."
12102   (let* ((beg (point-at-bol))
12103          (article (or (gnus-summary-article-number) gnus-current-article))
12104          (score (or (cdr (assq article
12105                                gnus-newsgroup-scored))
12106                     gnus-summary-default-score 0))
12107          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12108          (inhibit-read-only t)
12109          (default gnus-summary-default-score)
12110          (default-high gnus-summary-default-high-score)
12111          (default-low gnus-summary-default-low-score)
12112          (uncached (and gnus-summary-use-undownloaded-faces
12113                         (memq article gnus-newsgroup-undownloaded)
12114                         (not (memq article gnus-newsgroup-cached)))))
12115     (let ((face (funcall (gnus-summary-highlight-line-0))))
12116       (unless (eq face (get-text-property beg 'face))
12117         (gnus-put-text-property-excluding-characters-with-faces
12118          beg (point-at-eol) 'face
12119          (setq face (if (boundp face) (symbol-value face) face)))
12120         (when gnus-summary-highlight-line-function
12121           (funcall gnus-summary-highlight-line-function article face))))))
12122
12123 (defun gnus-update-read-articles (group unread &optional compute)
12124   "Update the list of read articles in GROUP.
12125 UNREAD is a sorted list."
12126   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12127         (info (gnus-get-info group))
12128         (prev 1)
12129         read)
12130     (if (or (not info) (not active))
12131         ;; There is no info on this group if it was, in fact,
12132         ;; killed.  Gnus stores no information on killed groups, so
12133         ;; there's nothing to be done.
12134         ;; One could store the information somewhere temporarily,
12135         ;; perhaps...  Hmmm...
12136         ()
12137       ;; Remove any negative articles numbers.
12138       (while (and unread (< (car unread) 0))
12139         (setq unread (cdr unread)))
12140       ;; Remove any expired article numbers
12141       (while (and unread (< (car unread) (car active)))
12142         (setq unread (cdr unread)))
12143       ;; Compute the ranges of read articles by looking at the list of
12144       ;; unread articles.
12145       (while unread
12146         (when (/= (car unread) prev)
12147           (push (if (= prev (1- (car unread))) prev
12148                   (cons prev (1- (car unread))))
12149                 read))
12150         (setq prev (1+ (car unread)))
12151         (setq unread (cdr unread)))
12152       (when (<= prev (cdr active))
12153         (push (cons prev (cdr active)) read))
12154       (setq read (if (> (length read) 1) (nreverse read) read))
12155       (if compute
12156           read
12157         (save-excursion
12158           (let (setmarkundo)
12159             ;; Propagate the read marks to the backend.
12160             (when (gnus-check-backend-function 'request-set-mark group)
12161               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12162                     (add (gnus-remove-from-range read (gnus-info-read info))))
12163                 (when (or add del)
12164                   (unless (gnus-check-group group)
12165                     (error "Can't open server for %s" group))
12166                   (gnus-request-set-mark
12167                    group (delq nil (list (if add (list add 'add '(read)))
12168                                          (if del (list del 'del '(read))))))
12169                   (setq setmarkundo
12170                         `(gnus-request-set-mark
12171                           ,group
12172                           ',(delq nil (list
12173                                        (if del (list del 'add '(read)))
12174                                        (if add (list add 'del '(read))))))))))
12175             (set-buffer gnus-group-buffer)
12176             (gnus-undo-register
12177               `(progn
12178                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12179                  (gnus-info-set-read ',info ',(gnus-info-read info))
12180                  (gnus-get-unread-articles-in-group ',info
12181                                                     (gnus-active ,group))
12182                  (gnus-group-update-group ,group t)
12183                  ,setmarkundo))))
12184         ;; Enter this list into the group info.
12185         (gnus-info-set-read info read)
12186         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12187         (gnus-get-unread-articles-in-group info (gnus-active group))
12188         t))))
12189
12190 (defun gnus-offer-save-summaries ()
12191   "Offer to save all active summary buffers."
12192   (let (buffers)
12193     ;; Go through all buffers and find all summaries.
12194     (dolist (buffer (buffer-list))
12195       (when (and (setq buffer (buffer-name buffer))
12196                  (string-match "Summary" buffer)
12197                  (with-current-buffer buffer
12198                    ;; We check that this is, indeed, a summary buffer.
12199                    (and (eq major-mode 'gnus-summary-mode)
12200                         ;; Also make sure this isn't bogus.
12201                         gnus-newsgroup-prepared
12202                         ;; Also make sure that this isn't a
12203                         ;; dead summary buffer.
12204                         (not gnus-dead-summary-mode))))
12205         (push buffer buffers)))
12206     ;; Go through all these summary buffers and offer to save them.
12207     (when buffers
12208       (save-excursion
12209         (map-y-or-n-p
12210          "Update summary buffer %s? "
12211          (lambda (buf)
12212            (switch-to-buffer buf)
12213            (gnus-summary-exit))
12214          buffers)))))
12215
12216 (defun gnus-summary-setup-default-charset ()
12217   "Setup newsgroup default charset."
12218   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12219       (setq gnus-newsgroup-charset nil)
12220     (let* ((ignored-charsets
12221             (or gnus-newsgroup-ephemeral-ignored-charsets
12222                 (append
12223                  (and gnus-newsgroup-name
12224                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12225                  gnus-newsgroup-ignored-charsets))))
12226       (setq gnus-newsgroup-charset
12227             (or gnus-newsgroup-ephemeral-charset
12228                 (and gnus-newsgroup-name
12229                      (gnus-parameter-charset gnus-newsgroup-name))
12230                 gnus-default-charset))
12231       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12232            ignored-charsets))))
12233
12234 ;;;
12235 ;;; Mime Commands
12236 ;;;
12237
12238 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12239   "Display the current article buffer fully MIME-buttonized.
12240 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12241 treated as multipart/mixed."
12242   (interactive "P")
12243   (require 'gnus-art)
12244   (let ((gnus-unbuttonized-mime-types nil)
12245         (gnus-mime-display-multipart-as-mixed show-all-parts))
12246     (gnus-summary-show-article)))
12247
12248 (defun gnus-summary-repair-multipart (article)
12249   "Add a Content-Type header to a multipart article without one."
12250   (interactive (list (gnus-summary-article-number)))
12251   (gnus-with-article article
12252     (message-narrow-to-head)
12253     (message-remove-header "Mime-Version")
12254     (goto-char (point-max))
12255     (insert "Mime-Version: 1.0\n")
12256     (widen)
12257     (when (search-forward "\n--" nil t)
12258       (let ((separator (buffer-substring (point) (point-at-eol))))
12259         (message-narrow-to-head)
12260         (message-remove-header "Content-Type")
12261         (goto-char (point-max))
12262         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12263                         separator))
12264         (widen))))
12265   (let (gnus-mark-article-hook)
12266     (gnus-summary-select-article t t nil article)))
12267
12268 (defun gnus-summary-toggle-display-buttonized ()
12269   "Toggle the buttonizing of the article buffer."
12270   (interactive)
12271   (require 'gnus-art)
12272   (if (setq gnus-inhibit-mime-unbuttonizing
12273             (not gnus-inhibit-mime-unbuttonizing))
12274       (let ((gnus-unbuttonized-mime-types nil))
12275         (gnus-summary-show-article))
12276     (gnus-summary-show-article)))
12277
12278 ;;;
12279 ;;; Generic summary marking commands
12280 ;;;
12281
12282 (defvar gnus-summary-marking-alist
12283   '((read gnus-del-mark "d")
12284     (unread gnus-unread-mark "u")
12285     (ticked gnus-ticked-mark "!")
12286     (dormant gnus-dormant-mark "?")
12287     (expirable gnus-expirable-mark "e"))
12288   "An alist of names/marks/keystrokes.")
12289
12290 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12291 (defvar gnus-summary-mark-map)
12292
12293 (defun gnus-summary-make-all-marking-commands ()
12294   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12295   (dolist (elem gnus-summary-marking-alist)
12296     (apply 'gnus-summary-make-marking-command elem)))
12297
12298 (defun gnus-summary-make-marking-command (name mark keystroke)
12299   (let ((map (make-sparse-keymap)))
12300     (define-key gnus-summary-generic-mark-map keystroke map)
12301     (dolist (lway `((next "next" next nil "n")
12302                     (next-unread "next unread" next t "N")
12303                     (prev "previous" prev nil "p")
12304                     (prev-unread "previous unread" prev t "P")
12305                     (nomove "" nil nil ,keystroke)))
12306       (let ((func (gnus-summary-make-marking-command-1
12307                    mark (car lway) lway name)))
12308         (setq func (eval func))
12309         (define-key map (nth 4 lway) func)))))
12310
12311 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12312   `(defun ,(intern
12313             (format "gnus-summary-put-mark-as-%s%s"
12314                     name (if (eq way 'nomove)
12315                              ""
12316                            (concat "-" (symbol-name way)))))
12317      (n)
12318      ,(format
12319        "Mark the current article as %s%s.
12320 If N, the prefix, then repeat N times.
12321 If N is negative, move in reverse order.
12322 The difference between N and the actual number of articles marked is
12323 returned."
12324        name (cadr lway))
12325      (interactive "p")
12326      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12327
12328 (defun gnus-summary-generic-mark (n mark move unread)
12329   "Mark N articles with MARK."
12330   (unless (eq major-mode 'gnus-summary-mode)
12331     (error "This command can only be used in the summary buffer"))
12332   (gnus-summary-show-thread)
12333   (let ((nummove
12334          (cond
12335           ((eq move 'next) 1)
12336           ((eq move 'prev) -1)
12337           (t 0))))
12338     (if (zerop nummove)
12339         (setq n 1)
12340       (when (< n 0)
12341         (setq n (abs n)
12342               nummove (* -1 nummove))))
12343     (while (and (> n 0)
12344                 (gnus-summary-mark-article nil mark)
12345                 (zerop (gnus-summary-next-subject nummove unread t)))
12346       (setq n (1- n)))
12347     (when (/= 0 n)
12348       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12349     (gnus-summary-recenter)
12350     (gnus-summary-position-point)
12351     (gnus-set-mode-line 'summary)
12352     n))
12353
12354 (defun gnus-summary-insert-articles (articles)
12355   (when (setq articles
12356               (gnus-sorted-difference articles
12357                                       (mapcar (lambda (h)
12358                                                 (mail-header-number h))
12359                                               gnus-newsgroup-headers)))
12360     (setq gnus-newsgroup-headers
12361           (gnus-merge 'list
12362                       gnus-newsgroup-headers
12363                       (gnus-fetch-headers articles)
12364                       'gnus-article-sort-by-number))
12365     ;; Suppress duplicates?
12366     (when gnus-suppress-duplicates
12367       (gnus-dup-suppress-articles))
12368
12369     (if (and gnus-fetch-old-headers
12370              (eq gnus-headers-retrieved-by 'nov))
12371         ;; We might want to build some more threads first.
12372         (if (eq gnus-fetch-old-headers 'invisible)
12373             (gnus-build-all-threads)
12374           (gnus-build-old-threads))
12375       ;; Mark the inserted articles that are unread as unread.
12376       (setq gnus-newsgroup-unreads
12377             (gnus-sorted-nunion
12378              gnus-newsgroup-unreads
12379              (gnus-sorted-nintersection
12380               (gnus-list-of-unread-articles gnus-newsgroup-name)
12381               articles)))
12382       ;; Mark the inserted articles as selected so that the information
12383       ;; of the marks having been changed by a user may be updated when
12384       ;; exiting this group.  See `gnus-summary-update-info'.
12385       (dolist (art articles)
12386         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12387     ;; Let the Gnus agent mark articles as read.
12388     (when gnus-agent
12389       (gnus-agent-get-undownloaded-list))
12390     ;; Remove list identifiers from subject
12391     (when gnus-list-identifiers
12392       (gnus-summary-remove-list-identifiers))
12393     ;; First and last article in this newsgroup.
12394     (when gnus-newsgroup-headers
12395       (setq gnus-newsgroup-begin
12396             (mail-header-number (car gnus-newsgroup-headers))
12397             gnus-newsgroup-end
12398             (mail-header-number
12399              (gnus-last-element gnus-newsgroup-headers))))
12400     (when gnus-use-scoring
12401       (gnus-possibly-score-headers))))
12402
12403 (defun gnus-summary-insert-old-articles (&optional all)
12404   "Insert all old articles in this group.
12405 If ALL is non-nil, already read articles become readable.
12406 If ALL is a number, fetch this number of articles."
12407   (interactive "P")
12408   (prog1
12409       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12410             older len)
12411         (setq older
12412               ;; Some nntp servers lie about their active range.  When
12413               ;; this happens, the active range can be in the millions.
12414               ;; Use a compressed range to avoid creating a huge list.
12415               (gnus-range-difference (list gnus-newsgroup-active) old))
12416         (setq len (gnus-range-length older))
12417         (cond
12418          ((null older) nil)
12419          ((numberp all)
12420           (if (< all len)
12421               (let ((older-range (nreverse older)))
12422                 (setq older nil)
12423
12424                 (while (> all 0)
12425                   (let* ((r (pop older-range))
12426                          (min (if (numberp r) r (car r)))
12427                          (max (if (numberp r) r (cdr r))))
12428                     (while (and (<= min max)
12429                                 (> all 0))
12430                       (push max older)
12431                       (setq all (1- all)
12432                             max (1- max))))))
12433             (setq older (gnus-uncompress-range older))))
12434          (all
12435           (setq older (gnus-uncompress-range older)))
12436          (t
12437           (when (and (numberp gnus-large-newsgroup)
12438                    (> len gnus-large-newsgroup))
12439               (let* ((cursor-in-echo-area nil)
12440                      (initial (gnus-parameter-large-newsgroup-initial
12441                                gnus-newsgroup-name))
12442                      (input
12443                       (read-string
12444                        (format
12445                         "How many articles from %s (%s %d): "
12446                         (gnus-group-decoded-name gnus-newsgroup-name)
12447                         (if initial "max" "default")
12448                         len)
12449                        (if initial
12450                            (cons (number-to-string initial)
12451                                  0)))))
12452                 (unless (string-match "^[ \t]*$" input)
12453                   (setq all (string-to-number input))
12454                   (if (< all len)
12455                       (let ((older-range (nreverse older)))
12456                         (setq older nil)
12457
12458                         (while (> all 0)
12459                           (let* ((r (pop older-range))
12460                                  (min (if (numberp r) r (car r)))
12461                                  (max (if (numberp r) r (cdr r))))
12462                             (while (and (<= min max)
12463                                         (> all 0))
12464                               (push max older)
12465                               (setq all (1- all)
12466                                     max (1- max))))))))))
12467           (setq older (gnus-uncompress-range older))))
12468         (if (not older)
12469             (message "No old news.")
12470           (gnus-summary-insert-articles older)
12471           (gnus-summary-limit (gnus-sorted-nunion old older))))
12472     (gnus-summary-position-point)))
12473
12474 (defun gnus-summary-insert-new-articles ()
12475   "Insert all new articles in this group."
12476   (interactive)
12477   (prog1
12478       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12479             (old-active gnus-newsgroup-active)
12480             (nnmail-fetched-sources (list t))
12481             i new)
12482         (setq gnus-newsgroup-active
12483               (gnus-activate-group gnus-newsgroup-name 'scan))
12484         (setq i (cdr gnus-newsgroup-active))
12485         (while (> i (cdr old-active))
12486           (push i new)
12487           (decf i))
12488         (if (not new)
12489             (message "No gnus is bad news")
12490           (gnus-summary-insert-articles new)
12491           (setq gnus-newsgroup-unreads
12492                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12493           (gnus-summary-limit (gnus-sorted-nunion old new))))
12494     (gnus-summary-position-point)))
12495
12496 (gnus-summary-make-all-marking-commands)
12497
12498 (gnus-ems-redefine)
12499
12500 (provide 'gnus-sum)
12501
12502 (run-hooks 'gnus-sum-load-hook)
12503
12504 ;; Local Variables:
12505 ;; coding: iso-8859-1
12506 ;; End:
12507
12508 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12509 ;;; gnus-sum.el ends here