0ed921f7e2717d28d1d8ee9b64b59bc3c601602a
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996-2014 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile
28   (require 'cl))
29 (eval-when-compile
30   (when (featurep 'xemacs)
31     (require 'easy-mmode))) ; for `define-minor-mode'
32
33 (defvar tool-bar-mode)
34 (defvar gnus-tmp-header)
35
36 (require 'gnus)
37 (require 'gnus-group)
38 (require 'gnus-spec)
39 (require 'gnus-range)
40 (require 'gnus-int)
41 (require 'gnus-undo)
42 (require 'gnus-util)
43 (require 'gmm-utils)
44 (require 'mm-decode)
45 (require 'nnoo)
46
47 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
48 (autoload 'gnus-cache-write-active "gnus-cache")
49 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
50 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
51 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
52 (autoload 'mm-uu-dissect "mm-uu")
53 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
54   "Deuglify broken Outlook (Express) articles and redisplay."
55   t)
56 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
57 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
58 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
59 (autoload 'nnir-article-rsv "nnir" nil nil 'macro)
60 (autoload 'nnir-article-group "nnir" nil nil 'macro)
61
62 (defcustom gnus-kill-summary-on-exit t
63   "*If non-nil, kill the summary buffer when you exit from it.
64 If nil, the summary will become a \"*Dead Summary*\" buffer, and
65 it will be killed sometime later."
66   :group 'gnus-summary-exit
67   :type 'boolean)
68
69 (defcustom gnus-summary-next-group-on-exit t
70   "If non-nil, go to the next unread newsgroup on summary exit.
71 See `gnus-group-goto-unread'."
72   :link '(custom-manual "(gnus)Group Maneuvering")
73   :group 'gnus-summary-exit
74   :version "23.1" ;; No Gnus
75   :type 'boolean)
76
77 (defcustom gnus-summary-stop-at-end-of-message nil
78   "If non-nil, don't select the next message when using `SPC'."
79   :link '(custom-manual "(gnus)Group Maneuvering")
80   :group 'gnus-summary-maneuvering
81   :version "24.1"
82   :type 'boolean)
83
84 (defcustom gnus-fetch-old-headers nil
85   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
86 If an unread article in the group refers to an older, already
87 read (or just marked as read) article, the old article will not
88 normally be displayed in the Summary buffer.  If this variable is
89 t, Gnus will attempt to grab the headers to the old articles, and
90 thereby build complete threads.  If it has the value `some', all
91 old headers will be fetched but only enough headers to connect
92 otherwise loose threads will be displayed.  This variable can
93 also be a number.  In that case, no more than that number of old
94 headers will be fetched.  If it has the value `invisible', all
95 old headers will be fetched, but none will be displayed.
96
97 The server has to support NOV for any of this to work.
98
99 This feature can seriously impact performance it ignores all
100 locally cached header entries.  Setting it to t for groups for a
101 server that doesn't expire articles (such as news.gmane.org),
102 leads to very slow summary generation."
103   :group 'gnus-thread
104   :type '(choice (const :tag "off" nil)
105                  (const :tag "on" t)
106                  (const some)
107                  (const invisible)
108                  number
109                  (sexp :menu-tag "other" t)))
110
111 (defcustom gnus-refer-thread-limit 500
112   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
113 If t, fetch all the available old headers."
114   :group 'gnus-thread
115   :type '(choice number
116                  (sexp :menu-tag "other" t)))
117
118 (defcustom gnus-refer-thread-use-nnir nil
119   "*Use nnir to search an entire server when referring threads. A
120 nil value will only search for thread-related articles in the
121 current group."
122   :version "24.1"
123   :group 'gnus-thread
124   :type 'boolean)
125
126 (defcustom gnus-summary-make-false-root 'adopt
127   "*nil means that Gnus won't gather loose threads.
128 If the root of a thread has expired or been read in a previous
129 session, the information necessary to build a complete thread has been
130 lost.  Instead of having many small sub-threads from this original thread
131 scattered all over the summary buffer, Gnus can gather them.
132
133 If non-nil, Gnus will try to gather all loose sub-threads from an
134 original thread into one large thread.
135
136 If this variable is non-nil, it should be one of `none', `adopt',
137 `dummy' or `empty'.
138
139 If this variable is `none', Gnus will not make a false root, but just
140 present the sub-threads after another.
141 If this variable is `dummy', Gnus will create a dummy root that will
142 have all the sub-threads as children.
143 If this variable is `adopt', Gnus will make one of the \"children\"
144 the parent and mark all the step-children as such.
145 If this variable is `empty', the \"children\" are printed with empty
146 subject fields.  (Or rather, they will be printed with a string
147 given by the `gnus-summary-same-subject' variable.)"
148   :group 'gnus-thread
149   :type '(choice (const :tag "off" nil)
150                  (const none)
151                  (const dummy)
152                  (const adopt)
153                  (const empty)))
154
155 (defcustom gnus-summary-make-false-root-always nil
156   "Always make a false dummy root."
157   :version "22.1"
158   :group 'gnus-thread
159   :type 'boolean)
160
161 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
162   "*A regexp to match subjects to be excluded from loose thread gathering.
163 As loose thread gathering is done on subjects only, that means that
164 there can be many false gatherings performed.  By rooting out certain
165 common subjects, gathering might become saner."
166   :group 'gnus-thread
167   :type 'regexp)
168
169 (defcustom gnus-summary-gather-subject-limit nil
170   "*Maximum length of subject comparisons when gathering loose threads.
171 Use nil to compare full subjects.  Setting this variable to a low
172 number will help gather threads that have been corrupted by
173 newsreaders chopping off subject lines, but it might also mean that
174 unrelated articles that have subject that happen to begin with the
175 same few characters will be incorrectly gathered.
176
177 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
178 comparing subjects."
179   :group 'gnus-thread
180   :type '(choice (const :tag "off" nil)
181                  (const fuzzy)
182                  (sexp :menu-tag "on" t)))
183
184 (defcustom gnus-simplify-subject-functions nil
185   "List of functions taking a string argument that simplify subjects.
186 The functions are applied recursively.
187
188 Useful functions to put in this list include:
189 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
190 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
191   :group 'gnus-thread
192   :type '(repeat function))
193
194 (defcustom gnus-simplify-ignored-prefixes nil
195   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
196   :group 'gnus-thread
197   :type '(choice (const :tag "off" nil)
198                  regexp))
199
200 (defcustom gnus-build-sparse-threads nil
201   "*If non-nil, fill in the gaps in threads.
202 If `some', only fill in the gaps that are needed to tie loose threads
203 together.  If `more', fill in all leaf nodes that Gnus can find.  If
204 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
205   :group 'gnus-thread
206   :type '(choice (const :tag "off" nil)
207                  (const some)
208                  (const more)
209                  (sexp :menu-tag "all" t)))
210
211 (defcustom gnus-summary-thread-gathering-function
212   'gnus-gather-threads-by-subject
213   "*Function used for gathering loose threads.
214 There are two pre-defined functions: `gnus-gather-threads-by-subject',
215 which only takes Subjects into consideration; and
216 `gnus-gather-threads-by-references', which compared the References
217 headers of the articles to find matches."
218   :group 'gnus-thread
219   :type '(radio (function-item gnus-gather-threads-by-subject)
220                 (function-item gnus-gather-threads-by-references)
221                 (function :tag "other")))
222
223 (defcustom gnus-summary-same-subject ""
224   "*String indicating that the current article has the same subject as the previous.
225 This variable will only be used if the value of
226 `gnus-summary-make-false-root' is `empty'."
227   :group 'gnus-summary-format
228   :type 'string)
229
230 (defcustom gnus-summary-goto-unread nil
231   "*If t, many commands will go to the next unread article.
232 This applies to marking commands as well as other commands that
233 \"naturally\" select the next article, like, for instance, `SPC' at
234 the end of an article.
235
236 If nil, the marking commands do NOT go to the next unread article
237 \(they go to the next article instead).  If `never', commands that
238 usually go to the next unread article, will go to the next article,
239 whether it is read or not."
240   :version "24.1"
241   :group 'gnus-summary-marks
242   :link '(custom-manual "(gnus)Setting Marks")
243   :type '(choice (const :tag "off" nil)
244                  (const never)
245                  (sexp :menu-tag "on" t)))
246
247 (defcustom gnus-summary-default-score 0
248   "*Default article score level.
249 All scores generated by the score files will be added to this score.
250 If this variable is nil, scoring will be disabled."
251   :group 'gnus-score-default
252   :type '(choice (const :tag "disable")
253                  integer))
254
255 (defcustom gnus-summary-default-high-score 0
256   "*Default threshold for a high scored article.
257 An article will be highlighted as high scored if its score is greater
258 than this score."
259   :version "22.1"
260   :group 'gnus-score-default
261   :type 'integer)
262
263 (defcustom gnus-summary-default-low-score 0
264   "*Default threshold for a low scored article.
265 An article will be highlighted as low scored if its score is smaller
266 than this score."
267   :version "22.1"
268   :group 'gnus-score-default
269   :type 'integer)
270
271 (defcustom gnus-summary-zcore-fuzz 0
272   "*Fuzziness factor for the zcore in the summary buffer.
273 Articles with scores closer than this to `gnus-summary-default-score'
274 will not be marked."
275   :group 'gnus-summary-format
276   :type 'integer)
277
278 (defcustom gnus-simplify-subject-fuzzy-regexp nil
279   "*Strings to be removed when doing fuzzy matches.
280 This can either be a regular expression or list of regular expressions
281 that will be removed from subject strings if fuzzy subject
282 simplification is selected."
283   :group 'gnus-thread
284   :type '(repeat regexp))
285
286 (defcustom gnus-show-threads t
287   "*If non-nil, display threads in summary mode."
288   :group 'gnus-thread
289   :type 'boolean)
290
291 (defcustom gnus-thread-hide-subtree nil
292   "*If non-nil, hide all threads initially.
293 This can be a predicate specifier which says which threads to hide.
294 If threads are hidden, you have to run the command
295 `gnus-summary-show-thread' by hand or select an article."
296   :group 'gnus-thread
297   :type '(radio (sexp :format "Non-nil\n"
298                       :match (lambda (widget value)
299                                (not (or (consp value) (functionp value))))
300                       :value t)
301                 (const nil)
302                 (sexp :tag "Predicate specifier")))
303
304 (defcustom gnus-thread-hide-killed t
305   "*If non-nil, hide killed threads automatically."
306   :group 'gnus-thread
307   :type 'boolean)
308
309 (defcustom gnus-thread-ignore-subject t
310   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
311 If nil, articles that have different subjects from their parents will
312 start separate threads."
313   :group 'gnus-thread
314   :type 'boolean)
315
316 (defcustom gnus-thread-operation-ignore-subject t
317   "*If non-nil, subjects will be ignored when doing thread commands.
318 This affects commands like `gnus-summary-kill-thread' and
319 `gnus-summary-lower-thread'.
320
321 If this variable is nil, articles in the same thread with different
322 subjects will not be included in the operation in question.  If this
323 variable is `fuzzy', only articles that have subjects that are fuzzily
324 equal will be included."
325   :group 'gnus-thread
326   :type '(choice (const :tag "off" nil)
327                  (const fuzzy)
328                  (sexp :tag "on" t)))
329
330 (defcustom gnus-thread-indent-level 4
331   "*Number that says how much each sub-thread should be indented."
332   :group 'gnus-thread
333   :type 'integer)
334
335 (defcustom gnus-auto-extend-newsgroup t
336   "*If non-nil, extend newsgroup forward and backward when requested."
337   :group 'gnus-summary-choose
338   :type 'boolean)
339
340 (defcustom gnus-auto-select-first t
341   "If non-nil, select an article on group entry.
342 An article is selected automatically when entering a group
343 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
344 `gnus-summary-catchup-and-goto-next-group'.
345
346 Which article is selected is controlled by the variable
347 `gnus-auto-select-subject'.
348
349 If you want to prevent automatic selection of articles in some
350 newsgroups, set the variable to nil in `gnus-select-group-hook'."
351   ;; Commands include...
352   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
353   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
354   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
355   :group 'gnus-group-select
356   :type '(choice (const :tag "none" nil)
357                  (sexp :menu-tag "first" t)))
358
359 (defcustom gnus-auto-select-subject 'unseen-or-unread
360   "*Says what subject to place under point when entering a group.
361
362 This variable can either be the symbols `first' (place point on the
363 first subject), `unread' (place point on the subject line of the first
364 unread article), `best' (place point on the subject line of the
365 highest-scored article), `unseen' (place point on the subject line of
366 the first unseen article), `unseen-or-unread' (place point on the subject
367 line of the first unseen article or, if all articles have been seen, on the
368 subject line of the first unread article), or a function to be called to
369 place point on some subject line."
370   :version "24.1"
371   :group 'gnus-group-select
372   :type '(choice (const best)
373                  (const unread)
374                  (const first)
375                  (const unseen)
376                  (const unseen-or-unread)
377                  (function :tag "Function to call")))
378
379 (defcustom gnus-auto-select-next t
380   "*If non-nil, offer to go to the next group from the end of the previous.
381 If the value is t and the next newsgroup is empty, Gnus will exit
382 summary mode and go back to group mode.  If the value is neither nil
383 nor t, Gnus will select the following unread newsgroup.  In
384 particular, if the value is the symbol `quietly', the next unread
385 newsgroup will be selected without any confirmation, and if it is
386 `almost-quietly', the next group will be selected without any
387 confirmation if you are located on the last article in the group.
388 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
389 will go to the next group without confirmation."
390   :group 'gnus-summary-maneuvering
391   :type '(choice (const :tag "off" nil)
392                  (const quietly)
393                  (const almost-quietly)
394                  (const slightly-quietly)
395                  (sexp :menu-tag "on" t)))
396
397 (defcustom gnus-auto-select-same nil
398   "*If non-nil, select the next article with the same subject.
399 If there are no more articles with the same subject, go to
400 the first unread article."
401   :group 'gnus-summary-maneuvering
402   :type 'boolean)
403
404 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
405   "What article should be selected after exiting an ephemeral group.
406 Valid values include:
407
408 `next'
409   Select the next article.
410 `next-unread'
411   Select the next unread article.
412 `next-noselect'
413   Move the cursor to the next article.  This is the default.
414 `next-unread-noselect'
415   Move the cursor to the next unread article.
416
417 If it has any other value or there is no next (unread) article, the
418 article selected before entering to the ephemeral group will appear."
419   :version "23.1" ;; No Gnus
420   :group 'gnus-summary-maneuvering
421   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
422                  (const next) (const next-unread)
423                  (const next-noselect) (const next-unread-noselect)
424                  (sexp :tag "other" :value nil)))
425
426 (defcustom gnus-auto-goto-ignores 'unfetched
427   "*Says how to handle unfetched articles when maneuvering.
428
429 This variable can either be the symbols nil (maneuver to any
430 article), `undownloaded' (maneuvering while unplugged ignores articles
431 that have not been fetched), `always-undownloaded' (maneuvering always
432 ignores articles that have not been fetched), `unfetched' (maneuvering
433 ignores articles whose headers have not been fetched).
434
435 NOTE: The list of unfetched articles will always be nil when plugged
436 and, when unplugged, a subset of the undownloaded article list."
437   :version "22.1"
438   :group 'gnus-summary-maneuvering
439   :type '(choice (const :tag "None" nil)
440                  (const :tag "Undownloaded when unplugged" undownloaded)
441                  (const :tag "Undownloaded" always-undownloaded)
442                  (const :tag "Unfetched" unfetched)))
443
444 (defcustom gnus-summary-check-current nil
445   "*If non-nil, consider the current article when moving.
446 The \"unread\" movement commands will stay on the same line if the
447 current article is unread."
448   :group 'gnus-summary-maneuvering
449   :type 'boolean)
450
451 (defcustom gnus-auto-center-summary
452   (max (or (bound-and-true-p scroll-margin) 0) 2)
453   "*If non-nil, always center the current summary buffer.
454 In particular, if `vertical' do only vertical recentering.  If non-nil
455 and non-`vertical', do both horizontal and vertical recentering."
456   :group 'gnus-summary-maneuvering
457   :type '(choice (const :tag "none" nil)
458                  (const vertical)
459                  (integer :tag "height")
460                  (sexp :menu-tag "both" t)))
461
462 (defcustom gnus-auto-center-group t
463   "If non-nil, always center the group buffer."
464   :group 'gnus-summary-maneuvering
465   :type 'boolean)
466
467 (defcustom gnus-show-all-headers nil
468   "*If non-nil, don't hide any headers."
469   :group 'gnus-article-hiding
470   :group 'gnus-article-headers
471   :type 'boolean)
472
473 (defcustom gnus-summary-ignore-duplicates nil
474   "*If non-nil, ignore articles with identical Message-ID headers."
475   :group 'gnus-summary
476   :type 'boolean)
477
478 (defcustom gnus-single-article-buffer nil
479   "*If non-nil, display all articles in the same buffer.
480 If nil, each group will get its own article buffer."
481   :version "24.1"
482   :group 'gnus-article-various
483   :type 'boolean)
484
485 (defcustom gnus-widen-article-window nil
486   "If non-nil, selecting the article buffer will display only the article buffer."
487   :version "24.1"
488   :group 'gnus-article-various
489   :type 'boolean)
490
491 (defcustom gnus-break-pages t
492   "*If non-nil, do page breaking on articles.
493 The page delimiter is specified by the `gnus-page-delimiter'
494 variable."
495   :group 'gnus-article-various
496   :type 'boolean)
497
498 (defcustom gnus-move-split-methods nil
499   "*Variable used to suggest where articles are to be moved to.
500 It uses the same syntax as the `gnus-split-methods' variable.
501 However, whereas `gnus-split-methods' specifies file names as targets,
502 this variable specifies group names."
503   :group 'gnus-summary-mail
504   :type '(repeat (choice (list :value (fun) function)
505                          (cons :value ("" "") regexp (repeat string))
506                          (sexp :value nil))))
507
508 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
509   "Function used to compute default prefix for article move/copy/etc prompts.
510 The function should take one argument, a group name, and return a
511 string with the suggested prefix."
512   :group 'gnus-summary-mail
513   :type 'function)
514
515 ;; FIXME: Although the custom type is `character' for the following variables,
516 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
517
518 (defcustom gnus-unread-mark ?           ;Whitespace
519   "*Mark used for unread articles."
520   :group 'gnus-summary-marks
521   :type 'character)
522
523 (defcustom gnus-ticked-mark ?!
524   "*Mark used for ticked articles."
525   :group 'gnus-summary-marks
526   :type 'character)
527
528 (defcustom gnus-dormant-mark ??
529   "*Mark used for dormant articles."
530   :group 'gnus-summary-marks
531   :type 'character)
532
533 (defcustom gnus-del-mark ?r
534   "*Mark used for del'd articles."
535   :group 'gnus-summary-marks
536   :type 'character)
537
538 (defcustom gnus-read-mark ?R
539   "*Mark used for read articles."
540   :group 'gnus-summary-marks
541   :type 'character)
542
543 (defcustom gnus-expirable-mark ?E
544   "*Mark used for expirable articles."
545   :group 'gnus-summary-marks
546   :type 'character)
547
548 (defcustom gnus-killed-mark ?K
549   "*Mark used for killed articles."
550   :group 'gnus-summary-marks
551   :type 'character)
552
553 (defcustom gnus-spam-mark ?$
554   "*Mark used for spam articles."
555   :version "22.1"
556   :group 'gnus-summary-marks
557   :type 'character)
558
559 (defcustom gnus-kill-file-mark ?X
560   "*Mark used for articles killed by kill files."
561   :group 'gnus-summary-marks
562   :type 'character)
563
564 (defcustom gnus-low-score-mark ?Y
565   "*Mark used for articles with a low score."
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-catchup-mark ?C
570   "*Mark used for articles that are caught up."
571   :group 'gnus-summary-marks
572   :type 'character)
573
574 (defcustom gnus-replied-mark ?A
575   "*Mark used for articles that have been replied to."
576   :group 'gnus-summary-marks
577   :type 'character)
578
579 (defcustom gnus-forwarded-mark ?F
580   "*Mark used for articles that have been forwarded."
581   :version "22.1"
582   :group 'gnus-summary-marks
583   :type 'character)
584
585 (defcustom gnus-recent-mark ?N
586   "*Mark used for articles that are recent."
587   :version "22.1"
588   :group 'gnus-summary-marks
589   :type 'character)
590
591 (defcustom gnus-cached-mark ?*
592   "*Mark used for articles that are in the cache."
593   :group 'gnus-summary-marks
594   :type 'character)
595
596 (defcustom gnus-saved-mark ?S
597   "*Mark used for articles that have been saved."
598   :group 'gnus-summary-marks
599   :type 'character)
600
601 (defcustom gnus-unseen-mark ?.
602   "*Mark used for articles that haven't been seen."
603   :version "22.1"
604   :group 'gnus-summary-marks
605   :type 'character)
606
607 (defcustom gnus-no-mark ?               ;Whitespace
608   "*Mark used for articles that have no other secondary mark."
609   :version "22.1"
610   :group 'gnus-summary-marks
611   :type 'character)
612
613 (defcustom gnus-ancient-mark ?O
614   "*Mark used for ancient articles."
615   :group 'gnus-summary-marks
616   :type 'character)
617
618 (defcustom gnus-sparse-mark ?Q
619   "*Mark used for sparsely reffed articles."
620   :group 'gnus-summary-marks
621   :type 'character)
622
623 (defcustom gnus-canceled-mark ?G
624   "*Mark used for canceled articles."
625   :group 'gnus-summary-marks
626   :type 'character)
627
628 (defcustom gnus-duplicate-mark ?M
629   "*Mark used for duplicate articles."
630   :group 'gnus-summary-marks
631   :type 'character)
632
633 (defcustom gnus-undownloaded-mark ?-
634   "*Mark used for articles that weren't downloaded."
635   :version "22.1"
636   :group 'gnus-summary-marks
637   :type 'character)
638
639 (defcustom gnus-downloaded-mark ?+
640   "*Mark used for articles that were downloaded."
641   :group 'gnus-summary-marks
642   :type 'character)
643
644 (defcustom gnus-downloadable-mark ?%
645   "*Mark used for articles that are to be downloaded."
646   :group 'gnus-summary-marks
647   :type 'character)
648
649 (defcustom gnus-unsendable-mark ?=
650   "*Mark used for articles that won't be sent."
651   :group 'gnus-summary-marks
652   :type 'character)
653
654 (defcustom gnus-score-over-mark ?+
655   "*Score mark used for articles with high scores."
656   :group 'gnus-summary-marks
657   :type 'character)
658
659 (defcustom gnus-score-below-mark ?-
660   "*Score mark used for articles with low scores."
661   :group 'gnus-summary-marks
662   :type 'character)
663
664 (defcustom gnus-empty-thread-mark ?     ;Whitespace
665   "*There is no thread under the article."
666   :group 'gnus-summary-marks
667   :type 'character)
668
669 (defcustom gnus-not-empty-thread-mark ?=
670   "*There is a thread under the article."
671   :group 'gnus-summary-marks
672   :type 'character)
673
674 (defcustom gnus-view-pseudo-asynchronously nil
675   "*If non-nil, Gnus will view pseudo-articles asynchronously."
676   :group 'gnus-extract-view
677   :type 'boolean)
678
679 (defcustom gnus-auto-expirable-marks
680   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
681         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
682         gnus-duplicate-mark)
683   "*The list of marks converted into expiration if a group is auto-expirable."
684   :version "24.1"
685   :group 'gnus-summary
686   :type '(repeat character))
687
688 (defcustom gnus-inhibit-user-auto-expire t
689   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
690   :version "21.1"
691   :group 'gnus-summary
692   :type 'boolean)
693
694 (defcustom gnus-mark-copied-or-moved-articles-as-expirable nil
695   "If non-nil, mark articles copied or moved to auto-expire group as expirable.
696 If nil, the expirable marks will be unchanged except that the marks
697 will be removed when copying or moving articles to a group that has
698 not turned auto-expire on.  If non-nil, articles that have been read
699 will be marked as expirable when being copied or moved to a group in
700 which auto-expire is turned on."
701   :version "23.2"
702   :type 'boolean
703   :group 'gnus-summary-marks)
704
705 (defcustom gnus-view-pseudos nil
706   "*If `automatic', pseudo-articles will be viewed automatically.
707 If `not-confirm', pseudos will be viewed automatically, and the user
708 will not be asked to confirm the command."
709   :group 'gnus-extract-view
710   :type '(choice (const :tag "off" nil)
711                  (const automatic)
712                  (const not-confirm)))
713
714 (defcustom gnus-view-pseudos-separately t
715   "*If non-nil, one pseudo-article will be created for each file to be viewed.
716 If nil, all files that use the same viewing command will be given as a
717 list of parameters to that command."
718   :group 'gnus-extract-view
719   :type 'boolean)
720
721 (defcustom gnus-insert-pseudo-articles t
722   "*If non-nil, insert pseudo-articles when decoding articles."
723   :group 'gnus-extract-view
724   :type 'boolean)
725
726 (defcustom gnus-summary-dummy-line-format
727   "   %(:                             :%) %S\n"
728   "*The format specification for the dummy roots in the summary buffer.
729 It works along the same lines as a normal formatting string,
730 with some simple extensions.
731
732 %S  The subject
733
734 General format specifiers can also be used.
735 See `(gnus)Formatting Variables'."
736   :link '(custom-manual "(gnus)Formatting Variables")
737   :group 'gnus-threading
738   :type 'string)
739
740 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
741   "*The format specification for the summary mode line.
742 It works along the same lines as a normal formatting string,
743 with some simple extensions:
744
745 %G  Group name
746 %p  Unprefixed group name
747 %A  Current article number
748 %z  Current article score
749 %V  Gnus version
750 %U  Number of unread articles in the group
751 %e  Number of unselected articles in the group
752 %Z  A string with unread/unselected article counts
753 %g  Shortish group name
754 %S  Subject of the current article
755 %u  User-defined spec
756 %s  Current score file name
757 %d  Number of dormant articles
758 %r  Number of articles that have been marked as read in this session
759 %E  Number of articles expunged by the score files"
760   :group 'gnus-summary-format
761   :type 'string)
762
763 (defcustom gnus-list-identifiers nil
764   "Regexp that matches list identifiers to be removed from subject.
765 This can also be a list of regexps."
766   :version "21.1"
767   :group 'gnus-summary-format
768   :group 'gnus-article-hiding
769   :type '(choice (const :tag "none" nil)
770                  (regexp :value ".*")
771                  (repeat :value (".*") regexp)))
772
773 (defcustom gnus-summary-mark-below 0
774   "*Mark all articles with a score below this variable as read.
775 This variable is local to each summary buffer and usually set by the
776 score file."
777   :group 'gnus-score-default
778   :type 'integer)
779
780 (defun gnus-widget-reversible-match (widget value)
781   "Ignoring WIDGET, convert VALUE to internal form.
782 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
783   ;; (debug value)
784   (or (symbolp value)
785       (and (listp value)
786            (eq (length value) 2)
787            (eq (nth 0 value) 'not)
788            (symbolp (nth 1 value)))))
789
790 (defun gnus-widget-reversible-to-internal (widget value)
791   "Ignoring WIDGET, convert VALUE to internal form.
792 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
793 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
794   ;; (debug value)
795   (if (atom value)
796       (list value nil)
797     (list (nth 1 value) t)))
798
799 (defun gnus-widget-reversible-to-external (widget value)
800   "Ignoring WIDGET, convert VALUE to external form.
801 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
802 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
803   ;; (debug value)
804   (if (nth 1 value)
805       (list 'not (nth 0 value))
806     (nth 0 value)))
807
808 (define-widget 'gnus-widget-reversible 'group
809   "A `group' that convert values."
810   :match 'gnus-widget-reversible-match
811   :value-to-internal 'gnus-widget-reversible-to-internal
812   :value-to-external 'gnus-widget-reversible-to-external)
813
814 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
815   "*List of functions used for sorting articles in the summary buffer.
816
817 Each function takes two articles and returns non-nil if the first
818 article should be sorted before the other.  If you use more than one
819 function, the primary sort function should be the last.  You should
820 probably always include `gnus-article-sort-by-number' in the list of
821 sorting functions -- preferably first.  Also note that sorting by date
822 is often much slower than sorting by number, and the sorting order is
823 very similar.  (Sorting by date means sorting by the time the message
824 was sent, sorting by number means sorting by arrival time.)
825
826 Each item can also be a list `(not F)' where F is a function;
827 this reverses the sort order.
828
829 Ready-made functions include `gnus-article-sort-by-number',
830 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
831 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
832 and `gnus-article-sort-by-score'.
833
834 When threading is turned on, the variable `gnus-thread-sort-functions'
835 controls how articles are sorted."
836   :group 'gnus-summary-sort
837   :type '(repeat (gnus-widget-reversible
838                   (choice (function-item gnus-article-sort-by-number)
839                           (function-item gnus-article-sort-by-author)
840                           (function-item gnus-article-sort-by-subject)
841                           (function-item gnus-article-sort-by-date)
842                           (function-item gnus-article-sort-by-score)
843                           (function-item gnus-article-sort-by-random)
844                           (function :tag "other"))
845                   (boolean :tag "Reverse order"))))
846
847 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
848   "*List of functions used for sorting threads in the summary buffer.
849 By default, threads are sorted by article number.
850
851 Each function takes two threads and returns non-nil if the first
852 thread should be sorted before the other.  If you use more than one
853 function, the primary sort function should be the last.  You should
854 probably always include `gnus-thread-sort-by-number' in the list of
855 sorting functions -- preferably first.  Also note that sorting by date
856 is often much slower than sorting by number, and the sorting order is
857 very similar.  (Sorting by date means sorting by the time the message
858 was sent, sorting by number means sorting by arrival time.)
859
860 Each list item can also be a list `(not F)' where F is a
861 function; this specifies reversed sort order.
862
863 Ready-made functions include `gnus-thread-sort-by-number',
864 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
865 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
866 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
867 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
868 and `gnus-thread-sort-by-total-score' (see
869 `gnus-thread-score-function').
870
871 When threading is turned off, the variable
872 `gnus-article-sort-functions' controls how articles are sorted.
873
874 By default, threads and their subthreads are sorted according to
875 the value of this variable.  To use a different sorting order for
876 subthreads, customize `gnus-subthread-sort-functions'."
877   :group 'gnus-summary-sort
878   :type '(repeat
879           (gnus-widget-reversible
880            (choice (function-item gnus-thread-sort-by-number)
881                    (function-item gnus-thread-sort-by-author)
882                    (function-item gnus-thread-sort-by-recipient)
883                    (function-item gnus-thread-sort-by-subject)
884                    (function-item gnus-thread-sort-by-date)
885                    (function-item gnus-thread-sort-by-score)
886                    (function-item gnus-thread-sort-by-most-recent-number)
887                    (function-item gnus-thread-sort-by-most-recent-date)
888                    (function-item gnus-thread-sort-by-random)
889                    (function-item gnus-thread-sort-by-total-score)
890                    (function :tag "other"))
891            (boolean :tag "Reverse order"))))
892
893 (defcustom gnus-subthread-sort-functions 'gnus-thread-sort-functions
894   "*List of functions used for sorting subthreads in the summary buffer.
895 By default, subthreads are sorted the same as threads, i.e.,
896 according to the value of `gnus-thread-sort-functions'."
897   :version "24.4"
898   :group 'gnus-summary-sort
899   :type '(choice
900           (const :tag "Sort subthreads like threads" gnus-thread-sort-functions)
901           (repeat
902            (gnus-widget-reversible
903             (choice (function-item gnus-thread-sort-by-number)
904                     (function-item gnus-thread-sort-by-author)
905                     (function-item gnus-thread-sort-by-recipient)
906                     (function-item gnus-thread-sort-by-subject)
907                     (function-item gnus-thread-sort-by-date)
908                     (function-item gnus-thread-sort-by-score)
909                     (function-item gnus-thread-sort-by-most-recent-number)
910                     (function-item gnus-thread-sort-by-most-recent-date)
911                     (function-item gnus-thread-sort-by-random)
912                     (function-item gnus-thread-sort-by-total-score)
913                     (function :tag "other"))
914             (boolean :tag "Reverse order")))))
915
916 (defcustom gnus-thread-score-function '+
917   "*Function used for calculating the total score of a thread.
918
919 The function is called with the scores of the article and each
920 subthread and should then return the score of the thread.
921
922 Some functions you can use are `+', `max', or `min'."
923   :group 'gnus-summary-sort
924   :type 'function)
925
926 (defcustom gnus-summary-expunge-below nil
927   "All articles that have a score less than this variable will be expunged.
928 This variable is local to the summary buffers."
929   :group 'gnus-score-default
930   :type '(choice (const :tag "off" nil)
931                  integer))
932
933 (defcustom gnus-thread-expunge-below nil
934   "All threads that have a total score less than this variable will be expunged.
935 See `gnus-thread-score-function' for en explanation of what a
936 \"thread score\" is.
937
938 This variable is local to the summary buffers."
939   :group 'gnus-threading
940   :group 'gnus-score-default
941   :type '(choice (const :tag "off" nil)
942                  integer))
943
944 (defcustom gnus-summary-mode-hook nil
945   "*A hook for Gnus summary mode.
946 This hook is run before any variables are set in the summary buffer."
947   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
948   :group 'gnus-summary-various
949   :type 'hook)
950
951 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
952 (when (featurep 'xemacs)
953   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
954   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
955   (add-hook 'gnus-summary-mode-hook
956             'gnus-xmas-switch-horizontal-scrollbar-off))
957
958 (defcustom gnus-summary-menu-hook nil
959   "*Hook run after the creation of the summary mode menu."
960   :group 'gnus-summary-visual
961   :type 'hook)
962
963 (defcustom gnus-summary-exit-hook nil
964   "*A hook called on exit from the summary buffer.
965 It will be called with point in the group buffer."
966   :group 'gnus-summary-exit
967   :type 'hook)
968
969 (defcustom gnus-summary-prepare-hook nil
970   "*A hook called after the summary buffer has been generated.
971 If you want to modify the summary buffer, you can use this hook."
972   :group 'gnus-summary-various
973   :type 'hook)
974
975 (defcustom gnus-summary-prepared-hook nil
976   "*A hook called as the last thing after the summary buffer has been generated."
977   :group 'gnus-summary-various
978   :type 'hook)
979
980 (defcustom gnus-summary-generate-hook nil
981   "*A hook run just before generating the summary buffer.
982 This hook is commonly used to customize threading variables and the
983 like."
984   :group 'gnus-summary-various
985   :type 'hook)
986
987 (defcustom gnus-select-group-hook nil
988   "*A hook called when a newsgroup is selected.
989
990 If you'd like to simplify subjects like the
991 `gnus-summary-next-same-subject' command does, you can use the
992 following hook:
993
994  (add-hook gnus-select-group-hook
995            (lambda ()
996              (mapcar (lambda (header)
997                        (mail-header-set-subject
998                         header
999                         (gnus-simplify-subject
1000                          (mail-header-subject header) 're-only)))
1001                      gnus-newsgroup-headers)))"
1002   :group 'gnus-group-select
1003   :type 'hook)
1004
1005 (defcustom gnus-select-article-hook nil
1006   "*A hook called when an article is selected."
1007   :group 'gnus-summary-choose
1008   :options '(gnus-agent-fetch-selected-article)
1009   :type 'hook)
1010
1011 (defcustom gnus-visual-mark-article-hook
1012   (list 'gnus-highlight-selected-summary)
1013   "*Hook run after selecting an article in the summary buffer.
1014 It is meant to be used for highlighting the article in some way.  It
1015 is not run if `gnus-visual' is nil."
1016   :group 'gnus-summary-visual
1017   :type 'hook)
1018
1019 (defcustom gnus-parse-headers-hook nil
1020   "*A hook called before parsing the headers."
1021   :group 'gnus-various
1022   :type 'hook)
1023
1024 (defcustom gnus-exit-group-hook nil
1025   "*A hook called when exiting summary mode.
1026 This hook is not called from the non-updating exit commands like `Q'."
1027   :group 'gnus-various
1028   :type 'hook)
1029
1030 (defcustom gnus-summary-update-hook nil
1031   "*A hook called when a summary line is changed.
1032 The hook will not be called if `gnus-visual' is nil.
1033
1034 The default function `gnus-summary-highlight-line' will
1035 highlight the line according to the `gnus-summary-highlight'
1036 variable."
1037   :group 'gnus-summary-visual
1038   :type 'hook)
1039
1040 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1041   "*A hook called when an article is selected for the first time.
1042 The hook is intended to mark an article as read (or unread)
1043 automatically when it is selected."
1044   :group 'gnus-summary-choose
1045   :type 'hook)
1046
1047 (defcustom gnus-group-no-more-groups-hook nil
1048   "*A hook run when returning to group mode having no more (unread) groups."
1049   :group 'gnus-group-select
1050   :type 'hook)
1051
1052 (defcustom gnus-ps-print-hook nil
1053   "*A hook run before ps-printing something from Gnus."
1054   :group 'gnus-summary
1055   :type 'hook)
1056
1057 (defcustom gnus-summary-article-move-hook nil
1058   "*A hook called after an article is moved, copied, respooled, or crossposted."
1059   :version "22.1"
1060   :group 'gnus-summary
1061   :type 'hook)
1062
1063 (defcustom gnus-summary-article-delete-hook nil
1064   "*A hook called after an article is deleted."
1065   :version "22.1"
1066   :group 'gnus-summary
1067   :type 'hook)
1068
1069 (defcustom gnus-summary-article-expire-hook nil
1070   "*A hook called after an article is expired."
1071   :version "22.1"
1072   :group 'gnus-summary
1073   :type 'hook)
1074
1075 (defcustom gnus-summary-display-arrow
1076   (and (fboundp 'display-graphic-p)
1077        (display-graphic-p))
1078   "*If non-nil, display an arrow highlighting the current article."
1079   :version "22.1"
1080   :group 'gnus-summary
1081   :type 'boolean)
1082
1083 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1084   "Face used for highlighting the current article in the summary buffer."
1085   :group 'gnus-summary-visual
1086   :type 'face)
1087
1088 (defvar gnus-tmp-downloaded nil)
1089
1090 (defcustom gnus-summary-highlight
1091   '(((eq mark gnus-canceled-mark)
1092      . gnus-summary-cancelled)
1093     ((and uncached (> score default-high))
1094      . gnus-summary-high-undownloaded)
1095     ((and uncached (< score default-low))
1096      . gnus-summary-low-undownloaded)
1097     (uncached
1098      . gnus-summary-normal-undownloaded)
1099     ((and (> score default-high)
1100           (or (eq mark gnus-dormant-mark)
1101               (eq mark gnus-ticked-mark)))
1102      . gnus-summary-high-ticked)
1103     ((and (< score default-low)
1104           (or (eq mark gnus-dormant-mark)
1105               (eq mark gnus-ticked-mark)))
1106      . gnus-summary-low-ticked)
1107     ((or (eq mark gnus-dormant-mark)
1108          (eq mark gnus-ticked-mark))
1109      . gnus-summary-normal-ticked)
1110     ((and (> score default-high) (eq mark gnus-ancient-mark))
1111      . gnus-summary-high-ancient)
1112     ((and (< score default-low) (eq mark gnus-ancient-mark))
1113      . gnus-summary-low-ancient)
1114     ((eq mark gnus-ancient-mark)
1115      . gnus-summary-normal-ancient)
1116     ((and (> score default-high) (eq mark gnus-unread-mark))
1117      . gnus-summary-high-unread)
1118     ((and (< score default-low) (eq mark gnus-unread-mark))
1119      . gnus-summary-low-unread)
1120     ((eq mark gnus-unread-mark)
1121      . gnus-summary-normal-unread)
1122     ((> score default-high)
1123      . gnus-summary-high-read)
1124     ((< score default-low)
1125      . gnus-summary-low-read)
1126     (t
1127      . gnus-summary-normal-read))
1128   "*Controls the highlighting of summary buffer lines.
1129
1130 A list of (FORM . FACE) pairs.  When deciding how a particular
1131 summary line should be displayed, each form is evaluated.  The content
1132 of the face field after the first true form is used.  You can change
1133 how those summary lines are displayed, by editing the face field.
1134
1135 You can use the following variables in the FORM field.
1136
1137 score:        The article's score.
1138 default:      The default article score.
1139 default-high: The default score for high scored articles.
1140 default-low:  The default score for low scored articles.
1141 mark:         The article's mark.
1142 uncached:     Non-nil if the article is uncached."
1143   :group 'gnus-summary-visual
1144   :type '(repeat (cons (sexp :tag "Form" nil)
1145                        face)))
1146 (put 'gnus-summary-highlight 'risky-local-variable t)
1147
1148 (defcustom gnus-alter-header-function nil
1149   "Function called to allow alteration of article header structures.
1150 The function is called with one parameter, the article header vector,
1151 which it may alter in any way."
1152   :type '(choice (const :tag "None" nil)
1153                  function)
1154   :group 'gnus-summary)
1155
1156 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1157   "Function used to decode a string with encoded words.")
1158
1159 (defvar gnus-decode-encoded-address-function
1160   'mail-decode-encoded-address-string
1161   "Function used to decode addresses with encoded words.")
1162
1163 (defcustom gnus-extra-headers '(To Cc Keywords Gcc Newsgroups)
1164   "*Extra headers to parse."
1165   :version "24.1"                       ; added Cc Keywords Gcc
1166   :group 'gnus-summary
1167   :type '(repeat symbol))
1168
1169 (defcustom gnus-ignored-from-addresses
1170   (and user-mail-address
1171        (not (string= user-mail-address ""))
1172        (regexp-quote user-mail-address))
1173   "*From headers that may be suppressed in favor of To headers.
1174 This can be a regexp or a list of regexps."
1175   :version "21.1"
1176   :group 'gnus-summary
1177   :type '(choice regexp
1178                  (repeat :tag "Regexp List" regexp)))
1179
1180 (defsubst gnus-ignored-from-addresses ()
1181   (gmm-regexp-concat gnus-ignored-from-addresses))
1182
1183 (defcustom gnus-summary-to-prefix "-> "
1184   "*String prefixed to the To field in the summary line when
1185 using `gnus-ignored-from-addresses'."
1186   :version "22.1"
1187   :group 'gnus-summary
1188   :type 'string)
1189
1190 (defcustom gnus-summary-newsgroup-prefix "=> "
1191   "*String prefixed to the Newsgroup field in the summary
1192 line when using the option `gnus-ignored-from-addresses'."
1193   :version "22.1"
1194   :group 'gnus-summary
1195   :type 'string)
1196
1197 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1198   "List of charsets that should be ignored.
1199 When these charsets are used in the \"charset\" parameter, the
1200 default charset will be used instead."
1201   :version "21.1"
1202   :type '(repeat symbol)
1203   :group 'gnus-charset)
1204
1205 (defcustom gnus-newsgroup-maximum-articles nil
1206   "The maximum number of articles a newsgroup.
1207 If this is a number, old articles in a newsgroup exceeding this number
1208 are silently ignored.  If it is nil, no article is ignored.  Note that
1209 setting this variable to a number might prevent you from reading very
1210 old articles."
1211   :group 'gnus-group-select
1212   :version "22.2"
1213   :type '(choice (const :tag "No limit" nil)
1214                  integer))
1215
1216 (gnus-define-group-parameter
1217  ignored-charsets
1218  :type list
1219  :function-document
1220  "Return the ignored charsets of GROUP."
1221  :variable gnus-group-ignored-charsets-alist
1222  :variable-default
1223  '(("alt\\.chinese\\.text" iso-8859-1))
1224  :variable-document
1225  "Alist of regexps (to match group names) and charsets that should be ignored.
1226 When these charsets are used in the \"charset\" parameter, the
1227 default charset will be used instead."
1228  :variable-group gnus-charset
1229  :variable-type '(repeat (cons (regexp :tag "Group")
1230                                (repeat symbol)))
1231  :parameter-type '(choice :tag "Ignored charsets"
1232                           :value nil
1233                           (repeat (symbol)))
1234  :parameter-document       "\
1235 List of charsets that should be ignored.
1236
1237 When these charsets are used in the \"charset\" parameter, the
1238 default charset will be used instead.")
1239
1240 (defcustom gnus-group-highlight-words-alist nil
1241   "Alist of group regexps and highlight regexps.
1242 This variable uses the same syntax as `gnus-emphasis-alist'."
1243   :version "21.1"
1244   :type '(repeat (cons (regexp :tag "Group")
1245                        (repeat (list (regexp :tag "Highlight regexp")
1246                                      (number :tag "Group for entire word" 0)
1247                                      (number :tag "Group for displayed part" 0)
1248                                      (symbol :tag "Face"
1249                                              gnus-emphasis-highlight-words)))))
1250   :group 'gnus-summary-visual)
1251
1252 (defcustom gnus-summary-show-article-charset-alist
1253   nil
1254   "Alist of number and charset.
1255 The article will be shown with the charset corresponding to the
1256 numbered argument.
1257 For example: ((1 . cn-gb-2312) (2 . big5))."
1258   :version "21.1"
1259   :type '(repeat (cons (number :tag "Argument" 1)
1260                        (symbol :tag "Charset")))
1261   :group 'gnus-charset)
1262
1263 (defcustom gnus-preserve-marks t
1264   "Whether marks are preserved when moving, copying and respooling messages."
1265   :version "21.1"
1266   :type 'boolean
1267   :group 'gnus-summary-marks)
1268
1269 (defcustom gnus-alter-articles-to-read-function nil
1270   "Function to be called to alter the list of articles to be selected."
1271   :type '(choice (const nil) function)
1272   :group 'gnus-summary)
1273
1274 (defcustom gnus-orphan-score nil
1275   "*All orphans get this score added.  Set in the score file."
1276   :group 'gnus-score-default
1277   :type '(choice (const nil)
1278                  integer))
1279
1280 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1281   "*A regexp to match MIME parts when saving multiple parts of a
1282 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1283 This regexp will be used by default when prompting the user for which
1284 type of files to save."
1285   :group 'gnus-summary
1286   :type 'regexp)
1287
1288 (defcustom gnus-read-all-available-headers nil
1289   "Whether Gnus should parse all headers made available to it.
1290 This is mostly relevant for slow back ends where the user may
1291 wish to widen the summary buffer to include all headers
1292 that were fetched."
1293   :version "22.1"
1294   :group 'gnus-summary
1295   :type '(choice boolean regexp))
1296
1297 (defcustom gnus-summary-pipe-output-default-command nil
1298   "Command (and optional arguments) used to pipe article to subprocess.
1299 This will be used as the default command if it is non-nil.  The value
1300 will be updated if you modify it when executing the command
1301 `gnus-summary-pipe-output' or the function `gnus-summary-save-in-pipe'."
1302   :version "23.1" ;; No Gnus
1303   :group 'gnus-summary
1304   :type '(radio (const :tag "None" nil) (string :tag "Command")))
1305
1306 (defcustom gnus-summary-muttprint-program "muttprint"
1307   "Command (and optional arguments) used to run Muttprint.
1308 The value will be updated if you modify it when executing the command
1309 `gnus-summary-muttprint'."
1310   :version "22.1"
1311   :group 'gnus-summary
1312   :type 'string)
1313
1314 (defcustom gnus-article-loose-mime t
1315   "If non-nil, don't require MIME-Version header.
1316 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1317 supply the MIME-Version header or deliberately strip it from the mail.
1318 If non-nil (the default), Gnus will treat some articles as MIME
1319 even if the MIME-Version header is missing."
1320   :version "22.1"
1321   :type 'boolean
1322   :group 'gnus-article-mime)
1323
1324 (defcustom gnus-article-emulate-mime t
1325   "If non-nil, use MIME emulation for uuencode and the like.
1326 This means that Gnus will search message bodies for text that look
1327 like uuencoded bits, yEncoded bits, and so on, and present that using
1328 the normal Gnus MIME machinery."
1329   :version "22.1"
1330   :type 'boolean
1331   :group 'gnus-article-mime)
1332
1333 ;;; Internal variables
1334
1335 (defvar gnus-summary-display-cache nil)
1336 (defvar gnus-article-mime-handles nil)
1337 (defvar gnus-article-decoded-p nil)
1338 (defvar gnus-article-charset nil)
1339 (defvar gnus-article-ignored-charsets nil)
1340 (defvar gnus-scores-exclude-files nil)
1341 (defvar gnus-page-broken nil)
1342
1343 (defvar gnus-original-article nil)
1344 (defvar gnus-article-internal-prepare-hook nil)
1345 (defvar gnus-newsgroup-process-stack nil)
1346
1347 (defvar gnus-thread-indent-array nil)
1348 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1349 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1350   "Function called to sort the articles within a thread after it has been gathered together.")
1351
1352 (defvar gnus-summary-save-parts-type-history nil)
1353 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1354
1355 ;; Avoid highlighting in kill files.
1356 (defvar gnus-summary-inhibit-highlight nil)
1357 (defvar gnus-newsgroup-selected-overlay nil)
1358 (defvar gnus-inhibit-limiting nil)
1359 (defvar gnus-newsgroup-adaptive-score-file nil)
1360 (defvar gnus-current-score-file nil)
1361 (defvar gnus-current-move-group nil)
1362 (defvar gnus-current-copy-group nil)
1363 (defvar gnus-current-crosspost-group nil)
1364 (defvar gnus-newsgroup-display nil)
1365
1366 (defvar gnus-newsgroup-dependencies nil)
1367 (defvar gnus-newsgroup-adaptive nil)
1368 (defvar gnus-summary-display-article-function nil)
1369 (defvar gnus-summary-highlight-line-function nil
1370   "Function called after highlighting a summary line.")
1371
1372 (defvar gnus-summary-line-format-alist
1373   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1374     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1375     (?s gnus-tmp-subject-or-nil ?s)
1376     (?n gnus-tmp-name ?s)
1377     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1378         ?s)
1379     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1380             gnus-tmp-from) ?s)
1381     (?F gnus-tmp-from ?s)
1382     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1383     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1384     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1385     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1386     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1387     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1388     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1389     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1390     (?L gnus-tmp-lines ?s)
1391     (?Z (or (nnir-article-rsv (mail-header-number gnus-tmp-header))
1392             0) ?d)
1393     (?G (or (nnir-article-group (mail-header-number gnus-tmp-header))
1394             "") ?s)
1395     (?g (or (gnus-group-short-name
1396              (nnir-article-group (mail-header-number gnus-tmp-header)))
1397             "") ?s)
1398     (?O gnus-tmp-downloaded ?c)
1399     (?I gnus-tmp-indentation ?s)
1400     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1401     (?R gnus-tmp-replied ?c)
1402     (?\[ gnus-tmp-opening-bracket ?c)
1403     (?\] gnus-tmp-closing-bracket ?c)
1404     (?\> (make-string gnus-tmp-level ? ) ?s)
1405     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1406     (?i gnus-tmp-score ?d)
1407     (?z gnus-tmp-score-char ?c)
1408     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1409     (?U gnus-tmp-unread ?c)
1410     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1411         ?s)
1412     (?t (gnus-summary-number-of-articles-in-thread
1413          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1414         ?d)
1415     (?e (gnus-summary-number-of-articles-in-thread
1416          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1417         ?c)
1418     (?u gnus-tmp-user-defined ?s)
1419     (?P (gnus-pick-line-number) ?d)
1420     (?B gnus-tmp-thread-tree-header-string ?s)
1421     (user-date (gnus-user-date
1422                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1423   "An alist of format specifications that can appear in summary lines.
1424 These are paired with what variables they correspond with, along with
1425 the type of the variable (string, integer, character, etc).")
1426
1427 (defvar gnus-summary-dummy-line-format-alist
1428   `((?S gnus-tmp-subject ?s)
1429     (?N gnus-tmp-number ?d)
1430     (?u gnus-tmp-user-defined ?s)))
1431
1432 (defvar gnus-summary-mode-line-format-alist
1433   `((?G gnus-tmp-group-name ?s)
1434     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1435     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1436     (?A gnus-tmp-article-number ?d)
1437     (?Z gnus-tmp-unread-and-unselected ?s)
1438     (?V gnus-version ?s)
1439     (?U gnus-tmp-unread-and-unticked ?d)
1440     (?S gnus-tmp-subject ?s)
1441     (?e gnus-tmp-unselected ?d)
1442     (?u gnus-tmp-user-defined ?s)
1443     (?d (length gnus-newsgroup-dormant) ?d)
1444     (?t (length gnus-newsgroup-marked) ?d)
1445     (?h (length gnus-newsgroup-spam-marked) ?d)
1446     (?r (length gnus-newsgroup-reads) ?d)
1447     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1448     (?E gnus-newsgroup-expunged-tally ?d)
1449     (?s (gnus-current-score-file-nondirectory) ?s)))
1450
1451 ;; This is here rather than in gnus-art for compilation reasons.
1452 (defvar gnus-article-mode-line-format-alist
1453   (nconc '((?w (gnus-article-wash-status) ?s)
1454            (?m (gnus-article-mime-part-status) ?s))
1455          gnus-summary-mode-line-format-alist))
1456
1457 (defvar gnus-last-search-regexp nil
1458   "Default regexp for article search command.")
1459
1460 (defvar gnus-last-shell-command nil
1461   "Default shell command on article.")
1462
1463 (defvar gnus-newsgroup-agentized nil
1464   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1465 (defvar gnus-newsgroup-begin nil)
1466 (defvar gnus-newsgroup-end nil)
1467 (defvar gnus-newsgroup-last-rmail nil)
1468 (defvar gnus-newsgroup-last-mail nil)
1469 (defvar gnus-newsgroup-last-folder nil)
1470 (defvar gnus-newsgroup-last-file nil)
1471 (defvar gnus-newsgroup-last-directory nil)
1472 (defvar gnus-newsgroup-auto-expire nil)
1473 (defvar gnus-newsgroup-active nil)
1474 (defvar gnus-newsgroup-highest nil)
1475
1476 (defvar gnus-newsgroup-data nil)
1477 (defvar gnus-newsgroup-data-reverse nil)
1478 (defvar gnus-newsgroup-limit nil)
1479 (defvar gnus-newsgroup-limits nil)
1480 (defvar gnus-summary-use-undownloaded-faces nil)
1481
1482 (defvar gnus-newsgroup-unreads nil
1483   "Sorted list of unread articles in the current newsgroup.")
1484
1485 (defvar gnus-newsgroup-unselected nil
1486   "Sorted list of unselected unread articles in the current newsgroup.")
1487
1488 (defvar gnus-newsgroup-reads nil
1489   "Alist of read articles and article marks in the current newsgroup.")
1490
1491 (defvar gnus-newsgroup-expunged-tally nil)
1492
1493 (defvar gnus-newsgroup-marked nil
1494   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1495
1496 (defvar gnus-newsgroup-spam-marked nil
1497   "List of ranges of articles that have been marked as spam.")
1498
1499 (defvar gnus-newsgroup-killed nil
1500   "List of ranges of articles that have been through the scoring process.")
1501
1502 (defvar gnus-newsgroup-cached nil
1503   "Sorted list of articles that come from the article cache.")
1504
1505 (defvar gnus-newsgroup-saved nil
1506   "List of articles that have been saved.")
1507
1508 (defvar gnus-newsgroup-kill-headers nil)
1509
1510 (defvar gnus-newsgroup-replied nil
1511   "List of articles that have been replied to in the current newsgroup.")
1512
1513 (defvar gnus-newsgroup-forwarded nil
1514   "List of articles that have been forwarded in the current newsgroup.")
1515
1516 (defvar gnus-newsgroup-expirable nil
1517   "Sorted list of articles in the current newsgroup that can be expired.")
1518
1519 (defvar gnus-newsgroup-processable nil
1520   "List of articles in the current newsgroup that can be processed.")
1521
1522 (defvar gnus-newsgroup-downloadable nil
1523   "Sorted list of articles in the current newsgroup that can be processed.")
1524
1525 (defvar gnus-newsgroup-unfetched nil
1526   "Sorted list of articles in the current newsgroup whose headers have
1527 not been fetched into the agent.
1528
1529 This list will always be a subset of gnus-newsgroup-undownloaded.")
1530
1531 (defvar gnus-newsgroup-undownloaded nil
1532   "List of articles in the current newsgroup that haven't been downloaded.")
1533
1534 (defvar gnus-newsgroup-unsendable nil
1535   "List of articles in the current newsgroup that won't be sent.")
1536
1537 (defvar gnus-newsgroup-bookmarks nil
1538   "List of articles in the current newsgroup that have bookmarks.")
1539
1540 (defvar gnus-newsgroup-dormant nil
1541   "Sorted list of dormant articles in the current newsgroup.")
1542
1543 (defvar gnus-newsgroup-unseen nil
1544   "List of unseen articles in the current newsgroup.")
1545
1546 (defvar gnus-newsgroup-seen nil
1547   "Range of seen articles in the current newsgroup.")
1548
1549 (defvar gnus-newsgroup-unexist nil
1550   "Range of unexisting articles in the current newsgroup.")
1551
1552 (defvar gnus-newsgroup-articles nil
1553   "List of articles in the current newsgroup.")
1554
1555 (defvar gnus-newsgroup-scored nil
1556   "List of scored articles in the current newsgroup.")
1557
1558 (defvar gnus-newsgroup-headers nil
1559   "List of article headers in the current newsgroup.")
1560
1561 (defvar gnus-newsgroup-threads nil)
1562
1563 (defvar gnus-newsgroup-prepared nil
1564   "Whether the current group has been prepared properly.")
1565
1566 (defvar gnus-newsgroup-ancient nil
1567   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1568
1569 (defvar gnus-newsgroup-sparse nil)
1570
1571 (defvar gnus-current-article nil)
1572 (defvar gnus-article-current nil)
1573 (defvar gnus-current-headers nil)
1574 (defvar gnus-have-all-headers nil)
1575 (defvar gnus-last-article nil)
1576 (defvar gnus-newsgroup-history nil)
1577 (defvar gnus-newsgroup-charset nil)
1578 (defvar gnus-newsgroup-ephemeral-charset nil)
1579 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1580
1581 (defvar gnus-article-before-search nil)
1582
1583 (defvar gnus-summary-local-variables
1584   '(gnus-newsgroup-name
1585
1586     ;; Marks lists
1587     gnus-newsgroup-unreads
1588     gnus-newsgroup-unselected
1589     gnus-newsgroup-marked
1590     gnus-newsgroup-spam-marked
1591     gnus-newsgroup-reads
1592     gnus-newsgroup-saved
1593     gnus-newsgroup-replied
1594     gnus-newsgroup-forwarded
1595     gnus-newsgroup-expirable
1596     gnus-newsgroup-killed
1597     gnus-newsgroup-unseen
1598     gnus-newsgroup-seen
1599     gnus-newsgroup-unexist
1600     gnus-newsgroup-cached
1601     gnus-newsgroup-downloadable
1602     gnus-newsgroup-undownloaded
1603     gnus-newsgroup-unsendable
1604
1605     gnus-newsgroup-begin gnus-newsgroup-end
1606     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1607     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1608     gnus-newsgroup-last-directory
1609     gnus-newsgroup-auto-expire
1610     gnus-newsgroup-processable
1611     gnus-newsgroup-unfetched
1612     gnus-newsgroup-articles
1613     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1614     gnus-newsgroup-headers gnus-newsgroup-threads
1615     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1616     gnus-current-article gnus-current-headers gnus-have-all-headers
1617     gnus-last-article gnus-article-internal-prepare-hook
1618     (gnus-summary-article-delete-hook . global)
1619     (gnus-summary-article-move-hook . global)
1620     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1621     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1622     gnus-thread-expunge-below
1623     gnus-score-alist gnus-current-score-file
1624     (gnus-summary-expunge-below . global)
1625     (gnus-summary-mark-below . global)
1626     (gnus-orphan-score . global)
1627     gnus-newsgroup-active gnus-scores-exclude-files
1628     gnus-newsgroup-highest
1629     gnus-newsgroup-history gnus-newsgroup-ancient
1630     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1631     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1632     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1633     (gnus-newsgroup-expunged-tally . 0)
1634     gnus-cache-removable-articles
1635     gnus-newsgroup-data gnus-newsgroup-data-reverse
1636     gnus-newsgroup-limit gnus-newsgroup-limits
1637     gnus-newsgroup-charset gnus-newsgroup-display
1638     gnus-summary-use-undownloaded-faces)
1639   "Variables that are buffer-local to the summary buffers.")
1640
1641 (defvar gnus-newsgroup-variables nil
1642   "A list of variables that have separate values in different newsgroups.
1643 A list of newsgroup (summary buffer) local variables, or cons of
1644 variables and their default expressions to be evalled (when the default
1645 values are not nil), that should be made global while the summary buffer
1646 is active.
1647
1648 Note: The default expressions will be evaluated (using function `eval')
1649 before assignment to the local variable rather than just assigned to it.
1650 If the default expression is the symbol `global', that symbol will not
1651 be evaluated but the global value of the local variable will be used
1652 instead.
1653
1654 These variables can be used to set variables in the group parameters
1655 while still allowing them to affect operations done in other buffers.
1656 For example:
1657
1658 \(setq gnus-newsgroup-variables
1659      '(message-use-followup-to
1660        (gnus-visible-headers .
1661          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1662 ")
1663
1664 (eval-when-compile
1665   ;; Bind features so that require will believe that gnus-sum has
1666   ;; already been loaded (avoids infinite recursion)
1667   (let ((features (cons 'gnus-sum features)))
1668     (require 'gnus-art)))
1669
1670 ;; MIME stuff.
1671
1672 (defvar gnus-decode-encoded-word-methods
1673   '(mail-decode-encoded-word-string)
1674   "List of methods used to decode encoded words.
1675
1676 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1677 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1678 \(REGEXP . FUNCTION), FUNCTION will be applied only to the newsgroups
1679 whose names match REGEXP.
1680
1681 For example:
1682 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1683  mail-decode-encoded-word-string
1684  (\"chinese\" . rfc1843-decode-string))")
1685
1686 (defvar gnus-decode-encoded-word-methods-cache nil)
1687
1688 (defun gnus-multi-decode-encoded-word-string (string)
1689   "Apply the functions from `gnus-encoded-word-methods' that match."
1690   (unless (and gnus-decode-encoded-word-methods-cache
1691                (eq gnus-newsgroup-name
1692                    (car gnus-decode-encoded-word-methods-cache)))
1693     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1694     (dolist (method gnus-decode-encoded-word-methods)
1695       (if (symbolp method)
1696           (nconc gnus-decode-encoded-word-methods-cache (list method))
1697         (if (and gnus-newsgroup-name
1698                  (string-match (car method) gnus-newsgroup-name))
1699             (nconc gnus-decode-encoded-word-methods-cache
1700                    (list (cdr method)))))))
1701   (dolist (method (cdr gnus-decode-encoded-word-methods-cache) string)
1702     (setq string (funcall method string))))
1703
1704 ;; Subject simplification.
1705
1706 (defun gnus-simplify-whitespace (str)
1707   "Remove excessive whitespace from STR."
1708   ;; Multiple spaces.
1709   (while (string-match "[ \t][ \t]+" str)
1710     (setq str (concat (substring str 0 (match-beginning 0))
1711                         " "
1712                         (substring str (match-end 0)))))
1713   ;; Leading spaces.
1714   (when (string-match "^[ \t]+" str)
1715     (setq str (substring str (match-end 0))))
1716   ;; Trailing spaces.
1717   (when (string-match "[ \t]+$" str)
1718     (setq str (substring str 0 (match-beginning 0))))
1719   str)
1720
1721 (defun gnus-simplify-all-whitespace (str)
1722   "Remove all whitespace from STR."
1723   (while (string-match "[ \t\n]+" str)
1724     (setq str (replace-match "" nil nil str)))
1725   str)
1726
1727 (defsubst gnus-simplify-subject-re (subject)
1728   "Remove \"Re:\" from subject lines."
1729   (if (string-match message-subject-re-regexp subject)
1730       (substring subject (match-end 0))
1731     subject))
1732
1733 (defun gnus-simplify-subject (subject &optional re-only)
1734   "Remove `Re:' and words in parentheses.
1735 If RE-ONLY is non-nil, strip leading `Re:'s only."
1736   (let ((case-fold-search t))           ;Ignore case.
1737     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1738     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1739       (setq subject (substring subject (match-end 0))))
1740     ;; Remove uninteresting prefixes.
1741     (when (and (not re-only)
1742                gnus-simplify-ignored-prefixes
1743                (string-match gnus-simplify-ignored-prefixes subject))
1744       (setq subject (substring subject (match-end 0))))
1745     ;; Remove words in parentheses from end.
1746     (unless re-only
1747       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1748         (setq subject (substring subject 0 (match-beginning 0)))))
1749     ;; Return subject string.
1750     subject))
1751
1752 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1753 ;; all whitespace.
1754 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1755   (goto-char (point-min))
1756   (while (re-search-forward regexp nil t)
1757     (replace-match (or newtext ""))))
1758
1759 (defun gnus-simplify-buffer-fuzzy (regexp)
1760   "Simplify string in the buffer fuzzily.
1761 The string in the accessible portion of the current buffer is simplified.
1762 It is assumed to be a single-line subject.
1763 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1764 matter is removed.  Additional things can be deleted by setting
1765 `gnus-simplify-subject-fuzzy-regexp'."
1766   (let ((case-fold-search t)
1767         (modified-tick))
1768     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1769
1770     (while (not (eq modified-tick (buffer-modified-tick)))
1771       (setq modified-tick (buffer-modified-tick))
1772       (cond
1773        ((listp regexp)
1774         (mapc 'gnus-simplify-buffer-fuzzy-step regexp))
1775        (regexp
1776         (gnus-simplify-buffer-fuzzy-step regexp)))
1777       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1778       (gnus-simplify-buffer-fuzzy-step
1779        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1780       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1781
1782     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1783     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1784     (gnus-simplify-buffer-fuzzy-step " $")
1785     (gnus-simplify-buffer-fuzzy-step "^ +")))
1786
1787 (defun gnus-simplify-subject-fuzzy (subject)
1788   "Simplify a subject string fuzzily.
1789 See `gnus-simplify-buffer-fuzzy' for details."
1790   (save-excursion
1791     (let ((regexp gnus-simplify-subject-fuzzy-regexp))
1792       (gnus-set-work-buffer)
1793       (let ((case-fold-search t))
1794         ;; Remove uninteresting prefixes.
1795         (when (and gnus-simplify-ignored-prefixes
1796                    (string-match gnus-simplify-ignored-prefixes subject))
1797           (setq subject (substring subject (match-end 0))))
1798         (insert subject)
1799         (inline (gnus-simplify-buffer-fuzzy regexp))
1800         (buffer-string)))))
1801
1802 (defsubst gnus-simplify-subject-fully (subject)
1803   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1804   (cond
1805    (gnus-simplify-subject-functions
1806     (gnus-map-function gnus-simplify-subject-functions subject))
1807    ((null gnus-summary-gather-subject-limit)
1808     (gnus-simplify-subject-re subject))
1809    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1810     (gnus-simplify-subject-fuzzy subject))
1811    ((numberp gnus-summary-gather-subject-limit)
1812     (truncate-string-to-width (gnus-simplify-subject-re subject)
1813                               gnus-summary-gather-subject-limit))
1814    (t
1815     subject)))
1816
1817 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1818   "Check whether two subjects are equal.
1819 If optional argument SIMPLE-FIRST is t, first argument is already
1820 simplified."
1821   (cond
1822    ((null simple-first)
1823     (equal (gnus-simplify-subject-fully s1)
1824            (gnus-simplify-subject-fully s2)))
1825    (t
1826     (equal s1
1827            (gnus-simplify-subject-fully s2)))))
1828
1829 (defun gnus-summary-bubble-group ()
1830   "Increase the score of the current group.
1831 This is a handy function to add to `gnus-summary-exit-hook' to
1832 increase the score of each group you read."
1833   (gnus-group-add-score gnus-newsgroup-name))
1834
1835 \f
1836 ;;;
1837 ;;; Gnus summary mode
1838 ;;;
1839
1840 (put 'gnus-summary-mode 'mode-class 'special)
1841
1842 (defvar gnus-article-commands-menu)
1843
1844 ;; Non-orthogonal keys
1845
1846 (gnus-define-keys gnus-summary-mode-map
1847   " " gnus-summary-next-page
1848   [?\S-\ ] gnus-summary-prev-page
1849   "\177" gnus-summary-prev-page
1850   [delete] gnus-summary-prev-page
1851   "\r" gnus-summary-scroll-up
1852   "\M-\r" gnus-summary-scroll-down
1853   "n" gnus-summary-next-unread-article
1854   "p" gnus-summary-prev-unread-article
1855   "N" gnus-summary-next-article
1856   "P" gnus-summary-prev-article
1857   "\M-\C-n" gnus-summary-next-same-subject
1858   "\M-\C-p" gnus-summary-prev-same-subject
1859   "\M-n" gnus-summary-next-unread-subject
1860   "\M-p" gnus-summary-prev-unread-subject
1861   "." gnus-summary-first-unread-article
1862   "," gnus-summary-best-unread-article
1863   "\M-s" gnus-summary-search-article-forward
1864   "\M-r" gnus-summary-search-article-backward
1865   "\M-S" gnus-summary-repeat-search-article-forward
1866   "\M-R" gnus-summary-repeat-search-article-backward
1867   "<" gnus-summary-beginning-of-article
1868   ">" gnus-summary-end-of-article
1869   "j" gnus-summary-goto-article
1870   "^" gnus-summary-refer-parent-article
1871   "\M-^" gnus-summary-refer-article
1872   "u" gnus-summary-tick-article-forward
1873   "!" gnus-summary-tick-article-forward
1874   "U" gnus-summary-tick-article-backward
1875   "d" gnus-summary-mark-as-read-forward
1876   "D" gnus-summary-mark-as-read-backward
1877   "E" gnus-summary-mark-as-expirable
1878   "\M-u" gnus-summary-clear-mark-forward
1879   "\M-U" gnus-summary-clear-mark-backward
1880   "k" gnus-summary-kill-same-subject-and-select
1881   "\C-k" gnus-summary-kill-same-subject
1882   "\M-\C-k" gnus-summary-kill-thread
1883   "\M-\C-l" gnus-summary-lower-thread
1884   "e" gnus-summary-edit-article
1885   "#" gnus-summary-mark-as-processable
1886   "\M-#" gnus-summary-unmark-as-processable
1887   "\M-\C-t" gnus-summary-toggle-threads
1888   "\M-\C-s" gnus-summary-show-thread
1889   "\M-\C-h" gnus-summary-hide-thread
1890   "\M-\C-f" gnus-summary-next-thread
1891   "\M-\C-b" gnus-summary-prev-thread
1892   [(meta down)] gnus-summary-next-thread
1893   [(meta up)] gnus-summary-prev-thread
1894   "\M-\C-u" gnus-summary-up-thread
1895   "\M-\C-d" gnus-summary-down-thread
1896   "&" gnus-summary-execute-command
1897   "c" gnus-summary-catchup-and-exit
1898   "\C-w" gnus-summary-mark-region-as-read
1899   "\C-t" gnus-summary-toggle-truncation
1900   "?" gnus-summary-mark-as-dormant
1901   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1902   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1903   "\C-c\C-s\C-m\C-n" gnus-summary-sort-by-most-recent-number
1904   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1905   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1906   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1907   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1908   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1909   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1910   "\C-c\C-s\C-m\C-d" gnus-summary-sort-by-most-recent-date
1911   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1912   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1913   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1914   "=" gnus-summary-expand-window
1915   "\C-x\C-s" gnus-summary-reselect-current-group
1916   "\M-g" gnus-summary-rescan-group
1917   "\C-c\C-r" gnus-summary-caesar-message
1918   "f" gnus-summary-followup
1919   "F" gnus-summary-followup-with-original
1920   "C" gnus-summary-cancel-article
1921   "r" gnus-summary-reply
1922   "R" gnus-summary-reply-with-original
1923   "\C-c\C-f" gnus-summary-mail-forward
1924   "o" gnus-summary-save-article
1925   "\C-o" gnus-summary-save-article-mail
1926   "|" gnus-summary-pipe-output
1927   "\M-k" gnus-summary-edit-local-kill
1928   "\M-K" gnus-summary-edit-global-kill
1929   ;; "V" gnus-version
1930   "\C-c\C-d" gnus-summary-describe-group
1931   "q" gnus-summary-exit
1932   "Q" gnus-summary-exit-no-update
1933   "\C-c\C-i" gnus-info-find-node
1934   gnus-mouse-2 gnus-mouse-pick-article
1935   [follow-link] mouse-face
1936   "m" gnus-summary-mail-other-window
1937   "a" gnus-summary-post-news
1938   "x" gnus-summary-limit-to-unread
1939   "s" gnus-summary-isearch-article
1940   "\t" gnus-summary-widget-forward
1941   [backtab] gnus-summary-widget-backward
1942   "t" gnus-summary-toggle-header
1943   "g" gnus-summary-show-article
1944   "l" gnus-summary-goto-last-article
1945   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1946   "\C-d" gnus-summary-enter-digest-group
1947   "\M-\C-d" gnus-summary-read-document
1948   "\M-\C-e" gnus-summary-edit-parameters
1949   "\M-\C-a" gnus-summary-customize-parameters
1950   "\C-c\C-b" gnus-bug
1951   "*" gnus-cache-enter-article
1952   "\M-*" gnus-cache-remove-article
1953   "\M-&" gnus-summary-universal-argument
1954   "\C-l" gnus-recenter
1955   "I" gnus-summary-increase-score
1956   "L" gnus-summary-lower-score
1957   "\M-i" gnus-symbolic-argument
1958   "h" gnus-summary-select-article-buffer
1959
1960   "b" gnus-article-view-part
1961   "\M-t" gnus-summary-toggle-display-buttonized
1962
1963   "V" gnus-summary-score-map
1964   "X" gnus-uu-extract-map
1965   "S" gnus-summary-send-map)
1966
1967 ;; Sort of orthogonal keymap
1968 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1969   "t" gnus-summary-tick-article-forward
1970   "!" gnus-summary-tick-article-forward
1971   "d" gnus-summary-mark-as-read-forward
1972   "r" gnus-summary-mark-as-read-forward
1973   "c" gnus-summary-clear-mark-forward
1974   " " gnus-summary-clear-mark-forward
1975   "e" gnus-summary-mark-as-expirable
1976   "x" gnus-summary-mark-as-expirable
1977   "?" gnus-summary-mark-as-dormant
1978   "b" gnus-summary-set-bookmark
1979   "B" gnus-summary-remove-bookmark
1980   "#" gnus-summary-mark-as-processable
1981   "\M-#" gnus-summary-unmark-as-processable
1982   "S" gnus-summary-limit-include-expunged
1983   "C" gnus-summary-catchup
1984   "H" gnus-summary-catchup-to-here
1985   "h" gnus-summary-catchup-from-here
1986   "\C-c" gnus-summary-catchup-all
1987   "k" gnus-summary-kill-same-subject-and-select
1988   "K" gnus-summary-kill-same-subject
1989   "P" gnus-uu-mark-map)
1990
1991 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1992   "c" gnus-summary-clear-above
1993   "u" gnus-summary-tick-above
1994   "m" gnus-summary-mark-above
1995   "k" gnus-summary-kill-below)
1996
1997 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1998   "/" gnus-summary-limit-to-subject
1999   "n" gnus-summary-limit-to-articles
2000   "b" gnus-summary-limit-to-bodies
2001   "h" gnus-summary-limit-to-headers
2002   "w" gnus-summary-pop-limit
2003   "s" gnus-summary-limit-to-subject
2004   "a" gnus-summary-limit-to-author
2005   "u" gnus-summary-limit-to-unread
2006   "m" gnus-summary-limit-to-marks
2007   "M" gnus-summary-limit-exclude-marks
2008   "v" gnus-summary-limit-to-score
2009   "*" gnus-summary-limit-include-cached
2010   "D" gnus-summary-limit-include-dormant
2011   "T" gnus-summary-limit-include-thread
2012   "d" gnus-summary-limit-exclude-dormant
2013   "t" gnus-summary-limit-to-age
2014   "." gnus-summary-limit-to-unseen
2015   "x" gnus-summary-limit-to-extra
2016   "p" gnus-summary-limit-to-display-predicate
2017   "E" gnus-summary-limit-include-expunged
2018   "c" gnus-summary-limit-exclude-childless-dormant
2019   "C" gnus-summary-limit-mark-excluded-as-read
2020   "o" gnus-summary-insert-old-articles
2021   "N" gnus-summary-insert-new-articles
2022   "S" gnus-summary-limit-to-singletons
2023   "r" gnus-summary-limit-to-replied
2024   "R" gnus-summary-limit-to-recipient
2025   "A" gnus-summary-limit-to-address)
2026
2027 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
2028   "n" gnus-summary-next-unread-article
2029   "p" gnus-summary-prev-unread-article
2030   "N" gnus-summary-next-article
2031   "P" gnus-summary-prev-article
2032   "\C-n" gnus-summary-next-same-subject
2033   "\C-p" gnus-summary-prev-same-subject
2034   "\M-n" gnus-summary-next-unread-subject
2035   "\M-p" gnus-summary-prev-unread-subject
2036   "f" gnus-summary-first-unread-article
2037   "b" gnus-summary-best-unread-article
2038   "j" gnus-summary-goto-article
2039   "g" gnus-summary-goto-subject
2040   "l" gnus-summary-goto-last-article
2041   "o" gnus-summary-pop-article)
2042
2043 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
2044   "k" gnus-summary-kill-thread
2045   "E" gnus-summary-expire-thread
2046   "l" gnus-summary-lower-thread
2047   "i" gnus-summary-raise-thread
2048   "T" gnus-summary-toggle-threads
2049   "t" gnus-summary-rethread-current
2050   "^" gnus-summary-reparent-thread
2051   "\M-^" gnus-summary-reparent-children
2052   "s" gnus-summary-show-thread
2053   "S" gnus-summary-show-all-threads
2054   "h" gnus-summary-hide-thread
2055   "H" gnus-summary-hide-all-threads
2056   "n" gnus-summary-next-thread
2057   "p" gnus-summary-prev-thread
2058   "u" gnus-summary-up-thread
2059   "o" gnus-summary-top-thread
2060   "d" gnus-summary-down-thread
2061   "#" gnus-uu-mark-thread
2062   "\M-#" gnus-uu-unmark-thread)
2063
2064 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
2065   "g" gnus-summary-prepare
2066   "c" gnus-summary-insert-cached-articles
2067   "d" gnus-summary-insert-dormant-articles
2068   "t" gnus-summary-insert-ticked-articles)
2069
2070 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
2071   "c" gnus-summary-catchup-and-exit
2072   "C" gnus-summary-catchup-all-and-exit
2073   "E" gnus-summary-exit-no-update
2074   "Q" gnus-summary-exit
2075   "Z" gnus-summary-exit
2076   "n" gnus-summary-catchup-and-goto-next-group
2077   "p" gnus-summary-catchup-and-goto-prev-group
2078   "R" gnus-summary-reselect-current-group
2079   "G" gnus-summary-rescan-group
2080   "N" gnus-summary-next-group
2081   "s" gnus-summary-save-newsrc
2082   "P" gnus-summary-prev-group)
2083
2084 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
2085   " " gnus-summary-next-page
2086   "n" gnus-summary-next-page
2087   [?\S-\ ] gnus-summary-prev-page
2088   "\177" gnus-summary-prev-page
2089   [delete] gnus-summary-prev-page
2090   "p" gnus-summary-prev-page
2091   "\r" gnus-summary-scroll-up
2092   "\M-\r" gnus-summary-scroll-down
2093   "<" gnus-summary-beginning-of-article
2094   ">" gnus-summary-end-of-article
2095   "b" gnus-summary-beginning-of-article
2096   "e" gnus-summary-end-of-article
2097   "^" gnus-summary-refer-parent-article
2098   "r" gnus-summary-refer-parent-article
2099   "C" gnus-summary-show-complete-article
2100   "D" gnus-summary-enter-digest-group
2101   "R" gnus-summary-refer-references
2102   "T" gnus-summary-refer-thread
2103   "W" gnus-warp-to-article
2104   "g" gnus-summary-show-article
2105   "s" gnus-summary-isearch-article
2106   "\t" gnus-summary-widget-forward
2107   [backtab] gnus-summary-widget-backward
2108   "P" gnus-summary-print-article
2109   "S" gnus-sticky-article
2110   "M" gnus-mailing-list-insinuate
2111   "t" gnus-article-babel)
2112
2113 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2114   "b" gnus-article-add-buttons
2115   "B" gnus-article-add-buttons-to-head
2116   "o" gnus-article-treat-overstrike
2117   "e" gnus-article-emphasize
2118   "w" gnus-article-fill-cited-article
2119   "Q" gnus-article-fill-long-lines
2120   "L" gnus-article-toggle-truncate-lines
2121   "C" gnus-article-capitalize-sentences
2122   "c" gnus-article-remove-cr
2123   "q" gnus-article-de-quoted-unreadable
2124   "6" gnus-article-de-base64-unreadable
2125   "Z" gnus-article-decode-HZ
2126   "A" gnus-article-treat-ansi-sequences
2127   "h" gnus-article-wash-html
2128   "u" gnus-article-unsplit-urls
2129   "s" gnus-summary-force-verify-and-decrypt
2130   "f" gnus-article-display-x-face
2131   "l" gnus-summary-stop-page-breaking
2132   "r" gnus-summary-caesar-message
2133   "m" gnus-summary-morse-message
2134   "t" gnus-summary-toggle-header
2135   "g" gnus-treat-smiley
2136   "v" gnus-summary-verbose-headers
2137   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2138   "p" gnus-article-verify-x-pgp-sig
2139   "d" gnus-article-treat-dumbquotes
2140   "U" gnus-article-treat-non-ascii
2141   "i" gnus-summary-idna-message)
2142
2143 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2144   ;; mnemonic: deuglif*Y*
2145   "u" gnus-article-outlook-unwrap-lines
2146   "a" gnus-article-outlook-repair-attribution
2147   "c" gnus-article-outlook-rearrange-citation
2148   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2149
2150 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2151   "a" gnus-article-hide
2152   "h" gnus-article-hide-headers
2153   "b" gnus-article-hide-boring-headers
2154   "s" gnus-article-hide-signature
2155   "c" gnus-article-hide-citation
2156   "C" gnus-article-hide-citation-in-followups
2157   "l" gnus-article-hide-list-identifiers
2158   "B" gnus-article-strip-banner
2159   "P" gnus-article-hide-pem
2160   "\C-c" gnus-article-hide-citation-maybe)
2161
2162 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2163   "a" gnus-article-highlight
2164   "h" gnus-article-highlight-headers
2165   "c" gnus-article-highlight-citation
2166   "s" gnus-article-highlight-signature)
2167
2168 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2169   "f" gnus-article-treat-fold-headers
2170   "u" gnus-article-treat-unfold-headers
2171   "n" gnus-article-treat-fold-newsgroups)
2172
2173 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2174   "x" gnus-article-display-x-face
2175   "d" gnus-article-display-face
2176   "s" gnus-treat-smiley
2177   "D" gnus-article-remove-images
2178   "W" gnus-article-show-images
2179   "f" gnus-treat-from-picon
2180   "m" gnus-treat-mail-picon
2181   "n" gnus-treat-newsgroups-picon
2182   "g" gnus-treat-from-gravatar
2183   "h" gnus-treat-mail-gravatar)
2184
2185 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2186   "w" gnus-article-decode-mime-words
2187   "c" gnus-article-decode-charset
2188   "v" gnus-mime-view-all-parts
2189   "b" gnus-article-view-part)
2190
2191 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2192   "z" gnus-article-date-ut
2193   "u" gnus-article-date-ut
2194   "l" gnus-article-date-local
2195   "p" gnus-article-date-english
2196   "e" gnus-article-date-lapsed
2197   "o" gnus-article-date-original
2198   "i" gnus-article-date-iso8601
2199   "s" gnus-article-date-user)
2200
2201 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2202   "t" gnus-article-remove-trailing-blank-lines
2203   "l" gnus-article-strip-leading-blank-lines
2204   "m" gnus-article-strip-multiple-blank-lines
2205   "a" gnus-article-strip-blank-lines
2206   "A" gnus-article-strip-all-blank-lines
2207   "s" gnus-article-strip-leading-space
2208   "e" gnus-article-strip-trailing-space
2209   "w" gnus-article-remove-leading-whitespace)
2210
2211 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2212   "v" gnus-version
2213   "d" gnus-summary-describe-group
2214   "h" gnus-summary-describe-briefly
2215   "i" gnus-info-find-node)
2216
2217 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2218   "e" gnus-summary-expire-articles
2219   "\M-\C-e" gnus-summary-expire-articles-now
2220   "\177" gnus-summary-delete-article
2221   [delete] gnus-summary-delete-article
2222   "m" gnus-summary-move-article
2223   "r" gnus-summary-respool-article
2224   "w" gnus-summary-edit-article
2225   "c" gnus-summary-copy-article
2226   "B" gnus-summary-crosspost-article
2227   "q" gnus-summary-respool-query
2228   "t" gnus-summary-respool-trace
2229   "i" gnus-summary-import-article
2230   "I" gnus-summary-create-article
2231   "p" gnus-summary-article-posted-p)
2232
2233 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2234   "o" gnus-summary-save-article
2235   "m" gnus-summary-save-article-mail
2236   "F" gnus-summary-write-article-file
2237   "r" gnus-summary-save-article-rmail
2238   "f" gnus-summary-save-article-file
2239   "b" gnus-summary-save-article-body-file
2240   "B" gnus-summary-write-article-body-file
2241   "h" gnus-summary-save-article-folder
2242   "v" gnus-summary-save-article-vm
2243   "p" gnus-summary-pipe-output
2244   "P" gnus-summary-muttprint)
2245
2246 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2247   "b" gnus-summary-display-buttonized
2248   "m" gnus-summary-repair-multipart
2249   "v" gnus-article-view-part
2250   "o" gnus-article-save-part
2251   "O" gnus-article-save-part-and-strip
2252   "r" gnus-article-replace-part
2253   "d" gnus-article-delete-part
2254   "t" gnus-article-view-part-as-type
2255   "j" gnus-article-jump-to-part
2256   "c" gnus-article-copy-part
2257   "C" gnus-article-view-part-as-charset
2258   "e" gnus-article-view-part-externally
2259   "H" gnus-article-browse-html-article
2260   "E" gnus-article-encrypt-body
2261   "i" gnus-article-inline-part
2262   "|" gnus-article-pipe-part)
2263
2264 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2265   "p" gnus-summary-mark-as-processable
2266   "u" gnus-summary-unmark-as-processable
2267   "U" gnus-summary-unmark-all-processable
2268   "v" gnus-uu-mark-over
2269   "s" gnus-uu-mark-series
2270   "r" gnus-uu-mark-region
2271   "g" gnus-uu-unmark-region
2272   "R" gnus-uu-mark-by-regexp
2273   "G" gnus-uu-unmark-by-regexp
2274   "t" gnus-uu-mark-thread
2275   "T" gnus-uu-unmark-thread
2276   "a" gnus-uu-mark-all
2277   "b" gnus-uu-mark-buffer
2278   "S" gnus-uu-mark-sparse
2279   "k" gnus-summary-kill-process-mark
2280   "y" gnus-summary-yank-process-mark
2281   "w" gnus-summary-save-process-mark
2282   "i" gnus-uu-invert-processable)
2283
2284 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2285   ;;"x" gnus-uu-extract-any
2286   "m" gnus-summary-save-parts
2287   "u" gnus-uu-decode-uu
2288   "U" gnus-uu-decode-uu-and-save
2289   "s" gnus-uu-decode-unshar
2290   "S" gnus-uu-decode-unshar-and-save
2291   "o" gnus-uu-decode-save
2292   "O" gnus-uu-decode-save
2293   "b" gnus-uu-decode-binhex
2294   "B" gnus-uu-decode-binhex
2295   "Y" gnus-uu-decode-yenc
2296   "p" gnus-uu-decode-postscript
2297   "P" gnus-uu-decode-postscript-and-save)
2298
2299 (gnus-define-keys
2300     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2301   "u" gnus-uu-decode-uu-view
2302   "U" gnus-uu-decode-uu-and-save-view
2303   "s" gnus-uu-decode-unshar-view
2304   "S" gnus-uu-decode-unshar-and-save-view
2305   "o" gnus-uu-decode-save-view
2306   "O" gnus-uu-decode-save-view
2307   "b" gnus-uu-decode-binhex-view
2308   "B" gnus-uu-decode-binhex-view
2309   "p" gnus-uu-decode-postscript-view
2310   "P" gnus-uu-decode-postscript-and-save-view)
2311
2312 (defvar gnus-article-post-menu nil)
2313
2314 (defconst gnus-summary-menu-maxlen 20)
2315
2316 (defun gnus-summary-menu-split (menu)
2317   ;; If we have lots of elements, divide them into groups of 20
2318   ;; and make a pane (or submenu) for each one.
2319   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2320       (let ((menu menu) sublists next
2321             (i 1))
2322         (while menu
2323           ;; Pull off the next gnus-summary-menu-maxlen elements
2324           ;; and make them the next element of sublist.
2325           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2326           (if next
2327               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2328                       nil))
2329           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2330                                              (aref (car (last menu)) 0)) menu)
2331                                sublists))
2332           (setq i (1+ i))
2333           (setq menu next))
2334         (nreverse sublists))
2335     ;; Few elements--put them all in one pane.
2336     menu))
2337
2338 (defun gnus-summary-make-menu-bar ()
2339   (gnus-turn-off-edit-menu 'summary)
2340
2341   (unless (boundp 'gnus-summary-misc-menu)
2342
2343     (easy-menu-define
2344       gnus-summary-kill-menu gnus-summary-mode-map ""
2345       (cons
2346        "Score"
2347        (nconc
2348         (list
2349          ["Customize" gnus-score-customize t])
2350         (gnus-make-score-map 'increase)
2351         (gnus-make-score-map 'lower)
2352         '(("Mark"
2353            ["Kill below" gnus-summary-kill-below t]
2354            ["Mark above" gnus-summary-mark-above t]
2355            ["Tick above" gnus-summary-tick-above t]
2356            ["Clear above" gnus-summary-clear-above t])
2357           ["Current article score" gnus-summary-current-score t]
2358           ["Current thread score" (gnus-summary-current-score 'total) t]
2359           ["Set score" gnus-summary-set-score t]
2360           ["Switch current score file..." gnus-score-change-score-file t]
2361           ["Set mark below..." gnus-score-set-mark-below t]
2362           ["Set expunge below..." gnus-score-set-expunge-below t]
2363           ["Edit current score file" gnus-score-edit-current-scores t]
2364           ["Edit score file..." gnus-score-edit-file t]
2365           ["Trace score" gnus-score-find-trace t]
2366           ["Find words" gnus-score-find-favourite-words t]
2367           ["Rescore buffer" gnus-summary-rescore t]
2368           ["Increase score..." gnus-summary-increase-score t]
2369           ["Lower score..." gnus-summary-lower-score t]))))
2370
2371     ;; Define both the Article menu in the summary buffer and the
2372     ;; equivalent Commands menu in the article buffer here for
2373     ;; consistency.
2374     (let ((innards
2375            `(("Hide"
2376               ["All" gnus-article-hide t]
2377               ["Headers" gnus-article-hide-headers t]
2378               ["Signature" gnus-article-hide-signature t]
2379               ["Citation" gnus-article-hide-citation t]
2380               ["List identifiers" gnus-article-hide-list-identifiers t]
2381               ["Banner" gnus-article-strip-banner t]
2382               ["Boring headers" gnus-article-hide-boring-headers t])
2383              ("Highlight"
2384               ["All" gnus-article-highlight t]
2385               ["Headers" gnus-article-highlight-headers t]
2386               ["Signature" gnus-article-highlight-signature t]
2387               ["Citation" gnus-article-highlight-citation t])
2388              ("MIME"
2389               ["Words" gnus-article-decode-mime-words t]
2390               ["Charset" gnus-article-decode-charset t]
2391               ["QP" gnus-article-de-quoted-unreadable t]
2392               ["Base64" gnus-article-de-base64-unreadable t]
2393               ["View MIME buttons" gnus-summary-display-buttonized t]
2394               ["View all" gnus-mime-view-all-parts t]
2395               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2396               ["Encrypt body" gnus-article-encrypt-body
2397                :active (not (gnus-group-read-only-p))
2398                ,@(if (featurep 'xemacs) nil
2399                    '(:help "Encrypt the message body on disk"))]
2400               ["Extract all parts..." gnus-summary-save-parts t]
2401               ("Multipart"
2402                ["Repair multipart" gnus-summary-repair-multipart t]
2403                ["Pipe part..." gnus-article-pipe-part t]
2404                ["Inline part" gnus-article-inline-part t]
2405                ["View part as type..." gnus-article-view-part-as-type t]
2406                ["Encrypt body" gnus-article-encrypt-body
2407                 :active (not (gnus-group-read-only-p))
2408                ,@(if (featurep 'xemacs) nil
2409                    '(:help "Encrypt the message body on disk"))]
2410                ["View part externally" gnus-article-view-part-externally t]
2411                ["View HTML parts in browser" gnus-article-browse-html-article t]
2412                ["View part with charset..." gnus-article-view-part-as-charset t]
2413                ["Copy part" gnus-article-copy-part t]
2414                ["Save part..." gnus-article-save-part t]
2415                ["View part" gnus-article-view-part t]))
2416              ("Date"
2417               ["Local" gnus-article-date-local t]
2418               ["ISO8601" gnus-article-date-iso8601 t]
2419               ["UT" gnus-article-date-ut t]
2420               ["Original" gnus-article-date-original t]
2421               ["Lapsed" gnus-article-date-lapsed t]
2422               ["User-defined" gnus-article-date-user t])
2423              ("Display"
2424               ["Remove images" gnus-article-remove-images t]
2425               ["Toggle smiley" gnus-treat-smiley t]
2426               ["Show X-Face" gnus-article-display-x-face t]
2427               ["Show picons in From" gnus-treat-from-picon t]
2428               ["Show picons in mail headers" gnus-treat-mail-picon t]
2429               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2430               ["Show Gravatars in From" gnus-treat-from-gravatar t]
2431               ["Show Gravatars in mail headers" gnus-treat-mail-gravatar t]
2432               ("View as different encoding"
2433                ,@(gnus-summary-menu-split
2434                   (mapcar
2435                    (lambda (cs)
2436                      ;; Since easymenu under Emacs doesn't allow
2437                      ;; lambda forms for menu commands, we should
2438                      ;; provide intern'ed function symbols.
2439                      (let ((command (intern (format "\
2440 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2441                        (fset command
2442                              `(lambda ()
2443                                 (interactive)
2444                                 (let ((gnus-summary-show-article-charset-alist
2445                                        '((1 . ,cs))))
2446                                   (gnus-summary-show-article 1))))
2447                        `[,(symbol-name cs) ,command t]))
2448                    (sort (if (fboundp 'coding-system-list)
2449                              (coding-system-list)
2450                            (mapcar 'car mm-mime-mule-charset-alist))
2451                          'string<)))))
2452              ("Washing"
2453               ("Remove Blanks"
2454                ["Leading" gnus-article-strip-leading-blank-lines t]
2455                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2456                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2457                ["All of the above" gnus-article-strip-blank-lines t]
2458                ["All" gnus-article-strip-all-blank-lines t]
2459                ["Leading space" gnus-article-strip-leading-space t]
2460                ["Trailing space" gnus-article-strip-trailing-space t]
2461                ["Leading space in headers"
2462                 gnus-article-remove-leading-whitespace t])
2463               ["Overstrike" gnus-article-treat-overstrike t]
2464               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2465               ["Non-ASCII" gnus-article-treat-non-ascii t]
2466               ["Emphasis" gnus-article-emphasize t]
2467               ["Word wrap" gnus-article-fill-cited-article t]
2468               ["Fill long lines" gnus-article-fill-long-lines t]
2469               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2470               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2471               ["Remove CR" gnus-article-remove-cr t]
2472               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2473               ["Base64" gnus-article-de-base64-unreadable t]
2474               ["Rot 13" gnus-summary-caesar-message
2475                ,@(if (featurep 'xemacs) '(t)
2476                    '(:help "\"Caesar rotate\" article by 13"))]
2477               ["De-IDNA" gnus-summary-idna-message t]
2478               ["Morse decode" gnus-summary-morse-message t]
2479               ["Unix pipe..." gnus-summary-pipe-message t]
2480               ["Add buttons" gnus-article-add-buttons t]
2481               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2482               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2483               ["Verbose header" gnus-summary-verbose-headers t]
2484               ["Toggle header" gnus-summary-toggle-header t]
2485               ["Unfold headers" gnus-article-treat-unfold-headers t]
2486               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2487               ["Html" gnus-article-wash-html t]
2488               ["Unsplit URLs" gnus-article-unsplit-urls t]
2489               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2490               ["Decode HZ" gnus-article-decode-HZ t]
2491               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2492               ("(Outlook) Deuglify"
2493                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2494                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2495                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2496                ["Full (Outlook) deuglify"
2497                 gnus-article-outlook-deuglify-article t])
2498               )
2499              ("Output"
2500               ["Save in default format..." gnus-summary-save-article
2501                ,@(if (featurep 'xemacs) '(t)
2502                    '(:help "Save article using default method"))]
2503               ["Save in file..." gnus-summary-save-article-file
2504                ,@(if (featurep 'xemacs) '(t)
2505                    '(:help "Save article in file"))]
2506               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2507               ["Save in MH folder..." gnus-summary-save-article-folder t]
2508               ["Save in VM folder..." gnus-summary-save-article-vm t]
2509               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2510               ["Save body in file..." gnus-summary-save-article-body-file t]
2511               ["Pipe through a filter..." gnus-summary-pipe-output t]
2512               ["Print with Muttprint..." gnus-summary-muttprint t]
2513               ["Print" gnus-summary-print-article
2514                ,@(if (featurep 'xemacs) '(t)
2515                    '(:help "Generate and print a PostScript image"))])
2516              ("Copy, move,... (Backend)"
2517               ,@(if (featurep 'xemacs) nil
2518                   '(:help "Copying, moving, expiring articles..."))
2519               ["Respool article..." gnus-summary-respool-article t]
2520               ["Move article..." gnus-summary-move-article
2521                (gnus-check-backend-function
2522                 'request-move-article gnus-newsgroup-name)]
2523               ["Copy article..." gnus-summary-copy-article t]
2524               ["Crosspost article..." gnus-summary-crosspost-article
2525                (gnus-check-backend-function
2526                 'request-replace-article gnus-newsgroup-name)]
2527               ["Import file..." gnus-summary-import-article
2528                (gnus-check-backend-function
2529                 'request-accept-article gnus-newsgroup-name)]
2530               ["Create article..." gnus-summary-create-article
2531                (gnus-check-backend-function
2532                 'request-accept-article gnus-newsgroup-name)]
2533               ["Check if posted" gnus-summary-article-posted-p t]
2534               ["Edit article" gnus-summary-edit-article
2535                (not (gnus-group-read-only-p))]
2536               ["Delete article" gnus-summary-delete-article
2537                (gnus-check-backend-function
2538                 'request-expire-articles gnus-newsgroup-name)]
2539               ["Query respool" gnus-summary-respool-query t]
2540               ["Trace respool" gnus-summary-respool-trace t]
2541               ["Delete expirable articles" gnus-summary-expire-articles-now
2542                (gnus-check-backend-function
2543                 'request-expire-articles gnus-newsgroup-name)])
2544              ("Extract"
2545               ["Uudecode" gnus-uu-decode-uu
2546                ,@(if (featurep 'xemacs) '(t)
2547                    '(:help "Decode uuencoded article(s)"))]
2548               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2549               ["Unshar" gnus-uu-decode-unshar t]
2550               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2551               ["Save" gnus-uu-decode-save t]
2552               ["Binhex" gnus-uu-decode-binhex t]
2553               ["PostScript" gnus-uu-decode-postscript t]
2554               ["All MIME parts" gnus-summary-save-parts t])
2555              ("Cache"
2556               ["Enter article" gnus-cache-enter-article t]
2557               ["Remove article" gnus-cache-remove-article t])
2558              ["Translate" gnus-article-babel t]
2559              ["Select article buffer" gnus-summary-select-article-buffer t]
2560              ["Make article buffer sticky" gnus-sticky-article t]
2561              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2562              ["Isearch article..." gnus-summary-isearch-article t]
2563              ["Beginning of the article" gnus-summary-beginning-of-article t]
2564              ["End of the article" gnus-summary-end-of-article t]
2565              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2566              ["Fetch referenced articles" gnus-summary-refer-references t]
2567              ["Fetch current thread" gnus-summary-refer-thread t]
2568              ["Fetch article with id..." gnus-summary-refer-article t]
2569              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2570              ["Redisplay" gnus-summary-show-article t]
2571              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2572       (easy-menu-define
2573         gnus-summary-article-menu gnus-summary-mode-map ""
2574         (cons "Article" innards))
2575
2576       (if (not (keymapp gnus-summary-article-menu))
2577           (easy-menu-define
2578             gnus-article-commands-menu gnus-article-mode-map ""
2579             (cons "Commands" innards))
2580         ;; in Emacs, don't share menu.
2581         (setq gnus-article-commands-menu
2582               (copy-keymap gnus-summary-article-menu))
2583         (define-key gnus-article-mode-map [menu-bar commands]
2584           (cons "Commands" gnus-article-commands-menu))))
2585
2586     (easy-menu-define
2587       gnus-summary-thread-menu gnus-summary-mode-map ""
2588       '("Threads"
2589         ["Find all messages in thread" gnus-summary-refer-thread t]
2590         ["Toggle threading" gnus-summary-toggle-threads t]
2591         ["Hide threads" gnus-summary-hide-all-threads t]
2592         ["Show threads" gnus-summary-show-all-threads t]
2593         ["Hide thread" gnus-summary-hide-thread t]
2594         ["Show thread" gnus-summary-show-thread t]
2595         ["Go to next thread" gnus-summary-next-thread t]
2596         ["Go to previous thread" gnus-summary-prev-thread t]
2597         ["Go down thread" gnus-summary-down-thread t]
2598         ["Go up thread" gnus-summary-up-thread t]
2599         ["Top of thread" gnus-summary-top-thread t]
2600         ["Mark thread as read" gnus-summary-kill-thread t]
2601         ["Mark thread as expired" gnus-summary-expire-thread t]
2602         ["Lower thread score" gnus-summary-lower-thread t]
2603         ["Raise thread score" gnus-summary-raise-thread t]
2604         ["Rethread current" gnus-summary-rethread-current t]))
2605
2606     (easy-menu-define
2607       gnus-summary-post-menu gnus-summary-mode-map ""
2608       `("Post"
2609         ["Send a message (mail or news)" gnus-summary-post-news
2610          ,@(if (featurep 'xemacs) '(t)
2611              '(:help "Compose a new message (mail or news)"))]
2612         ["Followup" gnus-summary-followup
2613          ,@(if (featurep 'xemacs) '(t)
2614              '(:help "Post followup to this article"))]
2615         ["Followup and yank" gnus-summary-followup-with-original
2616          ,@(if (featurep 'xemacs) '(t)
2617              '(:help "Post followup to this article, quoting its contents"))]
2618         ["Supersede article" gnus-summary-supersede-article t]
2619         ["Cancel article" gnus-summary-cancel-article
2620          ,@(if (featurep 'xemacs) '(t)
2621              '(:help "Cancel an article you posted"))]
2622         ["Reply" gnus-summary-reply t]
2623         ["Reply and yank" gnus-summary-reply-with-original t]
2624         ["Wide reply" gnus-summary-wide-reply t]
2625         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2626          ,@(if (featurep 'xemacs) '(t)
2627              '(:help "Mail a reply, quoting this article"))]
2628         ["Very wide reply" gnus-summary-very-wide-reply t]
2629         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2630          ,@(if (featurep 'xemacs) '(t)
2631              '(:help "Mail a very wide reply, quoting this article"))]
2632         ["Mail forward" gnus-summary-mail-forward t]
2633         ["Post forward" gnus-summary-post-forward t]
2634         ["Digest and mail" gnus-uu-digest-mail-forward t]
2635         ["Digest and post" gnus-uu-digest-post-forward t]
2636         ["Resend message" gnus-summary-resend-message t]
2637         ["Resend message edit" gnus-summary-resend-message-edit t]
2638         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2639         ["Send a mail" gnus-summary-mail-other-window t]
2640         ["Create a local message" gnus-summary-news-other-window t]
2641         ["Uuencode and post" gnus-uu-post-news
2642          ,@(if (featurep 'xemacs) '(t)
2643              '(:help "Post a uuencoded article"))]
2644         ["Followup via news" gnus-summary-followup-to-mail t]
2645         ["Followup via news and yank"
2646          gnus-summary-followup-to-mail-with-original t]
2647         ["Strip signature on reply"
2648          (lambda ()
2649            (interactive)
2650            (if (not (memq message-cite-function
2651                           '(message-cite-original-without-signature
2652                             message-cite-original)))
2653                ;; Stupid workaround for XEmacs not honoring :visible.
2654                (message "Can't toggle this value of `message-cite-function'")
2655              (setq message-cite-function
2656                    (if (eq message-cite-function
2657                            'message-cite-original-without-signature)
2658                        'message-cite-original
2659                      'message-cite-original-without-signature))))
2660          ;; XEmacs barfs on :visible.
2661          ,@(if (featurep 'xemacs) nil
2662              '(:visible (memq message-cite-function
2663                               '(message-cite-original-without-signature
2664                                 message-cite-original))))
2665          :style toggle
2666          :selected (eq message-cite-function
2667                        'message-cite-original-without-signature)
2668          ,@(if (featurep 'xemacs) nil
2669              '(:help "Strip signature from cited article when replying."))]
2670         ;;("Draft"
2671         ;;["Send" gnus-summary-send-draft t]
2672         ;;["Send bounced" gnus-resend-bounced-mail t])
2673         ))
2674
2675     (cond
2676      ((not (keymapp gnus-summary-post-menu))
2677       (setq gnus-article-post-menu gnus-summary-post-menu))
2678      ((not gnus-article-post-menu)
2679       ;; Don't share post menu.
2680       (setq gnus-article-post-menu
2681             (copy-keymap gnus-summary-post-menu))))
2682     (define-key gnus-article-mode-map [menu-bar post]
2683       (cons "Post" gnus-article-post-menu))
2684
2685     (easy-menu-define
2686       gnus-summary-misc-menu gnus-summary-mode-map ""
2687       `("Gnus"
2688         ("Mark Read"
2689          ["Mark as read" gnus-summary-mark-as-read-forward t]
2690          ["Mark same subject and select"
2691           gnus-summary-kill-same-subject-and-select t]
2692          ["Mark same subject" gnus-summary-kill-same-subject t]
2693          ["Catchup" gnus-summary-catchup
2694           ,@(if (featurep 'xemacs) '(t)
2695               '(:help "Mark unread articles in this group as read"))]
2696          ["Catchup all" gnus-summary-catchup-all t]
2697          ["Catchup to here" gnus-summary-catchup-to-here t]
2698          ["Catchup from here" gnus-summary-catchup-from-here t]
2699          ["Catchup region" gnus-summary-mark-region-as-read
2700           (gnus-mark-active-p)]
2701          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2702         ("Mark Various"
2703          ["Tick" gnus-summary-tick-article-forward t]
2704          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2705          ["Remove marks" gnus-summary-clear-mark-forward t]
2706          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2707          ["Set bookmark" gnus-summary-set-bookmark t]
2708          ["Remove bookmark" gnus-summary-remove-bookmark t])
2709         ("Limit to"
2710          ["Marks..." gnus-summary-limit-to-marks t]
2711          ["Subject..." gnus-summary-limit-to-subject t]
2712          ["Author..." gnus-summary-limit-to-author t]
2713          ["Recipient..." gnus-summary-limit-to-recipient t]
2714          ["Address..." gnus-summary-limit-to-address t]
2715          ["Age..." gnus-summary-limit-to-age t]
2716          ["Extra..." gnus-summary-limit-to-extra t]
2717          ["Score..." gnus-summary-limit-to-score t]
2718          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2719          ["Unread" gnus-summary-limit-to-unread t]
2720          ["Unseen" gnus-summary-limit-to-unseen t]
2721          ["Singletons" gnus-summary-limit-to-singletons t]
2722          ["Replied" gnus-summary-limit-to-replied t]
2723          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2724          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2725          ["Pop limit" gnus-summary-pop-limit t]
2726          ["Show dormant" gnus-summary-limit-include-dormant t]
2727          ["Hide childless dormant"
2728           gnus-summary-limit-exclude-childless-dormant t]
2729          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2730          ["Hide marked" gnus-summary-limit-exclude-marks t]
2731          ["Show expunged" gnus-summary-limit-include-expunged t])
2732         ("Process Mark"
2733          ["Set mark" gnus-summary-mark-as-processable t]
2734          ["Remove mark" gnus-summary-unmark-as-processable t]
2735          ["Remove all marks" gnus-summary-unmark-all-processable t]
2736          ["Invert marks" gnus-uu-invert-processable t]
2737          ["Mark above" gnus-uu-mark-over t]
2738          ["Mark series" gnus-uu-mark-series t]
2739          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2740          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2741          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2742          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2743          ["Mark all" gnus-uu-mark-all t]
2744          ["Mark buffer" gnus-uu-mark-buffer t]
2745          ["Mark sparse" gnus-uu-mark-sparse t]
2746          ["Mark thread" gnus-uu-mark-thread t]
2747          ["Unmark thread" gnus-uu-unmark-thread t]
2748          ("Process Mark Sets"
2749           ["Kill" gnus-summary-kill-process-mark t]
2750           ["Yank" gnus-summary-yank-process-mark
2751            gnus-newsgroup-process-stack]
2752           ["Save" gnus-summary-save-process-mark t]
2753           ["Run command on marked..." gnus-summary-universal-argument t]))
2754         ("Registry Marks")
2755         ("Scroll article"
2756          ["Page forward" gnus-summary-next-page
2757           ,@(if (featurep 'xemacs) '(t)
2758               '(:help "Show next page of article"))]
2759          ["Page backward" gnus-summary-prev-page
2760           ,@(if (featurep 'xemacs) '(t)
2761               '(:help "Show previous page of article"))]
2762          ["Line forward" gnus-summary-scroll-up t])
2763         ("Move"
2764          ["Next unread article" gnus-summary-next-unread-article t]
2765          ["Previous unread article" gnus-summary-prev-unread-article t]
2766          ["Next article" gnus-summary-next-article t]
2767          ["Previous article" gnus-summary-prev-article t]
2768          ["Next unread subject" gnus-summary-next-unread-subject t]
2769          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2770          ["Next article same subject" gnus-summary-next-same-subject t]
2771          ["Previous article same subject" gnus-summary-prev-same-subject t]
2772          ["First unread article" gnus-summary-first-unread-article t]
2773          ["Best unread article" gnus-summary-best-unread-article t]
2774          ["Go to subject number..." gnus-summary-goto-subject t]
2775          ["Go to article number..." gnus-summary-goto-article t]
2776          ["Go to the last article" gnus-summary-goto-last-article t]
2777          ["Pop article off history" gnus-summary-pop-article t])
2778         ("Sort"
2779          ["Sort by number" gnus-summary-sort-by-number t]
2780          ["Sort by most recent number" gnus-summary-sort-by-most-recent-number t]
2781          ["Sort by author" gnus-summary-sort-by-author t]
2782          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2783          ["Sort by subject" gnus-summary-sort-by-subject t]
2784          ["Sort by date" gnus-summary-sort-by-date t]
2785          ["Sort by most recent date" gnus-summary-sort-by-most-recent-date t]
2786          ["Sort by score" gnus-summary-sort-by-score t]
2787          ["Sort by lines" gnus-summary-sort-by-lines t]
2788          ["Sort by characters" gnus-summary-sort-by-chars t]
2789          ["Randomize" gnus-summary-sort-by-random t]
2790          ["Original sort" gnus-summary-sort-by-original t])
2791         ("Help"
2792          ["Describe group" gnus-summary-describe-group t]
2793          ["Read manual" gnus-info-find-node t])
2794         ("Modes"
2795          ["Pick and read" gnus-pick-mode t]
2796          ["Binary" gnus-binary-mode t])
2797         ("Regeneration"
2798          ["Regenerate" gnus-summary-prepare t]
2799          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2800          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2801          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2802          ["Toggle threading" gnus-summary-toggle-threads t])
2803         ["See old articles" gnus-summary-insert-old-articles t]
2804         ["See new articles" gnus-summary-insert-new-articles t]
2805         ["Filter articles..." gnus-summary-execute-command t]
2806         ["Run command on articles..." gnus-summary-universal-argument t]
2807         ["Search articles forward..." gnus-summary-search-article-forward t]
2808         ["Search articles backward..." gnus-summary-search-article-backward t]
2809         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2810         ["Expand window" gnus-summary-expand-window t]
2811         ["Expire expirable articles" gnus-summary-expire-articles
2812          (gnus-check-backend-function
2813           'request-expire-articles gnus-newsgroup-name)]
2814         ["Edit local kill file" gnus-summary-edit-local-kill t]
2815         ["Edit main kill file" gnus-summary-edit-global-kill t]
2816         ["Edit group parameters" gnus-summary-edit-parameters t]
2817         ["Customize group parameters" gnus-summary-customize-parameters t]
2818         ["Send a bug report" gnus-bug t]
2819         ("Exit"
2820          ["Catchup and exit" gnus-summary-catchup-and-exit
2821           ,@(if (featurep 'xemacs) '(t)
2822               '(:help "Mark unread articles in this group as read, then exit"))]
2823          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2824          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2825          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2826          ["Exit group" gnus-summary-exit
2827           ,@(if (featurep 'xemacs) '(t)
2828               '(:help "Exit current group, return to group selection mode"))]
2829          ["Exit group without updating" gnus-summary-exit-no-update t]
2830          ["Exit and goto next group" gnus-summary-next-group t]
2831          ["Exit and goto prev group" gnus-summary-prev-group t]
2832          ["Reselect group" gnus-summary-reselect-current-group t]
2833          ["Rescan group" gnus-summary-rescan-group t]
2834          ["Update dribble" gnus-summary-save-newsrc t])))
2835
2836     (gnus-run-hooks 'gnus-summary-menu-hook)))
2837
2838 (defvar gnus-summary-tool-bar-map nil)
2839
2840 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2841 ;; affect _new_ message buffers.  We might add a function that walks thru all
2842 ;; summary-mode buffers and force the update.
2843 (defun gnus-summary-tool-bar-update (&optional symbol value)
2844   "Update summary mode toolbar.
2845 Setter function for custom variables."
2846   (setq-default gnus-summary-tool-bar-map nil)
2847   (when symbol
2848     ;; When used as ":set" function:
2849     (set-default symbol value))
2850   (when (gnus-buffer-live-p gnus-summary-buffer)
2851     (with-current-buffer gnus-summary-buffer
2852       (gnus-summary-make-tool-bar))))
2853
2854 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2855                                      'gnus-summary-tool-bar-gnome
2856                                    'gnus-summary-tool-bar-retro)
2857   "Specifies the Gnus summary tool bar.
2858
2859 It can be either a list or a symbol referring to a list.  See
2860 `gmm-tool-bar-from-list' for the format of the list.  The
2861 default key map is `gnus-summary-mode-map'.
2862
2863 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2864 `gnus-summary-tool-bar-retro'."
2865   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2866                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2867                  (repeat :tag "User defined list" gmm-tool-bar-item)
2868                  (symbol))
2869   :version "23.1" ;; No Gnus
2870   :initialize 'custom-initialize-default
2871   :set 'gnus-summary-tool-bar-update
2872   :group 'gnus-summary)
2873
2874 (defcustom gnus-summary-tool-bar-gnome
2875   '((gnus-summary-post-news "mail/compose" nil)
2876     (gnus-summary-insert-new-articles "mail/inbox" nil
2877                                       :visible (or (not gnus-agent)
2878                                                    gnus-plugged))
2879     (gnus-summary-reply-with-original "mail/reply")
2880     (gnus-summary-reply "mail/reply" nil :visible nil)
2881     (gnus-summary-followup-with-original "mail/reply-all")
2882     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2883     (gnus-summary-mail-forward "mail/forward")
2884     (gnus-summary-save-article "mail/save")
2885     (gnus-summary-search-article-forward "search" nil :visible nil)
2886     (gnus-summary-print-article "print")
2887     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2888     ;; Some new commands that may need more suitable icons:
2889     (gnus-summary-save-newsrc "save" nil :visible nil)
2890     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2891     (gnus-summary-prev-article "left-arrow")
2892     (gnus-summary-next-article "right-arrow")
2893     (gnus-summary-next-page "next-page")
2894     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2895     ;;
2896     ;; Maybe some sort-by-... could be added:
2897     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2898     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2899     (gnus-summary-mark-as-expirable
2900      "delete" nil
2901      :visible (gnus-check-backend-function 'request-expire-articles
2902                                            gnus-newsgroup-name))
2903     (gnus-summary-mark-as-spam
2904      "mail/spam" t
2905      :visible (and (fboundp 'spam-group-ham-contents-p)
2906                    (spam-group-ham-contents-p gnus-newsgroup-name))
2907      :help "Mark as spam")
2908     (gnus-summary-mark-as-read-forward
2909      "mail/not-spam" nil
2910      :visible (and (fboundp 'spam-group-spam-contents-p)
2911                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2912     ;;
2913     (gnus-summary-exit "exit")
2914     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2915     (gnus-info-find-node "help"))
2916   "List of functions for the summary tool bar (GNOME style).
2917
2918 See `gmm-tool-bar-from-list' for the format of the list."
2919   :type '(repeat gmm-tool-bar-item)
2920   :version "23.1" ;; No Gnus
2921   :initialize 'custom-initialize-default
2922   :set 'gnus-summary-tool-bar-update
2923   :group 'gnus-summary)
2924
2925 (defcustom gnus-summary-tool-bar-retro
2926   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2927     (gnus-summary-next-unread-article "gnus/next-ur")
2928     (gnus-summary-post-news "gnus/post")
2929     (gnus-summary-followup-with-original "gnus/fuwo")
2930     (gnus-summary-followup "gnus/followup")
2931     (gnus-summary-reply-with-original "gnus/reply-wo")
2932     (gnus-summary-reply "gnus/reply")
2933     (gnus-summary-caesar-message "gnus/rot13")
2934     (gnus-uu-decode-uu "gnus/uu-decode")
2935     (gnus-summary-save-article-file "gnus/save-aif")
2936     (gnus-summary-save-article "gnus/save-art")
2937     (gnus-uu-post-news "gnus/uu-post")
2938     (gnus-summary-catchup "gnus/catchup")
2939     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2940     (gnus-summary-exit "gnus/exit-summ")
2941     ;; Some new command that may need more suitable icons:
2942     (gnus-summary-print-article "gnus/print" nil :visible nil)
2943     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2944     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2945     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2946     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2947     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2948     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2949     ;;
2950     (gnus-info-find-node "gnus/help" nil :visible nil))
2951   "List of functions for the summary tool bar (retro look).
2952
2953 See `gmm-tool-bar-from-list' for the format of the list."
2954   :type '(repeat gmm-tool-bar-item)
2955   :version "23.1" ;; No Gnus
2956   :initialize 'custom-initialize-default
2957   :set 'gnus-summary-tool-bar-update
2958   :group 'gnus-summary)
2959
2960 (defcustom gnus-summary-tool-bar-zap-list t
2961   "List of icon items from the global tool bar.
2962 These items are not displayed in the Gnus summary mode tool bar.
2963
2964 See `gmm-tool-bar-from-list' for the format of the list."
2965   :type 'gmm-tool-bar-zap-list
2966   :version "23.1" ;; No Gnus
2967   :initialize 'custom-initialize-default
2968   :set 'gnus-summary-tool-bar-update
2969   :group 'gnus-summary)
2970
2971 (defvar image-load-path)
2972 (defvar tool-bar-map)
2973
2974 (defun gnus-summary-make-tool-bar (&optional force)
2975   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2976 When FORCE, rebuild the tool bar."
2977   (when (and (not (featurep 'xemacs))
2978              (boundp 'tool-bar-mode)
2979              tool-bar-mode
2980              (or (not gnus-summary-tool-bar-map) force))
2981     (let* ((load-path
2982             (gmm-image-load-path-for-library "gnus"
2983                                              "mail/save.xpm"
2984                                              nil t))
2985            (image-load-path (cons (car load-path)
2986                                   (when (boundp 'image-load-path)
2987                                     image-load-path)))
2988            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2989                                         gnus-summary-tool-bar-zap-list
2990                                         'gnus-summary-mode-map)))
2991       (when map
2992         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2993         ;; uses its value.
2994         (setq gnus-summary-tool-bar-map map))))
2995   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2996
2997 (defun gnus-make-score-map (type)
2998   "Make a summary score map of type TYPE."
2999   (if t
3000       nil
3001     (let ((headers '(("author" "from" string)
3002                      ("subject" "subject" string)
3003                      ("article body" "body" string)
3004                      ("article head" "head" string)
3005                      ("xref" "xref" string)
3006                      ("extra header" "extra" string)
3007                      ("lines" "lines" number)
3008                      ("followups to author" "followup" string)))
3009           (types '((number ("less than" <)
3010                            ("greater than" >)
3011                            ("equal" =))
3012                    (string ("substring" s)
3013                            ("exact string" e)
3014                            ("fuzzy string" f)
3015                            ("regexp" r))))
3016           (perms '(("temporary" (current-time-string))
3017                    ("permanent" nil)
3018                    ("immediate" now)))
3019           header)
3020       (list
3021        (apply
3022         'nconc
3023         (list
3024          (if (eq type 'lower)
3025              "Lower score"
3026            "Increase score"))
3027         (let (outh)
3028           (while headers
3029             (setq header (car headers))
3030             (setq outh
3031                   (cons
3032                    (apply
3033                     'nconc
3034                     (list (car header))
3035                     (let ((ts (cdr (assoc (nth 2 header) types)))
3036                           outt)
3037                       (while ts
3038                         (setq outt
3039                               (cons
3040                                (apply
3041                                 'nconc
3042                                 (list (caar ts))
3043                                 (let ((ps perms)
3044                                       outp)
3045                                   (while ps
3046                                     (setq outp
3047                                           (cons
3048                                            (vector
3049                                             (caar ps)
3050                                             (list
3051                                              'gnus-summary-score-entry
3052                                              (nth 1 header)
3053                                              (if (or (string= (nth 1 header)
3054                                                               "head")
3055                                                      (string= (nth 1 header)
3056                                                               "body"))
3057                                                  ""
3058                                                (list 'gnus-summary-header
3059                                                      (nth 1 header)))
3060                                              (list 'quote (nth 1 (car ts)))
3061                                              (list 'gnus-score-delta-default
3062                                                    nil)
3063                                              (nth 1 (car ps))
3064                                              t)
3065                                             t)
3066                                            outp))
3067                                     (setq ps (cdr ps)))
3068                                   (list (nreverse outp))))
3069                                outt))
3070                         (setq ts (cdr ts)))
3071                       (list (nreverse outt))))
3072                    outh))
3073             (setq headers (cdr headers)))
3074           (list (nreverse outh))))))))
3075
3076
3077 (declare-function turn-on-gnus-mailing-list-mode "gnus-ml" ())
3078 (defvar bookmark-make-record-function)
3079 \f
3080 (defvar bidi-paragraph-direction)
3081
3082 (defun gnus-summary-mode (&optional group)
3083   "Major mode for reading articles.
3084
3085 All normal editing commands are switched off.
3086 \\<gnus-summary-mode-map>
3087 Each line in this buffer represents one article.  To read an
3088 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
3089 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
3090 respectively.
3091
3092 You can also post articles and send mail from this buffer.  To
3093 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
3094 of an article, type `\\[gnus-summary-reply]'.
3095
3096 There are approx. one gazillion commands you can execute in this
3097 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
3098
3099 The following commands are available:
3100
3101 \\{gnus-summary-mode-map}"
3102   ;; FIXME: Use define-derived-mode.
3103   (interactive)
3104   (kill-all-local-variables)
3105   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
3106     (gnus-summary-make-local-variables))
3107   (gnus-summary-make-local-variables)
3108   (setq gnus-newsgroup-name group)
3109   (when (gnus-visual-p 'summary-menu 'menu)
3110     (gnus-summary-make-menu-bar)
3111     (gnus-summary-make-tool-bar))
3112   (gnus-make-thread-indent-array)
3113   (gnus-simplify-mode-line)
3114   (setq major-mode 'gnus-summary-mode)
3115   (setq mode-name "Summary")
3116   (use-local-map gnus-summary-mode-map)
3117   (buffer-disable-undo)
3118   (setq buffer-read-only t              ;Disable modification
3119         show-trailing-whitespace nil)
3120   (setq truncate-lines t)
3121   ;; Force paragraph direction to be left-to-right.  Don't make it
3122   ;; bound globally in old Emacsen and XEmacsen.
3123   (set (make-local-variable 'bidi-paragraph-direction) 'left-to-right)
3124   (add-to-invisibility-spec '(gnus-sum . t))
3125   (gnus-summary-set-display-table)
3126   (gnus-set-default-directory)
3127   (make-local-variable 'gnus-summary-line-format)
3128   (make-local-variable 'gnus-summary-line-format-spec)
3129   (make-local-variable 'gnus-summary-dummy-line-format)
3130   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3131   (make-local-variable 'gnus-summary-mark-positions)
3132   (gnus-make-local-hook 'pre-command-hook)
3133   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3134   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3135   (turn-on-gnus-mailing-list-mode)
3136   (mm-enable-multibyte)
3137   (set (make-local-variable 'bookmark-make-record-function)
3138        'gnus-summary-bookmark-make-record)
3139   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3140   (gnus-update-summary-mark-positions))
3141
3142 (defun gnus-summary-make-local-variables ()
3143   "Make all the local summary buffer variables."
3144   (let (global)
3145     (dolist (local gnus-summary-local-variables)
3146       (if (consp local)
3147           (progn
3148             (if (eq (cdr local) 'global)
3149                 ;; Copy the global value of the variable.
3150                 (setq global (symbol-value (car local)))
3151               ;; Use the value from the list.
3152               (setq global (eval (cdr local))))
3153             (set (make-local-variable (car local)) global))
3154         ;; Simple nil-valued local variable.
3155         (set (make-local-variable local) nil)))))
3156
3157 ;; Summary data functions.
3158
3159 (defmacro gnus-data-number (data)
3160   `(car ,data))
3161
3162 (defmacro gnus-data-set-number (data number)
3163   `(setcar ,data ,number))
3164
3165 (defmacro gnus-data-mark (data)
3166   `(nth 1 ,data))
3167
3168 (defmacro gnus-data-set-mark (data mark)
3169   `(setcar (nthcdr 1 ,data) ,mark))
3170
3171 (defmacro gnus-data-pos (data)
3172   `(nth 2 ,data))
3173
3174 (defmacro gnus-data-set-pos (data pos)
3175   `(setcar (nthcdr 2 ,data) ,pos))
3176
3177 (defmacro gnus-data-header (data)
3178   `(nth 3 ,data))
3179
3180 (defmacro gnus-data-set-header (data header)
3181   `(setf (nth 3 ,data) ,header))
3182
3183 (defmacro gnus-data-level (data)
3184   `(nth 4 ,data))
3185
3186 (defmacro gnus-data-unread-p (data)
3187   `(= (nth 1 ,data) gnus-unread-mark))
3188
3189 (defmacro gnus-data-read-p (data)
3190   `(/= (nth 1 ,data) gnus-unread-mark))
3191
3192 (defmacro gnus-data-pseudo-p (data)
3193   `(consp (nth 3 ,data)))
3194
3195 (defmacro gnus-data-find (number)
3196   `(assq ,number gnus-newsgroup-data))
3197
3198 (defmacro gnus-data-find-list (number &optional data)
3199   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3200      (memq (assq ,number bdata)
3201            bdata)))
3202
3203 (defmacro gnus-data-make (number mark pos header level)
3204   `(list ,number ,mark ,pos ,header ,level))
3205
3206 (defun gnus-data-enter (after-article number mark pos header level offset)
3207   (let ((data (gnus-data-find-list after-article)))
3208     (unless data
3209       (error "No such article: %d" after-article))
3210     (setcdr data (cons (gnus-data-make number mark pos header level)
3211                        (cdr data)))
3212     (setq gnus-newsgroup-data-reverse nil)
3213     (gnus-data-update-list (cddr data) offset)))
3214
3215 (defun gnus-data-enter-list (after-article list &optional offset)
3216   (when list
3217     (let ((data (and after-article (gnus-data-find-list after-article)))
3218           (ilist list))
3219       (if (not (or data
3220                    after-article))
3221           (let ((odata gnus-newsgroup-data))
3222             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3223             (when offset
3224               (gnus-data-update-list odata offset)))
3225         ;; Find the last element in the list to be spliced into the main
3226         ;; list.
3227         (setq list (last list))
3228         (if (not data)
3229             (progn
3230               (setcdr list gnus-newsgroup-data)
3231               (setq gnus-newsgroup-data ilist)
3232               (when offset
3233                 (gnus-data-update-list (cdr list) offset)))
3234           (setcdr list (cdr data))
3235           (setcdr data ilist)
3236           (when offset
3237             (gnus-data-update-list (cdr list) offset))))
3238       (setq gnus-newsgroup-data-reverse nil))))
3239
3240 (defun gnus-data-remove (article &optional offset)
3241   (let ((data gnus-newsgroup-data))
3242     (if (= (gnus-data-number (car data)) article)
3243         (progn
3244           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3245                 gnus-newsgroup-data-reverse nil)
3246           (when offset
3247             (gnus-data-update-list gnus-newsgroup-data offset)))
3248       (while (cdr data)
3249         (when (= (gnus-data-number (cadr data)) article)
3250           (setcdr data (cddr data))
3251           (when offset
3252             (gnus-data-update-list (cdr data) offset))
3253           (setq data nil
3254                 gnus-newsgroup-data-reverse nil))
3255         (setq data (cdr data))))))
3256
3257 (defmacro gnus-data-list (backward)
3258   `(if ,backward
3259        (or gnus-newsgroup-data-reverse
3260            (setq gnus-newsgroup-data-reverse
3261                  (reverse gnus-newsgroup-data)))
3262      gnus-newsgroup-data))
3263
3264 (defun gnus-data-update-list (data offset)
3265   "Add OFFSET to the POS of all data entries in DATA."
3266   (setq gnus-newsgroup-data-reverse nil)
3267   (while data
3268     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3269     (setq data (cdr data))))
3270
3271 (defun gnus-summary-article-pseudo-p (article)
3272   "Say whether this article is a pseudo article or not."
3273   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3274
3275 (defmacro gnus-summary-article-sparse-p (article)
3276   "Say whether this article is a sparse article or not."
3277   `(memq ,article gnus-newsgroup-sparse))
3278
3279 (defmacro gnus-summary-article-ancient-p (article)
3280   "Say whether this article is a sparse article or not."
3281   `(memq ,article gnus-newsgroup-ancient))
3282
3283 (defun gnus-article-children (number)
3284   "Return a list of all children to NUMBER."
3285   (let* ((data (gnus-data-find-list number))
3286          (level (gnus-data-level (car data)))
3287          children)
3288     (setq data (cdr data))
3289     (while (and data
3290                 (= (gnus-data-level (car data)) (1+ level)))
3291       (push (gnus-data-number (car data)) children)
3292       (setq data (cdr data)))
3293     children))
3294
3295 (defmacro gnus-summary-skip-intangible ()
3296   "If the current article is intangible, then jump to a different article."
3297   '(let ((to (get-text-property (point) 'gnus-intangible)))
3298      (and to (gnus-summary-goto-subject to))))
3299
3300 (defmacro gnus-summary-article-intangible-p ()
3301   "Say whether this article is intangible or not."
3302   '(get-text-property (point) 'gnus-intangible))
3303
3304 ;; Some summary mode macros.
3305
3306 (defmacro gnus-summary-article-number ()
3307   "The article number of the article on the current line.
3308 If there isn't an article number here, then we return the current
3309 article number."
3310   '(progn
3311      (gnus-summary-skip-intangible)
3312      (or (get-text-property (point) 'gnus-number)
3313          (gnus-summary-last-subject))))
3314
3315 (defmacro gnus-summary-article-header (&optional number)
3316   "Return the header of article NUMBER."
3317   `(gnus-data-header (gnus-data-find
3318                       ,(or number '(gnus-summary-article-number)))))
3319
3320 (defmacro gnus-summary-thread-level (&optional number)
3321   "Return the level of thread that starts with article NUMBER."
3322   `(if (and (eq gnus-summary-make-false-root 'dummy)
3323             (get-text-property (point) 'gnus-intangible))
3324        0
3325      (gnus-data-level (gnus-data-find
3326                        ,(or number '(gnus-summary-article-number))))))
3327
3328 (defmacro gnus-summary-article-mark (&optional number)
3329   "Return the mark of article NUMBER."
3330   `(gnus-data-mark (gnus-data-find
3331                     ,(or number '(gnus-summary-article-number)))))
3332
3333 (defmacro gnus-summary-article-pos (&optional number)
3334   "Return the position of the line of article NUMBER."
3335   `(gnus-data-pos (gnus-data-find
3336                    ,(or number '(gnus-summary-article-number)))))
3337
3338 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3339 (defmacro gnus-summary-article-subject (&optional number)
3340   "Return current subject string or nil if nothing."
3341   `(let ((headers
3342           ,(if number
3343                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3344              '(gnus-data-header (assq (gnus-summary-article-number)
3345                                       gnus-newsgroup-data)))))
3346      (and headers
3347           (vectorp headers)
3348           (mail-header-subject headers))))
3349
3350 (defmacro gnus-summary-article-score (&optional number)
3351   "Return current article score."
3352   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3353                   gnus-newsgroup-scored))
3354        gnus-summary-default-score 0))
3355
3356 (defun gnus-summary-article-children (&optional number)
3357   "Return a list of article numbers that are children of article NUMBER."
3358   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3359          (level (gnus-data-level (car data)))
3360          l children)
3361     (while (and (setq data (cdr data))
3362                 (> (setq l (gnus-data-level (car data))) level))
3363       (and (= (1+ level) l)
3364            (push (gnus-data-number (car data))
3365                  children)))
3366     (nreverse children)))
3367
3368 (defun gnus-summary-article-parent (&optional number)
3369   "Return the article number of the parent of article NUMBER."
3370   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3371                                     (gnus-data-list t)))
3372          (level (gnus-data-level (car data))))
3373     (if (zerop level)
3374         ()                              ; This is a root.
3375       ;; We search until we find an article with a level less than
3376       ;; this one.  That function has to be the parent.
3377       (while (and (setq data (cdr data))
3378                   (not (< (gnus-data-level (car data)) level))))
3379       (and data (gnus-data-number (car data))))))
3380
3381 (defun gnus-unread-mark-p (mark)
3382   "Say whether MARK is the unread mark."
3383   (= mark gnus-unread-mark))
3384
3385 (defun gnus-read-mark-p (mark)
3386   "Say whether MARK is one of the marks that mark as read.
3387 This is all marks except unread, ticked, dormant, and expirable."
3388   (not (or (= mark gnus-unread-mark)
3389            (= mark gnus-ticked-mark)
3390            (= mark gnus-spam-mark)
3391            (= mark gnus-dormant-mark)
3392            (= mark gnus-expirable-mark))))
3393
3394 (defmacro gnus-article-mark (number)
3395   "Return the MARK of article NUMBER.
3396 This macro should only be used when computing the mark the \"first\"
3397 time; i.e., when generating the summary lines.  After that,
3398 `gnus-summary-article-mark' should be used to examine the
3399 marks of articles."
3400   `(cond
3401     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3402     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3403     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3404     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3405     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3406     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3407     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3408     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3409            gnus-ancient-mark))))
3410
3411 ;; Saving hidden threads.
3412
3413 (defmacro gnus-save-hidden-threads (&rest forms)
3414   "Save hidden threads, eval FORMS, and restore the hidden threads."
3415   (let ((config (make-symbol "config")))
3416     `(let ((,config (gnus-hidden-threads-configuration)))
3417        (unwind-protect
3418            (save-excursion
3419              ,@forms)
3420          (gnus-restore-hidden-threads-configuration ,config)))))
3421 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3422 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3423
3424 (defun gnus-data-compute-positions ()
3425   "Compute the positions of all articles."
3426   (setq gnus-newsgroup-data-reverse nil)
3427   (let ((data gnus-newsgroup-data))
3428     (save-excursion
3429       (gnus-save-hidden-threads
3430         (gnus-summary-show-all-threads)
3431         (goto-char (point-min))
3432         (while data
3433           (while (get-text-property (point) 'gnus-intangible)
3434             (forward-line 1))
3435           (gnus-data-set-pos (car data) (+ (point) 3))
3436           (setq data (cdr data))
3437           (forward-line 1))))))
3438
3439 (defun gnus-hidden-threads-configuration ()
3440   "Return the current hidden threads configuration."
3441   (save-excursion
3442     (let (config)
3443       (goto-char (point-min))
3444       (while (not (eobp))
3445         (when (eq (get-char-property (point-at-eol) 'invisible) 'gnus-sum)
3446           (push (save-excursion (forward-line 0) (point)) config))
3447         (forward-line 1))
3448       config)))
3449
3450 (defun gnus-restore-hidden-threads-configuration (config)
3451   "Restore hidden threads configuration from CONFIG."
3452   (save-excursion
3453     (let (point (inhibit-read-only t))
3454       (while (setq point (pop config))
3455         (goto-char point)
3456         (gnus-summary-hide-thread)))))
3457
3458 ;; Various summary mode internalish functions.
3459
3460 (defun gnus-mouse-pick-article (e)
3461   (interactive "e")
3462   (mouse-set-point e)
3463   (gnus-summary-next-page nil t))
3464
3465 (defun gnus-summary-set-display-table ()
3466   "Change the display table.
3467 Odd characters have a tendency to mess
3468 up nicely formatted displays - we make all possible glyphs
3469 display only a single character."
3470
3471   ;; We start from the standard display table, if any.
3472   (let ((table (or (copy-sequence standard-display-table)
3473                    (make-display-table)))
3474         (i 32))
3475     ;; Nix out all the control chars...
3476     (while (>= (setq i (1- i)) 0)
3477       (gnus-put-display-table i [??] table))
3478    ;; ... but not newline and cr, of course.  (cr is necessary for the
3479     ;; selective display).
3480     (gnus-put-display-table ?\n nil table)
3481     (gnus-put-display-table ?\r nil table)
3482     ;; We keep TAB as well.
3483     (gnus-put-display-table ?\t nil table)
3484     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3485     ;; Emacs 23 (unicode), that are not set already.
3486     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3487                  160
3488                256)))
3489       (while (>= (setq i (1- i)) 127)
3490         ;; Only modify if the entry is nil.
3491         (unless (gnus-get-display-table i table)
3492           (gnus-put-display-table i [??] table))))
3493     (setq buffer-display-table table)))
3494
3495 (defun gnus-summary-set-article-display-arrow (pos)
3496   "Update the overlay arrow to point to line at position POS."
3497   (when gnus-summary-display-arrow
3498     (make-local-variable 'overlay-arrow-position)
3499     (make-local-variable 'overlay-arrow-string)
3500     (save-excursion
3501       (goto-char pos)
3502       (beginning-of-line)
3503       (unless overlay-arrow-position
3504         (setq overlay-arrow-position (make-marker)))
3505       (setq overlay-arrow-string "=>"
3506             overlay-arrow-position (set-marker overlay-arrow-position
3507                                                (point)
3508                                                (current-buffer))))))
3509
3510 (defun gnus-summary-setup-buffer (group)
3511   "Initialize summary buffer.
3512 If the setup was successful, non-nil is returned."
3513   (let ((buffer (gnus-summary-buffer-name group))
3514         (dead-name (concat "*Dead Summary "
3515                            (gnus-group-decoded-name group) "*")))
3516     ;; If a dead summary buffer exists, we kill it.
3517     (when (gnus-buffer-live-p dead-name)
3518       (gnus-kill-buffer dead-name))
3519     (if (get-buffer buffer)
3520         (progn
3521           (set-buffer buffer)
3522           (setq gnus-summary-buffer (current-buffer))
3523           (not gnus-newsgroup-prepared))
3524       (set-buffer (gnus-get-buffer-create buffer))
3525       (setq gnus-summary-buffer (current-buffer))
3526       (gnus-summary-mode group)
3527       (when (gnus-group-quit-config group)
3528         (set (make-local-variable 'gnus-single-article-buffer) nil))
3529       (make-local-variable 'gnus-article-buffer)
3530       (make-local-variable 'gnus-article-current)
3531       (make-local-variable 'gnus-original-article-buffer)
3532       (setq gnus-newsgroup-name group)
3533       ;; Set any local variables in the group parameters.
3534       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3535       t)))
3536
3537 (defun gnus-set-global-variables ()
3538   "Set the global equivalents of the buffer-local variables.
3539 They are set to the latest values they had.  These reflect the summary
3540 buffer that was in action when the last article was fetched."
3541   (when (derived-mode-p 'gnus-summary-mode)
3542     (setq gnus-summary-buffer (current-buffer))
3543     (let ((name gnus-newsgroup-name)
3544           (marked gnus-newsgroup-marked)
3545           (spam gnus-newsgroup-spam-marked)
3546           (unread gnus-newsgroup-unreads)
3547           (headers gnus-current-headers)
3548           (data gnus-newsgroup-data)
3549           (summary gnus-summary-buffer)
3550           (article-buffer gnus-article-buffer)
3551           (original gnus-original-article-buffer)
3552           (gac gnus-article-current)
3553           (reffed gnus-reffed-article-number)
3554           (score-file gnus-current-score-file)
3555           (default-charset gnus-newsgroup-charset)
3556           vlist)
3557       (let ((locals gnus-newsgroup-variables))
3558         (while locals
3559           (if (consp (car locals))
3560               (push (eval (caar locals)) vlist)
3561             (push (eval (car locals)) vlist))
3562           (setq locals (cdr locals)))
3563         (setq vlist (nreverse vlist)))
3564       (with-temp-buffer
3565         (setq gnus-newsgroup-name name
3566               gnus-newsgroup-marked marked
3567               gnus-newsgroup-spam-marked spam
3568               gnus-newsgroup-unreads unread
3569               gnus-current-headers headers
3570               gnus-newsgroup-data data
3571               gnus-article-current gac
3572               gnus-summary-buffer summary
3573               gnus-article-buffer article-buffer
3574               gnus-original-article-buffer original
3575               gnus-reffed-article-number reffed
3576               gnus-current-score-file score-file
3577               gnus-newsgroup-charset default-charset)
3578         (let ((locals gnus-newsgroup-variables))
3579           (while locals
3580             (if (consp (car locals))
3581                 (set (caar locals) (pop vlist))
3582               (set (car locals) (pop vlist)))
3583             (setq locals (cdr locals))))))))
3584
3585 (defun gnus-summary-article-unread-p (article)
3586   "Say whether ARTICLE is unread or not."
3587   (memq article gnus-newsgroup-unreads))
3588
3589 (defun gnus-summary-first-article-p (&optional article)
3590   "Return whether ARTICLE is the first article in the buffer."
3591   (if (not (setq article (or article (gnus-summary-article-number))))
3592       nil
3593     (eq article (caar gnus-newsgroup-data))))
3594
3595 (defun gnus-summary-last-article-p (&optional article)
3596   "Return whether ARTICLE is the last article in the buffer."
3597   (if (not (setq article (or article (gnus-summary-article-number))))
3598       ;; All non-existent numbers are the last article.  :-)
3599       t
3600     (not (cdr (gnus-data-find-list article)))))
3601
3602 (defun gnus-make-thread-indent-array (&optional n)
3603   (when (or n
3604             (progn (setq n 200) nil)
3605             (null gnus-thread-indent-array)
3606             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3607     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3608           gnus-thread-indent-array-level gnus-thread-indent-level)
3609     (while (>= n 0)
3610       (aset gnus-thread-indent-array n
3611             (make-string (* n gnus-thread-indent-level) ? ))
3612       (setq n (1- n)))))
3613
3614 (defun gnus-update-summary-mark-positions ()
3615   "Compute where the summary marks are to go."
3616   (save-excursion
3617     (when (gnus-buffer-exists-p gnus-summary-buffer)
3618       (set-buffer gnus-summary-buffer))
3619     (let ((spec gnus-summary-line-format-spec)
3620           pos)
3621       (save-excursion
3622         (gnus-set-work-buffer)
3623         (let ((gnus-tmp-unread ?Z)
3624               (gnus-replied-mark ?Z)
3625               (gnus-score-below-mark ?Z)
3626               (gnus-score-over-mark ?Z)
3627               (gnus-undownloaded-mark ?Z)
3628               (gnus-summary-line-format-spec spec)
3629               (gnus-newsgroup-downloadable '(0))
3630               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3631               case-fold-search ignores)
3632           ;; Here, all marks are bound to Z.
3633           (gnus-summary-insert-line header
3634                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3635           (goto-char (point-min))
3636           ;; Memorize the positions of the same characters as dummy marks.
3637           (while (re-search-forward "[A-D]" nil t)
3638             (push (point) ignores))
3639           (erase-buffer)
3640           ;; We use A-D as dummy marks in order to know column positions
3641           ;; where marks should be inserted.
3642           (setq gnus-tmp-unread ?A
3643                 gnus-replied-mark ?B
3644                 gnus-score-below-mark ?C
3645                 gnus-score-over-mark ?C
3646                 gnus-undownloaded-mark ?D)
3647           (gnus-summary-insert-line header
3648                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3649           ;; Ignore characters which aren't dummy marks.
3650           (dolist (p ignores)
3651             (delete-region (goto-char (1- p)) p)
3652             (insert ?Z))
3653           (goto-char (point-min))
3654           (setq pos (list (cons 'unread
3655                                 (and (search-forward "A" nil t)
3656                                      (- (point) (point-min) 1)))))
3657           (goto-char (point-min))
3658           (push (cons 'replied (and (search-forward "B" nil t)
3659                                     (- (point) (point-min) 1)))
3660                 pos)
3661           (goto-char (point-min))
3662           (push (cons 'score (and (search-forward "C" nil t)
3663                                   (- (point) (point-min) 1)))
3664                 pos)
3665           (goto-char (point-min))
3666           (push (cons 'download (and (search-forward "D" nil t)
3667                                      (- (point) (point-min) 1)))
3668                 pos)))
3669       (setq gnus-summary-mark-positions pos))))
3670
3671 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3672   "Insert a dummy root in the summary buffer."
3673   (beginning-of-line)
3674   (gnus-add-text-properties
3675    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3676    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3677
3678 (defun gnus-summary-extract-address-component (from)
3679   (or (car (funcall gnus-extract-address-components from))
3680       from))
3681
3682 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3683   (let ((mail-parse-charset gnus-newsgroup-charset)
3684         (ignored-from-addresses (gnus-ignored-from-addresses))
3685         ;; Is it really necessary to do this next part for each summary line?
3686         ;; Luckily, doesn't seem to slow things down much.
3687         (mail-parse-ignored-charsets
3688          (with-current-buffer gnus-summary-buffer
3689            gnus-newsgroup-ignored-charsets)))
3690     (or
3691      (and ignored-from-addresses
3692           (string-match ignored-from-addresses gnus-tmp-from)
3693           (let ((extra-headers (mail-header-extra header))
3694                 to
3695                 newsgroups)
3696             (cond
3697              ((setq to (cdr (assq 'To extra-headers)))
3698               (concat gnus-summary-to-prefix
3699                       (inline
3700                         (gnus-summary-extract-address-component
3701                          (funcall gnus-decode-encoded-address-function to)))))
3702              ((setq newsgroups
3703                     (or
3704                      (cdr (assq 'Newsgroups extra-headers))
3705                      (and
3706                       (memq 'Newsgroups gnus-extra-headers)
3707                       (eq (car (gnus-find-method-for-group
3708                                 gnus-newsgroup-name)) 'nntp)
3709                       (gnus-group-real-name gnus-newsgroup-name))))
3710               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3711      (gnus-string-mark-left-to-right
3712       (inline
3713         (gnus-summary-extract-address-component gnus-tmp-from))))))
3714
3715 (defun gnus-summary-insert-line (gnus-tmp-header
3716                                  gnus-tmp-level gnus-tmp-current
3717                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3718                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3719                                  &optional gnus-tmp-dummy gnus-tmp-score
3720                                  gnus-tmp-process)
3721   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3722       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3723                                           gnus-tmp-level)))
3724   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3725          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3726          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3727          (gnus-tmp-score-char
3728           (if (or (null gnus-summary-default-score)
3729                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3730                       gnus-summary-zcore-fuzz))
3731               ?                         ;Whitespace
3732             (if (< gnus-tmp-score gnus-summary-default-score)
3733                 gnus-score-below-mark gnus-score-over-mark)))
3734          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3735          (gnus-tmp-replied
3736           (cond (gnus-tmp-process gnus-process-mark)
3737                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3738                  gnus-cached-mark)
3739                 (gnus-tmp-replied gnus-replied-mark)
3740                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3741                  gnus-forwarded-mark)
3742                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3743                  gnus-saved-mark)
3744                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3745                  gnus-unseen-mark)
3746                 (t gnus-no-mark)))
3747          (gnus-tmp-downloaded
3748           (cond (undownloaded
3749                  gnus-undownloaded-mark)
3750                 (gnus-newsgroup-agentized
3751                  gnus-downloaded-mark)
3752                 (t
3753                  gnus-no-mark)))
3754          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3755          (gnus-tmp-name
3756           (cond
3757            ((string-match "<[^>]+> *$" gnus-tmp-from)
3758             (let ((beg (match-beginning 0)))
3759               (or (and (string-match "^\".+\"" gnus-tmp-from)
3760                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3761                   (substring gnus-tmp-from 0 beg))))
3762            ((string-match "(.+)" gnus-tmp-from)
3763             (substring gnus-tmp-from
3764                        (1+ (match-beginning 0)) (1- (match-end 0))))
3765            (t gnus-tmp-from)))
3766          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3767          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3768          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3769          (inhibit-read-only t))
3770     (when (string= gnus-tmp-name "")
3771       (setq gnus-tmp-name gnus-tmp-from))
3772     (unless (numberp gnus-tmp-lines)
3773       (setq gnus-tmp-lines -1))
3774     (if (= gnus-tmp-lines -1)
3775         (setq gnus-tmp-lines "?")
3776       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3777     (condition-case ()
3778         (gnus-put-text-property
3779          (point)
3780          (progn (eval gnus-summary-line-format-spec) (point))
3781          'gnus-number gnus-tmp-number)
3782       (error (gnus-message 5 "Error updating the summary line")))
3783     (when (gnus-visual-p 'summary-highlight 'highlight)
3784       (forward-line -1)
3785       (gnus-summary-highlight-line)
3786       (gnus-run-hooks 'gnus-summary-update-hook)
3787       (forward-line 1))))
3788
3789 (defun gnus-summary-update-line (&optional dont-update)
3790   "Update summary line after change."
3791   (when (and gnus-summary-default-score
3792              (not gnus-summary-inhibit-highlight))
3793     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3794            (article (gnus-summary-article-number))
3795            (score (gnus-summary-article-score article)))
3796       (unless dont-update
3797         (if (and gnus-summary-mark-below
3798                  (< (gnus-summary-article-score)
3799                     gnus-summary-mark-below))
3800             ;; This article has a low score, so we mark it as read.
3801             (when (memq article gnus-newsgroup-unreads)
3802               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3803           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3804             ;; This article was previously marked as read on account
3805             ;; of a low score, but now it has risen, so we mark it as
3806             ;; unread.
3807             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3808         (gnus-summary-update-mark
3809          (if (or (null gnus-summary-default-score)
3810                  (<= (abs (- score gnus-summary-default-score))
3811                      gnus-summary-zcore-fuzz))
3812              ?                          ;Whitespace
3813            (if (< score gnus-summary-default-score)
3814                gnus-score-below-mark gnus-score-over-mark))
3815          'score))
3816       ;; Do visual highlighting.
3817       (when (gnus-visual-p 'summary-highlight 'highlight)
3818         (gnus-summary-highlight-line)
3819         (gnus-run-hooks 'gnus-summary-update-hook)))))
3820
3821 (defvar gnus-tmp-new-adopts nil)
3822
3823 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3824   "Return the number of articles in THREAD.
3825 This may be 0 in some cases -- if none of the articles in
3826 the thread are to be displayed."
3827   (let* ((number
3828          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3829           (cond
3830            ((not (listp thread))
3831             1)
3832            ((and (consp thread) (cdr thread))
3833             (apply
3834              '+ 1 (mapcar
3835                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3836            ((null thread)
3837             1)
3838            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3839             1)
3840            (t 0))))
3841     (when (and level (zerop level) gnus-tmp-new-adopts)
3842       (incf number
3843             (apply '+ (mapcar
3844                        'gnus-summary-number-of-articles-in-thread
3845                        gnus-tmp-new-adopts))))
3846     (if char
3847         (if (> number 1) gnus-not-empty-thread-mark
3848           gnus-empty-thread-mark)
3849       number)))
3850
3851 (defsubst gnus-summary-line-message-size (head)
3852   "Return pretty-printed version of message size.
3853 This function is intended to be used in
3854 `gnus-summary-line-format-alist'."
3855   (let ((c (or (mail-header-chars head) -1)))
3856     (cond ((< c 0) "n/a")               ; chars not available
3857           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3858           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3859           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3860           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3861
3862 (defcustom gnus-user-date-format-alist
3863   '(((gnus-seconds-today) . "Today, %H:%M")
3864     ((+ 86400 (gnus-seconds-today)) . "Yesterday, %H:%M")
3865     (604800 . "%A %H:%M")               ; That's one week
3866     ((gnus-seconds-month) . "%A %d")
3867     ((gnus-seconds-year) . "%B %d")
3868     (t . "%b %d %Y"))                   ; This one is used when no other
3869                                         ; does match
3870   "Specifies date format depending on age of article.
3871 This is an alist of items (AGE . FORMAT).  AGE can be a number (of
3872 seconds) or a Lisp expression evaluating to a number.  When the age of
3873 the article is less than this number, then use `format-time-string'
3874 with the corresponding FORMAT for displaying the date of the article.
3875 If AGE is not a number or a Lisp expression evaluating to a
3876 non-number, then the corresponding FORMAT is used as a default value.
3877
3878 Note that the list is processed from the beginning, so it should be
3879 sorted by ascending AGE.  Also note that items following the first
3880 non-number AGE will be ignored.
3881
3882 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
3883 and `gnus-seconds-year' in the AGE spec.  They return the number of
3884 seconds passed since the start of today, of this month, of this year,
3885 respectively."
3886   :version "24.1"
3887   :group 'gnus-summary-format
3888   :type '(alist :key-type sexp :value-type string))
3889
3890 (defun gnus-user-date (messy-date)
3891   "Format the messy-date according to `gnus-user-date-format-alist'.
3892 Returns \"  ?  \" if there's bad input or if another error occurs.
3893 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
3894   (condition-case ()
3895       (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date)))
3896              (now (gnus-float-time))
3897              ;;If we don't find something suitable we'll use this one
3898              (my-format "%b %d '%y"))
3899         (let* ((difference (- now messy-date))
3900                (templist gnus-user-date-format-alist)
3901                (top (eval (caar templist))))
3902           (while (if (numberp top) (< top difference) (not top))
3903             (progn
3904               (setq templist (cdr templist))
3905               (setq top (eval (caar templist)))))
3906           (if (stringp (cdr (car templist)))
3907               (setq my-format (cdr (car templist)))))
3908         (format-time-string (eval my-format) (seconds-to-time messy-date)))
3909     (error "  ?   ")))
3910
3911 (defun gnus-summary-set-local-parameters (group)
3912   "Go through the local params of GROUP and set all variable specs in that list."
3913   (let ((vars '(quit-config active)))   ; Ignore things that aren't
3914                                         ; really variables.
3915     (dolist (elem (gnus-group-find-parameter group))
3916       (and (consp elem)                 ; Has to be a cons.
3917            (consp (cdr elem))           ; The cdr has to be a list.
3918            (symbolp (car elem))         ; Has to be a symbol in there.
3919            (not (memq (car elem) vars))
3920            (ignore-errors
3921              (push (car elem) vars)
3922              ;; Variables like `gnus-show-threads' that are globally
3923              ;; bound, if used as group parameters, need to get to be
3924              ;; buffer-local, whereas just parameters like `gcc-self',
3925              ;; `timestamp', etc. should not be bound as variables.
3926              (if (boundp (car elem))
3927                  (set (make-local-variable (car elem)) (eval (nth 1 elem)))
3928                (eval (nth 1 elem))))))))
3929
3930 (defun gnus-summary-read-group (group &optional show-all no-article
3931                                       kill-buffer no-display backward
3932                                       select-articles)
3933   "Start reading news in newsgroup GROUP.
3934 If SHOW-ALL is non-nil, already read articles are also listed.
3935 If NO-ARTICLE is non-nil, no article is selected initially.
3936 If NO-DISPLAY, don't generate the summary buffer contents.
3937 If KILL-BUFFER, it should be a buffer that's killed once the new
3938 summary buffer has been generated.
3939 If BACKWARD, move point to the previous group in the group buffer
3940 If SELECT-ARTICLES, only select those articles from GROUP."
3941   (let (result)
3942     (while (and group
3943                 (null (setq result
3944                             (let ((gnus-auto-select-next nil))
3945                               (or (gnus-summary-read-group-1
3946                                    group show-all no-article
3947                                    kill-buffer no-display
3948                                    select-articles)
3949                                   (setq show-all nil
3950                                         select-articles nil)))))
3951                 (eq gnus-auto-select-next 'quietly))
3952       (set-buffer gnus-group-buffer)
3953       ;; The entry function called above goes to the next
3954       ;; group automatically, so we go two groups back
3955       ;; if we are searching for the previous group.
3956       (when backward
3957         (gnus-group-prev-unread-group 2))
3958       (if (not (equal group (gnus-group-group-name)))
3959           (setq group (gnus-group-group-name))
3960         (setq group nil)))
3961     result))
3962
3963 (defun gnus-summary-read-group-1 (group show-all no-article
3964                                         kill-buffer no-display
3965                                         &optional select-articles)
3966   ;; Killed foreign groups can't be entered.
3967   ;;  (when (and (not (gnus-group-native-p group))
3968   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3969   ;;    (error "Dead non-native groups can't be entered"))
3970   (gnus-message 7 "Retrieving newsgroup: %s..."
3971                 (gnus-group-decoded-name group))
3972   (let* ((new-group (gnus-summary-setup-buffer group))
3973          (quit-config (gnus-group-quit-config group))
3974          (did-select (and new-group (gnus-select-newsgroup
3975                                      group show-all select-articles))))
3976     (cond
3977      ;; This summary buffer exists already, so we just select it.
3978      ((not new-group)
3979       (gnus-set-global-variables)
3980       (when kill-buffer
3981         (gnus-kill-or-deaden-summary kill-buffer))
3982       (gnus-configure-windows 'summary 'force)
3983       (gnus-set-mode-line 'summary)
3984       (gnus-summary-position-point)
3985       (message "")
3986       t)
3987      ;; We couldn't select this group.
3988      ((null did-select)
3989       (when (and (derived-mode-p 'gnus-summary-mode)
3990                  (not (equal (current-buffer) kill-buffer)))
3991         (kill-buffer (current-buffer))
3992         (if (not quit-config)
3993             (progn
3994               ;; Update the info -- marks might need to be removed,
3995               ;; for instance.
3996               (gnus-summary-update-info)
3997               (set-buffer gnus-group-buffer)
3998               (gnus-group-jump-to-group group)
3999               (gnus-group-next-unread-group 1))
4000           (gnus-handle-ephemeral-exit quit-config)))
4001       (if (null (gnus-list-of-unread-articles group))
4002           (gnus-message 3 "Group %s contains no messages" group)
4003         (gnus-message 3 "Can't select group"))
4004       nil)
4005      ;; The user did a `C-g' while prompting for number of articles,
4006      ;; so we exit this group.
4007      ((eq did-select 'quit)
4008       (and (derived-mode-p 'gnus-summary-mode)
4009            (not (equal (current-buffer) kill-buffer))
4010            (kill-buffer (current-buffer)))
4011       (when kill-buffer
4012         (gnus-kill-or-deaden-summary kill-buffer))
4013       (if (not quit-config)
4014           (progn
4015             (set-buffer gnus-group-buffer)
4016             (gnus-group-jump-to-group group)
4017             (gnus-configure-windows 'group 'force))
4018         (gnus-handle-ephemeral-exit quit-config))
4019       ;; Finally signal the quit.
4020       (signal 'quit nil))
4021      ;; The group was successfully selected.
4022      (t
4023       (gnus-set-global-variables)
4024       (when (boundp 'spam-install-hooks)
4025         (spam-initialize))
4026       ;; Save the active value in effect when the group was entered.
4027       (setq gnus-newsgroup-active
4028             (gnus-copy-sequence
4029              (gnus-active gnus-newsgroup-name)))
4030       (setq gnus-newsgroup-highest (cdr gnus-newsgroup-active))
4031       ;; You can change the summary buffer in some way with this hook.
4032       (gnus-run-hooks 'gnus-select-group-hook)
4033       (when (memq 'summary (gnus-update-format-specifications
4034                             nil 'summary 'summary-mode 'summary-dummy))
4035         ;; The format specification for the summary line was updated,
4036         ;; so we need to update the mark positions as well.
4037         (gnus-update-summary-mark-positions))
4038       ;; Do score processing.
4039       (when gnus-use-scoring
4040         (gnus-possibly-score-headers))
4041       ;; Check whether to fill in the gaps in the threads.
4042       (when gnus-build-sparse-threads
4043         (gnus-build-sparse-threads))
4044       ;; Find the initial limit.
4045       (if show-all
4046           (let ((gnus-newsgroup-dormant nil))
4047             (gnus-summary-initial-limit show-all))
4048         (gnus-summary-initial-limit show-all))
4049       ;; Generate the summary buffer.
4050       (unless no-display
4051         (gnus-summary-prepare))
4052       (when gnus-use-trees
4053         (gnus-tree-open)
4054         (setq gnus-summary-highlight-line-function
4055               'gnus-tree-highlight-article))
4056       ;; If the summary buffer is empty, but there are some low-scored
4057       ;; articles or some excluded dormants, we include these in the
4058       ;; buffer.
4059       (when (and (zerop (buffer-size))
4060                  (not no-display))
4061         (cond (gnus-newsgroup-dormant
4062                (gnus-summary-limit-include-dormant))
4063               ((and gnus-newsgroup-scored show-all)
4064                (gnus-summary-limit-include-expunged t))))
4065       ;; Function `gnus-apply-kill-file' must be called in this hook.
4066       (gnus-run-hooks 'gnus-apply-kill-hook)
4067       (if (and (zerop (buffer-size))
4068                (not no-display))
4069           (progn
4070             ;; This newsgroup is empty.
4071             (gnus-summary-catchup-and-exit nil t)
4072             (gnus-message 6 "No unread news")
4073             (when kill-buffer
4074               (gnus-kill-or-deaden-summary kill-buffer))
4075             ;; Return nil from this function.
4076             nil)
4077         ;; Hide conversation thread subtrees.  We cannot do this in
4078         ;; gnus-summary-prepare-hook since kill processing may not
4079         ;; work with hidden articles.
4080         (gnus-summary-maybe-hide-threads)
4081         (gnus-configure-windows 'summary)
4082         (when kill-buffer
4083           (gnus-kill-or-deaden-summary kill-buffer))
4084         (gnus-summary-auto-select-subject)
4085         ;; Show first unread article if requested.
4086         (if (and (not no-article)
4087                  (not no-display)
4088                  gnus-newsgroup-unreads
4089                  gnus-auto-select-first)
4090             (progn
4091               (let ((art (gnus-summary-article-number)))
4092                 (when (and art
4093                            gnus-plugged
4094                            (not (memq art gnus-newsgroup-undownloaded))
4095                            (not (memq art gnus-newsgroup-downloadable)))
4096                   (gnus-summary-goto-article art))))
4097           ;; Don't select any articles.
4098           (gnus-summary-position-point)
4099           (gnus-configure-windows 'summary 'force)
4100           (gnus-set-mode-line 'summary))
4101         (when (and gnus-auto-center-group
4102                    (get-buffer-window gnus-group-buffer t))
4103           ;; Gotta use windows, because recenter does weird stuff if
4104           ;; the current buffer ain't the displayed window.
4105           (let ((owin (selected-window)))
4106             (select-window (get-buffer-window gnus-group-buffer t))
4107             (when (gnus-group-goto-group group)
4108               (recenter))
4109             (select-window owin)))
4110         ;; Mark this buffer as "prepared".
4111         (setq gnus-newsgroup-prepared t)
4112         (gnus-run-hooks 'gnus-summary-prepared-hook)
4113         (unless (gnus-ephemeral-group-p group)
4114           (gnus-group-update-group group nil t))
4115         t)))))
4116
4117 (defun gnus-summary-auto-select-subject ()
4118   "Select the subject line on initial group entry."
4119   (goto-char (point-min))
4120   (cond
4121    ((eq gnus-auto-select-subject 'best)
4122     (gnus-summary-best-unread-subject))
4123    ((eq gnus-auto-select-subject 'unread)
4124     (gnus-summary-first-unread-subject))
4125    ((eq gnus-auto-select-subject 'unseen)
4126     (gnus-summary-first-unseen-subject))
4127    ((eq gnus-auto-select-subject 'unseen-or-unread)
4128     (gnus-summary-first-unseen-or-unread-subject))
4129    ((eq gnus-auto-select-subject 'first)
4130     ;; Do nothing.
4131     )
4132    ((functionp gnus-auto-select-subject)
4133     (funcall gnus-auto-select-subject))))
4134
4135 (defun gnus-summary-prepare ()
4136   "Generate the summary buffer."
4137   (interactive)
4138   (let ((inhibit-read-only t))
4139     (erase-buffer)
4140     (setq gnus-newsgroup-data nil
4141           gnus-newsgroup-data-reverse nil)
4142     (gnus-run-hooks 'gnus-summary-generate-hook)
4143     ;; Generate the buffer, either with threads or without.
4144     (when gnus-newsgroup-headers
4145       (gnus-summary-prepare-threads
4146        (if gnus-show-threads
4147            (gnus-sort-gathered-threads
4148             (funcall gnus-summary-thread-gathering-function
4149                      (gnus-sort-threads
4150                       (gnus-cut-threads (gnus-make-threads)))))
4151          ;; Unthreaded display.
4152          (gnus-sort-articles gnus-newsgroup-headers))))
4153     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
4154     ;; Call hooks for modifying summary buffer.
4155     (goto-char (point-min))
4156     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4157
4158 (defsubst gnus-general-simplify-subject (subject)
4159   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4160   (setq subject
4161         (cond
4162          ;; Truncate the subject.
4163          (gnus-simplify-subject-functions
4164           (gnus-map-function gnus-simplify-subject-functions subject))
4165          ((numberp gnus-summary-gather-subject-limit)
4166           (setq subject (gnus-simplify-subject-re subject))
4167           (if (> (length subject) gnus-summary-gather-subject-limit)
4168               (substring subject 0 gnus-summary-gather-subject-limit)
4169             subject))
4170          ;; Fuzzily simplify it.
4171          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4172           (gnus-simplify-subject-fuzzy subject))
4173          ;; Just remove the leading "Re:".
4174          (t
4175           (gnus-simplify-subject-re subject))))
4176
4177   (if (and gnus-summary-gather-exclude-subject
4178            (string-match gnus-summary-gather-exclude-subject subject))
4179       nil                         ; This article shouldn't be gathered
4180     subject))
4181
4182 (defun gnus-summary-simplify-subject-query ()
4183   "Query where the respool algorithm would put this article."
4184   (interactive)
4185   (gnus-summary-select-article)
4186   (message "%s" (gnus-general-simplify-subject (gnus-summary-article-subject))))
4187
4188 (defun gnus-gather-threads-by-subject (threads)
4189   "Gather threads by looking at Subject headers."
4190   (if (not gnus-summary-make-false-root)
4191       threads
4192     (let ((hashtb (gnus-make-hashtable 1024))
4193           (prev threads)
4194           (result threads)
4195           subject hthread whole-subject)
4196       (while threads
4197         (setq subject (gnus-general-simplify-subject
4198                        (setq whole-subject (mail-header-subject
4199                                             (caar threads)))))
4200         (when subject
4201           (if (setq hthread (gnus-gethash subject hashtb))
4202               (progn
4203                 ;; We enter a dummy root into the thread, if we
4204                 ;; haven't done that already.
4205                 (unless (stringp (caar hthread))
4206                   (setcar hthread (list whole-subject (car hthread))))
4207                 ;; We add this new gathered thread to this gathered
4208                 ;; thread.
4209                 (setcdr (car hthread)
4210                         (nconc (cdar hthread) (list (car threads))))
4211                 ;; Remove it from the list of threads.
4212                 (setcdr prev (cdr threads))
4213                 (setq threads prev))
4214             ;; Enter this thread into the hash table.
4215             (gnus-sethash subject
4216                           (if gnus-summary-make-false-root-always
4217                               (progn
4218                                 ;; If you want a dummy root above all
4219                                 ;; threads...
4220                                 (setcar threads (list whole-subject
4221                                                       (car threads)))
4222                                 threads)
4223                             threads)
4224                           hashtb)))
4225         (setq prev threads)
4226         (setq threads (cdr threads)))
4227       result)))
4228
4229 (defun gnus-gather-threads-by-references (threads)
4230   "Gather threads by looking at References headers."
4231   (let ((idhashtb (gnus-make-hashtable 1024))
4232         (thhashtb (gnus-make-hashtable 1024))
4233         (prev threads)
4234         (result threads)
4235         ids references id gthread gid entered ref)
4236     (while threads
4237       (when (setq references (mail-header-references (caar threads)))
4238         (setq id (mail-header-id (caar threads))
4239               ids (inline (gnus-split-references references))
4240               entered nil)
4241         (while (setq ref (pop ids))
4242           (setq ids (delete ref ids))
4243           (if (not (setq gid (gnus-gethash ref idhashtb)))
4244               (progn
4245                 (gnus-sethash ref id idhashtb)
4246                 (gnus-sethash id threads thhashtb))
4247             (setq gthread (gnus-gethash gid thhashtb))
4248             (unless entered
4249               ;; We enter a dummy root into the thread, if we
4250               ;; haven't done that already.
4251               (unless (stringp (caar gthread))
4252                 (setcar gthread (list (mail-header-subject (caar gthread))
4253                                       (car gthread))))
4254               ;; We add this new gathered thread to this gathered
4255               ;; thread.
4256               (setcdr (car gthread)
4257                       (nconc (cdar gthread) (list (car threads)))))
4258             ;; Add it into the thread hash table.
4259             (gnus-sethash id gthread thhashtb)
4260             (setq entered t)
4261             ;; Remove it from the list of threads.
4262             (setcdr prev (cdr threads))
4263             (setq threads prev))))
4264       (setq prev threads)
4265       (setq threads (cdr threads)))
4266     result))
4267
4268 (defun gnus-sort-gathered-threads (threads)
4269   "Sort subthreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4270   (let ((result threads))
4271     (while threads
4272       (when (stringp (caar threads))
4273         (setcdr (car threads)
4274                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4275       (setq threads (cdr threads)))
4276     result))
4277
4278 (defun gnus-thread-loop-p (root thread)
4279   "Say whether ROOT is in THREAD."
4280   (let ((stack (list thread))
4281         (infloop 0)
4282         th)
4283     (while (setq thread (pop stack))
4284       (setq th (cdr thread))
4285       (while (and th
4286                   (not (eq (caar th) root)))
4287         (pop th))
4288       (if th
4289           ;; We have found a loop.
4290           (let (ref-dep)
4291             (setcdr thread (delq (car th) (cdr thread)))
4292             (if (boundp (setq ref-dep (intern "none"
4293                                               gnus-newsgroup-dependencies)))
4294                 (setcdr (symbol-value ref-dep)
4295                         (nconc (cdr (symbol-value ref-dep))
4296                                (list (car th))))
4297               (set ref-dep (list nil (car th))))
4298             (setq infloop 1
4299                   stack nil))
4300         ;; Push all the subthreads onto the stack.
4301         (push (cdr thread) stack)))
4302     infloop))
4303
4304 (defun gnus-make-threads ()
4305   "Go through the dependency hashtb and find the roots.  Return all threads."
4306   (let (threads)
4307     (while (catch 'infloop
4308              (mapatoms
4309               (lambda (refs)
4310                 ;; Deal with self-referencing References loops.
4311                 (when (and (car (symbol-value refs))
4312                            (not (zerop
4313                                  (apply
4314                                   '+
4315                                   (mapcar
4316                                    (lambda (thread)
4317                                      (gnus-thread-loop-p
4318                                       (car (symbol-value refs)) thread))
4319                                    (cdr (symbol-value refs)))))))
4320                   (setq threads nil)
4321                   (throw 'infloop t))
4322                 (unless (car (symbol-value refs))
4323                   ;; These threads do not refer back to any other
4324                   ;; articles, so they're roots.
4325                   (setq threads (append (cdr (symbol-value refs)) threads))))
4326               gnus-newsgroup-dependencies)))
4327     threads))
4328
4329 ;; Build the thread tree.
4330 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4331   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4332
4333 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4334 if it was already present.
4335
4336 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4337 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4338 Message-IDs will be renamed to a unique Message-ID before being
4339 entered.
4340
4341 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4342   (let* ((id (mail-header-id header))
4343          (id-dep (and id (intern id dependencies)))
4344          parent-id ref ref-dep ref-header replaced)
4345     ;; Enter this `header' in the `dependencies' table.
4346     (cond
4347      ((not id-dep)
4348       (setq header nil))
4349      ;; The first two cases do the normal part: enter a new `header'
4350      ;; in the `dependencies' table.
4351      ((not (boundp id-dep))
4352       (set id-dep (list header)))
4353      ((null (car (symbol-value id-dep)))
4354       (setcar (symbol-value id-dep) header))
4355
4356      ;; From here the `header' was already present in the
4357      ;; `dependencies' table.
4358      (force-new
4359       ;; Overrides an existing entry;
4360       ;; just set the header part of the entry.
4361       (setcar (symbol-value id-dep) header)
4362       (setq replaced t))
4363
4364      ;; Renames the existing `header' to a unique Message-ID.
4365      ((not gnus-summary-ignore-duplicates)
4366       ;; An article with this Message-ID has already been seen.
4367       ;; We rename the Message-ID.
4368       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4369            (list header))
4370       (mail-header-set-id header id))
4371
4372      ;; The last case ignores an existing entry, except it adds any
4373      ;; additional Xrefs (in case the two articles came from different
4374      ;; servers.
4375      ;; Also sets `header' to `nil' meaning that the `dependencies'
4376      ;; table was *not* modified.
4377      (t
4378       (mail-header-set-xref
4379        (car (symbol-value id-dep))
4380        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4381                    "")
4382                (or (mail-header-xref header) "")))
4383       (setq header nil)))
4384
4385     (when (and header (not replaced))
4386       ;; First check that we are not creating a References loop.
4387       (setq parent-id (gnus-parent-id (mail-header-references header)))
4388       (setq ref parent-id)
4389       (while (and ref
4390                   (setq ref-dep (intern-soft ref dependencies))
4391                   (boundp ref-dep)
4392                   (setq ref-header (car (symbol-value ref-dep))))
4393         (if (string= id ref)
4394             ;; Yuk!  This is a reference loop.  Make the article be a
4395             ;; root article.
4396             (progn
4397               (mail-header-set-references (car (symbol-value id-dep)) "none")
4398               (setq ref nil)
4399               (setq parent-id nil))
4400           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4401       (setq ref-dep (intern (or parent-id "none") dependencies))
4402       (if (boundp ref-dep)
4403           (setcdr (symbol-value ref-dep)
4404                   (nconc (cdr (symbol-value ref-dep))
4405                          (list (symbol-value id-dep))))
4406         (set ref-dep (list nil (symbol-value id-dep)))))
4407     header))
4408
4409 (defun gnus-extract-message-id-from-in-reply-to (string)
4410   (if (string-match "<[^>]+>" string)
4411       (substring string (match-beginning 0) (match-end 0))
4412     nil))
4413
4414 (defun gnus-build-sparse-threads ()
4415   (let ((headers gnus-newsgroup-headers)
4416         (mail-parse-charset gnus-newsgroup-charset)
4417         (gnus-summary-ignore-duplicates t)
4418         header references generation relations
4419         subject child end new-child date)
4420     ;; First we create an alist of generations/relations, where
4421     ;; generations is how much we trust the relation, and the relation
4422     ;; is parent/child.
4423     (gnus-message 7 "Making sparse threads...")
4424     (save-excursion
4425       (nnheader-set-temp-buffer " *gnus sparse threads*")
4426       (while (setq header (pop headers))
4427         (when (and (setq references (mail-header-references header))
4428                    (not (string= references "")))
4429           (insert references)
4430           (setq child (mail-header-id header)
4431                 subject (mail-header-subject header)
4432                 date (mail-header-date header)
4433                 generation 0)
4434           (while (search-backward ">" nil t)
4435             (setq end (1+ (point)))
4436             (when (search-backward "<" nil t)
4437               (setq new-child (buffer-substring (point) end))
4438               (push (list (incf generation)
4439                           child (setq child new-child)
4440                           subject date)
4441                     relations)))
4442           (when child
4443             (push (list (1+ generation) child nil subject) relations))
4444           (erase-buffer)))
4445       (kill-buffer (current-buffer)))
4446     ;; Sort over trustworthiness.
4447     (dolist (relation (sort relations 'car-less-than-car))
4448       (when (gnus-dependencies-add-header
4449              (make-full-mail-header
4450               gnus-reffed-article-number
4451               (nth 3 relation) "" (or (nth 4 relation) "")
4452               (nth 1 relation)
4453               (or (nth 2 relation) "") 0 0 "")
4454              gnus-newsgroup-dependencies nil)
4455         (push gnus-reffed-article-number gnus-newsgroup-limit)
4456         (push gnus-reffed-article-number gnus-newsgroup-sparse)
4457         (push (cons gnus-reffed-article-number gnus-sparse-mark)
4458               gnus-newsgroup-reads)
4459         (decf gnus-reffed-article-number)))
4460     (gnus-message 7 "Making sparse threads...done")))
4461
4462 (defun gnus-build-old-threads ()
4463   ;; Look at all the articles that refer back to old articles, and
4464   ;; fetch the headers for the articles that aren't there.  This will
4465   ;; build complete threads - if the roots haven't been expired by the
4466   ;; server, that is.
4467   (let ((mail-parse-charset gnus-newsgroup-charset)
4468         id heads)
4469     (mapatoms
4470      (lambda (refs)
4471        (when (not (car (symbol-value refs)))
4472          (setq heads (cdr (symbol-value refs)))
4473          (while heads
4474            (if (memq (mail-header-number (caar heads))
4475                      gnus-newsgroup-dormant)
4476                (setq heads (cdr heads))
4477              (setq id (symbol-name refs))
4478              (while (and (setq id (gnus-build-get-header id))
4479                          (not (car (gnus-id-to-thread id)))))
4480              (setq heads nil)))))
4481      gnus-newsgroup-dependencies)))
4482
4483 (defsubst gnus-remove-odd-characters (string)
4484   "Translate STRING into something that doesn't contain weird characters."
4485   (mm-subst-char-in-string
4486    ?\r ?\-
4487    (mm-subst-char-in-string ?\n ?\- string t) t))
4488
4489 ;; This function has to be called with point after the article number
4490 ;; on the beginning of the line.
4491 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4492   (let ((eol (point-at-eol))
4493         (buffer (current-buffer))
4494         header references in-reply-to)
4495
4496     ;; overview: [num subject from date id refs chars lines misc]
4497     (unwind-protect
4498         (let (x)
4499           (narrow-to-region (point) eol)
4500           (unless (eobp)
4501             (forward-char))
4502
4503           (setq header
4504                 (make-full-mail-header
4505                  number                 ; number
4506                  (condition-case ()     ; subject
4507                      (gnus-remove-odd-characters
4508                       (funcall gnus-decode-encoded-word-function
4509                                (setq x (nnheader-nov-field))))
4510                    (error x))
4511                  (condition-case ()     ; from
4512                      (gnus-remove-odd-characters
4513                       (funcall gnus-decode-encoded-address-function
4514                                (setq x (nnheader-nov-field))))
4515                    (error x))
4516                  (nnheader-nov-field)   ; date
4517                  (nnheader-nov-read-message-id number)  ; id
4518                  (setq references (nnheader-nov-field)) ; refs
4519                  (nnheader-nov-read-integer) ; chars
4520                  (nnheader-nov-read-integer) ; lines
4521                  (unless (eobp)
4522                    (if (looking-at "Xref: ")
4523                        (goto-char (match-end 0)))
4524                    (nnheader-nov-field)) ; Xref
4525                  (nnheader-nov-parse-extra)))) ; extra
4526
4527       (widen))
4528
4529     (when (and (string= references "")
4530                (setq in-reply-to (mail-header-extra header))
4531                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4532       (mail-header-set-references
4533        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4534
4535     (when gnus-alter-header-function
4536       (funcall gnus-alter-header-function header))
4537     (gnus-dependencies-add-header header dependencies force-new)))
4538
4539 (defun gnus-build-get-header (id)
4540   "Look through the buffer of NOV lines and find the header to ID.
4541 Enter this line into the dependencies hash table, and return
4542 the id of the parent article (if any)."
4543   (let ((deps gnus-newsgroup-dependencies)
4544         found header)
4545     (prog1
4546         (with-current-buffer nntp-server-buffer
4547           (let ((case-fold-search nil))
4548             (goto-char (point-min))
4549             (while (and (not found)
4550                         (search-forward id nil t))
4551               (beginning-of-line)
4552               (setq found (looking-at
4553                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4554                                    (regexp-quote id))))
4555               (or found (beginning-of-line 2)))
4556             (when found
4557               (beginning-of-line)
4558               (and
4559                (setq header (gnus-nov-parse-line
4560                              (read (current-buffer)) deps))
4561                (gnus-parent-id (mail-header-references header))))))
4562       (when header
4563         (let ((number (mail-header-number header)))
4564           (push number gnus-newsgroup-limit)
4565           (push header gnus-newsgroup-headers)
4566           (if (memq number gnus-newsgroup-unselected)
4567               (progn
4568                 (setq gnus-newsgroup-unreads
4569                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4570                                                number))
4571                 (setq gnus-newsgroup-unselected
4572                       (delq number gnus-newsgroup-unselected)))
4573             (push number gnus-newsgroup-ancient)))))))
4574
4575 (defun gnus-build-all-threads ()
4576   "Read all the headers."
4577   (let ((gnus-summary-ignore-duplicates t)
4578         (mail-parse-charset gnus-newsgroup-charset)
4579         (dependencies gnus-newsgroup-dependencies)
4580         header article)
4581     (with-current-buffer nntp-server-buffer
4582       (let ((case-fold-search nil))
4583         (goto-char (point-min))
4584         (while (not (eobp))
4585           (ignore-errors
4586             (setq article (read (current-buffer))
4587                   header (gnus-nov-parse-line article dependencies t)))
4588           (when header
4589             (with-current-buffer gnus-summary-buffer
4590               (push header gnus-newsgroup-headers)
4591               (if (memq (setq article (mail-header-number header))
4592                         gnus-newsgroup-unselected)
4593                   (progn
4594                     (setq gnus-newsgroup-unreads
4595                           (gnus-add-to-sorted-list
4596                            gnus-newsgroup-unreads article))
4597                     (setq gnus-newsgroup-unselected
4598                           (delq article gnus-newsgroup-unselected)))
4599                 (push article gnus-newsgroup-ancient)))
4600             (forward-line 1)))))))
4601
4602 (defun gnus-summary-update-article-line (article header)
4603   "Update the line for ARTICLE using HEADER."
4604   (let* ((id (mail-header-id header))
4605          (thread (gnus-id-to-thread id)))
4606     (unless thread
4607       (error "Article in no thread"))
4608     ;; Update the thread.
4609     (setcar thread header)
4610     (gnus-summary-goto-subject article)
4611     (let* ((datal (gnus-data-find-list article))
4612            (data (car datal))
4613            (inhibit-read-only t)
4614            (level (gnus-summary-thread-level)))
4615       (gnus-delete-line)
4616       (let ((inserted (- (point)
4617                          (progn
4618                            (gnus-summary-insert-line
4619                             header level nil
4620                             (memq article gnus-newsgroup-undownloaded)
4621                             (gnus-article-mark article)
4622                             (memq article gnus-newsgroup-replied)
4623                             (memq article gnus-newsgroup-expirable)
4624                             ;; Only insert the Subject string when it's different
4625                             ;; from the previous Subject string.
4626                             (if (and
4627                                  gnus-show-threads
4628                                  (gnus-subject-equal
4629                                   (condition-case ()
4630                                       (mail-header-subject
4631                                        (gnus-data-header
4632                                         (cadr
4633                                          (gnus-data-find-list
4634                                           article
4635                                           (gnus-data-list t)))))
4636                                     ;; Error on the side of excessive subjects.
4637                                     (error ""))
4638                                   (mail-header-subject header)))
4639                                 ""
4640                               (mail-header-subject header))
4641                             nil (cdr (assq article gnus-newsgroup-scored))
4642                             (memq article gnus-newsgroup-processable))
4643                            (point)))))
4644         (when (cdr datal)
4645           (gnus-data-update-list
4646            (cdr datal)
4647            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4648
4649 (defun gnus-summary-update-article (article &optional iheader)
4650   "Update ARTICLE in the summary buffer."
4651   (set-buffer gnus-summary-buffer)
4652   (let* ((header (gnus-summary-article-header article))
4653          (id (mail-header-id header))
4654          (data (gnus-data-find article))
4655          (thread (gnus-id-to-thread id))
4656          (references (mail-header-references header))
4657          (parent
4658           (gnus-id-to-thread
4659            (or (gnus-parent-id
4660                 (when (and references
4661                            (not (equal "" references)))
4662                   references))
4663                "none")))
4664          (inhibit-read-only t)
4665          (old (car thread)))
4666     (when thread
4667       (unless iheader
4668         (setcar thread nil)
4669         (when parent
4670           (delq thread parent)))
4671       (if (gnus-summary-insert-subject id header)
4672           ;; Set the (possibly) new article number in the data structure.
4673           (gnus-data-set-number data (gnus-id-to-article id))
4674         (setcar thread old)
4675         nil))))
4676
4677 (defun gnus-rebuild-thread (id &optional line)
4678   "Rebuild the thread containing ID.
4679 If LINE, insert the rebuilt thread starting on line LINE."
4680   (let ((inhibit-read-only t)
4681         old-pos current thread data)
4682     (if (not gnus-show-threads)
4683         (setq thread (list (car (gnus-id-to-thread id))))
4684       ;; Get the thread this article is part of.
4685       (setq thread (gnus-remove-thread id)))
4686     (setq old-pos (point-at-bol))
4687     (setq current (save-excursion
4688                     (and (re-search-backward "[\r\n]" nil t)
4689                          (gnus-summary-article-number))))
4690     ;; If this is a gathered thread, we have to go some re-gathering.
4691     (when (stringp (car thread))
4692       (let ((subject (car thread))
4693             roots thr)
4694         (setq thread (cdr thread))
4695         (while thread
4696           (unless (memq (setq thr (gnus-id-to-thread
4697                                    (gnus-root-id
4698                                     (mail-header-id (caar thread)))))
4699                         roots)
4700             (push thr roots))
4701           (setq thread (cdr thread)))
4702         ;; We now have all (unique) roots.
4703         (if (= (length roots) 1)
4704             ;; All the loose roots are now one solid root.
4705             (setq thread (car roots))
4706           (setq thread (cons subject (gnus-sort-threads roots))))))
4707     (let (threads)
4708       ;; We then insert this thread into the summary buffer.
4709       (when line
4710         (goto-char (point-min))
4711         (forward-line (1- line)))
4712       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4713         (if gnus-show-threads
4714             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4715           (gnus-summary-prepare-unthreaded thread))
4716         (setq data (nreverse gnus-newsgroup-data))
4717         (setq threads gnus-newsgroup-threads))
4718       ;; We splice the new data into the data structure.
4719       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4720       ;;!!! then we want to insert at the beginning of the buffer.
4721       ;;!!! That happens to be true with Gnus now, but that may
4722       ;;!!! change in the future.  Perhaps.
4723       (gnus-data-enter-list
4724        (if line nil current) data (- (point) old-pos))
4725       (setq gnus-newsgroup-threads
4726             (nconc threads gnus-newsgroup-threads))
4727       (gnus-data-compute-positions))))
4728
4729 (defun gnus-number-to-header (number)
4730   "Return the header for article NUMBER."
4731   (let ((headers gnus-newsgroup-headers))
4732     (while (and headers
4733                 (not (= number (mail-header-number (car headers)))))
4734       (pop headers))
4735     (when headers
4736       (car headers))))
4737
4738 (defun gnus-parent-headers (in-headers &optional generation)
4739   "Return the headers of the GENERATIONth parent of HEADERS."
4740   (unless generation
4741     (setq generation 1))
4742   (let ((parent t)
4743         (headers in-headers)
4744         references)
4745     (while (and parent
4746                 (not (zerop generation))
4747                 (setq references (mail-header-references headers)))
4748       (setq headers (if (and references
4749                              (setq parent (gnus-parent-id references)))
4750                         (car (gnus-id-to-thread parent))
4751                       nil))
4752       (decf generation))
4753     (and (not (eq headers in-headers))
4754          headers)))
4755
4756 (defun gnus-id-to-thread (id)
4757   "Return the (sub-)thread where ID appears."
4758   (gnus-gethash id gnus-newsgroup-dependencies))
4759
4760 (defun gnus-id-to-article (id)
4761   "Return the article number of ID."
4762   (let ((thread (gnus-id-to-thread id)))
4763     (when (and thread
4764                (car thread))
4765       (mail-header-number (car thread)))))
4766
4767 (defun gnus-id-to-header (id)
4768   "Return the article headers of ID."
4769   (car (gnus-id-to-thread id)))
4770
4771 (defun gnus-article-displayed-root-p (article)
4772   "Say whether ARTICLE is a root(ish) article."
4773   (let ((level (gnus-summary-thread-level article))
4774         (refs (mail-header-references  (gnus-summary-article-header article)))
4775         particle)
4776     (cond
4777      ((null level) nil)
4778      ((zerop level) t)
4779      ((null refs) t)
4780      ((null (gnus-parent-id refs)) t)
4781      ((and (= 1 level)
4782            (null (setq particle (gnus-id-to-article
4783                                  (gnus-parent-id refs))))
4784            (null (gnus-summary-thread-level particle)))))))
4785
4786 (defun gnus-root-id (id)
4787   "Return the id of the root of the thread where ID appears."
4788   (let (last-id prev)
4789     (while (and id (setq prev (car (gnus-id-to-thread id))))
4790       (setq last-id id
4791             id (gnus-parent-id (mail-header-references prev))))
4792     last-id))
4793
4794 (defun gnus-articles-in-thread (thread)
4795   "Return the list of articles in THREAD."
4796   (cons (mail-header-number (car thread))
4797         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4798
4799 (defun gnus-remove-thread (id &optional dont-remove)
4800   "Remove the thread that has ID in it."
4801   (let (headers thread last-id)
4802     ;; First go up in this thread until we find the root.
4803     (setq last-id (gnus-root-id id)
4804           headers (message-flatten-list (gnus-id-to-thread last-id)))
4805     ;; We have now found the real root of this thread.  It might have
4806     ;; been gathered into some loose thread, so we have to search
4807     ;; through the threads to find the thread we wanted.
4808     (let ((threads gnus-newsgroup-threads)
4809           sub)
4810       (while threads
4811         (setq sub (car threads))
4812         (if (stringp (car sub))
4813             ;; This is a gathered thread, so we look at the roots
4814             ;; below it to find whether this article is in this
4815             ;; gathered root.
4816             (progn
4817               (setq sub (cdr sub))
4818               (while sub
4819                 (when (member (caar sub) headers)
4820                   (setq thread (car threads)
4821                         threads nil
4822                         sub nil))
4823                 (setq sub (cdr sub))))
4824           ;; It's an ordinary thread, so we check it.
4825           (when (eq (car sub) (car headers))
4826             (setq thread sub
4827                   threads nil)))
4828         (setq threads (cdr threads)))
4829       ;; If this article is in no thread, then it's a root.
4830       (if thread
4831           (unless dont-remove
4832             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4833         (setq thread (gnus-id-to-thread last-id)))
4834       (when thread
4835         (prog1
4836             thread                      ; We return this thread.
4837           (unless dont-remove
4838             (if (stringp (car thread))
4839                 (progn
4840                   ;; If we use dummy roots, then we have to remove the
4841                   ;; dummy root as well.
4842                   (when (eq gnus-summary-make-false-root 'dummy)
4843                     ;; We go to the dummy root by going to
4844                     ;; the first sub-"thread", and then one line up.
4845                     (gnus-summary-goto-article
4846                      (mail-header-number (caadr thread)))
4847                     (forward-line -1)
4848                     (gnus-delete-line)
4849                     (gnus-data-compute-positions))
4850                   (setq thread (cdr thread))
4851                   (while thread
4852                     (gnus-remove-thread-1 (car thread))
4853                     (setq thread (cdr thread))))
4854               (gnus-remove-thread-1 thread))))))))
4855
4856 (defun gnus-remove-thread-1 (thread)
4857   "Remove the thread THREAD recursively."
4858   (let ((number (mail-header-number (pop thread)))
4859         d)
4860     (setq thread (reverse thread))
4861     (while thread
4862       (gnus-remove-thread-1 (pop thread)))
4863     (when (setq d (gnus-data-find number))
4864       (goto-char (gnus-data-pos d))
4865       (gnus-summary-show-thread)
4866       (gnus-data-remove
4867        number
4868        (- (point-at-bol)
4869           (prog1
4870               (1+ (point-at-eol))
4871             (gnus-delete-line)))))))
4872
4873 (defun gnus-sort-threads-recursive (threads func)
4874   ;; Responsible for sorting the root articles of threads.
4875   (let ((subthread-sort-func (if (eq gnus-subthread-sort-functions
4876                                      'gnus-thread-sort-functions)
4877                                  func
4878                                (gnus-make-sort-function
4879                                 gnus-subthread-sort-functions))))
4880     (sort (mapcar (lambda (thread)
4881                     (cons (car thread)
4882                           (and (cdr thread)
4883                                (gnus-sort-subthreads-recursive
4884                                 (cdr thread) subthread-sort-func))))
4885                   threads) func)))
4886
4887 (defun gnus-sort-subthreads-recursive (threads func)
4888   ;; Responsible for sorting subthreads.
4889   (sort (mapcar (lambda (thread)
4890                   (cons (car thread)
4891                         (and (cdr thread)
4892                              (gnus-sort-subthreads-recursive (cdr thread) func))))
4893                 threads) func))
4894
4895 (defun gnus-sort-threads-loop (threads func)
4896   (let* ((superthread (cons nil threads))
4897          (stack (list (cons superthread threads)))
4898          remaining-threads thread)
4899     (while stack
4900       (setq remaining-threads (cdr (car stack)))
4901       (if remaining-threads
4902           (progn (setq thread (car remaining-threads))
4903                  (setcdr (car stack) (cdr remaining-threads))
4904                  (if (cdr thread)
4905                      (push (cons thread (cdr thread)) stack)))
4906         (setq thread (caar stack))
4907         (setcdr thread (sort (cdr thread) func))
4908         (pop stack)))
4909     (cdr superthread)))
4910
4911 (defun gnus-sort-threads (threads)
4912   "Sort THREADS."
4913   (if (not gnus-thread-sort-functions)
4914       threads
4915     (gnus-message 8 "Sorting threads...")
4916     (prog1
4917         (condition-case nil
4918             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000))
4919                   (sort-func (gnus-make-sort-function gnus-thread-sort-functions)))
4920               (gnus-sort-threads-recursive threads sort-func))
4921           ;; Even after binding max-lisp-eval-depth, the recursive
4922           ;; sorter might fail for very long threads.  In that case,
4923           ;; try using a (less well-tested) non-recursive sorter.
4924           (error (gnus-message 9 "Sorting threads with loop...")
4925                  (gnus-sort-threads-loop
4926                   threads (gnus-make-sort-function
4927                            gnus-thread-sort-functions))))
4928       (gnus-message 8 "Sorting threads...done"))))
4929
4930 (defun gnus-sort-articles (articles)
4931   "Sort ARTICLES."
4932   (when gnus-article-sort-functions
4933     (gnus-message 7 "Sorting articles...")
4934     (prog1
4935         (setq gnus-newsgroup-headers
4936               (sort articles (gnus-make-sort-function
4937                               gnus-article-sort-functions)))
4938       (gnus-message 7 "Sorting articles...done"))))
4939
4940 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4941 (defmacro gnus-thread-header (thread)
4942   "Return header of first article in THREAD.
4943 Note that THREAD must never, ever be anything else than a variable -
4944 using some other form will lead to serious barfage."
4945   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4946   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4947   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4948         (vector thread) 2))
4949
4950 (defsubst gnus-article-sort-by-number (h1 h2)
4951   "Sort articles by article number."
4952   (< (mail-header-number h1)
4953      (mail-header-number h2)))
4954
4955 (defun gnus-thread-sort-by-number (h1 h2)
4956   "Sort threads by root article number."
4957   (gnus-article-sort-by-number
4958    (gnus-thread-header h1) (gnus-thread-header h2)))
4959
4960 (defsubst gnus-article-sort-by-random (h1 h2)
4961   "Sort articles randomly."
4962   (zerop (random 2)))
4963
4964 (defun gnus-thread-sort-by-random (h1 h2)
4965   "Sort threads randomly."
4966   (gnus-article-sort-by-random
4967    (gnus-thread-header h1) (gnus-thread-header h2)))
4968
4969 (defsubst gnus-article-sort-by-lines (h1 h2)
4970   "Sort articles by article Lines header."
4971   (< (mail-header-lines h1)
4972      (mail-header-lines h2)))
4973
4974 (defun gnus-thread-sort-by-lines (h1 h2)
4975   "Sort threads by root article Lines header."
4976   (gnus-article-sort-by-lines
4977    (gnus-thread-header h1) (gnus-thread-header h2)))
4978
4979 (defsubst gnus-article-sort-by-chars (h1 h2)
4980   "Sort articles by octet length."
4981   (< (mail-header-chars h1)
4982      (mail-header-chars h2)))
4983
4984 (defun gnus-thread-sort-by-chars (h1 h2)
4985   "Sort threads by root article octet length."
4986   (gnus-article-sort-by-chars
4987    (gnus-thread-header h1) (gnus-thread-header h2)))
4988
4989 (defsubst gnus-article-sort-by-author (h1 h2)
4990   "Sort articles by root author."
4991   (gnus-string<
4992    (let ((extract (funcall
4993                    gnus-extract-address-components
4994                    (mail-header-from h1))))
4995      (or (car extract) (cadr extract) ""))
4996    (let ((extract (funcall
4997                    gnus-extract-address-components
4998                    (mail-header-from h2))))
4999      (or (car extract) (cadr extract) ""))))
5000
5001 (defun gnus-thread-sort-by-author (h1 h2)
5002   "Sort threads by root author."
5003   (gnus-article-sort-by-author
5004    (gnus-thread-header h1)  (gnus-thread-header h2)))
5005
5006 (defsubst gnus-article-sort-by-recipient (h1 h2)
5007   "Sort articles by recipient."
5008   (gnus-string<
5009    (let ((extract (funcall
5010                    gnus-extract-address-components
5011                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
5012      (or (car extract) (cadr extract)))
5013    (let ((extract (funcall
5014                    gnus-extract-address-components
5015                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
5016      (or (car extract) (cadr extract)))))
5017
5018 (defun gnus-thread-sort-by-recipient (h1 h2)
5019   "Sort threads by root recipient."
5020   (gnus-article-sort-by-recipient
5021    (gnus-thread-header h1) (gnus-thread-header h2)))
5022
5023 (defsubst gnus-article-sort-by-subject (h1 h2)
5024   "Sort articles by root subject."
5025   (gnus-string<
5026    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
5027    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
5028
5029 (defun gnus-thread-sort-by-subject (h1 h2)
5030   "Sort threads by root subject."
5031   (gnus-article-sort-by-subject
5032    (gnus-thread-header h1) (gnus-thread-header h2)))
5033
5034 (defsubst gnus-article-sort-by-date (h1 h2)
5035   "Sort articles by root article date."
5036   (time-less-p
5037    (gnus-date-get-time (mail-header-date h1))
5038    (gnus-date-get-time (mail-header-date h2))))
5039
5040 (defun gnus-thread-sort-by-date (h1 h2)
5041   "Sort threads by root article date."
5042   (gnus-article-sort-by-date
5043    (gnus-thread-header h1) (gnus-thread-header h2)))
5044
5045 (defsubst gnus-article-sort-by-score (h1 h2)
5046   "Sort articles by root article score.
5047 Unscored articles will be counted as having a score of zero."
5048   (> (or (cdr (assq (mail-header-number h1)
5049                     gnus-newsgroup-scored))
5050          gnus-summary-default-score 0)
5051      (or (cdr (assq (mail-header-number h2)
5052                     gnus-newsgroup-scored))
5053          gnus-summary-default-score 0)))
5054
5055 (defun gnus-thread-sort-by-score (h1 h2)
5056   "Sort threads by root article score."
5057   (gnus-article-sort-by-score
5058    (gnus-thread-header h1) (gnus-thread-header h2)))
5059
5060 (defun gnus-thread-sort-by-total-score (h1 h2)
5061   "Sort threads by the sum of all scores in the thread.
5062 Unscored articles will be counted as having a score of zero."
5063   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
5064
5065 (defun gnus-thread-total-score (thread)
5066   ;; This function find the total score of THREAD.
5067   (cond
5068    ((null thread)
5069     0)
5070    ((consp thread)
5071     (if (stringp (car thread))
5072         (apply gnus-thread-score-function 0
5073                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
5074       (gnus-thread-total-score-1 thread)))
5075    (t
5076     (gnus-thread-total-score-1 (list thread)))))
5077
5078 (defun gnus-article-sort-by-most-recent-number (h1 h2)
5079   "Sort articles by number."
5080   (gnus-article-sort-by-number h1 h2))
5081
5082 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
5083   "Sort threads such that the thread with the most recently arrived article comes first."
5084   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
5085
5086 (defun gnus-thread-highest-number (thread)
5087   "Return the highest article number in THREAD."
5088   (apply 'max (mapcar (lambda (header)
5089                         (mail-header-number header))
5090                       (message-flatten-list thread))))
5091
5092 (defun gnus-article-sort-by-most-recent-date (h1 h2)
5093   "Sort articles by number."
5094   (gnus-article-sort-by-date h1 h2))
5095
5096 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
5097   "Sort threads such that the thread with the most recently dated article comes first."
5098   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
5099
5100 ; Since this is called not only to sort the top-level threads, but
5101 ; also in recursive sorts to order the articles within a thread, each
5102 ; article will be processed many times.  Thus it speeds things up
5103 ; quite a bit to use gnus-date-get-time, which caches the time value.
5104 (defun gnus-thread-latest-date (thread)
5105   "Return the highest article date in THREAD."
5106   (apply 'max
5107          (mapcar (lambda (header) (gnus-float-time
5108                                    (gnus-date-get-time
5109                                     (mail-header-date header))))
5110                  (message-flatten-list thread))))
5111
5112 (defun gnus-thread-total-score-1 (root)
5113   ;; This function find the total score of the thread below ROOT.
5114   (setq root (car root))
5115   (apply gnus-thread-score-function
5116          (or (append
5117               (mapcar 'gnus-thread-total-score
5118                       (cdr (gnus-id-to-thread (mail-header-id root))))
5119               (when (> (mail-header-number root) 0)
5120                 (list (or (cdr (assq (mail-header-number root)
5121                                      gnus-newsgroup-scored))
5122                           gnus-summary-default-score 0))))
5123              (list gnus-summary-default-score)
5124              '(0))))
5125
5126 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
5127 (defvar gnus-tmp-prev-subject nil)
5128 (defvar gnus-tmp-false-parent nil)
5129 (defvar gnus-tmp-root-expunged nil)
5130 (defvar gnus-tmp-dummy-line nil)
5131
5132 (defun gnus-extra-header (type &optional header)
5133   "Return the extra header of TYPE."
5134   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
5135       ""))
5136
5137 (defvar gnus-tmp-thread-tree-header-string "")
5138
5139 (defcustom gnus-sum-thread-tree-root "> "
5140   "With %B spec, used for the root of a thread.
5141 If nil, use subject instead."
5142   :version "22.1"
5143   :type '(radio (const :format "%v  " nil) string)
5144   :group 'gnus-thread)
5145
5146 (defcustom gnus-sum-thread-tree-false-root "> "
5147   "With %B spec, used for a false root of a thread.
5148 If nil, use subject instead."
5149   :version "22.1"
5150   :type '(radio (const :format "%v  " nil) string)
5151   :group 'gnus-thread)
5152
5153 (defcustom gnus-sum-thread-tree-single-indent ""
5154   "With %B spec, used for a thread with just one message.
5155 If nil, use subject instead."
5156   :version "22.1"
5157   :type '(radio (const :format "%v  " nil) string)
5158   :group 'gnus-thread)
5159
5160 (defcustom gnus-sum-thread-tree-vertical "| "
5161   "With %B spec, used for drawing a vertical line."
5162   :version "22.1"
5163   :type 'string
5164   :group 'gnus-thread)
5165
5166 (defcustom gnus-sum-thread-tree-indent "  "
5167   "With %B spec, used for indenting."
5168   :version "22.1"
5169   :type 'string
5170   :group 'gnus-thread)
5171
5172 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5173   "With %B spec, used for a leaf with brothers."
5174   :version "22.1"
5175   :type 'string
5176   :group 'gnus-thread)
5177
5178 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5179   "With %B spec, used for a leaf without brothers."
5180   :version "22.1"
5181   :type 'string
5182   :group 'gnus-thread)
5183
5184 (defcustom gnus-summary-display-while-building nil
5185   "If non-nil, show and update the summary buffer as it's being built.
5186 If the value is t, update the buffer after every line is inserted.  If
5187 the value is an integer (N), update the display every N lines."
5188   :version "22.1"
5189   :group 'gnus-thread
5190   :type '(choice (const :tag "off" nil)
5191                  number
5192                  (const :tag "frequently" t)))
5193
5194 (defun gnus-summary-prepare-threads (threads)
5195   "Prepare summary buffer from THREADS and indentation LEVEL.
5196 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5197 or a straight list of headers."
5198   (gnus-message 7 "Generating summary...")
5199
5200   (setq gnus-newsgroup-threads threads)
5201   (beginning-of-line)
5202
5203   (let ((gnus-tmp-level 0)
5204         (default-score (or gnus-summary-default-score 0))
5205         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5206         (building-line-count gnus-summary-display-while-building)
5207         (building-count (integerp gnus-summary-display-while-building))
5208         thread number subject stack state gnus-tmp-gathered beg-match
5209         new-roots gnus-tmp-new-adopts thread-end simp-subject
5210         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5211         gnus-tmp-replied gnus-tmp-subject-or-nil
5212         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5213         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5214         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5215         tree-stack)
5216
5217     (setq gnus-tmp-prev-subject nil
5218           gnus-tmp-thread-tree-header-string "")
5219
5220     (if (vectorp (car threads))
5221         ;; If this is a straight (sic) list of headers, then a
5222         ;; threaded summary display isn't required, so we just create
5223         ;; an unthreaded one.
5224         (gnus-summary-prepare-unthreaded threads)
5225
5226       ;; Do the threaded display.
5227
5228       (if gnus-summary-display-while-building
5229           (switch-to-buffer (buffer-name)))
5230       (while (or threads stack gnus-tmp-new-adopts new-roots)
5231
5232         (if (and (= gnus-tmp-level 0)
5233                  (or (not stack)
5234                      (= (caar stack) 0))
5235                  (not gnus-tmp-false-parent)
5236                  (or gnus-tmp-new-adopts new-roots))
5237             (if gnus-tmp-new-adopts
5238                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5239                       thread (list (car gnus-tmp-new-adopts))
5240                       gnus-tmp-header (caar thread)
5241                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5242               (when new-roots
5243                 (setq thread (list (car new-roots))
5244                       gnus-tmp-header (caar thread)
5245                       new-roots (cdr new-roots))))
5246
5247           (if threads
5248               ;; If there are some threads, we do them before the
5249               ;; threads on the stack.
5250               (setq thread threads
5251                     gnus-tmp-header (caar thread))
5252             ;; There were no current threads, so we pop something off
5253             ;; the stack.
5254             (setq state (car stack)
5255                   gnus-tmp-level (car state)
5256                   tree-stack (cadr state)
5257                   thread (caddr state)
5258                   stack (cdr stack)
5259                   gnus-tmp-header (caar thread))))
5260
5261         (setq gnus-tmp-false-parent nil)
5262         (setq gnus-tmp-root-expunged nil)
5263         (setq thread-end nil)
5264
5265         (if (stringp gnus-tmp-header)
5266             ;; The header is a dummy root.
5267             (cond
5268              ((eq gnus-summary-make-false-root 'adopt)
5269               ;; We let the first article adopt the rest.
5270               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5271                                                (cddar thread)))
5272               (setq gnus-tmp-gathered
5273                     (nconc (mapcar
5274                             (lambda (h) (mail-header-number (car h)))
5275                             (cddar thread))
5276                            gnus-tmp-gathered))
5277               (setq thread (cons (list (caar thread)
5278                                        (cadar thread))
5279                                  (cdr thread)))
5280               (setq gnus-tmp-level -1
5281                     gnus-tmp-false-parent t))
5282              ((eq gnus-summary-make-false-root 'empty)
5283               ;; We print adopted articles with empty subject fields.
5284               (setq gnus-tmp-gathered
5285                     (nconc (mapcar
5286                             (lambda (h) (mail-header-number (car h)))
5287                             (cddar thread))
5288                            gnus-tmp-gathered))
5289               (setq gnus-tmp-level -1))
5290              ((eq gnus-summary-make-false-root 'dummy)
5291               ;; We remember that we probably want to output a dummy
5292               ;; root.
5293               (setq gnus-tmp-dummy-line gnus-tmp-header)
5294               (setq gnus-tmp-prev-subject gnus-tmp-header))
5295              (t
5296               ;; We do not make a root for the gathered
5297               ;; sub-threads at all.
5298               (setq gnus-tmp-level -1)))
5299
5300           (setq number (mail-header-number gnus-tmp-header)
5301                 subject (mail-header-subject gnus-tmp-header)
5302                 simp-subject (gnus-simplify-subject-fully subject))
5303
5304           (cond
5305            ;; If the thread has changed subject, we might want to make
5306            ;; this subthread into a root.
5307            ((and (null gnus-thread-ignore-subject)
5308                  (not (zerop gnus-tmp-level))
5309                  gnus-tmp-prev-subject
5310                  (not (string= gnus-tmp-prev-subject simp-subject)))
5311             (setq new-roots (nconc new-roots (list (car thread)))
5312                   thread-end t
5313                   gnus-tmp-header nil))
5314            ;; If the article lies outside the current limit,
5315            ;; then we do not display it.
5316            ((not (memq number gnus-newsgroup-limit))
5317             (setq gnus-tmp-gathered
5318                   (nconc (mapcar
5319                           (lambda (h) (mail-header-number (car h)))
5320                           (cdar thread))
5321                          gnus-tmp-gathered))
5322             (setq gnus-tmp-new-adopts (if (cdar thread)
5323                                           (append gnus-tmp-new-adopts
5324                                                   (cdar thread))
5325                                         gnus-tmp-new-adopts)
5326                   thread-end t
5327                   gnus-tmp-header nil)
5328             (when (zerop gnus-tmp-level)
5329               (setq gnus-tmp-root-expunged t)))
5330            ;; Perhaps this article is to be marked as read?
5331            ((and gnus-summary-mark-below
5332                  (< (or (cdr (assq number gnus-newsgroup-scored))
5333                         default-score)
5334                     gnus-summary-mark-below)
5335                  ;; Don't touch sparse articles.
5336                  (not (gnus-summary-article-sparse-p number))
5337                  (not (gnus-summary-article-ancient-p number)))
5338             (setq gnus-newsgroup-unreads
5339                   (delq number gnus-newsgroup-unreads))
5340             (if gnus-newsgroup-auto-expire
5341                 (setq gnus-newsgroup-expirable
5342                       (gnus-add-to-sorted-list
5343                        gnus-newsgroup-expirable number))
5344               (push (cons number gnus-low-score-mark)
5345                     gnus-newsgroup-reads))))
5346
5347           (when gnus-tmp-header
5348             ;; We may have an old dummy line to output before this
5349             ;; article.
5350             (when (and gnus-tmp-dummy-line
5351                        (gnus-subject-equal
5352                         gnus-tmp-dummy-line
5353                         (mail-header-subject gnus-tmp-header)))
5354               (gnus-summary-insert-dummy-line
5355                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5356               (setq gnus-tmp-dummy-line nil))
5357
5358             ;; Compute the mark.
5359             (setq gnus-tmp-unread (gnus-article-mark number))
5360
5361             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5362                                   gnus-tmp-header gnus-tmp-level)
5363                   gnus-newsgroup-data)
5364
5365             ;; Actually insert the line.
5366             (setq
5367              gnus-tmp-subject-or-nil
5368              (cond
5369               ((and gnus-thread-ignore-subject
5370                     gnus-tmp-prev-subject
5371                     (not (string= gnus-tmp-prev-subject simp-subject)))
5372                subject)
5373               ((zerop gnus-tmp-level)
5374                (if (and (eq gnus-summary-make-false-root 'empty)
5375                         (memq number gnus-tmp-gathered)
5376                         gnus-tmp-prev-subject
5377                         (string= gnus-tmp-prev-subject simp-subject))
5378                    gnus-summary-same-subject
5379                  subject))
5380               (t gnus-summary-same-subject)))
5381             (if (and (eq gnus-summary-make-false-root 'adopt)
5382                      (= gnus-tmp-level 1)
5383                      (memq number gnus-tmp-gathered))
5384                 (setq gnus-tmp-opening-bracket ?\<
5385                       gnus-tmp-closing-bracket ?\>)
5386               (setq gnus-tmp-opening-bracket ?\[
5387                     gnus-tmp-closing-bracket ?\]))
5388             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5389                 (gnus-make-thread-indent-array
5390                  (max (* 2 (length gnus-thread-indent-array))
5391                       gnus-tmp-level)))
5392             (setq
5393              gnus-tmp-indentation
5394              (aref gnus-thread-indent-array gnus-tmp-level)
5395              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5396              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5397                                 gnus-summary-default-score 0)
5398              gnus-tmp-score-char
5399              (if (or (null gnus-summary-default-score)
5400                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5401                          gnus-summary-zcore-fuzz))
5402                  ?                      ;Whitespace
5403                (if (< gnus-tmp-score gnus-summary-default-score)
5404                    gnus-score-below-mark gnus-score-over-mark))
5405              gnus-tmp-replied
5406              (cond ((memq number gnus-newsgroup-processable)
5407                     gnus-process-mark)
5408                    ((memq number gnus-newsgroup-cached)
5409                     gnus-cached-mark)
5410                    ((memq number gnus-newsgroup-replied)
5411                     gnus-replied-mark)
5412                    ((memq number gnus-newsgroup-forwarded)
5413                     gnus-forwarded-mark)
5414                    ((memq number gnus-newsgroup-saved)
5415                     gnus-saved-mark)
5416                    ((memq number gnus-newsgroup-unseen)
5417                     gnus-unseen-mark)
5418                    (t gnus-no-mark))
5419              gnus-tmp-downloaded
5420              (cond ((memq number gnus-newsgroup-undownloaded)
5421                     gnus-undownloaded-mark)
5422                    (gnus-newsgroup-agentized
5423                     gnus-downloaded-mark)
5424                    (t
5425                     gnus-no-mark))
5426              gnus-tmp-from (mail-header-from gnus-tmp-header)
5427              gnus-tmp-name
5428              (cond
5429               ((string-match "<[^>]+> *$" gnus-tmp-from)
5430                (setq beg-match (match-beginning 0))
5431                (or (and (string-match "^\".+\"" gnus-tmp-from)
5432                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5433                    (substring gnus-tmp-from 0 beg-match)))
5434               ((string-match "(.+)" gnus-tmp-from)
5435                (substring gnus-tmp-from
5436                           (1+ (match-beginning 0)) (1- (match-end 0))))
5437               (t gnus-tmp-from))
5438
5439              ;; Do the %B string
5440              gnus-tmp-thread-tree-header-string
5441              (cond
5442               ((not gnus-show-threads) "")
5443               ((zerop gnus-tmp-level)
5444                (cond ((cdar thread)
5445                       (or gnus-sum-thread-tree-root subject))
5446                      (gnus-tmp-new-adopts
5447                       (or gnus-sum-thread-tree-false-root subject))
5448                      (t
5449                       (or gnus-sum-thread-tree-single-indent subject))))
5450               (t
5451                (concat (apply 'concat
5452                               (mapcar (lambda (item)
5453                                         (if (= item 1)
5454                                             gnus-sum-thread-tree-vertical
5455                                           gnus-sum-thread-tree-indent))
5456                                       (cdr (reverse tree-stack))))
5457                        (if (nth 1 thread)
5458                            gnus-sum-thread-tree-leaf-with-other
5459                          gnus-sum-thread-tree-single-leaf)))))
5460             (when (string= gnus-tmp-name "")
5461               (setq gnus-tmp-name gnus-tmp-from))
5462             (unless (numberp gnus-tmp-lines)
5463               (setq gnus-tmp-lines -1))
5464             (if (= gnus-tmp-lines -1)
5465                 (setq gnus-tmp-lines "?")
5466               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5467             (gnus-put-text-property
5468              (point)
5469              (progn (eval gnus-summary-line-format-spec) (point))
5470              'gnus-number number)
5471             (when gnus-visual-p
5472               (forward-line -1)
5473               (gnus-summary-highlight-line)
5474               (when gnus-summary-update-hook
5475                 (gnus-run-hooks 'gnus-summary-update-hook))
5476               (forward-line 1))
5477
5478             (setq gnus-tmp-prev-subject simp-subject)))
5479
5480         (when (nth 1 thread)
5481           (push (list (max 0 gnus-tmp-level)
5482                       (copy-sequence tree-stack)
5483                       (nthcdr 1 thread))
5484                 stack))
5485         (push (if (nth 1 thread) 1 0) tree-stack)
5486         (incf gnus-tmp-level)
5487         (setq threads (if thread-end nil (cdar thread)))
5488         (if gnus-summary-display-while-building
5489             (if building-count
5490                 (progn
5491                   ;; use a set frequency
5492                   (setq building-line-count (1- building-line-count))
5493                   (when (= building-line-count 0)
5494                     (sit-for 0)
5495                     (setq building-line-count
5496                           gnus-summary-display-while-building)))
5497               ;; always
5498               (sit-for 0)))
5499         (unless threads
5500           (setq gnus-tmp-level 0)))))
5501   (gnus-message 7 "Generating summary...done"))
5502
5503 (defun gnus-summary-prepare-unthreaded (headers)
5504   "Generate an unthreaded summary buffer based on HEADERS."
5505   (let (header number mark)
5506
5507     (beginning-of-line)
5508
5509     (while headers
5510       ;; We may have to root out some bad articles...
5511       (when (memq (setq number (mail-header-number
5512                                 (setq header (pop headers))))
5513                   gnus-newsgroup-limit)
5514         ;; Mark article as read when it has a low score.
5515         (when (and gnus-summary-mark-below
5516                    (< (or (cdr (assq number gnus-newsgroup-scored))
5517                           gnus-summary-default-score 0)
5518                       gnus-summary-mark-below)
5519                    (not (gnus-summary-article-ancient-p number)))
5520           (setq gnus-newsgroup-unreads
5521                 (delq number gnus-newsgroup-unreads))
5522           (if gnus-newsgroup-auto-expire
5523               (push number gnus-newsgroup-expirable)
5524             (push (cons number gnus-low-score-mark)
5525                   gnus-newsgroup-reads)))
5526
5527         (setq mark (gnus-article-mark number))
5528         (push (gnus-data-make number mark (1+ (point)) header 0)
5529               gnus-newsgroup-data)
5530         (gnus-summary-insert-line
5531          header 0 number
5532          (memq number gnus-newsgroup-undownloaded)
5533          mark (memq number gnus-newsgroup-replied)
5534          (memq number gnus-newsgroup-expirable)
5535          (mail-header-subject header) nil
5536          (cdr (assq number gnus-newsgroup-scored))
5537          (memq number gnus-newsgroup-processable))))))
5538
5539 (declare-function gnus-parameter-list-identifier "gnus-art" (name) t)
5540
5541 (defun gnus-group-get-list-identifiers (group)
5542   "Get list identifier regexp for GROUP."
5543   (or (gnus-parameter-list-identifier group)
5544       (if (consp gnus-list-identifiers)
5545           (mapconcat 'identity gnus-list-identifiers " *\\|")
5546         gnus-list-identifiers)))
5547
5548 (defun gnus-summary-remove-list-identifiers ()
5549   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5550   (let ((regexp (gnus-group-get-list-identifiers gnus-newsgroup-name))
5551         changed subject)
5552     (when regexp
5553       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5554       (dolist (header gnus-newsgroup-headers)
5555         (setq subject (mail-header-subject header)
5556               changed nil)
5557         (while (string-match regexp subject)
5558           (setq subject
5559                 (concat (substring subject 0 (match-beginning 1))
5560                         (substring subject (match-end 0)))
5561                 changed t))
5562         (when changed
5563           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5564             (setq subject
5565                   (concat (substring subject 0 (match-beginning 1))
5566                           (substring subject (match-end 1)))))
5567           (mail-header-set-subject header subject))))))
5568
5569 (defun gnus-fetch-headers (articles &optional limit force-new dependencies)
5570   "Fetch headers of ARTICLES."
5571   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5572     (gnus-message 7 "Fetching headers for %s..." name)
5573     (prog1
5574         (if (eq 'nov
5575                 (setq gnus-headers-retrieved-by
5576                       (gnus-retrieve-headers
5577                        articles gnus-newsgroup-name
5578                        (or limit
5579                            ;; We might want to fetch old headers, but
5580                            ;; not if there is only 1 article.
5581                            (and (or (and
5582                                      (not (eq gnus-fetch-old-headers 'some))
5583                                      (not (numberp gnus-fetch-old-headers)))
5584                                     (> (length articles) 1))
5585                                 gnus-fetch-old-headers)))))
5586             (gnus-get-newsgroup-headers-xover
5587              articles force-new dependencies gnus-newsgroup-name t)
5588           (gnus-get-newsgroup-headers dependencies force-new))
5589       (gnus-message 7 "Fetching headers for %s...done" name))))
5590
5591 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5592   "Select newsgroup GROUP.
5593 If READ-ALL is non-nil, all articles in the group are selected.
5594 If SELECT-ARTICLES, only select those articles from GROUP."
5595   (let* ((entry (gnus-group-entry group))
5596          ;;!!! Dirty hack; should be removed.
5597          (gnus-summary-ignore-duplicates
5598           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5599               t
5600             gnus-summary-ignore-duplicates))
5601          (info (nth 2 entry))
5602          charset articles fetched-articles cached)
5603
5604     (unless (gnus-check-server
5605              (set (make-local-variable 'gnus-current-select-method)
5606                   (gnus-find-method-for-group group)))
5607       (error "Couldn't open server"))
5608     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5609
5610     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5611         (gnus-activate-group group)     ; Or we can activate it...
5612         (progn                          ; Or we bug out.
5613           (when (derived-mode-p 'gnus-summary-mode)
5614             (gnus-kill-buffer (current-buffer)))
5615           (error
5616            "Couldn't activate group %s: %s"
5617            (mm-decode-coding-string group charset)
5618            (mm-decode-coding-string (gnus-status-message group) charset))))
5619
5620     (unless (gnus-request-group group t)
5621       (when (derived-mode-p 'gnus-summary-mode)
5622         (gnus-kill-buffer (current-buffer)))
5623       (error "Couldn't request group %s: %s"
5624              (mm-decode-coding-string group charset)
5625              (mm-decode-coding-string (gnus-status-message group) charset)))
5626
5627     (when (and gnus-agent
5628                (gnus-active group))
5629       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5630
5631       (setq gnus-summary-use-undownloaded-faces
5632             (gnus-agent-find-parameter
5633              group
5634              'agent-enable-undownloaded-faces)))
5635
5636     (setq gnus-newsgroup-name group
5637           gnus-newsgroup-unselected nil
5638           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5639
5640     (let ((display (gnus-group-find-parameter group 'display)))
5641       (setq gnus-newsgroup-display
5642             (cond
5643              ((not (zerop (or (car-safe read-all) 0)))
5644               ;; The user entered the group with C-u SPC/RET, let's show
5645               ;; all articles.
5646               'gnus-not-ignore)
5647              ((eq display 'all)
5648               'gnus-not-ignore)
5649              ((arrayp display)
5650               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5651              ((numberp display)
5652               ;; The following is probably the "correct" solution, but
5653               ;; it makes Gnus fetch all headers and then limit the
5654               ;; articles (which is slow), so instead we hack the
5655               ;; select-articles parameter instead. -- Simon Josefsson
5656               ;; <jas@kth.se>
5657               ;;
5658               ;; (gnus-byte-compile
5659               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5660               ;;                         display)))))
5661               (setq select-articles
5662                     (gnus-uncompress-range
5663                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5664                              (if (> tmp 0)
5665                                  tmp
5666                                1))
5667                            (cdr (gnus-active group)))))
5668               nil)
5669              (t
5670               nil))))
5671
5672     (gnus-summary-setup-default-charset)
5673
5674     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5675     (when (gnus-virtual-group-p group)
5676       (setq cached gnus-newsgroup-cached))
5677
5678     (setq gnus-newsgroup-unreads
5679           (gnus-sorted-ndifference
5680            (gnus-sorted-ndifference gnus-newsgroup-unreads
5681                                     gnus-newsgroup-marked)
5682            gnus-newsgroup-dormant))
5683
5684     (setq gnus-newsgroup-processable nil)
5685
5686     (gnus-update-read-articles group gnus-newsgroup-unreads t)
5687
5688     ;; Adjust and set lists of article marks.
5689     (when info
5690       (gnus-adjust-marked-articles info))
5691     (if (setq articles select-articles)
5692         (setq gnus-newsgroup-unselected
5693               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5694       (setq articles (gnus-articles-to-read group read-all)))
5695
5696     (cond
5697      ((null articles)
5698       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5699       'quit)
5700      ((eq articles 0) nil)
5701      (t
5702       ;; Init the dependencies hash table.
5703       (setq gnus-newsgroup-dependencies
5704             (gnus-make-hashtable (length articles)))
5705       (if (gnus-buffer-live-p gnus-group-buffer)
5706           (gnus-set-global-variables)
5707         (set-default 'gnus-newsgroup-name gnus-newsgroup-name))
5708       ;; Retrieve the headers and read them in.
5709
5710       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5711
5712       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5713       (when cached
5714         (setq gnus-newsgroup-cached cached))
5715
5716       ;; Suppress duplicates?
5717       (when gnus-suppress-duplicates
5718         (gnus-dup-suppress-articles))
5719
5720       ;; Set the initial limit.
5721       (setq gnus-newsgroup-limit (copy-sequence articles))
5722       ;; Remove canceled articles from the list of unread articles.
5723       (setq fetched-articles
5724             (mapcar (lambda (headers) (mail-header-number headers))
5725                     gnus-newsgroup-headers))
5726       (setq gnus-newsgroup-articles fetched-articles)
5727       (setq gnus-newsgroup-unreads
5728             (gnus-sorted-nintersection
5729              gnus-newsgroup-unreads fetched-articles))
5730       (gnus-compute-unseen-list)
5731
5732       ;; Removed marked articles that do not exist.
5733       (gnus-update-missing-marks
5734        (gnus-sorted-difference articles fetched-articles))
5735       ;; We might want to build some more threads first.
5736       (when (and gnus-fetch-old-headers
5737                  (eq gnus-headers-retrieved-by 'nov))
5738         (if (eq gnus-fetch-old-headers 'invisible)
5739             (gnus-build-all-threads)
5740           (gnus-build-old-threads)))
5741       ;; Let the Gnus agent mark articles as read.
5742       (when gnus-agent
5743         (gnus-agent-get-undownloaded-list))
5744       ;; Remove list identifiers from subject
5745       (gnus-summary-remove-list-identifiers)
5746       ;; Check whether auto-expire is to be done in this group.
5747       (setq gnus-newsgroup-auto-expire
5748             (and (gnus-group-auto-expirable-p group)
5749                  (not (gnus-group-read-only-p group))))
5750       ;; Set up the article buffer now, if necessary.
5751       (unless (and gnus-single-article-buffer
5752                    (equal gnus-article-buffer "*Article*"))
5753         (gnus-article-setup-buffer))
5754       ;; First and last article in this newsgroup.
5755       (when gnus-newsgroup-headers
5756         (setq gnus-newsgroup-begin
5757               (mail-header-number (car gnus-newsgroup-headers))
5758               gnus-newsgroup-end
5759               (mail-header-number
5760                (gnus-last-element gnus-newsgroup-headers))))
5761       ;; GROUP is successfully selected.
5762       (or gnus-newsgroup-headers t)))))
5763
5764 (defun gnus-compute-unseen-list ()
5765   ;; The `seen' marks are treated specially.
5766   (if (not gnus-newsgroup-seen)
5767       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5768     (setq gnus-newsgroup-unseen
5769           (gnus-inverse-list-range-intersection
5770            gnus-newsgroup-articles gnus-newsgroup-seen))))
5771
5772 (declare-function gnus-get-predicate "gnus-agent" (predicate))
5773
5774 (defun gnus-summary-display-make-predicate (display)
5775   (require 'gnus-agent)
5776   (when (= (length display) 1)
5777     (setq display (car display)))
5778   (unless gnus-summary-display-cache
5779     (dolist (elem (append '((unread . unread)
5780                             (read . read)
5781                             (unseen . unseen))
5782                           gnus-article-mark-lists))
5783       (push (cons (cdr elem)
5784                   (gnus-byte-compile    ;Why bother?
5785                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5786             gnus-summary-display-cache)))
5787   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5788         (gnus-category-predicate-cache gnus-summary-display-cache))
5789     (gnus-get-predicate display)))
5790
5791 ;; Uses the dynamically bound `gnus-number' variable.
5792 (defvar gnus-number)
5793 (defun gnus-article-marked-p (type &optional article)
5794   (let ((article (or article gnus-number)))
5795     (cond
5796      ((eq type 'tick)
5797       (memq article gnus-newsgroup-marked))
5798      ((eq type 'spam)
5799       (memq article gnus-newsgroup-spam-marked))
5800      ((eq type 'unsend)
5801       (memq article gnus-newsgroup-unsendable))
5802      ((eq type 'undownload)
5803       (memq article gnus-newsgroup-undownloaded))
5804      ((eq type 'download)
5805       (memq article gnus-newsgroup-downloadable))
5806      ((eq type 'unread)
5807       (memq article gnus-newsgroup-unreads))
5808      ((eq type 'read)
5809       (memq article gnus-newsgroup-reads))
5810      ((eq type 'dormant)
5811       (memq article gnus-newsgroup-dormant) )
5812      ((eq type 'expire)
5813       (memq article gnus-newsgroup-expirable))
5814      ((eq type 'reply)
5815       (memq article gnus-newsgroup-replied))
5816      ((eq type 'killed)
5817       (memq article gnus-newsgroup-killed))
5818      ((eq type 'bookmark)
5819       (assq article gnus-newsgroup-bookmarks))
5820      ((eq type 'score)
5821       (assq article gnus-newsgroup-scored))
5822      ((eq type 'save)
5823       (memq article gnus-newsgroup-saved))
5824      ((eq type 'cache)
5825       (memq article gnus-newsgroup-cached))
5826      ((eq type 'forward)
5827       (memq article gnus-newsgroup-forwarded))
5828      ((eq type 'seen)
5829       (not (memq article gnus-newsgroup-unseen)))
5830      (t t))))
5831
5832 (defun gnus-articles-to-read (group &optional read-all)
5833   "Find out what articles the user wants to read."
5834   (let* ((only-read-p t)
5835          (articles
5836           (gnus-list-range-difference
5837           ;; Select all articles if `read-all' is non-nil, or if there
5838           ;; are no unread articles.
5839           (if (or read-all
5840                   (and (zerop (length gnus-newsgroup-marked))
5841                        (zerop (length gnus-newsgroup-unreads)))
5842                   ;; Fetch all if the predicate is non-nil.
5843                   gnus-newsgroup-display)
5844               ;; We want to select the headers for all the articles in
5845               ;; the group, so we select either all the active
5846               ;; articles in the group, or (if that's nil), the
5847               ;; articles in the cache.
5848               (or
5849                (if gnus-newsgroup-maximum-articles
5850                    (let ((active (gnus-active group)))
5851                      (gnus-uncompress-range
5852                       (cons (max (car active)
5853                                  (- (cdr active)
5854                                     gnus-newsgroup-maximum-articles
5855                                     -1))
5856                             (cdr active))))
5857                  (gnus-uncompress-range (gnus-active group)))
5858                (gnus-cache-articles-in-group group))
5859             ;; Select only the "normal" subset of articles.
5860             (setq only-read-p nil)
5861             (gnus-sorted-nunion
5862              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5863              gnus-newsgroup-unreads))
5864           (cdr (assq 'unexist (gnus-info-marks (gnus-get-info group))))))
5865          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5866          (scored (length scored-list))
5867          (number (length articles))
5868          (marked (+ (length gnus-newsgroup-marked)
5869                     (length gnus-newsgroup-dormant)))
5870          (select
5871           (cond
5872            ((numberp read-all)
5873             read-all)
5874            ((numberp gnus-newsgroup-display)
5875             gnus-newsgroup-display)
5876            (t
5877             (condition-case ()
5878                 (cond
5879                  ((and (or (<= scored marked) (= scored number))
5880                        (numberp gnus-large-newsgroup)
5881                        (> number gnus-large-newsgroup))
5882                   (let* ((cursor-in-echo-area nil)
5883                          (initial (gnus-parameter-large-newsgroup-initial
5884                                    gnus-newsgroup-name))
5885                          (default (if only-read-p
5886                                       (or initial gnus-large-newsgroup)
5887                                     number))
5888                          (input
5889                           (read-string
5890                            (if only-read-p
5891                                (format
5892                                 "How many articles from %s (available %d, default %d): "
5893                                 (gnus-group-decoded-name
5894                                  (gnus-group-real-name gnus-newsgroup-name))
5895                                 number default)
5896                              (format
5897                               "How many articles from %s (%d default): "
5898                               (gnus-group-decoded-name
5899                                (gnus-group-real-name gnus-newsgroup-name))
5900                               default))
5901                            nil
5902                            nil
5903                            (number-to-string default))))
5904                     (if (string-match "^[ \t]*$" input) number input)))
5905                  ((and (> scored marked) (< scored number)
5906                        (> (- scored number) 20))
5907                   (let ((input
5908                          (read-string
5909                           (format "%s %s (%d scored, %d total): "
5910                                   "How many articles from"
5911                                   (gnus-group-decoded-name
5912                                    (gnus-group-real-name gnus-newsgroup-name))
5913                                   scored number))))
5914                     (if (string-match "^[ \t]*$" input)
5915                         number input)))
5916                  (t number))
5917               (quit
5918                (message "Quit getting the articles to read")
5919                nil))))))
5920     (setq select (if (stringp select) (string-to-number select) select))
5921     (if (or (null select) (zerop select))
5922         select
5923       (if (and (not (zerop scored)) (<= (abs select) scored))
5924           (progn
5925             (setq articles (sort scored-list '<))
5926             (setq number (length articles)))
5927         (setq articles (copy-sequence articles)))
5928
5929       (when (< (abs select) number)
5930         (if (< select 0)
5931             ;; Select the N oldest articles.
5932             (setcdr (nthcdr (1- (abs select)) articles) nil)
5933           ;; Select the N most recent articles.
5934           (setq articles (nthcdr (- number select) articles))))
5935       (setq gnus-newsgroup-unselected
5936             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5937       (when gnus-alter-articles-to-read-function
5938         (setq articles
5939               (sort
5940                (funcall gnus-alter-articles-to-read-function
5941                         gnus-newsgroup-name articles)
5942                '<)))
5943       articles)))
5944
5945 (defun gnus-killed-articles (killed articles)
5946   (let (out)
5947     (while articles
5948       (when (inline (gnus-member-of-range (car articles) killed))
5949         (push (car articles) out))
5950       (setq articles (cdr articles)))
5951     out))
5952
5953 (defun gnus-article-mark-to-type (mark)
5954   "Return the type of MARK."
5955   (or (cadr (assq mark gnus-article-special-mark-lists))
5956       'list))
5957
5958 (defun gnus-article-unpropagatable-p (mark)
5959   "Return whether MARK should be propagated to back end."
5960   (memq mark gnus-article-unpropagated-mark-lists))
5961
5962 (defun gnus-adjust-marked-articles (info)
5963   "Set all article lists and remove all marks that are no longer valid."
5964   (let* ((marked-lists (gnus-info-marks info))
5965          (active (gnus-active (gnus-info-group info)))
5966          (min (car active))
5967          (max (cdr active))
5968          (types gnus-article-mark-lists)
5969          marks var articles article mark mark-type
5970          bgn end)
5971     ;; Hack to avoid adjusting marks for imap.
5972     (when (eq (car (gnus-find-method-for-group (gnus-info-group info)))
5973               'nnimap)
5974       (setq min 1))
5975
5976     (dolist (marks marked-lists)
5977       (setq mark (car marks)
5978             mark-type (gnus-article-mark-to-type mark)
5979             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5980       ;; We set the variable according to the type of the marks list,
5981       ;; and then adjust the marks to a subset of the active articles.
5982       (cond
5983        ;; Adjust "simple" lists - compressed yet unsorted
5984        ((eq mark-type 'list)
5985         ;; Simultaneously uncompress and clip to active range
5986         ;; See gnus-uncompress-range for a description of possible marks
5987         (let (l lh)
5988           (if (not (cadr marks))
5989               (set var nil)
5990             (setq articles (if (numberp (cddr marks))
5991                                (list (cdr marks))
5992                              (cdr marks))
5993                   lh (cons nil nil)
5994                   l lh)
5995
5996             (while (setq article (pop articles))
5997               (cond ((consp article)
5998                      (setq bgn (max (car article) min)
5999                            end (min (cdr article) max))
6000                      (while (<= bgn end)
6001                        (setq l (setcdr l (cons bgn nil))
6002                              bgn (1+ bgn))))
6003                     ((and (<= min article)
6004                           (>= max article))
6005                      (setq l (setcdr l (cons article nil))))))
6006             (set var (cdr lh)))))
6007        ;; Adjust assocs.
6008        ((eq mark-type 'tuple)
6009         (set var (setq articles (cdr marks)))
6010         (when (not (listp (cdr (symbol-value var))))
6011           (set var (list (symbol-value var))))
6012         (when (not (listp (cdr articles)))
6013           (setq articles (list articles)))
6014         (while articles
6015           (when (or (not (consp (setq article (pop articles))))
6016                     (< (car article) min)
6017                     (> (car article) max))
6018             (set var (delq article (symbol-value var))))))
6019        ;; Adjust ranges (sloppily).
6020        ((eq mark-type 'range)
6021         (cond
6022          ((eq mark 'seen)
6023           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
6024           ;; It should be (seen (NUM1 . NUM2)).
6025           (when (numberp (cddr marks))
6026             (setcdr marks (list (cdr marks))))
6027           (setq articles (cdr marks))
6028           (while (and articles
6029                       (or (and (consp (car articles))
6030                                (> min (cdar articles)))
6031                           (and (numberp (car articles))
6032                                (> min (car articles)))))
6033             (pop articles))
6034           (set var articles))
6035          ((eq mark 'unexist)
6036           (set var (cdr marks)))))))))
6037
6038 (defun gnus-update-missing-marks (missing)
6039   "Go through the list of MISSING articles and remove them from the mark lists."
6040   (when missing
6041     (let (var m)
6042       ;; Go through all types.
6043       (dolist (elem gnus-article-mark-lists)
6044         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
6045           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
6046           (when (symbol-value var)
6047             ;; This list has articles.  So we delete all missing
6048             ;; articles from it.
6049             (setq m missing)
6050             (while m
6051               (set var (delq (pop m) (symbol-value var))))))))))
6052
6053 (defun gnus-update-marks ()
6054   "Enter the various lists of marked articles into the newsgroup info list."
6055   (let ((types gnus-article-mark-lists)
6056         (info (gnus-get-info gnus-newsgroup-name))
6057         type list newmarked symbol delta-marks)
6058     (when info
6059       ;; Add all marks lists to the list of marks lists.
6060       (while (setq type (pop types))
6061         (setq list (symbol-value
6062                     (setq symbol
6063                           (intern (format "gnus-newsgroup-%s" (car type))))))
6064
6065         (when list
6066           ;; Get rid of the entries of the articles that have the
6067           ;; default score.
6068           (when (and (eq (cdr type) 'score)
6069                      gnus-save-score
6070                      list)
6071             (let* ((arts list)
6072                    (prev (cons nil list))
6073                    (all prev))
6074               (while arts
6075                 (if (or (not (consp (car arts)))
6076                         (= (cdar arts) gnus-summary-default-score))
6077                     (setcdr prev (cdr arts))
6078                   (setq prev arts))
6079                 (setq arts (cdr arts)))
6080               (setq list (cdr all)))))
6081
6082         (when (eq (cdr type) 'seen)
6083           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
6084
6085         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
6086           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
6087
6088         (when (and (gnus-check-backend-function
6089                     'request-set-mark gnus-newsgroup-name)
6090                    (not (gnus-article-unpropagatable-p (cdr type))))
6091           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
6092                  ;; Don't do anything about marks for articles we
6093                  ;; didn't actually get any headers for.
6094                  (del
6095                   (gnus-list-range-intersection
6096                    gnus-newsgroup-articles
6097                    (gnus-remove-from-range (gnus-copy-sequence old) list)))
6098                  (add
6099                   (gnus-list-range-intersection
6100                    gnus-newsgroup-articles
6101                    (gnus-remove-from-range
6102                     (gnus-copy-sequence list) old))))
6103             (when add
6104               (push (list add 'add (list (cdr type))) delta-marks))
6105             (when del
6106               ;; Don't delete marks from outside the active range.
6107               ;; This shouldn't happen, but is a sanity check.
6108               (setq del (gnus-sorted-range-intersection
6109                          (gnus-active gnus-newsgroup-name) del))
6110               (push (list del 'del (list (cdr type))) delta-marks))))
6111
6112         (when (or list
6113                   (eq (cdr type) 'unexist))
6114           (push (cons (cdr type) list) newmarked)))
6115
6116       (when delta-marks
6117         (unless (gnus-check-group gnus-newsgroup-name)
6118           (error "Can't open server for %s" gnus-newsgroup-name))
6119         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
6120
6121       ;; Enter these new marks into the info of the group.
6122       (if (nthcdr 3 info)
6123           (setcar (nthcdr 3 info) newmarked)
6124         ;; Add the marks lists to the end of the info.
6125         (when newmarked
6126           (setcdr (nthcdr 2 info) (list newmarked))))
6127
6128       ;; Cut off the end of the info if there's nothing else there.
6129       (let ((i 5))
6130         (while (and (> i 2)
6131                     (not (nth i info)))
6132           (when (nthcdr (decf i) info)
6133             (setcdr (nthcdr i info) nil)))))))
6134
6135 (defun gnus-set-mode-line (where)
6136   "Set the mode line of the article or summary buffers.
6137 If WHERE is `summary', the summary mode line format will be used."
6138   ;; Is this mode line one we keep updated?
6139   (when (and (memq where gnus-updated-mode-lines)
6140              (symbol-value
6141               (intern (format "gnus-%s-mode-line-format-spec" where))))
6142     (let (mode-string)
6143       ;; We evaluate this in the summary buffer since these
6144       ;; variables are buffer-local to that buffer.
6145       (with-current-buffer gnus-summary-buffer
6146         ;; We bind all these variables that are used in the `eval' form
6147         ;; below.
6148         (let* ((mformat (symbol-value
6149                          (intern
6150                           (format "gnus-%s-mode-line-format-spec" where))))
6151                (gnus-tmp-group-name (gnus-mode-string-quote
6152                                      (gnus-group-decoded-name
6153                                       gnus-newsgroup-name)))
6154                (gnus-tmp-article-number (or gnus-current-article 0))
6155                (gnus-tmp-unread gnus-newsgroup-unreads)
6156                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
6157                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
6158                (gnus-tmp-unread-and-unselected
6159                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
6160                             (zerop gnus-tmp-unselected))
6161                        "")
6162                       ((zerop gnus-tmp-unselected)
6163                        (format "{%d more}" gnus-tmp-unread-and-unticked))
6164                       (t (format "{%d(+%d) more}"
6165                                  gnus-tmp-unread-and-unticked
6166                                  gnus-tmp-unselected))))
6167                (gnus-tmp-subject
6168                 (if (and gnus-current-headers
6169                          (vectorp gnus-current-headers))
6170                     (gnus-mode-string-quote
6171                      (mail-header-subject gnus-current-headers))
6172                   ""))
6173                bufname-length max-len
6174                gnus-tmp-header) ;; passed as argument to any user-format-funcs
6175           (setq mode-string (eval mformat))
6176           (setq bufname-length (if (string-match "%b" mode-string)
6177                                    (- (length
6178                                        (buffer-name
6179                                         (if (eq where 'summary)
6180                                             nil
6181                                           (get-buffer gnus-article-buffer))))
6182                                       2)
6183                                  0))
6184           (setq max-len (max 4 (if gnus-mode-non-string-length
6185                                    (- (window-width)
6186                                       gnus-mode-non-string-length
6187                                       bufname-length)
6188                                  (length mode-string))))
6189           ;; We might have to chop a bit of the string off...
6190           (when (> (length mode-string) max-len)
6191             (setq mode-string
6192                   (concat (truncate-string-to-width mode-string (- max-len 3))
6193                           "...")))))
6194       ;; Update the mode line.
6195       (setq mode-line-buffer-identification
6196             (gnus-mode-line-buffer-identification (list mode-string)))
6197       (set-buffer-modified-p t))))
6198
6199 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
6200   "Go through the HEADERS list and add all Xrefs to a hash table.
6201 The resulting hash table is returned, or nil if no Xrefs were found."
6202   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
6203          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
6204          (xref-hashtb (gnus-make-hashtable))
6205          start group entry number xrefs header)
6206     (while headers
6207       (setq header (pop headers))
6208       (when (and (setq xrefs (mail-header-xref header))
6209                  (not (memq (setq number (mail-header-number header))
6210                             unreads)))
6211         (setq start 0)
6212         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6213           (setq start (match-end 0))
6214           (setq group (if prefix
6215                           (concat prefix (substring xrefs (match-beginning 1)
6216                                                     (match-end 1)))
6217                         (substring xrefs (match-beginning 1) (match-end 1))))
6218           (setq number
6219                 (string-to-number (substring xrefs (match-beginning 2)
6220                                           (match-end 2))))
6221           (if (setq entry (gnus-gethash group xref-hashtb))
6222               (setcdr entry (cons number (cdr entry)))
6223             (gnus-sethash group (cons number nil) xref-hashtb)))))
6224     (and start xref-hashtb)))
6225
6226 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6227   "Look through all the headers and mark the Xrefs as read."
6228   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6229         name info xref-hashtb idlist method nth4)
6230     (with-current-buffer gnus-group-buffer
6231       (when (setq xref-hashtb
6232                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6233         (mapatoms
6234          (lambda (group)
6235            (unless (string= from-newsgroup (setq name (symbol-name group)))
6236              (setq idlist (symbol-value group))
6237              ;; Dead groups are not updated.
6238              (and (prog1
6239                       (setq info (gnus-get-info name))
6240                     (when (stringp (setq nth4 (gnus-info-method info)))
6241                       (setq nth4 (gnus-server-to-method nth4))))
6242                   ;; Only do the xrefs if the group has the same
6243                   ;; select method as the group we have just read.
6244                   (or (gnus-methods-equal-p
6245                        nth4 (gnus-find-method-for-group from-newsgroup))
6246                       virtual
6247                       (equal nth4 (setq method (gnus-find-method-for-group
6248                                                 from-newsgroup)))
6249                       (and (equal (car nth4) (car method))
6250                            (equal (nth 1 nth4) (nth 1 method))))
6251                   gnus-use-cross-reference
6252                   (or (not (eq gnus-use-cross-reference t))
6253                       virtual
6254                       ;; Only do cross-references on subscribed
6255                       ;; groups, if that is what is wanted.
6256                       (<= (gnus-info-level info) gnus-level-subscribed))
6257                   (gnus-group-make-articles-read name idlist))))
6258          xref-hashtb)))))
6259
6260 (defun gnus-compute-read-articles (group articles)
6261   (let* ((entry (gnus-group-entry group))
6262          (info (nth 2 entry))
6263          (active (gnus-active group))
6264          ninfo)
6265     (when entry
6266       ;; First peel off all invalid article numbers.
6267       (when active
6268         (let ((ids articles)
6269               id first)
6270           (while (setq id (pop ids))
6271             (when (and first (> id (cdr active)))
6272               ;; We'll end up in this situation in one particular
6273               ;; obscure situation.  If you re-scan a group and get
6274               ;; a new article that is cross-posted to a different
6275               ;; group that has not been re-scanned, you might get
6276               ;; crossposted article that has a higher number than
6277               ;; Gnus believes possible.  So we re-activate this
6278               ;; group as well.  This might mean doing the
6279               ;; crossposting thingy will *increase* the number
6280               ;; of articles in some groups.  Tsk, tsk.
6281               (setq active (or (gnus-activate-group group) active)))
6282             (when (or (> id (cdr active))
6283                       (< id (car active)))
6284               (setq articles (delq id articles))))))
6285       ;; If the read list is nil, we init it.
6286       (if (and active
6287                (null (gnus-info-read info))
6288                (> (car active) 1))
6289           (setq ninfo (cons 1 (1- (car active))))
6290         (setq ninfo (gnus-info-read info)))
6291       ;; Then we add the read articles to the range.
6292       (gnus-add-to-range
6293        ninfo (setq articles (sort articles '<))))))
6294
6295 (defun gnus-group-make-articles-read (group articles)
6296   "Update the info of GROUP to say that ARTICLES are read."
6297   (let* ((num 0)
6298          (entry (gnus-group-entry group))
6299          (info (nth 2 entry))
6300          (active (gnus-active group))
6301          (set-marks
6302           (gnus-method-option-p
6303            (gnus-find-method-for-group group)
6304            'server-marks))
6305          range)
6306     (if (not entry)
6307         ;; Group that Gnus doesn't know exists, but still allow the
6308         ;; backend to set marks.
6309         (when set-marks
6310           (gnus-request-set-mark
6311            group (list (list (gnus-compress-sequence (sort articles #'<))
6312                              'add '(read)))))
6313       ;; Normal, subscribed groups.
6314       (setq range (gnus-compute-read-articles group articles))
6315       (with-current-buffer gnus-group-buffer
6316         (gnus-undo-register
6317           `(progn
6318              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6319              (gnus-info-set-read ',info ',(gnus-info-read info))
6320              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6321              (when ,set-marks
6322                (gnus-request-set-mark
6323                 ,group (list (list ',range 'del '(read)))))
6324              (gnus-group-update-group ,group t))))
6325       ;; Add the read articles to the range.
6326       (gnus-info-set-read info range)
6327       (when set-marks
6328         (gnus-request-set-mark group (list (list range 'add '(read)))))
6329       ;; Then we have to re-compute how many unread
6330       ;; articles there are in this group.
6331       (when active
6332         (cond
6333          ((not range)
6334           (setq num (- (1+ (cdr active)) (car active))))
6335          ((not (listp (cdr range)))
6336           (setq num (- (cdr active) (- (1+ (cdr range))
6337                                        (car range)))))
6338          (t
6339           (while range
6340             (if (numberp (car range))
6341                 (setq num (1+ num))
6342               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6343             (setq range (cdr range)))
6344           (setq num (- (cdr active) num))))
6345         ;; Update the number of unread articles.
6346         (setcar entry num)
6347         ;; Update the group buffer.
6348         (unless (gnus-ephemeral-group-p group)
6349           (gnus-group-update-group group t))))))
6350
6351 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6352   (let ((cur nntp-server-buffer)
6353         (dependencies
6354          (or dependencies
6355              (with-current-buffer gnus-summary-buffer
6356                gnus-newsgroup-dependencies)))
6357         headers id end ref number
6358         (mail-parse-charset gnus-newsgroup-charset)
6359         (mail-parse-ignored-charsets
6360          (save-current-buffer (condition-case nil
6361                                   (set-buffer gnus-summary-buffer)
6362                                 (error))
6363                               gnus-newsgroup-ignored-charsets)))
6364     (with-current-buffer nntp-server-buffer
6365       ;; Translate all TAB characters into SPACE characters.
6366       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6367       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6368       (ietf-drums-unfold-fws)
6369       (gnus-run-hooks 'gnus-parse-headers-hook)
6370       (let ((case-fold-search t)
6371             in-reply-to header p lines chars)
6372         (goto-char (point-min))
6373         ;; Search to the beginning of the next header.  Error messages
6374         ;; do not begin with 2 or 3.
6375         (while (re-search-forward "^[23][0-9]+ " nil t)
6376           (setq id nil
6377                 ref nil)
6378           ;; This implementation of this function, with nine
6379           ;; search-forwards instead of the one re-search-forward and
6380           ;; a case (which basically was the old function) is actually
6381           ;; about twice as fast, even though it looks messier.  You
6382           ;; can't have everything, I guess.  Speed and elegance
6383           ;; doesn't always go hand in hand.
6384           (setq
6385            header
6386            (vector
6387             ;; Number.
6388             (prog1
6389                 (setq number (read cur))
6390               (end-of-line)
6391               (setq p (point))
6392               (narrow-to-region (point)
6393                                 (or (and (search-forward "\n.\n" nil t)
6394                                          (- (point) 2))
6395                                     (point))))
6396             ;; Subject.
6397             (progn
6398               (goto-char p)
6399               (if (search-forward "\nsubject:" nil t)
6400                   (funcall gnus-decode-encoded-word-function
6401                            (nnheader-header-value))
6402                 "(none)"))
6403             ;; From.
6404             (progn
6405               (goto-char p)
6406               (if (search-forward "\nfrom:" nil t)
6407                   (funcall gnus-decode-encoded-address-function
6408                            (nnheader-header-value))
6409                 "(nobody)"))
6410             ;; Date.
6411             (progn
6412               (goto-char p)
6413               (if (search-forward "\ndate:" nil t)
6414                   (nnheader-header-value) ""))
6415             ;; Message-ID.
6416             (progn
6417               (goto-char p)
6418               (setq id (if (re-search-forward
6419                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6420                            ;; We do it this way to make sure the Message-ID
6421                            ;; is (somewhat) syntactically valid.
6422                            (buffer-substring (match-beginning 1)
6423                                              (match-end 1))
6424                          ;; If there was no message-id, we just fake one
6425                          ;; to make subsequent routines simpler.
6426                          (nnheader-generate-fake-message-id number))))
6427             ;; References.
6428             (progn
6429               (goto-char p)
6430               (if (search-forward "\nreferences:" nil t)
6431                   (progn
6432                     (setq end (point))
6433                     (prog1
6434                         (nnheader-header-value)
6435                       (setq ref
6436                             (buffer-substring
6437                              (progn
6438                                (end-of-line)
6439                                (search-backward ">" end t)
6440                                (1+ (point)))
6441                              (progn
6442                                (search-backward "<" end t)
6443                                (point))))))
6444                 ;; Get the references from the in-reply-to header if there
6445                 ;; were no references and the in-reply-to header looks
6446                 ;; promising.
6447                 (if (and (search-forward "\nin-reply-to:" nil t)
6448                          (setq in-reply-to (nnheader-header-value))
6449                          (string-match "<[^>]+>" in-reply-to))
6450                     (let (ref2)
6451                       (setq ref (substring in-reply-to (match-beginning 0)
6452                                            (match-end 0)))
6453                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6454                         (setq ref2 (substring in-reply-to (match-beginning 0)
6455                                               (match-end 0)))
6456                         (when (> (length ref2) (length ref))
6457                           (setq ref ref2)))
6458                       ref)
6459                   (setq ref nil))))
6460             ;; Chars.
6461             (progn
6462               (goto-char p)
6463               (if (search-forward "\nchars: " nil t)
6464                   (if (numberp (setq chars (ignore-errors (read cur))))
6465                       chars -1)
6466                 -1))
6467             ;; Lines.
6468             (progn
6469               (goto-char p)
6470               (if (search-forward "\nlines: " nil t)
6471                   (if (numberp (setq lines (ignore-errors (read cur))))
6472                       lines -1)
6473                 -1))
6474             ;; Xref.
6475             (progn
6476               (goto-char p)
6477               (and (search-forward "\nxref:" nil t)
6478                    (nnheader-header-value)))
6479             ;; Extra.
6480             (when gnus-extra-headers
6481               (let ((extra gnus-extra-headers)
6482                     out)
6483                 (while extra
6484                   (goto-char p)
6485                   (when (search-forward
6486                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6487                     (push (cons (car extra) (nnheader-header-value))
6488                           out))
6489                   (pop extra))
6490                 out))))
6491           (when (equal id ref)
6492             (setq ref nil))
6493
6494           (when gnus-alter-header-function
6495             (funcall gnus-alter-header-function header)
6496             (setq id (mail-header-id header)
6497                   ref (gnus-parent-id (mail-header-references header))))
6498
6499           (when (setq header
6500                       (gnus-dependencies-add-header
6501                        header dependencies force-new))
6502             (push header headers))
6503           (goto-char (point-max))
6504           (widen))
6505         (nreverse headers)))))
6506
6507 ;; Goes through the xover lines and returns a list of vectors
6508 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6509                                                   force-new dependencies
6510                                                   group also-fetch-heads)
6511   "Parse the news overview data in the server buffer.
6512 Return a list of headers that match SEQUENCE (see
6513 `nntp-retrieve-headers')."
6514   ;; Get the Xref when the users reads the articles since most/some
6515   ;; NNTP servers do not include Xrefs when using XOVER.
6516   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6517   (let ((mail-parse-charset gnus-newsgroup-charset)
6518         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6519         (cur nntp-server-buffer)
6520         (dependencies (or dependencies gnus-newsgroup-dependencies))
6521         (allp (cond
6522                ((eq gnus-read-all-available-headers t)
6523                 t)
6524                ((and (stringp gnus-read-all-available-headers)
6525                      group)
6526                 (string-match gnus-read-all-available-headers group))
6527                (t
6528                 nil)))
6529         number headers header)
6530     (with-current-buffer nntp-server-buffer
6531       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6532       ;; Allow the user to mangle the headers before parsing them.
6533       (gnus-run-hooks 'gnus-parse-headers-hook)
6534       (goto-char (point-min))
6535       (gnus-parse-without-error
6536         (while (and (or sequence allp)
6537                     (not (eobp)))
6538           (setq number (read cur))
6539           (when (not allp)
6540             (while (and sequence
6541                         (< (car sequence) number))
6542               (setq sequence (cdr sequence))))
6543           (when (and (or allp
6544                          (and sequence
6545                               (eq number (car sequence))))
6546                      (progn
6547                        (setq sequence (cdr sequence))
6548                        (setq header (inline
6549                                       (gnus-nov-parse-line
6550                                        number dependencies force-new)))))
6551             (push header headers))
6552           (forward-line 1)))
6553       ;; A common bug in inn is that if you have posted an article and
6554       ;; then retrieves the active file, it will answer correctly --
6555       ;; the new article is included.  However, a NOV entry for the
6556       ;; article may not have been generated yet, so this may fail.
6557       ;; We work around this problem by retrieving the last few
6558       ;; headers using HEAD.
6559       (if (or (not also-fetch-heads)
6560               (not sequence))
6561           ;; We (probably) got all the headers.
6562           (nreverse headers)
6563         (let ((gnus-nov-is-evil t))
6564           (nconc
6565            (nreverse headers)
6566            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6567              (gnus-get-newsgroup-headers))))))))
6568
6569 (defun gnus-article-get-xrefs ()
6570   "Fill in the Xref value in `gnus-current-headers', if necessary.
6571 This is meant to be called in `gnus-article-internal-prepare-hook'."
6572   (let ((headers (with-current-buffer gnus-summary-buffer
6573                    gnus-current-headers)))
6574     (or (not gnus-use-cross-reference)
6575         (not headers)
6576         (and (mail-header-xref headers)
6577              (not (string= (mail-header-xref headers) "")))
6578         (let ((case-fold-search t)
6579               xref)
6580           (save-restriction
6581             (nnheader-narrow-to-headers)
6582             (goto-char (point-min))
6583             (when (or (and (not (eobp))
6584                            (eq (downcase (char-after)) ?x)
6585                            (looking-at "Xref:"))
6586                       (search-forward "\nXref:" nil t))
6587               (goto-char (1+ (match-end 0)))
6588               (setq xref (buffer-substring (point) (point-at-eol)))
6589               (mail-header-set-xref headers xref)))))))
6590
6591 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6592   "Find article ID and insert the summary line for that article.
6593 OLD-HEADER can either be a header or a line number to insert
6594 the subject line on.
6595 If USE-OLD-HEADER is non-nil, then OLD-HEADER should be a header,
6596 and OLD-HEADER will be used when the summary line is inserted,
6597 too, instead of trying to fetch new headers."
6598   (let* ((line (and (numberp old-header) old-header))
6599          (old-header (and (vectorp old-header) old-header))
6600          (header (cond ((and old-header use-old-header)
6601                         old-header)
6602                        ((and (numberp id)
6603                              (gnus-number-to-header id))
6604                         (gnus-number-to-header id))
6605                        (t
6606                         (gnus-read-header id))))
6607          (number (and (numberp id) id))
6608          d)
6609     (when header
6610       ;; Rebuild the thread that this article is part of and go to the
6611       ;; article we have fetched.
6612       (when (and (not gnus-show-threads)
6613                  old-header)
6614         (when (and number
6615                    (setq d (gnus-data-find (mail-header-number old-header))))
6616           (goto-char (gnus-data-pos d))
6617           (gnus-data-remove
6618            number
6619            (- (point-at-bol)
6620               (prog1
6621                   (1+ (point-at-eol))
6622                 (gnus-delete-line))))))
6623       ;; Remove list identifiers from subject.
6624       (let ((gnus-newsgroup-headers (list header)))
6625         (gnus-summary-remove-list-identifiers))
6626       (when old-header
6627         (mail-header-set-number header (mail-header-number old-header)))
6628       (setq gnus-newsgroup-sparse
6629             (delq (setq number (mail-header-number header))
6630                   gnus-newsgroup-sparse))
6631       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6632       (push number gnus-newsgroup-limit)
6633       (gnus-rebuild-thread (mail-header-id header) line)
6634       (gnus-summary-goto-subject number nil t))
6635     (when (and (numberp number)
6636                (> number 0))
6637       ;; We have to update the boundaries even if we can't fetch the
6638       ;; article if ID is a number -- so that the next `P' or `N'
6639       ;; command will fetch the previous (or next) article even
6640       ;; if the one we tried to fetch this time has been canceled.
6641       (unless (and gnus-newsgroup-end (< number gnus-newsgroup-end))
6642         (setq gnus-newsgroup-end number))
6643       (unless (and gnus-newsgroup-begin (> number gnus-newsgroup-begin))
6644         (setq gnus-newsgroup-begin number))
6645       (setq gnus-newsgroup-unselected
6646             (delq number gnus-newsgroup-unselected)))
6647     ;; Report back a success?
6648     (and header (mail-header-number header))))
6649
6650 ;;; Process/prefix in the summary buffer
6651
6652 (defun gnus-summary-work-articles (n)
6653   "Return a list of articles to be worked upon.
6654 The prefix argument, the list of process marked articles, and the
6655 current article will be taken into consideration."
6656   (with-current-buffer gnus-summary-buffer
6657     (cond
6658      (n
6659       ;; A numerical prefix has been given.
6660       (setq n (prefix-numeric-value n))
6661       (let ((backward (< n 0))
6662             (n (abs (prefix-numeric-value n)))
6663             articles article)
6664         (save-excursion
6665           (while
6666               (and (> n 0)
6667                    (push (setq article (gnus-summary-article-number))
6668                          articles)
6669                    (if backward
6670                        (gnus-summary-find-prev nil article)
6671                      (gnus-summary-find-next nil article)))
6672             (decf n)))
6673         (nreverse articles)))
6674      ((and (gnus-region-active-p) (mark))
6675       (message "region active")
6676       ;; Work on the region between point and mark.
6677       (let ((max (max (point) (mark)))
6678             articles article)
6679         (save-excursion
6680           (goto-char (min (point) (mark)))
6681           (while
6682               (and
6683                (push (setq article (gnus-summary-article-number)) articles)
6684                (gnus-summary-find-next nil article)
6685                (< (point) max)))
6686           (nreverse articles))))
6687      (gnus-newsgroup-processable
6688       ;; There are process-marked articles present.
6689       ;; Save current state.
6690       (gnus-summary-save-process-mark)
6691       ;; Return the list.
6692       (reverse gnus-newsgroup-processable))
6693      (t
6694       ;; Just return the current article.
6695       (list (gnus-summary-article-number))))))
6696
6697 (defmacro gnus-summary-iterate (arg &rest forms)
6698   "Iterate over the process/prefixed articles and do FORMS.
6699 ARG is the interactive prefix given to the command.  FORMS will be
6700 executed with point over the summary line of the articles."
6701   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6702     `(let ((,articles (gnus-summary-work-articles ,arg)))
6703        (while ,articles
6704          (gnus-summary-goto-subject (car ,articles))
6705          ,@forms
6706          (pop ,articles)))))
6707
6708 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6709 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6710
6711 (defun gnus-summary-save-process-mark ()
6712   "Push the current set of process marked articles on the stack."
6713   (interactive)
6714   (push (copy-sequence gnus-newsgroup-processable)
6715         gnus-newsgroup-process-stack))
6716
6717 (defun gnus-summary-kill-process-mark ()
6718   "Push the current set of process marked articles on the stack and unmark."
6719   (interactive)
6720   (gnus-summary-save-process-mark)
6721   (gnus-summary-unmark-all-processable))
6722
6723 (defun gnus-summary-yank-process-mark ()
6724   "Pop the last process mark state off the stack and restore it."
6725   (interactive)
6726   (unless gnus-newsgroup-process-stack
6727     (error "Empty mark stack"))
6728   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6729
6730 (defun gnus-summary-process-mark-set (set)
6731   "Make SET into the current process marked articles."
6732   (gnus-summary-unmark-all-processable)
6733   (mapc 'gnus-summary-set-process-mark set))
6734
6735 ;;; Searching and stuff
6736
6737 (defun gnus-summary-search-group (&optional backward use-level)
6738   "Search for next unread newsgroup.
6739 If optional argument BACKWARD is non-nil, search backward instead."
6740   (with-current-buffer gnus-group-buffer
6741     (when (gnus-group-search-forward
6742            backward nil (if use-level (gnus-group-group-level) nil))
6743       (gnus-group-group-name))))
6744
6745 (defun gnus-summary-best-group (&optional exclude-group)
6746   "Find the name of the best unread group.
6747 If EXCLUDE-GROUP, do not go to this group."
6748   (with-current-buffer gnus-group-buffer
6749     (save-excursion
6750       (gnus-group-best-unread-group exclude-group))))
6751
6752 (defun gnus-summary-find-next (&optional unread article backward)
6753   (if backward
6754       (gnus-summary-find-prev unread article)
6755     (let* ((dummy (gnus-summary-article-intangible-p))
6756            (article (or article (gnus-summary-article-number)))
6757            (data (gnus-data-find-list article))
6758            result)
6759       (when (and (not dummy)
6760                  (or (not gnus-summary-check-current)
6761                      (not unread)
6762                      (not (gnus-data-unread-p (car data)))))
6763         (setq data (cdr data)))
6764       (when (setq result
6765                   (if unread
6766                       (progn
6767                         (while data
6768                           (unless (memq (gnus-data-number (car data))
6769                                         (cond
6770                                          ((eq gnus-auto-goto-ignores
6771                                               'always-undownloaded)
6772                                           gnus-newsgroup-undownloaded)
6773                                          (gnus-plugged
6774                                           nil)
6775                                          ((eq gnus-auto-goto-ignores
6776                                               'unfetched)
6777                                           gnus-newsgroup-unfetched)
6778                                          ((eq gnus-auto-goto-ignores
6779                                               'undownloaded)
6780                                           gnus-newsgroup-undownloaded)))
6781                             (when (gnus-data-unread-p (car data))
6782                               (setq result (car data)
6783                                     data nil)))
6784                           (setq data (cdr data)))
6785                         result)
6786                     (car data)))
6787         (goto-char (gnus-data-pos result))
6788         (gnus-data-number result)))))
6789
6790 (defun gnus-summary-find-prev (&optional unread article)
6791   (let* ((eobp (eobp))
6792          (article (or article (gnus-summary-article-number)))
6793          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6794          result)
6795     (when (and (not eobp)
6796                (or (not gnus-summary-check-current)
6797                    (not unread)
6798                    (not (gnus-data-unread-p (car data)))))
6799       (setq data (cdr data)))
6800     (when (setq result
6801                 (if unread
6802                     (progn
6803                       (while data
6804                         (unless (memq (gnus-data-number (car data))
6805                                       (cond
6806                                        ((eq gnus-auto-goto-ignores
6807                                             'always-undownloaded)
6808                                         gnus-newsgroup-undownloaded)
6809                                        (gnus-plugged
6810                                         nil)
6811                                        ((eq gnus-auto-goto-ignores
6812                                             'unfetched)
6813                                         gnus-newsgroup-unfetched)
6814                                        ((eq gnus-auto-goto-ignores
6815                                             'undownloaded)
6816                                         gnus-newsgroup-undownloaded)))
6817                           (when (gnus-data-unread-p (car data))
6818                             (setq result (car data)
6819                                   data nil)))
6820                         (setq data (cdr data)))
6821                       result)
6822                   (car data)))
6823       (goto-char (gnus-data-pos result))
6824       (gnus-data-number result))))
6825
6826 (defun gnus-summary-find-subject (subject &optional unread backward article)
6827   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6828          (article (or article (gnus-summary-article-number)))
6829          (articles (gnus-data-list backward))
6830          (arts (gnus-data-find-list article articles))
6831          result)
6832     (when (or (not gnus-summary-check-current)
6833               (not unread)
6834               (not (gnus-data-unread-p (car arts))))
6835       (setq arts (cdr arts)))
6836     (while arts
6837       (and (or (not unread)
6838                (gnus-data-unread-p (car arts)))
6839            (vectorp (gnus-data-header (car arts)))
6840            (gnus-subject-equal
6841             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6842            (setq result (car arts)
6843                  arts nil))
6844       (setq arts (cdr arts)))
6845     (and result
6846          (goto-char (gnus-data-pos result))
6847          (gnus-data-number result))))
6848
6849 (defun gnus-summary-search-forward (&optional unread subject backward)
6850   "Search forward for an article.
6851 If UNREAD, look for unread articles.  If SUBJECT, look for
6852 articles with that subject.  If BACKWARD, search backward instead."
6853   (cond (subject (gnus-summary-find-subject subject unread backward))
6854         (backward (gnus-summary-find-prev unread))
6855         (t (gnus-summary-find-next unread))))
6856
6857 (defun gnus-recenter (&optional n)
6858   "Center point in window and redisplay frame.
6859 Also do horizontal recentering."
6860   (interactive "P")
6861   (when (and gnus-auto-center-summary
6862              (not (eq gnus-auto-center-summary 'vertical)))
6863     (gnus-horizontal-recenter))
6864   (if (fboundp 'recenter-top-bottom)
6865       (recenter-top-bottom n)
6866     (recenter n)))
6867
6868 (put 'gnus-recenter 'isearch-scroll t)
6869
6870 (defun gnus-forward-line-ignore-invisible (n)
6871   "Move N lines forward (backward if N is negative).
6872 Like forward-line, but skip over (and don't count) invisible lines."
6873   (let (done)
6874     (while (and (> n 0) (not done))
6875       ;; If the following character is currently invisible,
6876       ;; skip all characters with that same `invisible' property value.
6877       (while (gnus-invisible-p (point))
6878         (goto-char (gnus-next-char-property-change (point))))
6879       (forward-line 1)
6880       (if (eobp)
6881           (setq done t)
6882         (setq n (1- n))))
6883     (while (and (< n 0) (not done))
6884       (forward-line -1)
6885       (if (bobp) (setq done t)
6886         (setq n (1+ n))
6887         (while (and (not (bobp)) (gnus-invisible-p (1- (point))))
6888           (goto-char (gnus-previous-char-property-change (point))))))))
6889
6890 (defun gnus-summary-recenter ()
6891   "Center point in the summary window.
6892 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6893 displayed, no centering will be performed."
6894   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6895   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6896   (interactive)
6897   ;; The user has to want it.
6898   (when gnus-auto-center-summary
6899     (let* ((top (cond ((< (window-height) 4) 0)
6900                       ((< (window-height) 7) 1)
6901                       (t (if (numberp gnus-auto-center-summary)
6902                              gnus-auto-center-summary
6903                            (/ (1- (window-height)) 2)))))
6904            (height (1- (window-height)))
6905            (bottom (save-excursion
6906                      (goto-char (point-max))
6907                      (gnus-forward-line-ignore-invisible (- height))
6908                      (point)))
6909            (window (get-buffer-window (current-buffer))))
6910       (when (get-buffer-window gnus-article-buffer)
6911         ;; Only do recentering when the article buffer is displayed,
6912         ;; Set the window start to either `bottom', which is the biggest
6913         ;; possible valid number, or the second line from the top,
6914         ;; whichever is the least.
6915         (let ((top-pos (save-excursion
6916                          (gnus-forward-line-ignore-invisible (- top))
6917                          (point))))
6918           (if (> bottom top-pos)
6919               ;; Keep the second line from the top visible
6920               (set-window-start window top-pos)
6921             ;; Try to keep the bottom line visible; if it's partially
6922             ;; obscured, either scroll one more line to make it fully
6923             ;; visible, or revert to using TOP-POS.
6924             (save-excursion
6925               (goto-char (point-max))
6926               (gnus-forward-line-ignore-invisible -1)
6927               (let ((last-line-start (point)))
6928                 (goto-char bottom)
6929                 (set-window-start window (point) t)
6930                 (when (not (pos-visible-in-window-p last-line-start window))
6931                   (gnus-forward-line-ignore-invisible 1)
6932                   (set-window-start window (min (point) top-pos) t)))))))
6933       ;; Do horizontal recentering while we're at it.
6934       (when (and (get-buffer-window (current-buffer) t)
6935                  (not (eq gnus-auto-center-summary 'vertical)))
6936         (let ((selected (selected-window)))
6937           (select-window (get-buffer-window (current-buffer) t))
6938           (gnus-summary-position-point)
6939           (gnus-horizontal-recenter)
6940           (select-window selected))))))
6941
6942 (defun gnus-summary-jump-to-group (newsgroup)
6943   "Move point to NEWSGROUP in group mode buffer."
6944   ;; Keep update point of group mode buffer if visible.
6945   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6946       (save-window-excursion
6947         ;; Take care of tree window mode.
6948         (when (get-buffer-window gnus-group-buffer)
6949           (pop-to-buffer gnus-group-buffer))
6950         (gnus-group-jump-to-group newsgroup))
6951     (save-excursion
6952       ;; Take care of tree window mode.
6953       (if (get-buffer-window gnus-group-buffer 0)
6954           (pop-to-buffer gnus-group-buffer)
6955         (set-buffer gnus-group-buffer))
6956       (gnus-group-jump-to-group newsgroup))))
6957
6958 ;; This function returns a list of article numbers based on the
6959 ;; difference between the ranges of read articles in this group and
6960 ;; the range of active articles.
6961 (defun gnus-list-of-unread-articles (group)
6962   (let* ((read (gnus-info-read (gnus-get-info group)))
6963          (active (or (gnus-active group) (gnus-activate-group group)))
6964          (last (or (cdr active)
6965                    (error "Group %s couldn't be activated " group)))
6966          (bottom (if gnus-newsgroup-maximum-articles
6967                      (max (car active)
6968                           (- last gnus-newsgroup-maximum-articles -1))
6969                    (car active)))
6970          first nlast unread)
6971     ;; If none are read, then all are unread.
6972     (if (not read)
6973         (setq first bottom)
6974       ;; If the range of read articles is a single range, then the
6975       ;; first unread article is the article after the last read
6976       ;; article.  Sounds logical, doesn't it?
6977       (if (and (not (listp (cdr read)))
6978                (or (< (car read) bottom)
6979                    (progn (setq read (list read))
6980                           nil)))
6981           (setq first (max bottom (1+ (cdr read))))
6982         ;; `read' is a list of ranges.
6983         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6984                                   (caar read)))
6985                   1)
6986           (setq first bottom))
6987         (while read
6988           (when first
6989             (while (< first nlast)
6990               (setq unread (cons first unread)
6991                     first (1+ first))))
6992           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6993           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6994           (setq read (cdr read)))))
6995     ;; And add the last unread articles.
6996     (while (<= first last)
6997       (setq unread (cons first unread)
6998             first (1+ first)))
6999     ;; Return the list of unread articles.
7000     (delq 0 (nreverse unread))))
7001
7002 (defun gnus-list-of-read-articles (group)
7003   "Return a list of unread, unticked and non-dormant articles."
7004   (let* ((info (gnus-get-info group))
7005          (marked (gnus-info-marks info))
7006          (active (gnus-active group)))
7007     (and info active
7008          (gnus-list-range-difference
7009           (gnus-list-range-difference
7010            (gnus-sorted-complement
7011             (gnus-uncompress-range
7012              (if gnus-newsgroup-maximum-articles
7013                  (cons (max (car active)
7014                             (- (cdr active)
7015                                gnus-newsgroup-maximum-articles
7016                                -1))
7017                        (cdr active))
7018                active))
7019             (gnus-list-of-unread-articles group))
7020            (cdr (assq 'dormant marked)))
7021           (cdr (assq 'tick marked))))))
7022
7023 ;; This function returns a sequence of article numbers based on the
7024 ;; difference between the ranges of read articles in this group and
7025 ;; the range of active articles.
7026 (defun gnus-sequence-of-unread-articles (group)
7027   (let* ((read (gnus-info-read (gnus-get-info group)))
7028          (active (or (gnus-active group) (gnus-activate-group group)))
7029          (last (cdr active))
7030          (bottom (if gnus-newsgroup-maximum-articles
7031                      (max (car active)
7032                           (- last gnus-newsgroup-maximum-articles -1))
7033                    (car active)))
7034          first nlast unread)
7035     ;; If none are read, then all are unread.
7036     (if (not read)
7037         (setq first bottom)
7038       ;; If the range of read articles is a single range, then the
7039       ;; first unread article is the article after the last read
7040       ;; article.  Sounds logical, doesn't it?
7041       (if (and (not (listp (cdr read)))
7042                (or (< (car read) bottom)
7043                    (progn (setq read (list read))
7044                           nil)))
7045           (setq first (max bottom (1+ (cdr read))))
7046         ;; `read' is a list of ranges.
7047         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
7048                                   (caar read)))
7049                   1)
7050           (setq first bottom))
7051         (while read
7052           (when first
7053             (push (cons first nlast) unread))
7054           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
7055           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
7056           (setq read (cdr read)))))
7057     ;; And add the last unread articles.
7058     (cond ((not (and first last))
7059            nil)
7060           ((< first last)
7061            (push (cons first last) unread))
7062           ((= first last)
7063            (push first unread)))
7064     ;; Return the sequence of unread articles.
7065     (delq 0 (nreverse unread))))
7066
7067 ;; Various summary commands
7068
7069 (defun gnus-summary-select-article-buffer ()
7070   "Reconfigure windows to show the article buffer.
7071 If `gnus-widen-article-window' is set, show only the article
7072 buffer."
7073   (interactive)
7074   (if (not (gnus-buffer-live-p gnus-article-buffer))
7075       (error "There is no article buffer for this summary buffer")
7076     (unless (get-buffer-window gnus-article-buffer)
7077       (gnus-summary-show-article))
7078     (gnus-configure-windows
7079      (if gnus-widen-article-window
7080          'only-article
7081        'article)
7082      t)
7083     (select-window (get-buffer-window gnus-article-buffer))))
7084
7085 (defun gnus-summary-universal-argument (arg)
7086   "Perform any operation on all articles that are process/prefixed."
7087   (interactive "P")
7088   (let ((articles (gnus-summary-work-articles arg))
7089         func article)
7090     (if (eq
7091          (setq
7092           func
7093           (key-binding
7094            (read-key-sequence
7095             (substitute-command-keys
7096              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
7097          'undefined)
7098         (gnus-error 1 "Undefined key")
7099       (save-excursion
7100         (while articles
7101           (gnus-summary-goto-subject (setq article (pop articles)))
7102           (let (gnus-newsgroup-processable)
7103             (command-execute func))
7104           (gnus-summary-remove-process-mark article)))))
7105   (gnus-summary-position-point))
7106
7107 (defun gnus-summary-toggle-truncation (&optional arg)
7108   "Toggle truncation of summary lines.
7109 With ARG, turn line truncation on if ARG is positive."
7110   (interactive "P")
7111   (setq truncate-lines
7112         (if (null arg) (not truncate-lines)
7113           (> (prefix-numeric-value arg) 0)))
7114   (redraw-display))
7115
7116 (defun gnus-summary-find-for-reselect ()
7117   "Return the number of an article to stay on across a reselect.
7118 The current article is considered, then following articles, then previous
7119 articles.  An article is sought which is not canceled and isn't a temporary
7120 insertion from another group.  If there's no such then return a dummy 0."
7121   (let (found)
7122     (dolist (rev '(nil t))
7123       (unless found      ; don't demand the reverse list if we don't need it
7124         (let ((data (gnus-data-find-list
7125                      (gnus-summary-article-number) (gnus-data-list rev))))
7126           (while (and data (not found))
7127             (if (and (< 0 (gnus-data-number (car data)))
7128                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
7129                 (setq found (gnus-data-number (car data))))
7130             (setq data (cdr data))))))
7131     (or found 0)))
7132
7133 (defun gnus-summary-reselect-current-group (&optional all rescan)
7134   "Exit and then reselect the current newsgroup.
7135 The prefix argument ALL means to select all articles."
7136   (interactive "P")
7137   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
7138     (error "Ephemeral groups can't be reselected"))
7139   (let ((current-subject (gnus-summary-find-for-reselect))
7140         (group gnus-newsgroup-name))
7141     (setq gnus-newsgroup-begin nil)
7142     (gnus-summary-exit nil 'leave-hidden)
7143     ;; We have to adjust the point of group mode buffer because
7144     ;; point was moved to the next unread newsgroup by exiting.
7145     (gnus-summary-jump-to-group group)
7146     (when rescan
7147       (save-excursion
7148         (gnus-group-get-new-news-this-group 1)))
7149     (gnus-group-read-group all t)
7150     (gnus-summary-goto-subject current-subject nil t)))
7151
7152 (defun gnus-summary-rescan-group (&optional all)
7153   "Exit the newsgroup, ask for new articles, and select the newsgroup."
7154   (interactive "P")
7155   (let ((config gnus-current-window-configuration))
7156     (gnus-summary-reselect-current-group all t)
7157     (gnus-configure-windows config)
7158     (when (eq config 'article)
7159       (gnus-summary-select-article))))
7160
7161 (defun gnus-summary-update-info (&optional non-destructive)
7162   (save-excursion
7163     (let ((group gnus-newsgroup-name))
7164       (when group
7165         (when gnus-newsgroup-kill-headers
7166           (setq gnus-newsgroup-killed
7167                 (gnus-compress-sequence
7168                  (gnus-sorted-union
7169                   (gnus-list-range-intersection
7170                    gnus-newsgroup-unselected gnus-newsgroup-killed)
7171                   gnus-newsgroup-unreads)
7172                  t)))
7173         (unless (listp (cdr gnus-newsgroup-killed))
7174           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
7175         (let ((headers gnus-newsgroup-headers)
7176               (ephemeral-p (gnus-ephemeral-group-p group))
7177               info)
7178           (unless ephemeral-p
7179             (setq info (copy-sequence (gnus-get-info group))
7180                   info (delq (gnus-info-params info) info)))
7181           ;; Set the new ranges of read articles.
7182           (with-current-buffer gnus-group-buffer
7183             (gnus-undo-force-boundary))
7184           (gnus-update-read-articles
7185            group (gnus-sorted-union
7186                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
7187           ;; Set the current article marks.
7188           (let ((gnus-newsgroup-scored
7189                  (if (and (not gnus-save-score)
7190                           (not non-destructive))
7191                      nil
7192                    gnus-newsgroup-scored)))
7193             (save-excursion
7194               (gnus-update-marks)))
7195           ;; Do the cross-ref thing.
7196           (when gnus-use-cross-reference
7197             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
7198           ;; Do not switch windows but change the buffer to work.
7199           (set-buffer gnus-group-buffer)
7200           (unless ephemeral-p
7201             (gnus-group-update-group
7202              group nil
7203              (equal info
7204                     (setq info (copy-sequence (gnus-get-info group))
7205                           info (delq (gnus-info-params info) info))))))))))
7206
7207 (defun gnus-summary-save-newsrc (&optional force)
7208   "Save the current number of read/marked articles in the dribble buffer.
7209 The dribble buffer will then be saved.
7210 If FORCE (the prefix), also save the .newsrc file(s)."
7211   (interactive "P")
7212   (gnus-summary-update-info t)
7213   (if force
7214       (gnus-save-newsrc-file)
7215     (gnus-dribble-save)))
7216
7217 (declare-function gnus-cache-write-active "gnus-cache" (&optional force))
7218 (declare-function gnus-article-stop-animations "gnus-art" ())
7219
7220 (defun gnus-summary-exit (&optional temporary leave-hidden)
7221   "Exit reading current newsgroup, and then return to group selection mode.
7222 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
7223   (interactive)
7224   (gnus-set-global-variables)
7225   (when (gnus-buffer-live-p gnus-article-buffer)
7226     (with-current-buffer gnus-article-buffer
7227       (mm-destroy-parts gnus-article-mime-handles)
7228       ;; Set it to nil for safety reason.
7229       (setq gnus-article-mime-handle-alist nil)
7230       (setq gnus-article-mime-handles nil)))
7231   (gnus-kill-save-kill-buffer)
7232   (gnus-async-halt-prefetch)
7233   (let* ((group gnus-newsgroup-name)
7234          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
7235          (gnus-group-is-exiting-p t)
7236          (article-buffer gnus-article-buffer)
7237          (original-article-buffer gnus-original-article-buffer)
7238          (mode major-mode)
7239          (group-point nil)
7240          (buf (current-buffer))
7241          ;; `gnus-single-article-buffer' is nil buffer-locally in
7242          ;; ephemeral group of which summary buffer will be killed,
7243          ;; but the global value may be non-nil.
7244          (single-article-buffer gnus-single-article-buffer))
7245     (unless quit-config
7246       ;; Do adaptive scoring, and possibly save score files.
7247       (when gnus-newsgroup-adaptive
7248         (gnus-score-adaptive))
7249       (when gnus-use-scoring
7250         (gnus-score-save)))
7251     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
7252     (when gnus-use-cache
7253       (gnus-cache-possibly-remove-articles)
7254       (gnus-cache-save-buffers))
7255     (gnus-async-prefetch-remove-group group)
7256     (when gnus-suppress-duplicates
7257       (gnus-dup-enter-articles))
7258     (when gnus-use-trees
7259       (gnus-tree-close))
7260     (when gnus-use-cache
7261       (gnus-cache-write-active))
7262     ;; Remove entries for this group.
7263     (nnmail-purge-split-history (gnus-group-real-name group))
7264     ;; Make all changes in this group permanent.
7265     (unless quit-config
7266       (gnus-run-hooks 'gnus-exit-group-hook)
7267       (gnus-summary-update-info))
7268     (gnus-close-group group)
7269     ;; Make sure where we were, and go to next newsgroup.
7270     (when (buffer-live-p (get-buffer gnus-group-buffer))
7271       (set-buffer gnus-group-buffer))
7272     (unless quit-config
7273       (gnus-group-jump-to-group group))
7274     (gnus-run-hooks 'gnus-summary-exit-hook)
7275     (unless (or quit-config
7276                 (not gnus-summary-next-group-on-exit)
7277                 ;; If this group has disappeared from the summary
7278                 ;; buffer, don't skip forwards.
7279                 (not (string= group (gnus-group-group-name))))
7280       (gnus-group-next-unread-group 1))
7281     (setq group-point (point))
7282     (gnus-article-stop-animations)
7283     (if temporary
7284         nil                             ;Nothing to do.
7285       (set-buffer buf)
7286       (if (not gnus-kill-summary-on-exit)
7287           (progn
7288             (gnus-deaden-summary)
7289             (setq mode nil))
7290         (when (get-buffer gnus-article-buffer)
7291           (bury-buffer gnus-article-buffer))
7292         ;; Return to group mode buffer.
7293         (when (eq mode 'gnus-summary-mode)
7294           (gnus-kill-buffer buf)))
7295
7296       (setq gnus-current-select-method gnus-select-method)
7297       (when (gnus-buffer-live-p gnus-group-buffer)
7298         (set-buffer gnus-group-buffer))
7299       (if quit-config
7300           (gnus-handle-ephemeral-exit quit-config)
7301         (goto-char group-point)
7302         ;; If gnus-group-buffer is already displayed, make sure we also move
7303         ;; the cursor in the window that displays it.
7304         (let ((win (get-buffer-window (current-buffer) 0)))
7305           (if win (set-window-point win (point))))
7306         (unless leave-hidden
7307           (gnus-configure-windows 'group 'force)))
7308
7309       ;; If we have several article buffers, we kill them at exit.
7310       (unless single-article-buffer
7311         (when (gnus-buffer-live-p article-buffer)
7312           (with-current-buffer article-buffer
7313             ;; Don't kill sticky article buffers
7314             (unless (eq major-mode 'gnus-sticky-article-mode)
7315               (gnus-kill-buffer article-buffer)
7316               (setq gnus-article-current nil))))
7317         (gnus-kill-buffer original-article-buffer))
7318
7319       ;; Clear the current group name.
7320       (unless quit-config
7321         (setq gnus-newsgroup-name nil)))))
7322
7323 (declare-function gnus-stop-downloads "gnus-art" ())
7324
7325 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7326 (defun gnus-summary-exit-no-update (&optional no-questions)
7327   "Quit reading current newsgroup without updating read article info."
7328   (interactive)
7329   (let* ((group gnus-newsgroup-name)
7330          (gnus-group-is-exiting-p t)
7331          (gnus-group-is-exiting-without-update-p t)
7332          (quit-config (gnus-group-quit-config group)))
7333     (when (or no-questions
7334               gnus-expert-user
7335               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7336       (gnus-async-halt-prefetch)
7337       (run-hooks 'gnus-summary-prepare-exit-hook)
7338       (when (gnus-buffer-live-p gnus-article-buffer)
7339         (with-current-buffer gnus-article-buffer
7340           (gnus-article-stop-animations)
7341           (gnus-stop-downloads)
7342           (mm-destroy-parts gnus-article-mime-handles)
7343           ;; Set it to nil for safety reason.
7344           (setq gnus-article-mime-handle-alist nil)
7345           (setq gnus-article-mime-handles nil)))
7346       ;; If we have several article buffers, we kill them at exit.
7347       (unless gnus-single-article-buffer
7348         (gnus-kill-buffer gnus-article-buffer)
7349         (gnus-kill-buffer gnus-original-article-buffer)
7350         (setq gnus-article-current nil))
7351       ;; Return to the group buffer.
7352       (if (not gnus-kill-summary-on-exit)
7353           (progn
7354             (gnus-deaden-summary)
7355             (gnus-configure-windows 'group 'force))
7356         (gnus-configure-windows 'group 'force)
7357         (gnus-close-group group)
7358         (gnus-kill-buffer gnus-summary-buffer))
7359       (unless gnus-single-article-buffer
7360         (setq gnus-article-current nil))
7361       (when gnus-use-trees
7362         (gnus-tree-close))
7363       (gnus-async-prefetch-remove-group group)
7364       (when (get-buffer gnus-article-buffer)
7365         (bury-buffer gnus-article-buffer))
7366       ;; Clear the current group name.
7367       (setq gnus-newsgroup-name nil)
7368       (unless (gnus-ephemeral-group-p group)
7369         (gnus-group-update-group group nil t))
7370       (when (equal (gnus-group-group-name) group)
7371         (gnus-group-next-unread-group 1))
7372       (gnus-article-stop-animations)
7373       (when quit-config
7374         (gnus-handle-ephemeral-exit quit-config)))))
7375
7376 (defun gnus-handle-ephemeral-exit (quit-config)
7377   "Handle movement when leaving an ephemeral group.
7378 The state which existed when entering the ephemeral is reset."
7379   (if (not (buffer-live-p (car quit-config)))
7380       (when (gnus-buffer-live-p gnus-group-buffer)
7381         (gnus-configure-windows 'group 'force))
7382     (set-buffer (car quit-config))
7383     (unless (eq (cdr quit-config) 'group)
7384       (setq gnus-current-select-method
7385             (gnus-find-method-for-group gnus-newsgroup-name)))
7386     (cond ((derived-mode-p 'gnus-summary-mode)
7387            (gnus-set-global-variables))
7388           ((derived-mode-p 'gnus-article-mode)
7389            (save-current-buffer
7390              ;; The `gnus-summary-buffer' variable may point
7391              ;; to the old summary buffer when using a single
7392              ;; article buffer.
7393              (unless (gnus-buffer-live-p gnus-summary-buffer)
7394                (set-buffer gnus-group-buffer))
7395              (set-buffer gnus-summary-buffer)
7396              (gnus-set-global-variables))))
7397     (if (or (eq (cdr quit-config) 'article)
7398             (eq (cdr quit-config) 'pick))
7399         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7400             (gnus-configure-windows 'pick 'force)
7401           (gnus-configure-windows (cdr quit-config) 'force))
7402       (gnus-configure-windows (cdr quit-config) 'force))
7403     (when (derived-mode-p 'gnus-summary-mode)
7404       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7405                                                      next-unread-noselect))
7406           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7407                                   'next-noselect)
7408                               (gnus-summary-next-subject 1 nil t))
7409                              ((eq gnus-auto-select-on-ephemeral-exit
7410                                   'next-unread-noselect)
7411                               (gnus-summary-next-subject 1 t t))))
7412             ;; Hide the article buffer which displays the article different
7413             ;; from the one that the cursor points to in the summary buffer.
7414             (gnus-configure-windows 'summary 'force))
7415         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7416                (gnus-summary-next-subject 1))
7417               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7418                (gnus-summary-next-subject 1 t))))
7419       (gnus-summary-recenter)
7420       (gnus-summary-position-point))))
7421
7422 ;;; Dead summaries.
7423
7424 (defvar gnus-dead-summary-mode-map
7425   (let ((map (make-keymap)))
7426     (suppress-keymap map)
7427     (substitute-key-definition 'undefined 'gnus-summary-wake-up-the-dead map)
7428     (dolist (key '("\C-d" "\r" "\177" [delete]))
7429       (define-key map key 'gnus-summary-wake-up-the-dead))
7430     (dolist (key '("q" "Q"))
7431       (define-key map key 'bury-buffer))
7432     map))
7433
7434 (define-minor-mode gnus-dead-summary-mode
7435   "Minor mode for Gnus summary buffers."
7436   :lighter " Dead" :keymap gnus-dead-summary-mode-map
7437   (unless (derived-mode-p 'gnus-summary-mode)
7438     (setq gnus-dead-summary-mode nil)))
7439
7440 (defun gnus-deaden-summary ()
7441   "Make the current summary buffer into a dead summary buffer."
7442   ;; Kill any previous dead summary buffer.
7443   (when (and gnus-dead-summary
7444              (buffer-name gnus-dead-summary))
7445     (with-current-buffer gnus-dead-summary
7446       (when gnus-dead-summary-mode
7447         (kill-buffer (current-buffer)))))
7448   ;; Make this the current dead summary.
7449   (setq gnus-dead-summary (current-buffer))
7450   (gnus-dead-summary-mode 1)
7451   (let ((name (buffer-name)))
7452     (when (string-match "Summary" name)
7453       (rename-buffer
7454        (concat (substring name 0 (match-beginning 0)) "Dead "
7455                (substring name (match-beginning 0)))
7456        t)
7457       (bury-buffer))))
7458
7459 (defun gnus-kill-or-deaden-summary (buffer)
7460   "Kill or deaden the summary BUFFER."
7461   (save-excursion
7462     (when (and (buffer-name buffer)
7463                (not gnus-single-article-buffer))
7464       (with-current-buffer buffer
7465         (gnus-kill-buffer gnus-article-buffer)
7466         (gnus-kill-buffer gnus-original-article-buffer)))
7467     (cond
7468      ;; Kill the buffer.
7469      (gnus-kill-summary-on-exit
7470       (when (and gnus-use-trees
7471                  (gnus-buffer-exists-p buffer))
7472         (with-current-buffer buffer
7473           (gnus-tree-close)))
7474       (gnus-kill-buffer buffer))
7475      ;; Deaden the buffer.
7476      ((gnus-buffer-exists-p buffer)
7477       (with-current-buffer buffer
7478         (gnus-deaden-summary))))))
7479
7480 (defun gnus-summary-wake-up-the-dead (&rest args)
7481   "Wake up the dead summary buffer."
7482   (interactive)
7483   (gnus-dead-summary-mode -1)
7484   (let ((name (buffer-name)))
7485     (when (string-match "Dead " name)
7486       (rename-buffer
7487        (concat (substring name 0 (match-beginning 0))
7488                (substring name (match-end 0)))
7489        t)))
7490   (gnus-message 3 "This dead summary is now alive again"))
7491
7492 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7493 (defun gnus-summary-describe-group (&optional force)
7494   "Describe the current newsgroup."
7495   (interactive "P")
7496   (gnus-group-describe-group force gnus-newsgroup-name))
7497
7498 (defun gnus-summary-describe-briefly ()
7499   "Describe summary mode commands briefly."
7500   (interactive)
7501   (gnus-message 6 "%s" (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")))
7502
7503 ;; Walking around group mode buffer from summary mode.
7504
7505 (defun gnus-summary-next-group (&optional no-article target-group backward)
7506   "Exit current newsgroup and then select next unread newsgroup.
7507 If prefix argument NO-ARTICLE is non-nil, no article is selected
7508 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7509 previous group instead."
7510   (interactive "P")
7511   ;; Stop pre-fetching.
7512   (gnus-async-halt-prefetch)
7513   (let ((current-group gnus-newsgroup-name)
7514         (current-buffer (current-buffer))
7515         entered)
7516     ;; First we semi-exit this group to update Xrefs and all variables.
7517     ;; We can't do a real exit, because the window conf must remain
7518     ;; the same in case the user is prompted for info, and we don't
7519     ;; want the window conf to change before that...
7520     (gnus-summary-exit t)
7521     (while (not entered)
7522       ;; Then we find what group we are supposed to enter.
7523       (set-buffer gnus-group-buffer)
7524       (gnus-group-jump-to-group current-group)
7525       (setq target-group
7526             (or target-group
7527                 (if (eq gnus-keep-same-level 'best)
7528                     (gnus-summary-best-group gnus-newsgroup-name)
7529                   (gnus-summary-search-group backward gnus-keep-same-level))))
7530       (if (not target-group)
7531           ;; There are no further groups, so we return to the group
7532           ;; buffer.
7533           (progn
7534             (gnus-message 5 "Returning to the group buffer")
7535             (setq entered t)
7536             (when (gnus-buffer-live-p current-buffer)
7537               (set-buffer current-buffer)
7538               (gnus-summary-exit))
7539             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7540         ;; We try to enter the target group.
7541         (gnus-group-jump-to-group target-group)
7542         (let ((unreads (gnus-group-group-unread)))
7543           (if (and (or (eq t unreads)
7544                        (and unreads (not (zerop unreads))))
7545                    (gnus-summary-read-group
7546                     target-group nil no-article
7547                     (and (buffer-name current-buffer) current-buffer)
7548                     nil backward))
7549               (setq entered t)
7550             (setq current-group target-group
7551                   target-group nil)))))))
7552
7553 (defun gnus-summary-prev-group (&optional no-article)
7554   "Exit current newsgroup and then select previous unread newsgroup.
7555 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7556   (interactive "P")
7557   (gnus-summary-next-group no-article nil t))
7558
7559 ;; Walking around summary lines.
7560
7561 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7562   "Go to the first subject satisfying any non-nil constraint.
7563 If UNREAD is non-nil, the article should be unread.
7564 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7565 If UNSEEN is non-nil, the article should be unseen as well as unread.
7566 Returns the article selected or nil if there are no matching articles."
7567   (interactive "P")
7568   (cond
7569    ;; Empty summary.
7570    ((null gnus-newsgroup-data)
7571     (gnus-message 3 "No articles in the group")
7572     nil)
7573    ;; Pick the first article.
7574    ((not (or unread undownloaded unseen))
7575     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7576     (gnus-data-number (car gnus-newsgroup-data)))
7577    ;; Find the first unread article.
7578    (t
7579     (let ((data gnus-newsgroup-data))
7580       (while (and data
7581                   (let ((num (gnus-data-number (car data))))
7582                     (or (memq num gnus-newsgroup-unfetched)
7583                         (not (or (and unread
7584                                       (memq num gnus-newsgroup-unreads))
7585                                  (and undownloaded
7586                                       (memq num gnus-newsgroup-undownloaded))
7587                                  (and unseen
7588                                       (memq num gnus-newsgroup-unseen)
7589                                       (memq num gnus-newsgroup-unreads)))))))
7590         (setq data (cdr data)))
7591       (prog1
7592           (if data
7593               (progn
7594                 (goto-char (gnus-data-pos (car data)))
7595                 (gnus-data-number (car data)))
7596             (gnus-message 3 "No more%s articles"
7597                           (let* ((r (when unread " unread"))
7598                                  (d (when undownloaded " undownloaded"))
7599                                  (s (when unseen " unseen"))
7600                                  (l (delq nil (list r d s))))
7601                             (cond ((= 3 (length l))
7602                                    (concat r "," d ", or" s))
7603                                   ((= 2 (length l))
7604                                    (concat (car l) ", or" (cadr l)))
7605                                   ((= 1 (length l))
7606                                    (car l))
7607                                   (t
7608                                    ""))))
7609             nil
7610             )
7611         (gnus-summary-position-point))))))
7612
7613 (defun gnus-summary-next-subject (n &optional unread dont-display)
7614   "Go to next N'th summary line.
7615 If N is negative, go to the previous N'th subject line.
7616 If UNREAD is non-nil, only unread articles are selected.
7617 The difference between N and the actual number of steps taken is
7618 returned."
7619   (interactive "p")
7620   (let ((backward (< n 0))
7621         (n (abs n)))
7622     (while (and (> n 0)
7623                 (if backward
7624                     (gnus-summary-find-prev unread)
7625                   (gnus-summary-find-next unread)))
7626       (unless (zerop (setq n (1- n)))
7627         (gnus-summary-show-thread)))
7628     (when (/= 0 n)
7629       (gnus-message 7 "No more%s articles"
7630                     (if unread " unread" "")))
7631     (unless dont-display
7632       (gnus-summary-recenter)
7633       (gnus-summary-position-point))
7634     n))
7635
7636 (defun gnus-summary-next-unread-subject (n)
7637   "Go to next N'th unread summary line."
7638   (interactive "p")
7639   (gnus-summary-next-subject n t))
7640
7641 (defun gnus-summary-prev-subject (n &optional unread)
7642   "Go to previous N'th summary line.
7643 If optional argument UNREAD is non-nil, only unread article is selected."
7644   (interactive "p")
7645   (gnus-summary-next-subject (- n) unread))
7646
7647 (defun gnus-summary-prev-unread-subject (n)
7648   "Go to previous N'th unread summary line."
7649   (interactive "p")
7650   (gnus-summary-next-subject (- n) t))
7651
7652 (defun gnus-summary-goto-subjects (articles)
7653   "Insert the subject header for ARTICLES in the current buffer."
7654   (save-excursion
7655     (dolist (article articles)
7656       (gnus-summary-goto-subject article t)))
7657   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7658   (gnus-summary-position-point))
7659
7660 (defun gnus-summary-goto-subject (article &optional force silent)
7661   "Go to the subject line of ARTICLE.
7662 If FORCE, also allow jumping to articles not currently shown."
7663   (interactive "nArticle number: ")
7664   (unless (numberp article)
7665     (error "Article %s is not a number" article))
7666   (let ((b (point))
7667         (data (gnus-data-find article)))
7668     ;; We read in the article if we have to.
7669     (and (not data)
7670          force
7671          (gnus-summary-insert-subject
7672           article
7673           (if (or (numberp force) (vectorp force)) force)
7674           t)
7675          (setq data (gnus-data-find article)))
7676     (goto-char b)
7677     (if (not data)
7678         (progn
7679           (unless silent
7680             (gnus-message 3 "Can't find article %d" article))
7681           nil)
7682       (let ((pt (gnus-data-pos data)))
7683         (goto-char pt)
7684         (gnus-summary-set-article-display-arrow pt))
7685       (gnus-summary-position-point)
7686       article)))
7687
7688 ;; Walking around summary lines with displaying articles.
7689
7690 (defun gnus-summary-expand-window (&optional arg)
7691   "Make the summary buffer take up the entire Emacs frame.
7692 Given a prefix, will force an `article' buffer configuration."
7693   (interactive "P")
7694   (if arg
7695       (gnus-configure-windows 'article 'force)
7696     (gnus-configure-windows 'summary 'force)))
7697
7698 (defun gnus-summary-display-article (article &optional all-header)
7699   "Display ARTICLE in article buffer."
7700   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7701                (with-current-buffer gnus-article-buffer
7702                  (derived-mode-p 'gnus-article-mode)))
7703     (gnus-article-setup-buffer))
7704   (gnus-set-global-variables)
7705   (with-current-buffer gnus-article-buffer
7706     (setq gnus-article-charset gnus-newsgroup-charset)
7707     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7708     (mm-enable-multibyte))
7709   (if (null article)
7710       nil
7711     (prog1
7712         (if gnus-summary-display-article-function
7713             (funcall gnus-summary-display-article-function article all-header)
7714           (gnus-article-prepare article all-header))
7715       (gnus-run-hooks 'gnus-select-article-hook)
7716       (when (and gnus-current-article
7717                  (not (zerop gnus-current-article)))
7718         (gnus-summary-goto-subject gnus-current-article))
7719       (gnus-summary-recenter)
7720       (when (and gnus-use-trees gnus-show-threads)
7721         (gnus-possibly-generate-tree article)
7722         (gnus-highlight-selected-tree article))
7723       ;; Successfully display article.
7724       (gnus-article-set-window-start
7725        (cdr (assq article gnus-newsgroup-bookmarks))))))
7726
7727 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7728   "Select the current article.
7729 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7730 non-nil, the article will be re-fetched even if it already present in
7731 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7732 be displayed."
7733   ;; Make sure we are in the summary buffer to work around bbdb bug.
7734   (unless (derived-mode-p 'gnus-summary-mode)
7735     (set-buffer gnus-summary-buffer))
7736   (let ((article (or article (gnus-summary-article-number)))
7737         (all-headers (not (not all-headers))) ;Must be t or nil.
7738         gnus-summary-display-article-function)
7739     (and (not pseudo)
7740          (gnus-summary-article-pseudo-p article)
7741          (error "This is a pseudo-article"))
7742     (with-current-buffer gnus-summary-buffer
7743       (if (or (and gnus-single-article-buffer
7744                    (or (null gnus-current-article)
7745                        (null gnus-article-current)
7746                        (null (get-buffer gnus-article-buffer))
7747                        (not (eq article (cdr gnus-article-current)))
7748                        (not (equal (car gnus-article-current)
7749                                    gnus-newsgroup-name))
7750                        (not (get-buffer gnus-original-article-buffer))))
7751               (and (not gnus-single-article-buffer)
7752                    (or (null gnus-current-article)
7753                        (not (get-buffer gnus-original-article-buffer))
7754                        (not (eq gnus-current-article article))))
7755               force)
7756           ;; The requested article is different from the current article.
7757           (progn
7758             (gnus-summary-display-article article all-headers)
7759             (when (gnus-buffer-live-p gnus-article-buffer)
7760               (with-current-buffer gnus-article-buffer
7761                 (if (not gnus-article-decoded-p) ;; a local variable
7762                     (mm-disable-multibyte))))
7763             (gnus-article-set-window-start
7764              (cdr (assq article gnus-newsgroup-bookmarks)))
7765             article)
7766         'old))))
7767
7768 (defun gnus-summary-force-verify-and-decrypt ()
7769   "Display buttons for signed/encrypted parts and verify/decrypt them."
7770   (interactive)
7771   (let ((mm-verify-option 'known)
7772         (mm-decrypt-option 'known)
7773         (gnus-article-emulate-mime t)
7774         (gnus-buttonized-mime-types (append (list "multipart/signed"
7775                                                   "multipart/encrypted")
7776                                             gnus-buttonized-mime-types)))
7777     (gnus-summary-select-article nil 'force)))
7778
7779 (defun gnus-summary-next-article (&optional unread subject backward push)
7780   "Select the next article.
7781 If UNREAD, only unread articles are selected.
7782 If SUBJECT, only articles with SUBJECT are selected.
7783 If BACKWARD, the previous article is selected instead of the next."
7784   (interactive "P")
7785   ;; Make sure we are in the summary buffer.
7786   (unless (derived-mode-p 'gnus-summary-mode)
7787     (set-buffer gnus-summary-buffer))
7788   (cond
7789    ;; Is there such an article?
7790    ((and (gnus-summary-search-forward unread subject backward)
7791          (or (gnus-summary-display-article (gnus-summary-article-number))
7792              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7793     (gnus-summary-position-point))
7794    ;; If not, we try the first unread, if that is wanted.
7795    ((and subject
7796          gnus-auto-select-same
7797          (gnus-summary-first-unread-article))
7798     (gnus-summary-position-point)
7799     (gnus-message 6 "Wrapped"))
7800    ;; Try to get next/previous article not displayed in this group.
7801    ((and gnus-auto-extend-newsgroup
7802          (not unread) (not subject))
7803     (gnus-summary-goto-article
7804      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7805      nil (count-lines (point-min) (point))))
7806    ;; Go to next/previous group.
7807    (t
7808     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7809       (gnus-summary-jump-to-group gnus-newsgroup-name))
7810     (let ((cmd (if (featurep 'xemacs)
7811                    last-command-char
7812                  last-command-event))
7813           (point
7814            (with-current-buffer gnus-group-buffer
7815              (point)))
7816           (current-summary (current-buffer))
7817           (group
7818            (if (eq gnus-keep-same-level 'best)
7819                (gnus-summary-best-group gnus-newsgroup-name)
7820              (gnus-summary-search-group backward gnus-keep-same-level))))
7821       ;; Select next unread newsgroup automagically.
7822       (cond
7823        ((or (not gnus-auto-select-next)
7824             (not cmd))
7825         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7826        ((or (eq gnus-auto-select-next 'quietly)
7827             (and (eq gnus-auto-select-next 'slightly-quietly)
7828                  push)
7829             (and (eq gnus-auto-select-next 'almost-quietly)
7830                  (gnus-summary-last-article-p)))
7831         ;; Select quietly.
7832         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7833             (gnus-summary-exit)
7834           (gnus-message 7 "No more%s articles (%s)..."
7835                         (if unread " unread" "")
7836                         (if group (concat "selecting " group)
7837                           "exiting"))
7838           (gnus-summary-next-group nil group backward)))
7839        (t
7840         (when (gnus-key-press-event-p last-input-event)
7841           ;; Somehow or other, we may now have selected a different
7842           ;; window.  Make point go back to the summary buffer.
7843           (when (eq current-summary (current-buffer))
7844             ;; FIXME: This burps when get-buffer-window returns nil.
7845             (select-window (get-buffer-window current-summary 0)))
7846           (gnus-summary-walk-group-buffer
7847            gnus-newsgroup-name cmd unread backward point))))))))
7848
7849 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7850   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7851                       (?\C-p (gnus-group-prev-unread-group 1))))
7852         (cursor-in-echo-area t)
7853         keve key group ended prompt)
7854     (with-current-buffer gnus-group-buffer
7855       (goto-char start)
7856       (setq group
7857             (if (eq gnus-keep-same-level 'best)
7858                 (gnus-summary-best-group gnus-newsgroup-name)
7859               (gnus-summary-search-group backward gnus-keep-same-level))))
7860     (while (not ended)
7861       (setq prompt
7862             (format
7863              "No more%s articles%s " (if unread " unread" "")
7864              (if (and group
7865                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7866                  (format " (Type %s for %s [%s])"
7867                          (single-key-description cmd)
7868                          (gnus-group-decoded-name group)
7869                          (gnus-group-unread group))
7870                (format " (Type %s to exit %s)"
7871                        (single-key-description cmd)
7872                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7873       ;; Confirm auto selection.
7874       (setq key (car (setq keve (gnus-read-event-char prompt)))
7875             ended t)
7876       (cond
7877        ((assq key keystrokes)
7878         (let ((obuf (current-buffer)))
7879           (switch-to-buffer gnus-group-buffer)
7880           (when group
7881             (gnus-group-jump-to-group group))
7882           (eval (cadr (assq key keystrokes)))
7883           (setq group (gnus-group-group-name))
7884           (switch-to-buffer obuf))
7885         (setq ended nil))
7886        ((equal key cmd)
7887         (if (or (not group)
7888                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7889             (gnus-summary-exit)
7890           (gnus-summary-next-group nil group backward)))
7891        (t
7892         (push (cdr keve) unread-command-events))))))
7893
7894 (defun gnus-summary-next-unread-article ()
7895   "Select unread article after current one."
7896   (interactive)
7897   (gnus-summary-next-article
7898    (or (not (eq gnus-summary-goto-unread 'never))
7899        (gnus-summary-last-article-p (gnus-summary-article-number)))
7900    (and gnus-auto-select-same
7901         (gnus-summary-article-subject))))
7902
7903 (defun gnus-summary-prev-article (&optional unread subject)
7904   "Select the article before the current one.
7905 If UNREAD is non-nil, only unread articles are selected."
7906   (interactive "P")
7907   (gnus-summary-next-article unread subject t))
7908
7909 (defun gnus-summary-prev-unread-article ()
7910   "Select unread article before current one."
7911   (interactive)
7912   (gnus-summary-prev-article
7913    (or (not (eq gnus-summary-goto-unread 'never))
7914        (gnus-summary-first-article-p (gnus-summary-article-number)))
7915    (and gnus-auto-select-same
7916         (gnus-summary-article-subject))))
7917
7918 (declare-function gnus-article-only-boring-p "gnus-art" ())
7919
7920 (defun gnus-summary-next-page (&optional lines circular stop)
7921   "Show next page of the selected article.
7922 If at the end of the current article, select the next article.
7923 LINES says how many lines should be scrolled up.
7924
7925 If CIRCULAR is non-nil, go to the start of the article instead of
7926 selecting the next article when reaching the end of the current
7927 article.
7928
7929 If STOP is non-nil, just stop when reaching the end of the message.
7930
7931 Also see the variable `gnus-article-skip-boring'."
7932   (interactive "P")
7933   (gnus-set-global-variables)
7934   (let ((article (gnus-summary-article-number))
7935         (article-window (get-buffer-window gnus-article-buffer t))
7936         endp)
7937     ;; If the buffer is empty, we have no article.
7938     (unless article
7939       (error "No article to select"))
7940     (gnus-configure-windows 'article)
7941     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7942         (if (and (eq gnus-summary-goto-unread 'never)
7943                  (not (gnus-summary-last-article-p article)))
7944             (gnus-summary-next-article)
7945           (gnus-summary-next-unread-article))
7946       (if (or (null gnus-current-article)
7947               (null gnus-article-current)
7948               (/= article (cdr gnus-article-current))
7949               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7950           ;; Selected subject is different from current article's.
7951           (gnus-summary-display-article article)
7952         (when article-window
7953           (gnus-eval-in-buffer-window gnus-article-buffer
7954             (setq endp (or (gnus-article-next-page lines)
7955                            (gnus-article-only-boring-p))))
7956           (when endp
7957             (cond ((or stop gnus-summary-stop-at-end-of-message)
7958                    (gnus-message 3 "End of message"))
7959                   (circular
7960                    (gnus-summary-beginning-of-article))
7961                   (lines
7962                    (gnus-message 3 "End of message"))
7963                   ((null lines)
7964                    (if (and (eq gnus-summary-goto-unread 'never)
7965                             (not (gnus-summary-last-article-p article)))
7966                        (gnus-summary-next-article)
7967                      (gnus-summary-next-unread-article))))))))
7968     (gnus-summary-recenter)
7969     (gnus-summary-position-point)))
7970
7971 (defun gnus-summary-prev-page (&optional lines move)
7972   "Show previous page of selected article.
7973 Argument LINES specifies lines to be scrolled down.
7974 If MOVE, move to the previous unread article if point is at
7975 the beginning of the buffer."
7976   (interactive "P")
7977   (let ((article (gnus-summary-article-number))
7978         (article-window (get-buffer-window gnus-article-buffer t))
7979         endp)
7980     (gnus-configure-windows 'article)
7981     (if (or (null gnus-current-article)
7982             (null gnus-article-current)
7983             (/= article (cdr gnus-article-current))
7984             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7985         ;; Selected subject is different from current article's.
7986         (gnus-summary-display-article article)
7987       (gnus-summary-recenter)
7988       (when article-window
7989         (gnus-eval-in-buffer-window gnus-article-buffer
7990           (setq endp (gnus-article-prev-page lines)))
7991         (when (and move endp)
7992           (cond (lines
7993                  (gnus-message 3 "Beginning of message"))
7994                 ((null lines)
7995                  (if (and (eq gnus-summary-goto-unread 'never)
7996                           (not (gnus-summary-first-article-p article)))
7997                      (gnus-summary-prev-article)
7998                    (gnus-summary-prev-unread-article))))))))
7999   (gnus-summary-position-point))
8000
8001 (defun gnus-summary-prev-page-or-article (&optional lines)
8002   "Show previous page of selected article.
8003 Argument LINES specifies lines to be scrolled down.
8004 If at the beginning of the article, go to the next article."
8005   (interactive "P")
8006   (gnus-summary-prev-page lines t))
8007
8008 (defun gnus-summary-scroll-up (lines)
8009   "Scroll up (or down) one line current article.
8010 Argument LINES specifies lines to be scrolled up (or down if negative).
8011 If no article is selected, then the current article will be selected first."
8012   (interactive "p")
8013   (gnus-configure-windows 'article)
8014   (gnus-summary-show-thread)
8015   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
8016     (gnus-eval-in-buffer-window gnus-article-buffer
8017       (cond ((> lines 0)
8018              (when (gnus-article-next-page lines)
8019                (gnus-message 3 "End of message")))
8020             ((< lines 0)
8021              (gnus-article-prev-page (- lines))))))
8022   (gnus-summary-recenter)
8023   (gnus-summary-position-point))
8024
8025 (defun gnus-summary-scroll-down (lines)
8026   "Scroll down (or up) one line current article.
8027 Argument LINES specifies lines to be scrolled down (or up if negative).
8028 If no article is selected, then the current article will be selected first."
8029   (interactive "p")
8030   (gnus-summary-scroll-up (- lines)))
8031
8032 (defun gnus-summary-next-same-subject ()
8033   "Select next article which has the same subject as current one."
8034   (interactive)
8035   (gnus-summary-next-article nil (gnus-summary-article-subject)))
8036
8037 (defun gnus-summary-prev-same-subject ()
8038   "Select previous article which has the same subject as current one."
8039   (interactive)
8040   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
8041
8042 (defun gnus-summary-next-unread-same-subject ()
8043   "Select next unread article which has the same subject as current one."
8044   (interactive)
8045   (gnus-summary-next-article t (gnus-summary-article-subject)))
8046
8047 (defun gnus-summary-prev-unread-same-subject ()
8048   "Select previous unread article which has the same subject as current one."
8049   (interactive)
8050   (gnus-summary-prev-article t (gnus-summary-article-subject)))
8051
8052 (defun gnus-summary-first-unread-article ()
8053   "Select the first unread article.
8054 Return nil if there are no unread articles."
8055   (interactive)
8056   (prog1
8057       (when (gnus-summary-first-subject t)
8058         (gnus-summary-show-thread)
8059         (gnus-summary-first-subject t)
8060         (gnus-summary-display-article (gnus-summary-article-number)))
8061     (gnus-summary-position-point)))
8062
8063 (defun gnus-summary-first-unread-subject ()
8064   "Place the point on the subject line of the first unread article.
8065 Return nil if there are no unread articles."
8066   (interactive)
8067   (prog1
8068       (when (gnus-summary-first-subject t)
8069         (gnus-summary-show-thread)
8070         (gnus-summary-first-subject t))
8071     (gnus-summary-position-point)))
8072
8073 (defun gnus-summary-first-unseen-subject ()
8074   "Place the point on the subject line of the first unseen article.
8075 Return nil if there are no unseen articles."
8076   (interactive)
8077   (prog1
8078       (when (gnus-summary-first-subject nil nil t)
8079         (gnus-summary-show-thread)
8080         (gnus-summary-first-subject nil nil t))
8081     (gnus-summary-position-point)))
8082
8083 (defun gnus-summary-first-unseen-or-unread-subject ()
8084   "Place the point on the subject line of the first unseen and unread article.
8085 If all article have been seen, on the subject line of the first unread
8086 article."
8087   (interactive)
8088   (prog1
8089       (unless (when (gnus-summary-first-subject nil nil t)
8090                 (gnus-summary-show-thread)
8091                 (gnus-summary-first-subject nil nil t))
8092         (when (gnus-summary-first-subject t)
8093           (gnus-summary-show-thread)
8094           (gnus-summary-first-subject t)))
8095     (gnus-summary-position-point)))
8096
8097 (defun gnus-summary-first-article ()
8098   "Select the first article.
8099 Return nil if there are no articles."
8100   (interactive)
8101   (prog1
8102       (when (gnus-summary-first-subject)
8103         (gnus-summary-show-thread)
8104         (gnus-summary-first-subject)
8105         (gnus-summary-display-article (gnus-summary-article-number)))
8106     (gnus-summary-position-point)))
8107
8108 (defun gnus-summary-best-unread-article (&optional arg)
8109   "Select the unread article with the highest score.
8110 If given a prefix argument, select the next unread article that has a
8111 score higher than the default score."
8112   (interactive "P")
8113   (let ((article (if arg
8114                      (gnus-summary-better-unread-subject)
8115                    (gnus-summary-best-unread-subject))))
8116     (if article
8117         (gnus-summary-goto-article article)
8118       (error "No unread articles"))))
8119
8120 (defun gnus-summary-best-unread-subject ()
8121   "Select the unread subject with the highest score."
8122   (interactive)
8123   (let ((best -1000000)
8124         (data gnus-newsgroup-data)
8125         article score)
8126     (while data
8127       (and (gnus-data-unread-p (car data))
8128            (> (setq score
8129                     (gnus-summary-article-score (gnus-data-number (car data))))
8130               best)
8131            (setq best score
8132                  article (gnus-data-number (car data))))
8133       (setq data (cdr data)))
8134     (when article
8135       (gnus-summary-goto-subject article))
8136     (gnus-summary-position-point)
8137     article))
8138
8139 (defun gnus-summary-better-unread-subject ()
8140   "Select the first unread subject that has a score over the default score."
8141   (interactive)
8142   (let ((data gnus-newsgroup-data)
8143         article score)
8144     (while (and (setq article (gnus-data-number (car data)))
8145                 (or (gnus-data-read-p (car data))
8146                     (not (> (gnus-summary-article-score article)
8147                             gnus-summary-default-score))))
8148       (setq data (cdr data)))
8149     (when article
8150       (gnus-summary-goto-subject article))
8151     (gnus-summary-position-point)
8152     article))
8153
8154 (defun gnus-summary-last-subject ()
8155   "Go to the last displayed subject line in the group."
8156   (let ((article (gnus-data-number (car (gnus-data-list t)))))
8157     (when article
8158       (gnus-summary-goto-subject article))))
8159
8160 (defun gnus-summary-goto-article (article &optional all-headers force)
8161   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
8162 If ALL-HEADERS is non-nil, no header lines are hidden.
8163 If FORCE, go to the article even if it isn't displayed.  If FORCE
8164 is a number, it is the line the article is to be displayed on."
8165   (interactive
8166    (list
8167     (gnus-completing-read
8168      "Article number or Message-ID"
8169      (mapcar 'int-to-string gnus-newsgroup-limit))
8170     current-prefix-arg
8171     t))
8172   (prog1
8173       (if (and (stringp article)
8174                (string-match "@\\|%40" article))
8175           (gnus-summary-refer-article article)
8176         (when (stringp article)
8177           (setq article (string-to-number article)))
8178         (if (gnus-summary-goto-subject article force)
8179             (gnus-summary-display-article article all-headers)
8180           (gnus-message 4 "Couldn't go to article %s" article) nil))
8181     (gnus-summary-position-point)))
8182
8183 (defun gnus-summary-goto-last-article ()
8184   "Go to the previously read article."
8185   (interactive)
8186   (prog1
8187       (when gnus-last-article
8188         (gnus-summary-goto-article gnus-last-article nil t))
8189     (gnus-summary-position-point)))
8190
8191 (defun gnus-summary-pop-article (number)
8192   "Pop one article off the history and go to the previous.
8193 NUMBER articles will be popped off."
8194   (interactive "p")
8195   (let (to)
8196     (setq gnus-newsgroup-history
8197           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
8198     (if to
8199         (gnus-summary-goto-article (car to) nil t)
8200       (error "Article history empty")))
8201   (gnus-summary-position-point))
8202
8203 ;; Summary commands and functions for limiting the summary buffer.
8204
8205 (defun gnus-summary-limit-to-articles (n)
8206   "Limit the summary buffer to the next N articles.
8207 If not given a prefix, use the process marked articles instead."
8208   (interactive "P")
8209   (prog1
8210       (let ((articles (gnus-summary-work-articles n)))
8211         (setq gnus-newsgroup-processable nil)
8212         (gnus-summary-limit articles))
8213     (gnus-summary-position-point)))
8214
8215 (defun gnus-summary-pop-limit (&optional total)
8216   "Restore the previous limit.
8217 If given a prefix, remove all limits."
8218   (interactive "P")
8219   (when total
8220     (setq gnus-newsgroup-limits
8221           (list (mapcar (lambda (h) (mail-header-number h))
8222                         gnus-newsgroup-headers))))
8223   (unless gnus-newsgroup-limits
8224     (error "No limit to pop"))
8225   (prog1
8226       (gnus-summary-limit nil 'pop)
8227     (gnus-summary-position-point)))
8228
8229 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
8230   "Limit the summary buffer to articles that have subjects that match a regexp.
8231 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
8232   (interactive
8233    (list (read-string (if current-prefix-arg
8234                           "Exclude subject (regexp): "
8235                         "Limit to subject (regexp): "))
8236          nil current-prefix-arg))
8237   (unless header
8238     (setq header "subject"))
8239   (when (not (equal "" subject))
8240     (prog1
8241         (let ((articles (gnus-summary-find-matching
8242                          (or header "subject") subject 'all nil nil
8243                          not-matching)))
8244           (unless articles
8245             (error "Found no matches for \"%s\"" subject))
8246           (gnus-summary-limit articles))
8247       (gnus-summary-position-point))))
8248
8249 (defun gnus-summary-limit-to-author (from &optional not-matching)
8250   "Limit the summary buffer to articles that have authors that match a regexp.
8251 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8252   (interactive
8253    (list (let* ((header (gnus-summary-article-header))
8254                 (default (and header (car (mail-header-parse-address
8255                                            (mail-header-from header))))))
8256            (read-string (concat (if current-prefix-arg
8257                                     "Exclude author (regexp"
8258                                   "Limit to author (regexp")
8259                                 (if default
8260                                     (concat ", default \"" default "\"): ")
8261                                   "): "))
8262                         nil nil
8263                         default))
8264          current-prefix-arg))
8265   (gnus-summary-limit-to-subject from "from" not-matching))
8266
8267 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8268   "Limit the summary buffer to articles with the given RECIPIENT.
8269
8270 If NOT-MATCHING, exclude RECIPIENT.
8271
8272 To and Cc headers are checked.  You need to include them in
8273 `nnmail-extra-headers'."
8274   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8275   (interactive
8276    (list (read-string (format "%s recipient (regexp): "
8277                               (if current-prefix-arg "Exclude" "Limit to")))
8278          current-prefix-arg))
8279   (when (not (equal "" recipient))
8280     (prog1 (let* ((to
8281                    (if (memq 'To nnmail-extra-headers)
8282                        (gnus-summary-find-matching
8283                         (cons 'extra 'To) recipient 'all nil nil
8284                         not-matching)
8285                      (gnus-message
8286                       1 "`To' isn't present in `nnmail-extra-headers'")
8287                      (sit-for 1)
8288                      nil))
8289                   (cc
8290                    (if (memq 'Cc nnmail-extra-headers)
8291                        (gnus-summary-find-matching
8292                         (cons 'extra 'Cc) recipient 'all nil nil
8293                         not-matching)
8294                      (gnus-message
8295                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8296                      (sit-for 1)
8297                      nil))
8298                   (articles
8299                    (if not-matching
8300                        ;; We need the numbers that are in both lists:
8301                        (mapcar (lambda (a)
8302                                  (and (memq a to) a))
8303                                cc)
8304                      (nconc to cc))))
8305              (unless articles
8306                (error "Found no matches for \"%s\"" recipient))
8307              (gnus-summary-limit articles))
8308       (gnus-summary-position-point))))
8309
8310 (defun gnus-summary-limit-to-address (address &optional not-matching)
8311   "Limit the summary buffer to articles with the given ADDRESS.
8312
8313 If NOT-MATCHING, exclude ADDRESS.
8314
8315 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8316 in `nnmail-extra-headers'."
8317   (interactive
8318    (list (read-string (format "%s address (regexp): "
8319                               (if current-prefix-arg "Exclude" "Limit to")))
8320          current-prefix-arg))
8321   (when (not (equal "" address))
8322     (prog1 (let* ((to
8323                    (if (memq 'To nnmail-extra-headers)
8324                        (gnus-summary-find-matching
8325                         (cons 'extra 'To) address 'all nil nil
8326                         not-matching)
8327                      (gnus-message
8328                       1 "`To' isn't present in `nnmail-extra-headers'")
8329                      (sit-for 1)
8330                      t))
8331                   (cc
8332                    (if (memq 'Cc nnmail-extra-headers)
8333                        (gnus-summary-find-matching
8334                         (cons 'extra 'Cc) address 'all nil nil
8335                         not-matching)
8336                      (gnus-message
8337                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8338                      (sit-for 1)
8339                      t))
8340                   (from
8341                    (gnus-summary-find-matching "from" address
8342                                                'all nil nil not-matching))
8343                   (articles
8344                    (if not-matching
8345                        ;; We need the numbers that are in all lists:
8346                        (if (eq cc t)
8347                            (if (eq to t)
8348                                from
8349                              (mapcar (lambda (a) (car (memq a from))) to))
8350                          (if (eq to t)
8351                              (mapcar (lambda (a) (car (memq a from))) cc)
8352                            (mapcar (lambda (a) (car (memq a from)))
8353                                    (mapcar (lambda (a) (car (memq a to)))
8354                                            cc))))
8355                      (nconc (if (eq to t) nil to)
8356                             (if (eq cc t) nil cc)
8357                             from))))
8358              (unless articles
8359                (error "Found no matches for \"%s\"" address))
8360              (gnus-summary-limit articles))
8361       (gnus-summary-position-point))))
8362
8363 (defun gnus-summary-limit-strange-charsets-predicate (header)
8364   (when (fboundp 'char-charset)
8365     (let ((string (concat (mail-header-subject header)
8366                           (mail-header-from header)))
8367           charset found)
8368       (dotimes (i (1- (length string)))
8369         (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8370         (when (string-match "unicode\\|big\\|japanese" charset)
8371           (setq found t)))
8372       found)))
8373
8374 (defun gnus-summary-limit-to-predicate (predicate)
8375   "Limit to articles where PREDICATE returns non-nil.
8376 PREDICATE will be called with the header structures of the
8377 articles."
8378   (let ((articles nil)
8379         (case-fold-search t))
8380     (dolist (header gnus-newsgroup-headers)
8381       (when (funcall predicate header)
8382         (push (mail-header-number header) articles)))
8383     (gnus-summary-limit (nreverse articles))))
8384
8385 (defun gnus-summary-limit-to-age (age &optional younger-p)
8386   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8387 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8388 articles that are younger than AGE days."
8389   (interactive
8390    (let ((younger current-prefix-arg)
8391          (days-got nil)
8392          days)
8393      (while (not days-got)
8394        (setq days (if younger
8395                       (read-string "Limit to articles younger than (in days, older when negative): ")
8396                     (read-string
8397                      "Limit to articles older than (in days, younger when negative): ")))
8398        (when (> (length days) 0)
8399          (setq days (read days)))
8400        (if (numberp days)
8401            (progn
8402              (setq days-got t)
8403              (when (< days 0)
8404                (setq younger (not younger))
8405                (setq days (* days -1))))
8406          (message "Please enter a number.")
8407          (sleep-for 1)))
8408      (list days younger)))
8409   (prog1
8410       (let ((data gnus-newsgroup-data)
8411             (cutoff (days-to-time age))
8412             articles d date is-younger)
8413         (while (setq d (pop data))
8414           (when (and (vectorp (gnus-data-header d))
8415                      (setq date (mail-header-date (gnus-data-header d))))
8416             (setq is-younger (time-less-p
8417                               (time-since (gnus-date-get-time date))
8418                               cutoff))
8419             (when (if younger-p
8420                       is-younger
8421                     (not is-younger))
8422               (push (gnus-data-number d) articles))))
8423         (gnus-summary-limit (nreverse articles)))
8424     (gnus-summary-position-point)))
8425
8426 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8427   "Limit the summary buffer to articles that match an 'extra' header."
8428   (interactive
8429    (let ((header
8430           (intern
8431            (gnus-completing-read
8432             (if current-prefix-arg
8433                 "Exclude extra header"
8434               "Limit extra header")
8435             (mapcar 'symbol-name gnus-extra-headers)
8436             t nil nil
8437             (symbol-name (car gnus-extra-headers))))))
8438      (list header
8439            (read-string (format "%s header %s (regexp): "
8440                                 (if current-prefix-arg "Exclude" "Limit to")
8441                                 header))
8442            current-prefix-arg)))
8443   (when (not (equal "" regexp))
8444     (prog1
8445         (let ((articles (gnus-summary-find-matching
8446                          (cons 'extra header) regexp 'all nil nil
8447                          not-matching)))
8448           (unless articles
8449             (error "Found no matches for \"%s\"" regexp))
8450           (gnus-summary-limit articles))
8451       (gnus-summary-position-point))))
8452
8453 (defun gnus-summary-limit-to-display-predicate ()
8454   "Limit the summary buffer to the predicated in the `display' group parameter."
8455   (interactive)
8456   (unless gnus-newsgroup-display
8457     (error "There is no `display' group parameter"))
8458   (let (articles)
8459     (dolist (gnus-number gnus-newsgroup-articles)
8460       (when (funcall gnus-newsgroup-display)
8461         (push gnus-number articles)))
8462     (gnus-summary-limit articles))
8463   (gnus-summary-position-point))
8464
8465 (defun gnus-summary-limit-to-unread (&optional all)
8466   "Limit the summary buffer to articles that are not marked as read.
8467 If ALL is non-nil, limit strictly to unread articles."
8468   (interactive "P")
8469   (if all
8470       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8471     (gnus-summary-limit-to-marks
8472      ;; Concat all the marks that say that an article is read and have
8473      ;; those removed.
8474      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8475            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8476            gnus-low-score-mark gnus-expirable-mark
8477            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8478            gnus-duplicate-mark)
8479      'reverse)))
8480
8481 (defun gnus-summary-limit-to-headers (match &optional reverse)
8482   "Limit the summary buffer to articles that have headers that match MATCH.
8483 If REVERSE (the prefix), limit to articles that don't match."
8484   (interactive "sMatch headers (regexp): \nP")
8485   (gnus-summary-limit-to-bodies match reverse t))
8486
8487 (declare-function article-goto-body "gnus-art" ())
8488
8489 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8490   "Limit the summary buffer to articles that have bodies that match MATCH.
8491 If REVERSE (the prefix), limit to articles that don't match."
8492   (interactive "sMatch body (regexp): \nP")
8493   (let ((articles nil)
8494         (gnus-select-article-hook nil)  ;Disable hook.
8495         (gnus-article-prepare-hook nil)
8496         (gnus-use-article-prefetch nil)
8497         (gnus-keep-backlog nil)
8498         (gnus-break-pages nil)
8499         (gnus-summary-display-arrow nil)
8500         (gnus-updated-mode-lines nil)
8501         (gnus-auto-center-summary nil)
8502         (gnus-display-mime-function nil))
8503     (dolist (data gnus-newsgroup-data)
8504       (let (gnus-mark-article-hook)
8505         (gnus-summary-select-article t t nil (gnus-data-number data)))
8506       (with-current-buffer gnus-article-buffer
8507         (article-goto-body)
8508         (let* ((case-fold-search t)
8509                (found (if headersp
8510                           (re-search-backward match nil t)
8511                         (re-search-forward match nil t))))
8512           (when (or (and found
8513                          (not reverse))
8514                     (and (not found)
8515                          reverse))
8516             (push (gnus-data-number data) articles)))))
8517     (if (not articles)
8518         (message "No messages matched")
8519       (gnus-summary-limit articles)))
8520   (gnus-summary-position-point))
8521
8522 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8523   "Limit the summary buffer to articles that aren't part on any thread.
8524 If THREADSP (the prefix), limit to articles that are in threads."
8525   (interactive "P")
8526   (let ((articles nil)
8527         thread-articles
8528         threads)
8529     (dolist (thread gnus-newsgroup-threads)
8530       (if (stringp (car thread))
8531           (dolist (thread (cdr thread))
8532             (push thread threads))
8533         (push thread threads)))
8534     (dolist (thread threads)
8535       (setq thread-articles (gnus-articles-in-thread thread))
8536       (when (or (and threadsp
8537                      (> (length thread-articles) 1))
8538                 (and (not threadsp)
8539                      (= (length thread-articles) 1)))
8540         (setq articles (nconc thread-articles articles))))
8541     (if (not articles)
8542         (message "No messages matched")
8543       (gnus-summary-limit articles))
8544     (gnus-summary-position-point)))
8545
8546 (defun gnus-summary-limit-to-replied (&optional unreplied)
8547   "Limit the summary buffer to replied articles.
8548 If UNREPLIED (the prefix), limit to unreplied articles."
8549   (interactive "P")
8550   (if unreplied
8551       (gnus-summary-limit
8552        (gnus-set-difference gnus-newsgroup-articles
8553         gnus-newsgroup-replied))
8554     (gnus-summary-limit gnus-newsgroup-replied))
8555   (gnus-summary-position-point))
8556
8557 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8558   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8559 If REVERSE, limit the summary buffer to articles that are marked
8560 with MARKS.  MARKS can either be a string of marks or a list of marks.
8561 Returns how many articles were removed."
8562   (interactive "sMarks: ")
8563   (gnus-summary-limit-to-marks marks t))
8564
8565 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8566   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8567 If REVERSE (the prefix), limit the summary buffer to articles that are
8568 not marked with MARKS.  MARKS can either be a string of marks or a
8569 list of marks.
8570 Returns how many articles were removed."
8571   (interactive "sMarks: \nP")
8572   (prog1
8573       (let ((data gnus-newsgroup-data)
8574             (marks (if (listp marks) marks
8575                      (append marks nil))) ; Transform to list.
8576             articles)
8577         (while data
8578           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8579                   (memq (gnus-data-mark (car data)) marks))
8580             (push (gnus-data-number (car data)) articles))
8581           (setq data (cdr data)))
8582         (gnus-summary-limit articles))
8583     (gnus-summary-position-point)))
8584
8585 (defun gnus-summary-limit-to-score (score)
8586   "Limit to articles with score at or above SCORE."
8587   (interactive "NLimit to articles with score of at least: ")
8588   (let ((data gnus-newsgroup-data)
8589         articles)
8590     (while data
8591       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8592                 score)
8593         (push (gnus-data-number (car data)) articles))
8594       (setq data (cdr data)))
8595     (prog1
8596         (gnus-summary-limit articles)
8597       (gnus-summary-position-point))))
8598
8599 (defun gnus-summary-limit-to-unseen ()
8600   "Limit to unseen articles."
8601   (interactive)
8602   (prog1
8603       (gnus-summary-limit gnus-newsgroup-unseen)
8604     (gnus-summary-position-point)))
8605
8606 (defun gnus-summary-limit-include-thread (id)
8607   "Display all the hidden articles that is in the thread with ID in it.
8608 When called interactively, ID is the Message-ID of the current
8609 article."
8610   (interactive (list (mail-header-id (gnus-summary-article-header))))
8611   (let ((articles (gnus-articles-in-thread
8612                    (gnus-id-to-thread (gnus-root-id id))))
8613         ;;we REALLY want the whole thread---this prevents cut-threads
8614         ;;from removing the thread we want to include.
8615         (gnus-fetch-old-headers nil)
8616         (gnus-build-sparse-threads nil))
8617     (prog1
8618         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8619       (gnus-summary-limit-include-matching-articles
8620        "subject"
8621        (regexp-quote (gnus-simplify-subject-re
8622                       (mail-header-subject (gnus-id-to-header id)))))
8623       (gnus-summary-position-point))))
8624
8625 (defun gnus-summary-limit-include-matching-articles (header regexp)
8626   "Display all the hidden articles that have HEADERs that match REGEXP."
8627   (interactive (list (read-string "Match on header: ")
8628                      (read-string "Regexp: ")))
8629   (let ((articles (gnus-find-matching-articles header regexp)))
8630     (prog1
8631         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8632       (gnus-summary-position-point))))
8633
8634 (defun gnus-summary-insert-dormant-articles ()
8635   "Insert all the dormant articles for this group into the current buffer."
8636   (interactive)
8637   (let ((gnus-verbose (max 6 gnus-verbose)))
8638     (if (not gnus-newsgroup-dormant)
8639         (gnus-message 3 "No dormant articles for this group")
8640       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8641
8642 (defun gnus-summary-insert-ticked-articles ()
8643   "Insert ticked articles for this group into the current buffer."
8644   (interactive)
8645   (let ((gnus-verbose (max 6 gnus-verbose)))
8646     (if (not gnus-newsgroup-marked)
8647         (gnus-message 3 "No ticked articles for this group")
8648       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8649
8650 (defun gnus-summary-limit-include-dormant ()
8651   "Display all the hidden articles that are marked as dormant.
8652 Note that this command only works on a subset of the articles currently
8653 fetched for this group."
8654   (interactive)
8655   (unless gnus-newsgroup-dormant
8656     (error "There are no dormant articles in this group"))
8657   (prog1
8658       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8659     (gnus-summary-position-point)))
8660
8661 (defun gnus-summary-include-articles (articles)
8662   "Fetch the headers for ARTICLES and then display the summary lines."
8663   (let ((gnus-inhibit-demon t)
8664         (gnus-agent nil)
8665         (gnus-read-all-available-headers t))
8666     (setq gnus-newsgroup-headers
8667           (gnus-merge
8668            'list gnus-newsgroup-headers
8669            (gnus-fetch-headers articles nil t)
8670            'gnus-article-sort-by-number))
8671     (setq gnus-newsgroup-articles
8672           (gnus-sorted-nunion gnus-newsgroup-articles articles))
8673     (gnus-summary-limit (append articles gnus-newsgroup-limit))))
8674
8675 (defun gnus-summary-limit-exclude-dormant ()
8676   "Hide all dormant articles."
8677   (interactive)
8678   (prog1
8679       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8680     (gnus-summary-position-point)))
8681
8682 (defun gnus-summary-limit-exclude-childless-dormant ()
8683   "Hide all dormant articles that have no children."
8684   (interactive)
8685   (let ((data (gnus-data-list t))
8686         articles d children)
8687     ;; Find all articles that are either not dormant or have
8688     ;; children.
8689     (while (setq d (pop data))
8690       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8691                 (and (setq children
8692                            (gnus-article-children (gnus-data-number d)))
8693                      (let (found)
8694                        (while children
8695                          (when (memq (car children) articles)
8696                            (setq children nil
8697                                  found t))
8698                          (pop children))
8699                        found)))
8700         (push (gnus-data-number d) articles)))
8701     ;; Do the limiting.
8702     (prog1
8703         (gnus-summary-limit articles)
8704       (gnus-summary-position-point))))
8705
8706 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8707   "Mark all unread excluded articles as read.
8708 If ALL, mark even excluded ticked and dormants as read."
8709   (interactive "P")
8710   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8711   (let ((articles (gnus-sorted-ndifference
8712                    (sort
8713                     (mapcar (lambda (h) (mail-header-number h))
8714                             gnus-newsgroup-headers)
8715                     '<)
8716                    gnus-newsgroup-limit))
8717         article)
8718     (setq gnus-newsgroup-unreads
8719           (gnus-sorted-intersection gnus-newsgroup-unreads
8720                                     gnus-newsgroup-limit))
8721     (if all
8722         (setq gnus-newsgroup-dormant nil
8723               gnus-newsgroup-marked nil
8724               gnus-newsgroup-reads
8725               (nconc
8726                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8727                gnus-newsgroup-reads))
8728       (while (setq article (pop articles))
8729         (unless (or (memq article gnus-newsgroup-dormant)
8730                     (memq article gnus-newsgroup-marked))
8731           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8732
8733 (defun gnus-summary-limit (articles &optional pop)
8734   (if pop
8735       ;; We pop the previous limit off the stack and use that.
8736       (setq articles (car gnus-newsgroup-limits)
8737             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8738     ;; We use the new limit, so we push the old limit on the stack.
8739     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8740   ;; Set the limit.
8741   (setq gnus-newsgroup-limit articles)
8742   (let ((total (length gnus-newsgroup-data))
8743         (data (gnus-data-find-list (gnus-summary-article-number)))
8744         (gnus-summary-mark-below nil)   ; Inhibit this.
8745         found)
8746     ;; This will do all the work of generating the new summary buffer
8747     ;; according to the new limit.
8748     (gnus-summary-prepare)
8749     ;; Hide any threads, possibly.
8750     (gnus-summary-maybe-hide-threads)
8751     ;; Try to return to the article you were at, or one in the
8752     ;; neighborhood.
8753     (when data
8754       ;; We try to find some article after the current one.
8755       (while data
8756         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8757           (setq data nil
8758                 found t))
8759         (setq data (cdr data))))
8760     (unless found
8761       ;; If there is no data, that means that we were after the last
8762       ;; article.  The same goes when we can't find any articles
8763       ;; after the current one.
8764       (goto-char (point-max))
8765       (gnus-summary-find-prev))
8766     (gnus-set-mode-line 'summary)
8767     ;; We return how many articles were removed from the summary
8768     ;; buffer as a result of the new limit.
8769     (- total (length gnus-newsgroup-data))))
8770
8771 (defsubst gnus-invisible-cut-children (threads)
8772   (let ((num 0))
8773     (while threads
8774       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8775         (incf num))
8776       (pop threads))
8777     (< num 2)))
8778
8779 (defsubst gnus-cut-thread (thread)
8780   "Go forwards in the thread until we find an article that we want to display."
8781   (when (or (eq gnus-fetch-old-headers 'some)
8782             (eq gnus-fetch-old-headers 'invisible)
8783             (numberp gnus-fetch-old-headers)
8784             (eq gnus-build-sparse-threads 'some)
8785             (eq gnus-build-sparse-threads 'more))
8786     ;; Deal with old-fetched headers and sparse threads.
8787     (while (and
8788             thread
8789             (or
8790              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8791              (gnus-summary-article-ancient-p
8792               (mail-header-number (car thread))))
8793             (if (or (<= (length (cdr thread)) 1)
8794                     (eq gnus-fetch-old-headers 'invisible))
8795                 (setq gnus-newsgroup-limit
8796                       (delq (mail-header-number (car thread))
8797                             gnus-newsgroup-limit)
8798                       thread (cadr thread))
8799               (when (gnus-invisible-cut-children (cdr thread))
8800                 (let ((th (cdr thread)))
8801                   (while th
8802                     (if (memq (mail-header-number (caar th))
8803                               gnus-newsgroup-limit)
8804                         (setq thread (car th)
8805                               th nil)
8806                       (setq th (cdr th))))))))))
8807   thread)
8808
8809 (defun gnus-cut-threads (threads)
8810   "Cut off all uninteresting articles from the beginning of THREADS."
8811   (when (or (eq gnus-fetch-old-headers 'some)
8812             (eq gnus-fetch-old-headers 'invisible)
8813             (numberp gnus-fetch-old-headers)
8814             (eq gnus-build-sparse-threads 'some)
8815             (eq gnus-build-sparse-threads 'more))
8816     (let ((th threads))
8817       (while th
8818         (setcar th (gnus-cut-thread (car th)))
8819         (setq th (cdr th)))))
8820   ;; Remove nixed out threads.
8821   (delq nil threads))
8822
8823 (defun gnus-summary-initial-limit (&optional show-if-empty)
8824   "Figure out what the initial limit is supposed to be on group entry.
8825 This entails weeding out unwanted dormants, low-scored articles,
8826 fetch-old-headers verbiage, and so on."
8827   ;; Most groups have nothing to remove.
8828   (unless (or gnus-inhibit-limiting
8829               (and (null gnus-newsgroup-dormant)
8830                    (eq gnus-newsgroup-display 'gnus-not-ignore)
8831                    (not (eq gnus-fetch-old-headers 'some))
8832                    (not (numberp gnus-fetch-old-headers))
8833                    (not (eq gnus-fetch-old-headers 'invisible))
8834                    (null gnus-summary-expunge-below)
8835                    (not (eq gnus-build-sparse-threads 'some))
8836                    (not (eq gnus-build-sparse-threads 'more))
8837                    (null gnus-thread-expunge-below)))
8838     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8839     (setq gnus-newsgroup-limit nil)
8840     (mapatoms
8841      (lambda (node)
8842        (unless (car (symbol-value node))
8843          ;; These threads have no parents -- they are roots.
8844          (let ((nodes (cdr (symbol-value node)))
8845                thread)
8846            (while nodes
8847              (if (and gnus-thread-expunge-below
8848                       (< (gnus-thread-total-score (car nodes))
8849                          gnus-thread-expunge-below))
8850                  (gnus-expunge-thread (pop nodes))
8851                (setq thread (pop nodes))
8852                (gnus-summary-limit-children thread))))))
8853      gnus-newsgroup-dependencies)
8854     ;; If this limitation resulted in an empty group, we might
8855     ;; pop the previous limit and use it instead.
8856     (when (and (not gnus-newsgroup-limit)
8857                show-if-empty)
8858       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8859     gnus-newsgroup-limit))
8860
8861 (defun gnus-summary-limit-children (thread)
8862   "Return 1 if this subthread is visible and 0 if it is not."
8863   ;; First we get the number of visible children to this thread.  This
8864   ;; is done by recursing down the thread using this function, so this
8865   ;; will really go down to a leaf article first, before slowly
8866   ;; working its way up towards the root.
8867   (when thread
8868     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8869            (children
8870            (if (cdr thread)
8871                (apply '+ (mapcar 'gnus-summary-limit-children
8872                                  (cdr thread)))
8873              0))
8874            (number (mail-header-number (car thread)))
8875            score)
8876       (if (and
8877            (not (memq number gnus-newsgroup-marked))
8878            (or
8879             ;; If this article is dormant and has absolutely no visible
8880             ;; children, then this article isn't visible.
8881             (and (memq number gnus-newsgroup-dormant)
8882                  (zerop children))
8883             ;; If this is "fetch-old-headered" and there is no
8884             ;; visible children, then we don't want this article.
8885             (and (or (eq gnus-fetch-old-headers 'some)
8886                      (numberp gnus-fetch-old-headers))
8887                  (gnus-summary-article-ancient-p number)
8888                  (zerop children))
8889             ;; If this is "fetch-old-headered" and `invisible', then
8890             ;; we don't want this article.
8891             (and (eq gnus-fetch-old-headers 'invisible)
8892                  (gnus-summary-article-ancient-p number))
8893             ;; If this is a sparsely inserted article with no children,
8894             ;; we don't want it.
8895             (and (eq gnus-build-sparse-threads 'some)
8896                  (gnus-summary-article-sparse-p number)
8897                  (zerop children))
8898             ;; If we use expunging, and this article is really
8899             ;; low-scored, then we don't want this article.
8900             (when (and gnus-summary-expunge-below
8901                        (< (setq score
8902                                 (or (cdr (assq number gnus-newsgroup-scored))
8903                                     gnus-summary-default-score))
8904                           gnus-summary-expunge-below))
8905               ;; We increase the expunge-tally here, but that has
8906               ;; nothing to do with the limits, really.
8907               (incf gnus-newsgroup-expunged-tally)
8908               ;; We also mark as read here, if that's wanted.
8909               (when (and gnus-summary-mark-below
8910                          (< score gnus-summary-mark-below))
8911                 (setq gnus-newsgroup-unreads
8912                       (delq number gnus-newsgroup-unreads))
8913                 (if gnus-newsgroup-auto-expire
8914                     (push number gnus-newsgroup-expirable)
8915                   (push (cons number gnus-low-score-mark)
8916                         gnus-newsgroup-reads)))
8917               t)
8918             ;; Do the `display' group parameter.
8919             (and gnus-newsgroup-display
8920                  (let ((gnus-number number))
8921                    (not (funcall gnus-newsgroup-display))))))
8922           ;; Nope, invisible article.
8923           0
8924         ;; Ok, this article is to be visible, so we add it to the limit
8925         ;; and return 1.
8926         (push number gnus-newsgroup-limit)
8927         1))))
8928
8929 (defun gnus-expunge-thread (thread)
8930   "Mark all articles in THREAD as read."
8931   (let* ((number (mail-header-number (car thread))))
8932     (incf gnus-newsgroup-expunged-tally)
8933     ;; We also mark as read here, if that's wanted.
8934     (setq gnus-newsgroup-unreads
8935           (delq number gnus-newsgroup-unreads))
8936     (if gnus-newsgroup-auto-expire
8937         (push number gnus-newsgroup-expirable)
8938       (push (cons number gnus-low-score-mark)
8939             gnus-newsgroup-reads)))
8940   ;; Go recursively through all subthreads.
8941   (mapcar 'gnus-expunge-thread (cdr thread)))
8942
8943 ;; Summary article oriented commands
8944
8945 (defun gnus-summary-refer-parent-article (n)
8946   "Refer parent article N times.
8947 If N is negative, go to ancestor -N instead.
8948 The difference between N and the number of articles fetched is returned."
8949   (interactive "p")
8950   (let ((skip 1)
8951         error header ref)
8952     (when (not (natnump n))
8953       (setq skip (abs n)
8954             n 1))
8955     (while (and (> n 0)
8956                 (not error))
8957       (setq header (gnus-summary-article-header))
8958       (if (and (eq (mail-header-number header)
8959                    (cdr gnus-article-current))
8960                (equal gnus-newsgroup-name
8961                       (car gnus-article-current)))
8962           ;; If we try to find the parent of the currently
8963           ;; displayed article, then we take a look at the actual
8964           ;; References header, since this is slightly more
8965           ;; reliable than the References field we got from the
8966           ;; server.
8967           (with-current-buffer gnus-original-article-buffer
8968             (nnheader-narrow-to-headers)
8969             (unless (setq ref (message-fetch-field "references"))
8970               (when (setq ref (message-fetch-field "in-reply-to"))
8971                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8972             (widen))
8973         (setq ref
8974               ;; It's not the current article, so we take a bet on
8975               ;; the value we got from the server.
8976               (mail-header-references header)))
8977       (if (and ref
8978                (not (equal ref "")))
8979           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8980             (gnus-message 1 "Couldn't find parent"))
8981         (gnus-message 1 "No references in article %d"
8982                       (gnus-summary-article-number))
8983         (setq error t))
8984       (decf n))
8985     (gnus-summary-position-point)
8986     n))
8987
8988 (defun gnus-summary-refer-references ()
8989   "Fetch all articles mentioned in the References header.
8990 Return the number of articles fetched."
8991   (interactive)
8992   (let ((ref (mail-header-references (gnus-summary-article-header)))
8993         (current (gnus-summary-article-number))
8994         (n 0))
8995     (if (or (not ref)
8996             (equal ref ""))
8997         (error "No References in the current article")
8998       ;; For each Message-ID in the References header...
8999       (while (string-match "<[^>]*>" ref)
9000         (incf n)
9001         ;; ... fetch that article.
9002         (gnus-summary-refer-article
9003          (prog1 (match-string 0 ref)
9004            (setq ref (substring ref (match-end 0))))))
9005       (gnus-summary-goto-subject current)
9006       (gnus-summary-position-point)
9007       n)))
9008
9009 (defun gnus-delete-duplicate-headers (headers)
9010   ;; First remove leading duplicates.
9011   (while (and (> (length headers) 1)
9012               (= (mail-header-number (car headers))
9013                  (mail-header-number (cadr headers))))
9014     (pop headers))
9015   ;; Then the rest.
9016   (let ((result headers))
9017     (while (> (length headers) 1)
9018       (if (= (mail-header-number (car headers))
9019              (mail-header-number (cadr headers)))
9020           (setcdr headers (cddr headers))
9021         (pop headers)))
9022     result))
9023
9024 (defun gnus-summary-refer-thread (&optional limit)
9025   "Fetch all articles in the current thread. For backends that
9026 know how to search for threads (currently only 'nnimap) a
9027 non-numeric prefix arg will use nnir to search the entire
9028 server; without a prefix arg only the current group is
9029 searched. If the variable `gnus-refer-thread-use-nnir' is
9030 non-nil the prefix arg has the reverse meaning. If no
9031 backend-specific 'request-thread function is available fetch
9032 LIMIT (the numerical prefix) old headers. If LIMIT is
9033 non-numeric or nil fetch the number specified by the
9034 `gnus-refer-thread-limit' variable."
9035   (interactive "P")
9036   (gnus-warp-to-article)
9037   (let* ((header (gnus-summary-article-header))
9038          (id (mail-header-id header))
9039          (gnus-inhibit-demon t)
9040          (gnus-summary-ignore-duplicates t)
9041          (gnus-read-all-available-headers t)
9042          (gnus-refer-thread-use-nnir
9043           (if (and (not (null limit)) (listp limit))
9044               (not gnus-refer-thread-use-nnir) gnus-refer-thread-use-nnir))
9045          (new-headers
9046           (if (gnus-check-backend-function
9047                'request-thread gnus-newsgroup-name)
9048               (gnus-request-thread header gnus-newsgroup-name)
9049             (let* ((limit (if (numberp limit) (prefix-numeric-value limit)
9050                             gnus-refer-thread-limit))
9051                    (last (if (numberp limit)
9052                              (min (+ (mail-header-number header)
9053                                      limit)
9054                                   gnus-newsgroup-highest)
9055                            gnus-newsgroup-highest))
9056                    (subject (gnus-simplify-subject
9057                              (mail-header-subject header)))
9058                    (refs (split-string (or (mail-header-references header)
9059                                            "")))
9060                    (gnus-parse-headers-hook
9061                     `(lambda () (goto-char (point-min))
9062                       (keep-lines
9063                        (regexp-opt ',(append refs (list id subject)))))))
9064               (gnus-fetch-headers (list last) (if (numberp limit)
9065                                                   (* 2 limit) limit) t))))
9066          article-ids)
9067     (when (listp new-headers)
9068       (dolist (header new-headers)
9069         (push (mail-header-number header) article-ids)
9070         (when (member (mail-header-number header) gnus-newsgroup-unselected)
9071           (push (mail-header-number header) gnus-newsgroup-unreads)
9072           (setq gnus-newsgroup-unselected
9073                 (delete (mail-header-number header)
9074                         gnus-newsgroup-unselected))))
9075       (setq gnus-newsgroup-headers
9076             (gnus-delete-duplicate-headers
9077              (gnus-merge
9078               'list gnus-newsgroup-headers new-headers
9079               'gnus-article-sort-by-number)))
9080       (setq gnus-newsgroup-articles
9081             (gnus-sorted-nunion gnus-newsgroup-articles (nreverse article-ids)))
9082       (gnus-summary-limit-include-thread id)))
9083   (gnus-summary-show-thread))
9084
9085 (defun gnus-summary-open-group-with-article (message-id)
9086   "Open a group containing the article with the given MESSAGE-ID."
9087   (interactive "sMessage-ID: ")
9088   (require 'nndoc)
9089   (with-temp-buffer
9090     ;; Prepare a dummy article
9091     (erase-buffer)
9092     (insert "From nobody Tue Sep 13 22:05:34 2011\n\n")
9093
9094     ;; Prepare pretty modelines for summary and article buffers
9095     (let ((gnus-summary-mode-line-format "Found %G")
9096           (gnus-article-mode-line-format
9097            ;; Group names just get in the way here, especially the
9098            ;; abbreviated ones
9099            (if (string-match "%[gG]" gnus-article-mode-line-format)
9100                (concat (substring gnus-article-mode-line-format
9101                                   0 (match-beginning 0))
9102                        (substring gnus-article-mode-line-format (match-end 0)))
9103              gnus-article-mode-line-format)))
9104
9105       ;; Build an ephemeral group containing the dummy article (hidden)
9106       (gnus-group-read-ephemeral-group
9107        message-id
9108        `(nndoc ,message-id
9109                (nndoc-address ,(current-buffer))
9110                (nndoc-article-type mbox))
9111        :activate
9112        (cons (current-buffer) gnus-current-window-configuration)
9113        (not :request-only)
9114        '(-1)                            ; :select-articles
9115        (not :parameters)
9116        0))                              ; :number
9117     ;; Fetch the desired article
9118     (gnus-summary-refer-article message-id)))
9119
9120 (defun gnus-summary-refer-article (message-id)
9121   "Fetch an article specified by MESSAGE-ID."
9122   (interactive "sMessage-ID: ")
9123   (gnus-warp-to-article)
9124   (when (and (stringp message-id)
9125              (not (zerop (length message-id))))
9126     (setq message-id (gnus-replace-in-string message-id " " ""))
9127     ;; Construct the correct Message-ID if necessary.
9128     ;; Suggested by tale@pawl.rpi.edu.
9129     (unless (string-match "^<" message-id)
9130       (setq message-id (concat "<" message-id)))
9131     (unless (string-match ">$" message-id)
9132       (setq message-id (concat message-id ">")))
9133     ;; People often post MIDs from URLs, so unhex it:
9134     (unless (string-match "@" message-id)
9135       (setq message-id (gnus-url-unhex-string message-id)))
9136     (let* ((header (gnus-id-to-header message-id))
9137            (sparse (and header
9138                         (gnus-summary-article-sparse-p
9139                          (mail-header-number header))
9140                         (memq (mail-header-number header)
9141                               gnus-newsgroup-limit)))
9142            number)
9143       (cond
9144        ;; If the article is present in the buffer we just go to it.
9145        ((and header
9146              (or (not (gnus-summary-article-sparse-p
9147                        (mail-header-number header)))
9148                  sparse))
9149         (prog1
9150             (gnus-summary-goto-article
9151              (mail-header-number header) nil t)
9152           (when sparse
9153             (gnus-summary-update-article (mail-header-number header)))))
9154        (t
9155         ;; We fetch the article.
9156         (catch 'found
9157           (dolist (gnus-override-method (gnus-refer-article-methods))
9158             (when (and (gnus-check-server gnus-override-method)
9159                        ;; Fetch the header,
9160                        (setq number (gnus-summary-insert-subject message-id)))
9161               ;; and display the article.
9162               (gnus-summary-select-article nil nil nil number)
9163               (throw 'found t)))
9164           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
9165
9166 (defun gnus-refer-article-methods ()
9167   "Return a list of referable methods."
9168   (cond
9169    ;; No method, so we default to current and native.
9170    ((null gnus-refer-article-method)
9171     (list gnus-current-select-method gnus-select-method))
9172    ;; Current.
9173    ((eq 'current gnus-refer-article-method)
9174     (list gnus-current-select-method))
9175    ;; List of select methods.
9176    ((not (and (symbolp (car gnus-refer-article-method))
9177               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
9178     (let (out)
9179       (dolist (method gnus-refer-article-method)
9180         (push (if (eq 'current method)
9181                   gnus-current-select-method
9182                 (if (eq 'nnir (car method))
9183                     (list
9184                      'nnir
9185                      (or (cadr method)
9186                          (gnus-method-to-server gnus-current-select-method)))
9187                   method))
9188               out))
9189       (nreverse out)))
9190    ;; One single select method.
9191    (t
9192     (list gnus-refer-article-method))))
9193
9194 (defun gnus-summary-edit-parameters ()
9195   "Edit the group parameters of the current group."
9196   (interactive)
9197   (gnus-group-edit-group gnus-newsgroup-name 'params))
9198
9199 (defun gnus-summary-customize-parameters ()
9200   "Customize the group parameters of the current group."
9201   (interactive)
9202   (gnus-group-customize gnus-newsgroup-name))
9203
9204 (defun gnus-summary-enter-digest-group (&optional force)
9205   "Enter an nndoc group based on the current article.
9206 If FORCE, force a digest interpretation.  If not, try to guess
9207 what the document format is.
9208
9209 To control what happens when you exit the group, see the
9210 `gnus-auto-select-on-ephemeral-exit' variable."
9211   (interactive "P")
9212   (let ((conf gnus-current-window-configuration))
9213     (save-window-excursion
9214       (save-excursion
9215         (let (gnus-article-prepare-hook
9216               gnus-display-mime-function
9217               gnus-break-pages)
9218           (gnus-summary-select-article))))
9219     (setq gnus-current-window-configuration conf)
9220     (let* ((name (format "%s-%d"
9221                          (gnus-group-prefixed-name
9222                           gnus-newsgroup-name (list 'nndoc ""))
9223                          (with-current-buffer gnus-summary-buffer
9224                            gnus-current-article)))
9225            (ogroup gnus-newsgroup-name)
9226            (params (append (gnus-info-params (gnus-get-info ogroup))
9227                            (list (cons 'to-group ogroup))
9228                            (list (cons 'parent-group ogroup))
9229                            (list (cons 'save-article-group ogroup))))
9230            (case-fold-search t)
9231            (buf (current-buffer))
9232            dig to-address charset)
9233       (with-current-buffer gnus-original-article-buffer
9234         ;; Have the digest group inherit the main mail address of
9235         ;; the parent article.
9236         (when (setq to-address (or (gnus-fetch-field "reply-to")
9237                                    (gnus-fetch-field "from")))
9238           (setq params
9239                 (append
9240                  params
9241                  (list (cons 'to-address
9242                              (funcall gnus-decode-encoded-address-function
9243                                       to-address))))))
9244         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
9245         (insert-buffer-substring gnus-original-article-buffer)
9246         (narrow-to-region
9247          (goto-char (point-min))
9248          (or (search-forward "\n\n" nil t) (point)))
9249         ;; Remove lines that may lead nndoc to misinterpret the
9250         ;; document type.
9251         (goto-char (point-min))
9252         (delete-matching-lines "^Path:\\|^From ")
9253         ;; Parse charset, and decode content transfer encoding.
9254         (setq charset (mail-content-type-get
9255                        (mail-header-parse-content-type
9256                         (or (gnus-fetch-field "content-type") ""))
9257                        'charset))
9258         (let ((encoding (gnus-fetch-field "content-transfer-encoding")))
9259           (when encoding
9260             (message-remove-header "content-transfer-encoding")
9261             (goto-char (point-max))
9262             (widen)
9263             (narrow-to-region (point) (point-max))
9264             (mm-decode-content-transfer-encoding
9265              (intern (downcase (mail-header-strip encoding))))))
9266         (widen))
9267       (unwind-protect
9268           (if (let ((gnus-newsgroup-ephemeral-charset
9269                      (if charset
9270                          (intern (downcase (gnus-strip-whitespace charset)))
9271                        gnus-newsgroup-charset))
9272                     (gnus-newsgroup-ephemeral-ignored-charsets
9273                      gnus-newsgroup-ignored-charsets))
9274                 (gnus-group-read-ephemeral-group
9275                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
9276                               (nndoc-article-type
9277                                ,(if force 'mbox 'guess)))
9278                  t nil nil nil
9279                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
9280                                                         "ADAPT")))))
9281               ;; Make all postings to this group go to the parent group.
9282               (nconc (gnus-info-params (gnus-get-info name))
9283                      params)
9284             ;; Couldn't select this doc group.
9285             (switch-to-buffer buf)
9286             (gnus-set-global-variables)
9287             (gnus-configure-windows 'summary)
9288             (gnus-message 3 "Article couldn't be entered?"))
9289         (kill-buffer dig)))))
9290
9291 (defun gnus-summary-read-document (n)
9292   "Open a new group based on the current article(s).
9293 This will allow you to read digests and other similar
9294 documents as newsgroups.
9295 Obeys the standard process/prefix convention."
9296   (interactive "P")
9297   (let* ((ogroup gnus-newsgroup-name)
9298          (params (append (gnus-info-params (gnus-get-info ogroup))
9299                          (list (cons 'to-group ogroup))))
9300          group egroup groups vgroup)
9301     (dolist (article (gnus-summary-work-articles n))
9302       (setq group (format "%s-%d" gnus-newsgroup-name article))
9303       (gnus-summary-remove-process-mark article)
9304       (when (gnus-summary-display-article article)
9305         (save-excursion ;;What for?
9306           (with-temp-buffer
9307             (insert-buffer-substring gnus-original-article-buffer)
9308             ;; Remove some headers that may lead nndoc to make
9309             ;; the wrong guess.
9310             (message-narrow-to-head)
9311             (goto-char (point-min))
9312             (delete-matching-lines "^Path:\\|^From ")
9313             (widen)
9314             (if (setq egroup
9315                       (gnus-group-read-ephemeral-group
9316                        group `(nndoc ,group (nndoc-address ,(current-buffer))
9317                                      (nndoc-article-type guess))
9318                        t nil t))
9319                 (progn
9320                   ;; Make all postings to this group go to the parent group.
9321                   (nconc (gnus-info-params (gnus-get-info egroup))
9322                          params)
9323                   (push egroup groups))
9324               ;; Couldn't select this doc group.
9325               (gnus-error 3 "Article couldn't be entered"))))))
9326     ;; Now we have selected all the documents.
9327     (cond
9328      ((not groups)
9329       (error "None of the articles could be interpreted as documents"))
9330      ((gnus-group-read-ephemeral-group
9331        (setq vgroup (format
9332                      "nnvirtual:%s-%s" gnus-newsgroup-name
9333                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
9334        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
9335        t
9336        (cons (current-buffer) 'summary)))
9337      (t
9338       (error "Couldn't select virtual nndoc group")))))
9339
9340 (defun gnus-summary-widget-forward (arg)
9341   "Move point to the next field or button in the article.
9342 With optional ARG, move across that many fields."
9343   (interactive "p")
9344   (gnus-summary-select-article)
9345   (gnus-configure-windows 'article)
9346   (select-window (gnus-get-buffer-window gnus-article-buffer))
9347   (widget-forward arg))
9348
9349 (defun gnus-summary-widget-backward (arg)
9350   "Move point to the previous field or button in the article.
9351 With optional ARG, move across that many fields."
9352   (interactive "p")
9353   (gnus-summary-select-article)
9354   (gnus-configure-windows 'article)
9355   (select-window (gnus-get-buffer-window gnus-article-buffer))
9356   (unless (widget-at (point))
9357     (goto-char (point-max)))
9358   (widget-backward arg))
9359
9360 (defun gnus-summary-isearch-article (&optional regexp-p)
9361   "Do incremental search forward on the current article.
9362 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
9363   (interactive "P")
9364   (gnus-summary-select-article)
9365   (gnus-configure-windows 'article)
9366   (gnus-eval-in-buffer-window gnus-article-buffer
9367     (save-restriction
9368       (widen)
9369       (isearch-forward regexp-p))))
9370
9371 (defun gnus-summary-repeat-search-article-forward ()
9372   "Repeat the previous search forwards."
9373   (interactive)
9374   (unless gnus-last-search-regexp
9375     (error "No previous search"))
9376   (gnus-summary-search-article-forward gnus-last-search-regexp))
9377
9378 (defun gnus-summary-repeat-search-article-backward ()
9379   "Repeat the previous search backwards."
9380   (interactive)
9381   (unless gnus-last-search-regexp
9382     (error "No previous search"))
9383   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9384
9385 (defun gnus-summary-search-article-forward (regexp &optional backward)
9386   "Search for an article containing REGEXP forward.
9387 If BACKWARD, search backward instead."
9388   (interactive
9389    (list (read-string
9390           (format "Search article %s (regexp%s): "
9391                   (if current-prefix-arg "backward" "forward")
9392                   (if gnus-last-search-regexp
9393                       (concat ", default " gnus-last-search-regexp)
9394                     "")))
9395          current-prefix-arg))
9396   (if (string-equal regexp "")
9397       (setq regexp (or gnus-last-search-regexp ""))
9398     (setq gnus-last-search-regexp regexp)
9399     (setq gnus-article-before-search gnus-current-article))
9400   ;; Intentionally set gnus-last-article.
9401   (setq gnus-last-article gnus-article-before-search)
9402   (let ((gnus-last-article gnus-last-article))
9403     (if (gnus-summary-search-article regexp backward)
9404         (gnus-summary-show-thread)
9405       (signal 'search-failed (list regexp)))))
9406
9407 (defun gnus-summary-search-article-backward (regexp)
9408   "Search for an article containing REGEXP backward."
9409   (interactive
9410    (list (read-string
9411           (format "Search article backward (regexp%s): "
9412                   (if gnus-last-search-regexp
9413                       (concat ", default " gnus-last-search-regexp)
9414                     "")))))
9415   (gnus-summary-search-article-forward regexp 'backward))
9416
9417 (defun gnus-summary-search-article (regexp &optional backward)
9418   "Search for an article containing REGEXP.
9419 Optional argument BACKWARD means do search for backward.
9420 `gnus-select-article-hook' is not called during the search."
9421   ;; We have to require this here to make sure that the following
9422   ;; dynamic binding isn't shadowed by autoloading.
9423   (require 'gnus-async)
9424   (require 'gnus-art)
9425   (let ((gnus-select-article-hook nil)  ;Disable hook.
9426         (gnus-article-prepare-hook nil)
9427         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9428         (gnus-use-article-prefetch nil)
9429         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9430         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9431         (gnus-visual nil)
9432         (gnus-keep-backlog nil)
9433         (gnus-break-pages nil)
9434         (gnus-summary-display-arrow nil)
9435         (gnus-updated-mode-lines nil)
9436         (gnus-auto-center-summary nil)
9437         (sum (current-buffer))
9438         (gnus-display-mime-function nil)
9439         (found nil)
9440         point)
9441     (gnus-save-hidden-threads
9442       (gnus-summary-select-article)
9443       (set-buffer gnus-article-buffer)
9444       (goto-char (window-point (get-buffer-window (current-buffer))))
9445       (when backward
9446         (forward-line -1))
9447       (while (not found)
9448         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9449         (if (if backward
9450                 (re-search-backward regexp nil t)
9451               (re-search-forward regexp nil t))
9452             ;; We found the regexp.
9453             (progn
9454               (setq found 'found)
9455               (beginning-of-line)
9456               (set-window-start
9457                (get-buffer-window (current-buffer))
9458                (point))
9459               (forward-line 1)
9460               (set-window-point
9461                (get-buffer-window (current-buffer))
9462                (point))
9463               (set-buffer sum)
9464               (setq point (point)))
9465           ;; We didn't find it, so we go to the next article.
9466           (set-buffer sum)
9467           (setq found 'not)
9468           (while (eq found 'not)
9469             (if (not (if backward (gnus-summary-find-prev)
9470                        (gnus-summary-find-next)))
9471                 ;; No more articles.
9472                 (setq found t)
9473               ;; Select the next article and adjust point.
9474               (unless (gnus-summary-article-sparse-p
9475                        (gnus-summary-article-number))
9476                 (setq found nil)
9477                 (gnus-summary-select-article)
9478                 (set-buffer gnus-article-buffer)
9479                 (widen)
9480                 (goto-char (if backward (point-max) (point-min))))))))
9481       (gnus-message 7 ""))
9482     ;; Return whether we found the regexp.
9483     (when (eq found 'found)
9484       (goto-char point)
9485       (gnus-summary-show-thread)
9486       (gnus-summary-goto-subject gnus-current-article)
9487       (gnus-summary-position-point)
9488       t)))
9489
9490 (defun gnus-find-matching-articles (header regexp)
9491   "Return a list of all articles that match REGEXP on HEADER.
9492 This search includes all articles in the current group that Gnus has
9493 fetched headers for, whether they are displayed or not."
9494   (let ((articles nil)
9495         ;; Can't eta-reduce because it's a macro.
9496         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9497         (case-fold-search t))
9498     (dolist (header gnus-newsgroup-headers)
9499       (when (string-match regexp (funcall func header))
9500         (push (mail-header-number header) articles)))
9501     (nreverse articles)))
9502
9503 (defun gnus-summary-find-matching (header regexp &optional backward unread
9504                                           not-case-fold not-matching)
9505   "Return a list of all articles that match REGEXP on HEADER.
9506 The search stars on the current article and goes forwards unless
9507 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9508 If UNREAD is non-nil, only unread articles will
9509 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9510 in the comparisons. If NOT-MATCHING, return a list of all articles that
9511 not match REGEXP on HEADER."
9512   (let ((case-fold-search (not not-case-fold))
9513         articles d func)
9514     (if (consp header)
9515         (if (eq (car header) 'extra)
9516             (setq func
9517                   `(lambda (h)
9518                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9519                          "")))
9520           (error "%s is an invalid header" header))
9521       (unless (fboundp (intern (concat "mail-header-" header)))
9522         (error "%s is not a valid header" header))
9523       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9524     (dolist (d (if (eq backward 'all)
9525                    gnus-newsgroup-data
9526                  (gnus-data-find-list
9527                   (gnus-summary-article-number)
9528                   (gnus-data-list backward))))
9529       (when (and (or (not unread)       ; We want all articles...
9530                      (gnus-data-unread-p d)) ; Or just unreads.
9531                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9532                  (if not-matching
9533                      (not (string-match
9534                            regexp
9535                            (funcall func (gnus-data-header d))))
9536                    (string-match regexp
9537                                  (funcall func (gnus-data-header d)))))
9538         (push (gnus-data-number d) articles))) ; Success!
9539     (nreverse articles)))
9540
9541 (defun gnus-summary-execute-command (header regexp command &optional backward)
9542   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9543 If HEADER is an empty string (or nil), the match is done on the entire
9544 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9545   (interactive
9546    (list (let ((completion-ignore-case t))
9547            (gnus-completing-read
9548             "Header name"
9549             (mapcar 'symbol-name
9550                     (append
9551                      '(Number Subject From Lines Date
9552                        Message-ID Xref References Body)
9553                      gnus-extra-headers))
9554             'require-match))
9555          (read-string "Regexp: ")
9556          (read-key-sequence "Command: ")
9557          current-prefix-arg))
9558   (when (equal header "Body")
9559     (setq header ""))
9560   ;; Hidden thread subtrees must be searched as well.
9561   (gnus-summary-show-all-threads)
9562   ;; We don't want to change current point nor window configuration.
9563   (save-excursion
9564     (save-window-excursion
9565       (let (gnus-visual
9566             gnus-treat-strip-trailing-blank-lines
9567             gnus-treat-strip-leading-blank-lines
9568             gnus-treat-strip-multiple-blank-lines
9569             gnus-treat-hide-boring-headers
9570             gnus-treat-fold-newsgroups
9571             gnus-article-prepare-hook)
9572         (gnus-message 6 "Executing %s..." (key-description command))
9573         ;; We'd like to execute COMMAND interactively so as to give arguments.
9574         (gnus-execute header regexp
9575                       `(call-interactively ',(key-binding command))
9576                       backward)
9577         (gnus-message 6 "Executing %s...done" (key-description command))))))
9578
9579 (defun gnus-summary-beginning-of-article ()
9580   "Scroll the article back to the beginning."
9581   (interactive)
9582   (gnus-summary-select-article)
9583   (gnus-configure-windows 'article)
9584   (gnus-eval-in-buffer-window gnus-article-buffer
9585     (widen)
9586     (goto-char (point-min))
9587     (when gnus-break-pages
9588       (gnus-narrow-to-page))))
9589
9590 (defun gnus-summary-end-of-article ()
9591   "Scroll to the end of the article."
9592   (interactive)
9593   (gnus-summary-select-article)
9594   (gnus-configure-windows 'article)
9595   (gnus-eval-in-buffer-window gnus-article-buffer
9596     (widen)
9597     (goto-char (point-max))
9598     (recenter -3)
9599     (when gnus-break-pages
9600       (gnus-narrow-to-page))))
9601
9602 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9603   "Truncate to LEN and quote all \"(\"'s in STRING."
9604   (gnus-replace-in-string (if (and len (> (length string) len))
9605                               (substring string 0 len)
9606                             string)
9607                           "[()]" "\\\\\\&"))
9608
9609 (defun gnus-summary-print-article (&optional filename n)
9610   "Generate and print a PostScript image of the process-marked (mail) articles.
9611
9612 If used interactively, print the current article if none are
9613 process-marked.  With prefix arg, prompt the user for the name of the
9614 file to save in.
9615
9616 When used from Lisp, accept two optional args FILENAME and N.  N means
9617 to print the next N articles.  If N is negative, print the N previous
9618 articles.  If N is nil and articles have been marked with the process
9619 mark, print these instead.
9620
9621 If the optional first argument FILENAME is nil, send the image to the
9622 printer.  If FILENAME is a string, save the PostScript image in a file with
9623 that name.  If FILENAME is a number, prompt the user for the name of the file
9624 to save in."
9625   (interactive (list (ps-print-preprint current-prefix-arg)))
9626   (dolist (article (gnus-summary-work-articles n))
9627     (gnus-summary-select-article nil nil 'pseudo article)
9628     (gnus-eval-in-buffer-window gnus-article-buffer
9629       (gnus-print-buffer))
9630     (gnus-summary-remove-process-mark article))
9631   (ps-despool filename))
9632
9633 (defun gnus-print-buffer ()
9634   (let ((ps-left-header
9635          (list
9636           (concat "("
9637                   (gnus-summary-print-truncate-and-quote
9638                    (mail-header-subject gnus-current-headers)
9639                    66) ")")
9640           (concat "("
9641                   (gnus-summary-print-truncate-and-quote
9642                    (mail-header-from gnus-current-headers)
9643                    45) ")")))
9644         (ps-right-header
9645          (list
9646           "/pagenumberstring load"
9647           (concat "("
9648                   (mail-header-date gnus-current-headers) ")"))))
9649     (gnus-run-hooks 'gnus-ps-print-hook)
9650     (save-excursion
9651       (if ps-print-color-p
9652           (ps-spool-buffer-with-faces)
9653         (ps-spool-buffer)))))
9654
9655 (declare-function gnus-flush-original-article-buffer "gnus-art" ())
9656
9657 (defun gnus-summary-show-complete-article ()
9658   "Show a complete version of the current article.
9659 This is only useful if you're looking at a partial version of the
9660 article currently."
9661   (interactive)
9662   (let ((gnus-keep-backlog nil)
9663         (gnus-use-cache nil)
9664         (gnus-agent nil)
9665         (variable (intern
9666                    (format "%s-fetch-partial-articles"
9667                            (car (gnus-find-method-for-group
9668                                  gnus-newsgroup-name)))
9669                    obarray))
9670         old-val)
9671     (unwind-protect
9672         (progn
9673           (setq old-val (symbol-value variable))
9674           (set variable nil)
9675           (gnus-flush-original-article-buffer)
9676           (gnus-summary-show-article))
9677       (set variable old-val))))
9678
9679 (defun gnus-summary-show-article (&optional arg)
9680   "Force redisplaying of the current article.
9681 If ARG (the prefix) is a number, show the article with the charset
9682 defined in `gnus-summary-show-article-charset-alist', or the charset
9683 input.
9684 If ARG (the prefix) is non-nil and not a number, show the article,
9685 but without running any of the article treatment functions
9686 article.  Normally, the keystroke is `C-u g'.  When using `C-u
9687 C-u g', show the raw article."
9688   (interactive "P")
9689   (cond
9690    ((numberp arg)
9691     (gnus-summary-show-article t)
9692     (let ((gnus-newsgroup-charset
9693            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9694                (mm-read-coding-system
9695                 "View as charset: " ;; actually it is coding system.
9696                 (with-current-buffer gnus-article-buffer
9697                   (mm-detect-coding-region (point) (point-max))))))
9698           (gnus-newsgroup-ignored-charsets 'gnus-all))
9699       (gnus-summary-select-article nil 'force)
9700       (let ((deps gnus-newsgroup-dependencies)
9701             head header lines)
9702         (with-current-buffer gnus-original-article-buffer
9703           (save-restriction
9704             (message-narrow-to-head)
9705             (setq head (buffer-string))
9706             (goto-char (point-min))
9707             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9708               (goto-char (point-max))
9709               (widen)
9710               (setq lines (1- (count-lines (point) (point-max))))))
9711           (with-temp-buffer
9712             (insert (format "211 %d Article retrieved.\n"
9713                             (cdr gnus-article-current)))
9714             (insert head)
9715             (if lines (insert (format "Lines: %d\n" lines)))
9716             (insert ".\n")
9717             (let ((nntp-server-buffer (current-buffer)))
9718               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9719         (gnus-data-set-header
9720          (gnus-data-find (cdr gnus-article-current))
9721          header)
9722         (gnus-summary-update-article-line
9723          (cdr gnus-article-current) header)
9724         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9725           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9726    ((not arg)
9727     ;; Select the article the normal way.
9728     (if (eq mm-text-html-renderer 'shr)
9729         (progn
9730           (require 'shr)
9731           (let ((shr-ignore-cache t))
9732             (gnus-summary-select-article nil 'force)))
9733       (gnus-summary-select-article nil 'force)))
9734    ((equal arg '(16))
9735     ;; C-u C-u g
9736     (let ((gnus-inhibit-article-treatments t))
9737       (gnus-summary-select-article nil 'force)))
9738    (t
9739     ;; We have to require this here to make sure that the following
9740     ;; dynamic binding isn't shadowed by autoloading.
9741     (require 'gnus-async)
9742     (require 'gnus-art)
9743     ;; Bind the article treatment functions to nil.
9744     (let ((gnus-have-all-headers t)
9745           gnus-article-prepare-hook
9746           gnus-article-decode-hook
9747           gnus-display-mime-function
9748           gnus-break-pages)
9749       ;; Destroy any MIME parts.
9750       (when (gnus-buffer-live-p gnus-article-buffer)
9751         (with-current-buffer gnus-article-buffer
9752           (gnus-article-stop-animations)
9753           (gnus-stop-downloads)
9754           (mm-destroy-parts gnus-article-mime-handles)
9755           ;; Set it to nil for safety reason.
9756           (setq gnus-article-mime-handle-alist nil)
9757           (setq gnus-article-mime-handles nil)))
9758       (gnus-summary-select-article nil 'force))))
9759   (gnus-summary-goto-subject gnus-current-article)
9760   (gnus-summary-position-point))
9761
9762 (defun gnus-summary-show-raw-article ()
9763   "Show the raw article without any article massaging functions being run."
9764   (interactive)
9765   (gnus-summary-show-article t))
9766
9767 (defun gnus-summary-verbose-headers (&optional arg)
9768   "Toggle permanent full header display.
9769 If ARG is a positive number, turn header display on.
9770 If ARG is a negative number, turn header display off."
9771   (interactive "P")
9772   (setq gnus-show-all-headers
9773         (cond ((or (not (numberp arg))
9774                    (zerop arg))
9775                (not gnus-show-all-headers))
9776               ((natnump arg)
9777                t)))
9778   (gnus-summary-show-article))
9779
9780 (declare-function article-narrow-to-head "gnus-art" ())
9781 (declare-function gnus-article-hidden-text-p "gnus-art" (type))
9782 (declare-function gnus-delete-wash-type "gnus-art" (type))
9783
9784 (defun gnus-summary-toggle-header (&optional arg)
9785   "Show the headers if they are hidden, or hide them if they are shown.
9786 If ARG is a positive number, show the entire header.
9787 If ARG is a negative number, hide the unwanted header lines."
9788   (interactive "P")
9789   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9790                      (get-buffer-window gnus-article-buffer t))))
9791     (with-current-buffer gnus-article-buffer
9792       (widen)
9793       (article-narrow-to-head)
9794       (let* ((inhibit-read-only t)
9795              (inhibit-point-motion-hooks t)
9796              (hidden (if (numberp arg)
9797                          (>= arg 0)
9798                        (or (not (looking-at "[^ \t\n]+:"))
9799                            (gnus-article-hidden-text-p 'headers))))
9800              s e)
9801         (delete-region (point-min) (point-max))
9802         (with-current-buffer gnus-original-article-buffer
9803           (goto-char (setq s (point-min)))
9804           (setq e (if (search-forward "\n\n" nil t)
9805                       (1- (point))
9806                     (point-max))))
9807         (insert-buffer-substring gnus-original-article-buffer s e)
9808         (run-hooks 'gnus-article-decode-hook)
9809         (if hidden
9810             (let ((gnus-treat-hide-headers nil)
9811                   (gnus-treat-hide-boring-headers nil))
9812               (gnus-delete-wash-type 'headers)
9813               (gnus-treat-article 'head))
9814           (gnus-treat-article 'head))
9815         (widen)
9816         (if window
9817             (set-window-start window (goto-char (point-min))))
9818         (if gnus-break-pages
9819             (gnus-narrow-to-page)
9820           (when (gnus-visual-p 'page-marker)
9821             (let ((inhibit-read-only t))
9822               (gnus-remove-text-with-property 'gnus-prev)
9823               (gnus-remove-text-with-property 'gnus-next))))
9824         (gnus-set-mode-line 'article)))))
9825
9826 (defun gnus-summary-show-all-headers ()
9827   "Make all header lines visible."
9828   (interactive)
9829   (gnus-summary-toggle-header 1))
9830
9831 (defun gnus-summary-caesar-message (&optional arg)
9832   "Caesar rotate the current article by 13.
9833 With a non-numerical prefix, also rotate headers.  A numerical
9834 prefix specifies how many places to rotate each letter forward."
9835   (interactive "P")
9836   (gnus-summary-select-article)
9837   (let ((mail-header-separator ""))
9838     (gnus-eval-in-buffer-window gnus-article-buffer
9839       (save-restriction
9840         (widen)
9841         (let ((start (window-start))
9842               (inhibit-read-only t))
9843           (if (equal arg '(4))
9844               (message-caesar-buffer-body nil t)
9845             (message-caesar-buffer-body arg))
9846           (set-window-start (get-buffer-window (current-buffer)) start)))))
9847   ;; Create buttons and stuff...
9848   (gnus-treat-article nil))
9849
9850 (declare-function idna-to-unicode "ext:idna" (str))
9851
9852 (defun gnus-summary-idna-message (&optional arg)
9853   "Decode IDNA encoded domain names in the current articles.
9854 IDNA encoded domain names looks like `xn--bar'.  If a string
9855 remain unencoded after running this function, it is likely an
9856 invalid IDNA string (`xn--bar' is invalid).
9857
9858 You must have GNU Libidn (URL `http://www.gnu.org/software/libidn/')
9859 installed for this command to work."
9860   (interactive "P")
9861   (if (not (and (condition-case nil (require 'idna)
9862                   (file-error))
9863                 (mm-coding-system-p 'utf-8)
9864                 (executable-find (symbol-value 'idna-program))))
9865       (gnus-message
9866        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9867     (gnus-summary-select-article)
9868     (let ((mail-header-separator ""))
9869       (gnus-eval-in-buffer-window gnus-article-buffer
9870         (save-restriction
9871           (widen)
9872           (let ((start (window-start))
9873                 buffer-read-only)
9874             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9875               (replace-match (idna-to-unicode (match-string 1))))
9876             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9877
9878 (defun gnus-summary-morse-message (&optional arg)
9879   "Morse decode the current article."
9880   (interactive "P")
9881   (gnus-summary-select-article)
9882   (let ((mail-header-separator ""))
9883     (gnus-eval-in-buffer-window gnus-article-buffer
9884       (save-excursion
9885         (save-restriction
9886           (widen)
9887           (let ((pos (window-start))
9888                 (inhibit-read-only t))
9889             (goto-char (point-min))
9890             (when (message-goto-body)
9891               (gnus-narrow-to-body))
9892             (goto-char (point-min))
9893             (while (search-forward "·" (point-max) t)
9894               (replace-match "."))
9895             (unmorse-region (point-min) (point-max))
9896             (widen)
9897             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9898
9899 (defun gnus-summary-stop-page-breaking ()
9900   "Stop page breaking in the current article."
9901   (interactive)
9902   (gnus-summary-select-article)
9903   (gnus-eval-in-buffer-window gnus-article-buffer
9904     (widen)
9905     (when (gnus-visual-p 'page-marker)
9906       (let ((inhibit-read-only t))
9907         (gnus-remove-text-with-property 'gnus-prev)
9908         (gnus-remove-text-with-property 'gnus-next))
9909       (setq gnus-page-broken nil))))
9910
9911 (defun gnus-summary-move-article (&optional n to-newsgroup
9912                                             select-method action)
9913   "Move the current article to a different newsgroup.
9914 If N is a positive number, move the N next articles.
9915 If N is a negative number, move the N previous articles.
9916 If N is nil and any articles have been marked with the process mark,
9917 move those articles instead.
9918 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9919 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9920 re-spool using this method.
9921
9922 When called interactively with TO-NEWSGROUP being nil, the value of
9923 the variable `gnus-move-split-methods' is used for finding a default
9924 for the target newsgroup.
9925
9926 For this function to work, both the current newsgroup and the
9927 newsgroup that you want to move to have to support the `request-move'
9928 and `request-accept' functions.
9929
9930 ACTION can be either `move' (the default), `crosspost' or `copy'."
9931   (interactive "P")
9932   (unless action
9933     (setq action 'move))
9934   ;; Check whether the source group supports the required functions.
9935   (cond ((and (eq action 'move)
9936               (not (gnus-check-backend-function
9937                     'request-move-article gnus-newsgroup-name)))
9938          (error "The current group does not support article moving"))
9939         ((and (eq action 'crosspost)
9940               (not (gnus-check-backend-function
9941                     'request-replace-article gnus-newsgroup-name)))
9942          (error "The current group does not support article editing")))
9943   (let ((articles (gnus-summary-work-articles n))
9944         (prefix (if (gnus-check-backend-function
9945                      'request-move-article gnus-newsgroup-name)
9946                     (funcall gnus-move-group-prefix-function
9947                              gnus-newsgroup-name)
9948                   ""))
9949         (names '((move "Move" "Moving")
9950                  (copy "Copy" "Copying")
9951                  (crosspost "Crosspost" "Crossposting")))
9952         (copy-buf (save-excursion
9953                     (nnheader-set-temp-buffer " *copy article*")))
9954         art-group to-method new-xref article to-groups
9955         articles-to-update-marks encoded)
9956     (unless (assq action names)
9957       (error "Unknown action %s" action))
9958     ;; Read the newsgroup name.
9959     (when (and (not to-newsgroup)
9960                (not select-method))
9961       (if (and gnus-move-split-methods
9962                (not
9963                 (and (memq gnus-current-article articles)
9964                      (gnus-buffer-live-p gnus-original-article-buffer))))
9965           ;; When `gnus-move-split-methods' is non-nil, we have to
9966           ;; select an article to give `gnus-read-move-group-name' an
9967           ;; opportunity to suggest an appropriate default.  However,
9968           ;; we needn't render or mark the article.
9969           (let ((gnus-display-mime-function nil)
9970                 (gnus-article-prepare-hook nil)
9971                 (gnus-mark-article-hook nil))
9972             (gnus-summary-select-article nil nil nil (car articles))))
9973       (setq to-newsgroup (gnus-read-move-group-name
9974                           (cadr (assq action names))
9975                           (symbol-value
9976                            (intern (format "gnus-current-%s-group" action)))
9977                           articles prefix)
9978             encoded to-newsgroup
9979             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9980       (set (intern (format "gnus-current-%s-group" action))
9981            (mm-decode-coding-string
9982             to-newsgroup
9983             (gnus-group-name-charset to-method to-newsgroup))))
9984     (unless to-method
9985       (setq to-method (or select-method
9986                           (gnus-server-to-method
9987                            (gnus-group-method to-newsgroup)))))
9988     (setq to-newsgroup
9989           (or encoded
9990               (and to-newsgroup
9991                    (mm-encode-coding-string
9992                     to-newsgroup
9993                     (gnus-group-name-charset to-method to-newsgroup)))))
9994     ;; Check the method we are to move this article to...
9995     (unless (gnus-check-backend-function
9996              'request-accept-article (car to-method))
9997       (error "%s does not support article copying" (car to-method)))
9998     (unless (gnus-check-server to-method)
9999       (error "Can't open server %s" (car to-method)))
10000     (gnus-message 6 "%s to %s: %s..."
10001                   (caddr (assq action names))
10002                   (or (car select-method)
10003                       (gnus-group-decoded-name to-newsgroup))
10004                   articles)
10005     (while articles
10006       (setq article (pop articles))
10007       ;; Set any marks that may have changed in the summary buffer.
10008       (when gnus-preserve-marks
10009         (gnus-summary-push-marks-to-backend article))
10010       (setq
10011        art-group
10012        (cond
10013         ;; Move the article.
10014         ((eq action 'move)
10015          ;; Remove this article from future suppression.
10016          (gnus-dup-unsuppress-article article)
10017          (let* ((from-method (gnus-find-method-for-group
10018                               gnus-newsgroup-name))
10019                 (to-method (or select-method
10020                                (gnus-find-method-for-group to-newsgroup)))
10021                 (move-is-internal (gnus-server-equal from-method to-method)))
10022            (gnus-request-move-article
10023             article                     ; Article to move
10024             gnus-newsgroup-name         ; From newsgroup
10025             (nth 1 (gnus-find-method-for-group
10026                     gnus-newsgroup-name)) ; Server
10027             (list 'gnus-request-accept-article
10028                   to-newsgroup (list 'quote select-method)
10029                   (not articles) t)     ; Accept form
10030             (not articles)              ; Only save nov last time
10031             (and move-is-internal
10032                  to-newsgroup           ; Not respooling
10033                                         ; Is this move internal?
10034                  (gnus-group-real-name to-newsgroup)))))
10035         ;; Copy the article.
10036         ((eq action 'copy)
10037          (with-current-buffer copy-buf
10038            (when (gnus-request-article-this-buffer article
10039                                                    gnus-newsgroup-name)
10040              (save-restriction
10041                (nnheader-narrow-to-headers)
10042                (dolist (hdr gnus-copy-article-ignored-headers)
10043                  (message-remove-header hdr t)))
10044              (gnus-request-accept-article
10045               to-newsgroup select-method (not articles) t))))
10046         ;; Crosspost the article.
10047         ((eq action 'crosspost)
10048          (let ((xref (message-tokenize-header
10049                       (mail-header-xref (gnus-summary-article-header
10050                                          article))
10051                       " ")))
10052            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
10053                                   ":" (number-to-string article)))
10054            (unless xref
10055              (setq xref (list (system-name))))
10056            (setq new-xref
10057                  (concat
10058                   (mapconcat 'identity
10059                              (delete "Xref:" (delete new-xref xref))
10060                              " ")
10061                   " " new-xref))
10062            (with-current-buffer copy-buf
10063              ;; First put the article in the destination group.
10064              (gnus-request-article-this-buffer article gnus-newsgroup-name)
10065              (when (consp (setq art-group
10066                                 (gnus-request-accept-article
10067                                  to-newsgroup select-method (not articles)
10068                                  t)))
10069                (setq new-xref (concat new-xref " " (car art-group)
10070                                       ":"
10071                                       (number-to-string (cdr art-group))))
10072                ;; Now we have the new Xrefs header, so we insert
10073                ;; it and replace the new article.
10074                (nnheader-replace-header "Xref" new-xref)
10075                (gnus-request-replace-article
10076                 (cdr art-group) to-newsgroup (current-buffer) t)
10077                art-group))))))
10078       (cond
10079        ((not art-group)
10080         (gnus-message 1 "Couldn't %s article %s: %s"
10081                       (cadr (assq action names)) article
10082                       (nnheader-get-report (car to-method))))
10083        ((eq art-group 'junk)
10084         (when (eq action 'move)
10085           (gnus-summary-mark-article article gnus-canceled-mark)
10086           (gnus-message 4 "Deleted article %s" article)
10087           ;; run the delete hook
10088           (run-hook-with-args 'gnus-summary-article-delete-hook
10089                               action
10090                               (gnus-data-header
10091                                (assoc article (gnus-data-list nil)))
10092                               gnus-newsgroup-name nil
10093                               select-method)))
10094        (t
10095         (let* ((pto-group (gnus-group-prefixed-name
10096                            (car art-group) to-method))
10097                (info (gnus-get-info pto-group))
10098                (to-group (gnus-info-group info))
10099                to-marks)
10100           ;; Update the group that has been moved to.
10101           (when (and info
10102                      (memq action '(move copy)))
10103             (unless (member to-group to-groups)
10104               (push to-group to-groups))
10105
10106             (when (and (not (memq article gnus-newsgroup-unreads))
10107                        (cdr art-group))
10108               (push 'read to-marks)
10109               (gnus-info-set-read
10110                info (gnus-add-to-range (gnus-info-read info)
10111                                        (list (cdr art-group)))))
10112
10113             ;; See whether the article is to be put in the cache.
10114             (let* ((expirable (gnus-group-auto-expirable-p to-group))
10115                    (marks (if expirable
10116                               gnus-article-mark-lists
10117                             (delete '(expirable . expire)
10118                                     (copy-sequence
10119                                      gnus-article-mark-lists))))
10120                    (to-article (cdr art-group)))
10121
10122               ;; Enter the article into the cache in the new group,
10123               ;; if that is required.
10124               (when (and to-article
10125                          gnus-use-cache)
10126                 (gnus-cache-possibly-enter-article
10127                  to-group to-article
10128                  (memq article gnus-newsgroup-marked)
10129                  (memq article gnus-newsgroup-dormant)
10130                  (memq article gnus-newsgroup-unreads)))
10131
10132               (when (and gnus-preserve-marks
10133                          to-article)
10134                 ;; Copy any marks over to the new group.
10135                 (when (and (equal to-group gnus-newsgroup-name)
10136                            (not (memq article gnus-newsgroup-unreads)))
10137                   ;; Mark this article as read in this group.
10138                   (push (cons to-article gnus-read-mark)
10139                         gnus-newsgroup-reads)
10140                   ;; Increase the active status of this group.
10141                   (setcdr (gnus-active to-group) to-article)
10142                   (setcdr gnus-newsgroup-active to-article))
10143
10144                 (while marks
10145                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10146                     (when (memq article (symbol-value
10147                                          (intern (format "gnus-newsgroup-%s"
10148                                                          (caar marks)))))
10149                       (push (cdar marks) to-marks)
10150                       ;; If the other group is the same as this group,
10151                       ;; then we have to add the mark to the list.
10152                       (when (equal to-group gnus-newsgroup-name)
10153                         (set (intern (format "gnus-newsgroup-%s"
10154                                              (caar marks)))
10155                              (cons to-article
10156                                    (symbol-value
10157                                     (intern (format "gnus-newsgroup-%s"
10158                                                     (caar marks)))))))
10159                       ;; Copy the marks to other group.
10160                       (gnus-add-marked-articles
10161                        to-group (cdar marks) (list to-article) info)))
10162                   (setq marks (cdr marks)))
10163
10164                 (when (and expirable
10165                            gnus-mark-copied-or-moved-articles-as-expirable
10166                            (not (memq 'expire to-marks)))
10167                   ;; Mark this article as expirable.
10168                   (push 'expire to-marks)
10169                   (when (equal to-group gnus-newsgroup-name)
10170                     (push to-article gnus-newsgroup-expirable))
10171                   ;; Copy the expirable mark to other group.
10172                   (gnus-add-marked-articles
10173                    to-group 'expire (list to-article) info))
10174
10175                 (when (and to-marks
10176                            (gnus-method-option-p
10177                             (gnus-find-method-for-group to-group)
10178                             'server-marks))
10179                   (gnus-request-set-mark
10180                    to-group (list (list (list to-article) 'add to-marks)))))
10181
10182               (gnus-dribble-enter
10183                (concat "(gnus-group-set-info '"
10184                        (gnus-prin1-to-string (gnus-get-info to-group))
10185                        ")")
10186                (concat "^(gnus-group-set-info '(\""
10187                        (regexp-quote to-group) "\""))))
10188
10189           ;; Update the Xref header in this article to point to
10190           ;; the new crossposted article we have just created.
10191           (when (eq action 'crosspost)
10192             (with-current-buffer copy-buf
10193               (gnus-request-article-this-buffer article gnus-newsgroup-name)
10194               (nnheader-replace-header "Xref" new-xref)
10195               (gnus-request-replace-article
10196                article gnus-newsgroup-name (current-buffer) t)))
10197
10198           ;; run the move/copy/crosspost/respool hook
10199           (run-hook-with-args 'gnus-summary-article-move-hook
10200                               action
10201                               (gnus-data-header
10202                                (assoc article (gnus-data-list nil)))
10203                               gnus-newsgroup-name
10204                               to-newsgroup
10205                               select-method))
10206
10207         ;;;!!!Why is this necessary?
10208         (set-buffer gnus-summary-buffer)
10209
10210         (when (eq action 'move)
10211           (save-excursion
10212             (gnus-summary-goto-subject article)
10213             (gnus-summary-mark-article article gnus-canceled-mark)))))
10214       (push article articles-to-update-marks))
10215
10216     (save-excursion
10217       (apply 'gnus-summary-remove-process-mark articles-to-update-marks))
10218     ;; Re-activate all groups that have been moved to.
10219     (with-current-buffer gnus-group-buffer
10220       (let ((gnus-group-marked to-groups))
10221         (gnus-group-get-new-news-this-group nil t)))
10222
10223     (gnus-kill-buffer copy-buf)
10224     (gnus-summary-position-point)
10225     (gnus-set-mode-line 'summary)))
10226
10227 (defun gnus-summary-push-marks-to-backend (article)
10228   (let ((set nil)
10229         (del nil)
10230         (marks gnus-article-mark-lists))
10231     (unless (memq article gnus-newsgroup-unreads)
10232       (push 'read set))
10233     (while marks
10234       (if (and (eq (gnus-article-mark-to-type (cdar marks)) 'list)
10235                (memq article (symbol-value
10236                               (intern (format "gnus-newsgroup-%s"
10237                                               (caar marks))))))
10238           (push (cdar marks) set)
10239         (push (cdar marks) del))
10240       (pop marks))
10241     (gnus-request-set-mark gnus-newsgroup-name `(((,article) set ,set)
10242                                                  ((,article) del ,del)))))
10243
10244 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10245   "Copy the current article to some other group.
10246 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
10247 When called interactively, if TO-NEWSGROUP is nil, use the value of
10248 the variable `gnus-move-split-methods' for finding a default target
10249 newsgroup.
10250 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
10251 re-spool using this method."
10252   (interactive "P")
10253   (gnus-summary-move-article n to-newsgroup select-method 'copy))
10254
10255 (defun gnus-summary-crosspost-article (&optional n)
10256   "Crosspost the current article to some other group."
10257   (interactive "P")
10258   (gnus-summary-move-article n nil nil 'crosspost))
10259
10260 (defcustom gnus-summary-respool-default-method nil
10261   "Default method type for respooling an article.
10262 If nil, use to the current newsgroup method."
10263   :type 'symbol
10264   :group 'gnus-summary-mail)
10265
10266 (defun gnus-summary-respool-article (&optional n method)
10267   "Respool the current article.
10268 The article will be squeezed through the mail spooling process again,
10269 which means that it will be put in some mail newsgroup or other
10270 depending on `nnmail-split-methods'.
10271 If N is a positive number, respool the N next articles.
10272 If N is a negative number, respool the N previous articles.
10273 If N is nil and any articles have been marked with the process mark,
10274 respool those articles instead.
10275
10276 Respooling can be done both from mail groups and \"real\" newsgroups.
10277 In the former case, the articles in question will be moved from the
10278 current group into whatever groups they are destined to.  In the
10279 latter case, they will be copied into the relevant groups."
10280   (interactive
10281    (list current-prefix-arg
10282          (let* ((methods (mapcar #'car (gnus-methods-using 'respool)))
10283                 (methname
10284                  (symbol-name (or gnus-summary-respool-default-method
10285                                   (car (gnus-find-method-for-group
10286                                         gnus-newsgroup-name)))))
10287                 (method
10288                  (gnus-completing-read
10289                   "Backend to use when respooling"
10290                   methods t nil 'gnus-mail-method-history methname))
10291                 ms)
10292            (cond
10293             ((zerop (length (setq ms (gnus-servers-using-backend
10294                                       (intern method)))))
10295              (list (intern method) ""))
10296             ((= 1 (length ms))
10297              (car ms))
10298             (t
10299              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
10300                (cdr (assoc (gnus-completing-read "Server name" ms-alist t)
10301                            ms-alist))))))))
10302   (unless method
10303     (error "No method given for respooling"))
10304   (if (assoc (symbol-name
10305               (car (gnus-find-method-for-group gnus-newsgroup-name)))
10306              (gnus-methods-using 'respool))
10307       (gnus-summary-move-article n nil method)
10308     (gnus-summary-copy-article n nil method)))
10309
10310 (defun gnus-summary-import-article (file &optional edit)
10311   "Import an arbitrary file into a mail newsgroup."
10312   (interactive "fImport file: \nP")
10313   (let ((group gnus-newsgroup-name)
10314         (now (current-time))
10315         atts lines group-art)
10316     (unless (gnus-check-backend-function 'request-accept-article group)
10317       (error "%s does not support article importing" group))
10318     (or (file-readable-p file)
10319         (not (file-regular-p file))
10320         (error "Can't read %s" file))
10321     (with-current-buffer (gnus-get-buffer-create " *import file*")
10322       (erase-buffer)
10323       (nnheader-insert-file-contents file)
10324       (goto-char (point-min))
10325       (if (nnheader-article-p)
10326           (save-restriction
10327             (goto-char (point-min))
10328             (search-forward "\n\n" nil t)
10329             (narrow-to-region (point-min) (1- (point)))
10330             (goto-char (point-min))
10331             (unless (re-search-forward "^date:" nil t)
10332               (goto-char (point-max))
10333               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
10334        ;; This doesn't look like an article, so we fudge some headers.
10335         (setq atts (file-attributes file)
10336               lines (count-lines (point-min) (point-max)))
10337         (insert "From: " (read-string "From: ") "\n"
10338                 "Subject: " (read-string "Subject: ") "\n"
10339                 "Date: " (message-make-date (nth 5 atts)) "\n"
10340                 "Message-ID: " (message-make-message-id) "\n"
10341                 "Lines: " (int-to-string lines) "\n"
10342                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10343       (setq group-art (gnus-request-accept-article group nil t))
10344       (kill-buffer (current-buffer)))
10345     (setq gnus-newsgroup-active (gnus-activate-group group))
10346     (forward-line 1)
10347     (gnus-summary-goto-article (cdr group-art) nil t)
10348     (when edit
10349       (gnus-summary-edit-article))))
10350
10351 (defun gnus-summary-create-article ()
10352   "Create an article in a mail newsgroup."
10353   (interactive)
10354   (let ((group gnus-newsgroup-name)
10355         (now (current-time))
10356         group-art)
10357     (unless (gnus-check-backend-function 'request-accept-article group)
10358       (error "%s does not support article importing" group))
10359     (with-current-buffer (gnus-get-buffer-create " *import file*")
10360       (erase-buffer)
10361       (goto-char (point-min))
10362       ;; This doesn't look like an article, so we fudge some headers.
10363       (insert "From: " (read-string "From: ") "\n"
10364               "Subject: " (read-string "Subject: ") "\n"
10365               "Date: " (message-make-date now) "\n"
10366               "Message-ID: " (message-make-message-id) "\n")
10367       (setq group-art (gnus-request-accept-article group nil t))
10368       (kill-buffer (current-buffer)))
10369     (setq gnus-newsgroup-active (gnus-activate-group group))
10370     (forward-line 1)
10371     (gnus-summary-goto-article (cdr group-art) nil t)
10372     (gnus-summary-edit-article)))
10373
10374 (defun gnus-summary-article-posted-p ()
10375   "Say whether the current (mail) article is available from news as well.
10376 This will be the case if the article has both been mailed and posted."
10377   (interactive)
10378   (let ((id (mail-header-references (gnus-summary-article-header)))
10379         (gnus-override-method (car (gnus-refer-article-methods))))
10380     (if (gnus-request-head id "")
10381         (gnus-message 2 "The current message was found on %s"
10382                       gnus-override-method)
10383       (gnus-message 2 "The current message couldn't be found on %s"
10384                     gnus-override-method)
10385       nil)))
10386
10387 (defun gnus-summary-expire-articles (&optional now)
10388   "Expire all articles that are marked as expirable in the current group."
10389   (interactive)
10390   (when (and (not gnus-group-is-exiting-without-update-p)
10391              (gnus-check-backend-function
10392               'request-expire-articles gnus-newsgroup-name))
10393     ;; This backend supports expiry.
10394     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10395            (expirable
10396             (gnus-list-range-difference
10397              (if total
10398                  (progn
10399                    ;; We need to update the info for
10400                    ;; this group for `gnus-list-of-read-articles'
10401                    ;; to give us the right answer.
10402                    (gnus-run-hooks 'gnus-exit-group-hook)
10403                    (gnus-summary-update-info)
10404                    (gnus-list-of-read-articles gnus-newsgroup-name))
10405                (setq gnus-newsgroup-expirable
10406                      (sort gnus-newsgroup-expirable '<)))
10407              gnus-newsgroup-unexist))
10408            (expiry-wait (if now 'immediate
10409                           (gnus-group-find-parameter
10410                            gnus-newsgroup-name 'expiry-wait)))
10411            (nnmail-expiry-target
10412             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
10413                 nnmail-expiry-target))
10414            es)
10415       (when expirable
10416         ;; There are expirable articles in this group, so we run them
10417         ;; through the expiry process.
10418         (gnus-message 6 "Expiring articles...")
10419         (when (gnus-check-group gnus-newsgroup-name)
10420           ;; The list of articles that weren't expired is returned.
10421           (save-excursion
10422             (if expiry-wait
10423                 (let ((nnmail-expiry-wait-function nil)
10424                       (nnmail-expiry-wait expiry-wait))
10425                   (setq es (gnus-request-expire-articles
10426                             expirable gnus-newsgroup-name)))
10427               (setq es (gnus-request-expire-articles
10428                         expirable gnus-newsgroup-name)))
10429             (unless total
10430               (setq gnus-newsgroup-expirable es))
10431             ;; We go through the old list of expirable, and mark all
10432             ;; really expired articles as nonexistent.
10433             (unless (eq es expirable) ;If nothing was expired, we don't mark.
10434               (let ((gnus-use-cache nil))
10435                 (dolist (article expirable)
10436                   (when (and (not (memq article es))
10437                              (gnus-data-find article))
10438                     (gnus-summary-mark-article article gnus-canceled-mark)
10439                     (run-hook-with-args 'gnus-summary-article-expire-hook
10440                                         'delete
10441                                         (gnus-data-header
10442                                          (assoc article (gnus-data-list nil)))
10443                                         gnus-newsgroup-name
10444                                         nil
10445                                         nil)))))))
10446         (gnus-message 6 "Expiring articles...done")))))
10447
10448 (defun gnus-summary-expire-articles-now ()
10449   "Expunge all expirable articles in the current group.
10450 This means that *all* articles that are marked as expirable will be
10451 deleted forever, right now."
10452   (interactive)
10453   (or gnus-expert-user
10454       (gnus-yes-or-no-p
10455        "Are you really, really sure you want to delete all expirable messages? ")
10456       (error "Phew!"))
10457   (gnus-summary-expire-articles t))
10458
10459 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10460 (defun gnus-summary-delete-article (&optional n)
10461   "Delete the N next (mail) articles.
10462 This command actually deletes articles.  This is not a marking
10463 command.  The article will disappear forever from your life, never to
10464 return.
10465
10466 If N is negative, delete backwards.
10467 If N is nil and articles have been marked with the process mark,
10468 delete these instead.
10469
10470 If `gnus-novice-user' is non-nil you will be asked for
10471 confirmation before the articles are deleted."
10472   (interactive "P")
10473   (unless (gnus-check-backend-function 'request-expire-articles
10474                                        gnus-newsgroup-name)
10475     (error "The current newsgroup does not support article deletion"))
10476   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10477     (error "Couldn't open server"))
10478   ;; Compute the list of articles to delete.
10479   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10480         (nnmail-expiry-target 'delete)
10481         not-deleted)
10482     (if (and gnus-novice-user
10483              (not (gnus-yes-or-no-p
10484                    (format "Do you really want to delete %s forever? "
10485                            (if (> (length articles) 1)
10486                                (format "these %s articles" (length articles))
10487                              "this article")))))
10488         ()
10489       ;; Delete the articles.
10490       (setq not-deleted (gnus-request-expire-articles
10491                          articles gnus-newsgroup-name 'force))
10492       (save-excursion
10493         (while articles
10494           (gnus-summary-remove-process-mark (car articles))
10495           ;; The backend might not have been able to delete the article
10496           ;; after all.
10497           (unless (memq (car articles) not-deleted)
10498             (gnus-summary-mark-article (car articles) gnus-canceled-mark)
10499             (let* ((article (car articles))
10500                    (ghead  (gnus-data-header
10501                             (assoc article (gnus-data-list nil)))))
10502               (run-hook-with-args 'gnus-summary-article-delete-hook
10503                                   'delete ghead gnus-newsgroup-name nil
10504                                   nil)))
10505           (setq articles (cdr articles))))
10506       (when not-deleted
10507         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10508     (gnus-summary-position-point)
10509     (gnus-set-mode-line 'summary)
10510     not-deleted))
10511
10512 (defun gnus-summary-edit-article (&optional arg)
10513   "Edit the current article.
10514 This will have permanent effect only in mail groups.
10515 If ARG is nil, edit the decoded articles.
10516 If ARG is 1, edit the raw articles.
10517 If ARG is 2, edit the raw articles even in read-only groups.
10518 If ARG is 3, edit the articles with the current handles.
10519 Otherwise, allow editing of articles even in read-only
10520 groups."
10521   (interactive "P")
10522   (let (force raw current-handles)
10523     (cond
10524      ((null arg))
10525      ((eq arg 1)
10526       (setq raw t))
10527      ((eq arg 2)
10528       (setq raw t
10529             force t))
10530      ((eq arg 3)
10531       (setq current-handles
10532             (and (gnus-buffer-live-p gnus-article-buffer)
10533                  (with-current-buffer gnus-article-buffer
10534                    (prog1
10535                        gnus-article-mime-handles
10536                      (setq gnus-article-mime-handles nil))))))
10537      (t
10538       (setq force t)))
10539     (when (and raw (not force)
10540                (member gnus-newsgroup-name '("nndraft:delayed"
10541                                              "nndraft:drafts"
10542                                              "nndraft:queue")))
10543       (error "Can't edit the raw article in group %s"
10544              gnus-newsgroup-name))
10545     (with-current-buffer gnus-summary-buffer
10546       (let ((mail-parse-charset gnus-newsgroup-charset)
10547             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10548         (gnus-set-global-variables)
10549         (when (and (not force)
10550                    (gnus-group-read-only-p))
10551           (error "The current newsgroup does not support article editing"))
10552         (gnus-summary-show-article t)
10553         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10554           (with-current-buffer gnus-article-buffer
10555             (mm-enable-multibyte)))
10556         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10557             (setq raw t))
10558         (gnus-article-edit-article
10559          (if raw 'ignore
10560            `(lambda ()
10561               (let ((mbl mml-buffer-list))
10562                 (setq mml-buffer-list nil)
10563                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10564                   (mime-to-mml ,'current-handles))
10565                 (let ((mbl1 mml-buffer-list))
10566                   (setq mml-buffer-list mbl)
10567                   (set (make-local-variable 'mml-buffer-list) mbl1))
10568                 (gnus-make-local-hook 'kill-buffer-hook)
10569                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10570          `(lambda (no-highlight)
10571             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10572                   (message-options message-options)
10573                   (message-options-set-recipient)
10574                   (mail-parse-ignored-charsets
10575                    ',gnus-newsgroup-ignored-charsets)
10576                   (rfc2047-header-encoding-alist
10577                    ',(let ((charset (gnus-group-name-charset
10578                                      (gnus-find-method-for-group
10579                                       gnus-newsgroup-name)
10580                                      gnus-newsgroup-name)))
10581                        (append (list (cons "Newsgroups" charset)
10582                                      (cons "Followup-To" charset)
10583                                      (cons "Xref" charset))
10584                                rfc2047-header-encoding-alist))))
10585               ,(if (not raw) '(progn
10586                                 (mml-to-mime)
10587                                 (mml-destroy-buffers)
10588                                 (remove-hook 'kill-buffer-hook
10589                                              'mml-destroy-buffers t)
10590                                 (kill-local-variable 'mml-buffer-list)))
10591               (gnus-summary-edit-article-done
10592                ,(or (mail-header-references gnus-current-headers) "")
10593                ,(gnus-group-read-only-p)
10594                ,gnus-summary-buffer no-highlight))))))))
10595
10596 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10597
10598 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10599                                                  no-highlight)
10600   "Make edits to the current article permanent."
10601   (interactive)
10602   (save-excursion
10603     ;; The buffer restriction contains the entire article if it exists.
10604     (when (article-goto-body)
10605       (let ((lines (count-lines (point) (point-max)))
10606             (length (- (point-max) (point)))
10607             (case-fold-search t)
10608             (body (copy-marker (point))))
10609         (goto-char (point-min))
10610         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10611           (delete-region (match-beginning 1) (match-end 1))
10612           (insert (number-to-string length)))
10613         (goto-char (point-min))
10614         (when (re-search-forward
10615                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10616           (delete-region (match-beginning 1) (match-end 1))
10617           (insert (number-to-string length)))
10618         (goto-char (point-min))
10619         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10620           (delete-region (match-beginning 1) (match-end 1))
10621           (insert (number-to-string lines))))))
10622   ;; Replace the article.
10623   (let ((buf (current-buffer))
10624         (article (cdr gnus-article-current))
10625         replace-result)
10626     (with-temp-buffer
10627       (insert-buffer-substring buf)
10628       (if (and (not read-only)
10629                (not (setq replace-result
10630                           (gnus-request-replace-article
10631                            article (car gnus-article-current)
10632                            (current-buffer) t))))
10633           (error "Couldn't replace article")
10634         ;; If we got a number back, then that's the new article number
10635         ;; for this article.  Otherwise, the article number didn't change.
10636         (when (numberp replace-result)
10637           (with-current-buffer gnus-summary-buffer
10638             (setq gnus-newsgroup-limit (delq article gnus-newsgroup-limit))
10639             (gnus-summary-limit gnus-newsgroup-limit)
10640             (setq article replace-result)
10641             (gnus-summary-goto-subject article t)))
10642         ;; Update the summary buffer.
10643         (if (and references
10644                  (equal (message-tokenize-header references " ")
10645                         (message-tokenize-header
10646                          (or (message-fetch-field "references") "") " ")))
10647             ;; We only have to update this line.
10648             (save-excursion
10649               (save-restriction
10650                 (message-narrow-to-head)
10651                 (let ((head (buffer-substring-no-properties
10652                              (point-min) (point-max)))
10653                       header)
10654                   (with-temp-buffer
10655                     (insert (format "211 %d Article retrieved.\n" article))
10656                     (insert head)
10657                     (insert ".\n")
10658                     (let ((nntp-server-buffer (current-buffer)))
10659                       (setq header (car (gnus-get-newsgroup-headers nil t))))
10660                     (with-current-buffer gnus-summary-buffer
10661                       (gnus-data-set-header (gnus-data-find article) header)
10662                       (gnus-summary-update-article-line article header)
10663                       (if (gnus-summary-goto-subject article nil t)
10664                           (gnus-summary-update-secondary-mark article)))))))
10665           ;; Update threads.
10666           (set-buffer (or buffer gnus-summary-buffer))
10667           (gnus-summary-update-article article)
10668           (if (gnus-summary-goto-subject article nil t)
10669               (gnus-summary-update-secondary-mark article)))
10670         ;; Prettify the article buffer again.
10671         (unless no-highlight
10672           (with-current-buffer gnus-article-buffer
10673             ;;!!! Fix this -- article should be rehighlighted.
10674             ;;(gnus-run-hooks 'gnus-article-display-hook)
10675             (set-buffer gnus-original-article-buffer)
10676             (gnus-request-article
10677              article (car gnus-article-current) (current-buffer))))
10678         ;; Prettify the summary buffer line.
10679         (when (gnus-visual-p 'summary-highlight 'highlight)
10680           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10681
10682 (defun gnus-summary-edit-wash (key)
10683   "Perform editing command KEY in the article buffer."
10684   (interactive
10685    (list
10686     (progn
10687       (message "%s" (concat (this-command-keys) "- "))
10688       (read-char))))
10689   (message "")
10690   (gnus-summary-edit-article)
10691   (execute-kbd-macro (concat (this-command-keys) key))
10692   (gnus-article-edit-done))
10693
10694 ;;; Respooling
10695
10696 (defvar nnimap-split-fancy)
10697 (defvar nnimap-split-methods)
10698
10699 (defun gnus-summary-respool-query (&optional silent trace)
10700   "Query where the respool algorithm would put this article."
10701   (interactive)
10702   (let (gnus-mark-article-hook)
10703     (gnus-summary-select-article)
10704     (with-current-buffer gnus-original-article-buffer
10705       (let ((groups
10706              (if (eq (car (gnus-find-method-for-group gnus-newsgroup-name))
10707                      'nnimap)
10708                  ;; nnimap has its own splitting variables.
10709                  (let ((nnmail-split-methods
10710                         (cond
10711                          ((eq nnimap-split-methods 'default)
10712                           nnmail-split-methods)
10713                          (nnimap-split-methods
10714                           nnimap-split-methods)
10715                          (nnimap-split-fancy
10716                           'nnmail-split-fancy)))
10717                        (nnmail-split-fancy (or nnimap-split-fancy
10718                                                nnmail-split-fancy)))
10719                    (nnmail-article-group 'identity trace))
10720                (nnmail-article-group 'identity trace))))
10721         (unless silent
10722           (if groups
10723               (message "This message would go to %s"
10724                        (mapconcat 'car groups ", "))
10725             (message "This message would go to no groups"))
10726           groups)))))
10727
10728 (defun gnus-summary-respool-trace ()
10729   "Trace where the respool algorithm would put this article.
10730 Display a buffer showing all fancy splitting patterns which matched."
10731   (interactive)
10732   (gnus-summary-respool-query nil t))
10733
10734 ;; Summary marking commands.
10735
10736 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10737   "Mark articles which has the same subject as read, and then select the next.
10738 If UNMARK is positive, remove any kind of mark.
10739 If UNMARK is negative, tick articles."
10740   (interactive "P")
10741   (when unmark
10742     (setq unmark (prefix-numeric-value unmark)))
10743   (let ((count
10744          (gnus-summary-mark-same-subject
10745           (gnus-summary-article-subject) unmark)))
10746     ;; Select next unread article.  If auto-select-same mode, should
10747     ;; select the first unread article.
10748     (gnus-summary-next-article t (and gnus-auto-select-same
10749                                       (gnus-summary-article-subject)))
10750     (gnus-message 7 "%d article%s marked as %s"
10751                   count (if (= count 1) " is" "s are")
10752                   (if unmark "unread" "read"))))
10753
10754 (defun gnus-summary-kill-same-subject (&optional unmark)
10755   "Mark articles which has the same subject as read.
10756 If UNMARK is positive, remove any kind of mark.
10757 If UNMARK is negative, tick articles."
10758   (interactive "P")
10759   (when unmark
10760     (setq unmark (prefix-numeric-value unmark)))
10761   (let ((count
10762          (gnus-summary-mark-same-subject
10763           (gnus-summary-article-subject) unmark)))
10764     ;; If marked as read, go to next unread subject.
10765     (when (null unmark)
10766       ;; Go to next unread subject.
10767       (gnus-summary-next-subject 1 t))
10768     (gnus-message 7 "%d articles are marked as %s"
10769                   count (if unmark "unread" "read"))))
10770
10771 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10772   "Mark articles with same SUBJECT as read, and return marked number.
10773 If optional argument UNMARK is positive, remove any kinds of marks.
10774 If optional argument UNMARK is negative, mark articles as unread instead."
10775   (let ((count 1))
10776     (save-excursion
10777       (cond
10778        ((null unmark)                   ; Mark as read.
10779         (while (and
10780                 (progn
10781                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10782                   (gnus-summary-show-thread) t)
10783                 (gnus-summary-find-subject subject))
10784           (setq count (1+ count))))
10785        ((> unmark 0)                    ; Tick.
10786         (while (and
10787                 (progn
10788                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10789                   (gnus-summary-show-thread) t)
10790                 (gnus-summary-find-subject subject))
10791           (setq count (1+ count))))
10792        (t                               ; Mark as unread.
10793         (while (and
10794                 (progn
10795                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10796                   (gnus-summary-show-thread) t)
10797                 (gnus-summary-find-subject subject))
10798           (setq count (1+ count)))))
10799       (gnus-set-mode-line 'summary)
10800       ;; Return the number of marked articles.
10801       count)))
10802
10803 (defun gnus-summary-mark-as-processable (n &optional unmark)
10804   "Set the process mark on the next N articles.
10805 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10806 the process mark instead.  The difference between N and the actual
10807 number of articles marked is returned."
10808   (interactive "P")
10809   (if (and (null n) (gnus-region-active-p))
10810       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10811     (setq n (prefix-numeric-value n))
10812     (let ((backward (< n 0))
10813           (n (abs n)))
10814       (while (and
10815               (> n 0)
10816               (if unmark
10817                   (gnus-summary-remove-process-mark
10818                    (gnus-summary-article-number))
10819                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10820               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10821         (setq n (1- n)))
10822       (when (/= 0 n)
10823         (gnus-message 7 "No more articles"))
10824       (gnus-summary-recenter)
10825       (gnus-summary-position-point)
10826       n)))
10827
10828 (defun gnus-summary-unmark-as-processable (n)
10829   "Remove the process mark from the next N articles.
10830 If N is negative, unmark backward instead.  The difference between N and
10831 the actual number of articles unmarked is returned."
10832   (interactive "P")
10833   (gnus-summary-mark-as-processable n t))
10834
10835 (defun gnus-summary-unmark-all-processable ()
10836   "Remove the process mark from all articles."
10837   (interactive)
10838   (save-excursion
10839     (while gnus-newsgroup-processable
10840       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10841   (gnus-summary-position-point))
10842
10843 (defun gnus-summary-add-mark (article type)
10844   "Mark ARTICLE with a mark of TYPE."
10845   (let ((vtype (car (assq type gnus-article-mark-lists)))
10846         var)
10847     (if (not vtype)
10848         (error "No such mark type: %s" type)
10849       (setq var (intern (format "gnus-newsgroup-%s" type)))
10850       (set var (cons article (symbol-value var)))
10851       (if (memq type '(processable cached replied forwarded recent saved))
10852           (gnus-summary-update-secondary-mark article)
10853         ;;; !!! This is bogus.  We should find out what primary
10854         ;;; !!! mark we want to set.
10855         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10856
10857 (defun gnus-summary-mark-as-expirable (n)
10858   "Mark N articles forward as expirable.
10859 If N is negative, mark backward instead.  The difference between N and
10860 the actual number of articles marked is returned."
10861   (interactive "p")
10862   (gnus-summary-mark-forward n gnus-expirable-mark))
10863
10864 (defun gnus-summary-mark-as-spam (n)
10865   "Mark N articles forward as spam.
10866 If N is negative, mark backward instead.  The difference between N and
10867 the actual number of articles marked is returned."
10868   (interactive "p")
10869   (gnus-summary-mark-forward n gnus-spam-mark))
10870
10871 (defun gnus-summary-mark-article-as-replied (article)
10872   "Mark ARTICLE as replied to and update the summary line.
10873 ARTICLE can also be a list of articles."
10874   (interactive (list (gnus-summary-article-number)))
10875   (let ((articles (if (listp article) article (list article))))
10876     (dolist (article articles)
10877       (unless (numberp article)
10878         (error "%s is not a number" article))
10879       (push article gnus-newsgroup-replied)
10880       (let ((inhibit-read-only t))
10881         (when (gnus-summary-goto-subject article nil t)
10882           (gnus-summary-update-secondary-mark article))))))
10883
10884 (defun gnus-summary-mark-article-as-forwarded (article)
10885   "Mark ARTICLE as forwarded and update the summary line.
10886 ARTICLE can also be a list of articles."
10887   (let ((articles (if (listp article) article (list article))))
10888     (dolist (article articles)
10889       (push article gnus-newsgroup-forwarded)
10890       (let ((inhibit-read-only t))
10891         (when (gnus-summary-goto-subject article nil t)
10892           (gnus-summary-update-secondary-mark article))))))
10893
10894 (defun gnus-summary-set-bookmark (article)
10895   "Set a bookmark in current article."
10896   (interactive (list (gnus-summary-article-number)))
10897   (when (or (not (get-buffer gnus-article-buffer))
10898             (not gnus-current-article)
10899             (not gnus-article-current)
10900             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10901     (error "No current article selected"))
10902   ;; Remove old bookmark, if one exists.
10903   (gnus-alist-pull article gnus-newsgroup-bookmarks)
10904   ;; Set the new bookmark, which is on the form
10905   ;; (article-number . line-number-in-body).
10906   (push
10907    (cons article
10908          (with-current-buffer gnus-article-buffer
10909            (count-lines
10910             (min (point)
10911                  (save-excursion
10912                    (article-goto-body)
10913                    (point)))
10914             (point))))
10915    gnus-newsgroup-bookmarks)
10916   (gnus-message 6 "A bookmark has been added to the current article."))
10917
10918 (defun gnus-summary-remove-bookmark (article)
10919   "Remove the bookmark from the current article."
10920   (interactive (list (gnus-summary-article-number)))
10921   ;; Remove old bookmark, if one exists.
10922   (if (not (assq article gnus-newsgroup-bookmarks))
10923       (gnus-message 6 "No bookmark in current article.")
10924     (gnus-alist-pull article gnus-newsgroup-bookmarks)
10925     (gnus-message 6 "Removed bookmark.")))
10926
10927 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10928 (defun gnus-summary-mark-as-dormant (n)
10929   "Mark N articles forward as dormant.
10930 If N is negative, mark backward instead.  The difference between N and
10931 the actual number of articles marked is returned."
10932   (interactive "p")
10933   (gnus-summary-mark-forward n gnus-dormant-mark))
10934
10935 (defun gnus-summary-set-process-mark (article)
10936   "Set the process mark on ARTICLE and update the summary line."
10937   (setq gnus-newsgroup-processable
10938         (cons article
10939               (delq article gnus-newsgroup-processable)))
10940   (when (gnus-summary-goto-subject article)
10941     (gnus-summary-show-thread)
10942     (gnus-summary-goto-subject article)
10943     (gnus-summary-update-secondary-mark article)))
10944
10945 (defun gnus-summary-remove-process-mark (&rest articles)
10946   "Remove the process mark from ARTICLES and update the summary line."
10947   (dolist (article articles)
10948     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10949     (when (gnus-summary-goto-subject article)
10950       (gnus-summary-show-thread)
10951       (gnus-summary-goto-subject article)
10952       (gnus-summary-update-secondary-mark article)))
10953   t)
10954
10955 (defun gnus-summary-set-saved-mark (article)
10956   "Set the process mark on ARTICLE and update the summary line."
10957   (push article gnus-newsgroup-saved)
10958   (when (gnus-summary-goto-subject article)
10959     (gnus-summary-update-secondary-mark article)))
10960
10961 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10962   "Mark N articles as read forwards.
10963 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10964 The difference between N and the actual number of articles marked is
10965 returned.
10966 If NO-EXPIRE, auto-expiry will be inhibited."
10967   (interactive "p")
10968   (gnus-summary-show-thread)
10969   (let ((backward (< n 0))
10970         (gnus-summary-goto-unread
10971          (and gnus-summary-goto-unread
10972               (not (eq gnus-summary-goto-unread 'never))
10973               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10974                                     gnus-ticked-mark gnus-dormant-mark)))))
10975         (n (abs n))
10976         (mark (or mark gnus-del-mark)))
10977     (while (and (> n 0)
10978                 (gnus-summary-mark-article nil mark no-expire)
10979                 (zerop (gnus-summary-next-subject
10980                         (if backward -1 1)
10981                         (and gnus-summary-goto-unread
10982                              (not (eq gnus-summary-goto-unread 'never)))
10983                         t)))
10984       (setq n (1- n)))
10985     (when (/= 0 n)
10986       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10987     (gnus-summary-recenter)
10988     (gnus-summary-position-point)
10989     (gnus-set-mode-line 'summary)
10990     n))
10991
10992 (defun gnus-summary-mark-article-as-read (mark)
10993   "Mark the current article quickly as read with MARK."
10994   (let ((article (gnus-summary-article-number)))
10995     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10996     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10997     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10998     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10999     (push (cons article mark) gnus-newsgroup-reads)
11000     ;; Possibly remove from cache, if that is used.
11001     (when gnus-use-cache
11002       (gnus-cache-enter-remove-article article))
11003     ;; Allow the backend to change the mark.
11004     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11005     ;; Check for auto-expiry.
11006     (when (and gnus-newsgroup-auto-expire
11007                (memq mark gnus-auto-expirable-marks))
11008       (setq mark gnus-expirable-mark)
11009       ;; Let the backend know about the mark change.
11010       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11011       (push article gnus-newsgroup-expirable))
11012     ;; Set the mark in the buffer.
11013     (gnus-summary-update-mark mark 'unread)
11014     t))
11015
11016 (defun gnus-summary-mark-article-as-unread (mark)
11017   "Mark the current article quickly as unread with MARK."
11018   (let* ((article (gnus-summary-article-number))
11019          (old-mark (gnus-summary-article-mark article)))
11020     ;; Allow the backend to change the mark.
11021     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11022     (if (eq mark old-mark)
11023         t
11024       (if (<= article 0)
11025           (progn
11026             (gnus-error 1 "Can't mark negative article numbers")
11027             nil)
11028         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11029         (setq gnus-newsgroup-spam-marked
11030               (delq article gnus-newsgroup-spam-marked))
11031         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11032         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11033         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11034         (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11035         (cond ((= mark gnus-ticked-mark)
11036                (setq gnus-newsgroup-marked
11037                      (gnus-add-to-sorted-list gnus-newsgroup-marked
11038                                               article)))
11039               ((= mark gnus-spam-mark)
11040                (setq gnus-newsgroup-spam-marked
11041                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11042                                               article)))
11043               ((= mark gnus-dormant-mark)
11044                (setq gnus-newsgroup-dormant
11045                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
11046                                               article)))
11047               (t
11048                (setq gnus-newsgroup-unreads
11049                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
11050                                               article))))
11051         (gnus-alist-pull article gnus-newsgroup-reads)
11052
11053         ;; See whether the article is to be put in the cache.
11054         (and gnus-use-cache
11055              (vectorp (gnus-summary-article-header article))
11056              (save-excursion
11057                (gnus-cache-possibly-enter-article
11058                 gnus-newsgroup-name article
11059                 (= mark gnus-ticked-mark)
11060                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11061
11062         ;; Fix the mark.
11063         (gnus-summary-update-mark mark 'unread)
11064         t))))
11065
11066 (defun gnus-summary-mark-article (&optional article mark no-expire)
11067   "Mark ARTICLE with MARK.  MARK can be any character.
11068 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11069 `??' (dormant) and `?E' (expirable).
11070 If MARK is nil, then the default character `?r' is used.
11071 If ARTICLE is nil, then the article on the current line will be
11072 marked.
11073 If NO-EXPIRE, auto-expiry will be inhibited."
11074   ;; The mark might be a string.
11075   (when (stringp mark)
11076     (setq mark (aref mark 0)))
11077   ;; If no mark is given, then we check auto-expiring.
11078   (when (null mark)
11079     (setq mark gnus-del-mark))
11080   (when (and (not no-expire)
11081              gnus-newsgroup-auto-expire
11082              (memq mark gnus-auto-expirable-marks))
11083     (setq mark gnus-expirable-mark))
11084   (let ((article (or article (gnus-summary-article-number)))
11085         (old-mark (gnus-summary-article-mark article)))
11086     ;; Allow the backend to change the mark.
11087     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11088     (if (eq mark old-mark)
11089         t
11090       (unless article
11091         (error "No article on current line"))
11092       (if (not (if (or (= mark gnus-unread-mark)
11093                        (= mark gnus-ticked-mark)
11094                        (= mark gnus-spam-mark)
11095                        (= mark gnus-dormant-mark))
11096                    (gnus-mark-article-as-unread article mark)
11097                  (gnus-mark-article-as-read article mark)))
11098           t
11099         ;; See whether the article is to be put in the cache.
11100         (and gnus-use-cache
11101              (not (= mark gnus-canceled-mark))
11102              (vectorp (gnus-summary-article-header article))
11103              (save-excursion
11104                (gnus-cache-possibly-enter-article
11105                 gnus-newsgroup-name article
11106                 (= mark gnus-ticked-mark)
11107                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11108
11109         (when (gnus-summary-goto-subject article nil t)
11110           (let ((inhibit-read-only t))
11111             (gnus-summary-show-thread)
11112             ;; Fix the mark.
11113             (gnus-summary-update-mark mark 'unread)
11114             t))))))
11115
11116 (defun gnus-summary-update-secondary-mark (article)
11117   "Update the secondary (read, process, cache) mark."
11118   (gnus-summary-update-mark
11119    (cond ((memq article gnus-newsgroup-processable)
11120           gnus-process-mark)
11121          ((memq article gnus-newsgroup-cached)
11122           gnus-cached-mark)
11123          ((memq article gnus-newsgroup-replied)
11124           gnus-replied-mark)
11125          ((memq article gnus-newsgroup-forwarded)
11126           gnus-forwarded-mark)
11127          ((memq article gnus-newsgroup-saved)
11128           gnus-saved-mark)
11129          ((memq article gnus-newsgroup-unseen)
11130           gnus-unseen-mark)
11131          (t gnus-no-mark))
11132    'replied)
11133   (when (gnus-visual-p 'summary-highlight 'highlight)
11134     (gnus-summary-highlight-line)
11135     (gnus-run-hooks 'gnus-summary-update-hook))
11136   t)
11137
11138 (defun gnus-summary-update-download-mark (article)
11139   "Update the download mark."
11140   (gnus-summary-update-mark
11141    (cond ((memq article gnus-newsgroup-undownloaded)
11142           gnus-undownloaded-mark)
11143          (gnus-newsgroup-agentized
11144           gnus-downloaded-mark)
11145          (t
11146           gnus-no-mark))
11147    'download)
11148   (gnus-summary-update-line t)
11149   t)
11150
11151 (defun gnus-summary-update-mark (mark type)
11152   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11153         (inhibit-read-only t))
11154     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
11155     (when forward
11156       (when (looking-at "\r")
11157         (incf forward))
11158       (when (<= (+ forward (point)) (point-max))
11159         ;; Go to the right position on the line.
11160         (goto-char (+ forward (point)))
11161         ;; Replace the old mark with the new mark.
11162         (let ((to-insert
11163                (mm-subst-char-in-string
11164                 (char-after) mark
11165                 (buffer-substring (point) (1+ (point))))))
11166           (delete-region (point) (1+ (point)))
11167           (insert to-insert))
11168         ;; Optionally update the marks by some user rule.
11169         (when (eq type 'unread)
11170           (gnus-data-set-mark
11171            (gnus-data-find (gnus-summary-article-number)) mark)
11172           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
11173
11174 (defun gnus-mark-article-as-read (article &optional mark)
11175   "Enter ARTICLE in the pertinent lists and remove it from others."
11176   ;; Make the article expirable.
11177   (let ((mark (or mark gnus-del-mark)))
11178     (setq gnus-newsgroup-expirable
11179           (if (= mark gnus-expirable-mark)
11180               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
11181             (delq article gnus-newsgroup-expirable)))
11182     ;; Remove from unread and marked lists.
11183     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11184     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11185     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
11186     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11187     (push (cons article mark) gnus-newsgroup-reads)
11188     ;; Possibly remove from cache, if that is used.
11189     (when gnus-use-cache
11190       (gnus-cache-enter-remove-article article))
11191     t))
11192
11193 (defun gnus-mark-article-as-unread (article &optional mark)
11194   "Enter ARTICLE in the pertinent lists and remove it from others."
11195   (let ((mark (or mark gnus-ticked-mark)))
11196     (if (<= article 0)
11197         (progn
11198           (gnus-error 1 "Can't mark negative article numbers")
11199           nil)
11200       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
11201             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
11202             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
11203             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
11204             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11205
11206       ;; Unsuppress duplicates?
11207       (when gnus-suppress-duplicates
11208         (gnus-dup-unsuppress-article article))
11209
11210       (cond ((= mark gnus-ticked-mark)
11211              (setq gnus-newsgroup-marked
11212                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
11213             ((= mark gnus-spam-mark)
11214              (setq gnus-newsgroup-spam-marked
11215                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
11216                                             article)))
11217             ((= mark gnus-dormant-mark)
11218              (setq gnus-newsgroup-dormant
11219                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
11220             (t
11221              (setq gnus-newsgroup-unreads
11222                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
11223       (gnus-alist-pull article gnus-newsgroup-reads)
11224       t)))
11225
11226 (defun gnus-summary-tick-article-forward (n)
11227   "Tick N articles forwards.
11228 If N is negative, tick backwards instead.
11229 The difference between N and the number of articles ticked is returned."
11230   (interactive "p")
11231   (gnus-summary-mark-forward n gnus-ticked-mark))
11232
11233 (defun gnus-summary-tick-article-backward (n)
11234   "Tick N articles backwards.
11235 The difference between N and the number of articles ticked is returned."
11236   (interactive "p")
11237   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11238
11239 (defun gnus-summary-tick-article (&optional article clear-mark)
11240   "Mark current article as unread.
11241 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11242 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11243   (interactive)
11244   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11245                                        gnus-ticked-mark)))
11246
11247 (defun gnus-summary-mark-as-read-forward (n)
11248   "Mark N articles as read forwards.
11249 If N is negative, mark backwards instead.
11250 The difference between N and the actual number of articles marked is
11251 returned."
11252   (interactive "p")
11253   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
11254
11255 (defun gnus-summary-mark-as-read-backward (n)
11256   "Mark the N articles as read backwards.
11257 The difference between N and the actual number of articles marked is
11258 returned."
11259   (interactive "p")
11260   (gnus-summary-mark-forward
11261    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
11262
11263 (defun gnus-summary-mark-as-read (&optional article mark)
11264   "Mark current article as read.
11265 ARTICLE specifies the article to be marked as read.
11266 MARK specifies a string to be inserted at the beginning of the line."
11267   (gnus-summary-mark-article article mark))
11268
11269 (defun gnus-summary-clear-mark-forward (n)
11270   "Clear marks from N articles forward.
11271 If N is negative, clear backward instead.
11272 The difference between N and the number of marks cleared is returned."
11273   (interactive "p")
11274   (gnus-summary-mark-forward n gnus-unread-mark))
11275
11276 (defun gnus-summary-clear-mark-backward (n)
11277   "Clear marks from N articles backward.
11278 The difference between N and the number of marks cleared is returned."
11279   (interactive "p")
11280   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11281
11282 (defun gnus-summary-mark-unread-as-read ()
11283   "Intended to be used by `gnus-mark-article-hook'."
11284   (when (memq gnus-current-article gnus-newsgroup-unreads)
11285     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11286
11287 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
11288   "Intended to be used by `gnus-mark-article-hook'."
11289   (let ((mark (gnus-summary-article-mark)))
11290     (when (or (gnus-unread-mark-p mark)
11291               (gnus-read-mark-p mark))
11292       (gnus-summary-mark-article gnus-current-article
11293                                  (or new-mark gnus-read-mark)))))
11294
11295 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
11296   "Intended to be used by `gnus-mark-article-hook'."
11297   (let ((mark (gnus-summary-article-mark)))
11298     (when (or (gnus-unread-mark-p mark)
11299               (gnus-read-mark-p mark))
11300       (gnus-summary-mark-article (gnus-summary-article-number)
11301                                  (or new-mark gnus-read-mark)))))
11302
11303 (defun gnus-summary-mark-unread-as-ticked ()
11304   "Intended to be used by `gnus-mark-article-hook'."
11305   (when (memq gnus-current-article gnus-newsgroup-unreads)
11306     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
11307
11308 (defun gnus-summary-mark-region-as-read (point mark all)
11309   "Mark all unread articles between point and mark as read.
11310 If given a prefix, mark all articles between point and mark as read,
11311 even ticked and dormant ones."
11312   (interactive "r\nP")
11313   (save-excursion
11314     (let (article)
11315       (goto-char point)
11316       (beginning-of-line)
11317       (while (and
11318               (< (point) mark)
11319               (progn
11320                 (when (or all
11321                           (memq (setq article (gnus-summary-article-number))
11322                                 gnus-newsgroup-unreads))
11323                   (gnus-summary-mark-article article gnus-del-mark))
11324                 t)
11325               (gnus-summary-find-next))))))
11326
11327 (defun gnus-summary-mark-below (score mark)
11328   "Mark articles with score less than SCORE with MARK."
11329   (interactive "P\ncMark: ")
11330   (setq score (if score
11331                   (prefix-numeric-value score)
11332                 (or gnus-summary-default-score 0)))
11333   (with-current-buffer gnus-summary-buffer
11334     (goto-char (point-min))
11335     (while
11336         (progn
11337           (and (< (gnus-summary-article-score) score)
11338                (gnus-summary-mark-article nil mark))
11339           (gnus-summary-find-next)))))
11340
11341 (defun gnus-summary-kill-below (&optional score)
11342   "Mark articles with score below SCORE as read."
11343   (interactive "P")
11344   (gnus-summary-mark-below score gnus-killed-mark))
11345
11346 (defun gnus-summary-clear-above (&optional score)
11347   "Clear all marks from articles with score above SCORE."
11348   (interactive "P")
11349   (gnus-summary-mark-above score gnus-unread-mark))
11350
11351 (defun gnus-summary-tick-above (&optional score)
11352   "Tick all articles with score above SCORE."
11353   (interactive "P")
11354   (gnus-summary-mark-above score gnus-ticked-mark))
11355
11356 (defun gnus-summary-mark-above (score mark)
11357   "Mark articles with score over SCORE with MARK."
11358   (interactive "P\ncMark: ")
11359   (setq score (if score
11360                   (prefix-numeric-value score)
11361                 (or gnus-summary-default-score 0)))
11362   (with-current-buffer gnus-summary-buffer
11363     (goto-char (point-min))
11364     (while (and (progn
11365                   (when (> (gnus-summary-article-score) score)
11366                     (gnus-summary-mark-article nil mark))
11367                   t)
11368                 (gnus-summary-find-next)))))
11369
11370 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11371 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11372 (defun gnus-summary-limit-include-expunged (&optional no-error)
11373   "Display all the hidden articles that were expunged for low scores."
11374   (interactive)
11375   (let ((inhibit-read-only t))
11376     (let ((scored gnus-newsgroup-scored)
11377           headers h)
11378       (while scored
11379         (unless (gnus-summary-article-header (caar scored))
11380           (and (setq h (gnus-number-to-header (caar scored)))
11381                (< (cdar scored) gnus-summary-expunge-below)
11382                (push h headers)))
11383         (setq scored (cdr scored)))
11384       (if (not headers)
11385           (when (not no-error)
11386             (error "No expunged articles hidden"))
11387         (goto-char (point-min))
11388         (push gnus-newsgroup-limit gnus-newsgroup-limits)
11389         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
11390         (dolist (x headers)
11391           (push (mail-header-number x) gnus-newsgroup-limit))
11392         (gnus-summary-prepare-unthreaded (nreverse headers))
11393         (goto-char (point-min))
11394         (gnus-summary-position-point)
11395         t))))
11396
11397 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
11398   "Mark all unread articles in this newsgroup as read.
11399 If prefix argument ALL is non-nil, ticked and dormant articles will
11400 also be marked as read.
11401 If QUIETLY is non-nil, no questions will be asked.
11402
11403 If TO-HERE is non-nil, it should be a point in the buffer.  All
11404 articles before (after, if REVERSE is set) this point will be marked
11405 as read.
11406
11407 Note that this function will only catch up the unread article
11408 in the current summary buffer limitation.
11409
11410 The number of articles marked as read is returned."
11411   (interactive "P")
11412   (prog1
11413       (save-excursion
11414         (when (or quietly
11415                   (not gnus-interactive-catchup) ;Without confirmation?
11416                   gnus-expert-user
11417                   (gnus-y-or-n-p
11418                    (if all
11419                        "Mark absolutely all articles as read? "
11420                      "Mark all unread articles as read? ")))
11421           (if (and not-mark
11422                    (not gnus-newsgroup-adaptive)
11423                    (not gnus-newsgroup-auto-expire)
11424                    (not gnus-suppress-duplicates)
11425                    (or (not gnus-use-cache)
11426                        (eq gnus-use-cache 'passive)))
11427               (progn
11428                 (when all
11429                   (setq gnus-newsgroup-marked nil
11430                         gnus-newsgroup-spam-marked nil
11431                         gnus-newsgroup-dormant nil))
11432                 (setq gnus-newsgroup-unreads
11433                       (gnus-sorted-nunion
11434                        (gnus-sorted-intersection gnus-newsgroup-unreads
11435                                                  gnus-newsgroup-downloadable)
11436                        (gnus-sorted-difference gnus-newsgroup-unfetched
11437                                                gnus-newsgroup-cached))))
11438             ;; We actually mark all articles as canceled, which we
11439             ;; have to do when using auto-expiry or adaptive scoring.
11440             (gnus-summary-show-all-threads)
11441             (if (and to-here reverse)
11442                 (progn
11443                   (goto-char to-here)
11444                   (gnus-summary-mark-current-read-and-unread-as-read
11445                    gnus-catchup-mark)
11446                   (while (gnus-summary-find-next (not all))
11447                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
11448               (when (gnus-summary-first-subject (not all))
11449                 (while (and
11450                         (if to-here (< (point) to-here) t)
11451                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11452                         (gnus-summary-find-next (not all))))))
11453             (gnus-set-mode-line 'summary))
11454           t))
11455     (gnus-summary-position-point)))
11456
11457 (defun gnus-summary-catchup-to-here (&optional all)
11458   "Mark all unticked articles before the current one as read.
11459 If ALL is non-nil, also mark ticked and dormant articles as read."
11460   (interactive "P")
11461   (save-excursion
11462     (gnus-save-hidden-threads
11463       (let ((beg (point)))
11464         ;; We check that there are unread articles.
11465         (when (or all (gnus-summary-find-prev))
11466           (gnus-summary-catchup all t beg)))))
11467   (gnus-summary-position-point))
11468
11469 (defun gnus-summary-catchup-from-here (&optional all)
11470   "Mark all unticked articles after (and including) the current one as read.
11471 If ALL is non-nil, also mark ticked and dormant articles as read."
11472   (interactive "P")
11473   (save-excursion
11474     (gnus-save-hidden-threads
11475       (let ((beg (point)))
11476         ;; We check that there are unread articles.
11477         (when (or all (gnus-summary-find-next))
11478           (gnus-summary-catchup all t beg nil t)))))
11479   (gnus-summary-position-point))
11480
11481 (defun gnus-summary-catchup-all (&optional quietly)
11482   "Mark all articles in this newsgroup as read.
11483 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11484 instead, which marks only unread articles as read."
11485   (interactive "P")
11486   (gnus-summary-catchup t quietly))
11487
11488 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11489   "Mark all unread articles in this group as read, then exit.
11490 If prefix argument ALL is non-nil, all articles are marked as read.
11491 If QUIETLY is non-nil, no questions will be asked."
11492   (interactive "P")
11493   (when (gnus-summary-catchup all quietly nil 'fast)
11494     ;; Select next newsgroup or exit.
11495     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11496              (eq gnus-auto-select-next 'quietly))
11497         (gnus-summary-next-group nil)
11498       (gnus-summary-exit))))
11499
11500 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11501   "Mark all articles in this newsgroup as read, and then exit.
11502 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11503 instead, which marks only unread articles as read."
11504   (interactive "P")
11505   (gnus-summary-catchup-and-exit t quietly))
11506
11507 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11508   "Mark all articles in this group as read and select the next group.
11509 If given a prefix, mark all articles, unread as well as ticked, as
11510 read."
11511   (interactive "P")
11512   (save-excursion
11513     (gnus-summary-catchup all))
11514   (gnus-summary-next-group))
11515
11516 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11517   "Mark all articles in this group as read and select the previous group.
11518 If given a prefix, mark all articles, unread as well as ticked, as
11519 read."
11520   (interactive "P")
11521   (save-excursion
11522     (gnus-summary-catchup all))
11523   (gnus-summary-next-group nil nil t))
11524
11525 ;;;
11526 ;;; with article
11527 ;;;
11528
11529 (defmacro gnus-with-article (article &rest forms)
11530   "Select ARTICLE and perform FORMS in the original article buffer.
11531 Then replace the article with the result."
11532   `(progn
11533      ;; We don't want the article to be marked as read.
11534      (let (gnus-mark-article-hook)
11535        (gnus-summary-select-article t t nil ,article))
11536      (set-buffer gnus-original-article-buffer)
11537      ,@forms
11538      (if (not (gnus-check-backend-function
11539                'request-replace-article (car gnus-article-current)))
11540          (gnus-message 5 "Read-only group; not replacing")
11541        (unless (gnus-request-replace-article
11542                 ,article (car gnus-article-current)
11543                 (current-buffer) t)
11544          (error "Couldn't replace article")))
11545      ;; The cache and backlog have to be flushed somewhat.
11546      (when gnus-keep-backlog
11547        (gnus-backlog-remove-article
11548         (car gnus-article-current) (cdr gnus-article-current)))
11549      (when gnus-use-cache
11550        (gnus-cache-update-article
11551         (car gnus-article-current) (cdr gnus-article-current)))))
11552
11553 (put 'gnus-with-article 'lisp-indent-function 1)
11554 (put 'gnus-with-article 'edebug-form-spec '(form body))
11555
11556 ;; Thread-based commands.
11557
11558 (defun gnus-summary-articles-in-thread (&optional article)
11559   "Return a list of all articles in the current thread.
11560 If ARTICLE is non-nil, return all articles in the thread that starts
11561 with that article."
11562   (let* ((article (or article (gnus-summary-article-number)))
11563          (data (gnus-data-find-list article))
11564          (top-level (gnus-data-level (car data)))
11565          (top-subject
11566           (cond ((null gnus-thread-operation-ignore-subject)
11567                  (gnus-simplify-subject-re
11568                   (mail-header-subject (gnus-data-header (car data)))))
11569                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11570                  (gnus-simplify-subject-fuzzy
11571                   (mail-header-subject (gnus-data-header (car data)))))
11572                 (t nil)))
11573          (end-point (save-excursion
11574                       (goto-char (gnus-data-pos (car data)))
11575                       (if (gnus-summary-go-to-next-thread)
11576                           (point) (point-max))))
11577          articles)
11578     (while (and data
11579                 (< (gnus-data-pos (car data)) end-point))
11580       (when (or (not top-subject)
11581                 (string= top-subject
11582                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11583                              (gnus-simplify-subject-fuzzy
11584                               (mail-header-subject
11585                                (gnus-data-header (car data))))
11586                            (gnus-simplify-subject-re
11587                             (mail-header-subject
11588                              (gnus-data-header (car data)))))))
11589         (push (gnus-data-number (car data)) articles))
11590       (unless (and (setq data (cdr data))
11591                    (> (gnus-data-level (car data)) top-level))
11592         (setq data nil)))
11593     ;; Return the list of articles.
11594     (nreverse articles)))
11595
11596 (defun gnus-summary-rethread-current ()
11597   "Rethread the thread the current article is part of."
11598   (interactive)
11599   (let* ((gnus-show-threads t)
11600          (article (gnus-summary-article-number))
11601          (id (mail-header-id (gnus-summary-article-header)))
11602          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11603     (unless id
11604       (error "No article on the current line"))
11605     (gnus-rebuild-thread id)
11606     (gnus-summary-goto-subject article)))
11607
11608 (defun gnus-summary-reparent-thread ()
11609   "Make the current article child of the marked (or previous) article.
11610
11611 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11612 is non-nil or the Subject: of both articles are the same."
11613   (interactive)
11614   (unless (not (gnus-group-read-only-p))
11615     (error "The current newsgroup does not support article editing"))
11616   (unless (<= (length gnus-newsgroup-processable) 1)
11617     (error "No more than one article may be marked"))
11618   (let ((child (gnus-summary-article-number))
11619         ;; First grab the marked article, otherwise one line up.
11620         (parent (if (not (null gnus-newsgroup-processable))
11621                     (car gnus-newsgroup-processable)
11622                   (save-excursion
11623                     (if (eq (forward-line -1) 0)
11624                         (gnus-summary-article-number)
11625                       (error "Beginning of summary buffer"))))))
11626     (gnus-summary-reparent-children parent (list child))))
11627
11628 (defun gnus-summary-reparent-children (parent children)
11629   "Make PARENT the parent of CHILDREN.
11630 When called interactively, PARENT is the current article and CHILDREN
11631 are the process-marked articles."
11632   (interactive
11633    (list (gnus-summary-article-number)
11634          (gnus-summary-work-articles nil)))
11635   (dolist (child children)
11636     (save-window-excursion
11637       (let ((gnus-article-buffer " *reparent*"))
11638         (unless (not (eq parent child))
11639           (error "An article may not be self-referential"))
11640         (let ((message-id (mail-header-id
11641                            (gnus-summary-article-header parent))))
11642           (unless (and message-id (not (equal message-id "")))
11643             (error "No message-id in desired parent"))
11644           (gnus-with-article child
11645             (save-restriction
11646               (goto-char (point-min))
11647               (message-narrow-to-head)
11648               (if (re-search-forward "^References: " nil t)
11649                   (progn
11650                     (re-search-forward "^[^ \t]" nil t)
11651                     (forward-line -1)
11652                     (end-of-line)
11653                     (insert " " message-id))
11654                 (insert "References: " message-id "\n"))))
11655           (set-buffer gnus-summary-buffer)
11656           (gnus-summary-unmark-all-processable)
11657           (gnus-summary-update-article child)
11658           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11659             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11660           (gnus-summary-rethread-current)
11661           (gnus-message 3 "Article %d is now the child of article %d"
11662                         child parent))))))
11663
11664 (defun gnus-summary-toggle-threads (&optional arg)
11665   "Toggle showing conversation threads.
11666 If ARG is positive number, turn showing conversation threads on."
11667   (interactive "P")
11668   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11669     (setq gnus-show-threads
11670           (if (null arg) (not gnus-show-threads)
11671             (> (prefix-numeric-value arg) 0)))
11672     (gnus-summary-prepare)
11673     (gnus-summary-goto-subject current)
11674     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11675     (gnus-summary-position-point)))
11676
11677 (eval-and-compile
11678   (if (fboundp 'remove-overlays)
11679       (defalias 'gnus-remove-overlays 'remove-overlays)
11680     (defun gnus-remove-overlays (beg end name val)
11681       "Clear BEG and END of overlays whose property NAME has value VAL.
11682 For compatibility with XEmacs."
11683       (dolist (ov (gnus-overlays-in beg end))
11684         (when (eq (gnus-overlay-get ov name) val)
11685           (gnus-delete-overlay ov))))))
11686
11687 (defun gnus-summary-show-all-threads ()
11688   "Show all threads."
11689   (interactive)
11690   (gnus-remove-overlays (point-min) (point-max) 'invisible 'gnus-sum)
11691   (gnus-summary-position-point))
11692
11693 (defsubst gnus-summary--inv (p)
11694   (and (eq (get-char-property p 'invisible) 'gnus-sum) p))
11695
11696 (defun gnus-summary-show-thread ()
11697   "Show thread subtrees.
11698 Returns nil if no thread was there to be shown."
11699   (interactive)
11700   (let* ((orig (point))
11701          (end (point-at-eol))
11702          (end (or (gnus-summary--inv end) (gnus-summary--inv (1- end))))
11703          ;; Leave point at bol
11704          (beg (progn (beginning-of-line) (if (bobp) (point) (1- (point)))))
11705          (eoi (when end
11706                 (if (fboundp 'next-single-char-property-change)
11707                     ;; Note: XEmacs version of n-s-c-p-c may return nil
11708                     (or (next-single-char-property-change end 'invisible)
11709                         (point-max))
11710                   (while (progn
11711                            (end-of-line 2)
11712                            (and (not (eobp))
11713                                 (eq (get-char-property (point) 'invisible)
11714                                     'gnus-sum))))
11715                   (point)))))
11716     (when eoi
11717       (gnus-remove-overlays beg eoi 'invisible 'gnus-sum)
11718       (goto-char orig)
11719       (gnus-summary-position-point)
11720       eoi)))
11721
11722 (defun gnus-summary-maybe-hide-threads ()
11723   "If requested, hide the threads that should be hidden."
11724   (when (and gnus-show-threads
11725              gnus-thread-hide-subtree)
11726     (gnus-summary-hide-all-threads
11727      (if (or (consp gnus-thread-hide-subtree)
11728              (functionp gnus-thread-hide-subtree))
11729          (gnus-make-predicate gnus-thread-hide-subtree)
11730        nil))))
11731
11732 ;;; Hiding predicates.
11733
11734 (defun gnus-article-unread-p (header)
11735   (memq (mail-header-number header) gnus-newsgroup-unreads))
11736
11737 (defun gnus-article-unseen-p (header)
11738   (memq (mail-header-number header) gnus-newsgroup-unseen))
11739
11740 (defun gnus-map-articles (predicate articles)
11741   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11742   (apply 'gnus-or (mapcar predicate
11743                           (mapcar (lambda (number)
11744                                     (gnus-summary-article-header number))
11745                                   articles))))
11746
11747 (defun gnus-summary-hide-all-threads (&optional predicate)
11748   "Hide all thread subtrees.
11749 If PREDICATE is supplied, threads that satisfy this predicate
11750 will not be hidden."
11751   (interactive)
11752   (save-excursion
11753     (goto-char (point-min))
11754     (let ((end nil)
11755           (count 0))
11756       (while (not end)
11757         (incf count)
11758         (when (zerop (mod count 1000))
11759           (message "Hiding all threads... %d" count))
11760         (when (or (not predicate)
11761                   (gnus-map-articles
11762                    predicate (gnus-summary-article-children)))
11763             (gnus-summary-hide-thread))
11764         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11765   (gnus-summary-position-point))
11766
11767 (defun gnus-summary-hide-thread ()
11768   "Hide thread subtrees.
11769 If PREDICATE is supplied, threads that satisfy this predicate
11770 will not be hidden.
11771 Returns nil if no threads were there to be hidden."
11772   (interactive)
11773   (beginning-of-line)
11774   (let ((start (point))
11775         (starteol (line-end-position))
11776         (article (gnus-summary-article-number)))
11777     ;; Go forward until either the buffer ends or the subthread ends.
11778     (when (and (not (eobp))
11779                (or (zerop (gnus-summary-next-thread 1 t))
11780                    (goto-char (point-max))))
11781       (if (and (> (point) start)
11782                ;; FIXME: this should actually search for a non-invisible \n.
11783                (search-backward "\n" start t))
11784           (progn
11785             (when (> (point) starteol)
11786               (gnus-remove-overlays starteol (point) 'invisible 'gnus-sum)
11787               (let ((ol (gnus-make-overlay starteol (point) nil t nil)))
11788                 (gnus-overlay-put ol 'invisible 'gnus-sum)
11789                 (gnus-overlay-put ol 'evaporate t)))
11790             (gnus-summary-goto-subject article)
11791             (when (> start (point))
11792               (message "Hiding the thread moved us backwards, aborting!")
11793               (goto-char (point-max))))
11794         (goto-char start)
11795         nil))))
11796
11797 (defun gnus-summary-go-to-next-thread (&optional previous)
11798   "Go to the same level (or less) next thread.
11799 If PREVIOUS is non-nil, go to previous thread instead.
11800 Return the article number moved to, or nil if moving was impossible."
11801   (let ((level (gnus-summary-thread-level))
11802         (way (if previous -1 1))
11803         (beg (point)))
11804     (forward-line way)
11805     (while (and (not (eobp))
11806                 (< level (gnus-summary-thread-level)))
11807       (forward-line way))
11808     (if (eobp)
11809         (progn
11810           (goto-char beg)
11811           nil)
11812       (setq beg (point))
11813       (prog1
11814           (gnus-summary-article-number)
11815         (goto-char beg)))))
11816
11817 (defun gnus-summary-next-thread (n &optional silent)
11818   "Go to the same level next N'th thread.
11819 If N is negative, search backward instead.
11820 Returns the difference between N and the number of skips actually
11821 done.
11822
11823 If SILENT, don't output messages."
11824   (interactive "p")
11825   (let ((backward (< n 0))
11826         (n (abs n)))
11827     (while (and (> n 0)
11828                 (gnus-summary-go-to-next-thread backward))
11829       (decf n))
11830     (unless silent
11831       (gnus-summary-position-point))
11832     (when (and (not silent) (/= 0 n))
11833       (gnus-message 7 "No more threads"))
11834     n))
11835
11836 (defun gnus-summary-prev-thread (n)
11837   "Go to the same level previous N'th thread.
11838 Returns the difference between N and the number of skips actually
11839 done."
11840   (interactive "p")
11841   (gnus-summary-next-thread (- n)))
11842
11843 (defun gnus-summary-go-down-thread ()
11844   "Go down one level in the current thread."
11845   (let ((children (gnus-summary-article-children)))
11846     (when children
11847       (gnus-summary-goto-subject (car children)))))
11848
11849 (defun gnus-summary-go-up-thread ()
11850   "Go up one level in the current thread."
11851   (let ((parent (gnus-summary-article-parent)))
11852     (when parent
11853       (gnus-summary-goto-subject parent))))
11854
11855 (defun gnus-summary-down-thread (n)
11856   "Go down thread N steps.
11857 If N is negative, go up instead.
11858 Returns the difference between N and how many steps down that were
11859 taken."
11860   (interactive "p")
11861   (let ((up (< n 0))
11862         (n (abs n)))
11863     (while (and (> n 0)
11864                 (if up (gnus-summary-go-up-thread)
11865                   (gnus-summary-go-down-thread)))
11866       (setq n (1- n)))
11867     (gnus-summary-position-point)
11868     (when (/= 0 n)
11869       (gnus-message 7 "Can't go further"))
11870     n))
11871
11872 (defun gnus-summary-up-thread (n)
11873   "Go up thread N steps.
11874 If N is negative, go down instead.
11875 Returns the difference between N and how many steps down that were
11876 taken."
11877   (interactive "p")
11878   (gnus-summary-down-thread (- n)))
11879
11880 (defun gnus-summary-top-thread ()
11881   "Go to the top of the thread."
11882   (interactive)
11883   (while (gnus-summary-go-up-thread))
11884   (gnus-summary-article-number))
11885
11886 (defun gnus-summary-expire-thread ()
11887   "Mark articles under current thread as expired."
11888   (interactive)
11889   (gnus-summary-kill-thread 0))
11890
11891 (defun gnus-summary-kill-thread (&optional unmark)
11892   "Mark articles under current thread as read.
11893 If the prefix argument is positive, remove any kinds of marks.
11894 If the prefix argument is zero, mark thread as expired.
11895 If the prefix argument is negative, tick articles instead."
11896   (interactive "P")
11897   (when unmark
11898     (setq unmark (prefix-numeric-value unmark)))
11899   (let ((articles (gnus-summary-articles-in-thread))
11900         (hide (or (null unmark) (= unmark 0))))
11901     (save-excursion
11902       ;; Expand the thread.
11903       (gnus-summary-show-thread)
11904       ;; Mark all the articles.
11905       (while articles
11906         (gnus-summary-goto-subject (car articles))
11907         (cond ((null unmark)
11908                (gnus-summary-mark-article-as-read gnus-killed-mark))
11909               ((> unmark 0)
11910                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11911               ((= unmark 0)
11912                (gnus-summary-mark-article nil gnus-expirable-mark))
11913               (t
11914                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11915         (setq articles (cdr articles))))
11916     ;; Hide killed subtrees when hide is true.
11917     (and hide
11918          gnus-thread-hide-killed
11919          (gnus-summary-hide-thread))
11920     ;; If hide is t, go to next unread subject.
11921     (when hide
11922       ;; Go to next unread subject.
11923       (gnus-summary-next-subject 1 t)))
11924   (gnus-set-mode-line 'summary))
11925
11926 ;; Summary sorting commands
11927
11928 (defun gnus-summary-sort-by-number (&optional reverse)
11929   "Sort the summary buffer by article number.
11930 Argument REVERSE means reverse order."
11931   (interactive "P")
11932   (gnus-summary-sort 'number reverse))
11933
11934 (defun gnus-summary-sort-by-most-recent-number (&optional reverse)
11935   "Sort the summary buffer by most recent article number.
11936 Argument REVERSE means reverse order."
11937   (interactive "P")
11938   (gnus-summary-sort 'most-recent-number reverse))
11939
11940 (defun gnus-summary-sort-by-random (&optional reverse)
11941   "Randomize the order in the summary buffer.
11942 Argument REVERSE means to randomize in reverse order."
11943   (interactive "P")
11944   (gnus-summary-sort 'random reverse))
11945
11946 (defun gnus-summary-sort-by-author (&optional reverse)
11947   "Sort the summary buffer by author name alphabetically.
11948 If `case-fold-search' is non-nil, case of letters is ignored.
11949 Argument REVERSE means reverse order."
11950   (interactive "P")
11951   (gnus-summary-sort 'author reverse))
11952
11953 (defun gnus-summary-sort-by-recipient (&optional reverse)
11954   "Sort the summary buffer by recipient name alphabetically.
11955 If `case-fold-search' is non-nil, case of letters is ignored.
11956 Argument REVERSE means reverse order."
11957   (interactive "P")
11958   (gnus-summary-sort 'recipient reverse))
11959
11960 (defun gnus-summary-sort-by-subject (&optional reverse)
11961   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11962 If `case-fold-search' is non-nil, case of letters is ignored.
11963 Argument REVERSE means reverse order."
11964   (interactive "P")
11965   (gnus-summary-sort 'subject reverse))
11966
11967 (defun gnus-summary-sort-by-date (&optional reverse)
11968   "Sort the summary buffer by date.
11969 Argument REVERSE means reverse order."
11970   (interactive "P")
11971   (gnus-summary-sort 'date reverse))
11972
11973 (defun gnus-summary-sort-by-most-recent-date (&optional reverse)
11974   "Sort the summary buffer by most recent date.
11975 Argument REVERSE means reverse order."
11976   (interactive "P")
11977   (gnus-summary-sort 'most-recent-date reverse))
11978
11979 (defun gnus-summary-sort-by-score (&optional reverse)
11980   "Sort the summary buffer by score.
11981 Argument REVERSE means reverse order."
11982   (interactive "P")
11983   (gnus-summary-sort 'score reverse))
11984
11985 (defun gnus-summary-sort-by-lines (&optional reverse)
11986   "Sort the summary buffer by the number of lines.
11987 Argument REVERSE means reverse order."
11988   (interactive "P")
11989   (gnus-summary-sort 'lines reverse))
11990
11991 (defun gnus-summary-sort-by-chars (&optional reverse)
11992   "Sort the summary buffer by article length.
11993 Argument REVERSE means reverse order."
11994   (interactive "P")
11995   (gnus-summary-sort 'chars reverse))
11996
11997 (defun gnus-summary-sort-by-original (&optional reverse)
11998   "Sort the summary buffer using the default sorting method.
11999 Argument REVERSE means reverse order."
12000   (interactive "P")
12001   (let* ((inhibit-read-only t)
12002          (gnus-summary-prepare-hook nil))
12003     ;; We do the sorting by regenerating the threads.
12004     (gnus-summary-prepare)
12005     ;; Hide subthreads if needed.
12006     (gnus-summary-maybe-hide-threads)))
12007
12008 (defun gnus-summary-sort (predicate reverse)
12009   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12010   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12011          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12012          (gnus-thread-sort-functions
12013           (if (not reverse)
12014               thread
12015             `(lambda (t1 t2)
12016                (,thread t2 t1))))
12017          (gnus-sort-gathered-threads-function
12018           gnus-thread-sort-functions)
12019          (gnus-article-sort-functions
12020           (if (not reverse)
12021               article
12022             `(lambda (t1 t2)
12023                (,article t2 t1))))
12024          (inhibit-read-only t)
12025          (gnus-summary-prepare-hook nil))
12026     ;; We do the sorting by regenerating the threads.
12027     (gnus-summary-prepare)
12028     ;; Hide subthreads if needed.
12029     (gnus-summary-maybe-hide-threads)))
12030
12031 ;; Summary saving commands.
12032
12033 (defun gnus-summary-save-article (&optional n not-saved)
12034   "Save the current article using the default saver function.
12035 If N is a positive number, save the N next articles.
12036 If N is a negative number, save the N previous articles.
12037 If N is nil and any articles have been marked with the process mark,
12038 save those articles instead.
12039 The variable `gnus-default-article-saver' specifies the saver function.
12040
12041 If the optional second argument NOT-SAVED is non-nil, articles saved
12042 will not be marked as saved."
12043   (interactive "P")
12044   (require 'gnus-art)
12045   (let* ((articles (gnus-summary-work-articles n))
12046          (save-buffer (save-excursion
12047                         (nnheader-set-temp-buffer " *Gnus Save*")))
12048          (num (length articles))
12049          ;; Whether to save decoded articles or raw articles.
12050          (decode (when gnus-article-save-coding-system
12051                    (get gnus-default-article-saver :decode)))
12052          ;; When saving many articles in a single file, use the other
12053          ;; function to save articles other than the first one.
12054          (saver2 (get gnus-default-article-saver :function))
12055          (gnus-prompt-before-saving (if saver2
12056                                         t
12057                                       gnus-prompt-before-saving))
12058          (gnus-default-article-saver gnus-default-article-saver)
12059          header file)
12060     (dolist (article articles)
12061       (setq header (gnus-summary-article-header article))
12062       (if (not (vectorp header))
12063           ;; This is a pseudo-article.
12064           (if (assq 'name header)
12065               (gnus-copy-file (cdr (assq 'name header)))
12066             (gnus-message 1 "Article %d is unsavable" article))
12067         ;; This is a real article.
12068         (save-window-excursion
12069           (gnus-summary-select-article decode decode nil article)
12070           (gnus-summary-goto-subject article))
12071         (with-current-buffer save-buffer
12072           (erase-buffer)
12073           (insert-buffer-substring (if decode
12074                                        gnus-article-buffer
12075                                      gnus-original-article-buffer)))
12076         (setq file (gnus-article-save save-buffer file num))
12077         (gnus-summary-remove-process-mark article)
12078         (unless not-saved
12079           (gnus-summary-set-saved-mark article)))
12080       (when saver2
12081         (setq gnus-default-article-saver saver2
12082               saver2 nil)))
12083     (gnus-kill-buffer save-buffer)
12084     (gnus-summary-position-point)
12085     (gnus-set-mode-line 'summary)
12086     n))
12087
12088 (declare-function gnus-summary-save-in-pipe "gnus-art" (&optional command raw))
12089
12090 (defun gnus-summary-pipe-output (&optional n sym)
12091   "Pipe the current article to a subprocess.
12092 If N is a positive number, pipe the N next articles.
12093 If N is a negative number, pipe the N previous articles.
12094 If N is nil and any articles have been marked with the process mark,
12095 pipe those articles instead.
12096 The default command to which articles are piped is specified by the
12097 variable `gnus-summary-pipe-output-default-command'; if it is nil, you
12098 will be prompted for the command.
12099
12100 The properties `:decode' and `:headers' that are put to the function
12101 symbol `gnus-summary-save-in-pipe' control whether this function
12102 decodes articles and what headers to keep (see the doc string for the
12103 `gnus-default-article-saver' variable).  If SYM (the symbolic prefix)
12104 is neither omitted nor the symbol `r', force including all headers
12105 regardless of the `:headers' property.  If it is the symbol `r',
12106 articles that are not decoded and include all headers will be piped
12107 no matter what the properties `:decode' and `:headers' are."
12108   (interactive (gnus-interactive "P\ny"))
12109   (require 'gnus-art)
12110   (let* ((articles (gnus-summary-work-articles n))
12111          (result-buffer "*Shell Command Output*")
12112          (all-headers (not (memq sym '(nil r))))
12113          (gnus-save-all-headers (or all-headers gnus-save-all-headers))
12114          (raw (eq sym 'r))
12115          (headers (get 'gnus-summary-save-in-pipe :headers))
12116          command result)
12117     (unless (numberp (car articles))
12118       (error "No article to pipe"))
12119     (setq command (gnus-read-shell-command
12120                    (concat "Shell command on "
12121                            (if (cdr articles)
12122                                (format "these %d articles" (length articles))
12123                              "this article")
12124                            ": ")
12125                    gnus-summary-pipe-output-default-command))
12126     (when (string-equal command "")
12127       (error "A command is required"))
12128     (when all-headers
12129       (put 'gnus-summary-save-in-pipe :headers nil))
12130     (unwind-protect
12131         (while articles
12132           (gnus-summary-goto-subject (pop articles))
12133           (save-window-excursion (gnus-summary-save-in-pipe command raw))
12134           (when (and (get-buffer result-buffer)
12135                      (not (zerop (buffer-size (get-buffer result-buffer)))))
12136             (setq result (concat result (with-current-buffer result-buffer
12137                                           (buffer-string))))))
12138       (put 'gnus-summary-save-in-pipe :headers headers))
12139     (unless (zerop (length result))
12140       (if (with-current-buffer (get-buffer-create result-buffer)
12141             (erase-buffer)
12142             (insert result)
12143             (prog1
12144                 (and (= (count-lines (point-min) (point)) 1)
12145                      (progn
12146                        (end-of-line 0)
12147                        (<= (current-column)
12148                            (window-width (minibuffer-window)))))
12149               (goto-char (point-min))))
12150           (message "%s" (substring result 0 -1))
12151         (message nil)
12152         (gnus-configure-windows 'pipe)))))
12153
12154 (defun gnus-summary-save-article-mail (&optional arg)
12155   "Append the current article to a Unix mail box file.
12156 If N is a positive number, save the N next articles.
12157 If N is a negative number, save the N previous articles.
12158 If N is nil and any articles have been marked with the process mark,
12159 save those articles instead."
12160   (interactive "P")
12161   (require 'gnus-art)
12162   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12163     (gnus-summary-save-article arg)))
12164
12165 (defun gnus-summary-save-article-rmail (&optional arg)
12166   "Append the current article to an rmail file.
12167 If N is a positive number, save the N next articles.
12168 If N is a negative number, save the N previous articles.
12169 If N is nil and any articles have been marked with the process mark,
12170 save those articles instead."
12171   (interactive "P")
12172   (require 'gnus-art)
12173   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12174     (gnus-summary-save-article arg)))
12175
12176 (defun gnus-summary-save-article-file (&optional arg)
12177   "Append the current article to a file.
12178 If N is a positive number, save the N next articles.
12179 If N is a negative number, save the N previous articles.
12180 If N is nil and any articles have been marked with the process mark,
12181 save those articles instead."
12182   (interactive "P")
12183   (require 'gnus-art)
12184   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12185     (gnus-summary-save-article arg)))
12186
12187 (defun gnus-summary-write-article-file (&optional arg)
12188   "Write the current article to a file, deleting the previous file.
12189 If N is a positive number, save the N next articles.
12190 If N is a negative number, save the N previous articles.
12191 If N is nil and any articles have been marked with the process mark,
12192 save those articles instead."
12193   (interactive "P")
12194   (require 'gnus-art)
12195   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
12196     (gnus-summary-save-article arg)))
12197
12198 (defun gnus-summary-save-article-body-file (&optional arg)
12199   "Append the current article body to a file.
12200 If N is a positive number, save the N next articles.
12201 If N is a negative number, save the N previous articles.
12202 If N is nil and any articles have been marked with the process mark,
12203 save those articles instead."
12204   (interactive "P")
12205   (require 'gnus-art)
12206   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12207     (gnus-summary-save-article arg)))
12208
12209 (defun gnus-summary-write-article-body-file (&optional arg)
12210   "Write the current article body to a file, deleting the previous file.
12211 If N is a positive number, save the N next articles.
12212 If N is a negative number, save the N previous articles.
12213 If N is nil and any articles have been marked with the process mark,
12214 save those articles instead."
12215   (interactive "P")
12216   (require 'gnus-art)
12217   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
12218     (gnus-summary-save-article arg)))
12219
12220 (defun gnus-summary-muttprint (&optional arg)
12221   "Print the current article using Muttprint.
12222 If N is a positive number, save the N next articles.
12223 If N is a negative number, save the N previous articles.
12224 If N is nil and any articles have been marked with the process mark,
12225 save those articles instead."
12226   (interactive "P")
12227   (require 'gnus-art)
12228   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
12229     (gnus-summary-save-article arg t)))
12230
12231 (defun gnus-summary-pipe-message (program)
12232   "Pipe the current article through PROGRAM."
12233   (interactive "sProgram: ")
12234   (gnus-summary-select-article)
12235   (let ((mail-header-separator ""))
12236     (gnus-eval-in-buffer-window gnus-article-buffer
12237       (save-restriction
12238         (widen)
12239         (let ((start (window-start))
12240               (inhibit-read-only t))
12241           (message-pipe-buffer-body program)
12242           (set-window-start (get-buffer-window (current-buffer)) start))))))
12243
12244 (defun gnus-get-split-value (methods)
12245   "Return a value based on the split METHODS."
12246   (let (split-name method result match)
12247     (when methods
12248       (with-current-buffer gnus-original-article-buffer
12249         (save-restriction
12250           (nnheader-narrow-to-headers)
12251           (while (and methods (not split-name))
12252             (goto-char (point-min))
12253             (setq method (pop methods))
12254             (setq match (car method))
12255             (when (cond
12256                    ((stringp match)
12257                     ;; Regular expression.
12258                     (ignore-errors
12259                       (re-search-forward match nil t)))
12260                    ((functionp match)
12261                     ;; Function.
12262                     (save-restriction
12263                       (widen)
12264                       (setq result (funcall match gnus-newsgroup-name))))
12265                    ((consp match)
12266                     ;; Form.
12267                     (save-restriction
12268                       (widen)
12269                       (setq result (eval match)))))
12270               (setq split-name (cdr method))
12271               (cond ((stringp result)
12272                      (push (expand-file-name
12273                             result gnus-article-save-directory)
12274                            split-name))
12275                     ((consp result)
12276                      (setq split-name (append result split-name)))))))))
12277     (nreverse split-name)))
12278
12279 (defun gnus-valid-move-group-p (group)
12280   (and (symbolp group)
12281        (boundp group)
12282        (symbol-name group)
12283        (symbol-value group)
12284        (gnus-get-function (gnus-find-method-for-group
12285                            (symbol-name group)) 'request-accept-article t)))
12286
12287 (defun gnus-read-move-group-name (prompt default articles prefix)
12288   "Read a group name."
12289   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12290          (minibuffer-confirm-incomplete nil) ; XEmacs
12291          (prom
12292           (format "%s %s to"
12293                   prompt
12294                   (if (> (length articles) 1)
12295                       (format "these %d articles" (length articles))
12296                     "this article")))
12297          (to-newsgroup
12298           (cond
12299            ((null split-name)
12300             (gnus-group-completing-read
12301              prom
12302              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12303              nil prefix nil default))
12304            ((= 1 (length split-name))
12305             (gnus-group-completing-read
12306              prom
12307              (gnus-remove-if-not 'gnus-valid-move-group-p gnus-active-hashtb t)
12308              nil prefix 'gnus-group-history (car split-name)))
12309            (t
12310             (gnus-completing-read
12311              prom (nreverse split-name) nil nil 'gnus-group-history))))
12312          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
12313          encoded)
12314     (when to-newsgroup
12315       (if (or (string= to-newsgroup "")
12316               (string= to-newsgroup prefix))
12317           (setq to-newsgroup default))
12318       (unless to-newsgroup
12319         (error "No group name entered"))
12320       (setq encoded (mm-encode-coding-string
12321                      to-newsgroup
12322                      (gnus-group-name-charset to-method to-newsgroup)))
12323       (or (gnus-active encoded)
12324           (gnus-activate-group encoded nil nil to-method)
12325           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
12326                                      to-newsgroup))
12327               (or (and (gnus-request-create-group encoded to-method)
12328                        (gnus-activate-group encoded nil nil to-method)
12329                        (gnus-subscribe-group encoded))
12330                   (error "Couldn't create group %s" to-newsgroup)))
12331           (error "No such group: %s" to-newsgroup))
12332       encoded)))
12333
12334 (defvar gnus-summary-save-parts-counter)
12335 (declare-function mm-uu-dissect "mm-uu" (&optional noheader mime-type))
12336
12337 (defun gnus-summary-save-parts (type dir n &optional reverse)
12338   "Save parts matching TYPE to DIR.
12339 If REVERSE, save parts that do not match TYPE."
12340   (interactive
12341    (list (read-string "Save parts of type: "
12342                       (or (car gnus-summary-save-parts-type-history)
12343                           gnus-summary-save-parts-default-mime)
12344                       'gnus-summary-save-parts-type-history)
12345          (setq gnus-summary-save-parts-last-directory
12346                (read-directory-name "Save to directory: "
12347                                     gnus-summary-save-parts-last-directory
12348                                     nil t))
12349          current-prefix-arg))
12350   (gnus-summary-iterate n
12351     (let ((gnus-display-mime-function nil)
12352           gnus-article-prepare-hook
12353           gnus-article-decode-hook
12354           gnus-display-mime-function
12355           gnus-break-pages
12356           (gnus-inhibit-treatment t))
12357       (gnus-summary-select-article))
12358     (with-current-buffer gnus-article-buffer
12359       (let ((handles (or gnus-article-mime-handles
12360                          (mm-dissect-buffer nil gnus-article-loose-mime)
12361                          (and gnus-article-emulate-mime
12362                               (mm-uu-dissect))))
12363             (gnus-summary-save-parts-counter 1))
12364         (when handles
12365           (gnus-summary-save-parts-1 type dir handles reverse)
12366           (unless gnus-article-mime-handles ;; Don't destroy this case.
12367             (mm-destroy-parts handles)))))))
12368
12369 (defun gnus-summary-save-parts-1 (type dir handle reverse)
12370   (if (stringp (car handle))
12371       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
12372               (cdr handle))
12373     (when (if reverse
12374               (not (string-match type (mm-handle-media-type handle)))
12375             (string-match type (mm-handle-media-type handle)))
12376       (let ((file (expand-file-name
12377                    (gnus-map-function
12378                     mm-file-name-rewrite-functions
12379                     (file-name-nondirectory
12380                      (or
12381                       (mm-handle-filename handle)
12382                       (format "%s.%d.%d" gnus-newsgroup-name
12383                               (cdr gnus-article-current)
12384                               gnus-summary-save-parts-counter))))
12385                    dir)))
12386         (incf gnus-summary-save-parts-counter)
12387         (unless (file-exists-p file)
12388           (mm-save-part-to-file handle file))))))
12389
12390 ;; Summary extract commands
12391
12392 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12393   (let ((inhibit-read-only t)
12394         (article (gnus-summary-article-number))
12395         after-article b e)
12396     (unless (gnus-summary-goto-subject article)
12397       (error "No such article: %d" article))
12398     (gnus-summary-position-point)
12399     ;; If all commands are to be bunched up on one line, we collect
12400     ;; them here.
12401     (unless gnus-view-pseudos-separately
12402       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12403             files action)
12404         (while ps
12405           (setq action (cdr (assq 'action (car ps))))
12406           (setq files (list (cdr (assq 'name (car ps)))))
12407           (while (and ps (cdr ps)
12408                       (string= (or action "1")
12409                                (or (cdr (assq 'action (cadr ps))) "2")))
12410             (push (cdr (assq 'name (cadr ps))) files)
12411             (setcdr ps (cddr ps)))
12412           (when files
12413             (when (not (string-match "%s" action))
12414               (push " " files))
12415             (push " " files)
12416             (when (assq 'execute (car ps))
12417               (setcdr (assq 'execute (car ps))
12418                       (funcall (if (string-match "%s" action)
12419                                    'format 'concat)
12420                                action
12421                                (mapconcat
12422                                 (lambda (f)
12423                                   (if (equal f " ")
12424                                       f
12425                                     (shell-quote-argument f)))
12426                                 files " ")))))
12427           (setq ps (cdr ps)))))
12428     (if (and gnus-view-pseudos (not not-view))
12429         (while pslist
12430           (when (assq 'execute (car pslist))
12431             (gnus-execute-command (cdr (assq 'execute (car pslist)))
12432                                   (eq gnus-view-pseudos 'not-confirm)))
12433           (setq pslist (cdr pslist)))
12434       (save-excursion
12435         (while pslist
12436           (setq after-article (or (cdr (assq 'article (car pslist)))
12437                                   (gnus-summary-article-number)))
12438           (gnus-summary-goto-subject after-article)
12439           (forward-line 1)
12440           (setq b (point))
12441           (insert "    " (file-name-nondirectory
12442                           (cdr (assq 'name (car pslist))))
12443                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12444           (setq e (point))
12445           (forward-line -1)             ; back to `b'
12446           (gnus-add-text-properties
12447            b (1- e) (list 'gnus-number gnus-reffed-article-number
12448                           gnus-mouse-face-prop gnus-mouse-face))
12449           (gnus-data-enter
12450            after-article gnus-reffed-article-number
12451            gnus-unread-mark b (car pslist) 0 (- e b))
12452           (setq gnus-newsgroup-unreads
12453                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
12454                                          gnus-reffed-article-number))
12455           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12456           (setq pslist (cdr pslist)))))))
12457
12458 (defun gnus-pseudos< (p1 p2)
12459   (let ((c1 (cdr (assq 'action p1)))
12460         (c2 (cdr (assq 'action p2))))
12461     (and c1 c2 (string< c1 c2))))
12462
12463 (defun gnus-request-pseudo-article (props)
12464   (cond ((assq 'execute props)
12465          (gnus-execute-command (cdr (assq 'execute props)))))
12466   (let ((gnus-current-article (gnus-summary-article-number)))
12467     (gnus-run-hooks 'gnus-mark-article-hook)))
12468
12469 (defun gnus-execute-command (command &optional automatic)
12470   (save-excursion
12471     (gnus-article-setup-buffer)
12472     (set-buffer gnus-article-buffer)
12473     (setq buffer-read-only nil)
12474     (let ((command (if automatic command
12475                      (read-string "Command: " (cons command 0)))))
12476       (erase-buffer)
12477       (insert "$ " command "\n\n")
12478       (if gnus-view-pseudo-asynchronously
12479           (start-process "gnus-execute" (current-buffer) shell-file-name
12480                          shell-command-switch command)
12481         (call-process shell-file-name nil t nil
12482                       shell-command-switch command)))))
12483
12484 ;; Summary kill commands.
12485
12486 (defun gnus-summary-edit-global-kill (article)
12487   "Edit the \"global\" kill file."
12488   (interactive (list (gnus-summary-article-number)))
12489   (gnus-group-edit-global-kill article))
12490
12491 (defun gnus-summary-edit-local-kill ()
12492   "Edit a local kill file applied to the current newsgroup."
12493   (interactive)
12494   (setq gnus-current-headers (gnus-summary-article-header))
12495   (gnus-group-edit-local-kill
12496    (gnus-summary-article-number) gnus-newsgroup-name))
12497
12498 ;;; Header reading.
12499
12500 (defun gnus-read-header (id &optional header)
12501   "Read the headers of article ID and enter them into the Gnus system."
12502   (let ((group gnus-newsgroup-name)
12503         (gnus-override-method
12504          (or
12505           gnus-override-method
12506           (and (gnus-news-group-p gnus-newsgroup-name)
12507                (car (gnus-refer-article-methods)))))
12508         where)
12509     ;; First we check to see whether the header in question is already
12510     ;; fetched.
12511     (if (stringp id)
12512         ;; This is a Message-ID.
12513         (setq header (or header (gnus-id-to-header id)))
12514       ;; This is an article number.
12515       (setq header (or header (gnus-summary-article-header id))))
12516     (if (and header
12517              (not (gnus-summary-article-sparse-p (mail-header-number header))))
12518         ;; We have found the header.
12519         header
12520       ;; We have to really fetch the header to this article.
12521       (with-current-buffer nntp-server-buffer
12522         (when (setq where (gnus-request-head id group))
12523           (nnheader-fold-continuation-lines)
12524           (goto-char (point-max))
12525           (insert ".\n")
12526           (goto-char (point-min))
12527           (insert "211 ")
12528           (princ (cond
12529                   ((numberp id) id)
12530                   ((cdr where) (cdr where))
12531                   (header (mail-header-number header))
12532                   (t gnus-reffed-article-number))
12533                  (current-buffer))
12534           (insert " Article retrieved.\n"))
12535         (if (or (not where)
12536                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12537             ()                          ; Malformed head.
12538           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12539             (when (and (bound-and-true-p gnus-registry-enabled)
12540                        (not (gnus-ephemeral-group-p (car where))))
12541               (gnus-registry-handle-action
12542                (mail-header-id header) nil
12543                (gnus-group-prefixed-name
12544                 (car where)
12545                 (or gnus-override-method (gnus-find-method-for-group group)))
12546                (mail-header-subject header)
12547                (mail-header-from header)))
12548             (when (and (stringp id)
12549                        (or
12550                         (not (string= (gnus-group-real-name group)
12551                                       (car where)))
12552                         (not (gnus-server-equal gnus-override-method
12553                                                 (gnus-group-method group)))))
12554               ;; If we fetched by Message-ID and the article came from
12555               ;; a different group (or server), we fudge some bogus
12556               ;; article numbers for this article.
12557               (mail-header-set-number header gnus-reffed-article-number))
12558             (with-current-buffer gnus-summary-buffer
12559               (decf gnus-reffed-article-number)
12560               (gnus-remove-header (mail-header-number header))
12561               (push header gnus-newsgroup-headers)
12562               (setq gnus-current-headers header)
12563               (push (mail-header-number header) gnus-newsgroup-limit)))
12564           header)))))
12565
12566 (defun gnus-remove-header (number)
12567   "Remove header NUMBER from `gnus-newsgroup-headers'."
12568   (if (and gnus-newsgroup-headers
12569            (= number (mail-header-number (car gnus-newsgroup-headers))))
12570       (pop gnus-newsgroup-headers)
12571     (let ((headers gnus-newsgroup-headers))
12572       (while (and (cdr headers)
12573                   (not (= number (mail-header-number (cadr headers)))))
12574         (pop headers))
12575       (when (cdr headers)
12576         (setcdr headers (cddr headers))))))
12577
12578 ;;;
12579 ;;; summary highlights
12580 ;;;
12581
12582 (defun gnus-highlight-selected-summary ()
12583   "Highlight selected article in summary buffer."
12584   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12585   (when gnus-summary-selected-face
12586     (save-excursion
12587       (let* ((beg (point-at-bol))
12588              (end (point-at-eol))
12589              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12590              (from (if (get-text-property beg gnus-mouse-face-prop)
12591                        beg
12592                      (or (next-single-property-change
12593                           beg gnus-mouse-face-prop nil end)
12594                          beg)))
12595              (to
12596               (if (= from end)
12597                   (- from 2)
12598                 (or (next-single-property-change
12599                      from gnus-mouse-face-prop nil end)
12600                     end))))
12601         ;; If no mouse-face prop on line we will have to = from = end,
12602         ;; so we highlight the entire line instead.
12603         (when (= (+ to 2) from)
12604           (setq from beg)
12605           (setq to end))
12606         (if gnus-newsgroup-selected-overlay
12607             ;; Move old overlay.
12608             (gnus-move-overlay
12609              gnus-newsgroup-selected-overlay from to (current-buffer))
12610           ;; Create new overlay.
12611           (gnus-overlay-put
12612            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12613            'face gnus-summary-selected-face))))))
12614
12615 (defvar gnus-summary-highlight-line-cached nil)
12616 (defvar gnus-summary-highlight-line-trigger nil)
12617
12618 (defun gnus-summary-highlight-line-0 ()
12619   (if (and (eq gnus-summary-highlight-line-trigger
12620                gnus-summary-highlight)
12621            gnus-summary-highlight-line-cached)
12622       gnus-summary-highlight-line-cached
12623     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12624           gnus-summary-highlight-line-cached
12625           (let* ((cond (list 'cond))
12626                  (c cond)
12627                  (list gnus-summary-highlight))
12628             (while list
12629               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12630                               nil))
12631               (setq c (cdr c)
12632                     list (cdr list)))
12633             (gnus-byte-compile (list 'lambda nil cond))))))
12634
12635 (defun gnus-summary-highlight-line ()
12636   "Highlight current line according to `gnus-summary-highlight'."
12637   (let* ((beg (point-at-bol))
12638          (article (or (gnus-summary-article-number) gnus-current-article))
12639          (score (or (cdr (assq article
12640                                gnus-newsgroup-scored))
12641                     gnus-summary-default-score 0))
12642          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12643          (inhibit-read-only t)
12644          (default gnus-summary-default-score)
12645          (default-high gnus-summary-default-high-score)
12646          (default-low gnus-summary-default-low-score)
12647          (uncached (and gnus-summary-use-undownloaded-faces
12648                         (memq article gnus-newsgroup-undownloaded)
12649                         (not (memq article gnus-newsgroup-cached)))))
12650     (let ((face (funcall (gnus-summary-highlight-line-0))))
12651       (unless (eq face (gnus-get-text-property-excluding-characters-with-faces beg 'face))
12652         (gnus-put-text-property-excluding-characters-with-faces
12653          beg (point-at-eol) 'face
12654          (setq face (if (boundp face) (symbol-value face) face)))
12655         (when gnus-summary-highlight-line-function
12656           (funcall gnus-summary-highlight-line-function article face))))))
12657
12658 (defun gnus-update-read-articles (group unread &optional compute)
12659   "Update the list of read articles in GROUP.
12660 UNREAD is a sorted list."
12661   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12662         (info (gnus-get-info group))
12663         (prev 1)
12664         read)
12665     (if (or (not info) (not active))
12666         ;; There is no info on this group if it was, in fact,
12667         ;; killed.  Gnus stores no information on killed groups, so
12668         ;; there's nothing to be done.
12669         ;; One could store the information somewhere temporarily,
12670         ;; perhaps...  Hmmm...
12671         ()
12672       ;; Remove any negative articles numbers.
12673       (while (and unread (< (car unread) 0))
12674         (setq unread (cdr unread)))
12675       ;; Remove any expired article numbers
12676       (while (and unread (< (car unread) (car active)))
12677         (setq unread (cdr unread)))
12678       ;; Compute the ranges of read articles by looking at the list of
12679       ;; unread articles.
12680       (while unread
12681         (when (/= (car unread) prev)
12682           (push (if (= prev (1- (car unread))) prev
12683                   (cons prev (1- (car unread))))
12684                 read))
12685         (setq prev (1+ (car unread)))
12686         (setq unread (cdr unread)))
12687       (when (<= prev (cdr active))
12688         (push (cons prev (cdr active)) read))
12689       (setq read (if (> (length read) 1) (nreverse read) read))
12690       (if compute
12691           read
12692         (save-excursion
12693           (let (setmarkundo)
12694             ;; Propagate the read marks to the backend.
12695             (when (and (gnus-method-option-p
12696                         (gnus-find-method-for-group group)
12697                         'server-marks)
12698                        (gnus-check-backend-function 'request-set-mark group))
12699               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12700                     (add (gnus-remove-from-range read (gnus-info-read info))))
12701                 (when (or add del)
12702                   (unless (gnus-check-group group)
12703                     (error "Can't open server for %s" group))
12704                   (gnus-request-set-mark
12705                    group (delq nil (list (if add (list add 'add '(read)))
12706                                          (if del (list del 'del '(read))))))
12707                   (setq setmarkundo
12708                         `(gnus-request-set-mark
12709                           ,group
12710                           ',(delq nil (list
12711                                        (if del (list del 'add '(read)))
12712                                        (if add (list add 'del '(read))))))))))
12713             (set-buffer gnus-group-buffer)
12714             (gnus-undo-register
12715               `(progn
12716                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12717                  (gnus-info-set-read ',info ',(gnus-info-read info))
12718                  (gnus-get-unread-articles-in-group ',info
12719                                                     (gnus-active ,group))
12720                  (gnus-group-update-group ,group t)
12721                  ,setmarkundo))))
12722         ;; Enter this list into the group info.
12723         (gnus-info-set-read info read)
12724         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12725         (gnus-get-unread-articles-in-group info (gnus-active group))
12726         t))))
12727
12728 (defun gnus-offer-save-summaries ()
12729   "Offer to save all active summary buffers."
12730   (let (buffers)
12731     ;; Go through all buffers and find all summaries.
12732     (dolist (buffer (buffer-list))
12733       (when (and (setq buffer (buffer-name buffer))
12734                  (string-match "Summary" buffer)
12735                  (with-current-buffer buffer
12736                    ;; We check that this is, indeed, a summary buffer.
12737                    (and (derived-mode-p 'gnus-summary-mode)
12738                         ;; Also make sure this isn't bogus.
12739                         gnus-newsgroup-prepared
12740                         ;; Also make sure that this isn't a
12741                         ;; dead summary buffer.
12742                         (not gnus-dead-summary-mode))))
12743         (push buffer buffers)))
12744     ;; Go through all these summary buffers and offer to save them.
12745     (when buffers
12746       (save-excursion
12747         (if (eq gnus-interactive-exit 'quiet)
12748             (dolist (buffer buffers)
12749               (switch-to-buffer buffer)
12750               (gnus-summary-exit))
12751           (map-y-or-n-p
12752            "Update summary buffer %s? "
12753            (lambda (buf)
12754              (switch-to-buffer buf)
12755              (gnus-summary-exit))
12756            buffers))))))
12757
12758 (defun gnus-summary-setup-default-charset ()
12759   "Setup newsgroup default charset."
12760   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12761       (setq gnus-newsgroup-charset nil)
12762     (let* ((ignored-charsets
12763             (or gnus-newsgroup-ephemeral-ignored-charsets
12764                 (append
12765                  (and gnus-newsgroup-name
12766                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12767                  gnus-newsgroup-ignored-charsets))))
12768       (setq gnus-newsgroup-charset
12769             (or gnus-newsgroup-ephemeral-charset
12770                 (and gnus-newsgroup-name
12771                      (gnus-parameter-charset gnus-newsgroup-name))
12772                 gnus-default-charset))
12773       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12774            ignored-charsets))))
12775
12776 ;;;
12777 ;;; Mime Commands
12778 ;;;
12779
12780 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12781   "Display the current article buffer fully MIME-buttonized.
12782 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12783 treated as multipart/mixed."
12784   (interactive "P")
12785   (require 'gnus-art)
12786   (let ((gnus-unbuttonized-mime-types nil)
12787         (gnus-mime-display-multipart-as-mixed show-all-parts))
12788     (gnus-summary-show-article)))
12789
12790 (defun gnus-summary-repair-multipart (article)
12791   "Add a Content-Type header to a multipart article without one."
12792   (interactive (list (gnus-summary-article-number)))
12793   (gnus-with-article article
12794     (message-narrow-to-head)
12795     (message-remove-header "Mime-Version")
12796     (goto-char (point-max))
12797     (insert "Mime-Version: 1.0\n")
12798     (widen)
12799     (when (search-forward "\n--" nil t)
12800       (let ((separator (buffer-substring (point) (point-at-eol))))
12801         (message-narrow-to-head)
12802         (message-remove-header "Content-Type")
12803         (goto-char (point-max))
12804         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12805                         separator))
12806         (widen))))
12807   (let (gnus-mark-article-hook)
12808     (gnus-summary-select-article t t nil article)))
12809
12810 (defun gnus-summary-toggle-display-buttonized ()
12811   "Toggle the buttonizing of the article buffer."
12812   (interactive)
12813   (require 'gnus-art)
12814   (if (setq gnus-inhibit-mime-unbuttonizing
12815             (not gnus-inhibit-mime-unbuttonizing))
12816       (let ((gnus-unbuttonized-mime-types nil))
12817         (gnus-summary-show-article))
12818     (gnus-summary-show-article)))
12819
12820 ;;;
12821 ;;; Generic summary marking commands
12822 ;;;
12823
12824 (defvar gnus-summary-marking-alist
12825   '((read gnus-del-mark "d")
12826     (unread gnus-unread-mark "u")
12827     (ticked gnus-ticked-mark "!")
12828     (dormant gnus-dormant-mark "?")
12829     (expirable gnus-expirable-mark "e"))
12830   "An alist of names/marks/keystrokes.")
12831
12832 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12833 (defvar gnus-summary-mark-map)
12834
12835 (defun gnus-summary-make-all-marking-commands ()
12836   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12837   (dolist (elem gnus-summary-marking-alist)
12838     (apply 'gnus-summary-make-marking-command elem)))
12839
12840 (defun gnus-summary-make-marking-command (name mark keystroke)
12841   (let ((map (make-sparse-keymap)))
12842     (define-key gnus-summary-generic-mark-map keystroke map)
12843     (dolist (lway `((next "next" next nil "n")
12844                     (next-unread "next unread" next t "N")
12845                     (prev "previous" prev nil "p")
12846                     (prev-unread "previous unread" prev t "P")
12847                     (nomove "" nil nil ,keystroke)))
12848       (let ((func (gnus-summary-make-marking-command-1
12849                    mark (car lway) lway name)))
12850         (setq func (eval func))
12851         (define-key map (nth 4 lway) func)))))
12852
12853 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12854   `(defun ,(intern
12855             (format "gnus-summary-put-mark-as-%s%s"
12856                     name (if (eq way 'nomove)
12857                              ""
12858                            (concat "-" (symbol-name way)))))
12859      (n)
12860      ,(format
12861        "Mark the current article as %s%s.
12862 If N, the prefix, then repeat N times.
12863 If N is negative, move in reverse order.
12864 The difference between N and the actual number of articles marked is
12865 returned."
12866        name (cadr lway))
12867      (interactive "p")
12868      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12869
12870 (defun gnus-summary-generic-mark (n mark move unread)
12871   "Mark N articles with MARK."
12872   (unless (derived-mode-p 'gnus-summary-mode)
12873     (error "This command can only be used in the summary buffer"))
12874   (gnus-summary-show-thread)
12875   (let ((nummove
12876          (cond
12877           ((eq move 'next) 1)
12878           ((eq move 'prev) -1)
12879           (t 0))))
12880     (if (zerop nummove)
12881         (setq n 1)
12882       (when (< n 0)
12883         (setq n (abs n)
12884               nummove (* -1 nummove))))
12885     (while (and (> n 0)
12886                 (gnus-summary-mark-article nil mark)
12887                 (zerop (gnus-summary-next-subject nummove unread t)))
12888       (setq n (1- n)))
12889     (when (/= 0 n)
12890       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12891     (gnus-summary-recenter)
12892     (gnus-summary-position-point)
12893     (gnus-set-mode-line 'summary)
12894     n))
12895
12896 (defun gnus-summary-insert-articles (articles)
12897   (when (setq articles
12898               (gnus-sorted-difference articles
12899                                       (mapcar (lambda (h)
12900                                                 (mail-header-number h))
12901                                               gnus-newsgroup-headers)))
12902     (setq gnus-newsgroup-headers
12903           (gnus-merge 'list
12904                       gnus-newsgroup-headers
12905                       (gnus-fetch-headers articles nil t)
12906                       'gnus-article-sort-by-number))
12907     (setq gnus-newsgroup-articles
12908           (gnus-sorted-nunion gnus-newsgroup-articles articles))
12909     ;; Suppress duplicates?
12910     (when gnus-suppress-duplicates
12911       (gnus-dup-suppress-articles))
12912
12913     (if (and gnus-fetch-old-headers
12914              (eq gnus-headers-retrieved-by 'nov))
12915         ;; We might want to build some more threads first.
12916         (if (eq gnus-fetch-old-headers 'invisible)
12917             (gnus-build-all-threads)
12918           (gnus-build-old-threads))
12919       ;; Mark the inserted articles that are unread as unread.
12920       (setq gnus-newsgroup-unreads
12921             (gnus-sorted-nunion
12922              gnus-newsgroup-unreads
12923              (gnus-sorted-nintersection
12924               (gnus-list-of-unread-articles gnus-newsgroup-name)
12925               articles)))
12926       ;; Mark the inserted articles as selected so that the information
12927       ;; of the marks having been changed by a user may be updated when
12928       ;; exiting this group.  See `gnus-summary-update-info'.
12929       (dolist (art articles)
12930         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12931     ;; Let the Gnus agent mark articles as read.
12932     (when gnus-agent
12933       (gnus-agent-get-undownloaded-list))
12934     ;; Remove list identifiers from subject
12935     (gnus-summary-remove-list-identifiers)
12936     ;; First and last article in this newsgroup.
12937     (when gnus-newsgroup-headers
12938       (setq gnus-newsgroup-begin
12939             (mail-header-number (car gnus-newsgroup-headers))
12940             gnus-newsgroup-end
12941             (mail-header-number
12942              (gnus-last-element gnus-newsgroup-headers))))
12943     (when gnus-use-scoring
12944       (gnus-possibly-score-headers))))
12945
12946 (defun gnus-summary-insert-old-articles (&optional all)
12947   "Insert all old articles in this group.
12948 If ALL is non-nil, already read articles become readable.
12949 If ALL is a number, fetch this number of articles."
12950   (interactive "P")
12951   (prog1
12952       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12953             older len)
12954         (setq older
12955               ;; Some nntp servers lie about their active range.  When
12956               ;; this happens, the active range can be in the millions.
12957               ;; Use a compressed range to avoid creating a huge list.
12958               (gnus-range-difference
12959                (gnus-range-difference (list gnus-newsgroup-active) old)
12960                gnus-newsgroup-unexist))
12961         (setq len (gnus-range-length older))
12962         (cond
12963          ((null older) nil)
12964          ((numberp all)
12965           (if (< all len)
12966               (let ((older-range (nreverse older)))
12967                 (setq older nil)
12968
12969                 (while (> all 0)
12970                   (let* ((r (pop older-range))
12971                          (min (if (numberp r) r (car r)))
12972                          (max (if (numberp r) r (cdr r))))
12973                     (while (and (<= min max)
12974                                 (> all 0))
12975                       (push max older)
12976                       (setq all (1- all)
12977                             max (1- max))))))
12978             (setq older (gnus-uncompress-range older))))
12979          (all
12980           (setq older (gnus-uncompress-range older)))
12981          (t
12982           (when (and (numberp gnus-large-newsgroup)
12983                    (> len gnus-large-newsgroup))
12984               (let* ((cursor-in-echo-area nil)
12985                      (initial (gnus-parameter-large-newsgroup-initial
12986                                gnus-newsgroup-name))
12987                      (input
12988                       (read-string
12989                        (format
12990                         "How many articles from %s (%s %d): "
12991                         (gnus-group-decoded-name gnus-newsgroup-name)
12992                         (if initial "max" "default")
12993                         len)
12994                        nil nil
12995                        (and initial
12996                             (number-to-string initial)))))
12997                 (unless (string-match "^[ \t]*$" input)
12998                   (setq all (string-to-number input))
12999                   (if (< all len)
13000                       (let ((older-range (nreverse older)))
13001                         (setq older nil)
13002
13003                         (while (> all 0)
13004                           (let* ((r (pop older-range))
13005                                  (min (if (numberp r) r (car r)))
13006                                  (max (if (numberp r) r (cdr r))))
13007                             (while (and (<= min max)
13008                                         (> all 0))
13009                               (push max older)
13010                               (setq all (1- all)
13011                                     max (1- max))))))))))
13012           (setq older (gnus-uncompress-range older))))
13013         (if (not older)
13014             (message "No old news.")
13015           (gnus-summary-insert-articles older)
13016           (gnus-summary-limit (gnus-sorted-nunion old older))))
13017     (gnus-summary-position-point)))
13018
13019 (defun gnus-summary-insert-new-articles ()
13020   "Insert all new articles in this group."
13021   (interactive)
13022   (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
13023         (old-high gnus-newsgroup-highest)
13024         (nnmail-fetched-sources (list t))
13025         (new-active (gnus-activate-group gnus-newsgroup-name 'scan))
13026         i new)
13027     (unless new-active
13028       (error "Couldn't fetch new data"))
13029     (setq gnus-newsgroup-active (gnus-copy-sequence new-active))
13030     (setq i (cdr gnus-newsgroup-active)
13031           gnus-newsgroup-highest i)
13032     (while (> i old-high)
13033       (push i new)
13034       (decf i))
13035     (if (not new)
13036         (message "No gnus is bad news")
13037       (gnus-summary-insert-articles new)
13038       (setq gnus-newsgroup-unreads
13039             (gnus-sorted-nunion gnus-newsgroup-unreads new))
13040       (gnus-summary-limit (gnus-sorted-nunion old new))))
13041   (gnus-summary-position-point))
13042
13043 ;;; Bookmark support for Gnus.
13044 (declare-function gnus-article-show-summary "gnus-art" ())
13045 (declare-function bookmark-make-record-default
13046                   "bookmark" (&optional no-file no-context posn))
13047 (declare-function bookmark-prop-get "bookmark" (bookmark prop))
13048 (declare-function bookmark-default-handler "bookmark" (bmk))
13049 (declare-function bookmark-get-bookmark-record "bookmark" (bmk))
13050 (defvar bookmark-yank-point)
13051 (defvar bookmark-current-buffer)
13052
13053 (defun gnus-summary-bookmark-make-record ()
13054   "Make a bookmark entry for a Gnus summary buffer."
13055   (let (pos buf)
13056     (unless (and (derived-mode-p 'gnus-summary-mode) gnus-article-current)
13057       (save-restriction              ; FIXME is it necessary to widen?
13058         (widen) (setq pos (point))) ; Set position in gnus-article buffer.
13059       (setq buf "art") ; We are recording bookmark from article buffer.
13060       (setq bookmark-yank-point (point))
13061       (setq bookmark-current-buffer (current-buffer))
13062       (gnus-article-show-summary))      ; Go back in summary buffer.
13063     ;; We are now recording bookmark from summary buffer.
13064     (unless buf (setq buf "sum"))
13065     (let* ((subject (elt (gnus-summary-article-header) 1))
13066            (grp     (car gnus-article-current))
13067            (art     (cdr gnus-article-current))
13068            (head    (gnus-summary-article-header art))
13069            (id      (mail-header-id head)))
13070       `(,subject
13071         ,@(condition-case nil
13072               (bookmark-make-record-default 'no-file 'no-context pos)
13073             (wrong-number-of-arguments
13074              (bookmark-make-record-default 'point-only)))
13075         (location . ,(format "Gnus-%s %s:%d:%s" buf grp art id))
13076         (group . ,grp) (article . ,art)
13077         (message-id . ,id) (handler . gnus-summary-bookmark-jump)))))
13078
13079 ;;;###autoload
13080 (defun gnus-summary-bookmark-jump (bookmark)
13081   "Handler function for record returned by `gnus-summary-bookmark-make-record'.
13082 BOOKMARK is a bookmark name or a bookmark record."
13083   (let ((group    (bookmark-prop-get bookmark 'group))
13084         (article  (bookmark-prop-get bookmark 'article))
13085         (id       (bookmark-prop-get bookmark 'message-id))
13086         (buf      (car (split-string (bookmark-prop-get bookmark 'location)))))
13087     (gnus-fetch-group group (list article))
13088     (gnus-summary-insert-cached-articles)
13089     (gnus-summary-goto-article id nil 'force)
13090     ;; FIXME we have to wait article buffer is ready (only large buffer)
13091     ;; Is there a better solution to know that?
13092     ;; If we don't wait `bookmark-default-handler' will have no chance
13093     ;; to set position. However there is no error, just wrong pos.
13094     (sit-for 1)
13095     (when (string= buf "Gnus-art")
13096       (other-window 1))
13097     (bookmark-default-handler
13098      `(""
13099        (buffer . ,(current-buffer))
13100        . ,(bookmark-get-bookmark-record bookmark)))))
13101
13102 (gnus-summary-make-all-marking-commands)
13103
13104 (gnus-ems-redefine)
13105
13106 (provide 'gnus-sum)
13107
13108 (run-hooks 'gnus-sum-load-hook)
13109
13110 ;; Local Variables:
13111 ;; coding: utf-8
13112 ;; End:
13113
13114 ;;; gnus-sum.el ends here